From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Re: Adding and running a major mode hook Date: Sun, 27 Apr 2014 13:15:33 +0800 Message-ID: <87y4yruzd6.fsf@ericabrahamsen.net> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1398575584 8160 80.91.229.3 (27 Apr 2014 05:13:04 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 27 Apr 2014 05:13:04 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Apr 27 07:12:56 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WeHOa-0005qy-56 for geh-help-gnu-emacs@m.gmane.org; Sun, 27 Apr 2014 07:12:56 +0200 Original-Received: from localhost ([::1]:38042 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WeHOZ-0006Hf-ME for geh-help-gnu-emacs@m.gmane.org; Sun, 27 Apr 2014 01:12:55 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58463) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WeHOI-0006Ha-Es for help-gnu-emacs@gnu.org; Sun, 27 Apr 2014 01:12:45 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WeHOA-0003Ph-VP for help-gnu-emacs@gnu.org; Sun, 27 Apr 2014 01:12:38 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:58466) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WeHOA-0003Pa-P2 for help-gnu-emacs@gnu.org; Sun, 27 Apr 2014 01:12:30 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1WeHO9-0005Gn-LK for help-gnu-emacs@gnu.org; Sun, 27 Apr 2014 07:12:29 +0200 Original-Received: from 123.122.38.2 ([123.122.38.2]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 27 Apr 2014 07:12:29 +0200 Original-Received: from eric by 123.122.38.2 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 27 Apr 2014 07:12:29 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 82 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 123.122.38.2 In-Reply-To: (Jacob Gerlach's message of "Sat, 26 Apr 2014 10:25:52 -0700 (PDT)") User-Agent: Gnus/5.13001 (Ma Gnus v0.10) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:GL/0MnY1RMnbQpjEkFtP1D3lmd0= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:97392 Archived-At: Jacob Gerlach writes: > I think that a hook is the right way for me to allow users some customization in my major mode. > > I have a list that is used to create font lock constructs, and I'd like to allow users to add to that list before my code processes it into font lock constructs. > > What I have now is basically: > > (setq content-list...) > > (mapc 'create-constructs content-list) > > What I've tried to do is > > (setq content-list ...) > > (defvar add-user-content-hook nil) > (defun add-user-content (input) > (add-hook 'add-user-content-hook > (lambda () (add-to-list 'content-list input)))) > > (run-hooks 'add-user-content-hook) > > (mapc 'create-constructs content-list) > > With the intention that users put something like > > (add-user-content '("my content")) > > in their .emacs > > This code doesn't generate any errors, but it doesn't work either. > > Reading about hooks in the documentation makes me think that I have a conceptual misunderstanding about the manner and sequence in which code in mymode.el is executed. Specifically, what is the difference between putting (run-hooks... in (define-derived-mode ... vs putting (run-hooks... somewhere in the mode's code (as I have done above)? If you've got the run-hooks inside the mode declaration, then the hooks won't run until the user activates the mode in some particular buffer. They will then run *every* time your mode is activated. If you've got it at the top level of the file, the hooks will run once, *while* the file is being loaded for the first time: ie quite likely during emacs startup, possibly before the user's relevant customization is loaded. They won't run again later, when the user calls `add-user-content', because all `add-user-content' does is add to the hook, it doesn't run the hook again. >From what I can see from your examples above, it might be enough just to have: (defvar content-list '(default values)) (define-derived-mode 'your-mode .... (mapc 'create-constructs content-list) (defun add-user-content (input) (add-to-list 'content-list input)) Without any hooks at all. When the file is loaded, these functions are defined, but no constructs are created. After the file is loaded, the user's customizations are loaded, including possible calls to `add-user-content', which put new values into `content-list'. Then, *every time* the user activates this mode, constructs are created out of whatever's in `content-list'. Now your new problem is, do you want new constructs created every time the mode is activated? Will that create duplicate/extraneous constructs? Perhaps you could use a buffer-local variable to store the constructs, so that each buffer has its own list. Anyway, if you do go the hook route, remember that it won't make much sense to call `run-hooks' at the top level of the file. That's pretty much always done in the mode declaration. Hooks are meant to store functions to be run later, when something else happens. If you put it at the top level of the file, it will only happen on load, in which case you might as well just call whatever the function is directly. Hope that didn't make it worse, Eric