let the caller deal with M_FORMAT_ARROWCURSOR, not the callee.
[apps/madmutt.git] / muttlib.c
index e56bbcd..9c108b7 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -328,20 +328,18 @@ void mutt_safe_path(char *s, ssize_t l, address_t *a)
     }
 }
 
-ssize_t
-mutt_FormatString(char *dst, ssize_t dlen, const char *fmt,
-                  format_t *callback, unsigned long data, format_flag flags)
+ssize_t m_strformat(char *dst, ssize_t dlen, const char *fmt,
+                    format_t *callback, unsigned long data, format_flag flags)
 {
-    ssize_t pos = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
+    ssize_t pos = flags & M_FORMAT_ARROWCURSOR ? 3 : 0;
 
     while (*fmt) {
-        char ifstring[STRING] = "", elsestring[STRING] = "", prefix[STRING] = "";
+        char ifstring[STRING], elsestring[STRING], prefix[STRING] = "";
         int ch;
 
         if (*fmt == '%') {
             if (*++fmt == '%') {
-                pos += m_strputc(dst + pos, dlen - pos, '%');
-                fmt++;
+                pos += m_strputc(dst + pos, dlen - pos, *fmt++);
                 continue;
             }
 
@@ -384,6 +382,8 @@ mutt_FormatString(char *dst, ssize_t dlen, const char *fmt,
 
                 if (!*fmt++)              /* move past the trailing `?' */
                     break;                /* bad format */
+            } else {
+                *ifstring = *elsestring = '\0';
             }
 
             switch (ch) {
@@ -396,23 +396,16 @@ mutt_FormatString(char *dst, ssize_t dlen, const char *fmt,
                 ch = *fmt++;            /* pad char */
 
                 if (COLS - SW > col) {
-                    ssize_t wid;
-
-                    mutt_FormatString(buf, sizeof(buf), fmt, callback, data, flags);
-                    wid = mutt_strwidth(buf);
-
-                    pos += m_strpad(dst + pos, dlen - pos, ch, COLS - SW - col - wid);
+                    m_strformat(buf, sizeof(buf), fmt, callback, data, flags);
+                    pos += m_strpad(dst + pos, dlen - pos, ch,
+                                    COLS - SW - col - mutt_strwidth(buf));
                     pos += m_strcpy(dst + pos, dlen - pos, buf);
                 }
                 return pos;             /* skip rest of input */
 
-              case '|':
-                col = mutt_strwidth(dst);
-
-                ch = *fmt++;
-                /* pad to EOL */
-                pos += m_strpad(dst + pos, dlen - pos, ch, COLS - SW - col);
-                return pos;             /* skip rest of input */
+              case '|':                 /* pad to EOL */
+                return pos + m_strpad(dst + pos, dlen - pos, *fmt,
+                                      COLS - SW - mutt_strwidth(dst));
 
               default:
                 lower = nodots = 0;
@@ -440,22 +433,22 @@ mutt_FormatString(char *dst, ssize_t dlen, const char *fmt,
                 }
 
                 pos += m_strcpy(dst + pos, dlen - pos, buf);
-                continue;
+                break;
             }
+            continue;
         }
 
         if (*fmt == '\\') {
             if (!*++fmt)
                 break;
-            switch (*fmt) {
+            switch ((ch = *fmt++)) {
               case 'n': pos += m_strputc(dst + pos, dlen - pos, '\n'); break;
               case 't': pos += m_strputc(dst + pos, dlen - pos, '\t'); break;
               case 'r': pos += m_strputc(dst + pos, dlen - pos, '\r'); break;
               case 'f': pos += m_strputc(dst + pos, dlen - pos, '\f'); break;
               case 'v': pos += m_strputc(dst + pos, dlen - pos, '\v'); break;
-              default:  pos += m_strputc(dst + pos, dlen - pos, *fmt); break;
+              default:  pos += m_strputc(dst + pos, dlen - pos, ch);   break;
             }
-            fmt++;
         } else {
             ssize_t len = strcspn(fmt, "%\\");