1 ;;; egg/sj3.el --- SJ3 Support (high level interface) in Egg
2 ;;; Input Method Architecture
4 ;; Copyright (C) 1997, 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.
35 (defmacro SJ3-const (c)
36 (cond ((eq c 'FileNotExist) 35)
39 (defconst sj3-conversion-backend
43 sj3-get-bunsetsu-converted
44 sj3-get-bunsetsu-source
46 sj3-get-number-of-candidates
47 sj3-get-current-candidate-number
48 sj3-get-all-candidates
50 sj3-change-bunsetsu-length
57 (defvar sj3-server-port 3000 "Port number of SJ3 server")
58 (defvar sj3-stdy-size 0 "STDYSIZE of SJ3 server (will be set on connection)")
59 (defvar sj3-hostname "localhost"
60 "Hostname of SJ3 server")
62 (defvar sj3-open-message)
64 (defun sj3-open (hostname)
65 "Establish the connection to SJ3 server. Return process object."
66 (let ((buf (generate-new-buffer " *SJ3*"))
69 (setq proc (open-network-stream "SJ3" buf hostname sj3-server-port))
72 (signal 'file-error (cdr err)))) ; Re-raise the signal with file-error
73 (process-kill-without-query proc)
74 (set-process-coding-system proc 'no-conversion 'no-conversion)
75 (set-marker-insertion-type (process-mark proc) t)
80 (set-buffer-multibyte nil))
81 (setq result (sj3rpc-open proc (system-name) (user-login-name)))
83 (let ((msg (sj3rpc-get-error-message (- result))))
87 (list (format "Can't open SJ3 session (%s): %s" hostname msg))))
88 (setq result (sj3rpc-get-stdy-size proc))
90 (let ((msg (sj3rpc-get-error-message (- result))))
94 (list (format "Can't get SJ3 STDYSIZE: %s" msg)))))
95 (setq sj3-stdy-size result)
98 ;; <env> ::= [ <proc> <dictionary-list> ]
99 (defvar sj3-environment nil
100 "Environment for SJ3 kana-kanji conversion")
102 (defsubst sj3env-get-proc (env)
104 (defsubst sj3env-get-dictionary-list (env)
108 ;; [ <env> <source> <converted> <rest>
109 ;; <stdy> <zenkouho> <zenkouho-pos> <kugiri-changed> ]
110 (defsubst sj3-make-bunsetsu (env source converted rest stdy)
111 (vector env source converted rest stdy nil nil nil))
113 (defsubst sj3bunsetsu-get-env (b)
115 (defsubst sj3bunsetsu-get-source (b)
117 (defsubst sj3bunsetsu-get-converted (b)
119 (defsubst sj3bunsetsu-get-rest (b)
121 (defsubst sj3bunsetsu-get-stdy (b)
123 (defsubst sj3bunsetsu-get-zenkouho (b)
125 (defsubst sj3bunsetsu-set-zenkouho (b z)
127 (defsubst sj3bunsetsu-get-zenkouho-pos (b)
129 (defsubst sj3bunsetsu-set-zenkouho-pos (b p)
131 (defsubst sj3bunsetsu-get-kugiri-changed (b)
133 (defsubst sj3bunsetsu-set-kugiri-changed (b s)
136 (defun sj3-get-bunsetsu-source (b)
137 (sj3bunsetsu-get-source b))
138 (defun sj3-get-bunsetsu-converted (b)
139 (concat (sj3bunsetsu-get-converted b)
140 (sj3bunsetsu-get-rest b)))
141 (defun sj3-get-bunsetsu-stdy (b)
142 (sj3bunsetsu-get-stdy b))
144 (defvar sj3-dictionary-specification
147 [("private.dic") ""])
148 "Dictionary specification of SJ3.")
150 (defvar sj3-usr-dic-dir (concat "user/" (user-login-name))
151 "*Directory of user dictionary for SJ3.")
153 (defun sj3-filename (p)
155 (cond ((consp p) (concat sj3-usr-dic-dir "/" (car p)))
158 (defun sj3-get-environment ()
159 "Return the backend of SJ3 environment."
162 (let* ((proc (sj3-open sj3-hostname))
163 (freq-info-name (sj3-filename (car sj3-dictionary-specification)))
164 (l (cdr sj3-dictionary-specification))
166 (sj3-open-freq-info proc freq-info-name)
171 (sj3-open-dictionary proc (sj3-filename (aref dic 0))
174 (error "Dame2") ; XXX
175 (setq dict-list (cons dic-id dict-list)
177 (setq sj3-environment (vector proc dict-list)))))
179 (defun sj3-open-freq-info (proc name)
183 (setq ret (sj3rpc-open-stdy proc name))
186 (message "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s
\e(B" name)
187 (if (/= ret (SJ3-const FileNotExist))
188 (error "Fatal1") ; XXX
190 (format "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s!#:n$j$^$9$+
\e(B? "
192 (sj3rpc-make-directory proc
193 (file-name-directory name))
195 (= (sj3rpc-make-stdy proc name) 0))
196 (message "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$r:n$j$^$7$?
\e(B" name)
197 (error "Fatal2"))))))) ; XXX
199 (defun sj3-open-dictionary (proc name passwd)
203 (setq ret (sj3rpc-open-dictionary proc name passwd))
206 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s
\e(B" name)
207 (setq ret (- ret)) ; Get error code.
208 (if (/= ret (SJ3-const FileNotExist))
209 (error "Fatal3 %d" ret) ; XXX
211 (format "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s!#:n$j$^$9$+
\e(B? "
213 (= (sj3rpc-make-dictionary proc name) 0))
214 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$r:n$j$^$7$?
\e(B" name)
215 (error "Fatal4"))))) ; XXX
221 (defun sj3-start-conversion (yomi &optional lang)
222 "Convert YOMI string to kanji, and enter conversion mode.
223 Return the list of bunsetsu."
224 (if (eq lang 'Japanese)
225 (let ((env (sj3-get-environment)))
226 (sj3rpc-begin env yomi))
227 (signal 'lang-not-supported)))
229 (defun sj3-end-conversion (bunsetsu-list abort)
232 (let ((env (sj3bunsetsu-get-env (car bunsetsu-list)))
234 bunsetsu stdy kugiri-changed)
236 (setq bunsetsu (car l))
238 (setq stdy (sj3bunsetsu-get-stdy bunsetsu))
240 (sj3rpc-bunsetsu-stdy env stdy))
241 (if (setq kugiri-changed (sj3bunsetsu-get-kugiri-changed bunsetsu))
242 (let ((yomi1 (sj3bunsetsu-get-source bunsetsu))
243 (yomi2 (sj3bunsetsu-get-source (car l))))
244 (if (/= kugiri-changed (length yomi1))
245 (sj3rpc-kugiri-stdy env yomi1 yomi2
246 (sj3bunsetsu-get-stdy (car l))))))))))
248 (defun sj3-list-candidates (bunsetsu prev-bunsetsu)
249 (let* ((env (sj3bunsetsu-get-env bunsetsu))
250 (yomi (sj3bunsetsu-get-source bunsetsu))
251 (z (sj3rpc-get-bunsetsu-candidates env yomi)))
252 (sj3bunsetsu-set-zenkouho bunsetsu z)
253 (sj3bunsetsu-set-zenkouho-pos bunsetsu 0)
256 (defun sj3-get-number-of-candidates (bunsetsu)
257 (let ((l (sj3bunsetsu-get-zenkouho bunsetsu)))
262 (defun sj3-decide-candidate (bunsetsu candidate-pos)
263 (let* ((candidate-list (sj3bunsetsu-get-zenkouho bunsetsu))
264 (candidate (nth candidate-pos candidate-list)))
265 (sj3bunsetsu-set-zenkouho candidate candidate-list)
266 (sj3bunsetsu-set-zenkouho-pos candidate candidate-pos)
269 (defun sj3-get-current-candidate-number (bunsetsu)
270 (sj3bunsetsu-get-zenkouho-pos bunsetsu))
272 (defun sj3-get-all-candidates (bunsetsu)
273 (let* ((l (sj3bunsetsu-get-zenkouho bunsetsu))
274 (result (cons nil nil))
278 (let ((candidate (car l)))
279 (setcar r (sj3bunsetsu-get-converted candidate))
280 (if (null (setq l (cdr l)))
282 (setq r (setcdr r (cons nil nil)))))))
285 (defun sj3-change-bunsetsu-length (b0 b1 b2 len)
287 (sj3bunsetsu-get-source b1)
288 (if b2 (sj3bunsetsu-get-source b2))))
289 (env (sj3bunsetsu-get-env b1))
292 (setq yomi1 (substring yomi 0 len)
293 yomi2 (substring yomi len))
295 (sj3rpc-tanbunsetsu-conversion env yomi1))
296 ;; Only set once (memory original length of the bunsetsu).
297 (sj3bunsetsu-set-kugiri-changed bunsetsu1
298 (or (sj3bunsetsu-get-kugiri-changed b1)
299 (length (sj3bunsetsu-get-source b1))))
300 (if (< 0 (length yomi2))
301 (setq bunsetsu2 (sj3rpc-tanbunsetsu-conversion env yomi2))
302 (setq bunsetsu2 nil))
304 (list bunsetsu1 bunsetsu2)
308 (let ((proc (sj3env-get-proc sj3-environment))
309 (dict-list (sj3env-get-dictionary-list sj3-environment))
312 (setq dict (car dict-list))
313 (setq dict-list (cdr dict-list))
314 (sj3rpc-close-dictionary proc dict)) ; XXX: check error
315 (sj3rpc-close-stdy proc)
317 (setq sj3-environment nil))
323 (defun egg-activate-sj3 (&rest arg)
324 "Activate SJ3 backend of Tamagotchy."
325 (setq egg-conversion-backend sj3-conversion-backend)
326 (if (not (fboundp 'sj3rpc-open))
327 (load-library "egg/sj3rpc"))
328 (apply 'egg-mode arg))
330 ;;; egg/sj3.el ends here.