Commit 12e4f64d authored by Frank Terbeck's avatar Frank Terbeck

Add generic jump-to-delim(iter) widget

With this one, it is easy to write widgets that jump to a certain (possibly
multi character) delimiter within a ZLE buffer.
parent d7a9243f
HARNESS = prove --merge --color
TA_TESTS = ./tests/jump-to-delim.t
zwc-clean:
rm -f ~/.zshrc.zwc ~/.zshrc.real.zwc
find ~/.zfunctions -type f -name "*.zwc" -exec rm -f '{}' '+'
test:
$(HARNESS) --verbose $(TA_TESTS)
zsh -f tests/vcsup.t
.PHONY: test zwc-clean
# usage: jump-to-delim DELIM [FORWARD?] [UPTO-ONTO-ACROSS?]
function jumpToDelim () {
local delim="$1"
local -i forward="${2:-1}"
local -i onto="${3:-0}"
# foo///bar///baz
# ^- Cursor
# CURSOR=3
# LBUFFER=foo
# RBUFFER=///bar///baz
if (( forward )); then
(( CURSOR += 2 ))
(( CURSOR = ${RBUFFER[(i)${delim}]} + ${#LBUFFER} ))
(( CURSOR += ${#delim} - 1 ))
(( CURSOR > ${#delim} )) && (( CURSOR -= ${#delim} + 1 ))
if (( onto )); then
(( CURSOR++ ))
(( onto == 2 )) && (( CURSOR += ${#delim} ))
fi
else
# Move back a character: If we're already on an instance of the
# delimiter, we want to be able to move off of it to the next instance.
(( CURSOR-- ))
# This subscript expansion equates to the position of the delimiter in
# the buffer, which is basically "onto == 1" behaviour.
(( CURSOR = ${LBUFFER[(I)${delim}]} - 1 ))
# ...and adjust for upto/across modes.
if (( onto == 0 )); then
(( CURSOR += ${#delim} ))
elif (( onto == 2 )); then
(( CURSOR-- ))
fi
fi
return 0
}
function jump-to-delim () {
local -i num=${NUMERIC:-1}
while (( num-- > 0 )); do
jumpToDelim "$@"
done
return 0
}
jump-to-delim "$@"
#!/bin/zsh
emulate zsh
setopt null_glob
setopt extended_glob
fpath=( $PWD/functions "${fpath[@]}" )
() { (( $# > 0 )) } $^fpath/jump-to-delim(N) || {
printf 'Could not find jump-to-delim in $fpath...\n'
exit 1; }
source ./tests/test-library || {
printf 'Failed to load test-library...\n'
exit 1; }
zpty_run 'autoload -Uz jump-to-delim
function jump-to-next-/ () { jump-to-delim / }
function jump-to-prev-/ () { jump-to-delim / 0 }
function jump-onto-next-/ () { jump-to-delim / 1 1 }
function jump-onto-prev-/ () { jump-to-delim / 0 1 }
function jump-across-next-/ () { jump-to-delim / 1 2 }
function jump-across-prev-/ () { jump-to-delim / 0 2 }
function jump-to-next-// () { jump-to-delim // }
function jump-to-prev-// () { jump-to-delim // 0 }
function jump-onto-next-// () { jump-to-delim // 1 1 }
function jump-onto-prev-// () { jump-to-delim // 0 1 }
function jump-across-next-// () { jump-to-delim // 1 2 }
function jump-across-prev-// () { jump-to-delim // 0 2 }
zle -N jump-to-next-/
zle -N jump-to-prev-/
zle -N jump-onto-next-/
zle -N jump-onto-prev-/
zle -N jump-across-next-/
zle -N jump-across-prev-/
zle -N jump-to-next-//
zle -N jump-to-prev-//
zle -N jump-onto-next-//
zle -N jump-onto-prev-//
zle -N jump-across-next-//
zle -N jump-across-prev-//'
zpty_run 'bindkey "^xb" jump-to-prev-/
bindkey "^xf" jump-to-next-/'
zle-test-define 'One level back to slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb' | zle-results CURSOR=12
zle-test-define 'One level forward to slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf' | zle-results CURSOR=2
zle-test-define 'Two levels back to slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb\C-xb' | zle-results CURSOR=8
zle-test-define 'Two levels forward to slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf\C-xf' | zle-results CURSOR=6
zle-test-define 'Three levels back to slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb\C-xb\C-xb' | zle-results CURSOR=4
zle-test-define 'Three levels forward to slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf\C-xf\C-xf' | zle-results CURSOR=10
zpty_run 'bindkey -r "^xb"
bindkey -r "^xf"'
zpty_run 'bindkey "^xb" jump-onto-prev-/
bindkey "^xf" jump-onto-next-/'
zle-test-define 'One level back onto slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb' | zle-results CURSOR=11
zle-test-define 'One level forward onto slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf' | zle-results CURSOR=3
zle-test-define 'Two levels back onto slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb\C-xb' | zle-results CURSOR=7
zle-test-define 'Two levels forward onto slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf\C-xf' | zle-results CURSOR=7
zle-test-define 'Three levels back onto slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb\C-xb\C-xb' | zle-results CURSOR=3
zle-test-define 'Three levels forward onto slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf\C-xf\C-xf' | zle-results CURSOR=11
zpty_run 'bindkey -r "^xb"
bindkey -r "^xf"'
zpty_run 'bindkey "^xb" jump-across-prev-/
bindkey "^xf" jump-across-next-/'
zle-test-define 'One level back across slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb' | zle-results CURSOR=10
zle-test-define 'One level forward across slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf' | zle-results CURSOR=4
zle-test-define 'Two levels back across slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb\C-xb' | zle-results CURSOR=6
zle-test-define 'Two levels forward across slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf\C-xf' | zle-results CURSOR=8
zle-test-define 'Three levels back across slash'
zletest $'foo/bar/baz/zsh.scm\C-e\C-xb\C-xb\C-xb' | zle-results CURSOR=2
zle-test-define 'Three levels forward across slash'
zletest $'foo/bar/baz/zsh.scm\C-a\C-xf\C-xf\C-xf' | zle-results CURSOR=12
zpty_run 'bindkey -r "^xb"
bindkey -r "^xf"'
zpty_run 'bindkey "^xb" jump-to-prev-//
bindkey "^xf" jump-to-next-//'
zle-test-define 'One level back to slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb' | zle-results CURSOR=15
zle-test-define 'One level forward to slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf' | zle-results CURSOR=2
zle-test-define 'Two levels back to slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb\C-xb' | zle-results CURSOR=10
zle-test-define 'Two levels forward to slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf\C-xf' | zle-results CURSOR=7
zle-test-define 'Three levels back to slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb\C-xb\C-xb' | zle-results CURSOR=5
zle-test-define 'Three levels forward to slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf\C-xf\C-xf' | zle-results CURSOR=12
zpty_run 'bindkey -r "^xb"
bindkey -r "^xf"'
zpty_run 'bindkey "^xb" jump-onto-prev-//
bindkey "^xf" jump-onto-next-//'
zle-test-define 'One level back onto slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb' | zle-results CURSOR=13
zle-test-define 'One level forward onto slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf' | zle-results CURSOR=3
zle-test-define 'Two levels back onto slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb\C-xb' | zle-results CURSOR=8
zle-test-define 'Two levels forward onto slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf\C-xf' | zle-results CURSOR=8
zle-test-define 'Three levels back onto slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb\C-xb\C-xb' | zle-results CURSOR=3
zle-test-define 'Three levels forward onto slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf\C-xf\C-xf' | zle-results CURSOR=13
zpty_run 'bindkey -r "^xb"
bindkey -r "^xf"'
zpty_run 'bindkey "^xb" jump-across-prev-//
bindkey "^xf" jump-across-next-//'
zle-test-define 'One level back across slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb' | zle-results CURSOR=12
zle-test-define 'One level forward across slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf' | zle-results CURSOR=5
zle-test-define 'Two levels back across slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb\C-xb' | zle-results CURSOR=7
zle-test-define 'Two levels forward across slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf\C-xf' | zle-results CURSOR=10
zle-test-define 'Three levels back across slashslash'
zletest $'foo//bar//baz//zsh.scm\C-e\C-xb\C-xb\C-xb' | zle-results CURSOR=2
zle-test-define 'Three levels forward across slashslash'
zletest $'foo//bar//baz//zsh.scm\C-a\C-xf\C-xf\C-xf' | zle-results CURSOR=15
zpty_run 'bindkey -r "^xb"
bindkey -r "^xf"'
zle-test-finish
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment