From: Eric Nielsen <eric@amalgamar.com.br>
To: Zsh Users <zsh-users@zsh.org>
Subject: Local inner functions in a prompt
Date: Fri, 21 Dec 2018 22:13:57 +0000 [thread overview]
Message-ID: <SC1P152MB175887940F318B2BD39AAE89EFB80@SC1P152MB1758.LAMP152.PROD.OUTLOOK.COM> (raw)
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
next reply other threads:[~2018-12-21 22:14 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-21 22:13 Eric Nielsen [this message]
2018-12-21 22:48 ` Mikael Magnusson
2018-12-21 23:13 ` Eric Nielsen
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=SC1P152MB175887940F318B2BD39AAE89EFB80@SC1P152MB1758.LAMP152.PROD.OUTLOOK.COM \
--to=eric@amalgamar.com.br \
--cc=zsh-users@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).