Backport commit 07ea5ef99 from Emacs