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

Re: bash completion functions



On 28 Jan, Bart wrote:
> On Jan 28, 10:18am, Oliver Kiddle wrote:
> } The only things I've knowingly left unimplemented is completion for
> } jobs (because compgen is always run in a subshell)
> 
> We could do something about this, generically, by saving local copies of
> the jobstates and jobtexts hashes in _main_complete or some such.

I didn't think of that. I've done that in the _bash_completion
function. I can't see that it would be particularly useful to have in
_main_complete (matches added by a compadd running in a subshell are
lost anyway).

It's not likely to be so useful though because job completion is most
useful with builtins for which we have perfectly good zsh completions
already. But it is nice to have for completeness.

I've attached an update patch which does this job stuff, fixes
$COMP_POINT and does complete -r. Only thing it doesn't do now is
complete bash help topics. The second (mime) attachment is the 4.0 back
port.

Any ideas on where in the manual I should mention this function? It
only needs a couple of lines. The User Contributions chapter seems to
cover stuff in the Functions directory. Or would it be better mentioned
in the FAQ?

Oliver

--- Completion/bashcompinit	Wed Jan 29 10:28:23 2003
+++ Completion/bashcompinit	Wed Jan 29 10:28:23 2003
@@ -6,12 +6,16 @@
   local COMP_POINT COMP_CWORD
   local -a COMP_WORDS COMPREPLY BASH_VERSINFO
   local COMP_LINE="$words"
+  local -A savejobstates savejobtexts
 
-  (( COMP_POINT = ${#${(j. .)words}} + $$#QIPREFIX + $#IPREFIX + $#PREFIX ))
+  (( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT-1]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX ))
   (( COMP_CWORD = CURRENT - 1))
   COMP_WORDS=( $words )
   BASH_VERSINFO=( 2 05b 0 1 release )
   
+  savejobstates=( ${(kv)jobstates} )
+  savejobtexts=( ${(kv)jobtexts} )
+  
   [[ ${argv[${argv[(I)nospace]:-0}-1]} = -o ]] && suf=( -S '' )
  
   matches=( ${(f)"$(compgen $@)"} )
@@ -38,8 +42,8 @@
 }
 
 compgen() {
-  local opts prefix suffix ret=1 OPTARG OPTIND
-  local -a name res results
+  local opts prefix suffix job OPTARG OPTIND ret=1 
+  local -a name res results jids
   local -A shortopts
   
   emulate -L sh
@@ -93,12 +97,25 @@
 	    setopt kshglob noshglob braceexpand
 	    results+=( "${res[@]}" )
 	  ;;
+	  job) results+=( "${savejobtexts[@]%% *}" );;
 	  keyword) results+=( "${(k)reswords[@]}" ) ;;
+	  running)
+	    jids=( "${(@k)savejobstates[(R)running*]}" )
+	    for job in "${jids[@]}"; do
+	      results+=( ${savejobtexts[$job]%% *} )
+	    done
+	  ;;
+	  stopped)
+	    jids=( "${(@k)savejobstates[(R)suspended*]}" )
+	    for job in "${jids[@]}"; do
+	      results+=( ${savejobtexts[$job]%% *} )
+	    done
+	  ;;
 	  setopt|shopt) results+=( "${(k)options[@]}" ) ;;
 	  signal) results+=( "SIG${^signals[@]}" ) ;;
 	  user) results+=( "${(k)userdirs[@]}" ) ;;
       	  variable) results+=( "${(k)parameters[@]}" ) ;;
-	  helptopic|job|running|stopped) ;;
+	  helptopic) ;;
 	esac
       ;;
       F)
@@ -125,8 +142,8 @@
     esac
   done
   
-  # support for the last, `word' option to compgen. Not particularly valuable
-  # and without it zsh matching can do a better job. Comment in if needed.
+  # support for the last, `word' option to compgen. Zsh's matching does a
+  # better job but if you need to, comment this in and use compadd -U
   #shift $(( OPTIND - 1 ))
   #(( $# )) && results=( "${(M)results[@]:#$1*}" )
 
