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

Re: PATCH: following -C option in make completion

Daniel Shahaf wrote:
>     % make -C fo<TAB>
>     [no matches]
> It does work if I revert this patch.

The easy quick fix would be to use _directories for -C. That's only
wrong if multiple -C options are used which is obscure enough that
it didn't occur to me when I wrote the patch. The state is used for
-C's argument itself because of that GNU specific feature of allowing
-C to be repeatable. But after make -C <tab>, $opt_args[-C] is set
to the empty string for the current -C option. Hence the breakage you

I think it is preferable to use $opt_args over scanning $words as
in the original code. That'll only be wrong if the user mixes -C
and --directory (the old code ignored --directory completely so I
don't feel bad about that).

The following patch changes the basedir assignment. The new expansion is
especially gnarly so I'd appreciate if you could give it some testing.
It has to:
  - (temporarily) convert quoted colons to nulls
  - remove one level of shell quoting
  - prepend $PWD: so the current directory is the default and to
    workaround problems with splitting giving a string not an array when
    there's no separator at all
  - split on colons and use (@) and double quotes to avoid losing a
    final empty element
  - expand named directories for each element
  - throw away initial elements if a later one is absolute
  - drop the last element for -C (but not -I)
  - turn nulls back into colons
  - join elements with /


diff --git a/Completion/Unix/Command/_make b/Completion/Unix/Command/_make
index 06971f07a..21ed56184 100644
--- a/Completion/Unix/Command/_make
+++ b/Completion/Unix/Command/_make
@@ -120,12 +120,12 @@ _make-parseMakefile () {
 _make() {
-  local prev="$words[CURRENT-1]" file expl tmp is_gnu incl match basedir
+  local prev="$words[CURRENT-1]" file expl tmp is_gnu incl match basedir nul=$'\0'
   local context state state_descr line
   local -a option_specs
   local -A VARIABLES VAR_ARGS opt_args
   local -aU TARGETS keys
-  local ret=1
+  local -i cdir=-1 ret=1
   # VAR=VAL on the current command line
   for tmp in $words; do
@@ -142,7 +142,7 @@ _make() {
       '(-B --always-make)'{-B,--always-make}'[unconditionally make all targets]'
-      '*'{-C,--directory=}'[change directory first]:change to directory:->dir'
+      '*'{-C,--directory=}'[change directory first]:change to directory:->cdir'
       '-d[print lots of debug information]'
       '--debug=-[print various types of debug information]:debug options:->debug'
       '(-e --environment-overrides)'{-e,--environment-overrides}'[environment variables override makefiles]'
@@ -177,7 +177,7 @@ _make() {
     # Basic make options only.
-      '-C[change directory first]:directory:->dir'
+      '-C[change directory first]:directory:->cdir'
       '-I[include directory for makefiles]:directory:->dir'
       '-f[specify makefile]:makefile:->file'
       '-o[specify file not to remake]:file not to remake:->file'
@@ -188,11 +188,12 @@ _make() {
   _arguments -s $option_specs \
     '*:make target:->target' && ret=0
-  basedir=${(Q)~opt_args[-C]:-${opt_args[--directory]}}
-  VAR_ARGS[CURDIR]="${basedir:=$PWD}"
+  [[ $state = cdir ]] && cdir=-2
+  basedir=${(j./.)${${~"${(@s.:.):-$PWD:${(Q)${opt_args[-C]:-$opt_args[--directory]}//\\:/$nul}}"}[(R)/*,cdir]}//$nul/:}
+  VAR_ARGS[CURDIR]="${basedir}"
   case $state in
-    (dir)
+    (*dir)
     _description directories expl "$state_descr"
     _files "$expl[@]" -W $basedir -/ && ret=0

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