tamago-4.0.5
[elisp/tamago.git] / egg / sj3rpc.el
1 ;;; egg/sj3rpc.el --- SJ3 Support (low level interface) in Egg
2 ;;;                   Input Method Architecture
3
4 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc
5
6 ;; Author: NIIBE Yutaka <gniibe@chroot.org>
7
8 ;; Maintainer: TOMURA Satoru <tomura@etl.go.jp>
9
10 ;; Keywords: mule, multilingual, input method
11
12 ;; This file is part of EGG.
13
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)
17 ;; any later version.
18
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.
23
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.
28
29 ;;; Commentary:
30
31
32 ;;; Code:
33
34 (eval-when-compile
35   (require 'egg-com)
36 ;;  (load-library "egg/sj3")
37   (defmacro sj3-const (c)
38     (cond ((eq c 'OPEN)            1)
39           ((eq c 'CLOSE)           2)
40           ((eq c 'DICADD)         11)
41           ((eq c 'DICDEL)         12)
42           ((eq c 'OPENSTDY)       21)
43           ((eq c 'CLOSESTDY)      22)
44           ((eq c 'STDYSIZE)       23)
45           ((eq c 'LOCK)           31)
46           ((eq c 'UNLOCK)         32)
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))
51           ((eq c 'STDY)           61)
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))
55           ((eq c 'MKDIC)          81)
56           ((eq c 'MKSTDY)         82)
57           ((eq c 'MKDIR)          83)
58           ((eq c 'ACCESS)         84)
59           ((eq c 'WSCH)    '(if (eq 1 sj3-server-version) 91 120))
60           ((eq c 'WNSCH)   '(if (eq 1 sj3-server-version) 92 121))
61           ((eq c 'VERSION)       103)
62           (t (error "No such constant")))))
63
64 ;; XXX
65 (defconst sj3rpc-error-message (vector ))
66
67 (defun sj3rpc-get-error-message (errno)
68   (or (and (>= errno 0)
69            (< errno (length sj3rpc-error-message))
70            (aref sj3rpc-error-message errno))
71       (format "#%d" errno)))
72
73 (defmacro sj3rpc-call-with-environment (e vlist send-expr &rest receive-exprs)
74   (let ((v (append
75             `((proc (sj3env-get-proc ,e)))
76             vlist)))
77     (list
78      'let v
79      (append
80         `(save-excursion
81            (set-buffer (process-buffer proc))
82            (erase-buffer)
83            ,send-expr
84            (process-send-region proc (point-min) (point-max))
85            (goto-char (prog1 (point) (accept-process-output proc))))
86         receive-exprs))))
87
88 (defmacro sj3rpc-server-coding-system ()
89   '(nth (1- sj3-server-version) sj3-server-coding-system-list))
90
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)))
96                            ,coding-system)))
97 \f
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
102                  myhostname username
103                  ;; program name
104                  (format "%d.emacs-egg" (emacs-pid)))
105     (comm-unpack (u) result)
106     (if (= result -2)
107         0
108       result)))
109
110 (defun sj3rpc-close (proc)
111   (comm-call-with-proc proc (result)
112     (comm-format (u) (sj3-const CLOSE))
113     (comm-unpack (u) result)
114     result))
115
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)
121     (if (/= result 0)
122         (- result)                      ; failure
123       (comm-unpack (u) result)
124       result)))
125
126 (defsubst sj3rpc-get-stdy (proc)
127   (let ((n 0)
128         (stdy (make-vector sj3-stdy-size 0)))
129     (while (< n sj3-stdy-size)
130       (comm-unpack (b) r)
131       (aset stdy n r)
132       (setq n (1+ n)))
133     stdy))
134
135 (defun sj3rpc-begin (env yomi)
136   "Begin conversion."
137   (let* ((codesys (sj3rpc-server-coding-system))
138          (yomi-ext (encode-coding-string yomi codesys))
139          (p 0)
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)
144       (if (/= result 0)
145           (- result)                    ; failure
146         (comm-unpack (u) result)        ; skip
147         (while (progn
148                  (comm-unpack (b) len)
149                  (> len 0))
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))
153                                              codesys)
154                 p (+ p len))
155           (let ((bl1 (cons (sj3-make-bunsetsu env
156                                               source converted nil stdy) nil)))
157             (if bl
158                 (setq bl (setcdr bl bl1))
159               (setq bunsetsu-list (setq bl bl1)))))
160         bunsetsu-list))))
161
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)
166     (if (/= result 0)
167         (- result)                      ; failure
168       (comm-unpack (u) result)
169       result)))
170
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)
175     result))
176
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
180                  2048  ; Index length
181                  2048  ; Length
182                  256   ; Number
183                  )
184     (comm-unpack (u) result)
185     result))
186
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)
191     result))
192
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)
197     result))
198
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
202                  2048  ; Number
203                  1     ; Step
204                  2048  ; Length
205                  )
206     (comm-unpack (u) result)
207     result))
208
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)
213     result))
214
215 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
216   (let ((codesys (sj3rpc-server-coding-system))
217         (i 0)
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)
222       (if (/= result 0)
223           (- result)                    ; failure
224         (while (< i n)
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)
229                                            codesys))
230           (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
231           (if bl
232               (setq bl (setcdr bl (cons bunsetsu nil)))
233             (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
234           (setq i (1+ i)))
235         (setq bunsetsu (sj3-make-bunsetsu env yomi yomi nil nil))
236         (setq bl (setcdr bl (cons bunsetsu nil)))
237         (setq bunsetsu
238               (sj3-make-bunsetsu env yomi (japanese-katakana yomi) nil nil))
239         (setq bl (setcdr bl (cons bunsetsu nil)))
240         bunsetsu-list))))
241
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)
248       (if (/= result 0)
249           (- result)                    ; failure
250         (comm-unpack (u) result)
251         (if (= result 0)
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))))))
255
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)
263       (if (/= result 0)
264           (- 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))))))
270
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)
275       (if (/= result 0)
276           (- result)
277         0)))
278
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)
287       (if (/= result 0)
288           (- result)
289         0))))
290
291 ;;; egg/sj3rpc.el ends here.