hl-line.el 8.5 KB
Newer Older
Dave Love's avatar
Dave Love committed
1 2
;;; hl-line.el --- highlight the current line

3
;; Copyright (C) 1998, 2000-2011  Free Software Foundation, Inc.
Dave Love's avatar
Dave Love committed
4 5

;; Author:  Dave Love <fx@gnu.org>
Dave Love's avatar
Dave Love committed
6
;; Maintainer: FSF
Dave Love's avatar
Dave Love committed
7
;; Created: 1998-09-13
8
;; Keywords: faces, frames, emulations
Dave Love's avatar
Dave Love committed
9

Dave Love's avatar
Dave Love committed
10 11
;; This file is part of GNU Emacs.

12
;; GNU Emacs is free software: you can redistribute it and/or modify
Dave Love's avatar
Dave Love committed
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Dave Love's avatar
Dave Love committed
16

Dave Love's avatar
Dave Love committed
17
;; GNU Emacs is distributed in the hope that it will be useful,
Dave Love's avatar
Dave Love committed
18 19 20 21 22
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Dave Love's avatar
Dave Love committed
24 25 26

;;; Commentary:

27 28
;; Provides a local minor mode (toggled by M-x hl-line-mode) and
;; a global minor mode (toggled by M-x global-hl-line-mode) to
29 30 31 32 33 34 35 36 37 38 39
;; highlight, on a suitable terminal, the line on which point is.  The
;; global mode highlights the current line in the selected window only
;; (except when the minibuffer window is selected).  This was
;; implemented to satisfy a request for a feature of Lesser Editors.
;; The local mode is sticky: it highlights the line about the buffer's
;; point even if the buffer's window is not selected.  Caveat: the
;; buffer's point might be different from the point of a non-selected
;; window.  Set the variable `hl-line-sticky-flag' to nil to make the
;; local mode behave like the global mode.

;; You probably don't really want to use the global mode; if the
Glenn Morris's avatar
Glenn Morris committed
40
;; cursor is difficult to spot, try changing its color, relying on
41 42 43 44 45 46 47 48 49 50 51 52 53
;; `blink-cursor-mode' or both.  The hookery used might affect
;; response noticeably on a slow machine.  The local mode may be
;; useful in non-editing buffers such as Gnus or PCL-CVS though.

;; An overlay is used.  In the non-sticky cases, this overlay is
;; active only on the selected window.  A hook is added to
;; `post-command-hook' to activate the overlay and move it to the line
;; about point.  To get the non-sticky behavior, `hl-line-unhighlight'
;; is added to `pre-command-hook' as well.  This function deactivates
;; the overlay unconditionally in case the command changes the
;; selected window.  (It does so rather than keeping track of changes
;; in the selected window).

54 55 56 57
;; You could make variable `global-hl-line-mode' buffer-local and set
;; it to nil to avoid highlighting specific buffers, when the global
;; mode is used.

58
;; By default the whole line is highlighted.  The range of highlighting
59
;; can be changed by defining an appropriate function as the
60 61
;; buffer-local value of `hl-line-range-function'.

Dave Love's avatar
Dave Love committed
62 63
;;; Code:

64 65 66 67 68 69 70
(defvar hl-line-overlay nil
  "Overlay used by Hl-Line mode to highlight the current line.")
(make-variable-buffer-local 'hl-line-overlay)

(defvar global-hl-line-overlay nil
  "Overlay used by Global-Hl-Line mode to highlight the current line.")

Dave Love's avatar
Dave Love committed
71
(defgroup hl-line nil
Dave Love's avatar
Dave Love committed
72
  "Highlight the current line."
Dave Love's avatar
Dave Love committed
73
  :version "21.1"
Chong Yidong's avatar
Chong Yidong committed
74
  :group 'convenience)
Dave Love's avatar
Dave Love committed
75

76 77 78 79
(defface hl-line
  '((t :inherit highlight))
  "Default face for highlighting the current line in Hl-Line mode."
  :version "22.1"
Dave Love's avatar
Dave Love committed
80 81
  :group 'hl-line)

82 83 84 85 86 87 88 89 90 91 92 93 94
(defcustom hl-line-face 'hl-line
  "Face with which to highlight the current line in Hl-Line mode."
  :type 'face
  :group 'hl-line
  :set (lambda (symbol value)
	 (set symbol value)
	 (dolist (buffer (buffer-list))
	   (with-current-buffer buffer
	     (when hl-line-overlay
	       (overlay-put hl-line-overlay 'face hl-line-face))))
	 (when global-hl-line-overlay
	   (overlay-put global-hl-line-overlay 'face hl-line-face))))

95
(defcustom hl-line-sticky-flag t
96
  "Non-nil means highlight the current line in all windows.
97 98 99 100
Otherwise Hl-Line mode will highlight only in the selected
window.  Setting this variable takes effect the next time you use
the command `hl-line-mode' to turn Hl-Line mode on."
  :type 'boolean
101
  :version "22.1"
102 103
  :group 'hl-line)

104 105 106
(defvar hl-line-range-function nil
  "If non-nil, function to call to return highlight range.
The function of no args should return a cons cell; its car value
107
is the beginning position of highlight and its cdr value is the
108 109 110 111 112
end position of highlight in the buffer.
It should return nil if there's no region to be highlighted.

This variable is expected to be made buffer-local by modes.")

113
;;;###autoload
Dave Love's avatar
Dave Love committed
114
(define-minor-mode hl-line-mode
115
  "Buffer-local minor mode to highlight the line about point.
Dave Love's avatar
Dave Love committed
116
With ARG, turn Hl-Line mode on if ARG is positive, off otherwise.
117 118 119 120 121 122 123 124 125 126 127

If `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the
line about the buffer's point in all windows.  Caveat: the
buffer's point might be different from the point of a
non-selected window.  Hl-Line mode uses the function
`hl-line-highlight' on `post-command-hook' in this case.

When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the
line about point in the selected window only.  In this case, it
uses the function `hl-line-unhighlight' on `pre-command-hook' in
addition to `hl-line-highlight' on `post-command-hook'."
Lute Kamstra's avatar
Lute Kamstra committed
128
  :group 'hl-line
Dave Love's avatar
Dave Love committed
129 130
  (if hl-line-mode
      (progn
131 132 133 134 135 136
        ;; In case `kill-all-local-variables' is called.
        (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t)
        (if hl-line-sticky-flag
            (remove-hook 'pre-command-hook #'hl-line-unhighlight t)
          (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
        (hl-line-highlight)
Lute Kamstra's avatar
Lute Kamstra committed
137
	(add-hook 'post-command-hook #'hl-line-highlight nil t))
138
    (remove-hook 'post-command-hook #'hl-line-highlight t)
Dave Love's avatar
Dave Love committed
139
    (hl-line-unhighlight)
140 141
    (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t)
    (remove-hook 'pre-command-hook #'hl-line-unhighlight t)))
142

143
(defun hl-line-highlight ()
144
  "Activate the Hl-Line overlay on the current line."
145 146 147 148 149 150 151
  (if hl-line-mode	; Might be changed outside the mode function.
      (progn
        (unless hl-line-overlay
          (setq hl-line-overlay (make-overlay 1 1)) ; to be moved
          (overlay-put hl-line-overlay 'face hl-line-face))
        (overlay-put hl-line-overlay
                     'window (unless hl-line-sticky-flag (selected-window)))
152
	(hl-line-move hl-line-overlay))
153
    (hl-line-unhighlight)))
154 155

(defun hl-line-unhighlight ()
156
  "Deactivate the Hl-Line overlay on the current line."
157 158
  (when hl-line-overlay
    (delete-overlay hl-line-overlay)))
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
;;;###autoload
(define-minor-mode global-hl-line-mode
  "Global minor mode to highlight the line about point in the current window.
With ARG, turn Global-Hl-Line mode on if ARG is positive, off otherwise.

Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and
`global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'."
  :global t
  :group 'hl-line
  (if global-hl-line-mode
      (progn
	(add-hook 'pre-command-hook #'global-hl-line-unhighlight)
	(add-hook 'post-command-hook #'global-hl-line-highlight))
    (global-hl-line-unhighlight)
    (remove-hook 'pre-command-hook #'global-hl-line-unhighlight)
    (remove-hook 'post-command-hook #'global-hl-line-highlight)))

(defun global-hl-line-highlight ()
178
  "Highlight the current line in the current window."
179 180 181 182 183 184
  (when global-hl-line-mode	; Might be changed outside the mode function.
    (unless (window-minibuffer-p (selected-window))
      (unless global-hl-line-overlay
        (setq global-hl-line-overlay (make-overlay 1 1)) ; to be moved
        (overlay-put global-hl-line-overlay 'face hl-line-face))
      (overlay-put global-hl-line-overlay 'window (selected-window))
185
      (hl-line-move global-hl-line-overlay))))
186 187 188

(defun global-hl-line-unhighlight ()
  "Deactivate the Global-Hl-Line overlay on the current line."
189 190
  (when global-hl-line-overlay
    (delete-overlay global-hl-line-overlay)))
191

192
(defun hl-line-move (overlay)
193
  "Move the Hl-Line overlay.
194
If `hl-line-range-function' is non-nil, move the OVERLAY to the position
195
where the function returns.  If `hl-line-range-function' is nil, fill
196 197 198 199 200 201 202 203 204 205 206 207 208
the line including the point by OVERLAY."
  (let (tmp b e)
    (if hl-line-range-function
	(setq tmp (funcall hl-line-range-function)
	      b   (car tmp)
	      e   (cdr tmp))
      (setq tmp t
	    b (line-beginning-position)
	    e (line-beginning-position 2)))
    (if tmp
	(move-overlay overlay b e)
      (move-overlay overlay 1 1))))

209 210 211 212 213 214 215 216 217 218
(defun hl-line-unload-function ()
  "Unload the Hl-Line library."
  (global-hl-line-mode -1)
  (save-current-buffer
    (dolist (buffer (buffer-list))
      (set-buffer buffer)
      (when hl-line-mode (hl-line-mode -1))))
  ;; continue standard unloading
  nil)

Dave Love's avatar
Dave Love committed
219 220 221
(provide 'hl-line)

;;; hl-line.el ends here