main: Allow for "]" in shell aliases

PR #776 fixed an issue with complex aliases and expansion. However, this change
also introduced a problem with aliases which contain `]` (for example, commonly
seen on macOS: `alias ]=open`), due to using an associative array `seen_alias`,
indexed by the alias name. Due to `"$seen_alias[$arg]"`, it would fail when
`$arg` is expanded to anything containing `]`'. Thus, typing `] /` would result
in:

```
> ] /
(anon):unset:3: seen_alias[]]: invalid parameter name
```

This change fixes the issue by ensuring we properly access keys in the
associative array `seen_alias`.

Older versions of zsh have issues with map keys having special
characters, especially lacking ways to remove such keys. The
issue is described in detail in
https://unix.stackexchange.com/questions/626393/in-zsh-how-do-i-unset-an-arbitrary-associative-array-element.

This fix uses proposal from
[zsh-workers/43269](https://www.zsh.org/mla/workers/2018/msg01073.html),
discovered by Stephane Chazelas, that boils down to avoid removing keys
from the map, and reconstruct the map anew with some keys omitted.

Co-authored-by: @phy1729
This commit is contained in:
Nikola Knezevic
2021-02-09 15:34:36 +01:00
committed by Matthew Martin
parent 205bc7ea19
commit e8517244f7
2 changed files with 53 additions and 7 deletions

View File

@@ -601,16 +601,19 @@ _zsh_highlight_main_highlighter_highlight_list()
(( in_alias[1]-- ))
# Remove leading 0 entries
in_alias=($in_alias[$in_alias[(i)<1->],-1])
(){
local alias_name
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
unset "seen_alias[$alias_name]"
done
}
if (( $#in_alias == 0 )); then
seen_alias=()
# start_pos and end_pos are of the alias (previous $arg) here
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
else
# We can't unset keys that contain special characters (] \ and some others).
# More details: https://www.zsh.org/workers/43269
(){
local alias_name
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
seen_alias=("${(@kv)seen_alias[(I)^$alias_name]}")
done
}
fi
fi
if (( in_param )); then
@@ -890,7 +893,9 @@ _zsh_highlight_main_highlighter_highlight_list()
(){
local alias_name
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
unset "seen_alias[$alias_name]"
# We can't unset keys that contain special characters (] \ and some others).
# More details: https://www.zsh.org/workers/43269
seen_alias=("${(@kv)seen_alias[(I)^$alias_name]}")
done
}
if [[ $arg != '|' && $arg != '|&' ]]; then