#!/usr/bin/snobol4 -b 
# vi:ts=8:sw=8:wm=0:ai:
*
* experimental noweb filter to prettyprint comments in LaTeX
* (currently works only for //-style -- ended by newline -- comments)
*
* usage: pretty-comment [ comment-start-string [ comment-typeset-string ] ]
* (default equiv to "pretty-comment // '/\negthinspace\negthinspace/'")
*
	DEFINE('DQ(S)TEXT,TAIL')
*
	CODEPAT = POS(0) '@' ('begin code' | 'quote' ) . INCODE
	ENDPAT = POS(0) '@end' ('quote' | ' code')
+		NULL . INCODE NULL . INCOMMENT
	TEXTPAT = '@text ' REM . TEXT
	NLPAT = '@nl' NULL . INCOMMENT
	NEWLINE = CHAR(10)
* check for optional command-line args
	FIRSTARG = HOST(3)
	CMNTCHARS = DIFFER(HOST(2,FIRSTARG)) HOST(2,FIRSTARG)	:F(SETCMNT)
	CMNTTYPESET = CMNTCHARS
	CMNTTYPESET = DIFFER(HOST(2,FIRSTARG + 1)) HOST(2,FIRSTARG + 1)
* set comment chars & typsetting appropriately
SETCMNT	CMNTCHARS = IDENT(CMNTCHARS) '//'
	CMNTTYPESET = IDENT(CMNTTYPESET) '/\negthinspace\negthinspace/'
	COMMENTPAT = CMNTCHARS . INCOMMENT REM . COMMENT
	COMMENTSTART = NEWLINE '@literal ' CMNTTYPESET '\hbox{\setupmodname{}'
	COMMENTRESTART = NEWLINE '@literal \hbox{\setupmodname{}'
*
READ	INLINE = INPUT					:F(END)
* @begin code -- now in code chunk (see pattern)
	INLINE CODEPAT					:S(PRINT)
* @nl or @end within a comment -- end the comment (see pattern)
	INLINE DIFFER(INCOMMENT) (NLPAT | ENDPAT) . TEXT
+		= '@literal }' NEWLINE TEXT		:S(PRINT)
* @end code -- no longer in code chunk (see pattern)
	INLINE ENDPAT					:S(PRINT)
* pass through other lines or @text not in code chunks
	INLINE DIFFER(INCODE) TEXTPAT		 	:F(PRINT)
* @text line in code chunk
*   inside a comment -- make @text @literal
	OUTPUT = DIFFER(INCOMMENT) '@literal ' DQ(TEXT)	:S(READ)
*   outside a comment -- with a comment on line
	INLINE COMMENTPAT = COMMENTSTART DQ(COMMENT)	:S(PRINT)
*   outside a comment -- leave alone
PRINT	OUTPUT = INLINE					:(READ)
*
* Function to deal with [[...]] inside a comment
* (all we need do is replace [[ with }, ]] with COMMENTSTART
* and put the stuff in between in an @text line
DQ	DQ = S
DQLOOP	DQ '[[' ARB . TEXT ']]' (NOTANY(']') | RPOS(0)) . TAIL
+		= '}' NEWLINE '@text ' TEXT NEWLINE COMMENTRESTART TAIL
+							:S(DQLOOP)F(RETURN)
END
