emacs lisp

Posted on 2002-05-01
Medium Priority
Last Modified: 2010-04-22
I have a hook on.

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.
Question by:bearware
  • 2

Expert Comment

ID: 6990660
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?

Author Comment

ID: 6993533
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))
       (let ((template (cdr (template-derivation buffer-file-name t))))
      (and (file-readable-p template)
           (or (eq template-auto-insert t)
             (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)


Accepted Solution

bryanh earned 200 total points
ID: 6994103
What do you mean "did not work"?  The point of the ctl-J was to find out if you get the same symptom as when you invoke the function in your own program.  Were you unable to find out?  If so, how?  If you mean you were able to duplicate the symptom (i.e. the hook appeared not to run), then the ctl-J worked.

This hook function is too complex for me to see if there's something wrong -- you need to isolate the problem by making a simpler test case.  How about writing a one-line hook that simply inserts the string "I'm running" in all cases.  If that doesn't work, then the problem has nothing to do with this code and we don't have to waste time reading it.

I note in the documentation of file-not-found-hooks that it says this is not a normal hook, but rather a simple list of functions.  So I did not use add-hook in my test.  I simply created a list that consisted of my one test hook function.
LVL 20

Expert Comment

ID: 9914913
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.

EE Cleanup Volunteer

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question