From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Rocky Bernstein Newsgroups: gmane.emacs.devel Subject: relative load-file Date: Wed, 11 Nov 2009 12:01:31 -0500 Message-ID: <6cd6de210911110901v24307163i253e69e89c72c9e@mail.gmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=0016368e26dee38db804781b6062 X-Trace: ger.gmane.org 1257960464 22457 80.91.229.12 (11 Nov 2009 17:27:44 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 11 Nov 2009 17:27:44 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Nov 11 18:27:37 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1N8Gyl-0002qP-Io for ged-emacs-devel@m.gmane.org; Wed, 11 Nov 2009 18:27:36 +0100 Original-Received: from localhost ([127.0.0.1]:54810 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8Gyk-0005Vx-H6 for ged-emacs-devel@m.gmane.org; Wed, 11 Nov 2009 12:27:34 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N8GZh-0002i8-AE for emacs-devel@gnu.org; Wed, 11 Nov 2009 12:01:41 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N8GZb-0002Zd-Sm for emacs-devel@gnu.org; Wed, 11 Nov 2009 12:01:40 -0500 Original-Received: from [199.232.76.173] (port=35028 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8GZa-0002ZQ-DK for emacs-devel@gnu.org; Wed, 11 Nov 2009 12:01:35 -0500 Original-Received: from mail-px0-f191.google.com ([209.85.216.191]:49032) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1N8GZZ-0005mY-Gy for emacs-devel@gnu.org; Wed, 11 Nov 2009 12:01:34 -0500 Original-Received: by pxi29 with SMTP id 29so896955pxi.1 for ; Wed, 11 Nov 2009 09:01:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:date :x-google-sender-auth:message-id:subject:from:to:content-type; bh=GdXLMN4GBXEFePRrZRWAd5SCYkjHGqpdtv18845iJQo=; b=F80iaMIQAKSW9TPY2tzT1ihvMJFIvhxHPaNA1hGvNYNXtnSlV+xclrMGsbBKf3IpF7 v47tq0GLvNO31CpUcEPKUw5dlKdJ0sF0D3dTXVqOK7Ap9krb/PlvSQ3cmZISpOzQHzQd nhMf/ZYGV+eG7+DPVxzJSNB1jqCC2PuxzhXF8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:content-type; b=RpvIMCaVEDj94x5F8R85DjVUIkFRqEP1/DKln9//1aGJFgr30MAhx9SiUcJFPuaZTc E77W4/pOUn8qZoIY/JzUtquQpufv0IalVjDH6GcTnnyPae9wQYbD8FEcWJGDP83TurQw r07m3ehQc1zGC5/lxnEyfkr/22/J4NUHO0eGU= Original-Received: by 10.142.3.13 with SMTP id 13mr191414wfc.273.1257958891818; Wed, 11 Nov 2009 09:01:31 -0800 (PST) X-Google-Sender-Auth: 469a45f441aaf35b X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:116834 Archived-At: --0016368e26dee38db804781b6062 Content-Type: text/plain; charset=ISO-8859-1 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))))) --0016368e26dee38db804781b6062 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable For developing multi-file emacs packages, it would be helpful to have=A0 so= mething akin to require-relative in Ruby 1.9. That is, one wants to
lo= ad 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 pro= vides. In Ruby 1.8, using __FILE__ one can simulate require-relative of Rub= y 1.9.

I figure it should be possible to write such a function o= r 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 whic= h is defined prior in the loaded file. (defmacro involving (symbol-file (ge= nsym)) 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?
<= /p>

Should this be part of Emacs where I suspect it would be easier to wr= ite?

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

SYMBOL should be some symbol defined in the file, but it is not
us= ed if Emacs is currently running `load' of a file. The simplest
thing to do is call `provide' prior to this and use the value
giv= en for that for SYMBOL. For example:
=A0 (provide 'something)
= =A0 (__FILE__ 'something)
"
=A0 (cond
=A0 =A0 =A0(= load-file-name)
=A0 =A0 =A0((symbol-file symbol))
=A0 =A0 =A0(t nil)))
(defun=A0= load-relative (file-or-list symbol)
=A0 "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
defi= ned another Emacs Lisp file that you want FILE loaded relative
to. If = this is called inside a `load', then SYMBOL is ignored and
`load-f= ile-name' is used instead."

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

--0016368e26dee38db804781b6062--