Backport commit 2999341 from Emacs master branch