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)



On Fri, Dec 9, 2022 at 4:46 AM Philippe Altherr
<philippe.altherr@xxxxxxxxx> wrote:
>
> I just stumbled on the module zsh/param/private, which makes it possible (but I won't claim that it's very practical):
>[...]
> 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.

This isn't exactly an effect of private, it's the previously observed
effect that there is no way to "unlocal" a parameter in the current
scope, so you have to add it to the desired scope before declaring it
local.

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

You don't actually need a named function or print -v here, although I
guess "print -v" is shorter to write than "typeset -g".

curpos() {
    set -o localoptions -o extendedglob
    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['(<->)';'(<->) ]] &&
        () { typeset -g "$1"="$2" } ${1-x} "$match[2]" &&
        () { typeset -g "$1"="$2" } ${2-y} "$match[1]"
}

I'm torn about that eval-as-delcarator.  My first thought would be to use

  typeset -gi ${1-x} ${2-y}

but the eval avoids changing any declared flags on existing parameters
in the enclosing scope.  On the other hand both that eval and the use
of "print -v" force the variables to be scalar, whereas "typeset -g"
throws errors on assignment mismatch.




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