1 ;;; egg/cannarpc.el --- Canna Support (low level interface) in
2 ;;; Egg Input Method Architecture
4 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc
6 ;; Author: NIIBE Yutaka <gniibe@chroot.org>
8 ;; Maintainer: TOMURA Satoru <tomura@etl.go.jp>
10 ;; Keywords: mule, multilingual, input method
12 ;; This file is part of EGG.
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)
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.
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.
38 ;; (load-library "egg/canna")
39 (defmacro canna-const (c)
40 (cond ((eq c 'Initialize) 1)
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)
55 ((eq c 'ResizePause) 26)
56 ((eq c 'GetHinshi) 27)
58 ((eq c 'SetApplicationName) 33)
59 ((eq c 'NoticeGroupName) 34)
61 ((eq c 'CreateDictionary) 3)
63 (t (error "No such constant")))))
66 (defconst cannarpc-error-message (vector ))
68 (defun cannarpc-get-error-message (errno)
70 (< errno (length cannarpc-error-message))
71 (aref cannarpc-error-message errno))
72 (format "#%d" errno)))
74 (defmacro cannarpc-call-with-environment (e vlist send-expr &rest receive-exprs)
76 `((proc (cannaenv-get-proc ,e))
77 (context (cannaenv-get-context ,e)))
83 (set-buffer (process-buffer proc))
86 (process-send-region proc (point-min) (point-max))
87 (goto-char (prog1 (point) (accept-process-output proc))))
90 (defconst canna-version-fmt "3.3:%s")
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)
98 (comm-unpack (w w) minor context)
99 (cond ((and (= minor 65535) (= context 65535))
101 ((and (= minor 65535) (= context 65534))
102 -1) ; version miss match
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)
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)
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)
129 (defun cannarpc-get-dictionary-list (env)
132 (cannarpc-call-with-environment env (dummy result)
133 (comm-format (b b w w w) (canna-const GetDictionaryList) 0 4
135 (comm-unpack (u w) dummy result)
136 ;; follow list of dictionaries
140 (comm-unpack (s) dic)
142 (setq dl (setcdr dl (cons dic nil)))
143 (setq dic-list (setq dl (cons dic nil))))
147 (defun cannarpc-get-directory-list (env)
150 (cannarpc-call-with-environment env (dummy result)
151 (comm-format (b b w w w) (canna-const GetDirectoryList) 0 4
153 (comm-unpack (u w) dummy result)
154 ;; follow list of directories
158 (comm-unpack (s) dir)
160 (setq dl (setcdr dl (cons dir nil)))
161 (setq dir-list (setq dl (cons dir nil))))
165 (defun cannarpc-get-mount-dictionary-list (env)
168 (cannarpc-call-with-environment env (dummy result)
169 (comm-format (b b w w w) (canna-const GetMountDictionaryList) 0 4
171 (comm-unpack (u w) dummy result)
172 ;; follow list of dictionaries
176 (comm-unpack (s) dic)
178 (setq dl (setcdr dl (cons dic nil)))
179 (setq dic-list (setq dl (cons dic nil))))
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)
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)
199 (defun cannarpc-begin-conversion (env yomi)
201 (let ((yomi-ext (encode-coding-string yomi 'fixed-euc-jp))
202 (mode (or (cannaenv-get-mode env) 19)) ; 19 kana hiragana
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)
212 (comm-unpack (S) converted)
213 (let ((bl1 (cons (canna-make-bunsetsu env converted i nil)
216 (setq bl (setcdr bl bl1))
217 (setq bunsetsu-list (setq bl bl1))))
221 (defun cannarpc-cancel-conversion (env)
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)
230 (defun cannarpc-end-conversion (env len zenkouho-pos-vector mode)
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)
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)
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)
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)
263 ;; (comm-unpack (S) result)
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
270 (comm-unpack (u w) dummy result)
273 (comm-unpack (S) result)
276 (defun cannarpc-get-bunsetsu-candidates (env bunsetsu-pos yomi)
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
282 (comm-unpack (u w) dummy result)
286 (comm-unpack (S) converted)
287 (let ((bl1 (cons (canna-make-bunsetsu env converted
291 (setq bl (setcdr bl bl1))
292 (setq bunsetsu-list (setq bl bl1))))
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)
307 (comm-unpack (S) converted)
308 (let ((bl1 (cons (canna-make-bunsetsu env converted i nil) nil)))
310 (setq bl (setcdr bl bl1))
311 (setq bunsetsu-list (setq bl bl1))))
315 (defun cannarpc-get-hinshi (env bunsetsu-pos kouho-pos)
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)
325 (setq hinshi (concat hinshi (char-to-string b)))
326 (setq result (1- result)))
329 (defun cannarpc-get-lex (env bunsetsu-pos kouho-pos)
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)
339 (comm-unpack (i i i i i) ylen klen rownum coldnum dicnum)
340 (let ((ll1 (cons (list ylen klen rownum coldnum dicnum) nil)))
342 (setq ll (setcdr ll ll1))
343 (setq lex-list (setq ll ll1))))
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)
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)
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
381 (comm-unpack (u b) dummy result)
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)
395 ;;; egg/cannarpc.el ends here.