[PATCH] FindOrMake

Johannes Weiner <[email protected]> Fri, 29 Jun 2007 21:36:39 +0200
Newsgroups gmane.comp.gnu.screen
Message-ID <[email protected]>
Hi,

this patch adds a -F argument to the screen command which makes it try to
lookup a window by title instead of creating a new one.

I have this in my .screenrc:

	bind ^w screen -F -t www elinks

Now, when I need a browser I hit C-o C-w to get one. If there is no window
called www yet, it is created and elinks is spawned in there as usual.
If it already exists, screen simply hops to it instead of creating a second
one. Perfect for impatient people like me who like to behave like babies,
yelling 'gimme this, now, no matter how' and then end up having 10 browser
windows open because of not keeping track of them.

	Hannes
screen-4.0.3jw4-find-or-make-window-1.patch (text/x-diff, 2.1 KB)
diff -Naur screen.old/src/extern.h screen/src/extern.h
--- screen.old/src/extern.h	2007-05-29 19:59:51.000000000 +0200
+++ screen/src/extern.h	2007-05-29 20:00:44.000000000 +0200
@@ -132,6 +132,7 @@
 
 /* window.c */
 extern int   MakeWindow __P((struct NewWindow *));
+extern int   FindOrMakeWindow __P((struct NewWindow *));
 extern int   RemakeWindow __P((struct win *));
 extern void  FreeWindow __P((struct win *));
 #ifdef PSEUDOS
diff -Naur screen.old/src/process.c screen/src/process.c
--- screen.old/src/process.c	2007-05-29 19:59:51.000000000 +0200
+++ screen/src/process.c	2007-05-29 20:00:04.000000000 +0200
@@ -5569,6 +5569,7 @@
 DoScreen(fn, av)
 char *fn, **av;
 {
+  int find = 0;
   struct NewWindow nwin;
   register int num;
   char buf[20];
@@ -5602,6 +5603,9 @@
 	      break;
 	    }
 	  break;
+	case 'F':
+	  find = 1;
+	  break;
 	case 't':	/* no more -k */
 	  if (av[0][2])
 	    nwin.aka = &av[0][2];
@@ -5682,7 +5686,10 @@
       if (!nwin.aka)
         nwin.aka = Filename(*av);
     }
-  MakeWindow(&nwin);
+  if (find)
+    FindOrMakeWindow(&nwin);
+  else
+    MakeWindow(&nwin);
 }
 
 #ifdef COPY_PASTE
diff -Naur screen.old/src/window.c screen/src/window.c
--- screen.old/src/window.c	2007-05-29 19:59:51.000000000 +0200
+++ screen/src/window.c	2007-05-29 20:00:04.000000000 +0200
@@ -846,6 +846,40 @@
 }
 
 /*
+ * Look up a window via its title. If it's found, it is just fetched to the
+ * foreground. If not, it gets created with the given flags.
+ */
+int
+FindOrMakeWindow(nwin)
+struct NewWindow *nwin;
+{
+  struct win *r = NULL, *p = windows;
+
+  if (!nwin->aka)
+    {
+      Msg(0, "Window lookup impossible without title, creating new.");
+      return MakeWindow(nwin);
+    }
+
+  while (p)
+    {
+      if (strcmp(nwin->aka, p->w_title) == 0)
+	{
+	  r = p;
+	  break;
+	}
+      p = p->w_next;
+    }
+
+  if (r)
+    SwitchWindow(r->w_number);
+  else
+    return MakeWindow(nwin);
+
+  return 0;
+}
+
+/*
  * Resurrect a window from Zombie state.
  * The command vector is therefore stored in the window structure.
  * Note: The terminaltype defaults to screenterm again, the current