emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: highlight current line when Emacs is idle


From: Drew Adams
Subject: RE: highlight current line when Emacs is idle
Date: Fri, 13 Jul 2007 07:16:58 -0700

I sent this last September. Here is a patch that lets you, as two
alternatives to the current hl-line feature:

 * Turn on hl-line highlighting only when Emacs is idle.
 * Turn on hl-line highlighting for only a few seconds, on demand.

The patch also changes the default value of face `hl-line' for light
backgrounds. Suggestions welcome for other backgrounds. In general, I don't
think `highlight' is a good default choice for this. Many people will make
`highlight' fairly bright, to, well, highlight, whereas the hl-line
highlighting should be somewhat subtle, IMO.

> From: Drew Adams Sent: Monday, September 04, 2006 11:04 AM
>
> This suggestion (for after the release) is a follow-on to my
> suggestion to change the cursor to a box when Emacs is idle in
> order to more easily spot it.
>
> Suggestion: Optionally turn on `global-hl-line-mode' and
> highlight the current line only when Emacs is idle. I use such an
> option in my code, and I find it useful, for the same reason that
> changing to a box cursor when idle is useful. I use a similar
> implementation for both (an input event ends the highlighting).
>
> I use a (configurable) idle-timer interval of several seconds -
> longer than the timer interval for the cursor-type change. I also
> use a less-obtrusive, yellow underline face for the highlighting
> (my background is light blue).
>
> My guess is that more people might find hl-line useful with this
> option added. The main reason some people don't like it is that
> it is very intrusive and can be distracting.
>
> In fact, I suspect that most people who use `global-hl-line-mode'
> (or `hl-line-mode') would prefer this idle-only highlighting:
> there is usually no need to continue highlighting the line after
> you have already noticed it.

Here is the patch.

---------------8<--------------------------------------

*** hl-line-CVS-2007-07-13.el   Fri Jul 13 06:55:06 2007
--- hl-line-CVS-patched-2007-07-13.el Fri Jul 13 07:02:18 2007
***************
*** 77,83 ****
    :group 'editing)

  (defface hl-line
!   '((t :inherit highlight))
    "Default face for highlighting the current line in Hl-Line mode."
    :version "22.1"
    :group 'hl-line)
--- 77,84 ----
    :group 'editing)

  (defface hl-line
!     '((((class color) (min-colors 88) (background light)) :background
"SlateGray3")
!       (t :inherit highlight))
    "Default face for highlighting the current line in Hl-Line mode."
    :version "22.1"
    :group 'hl-line)
***************
*** 104,109 ****
--- 105,114 ----
    :version "22.1"
    :group 'hl-line)

+ (defcustom line-show-period 1
+   "Number of seconds to highlight the current line."
+   :type 'integer :group 'cursor :group 'hl-line)
+
  (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
***************
*** 113,118 ****
--- 118,141 ----

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

+ (defvar hl-line-idle-interval 5
+   "Number of seconds to wait before turning on `global-hl-line-mode'.
+ Do NOT change this yourself to change the wait period; instead, use
+ `\\[hl-line-when-idle-interval]'.")
+
+ (defvar hl-line-idle-timer
+   (progn                                ; Cancel to prevent duplication.
+     (when (boundp 'hl-line-idle-timer) (cancel-timer hl-line-idle-timer))
+     (run-with-idle-timer hl-line-idle-interval t 'hl-line-highlight-now))
+   "Timer used to turn on `global-hl-line-mode' whenever Emacs is idle.")
+
+ ;; Turn it off, by default.  You must use `toggle-hl-line-when-idle' to
turn it on.
+ (cancel-timer hl-line-idle-timer)
+
+ (defvar hl-line-when-idle-p nil
+   "Non-nil means to use turn on `global-hl-line-mode' whenever Emacs is
idle.
+ Do NOT change this yourself; instead, use
`\\[toggle-hl-line-when-idle]'.")
+
  ;;;###autoload
  (define-minor-mode hl-line-mode
    "Buffer-local minor mode to highlight the line about point.
***************
*** 209,214 ****
--- 232,288 ----
        (move-overlay overlay b e)
        (move-overlay overlay 1 1))))

+ (defalias 'toggle-hl-line-when-idle 'hl-line-toggle-when-idle)
+ (defun hl-line-toggle-when-idle (&optional arg)
+ "Turn on or off using `global-hl-line-mode' when Emacs is idle.
+ When on, use `global-hl-line-mode' whenever Emacs is idle.
+ With prefix argument, turn on if ARG > 0; else turn off."
+   (interactive "P")
+   (setq hl-line-when-idle-p
+         (if arg (> (prefix-numeric-value arg) 0) (not
hl-line-when-idle-p)))
+   (cond (hl-line-when-idle-p
+          (timer-activate-when-idle hl-line-idle-timer)
+          (add-hook 'pre-command-hook 'hl-line-unhighlight-now)
+          (message "Turned ON using `global-hl-line-mode' when Emacs is
idle."))
+         (t
+          (cancel-timer hl-line-idle-timer)
+          (remove-hook 'pre-command-hook 'hl-line-unhighlight-now)
+          (message "Turned OFF using `global-hl-line-mode' when Emacs is
idle."))))
+
+ (defun hl-line-when-idle-interval (secs)
+   "Set wait until using `global-hl-line-mode' when Emacs is idle.
+ Whenever Emacs is idle for this many seconds, `global-hl-line-mode'
+ will be turned on.
+
+ To turn on or off using `global-hl-line-mode' when idle,
+ use `\\[toggle-hl-line-when-idle]."
+   (interactive "nSeconds to idle, before using `global-hl-line-mode': ")
+   (timer-set-idle-time hl-line-idle-timer
+                        (setq hl-line-idle-interval secs)
+                        t))
+
+ (defun hl-line-highlight-now ()
+   "Turn on `global-hl-line-mode' and highlight current line now."
+   (unless global-hl-line-mode
+     (global-hl-line-mode 1)
+     (global-hl-line-highlight)))
+
+ (defun hl-line-unhighlight-now ()
+   "Turn off `global-hl-line-mode' and unhighlight current line now."
+   (global-hl-line-mode -1)
+   (global-hl-line-unhighlight))
+
+ (defalias 'flash-line-highlight 'hl-line-flash)
+ (defun hl-line-flash (&optional arg)
+   "Highlight the current line for `line-show-period' seconds.
+ With a prefix argument, highlight for that many seconds."
+   (interactive)
+   (hl-line-highlight-now)
+   (let ((line-period line-show-period))
+     (when current-prefix-arg
+       (setq line-period (prefix-numeric-value current-prefix-arg)))
+     (run-at-time line-period nil #'hl-line-unhighlight-now)))
+
  (provide 'hl-line)

  ;;; arch-tag: ac806940-0876-4959-8c89-947563ee2833





reply via email to

[Prev in Thread] Current Thread [Next in Thread]