Backport commit 265a5d979 from Emacs