1 .TH MH-FORMAT %manext5% 2015-01-10 "%nmhversion%"
6 mh-format \- formatting language for nmh message system
10 commands utilize either a
14 file during their execution. For example,
16 uses a format string to generate its listing of messages;
18 uses a format file to generate message replies, and so on.
20 There are a number of scan listing formats available,
22 .IR nmh/etc/scan.time ,
23 .IR nmh/etc/scan.size ,
25 .IR nmh/etc/scan.timely .
32 format files which may have been written at your site.
34 You can have your local
36 expert write new format commands or modify existing ones,
37 or you can try your hand at it yourself.
38 This manual section explains how to do that.
39 Note: some familiarity with the C
43 A format string consists of ordinary text combined with special,
44 multi-character, escape sequences which begin with `%'.
45 When specifying a format string, the usual C backslash characters
46 are honored: `\\b', `\\f', `\\n', `\\r', and `\\t'.
47 Continuation lines in format files end with `\\' followed by the
48 newline character. A literal `%' can be inserted into a format
49 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 sequence: 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)
63 .SS "Component escapes"
66 escape is specified as
69 exists for each header in the message being processed. For example,
71 refers to the \*(lqDate:\*(rq field of the message.
72 All component escapes have a string value. Such values are usually
73 compressed by converting any control characters (tab and newline included)
74 to spaces, then eliding any leading or multiple spaces. Some commands,
75 however, may interpret some component escapes differently; be sure to
76 refer to each command's manual entry for details.
77 Some commands (such as
81 use a special component
83 to refer to the text being processed; see their respective man pages for
85 .SS "Function escapes"
88 escape is specified as
90 All functions are built-in, and most have a string or integer value.
91 A function escape may take an
93 The argument follows the function escape (and any separating
94 whitespace is discarded) as in the following example:
98 .RI %( function " " argument )
102 In addition to literal numbers or strings, the argument to a
103 function escape can be another function, or a component,
104 or a control escape. When the argument is a function or a
105 component, the argument is specified without a leading `%'.
106 When the argument is a control escape, it is specified
108 .SS "Control escapes"
111 escape is one of: `%<', `%?', `%|', or `%>'.
112 These are combined into the conditional execution construct:
116 .RI "%< " condition " " "format-text"
117 .RI "%? " condition " " "format-text"
119 .RI "%| " "format-text"
124 (Extra white space is shown here only for clarity.)
125 These constructs, which may be nested without ambiguity, form a general
126 .B if-elseif-else-endif
127 block where only one of the format-texts is interpreted. In other
128 words, `%<' is like the "if", `%?' is like the "elseif", `%|' is like
129 "else", and `%>' is like "endif".
131 A `%<' or `%?' control escape causes its condition to be evaluated.
136 For components and functions whose value is an integer, the condition is true
137 if it is non-zero, and false if zero.
138 For components and functions whose value is a string, the condition is true
139 it is a non-empty string, and false if an empty string.
141 The `%?' control escape is optional, and can be used multiple times
142 in a conditional block. The `%|' control escape is also optional,
143 but may only be used once.
144 .SS "Function escapes"
145 Functions expecting an argument generally require an argument of a
146 particular type. In addition to the integer 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 types have the same syntax as the component type,
170 but require a header component which is a date, or address,
171 string, respectively.
173 Most arguments not of type
176 When escapes are nested (via expr arguments), evaluation is done
177 from innermost to outermost. As noted above, for the
179 argument type, functions and components are written without a
181 Control escape arguments must use a leading `%', preceded by a space.
187 %<(mymbox{from}) To: %{to}%>
191 writes the value of the header component \*(lqFrom:\*(rq to the
192 internal register named str; then (\fImymbox\fR\^) reads str and
193 writes its result to the internal register named
195 then the control escape, `%<', evaluates
199 is non-zero, the string \*(lqTo:\*(rq is printed followed by the
200 value of the header component \*(lqTo:\*(rq.
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, has an integer register
207 and a text string register
209 When a function escape that accepts an optional argument is processed,
210 and the argument is not present, the current value of either
214 is substituted as the argument: the register used depends on the function,
217 Component escapes write the value of their message header in
219 Function escapes write their return value in
221 for functions returning integer or boolean values, and in
223 for functions returning string values. (The boolean type is a subset
224 of integers, with usual values 0=false and 1=true.) Control escapes
225 return a boolean value, setting
227 to 1 if the last explicit condition
228 evaluated by a `%<' or `%?' control escape succeeded, and 0 otherwise.
230 All component escapes, and those function escapes which return an
231 integer or string value, evaluate to their value as well as setting
235 Outermost escape expressions in these forms will print
236 their value, but outermost escapes which return a boolean value
237 do not result in printed output.
239 The function escapes may be roughly grouped into a few categories.
243 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
244 .I "Function Argument Return Description"
245 msg integer message number
246 cur integer message is current (0 or 1)
247 unseen integer message is unseen (0 or 1)
248 size integer size of message
249 strlen integer length of \fIstr\fR
250 width integer column width of terminal
251 charleft integer bytes left in output buffer
252 timenow integer seconds since the Unix epoch
253 me string the user's mailbox (username)
254 myhost string the user's local hostname
255 myname string the user's name
256 localmbox string the complete local mailbox
257 eq literal boolean \fInum\fR == \fIarg\fR
258 ne literal boolean \fInum\fR != \fIarg\fR
259 gt literal boolean \fInum\fR > \fIarg\fR
260 match literal boolean \fIstr\fR contains \fIarg\fR
261 amatch literal boolean \fIstr\fR starts with \fIarg\fR
262 plus literal integer \fIarg\fR plus \fInum\fR
263 minus literal integer \fIarg\fR minus \fInum\fR
264 multiply literal integer \fInum\fR multiplied by \fIarg\fR
265 divide literal integer \fInum\fR divided by \fIarg\fR
266 modulo literal integer \fInum\fR modulo \fIarg\fR
267 num literal integer Set \fInum\fR to \fIarg\fR.
268 num integer Set \fInum\fR to zero.
269 lit literal string Set \fIstr\fR to \fIarg\fR.
270 lit string Clear \fIstr\fR.
271 getenv literal string Set \fIstr\fR to environment value of \fIarg\fR
272 profile literal string Set \fIstr\fR to profile or context
273 component \fIarg\fR value
274 .\" dat literal int return value of dat[arg]
275 nonzero expr boolean \fInum\fR is non-zero
276 zero expr boolean \fInum\fR is zero
277 null expr boolean \fIstr\fR is empty
278 nonnull expr boolean \fIstr\fR is non-empty
279 void expr Set \fIstr\fR or \fInum\fR
280 comp comp string Set \fIstr\fR to component text
281 compval comp integer Set \fInum\fR to \*(lq\fBatoi\fR(\fIcomp\fR\^)\*(rq
282 .\" compflag comp integer Set \fInum\fR to component flags bits (internal)
283 .\" decodecomp comp string Set \fIstr\fR to RFC 2047 decoded component text
284 decode expr string decode \fIstr\fR as RFC 2047 (MIME-encoded)
286 unquote expr string remove RFC 2822 quotes from \fIstr\fR
287 trim expr trim trailing whitespace from \fIstr\fR
288 trimr expr string Like %(trim), also returns string
289 kilo expr string express in SI units: 15.9K, 2.3M, etc.
290 %(kilo) scales by factors of 1000,
291 kibi expr string express in IEC units: 15.5Ki, 2.2Mi.
292 %(kibi) scales by factors of 1024.
293 ordinal expr string Output ordinal suffix based on value
294 of \fInum\fR (st, nd, rd, th)
295 putstr expr print \fIstr\fR
296 putstrf expr print \fIstr\fR in a fixed width
297 putnum expr print \fInum\fR
298 putnumf expr print \fInum\fR in a fixed width
299 putlit expr print \fIstr\fR without space compression
300 zputlit expr print \fIstr\fR without space compression;
301 \fIstr\fR must occupy no width on display
302 bold string set terminal bold mode
303 underline string set terminal underlined mode
304 standout string set terminal standout mode
305 resetterm string reset all terminal attributes
306 hascolor boolean terminal supports color
307 fgcolor literal string set terminal foreground color
308 bgcolor literal string set terminal background color
309 formataddr expr append \fIarg\fR to \fIstr\fR as a
310 (comma separated) address list
311 concataddr expr append \fIarg\fR to \fIstr\fR as a
312 (comma separated) address list,
313 including duplicates,
315 putaddr literal print \fIstr\fR address list with
316 \fIarg\fR as optional label;
317 get line width from \fInum\fR
321 The (\fIme\fR\^) function returns the username of the current user.
322 The (\fImyhost\fR\^) function returns the
326 or the local hostname if
328 is not configured. The (\fImyname\fR\^) function will return the value of
331 environment variable if set, otherwise it will return the passwd GECOS field
332 (truncated at the first comma if it contains one) for
333 the current user. The (\fIlocalmbox\fR\^) function will return the complete
334 form of the local mailbox, suitable for use in a \*(lqFrom\*(rq header.
336 .RI \*(lq Local-Mailbox \*(rq
337 profile entry if there is one; if not, it will be equivalent to:
341 %(myname) <%(me)@%(myhost)>
345 The following functions require a date component as an argument:
349 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
350 .I "Function Argument Return Description"
351 sec date integer seconds of the minute
352 min date integer minutes of the hour
353 hour date integer hours of the day (0-23)
354 wday date integer day of the week (Sun=0)
355 day date string day of the week (abbrev.)
356 weekday date string day of the week
357 sday date integer day of the week known?
358 (1=explicit,0=implicit,\-1=unknown)
359 mday date integer day of the month
360 yday date integer day of the year
361 mon date integer month of the year
362 month date string month of the year (abbrev.)
363 lmonth date string month of the year
364 year date integer year (may be > 100)
365 zone date integer timezone in minutes
366 tzone date string timezone string
367 szone date integer timezone explicit?
368 (1=explicit,0=implicit,\-1=unknown)
369 date2local date coerce date to local timezone
370 date2gmt date coerce date to GMT
371 dst date integer daylight savings in effect? (0 or 1)
372 clock date integer seconds since the Unix epoch
373 rclock date integer seconds prior to current time
374 tws date string official RFC 822 rendering
375 pretty date string user-friendly rendering
376 nodate date integer returns 1 if date is invalid
380 The following functions require an address component as an argument.
381 The return value of functions noted with `*' is computed from
382 the first address present in the header component.
386 .ta \w'Fformataddr 'u +\w'Aboolean 'u +\w'Rboolean 'u
387 .I "Function Argument Return Description"
388 proper addr string official RFC 822 rendering
389 friendly addr string user-friendly rendering
390 addr addr string mbox@host or host!mbox rendering*
391 pers addr string the personal name*
392 note addr string commentary text*
393 mbox addr string the local mailbox*
394 mymbox addr integer list has the user's address? (0 or 1)
395 getmymbox addr string the user's (first) address,
397 getmyaddr addr string the user's (first) address,
398 without personal name
399 host addr string the host domain*
400 nohost addr integer no host was present (0 or 1)*
401 type addr integer host type* (0=local,1=network,
403 path addr string any leading host route*
404 ingrp addr integer address was inside a group (0 or 1)*
405 gname addr string name of group*
409 (A clarification on (\fImymbox\fR\^{\fIcomp\fR\^}) is in order.
410 This function checks each of the addresses in the header component
411 \*(lq\fIcomp\fR\*(rq against the user's mailbox name and any
412 .RI \*(lq Alternate-Mailboxes \*(rq.
413 It returns true if any address matches. However, it also returns true
414 if the \*(lq\fIcomp\fR\*(rq header is not present in the message.
415 If needed, the (\fInull\fR\^) function can be used to explicitly
418 When a function or component escape is interpreted and the result will
419 be printed immediately, an optional field width can be specified to
420 print the field in exactly a given number of characters. For example, a
421 numeric escape like %4(\fIsize\fR\^) will print at most 4 digits of the
422 message size; overflow will be indicated by a `?' in the first position
423 (like `?234'). A string escape like %4(\fIme\fR\^) will print the first 4
424 characters and truncate at the end. Short fields are padded at the right
425 with the fill character (normally, a blank). If the field width argument
426 begins with a leading zero, then the fill character is set to a zero.
428 The functions (\fIputnumf\fR\^) and (\fIputstrf\fR\^)
429 print their result in exactly the number of characters
430 specified by their leading field width argument. For example,
431 %06(\fIputnumf\fR\^(\fIsize\fR\^)) will print the message
432 size in a field six characters wide filled with leading zeros;
433 %14(\fIputstrf\^\fR{\fIfrom\^\fR}) will print the \*(lqFrom:\*(rq header
434 component in fourteen characters with trailing spaces added as needed.
435 Using a negative value for the field width causes
436 right-justification within the field, with padding on
437 the left up to the field width.
438 Padding is with spaces except for a left-padded
440 when the width starts with zero.
441 The functions (\fIputnum\fR\^) and
442 (\fIputstr\fR\^) are somewhat special: they print their result in the
443 minimum number of characters required, and ignore any leading field width
444 argument. The (\fIputlit\fR\^) function outputs the exact contents of the
445 str register without any changes such as duplicate space removal or control
446 character conversion. Similarly, the (\fIzputlit\fR\^) function outputs
447 the exact contents of the str register, but requires that those contents
448 not occupy any output width. It can therefore be used for outputting
449 terminal escape sequences.
451 There are a limited number of function escapes to output terminal escape
452 sequences. These sequences are retrieved from the
454 database according to the current terminal setting. The (\fIbold\fR\^),
455 (\fIunderline\fR\^), and (\fIstandout\fR\^) escapes set bold mode,
456 underline mode, and standout mode respectively.
458 can be used to determine if the current terminal supports color.
459 (\fIfgcolor\fR\^) and (\fIbgcolor\fR\^) set the foreground and
460 background colors respectively. Both of these escapes take one literal
461 argument, the color name, which can be one of: black, red, green, yellow,
462 blue, magenta, cyan, white. (\fIresetterm\fR\^) resets all terminal
463 attributes to their default setting. These terminal escapes should be
464 used in conjunction with (\fIzputlit\fR\^) (preferred) or
465 (\fIputlit\fR\^), as the normal (\fIputstr\fR\^) function will strip
466 out control characters.
468 The available output width is kept in an internal register; any output
469 exceeding this width will be truncated. The one exception to this is that
470 (\fIzputlit\fR\^) functions will still be executed if a terminal
471 reset code is being placed at the end of a line.
473 Some functions have different behavior depending on the command they are
478 the (\fIformataddr\fR\^) function stores all email addresses encountered into
479 an internal cache and will use this cache to suppress duplicate addresses.
480 If you need to create an address list that includes previously-seen
481 addresses you may use the (\fIconcataddr\fR\^) function, which is identical
482 to (\fIformataddr\fR\^) in all other respects. Note that (\fIconcataddr\fR\^)
485 add addresses to the duplicate-suppression cache.
486 .SS Other Hints and Tips
487 Sometimes, the writer of a format function is confused because output is
488 duplicated. The general rule to remember is simple: If a function or
489 component escape begins with a `%', it will generate text in the output file.
490 Otherwise, it will not.
492 A good example is a simple attempt to generate a To: header based on
493 the From: and Reply-To: headers:
497 %(formataddr %<{reply-to}%|%{from})%(putaddr To: )
501 Unfortunately, if the Reply-to: header is
503 present, the output line
504 will be something like:
508 My From User <from@example.com>To: My From User <from@example.com>
512 What went wrong? When performing the test for the
514 clause (%<), the component is not output because it is considered an
517 statement (so the rule about not starting with % applies). But the component
520 statement (everything after the `%|') is
522 an argument to anything;
523 it begins with a %, and thus the value of that component is output.
524 This also has the side effect of setting the
526 register, which is later picked up by the (\fIformataddr\fR\^) function
527 and then output by (\fIputaddr\fR\^). The example format string above
528 has another bug: there should always be a valid width value in the
530 register when (\fIputaddr\fR\^) is called, otherwise bad formatting can take
533 The solution is to use the (\fIvoid\fR\^) function; this will prevent the
534 function or component from outputting any text. With this in place (and
535 using (\fIwidth\fR\^) to set the
537 register for the width) a better implementation would look like:
541 %(formataddr %<{reply-to}%|%(void{from})%(void(width))%(putaddr To: )
545 It should be noted here that the side effects of function and component
546 escapes are still in force and, as a result, each component test in the
547 .B if-elseif-else-endif
552 As an additional note, the (\fIformataddr\fR\^) and (\fIconcataddr\fR\^)
553 functions have special behavior when it comes to the
555 register. The starting point of the register is saved and is used to
556 build up entries in the address list.
560 utility invaluable when debugging problems with format strings.
562 With all the above in mind, here is a breakdown of the default format string for
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 is present it is printed, followed by a space;
594 otherwise a `*' is printed.
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, consider a possible
634 %(lit)%(formataddr %<{reply-to}
640 and formats the \*(lqReply-To:\*(rq header
641 if present. If not present, the else-if clause is executed.
645 %?{from}%?{sender}%?{return-path}%>)\\
650 \*(lqFrom:\*(rq, \*(lqSender:\*(rq and \*(lqReturn-Path:\*(rq
651 headers, stopping as soon as one of them is present. Next:
655 %<(nonnull)%(void(width))%(putaddr To: )\\n%>\\
659 If the \fIformataddr\fR result is non-null, it is printed as
660 an address (with line folding if needed) in a field \fIwidth\fR
661 wide, with a leading label of \*(lqTo:\*(rq.
665 %(lit)%(formataddr{to})%(formataddr{cc})%(formataddr(me))\\
670 is cleared, and the \*(lqTo:\*(rq and \*(lqCc:\*(rq headers, along with the user's
671 address (depending on what was specified with
672 the \*(lq\-cc\*(rq switch to \fIrepl\fR\^) are formatted.
676 %<(nonnull)%(void(width))%(putaddr cc: )\\n%>\\
680 If the result is non-null, it is printed as above with a
681 leading label of \*(lqcc:\*(rq.
685 %<{fcc}Fcc: %{fcc}\\n%>\\
696 for more details about %{\fIfcc\fR\^}),
697 an \*(lqFcc:\*(rq header is output.
701 %<{subject}Subject: Re: %{subject}\\n%>\\
705 If a subject component was present,
706 a suitable reply subject is output.
710 %<{message-id}In-Reply-To: %{message-id}\\n%>\\
711 %<{message-id}References: %<{references} %{references}%>\\
717 If a message-id component was present, an \*(lqIn-Reply-To:\*(rq header is
718 output including the message-id, followed by a \*(lqReferences:\*(rq
719 header with references, if present, and the message-id.
721 plain-text, the row of dashes are output as-is.
723 This last part is a good example for a little more elaboration.
724 Here's that part again in pseudo-code:
729 if (comp_exists(message-id)) then
730 print (\*(lqIn-reply-to: \*(rq)
731 print (message-id.value)
732 print (\*(lq\\n\*(rq)
734 if (comp_exists(message-id)) then
735 print (\*(lqReferences: \*(rq)
736 if (comp_exists(references)) then
737 print(references.value);
739 print (message-id.value)
740 print (\*(lq\\n\*(rq)
745 .\" (Note that this pseudocode begs the question ``why not just
746 .\" support this syntax?'' MH has been hacked on for a long time...)
748 One more example: Currently,
750 supports very large message numbers, and it is not uncommon for a folder
751 to have far more than 10000 messages.
752 .\" (Indeed, the original MH
753 .\" tutorial document by Rose and Romine is entitled "How to
754 .\" process 200 messages a day and still get some real work
755 .\" done." The authors apparently only planned to get
756 .\" real work done for about 50 days per folder.)
757 Nonetheless (as noted above) the various scan format strings, inherited
758 from older MH versions, are generally hard-coded to 4 digits for the message
759 number. Thereafter, formatting problems occur.
760 The nmh format strings can be modified to behave more sensibly with larger
765 %(void(msg))%<(gt 9999)%(msg)%|%4(msg)%>
769 The current message number is placed in \fInum\fP.
772 is a function escape which returns an integer, it is not a component.)
776 is used to test whether the message number
779 If so, it is printed at full width, otherwise