hl-line.el 9.4 KB

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