[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: relative load-file

From: M Jared Finder
Subject: Re: relative load-file
Date: Sun, 15 Nov 2009 13:59:10 -0800
User-agent: Mozilla-Thunderbird (X11/20090707)

Let me state this clearly, because I'm not sure you're getting it.

A function that is being defined does not necessarily get defined from a file.

For example, you can send code directly to the interpreter.  Like this:
(eval '(defun foo () (message "Hello")))

What should __FILE__ return in this case?

eval-current-buffer and friends all are using this code path. require uses load which is why load-file-name and #$ work.

Oh and by the way, (car-safe current-load-list) doesn't do anything useful in Emacs 22.

  -- MJF

Rocky Bernstein wrote:

On Sat, Nov 14, 2009 at 6:24 AM, Richard Stallman <address@hidden <mailto:address@hidden>> wrote:

    The basic idea of `require' is that you specify a feature which gets
    found through a search of all libraries.  The idea is that these are
    general features which anything might want to use.

Require has/ two/ parts which are distinct. One part has the searching for a file in it. The other part is the conditional load depending on whether or not a feature has already been loaded

    You want something different:

       That is, one wants to
       load an Emacs Lisp file relative the file that issues the load
    which is
       often in the same directory or a nearby directory.

    It seems to me that this is useful only for loading other parts of one
    single multi-file program.

No. A large modular program can be composed of other smaller modular programs. In theory, one can package each file or subsets of files as their own package, but often those smaller modules may be custom enough that it doesn't make sense to do so. Or at least not initially.

However when developing or working on the program, one wants may want to work with each of the subparts independently.

Here's an example that no doubt you are familiar with: a compile has a parser, code generator and code optimizer. In a well-written modular compiler one may want to work on and test the parser as an independent unit even though I'm not sure you would want to package the front-end for a specific language independently.

So what require-relative and load-relative allow one to do is create these little independent units without the overhead of using a more elaborate packaging systems. require/provide in fact does this too. However it pulls in file searching which is not desired here. Instead, what we want to do in a program built of custom modules but is just change how the file searching is managed.

The second benefit of this is that we can develop out of the source tree (that is without having to "install" the submodules) and have another version or many versions "installed" at the same time. Starting with a member of each version stays within that version of the code.

    If you have a program which is in multiple files, you can write a
    function similar to `require' which searches for files in any way you
    like, and then you can use it.  The function can be in your program;
    it does not need to be built-in.  It can load the chosen file using

    The files should each use `provide' in the usual way, and your
    function should use the variable `features' to see if the desired
    feature has already been loaded.  The only difference would be in
    choosing which file to load.

That's in fact what I have done <http://github.com/rocky/emacs-load-relative>. The only "primitive" needed is Ruby's __FILE__ which is the same thing as the C preprocessor __FILE__.

$# and load-file-name are close; better is (car-safe current-load-list). How these differ, and where __FILE__ is better, is that a macro substitution of the file name is done at compile time inside the code the if compiled, or at eval time if not.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]