1 ;;; hl-line.el --- highlight the current line
3 ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Dave Love <fx@gnu.org>
9 ;; Keywords: faces, frames, emulation
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Provides a local minor mode (toggled by M-x hl-line-mode) and
29 ;; a global minor mode (toggled by M-x global-hl-line-mode) to
30 ;; highlight, on a suitable terminal, the line on which point is. The
31 ;; global mode highlights the current line in the selected window only
32 ;; (except when the minibuffer window is selected). This was
33 ;; implemented to satisfy a request for a feature of Lesser Editors.
34 ;; The local mode is sticky: it highlights the line about the buffer's
35 ;; point even if the buffer's window is not selected. Caveat: the
36 ;; buffer's point might be different from the point of a non-selected
37 ;; window. Set the variable `hl-line-sticky-flag' to nil to make the
38 ;; local mode behave like the global mode.
40 ;; You probably don't really want to use the global mode; if the
41 ;; cursor is difficult to spot, try changing its colour, relying on
42 ;; `blink-cursor-mode' or both. The hookery used might affect
43 ;; response noticeably on a slow machine. The local mode may be
44 ;; useful in non-editing buffers such as Gnus or PCL-CVS though.
46 ;; An overlay is used. In the non-sticky cases, this overlay is
47 ;; active only on the selected window. A hook is added to
48 ;; `post-command-hook' to activate the overlay and move it to the line
49 ;; about point. To get the non-sticky behavior, `hl-line-unhighlight'
50 ;; is added to `pre-command-hook' as well. This function deactivates
51 ;; the overlay unconditionally in case the command changes the
52 ;; selected window. (It does so rather than keeping track of changes
53 ;; in the selected window).
55 ;; You could make variable `global-hl-line-mode' buffer-local and set
56 ;; it to nil to avoid highlighting specific buffers, when the global
59 ;; By default the whole line is highlighted. The range of highlighting
60 ;; can be changed by defining an appropriate function as the
61 ;; buffer-local value of `hl-line-range-function'.
65 (defvar hl-line-overlay nil
66 "Overlay used by Hl-Line mode to highlight the current line.")
67 (make-variable-buffer-local 'hl-line-overlay
)
69 (defvar global-hl-line-overlay nil
70 "Overlay used by Global-Hl-Line mode to highlight the current line.")
73 "Highlight the current line."
78 '((t :inherit highlight
))
79 "Default face for highlighting the current line in Hl-Line mode."
83 (defcustom hl-line-face
'hl-line
84 "Face with which to highlight the current line in Hl-Line mode."
87 :set
(lambda (symbol value
)
89 (dolist (buffer (buffer-list))
90 (with-current-buffer buffer
92 (overlay-put hl-line-overlay
'face hl-line-face
))))
93 (when global-hl-line-overlay
94 (overlay-put global-hl-line-overlay
'face hl-line-face
))))
96 (defcustom hl-line-sticky-flag t
97 "*Non-nil means highlight the current line in all windows.
98 Otherwise Hl-Line mode will highlight only in the selected
99 window. Setting this variable takes effect the next time you use
100 the command `hl-line-mode' to turn Hl-Line mode on."
105 (defvar hl-line-range-function nil
106 "If non-nil, function to call to return highlight range.
107 The function of no args should return a cons cell; its car value
108 is the beginning position of highlight and its cdr value is the
109 end position of highlight in the buffer.
110 It should return nil if there's no region to be highlighted.
112 This variable is expected to be made buffer-local by modes.")
115 (define-minor-mode hl-line-mode
116 "Buffer-local minor mode to highlight the line about point.
117 With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
119 If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
120 line about the buffer's point in all windows. Caveat: the
121 buffer's point might be different from the point of a
122 non-selected window. Hl-Line mode uses the function
123 `hl-line-highlight' on `post-command-hook' in this case.
125 When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
126 line about point in the selected window only. In this case, it
127 uses the function `hl-line-unhighlight' on `pre-command-hook' in
128 addition to `hl-line-highlight' on `post-command-hook'."
132 ;; In case `kill-all-local-variables' is called.
133 (add-hook 'change-major-mode-hook
#'hl-line-unhighlight nil t
)
134 (if hl-line-sticky-flag
135 (remove-hook 'pre-command-hook
#'hl-line-unhighlight t
)
136 (add-hook 'pre-command-hook
#'hl-line-unhighlight nil t
))
138 (add-hook 'post-command-hook
#'hl-line-highlight nil t
))
139 (remove-hook 'post-command-hook
#'hl-line-highlight t
)
140 (hl-line-unhighlight)
141 (remove-hook 'change-major-mode-hook
#'hl-line-unhighlight t
)
142 (remove-hook 'pre-command-hook
#'hl-line-unhighlight t
)))
144 (defun hl-line-highlight ()
145 "Activate the Hl-Line overlay on the current line."
146 (if hl-line-mode
; Might be changed outside the mode function.
148 (unless hl-line-overlay
149 (setq hl-line-overlay
(make-overlay 1 1)) ; to be moved
150 (overlay-put hl-line-overlay
'face hl-line-face
))
151 (overlay-put hl-line-overlay
152 'window
(unless hl-line-sticky-flag
(selected-window)))
153 (hl-line-move hl-line-overlay
))
154 (hl-line-unhighlight)))
156 (defun hl-line-unhighlight ()
157 "Deactivate the Hl-Line overlay on the current line."
159 (delete-overlay hl-line-overlay
)))
162 (define-minor-mode global-hl-line-mode
163 "Global minor mode to highlight the line about point in the current window.
164 With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.
166 Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
167 `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
170 (if global-hl-line-mode
172 (add-hook 'pre-command-hook
#'global-hl-line-unhighlight
)
173 (add-hook 'post-command-hook
#'global-hl-line-highlight
))
174 (global-hl-line-unhighlight)
175 (remove-hook 'pre-command-hook
#'global-hl-line-unhighlight
)
176 (remove-hook 'post-command-hook
#'global-hl-line-highlight
)))
178 (defun global-hl-line-highlight ()
179 "Active the Global-Hl-Line overlay on the current line in the current window."
180 (when global-hl-line-mode
; Might be changed outside the mode function.
181 (unless (window-minibuffer-p (selected-window))
182 (unless global-hl-line-overlay
183 (setq global-hl-line-overlay
(make-overlay 1 1)) ; to be moved
184 (overlay-put global-hl-line-overlay
'face hl-line-face
))
185 (overlay-put global-hl-line-overlay
'window
(selected-window))
186 (hl-line-move global-hl-line-overlay
))))
188 (defun global-hl-line-unhighlight ()
189 "Deactivate the Global-Hl-Line overlay on the current line."
190 (if global-hl-line-overlay
191 (delete-overlay global-hl-line-overlay
)))
193 (defun hl-line-move (overlay)
194 "Move the Hl-Line overlay.
195 If `hl-line-range-function' is non-nil, move the OVERLAY to the position
196 where the function returns. If `hl-line-range-function' is nil, fill
197 the line including the point by OVERLAY."
199 (if hl-line-range-function
200 (setq tmp
(funcall hl-line-range-function
)
204 b
(line-beginning-position)
205 e
(line-beginning-position 2)))
207 (move-overlay overlay b e
)
208 (move-overlay overlay
1 1))))
212 ;; arch-tag: ac806940-0876-4959-8c89-947563ee2833
213 ;;; hl-line.el ends here