zsh-workers
 help / Atom feed
* Macros created via global aliases don't work
@ 2019-08-12  8:54 Aryn Starr
  2019-08-13  0:31 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Aryn Starr @ 2019-08-12  8:54 UTC (permalink / raw)
  To: zsh-workers

Hi! I am trying to create a macro in zsh using global aliases:

```
alias -g MAGIC=')"'
function m_doc() {
    print -r -- "! { [[ ${(q+)1} == '-h' ]] || [[ ${(q+)1} == '--help' ]] } || {
        print -r -- \"Help for $2:
$@[3,-1]\” ; return 0 }"
}
alias mdoc='eval "$(m_doc "$*" "$0" '

## Testing it

mdoc_test() {
    mdoc Usage: Some documentation here. MAGIC
    echo “You shouldn’t see this if using the help flags"
}

## Running this:
#$ mdoc_test -h
#mdoc_test:1: parse error near `)'
#mdoc_test:1: parse error in command substitution

mdoc_test2() {
    eval "$(m_doc "$*" "$0" Usage: Some documentation here.)"
    ec "$*"
}
## Running this:
#$ mdoc_test2 -h                                                                                                  1
#Help for mdoc_test2:
#Usage: Some documentation here.

mdoc_test3() {
    eval "$(m_doc "$*" "$0" Usage: Some documentation here. MAGIC
    ec "$*"
}
## Running this:
#$ mdoc_test3 -h
#mdoc_test3:1: parse error near `)'
#mdoc_test3:1: parse error in command substitution

```

It seems to me that this is a bug?

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

* Re: Macros created via global aliases don't work
  2019-08-12  8:54 Macros created via global aliases don't work Aryn Starr
@ 2019-08-13  0:31 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2019-08-13  0:31 UTC (permalink / raw)
  To: Aryn Starr; +Cc: zsh-workers

On Mon, Aug 12, 2019 at 1:55 AM Aryn Starr <whereislelouch@icloud.com> wrote:
>
> alias -g MAGIC=')"'
> alias mdoc='eval "$(m_doc "$*" "$0" '
>
>     mdoc Usage: Some documentation here. MAGIC

You've created an ambiguous "sentence".  Aliases are not really
macros; they are both expanded and parsed left-to-right, not fully
expanded before parsing begins. The ambiguity is that because you've
hidden the closing paren from the parser, it follows through the
depth-first interpretation rather than the breadth-first.

First mdoc expands, and then that expansion is parsed, so now the
parser is now working inside the command substitution, and when MAGIC
expands the shell sees it as part of the m_doc command rather than as
part of the mdoc command, if you see what I mean.

I think you can unambiguously get what you're after by using

alias mdoc='m_doc '
alias -g MAGIC='| { eval "$(read -d "" -r -E)" }'

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-12  8:54 Macros created via global aliases don't work Aryn Starr
2019-08-13  0:31 ` Bart Schaefer

zsh-workers

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

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


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