Backport commit 753fbfb from Emacs master branch