zsh-workers
 help / color / mirror / code / Atom feed
* Can this snippet be further improved?
@ 2023-07-28 14:53 Sebastian Gniazdowski
  2023-07-28 15:06 ` Sebastian Gniazdowski
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2023-07-28 14:53 UTC (permalink / raw)
  To: Zsh hackers list

I'm preparing update to zsh plugin standard. It's about getting the
path to the sourced (….plugin.zsh) script. The current version is:

    0=${${ZERO:-${(%):-%x}}:A}

I wonder if it could be improved? The current version:
- favors ZERO,
- falls back to %x if ZERO is empty,
- makes the path absolute,
- resolved symlinks and ,,./ components.

Is there a bug or improvement that's possible?

-- 
Best regards,
Sebastian Gniazdowski


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

* Re: Can this snippet be further improved?
  2023-07-28 14:53 Can this snippet be further improved? Sebastian Gniazdowski
@ 2023-07-28 15:06 ` Sebastian Gniazdowski
  2023-07-28 21:37   ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2023-07-28 15:06 UTC (permalink / raw)
  To: Zsh hackers list

For example, would be :P instead of :A better?

On Fri, 28 Jul 2023 at 09:53, Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> I'm preparing update to zsh plugin standard. It's about getting the
> path to the sourced (….plugin.zsh) script. The current version is:
>
>     0=${${ZERO:-${(%):-%x}}:A}
>
> I wonder if it could be improved? The current version:
> - favors ZERO,
> - falls back to %x if ZERO is empty,
> - makes the path absolute,
> - resolved symlinks and ,,./ components.
>
> Is there a bug or improvement that's possible?
>
> --
> Best regards,
> Sebastian Gniazdowski



-- 
Best regards,
Sebastian Gniazdowski


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

* Re: Can this snippet be further improved?
  2023-07-28 15:06 ` Sebastian Gniazdowski
@ 2023-07-28 21:37   ` Bart Schaefer
  2023-07-29  3:09     ` Sebastian Gniazdowski
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2023-07-28 21:37 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

On Fri, Jul 28, 2023 at 3:07 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> For example, would be :P instead of :A better?
>
> On Fri, 28 Jul 2023 at 09:53, Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> >
> >     0=${${ZERO:-${(%):-%x}}:A}

What is $ZERO ?  (I'm not very familiar with the "zsh plugin standard" here.)

Is there a reason not to use ${funcstack[1]} instead of ${(%):-%x} ?
I presume you're assigning 0= at top level of the plugin, otherwise %x
is going to be the file where the function is defined rather than the
file where the function is being executed and further you'd mess up
FUNCTIONARGZERO et al.  I suppose %x doesn't require zsh/parameter, is
that really an issue?

As to :A vs. :P, it almost seems like :a:P would be preferable to
either of those ... :A is described as ":a followed by realpath()
except where there is no realpath(3)" whereas :P is "LIKE realpath()"
but doesn't actually require realpath(3).


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

* Re: Can this snippet be further improved?
  2023-07-29  3:09     ` Sebastian Gniazdowski
@ 2023-07-28 22:17       ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2023-07-28 22:17 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

On Fri, Jul 28, 2023 at 3:09 PM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> ZERO is a $0-like variable however provided by the plugin manager.
>
> I think that the returning of origin source file is a plus – one can
> throw in the 0="…" snippet in any kind of file, hash-bang script,
> sourced script and autoload function and always get expected result.

Seems like you'd do less potential damage to other things that might
examine $0 if you overloaded $ZSH_SCRIPT instead, here.


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

* Re: Can this snippet be further improved?
  2023-07-28 21:37   ` Bart Schaefer
@ 2023-07-29  3:09     ` Sebastian Gniazdowski
  2023-07-28 22:17       ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Gniazdowski @ 2023-07-29  3:09 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list

On Fri, 28 Jul 2023 at 16:38, Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Fri, Jul 28, 2023 at 3:07 AM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> > >     0=${${ZERO:-${(%):-%x}}:A}
>
> What is $ZERO ?  (I'm not very familiar with the "zsh plugin standard" here.)

ZERO is a $0-like variable however provided by the plugin manager. If
a plugin supports it then it may be easily loaded in an non-standard
way, like eval "$(<plugin-file.zsh):" (a faster method, however if the
script is compiled, then dot/source wins,so it's controversial…),

> Is there a reason not to use ${funcstack[1]} instead of ${(%):-%x} ?
> I presume you're assigning 0= at top level of the plugin, otherwise %x
> is going to be the file where the function is defined rather than the
> file where the function is being executed and further you'd mess up
> FUNCTIONARGZERO et al.  I suppose %x doesn't require zsh/parameter, is
> that really an issue?

I think that the returning of origin source file is a plus – one can
throw in the 0="…" snippet in any kind of file, hash-bang script,
sourced script and autoload function and always get expected result. I
think that if it'll be called in a function defined inside the sourced
script, then it'll still return the same, correct file path.

> As to :A vs. :P, it almost seems like :a:P would be preferable to
> either of those ... :A is described as ":a followed by realpath()
> except where there is no realpath(3)" whereas :P is "LIKE realpath()"
> but doesn't actually require realpath(3).

Thanks, it seems to clarify the :A and :P difference, I'll go for :P then.

-- 
Best regards,
Sebastian Gniazdowski


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

end of thread, other threads:[~2023-07-28 22:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-28 14:53 Can this snippet be further improved? Sebastian Gniazdowski
2023-07-28 15:06 ` Sebastian Gniazdowski
2023-07-28 21:37   ` Bart Schaefer
2023-07-29  3:09     ` Sebastian Gniazdowski
2023-07-28 22:17       ` 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).