24961
|
1 ;;; hl-line.el --- highlight the current line
|
|
2
|
51010
|
3 ;; Copyright (C) 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
|
24961
|
4
|
|
5 ;; Author: Dave Love <fx@gnu.org>
|
52777
|
6 ;; Maintainer: FSF
|
24961
|
7 ;; Created: 1998-09-13
|
29369
|
8 ;; Keywords: faces, frames, emulation
|
24961
|
9
|
29087
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
24961
|
13 ;; it under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
29087
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
24961
|
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; see the file COPYING. If not, write to the
|
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
52496
|
29 ;; Provides a local minor mode (toggled by M-x hl-line-mode) and
|
|
30 ;; a global minor mode (toggled by M-x global-hl-line-mode) to
|
51010
|
31 ;; highlight, on a suitable terminal, the line on which point is. The
|
|
32 ;; global mode highlights the current line in the selected window only
|
|
33 ;; (except when the minibuffer window is selected). This was
|
|
34 ;; implemented to satisfy a request for a feature of Lesser Editors.
|
|
35 ;; The local mode is sticky: it highlights the line about the buffer's
|
|
36 ;; point even if the buffer's window is not selected. Caveat: the
|
|
37 ;; buffer's point might be different from the point of a non-selected
|
|
38 ;; window. Set the variable `hl-line-sticky-flag' to nil to make the
|
|
39 ;; local mode behave like the global mode.
|
24961
|
40
|
51010
|
41 ;; You probably don't really want to use the global mode; if the
|
|
42 ;; cursor is difficult to spot, try changing its colour, relying on
|
|
43 ;; `blink-cursor-mode' or both. The hookery used might affect
|
|
44 ;; response noticeably on a slow machine. The local mode may be
|
|
45 ;; useful in non-editing buffers such as Gnus or PCL-CVS though.
|
24961
|
46
|
51010
|
47 ;; An overlay is used. In the non-sticky cases, this overlay is
|
|
48 ;; active only on the selected window. A hook is added to
|
|
49 ;; `post-command-hook' to activate the overlay and move it to the line
|
|
50 ;; about point. To get the non-sticky behavior, `hl-line-unhighlight'
|
|
51 ;; is added to `pre-command-hook' as well. This function deactivates
|
|
52 ;; the overlay unconditionally in case the command changes the
|
|
53 ;; selected window. (It does so rather than keeping track of changes
|
|
54 ;; in the selected window).
|
|
55
|
52834
|
56 ;; You could make variable `global-hl-line-mode' buffer-local and set
|
|
57 ;; it to nil to avoid highlighting specific buffers, when the global
|
|
58 ;; mode is used.
|
|
59
|
24961
|
60 ;;; Code:
|
|
61
|
|
62 (defgroup hl-line nil
|
35031
|
63 "Highlight the current line."
|
25174
|
64 :version "21.1"
|
24961
|
65 :group 'editing)
|
|
66
|
|
67 (defcustom hl-line-face 'highlight
|
|
68 "Face with which to highlight the current line."
|
|
69 :type 'face
|
|
70 :group 'hl-line)
|
|
71
|
51010
|
72 (defcustom hl-line-sticky-flag t
|
|
73 "*Non-nil means highlight the current line in all windows.
|
|
74 Otherwise Hl-Line mode will highlight only in the selected
|
|
75 window. Setting this variable takes effect the next time you use
|
|
76 the command `hl-line-mode' to turn Hl-Line mode on."
|
|
77 :type 'boolean
|
|
78 :version "21.4"
|
|
79 :group 'hl-line)
|
|
80
|
|
81 (defvar hl-line-overlay nil
|
|
82 "Overlay used by Hl-Line mode to highlight the current line.")
|
|
83 (make-variable-buffer-local 'hl-line-overlay)
|
|
84
|
|
85 (defvar global-hl-line-overlay nil
|
|
86 "Overlay used by Global-Hl-Line mode to highlight the current line.")
|
24961
|
87
|
25051
|
88 ;;;###autoload
|
29369
|
89 (define-minor-mode hl-line-mode
|
51010
|
90 "Buffer-local minor mode to highlight the line about point.
|
24961
|
91 With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
|
51010
|
92
|
|
93 If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
|
|
94 line about the buffer's point in all windows. Caveat: the
|
|
95 buffer's point might be different from the point of a
|
|
96 non-selected window. Hl-Line mode uses the function
|
|
97 `hl-line-highlight' on `post-command-hook' in this case.
|
|
98
|
|
99 When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
|
|
100 line about point in the selected window only. In this case, it
|
|
101 uses the function `hl-line-unhighlight' on `pre-command-hook' in
|
|
102 addition to `hl-line-highlight' on `post-command-hook'."
|
38955
|
103 nil nil nil
|
29369
|
104 (if hl-line-mode
|
|
105 (progn
|
51010
|
106 ;; In case `kill-all-local-variables' is called.
|
|
107 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
|
|
108 (if hl-line-sticky-flag
|
|
109 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)
|
|
110 (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
|
|
111 (hl-line-highlight)
|
50847
|
112 (add-hook 'post-command-hook #'hl-line-highlight nil t))
|
51010
|
113 (remove-hook 'post-command-hook #'hl-line-highlight t)
|
29369
|
114 (hl-line-unhighlight)
|
51010
|
115 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
|
|
116 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
|
38937
|
117
|
43031
|
118 (defun hl-line-highlight ()
|
51010
|
119 "Active the Hl-Line overlay on the current line."
|
|
120 (if hl-line-mode ; Might be changed outside the mode function.
|
|
121 (progn
|
|
122 (unless hl-line-overlay
|
|
123 (setq hl-line-overlay (make-overlay 1 1)) ; to be moved
|
|
124 (overlay-put hl-line-overlay 'face hl-line-face))
|
|
125 (overlay-put hl-line-overlay
|
|
126 'window (unless hl-line-sticky-flag (selected-window)))
|
|
127 (move-overlay hl-line-overlay
|
51018
|
128 (line-beginning-position) (line-beginning-position 2)))
|
51010
|
129 (hl-line-unhighlight)))
|
43031
|
130
|
|
131 (defun hl-line-unhighlight ()
|
51010
|
132 "Deactivate the Hl-Line overlay on the current line."
|
43031
|
133 (if hl-line-overlay
|
|
134 (delete-overlay hl-line-overlay)))
|
|
135
|
51010
|
136 ;;;###autoload
|
|
137 (define-minor-mode global-hl-line-mode
|
|
138 "Global minor mode to highlight the line about point in the current window.
|
|
139 With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.
|
|
140
|
|
141 Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
|
|
142 `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
|
|
143 :global t
|
|
144 :group 'hl-line
|
|
145 (if global-hl-line-mode
|
|
146 (progn
|
|
147 (add-hook 'pre-command-hook #'global-hl-line-unhighlight)
|
|
148 (add-hook 'post-command-hook #'global-hl-line-highlight))
|
|
149 (global-hl-line-unhighlight)
|
|
150 (remove-hook 'pre-command-hook #'global-hl-line-unhighlight)
|
|
151 (remove-hook 'post-command-hook #'global-hl-line-highlight)))
|
|
152
|
|
153 (defun global-hl-line-highlight ()
|
|
154 "Active the Global-Hl-Line overlay on the current line in the current window."
|
|
155 (when global-hl-line-mode ; Might be changed outside the mode function.
|
|
156 (unless (window-minibuffer-p (selected-window))
|
|
157 (unless global-hl-line-overlay
|
|
158 (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved
|
|
159 (overlay-put global-hl-line-overlay 'face hl-line-face))
|
|
160 (overlay-put global-hl-line-overlay 'window (selected-window))
|
|
161 (move-overlay global-hl-line-overlay
|
51018
|
162 (line-beginning-position) (line-beginning-position 2)))))
|
51010
|
163
|
|
164 (defun global-hl-line-unhighlight ()
|
|
165 "Deactivate the Global-Hl-Line overlay on the current line."
|
|
166 (if global-hl-line-overlay
|
|
167 (delete-overlay global-hl-line-overlay)))
|
|
168
|
24961
|
169 (provide 'hl-line)
|
|
170
|
52401
|
171 ;;; arch-tag: ac806940-0876-4959-8c89-947563ee2833
|
24961
|
172 ;;; hl-line.el ends here
|