hangul.el 20.9 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
;;; hangul.el --- Korean Hangul input method
Kenichi Handa's avatar
Kenichi Handa committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2008-2020 Free Software Foundation, Inc.
Glenn Morris's avatar
Glenn Morris committed
4

Kenichi Handa's avatar
Kenichi Handa committed
5
;; Author: Jihyun Cho <jihyun.jo@gmail.com>
Kenichi Handa's avatar
Kenichi Handa committed
6 7 8 9
;; Keywords: multilingual, input method, Korean, Hangul

;; This file is part of GNU Emacs.

10
;; GNU Emacs is free software: you can redistribute it and/or modify
Kenichi Handa's avatar
Kenichi Handa committed
11
;; it under the terms of the GNU General Public License as published by
12 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Kenichi Handa's avatar
Kenichi Handa committed
14 15 16 17 18 19 20

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
21
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Kenichi Handa's avatar
Kenichi Handa committed
22

23 24
;;; Commentary:

Kenichi Handa's avatar
Kenichi Handa committed
25 26 27 28
;; This file is to implement the following hangul automata:
;; - Hangul 2-Bulsik input method
;; - Hangul 3-Bulsik final input method
;; - Hangul 3-Bulsik 390 input method
Jaeyoun Chung's avatar
Jaeyoun Chung committed
29

Kenichi Handa's avatar
Kenichi Handa committed
30 31 32
;;; Code:

(require 'quail)
Kenichi Handa's avatar
Kenichi Handa committed
33 34
(require 'hanja-util)

Kenichi Handa's avatar
Kenichi Handa committed
35
;; Hangul double Jamo table.
36 37 38 39 40
;; The format is an alist of JAMO-TYPE vs. DOUBLE-JAMO-TABLE.
;;
;; JAMO-TYPE is a symbol `cho' for Choseong, `jung' for Jungseong, and
;; `jong' for Jongseong.
;;
Kenichi Handa's avatar
Kenichi Handa committed
41
;; DOUBLE-JAMO-TABLE is an alist of Jamo index vs. the vector of Jamo
42 43 44 45 46
;; indies that can be combined with the car part.
;;
;; Jamo index is a relative index in `hangul Compatibility Jamo' area
;; of the Unicode (i.e. 1 for U+3131).

Kenichi Handa's avatar
Kenichi Handa committed
47
(defconst hangul-djamo-table
48
  '((cho . ((1 . [1])                   ; Choseong
Kenichi Handa's avatar
Kenichi Handa committed
49 50 51 52
            (7 . [7])
            (18 . [18])
            (21 . [21])
            (24 . [24])))
53
    (jung . ((39 . [31 32 51])          ; Jungseong
Kenichi Handa's avatar
Kenichi Handa committed
54 55
             (44 . [35 36 51])
             (49 . [51])))
56
    (jong . ((1 . [1 21])               ; Jongseong
Kenichi Handa's avatar
Kenichi Handa committed
57 58 59 60 61 62
             (4 . [24 30])
             (9 . [1 17 18 21 28 29 30])
             (18 . [18 21])
             (21 . [21])))))

;; Hangul 2-Bulsik keymap.
63
;; It converts an ASCII code A-Z, a-z, to the corresponding hangul
Kenichi Handa's avatar
Kenichi Handa committed
64
;; Jamo index.
65

Kenichi Handa's avatar
Kenichi Handa committed
66 67 68 69
(defconst hangul2-keymap
  [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])

