zsh-users
 help / Atom feed
* Local inner functions in a prompt
@ 2018-12-21 22:13 Eric Nielsen
  2018-12-21 22:48 ` Mikael Magnusson
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Nielsen @ 2018-12-21 22:13 UTC (permalink / raw)
  To: Zsh Users

Hi all,

I'm aware of https://www.zsh.org/mla/users/2011/msg00207.html and that there's no such thing as an inner function.

But I'm surprised to see that if I declare "inner" functions inside a prompt function, they are not visible from "outside".

To reproduce this:

% cat <<'EOF' >prompt_outer_setup
prompt_outer_main() {
  local -i foo=1
  increment_foo() {
    (( foo++ ))
  }
  print_foo() {
    print -n ${foo}
  }
  inner1() {
    increment_foo
  }
  inner2() {
    increment_foo
  }
  inner1
  inner2
  print_foo
}
prompt_outer_setup() {
  PS1='$(prompt_outer_main)%# '
}
prompt_outer_setup
EOF
% fpath+=(${PWD})
% autoload -Uz promptinit && promptinit
% prompt outer
3%  print ${+functions[prompt_outer_setup]}
1
3%  print ${+functions[prompt_outer_main]}
1
3%  print ${+functions[increment_foo]}
0
3%  print ${+functions[print_foo]}
0
3%  print ${+functions[inner1]}
0
3%  print ${+functions[inner2]}
0
3%

As you can see only the "outer" functions prompt_outer_setup and prompt_outer_main are declared in the functions array. And the "inner" functions can access the local variable from the "outer" function, and an "inner" function can call other "inner" functions.

Is this scenario causing an unexpected behavior in Zsh, or is it really possible to create "inner" functions for prompts?

Kind regards,
Eric

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Local inner functions in a prompt
  2018-12-21 22:13 Local inner functions in a prompt Eric Nielsen
@ 2018-12-21 22:48 ` Mikael Magnusson
  2018-12-21 23:13   ` Eric Nielsen
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Magnusson @ 2018-12-21 22:48 UTC (permalink / raw)
  To: Eric Nielsen; +Cc: Zsh Users

On 12/21/18, Eric Nielsen <eric@amalgamar.com.br> wrote:
> Hi all,
>
> I'm aware of https://www.zsh.org/mla/users/2011/msg00207.html and that
> there's no such thing as an inner function.
>
> But I'm surprised to see that if I declare "inner" functions inside a prompt
> function, they are not visible from "outside".
> prompt_outer_setup() {
>   PS1='$(prompt_outer_main)%# '
> }
> prompt_outer_setup

You're running the function in a subshell here.

-- 
Mikael Magnusson

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Local inner functions in a prompt
  2018-12-21 22:48 ` Mikael Magnusson
@ 2018-12-21 23:13   ` Eric Nielsen
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Nielsen @ 2018-12-21 23:13 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh Users


Hi Mikael,
 
> On 12/21/18, Eric Nielsen <eric@amalgamar.com.br> wrote:
>> Hi all,
>>
>> I'm aware of https://www.zsh.org/mla/users/2011/msg00207.html and that
>> there's no such thing as an inner function.
>>
>> But I'm surprised to see that if I declare "inner" functions inside a prompt
>> function, they are not visible from "outside".
>> prompt_outer_setup() {
>>   PS1='$(prompt_outer_main)%# '
>> }
>> prompt_outer_setup
>
> You're running the function in a subshell here.

Oh, of course! (facepalm)

Do you recommend a better way to have a similar structure (one function that is called inside the prompt, that needs to call other functions), or my code structure is completely fine?

Thanks,
Eric

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-21 22:13 Local inner functions in a prompt Eric Nielsen
2018-12-21 22:48 ` Mikael Magnusson
2018-12-21 23:13   ` Eric Nielsen

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/ public-inbox