*** empty log message ***
[emacs.git] / lisp / hl-line.el
blob8b372f281c393778ebc5776480f329e187bb2377
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 ;; Created: 1998-09-13
7 ;; Keywords: faces, frames, emulation
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
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
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.
40 ;; You probably don't really want to use the global mode; if the
41 ;; cursor is difficult to spot, try changing its colour, relying on
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.
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).
55 ;;; Code:
57 (defgroup hl-line nil
58 "Highlight the current line."
59 :version "21.1"
60 :group 'editing)
62 (defcustom hl-line-face 'highlight
63 "Face with which to highlight the current line."
64 :type 'face
65 :group 'hl-line)
67 (defcustom hl-line-sticky-flag t
68 "*Non-nil means highlight the current line in all windows.
69 Otherwise Hl-Line mode will highlight only in the selected
70 window. Setting this variable takes effect the next time you use
71 the command `hl-line-mode' to turn Hl-Line mode on."
72 :type 'boolean
73 :version "21.4"
74 :group 'hl-line)
76 (defvar hl-line-overlay nil
77 "Overlay used by Hl-Line mode to highlight the current line.")
78 (make-variable-buffer-local 'hl-line-overlay)
80 (defvar global-hl-line-overlay nil
81 "Overlay used by Global-Hl-Line mode to highlight the current line.")
83 ;;;###autoload
84 (define-minor-mode hl-line-mode
85 "Buffer-local minor mode to highlight the line about point.
86 With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
88 If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
89 line about the buffer's point in all windows. Caveat: the
90 buffer's point might be different from the point of a
91 non-selected window. Hl-Line mode uses the function
92 `hl-line-highlight' on `post-command-hook' in this case.
94 When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
95 line about point in the selected window only. In this case, it
96 uses the function `hl-line-unhighlight' on `pre-command-hook' in
97 addition to `hl-line-highlight' on `post-command-hook'."
98 nil nil nil
99 (if hl-line-mode
100 (progn
101 ;; In case `kill-all-local-variables' is called.
102 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
103 (if hl-line-sticky-flag
104 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)
105 (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
106 (hl-line-highlight)
107 (add-hook 'post-command-hook #'hl-line-highlight nil t))
108 (remove-hook 'post-command-hook #'hl-line-highlight t)
109 (hl-line-unhighlight)
110 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
111 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
113 (defun hl-line-highlight ()
114 "Active the Hl-Line overlay on the current line."
115 (if hl-line-mode ; Might be changed outside the mode function.
116 (progn
117 (unless hl-line-overlay
118 (setq hl-line-overlay (make-overlay 1 1)) ; to be moved
119 (overlay-put hl-line-overlay 'face hl-line-face))
120 (overlay-put hl-line-overlay
121 'window (unless hl-line-sticky-flag (selected-window)))
122 (move-overlay hl-line-overlay
123 (line-beginning-position) (line-beginning-position 2)))
124 (hl-line-unhighlight)))
126 (defun hl-line-unhighlight ()
127 "Deactivate the Hl-Line overlay on the current line."
128 (if hl-line-overlay
129 (delete-overlay hl-line-overlay)))
131 ;;;###autoload
132 (define-minor-mode global-hl-line-mode
133 "Global minor mode to highlight the line about point in the current window.
134 With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.
136 Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
137 `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
138 :global t
139 :group 'hl-line
140 (if global-hl-line-mode
141 (progn
142 (add-hook 'pre-command-hook #'global-hl-line-unhighlight)
143 (add-hook 'post-command-hook #'global-hl-line-highlight))
144 (global-hl-line-unhighlight)
145 (remove-hook 'pre-command-hook #'global-hl-line-unhighlight)
146 (remove-hook 'post-command-hook #'global-hl-line-highlight)))
148 (defun global-hl-line-highlight ()
149 "Active the Global-Hl-Line overlay on the current line in the current window."
150 (when global-hl-line-mode ; Might be changed outside the mode function.
151 (unless (window-minibuffer-p (selected-window))
152 (unless global-hl-line-overlay
153 (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved
154 (overlay-put global-hl-line-overlay 'face hl-line-face))
155 (overlay-put global-hl-line-overlay 'window (selected-window))
156 (move-overlay global-hl-line-overlay
157 (line-beginning-position) (line-beginning-position 2)))))
159 (defun global-hl-line-unhighlight ()
160 "Deactivate the Global-Hl-Line overlay on the current line."
161 (if global-hl-line-overlay
162 (delete-overlay global-hl-line-overlay)))
164 (provide 'hl-line)
166 ;;; arch-tag: ac806940-0876-4959-8c89-947563ee2833
167 ;;; hl-line.el ends here