From 8e5f212bc7abd7bfc40ab7460008184c6394d2cd Mon Sep 17 00:00:00 2001 From: Andreas Gerler Date: Tue, 13 Nov 2018 11:15:51 +0100 Subject: [PATCH] update color-identifiers. Signed-off-by: Andreas Gerler --- .../color-identifiers-mode-pkg.el | 1 - .../color-identifiers-mode-autoloads.el | 17 +- .../color-identifiers-mode-pkg.el | 2 + .../color-identifiers-mode.el | 319 ++++++++++++++---- 4 files changed, 259 insertions(+), 80 deletions(-) delete mode 100644 elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode-pkg.el rename elpa/{color-identifiers-mode-20150602.1804 => color-identifiers-mode-20181011.2114}/color-identifiers-mode-autoloads.el (72%) create mode 100644 elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode-pkg.el rename elpa/{color-identifiers-mode-20150602.1804 => color-identifiers-mode-20181011.2114}/color-identifiers-mode.el (68%) diff --git a/elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode-pkg.el b/elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode-pkg.el deleted file mode 100644 index fda15031..00000000 --- a/elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode-pkg.el +++ /dev/null @@ -1 +0,0 @@ -(define-package "color-identifiers-mode" "20150602.1804" "Color identifiers based on their names" '((dash "2.5.0") (emacs "24")) :url "https://github.com/ankurdave/color-identifiers-mode" :keywords '("faces" "languages")) diff --git a/elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode-autoloads.el b/elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode-autoloads.el similarity index 72% rename from elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode-autoloads.el rename to elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode-autoloads.el index 11ba07f1..4f021f4f 100644 --- a/elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode-autoloads.el +++ b/elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode-autoloads.el @@ -1,10 +1,13 @@ ;;; color-identifiers-mode-autoloads.el --- automatically extracted autoloads ;; ;;; Code: -(add-to-list 'load-path (or (file-name-directory #$) (car load-path))) + +(add-to-list 'load-path (directory-file-name + (or (file-name-directory #$) (car load-path)))) + ;;;### (autoloads nil "color-identifiers-mode" "color-identifiers-mode.el" -;;;;;; (22294 20041 0 0)) +;;;;;; (0 0 0 0)) ;;; Generated autoloads from color-identifiers-mode.el (autoload 'color-identifiers-mode "color-identifiers-mode" "\ @@ -13,8 +16,9 @@ Color the identifiers in the current buffer based on their names. \(fn &optional ARG)" t nil) (defvar global-color-identifiers-mode nil "\ -Non-nil if Global-Color-Identifiers mode is enabled. -See the command `global-color-identifiers-mode' for a description of this minor mode. +Non-nil if Global Color-Identifiers mode is enabled. +See the `global-color-identifiers-mode' command +for a description of this minor mode. Setting this variable directly does not take effect; either customize it (see the info node `Easy Customization') or call the function `global-color-identifiers-mode'.") @@ -23,7 +27,7 @@ or call the function `global-color-identifiers-mode'.") (autoload 'global-color-identifiers-mode "color-identifiers-mode" "\ Toggle Color-Identifiers mode in all buffers. -With prefix ARG, enable Global-Color-Identifiers mode if ARG is positive; +With prefix ARG, enable Global Color-Identifiers mode if ARG is positive; otherwise, disable it. If called from Lisp, enable the mode if ARG is omitted or nil. @@ -33,11 +37,14 @@ See `color-identifiers-mode' for more information on Color-Identifiers mode. \(fn &optional ARG)" t nil) +(if (fboundp 'register-definition-prefixes) (register-definition-prefixes "color-identifiers-mode" '("color-identifiers"))) + ;;;*** ;; Local Variables: ;; version-control: never ;; no-byte-compile: t ;; no-update-autoloads: t +;; coding: utf-8 ;; End: ;;; color-identifiers-mode-autoloads.el ends here diff --git a/elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode-pkg.el b/elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode-pkg.el new file mode 100644 index 00000000..82a0f2e7 --- /dev/null +++ b/elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode-pkg.el @@ -0,0 +1,2 @@ +;;; -*- no-byte-compile: t -*- +(define-package "color-identifiers-mode" "20181011.2114" "Color identifiers based on their names" '((dash "2.5.0") (emacs "24")) :commit "91296e02dd5f03fe5ee9aa08f95120fb716d2128" :keywords '("faces" "languages") :authors '(("Ankur Dave" . "ankurdave@gmail.com")) :maintainer '("Ankur Dave" . "ankurdave@gmail.com") :url "https://github.com/ankurdave/color-identifiers-mode") diff --git a/elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode.el b/elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode.el similarity index 68% rename from elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode.el rename to elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode.el index a6774230..0bcec3ee 100644 --- a/elpa/color-identifiers-mode-20150602.1804/color-identifiers-mode.el +++ b/elpa/color-identifiers-mode-20181011.2114/color-identifiers-mode.el @@ -4,7 +4,7 @@ ;; Author: Ankur Dave ;; Url: https://github.com/ankurdave/color-identifiers-mode -;; Package-Version: 20150602.1804 +;; Package-Version: 20181011.2114 ;; Created: 24 Jan 2014 ;; Version: 1.1 ;; Keywords: faces, languages @@ -40,8 +40,10 @@ (require 'color) (require 'dash) (require 'python) +(require 'cl-lib) -(defvar color-identifiers:timer) +(defgroup color-identifiers nil "Color identifiers based on their names." + :group 'faces) ;;;###autoload (define-minor-mode color-identifiers-mode @@ -62,7 +64,8 @@ (cancel-timer color-identifiers:timer)) (setq color-identifiers:timer nil) (font-lock-remove-keywords nil '((color-identifiers:colorize . default))) - (ad-deactivate 'enable-theme)) + (ad-deactivate 'enable-theme) + (run-hooks 'color-identifiers-mode-hook)) (color-identifiers:refontify)) ;;;###autoload @@ -75,6 +78,23 @@ ;;; USER-VISIBLE VARIABLES AND FUNCTIONS ======================================= +(defcustom color-identifiers-coloring-method 'sequential + "How to assign colors: sequentially or using the hash of the identifier. +Sequential color assignment (the default) reduces collisions +between adjacent identifiers. Hash-based color assignment ensures +that a particular identifier is always assigned the same color +across buffers." + :type '(choice + (const :tag "Sequential" sequential) + (const :tag "Hash-based" hash))) + + +(defcustom color-identifiers-avoid-faces nil + "Which color faces to avoid: A list of faces whose foreground +color should be avoided when generating colors, this can be warning colors, +error colors etc." + :type '(repeat face)) + (defvar color-identifiers:modes-alist nil "Alist of major modes and the ways to distinguish identifiers in those modes. The value of each cons cell provides four constraints for finding identifiers. @@ -89,7 +109,11 @@ IDENTIFIER-FACES is a list of faces with which the major mode decorates identifiers or a function returning such a list. If the list includes nil, unfontified words will be considered. IDENTIFIER-EXCLUSION-RE is a regexp that must not match identifiers, -or nil.") +or nil. + +If a scan function is registered for a mode, candidate +identifiers will be further restricted to those returned by the +scan function.") (defvar color-identifiers:num-colors 10 "The number of different colors to generate.") @@ -106,13 +130,25 @@ of the default face.") (defvar color-identifiers:mode-to-scan-fn-alist nil "Alist from major modes to their declaration scan functions, for internal use. -Modify this using `color-identifiers:set-declaration-scan-fn'.") +If no scan function is registered for a particular mode, all +candidates matching the constraints in +`color-identifiers:modes-alist' will be colored. + +Modify this variable using +`color-identifiers:set-declaration-scan-fn'.") + +(defvar color-identifiers-mode-hook nil + "List of functions to run every time the mode enabled") (defun color-identifiers:set-declaration-scan-fn (mode scan-fn) "Register SCAN-FN as the declaration scanner for MODE. SCAN-FN must scan the entire current buffer and return the -identifiers to highlight as a list of strings. See -`color-identifiers:elisp-get-declarations' for an example." +identifiers to highlight as a list of strings. Only identifiers +produced by SCAN-FN that also match all constraints in +`color-identifiers:modes-alist' will be colored. + +See `color-identifiers:elisp-get-declarations' for an example +SCAN-FN." (let ((entry (assoc mode color-identifiers:mode-to-scan-fn-alist))) (if entry (setcdr entry scan-fn) @@ -151,7 +187,7 @@ For cc-mode support within color-identifiers-mode." (delete-dups result) result)) -(dolist (maj-mode '(c-mode c++-mode java-mode)) +(dolist (maj-mode '(c-mode c++-mode java-mode rust-mode)) (color-identifiers:set-declaration-scan-fn maj-mode 'color-identifiers:cc-mode-get-declarations) (add-to-list @@ -179,6 +215,18 @@ For cc-mode support within color-identifiers-mode." "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" (nil font-lock-variable-name-face js3-function-param-face)))) +(add-to-list + 'color-identifiers:modes-alist + `(js-jsx-mode . ("[^.][[:space:]]*" + "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" + (nil font-lock-variable-name-face js2-function-param)))) + +(add-to-list + 'color-identifiers:modes-alist + `(js2-jsx-mode . ("[^.][[:space:]]*" + "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" + (nil font-lock-variable-name-face js2-function-param)))) + ;; CoffeeScript ;; May need to add the @ to the symbol syntax ;; (add-hook 'coffee-mode-hook (lambda () (modify-syntax-entry ?\@ "_"))) in .emacs @@ -199,6 +247,16 @@ For cc-mode support within color-identifiers-mode." 'color-identifiers:modes-alist `(ruby-mode . ("[^.][[:space:]]*" "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" (nil)))) +;; R +(add-to-list + 'color-identifiers:modes-alist + `(R-mode . ("[^.][[:space:]]*" "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" (nil)))) + +;; SQL +(add-to-list + 'color-identifiers:modes-alist + `(sql-mode . ("[^.][[:space:]]*" "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" (nil)))) + ;; Groovy (add-to-list 'color-identifiers:modes-alist @@ -214,6 +272,13 @@ For cc-mode support within color-identifiers-mode." (nil font-lock-variable-name-face) "[a-zA-Z_$]\\(\\s_\\|\\sw\\)*\\s-*[(:]"))) +;; Golang +(add-to-list + 'color-identifiers:modes-alist + `(go-mode . ("[^.][[:space:]]*" + "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\)*\\)" + (nil font-lock-variable-name-face)))) + ;; Python (when (fboundp 'python-nav-forward-defun) (defun color-identifiers:python-get-declarations () @@ -226,18 +291,20 @@ arguments, loops (for .. in), or for comprehensions." (save-excursion (goto-char (point-min)) (while (python-nav-forward-defun) - (let ((arglist (sexp-at-point))) - (when (and arglist (listp arglist)) - (let* ((first-arg (car arglist)) - (rest (cdr arglist)) - (rest-args - (-map (lambda (token) (cadr token)) - (-filter (lambda (token) (and (listp token) (eq (car token) '\,))) rest))) - (args-filtered (cons first-arg rest-args)) - (params (-map (lambda (token) - (car (split-string (symbol-name token) "="))) - args-filtered))) - (setq result (append params result))))))) + (condition-case nil + (let ((arglist (sexp-at-point))) + (when (and arglist (listp arglist)) + (let* ((first-arg (car arglist)) + (rest (cdr arglist)) + (rest-args + (-map (lambda (token) (cadr token)) + (-filter (lambda (token) (and (listp token) (eq (car token) '\,))) rest))) + (args-filtered (cons first-arg rest-args)) + (params (-map (lambda (token) + (car (split-string (symbol-name token) "="))) + args-filtered))) + (setq result (append params result))))) + (wrong-type-argument nil)))) ;; Variables that python-mode highlighted with font-lock-variable-name-face (save-excursion (goto-char (point-min)) @@ -359,7 +426,7 @@ For Clojure support within color-identifiers-mode. " (append (when (sequencep (car rest)) (let* ((bindings (append (car rest) nil)) (even-indices - (-filter 'evenp (number-sequence 0 (1- (length bindings))))) + (-filter 'cl-evenp (number-sequence 0 (1- (length bindings))))) (binding-forms (-select-by-indices even-indices bindings))) (color-identifiers:clojure-extract-params binding-forms))) (color-identifiers:clojure-declarations-in-sexp rest))) @@ -420,7 +487,8 @@ incompatible with Emacs Lisp syntax, such as reader macros (#)." (when (symbolp id) (symbol-name id))) ids)))) (setq result (append strs result))) - (invalid-read-syntax nil))) + (invalid-read-syntax nil) + (wrong-type-argument nil))) (end-of-file nil))) (delete-dups result) result)) @@ -434,16 +502,79 @@ incompatible with Emacs Lisp syntax, such as reader macros (#)." "\\_<\\(\\(?:\\s_\\|\\sw\\)+\\)" (nil)))) +(dolist (maj-mode '(tuareg-mode sml-mode)) + (color-identifiers:set-declaration-scan-fn + maj-mode 'color-identifiers:cc-mode-get-declarations) + (add-to-list + 'color-identifiers:modes-alist + `(,maj-mode . ("" + "\\_<\\([a-zA-Z_$]\\(?:\\s_\\|\\sw\\|'\\)*\\)" + (nil font-lock-variable-name-face))))) + +;; R support in ess-mode +(defun color-identifiers:remove-string-or-comment (str) + "Remove string or comment in str, based on font lock faces" + (let ((remove (memq (get-text-property 0 'face str) + '(font-lock-string-face font-lock-comment-face))) + (pos 0) + (nextpos) + (result "")) + (while (setq nextpos (next-single-property-change pos 'face str)) + (unless remove + (setq result (concat result (substring-no-properties str pos nextpos)))) + (setq pos nextpos) + (setq remove (memq (get-text-property pos 'face str) + '(font-lock-string-face font-lock-comment-face)))) + (unless remove + (setq result (concat result (substring-no-properties str pos nextpos)))) + result)) + + (defun color-identifiers:r-get-args (lend) + "Extract a list of function arg names. LEND is the point at +the left parenthesis, after `function' keyword." + (let* ((rend (save-excursion + (goto-char lend) + (forward-sexp) + (point))) + (str (color-identifiers:remove-string-or-comment + (buffer-substring (1+ lend) (1- rend)))) + (result)) + (mapcar (lambda (s) (replace-regexp-in-string "\\s *=.*" "" s)) + (split-string str "," t " ")))) + + (defun color-identifiers:r-get-declarations () + "Extract a list of identifiers declared in the current buffer. +For Emacs Lisp support within color-identifiers-mode." + (let ((result nil)) + (save-excursion + (goto-char (point-min)) + (while (re-search-forward "\\(\\(?:\\w\\|\\s_\\)*\\)\\s *< (cdr x) (cdr y))) min-dists))) (funcall choose-candidate (car best)))) (setq color-identifiers:colors (-map (lambda (lab) - (apply 'color-rgb-to-hex (apply 'color-lab-to-srgb lab))) + (let* ((srgb (apply 'color-lab-to-srgb lab)) + (rgb (mapcar 'color-clamp srgb))) + (apply 'color-rgb-to-hex rgb))) chosens))))) (defvar color-identifiers:color-index-for-identifier nil "Alist of identifier-index pairs for internal use. -The index refers to `color-identifiers:colors'.") +The index refers to `color-identifiers:colors'. Only used when +`color-identifiers-coloring-method' is `sequential'.") (make-variable-buffer-local 'color-identifiers:color-index-for-identifier) -(defvar color-identifiers:current-index 0 - "Current color index for new identifiers, for internal use. -The index refers to `color-identifiers:colors'.") -(make-variable-buffer-local 'color-identifiers:current-index) +(defvar color-identifiers:identifiers nil + "Set of identifiers in the current buffer. +Only used when `color-identifiers-coloring-method' is `hash' and +a declaration scan function is registered for the current major +mode. This variable memoizes the result of the declaration scan function.") +(make-variable-buffer-local 'color-identifiers:identifiers) (defun color-identifiers:attribute-luminance (attribute) "Find the HSL luminance of the specified ATTRIBUTE on the default face." @@ -521,35 +658,62 @@ The index refers to `color-identifiers:colors'.") (apply 'color-srgb-to-lab rgb) '(0.0 0.0 0.0)))) +(defun color-identifiers:foreground-lab (face) + "Find the LAB color value of the foreground attribute on the +specified face." + (let ((rgb (color-name-to-rgb (face-attribute face :foreground)))) + (if rgb + (apply 'color-srgb-to-lab rgb) + '(0.0 0.0 0.0)))) + (defun color-identifiers:refresh () - "Refresh `color-identifiers:color-index-for-identifier' from current buffer." + "Refresh the set of identifiers in the current buffer. +If `color-identifiers-coloring-method' is `sequential', +identifiers and their corresponding color indexes are saved to +`color-identifiers:color-index-for-identifier'. + +If `color-identifiers-coloring-method' is `hash' and a +declaration scan function is registered for the current buffer's +major mode, identifiers are saved to +`color-identifiers:identifiers'." (interactive) (when color-identifiers-mode - (if (color-identifiers:get-declaration-scan-fn major-mode) - (progn - (setq color-identifiers:identifiers - (funcall (color-identifiers:get-declaration-scan-fn major-mode))) - (setq color-identifiers:color-index-for-identifier - (-map-indexed (lambda (i identifier) - (cons identifier (% i color-identifiers:num-colors))) - color-identifiers:identifiers))) - (save-excursion - (goto-char (point-min)) - (catch 'input-pending - (let ((i 0) - (n color-identifiers:num-colors) - (result nil)) - (color-identifiers:scan-identifiers - (lambda (start end) - (let ((identifier (buffer-substring-no-properties start end))) - (unless (assoc-string identifier result) - (push (cons identifier (% i n)) result) - (setq i (1+ i))))) - (point-max) - (lambda () (if (input-pending-p) (throw 'input-pending nil) t))) - (setq color-identifiers:color-index-for-identifier result))))) + (cond + ((eq color-identifiers-coloring-method 'sequential) + (setq color-identifiers:color-index-for-identifier + (append (-map-indexed + (lambda (i identifier) + ;; to make sure subsequently added vars aren't colorized the same add a (point) + (cons identifier (% (+ (point) i) color-identifiers:num-colors))) + (-filter (lambda (e) + (cl-notany (lambda (d) (equal e (car d))) + color-identifiers:color-index-for-identifier)) + (color-identifiers:list-identifiers))) + color-identifiers:color-index-for-identifier))) + ((and (eq color-identifiers-coloring-method 'hash) + (color-identifiers:get-declaration-scan-fn major-mode)) + (setq color-identifiers:identifiers + (color-identifiers:list-identifiers)))) (color-identifiers:refontify))) +(defun color-identifiers:list-identifiers () + "Return all identifiers in the current buffer." + (if (color-identifiers:get-declaration-scan-fn major-mode) + (funcall (color-identifiers:get-declaration-scan-fn major-mode)) + ;; When no scan function is registered, fall back to + ;; `color-identifiers:scan-identifiers', which returns all identifiers + (save-excursion + (goto-char (point-min)) + (catch 'input-pending + (let ((result nil)) + (color-identifiers:scan-identifiers + (lambda (start end) + (push (buffer-substring-no-properties start end) result)) + (point-max) + (lambda () (if (input-pending-p) (throw 'input-pending nil) t))) + (delete-dups result) + result))))) + (defun color-identifiers:refontify () "Refontify the buffer using font-lock." (if (fboundp 'font-lock-flush) @@ -559,25 +723,32 @@ The index refers to `color-identifiers:colors'.") (font-lock-fontify-buffer))))) (defun color-identifiers:color-identifier (identifier) - "Look up or generate the hex color for IDENTIFIER. -IDENTIFIER is looked up in `color-identifiers:color-index-for-identifier' and -generated if not present there." - (unless (and (color-identifiers:get-declaration-scan-fn major-mode) - (not (member identifier color-identifiers:identifiers))) + "Return the hex color for IDENTIFIER, or nil if it should not +be colored." + (cond + ((eq color-identifiers-coloring-method 'sequential) (let ((entry (assoc-string identifier color-identifiers:color-index-for-identifier))) - (if entry - (nth (cdr entry) color-identifiers:colors) - ;; If not present, make a temporary color using the rotating index - (push (cons identifier (% color-identifiers:current-index - (length color-identifiers:colors))) - color-identifiers:color-index-for-identifier) - (setq color-identifiers:current-index - (1+ color-identifiers:current-index)))))) + (when entry + (nth (cdr entry) color-identifiers:colors)))) + ((eq color-identifiers-coloring-method 'hash) + ;; If there is a declaration scan function for this major mode, the + ;; candidate identifier should only be colored if it is in the memoized list + ;; of identifiers. Otherwise, it should be colored unconditionally. + (when (or (not (color-identifiers:get-declaration-scan-fn major-mode)) + (member identifier color-identifiers:identifiers)) + (color-identifiers:hash-identifier identifier))))) + +(defun color-identifiers:hash-identifier (identifier) + "Return a color for IDENTIFIER based on its hash." + (nth (% (abs (sxhash identifier)) color-identifiers:num-colors) + color-identifiers:colors)) (defun color-identifiers:scan-identifiers (fn limit &optional continue-p) - "Run FN on all identifiers from point up to LIMIT. -Identifiers are defined by `color-identifiers:modes-alist'. -If supplied, iteration only continues if CONTINUE-P evaluates to true." + "Run FN on all candidate identifiers from point up to LIMIT. +Candidate identifiers are defined by +`color-identifiers:modes-alist'. Declaration scan functions are +not applied. If supplied, iteration only continues if CONTINUE-P +evaluates to true." (let ((entry (assoc major-mode color-identifiers:modes-alist))) (when entry (let ((identifier-context-re (nth 1 entry)) @@ -596,7 +767,7 @@ If supplied, iteration only continues if CONTINUE-P evaluates to true." (and flface-prop (memq flface-prop identifier-faces))) (get-text-property (point) 'color-identifiers:fontified))) (goto-char (next-property-change (point) nil limit)) - (if (not (and (looking-back identifier-context-re) + (if (not (and (looking-back identifier-context-re nil) (or (not identifier-exclusion-re) (not (looking-at identifier-exclusion-re))) (looking-at identifier-re))) (progn -- 2.47.3