unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Defining minor mode with minor-mode condition
@ 2021-05-04  3:52 Christopher Dimech
  2021-05-04  4:12 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-04  7:01 ` Joost Kremers
  0 siblings, 2 replies; 7+ messages in thread
From: Christopher Dimech @ 2021-05-04  3:52 UTC (permalink / raw)
  To: Help Gnu Emacs

Is it valid to define a minor mode "crucibulum-minor-mode" and then
have a "when" condition using crucibulum-minor-mode inside of it?

Regards
Christopher

;;;###autoload
(define-minor-mode crucibulum-minor-mode
  "Minor mode for assisting with superior & inferior typeface."
  :init-value nil
  :global nil
  :lighter " Crucibulum"

  (when crucibulum-minor-mode
    (enable-texcom-typeface)
    (enable-ricci-notation)) )


---------------------
Christopher Dimech
General Administrator - Naiad Informatics - GNU Project (Geocomputation)
- Geophysical Simulation
- Geological Subsurface Mapping
- Disaster Preparedness and Mitigation
- Natural Resource Exploration and Production
- Free Software Advocacy




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

* Re: Defining minor mode with minor-mode condition
  2021-05-04  3:52 Defining minor mode with minor-mode condition Christopher Dimech
@ 2021-05-04  4:12 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-04  7:01 ` Joost Kremers
  1 sibling, 0 replies; 7+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-04  4:12 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Dimech wrote:

> Is it valid to define a minor mode "crucibulum-minor-mode"
> and then have a "when" condition using crucibulum-minor-mode
> inside of it?

Yes, you can have an `if' also if you need to disable stuff
when it is disabled...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Defining minor mode with minor-mode condition
  2021-05-04  3:52 Defining minor mode with minor-mode condition Christopher Dimech
  2021-05-04  4:12 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-04  7:01 ` Joost Kremers
  2021-05-04  7:20   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-04 12:52   ` Stefan Monnier
  1 sibling, 2 replies; 7+ messages in thread
From: Joost Kremers @ 2021-05-04  7:01 UTC (permalink / raw)
  To: Christopher Dimech; +Cc: help-gnu-emacs


On Tue, May 04 2021, Christopher Dimech wrote:
> Is it valid to define a minor mode "crucibulum-minor-mode" and then
> have a "when" condition using crucibulum-minor-mode inside of it?
>
> Regards
> Christopher
>
> ;;;###autoload
> (define-minor-mode crucibulum-minor-mode
>   "Minor mode for assisting with superior & inferior typeface."
>   :init-value nil
>   :global nil
>   :lighter " Crucibulum"
>
>   (when crucibulum-minor-mode
>     (enable-texcom-typeface)
>     (enable-ricci-notation)) )

Yes, AFAIK it's the normal way of doing this. Note that what you're defining
with `define-minor-mode` is a function with the name `crucibulum-minor-mode`
*plus* a variable of the same name.

When executed, the function `crucibulum-minor-mode` runs the code in the body of
the `define-minor-mode`, but before that, it does some other stuff, among which
is setting the variable `crucibulum-minor-mode`. So that's why this code works.

Note that when `define-minor-mode` is executed, it does *not* run the code in
its body. It just uses it to construct the function `crucibulum-minor-mode`.

Note also that the normal form is not with a `when` but with an `if`, since
normally you'll also want to add code to disable the minor mode. So it would
look something like:

```
;;;###autoload
(define-minor-mode crucibulum-minor-mode
  "Minor mode for assisting with superior & inferior typeface."
  :init-value nil
  :global nil
  :lighter " Crucibulum"
  
  (if crucibulum-minor-mode
      (progn
        (enable-texcom-typeface)
        (enable-ricci-notation))
    (disable-texcom-typeface)
    (disable-ricci-notation)))
```



-- 
Joost Kremers
Life has its moments



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

* Re: Defining minor mode with minor-mode condition
  2021-05-04  7:01 ` Joost Kremers
@ 2021-05-04  7:20   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-04 12:52   ` Stefan Monnier
  1 sibling, 0 replies; 7+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-04  7:20 UTC (permalink / raw)
  To: help-gnu-emacs

Joost Kremers wrote:

> Note also that the normal form is not with a `when` but with
> an `if`, since normally you'll also want to add code to
> disable the minor mode

Right, the minor mode itself is actually disabled even without
it including the keymap and possibly some other things but yes
whatever isn't reset or deallocated or whatever should be put
there...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Defining minor mode with minor-mode condition
  2021-05-04  7:01 ` Joost Kremers
  2021-05-04  7:20   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-04 12:52   ` Stefan Monnier
  2021-05-04 13:01     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-04 13:03     ` Christopher Dimech
  1 sibling, 2 replies; 7+ messages in thread
