Commit | Line | Data |
---|---|---|
798330fe DL |
1 | ;;; hl-line.el --- highlight the current line |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1998, 2000, 2001, 2002, 2003, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 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 | ||
eb3fa2cf | 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 |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
798330fe | 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 | |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
798330fe DL |
25 | |
26 | ;;; Commentary: | |
27 | ||
e5f06fce EZ |
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 | |
23db85ff LK |
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. | |
39 | ||
40 | ;; You probably don't really want to use the global mode; if the | |
6772c8e1 | 41 | ;; cursor is difficult to spot, try changing its color, relying on |
23db85ff LK |
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. | |
45 | ||
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). | |
54 | ||
0052fe2a LK |
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 | |
57 | ;; mode is used. | |
58 | ||
a4c6ebf9 | 59 | ;; By default the whole line is highlighted. The range of highlighting |
bf247b6e | 60 | ;; can be changed by defining an appropriate function as the |
9fd76d04 MY |
61 | ;; buffer-local value of `hl-line-range-function'. |
62 | ||
798330fe DL |
63 | ;;; Code: |
64 | ||
3775cb5c CY |
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) | |
68 | ||
69 | (defvar global-hl-line-overlay nil | |
70 | "Overlay used by Global-Hl-Line mode to highlight the current line.") | |
71 | ||
798330fe | 72 | (defgroup hl-line nil |
99f2fb1f | 73 | "Highlight the current line." |
eee06d26 | 74 | :version "21.1" |
798330fe DL |
75 | :group 'editing) |
76 | ||
3775cb5c CY |
77 | (defface hl-line |
78 | '((t :inherit highlight)) | |
79 | "Default face for highlighting the current line in Hl-Line mode." | |
80 | :version "22.1" | |
798330fe DL |
81 | :group 'hl-line) |
82 | ||
3775cb5c CY |
83 | (defcustom hl-line-face 'hl-line |
84 | "Face with which to highlight the current line in Hl-Line mode." | |
85 | :type 'face | |
86 | :group 'hl-line | |
87 | :set (lambda (symbol value) | |
88 | (set symbol value) | |
89 | (dolist (buffer (buffer-list)) | |
90 | (with-current-buffer buffer | |
91 | (when hl-line-overlay | |
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)))) | |
95 | ||
23db85ff LK |
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." | |
101 | :type 'boolean | |
bf247b6e | 102 | :version "22.1" |
23db85ff LK |
103 | :group 'hl-line) |
104 | ||
9fd76d04 MY |
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 | |
bf247b6e | 108 | is the beginning position of highlight and its cdr value is the |
9fd76d04 MY |
109 | end position of highlight in the buffer. |
110 | It should return nil if there's no region to be highlighted. | |
111 | ||
112 | This variable is expected to be made buffer-local by modes.") | |
113 | ||
ceafc2fa | 114 | ;;;###autoload |
3a850efa | 115 | (define-minor-mode hl-line-mode |
23db85ff | 116 | "Buffer-local minor mode to highlight the line about point. |
798330fe | 117 | With ARG, turn Hl-Line mode on if ARG is positive, off otherwise. |
23db85ff LK |
118 | |
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. | |
124 | ||
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'." | |
b862cd15 | 129 | :group 'hl-line |
3a850efa DL |
130 | (if hl-line-mode |
131 | (progn | |
23db85ff LK |
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)) | |
137 | (hl-line-highlight) | |
9df382fe | 138 | (add-hook 'post-command-hook #'hl-line-highlight nil t)) |
23db85ff | 139 | (remove-hook 'post-command-hook #'hl-line-highlight t) |
3a850efa | 140 | (hl-line-unhighlight) |
23db85ff LK |
141 | (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t) |
142 | (remove-hook 'pre-command-hook #'hl-line-unhighlight t))) | |
b184b2dd | 143 | |
57e46f94 | 144 | (defun hl-line-highlight () |
a4c6ebf9 | 145 | "Activate the Hl-Line overlay on the current line." |
23db85ff LK |
146 | (if hl-line-mode ; Might be changed outside the mode function. |
147 | (progn | |
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))) | |
9fd76d04 | 153 | (hl-line-move hl-line-overlay)) |
23db85ff | 154 | (hl-line-unhighlight))) |
57e46f94 RS |
155 | |
156 | (defun hl-line-unhighlight () | |
23db85ff | 157 | "Deactivate the Hl-Line overlay on the current line." |
57e46f94 RS |
158 | (if hl-line-overlay |
159 | (delete-overlay hl-line-overlay))) | |
160 | ||
23db85ff LK |
161 | ;;;###autoload |
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. | |
165 | ||
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'." | |
168 | :global t | |
169 | :group 'hl-line | |
170 | (if global-hl-line-mode | |
171 | (progn | |
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))) | |
177 | ||
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)) | |
9fd76d04 | 186 | (hl-line-move global-hl-line-overlay)))) |
23db85ff LK |
187 | |
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))) | |
192 | ||
9fd76d04 | 193 | (defun hl-line-move (overlay) |
a4c6ebf9 | 194 | "Move the Hl-Line overlay. |
9fd76d04 | 195 | If `hl-line-range-function' is non-nil, move the OVERLAY to the position |
a4c6ebf9 | 196 | where the function returns. If `hl-line-range-function' is nil, fill |
9fd76d04 MY |
197 | the line including the point by OVERLAY." |
198 | (let (tmp b e) | |
199 | (if hl-line-range-function | |
200 | (setq tmp (funcall hl-line-range-function) | |
201 | b (car tmp) | |
202 | e (cdr tmp)) | |
203 | (setq tmp t | |
204 | b (line-beginning-position) | |
205 | e (line-beginning-position 2))) | |
206 | (if tmp | |
207 | (move-overlay overlay b e) | |
208 | (move-overlay overlay 1 1)))) | |
209 | ||
798330fe DL |
210 | (provide 'hl-line) |
211 | ||
cbee283d | 212 | ;; arch-tag: ac806940-0876-4959-8c89-947563ee2833 |
798330fe | 213 | ;;; hl-line.el ends here |