unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* file/line info for macro expansion
@ 2011-01-04 16:10 Maurizio Vitale
  0 siblings, 0 replies; 10+ messages in thread
From: Maurizio Vitale @ 2011-01-04 16:10 UTC (permalink / raw)
  To: help-gnu-emacs

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,

        Maurizio




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

* Re: file/line info for macro expansion
       [not found] <mailman.1.1294157491.12765.help-gnu-emacs@gnu.org>
@ 2011-01-04 16:18 ` Elena
  2011-01-04 19:52 ` Pascal J. Bourguignon
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Elena @ 2011-01-04 16:18 UTC (permalink / raw)
  To: help-gnu-emacs

For file-name: "C-h v load-file-name RET"


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

* Re: file/line info for macro expansion
       [not found] <mailman.1.1294157491.12765.help-gnu-emacs@gnu.org>
  2011-01-04 16:18 ` Elena
@ 2011-01-04 19:52 ` Pascal J. Bourguignon
  2011-01-04 22:32   ` Elena
  2011-01-04 23:46 ` Tim X
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Pascal J. Bourguignon @ 2011-01-04 19:52 UTC (permalink / raw)
  To: help-gnu-emacs

Maurizio Vitale <mav@google.com> 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 {}.


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

* Re: file/line info for macro expansion
  2011-01-04 19:52 ` Pascal J. Bourguignon
@ 2011-01-04 22:32   ` Elena
  2011-01-05  6:11     ` Stefan Monnier
  0 siblings, 1 reply; 10+ messages in thread
From: Elena @ 2011-01-04 22:32 UTC (permalink / raw)
  To: help-gnu-emacs

However, both `find-function' and `find-variable' seem to know file
and line number where a function/variable has been defined.


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

* Re: file/line info for macro expansion
       [not found] <mailman.1.1294157491.12765.help-gnu-emacs@gnu.org>
  2011-01-04 16:18 ` Elena
  2011-01-04 19:52 ` Pascal J. Bourguignon
@ 2011-01-04 23:46 ` Tim X
  2011-01-05  6:10 ` Stefan Monnier
  2011-01-05 15:16 ` Elena
  4 siblings, 0 replies; 10+ messages in thread
From: Tim X @ 2011-01-04 23:46 UTC (permalink / raw)
  To: help-gnu-emacs

Maurizio Vitale <mav@google.com> 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,
>

I don't understand what your requirement is. I also suspect you are
possibly misunderstanding the role of macros. Can you expand on what the
functionality is you want and how you would use it? It may then be
possible to suggest something more relevant. 

Tim

-- 
tcross (at) rapttech dot com dot au


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

* Re: file/line info for macro expansion
       [not found] <mailman.1.1294157491.12765.help-gnu-emacs@gnu.org>
                   ` (2 preceding siblings ...)
  2011-01-04 23:46 ` Tim X
@ 2011-01-05  6:10 ` Stefan Monnier
  2011-01-05 15:16 ` Elena
  4 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2011-01-05  6:10 UTC (permalink / raw)
  To: help-gnu-emacs

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

You mostly can't right now, sorry.

It would be feasible for byte-compiled code (and indeed, you can
somewhat do it by outputting #$ in the .elc file which will be replaced
by the file name), but for interpreted code it's a lot more tricky,
because the macro is only expanded when it is run, which can be months
after the file has been read.  And before the macro is expanded, Emacs
has no idea it's a macro call rather than a function call, so it can't
realistically remember the file&line info for that call until it
gets expanded.


        Stefan



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

* Re: file/line info for macro expansion
  2011-01-04 22:32   ` Elena
@ 2011-01-05  6:11     ` Stefan Monnier
  2011-01-05 14:50       ` Steven W. Orr
  2011-01-05 22:36       ` Maurizio Vitale
  0 siblings, 2 replies; 10+ messages in thread
From: Stefan Monnier @ 2011-01-05  6:11 UTC (permalink / raw)
  To: help-gnu-emacs

> However, both `find-function' and `find-variable' seem to know file
> and line number where a function/variable has been defined.

Emacs knows the file name (in most cases, at least), thanks to
load-history.  But it doesn't know the line number.  Instead,
find-function will look for the definition with a regular expression.


        Stefan


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

