-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
558 lines (486 loc) · 11.4 KB
/
Copy pathinit.lua
File metadata and controls
558 lines (486 loc) · 11.4 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
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
VERSION = "0.0.7"
local micro = import("micro")
local config = import("micro/config")
local buffer = import("micro/buffer")
local plug_path = config.ConfigDir .. "/plug/?.lua"
if not package.path:find(plug_path, 1, true) then
package.path = package.path .. ";" .. plug_path
end
local bell = require("skk/bell")
local download = require("skk/download")
local romaji = require("skk/romaji")
local jisyo = require("skk/jisyo")
local kana = require("skk/kana")
local d
-- internal constants
local TEXT_EVENT_INSERT = 1
local TEXT_EVENT_REPLACE = 0
local TEXT_EVENT_REMOVE = -1
-- romaji modes
local DIRECT_MODE = 0 -- no conversion
local HIRAGANA_MODE = 1
local KATAKANA_MODE = 2
local ALPHABET_MODE = 3 -- wide alphabet
-- conversion modes
local CONV_NONE = 0
local CONV_START = 1
local CONV_OKURI = 2
local CONV_ENGLISH = 3
-- states
local romaji_mode = DIRECT_MODE
local kana_buffer = ""
local conv_mode = CONV_NONE
local conv_buffer = ""
local conv_list = nil
local conv_index = 1
local conv_cand = ""
local conv_okuri = ""
local function show_mode()
local mark
if romaji_mode == DIRECT_MODE then
mark = "aA"
elseif romaji_mode == HIRAGANA_MODE then
mark = "あ"
elseif romaji_mode == KATAKANA_MODE then
mark = "ア"
elseif romaji_mode == ALPHABET_MODE then
mark = "aA"
else
bell.program_error("invalid romaji_mode == " .. romaji_mode)
return
end
local head = ""
if conv_mode ~= CONV_NONE then
if #conv_cand > 0 then
head = "▼" .. conv_cand
else
head = "▽" .. conv_buffer
end
end
local buf
if #conv_okuri > 0 then
buf = conv_okuri
else
buf = kana_buffer
end
micro.InfoBar():Message("[" .. mark .. "]" .. head .. buf)
end
local function reset_conv()
conv_mode = CONV_NONE
conv_buffer = ""
conv_list = nil
conv_index = 1
conv_cand = ""
conv_okuri = ""
end
function Skk()
if not d then
d, err = jisyo.open(config.ConfigDir .. "/skk-dict/SKK-JISYO.L.cdb")
if err then
micro.InfoBar():Error("Error in loading SKK dictionaries.")
end
end
if conv_mode ~= CONV_NONE then
local out = (conv_cand ~= "" and conv_cand) or conv_buffer
out = out .. conv_okuri
if #out > 0 then
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local loc = buffer.Loc(cursor.X, cursor.Y)
micro.CurPane().Buf:Insert(loc, out)
end
end
romaji_mode = HIRAGANA_MODE
kana_buffer = ""
reset_conv()
if d then
show_mode()
end
end
function SkkDirect(pane)
romaji_mode = DIRECT_MODE
kana_buffer = ""
if conv_mode ~= CONV_NONE then
local out = conv_cand ~= "" and conv_cand or conv_buffer
out = out .. conv_okuri
if #out > 0 then
local buf = pane.Buf
local cursor = buf:GetActiveCursor()
local loc = buffer.Loc(cursor.X, cursor.Y)
micro.CurPane().Buf:Insert(loc, out)
end
reset_conv()
end
show_mode()
return
end
function SkkGet()
download.defaults()
end
local function bytes_to_string(array)
local buf = {}
for i = 1, #array do
table.insert(buf, string.char(array[i]))
end
return table.concat(buf)
end
local function utf8_chop(s)
local i = #s
while i > 0 do
local b = s:byte(i)
if b < 128 or b >= 192 then
return s:sub(1, i - 1)
end
i = i - 1
end
return ""
end
local vowels = {
["あ"] = "a",
["い"] = "i",
["う"] = "u",
["え"] = "e",
["お"] = "o",
}
function onBeforeTextEvent(buf, ev)
if romaji_mode == DIRECT_MODE then
return true
end
-- expects only backspace
if ev.EventType == TEXT_EVENT_REMOVE then
if #kana_buffer > 0 then
kana_buffer = string.sub(kana_buffer, 1, -2)
show_mode()
local cursor = micro.CurPane().Buf:GetActiveCursor()
cursor:ResetSelection()
cursor.X = cursor.X + 1
return false
elseif #conv_okuri > 0 then
conv_okuri = utf8_chop(conv_okuri)
show_mode()
local cursor = micro.CurPane().Buf:GetActiveCursor()
cursor:ResetSelection()
cursor.X = cursor.X + 1
return false
elseif conv_mode ~= CONV_NONE and #conv_buffer > 0 then
conv_buffer = utf8_chop(conv_buffer)
if #conv_buffer < 1 then
conv_mode = CONV_NONE
end
conv_list = nil
conv_index = 1
conv_cand = ""
show_mode()
local cursor = micro.CurPane().Buf:GetActiveCursor()
cursor:ResetSelection()
cursor.X = cursor.X + 1
return false
else
show_mode()
return true
end
end
if ev.EventType == TEXT_EVENT_REPLACE then
return true
end
-- assert
if ev.EventType ~= TEXT_EVENT_INSERT then
bell.program_error("invalid ev.EventType == " .. ev.EventType)
return true
end
if #ev.Deltas ~= 1 then
return true
end
local delta = ev.Deltas[1]
local output = ""
-- Text is byte array
local text = bytes_to_string(delta.Text)
-- pass through pasted long text
if #text ~= 1 then
return true
end
if romaji_mode == ALPHABET_MODE then
local alphabet = romaji.to_alphabet[text]
if alphabet then
delta.Text = output .. alphabet
end
return true
end
-- now in Hiragana or Katakana mode
if text == "L" then
romaji_mode = ALPHABET_MODE
kana_buffer = ""
if conv_mode ~= CONV_NONE then
local out = conv_cand ~= "" and conv_cand or conv_buffer
out = out .. conv_okuri
output = output .. out
reset_conv()
end
show_mode()
delta.Text = output
return true
end
if text == "\n" and conv_mode ~= CONV_NONE then
local out = conv_cand ~= "" and conv_cand or conv_buffer
out = out .. conv_okuri
output = output .. out
reset_conv()
show_mode()
delta.Text = output
return true
end
if text == " " and conv_mode ~= CONV_NONE then
if not d then
micro.InfoBar():Message("No SKK dictionaries.")
delta.Text = output
return true
end
if not conv_list then
if conv_mode == CONV_OKURI then
conv_list = d:lookup_okuri(conv_buffer)
else
conv_list = d:lookup(conv_buffer)
end
conv_index = 1
if #conv_list < 1 then
micro.InfoBar():Message("SKK: 候補なし")
delta.Text = output
return true
end
else
conv_index = conv_index + 1
if conv_index > #conv_list then
conv_index = #conv_list
end
end
conv_cand = conv_list[conv_index] or ""
conv_cand = conv_cand:match("^([^;]*)")
show_mode()
delta.Text = output
return true
end
if text == "x" and conv_list then
conv_index = conv_index - 1
if conv_index < 1 then
conv_list = nil
conv_index = 1
conv_cand = ""
conv_okuri = ""
else
conv_cand = conv_list[conv_index] or ""
conv_cand = conv_cand:match("^([^;]*)")
end
show_mode()
delta.Text = output
return true
end
if text == "/" then
if conv_list then
local out = conv_cand ~= "" and conv_cand or conv_buffer
out = out .. conv_okuri
output = output .. out
reset_conv()
end
conv_mode = CONV_ENGLISH
show_mode()
delta.Text = output
return true
end
if conv_mode == CONV_ENGLISH then
if conv_list then
local out = conv_cand ~= "" and conv_cand or conv_buffer
out = out .. conv_okuri
output = output .. out
reset_conv()
else
conv_buffer = conv_buffer .. text
show_mode()
delta.Text = output
return true
end
end
if #conv_cand > 0 then
if conv_mode == CONV_OKURI then
if #conv_okuri > 0 and not conv_okuri:match("っ$") and not conv_okuri:match("ッ$") then
output = output .. conv_cand .. conv_okuri
reset_conv()
kana_buffer = ""
end
else
output = output .. conv_cand .. conv_okuri
reset_conv()
end
end
if text:match("^[A-Z]$") then
if conv_mode == CONV_NONE then
conv_mode = CONV_START
elseif conv_mode == CONV_START then
conv_mode = CONV_OKURI
end
text = text:lower()
end
local kigou = romaji.to_kigou[text]
if kigou then
if conv_mode ~= CONV_NONE and kigou == "ー" then
if not conv_list then
conv_buffer = conv_buffer .. kigou
show_mode()
delta.Text = output
return true
end
end
kana_buffer = ""
show_mode()
delta.Text = output .. kigou
return true
end
if not text:match("^%a$") then
if conv_mode ~= CONV_NONE then
local out = conv_cand ~= "" and conv_cand or conv_buffer
out = out .. conv_okuri
output = output .. out
reset_conv()
end
if #output > 0 then
show_mode()
end
delta.Text = output .. text
return true
end
if text == "l" then
romaji_mode = DIRECT_MODE
kana_buffer = ""
if conv_mode ~= CONV_NONE then
local out = conv_cand ~= "" and conv_cand or conv_buffer
out = out .. conv_okuri
output = output .. out
reset_conv()
end
show_mode()
delta.Text = output
return true
end
if text == "q" then
if conv_mode == CONV_NONE then
if romaji_mode == HIRAGANA_MODE then
romaji_mode = KATAKANA_MODE
elseif romaji_mode == KATAKANA_MODE then
romaji_mode = HIRAGANA_MODE
else
bell.program_error("q: invalid romaji_mode == " .. romaji_mode)
return true
end
show_mode()
delta.Text = output
return true
elseif conv_mode == CONV_START then
if romaji_mode == HIRAGANA_MODE then
local kata = kana.to_kata(conv_buffer)
conv_list = { kata }
conv_index = 1
conv_cand = kata
elseif romaji_mode == KATAKANA_MODE then
local hira = kana.to_hira(conv_buffer)
conv_list = { hira }
conv_index = 1
conv_cand = hira
else
bell.program_error("q (conv): invalid romaji_mode == " .. romaji_mode)
return true
end
show_mode()
delta.Text = output
return true
end
end
kana_buffer = kana_buffer .. text
local kana
if romaji.is_sokuon[kana_buffer] then
if romaji_mode == HIRAGANA_MODE then
kana = "っ"
elseif romaji_mode == KATAKANA_MODE then
kana = "ッ"
else
bell.program_error("sokuon: invalid romaji_mode == " .. romaji_mode)
kana = nil
end
kana_buffer = string.sub(kana_buffer, 2)
elseif romaji.is_n[kana_buffer] then
if romaji_mode == HIRAGANA_MODE then
kana = "ん"
elseif romaji_mode == KATAKANA_MODE then
kana = "ン"
else
bell.program_error("n: invalid romaji_mode == " .. romaji_mode)
kana = nil
end
kana_buffer = string.sub(kana_buffer, 2)
else
local lookup = kana_buffer
if romaji.aliases[lookup] then
lookup = romaji.aliases[lookup]
end
if romaji_mode == HIRAGANA_MODE then
kana = romaji.to_hiragana[lookup]
elseif romaji_mode == KATAKANA_MODE then
kana = romaji.to_katakana[lookup]
else
bell.program_error("kana: invalid romaji_mode == " .. romaji_mode)
kana = nil
end
if kana then
kana_buffer = ""
end
end
if conv_mode == CONV_NONE then
if kana then
delta.Text = output .. kana
else
delta.Text = output
end
show_mode()
return true
elseif conv_mode == CONV_START then
if kana then
conv_buffer = conv_buffer .. kana
conv_list = nil
conv_index = 1
conv_cand = ""
end
show_mode()
delta.Text = output
return true
elseif conv_mode == CONV_OKURI then
local vowel = vowels[kana]
if vowel then
conv_buffer = conv_buffer .. vowel
conv_okuri = conv_okuri .. kana
elseif kana then
conv_okuri = conv_okuri .. kana
else
conv_buffer = conv_buffer .. text
end
if not d then
micro.InfoBar():Message("Please wait.. Loading SKK dictionaries..")
delta.Text = output
return true
end
conv_list = d:lookup_okuri(conv_buffer)
conv_index = 1
conv_cand = conv_list[conv_index] or ""
conv_cand = conv_cand:match("^([^;]*)")
show_mode()
delta.Text = output
return true
else
bell.program_error("onBeforeTextEvent: invalid conv_mode == " .. conv_mode)
return true
end
end
function init()
config.MakeCommand("skk", Skk, config.NoComplete)
config.MakeCommand("skkdirect", SkkDirect, config.NoComplete)
config.MakeCommand("skkget", SkkGet, config.NoComplete)
config.TryBindKey("Ctrl-j", "lua:skk.Skk", false)
config.AddRuntimeFile("skk", config.RTHelp, "help/skk.md")
end