help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: the hand that feeds you


From: Harald Hanche-Olsen
Subject: Re: the hand that feeds you
Date: Fri, 28 Jul 2006 00:05:30 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (berkeley-unix)

+ Andreas Seik <andreas_neu@gmxpro.de>:

| Hello Newsgroup,
|
| i need code in a hook that is executed only once,
| so i tested the following:
|
|
| (defvar my-hook '())
| (defun kill-myself ()
|  (insert "only once")(remove-hook 'my-hook 'kill-myself))
| (add-hook 'my-hook 'kill-myself)
| (run-hooks 'my-hook);; i think it might crash here, but it does not
| (run-hooks 'my-hook)
|
| it, seems to work, but i do not trust it.
| is it not "biting the hand that feeds you"

It works, even in the presence of more hooks on the hook variable.
But I agree it looks scary, and it seems to rely on the implementation
of run-hooks and remove-hook:  In all likelihood, run-hooks will just
cdr down the list, running the car of the rest each time.  And
remove-hook will not modify the cons cell containing kill-myself as
its car and the remaining list as its cdr, so all is well.

But it's bad coding style, because you must reason out all that to see
why it works.  And it is conceivable, if unlikely, that the
implementation one of these functions will change so that it no longer
works.  Better then, to let kill-myself really to commit suicide,
rather than just to remove itself from a hook:

(defun kill-myself ()
  (insert "only once")
  (defun kill-myself ()))

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell


reply via email to

[Prev in Thread] Current Thread [Next in Thread]