all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* find-file-hook for a specific file?
@ 2011-02-17  0:32 Plugger
  2011-02-17  1:34 ` Stefan Monnier
  0 siblings, 1 reply; 2+ messages in thread
From: Plugger @ 2011-02-17  0:32 UTC (permalink / raw)
  To: help-gnu-emacs

How can I get a find-file hook function to run for only a specific file?

Conceptually, I'd like this: (find-file "myfile.txt" 'myfile-hook-function)

I want myfile-hook-function to run only on the first call to find-file, i.e. when the file is opened and loaded, but not if the file is already loaded.

I do this now:
  (add-hook 'find-file-hook 'myfile-hook-function)
  (find-file "myfile.txt")
  (remove-hook 'find-file-hook 'myfile-hook-function)

This works, but it seems hokey.


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

* Re: find-file-hook for a specific file?
  2011-02-17  0:32 find-file-hook for a specific file? Plugger
@ 2011-02-17  1:34 ` Stefan Monnier
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier @ 2011-02-17  1:34 UTC (permalink / raw)
  To: help-gnu-emacs

> I do this now:
>   (add-hook 'find-file-hook 'myfile-hook-function)
>   (find-file "myfile.txt")
>   (remove-hook 'find-file-hook 'myfile-hook-function)

I don't think you can get something much cleaner, no.
But the above should use

   (add-hook 'find-file-hook 'myfile-hook-function)
   (unwind-protect (find-file "myfile.txt")
     (remove-hook 'find-file-hook 'myfile-hook-function))

So that the remove-hook is also executed when find-file signals
an error.


        Stefan


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

end of thread, other threads:[~2011-02-17  1:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-17  0:32 find-file-hook for a specific file? Plugger
2011-02-17  1:34 ` Stefan Monnier

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.