Backport commit 7e09ef0 from Emacs master branch