bug#81611: 31.0.90; show-paren-mode reports false mismatch

Al Haji-Ali <[email protected]>
Newsgroups gmane.emacs.bugs
Message-ID <[email protected]>
On 12/08/2026, Stefan Monnier wrote:

>> Suggested fix: Before narrowing, check syntax-ppss at the cutoff; if it
>> is inside a string or comment, don't cut there -- but snap forward (or
>> backward) to a position outside strings/comments.
>
> +1
>
> And I believe the "snap forward" can be done by `parse-partial-sexp` with
> COMMENTSTOP arg of `syntax-table`.  The snap backward is simpler (just
> jump to (nth 8 ppss)), but could jump very far back if the
> string/comment is very long.

Something like the attached?

-- Al
0001-Snap-out-of-strings-comments-before-paren-matching.patch (text/x-patch, 1.8 KB)
From cb7e423bc9add56bee0003c0c5c3744da8186110 Mon Sep 17 00:00:00 2001
From: Al Haji-Ali <[email protected]>
Date: Fri, 14 Aug 2026 15:47:39 +0100
Subject: [PATCH] Snap out of strings/comments before paren matching

* lisp/paren.el (show-paren--snap): New function
(show-paren--default): Call new function before narrowing.
---
 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 @@ show-paren-data-function
 \(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 @@ show-paren--default
       (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)
-- 
2.50.1 (Apple Git-155)
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.