Cameron
Cameron

Reputation: 53

How can I get the current file name in Common Lisp

TL;DR

Is there an equivalent, in CL, to the special __file__ variable that Python makes available? If not, is there another way to get this information from within a lisp file?

Motivation

The reason I'm trying to access the current filename from within the file is because I'm experimenting with writing my tests in my main source file. Before I get my head chewed off, this is a very small kata project where I'm allowing myself the flexibility to try things that may be dumb/bad design/etc, but the experimentation is fun and is giving me a chance to learn more about CL.

Question

That said, I am working on a simple macro that sets up a Fiveam test suite, etc and I'd like to name the test suite based on the name of the current file. Obviously, it would be very easy to pass in the "basename" of the file but then I wouldn't be able to find out if there's another way.

Granted, in Python, CL macros don't exist but if I wanted to do something with the file from which code is executing, I could use the special __file__ variable to get that filename. I'd like to see if there is an equivalent feature in CL (libraries are okay as long as I can see the source to see what it's doing)? If not, how might one go about providing this functionality, assuming it's possible?

Final Notes

If you want to tell me this is dumb and I shouldn't do it, feel free, but please include at least a brief description of why and what a better option might be. Thank you.

Upvotes: 1

Views: 853

Answers (1)

Xach
Xach

Reputation: 11854

*load-truename* and *compile-file-truename* are useful for getting the pathname of the current file being loaded or compiled, respectively. And you get a useful value at read time with #.(or *compile-file-truename* *load-truename*).

Upvotes: 10

Related Questions