Skip to content

Commit e851724

Browse files
knlphy1729
authored andcommitted
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
1 parent 205bc7e commit e851724

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

highlighters/main/main-highlighter.zsh

+12-7
Original file line numberDiff line numberDiff line change
@@ -601,16 +601,19 @@ _zsh_highlight_main_highlighter_highlight_list()
601601
(( in_alias[1]-- ))
602602
# Remove leading 0 entries
603603
in_alias=($in_alias[$in_alias[(i)<1->],-1])
604-
(){
605-
local alias_name
606-
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
607-
unset "seen_alias[$alias_name]"
608-
done
609-
}
610604
if (( $#in_alias == 0 )); then
611605
seen_alias=()
612606
# start_pos and end_pos are of the alias (previous $arg) here
613607
_zsh_highlight_main_add_region_highlight $start_pos $end_pos $alias_style
608+
else
609+
# We can't unset keys that contain special characters (] \ and some others).
610+
# More details: https://www.zsh.org/workers/43269
611+
(){
612+
local alias_name
613+
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
614+
seen_alias=("${(@kv)seen_alias[(I)^$alias_name]}")
615+
done
616+
}
614617
fi
615618
fi
616619
if (( in_param )); then
@@ -890,7 +893,9 @@ _zsh_highlight_main_highlighter_highlight_list()
890893
(){
891894
local alias_name
892895
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
893-
unset "seen_alias[$alias_name]"
896+
# We can't unset keys that contain special characters (] \ and some others).
897+
# More details: https://www.zsh.org/workers/43269
898+
seen_alias=("${(@kv)seen_alias[(I)^$alias_name]}")
894899
done
895900
}
896901
if [[ $arg != '|' && $arg != '|&' ]]; then
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2021 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
# Have to use cat here as it must be a command that exists.
32+
# Otherwise, the test would fail with the first token being recognized
33+
# as an "unknown-token".
34+
alias ]=cat
35+
36+
BUFFER='] /'
37+
38+
expected_region_highlight=(
39+
'1 1 alias' # ]
40+
'3 3 path' # /
41+
)

0 commit comments

Comments
 (0)