MH-FORMAT(5mh)

πŸ“¦
version 1.8 Β· 4 versions
, in debian trixie, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ MH-FORMAT(5mh) β€” formatting language for nmh, bestie ✨

🌸 NAME 🌸

mh-format β€” formatting language for nmh message system

πŸ’… DESCRIPTION πŸ’…

okay so, like, several nmh commands are out here using either a format string or a format file while they do their thing. For example, scan uses a format string to generate its listing of messages; repl uses a format file to generate message replies, and so on. it's giving very customizable, no cap.

There are a number of scan listing formats available, including nmh/etc/scan.time, nmh/etc/scan.size, and nmh/etc/scan.timely. Look in /etc/nmh for other scan and repl format files which may have been written at your site. πŸ‘€

You can have your local nmh expert write new format commands or modify existing ones, or you can try your hand at it yourself. This manual section explains how to do that. Note: some familiarity with the C printf routine is assumed. so like, if printf is your bestie already, slay.

so a format string is literally just normal text mixed with these special, multi-character escape sequences that start with %. when you're writing 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. and you can insert a literal % into a format file by using the sequence %%. πŸ’–

✨ SYNTAX ✨

Format strings are built around escape sequences. there are three types of escape sequence: header components, built-in functions, and flow control. you can insert comments in most places where a function argument is not expected. a comment begins with %; and ends with a (non-escaped) newline. periodt.

πŸŽ€ Component escapes πŸŽ€

A component escape is specified as %{component}, and exists for each header in the message being processed. For example, %{date} refers to the β€œDate:” field of the message. All component escapes have a string value. Such values are usually compressed by converting any control characters (tab and newline included) to spaces, then eliding any leading or multiple spaces. Some commands, however, may interpret some component escapes differently; be sure to refer to each command's manual entry for details. Some commands (such as ap(8) and mhl(1)) use a special component %{text} to refer to the text being processed; see their respective man pages for details and examples. πŸ’Œ

🌈 Function escapes 🌈

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

%(function argument)

In addition to literal numbers or strings, the argument to a function escape can be another function, or a component, or a control escape. When the argument is a function or a component, the argument is specified without a leading %. When the argument is a control escape, it is specified with a leading %. iykyk.

πŸ’« 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, which may be nested without ambiguity, 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". main character energy fr.

