]> code.delx.au - gnu-emacs-elpa/blobdiff - context-coloring.el
Add `:setup' and `:teardown' properties to dispatches.
[gnu-emacs-elpa] / context-coloring.el
index 47cb00b298c4b7667d236fc2fe485d42a4d819f3..574f0bcd7f985d9082999e3741cb64e922629d8e 100644 (file)
@@ -354,7 +354,13 @@ buffer a returns a flat vector of start, end and level data.
 
 `:command' - Shell command to execute with the current buffer
 sent via stdin, and with a flat JSON array of start, end and
-level data returned via stdout."
+level data returned via stdout.
+
+`:setup' - Arbitrary code to set up this dispatch when
+`context-coloring-mode' is enabled.
+
+`:teardown' - Arbitrary code to tear down this dispatch when
+`context-coloring-mode' is disabled."
   (let ((modes (plist-get properties :modes))
         (colorizer (plist-get properties :colorizer))
         (scopifier (plist-get properties :scopifier))
@@ -379,7 +385,13 @@ level data returned via stdout."
 (context-coloring-define-dispatch
  'javascript-js2
  :modes '(js2-mode)
- :colorizer 'context-coloring-js2-colorize)
+ :colorizer 'context-coloring-js2-colorize
+ :setup
+ (lambda ()
+   (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
+ :teardown
+ (lambda ()
+   (remove-hook 'js2-post-parse-callbacks 'context-coloring-colorize t)))
 
 (defun context-coloring-dispatch (&optional callback)
   "Determine the optimal track for scopification / coloring of
@@ -412,22 +424,14 @@ elisp tracks, and asynchronously for shell command tracks."
 
 ;;; Colorization
 
-(defcustom context-coloring-benchmark-colorization nil
-  "If non-nil, track how long colorization takes and print
-messages with the colorization duration."
-  :group 'context-coloring)
-
 (defun context-coloring-colorize (&optional callback)
   "Color the current buffer by function context.
 
 Invoke CALLBACK when complete; see `context-coloring-dispatch'."
   (interactive)
-  (let ((start-time (float-time)))
-    (context-coloring-dispatch
-     (lambda ()
-       (when context-coloring-benchmark-colorization
-         (message "Colorization took %.3f seconds" (- (float-time) start-time)))
-       (when callback (funcall callback))))))
+  (context-coloring-dispatch
+   (lambda ()
+     (when callback (funcall callback)))))
 
 (defvar-local context-coloring-changed nil
   "Indication that the buffer has changed recently, which implies
@@ -812,10 +816,15 @@ Supported modes: `js-mode', `js3-mode'"
         (context-coloring-kill-scopifier)
         (when context-coloring-colorize-idle-timer
           (cancel-timer context-coloring-colorize-idle-timer))
-        (remove-hook
-         'js2-post-parse-callbacks 'context-coloring-colorize t)
-        (remove-hook
-         'after-change-functions 'context-coloring-change-function t)
+        (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+          (when dispatch
+            (let ((command (plist-get dispatch :command))
+                  (teardown (plist-get dispatch :teardown)))
+              (when command
+                (remove-hook
+                 'after-change-functions 'context-coloring-change-function t))
+              (when teardown
+                (funcall teardown)))))
         (font-lock-mode)
         (jit-lock-mode t))
 
@@ -826,21 +835,24 @@ Supported modes: `js-mode', `js3-mode'"
     (font-lock-mode 0)
     (jit-lock-mode nil)
 
-    ;; Colorize once initially.
-    (context-coloring-colorize)
+    (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+      (when dispatch
+        (let ((command (plist-get dispatch :command))
+              (setup (plist-get dispatch :setup)))
+          (when command
+            ;; Shell commands recolor on change, idly.
+            (add-hook
+             'after-change-functions 'context-coloring-change-function nil t)
+            (setq context-coloring-colorize-idle-timer
+                  (run-with-idle-timer
+                   context-coloring-delay
+                   t
+                   'context-coloring-maybe-colorize)))
+          (when setup
+            (funcall setup)))))
 
-    (cond
-     ((equal major-mode 'js2-mode)
-      ;; Only recolor on reparse.
-      (add-hook 'js2-post-parse-callbacks 'context-coloring-colorize nil t))
-     (t
-      ;; Only recolor on change, idly.
-      (add-hook 'after-change-functions 'context-coloring-change-function nil t)
-      (setq context-coloring-colorize-idle-timer
-            (run-with-idle-timer
-             context-coloring-delay
-             t
-             'context-coloring-maybe-colorize))))))
+    ;; Colorize once initially.
+    (context-coloring-colorize)))
 
 (provide 'context-coloring)