From ebaae4480eecb7e4757296d0a2b776ad0b960be8 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Tue, 10 Aug 2021 16:40:58 +0200 Subject: [PATCH] Add a new command `revert-buffer-quick' * doc/emacs/files.texi (Reverting): Document it. * lisp/bindings.el (ctl-x-x-map): Bind `C-x x g' to `revert-buffer-quick' instead. * lisp/files.el (revert-buffer-quick-short-answers): New user option. (revert-buffer-quick): New command (bug#49869). --- doc/emacs/files.texi | 13 ++++++++++++- etc/NEWS | 12 +++++++++++- lisp/bindings.el | 2 +- lisp/files.el | 31 +++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi index 7edf4d2bbbf..8304e40706a 100644 --- a/doc/emacs/files.texi +++ b/doc/emacs/files.texi @@ -948,7 +948,7 @@ Manual}). For customizations, see the Custom group @code{time-stamp}. then change your mind, you can @dfn{revert} the changes and go back to the saved version of the file. To do this, type @kbd{C-x x g}. Since reverting unintentionally could lose a lot of work, Emacs asks for -confirmation first. +confirmation first if the buffer is modified. The @code{revert-buffer} command tries to position point in such a way that, if the file was edited only slightly, you will be at @@ -991,6 +991,17 @@ revert it automatically if it has changed---provided the buffer itself is not modified. (If you have edited the text, it would be wrong to discard your changes.) +@vindex revert-buffer-quick-short-answers +@findex revert-buffer-quick + The @kbd{C-x x g} keystroke is bound to the +@code{revert-buffer-quick} command. This is like the +@code{revert-buffer} command, but prompts less. Unlike +@code{revert-buffer}, it will not prompt if the current buffer visits +a file, and the buffer is not modified. It also respects the +@code{revert-buffer-quick-short-answers} user option. If this option +is non-@code{nil}, use a shorter @kbd{y/n} query instead of a longer +@kbd{yes/no} query. + You can also tell Emacs to revert buffers automatically when their visited files change on disk; @pxref{Auto Revert}. diff --git a/etc/NEWS b/etc/NEWS index 378a32e2d35..34e4cd73a7b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -310,7 +310,7 @@ search buffer due to too many matches being highlighted. +++ ** A new keymap for buffer actions has been added. The 'C-x x' keymap now holds keystrokes for various buffer-oriented -commands. The new keystrokes are 'C-x x g' ('revert-buffer'), +commands. The new keystrokes are 'C-x x g' ('revert-buffer-quick'), 'C-x x r' ('rename-buffer'), 'C-x x u' ('rename-uniquely'), 'C-x x n' ('clone-buffer'), 'C-x x i' ('insert-buffer'), 'C-x x t' ('toggle-truncate-lines') and 'C-x x f' ('font-lock-update'). @@ -2394,6 +2394,16 @@ This command, called interactively, toggles the local value of ** Miscellaneous ++++ +*** New command 'revert-buffer-quick'. +This is bound to 'C-x x g' and is like `revert-buffer', but prompts +less. + ++++ +*** New user option 'revert-buffer-quick-short-answers'. This +controls how the new 'revert-buffer-quick' (`C-x x g') command +prompts. + --- *** fileloop will now skip missing files instead of signalling an error. diff --git a/lisp/bindings.el b/lisp/bindings.el index 8e5799fbe88..03459448943 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -1469,7 +1469,7 @@ if `inhibit-field-text-motion' is non-nil." (defvar ctl-x-x-map (let ((map (make-sparse-keymap))) (define-key map "f" #'font-lock-update) - (define-key map "g" #'revert-buffer) + (define-key map "g" #'revert-buffer-quick) (define-key map "r" #'rename-buffer) (define-key map "u" #'rename-uniquely) (define-key map "n" #'clone-buffer) diff --git a/lisp/files.el b/lisp/files.el index 6c366177ce8..dffce2b2474 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -6561,6 +6561,37 @@ details on the arguments, see `revert-buffer'." (revert-buffer-with-fine-grain-success-p) (fmakunbound 'revert-buffer-with-fine-grain-success-p))))) +(defcustom revert-buffer-quick-short-answers nil + "How much confirmation to be done by the `revert-buffer-quit' command. +If non-nil, use `y-or-n-p' instead of `yes-or-no-p'." + :version "28.1" + :type 'boolean) + +(defun revert-buffer-quick (&optional auto-save) + "Like `revert-buffer', but asks for less confirmation. +If the current buffer is visiting a file, and the buffer is not +modified, no confirmation is required. + +This command heeds the `revert-buffer-quick-short-answers' user option. + +If AUTO-SAVE (the prefix argument), offer to revert from latest +auto-save file, if that is more recent than the visited file." + (interactive "P") + (cond + ;; If we've visiting a file, and we have no changes, don't ask for + ;; confirmation. + ((and buffer-file-name + (not (buffer-modified-p))) + (revert-buffer (not auto-save) t) + (message "Reverted buffer")) + ;; Heed `revert-buffer-quick-short-answers'. + (revert-buffer-quick-short-answers + (let ((use-short-answers t)) + (revert-buffer (not auto-save)))) + ;; Call `revert-buffer' normally. + (t + (revert-buffer (not auto-save))))) + (defun recover-this-file () "Recover the visited file--get contents from its last auto-save file." (interactive) -- 2.39.5