]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-margin.el
Fix byte-compilation warnings
[gnu-emacs-elpa] / diff-hl-margin.el
1 ;;; diff-hl-margin.el --- Highlight buffer changes on margins -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Commentary:
21
22 ;; This is a global mode, it modified `diff-hl-mode' to use the margin
23 ;; instead of the fringe. To toggle, type `M-x diff-hl-margin-mode'.
24 ;;
25 ;; Compared to the default behavior, this makes `diff-hl-mode'
26 ;; indicators show up even when Emacs is running in a terminal.
27 ;;
28 ;; On the flip side, the indicators look simpler, and they are
29 ;; incompatible with `linum-mode' or any other mode that uses the
30 ;; margin.
31 ;;
32 ;; You might want to enable it conditionally in your init file
33 ;; depending on whether Emacs is running in graphical mode:
34 ;;
35 ;; (unless (window-system) (diff-hl-margin-mode))
36
37 (require 'diff-hl)
38 (require 'diff-hl-dired)
39
40 (defvar diff-hl-margin-old-highlight-function nil)
41
42 (defgroup diff-hl-margin nil
43 "Highlight buffer changes on margin"
44 :group 'diff-hl)
45
46 (defcustom diff-hl-margin-side 'left
47 "Which margin to use for indicators."
48 :type '(choice (const left)
49 (const right))
50 :set (lambda (var value)
51 (let ((on diff-hl-margin-mode))
52 (when on (diff-hl-margin-mode -1))
53 (set-default var value)
54 (when on (diff-hl-margin-mode 1)))))
55
56 ;;;###autoload
57 (define-minor-mode diff-hl-margin-mode
58 "Toggle displaying `diff-hl-mode' highlights on the margin."
59 :lighter "" :global t
60 (let ((width-var (intern (format "%s-margin-width" diff-hl-margin-side))))
61 (if diff-hl-margin-mode
62 (progn
63 (setq diff-hl-margin-old-highlight-function diff-hl-highlight-function
64 diff-hl-highlight-function 'diff-hl-highlight-on-margin)
65 (set-default width-var 1))
66 (setq diff-hl-highlight-function diff-hl-margin-old-highlight-function
67 diff-hl-margin-old-highlight-function nil)
68 (set-default width-var 0)))
69 (dolist (buffer (buffer-list))
70 (with-current-buffer buffer
71 (cond
72 (diff-hl-mode
73 (diff-hl-update))
74 (diff-hl-dired-mode
75 (diff-hl-dired-update)))))
76 (walk-windows (lambda (win) (set-window-buffer win (window-buffer win)))))
77
78 (defvar diff-hl-margin-spec-cache
79 (loop for (type . char) in '((insert . "+") (delete . "-")
80 (change . "|") (unknown . "?"))
81 nconc
82 (loop for side in '(left right)
83 collect
84 (cons (cons type side)
85 (propertize
86 " " 'display
87 `((margin ,(intern (format "%s-margin" side)))
88 ,(propertize char 'face
89 (intern (format "diff-hl-%s" type)))))))))
90
91 (defun diff-hl-highlight-on-margin (ovl type _shape)
92 (let ((spec (cdr (assoc (cons type diff-hl-margin-side)
93 diff-hl-margin-spec-cache))))
94 (overlay-put ovl 'before-string spec)))
95
96 (provide 'diff-hl-margin)
97
98 ;;; diff-hl-margin.el ends here