diff options
author | Paul Ackersviller <packersv@users.sourceforge.net> | 2007-11-11 02:17:23 +0000 |
---|---|---|
committer | Paul Ackersviller <packersv@users.sourceforge.net> | 2007-11-11 02:17:23 +0000 |
commit | 083100056939b7b29f857bb2e41e6f7947da247c (patch) | |
tree | a5950f4598745687198ef06cc65f2c8406b2c8ee /Functions/Zle/split-shell-arguments | |
parent | 68b705eda289521adc5ca96dd7c2e91371c074f0 (diff) | |
download | zsh-083100056939b7b29f857bb2e41e6f7947da247c.tar.gz zsh-083100056939b7b29f857bb2e41e6f7947da247c.tar.xz zsh-083100056939b7b29f857bb2e41e6f7947da247c.zip |
Merge of users/11083: functions for handling splitting of shell arguments in ZLE.
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 |