Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: annoying correction of directory name to non-directory name for cd



On Oct 4,  2:17pm, Vincent Lefevre wrote:
} Subject: Re: annoying correction of directory name to non-directory name f
}
} On 2015-09-16 18:07:14 -0700, Bart Schaefer wrote:
} > On Sep 17,  1:21am, Vincent Lefevre wrote:
} > } zsh often proposes to correct a directory name to a non-directory
} > } name. This is annoying.
} > 
} > CORRECT_ALL isn't intended to be that smart.
} 
} For the simplest cases, it doesn't need to know the meaning: when the
} word is followed by a slash, the filename must be a directory name.

Hmm.  Does this do what you want?

I've tried to minimize the stat()-ing to the names that are actually
candidate corrections.

diff --git a/Src/utils.c b/Src/utils.c
index ab3b0c2..530b51e 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2812,7 +2812,7 @@ spckword(char **s, int hist, int cmd, int ask)
 		     * as used in spscan(), so that an autocd is chosen *
 		     * only when it is better than anything so far, and *
 		     * so we prefer directories earlier in the cdpath.  */
-		    if ((thisdist = mindist(*pp, *s, bestcd)) < d) {
+		    if ((thisdist = mindist(*pp, *s, bestcd, 1)) < d) {
 			best = dupstring(bestcd);
 			d = thisdist;
 		    }
@@ -4057,7 +4057,8 @@ spname(char *oldname)
 	    thresh = 3;
 	else if (thresh > 100)
 	    thresh = 100;
-	if ((thisdist = mindist(newname, spnameguess, spnamebest)) >= thresh) {
+	thisdist = mindist(newname, spnameguess, spnamebest, *old == '/');
+	if (thisdist >= thresh) {
 	    /* The next test is always true, except for the first path    *
 	     * component.  We could initialize bestdist to some large     *
 	     * constant instead, and then compare to that constant here,  *
@@ -4082,12 +4083,13 @@ spname(char *oldname)
 
 /**/
 static int
-mindist(char *dir, char *mindistguess, char *mindistbest)
+mindist(char *dir, char *mindistguess, char *mindistbest, int isdir)
 {
     int mindistd, nd;
     DIR *dd;
     char *fn;
     char *buf;
+    struct stat st;
 
     if (dir[0] == '\0')
 	dir = ".";
@@ -4096,7 +4098,7 @@ mindist(char *dir, char *mindistguess, char *mindistbest)
     buf = zalloc(strlen(dir) + strlen(mindistguess) + 2);
     sprintf(buf, "%s/%s", dir, mindistguess);
 
-    if (access(unmeta(buf), F_OK) == 0) {
+    if (stat(unmeta(buf), &st) == 0 && (!isdir || S_ISDIR(st.st_mode))) {
 	strcpy(mindistbest, mindistguess);
 	free(buf);
 	return 0;
@@ -4110,7 +4112,8 @@ mindist(char *dir, char *mindistguess, char *mindistbest)
 	    continue;
 	nd = spdist(fn, mindistguess,
 		    (int)strlen(mindistguess) / 4 + 1);
-	if (nd <= mindistd) {
+	if (nd <= mindistd &&
+	    (!isdir || (stat(unmeta(fn), &st) == 0 && S_ISDIR(st.st_mode)))) {
 	    strcpy(mindistbest, fn);
 	    mindistd = nd;
 	    if (mindistd == 0)



Messages sorted by: Reverse Date, Date, Thread, Author