[comp.lang.lisp] Looking for "literate programming" tools for Scheme/LISP

plogan@mentor.com (Patrick Logan) (10/16/89)

(1) Does someone have WEB or equivalent set up for Scheme or another LISP?

(2) Do you know of any references to "literate programming" applied to
Scheme/LISP?

Thanks.
-- 
Patrick Logan                | ...!{decwrl,sequent,tessi}!mntgfx!plogan 
Mentor Graphics Corporation  | plogan@pdx.MENTOR.COM                    
Beaverton, Oregon            |

pk@tut.fi (Kellom{ki Pertti) (10/19/89)

On 16 Oct 89 15:32:32 GMT,plogan@mentor.com (Patrick Logan) said:
Patrick> (1) Does someone have WEB or equivalent set up for Scheme or another LISP?
Patrick> (2) Do you know of any references to "literate programming" applied to
Patrick> Scheme/LISP?

I asked the same thing a while back, and this is the answer I got.
I've used SchemeTex quite a bit and I'm quite satisfied with it. If
you are interested, I've hacked it a bit to be used with GNU Texinfo,
and also cooked up some Emacs lisp support for dealing with SchemeTex
files. It is not at all ready to be released, but if you want to take
look at it, I'll be happy to share.

dorab> From: dorab@CS.UCLA.EDU (Dorab Patel)
...
dorab> i dont know if this helps, but  ....
dorab> From: John D. Ramsdell <ramsdell%linus@mitre-bedford.ARPA>
dorab> Date: Mon, 18 Apr 88 09:22:39 EDT
dorab> Message-Id: <8804181322.AA11583@darwin.sun.uucp>
dorab> To: scheme@mc.lcs.mit.edu, texhax@score.stanford.edu
dorab> Subject: SchemeTeX---Simple support for literate programming in Lisp.
dorab> SchemeTeX provides simple support for literate programming in any
dorab> dialect of Lisp on Unix.  Originally created for use with Scheme, it
dorab> defines a new source file format which may be used to produce LaTeX
dorab> code or Lisp code.  Roughly speaking, LaTeX formats Lisp code in a
dorab> verbatum-like environment, and it formats Lisp comments in an ordinary
dorab> environment.

dorab> SchemeTeX is available via anonymous FTP from linus (192.12.120.51) in
dorab> the shar file named "pub/schemeTeX.sh".  Included is an operating
dorab> system independent version for the T dialect of Lisp.

dorab> John
--
Pertti Kellom\"aki (TeX format)  #       These opinions are mine, 
  Tampere Univ. of TeXnology     #              ALL MINE !
      Software Systems Lab       #  (but go ahead and use them, if you like)

sra@ecs.soton.ac.uk (Stephen Adams) (10/23/89)

In article <1989Oct16.153232.19051@mentor.com> plogan@mentor.com
(Patrick Logan) writes:

   (1) Does someone have WEB or equivalent set up for Scheme or another LISP?

   (2) Do you know of any references to "literate programming" applied to
   Scheme/LISP?

I have a solution for Common lisp.  The same idea should work for most
lisps.  I use the following code to read in latex files that have bits
of lisp code embedded in `lisp' or `program' environments.  It works
by redefining the readtable so that

  1.  Latex comments are treated as comments in lisp .to treat latex
      comment as lisp comments.

  2.  Anything starting with a `\' is a comment until the start of a
      `lisp' or `program' environment.

It has been used under Sun Common Lisp (Lucid) and Kyoto Common Lisp.

I think that a full-blown WEB equivalent for lisp is a waste of time.
Much of what WEB offers is there to circumvent weaknesses in the host
language.  Many lisp programs are declaration order independent (or
should be if you want to compile them).  If you want to refine
something later functions and macros can be used.

Cheers


---- cut here, put it in latex.lisp -----
;
;   Literate programming for common lisp and latex
;
;   Only those bits of program in a `lisp' or `program' environment
;   are loaded.  This is achieved as follows:
;
;   1.  `%' (the tex comment character) is make to act exactly like lisp's
;       semicolon
;   2.  `\' is make to skip text until a line containing only
;           \begin{lisp}
;       or  \begin{program}
;       The skipped text including the \begin bit is treated as a comment.
;   3.  Devious code make the character dispatch read macro (#\... )
;       still work.  I hope.
;
;   
;   Re-building this file:
;   On both lucid and kcl there are problems with overwriting the readtable
;   while loading.  It only seems to work properly if the functions are
;   compiled and the readtable is updated in one operation.  But that is
;   a one-legged-chicken theory.
;
;   To compile:
;       Start lucid or KCL.  (if you are already in lisp, exit & restart it)
;       (compile-file "latex.lsp")
;       either (bye) [in kcl] or (quit) [in lucid]
;       Dont be tempted to load latex.{o,lbin} after compiling. It doesnt work.
;
;   At any other time it is reasonable to load the compiled stuff.
;


(eval-when (compile)  (proclaim '(optimize (safety 2))))


(defvar *default-hash-backslash-function*
    (get-dispatch-macro-character #\# #\\))
    
(defun  install-latex-readtable ()

  (labels (
    (backslash-mystifier (stream ch &aux line)
        (unread-char ch stream)
        (setf line (read-line stream  nil nil t))
        (loop
;;;;        (format t "---> ~S~%" line)
            (if (equal (peek-char nil stream nil 'T) 'T) (return (values)))
            (setf line (read-line stream nil nil t))
            (if (or (null line)
                  (string-equal line "\\begin{lisp}")
                  (string-equal line "\\begin{program}") )
                (return (values)))
        )
    )

    (grotty-new-character-reader (stream ch prefix &aux name (result nil))
        (unwind-protect
            (progn
                (set-syntax-from-char #\\ #\\)
                (set-dispatch-macro-character #\# #\\
                    *default-hash-backslash-function*)
                (unread-char ch stream)
                (setf name (read stream nil nil nil))
                (setf prefix (if prefix (format nil "~D" prefix) ""))
                (setf result (read-from-string
                            (format nil "#~A\\~A" prefix name)))
            )
            (progn
                (set-macro-character #\\ #'backslash-mystifier nil)
                (set-dispatch-macro-character  #\#  #\\
                    #'grotty-new-character-reader)
                result))
    ))


   (let ((newtable (copy-readtable nil)))

        (set-syntax-from-char #\% #\; newtable newtable)
        (set-macro-character #\\ #'backslash-mystifier nil newtable)
        (set-dispatch-macro-character  #\#  #\\  #'grotty-new-character-reader
            newtable)
        (setf *readtable* newtable)
)))


(eval-when (load) (install-latex-readtable))

--
Stephen Adams                          S.Adams@uk.ac.soton.ecs (JANET)
Computer Science                       S.Adams@ecs.soton.ac.uk (Bitnet)
Southampton S09 5NH, UK                S.Adams@sot-ecs.uucp    (uucp)

jeff@aiai.ed.ac.uk (Jeff Dalton) (10/24/89)

A while ago, I prepared some really simple tools for processing files
that were both documents and source code.  The basic idea is to use a
verbatim environment (with *no* interesting processing) for code, but
under a different name.  Consequently, it will work for any programming
language, but will never do any fancy formatting.

The idea is this:  You write a ".ptex" file.  Two "projections" are
defined for such files: one converts a ".ptex" file to a ".tex" file;
the other converts it to a ".lsp" (or whateverP file.  Two new
environments are defined:

   program  --  converted directly to verbatim for ".tex";
                contents outout as-is for ".lsp".

   program-only -- like program, but discarded for ".tex"

All that's required are two AWK scripts and some entries in a
Makefile:

-------------------- Makefile entries --------------------

.SUFFIXES: .ptex .tex .lisp .lsp

.ptex.tex: ; awk -f totex < $*.ptex > $*.tex

.ptex.lsp: ; awk -f tocode < $*.ptex > $*.lsp

.ptex.lisp: ; awk -f tocode < $*.ptex > $*.lisp

-------------------- totex script --------------------
BEGIN { state="copy" }

state == "copy" && /^ *\\begin{program}/       {print "\\begin{verbatim}"; break}
state == "copy" && /^ *\\end{program}/         {print "\\end{verbatim}"; break}
state == "copy" && /^ *\\begin{program-only}/  {state="ignore"; break}
state == "copy"                                {print; break}

/^ *\\end{program-only}/                       {state = "copy"; break}
-------------------- tocode script --------------------
BEGIN { state="ignore" }

state == "copy" && /^ *\\end{program}/         {state = "ignore"; printf "\n"; break}
state == "copy" && /^ *\\end{program-only}/    {state = "ignore"; printf "\n"; break}
state == "copy"                                {print; break}

/^ *\\begin{program-only}/                     {state = "copy"; break}
/^ *\\begin{program}/                          {state = "copy"; break}
-------------------- end --------------------

Simple, but can be effective.

Jeff Dalton,                      JANET: J.Dalton@uk.ac.ed             
AI Applications Institute,        ARPA:  J.Dalton%uk.ac.ed@nsfnet-relay.ac.uk
Edinburgh University.             UUCP:  ...!ukc!ed.ac.uk!J.Dalton

plogan@mentor.com (Patrick Logan) (10/24/89)

In article <SRA.89Oct22205835@alonzo.ecs.soton.ac.uk> sra@ecs.soton.ac.uk (Stephen Adams) writes:
  >> I think that a full-blown WEB equivalent for lisp is a waste of time.
  >> Much of what WEB offers is there to circumvent weaknesses in the host
  >> language.  Many lisp programs are declaration order independent (or
  >> should be if you want to compile them).  If you want to refine
  >> something later functions and macros can be used.

I came to the same conclusion. A couple of people also mailed similar
solutions. What I'm planning on now is to create my own with texinfo.
This way, there'll be a printed document, executable code, and an
"info" version of the document. It should facilitate a nice Scheme
library.

If anyone wants the finished product, send mail and I'll hold on to
your address. I'm not even going to guess at when I'll complete it.

Thanks very much to everyone who responded. I appreciate it.
-- 
Patrick Logan                | ...!{decwrl,sequent,tessi}!mntgfx!plogan 
Mentor Graphics Corporation  | plogan@pdx.MENTOR.COM                    
Beaverton, Oregon            |