all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* doubt about mode hooks
@ 2013-06-03 14:51 Luca Ferrari
  2013-06-03 15:21 ` Le Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Luca Ferrari @ 2013-06-03 14:51 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,
so far I've added mode hooks with something like the following:

(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. I cannot see any particular advantage
from the manual, so I'm guessing the second effectively does not add
the hook itself, rather a function that runs the hook.
Any explaination?

Thanks,
Luca



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: doubt about mode hooks
  2013-06-03 14:51 doubt about mode hooks Luca Ferrari
@ 2013-06-03 15:21 ` Le Wang
  0 siblings, 0 replies; 3+ messages in thread
From: Le Wang @ 2013-06-03 15:21 UTC (permalink / raw)
  To: Luca Ferrari; +Cc: help-gnu-emacs

On Mon, Jun 3, 2013 at 10:51 PM, Luca Ferrari <fluca1978@infinito.it> wrote:
> Hi all,
> so far I've added mode hooks with something like the following:
>
> (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. I cannot see any particular advantage
> from the manual, so I'm guessing the second effectively does not add
> the hook itself, rather a function that runs the hook.
> Any explaination?

Second way looks like fanciness for the sake of fanciness to me.

The first way can also be simpler:

(add-hook 'c-mode-hook 'my-c-setup-func)


--
Le



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: doubt about mode hooks
       [not found] <mailman.929.1370271116.22516.help-gnu-emacs@gnu.org>
@ 2013-06-03 20:14 ` Emanuel Berg
  0 siblings, 0 replies; 3+ messages in thread
From: Emanuel Berg @ 2013-06-03 20:14 UTC (permalink / raw)
  To: help-gnu-emacs

Luca Ferrari <fluca1978@infinito.it> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-06-03 20:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-03 14:51 doubt about mode hooks Luca Ferrari
2013-06-03 15:21 ` Le Wang
     [not found] <mailman.929.1370271116.22516.help-gnu-emacs@gnu.org>
2013-06-03 20:14 ` Emanuel Berg

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.