Backport commit 7d90d2ece from Emacs