Backport commit 74b8615fc from Emacs