Backport commit f2caf08ca from Emacs