既存のzsh補完機能を拡張する方法は?

既存のzsh補完機能を拡張する方法は?

一部のカスタムアリパラメータにオートコンプリートを追加しようとしています。しかし、これは私が維持したい既存のoh my zsh antプラグインのオートコンプリート機能を上書きするようです。私のzshプラグインが私のカスタムアリオートコンプリート機能と調和して動作するようにする簡単な方法はありますか?

既存のプラグインです~/.oh-my-zsh/plugins/ant/ant.plugin.zsh

_ant_does_target_list_need_generating () {
  [ ! -f .ant_targets ] && return 0;
  [ build.xml -nt .ant_targets ] && return 0;
  return 1;
}

_ant () {
  if [ -f build.xml ]; then
    if _ant_does_target_list_need_generating; then
        ant -p | awk -F " " 'NR > 5 { print lastTarget }{lastTarget = $1}' > .ant_targets
    fi
    compadd -- `cat .ant_targets`
  fi
}

compdef _ant ant

私の自動完成時間は~/my_completions/_ant

#compdef ant

_arguments '-Dprop=-[properties file]:filename:->files' '-Dsrc=-[build directory]:directory:_files -/'
case "$state" in
    files)
        local -a property_files
        property_files=( *.properties )
        _multi_parts / property_files
        ;;
esac

これは私のものです$fpath。私の完了パスはリストの前にあるので、私のスクリプトは優先順位を持つようです。

/Users/myusername/my_completions /Users/myusername/.oh-my-zsh/plugins/git /Users/myusername/.oh-my-zsh/functions /Users/myusername/.oh-my-zsh/completions /usr/local/share/zsh/site-functions /usr/share/zsh/site-functions /usr/share/zsh/5.0.8/functions

ベストアンサー1

最良の方法は完成機能を拡張することだと思います。次のことができます。 https://unix.stackexchange.com/a/450133

まず、既存の関数の名前を見つける必要があります。これを行うには、_complete_helpCTRL-h(または他のショートカット)をバインドし、コマンドを入力して完成関数を見つけることができます。以下はgitで実行する例です。

% bindkey '^h' _complete_help  
% git [press ctrl-h]
tags in context :completion::complete:git::
    argument-1 options  (_arguments _git)
tags in context :completion::complete:git:argument-1:
    aliases main-porcelain-commands user-commands third-party-commands ancillary-manipulator-commands ancillary-interrogator-commands interaction-commands plumbing-manipulator-commands plumbing-interrogator-commands plumbing-sync-commands plumbing-sync-helper-commands plumbing-internal-helper-commands  (_git_commands _git)

この場合、完成関数はです_git。次に、.zshrc次のようにオーバーライドできます。

# Call the function to make sure that it is loaded.
_git 2>/dev/null
# Save the original function.
functions[_git-orig]=$functions[_git]
# Redefine your completion function referencing the original.
_git() {
    _git-orig "$@"
    ...
}

compdefすでに関数にバインドされており、関数定義のみが変更されているため、再度呼び出す必要はありません。

おすすめ記事