1 ;;; diff-hl-margin.el --- Highlight buffer changes on margins -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;; This is a global mode, it modifies `diff-hl-mode' to use the margin
23 ;; instead of the fringe. To toggle, type `M-x diff-hl-margin-mode'.
25 ;; Compared to the default behavior, this makes `diff-hl-mode'
26 ;; indicators show up even when Emacs is running in a terminal.
28 ;; On the flip side, the indicators look simpler, and they are
29 ;; incompatible with `linum-mode' or any other mode that uses the
32 ;; You might want to enable it conditionally in your init file
33 ;; depending on whether Emacs is running in graphical mode:
35 ;; (unless (window-system) (diff-hl-margin-mode))
39 (require 'diff-hl-dired)
41 (defvar diff-hl-margin-old-highlight-function nil)
43 (defgroup diff-hl-margin nil
44 "Highlight buffer changes on margin"
47 (defcustom diff-hl-margin-side 'left
48 "Which margin to use for indicators."
49 :type '(choice (const left)
51 :set (lambda (var value)
52 (let ((on diff-hl-margin-mode))
53 (when on (diff-hl-margin-mode -1))
54 (set-default var value)
55 (when on (diff-hl-margin-mode 1)))))
58 (define-minor-mode diff-hl-margin-mode
59 "Toggle displaying `diff-hl-mode' highlights on the margin."
61 (if diff-hl-margin-mode
63 (add-hook 'diff-hl-mode-on-hook 'diff-hl-margin-minor-mode)
64 (add-hook 'diff-hl-mode-off-hook 'diff-hl-margin-minor-mode-off)
65 (add-hook 'diff-hl-dired-mode-on-hook 'diff-hl-margin-minor-mode)
66 (add-hook 'diff-hl-dired-mode-off-hook 'diff-hl-margin-minor-mode-off))
67 (remove-hook 'diff-hl-mode-on-hook 'diff-hl-margin-minor-mode)
68 (remove-hook 'diff-hl-mode-off-hook 'diff-hl-margin-minor-mode-off)
69 (remove-hook 'diff-hl-dired-mode-on-hook 'diff-hl-margin-minor-mode)
70 (remove-hook 'diff-hl-dired-mode-off-hook 'diff-hl-margin-minor-mode-off))
71 (dolist (buf (buffer-list))
72 (with-current-buffer buf
75 (diff-hl-margin-minor-mode (if diff-hl-margin-mode 1 -1))
78 (diff-hl-margin-minor-mode (if diff-hl-margin-mode 1 -1))
79 (diff-hl-dired-update))))))
81 (define-minor-mode diff-hl-margin-minor-mode
82 "Toggle displaying `diff-hl-mode' highlights on the margin locally.
83 You probably shouldn't use this function directly."
85 (let ((width-var (intern (format "%s-margin-width" diff-hl-margin-side))))
86 (if diff-hl-margin-minor-mode
88 (set (make-local-variable 'diff-hl-margin-old-highlight-function)
89 diff-hl-highlight-function)
90 (set (make-local-variable 'diff-hl-highlight-function)
91 'diff-hl-highlight-on-margin)
93 (setq diff-hl-highlight-function diff-hl-margin-old-highlight-function
94 diff-hl-margin-old-highlight-function nil)
96 (dolist (win (get-buffer-window-list))
97 (set-window-buffer win (current-buffer))))
99 (defun diff-hl-margin-minor-mode-off ()
100 (diff-hl-margin-minor-mode -1))
102 (defvar diff-hl-margin-spec-cache
103 (cl-loop for (type . char) in '((insert . "+") (delete . "-")
104 (change . "!") (unknown . "?")
107 (cl-loop for side in '(left right)
109 (cons (cons type side)
112 `((margin ,(intern (format "%s-margin" side)))
113 ,(propertize char 'face
114 (intern (format "diff-hl-%s" type)))))))))
116 (defun diff-hl-highlight-on-margin (ovl type _shape)
117 (let ((spec (cdr (assoc (cons type diff-hl-margin-side)
118 diff-hl-margin-spec-cache))))
119 (overlay-put ovl 'before-string spec)))
121 (provide 'diff-hl-margin)
123 ;;; diff-hl-margin.el ends here