Scroll to navigation

MH-FORMAT(5mh) [mmh-0.4] MH-FORMAT(5mh)


mh-format - format file for mh message system


Several mmh commands utilize either a format string or a format file during their execution. For example, scan uses a format string which directs it how to generate the scan listing for each message; repl uses a format file which directs it how to generate the reply to a message, and so on.

There are a few alternate scan listing formats available, e.g. scan.nmh, scan.mailx, and scan.timely. Look in /etc/mmh for other scan and repl format files.

This manual section explains how to write and modify format commands. Note: familiarity with the C printf routine is assumed.

A format string consists of ordinary text, and special multi-character escape sequences which begin with `%'. When specifying a format string, the usual C backslash characters are honored: `\b', `\f', `\n', `\r', and `\t'. Continuation lines in format files end with `\' followed by the newline character.


Format strings are built around escape sequences. There are four types of escape sequences:

1)  header components	%{component}
2)  built-in functions	%(function arg)
3)  flow control	%< ... %? ... %| ... %>
4)  comments	%; ...

Comments may be inserted in most places where no function argument is expected. A comment begins with `%;' and ends with a (non-escaped) newline.

A component escape is specified as `%{component}', and exists for each header found in the message being processed. For example `%{date}' refers to the `Date:' field of the appropriate message. All component escapes have a string value. Normally, component values are compressed by converting any control characters (tab and newline included) to spaces, then eliding any leading or multiple spaces. However, commands may give different interpretations to some component escapes; be sure to refer to each command's manual entry for complete details.

A function escape is specified as `%(function)'. All functions are built-in, and most have a string or numeric value. A function escape may have an argument. The argument follows the function escape: separating whitespace is discarded: `%(function argument)'.

In addition to literal numbers or strings, the argument to a function escape can be another function, a component, or a control escape. When the argument is a function or a component, they are listed without a leading `%'. When control escapes are used as function arguments, they written as normally, with a leading `%'.

Control escapes

A control escape is one of: `%<', `%?', `%|', or `%>'. These are combined into the conditional execution construct:

%< condition format-text
%? condition format-text

... %| format-text %>

(Extra white space is shown here only for clarity.) These constructs may be nested without ambiguity. They form a general if-elseif-else-endif block where only one of the format-texts is interpreted. In other words, `%<' is like the "if", `%?' is like the "elseif", `%|' is like "else", and `%>' is like "endif".

A `%<' or `%?' control escape causes its condition to be evaluated. This condition is a component or function. For integer valued functions or components, the condition is true if the function return or component value is non-zero, and false if zero. For string valued functions or components, the condition is true if the function return or component value is a non-empty string, and false for an empty string.

The `%?' control escape is optional, and there may be more than one `%?' control escape in a conditional block. The `%|' control escape is also optional, but may be included at most once.

Function escapes

Functions expecting an argument generally require an argument of a particular type. In addition to the number and string types, these include:

Argument	Description	Example Syntax
literal	A literal number	%(func 1234)
	or string		%(func text string)
comp	Any component		%(func{in-reply-to})
date	A date component	%(func{date})
addr	An address component	%(func{from})
expr	Nothing	%(func)
	or a subexpression	%(func(func2))
	or control escape	%(func %<{reply-to}%|%{from}%>)

The types date and addr have the same syntax as comp, but require that the header component be a date string, or address string, respectively.

Most arguments not of type expr are required. When escapes are nested (via expr arguments), evaluation is done from inner-most to outer-most. As noted above, for the expr argument type, functions and components are written without a leading `%'. Control escape arguments must use a leading `%', preceded by a space.

For example,

%<(mymbox{from}) To: %{to}%>

writes the value of the header component `From:' to the internal register named str; then (mymbox) reads str and writes its result to the internal register named num; then the control escape evaluates num. If num is non-zero, the string `To:' is printed followed by the value of the header component `To:'.


The evaluation of format strings is performed by a small virtual machine. The machine is capable of evaluating nested expressions as described above, and in addition has an integer register num, and a text string register str. When a function escape that accepts an optional argument is processed, and the argument is not present, the current value of either num or str is used as the argument: which register is used depends on the function, as listed below.

