1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-01-04 06:14:50 +01:00
zsh/Functions/Zle/delete-whole-word-match

55 lines
1.6 KiB
Text
Raw Normal View History

2003-10-13 18:50:14 +02:00
# 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
2007-11-10 18:37:55 +01:00
setopt extendedglob
2003-10-13 18:50:14 +02:00
local curcontext=:zle:$WIDGET
local -A matched_words
2003-10-13 18:50:14 +02:00
# Start and end of range of characters to remove.
integer pos1 pos2
autoload -Uz match-words-by-style
2003-10-13 18:50:14 +02:00
match-words-by-style
if (( ${matched_words[is-word-start]} )); then
# The word we are deleting starts at the cursor position.
2003-10-13 18:50:14 +02:00
pos1=$CURSOR
else
# Not, so delete any wordcharacters before, too
pos1="${#matched_words[start]}"
2003-10-13 18:50:14 +02:00
fi
if [[ -n "${matched_words[ws-after-cursor]}" ]]; then
2003-10-13 18:50:14 +02:00
# There's whitespace at the cursor position, so only delete
# up to the cursor position.
(( pos2 = CURSOR + 1 ))
2003-10-13 18:50:14 +02:00
else
# No whitespace at the cursor position, so delete the
# current character and any following wordcharacters.
(( pos2 = CURSOR + ${#matched_words[word-after-cursor]} + 1 ))
2003-10-13 18:50:14 +02:00
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
zle copy-region-as-kill -- "$word"
2003-10-13 18:50:14 +02:00
fi
fi
BUFFER="${BUFFER[1,pos1]}${BUFFER[pos2,-1]}"