Backport commit 52a4e87 from Emacs master branch