;; Hangul 3-Bulsik final keymap.  3-Bulsik use full keyboard layout.
70
;; Therefore, we must map all printable ASCII codes (`!' to `~')
Kenichi Handa's avatar
Kenichi Handa committed
71
;; to Hangul 3-Bulsik codes.
72
;; Other parts are the same as `hangul2-keymap'.
Kenichi Handa's avatar
Kenichi Handa committed
73 74 75 76 77 78 79 80
(defconst hangul3-keymap
  [2 183 24 15 14 8220 120 39 126 8221 43 44 41 46 74 119 30 22 18 78 83
     68 73 85 79 52 110 44 62 46 33 10 7 63 27 12 5 11 69 48 55 49 50 51
     34 45 56 57 29 16 6 13 54 3 28 20 53 26 40 58 60 61 59 42 23 79 71
     86 72 66 84 96 109 115 93 116 122 113 118 121 21 67 4 70 99 74 9 1
     101 17 37 92 47 8251])

;; Hangul 3-Bulsik 390 keymap.
81
;; The role is the same as `hangul3-keymap'.
Kenichi Handa's avatar
Kenichi Handa committed
82 83 84 85 86 87 88 89 90 91 92
(defconst hangul390-keymap
  [24 34 35 36 37 38 120 40 41 42 43 44 45 46 73 119 30 22 18 77 82 67 72
      84 78 58 110 50 61 51 63 64 7 33 11 10 27 2 47 39 56 52 53 54 49 48
      57 62 29 68 6 59 55 16 28 20 60 26 91 92 93 94 95 96 23 78 70 85 71
      65 83 90 109 115 87 116 122 113 118 121 21 66 4 69 99 73 9 1 101 17
      123 124 125 126])

(defvar hangul-im-keymap
  (let ((map (make-sparse-keymap)))
    (define-key map "\d" 'hangul-delete-backward-char)
    (define-key map [f9] 'hangul-to-hanja-conversion)
93
    (define-key map [Hangul_Hanja] 'hangul-to-hanja-conversion)
Kenichi Handa's avatar
Kenichi Handa committed
94
    map)
95
  "Keymap for Hangul method.  It is used by all Hangul input methods.")
Kenichi Handa's avatar
Kenichi Handa committed
96 97

;; Current input character buffer. Store separated hangul character.
Kenichi Handa's avatar
Kenichi Handa committed
98 99 100 101
;; The first and second are Choseong position.
;; The third and forth are Jungseong position.
;; The fifth and sixth are Jongseong position.
;; The second, forth and sixth are double Jamo position.
Kenichi Handa's avatar
Kenichi Handa committed
102 103 104 105 106 107 108 109 110 111 112
(defvar hangul-queue
  (make-vector 6 0))

(defsubst notzerop (number)
  (not (zerop number)))

(defsubst alphabetp (char)
  (or (and (>= char ?A) (<= char ?Z))
      (and (>= char ?a) (<= char ?z))))

(defun hangul-character (cho jung jong)
113
  "Convert CHO, JUNG, JONG to the precomposed `Hangul Syllables' character.
Juanma Barranquero's avatar
Juanma Barranquero committed
114
CHO, JUNG, JONG are relative indices in `Hangul Compatibility Jamo' of Unicode.
115
Return a zero-length string if the conversion fails."
Kenichi Handa's avatar
Kenichi Handa committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  (or
   (decode-char
    'ucs
    (if (and (/= cho 0) (/= jung 0))
        (+ #xac00
           (* 588
              (- cho
                 (cond ((< cho 3) 1)
                       ((< cho 5) 2)
                       ((< cho 10) 4)
                       ((< cho 20) 11)
                       (t 12))))
           (* 28 (- jung 31))
           (- jong
              (cond ((< jong 8) 0)
                    ((< jong 19) 1)
                    ((< jong 25) 2)
                    (t 3))))
Kenichi Handa's avatar
Kenichi Handa committed
134 135 136 137
      (+ #x3130
	 (cond ((/= cho 0) cho)
	       ((/= jung 0) jung)
	       ((/= jong 0) jong)))))
Kenichi Handa's avatar
Kenichi Handa committed
138 139 140
   ""))

(defun hangul-insert-character (&rest queues)
141 142
  "Insert characters generated from QUEUES.
Each queue has the same form as `hangul-queue'.
143
Setup `quail-overlay' to the last character."
Kenichi Handa's avatar
Kenichi Handa committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  (if (and mark-active transient-mark-mode)
      (progn
        (delete-region (region-beginning) (region-end))
        (deactivate-mark)))
  (quail-delete-region)
  (let ((first (car queues)))
    (insert
     (hangul-character
      (+ (aref first 0) (hangul-djamo 'cho (aref first 0) (aref first 1)))
      (+ (aref first 2) (hangul-djamo 'jung (aref first 2) (aref first 3)))
      (+ (aref first 4) (hangul-djamo 'jong (aref first 4) (aref first 5))))))
  (move-overlay quail-overlay (overlay-start quail-overlay) (point))
  (dolist (queue (cdr queues))
    (insert
     (hangul-character
      (+ (aref queue 0) (hangul-djamo 'cho (aref queue 0) (aref queue 1)))
      (+ (aref queue 2) (hangul-djamo 'jung (aref queue 2) (aref queue 3)))
      (+ (aref queue 4) (hangul-djamo 'jong (aref queue 4) (aref queue 5)))))
    (move-overlay quail-overlay (1+ (overlay-start quail-overlay)) (point))))

(defun hangul-djamo (jamo char1 char2)
165
  "Return the double Jamo index calculated from the arguments.
Kenichi Handa's avatar
Kenichi Handa committed
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."
Kenichi Handa's avatar
Kenichi Handa committed
169 170 171 172 173 174 175 176 177 178
  (let* ((jamo (cdr (assoc jamo hangul-djamo-table)))
         (char1 (cdr (assoc char1 jamo))))
    (if char1
        (let ((i (length char1)))
          (or (catch 'found
                (while (> i 0)
                  (if (= char2 (aref char1 (1- i)))
                      (throw 'found i))
                  (setf i (1- i))))
              0))
Kenichi Handa's avatar
Kenichi Handa committed
179
      0)))
Kenichi Handa's avatar
Kenichi Handa committed
180 181

(defsubst hangul2-input-method-jaum (char)
Kenichi Handa's avatar
Kenichi Handa committed
182 183 184 185 186 187 188 189
  "Store Hangul Jamo indice CHAR in `hangul-queue'.
It is a Hangul 2-Bulsik Jaum.
This function processes a Hangul 2-Bulsik Jaum.
The Hangul 2-Bulsik is composed of a Jaum and a Moum.
The Jaum can be located in a Choseong position and a Jongseong position.
Unless the function inserts CHAR to `hangul-queue',
commit current `hangul-queue' and then set a new `hangul-queue',
and insert CHAR to new `hangul-queue'."
Kenichi Handa's avatar
Kenichi Handa committed
190 191 192 193 194 195 196 197 198 199 200 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
  (if (cond ((zerop (aref hangul-queue 0))
             (aset hangul-queue 0 char))
            ((and (zerop (aref hangul-queue 1))
                  (zerop (aref hangul-queue 2))
                  (notzerop (hangul-djamo 'cho (aref hangul-queue 0) char)))
             (aset hangul-queue 1 char))
            ((and (zerop (aref hangul-queue 4))
                  (notzerop (aref hangul-queue 2))
                  (/= char 8)
                  (/= char 19)
                  (/= char 25)
                  (numberp
                   (hangul-character
                    (+ (aref hangul-queue 0)
                       (hangul-djamo
                        'cho
                        (aref hangul-queue 0)
                        (aref hangul-queue 1)))
                    (+ (aref hangul-queue 2)
                       (hangul-djamo
                        'jung
                        (aref hangul-queue 2)
                        (aref hangul-queue 3)))
                    char)))
             (aset hangul-queue 4 char))
            ((and (zerop (aref hangul-queue 5))
                  (notzerop (hangul-djamo 'jong (aref hangul-queue 4) char))
                  (numberp
                   (hangul-character
                    (+ (aref hangul-queue 0)
                       (hangul-djamo
                        'cho
                        (aref hangul-queue 0)
                        (aref hangul-queue 1)))
                    (+ (aref hangul-queue 2)
                       (hangul-djamo
                        'jung
                        (aref hangul-queue 2)
                        (aref hangul-queue 3)))
                    (+ (aref hangul-queue 4)
                       (hangul-djamo
                        'jong
                        (aref hangul-queue 4)
                        char)))))
             (aset hangul-queue 5 char)))
      (hangul-insert-character hangul-queue)
Kenichi Handa's avatar
Kenichi Handa committed
236 237
    (hangul-insert-character hangul-queue
			     (setq hangul-queue (vector char 0 0 0 0 0)))))
Kenichi Handa's avatar
Kenichi Handa committed
238 239

(defsubst hangul2-input-method-moum (char)
Kenichi Handa's avatar
Kenichi Handa committed
240 241
  "Store Hangul Jamo indice CHAR in `hangul-queue'.
It is a Hangul 2-Bulsik Moum.
242
This function processes a Hangul 2-Bulsik Moum.
Kenichi Handa's avatar
Kenichi Handa committed
243
The Moum can be located in a Jungseong position.
244
Other parts are the same as a `hangul2-input-method-jaum'."
Kenichi Handa's avatar
Kenichi Handa committed
245 246 247 248 249 250 251
  (if (cond ((zerop (aref hangul-queue 2))
             (aset hangul-queue 2 char))
            ((and (zerop (aref hangul-queue 3))
                  (zerop (aref hangul-queue 4))
                  (notzerop (hangul-djamo 'jung (aref hangul-queue 2) char)))
             (aset hangul-queue 3 char)))
      (hangul-insert-character hangul-queue)
Kenichi Handa's avatar
Kenichi Handa committed
252 253 254 255 256 257 258 259 260
    (let ((next-char (vector 0 0 char 0 0 0)))
      (cond ((notzerop (aref hangul-queue 5))
	     (aset next-char 0 (aref hangul-queue 5))
	     (aset hangul-queue 5 0))
	    ((notzerop (aref hangul-queue 4))
	     (aset next-char 0 (aref hangul-queue 4))
	     (aset hangul-queue 4 0)))
      (hangul-insert-character hangul-queue
			       (setq hangul-queue next-char)))))
Kenichi Handa's avatar
Kenichi Handa committed
261 262

(defsubst hangul3-input-method-cho (char)
Kenichi Handa's avatar
Kenichi Handa committed
263 264
  "Store Hangul Jamo indice CHAR in `hangul-queue'.
It is a Hangul 3-Bulsik Choseong.
265
This function processes a Hangul 3-Bulsik Choseong.
Kenichi Handa's avatar
Kenichi Handa committed
266 267
The Hangul 3-Bulsik is composed of a Choseong, a Jungseong and a Jongseong.
The Choseong can be located in a Choseong position.
268
Other parts are the same as a `hangul2-input-method-jaum'."
Kenichi Handa's avatar
Kenichi Handa committed
269 270 271 272 273 274 275 276
  (if (cond ((and (zerop (aref hangul-queue 0))
                  (zerop (aref hangul-queue 4)))
             (aset hangul-queue 0 char))
            ((and (zerop (aref hangul-queue 1))
                  (zerop (aref hangul-queue 2))
                  (notzerop (hangul-djamo 'cho (aref hangul-queue 0) char)))
             (aset hangul-queue 1 char)))
      (hangul-insert-character hangul-queue)
Kenichi Handa's avatar
Kenichi Handa committed
277 278
    (hangul-insert-character hangul-queue
			     (setq hangul-queue (vector char 0 0 0 0 0)))))
Kenichi Handa's avatar
Kenichi Handa committed
279 280

(defsubst hangul3-input-method-jung (char)
Kenichi Handa's avatar
Kenichi Handa committed
281 282
  "Store Hangul Jamo indice CHAR in `hangul-queue'.
It is a Hangul 3-Bulsik Jungseong.
283
This function processes a Hangul 3-Bulsik Jungseong.
Kenichi Handa's avatar
Kenichi Handa committed
284
The Jungseong can be located in a Jungseong position.
285
Other parts are the same as a `hangul3-input-method-cho'."
Kenichi Handa's avatar
Kenichi Handa committed
286 287 288 289 290 291 292
  (if (cond ((and (zerop (aref hangul-queue 2))
                  (zerop (aref hangul-queue 4)))
             (aset hangul-queue 2 char))
            ((and (zerop (aref hangul-queue 3))
                  (notzerop (hangul-djamo 'jung (aref hangul-queue 2) char)))
             (aset hangul-queue 3 char)))
      (hangul-insert-character hangul-queue)
Kenichi Handa's avatar
Kenichi Handa committed
293 294
    (hangul-insert-character hangul-queue
			     (setq hangul-queue (vector 0 0 char 0 0 0)))))
Kenichi Handa's avatar
Kenichi Handa committed
295 296

(defsubst hangul3-input-method-jong (char)
Kenichi Handa's avatar
Kenichi Handa committed
297 298
  "Store Hangul Jamo indice CHAR in `hangul-queue'.
It is a Hangul 3-Bulsik Jongseong.
299
This function processes a Hangul 3-Bulsik Jongseong.
Kenichi Handa's avatar
Kenichi Handa committed
300
The Jongseong can be located in a Jongseong position.
301
Other parts are the same as a `hangul3-input-method-cho'."
Kenichi Handa's avatar
Kenichi Handa committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  (if (cond ((and (zerop (aref hangul-queue 4))
                  (notzerop (aref hangul-queue 0))
                  (notzerop (aref hangul-queue 2))
                  (numberp
                   (hangul-character
                    (+ (aref hangul-queue 0)
                       (hangul-djamo
                        'cho
                        (aref hangul-queue 0)
                        (aref hangul-queue 1)))
                    (+ (aref hangul-queue 2)
                       (hangul-djamo
                        'jung
                        (aref hangul-queue 2)
                        (aref hangul-queue 3)))
                    char)))
             (aset hangul-queue 4 char))
            ((and (zerop (aref hangul-queue 5))
                  (notzerop (hangul-djamo 'jong (aref hangul-queue 4) char))
                  (numberp
                   (hangul-character
                    (+ (aref hangul-queue 0)
                       (hangul-djamo
                        'cho
                        (aref hangul-queue 0)
                        (aref hangul-queue 1)))
                    (+ (aref hangul-queue 2)
                       (hangul-djamo
                        'jung
                        (aref hangul-queue 2)
                        (aref hangul-queue 3)))
                    (+ (aref hangul-queue 4)
                       (hangul-djamo
                        'jong
                        (aref hangul-queue 4)
                        char)))))
338
             (aset hangul-queue 5 char)))
Kenichi Handa's avatar
Kenichi Handa committed
339
      (hangul-insert-character hangul-queue)
Kenichi Handa's avatar
Kenichi Handa committed
340 341 342 343
    (if (zerop (apply '+ (append hangul-queue nil)))
	(hangul-insert-character (setq hangul-queue (vector 0 0 0 0 char 0)))
      (hangul-insert-character hangul-queue
			       (setq hangul-queue (vector 0 0 0 0 char 0))))))
Kenichi Handa's avatar
Kenichi Handa committed
344 345

(defun hangul-delete-backward-char ()
Kenichi Handa's avatar
Kenichi Handa committed
346
  "Delete the previous hangul character by Jaso units."
Kenichi Handa's avatar
Kenichi Handa committed
347 348 349 350 351 352 353
  (interactive)
  (let ((i 5))
    (while (and (> i 0) (zerop (aref hangul-queue i)))
      (setq i (1- i)))
    (aset hangul-queue i 0))
  (if (notzerop (apply '+ (append hangul-queue nil)))
      (hangul-insert-character hangul-queue)
354
    (delete-char -1)))
Kenichi Handa's avatar
Kenichi Handa committed
355

Kenichi Handa's avatar
Kenichi Handa committed
356
(defun hangul-to-hanja-conversion ()
357 358
  "Convert the previous hangul character to the corresponding hanja character.
When a Korean input method is off, convert the following hangul character."
Kenichi Handa's avatar
Kenichi Handa committed
359 360 361 362
  (interactive)
  (let ((echo-keystrokes 0)
        delete-func
        hanja-character)
363 364 365
    (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
        (progn
	  (setq hanja-character (hangul-to-hanja-char (preceding-char)))
366
	  (setq delete-func (lambda () (delete-char -1))))
367 368
      (setq hanja-character (hangul-to-hanja-char (following-char)))
      (setq delete-func (lambda () (delete-char 1))))
Kenichi Handa's avatar
Kenichi Handa committed
369
    (when hanja-character
370
      (funcall delete-func)
Kenichi Handa's avatar
Kenichi Handa committed
371 372
      (insert hanja-character)
      (setq hangul-queue (make-vector 6 0))
373 374
      (if (and (overlayp quail-overlay) (overlay-start quail-overlay))
	  (move-overlay quail-overlay (point) (point))))))
Kenichi Handa's avatar
Kenichi Handa committed
375

376 377 378
;; Support function for `hangul2-input-method'.  Actually, this
;; function handles the Hangul 2-Bulsik.  KEY is an entered key code
;; used for looking up `hangul2-keymap'."
Kenichi Handa's avatar
Kenichi Handa committed
379 380 381 382 383 384 385 386
(defun hangul2-input-method-internal (key)
  (let ((char (+ (aref hangul2-keymap (1- (% key 32)))
                 (cond ((or (= key ?O) (= key ?P)) 2)
                       ((or (= key ?E) (= key ?Q) (= key ?R)
                            (= key ?T) (= key ?W)) 1)
                       (t 0)))))
    (if (< char 31)
        (hangul2-input-method-jaum char)
Kenichi Handa's avatar
Kenichi Handa committed
387
      (hangul2-input-method-moum char))))
Kenichi Handa's avatar
Kenichi Handa committed
388

Kenichi Handa's avatar
Kenichi Handa committed
389
(defun hangul2-input-method (key)
390
  "2-Bulsik input method."
Kenichi Handa's avatar
Kenichi Handa committed
391 392
  (if (or buffer-read-only (not (alphabetp key)))
      (list key)
Kenichi Handa's avatar
Kenichi Handa committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    (quail-setup-overlays nil)
    (let ((input-method-function nil)
	  (echo-keystrokes 0)
	  (help-char nil))
      (setq hangul-queue (make-vector 6 0))
      (hangul2-input-method-internal key)
      (unwind-protect
	  (catch 'exit-input-loop
	    (while t
	      (let* ((seq (read-key-sequence nil))
		     (cmd (lookup-key hangul-im-keymap seq))
		     key)
		(cond ((and (stringp seq)
			    (= 1 (length seq))
			    (setq key (aref seq 0))
			    (alphabetp key))
		       (hangul2-input-method-internal key))
		      ((commandp cmd)
		       (call-interactively cmd))
		      (t
413 414 415
		       (setq unread-command-events
                             (nconc (listify-key-sequence seq)
                                    unread-command-events))
Kenichi Handa's avatar
Kenichi Handa committed
416 417
		       (throw 'exit-input-loop nil))))))
	(quail-delete-overlays)))))
Kenichi Handa's avatar
Kenichi Handa committed
418

419 420 421
;; Support function for `hangul3-input-method'.  Actually, this
;; function handles the Hangul 3-Bulsik final.  KEY is an entered key
;; code used for looking up `hangul3-keymap'."
Kenichi Handa's avatar
Kenichi Handa committed
422 423 424 425 426 427 428 429 430 431 432 433
(defun hangul3-input-method-internal (key)
  (let ((char (aref hangul3-keymap (- key 33))))
    (cond ((and (> char 92) (< char 123))
           (hangul3-input-method-cho (- char 92)))
          ((and (> char 65) (< char 87))
           (hangul3-input-method-jung (- char 35)))
          ((< char 31)
           (hangul3-input-method-jong char))
          (t
           (setq hangul-queue (make-vector 6 0))
           (insert (decode-char 'ucs char))
           (move-overlay quail-overlay (point) (point))))))
434

Kenichi Handa's avatar
Kenichi Handa committed
435
(defun hangul3-input-method (key)
436
  "3-Bulsik final input method."
Kenichi Handa's avatar
Kenichi Handa committed
437 438
  (if (or buffer-read-only (< key 33) (>= key 127))
      (list key)
Kenichi Handa's avatar
Kenichi Handa committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    (quail-setup-overlays nil)
    (let ((input-method-function nil)
	  (echo-keystrokes 0)
	  (help-char nil))
      (setq hangul-queue (make-vector 6 0))
      (hangul3-input-method-internal key)
      (unwind-protect
	  (catch 'exit-input-loop
	    (while t
	      (let* ((seq (read-key-sequence nil))
		     (cmd (lookup-key hangul-im-keymap seq))
		     key)
		(cond ((and (stringp seq)
			    (= 1 (length seq))
			    (setq key (aref seq 0))
			    (and (>= key 33) (< key 127)))
		       (hangul3-input-method-internal key))
		      ((commandp cmd)
		       (call-interactively cmd))
		      (t
459 460 461
		       (setq unread-command-events
                             (nconc (listify-key-sequence seq)
                                    unread-command-events))
Kenichi Handa's avatar
Kenichi Handa committed
462 463
		       (throw 'exit-input-loop nil))))))
	(quail-delete-overlays)))))
464

465 466 467
;; Support function for `hangul390-input-method'.  Actually, this
;; function handles the Hangul 3-Bulsik 390.  KEY is an entered key
;; code used for looking up `hangul390-keymap'."
Kenichi Handa's avatar
Kenichi Handa committed
468 469 470 471 472 473 474 475 476 477 478 479 480
(defun hangul390-input-method-internal (key)
  (let ((char (aref hangul390-keymap (- key 33))))
    (cond ((or (and (> char 86) (< char 91))
               (and (> char 96) (< char 123)))
           (hangul3-input-method-cho (- char (if (< char 97) 86 92))))
          ((and (> char 64) (< char 86))
           (hangul3-input-method-jung (- char 34)))
          ((< char 31)
           (hangul3-input-method-jong char))
          (t
           (setq hangul-queue (make-vector 6 0))
           (insert (decode-char 'ucs char))
           (move-overlay quail-overlay (point) (point))))))
481

Kenichi Handa's avatar
Kenichi Handa committed
482
(defun hangul390-input-method (key)
483
  "3-Bulsik 390 input method."
Kenichi Handa's avatar
Kenichi Handa committed
484 485
  (if (or buffer-read-only (< key 33) (>= key 127))
      (list key)
Kenichi Handa's avatar
Kenichi Handa committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    (quail-setup-overlays nil)
    (let ((input-method-function nil)
	  (echo-keystrokes 0)
	  (help-char nil))
      (setq hangul-queue (make-vector 6 0))
      (hangul390-input-method-internal key)
      (unwind-protect
	  (catch 'exit-input-loop
	    (while t
	      (let* ((seq (read-key-sequence nil))
		     (cmd (lookup-key hangul-im-keymap seq))
		     key)
		(cond ((and (stringp seq)
			    (= 1 (length seq))
			    (setq key (aref seq 0))
			    (and (>= key 33) (< key 127)))
		       (hangul390-input-method-internal key))
		      ((commandp cmd)
		       (call-interactively cmd))
		      (t
506 507 508
		       (setq unread-command-events
                             (nconc (listify-key-sequence seq)
                                    unread-command-events))
Kenichi Handa's avatar
Kenichi Handa committed
509 510
		       (throw 'exit-input-loop nil))))))
	(quail-delete-overlays)))))
511

Kenichi Handa's avatar
Kenichi Handa committed
512 513 514 515
;; Text shown by describe-input-method.  Set to a proper text by
;; hangul-input-method-activate.
(defvar hangul-input-method-help-text nil)
(make-variable-buffer-local 'hangul-input-method-help-text)
516

517
;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
518 519 520 521
(defun hangul-input-method-activate (input-method func help-text &rest args)
  "Activate Hangul input method INPUT-METHOD.
FUNC is a function to handle input key.
HELP-TEXT is a text set in `hangul-input-method-help-text'."
522
  (setq deactivate-current-input-method-function 'hangul-input-method-deactivate
Kenichi Handa's avatar
Kenichi Handa committed
523 524
	describe-current-input-method-function 'hangul-input-method-help
	hangul-input-method-help-text help-text)
Kenichi Handa's avatar
Kenichi Handa committed
525 526 527 528
  (quail-delete-overlays)
  (if (eq (selected-window) (minibuffer-window))
      (add-hook 'minibuffer-exit-hook 'quail-exit-from-minibuffer))
  (set (make-local-variable 'input-method-function) func))
529

530 531
(defun hangul-input-method-deactivate ()
  "Deactivate the current Hangul input method."
Kenichi Handa's avatar
Kenichi Handa committed
532 533 534
  (interactive)
  (unwind-protect
      (progn
Kenichi Handa's avatar
Kenichi Handa committed
535 536 537
        (quail-hide-guidance)
        (quail-delete-overlays)
        (setq describe-current-input-method-function nil))
Kenichi Handa's avatar
Kenichi Handa committed
538
    (kill-local-variable 'input-method-function)))
539

540 541
(define-obsolete-function-alias
  'hangul-input-method-inactivate
542
  'hangul-input-method-deactivate "24.3")
543

Kenichi Handa's avatar
Kenichi Handa committed
544 545 546 547 548
(defun hangul-input-method-help ()
  "Describe the current Hangul input method."
  (interactive)
  (with-output-to-temp-buffer "*Help*"
    (princ hangul-input-method-help-text)))
549

Kenichi Handa's avatar
Kenichi Handa committed
550
(provide 'hangul)
551

552 553 554 555
;; Local Variables:
;; generated-autoload-load-name: "quail/hangul"
;; End:

556
;;; hangul.el ends here