{"thread":{"id":"29482","subject":"[PATCH v2] completion: add new zsh completion","startedAt":"2012-01-30T17:40:45Z","lastAt":"2012-01-30T17:40:45Z","messageCount":1,"participants":["Felipe Contreras"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"183343","messageId":"1327945245-7083-1-git-send-email-felipec@infradead.org","threadId":"29482","inReplyTo":null,"subject":"[PATCH v2] completion: add new zsh completion","fromName":"Felipe Contreras","fromEmail":"felipec@infradead.org","sentAt":"2012-01-30T17:40:45Z","receivedAt":"2012-01-30T17:40:45Z","isPatch":true,"sender":{"key":"felipec@infradead.org","avatar":null},"body":"From: Felipe Contreras <felipe.contreras@gmail.com>\n\nIt seems there's always issues with zsh's bash completion emulation,\nafter I took a deep look at the code, I found many issues[1].\n\nSo, here is a kind of wrapper that does the same, but properly :)\n\nThis would also allow us to make fixes if necessary, since the code is\nsimple enough, and extend functionality.\n\n[1] http://article.gmane.org/gmane.comp.shells.zsh.devel/24290\n\nSigned-off-by: Felipe Contreras <felipe.contreras@gmail.com>\n---\n\nv2:\n\n * Fix completion of aliased commands\n * Add check for wrong commands\n * Properly set return codes\n * Add default actiooon; for 'git foo -- <tab>'\n\n contrib/completion/git-completion.zsh |  181 +++++++++++++++++++++++++++++++++\n 1 files changed, 181 insertions(+), 0 deletions(-)\n create mode 100644 contrib/completion/git-completion.zsh\n\ndiff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh\nnew file mode 100644\nindex 0000000..bca08f2\n--- /dev/null\n+++ b/contrib/completion/git-completion.zsh\n@@ -0,0 +1,181 @@\n+#compdef git gitk\n+\n+# zsh completion wrapper for git\n+#\n+# You need git's bash completion script installed somewhere, by default on the\n+# same directory as this script.\n+#\n+# If your script is on ~/.git-completion.sh instead, you can configure it on\n+# your ~/.zshrc:\n+#\n+#  zstyle ':completion:*:*:git:*' script ~/.git-completion.sh\n+#\n+# The recommended way to install this script is to copy to\n+# '~/.zsh/completion/_git', and then add the following to your ~/.zshrc file:\n+#\n+#  fpath=(~/.zsh/completion $fpath)\n+\n+compgen () {\n+\tlocal prefix suffix\n+\tlocal -a results\n+\twhile getopts \"W:P:S:\" opt\n+\tdo\n+\t\tcase $opt in\n+\t\tW)\n+\t\t\tresults=( ${(Q)~=OPTARG} )\n+\t\t\t;;\n+\t\tP)\n+\t\t\tprefix=\"$OPTARG\"\n+\t\t\t;;\n+\t\tS)\n+\t\t\tsuffix=\"$OPTARG\"\n+\t\t\t;;\n+\t\tesac\n+\tdone\n+\tprint -l -r -- \"$prefix${^results[@]}$suffix\"\n+}\n+\n+complete () {\n+\t# do nothing\n+\treturn 0\n+}\n+\n+zstyle -s \":completion:$curcontext:\" script script\n+test -z \"$script\" && script=\"$(dirname ${funcsourcetrace[1]%:*})\"/git-completion.bash\n+ZSH_VERSION='' . \"$script\"\n+\n+_get_comp_words_by_ref ()\n+{\n+\twhile [ $# -gt 0 ]; do\n+\t\tcase \"$1\" in\n+\t\tcur)\n+\t\t\tcur=${_words[CURRENT]}\n+\t\t\t;;\n+\t\tprev)\n+\t\t\tcur=${_words[CURRENT-1]}\n+\t\t\t;;\n+\t\twords)\n+\t\t\twords=(\"${_words[@]}\")\n+\t\t\t;;\n+\t\tcword)\n+\t\t\t((cword = CURRENT - 1))\n+\t\t\t;;\n+\t\tesac\n+\t\tshift\n+\tdone\n+}\n+\n+_bash_wrap ()\n+{\n+\tlocal -a COMPREPLY results _words\n+\t_words=( $words )\n+\t() {\n+\t\temulate -L sh\n+\t\tsetopt KSH_TYPESET\n+\t\tsetopt kshglob noshglob braceexpand nokshautoload\n+\t\ttypeset -h words\n+\t\tlocal cur words cword prev\n+\t\t_get_comp_words_by_ref -n =: cur words cword prev\n+\t\t$1\n+\t} $1\n+\tresults=( \"${^COMPREPLY[@]}\" )\n+\tif [[ -n $results ]]; then\n+\t\tlocal COMP_WORDBREAKS=\"\\\"'@><=;|&(:\"\n+\t\tlocal i start\n+\t\tlocal cur=\"${words[CURRENT]}\"\n+\t\ti=$(expr index \"$cur\" \"$COMP_WORDBREAKS\")\n+\t\tstart=\"${cur:0:$i}\"\n+\t\tcompadd -Q -S '' -p \"$start\" -a results && ret=0\n+\tfi\n+\tif (( ret )); then\n+\t\t_default -S '' && ret=0\n+\tfi\n+}\n+\n+_gitk ()\n+{\n+\t__git_has_doubledash && return\n+\n+\tlocal g=\"$(__gitdir)\"\n+\tlocal merge=\"\"\n+\tif [ -f \"$g/MERGE_HEAD\" ]; then\n+\t\tmerge=\"--merge\"\n+\tfi\n+\tcase \"$cur\" in\n+\t--*)\n+\t\t__gitcomp \"\n+\t\t$__git_log_common_options\n+\t\t$__git_log_gitk_options\n+\t\t$merge\n+\t\t\"\n+\t\treturn\n+\t\t;;\n+\tesac\n+\t__git_complete_revlist\n+}\n+\n+_get_completion_func ()\n+{\n+\temulate -L sh\n+\tsetopt KSH_TYPESET\n+\tlocal command=\"$1\"\n+\n+\tcompletion_func=\"_git_${command//-/_}\"\n+\tdeclare -f $completion_func >/dev/null && return\n+\n+\tlocal expansion=$(__git_aliased_command \"$command\")\n+\tif [ -n \"$expansion\" ]; then\n+\t\tcompletion_func=\"_git_${expansion//-/_}\"\n+\t\tdeclare -f $completion_func >/dev/null && return\n+\tfi\n+\n+\tcompletion_func=\n+}\n+\n+_git ()\n+{\n+\tlocal ret=1\n+\n+\tif [[ $service != git ]]\n+\tthen\n+\t\t_bash_wrap _$service\n+\t\treturn ret\n+\tfi\n+\n+\tlocal curcontext=\"$curcontext\" state state_descr line\n+\ttypeset -A opt_args\n+\n+\t_arguments -C \\\n+\t\t'(-p --paginate --no-pager)'{-p,--paginate}'[Pipe all output into ''less'']' \\\n+\t\t'(-p --paginate)--no-pager[Do not pipe git output into a pager]' \\\n+\t\t'--git-dir=-[Set the path to the repository]: :_directories' \\\n+\t\t'--bare[Treat the repository as a bare repository]' \\\n+\t\t'(- :)--version[Prints the git suite version]' \\\n+\t\t'--exec-path=-[Path to where your core git programs are installed]:: :_directories' \\\n+\t\t'--html-path[Print the path where git''s HTML documentation is installed]' \\\n+\t\t'--work-tree=-[Set the path to the working tree]: :_directories' \\\n+\t\t'--namespace=-[Set the git namespace]: :_directories' \\\n+\t\t'(- :)--help[Prints the synopsis and a list of the most commonly used commands]' \\\n+\t\t'(-): :->command' \\\n+\t\t'(-)*:: :->option-or-argument' && return 0\n+\n+\tcase $state in\n+\t(command)\n+\t\temulate sh -c __git_compute_porcelain_commands\n+\t\tlocal -a porcelain aliases\n+\t\tporcelain=( ${=__git_porcelain_commands} )\n+\t\taliases=( $(__git_aliases) )\n+\t\t_describe -t porcelain-commands 'porcelain commands' porcelain && ret=0\n+\t\t_describe -t aliases 'aliases' aliases && ret=0\n+\t\t;;\n+\t(option-or-argument)\n+\t\tlocal completion_func\n+\t\t_get_completion_func \"${words[1]}\"\n+\t\ttest \"$completion_func\" && _bash_wrap $completion_func\n+\t\t;;\n+\tesac\n+\n+\treturn ret\n+}\n+\n+_git\n-- \n1.7.8\n"}]}