Skip to content

Commit be93c7d

Browse files
committed
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`.
1 parent 5eb4948 commit be93c7d

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

highlighters/main/main-highlighter.zsh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ _zsh_highlight_main_highlighter_highlight_list()
604604
(){
605605
local alias_name
606606
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
607-
unset "seen_alias[$alias_name]"
607+
unset "seen_alias[${(q)alias_name}]"
608608
done
609609
}
610610
if (( $#in_alias == 0 )); then
@@ -697,15 +697,15 @@ _zsh_highlight_main_highlighter_highlight_list()
697697
if [[ $this_word == *':start:'* ]] && ! (( in_redirection )); then
698698
# Expand aliases.
699699
# An alias is ineligible for expansion while it's being expanded (see #652/#653).
700-
_zsh_highlight_main__type "$arg" "$(( ! ${+seen_alias[$arg]} ))"
700+
_zsh_highlight_main__type "$arg" "$(( ! ${+seen_alias[${(q)arg}]} ))"
701701
local res="$REPLY"
702702
if [[ $res == "alias" ]]; then
703703
# Mark insane aliases as unknown-token (cf. #263).
704704
if [[ $arg == ?*=* ]]; then
705705
_zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
706706
continue
707707
fi
708-
seen_alias[$arg]=$#in_alias
708+
seen_alias+=(${(q)arg} $#in_alias)
709709
_zsh_highlight_main__resolve_alias $arg
710710
local -a alias_args
711711
# Elision is desired in case alias x=''
@@ -890,7 +890,7 @@ _zsh_highlight_main_highlighter_highlight_list()
890890
(){
891891
local alias_name
892892
for alias_name in ${(k)seen_alias[(R)<$#in_alias->]}; do
893-
unset "seen_alias[$alias_name]"
893+
unset "seen_alias[${(q)alias_name}]"
894894
done
895895
}
896896
if [[ $arg != '|' && $arg != '|&' ]]; then
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
alias ]=open
32+
33+
BUFFER=$'] /'
34+
35+
expected_region_highlight=(
36+
'1 1 alias' # ]
37+
'3 3 path' # /
38+
)

0 commit comments

Comments
 (0)