summary refs log tree commit diff
path: root/Functions/Zle/split-shell-arguments
blob: 32b04fcb57c6d38b1582cbef9c6df7d3e39b96fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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.

emulate -L zsh
setopt extendedglob

local -a bufwords lbufwords
local word
integer pos=1 cpos=$((CURSOR+1)) opos iword ichar

bufwords=(${(Z+n+)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