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

Re: Redirection Symbol After Completion



Chris Johnson wrote:
> Hi.  This is just a minor, cosmetic nuance of zsh that I'd like to
> change, if possible.  When I complete a filename with
> 
>    cat fil<TAB>
> 
> the command line becomes "cat file.txt ".  A space is displayed after
> the completed filename, though I didn't type it.  If I type another
> argument, the space remains and separates the arguments.  However, if I
> type a redirection character like & or |, the space is removed and I get
> 
>    cat file.txt|
> 
> I personally find commands easier to parse with my eye when the space
> remains before the redirection symbol:
> 
>    cat file.txt |
> 
> Is there any way to force this space to persist even if I type a
> redirection operator?  Thanks for any insight.

It's a perfectly reasonable thing to want to configure, but
unfortunately I don't think it's possible to do this without some
tweaking of the completion system.  Even then, it's quite tricky to get
right and I haven't managed to so far.

To illustrate, here is a partial answer.  With the patch below you can
set, say,

  zstyle ":completion::complete:*:all-files" remove-chars " \t\n;&"

to change the set of characters which, when typed, will cause suffix
removal for the "all-files" tag (the general catch-all file completion);
it's the usual set with "|" removed.  (You'd probably want it more
generally, but as we'll see that's problematic.)

At first sight this does what you want.  However, try this

  mkdir testdir
  echo test<TAB>

giving

  echo testdir/

and now type "|".  The slash isn't removed, as it usually would be.
That's because the same mechanism controls the characters removed when
you have a suffix, including an automatically added /, and when you
don't, i.e. you get the space in your original question.

(The rest of this description is for connoisseurs...)

It gets even more complicated when a function specifies its own suffix
removal.  This again conflicts with with what I've done: I added the
test for the style to _description, which prepares options for the
builtin compadd that passes the details to the shell's internals for
handling.  Unfortunately _description not only doesn't know what the
suffix is, it doesn't know whether someone else might have used the -r
option.

For example when completing values for PATH the -r option is
passed down to indicate that ":" should cause a space to be removed.
_description doesn't see this and _path_files (the core of file
completion) sees two incompatible -r options and, as luck would have it,
uses the wrong one.  To complicate matters even further, sometimes
_path_files will call _description itself while at other times (as in
this case) it's already been called.

Probably this can be done better but it needs some clear thinking.  I
won't commit this patch.

The easiest thing to do is to add two options to compadd: one to specify
the default characters when there's a suffix, one to specify it when
there isn't.  These would both be overridden by -r.  However, that seems
a rather feeble alternative to getting the logic in the functions right.

Index: Completion/Base/Core/_description
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/Core/_description,v
retrieving revision 1.5
diff -u -r1.5 _description
--- Completion/Base/Core/_description	21 Jul 2003 17:01:44 -0000	1.5
+++ Completion/Base/Core/_description	8 Jul 2006 11:16:31 -0000
@@ -1,12 +1,10 @@
 #autoload
 
-local name gropt nopt xopt format gname hidden hide match opts tag sort
-
-opts=()
+local name gropt xopt format gname hidden hide match tag sort remchars
+local -a opts nopt
 
 gropt=(-J)
 xopt=(-X)
-nopt=()
 zparseopts -K -D -a nopt 1 2 V=gropt J=gropt x=xopt
 
 3="${${3##[[:blank:]]#}%%[[:blank:]]#}"
@@ -31,6 +29,9 @@
     opts=($opts -M "$match")
 [[ -n "$_matcher" ]] && opts=($opts -M "$_matcher")
 
+zstyle -s ":completion:${curcontext}:$1" remove-chars remchars &&
+    opts=($opts -r $remchars)
+
 # Use sort style, but ignore `menu' value to help _expand.
 # Also don't override explicit use of -V.
 if { zstyle -s ":completion:${curcontext}:$1" sort sort ||

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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