Subject: Re: Completion in empty double-quotes generates error
From: Bart Schaefer <[email-masked]>
To: [email-masked]
Date: Sat, 2 Apr 2016 11:18:15 -0700
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Apr 1, 11:20pm, Bart Schaefer wrote:
} Subject: Re: Completion in empty double-quotes generates error
}
} In the redirection 2>1, gotword() has concluded that the word to be
} completed is "1" rather than (a prefix of) "2".
} 
} This is probably because the lexer wants to treat "2>" as a single
} token

Indeed, zshlex() consumes "2>" as a single OUTANG token with tokfd = 2
handled as metadata.

This means that in "2>1" the only complete-able "word" that the lexer
recognizes on the line is the "1" following the redirection.

What would one expect to be completed with the cursor on the "2"?  The
-redirect- special context is for what comes *after* the operator.  I
can think of only two choices:

1.  Complete as if there were an implicit space to the right of the
    cursor.  This makes some sense because "2>" is treated as the same
    single token as ">" all alone.

2.  Treat it as an error and simply fail.

Patch below does this, but it ought to also insert a space to the right
of any inserted word, which I haven't done yet.  If someone else knows
how to do that, please jump in.

As a bonus, this patch fixes a case where wordpos was incorrectly left
at 1 near a redirection, causing command completion to incorrectly be
invoked.

diff --git a/Src/Zle/zle_tricky.c b/Src/Zle/zle_tricky.c
index a89b2a3..553721c 100644
--- a/Src/Zle/zle_tricky.c
+++ b/Src/Zle/zle_tricky.c
@@ -1238,6 +1238,15 @@ get_comp_string(void)
 	    /* Record if we haven't had the command word yet */
 	    if (wordpos == redirpos)
 		redirpos++;
+	    if (zlemetacs < (zlemetall - inbufct)) {
+	      /* Cursor is in the middle of a redirection, treat as a word */
+	      DPUTS3(addedx,
+		     "added x in redirect: wb = %d, we = %d, zlemetacs = %d",
+		     wb, we, zlemetacs);
+	      wb = zlemetacs;
+	      we = zlemetall - inbufct;
+	      wordpos++;
+	    }
         }
 	if (tok == DINPAR)
 	    tokstr = NULL;