zsh-users
 help / color / mirror / code / Atom feed
* Redirection Symbol After Completion
@ 2006-07-07 17:32 Chris Johnson
  2006-07-08 11:44 ` Peter Stephenson
  0 siblings, 1 reply; 8+ messages in thread
From: Chris Johnson @ 2006-07-07 17:32 UTC (permalink / raw)
  To: zsh-users

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.

-- 
Chris Johnson
cjohnson@cs.utk.edu
http://www.cs.utk.edu/~cjohnson


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Redirection Symbol After Completion
  2006-07-07 17:32 Redirection Symbol After Completion Chris Johnson
@ 2006-07-08 11:44 ` Peter Stephenson
  2006-07-08 17:56   ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Stephenson @ 2006-07-08 11:44 UTC (permalink / raw)
  To: zsh-users

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/


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Redirection Symbol After Completion
  2006-07-08 11:44 ` Peter Stephenson
@ 2006-07-08 17:56   ` Bart Schaefer
  2006-07-08 21:44     ` Bart Schaefer
  2006-07-10 14:12     ` Chris Johnson
  0 siblings, 2 replies; 8+ messages in thread
From: Bart Schaefer @ 2006-07-08 17:56 UTC (permalink / raw)
  To: zsh-users

On Jul 8, 12:44pm, Peter Stephenson wrote:
} Subject: Re: Redirection Symbol After Completion
}
} Chris Johnson wrote:
} > 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?
} 
} 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.

Skipping ahead a bit ...

}   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.

Presumably he wants the slash replaced by a space in that instance.  I
think you're going about this the wrong way -- rather than revamping
how autoremoval occurs in the general case, adjust the way redirection
ops are inserted.

    self-insert-redir() {
        integer l=$#LBUFFER
	zle self-insert
	(( $l >= $#LBUFFER )) && LBUFFER[-1]=" $LBUFFER[-1]"
    }
    zle -N self-insert-redir
    for op in \| \< \> \&
    do bindkey "$op" self-insert-redir
    done

I like that so much I might even keep it.

It'd be nice if it were possible to test whether a suffix removal is
pending rather than checking the length of LBUFFER before and after
the self-insert, but I think this suffices (no pun intended).

One could go so far as to actually replace self-insert and also check
with a style for what constitutes a redirection operator, but that
gets a bit uglier.  I believe there was a discussion several months
ago about the problems caused by not being able to instruct zsh when
to use the ZLE_KEEPSUFFIX internal flag on a user-defined widget.
(Hmm, maybe that problem doesn't apply here anyway.)

-- 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Redirection Symbol After Completion
  2006-07-08 17:56   ` Bart Schaefer
@ 2006-07-08 21:44     ` Bart Schaefer
  2006-07-10 14:12     ` Chris Johnson
  1 sibling, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2006-07-08 21:44 UTC (permalink / raw)
  To: zsh-users

On Jul 8, 10:56am, Bart Schaefer wrote:
} Subject: Re: Redirection Symbol After Completion
}
}     for op in \| \< \> \&
}     do bindkey "$op" self-insert-redir
}     done

Misc. aside:

    REDIR_OPS='|<>&'
    bindkey ${(s: :):-${^${(s::)REDIR_OPS}}" self-insert-redir"}


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Redirection Symbol After Completion
  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
  1 sibling, 1 reply; 8+ messages in thread
From: Chris Johnson @ 2006-07-10 14:12 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer sent me the following 1.9K:

> } > 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?
> 
>     self-insert-redir() {
>         integer l=$#LBUFFER
> 	       zle self-insert
> 	       (( $l >= $#LBUFFER )) && LBUFFER[-1]=" $LBUFFER[-1]"
>     }
>     zle -N self-insert-redir
>     for op in \| \< \> \&
>     do bindkey "$op" self-insert-redir
>     done
> 
> I like that so much I might even keep it.

I like it too.  Thanks so much, Bart!

-- 
Chris Johnson
cjohnson@cs.utk.edu
http://www.cs.utk.edu/~cjohnson


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Redirection Symbol After Completion
  2006-07-10 14:12     ` Chris Johnson
@ 2006-07-10 23:30       ` Scott Anderson
  2006-07-10 23:43         ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Scott Anderson @ 2006-07-10 23:30 UTC (permalink / raw)
  To: zsh-users

----- Original Message ----
From: Chris Johnson <cjohnson@cs.utk.edu>
To: zsh-users@sunsite.dk
Sent: Monday, July 10, 2006 8:12:20 AM
Subject: Re: Redirection Symbol After Completion

Bart Schaefer sent me the following 1.9K:

> } > 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?
> 
>     self-insert-redir() {
>         integer l=$#LBUFFER
>            zle self-insert
>            (( $l >= $#LBUFFER )) && LBUFFER[-1]=" $LBUFFER[-1]"
>     }
>     zle -N self-insert-redir
>     for op in \| \< \> \&
>     do bindkey "$op" self-insert-redir
>     done
> 
> I like that so much I might even keep it.

This is not working for me.

> ls
noname

> cat no<TAB>

  completes the file as expected.

> cat noname<SPACE>

  Then I type "|"

> cat noname cat noname|[-1]

Am I missing an option?

> echo $ZSH_NAME $ZSH_VERSION
zsh 4.3.2

> setopt
autocd
autopushd
nobeep
nobgnice
correct
extendedglob
extendedhistory
histfindnodups
histignorealldups
histignoredups
histnostore
histsavenodups
nohup
incappendhistory
interactive
interactivecomments
ksharrays
kshglob
monitor
nonomatch
numericglobsort
promptsubst
pushdignoredups
pushdminus
pushdsilent
sharehistory
shinstdin
shwordsplit
zle

Thanks in advance for the help.

Scott




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Redirection Symbol After Completion
  2006-07-10 23:30       ` Scott Anderson
@ 2006-07-10 23:43         ` Bart Schaefer
  2006-07-10 23:53           ` Scott Anderson
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2006-07-10 23:43 UTC (permalink / raw)
  To: Scott Anderson, zsh-users

On Jul 10,  4:30pm, Scott Anderson wrote:
> 
> Am I missing an option?

Add

    setopt localoptions noksharrays

to the top of the function definition.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Redirection Symbol After Completion
  2006-07-10 23:43         ` Bart Schaefer
@ 2006-07-10 23:53           ` Scott Anderson
  0 siblings, 0 replies; 8+ messages in thread
From: Scott Anderson @ 2006-07-10 23:53 UTC (permalink / raw)
  To: zsh-users

----- Original Message ----
From: Bart Schaefer <schaefer@brasslantern.com>
To: Scott Anderson <ee_in_co@yahoo.com>; zsh-users@sunsite.dk
Sent: Monday, July 10, 2006 5:43:26 PM
Subject: Re: Redirection Symbol After Completion

On Jul 10,  4:30pm, Scott Anderson wrote:
> 
> Am I missing an option?

Add

    setopt localoptions noksharrays

to the top of the function definition.

That was the trick!  Thanks.




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-07-10 23:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-07 17:32 Redirection Symbol After Completion Chris Johnson
2006-07-08 11:44 ` Peter Stephenson
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

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).