aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPo Lu2022-03-23 16:48:49 +0800
committerPo Lu2022-03-23 16:48:49 +0800
commita34afbf2aea2fdaf691f4bf250a18991b21301d7 (patch)
treee07fac6f075f977ea9df87364de577ea51108575
parentfed9a353dbe79a7a6acc74c1e223c46e7541e627 (diff)
downloademacs-a34afbf2aea2fdaf691f4bf250a18991b21301d7.tar.gz
emacs-a34afbf2aea2fdaf691f4bf250a18991b21301d7.zip
Restore old hl-line code
* lisp/hl-line.el: Restore old code to remove obsoletions. (hl-line-overlay-priority): Make defcustom.
-rw-r--r--lisp/hl-line.el289
1 files changed, 214 insertions, 75 deletions
diff --git a/lisp/hl-line.el b/lisp/hl-line.el
index f1c2e1ebf23..20b3f4160fd 100644
--- a/lisp/hl-line.el
+++ b/lisp/hl-line.el
@@ -24,40 +24,58 @@
24 24
25;;; Commentary: 25;;; Commentary:
26 26
27;; Proper scuttling of unsticky overlays relies on `post-command-hook` 27;; Provides a local minor mode (toggled by M-x hl-line-mode) and
28;; being called on a buffer switch and the stationarity of 28;; a global minor mode (toggled by M-x global-hl-line-mode) to
29;; `hl-line--buffer` across switches. One could easily imagine 29;; highlight, on a suitable terminal, the line on which point is. The
30;; programatically defeating unsticky overlays by bypassing 30;; global mode highlights the current line in the selected window only
31;; `post-command-hook`. 31;; (except when the minibuffer window is selected). This was
32;; implemented to satisfy a request for a feature of Lesser Editors.
33;; The local mode is sticky: it highlights the line about the buffer's
34;; point even if the buffer's window is not selected. Caveat: the
35;; buffer's point might be different from the point of a non-selected
36;; window. Set the variable `hl-line-sticky-flag' to nil to make the
37;; local mode behave like the global mode.
32 38
33;;; Code: 39;; You probably don't really want to use the global mode; if the
40;; cursor is difficult to spot, try changing its color, relying on
41;; `blink-cursor-mode' or both. The hookery used might affect
42;; response noticeably on a slow machine. The local mode may be
43;; useful in non-editing buffers such as Gnus or PCL-CVS though.
44
45;; An overlay is used. In the non-sticky cases, this overlay is
46;; active only on the selected window. A hook is added to
47;; `post-command-hook' to activate the overlay and move it to the line
48;; about point.
49
50;; You could make variable `global-hl-line-mode' buffer-local and set
51;; it to nil to avoid highlighting specific buffers, when the global
52;; mode is used.
34 53
35(make-obsolete-variable 'hl-line-overlay 'hl-line--overlay "29.1") 54;; By default the whole line is highlighted. The range of highlighting
36(make-obsolete-variable 'global-hl-line-overlay nil "29.1") 55;; can be changed by defining an appropriate function as the
37(make-obsolete-variable 'global-hl-line-overlays nil "29.1") 56;; buffer-local value of `hl-line-range-function'.
38(make-obsolete-variable 'global-hl-line-sticky-flag nil "29.1")
39(make-obsolete-variable 'hl-line-overlay-buffer 'hl-line--buffer "29.1")
40(make-obsolete-variable 'hl-line-range-function nil "29.1")
41 57
42(defvar-local hl-line--overlay nil 58;;; Code:
43 "The prevailing highlighting overlay per buffer.") 59
60(defvar-local hl-line-overlay nil
61 "Overlay used by Hl-Line mode to highlight the current line.")
44 62
45(defvar hl-line--buffer nil 63(defvar-local global-hl-line-overlay nil
46 "Used to track last buffer.") 64 "Overlay used by Global-Hl-Line mode to highlight the current line.")
47 65
48;; 1. define-minor-mode creates buffer-local hl-line--overlay 66(defvar global-hl-line-overlays nil
49;; 2. overlay wiped by kill-all-local-variables 67 "Overlays used by Global-Hl-Line mode in various buffers.
50;; 3. post-command-hook dupes overlay 68Global-Hl-Line keeps displaying one overlay in each buffer
51;; Solution: prevent step 2. 69when `global-hl-line-sticky-flag' is non-nil.")
52(put 'hl-line--overlay 'permanent-local t)
53 70
54(defgroup hl-line nil 71(defgroup hl-line nil
55 "Highlight the current line." 72 "Highlight the current line."
56 :version "21.1" 73 :version "21.1"
57 :group 'convenience) 74 :group 'convenience)
58 75
59(defface hl-line '((t :inherit highlight :extend t)) 76(defface hl-line
60 "Default face for highlighting the current line in hl-line-mode." 77 '((t :inherit highlight :extend t))
78 "Default face for highlighting the current line in Hl-Line mode."
61 :version "22.1" 79 :version "22.1"
62 :group 'hl-line) 80 :group 'hl-line)
63 81
@@ -69,77 +87,204 @@
69 (set symbol value) 87 (set symbol value)
70 (dolist (buffer (buffer-list)) 88 (dolist (buffer (buffer-list))
71 (with-current-buffer buffer 89 (with-current-buffer buffer
72 (when hl-line--overlay 90 (when (overlayp hl-line-overlay)
73 (overlay-put hl-line--overlay 'face hl-line-face)))))) 91 (overlay-put hl-line-overlay 'face hl-line-face))))
92 (when (overlayp global-hl-line-overlay)
93 (overlay-put global-hl-line-overlay 'face hl-line-face))))
74 94
75(defcustom hl-line-sticky-flag t 95(defcustom hl-line-sticky-flag t
76 "Non-nil to preserve highlighting overlay when focus leaves window." 96 "Non-nil means the HL-Line mode highlight appears in all windows.
97Otherwise Hl-Line mode will highlight only in the selected
98window. Setting this variable takes effect the next time you use
99the command `hl-line-mode' to turn Hl-Line mode on.
100
101This variable has no effect in Global Highlight Line mode.
102For that, use `global-hl-line-sticky-flag'."
77 :type 'boolean 103 :type 'boolean
78 :version "22.1" 104 :version "22.1"
79 :group 'hl-line 105 :group 'hl-line)
80 :initialize #'custom-initialize-default 106
81 :set (lambda (symbol value) 107(defcustom global-hl-line-sticky-flag nil
82 (set-default symbol value) 108 "Non-nil means the Global HL-Line mode highlight appears in all windows.
83 (unless value 109Otherwise Global Hl-Line mode will highlight only in the selected
84 (let ((selected (window-buffer (selected-window)))) 110window. Setting this variable takes effect the next time you use
85 (dolist (buffer (buffer-list)) 111the command `global-hl-line-mode' to turn Global Hl-Line mode on."
86 (unless (eq buffer selected) 112 :type 'boolean
87 (with-current-buffer buffer 113 :version "24.1"
88 (hl-line-unhighlight)))))))) 114 :group 'hl-line)
115
116(defvar hl-line-range-function nil
117 "If non-nil, function to call to return highlight range.
118The function of no args should return a cons cell; its car value
119is the beginning position of highlight and its cdr value is the
120end position of highlight in the buffer.
121It should return nil if there's no region to be highlighted.
122
123This variable is expected to be made buffer-local by modes.")
124
125(defvar hl-line-overlay-buffer nil
126 "Most recently visited buffer in which Hl-Line mode is enabled.")
89 127
90(defcustom hl-line-overlay-priority -50 128(defcustom hl-line-overlay-priority -50
91 "Priority used on the overlay used by hl-line." 129 "Priority used on the overlay used by hl-line."
92 :type 'integer 130 :type 'integer
93 :version "22.1" 131 :version "28.1"
94 :group 'hl-line)
95
96(defcustom hl-line-highlight-hook nil
97 "After hook for `hl-line-highlight'.
98Currently used in calendar/todo-mode."
99 :type 'hook
100 :group 'hl-line) 132 :group 'hl-line)
101 133
102;;;###autoload 134;;;###autoload
103(define-minor-mode hl-line-mode 135(define-minor-mode hl-line-mode
104 "Toggle highlighting of the current line." 136 "Toggle highlighting of the current line (Hl-Line mode).
137
138Hl-Line mode is a buffer-local minor mode. If
139`hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
140line about the buffer's point in all windows. Caveat: the
141buffer's point might be different from the point of a
142non-selected window. Hl-Line mode uses the function
143`hl-line-highlight' on `post-command-hook' in this case.
144
145When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
146line about point in the selected window only."
105 :group 'hl-line 147 :group 'hl-line
106 (if hl-line-mode 148 (if hl-line-mode
107 (progn 149 (progn
108 (hl-line-highlight) 150 ;; In case `kill-all-local-variables' is called.
109 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t) 151 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
152 (hl-line-highlight)
153 (setq hl-line-overlay-buffer (current-buffer))
110 (add-hook 'post-command-hook #'hl-line-highlight nil t)) 154 (add-hook 'post-command-hook #'hl-line-highlight nil t))
111 (remove-hook 'post-command-hook #'hl-line-highlight t) 155 (remove-hook 'post-command-hook #'hl-line-highlight t)
112 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t) 156 (hl-line-unhighlight)
157 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)))
158
159(defun hl-line-make-overlay ()
160 (let ((ol (make-overlay (point) (point))))
161 (overlay-put ol 'priority hl-line-overlay-priority) ;(bug#16192)
162 (overlay-put ol 'face hl-line-face)
163 ol))
164
165(defun hl-line-highlight ()
166 "Activate the Hl-Line overlay on the current line."
167 (if hl-line-mode ; Might be changed outside the mode function.
168 (progn
169 (unless (overlayp hl-line-overlay)
170 (setq hl-line-overlay (hl-line-make-overlay))) ; To be moved.
171 (overlay-put hl-line-overlay
172 'window (unless hl-line-sticky-flag (selected-window)))
173 (hl-line-move hl-line-overlay)
174 (hl-line-maybe-unhighlight))
113 (hl-line-unhighlight))) 175 (hl-line-unhighlight)))
114 176
115(defun hl-line-unhighlight () 177(defun hl-line-unhighlight ()
116 (when hl-line--overlay 178 "Deactivate the Hl-Line overlay on the current line."
117 (delete-overlay hl-line--overlay) 179 (when (overlayp hl-line-overlay)
118 (setq hl-line--overlay nil))) 180 (delete-overlay hl-line-overlay)
181 (setq hl-line-overlay nil)))
119 182
120(defun hl-line-highlight () 183(defun hl-line-maybe-unhighlight ()
121 (unless (minibufferp) 184 "Maybe deactivate the Hl-Line overlay on the current line.
122 (unless hl-line--overlay 185Specifically, when `hl-line-sticky-flag' is nil deactivate all
123 (setq hl-line--overlay 186such overlays in all buffers except the current one."
124 (let ((ol (make-overlay (point) (point)))) 187 (let ((hlob hl-line-overlay-buffer)
125 (prog1 ol 188 (curbuf (current-buffer)))
126 (overlay-put ol 'priority hl-line-overlay-priority) 189 (when (and (buffer-live-p hlob)
127 (overlay-put ol 'face hl-line-face)))))
128 (move-overlay hl-line--overlay
129 (line-beginning-position)
130 (line-beginning-position 2))
131 (when (and (not (eq hl-line--buffer (current-buffer)))
132 (not hl-line-sticky-flag) 190 (not hl-line-sticky-flag)
133 (buffer-live-p hl-line--buffer)) 191 (not (eq curbuf hlob))
134 (with-current-buffer hl-line--buffer 192 (not (minibufferp)))
193 (with-current-buffer hlob
135 (hl-line-unhighlight))) 194 (hl-line-unhighlight)))
136 (setq hl-line--buffer (current-buffer)) 195 (when (and (overlayp hl-line-overlay)
137 (run-hooks 'hl-line-highlight-hook))) 196 (eq (overlay-buffer hl-line-overlay) curbuf))
197 (setq hl-line-overlay-buffer curbuf))))
198
199;;;###autoload
200(define-minor-mode global-hl-line-mode
201 "Toggle line highlighting in all buffers (Global Hl-Line mode).
202
203If `global-hl-line-sticky-flag' is non-nil, Global Hl-Line mode
204highlights the line about the current buffer's point in all live
205windows.
206
207Global-Hl-Line mode uses the function `global-hl-line-highlight'
208on `post-command-hook'."
209 :global t
210 :group 'hl-line
211 (if global-hl-line-mode
212 (progn
213 ;; In case `kill-all-local-variables' is called.
214 (add-hook 'change-major-mode-hook #'global-hl-line-unhighlight)
215 (global-hl-line-highlight-all)
216 (add-hook 'post-command-hook #'global-hl-line-highlight))
217 (global-hl-line-unhighlight-all)
218 (remove-hook 'post-command-hook #'global-hl-line-highlight)
219 (remove-hook 'change-major-mode-hook #'global-hl-line-unhighlight)))
220
221(defun global-hl-line-highlight ()
222 "Highlight the current line in the current window."
223 (when global-hl-line-mode ; Might be changed outside the mode function.
224 (unless (window-minibuffer-p)
225 (unless (overlayp global-hl-line-overlay)
226 (setq global-hl-line-overlay (hl-line-make-overlay))) ; To be moved.
227 (unless (member global-hl-line-overlay global-hl-line-overlays)
228 (push global-hl-line-overlay global-hl-line-overlays))
229 (overlay-put global-hl-line-overlay 'window
230 (unless global-hl-line-sticky-flag
231 (selected-window)))
232 (hl-line-move global-hl-line-overlay)
233 (global-hl-line-maybe-unhighlight))))
234
235(defun global-hl-line-highlight-all ()
236 "Highlight the current line in all live windows."
237 (walk-windows (lambda (w)
238 (with-current-buffer (window-buffer w)
239 (global-hl-line-highlight)))
240 nil t))
241
242(defun global-hl-line-unhighlight ()
243 "Deactivate the Global-Hl-Line overlay on the current line."
244 (when (overlayp global-hl-line-overlay)
245 (delete-overlay global-hl-line-overlay)
246 (setq global-hl-line-overlay nil)))
138 247
139(defun hl-line-turn-on () 248(defun global-hl-line-maybe-unhighlight ()
140 (unless (minibufferp) 249 "Maybe deactivate the Global-Hl-Line overlay on the current line.
141 (let (inhibit-quit) 250Specifically, when `global-hl-line-sticky-flag' is nil deactivate
142 (hl-line-mode 1)))) 251all such overlays in all buffers except the current one."
252 (mapc (lambda (ov)
253 (let ((ovb (overlay-buffer ov)))
254 (when (and (not global-hl-line-sticky-flag)
255 (bufferp ovb)
256 (not (eq ovb (current-buffer)))
257 (not (minibufferp)))
258 (with-current-buffer ovb
259 (global-hl-line-unhighlight)))))
260 global-hl-line-overlays))
261
262(defun global-hl-line-unhighlight-all ()
263 "Deactivate all Global-Hl-Line overlays."
264 (mapc (lambda (ov)
265 (let ((ovb (overlay-buffer ov)))
266 (when (bufferp ovb)
267 (with-current-buffer ovb
268 (global-hl-line-unhighlight)))))
269 global-hl-line-overlays)
270 (setq global-hl-line-overlays nil))
271
272(defun hl-line-move (overlay)
273 "Move the Hl-Line overlay.
274If `hl-line-range-function' is non-nil, move the OVERLAY to the position
275where the function returns. If `hl-line-range-function' is nil, fill
276the line including the point by OVERLAY."
277 (let (tmp b e)
278 (if hl-line-range-function
279 (setq tmp (funcall hl-line-range-function)
280 b (car tmp)
281 e (cdr tmp))
282 (setq tmp t
283 b (line-beginning-position)
284 e (line-beginning-position 2)))
285 (if tmp
286 (move-overlay overlay b e)
287 (move-overlay overlay 1 1))))
143 288
144(defun hl-line-unload-function () 289(defun hl-line-unload-function ()
145 "Unload the Hl-Line library." 290 "Unload the Hl-Line library."
@@ -151,12 +296,6 @@ Currently used in calendar/todo-mode."
151 ;; continue standard unloading 296 ;; continue standard unloading
152 nil) 297 nil)
153 298
154;;;###autoload
155(define-globalized-minor-mode global-hl-line-mode
156 hl-line-mode hl-line-turn-on
157 :group 'hl-line
158 :version "29.1")
159
160(provide 'hl-line) 299(provide 'hl-line)
161 300
162;;; hl-line.el ends here 301;;; hl-line.el ends here