Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!eos!ptolemy!raymond From: raymond@ptolemy.arc.nasa.gov (Eric A. Raymond) Newsgroups: comp.lang.lisp Subject: Re: readmacros in CL: getting "[", "]" to work as "(", ")" Message-ID: <1053@ptolemy.arc.nasa.gov> Date: 15 Mar 89 02:33:33 GMT References: <424@soi.UUCP> <2848@kalliope.rice.edu> Reply-To: raymond@ptolemy.arc.nasa.gov (Eric A. Raymond) Organization: NASA Ames Research Center, Moffett Field, CA Lines: 45 In article <2848@kalliope.rice.edu> dorai@titan.rice.edu (Dorai Sitaram) writes: >Could someone please tell me how to define brackets ("[", "]") as >macro characters in CommonLisp so that they have the same effect as >the usual parens (but make for more readable code)? Here goes, but I bet you'll never use them if you let your editor do your formatting (and are too lazy to rewrite the indenter). ;;; This may reference LISPM specific code (defun bracket->list-reader (stream sub-char) "This allows you to use the characters [ and ] to delimit lists. This is especially useful when using nested list structures. (i.e. (let ([this 1] [that 2]) (+ this that)) ) One drawback is that the editor will not flash the opening/closing bracket like it will a parenthesis. Also, it will not catch unbalanced brackets or indent some special forms properly. Hint: Use brackets for inner most forms (i.e. (let ([a 1]) ) not (let [(a 1)])) Do NOT mix parenthesis and brackets." (declare (ignore sub-char)) (values (read-delimited-list #\] stream t))) (set-macro-character #\[ #'bracket->list-reader T) (set-macro-character #\] (get-macro-character #\))) Here's something for progn's: (defun curly-brace->progn-reader (stream sub-char) "This allows a shorthand for progn (ala C). Uses balance pairs of {} to delimit progns." (declare (ignore sub-char)) (list* 'progn (read-delimited-list #\} stream t))) (set-macro-character #\{ #'curly-brace->progn-reader T) ;;; This prevents } from being part of form (set-macro-character #\} (get-macro-character #\))) -- Eric A. Raymond (raymond@pluto.arc.nasa.gov) Nothing left to do but :-) :-) :-)