From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Re: Adding functionality to a minor mode Date: Sun, 06 Feb 2022 22:00:34 -0800 Message-ID: <8735kvtdvh.fsf@ericabrahamsen.net> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25194"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:iiL0xWqvmF1vEai+uJv9iRzPrsY= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 07 07:04:48 2022 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 1nGx8p-0006MX-80 for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 07 Feb 2022 07:04:47 +0100 Original-Received: from localhost ([::1]:36366 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nGx8n-0001J0-My for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 07 Feb 2022 01:04:45 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:33690) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nGx4w-0001Il-Nq for help-gnu-emacs@gnu.org; Mon, 07 Feb 2022 01:00:46 -0500 Original-Received: from ciao.gmane.io ([116.202.254.214]:42814) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nGx4t-0003rB-SK for help-gnu-emacs@gnu.org; Mon, 07 Feb 2022 01:00:45 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1nGx4r-0001Rm-1R for help-gnu-emacs@gnu.org; Mon, 07 Feb 2022 07:00:41 +0100 X-Injected-Via-Gmane: http://gmane.org/ Received-SPF: pass client-ip=116.202.254.214; envelope-from=geh-help-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.249, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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" Xref: news.gmane.io gmane.emacs.help:135796 Archived-At: goncholden writes: > ------ Original Message ------- > > On Saturday, February 5th, 2022 at 6:50 PM, goncholden wrote: > >> ------- Original Message ------- >> >> On Saturday, February 5th, 2022 at 10:00 AM, goncholden via Users >> list for the GNU Emacs text editor help-gnu-emacs@gnu.org wrote: >> >> > I have made a minor-mode with some defvar and defun. Have added some functionality >> > >> > for comments, and put everything in the following function definition. >> > >> > (defun rich-annotation-tools () >> > >> > "Aggregates annotation tools for comments." >> > >> > (rich-annotation-font-weight) >> > >> > (rich-annotation-low-contrast) >> > >> > (rich-annotation-keytrigger)) >> > >> > I would expect to add "(rich-annotation-tools)" in >> > define-minor-mode. Would I introduce it >> > >> > within the "(when rich-minor-mode" part? >> > >> > ;;;###autoload >> > >> > (define-minor-mode rich-minor-mode >> > >> > "This is the description." >> > :lighter "rich" ; indicator in mode-line >> > (font-lock-remove-keywords nil rich-font-lock) >> > >> > (when rich-minor-mode >> > (font-lock-add-keywords nil rich-font-lock 'append) >> > (set (make-local-variable 'jit-lock-contextually) t) ) >> > (rich-annotation-tools) >> > >> > (with-no-warnings (font-lock-fontify-buffer)) )) ) > > Could I get some clarification for using "(when rich-minor-mode" and when not to? What's happening here is that, when you define a minor mode, it also defines a variable of the same name, which can be tested as a boolean to see if the minor mode is currently enabled or not. The only thing you need to know is that, when the minor mode is turned on or off, the variable is set *before* the body is run. So if you test the boolean variable in the body code of the minor mode, it will be t if you've just turned the mode on, and nil if you've just turned it off. For some reason, when I first started playing with minor modes, this struck me as backwards. I can no longer say why, exactly, but I remember it did.