Backport commit 3a5f751 from Emacs master branch