1 ;;; egg/sj3.el --- SJ3 Support (high level interface) in Egg
2 ;;; 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 "SJ3 interface for Tamago 4"
41 (defcustom sj3-hostname "localhost"
42 "*Hostname of SJ3 server"
43 :group 'sj3 :type 'string)
45 (defcustom sj3-server-port 3086
46 "*Port number of SJ3 server"
47 :group 'sj3 :type 'integer)
51 (defmacro SJ3-const (c)
52 (cond ((eq c 'FileNotExist) 35)
55 (setplist 'sj3-conversion-backend
56 '(egg-start-conversion sj3-start-conversion
57 egg-get-bunsetsu-source sj3-get-bunsetsu-source
58 egg-get-bunsetsu-converted sj3-get-bunsetsu-converted
59 egg-get-source-language sj3-get-source-language
60 egg-get-converted-language sj3-get-converted-language
61 egg-list-candidates sj3-list-candidates
62 egg-decide-candidate sj3-decide-candidate
63 egg-change-bunsetsu-length sj3-change-bunsetsu-length
64 egg-end-conversion sj3-end-conversion))
66 (defconst sj3-backend-alist '((Japanese ((sj3-conversion-backend)))))
68 (egg-set-finalize-backend '(sj3-finalize-backend))
70 (defvar sj3-stdy-size 0 "STDYSIZE of SJ3 server")
72 (defvar sj3-open-message)
74 (defun sj3-open (hostname)
75 "Establish the connection to SJ3 server. Return process object."
76 (let* ((buf (generate-new-buffer " *SJ3*"))
79 (setq proc (open-network-stream "SJ3" buf hostname sj3-server-port))
81 (egg-error "failed to connect sj3 server")))
82 (process-kill-without-query proc)
83 (set-process-coding-system proc 'no-conversion 'no-conversion)
84 (set-marker-insertion-type (process-mark proc) t)
89 (set-buffer-multibyte nil))
90 ;; Initialize dictionaries
91 (setq sj3-sys-dict-list nil)
92 (setq sj3-user-dict-list nil)
93 (setq result (sj3rpc-open proc (system-name) (user-login-name)))
95 (let ((msg (sj3rpc-get-error-message (- result))))
98 (egg-error "Can't open SJ3 session (%s): %s" hostname msg)))
99 (setq result (sj3rpc-get-stdy-size proc))
101 (let ((msg (sj3rpc-get-error-message (- result))))
102 (delete-process proc)
104 (egg-error "Can't get SJ3 STDYSIZE: %s"msg)))
105 (setq sj3-stdy-size result)
108 ;; (defun sj3-open (hostname-list)
109 ;; "Establish the connection to SJ3 server. Return process object."
110 ;; (let* ((buf (generate-new-buffer " *SJ3*"))
111 ;; (msg-form "SJ3: connecting to sj3serv at %s...")
112 ;; hostname proc result msg)
116 ;; (buffer-disable-undo)
117 ;; (setq enable-multibyte-characters nil))
119 ;; ((null hostname-list)
120 ;; (setq hostname-list '("localhost")))
121 ;; ((null (listp hostname-list))
122 ;; (setq hostname-list (list hostname-list))))
123 ;; (while (and hostname-list (null proc))
124 ;; (setq hostname (car hostname-list)
125 ;; hostname-list (cdr hostname-list))
126 ;; (message msg-form hostname)
128 ;; (condition-case result
129 ;; (setq proc (open-network-stream "SJ3" buf hostname sj3-server-port))
133 ;; (process-kill-without-query proc)
134 ;; (set-process-coding-system proc 'no-conversion 'no-conversion)
135 ;; (set-marker-insertion-type (process-mark proc) t)
136 ;; ;; Initialize dictionaries
137 ;; (setq sj3-sys-dict-list nil)
138 ;; (setq sj3-user-dict-list nil)
139 ;; (setq result (sj3rpc-open proc (system-name) (user-login-name)))
142 ;; (delete-process proc)
144 ;; msg (format "Can't open SJ3 session (%s): %s"
146 ;; (setq result (sj3rpc-get-stdy-size proc))
149 ;; (delete-process proc)
151 ;; msg (format "Can't get SJ3 STDYSIZE: %s"
152 ;; (sj3rpc-get-error-message (- result)))))
153 ;; (setq sj3-stdy-size result))))))
156 ;; (setq sj3-open-message (format (concat msg-form "done") hostname))
159 ;; (error "%s" (or msg "no sj3serv available")))))
161 ;; <env> ::= [ <proc> <dictionary-list> ]
162 (defvar sj3-environment nil
163 "Environment for SJ3 kana-kanji conversion")
165 (defsubst sj3env-get-proc (env)
167 (defsubst sj3env-get-dictionary-list (env)
170 ;; <sj3-bunsetsu> ::=
171 ;; [ <env> <source> <converted> <rest> <stdy>
172 ;; <zenkouho> <zenkouho-pos> <zenkouho-converted>
173 ;; <kugiri-changed> ]
174 (defsubst sj3-make-bunsetsu (env source converted rest stdy)
176 'sj3-conversion-backend
177 (vector env source converted rest stdy nil nil nil nil nil)))
179 (defsubst sj3bunsetsu-get-env (b)
180 (aref (egg-bunsetsu-get-info b) 0))
181 (defsubst sj3bunsetsu-get-source (b)
182 (aref (egg-bunsetsu-get-info b) 1))
183 (defsubst sj3bunsetsu-get-converted (b)
184 (aref (egg-bunsetsu-get-info b) 2))
185 (defsubst sj3bunsetsu-get-rest (b)
186 (aref (egg-bunsetsu-get-info b) 3))
187 (defsubst sj3bunsetsu-get-stdy (b)
188 (aref (egg-bunsetsu-get-info b) 4))
190 (defsubst sj3bunsetsu-get-zenkouho (b)
191 (aref (egg-bunsetsu-get-info b) 5))
192 (defsubst sj3bunsetsu-set-zenkouho (b z)
193 (aset (egg-bunsetsu-get-info b) 5 z))
195 (defsubst sj3bunsetsu-get-zenkouho-pos (b)
196 (aref (egg-bunsetsu-get-info b) 6))
197 (defsubst sj3bunsetsu-set-zenkouho-pos (b zp)
198 (aset (egg-bunsetsu-get-info b) 6 zp))
200 (defsubst sj3bunsetsu-get-zenkouho-converted (b)
201 (aref (egg-bunsetsu-get-info b) 7))
202 (defsubst sj3bunsetsu-set-zenkouho-converted (b zc)
203 (aset (egg-bunsetsu-get-info b) 7 zc))
205 (defsubst sj3bunsetsu-get-kugiri-changed (b)
206 (aref (egg-bunsetsu-get-info b) 8))
207 (defsubst sj3bunsetsu-set-kugiri-changed (b s)
208 (aset (egg-bunsetsu-get-info b) 8 s))
210 (defun sj3-get-bunsetsu-source (b)
211 (sj3bunsetsu-get-source b))
213 (defun sj3-get-bunsetsu-converted (b)
214 (concat (sj3bunsetsu-get-converted b) (sj3bunsetsu-get-rest b)))
216 (defun sj3-get-source-language (b) 'Japanese)
217 (defun sj3-get-converted-language (b) 'Japanese)
218 (defun sj3-get-bunsetsu-stdy (b) (sj3bunsetsu-get-stdy b))
220 (defvar sj3-dictionary-specification
223 [("private.dic") ""])
224 "Dictionary specification of SJ3.")
226 (defvar sj3-usr-dic-dir (concat "user/" (user-login-name))
227 "*Directory of user dictionary for SJ3.")
229 (defun sj3-filename (p)
231 (cond ((consp p) (concat sj3-usr-dic-dir "/" (car p)))
234 (defun sj3-get-environment ()
235 "Return the backend of SJ3 environment."
238 (let* ((proc (sj3-open sj3-hostname))
239 (freq-info-name (sj3-filename (car sj3-dictionary-specification)))
240 (l (cdr sj3-dictionary-specification))
242 (sj3-open-freq-info proc freq-info-name)
247 (sj3-open-dictionary proc (sj3-filename (aref dic 0))
250 (egg-error "Dame2") ; XXX
251 (setq dict-list (cons dic-id dict-list)
253 (setq sj3-environment (vector proc dict-list)))))
255 (defun sj3-open-freq-info (proc name)
259 (setq ret (sj3rpc-open-stdy proc name))
262 (message "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s
\e(B" name)
263 (if (/= ret (SJ3-const FileNotExist))
264 (egg-error "Fatal1") ; XXX
266 (format "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s!#:n$j$^$9$+
\e(B? "
268 (sj3rpc-make-directory proc
269 (file-name-directory name))
271 (= (sj3rpc-make-stdy proc name) 0))
272 (message "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$r:n$j$^$7$?
\e(B" name)
273 (egg-error "Fatal2"))))))) ; XXX
275 (defun sj3-open-dictionary (proc name passwd)
279 (setq ret (sj3rpc-open-dictionary proc name passwd))
282 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s
\e(B" name)
283 (setq ret (- ret)) ; Get error code.
284 (if (/= ret (SJ3-const FileNotExist))
285 (egg-error "Fatal3 %d" ret) ; XXX
287 (format "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s!#:n$j$^$9$+
\e(B? "
289 (= (sj3rpc-make-dictionary proc name) 0))
290 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$r:n$j$^$7$?
\e(B" name)
291 (egg-error "Fatal4"))))) ; XXX
294 (defun sj3-start-conversion (backend yomi &optional context)
295 "Convert YOMI string to kanji, and enter conversion mode.
296 Return the list of bunsetsu."
297 (let ((env (sj3-get-environment)))
298 (sj3rpc-begin env yomi)))
300 (defun sj3-end-conversion (bunsetsu-list abort)
303 (let ((env (sj3bunsetsu-get-env (car bunsetsu-list)))
305 bunsetsu stdy kugiri-changed)
307 (setq bunsetsu (car l))
309 (setq stdy (sj3bunsetsu-get-stdy bunsetsu))
311 (sj3rpc-bunsetsu-stdy env stdy))
312 (if (setq kugiri-changed (sj3bunsetsu-get-kugiri-changed bunsetsu))
313 (let ((yomi1 (sj3bunsetsu-get-source bunsetsu))
314 (yomi2 (sj3bunsetsu-get-source (car l))))
315 (if (/= kugiri-changed (length yomi1))
316 (sj3rpc-kugiri-stdy env yomi1 yomi2
317 (sj3bunsetsu-get-stdy (car l))))))))))
319 (defun sj3-list-candidates (bunsetsu prev-bunsetsu next-bunsetsu major)
320 (setq bunsetsu (car bunsetsu))
321 (if (sj3bunsetsu-get-zenkouho bunsetsu)
322 (cons (sj3bunsetsu-get-zenkouho-pos bunsetsu)
323 (sj3bunsetsu-get-zenkouho-converted bunsetsu))
324 (let* ((env (sj3bunsetsu-get-env bunsetsu))
325 (yomi (sj3bunsetsu-get-source bunsetsu))
326 (z (sj3rpc-get-bunsetsu-candidates env yomi)))
327 (sj3bunsetsu-set-zenkouho bunsetsu z)
328 (cons (sj3bunsetsu-set-zenkouho-pos bunsetsu 0)
329 (sj3bunsetsu-set-zenkouho-converted
331 (mapcar 'sj3bunsetsu-get-converted z))))))
333 (defun sj3-decide-candidate (bunsetsu candidate-pos prev-b next-b)
334 (setq bunsetsu (car bunsetsu))
335 (let* ((candidate-list (sj3bunsetsu-get-zenkouho bunsetsu))
336 (candidate (nth candidate-pos candidate-list)))
337 (sj3bunsetsu-set-zenkouho candidate candidate-list)
338 (sj3bunsetsu-set-zenkouho-pos candidate candidate-pos)
339 (sj3bunsetsu-set-zenkouho-converted
340 candidate (sj3bunsetsu-get-zenkouho-converted bunsetsu))
341 (list (list candidate))))
343 (defun sj3-change-bunsetsu-length (bunsetsu prev-b next-b len major)
344 (let ((yomi (apply 'concat (mapcar 'sj3bunsetsu-get-source bunsetsu)))
345 (env (sj3bunsetsu-get-env (car bunsetsu)))
348 (setq yomi1 (substring yomi 0 len)
349 yomi2 (substring yomi len))
350 (setq new (sj3rpc-tanbunsetsu-conversion env yomi1))
351 ;; Only set once (memory original length of the bunsetsu).
352 (sj3bunsetsu-set-kugiri-changed new
353 (or (sj3bunsetsu-get-kugiri-changed old)
354 (length (sj3bunsetsu-get-source old))))
355 (if (> (length yomi2) 0)
356 (list (list new (sj3rpc-tanbunsetsu-conversion env yomi2)))
359 (defun sj3-finalize-backend ()
361 (let ((proc (sj3env-get-proc sj3-environment))
362 (dict-list (sj3env-get-dictionary-list sj3-environment))
365 (setq dict (car dict-list))
366 (setq dict-list (cdr dict-list))
367 (sj3rpc-close-dictionary proc dict)) ; XXX: check error
368 (sj3rpc-close-stdy proc)
370 (setq sj3-environment nil))))
375 (run-hooks 'sj3-load-hook)
378 (defun egg-activate-sj3 (&rest arg)
379 "Activate SJ3 backend of Tamagotchy."
380 (apply 'egg-mode (append arg sj3-backend-alist)))
382 ;;; egg/sj3.el ends here.