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

Re: Get cursor position (Was: [bug report] prompt can erase messages written on the terminal by background processes)



typeset -g "${1-x}=$match[2]" "${2-y}=$match[1]"

Doesn't work either. I guess there's no way to access variables
by the same name in that parent scope or to unlocal a variable?

I just stumbled on the module zsh/param/private, which makes it possible (but I won't claim that it's very practical):

assign() {
    print -v "$1" "$2"
}

curpos() {
    set -o localoptions -o extendedglob
    # Initialize the return variables. Without this, print -v fails if
    # the variable is the same as one of the private variables.
    eval "${1-x}= ${2-y}="
    zmodload zsh/param/private
    local -P -a match mbegin mend
    local -P answer
    IFS= read -rsdR -t0.2 answer$'?\e[6n' &&
        [[ $answer = (#b)$'\e['(<->)';'(<->) ]] &&
        assign ${1-x} "$match[2]" &&
        assign ${2-y} "$match[1]"
}

curpos answer match
echo x=$answer y=$match

It's a bit strange that the initialization is needed. Private variables don't hide global variables of the same name, nor prevent their assignment, but prevent their initial declaration.

Philippe



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