0

I want to define a action that says open <config file> within the actual config file itself. However, this config file may be in a different location on different computers. How can I get the current file's filename with a macro?


In this example, I'm looking avoid typing "etude-module-lisp" or the filename of the file in this particular piece of code:

(on/mode: [::lisp   lisp-interaction-mode]
  "etude-module-lisp"
  ::eval-cursor    'eval-last-sexp
  ::eval-buffer    'eval-buffer)
  • 2
    It's not clear (to me) what you're asking. Are you looking for a keyboard macro or a Lisp macro (why)? What is that "action" (doesn't look like Lisp). How would you define it normally, e.g. without Emacs? And what do you mean by the current file - do you mean the file visited by the current-buffer? (If so, use function buffer-file-name.) – Drew Sep 24 '18 at 3:59
  • Apparently this is about determining a namespace/module equivalent at compilation time. This kind of thing can be done easily in Clojure, in Emacs Lisp, not so much. – wasamasa Sep 27 '18 at 7:36
  • I think you're looking for load-file-name, but I'm not sure how that macro is supposed to work. – npostavs Sep 27 '18 at 12:40
  • @npostavs. this works. it's not a macro. it's a variable that's set automatically when the file is compiled. I'll accept your answer if you write it up. – zcaudate Sep 27 '18 at 13:21
1

You can use the variable load-file-name for this. Note that it's only bound while your file is loading. So this doesn't work (because you finished loading foo.el or foo.elc before foo-function is called):

(defun foo-function ()
  (message "loaded from %s" load-file-name))

You should save the value when the file is being loaded, e.g.:

(defconst foo-file (or load-file-name buffer-file-name))

(defun foo-function ()
  (message "loaded from %s" foo-file))

Falling back to buffer-file-name lets it work with eval-buffer as well as load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.