* Re: file/line info for macro expansion
  2011-01-05  6:11     ` Stefan Monnier
@ 2011-01-05 14:50       ` Steven W. Orr
  2011-01-05 22:36       ` Maurizio Vitale
  1 sibling, 0 replies; 10+ messages in thread
From: Steven W. Orr @ 2011-01-05 14:50 UTC (permalink / raw)
  To: help-gnu-emacs

On 1/5/2011 1:11 AM, Stefan Monnier wrote:
>> However, both `find-function' and `find-variable' seem to know file
>> and line number where a function/variable has been defined.
>
> Emacs knows the file name (in most cases, at least), thanks to
> load-history.  But it doesn't know the line number.  Instead,
> find-function will look for the definition with a regular expression.
>
>
>          Stefan

I find this question to be interesting. I have written a lot of python and C 
code where I needed to enable debug modes, but I also had a requirement for 
the debug code to not place any overhead on the code when debug mode was not 
enabled.

I solved this problem by elevating the code to a higher level. In my case, I 
found a preprocessor called cog (which happens to be written in python, though 
any preprocessor would do). Then instead of writing my .py files, I write 
.cog.py files which get run through a Makefile to produce the resulting .py 
files. The preprocessor expands the macros (or not) as specified on the make 
command line, and the result contains exactly what I want it to have.

In the case of .el files, I understand that the current line number is not 
terribly easily accessible, and I understand why (for the most part). Python 
has less of a problem in this regard. So, it would be easy to embed (something 
like) cog macros in a .cog.el file and after the .el file was produced, the 
resulting file would contain actual line numbers in the code.

And, if you are not trying to debug a problem, then the el files that you 
would be running would not (have to) contain any expanded macros.

Here's the link for the cog home page, but frankly, I only mention this 
because I found it, it was easy to use and it beats using m4. If someone likes 
some other system better, that's just peachy.

The big thing is if the idea of a preprocessor has any appeal at all, or if 
this feels like it's more trouble than it's worth.

http://nedbatchelder.com/code/cog/

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



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

* Re: file/line info for macro expansion
       [not found] <mailman.1.1294157491.12765.help-gnu-emacs@gnu.org>
                   ` (3 preceding siblings ...)
  2011-01-05  6:10 ` Stefan Monnier
@ 2011-01-05 15:16 ` Elena
  4 siblings, 0 replies; 10+ messages in thread
From: Elena @ 2011-01-05 15:16 UTC (permalink / raw)
  To: help-gnu-emacs

On Jan 4, 4:10 pm, Maurizio Vitale <m...@google.com> wrote:
> 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.

I'm curious about what you mean...  Could you please provide us an
example?

Thanks.


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

* Re: file/line info for macro expansion
  2011-01-05  6:11     ` Stefan Monnier
  2011-01-05 14:50       ` Steven W. Orr
@ 2011-01-05 22:36       ` Maurizio Vitale
  1 sibling, 0 replies; 10+ messages in thread
From: Maurizio Vitale @ 2011-01-05 22:36 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> "Stefan" == Stefan Monnier <monnier@iro.umontreal.ca> writes:

    >> However, both `find-function' and `find-variable' seem to know
    >> file and line number where a function/variable has been defined.

    Stefan> Emacs knows the file name (in most cases, at least), thanks
    Stefan> to load-history.  But it doesn't know the line number.
    Stefan> Instead, find-function will look for the definition with a
    Stefan> regular expression.

And this is exactly what I'm likely to be doing. I was not aware of the
filename info (thanks Elena) and for my purposeses that is close enough.
Thanks to everybody,

       Maurizio




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

end of thread, other threads:[~2011-01-05 22:36 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-04 16:10 file/line info for macro expansion Maurizio Vitale
     [not found] <mailman.1.1294157491.12765.help-gnu-emacs@gnu.org>
2011-01-04 16:18 ` Elena
2011-01-04 19:52 ` Pascal J. Bourguignon
2011-01-04 22:32   ` Elena
2011-01-05  6:11     ` Stefan Monnier
2011-01-05 14:50       ` Steven W. Orr
2011-01-05 22:36       ` Maurizio Vitale
2011-01-04 23:46 ` Tim X
2011-01-05  6:10 ` Stefan Monnier
2011-01-05 15:16 ` Elena

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).