@@ -140,11 +157,11 @@
   zparseopts -D -a void o: A: G: W: C: F: P: S: X: a b c d e f g j k u v \
       p=print r=remove
   if [[ -n $print ]]; then
-    for cmd print in ${(kv)_comps[(R)_bash*]}; do
-      print "complete ${print#* } $cmd"
-    done
+    printf 'complete %2$s %1$s\n' "${(@kv)_comps[(R)_bash*]#* }"
   elif [[ -n $remove ]]; then
-    print "not implemented: -r option"
+    for cmd; do
+      unset "_comps[$cmd]"
+    done
   else
     compdef _bash_complete\ ${(j. .)${(q)args[1,-1-$#]}} "$@"
   fi



This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.
--- bashcompinit.new	Wed Jan 29 10:28:23 2003
+++ bashcompinit.4.0	Wed Jan 29 10:28:23 2003
@@ -7,6 +7,7 @@
   local -a COMP_WORDS COMPREPLY BASH_VERSINFO
   local COMP_LINE="$words"
   local -A savejobstates savejobtexts
+  set -- $=_bash_comps[$service]
 
   (( COMP_POINT = 1 + ${#${(j. .)words[1,CURRENT-1]}} + $#QIPREFIX + $#IPREFIX + $#PREFIX ))
   (( COMP_CWORD = CURRENT - 1))
@@ -59,77 +60,77 @@
       [abcdefgjkuv]) OPTARG="${shortopts[$name]}" ;&
       A)
         case $OPTARG in
-	  alias) results+=( "${(k)aliases[@]}" ) ;;
-	  arrayvar) results+=( "${(k@)parameters[(R)array*]}" ) ;;
-	  binding) results+=( "${(k)widgets[@]}" ) ;;
-	  builtin) results+=( "${(k)builtins[@]}" "${(k)dis_builtins[@]}" ) ;;
+	  alias) results=( "${results[@]}" "${(k)aliases[@]}" ) ;;
+	  arrayvar) results=( "${results[@]}" "${(k@)parameters[(R)array*]}" ) ;;
+	  binding) results=( "${results[@]}" "${(k)widgets[@]}" ) ;;
+	  builtin) results=( "${results[@]}" "${(k)builtins[@]}" "${(k)dis_builtins[@]}" ) ;;
 	  command)
-	    results+=(
-	      "${(k)commands[@]}" "${(k)aliases[@]}" "${(k)builtins[@]}"
-	      "${(k)functions[@]}" "${(k)reswords[@]}"
+	    results=(
+	      "${results[@]}" "${(k)commands[@]}" "${(k)aliases[@]}"
+	      "${(k)builtins[@]}" "${(k)functions[@]}" "${(k)reswords[@]}"
 	    )
 	  ;;
 	  directory)
 	    setopt bareglobqual
-	    results+=( ${IPREFIX}${PREFIX}*${SUFFIX}${ISUFFIX}(N-/) )
+	    results=( "${results[@]}" ${IPREFIX}${PREFIX}*${SUFFIX}${ISUFFIX}(N-/) )
 	    setopt nobareglobqual
 	  ;;
-	  disabled) results+=( "${(k)dis_builtins[@]}" ) ;;
-	  enabled) results+=( "${(k)builtins[@]}" ) ;;
-	  export) results+=( "${(k)parameters[(R)*export*]}" ) ;;
+	  disabled) results=( "${results[@]}" "${(k)dis_builtins[@]}" ) ;;
+	  enabled) results=( "${results[@]}" "${(k)builtins[@]}" ) ;;
+	  export) results=( "${results[@]}" "${(k)parameters[(R)*export*]}" ) ;;
 	  file)
 	    setopt bareglobqual
-	    results+=( ${IPREFIX}${PREFIX}*${SUFFIX}${ISUFFIX}(N) )
+	    results=( "${results[@]}" ${IPREFIX}${PREFIX}*${SUFFIX}${ISUFFIX}(N) )
 	    setopt nobareglobqual
 	  ;;