A %< or %? control escape causes its condition to be evaluated. This condition is a component or function. For components and functions whose value is an integer, the condition is true if it is non-zero, and false if zero. For components and functions whose value is a string, the condition is true it is a non-empty string, and false if an empty string. (yes, the original says β€œtrue it is a non-empty string” β€” we're keeping every detail, so here it is, bestie. no fixing, just vibes.)

The %? control escape is optional, and can be used multiple times in a conditional block. The %| control escape is also optional, but may only be used once.

πŸ¦‹ Function escapes πŸ¦‹

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

  • literal β€” A literal number or string. Examples: %(func 1234), %(func text string)
  • comp β€” Any component. Example: %(func{in-reply-to})
  • date β€” A date component. Example: %(func{date})
  • addr β€” An address component. Example: %(func{from})
  • expr β€” Nothing, or a subexpression, or a control escape. Examples: %(func), %(func(func2)), %(func %<{reply-to}%|%{from}%>)

The date and addr types have the same syntax as the component type, comp, but require a header component which is a date, or address, string, respectively. πŸ’…

Most arguments not of type expr are required. When escapes are nested (via expr arguments), evaluation is done from innermost to outermost. 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. no cap.

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:”. iconic behavior tbh.

🧚 Evaluation 🧚

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 substituted as the argument: the register used depends on the function, as listed below. (we'll get to that list later, bestie, don't stress.)

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 escape 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. slay.

πŸ‘‘ Functions πŸ‘‘

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

kay so here's the full function menu, bestie 🌸 read it like a little chart you're holding in your head β€” which one you're calling, what kind of argument it wants, what it hands back, and what it actually does. slay.

  • msg β€” integer β€” message number
  • cur β€” integer β€” is the message current (0 or 1)
  • unseen β€” integer β€” is the message unseen (0 or 1)
  • size β€” integer β€” size of the message
  • strlen β€” integer β€” length of str
  • width β€” integer β€” column width of the terminal
  • charleft β€” integer β€” bytes left in the output buffer
  • timenow β€” integer β€” seconds since the Unix epoch
  • me β€” string β€” the user's mailbox (username)
  • myhost β€” string β€” the user's local hostname
  • myname β€” string β€” the user's name
  • localmbox β€” string β€” the complete local mailbox
  • eq (takes a literal) β€” boolean β€” num == arg
  • ne (takes a literal) β€” boolean β€” num != arg
  • gt (takes a literal) β€” boolean β€” num > arg
  • match (takes a literal) β€” boolean β€” str contains arg
  • amatch (takes a literal) β€” boolean β€” str starts with arg
  • plus (takes a literal) β€” integer β€” arg plus num
  • minus (takes a literal) β€” integer β€” arg minus num
  • multiply (takes a literal) β€” integer β€” num multiplied by arg
  • divide (takes a literal) β€” integer β€” num divided by arg
  • modulo (takes a literal) β€” integer β€” num modulo arg
  • num (takes a literal) β€” integer β€” set num to arg.
  • num (no argument) β€” integer β€” set num to zero.
  • lit (takes a literal) β€” string β€” set str to arg.
  • lit (no argument) β€” string β€” clear str.
  • getenv (takes a literal) β€” string β€” set str to the environment value of arg
  • profile (takes a literal) β€” string β€” set str to the profile or context component arg value
  • nonzero (takes an expr) β€” boolean β€” num is non-zero
  • zero (takes an expr) β€” boolean β€” num is zero
  • null (takes an expr) β€” boolean β€” str is empty
  • nonnull (takes an expr) β€” boolean β€” str is non-empty
  • void (takes an expr) β€” set str or num
  • comp (takes a comp) β€” string β€” set str to component text
  • compval (takes a comp) β€” integer β€” set num to atoi(comp)
  • decode (takes an expr) β€” string β€” decode str as RFC 2047 (MIME-encoded) component
  • unquote (takes an expr) β€” string β€” remove RFC 2822 quotes from str
  • trim (takes an expr) β€” trim trailing whitespace from str
  • trimr (takes an expr) β€” string β€” like %(trim), also returns string
  • kilo (takes an expr) β€” string β€” express in SI units: 15.9K, 2.3M, etc. %(kilo) scales by factors of 1000,
  • kibi (takes an expr) β€” string β€” express in IEC units: 15.5Ki, 2.2Mi. %(kibi) scales by factors of 1024.
  • ordinal (takes an expr) β€” string β€” output the ordinal suffix based on the value of num (st, nd, rd, th)
  • putstr (takes an expr) β€” print str
  • putstrf (takes an expr) β€” print str in a fixed width
  • putnum (takes an expr) β€” print num
  • putnumf (takes an expr) β€” print num in a fixed width
  • putlit (takes an expr) β€” print str without space compression
  • zputlit (takes an expr) β€” print str without space compression; str must occupy no width on display
  • bold β€” string β€” set terminal bold mode
  • underline β€” string β€” set terminal underlined mode
  • standout β€” string β€” set terminal standout mode
  • resetterm β€” string β€” reset all terminal attributes
  • hascolor β€” boolean β€” terminal supports color
  • fgcolor (takes a literal) β€” string β€” set terminal foreground color
  • bgcolor (takes a literal) β€” string β€” set terminal background color
  • formataddr (takes an expr) β€” append arg to str as a (comma separated) address list
  • concataddr (takes an expr) β€” append arg to str as a (comma separated) address list, including duplicates, see Special Handling
  • putaddr (takes a literal) β€” print str address list with arg as optional label; get line width from num

Now the ✨lore✨ on a few of those, because they're special:

The (me) function returns the username of the current user. The (myhost) function returns the localname entry in mts.conf, or the local hostname if localname is not configured. The (myname) function will return the value of the SIGNATURE environment variable if set, otherwise it will return the passwd GECOS field (truncated at the first comma if it contains one) for the current user. The (localmbox) function will return the complete form of the local mailbox, suitable for use in a From header. It will return the Local-Mailbox profile entry if there is one; if not, it will be equivalent to:

%(myname) <%(me)@%(myhost)>

okay and then these next ones need a date component handed to them as an argument πŸŽ€:

  • 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? (1=explicit, 0=implicit, -1=unknown)
  • 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 minutes
  • tzone β€” date β†’ string β€” timezone string
  • szone β€” date β†’ integer β€” timezone explicit? (1=explicit, 0=implicit, -1=unknown)
  • 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
  • nodate β€” date β†’ integer β€” returns 1 if date is invalid

and these cuties want an address component as their argument πŸ’Œ and heads up: anything marked with a * computes its return value from the first address present in the header component. iykyk.

  • 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)
  • getmymbox β€” addr β†’ string β€” the user's (first) address, with personal name
  • getmyaddr β€” addr β†’ string β€” the user's (first) address, without personal name
  • 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, -1=uucp, 2=unknown)
  • path β€” addr β†’ string β€” any leading host route*
  • ingrp β€” addr β†’ integer β€” address was inside a group (0 or 1)*
  • gname β€” addr β†’ string β€” name of group*

