569b34a20d64c368af520620dc7abef45baba421
[visible-mark] / visible-mark.el
1 ;;; visible-mark.el --- Make marks visible.
2
3 ;; Copyright (C) 2014 by Ian Kelling
4
5 ;; Maintainer: Ian Kelling <ian@iankelling.org>
6 ;; Author: Ian Kelling <ian@iankelling.org>
7 ;; Author: Yann Hodique
8 ;; Author: MATSUYAMA Tomohiro <t.matsuyama.pub@gmail.com>
9 ;; Author: John Foerch <jjfoerch@earthlink.net>
10 ;; Keywords: marking color faces
11 ;; URL: https://gitlab.com/iankelling/visible-mark
12 ;; Created: 2008-02-21
13
14 ;;; License:
15
16 ;; This program is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
20
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28
29
30 ;;; Commentary:
31
32 ;; Emacs minor mode to highlight mark(s).
33 ;;
34 ;; Allows setting the number of marks to display, and the faces to display them.
35 ;;
36 ;; Example installation:
37 ;;
38 ;; 1. Put this file in Emacs's load-path
39 ;;
40 ;; 2. add custom faces to init file
41 ;; (require 'visible-mark)
42 ;; (global-visible-mark-mode 1) ;; or add (visible-mark-mode) to specific hooks
43 ;;
44 ;; 3. Add customizations. The defaults are very minimal. They could also be set
45 ;; via customize.
46 ;;
47 ;; (defface visible-mark-active ;; put this before (require 'visible-mark)
48 ;; '((((type tty) (class mono)))
49 ;; (t (:background "magenta"))) "")
50 ;; (setq visible-mark-max 2)
51 ;; (setq visible-mark-faces `(visible-mark-face1 my-visible-mark-face2))
52 ;;
53 ;;
54 ;; Additional useful functions like unpoping the mark are at
55 ;; http://www.emacswiki.org/emacs/MarkCommands
56 ;; and http://www.emacswiki.org/emacs/VisibleMark
57
58 ;; Known bugs
59 ;;
60 ;; Observed in circe, when the buffer has a right margin, and there
61 ;; is a mark at the beginning of a line, any text in the margin on that line
62 ;; gets styled with the mark's face. May also happen for left margins, but
63 ;; haven't tested yet.
64 ;;
65 ;; Patches / pull requests welcome.
66
67 ;;; Code:
68
69 (eval-when-compile
70 (require 'cl))
71
72 (defgroup visible-mark nil
73 "Show the position of your mark."
74 :group 'convenience
75 :prefix "visible-mark-")
76
77 (defface visible-mark-active
78 '((((type tty) (class color))
79 (:background "gray" :foreground "black"))
80 (((type tty) (class mono))
81 (:inverse-video t))
82 (((class color) (background dark))
83 (:background "gray" :foreground "black"))
84 (((class color) (background light))
85 (:background "grey80"))
86 (t (:background "gray")))
87 "Face for the active mark. To redefine this in your init file,
88 do it before loading/requiring visible-mark."
89 :group 'visible-mark)
90
91 (defcustom visible-mark-inhibit-trailing-overlay t
92 "If non-nil, inhibit the extension of an overlay at the end of a line
93 to the window margin."
94 :group 'visible-mark
95 :type 'boolean)
96
97 (defcustom global-visible-mark-mode-exclude-alist nil
98 "A list of buffer names to be excluded."
99 :group 'visible-mark
100 :type '(repeat regexp))
101
102
103 (defcustom visible-mark-max 1
104 "The number of marks in the backward direction to be visible."
105 :group 'visible-mark
106 :type 'integer)
107
108 (defcustom visible-mark-forward-max 0
109 "The number of marks in the forward direction to be visible."
110 :group 'visible-mark
111 :type 'integer)
112
113 (defcustom visible-mark-faces nil
114 "A list of mark faces for marks in the backward direction.
115 If visible-mark-max is greater than the amount of visible-mark-faces,
116 the last defined face will be reused."
117 :group 'visible-mark
118 :type '(repeat face))
119
120 (defcustom visible-mark-forward-faces nil
121 "A list of mark faces for marks in the forward direction."
122 :group 'visible-mark
123 :type '(repeat face))
124
125
126 ;;; example faces
127
128 (defface visible-mark-face1
129 '((((type tty) (class mono)))
130 (t (:background "light salmon")))
131 "Example face which can be customized and added to subsequent face lists."
132 :group 'visible-mark)
133
134 (defface visible-mark-face2
135 '((((type tty) (class mono)))
136 (t (:background "light goldenrod")))
137 "Example face which can be customized and added to subsequent face lists."
138 :group 'visible-mark)
139
140 (defface visible-mark-forward-face1
141 '((((type tty) (class mono)))
142 (t (:background "pale green")))
143 "Example face which can be customized and added to subsequent face lists."
144 :group 'visible-mark)
145
146 (defface visible-mark-forward-face2
147 nil
148 "Placeholder face for customization and addition to subsequent face lists."
149 :group 'visible-mark)
150
151
152
153
154 (defvar visible-mark-overlays nil
155 "The overlays used for mark faces. Used internally by visible-mark-mode.")
156 (make-variable-buffer-local 'visible-mark-overlays)
157
158
159
160 (defun visible-mark-initialize-overlays ()
161 (mapc
162 (lambda (x)
163 (when (eq 'visible-mark (overlay-get x 'category))
164 (delete-overlay x)))
165 (overlays-in (point-min) (point-max)))
166 (let (overlays)
167 (dotimes (i (+ visible-mark-max visible-mark-forward-max))
168 (let ((overlay (make-overlay (point-min) (point-min))))
169 (overlay-put overlay 'category 'visible-mark)
170 (push overlay overlays)))
171 (setq visible-mark-overlays (nreverse overlays))))
172
173 (defun visible-mark-find-overlay-at (pos)
174 (let ((overlays (overlays-at pos))
175 found)
176 (while (and overlays (not found))
177 (let ((overlay (car overlays)))
178 (if (eq 'visible-mark (overlay-get overlay 'category))
179 (setq found overlay)))
180 (setq overlays (cdr overlays)))
181 found))
182
183 (defun visible-mark-move-overlays ()
184 "Update overlays in `visible-mark-overlays'. This is run in the `post-command-hook'"
185 (mapc (lambda (x) (delete-overlay x))
186 visible-mark-overlays)
187 (let ((marks (cons (mark-marker) mark-ring))
188 (overlays visible-mark-overlays)
189 (faces visible-mark-faces)
190 (faces-forward visible-mark-forward-faces))
191 (if mark-active (setq faces (cons 'visible-mark-active (cdr faces))))
192 (dotimes (i visible-mark-max)
193 (visible-mark-move-overlay (pop overlays) (pop marks) (car faces))
194 (if (cdr faces) (pop faces)))
195 (dotimes (i visible-mark-forward-max)
196 (visible-mark-move-overlay (pop overlays) (car (last marks (1+ i))) (car faces-forward))
197 (if (cdr faces-forward) (pop faces-forward)))))
198
199 (defun visible-mark-move-overlay (overlay mark face)
200 "Set OVERLAY to position of MARK and display of FACE."
201 (let ((pos (and mark (marker-position mark))))
202 (when (and pos (not (equal (point) pos)))
203 (cond
204 ((and
205 visible-mark-inhibit-trailing-overlay
206 (save-excursion (goto-char pos) (eolp)))
207 (overlay-put overlay 'face nil)
208 (if (visible-mark-find-overlay-at pos)
209 (progn (overlay-put overlay 'before-string nil))
210 (overlay-put overlay 'before-string
211 (propertize
212 " "
213 'face face))
214 (move-overlay overlay pos (1+ pos))))
215 (t
216 (overlay-put overlay 'before-string nil)
217 (overlay-put overlay 'face face)
218 (move-overlay overlay pos (1+ pos)))))))
219
220 (require 'easy-mmode)
221 (defun visible-mark-mode-maybe ()
222 (when (cond
223 ((minibufferp (current-buffer)) nil)
224 ((cl-flet ((fun (arg)
225 (if (null arg) nil
226 (or (string-match (car arg) (buffer-name))
227 (fun (cdr arg))))))
228 (fun global-visible-mark-mode-exclude-alist)) nil)
229 (t t))
230 (visible-mark-mode t)))
231
232 ;;;###autoload
233 (define-minor-mode visible-mark-mode
234 "A mode to make the mark visible."
235 nil nil nil
236 :group 'visible-mark
237 (if visible-mark-mode
238 (progn
239 (visible-mark-initialize-overlays)
240 (add-hook 'post-command-hook 'visible-mark-move-overlays nil t))
241 (mapc 'delete-overlay visible-mark-overlays)
242 (setq visible-mark-overlays nil)
243 (remove-hook 'post-command-hook 'visible-mark-move-overlays t)))
244
245 ;;;###autoload
246 (define-global-minor-mode
247 global-visible-mark-mode visible-mark-mode visible-mark-mode-maybe
248 :group 'visible-mark)
249
250 (provide 'visible-mark)
251 ;;; visible-mark.el ends here