all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* relative load-file
@ 2009-11-11 17:01 Rocky Bernstein
  2009-11-11 18:35 ` Tassilo Horn
  2009-11-14 11:24 ` Richard Stallman
  0 siblings, 2 replies; 38+ messages in thread
From: Rocky Bernstein @ 2009-11-11 17:01 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2047 bytes --]

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, using __FILE__ one can simulate require-relative of
Ruby 1.9.

I figure it should be possible to write such a function or macro 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. (defmacro involving (symbol-file (gensym)) doesn't seem
to work.)

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)))))

[-- Attachment #2: Type: text/html, Size: 2337 bytes --]

^ permalink raw reply	[flat|nested] 38+ messages in thread
* Re: relative load-file
@ 2009-11-13  5:24 grischka
  2009-11-13  5:59 ` Rocky Bernstein
  0 siblings, 1 reply; 38+ messages in thread
From: grischka @ 2009-11-13  5:24 UTC (permalink / raw)
  To: rocky; +Cc: emacs-devel

 > [...]
 > 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.

Why would you need __FILE__ if in emacs you can type much more easily
    #$
As in
    (load (concat (file-name-directory #$) "extra-stuff"))

--- grischka





^ permalink raw reply	[flat|nested] 38+ messages in thread
* relative load-file
@ 2009-11-09 14:54 rocky
  0 siblings, 0 replies; 38+ messages in thread
From: rocky @ 2009-11-09 14:54 UTC (permalink / raw)
  To: help-gnu-emacs

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)))))


^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2009-11-24 14:11 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-11 17:01 relative load-file Rocky Bernstein
2009-11-11 18:35 ` Tassilo Horn
2009-11-11 19:26   ` Rocky Bernstein
2009-11-11 19:54     ` Tassilo Horn
2009-11-11 20:17     ` Stefan Monnier
2009-11-11 21:21       ` Rocky Bernstein
2009-11-11 23:06         ` Stefan Monnier
2009-11-12  1:01           ` Rocky Bernstein
2009-11-12  1:20             ` Stefan Monnier
2009-11-12  2:09               ` Rocky Bernstein
2009-11-12  4:22                 ` Stefan Monnier
2009-11-12 13:01                   ` Rocky Bernstein
2009-11-12 13:52                     ` spedrosa
2009-11-12 14:11                       ` Andreas Schwab
2009-11-12 15:34                     ` Stefan Monnier
2009-11-13  4:34                       ` Rocky Bernstein
2009-11-13 14:22                         ` Stefan Monnier
2009-11-13 15:03                           ` Rocky Bernstein
2009-11-13 16:17                             ` Stefan Monnier
2009-11-13 16:39                               ` Rocky Bernstein
2009-11-14 11:24 ` Richard Stallman
2009-11-14 15:44   ` Rocky Bernstein
2009-11-15 21:59     ` M Jared Finder
2009-11-18  3:20       ` Stefan Monnier
2009-11-15 22:38     ` Richard Stallman
2009-11-15 23:50       ` Rocky Bernstein
2009-11-18 12:10         ` Richard Stallman
2009-11-18 13:39           ` Rocky Bernstein
2009-11-21 22:52             ` Richard Stallman
2009-11-22  4:45               ` Rocky Bernstein
2009-11-23  2:29                 ` Richard Stallman
2009-11-23 15:04                   ` Rocky Bernstein
2009-11-24 14:11                     ` Richard Stallman
  -- strict thread matches above, loose matches on Subject: below --
2009-11-13  5:24 grischka
2009-11-13  5:59 ` Rocky Bernstein
2009-11-13 14:26   ` Stefan Monnier
2009-11-13 15:06     ` Rocky Bernstein
2009-11-09 14:54 rocky

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.