Subject: bug#68234: [PATCH] Increase blink-matching-paren-distance by 300kb.

From: Antero Mejr <[email-masked]>
Date: Fri, 05 Jan 2024 20:50:45 +0000

Eli Zaretskii <[email-masked]> writes:

> I think we can at most double it at this time.  This is a defcustom,
> so people can always increase it in their local configurations if they
> have much faster machines and need routinely to cope with such large
> parenthetical expressions.

If we're not increasing it that much, can we change the behavior to not
wrongly indicate mismatched parens for expressions where you can't be
sure? Patch is attached.

>From a6dcc9007601122e4bef95f4316cd5452e8d6b38 Mon Sep 17 00:00:00 2001
From: Antero Mejr <[email-masked]>
Date: Fri, 5 Jan 2024 20:37:52 +0000
Subject: [PATCH] Don't show mismatched parens if search region is narrowed

* lisp/paren.el (show-paren--default): Don't highlight paren as
mismatched if region is narrowed.
* lisp/simple.el (blink-matching-open): Don't display mismatched paren
message if region is narrowed.  (Bug#68234)
---
 lisp/paren.el  | 18 ++++++++++++------
 lisp/simple.el |  5 ++++-
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/lisp/paren.el b/lisp/paren.el
index cb29b8311a5..dbfd0ab4ffd 100644
--- a/lisp/paren.el
+++ b/lisp/paren.el
@@ -246,7 +246,7 @@ It is the default value of `show-paren-data-function'."
           (if (ppss-comment-depth (syntax-ppss))
               nil
             parse-sexp-ignore-comments))
-	 pos mismatch here-beg here-end)
+	 pos mismatch here-beg here-end narrowed)
     ;;
     ;; Find the other end of the sexp.
     (when dir
@@ -254,10 +254,15 @@ It is the default value of `show-paren-data-function'."
 	    here-end (if (eq dir 1) (1+ outside) outside))
       (save-restriction
 	;; Determine the range within which to look for a match.
-	(when blink-matching-paren-distance
-	  (narrow-to-region
-	   (max (point-min) (- (point) blink-matching-paren-distance))
-	   (min (point-max) (+ (point) blink-matching-paren-distance))))
+        (let ((start (- (point) blink-matching-paren-distance))
+              (end (+ (point) blink-matching-paren-distance)))
+	  (when blink-matching-paren-distance
+            ;; If narrowed, the match may be outside the region.
+            (setq narrowed (or (and (< (point-min) start) (eq dir -1))
+                               (and (> (point-max) end) (eq dir 1))))
+	    (narrow-to-region
+	     (max (point-min) start)
+	     (min (point-max) end))))
 	;; Scan across one sexp within that range.
 	;; Errors or nil mean there is a mismatch.
 	(condition-case ()
@@ -275,7 +280,8 @@ It is the default value of `show-paren-data-function'."
 	;; If found a "matching" paren, see if it is the right
 	;; kind of paren to match the one we started at.
 	(if (not (integerp pos))
-	    (if mismatch (list here-beg here-end nil nil t))
+	    (if (and mismatch (not narrowed))
+                (list here-beg here-end nil nil t))
 	  (let ((beg (min pos outside)) (end (max pos outside)))
 	    (unless (eq (syntax-class (syntax-after beg)) 8)
 	      (setq mismatch
diff --git a/lisp/simple.el b/lisp/simple.el
index a464123f8b5..b3c4f456372 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -9360,9 +9360,12 @@ The function should return non-nil if the two tokens do not match.")
                           (forward-char 1))
                         (point))
                     (error nil))))))
+           ;; If narrowed, the match may be outside the region.
+           (narrowed (< (minibuffer-prompt-end)
+                        (- (point) blink-matching-paren-distance)))
            (mismatch (funcall blink-matching-check-function blinkpos oldpos)))
       (cond
-       (mismatch
+       ((and mismatch (not narrowed))
         (if blinkpos
             (if (minibufferp)
                 (minibuffer-message "Mismatched parentheses")
-- 
2.43.0