@@ -14,13 +14,36 @@ var sanitizer = strings.NewReplacer(
14
14
"\t " , `` ,
15
15
)
16
16
17
- var valueReplacer = strings .NewReplacer (
17
+ var escapingQuotedReplacer = strings .NewReplacer (
18
18
`\` , `\\` ,
19
19
`"` , `\"` ,
20
20
`$` , `\$` ,
21
21
"`" , "\\ `" ,
22
22
)
23
23
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
+
24
47
var displayReplacer = strings .NewReplacer (
25
48
`${` , `\\\${` ,
26
49
)
@@ -81,19 +104,11 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
81
104
nospace = nospace || meta .Nospace .Matches (val .Value )
82
105
83
106
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 ]))
97
112
}
98
113
} else {
99
114
nospace = true
@@ -111,6 +126,7 @@ func ActionRawValues(currentWord string, meta common.Meta, values common.RawValu
111
126
112
127
func requiresQuoting (s string ) bool {
113
128
chars := " \t \r \n `" + `[]{}()<>;|$&:*#`
129
+ chars += `'"`
114
130
chars += os .Getenv ("COMP_WORDBREAKS" )
115
131
chars += `\`
116
132
return strings .ContainsAny (s , chars )
0 commit comments