Backport commit 26a308ea1 from Emacs