]> code.delx.au - gnu-emacs-elpa/blobdiff - context-coloring.el
Add define-deftest macro.
[gnu-emacs-elpa] / context-coloring.el
index f21b32371f4e31c51b18a56026f4a711938aed33..cb74ee74e8387cf7db8366b656b21d9159439267 100644 (file)
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2014-2015  Free Software Foundation, Inc.
 
 ;; Author: Jackson Ray Hamilton <jackson@jacksonrayhamilton.com>
-;; Version: 6.2.1
+;; Version: 6.3.0
 ;; Keywords: convenience faces tools
 ;; Package-Requires: ((emacs "24") (js2-mode "20150126"))
 ;; URL: https://github.com/jacksonrayhamilton/context-coloring
 
 ;; By default, comments and strings are still highlighted syntactically.
 
-;; To use with js2-mode, add the following to your init file:
-
-;; (require 'context-coloring)
-;; (add-hook 'js2-mode-hook 'context-coloring-mode)
-
-;; To use with js-mode or js3-mode, install Node.js 0.10+ and the scopifier
-;; executable:
-
-;; $ npm install -g scopifier
-
 ;;; Code:
 
 (require 'js2-mode)
 
 
-;;; Local variables
-
-(defvar-local context-coloring-buffer nil
-  "Reference to this buffer (for timers).")
-
-
 ;;; Utilities
 
 (defun context-coloring-join (strings delimiter)
@@ -172,15 +156,15 @@ the END point (exclusive) with the face corresponding to LEVEL."
   "Tell `font-lock' to color a string but not a comment."
   (if (nth 3 state) font-lock-string-face nil))
 
-;; TODO: Add specialized emacs-lisp version based on
-;; `lisp-font-lock-syntactic-face-function'.
 (defsubst context-coloring-maybe-colorize-comments-and-strings (&optional min max)
   "Color the current buffer's comments and strings if
 `context-coloring-comments-and-strings' is non-nil."
   (when (or context-coloring-comments-and-strings
             context-coloring-syntactic-comments
             context-coloring-syntactic-strings)
-    (let ((font-lock-syntactic-face-function
+    (let ((min (or min (point-min)))
+          (max (or max (point-max)))
+          (font-lock-syntactic-face-function
            (cond
             ((and context-coloring-syntactic-comments
                   (not context-coloring-syntactic-strings))
@@ -191,8 +175,10 @@ the END point (exclusive) with the face corresponding to LEVEL."
             (t
              font-lock-syntactic-face-function))))
       (save-excursion
-        (font-lock-fontify-syntactically-region (or min (point-min))
-                                                (or max (point-max)))))))
+        (font-lock-fontify-syntactically-region min max)
+        ;; TODO: Make configurable at the dispatch level.
+        (when (eq major-mode 'emacs-lisp-mode)
+          (font-lock-fontify-keywords-region min max))))))
 
 
 ;;; js2-mode colorization
@@ -294,22 +280,22 @@ generated by `js2-mode'."
 
 ;;; Emacs Lisp colorization
 
-(defun context-coloring-make-scope (depth level)
+(defsubst context-coloring-make-scope (depth level)
   (list
    :depth depth
    :level level
    :variables (make-hash-table)))
 
-(defun context-coloring-scope-get-level (scope)
+(defsubst context-coloring-scope-get-level (scope)
   (plist-get scope :level))
 
-(defun context-coloring-scope-add-variable (scope variable)
+(defsubst context-coloring-scope-add-variable (scope variable)
   (puthash variable t (plist-get scope :variables)))
 
-(defun context-coloring-scope-get-variable (scope variable)
+(defsubst context-coloring-scope-get-variable (scope variable)
   (gethash variable (plist-get scope :variables)))
 
-(defun context-coloring-get-variable-level (scope-stack variable)
+(defsubst context-coloring-get-variable-level (scope-stack variable)
   (let* (scope
          level)
     (while (and scope-stack (not level))
@@ -322,33 +308,33 @@ generated by `js2-mode'."
     ;; Assume a global variable.
     (or level 0)))
 
