]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-margin.el
diff-hl-margin-mode: Walk buffers outside of the if form
[gnu-emacs-elpa] / diff-hl-margin.el
1 ;;; diff-hl-margin.el --- Highlight buffer changes on margins -*- lexical-binding: t -*-
2
3 ;; This file is not part of GNU Emacs.
4
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.
9
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.
14
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/>.
17
18 ;;; Commentary:
19
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'.
22 ;;
23 ;; Compared to the default behavior, this makes `diff-hl-mode'
24 ;; indicators show up even when Emacs is running in a terminal.
25 ;;
26 ;; On the flip side, the indicators look simpler, and they are
27 ;; incompatible with `linum-mode' or any other mode that uses the
28 ;; margin.
29 ;;
30 ;; You might want to enable it conditionally in your init file
31 ;; depending on whether Emacs is running in graphical mode:
32 ;;
33 ;; (unless (window-system) (diff-hl-margin-mode))
34
35 (require 'diff-hl)
36 (require 'diff-hl-dired)
37
38 (defvar diff-hl-margin-old-highlight-function nil)
39
40 ;;;###autoload
41 (define-minor-mode diff-hl-margin-mode
42 "Toggle displaying `diff-hl-mode' highlights on the margin."
43 :lighter "" :global t
44 (if diff-hl-margin-mode
45 (progn
46 (setq diff-hl-margin-old-highlight-function diff-hl-highlight-function
47 diff-hl-highlight-function 'diff-hl-highlight-on-margin)
48 (setq-default left-margin-width 1))
49 (setq diff-hl-highlight-function diff-hl-margin-old-highlight-function
50 diff-hl-margin-old-highlight-function nil)
51 (setq-default left-margin-width 0))
52 (dolist (buffer (buffer-list))
53 (with-current-buffer buffer
54 (cond
55 (diff-hl-mode
56 (diff-hl-update))
57 (diff-hl-dired-mode
58 (diff-hl-dired-update)))))
59 (walk-windows (lambda (win) (set-window-buffer win (window-buffer win)))))
60
61 (defvar diff-hl-margin-spec-cache
62 (loop for (type . char) in '((insert . "+") (delete . "-")
63 (change . "|") (unknown . "?"))
64 collect (cons type
65 (propertize
66 " " 'display
67 `((margin left-margin)
68 ,(propertize char 'face
69 (intern (format "diff-hl-%s" type))))))))
70
71 (defun diff-hl-highlight-on-margin (ovl type _shape)
72 (let ((spec (cdr (assoc type diff-hl-margin-spec-cache))))
73 (overlay-put ovl 'before-string spec)))
74
75 (provide 'diff-hl-margin)
76
77 ;;; diff-hl-margin.el ends here