Make NS port use the normal dnd functions.
[bpt/emacs.git] / lisp / hl-line.el
CommitLineData
798330fe
DL
1;;; hl-line.el --- highlight the current line
2
ab422c4d 3;; Copyright (C) 1998, 2000-2013 Free Software Foundation, Inc.
798330fe
DL
4
5;; Author: Dave Love <fx@gnu.org>
e2f9e1b2 6;; Maintainer: FSF
798330fe 7;; Created: 1998-09-13
39f8a48b 8;; Keywords: faces, frames, emulations
798330fe 9
643c911e
DL
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
798330fe 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
798330fe 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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
798330fe
DL
24
25;;; Commentary:
26
e5f06fce
EZ
27;; Provides a local minor mode (toggled by M-x hl-line-mode) and
28;; a global minor mode (toggled by M-x global-hl-line-mode) to
23db85ff
LK
29;; highlight, on a suitable terminal, the line on which point is. The
30;; global mode highlights the current line in the selected window only
31;; (except when the minibuffer window is selected). This was
32;; implemented to satisfy a request for a feature of Lesser Editors.
33;; The local mode is sticky: it highlights the line about the buffer's
34;; point even if the buffer's window is not selected. Caveat: the
35;; buffer's point might be different from the point of a non-selected
36;; window. Set the variable `hl-line-sticky-flag' to nil to make the
37;; local mode behave like the global mode.
38
39;; You probably don't really want to use the global mode; if the
6772c8e1 40;; cursor is difficult to spot, try changing its color, relying on
23db85ff
LK
41;; `blink-cursor-mode' or both. The hookery used might affect
42;; response noticeably on a slow machine. The local mode may be
43;; useful in non-editing buffers such as Gnus or PCL-CVS though.
44
45;; An overlay is used. In the non-sticky cases, this overlay is
46;; active only on the selected window. A hook is added to
47;; `post-command-hook' to activate the overlay and move it to the line
48;; about point. To get the non-sticky behavior, `hl-line-unhighlight'
49;; is added to `pre-command-hook' as well. This function deactivates
50;; the overlay unconditionally in case the command changes the
51;; selected window. (It does so rather than keeping track of changes
52;; in the selected window).
53
0052fe2a
LK
54;; You could make variable `global-hl-line-mode' buffer-local and set
55;; it to nil to avoid highlighting specific buffers, when the global
56;; mode is used.
57
a4c6ebf9 58;; By default the whole line is highlighted. The range of highlighting
bf247b6e 59;; can be changed by defining an appropriate function as the
9fd76d04
MY
60;; buffer-local value of `hl-line-range-function'.
61
798330fe
DL
62;;; Code:
63
3775cb5c
CY
64(defvar hl-line-overlay nil
65 "Overlay used by Hl-Line mode to highlight the current line.")
66(make-variable-buffer-local 'hl-line-overlay)
67
68(defvar global-hl-line-overlay nil
69 "Overlay used by Global-Hl-Line mode to highlight the current line.")
70
798330fe 71(defgroup hl-line nil
99f2fb1f 72 "Highlight the current line."
eee06d26 73 :version "21.1"
eba5b4dd 74 :group 'convenience)
798330fe 75
3775cb5c
CY
76(defface hl-line
77 '((t :inherit highlight))
78 "Default face for highlighting the current line in Hl-Line mode."
79 :version "22.1"
798330fe
DL
80 :group 'hl-line)
81
3775cb5c
CY
82(defcustom hl-line-face 'hl-line
83 "Face with which to highlight the current line in Hl-Line mode."
84 :type 'face
85 :group 'hl-line
86 :set (lambda (symbol value)
87 (set symbol value)
88 (dolist (buffer (buffer-list))
89 (with-current-buffer buffer
90 (when hl-line-overlay
91 (overlay-put hl-line-overlay 'face hl-line-face))))
92 (when global-hl-line-overlay
93 (overlay-put global-hl-line-overlay 'face hl-line-face))))
94
23db85ff 95(defcustom hl-line-sticky-flag t
88821ca0 96 "Non-nil means the HL-Line mode highlight appears in all windows.
23db85ff
LK
97Otherwise Hl-Line mode will highlight only in the selected
98window. Setting this variable takes effect the next time you use
88821ca0
CY
99the command `hl-line-mode' to turn Hl-Line mode on.
100
101This variable has no effect in Global Highlight Line mode.
102For that, use `global-hl-line-sticky-flag'."
23db85ff 103 :type 'boolean
bf247b6e 104 :version "22.1"
23db85ff
LK
105 :group 'hl-line)
106
88821ca0
CY
107(defcustom global-hl-line-sticky-flag nil
108 "Non-nil means the Global HL-Line mode highlight appears in all windows.
109Otherwise Global Hl-Line mode will highlight only in the selected
110window. Setting this variable takes effect the next time you use
111the command `global-hl-line-mode' to turn Global Hl-Line mode on."
112 :type 'boolean
113 :version "24.1"
114 :group 'hl-line)
115
9fd76d04
MY
116(defvar hl-line-range-function nil
117 "If non-nil, function to call to return highlight range.
118The function of no args should return a cons cell; its car value
bf247b6e 119is the beginning position of highlight and its cdr value is the
9fd76d04
MY
120end position of highlight in the buffer.
121It should return nil if there's no region to be highlighted.
122
123This variable is expected to be made buffer-local by modes.")
124
ceafc2fa 125;;;###autoload
3a850efa 126(define-minor-mode hl-line-mode
06e21633
CY
127 "Toggle highlighting of the current line (Hl-Line mode).
128With a prefix argument ARG, enable Hl-Line mode if ARG is
129positive, and disable it otherwise. If called from Lisp, enable
130the mode if ARG is omitted or nil.
23db85ff 131
06e21633
CY
132Hl-Line mode is a buffer-local minor mode. If
133`hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
23db85ff
LK
134line about the buffer's point in all windows. Caveat: the
135buffer's point might be different from the point of a
136non-selected window. Hl-Line mode uses the function
137`hl-line-highlight' on `post-command-hook' in this case.
138
139When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
140line about point in the selected window only. In this case, it
141uses the function `hl-line-unhighlight' on `pre-command-hook' in
142addition to `hl-line-highlight' on `post-command-hook'."
b862cd15 143 :group 'hl-line
3a850efa
DL
144 (if hl-line-mode
145 (progn
23db85ff
LK
146 ;; In case `kill-all-local-variables' is called.
147 (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
148 (if hl-line-sticky-flag
149 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)
150 (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
151 (hl-line-highlight)
9df382fe 152 (add-hook 'post-command-hook #'hl-line-highlight nil t))
23db85ff 153 (remove-hook 'post-command-hook #'hl-line-highlight t)
3a850efa 154 (hl-line-unhighlight)
23db85ff
LK
155 (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
156 (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
b184b2dd 157
57e46f94 158(defun hl-line-highlight ()
a4c6ebf9 159 "Activate the Hl-Line overlay on the current line."
23db85ff
LK
160 (if hl-line-mode ; Might be changed outside the mode function.
161 (progn
162 (unless hl-line-overlay
163 (setq hl-line-overlay (make-overlay 1 1)) ; to be moved
164 (overlay-put hl-line-overlay 'face hl-line-face))
165 (overlay-put hl-line-overlay
166 'window (unless hl-line-sticky-flag (selected-window)))
9fd76d04 167 (hl-line-move hl-line-overlay))
23db85ff 168 (hl-line-unhighlight)))
57e46f94
RS
169
170(defun hl-line-unhighlight ()
23db85ff 171 "Deactivate the Hl-Line overlay on the current line."
b1bad9f3
JB
172 (when hl-line-overlay
173 (delete-overlay hl-line-overlay)))
57e46f94 174
23db85ff
LK
175;;;###autoload
176(define-minor-mode global-hl-line-mode
06e21633
CY
177 "Toggle line highlighting in all buffers (Global Hl-Line mode).
178With a prefix argument ARG, enable Global Hl-Line mode if ARG is
179positive, and disable it otherwise. If called from Lisp, enable
180the mode if ARG is omitted or nil.
23db85ff 181
88821ca0
CY
182If `global-hl-line-sticky-flag' is non-nil, Global Hl-Line mode
183highlights the line about the current buffer's point in all
184windows.
185
23db85ff
LK
186Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
187`global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
188 :global t
189 :group 'hl-line
190 (if global-hl-line-mode
191 (progn
192 (add-hook 'pre-command-hook #'global-hl-line-unhighlight)
193 (add-hook 'post-command-hook #'global-hl-line-highlight))
194 (global-hl-line-unhighlight)
195 (remove-hook 'pre-command-hook #'global-hl-line-unhighlight)
196 (remove-hook 'post-command-hook #'global-hl-line-highlight)))
197
198(defun global-hl-line-highlight ()
503edac9 199 "Highlight the current line in the current window."
23db85ff 200 (when global-hl-line-mode ; Might be changed outside the mode function.
290d5b58 201 (unless (window-minibuffer-p)
23db85ff
LK
202 (unless global-hl-line-overlay
203 (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved
204 (overlay-put global-hl-line-overlay 'face hl-line-face))
88821ca0
CY
205 (overlay-put global-hl-line-overlay 'window
206 (unless global-hl-line-sticky-flag
207 (selected-window)))
9fd76d04 208 (hl-line-move global-hl-line-overlay))))
23db85ff
LK
209
210(defun global-hl-line-unhighlight ()
211 "Deactivate the Global-Hl-Line overlay on the current line."
b1bad9f3
JB
212 (when global-hl-line-overlay
213 (delete-overlay global-hl-line-overlay)))
23db85ff 214
9fd76d04 215(defun hl-line-move (overlay)
a4c6ebf9 216 "Move the Hl-Line overlay.
9fd76d04 217If `hl-line-range-function' is non-nil, move the OVERLAY to the position
a4c6ebf9 218where the function returns. If `hl-line-range-function' is nil, fill
9fd76d04
MY
219the line including the point by OVERLAY."
220 (let (tmp b e)
221 (if hl-line-range-function
222 (setq tmp (funcall hl-line-range-function)
223 b (car tmp)
224 e (cdr tmp))
225 (setq tmp t
226 b (line-beginning-position)
227 e (line-beginning-position 2)))
228 (if tmp
229 (move-overlay overlay b e)
230 (move-overlay overlay 1 1))))
231
b1bad9f3
JB
232(defun hl-line-unload-function ()
233 "Unload the Hl-Line library."
234 (global-hl-line-mode -1)
235 (save-current-buffer
236 (dolist (buffer (buffer-list))
237 (set-buffer buffer)
238 (when hl-line-mode (hl-line-mode -1))))
239 ;; continue standard unloading
240 nil)
241
798330fe
DL
242(provide 'hl-line)
243
244;;; hl-line.el ends here