zsh-workers
 help / color / mirror / code / Atom feed
* what can be global aliases used for?
       [not found] <CAKc7PVA0X1786JP7g69aDP8eL+1oAZpipVC-4BNm1KV=OPxe7A@mail.gmail.com>
@ 2023-02-17 14:54 ` Sebastian Gniazdowski
  2023-02-17 17:15   ` Roman Perepelitsa
                     ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Sebastian Gniazdowski @ 2023-02-17 14:54 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 334 bytes --]

I'm trying to get the suspected capabilities out of global aliases.
Currently I'm using them for:
- macro-like labels for exporting patterns like EMPTYSTR being
[[:space:][:INVALID:]]# roughly, to a single place,
- macro-like SNIP_EMULATE_OPTIONS_ZERO that invokes emulate, setopt and
0=...

I wonder what else can global aliases do?

[-- Attachment #2: Type: text/html, Size: 589 bytes --]

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

* Re: what can be global aliases used for?
  2023-02-17 14:54 ` what can be global aliases used for? Sebastian Gniazdowski
@ 2023-02-17 17:15   ` Roman Perepelitsa
  2023-02-17 19:10   ` Bart Schaefer
  2023-02-17 21:11   ` Lawrence Velázquez
  2 siblings, 0 replies; 6+ messages in thread
From: Roman Perepelitsa @ 2023-02-17 17:15 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

On Fri, Feb 17, 2023 at 3:59 PM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> I'm trying to get the suspected capabilities out of global aliases. Currently I'm using them for:
> - macro-like labels for exporting patterns like EMPTYSTR being [[:space:][:INVALID:]]# roughly, to a single place,
> - macro-like SNIP_EMULATE_OPTIONS_ZERO that invokes emulate, setopt and 0=...
>
> I wonder what else can global aliases do?

Global aliases are unhygienic macros. It's rarely a good idea to use
unhygienic macros in the code you share with others. If you must use
an unhygienic macro in shared code, give it a long and ugly name to
avoid clashes.

EMPTYSTR can be a parameter. You would have to use $~EMPTYSTR but
you'll avoid most of the pitfalls of unhygienic macros.

SNIP_EMULATE_OPTIONS_ZERO can also be a parameter, which you can use
via `eval $SNIP_EMULATE_OPTIONS_ZERO`. I do something like this in
zsh4humans:

typeset -gr _z4h_opt='emulate -L zsh &&
  setopt typeset_silent pipe_fail extended_glob &&
  setopt prompt_percent no_prompt_subst no_prompt_bang &&
  setopt no_bg_nice no_aliases || return'

function foo() {
  eval "$_z4h_opt"
  # do stuff
}

function bar() {
  eval "$_z4h_opt"
  # do stuff
}

Roman.


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

* Re: what can be global aliases used for?
  2023-02-17 14:54 ` what can be global aliases used for? Sebastian Gniazdowski
  2023-02-17 17:15   ` Roman Perepelitsa
@ 2023-02-17 19:10   ` Bart Schaefer
  2023-02-17 21:11   ` Lawrence Velázquez
  2 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2023-02-17 19:10 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

On Fri, Feb 17, 2023 at 6:59 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> I wonder what else can global aliases do?

As Roman has already indicated, it's not a recommended practice to
rely on aliases of any kind in scripts.  Aliasing is intended as an
interactive feature to reduce time spent typing commonly-used idioms.

That said, the most common use I've seen made of global aliases is
this kind of thing:

alias -g L='|less'
% ls -lLR L

That is, append L instead of "|less" to commands that are expected to
produce many pages of output.

Similarly stuff like

alias -g TODAYS_FILES='-lrt | fgrep "$(date +"%b %2d")" | awk '\''{print $NF}'\'
% ls TODAYS_FILES

(yes, there's a more zsh-ish way to do that, and it could also be a
command alias by sticking the "ls" in front, just using the pipeline
as an example).


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

* Re: what can be global aliases used for?
  2023-02-17 14:54 ` what can be global aliases used for? Sebastian Gniazdowski
  2023-02-17 17:15   ` Roman Perepelitsa
  2023-02-17 19:10   ` Bart Schaefer
@ 2023-02-17 21:11   ` Lawrence Velázquez
  2023-02-17 21:19     ` Roman Perepelitsa
  2 siblings, 1 reply; 6+ messages in thread
From: Lawrence Velázquez @ 2023-02-17 21:11 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: zsh-workers

On Fri, Feb 17, 2023, at 9:54 AM, Sebastian Gniazdowski wrote:
> I wonder what else can global aliases do?

An esoteric but amusing example:

  1) Autoconf uses ${1+"$@"} instead of "$@" to work around a bug
     in pre-SVR3 Bourne shells.

  2) This doesn't work properly in pre-4.3 zsh because SH_WORD_SPLIT
     splits ${1+"$@"} too aggressively.

  3) But zsh doesn't have a problem with "$@" in the first place,
     so Autoconf defines a global alias that dynamically replaces
     ${1+"$@"} with "$@", undoing the original workaround.

https://www.gnu.org/software/autoconf/manual/autoconf-2.71/html_node/Shell-Substitutions.html#index-_0022_0024_0040_0022

-- 
vq


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

* Re: what can be global aliases used for?
  2023-02-17 21:11   ` Lawrence Velázquez
@ 2023-02-17 21:19     ` Roman Perepelitsa
  2023-02-17 23:27       ` Lawrence Velázquez
  0 siblings, 1 reply; 6+ messages in thread
From: Roman Perepelitsa @ 2023-02-17 21:19 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: Sebastian Gniazdowski, zsh-workers

On Fri, Feb 17, 2023 at 10:13 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>
>   1) Autoconf uses ${1+"$@"} instead of "$@" to work around a bug
>      in pre-SVR3 Bourne shells.

