help-gnu-emacs
[Top][All Lists]
Advanced

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

relative load-file


From: rocky
Subject: relative load-file
Date: Mon, 9 Nov 2009 06:54:17 -0800 (PST)
User-agent: G2/1.0

For developing multi-file emacs packages it would be helpful to have
something akin to require-relative in Ruby 1.9. 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.

This would be easy if there were a variable like __FILE__ that Ruby
provides. (In Ruby 1.8 via __FILE__ is how folks simulate require-
relative of Ruby 1.9).

I figure it should be possible using functions symbol-file, file-name-
directory and variable load-file-name, but the closest I've come so
far is to pass in a symbol which is defined prior in the loaded file.

The code I have is pretty short so I post it below. Any suggestions
for improving, specifically to remove the need to pass in a symbol?

Should this be part of Emacs where I suspect it would be easier to
write?


(defun __FILE__ (symbol)
  "Return the string name of file of the currently running Emacs Lisp
program,
or nil.

SYMBOL should be some symbol defined in the file, but it is not
used if Emacs is currently running `load' of a file. The simplest
thing to do is call `provide' prior to this and use the value
given for that for SYMBOL. For example:
  (provide 'something)
  (__FILE__ 'something)
"
  (cond
     (load-file-name)
     ((symbol-file symbol))
     (t nil)))
(defun load-relative (file-or-list symbol)
  "Load an Emacs Lisp file relative to some other currently loaded
Emacs
Lisp file.

FILE-OR-LIST is Emacs Lisp either a string or a list of strings
containing files that you want to loaded. SYMBOL should a symbol
defined another Emacs Lisp file that you want FILE loaded relative
to. If this is called inside a `load', then SYMBOL is ignored and
`load-file-name' is used instead."

  (let ((prefix (file-name-directory (or (__FILE__ symbol) "./"))))
    (if (listp file-or-list)
        (mapcar (lambda(file) (load (concat prefix file))) file-or-list)
      (load (concat prefix file-or-list)))))


reply via email to

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