troff/nroff, traps

setting and removing page traps

.wh N xx
execute macro xx at vertical page position N. A negative N counts from the bottom of the page upwards. Overrides any previously installed trap at that location. The default scale for N is v, the distance between two lines.

Traps and .ms macros interfere in some weird way; in particular, I don't know how to cause traps on the very first page, even if my document doesn't have a cover sheet or title.

.wh N
pops a trap at N, if any.

changing a page trap position

.ch xx N
moves the trap for macro xx to page position N. On our implementation, it moves the first-installed such trap (of any number of traps that invoke the same macro). As with .wh, the default scale for N is v. When two traps are moved to the same position with .ch, they stay distinct and are both executed.
.ch xx
pops a trap for macro xx, if any.

distance to the next page trap

\\n(.t
the number register .t holds the distance to the next trap, or to the page bottom if there are no traps.

trap input line count

.it N xx
calls the macro xx after all N lines of text input have been read.

trap end-of-input

.em xx
calls the macro xx after all input has been read.

diversion trap

.dt N xx
Makes macro xx execute a distance of vertical N (default scale: v) from the start of the current diversion.