Some enhanced string escapes for screen

Ivan Richwalski <[email protected]> Sun, 12 Feb 2006 01:17:26 -0600
Newsgroups gmane.comp.gnu.screen
Message-ID <43EEE106.3070900__5358.17324785075$1238088938$gmane$org@seppuku.net>
I have a patch for screen that I've been using locally for a while now, 
and would like to contribute back.  The main feature is the being able 
to use numeric qualifiers on the 'w' and 'W' string escapes to specify 
the format of the window number.  So a string escape of "%03w" will 
output "001 title  002 title".  The patch also allows using leading zero 
for %n.  Finally, 2 new string escapes, 'z' and 'Z', which function like 
'w', but display only the window number with no title.

Ivan.
winnum-patch.txt (text/plain, 4.6 KB)
diff -ur screen-4.0.2.orig/doc/screen.info-4 screen-4.0.2/doc/screen.info-4
--- screen-4.0.2.orig/doc/screen.info-4	2003-12-05 07:52:07.000000000 -0600
+++ screen-4.0.2/doc/screen.info-4	2006-02-11 21:19:53.379194000 -0600
@@ -1206,6 +1206,14 @@
 `W'
      all window numbers and names except the current one
 
+`z'
+     all window numbers. With `-' quailifier: up to the
+     current window; with `+' qualifier: starting with the window after
+     the current one.
+
+`Z'
+     all window numbers except the current one
+
 `y'
      last two digits of the year number
 
diff -ur screen-4.0.2.orig/extern.h screen-4.0.2/extern.h
--- screen-4.0.2.orig/extern.h	2003-08-22 07:27:57.000000000 -0500
+++ screen-4.0.2/extern.h	2006-02-11 19:35:51.529586000 -0600
@@ -195,7 +195,8 @@
 extern void  DoScreen __P((char *, char **));
 extern int   IsNumColon __P((char *, int, char *, int));
 extern void  ShowWindows __P((int));
-extern char *AddWindows __P((char *, int, int, int));
+extern char *AddWindows __P((char *, int, int, int, int));
+extern char *AddNumbers __P((char *, int, int, int, int));
 extern char *AddWindowFlags __P((char *, int, struct win *));
 extern char *AddOtherUsers __P((char *, int, struct win *));
 extern int   WindowByNoN __P((char *));
diff -ur screen-4.0.2.orig/process.c screen-4.0.2/process.c
--- screen-4.0.2.orig/process.c	2003-09-18 07:53:54.000000000 -0500
+++ screen-4.0.2/process.c	2006-02-11 19:35:51.559557000 -0600
@@ -4736,11 +4736,12 @@
 }
 
 char *
-AddWindows(buf, len, flags, where)
+AddWindows(buf, len, flags, where, num)
 char *buf;
 int len;
 int flags;
 int where;
+int num;
 {
   register char *s, *ss;
   register struct win **pp, *p;
@@ -4768,7 +4769,10 @@
 	  *s++ = ' ';
 	  *s++ = ' ';
 	}
-      sprintf(s, "%d", p->w_number);
+      if ( num )
+        sprintf(s, (flags & 8) ? "%0*d" : "%*d", num, p->w_number);
+      else
+        sprintf(s, "%d", p->w_number );
       if (p->w_number == where)
         ss = s;
       s += strlen(s);
@@ -4789,6 +4793,52 @@
 }
 
 char *
+AddNumbers(buf, len, flags, where, num)
+char *buf;
+int len;
+int flags;
+int where;
+int num;
+{
+  register char *s, *ss;
+  register struct win **pp, *p;
+
+  s = ss = buf;
+  for (pp = ((flags & 4) && where >= 0) ? wtab + where + 1: wtab; pp < wtab + MAXWIN; pp++)
+    {
+      if (pp - wtab == where && ss == buf)
+	ss = s;
+      if ((p = *pp) == 0)
+	continue;
+      if ((flags & 1) && display && p == D_fore)
+	continue;
+
+      if (s - buf > len - 24)
+	break;
+      if (s > buf || (flags & 4))
+	{
+	  *s++ = ' ';
+	  *s++ = ' ';
+	}
+      sprintf(s, (flags & 8) ? "%0*d" : "%*d", num, p->w_number);
+      if (p->w_number == where)
+        ss = s;
+      s += strlen(s);
+      if (display && p == D_fore)
+	*s++ = '*';
+      if (!(flags & 2))
+	{
+          if (display && p == D_other)
+	    *s++ = '-';
+          s = AddWindowFlags(s, len, p);
+	}
+      //*s++ = '+';
+    }
+  *s = 0;
+  return ss;
+}
+
+char *
 AddWindowFlags(buf, len, p)
 char *buf;
 int len;
@@ -4881,7 +4931,7 @@
     return;
   if (where == -1 && D_fore)
     where = D_fore->w_number;
-  ss = AddWindows(buf, sizeof(buf), 0, where);
+  ss = AddWindows(buf, sizeof(buf), 0, where, 0 );
   s = buf + strlen(buf);
   if (ss - buf > D_width / 2)
     {
diff -ur screen-4.0.2.orig/screen.c screen-4.0.2/screen.c
--- screen-4.0.2.orig/screen.c	2003-09-08 09:26:41.000000000 -0500
+++ screen-4.0.2/screen.c	2006-02-11 20:40:01.687856000 -0600
@@ -2528,7 +2528,7 @@
 		oldfore = D_fore;
 		D_fore = win;
 	      }
-	    ss = AddWindows(p, l - 1, (*s == 'w' ? 0 : 1) | (longflg ? 0 : 2) | (plusflg ? 4 : 0), win ? win->w_number : -1);
+	    ss = AddWindows(p, l - 1, (*s == 'w' ? 0 : 1) | (longflg ? 0 : 2) | (plusflg ? 4 : 0) | ( zeroflg ? 8 : 0 ), win ? win->w_number : -1, num);
 	    if (minusflg)
 	       *ss = 0;
 	    if (display)
@@ -2729,6 +2729,26 @@
 	      numpad++;
 	    }
 	  break;
+	case 'z':
+	case 'Z':
+	  {
+	    struct win *oldfore = 0;
+	    char *ss;
+	    if (display)
+	      {
+		oldfore = D_fore;
+		D_fore = win;
+	      }
+	    ss = AddNumbers(p, l - 1, (*s == 'z' ? 0 : 1) | (longflg ? 0 : 2) | (plusflg ? 4 : 0) | ( zeroflg ? 8 : 0 ), win ? win->w_number : -1, num);
+	    if (minusflg)
+	       *ss = 0;
+	    if (display)
+	      D_fore = oldfore;
+	  }
+	  if (*p)
+	    qmflag = 1;
+	  p += strlen(p) - 1;
+	  break;
 	case 'n':
 	  s++;
 	  /* FALLTHROUGH */
@@ -2741,7 +2761,7 @@
 	      if (!win)
 	        sprintf(p, "%*s", num, num > 1 ? "--" : "-");
 	      else
-	        sprintf(p, "%*d", num, win->w_number);
+	        sprintf(p, zeroflg ? "%0*d" : "%*d", num, win->w_number);
 	      qmflag = 1;
 	      p += strlen(p) - 1;
 	    }