3935872ed2a6d8068014e94e78607451b7b49511
[mmh] / man / mh-format.man5
1 .\"
2 .\" %nmhwarning%
3 .\"
4 .TH MH-FORMAT %manext5% "%nmhdate%" MH.6.8 [%nmhversion%]
5 .SH NAME
6 mh-format \- format file for mh message system
7 .SH DESCRIPTION
8 Several
9 .B mmh
10 commands utilize either a
11 .I format
12 string or a
13 .I format
14 file during their execution.  For example,
15 .B scan
16 uses a format string which directs it how to generate the scan listing
17 for each message;
18 .B repl
19 uses a format file which directs it
20 how to generate the reply to a message, and so on.
21 .PP
22 There are a few alternate scan listing formats available, e.g.
23 .IR scan.nmh ,
24 .IR scan.mailx ,
25 and
26 .IR scan.timely .
27 Look in
28 .I %etcdir%
29 for other
30 .B scan
31 and
32 .B repl
33 format files.
34 .PP
35 This manual section explains how to write and modify format commands.
36 Note: familiarity with the C
37 .B printf
38 routine is assumed.
39 .PP
40 A format string consists of ordinary text, and special multi-character
41 escape sequences which begin with `%'.  When specifying a format
42 string, the usual C backslash characters are honored: `\\b', `\\f',
43 `\\n', `\\r', and `\\t'.  Continuation lines in format files end with
44 `\\' followed by the newline character.
45
46 .\" TALK ABOUT SYNTAX FIRST, THEN SEMANTICS
47 .SS SYNTAX
48 Format strings are built around
49 .IR "escape sequences" .
50 There are four types of escape sequences:
51 .PP
52 .RS 5
53 .nf
54 .ta +\w'name of escape class xxxxxxx'u
55 .RI "1)  header components      %{" component }
56 .RI "2)  built-in functions     %(" "function arg" )
57 .RI "3)  flow control   %< ... %? ... %| ... %>
58 .RI "4)  comments       %; ...
59 .fi
60 .RE
61 .PP
62 Comments may be inserted in most places where no function argument is
63 expected.  A comment begins with `%;' and ends with a (non-escaped)
64 newline.
65 .PP
66 A
67 .I component
68 escape is specified as
69 .RI `%{ component }',
70 and
71 exists for each header found in the message being processed.  For example
72 .RI `%{ date }'
73 refers to the `Date:' field of the appropriate message.
74 All component escapes have a string value.  Normally, component values are
75 compressed by converting any control characters (tab and newline included)
76 to spaces, then eliding any leading or multiple spaces.  However, commands
77 may give different interpretations to some component escapes; be sure
78 to refer to each command's manual entry for complete details.
79 .PP
80 A
81 .I function
82 escape is specified as
83 .RI `%( function )'.
84 All functions are built-in, and most have a string or numeric value.
85 A function escape may have an
86 .IR argument .
87 The argument follows the function escape: separating
88 whitespace is discarded:
89 .RI `%( function " " argument )'.
90 .PP
91 In addition to literal numbers or strings,
92 the argument to a function escape can be another function, a component,
93 or a control escape.  When the argument is a function or a
94 component, they are listed without a leading `%'.  When control escapes
95 are used as function arguments, they written as normally, with
96 a leading `%'.
97
98 .SS "Control escapes"
99 .PP
100 A
101 .I control
102 escape is one of: `%<', `%?', `%|', or `%>'.
103 These are combined into the conditional execution construct:
104 .PP
105 .RS 5
106 .nf
107 .RI "%< " condition " " "format-text"
108 .RI "%? " condition " " "format-text"
109     \&...
110 .RI "%| " "format-text"
111 %>
112 .fi
113 .RE
114 .PP
115 (Extra white space is shown here only for clarity.)  These
116 constructs may be nested without ambiguity.  They form a general
117 .B if\-elseif\-else\-endif
118 block where only one of the
119 format-texts
120 is interpreted.  In other
121 words, `%<' is like the "if", `%?' is like the "elseif", `%|' is like
122 "else", and `%>' is like "endif".
123 .PP
124 A `%<' or `%?' control escape causes its condition to be evaluated.
125 This condition is a
126 .I component
127 or
128 .IR function .
129 For integer valued functions or components, the condition is true
130 if the function return or component value is non-zero, and false if zero.
131 For string valued functions or components, the condition is true
132 if the function return or component value is
133 a non-empty string, and false for an empty string.
134
135 .PP
136 The `%?' control escape is optional, and there may be more
137 than one `%?' control escape in a conditional block.
138 The `%|' control escape
139 is also optional, but may be included at most once.
140
141 .SS "Function escapes"
142 Functions expecting an argument generally
143 require an argument of a particular type.
144 In addition to the number and string types,
145 these include:
146 .PP
147 .RS 5
148 .nf
149 .ta +\w'Argument 'u +\w'An optional component, 'u
150 .I "Argument    Description     Example Syntax
151 literal A literal number        %(\fIfunc\fR 1234)
152         or string               %(\fIfunc\fR text string)
153 comp    Any component           %(\fIfunc\fR\^{\fIin-reply-to\fR\^})
154 date    A date component        %(\fIfunc\fR\^{\fIdate\fR\^})
155 addr    An address component    %(\fIfunc\fR\^{\fIfrom\fR\^})
156 expr    Nothing %(\fIfunc\fR)
157         or a subexpression      %(\fIfunc\fR\^(\fIfunc2\fR\^))
158         or control escape       %(\fIfunc\fR %<{\fIreply-to\fR\^}%|%{\fIfrom\fR\^}%>)
159 .fi
160 .RE
161 .PP
162 The types
163 .I date
164 and
165 .I addr
166 have the same syntax as
167 .IR comp ,
168 but require that the header component be a date string, or address
169 string, respectively.
170 .PP
171 Most arguments not of type
172 .IR expr
173 are required.
174 When escapes are nested (via expr arguments), evaluation is done from inner-most to outer-most.
175 As noted above, for the
176 expr
177 argument type,
178 functions and components are written without a
179 leading `%'.
180 Control escape arguments must use a leading `%', preceded by a space.
181 .PP
182 For example,
183 .PP
184 .RS 5
185 .nf
186 %<(mymbox{from}) To: %{to}%>
187 .fi
188 .RE
189 .PP
190 writes  the  value of the header component `From:' to the
191 internal register named str; then (\fImymbox\fR\^) reads str and
192 writes its result to the internal register named
193 .IR num ;
194 then the control escape evaluates
195 .IR num .
196 If
197 .IR num
198 is non-zero, the
199 string `To:' is printed  followed  by  the  value  of  the
200 header component `To:'.
201 .SS Evaluation
202 The evaluation of format strings is performed
203 by a small virtual machine.
204 The machine is capable of evaluating nested expressions
205 as described above, and in addition
206 has an integer register
207 .IR num ,
208 and a text string register
209 .IR str .
210 When a function escape that
211 accepts an optional argument is processed,
212 and the argument is not present, the current value of either
213 .I num
214 or
215 .I str
216 is used as the argument: which register is
217 used depends on the function, as listed below.
218 .\"  What is the difference between these two lines:
219 .\"      %(void{comp})%(trim)%(putstr)
220 .\"      %(putstr(trim{comp}))
221 .\"  The latter can be used as a single expression for %<.
222 .\"  It does make a difference for (decode) because in the former
223 .\"  way, wrapping (decode) with (void) can be necessary.
224 .\"  What is the preferred way?
225 .PP
226 Component escapes write the value of their message header in
227 .IR str .
228 Function escapes write their return value in
229 .I num
230 for functions returning integer or boolean values, and in
231 .I str
232 for functions returning string values.  (The boolean type is a subset
233 of integers with usual values 0=false and 1=true.)  Control escapes
234 return a boolean value, setting
235 .I num
236 to 1 if the last explicit condition
237 evaluated by a `%<' or `%?' control
238 succeeded, and 0 otherwise.
239 .PP
240 All component escapes, and those function escapes which return an
241 integer or string value, evaluate to their value as well as setting
242 .I str
243 or
244 .IR num .
245 Outermost escape expressions in
246 these forms will print
247 their value, but outermost escapes which return a boolean value
248 do not result in printed output.
249 .SS Functions
250 The function escapes may be roughly grouped into a few categories.
251 .PP
252 .RS 5
253 .nf
254 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
255 .I "Function    Argument   Result       Description
256 msg             integer message number
257 cur             integer message is current (0 or 1)
258 unseen          integer message is unseen (0 or 1)
259 size            integer size of message
260 strlen          integer length of \fIstr\fR
261 width           integer output buffer size in bytes
262 charleft                integer bytes left in output buffer
263 timenow         integer seconds since the UNIX epoch
264 me              string  the user's mailbox
265 eq      literal boolean \fInum\fR == \fIarg\fR
266 ne      literal boolean \fInum\fR != \fIarg\fR
267 gt      literal boolean \fInum\fR > \fIarg\fR
268 match   literal boolean \fIstr\fR contains \fIarg\fR
269 amatch  literal boolean \fIstr\fR starts with \fIarg\fR
270 plus    literal integer \fIarg\fR plus \fInum\fR
271 minus   literal integer \fIarg\fR minus \fInum\fR
272 divide  literal integer \fInum\fR divided by \fIarg\fR
273 modulo  literal integer \fInum\fR modulo \fIarg\fR
274 num     literal integer Set \fInum\fR to \fIarg\fR.
275 num             integer Set \fInum\fR to zero.
276 lit     literal string  Set \fIstr\fR to \fIarg\fR.
277 lit             string  Clear \fIstr\fR.
278 getenv  literal string  Set \fIstr\fR to environment value of \fIarg\fR
279 profile literal string  Set \fIstr\fR to profile component \fIarg\fR
280                         value
281 .\" dat literal int     return value of dat[arg]
282 nonzero expr    boolean \fInum\fR is non-zero
283 zero    expr    boolean \fInum\fR is zero
284 null    expr    boolean \fIstr\fR is empty
285 nonnull expr    boolean \fIstr\fR is non-empty
286 void    expr            Set \fIstr\fR or \fInum\fR
287 comp    comp    string  Set \fIstr\fR to component text
288 compval comp    integer Set \fInum\fR to `\fBatoi\fR(\fIcomp\fR\^)'
289 .\" compflag    comp    integer Set \fInum\fR to component flags bits (internal)
290 .\" decodecomp  comp    string  Set \fIstr\fR to RFC-2047 decoded component text
291 decode  expr    string  decode \fIstr\fR as RFC-2047 (MIME-encoded)
292                         component and print it
293 unquote expr    string  remove RFC-2822 quotes from \fIstr\fR
294 unmailto        expr    string  remove `mailto:' and < > from \fIstr\fR
295 trim    expr            trim white-space from \fIstr\fR
296 putstr  expr            print \fIstr\fR
297 putstrf expr            print \fIstr\fR in a fixed width
298 putnum  expr            print \fInum\fR
299 putnumf expr            print \fInum\fR in a fixed width
300 nodate  string  integer Argument not a date string (0 or 1)
301 formataddr      expr            append \fIarg\fR to \fIstr\fR as a
302                         (comma separated) address list
303 putaddr literal         print \fIstr\fR address list with
304                         \fIarg\fR as optional label;
305                         get line width from \fInum\fR
306 .fi
307 .RE
308 .PP
309 The following functions require a date component as an argument:
310 .PP
311 .RS 5
312 .nf
313 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
314 .I "Function    Argument        Return  Description
315 sec     date    integer seconds of the minute
316 min     date    integer minutes of the hour
317 hour    date    integer hours of the day (0-23)
318 wday    date    integer day of the week (Sun=0)
319 day     date    string  day of the week (abbrev.)
320 weekday date    string  day of the week
321 sday    date    integer day of the week known?
322                         (1=explicit,0=implicit,\-1=unknown)
323 mday    date    integer day of the month
324 yday    date    integer day of the year
325 mon     date    integer month of the year
326 month   date    string  month of the year (abbrev.)
327 lmonth  date    string  month of the year
328 year    date    integer year (may be > 100)
329 zone    date    integer timezone in hours
330 tzone   date    string  timezone string
331 szone   date    integer timezone explicit?
332                         (1=explicit,0=implicit,\-1=unknown)
333 date2local      date            coerce date to local timezone
334 date2gmt        date            coerce date to GMT
335 dst     date    integer daylight savings in effect? (0 or 1)
336 clock   date    integer seconds since the UNIX epoch
337 rclock  date    integer seconds prior to current time
338 tws     date    string  official RFC-822 rendering
339 pretty  date    string  user-friendly rendering
340 .fi
341 .RE
342 .PP
343 These functions require an address component as an argument.
344 The return value of functions noted with `*' is computed from
345 the first address present in the header component.
346 .PP
347 .RS 5
348 .nf
349 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
350 .I "Function    Argument        Return  Description
351 proper  addr    string  official RFC-822 rendering
352 friendly        addr    string  user-friendly rendering
353 addr    addr    string  mbox@host or host!mbox rendering*
354 pers    addr    string  the personal name*
355 note    addr    string  commentary text*
356 mbox    addr    string  the local mailbox*
357 mymbox  addr    integer List has the user's address? (0 or 1)
358 host    addr    string  the host domain*
359 nohost  addr    integer no host was present (0 or 1)*
360 type    addr    integer host type* (0=local,1=network,
361                         \-1=uucp,2=unknown)
362 path    addr    string  any leading host route*
363 ingrp   addr    integer address was inside a group (0 or 1)*
364 gname   addr    string  name of group*
365 .fi
366 .RE
367 .PP
368 (A clarification on (\fImymbox\fR\^{\fIcomp\fR\^}) is in order.
369 This function checks each of the addresses in the header component
370 `\fIcomp\fR' against the user's mailbox name and any
371 .RI ` Alternate-Mailboxes '.
372 It returns true if any address matches,
373 however, it also returns true if the `\fIcomp\fR' header is not
374 present in the message.  If needed, the (\fInull\fR\^) function can be
375 used to explicitly test for this case.)
376 .SS Formatting
377 When a function or component escape is interpreted and the result will
378 be immediately printed, an optional field width can be specified to
379 print the field in exactly a given number of characters.  For example, a
380 numeric escape like %4(\fIsize\fR\^) will print at most 4 digits of the
381 message size; overflow will be indicated by a `?' in the first position
382 (like `?234').  A string escape like %4(\fIme\fR\^) will print the first 4
383 characters and truncate at the end.  Short fields are padded at the right
384 with the fill character (normally, a blank).  If the field width argument
385 begins with a leading zero, then the fill character is set to a zero.
386 .PP
387 The functions (\fIputnumf\fR\^) and (\fIputstrf\fR\^)
388 print their result in exactly the number of characters
389 specified by their leading field width argument.  For example,
390 %06(\fIputnumf\fR\^(\fIsize\fR\^)) will print the message
391 size in a field six characters wide filled with leading zeros;
392 %14(\fIputstrf\^\fR{\fIfrom\^\fR}) will print the `From:' header
393 component in fourteen characters with trailing spaces added as needed.
394 For \fIputstrf\fR, using a negative value for the field width causes
395 right-justification of the string within the field, with padding on
396 the left up to the field width. The functions (\fIputnum\fR\^) and
397 (\fIputstr\fR\^) are somewhat special: they print their result in
398 the minimum number of characters required, and ignore any leading
399 field width argument.
400 .PP
401 The available output width is kept in an internal register; any output
402 past this width will be truncated.
403 .SS Examples
404 With all this in mind, here's a format string for
405 .BR scan .
406 It's been divided into several pieces for readability.
407 The first part is:
408 .PP
409 .RS
410 .nf
411 %4(msg)%<(cur)+%| %>%<{replied}\-%| %>
412 .fi
413 .RE
414 .PP
415 which says that the message number should be printed in four digits.
416 If the message is the current message then a `+' else a space should
417 be printed; if a `Replied:' field is present then a `\-'
418 else a space should be printed.  Next:
419 .PP
420 .RS
421 .nf
422 %02(mon{date})/%02(mday{date})
423 .fi
424 .RE
425 .PP
426 the month and date are printed in two digits (zero filled) separated by
427 a slash. Next,
428 .PP
429 .RS 5
430 .nf
431 %<{date} %|*%>
432 .fi
433 .RE
434 .PP
435 If a `Date:' field was present,
436 then a space is printed, otherwise a `*'.
437 Next,
438 .PP
439 .RS 5
440 .nf
441 %<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>
442 .fi
443 .RE
444 .PP
445 if the message is from me, and there is a `To:' header,
446 print `To:' followed by a `user-friendly' rendering of the
447 first address in the `To:' field; any MIME-encoded
448 characters are decoded into the actual characters.
449 Continuing,
450 .PP
451 .RS 5
452 .nf
453 %<(zero)%17(decode(friendly{from}))%>
454 .fi
455 .RE
456 .PP
457 if either of the above two tests failed,
458 then the `From:' address is printed
459 in a mime-decoded, `user-friendly' format.
460 And finally,
461 .PP
462 .RS 5
463 .nf
464 %(decode{subject})
465 .fi
466 .RE
467 .PP
468 the mime-decoded subject is printed.
469 .PP
470 For a more complicated example, next consider
471 a possible
472 .I replcomps
473 format file.
474 .PP
475 .RS 5
476 .nf
477 %(lit)%(formataddr %<{reply-to}
478 .fi
479 .RE
480 .PP
481 This clears
482 .I str
483 and formats the `Reply-To:' header
484 if present.  If not present, the else-if clause is executed.
485 .PP
486 .RS 5
487 .nf
488 %?{from}%?{sender}%?{return-path}%>)\\
489 .fi
490 .RE
491 .PP
492 This formats the
493 `From:', `Sender:' or `Return-Path:'
494 headers, stopping as soon as one of them is present.  Next:
495 .PP
496 .RS 5
497 .nf
498 %<(nonnull)%(void(width))%(putaddr To: )\\n%>\\
499 .fi
500 .RE
501 .PP
502 If the \fIformataddr\fR result is non-null, it is printed as
503 an address (with line folding if needed) in a field \fIwidth\fR
504 wide with a leading label of `To:'.
505 .PP
506 .RS 5
507 .nf
508 %(lit)%(formataddr{to})%(formataddr{cc})%(formataddr(me))\\
509 .fi
510 .RE
511 .PP
512 .I str
513 is cleared, and the `To:' and `Cc:' headers, along with the user's
514 address (depending on what was specified with
515 the `\-cc' switch to \fIrepl\fR\^) are formatted.
516 .PP
517 .RS 5
518 .nf
519 %<(nonnull)%(void(width))%(putaddr cc: )\\n%>\\
520 .fi
521 .RE
522 .PP
523 If the result is non-null, it is printed as above with a
524 leading label of `Cc:'.
525 .PP
526 .RS 5
527 .nf
528 %<{subject}Subject: Re: %(decode{subject})\\n%>\\
529 .fi
530 .RE
531 .PP
532 If a subject component was present,
533 a suitable reply subject is output.
534 .PP
535 .RS 5
536 .nf
537 %<{message-id}In-Reply-To: %{message-id}\\n%>\\
538 %<{message-id}References: %<{references} %{references}%>\\
539 %{message-id}\\n%>
540 \-\-\-\-\-\-\-\-
541 .fi
542 .RE
543 .PP
544 If a message-id component was present, an `In-Reply-To:' header is
545 output including the message-id, followed by a `References:'
546 header with references, if present, and the message-id.
547 As with all
548 plain-text, the row of dashes are output as-is.
549 .PP
550 This last part is a good example for a little more elaboration.
551 Here's that part again in pseudo-code:
552 .PP
553 .RS 5
554 .nf
555 .ta .5i 1i 1.5i 2i
556 if (comp_exists(message-id))  then
557         print("In-reply-to: ")
558         print(message-id.value)
559         print("\\n")
560 endif
561 if (comp_exists(message-id)) then
562         print("References: ")
563         if (comp_exists(references)) then
564               print(references.value);
565         endif
566         print(message-id.value)
567         print("\\n")
568 endif
569 .fi
570 .RE
571 .PP
572 .\" (Note that this pseudocode begs the question ``why not just
573 .\" support this syntax?''  MH has been hacked on for a long time...)
574 .\".PP
575 One more example:
576 .B Mmh
577 supports very
578 large message numbers, and it is not uncommon for a folder
579 to have far more than 10000 messages.
580 .\" (Indeed, the original MH
581 .\" tutorial document by Rose and Romine is entitled "How to
582 .\" process 200 messages a day and still get some real work
583 .\" done."  The authors apparently only planned to get
584 .\" real work done for about 50 days per folder.)
585 Nonetheless several scan format strings are inherited
586 from older MH versions, and are generally hard-coded to 4
587 digits of message number before formatting problems
588 start to occur.
589 The mh format strings can be modified to behave more sensibly with larger
590 message numbers:
591 .PP
592 .RS
593 .nf
594 %(void(msg))%<(gt 9999)%(msg)%|%4(msg)%>
595 .fi
596 .RE
597 .PP
598 The current message number is placed in \fInum\fP.
599 (Note that
600 .RI ( msg )
601 is an int function, not a component.)
602 The
603 .RI ( gt )
604 conditional
605 is used to test whether the message number
606 has 5
607 or more digits.
608 If so, it is printed at full width: otherwise
609 at 4 digits.
610 .SH "SEE ALSO"
611 scan(1), repl(1), ap(8), dp(8)
612
613 .SH CONTEXT
614 None