Subject: Re: bug#28229: 26.0.50; outline-minor-mode-hook run twice when reverting buffer
From: Kaushal Modi <[email-masked]>
To: Eli Zaretskii <[email-masked]>
Cc: [email-masked]
Date: Fri, 25 Aug 2017 15:06:16 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

On Fri, Aug 25, 2017 at 9:08 AM Eli Zaretskii <[email-masked]> wrote:

> > From: Kaushal Modi <[email-masked]>
> > Date: Fri, 25 Aug 2017 12:14:23 +0000
> >
> > I recently discovered an anomaly with my outline mode customization and
> it boiled down to this unexplained
> > behavior.. the outline-minor-mode-hook was getting triggered twice on
> doing revert-buffer.
>
> Isn't this expected behavior?  revert-buffer calls normal-mode, which
> first kills all local variables, which involves turning off
> outline-minor-mode by calling
>
>   (outline-minor-mode -1)
>
> And that calls your hook.
>
> The other time is when normal-mode turns outline-minor-mode ON.
>
> The doc string of outline-minor-mode-hook (and any other hook created
> by define-minor-mode) clearly says:
>
>   Hook run after entering or leaving ‘outline-minor-mode’.
>

Hi Eli,

Thanks for reviewing the report. I have now fixed the problem at my end.

Here is an updated MWE to exactly show the problem I was facing:

- I needed to update a local var specific to the major mode.
- The minor mode needed a act a bit differently based on the value of that
local variable.
- The problem was that that local variable was getting wiped out after the
first call of the minor mode hook and it wasn't getting set correctly by
my/text-mode-customization (below example)

=====
(defvar-local my/var 'unknown
  "This var is set to the major mode locally.")

(defun modi/debug ()
  (message "In outline debug")
  (message "buf name: `%S'" (buffer-name))
  (message "major-mode: `%S'" major-mode)
  (message "my/var: `%S'" my/var))
(add-hook 'outline-minor-mode-hook #'modi/debug)
(add-hook 'text-mode-hook #'outline-minor-mode :append)

(defun my/text-mode-customization ()
  (message "In text mode customization")
  (setq-local my/var major-mode))
(remove-hook 'text-mode-hook #'my/text-mode-customization)
(add-hook 'text-mode-hook #'my/text-mode-customization :append) ;Culprit!

(global-set-key (kbd "<f5>") (lambda () (interactive) (revert-buffer)))
=====

On doing C-h v text-mode-hook, I saw:

(outline-minor-mode text-mode-hook-identify my/text-mode-customization)

That explained that the outline-minor-mode's hook ran before
my/text-mode-customization can set the my/var correctly.

The fix was simply to keep the APPEND arg of add-hook at its default value
nil.

The fix was simple but the debug was mind-numbing.. thanks for the help!
-- 

Kaushal Modi