Backport commit c92bae9a0 from Emacs