Fascinating.

This made me Google SVR3. That's System V, Release 3, right? 1987.
Before the first release of bash and zsh.

Roman.


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

* Re: what can be global aliases used for?
  2023-02-17 21:19     ` Roman Perepelitsa
@ 2023-02-17 23:27       ` Lawrence Velázquez
  0 siblings, 0 replies; 6+ messages in thread
From: Lawrence Velázquez @ 2023-02-17 23:27 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: zsh-workers

On Fri, Feb 17, 2023, at 4:19 PM, Roman Perepelitsa wrote:
> On Fri, Feb 17, 2023 at 10:13 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>>
>>   1) Autoconf uses ${1+"$@"} instead of "$@" to work around a bug
>>      in pre-SVR3 Bourne shells.
>
> Fascinating.
>
> This made me Google SVR3. That's System V, Release 3, right? 1987.
> Before the first release of bash and zsh.

That's right!  Absolutely ancient, but -- to be fair -- downright
modern compared to its ancestors [1].  Sensible "$@" [2]?  Functions
that don't trash the global positional parameters [3]?  Sign me up.

  [1]: https://www.in-ulm.de/~mascheck/bourne/
  [2]: https://www.in-ulm.de/~mascheck/various/bourne_args/
  [3]: https://www.in-ulm.de/~mascheck/bourne/function_parameters.html

-- 
vq


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

end of thread, other threads:[~2023-02-17 23:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAKc7PVA0X1786JP7g69aDP8eL+1oAZpipVC-4BNm1KV=OPxe7A@mail.gmail.com>
2023-02-17 14:54 ` what can be global aliases used for? Sebastian Gniazdowski
2023-02-17 17:15   ` Roman Perepelitsa
2023-02-17 19:10   ` Bart Schaefer
2023-02-17 21:11   ` Lawrence Velázquez
2023-02-17 21:19     ` Roman Perepelitsa
2023-02-17 23:27       ` Lawrence Velázquez

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