Class: Textbringer::HangulInputMethod

Inherits:
InputMethod show all
Defined in:
lib/textbringer/input_methods/hangul_input_method.rb

Constant Summary collapse

KEY_TO_COMPATIBILITY_JAMO =
{
  "q" => "ㅂ", "w" => "ㅈ", "e" => "ㄷ", "r" => "ㄱ", "t" => "ㅅ",
  "y" => "ㅛ", "u" => "ㅕ", "i" => "ㅑ", "o" => "ㅐ", "p" => "ㅔ",
  "a" => "ㅁ", "s" => "ㄴ", "d" => "ㅇ", "f" => "ㄹ", "g" => "ㅎ",
  "h" => "ㅗ", "j" => "ㅓ", "k" => "ㅏ", "l" => "ㅣ",
  "z" => "ㅋ", "x" => "ㅌ", "c" => "ㅊ", "v" => "ㅍ", "b" => "ㅠ",
  "n" => "ㅜ", "m" => "ㅡ",
  "Q" => "ㅃ", "W" => "ㅉ", "E" => "ㄸ", "R" => "ㄲ", "T" => "ㅆ",
  "O" => "ㅒ", "P" => "ㅖ"
}
COMPATIBILITY_JAMO_TO_FINAL =
{
  "ㄱ" => "ᆨ", "ㄲ" => "ᆩ", "ㄳ" => "ᆪ", "ㄴ" => "ᆫ",
  "ㄵ" => "ᆬ", "ㄶ" => "ᆭ", "ㄷ" => "ᆮ", "ㄹ" => "ᆯ",
  "ㄺ" => "ᆰ", "ㄻ" => "ᆱ", "ㄼ" => "ᆲ", "ㄽ" => "ᆳ",
  "ㄾ" => "ᆴ", "ㄿ" => "ᆵ", "ㅀ" => "ᆶ", "ㅁ" => "ᆷ",
  "ㅂ" => "ᆸ", "ㅄ" => "ᆹ", "ㅅ" => "ᆺ", "ㅆ" => "ᆻ",
  "ㅇ" => "ᆼ", "ㅈ" => "ᆽ", "ㅊ" => "ᆾ", "ㅋ" => "ᆿ",
  "ㅌ" => "ᇀ", "ㅍ" => "ᇁ", "ㅎ" => "ᇂ"
}
FINAL_TO_INITIAL =
{
  "ᆨ" => "ᄀ", "ᆩ" => "ᄁ", "ᆫ" => "ᄂ", "ᆮ" => "ᄃ",
  "ᆯ" => "ᄅ", "ᆷ" => "ᄆ", "ᆸ" => "ᄇ", "ᆺ" => "ᄉ",
  "ᆻ" => "ᄊ", "ᆼ" => "ᄋ", "ᆽ" => "ᄌ", "ᆾ" => "ᄎ",
  "ᆿ" => "ᄏ", "ᇀ" => "ᄐ", "ᇁ" => "ᄑ", "ᇂ" => "ᄒ"
}

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, #initialize, 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

This class inherits a constructor from Textbringer::InputMethod

Instance Method Details

#compose_hangul(prev, jamo) ⇒ Object



66
67
68
69
70
71
# File 'lib/textbringer/input_methods/hangul_input_method.rb', line 66

def compose_hangul(prev, jamo)
  # Use NFKC for compatibility jamo
  c = (prev + (COMPATIBILITY_JAMO_TO_FINAL[jamo] || jamo)).
    unicode_normalize(:nfkc)
  c.size == 1 ? c : nil
end

#decompose_prev(prev, jamo) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/textbringer/input_methods/hangul_input_method.rb', line 55

def decompose_prev(prev, jamo)
  if /[\u{ac00}-\u{d7a3}]/.match?(prev) && # syllables
    /[\u{314f}-\u{3163}]/.match?(jamo) # vowels
    s = prev.unicode_normalize(:nfd)
    if s.size == 3 && (initial = FINAL_TO_INITIAL[s[2]])
      return s[0, 2].unicode_normalize(:nfc), initial
    end
  end
  return nil, prev
end

#handle_event(event) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/textbringer/input_methods/hangul_input_method.rb', line 35

def handle_event(event)
  return event if !event.is_a?(String)
  jamo = KEY_TO_COMPATIBILITY_JAMO[event]
  return event if jamo.nil?
  with_target_buffer do |buffer|
    prev = buffer.char_before
    if /[\u{3131}-\u{3183}\u{ac00}-\u{d7a3}]/.match?(prev) # jamo or syllables
      decomposed_prev, prev = decompose_prev(prev, jamo)
      if c = compose_hangul(prev, jamo)
        buffer.backward_delete_char
        c = decomposed_prev + c if decomposed_prev
        buffer.insert(c)
        Window.redisplay
        return nil
      end
    end
  end
  jamo
end

#statusObject



31
32
33
# File 'lib/textbringer/input_methods/hangul_input_method.rb', line 31

def status
  "한"
end