1 .TH MH-FORMAT %manext5% "March 24, 2012" "%nmhversion%"
6 mh-format \- format file for nmh message system
10 commands utilize either a
14 file during their execution. For example,
16 uses a format string which directs it how to generate the scan listing
19 uses a format file which directs it
20 how to generate the reply to a message, and so on.
22 There are a few alternate scan listing formats available
24 .IR nmh/etc/scan.time ,
25 .IR nmh/etc/scan.size ,
27 .IR nmh/etc/scan.timely .
34 format files which may have been written at your site.
36 It suffices to have your local
38 expert actually write new format
39 commands or modify existing ones. This manual section explains how to
40 do that. Note: familiarity with the C
44 A format string consists of ordinary text, and special multi-character
45 escape sequences which begin with `%'. When specifying a format
46 string, the usual C backslash characters are honored: `\\b', `\\f',
47 `\\n', `\\r', and `\\t'. Continuation lines in format files end with
48 `\\' followed by the newline character.
49 .\" TALK ABOUT SYNTAX FIRST, THEN SEMANTICS
51 Format strings are built around
52 .IR "escape sequences" .
53 There are three types of escape sequences: header
59 Comments may be inserted in most places where a function argument is
60 not expected. A comment begins with `%;' and ends with a (non-escaped)
65 escape is specified as
68 exists for each header found in the message being processed. For example
70 refers to the \*(lqDate:\*(rq field of the appropriate message.
71 All component escapes have a string value. Normally, component values are
72 compressed by converting any control characters (tab and newline included)
73 to spaces, then eliding any leading or multiple spaces. However, commands
74 may give different interpretations to some component escapes; be sure
75 to refer to each command's manual entry for complete details. Some commands
80 use a special component
82 to refer to the text being processed; see their respective man pages for
87 escape is specified as
89 All functions are built-in, and most have a string or numeric value.
90 A function escape may have an
92 The argument follows the function escape: separating
93 whitespace is discarded:
94 .RI `%( function " " argument )'.
96 In addition to literal numbers or strings,
97 the argument to a function escape can be another function, a component,
98 or a control escape. When the argument is a function or a
99 component, they are listed without a leading `%'. When control escapes
100 are used as function arguments, they written as normally, with
102 .SS "Control escapes"
106 escape is one of: `%<', `%?', `%|', or `%>'.
107 These are combined into the conditional execution construct:
111 .RI "%< " condition " " "format-text"
112 .RI "%? " condition " " "format-text"
114 .RI "%| " "format-text"
119 Extra white space is shown here only for clarity. These
120 constructs may be nested without ambiguity. They form a general
121 .B if\-elseif\-else\-endif
122 block where only one of the
124 is interpreted. In other
125 words, `%<' is like the "if", `%?' is like the "elseif", `%|' is like
126 "else", and `%>' is like "endif".
128 A `%<' or `%?' control escape causes its condition to be evaluated.
133 For integer valued functions or components, the condition is true
134 if the function return or component value is non-zero, and false if zero.
135 For string valued functions or components, the condition is true
136 if the function return or component value is
137 a non-empty string, and false for an empty string.
139 The `%?' control escape is optional, and may there may be more
140 than one `%?' control escape in a conditional block.
141 The `%|' control escape
142 is also optional, but may be included at most once.
143 .SS "Function escapes"
144 Functions expecting an argument generally
145 require an argument of a particular type.
146 In addition to the number and string types,
151 .ta +\w'Argument 'u +\w'An optional component, 'u
152 .I "Argument Description Example Syntax"
153 literal A literal number %(\fIfunc\fR 1234)
154 or string %(\fIfunc\fR text string)
155 comp Any component %(\fIfunc\fR\^{\fIin-reply-to\fR\^})
156 date A date component %(\fIfunc\fR\^{\fIdate\fR\^})
157 addr An address component %(\fIfunc\fR\^{\fIfrom\fR\^})
158 expr Nothing %(\fIfunc\fR)
159 or a subexpression %(\fIfunc\fR\^(\fIfunc2\fR\^))
160 or control escape %(\fIfunc\fR %<{\fIreply-to\fR\^}%|%{\fIfrom\fR\^}%>)
168 have the same syntax as
170 but require that the header component be a date string, or address
171 string, respectively.
173 Most arguments not of type
176 When escapes are nested (via expr arguments), evaluation is done from inner-most to outer-most.
177 As noted above, for the
180 functions and components are written without a
182 Control escape arguments must use a leading `%', preceded by a space.
188 %<(mymbox{from}) To: %{to}%>
192 writes the value of the header component \*(lqFrom:\*(rq to the
193 internal register named str; then (\fImymbox\fR\^) reads str and
194 writes its result to the internal register named
196 then the control escape evaluates
201 string \*(lqTo:\*(rq is printed followed by the value of the
202 header component \*(lqTo:\*(rq.
204 The evaluation of format strings is performed
205 by a small virtual machine.
206 The machine is capable of evaluating nested expressions
207 as described above, and in addition
208 has an integer register
210 and a text string register
212 When a function escape that
213 accepts an optional argument is processed,
214 and the argument is not present, the current value of either
218 is used as the argument: which register is
219 used depends on the function, as listed below.
221 Component escapes write the value of their message header in
223 Function escapes write their return value in
225 for functions returning integer or boolean values, and in
227 for functions returning string values. (The boolean type is a subset
228 of integers with usual values 0=false and 1=true.) Control escapes
229 return a boolean value, setting
231 to 1 if the last explicit condition
232 evaluated by a `%<' or `%?' control
233 succeeded, and 0 otherwise.
235 All component escapes, and those function escapes which return an
236 integer or string value, evaluate to their value as well as setting
240 Outermost escape expressions in
241 these forms will print
242 their value, but outermost escapes which return a boolean value
243 do not result in printed output.
245 The function escapes may be roughly grouped into a few categories.
249 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
250 .I "Function Argument Result Description"
251 msg integer message number
252 cur integer message is current (0 or 1)
253 unseen integer message is unseen (0 or 1)
254 size integer size of message
255 strlen integer length of \fIstr\fR
256 width integer output buffer size in bytes
257 charleft integer bytes left in output buffer
258 timenow integer seconds since the UNIX epoch
259 me string the user's mailbox (username)
260 myhost string the user's local hostname
261 myname string the user's name
262 localmbox string the complete local mailbox
263 eq literal boolean \fInum\fR == \fIarg\fR
264 ne literal boolean \fInum\fR != \fIarg\fR
265 gt literal boolean \fInum\fR > \fIarg\fR
266 match literal boolean \fIstr\fR contains \fIarg\fR
267 amatch literal boolean \fIstr\fR starts with \fIarg\fR
268 plus literal integer \fIarg\fR plus \fInum\fR
269 minus literal integer \fIarg\fR minus \fInum\fR
270 divide literal integer \fInum\fR divided by \fIarg\fR
271 modulo literal integer \fInum\fR modulo \fIarg\fR
272 num literal integer Set \fInum\fR to \fIarg\fR.
273 num integer Set \fInum\fR to zero.
274 lit literal string Set \fIstr\fR to \fIarg\fR.
275 lit string Clear \fIstr\fR.
276 getenv literal string Set \fIstr\fR to environment value of \fIarg\fR
277 profile literal string Set \fIstr\fR to profile component \fIarg\fR
279 .\" dat literal int return value of dat[arg]
280 nonzero expr boolean \fInum\fR is non-zero
281 zero expr boolean \fInum\fR is zero
282 null expr boolean \fIstr\fR is empty
283 nonnull expr boolean \fIstr\fR is non-empty
284 void expr Set \fIstr\fR or \fInum\fR
285 comp comp string Set \fIstr\fR to component text
286 compval comp integer Set \fInum\fR to \*(lq\fBatoi\fR(\fIcomp\fR\^)\*(rq
287 .\" compflag comp integer Set \fInum\fR to component flags bits (internal)
288 .\" decodecomp comp string Set \fIstr\fR to RFC-2047 decoded component text
289 decode expr string decode \fIstr\fR as RFC-2047 (MIME-encoded)
291 unquote expr string remove RFC-2822 quotes from \fIstr\fR
292 trim expr trim trailing white-space from \fIstr\fR
293 putstr expr print \fIstr\fR
294 putstrf expr print \fIstr\fR in a fixed width
295 putnum expr print \fInum\fR
296 putnumf expr print \fInum\fR in a fixed width
297 .\" addtoseq literal add msg to sequence (LBL option)
298 putlit expr print \fIstr\fR without space compression
299 nodate string integer Argument not a date string (0 or 1)
300 formataddr expr append \fIarg\fR to \fIstr\fR as a
301 (comma separated) address list
302 concataddr expr append \fIarg\fR to \fIstr\fR as a
303 (comma separated) address list,
304 including duplicates,
306 putaddr literal print \fIstr\fR address list with
307 \fIarg\fR as optional label;
308 get line width from \fInum\fR
312 The (\fIme\fR\^) function returns the username of the current user. The
313 (\fImyhost\fR\^) function returns the
317 or the local hostname if
319 is not configured. The (\fImyname\fR\^) function will return the value of
322 environment variable if set, otherwise will return the passwd GECOS field
323 (truncated at the first comma if it contains one) for
324 the current user. The (\fIlocalmbox\fR\^) function will return the complete
325 form of the local mailbox, suitable for use in a \*(lqFrom\*(rq header.
327 .RI \*(lq Local-Mailbox \*(rq
328 profile entry if it is set; if it is not, it will be equivalent to:
332 %(myname) <%(me)@%(myhost)>
336 The following functions require a date component as an argument:
340 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
341 .I "Function Argument Return Description"
342 sec date integer seconds of the minute
343 min date integer minutes of the hour
344 hour date integer hours of the day (0-23)
345 wday date integer day of the week (Sun=0)
346 day date string day of the week (abbrev.)
347 weekday date string day of the week
348 sday date integer day of the week known?
349 (1=explicit,0=implicit,\-1=unknown)
350 mday date integer day of the month
351 yday date integer day of the year
352 mon date integer month of the year
353 month date string month of the year (abbrev.)
354 lmonth date string month of the year
355 year date integer year (may be > 100)
356 zone date integer timezone in hours
357 tzone date string timezone string
358 szone date integer timezone explicit?
359 (1=explicit,0=implicit,\-1=unknown)
360 date2local date coerce date to local timezone
361 date2gmt date coerce date to GMT
362 dst date integer daylight savings in effect? (0 or 1)
363 clock date integer seconds since the UNIX epoch
364 rclock date integer seconds prior to current time
365 tws date string official 822 rendering
366 pretty date string user-friendly rendering
370 These functions require an address component as an argument.
371 The return value of functions noted with `*' is computed from
372 the first address present in the header component.
376 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
377 .I "Function Argument Return Description"
378 proper addr string official 822 rendering
379 friendly addr string user-friendly rendering
380 addr addr string mbox@host or host!mbox rendering*
381 pers addr string the personal name*
382 note addr string commentary text*
383 mbox addr string the local mailbox*
384 mymbox addr integer List has the user's address? (0 or 1)
385 host addr string the host domain*
386 nohost addr integer no host was present (0 or 1)*
387 type addr integer host type* (0=local,1=network,
389 path addr string any leading host route*
390 ingrp addr integer address was inside a group (0 or 1)*
391 gname addr string name of group*
395 (A clarification on (\fImymbox\fR\^{\fIcomp\fR\^}) is in order.
396 This function checks each of the addresses in the header component
397 \*(lq\fIcomp\fR\*(rq against the user's mailbox name and any
398 .RI \*(lq Alternate-Mailboxes \*(rq.
399 It returns true if any address matches,
400 however, it also returns true if the \*(lq\fIcomp\fR\*(rq header is not
401 present in the message. If needed, the (\fInull\fR\^) function can be
402 used to explicitly test for this case.)
404 When a function or component escape is interpreted and the result will
405 be immediately printed, an optional field width can be specified to
406 print the field in exactly a given number of characters. For example, a
407 numeric escape like %4(\fIsize\fR\^) will print at most 4 digits of the
408 message size; overflow will be indicated by a `?' in the first position
409 (like `?234'). A string escape like %4(\fIme\fR\^) will print the first 4
410 characters and truncate at the end. Short fields are padded at the right
411 with the fill character (normally, a blank). If the field width argument
412 begins with a leading zero, then the fill character is set to a zero.
414 The functions (\fIputnumf\fR\^) and (\fIputstrf\fR\^)
415 print their result in exactly the number of characters
416 specified by their leading field width argument. For example,
417 %06(\fIputnumf\fR\^(\fIsize\fR\^)) will print the message
418 size in a field six characters wide filled with leading zeros;
419 %14(\fIputstrf\^\fR{\fIfrom\^\fR}) will print the \*(lqFrom:\*(rq header
420 component in fourteen characters with trailing spaces added as needed.
421 For \fIputstrf\fR, using a negative value for the field width causes
422 right-justification of the string within the field, with padding on
423 the left up to the field width.
424 The functions (\fIputnum\fR\^) and
425 (\fIputstr\fR\^) are somewhat special: they print their result in the minimum number of characters
426 required, and ignore any leading field width argument. The (\fIputlit\fR\^)
427 function outputs the exact contents of str register without any changes
428 such as duplicate space removal or control character conversion.
430 The available output width is kept in an internal register; any output
431 past this width will be truncated.
433 A few functions have different behavior depending on what command they are
438 the (\fIformataddr\fR\^) function stores all email addresses encountered into
439 an internal cache and will use this cache to suppress duplicate addresses.
440 If you need to create an address list that includes previously-seen
441 addresses you may use the (\fIconcataddr\fR\^) function, which is identical
442 to (\fIformataddr\fR\^) in all other respects. Note that (\fIconcataddr\fR\^)
443 will NOT add addresses to the duplicate-suppression cache.
444 .SS Other Hints and Tips
445 Sometimes to format function writers it is confusing as to why output is
446 duplicated. The general rule to remember is simple: If a function or
447 component escape is used where it starts with a %, then it will generate
448 text in the output file. Otherwise, it will not.
450 A good example is a simple attempt to generate a To: header based on
451 the From: and Reply-To: headers:
455 %(formataddr %<{reply-to}%|%{from})%(putaddr To: )
459 Unfortuantely if the Reply-to: header is NOT present, the output line that is
460 generated will be something like:
464 My From User <from@example.com>To: My From User <from@example.com>
468 What went wrong? When performing the test for the
470 clause (%<), the component is not output because it is considered an
473 statement (hence the rule about the lack of % applies). But the component
476 statement (everything after the `%|') is NOT an argument to anything; the
477 syntax is that it is written with a %, and thus the value of that component
478 is output. This also has the side effect of setting the
480 register, which is later picked up by the (\fIformataddr\fR\^) function
481 and then output by (\fIputaddr\fR\^). This format string has another bug
482 as well; there should always be a valid width value in the
484 register when (\fIputaddr\fR\^) is called, otherwise bad formatting can take
487 The solution is to use the (\fIvoid\fR\^) function; this will prevent the
488 function or component from outputting any text. With this in place (and
489 using (\fIwidth\fR\^) to set the
491 register for the width, a better implementation would look like:
495 %(formataddr %<{reply-to}%|%(void{from})%(void(width))%(putaddr To: )
499 It should be noted here that the side-effects of functions and component
500 escapes still are in force: as a result each component
502 .B if\-elseif\-else\-endif
507 As an additional note, the (\fIformataddr\fR\^) and (\fIconcataddr\fR\^)
508 functions have some behavior when it comes to the
510 register. The starting point of the register is saved and is used to
511 build up entries in the address list.
517 utilities invaluable in debugging problems with format strings.
519 With all this in mind,
520 here's the default format string for
522 It's been divided into several pieces for readability.
527 %4(msg)%<(cur)+%| %>%<{replied}\-%?{encrypted}E%| %>
531 which says that the message number should be printed in four digits.
532 If the message is the current message then a `+' else a space should
533 be printed; if a \*(lqReplied:\*(rq field is present then a `\-'
534 else if an \*(lqEncrypted:\*(rq field is present then an `E' otherwise
535 a space should be printed. Next:
539 %02(mon{date})/%02(mday{date})
543 the month and date are printed in two digits (zero filled) separated by
552 If a \*(lqDate:\*(rq field was present,
553 then a space is printed, otherwise a `*'.
558 %<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>
562 if the message is from me, and there is a \*(lqTo:\*(rq header,
563 print \*(lqTo:\*(rq followed by a \*(lquser-friendly\*(rq rendering of the
564 first address in the \*(lqTo:\*(rq field; any MIME-encoded
565 characters are decoded into the actual characters.
570 %<(zero)%17(decode(friendly{from}))%>
574 if either of the above two tests failed,
575 then the \*(lqFrom:\*(rq address is printed
576 in a mime-decoded, \*(lquser-friendly\*(rq format.
581 %(decode{subject})%<{body}<<%{body}>>%>
585 the mime-decoded subject and initial body (if any) are printed.
587 For a more complicated example, next consider
594 %(lit)%(formataddr %<{reply-to}
600 and formats the \*(lqReply-To:\*(rq header
601 if present. If not present, the else-if clause is executed.
605 %?{from}%?{sender}%?{return-path}%>)\\
610 \*(lqFrom:\*(rq, \*(lqSender:\*(rq and \*(lqReturn-Path:\*(rq
611 headers, stopping as soon as one of them is present. Next:
615 %<(nonnull)%(void(width))%(putaddr To: )\\n%>\\
619 If the \fIformataddr\fR result is non-null, it is printed as
620 an address (with line folding if needed) in a field \fIwidth\fR
621 wide with a leading label of \*(lqTo:\*(rq.
625 %(lit)%(formataddr{to})%(formataddr{cc})%(formataddr(me))\\
630 is cleared, and the \*(lqTo:\*(rq and \*(lqCc:\*(rq headers, along with the user's
631 address (depending on what was specified with
632 the \*(lq\-cc\*(rq switch to \fIrepl\fR\^) are formatted.
636 %<(nonnull)%(void(width))%(putaddr cc: )\\n%>\\
640 If the result is non-null, it is printed as above with a
641 leading label of \*(lqcc:\*(rq.
645 %<{fcc}Fcc: %{fcc}\\n%>\\
656 for more details about %{\fIfcc\fR\^}),
657 an \*(lqFcc:\*(rq header is output.
661 %<{subject}Subject: Re: %{subject}\\n%>\\
665 If a subject component was present,
666 a suitable reply subject is output.
670 %<{message-id}In-Reply-To: %{message-id}\\n%>\\
671 %<{message-id}References: %<{references} %{references}%>\\
677 If a message-id component was present, an \*(lqIn-Reply-To:\*(rq header is
678 output including the message-id, followed by a \*(lqReferences:\*(rq
679 header with references, if present, and the message-id.
681 plain-text, the row of dashes are output as-is.
683 This last part is a good example for a little more elaboration.
684 Here's that part again in pseudo-code:
689 if (comp_exists(message-id)) then
690 print (\*(lqIn-reply-to: \*(rq)
691 print (message-id.value)
692 print (\*(lq\\n\*(rq)
694 if (comp_exists(message-id)) then
695 print (\*(lqReferences: \*(rq)
696 if (comp_exists(references)) then
697 print(references.value);
699 print (message-id.value)
700 print (\*(lq\\n\*(rq)
705 .\" (Note that this pseudocode begs the question ``why not just
706 .\" support this syntax?'' MH has been hacked on for a long time...)
708 One more example: Currently,
711 large message numbers, and it is not uncommon for a folder
712 to have far more than 10000 messages.
713 .\" (Indeed, the original MH
714 .\" tutorial document by Rose and Romine is entitled "How to
715 .\" process 200 messages a day and still get some real work
716 .\" done." The authors apparently only planned to get
717 .\" real work done for about 50 days per folder.)
718 Nontheless (as noted above)
719 the various scan format strings are inherited
720 from older MH versions, and are generally hard-coded to 4
721 digits of message number before formatting problems
723 The nmh format strings can be modified to behave more sensibly with larger
728 %(void(msg))%<(gt 9999)%(msg)%|%4(msg)%>
732 The current message number is placed in \fInum\fP.
735 is an int function, not a component.)
739 is used to test whether the message number
742 If so, it is printed at full width, otherwise