]> code.delx.au - gnu-emacs-elpa/commitdiff
Fully recolor later after a fast recolor.
authorJackson Ray Hamilton <jackson@jacksonrayhamilton.com>
Sat, 13 Jun 2015 23:42:59 +0000 (16:42 -0700)
committerJackson Ray Hamilton <jackson@jacksonrayhamilton.com>
Sat, 13 Jun 2015 23:42:59 +0000 (16:42 -0700)
context-coloring.el
test/context-coloring-test.el

index 6b78a1a8be46abeb60f2a5877090fa1cd0f50eb9..d248edc6943aac9f54f9c74a4bbe666abfad0fc6 100644 (file)
@@ -125,8 +125,8 @@ themes have been disabled.")
 (defvar-local context-coloring-changed-p nil
   "Indication that the buffer has changed recently, which implies
 that it should be colored again by
-`context-coloring-colorize-idle-timer' if that timer is being
-used.")
+`context-coloring-maybe-colorize-idle-timer' if that timer is
+being used.")
 
 (defvar-local context-coloring-changed-start nil
   "Beginning of last text that changed.")
@@ -148,18 +148,20 @@ START, END and LENGTH are recorded for later use."
   (setq context-coloring-changed-length length)
   (setq context-coloring-changed-p t))
 
-(defun context-coloring-maybe-colorize (buffer)
-  "Colorize the current buffer if it is BUFFER and has changed."
-  (when (and (eq buffer (current-buffer))
-             context-coloring-changed-p)
-    (context-coloring-colorize)
+(defun context-coloring-maybe-colorize-with-buffer (buffer)
+  "Color BUFFER and if it has changed."
+  (when context-coloring-changed-p
+    (context-coloring-colorize-with-buffer buffer)
     (setq context-coloring-changed-p nil)
     (setq context-coloring-changed-start nil)
     (setq context-coloring-changed-end nil)
     (setq context-coloring-changed-length nil)))
 
+(defvar-local context-coloring-maybe-colorize-idle-timer nil
+  "The currently-running idle timer for conditional coloring.")
+
 (defvar-local context-coloring-colorize-idle-timer nil
-  "The currently-running idle timer.")
+  "The currently-running idle timer for unconditional coloring.")
 
 (defcustom context-coloring-default-delay 0.25
   "Default (sometimes overridden) delay between a buffer update
@@ -176,6 +178,21 @@ Supported modes: `js-mode', `js3-mode'"
  'context-coloring-default-delay
  "6.4.0")
 
+(defun context-coloring-cancel-timer (timer)
+  "Cancel TIMER."
+  (when timer
+    (cancel-timer timer)))
+
+(defun context-coloring-schedule-coloring (time)
+  "Schedule coloring to occur once after Emacs is idle for TIME."
+  (context-coloring-cancel-timer context-coloring-colorize-idle-timer)
+  (setq context-coloring-colorize-idle-timer
+        (run-with-idle-timer
+         time
+         nil
+         #'context-coloring-colorize-with-buffer
+         (current-buffer))))
+
 (defun context-coloring-setup-idle-change-detection ()
   "Setup idle change detection."
   (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
@@ -183,18 +200,19 @@ Supported modes: `js-mode', `js3-mode'"
      'after-change-functions #'context-coloring-change-function nil t)
     (add-hook
      'kill-buffer-hook #'context-coloring-teardown-idle-change-detection nil t)
-    (setq context-coloring-colorize-idle-timer
+    (setq context-coloring-maybe-colorize-idle-timer
           (run-with-idle-timer
            (or (plist-get dispatch :delay) context-coloring-default-delay)
            t
-           #'context-coloring-maybe-colorize
+           #'context-coloring-maybe-colorize-with-buffer
            (current-buffer)))))
 
 (defun context-coloring-teardown-idle-change-detection ()
   "Teardown idle change detection."
   (context-coloring-cancel-scopification)
-  (when context-coloring-colorize-idle-timer
-    (cancel-timer context-coloring-colorize-idle-timer))
+  (dolist (timer (list context-coloring-colorize-idle-timer
+                       context-coloring-maybe-colorize-idle-timer))
+    (context-coloring-cancel-timer timer))
   (remove-hook
    'kill-buffer-hook #'context-coloring-teardown-idle-change-detection t)
   (remove-hook
@@ -1015,7 +1033,12 @@ scopes and variables."
                                  (forward-line 1))
                                (end-of-defun)
                                (point))))
-              (context-coloring-elisp-colorize-region-initially start end)))
+              (context-coloring-elisp-colorize-region-initially start end)
+              ;; Fast coloring is nice, but if the code is not well-formed
+              ;; (e.g. an unclosed string literal is parsed at any time) then
+              ;; there could be leftover incorrectly-colored code offscreen.  So
+              ;; do a clean sweep as soon as appropriate.
+              (context-coloring-schedule-coloring context-coloring-default-delay)))
            (t
             (context-coloring-elisp-colorize-region-initially (point-min) (point-max))))
         ;; Scan errors can happen virtually anywhere if parenthesis are
@@ -1279,6 +1302,13 @@ Invoke CALLBACK when complete; see `context-coloring-dispatch'."
      (when callback (funcall callback))
      (run-hooks 'context-coloring-colorize-hook))))
 
+(defun context-coloring-colorize-with-buffer (buffer)
+  "Color BUFFER."
+  ;; Don't select deleted buffers.
+  (when (get-buffer buffer)
+    (with-current-buffer buffer
+      (context-coloring-colorize))))
+
 
 ;;; Versioning
 
index cce22a307c9b4c8b753156c9525a5e55af1d1854..702058924d26d8ec75ec0bf33b5e8642e2473b7c 100644 (file)
@@ -375,7 +375,7 @@ ARGS)."
           (funcall done)))
        (insert " ")
        (set-window-buffer (selected-window) (current-buffer))
-       (context-coloring-maybe-colorize (current-buffer))))
+       (context-coloring-maybe-colorize-with-buffer (current-buffer))))
     (context-coloring-mode))
   :after (lambda ()
            (setq context-coloring-colorize-hook nil)))