From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: file/line info for macro expansion Date: Tue, 04 Jan 2011 20:52:57 +0100 Organization: Informatimago Message-ID: <87aajgfnty.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1294174683 15442 80.91.229.12 (4 Jan 2011 20:58:03 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 4 Jan 2011 20:58:03 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jan 04 21:57:59 2011 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.69) (envelope-from ) id 1PaDx8-0002Hk-KF for geh-help-gnu-emacs@m.gmane.org; Tue, 04 Jan 2011 21:57:58 +0100 Original-Received: from localhost ([127.0.0.1]:48114 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PaDhQ-0002Qr-Nc for geh-help-gnu-emacs@m.gmane.org; Tue, 04 Jan 2011 15:41:44 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 91 Original-X-Trace: individual.net H7EHFCMf2pgdYy3WU8QsnAePJWtU5xa8RXijJDPt18NA5OzdFh Cancel-Lock: sha1:NmY1ZDEyYzg0ZDU0OGExOTYwNjA3MGU2ZTllN2NmZmE5Zjg0NWNlNA== sha1:5lXPbzQCA2/wnSQknYynrFxQMjc= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:183982 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:78186 Archived-At: Maurizio Vitale writes: > I think it is not possible, but I'm ready to be surprised. > Basically, I'd like macros of the form: > > (declare-foo args) > > to expand to something containing the file and line number of the > expansion site. This way I could easily jump back to the definition. > One use is in my .emacs, where I'd like each mode customization to be > able to define its own keys and still be able to edit all keys > definitions in one place. > Thanks, For the line number, AFAIK, it's not possible in emacs lisp. First, in general, in Lisp, this questions is very difficult, because the SOURCE of a lisp program IS NOT the text file! The source of a lisp program is the data structure made of cons cells and atoms, the symbolic expression (s-exp). So the file and line number of where a cons cell is created is rather hard to define. Is it emacs-23.2/src/alloc.c:2732 ? Is it the function: (defun generate-code (x) (cons '+ (cons '42 (cons x nil)))) or is it the macro: (defmacro mac (x) (list 'progn (list 'print (generate-code x)) (list 'quote x))) or is it the defun macro? (defun f (x) (mac x)) In lisp, you could write something like: (defun genrate-functions (&rest fnames) ... `(defun ,fname ,parameters (declare-foo ,parameters) ,body ,return) ...) (defmacro define-return ...) (defmacro define-parameters ...) (defmacro define-body ...) (define-return f (/ x 2)) (define-return g b)) (define-parameters f (x y)) (define-parameters g (b c)) (define-body f (incf x y)) (define-body g (if (evenp c) (setf b c))) (generate-functions f g) So, where is the source line where declare-foo is expanded, in the function f and the function g? (Even if my example is horrible, this kind of things are done often when you define non-algorithmic DSLs; code is routinely built from s-exps coming from various parts, from massaged and mixed s-exps, etc). The fact is that a fundamental property of lisp and its macros, is that the parentheses define some 'lexical' scoping. Macros just don't have access to the environment where they're called, and they cannot compute their resulting form on anything else than their argument (and global variables, which they should avoid). In Common Lisp, macros can get an environment handle, but the only operation defined on it, is to pass it as an opaque value to macroexpand, they don't have any access to the environment either). Finally, in Common Lisp, there would be a way to do what you want, to get a "line number of serialized text of some forms", thanks to reader macros. It would be somewhat hairy, but it would be possible. Unfortunately, emacs lisp lacks reader macros. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}.