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.
34 (defvar sj3-server-version 2
35 "*Major version number of SJ3 server.")
37 (defvar sj3-server-coding-system 'shift_jis
38 "*Coding system used when decoding and encoding of I/O operation with
39 SJ3 server. Valid coding systems are depend on the server spec.")
43 (defmacro sj3-sjis-p ()
44 '(eq 'coding-category-sjis (coding-system-category
45 sj3-server-coding-system)))
46 (defmacro sj3-const (c)
47 (cond ((eq c 'OPEN) 1)
52 ((eq c 'CLOSESTDY) 22)
56 ((eq c 'BEGIN) '(if (sj3-sjis-p) 41 111))
57 ((eq c 'TANCONV) '(if (sj3-sjis-p) 51 112))
58 ((eq c 'KOUHO) '(if (sj3-sjis-p) 54 115))
59 ((eq c 'KOUHOSU) '(if (sj3-sjis-p) 55 116))
61 ((eq c 'CLSTDY) '(if (sj3-sjis-p) 62 117))
62 ((eq c 'WREG) '(if (sj3-sjis-p) 71 118))
63 ((eq c 'WDEL) '(if (sj3-sjis-p) 72 119))
68 ((eq c 'WSCH) '(if (sj3-sjis-p) 91 120))
69 ((eq c 'WNSCH) '(if (sj3-sjis-p) 92 121))
71 (t (error "No such constant")))))
74 (defconst sj3rpc-error-message (vector ))
76 (defun sj3rpc-get-error-message (errno)
78 (< errno (length sj3rpc-error-message))
79 (aref sj3rpc-error-message errno))
80 (format "#%d" errno)))
82 (defmacro sj3rpc-call-with-environment (e vlist send-expr &rest receive-exprs)
84 `((proc (sj3env-get-proc ,e)))
90 (set-buffer (process-buffer proc))
93 (process-send-region proc (point-min) (point-max))
94 (goto-char (prog1 (point) (accept-process-output proc))))
97 (defmacro sj3rpc-unpack-mb-string ()
98 '(let ((start (point)))
99 (while (not (search-forward "\0" nil t))
100 (comm-accept-process-output))
101 (decode-coding-string (buffer-substring start (1- (point)))
102 sj3-server-coding-system)))
104 (defun sj3rpc-open (proc myhostname username)
105 "Open the session. Return 0 on success, error code on failure."
106 (comm-call-with-proc proc (result)
107 (comm-format (u u s s s) (sj3-const OPEN) sj3-server-version
110 (format "%d.emacs-egg" (emacs-pid)))
111 (comm-unpack (i) result)
116 (defun sj3rpc-close (proc)
117 (comm-call-with-proc proc (result)
118 (comm-format (u) (sj3-const CLOSE))
119 (comm-unpack (i) result)
122 (defun sj3rpc-get-stdy-size (proc)
123 "Return STDYSIZE of SJ3 server. On failure, return error code."
124 (comm-call-with-proc proc (result)
125 (comm-format (u) (sj3-const STDYSIZE))
126 (comm-unpack (u) result)
129 (comm-unpack (u) result)
132 (defsubst sj3rpc-get-stdy (proc)
134 (stdy (make-vector sj3-stdy-size 0)))
135 (while (< n sj3-stdy-size)
141 (defun sj3rpc-begin (env yomi)
143 (let ((yomi-ext (encode-coding-string yomi sj3-server-coding-system))
145 len source converted stdy bunsetsu-list bl)
146 (sj3rpc-call-with-environment env (result)
147 (comm-format (u s) (sj3-const BEGIN) yomi-ext)
148 (comm-unpack (u) result)
151 (comm-unpack (u) result) ; skip
153 (comm-unpack (b) len)
155 (setq stdy (sj3rpc-get-stdy proc))
156 (setq converted (sj3rpc-unpack-mb-string))
157 (setq source (decode-coding-string (substring yomi-ext p (+ p len))
158 sj3-server-coding-system)
160 (let ((bl1 (cons (sj3-make-bunsetsu env
161 source converted nil stdy) nil)))
163 (setq bl (setcdr bl bl1))
164 (setq bunsetsu-list (setq bl bl1)))))
167 (defun sj3rpc-open-dictionary (proc dict-file-name password)
168 (comm-call-with-proc proc (result)
169 (comm-format (u s s) (sj3-const DICADD) dict-file-name password)
170 (comm-unpack (u) result)
173 (comm-unpack (u) result)
176 (defun sj3rpc-close-dictionary (proc dict-no)
177 (comm-call-with-proc proc (result)
178 (comm-format (u u) (sj3-const DICDEL) dict-no)
179 (comm-unpack (i) result)
182 (defun sj3rpc-make-dictionary (proc dict-name)
183 (comm-call-with-proc proc (result)
184 (comm-format (u s u u u) (sj3-const MKDIC) dict-name
189 (comm-unpack (i) result)
192 (defun sj3rpc-open-stdy (proc stdy-name)
193 (comm-call-with-proc proc (result)
194 (comm-format (u s s) (sj3-const OPENSTDY) stdy-name "")
195 (comm-unpack (i) result)
198 (defun sj3rpc-close-stdy (proc)
199 (comm-call-with-proc proc (result)
200 (comm-format (u) (sj3-const CLOSESTDY))
201 (comm-unpack (i) result)
204 (defun sj3rpc-make-stdy (proc stdy-name)
205 (comm-call-with-proc proc (result)
206 (comm-format (u s u u u) (sj3-const MKSTDY) stdy-name
211 (comm-unpack (i) result)
214 (defun sj3rpc-make-directory (proc name)
215 (comm-call-with-proc proc (result)
216 (comm-format (u s) (sj3-const MKDIR) name)
217 (comm-unpack (i) result)
220 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
222 stdy converted bunsetsu bl bunsetsu-list cylen rest)
223 (comm-call-with-proc-1 proc (result)
224 (comm-format (u u s) (sj3-const KOUHO) len yomi-ext)
225 (comm-unpack (u) result)
229 (comm-unpack (u) cylen)
230 (setq stdy (sj3rpc-get-stdy proc))
231 (setq converted (sj3rpc-unpack-mb-string))
232 (setq rest (decode-coding-string (substring yomi-ext cylen)
233 sj3-server-coding-system))
234 (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
236 (setq bl (setcdr bl (cons bunsetsu nil)))
237 (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
239 (setq bunsetsu (sj3-make-bunsetsu env yomi yomi nil nil))
240 (setq bl (setcdr bl (cons bunsetsu nil)))
242 (sj3-make-bunsetsu env yomi (japanese-katakana yomi) nil nil))
243 (setq bl (setcdr bl (cons bunsetsu nil)))
246 (defun sj3rpc-get-bunsetsu-candidates (env yomi)
247 (let* ((yomi-ext (encode-coding-string yomi sj3-server-coding-system))
248 (len (length yomi-ext)))
249 (sj3rpc-call-with-environment env (result)
250 (comm-format (u u s) (sj3-const KOUHOSU) len yomi-ext)
251 (comm-unpack (u) result)
254 (comm-unpack (u) result)
256 (list (sj3-make-bunsetsu env yomi yomi nil nil)) ; XXX
257 (sj3rpc-get-bunsetsu-candidates-sub proc env
258 yomi yomi-ext len result))))))
260 (defun sj3rpc-tanbunsetsu-conversion (env yomi)
261 (let* ((yomi-ext (encode-coding-string yomi sj3-server-coding-system))
262 (len (length yomi-ext)) cylen stdy converted rest)
263 (sj3rpc-call-with-environment env (result)
264 (comm-format (u u s) (sj3-const TANCONV) len yomi-ext)
265 (comm-unpack (u) result)
268 (comm-unpack (u) cylen)
269 (setq stdy (sj3rpc-get-stdy proc))
270 (setq converted (sj3rpc-unpack-mb-string))
271 (setq rest (decode-coding-string (substring yomi-ext cylen)
272 sj3-server-coding-system))
273 (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))))))
275 (defun sj3rpc-bunsetsu-stdy (env stdy)
276 (sj3rpc-call-with-environment env (result)
277 (comm-format (u v) (sj3-const STDY) stdy (length stdy))
278 (comm-unpack (u) result)
281 (defun sj3rpc-kugiri-stdy (env yomi1 yomi2 stdy)
282 (sj3rpc-call-with-environment env (result)
283 (comm-format (u s s v) (sj3-const CLSTDY)
284 (encode-coding-string yomi1 sj3-server-coding-system)
285 (encode-coding-string yomi2 sj3-server-coding-system)
287 (comm-unpack (u) result)
290 (defun sj3rpc-add-word (env dictionary yomi kanji hinshi)
291 "Register a word KANJI into DICTIONARY with a pronunciation YOMI and
292 a part of speech HINSHI. Where DICTIONARY should be an integer."
293 (sj3rpc-call-with-environment env ()
294 (comm-format (u u s s u) (sj3-const WREG) dictionary
295 (encode-coding-string yomi sj3-server-coding-system)
296 (encode-coding-string kanji sj3-server-coding-system)
298 (comm-unpack (u) result)
301 ;;; egg/sj3rpc.el ends here.