about summary refs log tree commit diff
path: root/Functions/Zle/split-shell-arguments
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2006-12-15 11:44:26 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2006-12-15 11:44:26 +0000
commit937dc9a84d85bb1ca5135badd52461a928c17f6a (patch)
tree58c148452f2f93a70f690a44b0b95336557e5b0c /Functions/Zle/split-shell-arguments
parentd8e36bffa29b31c3533b4877701e37e3cffe44fb (diff)
downloadzsh-937dc9a84d85bb1ca5135badd52461a928c17f6a.tar.gz
zsh-937dc9a84d85bb1ca5135badd52461a928c17f6a.tar.xz
zsh-937dc9a84d85bb1ca5135badd52461a928c17f6a.zip
users/11083: functions for handling shell arguments in ZLE
Diffstat (limited to 'Functions/Zle/split-shell-arguments')
-rw-r--r--Functions/Zle/split-shell-arguments58
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