master 5b4d9683f05: Move out of strings/comments before matching in 'show-paren-mode'

Eli Zaretskii <[email protected]>
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit 5b4d9683f05c37e42c84ec3c2a20fa900e6c3a9c
Author: Al Haji-Ali <[email protected]>
Commit: Eli Zaretskii <[email protected]>

    Move out of strings/comments before matching in 'show-paren-mode'
    
    * lisp/paren.el (show-paren--snap): New function.
    (show-paren--default): Call 'show-paren--snap' to move out of
    comments and strings before narrowing and matching.  (Bug#81611)
    
    Copyright-paperwork-exempt: yes
---
 lisp/paren.el | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/lisp/paren.el b/lisp/paren.el
index c2ecdc60c72..0af9d057dea 100644
--- a/lisp/paren.el
+++ b/lisp/paren.el
@@ -249,6 +249,18 @@ if there's no opener/closer near point, or a list of the form
 \(HERE-BEG HERE-END THERE-BEG THERE-END MISMATCH)
 Where HERE-BEG..HERE-END is expected to be near point.")
 
+(defun show-paren--snap (pos)
+  "Return the first position at or after POS that is not mid-string/comment."
+  (save-excursion
+    (let ((ppss (syntax-ppss pos)))
+      (if (not (or (nth 3 ppss) (nth 4 ppss)))
+          pos
+        (condition-case nil
+            (progn (parse-partial-sexp pos (point-max) nil nil
+                                       ppss 'syntax-table)
+                   (point))
+          (error pos))))))
+
 (defun show-paren--default ()
   "Find the opener/closer near point and its match.
 
@@ -272,8 +284,11 @@ It is the default value of `show-paren-data-function'."
       (save-restriction
 	;; Determine the range within which to look for a match.
 	(when blink-matching-paren-distance
-	  (let ((beg (max (point-min)
-	                  (- (point) blink-matching-paren-distance))))
+	  (let ((beg
+                 (min (point)
+                      (show-paren--snap
+                       (max (point-min)
+	                    (- (point) blink-matching-paren-distance))))))
 	    ;; `syntax-propertize' can't widen so make sure it won't
 	    ;; need to (bug#81035).
 	    (syntax-propertize beg)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.