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

Re: PATCH: cd -q (was Re: _canonical_path ...)



On Fri, 28 Mar 2008 12:01:15 +0100
"Mikael Magnusson" <mikachu@xxxxxxxxx> wrote:
> I couldn't help but notice _cd doesn't complete directories after -q
> (or -L or -P or -s)

Yes, this is a long-standing bug and it's easy enough to teach it to
ignore options (remembering that -<-> is not an option).

> nor the options themselves.

This is less than easy enough to get working normally because we don't use
_arguments.  Probably the best thing to do is to use _arguments at the top
level and dispatch to the current code for first or second arguments.
Until then I've made it complete options only after a -, where it also
completes directory stack entries.  Since completing directory stack
entries is much more useful (at least with verbose listing) and the vast
majority of the time the options are just in the way I've added a style to
suppress option completions.  Normally this would be done with tags but in
this case I think we need them separate by default.  This all seems to make
rather heavy weather of something fairly simple.

Index: Completion/Zsh/Command/_cd
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Command/_cd,v
retrieving revision 1.7
diff -u -r1.7 _cd
--- Completion/Zsh/Command/_cd	3 Sep 2003 14:07:26 -0000	1.7
+++ Completion/Zsh/Command/_cd	28 Mar 2008 14:33:02 -0000
@@ -11,17 +11,37 @@
 #    it's not a lot of use.  If you don't type the + or - it will
 #    complete directories as normal.
 
+_cd_options() {
+  _arguments -s \
+  '-q[Quiet, no output or use of hooks]' \
+  '-s[Refuse to use paths with symlinks]' \
+  '(-P)-L[Retain symbolic links ignoring CHASE_LINKS]' \
+  '(-L)-P[Resolve symbolic links as CHASE_LINKS]'
+}
+
 setopt localoptions nonomatch
 
-local expl ret=1
+local expl ret=1 curarg
+integer argstart=2 noopts
+
+if (( CURRENT > 1 )); then
+  # if not in command position, may have options.
+  # Careful: -<-> is not an option.
+  while [[ $words[$argstart] = -* && argstart -lt CURRENT ]]; do
+    curarg=$words[$argstart]
+    [[ $curarg = -<-> ]] && break
+    (( argstart++ ))
+    [[ $curarg = -- ]] && noopts=1 && break
+  done
+fi
 
-if [[ CURRENT -eq 3 ]]; then
+if [[ CURRENT -eq $((argstart+1)) ]]; then
   # cd old new: look for old in $PWD and see what can replace it
   local rep
   # Get possible completions using word in position 2
-  rep=(${~PWD/$words[2]/*}~$PWD(-/))
+  rep=(${~PWD/$words[$argstart]/*}~$PWD(-/))
   # Now remove all the common parts of $PWD and the completions from this
-  rep=(${${rep#${PWD%%$words[2]*}}%${PWD#*$words[2]}})
+  rep=(${${rep#${PWD%%$words[$argstart]*}}%${PWD#*$words[$argstart]}})
   (( $#rep )) && _wanted -C replacement strings expl replacement compadd -a rep
 else
   # Complete directory stack entries with ~ or when not in command position
@@ -70,6 +90,11 @@
     [[ CURRENT -ne 1 || ( -z "$path[(r).]" && $PREFIX != */* ) ]] &&
         alt=( "${cdpath+local-}directories:${cdpath+local }directory:_path_files -/" "$alt[@]" )
 
+    if [[ CURRENT -eq argstart && noopts -eq 0 && $PREFIX = -* ]] &&
+      zstyle -t ":completion:${curcontext}:options" complete-options; then
+      alt=("$service-options:$service option:_cd_options" "$alt[@]")
+    fi
+
     _alternative "$alt[@]" && ret=0
 
     return ret
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.205
diff -u -r1.205 compsys.yo
--- Doc/Zsh/compsys.yo	28 Feb 2008 18:29:05 -0000	1.205
+++ Doc/Zsh/compsys.yo	28 Mar 2008 14:33:05 -0000
@@ -1221,6 +1221,16 @@
 line is not the name of an alias, matching alias names will be
 completed.
 )
+kindex(complete-options, completion style)
+time(tt(complete-options))(
+This is used by the completer for tt(cd), tt(chdir) and tt(pushd).
+For these commands a tt(-) is used to introduce a directory stack entry
+and completion of these is far more common than completing options.
+Hence unless the value of this style is true options will not be
+completed, even after an initial tt(-).  If it is true, options will
+be completed after an initial tt(-) unless there is a preceeding
+tt(--) on the command line.
+)
 kindex(completer, completion style)
 item(tt(completer))(
 The strings given as the value of this style provide the names of the


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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