using stls should not enable new CAPAs
[apps/madmutt.git] / lib-ui / color.c
index 87a8042..7aa0d16 100644 (file)
@@ -7,12 +7,10 @@
  * please see the file GPL in the top level source directory.
  */
 
-#include <lib-lib/lib-lib.h>
-
-#include "curses.h"
-
+#include <lib-ui/lib-ui.h>
 #include "pattern.h"
 #include "mutt.h"
+#include "madtty.h"
 
 /* globals */
 int *ColorQuote;
@@ -27,17 +25,6 @@ static int ColorQuoteSize;
 
 #define COLOR_DEFAULT (-2)
 
-typedef struct color_list {
-  short fg;
-  short bg;
-  short index;
-  short count;
-  struct color_list *next;
-} COLOR_LIST;
-
-static COLOR_LIST *ColorList = NULL;
-static int UserColors = 0;
-
 static struct mapping_t Colors[] = {
   {"black", COLOR_BLACK},
   {"blue", COLOR_BLUE},
@@ -84,23 +71,18 @@ static COLOR_LINE *mutt_new_color_line (void)
 
   p->fg = p->bg = -1;
 
-  return (p);
+  return p;
 }
 
-static void mutt_free_color_line (COLOR_LINE ** l, int free_colors)
+static void mutt_free_color_line(COLOR_LINE ** l)
 {
   COLOR_LINE *tmp;
 
   if (!l || !*l)
     return;
-
   tmp = *l;
 
-  if (free_colors && tmp->fg != -1 && tmp->bg != -1)
-    mutt_free_color (tmp->fg, tmp->bg);
-
   /* we should really use the container type for regular expressions. */
-
   regfree (&tmp->rx);
   pattern_list_wipe(&tmp->color_pattern);
   p_delete(&tmp->pattern);
@@ -123,146 +105,27 @@ void ci_start_color (void)
     /* special meaning: toggle the relevant attribute */
     ColorDefs[MT_COLOR_BOLD] = 0;
     ColorDefs[MT_COLOR_UNDERLINE] = 0;
-
-    start_color ();
-}
-
-int mutt_alloc_color (int fg, int bg)
-{
-  COLOR_LIST *p = ColorList;
-  int i;
-
-  /* check to see if this color is already allocated to save space */
-  while (p) {
-    if (p->fg == fg && p->bg == bg) {
-      (p->count)++;
-      return (COLOR_PAIR (p->index));
-    }
-    p = p->next;
-  }
-
-  /* check to see if there are colors left */
-  if (++UserColors > COLOR_PAIRS)
-    return (A_NORMAL);
-
-  /* find the smallest available index (object) */
-  i = 1;
-  for (;;) {
-    p = ColorList;
-    while (p) {
-      if (p->index == i)
-        break;
-      p = p->next;
-    }
-    if (p == NULL)
-      break;
-    i++;
-  }
-
-  p = p_new(COLOR_LIST, 1);
-  p->next = ColorList;
-  ColorList = p;
-
-  p->index = i;
-  p->count = 1;
-  p->bg = bg;
-  p->fg = fg;
-
-  if (fg == COLOR_DEFAULT)
-    fg = -1;
-  if (bg == COLOR_DEFAULT)
-    bg = -1;
-
-  init_pair (i, fg, bg);
-
-  return (COLOR_PAIR (p->index));
-}
-
-void mutt_free_color (int fg, int bg)
-{
-  COLOR_LIST *p, *q;
-
-  p = ColorList;
-  while (p) {
-    if (p->fg == fg && p->bg == bg) {
-      (p->count)--;
-      if (p->count > 0)
-        return;
-
-      UserColors--;
-
-      if (p == ColorList) {
-        ColorList = ColorList->next;
-        p_delete(&p);
-        return;
-      }
-      q = ColorList;
-      while (q) {
-        if (q->next == p) {
-          q->next = p->next;
-          p_delete(&p);
-          return;
-        }
-        q = q->next;
-      }
-      /* can't get here */
-    }
-    p = p->next;
-  }
 }
 
 static int
 parse_color_name (const char *s, int *col, int *attr, int brite, BUFFER * err)
 {
-  char *eptr;
-
-  if (m_strncasecmp(s, "bright", 6) == 0) {
-    *attr |= brite;
-    s += 6;
-  }
-
-  /* allow aliases for xterm color resources */
-  if (m_strncasecmp(s, "color", 5) == 0) {
-    s += 5;
-    *col = strtol (s, &eptr, 10);
-    if (!*s || *eptr || *col < 0 ||
-        (*col >= COLORS && !option (OPTNOCURSES) && has_colors ())) {
-      snprintf (err->data, err->dsize, _("%s: color not supported by term"),
-                s);
-      return (-1);
+    if (m_strncasecmp(s, "bright", 6) == 0) {
+        *attr |= brite;
+        s += 6;
     }
-  }
-  else if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
-    snprintf (err->data, err->dsize, _("%s: no such color"), s);
-    return (-1);
-  }
-
-  return 0;
-}
-
-/* usage: uncolor index pattern [pattern...]
- *       unmono  index pattern [pattern...]
- */
-
-static int
-_mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
-                     BUFFER * err, short parse_uncolor);
 
-int mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data,
-                        BUFFER * err)
-{
-  return _mutt_parse_uncolor (buf, s, data, err, 1);
-}
+    if ((*col = mutt_getvaluebyname (s, Colors)) == -1) {
+        snprintf (err->data, err->dsize, _("%s: no such color"), s);
+        return -1;
+    }
 
-int mutt_parse_unmono (BUFFER * buf, BUFFER * s, unsigned long data,
-                       BUFFER * err)
-{
-  return _mutt_parse_uncolor (buf, s, data, err, 0);
+    return 0;
 }
 
-static int
-_mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
-                     BUFFER * err, short parse_uncolor)
+/* usage: uncolor index pattern [pattern...] */
+int mutt_parse_uncolor(BUFFER *buf, BUFFER *s, unsigned long data, BUFFER
+                       *err)
 {
   int object = 0, do_cache = 0;
   COLOR_LINE *tmp, *last = NULL;
@@ -271,24 +134,21 @@ _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__
 
   if ((object = mutt_getvaluebyname (buf->data, Fields)) == -1) {
     snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
-    return (-1);
+    return -1;
   }
 
   if (m_strncmp(buf->data, "index", 5) != 0) {
     snprintf (err->data, err->dsize,
-              _("%s: command valid only for index object"),
-              parse_uncolor ? "uncolor" : "unmono");
-    return (-1);
+              _("%s: command valid only for index object"), "uncolor");
+    return -1;
   }
 
   if (!MoreArgs (s)) {
-    snprintf (err->data, err->dsize,
-              _("%s: too few arguments"),
-              parse_uncolor ? "uncolor" : "unmono");
-    return (-1);
+    snprintf (err->data, err->dsize, _("%s: too few arguments"), "uncolor");
+    return -1;
   }
 
-  if (option (OPTNOCURSES) || (parse_uncolor != has_colors())) {
+  if (option(OPTNOCURSES)) {
     /* just eat the command, but don't do anything real about it */
     do {
       mutt_extract_token (buf, s, 0);
@@ -304,7 +164,7 @@ _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__
           do_cache = 1;
         last = tmp;
         tmp = tmp->next;
-        mutt_free_color_line (&last, parse_uncolor);
+        mutt_free_color_line(&last);
       }
       ColorIndexList = NULL;
     }
@@ -318,7 +178,7 @@ _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__
             last->next = tmp->next;
           else
             ColorIndexList = tmp->next;
-          mutt_free_color_line (&tmp, parse_uncolor);
+          mutt_free_color_line(&tmp);
           break;
         }
       }
@@ -335,7 +195,7 @@ _mutt_parse_uncolor (BUFFER * buf, BUFFER * s, unsigned long data __attribute__
     for (i = 0; Context && i < Context->msgcount; i++)
       Context->hdrs[i]->pair = 0;
   }
-  return (0);
+  return 0;
 }
 
 
@@ -365,13 +225,12 @@ add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
   if (tmp) {
     if (fg != -1 && bg != -1) {
       if (tmp->fg != fg || tmp->bg != bg) {
-        mutt_free_color (tmp->fg, tmp->bg);
         tmp->fg = fg;
         tmp->bg = bg;
-        attr |= mutt_alloc_color (fg, bg);
-      }
-      else
+        attr |= madtty_color_pair(fg, bg);
+      } else {
         attr |= (tmp->pair & ~A_BOLD);
+      }
     }
     tmp->pair = attr;
   } else {
@@ -386,7 +245,7 @@ add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
       mutt_check_simple (buf, sizeof (buf), NONULL (SimpleSearch));
       if ((tmp->color_pattern =
            mutt_pattern_comp (buf, M_FULL_MSG, err)) == NULL) {
-        mutt_free_color_line (&tmp, 1);
+        mutt_free_color_line(&tmp);
         return -1;
       }
       /* force re-caching of index colors */
@@ -398,15 +257,15 @@ add_pattern (COLOR_LINE ** top, const char *s, int sensitive,
            REGCOMP (&tmp->rx, s,
                     (sensitive ? mutt_which_case (s) : REG_ICASE))) != 0) {
       regerror (r, &tmp->rx, err->data, err->dsize);
-      mutt_free_color_line (&tmp, 1);
-      return (-1);
+      mutt_free_color_line(&tmp);
+      return -1;
     }
     tmp->next = *top;
     tmp->pattern = m_strdup(s);
     if (fg != -1 && bg != -1) {
       tmp->fg = fg;
       tmp->bg = bg;
-      attr |= mutt_alloc_color (fg, bg);
+      attr |= madtty_color_pair(fg, bg);
     }
     tmp->pair = attr;
     *top = tmp;
@@ -442,42 +301,40 @@ parse_object (BUFFER * buf, BUFFER * s, int *o, int *ql, BUFFER * err)
   }
   else if ((*o = mutt_getvaluebyname (buf->data, Fields)) == -1) {
     snprintf (err->data, err->dsize, _("%s: no such object"), buf->data);
-    return (-1);
+    return -1;
   }
 
   return 0;
 }
 
-typedef int (*parser_callback_t) (BUFFER *, BUFFER *, int *, int *, int *,
-                                  BUFFER *);
-
 static int
 parse_color_pair (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
                   BUFFER * err)
 {
   if (!MoreArgs (s)) {
     m_strcpy(err->data, err->dsize, _("color: too few arguments"));
-    return (-1);
+    return -1;
   }
 
   mutt_extract_token (buf, s, 0);
 
   if (parse_color_name (buf->data, fg, attr, A_BOLD, err) != 0)
-    return (-1);
+    return -1;
 
   if (!MoreArgs (s)) {
     m_strcpy(err->data, err->dsize, _("color: too few arguments"));
-    return (-1);
+    return -1;
   }
 
   mutt_extract_token (buf, s, 0);
 
   if (parse_color_name (buf->data, bg, attr, A_BLINK, err) != 0)
-    return (-1);
+    return -1;
 
   return 0;
 }
 
+#if 0
 static int
 parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
                  BUFFER * err)
@@ -490,7 +347,7 @@ parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
 
   if (!MoreArgs (s)) {
     m_strcpy(err->data, err->dsize, _("mono: too few arguments"));
-    return (-1);
+    return -1;
   }
 
   mutt_extract_token (buf, s, 0);
@@ -509,27 +366,23 @@ parse_attr_spec (BUFFER * buf, BUFFER * s, int *fg, int *bg, int *attr,
     *attr = A_NORMAL;           /* needs use = instead of |= to clear other bits */
   else {
     snprintf (err->data, err->dsize, _("%s: no such attribute"), buf->data);
-    return (-1);
+    return -1;
   }
 
   return 0;
 }
