1 ;;; titdic-cnv.el --- convert cxterm dictionary (TIT format) to Quail package
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: Quail, TIT, cxterm
8 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; Convert cxterm dictionary (of TIT format) to quail-package.
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] ...
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.
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.
46 ;; Each line in the header part has two fields, KEY and VALUE. These
47 ;; fields are separated by one or more white characters.
49 ;; Each line in the body part has two fields, KEYSEQ and TRANSLATIONS.
50 ;; These fields are separated by one or more white characters.
52 ;; See the manual page of `tit2cit' of cxterm distribution for more
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")))
67 ;; List of package names and the corresponding titles.
68 (defvar quail-cxterm-package-title-alist
69 '(("chinese-4corner" .
"\e$(0(?-F\e(B")
70 ("chinese-array30" .
"\e$(0#R#O\e(B")
71 ("chinese-ccdospy" .
"\e$AKuF4\e(B")
72 ("chinese-ctlau" .
"\e$AAuTA\e(B")
73 ("chinese-ctlaub" .
"\e$(0N,Gn\e(B")
74 ("chinese-ecdict" .
"\e$(05CKH\e(B")
75 ("chinese-etzy" .
"\e$(06/0D\e(B")
76 ("chinese-punct-b5" .
"\e$(0O:\e(BB")
77 ("chinese-punct" .
"\e$A1j\e(BG")
78 ("chinese-py-b5" .
"\e$(03<\e(BB")
79 ("chinese-py" .
"\e$AF4\e(BG")
80 ("chinese-qj-b5" .
"\e$(0)A\e(BB")
81 ("chinese-qj" .
"\e$AH+\e(BG")
82 ("chinese-sw" .
"\e$AJWN2\e(B")
83 ("chinese-tonepy" .
"\e$A5wF4\e(B")
84 ("chinese-ziranma" .
"\e$AK+F4\e(B")
85 ("chinese-zozy" .
"\e$(0I\0D\e(B")))
87 ;; Return a value of the key in the current line.
88 (defsubst tit-read-key-value
()
89 (if (looking-at "[^ \t\n]+")
90 (car (read-from-string (concat "\"" (match-string 0) "\"")))))
92 ;; Return an appropriate quail-package filename from FILENAME (TIT
93 ;; dictionary filename). For instance, ".../ZOZY.tit" -> "ZOZY.el".
94 (defun tit-make-quail-package-file-name (filename &optional dirname
)
96 (concat (file-name-nondirectory (substring filename
0 -
4)) ".el")
99 ;; This value is t if we are processing phrase dictionary.
100 (defvar tit-phrase nil
)
101 (defvar tit-encode nil
)
102 (defvar tit-default-encode
"GB")
104 ;; Generate elements of KEY-BINDINGS arg for `quail-define-package' so
105 ;; that each characters in KEYS invokes FUNCTION-SYMBOL.
106 (defun tit-generate-key-bindings (keys function-symbol
)
107 (let ((len (length keys
))
111 (setq key
(aref keys i
))
114 (if (eq (lookup-key quail-translation-keymap
(char-to-string key
))
115 'quail-execute-non-quail-command
)
116 (insert (format "(\"\\C-%c\" . %s)\n"
117 (+ key ?
@) function-symbol
)))
119 (insert (format "(\"%c\" . %s)\n" key function-symbol
))
120 (insert (format "(\"\\C-?\" . %s)\n" function-symbol
))))
123 ;; Analyze header part of TIT dictionary and generate an appropriate
124 ;; `quail-define-package' function call.
125 (defun tit-process-header (filename)
126 (message "Processing header part...")
127 (goto-char (point-min))
129 (let (;; TIT keywords and the corresponding default values.
133 (tit-backspace "\010\177")
134 (tit-deleteall "\015\025")
138 ;; At first, collect information from the header.
141 (let ((ch (following-char)))
142 (cond ((= ch ?C
) ; COMMENT
143 (cond ((looking-at "COMMENT")
144 (let ((pos (match-end 0)))
146 (while (re-search-backward "[\"\\]" pos t
)
150 (setq tit-comments
(cons (buffer-substring pos
(point))
152 ((= ch ?M
) ; MULTICHOICE, MOVERIGHT, MOVELEFT
153 (cond ((looking-at "MULTICHOICE:[ \t]*")
154 (goto-char (match-end 0))
155 (setq tit-multichoice
(looking-at "YES")))
156 ((looking-at "MOVERIGHT:[ \t]*")
157 (goto-char (match-end 0))
158 (setq tit-moveright
(tit-read-key-value)))
159 ((looking-at "MOVELEFT:[ \t]*")
160 (goto-char (match-end 0))
161 (setq tit-moveleft
(tit-read-key-value)))))
163 (cond ((looking-at "PROMPT:[ \t]*")
164 (goto-char (match-end 0))
165 (setq tit-prompt
(tit-read-key-value)))))
166 ((= ch ?B
) ; BACKSPACE, BEGINDICTIONARY,
168 (cond ((looking-at "BACKSPACE:[ \t]*")
169 (goto-char (match-end 0))
170 (setq tit-backspace
(tit-read-key-value)))
171 ((looking-at "BEGINDICTIONARY")
172 (setq tit-phrase nil
))
173 ((looking-at "BEGINPHRASE")
174 (setq tit-phrase t
))))
175 ((= ch ?K
) ; KEYPROMPT
176 (cond ((looking-at "KEYPROMPT(\\(.*\\)):[ \t]*")
177 (let ((key-char (match-string 1)))
178 (goto-char (match-end 0))
180 (cons (cons key-char
(tit-read-key-value))
181 tit-keyprompt
))))))))
184 ;; Then, generate header part of the Quail package.
185 (goto-char (point-min))
189 (substring (downcase (file-name-nondirectory buffer-file-name
))
191 (insert ";; Quail package `"
193 "' generated by the command `titdic-convert'\n"
194 ";;\tDate: " (current-time-string) "\n"
195 ";;\tOriginal TIT dictionary file: "
196 (file-name-nondirectory filename
)
199 ";; Do byte-compile this file again after any modification.\n\n"
200 ";;; Start of the header of original TIT dictionary.\n\n")
202 (goto-char (point-max))
204 ";;; End of the header of original TIT dictionary.\n\n"
206 "(require 'quail)\n\n")
208 (insert "(quail-define-package ")
209 ;; Args NAME, LANGUAGE, TITLE
210 (let ((title (cdr (assoc package quail-cxterm-package-title-alist
))))
214 "\" \"" (nth 2 (assoc tit-encode tit-encode-list
))
217 (if (string-match "[:\e$A!K\e$(0!(!J\e(B]+\\([^:\e$A!K\e$(0!(!K\e(B]+\\)" tit-prompt
)
218 (substring tit-prompt
(match-beginning 1) (match-end 1))
229 (insert (format "(%d . \"%s\")\n"
230 (string-to-char (car (car tit-keyprompt
)))
231 (cdr (car tit-keyprompt
))))
232 (setq tit-keyprompt
(cdr tit-keyprompt
)))
239 (insert "\"" tit-prompt
"\n")
240 (let ((l (nreverse tit-comments
)))
242 (insert (format "%s\n" (car l
)))
248 (tit-generate-key-bindings tit-backspace
'quail-delete-last-char
)
249 (tit-generate-key-bindings tit-deleteall
'quail-abort-translation
)
250 (tit-generate-key-bindings tit-moveright
'quail-next-translation
)
251 (tit-generate-key-bindings tit-moveleft
'quail-prev-translation
)
256 ;; Args FORGET-TRANSLATION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT.
257 ;; The remaining args are all nil.
259 (if tit-multichoice
" nil" " t")
260 (if tit-keyprompt
" t t)\n\n" " nil nil)\n\n")))
262 ;; Return the position of end of the header.
265 ;; Convert body part of TIT dictionary into `quail-define-rules'
267 (defun tit-process-body ()
268 (message "Formatting translation rules...")
269 (let ((enable-multibyte-characters nil
)
272 (insert "(quail-define-rules\n")
274 (if (or (= (following-char) ?
#) (= (following-char) ?
\n))
278 (skip-chars-forward "^ \t")
280 (concat (regexp-quote (buffer-substring pos
(point))) "[ \t]+"))
282 (while (re-search-backward "[\\\"]" pos t
)
286 (skip-chars-forward " \t")
288 ;; Now point is at the start of translations. Remember it in
289 ;; POS and combine lines of the same key sequence while
290 ;; deleting trailing white spaces and comments (start with
291 ;; '#'). POS doesn't has to be a marker because we never
292 ;; modify region before POS.
294 (if (looking-at "[^ \t]*\\([ \t]*#.*\\)")
295 (delete-region (match-beginning 1) (match-end 1)))
296 (while (and (= (forward-line 1) 0)
298 (let ((p (match-end 0)))
299 (skip-chars-backward " \t\n")
300 (delete-region (point) p
)
301 (if tit-phrase
(insert " "))
302 (if (looking-at "[^ \t]*\\([ \t]*#.*\\)")
303 (delete-region (match-beginning 1) (match-end 1)))
306 ;; Modify the current line to meet the syntax of Quail package.
310 ;; PHRASE1 PHRASE2 ... => ["PHRASE1" "PHRASE2" ...]
312 (skip-chars-forward "^ \t\n")
317 (skip-chars-forward "^ \t\n"))
319 ;; TRANSLATIONS => "TRANSLATIONS"
327 (defun titdic-convert (filename &optional dirname
)
328 "Convert a TIT dictionary of FILENAME into a Quail package.
329 Optional argument DIRNAME if specified is the directory name under which
330 the generated Quail package is saved."
331 (interactive "FTIT dictionary file: ")
332 (let ((buf (get-buffer-create "*tit-work*")))
337 (let ((coding-system-for-read 'no-conversion
))
338 (insert-file-contents (expand-file-name filename
)))
339 (set-visited-file-name
340 (tit-make-quail-package-file-name filename dirname
) t
)
341 (set-buffer-file-coding-system 'iso-2022-7bit
)
343 ;; Decode the buffer contents from the encoding specified by a
344 ;; value of the key "ENCODE:".
347 (if (search-forward "\nBEGIN" nil t
)
348 (let ((limit (point))
351 (if (re-search-forward "^ENCODE:[ \t]*" limit t
)
353 (goto-char (match-end 0))
354 (setq tit-encode
(tit-read-key-value)))
355 (setq tit-encode tit-default-encode
))
356 (setq slot
(assoc tit-encode tit-encode-list
))
358 (setq coding-system
(nth 1 slot
))
359 (error "Invalid ENCODE: value in TIT dictionary")))
360 (error "TIT dictionary doesn't have body part")))
361 (message "Decoding %s..." coding-system
)
363 (decode-coding-region 1 (point-max) coding-system
))
365 ;; Set point the starting position of the body part.
367 (if (search-forward "\nBEGIN" nil t
)
369 (error "TIT dictionary can't be decoded correctly"))
371 ;; Now process the header and body parts.
375 (narrow-to-region 1 (point))
376 (tit-process-header filename
))))
380 ;; Save the Quail package file.
384 ;; Show the Quail package just generated.
385 (switch-to-buffer buf
)
387 (message "Save this buffer after you make any modification"))))
390 (defun batch-titdic-convert ()
391 "Run `titdic-convert' on the files remaining on the command line.
392 Use this from the command line, with `-batch';
393 it won't work in an interactive Emacs.
394 For example, invoke \"emacs -batch -f batch-titdic-convert XXX.tit\" to
395 generate Quail package file \"xxx.el\" from TIT dictionary file \"XXX.tit\".
396 To get complete usage, invoke \"emacs -batch -f batch-titdic-convert -h\"."
397 (defvar command-line-args-left
) ; Avoid compiler warning.
398 (if (not noninteractive
)
399 (error "`batch-titdic-convert' should be used only with -batch"))
400 (if (string= (car command-line-args-left
) "-h")
402 (message "To convert XXX.tit and YYY.tit into xxx.el and yyy.el:")
403 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert XXX.tit YYY.tit")
404 (message "To convert XXX.tit into DIR/xxx.el:")
405 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert -dir DIR XXX.tit"))
406 (let (targetdir filename files file
)
407 (if (string= (car command-line-args-left
) "-dir")
409 (setq command-line-args-left
(cdr command-line-args-left
))
410 (setq targetdir
(car command-line-args-left
))
411 (setq command-line-args-left
(cdr command-line-args-left
))))
412 (while command-line-args-left
413 (setq filename
(expand-file-name (car command-line-args-left
)))
414 (if (file-directory-p filename
)
416 (message "Converting all tit files in the directory %s" filename
)
417 (setq files
(directory-files filename t
"\\.tit$")))
418 (setq files
(list filename
)))
420 (setq file
(expand-file-name (car files
)))
421 (if (file-newer-than-file-p
422 file
(tit-make-quail-package-file-name file targetdir
))
424 (message "Converting %s to quail-package..." file
)
425 (titdic-convert file targetdir
)))
426 (setq files
(cdr files
)))
427 (setq command-line-args-left
(cdr command-line-args-left
)))
428 (message "Do byte-compile the created files by:")
429 (message " %% emacs -batch -f batch-byte-compile XXX.el")))
432 ;;; titdic-cnv.el ends here