From: Didier Verna
Subject: [Q] package-specific dispatch macro character
Date: 
Message-ID: <mux8wtirgu3.fsf@uzeb.lrde.epita.fr>
       Hello,

I have a package which is ASDF installable and in which I'd like to use
a personal dispatch macro character. This particular DMC should be
active when the package files are compiled or evaluated (via ASDF, but
ideally in other situations), but I don't want to pollute the rest of
the lisp session with it.

So my question is: what's the best way to do that?

Thanks.

-- 
Resistance is futile. You will be jazzimilated.

Scientific site:   http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bic�tre, France
Tel. +33 (0)1 44 08 01 85       Fax. +33 (0)1 53 14 59 22

From: Thomas F. Burdick
Subject: Re: package-specific dispatch macro character
Date: 
Message-ID: <e4aa6897-ef4c-4b87-b445-e98b7c05ce27@l42g2000hsc.googlegroups.com>
On Sep 23, 6:02 pm, Didier Verna <······@lrde.epita.fr> wrote:
>        Hello,
>
> I have a package which is ASDF installable and in which I'd like to use
> a personal dispatch macro character. This particular DMC should be
> active when the package files are compiled or evaluated (via ASDF, but
> ideally in other situations), but I don't want to pollute the rest of
> the lisp session with it.
>
> So my question is: what's the best way to do that?

Rainer already gave you the standard autistic cll answer, so it's safe
to answer your question.

Both LOAD and COMPILE-FILE bind *readtable*, so you can safely set it
at the top of a source file, much like you do with *package*
(indirectly via IN-PACKAGE).  So here's the approach that I think
works best.  Define a function that returns a copy of a readtable, but
with all your custom stuff installed:

  (defun customize-readtable (rt)
    (let ((rt (copy-readtable rt)))
      ... install your character macros here ...))

Now define an otherwise-vanilla readtable for your package. Notice how
NIL means the standard readtable (clever, non?):

  (defvar *my-readtable* (customize-readtable nil))