+#endif
 
 static int fgbgattr_to_color (int fg, int bg, int attr)
 {
   if (fg != -1 && bg != -1)
-    return attr | mutt_alloc_color (fg, bg);
+    return attr | madtty_color_pair(fg, bg);
   else
     return attr;
 }
 
-/* usage: color <object> <fg> <bg> [ <regexp> ] 
- *       mono  <object> <attr> [ <regexp> ]
- */
-
-static int
-_mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
-                   parser_callback_t callback, short dry_run)
+/* usage: color <object> <fg> <bg> [ <regexp> ] */
+int mutt_parse_color(BUFFER *buf, BUFFER *s, unsigned long i, BUFFER *err)
 {
   int object = 0, attr = 0, fg = 0, bg = 0, q_level = 0;
   int r = 0;
@@ -537,7 +390,7 @@ _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
   if (parse_object (buf, s, &object, &q_level, err) == -1)
     return -1;
 
-  if (callback (buf, s, &fg, &bg, &attr, err) == -1)
+  if (parse_color_pair(buf, s, &fg, &bg, &attr, err) == -1)
     return -1;
 
   /* extract a regular expression if needed */
@@ -546,7 +399,7 @@ _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
       || object == MT_COLOR_INDEX) {
     if (!MoreArgs (s)) {
       m_strcpy(err->data, err->dsize, _("too few arguments"));
-      return (-1);
+      return -1;
     }
 
     mutt_extract_token (buf, s, 0);
@@ -554,21 +407,18 @@ _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
 
   if (MoreArgs (s)) {
     m_strcpy(err->data, err->dsize, _("too many arguments"));
-    return (-1);
+    return -1;
   }
 
-  /* dry run? */
-
-  if (dry_run)
+  if (option(OPTNOCURSES))
     return 0;
 
-
-  if (!option (OPTNOCURSES) && has_colors ()
-      /* delay use_default_colors() until needed, since it initializes things */
-      && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
-      && use_default_colors () != OK) {
+  /* delay use_default_colors() until needed, since it initializes things */
+  if (has_colors () && (fg == COLOR_DEFAULT || bg == COLOR_DEFAULT)
+  &&  use_default_colors () != OK)
+  {
     m_strcpy(err->data, err->dsize, _("default colors not supported"));
-    return (-1);
+    return -1;
   }
 
   if (object == MT_COLOR_HEADER)
@@ -595,37 +445,15 @@ _mutt_parse_color (BUFFER * buf, BUFFER * s, BUFFER * err,
         if (ColorQuote[q_level] == A_NORMAL)
           ColorQuote[q_level] = ColorDefs[MT_COLOR_QUOTED];
       }
-    }
-    else
+    } else {
       ColorQuote[q_level] = fgbgattr_to_color (fg, bg, attr);
-  }
-  else
+    }
+  } else {
     ColorDefs[object] = fgbgattr_to_color (fg, bg, attr);
+  }
 
-  if (object == MT_COLOR_NORMAL && !option (OPTNOCURSES) && has_colors ())
-    BKGDSET (MT_COLOR_NORMAL);
-
-  return (r);
-}
-
-int mutt_parse_color (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
-                      BUFFER * err)
-{
-  int dry_run = 0;
-
-  if (option (OPTNOCURSES) || !has_colors ())
-    dry_run = 1;
-
-  return _mutt_parse_color (buff, s, err, parse_color_pair, dry_run);
-}
-
-int mutt_parse_mono (BUFFER * buff, BUFFER * s, unsigned long data __attribute__ ((unused)),
-                     BUFFER * err)
-{
-  int dry_run = 0;
-
-  if (option (OPTNOCURSES) || has_colors ())
-    dry_run = 1;
+  if (object == MT_COLOR_NORMAL)
+    BKGDSET(main_w, MT_COLOR_NORMAL);
 
-  return _mutt_parse_color (buff, s, err, parse_attr_spec, dry_run);
+  return r;
 }