Backport commit 7aa506e from Emacs master branch