unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Harald Hanche-Olsen <hanche@math.ntnu.no>
Subject: Re: the hand that feeds you
Date: Fri, 28 Jul 2006 00:05:30 +0200	[thread overview]
Message-ID: <pco8xme1z6d.fsf@shuttle.math.ntnu.no> (raw)
In-Reply-To: 1154035859.849298@elch.in-berlin.de

+ 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	other threads:[~2006-07-27 22:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-27 21:31 the hand that feeds you Andreas Seik
2006-07-27 22:05 ` Harald Hanche-Olsen [this message]
2006-07-27 23:43   ` Kevin Rodgers
     [not found]   ` <mailman.4562.1154043820.9609.help-gnu-emacs@gnu.org>
2006-07-28  9:12     ` Harald Hanche-Olsen
2006-07-28  6:50 ` wenbinye

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pco8xme1z6d.fsf@shuttle.math.ntnu.no \
    --to=hanche@math.ntnu.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).