14

From within a lisp file being loaded and run with emacs -l path/to/script/foo.el can I get the path to file being run?

For example, is there some way to determine what path/to/script is from within foo.el?

I am not very experienced with lisp so please be gentle with your solutions.

Emacs 22.3.1 on Windows 7

Jesse Vogt
  • 16,229
  • 16
  • 59
  • 72
  • possible duplicate of [How to refer to the file currently being loaded in Emacs Lisp?](http://stackoverflow.com/questions/1344747/how-to-refer-to-the-file-currently-being-loaded-in-emacs-lisp) – Trey Jackson Nov 03 '10 at 16:01
  • @Trey Jackson, I agree that it is close but it would have taken me a little more time to track down the file-name-directory function. – Jesse Vogt Nov 04 '10 at 21:50

1 Answers1

30

The variable load-file-name is bound to the name of the file as it is being loaded. To get the directory, you'd just use file-name-directory, like so:

(file-name-directory load-file-name)
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • @JesseVogt Not so *perfect* ; `eval` it in a `require`d package file: `Debugger entered--Lisp error: (wrong-type-argument stringp nil) file-name-directory(nil) ` – yPhil Jul 30 '17 at 11:55
  • 1
    @yPhil The call to `file-name-directory` requires a string be passed in, you apparently called it with a `nil`. The variable is only set during a call to `load` - which is the context of the question. – Trey Jackson Jul 31 '17 at 17:28