Subject: Re: bug#61098: 29.0.60; Confusing behavior of show-paren-mode
From: Dmitry Gutov <[email-masked]>
To: Stefan Monnier <[email-masked]>
Cc: [email-masked], Eli Zaretskii <[email-masked]>, [email-masked]
Date: Sun, 5 Feb 2023 21:09:36 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

On 05/02/2023 20:57, Stefan Monnier via Bug reports for GNU Emacs, the 
Swiss army knife of text editors wrote:
>> +  :variable ( (and show-paren-mode
>> +                   (buffer-match-p show-paren-predicate (current-buffer)))
>> +              .
>>                 (lambda (val) (setq-local show-paren-mode val)))
> This doesn't look quote right because it doesn't pay attention to
> `local-variable-p`.  We should extract the test performed in the timer
> and reuse it here.

Thank you, makes sense.

Here's the patch I installed:

diff --git a/lisp/paren.el b/lisp/paren.el
index 7ee4e9ae682..4c91fd29490 100644
--- a/lisp/paren.el
+++ b/lisp/paren.el
@@ -161,8 +161,9 @@ show-paren--delete-overlays
  ;;;###autoload
  (define-minor-mode show-paren-local-mode
    "Toggle `show-paren-mode' only in this buffer."
-  :variable ( show-paren-mode .
-              (lambda (val) (setq-local show-paren-mode val)))
+  :variable ((show-paren--enabled-p)
+             .
+             (lambda (val) (setq-local show-paren-mode val)))
    (cond
     ((eq show-paren-mode (default-value 'show-paren-mode))
      (unless show-paren-mode
@@ -428,14 +429,17 @@ show-paren--show-context-in-overlay
  ;; `show-paren-delay'.
  (defvar-local show-paren--last-pos nil)

+(defun show-paren--enabled-p ()
+  (and show-paren-mode
+       ;; If we're using `show-paren-local-mode', then
+       ;; always heed the value.
+       (or (local-variable-p 'show-paren-mode)
+           ;; If not, check that the predicate matches.
+           (buffer-match-p show-paren-predicate (current-buffer)))))
+
  (defun show-paren-function ()
    "Highlight the parentheses until the next input arrives."
-  (let ((data (and show-paren-mode
-                   ;; If we're using `show-paren-local-mode', then
-                   ;; always heed the value.
-                   (or (local-variable-p 'show-paren-mode)
-                       ;; If not, check that the predicate matches.
-                       (buffer-match-p show-paren-predicate 
(current-buffer)))
+  (let ((data (and (show-paren--enabled-p)
                     (funcall show-paren-data-function))))
      (if (not data)
          (progn