bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [patch] selected/context colors


From: Charles Levert
Subject: Re: [patch] selected/context colors
Date: Mon, 14 Nov 2005 21:54:25 -0500
User-agent: Mutt/1.4.1i

* On Monday 2005-11-14 at 20:40:17 +0000, Julian Foad wrote:
> 
> Yes, that is what I want.  Just to be doubly sure, I'll state it another 
> way: the colour given by "sl" is used (with -v and without -v) for the 
> lines that use ":" as a separator (or would use it if prefixes were 
> present), and "cx" for the lines that use (or would use) "-".

Here is a new version of the patch.

I have attached three screen shots to show
various combinations of GREP_COLORS capabilities
and command-line options.  Sorry for the size;
I hope the message doesn't get quarantined by
the mailing list software because of its size.

The line colors in these examples set both the
foreground and the background, but the match
colors set only the _background_ this time.

Please check that all colors as well as the various
separators are ok.

There is the matter of the "01;31" (instead of
"1;31") that is mildly bugging me, but I left
it as is.  To change it, tests/foad1.sh would
have to be minimally modified as well.



Index: grep/ChangeLog
===================================================================
RCS file: /cvsroot/grep/grep/ChangeLog,v
retrieving revision 1.284
diff -u -r1.284 ChangeLog
--- grep/ChangeLog      13 Nov 2005 08:15:16 -0000      1.284
+++ grep/ChangeLog      15 Nov 2005 02:44:02 -0000
@@ -1,3 +1,33 @@
+2005-11-15  Charles Levert  <address@hidden>
+
+       * src/grep.c: Rename/add global variables, macros, and capabilities:
+           SEP_CHAR_MATCH    -->  SEP_CHAR_SELECTED
+           SEP_CHAR_CONTEXT  -->  SEP_CHAR_REJECTED
+           SEP_STR_CHUNK     -->  SEP_STR_GROUP
+           grep_color        -->  selected_match_color + context_match_color
+           mlines_color      -->  selected_line_color
+           context_color     -->  context_line_color
+           "ml"              -->  "sl"
+           "mt"              -->  "mt" = "ms" + "mc"
+                             -->  "rv" (reverse "sl"/"cx" when -v)
+       * src/grep.c (color_cap_mt_fct, color_cap_rv_fct): New functions.
+       * src/grep.c (print_line_tail): Renamed color argument to line_color.
+       * src/grep.c (print_line_middle, prline): Revert part of the logic to
+         a pre-2005-06-21 one so that lines with matches have their matched
+         parts properly handled again ("m?" colors or --only-matching),
+         whether -v (or not -v).  Whole line colors ("sl", "cx") follow a
+         selected / rejected(context) logic, as opposed to a matched /
+         non-matched one (unless "rv").  Matched text colors ("ms",
+         "mc") always follow a selected / rejected(context) logic,
+         regardless of "rv", because only matched lines use them anyway.
+         pr_line_middle() now takes additional line_color and match_color
+         arguments computed by prline() prior to calling that function.
+         The old logic was a buggy hybrid matched / rejected(context) one.
+       * src/grep.c (prpending, prtext, grepfile): Renamed macro invocations.
+       * src/grep.c (parse_grep_colors): Update top comment.
+       * src/grep.c (main): GREP_COLOR (singular) now sets both
+         selected_match_color and context_match_color.
+
 2005-11-13  Charles Levert  <address@hidden>
 
        * tests/yesno.sh: New file.  Test feature interaction
Index: grep/src/grep.c
===================================================================
RCS file: /cvsroot/grep/grep/src/grep.c,v
retrieving revision 1.118
diff -p -u -r1.118 grep.c
--- grep/src/grep.c     11 Nov 2005 12:17:52 -0000      1.118
+++ grep/src/grep.c     15 Nov 2005 02:44:04 -0000
@@ -51,9 +51,9 @@
 #undef MAX
 #define MAX(A,B) ((A) > (B) ? (A) : (B))
 
