<<< Date Index >>>     <<< Thread Index >>>

[PATCH] Pretty-print variable values



Hi,

the attached patch pretty-prints values for string based options. I'm not really sure if we really need this for all cases (esp. pretty-printing for paths), but to me it simply looks better if \r, \n and \t are pretty-printed and " as well as \ are properly escaped.

With this patch at least $attach_sep prints to attach_sep="\n" instead of attach_sep="?" within mutt.

Opinions?

  bye, Rocco
--
:wq!
diff --git a/init.c b/init.c
index 100caf7..be14882 100644
--- a/init.c
+++ b/init.c
@@ -1676,6 +1676,47 @@ static void mutt_restore_default (struct option_t *p)
     set_option (OPTREDRAWTREE);
 }
 
+static void pretty_var (char *dst, size_t len, const char *option, const char 
*val)
+{
+  char *p = dst;
+  size_t l = mutt_strlen (option);
+
+  len -= 2; /* save room for "\0 */
+  strfcpy (dst, option, len);
+  p += l;
+
+  if (p - dst + 2 < len)
+  {
+    *p++ = '=';
+    *p++ = '"';
+  }
+
+#define ESC_CHAR(C)    do { *p++ = '\\'; if (p - dst < len - 1) *p++ = C; } 
while(0)
+  while (p - dst < len && val && *val)
+  {
+    switch (*val)
+    {
+    case '\n':
+      ESC_CHAR('n');
+      break;
+    case '\r':
+      ESC_CHAR('r');
+      break;
+    case '\t':
+      ESC_CHAR('t');
+      break;
+    default:
+      if (strchr ("\"\\", *val) != NULL && p - dst < len - 1)
+       *p++ = '\\';
+      *p++ = *val;
+    }
+    val++;
+  }
+#undef ESC_CHAR
+  *p++ = '"';
+  *p = 0;
+}
+
 static int parse_set (BUFFER *tmp, BUFFER *s, unsigned long data, BUFFER *err)
 {
   int query, unset, inv, reset, r = 0;
@@ -1829,7 +1870,7 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned 
long data, BUFFER *err)
         {
           if ((val = myvar_get (myvar)))
           {
-            snprintf (err->data, err->dsize, "%s=\"%s\"", myvar, val);
+           pretty_var (err->data, err->dsize, myvar, val);
             break;
           }
           else
@@ -1844,12 +1885,18 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned 
long data, BUFFER *err)
          rfc822_write_address (_tmp, sizeof (_tmp), *((ADDRESS **) 
MuttVars[idx].data), 0);
          val = _tmp;
        }
+       else if (DTYPE (MuttVars[idx].type) == DT_PATH)
+       {
+         _tmp[0] = '\0';
+         strfcpy (_tmp, *((char **) MuttVars[idx].data), sizeof (_tmp));
+         mutt_pretty_mailbox (_tmp);
+         val = _tmp;
+       }
        else
          val = *((char **) MuttVars[idx].data);
        
        /* user requested the value of this variable */
-       snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
-                 NONULL (val));
+       pretty_var (err->data, err->dsize, MuttVars[idx].option, NONULL(val));
        break;
       }
       else
@@ -1904,8 +1951,7 @@ static int parse_set (BUFFER *tmp, BUFFER *s, unsigned 
long data, BUFFER *err)
       if (query || *s->dptr != '=')
       {
        /* user requested the value of this variable */
-       snprintf (err->data, err->dsize, "%s=\"%s\"", MuttVars[idx].option,
-                 NONULL (ptr->pattern));
+       pretty_var (err->data, err->dsize, MuttVars[idx].option, 
NONULL(ptr->pattern));
        break;
       }