summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2003-10-13 16:50:14 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2003-10-13 16:50:14 +0000
commit78bef99b732ade6b8d4f7784624fa5dcaed2c446 (patch)
treedbce80816af617bedcbe343edced801778af8386 /Functions
parent9d9e92d20950ee9796e741f392e0b6d57cea0c4e (diff)
downloadzsh-78bef99b732ade6b8d4f7784624fa5dcaed2c446.tar.gz
zsh-78bef99b732ade6b8d4f7784624fa5dcaed2c446.tar.xz
zsh-78bef99b732ade6b8d4f7784624fa5dcaed2c446.zip
19183: new delete-whole-word-match
Diffstat (limited to 'Functions')
-rw-r--r--Functions/Zle/delete-whole-word-match56
1 files changed, 56 insertions, 0 deletions
diff --git a/Functions/Zle/delete-whole-word-match b/Functions/Zle/delete-whole-word-match
new file mode 100644
index 000000000..9f7b29128
--- /dev/null
+++ b/Functions/Zle/delete-whole-word-match
@@ -0,0 +1,56 @@
+# Delete the entire word around the cursor.  Does not handle
+# a prefix argument; either the cursor is in the word or it isn't.
+# The word may be just before the cursor, e.g.
+#   print this is a line
+#             ^ here
+# and then the word before (i.e. `this') will be deleted.
+#
+# If the widget has the name `kill' in, the text deleted will be
+# saved for future yanking in the normal way.
+
+emulate -L zsh
+setopt extendedglob
+
+local curcontext=:zle:delete-whole-word
+local -a matched_words
+# Start and end of range of characters to remove.
+integer pos1 pos2
+
+autoload -U match-words-by-style
+match-words-by-style
+
+if [[ -n "${matched_words[3]}" ]]; then
+    # There's whitespace before the cursor, so the word we are deleting
+    # starts at the cursor position.
+    pos1=$CURSOR
+else
+    # No whitespace before us, so delete any wordcharacters there.
+    pos1="${#matched_words[1]}"
+fi
+
+if [[ -n "${matched_words[4]}" ]]; then
+    # There's whitespace at the cursor position, so only delete
+    # up to the cursor position.
+    pos2=$CURSOR
+else
+    # No whitespace at the cursor position, so delete the
+    # current character and any following wordcharacters.
+    (( pos2 = CURSOR + ${#matched_words[5]} + 1 ))
+fi
+
+# Move the cursor then delete the block in one go for the
+# purpose of undoing (and yanking, if appropriate).
+(( CURSOR = pos1 ))
+
+# If the widget name includes the word `kill', the removed
+# text goes into the cutbuffer in the standard way.
+if [[ $WIDGET = *kill* ]]; then
+  local word="${BUFFER[pos1+1,pos2-1]}"
+  if [[ $LASTWIDGET = *kill* ]]; then
+    CUTBUFFER="$CUTBUFFER$word"
+  else
+    killring=("$CUTBUFFER" "${(@)killring[1,-2]}")
+    CUTBUFFER=$word
+  fi
+fi
+BUFFER="${BUFFER[1,pos1]}${BUFFER[pos2,-1]}"