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

PATCH: expansion of custom abbreviations in configure (autoconf) completion



Completion can be used for arbitrary expansions and while there are
plenty of other approaches to abbreviations, it is the one place where
we already parse the line and dispatch to different functions based on
context. We have some nifty uses such as pid completion.

configure scripts are one place where I often want to reuse complex
mixes of options that I tend to forget. They also don't take any normal
arguments aside from uppercase variable assignments and options so extra
expandable arguments won't conflict.

The patch below allows, e.g.:

    zstyle ':completion:*:configure:*:arguments' build-profiles \
      zdebug 'CC=clang-devel CFLAGS="-ggdb -Og -Wall" --prefix=$scratch/inst --enable-zsh-debug' \
      asan   'CFLAGS="-fsanitize=address -fno-omit-frame-pointer" LDFLAGS="-fsanitize=address"'

And then, completion after configure zd<tab> will expand those options.
Without the style, nothing much changes so the feature is hidden from
most users.

Or should we leave such things to abbreviation plugins? Or does anyone
have better suggestions for solving this? I also pondered separating
this out so it can be used elsewhere and use more consistent style/tag
naming. There are other commands where I could imagine using this but
often the aliases might clash with real arguments. Given the different
characteristics, I would want such things to appear differently: e.g.
via:
  zstyle ':completion:*:configure:*:build-profiles' list-separator ""
  zstyle ':completion:*:configure:*:build-profiles' list-colors \
      "=(#b)([^ ]#) #(*)==${.zle.sgr[abbrev]}=${.zle.sgr[subdue]}"

The patch also adds completion for values of CC and CXX, completes
common variables after configure and their values using _value.

Oliver

diff --git a/Completion/Unix/Command/_configure b/Completion/Unix/Command/_configure
index 3b22b53db..74552d71c 100644
--- a/Completion/Unix/Command/_configure
+++ b/Completion/Unix/Command/_configure
@@ -1,6 +1,12 @@
 #compdef configure config.status
 
-_arguments -- -i '(--(disable|enable)-FEATURE* --(with|without)-PACKAGE*)' \
+local pad sep ret=1
+local -a expl suf plist filter
+local -A profiles
+
+[[ -prefix - ]] ||
+    ! zstyle -T ":completion:${curcontext}:options" prefix-needed &&
+    _arguments -- -i '(--(disable|enable)-FEATURE* --(with|without)-PACKAGE*)' \
               -s '((#s)--disable- --enable-
 	           (#s)--enable- --disable-
 		   (#s)--with- --without-
@@ -8,4 +14,32 @@ _arguments -- -i '(--(disable|enable)-FEATURE* --(with|without)-PACKAGE*)' \
 	      '*=(E|)PREFIX*:prefix directory:_files -/' \
               '*=PROGRAM*:program:_command_names -e' \
 	      '*=NAME*executable*:program:_command_names -e' \
-	      '*=NAME*:file:_files'
+	      '*=NAME*:file:_files' && ret=0
+
+if [[ ! -prefix - ]]; then
+  if [[ "$PREFIX" = *\=* ]]; then
+    compstate[parameter]="${PREFIX%%\=*}"
+    compset -P 1 '*='
+    _value && ret=0
+  else
+    compset -S '=*' || suf=( -S = )
+    _description -x variables expl variable
+    compadd "$expl[@]" $suf CC CFLAGS CPP CPPFLAGS CXX CXXFLAGS DEFS \
+        ERLCFLAGS FCFLAGS FFLAGS GOFLAGS LDFLAGS LIBS OBJCFLAGS \
+        OBJCXXFLAGS && ret=0
+
+    if zstyle -a ":completion:${curcontext}:arguments" build-profiles plist; then
+      profiles=( "$plist[@]" )
+      _description build-profiles expl "build profile"
+      compadd "$expl[@]" -O filter -k profiles
+      pad=${#${(O)${filter//?/.}}[1]}
+      profiles=( ${(kv)profiles[(I)(${(j.|.)${(@b)filter}})]} )
+      zstyle -s ":completion:${curcontext}:build-profiles" list-separator sep || sep=--
+      print -v plist -f "%-${pad}s $sep %s" ${(kv)profiles}
+      compadd "$expl[@]" -Q -U -P "$IPREFIX" -S "$ISUFFIX" -ld plist -a profiles && ret=0
+      [[ -n "$PREFIX$SUFFIX" && compstate[nmatches] -gt 1 ]] && compstate[insert]=
+    fi
+  fi
+fi
+
+return ret
diff --git a/Completion/Unix/Command/_gcc b/Completion/Unix/Command/_gcc
index 1f05edb73..3c7db6beb 100644
--- a/Completion/Unix/Command/_gcc
+++ b/Completion/Unix/Command/_gcc
@@ -1,10 +1,18 @@
-#compdef gcc g++ cc c++ llvm-gcc llvm-g++ clang clang++ -value-,LDFLAGS,-default- -value-,CFLAGS,-default- -value-,CXXFLAGS,-default- -value-,CPPFLAGS,-default- -P gcc-* -P g++-* -P c++-*
+#compdef gcc g++ cc c++ llvm-gcc llvm-g++ clang clang++ -value-,LDFLAGS,-default- -value-,CFLAGS,-default- -value-,CXXFLAGS,-default- -value-,CPPFLAGS,-default- -value-,CC,-default- -value-,CXX,-default- -P gcc-* -P g++-* -P c++-*
 
 local curcontext="$curcontext" state line ret=1 expl i
 local -a args args2 warnings arch
 typeset -A opt_args
 
-if [[ "$service" = -value-* ]]; then
+if [[ "$service" = -value-,CC,* ]]; then
+  _description compilers expl compiler
+  compadd "$expl[@]" -M 'r:|-=* r:|=*' -k 'commands[(I)(clang|([ig]|sun|open)cc(|<->|-devel)|icx)]'
+  return
+elif [[ "$service" = -value-,CXX,* ]]; then
+  _description compilers expl compiler
+  compadd "$expl[@]" -M 'r:|-=* r:|=*' -k 'commands[(I)((clang|g)++(|<->|-devel)|(sun|open|)CC|icpx)]'
+  return
+elif [[ "$service" = -value-* ]]; then
   compset -q
   words=( fake "$words[@]" )
   (( CURRENT++ ))




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