4

How do I load another Lisp file without having to specify the full path? I tried (load /path/to/file), but it seems to work with absolute path only.

I know, using Eclipse does not seems right to many Lispers. I used Emacs for my C++ programming and shell scripting, however, I don't want to spend time reading 50 pages manual of SLIME. I will learn it later, but for now I just want to learn the language without too much trouble.

Amumu
  • 17,924
  • 31
  • 84
  • 131

2 Answers2

5

load is really a low-level primitive for building things like library management systems.

If the system you're writing consists of several files, the best choice is to use a system definition facility such as ASDF. This will both make life easier for you by loading (and reloading) everything in the right order and automatically loading dependencies as needed, and enable the system to interoperate well with third-party code like Quicklisp.

Should you really want to deal with loading files yourself, cl:*default-pathname-defaults* is one setting to keep in mind.

Matthias Benkard
  • 15,497
  • 4
  • 39
  • 47
  • I found the solution. It's true as you said that ASDF is the suitable tool, and CUSP really did use ASDF to manage the project. All I need to do is to add the file name and its dependencies to the project file manage by CUSP (the project file has the same name as the package name). No need to add `(load)` into my source file. ASDF takes care of that. I will spend some time with ASDF in the future. – Amumu Mar 07 '12 at 11:35
2

It should work fine loading a file in the same directory with (load "file"), a file in a subdirectory with (load "subdir/file") and in a "sibling" directory with (load "../otherdir/file") but if your filename starts with a / it is inherently an absolute path.

Vatine
  • 20,782
  • 4
  • 54
  • 70
  • 3
    I use `sbcl`, and it seems to not work for me. It works in `clisp` though. – Amumu Mar 06 '12 at 15:20
  • @Amumu If you call `(sb-posix:getcwd)` in SBCL, is the directory name returned the one you expect for your relative path(s) to work? – Vatine Mar 06 '12 at 15:25
  • Ah, it returns the path of eclipse. I am using CUSP, an eclipse plugin, which uses SBCL as behind. – Amumu Mar 06 '12 at 15:27
  • 1
    I think the REPL searches included files for the current working directory, but not directory of the including file. How do I adjust it? – Amumu Mar 06 '12 at 15:32
  • 1
    @amumu How do you mean? Relative paths are always relative to the working directory of the process. If you want to load a file relative to the file you're loading, you'll need to construct a path load-time. It also depends on what you're doing, it sounds like you're trying something package-management-like and if so, you may want to have a look at ASDF or ASDF2. – Vatine Mar 07 '12 at 10:26
  • It's true that I have to used ASDF to manage file, and CUSP uses ASDF to manage it. Thanks for the answer still. – Amumu Mar 07 '12 at 11:37