zsh-workers
 help / color / mirror / code / Atom feed
* trying to understand how the zsh completer works
@ 2013-02-28 23:38 joe M
  2013-03-01  5:56 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: joe M @ 2013-02-28 23:38 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-workers

Hello,

I am trying to tell the completer to use a different completer based on context.

In this situation, I am trying to tell it to use the _approximate
completer when completing the argument-rest context of grep.

zstyle -L
zstyle -L
zstyle ':completion:*' accept-exact-dirs true
zstyle ':completion:*:options' auto-description %d
zstyle :auto-fu:var autoable-function/skiplines '[[:blank:]\\\\\"'\'']*'
zstyle :auto-fu:var autoable-function/skipwords '[\\\\]*'
zstyle ':completion::complete:*' cache-path /home/j/var/zsh/zcompcache
zstyle :completion::complete:grep:argument-rest: completer _approximate
zstyle ':completion:hist-complete:*' completer _history
zstyle ':completion::*' completer _complete
zstyle :auto-fu:highlight completion 'bold,fg=blue'
zstyle :auto-fu:highlight completion/one 'fg=blue'
zstyle ':completion:*:options' description yes
zstyle :auto-fu:var disable magic-space
zstyle :auto-fu:var enable all
zstyle :auto-fu:var eof-installed-p yes
zstyle ':completion:*:corrections' format ' %F{green}-- %d (errors: %e) --%f'
zstyle ':completion:*:descriptions' format ' %F{yellow}-- %d --%f'
zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
zstyle ':completion:*' format ' %F{yellow}-- %d --%f'
zstyle ':completion:*:matches' group yes
zstyle ':completion:*' group-name ''
zstyle :auto-fu:highlight input
zstyle :auto-fu:var isearchmap-installed-p yes
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=*
r:|=*' 'l:|=* r:|=*'
zstyle ':completion:predict:*' menu yes
zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*' menu 'select=1'
zstyle :auto-fu:var misc-installed-p yes
zstyle :auto-fu:var postdisplay
zstyle :auto-fu:var preexec-installed-p yes
zstyle ':completion:*' show-completer true
zstyle ':completion::complete:*' use-cache on
zstyle ':completion:*' use-compctl false
zstyle :predict verbose true
zstyle ':completion:*' verbose yes
--(~/etc/zsh)----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(pts/12@master)--

--(~/etc/zsh)----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(pts/12@master)--
zstyle ':completion::*' completer _complete
zstyle ':completion::*' completer _complete
--(~/etc/zsh)----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(pts/12@master)--
zstyle ':completion::complete:grep:argument-rest:' completer  _approximate
zstyle ':completion::complete:grep:argument-rest:' completer _approximate
--(~/etc/zsh)----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(pts/12@master)--
grep test 44
tags in context :completion::complete:grep::
    argument-rest options  (_arguments _grep (eval))
tags in context :completion::complete:grep:argument-rest:
    all-files  (_files _arguments _grep (eval))

I do not see the approximate completer being used here.

Any thoughts on what I am missing please?

Thanks
Joe


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: trying to understand how the zsh completer works
  2013-02-28 23:38 trying to understand how the zsh completer works joe M
@ 2013-03-01  5:56 ` Bart Schaefer
  2013-03-03  6:09   ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2013-03-01  5:56 UTC (permalink / raw)
  To: zsh-workers

I'm wondering if this doesn't belong on zsh-users rather than -workers?

On Feb 28,  6:38pm, joe M wrote:
}
} I am trying to tell the completer to use a different completer based
} on context.
}
} In this situation, I am trying to tell it to use the _approximate
} completer when completing the argument-rest context of grep.

Unfortunately this is one of the things that mostly doesn't work.  The
set of completers is determined very early in the process when the
context is still very non-specific -- because it is the completers
themselves that supply the semantic analysis to fill out the rest of
the context.

Note that the definition of a the zstyle context for completion is

    :completion:FUNCTION:COMPLETER:COMMAND:ARGUMENT:tag

This roughly corresponds to how far down into the completion process
the current operation has progressed.  More detail is appended to the
context at each stage.

The current completer is the third component; by the time the context
has been built up to the fifth component, it's way too late for the
style "completer" to be meaningful.  It can only be usefully looked
up at the very beginning (:completion:) or during the first FUNCTION
(:completion:hist-complete: to use one of your examples).

In retrospect it might have been useful to split the context analysis
from the generation of matches, rather than having a single function
responsible for doing both parts for each command name.  On the other
hand that would have doubled the number of functions (or at least the
number of call signatures) and we'd probably never have gotten done
with the implementation. :-}

If you do put _approximate in the global list of completers, you can
see where it appears in the context -- see how it's "upstream" from
*:grep:argument-rest: ?

tags in context :completion::approximate:::
    corrections original  (_approximate)
tags in context :completion::approximate-1:grep::
    argument-rest options  (_arguments _grep (eval))
tags in context :completion::approximate-1:grep:argument-rest:
    globbed-files  (_files _arguments _grep (eval))
tags in context :completion::approximate-1:grep:options:
    options  (_describe _arguments _grep (eval))
tags in context :completion::complete:grep::
    argument-rest options  (_arguments _grep (eval))
tags in context :completion::complete:grep:argument-rest:
    globbed-files  (_files _arguments _grep (eval))

This means that _approximate is (indirectly) calling _grep, not the
other way around as you might expect.

The only workaround for this is to do your own context analysis and
embed that in the style value via "zstyle -e".  Something like:

zstyle -e ':completion:*' completer \
 'reply=(_complete); \
  (( $CURRENT > 2 )) && [[ $words[1] = grep ]] && reply+=(_approximate)'

Yes, this could get ugly quickly if you want to customize a lot of
completers for individual commands or word positions.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: trying to understand how the zsh completer works
  2013-03-01  5:56 ` Bart Schaefer
