+;;; Emacs Lisp colorization
+
+(defsubst context-coloring-make-scope (depth level)
+ (list
+ :depth depth
+ :level level
+ :variables (make-hash-table)))
+
+(defsubst context-coloring-scope-get-level (scope)
+ (plist-get scope :level))
+
+(defsubst context-coloring-scope-add-variable (scope variable)
+ (puthash variable t (plist-get scope :variables)))
+
+(defsubst context-coloring-scope-get-variable (scope variable)
+ (gethash variable (plist-get scope :variables)))
+
+(defsubst context-coloring-get-variable-level (scope-stack variable)
+ (let* (scope
+ level)
+ (while (and scope-stack (not level))
+ (setq scope (car scope-stack))
+ (cond
+ ((context-coloring-scope-get-variable scope variable)
+ (setq level (context-coloring-scope-get-level scope)))
+ (t
+ (setq scope-stack (cdr scope-stack)))))
+ ;; Assume a global variable.
+ (or level 0)))
+
+(defsubst context-coloring-make-backtick (end enabled)
+ (list
+ :end end
+ :enabled enabled))
+
+(defsubst context-coloring-backtick-get-end (backtick)
+ (plist-get backtick :end))
+
+(defsubst context-coloring-backtick-get-enabled (backtick)
+ (plist-get backtick :enabled))
+
+(defsubst context-coloring-backtick-enabled-p (backtick-stack)
+ (context-coloring-backtick-get-enabled (car backtick-stack)))
+
+(defsubst context-coloring-make-let-varlist (depth type)
+ (list
+ :depth depth
+ :type type
+ :vars '()))
+
+(defsubst context-coloring-let-varlist-get-type (let-varlist)
+ (plist-get let-varlist :type))
+
+(defsubst context-coloring-let-varlist-add-var (let-varlist var)
+ (plist-put let-varlist :vars (cons var (plist-get let-varlist :vars))))
+
+(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
+ ;; `let' binds all at once at the end.
+ ((eq type 'let)
+ (prog1
+ vars
+ (plist-put let-varlist :vars '())))
+ ;; `let*' binds incrementally.
+ ((eq type 'let*)
+ (prog1
+ (list (car vars))
+ (plist-put let-varlist :vars (cdr vars)))))))
+
+(defsubst context-coloring-forward-sws ()
+ "Move forward through whitespace and comments."
+ (while (forward-comment 1)))
+
+(defsubst context-coloring-forward-sexp-position ()
+ "Like vanilla `forward-sexp', but just return the position."
+ (scan-sexps (point) 1))
+
+(defsubst context-coloring-emacs-lisp-identifier-syntax-p (syntax-code)
+ (or (= 2 syntax-code)
+ (= 3 syntax-code)))
+
+(defsubst context-coloring-open-parenthesis-p (syntax-code)
+ (= 4 syntax-code))
+
+(defsubst context-coloring-close-parenthesis-p (syntax-code)
+ (= 5 syntax-code))
+
+(defsubst context-coloring-expression-prefix-p (syntax-code)
+ (= 6 syntax-code))
+
+(defsubst context-coloring-at-open-parenthesis-p ()
+ (= 4 (logand #xFFFF (car (syntax-after (point))))))
+
+(defsubst context-coloring-ppss-depth (ppss)
+ ;; Same as (nth 0 ppss).
+ (car ppss))
+
+(defsubst context-coloring-at-stack-depth-p (stack depth)
+ (= (plist-get (car stack) :depth) depth))
+
+(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
+ (concat "\\`[-+]?[0-9]\\|" (context-coloring-exact-or-regexp
+ '("t" "nil" "." "?"))))
+
+(defconst context-coloring-COMMA-CHAR 44)
+(defconst context-coloring-BACKTICK-CHAR 96)
+
+(defvar context-coloring-parse-interruptable-p t
+ "Set this to nil to force parse to continue until finished.")
+
+(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.")
+
+(defun context-coloring-emacs-lisp-colorize ()
+ "Color the current buffer by parsing emacs lisp sexps."
+ (with-silent-modifications
+ (save-excursion
+ ;; TODO: Can probably make this lazy to the nearest defun.
+ (goto-char (point-min))
+ (let* ((inhibit-point-motion-hooks t)
+ (end (point-max))
+ (iteration-count 0)
+ (last-fontified-position (point))
+ beginning-of-current-defun
+ end-of-current-defun
+ (last-ppss-pos (point))
+ (ppss (syntax-ppss))
+ ppss-depth
+ ;; -1 never matches a depth. This is a minor optimization.
+ (scope-stack `(,(context-coloring-make-scope -1 0)))
+ (backtick-stack '())
+ (let-varlist-stack '())
+ (let-var-stack '())
+ popped-vars
+ one-word-found-p
+ in-defun-p
+ in-lambda-p
+ in-let-p
+ in-let*-p
+ defun-arglist
+ defun-arg
+ let-varlist
+ let-varlist-type
+ variable
+ variable-end
+ variable-string
+ variable-scope-level
+ token-pos
+ token-syntax
+ token-syntax-code
+ token-char
+ child-0-pos
+ child-0-end
+ child-0-syntax
+ child-0-syntax-code
+ child-0-string
+ child-1-pos
+ child-1-end
+ child-1-syntax
+ child-1-syntax-code
+ child-2-end)
+ (while (> end (progn (skip-syntax-forward "^()w_'" end)
+ (point)))
+ ;; Sparingly-executed tasks.
+ (setq iteration-count (1+ iteration-count))
+ (when (zerop (% iteration-count
+ context-coloring-emacs-lisp-iterations-per-pause))
+ ;; 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.
+ (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
+ (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)))
+
+ (setq token-pos (point))
+ (setq token-syntax (syntax-after token-pos))
+ (setq token-syntax-code (logand #xFFFF (car token-syntax)))
+ (setq token-char (char-after))
+ (setq ppss (parse-partial-sexp last-ppss-pos token-pos nil nil ppss))
+ (setq last-ppss-pos token-pos)
+ (cond
+
+ ;; Resolve an invalid state.
+ ((cond
+ ;; Inside string?
+ ((nth 3 ppss)
+ (skip-syntax-forward "^\"" end)
+ (forward-char)
+ t)
+ ;; Inside comment?
+ ((nth 4 ppss)
+ (skip-syntax-forward "^>" end)
+ t)))
+
+ ;; Need to check early in case there's a comma.
+ ((context-coloring-expression-prefix-p token-syntax-code)
+ (forward-char)
+ (cond
+ ;; Skip top-level symbols.
+ ((not (or backtick-stack
+ (= token-char context-coloring-BACKTICK-CHAR)))
+ (goto-char (context-coloring-forward-sexp-position)))
+ ;; Push a backtick state.
+ ((or (= token-char context-coloring-BACKTICK-CHAR)
+ (= token-char context-coloring-COMMA-CHAR))
+ (setq backtick-stack (cons (context-coloring-make-backtick
+ (context-coloring-forward-sexp-position)
+ (= token-char context-coloring-BACKTICK-CHAR))
+ backtick-stack)))))
+
+ ;; Pop a backtick state.
+ ((and backtick-stack
+ (>= (point) (context-coloring-backtick-get-end (car backtick-stack))))
+ (setq backtick-stack (cdr backtick-stack)))
+
+ ;; Restricted by an enabled backtick.
+ ((and backtick-stack
+ (context-coloring-backtick-enabled-p backtick-stack))
+ (forward-char))
+
+ ((context-coloring-open-parenthesis-p token-syntax-code)
+ (forward-char)
+ ;; Look for function calls.
+ (context-coloring-forward-sws)
+ (setq child-0-pos (point))
+ (setq child-0-syntax (syntax-after child-0-pos))
+ (setq child-0-syntax-code (logand #xFFFF (car child-0-syntax)))
+ (cond
+ ((context-coloring-emacs-lisp-identifier-syntax-p child-0-syntax-code)
+ (setq one-word-found-p t)
+ (setq child-0-end (scan-sexps child-0-pos 1))
+ (setq child-0-string (buffer-substring-no-properties child-0-pos child-0-end))
+ (cond
+ ;; Parse a var in a `let' varlist.
+ ((and
+ let-varlist-stack
+ (context-coloring-at-stack-depth-p
+ let-varlist-stack
+ ;; 1- because we're inside the varlist.
+ (1- (context-coloring-ppss-depth ppss))))
+ (context-coloring-let-varlist-add-var
+ (car let-varlist-stack)
+ (intern child-0-string))
+ (setq let-var-stack (cons (context-coloring-ppss-depth ppss)
+ let-var-stack)))
+ ((string-match-p context-coloring-emacs-lisp-defun-regexp child-0-string)
+ (setq in-defun-p t))
+ ((string-match-p context-coloring-emacs-lisp-lambda-regexp child-0-string)
+ (setq in-lambda-p t))
+ ((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 context-coloring-emacs-lisp-let*-regexp child-0-string)
+ (setq in-let*-p t)
+ (setq let-varlist-type 'let*)))))
+ (when (or in-defun-p
+ in-lambda-p
+ in-let-p
+ in-let*-p)
+ (setq scope-stack (cons (context-coloring-make-scope
+ (context-coloring-ppss-depth ppss)
+ (1+ (context-coloring-scope-get-level
+ (car scope-stack))))
+ scope-stack)))
+ ;; 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
+ (car scope-stack)))
+ (cond
+ ((or in-defun-p
+ in-lambda-p)
+ (goto-char child-0-end)
+ (when in-defun-p
+ ;; Look for a function name.
+ (context-coloring-forward-sws)
+ (setq child-1-pos (point))
+ (setq child-1-syntax (syntax-after child-1-pos))
+ (setq child-1-syntax-code (logand #xFFFF (car child-1-syntax)))
+ (cond
+ ((context-coloring-emacs-lisp-identifier-syntax-p child-1-syntax-code)
+ (setq child-1-end (scan-sexps child-1-pos 1))
+ ;; Defuns are global, so use level 0.
+ (context-coloring-colorize-region child-1-pos child-1-end 0)
+ (goto-char child-1-end))))
+ ;; Look for an arglist.
+ (context-coloring-forward-sws)
+ (when (context-coloring-at-open-parenthesis-p)
+ ;; (Actually it should be `child-1-end' for `lambda'.)
+ (setq child-2-end (context-coloring-forward-sexp-position))
+ (setq defun-arglist (read (buffer-substring-no-properties
+ (point)
+ child-2-end)))
+ (while defun-arglist
+ (setq defun-arg (car defun-arglist))
+ (when (and (symbolp defun-arg)
+ (string-match-p
+ context-coloring-arglist-arg-regexp
+ (symbol-name defun-arg)))
+ (context-coloring-scope-add-variable
+ (car scope-stack)
+ defun-arg))
+ (setq defun-arglist (cdr defun-arglist)))
+ (goto-char child-2-end))
+ ;; Cleanup.
+ (setq in-defun-p nil)
+ (setq in-lambda-p nil))
+ ((or in-let-p
+ in-let*-p)
+ (goto-char child-0-end)
+ ;; Look for a varlist.
+ (context-coloring-forward-sws)
+ (setq child-1-pos (point))
+ (setq child-1-syntax (syntax-after child-1-pos))
+ (setq child-1-syntax-code (logand #xFFFF (car child-1-syntax)))
+ (when (context-coloring-open-parenthesis-p child-1-syntax-code)
+ ;; Begin parsing the varlist.
+ (forward-char)
+ (setq let-varlist-stack (cons (context-coloring-make-let-varlist
+ ;; 1+ because we parsed it at a
+ ;; higher depth.
+ (1+ (context-coloring-ppss-depth ppss))
+ let-varlist-type)
+ let-varlist-stack)))
+ ;; Cleanup.
+ (setq in-let-p nil)
+ (setq in-let*-p nil))
+ (t
+ (goto-char (cond
+ ;; If there was a word, continue parsing after it.
+ (one-word-found-p
+ (1+ child-0-end))
+ (t
+ (1+ token-pos))))))
+ ;; Cleanup.
+ (setq one-word-found-p nil))
+
+ ((context-coloring-emacs-lisp-identifier-syntax-p token-syntax-code)
+ (setq variable-end (context-coloring-forward-sexp-position))
+ (setq variable-string (buffer-substring-no-properties
+ token-pos
+ variable-end))
+ (cond
+ ;; Ignore constants such as numbers, keywords, t, nil. These can't
+ ;; be rebound, so they should be treated like syntax.
+ ((string-match-p context-coloring-ignored-word-regexp variable-string))
+ ((keywordp (read variable-string)))
+ (t
+ (setq variable (intern variable-string))
+ (cond
+ ;; Parse a `let' varlist's uninitialized var.
+ ((and
+ let-varlist-stack
+ (context-coloring-at-stack-depth-p
+ let-varlist-stack
+ ;; 1- because we're inside the varlist.
+ (1- (context-coloring-ppss-depth ppss))))
+ (setq let-varlist (car let-varlist-stack))
+ (setq let-varlist-type (context-coloring-let-varlist-get-type let-varlist))
+ (cond
+ ;; Defer `let' binding until the end of the varlist.
+ ((eq let-varlist-type 'let)
+ (context-coloring-let-varlist-add-var let-varlist variable))
+ ;; Bind a `let*' right away.
+ ((eq let-varlist-type 'let*)
+ (context-coloring-scope-add-variable (car scope-stack) variable))))
+ (t
+ (setq variable-scope-level
+ (context-coloring-get-variable-level scope-stack variable))
+ (when (/= variable-scope-level (context-coloring-scope-get-level
+ (car scope-stack)))
+ (context-coloring-colorize-region
+ token-pos
+ variable-end
+ variable-scope-level))))))
+ (goto-char variable-end))
+
+ ((context-coloring-close-parenthesis-p token-syntax-code)
+ (forward-char)
+ (setq ppss (parse-partial-sexp last-ppss-pos (point) nil nil ppss))
+ (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-at-stack-depth-p scope-stack ppss-depth)
+ (setq scope-stack (cdr scope-stack)))
+ (when (and
+ let-var-stack
+ (= (car let-var-stack) ppss-depth))
+ (setq let-var-stack (cdr let-var-stack))
+ (when (eq (context-coloring-let-varlist-get-type (car let-varlist-stack))
+ 'let*)
+ (setq popped-vars (context-coloring-let-varlist-pop-vars
+ (car let-varlist-stack)))))
+ (when (and
+ let-varlist-stack
+ (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)))
+ (while popped-vars
+ (context-coloring-scope-add-variable (car scope-stack) (car popped-vars))
+ (setq popped-vars (cdr popped-vars))))
+
+ ))
+ ;; Fontify the last stretch.
+ (context-coloring-maybe-colorize-comments-and-strings
+ last-fontified-position
+ (point))))))
+
+