1 ;;; titdic-cnv.el --- convert TIT dictionary to Quail package
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
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 TIT format dictionary (of cxterm) to quail-package.
29 ;; Usage (within Emacs):
30 ;; M-x titdic-convert<CR>TIT-FILE-NAME<CR>
31 ;; Usage (from shell):
32 ;; % emacs -batch -l titdic-convert -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 ;; TIT 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" coding-system-euc-china "Chinese-GB")
63 ("BIG5" coding-system-big5 "Chinese-BIG5")
64 ("JIS" coding-system-euc-japan "Japanese")
65 ("KS" coding-system-euc-korea "Korean")))
67 ;; Return a value of the key in the current line.
68 (defsubst tit-read-key-value ()
69 (if (looking-at "[^ \t\n]+")
70 (car (read-from-string (concat "\"" (match-string 0) "\"")))))
72 ;; Return an appropriate quail-package filename from FILENAME (TIT
73 ;; dictionary filename). For instance, ".../ZOZY.tit" -> "zozy.el".
74 (defun tit-make-quail-package-name (filename &optional dirname)
76 (concat (downcase (file-name-nondirectory (substring filename 0 -4))) ".el")
79 ;; This value is t if we are processing phrase dictionary.
80 (defvar tit-phrase nil)
81 (defvar tit-encode nil)
82 (defvar tit-default-encode "GB")
84 ;; Generate elements of KEY-BINDINGS arg for `quail-define-package' so
85 ;; that each characters in KEYS invokes FUNCTION-SYMBOL.
86 (defun tit-generate-key-bindings (keys function-symbol)
87 (let ((len (length keys))
91 (setq key (aref keys i))
94 (if (eq (lookup-key quail-translation-keymap (char-to-string key))
95 'quail-execute-non-quail-command)
96 (insert (format "(\"\\C-%c\" . %s)\n"
97 (+ key ?@) function-symbol)))
99 (insert (format "(\"%c\" . %s)\n" key function-symbol))
100 (insert (format "(\"\\C-?\" . %s)\n" function-symbol))))
103 ;; Analyze header part of TIT dictionary and generate an appropriate
104 ;; `quail-define-package' function call.
105 (defun tit-process-header (filename)
106 (message "Processing header part...")
107 (goto-char (point-min))
109 (let (;; TIT keywords and the corresponding default values.
113 (tit-backspace "\010\177")
114 (tit-deleteall "\015\025")
118 ;; At first, collect information from the header.
121 (let ((ch (following-char)))
122 (cond ((= ch ?C) ; COMMENT
123 (cond ((looking-at "COMMENT")
124 (let ((pos (match-end 0)))
126 (while (re-search-backward "[\"\\]" pos t)
130 (setq tit-comments (cons (buffer-substring pos (point))
132 ((= ch ?M) ; MULTICHOICE, MOVERIGHT, MOVELEFT
133 (cond ((looking-at "MULTICHOICE:[ \t]*")
134 (goto-char (match-end 0))
135 (setq tit-multichoice (looking-at "YES")))
136 ((looking-at "MOVERIGHT:[ \t]*")
137 (goto-char (match-end 0))
138 (setq tit-moveright (tit-read-key-value)))
139 ((looking-at "MOVELEFT:[ \t]*")
140 (goto-char (match-end 0))
141 (setq tit-moveleft (tit-read-key-value)))))
143 (cond ((looking-at "PROMPT:[ \t]*")
144 (goto-char (match-end 0))
145 (setq tit-prompt (tit-read-key-value)))))
146 ((= ch ?B) ; BACKSPACE, BEGINDICTIONARY,
148 (cond ((looking-at "BACKSPACE:[ \t]*")
149 (goto-char (match-end 0))
150 (setq tit-backspace (tit-read-key-value)))
151 ((looking-at "BEGINDICTIONARY")
152 (setq tit-phrase nil))
153 ((looking-at "BEGINPHRASE")
154 (setq tit-phrase t))))
155 ((= ch ?K) ; KEYPROMPT
156 (cond ((looking-at "KEYPROMPT(\\(.*\\)):[ \t]*")
157 (let ((key-char (match-string 1)))
158 (goto-char (match-end 0))
160 (cons (cons key-char (tit-read-key-value))
161 tit-keyprompt))))))))
164 ;; Then, generate header part of the Quail package.
165 (goto-char (point-min))
166 (insert ";; Quail package `"
167 (substring (file-name-nondirectory buffer-file-name) 0 -3)
168 "' generated by the command `titdic-convert'\n"
169 ";;\tDate: " (current-time-string) "\n"
170 ";;\tOriginal TIT dictionary file: "
171 (file-name-nondirectory filename)
174 ";; Do byte-compile this file again after any modification.\n\n"
175 ";;; Start of the header of original TIT dictionary.\n\n")
177 (goto-char (point-max))
179 ";;; End of the header of original TIT dictionary.\n\n"
181 "(require 'quail)\n\n")
183 (insert "(quail-define-package ")
184 ;; Args NAME, LANGUAGE, TITLE
188 (substring (file-name-nondirectory buffer-file-name) 0 -3))
189 "\" \"" (nth 2 (assoc tit-encode tit-encode-list))
191 (if (string-match "[:
\e$A!K
\e$(0!(!J
\e(B]+\\([^:
\e$A!K
\e$(0!(!K
\e(B]+\\)" tit-prompt)
192 (substring tit-prompt (match-beginning 1) (match-end 1))
202 (insert (format "(%d . \"%s\")\n"
203 (string-to-char (car (car tit-keyprompt)))
204 (cdr (car tit-keyprompt))))
205 (setq tit-keyprompt (cdr tit-keyprompt)))
212 (insert "\"" tit-prompt "\n")
213 (let ((l (nreverse tit-comments)))
215 (insert (format "%s\n" (car l)))
221 (tit-generate-key-bindings tit-backspace 'quail-delete-last-char)
222 (tit-generate-key-bindings tit-deleteall 'quail-abort-translation)
223 (tit-generate-key-bindings tit-moveright 'quail-next-translation)
224 (tit-generate-key-bindings tit-moveleft 'quail-prev-translation)
229 ;; Args FORGET-TRANSLATION, DETERMINISTIC, KBD-TRANSLATE, SHOW-LAYOUT.
230 ;; The remaining args are all nil.
232 (if tit-multichoice " nil" " t")
233 (if tit-keyprompt " t t)\n\n" " nil nil)\n\n")))
235 ;; Return the position of end of the header.
238 ;; Convert body part of TIT dictionary into `quail-define-rules'
240 (defun tit-process-body ()
241 (message "Formatting translation rules...")
242 (let ((enable-multibyte-characters nil)
245 (insert "(quail-define-rules\n")
247 (if (or (= (following-char) ?#) (= (following-char) ?\n))
251 (skip-chars-forward "^ \t")
253 (concat (regexp-quote (buffer-substring pos (point))) "[ \t]+"))
255 (while (re-search-backward "[\\\"]" pos t)
259 (skip-chars-forward " \t")
261 ;; Now point is at the start of translations. Remember it in
262 ;; POS and combine lines of the same key sequence while
263 ;; deleting trailing white spaces and comments (start with
264 ;; '#'). POS doesn't has to be a marker because we never
265 ;; modify region before POS.
267 (if (looking-at "[^ \t]*\\([ \t]*#.*\\)")
268 (delete-region (match-beginning 1) (match-end 1)))
269 (while (and (= (forward-line 1) 0)
271 (let ((p (match-end 0)))
272 (skip-chars-backward " \t\n")
273 (delete-region (point) p)
274 (if tit-phrase (insert " "))
275 (if (looking-at "[^ \t]*\\([ \t]*#.*\\)")
276 (delete-region (match-beginning 1) (match-end 1)))
279 ;; Modify the current line to meet the syntax of Quail package.
283 ;; PHRASE1 PHRASE2 ... => ["PHRASE1" "PHRASE2" ...]
285 (skip-chars-forward "^ \t\n")
290 (skip-chars-forward "^ \t\n"))
292 ;; TRANSLATIONS => "TRANSLATIONS"
300 (defun titdic-convert (filename &optional dirname)
301 "Convert a TIT dictionary of FILENAME into a Quail package.
302 Optional argument DIRNAME if specified is the directory name under which
303 the generated Quail package is saved."
304 (interactive "FTIT dictionary file: ")
305 (let ((buf (get-buffer-create "*tit-work*")))
310 (let ((coding-system-for-read 'no-conversion))
311 (insert-file-contents (expand-file-name filename)))
312 (set-visited-file-name (tit-make-quail-package-name filename dirname) t)
313 (set-buffer-file-coding-system 'coding-system-iso-2022-7)
315 ;; Decode the buffer contents from the encoding specified by a
316 ;; value of the key "ENCODE:".
319 (if (search-forward "\nBEGIN" nil t)
320 (let ((limit (point))
323 (if (re-search-forward "^ENCODE:[ \t]*" limit t)
325 (goto-char (match-end 0))
326 (setq tit-encode (tit-read-key-value)))
327 (setq tit-encode tit-default-encode))
328 (setq slot (assoc tit-encode tit-encode-list))
330 (setq coding-system (nth 1 slot))
331 (error "Invalid ENCODE: value in TIT dictionary")))
332 (error "TIT dictionary doesn't have body part")))
333 (message "Decoding %s..." coding-system)
335 (decode-coding-region 1 (point-max) coding-system))
337 ;; Set point the starting position of the body part.
339 (if (search-forward "\nBEGIN" nil t)
341 (error "TIT dictionary can't be decoded correctly"))
343 ;; Now process the header and body parts.
347 (narrow-to-region 1 (point))
348 (tit-process-header filename))))
352 ;; Save the Quail package file.
356 ;; Show the Quail package just generated.
357 (switch-to-buffer buf)
359 (message "Save this buffer after you make any modification"))))
362 (defun batch-titdic-convert ()
363 "Run `titdic-convert' on the files remaining on the command line.
364 Use this from the command line, with `-batch';
365 it won't work in an interactive Emacs.
366 For example, invoke \"emacs -batch -f batch-titdic-convert XXX.tit\" to
367 generate Quail package file \"xxx.el\" from TIT dictionary file \"XXX.tit\".
368 To get complete usage, invoke \"emacs -batch -f batch-titdic-convert -h\"."
369 (defvar command-line-args-left) ; Avoid compiler warning.
370 (if (not noninteractive)
371 (error "`batch-titdic-convert' should be used only with -batch"))
372 (if (string= (car command-line-args-left) "-h")
374 (message "To convert XXX.tit and YYY.tit into xxx.el and yyy.el:")
375 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert XXX.tit YYY.tit")
376 (message "To convert XXX.tit into DIR/xxx.el:")
377 (message " %% emacs -batch -l titdic-cnv -f batch-titdic-convert -dir DIR XXX.tit"))
378 (let (targetdir filename files file)
379 (if (string= (car command-line-args-left) "-dir")
381 (setq command-line-args-left (cdr command-line-args-left))
382 (setq targetdir (car command-line-args-left))
383 (setq command-line-args-left (cdr command-line-args-left))))
384 (while command-line-args-left
385 (setq filename (expand-file-name (car command-line-args-left)))
386 (if (file-directory-p filename)
388 (message "Converting all tit files in the directory %s" filename)
389 (setq files (directory-files filename t "\\.tit$")))
390 (setq files (list filename)))
392 (setq file (expand-file-name (car files)))
393 (if (file-newer-than-file-p
394 file (tit-make-quail-package-name file targetdir))
396 (message "Converting %s to quail-package..." file)
397 (titdic-convert file targetdir)))
398 (setq files (cdr files)))
399 (setq command-line-args-left (cdr command-line-args-left)))
400 (message "Do byte-compile the created files by:")
401 (message " %% emacs -batch -f batch-byte-compile XXX.el")))
404 ;;; titdic-cnv.el ends here