zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@sunsite.dk
Subject: Re: Redirection Symbol After Completion
Date: Sat, 08 Jul 2006 12:44:58 +0100	[thread overview]
Message-ID: <200607081145.k68BiwEY023823@pwslaptop.csr.com> (raw)
In-Reply-To: Your message of "Fri, 07 Jul 2006 13:32:13 EDT." <20060707173213.GA22592@namib.cs.utk.edu>

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@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


  reply	other threads:[~2006-07-08 11:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-07 17:32 Chris Johnson
2006-07-08 11:44 ` Peter Stephenson [this message]
2006-07-08 17:56   ` Bart Schaefer
2006-07-08 21:44     ` Bart Schaefer
2006-07-10 14:12     ` Chris Johnson
2006-07-10 23:30       ` Scott Anderson
2006-07-10 23:43         ` Bart Schaefer
2006-07-10 23:53           ` Scott Anderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200607081145.k68BiwEY023823@pwslaptop.csr.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).