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

Re: A way to untie -T vars?



On Tue, Jan 24, 2023 at 1:56 AM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> On Tue, Jan 24, 2023 at 6:45 AM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> >
> > I've reached the conclusion that "local" makes no difference here.
>
> I actually knew that! There is no way to remove a parameter from a
> scope. Once a name is added to a scope, it stays there forever

Turns out that such a name also retains its justification settings
when unset, so there's no need to resort to typeset +m to reassert
them.

On Tue, Jan 24, 2023 at 9:55 AM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> If I were writing this code, I would change it without computing
> probabilities because it feels wrong to me.

Given the foregoing about justification there are a number of things
about the previously-attached "untie" that can be streamlined, so this
mostly became a non-issue.

In this replacement variant, the readonly|special branch could become
a fatal error (return 1) if that's preferred.
untie () {
  emulate -L zsh -o extendedglob -o errreturn
  while ((ARGC))
  do
    () {
      case -${(tP)1}- in
        (*-(readonly|special)-*) print -u2 -r "Can't untie ${(qqq)1}: ${(tP)1}"
          return 0 ;;
        (*-tied-*) set -- ${(s:-:tP)1} - $1 -g ;;
        (*) : "${1}: not a tied parameter"
          return 0 ;;
      esac
      while [[ $1 != - ]]
      do
        case $1 in
          (export) set -- "$@" -x ;;
          (tag) set -- "$@" -t ;;
          (unique) set -- "$@" -U ;;
          (upper) set -- "$@" -u ;;
          (lower) set -- "$@" -l ;;
          (hide) set -- "$@" -h ;;
          (hideval) set -- "$@" -H ;;
          (left) set -- "$@" -L ;;
          (right_blanks) set -- "$@" -R ;;
          (right_zeros) set -- "$@" -Z ;;
        esac
        shift
      done
      shift
      case -${(tP)1}- in
        (*-array-*) set -- $# "$@" $1 "${(@P)1}"
          unset $2
          typeset -a ${argv[3,$1+1]} $2
          shift $1+1
          set -A "$@" ;;
        (*-scalar-*) set -- "$@" $1=${(P)1}
          unset $1
          shift
          typeset "$@" ;;
        (*) print -u2 -r -- "${1}: impossible: ${(tP)1}"
          return 1 ;;
      esac
    } $1
    shift
  done
}


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