Mercurial > kinput2.yaz
comparison lib/Ruby.rb @ 16:598fcbe482b5
imported patch 19_kinput2-v3.1-ruby.patch
| author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
|---|---|
| date | Mon, 08 Mar 2010 20:38:17 +0900 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 15:89750191b165 | 16:598fcbe482b5 |
|---|---|
| 1 $LOAD_PATH.push(ENV['KINPUT2_RUBY']) | |
| 2 | |
| 3 $KCODE = 'e' | |
| 4 require "jcode" | |
| 5 require "kconv" | |
| 6 require 'suikyo/suikyo' | |
| 7 | |
| 8 class TextEdit | |
| 9 attr_reader :cursor | |
| 10 def initialize (text = "", cursor = 0) | |
| 11 set (text, cursor) | |
| 12 end | |
| 13 | |
| 14 def set (data, cursor = 0) | |
| 15 @data = data | |
| 16 @cursor = cursor | |
| 17 end | |
| 18 | |
| 19 def text | |
| 20 return @data | |
| 21 end | |
| 22 | |
| 23 def reset | |
| 24 set ("", 0) | |
| 25 end | |
| 26 | |
| 27 def insert (string) | |
| 28 @data = @data[0,@cursor] + string + @data[@cursor..-1] | |
| 29 @cursor += string.length; | |
| 30 end | |
| 31 | |
| 32 def delete | |
| 33 if (@cursor < @data.length) then | |
| 34 @data = @data[0,@cursor] + @data[(@cursor + 1)..-1] | |
| 35 end | |
| 36 end | |
| 37 | |
| 38 def backspace | |
| 39 if (@cursor > 0) then | |
| 40 @data = @data[0,(@cursor - 1)] + @data[@cursor..-1] | |
| 41 @cursor -= 1 | |
| 42 end | |
| 43 end | |
| 44 | |
| 45 def cursor_left | |
| 46 if (@cursor > 0) then | |
| 47 @cursor -= 1 | |
| 48 end | |
| 49 end | |
| 50 | |
| 51 def cursor_right | |
| 52 if (@cursor < @data.length) then | |
| 53 @cursor += 1 | |
| 54 end | |
| 55 end | |
| 56 | |
| 57 def cursor_beginning | |
| 58 @cursor = 0 | |
| 59 end | |
| 60 | |
| 61 def cursor_end | |
| 62 @cursor = @data.length | |
| 63 end | |
| 64 | |
| 65 def segments | |
| 66 # [text before the cursor, text on the cursor, text after the cursor] | |
| 67 return [@data[0,@cursor], @data[@cursor,1], @data[(@cursor + 1)..-1]] | |
| 68 end | |
| 69 end | |
| 70 | |
| 71 class JTextEdit < TextEdit | |
| 72 def initialize (text = "", cursor = 0) | |
| 73 super | |
| 74 @suikyo = Suikyo.new | |
| 75 @suikyo.table.loadfile("romaji-kana") | |
| 76 end | |
| 77 | |
| 78 def text | |
| 79 if @cursor == @data.length then | |
| 80 return @suikyo.convert (@data) | |
| 81 else | |
| 82 return super | |
| 83 end | |
| 84 end | |
| 85 | |
| 86 def segments | |
| 87 if @cursor == @data.length then | |
| 88 return [@suikyo.convert (@data), '', ''] | |
| 89 else | |
| 90 return super | |
| 91 end | |
| 92 end | |
| 93 end | |
| 94 | |
| 95 class Window | |
| 96 attr_reader :call_open, :call_close, :visible | |
| 97 | |
| 98 def initialize (parent) | |
| 99 @parent = parent | |
| 100 @visible = false | |
| 101 end | |
| 102 | |
| 103 def open | |
| 104 @call_open = true unless @visible | |
| 105 end | |
| 106 def open_end | |
| 107 @call_open = false | |
| 108 @visible = true | |
| 109 end | |
| 110 | |
| 111 def close | |
| 112 @call_close = true if @visible | |
| 113 end | |
| 114 def close_end | |
| 115 @call_close = false | |
| 116 @visible = false | |
| 117 end | |
| 118 end | |
| 119 | |
| 120 class Dialog < Window | |
| 121 def text | |
| 122 @parent.cand_list.join(" ") | |
| 123 end | |
| 124 end | |
| 125 | |
| 126 class Selection < Window | |
| 127 attr_reader :call_right, :call_left, :call_line_beginning, :call_line_end, | |
| 128 :call_up, :call_down | |
| 129 | |
| 130 def right | |
| 131 @call_right = true | |
| 132 end | |
| 133 def right_end | |
| 134 @call_right = false | |
| 135 end | |
| 136 | |
| 137 def left | |
| 138 @call_left = true | |
| 139 end | |
| 140 def left_end | |
| 141 @call_left = false | |
| 142 end | |
| 143 | |
| 144 def line_beginning | |
| 145 @call_line_beginning = true | |
| 146 end | |
| 147 def line_beginning_end | |
| 148 @call_line_beginning = false | |
| 149 end | |
| 150 | |
| 151 def line_end | |
| 152 @call_line_end = true | |
| 153 end | |
| 154 def line_end_end | |
| 155 @call_line_end = false | |
| 156 end | |
| 157 | |
| 158 def up | |
| 159 @call_up = true | |
| 160 end | |
| 161 def up_end | |
| 162 @call_up = false | |
| 163 end | |
| 164 | |
| 165 def down | |
| 166 @call_down = true | |
| 167 end | |
| 168 def down_end | |
| 169 @call_down = false | |
| 170 end | |
| 171 end | |
| 172 | |
| 173 class KeyMap | |
| 174 @@keysym = { | |
| 175 :enter => 65293, | |
| 176 :space => 32, | |
| 177 :tab => 65289, | |
| 178 :delete => 65535, | |
| 179 :insert => 65379, | |
| 180 :home => 65360, | |
| 181 :end => 65367, | |
| 182 :page_up => 65365, | |
| 183 :page_down => 65366, | |
| 184 :esc => 65307, | |
| 185 :f1 => 65470, | |
| 186 :f2 => 65471, | |
| 187 :f3 => 65472, | |
| 188 :f4 => 65473, | |
| 189 :f5 => 65474, | |
| 190 :f6 => 65475, | |
| 191 :f7 => 65476, | |
| 192 :f8 => 65477, | |
| 193 :f9 => 65478, | |
| 194 :f10 => 65479, | |
| 195 :f11 => 65480, | |
| 196 :f12 => 65481, | |
| 197 :backspace => 65288, | |
| 198 :muhenkan => 65314, | |
| 199 :henkan => 65315, | |
| 200 :hankaku_zenkaku => 65322, | |
| 201 :katakana_hiragana => 65319, | |
| 202 :up => 65362, | |
| 203 :down => 65364, | |
| 204 :right => 65363, | |
| 205 :left => 65361, | |
| 206 :ctrl_l => 65507, | |
| 207 :ctrl_r => 65508, | |
| 208 :alt_l => 65513, | |
| 209 :alt_r => 65514, | |
| 210 :shift_l => 65505, | |
| 211 :shift_r => 65506, | |
| 212 } | |
| 213 | |
| 214 # @@enter = [13, 65293, 0] | |
| 215 # @@ctrl_a = [1, 97, 4]; @@alt_a = [97, 97, 8] | |
| 216 # @@ctrl_b = [2, 98, 4]; @@alt_b = [98, 98, 8] | |
| 217 # @@ctrl_c = [3, 99, 4]; @@alt_c = [99, 99, 8] | |
| 218 # @@ctrl_d = [4, 100, 4]; @@alt_d = [100, 100, 8] | |
| 219 # @@ctrl_e = [5, 101, 4]; @@alt_e = [101, 101, 8] | |
| 220 # @@ctrl_f = [6, 102, 4]; @@alt_f = [102, 102, 8] | |
| 221 # @@ctrl_g = [7, 103, 4]; @@alt_g = [103, 103, 8] | |
| 222 # @@ctrl_h = [8, 104, 4]; @@alt_h = [104, 104, 8] | |
| 223 # @@ctrl_i = [9, 105, 4]; @@alt_i = [105, 105, 8] | |
| 224 # @@ctrl_j = [10, 106, 4]; @@alt_j = [106, 106, 8] | |
| 225 # @@ctrl_k = [11, 107, 4]; @@alt_k = [107, 107, 8] | |
| 226 # @@ctrl_l = [12, 108, 4]; @@alt_l = [108, 108, 8] | |
| 227 # @@ctrl_m = [13, 109, 4]; @@alt_m = [109, 109, 8] | |
| 228 # @@ctrl_n = [14, 110, 4]; @@alt_n = [110, 110, 8] | |
| 229 # @@ctrl_o = [15, 111, 4]; @@alt_o = [111, 111, 8] | |
| 230 # @@ctrl_p = [16, 112, 4]; @@alt_p = [112, 112, 8] | |
| 231 # @@ctrl_q = [17, 113, 4]; @@alt_q = [113, 113, 8] | |
| 232 # @@ctrl_r = [18, 114, 4]; @@alt_r = [114, 114, 8] | |
| 233 # @@ctrl_s = [19, 115, 4]; @@alt_s = [115, 115, 8] | |
| 234 # @@ctrl_t = [20, 116, 4]; @@alt_t = [116, 116, 8] | |
| 235 # @@ctrl_u = [21, 117, 4]; @@alt_u = [117, 117, 8] | |
| 236 # @@ctrl_v = [22, 118, 4]; @@alt_v = [118, 118, 8] | |
| 237 # @@ctrl_w = [23, 119, 4]; @@alt_w = [119, 119, 8] | |
| 238 # @@ctrl_x = [24, 120, 4]; @@alt_x = [120, 120, 8] | |
| 239 # @@ctrl_y = [25, 121, 4]; @@alt_y = [121, 121, 8] | |
| 240 # @@ctrl_z = [26, 122, 4]; @@alt_z = [122, 122, 8] | |
| 241 | |
| 242 @@modifier = { :shift => 1, :ctrl => 4, :alt => 8 } | |
| 243 | |
| 244 def initialize | |
| 245 @keymap = Hash.new | |
| 246 end | |
| 247 | |
| 248 def key (stroke) | |
| 249 modifiers = 0 | |
| 250 if stroke.kind_of?(Array) then | |
| 251 main_key = stroke[0] | |
| 252 stroke[1..-1].each {|modifier| | |
| 253 modifiers |= | |
| 254 (modifier.kind_of?(Symbol) ? @@modifier[modifier] : modifier) | |
| 255 } | |
| 256 else | |
| 257 main_key = stroke | |
| 258 end | |
| 259 if main_key.kind_of?(Symbol) then | |
| 260 main_key = @@keysym[main_key] | |
| 261 end | |
| 262 return [main_key, modifiers] | |
| 263 end | |
| 264 | |
| 265 def ignore_shift (key) | |
| 266 (main_key, modifiers) = key | |
| 267 return [main_key, (modifiers & ~@@modifier[:shift])] | |
| 268 end | |
| 269 | |
| 270 def add (stroke, command) | |
| 271 @keymap[key(stroke)] = command | |
| 272 end | |
| 273 | |
| 274 def del (stroke) | |
| 275 @keymap.delete(key(stroke)) | |
| 276 end | |
| 277 | |
| 278 def command (stroke) | |
| 279 return @keymap[key(stroke)] || @keymap[ignore_shift(key(stroke))] | |
| 280 end | |
| 281 end | |
| 282 | |
| 283 | |
| 284 class ModeCore | |
| 285 attr_accessor :trap | |
| 286 attr_reader :label, :keymap | |
| 287 def initialize (parent) | |
| 288 @parent = parent | |
| 289 @label = "" | |
| 290 @keymap = KeyMap.new | |
| 291 initialize_keys | |
| 292 @trap = true | |
| 293 end | |
| 294 | |
| 295 def on (prev_mode = nil) | |
| 296 end | |
| 297 | |
| 298 def off (next_mode = nil) | |
| 299 end | |
| 300 | |
| 301 def entries | |
| 302 return [] | |
| 303 end | |
| 304 | |
| 305 def call (char, keysym, modifiers) | |
| 306 command = @keymap.command([keysym, modifiers]) | |
| 307 if command then | |
| 308 return send(command, keysym, modifiers) | |
| 309 else | |
| 310 return @trap | |
| 311 end | |
| 312 end | |
| 313 end | |
| 314 | |
| 315 class ModeMaster | |
| 316 attr_reader :current_name | |
| 317 | |
| 318 def initialize (parent, mode = :fund) | |
| 319 @parent = parent | |
| 320 @mode = Hash.new | |
| 321 @current_name = nil | |
| 322 end | |
| 323 | |
| 324 def set (name, mode) | |
| 325 @mode[name] = mode | |
| 326 unless @current then | |
| 327 @current_name = name | |
| 328 end | |
| 329 end | |
| 330 | |
| 331 def current | |
| 332 return @mode[@current_name] | |
| 333 end | |
| 334 | |
| 335 def change (name) | |
| 336 if @mode.key?(name) then | |
| 337 @current_name = name | |
| 338 current.on | |
| 339 return true | |
| 340 else | |
| 341 return false | |
| 342 end | |
| 343 end | |
| 344 | |
| 345 def label (name = @current_name) | |
| 346 if name then | |
| 347 mode = @mode[name] | |
| 348 else | |
| 349 mode = current | |
| 350 end | |
| 351 | |
| 352 if mode then | |
| 353 return mode.label.toeuc | |
| 354 else | |
| 355 return "NOMODE" | |
| 356 end | |
| 357 end | |
| 358 end | |
| 359 | |
| 360 class KanjiConvCore | |
| 361 attr_reader :cand_list, :cand_index, | |
| 362 :value_fixed, :call_fix, :mode | |
| 363 | |
| 364 def initialize | |
| 365 @textEdit = TextEdit.new | |
| 366 @mode = ModeMaster.new (self) | |
| 367 clear | |
| 368 end | |
| 369 | |
| 370 def reset | |
| 371 clear | |
| 372 end | |
| 373 | |
| 374 def clear | |
| 375 @cand_list = [] | |
| 376 @cand_index = 0 | |
| 377 | |
| 378 @call_fix = false | |
| 379 @value_fixed = "" | |
| 380 end | |
| 381 | |
| 382 ## inputEvent (keynum, keysym = nil, state = nil) | |
| 383 ## ReturnValue 1:Pass 0:Trap | |
| 384 def inputEvent (keynum, keysym = nil, state = nil) | |
| 385 return 1 | |
| 386 end | |
| 387 | |
| 388 def fix (fixed_arg) | |
| 389 # fixed_arg is a string or an index number of cand_list. | |
| 390 @call_fix = true | |
| 391 if fixed_arg.kind_of?(Integer) then | |
| 392 word = @cand_list[fixed_arg] | |
| 393 else | |
| 394 word = fixed_arg | |
| 395 end | |
| 396 @value_fixed = word | |
| 397 end | |
| 398 def fix_end | |
| 399 @call_fix = false | |
| 400 @value_fixed = "" | |
| 401 end | |
| 402 | |
| 403 def modeline | |
| 404 return @mode.label | |
| 405 end | |
| 406 | |
| 407 def set_cand_list (list, index = nil) | |
| 408 if (list.is_a?(Array) && list.length > 0) then | |
| 409 @cand_list = list.map {|cand| cand.toeuc} | |
| 410 @cand_index = index if index | |
| 411 return true | |
| 412 else | |
| 413 reset_cand_list | |
| 414 return false | |
| 415 end | |
| 416 end | |
| 417 def set_cand_index (index) | |
| 418 puts "<<set_cand_index>> #{index}" | |
| 419 @cand_index = index | |
| 420 end | |
| 421 def reset_cand_list | |
| 422 @cand_list = [] | |
| 423 @cand_index = 0 | |
| 424 end | |
| 425 | |
| 426 def segment_length | |
| 427 segments = (@mode.current.entries - ['']) | |
| 428 return segments.length | |
| 429 end | |
| 430 def segment_word (n) | |
| 431 segments = (@mode.current.entries - ['']) | |
| 432 return segments[n] | |
| 433 end | |
| 434 def segment_status (n) | |
| 435 segments = @mode.current.entries | |
| 436 offset = 0 | |
| 437 if segments[0] == "" then | |
| 438 offset += 1 | |
| 439 end | |
| 440 return ((n + offset) % 3 == 1) ? :highlight : :normal | |
| 441 end | |
| 442 end | |
| 443 | |
| 444 require 'Prime' |
