dynwind fixes
[bpt/emacs.git] / lisp / international / titdic-cnv.el
CommitLineData
c7211fed 1;;; titdic-cnv.el --- convert cxterm dictionary (TIT format) to Quail package -*- coding:iso-2022-7bit; -*-
4ed46869 2
ba318903 3;; Copyright (C) 1997-1998, 2000-2014 Free Software Foundation, Inc.
7976eda0 4;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5df4f04c 5;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
2fd125a3
KH
6;; National Institute of Advanced Industrial Science and Technology (AIST)
7;; Registration Number H14PRO021
8f924df7
KH
8;; Copyright (C) 2003
9;; National Institute of Advanced Industrial Science and Technology (AIST)
10;; Registration Number H13PRO009
4ed46869
KH
11
12;; Keywords: Quail, TIT, cxterm
13
14;; This file is part of GNU Emacs.
15
4936186e 16;; GNU Emacs is free software: you can redistribute it and/or modify
4ed46869 17;; it under the terms of the GNU General Public License as published by
4936186e
GM
18;; the Free Software Foundation, either version 3 of the License, or
19;; (at your option) any later version.
4ed46869
KH
20
21;; GNU Emacs is distributed in the hope that it will be useful,
22;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24;; GNU General Public License for more details.
25
26;; You should have received a copy of the GNU General Public License
4936186e 27;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
4ed46869 28
60370d40 29;;; Commentary:
4ed46869 30
49ed466f 31;; Convert cxterm dictionary (of TIT format) to quail-package.
4ed46869
KH
32;;
33;; Usage (within Emacs):
49ed466f 34;; M-x titdic-convert<CR>CXTERM-DICTIONARY-NAME<CR>
4ed46869 35;; Usage (from shell):
49ed466f 36;; % emacs -batch -l titdic-cnv -f batch-titdic-convert\
4ed46869
KH
37;; [-dir DIR] [DIR | FILE] ...
38;;
39;; When you run titdic-convert within Emacs, you have a chance to
40;; modify arguments of `quail-define-package' before saving the
41;; converted file. For instance, you are likely to modify TITLE,
42;; DOCSTRING, and KEY-BINDINGS.
43
49ed466f 44;; Cxterm dictionary file (*.tit) is a line-oriented text (English,
4ed46869
KH
45;; Chinese, Japanese, and Korean) file. The whole file contains of
46;; two parts, the definition part (`header' here after) followed by
47;; the dictionary part (`body' here after). All lines begin with
48;; leading '#' are ignored.
49;;
50;; Each line in the header part has two fields, KEY and VALUE. These
51;; fields are separated by one or more white characters.
52;;
53;; Each line in the body part has two fields, KEYSEQ and TRANSLATIONS.
54;; These fields are separated by one or more white characters.
55;;
56;; See the manual page of `tit2cit' of cxterm distribution for more
57;; detail.
c063e381 58;;
b138056a 59;; Near the end of this file, we also have a few other tools to convert
c063e381 60;; miscellaneous dictionaries.
4ed46869
KH
61
62;;; Code:
63
64(require 'quail)
65
49ed466f 66;; List of values of key "ENCODE:" and the corresponding Emacs
4ed46869
KH
67;; coding-system and language environment name.
68(defvar tit-encode-list
a7f2c216
KH
69 '(("GB" euc-china "Chinese-GB")
70 ("BIG5" cn-big5 "Chinese-BIG5")
71 ("JIS" euc-japan "Japanese")
49ed466f
KH
72 ("KS" euc-kr "Korean")))
73
4558e816
KH
74;; Alist of input method names and the corresponding title and extra
75;; docstring. For each of input method generated from TIT dictionary,
76;; a docstring is automatically generated from the comments in the
77;; dictionary. The extra docstring in this alist is to add more
78;; information.
79;; The command describe-input-method shows the automatically generated
c7211fed 80;; docstring, then an extra docstring while replacing the form \<VAR>
4558e816
KH
81;; by the value of variable VAR. For instance, the form
82;; \<quail-translation-docstring> is replaced by a description about
83;; how to select a translation from a list of candidates.
84
6b1e079c
KH
85(defvar quail-cxterm-package-ext-info
86 '(("chinese-4corner" "\e$(0(?-F\e(B")
87 ("chinese-array30" "\e$(0#R#O\e(B")
4558e816
KH
88 ("chinese-ccdospy" "\e$AKuF4\e(B"
89 "Pinyin base input method for Chinese charset GB2312 \(`chinese-gb2312').
90
c7211fed 91Pinyin is the standard Roman transliteration method for Chinese.
4558e816
KH
92For the detail of Pinyin system, see the documentation of the input
93method `chinese-py'.
94
95This input method works almost the same way as `chinese-py'. The
96difference is that you type a single key for these Pinyin spelling.
97 Pinyin: zh en eng ang ch an ao ai ong sh ing yu(\e$A(9\e(B)
98 keyseq: a f g h i j k l s u y v
c7211fed 99For example:
4558e816
KH
100 Chinese: \e$A0!\e(B \e$A9{\e(B \e$AVP\e(B \e$AND\e(B \e$A9b\e(B \e$ASq\e(B \e$AH+\e(B
101 Pinyin: a guo zhong wen guang yu quan
102 Keyseq: a1 guo4 as1 wf4 guh1 yu..6 qvj6
103
104\\<quail-translation-docstring>
105
bbd240ce 106For double-width GB2312 characters corresponding to ASCII, use the
4558e816
KH
107input method `chinese-qj'.")
108
4558e816 109 ("chinese-ecdict" "\e$(05CKH\e(B"
c0943d3d 110"In this input method, you enter a Chinese (Big5) character or word
4558e816
KH
111by typing the corresponding English word. For example, if you type
112\"computer\", \"\e$(0IZH+\e(B\" is input.
113
114\\<quail-translation-docstring>")
115
116 ("chinese-etzy" "\e$(06/0D\e(B"
117"Zhuyin base input method for Chinese Big5 characters (`chinese-big5-1',
118`chinese-big5-2').
119
120Zhuyin is a kind of phonetic symbol. One to three Zhuyin symbols
121compose one Chinese character.
122
123In this input method, you enter a Chinese character by first typing
124keys corresponding to Zhuyin symbols (see the above table) followed by
c80e3b4a 125SPC, 1, 2, 3, or 4 specifying a tone (SPC:\e$(0?v(N\e(B, 1:\e$(0M=Vy\e(B, 2:\e$(0Dm(N\e(B, 3: \e$(0&9Vy\e(B,
4558e816
KH
1264:\e$(0(+Vy\e(B).
127
128\\<quail-translation-docstring>")
6b1e079c
KH
129
130 ("chinese-punct-b5" "\e$(0O:\e(BB"
e4920bc9 131 "Input method for Chinese punctuation and symbols of Big5
6b1e079c
KH
132\(`chinese-big5-1' and `chinese-big5-2').")
133
134 ("chinese-punct" "\e$A1j\e(BG"
e4920bc9 135 "Input method for Chinese punctuation and symbols of GB2312
6b1e079c
KH
136\(`chinese-gb2312').")
137
138 ("chinese-py-b5" "\e$(03<\e(BB"
139 "Pinyin base input method for Chinese Big5 characters
140\(`chinese-big5-1', `chinese-big5-2').
141
142This input method works almost the same way as `chinese-py' (which
143see).
144
145This input method supports only Han characters. The more convenient
43b11fee
EZ
146method is `chinese-py-punct-b5', which is the combination of this
147method and `chinese-punct-b5' and which supports both Han characters
148and punctuation/symbols.
6b1e079c 149
43b11fee 150For double-width Big5 characters corresponding to ASCII, use the input
6b1e079c
KH
151method `chinese-qj-b5'.
152
153The input method `chinese-py' and `chinese-tonepy' are also Pinyin
43b11fee 154based, but for the character set GB2312 (`chinese-gb2312').")
6b1e079c 155
4558e816
KH
156 ("chinese-qj-b5" "\e$(0)A\e(BB")
157
158 ("chinese-qj" "\e$AH+\e(BG")
159
6b1e079c 160 ("chinese-sw" "\e$AJWN2\e(B"
4558e816
KH
161"Radical base input method for Chinese charset GB2312 (`chinese-gb2312').
162
d20faceb
EZ
163In this input method, you enter a Chinese character by typing two
164keys. The first key corresponds to the first (\e$AJW\e(B) radical, the second
165key corresponds to the last (\e$AN2\e(B) radical. The correspondence of keys
166and radicals is as below:
4558e816
KH
167
168 first radical:
169 a b c d e f g h i j k l m n o p q r s t u v w x y z
a1506d29 170 \e$APD\e(B \e$AZ"\e(B \e$AJ,\e(B \e$AX<\e(B \e$A;p\e(B \e$A?Z\e(B \e$A^P\e(B \e$Ac_\e(B \e$AZ%\e(B \e$A\3\e(B \e$AXi\e(B \e$AD>\e(B \e$Alj\e(B \e$Ab;\e(B \e$ATB\e(B \e$Afy\e(B \e$AJ/\e(B \e$AMu\e(B \e$A0K\e(B \e$AX/\e(B \e$AHU\e(B \e$AeA\e(B \e$Aak\e(B \e$AVq\e(B \e$AR;\e(B \e$AHK\e(B
4558e816
KH
171 last radical:
172 a b c d e f g h i j k l m n o p q r s t u v w x y z
a1506d29 173 \e$ASV\e(B \e$AI=\e(B \e$AMA\e(B \e$A56\e(B \e$AZb\e(B \e$A?Z\e(B \e$ARB\e(B \e$Aqb\e(B \e$A4s\e(B \e$A6!\e(B \e$A[L\e(B \e$Ala\e(B \e$AJ.\e(B \e$A4u\e(B \e$AXg\e(B \e$ACE\e(B \e$A=q\e(B \e$AX-\e(B \e$AE.\e(B \e$ARR\e(B \e$A`m\e(B \e$AP!\e(B \e$A3'\e(B \e$A3f\e(B \e$A_.\e(B \e$A27\e(B
4558e816 174
43e5a7fe 175\\<quail-translation-docstring>")
4558e816 176
6b1e079c
KH
177 ("chinese-tonepy" "\e$A5wF4\e(B"
178 "Pinyin base input method for Chinese charset GB2312 (`chinese-gb2312').
179
c3ff164a 180Pinyin is the standard roman transliteration method for Chinese.
d20faceb 181For the details of Pinyin system, see the documentation of the input
6b1e079c
KH
182method `chinese-py'.
183
184This input method works almost the same way as `chinese-py'. The
4558e816
KH
185difference is that you must type 1..5 after each Pinyin spelling to
186specify a tone (1:\e$ARuF=\e(B, 2:\e$AQtF=\e(B, 3:\e$AIOIy\e(B, 4\e$AOBIy\e(B, 5:\e$AGaIy\e(B).
187
43e5a7fe 188\\<quail-translation-docstring>
4558e816
KH
189
190For instance, to input \e$ADc\e(B, you type \"n i 3 3\", the first \"n i\" is
191a Pinyin, the next \"3\" specifies tone, and the last \"3\" selects
192the third character from the candidate list.
6b1e079c 193
bbd240ce 194For double-width GB2312 characters corresponding to ASCII, use the
6b1e079c
KH
195input method `chinese-qj'.")
196
4558e816
KH
197 ("chinese-zozy" "\e$(0I\0D\e(B"
198"Zhuyin base input method for Chinese Big5 characters (`chinese-big5-1',
199`chinese-big5-2').
200
d20faceb 201Zhuyin is a kind of a phonetic symbol. One to three Zhuyin symbols
4558e816
KH
202compose a Chinese character.
203
204In this input method, you enter a Chinese character by first typing
205keys corresponding to Zhuyin symbols (see the above table) followed by
c80e3b4a 206SPC, 6, 3, 4, or 7 specifying a tone (SPC:\e$(0?v(N\e(B, 6:\e$(0Dm(N\e(B, 3:\e$(0&9Vy\e(B, 4:\e$(0(+Vy\e(B,
4558e816
KH
2077:\e$(0M=Vy\e(B).
208
43e5a7fe 209\\<quail-translation-docstring>")))
4ed46869
KH
210
211;; Return a value of the key in the current line.
212(defsubst tit-read-key-value ()
6b61353c 213 (if (looking-at "[^ \t\r\n]+")
4ed46869
KH
214 (car (read-from-string (concat "\"" (match-string 0) "\"")))))
215
216;; Return an appropriate quail-package filename from FILENAME (TIT
49ed466f
KH
217;; dictionary filename). For instance, ".../ZOZY.tit" -> "ZOZY.el".
218(defun tit-make-quail-package-file-name (filename &optional dirname)
4ed46869 219 (expand-file-name
49ed466f 220 (concat (file-name-nondirectory (substring filename 0 -4)) ".el")
4ed46869
KH
221 dirname))
222
1375754c 223;; This value is nil if we are processing phrase dictionary.
8c1ccc6c 224(defvar tit-dictionary t)
4ed46869
KH
225(defvar tit-encode nil)
226(defvar tit-default-encode "GB")
227
228;; Generate elements of KEY-BINDINGS arg for `quail-define-package' so
229;; that each characters in KEYS invokes FUNCTION-SYMBOL.
230(defun tit-generate-key-bindings (keys function-symbol)
231 (let ((len (length keys))
232 (i 0)
1375754c 233 (first t)
4ed46869
KH
234 key)
235 (while (< i len)
1375754c 236 (or first (princ "\n "))
4ed46869 237 (setq key (aref keys i))
1375754c
KH
238 (if (if (< key ?\ )
239 (eq (lookup-key quail-translation-keymap
240 (char-to-string key))
4ed46869 241 'quail-execute-non-quail-command)
1375754c
KH
242 (<= key 127))
243 (progn
244 (princ (cons (cond ((< key ?\ ) (format "\"\\C-%c\"" (+ key ?@)))
245 ((< key 127) (format "\"%c\"" key))
246 (t "\"\\C-?\""))
247 function-symbol))
248 (setq first nil)))
4ed46869
KH
249 (setq i (1+ i)))))
250
251;; Analyze header part of TIT dictionary and generate an appropriate
252;; `quail-define-package' function call.
253(defun tit-process-header (filename)
254 (message "Processing header part...")
255 (goto-char (point-min))
256
1375754c
KH
257 ;; At first, generate header part of the Quail package while
258 ;; collecting information from the original header.
259 (let ((package (concat
260 "chinese-"
261 (substring (downcase (file-name-nondirectory filename))
262 0 -4)))
263 ;; TIT keywords and the corresponding default values.
4ed46869
KH
264 (tit-multichoice t)
265 (tit-prompt "")
266 (tit-comments nil)
267 (tit-backspace "\010\177")
268 (tit-deleteall "\015\025")
269 (tit-moveright ".>")
270 (tit-moveleft ",<")
271 (tit-keyprompt nil))
1375754c
KH
272
273 (princ ";; Quail package `")
98223b73 274 (princ package)
cb6c95a3 275 (princ "\n")
011c4552
GM
276 (princ ";; Generated by the command `titdic-convert'\n")
277 (princ ";;\tOriginal TIT dictionary file: ")
1375754c 278 (princ (file-name-nondirectory filename))
cb6c95a3 279 (princ "\n\n")
1375754c 280
4ed46869 281 (while (not (eobp))
1375754c
KH
282 (let ((ch (following-char))
283 (pos (point)))
4ed46869
KH
284 (cond ((= ch ?C) ; COMMENT
285 (cond ((looking-at "COMMENT")
286 (let ((pos (match-end 0)))
287 (end-of-line)
8b735b2b
KH
288 (setq tit-comments
289 (cons (buffer-substring-no-properties pos (point))
290 tit-comments))))))
4ed46869
KH
291 ((= ch ?M) ; MULTICHOICE, MOVERIGHT, MOVELEFT
292 (cond ((looking-at "MULTICHOICE:[ \t]*")
293 (goto-char (match-end 0))
294 (setq tit-multichoice (looking-at "YES")))
295 ((looking-at "MOVERIGHT:[ \t]*")
296 (goto-char (match-end 0))
297 (setq tit-moveright (tit-read-key-value)))
298 ((looking-at "MOVELEFT:[ \t]*")
299 (goto-char (match-end 0))
300 (setq tit-moveleft (tit-read-key-value)))))
301 ((= ch ?P) ; PROMPT
302 (cond ((looking-at "PROMPT:[ \t]*")
303 (goto-char (match-end 0))
6b1e079c 304 (setq tit-prompt (tit-read-key-value))
4c36be58 305 ;; Some TIT dictionaries that are encoded by
6b1e079c
KH
306 ;; euc-china contains invalid character at the tail.
307 (let* ((last (aref tit-prompt (1- (length tit-prompt))))
308 (split (split-char last)))
309 (if (or (eq (nth 1 split) 32)
310 (eq (nth 2 split) 32))
311 (setq tit-prompt (substring tit-prompt 0 -1)))))))
4ed46869
KH
312 ((= ch ?B) ; BACKSPACE, BEGINDICTIONARY,
313 ; BEGINPHRASE
314 (cond ((looking-at "BACKSPACE:[ \t]*")
315 (goto-char (match-end 0))
316 (setq tit-backspace (tit-read-key-value)))
317 ((looking-at "BEGINDICTIONARY")
1375754c 318 (setq tit-dictionary t))
4ed46869 319 ((looking-at "BEGINPHRASE")
1375754c 320 (setq tit-dictionary nil))))
4ed46869
KH
321 ((= ch ?K) ; KEYPROMPT
322 (cond ((looking-at "KEYPROMPT(\\(.*\\)):[ \t]*")
323 (let ((key-char (match-string 1)))
324 (goto-char (match-end 0))
1fa1e1f5
RS
325 (if (string-match "\\\\[0-9]+" key-char)
326 (setq key-char
327 (car (read-from-string (format "\"%s\""
328 key-char)))))
4ed46869
KH
329 (setq tit-keyprompt
330 (cons (cons key-char (tit-read-key-value))
1375754c
KH
331 tit-keyprompt)))))))
332 (end-of-line)
333 (princ ";; ")
8b735b2b 334 (princ (buffer-substring-no-properties pos (point)))
1375754c
KH
335 (princ "\n")
336 (forward-line 1)))
a1506d29 337
1375754c
KH
338 (princ "\n;;; End of the header of original TIT dictionary.\n\n")
339 (princ ";;; Code:\n\n(require 'quail)\n\n")
340
341 (princ "(quail-define-package ")
342 ;; Args NAME, LANGUAGE, TITLE
6b1e079c 343 (let ((title (nth 1 (assoc package quail-cxterm-package-ext-info))))
1375754c
KH
344 (princ "\"")
345 (princ package)
346 (princ "\" \"")
347 (princ (nth 2 (assoc tit-encode tit-encode-list)))
348 (princ "\" \"")
349 (princ (or title
350 (if (string-match "[:\e$A!K\e$(0!(!J\e(B]+\\([^:\e$A!K\e$(0!(!K\e(B]+\\)" tit-prompt)
351 (substring tit-prompt (match-beginning 1) (match-end 1))
352 tit-prompt)))
353 (princ "\"\n"))
4ed46869
KH
354
355 ;; Arg GUIDANCE
356 (if tit-keyprompt
357 (progn
1375754c 358 (princ " '(")
4ed46869 359 (while tit-keyprompt
1375754c
KH
360 (princ " ")
361 (princ (format "(%d . \"%s\")\n"
362 (string-to-char (car (car tit-keyprompt)))
363 (cdr (car tit-keyprompt))))
4ed46869 364 (setq tit-keyprompt (cdr tit-keyprompt)))
1375754c
KH
365 (princ ")"))
366 (princ " t\n"))
4ed46869
KH
367
368 ;; Arg DOCSTRING
6b1e079c
KH
369 (let ((doc (concat tit-prompt "\n"))
370 (comments (if tit-comments
371 (mapconcat 'identity (nreverse tit-comments) "\n")))
372 (doc-ext (nth 2 (assoc package quail-cxterm-package-ext-info))))
373 (if comments
374 (setq doc (concat doc "\n" comments "\n")))
375 (if doc-ext
376 (setq doc (concat doc "\n" doc-ext "\n")))
377 (prin1 doc)
378 (terpri))
4ed46869
KH
379
380 ;; Arg KEY-BINDINGS
1375754c 381 (princ " '(")
4ed46869 382 (tit-generate-key-bindings tit-backspace 'quail-delete-last-char)
1375754c 383 (princ "\n ")
4ed46869 384 (tit-generate-key-bindings tit-deleteall 'quail-abort-translation)
1375754c 385 (princ "\n ")
4ed46869 386 (tit-generate-key-bindings tit-moveright 'quail-next-translation)
1375754c 387 (princ "\n ")
4ed46869 388 (tit-generate-key-bindings tit-moveleft 'quail-prev-translation)
1375754c 389 (princ ")\n")
4ed46869
KH
390
391 ;; Args FORGET-TRANSLATION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT.
392 ;; The remaining args are all nil.
1375754c
KH
393 (princ " nil")
394 (princ (if tit-multichoice " nil" " t"))
395 (princ (if tit-keyprompt " t t)\n\n" " nil nil)\n\n"))))
396
397(defsubst tit-flush-translations (key translations)
398 (if (string-match "\\\\[0-9][0-9][0-9]" key)
399 (let ((newkey (concat (substring key 0 (match-beginning 0))
400 (car (read-from-string
401 (concat "\"" (match-string 0 key) "\"")))))
402 (idx (match-end 0)))
403 (while (string-match "\\\\[0-9][0-9][0-9]" key idx)
404 (setq newkey (concat
405 newkey
406 (substring key idx (match-beginning 0))
407 (car (read-from-string
408 (concat "\"" (match-string 0 key) "\"")))))
409 (setq idx (match-end 0)))
410 (setq key (concat newkey (substring key idx)))))
411 (prin1 (list key (if tit-dictionary translations
412 (vconcat (nreverse translations)))))
413 (princ "\n"))
4ed46869
KH
414
415;; Convert body part of TIT dictionary into `quail-define-rules'
416;; function call.
417(defun tit-process-body ()
418 (message "Formatting translation rules...")
1375754c
KH
419 (let* ((template (list nil nil))
420 (second (cdr template))
421 (prev-key "")
422 ch key translations pos)
423 (princ "(quail-define-rules\n")
4ed46869 424 (while (null (eobp))
1375754c
KH
425 (setq ch (following-char))
426 (if (or (= ch ?#) (= ch ?\n))
427 (forward-line 1)
4ed46869 428 (setq pos (point))
1375754c 429 (skip-chars-forward "^ \t\n")
8b735b2b 430 (setq key (buffer-substring-no-properties pos (point)))
4ed46869 431 (skip-chars-forward " \t")
1375754c
KH
432 (setq ch (following-char))
433 (if (or (= ch ?#) (= ch ?\n))
08a1bf22 434 ;; This entry contains no translations. Let's ignore it.
1375754c
KH
435 (forward-line 1)
436 (or (string= key prev-key)
08a1bf22 437 (progn
1375754c
KH
438 (if translations
439 (tit-flush-translations prev-key translations))
440 (setq translations nil
441 prev-key key)))
442 (if tit-dictionary
443 (progn
444 (setq pos (point))
445 (skip-chars-forward "^ \t#\n")
446 (setq translations
447 (if translations
448 (concat translations
8b735b2b
KH
449 (buffer-substring-no-properties pos (point)))
450 (buffer-substring-no-properties pos (point)))))
1375754c
KH
451 (while (not (eolp))
452 (setq pos (point))
453 (skip-chars-forward "^ \t\n")
8b735b2b
KH
454 (setq translations (cons (buffer-substring-no-properties
455 pos (point))
1375754c
KH
456 translations))
457 (skip-chars-forward " \t")
458 (setq ch (following-char))
459 (if (= ch ?#) (end-of-line))))
08a1bf22 460 (forward-line 1))))
1375754c
KH
461
462 (if translations
463 (tit-flush-translations prev-key translations))
464 (princ ")\n")))
4ed46869
KH
465
466;;;###autoload
467(defun titdic-convert (filename &optional dirname)
468 "Convert a TIT dictionary of FILENAME into a Quail package.
469Optional argument DIRNAME if specified is the directory name under which
470the generated Quail package is saved."
471 (interactive "FTIT dictionary file: ")
98223b73 472 (let ((coding-system-for-write nil))
c7211fed 473 (with-temp-file (tit-make-quail-package-file-name filename dirname)
c7211fed
DL
474 (let ((standard-output (current-buffer)))
475 (with-temp-buffer
476 (set-buffer-multibyte nil)
f9362982
KH
477 ;; Here we must use `raw-text' instead of `no-conversion' to
478 ;; enable auto-decoding of eol format (CRLF->LF).
479 (let ((coding-system-for-read 'raw-text))
c7211fed 480 (insert-file-contents (expand-file-name filename)))
a1506d29 481
c7211fed
DL
482 ;; Decode the buffer contents from the encoding specified by a
483 ;; value of the key "ENCODE:".
484 (if (not (search-forward "\nBEGIN" nil t))
485 (error "TIT dictionary doesn't have body part"))
486 (let ((limit (point))
487 coding-system slot)
488 (goto-char (point-min))
489 (if (re-search-forward "^ENCODE:[ \t]*" limit t)
490 (progn
491 (goto-char (match-end 0))
492 (setq tit-encode (tit-read-key-value)))
493 (setq tit-encode tit-default-encode))
494 (setq slot (assoc tit-encode tit-encode-list))
495 (if (not slot)
496 (error "Invalid ENCODE: value in TIT dictionary"))
497 (setq coding-system (nth 1 slot))
498 (message "Decoding with coding system %s..." coding-system)
499 (goto-char (point-min))
98223b73 500 (decode-coding-region (point-min) (point-max) coding-system)
f9362982
KH
501 ;; Explicitly set eol format to `unix'.
502 (setq coding-system-for-write
503 (coding-system-change-eol-conversion coding-system 'unix))
98223b73 504 (remove-text-properties (point-min) (point-max) '(charset nil)))
c7211fed 505
66b820ba 506 (set-buffer-multibyte t)
c7211fed 507 ;; Set point the starting position of the body part.
1375754c 508 (goto-char (point-min))
c7211fed
DL
509 (if (not (search-forward "\nBEGIN" nil t))
510 (error "TIT dictionary can't be decoded correctly"))
511
2db18278 512 ;; Process the header part.
c7211fed
DL
513 (forward-line 1)
514 (narrow-to-region (point-min) (point))
515 (tit-process-header filename)
516 (widen)
517
8f924df7 518 ;; Process the body part
cb6c95a3
GM
519 (tit-process-body)
520
521 (princ ";; Local Variables:\n")
522 (princ ";; version-control: never\n")
523 (princ ";; no-update-autoloads: t\n")
524 (princ (format ";; coding: %s\n" coding-system-for-write))
525 (princ ";; End:\n"))))))
4ed46869
KH
526
527;;;###autoload
44cbfae9 528(defun batch-titdic-convert (&optional force)
4ed46869
KH
529 "Run `titdic-convert' on the files remaining on the command line.
530Use this from the command line, with `-batch';
531it won't work in an interactive Emacs.
532For example, invoke \"emacs -batch -f batch-titdic-convert XXX.tit\" to
533 generate Quail package file \"xxx.el\" from TIT dictionary file \"XXX.tit\".
534To get complete usage, invoke \"emacs -batch -f batch-titdic-convert -h\"."
535 (defvar command-line-args-left) ; Avoid compiler warning.
536 (if (not noninteractive)
537 (error "`batch-titdic-convert' should be used only with -batch"))
538 (if (string= (car command-line-args-left) "-h")
539 (progn
540 (message "To convert XXX.tit and YYY.tit into xxx.el and yyy.el:")
541 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert XXX.tit YYY.tit")
542 (message "To convert XXX.tit into DIR/xxx.el:")
543 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert -dir DIR XXX.tit"))
544 (let (targetdir filename files file)
545 (if (string= (car command-line-args-left) "-dir")
546 (progn
547 (setq command-line-args-left (cdr command-line-args-left))
548 (setq targetdir (car command-line-args-left))
549 (setq command-line-args-left (cdr command-line-args-left))))
550 (while command-line-args-left
551 (setq filename (expand-file-name (car command-line-args-left)))
552 (if (file-directory-p filename)
553 (progn
554 (message "Converting all tit files in the directory %s" filename)
555 (setq files (directory-files filename t "\\.tit$")))
556 (setq files (list filename)))
557 (while files
558 (setq file (expand-file-name (car files)))
44cbfae9
KH
559 (when (or force
560 (file-newer-than-file-p
561 file (tit-make-quail-package-file-name file targetdir)))
562 (message "Converting %s to quail-package..." file)
563 (titdic-convert file targetdir))
4ed46869
KH
564 (setq files (cdr files)))
565 (setq command-line-args-left (cdr command-line-args-left)))
86e4f7c0 566 (message "Byte-compile the created files by:")
4ed46869
KH
567 (message " %% emacs -batch -f batch-byte-compile XXX.el")))
568 (kill-emacs 0))
569
c063e381
KH
570\f
571;;; Converter of miscellaneous dictionaries other than TIT format.
572
573;; Alist of input method names and the corresponding information.
574;; Each element has this form:
575;; (INPUT-METHOD-NAME ;; Name of the input method.
a1506d29 576;; INPUT-METHOD-TITLE ;; Title string of the input method
c063e381
KH
577;; DICFILE ;; Name of the source dictionary file.
578;; CODING ;; Coding system of the dictionary file.
579;; QUAILFILE ;; Name of the Quail package file.
580;; CONVERTER ;; Function to generate the Quail package.
581;; COPYRIGHT-NOTICE ;; Copyright notice of the source dictionary.
582;; )
583
584(defvar quail-misc-package-ext-info
585 '(("chinese-b5-tsangchi" "\e$(06A\e(BB"
a1506d29 586 "cangjie-table.b5" big5 "tsang-b5.el"
c063e381
KH
587 tsang-b5-converter
588 "\
589;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
590;; #
591;; # Permission to copy and distribute both modified and
592;; # unmodified versions is granted without royalty provided
593;; # this notice is preserved.")
594
595 ("chinese-b5-quick" "\e$(0X|\e(BB"
a1506d29 596 "cangjie-table.b5" big5 "quick-b5.el"
c063e381
KH
597 quick-b5-converter
598 "\
599;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
600;; #
601;; # Permission to copy and distribute both modified and
602;; # unmodified versions is granted without royalty provided
603;; # this notice is preserved.")
604
605 ("chinese-cns-tsangchi" "\e$(GT?\e(BC"
606 "cangjie-table.cns" iso-2022-cn-ext "tsang-cns.el"
607 tsang-cns-converter
608 "\
609;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
610;; #
611;; # Permission to copy and distribute both modified and
612;; # unmodified versions is granted without royalty provided
613;; # this notice is preserved.")
614
615 ("chinese-cns-quick" "\e$(Gv|\e(BC"
616 "cangjie-table.cns" iso-2022-cn-ext "quick-cns.el"
617 quick-cns-converter
618 "\
619;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
620;; #
621;; # Permission to copy and distribute both modified and
622;; # unmodified versions is granted without royalty provided
623;; # this notice is preserved.")
624
625 ("chinese-py" "\e$AF4\e(BG"
626 "pinyin.map" cn-gb-2312 "PY.el"
627 py-converter
628 "\
629;; \"pinyin.map\" is included in a free package called CCE. It is
630;; available at:
631;; http://ftp.debian.org/debian/dists/potato/main
632;; /source/utils/cce_0.36.orig.tar.gz
633;; This package contains the following copyright notice.
634;;
635;;
636;; Copyright (C) 1999, Rui He, herui@cs.duke.edu
a1506d29
JB
637;;
638;;
c063e381 639;; CCE(Console Chinese Environment) 0.32
a1506d29
JB
640;;
641;; CCE is free software; you can redistribute it and/or modify it under the
642;; terms of the GNU General Public License as published by the Free Software
643;; Foundation; either version 1, or (at your option) any later version.
644;;
645;; CCE is distributed in the hope that it will be useful, but WITHOUT ANY
646;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
647;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
648;; details.
649;;
c063e381 650;; You should have received a copy of the GNU General Public License along with
fee0bd5f 651;; CCE. If not, see <http://www.gnu.org/licenses/>.")
c063e381
KH
652
653 ("chinese-ziranma" "\e$AWTH;\e(B"
654 "ziranma.cin" cn-gb-2312 "ZIRANMA.el"
655 ziranma-converter
656 "\
657;; \"ziranma.cin\" is included in a free package called CCE. It is
658;; available at:
659;; http://ftp.debian.org/debian/dists/potato/main
660;; /source/utils/cce_0.36.orig.tar.gz
661;; This package contains the following copyright notice.
662;;
663;;
664;; Copyright (C) 1999, Rui He, herui@cs.duke.edu
a1506d29
JB
665;;
666;;
c063e381 667;; CCE(Console Chinese Environment) 0.32
a1506d29
JB
668;;
669;; CCE is free software; you can redistribute it and/or modify it under the
670;; terms of the GNU General Public License as published by the Free Software
671;; Foundation; either version 1, or (at your option) any later version.
672;;
673;; CCE is distributed in the hope that it will be useful, but WITHOUT ANY
674;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
675;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
676;; details.
677;;
c063e381 678;; You should have received a copy of the GNU General Public License along with
fee0bd5f 679;; CCE. If not, see <http://www.gnu.org/licenses/>.")
a4c4011b 680
a4c4011b
AC
681 ("chinese-ctlau" "\e$AAuTA\e(B"
682 "CTLau.html" cn-gb-2312 "CTLau.el"
683 ctlau-gb-converter
684 "\
685;; \"CTLau.html\" is available at:
686;;
687;; http://umunhum.stanford.edu/~lee/chicomp/CTLau.html
688;;
689;; It contains the following copyright notice:
690;;
691;; # Copyright (C) 1988-2001 Fung Fung Lee (lee@umunhum.stanford.edu)
a1506d29 692;; #
a4c4011b
AC
693;; # This program is free software; you can redistribute it and/or
694;; # modify it under the terms of the GNU General Public License
695;; # as published by the Free Software Foundation; either version 2
696;; # of the License, or any later version.
a1506d29 697;; #
a4c4011b
AC
698;; # This program is distributed in the hope that it will be useful,
699;; # but WITHOUT ANY WARRANTY; without even the implied warranty of
700;; # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
701;; # GNU General Public License for more details.
a1506d29 702;; #
a4c4011b 703;; # You should have received a copy of the GNU General Public License
fee0bd5f 704;; # along with this program. If not, see <http://www.gnu.org/licenses/>.")
a4c4011b 705
e089be2d 706 ("chinese-ctlaub" "\e$(0N,Gn\e(B"
2bef0948 707 "CTLau-b5.html" big5 "CTLau-b5.el"
a4c4011b
AC
708 ctlau-b5-converter
709 "\
710;; \"CTLau-b5.html\" is available at:
711;;
712;; http://umunhum.stanford.edu/~lee/chicomp/CTLau-b5.html
713;;
714;; It contains the following copyright notice:
715;;
716;; # Copyright (C) 1988-2001 Fung Fung Lee (lee@umunhum.stanford.edu)
a1506d29 717;; #
a4c4011b
AC
718;; # This program is free software; you can redistribute it and/or
719;; # modify it under the terms of the GNU General Public License
720;; # as published by the Free Software Foundation; either version 2
721;; # of the License, or any later version.
a1506d29 722;; #
a4c4011b
AC
723;; # This program is distributed in the hope that it will be useful,
724;; # but WITHOUT ANY WARRANTY; without even the implied warranty of
725;; # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
726;; # GNU General Public License for more details.
a1506d29 727;; #
a4c4011b 728;; # You should have received a copy of the GNU General Public License
fee0bd5f 729;; # along with this program. If not, see <http://www.gnu.org/licenses/>.")
c063e381
KH
730 ))
731
732;; Generate a code of a Quail package in the current buffer from Tsang
733;; dictionary in the buffer DICBUF. The input method name of the
734;; Quail package is NAME, and the title string is TITLE.
735
0b381c7e 736;; TSANG-P is non-nil, generate \e$(06AQo\e(B input method. Otherwise
c063e381
KH
737;; generate \e$(0X|/y\e(B (simple version of \e$(06AQo\e(B). If BIG5-P is non-nil, the
738;; input method is for inputting Big5 characters. Otherwise the input
739;; method is for inputting CNS characters.
740
741(defun tsang-quick-converter (dicbuf name title tsang-p big5-p)
742 (let ((fulltitle (if tsang-p (if big5-p "\e$(06AQo\e(B" "\e$(GT?on\e(B")
743 (if big5-p "\e$(0X|/y\e(B" "\e$(Gv|Mx\e(B")))
744 dic)
745 (goto-char (point-max))
746 (if big5-p
747 (insert (format "\"\e$(0&d'GTT&,!J\e(B%s\e$(0!K\e(BBIG5
748
749 \e$(0KHM$\e(B%s\e$(0TT&,WoOu\e(B
750
751 [Q \e$(0'D\e(B] [W \e$(0(q\e(B] [E \e$(0'V\e(B] [R \e$(0&H\e(B] [T \e$(0'>\e(B] [Y \e$(0&4\e(B] [U \e$(0&U\e(B] [I \e$(0'B\e(B] [O \e$(0&*\e(B] [P \e$(0'A\e(B]
752
753 [A \e$(0'K\e(B] [S \e$(0&T\e(B] [D \e$(0'N\e(B] [F \e$(0'W\e(B] [G \e$(0&I\e(B] [H \e$(0*M\e(B] [J \e$(0&3\e(B] [L \e$(0&d\e(B]
a1506d29 754
c063e381
KH
755 [Z ] [X \e$(0[E\e(B] [C \e$(01[\e(B] [V \e$(0&M\e(B] [B \e$(0'M\e(B] [N \e$(0&_\e(B] [M \e$(0&"\e(B]
756
757\\\\<quail-translation-docstring>\"\n"
758 fulltitle fulltitle))
759 (insert (format "\"\e$(GDcEFrSD+!J\e(B%s\e$(G!K\e(BCNS
760
761 \e$(GiGk#\e(B%s\e$(GrSD+uomu\e(B
762
763 [Q \e$(GEC\e(B] [W \e$(GFp\e(B] [E \e$(GEU\e(B] [R \e$(GDG\e(B] [T \e$(GE=\e(B] [Y \e$(GD3\e(B] [U \e$(GDT\e(B] [I \e$(GEA\e(B] [O \e$(GD)\e(B] [P \e$(GE@\e(B]
764
765 [A \e$(GEJ\e(B] [S \e$(GDS\e(B] [D \e$(GEM\e(B] [F \e$(GEV\e(B] [G \e$(GDH\e(B] [H \e$(GHL\e(B] [J \e$(GD2\e(B] [L \e$(GDc\e(B]
a1506d29
JB
766
767 [Z ] [X \e$(GyE\e(B] [C \e$(GOZ\e(B] [V \e$(GDL\e(B] [B \e$(GEL\e(B] [N \e$(GD^\e(B] [M \e$(GD!\e(B]
c063e381
KH
768
769\\\\<quail-translation-docstring>\"\n"
770 fulltitle fulltitle)))
771 (insert " '((\".\" . quail-next-translation-block)
772 (\",\" . quail-prev-translation-block))
773 nil nil)\n\n")
774 (insert "(quail-define-rules\n")
9a529312 775 (with-current-buffer dicbuf
6b61353c
KH
776 ;; Handle double CR line ends, which result when checking out of
777 ;; CVS on MS-Windows.
778 (goto-char (point-min))
c063e381
KH
779 (search-forward "A440")
780 (beginning-of-line)
781 (let ((table (make-hash-table :test 'equal))
782 val)
783 (while (not (eobp))
784 (forward-char 5)
785 (let ((trans (char-to-string (following-char)))
786 key slot)
8b735b2b 787 (re-search-forward "\\([A-Z]+\\)\r*$" nil t)
c063e381
KH
788 (setq key (downcase
789 (if (or tsang-p
8b735b2b
KH
790 (<= (- (match-end 1) (match-beginning 1)) 1))
791 (match-string 1)
792 (string (char-after (match-beginning 1))
793 (char-after (1- (match-end 1)))))))
c063e381
KH
794 (setq val (gethash key table))
795 (if val (setq trans (concat val trans)))
796 (puthash key trans table)
797 (forward-line 1)))
798 (maphash #'(lambda (key val) (setq dic (cons (cons key val) dic)))
799 table)))
800 (setq dic (sort dic (function (lambda (x y) (string< (car x ) (car y))))))
801 (dolist (elt dic)
802 (insert (format "(%S\t%S)\n" (car elt) (cdr elt))))
e4920bc9
PE
803 (let ((punctuation '((";" "\e$(0!'!2!"!#!.!/\e(B" "\e$(G!'!2!"!#!.!/\e(B")
804 (":" "\e$(0!(!+!3!%!$!&!0!1\e(B" "\e$(G!(!+!3!%!$!&!0!1\e(B")
805 ("'" "\e$(0!e!d\e(B" "\e$(G!e!d\e(B")
806 ("\"" "\e$(0!g!f!h!i!q\e(B" "\e$(G!g!f!h!i!q\e(B")
807 ("\\" "\e$(0"`"b#M\e(B" "\e$(G"`"b#M\e(B")
808 ("|" "\e$(0!6!8!:"^\e(B" "\e$(G!6!8!:"^\e(B")
809 ("/" "\e$(0"_"a#L\e(B" "\e$(G"_"a#L\e(B")
810 ("?" "\e$(0!)!4\e(B" "\e$(G!)!4\e(B")
811 ("<" "\e$(0!R"6"A!T"H\e(B" "\e$(G!R"6"A!T"H\e(B")
812 (">" "\e$(0!S"7"B!U\e(B" "\e$(G!S"7"B!U\e(B")
813 ("[" "\e$(0!F!J!b!H!L!V!Z!X!\\e(B" "\e$(G!F!J!b!H!L!V!Z!X!\\e(B")
814 ("]" "\e$(0!G!K!c!I!M!W![!Y!]\e(B" "\e$(G!G!K!c!I!M!W![!Y!]\e(B")
815 ("{" "\e$(0!B!`!D\e(B " "\e$(G!B!`!D\e(B ")
816 ("}" "\e$(0!C!a!E\e(B" "\e$(G!C!a!E\e(B")
817 ("`" "\e$(0!j!k\e(B" "\e$(G!j!k\e(B")
818 ("~" "\e$(0"D"+",!<!=\e(B" "\e$(G"D"+",!<!=\e(B")
819 ("!" "\e$(0!*!5\e(B" "\e$(G!*!5\e(B")
820 ("@" "\e$(0"i"n\e(B" "\e$(G"i"n\e(B")
821 ("#" "\e$(0!l"-\e(B" "\e$(G!l"-\e(B")
822 ("$" "\e$(0"c"l\e(B" "\e$(G"c"l\e(B")
823 ("%" "\e$(0"h"m\e(B" "\e$(G"h"m\e(B")
824 ("&" "\e$(0!m".\e(B" "\e$(G!m".\e(B")
825 ("*" "\e$(0!n"/!o!w!x\e(B" "\e$(G!n"/!o!w!x\e(B")
826 ("(" "\e$(0!>!^!@\e(B" "\e$(G!>!^!@\e(B")
827 (")" "\e$(0!?!_!A\e(B" "\e$(G!?!_!A\e(B")
828 ("-" "\e$(0!7!9"#"$"1"@\e(B" "\e$(G!7!9"#"$"1"@\e(B")
829 ("_" "\e$(0"%"&\e(B" "\e$(G"%"&\e(B")
830 ("=" "\e$(0"8"C\e(B" "\e$(G"8"C\e(B")
831 ("+" "\e$(0"0"?\e(B" "\e$(G"0"?\e(B"))))
832 (dolist (elt punctuation)
c063e381
KH
833 (insert (format "(%S %S)\n" (concat "z" (car elt))
834 (if big5-p (nth 1 elt) (nth 2 elt))))))
835 (insert ")\n")))
836
837(defun tsang-b5-converter (dicbuf name title)
838 (tsang-quick-converter dicbuf name title t t))
839
840(defun quick-b5-converter (dicbuf name title)
841 (tsang-quick-converter dicbuf name title nil t))
842
843(defun tsang-cns-converter (dicbuf name title)
844 (tsang-quick-converter dicbuf name title t nil))
845
846(defun quick-cns-converter (dicbuf name title)
847 (tsang-quick-converter dicbuf name title nil nil))
848
849;; Generate a code of a Quail package in the current buffer from
850;; Pinyin dictionary in the buffer DICBUF. The input method name of
851;; the Quail package is NAME, and the title string is TITLE.
852
853(defun py-converter (dicbuf name title)
854 (goto-char (point-max))
855 (insert (format "%S\n" "\e$A::WVJdHk!KF4Rt!K\e(B
856
857 \e$AF4Rt7=08\e(B
858
859 \e$AP!P4S"NDWVD84z1m!8F4Rt!97{:E#,\e(B \"u(yu) \e$ATrSC\e(B u: \e$A1mJ>!C\e(B
860
861Pinyin base input method for Chinese charset GB2312 (`chinese-gb2312').
862
c3ff164a 863Pinyin is the standard roman transliteration method for Chinese.
c063e381
KH
864Pinyin uses a sequence of Latin alphabetic characters for each Chinese
865character. The sequence is made by the combination of the initials
866\(the beginning sounds) and finals (the ending sounds).
867
868 initials: b p m f d t n l z c s zh ch sh r j q x g k h
869 finals: a o e i er ai ei oa ou an en ang eng ong i ia iao ie iu ian in
870 iang ing iong u ua uo uai ui uan un uan ueng yu yue yuan yun
871
872 (Note: In the correct Pinyin writing, the sequence \"yu\" in the last
873 four finals should be written by the character u-umlaut `\e$A(9\e(B'.)
874
875With this input method, you enter a Chinese character by first
876entering its pinyin spelling.
877
878\\<quail-translation-docstring>
879
880For instance, to input \e$ADc\e(B, you type \"n i C-n 3\". The first \"n i\"
881is a Pinyin, \"C-n\" selects the next group of candidates (each group
882contains at most 10 characters), \"3\" select the third character in
883that group.
884
885This input method supports only Han characters. The related input
886method `chinese-py-punct' is the combination of this method and
887`chinese-punct'; it supports both Han characters and punctuation
888characters.
889
890For double-width GB2312 characters corresponding to ASCII, use the
891input method `chinese-qj'.
892
893The correct Pinyin system specifies tones by diacritical marks, but
894this input method doesn't use them, which results in easy (you don't
895have to know the exact tones), but verbose (many characters are assigned
896to the same key sequence) input. You may also want to try the input
897method `chinese-tonepy' with which you must specify tones by digits
898\(1..5)."))
899 (insert " '((\"\C-?\" . quail-delete-last-char)
900 (\".\" . quail-next-translation)
901 (\">\" . quail-next-translation)
902 (\",\" . quail-prev-translation)
903 (\"<\" . quail-prev-translation))
904 nil nil nil nil)\n\n")
905 (insert "(quail-define-rules\n")
906 (let ((pos (point)))
8b735b2b 907 (insert-buffer-substring-no-properties dicbuf)
c063e381 908 (goto-char pos)
8c8d1081 909 (re-search-forward "^[a-z]")
7d15a839
KH
910 (beginning-of-line)
911 (delete-region pos (point))
c063e381
KH
912 (while (not (eobp))
913 (insert "(\"")
914 (skip-chars-forward "a-z")
915 (insert "\" \"")
916 (delete-char 1)
917 (end-of-line)
8b735b2b
KH
918 (while (= (preceding-char) ?\r)
919 (delete-char -1))
c063e381
KH
920 (insert "\")")
921 (forward-line 1)))
922 (insert ")\n"))
923
924;; Generate a code of a Quail package in the current buffer from
925;; Ziranma dictionary in the buffer DICBUF. The input method name of
926;; the Quail package is NAME, and the title string is TITLE.
927
928(defun ziranma-converter (dicbuf name title)
929 (let (dic)
9a529312 930 (with-current-buffer dicbuf
c063e381 931 (goto-char (point-min))
8b735b2b
KH
932 (search-forward "\n%keyname end")
933 (forward-line 1)
c063e381
KH
934 (let ((table (make-hash-table :test 'equal))
935 elt pos key trans val)
936 (while (not (eobp))
937 (setq pos (point))
938 (skip-chars-forward "^ \t")
8b735b2b 939 (setq key (buffer-substring-no-properties pos (point)))
c063e381 940 (skip-chars-forward " \t")
8b735b2b
KH
941 (setq pos (point))
942 (skip-chars-forward "^\r\n")
943 (setq trans (vector (buffer-substring-no-properties pos (point))))
c063e381
KH
944 (setq val (gethash key table))
945 (if val (setq trans (vconcat val trans)))
946 (puthash key trans table)
947 (forward-line 1))
948 (maphash #'(lambda (key trans)
949 (let ((len (length trans))
950 i)
951 (if (and (= len 1) (= (length (aref trans 0)) 1))
952 (setq trans (aref trans 0))
953 (setq i 0)
954 (while (and (< i len)
955 (= (length (aref trans i)) 1))
956 (setq i (1+ i)))
957 (if (= i len)
958 (setq trans (mapconcat 'identity trans "")))))
959 (setq dic (cons (cons key trans) dic)))
960 table)))
961 (setq dic (sort dic (function (lambda (x y) (string< (car x) (car y))))))
962 (goto-char (point-max))
963 (insert (format "%S\n" "\e$A::WVJdHk!K!>WTH;!?!K\e(B
964
965 \e$A<|EL6TUU1m\e(B:
966 \e$A)3)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)7\e(B
967 \e$A)'#Q\e(B \e$A)'#W\e(B \e$A)'#E\e(B \e$A)'#R\e(B \e$A)'#T\e(B \e$A)'#Y\e(B \e$A)'#U\e(Bsh\e$A)'#I\e(Bch\e$A)'#O\e(B \e$A)'#P\e(B \e$A)'\e(B
968 \e$A)'\e(B iu\e$A)'\e(B ua\e$A)'\e(B e\e$A)'\e(B uan\e$A)'\e(B ue\e$A)'\e(B uai\e$A)'\e(B u\e$A)'\e(B i\e$A)'\e(B o\e$A)'\e(B un\e$A)'\e(B
969 \e$A)'\e(B \e$A)'\e(B ia\e$A)'\e(B \e$A)'\e(B van\e$A)'\e(B ve\e$A)'\e(B ing\e$A)'\e(B \e$A)'\e(B \e$A)'\e(B uo\e$A)'\e(B vn\e$A)'\e(B
970 \e$A);)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)?\e(B
971 \e$A)'#A\e(B \e$A)'#S\e(B \e$A)'#D\e(B \e$A)'#F\e(B \e$A)'#G\e(B \e$A)'#H\e(B \e$A)'#J\e(B \e$A)'#K\e(B \e$A)'#L\e(B \e$A)'\e(B
972 \e$A)'\e(B a\e$A)'\e(Biong\e$A)'\e(Buang\e$A)'\e(B en\e$A)'\e(B eng\e$A)'\e(B ang\e$A)'\e(B an\e$A)'\e(B ao\e$A)'\e(B ai\e$A)'\e(B
973 \e$A)'\e(B \e$A)'\e(B ong\e$A)'\e(Biang\e$A)'\e(B \e$A)'\e(B ng\e$A)'\e(B \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B
974 \e$A);)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)%)7\e(B
975 \e$A)'#Z\e(B \e$A)'#X\e(B \e$A)'#C\e(B \e$A)'#V\e(Bzh\e$A)'#B\e(B \e$A)'#N\e(B \e$A)'#M\e(B \e$A)'#,\e(B \e$A)'#.\e(B \e$A)'\e(B \e$A#/\e(B \e$A)'\e(B
976 \e$A)'\e(B ei\e$A)'\e(B ie\e$A)'\e(B iao\e$A)'\e(B ui\e$A)'\e(B ou\e$A)'\e(B in\e$A)'\e(B ian\e$A)'G0R3)':sR3)'7{:E)'\e(B
977 \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B v\e$A)'\e(B \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B \e$A)'\e(B
978 \e$A);)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)?\e(B
979
980
981Pinyin base input method for Chinese GB2312 characters (`chinese-gb2312').
982
983Pinyin is the standard roman transliteration method for Chinese.
984For the details of Pinyin system, see the documentation of the input
985method `chinese-py'.
986
987Unlike the standard spelling of Pinyin, in this input method all
988initials and finals are assigned to single keys (see the above table).
989For instance, the initial \"ch\" is assigned to the key `i', the final
990\"iu\" is assigned to the key `q', and tones 1, 2, 3, 4, and \e$AGaIy\e(B are
991assigned to the keys `q', `w', `e', `r', `t' respectively.
992
993\\<quail-translation-docstring>
994
995To input one-letter words, you type 4 keys, the first two for the
996Pinyin of the letter, next one for tone, and the last one is always a
997quote ('). For instance, \"vsq'\" input \e$AVP\e(B. Exceptions are these
998letters. You can input them just by typing a single key.
999
1000 Character: \e$A04\e(B \e$A2;\e(B \e$A4N\e(B \e$A5D\e(B \e$A6~\e(B \e$A7"\e(B \e$A8v\e(B \e$A:M\e(B \e$A3v\e(B \e$A<0\e(B \e$A?I\e(B \e$AAK\e(B \e$AC;\e(B
1001 Key: a b c d e f g h i j k l m
1002 Character: \e$ADc\e(B \e$AE7\e(B \e$AF,\e(B \e$AF_\e(B \e$AHK\e(B \e$AH}\e(B \e$AK{\e(B \e$AJG\e(B \e$AWE\e(B \e$ANR\e(B \e$AP!\e(B \e$AR;\e(B \e$ATZ\e(B
1003 Key: n o p q r s t u v w x y z
1004
1005To input two-letter words, you have two ways. One way is to type 4
1006keys, two for the first Pinyin, two for the second Pinyin. For
1007instance, \"vsgo\" inputs \e$AVP9z\e(B. Another way is to type 3 keys: 2
1008initials of two letters, and quote ('). For instance, \"vg'\" also
1009inputs \e$AVP9z\e(B.
1010
1011To input three-letter words, you type 4 keys: initials of three
1012letters, and the last is quote ('). For instance, \"bjy'2\" inputs \e$A11\e(B
1013\e$A>)Q<\e(B (the last `2' is to select one of the candidates).
1014
1015To input words of more than three letters, you type 4 keys, initials
1016of the first three letters and the last letter. For instance,
1017\"bjdt\" inputs \e$A11>)5gJSL(\e(B.
1018
e4920bc9 1019To input symbols and punctuation, type `/' followed by one of `a' to
c063e381
KH
1020`z', then select one of the candidates."))
1021 (insert " '((\"\C-?\" . quail-delete-last-char)
1022 (\".\" . quail-next-translation)
1023 (\"[\" . quail-next-translation)
1024 (\",\" . quail-prev-translation)
1025 (\"]\" . quail-prev-translation))
1026 nil nil nil nil)\n\n")
1027 (insert "(quail-define-rules\n")
1028 (dolist (elt dic)
1029 (insert (format "(%S %S)\n" (car elt) (cdr elt))))
1030 (insert ")\n")))
1031
a4c4011b
AC
1032;; Generate the code for a Quail package in the current buffer from a
1033;; CTLau or CTLau-b5 dictionary in the buffer DICBUF. The input
1034;; method name of the Quail package is NAME, and the title string is
1035;; TITLE. DESCRIPTION is the string shown by describe-input-method.
1036
1037(defun ctlau-converter (dicbuf name title description)
1038 (goto-char (point-max))
1039 (insert (format "%S\n" description))
1040 (insert " '((\"\C-?\" . quail-delete-last-char)
1041 (\".\" . quail-next-translation)
1042 (\">\" . quail-next-translation)
1043 (\",\" . quail-prev-translation)
1044 (\"<\" . quail-prev-translation))
1045 nil nil nil nil)\n\n")
1046 (insert "(quail-define-rules\n")
1047 (let (dicbuf-start dicbuf-end key-start key (pos (point)))
1048 ;; Find the dictionary, which starts below a horizontal rule and
1049 ;; ends at the second to last line in the HTML file.
9a529312 1050 (with-current-buffer dicbuf
a4c4011b 1051 (goto-char (point-min))
8b735b2b
KH
1052 (re-search-forward "^#<hr>")
1053 (forward-line 1)
a4c4011b
AC
1054 (setq dicbuf-start (point))
1055 (goto-char (point-max))
8b735b2b 1056 (re-search-backward "^<hr>")
a4c4011b 1057 (setq dicbuf-end (point)))
8b735b2b 1058 (insert-buffer-substring-no-properties dicbuf dicbuf-start dicbuf-end)
a4c4011b
AC
1059 ;; CTLau-b5.html contains characters (0xa1 0xbc) which show up as
1060 ;; hollow boxes when the original characters in CTLau.html from
1061 ;; which the file is converted have no Big5 equivalent. Go
1062 ;; through and delete them.
1063 (goto-char pos)
1064 (while (search-forward "\e$(0!{\e(B" nil t)
1065 (delete-char -1))
1066 ;; Uppercase keys in dictionary need to be downcased. Backslashes
1067 ;; at the beginning of keys need to be turned into double
1068 ;; backslashes.
1069 (goto-char pos)
1070 (while (not (eobp))
1071 (insert "(\"")
1072 (if (char-equal (following-char) ?\\)
1073 (insert "\\"))
1074 (setq key-start (point))
1075 (skip-chars-forward "\\\\A-Z")
1076 (downcase-region key-start (point))
1077 (insert "\" \"")
1078 (delete-char 1)
1079 (end-of-line)
8b735b2b
KH
1080 (while (= (preceding-char) ?\r)
1081 (delete-char -1))
a4c4011b
AC
1082 (insert "\")")
1083 (forward-line 1)))
1084 (insert ")\n"))
1085
1086(defun ctlau-gb-converter (dicbuf name title)
1087 (ctlau-converter dicbuf name title
1088"\e$A::WVJdHk!KAuN}OiJ=TARt!K\e(B
1089
1090 \e$AAuN}OiJ=TASoW"Rt7=08\e(B
1091 Sidney Lau's Cantonese transcription scheme as described in his book
1092 \"Elementary Cantonese\", The Government Printer, Hong Kong, 1972.
1093 This file was prepared by Fung Fung Lee (\e$A@n7c7e\e(B).
1094 Originally converted from CTCPS3.tit
1095 Last modified: June 2, 1993.
1096
4e7e1f03 1097 Some infrequent GB characters are accessed by typing \\, followed by
a4c4011b
AC
1098 the Cantonese romanization of the respective radical (\e$A2?JW\e(B)."))
1099
1100(defun ctlau-b5-converter (dicbuf name title)
1101 (ctlau-converter dicbuf name title
1102"\e$(0KH)tTT&,!(N,Tg>A*#Gn5x!(\e(B
1103
1104 \e$(0N,Tg>A*#GnM$0D5x'J7{\e(B
1105 Sidney Lau's Cantonese transcription scheme as described in his book
1106 \"Elementary Cantonese\", The Government Printer, Hong Kong, 1972.
1107 This file was prepared by Fung Fung Lee (\e$(0,XFS76\e(B).
1108 Originally converted from CTCPS3.tit
1109 Last modified: June 2, 1993.
1110
4e7e1f03 1111 Some infrequent characters are accessed by typing \\, followed by
a4c4011b
AC
1112 the Cantonese romanization of the respective radical (\e$(0?f5}\e(B)."))
1113
5cec3056 1114(declare-function dos-8+3-filename "dos-fns.el" (filename))
73e6adaa 1115
c063e381 1116(defun miscdic-convert (filename &optional dirname)
a1506d29 1117 "Convert a dictionary file FILENAME into a Quail package.
c063e381
KH
1118Optional argument DIRNAME if specified is the directory name under which
1119the generated Quail package is saved."
1120 (interactive "FInput method dictionary file: ")
1121 (or (file-readable-p filename)
1122 (error "%s does not exist" filename))
1123 (let ((tail quail-misc-package-ext-info)
98223b73 1124 coding-system-for-write
c063e381
KH
1125 slot
1126 name title dicfile coding quailfile converter copyright
1127 dicbuf)
1128 (while tail
d1df889e
KH
1129 (setq slot (car tail)
1130 dicfile (nth 2 slot)
1131 quailfile (nth 4 slot))
1132 (when (and (or (string-match dicfile filename)
1133 ;; MS-DOS filesystem truncates file names to 8+3
1134 ;; limits, so "cangjie-table.cns" becomes
1135 ;; "cangjie-.cns", and the above string-match
1136 ;; fails. Give DOS users a chance...
1137 (and (fboundp 'msdos-long-file-names)
1138 (not (msdos-long-file-names))
1139 (string-match (dos-8+3-filename dicfile) filename)))
1140 (if (file-newer-than-file-p
1141 filename (expand-file-name quailfile dirname))
1142 t
1143 (message "%s is up to date" quailfile)
1144 nil))
1145 (setq name (car slot)
c063e381 1146 title (nth 1 slot)
c063e381 1147 coding (nth 3 slot)
c063e381
KH
1148 converter (nth 5 slot)
1149 copyright (nth 6 slot))
1150 (message "Converting %s to %s..." dicfile quailfile)
f9362982
KH
1151 ;; Explicitly set eol format to `unix'.
1152 (setq coding-system-for-write
1153 (coding-system-change-eol-conversion coding 'unix))
c063e381 1154 (with-temp-file (expand-file-name quailfile dirname)
cb6c95a3 1155 (insert (format ";; Quail package `%s'\n" name))
c063e381 1156 (insert ";; Generated by the command `miscdic-convert'\n")
c063e381
KH
1157 (insert ";; Source dictionary file: " dicfile "\n")
1158 (insert ";; Copyright notice of the source file\n")
1159 (insert ";;------------------------------------------------------\n")
1160 (insert copyright "\n")
1161 (insert ";;------------------------------------------------------\n")
1162 (insert "\n")
1163 (insert ";;; Code:\n\n")
1164 (insert "(require 'quail)\n")
1165 (insert "(quail-define-package \"" name "\" \""
98223b73
KH
1166 (if (eq coding 'big5) "Chinese-BIG5"
1167 (if (eq coding 'iso-2022-cn-ext) "Chinese-CNS"
1168 "Chinese-GB"))
c063e381 1169 "\" \"" title "\" t\n")
8b735b2b
KH
1170 (let* ((coding-system-for-read
1171 (coding-system-change-eol-conversion coding 'unix))
c063e381
KH
1172 (dicbuf (find-file-noselect filename)))
1173 (funcall converter dicbuf name title)
cb6c95a3
GM
1174 (kill-buffer dicbuf))
1175 (insert ";; Local Variables:\n"
1176 ";; version-control: never\n"
1177 ";; no-update-autoloads: t\n"
1178 (format ";; coding: %s\n" coding)
1179 ";; End:\n\n"
1180 ";;; " quailfile " ends here\n"))
c063e381
KH
1181 (message "Converting %s to %s...done" dicfile quailfile))
1182 (setq tail (cdr tail)))))
1183
1184(defun batch-miscdic-convert ()
40ba43b4 1185 "Run `miscdic-convert' on the files remaining on the command line.
c063e381
KH
1186Use this from the command line, with `-batch';
1187it won't work in an interactive Emacs.
1188If there's an argument \"-dir\", the next argument specifies a directory
1189to store generated Quail packages."
1190 (defvar command-line-args-left) ; Avoid compiler warning.
1191 (if (not noninteractive)
1192 (error "`batch-miscdic-convert' should be used only with -batch"))
1193 (let ((dir default-directory)
1194 filename)
1195 (while command-line-args-left
1196 (if (string= (car command-line-args-left) "-dir")
1197 (progn
1198 (setq command-line-args-left (cdr command-line-args-left))
0631f894
KH
1199 (setq dir (car command-line-args-left))
1200 (setq command-line-args-left (cdr command-line-args-left))))
c063e381
KH
1201 (setq filename (car command-line-args-left)
1202 command-line-args-left (cdr command-line-args-left))
1203 (if (file-directory-p filename)
1204 (dolist (file (directory-files filename t nil t))
98223b73
KH
1205 (or (file-directory-p file)
1206 (miscdic-convert file dir)))
c063e381
KH
1207 (miscdic-convert filename dir))))
1208 (kill-emacs 0))
1209
a64133bd
GM
1210;; Prevent "Local Variables" above confusing Emacs.
1211\f
1212
60370d40 1213;;; titdic-cnv.el ends here