diff options
Diffstat (limited to 'Functions/Zle/split-shell-arguments')
-rw-r--r-- | Functions/Zle/split-shell-arguments | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/Functions/Zle/split-shell-arguments b/Functions/Zle/split-shell-arguments new file mode 100644 index 000000000..e8a231732 --- /dev/null +++ b/Functions/Zle/split-shell-arguments @@ -0,0 +1,58 @@ +# Split a command line into shell arguments and whitespace in $reply. +# Odd elements (starting from 1) are whitespace, even elements +# are shell arguments (possibly quoted strings). Whitespace at +# start and end is always included in the array but may be an empty string. +# $REPLY holds NO_KSH_ARRAYS index of current word in $reply. +# $REPLY2 holds NO_KSH_ARRAYS index of current character in current word. +# Hence ${reply[$REPLY][$REPLY2]} is the character under the cursor. +# +# reply, REPLY, REPLY2 should therefore be local to the enclosing function. +# +# The following formula replaces the current shell word, or previous word +# if the cursor is on whitespace, by uppercasing all characters. + +emulate -L zsh +setopt extendedglob + +local -a bufwords lbufwords +local word +integer pos=1 cpos=$((CURSOR+1)) opos iword ichar + +bufwords=(${(z)BUFFER}) + +reply=() +while [[ ${BUFFER[pos]} = [[:space:]] ]]; do + (( pos++ )) +done +reply+=${BUFFER[1,pos-1]} +(( cpos < pos )) && (( iword = 1, ichar = cpos )) + +for word in "${bufwords[@]}"; do + (( opos = pos )) + (( pos += ${#word} )) + reply+=("$word") + if (( iword == 0 && cpos < pos )); then + (( iword = ${#reply} )) + (( ichar = cpos - opos + 1 )) + fi + + (( opos = pos )) + while [[ ${BUFFER[pos]} = [[:space:]] ]]; do + (( pos++ )) + done + reply+=("${BUFFER[opos,pos-1]}") + if (( iword == 0 && cpos < pos )); then + (( iword = ${#reply} )) + (( ichar = cpos - opos + 1 )) + fi +done + +if (( iword == 0 )); then + # At the end of the line, so off the indexable positions + # (but still a valid cursor position). + (( REPLY = ${#reply} )) + (( REPLY2 = 1 )) +else + (( REPLY = iword )) + (( REPLY2 = ichar )) +fi |