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.
34 (defconst sj3-support-languages '(Japanese))
37 (defmacro SJ3-const (c)
38 (cond ((eq c 'FileNotExist) 35)
41 (defconst sj3-conversion-backend
45 sj3-get-bunsetsu-converted
46 sj3-get-bunsetsu-source
48 sj3-get-number-of-candidates
49 sj3-get-current-candidate-number
50 sj3-get-all-candidates
52 sj3-change-bunsetsu-length
59 (defvar sj3-server-port 3000 "Port number of SJ3 server")
60 (defvar sj3-stdy-size 0 "STDYSIZE of SJ3 server")
61 (defvar sj3-hostname "localhost"
62 "Hostname of SJ3 server")
64 (defvar sj3-open-message)
66 (defun sj3-open (hostname)
67 "Establish the connection to SJ3 server. Return process object."
68 (let* ((buf (generate-new-buffer " *SJ3*"))
69 (proc (open-network-stream "SJ3" buf hostname sj3-server-port))
71 (process-kill-without-query proc)
72 (set-process-coding-system proc 'no-conversion 'no-conversion)
73 (set-marker-insertion-type (process-mark proc) t)
78 (set-buffer-multibyte nil))
79 ;; Initialize dictionaries
80 (setq sj3-sys-dict-list nil)
81 (setq sj3-user-dict-list nil)
82 (setq result (sj3rpc-open proc (system-name) (user-login-name)))
84 (let ((msg (sj3rpc-get-error-message (- result))))
87 (error "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))))
93 (error "Can't get SJ3 STDYSIZE: %s"msg)))
94 (setq sj3-stdy-size result)
97 ;; (defun sj3-open (hostname-list)
98 ;; "Establish the connection to SJ3 server. Return process object."
99 ;; (let* ((buf (generate-new-buffer " *SJ3*"))
100 ;; (msg-form "SJ3: connecting to sj3serv at %s...")
101 ;; hostname proc result msg)
105 ;; (buffer-disable-undo)
106 ;; (setq enable-multibyte-characters nil))
108 ;; ((null hostname-list)
109 ;; (setq hostname-list '("localhost")))
110 ;; ((null (listp hostname-list))
111 ;; (setq hostname-list (list hostname-list))))
112 ;; (while (and hostname-list (null proc))
113 ;; (setq hostname (car hostname-list)
114 ;; hostname-list (cdr hostname-list))
115 ;; (message msg-form hostname)
117 ;; (condition-case result
118 ;; (setq proc (open-network-stream "SJ3" buf hostname sj3-server-port))
122 ;; (process-kill-without-query proc)
123 ;; (set-process-coding-system proc 'no-conversion 'no-conversion)
124 ;; (set-marker-insertion-type (process-mark proc) t)
125 ;; ;; Initialize dictionaries
126 ;; (setq sj3-sys-dict-list nil)
127 ;; (setq sj3-user-dict-list nil)
128 ;; (setq result (sj3rpc-open proc (system-name) (user-login-name)))
131 ;; (delete-process proc)
133 ;; msg (format "Can't open SJ3 session (%s): %s"
135 ;; (setq result (sj3rpc-get-stdy-size proc))
138 ;; (delete-process proc)
140 ;; msg (format "Can't get SJ3 STDYSIZE: %s"
141 ;; (sj3rpc-get-error-message (- result)))))
142 ;; (setq sj3-stdy-size result))))))
145 ;; (setq sj3-open-message (format (concat msg-form "done") hostname))
148 ;; (error "%s" (or msg "no sj3serv available")))))
150 ;; <env> ::= [ <proc> <dictionary-list> ]
151 (defvar sj3-environment nil
152 "Environment for SJ3 kana-kanji conversion")
154 (defsubst sj3env-get-proc (env)
156 (defsubst sj3env-get-dictionary-list (env)
160 ;; [ <env> <source> <converted> <rest>
161 ;; <stdy> <zenkouho> <zenkouho-pos> <kugiri-changed> ]
162 (defsubst sj3-make-bunsetsu (env source converted rest stdy)
163 (vector env source converted rest stdy nil nil nil))
165 (defsubst sj3bunsetsu-get-env (b)
167 (defsubst sj3bunsetsu-get-source (b)
169 (defsubst sj3bunsetsu-get-converted (b)
171 (defsubst sj3bunsetsu-get-rest (b)
173 (defsubst sj3bunsetsu-get-stdy (b)
175 (defsubst sj3bunsetsu-get-zenkouho (b)
177 (defsubst sj3bunsetsu-set-zenkouho (b z)
179 (defsubst sj3bunsetsu-get-zenkouho-pos (b)
181 (defsubst sj3bunsetsu-set-zenkouho-pos (b p)
183 (defsubst sj3bunsetsu-get-kugiri-changed (b)
185 (defsubst sj3bunsetsu-set-kugiri-changed (b s)
188 (defun sj3-get-bunsetsu-source (b)
189 (sj3bunsetsu-get-source b))
190 (defun sj3-get-bunsetsu-converted (b)
191 (concat (sj3bunsetsu-get-converted b)
192 (sj3bunsetsu-get-rest b)))
193 (defun sj3-get-bunsetsu-stdy (b)
194 (sj3bunsetsu-get-stdy b))
196 (defvar sj3-dictionary-specification
199 [("private.dic") ""])
200 "Dictionary specification of SJ3.")
202 (defvar sj3-usr-dic-dir (concat "user/" (user-login-name))
203 "*Directory of user dictionary for SJ3.")
205 (defun sj3-filename (p)
207 (cond ((consp p) (concat sj3-usr-dic-dir "/" (car p)))
210 (defun sj3-get-environment ()
211 "Return the backend of SJ3 environment."
214 (let* ((proc (sj3-open sj3-hostname))
215 (freq-info-name (sj3-filename (car sj3-dictionary-specification)))
216 (l (cdr sj3-dictionary-specification))
218 (sj3-open-freq-info proc freq-info-name)
223 (sj3-open-dictionary proc (sj3-filename (aref dic 0))
226 (error "Dame2") ; XXX
227 (setq dict-list (cons dic-id dict-list)
229 (setq sj3-environment (vector proc dict-list)))))
231 (defun sj3-open-freq-info (proc name)
235 (setq ret (sj3rpc-open-stdy proc name))
238 (message "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s
\e(B" name)
239 (if (/= ret (SJ3-const FileNotExist))
240 (error "Fatal1") ; XXX
242 (format "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s!#:n$j$^$9$+
\e(B? "
244 (sj3rpc-make-directory proc
245 (file-name-directory name))
247 (= (sj3rpc-make-stdy proc name) 0))
248 (message "
\e$B3X=,%U%!%$%k
\e(B(%s)
\e$B$r:n$j$^$7$?
\e(B" name)
249 (error "Fatal2"))))))) ; XXX
251 (defun sj3-open-dictionary (proc name passwd)
255 (setq ret (sj3rpc-open-dictionary proc name passwd))
258 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s
\e(B" name)
259 (setq ret (- ret)) ; Get error code.
260 (if (/= ret (SJ3-const FileNotExist))
261 (error "Fatal3 %d" ret) ; XXX
263 (format "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$,$"$j$^$;$s!#:n$j$^$9$+
\e(B? "
265 (= (sj3rpc-make-dictionary proc name) 0))
266 (message "
\e$B<-=q%U%!%$%k
\e(B(%s)
\e$B$r:n$j$^$7$?
\e(B" name)
267 (error "Fatal4"))))) ; XXX
273 (defun sj3-start-conversion (yomi &optional lang)
274 "Convert YOMI string to kanji, and enter conversion mode.
275 Return the list of bunsetsu."
276 (let ((env (sj3-get-environment)))
277 (sj3rpc-begin env yomi)))
279 (defun sj3-end-conversion (bunsetsu-list abort)
282 (let ((env (sj3bunsetsu-get-env (car bunsetsu-list)))
284 bunsetsu stdy kugiri-changed)
286 (setq bunsetsu (car l))
288 (setq stdy (sj3bunsetsu-get-stdy bunsetsu))
290 (sj3rpc-bunsetsu-stdy env stdy))
291 (if (setq kugiri-changed (sj3bunsetsu-get-kugiri-changed bunsetsu))
292 (let ((yomi1 (sj3bunsetsu-get-source bunsetsu))
293 (yomi2 (sj3bunsetsu-get-source (car l))))
294 (if (/= kugiri-changed (length yomi1))
295 (sj3rpc-kugiri-stdy env yomi1 yomi2
296 (sj3bunsetsu-get-stdy (car l))))))))))
298 (defun sj3-list-candidates (bunsetsu prev-bunsetsu)
299 (let* ((env (sj3bunsetsu-get-env bunsetsu))
300 (yomi (sj3bunsetsu-get-source bunsetsu))
301 (z (sj3rpc-get-bunsetsu-candidates env yomi)))
302 (sj3bunsetsu-set-zenkouho bunsetsu z)
303 (sj3bunsetsu-set-zenkouho-pos bunsetsu 0)
306 (defun sj3-get-number-of-candidates (bunsetsu)
307 (let ((l (sj3bunsetsu-get-zenkouho bunsetsu)))
312 (defun sj3-decide-candidate (bunsetsu candidate-pos)
313 (let* ((candidate-list (sj3bunsetsu-get-zenkouho bunsetsu))
314 (candidate (nth candidate-pos candidate-list)))
315 (sj3bunsetsu-set-zenkouho candidate candidate-list)
316 (sj3bunsetsu-set-zenkouho-pos candidate candidate-pos)
319 (defun sj3-get-current-candidate-number (bunsetsu)
320 (sj3bunsetsu-get-zenkouho-pos bunsetsu))
322 (defun sj3-get-all-candidates (bunsetsu)
323 (let* ((l (sj3bunsetsu-get-zenkouho bunsetsu))
324 (result (cons nil nil))
328 (let ((candidate (car l)))
329 (setcar r (sj3bunsetsu-get-converted candidate))
330 (if (null (setq l (cdr l)))
332 (setq r (setcdr r (cons nil nil)))))))
335 (defun sj3-change-bunsetsu-length (b0 b1 b2 len)
337 (sj3bunsetsu-get-source b1)
338 (if b2 (sj3bunsetsu-get-source b2))))
339 (env (sj3bunsetsu-get-env b1))
342 (setq yomi1 (substring yomi 0 len)
343 yomi2 (substring yomi len))
345 (sj3rpc-tanbunsetsu-conversion env yomi1))
346 ;; Only set once (memory original length of the bunsetsu).
347 (sj3bunsetsu-set-kugiri-changed bunsetsu1
348 (or (sj3bunsetsu-get-kugiri-changed b1)
349 (length (sj3bunsetsu-get-source b1))))
350 (if (< 0 (length yomi2))
351 (setq bunsetsu2 (sj3rpc-tanbunsetsu-conversion env yomi2))
352 (setq bunsetsu2 nil))
354 (list bunsetsu1 bunsetsu2)
357 (defun sj3-fini (lang)
358 (let ((proc (sj3env-get-proc sj3-environment))
359 (dict-list (sj3env-get-dictionary-list sj3-environment))
362 (setq dict (car dict-list))
363 (setq dict-list (cdr dict-list))
364 (sj3rpc-close-dictionary proc dict)) ; XXX: check error
365 (sj3rpc-close-stdy proc)
367 (setq sj3-environment nil))
375 (defun egg-activate-sj3 (&rest arg)
376 "Activate SJ3 backend of Tamagotchy."
377 (egg-set-support-languages sj3-support-languages)
378 (egg-set-conversion-backend sj3-conversion-backend
379 sj3-support-languages)
380 (apply 'egg-mode arg))
382 ;;; egg/sj3.el ends here.