# -me-specific stuff for tc2html

# Need:
# - +c
# - (c, )c
# - default is different for (z than for (l, (b.
# - .sh should number headings

# this helps to map -me macros onto tc2hmtl concepts

# When a name is redefined below, it's first removed.  This is because groff
# aliases many of the macros to an error macro until .TL is seen.  Unless
# a given name is removed before being redefined, redefining it would also
# redefine all the other names that are aliases for it!

# define an internal-use-only request to reset the paragraph state

req reset*para*params eol \
		break fill adjust b font R \
		point-size 10 spacing 12 line-spacing 1

# define a request for resetting the numbered-paragraph register, and invoke
# the request immediately

req reset*para*num eol \
	push-string ".nr \$p 0 1\n"
imm push-string ".reset*para*num\n"

imm remove-name n lp
req lp parse-macro-args eol \
		push-string ".reset*para*params\n" \
		push-string ".reset*para*num\n" \
		push-string ".H*para\n" font "R"

imm remove-name n pp
imm alias-macro pp lp

# write indented paragraphs as a definition list
imm remove-name n ip
req ip parse-macro-args eol \
		push-string ".reset*para*params\n" \
		push-string ".H*dterm\n" font "R" \
		push-string "$1" \
		push-string ".H*ddesc\n"

# write bulleted paragraphs as an indented paragraph with a bullet tag
imm remove-name n bu
req bu parse-macro-args eol \
		push-string ".ip \\(bu\n"

# write numbered paragraphs as an indented paragraph with a number tag
imm remove-name n np
req np parse-macro-args eol \
		push-string ".ip (\\n+(\$p)\n"

imm remove-name n RS
req RS parse-macro-args eol \
		break \
		push-string ".H*shift*right\n"

imm remove-name n RE
req RE parse-macro-args eol \
		break \
		push-string ".H*shift*left\n"

# if the first argument is < 1, use "1".  Then generate, for argument
# N, a level-N TOC entry and a level-(N+1) header using the second argument
# for the header text.

imm remove-name n sh
req sh parse-num x parse-macro-args eol \
		push-string ".reset*para*params\n" \
		push-string ".reset*para*num\n" \
		push-string ".if $1<1 .nr H*sh*level 1 1\n" \
		push-string ".if $1>0 .nr H*sh*level $1 1\n" \
		push-string ".H*atoc \\n[H*sh*level]\n" \
		push-string ".H*header \\n+[H*sh*level]\n" \
		push-string "$2\n" \
		push-string ".H*header*end\n" \
		push-string ".H*aend\n"

# .uh T is like .sh 1 T
imm remove-name n uh
req uh parse-macro-args eol \
		push-string '.sh 1 "$1"\n'

imm remove-name n (q
req (q parse-macro-args eol \
		push-string ".reset*para*params\n" \
		push-string ".H*bq" font "R"

imm remove-name n )q
req )q parse-macro-args eol \
		push-string ".reset*para*params\n" \
		push-string ".H*bq*end" font "R"

# Begin a block as a display.  Argument is the display indent.

req H*me*list parse-macro-args eol \
		push-string ".reset*para*params\n" \
		nofill \
		push-string ".H*disp*indent $1\n" \
		push-string ".H*disp" font "R"

# End a block display

req H*me*list*end parse-macro-args eol \
		push-string ".H*disp*end" \
		push-string ".reset*para*params\n"

# Begin a block. Arguments are:
# $1 = Default display for no-fill case
# $2 = Default display for fill case
# $3, $4 = The m and f arguments to the calling macro, as those argument
# are explained in the -me reference.
#
# H*me*list*type indicates the list type:
# 0 = regular list, indented
# 1 = regular list, not indented
# 2 = center line-by-line
# 3 = just like .lp

req H*me*block parse-macro-args eol \
		push-string ".nr H*me*list*type $1\n" \
		push-string ".if '$3'' .nr H*me*list*type $1\n" \
		push-string ".if '$3'F' .nr H*me*list*type $2\n" \
		push-string ".if '$3'' .if '$4'F' .nr H*me*list*type $2\n" \
		push-string ".if '$3'C' .nr H*me*list*type 2\n" \
		push-string ".if '$3'I' .nr H*me*list*type 0\n" \
		push-string ".if '$3'I' .if '$4'F' .nr H*me*list*type 3\n" \
		push-string ".if '$3'L' .nr H*me*list*type 1\n" \
		push-string ".if '$3'L' .if '$4'F' .nr H*me*list*type 3\n" \
		push-string ".if '$3'M' .nr H*me*list*type 1\n" \
		push-string ".if '$3'M' .if '$4'F' .nr H*me*list*type 3\n" \
		push-string ".if \\n[H*me*list*type]=0 .H*me*list 3\n" \
		push-string ".if \\n[H*me*list*type]=1 .H*me*list 0\n" \
		push-string ".if \\n[H*me*list*type]=2 .ce 9999\n" \
		push-string ".if \\n[H*me*list*type]=3 .lp\n"

# End a block.  (This assumes blocks aren't nested.)

req H*me*block*end eol \
		push-string ".if \\n[H*me*list*type]=0 .H*me*list*end\n" \
		push-string ".if \\n[H*me*list*type]=1 .H*me*list*end\n" \
		push-string ".if \\n[H*me*list*type]=2 .ce 0\n" \
		push-string '.if \\n[H*me*list*type]=3 .\\" nothing to do\n'

imm remove-name n (b
req (b parse-macro-args eol push-string ".H*me*block 0 3 $1 $2\n"
imm remove-name n )b
req )b eol push-string ".H*me*block*end\n"

imm remove-name n (l
imm alias-macro (l (b
imm remove-name n )l
imm alias-macro )l )b

imm remove-name n (z
req (z parse-macro-args eol push-string ".H*me*block 1 3 $1 $2\n"
imm remove-name n )z
req )z eol push-string ".H*me*block*end\n"

# Footnote support.  All these versions of .(f/.)f do is bracket the
# footnote text with "[[footnote:" and "]]" so that at least the
# footnote can be recognized for what it is.

imm remove-name n (f
req (f eol push-string "[[footnote:\n"

imm remove-name n )f
req )f eol push-string "]]\n"
