aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Távora2023-11-29 16:48:34 -0600
committerJoão Távora2024-02-03 08:40:03 -0600
commit0f715f9c154a47de57a2f24f19b4a402604e6dc0 (patch)
tree598f6a7a5a16e5f20d928713645b4b407faded23
parent17c3610c56155dd5b1efd5b7e8d6a58112f43a59 (diff)
downloademacs-0f715f9c154a47de57a2f24f19b4a402604e6dc0.tar.gz
emacs-0f715f9c154a47de57a2f24f19b4a402604e6dc0.zip
Improve shorthands-font-lock-shorthands (bug#67390)
Add font locking to the shorthand prefix of a given printed symbol name by checking if any of the shorthand prefixes in read-symbol-shorthands are a prefix for that print name. Although this does more string comparisons, it didn't prove to be any slower than the existing approach, and is more correct. This version is more accurate when highlighting files with many overlapping shorthands. Given: ;; Local Variables: ;; read-symbol-shorthands: (("bc-" . "breadcrumb-") ;; ("aw-" . "ace-window-") ;; ("zorglub/" . "ace-window-") ;; ("he//" . "hyperdrive-entry--") ;; ("h//" . "hyperdrive--") ;; ("he/" . "hyperdrive-entry-") ;; ("h/" . "hyperdrive-")) ;; End: The following are correct highlights on print names '(zorglub/blerh ; hilits "zorglub/" reads to 'ace-window-blerh' he/foo ; hilits "he/" reads to 'hyperdrive-entry-foo' he//bar ; hilits "he//" reads to 'hyperdrive-entry--bar' h/coiso ; hilits "h/" reads to 'hyperdrive-coiso' h//thingy ; hilits "h//" reads to 'hyperdrive--thingy' bc-yo ; hilits "bc-" reads to 'breadcrumb-yo' aw-thingy ; hilits "aw-" reads to 'ace-window-thingy' ) Co-authored-by: Jonas Bernoulli <jonas@bernoul.li> Co-authored-by: Joseph Turner <joseph@ushin.org> * lisp/emacs-lisp/shorthands.el (shorthands-font-lock-shorthands):
-rw-r--r--lisp/emacs-lisp/shorthands.el34
1 files changed, 11 insertions, 23 deletions
diff --git a/lisp/emacs-lisp/shorthands.el b/lisp/emacs-lisp/shorthands.el
index 6348aaccf93..379fb0baec9 100644
--- a/lisp/emacs-lisp/shorthands.el
+++ b/lisp/emacs-lisp/shorthands.el
@@ -52,38 +52,26 @@
52 :version "28.1" 52 :version "28.1"
53 :group 'font-lock-faces) 53 :group 'font-lock-faces)
54 54
55(defun shorthands--mismatch-from-end (str1 str2)
56 "Tell index of first mismatch in STR1 and STR2, from end.
57The index is a valid 0-based index on STR1. Returns nil if STR1
58equals STR2. Return 0 if STR1 is a suffix of STR2."
59 (cl-loop with l1 = (length str1) with l2 = (length str2)
60 for i from 1
61 for i1 = (- l1 i) for i2 = (- l2 i)
62 while (eq (aref str1 i1) (aref str2 i2))
63 if (zerop i2) return (if (zerop i1) nil i1)
64 if (zerop i1) return 0
65 finally (return i1)))
66
67(defun shorthands-font-lock-shorthands (limit) 55(defun shorthands-font-lock-shorthands (limit)
56 "Font lock until LIMIT considering `read-symbol-shorthands'."
68 (when read-symbol-shorthands 57 (when read-symbol-shorthands
69 (while (re-search-forward 58 (while (re-search-forward
70 (concat "\\_<\\(" (rx lisp-mode-symbol) "\\)\\_>") 59 (concat "\\_<\\(" (rx lisp-mode-symbol) "\\)\\_>")
71 limit t) 60 limit t)
72 (let* ((existing (get-text-property (match-beginning 1) 'face)) 61 (let* ((existing (get-text-property (match-beginning 1) 'face))
62 (print-name (match-string 1))
73 (probe (and (not (memq existing '(font-lock-comment-face 63 (probe (and (not (memq existing '(font-lock-comment-face
74 font-lock-string-face))) 64 font-lock-string-face)))
75 (intern-soft (match-string 1)))) 65 (intern-soft print-name)))
76 (sname (and probe (symbol-name probe))) 66 (symbol-name (and probe (symbol-name probe)))
77 (mismatch (and sname (shorthands--mismatch-from-end 67 (prefix (and symbol-name
78 (match-string 1) sname))) 68 (not (string-equal print-name symbol-name))
79 (guess (and mismatch (1+ mismatch)))) 69 (car (assoc print-name
80 (when guess 70 read-symbol-shorthands
81 (when (and (< guess (1- (length (match-string 1)))) 71 #'string-prefix-p)))))
82 ;; In bug#67390 we allow other separators 72 (when prefix
83 (eq (char-syntax (aref (match-string 1) guess)) ?_))
84 (setq guess (1+ guess)))
85 (add-face-text-property (match-beginning 1) 73 (add-face-text-property (match-beginning 1)
86 (+ (match-beginning 1) guess) 74 (+ (match-beginning 1) (length prefix))
87 'elisp-shorthand-font-lock-face)))))) 75 'elisp-shorthand-font-lock-face))))))
88 76
89(font-lock-add-keywords 'emacs-lisp-mode '((shorthands-font-lock-shorthands)) t) 77(font-lock-add-keywords 'emacs-lisp-mode '((shorthands-font-lock-shorthands)) t)