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