textbringer 1.0.3 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ module Textbringer
2
+ class TCodeInputMethod
3
+ KEYBOARD = "1234567890" "qwertyuiop" "asdfghjkl;" "zxcvbnm,./"
4
+
5
+ KEY_TABLE = KEYBOARD.each_char.with_index.map { |c, i|
6
+ [c, i]
7
+ }.to_h
8
+
9
+ KANJI_TABLE = [
10
+ "■■■■■■■■■■ヮヰヱヵヶ請境系探象ゎゐゑ■■盛革突温捕■■■■■依繊借須訳",
11
+ "■■■■■■■■■■丑臼宴縁曳尚賀岸責漁於汚乙穏■益援周域荒■■■■■織父枚乱香",
12
+ "■■■■■■■■■■鬼虚狭脅驚舎喜幹丘糖奇既菊却享康徒景処ぜ■■■■■譲ヘ模降走",
13
+ "■■■■■■■■■■孤誇黄后耕布苦圧恵固巧克懇困昏邦舞雑漢緊■■■■■激干彦均又",
14
+ "■■■■⑤■■■■■奉某貌卜■姿絶密秘押■■■■■衆節杉肉除■■■■■測血散笑弁",
15
+ "■■■■■■■■■■湖礼著移郷■■■■■償欧努底亜■■■■■禁硝樹句礎■■■■■",
16
+ "■■■■■■■■■■端飾郵塩群■星析遷宣紅傷豪維脱鼠曹奏尊■絹被源願臨■■■■■",
17
+ "■■■■◈■■■■■刷寿順危砂庶粧丈称蒸舗充喫腕暴■■■■■批慶渉竜併■■■■■",
18
+ "■■■■■■■■■■震扱片札乞■乃如尼帳輪倒操柄魚■■■■■就駐揮丹鮮■■■■■",
19
+ "■■■■■■■■■■弘痛票訴遺欄龍略慮累則存倍牛釈■■■■■綱潟創背皮■■■■■",
20
+ "ヲ哀暇啓把酸昼炭稲湯果告策首農歩回務島開報紙館夜位給員ど代レ欠夏彼妻善相家的対歴",
21
+ "ゥ逢牙掲伐貿捜異隣旧概買詳由死キせ区百木音王放々応分よル千ア財針裏居差付プばュ作",
22
+ "ヴ宛壊携避攻焼闘奈夕武残両在!や出タ手保案曲情引職7か(トれ従骨厚顔量内工八テ見",
23
+ "ヂ囲較劇卑盤帯易速拡風階能論増コ山者発立横興刺側覚きっ日国二適類御宇推九名川機チ",
24
+ "ヅ庵寒賢藩汽換延雪互細古利ペゃナ金マ和女崎白ぐ官球上く8え年母奥因酒伸サ建パ第入",
25
+ "簡徴触宗植■索射濁慢害賃整軽評佐法数郎談服声任検豊美題井洋実爆仲茶率比昔短岩巨敗",
26
+ "承章候途複■冊需詑迷撃折追隊角接備最急験変審改昇芸宿制集安画陽構旅施曜遠ォ将ぞ塚",
27
+ "快否歯筆里■皿輯蓄戻浴秀糸春幸記朝知ワ送限研労統役セ運ツ特谷ァ導認健尾序振練念働",
28
+ "包納頼逃寝■賛瞬貯羊積程断低減モ資士費ィ逆企精ざ印神び打勤ャ殺負何履般耳授版効視",
29
+ "唱暮憲勉罪■■盾虫■故鉱提児敷無石屋解募令違装然確優公品語演券悪秋非便示即難普辺",
30
+ "ぱ慰我兼菱桜瀬鳥催障収際太園船中スもお定種岡結進真3と〇てるヒ江別考権ッ人三京ち",
31
+ "ぴ為掛嫌紐典博筋忠乳若雄査ふ賞わラ東生ろ宅熟待取科ーした一が及久蔵早造ロク万方フ",
32
+ "ぷ陰敢顕描採謡希仏察指氏丸続ェう4)十リ料土活ね参い、の51投義算半県んまンつ四",
33
+ "ぺ隠甘牽憤君純副盟標ぎ格次習火あこ6学月受予切育池。◆0・2込沢軍青清けイす電地",
34
+ "ぽ胃患厳弊犯余堀肩療思術広門聞本さら高シ英ボ加室少ではになを転空性使級業時「長み",
35
+ "朱遅甲致汎■衰滋沈己病終起路越む南原駅物勢必講愛管要設水藤有素兵専親寮ホ共ブ平楽",
36
+ "陣鶴鹿貨絡■趨湿添已常張薬防得ケ式戦関男輸形助◇流連鉄教力ベ毛永申袋良私ゴ来信午",
37
+ "眼繁誌招季■垂甚徹巳寺質づ港条話座線ダ橋基好味宝争デ現エ他度等浅頃落命村ガ製校ご",
38
+ "執紹夢卸阿■粋■爪巴停領容玉右べ民ソ点遇足草築観言車成天世文板客師税飛ノ完重約各",
39
+ "岳刑弱雲窓■寸瞳陶■河置供試席期ゾ歳強係婦段衛額渋主映書可へ伝庭課着坂近外米ョ光",
40
+ "ぁ■瓦■■呼幅歓功盗徳渡守登退店持町所ほ件友卒初慣行ド円小ジヨ誤証含%海道ず西げ",
41
+ "ぃ■■■■紀破郡抗幡械刊訪融雨全じ自議明宮伊求技写通カ社野同判規感値ギ当理メウグ",
42
+ "ぅ■■■■房績識属衣帝始了極熱バ部六経動局頭配黒院だり—め大済吉ゆ器照不合面政オ",
43
+ "ぇ■■■■去疑ぢ綿離読鈴恐督況後間場ニ産向府富直倉新」9子五説週号葉派委化ビ目市",
44
+ "ぉ■■■■秒範核影麻族丁未才返問ム七住北割ぶ番望元事田会前そ休省央福毎気売下都株",
45
+ "欲巣茂述朗■■■■■帰庁昨跡ゲ洗羽個医静億録赤想消支協用表正図挙険ゼ波ヤ心界意今",
46
+ "迫災恋脳老■■■■■監寄裁達芝響忘討史環色貸販編仕先多商ハ交之末ぼ街免再ネ〜口台",
47
+ "留列刻豆看■■■■■竹注介具失司迎華許補左態花栄ザ調混ポ決ミ州払乗庫状団計夫食総",
48
+ "替沼?辞献■■■■■ゅ修究答養復並浦ユ冷ぬ展警型誰組選党択体例満津準遊戸ひょ価与",
49
+ "還更占箱矢■■■■■志抜航層深担陸巻競護根様独止堂銀以ヌ営治字材過諸単身ピ勝反ズ"
50
+ ]
51
+
52
+ # Key positions
53
+ # 0 1 2 3 4 5 6 7 8 9
54
+ # 10 11 12 13 14 15 16 17 18 19
55
+ # 20 21 22 23 24 25 26 27 28 29
56
+ # 30 31 32 33 34 35 36 37 38 39
57
+ MAZEGAKI_STROKE_PRIORITY_LIST = [
58
+ 22, 23, 21, 24, 20,
59
+ 12, 13, 11, 14, 10,
60
+ 27, 26, 28, 25, 29,
61
+ 17, 16, 18, 15, 19
62
+ ]
63
+ end
64
+ end
@@ -0,0 +1,458 @@
1
+ module Textbringer
2
+ class TCodeInputMethod < InputMethod
3
+ require_relative "t_code_input_method/tables"
4
+
5
+ data_dir = File.expand_path("t_code_input_method", __dir__)
6
+ BUSHU_PATH = File.expand_path("bushu.rev", data_dir)
7
+ BUSHU_DIC = {} unless defined?(BUSHU_DIC)
8
+ MAZEGAKI_PATH = File.expand_path("mazegaki.dic", data_dir)
9
+ MAZEGAKI_DIC = {} unless defined?(MAZEGAKI_DIC)
10
+ MAZEGAKI_MAX_WORD_LEN = 12 # じょうほうしょりがっかい
11
+ MAZEGAKI_MAX_SUFFIX_LEN = 4
12
+
13
+ def initialize
14
+ super
15
+ @prev_key_index = nil
16
+ @mazegaki_start_pos = nil
17
+ @mazegaki_candidates = nil
18
+ @delete_help_window = false
19
+ @help_window = nil
20
+ @prev_buffer = nil
21
+ setup_dictionaries
22
+ end
23
+
24
+ def setup_dictionaries
25
+ data_dir = CONFIG[:t_code_data_dir] ||
26
+ File.expand_path("~/.textbringer/tcode")
27
+ bushu_path = File.join(data_dir, "bushu.rev")
28
+ mazegaki_path = File.join(data_dir, "mazegaki.dic")
29
+ if BUSHU_DIC.empty?
30
+ File.open(bushu_path) do |f|
31
+ f.each_line do |line|
32
+ x, *xs = line.chomp.chars
33
+ BUSHU_DIC[xs.sort.join] = x
34
+ end
35
+ end
36
+ end
37
+ if MAZEGAKI_DIC.empty?
38
+ File.open(mazegaki_path) do |f|
39
+ f.each_line do |line|
40
+ x, y = line.split
41
+ MAZEGAKI_DIC[x] = y
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ def status
48
+ "漢"
49
+ end
50
+
51
+ def handle_event(event)
52
+ key_index = KEY_TABLE[event]
53
+ if @mazegaki_start_pos
54
+ if process_mazegaki_conversion(event, key_index)
55
+ return nil
56
+ end
57
+ end
58
+ if key_index.nil?
59
+ @prev_key_index = nil
60
+ return event
61
+ end
62
+ if @prev_key_index.nil?
63
+ @prev_key_index = key_index
64
+ nil
65
+ else
66
+ c = KANJI_TABLE[key_index][@prev_key_index]
67
+ @prev_key_index = nil
68
+ case c
69
+ when ?■
70
+ nil
71
+ when ?◆
72
+ bushu_compose
73
+ when ?◇
74
+ start_mazegaki_conversion(false)
75
+ when ?◈
76
+ start_mazegaki_conversion(true)
77
+ when ?⑤
78
+ show_stroke
79
+ else
80
+ c
81
+ end
82
+ end
83
+ end
84
+
85
+ def with_target_buffer(&block)
86
+ if isearch_mode?
87
+ @isearch_buffer ||= Buffer.new
88
+ if @isearch_buffer.to_s != ISEARCH_STATUS[:string]
89
+ @isearch_buffer.replace(ISEARCH_STATUS[:string])
90
+ end
91
+ block.call(@isearch_buffer)
92
+ ISEARCH_STATUS[:string] = @isearch_buffer.to_s
93
+ if Buffer.current != Buffer.minibuffer
94
+ message(isearch_prompt + ISEARCH_STATUS[:string], log: false)
95
+ Window.redisplay
96
+ end
97
+ else
98
+ block.call(Buffer.current)
99
+ end
100
+ end
101
+
102
+ def bushu_compose
103
+ with_target_buffer do |buffer|
104
+ pos = buffer.point
105
+ s = 2.times.map {
106
+ buffer.backward_char
107
+ buffer.char_after
108
+ }.sort.join
109
+ c = BUSHU_DIC[s]
110
+ if c
111
+ buffer.replace(c, start: buffer.point, end: pos)
112
+ else
113
+ buffer.goto_char(pos)
114
+ end
115
+ end
116
+ isearch_search if isearch_mode?
117
+ Window.redisplay
118
+ nil
119
+ end
120
+
121
+ def start_mazegaki_conversion(with_inflection = false)
122
+ @mazegaki_convert_with_inflection = with_inflection
123
+ pos, yomi = find_mazegaki_start_pos(with_inflection)
124
+ if pos.nil?
125
+ raise EditorError, "No mazegaki conversion candidate"
126
+ end
127
+ mazegaki_convert(pos, yomi)
128
+ end
129
+
130
+ def mazegaki_convert(pos, yomi)
131
+ with_target_buffer do |buffer|
132
+ candidates = mazegaki_lookup_candidates(yomi)
133
+ if candidates
134
+ @mazegaki_yomi = yomi
135
+ @mazegaki_suffix = buffer.substring(pos + yomi.bytesize,
136
+ buffer.point)
137
+ case candidates.size
138
+ when 1
139
+ buffer.composite_edit do
140
+ buffer.delete_region(pos, buffer.point)
141
+ buffer.insert("△" + candidates[0] + @mazegaki_suffix)
142
+ end
143
+ when 2
144
+ buffer.composite_edit do
145
+ buffer.delete_region(pos, buffer.point)
146
+ buffer.insert("△{" + candidates.join(",") + "}" +
147
+ @mazegaki_suffix)
148
+ end
149
+ else
150
+ buffer.save_excursion do
151
+ buffer.goto_char(pos)
152
+ buffer.insert("△")
153
+ end
154
+ end
155
+ @mazegaki_start_pos = pos
156
+ @mazegaki_candidates = candidates
157
+ @mazegaki_candidates_page = 0
158
+ if candidates.size > 2
159
+ show_mazegaki_candidates
160
+ end
161
+ end
162
+ Window.redisplay
163
+ nil
164
+ end
165
+ end
166
+
167
+ def mazegaki_lookup_yomi(s, with_inflectin)
168
+ if !with_inflectin
169
+ return MAZEGAKI_DIC.key?(s) ? s : nil
170
+ end
171
+ yomi = s.sub(/\p{hiragana}\z/, "")
172
+ (MAZEGAKI_MAX_SUFFIX_LEN + 1).times do
173
+ return yomi if MAZEGAKI_DIC.key?(yomi + "—")
174
+ break if !yomi.sub!(/\p{hiragana}\z/, "")
175
+ end
176
+ nil
177
+ end
178
+
179
+ def mazegaki_lookup_candidates(yomi)
180
+ if @mazegaki_convert_with_inflection
181
+ s = yomi + "—"
182
+ else
183
+ s = yomi
184
+ end
185
+ c = MAZEGAKI_DIC[s]
186
+ return nil if c.nil?
187
+ candidates = c.split("/").map { |i|
188
+ i.sub(/;.*/, "")
189
+ }.reject(&:empty?)
190
+ return nil if candidates.empty?
191
+ candidates
192
+ end
193
+
194
+ def find_mazegaki_start_pos(with_inflection)
195
+ with_target_buffer do |buffer|
196
+ buffer.save_excursion do
197
+ pos = buffer.point
198
+ start_pos = nil
199
+ yomi = nil
200
+ MAZEGAKI_MAX_WORD_LEN.times do
201
+ break if buffer.beginning_of_buffer?
202
+ buffer.backward_char
203
+ s = buffer.substring(buffer.point, pos)
204
+ y = mazegaki_lookup_yomi(s, with_inflection)
205
+ if y
206
+ start_pos = buffer.point
207
+ yomi = y
208
+ end
209
+ end
210
+ return start_pos, yomi
211
+ end
212
+ end
213
+ end
214
+
215
+ def process_mazegaki_conversion(event, key_index)
216
+ case event
217
+ when " "
218
+ mazegaki_next_page
219
+ return true
220
+ when "<"
221
+ mazegaki_relimit_left
222
+ return true
223
+ when ">"
224
+ mazegaki_relimit_right
225
+ return true
226
+ end
227
+ begin
228
+ if @mazegaki_candidates.size == 1
229
+ if event == "\C-m"
230
+ mazegaki_finish(@mazegaki_candidates[0])
231
+ return true
232
+ elsif key_index
233
+ mazegaki_finish(@mazegaki_candidates[0])
234
+ return false
235
+ end
236
+ elsif key_index
237
+ mazegaki_limit = MAZEGAKI_STROKE_PRIORITY_LIST.size
238
+ i = MAZEGAKI_STROKE_PRIORITY_LIST.index(key_index)
239
+ if i
240
+ offset = @mazegaki_candidates_page * mazegaki_limit + i
241
+ c = @mazegaki_candidates[offset]
242
+ if c
243
+ mazegaki_finish(c)
244
+ return true
245
+ end
246
+ end
247
+ end
248
+ mazegaki_reset
249
+ true
250
+ ensure
251
+ @mazegaki_start_pos = nil
252
+ @mazegaki_candidates = nil
253
+ Window.redisplay
254
+ end
255
+ end
256
+
257
+ def mazegaki_reset
258
+ with_target_buffer do |buffer|
259
+ buffer.undo
260
+ pos = @mazegaki_start_pos +
261
+ @mazegaki_yomi.bytesize + @mazegaki_suffix.bytesize
262
+ buffer.goto_char(pos)
263
+ hide_help_window
264
+ end
265
+ end
266
+
267
+ def mazegaki_finish(s)
268
+ mazegaki_reset
269
+ with_target_buffer do |buffer|
270
+ buffer.composite_edit do
271
+ buffer.delete_region(@mazegaki_start_pos, buffer.point)
272
+ buffer.insert(s + @mazegaki_suffix)
273
+ end
274
+ end
275
+ isearch_search if isearch_mode?
276
+ end
277
+
278
+ def hide_help_window
279
+ if @delete_help_window
280
+ Window.delete_window(@help_window)
281
+ elsif @prev_buffer
282
+ @help_window.buffer = @prev_buffer
283
+ end
284
+ @delete_help_window = false
285
+ @help_window = nil
286
+ @prev_buffer = nil
287
+ end
288
+
289
+ def mazegaki_next_page
290
+ if @mazegaki_candidates.size <= mazegaki_limit
291
+ return
292
+ end
293
+ @mazegaki_candidates_page += 1
294
+ if @mazegaki_candidates_page * mazegaki_limit >
295
+ @mazegaki_candidates.size
296
+ @mazegaki_candidates_page = 0
297
+ end
298
+ show_mazegaki_candidates
299
+ Window.redisplay
300
+ end
301
+
302
+ def mazegaki_relimit_left
303
+ with_target_buffer do |buffer|
304
+ yomi = nil
305
+ start_pos = nil
306
+ mazegaki_reset
307
+ buffer.save_excursion do
308
+ pos = buffer.point
309
+ buffer.goto_char(@mazegaki_start_pos)
310
+ s = buffer.substring(buffer.point, pos)
311
+ (MAZEGAKI_MAX_WORD_LEN - s.size).times do
312
+ break if buffer.beginning_of_buffer?
313
+ buffer.backward_char
314
+ s = buffer.substring(buffer.point, pos)
315
+ yomi = mazegaki_lookup_yomi(s, @mazegaki_convert_with_inflection)
316
+ if yomi
317
+ start_pos = buffer.point
318
+ break
319
+ end
320
+ end
321
+ if start_pos.nil?
322
+ message("Can't relimit left")
323
+ start_pos = @mazegaki_start_pos
324
+ yomi = @mazegaki_yomi
325
+ end
326
+ end
327
+ mazegaki_convert(start_pos, yomi)
328
+ Window.redisplay
329
+ end
330
+ end
331
+
332
+ def mazegaki_relimit_right
333
+ with_target_buffer do |buffer|
334
+ start_pos = nil
335
+ yomi = nil
336
+ mazegaki_reset
337
+ buffer.save_excursion do
338
+ pos = buffer.point
339
+ buffer.goto_char(@mazegaki_start_pos)
340
+ if @mazegaki_convert_with_inflection && @mazegaki_yomi &&
341
+ (yomi = mazegaki_lookup_yomi(@mazegaki_yomi, true))
342
+ start_pos = @mazegaki_start_pos
343
+ else
344
+ loop do
345
+ break if buffer.point >= pos
346
+ buffer.forward_char
347
+ s = buffer.substring(buffer.point, pos)
348
+ yomi = mazegaki_lookup_yomi(s, @mazegaki_convert_with_inflection)
349
+ if yomi
350
+ start_pos = buffer.point
351
+ break
352
+ end
353
+ end
354
+ end
355
+ end
356
+ if start_pos.nil?
357
+ if !@mazegaki_convert_with_inflection
358
+ start_pos, yomi = find_mazegaki_start_pos(true)
359
+ if start_pos
360
+ @mazegaki_convert_with_inflection = true
361
+ end
362
+ end
363
+ if start_pos.nil?
364
+ message("Can't relimit right")
365
+ start_pos = @mazegaki_start_pos
366
+ yomi = @mazegaki_yomi
367
+ end
368
+ end
369
+ mazegaki_convert(start_pos, yomi)
370
+ Window.redisplay
371
+ end
372
+ end
373
+
374
+ def show_mazegaki_candidates
375
+ offset = @mazegaki_candidates_page * mazegaki_limit
376
+ candidates = @mazegaki_candidates[offset, mazegaki_limit]
377
+ xs = Array.new(40, "-")
378
+ candidates.each_with_index do |s, i|
379
+ xs[MAZEGAKI_STROKE_PRIORITY_LIST[i]] = s
380
+ end
381
+ max_width = candidates.map { |s|
382
+ Buffer.display_width(s)
383
+ }.max
384
+ page = @mazegaki_candidates_page + 1
385
+ page_count =
386
+ (@mazegaki_candidates.size.to_f / mazegaki_limit).ceil
387
+ message = xs.map.with_index { |s, i|
388
+ space = " " * (max_width - Buffer.display_width(s))
389
+ if i % 10 < 5
390
+ s + space
391
+ else
392
+ space + s
393
+ end
394
+ }.each_slice(10).map.with_index { |ys, i|
395
+ if i == 0
396
+ " " + ys[0, 4].join(" ") + " " + ys[4, 2].join(" ") + " " +
397
+ ys[6, 4].join(" ")
398
+ else
399
+ "[" + ys[0, 4].join(" ") + "] " + ys[4, 2].join(" ") + " [" +
400
+ ys[6, 4].join(" ") + "]"
401
+ end
402
+ }.join("\n") + " (#{page}/#{page_count})"
403
+ show_help(message)
404
+ end
405
+
406
+ def mazegaki_limit
407
+ MAZEGAKI_STROKE_PRIORITY_LIST.size
408
+ end
409
+
410
+ def show_stroke
411
+ c = Buffer.current.char_after
412
+ x, y = KANJI_TABLE.find.with_index { |row, i|
413
+ j = row.index(c)
414
+ if j
415
+ break [j, i]
416
+ else
417
+ false
418
+ end
419
+ }
420
+ if x.nil?
421
+ raise EditorError, "Stroke not found"
422
+ end
423
+ s = " " * 10 + "・・・・  ・・・・" * 3
424
+ s[x] = "1"
425
+ s[y] = "2"
426
+ message = s.gsub(/.{10}/, "\\&\n").gsub(/ /, " ")
427
+ show_help(message)
428
+ Window.redisplay
429
+ nil
430
+ end
431
+
432
+ def show_help(message)
433
+ buffer = Buffer.find_or_new("*T-Code Help*",
434
+ undo_limit: 0, read_only: true)
435
+ buffer.read_only_edit do
436
+ buffer.clear
437
+ buffer.insert(message)
438
+ buffer.beginning_of_buffer
439
+ end
440
+ if Window.list.size == 1
441
+ Window.list.first.split(message.lines.size + 1)
442
+ @delete_help_window = true
443
+ end
444
+ if Window.current.echo_area?
445
+ window = Window.list.last
446
+ else
447
+ windows = Window.list
448
+ i = (windows.index(Window.current) + 1) % windows.size
449
+ window = windows[i]
450
+ end
451
+ @help_window = window
452
+ if window.buffer != buffer
453
+ @prev_buffer = window.buffer
454
+ window.buffer = buffer
455
+ end
456
+ end
457
+ end
458
+ end
@@ -211,6 +211,7 @@ module Textbringer
211
211
  GLOBAL_MAP.define_key([:f1, "f"], :describe_command)
