Created
November 7, 2011 14:39
-
-
Save hchbaw/1345186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#compdef - my-zargs t | |
setopt localoptions extendedglob | |
() { | |
local -a tmp0 tmp1 tmp | |
# copied from _zargs | |
echo "\ | |
-eof::=eof e::=eof \ | |
-exit x \ | |
-help \ | |
-interactive p \ | |
-max-args:=n n:=n \ | |
-max-chars:=s s:=s \ | |
-max-lines::=l l::=l L:=l \ | |
-max-procs:=P P:=P \ | |
-no-run-if-empty r \ | |
-null 0 \ | |
-replace::=i i::=i I:=i \ | |
-verbose t \ | |
-version | |
" | read -A tmp0 | |
: ${(A)tmp1::=${(M)tmp0:#-*}} | |
{ | |
: ${(A)tmp::=${(M)tmp1:#*:*}} | |
: ${(A)tmp::=${tmp%%:*}} | |
: ${(A)tmp::=-${^tmp}} | |
typeset -ga _zl_longs_arg | |
: ${(A)_zl_longs_arg::=$tmp} | |
: ${(A)tmp::=${tmp1:#*:*}} | |
: ${(A)tmp::=-${^tmp}} | |
typeset -ga _zl_longs_noarg | |
: ${(A)_zl_longs_noarg::=$tmp} | |
} | |
} | |
_zl-scan () { | |
local -i i="$1" | |
local eopp="$2" | |
local eofp="$3" | |
local accufun="$4" | |
local eofpred="$5" | |
local zargfun="$6" | |
local w1="$7" | |
local w2="$8" | |
shift 8 | |
((i==CURRENT)) && { | |
local s; _zl-eof-str s; "$zargfun" "$eopp" "$eofp" "$s"; return $? | |
} | |
((i++)) | |
local -a arg; : ${(A)arg::=$@} | |
_zl-scan-1 () { | |
_zl-scan $i "$1" "$2" $accufun $eofpred $zargfun "$arg[1]" "$3" $arg[2,-1] | |
return $? | |
} | |
if [[ $eopp == nil ]]; then | |
[[ -z $w2 ]] && [[ $w1 == -[^-]* ]] && { | |
"$accufun" short "$w1" | |
_zl-scan-1 $eopp $eofp ""; return $? | |
} | |
[[ -z $w2 ]] && [[ -n ${(M)_zl_longs_noarg:#${w1}} ]] && { | |
"$accufun" long "$w1" t | |
_zl-scan-1 $eopp $eofp ""; return $? | |
} | |
[[ -z $w2 ]] && [[ -n ${(M)_zl_longs_arg:#${w1}} ]] && { | |
_zl-scan-1 $eopp $eofp "$w1"; return $? | |
} | |
[[ -z $w2 ]] && [[ -n ${(M)_zl_longs_arg:#${w1%%\=*}} ]] && { | |
"$accufun" long "${w1%%\=*}" "${w1#*\=}" | |
_zl-scan-1 $eopp $eofp ""; return $? | |
} | |
[[ -z $w2 ]] && [[ $w1 == --[[:print:]]## ]] && ((i==CURRENT)) && { | |
_zl-scan-1 nil $eofp "$w1"; return $? | |
} | |
[[ -z $w2 ]] && [[ $w1 == --[[:print:]]## ]] && ((i!=CURRENT)) && { | |
_zl-scan-1 t $eofp "$w1"; return $? | |
} | |
[[ -z $w2 ]] && [[ -n $w1 ]] && [[ $w1 != -* ]] && { | |
_zl-scan-1 t $eofp ""; return $? | |
} | |
[[ -n $w2 ]] && [[ $w1 == -* ]] && { | |
_zl-scan-1 t $eofp ""; return $? | |
} | |
[[ -n $w2 ]] && [[ $w1 != -* ]] && { | |
"$accufun" long "$w2" "$w1" | |
_zl-scan-1 $eopp $eofp ""; return $? | |
} | |
_zl-scan-1 $eopp $eofp ""; return $? | |
fi | |
[[ $eofp == nil ]] && { "$eofpred" "$w1" } && ((i!=CURRENT)) && { | |
_zl-scan-1 $eopp $i ""; return $? | |
} | |
_zl-scan-1 $eopp $eofp ""; return $? | |
} | |
typeset -gA _zl_scan_options | |
_zl-scan-accum () { | |
local shortorlong="$1" | |
local -a match mbegin mend | |
case $shortorlong in | |
(short) # XXX: the '-e..' form only be considered. | |
[[ "$2" == (#b)(-[[:print:]])([[:print:]]##) ]] && | |
_zl_scan_options+=("$match[1]" "$match[2]") | |
;; | |
(long) # XXX: be considered as an argument is mandatory. | |
_zl_scan_options+=("$2" "$3") | |
;; | |
esac | |
} | |
_zl-eof-str () { | |
local place="$1" | |
local _tmp | |
_tmp=${_zl_scan_options[-e]-} | |
[[ -n $_tmp ]] && { : ${(P)place::=$_tmp}; return } | |
_tmp=${_zl_scan_options[--eof]-} | |
[[ -n $_tmp ]] && { : ${(P)place::=$_tmp}; return } | |
: ${(P)place::=--} | |
} | |
_zl-scan-eof-p () { local s; _zl-eof-str s; [[ "$s" == "$1" ]] } | |
_zl-complete () { | |
local cfun="$1" | |
local eopp="$2" | |
local eofn="$3" | |
local eofs="$4" | |
if [[ $eopp == nil ]]; then | |
[[ $words[CURRENT] == -* ]] && { _zl_zargs; return $? } | |
[[ -n $words[CURRENT-1] ]] && \ | |
[[ -n ${(M)_zl_longs_arg:#${words[CURRENT-1]}} ]] && { | |
_message "zargs long option"; return $? | |
} | |
[[ -z $words[CURRENT] ]] && {_zl-complete-files; return $? } | |
return -1 | |
fi | |
if [[ $eopp != nil ]] && [[ $eofn == nil ]]; then | |
[[ -n $words[CURRENT] ]] && [[ $eofs == "$words[CURRENT]"* ]] && { | |
"$cfun" "$eofs"; return $? | |
} | |
_zl-complete-files | |
return $? | |
fi | |
if [[ $eofn != nil ]]; then | |
if [[ $eofn == CURRENT ]] && [[ -z "$words[CURRENT]" ]]; then | |
"$cfun" "$eofs"; return $? | |
else | |
local -i i=$eofn | |
shift $i words; ((CURRENT-=i)); _normal | |
return $? | |
fi | |
fi | |
} | |
_zl-complete-files-globbing-p () { [[ "$1" == *'*'* ]] } | |
_zl-complete-files () { | |
if _zl-complete-files-globbing-p "$words[CURRENT]"; then | |
local -a match mbegin mend | |
_have_glob_qual "$words[CURRENT]" && { _files; return $? } | |
if [[ $KEYS != ' ' ]]; then | |
[[ $words[CURRENT] == (#b)(*/)'**'* ]] && PREFIX="$match[1]" | |
[[ $words[CURRENT] == '**'* ]] && PREFIX="./" | |
_files; return $? | |
else | |
_message "globbing"; return $? | |
fi | |
else | |
_files; return $? | |
fi | |
} | |
_zl-complete-annoying-maybe () { | |
local eofs="$1" | |
[[ $RBUFFER == *"$eofs $EDITOR "* ]] && return -1 | |
[[ "$words[CURRENT-1] $eofs $EDITOR" == "$eofs $EDITOR" ]] && return -1 | |
local -a opts; opts=($EDITOR) | |
_zl-complete-annoying-maybe-1 () { compadd "$@" -Q -a opts } | |
if [[ "$words[CURRENT-1]" == "$eofs" ]]; then | |
_zl-complete-annoying-maybe-1 | |
else | |
_zl-complete-annoying-maybe-1 -P "$eofs " | |
fi | |
} | |
_zl-complete~ () { | |
# XXX: ZL_COMPLETE_ANNOYANCE=_zl-complete-annoying-maybe | |
_zl-complete ${ZL_COMPLETE_ANNOYANCE:-_nothing} "$@" | |
return $? | |
} | |
_zargs_lazy () { | |
((CURRENT==1)) && return # paranoid | |
_zl_scan_options=() | |
_zl-scan 1 nil nil \ | |
_zl-scan-accum _zl-scan-eof-p _zl-complete~ $words[2] "" "" $words[3,-1] | |
} | |
_zargs_lazy_init () { | |
autoload +X _zargs | |
eval "_zl_zargs () { ${functions[_zargs]/_command_names -e/} }" | |
} | |
_zargs_lazy_init | |
_zargs_lazy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#autoload | |
local replace='{}' | |
(( ${@[$@[(I)--],-1][(I)$replace]} )) && \ | |
set -- -I$replace $@ | |
; set -- zargs $@ | |
$@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment