a7eda4c67cd166f6cde74258dfb5cc3520fdb940
[elisp/egg.git] / egg / sj3rpc.el
1 ;;; egg/sj3.el --- SJ3 Support (low level interface) in Egg
2 ;;;                Input Method Architecture
3
4 ;; Copyright (C) 1997 Mule Project, Powered by Electrotechnical
5 ;; Laboratory, JAPAN.
6 ;; Project Leader: Satoru Tomura <tomura@etl.go.jp>
7
8 ;; Author: NIIBE Yutaka <gniibe@mri.co.jp>
9 ;; Maintainer: NIIBE Yutaka <gniibe@mri.co.jp>
10
11 ;; This file will be part of GNU Emacs (in future).
12
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)
16 ;; any later version.
17
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.
22
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.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 ;; Only support SJ3 version 2.
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)          41)
48           ((eq c 'BEGIN_EUC)     111)
49           ((eq c 'TANCONV)        51)
50           ((eq c 'TANCONV_EUC)   112)
51           ((eq c 'KOUHO)          54)
52           ((eq c 'KOUHO_EUC)     115)
53           ((eq c 'KOUHOSU)        55)
54           ((eq c 'KOUHOSU_EUC)   116)
55           ((eq c 'STDY)           61)
56           ((eq c 'END)            62)
57           ((eq c 'END_EUC)       117)
58           ((eq c 'WREG)           71)
59           ((eq c 'WREG_EUC)      118)
60           ((eq c 'WDEL)           72)
61           ((eq c 'WDEL_EUC)      119)
62           ((eq c 'MKDIC)          81)
63           ((eq c 'MKSTDY)         82)
64           ((eq c 'MKDIR)          83)
65           ((eq c 'ACCESS)         84)
66           ((eq c 'WSCH)           91)
67           ((eq c 'WSCH_EUC)      120)
68           ((eq c 'WNSCH)          92)
69           ((eq c 'WNSCH_EUC)     121)
70           ((eq c 'VERSION)       103)
71           (t (error "No such constant")))))
72
73 ;; XXX
74 (defconst sj3rpc-error-message (vector ))
75
76 (defun sj3rpc-get-error-message (errno)
77   (or (and (>= errno 0)
78            (< errno (length sj3rpc-error-message))
79            (aref sj3rpc-error-message errno))
80       (format "#%d" errno)))
81
82 (defmacro sj3rpc-call-with-environment (e vlist send-expr &rest receive-exprs)
83   (let ((v (append
84             `((proc (sj3env-get-proc ,e)))
85             vlist)))
86     (list
87      'let v
88      (append
89         `(save-excursion
90            (set-buffer (process-buffer proc))
91            (erase-buffer)
92            ,send-expr
93            (process-send-region proc (point-min) (point-max))
94            (goto-char (prog1 (point) (accept-process-output proc))))
95         receive-exprs))))
96 \f
97 (defun sj3rpc-open (proc myhostname username)
98   "Open the session.  Return 0 on success, error code on failure."
99   (comm-call-with-proc proc (result)
100     (comm-format (u u s s s) (sj3-const OPEN) 2 ; Server version
101                  myhostname username
102                  ;; program name
103                  (format "%d.emacs-egg" (emacs-pid)))
104     (comm-unpack (u) result)
105     (if (= result -2)
106         0
107       result)))
108
109 (defun sj3rpc-get-stdy-size (proc)
110   "Return STDYSIZE of SJ3 server.  On failure, return error code."
111   (comm-call-with-proc proc (result)
112     (comm-format (u) (sj3-const STDYSIZE))
113     (comm-unpack (u) result)
114     (if (/= result 0)
115         (- result)                      ; failure
116       (comm-unpack (u) result)
117       result)))
118
119 (defsubst sj3rpc-get-stdy (proc)
120   (let ((n 0)
121         (stdy (make-vector sj3-stdy-size 0)))
122     (while (< n sj3-stdy-size)
123       (comm-unpack (b) r)
124       (aset stdy n r)
125       (setq n (1+ n)))
126     stdy))
127
128 (defun sj3rpc-begin (env yomi)
129   "Begin conversion."
130   (let ((yomi-ext (encode-coding-string yomi 'euc-japan))
131         (p 0)
132         len source converted stdy bunsetsu-list bl)
133     (sj3rpc-call-with-environment env (result)
134       (comm-format (u s) (sj3-const BEGIN_EUC) yomi-ext)
135       (comm-unpack (u) result)
136       (if (/= result 0)
137           (- result)                    ; failure
138         (comm-unpack (u) result)        ; skip
139         (while (progn
140                  (comm-unpack (b) len)
141                  (> len 0))
142           (setq stdy (sj3rpc-get-stdy proc))
143           (comm-unpack (E) converted)
144           (setq source
145                 (decode-coding-string (substring yomi-ext p (+ p len))
146                                       'euc-japan)
147                 p (+ p len))
148           (let ((bl1 (cons (sj3-make-bunsetsu env
149                                               source converted nil stdy) nil)))
150             (if bl
151                 (setq bl (setcdr bl bl1))
152               (setq bunsetsu-list (setq bl bl1)))))
153         bunsetsu-list))))
154
155 (defun sj3rpc-open-dictionary (proc dict-file-name password)
156   (comm-call-with-proc proc (result)
157     (comm-format (u s s) (sj3-const DICADD) dict-file-name password)
158     (comm-unpack (u) result)
159     (if (/= result 0)
160         (- result)                      ; failure
161       (comm-unpack (u) result)
162       result)))
163
164 (defun sj3rpc-close-dictionary (proc dict-no)
165   (comm-call-with-proc proc (result)
166     (comm-format (u u) (sj3-const DICDEL) dict-no)
167     (comm-unpack (u) result)
168     result))
169
170 (defun sj3rpc-make-dictionary (proc dict-name)
171   (comm-call-with-proc proc (result)
172     (comm-format (u s u u u) (sj3-const MKDIC) dict-name
173                  2048  ; Index length
174                  2048  ; Length
175                  256   ; Number
176     (comm-unpack (u) result)
177     result)))
178
179 (defun sj3rpc-open-stdy (proc stdy-name)
180   (comm-call-with-proc proc (result)
181     (comm-format (u s s) (sj3-const OPENSTDY) stdy-name "")
182     (comm-unpack (u) result)
183     result))
184
185 (defun sj3rpc-close-stdy (proc)
186   (comm-call-with-proc proc (result)
187     (comm-format (u) (sj3-const CLOSESTDY))
188     (comm-unpack (u) result)
189     result))
190
191 (defun sj3rpc-make-stdy (proc stdy-name)
192   (comm-call-with-proc proc (result)
193     (comm-format (u) (sj3-const MKSTDY) stdy-name "")
194                  2048  ; Number
195                  1     ; Step
196                  2048  ; Length
197     (comm-unpack (u) result)
198     result))
199
200 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
201   (let ((i 0)
202         stdy converted bunsetsu bl bunsetsu-list cylen rest)
203     (comm-call-with-proc-1 proc (result)
204       (comm-format (u u s) (sj3-const KOUHO_EUC) len yomi-ext)
205       (comm-unpack (u) result)
206       (if (/= result 0)
207           (- result)                    ; failure
208         (while (< i n)
209           (comm-unpack (u) cylen)
210           (setq stdy (sj3rpc-get-stdy proc))
211           (comm-unpack (E) converted)
212           (setq rest (decode-coding-string
213                       (substring yomi-ext cylen) 'euc-japan))
214           (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
215           (if bl
216               (setq bl (setcdr bl (cons bunsetsu nil)))
217             (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
218           (setq i (1+ i)))
219         bunsetsu-list))))
220
221 (defun sj3rpc-get-bunsetsu-candidates (env yomi)
222   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
223          (len (length yomi-ext)))
224     (sj3rpc-call-with-environment env (result)
225       (comm-format (u u s) (sj3-const KOUHOSU_EUC) len yomi-ext)
226       (comm-unpack (u) result)
227       (if (/= result 0)
228           (- result)                    ; failure
229         (comm-unpack (u) result)
230         (if (= result 0)
231             (list (sj3-make-bunsetsu env yomi yomi nil nil)) ; XXX
232           (sj3rpc-get-bunsetsu-candidates-sub proc env
233                                               yomi yomi-ext len result))))))
234
235 (defun sj3rpc-tanbunsetsu-conversion (env yomi)
236   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
237          (len (length yomi-ext)) cylen stdy converted rest)
238     (sj3rpc-call-with-environment env (result)
239       (comm-format (u u s) (sj3-const TANCONV_EUC) len yomi-ext)
240       (comm-unpack (u) result)
241       (if (/= result 0)
242           (- result)
243         (comm-unpack (u) cylen)
244         (setq stdy (sj3rpc-get-stdy proc))
245         (comm-unpack (E) converted)
246         (setq rest (decode-coding-string
247                     (substring yomi-ext cylen) 'euc-japan))
248         (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))))))
249
250 ;;; egg/sj3rpc.el ends here.