From: Aaron Schrab Date: Mon, 1 Feb 2016 01:04:48 +0000 (+0000) Subject: feature: cond-date X-Git-Tag: neomutt-20160822~43^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c935627f21a210dcf7f874969655e728891c79d5;p=neomutt feature: cond-date Use rules to choose date format --- diff --git a/hdrline.c b/hdrline.c index eef19e6c5..4b86352d0 100644 --- a/hdrline.c +++ b/hdrline.c @@ -328,6 +328,98 @@ hdr_format_str (char *dest, const char *cp; struct tm *tm; time_t T; + int i = 0, invert = 0; + + if (optional && ((op == '[') || (op == '('))) { + char *is; + T = time (NULL); + tm = localtime (&T); + T -= (op == '(') ? hdr->received : hdr->date_sent; + + is = (char *) prefix; + if (*is == '>') { + invert = 1; + is++; + } + + while (*is && (*is != '?')) { + int t = strtol (is, &is, 10); + /* semi-broken (assuming 30 days in all months) */ + switch (*(is++)) { + case 'y': + if (t > 1) { + t--; + t *= (60 * 60 * 24 * 365); + } + t += ((tm->tm_mon * 60 * 60 * 24 * 30) + + (tm->tm_mday * 60 * 60 * 24) + + (tm->tm_hour * 60 * 60) + + (tm->tm_min * 60) + + tm->tm_sec); + break; + + case 'm': + if (t > 1) { + t--; + t *= (60 * 60 * 24 * 30); + } + t += ((tm->tm_mday * 60 * 60 * 24) + + (tm->tm_hour * 60 * 60) + + (tm->tm_min * 60) + + tm->tm_sec); + break; + + case 'w': + if (t > 1) { + t--; + t *= (60 * 60 * 24 * 7); + } + t += ((tm->tm_wday * 60 * 60 * 24) + + (tm->tm_hour * 60 * 60) + + (tm->tm_min * 60) + + tm->tm_sec); + break; + + case 'd': + if (t > 1) { + t--; + t *= (60 * 60 * 24); + } + t += ((tm->tm_hour * 60 * 60) + + (tm->tm_min * 60) + + tm->tm_sec); + break; + + case 'H': + if (t > 1) { + t--; + t *= (60 * 60); + } + t += ((tm->tm_min * 60) + + tm->tm_sec); + break; + + case 'M': + if (t > 1) { + t--; + t *= (60); + } + t += (tm->tm_sec); + break; + + default: + break; + } + i += t; + } + + if (i < 0) + i *= -1; + + if (((T > i) || (T < (-1*i))) ^ invert) + optional = 0; + break; + } p = dest; diff --git a/init.h b/init.h index 3f3f96d9d..040277d2f 100644 --- a/init.h +++ b/init.h @@ -1372,6 +1372,10 @@ struct option_t MuttVars[] = { ** .dt %*X .dd soft-fill with character ``X'' as pad ** .de ** .pp + ** Date format expressions can be constructed based on relative dates. Using + ** the date formatting operators along with nested conditionals, the date + ** format can be modified based on how old a message is. See the section on + ** ``Conditional Dates'' for an explanation and examples ** ``Soft-fill'' deserves some explanation: Normal right-justification ** will print everything to the left of the ``%>'', displaying padding and ** whatever lies to the right only if there's room. By contrast, diff --git a/muttlib.c b/muttlib.c index 209c7ab90..0542f1e2c 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1217,7 +1217,15 @@ void mutt_FormatString (char *dest, /* output buffer */ if (*src == '?') { flags |= MUTT_FORMAT_OPTIONAL; + ch = *(++src); /* save the character to switch on */ src++; + cp = prefix; + count = 0; + while ((count < sizeof (prefix)) && (*src != '?')) { + *cp++ = *src++; + count++; + } + *cp = 0; } else { @@ -1233,12 +1241,12 @@ void mutt_FormatString (char *dest, /* output buffer */ count++; } *cp = 0; - } - if (!*src) - break; /* bad format */ + if (!*src) + break; /* bad format */ - ch = *src++; /* save the character to switch on */ + ch = *src++; /* save the character to switch on */ + } if (flags & MUTT_FORMAT_OPTIONAL) {