-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIExEditer.vim
566 lines (478 loc) · 14.3 KB
/
IExEditer.vim
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
" =============================================================================
"
" VimPinyin —— Vim 拼音输入法
"
" 作者: ChaiShushan<chaishushan@gmail.com>
" 版权: BSD
"
" =============================================================================
" UTF8无BOM编码
scriptencoding utf-8
" =============================================================================
" IExCommand
" =============================================================================
let s:IExCommand = {}
let s:IExCommand.cmd_name = "rq"
let s:IExCommand.func_name = "VimPinyin_IExCommand_rq"
let s:IExCommand.description = "输入日期"
let s:IExCommand.leading = "alpha"
let s:IExCommand.help = "输入可选日期,例如2008-08-08"
let s:IExCommand.is_valid = 0
function s:IExCommand.New() dict
let obj = copy(self)
call obj.Clear()
return obj
endfunction
function s:IExCommand.Clear() dict
let self.cmd_name = ""
let self.func_name = "VimPinyin_IExCommand_rq"
let self.description = ""
let self.leading = "digit"
let self.help = ""
let self.is_valid = 0
endfunction
function s:IExCommand.IsValid() dict
return self.is_valid
endfunction
function s:IExCommand.SetCmd(cmd_name,func_name,desc, ...) dict
let cmd_name = a:cmd_name
let func_name = a:func_name
let desc = a:desc
let leading = (a:0>0) ? (a:1) : ""
let help = (a:0>1) ? (a:2) : ""
"if !self.CheckCmd(cmd_name,func_name,desc,leading,help)
" return 0
"endif
return 1
endfunction
function s:IExCommand.Do(input) dict
return [ self.IsValid(), [ a:input ] ]
endfunction
" =============================================================================
" IExCommandDB
" =============================================================================
let s:IExCommandDB = {}
function s:IExCommandDB.HasCmd(cmd_name) dict
return 0
endfunction
function s:IExCommandDB.GetCmd(cmd_name) dict
return 0
endfunction
function s:IExCommandDB.SetCmd(cmd_name,func_name,desc, ...) dict
return 0
endfunction
" =============================================================================
" InputContext
" =============================================================================
"{{{
let s:InputContext = {}
" 原始输入字符
let s:InputContext.in_chars = [ 'i', 's', 'j', '?', '?', ]
" 当前光标位置
let s:InputContext.cursor_pos = 5
function s:InputContext.New() dict
let obj = copy(self)
call obj.Clear()
return obj
endfunction
" 是否为输入字符
function s:InputContext.IsInputKey(keycode) dict
if VimPinyin_Common_IsAlpha(a:keycode) | return 1 | endif
return 0
endfunction
function s:InputContext.IsBackspaceKey(keycode) dict
if VimPinyin_Common_IsBackspaceKey(a:keycode) | return 1 | endif
return 0
endfunction
function s:InputContext.IsDeleteKey(keycode) dict
if VimPinyin_Common_IsDeleteKey(a:keycode) | return 1 | endif
return 0
endfunction
function s:InputContext.IsLeftKey(keycode) dict
if VimPinyin_Common_IsLeftKey(a:keycode) | return 1 | endif
return 0
endfunction
function s:InputContext.IsRightKey(keycode) dict
if VimPinyin_Common_IsRightKey(a:keycode) | return 1 | endif
return 0
endfunction
" 是否为空
function s:InputContext.IsEmpty() dict
return len(self.in_chars) == 0
endfunction
" 当前字符数目
function s:InputContext.GetCharsNum() dict
return len(self.in_chars)
endfunction
" 输入新的字符
function s:InputContext.PushChar(keycode) dict
if !self.IsInputKey(a:keycode) | return 0 | endif
if self.GetCharsNum() > 16 | return 0 | endif
call insert(self.in_chars, nr2char(a:keycode), self.cursor_pos)
call self.MoveCursorRight()
return 1
endfunction
" 清空
function s:InputContext.Clear() dict
let self.in_chars = []
let self.cursor_pos = 0
let n = self.GetCharsNum()
endfunction
" 获取光标左边输入的原始字符
function s:InputContext.GetCursorLeftInput() dict
if self.GetCharsNum() <= 0 | return "" | endif
if self.GetCharsNum() == 1 | return "i" | endif
let list = self.in_chars[0:self.cursor_pos-1]
return join(list, "")
endfunction
" 获取光标右边输入的原始字符
function s:InputContext.GetCursorRightInput() dict
if self.GetCharsNum() <= 0 | return "" | endif
if self.cursor_pos < self.GetCharsNum()
let list = self.in_chars[self.cursor_pos:]
return join(list, "")
else
return ""
endif
endfunction
" 获取输入的单词(不含v前缀)
function s:InputContext.GetInputWord() dict
if self.GetCharsNum() <= 0 | return "" | endif
let list = self.in_chars[1:]
return join(list, "")
endfunction
" 光标位置
function s:InputContext.GetCursorPos() dict
return self.cursor_pos
endfunction
" 左右移动光标
function s:InputContext.MoveCursorLeft() dict
let self.cursor_pos = self.cursor_pos-1
call self.AdjustCursorPos()
endfunction
function s:InputContext.MoveCursorRight() dict
let self.cursor_pos = self.cursor_pos+1
call self.AdjustCursorPos()
endfunction
" 删除字符
function s:InputContext.RemoveCursorLeftChar() dict
if self.GetCharsNum() == 1
call remove(self.in_chars, self.cursor_pos-1)
let self.cursor_pos = 0
"call self.MoveCursorLeft()
elseif self.GetCharsNum() > 1
if self.cursor_pos > 1
call remove(self.in_chars, self.cursor_pos-1)
call self.MoveCursorLeft()
endif
endif
let n = self.GetCharsNum()
endfunction
function s:InputContext.RemoveCursorRightChar() dict
if self.GetCharsNum() > 0
if self.cursor_pos < len(self.in_chars)
call remove(self.in_chars, self.cursor_pos)
endif
endif
endfunction
" 调整光标位置到合适值
function s:InputContext.AdjustCursorPos() dict
if self.GetCharsNum() > 1
if self.cursor_pos < 1
let self.cursor_pos = len(self.in_chars)
endif
if self.cursor_pos > len(self.in_chars)
let self.cursor_pos = 1
endif
elseif self.GetCharsNum() == 1
let self.cursor_pos = 1
else
let self.cursor_pos = 0
endif
endfunction
"}}}
" =============================================================================
" OutputContext
" =============================================================================
"{{{
let s:OutputContext = {}
" 输入字符串
let s:OutputContext.in_chars = ""
" 输出列表
let s:OutputContext.out_list = [ "he", "hello", ]
" 总页码
let s:OutputContext.page_num = 1
" 当前页码
let s:OutputContext.page_pos = 0
" 当前光标位置
let s:OutputContext.cursor_pos = 0
" 当前页单词
let s:OutputContext.words_list = []
" 每页候选字最大数目
let s:OutputContext.pageSize = 5
" 屏幕宽度
let s:OutputContext.outputBarWidth = 80
" 构造函数
function s:OutputContext.New() dict
let obj = copy(self)
call obj.Clear()
return obj
endfunction
" 清空
function s:OutputContext.Clear() dict
let self.in_chars = ""
let self.out_list = []
let self.page_num = 1
let self.page_pos = 0
let self.cursor_pos = 0
let self.words_list = []
let self.pageSize = 5
let self.outputBarWidth = g:VimPinyin_vimColumns
endfunction
" 是否为选择键
function s:OutputContext.IsSelectKey(keycode) dict
if VimPinyin_Common_IsEnterKey(a:keycode) | return 1 | endif
if VimPinyin_Common_IsSpace(a:keycode) | return 1 | endif
if VimPinyin_Common_IsDigit(a:keycode)
let idx = a:keycode - char2nr('1')
if idx >= 0 && idx < len(self.words_list)
return 1
endif
endif
if a:keycode == char2nr(';')
let idx = self.cursor_pos + 1
if idx >= 0 && idx < len(self.words_list)
return 1
endif
endif
return 0
endfunction
function s:OutputContext.IsUpKey(keycode) dict
if VimPinyin_Common_IsUpKey(a:keycode) | return 1 | endif
return 0
endfunction
function s:OutputContext.IsDownKey(keycode) dict
if VimPinyin_Common_IsDownKey(a:keycode) | return 1 | endif
return 0
endfunction
function s:OutputContext.IsPageUpKey(keycode) dict
if VimPinyin_Common_IsPageUpKey(a:keycode) | return 1 | endif
return 0
endfunction
function s:OutputContext.IsPageDownKey(keycode) dict
if VimPinyin_Common_IsPageDownKey(a:keycode) | return 1 | endif
return 0
endfunction
" 设置结果
function s:OutputContext.SetList(in_chars, out_list) dict
if !VimPinyin_Common_IsStringType(a:in_chars) | return 0 | endif
if !VimPinyin_Common_IsListType(a:out_list) | return 0 | endif
let self.in_chars = a:in_chars
let self.out_list = a:out_list
let self.page_num = floor((len(a:out_list)+self.pageSize-1)/self.pageSize)
let self.page_pos = 0
let self.cursor_pos = 0
let self.words_list = []
for i in range(len(a:out_list))
call add(self.words_list, a:out_list[i])
if len(self.words_list) >= self.pageSize | break | endif
endfor
return 1
endfunction
function s:OutputContext.SelectByKey(keycode) dict
if VimPinyin_Common_IsEnterKey(a:keycode)
return "i" . self.in_chars
endif
if VimPinyin_Common_IsSpace(a:keycode)
return self.words_list[self.cursor_pos]
endif
if VimPinyin_Common_IsDigit(a:keycode)
let idx = a:keycode - char2nr('1')
if idx >= 0 && idx < len(self.words_list)
return self.words_list[idx]
endif
endif
if a:keycode == char2nr(';')
let idx = self.cursor_pos + 1
if idx >= 0 && idx < len(self.words_list)
return self.words_list[idx]
endif
endif
return ""
endfunction
" 选择操作
function s:OutputContext.GetWordList() dict
return self.words_list
endfunction
function s:OutputContext.GetCursorPos() dict
return self.cursor_pos
endfunction
function s:OutputContext.MovePageUp() dict
let pos = self.page_pos-1
if pos < 0 | let pos = 0 | endif
if pos != self.page_pos
let self.page_pos = pos
let self.cursor_pos = 0
let self.words_list = []
let iStart = self.page_pos * self.pageSize
let iEnd = iStart + self.pageSize
let self.words_list = self.out_list[iStart : iEnd]
endif
endfunction
function s:OutputContext.MovePageDown() dict
let pos = self.page_pos+1
if pos >= self.page_num | let pos = self.page_num-1 | endif
if pos != self.page_pos
let self.page_pos = pos
let self.cursor_pos = 0
let self.words_list = []
let iStart = self.page_pos * self.pageSize
let iEnd = iStart + self.pageSize
let self.words_list = self.out_list[iStart : iEnd]
endif
endfunction
function s:OutputContext.MoveCursorUp() dict
let self.cursor_pos = self.cursor_pos-1
if self.cursor_pos < 0
let self.cursor_pos = len(self.words_list)-1
endif
endfunction
function s:OutputContext.MoveCursorDown() dict
let self.cursor_pos = self.cursor_pos+1
if self.cursor_pos >= len(self.words_list)
let self.cursor_pos = 0
endif
endfunction
"}}}
" =============================================================================
"{{{
function! VimPinyin_IExEditer_MakeQuery(inCtx, outCtx)
let chars = a:inCtx.GetInputWord()
let list = VimPinyin_IExEngine_Search(chars)
if VimPinyin_Common_IsStringType(chars) && strlen(chars) > 0
call a:outCtx.SetList(chars, list)
else
call a:outCtx.SetList("", [])
endif
endfunction
" 显示输入栏
function! VimPinyin_IExEditer_ShowInputBar(inCtx, outCtx)
let in_text = a:inCtx.GetCursorLeftInput()
let in_chars = a:inCtx.GetCursorRightInput()
let out_list = a:outCtx.GetWordList()
let cursor_pos = a:outCtx.GetCursorPos()
call VimPinyin_Common_ClearCommandBar()
"------------------------------------------------------
" [英文] vhe|l >> 1.[hel] 2.help 3.held 4.helpful 5.hello
execute 'echohl Title | echon "["'
execute 'echohl Title | echon "扩展"'
execute 'echohl Title | echon "] "'
" debug
"let in_cursor_pos = a:inCtx.GetCursorPos()
"execute 'echohl None | echon "' . in_cursor_pos . ' "'
"let num = a:inCtx.GetCharsNum()
"execute 'echohl None | echon "-' . num . ' "'
if strlen(in_text) > 0
let chs = split(in_text, '\zs')
let wd = join(chs[1:], "")
execute 'echohl LineNr | echon "' . "i" . '"'
execute 'echohl None | echon "' . wd . '"'
endif
execute 'echohl LineNr | echon "|"'
if strlen(in_chars) > 0
execute 'echohl None | echon "' . in_chars . '"'
endif
execute 'echohl MoreMsg | echon " >> "'
for i in range(len(out_list))
" 超出最大宽度
if getcmdpos() >= (g:VimPinyin_vimColumns-20)
execute 'echohl None | echon " ..."'
break
endif
if i == cursor_pos
execute 'echohl LineNr | echon "' . (i+1) . '."'
execute 'echohl None | echon "["'
execute 'echohl Label | echon "' . out_list[i] . '"'
execute 'echohl None | echon "] "'
else
execute 'echohl LineNr | echon "' . (i+1) . '."'
execute 'echohl None | echon "' . out_list[i] . ' "'
endif
endfor
echohl None
return 1
endfunction
"}}}
" =============================================================================
"{{{
" 处理输入
function! VimPinyin_IExEditer_ProcessInput(keycode_)
let keycode = a:keycode_
let inCtx = s:InputContext.New()
let outCtx = s:OutputContext.New()
while 1
"--------------------------------------------------
" 处理退出控制键
if VimPinyin_Common_IsEscKey(keycode)
return VimPinyin_Common_ReturnNone()
endif
if VimPinyin_Common_IsUnknowKey(keycode)
return VimPinyin_Common_ReturnNone()
endif
"--------------------------------------------------
" 处理输入相关
let bInputChanged = 0
" 输入
if inCtx.IsInputKey(keycode)
call inCtx.PushChar(keycode)
let bInputChanged = 1
elseif inCtx.IsBackspaceKey(keycode)
call inCtx.RemoveCursorLeftChar()
let bInputChanged = 1
elseif inCtx.IsDeleteKey(keycode)
call inCtx.RemoveCursorRightChar()
let bInputChanged = 1
elseif inCtx.IsLeftKey(keycode)
call inCtx.MoveCursorLeft()
elseif inCtx.IsRightKey(keycode)
call inCtx.MoveCursorRight()
endif
" 根据输入更新输出
if bInputChanged
if inCtx.IsEmpty()
return VimPinyin_Common_ReturnNone()
endif
let v = VimPinyin_IExEditer_MakeQuery(inCtx, outCtx)
endif
"--------------------------------------------------
" 处理结果查看键
" 切换后选字
if outCtx.IsUpKey(keycode)
call outCtx.MoveCursorUp()
elseif outCtx.IsDownKey(keycode)
call outCtx.MoveCursorDown()
" 翻页
elseif outCtx.IsPageUpKey(keycode)
call outCtx.MovePageUp()
elseif outCtx.IsPageDownKey(keycode)
call outCtx.MovePageDown()
endif
"--------------------------------------------------
" 处理结果输出键
if outCtx.IsSelectKey(keycode)
return outCtx.SelectByKey(keycode)
endif
"--------------------------------------------------
" 显示输入面板
call VimPinyin_IExEditer_ShowInputBar(inCtx, outCtx)
" 获取新的字符
let keycode = getchar()
"--------------------------------------------------
endwhile
endfunction
" 清空状态
function! VimPinyin_IExEditer_Reset()
endfunction
"}}}
" =============================================================================
"{{{1 vim: foldmethod=marker: