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

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

Re: Functions which are mode dependent


From: Heime
Subject: Re: Functions which are mode dependent
Date: Sat, 24 Aug 2024 11:33:24 +0000

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)))



reply via email to

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