[comp.text] A Troff Challenge

simpson%trwarcadia.uucp@usc.edu (Scott Simpson) (05/11/89)

I didn't get a satisfactory answer last time I posed this question so
now I will turn it into a challenge to see if I get a better response. :-)
Can you implement TeX's equivalent of an \halign in troff?  What I want
to do is create hanging tags where the longest hanging tag is the width
of the paragraph indent.  For example, I would like to get the output

	/etc/motd	     The  message  of the day.  Continuation lines
			     such as these continue here.
	/usr/lib/tmac/tmac.s The  -ms  macros.   Notice  that  the longest 
			     hanging tag is used as the indent for all the
			     paragraphs
	
Also, how does the net world debug troff macros?  The .tm request helps
somewhat but it is always executed as soon as it read and you cannot delay
its output to print out when the macro is executed, not when it is defined.
		Scott Simpson

Here is my non-working (but close) solution to the above problem.  I don't
know why it doesn't work.  I'm using Sun OS 3.5 [nt]roff.

.\" %W% %G%
.\" General purpose macros.	Scott Simpson.
.\" Macros provided:
.\"	.FB file_name		Begin a File Section line in the -man
.\"				macros.  For example, in the -man section
.\"				where you say .SH FILES, you could say
.\"					.FB /etc/motd
.\"					The message of the day.
.\"					.FE
.\"					.FS
.\"				to format the file line.  .TP inserts a 
.\"				blank line so we don't use it.  Each .FB
.\"				must end with a .FE and a final .FS must end
.\"				the last .FB-.FE pair.  The widest tag is
.\"				computed and used for all the .FS-.FE pairs.
.\"	.FE			Terminates an .FB command.
.\"	.FS			Flushes a file section.
.\"
.\" This file uses the following:
.\"	Number registers:
.\"		mw	maximum width of a hanging tag
.\"	Macros, diversions and strings:
.\"		FD	diversion for .FB, .FE, .FS
.\"
.nr mw 0	\"indent size of hanging tag
.de FB		\"File Begin section
.if \w'\\$1'u+1m>\\n(mw .nr mw \w'\\$1'u+1m
.nf		\"get output in no-fill mode
.da FD		\"start diversion
.ft R		\"put environment in sane state
.ps 10
.vs 12p
.br		\"start a new paragraph
.ti -\\n(mwu	\"dedent
\\$1\\t\c
..
.de FE		\"File section End
.di		\"end diversion
..
.de FS		\"Flush Section
.in +\\n(mwu	\"set indentation to size of largest tag
.ta 0		\"set tab to indent size
.fi		\"set fill mode
.FD		\"output diversion
.\" Adding a comment to the end of the next line causes an infinite loop!
.\" Remove the macro request.
.rm FD
.in -\\n(mwu	\"dedent indent
.nr mw 0	\"reset hanging tag size
..
	Scott Simpson
	TRW Space and Defense Sector
	oberon!trwarcadia!simpson  	(UUCP)
	trwarcadia!simpson@usc.edu	(Internet)

simpson@minotaur.uucp (Scott Simpson) (05/12/89)

Whoa, ignore the challenge.  I received Mark Brader's reply soon after my
posting.  See his message for a solution.
	Scott Simpson
	TRW Space and Defense Sector
	oberon!trwarcadia!simpson  	(UUCP)
	trwarcadia!simpson@usc.edu	(Internet)

dns@sq.com (David Slocombe) (05/15/89)

In article <4971@wiley.UUCP>
simpson%trwarcadia.uucp@usc.edu (Scott Simpson) writes:

>Also, how does the net world debug troff macros?  The .tm request helps
>somewhat but it is always executed as soon as it read and you cannot delay
>its output to print out when the macro is executed, not when it is defined.

The .tm request, like any other troff request that is read in as
part of the body of a macro, is not executed until the macro is
called.  However, note that number-register and string references
get evaluated at definition-time unless you "defer" their evaluation
by extra backslashes.  Hence the content of the message put out by
.tm may reflect the situation at macro-definition time or at
macro-execution time, depending on the number of backslashes on
the \n and \* escape sequences in the message.  One backslash means
the escape sequence is evaluated immediately (as the macro is being
defined), while two backslashes mean it will be evaluated as the
macro is being executed.  (Complex circumstances can require four
or even eight backslashes.)

The sqtroff execution trace of the following simple troff input
illustrates this point:

	.nf
	.nr x 22
	.tm The value of x is \nx at start.
	.de YY
	.tm Value of x is \nx at macro definition but \\nx at execution.
	Let's do it in text too: \nx at definition, \\nx at execution.
	..
	.nr x \nx+11  \" 22 + 11 == 33
	.tm The value of x is now \nx, after macro definition and before
	.tm the execution of the macro.
	.YY
	.ex
	
	
We run this through sqtroff with call-trace enabled (-C) on the
command line, although with real macro packages we would place
".ctrace on" and ".ctrace off" lines in the macros of immediate
interest.  (Lines beginning with "% T" are trace-lines.  The actual
outputs of the .tm requests are marked with "stderr->".  The rest of
the lines are normal intermediate output):

	$ sqtroff -C -Tps dns 2>&1 | more
	X ps 300 1 1
	% T *.STARTUP 8.5 11 @ 21u 17u 56u 25u 8i 10.7i
	Y P default letter 2550 3300 21 17 56 25 2400 3210
	% T *.ds .pageinfo "P default letter 2550 3300 21 17 56 25 2400 3210
	% T *.ds .T "ps
	P 1
	% T *.nf 
	% T *.nr x 22
	% T *.tm The value of x is 22 at start.
stderr->The value of x is 22 at start.
	% T *.de YY
	% T *.nr x 22+11
	% T *.tm The value of x is now 33, after macro definition and before
stderr->The value of x is now 33, after macro definition and before
	% T *.tm the execution of the macro.
stderr->the execution of the macro.
	% T .YY (CALL) 
	% T .YY *.tm Value of x is 22 at macro definition but 33 at execution.
stderr->Value of x is 22 at macro definition but 33 at execution.
	V 50
	O 289
	w 12
	L 1950
	I 0
	N n l 11 1032
	f 10 TR
	k 0 1
	D w 0
	= (Let's do it in text too: 22 at de(fi)nition, 33 at execution.)
	n
	% T .YY (RETURN) 
	% T *.ex 
	p 3300

----------------------------------------------------------------
David Slocombe				(416) 963-8337
SoftQuad Inc.				(800) 387-2777 (from U.S. only)
720 Spadina Ave.			uucp: {uunet,utzoo}!sq!dns
Toronto, Ontario, Canada M5S 2T9	Internet: dns@sq.com