-(defun context-coloring-make-backtick (end enabled)
+(defsubst context-coloring-make-backtick (end enabled)
   (list
    :end end
    :enabled enabled))
 
-(defun context-coloring-backtick-get-end (backtick)
+(defsubst context-coloring-backtick-get-end (backtick)
   (plist-get backtick :end))
 
-(defun context-coloring-backtick-get-enabled (backtick)
+(defsubst context-coloring-backtick-get-enabled (backtick)
   (plist-get backtick :enabled))
 
-(defun context-coloring-backtick-enabled-p (backtick-stack)
+(defsubst context-coloring-backtick-enabled-p (backtick-stack)
   (context-coloring-backtick-get-enabled (car backtick-stack)))
 
-(defun context-coloring-make-let-varlist (depth type)
+(defsubst context-coloring-make-let-varlist (depth type)
   (list
    :depth depth
    :type type
    :vars '()))
 
-(defun context-coloring-let-varlist-get-type (let-varlist)
+(defsubst context-coloring-let-varlist-get-type (let-varlist)
   (plist-get let-varlist :type))
 
-(defun context-coloring-let-varlist-add-var (let-varlist var)
+(defsubst context-coloring-let-varlist-add-var (let-varlist var)
   (plist-put let-varlist :vars (cons var (plist-get let-varlist :vars))))
 
-(defun context-coloring-let-varlist-pop-vars (let-varlist)
+(defsubst context-coloring-let-varlist-pop-vars (let-varlist)
   (let ((type (context-coloring-let-varlist-get-type let-varlist))
         (vars (plist-get let-varlist :vars)))
     (cond
@@ -363,44 +349,66 @@ generated by `js2-mode'."
           (list (car vars))
         (plist-put let-varlist :vars (cdr vars)))))))
 
-(defun context-coloring-forward-sws ()
+(defsubst context-coloring-forward-sws ()
   "Move forward through whitespace and comments."
   (while (forward-comment 1)))
 
-(defun context-coloring-forward-sexp-position ()
+(defsubst context-coloring-forward-sexp-position ()
+  "Like vanilla `forward-sexp', but just return the position."
   (scan-sexps (point) 1))
 
-(defun context-coloring-emacs-lisp-identifier-syntax-p (syntax-code)
+(defsubst context-coloring-emacs-lisp-identifier-syntax-p (syntax-code)
   (or (= 2 syntax-code)
       (= 3 syntax-code)))
 
-(defun context-coloring-open-parenthesis-p (syntax-code)
+(defsubst context-coloring-open-parenthesis-p (syntax-code)
   (= 4 syntax-code))
 
-(defun context-coloring-close-parenthesis-p (syntax-code)
+(defsubst context-coloring-close-parenthesis-p (syntax-code)
   (= 5 syntax-code))
 
-(defun context-coloring-expression-prefix-p (syntax-code)
+(defsubst context-coloring-expression-prefix-p (syntax-code)
   (= 6 syntax-code))
 
-(defun context-coloring-at-open-parenthesis-p ()
+(defsubst context-coloring-at-open-parenthesis-p ()
   (= 4 (logand #xFFFF (car (syntax-after (point))))))
 
-(defun context-coloring-ppss-depth (ppss)
+(defsubst context-coloring-ppss-depth (ppss)
   ;; Same as (nth 0 ppss).
   (car ppss))
 
-(defun context-coloring-stack-depth-equal (stack depth)
+(defsubst context-coloring-at-stack-depth-p (stack depth)
   (= (plist-get (car stack) :depth) depth))
 
-(defconst context-coloring-defun-regexp
-  "\\`defun\\'\\|\\`defmacro\\'\\|\\`defsubst\\'")
+(defsubst context-coloring-exact-regexp (word)
+  "Create a regexp that matches exactly WORD."
+  (concat "\\`" (regexp-quote word) "\\'"))
+
+(defsubst context-coloring-exact-or-regexp (words)
+  "Create a regexp that matches any exact word in WORDS."
+  (context-coloring-join
+   (mapcar 'context-coloring-exact-regexp words) "\\|"))
+
+(defconst context-coloring-emacs-lisp-defun-regexp
+  (context-coloring-exact-or-regexp
+   '("defun" "defun*" "defsubst" "defmacro"
+     "cl-defun" "cl-defsubst" "cl-defmacro")))
+
+(defconst context-coloring-emacs-lisp-lambda-regexp
+  (context-coloring-exact-regexp "lambda"))
+
+(defconst context-coloring-emacs-lisp-let-regexp
+  (context-coloring-exact-regexp "let"))
+
+(defconst context-coloring-emacs-lisp-let*-regexp
+  (context-coloring-exact-regexp "let*"))
 
 (defconst context-coloring-arglist-arg-regexp
   "\\`[^&:]")
 
 (defconst context-coloring-ignored-word-regexp
-  "\\`[-+]?[0-9]\\|\\`t\\'\\|\\`nil\\'\\|\\`\\.\\'\\|\\`\\?\\'")
+  (concat "\\`[-+]?[0-9]\\|" (context-coloring-exact-or-regexp
+                              '("t" "nil" "." "?"))))
 
 (defconst context-coloring-COMMA-CHAR 44)
 (defconst context-coloring-BACKTICK-CHAR 96)
@@ -408,17 +416,14 @@ generated by `js2-mode'."
 (defvar context-coloring-parse-interruptable-p t
   "Set this to nil to force parse to continue until finished.")
 
-(defvar context-coloring-emacs-lisp-iterations-per-pause 1000
+(defconst context-coloring-emacs-lisp-iterations-per-pause 1000
   "Pause after this many iterations to check for user input.
 If user input is pending, stop the parse.  This makes for a
 smoother user experience for large files.
 
 As of this writing, emacs lisp colorization seems to run at about
 60,000 iterations per second.  A default value of 1000 should
-provide visually \"instant\" updates at ~60 frames per second.")
-
-(defvar context-coloring-verbose-parse nil
-  "Log useful information pertaining to a parse.")
+provide visually \"instant\" updates at 60 frames per second.")
 
 (defun context-coloring-emacs-lisp-colorize ()
   "Color the current buffer by parsing emacs lisp sexps."
@@ -426,12 +431,12 @@ provide visually \"instant\" updates at ~60 frames per second.")
     (save-excursion
       ;; TODO: Can probably make this lazy to the nearest defun.
       (goto-char (point-min))
-      (let* ((start-time (float-time))
-             (inhibit-point-motion-hooks t)
+      (let* ((inhibit-point-motion-hooks t)
+             (end (point-max))
              (iteration-count 0)
              (last-fontified-position (point))
+             beginning-of-current-defun
              end-of-current-defun
-             (end (point-max))
              (last-ppss-pos (point))
              (ppss (syntax-ppss))
              ppss-depth
@@ -477,14 +482,23 @@ provide visually \"instant\" updates at ~60 frames per second.")
             ;; Fontify until the end of the current defun because doing it in
             ;; chunks based soley on point could result in partial
             ;; re-fontifications over the contents of scopes.
-            (setq end-of-current-defun (save-excursion
-                                         (end-of-defun)
-                                         (point)))
+            (save-excursion
+              (end-of-defun)
+              (setq end-of-current-defun (point))
+              (beginning-of-defun)
+              (setq beginning-of-current-defun (point)))
+
             ;; Fontify in chunks.
             (context-coloring-maybe-colorize-comments-and-strings
              last-fontified-position
-             end-of-current-defun)
-            (setq last-fontified-position end-of-current-defun)
+             (cond
+              ;; We weren't actually in a defun, so don't color the next one, as
+              ;; that could result in `font-lock' properties being added to it.
+              ((> beginning-of-current-defun (point))
+               (point))
+              (t
+               end-of-current-defun)))
+            (setq last-fontified-position (point))
             (when (and context-coloring-parse-interruptable-p
                        (input-pending-p))
               (throw 'interrupted t)))
@@ -551,7 +565,7 @@ provide visually \"instant\" updates at ~60 frames per second.")
                ;; Parse a var in a `let' varlist.
                ((and
                  let-varlist-stack
-                 (context-coloring-stack-depth-equal
+                 (context-coloring-at-stack-depth-p
                   let-varlist-stack
                   ;; 1- because we're inside the varlist.
                   (1- (context-coloring-ppss-depth ppss))))
@@ -560,14 +574,14 @@ provide visually \"instant\" updates at ~60 frames per second.")
                  (intern child-0-string))
                 (setq let-var-stack (cons (context-coloring-ppss-depth ppss)
                                           let-var-stack)))
-               ((string-match-p context-coloring-defun-regexp child-0-string)
+               ((string-match-p context-coloring-emacs-lisp-defun-regexp child-0-string)
                 (setq in-defun-p t))
-               ((string-match-p "\\`lambda\\'" child-0-string)
+               ((string-match-p context-coloring-emacs-lisp-lambda-regexp child-0-string)
                 (setq in-lambda-p t))
-               ((string-match-p "\\`let\\'" child-0-string)
+               ((string-match-p context-coloring-emacs-lisp-let-regexp child-0-string)
                 (setq in-let-p t)
                 (setq let-varlist-type 'let))
-               ((string-match-p "\\`let\\*\\'" child-0-string)
+               ((string-match-p context-coloring-emacs-lisp-let*-regexp child-0-string)
                 (setq in-let*-p t)
                 (setq let-varlist-type 'let*)))))
             (when (or in-defun-p
@@ -579,7 +593,8 @@ provide visually \"instant\" updates at ~60 frames per second.")
                                        (1+ (context-coloring-scope-get-level
                                             (car scope-stack))))
                                       scope-stack)))
-            ;; TODO: Probably redundant and wasteful.
+            ;; TODO: Maybe wasteful but doing this conditionally doesn't make
+            ;; much of a difference.
             (context-coloring-colorize-region token-pos
                                               (scan-sexps token-pos 1)
                                               (context-coloring-scope-get-level
@@ -668,7 +683,7 @@ provide visually \"instant\" updates at ~60 frames per second.")
                ;; Parse a `let' varlist's uninitialized var.
                ((and
                  let-varlist-stack
-                 (context-coloring-stack-depth-equal
+                 (context-coloring-at-stack-depth-p
                   let-varlist-stack
                   ;; 1- because we're inside the varlist.
                   (1- (context-coloring-ppss-depth ppss))))
@@ -698,7 +713,7 @@ provide visually \"instant\" updates at ~60 frames per second.")
             (setq last-ppss-pos (point))
             (setq ppss-depth (context-coloring-ppss-depth ppss))
             ;; TODO: Order might matter here but I'm not certain.
-            (when (context-coloring-stack-depth-equal scope-stack ppss-depth)
+            (when (context-coloring-at-stack-depth-p scope-stack ppss-depth)
               (setq scope-stack (cdr scope-stack)))
             (when (and
                    let-var-stack
@@ -710,7 +725,7 @@ provide visually \"instant\" updates at ~60 frames per second.")
                                    (car let-varlist-stack)))))
             (when (and
                    let-varlist-stack
-                   (context-coloring-stack-depth-equal let-varlist-stack ppss-depth))
+                   (context-coloring-at-stack-depth-p let-varlist-stack ppss-depth))
               (setq popped-vars (context-coloring-let-varlist-pop-vars
                                  (car let-varlist-stack)))
               (setq let-varlist-stack (cdr let-varlist-stack)))
@@ -722,10 +737,7 @@ provide visually \"instant\" updates at ~60 frames per second.")
         ;; Fontify the last stretch.
         (context-coloring-maybe-colorize-comments-and-strings
          last-fontified-position
-         (point))
-        (when context-coloring-verbose-parse
-          (message "Elapsed: %s; iterations: %s"
-                   (- (float-time) start-time) iteration-count))))))
+         (point))))))
 
 
 ;;; Shell command scopification / colorization
@@ -809,7 +821,7 @@ read the scopifier's response asynchronously and apply a parsed
 list of tokens to `context-coloring-apply-tokens'.
 
 Invoke CALLBACK when complete."
-  (let ((buffer context-coloring-buffer))
+  (let ((buffer (current-buffer)))
     (context-coloring-scopify-shell-command
      command
      (lambda (output)
@@ -830,6 +842,15 @@ Invoke CALLBACK when complete."
 (defvar context-coloring-mode-hash-table (make-hash-table :test 'eq)
   "Map major mode names to dispatch property lists.")
 
+(defun context-coloring-get-dispatch-for-mode (mode)
+  "Return the dispatch for MODE (or a derivative mode)."
+  (let ((parent mode)
+        dispatch)
+    (while (and parent
+                (not (setq dispatch (gethash parent context-coloring-mode-hash-table)))
+                (setq parent (get parent 'derived-mode-parent))))
+    dispatch))
+
 (defun context-coloring-define-dispatch (symbol &rest properties)
   "Define a new dispatch named SYMBOL with PROPERTIES.
 
@@ -911,9 +932,9 @@ used.")
   (context-coloring-kill-scopifier)
   (setq context-coloring-changed t))
 
-(defun context-coloring-maybe-colorize ()
+(defun context-coloring-maybe-colorize (buffer)
   "Colorize the current buffer if it has changed."
-  (when (and (eq context-coloring-buffer (window-buffer (selected-window)))
+  (when (and (eq buffer (current-buffer))
              context-coloring-changed)
     (setq context-coloring-changed nil)
     (context-coloring-colorize)))
@@ -958,7 +979,7 @@ produces (1 0 0), \"19700101\" produces (19700101), etc."
   "Asynchronously invoke CALLBACK with a predicate indicating
 whether the current scopifier version satisfies the minimum
 version number required for the current major mode."
-  (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+  (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
     (when dispatch
       (let ((version (plist-get dispatch :version))
             (command (plist-get dispatch :command)))
@@ -1325,18 +1346,31 @@ precedence, i.e. the car of `custom-enabled-themes'."
 Increase this if your machine is high-performing.  Decrease it if
 it ain't.
 
-Supported modes: `js-mode', `js3-mode'"
+Supported modes: `js-mode', `js3-mode', `emacs-lisp-mode'"
   :group 'context-coloring)
 
 (defun context-coloring-setup-idle-change-detection ()
   "Setup idle change detection."
   (add-hook
    '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
         (run-with-idle-timer
          context-coloring-delay
          t
-         'context-coloring-maybe-colorize)))
+         'context-coloring-maybe-colorize
+         (current-buffer))))
+
+(defun context-coloring-teardown-idle-change-detection ()
+  "Teardown idle change detection."
+  (context-coloring-kill-scopifier)
+  (when context-coloring-colorize-idle-timer
+    (cancel-timer context-coloring-colorize-idle-timer))
+  (remove-hook
+   'kill-buffer-hook 'context-coloring-teardown-idle-change-detection t)
+  (remove-hook
+   'after-change-functions 'context-coloring-change-function t))
 
 
 ;;; Built-in dispatches
@@ -1363,15 +1397,8 @@ Supported modes: `js-mode', `js3-mode'"
  'emacs-lisp
  :modes '(emacs-lisp-mode)
  :colorizer 'context-coloring-emacs-lisp-colorize
- :setup
- (lambda ()
-   (context-coloring-setup-idle-change-detection))
- :teardown
- (lambda ()
-   (when context-coloring-colorize-idle-timer
-     (cancel-timer context-coloring-colorize-idle-timer))
-   (remove-hook
-    'after-change-functions 'context-coloring-change-function t)))
+ :setup 'context-coloring-setup-idle-change-detection
+ :teardown 'context-coloring-teardown-idle-change-detection)
 
 (defun context-coloring-dispatch (&optional callback)
   "Determine the optimal track for scopification / coloring of
@@ -1379,7 +1406,7 @@ the current buffer, then execute it.
 
 Invoke CALLBACK when complete.  It is invoked synchronously for
 elisp tracks, and asynchronously for shell command tracks."
-  (let* ((dispatch (gethash major-mode context-coloring-mode-hash-table))
+  (let* ((dispatch (context-coloring-get-dispatch-for-mode major-mode))
          (colorizer (plist-get dispatch :colorizer))
          (scopifier (plist-get dispatch :scopifier))
          (command (plist-get dispatch :command))
@@ -1410,32 +1437,28 @@ elisp tracks, and asynchronously for shell command tracks."
   nil " Context" nil
   (if (not context-coloring-mode)
       (progn
-        (context-coloring-kill-scopifier)
-        (when context-coloring-colorize-idle-timer
-          (cancel-timer context-coloring-colorize-idle-timer))
-        (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+        (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
           (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))
+                (context-coloring-teardown-idle-change-detection))
               (when teardown
                 (funcall teardown)))))
         (font-lock-mode)
         (jit-lock-mode t))
 
-    ;; Remember this buffer.  This value should not be dynamically-bound.
-    (setq context-coloring-buffer (current-buffer))
-
     ;; Font lock is incompatible with this mode; the converse is also true.
     (font-lock-mode 0)
     (jit-lock-mode nil)
 
+    ;; ...but we do use font-lock functions here.
+    (font-lock-set-defaults)
+
     ;; Safely change the valye of this function as necessary.
     (make-local-variable 'font-lock-syntactic-face-function)
 
-    (let ((dispatch (gethash major-mode context-coloring-mode-hash-table)))
+    (let ((dispatch (context-coloring-get-dispatch-for-mode major-mode)))
       (if dispatch
           (progn
             (let ((command (plist-get dispatch :command))