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