1 ;;; egg/canna.el --- Canna Support (high level interface) in
2 ;;; Egg Input Method Architecture
4 ;; Copyright (C) 1998 Mule Project,
5 ;; Powered by Electrotechnical Laboratory, JAPAN.
6 ;; Project Leader: Satoru Tomura <tomura@etl.go.jp>
8 ;; Author: NIIBE Yutaka <gniibe@mri.co.jp>
9 ;; Maintainer: NIIBE Yutaka <gniibe@mri.co.jp>
11 ;; This file is part of EGG.
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)
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.
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.
32 (defconst canna-support-languages '("Japanese"))
35 (defmacro CANNA-const (c)
36 (cond ((eq c 'FileNotExist) xxxxxxxxxxxxxx)
39 (defconst canna-conversion-backend
42 canna-start-conversion
43 canna-get-bunsetsu-converted
44 canna-get-bunsetsu-source
46 canna-get-number-of-candidates
47 canna-get-current-candidate-number
48 canna-get-all-candidates
49 canna-decide-candidate
50 canna-change-bunsetsu-length
57 (defconst canna-server-port 5680 "Port number of Canna server")
58 (defvar canna-hostname "localhost"
59 "Hostname of Canna server")
61 (defun canna-open (hostname)
62 "Establish the connection to CANNA server. Return environment object."
63 (let* ((buf (generate-new-buffer " *CANNA*"))
64 (proc (open-network-stream "CANNA" buf hostname canna-server-port))
66 (process-kill-without-query proc)
67 (set-process-coding-system proc 'no-conversion 'no-conversion)
68 (set-marker-insertion-type (process-mark proc) t)
73 (setq enable-multibyte-characters nil))
74 (setq result (cannarpc-open proc (user-login-name)))
76 (let ((msg (cannarpc-get-error-message (- result))))
79 (error "Can't open CANNA session (%s): %s" hostname msg)))
80 (vector proc result)))
82 ;; XXX: Should support multiple outstanding context
83 ;; <env> ::= [ <proc> <context> ]
84 (defvar canna-environment nil
85 "Environment for CANNA kana-kanji conversion")
87 (defsubst cannaenv-get-proc (env)
89 (defsubst cannaenv-get-context (env)
93 ;; [ <env> <converted> <bunsetsu-pos>
94 ;; <source> <zenkouho-pos> <zenkouho> ]
95 (defsubst canna-make-bunsetsu (env converted bunsetsu-pos)
96 (vector env converted bunsetsu-pos nil nil nil))
98 (defsubst cannabunsetsu-get-env (b)
100 (defsubst cannabunsetsu-get-converted (b)
102 (defsubst cannabunsetsu-get-bunsetsu-pos (b)
104 (defsubst cannabunsetsu-get-source (b)
106 (defsubst cannabunsetsu-set-source (b s)
108 (defsubst cannabunsetsu-get-zenkouho-pos (b)
110 (defsubst cannabunsetsu-set-zenkouho-pos (b p)
112 (defsubst cannabunsetsu-get-zenkouho (b)
114 (defsubst cannabunsetsu-set-zenkouho (b z)
117 (defun canna-get-bunsetsu-source (b)
118 (let ((s (cannabunsetsu-get-source b)))
120 (let* ((env (cannabunsetsu-get-env b))
121 (bp (cannabunsetsu-get-bunsetsu-pos b))
122 (s (cannarpc-get-bunsetsu-source env bp)))
123 (cannabunsetsu-set-source b s)))))
125 (defun canna-get-bunsetsu-converted (b)
126 (cannabunsetsu-get-converted b))
128 (defconst canna-dictionary-specification
136 "Dictionary specification of CANNA.")
138 (defun canna-filename (p)
140 (cond ((consp p) (concat (car p) "/" (user-login-name)))
143 (defun canna-get-environment ()
144 "Return the backend of CANNA environment."
145 (if canna-environment
147 (let* ((env (canna-open canna-hostname))
148 (l canna-dictionary-specification)
154 (canna-open-dictionary env (canna-filename dic)))
156 (error "Damedamedame") ; XXX
158 (setq canna-environment env))))
160 (defun canna-open-dictionary (env name)
164 (setq ret (cannarpc-open-dictionary env name 0)) ; XXX MODE=0
167 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s
\e(B" name)
168 (setq ret (- ret)) ; Get error code.
170 (format "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s!#:n$j$^$9$+
\e(B? "
172 (= (cannarpc-make-dictionary env name) 0))
173 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$r:n$j$^$7$?
\e(B" name)
180 (defun canna-start-conversion (yomi lang)
181 "Convert YOMI string to kanji, and enter conversion mode.
182 Return the list of bunsetsu."
183 (let ((env (canna-get-environment)))
184 (cannarpc-begin-conversion env yomi)))
186 (defun canna-end-conversion (bunsetsu-list abort)
187 (let* ((env (cannabunsetsu-get-env (car bunsetsu-list)))
189 (len (length bunsetsu-list))
190 (zenkouho-pos-vector (make-vector (* 2 len) 0))
192 (mode 1) ;XXX MODE=1 attru?
193 bunsetsu zenkouho-pos)
197 (setq bunsetsu (car l))
199 (setq zenkouho-pos (cannabunsetsu-get-zenkouho-pos bunsetsu))
200 (if (null zenkouho-pos)
201 () ; XXX: NIL--> 0 atteru???
202 (aset zenkouho-pos-vector i 0) ; XXX Don't support >=256
203 (aset zenkouho-pos-vector (1+ i) zenkouho-pos))
205 (cannarpc-end-conversion env len zenkouho-pos-vector 0)))
207 (defun canna-list-candidates (bunsetsu prev-bunsetsu)
208 (let* ((env (cannabunsetsu-get-env bunsetsu))
209 (bunsetsu-pos (cannabunsetsu-get-bunsetsu-pos bunsetsu))
210 (z (cannarpc-get-bunsetsu-candidates env bunsetsu-pos)))
211 (cannabunsetsu-set-zenkouho bunsetsu z)
212 (cannabunsetsu-set-zenkouho-pos bunsetsu 0)
215 (defun canna-get-number-of-candidates (bunsetsu)
216 (let ((l (cannabunsetsu-get-zenkouho bunsetsu)))
221 (defun canna-decide-candidate (bunsetsu candidate-pos)
222 (let* ((candidate-list (cannabunsetsu-get-zenkouho bunsetsu))
223 (candidate (nth candidate-pos candidate-list)))
224 (cannabunsetsu-set-zenkouho candidate candidate-list)
225 (cannabunsetsu-set-zenkouho-pos candidate candidate-pos)
228 (defun canna-get-current-candidate-number (bunsetsu)
229 (cannabunsetsu-get-zenkouho-pos bunsetsu))
231 (defun canna-get-all-candidates (bunsetsu)
232 (let* ((l (cannabunsetsu-get-zenkouho bunsetsu))
233 (result (cons nil nil))
237 (let ((candidate (car l)))
238 (setcar r (cannabunsetsu-get-converted candidate))
239 (if (null (setq l (cdr l)))
241 (setq r (setcdr r (cons nil nil)))))))
244 ;;;;;;;;;;;;;;;;;;;;;;; MADAMADA zenzendame, just copy from SJ3
245 (defun canna-change-bunsetsu-length (b0 b1 b2 len)
247 (cannabunsetsu-get-source b1)
248 (if b2 (cannabunsetsu-get-source b2))))
249 (env (cannabunsetsu-get-env b1))
252 (setq yomi1 (substring yomi 0 len)
253 yomi2 (substring yomi len))
255 (cannarpc-tanbunsetsu-conversion env yomi1))
256 ;; Only set once (memory original length of the bunsetsu).
257 (cannabunsetsu-set-kugiri-changed bunsetsu1
258 (or (cannabunsetsu-get-kugiri-changed b1)
259 (length (cannabunsetsu-get-source b1))))
260 (if (< 0 (length yomi2))
261 (setq bunsetsu2 (cannarpc-tanbunsetsu-conversion env yomi2))
262 (setq bunsetsu2 nil))
264 (list bunsetsu1 bunsetsu2)
267 ;;;;;;;;;;;;;; MADAMADA
268 (defun canna-fini (lang)
274 (load "egg/cannarpc")
277 (defun egg-activate-canna (&rest arg)
278 "Activate CANNA backend of Tamagotchy."
279 (egg-set-support-languages canna-support-languages)
280 (egg-set-conversion-backend canna-conversion-backend
281 canna-support-languages
283 (apply 'egg-mode arg))
285 ;;; egg/canna.el ends here.