egg-980217.
[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 (defun sj3rpc-get-error-message (errno)
74   (or (aref sj3rpc-error-message errno) (format "#%d" errno)))
75
76 (defmacro sj3rpc-call-with-environment (e vlist send-expr &rest receive-exprs)
77   (let ((v (append
78             `((proc (sj3env-get-proc ,e)))
79             vlist)))
80     (list
81      'let v
82      (append
83         `(save-excursion
84            (set-buffer (process-buffer proc))
85            (erase-buffer)
86            ,send-expr
87            (process-send-region proc (point-min) (point-max))
88            (goto-char (prog1 (point) (accept-process-output proc))))
89         receive-exprs))))
90 \f
91 (defun sj3rpc-open (proc myhostname username)
92   "Open the session.  Return 0 on success, error code on failure."
93   (comm-call-with-proc proc (result)
94     (comm-format (u u s s s) (sj3-const OPEN) 2 ; Server version
95                  myhostname username
96                  ;; program name
97                  (format "%d.emacs-egg" (emacs-pid)))
98     (comm-unpack (u) result)
99     (if (= result -2)
100         0
101       result)))
102
103 (defun sj3rpc-get-stdy-size (proc)
104   "Return STDYSIZE of SJ3 server.  On failure, return error code."
105   (comm-call-with-proc proc (result)
106     (comm-format (u) (sj3-const STDYSIZE))
107     (comm-unpack (u) result)
108     (if (/= result 0)
109         (- result)                      ; failure
110       (comm-unpack (u) result)
111       result)))
112
113 (defsubst sj3rpc-get-stdy (proc)
114   (let ((n 0)
115         (stdy (make-vector sj3-stdy-size 0)))
116     (while (< n sj3-stdy-size)
117       (comm-unpack (b) r)
118       (aset stdy n r)
119       (setq n (1+ n)))
120     stdy))
121
122 (defun sj3rpc-begin (env yomi)
123   "Begin conversion."
124   (let ((yomi-ext (encode-coding-string yomi 'euc-japan))
125         (p 0)
126         len source converted stdy bunsetsu-list bl)
127     (sj3rpc-call-with-environment env (result)
128       (comm-format (u s) (sj3-const BEGIN_EUC) yomi-ext)
129       (comm-unpack (u) result)
130       (if (/= result 0)
131           (- result)                    ; failure
132         (comm-unpack (u) result)        ; skip
133         (while (progn
134                  (comm-unpack (b) len)
135                  (> len 0))
136           (setq stdy (sj3rpc-get-stdy proc))
137           (comm-unpack (E) converted)
138           (setq source
139                 (decode-coding-string (substring yomi-ext p (+ p len))
140                                       'euc-japan)
141                 p (+ p len))
142           (let ((bl1 (cons (sj3-make-bunsetsu env
143                                               source converted nil stdy) nil)))
144             (if bl
145                 (setq bl (setcdr bl bl1))
146               (setq bunsetsu-list (setq bl bl1)))))
147         bunsetsu-list))))
148
149 (defun sj3rpc-open-dictionary (proc dict-file-name password)
150   (comm-call-with-proc proc (result)
151     (comm-format (u s s) (sj3-const DICADD) dict-file-name password)
152     (comm-unpack (u) result)
153     (if (/= result 0)
154         (- result)                      ; failure
155       (comm-unpack (u) result)
156       result)))
157
158 (defun sj3rpc-close-dictionary (proc dict-no)
159   (comm-call-with-proc proc (result)
160     (comm-format (u u) (sj3-const DICDEL) dict-no)
161     (comm-unpack (u) result)
162     result))
163
164 (defun sj3rpc-make-dictionary (proc dict-name)
165   (comm-call-with-proc proc (result)
166     (comm-format (u s u u u) (sj3-const MKDIC) dict-name
167                  2048  ; Index length
168                  2048  ; Length
169                  256   ; Number
170     (comm-unpack (u) result)
171     result)))
172
173 (defun sj3rpc-open-stdy (proc stdy-name)
174   (comm-call-with-proc proc (result)
175     (comm-format (u s s) (sj3-const OPENSTDY) stdy-name "")
176     (comm-unpack (u) result)
177     result))
178
179 (defun sj3rpc-close-stdy (proc)
180   (comm-call-with-proc proc (result)
181     (comm-format (u) (sj3-const CLOSESTDY))
182     (comm-unpack (u) result)
183     result))
184
185 (defun sj3rpc-make-stdy (proc stdy-name)
186   (comm-call-with-proc proc (result)
187     (comm-format (u) (sj3-const MKSTDY) stdy-name "")
188                  2048  ; Number
189                  1     ; Step
190                  2048  ; Length
191     (comm-unpack (u) result)
192     result))
193
194 (defun sj3rpc-get-bunsetsu-candidates-sub (proc env yomi yomi-ext len n)
195   (let ((i 0)
196         stdy converted bunsetsu bl bunsetsu-list cylen rest)
197     (comm-call-with-proc-1 proc (result)
198       (comm-format (u u s) (sj3-const KOUHO_EUC) len yomi-ext)
199       (comm-unpack (u) result)
200       (if (/= result 0)
201           (- result)                    ; failure
202         (while (< i n)
203           (comm-unpack (u) cylen)
204           (setq stdy (sj3rpc-get-stdy proc))
205           (comm-unpack (E) converted)
206           (setq rest (decode-coding-string
207                       (substring yomi-ext cylen) 'euc-japan))
208           (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))
209           (if bl
210               (setq bl (setcdr bl (cons bunsetsu nil)))
211             (setq bunsetsu-list (setq bl (cons bunsetsu nil))))
212           (setq i (1+ i)))
213         bunsetsu-list))))
214
215 (defun sj3rpc-get-bunsetsu-candidates (env yomi)
216   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
217          (len (length yomi-ext)))
218     (sj3rpc-call-with-environment env (result)
219       (comm-format (u u s) (sj3-const KOUHOSU_EUC) len yomi-ext)
220       (comm-unpack (u) result)
221       (if (/= result 0)
222           (- result)                    ; failure
223         (comm-unpack (u) result)
224         (if (= result 0)
225             (list (sj3-make-bunsetsu env yomi yomi nil nil)) ; XXX
226           (sj3rpc-get-bunsetsu-candidates-sub proc env
227                                               yomi yomi-ext len result))))))
228
229 (defun sj3rpc-tanbunsetsu-conversion (env yomi)
230   (let* ((yomi-ext (encode-coding-string yomi 'euc-japan))
231          (len (length yomi-ext)) cylen stdy converted rest)
232     (sj3rpc-call-with-environment env (result)
233       (comm-format (u u s) (sj3-const TANCONV_EUC) len yomi-ext)
234       (comm-unpack (u) result)
235       (if (/= result 0)
236           (- result)
237         (comm-unpack (u) cylen)
238         (setq stdy (sj3rpc-get-stdy proc))
239         (comm-unpack (E) converted)
240         (setq rest (decode-coding-string
241                     (substring yomi-ext cylen) 'euc-japan))
242         (setq bunsetsu (sj3-make-bunsetsu env yomi converted rest stdy))))))
243
244 ;;; egg/sj3rpc.el ends here.