From ec58a99326a6f10097446c3e68cd20ad5406c620 Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Sun, 5 Nov 2000 21:54:23 +0000
Subject: [PATCH] input_tab: Now autocomplete up until the matches diverge by
 nanme works, yay

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@268 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 files.c     | 61 +++++++++++++++++++++++++++++++++++++++++++++++------
 po/nano.pot |  2 +-
 2 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/files.c b/files.c
index c6279d70..86c075b3 100644
--- a/files.c
+++ b/files.c
@@ -604,9 +604,10 @@ char **cwd_tab_completion(char *buf, int *num_matches)
 int input_tab(char *buf, int place, int lastWasTab)
 {
     /* Do TAB completion */
-    static int num_matches = 0;
+    static int num_matches = 0, match_matches = 0;
     static char **matches = (char **) NULL;
-    int pos = place, newplace = 0;
+    int pos = place, newplace = 0, i = 0, col = 0, editline = 0;
+    int longestname = 0;
 
     if (lastWasTab == FALSE) {
 	char *tmp, *matchBuf;
@@ -656,24 +657,70 @@ int input_tab(char *buf, int place, int lastWasTab)
 		    strlen(matches[0]) - pos);
 	    newplace += strlen(matches[0]) - pos;
 	}
+	else {
+	    /* Check to see if all matches share a beginning, and if so
+		tack it onto buf and then beep */
+
+	    while (1) {
+		match_matches = 0;
+
+		for (i = 0; i < num_matches; i++) {
+		    if (matches[i][pos] == 0)
+			break;
+		    else if (matches[i][pos] == matches[0][pos])
+			match_matches++;
+		}
+		if (match_matches == num_matches && 
+			(i == num_matches || matches[i][pos] != 0)) {
+
+		    /* All the matches have the same character at pos+1,
+			so paste it into buf... */
+		    strncpy(buf + pos, matches[0] + pos, 1);
+	 	    newplace++;
+		    pos++;
+		}
+		else {
+		    beep();
+		    break;
+		}
+	    }
+	}
     } else {
 	/* Ok -- the last char was a TAB.  Since they
 	 * just hit TAB again, print a list of all the
 	 * available choices... */
 	if (matches && num_matches > 0) {
-	    int i, col;
 
 	    /* Blank the edit window, and print the matches out there */
 	    blank_edit();
 	    wmove(edit, 0, 0);
 
+	    editline = 0;
+	    /* Figure out the length of the longest filename */
+	    for (i = 0; i < num_matches; i++)
+		if (strlen(matches[i]) > longestname)
+		    longestname = strlen(matches[i]);
+
+	    if (longestname > COLS - 1)
+		longestname = COLS - 1;
+
 	    /* Print the list of matches */
 	    for (i = 0, col = 0; i < num_matches; i++) {
-		char foo[17];
-		sprintf(foo, "%-14s  ", matches[i]);
+		
+		/* make each filename shown be the same length as the longest
+			filename, with two spaces at the end */
+		char *foo;
+		foo = nmalloc(longestname + 5);
+		snprintf(foo, longestname + 1, matches[i]);
+		while (strlen(foo) < longestname)
+		    strcat(foo, " ");
+
+		strcat(foo, "  ");
+
 		col += waddnstr(edit, foo, strlen(foo));
-		if (col > 60 && matches[i + 1] != NULL) {
-		    waddstr(edit, "\n");
+		if (col > (COLS * .8)  && matches[i + 1] != NULL) {
+		    editline++;
+		    wmove(edit, editline, 0);
 		    col = 0;
 		}
 	    }
diff --git a/po/nano.pot b/po/nano.pot
index 95853cbd..86772dad 100644
--- a/po/nano.pot
+++ b/po/nano.pot
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
-"POT-Creation-Date: 2000-11-05 12:08-0500\n"
+"POT-Creation-Date: 2000-11-05 16:52-0500\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
-- 
GitLab