Link to home
Start Free TrialLog in
Avatar of bearware
bearwareFlag for United Kingdom of Great Britain and Northern Ireland

asked on

emacs lisp

I have a hook on.
find-file-not-found-hooks

I call find file interactively with \C-\F or \M-x find-file it works ok. but if I call it in a script (find-file "FileName") it is as if the hook is not there.

Any one know why, and how to get around it.
Avatar of bryanh
bryanh

I just tried it an it works for me.  

Maybe it is as if the hook is not there because your hook doesn't do anything when in that environment, even though it does something in the interactive environment.  You didn't mention what you expected it to do.

I used a hook that just inserts a string into the new buffer: (insert "new string") .  Does that work for you?

Also, the next step up from running find-file from the M-x buffer, but before running a program that calls find-file, is to put a buffer into lisp interaction mode and run find-file there:  To get into that mode, use the lisp-interaction-mode command.  Now put (find-file "doesntexist") into that buffer and with the cursor to the right of the closing parenthesis, hit ctl-J.  That evaluates the expression (runs the find-file function) in an environment much closer to what it will run in in your program that when you invoke it interactively.

What sort of results do you get when you do that?
Avatar of bearware

ASKER

to bryanh

Thanks for the help I tryed it but it did not work.
I tryed ctrl-J on:
(find-file "doesntexist.h")
The .h is so the template library does something

I have included some info below, If you need more just ask.

The code  on the hook is from a template library, I dont fully understand it, we need to call find-file from a lisp program as we have a popup window file dialoge that calls find-file.

The code is added to the hook with
(add-hook 'find-file-not-found-hooks 'template-not-found-function t))
The code on the hook is:
(defun template-not-found-function ()
  "Use a template when visiting a non-existent file.
See `template-auto-insert' and `template-find-file-commands'.  Function
in `find-file-not-found-hooks'."
  (and template-auto-insert (not buffer-read-only) (bobp) (eobp)
       (or (memq this-command template-find-file-commands)
        (and (memq this-command template-file-select-commands)
          ;; thanks to Dave Love <d.love@dl.ac.uk>:
          (memq (car-safe (car command-history))
                template-find-file-commands)))
       (let ((template (cdr (template-derivation buffer-file-name t))))
      (and (file-readable-p template)
           (or (eq template-auto-insert t)
            (y-or-n-p
             (format "Use template %s? "
                  (if (string-match "XEmacs\\|Lucid" emacs-version)
                      (abbreviate-file-name template t)
                    (abbreviate-file-name template)))))
           (progn (template-new-file buffer-file-name template t)
               t)))))


 
ASKER CERTIFIED SOLUTION
Avatar of bryanh
bryanh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: bryanh {http:#6994103}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer