f3ad8b2733a04651d9bd9976c77c7b6ace044839
[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
35
36 ;; Only support SJ3 version 2.
37
38 (eval-when-compile
39   (require 'egg-com)
40 ;;  (load-library "egg/sj3")
41   (defmacro sj3-const (c)
42     (cond ((eq c 'OPEN)            1)
43           ((eq c 'CLOSE)           2)
44           ((eq c 'DICADD)         11)
45           ((eq c 'DICDEL)         12)
46           ((eq c 'OPENSTDY)       21)
47           ((eq c 'CLOSESTDY)      22)
48           ((eq c 'STDYSIZE)       23)
49           ((eq c 'LOCK)           31)
50           ((eq c 'UNLOCK)         32)
51           ((eq c 'BEGIN)          41)
52           ((eq c 'BEGIN_EUC)     111)
53           ((eq c 'TANCONV)        51)
54           ((eq c 'TANCONV_EUC)   112)
55           ((eq c 'KOUHO)          54)
56           ((eq c 'KOUHO_EUC)     115)
57           ((eq c 'KOUHOSU)        55)
58           ((eq c 'KOUHOSU_EUC)   116)
59           ((eq c 'STDY)           61)
60           ((eq c 'CLSTDY)         62)
61           ((eq c 'CLSTDY_EUC)    117)
62           ((eq c 'WREG)           71)
63           ((eq c 'WREG_EUC)      118)
64           ((eq c 'WDEL)           72)
65           ((eq c 'WDEL_EUC)      119)
66           ((eq c 'MKDIC)          81)
67           ((eq c 'MKSTDY)         82)
68           ((eq c 'MKDIR)          83)
69           ((eq c 'ACCESS)         84)
70           ((eq c 'WSCH)           91)
71           ((eq c 'WSCH_EUC)      120)
72           ((eq c 'WNSCH)          92)
73           ((eq c 'WNSCH_EUC)     121)
74           ((eq c 'VERSION)       103)
75           (t (error "No such constant")))))
76
77 ;; XXX
78 (defconst sj3rpc-error-message (vector ))
79
80 (defun sj3rpc-get-error-message (errno)
81   (or (and (>= errno 0)
82            (< errno (length sj3rpc-error-message))
83            (aref sj3rpc-error-message errno))
84       (format "#%d" errno)))
85
86 (defmacro sj3rpc-call-with-environment (e vlist send-expr &rest receive-exprs)
87   (let ((v (append
88             `((proc (sj3env-get-proc ,e)))
89             vlist)))
90     (list
91      'let v
92      (append
93         `(save-excursion
94            (set-buffer (process-buffer proc))
95            (erase-buffer)
96            ,send-expr
97            (process-send-region proc (point-min) (point-max))
98            (goto-char (prog1 (point) (accept-process-output proc))))
99         receive-exprs))))
100 \f
101 (defun sj3rpc-open (proc myhostname username)
102   "Open the session.  Return 0 on success, error code on failure."
103   (comm-call-with-proc proc (result)
104     (comm-format (u u s s s) (sj3-const OPEN) 2 ; Server version
105                  myhostname username
106                  ;; program name
107                  (format "%d.emacs-egg" (emacs-pid)))
108     (comm-unpack (u) result)
109     (if (= result -2)
110         0
111       result)))
112
113 (defun sj3rpc-close (proc)
114   (comm-call-with-proc proc (result)
115     (comm-format (u) (sj3-const CLOSE))
116     (comm-unpack (u) result)
117     result))
118
119 (defun sj3rpc-get-stdy-size (proc)
120   "Return STDYSIZE of SJ3 server.  On failure, return error code."
121   (comm-call-with-proc proc (result)
122     (comm-format (u) (sj3-const STDYSIZE))
123     (comm-unpack (u) result)
124     (if (/= result 0)
125         (- result)                      ; failure
126       (comm-unpack (u) result)
127       result)))
128
129 (defsubst sj3rpc-get-stdy (proc)
130   (let ((n 0)
131         (stdy (make-vector sj3-stdy-size 0)))
132     (while (< n sj3-stdy-size)
133       (comm-unpack (b) r)
134       (aset stdy n r)
135       (setq n (1+ n)))
136     stdy))
137
138 (defun sj3rpc-begin (env yomi)
139   "Begin conversion."
140   (let ((yomi-ext (encode-coding-string yomi 'euc-japan))
141         (p 0)
142         len source converted stdy bunsetsu-list bl)
143     (sj3rpc-call-with-environment env (result)
144       (comm-format (u s) (sj3-const BEGIN_EUC) yomi-ext)
145       (comm-unpack (u) result)
146       (if (/= result 0)
147           (- result)                    ; failure
148         (comm-unpack (u) result)        ; skip
149         (while (progn
150                  (comm-unpack (b) len)
151                  (> len 0))
152           (setq stdy (sj3rpc-get-stdy proc))
153           (comm-unpack (E) converted)
154           (setq source
155                 (decode-coding-string (substring yomi-ext p (+ p len))
156                                       'euc-japan)
157                 p (+ p len))
158           (let ((bl1 (cons (sj3-make-bunsetsu env
159                                               source converted nil stdy) nil)))
160             (if bl
161                 (setq bl (setcdr bl bl1))
162               (setq bunsetsu-list (setq bl bl1)))))
163         bunsetsu-list))))
164
165 (defun sj3rpc-open-dictionary (proc dict-file-name password)
166   (comm-call-with-proc proc (result)
167     (comm-format (u s s) (sj3-const DICADD) dict-file-name password)
168     (comm-unpack (u) result)
169     (if (/= result 0)
170         (- result)                      ; failure
171       (comm-unpack (u) result)
172       result)))
173
174 (defun sj3rpc-close-dictionary (proc dict-no)
175   (comm-call-with-proc proc (result)
176     (comm-format (u u) (sj3-const DICDEL) dict-no)
177     (comm-unpack (u) result)
178     result))
179
180 (defun sj3rpc-make-dictionary (proc dict-name)
181   (comm-call-with-proc proc (result)
182     (comm-format (u s u u u) (sj3-const MKDIC) dict-name
183                  2048  ; Index length
184                  2048  ; Length
185                  256   ; Number
186                  )
187     (comm-unpack (u) result)
188     result))
189
190 (defun sj3rpc-open-stdy (proc stdy-name)
191   (comm-call-with-proc proc (result)
192     (comm-format (u s s) (sj3-const OPENSTDY) stdy-name "")
193     (comm-unpack (u) result)
194     result))
195
196 (defun sj3rpc-close-stdy (proc)
197   (comm-call-with-proc proc (result)
198     (comm-format (u) (sj3-const CLOSESTDY))
199     (comm-unpack (u) result)
200     result))
201
202 (defun sj3rpc-make-stdy (proc stdy-name)
203   (comm-call-with-proc proc (result)
204     (comm-format (u s u u u) (sj3-const MKSTDY) stdy-name
205                  2048  ; Number
206                  1     ; Step
207                  2048  ; Length
208                  )
209     (comm-unpack (u) result)
210     result))
211
212 (defun sj3rpc-make-directory (proc name)
213   (comm-call-with-proc proc (result)
214     (comm-format (u s) (sj3-const MKDIR) name)
215     (comm-unpack (u) result)
216     result))
217
218 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
219   (let ((i 0)
220         stdy converted bunsetsu bl bunsetsu-list cylen rest)
221     (comm-call-with-proc-1 proc (result)
222       (comm-format (u u s) (sj3-const KOUHO_EUC) len yomi-ext)
223       (comm-unpack (u) result)
224       (if (/= result 0)
225           (- result)                    ; failure
226         (while (< i n)
227           (comm-unpack (u) cylen)
228           (setq stdy (sj3rpc-get-stdy proc))
229           (comm-unpack (E) converted)
230           (setq rest (decode-coding-string
231                       (substring yomi-ext cylen) 'euc-japan))
232           (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
233           (if bl
234               (setq bl (setcdr bl (cons bunsetsu nil)))
235             (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
236           (setq i (1+ i)))
237         (setq bunsetsu (sj3-make-bunsetsu env yomi yomi nil nil))
238         (setq bl (setcdr bl (cons bunsetsu nil)))
239         (setq bunsetsu
240               (sj3-make-bunsetsu env yomi (japanese-katakana yomi) nil nil))
241         (setq bl (setcdr bl (cons bunsetsu nil)))
242         bunsetsu-list))))
243
244 (defun sj3rpc-get-bunsetsu-candidates (env yomi)
245   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
246          (len (length yomi-ext)))
247     (sj3rpc-call-with-environment env (result)
248       (comm-format (u u s) (sj3-const KOUHOSU_EUC) len yomi-ext)
249       (comm-unpack (u) result)
250       (if (/= result 0)
251           (- result)                    ; failure
252         (comm-unpack (u) result)
253         (if (= result 0)
254             (list (sj3-make-bunsetsu env yomi yomi nil nil)) ; XXX
255           (sj3rpc-get-bunsetsu-candidates-sub proc env
256                                               yomi yomi-ext len result))))))
257
258 (defun sj3rpc-tanbunsetsu-conversion (env yomi)
259   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
260          (len (length yomi-ext)) cylen stdy converted rest)
261     (sj3rpc-call-with-environment env (result)
262       (comm-format (u u s) (sj3-const TANCONV_EUC) len yomi-ext)
263       (comm-unpack (u) result)
264       (if (/= result 0)
265           (- result)
266         (comm-unpack (u) cylen)
267         (setq stdy (sj3rpc-get-stdy proc))
268         (comm-unpack (E) converted)
269         (setq rest (decode-coding-string
270                     (substring yomi-ext cylen) 'euc-japan))
271         (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))))))
272
273 (defun sj3rpc-bunsetsu-stdy (env stdy)
274   (sj3rpc-call-with-environment env (result)
275      (comm-format (u v) (sj3-const STDY) stdy (length stdy))
276      (comm-unpack (u) result)
277       (if (/= result 0)
278           (- result)
279         0)))
280
281 (defun sj3rpc-kugiri-stdy (env yomi1 yomi2 stdy)
282   (let* ((yomi1-ext (encode-coding-string yomi1 'euc-japan))
283          (yomi2-ext (encode-coding-string yomi2 'euc-japan)))
284     (sj3rpc-call-with-environment env (result)
285       (comm-format (u s s v) (sj3-const CLSTDY_EUC)
286                    yomi1-ext yomi2-ext stdy (length stdy))
287       (comm-unpack (u) result)
288       (if (/= result 0)
289           (- result)
290         0))))
291
292 ;;; egg/sj3rpc.el ends here.