branch: master commit 6d79b91656c359e288a1059872211858222198a3 Author: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com> Commit: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com>
Idly colorizing after changes working. Cancelling seemingly not working. --- context-coloring.el | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 39 insertions(+), 10 deletions(-) diff --git a/context-coloring.el b/context-coloring.el index 16ef241..2a0acb7 100644 --- a/context-coloring.el +++ b/context-coloring.el @@ -115,7 +115,8 @@ calling FUNCTION with the parsed list of tokens." (start-process-shell-command "tokenizer" nil context-coloring-tokenizer-path)) (let ((output "") - (buffer context-coloring-buffer)) + (buffer context-coloring-buffer) + (start-time context-coloring-colorize-start-time)) ;;The process may produce output in multiple chunks. This filter accumulates ;;the chunks into a message. @@ -132,7 +133,8 @@ calling FUNCTION with the parsed list of tokens." (json-read-from-string output)))) (with-current-buffer buffer (context-coloring-apply-tokens tokens)) - (message "%s (%f)" "Colorization complete." (float-time))))))) + (message "Colorized (after %f seconds)." + (- (float-time) start-time))))))) ;; Give the process its input. (process-send-region context-coloring-tokenizer-process (point-min) (point-max)) @@ -141,13 +143,24 @@ calling FUNCTION with the parsed list of tokens." ;;; Colorization functions -(defun context-coloring-colorize-buffer () +(defun context-coloring-colorize () (interactive) + (setq context-coloring-colorize-start-time (float-time)) + (message "%s" "Colorizing.") (context-coloring-tokenize)) +(defun context-coloring-change-function (start end length) + (setq context-coloring-changed t)) + (defun context-coloring-maybe-colorize () - (when (eq context-coloring-buffer (window-buffer (selected-window))) - (context-coloring-colorize-buffer))) + "Colorize under certain conditions. This will run as an idle +timer, so firstly the buffer must not be some other +buffer. Additionally, the buffer must have changed, otherwise +colorizing would be redundant." + (when (and (eq context-coloring-buffer (window-buffer (selected-window))) + context-coloring-changed) + (setq context-coloring-changed nil) + (context-coloring-colorize))) ;;; Local variables @@ -161,6 +174,19 @@ calling FUNCTION with the parsed list of tokens." is a reference to that one process.") (make-variable-buffer-local 'context-coloring-tokenizer-process) +(defvar context-coloring-colorize-idle-timer nil + "Reference to currently-running idle timer.") +(make-variable-buffer-local 'context-coloring-colorize-idle-timer) + +(defvar context-coloring-changed nil + "Indication that the buffer has changed recently, which would +imply that it should be colorized again.") +(make-variable-buffer-local 'context-coloring-changed) + +(defvar context-coloring-colorize-start-time nil + "Used for dirty benchmarking of async colorization time.") +(make-variable-buffer-local 'context-coloring-colorize-start-time) + ;;; Minor mode @@ -170,18 +196,21 @@ is a reference to that one process.") nil " Context" nil (if (not context-coloring-mode) (progn - (when (boundp 'context-coloring-colorize-idle-timer) + (when (not (null 'context-coloring-colorize-idle-timer)) (cancel-timer context-coloring-colorize-idle-timer))) (setq context-coloring-buffer (current-buffer)) ;; Colorize once initially. - (context-coloring-colorize-buffer) + (context-coloring-colorize) + + ;; Only recolor on change. So watch for changes. + (set (make-local-variable 'after-change-functions) + '(context-coloring-change-function)) ;; Only recolor idly. - ;; (set (make-local-variable 'context-coloring-colorize-idle-timer) - ;; (run-with-idle-timer 1 t 'context-coloring-maybe-colorize)) - )) + (setq context-coloring-colorize-idle-timer + (run-with-idle-timer 1 t 'context-coloring-maybe-colorize)))) ;;;###autoload (defun context-coloring-mode-enable ()