|
39 | 39 |
|
40 | 40 |
|
41 | 41 | # Escape a value for use in the 'P' ("Property") or 'E' ("Command Line") sequences.
|
| 42 | +# Backslashes are doubled and non-alphanumeric characters are hex encoded. |
42 | 43 | function __vsc_escape_value
|
43 |
| - set -l commandline "$argv" |
44 |
| - # `string replace` automatically breaks its input apart on any newlines. |
45 |
| - # Then `string join` at the end will bring it all back together. |
46 |
| - string replace --all '\\' '\\\\' $commandline \ |
47 |
| - | string replace --all ';' '\\x3b' \ |
48 |
| - | string join '\\x0a' |
| 44 | + # Replace all non-alnum characters with %XX hex form. |
| 45 | + string escape --style=url "$argv" \ |
| 46 | + # The characters [-./_~] are not encoded in the builtin url escaping, despite not being alphanumeric. |
| 47 | + # See: https://github.com/fish-shell/fish-shell/blob/f82537bcdcb32f85a530395f00e7be1aa9afc592/src/common.cpp#L738 |
| 48 | + # For consistency, also encode those characters. |
| 49 | + | string replace --all '-' '%2D' \ |
| 50 | + | string replace --all '.' '%2E' \ |
| 51 | + | string replace --all '/' '%2F' \ |
| 52 | + | string replace --all '_' '%5F' \ |
| 53 | + | string replace --all '~' '%7E' \ |
| 54 | + # Now everything is either alphanumeric [0-9A-Za-z] or %XX escapes. |
| 55 | + # Change the hex escape representation from '%' to '\x'. (e.g. ' ' → '%20' → '\x20'). |
| 56 | + # Note that all '%' characters are escapes: literal '%' will already have been encoded. |
| 57 | + | string replace --all '%' '\\x' \ |
| 58 | + # For readability, prefer to represent literal backslashes with doubling. ('\' → '%5C' → '\x5C' → '\\'). |
| 59 | + | string replace --all --ignore-case '%5C' '\\\\' \ |
| 60 | + ; |
49 | 61 | end
|
50 | 62 |
|
51 | 63 | # Sent right after an interactive command has finished executing.
|
|
0 commit comments