From 97806bc7deeec7ab1458eb5ff689f46ec8e7c652 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Mon, 10 Jan 2022 20:29:53 +0200 Subject: [PATCH] Add diff-mode-read-only to enable shorter keys in diff-mode To be able to use outline-minor-mode-cycle keys in diff-mode, outline-minor-mode should be higher in minor-mode-map-alist, and a new variable diff-mode-read-only should be set when the diff-mode buffer is read-only. https://lists.gnu.org/archive/html/emacs-devel/2022-01/msg00740.html * lisp/vc/diff-mode.el: Add diff-mode-read-only to minor-mode-map-alist. (diff-mode-read-only): New buffer-local variable. (diff-mode): Set diff-mode-read-only to t when buffer-read-only is non-nil. Update diff-mode-read-only in read-only-mode-hook. Remove buffer-read-only from minor-mode-overriding-map-alist. Remove view-mode-hook because now view-mode is higher than diff-mode-read-only in minor-mode-map-alist. * lisp/vc/vc-git.el (vc-git-stash-show): * lisp/vc/vc.el (vc-diff-internal): Set buffer-read-only to t before calling diff-mode that will set diff-mode-read-only. --- lisp/vc/diff-mode.el | 30 +++++++++++++++++++----------- lisp/vc/vc-git.el | 2 +- lisp/vc/vc.el | 8 ++++---- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index ca8df5d3805..37eaf254fdb 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -1479,6 +1479,14 @@ See `after-change-functions' for the meaning of BEG, END and LEN." (defvar whitespace-style) (defvar whitespace-trailing-regexp) +(defvar-local diff-mode-read-only nil + "Non-nil when read-only diff buffer uses short keys.") + +;; It should be lower than `outline-minor-mode' and `view-mode'. +(or (assq 'diff-mode-read-only minor-mode-map-alist) + (nconc minor-mode-map-alist + (list (cons 'diff-mode-read-only diff-mode-shared-map)))) + ;;;###autoload (define-derived-mode diff-mode fundamental-mode "Diff" "Major mode for viewing/editing context diffs. @@ -1516,23 +1524,23 @@ a diff with \\[diff-reverse-direction]. (diff-setup-whitespace) - (if diff-default-read-only - (setq buffer-read-only t)) + ;; read-only setup + (when diff-default-read-only + (setq buffer-read-only t)) + (when buffer-read-only + (setq diff-mode-read-only t)) + (add-hook 'read-only-mode-hook + (lambda () + (setq diff-mode-read-only buffer-read-only)) + nil t) + ;; setup change hooks (if (not diff-update-on-the-fly) (add-hook 'write-contents-functions #'diff-write-contents-hooks nil t) (make-local-variable 'diff-unhandled-changes) (add-hook 'after-change-functions #'diff-after-change-function nil t) (add-hook 'post-command-hook #'diff-post-command-hook nil t)) - ;; Neat trick from Dave Love to add more bindings in read-only mode: - (let ((ro-bind (cons 'buffer-read-only diff-mode-shared-map))) - (add-to-list 'minor-mode-overriding-map-alist ro-bind) - ;; Turn off this little trick in case the buffer is put in view-mode. - (add-hook 'view-mode-hook - (lambda () - (setq minor-mode-overriding-map-alist - (delq ro-bind minor-mode-overriding-map-alist))) - nil t)) + ;; add-log support (setq-local add-log-current-defun-function #'diff-current-defun) (setq-local add-log-buffer-file-name-function diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el index 2e6690c5376..ad39dc604a9 100644 --- a/lisp/vc/vc-git.el +++ b/lisp/vc/vc-git.el @@ -1695,8 +1695,8 @@ This command shares argument histories with \\[rgrep] and \\[grep]." (vc-setup-buffer "*vc-git-stash*") (vc-git-command "*vc-git-stash*" 'async nil "stash" "show" "-p" name) (set-buffer "*vc-git-stash*") - (diff-mode) (setq buffer-read-only t) + (diff-mode) (pop-to-buffer (current-buffer))) (defun vc-git-stash-apply (name) diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el index 6041c79efca..6f5c2f0934c 100644 --- a/lisp/vc/vc.el +++ b/lisp/vc/vc.el @@ -1808,16 +1808,16 @@ Return t if the buffer had changes, nil otherwise." (setq files (nreverse filtered)))) (vc-call-backend (car vc-fileset) 'diff files rev1 rev2 buffer async) (set-buffer buffer) + ;; Make the *vc-diff* buffer read only, the diff-mode key + ;; bindings are nicer for read only buffers. pcl-cvs does the + ;; same thing. + (setq buffer-read-only t) (diff-mode) (setq-local diff-vc-backend (car vc-fileset)) (setq-local diff-vc-revisions (list rev1 rev2)) (setq-local revert-buffer-function (lambda (_ignore-auto _noconfirm) (vc-diff-internal async vc-fileset rev1 rev2 verbose))) - ;; Make the *vc-diff* buffer read only, the diff-mode key - ;; bindings are nicer for read only buffers. pcl-cvs does the - ;; same thing. - (setq buffer-read-only t) (if (and (zerop (buffer-size)) (not (get-buffer-process (current-buffer)))) ;; Treat this case specially so as not to pop the buffer.