fish-shell/share/functions/__fish_tokenizer_state.fish
Johannes Altmanninger 4c1173f2ae fish_clipboard_paste: trim indentation when pasting multiple lines
When pasting a multiline command with indented blocks, extra indentation
from spaces, or tabs, is generally undesirable, because fish already indents
pipes and blocks. Discard the indentation unless the cursor or the pasted
part is inside quotes.

Users who copied fish_clipboard_paste need to update it because
__fish_commandline_is_singlequoted had an API change and was renamed.
2021-02-13 08:55:59 +01:00

63 lines
2.1 KiB
Fish

function __fish_tokenizer_state --description "Print the state of the tokenizer at the end of the given string"
# Go through the token char-by-char in a state machine.
# The states are:
# - normal - no quoting is active (the starting state)
# - single - open single-quote
# - double - open double
# - escaped - open \\ - the next character is non-special
# - single-escaped - open \\ inside single-quotes
# - double-escaped - open \\ inside double-quotes
argparse --min-args 1 --max-args 1 i/initial-state= -- $argv
or return 1
set -l state normal
if set -q _flag_initial_state
set str $_flag_initial_state
end
for char in (string split -- "" $argv[1])
switch $char
case "'" # single-quote
switch $state
case normal single-escaped
set state single
case single
set state normal
end
case '"' # double-quote
switch $state
case normal double-escaped
set state double
case double
set state normal
end
case \\ # backslash escapes the next character
switch $state
case double
set state double-escaped
case double-escaped
set state double
case single
set state single-escaped
case single-escaped
set state single
case normal
set state escaped
case escaped
set state normal
end
case "*" # Any other character
switch $state
case escaped
set state normal
case single-escaped
set state single
case double-escaped
set state double
end
end
end
echo $state
end