Backport commit 26d58f0c5 from Emacs