zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk
Subject: Re: PATCH: cd -q (was Re: _canonical_path ...)
Date: Fri, 28 Mar 2008 14:35:17 +0000	[thread overview]
Message-ID: <20080328143517.7785de01@news01> (raw)
In-Reply-To: <237967ef0803280401o7176d1c1n5e037a0574e8998e@mail.gmail.com>

On Fri, 28 Mar 2008 12:01:15 +0100
"Mikael Magnusson" <mikachu@gmail.com> 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@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


  reply	other threads:[~2008-03-28 14:36 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-26 10:44 _canonical_path not working on *BSD Baptiste Daroussin
2008-03-26 15:01 ` Debian bugs (Re: _canonical_path not working on *BSD) Bart Schaefer
2008-03-26 15:25   ` Clint Adams
2008-03-26 15:05 ` _canonical_path not working on *BSD Peter Stephenson
2008-03-26 15:27   ` Baptiste Daroussin
2008-03-26 15:34     ` Peter Stephenson
2008-03-26 15:51       ` Pea
2008-03-26 15:59       ` Clint Adams
2008-03-26 15:21 ` Pea
2008-03-26 15:36 ` Bart Schaefer
2008-03-26 15:40   ` Peter Stephenson
2008-03-26 16:04     ` Peter Stephenson
2008-03-26 16:18       ` Bart Schaefer
2008-03-26 16:21       ` Peter Stephenson
2008-03-26 16:38         ` Pea
2008-03-26 16:46           ` Peter Stephenson
2008-03-26 17:08             ` Pea
2008-03-26 17:17             ` Baptiste Daroussin
2008-03-27 10:23             ` Peter Stephenson
2008-03-27 11:08               ` Pea
2008-03-27 11:25                 ` Peter Stephenson
2008-03-27 12:15                   ` PATCH: cd -q (was Re: _canonical_path ...) Peter Stephenson
2008-03-27 12:25                     ` Stephane Chazelas
2008-03-27 12:35                     ` Mikael Magnusson
2008-03-27 12:48                       ` Peter Stephenson
2008-03-27 12:56                         ` Mikael Magnusson
2008-03-27 18:45                     ` Peter Stephenson
2008-03-28  8:16                       ` Pea
2008-03-28 11:01                       ` Mikael Magnusson
2008-03-28 14:35                         ` Peter Stephenson [this message]
2008-03-27 12:31                   ` _canonical_path not working on *BSD Pea
2008-03-27 15:39               ` Bart Schaefer
2008-03-27 18:06                 ` Peter Stephenson
2008-03-28  1:01                   ` Bart Schaefer
2008-03-28  7:51                     ` Mikael Magnusson
2008-03-28 10:01                     ` Peter Stephenson
2008-03-26 16:25       ` Pea

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=20080328143517.7785de01@news01 \
    --to=pws@csr.com \
    --cc=zsh-workers@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).