(Display Feature Testing): Explain about "vendor".
[emacs.git] / lisp / hl-line.el
blob5ed334f4049fa8e122e903835abdd578fa2414da
1 ;;; hl-line.el --- highlight the current line
3 ;; Copyright (C) 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Maintainer: FSF
7 ;; Created: 1998-09-13
8 ;; Keywords: faces, frames, emulation
10 ;; This file is part of GNU Emacs.
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 2, or (at your option)
15 ;; any later version.
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.
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.
27 ;;; Commentary:
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
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.
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.
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).
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.
60 ;; In default whole the line is highlighted. The range of highlighting
61 ;; can be changed by defining an appropriate function as the
62 ;; buffer-local value of `hl-line-range-function'.
64 ;;; Code:
66 (defgroup hl-line nil
67 "Highlight the current line."
68 :version "21.1"
69 :group 'editing)
71 (defcustom hl-line-face 'highlight
72 "Face with which to highlight the current line."
73 :type 'face
74 :group 'hl-line)
76 (defcustom hl-line-sticky-flag t
77 "*Non-nil means highlight the current line in all windows.
78 Otherwise Hl-Line mode will highlight only in the selected
79 window. Setting this variable takes effect the next time you use
80 the command `hl-line-mode' to turn Hl-Line mode on."
81 :type 'boolean
82 :version "21.4"
83 :group 'hl-line)
85 (defvar hl-line-range-function nil
86 "If non-nil, function to call to return highlight range.
87 The function of no args should return a cons cell; its car value
88 is the beginning position of highlight and its cdr value is the
89 end position of highlight in the buffer.
90 It should return nil if there's no region to be highlighted.
92 This variable is expected to be made buffer-local by modes.")
94 (defvar hl-line-overlay nil
95 "Overlay used by Hl-Line mode to highlight the current line.")
96 (make-variable-buffer-local 'hl-line-overlay)
98 (defvar global-hl-line-overlay nil
99 "Overlay used by Global-Hl-Line mode to highlight the current line.")
101 ;;;###autoload
102 (define-minor-mode hl-line-mode
103 "Buffer-local minor mode to highlight the line about point.
104 With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
106 If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
107 line about the buffer's point in all windows. Caveat: the
108 buffer's point might be different from the point of a
109 non-selected window. Hl-Line mode uses the function
110 `hl-line-highlight' on `post-command-hook' in this case.
112 When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
113 line about point in the selected window only. In this case, it
114 uses the function `hl-line-unhighlight' on `pre-command-hook' in
115 addition to `hl-line-highlight' on `post-command-hook'."
116 nil nil nil
117 (if hl-line-mode
118 (progn
119 ;; In case `kill-all-local-variables' is called.
120 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
121 (if hl-line-sticky-flag
122 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)
123 (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
124 (hl-line-highlight)
125 (add-hook 'post-command-hook #'hl-line-highlight nil t))
126 (remove-hook 'post-command-hook #'hl-line-highlight t)
127 (hl-line-unhighlight)
128 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
129 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
131 (defun hl-line-highlight ()
132 "Active the Hl-Line overlay on the current line."
133 (if hl-line-mode ; Might be changed outside the mode function.
134 (progn
135 (unless hl-line-overlay
136 (setq hl-line-overlay (make-overlay 1 1)) ; to be moved
137 (overlay-put hl-line-overlay 'face hl-line-face))
138 (overlay-put hl-line-overlay
139 'window (unless hl-line-sticky-flag (selected-window)))
140 (hl-line-move hl-line-overlay))
141 (hl-line-unhighlight)))
143 (defun hl-line-unhighlight ()
144 "Deactivate the Hl-Line overlay on the current line."
145 (if hl-line-overlay
146 (delete-overlay hl-line-overlay)))
148 ;;;###autoload
149 (define-minor-mode global-hl-line-mode
150 "Global minor mode to highlight the line about point in the current window.
151 With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.
153 Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
154 `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
155 :global t
156 :group 'hl-line
157 (if global-hl-line-mode
158 (progn
159 (add-hook 'pre-command-hook #'global-hl-line-unhighlight)
160 (add-hook 'post-command-hook #'global-hl-line-highlight))
161 (global-hl-line-unhighlight)
162 (remove-hook 'pre-command-hook #'global-hl-line-unhighlight)
163 (remove-hook 'post-command-hook #'global-hl-line-highlight)))
165 (defun global-hl-line-highlight ()
166 "Active the Global-Hl-Line overlay on the current line in the current window."
167 (when global-hl-line-mode ; Might be changed outside the mode function.
168 (unless (window-minibuffer-p (selected-window))
169 (unless global-hl-line-overlay
170 (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved
171 (overlay-put global-hl-line-overlay 'face hl-line-face))
172 (overlay-put global-hl-line-overlay 'window (selected-window))
173 (hl-line-move global-hl-line-overlay))))
175 (defun global-hl-line-unhighlight ()
176 "Deactivate the Global-Hl-Line overlay on the current line."
177 (if global-hl-line-overlay
178 (delete-overlay global-hl-line-overlay)))
180 (defun hl-line-move (overlay)
181 "Move the hl-line-mode overlay.
182 If `hl-line-range-function' is non-nil, move the OVERLAY to the position
183 where the function returns. If `hl-line-range-function' is nil, fill
184 the line including the point by OVERLAY."
185 (let (tmp b e)
186 (if hl-line-range-function
187 (setq tmp (funcall hl-line-range-function)
188 b (car tmp)
189 e (cdr tmp))
190 (setq tmp t
191 b (line-beginning-position)
192 e (line-beginning-position 2)))
193 (if tmp
194 (move-overlay overlay b e)
195 (move-overlay overlay 1 1))))
197 (provide 'hl-line)
199 ;;; arch-tag: ac806940-0876-4959-8c89-947563ee2833
200 ;;; hl-line.el ends here