1 ;;; hangul.el --- Korean Hangul input method
3 ;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Jihyun Cho <jihyun.jo@gmail.com>
6 ;; Keywords: multilingual, input method, Korean, Hangul
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This file is to implement the following hangul automata:
26 ;; - Hangul 2-Bulsik input method
27 ;; - Hangul 3-Bulsik final input method
28 ;; - Hangul 3-Bulsik 390 input method
36 ;; Hangul double Jamo table.
37 ;; The format is an alist of JAMO-TYPE vs. DOUBLE-JAMO-TABLE.
39 ;; JAMO-TYPE is a symbol `cho' for Choseong, `jung' for Jungseong, and
40 ;; `jong' for Jongseong.
42 ;; DOUBLE-JAMO-TABLE is an alist of Jamo index vs. the vector of Jamo
43 ;; indies that can be combined with the car part.
45 ;; Jamo index is a relative index in `hangul Compatibility Jamo' area
46 ;; of the Unicode (i.e. 1 for U+3131).
48 (defconst hangul-djamo-table
49 '((cho .
((1 .
[1]) ; Choseong
54 (jung . ((39 . [31 32 51]) ; Jungseong
57 (jong . ((1 . [1 21]) ; Jongseong
59 (9 . [1 17 18 21 28 29 30])
63 ;; Hangul 2-Bulsik keymap.
64 ;; It converts an ASCII code A-Z, a-z, to the corresponding hangul
67 (defconst hangul2-keymap
68 [17 48 26 23 7 9 30 39 33 35 31 51 49 44 32 36 18 1 4 21 37 29 24 28 43 27])
70 ;; Hangul 3-Bulsik final keymap. 3-Bulsik use full keyboard layout.
71 ;; Therefore, we must map all printable ASCII codes (`!' to `~')
72 ;; to Hangul 3-Bulsik codes.
73 ;; Other parts are the same as `hangul2-keymap'.
74 (defconst hangul3-keymap
75 [2 183 24 15 14 8220 120 39 126 8221 43 44 41 46 74 119 30 22 18 78 83
76 68 73 85 79 52 110 44 62 46 33 10 7 63 27 12 5 11 69 48 55 49 50 51
77 34 45 56 57 29 16 6 13 54 3 28 20 53 26 40 58 60 61 59 42 23 79 71
78 86 72 66 84 96 109 115 93 116 122 113 118 121 21 67 4 70 99 74 9 1
79 101 17 37 92 47 8251])
81 ;; Hangul 3-Bulsik 390 keymap.
82 ;; The role is the same as `hangul3-keymap'.
83 (defconst hangul390-keymap
84 [24 34 35 36 37 38 120 40 41 42 43 44 45 46 73 119 30 22 18 77 82 67 72
85 84 78 58 110 50 61 51 63 64 7 33 11 10 27 2 47 39 56 52 53 54 49 48
86 57 62 29 68 6 59 55 16 28 20 60 26 91 92 93 94 95 96 23 78 70 85 71
87 65 83 90 109 115 87 116 122 113 118 121 21 66 4 69 99 73 9 1 101 17
90 (defvar hangul-im-keymap
91 (let ((map (make-sparse-keymap)))
92 (define-key map "\d" 'hangul-delete-backward-char)
93 (define-key map [f9] 'hangul-to-hanja-conversion)
95 "Keymap for Hangul method. It is used by all Hangul input methods.")
97 ;; Current input character buffer. Store separated hangul character.
98 ;; The first and second are Choseong position.
99 ;; The third and forth are Jungseong position.
100 ;; The fifth and sixth are Jongseong position.
101 ;; The second, forth and sixth are double Jamo position.
105 (defsubst notzerop (number)
106 (not (zerop number)))
108 (defsubst alphabetp (char)
109 (or (and (>= char ?A) (<= char ?Z))
110 (and (>= char ?a) (<= char ?z))))
112 (defun hangul-character (cho jung jong)
113 "Convert CHO, JUNG, JONG to the precomposed `Hangul Syllables' character.
114 CHO, JUNG, JONG are relative indices in `Hangul Compatibility Jamo' of unicode.
115 Return a zero-length string if the conversion fails."
119 (if (and (/= cho 0) (/= jung 0))
135 (cond ((/= cho 0) cho)
137 ((/= jong 0) jong)))))
140 (defun hangul-insert-character (&rest queues)
141 "Insert characters generated from QUEUES.
142 Each queue has the same form as `hangul-queue'.
143 Setup `quail-overlay' to the last character."
144 (if (and mark-active transient-mark-mode)
146 (delete-region (region-beginning) (region-end))
148 (quail-delete-region)
149 (let ((first (car queues)))
152 (+ (aref first 0) (hangul-djamo 'cho (aref first 0) (aref first 1)))
153 (+ (aref first 2) (hangul-djamo 'jung (aref first 2) (aref first 3)))
154 (+ (aref first 4) (hangul-djamo 'jong (aref first 4) (aref first 5))))))
155 (move-overlay quail-overlay (overlay-start quail-overlay) (point))
156 (dolist (queue (cdr queues))
159 (+ (aref queue 0) (hangul-djamo 'cho (aref queue 0) (aref queue 1)))
160 (+ (aref queue 2) (hangul-djamo 'jung (aref queue 2) (aref queue 3)))
161 (+ (aref queue 4) (hangul-djamo 'jong (aref queue 4) (aref queue 5)))))
162 (move-overlay quail-overlay (1+ (overlay-start quail-overlay)) (point))))
164 (defun hangul-djamo (jamo char1 char2)
165 "Return the double Jamo index calculated from the arguments.
166 JAMO is a type of Hangul Jamo; `cho', `jung', or `jong'.
167 CHAR1 and CHAR2 are Hangul Jamo indices.
168 Return nil if CHAR1 and CHAR2 can not be combined."
169 (let* ((jamo (cdr (assoc jamo hangul-djamo-table)))
170 (char1 (cdr (assoc char1 jamo))))
172 (let ((i (length char1)))
175 (if (= char2 (aref char1 (1- i)))
181 (defsubst hangul2-input-method-jaum (char)
182 "Store Hangul Jamo indice CHAR in `hangul-queue'.
183 It is a Hangul 2-Bulsik Jaum.
184 This function processes a Hangul 2-Bulsik Jaum.
185 The Hangul 2-Bulsik is composed of a Jaum and a Moum.
186 The Jaum can be located in a Choseong position and a Jongseong position.
187 Unless the function inserts CHAR to `hangul-queue',
188 commit current `hangul-queue' and then set a new `hangul-queue',
189 and insert CHAR to new `hangul-queue'."
190 (if (cond ((zerop (aref hangul-queue 0))
191 (aset hangul-queue 0 char))
192 ((and (zerop (aref hangul-queue 1))
193 (zerop (aref hangul-queue 2))
194 (notzerop (hangul-djamo 'cho (aref hangul-queue 0) char)))
195 (aset hangul-queue 1 char))
196 ((and (zerop (aref hangul-queue 4))
197 (notzerop (aref hangul-queue 2))
203 (+ (aref hangul-queue 0)
206 (aref hangul-queue 0)
207 (aref hangul-queue 1)))
208 (+ (aref hangul-queue 2)
211 (aref hangul-queue 2)
212 (aref hangul-queue 3)))
214 (aset hangul-queue 4 char))
215 ((and (zerop (aref hangul-queue 5))
216 (notzerop (hangul-djamo 'jong (aref hangul-queue 4) char))
219 (+ (aref hangul-queue 0)
222 (aref hangul-queue 0)
223 (aref hangul-queue 1)))
224 (+ (aref hangul-queue 2)
227 (aref hangul-queue 2)
228 (aref hangul-queue 3)))
229 (+ (aref hangul-queue 4)
232 (aref hangul-queue 4)
234 (aset hangul-queue 5 char)))
235 (hangul-insert-character hangul-queue)
236 (hangul-insert-character hangul-queue
237 (setq hangul-queue (vector char 0 0 0 0 0)))))
239 (defsubst hangul2-input-method-moum (char)
240 "Store Hangul Jamo indice CHAR in `hangul-queue'.
241 It is a Hangul 2-Bulsik Moum.
242 This function processes a Hangul 2-Bulsik Moum.
243 The Moum can be located in a Jungseong position.
244 Other parts are the same as a `hangul2-input-method-jaum'."
245 (if (cond ((zerop (aref hangul-queue 2))
246 (aset hangul-queue 2 char))
247 ((and (zerop (aref hangul-queue 3))
248 (zerop (aref hangul-queue 4))
249 (notzerop (hangul-djamo 'jung (aref hangul-queue 2) char)))
250 (aset hangul-queue 3 char)))
251 (hangul-insert-character hangul-queue)
252 (let ((next-char (vector 0 0 char 0 0 0)))
253 (cond ((notzerop (aref hangul-queue 5))
254 (aset next-char 0 (aref hangul-queue 5))
255 (aset hangul-queue 5 0))
256 ((notzerop (aref hangul-queue 4))
257 (aset next-char 0 (aref hangul-queue 4))
258 (aset hangul-queue 4 0)))
259 (hangul-insert-character hangul-queue
260 (setq hangul-queue next-char)))))
262 (defsubst hangul3-input-method-cho (char)
263 "Store Hangul Jamo indice CHAR in `hangul-queue'.
264 It is a Hangul 3-Bulsik Choseong.
265 This function processes a Hangul 3-Bulsik Choseong.
266 The Hangul 3-Bulsik is composed of a Choseong, a Jungseong and a Jongseong.
267 The Choseong can be located in a Choseong position.
268 Other parts are the same as a `hangul2-input-method-jaum'."
269 (if (cond ((and (zerop (aref hangul-queue 0))
270 (zerop (aref hangul-queue 4)))
271 (aset hangul-queue 0 char))
272 ((and (zerop (aref hangul-queue 1))
273 (zerop (aref hangul-queue 2))
274 (notzerop (hangul-djamo 'cho (aref hangul-queue 0) char)))
275 (aset hangul-queue 1 char)))
276 (hangul-insert-character hangul-queue)
277 (hangul-insert-character hangul-queue
278 (setq hangul-queue (vector char 0 0 0 0 0)))))
280 (defsubst hangul3-input-method-jung (char)
281 "Store Hangul Jamo indice CHAR in `hangul-queue'.
282 It is a Hangul 3-Bulsik Jungseong.
283 This function processes a Hangul 3-Bulsik Jungseong.
284 The Jungseong can be located in a Jungseong position.
285 Other parts are the same as a `hangul3-input-method-cho'."
286 (if (cond ((and (zerop (aref hangul-queue 2))
287 (zerop (aref hangul-queue 4)))
288 (aset hangul-queue 2 char))
289 ((and (zerop (aref hangul-queue 3))
290 (notzerop (hangul-djamo 'jung (aref hangul-queue 2) char)))
291 (aset hangul-queue 3 char)))
292 (hangul-insert-character hangul-queue)
293 (hangul-insert-character hangul-queue
294 (setq hangul-queue (vector 0 0 char 0 0 0)))))
296 (defsubst hangul3-input-method-jong (char)
297 "Store Hangul Jamo indice CHAR in `hangul-queue'.
298 It is a Hangul 3-Bulsik Jongseong.
299 This function processes a Hangul 3-Bulsik Jongseong.
300 The Jongseong can be located in a Jongseong position.
301 Other parts are the same as a `hangul3-input-method-cho'."
302 (if (cond ((and (zerop (aref hangul-queue 4))
303 (notzerop (aref hangul-queue 0))
304 (notzerop (aref hangul-queue 2))
307 (+ (aref hangul-queue 0)
310 (aref hangul-queue 0)
311 (aref hangul-queue 1)))
312 (+ (aref hangul-queue 2)
315 (aref hangul-queue 2)
316 (aref hangul-queue 3)))
318 (aset hangul-queue 4 char))
319 ((and (zerop (aref hangul-queue 5))
320 (notzerop (hangul-djamo 'jong (aref hangul-queue 4) char))
323 (+ (aref hangul-queue 0)
326 (aref hangul-queue 0)
327 (aref hangul-queue 1)))
328 (+ (aref hangul-queue 2)
331 (aref hangul-queue 2)
332 (aref hangul-queue 3)))
333 (+ (aref hangul-queue 4)
336 (aref hangul-queue 4)
338 (aset hangul-queue 5 char)))
339 (hangul-insert-character hangul-queue)
340 (if (zerop (apply '+ (append hangul-queue nil)))
341 (hangul-insert-character (setq hangul-queue (vector 0 0 0 0 char 0)))
342 (hangul-insert-character hangul-queue
343 (setq hangul-queue (vector 0 0 0 0 char 0))))))
345 (defun hangul-delete-backward-char ()
346 "Delete the previous hangul character by Jaso units."
349 (while (and (> i 0) (zerop (aref hangul-queue i)))
351 (aset hangul-queue i 0))
352 (if (notzerop (apply '+ (append hangul-queue nil)))
353 (hangul-insert-character hangul-queue)
354 (delete-backward-char 1)))
356 (defun hangul-to-hanja-conversion ()
357 "Convert the previous hangul character to the corresponding hanja character."
359 (let ((echo-keystrokes 0)
362 (setq hanja-character (hangul-to-hanja-char (preceding-char)))
363 (when hanja-character
364 (delete-backward-char 1)
365 (insert hanja-character)
366 (setq hangul-queue (make-vector 6 0))
367 (move-overlay quail-overlay (point) (point)))))
369 ;; Support function for `hangul2-input-method'. Actually, this
370 ;; function handles the Hangul 2-Bulsik. KEY is an entered key code
371 ;; used for looking up `hangul2-keymap'."
372 (defun hangul2-input-method-internal (key)
373 (let ((char (+ (aref hangul2-keymap (1- (% key 32)))
374 (cond ((or (= key ?O) (= key ?P)) 2)
375 ((or (= key ?E) (= key ?Q) (= key ?R)
376 (= key ?T) (= key ?W)) 1)
379 (hangul2-input-method-jaum char)
380 (hangul2-input-method-moum char))))
382 (defun hangul2-input-method (key)
383 "2-Bulsik input method."
384 (if (or buffer-read-only (not (alphabetp key)))
386 (quail-setup-overlays nil)
387 (let ((input-method-function nil)
390 (setq hangul-queue (make-vector 6 0))
391 (hangul2-input-method-internal key)
393 (catch 'exit-input-loop
395 (let* ((seq (read-key-sequence nil))
396 (cmd (lookup-key hangul-im-keymap seq))
398 (cond ((and (stringp seq)
400 (setq key (aref seq 0))
402 (hangul2-input-method-internal key))
404 (call-interactively cmd))
406 (setq unread-command-events (listify-key-sequence seq))
407 (throw 'exit-input-loop nil))))))
408 (quail-delete-overlays)))))
410 ;; Support function for `hangul3-input-method'. Actually, this
411 ;; function handles the Hangul 3-Bulsik final. KEY is an entered key
412 ;; code used for looking up `hangul3-keymap'."
413 (defun hangul3-input-method-internal (key)
414 (let ((char (aref hangul3-keymap (- key 33))))
415 (cond ((and (> char 92) (< char 123))
416 (hangul3-input-method-cho (- char 92)))
417 ((and (> char 65) (< char 87))
418 (hangul3-input-method-jung (- char 35)))
420 (hangul3-input-method-jong char))
422 (setq hangul-queue (make-vector 6 0))
423 (insert (decode-char 'ucs char))
424 (move-overlay quail-overlay (point) (point))))))
426 (defun hangul3-input-method (key)
427 "3-Bulsik final input method."
428 (if (or buffer-read-only (< key 33) (>= key 127))
430 (quail-setup-overlays nil)
431 (let ((input-method-function nil)
434 (setq hangul-queue (make-vector 6 0))
435 (hangul3-input-method-internal key)
437 (catch 'exit-input-loop
439 (let* ((seq (read-key-sequence nil))
440 (cmd (lookup-key hangul-im-keymap seq))
442 (cond ((and (stringp seq)
444 (setq key (aref seq 0))
445 (and (>= key 33) (< key 127)))
446 (hangul3-input-method-internal key))
448 (call-interactively cmd))
450 (setq unread-command-events (listify-key-sequence seq))
451 (throw 'exit-input-loop nil))))))
452 (quail-delete-overlays)))))
454 ;; Support function for `hangul390-input-method'. Actually, this
455 ;; function handles the Hangul 3-Bulsik 390. KEY is an entered key
456 ;; code used for looking up `hangul390-keymap'."
457 (defun hangul390-input-method-internal (key)
458 (let ((char (aref hangul390-keymap (- key 33))))
459 (cond ((or (and (> char 86) (< char 91))
460 (and (> char 96) (< char 123)))
461 (hangul3-input-method-cho (- char (if (< char 97) 86 92))))
462 ((and (> char 64) (< char 86))
463 (hangul3-input-method-jung (- char 34)))
465 (hangul3-input-method-jong char))
467 (setq hangul-queue (make-vector 6 0))
468 (insert (decode-char 'ucs char))
469 (move-overlay quail-overlay (point) (point))))))
471 (defun hangul390-input-method (key)
472 "3-Bulsik 390 input method."
473 (if (or buffer-read-only (< key 33) (>= key 127))
475 (quail-setup-overlays nil)
476 (let ((input-method-function nil)
479 (setq hangul-queue (make-vector 6 0))
480 (hangul390-input-method-internal key)
482 (catch 'exit-input-loop
484 (let* ((seq (read-key-sequence nil))
485 (cmd (lookup-key hangul-im-keymap seq))
487 (cond ((and (stringp seq)
489 (setq key (aref seq 0))
490 (and (>= key 33) (< key 127)))
491 (hangul390-input-method-internal key))
493 (call-interactively cmd))
495 (setq unread-command-events (listify-key-sequence seq))
496 (throw 'exit-input-loop nil))))))
497 (quail-delete-overlays)))))
499 ;; Text shown by describe-input-method. Set to a proper text by
500 ;; hangul-input-method-activate.
501 (defvar hangul-input-method-help-text nil)
502 (make-variable-buffer-local 'hangul-input-method-help-text)
504 (defun hangul-input-method-activate (input-method func help-text &rest args)
505 "Activate Hangul input method INPUT-METHOD.
506 FUNC is a function to handle input key.
507 HELP-TEXT is a text set in `hangul-input-method-help-text'."
508 (setq inactivate-current-input-method-function 'hangul-input-method-inactivate
509 describe-current-input-method-function 'hangul-input-method-help
510 hangul-input-method-help-text help-text)
511 (quail-delete-overlays)
512 (if (eq (selected-window) (minibuffer-window))
513 (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
514 (set (make-local-variable 'input-method-function) func))
516 (defun hangul-input-method-inactivate ()
517 "Inactivate the current Hangul input method."
521 (quail-hide-guidance)
522 (quail-delete-overlays)
523 (setq describe-current-input-method-function nil))
524 (kill-local-variable 'input-method-function)))
526 (defun hangul-input-method-help ()
527 "Describe the current Hangul input method."
529 (with-output-to-temp-buffer "*Help*"
530 (princ hangul-input-method-help-text)))
534 ;; arch-tag: 26bc93fc-64ee-4fb1-b26d-22220d132dbe
535 ;;; hangul.el ends here