-	  function) results+=( "${(k)functions[@]}" ) ;;
+	  function) results=( "${results[@]}" "${(k)functions[@]}" ) ;;
 	  group)
 	    emulate zsh
 	    _groups -U -O res
 	    emulate sh
 	    setopt kshglob noshglob braceexpand
-	    results+=( "${res[@]}" )
+	    results=( "${results[@]}" "${res[@]}" )
 	  ;;
 	  hostname)
 	    emulate zsh
 	    _hosts -U -O res
 	    emulate sh
 	    setopt kshglob noshglob braceexpand
-	    results+=( "${res[@]}" )
+	    results=( "${results[@]}" "${res[@]}" )
 	  ;;
-	  job) results+=( "${savejobtexts[@]%% *}" );;
-	  keyword) results+=( "${(k)reswords[@]}" ) ;;
+	  job) results=( "${results[@]}" "${savejobtexts[@]%% *}" );;
+	  keyword) results=( "${results[@]}" "${(k)reswords[@]}" ) ;;
 	  running)
 	    jids=( "${(@k)savejobstates[(R)running*]}" )
 	    for job in "${jids[@]}"; do
-	      results+=( ${savejobtexts[$job]%% *} )
+	      results=( "${results[@]}" ${savejobtexts[$job]%% *} )
 	    done
 	  ;;
 	  stopped)
 	    jids=( "${(@k)savejobstates[(R)suspended*]}" )
 	    for job in "${jids[@]}"; do
-	      results+=( ${savejobtexts[$job]%% *} )
+	      results=( "${results[@]}" ${savejobtexts[$job]%% *} )
 	    done
 	  ;;
-	  setopt|shopt) results+=( "${(k)options[@]}" ) ;;
-	  signal) results+=( "SIG${^signals[@]}" ) ;;
-	  user) results+=( "${(k)userdirs[@]}" ) ;;
-      	  variable) results+=( "${(k)parameters[@]}" ) ;;
+	  setopt|shopt) results=( "${results[@]}" "${(k)options[@]}" ) ;;
+	  signal) results=( "${results[@]}" "SIG${^signals[@]}" ) ;;
+	  user) results=( "${results[@]}" "${(k)userdirs[@]}" ) ;;
+      	  variable) results=( "${results[@]}" "${(k)parameters[@]}" ) ;;
 	  helptopic) ;;
 	esac
       ;;
       F)
         COMPREPLY=()
 	$OPTARG "${words[0]}" "${words[CURRENT-1]}" "${words[CURRENT-2]}"
-	results+=( "${COMPREPLY[@]}" )
+	results=( "${results[@]}" "${COMPREPLY[@]}" )
       ;;
       G)
         setopt nullglob
-        results+=( ${~OPTARG} )
+        results=( "${results[@]}" ${~OPTARG} )
 	unsetopt nullglob
       ;;
-      W) eval "results+=( $OPTARG )" ;;
-      C) results+=( $(eval $OPTARG) ) ;;
+      W) eval "results=( "${results[@]}" $OPTARG )" ;;
+      C) results=( "${results[@]}" $(eval $OPTARG) ) ;;
       P) prefix="$OPTARG" ;;
       S) suffix="$OPTARG" ;;
       X)
@@ -157,16 +158,22 @@
   zparseopts -D -a void o: A: G: W: C: F: P: S: X: a b c d e f g j k u v \
       p=print r=remove
   if [[ -n $print ]]; then
-    printf 'complete %2$s %1$s\n' "${(@kv)_comps[(R)_bash*]#* }"
+    for cmd in ${(k)_bash_comps}; do
+      print "complete ${_bash_comps[$cmd]} $cmd"
+    done
   elif [[ -n $remove ]]; then
     for cmd; do
-      unset "_comps[$cmd]"
+      unset "_bash_comps[$cmd]"
     done
   else
-    compdef _bash_complete\ ${(j. .)${(q)args[1,-1-$#]}} "$@"
+    for cmd; do
+      _bash_comps[$cmd]="${args[1,-1-$#]}"
+    done
+    compdef _bash_complete "$@"
   fi
 }
 
+typeset -gA _bash_comps
 unfunction bashcompinit
 autoload -U bashcompinit
 return 0


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