Merge from emacs-23 branch
[bpt/emacs.git] / lisp / hl-line.el
1 ;;; hl-line.el --- highlight the current line
2
3 ;; Copyright (C) 1998, 2000-2011 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: FSF
7 ;; Created: 1998-09-13
8 ;; Keywords: faces, frames, emulations
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Provides a local minor mode (toggled by M-x hl-line-mode) and
28 ;; a global minor mode (toggled by M-x global-hl-line-mode) to
29 ;; highlight, on a suitable terminal, the line on which point is. The
30 ;; global mode highlights the current line in the selected window only
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.
38
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. To get the non-sticky behavior, `hl-line-unhighlight'
49 ;; is added to `pre-command-hook' as well. This function deactivates
50 ;; the overlay unconditionally in case the command changes the
51 ;; selected window. (It does so rather than keeping track of changes
52 ;; in the selected window).
53
54 ;; You could make variable `global-hl-line-mode' buffer-local and set
55 ;; it to nil to avoid highlighting specific buffers, when the global
56 ;; mode is used.
57
58 ;; By default the whole line is highlighted. The range of highlighting
59 ;; can be changed by defining an appropriate function as the
60 ;; buffer-local value of `hl-line-range-function'.
61
62 ;;; Code:
63
64 (defvar hl-line-overlay nil
65 "Overlay used by Hl-Line mode to highlight the current line.")
66 (make-variable-buffer-local 'hl-line-overlay)
67
68 (defvar global-hl-line-overlay nil
69 "Overlay used by Global-Hl-Line mode to highlight the current line.")
70
71 (defgroup hl-line nil
72 "Highlight the current line."
73 :version "21.1"
74 :group 'convenience)
75
76 (defface hl-line
77 '((t :inherit highlight))
78 "Default face for highlighting the current line in Hl-Line mode."
79 :version "22.1"
80 :group 'hl-line)
81
82 (defcustom hl-line-face 'hl-line
83 "Face with which to highlight the current line in Hl-Line mode."
84 :type 'face
85 :group 'hl-line
86 :set (lambda (symbol value)
87 (set symbol value)
88 (dolist (buffer (buffer-list))
89 (with-current-buffer buffer
90 (when hl-line-overlay
91 (overlay-put hl-line-overlay 'face hl-line-face))))
92 (when global-hl-line-overlay
93 (overlay-put global-hl-line-overlay 'face hl-line-face))))
94
95 (defcustom hl-line-sticky-flag t
96 "Non-nil means the HL-Line mode highlight appears in all windows.
97 Otherwise Hl-Line mode will highlight only in the selected
98 window. Setting this variable takes effect the next time you use
99 the command `hl-line-mode' to turn Hl-Line mode on.
100
101 This variable has no effect in Global Highlight Line mode.
102 For that, use `global-hl-line-sticky-flag'."
103 :type 'boolean
104 :version "22.1"
105 :group 'hl-line)
106
107 (defcustom global-hl-line-sticky-flag nil
108 "Non-nil means the Global HL-Line mode highlight appears in all windows.
109 Otherwise Global Hl-Line mode will highlight only in the selected
110 window. Setting this variable takes effect the next time you use
111 the command `global-hl-line-mode' to turn Global Hl-Line mode on."
112 :type 'boolean
113 :version "24.1"
114 :group 'hl-line)
115
116 (defvar hl-line-range-function nil
117 "If non-nil, function to call to return highlight range.
118 The function of no args should return a cons cell; its car value
119 is the beginning position of highlight and its cdr value is the
120 end position of highlight in the buffer.
121 It should return nil if there's no region to be highlighted.
122
123 This variable is expected to be made buffer-local by modes.")
124
125 ;;;###autoload
126 (define-minor-mode hl-line-mode
127 "Buffer-local minor mode to highlight the line about point.
128 With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
129
130 If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
131 line about the buffer's point in all windows. Caveat: the
132 buffer's point might be different from the point of a
133 non-selected window. Hl-Line mode uses the function
134 `hl-line-highlight' on `post-command-hook' in this case.
135
136 When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
137 line about point in the selected window only. In this case, it
138 uses the function `hl-line-unhighlight' on `pre-command-hook' in
139 addition to `hl-line-highlight' on `post-command-hook'."
140 :group 'hl-line
141 (if hl-line-mode
142 (progn
143 ;; In case `kill-all-local-variables' is called.
144 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
145 (if hl-line-sticky-flag
146 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)
147 (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
148 (hl-line-highlight)
149 (add-hook 'post-command-hook #'hl-line-highlight nil t))
150 (remove-hook 'post-command-hook #'hl-line-highlight t)
151 (hl-line-unhighlight)
152 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
153 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
154
155 (defun hl-line-highlight ()
156 "Activate the Hl-Line overlay on the current line."
157 (if hl-line-mode ; Might be changed outside the mode function.
158 (progn
159 (unless hl-line-overlay
160 (setq hl-line-overlay (make-overlay 1 1)) ; to be moved
161 (overlay-put hl-line-overlay 'face hl-line-face))
162 (overlay-put hl-line-overlay
163 'window (unless hl-line-sticky-flag (selected-window)))
164 (hl-line-move hl-line-overlay))
165 (hl-line-unhighlight)))
166
167 (defun hl-line-unhighlight ()
168 "Deactivate the Hl-Line overlay on the current line."
169 (when hl-line-overlay
170 (delete-overlay hl-line-overlay)))
171
172 ;;;###autoload
173 (define-minor-mode global-hl-line-mode
174 "Global minor mode to highlight the line about point in the current window.
175 With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.
176
177 If `global-hl-line-sticky-flag' is non-nil, Global Hl-Line mode
178 highlights the line about the current buffer's point in all
179 windows.
180
181 Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
182 `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
183 :global t
184 :group 'hl-line
185 (if global-hl-line-mode
186 (progn
187 (add-hook 'pre-command-hook #'global-hl-line-unhighlight)
188 (add-hook 'post-command-hook #'global-hl-line-highlight))
189 (global-hl-line-unhighlight)
190 (remove-hook 'pre-command-hook #'global-hl-line-unhighlight)
191 (remove-hook 'post-command-hook #'global-hl-line-highlight)))
192
193 (defun global-hl-line-highlight ()
194 "Highlight the current line in the current window."
195 (when global-hl-line-mode ; Might be changed outside the mode function.
196 (unless (window-minibuffer-p (selected-window))
197 (unless global-hl-line-overlay
198 (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved
199 (overlay-put global-hl-line-overlay 'face hl-line-face))
200 (overlay-put global-hl-line-overlay 'window
201 (unless global-hl-line-sticky-flag
202 (selected-window)))
203 (hl-line-move global-hl-line-overlay))))
204
205 (defun global-hl-line-unhighlight ()
206 "Deactivate the Global-Hl-Line overlay on the current line."
207 (when global-hl-line-overlay
208 (delete-overlay global-hl-line-overlay)))
209
210 (defun hl-line-move (overlay)
211 "Move the Hl-Line overlay.
212 If `hl-line-range-function' is non-nil, move the OVERLAY to the position
213 where the function returns. If `hl-line-range-function' is nil, fill
214 the line including the point by OVERLAY."
215 (let (tmp b e)
216 (if hl-line-range-function
217 (setq tmp (funcall hl-line-range-function)
218 b (car tmp)
219 e (cdr tmp))
220 (setq tmp t
221 b (line-beginning-position)
222 e (line-beginning-position 2)))
223 (if tmp
224 (move-overlay overlay b e)
225 (move-overlay overlay 1 1))))
226
227 (defun hl-line-unload-function ()
228 "Unload the Hl-Line library."
229 (global-hl-line-mode -1)
230 (save-current-buffer
231 (dolist (buffer (buffer-list))
232 (set-buffer buffer)
233 (when hl-line-mode (hl-line-mode -1))))
234 ;; continue standard unloading
235 nil)
236
237 (provide 'hl-line)
238
239 ;;; hl-line.el ends here