Backport commit ce557a924 from Emacs