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

From: Antero Mejr <[email-masked]>
Date: Fri, 05 Jan 2024 21:10:10 +0000

Antero Mejr <[email-masked]> writes:

> 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.

Updated version of the last patch, accounts for when
blink-matching-paren-distance is nil.

>From bb48036f39affb549fd71ee417d4cf9b111eaabf 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  | 16 +++++++++++-----
 lisp/simple.el | 17 +++++++++++------
 2 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/lisp/paren.el b/lisp/paren.el
index cb29b8311a5..267289f9273 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
@@ -255,9 +255,14 @@ 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
-	  (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)))
+            ;; 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..473532dc9bd 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -9335,15 +9335,20 @@ The function should return non-nil if the two tokens do not match.")
 	     blink-matching-paren)
     (let* ((oldpos (point))
 	   (message-log-max nil) ; Don't log messages about paren matching.
+           (narrowed nil)
 	   (blinkpos
             (save-excursion
               (save-restriction
 		(syntax-propertize (point))
-                (if blink-matching-paren-distance
-                    (narrow-to-region
-                     (max (minibuffer-prompt-end) ;(point-min) unless minibuf.
-                          (- (point) blink-matching-paren-distance))
-                     oldpos))
+                (when blink-matching-paren-distance
+                  (let ((start (- (point)
+                                  blink-matching-paren-distance)))
+                  ;; If narrowed, the match may be outside the region.
+                  (setq narrowed (< (minibuffer-prompt-end) start))
+                  (narrow-to-region
+                   (max (minibuffer-prompt-end) ;(point-min) unless minibuf.
+                        start)
+                   oldpos)))
                 (let ((parse-sexp-ignore-comments
                        (and parse-sexp-ignore-comments
                             (not blink-matching-paren-dont-ignore-comments))))
@@ -9362,7 +9367,7 @@ The function should return non-nil if the two tokens do not match.")
                     (error nil))))))
            (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