ngl, 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.

and the friendly{comp}) call? it'll return any double-quoted "personal name" (that is, anything before <>), then it will return that. If there's no personal name but there is a "note" (comments string after an email address), it will return that. If there is neither of those it will just return the bare email address. cute, efficient, no notes πŸ’…


✨ Formatting ✨

okay so when a function or component escape gets interpreted and the result is about to be printed immediately, you can optionally specify a field width to print the field in exactly a given number of characters πŸŽ€

So like, a numeric escape like %4(size) will print at most 4 digits of the message size; overflow gets 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 on the right with the fill character (normally, a blank). And if the field width argument begins with a leading zero, then the fill character gets set to a zero. slay.

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. Using a negative value for the field width causes right-justification within the field, with padding on the left up to the field width. Padding is with spaces except for a left-padded putnumf when the width starts with zero. πŸ’…

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 (putlit) function outputs the exact contents of the str register without any changes such as duplicate space removal or control character conversion. Similarly, the (zputlit) function outputs the exact contents of the str register, but requires that those contents not occupy any output width. It can therefore be used for outputting terminal escape sequences. iconic honestly 🌈

Okay bestie, we're in the ✨terminal escape✨ zone now, so buckle up πŸ’…

There are only a limited number of function escapes that spit out terminal escape sequences, and they're all pulled from the terminfo(5) database based on whatever your terminal setting is right now. So here's the lineup:

  • (bold) sets bold mode, obviously 🫢
  • (underline) sets underline mode
  • (standout) sets standout mode
  • (hascolor) β€” use this to figure out if your current terminal even does color
  • (fgcolor) sets the foreground color
  • (bgcolor) sets the background color

Both (fgcolor) and (bgcolor) take exactly one literal argument β€” the color name β€” and it has to be one of: black, red, green, yellow, blue, magenta, cyan, white. That's the whole palette, no custom hex codes, sorry babe 😭

(resetterm) resets all terminal attributes back to their default setting.

