1 ;;; egg/sj3rpc.el --- SJ3 Support (low level interface) in Egg
2 ;;; Input Method Architecture
4 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc
6 ;; Author: NIIBE Yutaka <gniibe@chroot.org>
8 ;; Maintainer: TOMURA Satoru <tomura@etl.go.jp>
10 ;; Keywords: mule, multilingual, input method
12 ;; This file is part of EGG.
14 ;; EGG is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; EGG is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
36 ;; (load-library "egg/sj3")
37 (defmacro sj3-const (c)
38 (cond ((eq c 'OPEN) 1)
43 ((eq c 'CLOSESTDY) 22)
47 ((eq c 'BEGIN) '(if (eq 1 sj3-server-version) 41 111))
48 ((eq c 'TANCONV) '(if (eq 1 sj3-server-version) 51 112))
49 ((eq c 'KOUHO) '(if (eq 1 sj3-server-version) 54 115))
50 ((eq c 'KOUHOSU) '(if (eq 1 sj3-server-version) 55 116))
52 ((eq c 'CLSTDY) '(if (eq 1 sj3-server-version) 62 117))
53 ((eq c 'WREG) '(if (eq 1 sj3-server-version) 71 118))
54 ((eq c 'WDEL) '(if (eq 1 sj3-server-version) 72 119))
59 ((eq c 'WSCH) '(if (eq 1 sj3-server-version) 91 120))
60 ((eq c 'WNSCH) '(if (eq 1 sj3-server-version) 92 121))
62 (t (error "No such constant")))))
65 (defconst sj3rpc-error-message (vector ))
67 (defun sj3rpc-get-error-message (errno)
69 (< errno (length sj3rpc-error-message))
70 (aref sj3rpc-error-message errno))
71 (format "#%d" errno)))
73 (defmacro sj3rpc-call-with-environment (e vlist send-expr &rest receive-exprs)
75 `((proc (sj3env-get-proc ,e)))
81 (set-buffer (process-buffer proc))
84 (process-send-region proc (point-min) (point-max))
85 (goto-char (prog1 (point) (accept-process-output proc))))
88 (defmacro sj3rpc-server-coding-system ()
89 '(nth (1- sj3-server-version) sj3-server-coding-system-list))
91 (defmacro sj3rpc-unpack-mb-string (coding-system)
92 `(let ((start (point)))
93 (while (not (search-forward "\0" nil t))
94 (comm-accept-process-output))
95 (decode-coding-string (buffer-substring start (1- (point)))
98 (defun sj3rpc-open (proc myhostname username)
99 "Open the session. Return 0 on success, error code on failure."
100 (comm-call-with-proc proc (result)
101 (comm-format (u u s s s) (sj3-const OPEN) sj3-server-version
104 (format "%d.emacs-egg" (emacs-pid)))
105 (comm-unpack (u) result)
110 (defun sj3rpc-close (proc)
111 (comm-call-with-proc proc (result)
112 (comm-format (u) (sj3-const CLOSE))
113 (comm-unpack (u) result)
116 (defun sj3rpc-get-stdy-size (proc)
117 "Return STDYSIZE of SJ3 server. On failure, return error code."
118 (comm-call-with-proc proc (result)
119 (comm-format (u) (sj3-const STDYSIZE))
120 (comm-unpack (u) result)
123 (comm-unpack (u) result)
126 (defsubst sj3rpc-get-stdy (proc)
128 (stdy (make-vector sj3-stdy-size 0)))
129 (while (< n sj3-stdy-size)
135 (defun sj3rpc-begin (env yomi)
137 (let* ((codesys (sj3rpc-server-coding-system))
138 (yomi-ext (encode-coding-string yomi codesys))
140 len source converted stdy bunsetsu-list bl)
141 (sj3rpc-call-with-environment env (result)
142 (comm-format (u s) (sj3-const BEGIN) yomi-ext)
143 (comm-unpack (u) result)
146 (comm-unpack (u) result) ; skip
148 (comm-unpack (b) len)
150 (setq stdy (sj3rpc-get-stdy proc))
151 (setq converted (sj3rpc-unpack-mb-string codesys))
152 (setq source (decode-coding-string (substring yomi-ext p (+ p len))
155 (let ((bl1 (cons (sj3-make-bunsetsu env
156 source converted nil stdy) nil)))
158 (setq bl (setcdr bl bl1))
159 (setq bunsetsu-list (setq bl bl1)))))
162 (defun sj3rpc-open-dictionary (proc dict-file-name password)
163 (comm-call-with-proc proc (result)
164 (comm-format (u s s) (sj3-const DICADD) dict-file-name password)
165 (comm-unpack (u) result)
168 (comm-unpack (u) result)
171 (defun sj3rpc-close-dictionary (proc dict-no)
172 (comm-call-with-proc proc (result)
173 (comm-format (u u) (sj3-const DICDEL) dict-no)
174 (comm-unpack (u) result)
177 (defun sj3rpc-make-dictionary (proc dict-name)
178 (comm-call-with-proc proc (result)
179 (comm-format (u s u u u) (sj3-const MKDIC) dict-name
184 (comm-unpack (u) result)
187 (defun sj3rpc-open-stdy (proc stdy-name)
188 (comm-call-with-proc proc (result)
189 (comm-format (u s s) (sj3-const OPENSTDY) stdy-name "")
190 (comm-unpack (u) result)
193 (defun sj3rpc-close-stdy (proc)
194 (comm-call-with-proc proc (result)
195 (comm-format (u) (sj3-const CLOSESTDY))
196 (comm-unpack (u) result)
199 (defun sj3rpc-make-stdy (proc stdy-name)
200 (comm-call-with-proc proc (result)
201 (comm-format (u s u u u) (sj3-const MKSTDY) stdy-name
206 (comm-unpack (u) result)
209 (defun sj3rpc-make-directory (proc name)
210 (comm-call-with-proc proc (result)
211 (comm-format (u s) (sj3-const MKDIR) name)
212 (comm-unpack (u) result)
215 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
216 (let ((codesys (sj3rpc-server-coding-system))
218 stdy converted bunsetsu bl bunsetsu-list cylen rest)
219 (comm-call-with-proc-1 proc (result)
220 (comm-format (u u s) (sj3-const KOUHO) len yomi-ext)
221 (comm-unpack (u) result)
225 (comm-unpack (u) cylen)
226 (setq stdy (sj3rpc-get-stdy proc))
227 (setq converted (sj3rpc-unpack-mb-string codesys))
228 (setq rest (decode-coding-string (substring yomi-ext cylen)
230 (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
232 (setq bl (setcdr bl (cons bunsetsu nil)))
233 (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
235 (setq bunsetsu (sj3-make-bunsetsu env yomi yomi nil nil))
236 (setq bl (setcdr bl (cons bunsetsu nil)))
238 (sj3-make-bunsetsu env yomi (japanese-katakana yomi) nil nil))
239 (setq bl (setcdr bl (cons bunsetsu nil)))
242 (defun sj3rpc-get-bunsetsu-candidates (env yomi)
243 (let* ((yomi-ext (encode-coding-string yomi (sj3rpc-server-coding-system)))
244 (len (length yomi-ext)))
245 (sj3rpc-call-with-environment env (result)
246 (comm-format (u u s) (sj3-const KOUHOSU) len yomi-ext)
247 (comm-unpack (u) result)
250 (comm-unpack (u) result)
252 (list (sj3-make-bunsetsu env yomi yomi nil nil)) ; XXX
253 (sj3rpc-get-bunsetsu-candidates-sub proc env
254 yomi yomi-ext len result))))))
256 (defun sj3rpc-tanbunsetsu-conversion (env yomi)
257 (let* ((codesys (sj3rpc-server-coding-system))
258 (yomi-ext (encode-coding-string yomi codesys))
259 (len (length yomi-ext)) cylen stdy converted rest)
260 (sj3rpc-call-with-environment env (result)
261 (comm-format (u u s) (sj3-const TANCONV) len yomi-ext)
262 (comm-unpack (u) result)
265 (comm-unpack (u) cylen)
266 (setq stdy (sj3rpc-get-stdy proc))
267 (setq converted (sj3rpc-unpack-mb-string codesys))
268 (setq rest (decode-coding-string (substring yomi-ext cylen) codesys))
269 (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))))))
271 (defun sj3rpc-bunsetsu-stdy (env stdy)
272 (sj3rpc-call-with-environment env (result)
273 (comm-format (u v) (sj3-const STDY) stdy (length stdy))
274 (comm-unpack (u) result)
279 (defun sj3rpc-kugiri-stdy (env yomi1 yomi2 stdy)
280 (let* ((codesys (sj3rpc-server-coding-system))
281 (yomi1-ext (encode-coding-string yomi1 codesys))
282 (yomi2-ext (encode-coding-string yomi2 codesys)))
283 (sj3rpc-call-with-environment env (result)
284 (comm-format (u s s v) (sj3-const CLSTDY)
285 yomi1-ext yomi2-ext stdy (length stdy))
286 (comm-unpack (u) result)
291 ;;; egg/sj3rpc.el ends here.