212
212
  GLOBAL_MAP.define_key([:f1, "k"], :describe_key)
213
213
  GLOBAL_MAP.define_key("\C-x#", :server_edit_done)
214
+ GLOBAL_MAP.define_key("\C-\\", :toggle_input_method)
214
215
  GLOBAL_MAP.handle_undefined_key do |key|
215
216
  if key.is_a?(String) && /[\0-\x7f]/ !~ key
216
217
  :self_insert
@@ -21,7 +21,7 @@ module Textbringer
21
21
  if | unless | then | elsif | else | case | when | while | until |
22
22
  for | break | next | redo | retry | in | do | return | yield |
23
23
  super | self | nil | true | false | and | or | not | alias
24
- ) \b | defined\? )
24
+ ) \b (?![!?]) | defined\? )
25
25
  /x
26
26
 
27
27
  define_syntax :string, /
@@ -273,9 +273,11 @@ module Textbringer
273
273
  }
274
274
  if start_with_period ||
275
275
  (last_event == :on_op && last_text != "|") ||
276
+ (last_event == :on_kw && /\A(and|or)\z/.match?(last_text)) ||
276
277
  last_event == :on_period ||
277
278
  (last_event == :on_comma && event != :on_lbrace &&
278
- event != :on_lparen && event != :on_lbracket)
279
+ event != :on_lparen && event != :on_lbracket) ||
280
+ last_event == :on_label
279
281
  indentation += @buffer[:indent_level]
