emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[nongnu] elpa/idle-highlight-mode 8b6e0dffd1 03/59: Implement idle highl


From: ELPA Syncer
Subject: [nongnu] elpa/idle-highlight-mode 8b6e0dffd1 03/59: Implement idle highlighting as a minor mode.
Date: Thu, 7 Jul 2022 12:00:28 -0400 (EDT)

branch: elpa/idle-highlight-mode
commit 8b6e0dffd16df5ee1584ac79686f37911d87d192
Author: Cornelius Mika <cornelius.mika@gmail.com>
Commit: Campbell Barton <ideasman42@gmail.com>

    Implement idle highlighting as a minor mode.
    
    This fixes the bug that a new highlighting timer is created for each
    buffer in which idle-highlight becomes active.
    
    After a long Emacs session one might end up with hundreds of timers
    all sequentially executing the highlighting code.
---
 idle-highlight.el | 53 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 28 insertions(+), 25 deletions(-)

diff --git a/idle-highlight.el b/idle-highlight.el
index 6f67fe7bbb..1272cbf46b 100755
--- a/idle-highlight.el
+++ b/idle-highlight.el
@@ -62,38 +62,41 @@
   "Face used to highlight other occurrences of the word at point."
   :group 'idle-highlight)
 
-(defvar idle-highlight-last-regexp nil "Last regexp to be idle-highlighted.")
+(defvar idle-highlight-regexp nil "Buffer-local regexp to be 
idle-highlighted.")
 
-(defvar idle-highlight-timer nil "Timer to activate re-highlighting.")
+(defvar idle-highlight-global-timer nil "Timer to trigger highlighting.")
 
 (defun idle-highlight-word-at-point ()
   "Highlight the word under the point."
-  (let*
-    (
-      (target-symbol (symbol-at-point))
-      (target (symbol-name target-symbol)))
-    (if idle-highlight-last-regexp
-      (unhighlight-regexp idle-highlight-last-regexp))
-    (when
-      (and
-        idle-highlight-timer target target-symbol
-        ;; TODO: no need to highlight keywords like if
-        (not (in-string-p)) (not (equal target "end")))
-      (setq idle-highlight-last-regexp (concat "\\<" (regexp-quote target) 
"\\>"))
-      (highlight-regexp idle-highlight-last-regexp 'idle-highlight))))
+  (if idle-highlight-mode
+    (let*
+      (
+        (target-symbol (symbol-at-point))
+        (target (symbol-name target-symbol)))
+      (when
+        (and
+          target-symbol (not (in-string-p))
+          ;; TODO: no need to highlight keywords like if
+          (not (equal target "end")))
+        (idle-highlight-unhighlight)
+        (setq idle-highlight-regexp (concat "\\<" (regexp-quote target) "\\>"))
+        (highlight-regexp idle-highlight-regexp 'idle-highlight)))))
+
+(defsubst idle-highlight-unhighlight ()
+  (if idle-highlight-regexp
+    (unhighlight-regexp idle-highlight-regexp)))
 
 ;;;###autoload
-(defun idle-highlight (&optional arg)
-  "Toggle idle-highlighting."
-  (interactive "P")
-  (if (and (boundp 'idle-highlight-timer) idle-highlight-timer)
+(define-minor-mode idle-highlight-mode
+  "Idle-Highlight Minor Mode"
+  :group 'idle-highlight
+  (if idle-highlight-mode
     (progn
-      (cancel-timer idle-highlight-timer)
-      (setq idle-highlight-timer nil))
-    (set (make-local-variable 'idle-highlight-last-regexp) nil)
-    (set
-      (make-local-variable 'idle-highlight-timer)
-      (run-with-idle-timer 0.5 :repeat 'idle-highlight-word-at-point))))
+      (unless idle-highlight-global-timer
+        (setq idle-highlight-global-timer
+          (run-with-idle-timer 0.5 :repeat 'idle-highlight-word-at-point)))
+      (set (make-local-variable 'idle-highlight-regexp) nil))
+    (idle-highlight-unhighlight)))
 
 (provide 'idle-highlight)
 ;;; idle-highlight.el ends here



reply via email to

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