]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-margin.el
Bump the version, add .elpaignore, drop Makefile
[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 (defgroup diff-hl-margin nil
41 "Highlight buffer changes on margin"
42 :group 'diff-hl)
43
44 (defcustom diff-hl-margin-side 'left
45 "Which margin to use for indicators."
46 :type '(choice (const left)
47 (const right))
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)))))
53
54 ;;;###autoload
55 (define-minor-mode diff-hl-margin-mode
56 "Toggle displaying `diff-hl-mode' highlights on the margin."
57 :lighter "" :global t
58 (let ((width-var (intern (format "%s-margin-width" diff-hl-margin-side))))
59 (if diff-hl-margin-mode
60 (progn
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
69 (cond
70 (diff-hl-mode
71 (diff-hl-update))
72 (diff-hl-dired-mode
73 (diff-hl-dired-update)))))
74 (walk-windows (lambda (win) (set-window-buffer win (window-buffer win)))))
75
76 (defvar diff-hl-margin-spec-cache
77 (loop for (type . char) in '((insert . "+") (delete . "-")
78 (change . "|") (unknown . "?"))
79 nconc
80 (loop for side in '(left right)
81 collect
82 (cons (cons type side)
83 (propertize
84 " " 'display
85 `((margin ,(intern (format "%s-margin" side)))
86 ,(propertize char 'face
87 (intern (format "diff-hl-%s" type)))))))))
88
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)))
93
94 (provide 'diff-hl-margin)
95
96 ;;; diff-hl-margin.el ends here