* egg-edep.el: Define compatibility functions.
[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 'GetMountDictionaryList) 11)
49           ((eq c 'DefineWord)             13)
50           ((eq c 'DeleteWord)             14)
51           ((eq c 'BeginConvert)         15)
52           ((eq c 'EndConvert)           16)
53           ((eq c 'GetCandidacyList)     17)
54           ((eq c 'GetYomi)              18)
55           ((eq c 'ResizePause)          26)
56           ((eq c 'GetHinshi)              27)
57           ((eq c 'GetLex)                 28)
58           ((eq c 'SetApplicationName)     33)
59           ((eq c 'NoticeGroupName)        34)
60
61           ((eq c 'CreateDictionary)      3)
62           ((eq c 'Sync)                    8)
63           (t (error "No such constant")))))
64
65 ;; XXX
66 (defconst cannarpc-error-message (vector ))
67
68 (defun cannarpc-get-error-message (errno)
69   (or (and (>= errno 0)
70            (< errno (length cannarpc-error-message))
71            (aref cannarpc-error-message errno))
72       (format "#%d" errno)))
73
74 (defmacro cannarpc-call-with-environment (e vlist send-expr &rest receive-exprs)
75   (let ((v (append
76             `((proc (cannaenv-get-proc ,e))
77               (context (cannaenv-get-context ,e)))
78             vlist)))
79     (list
80      'let v
81      (append
82         `(save-excursion
83            (set-buffer (process-buffer proc))
84            (erase-buffer)
85            ,send-expr
86            (process-send-region proc (point-min) (point-max))
87            (goto-char (prog1 (point) (accept-process-output proc))))
88         receive-exprs))))
89 \f
90 (defconst canna-version-fmt "3.3:%s")
91
92 (defun cannarpc-open (proc username)
93   "Open the session.  Return 0 on success, error code on failure."
94   (let ((verusr (format canna-version-fmt username)))
95     (comm-call-with-proc proc (minor context)
96       (comm-format (u u s) (canna-const Initialize) (+ (length verusr) 1)
97                    verusr)
98       (comm-unpack (w w) minor context)
99       (cond ((and (= minor 65535) (= context 65535))
100              -1)                        ; failure
101             ((and (= minor 65535) (= context 65534))
102              -1)                        ; version miss match
103             (t context)))))
104
105 (defun cannarpc-close (proc)
106   (comm-call-with-proc proc (dummy result)
107     (comm-format (b b w) (canna-const Finalize) 0 0)
108     (comm-unpack (u b) dummy result)
109     (if (= result 255)
110         -1                              ; failure
111       result)))
112
113 (defun cannarpc-create-context (proc)
114   (comm-call-with-proc proc (dummy result)
115     (comm-format (b b w) (canna-const CreateContext) 0 0)
116     (comm-unpack (u w) dummy result)
117     (if (= result 65535)
118         -1                              ; failure
119       result)))
120
121 (defun cannarpc-close-context (env)
122   (cannarpc-call-with-environment env (dummy result)
123     (comm-format (b b w w) (canna-const CloseContext) 0 2 context)
124     (comm-unpack (u b) dummy result)
125     (if (= result 255)
126         -1                              ; failure
127       result)))
128
129 (defun cannarpc-get-dictionary-list (env)
130   (let ((i 0)
131         dic dl dic-list)
132     (cannarpc-call-with-environment env (dummy result)
133     (comm-format (b b w w w) (canna-const GetDictionaryList) 0 4
134                  context 1024)
135     (comm-unpack (u w) dummy result)
136     ;; follow list of dictionaries
137       (if (= result 65535)
138           -1                            ; failure
139         (while (< i result)
140           (comm-unpack (s) dic)
141           (if dl
142               (setq dl (setcdr dl (cons dic nil)))
143             (setq dic-list (setq dl (cons dic nil))))
144           (setq i (1+ i)))
145         dic-list))))
146
147 (defun cannarpc-get-directory-list (env)
148   (let ((i 0)
149         dir dl dir-list)
150     (cannarpc-call-with-environment env (dummy result)
151     (comm-format (b b w w w) (canna-const GetDirectoryList) 0 4
152                  context 1024)
153     (comm-unpack (u w) dummy result)
154     ;; follow list of directories
155       (if (= result 65535)
156           -1                            ; failure
157         (while (< i result)
158           (comm-unpack (s) dir)
159           (if dl
160               (setq dl (setcdr dl (cons dir nil)))
161             (setq dir-list (setq dl (cons dir nil))))
162           (setq i (1+ i)))
163         dir-list))))
164
165 (defun cannarpc-get-mount-dictionary-list (env)
166   (let ((i 0)
167         dic dl dic-list)
168     (cannarpc-call-with-environment env (dummy result)
169       (comm-format (b b w w w) (canna-const GetMountDictionaryList) 0 4
170                    context 1024)
171       (comm-unpack (u w) dummy result)
172       ;; follow list of dictionaries
173       (if (= result 65535)
174           -1                            ; failure
175         (while (< i result)
176           (comm-unpack (s) dic)
177           (if dl
178               (setq dl (setcdr dl (cons dic nil)))
179             (setq dic-list (setq dl (cons dic nil))))
180           (setq i (1+ i)))
181         dic-list))))
182
183 (defun cannarpc-open-dictionary (env dict-file-name mode)
184   (cannarpc-call-with-environment env (dummy result)
185     (comm-format (b b w u w s) (canna-const MountDictionary) 0
186                  (+ (length dict-file-name) 7)
187                  mode context dict-file-name)
188     (comm-unpack (u b) dummy result)
189     result))
190
191 (defun cannarpc-close-dictionary (env dict-file-name mode)
192   (cannarpc-call-with-environment env (dummy result)
193     (comm-format (b b w u w s) (canna-const UnmountDictionary) 0
194                  (+ (length dict-file-name) 7)
195                  mode context dict-file-name)
196     (comm-unpack (u b) dummy result)
197     result))
198
199 (defun cannarpc-begin-conversion (env yomi)
200   "Begin conversion."
201   (let ((yomi-ext (encode-coding-string yomi 'fixed-euc-jp))
202         (mode (or (cannaenv-get-mode env) 19)) ; 19 kana hiragana
203         (i 0)
204         converted bunsetsu-list bl)
205     (cannarpc-call-with-environment env (dummy result)
206       (comm-format (b b w i w S) (canna-const BeginConvert) 0
207                    (+ (length yomi-ext) 8) mode context yomi)
208       (comm-unpack (u w) dummy result)
209       (if (= result 65535)
210           -1                            ; failure
211         (while (< i result)
212           (comm-unpack (S) converted)
213           (let ((bl1 (cons (canna-make-bunsetsu env converted i nil)
214                            nil)))
215             (if bl
216                 (setq bl (setcdr bl bl1))
217               (setq bunsetsu-list (setq bl bl1))))
218           (setq i (1+ i)))
219         bunsetsu-list))))
220
221 (defun cannarpc-cancel-conversion (env)
222   "Cancel conversion."
223   (cannarpc-call-with-environment env (dummy result)
224     (comm-format (b b w w w u) (canna-const EndConvert) 0 8 context 0 0)
225     (comm-unpack (u b) dummy result)
226     (if (= result 255)
227         -1                              ; failure
228       result)))
229
230 (defun cannarpc-end-conversion (env len zenkouho-pos-vector mode)
231   "End conversion."
232   (cannarpc-call-with-environment env (dummy result)
233     (comm-format (b b w w w u v) (canna-const EndConvert) 0
234                  (+ (* len 2) 8) context len mode zenkouho-pos-vector
235                  (length zenkouho-pos-vector))
236     (comm-unpack (u b) dummy result)
237     (if (= result 255)
238         -1                              ; failure
239       result)))
240
241 (defun cannarpc-make-dictionary (env dict-name)
242   (cannarpc-call-with-environment env (dummy result)
243     (comm-format (b b w u w s) (canna-const CreateDictionary) 1
244                  (+ (length dict-name) 7) 0 context dict-name)
245     (comm-unpack (u b) dummy result)
246     result))
247
248 (defun cannarpc-save-dictionary (env dict-name)
249   (cannarpc-call-with-environment env (dummy result)
250     (comm-format (b b w u w s) (canna-const Sync) 1
251                  (+ (length dict-name) 7) 0 context dict-name)
252     (comm-unpack (u b) dummy result)
253     result))
254
255 ;;; XXX not used
256 (defun cannarpc-get-dictionary-data (env dir dic)
257   (cannarpc-call-with-environment env (dummy result)
258     (comm-format (b b w w s s w) 6 1
259                  (+ (length dir) (length dic) 6) context dir dic 4096)
260     (comm-unpack (u w) dummy result)
261     (if (= result 65535)
262         -1
263 ;;      (comm-unpack (S) result)
264       result)))
265
266 (defun cannarpc-get-bunsetsu-source (env bunsetsu-pos)
267   (cannarpc-call-with-environment env (dummy result)
268     (comm-format (b b w w w w) (canna-const GetYomi) 0 6 context
269                  bunsetsu-pos 1024)
270     (comm-unpack (u w) dummy result)
271     (if (= result 65535)
272         -1
273       (comm-unpack (S) result)
274       result)))
275
276 (defun cannarpc-get-bunsetsu-candidates (env bunsetsu-pos yomi)
277   (let ((i 0)
278         converted bunsetsu-list bl)
279     (cannarpc-call-with-environment env (dummy result)
280       (comm-format (b b w w w w) (canna-const GetCandidacyList) 0 6 context
281                    bunsetsu-pos 1024)
282       (comm-unpack (u w) dummy result)
283       (if (= result 65535)
284           -1                            ; failure
285         (while (< i result)
286           (comm-unpack (S) converted)
287           (let ((bl1 (cons (canna-make-bunsetsu env converted
288                                                 bunsetsu-pos yomi)
289                            nil)))
290             (if bl
291                 (setq bl (setcdr bl bl1))
292               (setq bunsetsu-list (setq bl bl1))))
293           (setq i (1+ i)))
294         bunsetsu-list))))
295
296 (defun cannarpc-set-kugiri-changed (env yomi-length bunsetsu-pos)
297   ;; yomi-length -2\e$B!DJ8@a=L$a\e(B -1\e$B!DJ8@a?-$P$7\e(B
298   (let* ((i bunsetsu-pos)
299          converted bunsetsu-list bl)
300     (cannarpc-call-with-environment env (dummy result)
301       (comm-format (b b w w w w) (canna-const ResizePause) 0 6 context
302                    bunsetsu-pos yomi-length)
303       (comm-unpack (u w) dummy result)
304       (if (= result 65535)
305           -1                            ; failure
306         (while (< i result)
307           (comm-unpack (S) converted)
308           (let ((bl1 (cons (canna-make-bunsetsu env converted i nil) nil)))
309             (if bl
310                 (setq bl (setcdr bl bl1))
311               (setq bunsetsu-list (setq bl bl1))))
312           (setq i (1+ i)))
313         bunsetsu-list))))
314
315 (defun cannarpc-get-hinshi (env bunsetsu-pos kouho-pos)
316   (let (b hinshi)
317     (cannarpc-call-with-environment env (dummy result)
318       (comm-format (b b w w w w w) (canna-const GetHinshi) 0 8 context
319                    bunsetsu-pos kouho-pos 1024)
320       (comm-unpack (u w) dummy result)
321       (if (= result 65535)
322           -1
323         (while (> result 0)
324           (comm-unpack (w) b)
325           (setq hinshi (concat hinshi (char-to-string b)))
326           (setq result (1- result)))
327         hinshi))))
328
329 (defun cannarpc-get-lex (env bunsetsu-pos kouho-pos)
330   (let ((i 0)
331         ylen klen rownum coldnum dicnum lex-list ll)
332     (cannarpc-call-with-environment env (dummy result)
333       (comm-format (b b w w w w w) (canna-const GetLex) 0 8 context
334                    bunsetsu-pos kouho-pos 1024)
335       (comm-unpack (u w) dummy result)
336       (if (= result 65535)
337           -1
338         (while (< i result)
339           (comm-unpack (i i i i i) ylen klen rownum coldnum dicnum)
340           (let ((ll1 (cons (list ylen klen rownum coldnum dicnum) nil)))
341             (if ll
342                 (setq ll (setcdr ll ll1))
343               (setq lex-list (setq ll ll1))))
344           (setq i (1+ i)))
345         lex-list))))
346
347 (defun cannarpc-add-word (env dictionary yomi kanji hinshi)
348   "Register a word KANJI into DICTIONARY with a pronunciation YOMI and
349 a part of speech HINSHI.  Where DICTIONARY should be an integer."
350   (let* ((word-info (concat yomi " " hinshi " " kanji))
351          (word-info-ext (encode-coding-string word-info 'fixed-euc-jp))
352          (length (+ (length word-info-ext) (length dictionary) 5)))
353     (cannarpc-call-with-environment env (dummy result)
354       (comm-format (b b w w S s) (canna-const DefineWord) 0 length context
355                    word-info dictionary)
356       (comm-unpack (u b) dummy result)
357       (if (= result 255)
358           -1            ; failure
359         result))))
360
361 (defun cannarpc-delete-word (env dictionary yomi kanji hinshi)
362   "Delete the registered word KANJI from DICTIONARY with a
363 pronunciation YOMI and a part of speech HINSHI.  Where DICTIONARY
364 should be an integer."
365   (let* ((word-info (concat yomi " " hinshi " " kanji))
366          (word-info-ext (encode-coding-string word-info 'fixed-euc-jp))
367          (length (+ (length word-info-ext) (length dictionary) 5)))
368     (cannarpc-call-with-environment env (dummy result)
369       (comm-format (b b w w S s) (canna-const DeleteWord) 0 length context
370                    word-info dictionary)
371       (comm-unpack (u b) dummy result)
372       (if (= result 255)
373           -1            ; failure
374         result))))
375
376 (defun cannarpc-notice-group-name (proc context group)
377   (comm-call-with-proc proc (dummy result)
378     (comm-format (b b w u w s) (canna-const NoticeGroupName) 0
379                  (+ (length group) 7) 0 ;; mode = 0
380                  context group)
381     (comm-unpack (u b) dummy result)
382     (if (= result 255)
383         -1
384       result)))
385
386 (defun cannarpc-set-app-name (proc context name)
387   (comm-call-with-proc proc (dummy result)
388     (comm-format (b b w u w s) (canna-const SetApplicationName) 0
389                  (+ (length name) 7) 0 context name)
390     (comm-unpack (u b) dummy result)
391     (if (= result 255)
392         -1
393       result)))
394
395 ;;; egg/cannarpc.el ends here.