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