From 3fc89e085393074eb78a7265b5d11ad6898a4f88 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?=
 <marcodiegomesquita@gmail.com>
Date: Fri, 1 Sep 2017 13:47:19 -0300
Subject: [PATCH] new feature: show current and total number of open buffers in
 title bar
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When multiple buffers are open, replace nano's name and version number
with an indication how many buffers are open preceded by the sequence
number of the current buffer.

Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
---
 src/files.c  |  5 ++++-
 src/global.c |  2 ++
 src/nano.c   |  3 +++
 src/proto.h  |  1 +
 src/winio.c  | 25 +++++++++++++++++++++++++
 5 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/src/files.c b/src/files.c
index 6a6bb1ca..bf8d8f05 100644
--- a/src/files.c
+++ b/src/files.c
@@ -68,6 +68,7 @@ void make_new_buffer(void)
 	/* Make the first open file the only element in the list. */
 	newnode->prev = newnode;
 	newnode->next = newnode;
+	firstfile = newnode;
     } else {
 	/* Add the new open file after the current one in the list. */
 	newnode->prev = openfile;
@@ -669,8 +670,10 @@ bool close_buffer(void)
     /* Switch to the next file buffer. */
     switch_to_adjacent_buffer(TRUE);
 
-    /* Close the file buffer we had open before. */
+    /* Delete the old file buffer, and adjust the count in the top bar. */
     unlink_opennode(openfile->prev);
+    if (!inhelp)
+	titlebar(NULL);
 
     /* If now just one buffer remains open, show "Exit" in the help lines. */
     if (openfile == openfile->next)
diff --git a/src/global.c b/src/global.c
index e2017115..328d20f2 100644
--- a/src/global.c
+++ b/src/global.c
@@ -119,6 +119,8 @@ partition *filepart = NULL;
 	/* The "partition" where we store a portion of the current file. */
 openfilestruct *openfile = NULL;
 	/* The list of all open file buffers. */
+openfilestruct *firstfile = NULL;
+	/* The first open buffer. */
 
 #ifndef NANO_TINY
 char *matchbrackets = NULL;
diff --git a/src/nano.c b/src/nano.c
index f29cea90..e706c3c1 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -507,6 +507,9 @@ void unlink_opennode(openfilestruct *fileptr)
 {
     assert(fileptr != fileptr->prev && fileptr != fileptr->next);
 
+    if (fileptr == firstfile)
+	firstfile = firstfile->next;
+
     fileptr->prev->next = fileptr->next;
     fileptr->next->prev = fileptr->prev;
 
diff --git a/src/proto.h b/src/proto.h
index 8a3043ce..13cc5369 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -100,6 +100,7 @@ extern filestruct *cutbuffer;
 extern filestruct *cutbottom;
 extern partition *filepart;
 extern openfilestruct *openfile;
+extern openfilestruct *firstfile;
 
 #ifndef NANO_TINY
 extern char *matchbrackets;
diff --git a/src/winio.c b/src/winio.c
index 0a27f8d6..ebc37008 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -1977,6 +1977,19 @@ char *display_string(const char *buf, size_t column, size_t span, bool isdata)
     return converted;
 }
 
+/* Determine the sequence number of the given buffer in the circular list. */
+int buffer_number(openfilestruct *buffer)
+{
+    int count = 1;
+
+    while (buffer != firstfile) {
+	buffer = buffer->prev;
+	count++;
+    }
+
+    return count;
+}
+
 /* If path is NULL, we're in normal editing mode, so display the current
  * version of nano, the current filename, and whether the current file
  * has been modified on the titlebar.  If path isn't NULL, we're either
@@ -1998,6 +2011,8 @@ void titlebar(const char *path)
 	/* The state of the current buffer -- "Modified", "View", or "". */
     char *caption;
 	/* The presentable form of the pathname. */
+    char *indicator = NULL;
+	/* The buffer sequence number plus buffer count. */
 
     /* If the screen is too small, there is no titlebar. */
     if (topwin == NULL)
@@ -2014,6 +2029,14 @@ void titlebar(const char *path)
      * first sacrifice the version string, then eat up the side spaces,
      * then sacrifice the prefix, and only then start dottifying. */
 
+    /* When multiple buffers are open, show which one out of how many. */
+    if (path == NULL && firstfile != firstfile->next) {
+	indicator = charalloc(24);
+	sprintf(indicator, "[%i/%i]", buffer_number(openfile),
+					buffer_number(firstfile->prev));
+	branding = indicator;
+    }
+
     /* Figure out the path, prefix and state strings. */
     if (inhelp)
 	branding = "";
@@ -2064,6 +2087,8 @@ void titlebar(const char *path)
 	}
     }
 
+    free(indicator);
+
     /* If we have side spaces left, center the path name. */
     if (verlen > 0)
 	offset = verlen + (COLS - (verlen + pluglen + statelen) -
-- 
GitLab