1 ;;; egg/sj3rpc.el --- SJ3 Support (low level interface) in Egg
2 ;;; Input Method Architecture
4 ;; Copyright (C) 1997, 1998 Mule Project,
5 ;; Powered by Electrotechnical Laboratory, JAPAN.
6 ;; Project Leader: Satoru Tomura <tomura@etl.go.jp>
8 ;; Author: NIIBE Yutaka <gniibe@mri.co.jp>
9 ;; Maintainer: NIIBE Yutaka <gniibe@mri.co.jp>
11 ;; This file will be part of EGG.
13 ;; EGG is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; EGG is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
32 ;; Only support SJ3 version 2.
37 ;; To get definition of sj3-make-bunsetsu and others
38 (load-library "egg/sj3")
39 (defmacro sj3-const (c)
40 (cond ((eq c 'OPEN) 1)
45 ((eq c 'CLOSESTDY) 22)
50 ((eq c 'BEGIN_EUC) 111)
52 ((eq c 'TANCONV_EUC) 112)
54 ((eq c 'KOUHO_EUC) 115)
56 ((eq c 'KOUHOSU_EUC) 116)
59 ((eq c 'CLSTDY_EUC) 117)
61 ((eq c 'WREG_EUC) 118)
63 ((eq c 'WDEL_EUC) 119)
69 ((eq c 'WSCH_EUC) 120)
71 ((eq c 'WNSCH_EUC) 121)
73 (t (error "No such constant")))))
76 (defconst sj3rpc-error-message
78 "Error #0
\e$B$G$9
\e(B"
81 (defun sj3rpc-get-error-message (errno)
83 (< errno (length sj3rpc-error-message))
84 (aref sj3rpc-error-message errno))
85 (format "#%d" errno)))
87 (defmacro sj3rpc-call-with-environment (e vlist send-expr &rest receive-exprs)
89 `((proc (sj3env-get-proc ,e)))
95 (set-buffer (process-buffer proc))
98 (process-send-region proc (point-min) (point-max))
99 (goto-char (prog1 (point) (accept-process-output proc))))
102 (defun sj3rpc-open (proc myhostname username)
103 "Open the session. Return 0 on success, error code on failure."
104 (comm-call-with-proc proc (result)
105 (comm-format (u u s s s) (sj3-const OPEN) 2 ; Server version
108 (format "%d.emacs-egg" (emacs-pid)))
109 (comm-unpack (u) result)
110 (if (= result -2) ; negated version number
112 -1))) ; Failure (different version)
114 (defun sj3rpc-close (proc)
115 (comm-call-with-proc proc (result)
116 (comm-format (u) (sj3-const CLOSE))
117 (comm-unpack (u) result)
120 (defun sj3rpc-get-stdy-size (proc)
121 "Return STDYSIZE of SJ3 server. On failure, return error code."
122 (comm-call-with-proc proc (result)
123 (comm-format (u) (sj3-const STDYSIZE))
124 (comm-unpack (u) result)
127 (comm-unpack (u) result)
130 (defsubst sj3rpc-get-stdy (proc)
132 (stdy (make-vector sj3-stdy-size 0)))
133 (while (< n sj3-stdy-size)
139 (defun sj3rpc-begin (env yomi)
141 (let ((yomi-ext (encode-coding-string yomi 'euc-japan))
143 len source converted stdy bunsetsu-list bl)
144 (sj3rpc-call-with-environment env (result)
145 (comm-format (u s) (sj3-const BEGIN_EUC) yomi-ext)
146 (comm-unpack (u) result)
149 (comm-unpack (u) result) ; skip
151 (comm-unpack (b) len)
153 (setq stdy (sj3rpc-get-stdy proc))
154 (comm-unpack (E) converted)
156 (decode-coding-string (substring yomi-ext p (+ p len))
159 (let ((bl1 (cons (sj3-make-bunsetsu env
160 source converted nil stdy) nil)))
162 (setq bl (setcdr bl bl1))
163 (setq bunsetsu-list (setq bl bl1)))))
166 (defun sj3rpc-open-dictionary (proc dict-file-name password)
167 (comm-call-with-proc proc (result)
168 (comm-format (u s s) (sj3-const DICADD) dict-file-name password)
169 (comm-unpack (u) result)
172 (comm-unpack (u) result)
175 (defun sj3rpc-close-dictionary (proc dict-no)
176 (comm-call-with-proc proc (result)
177 (comm-format (u u) (sj3-const DICDEL) dict-no)
178 (comm-unpack (u) result)
181 (defun sj3rpc-make-dictionary (proc dict-name)
182 (comm-call-with-proc proc (result)
183 (comm-format (u s u u u) (sj3-const MKDIC) dict-name
188 (comm-unpack (u) result)
191 (defun sj3rpc-open-stdy (proc stdy-name)
192 (comm-call-with-proc proc (result)
193 (comm-format (u s s) (sj3-const OPENSTDY) stdy-name "")
194 (comm-unpack (u) result)
197 (defun sj3rpc-close-stdy (proc)
198 (comm-call-with-proc proc (result)
199 (comm-format (u) (sj3-const CLOSESTDY))
200 (comm-unpack (u) result)
203 (defun sj3rpc-make-stdy (proc stdy-name)
204 (comm-call-with-proc proc (result)
205 (comm-format (u s u u u) (sj3-const MKSTDY) stdy-name
210 (comm-unpack (u) result)
213 (defun sj3rpc-make-directory (proc name)
214 (comm-call-with-proc proc (result)
215 (comm-format (u s) (sj3-const MKDIR) name)
216 (comm-unpack (u) result)
219 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
221 stdy converted bunsetsu bl bunsetsu-list cylen rest)
222 (comm-call-with-proc-1 proc (result)
223 (comm-format (u u s) (sj3-const KOUHO_EUC) len yomi-ext)
224 (comm-unpack (u) result)
228 (comm-unpack (u) cylen)
229 (setq stdy (sj3rpc-get-stdy proc))
230 (comm-unpack (E) converted)
231 (setq rest (decode-coding-string
232 (substring yomi-ext cylen) 'euc-japan))
233 (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
235 (setq bl (setcdr bl (cons bunsetsu nil)))
236 (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
238 (setq bunsetsu (sj3-make-bunsetsu env yomi yomi nil nil))
239 (setq bl (setcdr bl (cons bunsetsu nil)))
241 (sj3-make-bunsetsu env yomi (japanese-katakana yomi) nil nil))
242 (setq bl (setcdr bl (cons bunsetsu nil)))
245 (defun sj3rpc-get-bunsetsu-candidates (env yomi)
246 (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
247 (len (length yomi-ext)))
248 (sj3rpc-call-with-environment env (result)
249 (comm-format (u u s) (sj3-const KOUHOSU_EUC) len yomi-ext)
250 (comm-unpack (u) result)
253 (comm-unpack (u) result)
255 (list (sj3-make-bunsetsu env yomi yomi nil nil)) ; XXX
256 (sj3rpc-get-bunsetsu-candidates-sub proc env
257 yomi yomi-ext len result))))))
259 (defun sj3rpc-tanbunsetsu-conversion (env yomi)
260 (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
261 (len (length yomi-ext)) cylen stdy converted rest)
262 (sj3rpc-call-with-environment env (result)
263 (comm-format (u u s) (sj3-const TANCONV_EUC) len yomi-ext)
264 (comm-unpack (u) result)
267 (comm-unpack (u) cylen)
268 (setq stdy (sj3rpc-get-stdy proc))
269 (comm-unpack (E) converted)
270 (setq rest (decode-coding-string
271 (substring yomi-ext cylen) 'euc-japan))
272 (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))))))
274 (defun sj3rpc-bunsetsu-stdy (env stdy)
275 (sj3rpc-call-with-environment env (result)
276 (comm-format (u v) (sj3-const STDY) stdy)
277 (comm-unpack (u) result)
282 (defun sj3rpc-kugiri-stdy (env yomi1 yomi2 stdy)
283 (let* ((yomi1-ext (encode-coding-string yomi1 'euc-japan))
284 (yomi2-ext (encode-coding-string yomi2 'euc-japan)))
285 (sj3rpc-call-with-environment env (result)
286 (comm-format (u s s v) (sj3-const CLSTDY_EUC) yomi1-ext yomi2-ext stdy)
287 (comm-unpack (u) result)
292 ;;; egg/sj3rpc.el ends here.