1 ;;; visible-mark.el --- Make marks visible.
3 ;; Copyright (C) 2014 by Ian Kelling
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
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.
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.
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/>.
32 ;; Emacs minor mode to highlight mark(s).
34 ;; Allows setting the number of marks to display, and the faces to display them.
36 ;; Example installation:
38 ;; 1. Put this file in Emacs's load-path
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
44 ;; 3. Add customizations. The defaults are very minimal. They could also be set
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))
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
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.
65 ;; Patches / pull requests welcome.
72 (defgroup visible-mark nil
73 "Show the position of your mark."
75 :prefix "visible-mark-")
77 (defface visible-mark-active
78 '((((type tty) (class color))
79 (:background "gray" :foreground "black"))
80 (((type tty) (class mono))
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."
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."
97 (defcustom global-visible-mark-mode-exclude-alist nil
98 "A list of buffer names to be excluded."
100 :type '(repeat regexp))
103 (defcustom visible-mark-max 1
104 "The number of marks in the backward direction to be visible."
108 (defcustom visible-mark-forward-max 0
109 "The number of marks in the forward direction to be visible."
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."
118 :type '(repeat face))
120 (defcustom visible-mark-forward-faces nil
121 "A list of mark faces for marks in the forward direction."
123 :type '(repeat face))
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)
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)
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)
146 (defface visible-mark-forward-face2
148 "Placeholder face for customization and addition to subsequent face lists."
149 :group 'visible-mark)
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)
160 (defun visible-mark-initialize-overlays ()
163 (when (eq 'visible-mark (overlay-get x 'category))
165 (overlays-in (point-min) (point-max)))
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))))
173 (defun visible-mark-find-overlay-at (pos)
174 (let ((overlays (overlays-at pos))
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)))
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)))))
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)))
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
214 (move-overlay overlay pos (1+ pos))))
216 (overlay-put overlay 'before-string nil)
217 (overlay-put overlay 'face face)
218 (move-overlay overlay pos (1+ pos)))))))
220 (require 'easy-mmode)
221 (defun visible-mark-mode-maybe ()
223 ((minibufferp (current-buffer)) nil)
224 ((cl-flet ((fun (arg)
226 (or (string-match (car arg) (buffer-name))
228 (fun global-visible-mark-mode-exclude-alist)) nil)
230 (visible-mark-mode t)))
233 (define-minor-mode visible-mark-mode
234 "A mode to make the mark visible."
237 (if visible-mark-mode
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)))
246 (define-global-minor-mode
247 global-visible-mark-mode visible-mark-mode visible-mark-mode-maybe
248 :group 'visible-mark)
250 (provide 'visible-mark)
251 ;;; visible-mark.el ends here