[PATCH 1/2] screen: Introduce sort command which sorts windows according to their title

Thomas Renninger <[email protected]>
Newsgroups gmane.comp.gnu.screen.devel,gmane.comp.gnu.screen.user
Message-ID <[email protected]>
The new command:
:sort
will sort all active screen windows ordered by title.
Limitation:
  - Will only work in overview window (CTRL-a-")

Still, this command is very helpful for users who use
screen sessions with a huge amount of active windows.

Signed-off-by: Thomas Renninger <[email protected]>
---
 src/comm.c       |    1 +
 src/doc/screen.1 |    5 +++++
 src/process.c    |   43 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/src/comm.c b/src/comm.c
index 5f4af8a..36b2270 100644
--- a/src/comm.c
+++ b/src/comm.c
@@ -297,6 +297,7 @@ struct comm comms[RC_LAST + 1] =
   { "sleep",		ARGS_1 },
   { "slowpaste",	NEED_FORE|ARGS_01 },
   { "sorendition",      ARGS_012 },
+  { "sort",		ARGS_0 },
   { "source",		ARGS_1 },
   { "split",		NEED_DISPLAY|ARGS_01 },
   { "startup_message",	ARGS_1 },
diff --git a/src/doc/screen.1 b/src/doc/screen.1
index 98674eb..f27e84c 100644
--- a/src/doc/screen.1
+++ b/src/doc/screen.1
@@ -3012,6 +3012,11 @@ underlying system exposes flow control problems while pasting large amounts of
 text. 
 .sp
 .ne 3
+.B sort
+.PP
+Sort the windows in alphabetical order of the window tiles.
+.sp
+.ne 3
 .BI "source " file
 .PP
 Read and execute commands from file \fIfile\fP. Source commands may
diff --git a/src/process.c b/src/process.c
index 30497a3..783ada5 100644
--- a/src/process.c
+++ b/src/process.c
@@ -3024,6 +3024,49 @@ int key;
 	    }
 	}
       break;
+    case RC_SORT:
+      if (fore)
+        {
+	  /* Better do not allow this. Not sure what the utmp stuff in number
+	     command above is for (you get four entries in e.g. /var/log/wtmp
+	     per number switch). But I don't know enough about this.*/
+	  Msg(0, "Sorting inside a window is not allowed. Push CTRL-a \" "
+	      "and try again\n");
+	  break;
+	}
+      /*
+       * Simple sort algorithm: Look out for the smallest, put it
+       * to the first place, look out for the 2nd smallest, ...
+      */
+      for (i = 0; i < maxwin ; i++)
+        {
+	  if (wtab[i] == NULL)
+	      continue;
+	  n = i;
+	  for (nr = i + 1; nr < maxwin; nr++)
+	    {
+	      if (wtab[nr] == NULL)
+		  continue;
+	      debug2("Testing window %d and %d.\n", nr, n);
+	      if (strcmp(wtab[nr]->w_title,wtab[n]->w_title) < 0)
+		    n = nr;
+	    }
+	  if (n != i)
+	    {
+		debug2("Exchange window %d and %d.\n", i, n);
+		p = wtab[n];
+		wtab[n] = wtab[i];
+		wtab[i] = p;
+		wtab[n]->w_number = n;
+		wtab[i]->w_number = i;
+#ifdef MULTIUSER
+		/* exchange the acls for these windows. */
+		AclWinSwap(i, n);
+#endif
+	    }
+	}
+      WindowChanged((struct win *)0, 0);
+      break;
     case RC_SILENCE:
       n = fore->w_silence != 0;
       i = fore->w_silencewait;
-- 
1.7.6.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.