Skip to content
Myr

Enable native completion for zsh

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nicholas Wiles · committed by David Lord · parent bf30737 · Browse files · 2 files+31 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+31 added−16 removed

  • other:+31 added−16 removed

2 files+31 added−16 removed

modified:click/_bashcomplete.py+22 added-6 removed
8 unmodified lines
9from .types import Choice1011WORDBREAK = '='1213COMPLETION_SCRIPT = '''13COMPLETION_SCRIPT_BASH = '''14%(complete_func)s() {15 local IFS=$'\n'16 COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\17 COMP_CWORD=$COMP_CWORD \\18 %(autocomplete_var)s=complete $1 ) )19 return 020}2122complete -F %(complete_func)s -o default %(script_names)s22complete -F %(complete_func)s %(script_names)s23'''2425COMPLETION_SCRIPT_ZSH = '''26%(complete_func)s() {27 emulate -L zsh28 local IFS=$'\n'29 local completions=( $( env COMP_WORDS="${words[*]}" \\30 COMP_CWORD=$((CURRENT-1)) \\31 %(autocomplete_var)s="complete" \\32 %(script_names)s ) )33 compadd -M 'r:|=* l:|=* r:|=*' -a -- completions 34}3536compdef %(complete_func)s %(script_names)s37'''3839_invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')404128def get_completion_script(prog_name, complete_var):42def get_completion_script(prog_name, complete_var, shell):43 cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_'))30 return (COMPLETION_SCRIPT % {44 script = COMPLETION_SCRIPT_ZSH if shell == 'zsh' else COMPLETION_SCRIPT_BASH45 return (script % {46 'complete_func': '_%s_completion' % cf_name,47 'script_names': prog_name,48 'autocomplete_var': complete_var,49 }).strip() + ';'
173 unmodified lines
223 return True224225226def bashcomplete(cli, prog_name, complete_var, complete_instr):212 if complete_instr == 'source':213 echo(get_completion_script(prog_name, complete_var))227 if complete_instr.startswith('source'):228 shell = 'zsh' if complete_instr.endswith('zsh') else 'bash'229 echo(get_completion_script(prog_name, complete_var, shell))230 return True231 elif complete_instr == 'complete':232 return do_complete(cli, prog_name)233 return False
modified:docs/bashcomplete.rst+9 added-10 removed
73 unmodified lines
74is what you would need to put into your ``.bashrc``::7576 eval "$(_FOO_BAR_COMPLETE=source foo-bar)"7778From this point onwards, your script will have Bash completion enabled.78For zsh users add this to your ``.zshrc``::7980 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"8182From this point onwards, your script will have autocompletion enabled.8384Activation Script85-----------------8687The above activation example will always invoke your application on
5 unmodified lines
93This can be easily accomplished::9495 _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh9693And then you would put this into your bashrc instead::97For zsh:9895 . /path/to/foo-bar-complete.sh99 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh10097Zsh Compatibility98-----------------101And then you would put this into your .bashrc or .zshrc instead::102100To enable Bash completion in Zsh, add the following lines to your .zshrc:103 . /path/to/foo-bar-complete.sh104102 autoload bashcompinit103 bashcompinit105105See https://github.com/pallets/click/issues/323 for more information on106this issue.