-#define SEP_CHAR_MATCH   ':'
-#define SEP_CHAR_CONTEXT '-'
-#define SEP_STR_CHUNK    "--"
+#define SEP_CHAR_SELECTED ':'
+#define SEP_CHAR_REJECTED '-'
+#define SEP_STR_GROUP    "--"
 
 struct stats
 {
@@ -76,7 +76,7 @@ static int suppress_errors;
 /* If nonzero, use mmap if possible.  */
 static int mmap_option;
 
-/* If nonzero, use grep_color marker.  */
+/* If nonzero, use color markers.  */
 static int color_option;
 
 /* If nonzero, show only the part of a line matching the expression. */
@@ -127,18 +127,19 @@ static int align_tabs;
         terminal with either a black (dark) or white (light) background.
         This only leaves red, magenta, green, and cyan (and their bold
         counterparts) and possibly bold blue.  */
-/* The color string used for matched text.
-   The user can overwrite it using the deprecated
+/* The color strings used for matched text.
+   The user can overwrite them using the deprecated
    environment variable GREP_COLOR or the new GREP_COLORS.  */
-static const char *grep_color = "01;31";       /* bold red */
+static const char *selected_match_color = "01;31";     /* bold red */
+static const char *context_match_color  = "01;31";     /* bold red */
 
 /* Other colors.  Defaults look damn good.  */
 static const char *filename_color = "35";      /* magenta */
 static const char *line_num_color = "32";      /* green */
 static const char *byte_num_color = "32";      /* green */
 static const char *sep_color      = "36";      /* cyan */
-static const char *mlines_color   = "";                /* default color pair */
-static const char *context_color  = "";                /* default color pair */
+static const char *selected_line_color = "";   /* default color pair */
+static const char *context_line_color  = "";   /* default color pair */
 
 /* Select Graphic Rendition (SGR, "\33[...m") strings.  */
 /* Also Erase in Line (EL) to Right ("\33[K") by default.  */
@@ -219,6 +220,24 @@ struct color_cap
   };
 
 static const char *
