Subject: bug#61098: 29.0.60; Confusing behavior of show-paren-mode

From: Stefan Monnier <[email-masked]>
Date: Sun, 05 Feb 2023 10:19:24 -0500

> I guess I don't understand what show-paren-local-mode does, then.  Let
> me explain my line of reasoning:
>
>   (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)))
>     (cond
>      ((eq show-paren-mode (default-value 'show-paren-mode))
>       (unless show-paren-mode
> 	(show-paren--delete-overlays))
>       (kill-local-variable 'show-paren-mode))
>      ((not (default-value 'show-paren-mode))
>       ;; Locally enabled, but globally disabled.
>       (show-paren-mode 1)                ; Setup the timer.
>       (setq-default show-paren-mode nil) ; But keep it globally disabled.
>       )
>      (t ;; Locally disabled only.
>       (show-paren--delete-overlays))))
>
> When I looked at the above function and stepped through it in the OP's
> scenario, what I saw was that the 2nd condition of 'cond' doesn't
> fire, because the default-value of show-paren-mode is t nowadays.  So
> I concluded that this code relies on the mode being globally off by
> default, and attempted to fix the condition that turns the mode on
> locally even though it is on globally.

But if the mode is already globally enabled, the (setq-default show-paren-mode nil)
will definitely not be right: you'd end up disabling it globally (while
maybe keeping it enabled locally).

> The predicate used by the timer requires either a match of the buffer
> (which doesn't happen in this case) or that the local value of
> show-paren-mode is non-nil.  Setting the local value non-nil is the
> job of the mode function, isn't it?

Yes, but the first branch of the `cond` incorrectly does
`kill-local-variable` under the assumption that the local value doesn't
matter if it's the same as the global one, but that's not true here
because (as a wise man said):

   The predicate used by the timer requires either a match of the buffer
   (which doesn't happen in this case) or that the local value of
   show-paren-mode is non-nil.

-- Stefan