zsh-users
 help / color / mirror / code / Atom feed
* Caching variables during completion
@ 2013-02-13  9:44 Jan Eike von Seggern
  2013-02-13 17:39 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Eike von Seggern @ 2013-02-13  9:44 UTC (permalink / raw)
  To: zsh-users

Hello,

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?

Explanation:
I'm trying to improve the completion for xrandr (mainly screen-scraping 
the available outputs and associated modes). However, my first try 
involved screen-scraping the output of `xrandr -q` every time a 
completion was made, which was slow. I then resorted to cache the 
available output/modes in a global variable. But this fails if a new 
output gets available.

Best,
Eike


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

* Re: Caching variables during completion
  2013-02-13  9:44 Caching variables during completion Jan Eike von Seggern
@ 2013-02-13 17:39 ` Bart Schaefer
  2013-02-13 20:30   ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2013-02-13 17:39 UTC (permalink / raw)
  To: zsh-users

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.

Crudely:

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


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

* Re: Caching variables during completion
  2013-02-13 17:39 ` Bart Schaefer
@ 2013-02-13 20:30   ` Peter Stephenson
  2013-02-14  0:39     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2013-02-13 20:30 UTC (permalink / raw)
  To: zsh-users

On Wed, 13 Feb 2013 09:39:24 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> Crudely:
> 
> _xrcache() {
>   if (( $_xr_HISTNO != $HISTNO ))
>   then
>     _xr_HISTNO=$HISTNO
>     _xr_output=$(xrandr -q)
>   fi
>   return 1 # always "fail" so other completers are tried
> }
> zstyle ':completion:*' completer _xrcache _oldlist _expand _complete # etc.

I don't see why you couldn't put that inside the completion function
that needs the cache, so you don't need anything special with
completers:

_xrandr() {                               # except this is the file _xrandr
   if (( $_xr_HISTNO != $HISTNO ))
   then
     _xr_HISTNO=$HISTNO
     _xr_output=$(xrandr -q)
   fi
 
   # use $_xr_output here
}

pws


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

* Re: Caching variables during completion
  2013-02-13 20:30   ` Peter Stephenson
@ 2013-02-14  0:39     ` Bart Schaefer
  2013-02-14  8:38       ` Jan Eike von Seggern
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2013-02-14  0:39 UTC (permalink / raw)
  To: zsh-users

On Feb 13,  8:30pm, Peter Stephenson wrote:
}
} Bart Schaefer <schaefer@brasslantern.com> wrote:
} > zstyle ':completion:*' completer _xrcache _oldlist _expand _complete # etc.
} 
} I don't see why you couldn't put that inside the completion function
} that needs the cache

True, you could put it in the completion function, but what if there's
more than one completion function that wants to share the same cache?

Which reminds me that I've never really bothered to understand how the
_store_cache/_retrieve_cache stuff is supposed to work.  I suspect it
would be overkill here.


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

* Re: Caching variables during completion
  2013-02-14  0:39     ` Bart Schaefer
@ 2013-02-14  8:38       ` Jan Eike von Seggern
  2013-03-18 17:34         ` xrandr completion (was Re: Caching variables during completion) Jan Eike von Seggern
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Eike von Seggern @ 2013-02-14  8:38 UTC (permalink / raw)
  To: zsh-users

Thanks a lot Bart and Peter!

Using HISTNO should be working.

BTW: I played around with _store_cache/_retrieve_cache but I could not 
get it working because I didn't now how to retrieve an associated array 
from the cache (not to mention that it's not fitting my need anyways).

Best
Eike


On 02/14/2013 01:39 AM, Bart Schaefer wrote:
> On Feb 13,  8:30pm, Peter Stephenson wrote:
> }
> } Bart Schaefer <schaefer@brasslantern.com> wrote:
> } > zstyle ':completion:*' completer _xrcache _oldlist _expand _complete # etc.
> }
> } I don't see why you couldn't put that inside the completion function
> } that needs the cache
>
> True, you could put it in the completion function, but what if there's
> more than one completion function that wants to share the same cache?
>
> Which reminds me that I've never really bothered to understand how the
> _store_cache/_retrieve_cache stuff is supposed to work.  I suspect it
> would be overkill here.
>


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

* xrandr completion (was Re: Caching variables during completion)
  2013-02-14  8:38       ` Jan Eike von Seggern
@ 2013-03-18 17:34         ` Jan Eike von Seggern
  2013-03-18 21:14           ` Oliver Kiddle
  0 siblings, 1 reply; 7+ messages in thread
From: Jan Eike von Seggern @ 2013-03-18 17:34 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1193 bytes --]

Hi,

attached is my slightly improved completion for xrandr:

It completes only connected outputs and only the available modes for 
each output.

Best
Eike


On 02/14/2013 09:38 AM, Jan Eike von Seggern wrote:
> Thanks a lot Bart and Peter!
>
> Using HISTNO should be working.
>
> BTW: I played around with _store_cache/_retrieve_cache but I could not
> get it working because I didn't now how to retrieve an associated array
> from the cache (not to mention that it's not fitting my need anyways).
>
> Best
> Eike
>
>
> On 02/14/2013 01:39 AM, Bart Schaefer wrote:
>> On Feb 13,  8:30pm, Peter Stephenson wrote:
>> }
>> } Bart Schaefer <schaefer@brasslantern.com> wrote:
>> } > zstyle ':completion:*' completer _xrcache _oldlist _expand
>> _complete # etc.
>> }
>> } I don't see why you couldn't put that inside the completion function
>> } that needs the cache
>>
>> True, you could put it in the completion function, but what if there's
>> more than one completion function that wants to share the same cache?
>>
>> Which reminds me that I've never really bothered to understand how the
>> _store_cache/_retrieve_cache stuff is supposed to work.  I suspect it
>> would be overkill here.
>>

