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

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

Re: Adding functionality to a minor mode


From: goncholden
Subject: Re: Adding functionality to a minor mode
Date: Mon, 07 Feb 2022 13:56:52 +0000

------- Original Message -------

On Monday, February 7th, 2022 at 6:00 AM, Eric Abrahamsen 
<eric@ericabrahamsen.net> wrote:

> goncholden goncholden@protonmail.com writes:
>
> > ------ Original Message -------
> >
> > On Saturday, February 5th, 2022 at 6:50 PM, goncholden 
> > goncholden@protonmail.com wrote:
> > > ------- Original Message -------
> > > On Saturday, February 5th, 2022 at 10:00 AM, goncholden via Users
> > > list for the GNU Emacs text editor help-gnu-emacs@gnu.org wrote:
> > >
> > > > I have made a minor-mode with some defvar and defun. Have added some 
> > > > functionality
> > > > for comments, and put everything in the following function definition.
> > > >
> > > > (defun rich-annotation-tools ()
> > > > "Aggregates annotation tools for comments."
> > > > (rich-annotation-font-weight)
> > > > (rich-annotation-low-contrast)
> > > > (rich-annotation-keytrigger))
> > > >
> > > > I would expect to add "(rich-annotation-tools)" in
> > > > define-minor-mode. Would I introduce it
> > > > within the "(when rich-minor-mode" part?
> > > >
> > > > ;;;###autoload
> > > >
> > > > (define-minor-mode rich-minor-mode
> > > >
> > > > "This is the description."
> > > > :lighter "rich" ; indicator in mode-line
> > > > (font-lock-remove-keywords nil rich-font-lock)
> > > >
> > > > (when rich-minor-mode
> > > > (font-lock-add-keywords nil rich-font-lock 'append)
> > > > (set (make-local-variable 'jit-lock-contextually) t) )
> > > > (rich-annotation-tools)
> > > >
> > > > (with-no-warnings (font-lock-fontify-buffer)) )) )
> >
> > Could I get some clarification for using "(when rich-minor-mode" and when 
> > not to?
>
> What's happening here is that, when you define a minor mode, it also
> defines a variable of the same name, which can be tested as a boolean to
> see if the minor mode is currently enabled or not.
>
> The only thing you need to know is that, when the minor mode is turned
> on or off, the variable is set before the body is run. So if you test
> the boolean variable in the body code of the minor mode, it will be t if
> you've just turned the mode on, and nil if you've just turned it off.
>
> For some reason, when I first started playing with minor modes, this
> struck me as backwards. I can no longer say why, exactly, but I remember
> it did.

I am also struck about what happens when "(define-minor-mode rich-minor-mode" is
used to disable the mode.  I suppose that "(when richerenkov-minor-mode" would
evaluate to false, but the other parts would evaluate.

Would you be so kind to assist me a little bit, if you please?

I normally use ultra-bold, but changed comments to use normal weight and 
included the functionality
inside a minor mode like this.

(defun rich-annotation-font-weight ()
  "Makes normal font weight for comments."
  (set-face-attribute 'font-lock-comment-face nil :weight 'normal))

Is there a way that I can remember the :weight used initially,
"(set-face-attribute 'default nil :height 160 :weight 'ultra-bold)"
so I can set comments to ultra-bold again when the minor-mode is disabled.

Then I added some additional functionality and put it here

(defun rich-annotation-tools ()
  "Aggregates annotation tools for comments."
  (rich-annotation-font-weight)
  (rich-annotation-low-contrast)
  (rich-annotation-keytrigger))

Here is my definition of the minor-mode

;;;###autoload
(define-minor-mode rich-minor-mode
  "Colour Brace Marks according to their depth."
  :lighter "rich"  ; indicator in mode-line

  (font-lock-remove-keywords nil rich-font-lock)

  (when rich-minor-mode
    (font-lock-add-keywords nil rich-font-lock 'append)
    (set (make-local-variable 'jit-lock-contextually) t) )

  (rich-annotation-tools)

  (when font-lock-mode
    (if (fboundp 'font-lock-flush)
        (font-lock-flush)
      (with-no-warnings (font-lock-fontify-buffer)) ))

 )

Would it be better to introduce (rich-annotation-tools) in the
"(when rich-minor-mode" part or outside it.

To enable and disable the minor-mode, I have added

;;;###autoload
(defun rich-minor-mode-enable ()
  "Enable `rich-minor-mode'."
  (rich-minor-mode 1))

;;;###autoload
(defun rich-minor-mode-disable ()
  "Disable `rich-minor-mode'."
  (rich-minor-mode 0))










reply via email to

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