(titdic-convert): Force files be
[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
4ed46869
KH
9
10;; Keywords: Quail, TIT, cxterm
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation; either version 2, or (at your option)
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
369314dc 25;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
26;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27;; Boston, MA 02110-1301, USA.
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
106For double-width GB2312 characters correponding to ASCII, use the
107input method `chinese-qj'.")
108
4558e816
KH
109 ("chinese-ecdict" "\e$(05CKH\e(B"
110"In this input method, you enter a Chinese (Big5) charactere or word
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
125SPC, 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,
1264:\e$(0(+Vy\e(B).
127
128\\<quail-translation-docstring>")
6b1e079c
KH
129
130 ("chinese-punct-b5" "\e$(0O:\e(BB"
131 "Input method for Chinese punctuations and symbols of Big5
132\(`chinese-big5-1' and `chinese-big5-2').")
133
134 ("chinese-punct" "\e$A1j\e(BG"
135 "Input method for Chinese punctuations and symbols of GB2312
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
KH
193
194For double-width GB2312 characters correponding to ASCII, use the
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
206SPC, 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,
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 ()
86e7801e 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 `")
7d15a839
KH
274 (princ package)
275 (princ "' -*- coding:iso-2022-7bit; ")
276 (princ "byte-compile-disable-print-circle:t; -*-\n")
c7211fed 277 (princ ";; Generated by the command `titdic-convert'\n;;\tDate: ")
1375754c
KH
278 (princ (current-time-string))
279 (princ "\n;;\tOriginal TIT dictionary file: ")
280 (princ (file-name-nondirectory filename))
281 (princ "\n\n;;; Comment:\n\n")
86e4f7c0 282 (princ ";; Byte-compile this file again after any modification.\n\n")
1375754c
KH
283 (princ ";;; Start of the header of original TIT dictionary.\n\n")
284
4ed46869 285 (while (not (eobp))
1375754c
KH
286 (let ((ch (following-char))
287 (pos (point)))
4ed46869
KH
288 (cond ((= ch ?C) ; COMMENT
289 (cond ((looking-at "COMMENT")
290 (let ((pos (match-end 0)))
291 (end-of-line)
4ed46869
KH
292 (setq tit-comments (cons (buffer-substring pos (point))
293 tit-comments))))))
294 ((= ch ?M) ; MULTICHOICE, MOVERIGHT, MOVELEFT
295 (cond ((looking-at "MULTICHOICE:[ \t]*")
296 (goto-char (match-end 0))
297 (setq tit-multichoice (looking-at "YES")))
298 ((looking-at "MOVERIGHT:[ \t]*")
299 (goto-char (match-end 0))
300 (setq tit-moveright (tit-read-key-value)))
301 ((looking-at "MOVELEFT:[ \t]*")
302 (goto-char (match-end 0))
303 (setq tit-moveleft (tit-read-key-value)))))
304 ((= ch ?P) ; PROMPT
305 (cond ((looking-at "PROMPT:[ \t]*")
306 (goto-char (match-end 0))
6b1e079c
KH
307 (setq tit-prompt (tit-read-key-value))
308 ;; Some TIT dictionaies that are encoded by
309 ;; euc-china contains invalid character at the tail.
310 (let* ((last (aref tit-prompt (1- (length tit-prompt))))
311 (split (split-char last)))
312 (if (or (eq (nth 1 split) 32)
313 (eq (nth 2 split) 32))
314 (setq tit-prompt (substring tit-prompt 0 -1)))))))
4ed46869
KH
315 ((= ch ?B) ; BACKSPACE, BEGINDICTIONARY,
316 ; BEGINPHRASE
317 (cond ((looking-at "BACKSPACE:[ \t]*")
318 (goto-char (match-end 0))
319 (setq tit-backspace (tit-read-key-value)))
320 ((looking-at "BEGINDICTIONARY")
1375754c 321 (setq tit-dictionary t))
4ed46869 322 ((looking-at "BEGINPHRASE")
1375754c 323 (setq tit-dictionary nil))))
4ed46869
KH
324 ((= ch ?K) ; KEYPROMPT
325 (cond ((looking-at "KEYPROMPT(\\(.*\\)):[ \t]*")
326 (let ((key-char (match-string 1)))
327 (goto-char (match-end 0))
1fa1e1f5
RS
328 (if (string-match "\\\\[0-9]+" key-char)
329 (setq key-char
330 (car (read-from-string (format "\"%s\""
331 key-char)))))
4ed46869
KH
332 (setq tit-keyprompt
333 (cons (cons key-char (tit-read-key-value))
1375754c
KH
334 tit-keyprompt)))))))
335 (end-of-line)
336 (princ ";; ")
337 (princ (buffer-substring pos (point)))
338 (princ "\n")
339 (forward-line 1)))
a1506d29 340
1375754c
KH
341 (princ "\n;;; End of the header of original TIT dictionary.\n\n")
342 (princ ";;; Code:\n\n(require 'quail)\n\n")
343
344 (princ "(quail-define-package ")
345 ;; Args NAME, LANGUAGE, TITLE
6b1e079c 346 (let ((title (nth 1 (assoc package quail-cxterm-package-ext-info))))
1375754c
KH
347 (princ "\"")
348 (princ package)
349 (princ "\" \"")
350 (princ (nth 2 (assoc tit-encode tit-encode-list)))
351 (princ "\" \"")
352 (princ (or title
353 (if (string-match "[:\e$A!K\e$(0!(!J\e(B]+\\([^:\e$A!K\e$(0!(!K\e(B]+\\)" tit-prompt)
354 (substring tit-prompt (match-beginning 1) (match-end 1))
355 tit-prompt)))
356 (princ "\"\n"))
4ed46869
KH
357
358 ;; Arg GUIDANCE
359 (if tit-keyprompt
360 (progn
1375754c 361 (princ " '(")
4ed46869 362 (while tit-keyprompt
1375754c
KH
363 (princ " ")
364 (princ (format "(%d . \"%s\")\n"
365 (string-to-char (car (car tit-keyprompt)))
366 (cdr (car tit-keyprompt))))
4ed46869 367 (setq tit-keyprompt (cdr tit-keyprompt)))
1375754c
KH
368 (princ ")"))
369 (princ " t\n"))
4ed46869
KH
370
371 ;; Arg DOCSTRING
6b1e079c
KH
372 (let ((doc (concat tit-prompt "\n"))
373 (comments (if tit-comments
374 (mapconcat 'identity (nreverse tit-comments) "\n")))
375 (doc-ext (nth 2 (assoc package quail-cxterm-package-ext-info))))
376 (if comments
377 (setq doc (concat doc "\n" comments "\n")))
378 (if doc-ext
379 (setq doc (concat doc "\n" doc-ext "\n")))
380 (prin1 doc)
381 (terpri))
4ed46869
KH
382
383 ;; Arg KEY-BINDINGS
1375754c 384 (princ " '(")
4ed46869 385 (tit-generate-key-bindings tit-backspace 'quail-delete-last-char)
1375754c 386 (princ "\n ")
4ed46869 387 (tit-generate-key-bindings tit-deleteall 'quail-abort-translation)
1375754c 388 (princ "\n ")
4ed46869 389 (tit-generate-key-bindings tit-moveright 'quail-next-translation)
1375754c 390 (princ "\n ")
4ed46869 391 (tit-generate-key-bindings tit-moveleft 'quail-prev-translation)
1375754c 392 (princ ")\n")
4ed46869
KH
393
394 ;; Args FORGET-TRANSLATION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT.
395 ;; The remaining args are all nil.
1375754c
KH
396 (princ " nil")
397 (princ (if tit-multichoice " nil" " t"))
398 (princ (if tit-keyprompt " t t)\n\n" " nil nil)\n\n"))))
399
400(defsubst tit-flush-translations (key translations)
401 (if (string-match "\\\\[0-9][0-9][0-9]" key)
402 (let ((newkey (concat (substring key 0 (match-beginning 0))
403 (car (read-from-string
404 (concat "\"" (match-string 0 key) "\"")))))
405 (idx (match-end 0)))
406 (while (string-match "\\\\[0-9][0-9][0-9]" key idx)
407 (setq newkey (concat
408 newkey
409 (substring key idx (match-beginning 0))
410 (car (read-from-string
411 (concat "\"" (match-string 0 key) "\"")))))
412 (setq idx (match-end 0)))
413 (setq key (concat newkey (substring key idx)))))
414 (prin1 (list key (if tit-dictionary translations
415 (vconcat (nreverse translations)))))
416 (princ "\n"))
4ed46869
KH
417
418;; Convert body part of TIT dictionary into `quail-define-rules'
419;; function call.
420(defun tit-process-body ()
421 (message "Formatting translation rules...")
1375754c
KH
422 (let* ((template (list nil nil))
423 (second (cdr template))
424 (prev-key "")
425 ch key translations pos)
426 (princ "(quail-define-rules\n")
4ed46869 427 (while (null (eobp))
1375754c
KH
428 (setq ch (following-char))
429 (if (or (= ch ?#) (= ch ?\n))
430 (forward-line 1)
4ed46869 431 (setq pos (point))
1375754c
KH
432 (skip-chars-forward "^ \t\n")
433 (setq key (buffer-substring pos (point)))
4ed46869 434 (skip-chars-forward " \t")
1375754c
KH
435 (setq ch (following-char))
436 (if (or (= ch ?#) (= ch ?\n))
08a1bf22 437 ;; This entry contains no translations. Let's ignore it.
1375754c
KH
438 (forward-line 1)
439 (or (string= key prev-key)
08a1bf22 440 (progn
1375754c
KH
441 (if translations
442 (tit-flush-translations prev-key translations))
443 (setq translations nil
444 prev-key key)))
445 (if tit-dictionary
446 (progn
447 (setq pos (point))
448 (skip-chars-forward "^ \t#\n")
449 (setq translations
450 (if translations
451 (concat translations
452 (buffer-substring pos (point)))
453 (buffer-substring pos (point)))))
454 (while (not (eolp))
455 (setq pos (point))
456 (skip-chars-forward "^ \t\n")
457 (setq translations (cons (buffer-substring pos (point))
458 translations))
459 (skip-chars-forward " \t")
460 (setq ch (following-char))
461 (if (= ch ?#) (end-of-line))))
08a1bf22 462 (forward-line 1))))
1375754c
KH
463
464 (if translations
465 (tit-flush-translations prev-key translations))
466 (princ ")\n")))
4ed46869
KH
467
468;;;###autoload
469(defun titdic-convert (filename &optional dirname)
470 "Convert a TIT dictionary of FILENAME into a Quail package.
471Optional argument DIRNAME if specified is the directory name under which
472the generated Quail package is saved."
473 (interactive "FTIT dictionary file: ")
1a03f515 474 (let ((coding-system-for-write 'iso-2022-7bit-unix))
c7211fed 475 (with-temp-file (tit-make-quail-package-file-name filename dirname)
1a03f515
KH
476 ;; Explicitly speficy eol format to `unix'.
477 (set-buffer-file-coding-system 'iso-2022-7bit-unix)
c7211fed
DL
478 (let ((standard-output (current-buffer)))
479 (with-temp-buffer
480 (set-buffer-multibyte nil)
1a03f515
KH
481 ;; Here we must use `raw-text' instead of `no-conversion' to
482 ;; enable auto-decoding of eol format (CRLF->LF).
483 (let ((coding-system-for-read 'raw-text))
c7211fed 484 (insert-file-contents (expand-file-name filename)))
a1506d29 485
c7211fed
DL
486 ;; Decode the buffer contents from the encoding specified by a
487 ;; value of the key "ENCODE:".
488 (if (not (search-forward "\nBEGIN" nil t))
489 (error "TIT dictionary doesn't have body part"))
490 (let ((limit (point))
491 coding-system slot)
492 (goto-char (point-min))
493 (if (re-search-forward "^ENCODE:[ \t]*" limit t)
494 (progn
495 (goto-char (match-end 0))
496 (setq tit-encode (tit-read-key-value)))
497 (setq tit-encode tit-default-encode))
498 (setq slot (assoc tit-encode tit-encode-list))
499 (if (not slot)
500 (error "Invalid ENCODE: value in TIT dictionary"))
501 (setq coding-system (nth 1 slot))
502 (message "Decoding with coding system %s..." coding-system)
503 (goto-char (point-min))
504 (decode-coding-region (point-min) (point-max) coding-system))
505
506 ;; Set point the starting position of the body part.
1375754c 507 (goto-char (point-min))
c7211fed
DL
508 (if (not (search-forward "\nBEGIN" nil t))
509 (error "TIT dictionary can't be decoded correctly"))
510
c3ff164a
KH
511 ;; Process the header part in multibyte mode.
512 (with-current-buffer standard-output
513 (set-buffer-multibyte t))
514 (set-buffer-multibyte t)
c7211fed
DL
515 (forward-line 1)
516 (narrow-to-region (point-min) (point))
517 (tit-process-header filename)
518 (widen)
519
520 ;; Process the body part. For speed, we turn off multibyte facility.
521 (with-current-buffer standard-output
522 (set-buffer-multibyte nil))
523 (set-buffer-multibyte nil)
524 (tit-process-body))))))
4ed46869
KH
525
526;;;###autoload
44cbfae9 527(defun batch-titdic-convert (&optional force)
4ed46869
KH
528 "Run `titdic-convert' on the files remaining on the command line.
529Use this from the command line, with `-batch';
530it won't work in an interactive Emacs.
531For example, invoke \"emacs -batch -f batch-titdic-convert XXX.tit\" to
532 generate Quail package file \"xxx.el\" from TIT dictionary file \"XXX.tit\".
533To get complete usage, invoke \"emacs -batch -f batch-titdic-convert -h\"."
534 (defvar command-line-args-left) ; Avoid compiler warning.
535 (if (not noninteractive)
536 (error "`batch-titdic-convert' should be used only with -batch"))
537 (if (string= (car command-line-args-left) "-h")
538 (progn
539 (message "To convert XXX.tit and YYY.tit into xxx.el and yyy.el:")
540 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert XXX.tit YYY.tit")
541 (message "To convert XXX.tit into DIR/xxx.el:")
542 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert -dir DIR XXX.tit"))
543 (let (targetdir filename files file)
544 (if (string= (car command-line-args-left) "-dir")
545 (progn
546 (setq command-line-args-left (cdr command-line-args-left))
547 (setq targetdir (car command-line-args-left))
548 (setq command-line-args-left (cdr command-line-args-left))))
549 (while command-line-args-left
550 (setq filename (expand-file-name (car command-line-args-left)))
551 (if (file-directory-p filename)
552 (progn
553 (message "Converting all tit files in the directory %s" filename)
554 (setq files (directory-files filename t "\\.tit$")))
555 (setq files (list filename)))
556 (while files
557 (setq file (expand-file-name (car files)))
44cbfae9
KH
558 (when (or force
559 (file-newer-than-file-p
560 file (tit-make-quail-package-file-name file targetdir)))
561 (message "Converting %s to quail-package..." file)
562 (titdic-convert file targetdir))
4ed46869
KH
563 (setq files (cdr files)))
564 (setq command-line-args-left (cdr command-line-args-left)))
86e4f7c0 565 (message "Byte-compile the created files by:")
4ed46869
KH
566 (message " %% emacs -batch -f batch-byte-compile XXX.el")))
567 (kill-emacs 0))
568
c063e381
KH
569\f
570;;; Converter of miscellaneous dictionaries other than TIT format.
571
572;; Alist of input method names and the corresponding information.
573;; Each element has this form:
574;; (INPUT-METHOD-NAME ;; Name of the input method.
a1506d29 575;; INPUT-METHOD-TITLE ;; Title string of the input method
c063e381
KH
576;; DICFILE ;; Name of the source dictionary file.
577;; CODING ;; Coding system of the dictionary file.
578;; QUAILFILE ;; Name of the Quail package file.
579;; CONVERTER ;; Function to generate the Quail package.
580;; COPYRIGHT-NOTICE ;; Copyright notice of the source dictionary.
581;; )
582
583(defvar quail-misc-package-ext-info
584 '(("chinese-b5-tsangchi" "\e$(06A\e(BB"
a1506d29 585 "cangjie-table.b5" big5 "tsang-b5.el"
c063e381
KH
586 tsang-b5-converter
587 "\
588;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
589;; #
590;; # Permission to copy and distribute both modified and
591;; # unmodified versions is granted without royalty provided
592;; # this notice is preserved.")
593
594 ("chinese-b5-quick" "\e$(0X|\e(BB"
a1506d29 595 "cangjie-table.b5" big5 "quick-b5.el"
c063e381
KH
596 quick-b5-converter
597 "\
598;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
599;; #
600;; # Permission to copy and distribute both modified and
601;; # unmodified versions is granted without royalty provided
602;; # this notice is preserved.")
603
604 ("chinese-cns-tsangchi" "\e$(GT?\e(BC"
605 "cangjie-table.cns" iso-2022-cn-ext "tsang-cns.el"
606 tsang-cns-converter
607 "\
608;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
609;; #
610;; # Permission to copy and distribute both modified and
611;; # unmodified versions is granted without royalty provided
612;; # this notice is preserved.")
613
614 ("chinese-cns-quick" "\e$(Gv|\e(BC"
615 "cangjie-table.cns" iso-2022-cn-ext "quick-cns.el"
616 quick-cns-converter
617 "\
618;; # Copyright 2001 Christian Wittern <wittern@iis.sinica.edu.tw>
619;; #
620;; # Permission to copy and distribute both modified and
621;; # unmodified versions is granted without royalty provided
622;; # this notice is preserved.")
623
624 ("chinese-py" "\e$AF4\e(BG"
625 "pinyin.map" cn-gb-2312 "PY.el"
626 py-converter
627 "\
628;; \"pinyin.map\" is included in a free package called CCE. It is
629;; available at:
630;; http://ftp.debian.org/debian/dists/potato/main
631;; /source/utils/cce_0.36.orig.tar.gz
632;; This package contains the following copyright notice.
633;;
634;;
635;; Copyright (C) 1999, Rui He, herui@cs.duke.edu
a1506d29
JB
636;;
637;;
c063e381 638;; CCE(Console Chinese Environment) 0.32
a1506d29
JB
639;;
640;; CCE is free software; you can redistribute it and/or modify it under the
641;; terms of the GNU General Public License as published by the Free Software
642;; Foundation; either version 1, or (at your option) any later version.
643;;
644;; CCE is distributed in the hope that it will be useful, but WITHOUT ANY
645;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
646;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
647;; details.
648;;
c063e381 649;; You should have received a copy of the GNU General Public License along with
a1506d29 650;; CCE; see the file COPYING. If not, write to the Free Software Foundation,
3ef97fb6 651;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
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
a1506d29 679;; CCE; see the file COPYING. If not, write to the Free Software Foundation,
3ef97fb6 680;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
a4c4011b 681
a4c4011b
AC
682 ("chinese-ctlau" "\e$AAuTA\e(B"
683 "CTLau.html" cn-gb-2312 "CTLau.el"
684 ctlau-gb-converter
685 "\
686;; \"CTLau.html\" is available at:
687;;
688;; http://umunhum.stanford.edu/~lee/chicomp/CTLau.html
689;;
690;; It contains the following copyright notice:
691;;
692;; # Copyright (C) 1988-2001 Fung Fung Lee (lee@umunhum.stanford.edu)
a1506d29 693;; #
a4c4011b
AC
694;; # This program is free software; you can redistribute it and/or
695;; # modify it under the terms of the GNU General Public License
696;; # as published by the Free Software Foundation; either version 2
697;; # of the License, or any later version.
a1506d29 698;; #
a4c4011b
AC
699;; # This program is distributed in the hope that it will be useful,
700;; # but WITHOUT ANY WARRANTY; without even the implied warranty of
701;; # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
702;; # GNU General Public License for more details.
a1506d29 703;; #
a4c4011b
AC
704;; # You should have received a copy of the GNU General Public License
705;; # along with this program; if not, write to the Free Software Foundation,
086add15 706;; # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
a4c4011b 707
e089be2d 708 ("chinese-ctlaub" "\e$(0N,Gn\e(B"
2bef0948 709 "CTLau-b5.html" big5 "CTLau-b5.el"
a4c4011b
AC
710 ctlau-b5-converter
711 "\
712;; \"CTLau-b5.html\" is available at:
713;;
714;; http://umunhum.stanford.edu/~lee/chicomp/CTLau-b5.html
715;;
716;; It contains the following copyright notice:
717;;
718;; # Copyright (C) 1988-2001 Fung Fung Lee (lee@umunhum.stanford.edu)
a1506d29 719;; #
a4c4011b
AC
720;; # This program is free software; you can redistribute it and/or
721;; # modify it under the terms of the GNU General Public License
722;; # as published by the Free Software Foundation; either version 2
723;; # of the License, or any later version.
a1506d29 724;; #
a4c4011b
AC
725;; # This program is distributed in the hope that it will be useful,
726;; # but WITHOUT ANY WARRANTY; without even the implied warranty of
727;; # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
728;; # GNU General Public License for more details.
a1506d29 729;; #
a4c4011b
AC
730;; # You should have received a copy of the GNU General Public License
731;; # along with this program; if not, write to the Free Software Foundation,
086add15 732;; # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.")
c063e381
KH
733 ))
734
735;; Generate a code of a Quail package in the current buffer from Tsang
736;; dictionary in the buffer DICBUF. The input method name of the
737;; Quail package is NAME, and the title string is TITLE.
738
739;; TSANG-P is non-nil, genereate \e$(06AQo\e(B input method. Otherwise
740;; generate \e$(0X|/y\e(B (simple version of \e$(06AQo\e(B). If BIG5-P is non-nil, the
741;; input method is for inputting Big5 characters. Otherwise the input
742;; method is for inputting CNS characters.
743
744(defun tsang-quick-converter (dicbuf name title tsang-p big5-p)
745 (let ((fulltitle (if tsang-p (if big5-p "\e$(06AQo\e(B" "\e$(GT?on\e(B")
746 (if big5-p "\e$(0X|/y\e(B" "\e$(Gv|Mx\e(B")))
747 dic)
748 (goto-char (point-max))
749 (if big5-p
750 (insert (format "\"\e$(0&d'GTT&,!J\e(B%s\e$(0!K\e(BBIG5
751
752 \e$(0KHM$\e(B%s\e$(0TT&,WoOu\e(B
753
754 [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]
755
756 [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 757
c063e381
KH
758 [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]
759
760\\\\<quail-translation-docstring>\"\n"
761 fulltitle fulltitle))
762 (insert (format "\"\e$(GDcEFrSD+!J\e(B%s\e$(G!K\e(BCNS
763
764 \e$(GiGk#\e(B%s\e$(GrSD+uomu\e(B
765
766 [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]
767
768 [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
769
770 [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
771
772\\\\<quail-translation-docstring>\"\n"
773 fulltitle fulltitle)))
774 (insert " '((\".\" . quail-next-translation-block)
775 (\",\" . quail-prev-translation-block))
776 nil nil)\n\n")
777 (insert "(quail-define-rules\n")
778 (save-excursion
779 (set-buffer dicbuf)
6ed24d2e
JR
780 ;; Handle double CR line ends, which result when checking out of
781 ;; CVS on MS-Windows.
782 (goto-char (point-min))
783 (while (re-search-forward "\r\r$" nil t)
784 (replace-match ""))
c063e381
KH
785 (goto-char (point-min))
786 (search-forward "A440")
787 (beginning-of-line)
788 (let ((table (make-hash-table :test 'equal))
789 val)
790 (while (not (eobp))
791 (forward-char 5)
792 (let ((trans (char-to-string (following-char)))
793 key slot)
794 (re-search-forward "[A-Z]+$" nil t)
795 (setq key (downcase
796 (if (or tsang-p
797 (<= (- (match-end 0) (match-beginning 0)) 1))
798 (match-string 0)
799 (string (char-after (match-beginning 0))
800 (char-after (1- (match-end 0)))))))
801 (setq val (gethash key table))
802 (if val (setq trans (concat val trans)))
803 (puthash key trans table)
804 (forward-line 1)))
805 (maphash #'(lambda (key val) (setq dic (cons (cons key val) dic)))
806 table)))
807 (setq dic (sort dic (function (lambda (x y) (string< (car x ) (car y))))))
808 (dolist (elt dic)
809 (insert (format "(%S\t%S)\n" (car elt) (cdr elt))))
810 (let ((punctuations '((";" "\e$(0!'!2!"!#!.!/\e(B" "\e$(G!'!2!"!#!.!/\e(B")
811 (":" "\e$(0!(!+!3!%!$!&!0!1\e(B" "\e$(G!(!+!3!%!$!&!0!1\e(B")
812 ("'" "\e$(0!e!d\e(B" "\e$(G!e!d\e(B")
813 ("\"" "\e$(0!g!f!h!i!q\e(B" "\e$(G!g!f!h!i!q\e(B")
814 ("\\" "\e$(0"`"b#M\e(B" "\e$(G"`"b#M\e(B")
815 ("|" "\e$(0!6!8!:"^\e(B" "\e$(G!6!8!:"^\e(B")
816 ("/" "\e$(0"_"a#L\e(B" "\e$(G"_"a#L\e(B")
817 ("?" "\e$(0!)!4\e(B" "\e$(G!)!4\e(B")
818 ("<" "\e$(0!R"6"A!T"H\e(B" "\e$(G!R"6"A!T"H\e(B")
819 (">" "\e$(0!S"7"B!U\e(B" "\e$(G!S"7"B!U\e(B")
820 ("[" "\e$(0!F!J!b!H!L!V!Z!X!\\e(B" "\e$(G!F!J!b!H!L!V!Z!X!\\e(B")
821 ("]" "\e$(0!G!K!c!I!M!W![!Y!]\e(B" "\e$(G!G!K!c!I!M!W![!Y!]\e(B")
822 ("{" "\e$(0!B!`!D\e(B " "\e$(G!B!`!D\e(B ")
823 ("}" "\e$(0!C!a!E\e(B" "\e$(G!C!a!E\e(B")
824 ("`" "\e$(0!j!k\e(B" "\e$(G!j!k\e(B")
825 ("~" "\e$(0"D"+",!<!=\e(B" "\e$(G"D"+",!<!=\e(B")
826 ("!" "\e$(0!*!5\e(B" "\e$(G!*!5\e(B")
827 ("@" "\e$(0"i"n\e(B" "\e$(G"i"n\e(B")
828 ("#" "\e$(0!l"-\e(B" "\e$(G!l"-\e(B")
829 ("$" "\e$(0"c"l\e(B" "\e$(G"c"l\e(B")
830 ("%" "\e$(0"h"m\e(B" "\e$(G"h"m\e(B")
831 ("&" "\e$(0!m".\e(B" "\e$(G!m".\e(B")
832 ("*" "\e$(0!n"/!o!w!x\e(B" "\e$(G!n"/!o!w!x\e(B")
833 ("(" "\e$(0!>!^!@\e(B" "\e$(G!>!^!@\e(B")
834 (")" "\e$(0!?!_!A\e(B" "\e$(G!?!_!A\e(B")
835 ("-" "\e$(0!7!9"#"$"1"@\e(B" "\e$(G!7!9"#"$"1"@\e(B")
836 ("_" "\e$(0"%"&\e(B" "\e$(G"%"&\e(B")
837 ("=" "\e$(0"8"C\e(B" "\e$(G"8"C\e(B")
838 ("+" "\e$(0"0"?\e(B" "\e$(G"0"?\e(B"))))
839 (dolist (elt punctuations)
840 (insert (format "(%S %S)\n" (concat "z" (car elt))
841 (if big5-p (nth 1 elt) (nth 2 elt))))))
842 (insert ")\n")))
843
844(defun tsang-b5-converter (dicbuf name title)
845 (tsang-quick-converter dicbuf name title t t))
846
847(defun quick-b5-converter (dicbuf name title)
848 (tsang-quick-converter dicbuf name title nil t))
849
850(defun tsang-cns-converter (dicbuf name title)
851 (tsang-quick-converter dicbuf name title t nil))
852
853(defun quick-cns-converter (dicbuf name title)
854 (tsang-quick-converter dicbuf name title nil nil))
855
856;; Generate a code of a Quail package in the current buffer from
857;; Pinyin dictionary in the buffer DICBUF. The input method name of
858;; the Quail package is NAME, and the title string is TITLE.
859
860(defun py-converter (dicbuf name title)
861 (goto-char (point-max))
862 (insert (format "%S\n" "\e$A::WVJdHk!KF4Rt!K\e(B
863
864 \e$AF4Rt7=08\e(B
865
866 \e$AP!P4S"NDWVD84z1m!8F4Rt!97{:E#,\e(B \"u(yu) \e$ATrSC\e(B u: \e$A1mJ>!C\e(B
867
868Pinyin base input method for Chinese charset GB2312 (`chinese-gb2312').
869
c3ff164a 870Pinyin is the standard roman transliteration method for Chinese.
c063e381
KH
871Pinyin uses a sequence of Latin alphabetic characters for each Chinese
872character. The sequence is made by the combination of the initials
873\(the beginning sounds) and finals (the ending sounds).
874
875 initials: b p m f d t n l z c s zh ch sh r j q x g k h
876 finals: a o e i er ai ei oa ou an en ang eng ong i ia iao ie iu ian in
877 iang ing iong u ua uo uai ui uan un uan ueng yu yue yuan yun
878
879 (Note: In the correct Pinyin writing, the sequence \"yu\" in the last
880 four finals should be written by the character u-umlaut `\e$A(9\e(B'.)
881
882With this input method, you enter a Chinese character by first
883entering its pinyin spelling.
884
885\\<quail-translation-docstring>
886
887For instance, to input \e$ADc\e(B, you type \"n i C-n 3\". The first \"n i\"
888is a Pinyin, \"C-n\" selects the next group of candidates (each group
889contains at most 10 characters), \"3\" select the third character in
890that group.
891
892This input method supports only Han characters. The related input
893method `chinese-py-punct' is the combination of this method and
894`chinese-punct'; it supports both Han characters and punctuation
895characters.
896
897For double-width GB2312 characters corresponding to ASCII, use the
898input method `chinese-qj'.
899
900The correct Pinyin system specifies tones by diacritical marks, but
901this input method doesn't use them, which results in easy (you don't
902have to know the exact tones), but verbose (many characters are assigned
903to the same key sequence) input. You may also want to try the input
904method `chinese-tonepy' with which you must specify tones by digits
905\(1..5)."))
906 (insert " '((\"\C-?\" . quail-delete-last-char)
907 (\".\" . quail-next-translation)
908 (\">\" . quail-next-translation)
909 (\",\" . quail-prev-translation)
910 (\"<\" . quail-prev-translation))
911 nil nil nil nil)\n\n")
912 (insert "(quail-define-rules\n")
913 (let ((pos (point)))
914 (insert-buffer-substring dicbuf)
915 (goto-char pos)
8c8d1081 916 (re-search-forward "^[a-z]")
7d15a839
KH
917 (beginning-of-line)
918 (delete-region pos (point))
c063e381
KH
919 (while (not (eobp))
920 (insert "(\"")
921 (skip-chars-forward "a-z")
922 (insert "\" \"")
923 (delete-char 1)
924 (end-of-line)
925 (insert "\")")
926 (forward-line 1)))
927 (insert ")\n"))
928
929;; Generate a code of a Quail package in the current buffer from
930;; Ziranma dictionary in the buffer DICBUF. The input method name of
931;; the Quail package is NAME, and the title string is TITLE.
932
933(defun ziranma-converter (dicbuf name title)
934 (let (dic)
935 (save-excursion
936 (set-buffer dicbuf)
937 (goto-char (point-min))
938 (search-forward "%keyname end\n")
939 (let ((table (make-hash-table :test 'equal))
940 elt pos key trans val)
941 (while (not (eobp))
942 (setq pos (point))
943 (skip-chars-forward "^ \t")
944 (setq key (buffer-substring pos (point)))
945 (skip-chars-forward " \t")
946 (setq trans (vector (buffer-substring (point) (line-end-position))))
947 (setq val (gethash key table))
948 (if val (setq trans (vconcat val trans)))
949 (puthash key trans table)
950 (forward-line 1))
951 (maphash #'(lambda (key trans)
952 (let ((len (length trans))
953 i)
954 (if (and (= len 1) (= (length (aref trans 0)) 1))
955 (setq trans (aref trans 0))
956 (setq i 0)
957 (while (and (< i len)
958 (= (length (aref trans i)) 1))
959 (setq i (1+ i)))
960 (if (= i len)
961 (setq trans (mapconcat 'identity trans "")))))
962 (setq dic (cons (cons key trans) dic)))
963 table)))
964 (setq dic (sort dic (function (lambda (x y) (string< (car x) (car y))))))
965 (goto-char (point-max))
966 (insert (format "%S\n" "\e$A::WVJdHk!K!>WTH;!?!K\e(B
967
968 \e$A<|EL6TUU1m\e(B:
969 \e$A)3)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)W)%)%)7\e(B
970 \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
971 \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
972 \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
973 \e$A);)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)?\e(B
974 \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
975 \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
976 \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
977 \e$A);)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)_)W)%)%)7\e(B
978 \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
979 \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
980 \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
981 \e$A);)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)_)%)%)?\e(B
982
983
984Pinyin base input method for Chinese GB2312 characters (`chinese-gb2312').
985
986Pinyin is the standard roman transliteration method for Chinese.
987For the details of Pinyin system, see the documentation of the input
988method `chinese-py'.
989
990Unlike the standard spelling of Pinyin, in this input method all
991initials and finals are assigned to single keys (see the above table).
992For instance, the initial \"ch\" is assigned to the key `i', the final
993\"iu\" is assigned to the key `q', and tones 1, 2, 3, 4, and \e$AGaIy\e(B are
994assigned to the keys `q', `w', `e', `r', `t' respectively.
995
996\\<quail-translation-docstring>
997
998To input one-letter words, you type 4 keys, the first two for the
999Pinyin of the letter, next one for tone, and the last one is always a
1000quote ('). For instance, \"vsq'\" input \e$AVP\e(B. Exceptions are these
1001letters. You can input them just by typing a single key.
1002
1003 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
1004 Key: a b c d e f g h i j k l m
1005 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
1006 Key: n o p q r s t u v w x y z
1007
1008To input two-letter words, you have two ways. One way is to type 4
1009keys, two for the first Pinyin, two for the second Pinyin. For
1010instance, \"vsgo\" inputs \e$AVP9z\e(B. Another way is to type 3 keys: 2
1011initials of two letters, and quote ('). For instance, \"vg'\" also
1012inputs \e$AVP9z\e(B.
1013
1014To input three-letter words, you type 4 keys: initials of three
1015letters, and the last is quote ('). For instance, \"bjy'2\" inputs \e$A11\e(B
1016\e$A>)Q<\e(B (the last `2' is to select one of the candidates).
1017
1018To input words of more than three letters, you type 4 keys, initials
1019of the first three letters and the last letter. For instance,
1020\"bjdt\" inputs \e$A11>)5gJSL(\e(B.
1021
1022To input symbols and punctuations, type `/' followed by one of `a' to
1023`z', then select one of the candidates."))
1024 (insert " '((\"\C-?\" . quail-delete-last-char)
1025 (\".\" . quail-next-translation)
1026 (\"[\" . quail-next-translation)
1027 (\",\" . quail-prev-translation)
1028 (\"]\" . quail-prev-translation))
1029 nil nil nil nil)\n\n")
1030 (insert "(quail-define-rules\n")
1031 (dolist (elt dic)
1032 (insert (format "(%S %S)\n" (car elt) (cdr elt))))
1033 (insert ")\n")))
1034
a4c4011b
AC
1035;; Generate the code for a Quail package in the current buffer from a
1036;; CTLau or CTLau-b5 dictionary in the buffer DICBUF. The input
1037;; method name of the Quail package is NAME, and the title string is
1038;; TITLE. DESCRIPTION is the string shown by describe-input-method.
1039
1040(defun ctlau-converter (dicbuf name title description)
1041 (goto-char (point-max))
1042 (insert (format "%S\n" description))
1043 (insert " '((\"\C-?\" . quail-delete-last-char)
1044 (\".\" . quail-next-translation)
1045 (\">\" . quail-next-translation)
1046 (\",\" . quail-prev-translation)
1047 (\"<\" . quail-prev-translation))
1048 nil nil nil nil)\n\n")
1049 (insert "(quail-define-rules\n")
1050 (let (dicbuf-start dicbuf-end key-start key (pos (point)))
1051 ;; Find the dictionary, which starts below a horizontal rule and
1052 ;; ends at the second to last line in the HTML file.
1053 (save-excursion
1054 (set-buffer dicbuf)
1055 (goto-char (point-min))
1056 (search-forward "#\n#<hr>\n")
1057 (setq dicbuf-start (point))
1058 (goto-char (point-max))
1059 (forward-line -1)
1060 (setq dicbuf-end (point)))
1061 (insert-buffer-substring dicbuf dicbuf-start dicbuf-end)
1062 ;; CTLau-b5.html contains characters (0xa1 0xbc) which show up as
1063 ;; hollow boxes when the original characters in CTLau.html from
1064 ;; which the file is converted have no Big5 equivalent. Go
1065 ;; through and delete them.
1066 (goto-char pos)
1067 (while (search-forward "\e$(0!{\e(B" nil t)
1068 (delete-char -1))
1069 ;; Uppercase keys in dictionary need to be downcased. Backslashes
1070 ;; at the beginning of keys need to be turned into double
1071 ;; backslashes.
1072 (goto-char pos)
1073 (while (not (eobp))
1074 (insert "(\"")
1075 (if (char-equal (following-char) ?\\)
1076 (insert "\\"))
1077 (setq key-start (point))
1078 (skip-chars-forward "\\\\A-Z")
1079 (downcase-region key-start (point))
1080 (insert "\" \"")
1081 (delete-char 1)
1082 (end-of-line)
1083 (insert "\")")
1084 (forward-line 1)))
1085 (insert ")\n"))
1086
1087(defun ctlau-gb-converter (dicbuf name title)
1088 (ctlau-converter dicbuf name title
1089"\e$A::WVJdHk!KAuN}OiJ=TARt!K\e(B
1090
1091 \e$AAuN}OiJ=TASoW"Rt7=08\e(B
1092 Sidney Lau's Cantonese transcription scheme as described in his book
1093 \"Elementary Cantonese\", The Government Printer, Hong Kong, 1972.
1094 This file was prepared by Fung Fung Lee (\e$A@n7c7e\e(B).
1095 Originally converted from CTCPS3.tit
1096 Last modified: June 2, 1993.
1097
4e7e1f03 1098 Some infrequent GB characters are accessed by typing \\, followed by
a4c4011b
AC
1099 the Cantonese romanization of the respective radical (\e$A2?JW\e(B)."))
1100
1101(defun ctlau-b5-converter (dicbuf name title)
1102 (ctlau-converter dicbuf name title
1103"\e$(0KH)tTT&,!(N,Tg>A*#Gn5x!(\e(B
1104
1105 \e$(0N,Tg>A*#GnM$0D5x'J7{\e(B
1106 Sidney Lau's Cantonese transcription scheme as described in his book
1107 \"Elementary Cantonese\", The Government Printer, Hong Kong, 1972.
1108 This file was prepared by Fung Fung Lee (\e$(0,XFS76\e(B).
1109 Originally converted from CTCPS3.tit
1110 Last modified: June 2, 1993.
1111
4e7e1f03 1112 Some infrequent characters are accessed by typing \\, followed by
a4c4011b
AC
1113 the Cantonese romanization of the respective radical (\e$(0?f5}\e(B)."))
1114
c063e381 1115(defun miscdic-convert (filename &optional dirname)
a1506d29 1116 "Convert a dictionary file FILENAME into a Quail package.
c063e381
KH
1117Optional argument DIRNAME if specified is the directory name under which
1118the generated Quail package is saved."
1119 (interactive "FInput method dictionary file: ")
1120 (or (file-readable-p filename)
1121 (error "%s does not exist" filename))
1122 (let ((tail quail-misc-package-ext-info)
1123 (default-buffer-file-coding-system 'iso-2022-7bit)
1124 slot
1125 name title dicfile coding quailfile converter copyright
1126 dicbuf)
1127 (while tail
d1df889e
KH
1128 (setq slot (car tail)
1129 dicfile (nth 2 slot)
1130 quailfile (nth 4 slot))
1131 (when (and (or (string-match dicfile filename)
1132 ;; MS-DOS filesystem truncates file names to 8+3
1133 ;; limits, so "cangjie-table.cns" becomes
1134 ;; "cangjie-.cns", and the above string-match
1135 ;; fails. Give DOS users a chance...
1136 (and (fboundp 'msdos-long-file-names)
1137 (not (msdos-long-file-names))
1138 (string-match (dos-8+3-filename dicfile) filename)))
1139 (if (file-newer-than-file-p
1140 filename (expand-file-name quailfile dirname))
1141 t
1142 (message "%s is up to date" quailfile)
1143 nil))
1144 (setq name (car slot)
c063e381 1145 title (nth 1 slot)
c063e381 1146 coding (nth 3 slot)
c063e381
KH
1147 converter (nth 5 slot)
1148 copyright (nth 6 slot))
1149 (message "Converting %s to %s..." dicfile quailfile)
1150 (with-temp-file (expand-file-name quailfile dirname)
1a03f515
KH
1151 ;; Explicitly speficy eol format to `unix'.
1152 (set-buffer-file-coding-system 'iso-2022-7bit-unix)
7d15a839
KH
1153 (insert ";; Quail package `" name "' -*- coding:iso-2022-7bit; ")
1154 (insert "byte-compile-disable-print-circle:t; -*-\n");
c063e381
KH
1155 (insert ";; Generated by the command `miscdic-convert'\n")
1156 (insert ";; Date: " (current-time-string) "\n")
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 "\" \""
9d5db27a 1166 (if (eq coding 'big5) "Chinese-BIG5" "Chinese-CNS")
c063e381
KH
1167 "\" \"" title "\" t\n")
1168 (let* ((coding-system-for-read coding)
1169 (dicbuf (find-file-noselect filename)))
1170 (funcall converter dicbuf name title)
1171 (kill-buffer dicbuf)))
1172 (message "Converting %s to %s...done" dicfile quailfile))
1173 (setq tail (cdr tail)))))
1174
1175(defun batch-miscdic-convert ()
1176 "Run `miscdic-convert' on the files remaing on the command line.
1177Use this from the command line, with `-batch';
1178it won't work in an interactive Emacs.
1179If there's an argument \"-dir\", the next argument specifies a directory
1180to store generated Quail packages."
1181 (defvar command-line-args-left) ; Avoid compiler warning.
1182 (if (not noninteractive)
1183 (error "`batch-miscdic-convert' should be used only with -batch"))
1184 (let ((dir default-directory)
1185 filename)
1186 (while command-line-args-left
1187 (if (string= (car command-line-args-left) "-dir")
1188 (progn
1189 (setq command-line-args-left (cdr command-line-args-left))
0631f894
KH
1190 (setq dir (car command-line-args-left))
1191 (setq command-line-args-left (cdr command-line-args-left))))
c063e381
KH
1192 (setq filename (car command-line-args-left)
1193 command-line-args-left (cdr command-line-args-left))
1194 (if (file-directory-p filename)
1195 (dolist (file (directory-files filename t nil t))
1196 (miscdic-convert file dir))
1197 (miscdic-convert filename dir))))
1198 (kill-emacs 0))
1199
5cdaf2a5
KH
1200;; Local Variables:
1201;; coding: iso-2022-7bit
1202;; End:
60370d40 1203
ab5796a9 1204;;; arch-tag: 8ad478b2-a985-4da2-b47f-d8ee5d7c24a3
60370d40 1205;;; titdic-cnv.el ends here