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