1 ;;; hl-line+.el --- Extensions to hl-line.el.
3 ;; Filename: hl-line+.el
4 ;; Description: Extensions to hl-line.el.
6 ;; Maintainer: Drew Adams
7 ;; Copyright (C) 2006-2011, Drew Adams, all rights reserved.
8 ;; Created: Sat Aug 26 18:17:18 2006
10 ;; Last-Updated: Thu Feb 24 15:22:18 2011 (-0800)
13 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/hl-line+.el
14 ;; Keywords: highlight, cursor, accessibility
15 ;; Compatibility: GNU Emacs: 22.x, 23.x
17 ;; Features that might be required by this library:
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;; This library extends standard library `hl-line.el' in these ways:
27 ;; 1. As an alternative to turning on `hl-line' highlighting at all
28 ;; times, you can turn it on only when Emacs is idle. To do that,
29 ;; use command `toggle-hl-line-when-idle' and customize
30 ;; `global-hl-line-mode' to nil.
32 ;; 2. As another alternative, you can turn it on for only a few
33 ;; seconds. To do that, use command `flash-line-highlight' and
34 ;; customize `global-hl-line-mode' to nil.
36 ;; 3. It provides a face, `hl-line', that you can customize, instead
37 ;; of using option `hl-line-face'.
39 ;; I suggested #3 to the Emacs developers, and it has been added
40 ;; to Emacs 22, but with a different default value. If you use
41 ;; library `crosshairs.el', you might want to customize this to a
42 ;; value similar to what is used there, so that the horizontal and
43 ;; vertical highlights will be the same.
45 ;; To use this library, put this in your Emacs init file (~/.emacs):
47 ;; (require 'hl-line+) ; Load this file (it will load `hl-line.el')
49 ;; To turn on `global-hl-line-mode' only when Emacs is idle, by
50 ;; default, add this line also to your init file:
52 ;; (toggle-hl-line-when-idle 1) ; Highlight only when idle
54 ;; You can use command `toggle-hl-line-when-idle' to turn idle
55 ;; highlighting on and off at any time. You can use command
56 ;; `hl-line-when-idle-interval' to change the number of idle seconds
57 ;; to wait before highlighting.
60 ;; See also these libraries:
62 ;; * `col-highlight.el', which highlights the current column.
64 ;; * `crosshairs.el', which highlights the current line and the
65 ;; current column, at the same time. It requires libraries
66 ;; `col-highlight.el' and `hl-line+.el'.
68 ;; * `hl-spotlight.el', which extends `hl-line.el' by spotlighting
69 ;; the lines around the cursor.
71 ;; * `cursor-chg.el' or library `oneonone.el', to change the cursor
72 ;; type when Emacs is idle.
75 ;; Faces defined here:
79 ;; User options defined here:
81 ;; `hl-line-flash-show-period',
82 ;; `hl-line-inhibit-highlighting-for-modes'.
84 ;; Commands defined here:
86 ;; `flash-line-highlight', `hl-line-flash',
87 ;; `hl-line-toggle-when-idle', `hl-line-when-idle-interval',
88 ;; `toggle-hl-line-when-idle'.
90 ;; Non-interactive functions defined here:
92 ;; `hl-line-highlight-now', `hl-line-unhighlight-now'.
94 ;; Internal variables defined here:
96 ;; `hl-line-idle-interval', `hl-line-idle-timer',
97 ;; `hl-line-when-idle-p'.
99 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 ;; Added autoload cookies for defcustom, defface and commands.
107 ;; Set ARG in interactive spec and use it. Thx to Philip Weaver.
109 ;; Added: hl-line-inhibit-highlighting-for-modes.
110 ;; hl-line-highlight-now: Respect hl-line-inhibit-for-modes. Thx to Sylecn.
112 ;; Removed spotlight stuff - moved to new library hl-spotlight.el.
114 ;; Added: hl-spotlight(-height|-old-state|widen|limits|mode),
115 ;; global-hl-spotlight-mode.
117 ;; Fix from Yann Yodique: Moved adding/removing hl-line-unhighlight-now as
118 ;; pre-command-hook from hl-line-toggle-when-idle to hl-line-(un)highlight-now.
120 ;; Renamed: line-show-period to hl-line-flash-show-period.
122 ;; Commentary typo: toggle-cursor-type-when-idle -> toggle-hl-line-when-idle.
124 ;; Update commentary to indicate that the face is now provided by default.
126 ;; Added: flash-line-highlight, hl-line-flash.
127 ;; Renamed: hl-line-when-idle(-off) to hl-line-(un)highlight-now.
129 ;; Added: hl-line-when-idle-p, hl-line-idle-interval, hl-line-idle-timer,
130 ;; hl-line-toggle-when-idle, hl-line-when-idle-interval,
131 ;; hl-line-when-idle(-off).
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 ;; This program is free software; you can redistribute it and/or
136 ;; modify it under the terms of the GNU General Public License as
137 ;; published by the Free Software Foundation; either version 3, or
138 ;; (at your option) any later version.
140 ;; This program is distributed in the hope that it will be useful,
141 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
142 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
143 ;; General Public License for more details.
145 ;; You should have received a copy of the GNU General Public License
146 ;; along with this program; see the file COPYING. If not, write to
147 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
148 ;; Floor, Boston, MA 02110-1301, USA.
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156 (defvar hl-line-face) ; Quiet the byte-compiler.
157 (defvar global-hl-line-mode) ; Quiet the byte-compiler.
159 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
161 ;; This will be ignored, since this is now defined by default in Emacs 22.
162 ;; I include it here as a different face definition that you might want to try.
164 (defface hl-line '((t (:background "SlateGray3"))) ; Try also (:underline "Yellow")
165 "*Face to use for `hl-line-face'." :group 'hl-line)
166 (setq hl-line-face 'hl-line)
169 (defcustom hl-line-flash-show-period 1
170 "*Number of seconds for `hl-line-flash' to highlight the line."
171 :type 'integer :group 'cursor :group 'hl-line)
173 ;; Possible value: `(Info-mode help-mode view-mode Man-mode)'
175 (defcustom hl-line-inhibit-highlighting-for-modes ()
176 "*Modes where highlighting is inhibited for `hl-line-highlight-now'.
177 A list of `major-mode' values (symbols)."
178 :type 'list :group 'hl-line)
180 (defvar hl-line-idle-interval 5
181 "Number of seconds to wait before turning on `global-hl-line-mode'.
182 Do NOT change this yourself to change the wait period; instead, use
183 `\\[hl-line-when-idle-interval]'.")
185 (defvar hl-line-idle-timer
186 (progn ; Cancel to prevent duplication.
187 (when (boundp 'hl-line-idle-timer) (cancel-timer hl-line-idle-timer))
188 (run-with-idle-timer hl-line-idle-interval t 'hl-line-highlight-now))
189 "Timer used to turn on `global-hl-line-mode' whenever Emacs is idle.")
191 ;; Turn it off, by default. You must use `toggle-hl-line-when-idle' to turn it on.
192 (cancel-timer hl-line-idle-timer)
194 (defvar hl-line-when-idle-p nil
195 "Non-nil means to turn on `global-hl-line-mode' whenever Emacs is idle.
196 Do NOT change this yourself; instead, use `\\[toggle-hl-line-when-idle]'.")
199 (defalias 'toggle-hl-line-when-idle 'hl-line-toggle-when-idle)
201 (defun hl-line-toggle-when-idle (&optional arg)
202 "Turn on or off using `global-hl-line-mode' when Emacs is idle.
203 When on, use `global-hl-line-mode' whenever Emacs is idle.
204 With prefix argument, turn on if ARG > 0; else turn off."
206 (setq hl-line-when-idle-p
207 (if arg (> (prefix-numeric-value arg) 0) (not hl-line-when-idle-p)))
208 (cond (hl-line-when-idle-p
209 (timer-activate-when-idle hl-line-idle-timer)
210 (message "Turned ON using `global-hl-line-mode' when Emacs is idle."))
212 (cancel-timer hl-line-idle-timer)
213 (message "Turned OFF using `global-hl-line-mode' when Emacs is idle."))))
216 (defun hl-line-when-idle-interval (secs)
217 "Set wait until using `global-hl-line-mode' when Emacs is idle.
218 Whenever Emacs is idle for this many seconds, `global-hl-line-mode'
221 To turn on or off using `global-hl-line-mode' when idle,
222 use `\\[toggle-hl-line-when-idle]."
223 (interactive "nSeconds to idle, before using `global-hl-line-mode': ")
224 (timer-set-idle-time hl-line-idle-timer (setq hl-line-idle-interval secs) t))
226 (defun hl-line-highlight-now ()
227 "Turn on `global-hl-line-mode' and highlight current line now."
228 (unless (or global-hl-line-mode
229 (member major-mode hl-line-inhibit-highlighting-for-modes))
230 (global-hl-line-mode 1)
231 (global-hl-line-highlight)
232 (add-hook 'pre-command-hook 'hl-line-unhighlight-now)))
234 (defun hl-line-unhighlight-now ()
235 "Turn off `global-hl-line-mode' and unhighlight current line now."
236 (global-hl-line-mode -1)
237 (global-hl-line-unhighlight)
238 (remove-hook 'pre-command-hook 'hl-line-unhighlight-now))
241 (defalias 'flash-line-highlight 'hl-line-flash)
243 (defun hl-line-flash (&optional arg)
244 "Highlight the current line for `hl-line-flash-show-period' seconds.
245 With a prefix argument, highlight for that many seconds."
247 (hl-line-highlight-now)
248 (let ((line-period hl-line-flash-show-period))
249 (when arg (setq line-period (prefix-numeric-value arg)))
250 (run-at-time line-period nil #'hl-line-unhighlight-now)))
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
257 ;;; hl-line+.el ends here