(elint-check-defcustom-form): Don't use `evenp' so we don't implicitly
[emacs.git] / lisp / hl-line.el
blob58921aaa58a3bbae446d74c2d808163899b82236
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 ;;; Code:
62 (defgroup hl-line nil
63 "Highlight the current line."
64 :version "21.1"
65 :group 'editing)
67 (defcustom hl-line-face 'highlight
68 "Face with which to highlight the current line."
69 :type 'face
70 :group 'hl-line)
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)
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)
85 (defvar global-hl-line-overlay nil
86 "Overlay used by Global-Hl-Line mode to highlight the current line.")
88 ;;;###autoload
89 (define-minor-mode hl-line-mode
90 "Buffer-local minor mode to highlight the line about point.
91 With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
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.
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'."
103 nil nil nil
104 (if hl-line-mode
105 (progn
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)
112 (add-hook 'post-command-hook #'hl-line-highlight nil t))
113 (remove-hook 'post-command-hook #'hl-line-highlight t)
114 (hl-line-unhighlight)
115 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
116 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
118 (defun hl-line-highlight ()
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
128 (line-beginning-position) (line-beginning-position 2)))
129 (hl-line-unhighlight)))
131 (defun hl-line-unhighlight ()
132 "Deactivate the Hl-Line overlay on the current line."
133 (if hl-line-overlay
134 (delete-overlay hl-line-overlay)))
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.
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)))
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
162 (line-beginning-position) (line-beginning-position 2)))))
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)))
169 (provide 'hl-line)
171 ;;; arch-tag: ac806940-0876-4959-8c89-947563ee2833
172 ;;; hl-line.el ends here