From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: Functions which are mode dependent Date: Fri, 23 Aug 2024 01:58:10 +0700 Message-ID: References: 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="37289"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 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 Thu Aug 22 20:58:53 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 1shD1J-0009Xs-NO for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 22 Aug 2024 20:58:53 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1shD0t-0006s6-PD; Thu, 22 Aug 2024 14:58:27 -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 1shD0s-0006lf-3q for help-gnu-emacs@gnu.org; Thu, 22 Aug 2024 14:58:26 -0400 Original-Received: from mail-ua1-x92f.google.com ([2607:f8b0:4864:20::92f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1shD0q-0004ch-JI for help-gnu-emacs@gnu.org; Thu, 22 Aug 2024 14:58:25 -0400 Original-Received: by mail-ua1-x92f.google.com with SMTP id a1e0cc1a2514c-842f5a3839bso360212241.0 for ; Thu, 22 Aug 2024 11:58:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724353103; x=1724957903; darn=gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=OizKyNxscFzY53zKOWZua/c4B9bzIhjHGnZPi/lNTpY=; b=WnVQ8VJVMZs/MWs1hWwlQpg6dsRll85rim6CFtcZHcz3XE9IYQHS/lRP/wSJVFsgH+ 4VNmbegIhlsOg3OS48UlfojOLkp0NPOSMFN86MkZckjqaY5PwU0A3sRuBSlmp0Td1xru wURi+3zoudA9s4ug8xEd6xcOpadP313nC6Yl65CdBn9wscavQwIlamrlCj2/dnZwepGh PZe/rNfJi52Dxpxzijw8AhSU68KewD/bsnVvLoEhX7IYNSBKiuEJzR/9iuA81pDNAV5M RfSWbYBEjUqyiHwXFe/3RuMyUwLvZqqQl5LlEc5x0l5dIUgwgO3l9rA3wN5tjqq3mLW3 5fOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724353103; x=1724957903; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OizKyNxscFzY53zKOWZua/c4B9bzIhjHGnZPi/lNTpY=; b=XHFlc6ptdeXLBWexT1FqEdulMuonqQX1CjP5By2aVXOLcXEsPBq84IGe8GhJOfJmMh RWZzXJi0J90nxiPSLEiD6Z7HS4PN4talk//sZDRUJxiwNF6Q2GNqhRT8CUQibOB5Xms7 umL4L4v5z3ykuiZdZHTz394dUcDofz+GPElDIyshmH3U1aMmdGPD/ke8185/w0pSnlLr qVZ4dTYY+22kOIkAPkvlAYVkWbCD/LrOKArLnNh01eeh75yLTxr6sKllo7QWbEbIvoSI 63JgiiHVk/dk/b02JzAIBRGtlmcepRZ0HKY/eemBNL6tSVy9HiyJoE+Ui83uDcaILrP9 H2Qg== X-Gm-Message-State: AOJu0Yx6DMVkYSjqJAoICYwuMbnmh3ML4+1mvq/DofbovT3fc+QjZLfx AkbfTbQJdkdWNe5l4zj3hjuPFCj6ic5IqsD57zS+H7EPTSYsbHAN5yPLVAkAuQXWLvaS9q3Puhn PR6/mQexJqQZuRjLEpAeFPzJPu8k= X-Google-Smtp-Source: AGHT+IHDPaI/b/+4OOkEHuPjx6CWDYXaw8S2YLUtTNST3VWuqs+SdkK+nM/f8N84+KX7CYOCUaT3orT9vWex+crFqZ8= X-Received: by 2002:a05:6102:6cf:b0:493:b9a0:8ee8 with SMTP id ada2fe7eead31-498d3eb7283mr6959017137.22.1724353102694; Thu, 22 Aug 2024 11:58:22 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::92f; envelope-from=yurivkhan@gmail.com; helo=mail-ua1-x92f.google.com 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_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-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:147765 Archived-At: On Mon, 19 Aug 2024 at 04:15, Heime wrote: > > I need some help understanding how to automatically enable some functiona= lity > when loading a file with some major mode. The major mode you=E2=80=99re customizing has a hook. As an end user, you a= dd your own function to that hook, and enable the functionality there. (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.) > 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 =E2=80=98major-mode=E2=80=99 against a symbol is a sign y= ou=E2=80=99re 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=E2=80=99d have to modify your =E2=80=98expr-selector=E2=80=99 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 =E2=80=98imenu-generic-expression=E2=80=99 to tha= t list for buffers using =E2=80=98emacs-lisp-mode=E2=80=99 or any major mode derived f= rom that, and to another list for buffers in =E2=80=98sh-mode=E2=80=99 or any major m= odes 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 `( =E2=80=A6something else=E2=80=A6 ))) (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.)