Nested If Feature

Allow complex nested conditions in format strings

1. Support

Since: NeoMutt 2016-03-07

Dependencies: None

2. Introduction

NeoMutt's format strings can contain embedded if-then-else conditions. They are of the form:

%?VAR?TRUE&FALSE?

If the variable VAR has a value greater than zero, print the TRUE string, otherwise print the FALSE string.

e.g. %?S?Size: %S&Empty?

Which can be read as:

if (%S > 0) { print "Size: %S" } else { print "Empty" }

These conditions are useful, but in NeoMutt they cannot be nested within one another. This feature uses the notation %<VAR?TRUE&FALSE> and allows them to be nested.

The %<...> notation was used to format the current local time. but that's not really very useful since NeoMutt has no means of refreshing the screen periodically.

A simple nested condition might be: (Some whitespace has been introduced for clarity)

%<x? %<y? XY & X > & %<y? Y & NONE > >  Conditions
     %<y? XY & X >                      x>0
          XY                            x>0,y>0
               X                        x>0,y=0
%<x? %<y? XY & X > & %<y? Y & NONE > >  Conditions
                     %<y? Y & NONE >    x=0
                          Y             x=0,y>0
                              NONE      x=0,y=0

Equivalent to:

if (x > 0) {
  if (y > 0) {
    print 'XY'
  } else {
    print 'X'
  }
} else {
  if (y > 0) {
    print 'Y'
  } else {
    print 'NONE'
  }
}

Examples:

set index_format='%4C %Z %{%b %d} %-25.25n %s%> %<M?%M Msgs &%<l?%l Lines&%c Bytes>>'

if a thread is folded display the number of messages (%M)
else if we know how many lines in the message display lines in message (%l)
else display the size of the message in bytes (%c)

set index_format='%4C %Z %{%b %d} %-25.25n %<M?[%M] %s&%s%* %<l?%l&%c>>'

if a thread is folded display the number of messages (%M) and the subject (%s)
else if we know how many lines are in the message display subject (%s) and the lines in message (%l)
else display the subject (%s) and the size of the message in bytes (%c)

Note

If you wish to use angle brackets < > in a nested condition, then it's necessary to escape them, e.g.

set index_format='%<M?\<%M\>&%s>'

3. Variables

The nested-if feature doesn't have any config of its own. It modifies the behavior of the format strings.

4. neomuttrc

# Example NeoMutt config file for the nested-if feature.

# This feature uses the format: '%<VAR?TRUE&FALSE>' for conditional
# format strings that can be nested.

# Example 1
# if a thread is folded
#       display the number of messages (%M)
# else if we know how many lines in the message
#       display lines in message (%l)
# else display the size of the message in bytes (%c)
set index_format='%4C %Z %{%b %d} %-25.25n %s%> %<M?%M Msgs &%<l?%l Lines&%c Bytes>>'

# Example 2
# if a thread is folded
#       display the number of messages (%M)
#       display the subject (%s)
# else if we know how many lines in the message
#       display lines in message (%l)
# else
#       display the size of the message in bytes (%c)
set index_format='%4C %Z %{%b %d} %-25.25n %<M?[%M] %s&%s%* %<l?%l&%c>>'

# vim: syntax=neomuttrc

6. Known Bugs

This feature is hard to understand

7. Credits

David Champion, Richard Russon, Aleksa Sarai

Search by Algolia