forked from z-shell/zgdbm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrime-ime
More file actions
126 lines (112 loc) · 3.44 KB
/
rime-ime
File metadata and controls
126 lines (112 loc) · 3.44 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
autoload -Uz rime-process-key || return 1
local indices left right left_sep right_sep cursor special preedit
local -i prompt_len
zstyle -a ':rime:ui' indices indices ||
indices=(① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⓪)
zstyle -s ':rime:ui' left left ||
left='<|'
zstyle -s ':rime:ui' right right ||
right='|>'
zstyle -s ':rime:ui' left-sep left_sep ||
left_sep='['
zstyle -s ':rime:ui' right-sep right_sep ||
right_sep=']'
zstyle -s ':rime:ui' cursor cursor ||
cursor='|'
zstyle -s ':rime:ui' prompt-len prompt_len ||
prompt_len=2
zstyle -a ':rime:keys' special special ||
special=('^'{\?.._} '^['{\ ..~} '^[['{A..D}) &&
zmodload zsh/termcap &&
for key in k{u,d,l,r,D,I,P,N,h,\;} @7 k{1..9} F{1..9} F{A..P}; do
special+=("$termcap[$key]")
done &&
zmodload zsh/terminfo &&
for key in kcu{u,d,b,f}1 k{d,i}ch1 k{p,n}p k{home,end} kf{1..35}; do
special+=("$terminfo[$key]")
done
draw-ui() {
rime getContext $1 context_composition context_menu context_menu_candidates_text context_menu_candidates_comment
if (( $#context_menu_candidates_text == 0 )) && rime commitComposition $1; then
rime getCommit $1 commit
LBUFFER+=$commit[text]
fi
local last_line=${LBUFFER##*$'\n'}
local -i ime_len=$((wcswidth($last_line)))
local ime_prefix=${(l:$ime_len:: :)}
local prompt_prefix
if [[ $last_line == $LBUFFER ]]; then
prompt_prefix=${(l:$prompt_len:: :)}
else
prompt_prefix=''
fi
local cursor_pos="$(( context_composition[cursor_pos] + 1 ))"
preedit=$context_composition[preedit]
local text="$prompt_prefix$ime_prefix$preedit[0, cursor_pos - 1]$cursor$preedit[cursor_pos,-1]"
if (( context_menu[num_candidates] )); then
text+=$'\n'"$ime_prefix"
if (( context_menu[page_no] )); then
text+=$left
else
text+=$prompt_prefix
fi
local highlighted_candidate_index=$(( context_menu[highlighted_candidate_index] + 1 ))
local -i i
for i in {1..$context_menu[num_candidates]}; do
if (( highlighted_candidate_index == i )); then
text+=$left_sep
elif (( highlighted_candidate_index + 1 == i )); then
text+=$right_sep
else
text+=' '
fi
text+="$indices[i] $context_menu_candidates_text[i]"
if [[ -n $context_menu_candidates_comment[i] ]]; then
text+=" $context_menu_candidates_comment[i]"
fi
done
if (( highlighted_candidate_index == context_menu[num_candidates] )); then
text+=$right_sep
else
text+=' '
fi
if (( context_menu[is_last_page] == 0 )); then
text+=$right
fi
fi
zle -M -- $text
unset context_composition context_menu context_menu_candidates_text context_menu_candidates_comment
}
self-insert() {
local -i keycode=$(printf %u "'$KEYS'")
local -i is_print=$(( 0x1f < keycode && keycode < 0x7f ))
if (( ! is_print )) && [[ -z $preedit ]]; then
zle ${"$(bindkey -M main "$KEYS")"##* }
else
if ! rime-process-key $rime_session_id "$KEYS" && (( is_print )); then
LBUFFER+="$KEYS"
fi
fi
draw-ui $rime_session_id
}
zmodload zi/wc
zle -N self-insert
zle -A rime-ime save-rime-ime
zle -A accept-line rime-ime
bindkey -N rime main
local key
for key in $special; do
bindkey -M rime "$key" self-insert
done
zle recursive-edit -K rime
bindkey -D rime
zle -M ''
integer stat=$?
zle -A .self-insert self-insert
zle -A save-rime-ime rime-ime
zle -D save-rime-ime
unfunction self-insert draw-ui
(( stat )) && zle send-break
rime getCurrentSchema $rime_session_id
return $stat
# ex: filetype=zsh