Document new find-file-hook operation.
[bpt/emacs.git] / lisp / hl-line.el
CommitLineData
798330fe
DL
1;;; hl-line.el --- highlight the current line
2
23db85ff 3;; Copyright (C) 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
798330fe
DL
4
5;; Author: Dave Love <fx@gnu.org>
e2f9e1b2 6;; Maintainer: FSF
798330fe 7;; Created: 1998-09-13
3a850efa 8;; Keywords: faces, frames, emulation
798330fe 9
643c911e
DL
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
798330fe
DL
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
643c911e 17;; GNU Emacs is distributed in the hope that it will be useful,
798330fe
DL
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
e5f06fce
EZ
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
23db85ff
LK
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.
40
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.
46
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
0052fe2a
LK
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
798330fe
DL
60;;; Code:
61
62(defgroup hl-line nil
99f2fb1f 63 "Highlight the current line."
eee06d26 64 :version "21.1"
798330fe
DL
65 :group 'editing)
66
798330fe
DL
67(defcustom hl-line-face 'highlight
68 "Face with which to highlight the current line."
69 :type 'face
70 :group 'hl-line)
71
23db85ff
LK
72(defcustom hl-line-sticky-flag t
73 "*Non-nil means highlight the current line in all windows.
74Otherwise Hl-Line mode will highlight only in the selected
75window. Setting this variable takes effect the next time you use
76the 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.")
798330fe 87
ceafc2fa 88;;;###autoload
3a850efa 89(define-minor-mode hl-line-mode
23db85ff 90 "Buffer-local minor mode to highlight the line about point.
798330fe 91With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
23db85ff
LK
92
93If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
94line about the buffer's point in all windows. Caveat: the
95buffer's point might be different from the point of a
96non-selected window. Hl-Line mode uses the function
97`hl-line-highlight' on `post-command-hook' in this case.
98
99When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
100line about point in the selected window only. In this case, it
101uses the function `hl-line-unhighlight' on `pre-command-hook' in
102addition to `hl-line-highlight' on `post-command-hook'."
08ead67b 103 nil nil nil
3a850efa
DL
104 (if hl-line-mode
105 (progn
23db85ff
LK
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)
9df382fe 112 (add-hook 'post-command-hook #'hl-line-highlight nil t))
23db85ff 113 (remove-hook 'post-command-hook #'hl-line-highlight t)
3a850efa 114 (hl-line-unhighlight)
23db85ff
LK
115 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
116 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
b184b2dd 117
57e46f94 118(defun hl-line-highlight ()
23db85ff
LK
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
733b241e 128 (line-beginning-position) (line-beginning-position 2)))
23db85ff 129 (hl-line-unhighlight)))
57e46f94
RS
130
131(defun hl-line-unhighlight ()
23db85ff 132 "Deactivate the Hl-Line overlay on the current line."
57e46f94
RS
133 (if hl-line-overlay
134 (delete-overlay hl-line-overlay)))
135
23db85ff
LK
136;;;###autoload
137(define-minor-mode global-hl-line-mode
138 "Global minor mode to highlight the line about point in the current window.
139With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.
140
141Global-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
733b241e 162 (line-beginning-position) (line-beginning-position 2)))))
23db85ff
LK
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
798330fe
DL
169(provide 'hl-line)
170
ab5796a9 171;;; arch-tag: ac806940-0876-4959-8c89-947563ee2833
798330fe 172;;; hl-line.el ends here