Finally, make yourself something like in-package, but for readtable:

  (defmacro use-readtable (rt)
    `(eval-when (:compile-toplevel :load-toplevel :execute)
       (setf *readtable* ,rt)))

Put all that stuff in the file where you define your reader macro
functions. Or if they come from another package, put them in a utils
file that gets loaded first in your system (maybe in the file with the
defpackage, for example). Now you can start all your files with:

  (in-package :my-package)
  (use-readtable *my-readtable*)

It would have been nice if the language had been a little more helpful
here. I'm guessing LispMs did this with magic mode-lines. Oh well,
it's not too bad as-is anyhow.

> Resistance is futile. You will be jazzimilated.

Composed to the strains of "J'ai fait un rêve" ... tout le contraire,
tes jazz seront funktifiés ;-)
From: Didier Verna
Subject: Re: package-specific dispatch macro character
Date: 
Message-ID: <muxmyhwpmqy.fsf@uzeb.lrde.epita.fr>
       For some reason, you message was not correctly threaded in my
Gnus Summary Buffer, so I didn't see it at first...



"Thomas F. Burdick" <········@gmail.com> wrote:

> So here's the approach that I think works best.

  Thanks a bunch. That was very helpful.


>> Resistance is futile. You will be jazzimilated.
>
> Composed to the strains of "J'ai fait un r�ve" ... tout le contraire,
> tes jazz seront funktifi�s ;-)

  Works for me[1] ;-)


Footnotes: 
[1]  Bonus point for the guy who tells me which guitarist released an
album with that name in 2001.

-- 
Resistance is futile. You will be jazzimilated.

Scientific site:   http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bic�tre, France
Tel. +33 (0)1 44 08 01 85       Fax. +33 (0)1 53 14 59 22
From: Duane Rettig
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <o0d4iuzmtf.fsf@gemini.franz.com>
Didier Verna <······@lrde.epita.fr> writes:

>        Hello,
>
> I have a package which is ASDF installable and in which I'd like to use
> a personal dispatch macro character. This particular DMC should be
> active when the package files are compiled or evaluated (via ASDF, but
> ideally in other situations), but I don't want to pollute the rest of
> the lisp session with it.
>
> So my question is: what's the best way to do that?

I'm not sure how many CLs support "named readtables".  Allegro CL
supports them
(http://www.franz.com/support/documentation/8.1/doc/operators/excl/named-readtable.htm)
and since *readtable* is bound to itself each time a file is loaded or
compiled, a setq of *readtable* during compilation or loading does not
affect the rest of the lisp (beware of compile-time-too actions with
side-effects, though).

Of course, to do this portably, you'd probably wouldn't be able to use
named-readtables - you'd want to set up a global variable that
contains the readtable that you have modified to do what you want.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Tobias C. Rittweiler
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <87prmu8xd7.fsf@freebits.de>
Duane Rettig <·····@franz.com> writes:

> I'm not sure how many CLs support "named readtables".  Allegro CL
> supports them
> (http://www.franz.com/support/documentation/8.1/doc/operators/excl/named-readtable.htm)
> and since *readtable* is bound to itself each time a file is loaded or
> compiled, a setq of *readtable* during compilation or loading does not
> affect the rest of the lisp (beware of compile-time-too actions with
> side-effects, though).

I'm working on a library that provides this functionality as well, in
fact extends on what ACL providse. It provides pretty much anything that
is also provided for packages by ANSI CL. And it does in fact hook into
ACL's named readtables machinery.

Does ACL provide a WITH-READTABLE-ITERATOR, and if not, can I convince
you that it'll be worthwhile to add it? :-)

  -T.
From: Rainer Joswig
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <joswig-5B2FDA.18340823092008@news-europe.giganews.com>
In article <···············@uzeb.lrde.epita.fr>,
 Didier Verna <······@lrde.epita.fr> wrote:

>        Hello,
> 
> I have a package which is ASDF installable

what is that 'package' ??? I thought ASDF is about files and collections
of files.

> and in which I'd like to use
> a personal dispatch macro character. This particular DMC should be
> active when the package files are compiled or evaluated (via ASDF, but
> ideally in other situations), but I don't want to pollute the rest of
> the lisp session with it.
> 
> So my question is: what's the best way to do that?
> 
> Thanks.

I'm not sure I understand...

PACKAGEs are a functionality defined by the Common Lisp
standard. PACKAGEs are independent of files - they are
not connected. A package is kind of a namespace for symbols.

I think you need to tell Lisp that you want
to use a particular readtable or want a readtable
modified. A typical way would be to modify the build system
that it knows about readtables and allow declarations
of which readtable to use for files/modules/systems...

-- 
http://lispm.dyndns.org/
From: Didier Verna
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <mux4p44r28t.fsf@uzeb.lrde.epita.fr>
Rainer Joswig <······@lisp.de> wrote:

> what is that 'package' ??? I thought ASDF is about files and collections
> of files.

  Poor choice of words. I didn't mean package in the Common Lisp sense,
but just as library, or collection of files for which there happens to
be a .asd file.

> I think you need to tell Lisp that you want to use a particular
> readtable or want a readtable modified. A typical way would be to
> modify the build system that it knows about readtables and allow
> declarations of which readtable to use for files/modules/systems...

  Sure. Precisely my question: what's the best way to do that?

-- 
Resistance is futile. You will be jazzimilated.

Scientific site:   http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bic�tre, France
Tel. +33 (0)1 44 08 01 85       Fax. +33 (0)1 53 14 59 22
From: Kaz Kylheku
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <20080923123151.917@gmail.com>
On 2008-09-23, Didier Verna <······@lrde.epita.fr> wrote:
>
>        Hello,
>
> I have a package which is ASDF installable and in which I'd like to use
> a personal dispatch macro character. This particular DMC should be
> active when the package files are compiled or evaluated (via ASDF, but
> ideally in other situations), but I don't want to pollute the rest of
> the lisp session with it.
>
> So my question is: what's the best way to do that?

Note that the functions COMPILE-FILE and LOAD save and restore the
values of *PACKAGE* and *READ-TABLE*.

This is why you can have (IN-PACKAGE "MY-PACKAGE") in a file
without worrying that loading the file will change the current package.

You can take the same liberty in changing the read table near
the top of your source files.

I would do it via read-time evaluation, since the read table adjustment is not
useful for load time:

  ;; top of the file

  ;; ...

  (in-package :my-package)

  ;; at read time, hash dot!
  #.(set-dispatch-macro-character ...)

  ;; ...

  ;; bottom of file: package and readtable are restored for us!

Hope this helps.
From: Marco Antoniotti
Subject: Re: package-specific dispatch macro character
Date: 
Message-ID: <9d5b5a65-b10c-4fa1-81a0-6aa2f583ed14@59g2000hsb.googlegroups.com>
On Sep 23, 9:46 pm, Kaz Kylheku <········@gmail.com> wrote:
> On 2008-09-23, Didier Verna <······@lrde.epita.fr> wrote:
>
>
>
> >        Hello,
>
> > I have a package which is ASDF installable and in which I'd like to use
> > a personal dispatch macro character. This particular DMC should be
> > active when the package files are compiled or evaluated (via ASDF, but
> > ideally in other situations), but I don't want to pollute the rest of
> > the lisp session with it.
>
> > So my question is: what's the best way to do that?
>
> Note that the functions COMPILE-FILE and LOAD save and restore the
> values of *PACKAGE* and *READ-TABLE*.
>
> This is why you can have (IN-PACKAGE "MY-PACKAGE") in a file
> without worrying that loading the file will change the current package.
>
> You can take the same liberty in changing the read table near
> the top of your source files.
>
> I would do it via read-time evaluation, since the read table adjustment is not
> useful for load time:
>
>   ;; top of the file
>
>   ;; ...
>
>   (in-package :my-package)
>
>   ;; at read time, hash dot!
>   #.(set-dispatch-macro-character ...)
>
>   ;; ...
>
>   ;; bottom of file: package and readtable are restored for us!
>
> Hope this helps.

Not really a followup... but didn't KMP have a WITH-SYNTAX macro
floating around?

Cheers
--
Marco
From: Rob Warnock
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <ooadnet9_psgO0TVnZ2dnUVZ_sninZ2d@speakeasy.net>
Kaz Kylheku  <········@gmail.com> wrote:
+---------------
| Didier Verna <······@lrde.epita.fr> wrote:
| > I have a package which is ASDF installable and in which I'd like to use
| > a personal dispatch macro character. This particular DMC should be
| > active when the package files are compiled or evaluated (via ASDF, but
| > ideally in other situations), but I don't want to pollute the rest of
| > the lisp session with it.
...
| Note that the functions COMPILE-FILE and LOAD save and restore the
| values of *PACKAGE* and *READ-TABLE*.
+---------------

Ummm, yes, saves/restores [binds, actually] the values of those
*variables*, but *doesn't* save/restore the objects they contain!!
(See below...)

+---------------
| This is why you can have (IN-PACKAGE "MY-PACKAGE") in a file
| without worrying that loading the file will change the current package.
+---------------

True, but IN-PACKAGE effectively just sets CL:*PACKAGE*, it doesn't mess
with the package *itself* which was the former value of CL:*PACKAGE*.

+---------------
| You can take the same liberty in changing the read table near
| the top of your source files.
+---------------

CAREFUL!! You may safely change the CL:*READTABLE* *variable* here,
but *NOT* the readtable it contains!!!

+---------------
| I would do it via read-time evaluation, since the read table
| adjustment is not useful for load time:
| 
|   ;; top of the file
|   ;; ...
|   (in-package :my-package)
|   ;; at read time, hash dot!
|   #.(set-dispatch-macro-character ...)
|   ;; ...
|   ;; bottom of file: package and readtable are restored for us!
+---------------

No, the CL:*PACKAGE* and CL:*READTABLE* *variables* are restored
for us, but the objects they originally contained *aren't*!
CL:*PACKAGE* isn't a problem, since you didn't change the formerly-
referenced package object, but by doing the SET-DISPATCH-MACRO-CHARACTER
you *did* mutate the actual readtable that the CL:*READTABLE* previously
held... and now still currently holds, even after the return to the
compiler/loader! (Oops.)

Thomas Burdick already noted up-thread the right way to do this, but
here's the short-short version [steps #2 & #3 may safely be swapped]:

1. Create a *new* readtable object;
2. Make your mods to *that* readtable; and then
3. Assign that object to the CL:*READTABLE* variable.

So, modifying your template:

    ;; top of the file
    ;; ...
    (in-package :my-package)

    (eval-when (:compile-toplevel :load-toplevel :execute)
      (setf cl:*readtable*
	    (let ((my-rt ((copy-readtable rt))))
	      (flet ((my-func (stream sub-char infix-count)
		       ;; Only if you really do ignore them
		       (declare (ignore sub-char infix-count))
	               ...{your new syntax implemented here}...))
		;; Using "#$" as an example
		(set-dispatch-macro-character #\# #\$ my-func my-rt))
	      my-rt)))
    ;; New syntax now enabled for the rest of the file.

    ;; ...
    ;; bottom of file: package and readtable are restored for us!

The above over-simplified template is really only useful if you have
only *one* file that needs to use the modified syntax and you want
it enabled *everywhere* within that file. Thomas Burdick's version is
better if you need multiple files to use the syntax and/or if you want
to be able to easily turn the syntax on & off within a given file.


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Didier Verna
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <muxr678pn43.fsf@uzeb.lrde.epita.fr>
····@rpw3.org (Rob Warnock) wrote:

> So, modifying your template:
>
>     ;; top of the file
>     ;; ...
>     (in-package :my-package)
>
>     (eval-when (:compile-toplevel :load-toplevel :execute)
>       (setf cl:*readtable*
> 	    (let ((my-rt ((copy-readtable rt))))
> 	      (flet ((my-func (stream sub-char infix-count)
> 		       ;; Only if you really do ignore them
> 		       (declare (ignore sub-char infix-count))
> 	               ...{your new syntax implemented here}...))
> 		;; Using "#$" as an example
> 		(set-dispatch-macro-character #\# #\$ my-func my-rt))
> 	      my-rt)))
>     ;; New syntax now enabled for the rest of the file.
>
>     ;; ...
>     ;; bottom of file: package and readtable are restored for us!

      Cool :-)

> The above over-simplified template is really only useful if you have
> only *one* file that needs to use the modified syntax and you want it
> enabled *everywhere* within that file. Thomas Burdick's version is

  ??? Do you mean Tobias ?

-- 
Resistance is futile. You will be jazzimilated.

Scientific site:   http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bic�tre, France
Tel. +33 (0)1 44 08 01 85       Fax. +33 (0)1 53 14 59 22
From: Rob Warnock
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <vcCdnT351fOtIEHVnZ2dnUVZ_hmdnZ2d@speakeasy.net>
Didier Verna  <······@lrde.epita.fr> wrote:
+---------------
| ····@rpw3.org (Rob Warnock) wrote:
| > So, modifying your template:
...[trimmed]...
| > The above over-simplified template is really only useful if you have
| > only *one* file that needs to use the modified syntax and you want it
| > enabled *everywhere* within that file. Thomas Burdick's version is
| 
|   ??? Do you mean Tobias ?
+---------------

No, I really meant this one by Thomas Burdick:

    Newsgroups: comp.lang.lisp
    From: "Thomas F. Burdick" <········@gmail.com>
    Subject: Re: package-specific dispatch macro character
    Date: Tue, 23 Sep 2008 12:23:58 -0700 (PDT)
    Message-ID: <····································@l42g2000hsc.googlegroups.com>
    Lines: 51


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Rob Warnock
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <5eKdnR9SlYjIIkHVnZ2dnUVZ_judnZ2d@speakeasy.net>
Didier Verna  <······@lrde.epita.fr> wrote:
+---------------
| ····@rpw3.org (Rob Warnock) wrote:
| > So, modifying your template:
| >     ;; top of the file
| >     ;; ...
| >     (in-package :my-package)
| >
| >     (eval-when (:compile-toplevel :load-toplevel :execute)
| >       (setf cl:*readtable*
| > 	    (let ((my-rt ((copy-readtable rt))))
| > 	      (flet ((my-func (stream sub-char infix-count)
| > 		       ;; Only if you really do ignore them
| > 		       (declare (ignore sub-char infix-count))
| > 	               ...{your new syntax implemented here}...))
| > 		;; Using "#$" as an example
| > 		(set-dispatch-macro-character #\# #\$ my-func my-rt))
| > 	      my-rt)))
| >     ;; New syntax now enabled for the rest of the file.
| >     ;; ...
| >     ;; bottom of file: package and readtable are restored for us!
| 
| Cool :-)
+---------------

Oops! *Not* so cool after all [cut & paste sloppiness]. This line:

    	    (let ((my-rt ((copy-readtable rt)))) ; BUG! Unbound free var RT

should be this instead:

    	    (let ((my-rt ((copy-readtable nil)))) ; Start with standard one

Sorry 'bout that...


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Didier Verna
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <muxvdwkpnc5.fsf@uzeb.lrde.epita.fr>
Kaz Kylheku <········@gmail.com> wrote:

> Note that the functions COMPILE-FILE and LOAD save and restore the
> values of *PACKAGE* and *READ-TABLE*.

  Hmmm I didn't know about *read-table* being saved and restored as
well. So the answer to my question might be simpler than I expected.

> Hope this helps.

Thanks a lot !

-- 
Resistance is futile. You will be jazzimilated.

Scientific site:   http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bic�tre, France
Tel. +33 (0)1 44 08 01 85       Fax. +33 (0)1 53 14 59 22
From: Kaz Kylheku
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <20080925131011.205@gmail.com>
On 2008-09-25, Didier Verna <······@lrde.epita.fr> wrote:
> Kaz Kylheku <········@gmail.com> wrote:
>
>> Note that the functions COMPILE-FILE and LOAD save and restore the
>> values of *PACKAGE* and *READ-TABLE*.
>
>   Hmmm I didn't know about *read-table* being saved and restored as
> well. So the answer to my question might be simpler than I expected.

But not quite as simple that; see Rob Warnock's followup.

The value of the *READ-TABLE* variable is saved and restored, but of course not
the read table itself. 

This means you have to assign your own read table to *READ-TABLE*:

  (eval-when (:compile-toplevel :load-toplevel :execute)
    (setf *read-table* (copy-readtable)))

Now the changes you make go to a private read table which becomes garbage at
the end of the file.

If you don't do this, then *READ-TABLE* still points to the original readtable,
and you will be changing that. 

You need the EVAL-WHEN because most top level forms are not evaluated at
compile time.   (The IN-PACKAGE macro is special; the spec requires it to be
processed at compile time also).
From: Tobias C. Rittweiler
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <87tzc68xsu.fsf@freebits.de>
Didier Verna <······@lrde.epita.fr> writes:

>        Hello,
>
> I have a package which is ASDF installable and in which I'd like to use
> a personal dispatch macro character. This particular DMC should be
> active when the package files are compiled or evaluated (via ASDF, but
> ideally in other situations), but I don't want to pollute the rest of
> the lisp session with it.
>
> So my question is: what's the best way to do that?

Wait a few weeks. I recently begun to continue working on my
named-readtables project again which provides a namespace akin to the
package namespace but for readtables. See [1] for a short introduction. 

The code is available at

  http://common-lisp.net/~trittweiler/darcs/editor-hints/

ANSI CL unfortunately lacks a way to iterate through all reader-macros
within a readtable. The above source works with SBCL only for the
moment. What's your implementation of choice? You could give me a hand
implementing WITH-READTABLE-ITERATOR on all major Lisp implementations.

Adding the necessary support to Slime is also planned.

  -T.

[1] http://groups.google.com/group/comp.lang.lisp/msg/21d290d6b0d8238b?dmode=source
From: Didier Verna
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <muxzllwpnji.fsf@uzeb.lrde.epita.fr>
"Tobias C. Rittweiler" <···@freebits.de> wrote:

> Wait a few weeks. I recently begun to continue working on my
> named-readtables project again which provides a namespace akin to the
> package namespace but for readtables. See [1] for a short
> introduction.

  Thanks Tobias and Duane; I'll look into it.

-- 
Resistance is futile. You will be jazzimilated.

Scientific site:   http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bic�tre, France
Tel. +33 (0)1 44 08 01 85       Fax. +33 (0)1 53 14 59 22
From: Madhu
Subject: Re: [Q] package-specific dispatch macro character
Date: 
Message-ID: <m3hc84bc8g.fsf@moon.robolove.meer.net>
* Didier Verna <···············@uzeb.lrde.epita.fr> :
Wrote on Thu, 25 Sep 2008 11:44:49 +0200:

| "Tobias C. Rittweiler" <···@freebits.de> wrote:
|
|> Wait a few weeks. I recently begun to continue working on my
|> named-readtables project again which provides a namespace akin to the
|> package namespace but for readtables. See [1] for a short
|> introduction.
|
|   Thanks Tobias and Duane; I'll look into it.

You can also check Drew McDermott's stubs for missing allegro
functionality at
<URL:http://clocc.sourceforge.net/clocc/src/ytools/ytools.lsy>

--
Madhu