@@ -14,13 +14,36 @@ var sanitizer = strings.NewReplacer(
1414 "\t " , `` ,
1515)
1616
17- var valueReplacer = strings .NewReplacer (
17+ var escapingQuotedReplacer = strings .NewReplacer (
1818 `\` , `\\` ,
1919 `"` , `\"` ,
2020 `$` , `\$` ,
2121 "`" , "\\ `" ,
2222)
2323
24+ var escapingReplacer = strings .NewReplacer (
25+ `\` , `\\` ,
26+ `&` , `\&` ,
27+ `<` , `\<` ,
28+ `>` , `\>` ,
29+ "`" , "\\ `" ,
30+ `'` , `\'` ,
31+ `"` , `\"` ,
32+ `{` , `\{` ,
33+ `}` , `\}` ,
34+ `$` , `\$` ,
35+ `#` , `\#` ,
36+ `|` , `\|` ,
37+ `?` , `\?` ,
38+ `(` , `\(` ,
39+ `)` , `\)` ,
40+ `;` , `\;` ,
41+ ` ` , `\ ` ,
42+ `[` , `\[` ,
43+ `]` , `\]` ,
44+ `*` , `\*` ,
45+ )
46+
2447var displayReplacer = strings .NewReplacer (
2548 `${` , `\\\${` ,
2649)
@@ -81,19 +104,11 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
81104 nospace = nospace || meta .Nospace .Matches (val .Value )
82105
83106 vals [index ] = sanitizer .Replace (val .Value )
84- if requiresQuoting (vals [index ]) {
85- vals [index ] = valueReplacer .Replace (vals [index ])
86- switch {
87- case strings .HasPrefix (vals [index ], "~" ): // assume homedir expansion
88- if splitted := strings .SplitAfterN (vals [index ], "/" , 2 ); len (splitted ) == 2 {
89- vals [index ] = fmt .Sprintf (`%v"%v"` , splitted [0 ], splitted [1 ])
90- } else {
91- // TODO homedir expansion won't work this way, but shouldn't reach this point anyway.
92- vals [index ] = fmt .Sprintf (`~"%v"` , strings .TrimPrefix (vals [index ], "~" ))
93- }
94- default :
95- vals [index ] = fmt .Sprintf (`"%v"` , vals [index ])
96- }
107+ switch {
108+ case strings .HasPrefix (vals [index ], "~" ): // assume homedir expansion
109+ vals [index ] = escapingReplacer .Replace (vals [index ])
110+ case requiresQuoting (vals [index ]):
111+ vals [index ] = fmt .Sprintf (`"%v"` , escapingQuotedReplacer .Replace (vals [index ]))
97112 }
98113 } else {
99114 nospace = true
@@ -111,6 +126,7 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
111126
112127func requiresQuoting (s string ) bool {
113128 chars := " \t \r \n `" + `[]{}()<>;|$&:*#`
129+ chars += `'"`
114130 chars += os .Getenv ("COMP_WORDBREAKS" )
115131 chars += `\`
116132 return strings .ContainsAny (s , chars )
0 commit comments