-
Notifications
You must be signed in to change notification settings - Fork 1
/
zew-rotate-shell-words
51 lines (40 loc) · 1.34 KB
/
zew-rotate-shell-words
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Transpose shell-words, i.e. parts of lines obtained by (Z) flag, i.e.
# as if zsh parsed the line.
#
# Code to activate the functionality with binding to Alt-t:
# autoload zew-rotate-shell-words
# zle -N zew-rotate-shell-words
# zle -N zew-rotate-shell-words-backwards zew-rotate-shell-words
# bindkey '^[r' zew-rotate-shell-words
# bindkey '^[R' zew-rotate-shell-words-backwards
local curcontext=":zle:$WIDGET"
local MATCH MBEGIN MEND i
# Prepare output variables for zew-process-buffer
local ZEW_PB_WORDS ZEW_PB_WORDS_BEGINNINGS ZEW_PB_SPACES
local ZEW_PB_SELECTED_WORD ZEW_PB_LEFT ZEW_PB_RIGHT
autoload zew-process-buffer
zew-process-buffer "$BUFFER"
# No active shell word found (shouldn't happen) (-1)
# or it's the first shell word (1), or word before first
# shell word (0)? Return
[ "$ZEW_PB_SELECTED_WORD" -le 1 ] && return 0
# Rotate
if [[ "$WIDGET" != *-backwards ]]; then
ZEW_PB_WORDS=( "${ZEW_PB_WORDS[-1]}" "${(@)ZEW_PB_WORDS[1,-2]}" )
else
ZEW_PB_WORDS=( "${(@)ZEW_PB_WORDS[2,-1]}" "${ZEW_PB_WORDS[1]}" )
fi
# Build BUFFER
integer size="${#ZEW_PB_WORDS}"
integer newcursor
buf=""
for (( i=1; i<=size; i++ )); do
buf+="$ZEW_PB_SPACES[i]$ZEW_PB_WORDS[i]"
[ "$i" = "$ZEW_PB_SELECTED_WORD" ] && newcursor="$#buf"
done
# Append final white spaces
buf+="$ZEW_PB_SPACES[i]"
BUFFER="$buf"
CURSOR="$newcursor"
return 0
# vim:ft=zsh