From 442f2c5156ae81dca1c6a8b15e5381db39197943 Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Tue, 14 Nov 2000 17:46:06 +0000
Subject: [PATCH] Latest tab completion update, sort of works =)

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@281 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 files.c     | 93 +++++++++++++++++++++++++++++++++++++++++++++--------
 po/nano.pot | 54 +++++++++++++++----------------
 proto.h     |  2 +-
 winio.c     |  7 ++--
 4 files changed, 113 insertions(+), 43 deletions(-)

diff --git a/files.c b/files.c
index 46401198..9afed011 100644
--- a/files.c
+++ b/files.c
@@ -551,10 +551,13 @@ char **cwd_tab_completion(char *buf, int *num_matches)
 	tmp = buf + strlen(buf);
 	while (*tmp != '/' && tmp != buf)
 	   tmp--;
-	strncpy(dirName, buf, tmp - buf);
-	dirName[tmp - buf] = 0;
+
 	tmp++;
 
+	strncpy(dirName, buf, tmp - buf + 1);
+	dirName[tmp - buf] = 0;
+/*	tmp++; */
+
     } else {
 	if ((dirName = getcwd(NULL, 0)) == NULL)
 	    return matches;
@@ -602,20 +605,21 @@ char **cwd_tab_completion(char *buf, int *num_matches)
     return (matches);
 }
 
-/* This function now return an int which refers to how much the 
+/* This function now has an arg which refers to how much the 
  * statusbar (place) should be advanced, i.e. the new cursor pos.
  */
