]> code.delx.au - gnu-emacs-elpa/blobdiff - diff-hl-margin.el
Require nadvice at toplevel
[gnu-emacs-elpa] / diff-hl-margin.el
index 57a0b703bc894e10067f89514a192dd70204aab9..32aa1237e91026fc73c9e9f1a388ca0596d26501 100644 (file)
@@ -1,13 +1,15 @@
 ;;; diff-hl-margin.el --- Highlight buffer changes on margins -*- lexical-binding: t -*-
 
-;; This file is not part of GNU Emacs.
+;; Copyright (C) 2012-2015  Free Software Foundation, Inc.
 
-;; This file is free software: you can redistribute it and/or modify
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation, either version 3 of the License, or
 ;; (at your option) any later version.
 
-;; This file is distributed in the hope that it will be useful,
+;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
@@ -17,7 +19,7 @@
 
 ;;; Commentary:
 
-;; This is a global mode, it modified `diff-hl-mode' to use the margin
+;; This is a global mode, it modifies `diff-hl-mode' to use the margin
 ;; instead of the fringe. To toggle, type `M-x diff-hl-margin-mode'.
 ;;
 ;; Compared to the default behavior, this makes `diff-hl-mode'
 ;;
 ;; (unless (window-system) (diff-hl-margin-mode))
 
+(require 'cl-lib)
 (require 'diff-hl)
 (require 'diff-hl-dired)
 
 (defvar diff-hl-margin-old-highlight-function nil)
 
+(defgroup diff-hl-margin nil
+  "Highlight buffer changes on margin"
+  :group 'diff-hl)
+
 ;;;###autoload
 (define-minor-mode diff-hl-margin-mode
   "Toggle displaying `diff-hl-mode' highlights on the margin."
   :lighter "" :global t
   (if diff-hl-margin-mode
       (progn
-        (setq diff-hl-margin-old-highlight-function diff-hl-highlight-function
-              diff-hl-highlight-function 'diff-hl-highlight-on-margin)
-        (setq-default left-margin-width 1)
-        (dolist (buffer (buffer-list))
-          (with-current-buffer buffer
-            (cond
-             (diff-hl-mode
-              (diff-hl-update))
-             (diff-hl-dired-mode
-              (diff-hl-dired-update))))))
-    (setq diff-hl-highlight-function diff-hl-margin-old-highlight-function
-          diff-hl-margin-old-highlight-function nil)
-    (setq-default left-margin-width 0)
-    (dolist (buffer (buffer-list))
-      (with-current-buffer buffer
-        (cond
-         (diff-hl-mode
-          (diff-hl-update))
-         (diff-hl-dired-mode
-          (diff-hl-dired-update))))))
-  (walk-windows (lambda (win) (set-window-buffer win (window-buffer win)))))
+        (add-hook 'diff-hl-mode-on-hook 'diff-hl-margin-minor-mode)
+        (add-hook 'diff-hl-mode-off-hook 'diff-hl-margin-minor-mode-off)
+        (add-hook 'diff-hl-dired-mode-on-hook 'diff-hl-margin-minor-mode)
+        (add-hook 'diff-hl-dired-mode-off-hook 'diff-hl-margin-minor-mode-off))
+    (remove-hook 'diff-hl-mode-on-hook 'diff-hl-margin-minor-mode)
+    (remove-hook 'diff-hl-mode-off-hook 'diff-hl-margin-minor-mode-off)
+    (remove-hook 'diff-hl-dired-mode-on-hook 'diff-hl-margin-minor-mode)
+    (remove-hook 'diff-hl-dired-mode-off-hook 'diff-hl-margin-minor-mode-off))
+  (dolist (buf (buffer-list))
+    (with-current-buffer buf
+      (cond
+       (diff-hl-mode
+        (diff-hl-margin-minor-mode (if diff-hl-margin-mode 1 -1))
+        (diff-hl-update))
+       (diff-hl-dired-mode
+        (diff-hl-margin-minor-mode (if diff-hl-margin-mode 1 -1))
+        (diff-hl-dired-update))))))
+
+(define-minor-mode diff-hl-margin-minor-mode
+  "Toggle displaying `diff-hl-mode' highlights on the margin locally.
+You probably shouldn't use this function directly."
+  :lighter ""
+  (let ((width-var (intern (format "%s-margin-width" diff-hl-side))))
+    (if diff-hl-margin-minor-mode
+        (progn
+          (set (make-local-variable 'diff-hl-margin-old-highlight-function)
+               diff-hl-highlight-function)
+          (set (make-local-variable 'diff-hl-highlight-function)
+               'diff-hl-highlight-on-margin)
+          (set width-var 1))
+      (setq diff-hl-highlight-function diff-hl-margin-old-highlight-function
+            diff-hl-margin-old-highlight-function nil)
+      (set width-var 0)))
+  (dolist (win (get-buffer-window-list))
+    (set-window-buffer win (current-buffer))))
+
+(define-obsolete-variable-alias 'diff-hl-margin-side 'diff-hl-side "1.7.1")
+
+(defun diff-hl-margin-minor-mode-off ()
+  (diff-hl-margin-minor-mode -1))
 
 (defvar diff-hl-margin-spec-cache
-  (loop for (type . char) in '((insert . "+") (delete . "-")
-                               (change . "|") (unknown . "?"))
-        collect (cons type
-                      (propertize
-                       " " 'display
-                       `((margin left-margin)
-                         ,(propertize char 'face
-                                      (intern (format "diff-hl-%s" type))))))))
+  (cl-loop for (type . char) in '((insert . "+") (delete . "-")
+                                  (change . "!") (unknown . "?")
+                                  (ignored . "i"))
+        nconc
+        (cl-loop for side in '(left right)
+                 collect
+                 (cons (cons type side)
+                       (propertize
+                        " " 'display
+                        `((margin ,(intern (format "%s-margin" side)))
+                          ,(propertize char 'face
+                                       (intern (format "diff-hl-%s" type)))))))))
 
 (defun diff-hl-highlight-on-margin (ovl type _shape)
-  (let ((spec (cdr (assoc type diff-hl-margin-spec-cache))))
+  (let ((spec (cdr (assoc (cons type diff-hl-side)
+                          diff-hl-margin-spec-cache))))
     (overlay-put ovl 'before-string spec)))
 
 (provide 'diff-hl-margin)