Version 2.0.34 (of Tramp) released.
[emacs.git] / lisp / hl-line.el
blob3ad75ae78fa8153c1d8ab54608ba684e867da872
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 buffer-local minor mode (toggled by M-x hl-line-mode)
29 ;; and 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 ;; You could make variable `global-hl-line-mode' buffer-local and set
56 ;; it to nil to avoid highlighting specific buffers, when the global
57 ;; mode is used.
59 ;;; Code:
61 (defgroup hl-line nil
62 "Highlight the current line."
63 :version "21.1"
64 :group 'editing)
66 (defcustom hl-line-face 'highlight
67 "Face with which to highlight the current line."
68 :type 'face
69 :group 'hl-line)
71 (defcustom hl-line-sticky-flag t
72 "*Non-nil means highlight the current line in all windows.
73 Otherwise Hl-Line mode will highlight only in the selected
74 window. Setting this variable takes effect the next time you use
75 the command `hl-line-mode' to turn Hl-Line mode on."
76 :type 'boolean
77 :version "21.4"
78 :group 'hl-line)
80 (defvar hl-line-overlay nil
81 "Overlay used by Hl-Line mode to highlight the current line.")
82 (make-variable-buffer-local 'hl-line-overlay)
84 (defvar global-hl-line-overlay nil
85 "Overlay used by Global-Hl-Line mode to highlight the current line.")
87 ;;;###autoload
88 (define-minor-mode hl-line-mode
89 "Buffer-local minor mode to highlight the line about point.
90 With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
92 If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
93 line about the buffer's point in all windows. Caveat: the
94 buffer's point might be different from the point of a
95 non-selected window. Hl-Line mode uses the function
96 `hl-line-highlight' on `post-command-hook' in this case.
98 When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
99 line about point in the selected window only. In this case, it
100 uses the function `hl-line-unhighlight' on `pre-command-hook' in
101 addition to `hl-line-highlight' on `post-command-hook'."
102 nil nil nil
103 (if hl-line-mode
104 (progn
105 ;; In case `kill-all-local-variables' is called.
106 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
107 (if hl-line-sticky-flag
108 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)
109 (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
110 (hl-line-highlight)
111 (add-hook 'post-command-hook #'hl-line-highlight nil t))
112 (remove-hook 'post-command-hook #'hl-line-highlight t)
113 (hl-line-unhighlight)
114 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
115 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
117 (defun hl-line-highlight ()
118 "Active the Hl-Line overlay on the current line."
119 (if hl-line-mode ; Might be changed outside the mode function.
120 (progn
121 (unless hl-line-overlay
122 (setq hl-line-overlay (make-overlay 1 1)) ; to be moved
123 (overlay-put hl-line-overlay 'face hl-line-face))
124 (overlay-put hl-line-overlay
125 'window (unless hl-line-sticky-flag (selected-window)))
126 (move-overlay hl-line-overlay
127 (line-beginning-position) (line-beginning-position 2)))
128 (hl-line-unhighlight)))
130 (defun hl-line-unhighlight ()
131 "Deactivate the Hl-Line overlay on the current line."
132 (if hl-line-overlay
133 (delete-overlay hl-line-overlay)))
135 ;;;###autoload
136 (define-minor-mode global-hl-line-mode
137 "Global minor mode to highlight the line about point in the current window.
138 With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.
140 Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
141 `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
142 :global t
143 :group 'hl-line
144 (if global-hl-line-mode
145 (progn
146 (add-hook 'pre-command-hook #'global-hl-line-unhighlight)
147 (add-hook 'post-command-hook #'global-hl-line-highlight))
148 (global-hl-line-unhighlight)
149 (remove-hook 'pre-command-hook #'global-hl-line-unhighlight)
150 (remove-hook 'post-command-hook #'global-hl-line-highlight)))
152 (defun global-hl-line-highlight ()
153 "Active the Global-Hl-Line overlay on the current line in the current window."
154 (when global-hl-line-mode ; Might be changed outside the mode function.
155 (unless (window-minibuffer-p (selected-window))
156 (unless global-hl-line-overlay
157 (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved
158 (overlay-put global-hl-line-overlay 'face hl-line-face))
159 (overlay-put global-hl-line-overlay 'window (selected-window))
160 (move-overlay global-hl-line-overlay
161 (line-beginning-position) (line-beginning-position 2)))))
163 (defun global-hl-line-unhighlight ()
164 "Deactivate the Global-Hl-Line overlay on the current line."
165 (if global-hl-line-overlay
166 (delete-overlay global-hl-line-overlay)))
168 (provide 'hl-line)
170 ;;; hl-line.el ends here