280
282
  end
281
283
  indentation
@@ -300,10 +302,12 @@ module Textbringer
300
302
  case text
301
303
  when "class", "module", "def", "if", "unless", "case",
302
304
  "do", "for", "while", "until", "begin"
303
- if /\A(if|unless|while|until)\z/.match?(text)
304
- ts = tokens[0...i].reverse_each.take_while { |(l,_),| l == line }
305
- t = ts.find { |_, e| e != :on_sp }
306
- next if t && !(t[1] == :on_op && t[2] == "=")
305
+ if /\A(if|unless|while|until)\z/.match?(text) &&
306
+ modifier?(tokens, i)
307
+ next
308
+ end
309
+ if text == "def" && endless_method_def?(tokens, i)
310
+ next
307
311
  end
308
312
  if stack.empty?
309
313
  return i
@@ -330,6 +334,39 @@ module Textbringer
330
334
  return nil, stack.grep_v(/[)\]]/).size
331
335
  end
332
336
 
337
+ def modifier?(tokens, i)
338
+ (line,), = tokens[i]
339
+ ts = tokens[0...i].reverse_each.take_while { |(l,_),| l == line }
340
+ t = ts.find { |_, e| e != :on_sp }
341
+ t && !(t[1] == :on_op && t[2] == "=")
342
+ end
343
+
344
+ def endless_method_def?(tokens, i)
345
+ ts = tokens.drop(i + 1)
346
+ ts.shift while ts[0][1] == :on_sp
347
+ _, event = ts.shift
348
+ return false if event != :on_ident
349
+ ts.shift while ts[0][1] == :on_sp
350
+ if ts[0][1] == :on_lparen
351
+ ts.shift
352
+ count = 1
353
+ while count > 0
354
+ _, event = ts.shift
355
+ return false if event.nil?
356
+ case event
357
+ when :on_lparen
358
+ count +=1
359
+ when :on_rparen
360
+ count -=1
361
+ end
362
+ end
363
+ ts.shift while ts[0][1] == :on_sp
364
+ end
365
+ ts[0][1] == :on_op && ts[0][2] == "="
366
+ rescue NoMethodError # no token
367
+ return false
368
+ end
369
+
333
370
  def find_test_target_path(base, namespace, name)
