zsh-users
 help / color / mirror / code / Atom feed
* Can $_ substitute $0 when nofunctionargzero
@ 2016-06-09 16:44 Sebastian Gniazdowski
  2016-06-09 17:06 ` Sebastian Gniazdowski
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-09 16:44 UTC (permalink / raw)
  To: Zsh Users

Hello,
this (one line):
mkdir -p pathcomponent; echo 'echo "0: $0, _: $_";' >
pathcomponent/sourceme; setopt nofunctionargzero; source
pathcomponent/sourceme

outputs:
0: /bin/zsh, _: pathcomponent/sourceme

So it looks like $_ can take back what nofunctionargzero takes away:
path to sourced file. Normally $0 is used in many plugins, making them
incompatible with nofunctionargzero. Z-sy-h has an explicit check for
this and does:

  echo "zsh-syntax-highlighting: error: not compatible with
NO_FUNCTION_ARGZERO" >&2

Can $_ take over? This would mean such scripts can be compatible with
emulate sh and ksh.

Best regards,
Sebastian Gniazdowski


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

* Re: Can $_ substitute $0 when nofunctionargzero
  2016-06-09 16:44 Can $_ substitute $0 when nofunctionargzero Sebastian Gniazdowski
@ 2016-06-09 17:06 ` Sebastian Gniazdowski
  2016-06-09 18:33   ` Sebastian Gniazdowski
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-09 17:06 UTC (permalink / raw)
  To: Zsh Users

Hello,
documentation says: "The  last  argument of the previous command.
Also, this parameter is set in the environment of every command
executed to the full pathname of the command."

Turns out it's the first sentence that happens. Last argument to
`source' is substituted for $_. But in real word, $_ can still be used
when option functionargzero is unset, unless someone will came up with
a standard of how plugins can be parametrized – they're normally
sourced without any additional arguments.

Any more flaws in this?

Best regards,
Sebastian Gniazdowski




On 9 June 2016 at 18:44, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> Hello,
> this (one line):
> mkdir -p pathcomponent; echo 'echo "0: $0, _: $_";' >
> pathcomponent/sourceme; setopt nofunctionargzero; source
> pathcomponent/sourceme
>
> outputs:
> 0: /bin/zsh, _: pathcomponent/sourceme
>
> So it looks like $_ can take back what nofunctionargzero takes away:
> path to sourced file. Normally $0 is used in many plugins, making them
> incompatible with nofunctionargzero. Z-sy-h has an explicit check for
> this and does:
>
>   echo "zsh-syntax-highlighting: error: not compatible with
> NO_FUNCTION_ARGZERO" >&2
>
> Can $_ take over? This would mean such scripts can be compatible with
> emulate sh and ksh.
>
> Best regards,
> Sebastian Gniazdowski


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

* Re: Can $_ substitute $0 when nofunctionargzero
  2016-06-09 17:06 ` Sebastian Gniazdowski
@ 2016-06-09 18:33   ` Sebastian Gniazdowski
  2016-06-09 20:46     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-09 18:33 UTC (permalink / raw)
  To: Zsh Users

Hello,
I read through discussion on ZSH_SCRIPT and ZSH_ARGZERO. Could there
be one more parameter added? Basically, it's hard to make a software
package without knowing where sourced file is located. A good example
is zsh-syntax-highlighting. It needs functionargzero to be able to do
source $0:h/highlighters/main/main-highlighter.zsh and other sources.
An alternative is a variable, which is $ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR
in this case.

This is for emulate sh and ksh, where functionargzero is unset. Would
we expect that some day software packages for shells with ksh
emulation will be created? It would be interesting I think.

Best regards,
Sebastian Gniazdowski


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