@ 2013-03-03  6:09   ` Bart Schaefer
  2013-03-03 15:56     ` joe M
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2013-03-03  6:09 UTC (permalink / raw)
  To: zsh-workers

On Feb 28,  9:56pm, Bart Schaefer wrote:
}
} In retrospect it might have been useful to split the context analysis
} from the generation of matches, rather than having a single function
} responsible for doing both parts for each command name.
} 
} The only workaround for this is to do your own context analysis and
} embed that in the style value via "zstyle -e".

... I had a mildy crazy idea that it should be possible to modify the
algorithm from _complete_help to create a function that could be called
from zstyle -e for the completer style.  This function would intuit the
contexts that would be used later when the completers themselves were
the callers, thereby allowing the list of completers to be modified for
the contexts they hadn't "really" created yet.

After working out some fairly convoluted recursion issues -- because
the completer style is being looked up in _main_complete, and then the
style evaluation wants to call _main_complete again to simulate what
the surrounding call in progess is going to do later -- I came up with
an example that generates all the deep contexts and successfully looks
up the value for the completer style in each of them.

Unfortunately, it still doesn't work, because to find the single most
specific context out of all the possible matching zstyle patterns is
equivalent to the zstyle internal algorithm for ordering patterns by
increasing specificity.  The simple ${(ok)...} used by _complete_help
is the wrong order nearly all the time.

If the preceding paragraph made sense to you, and you feel inclined
to implement order-by-context-specificity in _complete_help to replace
${(@ok)help_sfuncs} at around line 58, let me know what you come up
with.  Otherwise I may eventually get there.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: trying to understand how the zsh completer works
  2013-03-03  6:09   ` Bart Schaefer
@ 2013-03-03 15:56     ` joe M
  2013-03-03 18:05       ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: joe M @ 2013-03-03 15:56 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

Hello Bart,

> If the preceding paragraph made sense to you, and you feel inclined
> to implement order-by-context-specificity in _complete_help to replace
> ${(@ok)help_sfuncs} at around line 58, let me know what you come up
> with.  Otherwise I may eventually get there.

Thanks, I do not have the necessary skills to do what you did above.
But, I definitely can give it a try and test it out, if you get there.

Thanks again,
Joe


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: trying to understand how the zsh completer works
  2013-03-03 15:56     ` joe M
@ 2013-03-03 18:05       ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2013-03-03 18:05 UTC (permalink / raw)
  To: zsh-workers

On Mar 3, 10:56am, joe M wrote:
} 
} > If the preceding paragraph made sense to you, and you feel inclined
} > to implement order-by-context-specificity in _complete_help to replace
} > ${(@ok)help_sfuncs} at around line 58, let me know what you come up
} > with.  Otherwise I may eventually get there.
} 
} Thanks, I do not have the necessary skills to do what you did above.

The "you" in what I wrote was the generic zsh-workers "you", as in,
"Attention anyone two whom the paragraph made sense:  I welcome help."

Meanwhile, to Joe: Good luck with something simpler ...


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-03-03 18:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-28 23:38 trying to understand how the zsh completer works joe M
2013-03-01  5:56 ` Bart Schaefer
2013-03-03  6:09   ` Bart Schaefer
2013-03-03 15:56     ` joe M
2013-03-03 18:05       ` Bart Schaefer

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).