Backport commit 9c3883b from Emacs master branch