all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* the hand that feeds you
@ 2006-07-27 21:31 Andreas Seik
  2006-07-27 22:05 ` Harald Hanche-Olsen
  2006-07-28  6:50 ` wenbinye
  0 siblings, 2 replies; 5+ messages in thread
From: Andreas Seik @ 2006-07-27 21:31 UTC (permalink / raw)


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"

thank you
Andreas

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

* Re: the hand that feeds you
  2006-07-27 21:31 the hand that feeds you Andreas Seik
@ 2006-07-27 22:05 ` Harald Hanche-Olsen
  2006-07-27 23:43   ` Kevin Rodgers
       [not found]   ` <mailman.4562.1154043820.9609.help-gnu-emacs@gnu.org>
  2006-07-28  6:50 ` wenbinye
  1 sibling, 2 replies; 5+ messages in thread
From: Harald Hanche-Olsen @ 2006-07-27 22:05 UTC (permalink / raw)


+ 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

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

* Re: the hand that feeds you
  2006-07-27 22:05 ` Harald Hanche-Olsen
@ 2006-07-27 23:43   ` Kevin Rodgers
       [not found]   ` <mailman.4562.1154043820.9609.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 5+ messages in thread
From: Kevin Rodgers @ 2006-07-27 23:43 UTC (permalink / raw)


Harald Hanche-Olsen wrote:
> + 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 ()))

That is just as dependent on the implementation of defun as the original
is on the implementation of run-hooks and remove-hooks, isn't it?
Although I guess you could argue that it's only fragile with respect to
1 special form instead of 2 functions.

BTW, this is truly suicidal:

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

But maybe (fmakunbound 'kill-myself) or
(fset 'kill-myself (symbol-function 'ignore)) is good enough.  :-)

All I wanted was a Pepsi,
-- 
Kevin

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

* Re: the hand that feeds you
  2006-07-27 21:31 the hand that feeds you Andreas Seik
  2006-07-27 22:05 ` Harald Hanche-Olsen
@ 2006-07-28  6:50 ` wenbinye
  1 sibling, 0 replies; 5+ messages in thread
From: wenbinye @ 2006-07-28  6:50 UTC (permalink / raw)


how about define the function like this:

(defvar kill-myself nil)
(defun kill-myself ()
  (unless kill-myself
    (insert "only once")
    (setq kill-myself t)))

Andreas Seik wrote:
> 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"
> 
> thank you
> Andreas

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

* Re: the hand that feeds you
       [not found]   ` <mailman.4562.1154043820.9609.help-gnu-emacs@gnu.org>
@ 2006-07-28  9:12     ` Harald Hanche-Olsen
  0 siblings, 0 replies; 5+ messages in thread
From: Harald Hanche-Olsen @ 2006-07-28  9:12 UTC (permalink / raw)


+ Kevin Rodgers <ihs_4664@yahoo.com>:

| Harald Hanche-Olsen wrote:
|> (defun kill-myself ()
|>   (insert "only once")
|>   (defun kill-myself ()))
|
| That is just as dependent on the implementation of defun as the original
| is on the implementation of run-hooks and remove-hooks, isn't it?

Er, no, it is only dependent on the specification of defun.  Notably,
that the inner defun does not define a local function, as it might in
some lisp dialects (but not elisp).

| BTW, this is truly suicidal:
|
| (defun kill-myself ()
|    (insert "only once")
|    (unintern 'kill-myself))

Not suicidal at all.  The uninterned symbol remains in the hook, and
its function definition will get called.

| But maybe (fmakunbound 'kill-myself) or
| (fset 'kill-myself (symbol-function 'ignore)) is good enough.  :-)

That will cause run-hooks to fail on the second try.

-- 
* 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

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

end of thread, other threads:[~2006-07-28  9:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-07-27 21:31 the hand that feeds you Andreas Seik
2006-07-27 22:05 ` Harald Hanche-Olsen
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

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.