From: Stefan Monnier @ 2021-05-04 12:52 UTC (permalink / raw)
  To: help-gnu-emacs

> ;;;###autoload
> (define-minor-mode crucibulum-minor-mode
>   "Minor mode for assisting with superior & inferior typeface."
>   :init-value nil
>   :global nil
>   :lighter " Crucibulum"
>   
>   (if crucibulum-minor-mode
>       (progn
>         (enable-texcom-typeface)
>         (enable-ricci-notation))
>     (disable-texcom-typeface)
>     (disable-ricci-notation)))

[ Of course those `:foo nil` keyword arguments are redundant.  ]
I actually often find it convenient to unconditionally "disable" first:

    (define-minor-mode crucibulum-minor-mode
      "Minor mode for assisting with superior & inferior typeface."
      :lighter " Crucibulum"
      (disable-texcom-typeface)
      (disable-ricci-notation)
      (when crucibulum-minor-mode
        (enable-texcom-typeface)
        (enable-ricci-notation)))

the advantage is to avoid problems where, say, `enable-texcom-typeface`
is not idempotent, so if you call (crucibulum-minor-mode 1) several times
you might end up with multiple copies of the same thing added to a list.


        Stefan




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

* Re: Defining minor mode with minor-mode condition
  2021-05-04 12:52   ` Stefan Monnier
@ 2021-05-04 13:01     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-05-04 13:03     ` Christopher Dimech
  1 sibling, 0 replies; 7+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-04 13:01 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier wrote:

> I actually often find it convenient to unconditionally
> "disable" first:
>
>     (define-minor-mode crucibulum-minor-mode
>       "Minor mode for assisting with superior & inferior typeface."
>       :lighter " Crucibulum"
>       (disable-texcom-typeface)
>       (disable-ricci-notation)
>       (when crucibulum-minor-mode
>         (enable-texcom-typeface)
>         (enable-ricci-notation)))
>
> the advantage is to avoid problems where, say,
> `enable-texcom-typeface` is not idempotent, so if you call
> (crucibulum-minor-mode 1) several times you might end up
> with multiple copies of the same thing added to a list.

Good!

What about one function to enable all and one to disable all?

No - then these functions can be called from the outside which
will/can add confusion, and worse, enable some of the minor
mode functionality but not actually enabling the minor
mode itself!

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Defining minor mode with minor-mode condition
  2021-05-04 12:52   ` Stefan Monnier
  2021-05-04 13:01     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-05-04 13:03     ` Christopher Dimech
  1 sibling, 0 replies; 7+ messages in thread
From: Christopher Dimech @ 2021-05-04 13:03 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

> Sent: Wednesday, May 05, 2021 at 12:52 AM
> From: "Stefan Monnier" <monnier@iro.umontreal.ca>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Defining minor mode with minor-mode condition
>
> > ;;;###autoload
> > (define-minor-mode crucibulum-minor-mode
> >   "Minor mode for assisting with superior & inferior typeface."
> >   :init-value nil
> >   :global nil
> >   :lighter " Crucibulum"
> >
> >   (if crucibulum-minor-mode
> >       (progn
> >         (enable-texcom-typeface)
> >         (enable-ricci-notation))
> >     (disable-texcom-typeface)
> >     (disable-ricci-notation)))
>
> [ Of course those `:foo nil` keyword arguments are redundant.  ]
> I actually often find it convenient to unconditionally "disable" first:
>
>     (define-minor-mode crucibulum-minor-mode
>       "Minor mode for assisting with superior & inferior typeface."
>       :lighter " Crucibulum"
>       (disable-texcom-typeface)
>       (disable-ricci-notation)
>       (when crucibulum-minor-mode
>         (enable-texcom-typeface)
>         (enable-ricci-notation)))
>
> the advantage is to avoid problems where, say, `enable-texcom-typeface`
> is not idempotent, so if you call (crucibulum-minor-mode 1) several times
> you might end up with multiple copies of the same thing added to a list.
> - Stefan

You make a very good point

>
>
>



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

end of thread, other threads:[~2021-05-04 13:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-04  3:52 Defining minor mode with minor-mode condition Christopher Dimech
2021-05-04  4:12 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-04  7:01 ` Joost Kremers
2021-05-04  7:20   ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-04 12:52   ` Stefan Monnier
2021-05-04 13:01     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-05-04 13:03     ` Christopher Dimech

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).