unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Heime <heimeborgia@protonmail.com>
To: Heime <heimeborgia@protonmail.com>
Cc: Yuri Khan <yuri.v.khan@gmail.com>,
	Heime via Users list for the GNU Emacs text editor
	<help-gnu-emacs@gnu.org>
Subject: Re: Functions which are mode dependent
Date: Thu, 22 Aug 2024 22:16:22 +0000	[thread overview]
Message-ID: <qAaQUpb_JAFuU-wWLUZIi9mi6FRdXycVjQbRTi7ntswqlXGkv1k_aIwP9lr0av3lZ15ls2QpARUIkXOVwhHrlJ4wfyNv9PfiHjuVVzTANvc=@protonmail.com> (raw)
In-Reply-To: <3RfPnnmfz0Qgr0JGJbNKnydaMu1QfnGGBk7xX7qxkXSkzXBNt8snc9Ye1n6AvZtOln0VoPDXmjnYc_X5lGkPVxnvPPlBbjvnuKMQs4cki18=@protonmail.com>






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.



  reply	other threads:[~2024-08-22 22:16 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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>

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='qAaQUpb_JAFuU-wWLUZIi9mi6FRdXycVjQbRTi7ntswqlXGkv1k_aIwP9lr0av3lZ15ls2QpARUIkXOVwhHrlJ4wfyNv9PfiHjuVVzTANvc=@protonmail.com' \
    --to=heimeborgia@protonmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=yuri.v.khan@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).