From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.help Subject: Re: doubt about mode hooks Date: Mon, 03 Jun 2013 22:14:28 +0200 Organization: Aioe.org NNTP Server Message-ID: <87ip1vt0ij.fsf@VLAN-3434.student.uu.se> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1370290673 30823 80.91.229.3 (3 Jun 2013 20:17:53 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 3 Jun 2013 20:17:53 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Jun 03 22:17:54 2013 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 1UjbCU-0004qj-QE for geh-help-gnu-emacs@m.gmane.org; Mon, 03 Jun 2013 22:17:54 +0200 Original-Received: from localhost ([::1]:47563 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UjbCU-0001v2-CY for geh-help-gnu-emacs@m.gmane.org; Mon, 03 Jun 2013 16:17:54 -0400 Original-Path: usenet.stanford.edu!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 52 Original-NNTP-Posting-Host: SWN/nubmpQxYKwY7hPy4YA.user.speranza.aioe.org Original-X-Complaints-To: abuse@aioe.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) X-Notice: Filtered by postfilter v. 0.8.2 Cancel-Lock: sha1:3EJnSvvt9BmKq2z5NgyhkHrbZbI= Original-Xref: usenet.stanford.edu gnu.emacs.help:199007 X-Mailman-Approved-At: Mon, 03 Jun 2013 16:17:43 -0400 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:91274 Archived-At: Luca Ferrari writes: > (add-hook 'c-mode-hook (lambda() > (my-function-hook) ) > > but then I come across a snippet that does something different: > > (setq my-personal-hook 'my-function-hook) > (add-hook c-mode-hook (lambda () > (run-hooks 'my-personal-hook) ) t ) > > so I was wondering what are the differences and which advantages > should I get using the second. If you have something that you wish to hook to several events (or the same event, but in several modes), you can put that something in a function, that will, in turn, add the hook. Where you originally would have put that something, you can now (perhaps easier) just add a reference to the function, and the material will be added as a hook. If that was difficult to follow, let me give you an example. For almost all my typing, be it programming or not, I don't care for tabs. So for a lot of modes, I like an untab-all invocation on write. But, as there are exceptions to this rule (for example, GNU makefiles which need tabs), I cannot make it global; it has to be setup mode for mode. Thus: (defun add-write-contents-hooks-hook () (interactive) (add-hook 'write-contents-hooks 'untab-all nil ; APPEND unrelated, explicit default nil as optional t )) ; LOCAL non-nil => make hook local (add-hook 'emacs-lisp-mode-hook #'add-write-contents-hooks-hook) (add-hook 'c-mode-common-hook #'add-write-contents-hooks-hook) (add-hook 'sh-mode-hook #'add-write-contents-hooks-hook) ; ... I agree hooks can be confusing. As a rule of thumb, don't rely on them when it is not needed. Lots of stuff can be placed elsewhere, so they won't slow down everything. If you can solve a problem by placing stuff in a hook - do it! - but as your skills grow, always be on the lookout for when the possibility arises to lighten the burden on the hooks. -- Emanuel Berg - programmer (hire me! CV below) computer projects: http://user.it.uu.se/~embe8573 internet activity: http://home.student.uu.se/embe8573