all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Minor mode hook run when mode is deactivated?
@ 2020-10-07 23:15 Joost Kremers
  2020-10-07 23:45 ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Joost Kremers @ 2020-10-07 23:15 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I recently ran into a problem that was apparently caused by a 
minor mode hook being run when the mode is deactivated. 
Counter-intuitive as that may be (to me, at least), a quick look 
at the code for `define-minor-mode` seems to suggest that it's in 
fact correct.

Is there a rationale behind this? I noticed in the code that there 
are actually two more hooks, `<mode>-on-hook` and 
`<mode>-off-hook`, which according to a comment in the code are 
there for backward compatibility only. Which suggests that someone 
at some point thought about these things and made a conscious 
decision to this effect.

Which in turn makes me wonder what the rationale was for that 
decision. :-) Also, I haven't been able to find any info on this 
in the Elisp and Emacs manuals. Aren't minor modes supposed to be 
turned off?

TIA


-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Minor mode hook run when mode is deactivated?
  2020-10-07 23:15 Minor mode hook run when mode is deactivated? Joost Kremers
@ 2020-10-07 23:45 ` Stefan Monnier
  2020-10-08  6:44   ` Joost Kremers
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2020-10-07 23:45 UTC (permalink / raw)
  To: help-gnu-emacs

> I recently ran into a problem that was apparently caused by a minor mode
> hook being run when the mode is deactivated. Counter-intuitive as that may
> be (to me, at least), a quick look at the code for `define-minor-mode` seems
> to suggest that it's in fact correct.

If you need to do something when a minor mode is activated, you most
likely also need to do something to "undo" it when the mode
is deactivated.  So, yes, the minor mode hooks are run both when
activating and when deactivating, and they should check the value of the
minor mode variable to know which it is.


        Stefan




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Minor mode hook run when mode is deactivated?
  2020-10-07 23:45 ` Stefan Monnier
@ 2020-10-08  6:44   ` Joost Kremers
  2020-10-08 13:53     ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Joost Kremers @ 2020-10-08  6:44 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On Thu, Oct 08 2020, Stefan Monnier wrote:
> If you need to do something when a minor mode is activated, you 
> most
> likely also need to do something to "undo" it when the mode
> is deactivated.  So, yes, the minor mode hooks are run both when
> activating and when deactivating, and they should check the 
> value of the
> minor mode variable to know which it is.

That makes sense, but it does mean that you cannot put a minor 
mode onto another minor mode hook.[1]

Which is OK, I was just surprised that I wasn't able to find 
anything about this in the documentation. Is in there and I just 
didn't search well enough, or should it perhaps be added?

Joost


Footnotes:
[1]  That was my scenario. I have a minor mode that I wrote and to 
which I added an extra hook that's run when the mode is 
deactivated. I thought this would allow me to turn off other minor 
modes that I had activated in my minor mode's hook, but it wasn't 
working. Turns out the minor mode hook is also run when the mode 
is deactivated, after my additional deactivate-hook was run... 
Details here 
<https://github.com/joostkremers/writeroom-mode/issues/63> if 
anyone's interested.



-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Minor mode hook run when mode is deactivated?
  2020-10-08  6:44   ` Joost Kremers
@ 2020-10-08 13:53     ` Stefan Monnier
  2020-10-08 14:33       ` Joost Kremers
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2020-10-08 13:53 UTC (permalink / raw)
  To: Joost Kremers; +Cc: help-gnu-emacs

> That makes sense, but it does mean that you cannot put a minor mode onto
> another minor mode hook.[1]

And expect this minor mode to be enabled/disabled along with the
"parent" minor mode?  no, indeed!

> Which is OK, I was just surprised that I wasn't able to find anything about
> this in the documentation.  Is in there and I just didn't search well enough,
> or should it perhaps be added?

`C-h o <foo>-mode-hook RET` should say that it is run both when leaving
and entering.  So either your specific hook failed to mention it, or you
looked in some other part of the doc.  In either case it sounds like you
found a documentation bug, so please report it (it's important then to
say exactly where you looked for that info).


        Stefan




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Minor mode hook run when mode is deactivated?
  2020-10-08 13:53     ` Stefan Monnier
@ 2020-10-08 14:33       ` Joost Kremers
  0 siblings, 0 replies; 5+ messages in thread
From: Joost Kremers @ 2020-10-08 14:33 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On Thu, Oct 08 2020, Stefan Monnier wrote:
>> That makes sense, but it does mean that you cannot put a minor 
>> mode onto
>> another minor mode hook.[1]
>
> And expect this minor mode to be enabled/disabled along with the
> "parent" minor mode?  no, indeed!

No, not at all. :-) I added an extra hook variable that got run in 
the body of `define-minor-mode` when deactivating the "parent" 
mode, which I used to turn off the "child" minor mode. I was just 
surprised that the "child" mode was then immediately reactivated 
again.

>> Which is OK, I was just surprised that I wasn't able to find 
>> anything about
>> this in the documentation.  Is in there and I just didn't 
>> search well enough,
>> or should it perhaps be added?
>
> `C-h o <foo>-mode-hook RET` should say that it is run both when 
> leaving
> and entering.  So either your specific hook failed to mention 
> it, 

No, it's there. I just never bothered to look, because I it's a 
mode that I wrote myself. :-) I didn't realize that the mode hook 
gets a default doc string.

> or you
> looked in some other part of the doc.  In either case it sounds 
> like you
> found a documentation bug, so please report it (it's important 
> then to
> say exactly where you looked for that info).

I'll report it as a documentation bug, then. Thanks for your 
reply.


-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-10-08 14:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-07 23:15 Minor mode hook run when mode is deactivated? Joost Kremers
2020-10-07 23:45 ` Stefan Monnier
2020-10-08  6:44   ` Joost Kremers
2020-10-08 13:53     ` Stefan Monnier
2020-10-08 14:33       ` Joost Kremers

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.