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 ;; Keywords: mule, multilingual, input method
10 ;; This file is part of EGG.
12 ;; EGG is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; EGG is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc.,
25 ;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 ;; (load-library "egg/canna")
37 (defmacro canna-const (c)
38 (cond ((eq c 'Initialize) 1)
40 ((eq c 'CreateContext) 3)
41 ((eq c 'CloseContext) 5)
42 ((eq c 'GetDictionaryList) 6)
43 ((eq c 'GetDirectoryList) 7)
44 ((eq c 'MountDictionary) 8)
45 ((eq c 'UnmountDictionary) 9)
46 ((eq c 'GetMountDictionaryList) 11)
47 ((eq c 'DefineWord) 13)
48 ((eq c 'DeleteWord) 14)
49 ((eq c 'BeginConvert) 15)
50 ((eq c 'EndConvert) 16)
51 ((eq c 'GetCandidacyList) 17)
53 ((eq c 'ResizePause) 26)
54 ((eq c 'GetHinshi) 27)
56 ((eq c 'SetApplicationName) 33)
57 ((eq c 'NoticeGroupName) 34)
59 ((eq c 'CreateDictionary) 3)
61 (t (error "No such constant")))))
64 (defconst cannarpc-error-message (vector ))
66 (defun cannarpc-get-error-message (errno)
68 (< errno (length cannarpc-error-message))
69 (aref cannarpc-error-message errno))
70 (format "#%d" errno)))
72 (defmacro cannarpc-call-with-environment (e vlist send-expr &rest receive-exprs)
74 `((proc (cannaenv-get-proc ,e))
75 (context (cannaenv-get-context ,e)))
81 (set-buffer (process-buffer proc))
84 (process-send-region proc (point-min) (point-max))
85 (goto-char (prog1 (point) (accept-process-output proc))))
88 (defconst canna-version-fmt "3.3:%s")
90 (defun cannarpc-open (proc username)
91 "Open the session. Return 0 on success, error code on failure."
92 (let ((verusr (format canna-version-fmt username)))
93 (comm-call-with-proc proc (minor context)
94 (comm-format (u u s) (canna-const Initialize) (+ (length verusr) 1)
96 (comm-unpack (w w) minor context)
97 (cond ((and (= minor 65535) (= context 65535))
99 ((and (= minor 65535) (= context 65534))
100 -1) ; version miss match
103 (defun cannarpc-close (proc)
104 (comm-call-with-proc proc (dummy result)
105 (comm-format (b b w) (canna-const Finalize) 0 0)
106 (comm-unpack (u b) dummy result)
111 (defun cannarpc-create-context (proc)
112 (comm-call-with-proc proc (dummy result)
113 (comm-format (b b w) (canna-const CreateContext) 0 0)
114 (comm-unpack (u w) dummy result)
119 (defun cannarpc-close-context (env)
120 (cannarpc-call-with-environment env (dummy result)
121 (comm-format (b b w w) (canna-const CloseContext) 0 2 context)
122 (comm-unpack (u b) dummy result)
127 (defun cannarpc-get-dictionary-list (env)
130 (cannarpc-call-with-environment env (dummy result)
131 (comm-format (b b w w w) (canna-const GetDictionaryList) 0 4
133 (comm-unpack (u w) dummy result)
134 ;; follow list of dictionaries
138 (comm-unpack (s) dic)
140 (setq dl (setcdr dl (cons dic nil)))
141 (setq dic-list (setq dl (cons dic nil))))
145 (defun cannarpc-get-directory-list (env)
148 (cannarpc-call-with-environment env (dummy result)
149 (comm-format (b b w w w) (canna-const GetDirectoryList) 0 4
151 (comm-unpack (u w) dummy result)
152 ;; follow list of directories
156 (comm-unpack (s) dir)
158 (setq dl (setcdr dl (cons dir nil)))
159 (setq dir-list (setq dl (cons dir nil))))
163 (defun cannarpc-get-mount-dictionary-list (env)
166 (cannarpc-call-with-environment env (dummy result)
167 (comm-format (b b w w w) (canna-const GetMountDictionaryList) 0 4
169 (comm-unpack (u w) dummy result)
170 ;; follow list of dictionaries
174 (comm-unpack (s) dic)
176 (setq dl (setcdr dl (cons dic nil)))
177 (setq dic-list (setq dl (cons dic nil))))
181 (defun cannarpc-open-dictionary (env dict-file-name mode)
182 (cannarpc-call-with-environment env (dummy result)
183 (comm-format (b b w u w s) (canna-const MountDictionary) 0
184 (+ (length dict-file-name) 7)
185 mode context dict-file-name)
186 (comm-unpack (u b) dummy result)
189 (defun cannarpc-close-dictionary (env dict-file-name mode)
190 (cannarpc-call-with-environment env (dummy result)
191 (comm-format (b b w u w s) (canna-const UnmountDictionary) 0
192 (+ (length dict-file-name) 7)
193 mode context dict-file-name)
194 (comm-unpack (u b) dummy result)
197 (defun cannarpc-begin-conversion (env yomi)
199 (let ((yomi-ext (encode-coding-string yomi 'fixed-euc-jp))
200 (mode (or (cannaenv-get-mode env) 19)) ; 19 kana hiragana
202 converted bunsetsu-list bl)
203 (cannarpc-call-with-environment env (dummy result)
204 (comm-format (b b w i w S) (canna-const BeginConvert) 0
205 (+ (length yomi-ext) 8) mode context yomi)
206 (comm-unpack (u w) dummy result)
210 (comm-unpack (S) converted)
211 (let ((bl1 (cons (canna-make-bunsetsu env converted i nil)
214 (setq bl (setcdr bl bl1))
215 (setq bunsetsu-list (setq bl bl1))))
219 (defun cannarpc-cancel-conversion (env)
221 (cannarpc-call-with-environment env (dummy result)
222 (comm-format (b b w w w u) (canna-const EndConvert) 0 8 context 0 0)
223 (comm-unpack (u b) dummy result)
228 (defun cannarpc-end-conversion (env len zenkouho-pos-vector mode)
230 (cannarpc-call-with-environment env (dummy result)
231 (comm-format (b b w w w u v) (canna-const EndConvert) 0
232 (+ (* len 2) 8) context len mode zenkouho-pos-vector
233 (length zenkouho-pos-vector))
234 (comm-unpack (u b) dummy result)
239 (defun cannarpc-make-dictionary (env dict-name)
240 (cannarpc-call-with-environment env (dummy result)
241 (comm-format (b b w u w s) (canna-const CreateDictionary) 1
242 (+ (length dict-name) 7) 0 context dict-name)
243 (comm-unpack (u b) dummy result)
246 (defun cannarpc-save-dictionary (env dict-name)
247 (cannarpc-call-with-environment env (dummy result)
248 (comm-format (b b w u w s) (canna-const Sync) 1
249 (+ (length dict-name) 7) 0 context dict-name)
250 (comm-unpack (u b) dummy result)
254 (defun cannarpc-get-dictionary-data (env dir dic)
255 (cannarpc-call-with-environment env (dummy result)
256 (comm-format (b b w w s s w) 6 1
257 (+ (length dir) (length dic) 6) context dir dic 4096)
258 (comm-unpack (u w) dummy result)
261 ;; (comm-unpack (S) result)
264 (defun cannarpc-get-bunsetsu-source (env bunsetsu-pos)
265 (cannarpc-call-with-environment env (dummy result)
266 (comm-format (b b w w w w) (canna-const GetYomi) 0 6 context
268 (comm-unpack (u w) dummy result)
271 (comm-unpack (S) result)
274 (defun cannarpc-get-bunsetsu-candidates (env bunsetsu-pos yomi)
276 converted bunsetsu-list bl)
277 (cannarpc-call-with-environment env (dummy result)
278 (comm-format (b b w w w w) (canna-const GetCandidacyList) 0 6 context
280 (comm-unpack (u w) dummy result)
284 (comm-unpack (S) converted)
285 (let ((bl1 (cons (canna-make-bunsetsu env converted
289 (setq bl (setcdr bl bl1))
290 (setq bunsetsu-list (setq bl bl1))))
294 (defun cannarpc-set-kugiri-changed (env yomi-length bunsetsu-pos)
295 ;; yomi-length -2
\e$B!DJ8@a=L$a
\e(B -1
\e$B!DJ8@a?-$P$7
\e(B
296 (let* ((i bunsetsu-pos)
297 converted bunsetsu-list bl)
298 (cannarpc-call-with-environment env (dummy result)
299 (comm-format (b b w w w w) (canna-const ResizePause) 0 6 context
300 bunsetsu-pos yomi-length)
301 (comm-unpack (u w) dummy result)
305 (comm-unpack (S) converted)
306 (let ((bl1 (cons (canna-make-bunsetsu env converted i nil) nil)))
308 (setq bl (setcdr bl bl1))
309 (setq bunsetsu-list (setq bl bl1))))
313 (defun cannarpc-get-hinshi (env bunsetsu-pos kouho-pos)
315 (cannarpc-call-with-environment env (dummy result)
316 (comm-format (b b w w w w w) (canna-const GetHinshi) 0 8 context
317 bunsetsu-pos kouho-pos 1024)
318 (comm-unpack (u w) dummy result)
323 (setq hinshi (concat hinshi (char-to-string b)))
324 (setq result (1- result)))
327 (defun cannarpc-get-lex (env bunsetsu-pos kouho-pos)
329 ylen klen rownum coldnum dicnum lex-list ll)
330 (cannarpc-call-with-environment env (dummy result)
331 (comm-format (b b w w w w w) (canna-const GetLex) 0 8 context
332 bunsetsu-pos kouho-pos 1024)
333 (comm-unpack (u w) dummy result)
337 (comm-unpack (i i i i i) ylen klen rownum coldnum dicnum)
338 (let ((ll1 (cons (list ylen klen rownum coldnum dicnum) nil)))
340 (setq ll (setcdr ll ll1))
341 (setq lex-list (setq ll ll1))))
345 (defun cannarpc-add-word (env dictionary yomi kanji hinshi)
346 "Register a word KANJI into DICTIONARY with a pronunciation YOMI and
347 a part of speech HINSHI. Where DICTIONARY should be an integer."
348 (let* ((word-info (concat yomi " " hinshi " " kanji))
349 (word-info-ext (encode-coding-string word-info 'fixed-euc-jp))
350 (length (+ (length word-info-ext) (length dictionary) 5)))
351 (cannarpc-call-with-environment env (dummy result)
352 (comm-format (b b w w S s) (canna-const DefineWord) 0 length context
353 word-info dictionary)
354 (comm-unpack (u b) dummy result)
359 (defun cannarpc-delete-word (env dictionary yomi kanji hinshi)
360 "Delete the registered word KANJI from DICTIONARY with a
361 pronunciation YOMI and a part of speech HINSHI. Where DICTIONARY
362 should be an integer."
363 (let* ((word-info (concat yomi " " hinshi " " kanji))
364 (word-info-ext (encode-coding-string word-info 'fixed-euc-jp))
365 (length (+ (length word-info-ext) (length dictionary) 5)))
366 (cannarpc-call-with-environment env (dummy result)
367 (comm-format (b b w w S s) (canna-const DeleteWord) 0 length context
368 word-info dictionary)
369 (comm-unpack (u b) dummy result)
374 (defun cannarpc-notice-group-name (proc context group)
375 (comm-call-with-proc proc (dummy result)
376 (comm-format (b b w u w s) (canna-const NoticeGroupName) 0
377 (+ (length group) 7) 0 ;; mode = 0
379 (comm-unpack (u b) dummy result)
384 (defun cannarpc-set-app-name (proc context name)
385 (comm-call-with-proc proc (dummy result)
386 (comm-format (b b w u w s) (canna-const SetApplicationName) 0
387 (+ (length name) 7) 0 context name)
388 (comm-unpack (u b) dummy result)
393 ;;; egg/cannarpc.el ends here.