(chise-tex-encode-region-for-jis): New function.
[chise/omega.git] / chise2otf / elisp / chise-tex.el
1 ;;; chise-tex.el --- Coding-system based chise2otf like tool
2
3 ;; Copyright (C) 2004 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: OTF package, pTeX, CHISE, Multiscript, Multilingual
7
8 ;; This file is a part of Omega/CHISE.
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (defvar chise-tex-coded-charset-expression-alist
28   '((=ucs@gb    "UCSgb"         4 X)
29     (=ucs@jis   "UCSjis"        4 X)
30     (=gt-pj-1   "GTpj1"         4 X)
31     (=gt-pj-2   "GTpj2"         4 X)
32     (=gt-pj-3   "GTpj3"         4 X)
33     (=gt-pj-4   "GTpj4"         4 X)
34     (=gt-pj-5   "GTpj5"         4 X)
35     (=gt-pj-6   "GTpj6"         4 X)
36     (=gt-pj-7   "GTpj7"         4 X)
37     (=gt-pj-8   "GTpj8"         4 X)
38     (=gt-pj-9   "GTpj9"         4 X)
39     (=gt-pj-10  "GTpj10"        4 X)
40     (=gt-pj-11  "GTpj11"        4 X)
41     (=ucs@ks    "UCSks"         4 X)
42     (=ucs@cns   "UCScns"        4 X)))
43
44 (defun chise-tex-encode-region-for-gb (start end)
45   (interactive "r")
46   (save-excursion
47     (save-restriction
48       (narrow-to-region start end)
49       (goto-char start)
50       (let (chr ret rest spec)
51         (while (and (skip-chars-forward "\x00-\xFF")
52                     (not (eobp)))
53           (setq chr (char-after))
54           (cond ((memq chr '(?\e$(O#@\e(B))
55                  (delete-char)
56                  (insert (format "\\UCSjis{%04X}"
57                                  (encode-char chr '=ucs@jis)))
58                  )
59                 ((and (setq ret (encode-char chr '=jis-x0208-1983))
60                       (< ret #x3021))
61                  (forward-char))
62                 ;; ((setq ret (encode-char chr '=jis-x0208-1990))
63                 ;;  (delete-char)
64                 ;;  (insert (decode-char '=jis-x0208-1983 ret)))
65                 ((catch 'tag
66                    (setq rest chise-tex-coded-charset-expression-alist)
67                    (while (setq spec (car rest))
68                      (if (setq ret (encode-char chr (car spec)))
69                          (throw 'tag ret))
70                      (setq rest (cdr rest))))
71                  (delete-char)
72                  ;; (if (eq (char-before) ?\e$B!T\e(B)
73                  ;;     (insert " "))
74                  (insert (format (format "\\%s{%%0%d%s}"
75                                          (nth 1 spec)
76                                          (nth 2 spec)
77                                          (nth 3 spec))
78                                  ret)))
79                 (t
80                  (forward-char))))))))
81
82 (defun chise-tex-encode-region-for-jis (start end)
83   (interactive "r")
84   (save-excursion
85     (save-restriction
86       (narrow-to-region start end)
87       (goto-char start)
88       (let (chr ret rest spec)
89         (while (and (skip-chars-forward "\x00-\xFF")
90                     (not (eobp)))
91           (setq chr (char-after))
92           (cond ((encode-char chr '=jis-x0208-1983)
93                  (forward-char))
94                 ;; ((setq ret (encode-char chr '=jis-x0208-1990))
95                 ;;  (delete-char)
96                 ;;  (insert (decode-char '=jis-x0208-1983 ret)))
97                 ((catch 'tag
98                    (setq rest chise-tex-coded-charset-expression-alist)
99                    (while (setq spec (car rest))
100                      (if (setq ret (encode-char chr (car spec)))
101                          (throw 'tag ret))
102                      (setq rest (cdr rest))))
103                  (delete-char)
104                  ;; (if (eq (char-before) ?\e$B!T\e(B)
105                  ;;     (insert " "))
106                  (insert (format (format "\\%s{%%0%d%s}"
107                                          (nth 1 spec)
108                                          (nth 2 spec)
109                                          (nth 3 spec))
110                                  ret)))
111                 (t
112                  (forward-char))))))))
113
114 (defun chise-tex-decode-region (start end)
115   (interactive "r")
116   (save-excursion
117     (save-restriction
118       (narrow-to-region start end)
119       (goto-char start)
120       (let (macro code ret ms me)
121         (while (re-search-forward "\\\\\\([a-zA-Z0-9]+\\){\\([0-9A-Fa-f]+\\)}"
122                                   nil t)
123           (setq macro (match-string 1)
124                 code (match-string 2)
125                 ms (match-beginning 0)
126                 me (match-end 0))
127           (if (and (catch 'tag
128                      (setq rest chise-tex-coded-charset-expression-alist)
129                      (while (setq spec (car rest))
130                        (if (string= (nth 1 spec) macro)
131                            (throw 'tag spec))
132                        (setq rest (cdr rest))))
133                    (setq ret (decode-char (car spec)
134                                           (string-to-int
135                                            code
136                                            (if (eq (nth 3 spec) 'X)
137                                                16)))))
138               (progn
139                 (delete-region (match-beginning 0)(match-end 0))
140                 (insert ret))
141             (goto-char me)))))))
142
143 (make-coding-system
144  'iso-2022-jp-tex-gb 'iso2022
145  "ISO-2022-JP with TeX representation for GB fonts."
146  '(charset-g0 ascii
147    short t
148    seven t
149    input-charset-conversion ((latin-jisx0201 ascii)
150                              (japanese-jisx0208-1978 japanese-jisx0208))
151    pre-write-conversion chise-tex-encode-region-for-gb
152    post-read-conversion chise-tex-decode-region
153    mnemonic "pTeX(GB)/7bit"
154    ))
155
156 (make-coding-system
157  'iso-2022-jp-tex-jis 'iso2022
158  "ISO-2022-JP with TeX representation for JIS fonts."
159  '(charset-g0 ascii
160    short t
161    seven t
162    input-charset-conversion ((latin-jisx0201 ascii)
163                              (japanese-jisx0208-1978 japanese-jisx0208))
164    pre-write-conversion chise-tex-encode-region-for-jis
165    post-read-conversion chise-tex-decode-region
166    mnemonic "pTeX(JIS)/7bit"
167    ))
168
169
170 ;;; @ End.
171 ;;;
172
173 (provide 'chise-tex)
174
175 ;;; chise-tex.el ends here