-int input_tab(char *buf, int place, int lastWasTab)
+char *input_tab(char *buf, int place, int lastWasTab, int *newplace)
 {
     /* Do TAB completion */
     static int num_matches = 0, match_matches = 0;
     static char **matches = (char **) NULL;
-    int pos = place, newplace = 0, i = 0, col = 0, editline = 0;
+    int pos = place, i = 0, col = 0, editline = 0;
     int longestname = 0;
     char *foo;
+    struct stat fileinfo;
 
     if (lastWasTab == FALSE) {
-	char *tmp, *matchBuf;
+	char *tmp, *copyto, *matchBuf;
 
 	/* For now, we will not bother with trying to distinguish
 	 * whether the cursor is in/at a command extression -- we
@@ -636,6 +640,8 @@ int input_tab(char *buf, int place, int lastWasTab)
 
 	/* Free up any memory already allocated */
 	if (matches != NULL) {
+	    for (i = i; i < num_matches; i++)
+		free(matches[i]);
 	    free(matches);
 	    matches = (char **) NULL;
 	    num_matches = 0;
@@ -656,22 +662,80 @@ int input_tab(char *buf, int place, int lastWasTab)
 	/* Don't leak memory */
 	free(matchBuf);
 
+#ifdef DEBUG
+	fprintf(stderr, "%d matches found...\n", num_matches);
+#endif
 	/* Did we find exactly one match? */
 	switch(num_matches) {
 	case 0:
 	    blank_edit();
 	    break;
 	case 1:
-	    buf = nrealloc(buf, strlen(buf) + strlen(matches[0]) - pos + 1);
+
+	    buf = nrealloc(buf, strlen(buf) + strlen(matches[0]) + 1);
+
+	    if (strcmp(buf, "") && strstr(buf, "/")) {
+		for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf; tmp--)
+		    ;
+		tmp++;
+	    }
+	    else
+		tmp = buf;
+
+	    if (!strcmp(tmp, matches[0])) {
+
+		/* Is it a directory? */
+		if (stat(buf, &fileinfo) == -1)
+			break;
+		else if (S_ISDIR(fileinfo.st_mode)) {
+			strncat(buf, "/", 1);
+			*newplace += 1;
+		}
+		break;
+	    }
+
+	    copyto = tmp;
+	    for (pos = 0; *tmp == matches[0][pos] && 
+			pos <= strlen(matches[0]); pos++)
+		tmp++;
+
+
+#ifdef DEBUG
+	    fprintf(stderr, "buf = \'%s\'\n", buf);
+/*	    fprintf(stderr, "copyto = \'%s\'\n", copyto); */
+	    fprintf(stderr, "matches[0] = \'%s\'\n", matches[0]);
+	    fprintf(stderr, "pos = %d\n", pos);
+	    fflush(stderr);
+#endif
+
 	    /* write out the matched command */
-	    strncpy(buf + pos, matches[0] + pos,
-		    strlen(matches[0]) - pos);
-	    newplace += strlen(matches[0]) - pos;
+	    strncpy(copyto, matches[0],  strlen(matches[0]) + 1);
+	    *newplace += strlen(matches[0]) - pos;
+
+		if (stat(buf, &fileinfo) == -1)
+		    ;
+		else if (S_ISDIR(fileinfo.st_mode)) {
+			strncat(buf, "/", 1);
+			*newplace += 1;
+		}
+
 	    break;
 	default:
 	    /* Check to see if all matches share a beginning, and if so
 		tack it onto buf and then beep */
 
+	    if (strcmp(buf, "") && strstr(buf, "/")) {
+		for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf; tmp--)
+		    ;
+		tmp++;
+	    }
+	    else
+		tmp = buf;
+
+	    for (pos = 0; *tmp == matches[0][pos] && *tmp != 0 &&
+			pos <= strlen(matches[0]); pos++)
+		tmp++;
+
 	    while (1) {
 		match_matches = 0;
 
@@ -686,8 +750,8 @@ int input_tab(char *buf, int place, int lastWasTab)
 		    /* All the matches have the same character at pos+1,
 			so paste it into buf... */
 		    buf = nrealloc(buf, strlen(buf) + 2);
-		    strncpy(buf + pos, matches[0] + pos, 1);
-	 	    newplace++;
+		    strncat(buf, matches[0] + pos, 1);
+	 	    *newplace += 1;
 		    pos++;
 		} else {
 		    beep();
@@ -728,6 +792,8 @@ int input_tab(char *buf, int place, int lastWasTab)
 
 		strcat(foo, "  ");
 
+		/* Disable el cursor */
+		curs_set(0);
 		/* now, put the match on the screen */
 		waddnstr(edit, foo, strlen(foo));
 		col += strlen(foo);
@@ -752,5 +818,6 @@ int input_tab(char *buf, int place, int lastWasTab)
     }
 
     edit_refresh();
-    return newplace;
+    curs_set(1);
+    return buf;
 }
diff --git a/po/nano.pot b/po/nano.pot
index b07f124a..b761475e 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-09 23:23-0500\n"
+"POT-Creation-Date: 2000-11-13 23:13-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"
@@ -107,7 +107,7 @@ msgstr ""
 msgid "File exists, OVERWRITE ?"
 msgstr ""
 
-#: files.c:741
+#: files.c:807
 msgid "(more)"
 msgstr ""
 
@@ -384,7 +384,7 @@ msgid "Case Sens"
 msgstr ""
 
 #: global.c:344 global.c:364 global.c:375 global.c:385 global.c:401
-#: global.c:405 global.c:411 winio.c:1008
+#: global.c:405 global.c:411 winio.c:1011
 msgid "Cancel"
 msgstr ""
 
@@ -773,41 +773,41 @@ msgstr ""
 msgid "Replaced 1 occurence"
 msgstr ""
 
-#: search.c:443 search.c:536 search.c:552
+#: search.c:443 search.c:538 search.c:554
 msgid "Replace Cancelled"
 msgstr ""
 
-#: search.c:486
+#: search.c:488
 msgid "Replace this instance?"
 msgstr ""
 
-#: search.c:494
+#: search.c:496
 msgid "Replace failed: unknown subexpression!"
 msgstr ""
 
-#: search.c:569
+#: search.c:571
 #, c-format
 msgid "Replace with [%s]"
 msgstr ""
 
-#: search.c:573 search.c:577
+#: search.c:575 search.c:579
 msgid "Replace with"
 msgstr ""
 
 #. Ask for it
-#: search.c:612
+#: search.c:614
 msgid "Enter line number"
 msgstr ""
 
-#: search.c:614
+#: search.c:616
 msgid "Aborted"
 msgstr ""
 
-#: search.c:634
+#: search.c:636
 msgid "Come on, be reasonable"
 msgstr ""
 
-#: search.c:639
+#: search.c:641
 #, c-format
 msgid "Only %d lines available, skipping to last line"
 msgstr ""
@@ -817,67 +817,67 @@ msgstr ""
 msgid "actual_x_from_start for xplus=%d returned %d\n"
 msgstr ""
 
-#: winio.c:421
+#: winio.c:424
 #, c-format
 msgid "input '%c' (%d)\n"
 msgstr ""
 
-#: winio.c:459
+#: winio.c:462
 msgid "New Buffer"
 msgstr ""
 
-#: winio.c:462
+#: winio.c:465
 msgid "  File: ..."
 msgstr ""
 
-#: winio.c:470
+#: winio.c:473
 msgid "Modified"
 msgstr ""
 
-#: winio.c:922
+#: winio.c:925
 #, c-format
 msgid "Moved to (%d, %d) in edit buffer\n"
 msgstr ""
 
-#: winio.c:933
+#: winio.c:936
 #, c-format
 msgid "current->data = \"%s\"\n"
 msgstr ""
 
-#: winio.c:978
+#: winio.c:981
 #, c-format
 msgid "I got \"%s\"\n"
 msgstr ""
 
-#: winio.c:1003
+#: winio.c:1006
 msgid "Yes"
 msgstr ""
 
-#: winio.c:1005
+#: winio.c:1008
 msgid "All"
 msgstr ""
 
-#: winio.c:1007
+#: winio.c:1010
 msgid "No"
 msgstr ""
 
-#: winio.c:1144
+#: winio.c:1147
 #, c-format
 msgid "do_cursorpos: linepct = %f, bytepct = %f\n"
 msgstr ""
 
-#: winio.c:1148
+#: winio.c:1151
 msgid "line %d of %d (%.0f%%), character %d of %d (%.0f%%)"
 msgstr ""
 
-#: winio.c:1276
+#: winio.c:1279
 msgid "Dumping file buffer to stderr...\n"
 msgstr ""
 
-#: winio.c:1278
+#: winio.c:1281
 msgid "Dumping cutbuffer to stderr...\n"
 msgstr ""
 
-#: winio.c:1280
+#: winio.c:1283
 msgid "Dumping a buffer to stderr...\n"
 msgstr ""
diff --git a/proto.h b/proto.h
index ced01509..55691dda 100644
--- a/proto.h
+++ b/proto.h
@@ -88,7 +88,7 @@ int do_down(void);
 int do_left(void);
 int do_right(void);
 int check_wildcard_match(const char *text, const char *pattern);
-int input_tab(char *buf, int place, int lastWasTab);
+char *input_tab(char *buf, int place, int lastWasTab, int *newplace);
 
 void shortcut_init(void);
 void lowercase(char *src);
diff --git a/winio.c b/winio.c
index db8902c4..8e78e4c7 100644
--- a/winio.c
+++ b/winio.c
@@ -248,7 +248,7 @@ int nanogetstr(int allowtabs, char *buf, char *def, shortcut s[], int slen,
 	       int start_x)
 {
     int kbinput = 0, j = 0, x = 0, xend;
-    int x_left = 0, inputlen, tabbed = 0;
+    int x_left = 0, inputlen, tabbed = 0, shift = 0;
     char *inputbuf;
     
     inputbuf = nmalloc(strlen(def) + 1);
@@ -332,7 +332,10 @@ int nanogetstr(int allowtabs, char *buf, char *def, shortcut s[], int slen,
 	case NANO_CONTROL_I:
 	    if (allowtabs) {
 		tabbed++;
-		x += input_tab(inputbuf, (x - x_left), tabbed - 1);
+		shift = 0;
+		inputbuf = input_tab(inputbuf, (x - x_left), 
+				tabbed - 1, &shift);
+		x += shift;
 		nanoget_repaint(buf, inputbuf, x);
 		tabbed = 1;
 	    }
-- 
GitLab