* namespace completions ? @ 2014-02-13 10:03 Marc Chantreux 2014-02-14 23:55 ` Oliver Kiddle 0 siblings, 1 reply; 9+ messages in thread From: Marc Chantreux @ 2014-02-13 10:03 UTC (permalink / raw) To: zsh-users hello zsh users, as our .zshenv grown separately and became redundant in some points, i made a proposal to my corworkers to easily share and collaborate on zsh codes. month later, we're happy users of the 'uze' convention: https://github.com/eiro/uze. but we want more! we want completion: saying https://github.com/eiro/uze is set and ready, i would like to write this from the CLI: mc@machine.head> a=unistra/annuaire mc@machine.head> . uze/$a mc@machine.head> $a/<tab> then have the completion with my namespace completed. so according to https://github.com/eiro/uze/blob/master/unistra/annuaire, the possible completions would be $a/search $a/filter/cards $a/filter/emails $a/cards $a/emails can someone help me ? regards -- Marc Chantreux (eiro on github and freenode) http://eiro.github.com/ http://eiro.github.com/atom.xml "Don't believe everything you read on the Internet" -- Abraham Lincoln ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-13 10:03 namespace completions ? Marc Chantreux @ 2014-02-14 23:55 ` Oliver Kiddle 2014-02-15 2:27 ` Bart Schaefer 2014-02-15 10:23 ` Marc Chantreux 0 siblings, 2 replies; 9+ messages in thread From: Oliver Kiddle @ 2014-02-14 23:55 UTC (permalink / raw) To: Marc Chantreux; +Cc: zsh-users Marc Chantreux wrote: > > month later, we're happy users of the 'uze' convention: > https://github.com/eiro/uze. but we want more! we want completion: It's a pity that isn't using function autoloading. It had never occurred to me before but it apparently works to autoload functions with slashes in their names and have them picked up out of subdirectories of your function path. > saying https://github.com/eiro/uze is set and ready, i would like to > write this from the CLI: > > mc@machine.head> a=unistra/annuaire > mc@machine.head> . uze/$a > mc@machine.head> $a/<tab> So basically, you want completion of shell functions to handle the beginning of the function being a variable reference in much the same way as is done for filename completion. By copying parts of _path_files to _functions, this can be done by modifying _functions as in the patch below. I'm not sure if it makes sense to commit this or some variant of it. Any thoughts? Having borrowed from _path_files, it relies on / to mark the end of the variable expression. Oliver diff --git a/Completion/Zsh/Type/_command_names b/Completion/Zsh/Type/_command_names index d9fc62d..31cebb0 100644 --- a/Completion/Zsh/Type/_command_names +++ b/Completion/Zsh/Type/_command_names @@ -8,10 +8,6 @@ local args defs ffilt zstyle -t ":completion:${curcontext}:commands" rehash && rehash -zstyle -t ":completion:${curcontext}:functions" prefix-needed && \ - [[ $PREFIX != [_.]* ]] && \ - ffilt='[(I)[^_.]*]' - defs=( 'commands:external command:_path_commands' ) @@ -28,7 +24,7 @@ else defs=( "$defs[@]" 'builtins:builtin command:compadd -Qk builtins' - "functions:shell function:compadd -k 'functions$ffilt'" + "functions:shell function:_functions" 'aliases:alias:compadd -Qk aliases' 'suffix-aliases:suffix alias:_suffix_alias_files' 'reserved-words:reserved word:compadd -Qk reswords' diff --git a/Completion/Zsh/Type/_functions b/Completion/Zsh/Type/_functions index 4d33669..0f4b5c6 100644 --- a/Completion/Zsh/Type/_functions +++ b/Completion/Zsh/Type/_functions @@ -1,9 +1,24 @@ #compdef unfunction -local expl ffilt +local expl funcs +local ffilt=functions zstyle -t ":completion:${curcontext}:functions" prefix-needed && \ [[ $PREFIX != [_.]* ]] && \ - ffilt='[(I)[^_.]*]' + ffilt+='[(I)[^_.]*]' -_wanted functions expl 'shell function' compadd -k "$@" - "functions$ffilt" +funcs=( ${(kP)ffilt} ) +if [[ "$PREFIX" = \$*/* && "$compstate[quote]" != \' ]]; then + local realstr + local linestr="${(M)PREFIX##*\$[^/]##/}" + function { + # do not treat an unset parameter expansion as the empty string + setopt localoptions nounset + eval 'realstr=${(e)~linestr}' 2>/dev/null + } + [[ -z "$realstr" || "$realstr" = "$linestr" ]] && return 1 + funcs=( ${${(M)funcs:#$realstr*}##$realstr} ) + compset -P $linestr +fi + +_wanted functions expl 'shell function' compadd -a "$@" - funcs ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-14 23:55 ` Oliver Kiddle @ 2014-02-15 2:27 ` Bart Schaefer 2014-02-15 10:16 ` Marc Chantreux 2014-02-15 10:23 ` Marc Chantreux 1 sibling, 1 reply; 9+ messages in thread From: Bart Schaefer @ 2014-02-15 2:27 UTC (permalink / raw) To: zsh-users On Feb 15, 12:55am, Oliver Kiddle wrote: } } So basically, you want completion of shell functions to handle the } beginning of the function being a variable reference in much the same } way as is done for filename completion. By copying parts of _path_files } to _functions, this can be done by modifying _functions as in the patch } below. It might be sufficient for uze's purposes to try: _expand_command() { _expand_word || _autocd } compdef _expand_command -command- zstyle :completion:expand-word:expand:-command-:: tag-order all-expansions zstyle :completion:expand-word:expand:-command-:: suffix false Which contrary to how it reads means to allow parameters to expand in the command position even when the word containing them has a suffix. It's not quite the same but it might be enough. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-15 2:27 ` Bart Schaefer @ 2014-02-15 10:16 ` Marc Chantreux 2014-02-15 22:50 ` Bart Schaefer 0 siblings, 1 reply; 9+ messages in thread From: Marc Chantreux @ 2014-02-15 10:16 UTC (permalink / raw) To: Bart Schaefer; +Cc: zsh-users hello Bart, > _expand_command() { _expand_word || _autocd } > compdef _expand_command -command- > zstyle :completion:expand-word:expand:-command-:: tag-order all-expansions > zstyle :completion:expand-word:expand:-command-:: suffix false as always when it comes to completions: http://www.quickmeme.com/img/d6/d6a1143f571184db25f94613edd43b40af6d3a629221aba00d9efdcfef5efd84.jpg but yeah: i added it in my .zshrc > It's not quite the same but it might be enough. yes it is! thank you :) -- Marc Chantreux (eiro on github and freenode) http://eiro.github.com/ http://eiro.github.com/atom.xml "Don't believe everything you read on the Internet" -- Abraham Lincoln ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-15 10:16 ` Marc Chantreux @ 2014-02-15 22:50 ` Bart Schaefer 2014-02-17 16:48 ` Marc Chantreux 0 siblings, 1 reply; 9+ messages in thread From: Bart Schaefer @ 2014-02-15 22:50 UTC (permalink / raw) To: zsh-users On Feb 15, 11:16am, Marc Chantreux wrote: } } > _expand_command() { _expand_word || _autocd } } > compdef _expand_command -command- } > zstyle :completion:expand-word:expand:-command-:: tag-order all-expansions } > zstyle :completion:expand-word:expand:-command-:: suffix false } } as always when it comes to completions: } } http://www.quickmeme.com/img/d6/d6a1143f571184db25f94613edd43b40af6d3a629221aba00d9efdcfef5efd84.jpg Let's break it down then ... The compdef function accepts a completion function name and a context name. Normally the context is a command, but it may also be one of a number of special contexts, which for stream-of-consciousness reasons are described in the manual in the "Autoloaded files" section under "Completion System". One of those contexts is "-command-" which means you're completing the command name itself (so you can't use that name to look up a completion for its arguments). As it happens, the default completion function for -command- context is _autocd. It'd be nice if "compdef" had an option to display this but in the meantime you have to know to examine $_comps[-command-] to find this out. We know that what we want is to expand a $var reference in the command context, but _autocd won't do that. Fortunately there is a provided function _expand_word that can do it. So we create a new function: _expand_command() { _expand_word || _autocd } This just says that if _expand_word doesn't work, try the original default of _autocd. And then we replace the function for -command- with our new one: compdef _expand_command -command- This is cheating a little; the _expand_word function is supposed to be bound to a widget, but it happens to work to call recursively into the completion system at this point. This won't work with all such widget functions, I guessed that this one would and was rewarded. The _expand_word function sets up its own zstyle context that has the substring "expand-word" (again this is supposed to be so that you can tell it was a different widget, but we cheat a little). It also call the _expand completer which sets the "expand" substring. So you end up with a context that looks like: :completion:expand-word:expand:-command-:: (Aside: If all you do is add the _expand completer to your completers zstyle, the context is never more specific than :completion::expand::: so you can't restrict this behavior to the -command- context.) So we need to tell completion that in that context we want parameters to expand everywhere. The name of the style controlling that is "suffix" which really means "peform expansion only in the suffix, not in any prefix". So to get expansion to happen in the prefix: zstyle :completion:expand-word:expand:-command-:: suffix false However, that will return two completions, the expansion of $var and the original unexpanded string. In this particular case we don't want to enter menu completion on those two options, we just want to fail if there is no expansion. That's controlled by the tag-order style, which controls both the ordering and the inclusion of tags. "Tags" are what the completion system calls the various categories into which the possible completions may be divided. This example has two useful tags, "all-expansions" and "original". (There is also an "expansions" tag, but it doesn't do anything if the all-expansions tag is omitted.) If we exclude "original" by leaving it out of the tag-order zstyle, _expand_word returns with a single completion that is the expansion of $var OR a failure that falls through to the _autocd default. zstyle :completion:expand-word:expand:-command-:: tag-order \ all-expansions I skipped over how you find out what the tags are. Normally you'd just put the cursor in the command word and type ctrl+x h to run _complete_help. In this case, though, that sails past _expand_word and gives only the tags for _autocd. To actually see the tags for _expand_word you have to start by clearing the completer style (see my aside, above) and adding: zstyle \* suffix false Otherwise _expand aborts early, without generating any of its tags for _complete_help to find. (This is a case where NOBODY has any idea what they're doing, I sort of rediscovered it by accident and then only showed you the answer.) Anyway, ctrl+x h now says: tags in context :completion:expand-word:expand:-command-:: all-expansions expansions original (_expand) Ahh, better; now we know both the specific style to use to replace the \* and also what tags to use in tag-order. And we're done. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-15 22:50 ` Bart Schaefer @ 2014-02-17 16:48 ` Marc Chantreux 2014-02-17 17:37 ` Peter Stephenson 0 siblings, 1 reply; 9+ messages in thread From: Marc Chantreux @ 2014-02-17 16:48 UTC (permalink / raw) To: Bart Schaefer; +Cc: zsh-users hello Bart and thank you so much for your patience. * once again, i think i got the big picture. * once again, got to read the doc and experiment * once again, i have no time for it i use zsh for more than 10 years and am now very opinionated about what is writting a good zsh script but i started the documentation about completion regulary to give up after few chapter, letting me live in frustration. i will show off and ask questions next time. regards -- Marc Chantreux (eiro on github and freenode) http://eiro.github.com/ http://eiro.github.com/atom.xml "Don't believe everything you read on the Internet" -- Abraham Lincoln ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-17 16:48 ` Marc Chantreux @ 2014-02-17 17:37 ` Peter Stephenson 2014-02-18 9:31 ` Marc Chantreux 0 siblings, 1 reply; 9+ messages in thread From: Peter Stephenson @ 2014-02-17 17:37 UTC (permalink / raw) To: zsh-users On Mon, 17 Feb 2014 17:48:28 +0100 Marc Chantreux <khatar@phear.org> wrote: > hello Bart and thank you so much for your patience. > > * once again, i think i got the big picture. > * once again, got to read the doc and experiment > * once again, i have no time for it > > i use zsh for more than 10 years and am now very opinionated about what > is writting a good zsh script but i started the documentation about > completion regulary to give up after few chapter, letting me live in > frustration. For something written to be readable, you might want to look at Oliver's chapters towards the end of From Bash to Zsh: see http://www.bash2zsh.com/ . pws ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-17 17:37 ` Peter Stephenson @ 2014-02-18 9:31 ` Marc Chantreux 0 siblings, 0 replies; 9+ messages in thread From: Marc Chantreux @ 2014-02-18 9:31 UTC (permalink / raw) To: Peter Stephenson; +Cc: zsh-users > For something written to be readable, you might want to look at Oliver's > chapters towards the end of From Bash to Zsh: see > http://www.bash2zsh.com/ . thanks for the tips: i saw a link to a tutorial there and pushed it in my "#readlater" list. regards -- Marc Chantreux (eiro on github and freenode) http://eiro.github.com/ http://eiro.github.com/atom.xml "Don't believe everything you read on the Internet" -- Abraham Lincoln ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: namespace completions ? 2014-02-14 23:55 ` Oliver Kiddle 2014-02-15 2:27 ` Bart Schaefer @ 2014-02-15 10:23 ` Marc Chantreux 1 sibling, 0 replies; 9+ messages in thread From: Marc Chantreux @ 2014-02-15 10:23 UTC (permalink / raw) To: Oliver Kiddle; +Cc: zsh-users On Sat, Feb 15, 2014 at 12:55:00AM +0100, Oliver Kiddle wrote: > It's a pity that isn't using function autoloading. It had never occurred > to me before but it apparently works to autoload functions with slashes > in their names and have them picked up out of subdirectories of your > function path. well. * i want my functions to be in the same file * some stuff isn't autoloadable, see https://github.com/eiro/uze/blob/master/unistra/annuaire#L34 > > saying https://github.com/eiro/uze is set and ready, i would like to > > write this from the CLI: > > > > mc@machine.head> a=unistra/annuaire > > mc@machine.head> . uze/$a > > mc@machine.head> $a/<tab> > > So basically, you want completion of shell functions to handle the > ... i started with the Bart's answer (simplier) and it works. thank you! regards -- Marc Chantreux (eiro on github and freenode) http://eiro.github.com/ http://eiro.github.com/atom.xml "Don't believe everything you read on the Internet" -- Abraham Lincoln ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2014-02-18 9:40 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2014-02-13 10:03 namespace completions ? Marc Chantreux 2014-02-14 23:55 ` Oliver Kiddle 2014-02-15 2:27 ` Bart Schaefer 2014-02-15 10:16 ` Marc Chantreux 2014-02-15 22:50 ` Bart Schaefer 2014-02-17 16:48 ` Marc Chantreux 2014-02-17 17:37 ` Peter Stephenson 2014-02-18 9:31 ` Marc Chantreux 2014-02-15 10:23 ` Marc Chantreux
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/zsh/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).