Backport commit 0fda6d53e from Emacs