mirror of
git://git.code.sf.net/p/zsh/code
synced 2025-11-27 02:50:57 +01:00
Merge of users/11083: functions for handling splitting of shell arguments in ZLE.
This commit is contained in:
parent
68b705eda2
commit
0831000569
3 changed files with 163 additions and 0 deletions
58
Functions/Zle/split-shell-arguments
Normal file
58
Functions/Zle/split-shell-arguments
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue