diff options
author | Oliver Kiddle <opk@zsh.org> | 2015-05-13 23:05:20 +0200 |
---|---|---|
committer | Oliver Kiddle <opk@zsh.org> | 2015-05-13 23:05:20 +0200 |
commit | d257f0143e69c3724466c4c92f59538d2f3fffd1 (patch) | |
tree | a9c51888931eb9329ad0918f82b1563ca1ad7588 /Functions/Zle/select-quoted | |
parent | f454ee26a8f644a2d37874ea8274054203892f91 (diff) | |
download | zsh-d257f0143e69c3724466c4c92f59538d2f3fffd1.tar.gz zsh-d257f0143e69c3724466c4c92f59538d2f3fffd1.tar.xz zsh-d257f0143e69c3724466c4c92f59538d2f3fffd1.zip |
35093: new zle widgets for Vim-style text objects
Diffstat (limited to 'Functions/Zle/select-quoted')
-rw-r--r-- | Functions/Zle/select-quoted | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Functions/Zle/select-quoted b/Functions/Zle/select-quoted new file mode 100644 index 000000000..904f1e46d --- /dev/null +++ b/Functions/Zle/select-quoted @@ -0,0 +1,71 @@ +# Text object for matching characters between a particular delimiter +# +# So for example, given "text", the vi command vi" will select +# all the text between the double quotes +# +# The following is an example of how to enable this: +# autoload -U select-quoted +# zle -N select-quoted +# for m in visual viopp; do +# for c in {a,i}{\',\",\`}; do +# bindkey -M $m $c select-quoted +# done +# done + +setopt localoptions noksharrays + +local matching=${${1:-$KEYS}[2]} +local -i start=CURSOR+2 end=CURSOR+2 found=0 alt=0 count=0 + +if ((REGION_ACTIVE )); then + if (( MARK < CURSOR )); then + start=MARK+2 + else + end=MARK+2 + fi +fi + +[[ $BUFFER[CURSOR+1] = $matching && $BUFFER[CURSOR] != \\ ]] && count=1 +while (( (count || ! alt) && --start )) && [[ $BUFFER[start] != $'\n' ]]; do + if [[ $BUFFER[start] = "$matching" ]]; then + if [[ $BUFFER[start-1] = \\ ]]; then + (( start-- )) + elif (( ! found )); then + found=start + else + (( ! alt )) && alt=start + (( count && ++count )) + fi + fi +done + +for (( start=CURSOR+2; ! found && start+1 < $#BUFFER; start++ )); do + case $BUFFER[start] in + $'\n') return 1 ;; + \\) (( start++ )) ;; + "$matching") + (( end=start+1, found=start )) + ;; + esac +done + +[[ $BUFFER[end-1] = \\ ]] && (( end++ )) +until [[ $BUFFER[end] == "$matching" ]]; do + [[ $BUFFER[end] = \\ ]] && (( end++ )) + if [[ $BUFFER[end] = $'\n' ]] || (( ++end > $#BUFFER )); then + end=0 + break + fi +done + +if (( alt && (!end || count == 2) )); then + end=found + found=alt +fi +(( end )) || return 1 + +[[ ${${1:-$KEYS}[1]} = a ]] && (( found-- )) || (( end-- )) +(( REGION_ACTIVE = !!REGION_ACTIVE )) +[[ $KEYMAP = vicmd ]] && (( REGION_ACTIVE && end-- )) +MARK=found +CURSOR=end |