Component escapes write the value of their message header in str. Function escapes write their return value in num for functions returning integer or boolean values, and in str for functions returning string values. (The boolean type is a subset of integers with usual values 0=false and 1=true.) Control escapes return a boolean value, setting num to 1 if the last explicit condition evaluated by a `%<' or `%?' control succeeded, and 0 otherwise.

All component escapes, and those function escapes which return an integer or string value, evaluate to their value as well as setting str or num. Outermost escape expressions in these forms will print their value, but outermost escapes which return a boolean value do not result in printed output.


The function escapes may be roughly grouped into a few categories.

Function	Argument   Result	Description
msg		integer	message number
cur		integer	message is current (0 or 1)
unseen		integer	message is unseen (0 or 1)
size		integer	size of message
strlen		integer	length of str
width		integer	output buffer size in bytes
charleft		integer	bytes left in output buffer
timenow		integer	seconds since the UNIX epoch
me		string	the user's mailbox
eq	literal	boolean	num == arg
ne	literal	boolean	num != arg
gt	literal	boolean	num > arg
match	literal	boolean	str contains arg
amatch	literal	boolean	str starts with arg
plus	literal	integer	arg plus num
minus	literal	integer	arg minus num
divide	literal	integer	num divided by arg
modulo	literal	integer	num modulo arg
num	literal	integer	Set num to arg.
num		integer	Set num to zero.
lit 	literal	string	Set str to arg.
lit		string	Clear str.
getenv 	literal	string	Set str to environment value of arg
profile	literal	string	Set str to profile component arg
nonzero	expr	boolean	num is non-zero
zero	expr	boolean	num is zero
null	expr	boolean	str is empty
nonnull	expr	boolean	str is non-empty
void	expr		Set str or num
comp	comp	string	Set str to component text
compval	comp	integer	Set num to `atoi(comp)'
decode	expr	string	decode str as RFC-2047 (MIME-encoded)
			component and print it
unquote	expr	string	remove RFC-2822 quotes from str
unmailto	expr	string	remove `mailto:' and < > from str
trim	expr		trim white-space from str
putstr	expr		print str
putstrf	expr		print str in a fixed width
putnum	expr		print num
putnumf	expr		print num in a fixed width
nodate	string	integer	Argument not a date string (0 or 1)
formataddr	expr		append arg to str as a
			(comma separated) address list
putaddr	literal		print str address list with
			arg as optional label;
			get line width from num

The following functions require a date component as an argument:

Function	Argument	Return	Description
sec	date	integer	seconds of the minute
min	date	integer	minutes of the hour
hour	date	integer	hours of the day (0-23)
wday	date	integer	day of the week (Sun=0)
day	date	string	day of the week (abbrev.)
weekday	date	string	day of the week
sday	date	integer	day of the week known?
mday	date	integer	day of the month
yday	date	integer	day of the year
mon	date	integer	month of the year
month	date	string	month of the year (abbrev.)
lmonth	date	string	month of the year
year	date	integer	year (may be > 100)
zone	date	integer	timezone in hours
tzone	date	string	timezone string
szone	date	integer	timezone explicit?
date2local	date		coerce date to local timezone
date2gmt	date		coerce date to GMT
dst	date	integer	daylight savings in effect? (0 or 1)
clock	date	integer	seconds since the UNIX epoch
rclock	date	integer	seconds prior to current time
tws	date	string	official RFC-822 rendering
pretty	date	string	user-friendly rendering

These functions require an address component as an argument. The return value of functions noted with `*' is computed from the first address present in the header component.

Function	Argument	Return	Description
proper	addr	string	official RFC-822 rendering
friendly	addr	string	user-friendly rendering
addr	addr	string	mbox@host or host!mbox rendering*
pers	addr	string	the personal name*
note	addr	string	commentary text*
mbox	addr	string	the local mailbox*
mymbox	addr	integer	List has the user's address? (0 or 1)
host	addr	string	the host domain*
nohost	addr	integer	no host was present (0 or 1)*
type	addr	integer	host type* (0=local,1=network,
path	addr	string	any leading host route*
ingrp	addr	integer	address was inside a group (0 or 1)*
gname	addr	string	name of group*

(A clarification on (mymbox{comp}) is in order. This function checks each of the addresses in the header component `comp' against the user's mailbox name and any `Alternate-Mailboxes'. It returns true if any address matches, however, it also returns true if the `comp' header is not present in the message. If needed, the (null) function can be used to explicitly test for this case.)


