add cangjie-table.b5 cangjie-table.cns ziranma.cin pinyin.map
[bpt/emacs.git] / lisp / international / titdic-cnv.el
CommitLineData
49ed466f 1;;; titdic-cnv.el --- convert cxterm dictionary (TIT format) to Quail package
4ed46869 2
4ed46869 3;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
fa526c4a 4;; Licensed to the Free Software Foundation.
4ed46869
KH
5
6;; Keywords: Quail, TIT, cxterm
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
369314dc
KH
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
4ed46869
KH
24
25;;; Comments:
26
49ed466f 27;; Convert cxterm dictionary (of TIT format) to quail-package.
4ed46869
KH
28;;
29;; Usage (within Emacs):
49ed466f 30;; M-x titdic-convert<CR>CXTERM-DICTIONARY-NAME<CR>
4ed46869 31;; Usage (from shell):
49ed466f 32;; % emacs -batch -l titdic-cnv -f batch-titdic-convert\
4ed46869
KH
33;; [-dir DIR] [DIR | FILE] ...
34;;
35;; When you run titdic-convert within Emacs, you have a chance to
36;; modify arguments of `quail-define-package' before saving the
37;; converted file. For instance, you are likely to modify TITLE,
38;; DOCSTRING, and KEY-BINDINGS.
39
49ed466f 40;; Cxterm dictionary file (*.tit) is a line-oriented text (English,
4ed46869
KH
41;; Chinese, Japanese, and Korean) file. The whole file contains of
42;; two parts, the definition part (`header' here after) followed by
43;; the dictionary part (`body' here after). All lines begin with
44;; leading '#' are ignored.
45;;
46;; Each line in the header part has two fields, KEY and VALUE. These
47;; fields are separated by one or more white characters.
48;;
49;; Each line in the body part has two fields, KEYSEQ and TRANSLATIONS.
50;; These fields are separated by one or more white characters.
51;;
52;; See the manual page of `tit2cit' of cxterm distribution for more
53;; detail.
54
55;;; Code:
56
57(require 'quail)
58
49ed466f 59;; List of values of key "ENCODE:" and the corresponding Emacs
4ed46869
KH
60;; coding-system and language environment name.
61(defvar tit-encode-list
a7f2c216
KH
62 '(("GB" euc-china "Chinese-GB")
63 ("BIG5" cn-big5 "Chinese-BIG5")
64 ("JIS" euc-japan "Japanese")
49ed466f
KH
65 ("KS" euc-kr "Korean")))
66
4558e816
KH
67;; Alist of input method names and the corresponding title and extra
68;; docstring. For each of input method generated from TIT dictionary,
69;; a docstring is automatically generated from the comments in the
70;; dictionary. The extra docstring in this alist is to add more
71;; information.
72;; The command describe-input-method shows the automatically generated
73;; docstring, then an extra docstrings while replacing the form \<VAR>
74;; by the value of variable VAR. For instance, the form
75;; \<quail-translation-docstring> is replaced by a description about
76;; how to select a translation from a list of candidates.
77
6b1e079c
KH
78(defvar quail-cxterm-package-ext-info
79 '(("chinese-4corner" "\e$(0(?-F\e(B")
80 ("chinese-array30" "\e$(0#R#O\e(B")
4558e816
KH
81 ("chinese-ccdospy" "\e$AKuF4\e(B"
82 "Pinyin base input method for Chinese charset GB2312 \(`chinese-gb2312').
83
84Pinyin is the standared roman transliteration method for Chinese.
85For the detail of Pinyin system, see the documentation of the input
86method `chinese-py'.
87
88This input method works almost the same way as `chinese-py'. The
89difference is that you type a single key for these Pinyin spelling.
90 Pinyin: zh en eng ang ch an ao ai ong sh ing yu(\e$A(9\e(B)
91 keyseq: a f g h i j k l s u y v
92For expample:
93 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
94 Pinyin: a guo zhong wen guang yu quan
95 Keyseq: a1 guo4 as1 wf4 guh1 yu..6 qvj6
96
97\\<quail-translation-docstring>
98
99For double-width GB2312 characters correponding to ASCII, use the
100input method `chinese-qj'.")
101
6b1e079c 102 ("chinese-ctlau" "\e$AAuTA\e(B")
4558e816 103
6b1e079c 104 ("chinese-ctlaub" "\e$(0N,Gn\e(B")
4558e816
KH
105
106 ("chinese-ecdict" "\e$(05CKH\e(B"
107"In this input method, you enter a Chinese (Big5) charactere or word
108by typing the corresponding English word. For example, if you type
109\"computer\", \"\e$(0IZH+\e(B\" is input.
110
111\\<quail-translation-docstring>")
112
113 ("chinese-etzy" "\e$(06/0D\e(B"
114"Zhuyin base input method for Chinese Big5 characters (`chinese-big5-1',
115`chinese-big5-2').
116
117Zhuyin is a kind of phonetic symbol. One to three Zhuyin symbols
118compose one Chinese character.
119
120In this input method, you enter a Chinese character by first typing
121keys corresponding to Zhuyin symbols (see the above table) followed by
122SPC, 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,
1234:\e$(0(+Vy\e(B).
124
125\\<quail-translation-docstring>")
6b1e079c
KH
126
127 ("chinese-punct-b5" "\e$(0O:\e(BB"
128 "Input method for Chinese punctuations and symbols of Big5
129\(`chinese-big5-1' and `chinese-big5-2').")
130
131 ("chinese-punct" "\e$A1j\e(BG"
132 "Input method for Chinese punctuations and symbols of GB2312
133\(`chinese-gb2312').")
134
135 ("chinese-py-b5" "\e$(03<\e(BB"
136 "Pinyin base input method for Chinese Big5 characters
137\(`chinese-big5-1', `chinese-big5-2').
138
139This input method works almost the same way as `chinese-py' (which
140see).
141
142This input method supports only Han characters. The more convenient
43b11fee
EZ
143method is `chinese-py-punct-b5', which is the combination of this
144method and `chinese-punct-b5' and which supports both Han characters
145and punctuation/symbols.
6b1e079c 146
43b11fee 147For double-width Big5 characters corresponding to ASCII, use the input
6b1e079c
KH
148method `chinese-qj-b5'.
149
150The input method `chinese-py' and `chinese-tonepy' are also Pinyin
43b11fee 151based, but for the character set GB2312 (`chinese-gb2312').")
6b1e079c
KH
152
153 ("chinese-py" "\e$AF4\e(BG"
154 "Pinyin base input method for Chinese charset GB2312
155\(`chinese-gb2312').
156
157Pinyin is the standared roman transliteration method for Chinese.
43b11fee
EZ
158Pinyin uses a sequence of Latin alphabetic characters for each Chinese
159character. The sequence is made by the combination of the initials
4558e816 160\(the beginning sounds) and finals \(the ending sounds).
6b1e079c
KH
161
162 initials: b p m f d t n l z c s zh ch sh r j q x g k h
163 finals: a o e i er ai ei oa ou an en ang eng ong i ia iao ie iu ian in
164 iang ing iong u ua uo uai ui uan un uan ueng yu yue yuan yun
165
166 (Note: In the correct Pinyin writing, the sequence \"yu\" in the last
167 four finals should be written by the character u-umlaut `\e$A(9\e(B'.)
168
4558e816
KH
169With this input method, you enter a Chinese character by first
170entering its pinyin spelling.
171
172\\<quail-translation-docstring>
6b1e079c
KH
173
174For instance, to input \e$ADc\e(B, you type \"n i C-n 3\". The first \"n i\"
175is a Pinyin, \"C-n\" selects the next group of candidates (each group
176contains at most 10 characters), \"3\" select the third character in
43b11fee 177that group.
6b1e079c 178
4558e816 179This input method supports only Han characters. The related input
d20faceb 180method `chinese-py-punct' is the combination of this method and
4558e816
KH
181`chinese-punct'; it supports both Han characters and punctuation
182characters.
6b1e079c 183
43b11fee 184For double-width GB2312 characters corresponding to ASCII, use the
6b1e079c
KH
185input method `chinese-qj'.
186
187The correct Pinyin system specifies tones by diacritical marks, but
188this input method doesn't use them, which results in easy (you don't
43b11fee
EZ
189have to know the exact tones), but verbose (many characters are assigned
190to the same key sequence) input. You may also want to try the input
6b1e079c
KH
191method `chinese-tonepy' with which you must specify tones by digits
192\(1..5).")
193
4558e816
KH
194 ("chinese-qj-b5" "\e$(0)A\e(BB")
195
196 ("chinese-qj" "\e$AH+\e(BG")
197
6b1e079c 198 ("chinese-sw" "\e$AJWN2\e(B"
4558e816
KH
199"Radical base input method for Chinese charset GB2312 (`chinese-gb2312').
200
d20faceb
EZ
201In this input method, you enter a Chinese character by typing two
202keys. The first key corresponds to the first (\e$AJW\e(B) radical, the second
203key corresponds to the last (\e$AN2\e(B) radical. The correspondence of keys
204and radicals is as below:
4558e816
KH
205
206 first radical:
207 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
208 \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
209 last radical:
210 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
211 \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
212
43e5a7fe 213\\<quail-translation-docstring>")
4558e816 214
6b1e079c
KH
215 ("chinese-tonepy" "\e$A5wF4\e(B"
216 "Pinyin base input method for Chinese charset GB2312 (`chinese-gb2312').
217
218Pinyin is the standared roman transliteration method for Chinese.
d20faceb 219For the details of Pinyin system, see the documentation of the input
6b1e079c
KH
220method `chinese-py'.
221
222This input method works almost the same way as `chinese-py'. The
4558e816
KH
223difference is that you must type 1..5 after each Pinyin spelling to
224specify 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).
225
43e5a7fe 226\\<quail-translation-docstring>
4558e816
KH
227
228For instance, to input \e$ADc\e(B, you type \"n i 3 3\", the first \"n i\" is
229a Pinyin, the next \"3\" specifies tone, and the last \"3\" selects
230the third character from the candidate list.
6b1e079c
KH
231
232For double-width GB2312 characters correponding to ASCII, use the
233input method `chinese-qj'.")
234
4558e816
KH
235 ("chinese-ziranma" "\e$AK+F4\e(B"
236"Pinyin base input method for Chinese GB2312 characters (`chinese-gb2312').
237
d20faceb
EZ
238Pinyin is the standard roman transliteration method for Chinese.
239For the details of Pinyin system, see the documentation of the input
4558e816
KH
240method `chinese-py'.
241
d20faceb 242unlike the standard spelling of Pinyin, in this input method all
4558e816
KH
243initials and finals are assigned to single keys (see the above table).
244For instance, the initial \"ch\" is assigned to the key `i', the final
d20faceb 245\"iu\" is assigned to the key `q', and tones 1, 2, 3, 4, and \e$AGaIy\e(B are
4558e816
KH
246assigned to the keys `q', `w', `e', `r', `t' respectively.
247
43e5a7fe 248\\<quail-translation-docstring>
4558e816 249
d20faceb
EZ
250To input one-letter words, you type 4 keys, the first two for the
251Pinyin of the letter, next one for tone, and the last one is always a
252quote ('). For instance, \"vsq'1\" input \e$AVP\e(B. Exceptions are these
253letters. You can input them just by typing a single key.
4558e816
KH
254
255 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
256 Key: a b c d e f g h i j k l m
257 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
258 Key: n o p q r s t u v w x y z
259
d20faceb 260To input two-letter words, you have two ways. One way is to type 4
4558e816 261keys, two for the first Pinyin, two for the second Pinyin. For
d20faceb
EZ
262instance, \"vsgo\" inputs \e$AVP9z\e(B. Another way is to type 3 keys: 2
263initials of two letters, and quote ('). For instance, \"vg'\" also
264inputs \e$AVP9z\e(B.
4558e816 265
d20faceb
EZ
266To input three-letter words, you type 4 keys: initials of three
267letters, and the last is quote ('). For instance, \"bjy'2\" inputs \e$A11\e(B
268\e$A>)Q<\e(B (the last `2' is to select one of the candidates).
4558e816
KH
269
270To input words of more than three letters, you type 4 keys, initials
271of the first three letters and the last letter. For instance,
d20faceb 272\"bjdt\" inputs \e$A11>)5gJSL(\e(B.
4558e816
KH
273
274To input symbols and punctuations, type `/' followed by one of `a' to
d20faceb 275`z', then select one of the candidates.
4558e816
KH
276
277")
278
279 ("chinese-zozy" "\e$(0I\0D\e(B"
280"Zhuyin base input method for Chinese Big5 characters (`chinese-big5-1',
281`chinese-big5-2').
282
d20faceb 283Zhuyin is a kind of a phonetic symbol. One to three Zhuyin symbols
4558e816
KH
284compose a Chinese character.
285
286In this input method, you enter a Chinese character by first typing
287keys corresponding to Zhuyin symbols (see the above table) followed by
288SPC, 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,
2897:\e$(0M=Vy\e(B).
290
43e5a7fe 291\\<quail-translation-docstring>")))
4ed46869
KH
292
293;; Return a value of the key in the current line.
294(defsubst tit-read-key-value ()
295 (if (looking-at "[^ \t\n]+")
296 (car (read-from-string (concat "\"" (match-string 0) "\"")))))
297
298;; Return an appropriate quail-package filename from FILENAME (TIT
49ed466f
KH
299;; dictionary filename). For instance, ".../ZOZY.tit" -> "ZOZY.el".
300(defun tit-make-quail-package-file-name (filename &optional dirname)
4ed46869 301 (expand-file-name
49ed466f 302 (concat (file-name-nondirectory (substring filename 0 -4)) ".el")
4ed46869
KH
303 dirname))
304
1375754c
KH
305;; This value is nil if we are processing phrase dictionary.
306(defconst tit-dictionary t)
4ed46869
KH
307(defvar tit-encode nil)
308(defvar tit-default-encode "GB")
309
310;; Generate elements of KEY-BINDINGS arg for `quail-define-package' so
311;; that each characters in KEYS invokes FUNCTION-SYMBOL.
312(defun tit-generate-key-bindings (keys function-symbol)
313 (let ((len (length keys))
314 (i 0)
1375754c 315 (first t)
4ed46869
KH
316 key)
317 (while (< i len)
1375754c 318 (or first (princ "\n "))
4ed46869 319 (setq key (aref keys i))
1375754c
KH
320 (if (if (< key ?\ )
321 (eq (lookup-key quail-translation-keymap
322 (char-to-string key))
4ed46869 323 'quail-execute-non-quail-command)
1375754c
KH
324 (<= key 127))
325 (progn
326 (princ (cons (cond ((< key ?\ ) (format "\"\\C-%c\"" (+ key ?@)))
327 ((< key 127) (format "\"%c\"" key))
328 (t "\"\\C-?\""))
329 function-symbol))
330 (setq first nil)))
4ed46869
KH
331 (setq i (1+ i)))))
332
333;; Analyze header part of TIT dictionary and generate an appropriate
334;; `quail-define-package' function call.
335(defun tit-process-header (filename)
336 (message "Processing header part...")
337 (goto-char (point-min))
338
1375754c
KH
339 ;; At first, generate header part of the Quail package while
340 ;; collecting information from the original header.
341 (let ((package (concat
342 "chinese-"
343 (substring (downcase (file-name-nondirectory filename))
344 0 -4)))
345 ;; TIT keywords and the corresponding default values.
4ed46869
KH
346 (tit-multichoice t)
347 (tit-prompt "")
348 (tit-comments nil)
349 (tit-backspace "\010\177")
350 (tit-deleteall "\015\025")
351 (tit-moveright ".>")
352 (tit-moveleft ",<")
353 (tit-keyprompt nil))
1375754c
KH
354
355 (princ ";; Quail package `")
356 (princ package)
357 (princ "' generated by the command `titdic-convert'\n;;\tDate: ")
358 (princ (current-time-string))
359 (princ "\n;;\tOriginal TIT dictionary file: ")
360 (princ (file-name-nondirectory filename))
361 (princ "\n\n;;; Comment:\n\n")
86e4f7c0 362 (princ ";; Byte-compile this file again after any modification.\n\n")
1375754c
KH
363 (princ ";;; Start of the header of original TIT dictionary.\n\n")
364
4ed46869 365 (while (not (eobp))
1375754c
KH
366 (let ((ch (following-char))
367 (pos (point)))
4ed46869
KH
368 (cond ((= ch ?C) ; COMMENT
369 (cond ((looking-at "COMMENT")
370 (let ((pos (match-end 0)))
371 (end-of-line)
4ed46869
KH
372 (setq tit-comments (cons (buffer-substring pos (point))
373 tit-comments))))))
374 ((= ch ?M) ; MULTICHOICE, MOVERIGHT, MOVELEFT
375 (cond ((looking-at "MULTICHOICE:[ \t]*")
376 (goto-char (match-end 0))
377 (setq tit-multichoice (looking-at "YES")))
378 ((looking-at "MOVERIGHT:[ \t]*")
379 (goto-char (match-end 0))
380 (setq tit-moveright (tit-read-key-value)))
381 ((looking-at "MOVELEFT:[ \t]*")
382 (goto-char (match-end 0))
383 (setq tit-moveleft (tit-read-key-value)))))
384 ((= ch ?P) ; PROMPT
385 (cond ((looking-at "PROMPT:[ \t]*")
386 (goto-char (match-end 0))
6b1e079c
KH
387 (setq tit-prompt (tit-read-key-value))
388 ;; Some TIT dictionaies that are encoded by
389 ;; euc-china contains invalid character at the tail.
390 (let* ((last (aref tit-prompt (1- (length tit-prompt))))
391 (split (split-char last)))
392 (if (or (eq (nth 1 split) 32)
393 (eq (nth 2 split) 32))
394 (setq tit-prompt (substring tit-prompt 0 -1)))))))
4ed46869
KH
395 ((= ch ?B) ; BACKSPACE, BEGINDICTIONARY,
396 ; BEGINPHRASE
397 (cond ((looking-at "BACKSPACE:[ \t]*")
398 (goto-char (match-end 0))
399 (setq tit-backspace (tit-read-key-value)))
400 ((looking-at "BEGINDICTIONARY")
1375754c 401 (setq tit-dictionary t))
4ed46869 402 ((looking-at "BEGINPHRASE")
1375754c 403 (setq tit-dictionary nil))))
4ed46869
KH
404 ((= ch ?K) ; KEYPROMPT
405 (cond ((looking-at "KEYPROMPT(\\(.*\\)):[ \t]*")
406 (let ((key-char (match-string 1)))
407 (goto-char (match-end 0))
1fa1e1f5
RS
408 (if (string-match "\\\\[0-9]+" key-char)
409 (setq key-char
410 (car (read-from-string (format "\"%s\""
411 key-char)))))
4ed46869
KH
412 (setq tit-keyprompt
413 (cons (cons key-char (tit-read-key-value))
1375754c
KH
414 tit-keyprompt)))))))
415 (end-of-line)
416 (princ ";; ")
417 (princ (buffer-substring pos (point)))
418 (princ "\n")
419 (forward-line 1)))
4ed46869 420
1375754c
KH
421 (princ "\n;;; End of the header of original TIT dictionary.\n\n")
422 (princ ";;; Code:\n\n(require 'quail)\n\n")
423
424 (princ "(quail-define-package ")
425 ;; Args NAME, LANGUAGE, TITLE
6b1e079c 426 (let ((title (nth 1 (assoc package quail-cxterm-package-ext-info))))
1375754c
KH
427 (princ "\"")
428 (princ package)
429 (princ "\" \"")
430 (princ (nth 2 (assoc tit-encode tit-encode-list)))
431 (princ "\" \"")
432 (princ (or title
433 (if (string-match "[:\e$A!K\e$(0!(!J\e(B]+\\([^:\e$A!K\e$(0!(!K\e(B]+\\)" tit-prompt)
434 (substring tit-prompt (match-beginning 1) (match-end 1))
435 tit-prompt)))
436 (princ "\"\n"))
4ed46869
KH
437
438 ;; Arg GUIDANCE
439 (if tit-keyprompt
440 (progn
1375754c 441 (princ " '(")
4ed46869 442 (while tit-keyprompt
1375754c
KH
443 (princ " ")
444 (princ (format "(%d . \"%s\")\n"
445 (string-to-char (car (car tit-keyprompt)))
446 (cdr (car tit-keyprompt))))
4ed46869 447 (setq tit-keyprompt (cdr tit-keyprompt)))
1375754c
KH
448 (princ ")"))
449 (princ " t\n"))
4ed46869
KH
450
451 ;; Arg DOCSTRING
6b1e079c
KH
452 (let ((doc (concat tit-prompt "\n"))
453 (comments (if tit-comments
454 (mapconcat 'identity (nreverse tit-comments) "\n")))
455 (doc-ext (nth 2 (assoc package quail-cxterm-package-ext-info))))
456 (if comments
457 (setq doc (concat doc "\n" comments "\n")))
458 (if doc-ext
459 (setq doc (concat doc "\n" doc-ext "\n")))
460 (prin1 doc)
461 (terpri))
4ed46869
KH
462
463 ;; Arg KEY-BINDINGS
1375754c 464 (princ " '(")
4ed46869 465 (tit-generate-key-bindings tit-backspace 'quail-delete-last-char)
1375754c 466 (princ "\n ")
4ed46869 467 (tit-generate-key-bindings tit-deleteall 'quail-abort-translation)
1375754c 468 (princ "\n ")
4ed46869 469 (tit-generate-key-bindings tit-moveright 'quail-next-translation)
1375754c 470 (princ "\n ")
4ed46869 471 (tit-generate-key-bindings tit-moveleft 'quail-prev-translation)
1375754c 472 (princ ")\n")
4ed46869
KH
473
474 ;; Args FORGET-TRANSLATION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT.
475 ;; The remaining args are all nil.
1375754c
KH
476 (princ " nil")
477 (princ (if tit-multichoice " nil" " t"))
478 (princ (if tit-keyprompt " t t)\n\n" " nil nil)\n\n"))))
479
480(defsubst tit-flush-translations (key translations)
481 (if (string-match "\\\\[0-9][0-9][0-9]" key)
482 (let ((newkey (concat (substring key 0 (match-beginning 0))
483 (car (read-from-string
484 (concat "\"" (match-string 0 key) "\"")))))
485 (idx (match-end 0)))
486 (while (string-match "\\\\[0-9][0-9][0-9]" key idx)
487 (setq newkey (concat
488 newkey
489 (substring key idx (match-beginning 0))
490 (car (read-from-string
491 (concat "\"" (match-string 0 key) "\"")))))
492 (setq idx (match-end 0)))
493 (setq key (concat newkey (substring key idx)))))
494 (prin1 (list key (if tit-dictionary translations
495 (vconcat (nreverse translations)))))
496 (princ "\n"))
4ed46869
KH
497
498;; Convert body part of TIT dictionary into `quail-define-rules'
499;; function call.
500(defun tit-process-body ()
501 (message "Formatting translation rules...")
1375754c
KH
502 (let* ((template (list nil nil))
503 (second (cdr template))
504 (prev-key "")
505 ch key translations pos)
506 (princ "(quail-define-rules\n")
4ed46869 507 (while (null (eobp))
1375754c
KH
508 (setq ch (following-char))
509 (if (or (= ch ?#) (= ch ?\n))
510 (forward-line 1)
4ed46869 511 (setq pos (point))
1375754c
KH
512 (skip-chars-forward "^ \t\n")
513 (setq key (buffer-substring pos (point)))
4ed46869 514 (skip-chars-forward " \t")
1375754c
KH
515 (setq ch (following-char))
516 (if (or (= ch ?#) (= ch ?\n))
08a1bf22 517 ;; This entry contains no translations. Let's ignore it.
1375754c
KH
518 (forward-line 1)
519 (or (string= key prev-key)
08a1bf22 520 (progn
1375754c
KH
521 (if translations
522 (tit-flush-translations prev-key translations))
523 (setq translations nil
524 prev-key key)))
525 (if tit-dictionary
526 (progn
527 (setq pos (point))
528 (skip-chars-forward "^ \t#\n")
529 (setq translations
530 (if translations
531 (concat translations
532 (buffer-substring pos (point)))
533 (buffer-substring pos (point)))))
534 (while (not (eolp))
535 (setq pos (point))
536 (skip-chars-forward "^ \t\n")
537 (setq translations (cons (buffer-substring pos (point))
538 translations))
539 (skip-chars-forward " \t")
540 (setq ch (following-char))
541 (if (= ch ?#) (end-of-line))))
08a1bf22 542 (forward-line 1))))
1375754c
KH
543
544 (if translations
545 (tit-flush-translations prev-key translations))
546 (princ ")\n")))
4ed46869
KH
547
548;;;###autoload
549(defun titdic-convert (filename &optional dirname)
550 "Convert a TIT dictionary of FILENAME into a Quail package.
551Optional argument DIRNAME if specified is the directory name under which
552the generated Quail package is saved."
553 (interactive "FTIT dictionary file: ")
1375754c
KH
554 (with-temp-file (tit-make-quail-package-file-name filename dirname)
555 (set-buffer-file-coding-system 'iso-2022-7bit)
556 (let ((standard-output (current-buffer)))
557 (with-temp-buffer
558 (let ((coding-system-for-read 'no-conversion))
559 (insert-file-contents (expand-file-name filename)))
ecd57ad4 560 (set-buffer-multibyte t)
1375754c
KH
561
562 ;; Decode the buffer contents from the encoding specified by a
563 ;; value of the key "ENCODE:".
564 (if (not (search-forward "\nBEGIN" nil t))
565 (error "TIT dictionary doesn't have body part"))
566 (let ((limit (point))
567 coding-system slot)
568 (goto-char (point-min))
569 (if (re-search-forward "^ENCODE:[ \t]*" limit t)
570 (progn
571 (goto-char (match-end 0))
572 (setq tit-encode (tit-read-key-value)))
573 (setq tit-encode tit-default-encode))
574 (setq slot (assoc tit-encode tit-encode-list))
575 (if (not slot)
576 (error "Invalid ENCODE: value in TIT dictionary"))
577 (setq coding-system (nth 1 slot))
86e4f7c0 578 (message "Decoding with coding system %s..." coding-system)
1375754c
KH
579 (goto-char (point-min))
580 (decode-coding-region (point-min) (point-max) coding-system))
581
582 ;; Set point the starting position of the body part.
583 (goto-char (point-min))
584 (if (not (search-forward "\nBEGIN" nil t))
585 (error "TIT dictionary can't be decoded correctly"))
586
587 ;; Process the header part.
588 (forward-line 1)
589 (narrow-to-region (point-min) (point))
590 (tit-process-header filename)
591 (widen)
592
593 ;; Process the body part. For speed, we turn off multibyte facility.
594 (with-current-buffer standard-output
595 (set-buffer-multibyte nil))
596 (set-buffer-multibyte nil)
597 (tit-process-body)))))
4ed46869
KH
598
599;;;###autoload
44cbfae9 600(defun batch-titdic-convert (&optional force)
4ed46869
KH
601 "Run `titdic-convert' on the files remaining on the command line.
602Use this from the command line, with `-batch';
603it won't work in an interactive Emacs.
604For example, invoke \"emacs -batch -f batch-titdic-convert XXX.tit\" to
605 generate Quail package file \"xxx.el\" from TIT dictionary file \"XXX.tit\".
606To get complete usage, invoke \"emacs -batch -f batch-titdic-convert -h\"."
607 (defvar command-line-args-left) ; Avoid compiler warning.
608 (if (not noninteractive)
609 (error "`batch-titdic-convert' should be used only with -batch"))
610 (if (string= (car command-line-args-left) "-h")
611 (progn
612 (message "To convert XXX.tit and YYY.tit into xxx.el and yyy.el:")
613 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert XXX.tit YYY.tit")
614 (message "To convert XXX.tit into DIR/xxx.el:")
615 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert -dir DIR XXX.tit"))
616 (let (targetdir filename files file)
617 (if (string= (car command-line-args-left) "-dir")
618 (progn
619 (setq command-line-args-left (cdr command-line-args-left))
620 (setq targetdir (car command-line-args-left))
621 (setq command-line-args-left (cdr command-line-args-left))))
622 (while command-line-args-left
623 (setq filename (expand-file-name (car command-line-args-left)))
624 (if (file-directory-p filename)
625 (progn
626 (message "Converting all tit files in the directory %s" filename)
627 (setq files (directory-files filename t "\\.tit$")))
628 (setq files (list filename)))
629 (while files
630 (setq file (expand-file-name (car files)))
44cbfae9
KH
631 (when (or force
632 (file-newer-than-file-p
633 file (tit-make-quail-package-file-name file targetdir)))
634 (message "Converting %s to quail-package..." file)
635 (titdic-convert file targetdir))
4ed46869
KH
636 (setq files (cdr files)))
637 (setq command-line-args-left (cdr command-line-args-left)))
86e4f7c0 638 (message "Byte-compile the created files by:")
4ed46869
KH
639 (message " %% emacs -batch -f batch-byte-compile XXX.el")))
640 (kill-emacs 0))
641
642;;; titdic-cnv.el ends here
5cdaf2a5
KH
643
644;; Local Variables:
645;; coding: iso-2022-7bit
646;; End: