emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/rainbow-delimiters 36df2e014c 067/188: Add code to highlig


From: ELPA Syncer
Subject: [nongnu] elpa/rainbow-delimiters 36df2e014c 067/188: Add code to highlight mismatched closing delimiters.
Date: Sat, 1 Jan 2022 00:58:53 -0500 (EST)

branch: elpa/rainbow-delimiters
commit 36df2e014ce6e8d067e371c00b134aca84430ff7
Author: Fanael Linithien <fanael4@gmail.com>
Commit: Fanael Linithien <fanael4@gmail.com>

    Add code to highlight mismatched closing delimiters.
    
    Fixes #27.
---
 rainbow-delimiters.el | 97 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 37 deletions(-)

diff --git a/rainbow-delimiters.el b/rainbow-delimiters.el
index 16b8465f90..b6e784e7a8 100644
--- a/rainbow-delimiters.el
+++ b/rainbow-delimiters.el
@@ -216,6 +216,12 @@ Nil disables brace highlighting."
   "Face to highlight unmatched closing delimiters in."
   :group 'rainbow-delimiters-faces)
 
+;; Mismatched delimiter face:
+(defface rainbow-delimiters-mismatched-face
+  '((t :inherit rainbow-delimiters-unmatched-face))
+  "Face to highlight mismatched closing delimiters in."
+  :group 'rainbow-delimiters-faces)
+
 ;; Faces for highlighting delimiters by nested level:
 (defface rainbow-delimiters-depth-1-face
   '((((background light)) (:foreground "#707183"))
@@ -446,18 +452,23 @@ not really affect the buffer's content."
              (unless ,modified
                (restore-buffer-modified-p nil))))))))
 
