1 ;;; diff-hl-margin.el --- Highlight buffer changes on margins -*- lexical-binding: t -*-
3 ;; This file is not part of GNU Emacs.
5 ;; This file is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
10 ;; This file is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;; This is a global mode, it modified `diff-hl-mode' to use the margin
21 ;; instead of the fringe. To toggle, type `M-x diff-hl-margin-mode'.
23 ;; Compared to the default behavior, this makes `diff-hl-mode'
24 ;; indicators show up even when Emacs is running in a terminal.
26 ;; On the flip side, the indicators look simpler, and they are
27 ;; incompatible with `linum-mode' or any other mode that uses the
30 ;; You might want to enable it conditionally in your init file
31 ;; depending on whether Emacs is running in graphical mode:
33 ;; (unless (window-system) (diff-hl-margin-mode))
36 (require 'diff-hl-dired)
38 (defvar diff-hl-margin-old-highlight-function nil)
40 (defgroup diff-hl-margin nil
41 "Highlight buffer changes on margin"
44 (defcustom diff-hl-margin-side 'left
45 "Which margin to use for indicators."
46 :type '(choice (const left)
48 :set (lambda (var value)
49 (let ((on diff-hl-margin-mode))
50 (when on (diff-hl-margin-mode -1))
51 (set-default var value)
52 (when on (diff-hl-margin-mode 1)))))
55 (define-minor-mode diff-hl-margin-mode
56 "Toggle displaying `diff-hl-mode' highlights on the margin."
58 (let ((width-var (intern (format "%s-margin-width" diff-hl-margin-side))))
59 (if diff-hl-margin-mode
61 (setq diff-hl-margin-old-highlight-function diff-hl-highlight-function
62 diff-hl-highlight-function 'diff-hl-highlight-on-margin)
63 (set-default width-var 1))
64 (setq diff-hl-highlight-function diff-hl-margin-old-highlight-function
65 diff-hl-margin-old-highlight-function nil)
66 (set-default width-var 0)))
67 (dolist (buffer (buffer-list))
68 (with-current-buffer buffer
73 (diff-hl-dired-update)))))
74 (walk-windows (lambda (win) (set-window-buffer win (window-buffer win)))))
76 (defvar diff-hl-margin-spec-cache
77 (loop for (type . char) in '((insert . "+") (delete . "-")
78 (change . "|") (unknown . "?"))
80 (loop for side in '(left right)
82 (cons (cons type side)
85 `((margin ,(intern (format "%s-margin" side)))
86 ,(propertize char 'face
87 (intern (format "diff-hl-%s" type)))))))))
89 (defun diff-hl-highlight-on-margin (ovl type _shape)
90 (let ((spec (cdr (assoc (cons type diff-hl-margin-side)
91 diff-hl-margin-spec-cache))))
92 (overlay-put ovl 'before-string spec)))
94 (provide 'diff-hl-margin)
96 ;;; diff-hl-margin.el ends here