+color_cap_mt_fct(void)
+{
+  /* Our caller just set selected_match_color.  */
+  context_match_color = selected_match_color;
+
+  return NULL;
+}
+
+static const char *
+color_cap_rv_fct(void)
+{
+  /* By this point, it was 1 (or already -1).  */
+  color_option = -1;  /* That's still != 0.  */
+
+  return NULL;
+}
+
+static const char *
 color_cap_ne_fct(void)
 {
   sgr_start = "\33[%sm";
@@ -230,15 +249,18 @@ color_cap_ne_fct(void)
 /* For GREP_COLORS.  */
 static struct color_cap color_dict[] =
   {
-    { "mt", &grep_color,       NULL },                 /* matched text */
-    { "fn", &filename_color,   NULL },                 /* filename */
-    { "ln", &line_num_color,   NULL },                 /* line number */
-    { "bn", &byte_num_color,   NULL },                 /* byte (sic) offset */
-    { "se", &sep_color,                NULL },                 /* separator */
-    { "ml", &mlines_color,     NULL },                 /* matching lines */
-    { "cx", &context_color,    NULL },                 /* context lines */
-    { "ne", NULL,              color_cap_ne_fct },     /* no EL on SGR_* */
-    { NULL, NULL,              NULL }
+    { "mt", &selected_match_color, color_cap_mt_fct }, /* both ms/mc */
+    { "ms", &selected_match_color, NULL }, /* selected matched text */
+    { "mc", &context_match_color,  NULL }, /* context matched text */
+    { "fn", &filename_color,       NULL }, /* filename */
+    { "ln", &line_num_color,       NULL }, /* line number */
+    { "bn", &byte_num_color,       NULL }, /* byte (sic) offset */
+    { "se", &sep_color,            NULL }, /* separator */
+    { "sl", &selected_line_color,  NULL }, /* selected lines */
+    { "cx", &context_line_color,   NULL }, /* context lines */
+    { "rv", NULL,                  color_cap_rv_fct }, /* -v reverses sl/cx */
+    { "ne", NULL,                  color_cap_ne_fct }, /* no EL on SGR_* */
+    { NULL, NULL,                  NULL }
   };
 
 static struct exclude *excluded_patterns;
@@ -757,7 +779,8 @@ print_line_head (char const *beg, char c
 }
 
 static const char *
-print_line_middle (const char *beg, const char *lim)
+print_line_middle (const char *beg, const char *lim,
+                  const char *line_color, const char *match_color)
 {
   size_t match_size;
   size_t match_offset;
@@ -803,11 +826,14 @@ print_line_middle (const char *beg, cons
        }
       else
        {
+         /* This function is called on a matching line only,
+            but is it selected or rejected/context?  */
          if (only_matching)
-           print_line_head(b, lim, SEP_CHAR_MATCH);
+           print_line_head(b, lim, out_invert ? SEP_CHAR_REJECTED
+                                              : SEP_CHAR_SELECTED);
          else
            {
-             PR_SGR_START(mlines_color);
+             PR_SGR_START(line_color);
              if (mid)
                {
                  cur = mid;
@@ -816,9 +842,9 @@ print_line_middle (const char *beg, cons
              fwrite (cur, sizeof (char), b - cur, stdout);
            }
 
-         PR_SGR_START_IF(grep_color);
+         PR_SGR_START_IF(match_color);
          fwrite (b, sizeof (char), match_size, stdout);
-         PR_SGR_END_IF(grep_color);
+         PR_SGR_END_IF(match_color);
          if (only_matching)
            fputs("\n", stdout);
        }
@@ -837,7 +863,7 @@ print_line_middle (const char *beg, cons
 }
 
 static const char *
-print_line_tail (const char *beg, const char *lim, const char *color)
+print_line_tail (const char *beg, const char *lim, const char *line_color)
 {
   size_t  eol_size;
   size_t tail_size;
@@ -848,10 +874,10 @@ print_line_tail (const char *beg, const 
 
   if (tail_size > 0)
     {
-      PR_SGR_START(color);
+      PR_SGR_START(line_color);
       fwrite(beg, 1, tail_size, stdout);
       beg += tail_size;
-      PR_SGR_END(color);
+      PR_SGR_END(line_color);
     }
 
   return beg;
@@ -860,19 +886,32 @@ print_line_tail (const char *beg, const 
 static void
 prline (char const *beg, char const *lim, int sep)
 {
+  int matching;
   const char *line_color;
+  const char *match_color;
 
   if (!only_matching)
     print_line_head(beg, lim, sep);
 
+  matching = (sep == SEP_CHAR_SELECTED) ^ !!out_invert;
+
   if (color_option)
-    line_color = ((sep == SEP_CHAR_MATCH) ? mlines_color : context_color);
+    {
+      line_color  = (  (sep == SEP_CHAR_SELECTED)
+                    ^ (out_invert && (color_option < 0)))
+                 ? selected_line_color  : context_line_color;
+      match_color = (sep == SEP_CHAR_SELECTED)
+                 ? selected_match_color : context_match_color;
+    }
+  else
+    line_color = match_color = NULL; /* Shouldn't be used.  */
 
-  if (only_matching || (color_option && (*grep_color || *line_color)))
+  if (   (only_matching && matching)
+      || (color_option  && (*line_color || *match_color)))
     {
-      /* We already know that context lines have no match (to colorize).  */
-      if (only_matching || (*grep_color && sep == SEP_CHAR_MATCH))
-       beg = print_line_middle(beg, lim);
+      /* We already know that non-matching lines have no match (to colorize).  
*/
+      if (matching && (only_matching || *match_color))
+       beg = print_line_middle(beg, lim, line_color, match_color);
 
       if (!only_matching && *line_color);
        beg = print_line_tail(beg, lim, line_color);
@@ -906,7 +945,7 @@ prpending (char const *lim)
          || ((execute(lastout, nl + 1 - lastout,
                       &match_size, NULL) == (size_t) -1)
              == !out_invert))
-       prline (lastout, nl + 1, SEP_CHAR_CONTEXT);
+       prline (lastout, nl + 1, SEP_CHAR_REJECTED);
       else
        pending = 0;
     }
@@ -917,7 +956,7 @@ prpending (char const *lim)
 static void
 prtext (char const *beg, char const *lim, int *nlinesp)
 {
-  static int used;     /* avoid printing SEP_STR_CHUNK before any output */
+  static int used;     /* avoid printing SEP_STR_GROUP before any output */
   char const *bp, *p;
   char eol = eolbyte;
   int i, n;
@@ -938,12 +977,12 @@ prtext (char const *beg, char const *lim
            --p;
          while (p[-1] != eol);
 
-      /* We print the SEP_STR_CHUNK separator only if our output is
+      /* We print the SEP_STR_GROUP separator only if our output is
         discontiguous from the last output in the file. */
       if ((out_before || out_after) && used && p != lastout)
        {
          PR_SGR_START_IF(sep_color);
-         fputs (SEP_STR_CHUNK, stdout);
+         fputs (SEP_STR_GROUP, stdout);
          PR_SGR_END_IF(sep_color);
          fputc('\n', stdout);
        }
@@ -952,7 +991,7 @@ prtext (char const *beg, char const *lim
        {
          char const *nl = memchr (p, eol, beg - p);
          nl++;
-         prline (p, nl, SEP_CHAR_CONTEXT);
+         prline (p, nl, SEP_CHAR_REJECTED);
          p = nl;
        }
     }
@@ -965,7 +1004,7 @@ prtext (char const *beg, char const *lim
          char const *nl = memchr (p, eol, lim - p);
          nl++;
          if (!out_quiet)
-           prline (p, nl, SEP_CHAR_MATCH);
+           prline (p, nl, SEP_CHAR_SELECTED);
          p = nl;
        }
       *nlinesp = n;
@@ -975,7 +1014,7 @@ prtext (char const *beg, char const *lim
     }
   else
     if (!out_quiet)
-      prline (beg, lim, SEP_CHAR_MATCH);
+      prline (beg, lim, SEP_CHAR_SELECTED);
 
   pending = out_quiet ? 0 : out_after;
   used = 1;
@@ -1256,7 +1295,7 @@ grepfile (char const *file, struct stats
            {
              print_filename();
              if (filename_mask)
-               print_sep(SEP_CHAR_MATCH);
+               print_sep(SEP_CHAR_SELECTED);
              else
                fputc(0, stdout);
            }
@@ -1620,8 +1659,9 @@ get_nondigit_option (int argc, char *con
   return opt;
 }
 
-/* Parse GREP_COLORS.  The  default would look like:
-     GREP_COLORS='mt=01;31:ml=:cx=:fn=35:ln=32:bn=32:se=36'
+/* Parse GREP_COLORS.  The default would look like:
+     GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
+   with boolean capabilities (ne and rv) unset (i.e., omitted).
    No character escaping is needed or supported.  */
 static void
 parse_grep_colors (void)
@@ -2120,7 +2160,7 @@ main (int argc, char **argv)
       /* Legacy.  */
       char *userval = getenv ("GREP_COLOR");
       if (userval != NULL && *userval != '\0')
-       grep_color = userval;
+       selected_match_color = context_match_color = userval;
 
       /* New GREP_COLORS has priority.  */
       parse_grep_colors();

Attachment: grep-colors1.png
Description: PNG image

Attachment: grep-colors2.png
Description: PNG image

Attachment: grep-colors3.png
Description: PNG image


reply via email to

[Prev in Thread] Current Thread [Next in Thread]