bug-grep
[Top][All Lists]
Advanced

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

[patch] selected/context colors


From: Charles Levert
Subject: [patch] selected/context colors
Date: Sun, 13 Nov 2005 23:31:25 -0500
User-agent: Mutt/1.4.1i

Here is the selected/context colors patch, as discussed.
Nothing else that was discussed is touched by this.

I have attached a screen shot to be sure this
is what everybody understands and wants.

   Selected lines are bright yellow (93).
   Context lines are dark gray (90).
   Matched text parts are bright red (by default).

The other design choice that was available would
not have inverted the yellow and the gray with
-v, everything else staying the same.



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      14 Nov 2005 04:20:30 -0000
@@ -1,3 +1,18 @@
+2005-11-14  Charles Levert  <address@hidden>
+
+       * src/grep.c: Rename global variables, macros, and capabilities:
+           SEP_CHAR_MATCH    -->  SEP_CHAR_SELECTED
+           SEP_CHAR_CONTEXT  -->  SEP_CHAR_REJECTED
+           SEP_STR_CHUNK     -->  SEP_STR_GROUP
+           mlines_color      -->  selected_color
+           "ml"              -->  "sl"
+       * src/grep.c (print_line_middle, prline): Change logic so that
+         lines with matches have their matched part properly handled ("mt"
+         color 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.  The replaced logic
+         was a buggy hybrid matched / rejected(context) one.
+
 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     14 Nov 2005 04:20:42 -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
 {
@@ -137,7 +137,7 @@ static const char *filename_color = "35"
 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 *selected_color = "";                /* default color pair */
 static const char *context_color  = "";                /* default color pair */
 
 /* Select Graphic Rendition (SGR, "\33[...m") strings.  */
@@ -235,7 +235,7 @@ static struct color_cap color_dict[] =
     { "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 */
+    { "sl", &selected_color,   NULL },                 /* selected lines */
     { "cx", &context_color,    NULL },                 /* context lines */
     { "ne", NULL,              color_cap_ne_fct },     /* no EL on SGR_* */
     { NULL, NULL,              NULL }
@@ -803,11 +803,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(out_invert ? context_color : selected_color);
              if (mid)
                {
                  cur = mid;
@@ -860,18 +863,22 @@ print_line_tail (const char *beg, const 
 static void
 prline (char const *beg, char const *lim, int sep)
 {
+  int matching;
   const char *line_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) ? selected_color : context_color;
 
-  if (only_matching || (color_option && (*grep_color || *line_color)))
+  if (   (only_matching && matching)
+      || (color_option  && (*grep_color || *line_color)))
     {
-      /* We already know that context lines have no match (to colorize).  */
-      if (only_matching || (*grep_color && sep == SEP_CHAR_MATCH))
+      /* We already know that non-matching lines have no match (to colorize).  
*/
+      if (matching && (only_matching || *grep_color))
        beg = print_line_middle(beg, lim);
 
       if (!only_matching && *line_color);
@@ -906,7 +913,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 +924,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 +945,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 +959,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 +972,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 +982,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 +1263,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);
            }
@@ -1621,7 +1628,7 @@ get_nondigit_option (int argc, char *con
 }
 
 /* Parse GREP_COLORS.  The  default would look like:
-     GREP_COLORS='mt=01;31:ml=:cx=:fn=35:ln=32:bn=32:se=36'
+     GREP_COLORS='mt=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
    No character escaping is needed or supported.  */
 static void
 parse_grep_colors (void)

Attachment: grep-colors-sl-cx.png
Description: PNG image


reply via email to

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