1 .TH MH-FORMAT %manext5% "October 27, 2014" "%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. A literal `%' can be inserted into
49 a format file by using the sequence `%%'.
50 .\" TALK ABOUT SYNTAX FIRST, THEN SEMANTICS
52 Format strings are built around
53 .IR "escape sequences" .
54 There are three types of escape sequences: header
60 Comments may be inserted in most places where a function argument is
61 not expected. A comment begins with `%;' and ends with a (non-escaped)
66 escape is specified as
69 exists for each header found in the message being processed. For example
71 refers to the \*(lqDate:\*(rq field of the appropriate message.
72 All component escapes have a string value. Normally, component values are
73 compressed by converting any control characters (tab and newline included)
74 to spaces, then eliding any leading or multiple spaces. However, commands
75 may give different interpretations to some component escapes; be sure
76 to refer to each command's manual entry for complete details. Some commands
81 use a special component
83 to refer to the text being processed; see their respective man pages for
88 escape is specified as
90 All functions are built-in, and most have a string or numeric value.
91 A function escape may have an
93 The argument follows the function escape: separating
94 whitespace is discarded:
95 .RI `%( function " " argument )'.
97 In addition to literal numbers or strings,
98 the argument to a function escape can be another function, a component,
99 or a control escape. When the argument is a function or a
100 component, they are listed without a leading `%'. When control escapes
101 are used as function arguments, they written as normally, with
103 .SS "Control escapes"
107 escape is one of: `%<', `%?', `%|', or `%>'.
108 These are combined into the conditional execution construct:
112 .RI "%< " condition " " "format-text"
113 .RI "%? " condition " " "format-text"
115 .RI "%| " "format-text"
120 Extra white space is shown here only for clarity. These
121 constructs may be nested without ambiguity. They form a general
122 .B if\-elseif\-else\-endif
123 block where only one of the
125 is interpreted. In other
126 words, `%<' is like the "if", `%?' is like the "elseif", `%|' is like
127 "else", and `%>' is like "endif".
129 A `%<' or `%?' control escape causes its condition to be evaluated.
134 For integer valued functions or components, the condition is true
135 if the function return or component value is non-zero, and false if zero.
136 For string valued functions or components, the condition is true
137 if the function return or component value is
138 a non-empty string, and false for an empty string.
140 The `%?' control escape is optional, and may there may be more
141 than one `%?' control escape in a conditional block.
142 The `%|' control escape
143 is also optional, but may be included at most once.
144 .SS "Function escapes"
145 Functions expecting an argument generally
146 require an argument of a particular type.
147 In addition to the number and string types,
152 .ta +\w'Argument 'u +\w'An optional component, 'u
153 .I "Argument Description Example Syntax"
154 literal A literal number %(\fIfunc\fR 1234)
155 or string %(\fIfunc\fR text string)
156 comp Any component %(\fIfunc\fR\^{\fIin-reply-to\fR\^})
157 date A date component %(\fIfunc\fR\^{\fIdate\fR\^})
158 addr An address component %(\fIfunc\fR\^{\fIfrom\fR\^})
159 expr Nothing %(\fIfunc\fR)
160 or a subexpression %(\fIfunc\fR\^(\fIfunc2\fR\^))
161 or control escape %(\fIfunc\fR %<{\fIreply-to\fR\^}%|%{\fIfrom\fR\^}%>)
169 have the same syntax as
171 but require that the header component be a date string, or address
172 string, respectively.
174 Most arguments not of type
177 When escapes are nested (via expr arguments), evaluation is done from inner-most to outer-most.
178 As noted above, for the
181 functions and components are written without a
183 Control escape arguments must use a leading `%', preceded by a space.
189 %<(mymbox{from}) To: %{to}%>
193 writes the value of the header component \*(lqFrom:\*(rq to the
194 internal register named str; then (\fImymbox\fR\^) reads str and
195 writes its result to the internal register named
197 then the control escape evaluates
202 string \*(lqTo:\*(rq is printed followed by the value of the
203 header component \*(lqTo:\*(rq.
205 The evaluation of format strings is performed
206 by a small virtual machine.
207 The machine is capable of evaluating nested expressions
208 as described above, and in addition
209 has an integer register
211 and a text string register
213 When a function escape that
214 accepts an optional argument is processed,
215 and the argument is not present, the current value of either
219 is used as the argument: which register is
220 used depends on the function, as listed below.
222 Component escapes write the value of their message header in
224 Function escapes write their return value in
226 for functions returning integer or boolean values, and in
228 for functions returning string values. (The boolean type is a subset
229 of integers with usual values 0=false and 1=true.) Control escapes
230 return a boolean value, setting
232 to 1 if the last explicit condition
233 evaluated by a `%<' or `%?' control
234 succeeded, and 0 otherwise.
236 All component escapes, and those function escapes which return an
237 integer or string value, evaluate to their value as well as setting
241 Outermost escape expressions in
242 these forms will print
243 their value, but outermost escapes which return a boolean value
244 do not result in printed output.
246 The function escapes may be roughly grouped into a few categories.
250 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
251 .I "Function Argument Result Description"
252 msg integer message number
253 cur integer message is current (0 or 1)
254 unseen integer message is unseen (0 or 1)
255 size integer size of message
256 strlen integer length of \fIstr\fR
257 width integer column width of terminal
258 charleft integer bytes left in output buffer
259 timenow integer seconds since the UNIX epoch
260 me string the user's mailbox (username)
261 myhost string the user's local hostname
262 myname string the user's name
263 localmbox string the complete local mailbox
264 eq literal boolean \fInum\fR == \fIarg\fR
265 ne literal boolean \fInum\fR != \fIarg\fR
266 gt literal boolean \fInum\fR > \fIarg\fR
267 match literal boolean \fIstr\fR contains \fIarg\fR
268 amatch literal boolean \fIstr\fR starts with \fIarg\fR
269 plus literal integer \fIarg\fR plus \fInum\fR
270 minus literal integer \fIarg\fR minus \fInum\fR
271 divide literal integer \fInum\fR divided by \fIarg\fR
272 modulo literal integer \fInum\fR modulo \fIarg\fR
273 num literal integer Set \fInum\fR to \fIarg\fR.
274 num integer Set \fInum\fR to zero.
275 lit literal string Set \fIstr\fR to \fIarg\fR.
276 lit string Clear \fIstr\fR.
277 getenv literal string Set \fIstr\fR to environment value of \fIarg\fR
278 profile literal string Set \fIstr\fR to profile component \fIarg\fR
280 .\" dat literal int return value of dat[arg]
281 nonzero expr boolean \fInum\fR is non-zero
282 zero expr boolean \fInum\fR is zero
283 null expr boolean \fIstr\fR is empty
284 nonnull expr boolean \fIstr\fR is non-empty
285 void expr Set \fIstr\fR or \fInum\fR
286 comp comp string Set \fIstr\fR to component text
287 compval comp integer Set \fInum\fR to \*(lq\fBatoi\fR(\fIcomp\fR\^)\*(rq
288 .\" compflag comp integer Set \fInum\fR to component flags bits (internal)
289 .\" decodecomp comp string Set \fIstr\fR to RFC 2047 decoded component text
290 decode expr string decode \fIstr\fR as RFC 2047 (MIME-encoded)
292 unquote expr string remove RFC 2822 quotes from \fIstr\fR
293 trim expr trim trailing whitespace from \fIstr\fR
294 kilo expr string express in SI units: 15.9K, 2.3M, etc.
295 %(kilo) scales by factors of 1000,
296 kibi expr string express in IEC units: 15.5Ki, 2.2Mi.
297 %(kibi) scales by factors of 1024.
298 putstr expr print \fIstr\fR
299 putstrf expr print \fIstr\fR in a fixed width
300 putnum expr print \fInum\fR
301 putnumf expr print \fInum\fR in a fixed width
302 .\" addtoseq literal add msg to sequence (LBL option)
303 putlit expr print \fIstr\fR without space compression
304 zputlit expr print \fIstr\fR without space compression;
305 \fIstr\fR must occupy no width on display
306 bold string set terminal bold mode
307 underline string set terminal underlined mode
308 standout string set terminal standout mode
309 resetterm string reset all terminal attributes
310 hascolor boolean terminal supports color
311 fgcolor literal string set terminal foreground color
312 bgcolor literal string set terminal background color
313 formataddr expr append \fIarg\fR to \fIstr\fR as a
314 (comma separated) address list
315 concataddr expr append \fIarg\fR to \fIstr\fR as a
316 (comma separated) address list,
317 including duplicates,
319 putaddr literal print \fIstr\fR address list with
320 \fIarg\fR as optional label;
321 get line width from \fInum\fR
325 The (\fIme\fR\^) function returns the username of the current user. The
326 (\fImyhost\fR\^) function returns the
330 or the local hostname if
332 is not configured. The (\fImyname\fR\^) function will return the value of
335 environment variable if set, otherwise will return the passwd GECOS field
336 (truncated at the first comma if it contains one) for
337 the current user. The (\fIlocalmbox\fR\^) function will return the complete
338 form of the local mailbox, suitable for use in a \*(lqFrom\*(rq header.
340 .RI \*(lq Local-Mailbox \*(rq
341 profile entry if it is set; if it is not, it will be equivalent to:
345 %(myname) <%(me)@%(myhost)>
349 The following functions require a date component as an argument:
353 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
354 .I "Function Argument Return Description"
355 sec date integer seconds of the minute
356 min date integer minutes of the hour
357 hour date integer hours of the day (0-23)
358 wday date integer day of the week (Sun=0)
359 day date string day of the week (abbrev.)
360 weekday date string day of the week
361 sday date integer day of the week known?
362 (1=explicit,0=implicit,\-1=unknown)
363 mday date integer day of the month
364 yday date integer day of the year
365 mon date integer month of the year
366 month date string month of the year (abbrev.)
367 lmonth date string month of the year
368 year date integer year (may be > 100)
369 zone date integer timezone in minutes
370 tzone date string timezone string
371 szone date integer timezone explicit?
372 (1=explicit,0=implicit,\-1=unknown)
373 date2local date coerce date to local timezone
374 date2gmt date coerce date to GMT
375 dst date integer daylight savings in effect? (0 or 1)
376 clock date integer seconds since the UNIX epoch
377 rclock date integer seconds prior to current time
378 tws date string official RFC 822 rendering
379 pretty date string user-friendly rendering
380 nodate date integer returns 1 if date is invalid
384 These functions require an address component as an argument.
385 The return value of functions noted with `*' is computed from
386 the first address present in the header component.
390 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
391 .I "Function Argument Return Description"
392 proper addr string official RFC 822 rendering
393 friendly addr string user-friendly rendering
394 addr addr string mbox@host or host!mbox rendering*
395 pers addr string the personal name*
396 note addr string commentary text*
397 mbox addr string the local mailbox*
398 mymbox addr integer list has the user's address? (0 or 1)
399 getmymbox addr string the user's (first) address,
401 getmyaddr addr string the user's (first) address,
402 without personal name
403 host addr string the host domain*
404 nohost addr integer no host was present (0 or 1)*
405 type addr integer host type* (0=local,1=network,
407 path addr string any leading host route*
408 ingrp addr integer address was inside a group (0 or 1)*
409 gname addr string name of group*
413 (A clarification on (\fImymbox\fR\^{\fIcomp\fR\^}) is in order.
414 This function checks each of the addresses in the header component
415 \*(lq\fIcomp\fR\*(rq against the user's mailbox name and any
416 .RI \*(lq Alternate-Mailboxes \*(rq.
417 It returns true if any address matches,
418 however, it also returns true if the \*(lq\fIcomp\fR\*(rq header is not
419 present in the message. If needed, the (\fInull\fR\^) function can be
420 used to explicitly test for this case.)
422 When a function or component escape is interpreted and the result will
423 be immediately printed, an optional field width can be specified to
424 print the field in exactly a given number of characters. For example, a
425 numeric escape like %4(\fIsize\fR\^) will print at most 4 digits of the
426 message size; overflow will be indicated by a `?' in the first position
427 (like `?234'). A string escape like %4(\fIme\fR\^) will print the first 4
428 characters and truncate at the end. Short fields are padded at the right
429 with the fill character (normally, a blank). If the field width argument
430 begins with a leading zero, then the fill character is set to a zero.
432 The functions (\fIputnumf\fR\^) and (\fIputstrf\fR\^)
433 print their result in exactly the number of characters
434 specified by their leading field width argument. For example,
435 %06(\fIputnumf\fR\^(\fIsize\fR\^)) will print the message
436 size in a field six characters wide filled with leading zeros;
437 %14(\fIputstrf\^\fR{\fIfrom\^\fR}) will print the \*(lqFrom:\*(rq header
438 component in fourteen characters with trailing spaces added as needed.
439 For \fIputstrf\fR, using a negative value for the field width causes
440 right-justification of the string within the field, with padding on
441 the left up to the field width.
442 The functions (\fIputnum\fR\^) and
443 (\fIputstr\fR\^) are somewhat special: they print their result in the minimum number of characters
444 required, and ignore any leading field width argument. The (\fIputlit\fR\^)
445 function outputs the exact contents of the str register without any changes
446 such as duplicate space removal or control character conversion.
447 The (\fIzputlit\fR\^) function similarly outputs the exact contents of
448 the str register, but requires that those contents not occupy any
449 output width. It can therefore be used for outputting terminal escape
452 There are a limited number of function escapes to output terminal escape
453 sequences. These sequences are retrieved from the
455 database according to the current terminal setting. The (\fIbold\fR\^),
456 (\fIunderline\fR\^), and (\fIstandout\fR\^) escapes set bold mode,
457 underline mode, and standout mode respectively.
460 can be used to determine if the current terminal supports color.
461 (\fIfgcolor\fR\^) and (\fIbgcolor\fR\^) set the foreground and
462 background colors respectively. Both of these escapes take one literal
463 argument, the color name, which can be one of: black, red, green, yellow,
464 blue, magenta, cyan, white. (\fIresetterm\fR\^) resets all terminal
465 attributes back to their default setting.
467 All of these terminal escape should be used in conjunction with
468 (\fIzputlit\fR\^) (preferred) or (\fIputlit\fR\^), as the normal
469 (\fIputstr\fR\^) function will strip out control characters.
471 The available output width is kept in an internal register; any output
472 past this width will be truncated. The one exception to this is
473 (\fIzputlit\fR\^) functions will still be executed in case a terminal reset
474 code is being placed at the end of the line.
476 A few functions have different behavior depending on what command they are
481 the (\fIformataddr\fR\^) function stores all email addresses encountered into
482 an internal cache and will use this cache to suppress duplicate addresses.
483 If you need to create an address list that includes previously-seen
484 addresses you may use the (\fIconcataddr\fR\^) function, which is identical
485 to (\fIformataddr\fR\^) in all other respects. Note that (\fIconcataddr\fR\^)
486 will NOT add addresses to the duplicate-suppression cache.
487 .SS Other Hints and Tips
488 Sometimes to format function writers it is confusing as to why output is
489 duplicated. The general rule to remember is simple: If a function or
490 component escape is used where it starts with a %, then it will generate
491 text in the output file. Otherwise, it will not.
493 A good example is a simple attempt to generate a To: header based on
494 the From: and Reply-To: headers:
498 %(formataddr %<{reply-to}%|%{from})%(putaddr To: )
502 Unfortuantely if the Reply-to: header is NOT present, the output line that is
503 generated will be something like:
507 My From User <from@example.com>To: My From User <from@example.com>
511 What went wrong? When performing the test for the
513 clause (%<), the component is not output because it is considered an
516 statement (hence the rule about the lack of % applies). But the component
519 statement (everything after the `%|') is NOT an argument to anything; the
520 syntax is that it is written with a %, and thus the value of that component
521 is output. This also has the side effect of setting the
523 register, which is later picked up by the (\fIformataddr\fR\^) function
524 and then output by (\fIputaddr\fR\^). This format string has another bug
525 as well; there should always be a valid width value in the
527 register when (\fIputaddr\fR\^) is called, otherwise bad formatting can take
530 The solution is to use the (\fIvoid\fR\^) function; this will prevent the
531 function or component from outputting any text. With this in place (and
532 using (\fIwidth\fR\^) to set the
534 register for the width, a better implementation would look like:
538 %(formataddr %<{reply-to}%|%(void{from})%(void(width))%(putaddr To: )
542 It should be noted here that the side-effects of functions and component
543 escapes still are in force: as a result each component
545 .B if\-elseif\-else\-endif
550 As an additional note, the (\fIformataddr\fR\^) and (\fIconcataddr\fR\^)
551 functions have special behavior when it comes to the
553 register. The starting point of the register is saved and is used to
554 build up entries in the address list.
558 utility invaluable when debugging problems with format strings.
560 With all this in mind,
561 here's the default format string for
563 It's been divided into several pieces for readability.
568 %4(msg)%<(cur)+%| %>%<{replied}\-%?{encrypted}E%| %>
572 which says that the message number should be printed in four digits.
573 If the message is the current message then a `+' else a space should
574 be printed; if a \*(lqReplied:\*(rq field is present then a `\-'
575 else if an \*(lqEncrypted:\*(rq field is present then an `E' otherwise
576 a space should be printed. Next:
580 %02(mon{date})/%02(mday{date})
584 the month and date are printed in two digits (zero filled) separated by
593 If a \*(lqDate:\*(rq field was present,
594 then a space is printed, otherwise a `*'.
599 %<(mymbox{from})%<{to}To:%14(decode(friendly{to}))%>%>
603 if the message is from me, and there is a \*(lqTo:\*(rq header,
604 print \*(lqTo:\*(rq followed by a \*(lquser-friendly\*(rq rendering of the
605 first address in the \*(lqTo:\*(rq field; any MIME-encoded
606 characters are decoded into the actual characters.
611 %<(zero)%17(decode(friendly{from}))%>
615 if either of the above two tests failed,
616 then the \*(lqFrom:\*(rq address is printed
617 in a mime-decoded, \*(lquser-friendly\*(rq format.
622 %(decode{subject})%<{body}<<%{body}>>%>
626 the mime-decoded subject and initial body (if any) are printed.
628 For a more complicated example, next consider
635 %(lit)%(formataddr %<{reply-to}
641 and formats the \*(lqReply-To:\*(rq header
642 if present. If not present, the else-if clause is executed.
646 %?{from}%?{sender}%?{return-path}%>)\\
651 \*(lqFrom:\*(rq, \*(lqSender:\*(rq and \*(lqReturn-Path:\*(rq
652 headers, stopping as soon as one of them is present. Next:
656 %<(nonnull)%(void(width))%(putaddr To: )\\n%>\\
660 If the \fIformataddr\fR result is non-null, it is printed as
661 an address (with line folding if needed) in a field \fIwidth\fR
662 wide with a leading label of \*(lqTo:\*(rq.
666 %(lit)%(formataddr{to})%(formataddr{cc})%(formataddr(me))\\
671 is cleared, and the \*(lqTo:\*(rq and \*(lqCc:\*(rq headers, along with the user's
672 address (depending on what was specified with
673 the \*(lq\-cc\*(rq switch to \fIrepl\fR\^) are formatted.
677 %<(nonnull)%(void(width))%(putaddr cc: )\\n%>\\
681 If the result is non-null, it is printed as above with a
682 leading label of \*(lqcc:\*(rq.
686 %<{fcc}Fcc: %{fcc}\\n%>\\
697 for more details about %{\fIfcc\fR\^}),
698 an \*(lqFcc:\*(rq header is output.
702 %<{subject}Subject: Re: %{subject}\\n%>\\
706 If a subject component was present,
707 a suitable reply subject is output.
711 %<{message-id}In-Reply-To: %{message-id}\\n%>\\
712 %<{message-id}References: %<{references} %{references}%>\\
718 If a message-id component was present, an \*(lqIn-Reply-To:\*(rq header is
719 output including the message-id, followed by a \*(lqReferences:\*(rq
720 header with references, if present, and the message-id.
722 plain-text, the row of dashes are output as-is.
724 This last part is a good example for a little more elaboration.
725 Here's that part again in pseudo-code:
730 if (comp_exists(message-id)) then
731 print (\*(lqIn-reply-to: \*(rq)
732 print (message-id.value)
733 print (\*(lq\\n\*(rq)
735 if (comp_exists(message-id)) then
736 print (\*(lqReferences: \*(rq)
737 if (comp_exists(references)) then
738 print(references.value);
740 print (message-id.value)
741 print (\*(lq\\n\*(rq)
746 .\" (Note that this pseudocode begs the question ``why not just
747 .\" support this syntax?'' MH has been hacked on for a long time...)
749 One more example: Currently,
752 large message numbers, and it is not uncommon for a folder
753 to have far more than 10000 messages.
754 .\" (Indeed, the original MH
755 .\" tutorial document by Rose and Romine is entitled "How to
756 .\" process 200 messages a day and still get some real work
757 .\" done." The authors apparently only planned to get
758 .\" real work done for about 50 days per folder.)
759 Nontheless (as noted above)
760 the various scan format strings are inherited
761 from older MH versions, and are generally hard-coded to 4
762 digits of message number before formatting problems
764 The nmh format strings can be modified to behave more sensibly with larger
769 %(void(msg))%<(gt 9999)%(msg)%|%4(msg)%>
773 The current message number is placed in \fInum\fP.
776 is an int function, not a component.)
780 is used to test whether the message number
783 If so, it is printed at full width, otherwise