]> git.eshelyaron.com Git - emacs.git/commitdiff
Add diff-mode-read-only to enable shorter keys in diff-mode
authorJuri Linkov <juri@linkov.net>
Mon, 10 Jan 2022 18:29:53 +0000 (20:29 +0200)
committerJuri Linkov <juri@linkov.net>
Mon, 10 Jan 2022 18:29:53 +0000 (20:29 +0200)
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
lisp/vc/vc-git.el
lisp/vc/vc.el

index ca8df5d3805a54a937c83f76354bd57aa66f70df..37eaf254fdba5b1e930b115009c7a54060079c7e 100644 (file)
@@ -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
index 2e6690c537608155aeef4cdc9913bf6d6d43a736..ad39dc604a9e9b0b7783418061a0f81cc8d3fb0f 100644 (file)
@@ -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)
index 6041c79efca1aa443c5a508d9cd6dbe635d9fc84..6f5c2f0934ce4e7602b3d6fbc1a0f9bf7c4b14f8 100644 (file)
@@ -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.