Heads up fr fr: these terminal escapes should be used together with (zputlit) (this one's preferred, lowkey iconic) or (putlit), because the normal (putstr) function will strip out control characters. Not the vibe you want. πŸ’€

The available output width lives in an internal register, and literally anything that goes past that width gets truncated. The one exception? (zputlit) functions will still run even if a terminal reset code is being dropped at the end of a line. Little exception, big energy. ✨


πŸŽ€ Special Handling πŸŽ€

Some functions act different depending on which command called them β€” it's giving main character behavior, honestly.

In repl, the (formataddr) function stores every email address it bumps into inside an internal cache, and then uses that cache to suppress duplicate addresses. So if you need an address list that includes addresses you've already seen, you gotta use the (concataddr) function instead β€” it's identical to (formataddr) in every other way. Just know that (concataddr) does not add addresses to that duplicate-suppression cache. Noted. πŸ“


🌸 Other Hints and Tips 🌸

Sometimes whoever's writing a format function gets confused because the output is duplicated, and they're like "why is this happening to me 😭". The general rule to remember is simple, no cap: if a function or component escape begins with a %, it will generate text in the output file. Otherwise, it will not.

A good example is a simple attempt to generate a To: header based on the From: and Reply-To: headers:

%(formataddr %<{reply-to}%|%{from})%(putaddr To: )

Unfortunately, if the Reply-to: header is not present, the output line will be something like:

My From User <from@example.com>To: My From User <from@example.com>

So what went wrong bestie?? When it performs the test for the if clause (%<), the component is not output because it's considered an argument to the if statement (so that rule about not starting with % kicks in). But the component escape in our else statement (everything after the %|) is not an argument to anything; it begins with a %, and thus the value of that component is output. This also has the side effect of setting the str register, which is later picked up by the (formataddr) function and then output by (putaddr). And the example format string above has another bug: there should always be a valid width value in the num register when (putaddr) is called, otherwise bad formatting can take place. Two bugs, one string. It's giving chaos. πŸ’€

The solution is to use the (void) function β€” this prevents the function or component from outputting any text. With that in place (and using (width) to set the num register for the width), a better implementation would look like:

%(formataddr %<{reply-to}%|%(void{from})%(void(width))%(putaddr To: )

Also should be noted here: the side effects of function and component escapes are still in force, and as a result, each component test in the if-elseif-else-endif clause sets the str register. She's always working, even when she's quiet. πŸ‘‘

And as an additional note, the (formataddr) and (concataddr) functions have special behavior when it comes to the str register β€” the starting point of the register is saved and is used to build up entries in the address list.

You will find the fmttest(1) utility invaluable when debugging problems with format strings. Like genuinely, keep it in your back pocket. 🫢


πŸ‘‘ Examples πŸ‘‘

With all the above in mind, here's a breakdown of the default format string for scan. The first part is:

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

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 if an β€œEncrypted:” field is present then an E, otherwise a space, should be printed. Next:

%02(mon{date})/%02(mday{date})

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

%<{date} %|*%>

If a β€œDate:” field is present it is printed, followed by a space; otherwise a * is printed. Next,

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

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,

%<(zero)%17(decode(friendly{from}))%>

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

%(decode{subject})%<{body}<<%{body}>>%>

the mime-decoded subject and initial body (if any) are printed. Slay. ✨

For a more complicated example, 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.

%?{from}%?{sender}%?{return-path}%>)\

This formats the β€œFrom:”, β€œSender:” and β€œ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:”.

%(lit)%(formataddr{to})%(formataddr{cc})%(formataddr(me))\

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:”.

%<{fcc}Fcc: %{fcc}\n%>\

If a -fcc folder switch was given to repl (see repl(1) for more details about %{fcc}), an β€œFcc:” header is output.

%<{subject}Subject: Re: %{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}%>\
%{message-id}\n%>
--------

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. Periodt. πŸ’…

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: ”)
     print (message-id.value)
     print (β€œ\n”)
endif
if (comp_exists(message-id)) then
     print (β€œReferences: ”)
     if (comp_exists(references)) then
           print(references.value);
     endif
     print (message-id.value)
     print (β€œ\n”)
endif

One more example: Currently, nmh supports very large message numbers, and it is not uncommon for a folder to have far more than 10000 messages. Nonetheless (as noted above) the various scan format strings, inherited from older MH versions, are generally hard-coded to 4 digits for the message number. Thereafter, formatting problems occur. The nmh 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 a function escape which returns an integer, it is 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. Smart, honestly. 🧠✨


🌈 SEE ALSO 🌈

scan(1), repl(1), fmttest(1)


πŸ¦‹ CONTEXT πŸ¦‹

None


And that's the whole page bestie!! πŸ’– This was nmh-1.8, dated 2015-01-10, from MH-FORMAT(5mh) β€” go forth and write the most gorgeous format strings of your life, you've got this fr fr πŸŒΈπŸ‘‘

πŸ“¦ where this page comes from