Subject: [clisp-list] Common Lisp and here-strings

From: Christian Schuhegger <[email-masked]>
Date: Fri Oct 31 03:52:02 2003

Hello,

i was wondering if there is a portable implementation of 
here-strings/here-documents for lisp like perl's:
print $OUT <<EOS;

I was looking on google but did not find any hints in that direction.

Thanks a lot!
-- 
Christian Schuhegger
http://galileo.spaceports.com/~cschuheg

---

From: Pascal J.Bourguignon <[email-masked]>
Date: Fri Oct 31 04:34:37 2003

> I was looking on google but did not find any hints in that direction.

You can implement this with a reader macro; read Chapter 2 Syntax and
Chapter 23 Reader of CLHS
http://www.lisp.org/HyperSpec/FrontMatter/Chapter-Index.html

(set-dispatch-macro-character
 (character "#") (character "\"")
 (lambda (stream subchar infix-param)
   (declare (ignore subchar infix-param))
   (do ((eoln (format nil "~%"))
        (eof-token (string-trim " " (read-line stream nil nil t)))
                                        ; eof-token is what's after #"
        (line      (read-line stream nil nil t)
                   (read-line stream nil nil t))
        (lines '()))
       ((or (null line)
            (string-equal eof-token (string-trim " " line)))
        (apply (function concatenate) 'string (nreverse lines)))
     (push line lines)
     (push eoln lines)) ))

[12]> #"EOF
First line " foo "
second line \bar\
EOF
"First line \" foo \"
second line \\bar\\
"

Beware,  there's a  problem if  you type  this in  ilisp in  emacs: it
handles the double-quote character without taking into account the new
macro, thus  you need to  add a double-quote  after the EOF (a  bug in
ilisp). But reading from files or from the terminal it's OK.

HOWEVER, I assume that what  you want is avoid escaping anti-slash and
double-quote, and that's all that  this macro would buy you, since you
can as well write normal  strings over several lines, and with FORMAT,
you have fine control on what to do about new-lines and prefix spaces:

[5]> "first line
second line
"
"first line
second line
"

[6]> (format nil "first line ~
           second line~
           ")
"first line second line"
[7]> (format nil "first line ~:
           second line~
           ")
"first line            second line"
[8]> (format nil "first line ~@
           second line~
           ")
"first line 
second line"

-- 
__Pascal_Bourguignon__
http://www.informatimago.com/

---

From: Christian Schuhegger <[email-masked]>
Date: Fri Oct 31 06:23:12 2003

Pascal J.Bourguignon wrote:
 > ...

Thanks a lot! This is what I want to do. It is useful for me when I want 
to print long runs of HTML text without escaping all the quotes.

---

From: Sam Steingold <[email-masked]>
Date: Fri Oct 31 07:10:50 2003

> * Christian Schuhegger <[email-masked]> [2003-10-31 15:18:37 +0100]:
>
> Thanks a lot! This is what I want to do. It is useful for me when I
> want to print long runs of HTML text without escaping all the quotes.

You can use single quotes instead and avoid escaping.
E.g., <a href='http://clisp.cons.org'>CLISP</a>

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
There's always free cheese in a mouse trap.