334
371
  patterns = []
335
372
  if namespace
@@ -108,7 +108,7 @@ module Textbringer
108
108
  if e.is_a?(SystemExit) || e.is_a?(SignalException)
109
109
  raise
110
110
  end
111
- if Buffer.current&.name != "*Backtrace*"
111
+ if !e.is_a?(Quit) && Buffer.current&.name != "*Backtrace*"
112
112
  buffer = Buffer.find_or_new("*Backtrace*", undo_limit: 0)
113
113
  if !buffer.mode.is_a?(BacktraceMode)
114
114
  buffer.apply_mode(BacktraceMode)
@@ -179,6 +179,7 @@ module Textbringer
179
179
  Window.current.buffer = Buffer.current = old_buffer
180
180
  Buffer.minibuffer[:completion_proc] = old_completion_proc
181
181
  Buffer.minibuffer.keymap = old_minibuffer_map
182
+ Buffer.minibuffer.disable_input_method
182
183
  Controller.current.current_prefix_arg = old_current_prefix_arg
183
184
  if COMPLETION[:original_buffer]
184
185
  COMPLETION[:completions_window].buffer = COMPLETION[:original_buffer]
@@ -199,8 +200,9 @@ module Textbringer
199
200
  end
200
201
  }
201
202
  }
203
+ initial_value = default&.sub(%r"\A#{Regexp.quote(Dir.pwd)}/", "")
202
204
  file = read_from_minibuffer(prompt, completion_proc: f,
203
- initial_value: default)
205
+ initial_value: initial_value)
204
206
  File.expand_path(file)
205
207
  end
206
208
 
@@ -316,8 +318,8 @@ module Textbringer
316
318
 
317
319
  HOOKS = Hash.new { |h, k| h[k] = [] }
318
320
 
319
- def add_hook(name, func = Proc.new)
320
- HOOKS[name].unshift(func)
321
+ def add_hook(name, func = nil, &block)
322
+ HOOKS[name].unshift(func || block)
321
323
  end
322
324
 
323
325
  def remove_hook(name, func)
@@ -1,3 +1,3 @@
1
1
  module Textbringer
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.1"
3
3
  end