From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Heime Newsgroups: gmane.emacs.help Subject: Re: Functions which are mode dependent Date: Thu, 22 Aug 2024 22:16:22 +0000 Message-ID: References: <3RfPnnmfz0Qgr0JGJbNKnydaMu1QfnGGBk7xX7qxkXSkzXBNt8snc9Ye1n6AvZtOln0VoPDXmjnYc_X5lGkPVxnvPPlBbjvnuKMQs4cki18=@protonmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22086"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Yuri Khan , Heime via Users list for the GNU Emacs text editor To: Heime Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 23 00:17:04 2024 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1shG75-0005dA-Mv for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 23 Aug 2024 00:17:03 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1shG6f-0001o8-HD; Thu, 22 Aug 2024 18:16:37 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1shG6d-0001nh-7v for help-gnu-emacs@gnu.org; Thu, 22 Aug 2024 18:16:35 -0400 Original-Received: from mail-40140.protonmail.ch ([185.70.40.140]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1shG6b-0001UQ-8P for help-gnu-emacs@gnu.org; Thu, 22 Aug 2024 18:16:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1724364989; x=1724624189; bh=WpsXVreQKjRtehMS7KViPRyBFjz0/oxMFZDGetMtU34=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=hcO/kHNwD3hwXf6Owk/HmbPfEqZRgejHf/JNo4NUKJD1jRuU3MkP9ogSZnvpW9RXR WIGrEsBs7okzyjdwW1vCXHxcCJXgxVuCSfd5X/qTrHBuLhnqd9Lm0C54kx4mpZdUFm t+2sGewA5W6Goc3xED7Ji+/x+HA5BdJ33NLxPNcuyQS7JSPvFQvpl5tiJze23LfIXz grs4jIFHe7BXerc6/QdIw6cJf2QR4PNKbob6CPpzRXlE73hNvx0LDJM4BkKsEkVouv a2VgJm9iJuMjgkOv7kw0m6tKd1soLYTIA0jux6UFo2VqPqHFEzDBFWsZcM9Uwh/8Hp ufra9ELP41w7A== In-Reply-To: <3RfPnnmfz0Qgr0JGJbNKnydaMu1QfnGGBk7xX7qxkXSkzXBNt8snc9Ye1n6AvZtOln0VoPDXmjnYc_X5lGkPVxnvPPlBbjvnuKMQs4cki18=@protonmail.com> Feedback-ID: 57735886:user:proton X-Pm-Message-ID: 505706e214cb04940d20b478f350bb050b8c4209 Received-SPF: pass client-ip=185.70.40.140; envelope-from=heimeborgia@protonmail.com; helo=mail-40140.protonmail.ch X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, TO_EQ_FM_DIRECT_MX=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:147767 Archived-At: Sent with Proton Mail secure email. On Friday, August 23rd, 2024 at 10:07 AM, Heime wrote: > On Friday, August 23rd, 2024 at 6:58 AM, Yuri Khan yuri.v.khan@gmail.com = wrote: >=20 > > On Mon, 19 Aug 2024 at 04:15, Heime heimeborgia@protonmail.com wrote: > >=20 > > > I need some help understanding how to automatically enable some funct= ionality > > > when loading a file with some major mode. > >=20 > > The major mode you=E2=80=99re customizing has a hook. As an end user, y= ou add > > your own function to that hook, and enable the functionality there. > >=20 > > (As a package developer, you=E2=80=99d 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.) >=20 >=20 > How does one use the buffer local variable ? Is it to save the > regexp which I sot immediately to imenu-generic-expression ? >=20 > How would one do it ? There is this possibility.=20 (defun tema-expr-elisp () "Set `imenu-generic-expression` to match custom headings in the current b= uffer." (make-local-variable 'imenu-generic-expression) (setq imenu-generic-expression `(("DEFUN" ,(concat "^\\s-*" "(\\(defun\\|define-minor-mode\\)\\s-+" "\\(\\(\\sw\\|\\s_\\)+\\)") 2) ))) =20 > > > For instance I have a function that sets > > >=20 > > > (defun expr-elisp () > > >=20 > > > (setq imenu-generic-expression > > > `( ("defun" ,(concat "^\\s-*" > > > "(\\(defun\\|define-minor-mode\\)\\s-+" > > > "\\(\\(\\sw\\|\\s_\\)+\\)") 2) )) ) > > >=20 > > > (defun expr-selector () > > >=20 > > > (cond > > > ((eq major-mode 'emacs-lisp-mode) (expr-elisp)) > > > (t (expr-generic)) )) > >=20 > > Having to compare =E2=80=98major-mode=E2=80=99 against a symbol is a si= gn you=E2=80=99re doing > > something wrong, for several reasons. >=20 >=20 > 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. >=20 > > * 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. >=20 >=20 > What is one to use to handle an elisp derived mode for my case ? > Still with a mode hook ? >=20 > > * To extend your customization to modes not originally in your list, > > you=E2=80=99d have to modify your =E2=80=98expr-selector=E2=80=99 funct= ion. > >=20 > > > I have put the code above in a minor mode. > > >=20 > > > ;;;###autoload > > > (define-minor-mode tema-minor-mode > > >=20 > > > (if tema-minor-mode > > > (expr-selector) > > > (message "Tema Deactivated"))) > > >=20 > > > with hooks to load the minor mode automatically for specific major mo= des. > >=20 > > As told by Stefan, your minor mode is pointless because it does > > nothing when turned off. >=20 >=20 > For now I only want to make the functionality work, and concentrate > upon that first. >=20 > > > (add-hook emacs-lisp-mode-hook #'tema-minor-mode) >=20 > > If all you want is to set =E2=80=98imenu-generic-expression=E2=80=99 to= that list for > > buffers using =E2=80=98emacs-lisp-mode=E2=80=99 or any major mode deriv= ed from that, > > and to another list for buffers in =E2=80=98sh-mode=E2=80=99 or any maj= or modes > > derived from that, all you need to do is: > >=20 > > (defun expr-elisp () > > (setq imenu-generic-expression > > `( ("defun" ,(concat "^\\\\\\\\s-*" "(\\\\\\\\(defun\\\\\\\\|define-min= or-mode\\\\\\\\)\\\\\\\\s-+" "\\\\\\\\(\\\\\\\\(\\\\\\\\sw\\\\\\\\|\\\\\\\\= s_\\\\\\\\)+\\\\\\\\)") 2) )) ) (add-hook emacs-lisp-mode-hook #'expr-elisp= ) (defun expr-sh () (setq imenu-generic-expression` ( =E2=80=A6something el= se=E2=80=A6 ))) > >=20 > > (add-hook sh-mode-hook #'expr-sh) > >=20 > > You do not need a minor mode for that. >=20 >=20 > I know about using hook with a function such as (add-hook sh-mode-hook #'= expr-sh) >=20 > 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. >=20 > > (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.) >=20 >=20 > I have no problem with doing so.