* Re: Can $_ substitute $0 when nofunctionargzero
  2016-06-09 18:33   ` Sebastian Gniazdowski
@ 2016-06-09 20:46     ` Bart Schaefer
  2016-06-10  6:12       ` Sebastian Gniazdowski
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2016-06-09 20:46 UTC (permalink / raw)
  To: Zsh Users

On Jun 9,  7:06pm, Sebastian Gniazdowski wrote:
} Subject: Re: Can $_ substitute $0 when nofunctionargzero
}
} documentation says: "The  last  argument of the previous command.
} Also, this parameter is set in the environment of every command
} executed to the full pathname of the command."

That should probably say "the environment of every external command"
because the second part does not apply to builtins and subshells.

} Turns out it's the first sentence that happens. Last argument to
} `source' is substituted for $_. But in real word, $_ can still be used
} when option functionargzero is unset, unless someone will came up with
} a standard of how plugins can be parametrized - they're normally
} sourced without any additional arguments.
} 
} Any more flaws in this?

I had a bit of trouble untangling your "unless" clause there but if I
understand you correctly then I don't think you've overlooked any
other problems.

On Jun 9,  8:33pm, Sebastian Gniazdowski wrote:
} Subject: Re: Can $_ substitute $0 when nofunctionargzero
}
} I read through discussion on ZSH_SCRIPT and ZSH_ARGZERO. Could there
} be one more parameter added?

At the risk of becoming one of PWS's second category of outraged frothy
gibberers, there are already several other ways to obtain this:

    $(print -P %N), which works for any zsh of the past 16 years
    ${(%):-%N}, which has worked for the past 13 years
    ${funcstack[1]} (or $funcstack[0] if ksharrays), somewhat newer

(Not all versions that have $funcstack also treat "source" as a stack
position, as I recall, though $funcstack has been around a long time.)

If the concern (see discussion of add-zsh-hook-widget) is that plugins
need to work arcoss a range of versions, then adding another builtin
parameter is not going to be very useful.


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

* Re: Can $_ substitute $0 when nofunctionargzero
  2016-06-09 20:46     ` Bart Schaefer
@ 2016-06-10  6:12       ` Sebastian Gniazdowski
  2016-06-15 16:58         ` Sebastian Gniazdowski
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-10  6:12 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On 9 June 2016 at 22:46, Bart Schaefer <schaefer@brasslantern.com> wrote:
> That should probably say "the environment of every external command"
> because the second part does not apply to builtins and subshells.

It is little hard to understand. For example something works in following:

local var="I am $_"
> I am local

Wonder if some nice trickery can be done with $_, maybe in patterns

> I had a bit of trouble untangling your "unless" clause there but if I
> understand you correctly then I don't think you've overlooked any
> other problems.

I thought about e.g.:

source "/path/plugin" color=red start_in_incremental_search

when sourcing some plugin – that what I meant by "parametrizing".

> At the risk of becoming one of PWS's second category of outraged frothy
> gibberers, there are already several other ways to obtain this:
>
>     $(print -P %N), which works for any zsh of the past 16 years
>     ${(%):-%N}, which has worked for the past 13 years
>     ${funcstack[1]} (or $funcstack[0] if ksharrays), somewhat newer

That's just great, thanks! I already added "0=${(%):-%N}" to my
plugins. Should have just asked earlier.

> If the concern (see discussion of add-zsh-hook-widget) is that plugins
> need to work arcoss a range of versions, then adding another builtin
> parameter is not going to be very useful.

Yeah I thought about future being sure there's no current remedy for
nofunctionargzero.

Best regards,
Sebastian Gniazdowski


On 9 June 2016 at 22:46, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Jun 9,  7:06pm, Sebastian Gniazdowski wrote:
> } Subject: Re: Can $_ substitute $0 when nofunctionargzero
> }
> } documentation says: "The  last  argument of the previous command.
> } Also, this parameter is set in the environment of every command
> } executed to the full pathname of the command."
>
> That should probably say "the environment of every external command"
> because the second part does not apply to builtins and subshells.
>
> } Turns out it's the first sentence that happens. Last argument to
> } `source' is substituted for $_. But in real word, $_ can still be used
> } when option functionargzero is unset, unless someone will came up with
> } a standard of how plugins can be parametrized - they're normally
> } sourced without any additional arguments.
> }
> } Any more flaws in this?
>
> I had a bit of trouble untangling your "unless" clause there but if I
> understand you correctly then I don't think you've overlooked any
> other problems.
>
> On Jun 9,  8:33pm, Sebastian Gniazdowski wrote:
> } Subject: Re: Can $_ substitute $0 when nofunctionargzero
> }
> } I read through discussion on ZSH_SCRIPT and ZSH_ARGZERO. Could there
> } be one more parameter added?
>
> At the risk of becoming one of PWS's second category of outraged frothy
> gibberers, there are already several other ways to obtain this:
>
>     $(print -P %N), which works for any zsh of the past 16 years
>     ${(%):-%N}, which has worked for the past 13 years
>     ${funcstack[1]} (or $funcstack[0] if ksharrays), somewhat newer
>
> (Not all versions that have $funcstack also treat "source" as a stack
> position, as I recall, though $funcstack has been around a long time.)
>
> If the concern (see discussion of add-zsh-hook-widget) is that plugins
> need to work arcoss a range of versions, then adding another builtin
> parameter is not going to be very useful.


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

