Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Caching variables during completion

On Feb 13, 10:44am, Jan Eike von Seggern wrote:
} my question first: Is it possible to set a variable only once during 
} every command-line completion, i.e. only after hitting <Tab> for the 
} first time and then keeping the contents for this specific command-line?

Depending on whether you mean all completions for the current command
line or just all repetitions of completion for the same word (e.g.,
cycling through a menu) there may be different approaches to this.
Within completion on a single word, you can look at the _oldlist
completer for an example.

Based on your additional explanation, though, I suspect that's not what
you're after, but the basic idea is still the same:  Create a function
which you reference at the beginning of the completer zstyle.  That
function tests (somehow) whether the cached state needs to be refreshed.

In your particular example, it should work to cache the value of $HISTNO
and then reload the cache if it has changed.


_xrcache() {
  if (( $_xr_HISTNO != $HISTNO ))
    _xr_output=$(xrandr -q)
  return 1 # always "fail" so other completers are tried
zstyle ':completion:*' completer _xrcache _oldlist _expand _complete # etc.

Messages sorted by: Reverse Date, Date, Thread, Author