help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Defining minor mode with minor-mode condition


From: Joost Kremers
Subject: Re: Defining minor mode with minor-mode condition
Date: Tue, 04 May 2021 09:01:20 +0200
User-agent: mu4e 1.5.12; emacs 27.2

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]