zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Peter Stephenson <p.stephenson@samsung.com>
Cc: zsh workers <zsh-workers@zsh.org>
Subject: Re: [BUG] Can't mark unset variables as read-only
Date: Wed, 29 Apr 2015 13:01:10 +0200	[thread overview]
Message-ID: <CAHYJk3Q8gJWs=YKw7AtvqoyzGnn9Qbm2nFwLgzqBCnNwzmAQJQ@mail.gmail.com> (raw)
In-Reply-To: <CAHYJk3SPLgAQpzquwxypVbg2MsPu_39XHYZhYuJ-7obX5jSSNA@mail.gmail.com>

On Wed, Apr 29, 2015 at 12:57 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
> On Wed, Apr 29, 2015 at 12:36 PM, Peter Stephenson
> <p.stephenson@samsung.com> wrote:
>> On Wed, 29 Apr 2015 08:35:43 +0200
>> Martijn Dekker <martijn@inlv.org> wrote:
>>> Unlike other shells, zsh can't mark an unset variable as read-only.
>>
>> Yes, the standard does indeed require that ability.  As you can imagine,
>> something like this that completely breaks the normal programming model
>> of variables (a variable can't have state if it's unset because it
>> doesn't exist) is a nightmare to implement; however, it can at least be
>> limited to the POSIXBUILTINS option and maybe the cases using that are
>> simple enough that it mostly works.  I'm sure there are any number of
>> strange edge cases, though.
>>
>> The output of "readonly -p" is still broken (doesn't show the unset
>> variables in a fashion that can be used for restoring the current state)
>> but it was anyway:
>>
>>   typeset -ar '*'
>>   *=()
>>
>> Er, no, I don't think that's going to work.
>>
>> So that can be fixed separately.
>
> I wanted the opposite thing the other day, sort of. I have a ZLE
> widget that looks at $WIDGET, and I wanted to reuse it by calling it
> from another widget, setting WIDGET to another value first. However,
> ZLE makes $WIDGET readonly special, and I couldn't find any
> combination of flags that let me make a non-readonly local WIDGET. Is
> that possible?
>
> % zle -N h; h() { local -h WIDGET; WIDGET=hi; echo $WIDGET }; bindkey '^[h' h
> % <press alt-h>
> h: read-only variable: WIDGET
> % zle -N h; h() { local +r -h WIDGET; WIDGET=hi; echo $WIDGET }; bindkey '^[h' h
> % <press alt-h>
> h:local: WIDGET: can't change type of a special parameter
> % zle -N h; h() { local WIDGET; WIDGET=hi; echo $WIDGET }; bindkey '^[h' h
> % <press alt-h>WIDGET=h
>
> h: read-only variable: WIDGET
>
> compared to
> % () { readonly foo=3; () { local foo=5; echo $foo }; echo $foo }
> 5
> 3

Even when you think about something for a couple of days, you don't
realize what the answer is until after you send the question to a
public forum.

ZLE makes the scope of the WIDGET parameter start inside the widget
function already (which is confusing), so it is already local. The
answer is to start another parameter scope first,
% zle -N h; h() { () { local -h WIDGET; WIDGET=hi; echo $WIDGET } };
bindkey '^[h' h
% <press alt-h>hi

-- 
Mikael Magnusson


  reply	other threads:[~2015-04-29 11:01 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-29  6:35 Martijn Dekker
2015-04-29 10:36 ` Peter Stephenson
2015-04-29 10:57   ` Mikael Magnusson
2015-04-29 11:01     ` Mikael Magnusson [this message]
2015-04-30  0:08       ` PATCH: Don't define internal params directly in hook function scope Mikael Magnusson
2015-04-30  4:01         ` Bart Schaefer
2015-04-30  8:44           ` Peter Stephenson
2015-04-30 11:18             ` Peter Stephenson
2015-04-29 11:09     ` Local readonly specials (was: Can't mark unset variables as read-only) Peter Stephenson
2015-04-29 13:46   ` [BUG] Can't mark unset variables as read-only Martijn Dekker
2015-04-29 13:55   ` Bart Schaefer
2015-04-29 14:41     ` Peter Stephenson
2015-04-29 15:33       ` Chet Ramey
2015-04-29 19:09         ` Stephane Chazelas
2015-04-29 23:22           ` Chet Ramey
2015-04-30  3:57         ` Bart Schaefer
  -- strict thread matches above, loose matches on Subject: below --
2015-04-29  5:48 Martijn Dekker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHYJk3Q8gJWs=YKw7AtvqoyzGnn9Qbm2nFwLgzqBCnNwzmAQJQ@mail.gmail.com' \
    --to=mikachu@gmail.com \
    --cc=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).