[-- Attachment #2: _xrandr --]
[-- Type: text/plain, Size: 3544 bytes --]

#compdef xrandr
local context state state_descr line
typeset -A opt_args
local expl
local outputs

# TODO: This creates a global variable to cache the available outputs. 
if [[ $_xr_HISTNO != $HISTNO ]];
then
  _xr_HISTNO=$HISTNO
  typeset -gA _xr_modes 
  _xr_modes=()
  local last_output l
  for l in ${(f)"$(xrandr -q)"}; do
    l=${(z)l}
    if [[ $l[2] == "connected" ]]; then
      last_output=$l[1]
    elif [[ $l[2] == "disconnected" ]]; then
      last_output=""
    elif [[ -n $last_output ]]; then
      _xr_modes[$last_output]="$_xr_modes[$last_output]${_xr_modes[$last_output]+ }$l[1]"
    fi
  done
fi
outputs=${(k)_xr_modes}

_arguments \
  '(-d -display)'{-d,-display}'[X display to use]:X display:_x_display' \
  '-help[display help]' \
  '(-o --orientation)'{-o,--orientation}'[orientation of screen]:rotation:(normal inverted left right 0 1 2 3)' \
  '(-q --query)'{-q,--query}'[display current state]' \
  '(-s --size)'{-s,--size}'[set screen size]:size:' \
  '(-r --rate --refresh)'{*-r,*--rate,*--refresh}'[set refresh rate]:target refresh rate:' \
  '(-v --version)'{-v,--version}'[display version]' \
  '-x[reflect across X axis]' \
  '-y[reflect across Y axis]' \
  '--screen:X screen number' \
  '--verbose[be more verbose]' \
  '--dryrun[make no changes]' \
  "--nograb[don\'t grab screen]" \
  '(--prop --properties)'{--prop,--properties}'[display the contents of properties for each output]' \
  '--fb[configure screen size]:size:' \
  '--fbmm[configure physical screen size]:size:' \
  '--dpi[configure DPI]:dpi:' \
  "*--output[configure output]:output to reconfigure:($outputs)" \
  '*--auto[select preferred mode]' \
  "*--mode[select mode for output]:output mode:->mode" \
  '*--preferred[select preferred mode]' \
  '*--pos[position output]:position:' \
  '*--reflect[reflect across axes]:axes:(normal x y xy)' \
  '*--rotate[rotate output]:rotation:(normal inverted left right)' \
  "*--left-of[position output]:relative position to:($outputs)" \
  "*--right-of[position output]:relative position to:($outputs)" \
  "*--above[position output]:relative position to:($outputs)" \
  "*--below[position output]:relative position to:($outputs)" \
  "*--same-as[position output]:relative position to:($outputs)" \
  '*--set[set property]:property:(Backlight scaling\ mode):value:->value' \
  '*--scale[scale output]:output scaling XxY:' \
  '*--transform[transform output]:transformation matrix:' \
  '*--off[disable the output]' \
  '*--crtc:crtc to use:' \
  '*--panning[enable panning]:panning:' \
  '*--gamma:r\:g\:b:' \
  '*--primary[select output as primary]' \
  '--noprimary' \
  '*--newmode[add new mode line]:name: :clock MHz: :hdisp: :hsync-start: :hsync-end: :htotal: :vdisp: :vsync-start: :vsync-end: :vtotal:' \
  '*--rmmode[remove mode line]:Mode name:' \
  "*--addmode[add new mode to output]:output:($outputs):name:" \
  "*--delmode[remove mode from output]:output:($outputs):name:" \
  && return 0

if [[ $state == value ]]; then
    case $words[CURRENT-1] in
	(scaling* mode)
	    _description value expl "output property 'scaling mode'"
	    compadd "$@" "$expl[@]" None Full Center Full\ aspect && return 0
	    ;;
    esac
elif [[ $state == mode ]]; then
    local i=1 last_output
    while (( CURRENT - i > 2 )); do
        if [[ $words[CURRENT-1-i] == "--output" ]]; then
            last_output=$words[CURRENT-i]
            break
        fi
        (( i += 1 ))
    done
    _description -V mode expl "output mode"
    compadd "$@" "$expl[@]" ${(z)_xr_modes[$last_output]} && return 0
fi

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

* Re: xrandr completion (was Re: Caching variables during completion)
  2013-03-18 17:34         ` xrandr completion (was Re: Caching variables during completion) Jan Eike von Seggern
@ 2013-03-18 21:14           ` Oliver Kiddle
  0 siblings, 0 replies; 7+ messages in thread
From: Oliver Kiddle @ 2013-03-18 21:14 UTC (permalink / raw)
  To: Jan Eike von Seggern; +Cc: zsh-users

Jan Eike von Seggern wrote:
> attached is my slightly improved completion for xrandr:
> 
> It completes only connected outputs and only the available modes for 
> each output.

This updated function appears to be based on the function from the last
release and not from the latest function in CVS. There's other
improvements already in the CVS version. If you start with the function
in CVS and post a patch rather than a full file then we don't risk
losing other people's changes.

Oliver


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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-13  9:44 Caching variables during completion Jan Eike von Seggern
2013-02-13 17:39 ` Bart Schaefer
2013-02-13 20:30   ` Peter Stephenson
2013-02-14  0:39     ` Bart Schaefer
2013-02-14  8:38       ` Jan Eike von Seggern
2013-03-18 17:34         ` xrandr completion (was Re: Caching variables during completion) Jan Eike von Seggern
2013-03-18 21:14           ` Oliver Kiddle

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