From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: rocky Newsgroups: gmane.emacs.help Subject: relative load-file Date: Mon, 9 Nov 2009 06:54:17 -0800 (PST) Organization: http://groups.google.com Message-ID: <1e036b89-3c10-464f-a450-8911e6392593@j4g2000yqe.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1257781338 23342 80.91.229.12 (9 Nov 2009 15:42:18 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 9 Nov 2009 15:42:18 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Nov 09 16:42:11 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1N7WNa-0007j0-W1 for geh-help-gnu-emacs@m.gmane.org; Mon, 09 Nov 2009 16:42:07 +0100 Original-Received: from localhost ([127.0.0.1]:47573 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N7WNa-0004rf-RN for geh-help-gnu-emacs@m.gmane.org; Mon, 09 Nov 2009 10:42:06 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!postnews.google.com!j4g2000yqe.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 51 Original-NNTP-Posting-Host: 66.92.120.162 Original-X-Trace: posting.google.com 1257778458 26639 127.0.0.1 (9 Nov 2009 14:54:18 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Mon, 9 Nov 2009 14:54:18 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j4g2000yqe.googlegroups.com; posting-host=66.92.120.162; posting-account=jKjGDQoAAABKN2iauJtD3DV5oMZpXuQo User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.15) Gecko/2009102815 Ubuntu/9.04 (jaunty) Firefox/3.0.15, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:174550 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:69625 Archived-At: 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)))))