When a function or component escape is interpreted and the result will be immediately printed, an optional field width can be specified to print the field in exactly a given number of characters. For example, a numeric escape like %4(size) will print at most 4 digits of the message size; overflow will be indicated by a `?' in the first position (like `?234'). A string escape like %4(me) will print the first 4 characters and truncate at the end. Short fields are padded at the right with the fill character (normally, a blank). If the field width argument begins with a leading zero, then the fill character is set to a zero.

The functions (putnumf) and (putstrf) print their result in exactly the number of characters specified by their leading field width argument. For example, %06(putnumf(size)) will print the message size in a field six characters wide filled with leading zeros; %14(putstrf{from}) will print the `From:' header component in fourteen characters with trailing spaces added as needed. For putstrf, using a negative value for the field width causes right-justification of the string within the field, with padding on the left up to the field width. The functions (putnum) and (putstr) are somewhat special: they print their result in the minimum number of characters required, and ignore any leading field width argument.

The available output width is kept in an internal register; any output past this width will be truncated.


With all this in mind, here's a format string for scan. It's been divided into several pieces for readability. The first part is:

%4(msg)%<(cur)+%| %>%<{replied}-%| %>

which says that the message number should be printed in four digits. If the message is the current message then a `+' else a space should be printed; if a `Replied:' field is present then a `-' else a space should be printed. Next:


the month and date are printed in two digits (zero filled) separated by a slash. Next,

%<{date} %|*%>

If a `Date:' field was present, then a space is printed, otherwise a `*'. Next,


if the message is from me, and there is a `To:' header, print `To:' followed by a `user-friendly' rendering of the first address in the `To:' field; any MIME-encoded characters are decoded into the actual characters. Continuing,


if either of the above two tests failed, then the `From:' address is printed in a mime-decoded, `user-friendly' format. And finally,


the mime-decoded subject is printed.

For a more complicated example, next consider a possible replcomps format file.

%(lit)%(formataddr %<{reply-to}

This clears str and formats the `Reply-To:' header if present. If not present, the else-if clause is executed.


This formats the `From:', `Sender:' or `Return-Path:' headers, stopping as soon as one of them is present. Next:

%<(nonnull)%(void(width))%(putaddr To: )\n%>\

If the formataddr result is non-null, it is printed as an address (with line folding if needed) in a field width wide with a leading label of `To:'.


str is cleared, and the `To:' and `Cc:' headers, along with the user's address (depending on what was specified with the `-cc' switch to repl) are formatted.

%<(nonnull)%(void(width))%(putaddr cc: )\n%>\

If the result is non-null, it is printed as above with a leading label of `Cc:'.

%<{subject}Subject: Re: %(decode{subject})\n%>\

If a subject component was present, a suitable reply subject is output.

%<{message-id}In-Reply-To: %{message-id}\n%>\
%<{message-id}References: %<{references} %{references}%>\

If a message-id component was present, an `In-Reply-To:' header is output including the message-id, followed by a `References:' header with references, if present, and the message-id. As with all plain-text, the row of dashes are output as-is.

This last part is a good example for a little more elaboration. Here's that part again in pseudo-code:

if (comp_exists(message-id))  then
	print("In-reply-to: ")
if (comp_exists(message-id)) then
	print("References: ")
	if (comp_exists(references)) then

One more example: Mmh supports very large message numbers, and it is not uncommon for a folder to have far more than 10000 messages. Nonetheless several scan format strings are inherited from older MH versions, and are generally hard-coded to 4 digits of message number before formatting problems start to occur. The mh format strings can be modified to behave more sensibly with larger message numbers:

%(void(msg))%<(gt 9999)%(msg)%|%4(msg)%>

The current message number is placed in num. (Note that (msg) is an int function, not a component.) The (gt) conditional is used to test whether the message number has 5 or more digits. If so, it is printed at full width: otherwise at 4 digits.


scan(1), repl(1), ap(8), dp(8)



2019-01-06 MH.6.8