From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Roman Perepelitsa <roman.perepelitsa@gmail.com>
Cc: Marlon Richert <marlon.richert@gmail.com>,
Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Bug report: `setopt noaliases` not respected in `local` statement without assignment.
Date: Thu, 26 Mar 2020 22:34:49 +0000 [thread overview]
Message-ID: <20200326223449.0a090ae6@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <CAN=4vMqxxC62wQD0eKCqd8kmOq09RxSAf0t+E7eThku9ngFO1A@mail.gmail.com>
Roman Perepelitsa wrote on Thu, 26 Mar 2020 11:38 +0100:
> On Thu, Mar 26, 2020 at 11:27 AM Marlon Richert
> <marlon.richert@gmail.com> wrote:
> >
> > Aha. So, if I want that aliases don't get expanded in the functions in https://github.com/junegunn/fzf/blob/master/shell/completion.zsh, then where should I put the `setopt noaliases` statement? Does it suffice to just put `emulate -L zsh; setopt localoptions noaliases;` at the top of the file? I don't want the noaliases option to leak into my own shell environment.
> >
> > Or is there a better solution possible here than using noaliases?
>
> The official solution to this problem is to use autoloadable functions
> instead of sourcing zsh scripts with function definitions.
>
Note that the -U flag should be passed to «autoload» in that case.
> One practical alternative is to add a bit of code at the top and at
> the bottom of completion.zsh. Like this:
>
> 'builtin' 'local' '-a' '_fzf_completion_opts'
> [[ ! -o 'aliases' ]] || _fzf_completion_opts+=('aliases')
> [[ ! -o 'sh_glob' ]] || _fzf_completion_opts+=('sh_glob')
> [[ ! -o 'no_brace_expand' ]] || _fzf_completion_opts+=('no_brace_expand')
> 'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand'
>
> # the meat of completion.zsh goes here
>
> (( ${#_fzf_completion_opts} )) && setopt ${_fzf_completion_opts[@]}
> 'builtin' 'unset' '_fzf_completion_opts'
>
z-sy-h does something similar with aliases:
https://github.com/zsh-users/zsh-syntax-highlighting/blob/d1802e388e94aca25380a3a9aeb4a2b7ba661b41/zsh-syntax-highlighting.zsh#L30-L36
⋮
https://github.com/zsh-users/zsh-syntax-highlighting/blob/d1802e388e94aca25380a3a9aeb4a2b7ba661b41/zsh-syntax-highlighting.zsh#L442-L444
For options, z-sy-h uses «emulate -L» for the vast majority of its
code. It's on our todo list (issue #688) to add a «emulate zsh -o …
-c 'source …'» shim around the entry point script, as Roman proposes
below.
> Note that this doesn't just disable aliases but also changes a couple
> other options that similarly affect parsing of functions. (You could
> also wrap the whole script in try-always to make sure options are
> restored even if evaluation of the script stops prematurely.)
>
There are a few other options to worry about, e.g., KSH_ARRAYS,
IGNORE_CLOES_BRACES.
> Another alternative is to rename completion.zsh to
> internal-completion.zsh and place this shim in place of the original
> completion.zsh:
>
> 'builtin' 'emulate' 'zsh' '-o' 'no_aliases' '-c' 'builtin source
> ${${(%):-%x}:h}/internal-completion.zsh'
>
> Roman.
>
Cheers,
Daniel
>
>
>
> > On Thu, 26 Mar 2020 at 11:59, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> >>
> >> On Thu, Mar 26, 2020 at 10:55 AM Marlon Richert
> >> <marlon.richert@gmail.com> wrote:
> >> >
> >> > Test case:
> >> >
> >> > alias -g tail="multitail -Cs --follow-all"
> >> > f() {
> >> > setopt localoptions no_aliases
> >> > local tail
> >> > tail=1
> >> > echo $tail
> >> > }
> >>
> >> Alias expansion happens when functions get parsed. If you don't want
> >> `tail` to be alias-expanded within function `f`, you need to add
> >> `setopt no_aliases` before the function of `f`.
> >>
> >> > g() {
> >> > setopt localoptions no_aliases
> >> > local tail=1
> >> > echo $tail
> >> > }
> >>
> >> `tail` within `local tail=1` is not subject to global alias expansion.
> >>
> >> Roman.
next prev parent reply other threads:[~2020-03-26 22:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-26 9:54 Marlon Richert
2020-03-26 9:59 ` Roman Perepelitsa
2020-03-26 10:27 ` Marlon Richert
2020-03-26 10:38 ` Roman Perepelitsa
2020-03-26 22:34 ` Daniel Shahaf [this message]
2020-03-29 10:24 ` Roman Perepelitsa
2020-03-29 20:47 ` Daniel Shahaf
2020-03-29 21:03 ` Roman Perepelitsa
2020-03-29 21:12 ` Daniel Shahaf
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=20200326223449.0a090ae6@tarpaulin.shahaf.local2 \
--to=d.s@daniel.shahaf.name \
--cc=marlon.richert@gmail.com \
--cc=roman.perepelitsa@gmail.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).