Commit | Line | Data |
---|---|---|
798330fe DL |
1 | ;;; hl-line.el --- highlight the current line |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004, |
d7a0267c | 4 | ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
798330fe DL |
5 | |
6 | ;; Author: Dave Love <fx@gnu.org> | |
e2f9e1b2 | 7 | ;; Maintainer: FSF |
798330fe | 8 | ;; Created: 1998-09-13 |
3a850efa | 9 | ;; Keywords: faces, frames, emulation |
798330fe | 10 | |
643c911e DL |
11 | ;; This file is part of GNU Emacs. |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
798330fe | 14 | ;; it under the terms of the GNU General Public License as published by |
b4aa6026 | 15 | ;; the Free Software Foundation; either version 3, or (at your option) |
798330fe DL |
16 | ;; any later version. |
17 | ||
643c911e | 18 | ;; GNU Emacs is distributed in the hope that it will be useful, |
798330fe DL |
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. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
086add15 LK |
25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 | ;; Boston, MA 02110-1301, USA. | |
798330fe DL |
27 | |
28 | ;;; Commentary: | |
29 | ||
e5f06fce EZ |
30 | ;; Provides a local minor mode (toggled by M-x hl-line-mode) and |
31 | ;; a global minor mode (toggled by M-x global-hl-line-mode) to | |
23db85ff LK |
32 | ;; highlight, on a suitable terminal, the line on which point is. The |
33 | ;; global mode highlights the current line in the selected window only | |
34 | ;; (except when the minibuffer window is selected). This was | |
35 | ;; implemented to satisfy a request for a feature of Lesser Editors. | |
36 | ;; The local mode is sticky: it highlights the line about the buffer's | |
37 | ;; point even if the buffer's window is not selected. Caveat: the | |
38 | ;; buffer's point might be different from the point of a non-selected | |
39 | ;; window. Set the variable `hl-line-sticky-flag' to nil to make the | |
40 | ;; local mode behave like the global mode. | |
41 | ||
42 | ;; You probably don't really want to use the global mode; if the | |
43 | ;; cursor is difficult to spot, try changing its colour, relying on | |
44 | ;; `blink-cursor-mode' or both. The hookery used might affect | |
45 | ;; response noticeably on a slow machine. The local mode may be | |
46 | ;; useful in non-editing buffers such as Gnus or PCL-CVS though. | |
47 | ||
48 | ;; An overlay is used. In the non-sticky cases, this overlay is | |
49 | ;; active only on the selected window. A hook is added to | |
50 | ;; `post-command-hook' to activate the overlay and move it to the line | |
51 | ;; about point. To get the non-sticky behavior, `hl-line-unhighlight' | |
52 | ;; is added to `pre-command-hook' as well. This function deactivates | |
53 | ;; the overlay unconditionally in case the command changes the | |
54 | ;; selected window. (It does so rather than keeping track of changes | |
55 | ;; in the selected window). | |
56 | ||
0052fe2a LK |
57 | ;; You could make variable `global-hl-line-mode' buffer-local and set |
58 | ;; it to nil to avoid highlighting specific buffers, when the global | |
59 | ;; mode is used. | |
60 | ||
a4c6ebf9 | 61 | ;; By default the whole line is highlighted. The range of highlighting |
bf247b6e | 62 | ;; can be changed by defining an appropriate function as the |
9fd76d04 MY |
63 | ;; buffer-local value of `hl-line-range-function'. |
64 | ||
798330fe DL |
65 | ;;; Code: |
66 | ||
3775cb5c CY |
67 | (defvar hl-line-overlay nil |
68 | "Overlay used by Hl-Line mode to highlight the current line.") | |
69 | (make-variable-buffer-local 'hl-line-overlay) | |
70 | ||
71 | (defvar global-hl-line-overlay nil | |
72 | "Overlay used by Global-Hl-Line mode to highlight the current line.") | |
73 | ||
798330fe | 74 | (defgroup hl-line nil |
99f2fb1f | 75 | "Highlight the current line." |
eee06d26 | 76 | :version "21.1" |
798330fe DL |
77 | :group 'editing) |
78 | ||
3775cb5c CY |
79 | (defface hl-line |
80 | '((t :inherit highlight)) | |
81 | "Default face for highlighting the current line in Hl-Line mode." | |
82 | :version "22.1" | |
798330fe DL |
83 | :group 'hl-line) |
84 | ||
3775cb5c CY |
85 | (defcustom hl-line-face 'hl-line |
86 | "Face with which to highlight the current line in Hl-Line mode." | |
87 | :type 'face | |
88 | :group 'hl-line | |
89 | :set (lambda (symbol value) | |
90 | (set symbol value) | |
91 | (dolist (buffer (buffer-list)) | |
92 | (with-current-buffer buffer | |
93 | (when hl-line-overlay | |
94 | (overlay-put hl-line-overlay 'face hl-line-face)))) | |
95 | (when global-hl-line-overlay | |
96 | (overlay-put global-hl-line-overlay 'face hl-line-face)))) | |
97 | ||
23db85ff LK |
98 | (defcustom hl-line-sticky-flag t |
99 | "*Non-nil means highlight the current line in all windows. | |
100 | Otherwise Hl-Line mode will highlight only in the selected | |
101 | window. Setting this variable takes effect the next time you use | |
102 | the command `hl-line-mode' to turn Hl-Line mode on." | |
103 | :type 'boolean | |
bf247b6e | 104 | :version "22.1" |
23db85ff LK |
105 | :group 'hl-line) |
106 | ||
9fd76d04 MY |
107 | (defvar hl-line-range-function nil |
108 | "If non-nil, function to call to return highlight range. | |
109 | The function of no args should return a cons cell; its car value | |
bf247b6e | 110 | is the beginning position of highlight and its cdr value is the |
9fd76d04 MY |
111 | end position of highlight in the buffer. |
112 | It should return nil if there's no region to be highlighted. | |
113 | ||
114 | This variable is expected to be made buffer-local by modes.") | |
115 | ||
ceafc2fa | 116 | ;;;###autoload |
3a850efa | 117 | (define-minor-mode hl-line-mode |
23db85ff | 118 | "Buffer-local minor mode to highlight the line about point. |
798330fe | 119 | With ARG, turn Hl-Line mode on if ARG is positive, off otherwise. |
23db85ff LK |
120 | |
121 | If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the | |
122 | line about the buffer's point in all windows. Caveat: the | |
123 | buffer's point might be different from the point of a | |
124 | non-selected window. Hl-Line mode uses the function | |
125 | `hl-line-highlight' on `post-command-hook' in this case. | |
126 | ||
127 | When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the | |
128 | line about point in the selected window only. In this case, it | |
129 | uses the function `hl-line-unhighlight' on `pre-command-hook' in | |
130 | addition to `hl-line-highlight' on `post-command-hook'." | |
b862cd15 | 131 | :group 'hl-line |
3a850efa DL |
132 | (if hl-line-mode |
133 | (progn | |
23db85ff LK |
134 | ;; In case `kill-all-local-variables' is called. |
135 | (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t) | |
136 | (if hl-line-sticky-flag | |
137 | (remove-hook 'pre-command-hook #'hl-line-unhighlight t) | |
138 | (add-hook 'pre-command-hook #'hl-line-unhighlight nil t)) | |
139 | (hl-line-highlight) | |
9df382fe | 140 | (add-hook 'post-command-hook #'hl-line-highlight nil t)) |
23db85ff | 141 | (remove-hook 'post-command-hook #'hl-line-highlight t) |
3a850efa | 142 | (hl-line-unhighlight) |
23db85ff LK |
143 | (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t) |
144 | (remove-hook 'pre-command-hook #'hl-line-unhighlight t))) | |
b184b2dd | 145 | |
57e46f94 | 146 | (defun hl-line-highlight () |
a4c6ebf9 | 147 | "Activate the Hl-Line overlay on the current line." |
23db85ff LK |
148 | (if hl-line-mode ; Might be changed outside the mode function. |
149 | (progn | |
150 | (unless hl-line-overlay | |
151 | (setq hl-line-overlay (make-overlay 1 1)) ; to be moved | |
152 | (overlay-put hl-line-overlay 'face hl-line-face)) | |
153 | (overlay-put hl-line-overlay | |
154 | 'window (unless hl-line-sticky-flag (selected-window))) | |
9fd76d04 | 155 | (hl-line-move hl-line-overlay)) |
23db85ff | 156 | (hl-line-unhighlight))) |
57e46f94 RS |
157 | |
158 | (defun hl-line-unhighlight () | |
23db85ff | 159 | "Deactivate the Hl-Line overlay on the current line." |
57e46f94 RS |
160 | (if hl-line-overlay |
161 | (delete-overlay hl-line-overlay))) | |
162 | ||
23db85ff LK |
163 | ;;;###autoload |
164 | (define-minor-mode global-hl-line-mode | |
165 | "Global minor mode to highlight the line about point in the current window. | |
166 | With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise. | |
167 | ||
168 | Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and | |
169 | `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'." | |
170 | :global t | |
171 | :group 'hl-line | |
172 | (if global-hl-line-mode | |
173 | (progn | |
174 | (add-hook 'pre-command-hook #'global-hl-line-unhighlight) | |
175 | (add-hook 'post-command-hook #'global-hl-line-highlight)) | |
176 | (global-hl-line-unhighlight) | |
177 | (remove-hook 'pre-command-hook #'global-hl-line-unhighlight) | |
178 | (remove-hook 'post-command-hook #'global-hl-line-highlight))) | |
179 | ||
180 | (defun global-hl-line-highlight () | |
181 | "Active the Global-Hl-Line overlay on the current line in the current window." | |
182 | (when global-hl-line-mode ; Might be changed outside the mode function. | |
183 | (unless (window-minibuffer-p (selected-window)) | |
184 | (unless global-hl-line-overlay | |
185 | (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved | |
186 | (overlay-put global-hl-line-overlay 'face hl-line-face)) | |
187 | (overlay-put global-hl-line-overlay 'window (selected-window)) | |
9fd76d04 | 188 | (hl-line-move global-hl-line-overlay)))) |
23db85ff LK |
189 | |
190 | (defun global-hl-line-unhighlight () | |
191 | "Deactivate the Global-Hl-Line overlay on the current line." | |
192 | (if global-hl-line-overlay | |
193 | (delete-overlay global-hl-line-overlay))) | |
194 | ||
9fd76d04 | 195 | (defun hl-line-move (overlay) |
a4c6ebf9 | 196 | "Move the Hl-Line overlay. |
9fd76d04 | 197 | If `hl-line-range-function' is non-nil, move the OVERLAY to the position |
a4c6ebf9 | 198 | where the function returns. If `hl-line-range-function' is nil, fill |
9fd76d04 MY |
199 | the line including the point by OVERLAY." |
200 | (let (tmp b e) | |
201 | (if hl-line-range-function | |
202 | (setq tmp (funcall hl-line-range-function) | |
203 | b (car tmp) | |
204 | e (cdr tmp)) | |
205 | (setq tmp t | |
206 | b (line-beginning-position) | |
207 | e (line-beginning-position 2))) | |
208 | (if tmp | |
209 | (move-overlay overlay b e) | |
210 | (move-overlay overlay 1 1)))) | |
211 | ||
798330fe DL |
212 | (provide 'hl-line) |
213 | ||
ab5796a9 | 214 | ;;; arch-tag: ac806940-0876-4959-8c89-947563ee2833 |
798330fe | 215 | ;;; hl-line.el ends here |