1
1
--- rime support for neovim
2
2
--- @diagnostic disable : undefined-global
3
- -- luacheck: ignore 112 113 212/self
3
+ -- luacheck: ignore 112 113
4
4
local rime = require " rime"
5
5
local M = require " rime.config"
6
6
7
7
--- setup
8
8
--- @param conf table
9
- function M : setup (conf )
9
+ function M . setup (conf )
10
10
M = vim .tbl_deep_extend (" keep" , conf , M )
11
11
end
12
12
13
13
--- process key. wrap `lua.rime.utils.parse_key`()
14
14
--- @param key string
15
15
--- @param modifiers string[]
16
16
--- @see process_keys
17
- function M : process_key (key , modifiers )
17
+ function M . process_key (key , modifiers )
18
18
modifiers = modifiers or {}
19
19
local keycode , mask = require (" rime.utils" ).parse_key (key , modifiers )
20
20
return M .session_id :process_key (keycode , mask )
24
24
--- @param keys string
25
25
--- @param modifiers string[]
26
26
--- @see process_key
27
- function M : process_keys (keys , modifiers )
27
+ function M . process_keys (keys , modifiers )
28
28
modifiers = modifiers or {}
29
29
for key in keys :gmatch (" (.)" ) do
30
- if M : process_key (key , modifiers ) == false then
30
+ if M . process_key (key , modifiers ) == false then
31
31
return false
32
32
end
33
33
end
36
36
37
37
--- get callback for draw UI
38
38
--- @param key string
39
- function M : callback (key )
39
+ function M . callback (key )
40
40
return function ()
41
41
if vim .b .rime_is_enabled then
42
- return M : draw_ui (key )
42
+ return M . draw_ui (key )
43
43
end
44
44
end
45
45
end
46
46
47
47
--- get rime commit
48
- function M : get_commit_text ()
48
+ function M . get_commit_text ()
49
49
if M .session_id :commit_composition () then
50
50
return M .session_id :get_commit ().text
51
51
end
52
52
return " "
53
53
end
54
54
55
55
--- reset keymaps
56
- function M : reset_keymaps ()
56
+ function M . reset_keymaps ()
57
57
if M .preedit ~= " " and M .has_set_keymaps == false then
58
58
for _ , lhs in ipairs (M .keys .special ) do
59
- vim .keymap .set (" i" , lhs , M : callback (lhs ), { buffer = 0 , noremap = true , nowait = true , })
59
+ vim .keymap .set (" i" , lhs , M . callback (lhs ), { buffer = 0 , noremap = true , nowait = true , })
60
60
end
61
61
M .has_set_keymaps = true
62
62
elseif M .preedit == " " and M .has_set_keymaps == true then
69
69
70
70
--- feed keys
71
71
--- @param text string
72
- function M : feed_keys (text )
72
+ function M . feed_keys (text )
73
73
if vim .v .char ~= " " then
74
74
vim .v .char = text
75
75
else
@@ -81,35 +81,35 @@ function M:feed_keys(text)
81
81
vim .api .nvim_buf_set_text (0 , r - 1 , c , r - 1 , c , { text })
82
82
vim .api .nvim_win_set_cursor (0 , { r , c + # text })
83
83
end
84
- M : win_close ()
84
+ M . win_close ()
85
85
M .preedit = " "
86
- M : reset_keymaps ()
86
+ M . reset_keymaps ()
87
87
end
88
88
89
89
--- draw UI. wrap `lua.rime.utils.draw_ui`()
90
90
--- @param key string
91
- function M : draw_ui (key )
91
+ function M . draw_ui (key )
92
92
if key == " " then
93
93
key = vim .v .char
94
94
end
95
95
if M .preedit == " " then
96
96
for _ , disable_key in ipairs (M .keys .disable ) do
97
97
if key == vim .keycode (disable_key ) then
98
- M : disable ()
99
- M : update_status_bar ()
98
+ M . disable ()
99
+ M . update_status_bar ()
100
100
end
101
101
end
102
102
end
103
- if M : process_key (key , {}) == false then
103
+ if M . process_key (key , {}) == false then
104
104
if # key == 1 then
105
- M : feed_keys (key )
105
+ M . feed_keys (key )
106
106
end
107
107
return
108
108
end
109
- M : update_status_bar ()
109
+ M . update_status_bar ()
110
110
local context = M .session_id :get_context ()
111
111
if context .menu .num_candidates == 0 then
112
- M : feed_keys (M : get_commit_text ())
112
+ M . feed_keys (M . get_commit_text ())
113
113
return
114
114
end
115
115
vim .v .char = " "
@@ -144,11 +144,11 @@ function M:draw_ui(key)
144
144
end
145
145
end
146
146
)
147
- M : reset_keymaps ()
147
+ M . reset_keymaps ()
148
148
end
149
149
150
150
--- close IME window
151
- function M : win_close ()
151
+ function M . win_close ()
152
152
vim .schedule (
153
153
function ()
154
154
if M .win_id ~= 0 and vim .api .nvim_win_is_valid (M .win_id ) then
@@ -160,12 +160,12 @@ function M:win_close()
160
160
end
161
161
162
162
--- clear composition
163
- function M : clear_composition ()
163
+ function M . clear_composition ()
164
164
M .session_id :clear_composition ()
165
165
end
166
166
167
167
--- initial
168
- function M : init ()
168
+ function M . init ()
169
169
if M .session_id == nil then
170
170
vim .fn .mkdir (M .traits .log_dir , " p" )
171
171
local traits = M .traits
@@ -181,23 +181,23 @@ end
181
181
--- enable IME
182
182
--- @see disable
183
183
--- @see toggle
184
- function M : enable ()
185
- M : init ()
184
+ function M . enable ()
185
+ M . init ()
186
186
for _ , nowait_key in ipairs (M .keys .nowait ) do
187
187
vim .keymap .set (" i" , nowait_key , nowait_key , { buffer = 0 , noremap = true , nowait = true })
188
188
end
189
189
190
190
vim .api .nvim_create_autocmd (" InsertCharPre" , {
191
191
group = M .augroup_id ,
192
192
buffer = 0 ,
193
- callback = M : callback (" " ),
193
+ callback = M . callback (" " ),
194
194
})
195
195
vim .api .nvim_create_autocmd ({ " InsertLeave" , " WinLeave" }, {
196
196
group = M .augroup_id ,
197
197
buffer = 0 ,
198
198
callback = function ()
199
- M : clear_composition ()
200
- M : win_close ()
199
+ M . clear_composition ()
200
+ M . win_close ()
201
201
end
202
202
})
203
203
vim .b .rime_is_enabled = true
206
206
--- disable IME
207
207
--- @see enable
208
208
--- @see toggle
209
- function M : disable ()
209
+ function M . disable ()
210
210
for _ , nowait_key in ipairs (M .keys .nowait ) do
211
211
vim .keymap .del (" i" , nowait_key , { buffer = 0 })
212
212
end
@@ -218,26 +218,26 @@ end
218
218
--- toggle IME
219
219
--- @see enable
220
220
--- @see disable
221
- function M : toggle ()
221
+ function M . toggle ()
222
222
if vim .b .rime_is_enabled then
223
- M : disable ()
223
+ M . disable ()
224
224
else
225
- M : enable ()
225
+ M . enable ()
226
226
end
227
- M : update_status_bar ()
227
+ M . update_status_bar ()
228
228
end
229
229
230
230
--- get context with all candidates, useful for `lua.rime.nvim.cmp`
231
231
--- @param keys string
232
232
--- @return table
233
- function M : get_context_with_all_candidates (keys )
234
- M : init ()
235
- M : process_keys (keys , {})
233
+ function M . get_context_with_all_candidates (keys )
234
+ M . init ()
235
+ M . process_keys (keys , {})
236
236
local context = rime .get_context (M .sessionId )
237
237
if (keys ~= ' ' ) then
238
238
local result = context
239
239
while (not context .menu .is_last_page ) do
240
- M : process_key (' =' , {})
240
+ M . process_key (' =' , {})
241
241
context = rime .get_context (M .sessionId )
242
242
result .menu .num_candidates = result .menu .num_candidates + context .menu .num_candidates
243
243
if (result .menu .select_keys and context .menu .select_keys ) then
@@ -248,7 +248,7 @@ function M:get_context_with_all_candidates(keys)
248
248
end
249
249
end
250
250
end
251
- M : clear_composition ()
251
+ M . clear_composition ()
252
252
return context
253
253
end
254
254
@@ -257,15 +257,15 @@ end
257
257
--- @param old string
258
258
--- @param name string
259
259
--- @return string
260
- function M : get_new_symbol (old , name )
260
+ function M . get_new_symbol (old , name )
261
261
if old == M .airline_mode_map .i or old == M .airline_mode_map .ic or old == M .airline_mode_map .ix then
262
262
return name
263
263
end
264
264
return old .. name
265
265
end
266
266
267
267
--- update status bar by `airline_mode_map`. see `help airline`.
268
- function M : update_status_bar ()
268
+ function M . update_status_bar ()
269
269
if vim .g .airline_mode_map then
270
270
if M .airline_mode_map == nil then
271
271
M .airline_mode_map = vim .tbl_deep_extend (" keep" , vim .g .airline_mode_map , M .default .airline_mode_map )
@@ -283,7 +283,7 @@ function M:update_status_bar()
283
283
if schema .schema_id == schema_id then
284
284
for k , _ in pairs (M .default .airline_mode_map ) do
285
285
vim .g .airline_mode_map = vim .tbl_deep_extend (" keep" ,
286
- { [k ] = M : get_new_symbol (M .airline_mode_map [k ], schema .name ) }, vim .g .airline_mode_map )
286
+ { [k ] = M . get_new_symbol (M .airline_mode_map [k ], schema .name ) }, vim .g .airline_mode_map )
287
287
end
288
288
break
289
289
end
0 commit comments