| 1 | ;;; hl-line.el --- highlight the current line -*- lexical-binding:t -*- |
| 2 | |
| 3 | ;; Copyright (C) 1998, 2000-2013 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Dave Love <fx@gnu.org> |
| 6 | ;; Maintainer: FSF |
| 7 | ;; Created: 1998-09-13 |
| 8 | ;; Keywords: faces, frames, emulations |
| 9 | |
| 10 | ;; This file is part of GNU Emacs. |
| 11 | |
| 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 13 | ;; it under the terms of the GNU General Public License as published by |
| 14 | ;; the Free Software Foundation, either version 3 of the License, or |
| 15 | ;; (at your option) any later version. |
| 16 | |
| 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 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. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 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 |
| 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 |
| 40 | ;; cursor is difficult to spot, try changing its color, relying on |
| 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 | |
| 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 | |
| 58 | ;; By default the whole line is highlighted. The range of highlighting |
| 59 | ;; can be changed by defining an appropriate function as the |
| 60 | ;; buffer-local value of `hl-line-range-function'. |
| 61 | |
| 62 | ;;; Code: |
| 63 | |
| 64 | (defvar-local hl-line-overlay nil |
| 65 | "Overlay used by Hl-Line mode to highlight the current line.") |
| 66 | |
| 67 | (defvar global-hl-line-overlay nil |
| 68 | "Overlay used by Global-Hl-Line mode to highlight the current line.") |
| 69 | |
| 70 | (defgroup hl-line nil |
| 71 | "Highlight the current line." |
| 72 | :version "21.1" |
| 73 | :group 'convenience) |
| 74 | |
| 75 | (defface hl-line |
| 76 | '((t :inherit highlight)) |
| 77 | "Default face for highlighting the current line in Hl-Line mode." |
| 78 | :version "22.1" |
| 79 | :group 'hl-line) |
| 80 | |
| 81 | (defcustom hl-line-face 'hl-line |
| 82 | "Face with which to highlight the current line in Hl-Line mode." |
| 83 | :type 'face |
| 84 | :group 'hl-line |
| 85 | :set (lambda (symbol value) |
| 86 | (set symbol value) |
| 87 | (dolist (buffer (buffer-list)) |
| 88 | (with-current-buffer buffer |
| 89 | (when hl-line-overlay |
| 90 | (overlay-put hl-line-overlay 'face hl-line-face)))) |
| 91 | (when global-hl-line-overlay |
| 92 | (overlay-put global-hl-line-overlay 'face hl-line-face)))) |
| 93 | |
| 94 | (defcustom hl-line-sticky-flag t |
| 95 | "Non-nil means the HL-Line mode highlight appears in all windows. |
| 96 | Otherwise Hl-Line mode will highlight only in the selected |
| 97 | window. Setting this variable takes effect the next time you use |
| 98 | the command `hl-line-mode' to turn Hl-Line mode on. |
| 99 | |
| 100 | This variable has no effect in Global Highlight Line mode. |
| 101 | For that, use `global-hl-line-sticky-flag'." |
| 102 | :type 'boolean |
| 103 | :version "22.1" |
| 104 | :group 'hl-line) |
| 105 | |
| 106 | (defcustom global-hl-line-sticky-flag nil |
| 107 | "Non-nil means the Global HL-Line mode highlight appears in all windows. |
| 108 | Otherwise Global Hl-Line mode will highlight only in the selected |
| 109 | window. Setting this variable takes effect the next time you use |
| 110 | the command `global-hl-line-mode' to turn Global Hl-Line mode on." |
| 111 | :type 'boolean |
| 112 | :version "24.1" |
| 113 | :group 'hl-line) |
| 114 | |
| 115 | (defvar hl-line-range-function nil |
| 116 | "If non-nil, function to call to return highlight range. |
| 117 | The function of no args should return a cons cell; its car value |
| 118 | is the beginning position of highlight and its cdr value is the |
| 119 | end position of highlight in the buffer. |
| 120 | It should return nil if there's no region to be highlighted. |
| 121 | |
| 122 | This variable is expected to be made buffer-local by modes.") |
| 123 | |
| 124 | ;;;###autoload |
| 125 | (define-minor-mode hl-line-mode |
| 126 | "Toggle highlighting of the current line (Hl-Line mode). |
| 127 | With a prefix argument ARG, enable Hl-Line mode if ARG is |
| 128 | positive, and disable it otherwise. If called from Lisp, enable |
| 129 | the mode if ARG is omitted or nil. |
| 130 | |
| 131 | Hl-Line mode is a buffer-local minor mode. If |
| 132 | `hl-line-sticky-flag' is non-nil, Hl-Line mode highlights the |
| 133 | line about the buffer's point in all windows. Caveat: the |
| 134 | buffer's point might be different from the point of a |
| 135 | non-selected window. Hl-Line mode uses the function |
| 136 | `hl-line-highlight' on `post-command-hook' in this case. |
| 137 | |
| 138 | When `hl-line-sticky-flag' is nil, Hl-Line mode highlights the |
| 139 | line about point in the selected window only. In this case, it |
| 140 | uses the function `hl-line-unhighlight' on `pre-command-hook' in |
| 141 | addition to `hl-line-highlight' on `post-command-hook'." |
| 142 | :group 'hl-line |
| 143 | (if hl-line-mode |
| 144 | (progn |
| 145 | ;; In case `kill-all-local-variables' is called. |
| 146 | (add-hook 'change-major-mode-hook #'hl-line-unhighlight nil t) |
| 147 | (if hl-line-sticky-flag |
| 148 | (remove-hook 'pre-command-hook #'hl-line-unhighlight t) |
| 149 | (add-hook 'pre-command-hook #'hl-line-unhighlight nil t)) |
| 150 | (hl-line-highlight) |
| 151 | (add-hook 'post-command-hook #'hl-line-highlight nil t)) |
| 152 | (remove-hook 'post-command-hook #'hl-line-highlight t) |
| 153 | (hl-line-unhighlight) |
| 154 | (remove-hook 'change-major-mode-hook #'hl-line-unhighlight t) |
| 155 | (remove-hook 'pre-command-hook #'hl-line-unhighlight t))) |
| 156 | |
| 157 | (defun hl-line-make-overlay () |
| 158 | (let ((ol (make-overlay (point) (point)))) |
| 159 | (overlay-put ol 'priority -50) ;(bug#16192) |
| 160 | (overlay-put ol 'face hl-line-face) |
| 161 | ol)) |
| 162 | |
| 163 | (defun hl-line-highlight () |
| 164 | "Activate the Hl-Line overlay on the current line." |
| 165 | (if hl-line-mode ; Might be changed outside the mode function. |
| 166 | (progn |
| 167 | (unless hl-line-overlay |
| 168 | (setq hl-line-overlay (hl-line-make-overlay))) ; To be moved. |
| 169 | (overlay-put hl-line-overlay |
| 170 | 'window (unless hl-line-sticky-flag (selected-window))) |
| 171 | (hl-line-move hl-line-overlay)) |
| 172 | (hl-line-unhighlight))) |
| 173 | |
| 174 | (defun hl-line-unhighlight () |
| 175 | "Deactivate the Hl-Line overlay on the current line." |
| 176 | (when hl-line-overlay |
| 177 | (delete-overlay hl-line-overlay))) |
| 178 | |
| 179 | ;;;###autoload |
| 180 | (define-minor-mode global-hl-line-mode |
| 181 | "Toggle line highlighting in all buffers (Global Hl-Line mode). |
| 182 | With a prefix argument ARG, enable Global Hl-Line mode if ARG is |
| 183 | positive, and disable it otherwise. If called from Lisp, enable |
| 184 | the mode if ARG is omitted or nil. |
| 185 | |
| 186 | If `global-hl-line-sticky-flag' is non-nil, Global Hl-Line mode |
| 187 | highlights the line about the current buffer's point in all |
| 188 | windows. |
| 189 | |
| 190 | Global-Hl-Line mode uses the functions `global-hl-line-unhighlight' and |
| 191 | `global-hl-line-highlight' on `pre-command-hook' and `post-command-hook'." |
| 192 | :global t |
| 193 | :group 'hl-line |
| 194 | (if global-hl-line-mode |
| 195 | (progn |
| 196 | (add-hook 'pre-command-hook #'global-hl-line-unhighlight) |
| 197 | (add-hook 'post-command-hook #'global-hl-line-highlight)) |
| 198 | (global-hl-line-unhighlight) |
| 199 | (remove-hook 'pre-command-hook #'global-hl-line-unhighlight) |
| 200 | (remove-hook 'post-command-hook #'global-hl-line-highlight))) |
| 201 | |
| 202 | (defun global-hl-line-highlight () |
| 203 | "Highlight the current line in the current window." |
| 204 | (when global-hl-line-mode ; Might be changed outside the mode function. |
| 205 | (unless (window-minibuffer-p) |
| 206 | (unless global-hl-line-overlay |
| 207 | (setq global-hl-line-overlay (hl-line-make-overlay))) ; To be moved. |
| 208 | (overlay-put global-hl-line-overlay 'window |
| 209 | (unless global-hl-line-sticky-flag |
| 210 | (selected-window))) |
| 211 | (hl-line-move global-hl-line-overlay)))) |
| 212 | |
| 213 | (defun global-hl-line-unhighlight () |
| 214 | "Deactivate the Global-Hl-Line overlay on the current line." |
| 215 | (when global-hl-line-overlay |
| 216 | (delete-overlay global-hl-line-overlay))) |
| 217 | |
| 218 | (defun hl-line-move (overlay) |
| 219 | "Move the Hl-Line overlay. |
| 220 | If `hl-line-range-function' is non-nil, move the OVERLAY to the position |
| 221 | where the function returns. If `hl-line-range-function' is nil, fill |
| 222 | the line including the point by OVERLAY." |
| 223 | (let (tmp b e) |
| 224 | (if hl-line-range-function |
| 225 | (setq tmp (funcall hl-line-range-function) |
| 226 | b (car tmp) |
| 227 | e (cdr tmp)) |
| 228 | (setq tmp t |
| 229 | b (line-beginning-position) |
| 230 | e (line-beginning-position 2))) |
| 231 | (if tmp |
| 232 | (move-overlay overlay b e) |
| 233 | (move-overlay overlay 1 1)))) |
| 234 | |
| 235 | (defun hl-line-unload-function () |
| 236 | "Unload the Hl-Line library." |
| 237 | (global-hl-line-mode -1) |
| 238 | (save-current-buffer |
| 239 | (dolist (buffer (buffer-list)) |
| 240 | (set-buffer buffer) |
| 241 | (when hl-line-mode (hl-line-mode -1)))) |
| 242 | ;; continue standard unloading |
| 243 | nil) |
| 244 | |
| 245 | (provide 'hl-line) |
| 246 | |
| 247 | ;;; hl-line.el ends here |