Class: Textbringer::TCodeInputMethod
- Inherits:
-
InputMethod
show all
- Defined in:
- lib/textbringer/input_methods/t_code_input_method.rb,
lib/textbringer/input_methods/t_code_input_method/tables.rb
Constant Summary
collapse
- BUSHU_PATH =
File.expand_path("bushu.rev", data_dir)
- BUSHU_DIC =
{}
- MAZEGAKI_PATH =
File.expand_path("mazegaki.dic", data_dir)
- MAZEGAKI_DIC =
{}
- MAZEGAKI_MAX_WORD_LEN =
12
- MAZEGAKI_MAX_SUFFIX_LEN =
4
- KEYBOARD =
"1234567890" "qwertyuiop" "asdfghjkl;" "zxcvbnm,./"
- KEY_TABLE =
KEYBOARD.each_char.with_index.map { |c, i|
[c, i]
}.to_h
- KANJI_TABLE =
[
"■■■■■■■■■■ヮヰヱヵヶ請境系探象ゎゐゑ■■盛革突温捕■■■■■依繊借須訳",
"■■■■■■■■■■丑臼宴縁曳尚賀岸責漁於汚乙穏■益援周域荒■■■■■織父枚乱香",
"■■■■■■■■■■鬼虚狭脅驚舎喜幹丘糖奇既菊却享康徒景処ぜ■■■■■譲ヘ模降走",
"■■■■■■■■■■孤誇黄后耕布苦圧恵固巧克懇困昏邦舞雑漢緊■■■■■激干彦均又",
"■■■■⑤■■■■■奉某貌卜■姿絶密秘押■■■■■衆節杉肉除■■■■■測血散笑弁",
"■■■■■■■■■■湖礼著移郷■■■■■償欧努底亜■■■■■禁硝樹句礎■■■■■",
"■■■■■■■■■■端飾郵塩群■星析遷宣紅傷豪維脱鼠曹奏尊■絹被源願臨■■■■■",
"■■■■◈■■■■■刷寿順危砂庶粧丈称蒸舗充喫腕暴■■■■■批慶渉竜併■■■■■",
"■■■■■■■■■■震扱片札乞■乃如尼帳輪倒操柄魚■■■■■就駐揮丹鮮■■■■■",
"■■■■■■■■■■弘痛票訴遺欄龍略慮累則存倍牛釈■■■■■綱潟創背皮■■■■■",
"ヲ哀暇啓把酸昼炭稲湯果告策首農歩回務島開報紙館夜位給員ど代レ欠夏彼妻善相家的対歴",
"ゥ逢牙掲伐貿捜異隣旧概買詳由死キせ区百木音王放々応分よル千ア財針裏居差付プばュ作",
"ヴ宛壊携避攻焼闘奈夕武残両在!や出タ手保案曲情引職7か(トれ従骨厚顔量内工八テ見",
"ヂ囲較劇卑盤帯易速拡風階能論増コ山者発立横興刺側覚きっ日国二適類御宇推九名川機チ",
"ヅ庵寒賢藩汽換延雪互細古利ペゃナ金マ和女崎白ぐ官球上く8え年母奥因酒伸サ建パ第入",
"簡徴触宗植■索射濁慢害賃整軽評佐法数郎談服声任検豊美題井洋実爆仲茶率比昔短岩巨敗",
"承章候途複■冊需詑迷撃折追隊角接備最急験変審改昇芸宿制集安画陽構旅施曜遠ォ将ぞ塚",
"快否歯筆里■皿輯蓄戻浴秀糸春幸記朝知ワ送限研労統役セ運ツ特谷ァ導認健尾序振練念働",
"包納頼逃寝■賛瞬貯羊積程断低減モ資士費ィ逆企精ざ印神び打勤ャ殺負何履般耳授版効視",
"唱暮憲勉罪■■盾虫■故鉱提児敷無石屋解募令違装然確優公品語演券悪秋非便示即難普辺",
"ぱ慰我兼菱桜瀬鳥催障収際太園船中スもお定種岡結進真3と〇てるヒ江別考権ッ人三京ち",
"ぴ為掛嫌紐典博筋忠乳若雄査ふ賞わラ東生ろ宅熟待取科ーした一が及久蔵早造ロク万方フ",
"ぷ陰敢顕描採謡希仏察指氏丸続ェう4)十リ料土活ね参い、の51投義算半県んまンつ四",
"ぺ隠甘牽憤君純副盟標ぎ格次習火あこ6学月受予切育池。◆0・2込沢軍青清けイす電地",
"ぽ胃患厳弊犯余堀肩療思術広門聞本さら高シ英ボ加室少ではになを転空性使級業時「長み",
"朱遅甲致汎■衰滋沈己病終起路越む南原駅物勢必講愛管要設水藤有素兵専親寮ホ共ブ平楽",
"陣鶴鹿貨絡■趨湿添已常張薬防得ケ式戦関男輸形助◇流連鉄教力ベ毛永申袋良私ゴ来信午",
"眼繁誌招季■垂甚徹巳寺質づ港条話座線ダ橋基好味宝争デ現エ他度等浅頃落命村ガ製校ご",
"執紹夢卸阿■粋■爪巴停領容玉右べ民ソ点遇足草築観言車成天世文板客師税飛ノ完重約各",
"岳刑弱雲窓■寸瞳陶■河置供試席期ゾ歳強係婦段衛額渋主映書可へ伝庭課着坂近外米ョ光",
"ぁ■瓦■■呼幅歓功盗徳渡守登退店持町所ほ件友卒初慣行ド円小ジヨ誤証含%海道ず西げ",
"ぃ■■■■紀破郡抗幡械刊訪融雨全じ自議明宮伊求技写通カ社野同判規感値ギ当理メウグ",
"ぅ■■■■房績識属衣帝始了極熱バ部六経動局頭配黒院だり—め大済吉ゆ器照不合面政オ",
"ぇ■■■■去疑ぢ綿離読鈴恐督況後間場ニ産向府富直倉新」9子五説週号葉派委化ビ目市",
"ぉ■■■■秒範核影麻族丁未才返問ム七住北割ぶ番望元事田会前そ休省央福毎気売下都株",
"欲巣茂述朗■■■■■帰庁昨跡ゲ洗羽個医静億録赤想消支協用表正図挙険ゼ波ヤ心界意今",
"迫災恋脳老■■■■■監寄裁達芝響忘討史環色貸販編仕先多商ハ交之末ぼ街免再ネ〜口台",
"留列刻豆看■■■■■竹注介具失司迎華許補左態花栄ザ調混ポ決ミ州払乗庫状団計夫食総",
"替沼?辞献■■■■■ゅ修究答養復並浦ユ冷ぬ展警型誰組選党択体例満津準遊戸ひょ価与",
"還更占箱矢■■■■■志抜航層深担陸巻競護根様独止堂銀以ヌ営治字材過諸単身ピ勝反ズ"
]
- MAZEGAKI_STROKE_PRIORITY_LIST =
Key positions
0 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
[
22, 23, 21, 24, 20,
12, 13, 11, 14, 10,
27, 26, 28, 25, 29,
17, 16, 18, 15, 19
]
Constants included
from Commands
Commands::CLIPBOARD_AVAILABLE, Commands::CTAGS, Commands::HELP_RING, Commands::ISEARCH_STATUS, Commands::KEYBOARD_MACROS, Commands::REGISTERS, Commands::RE_SEARCH_STATUS
Constants included
from Utils
Utils::COMPLETION, Utils::EXPRESSION_COMPLETOR, Utils::EXPRESSION_COMPLETOR_OPTIONS, Utils::HOOKS
Instance Method Summary
collapse
Methods inherited from InputMethod
#disable, #enabled?, #filter_event, find, inherited, list, #toggle, #with_target_buffer
Methods included from Commands
[], #command_help, command_table, #current_prefix_arg, define_command, #execute_keyboard_macro, #get_tags, #isearch_done, #isearch_mode, #isearch_mode?, #isearch_pre_command_hook, #isearch_prompt, #isearch_repeat, #isearch_repeat_backward, #isearch_repeat_forward, #isearch_search, #keymap_bindings, list, #match_beginning, #match_end, #match_string, #number_prefix_arg, #prefix_numeric_value, #read_input_method_name, #read_keyboard_macro, #read_register, #replace_match, undefine_command, #universal_argument_mode
Methods included from Utils
add_hook, background, complete_for_minibuffer, foreground, foreground!, get_hooks, message, read_buffer, read_char, read_command_name, read_encoding, read_event, read_expression, read_file_name, read_from_minibuffer, read_key_sequence, read_object, read_single_char, received_keyboard_quit?, remove_hook, ruby_install_name, run_hooks, run_hooks_in, self_insert_and_exit_minibuffer, set_transient_map, show_exception, sit_for, sleep_for, y_or_n?, yes_or_no?
Constructor Details
Returns a new instance of TCodeInputMethod.
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 13
def initialize
super
@prev_key_index = nil
@mazegaki_start_pos = nil
@mazegaki_candidates = nil
@delete_help_window = false
@help_window = nil
@prev_buffer = nil
setup_dictionaries
end
|
Instance Method Details
#bushu_compose ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 89
def bushu_compose
with_target_buffer do |buffer|
pos = buffer.point
s = 2.times.map {
buffer.backward_char
buffer.char_after
}.sort.join
c = BUSHU_DIC[s]
if c
buffer.replace(c, start: buffer.point, end: pos)
else
buffer.goto_char(pos)
end
end
Window.redisplay
nil
end
|
#find_mazegaki_start_pos(with_inflection) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 180
def find_mazegaki_start_pos(with_inflection)
with_target_buffer do |buffer|
buffer.save_excursion do
pos = buffer.point
start_pos = nil
yomi = nil
MAZEGAKI_MAX_WORD_LEN.times do
break if buffer.beginning_of_buffer?
buffer.backward_char
s = buffer.substring(buffer.point, pos)
y = mazegaki_lookup_yomi(s, with_inflection)
if y
start_pos = buffer.point
yomi = y
end
end
return start_pos, yomi
end
end
end
|
#handle_event(event) ⇒ Object
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
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 51
def handle_event(event)
key_index = KEY_TABLE[event]
if @mazegaki_start_pos
if process_mazegaki_conversion(event, key_index)
return nil
end
end
if key_index.nil?
@prev_key_index = nil
if event.is_a?(String) && /\A[A-Z]\z/.match?(event)
return event.downcase
else
return event
end
end
if @prev_key_index.nil?
@prev_key_index = key_index
nil
else
c = KANJI_TABLE[key_index][@prev_key_index]
@prev_key_index = nil
case c
when ?■
nil
when ?◆
bushu_compose
when ?◇
start_mazegaki_conversion(false)
when ?◈
start_mazegaki_conversion(true)
when ?⑤
show_stroke
else
c
end
end
end
|
#hide_help_window ⇒ Object
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 263
def hide_help_window
if @delete_help_window
Window.delete_window(@help_window)
elsif @prev_buffer
@help_window.buffer = @prev_buffer
end
@delete_help_window = false
@help_window = nil
@prev_buffer = nil
end
|
#mazegaki_convert(pos, yomi) ⇒ Object
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
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 116
def mazegaki_convert(pos, yomi)
with_target_buffer do |buffer|
candidates = mazegaki_lookup_candidates(yomi)
if candidates
@mazegaki_yomi = yomi
@mazegaki_suffix = buffer.substring(pos + yomi.bytesize,
buffer.point)
case candidates.size
when 1
buffer.composite_edit do
buffer.delete_region(pos, buffer.point)
buffer.insert("△" + candidates[0] + @mazegaki_suffix)
end
when 2
buffer.composite_edit do
buffer.delete_region(pos, buffer.point)
buffer.insert("△{" + candidates.join(",") + "}" +
@mazegaki_suffix)
end
else
buffer.save_excursion do
buffer.goto_char(pos)
buffer.insert("△")
end
end
@mazegaki_start_pos = pos
@mazegaki_candidates = candidates
@mazegaki_candidates_page = 0
if candidates.size > 2
show_mazegaki_candidates
end
end
Window.redisplay
nil
end
end
|
#mazegaki_finish(s) ⇒ Object
253
254
255
256
257
258
259
260
261
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 253
def mazegaki_finish(s)
mazegaki_reset
with_target_buffer do |buffer|
buffer.composite_edit do
buffer.delete_region(@mazegaki_start_pos, buffer.point)
buffer.insert(s + @mazegaki_suffix)
end
end
end
|
#mazegaki_lookup_candidates(yomi) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 165
def mazegaki_lookup_candidates(yomi)
if @mazegaki_convert_with_inflection
s = yomi + "—"
else
s = yomi
end
c = MAZEGAKI_DIC[s]
return nil if c.nil?
candidates = c.split("/").map { |i|
i.sub(/;.*/, "")
}.reject(&:empty?)
return nil if candidates.empty?
candidates
end
|
#mazegaki_lookup_yomi(s, with_inflectin) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 153
def mazegaki_lookup_yomi(s, with_inflectin)
if !with_inflectin
return MAZEGAKI_DIC.key?(s) ? s : nil
end
yomi = s.sub(/\p{hiragana}\z/, "")
(MAZEGAKI_MAX_SUFFIX_LEN + 1).times do
return yomi if MAZEGAKI_DIC.key?(yomi + "—")
break if !yomi.sub!(/\p{hiragana}\z/, "")
end
nil
end
|
#mazegaki_next_page ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 274
def mazegaki_next_page
if @mazegaki_candidates.size <= mazegaki_limit
return
end
@mazegaki_candidates_page += 1
if @mazegaki_candidates_page * mazegaki_limit >
@mazegaki_candidates.size
@mazegaki_candidates_page = 0
end
show_mazegaki_candidates
Window.redisplay
end
|
#mazegaki_relimit_left ⇒ Object
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
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 287
def mazegaki_relimit_left
with_target_buffer do |buffer|
yomi = nil
start_pos = nil
mazegaki_reset
buffer.save_excursion do
pos = buffer.point
buffer.goto_char(@mazegaki_start_pos)
s = buffer.substring(buffer.point, pos)
(MAZEGAKI_MAX_WORD_LEN - s.size).times do
break if buffer.beginning_of_buffer?
buffer.backward_char
s = buffer.substring(buffer.point, pos)
yomi = mazegaki_lookup_yomi(s, @mazegaki_convert_with_inflection)
if yomi
start_pos = buffer.point
break
end
end
if start_pos.nil?
message("Can't relimit left")
start_pos = @mazegaki_start_pos
yomi = @mazegaki_yomi
end
end
mazegaki_convert(start_pos, yomi)
Window.redisplay
end
end
|
#mazegaki_relimit_right ⇒ Object
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
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 317
def mazegaki_relimit_right
with_target_buffer do |buffer|
start_pos = nil
yomi = nil
mazegaki_reset
buffer.save_excursion do
pos = buffer.point
buffer.goto_char(@mazegaki_start_pos)
if @mazegaki_convert_with_inflection && @mazegaki_yomi &&
(yomi = mazegaki_lookup_yomi(@mazegaki_yomi, true))
start_pos = @mazegaki_start_pos
else
loop do
break if buffer.point >= pos
buffer.forward_char
s = buffer.substring(buffer.point, pos)
yomi = mazegaki_lookup_yomi(s, @mazegaki_convert_with_inflection)
if yomi
start_pos = buffer.point
break
end
end
end
end
if start_pos.nil?
if !@mazegaki_convert_with_inflection
start_pos, yomi = find_mazegaki_start_pos(true)
if start_pos
@mazegaki_convert_with_inflection = true
end
end
if start_pos.nil?
message("Can't relimit right")
start_pos = @mazegaki_start_pos
yomi = @mazegaki_yomi
end
end
mazegaki_convert(start_pos, yomi)
Window.redisplay
end
end
|
#mazegaki_reset ⇒ Object
243
244
245
246
247
248
249
250
251
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 243
def mazegaki_reset
with_target_buffer do |buffer|
buffer.undo
pos = @mazegaki_start_pos +
@mazegaki_yomi.bytesize + @mazegaki_suffix.bytesize
buffer.goto_char(pos)
hide_help_window
end
end
|
#process_mazegaki_conversion(event, key_index) ⇒ Object
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
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 201
def process_mazegaki_conversion(event, key_index)
case event
when " "
mazegaki_next_page
return true
when "<"
mazegaki_relimit_left
return true
when ">"
mazegaki_relimit_right
return true
end
begin
if @mazegaki_candidates.size == 1
if event == "\C-m"
mazegaki_finish(@mazegaki_candidates[0])
return true
elsif key_index
mazegaki_finish(@mazegaki_candidates[0])
return false
end
elsif key_index
mazegaki_limit = MAZEGAKI_STROKE_PRIORITY_LIST.size
i = MAZEGAKI_STROKE_PRIORITY_LIST.index(key_index)
if i
offset = @mazegaki_candidates_page * mazegaki_limit + i
c = @mazegaki_candidates[offset]
if c
mazegaki_finish(c)
return true
end
end
end
mazegaki_reset
true
ensure
@mazegaki_start_pos = nil
@mazegaki_candidates = nil
Window.redisplay
end
end
|
#setup_dictionaries ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 24
def setup_dictionaries
data_dir = CONFIG[:t_code_data_dir] ||
File.expand_path("~/.textbringer/tcode")
bushu_path = File.join(data_dir, "bushu.rev")
mazegaki_path = File.join(data_dir, "mazegaki.dic")
if BUSHU_DIC.empty?
File.open(bushu_path) do |f|
f.each_line do |line|
x, *xs = line.chomp.chars
BUSHU_DIC[xs.sort.join] = x
end
end
end
if MAZEGAKI_DIC.empty?
File.open(mazegaki_path) do |f|
f.each_line do |line|
x, y = line.split
MAZEGAKI_DIC[x] = y
end
end
end
end
|
#show_help(message) ⇒ Object
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
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 417
def show_help(message)
buffer = Buffer.find_or_new("*T-Code Help*",
undo_limit: 0, read_only: true)
buffer.read_only_edit do
buffer.clear
buffer.insert(message)
buffer.beginning_of_buffer
end
if Window.list.size == 1
Window.list.first.split(message.lines.size + 1)
@delete_help_window = true
end
if Window.current.echo_area?
window = Window.list.last
else
windows = Window.list
i = (windows.index(Window.current) + 1) % windows.size
window = windows[i]
end
@help_window = window
if window.buffer != buffer
@prev_buffer = window.buffer
window.buffer = buffer
end
end
|
#show_mazegaki_candidates ⇒ Object
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
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 359
def show_mazegaki_candidates
offset = @mazegaki_candidates_page * mazegaki_limit
candidates = @mazegaki_candidates[offset, mazegaki_limit]
xs = Array.new(40, "-")
candidates.each_with_index do |s, i|
xs[MAZEGAKI_STROKE_PRIORITY_LIST[i]] = s
end
max_width = candidates.map { |s|
Buffer.display_width(s)
}.max
page = @mazegaki_candidates_page + 1
page_count =
(@mazegaki_candidates.size.to_f / mazegaki_limit).ceil
message = xs.map.with_index { |s, i|
space = " " * (max_width - Buffer.display_width(s))
if i % 10 < 5
s + space
else
space + s
end
}.each_slice(10).map.with_index { |ys, i|
if i == 0
" " + ys[0, 4].join(" ") + " " + ys[4, 2].join(" ") + " " +
ys[6, 4].join(" ")
else
"[" + ys[0, 4].join(" ") + "] " + ys[4, 2].join(" ") + " [" +
ys[6, 4].join(" ") + "]"
end
}.join("\n") + " (#{page}/#{page_count})"
show_help(message)
end
|
#show_stroke ⇒ Object
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 395
def show_stroke
c = Buffer.current.char_after
x, y = KANJI_TABLE.find.with_index { |row, i|
j = row.index(c)
if j
break [j, i]
else
false
end
}
if x.nil?
raise EditorError, "Stroke not found"
end
s = " " * 10 + "・・・・ ・・・・" * 3
s[x] = "1"
s[y] = "2"
message = s.gsub(/.{10}/, "\\&\n").gsub(/ /, " ")
show_help(message)
Window.redisplay
nil
end
|
#start_mazegaki_conversion(with_inflection = false) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 107
def start_mazegaki_conversion(with_inflection = false)
@mazegaki_convert_with_inflection = with_inflection
pos, yomi = find_mazegaki_start_pos(with_inflection)
if pos.nil?
raise EditorError, "No mazegaki conversion candidate"
end
mazegaki_convert(pos, yomi)
end
|
#status ⇒ Object
47
48
49
|
# File 'lib/textbringer/input_methods/t_code_input_method.rb', line 47
def status
"漢"
end
|