egg-980315.
[elisp/egg.git] / egg / sj3rpc.el
1 ;;; egg/sj3rpc.el --- SJ3 Support (low level interface) in Egg
2 ;;;                   Input Method Architecture
3
4 ;; Copyright (C) 1997, 1998 Mule Project,
5 ;; Powered by Electrotechnical 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 EGG.
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 'CLSTDY)         62)
57           ((eq c 'CLSTDY_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-close (proc)
110   (comm-call-with-proc proc (result)
111     (comm-format (u) (sj3-const CLOSE))
112     (comm-unpack (u) result)
113     result))
114
115 (defun sj3rpc-get-stdy-size (proc)
116   "Return STDYSIZE of SJ3 server.  On failure, return error code."
117   (comm-call-with-proc proc (result)
118     (comm-format (u) (sj3-const STDYSIZE))
119     (comm-unpack (u) result)
120     (if (/= result 0)
121         (- result)                      ; failure
122       (comm-unpack (u) result)
123       result)))
124
125 (defsubst sj3rpc-get-stdy (proc)
126   (let ((n 0)
127         (stdy (make-vector sj3-stdy-size 0)))
128     (while (< n sj3-stdy-size)
129       (comm-unpack (b) r)
130       (aset stdy n r)
131       (setq n (1+ n)))
132     stdy))
133
134 (defun sj3rpc-begin (env yomi)
135   "Begin conversion."
136   (let ((yomi-ext (encode-coding-string yomi 'euc-japan))
137         (p 0)
138         len source converted stdy bunsetsu-list bl)
139     (sj3rpc-call-with-environment env (result)
140       (comm-format (u s) (sj3-const BEGIN_EUC) yomi-ext)
141       (comm-unpack (u) result)
142       (if (/= result 0)
143           (- result)                    ; failure
144         (comm-unpack (u) result)        ; skip
145         (while (progn
146                  (comm-unpack (b) len)
147                  (> len 0))
148           (setq stdy (sj3rpc-get-stdy proc))
149           (comm-unpack (E) converted)
150           (setq source
151                 (decode-coding-string (substring yomi-ext p (+ p len))
152                                       'euc-japan)
153                 p (+ p len))
154           (let ((bl1 (cons (sj3-make-bunsetsu env
155                                               source converted nil stdy) nil)))
156             (if bl
157                 (setq bl (setcdr bl bl1))
158               (setq bunsetsu-list (setq bl bl1)))))
159         bunsetsu-list))))
160
161 (defun sj3rpc-open-dictionary (proc dict-file-name password)
162   (comm-call-with-proc proc (result)
163     (comm-format (u s s) (sj3-const DICADD) dict-file-name password)
164     (comm-unpack (u) result)
165     (if (/= result 0)
166         (- result)                      ; failure
167       (comm-unpack (u) result)
168       result)))
169
170 (defun sj3rpc-close-dictionary (proc dict-no)
171   (comm-call-with-proc proc (result)
172     (comm-format (u u) (sj3-const DICDEL) dict-no)
173     (comm-unpack (u) result)
174     result))
175
176 (defun sj3rpc-make-dictionary (proc dict-name)
177   (comm-call-with-proc proc (result)
178     (comm-format (u s u u u) (sj3-const MKDIC) dict-name
179                  2048  ; Index length
180                  2048  ; Length
181                  256   ; Number
182                  )
183     (comm-unpack (u) result)
184     result))
185
186 (defun sj3rpc-open-stdy (proc stdy-name)
187   (comm-call-with-proc proc (result)
188     (comm-format (u s s) (sj3-const OPENSTDY) stdy-name "")
189     (comm-unpack (u) result)
190     result))
191
192 (defun sj3rpc-close-stdy (proc)
193   (comm-call-with-proc proc (result)
194     (comm-format (u) (sj3-const CLOSESTDY))
195     (comm-unpack (u) result)
196     result))
197
198 (defun sj3rpc-make-stdy (proc stdy-name)
199   (comm-call-with-proc proc (result)
200     (comm-format (u s u u u) (sj3-const MKSTDY) stdy-name
201                  2048  ; Number
202                  1     ; Step
203                  2048  ; Length
204                  )
205     (comm-unpack (u) result)
206     result))
207
208 (defun sj3rpc-make-directory (proc name)
209   (comm-call-with-proc proc (result)
210     (comm-format (u s) (sj3-const MKDIR) name)
211     (comm-unpack (u) result)
212     result))
213
214 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
215   (let ((i 0)
216         stdy converted bunsetsu bl bunsetsu-list cylen rest)
217     (comm-call-with-proc-1 proc (result)
218       (comm-format (u u s) (sj3-const KOUHO_EUC) len yomi-ext)
219       (comm-unpack (u) result)
220       (if (/= result 0)
221           (- result)                    ; failure
222         (while (< i n)
223           (comm-unpack (u) cylen)
224           (setq stdy (sj3rpc-get-stdy proc))
225           (comm-unpack (E) converted)
226           (setq rest (decode-coding-string
227                       (substring yomi-ext cylen) 'euc-japan))
228           (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
229           (if bl
230               (setq bl (setcdr bl (cons bunsetsu nil)))
231             (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
232           (setq i (1+ i)))
233         (setq bunsetsu (sj3-make-bunsetsu env yomi yomi nil nil))
234         (setq bl (setcdr bl (cons bunsetsu nil)))
235         (setq bunsetsu
236               (sj3-make-bunsetsu env yomi (japanese-katakana yomi) nil nil))
237         (setq bl (setcdr bl (cons bunsetsu nil)))
238         bunsetsu-list))))
239
240 (defun sj3rpc-get-bunsetsu-candidates (env yomi)
241   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
242          (len (length yomi-ext)))
243     (sj3rpc-call-with-environment env (result)
244       (comm-format (u u s) (sj3-const KOUHOSU_EUC) len yomi-ext)
245       (comm-unpack (u) result)
246       (if (/= result 0)
247           (- result)                    ; failure
248         (comm-unpack (u) result)
249         (if (= result 0)
250             (list (sj3-make-bunsetsu env yomi yomi nil nil)) ; XXX
251           (sj3rpc-get-bunsetsu-candidates-sub proc env
252                                               yomi yomi-ext len result))))))
253
254 (defun sj3rpc-tanbunsetsu-conversion (env yomi)
255   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
256          (len (length yomi-ext)) cylen stdy converted rest)
257     (sj3rpc-call-with-environment env (result)
258       (comm-format (u u s) (sj3-const TANCONV_EUC) len yomi-ext)
259       (comm-unpack (u) result)
260       (if (/= result 0)
261           (- result)
262         (comm-unpack (u) cylen)
263         (setq stdy (sj3rpc-get-stdy proc))
264         (comm-unpack (E) converted)
265         (setq rest (decode-coding-string
266                     (substring yomi-ext cylen) 'euc-japan))
267         (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))))))
268
269 (defun sj3rpc-bunsetsu-stdy (env stdy)
270   (sj3rpc-call-with-environment env (result)
271      (comm-format (u v) (sj3-const STDY) stdy)
272      (comm-unpack (u) result)
273       (if (/= result 0)
274           (- result)
275         0)))
276
277 (defun sj3rpc-kugiri-stdy (env yomi1 yomi2 stdy)
278   (let* ((yomi1-ext (encode-coding-string yomi1 'euc-japan))
279          (yomi2-ext (encode-coding-string yomi2 'euc-japan)))
280     (sj3rpc-call-with-environment env (result)
281       (comm-format (u s s v) (sj3-const CLSTDY_EUC) yomi1-ext yomi2-ext stdy)
282       (comm-unpack (u) result)
283       (if (/= result 0)
284           (- result)
285         0))))
286
287 ;;; egg/sj3rpc.el ends here.