* Re: Can $_ substitute $0 when nofunctionargzero
  2016-06-10  6:12       ` Sebastian Gniazdowski
@ 2016-06-15 16:58         ` Sebastian Gniazdowski
  2016-06-15 17:40           ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-15 16:58 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On 10 June 2016 at 08:12, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote:
> On 9 June 2016 at 22:46, Bart Schaefer <schaefer@brasslantern.com> wrote:
>>     $(print -P %N), which works for any zsh of the past 16 years
>>     ${(%):-%N}, which has worked for the past 13 years
>>     ${funcstack[1]} (or $funcstack[0] if ksharrays), somewhat newer
>
> That's just great, thanks! I already added "0=${(%):-%N}" to my
> plugins. Should have just asked earlier.

Just noted that when recording asciinema video, $0 nor %N do work.
Both contain only file name, without path. Seems pretty weird?

~ Asciicast recording started.
~ Hit Ctrl-D or type "exit" to finish.
# PATH="$PATH:`pwd`"
test_program
0: test_program, ZERO: test_program
# exit
~ Asciicast recording finished.
~ Press <Enter> to upload, <Ctrl-C> to cancel.
^C%
# PATH="$PATH:`pwd`"
# test_program
0: /Users/sgniazdowski/test_program, ZERO: /Users/sgniazdowski/test_program

Best regards,
Sebastian Gniazdowski


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

* Re: Can $_ substitute $0 when nofunctionargzero
  2016-06-15 16:58         ` Sebastian Gniazdowski
@ 2016-06-15 17:40           ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2016-06-15 17:40 UTC (permalink / raw)
  To: Zsh Users

On Jun 15,  6:58pm, Sebastian Gniazdowski wrote:
}
} Just noted that when recording asciinema video, $0 nor %N do work.
} Both contain only file name, without path. Seems pretty weird?

$0 and %N only contain the path given to "source" or passed in as argv[0].
That will be "test_program" here unless a $path search changes it.

Perhaps asciinema inserts "." or an empty element into $PATH where
you don't normally have one, so `pwd` isn't used for the path search?


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

end of thread, other threads:[~2016-06-15 17:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-09 16:44 Can $_ substitute $0 when nofunctionargzero Sebastian Gniazdowski
2016-06-09 17:06 ` Sebastian Gniazdowski
2016-06-09 18:33   ` Sebastian Gniazdowski
2016-06-09 20:46     ` Bart Schaefer
2016-06-10  6:12       ` Sebastian Gniazdowski
2016-06-15 16:58         ` Sebastian Gniazdowski
2016-06-15 17:40           ` Bart Schaefer

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).