Backport commit 1203e8a from Emacs