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

Re: A way to untie -T vars?



On Mon, Jan 23, 2023 at 10:27 AM Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> array local left justified 10 zero filled 10 uppercase tagged unique
> tied FOO foo
>
> (plus export, hide, and hideval, which typeset +m does not display)

Minor correction:  typeset +m does output "exported" for scalars, but
not for arrays even when tied to an exported scalar.

> Pondering.

I've reached the conclusion that "local" makes no difference here.  If
any calling scope has declared the parameter local, it will remain
local to that scope even if unset, and the dynamic scope behavior of
"typeset -g" from inside "untie" will apply to the variable in that
scope.

Also, per the other thread, there's no way to get the justification
width for -L/-R/-Z without using typeset +m so that's what I've done.
Since -L and -Z may be specified together, I've made two calls to
typeset +m, the gyrations to reduce it to a single call seemed even
more confusing than the hoops necessary to shuffle all the switches
through the positional parameters.

Attached this time because I'm sure gmail would maim it.  This has
been tested with convoluted cases, but not with simple ones, so it may
still need refinement.
untie () {
  emulate -L zsh -o extendedglob -o errreturn
  while ((ARGC))
  do
    () {
      case ${(tP)1} in
        (*right_zeros*) set -- $1 -Z${${(M@)$(typeset +m $1):#[[:digit:]]#}[1]} ;;
      esac
      case ${(tP)1} in
        (*left*) set -- "$@" -L${${(M@)$(typeset +m $1):#[[:digit:]]#}[1]} ;;
        (*right_blanks*) set -- "$@" -R${${(M@)$(typeset +m $1):#[[:digit:]]#}[1]} ;;
      esac
      if [[ ${(tP)1} = *export* ]]
      then
        set -- $1 -x ${argv[2,$#]}
      else
        set -- $1 -g ${argv[2,$#]}
      fi
      set ${(s:-:tP)1} - $1 ${argv[2,$#]}
      while [[ $1 != - ]]
      do
        case $1 in
          (tag) set -- "$@" -t ;;
          (unique) set -- "$@" -U ;;
          (upper) set -- "$@" -u ;;
          (lower) set -- "$@" -l ;;
          (hide) set -- "$@" -h ;;
          (hideval) set -- "$@" -H ;;
        esac
        shift
      done
      shift
      case ${(tP)1} in
        (array*tied*) set -- $1 $# "$@" $1 "${(@P)1}"
          unset $1
          typeset -a ${argv[4,$2+2]} $1
          shift $2+2
          set -A "$@" ;;
        (scalar*tied*) set -- "$@" $1=${(P)1}
          unset $1
          shift
          typeset "$@" ;;
        (*tied*) print -u2 -r "Can't untie ${(qqq)1}: ${(tP)1}" ;;
        (*) : "${1}: not a tied parameter" ;;
      esac
    } $1
    shift
  done
}


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