all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Functions which are mode dependent
@ 2024-08-18 21:14 Heime
  2024-08-20 21:49 ` Heime
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Heime @ 2024-08-18 21:14 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

I need some help understanding how to automatically enable some functionality
when loading a file with some major mode.

For instance I have a function that sets 


(defun expr-elisp ()

  (setq imenu-generic-expression
    `( ("defun" ,(concat "^\\s-*"
                   "(\\(defun\\|define-minor-mode\\)\\s-+"
                   "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )


(defun expr-selector ()

  (cond
    ((eq major-mode 'emacs-lisp-mode)  (expr-elisp))
    (t  (expr-generic)) ))

I have put the code above in a minor mode. 

;;;###autoload
(define-minor-mode tema-minor-mode

  (if tema-minor-mode
        (expr-selector)
      (message "Tema Deactivated")))

with hooks to load the minor mode automatically for specific major modes.

(add-hook emacs-lisp-mode-hook #'tema-minor-mode)





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

* Re: Functions which are mode dependent
  2024-08-18 21:14 Functions which are mode dependent Heime
@ 2024-08-20 21:49 ` Heime
  2024-08-22 13:26   ` Heime
  2024-08-23 10:05   ` Joel Reicher
  2024-08-22 18:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2024-08-22 18:58 ` Yuri Khan
  2 siblings, 2 replies; 16+ messages in thread
From: Heime @ 2024-08-20 21:49 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Monday, August 19th, 2024 at 9:14 AM, Heime <heimeborgia@protonmail.com> wrote:

> I need some help understanding how to automatically enable some functionality
> when loading a file with some major mode.
>
> For instance I have a function that sets
>
>
> (defun expr-elisp ()
>
> (setq imenu-generic-expression
> `( ("defun" ,(concat "^\\s-*"
> "(\\(defun\\|define-minor-mode\\)\\s-+"
> "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )
>
>
> (defun expr-selector ()
>
> (cond
> ((eq major-mode 'emacs-lisp-mode) (expr-elisp))
> (t (expr-generic)) ))
>
> I have put the code above in a minor mode.
>
> ;;;###autoload
> (define-minor-mode tema-minor-mode
>
> (if tema-minor-mode
> (expr-selector)
> (message "Tema Deactivated")))
>
> with hooks to load the minor mode automatically for specific major modes.
>
> (add-hook emacs-lisp-mode-hook #'tema-minor-mode)

My confusion is whether after making the minor-mode hooked to emacs-lisp-mode,
should I also make a hook to the function as well ?

That is

(add-hook emacs-lisp-mode-hook #'expr-selector)

Is this check the correct way to apply the correct expr-elisp 
when emacs-lisp-mode is activated when I load an elisp file ?

(eq major-mode 'emacs-lisp-mode) (expr-elisp)




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

* Re: Functions which are mode dependent
  2024-08-20 21:49 ` Heime
@ 2024-08-22 13:26   ` Heime
  2024-08-23 10:05   ` Joel Reicher
  1 sibling, 0 replies; 16+ messages in thread
From: Heime @ 2024-08-22 13:26 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor


On Wednesday, August 21st, 2024 at 9:49 AM, Heime <heimeborgia@protonmail.com> wrote:

> On Monday, August 19th, 2024 at 9:14 AM, Heime heimeborgia@protonmail.com wrote:
> 
> > I need some help understanding how to automatically enable some functionality
> > when loading a file with some major mode.
> > 
> > For instance I have a function that sets
> > 
> > (defun expr-elisp ()
> > 
> > (setq imenu-generic-expression
> > `( ("defun" ,(concat "^\\s-*"
> > "(\\(defun\\|define-minor-mode\\)\\s-+"
> > "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )
> > 
> > (defun expr-selector ()
> > 
> > (cond
> > ((eq major-mode 'emacs-lisp-mode) (expr-elisp))
> > (t (expr-generic)) ))
> > 
> > I have put the code above in a minor mode.
> > 
> > ;;;###autoload
> > (define-minor-mode tema-minor-mode
> > 
> > (if tema-minor-mode
> > (expr-selector)
> > (message "Tema Deactivated")))
> > 
> > with hooks to load the minor mode automatically for specific major modes.
> > 
> > (add-hook emacs-lisp-mode-hook #'tema-minor-mode)
> 
> 
> My confusion is whether after making the minor-mode hooked to emacs-lisp-mode,
> should I also make a hook to the function as well ?
> 
> That is
> 
> (add-hook emacs-lisp-mode-hook #'expr-selector)
> 
> Is this check the correct way to apply the correct expr-elisp
> when emacs-lisp-mode is activated when I load an elisp file ?
> 
> (eq major-mode 'emacs-lisp-mode) (expr-elisp)

Are there any minor-modes that I can study that do a similar thing ?



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

* Re: Functions which are mode dependent
  2024-08-18 21:14 Functions which are mode dependent Heime
  2024-08-20 21:49 ` Heime
@ 2024-08-22 18:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2024-08-22 18:42   ` Heime
  2024-08-22 18:58 ` Yuri Khan
  2 siblings, 1 reply; 16+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-08-22 18:29 UTC (permalink / raw)
  To: help-gnu-emacs

> (defun expr-elisp ()
>   (setq imenu-generic-expression
>     `( ("defun" ,(concat "^\\s-*"
>                    "(\\(defun\\|define-minor-mode\\)\\s-+"
>                    "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )
>
>
> (defun expr-selector ()
>   (cond
>     ((eq major-mode 'emacs-lisp-mode)  (expr-elisp))
>     (t  (expr-generic)) ))
>
> I have put the code above in a minor mode. 
>
> ;;;###autoload
> (define-minor-mode tema-minor-mode
>   (if tema-minor-mode
>         (expr-selector)
>       (message "Tema Deactivated")))

This `message` is a lie because you do not re-set
`imenu-generic-expression` to its original value.


        Stefan




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

* Re: Functions which are mode dependent
  2024-08-22 18:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-08-22 18:42   ` Heime
  0 siblings, 0 replies; 16+ messages in thread
From: Heime @ 2024-08-22 18:42 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs






Sent with Proton Mail secure email.

On Friday, August 23rd, 2024 at 6:29 AM, Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> > (defun expr-elisp ()
> > (setq imenu-generic-expression
> > `( ("defun" ,(concat "^\\s-*"
> > "(\\(defun\\|define-minor-mode\\)\\s-+"
> > "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )
> > 
> > (defun expr-selector ()
> > (cond
> > ((eq major-mode 'emacs-lisp-mode) (expr-elisp))
> > (t (expr-generic)) ))
> > 
> > I have put the code above in a minor mode.
> > 
> > ;;;###autoload
> > (define-minor-mode tema-minor-mode
> > (if tema-minor-mode
> > (expr-selector)
> > (message "Tema Deactivated")))
> 
> 
> This `message` is a lie because you do not re-set
> `imenu-generic-expression` to its original value. - Stefan

You are correct.  I am using an "if" clause, and I had to write something.
But my difficulty is how to make a function that is major-mode dependent
and gets activated when a file is loaded.  So if I view an elisp file,
expr-selector uses the appropriate expr-elisp, and for sh-mode call expr-selector
which will use the corresponding expr-sh.






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

* Re: Functions which are mode dependent
  2024-08-18 21:14 Functions which are mode dependent Heime
  2024-08-20 21:49 ` Heime
  2024-08-22 18:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-08-22 18:58 ` Yuri Khan
  2024-08-22 22:07   ` Heime
  2 siblings, 1 reply; 16+ messages in thread
From: Yuri Khan @ 2024-08-22 18:58 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Mon, 19 Aug 2024 at 04:15, Heime <heimeborgia@protonmail.com> wrote:
>
> I need some help understanding how to automatically enable some functionality
> when loading a file with some major mode.

The major mode you’re customizing has a hook. As an end user, you add
your own function to that hook, and enable the functionality there.

(As a package developer, you’d make your functionality use some
buffer-local variables. Your users would add their own functions to
mode hooks where they would set their own variable values and then
call your function or enable your minor mode.)

> For instance I have a function that sets
>
>
> (defun expr-elisp ()
>
>   (setq imenu-generic-expression
>     `( ("defun" ,(concat "^\\s-*"
>                    "(\\(defun\\|define-minor-mode\\)\\s-+"
>                    "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )
>
>
> (defun expr-selector ()
>
>   (cond
>     ((eq major-mode 'emacs-lisp-mode)  (expr-elisp))
>     (t  (expr-generic)) ))

Having to compare ‘major-mode’ against a symbol is a sign you’re doing
something wrong, for several reasons.

* A major mode can inherit from another major mode. In that case, the
right thing is for your customization to work automatically for the
derived mode as well as the base mode. However, comparing for equality
only handles the base mode.

* To extend your customization to modes not originally in your list,
you’d have to modify your ‘expr-selector’ function.

> I have put the code above in a minor mode.
>
> ;;;###autoload
> (define-minor-mode tema-minor-mode
>
>   (if tema-minor-mode
>         (expr-selector)
>       (message "Tema Deactivated")))
>
> with hooks to load the minor mode automatically for specific major modes.

As told by Stefan, your minor mode is pointless because it does
nothing when turned off.

> (add-hook emacs-lisp-mode-hook #'tema-minor-mode)

If all you want is to set ‘imenu-generic-expression’ to that list for
buffers using ‘emacs-lisp-mode’ or any major mode derived from that,
and to another list for buffers in ‘sh-mode’ or any major modes
derived from that, all you need to do is:

    (defun expr-elisp ()
      (setq imenu-generic-expression
            `( ("defun" ,(concat "^\\s-*"
                                 "(\\(defun\\|define-minor-mode\\)\\s-+"
                                 "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )

    (add-hook emacs-lisp-mode-hook #'expr-elisp)

    (defun expr-sh ()
      (setq imenu-generic-expression
            `( …something else… )))

    (add-hook sh-mode-hook #'expr-sh)

You do not need a minor mode for that.

(Also, your private functions should be named starting with a short
string reasonably unique to you, so that it does not collide with any
public packages you could install.)



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

* Re: Functions which are mode dependent
  2024-08-22 18:58 ` Yuri Khan
@ 2024-08-22 22:07   ` Heime
  2024-08-22 22:16     ` Heime
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2024-08-22 22:07 UTC (permalink / raw)
  To: Yuri Khan; +Cc: Heime via Users list for the GNU Emacs text editor

On Friday, August 23rd, 2024 at 6:58 AM, Yuri Khan <yuri.v.khan@gmail.com> wrote:

> On Mon, 19 Aug 2024 at 04:15, Heime heimeborgia@protonmail.com wrote:
>
> > I need some help understanding how to automatically enable some functionality
> > when loading a file with some major mode.
>
>
> The major mode you’re customizing has a hook. As an end user, you add
> your own function to that hook, and enable the functionality there.
>
> (As a package developer, you’d make your functionality use some
> buffer-local variables. Your users would add their own functions to
> mode hooks where they would set their own variable values and then
> call your function or enable your minor mode.)

How does one use the buffer local variable ?  Is it to save the 
regexp which I sot immediately to imenu-generic-expression ?

How would one do it ?

> > For instance I have a function that sets
> >
> > (defun expr-elisp ()
> >
> > (setq imenu-generic-expression
> > `( ("defun" ,(concat "^\\s-*"
> > "(\\(defun\\|define-minor-mode\\)\\s-+"
> > "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )
> >
> > (defun expr-selector ()
> >
> > (cond
> > ((eq major-mode 'emacs-lisp-mode) (expr-elisp))
> > (t (expr-generic)) ))
>
>
> Having to compare ‘major-mode’ against a symbol is a sign you’re doing
> something wrong, for several reasons.

This is the first time doing such a thing, and came up with it.  I had
a hunch it was not the proper way, so I asked for some feedback to make
it good, with this help list.

> * A major mode can inherit from another major mode. In that case, the
> right thing is for your customization to work automatically for the
> derived mode as well as the base mode. However, comparing for equality
> only handles the base mode.

What is one to use to handle an elisp derived mode for my case ?
Still with a mode hook ?

> * To extend your customization to modes not originally in your list,
> you’d have to modify your ‘expr-selector’ function.
>
> > I have put the code above in a minor mode.
> >
> > ;;;###autoload
> > (define-minor-mode tema-minor-mode
> >
> > (if tema-minor-mode
> > (expr-selector)
> > (message "Tema Deactivated")))
> >
> > with hooks to load the minor mode automatically for specific major modes.
>
> As told by Stefan, your minor mode is pointless because it does
> nothing when turned off.

For now I only want to make the functionality work, and concentrate
upon that first.  

> > (add-hook emacs-lisp-mode-hook #'tema-minor-mode)

> If all you want is to set ‘imenu-generic-expression’ to that list for
> buffers using ‘emacs-lisp-mode’ or any major mode derived from that,
> and to another list for buffers in ‘sh-mode’ or any major modes
> derived from that, all you need to do is:
>
> (defun expr-elisp ()
> (setq imenu-generic-expression
> `( ("defun" ,(concat "^\\\\s-*" "(\\\\(defun\\\\|define-minor-mode\\\\)\\\\s-+" "\\\\(\\\\(\\\\sw\\\\|\\\\s_\\\\)+\\\\)") 2) )) ) (add-hook emacs-lisp-mode-hook #'expr-elisp) (defun expr-sh () (setq imenu-generic-expression` ( …something else… )))
>
> (add-hook sh-mode-hook #'expr-sh)
>
> You do not need a minor mode for that.

I know about using hook with a function such as (add-hook sh-mode-hook #'expr-sh)

But I want to work on making a minor-mode.  Firstly because I have some
other functionality to add to it. Secondly to see how to do a minor-mode
that in also major mode dependent.

> (Also, your private functions should be named starting with a short
> string reasonably unique to you, so that it does not collide with any
> public packages you could install.)

I have no problem with doing so.



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

* Re: Functions which are mode dependent
  2024-08-22 22:07   ` Heime
@ 2024-08-22 22:16     ` Heime
  2024-08-22 23:38       ` Heime
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2024-08-22 22:16 UTC (permalink / raw)
  To: Heime; +Cc: Yuri Khan, Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

On Friday, August 23rd, 2024 at 10:07 AM, Heime <heimeborgia@protonmail.com> wrote:

> On Friday, August 23rd, 2024 at 6:58 AM, Yuri Khan yuri.v.khan@gmail.com wrote:
> 
> > On Mon, 19 Aug 2024 at 04:15, Heime heimeborgia@protonmail.com wrote:
> > 
> > > I need some help understanding how to automatically enable some functionality
> > > when loading a file with some major mode.
> > 
> > The major mode you’re customizing has a hook. As an end user, you add
> > your own function to that hook, and enable the functionality there.
> > 
> > (As a package developer, you’d make your functionality use some
> > buffer-local variables. Your users would add their own functions to
> > mode hooks where they would set their own variable values and then
> > call your function or enable your minor mode.)
> 
> 
> How does one use the buffer local variable ? Is it to save the
> regexp which I sot immediately to imenu-generic-expression ?
> 
> How would one do it ?

There is this possibility. 

(defun tema-expr-elisp ()
  "Set `imenu-generic-expression` to match custom headings in the current buffer."
  (make-local-variable 'imenu-generic-expression)
  (setq imenu-generic-expression
        `(("DEFUN"
          ,(concat "^\\s-*"
                   "(\\(defun\\|define-minor-mode\\)\\s-+"
                   "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )))
 
> > > For instance I have a function that sets
> > > 
> > > (defun expr-elisp ()
> > > 
> > > (setq imenu-generic-expression
> > > `( ("defun" ,(concat "^\\s-*"
> > > "(\\(defun\\|define-minor-mode\\)\\s-+"
> > > "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) )
> > > 
> > > (defun expr-selector ()
> > > 
> > > (cond
> > > ((eq major-mode 'emacs-lisp-mode) (expr-elisp))
> > > (t (expr-generic)) ))
> > 
> > Having to compare ‘major-mode’ against a symbol is a sign you’re doing
> > something wrong, for several reasons.
> 
> 
> This is the first time doing such a thing, and came up with it. I had
> a hunch it was not the proper way, so I asked for some feedback to make
> it good, with this help list.
> 
> > * A major mode can inherit from another major mode. In that case, the
> > right thing is for your customization to work automatically for the
> > derived mode as well as the base mode. However, comparing for equality
> > only handles the base mode.
> 
> 
> What is one to use to handle an elisp derived mode for my case ?
> Still with a mode hook ?
> 
> > * To extend your customization to modes not originally in your list,
> > you’d have to modify your ‘expr-selector’ function.
> > 
> > > I have put the code above in a minor mode.
> > > 
> > > ;;;###autoload
> > > (define-minor-mode tema-minor-mode
> > > 
> > > (if tema-minor-mode
> > > (expr-selector)
> > > (message "Tema Deactivated")))
> > > 
> > > with hooks to load the minor mode automatically for specific major modes.
> > 
> > As told by Stefan, your minor mode is pointless because it does
> > nothing when turned off.
> 
> 
> For now I only want to make the functionality work, and concentrate
> upon that first.
> 
> > > (add-hook emacs-lisp-mode-hook #'tema-minor-mode)
> 
> > If all you want is to set ‘imenu-generic-expression’ to that list for
> > buffers using ‘emacs-lisp-mode’ or any major mode derived from that,
> > and to another list for buffers in ‘sh-mode’ or any major modes
> > derived from that, all you need to do is:
> > 
> > (defun expr-elisp ()
> > (setq imenu-generic-expression
> > `( ("defun" ,(concat "^\\\\\\\\s-*" "(\\\\\\\\(defun\\\\\\\\|define-minor-mode\\\\\\\\)\\\\\\\\s-+" "\\\\\\\\(\\\\\\\\(\\\\\\\\sw\\\\\\\\|\\\\\\\\s_\\\\\\\\)+\\\\\\\\)") 2) )) ) (add-hook emacs-lisp-mode-hook #'expr-elisp) (defun expr-sh () (setq imenu-generic-expression` ( …something else… )))
> > 
> > (add-hook sh-mode-hook #'expr-sh)
> > 
> > You do not need a minor mode for that.
> 
> 
> I know about using hook with a function such as (add-hook sh-mode-hook #'expr-sh)
> 
> But I want to work on making a minor-mode. Firstly because I have some
> other functionality to add to it. Secondly to see how to do a minor-mode
> that in also major mode dependent.
> 
> > (Also, your private functions should be named starting with a short
> > string reasonably unique to you, so that it does not collide with any
> > public packages you could install.)
> 
> 
> I have no problem with doing so.



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

* Re: Functions which are mode dependent
  2024-08-22 22:16     ` Heime
@ 2024-08-22 23:38       ` Heime
  2024-08-24  9:46         ` Heime
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2024-08-22 23:38 UTC (permalink / raw)
  To: Heime; +Cc: Yuri Khan, Heime via Users list for the GNU Emacs text editor


On Friday, August 23rd, 2024 at 10:16 AM, Heime <heimeborgia@protonmail.com> wrote:

> Sent with Proton Mail secure email.
>
> On Friday, August 23rd, 2024 at 10:07 AM, Heime heimeborgia@protonmail.com wrote:
> 
> > On Friday, August 23rd, 2024 at 6:58 AM, Yuri Khan yuri.v.khan@gmail.com wrote:
> > 
> > > On Mon, 19 Aug 2024 at 04:15, Heime heimeborgia@protonmail.com wrote:
> > > 
> > > > I need some help understanding how to automatically enable some functionality
> > > > when loading a file with some major mode.
> > > 
> > > The major mode you’re customizing has a hook. As an end user, you add
> > > your own function to that hook, and enable the functionality there.
> > > 
> > > (As a package developer, you’d make your functionality use some
> > > buffer-local variables. Your users would add their own functions to
> > > mode hooks where they would set their own variable values and then
> > > call your function or enable your minor mode.)

Something as below ?

(defvar tema-expr-elisp
  '(("Functions" "^\\s-*(defun\\s-+\\(\\_<.*?\\_>\\)" 1)))

(defun tema-imenu-expr-elisp ()
  "Set up a custom imenu expression for the current buffer."
  (setq-local imenu-generic-expression tema-expr-elisp))

;;;###autoload
(define-minor-mode tema-minor-mode
  "DESC."
  :init-value nil
  :lighter " Tema"

  (if tema-minor-mode
      (progn
        (add-hook 'sh-mode-hook #'tema-imenu-expr-sh)
        (add-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp))
    (remove-hook 'sh-mode-hook #'tema-imenu-expr-sh)
    (remove-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp)))

And adding more variables and functions for other major modes.

How can I handle derived modes ?





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

* Re: Functions which are mode dependent
  2024-08-20 21:49 ` Heime
  2024-08-22 13:26   ` Heime
@ 2024-08-23 10:05   ` Joel Reicher
  2024-08-23 12:26     ` Heime
  1 sibling, 1 reply; 16+ messages in thread
From: Joel Reicher @ 2024-08-23 10:05 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

Heime <heimeborgia@protonmail.com> writes:

> My confusion is whether after making the minor-mode hooked to 
> emacs-lisp-mode, should I also make a hook to the function as 
> well ?

In case it needs to be said, the purpose of a minor mode (and any 
mode, in fact) is to be able to change it while remaining in the 
same buffer, without modifying the buffer.

In other words, you might switch a minor mode on or off while 
staying in the buffer. Or you might change major modes while 
staying in the buffer.

But if what you want is for some code to always execute when you 
enter a particular major mode, that code is not a minor mode at 
all. It's just code that goes in the major mode's hook.

Conversely, if you want the user to be able to activate and 
deactivate your minor mode at will, then you can't tie it to a 
major mode at all. By definition of what you're offering, that 
connection is up to the user.

Cheers,

        - Joel



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

* Re: Functions which are mode dependent
  2024-08-23 10:05   ` Joel Reicher
@ 2024-08-23 12:26     ` Heime
  0 siblings, 0 replies; 16+ messages in thread
From: Heime @ 2024-08-23 12:26 UTC (permalink / raw)
  To: Joel Reicher; +Cc: Heime via Users list for the GNU Emacs text editor


On Friday, August 23rd, 2024 at 10:05 PM, Joel Reicher <joel.reicher@gmail.com> wrote:

> Heime heimeborgia@protonmail.com writes:
> 
> > My confusion is whether after making the minor-mode hooked to
> > emacs-lisp-mode, should I also make a hook to the function as
> > well ?
> 
> 
> In case it needs to be said, the purpose of a minor mode (and any
> mode, in fact) is to be able to change it while remaining in the
> same buffer, without modifying the buffer.
> 
> In other words, you might switch a minor mode on or off while
> staying in the buffer. Or you might change major modes while
> staying in the buffer.
> 
> But if what you want is for some code to always execute when you
> enter a particular major mode, that code is not a minor mode at
> all. It's just code that goes in the major mode's hook.

That is not what I want.  I want that when the minor-mode gets activated
only the correct regex expression specific to the buffer major-mode is used.

Thus for a buffer in emacs-lisp-mode, the variable tema-expr-elisp
is used to set imenu-generic-expression, rather than the possibilities
for buffers in other major-modes.
 
> Conversely, if you want the user to be able to activate and
> deactivate your minor mode at will, then you can't tie it to a
> major mode at all. By definition of what you're offering, that
> connection is up to the user.
> 
> Cheers,
> 
> - Joel



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

* Re: Functions which are mode dependent
  2024-08-22 23:38       ` Heime
@ 2024-08-24  9:46         ` Heime
  2024-08-24 10:20           ` Yuri Khan
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2024-08-24  9:46 UTC (permalink / raw)
  To: Heime; +Cc: Yuri Khan, Heime via Users list for the GNU Emacs text editor

Is this be the correct way for a minor mode to use the variable
tema-expr-elisp for imenu-generic-expression that will be applied
when enabling tema-minor-mode on a buffer in emacs-lisp-mode ?

(defvar tema-expr-elisp
  '(("Functions" "^\\s-(defun\\s-+\\(\\_<.?\\_>\\)" 1)))
 
(defun tema-imenu-expr-elisp ()
  "Set up a custom imenu expression for the current buffer."
  (setq-local imenu-generic-expression tema-expr-elisp))
 
;;;###autoload
(define-minor-mode tema-minor-mode
  "DESC."
  :init-value nil
  :lighter " Tema"
 
  (if tema-minor-mode
      (progn
        (add-hook 'sh-mode-hook #'tema-imenu-expr-sh)
        (add-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp))
    (remove-hook 'sh-mode-hook #'tema-imenu-expr-sh)
    (remove-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp)))
 




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

* Re: Functions which are mode dependent
  2024-08-24  9:46         ` Heime
@ 2024-08-24 10:20           ` Yuri Khan
  2024-08-24 11:33             ` Heime
  0 siblings, 1 reply; 16+ messages in thread
From: Yuri Khan @ 2024-08-24 10:20 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

On Sat, 24 Aug 2024 at 16:46, Heime <heimeborgia@protonmail.com> wrote:
>
> Is this be the correct way for a minor mode to use the variable
> tema-expr-elisp for imenu-generic-expression that will be applied
> when enabling tema-minor-mode on a buffer in emacs-lisp-mode ?

No. Consider:

1. You open an Elisp file (C-x C-f /home/me/some.el RET).
2. A buffer is created.
3. The file’s contents are loaded into the buffer.
4. The buffer switches to emacs-lisp-mode.
5. emacs-lisp-mode runs emacs-lisp-mode-hook. At this point, the hook
is empty so nothing special happens.
6. You enable your minor mode (M-x tema-minor-mode RET).
7. It adds tema-imenu-expr-elisp to emacs-lisp-mode-hook, but at this
point it is too late for your current buffer.

Observed result: your minor mode is enabled in the buffer of some.el,
but its configuration is not applied.

Further:

8. You open another Elisp file (C-x C-f /home/me/other.el RET).
9. A buffer is created.
10. The file’s contents are loaded into the buffer.
11. The buffer switches to emacs-lisp-mode.
12. emacs-lisp-mode runs emacs-lisp-mode-hook. The hook contains the
function added by enabling the minor mode in another buffer, so that
function runs and modifies imenu-generic-expression.

Observed result: In the buffer of other.el, tema-minor-mode is not
enabled, yet its configuration has been applied.


Now drop your focus on the solution. What problem are you solving? Why
do you want to modify imenu-generic-expression for emacs-lisp-mode
buffers but only sometimes?


> (defvar tema-expr-elisp
>   '(("Functions" "^\\s-(defun\\s-+\\(\\_<.?\\_>\\)" 1)))
>
> (defun tema-imenu-expr-elisp ()
>   "Set up a custom imenu expression for the current buffer."
>   (setq-local imenu-generic-expression tema-expr-elisp))
>
> ;;;###autoload
> (define-minor-mode tema-minor-mode
>   "DESC."
>   :init-value nil
>   :lighter " Tema"
>
>   (if tema-minor-mode
>       (progn
>         (add-hook 'sh-mode-hook #'tema-imenu-expr-sh)
>         (add-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp))
>     (remove-hook 'sh-mode-hook #'tema-imenu-expr-sh)
>     (remove-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp)))



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

* Re: Functions which are mode dependent
  2024-08-24 10:20           ` Yuri Khan
@ 2024-08-24 11:33             ` Heime
  2024-08-24 21:08               ` Heime
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2024-08-24 11:33 UTC (permalink / raw)
  To: Yuri Khan; +Cc: Heime via Users list for the GNU Emacs text editor

On Saturday, August 24th, 2024 at 10:20 PM, Yuri Khan <yuri.v.khan@gmail.com> wrote:

> On Sat, 24 Aug 2024 at 16:46, Heime heimeborgia@protonmail.com wrote:
> 
> > Is this be the correct way for a minor mode to use the variable
> > tema-expr-elisp for imenu-generic-expression that will be applied
> > when enabling tema-minor-mode on a buffer in emacs-lisp-mode ?
> 
> 
> No. Consider:
> 
> 1. You open an Elisp file (C-x C-f /home/me/some.el RET).
> 2. A buffer is created.
> 3. The file’s contents are loaded into the buffer.
> 4. The buffer switches to emacs-lisp-mode.
> 5. emacs-lisp-mode runs emacs-lisp-mode-hook. At this point, the hook
> is empty so nothing special happens.
> 6. You enable your minor mode (M-x tema-minor-mode RET).
> 7. It adds tema-imenu-expr-elisp to emacs-lisp-mode-hook, but at this
> point it is too late for your current buffer.
> 
> Observed result: your minor mode is enabled in the buffer of some.el,
> but its configuration is not applied.
> 
> Further:
> 
> 8. You open another Elisp file (C-x C-f /home/me/other.el RET).
> 9. A buffer is created.
> 10. The file’s contents are loaded into the buffer.
> 11. The buffer switches to emacs-lisp-mode.
> 12. emacs-lisp-mode runs emacs-lisp-mode-hook. The hook contains the
> function added by enabling the minor mode in another buffer, so that
> function runs and modifies imenu-generic-expression.
> 
> Observed result: In the buffer of other.el, tema-minor-mode is not
> enabled, yet its configuration has been applied.
> 
> 
> Now drop your focus on the solution. What problem are you solving? Why
> do you want to modify imenu-generic-expression for emacs-lisp-mode
> buffers but only sometimes?

Correct, to modify imenu-generic-expression only sometimes.

I want to have a minor mode.  When I enable the tema-minor-mode
in an emacs-lisp buffer, imenu-generic-expression is set properly
and the imenu menubar is added to the emacs menubar.  

When I disable tema-minor-mode, imenu is also disabled, meaning that
imenu menubar is removed, possibly resetting imenu-generic-expression
to the emacs default for that major-mode.  Thus not keeping the regexp
set by tema-minor-mode. 
 
> > (defvar tema-expr-elisp
> > '(("Functions" "^\\s-(defun\\s-+\\(\\<.?\\>\\)" 1)))
> > 
> > (defun tema-imenu-expr-elisp ()
> > "Set up a custom imenu expression for the current buffer."
> > (setq-local imenu-generic-expression tema-expr-elisp))
> > 
> > ;;;###autoload
> > (define-minor-mode tema-minor-mode
> > "DESC."
> > :init-value nil
> > :lighter " Tema"
> > 
> > (if tema-minor-mode
> > (progn
> > (add-hook 'sh-mode-hook #'tema-imenu-expr-sh)
> > (add-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp))
> > (remove-hook 'sh-mode-hook #'tema-imenu-expr-sh)
> > (remove-hook 'emacs-lisp-mode-hook #'tema-imenu-expr-elisp)))



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

* Re: Functions which are mode dependent
  2024-08-24 11:33             ` Heime
@ 2024-08-24 21:08               ` Heime
  2024-08-26 22:35                 ` Heime
  0 siblings, 1 reply; 16+ messages in thread
From: Heime @ 2024-08-24 21:08 UTC (permalink / raw)
  To: Heime; +Cc: Yuri Khan, Heime via Users list for the GNU Emacs text editor

On Saturday, August 24th, 2024 at 11:33 PM, Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 24th, 2024 at 10:20 PM, Yuri Khan yuri.v.khan@gmail.com wrote:
> 
> > On Sat, 24 Aug 2024 at 16:46, Heime heimeborgia@protonmail.com wrote:
> > 
> > > Is this be the correct way for a minor mode to use the variable
> > > tema-expr-elisp for imenu-generic-expression that will be applied
> > > when enabling tema-minor-mode on a buffer in emacs-lisp-mode ?
> > 
> > No. Consider:
> > 
> > 1. You open an Elisp file (C-x C-f /home/me/some.el RET).
> > 2. A buffer is created.
> > 3. The file’s contents are loaded into the buffer.
> > 4. The buffer switches to emacs-lisp-mode.
> > 5. emacs-lisp-mode runs emacs-lisp-mode-hook. At this point, the hook
> > is empty so nothing special happens.
> > 6. You enable your minor mode (M-x tema-minor-mode RET).
> > 7. It adds tema-imenu-expr-elisp to emacs-lisp-mode-hook, but at this
> > point it is too late for your current buffer.
> > 
> > Observed result: your minor mode is enabled in the buffer of some.el,
> > but its configuration is not applied.

When enabling the minor mode in an already opened buffer, I can check the 
current major-mode and directly call the appropriate configuration function 
(tema-imenu-expr-sh or tema-imenu-expr-elisp) for the current buffer.

But how would I disable the settings when the minor-mode is turned off ?

;;;###autoload
(define-minor-mode tema-minor-mode
  "DESC."
  :init-value nil
  :lighter " Tema"
  (if tema-minor-mode
      (progn
        ;; Apply the configuration for the current buffer if the major mode matches
        (cond
         ((eq major-mode 'sh-mode) (tema-imenu-expr-sh))
         ((eq major-mode 'emacs-lisp-mode) (tema-imenu-expr-elisp)))) 
    (tema-disable-imenu) ))




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

* Re: Functions which are mode dependent
  2024-08-24 21:08               ` Heime
@ 2024-08-26 22:35                 ` Heime
  0 siblings, 0 replies; 16+ messages in thread
From: Heime @ 2024-08-26 22:35 UTC (permalink / raw)
  To: Heime; +Cc: Yuri Khan, Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

On Sunday, August 25th, 2024 at 9:08 AM, Heime <heimeborgia@protonmail.com> wrote:

> On Saturday, August 24th, 2024 at 11:33 PM, Heime heimeborgia@protonmail.com wrote:
> 
> > On Saturday, August 24th, 2024 at 10:20 PM, Yuri Khan yuri.v.khan@gmail.com wrote:
> > 
> > > On Sat, 24 Aug 2024 at 16:46, Heime heimeborgia@protonmail.com wrote:
> > > 
> > > > Is this be the correct way for a minor mode to use the variable
> > > > tema-expr-elisp for imenu-generic-expression that will be applied
> > > > when enabling tema-minor-mode on a buffer in emacs-lisp-mode ?
> > > 
> > > No. Consider:
> > > 
> > > 1. You open an Elisp file (C-x C-f /home/me/some.el RET).
> > > 2. A buffer is created.
> > > 3. The file’s contents are loaded into the buffer.
> > > 4. The buffer switches to emacs-lisp-mode.
> > > 5. emacs-lisp-mode runs emacs-lisp-mode-hook. At this point, the hook
> > > is empty so nothing special happens.
> > > 6. You enable your minor mode (M-x tema-minor-mode RET).
> > > 7. It adds tema-imenu-expr-elisp to emacs-lisp-mode-hook, but at this
> > > point it is too late for your current buffer.
> > > 
> > > Observed result: your minor mode is enabled in the buffer of some.el,
> > > but its configuration is not applied.
> 
> 
> When enabling the minor mode in an already opened buffer, I can check the
> current major-mode and directly call the appropriate configuration function
> (tema-imenu-expr-sh or tema-imenu-expr-elisp) for the current buffer.
> 

This should not use any hooks, to make tema-imenu-expr-elisp activate
for the current buffer.  This should be a good idea I think.

But then, how would one disable the tema-imenu-expr-sh setting when the 
minor-mode is turned off ?
 
;;;###autoload
(define-minor-mode tema-minor-mode
  "DESC."
  :init-value nil
  :lighter " Tema"
  
  (if tema-minor-mode
      (progn
        ;; Apply the configuration for the current buffer if the major mode matches
        (cond
          ((eq major-mode 'sh-mode) (tema-imenu-expr-sh))
          ((eq major-mode 'emacs-lisp-mode) (tema-imenu-expr-elisp))))
    (tema-disable-imenu) ))



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

end of thread, other threads:[~2024-08-26 22:35 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-18 21:14 Functions which are mode dependent Heime
2024-08-20 21:49 ` Heime
2024-08-22 13:26   ` Heime
2024-08-23 10:05   ` Joel Reicher
2024-08-23 12:26     ` Heime
2024-08-22 18:29 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-08-22 18:42   ` Heime
2024-08-22 18:58 ` Yuri Khan
2024-08-22 22:07   ` Heime
2024-08-22 22:16     ` Heime
2024-08-22 23:38       ` Heime
2024-08-24  9:46         ` Heime
2024-08-24 10:20           ` Yuri Khan
2024-08-24 11:33             ` Heime
2024-08-24 21:08               ` Heime
2024-08-26 22:35                 ` Heime
     [not found] <cZeTZGgEO8ZqskRdwTYwyC8I8Nbdvc559IWYt1uERmxLYkgONsnVPIUsdZWEE43YjvR97Osjq3ZFn72HYANJnd0uZM10K1BZlgM-2iUDyAI=3D@protonmail.com>
     [not found] ` <CAP=5Fd=5F8XY-OK4kGu9LigEqbKiQgGft3ShWY58mrUPwqPcn15VPQ@mail.gmail.com>
     [not found]   ` <3RfPnnmfz0Qgr0JGJbNKnydaMu1QfnGGBk7xX7qxkXSkzXBNt8snc9Ye1n6AvZtOln0VoPDXmjnYc=5FX5lGkPVxnvPPlBbjvnuKMQs4cki18=3D@protonmail.com>
     [not found]     ` <qAaQUpb=5FJAFuU-wWLUZIi9mi6FRdXycVjQbRTi7ntswqlXGkv1k=5FaIwP9lr0av3lZ15ls2QpARUIkXOVwhHrlJ4wfyNv9PfiHjuVVzTANvc=3D@protonmail.com>
     [not found]       ` <b3r6FxVdJog97l0Sg4GtNPjvqS0kLsrpkgPKr8zOQnU3yNSLFkyCXgnGcrSaAT6hYmjme8qhUm3ecdhut8r6DXd4Hg=5FjRH0cU19tfjx=5F9JI=3D@protonmail.com>
     [not found]         ` <Y7NJnzV2gxP2ItkZNRVxRX8eD0Y-uEnE2EXo07CPNuckyabsLJoKMkpD82ReWhb3tFdL6uCFSsaLBhfZX7yXihd=5FNlUCgc2oBCFBbiv=5FCAw=3D@protonmail.com>
     [not found]           ` <CAP=5Fd=5F8UR+=3DB1GAKOTLwjgM=5FQ=5FGRbQttrwtO4 PD7wx9sJFErpcQ@mail.gmail.com>
     [not found]             ` <uumSbkMJHz2MJY2Yok3T3XDtIY2hSd8f=5FAYXIAqCmJYiv5Lf3in4c22uy6658I8RnsI4iIF-XWzGlfSPdX=5F13c6k1VPGPXov0Nnzeplp8e0=3D@protonmail.com>

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.