69b5aa4d15f51f62fb702b63bc26f6db7f7a5067
[elisp/tamago.git] / egg / sj3.el
1 ;;; egg/sj3.el --- SJ3 Support (high level interface) in Egg
2 ;;;                Input Method Architecture
3
4 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc
5
6 ;; Author: NIIBE Yutaka <gniibe@chroot.org>
7
8 ;; Maintainer: TOMURA Satoru <tomura@etl.go.jp>
9
10 ;; Keywords: mule, multilingual, input method
11
12 ;; This file is part of EGG.
13
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)
17 ;; any later version.
18
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.
23
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.
28
29 ;;; Commentary:
30
31
32 ;;; Code:
33
34 (require 'egg)
35 (require 'egg-edep)
36
37 (defgroup sj3 nil
38   "SJ3 interface for Tamago 4"
39   :group 'egg)
40
41 (defcustom  sj3-hostname "localhost"
42   "*Hostname of SJ3 server"
43   :group 'sj3 :type 'string)
44
45 (defcustom  sj3-server-port 3086 
46   "*Port number of SJ3 server"
47   :group 'sj3 :type 'integer)
48
49
50 (eval-when-compile
51   (defmacro SJ3-const (c)
52     (cond ((eq c 'FileNotExist) 35)
53           )))
54
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))
65
66 (defconst sj3-backend-alist '((Japanese ((sj3-conversion-backend)))))
67
68 (egg-set-finalize-backend '(sj3-finalize-backend))
69
70 (defvar sj3-stdy-size 0 "STDYSIZE of SJ3 server")
71
72 (defvar sj3-open-message)
73
74 (defun sj3-open (hostname)
75   "Establish the connection to SJ3 server.  Return process object."
76   (let* ((buf (generate-new-buffer " *SJ3*"))
77          proc result)
78     (condition-case err
79         (setq proc (open-network-stream "SJ3" buf hostname sj3-server-port))
80       ((error quit)
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)
85     (save-excursion
86       (set-buffer buf)
87       (erase-buffer)
88       (buffer-disable-undo)
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)))
94     (if (< result 0)
95         (let ((msg (sj3rpc-get-error-message (- result))))
96           (delete-process proc)
97           (kill-buffer buf)
98           (egg-error "Can't open SJ3 session (%s): %s" hostname msg)))
99     (setq result (sj3rpc-get-stdy-size proc))
100     (if (< result 0)
101         (let ((msg (sj3rpc-get-error-message (- result))))
102           (delete-process proc)
103           (kill-buffer buf)
104           (egg-error "Can't get SJ3 STDYSIZE: %s"msg)))
105     (setq sj3-stdy-size result)
106     proc))
107
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)
113 ;;     (save-excursion
114 ;;       (set-buffer buf)
115 ;;       (erase-buffer)
116 ;;       (buffer-disable-undo)
117 ;;       (setq enable-multibyte-characters nil))
118 ;;     (cond
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)
127 ;;       (sit-for 0)
128 ;;       (condition-case result
129 ;;        (setq proc (open-network-stream "SJ3" buf hostname sj3-server-port))
130 ;;      (error nil))
131 ;;       (if proc
132 ;;        (progn
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)))
140 ;;          (if (< result 0)
141 ;;              (progn
142 ;;                (delete-process proc)
143 ;;                (setq proc nil
144 ;;                      msg (format "Can't open SJ3 session (%s): %s"
145 ;;                                  hostname msg)))
146 ;;            (setq result (sj3rpc-get-stdy-size proc))
147 ;;            (if (< result 0)
148 ;;                (progn
149 ;;                  (delete-process proc)
150 ;;                  (setq proc nil
151 ;;                        msg (format "Can't get SJ3 STDYSIZE: %s"
152 ;;                                    (sj3rpc-get-error-message (- result)))))
153 ;;              (setq sj3-stdy-size result))))))
154 ;;     (if proc
155 ;;      (progn
156 ;;        (setq sj3-open-message (format (concat msg-form "done") hostname))
157 ;;        proc)
158 ;;       (kill-buffer buf)
159 ;;       (error "%s" (or msg "no sj3serv available")))))
160
161 ;; <env> ::= [ <proc> <dictionary-list> ]
162 (defvar sj3-environment nil
163   "Environment for SJ3 kana-kanji conversion")
164
165 (defsubst sj3env-get-proc (env)
166   (aref env 0))
167 (defsubst sj3env-get-dictionary-list (env)
168   (aref env 1))
169
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)
175   (egg-bunsetsu-create
176    'sj3-conversion-backend
177    (vector env source converted rest stdy nil nil nil nil nil)))
178
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))
189
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))
194
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))
199
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))
204
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))
209
210 (defun sj3-get-bunsetsu-source (b)
211   (sj3bunsetsu-get-source b))
212
213 (defun sj3-get-bunsetsu-converted (b)
214   (concat (sj3bunsetsu-get-converted b) (sj3bunsetsu-get-rest b)))
215
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))
219
220 (defvar sj3-dictionary-specification
221   '(("study.dat")
222     ["sj3main.dic" ""]
223     [("private.dic") ""])
224   "Dictionary specification of SJ3.")
225
226 (defvar sj3-usr-dic-dir (concat "user/" (user-login-name))
227   "*Directory of user dictionary for SJ3.")
228
229 (defun sj3-filename (p)
230   ""
231   (cond ((consp p) (concat sj3-usr-dic-dir "/" (car p)))
232         (t p)))
233
234 (defun sj3-get-environment ()
235   "Return the backend of SJ3 environment."
236   (if sj3-environment
237       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))
241            dict-list)
242       (sj3-open-freq-info proc freq-info-name)
243       (while l
244         (let ((dic (car l))
245               dic-id)
246           (setq dic-id
247                 (sj3-open-dictionary proc (sj3-filename (aref dic 0))
248                                      (aref dic 1)))
249           (if (< dic-id 0)
250               (egg-error "Dame2")       ; XXX
251             (setq dict-list (cons dic-id dict-list)
252                   l (cdr l)))))
253       (setq sj3-environment (vector proc dict-list)))))
254
255 (defun sj3-open-freq-info (proc name)
256   (let ((trying t)
257         ret)
258     (while trying
259       (setq ret (sj3rpc-open-stdy proc name))
260       (if (= ret 0)
261           (setq trying nil)
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
265           (if (and (y-or-n-p
266                     (format "\e$B3X=,%U%!%$%k\e(B(%s)\e$B$,$"$j$^$;$s!#:n$j$^$9$+\e(B? "
267                             name))
268                    (sj3rpc-make-directory proc
269                                           (file-name-directory name))
270                    ;; ignore error
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
274
275 (defun sj3-open-dictionary (proc name passwd)
276   (let ((trying t)
277         ret)
278     (while trying
279       (setq ret (sj3rpc-open-dictionary proc name passwd))
280       (if (>= ret 0)
281           (setq trying nil)
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
286           (if (and (y-or-n-p
287                     (format "\e$B<-=q%U%!%$%k\e(B(%s)\e$B$,$"$j$^$;$s!#:n$j$^$9$+\e(B? "
288                             name))
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
292     ret))
293
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)))
299
300 (defun sj3-end-conversion (bunsetsu-list abort)
301   (if abort
302       ()
303     (let ((env (sj3bunsetsu-get-env (car bunsetsu-list)))
304           (l bunsetsu-list)
305           bunsetsu stdy kugiri-changed)
306       (while l
307         (setq bunsetsu (car l))
308         (setq l (cdr l))
309         (setq stdy (sj3bunsetsu-get-stdy bunsetsu))
310         (if stdy
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))))))))))
318
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
330              bunsetsu
331              (mapcar 'sj3bunsetsu-get-converted z))))))
332
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))))
342
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)))
346         (old (car bunsetsu))
347         new yomi1 yomi2)
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)))
357       (list (list new)))))
358
359 (defun sj3-finalize-backend ()
360   (if sj3-environment
361       (let ((proc (sj3env-get-proc sj3-environment))
362             (dict-list (sj3env-get-dictionary-list sj3-environment))
363             dict)
364         (while dict-list
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)
369         (sj3rpc-close proc)
370         (setq sj3-environment nil))))
371
372 ;;; setup
373
374 (load "egg/sj3rpc")
375 (run-hooks 'sj3-load-hook)
376
377 ;;;###autoload
378 (defun egg-activate-sj3 (&rest arg)
379   "Activate SJ3 backend of Tamagotchy."
380   (apply 'egg-mode (append arg sj3-backend-alist)))
381
382 ;;; egg/sj3.el ends here.