-(defsubst rainbow-delimiters-propertize-delimiter (loc depth)
+(defsubst rainbow-delimiters-propertize-delimiter (loc depth match)
   "Highlight a single delimiter at LOC according to DEPTH.
 
 LOC is the location of the character to add text properties to.
 DEPTH is the nested depth at LOC, which determines the face to use.
+MATCH is nil iff it's a mismatched closing delimiter.
 
 Sets text properties:
 `font-lock-face' to the appropriate delimiter face.
 `rear-nonsticky' to prevent color from bleeding into subsequent characters 
typed by the user."
-  (let ((delim-face (if (<= depth 0)
-                        'rainbow-delimiters-unmatched-face
-                      (rainbow-delimiters-depth-face depth))))
+  (let ((delim-face (cond
+                     ((<= depth 0)
+                      'rainbow-delimiters-unmatched-face)
+                     ((not match)
+                      'rainbow-delimiters-mismatched-face)
+                     (t
+                      (rainbow-delimiters-depth-face depth)))))
     ;; (when (eq depth -1) (message "Unmatched delimiter at char %s." loc))
     (add-text-properties loc (1+ loc)
                          `(font-lock-face ,delim-face
@@ -494,33 +505,35 @@ Sets text properties:
   "Non-nil iff the character at LOC is escaped as per some generic Lisp rules."
   (eq (char-before loc) ?\\))
 
-(defsubst rainbow-delimiters-char-ineligible-p (loc)
+(defsubst rainbow-delimiters-char-ineligible-p (loc ppss)
   "Return t if char at LOC should be skipped, e.g. if inside a comment.
+PPSS should be the `parse-partial-sexp' state at LOC.
 
 Returns t if char at loc meets one of the following conditions:
 - Inside a string.
 - Inside a comment.
 - Is an escaped char, e.g. ?\)"
-  (let ((parse-state (rainbow-delimiters-syntax-ppss loc)))
-    (or
-     (nth 3 parse-state)                ; inside string?
-     (nth 4 parse-state)                ; inside comment?
-     (and rainbow-delimiters-escaped-char-predicate
-          (funcall rainbow-delimiters-escaped-char-predicate loc)))))
+  (or
+   (nth 3 ppss)                ; inside string?
+   (nth 4 ppss)                ; inside comment?
+   (and rainbow-delimiters-escaped-char-predicate
+        (funcall rainbow-delimiters-escaped-char-predicate loc))))
 
 
-(defsubst rainbow-delimiters-apply-color (delim depth loc)
+(defsubst rainbow-delimiters-apply-color (delim depth loc match)
   "Apply color for DEPTH to DELIM at LOC following user settings.
 
 DELIM is a string specifying delimiter type.
 DEPTH is the delimiter depth, or corresponding face # if colors are repeating.
-LOC is location of character (delimiter) to be colorized."
+LOC is location of character (delimiter) to be colorized.
+MATCH is nil iff it's a mismatched closing delimiter."
   (and
    ;; Ensure user has enabled highlighting of this delimiter type.
    (symbol-value (intern-soft
                   (concat "rainbow-delimiters-highlight-" delim "s-p")))
    (rainbow-delimiters-propertize-delimiter loc
-                                            depth)))
+                                            depth
+                                            match)))
 
 
 ;;; JIT-Lock functionality
@@ -545,29 +558,39 @@ Used by jit-lock for dynamic highlighting."
           (while (and (< (point) end)
                       (re-search-forward rainbow-delimiters-delim-regex end t))
             (backward-char) ; re-search-forward places point after delim; go 
back.
-            (unless (rainbow-delimiters-char-ineligible-p (point))
-              (let ((delim (char-after (point))))
-                (cond ((eq ?\( delim)       ; (
-                       (setq depth (1+ depth))
-                       (rainbow-delimiters-apply-color "paren" depth (point)))
-                      ((eq ?\) delim)       ; )
-                       (rainbow-delimiters-apply-color "paren" depth (point))
-                       (setq depth (or (and (<= depth 0) 0) ; unmatched paren
-                                       (1- depth))))
-                      ((eq ?\[ delim)       ; [
-                       (setq depth (1+ depth))
-                       (rainbow-delimiters-apply-color "bracket" depth 
(point)))
-                      ((eq ?\] delim)       ; ]
-                       (rainbow-delimiters-apply-color "bracket" depth (point))
-                       (setq depth (or (and (<= depth 0) 0) ; unmatched bracket
-                                       (1- depth))))
-                      ((eq ?\{ delim)       ; {
-                       (setq depth (1+ depth))
-                       (rainbow-delimiters-apply-color "brace" depth (point)))
-                      ((eq ?\} delim)       ; }
-                       (rainbow-delimiters-apply-color "brace" depth (point))
-                       (setq depth (or (and (<= depth 0) 0) ; unmatched brace
-                                       (1- depth)))))))
+            (let ((ppss (rainbow-delimiters-syntax-ppss (point))))
+              (unless (rainbow-delimiters-char-ineligible-p (point) ppss)
+                (let ((delim (char-after (point))))
+                  (cond ((eq ?\( delim)       ; (
+                         (setq depth (1+ depth))
+                         (rainbow-delimiters-apply-color "paren" depth (point) 
t))
+                        ((eq ?\) delim)       ; )
+                         (rainbow-delimiters-apply-color "paren"
+                                                         depth
+                                                         (point)
+                                                         (= ?\( (char-after 
(nth 1 ppss))))
+                         (setq depth (or (and (<= depth 0) 0) ; unmatched paren
+                                         (1- depth))))
+                        ((eq ?\[ delim)       ; [
+                         (setq depth (1+ depth))
+                         (rainbow-delimiters-apply-color "bracket" depth 
(point) t))
+                        ((eq ?\] delim)       ; ]
+                         (rainbow-delimiters-apply-color "bracket"
+                                                         depth
+                                                         (point)
+                                                         (= ?\[ (char-after 
(nth 1 ppss))))
+                         (setq depth (or (and (<= depth 0) 0) ; unmatched 
bracket
+                                         (1- depth))))
+                        ((eq ?\{ delim)       ; {
+                         (setq depth (1+ depth))
+                         (rainbow-delimiters-apply-color "brace" depth (point) 
t))
+                        ((eq ?\} delim)       ; }
+                         (rainbow-delimiters-apply-color "brace"
+                                                         depth
+                                                         (point)
+                                                         (= ?\{ (char-after 
(nth 1 ppss))))
+                         (setq depth (or (and (<= depth 0) 0) ; unmatched brace
+                                         (1- depth))))))))
             ;; move past delimiter so re-search-forward doesn't pick it up 
again
             (forward-char)))))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]