]> code.delx.au - gnu-emacs/blob - lisp/international/titdic-cnv.el
(quail-cxterm-package-ext-info): Fix typos in doc strings.
[gnu-emacs] / lisp / international / titdic-cnv.el
1 ;;; titdic-cnv.el --- convert cxterm dictionary (TIT format) to Quail package
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
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
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.
24
25 ;;; Comments:
26
27 ;; Convert cxterm dictionary (of TIT format) to quail-package.
28 ;;
29 ;; Usage (within Emacs):
30 ;; M-x titdic-convert<CR>CXTERM-DICTIONARY-NAME<CR>
31 ;; Usage (from shell):
32 ;; % emacs -batch -l titdic-cnv -f batch-titdic-convert\
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
40 ;; Cxterm dictionary file (*.tit) is a line-oriented text (English,
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
59 ;; List of values of key "ENCODE:" and the corresponding Emacs
60 ;; coding-system and language environment name.
61 (defvar tit-encode-list
62 '(("GB" euc-china "Chinese-GB")
63 ("BIG5" cn-big5 "Chinese-BIG5")
64 ("JIS" euc-japan "Japanese")
65 ("KS" euc-kr "Korean")))
66
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
78 (defvar quail-cxterm-package-ext-info
79 '(("chinese-4corner" "\e$(0(?-F\e(B")
80 ("chinese-array30" "\e$(0#R#O\e(B")
81 ("chinese-ccdospy" "\e$AKuF4\e(B"
82 "Pinyin base input method for Chinese charset GB2312 \(`chinese-gb2312').
83
84 Pinyin is the standared roman transliteration method for Chinese.
85 For the detail of Pinyin system, see the documentation of the input
86 method `chinese-py'.
87
88 This input method works almost the same way as `chinese-py'. The
89 difference 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
92 For 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
99 For double-width GB2312 characters correponding to ASCII, use the
100 input method `chinese-qj'.")
101
102 ("chinese-ctlau" "\e$AAuTA\e(B")
103
104 ("chinese-ctlaub" "\e$(0N,Gn\e(B")
105
106 ("chinese-ecdict" "\e$(05CKH\e(B"
107 "In this input method, you enter a Chinese (Big5) charactere or word
108 by 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
117 Zhuyin is a kind of phonetic symbol. One to three Zhuyin symbols
118 compose one Chinese character.
119
120 In this input method, you enter a Chinese character by first typing
121 keys corresponding to Zhuyin symbols (see the above table) followed by
122 SPC, 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,
123 4:\e$(0(+Vy\e(B).
124
125 \\<quail-translation-docstring>")
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
139 This input method works almost the same way as `chinese-py' (which
140 see).
141
142 This input method supports only Han characters. The more convenient
143 method is `chinese-py-punct-b5', which is the combination of this
144 method and `chinese-punct-b5' and which supports both Han characters
145 and punctuation/symbols.
146
147 For double-width Big5 characters corresponding to ASCII, use the input
148 method `chinese-qj-b5'.
149
150 The input method `chinese-py' and `chinese-tonepy' are also Pinyin
151 based, but for the character set GB2312 (`chinese-gb2312').")
152
153 ("chinese-py" "\e$AF4\e(BG"
154 "Pinyin base input method for Chinese charset GB2312
155 \(`chinese-gb2312').
156
157 Pinyin is the standared roman transliteration method for Chinese.
158 Pinyin uses a sequence of Latin alphabetic characters for each Chinese
159 character. The sequence is made by the combination of the initials
160 \(the beginning sounds) and finals \(the ending sounds).
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
169 With this input method, you enter a Chinese character by first
170 entering its pinyin spelling.
171
172 \\<quail-translation-docstring>
173
174 For instance, to input \e$ADc\e(B, you type \"n i C-n 3\". The first \"n i\"
175 is a Pinyin, \"C-n\" selects the next group of candidates (each group
176 contains at most 10 characters), \"3\" select the third character in
177 that group.
178
179 This input method supports only Han characters. The related input
180 method `chinese-py-punct' is the combination of this method and
181 `chinese-punct'; it supports both Han characters and punctuation
182 characters.
183
184 For double-width GB2312 characters corresponding to ASCII, use the
185 input method `chinese-qj'.
186
187 The correct Pinyin system specifies tones by diacritical marks, but
188 this input method doesn't use them, which results in easy (you don't
189 have to know the exact tones), but verbose (many characters are assigned
190 to the same key sequence) input. You may also want to try the input
191 method `chinese-tonepy' with which you must specify tones by digits
192 \(1..5).")
193
194 ("chinese-qj-b5" "\e$(0)A\e(BB")
195
196 ("chinese-qj" "\e$AH+\e(BG")
197
198 ("chinese-sw" "\e$AJWN2\e(B"
199 "Radical base input method for Chinese charset GB2312 (`chinese-gb2312').
200
201 In this input method, you enter a Chinese character by typing two
202 keys. The first key corresponds to the first (\e$AJW\e(B) radical, the second
203 key corresponds to the last (\e$AN2\e(B) radical. The correspondence of keys
204 and radicals is as below:
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
213 \<quail-translation-docstring>")
214
215 ("chinese-tonepy" "\e$A5wF4\e(B"
216 "Pinyin base input method for Chinese charset GB2312 (`chinese-gb2312').
217
218 Pinyin is the standared roman transliteration method for Chinese.
219 For the details of Pinyin system, see the documentation of the input
220 method `chinese-py'.
221
222 This input method works almost the same way as `chinese-py'. The
223 difference is that you must type 1..5 after each Pinyin spelling to
224 specify 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
226 \<quail-translation-docstring>
227
228 For instance, to input \e$ADc\e(B, you type \"n i 3 3\", the first \"n i\" is
229 a Pinyin, the next \"3\" specifies tone, and the last \"3\" selects
230 the third character from the candidate list.
231
232 For double-width GB2312 characters correponding to ASCII, use the
233 input method `chinese-qj'.")
234
235 ("chinese-ziranma" "\e$AK+F4\e(B"
236 "Pinyin base input method for Chinese GB2312 characters (`chinese-gb2312').
237
238 Pinyin is the standard roman transliteration method for Chinese.
239 For the details of Pinyin system, see the documentation of the input
240 method `chinese-py'.
241
242 unlike the standard spelling of Pinyin, in this input method all
243 initials and finals are assigned to single keys (see the above table).
244 For instance, the initial \"ch\" is assigned to the key `i', the final
245 \"iu\" is assigned to the key `q', and tones 1, 2, 3, 4, and \e$AGaIy\e(B are
246 assigned to the keys `q', `w', `e', `r', `t' respectively.
247
248 \<quail-translation-docstring>
249
250 To input one-letter words, you type 4 keys, the first two for the
251 Pinyin of the letter, next one for tone, and the last one is always a
252 quote ('). For instance, \"vsq'1\" input \e$AVP\e(B. Exceptions are these
253 letters. You can input them just by typing a single key.
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
260 To input two-letter words, you have two ways. One way is to type 4
261 keys, two for the first Pinyin, two for the second Pinyin. For
262 instance, \"vsgo\" inputs \e$AVP9z\e(B. Another way is to type 3 keys: 2
263 initials of two letters, and quote ('). For instance, \"vg'\" also
264 inputs \e$AVP9z\e(B.
265
266 To input three-letter words, you type 4 keys: initials of three
267 letters, 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).
269
270 To input words of more than three letters, you type 4 keys, initials
271 of the first three letters and the last letter. For instance,
272 \"bjdt\" inputs \e$A11>)5gJSL(\e(B.
273
274 To input symbols and punctuations, type `/' followed by one of `a' to
275 `z', then select one of the candidates.
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
283 Zhuyin is a kind of a phonetic symbol. One to three Zhuyin symbols
284 compose a Chinese character.
285
286 In this input method, you enter a Chinese character by first typing
287 keys corresponding to Zhuyin symbols (see the above table) followed by
288 SPC, 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,
289 7:\e$(0M=Vy\e(B).
290
291 \<quail-translation-docstring>")))
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
299 ;; dictionary filename). For instance, ".../ZOZY.tit" -> "ZOZY.el".
300 (defun tit-make-quail-package-file-name (filename &optional dirname)
301 (expand-file-name
302 (concat (file-name-nondirectory (substring filename 0 -4)) ".el")
303 dirname))
304
305 ;; This value is nil if we are processing phrase dictionary.
306 (defconst tit-dictionary t)
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)
315 (first t)
316 key)
317 (while (< i len)
318 (or first (princ "\n "))
319 (setq key (aref keys i))
320 (if (if (< key ?\ )
321 (eq (lookup-key quail-translation-keymap
322 (char-to-string key))
323 'quail-execute-non-quail-command)
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)))
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
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.
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))
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")
362 (princ ";; Byte-compile this file again after any modification.\n\n")
363 (princ ";;; Start of the header of original TIT dictionary.\n\n")
364
365 (while (not (eobp))
366 (let ((ch (following-char))
367 (pos (point)))
368 (cond ((= ch ?C) ; COMMENT
369 (cond ((looking-at "COMMENT")
370 (let ((pos (match-end 0)))
371 (end-of-line)
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))
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)))))))
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")
401 (setq tit-dictionary t))
402 ((looking-at "BEGINPHRASE")
403 (setq tit-dictionary nil))))
404 ((= ch ?K) ; KEYPROMPT
405 (cond ((looking-at "KEYPROMPT(\\(.*\\)):[ \t]*")
406 (let ((key-char (match-string 1)))
407 (goto-char (match-end 0))
408 (if (string-match "\\\\[0-9]+" key-char)
409 (setq key-char
410 (car (read-from-string (format "\"%s\""
411 key-char)))))
412 (setq tit-keyprompt
413 (cons (cons key-char (tit-read-key-value))
414 tit-keyprompt)))))))
415 (end-of-line)
416 (princ ";; ")
417 (princ (buffer-substring pos (point)))
418 (princ "\n")
419 (forward-line 1)))
420
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
426 (let ((title (nth 1 (assoc package quail-cxterm-package-ext-info))))
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"))
437
438 ;; Arg GUIDANCE
439 (if tit-keyprompt
440 (progn
441 (princ " '(")
442 (while tit-keyprompt
443 (princ " ")
444 (princ (format "(%d . \"%s\")\n"
445 (string-to-char (car (car tit-keyprompt)))
446 (cdr (car tit-keyprompt))))
447 (setq tit-keyprompt (cdr tit-keyprompt)))
448 (princ ")"))
449 (princ " t\n"))
450
451 ;; Arg DOCSTRING
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))
462
463 ;; Arg KEY-BINDINGS
464 (princ " '(")
465 (tit-generate-key-bindings tit-backspace 'quail-delete-last-char)
466 (princ "\n ")
467 (tit-generate-key-bindings tit-deleteall 'quail-abort-translation)
468 (princ "\n ")
469 (tit-generate-key-bindings tit-moveright 'quail-next-translation)
470 (princ "\n ")
471 (tit-generate-key-bindings tit-moveleft 'quail-prev-translation)
472 (princ ")\n")
473
474 ;; Args FORGET-TRANSLATION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT.
475 ;; The remaining args are all nil.
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"))
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...")
502 (let* ((template (list nil nil))
503 (second (cdr template))
504 (prev-key "")
505 ch key translations pos)
506 (princ "(quail-define-rules\n")
507 (while (null (eobp))
508 (setq ch (following-char))
509 (if (or (= ch ?#) (= ch ?\n))
510 (forward-line 1)
511 (setq pos (point))
512 (skip-chars-forward "^ \t\n")
513 (setq key (buffer-substring pos (point)))
514 (skip-chars-forward " \t")
515 (setq ch (following-char))
516 (if (or (= ch ?#) (= ch ?\n))
517 ;; This entry contains no translations. Let's ignore it.
518 (forward-line 1)
519 (or (string= key prev-key)
520 (progn
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))))
542 (forward-line 1))))
543
544 (if translations
545 (tit-flush-translations prev-key translations))
546 (princ ")\n")))
547
548 ;;;###autoload
549 (defun titdic-convert (filename &optional dirname)
550 "Convert a TIT dictionary of FILENAME into a Quail package.
551 Optional argument DIRNAME if specified is the directory name under which
552 the generated Quail package is saved."
553 (interactive "FTIT dictionary file: ")
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)))
560 (set-buffer-multibyte t)
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))
578 (message "Decoding with coding system %s..." coding-system)
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)))))
598
599 ;;;###autoload
600 (defun batch-titdic-convert (&optional force)
601 "Run `titdic-convert' on the files remaining on the command line.
602 Use this from the command line, with `-batch';
603 it won't work in an interactive Emacs.
604 For example, invoke \"emacs -batch -f batch-titdic-convert XXX.tit\" to
605 generate Quail package file \"xxx.el\" from TIT dictionary file \"XXX.tit\".
606 To 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)))
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))
636 (setq files (cdr files)))
637 (setq command-line-args-left (cdr command-line-args-left)))
638 (message "Byte-compile the created files by:")
639 (message " %% emacs -batch -f batch-byte-compile XXX.el")))
640 (kill-emacs 0))
641
642 ;;; titdic-cnv.el ends here