egg-980304.
[elisp/egg.git] / egg / cannarpc.el
1 ;;; egg/cannarpc.el --- Canna Support (low level interface) in
2 ;;;                     Egg 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 is 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 (eval-when-compile
33   (require 'egg-com)
34 ;;  (load-library "egg/canna")
35   (defmacro canna-const (c)
36     (cond ((eq c 'Initialize)            1)
37           ((eq c 'Finalize)              2)
38           ((eq c 'CreateContext)         3)
39           ((eq c 'CloseContext)          5)
40           ((eq c 'GetDictionaryList)     6)
41           ((eq c 'GetDirectoryList)      7)
42           ((eq c 'MountDictionary)       8)
43           ((eq c 'UnmountDictionary)       9)
44           ((eq c 'BeginConvert)         15)
45           ((eq c 'EndConvert)           16)
46           ((eq c 'GetCandidacyList)     17)
47           ((eq c 'GetYomi)              18)
48           ((eq c 'ResizePause)          26)
49
50           ((eq c 'CreateDictionary)      3)
51           (t (error "No such constant")))))
52
53 (defun cannarpc-get-error-message (errno)
54   (or (aref cannarpc-error-message errno) (format "#%d" errno)))
55
56 (defmacro cannarpc-call-with-environment (e vlist send-expr &rest receive-exprs)
57   (let ((v (append
58             `((proc (cannaenv-get-proc ,e))
59               (context (cannaenv-get-context ,e)))
60             vlist)))
61     (list
62      'let v
63      (append
64         `(save-excursion
65            (set-buffer (process-buffer proc))
66            (erase-buffer)
67            ,send-expr
68            (process-send-region proc (point-min) (point-max))
69            (goto-char (prog1 (point) (accept-process-output proc))))
70         receive-exprs))))
71 \f
72 (defconst canna-version-fmt "2.0:%s")
73
74 (defun cannarpc-open (proc username)
75   "Open the session.  Return 0 on success, error code on failure."
76   (let ((verusr (format canna-version-fmt username)))
77     (comm-call-with-proc proc (result)
78       (comm-format (u u v) (canna-const Initialize) (length verusr) verusr)
79       (comm-unpack (u) result)
80       result)))
81
82 (defun cannarpc-close (proc)
83   (comm-call-with-proc proc (dummy result)
84     (comm-format (b b w) (canna-const Finalize) 0 0)
85     (comm-unpack (b b w b) dummy dummy dummy result)
86     result))
87
88 (defun cannarpc-create-context (proc)
89   (comm-call-with-proc proc (dummy result)
90     (comm-format (b b w) (canna-const CreateContext) 0 0)
91     (comm-unpack (b b w w) dummy dummy dummy result)
92     result))
93
94 (defun cannarpc-close-context (proc context)
95   (comm-call-with-proc proc (dummy result)
96     (comm-format (b b w w) (canna-const CloseContext) 0 2 context)
97     (comm-unpack (b b w b) dummy dummy dummy result)
98     result))
99
100 ;; XXX: Not implemented fully
101 (defun cannarpc-get-dictionary-list (env)
102   (cannarpc-call-with-environment env (dymmy result)
103     (comm-format (b b w w w) (canna-const GetDictionaryList) 0 4
104                  context 1024)
105     (comm-unpack (u w) dummy result)
106     ;; follow list of dictionaries
107     result))
108
109 ;; XXX: Not implemented fully
110 (defun cannarpc-get-directory-list (env)
111   (cannarpc-call-with-environment env (dymmy result)
112     (comm-format (b b w w w) (canna-const GetDirectoryList) 0 4
113                  context 1024)
114     (comm-unpack (u w) dummy result)
115     ;; follow list of directories
116     result))
117
118 (defun cannarpc-open-dictionary (env dict-file-name mode)
119   (cannarpc-call-with-environment env (dymmy result)
120     (comm-format (b b w u w s) (canna-const MountDictionary) 0
121                  (+ (length dict-file-name) 7)
122                  mode context dict-file-name)
123     (comm-unpack (u b) dummy result)
124     result))
125
126 (defun cannarpc-close-dictionary (env dict-file-name mode)
127   (cannarpc-call-with-environment env (dymmy result)
128     (comm-format (b b w u w s) (canna-const UnmountDictionary) 0
129                  (+ (length dict-file-name) 6)
130                  mode context dict-file-name)
131     (comm-unpack (u b) dummy result)
132     result))
133
134 (defun cannarpc-begin-conversion (env yomi)
135   "Begin conversion."
136   (let ((yomi-ext (encode-coding-string yomi 'euc-japan))
137         (i 0)
138         converted bunsetsu-list bl)
139     (cannarpc-call-with-environment env (dummy result)
140       (comm-format (b b w u w S) (canna-const BeginConvert) 0
141                    (+ (length yomi-ext) 8) 0 context yomi)
142       (comm-unpack (u w) dummy result)
143       (if (= result 65535)
144           -1                            ; failure
145         (while (< i result)
146           (comm-unpack (S) converted)
147           (let ((bl1 (cons (canna-make-bunsetsu env converted i)
148                            nil)))
149             (if bl
150                 (setq bl (setcdr bl bl1))
151               (setq bunsetsu-list (setq bl bl1))))
152           (setq i (1+ i)))
153         bunsetsu-list))))
154
155 (defun cannarpc-end-conversion (env len zenkouho-pos-vector mode)
156   "End conversion."
157   (cannarpc-call-with-environment env (dummy result)
158     (comm-format (b b w w w u v) (canna-const EndConvert) 0
159                  (+ (* len 2) 8) context len mode zenkouho-pos-vector)
160     (comm-unpack (u b) dummy result)
161     (if (= result 255)
162         -1                              ; failure
163       result)))
164
165 (defun cannarpc-make-dictionary (env dict-name)
166   (cannarpc-call-with-environment env (dummy result)
167     (comm-format (b b w u w s) (canna-const CreateDictionary) 1
168                  (+ (length dict-name) 7) 0 context dict-name)
169     (comm-unpack (u b) dummy result)
170     result))
171
172 (defun cannarpc-get-bunsetsu-source (env bunsetsu-pos)
173   (cannarpc-call-with-environment env (dummy result)
174     (comm-format (b b w w w w) (canna-const GetYomi) 0 6 context
175                  bunsetsu-pos 1024)
176     (comm-unpack (u w) dummy result)
177     (if (= result 65535)
178         -1
179       (comm-unpack (S) result)
180       result)))
181
182 (defun cannarpc-get-bunsetsu-candidates (env bunsetsu-pos)
183   (let ((i 0)
184         converted bunsetsu-list bl)
185     (cannarpc-call-with-environment env (dummy result)
186       (comm-format (b b w w w w) (canna-const GetCandidacyList) 0 6 context
187                    bunsetsu-pos 1024)
188       (comm-unpack (u w) dymmy result)
189       (if (= result 65535)
190           -1                            ; failure
191         (while (< i result)
192           (comm-unpack (S) converted)
193           (let ((bl1 (cons (canna-make-bunsetsu env converted bunsetsu-pos)
194                            nil)))
195             (if bl
196                 (setq bl (setcdr bl bl1))
197               (setq bunsetsu-list (setq bl bl1))))
198           (setq i (1+ i)))
199         bunsetsu-list))))
200
201 ;;; egg/cannarpc.el ends here.