Modified Files:
[elisp/tamago.git] / egg / wnn.el
1 ;;; egg/wnn.el --- WNN Support (high level interface) in Egg
2 ;;;                Input Method Architecture
3
4 ;; Copyright (C) 1999,2000 PFU LIMITED
5
6 ;; Author: NIIBE Yutaka <gniibe@chroot.org>
7 ;;         KATAYAMA Yoshio <kate@pfu.co.jp>
8
9 ;; Maintainer: TOMURA Satoru <tomura@etl.go.jp>
10
11 ;; Keywords: mule, multilingual, input method
12
13 ;; This file is part of EGG.
14
15 ;; EGG is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; EGG is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31
32
33 ;;; Code:
34
35 (require 'egg)
36 (require 'egg-edep)
37
38 (defgroup wnn nil
39   "Wnn interface for Tamago 4."
40   :group 'egg)
41
42 (defcustom wnn-auto-save-dictionaries 0
43   "*Save dictionaries automatically after N-th end conversion, if positive"
44   :group 'wnn :type 'integer)
45
46 (defcustom wnn-use-v3-eggrc nil
47   "*Enable old style eggrc, if non-NIL"
48   :group 'wnn :type 'boolean)
49
50 (defcustom wnn-use-bixing (not wnn-use-v3-eggrc)
51   "*Enable bixing (stroke) input-method, if non-NIL"
52   :group 'wnn :type 'boolean)
53
54 (defcustom wnn-force-set-environment nil
55   "*Regardless the existence of the Wnn environment in the server,
56 reset the environment, if non-NIL.  Otherwise, reset the environment
57 only when it is created."
58   :group 'wnn :type 'boolean)
59
60 (defcustom wnn-one-level-conversion nil
61   "*Don't use major clause (dai bunsetu/da wenjie/dae munjeol), if non-NIL."
62   :group 'wnn :type 'boolean)
63
64 (defcustom wnn-usr-dic-dir (concat "usr/" (user-login-name))
65   "*Directory of user dictionary for Wnn."
66   :group 'wnn
67   :type 'string)
68
69 (defcustom wnn-user-name (or (getenv "WNNUSER") (user-login-name))
70   "User name at Wnn server.  Default value is login name or
71 WNNUSER evironment variable, if it is defined."
72   :group 'wnn :type 'string)
73
74 (defcustom wnn-uniq-level 'wnn-uniq
75   "Uniq level for candidate selection.
76 wnn-no-uniq:    Use all candidates.
77 wnn-uniq-entry: Use only one among same dictionary entry candidates.
78 wnn-uniq:       Use only one among same hinshi candidates. (default)
79 wnn-uniq-kanji: Use only one among same kanji candidates."
80   :group 'wnn :type '(choice (const wnn-no-uniq)
81                              (const wnn-uniq-entry)
82                              (const wnn-uniq)
83                              (const wnn-uniq-kanji)))
84
85 (defcustom wnn-jserver nil
86   "jserver hostname list.  Use N-th port, if hostname is followed
87 by ':' and digit N."
88   :group 'wnn :type '(repeat string))
89 (defcustom wnn-cserver nil
90   "cserver hostname list.  Use N-th port, if hostname is followed
91 by ':' and digit N."
92   :group 'wnn :type '(repeat string))
93 (defcustom wnn-tserver nil
94   "tserver hostname list.  Use N-th port, if hostname is followed
95 by ':' and digit N."
96   :group 'wnn :type '(repeat string))
97 (defcustom wnn-kserver nil
98   "kserver hostname list.  Use N-th port, if hostname is followed
99  by ':' and digit N."
100   :group 'wnn :type '(repeat string))
101
102 (defcustom wnn-jport 22273 "jserver port number" :group 'wnn :type 'integer)
103 (defcustom wnn-cport 22289 "cserver port number" :group 'wnn :type 'integer)
104 (defcustom wnn-tport 22321 "tserver port number" :group 'wnn :type 'integer)
105 (defcustom wnn-kport 22305 "kserver port number" :group 'wnn :type 'integer)
106
107 (defmacro wnn-backend-plist ()
108   ''(egg-initialize-backend        wnn-init
109      egg-start-conversion          wnn-start-conversion
110      egg-get-bunsetsu-source       wnn-get-bunsetsu-source
111      egg-get-bunsetsu-converted    wnn-get-bunsetsu-converted
112      egg-get-source-language       wnn-get-source-language
113      egg-get-converted-language    wnn-get-converted-language
114      egg-major-bunsetsu-continue-p wnn-major-bunsetsu-continue-p
115      egg-list-candidates           wnn-list-candidates
116      egg-decide-candidate          wnn-decide-candidate
117      egg-special-candidate         wnn-special-candidate
118      egg-change-bunsetsu-length    wnn-change-bunsetsu-length
119      egg-bunsetsu-combinable-p     wnn-bunsetsu-combinable-p
120      egg-end-conversion            wnn-end-conversion
121      egg-word-inspection           wnn-word-inspection
122      egg-word-registration         wnn-word-registration))
123
124 (defun wnn-backend-func-name (name lang &optional env)
125   (intern (concat name "-" (symbol-name lang)
126                   (and env "-") (and env (symbol-name env)))))
127
128 (defun wnn-make-backend (lang env &optional source-lang converted-lang)
129   (let ((finalize (wnn-backend-func-name "wnn-finalize-backend" lang))
130         (backend (wnn-backend-func-name "wnn-backend" lang env)))
131     (if (null (fboundp finalize))
132         (progn
133           (fset finalize `(lambda () (wnn-finalize-backend ',lang)))
134           (egg-set-finalize-backend (list finalize))))
135     (if (null (get backend 'egg-start-conversion))
136         (setplist backend (apply 'list
137                                  'language lang
138                                  'source-language (or source-lang lang)
139                                  'converted-language (or converted-lang lang)
140                                  (wnn-backend-plist))))
141     backend))
142
143 (defun wnn-define-backend (lang env-name-list)
144   (mapcar (lambda (env)
145             (if (consp env)
146                 (wnn-define-backend lang env)
147               (wnn-make-backend lang env)))
148           env-name-list))
149
150 (wnn-make-backend 'Chinese-GB 'Q  'QianMa 'Chinese-GB)
151 (wnn-make-backend 'Chinese-GB 'QR 'Chinese-GB 'QianMa)
152 (wnn-make-backend 'Chinese-GB 'W  'WuBi 'Chinese-GB)
153 (wnn-make-backend 'Chinese-GB 'WR 'Chinese-GB 'WuBi)
154
155 (defconst wnn-backend-language-alist '((QianMa . Chinese-GB)
156                                        (WuBi . Chinese-GB)))
157
158 (defvar wnn-backend-alist nil)
159
160 (defun wnn-define-backend-alist (deflist)
161   (setq wnn-backend-alist
162         (mapcar (lambda (slot)
163                   (let* ((lang (car slot))
164                          (alt (cdr (assq lang wnn-backend-language-alist))))
165                     (cons lang (wnn-define-backend (or alt lang) (cdr slot)))))
166                 deflist)))
167
168 (defcustom wnn-backend-define-list
169   (if wnn-use-bixing
170       '((Japanese    ((nil nil R))   ((R   nil R)))
171         (Chinese-GB  ((PZ  PZ  PZR)) ((PZR PZ  PZR))
172                      ((QR  Q   QR))  ((WR  W   WR)))
173         (Chinese-CNS ((PZ  PZ  PZR)) ((PZR PZ  PZR)))
174         (Korean      ((nil nil R))   ((R   nil R)))
175         (QianMa      ((Q Q QR)))
176         (WuBi        ((W W WR))))
177     '((Japanese    ((nil nil R))   ((R   nil R)))
178       (Chinese-GB  ((PZ  PZ  PZR)) ((PZR PZ  PZR)))
179       (Chinese-CNS ((PZ  PZ  PZR)) ((PZR PZ  PZR)))
180       (Korean      ((nil nil R))   ((R   nil R)))))
181   "Alist of language and lists of the Wnn backend suffixes."
182   :group 'wnn
183   :set (lambda (sym value)
184          (set-default sym value)
185          (wnn-define-backend-alist value))
186   :type '(repeat
187           (cons
188            :tag "Language - Backend"
189            (choice :tag "Language"
190                    (const Japanese)
191                    (const Chinese-GB)
192                    (const Chinese-CNS)
193                    (const Korean)
194                    (const QianMa)
195                    (const WuBi)
196                    (symbol :tag "Other"))
197            (repeat
198             (cons
199              :tag "Backend Sequece"
200              (cons :tag "First Conversion Stage"
201                    (symbol :tag "Backend for Start Conversion")
202                    (repeat :tag "Backends for Reconvert"
203                            (symbol :tag "Backend")))
204              (repeat
205               :tag "Following Conversion Stages"
206               (cons
207                :tag "N-th Stage"
208                (symbol :tag "Backend for This Stage")
209                (repeat :tag "Backends for Reconvert"
210                        (symbol :tag "Backend")))))))))
211
212 (eval-when-compile
213   (defmacro WNN-const (c)
214     (cond ((eq c 'BUN_SENTOU)    -1)
215           ((eq c 'NO_EXIST)       1)
216           ((eq c 'NO_MATCH)      10)
217           ((eq c 'IMA_OFF)       -4)
218           ((eq c 'IMA_ON)        -3)
219           ((eq c 'CONNECT)        1)
220           ((eq c 'CONNECT_BK)     1)
221           ((eq c 'HIRAGANA)      -1)
222           ((eq c 'KATAKANA)     -11)
223           ((eq c 'IKEIJI_ENTRY) -50)
224           ((eq c 'LEARNING_LEN)   3)
225           ((eq c 'MUHENKAN_DIC)  -3)
226           ((eq c 'HINDO_NOP)     -2)
227           ((eq c 'HINDO_INC)     -3)
228           ((eq c 'DIC_RW)         0)
229           ((eq c 'DIC_RDONLY)     1)
230           ((eq c 'DIC_GROUP)      3)
231           ((eq c 'DIC_MERGE)      4)
232           ((eq c 'NOTRANS_LEARN)  1)
233           ((eq c 'BMODIFY_LEARN)  2)
234           ((eq c 'DIC_NO_TEMPS)   ?\x3f))))
235 \f
236 ;; Retern value of system-name may differ from hostname.
237 (defconst wnn-system-name
238   (or (with-temp-buffer
239         (condition-case nil
240             (call-process "hostname"
241                           nil `(,(current-buffer) nil) "hostname")
242           (error))
243         (goto-char (point-min))
244         (if (re-search-forward "[\0- ]" nil 0)
245             (goto-char (1- (point))))
246         (if (> (point) 1)
247             (buffer-substring 1 (point))))
248       (system-name)))
249
250 (egg-add-message
251  '((nil
252     (wnn-connect-error  "cannot connect to the server")
253     (wnn-fail-make-env  "cannot make the Wnn environment")
254     (wnn-dict-saving    "saving %s's frequency/dictionary information")
255     (wnn-dict-saved     "finish to save %s's frequency/dictionary information")
256     (wnn-dir-missing    "directory %s missing. Create it? ")
257     (wnn-dir-failed     "failed to create directory %s")
258     (wnn-dir-created    "directory %s created")
259     (wnn-dict-missing-1 "dictionary file %s is missing: %s")
260     (wnn-dict-missing-2 "dictionary file %s is missing. Create it? ")
261     (wnn-dict-created   "dictionary file %s is created")
262     (wnn-freq-missing-1 "frequency file %s is missing: %s")
263     (wnn-freq-missing-2 "frequency file %s is missing. Create it? ")
264     (wnn-freq-created   "frequency file %s is created")
265     (wnn-no-match       "unmatch dictionary and freq. file %s. Re-create it? ")
266     (wnn-re-create-freq "frequency file %s is re-created")
267     (wnn-pseud-bunsetsu "pseud clause")
268     (wnn-register-1     "dictionary name:")
269     (wnn-register-2     "clause class name")
270     (wnn-no-writable-d  "no writable dictionary"))
271    (Japanese
272     (wnn-connect-error  "\e$B%5!<%P$H@\B3$G$-$^$;$s$G$7$?\e(B")
273     (wnn-fail-make-env  "\e$B4D6-$r:n$k$3$H$O$G$-$^$;$s$G$7$?\e(B")
274     (wnn-dict-saving    "%s \e$B$NIQEY>pJs!&<-=q>pJs$rB`Hr$7$F$$$^$9\e(B")
275     (wnn-dict-saved     "%s \e$B$NIQEY>pJs!&<-=q>pJs$rB`Hr$7$^$7$?\e(B")
276     (wnn-dir-missing    "\e$B%G%#%l%/%H%j\e(B %s \e$B$,$"$j$^$;$s!#:n$j$^$9$+\e(B? ")
277     (wnn-dir-failed     "\e$B%G%#%l%/%H%j\e(B %s \e$B$N:n@.$K<:GT$7$^$7$?\e(B")
278     (wnn-dir-created    "\e$B%G%#%l%/%H%j\e(B %s \e$B$r:n$j$^$7$?\e(B")
279     (wnn-dict-missing-1 "\e$B<-=q%U%!%$%k\e(B %s \e$B$,$"$j$^$;$s\e(B: %s")
280     (wnn-dict-missing-2 "\e$B<-=q%U%!%$%k\e(B %s \e$B$,$"$j$^$;$s!#:n$j$^$9$+\e(B? ")
281     (wnn-dict-created   "\e$B<-=q%U%!%$%k\e(B %s \e$B$r:n$j$^$7$?\e(B")
282     (wnn-freq-missing-1 "\e$BIQEY%U%!%$%k\e(B %s \e$B$,$"$j$^$;$s\e(B: %s")
283     (wnn-freq-missing-2 "\e$BIQEY%U%!%$%k\e(B %s \e$B$,$"$j$^$;$s!#:n$j$^$9$+\e(B? ")
284     (wnn-freq-created   "\e$BIQEY%U%!%$%k\e(B %s \e$B$r:n$j$^$7$?\e(B")
285     (wnn-no-match       "\e$B<-=q$HIQEY\e(B %s \e$B$N@09g@-$,$"$j$^$;$s!#:n$jD>$7$^$9$+\e(B? ")
286     (wnn-re-create-freq "\e$BIQEY%U%!%$%k\e(B %s \e$B$r:n$jD>$7$^$7$?\e(B")
287     (wnn-pseud-bunsetsu "\e$B5?;wJ8@a\e(B")
288     (wnn-register-1     "\e$BEPO?<-=qL>\e(B:")
289     (wnn-register-2     "\e$BIJ;lL>\e(B")
290     (wnn-no-writable-d  "\e$BEPO?2DG=$J<-=q$,$"$j$^$;$s\e(B"))
291    (Chinese-GB
292     (wnn-connect-error  "\e$A2;D\:M\e(Bserver\e$AA,=S\e(B")
293     (wnn-fail-make-env  "\e$A;7>32;D\44=(\e(B")
294     (wnn-dict-saving    "%s \e$A5DF56HND<~:M4G5dPEO"U}TZMK3v\e(B")
295     (wnn-dict-saved     "%s \e$A5DF56HND<~:M4G5dPEO"RQ>-MK3vAK\e(B")
296     (wnn-dir-missing    "\e$AD?B<\e(B %s \e$AC;SP!#R*=(A"Bp\e(B? ")
297     (wnn-dir-failed     "\e$AD?B<\e(B %s \e$A=(A"J'0\AK\e(B")
298     (wnn-dir-created    "\e$AD?B<\e(B %s \e$A=(A"AK\e(B")
299     (wnn-dict-missing-1 "\e$AWV5dND<~\e(B %s \e$AC;SP\e(B: %s")
300     (wnn-dict-missing-2 "\e$AWV5dND<~\e(B %s \e$AC;SP!#R*=(A"Bp\e(B? ")
301     (wnn-dict-created   "\e$AWV5dND<~\e(B %s \e$A=(A"AK\e(B")
302     (wnn-freq-missing-1 "\e$AF56HND<~\e(B %s \e$AC;SP\e(B: %s")
303     (wnn-freq-missing-2 "\e$AF56HND<~\e(B %s \e$AC;SP!#R*=(A"Bp\e(B? ")
304     (wnn-freq-created   "\e$AF56HND<~\e(B %s \e$A=(A"AK\e(B")
305     (wnn-no-match       "\e$AWV5d:MF56H\e(B %s \e$A5DU{:OPTC;SP!#R*TY=(A"Bp\e(B? ")
306     (wnn-re-create-freq "\e$AF56HND<~\e(B %s \e$ATY=(A"AK\e(B")
307     (wnn-pseud-bunsetsu "\e$ARIKFND=Z\e(B")
308     (wnn-register-1     "\e$A5GB<4G5dC{\e(B:")
309     (wnn-register-2     "\e$A4JPTC{\e(B")
310     (wnn-no-writable-d  "\e$AC;SP?ID\5GB<5D4G5d\e(B"))
311    (Chinese-CNS
312     (wnn-connect-error  "\e$(GDbWdLO\e(Bserver\e$(G]YZY\e(B")
313     (wnn-fail-make-env  "\e$(Gt?h:DbWd^6Pz\e(B")
314     (wnn-dict-saving    "%s \e$(GN{s"PyEFG5LOy0L(OjUIF_GcXMEx\e(B")
315     (wnn-dict-saved     "%s \e$(GN{s"PyEFG5LOy0L(OjUIDXenXMExD'\e(B")
316     (wnn-dir-missing    "\e$(GFxrg\e(B %s \e$(GJtH4!$SQPzG!cC\e(B? ")
317     (wnn-dir-failed     "\e$(GFxrg\e(B %s \e$(GPzG!FBZuD'\e(B")
318     (wnn-dir-created    "\e$(GFxrg\e(B %s \e$(GPzG!D'\e(B")
319     (wnn-dict-missing-1 "\e$(GGsL(EFG5\e(B %s \e$(GJtH4\e(B: %s")
320     (wnn-dict-missing-2 "\e$(GGsL(EFG5\e(B %s \e$(GJtH4!$SQPzG!cC\e(B? ")
321     (wnn-dict-created   "\e$(GGsL(EFG5\e(B %s \e$(GPzG!D'\e(B")
322     (wnn-freq-missing-1 "\e$(Gs"PyEFG5\e(B %s \e$(GJtH4\e(B: %s")
323     (wnn-freq-missing-2 "\e$(Gs"PyEFG5\e(B %s \e$(GJtH4!$SQPzG!cC\e(B? ")
324     (wnn-freq-created   "\e$(Gs"PyEFG5\e(B %s \e$(GPzG!D'\e(B")
325     (wnn-no-match       "\e$(GGsL(LOs"Py\e(B %s \e$(GN{plLOMLJtH4!$SQGBPzG!cC\e(B? ")
326     (wnn-re-create-freq "\e$(Gs"PyEFG5\e(B %s \e$(GGBPzG!D'\e(B")
327     (wnn-pseud-bunsetsu "\e$(GijH}EFeg\e(B")
328     (wnn-register-1     "\e$(G`trgy0L(GX\e(B:")
329     (wnn-register-2     "\e$(Gb$MLGX\e(B")
330     (wnn-no-writable-d  "\e$(GJtH4F+Wd`trgN{GsL(\e(B"))
331    (Korean
332     (wnn-connect-error  "\e$(C<-9v\e(B(Server) \e$(C?M\e(B \e$(CA"CKGR\e(B \e$(C<v\e(B \e$(C>x@>4O4Y\e(B")
333     (wnn-fail-make-env  "\e$(CH/0f@;\e(B \e$(C@[<:GR\e(B \e$(C<v\e(B \e$(C>x@>4O4Y\e(B")
334     (wnn-dict-saving    "%s \e$(C@G\e(B \e$(C:s55A$:8?M\e(B \e$(C;g@|A$:88&\e(B \e$(C<<@L:jGO0m\e(B \e$(C@V=@4O4Y\e(B")
335     (wnn-dict-saved     "%s \e$(C@G\e(B \e$(C:s55A$:8?M\e(B \e$(C;g@|A$:88&\e(B \e$(C<<@L:jG_=@4O4Y\e(B")
336     (wnn-dir-missing    "\e$(C5p7:Ed8.\e(B %s \e$(C@L\e(B \e$(C>x@>4O4Y#.@[<:GO0Z=@4O1n\e(B? ")
337     (wnn-dir-failed     "\e$(C5p7:Ed8.\e(B %s \e$(C@;\e(B \e$(C@[<:GR\e(B \e$(C<v\e(B \e$(C>x@>4O4Y\e(B")
338     (wnn-dir-created    "\e$(C5p7:Ed8.\e(B %s \e$(C@;\e(B \e$(C@[<:G_=@4O4Y\e(B")
339     (wnn-dict-missing-1 "\e$(C;g@|H-@O\e(B %s \e$(C@L\e(B \e$(C>x@>4O4Y\e(B: %s")
340     (wnn-dict-missing-2 "\e$(C;g@|H-@O\e(B %s \e$(C@L\e(B \e$(C>x@>4O4Y#.4Y=C\e(B \e$(C@[<:GO0Z=@4O1n\e(B? ")
341     (wnn-dict-created   "\e$(C;g@|H-@O\e(B %s \e$(C@;\e(B \e$(C@[<:G_=@4O4Y\e(B")
342     (wnn-freq-missing-1 "\e$(C:s55H-@O\e(B %s \e$(C@L\e(B \e$(C>x@>4O4Y\e(B: %s")
343     (wnn-freq-missing-2 "\e$(C:s55H-@O\e(B %s \e$(C@L\e(B \e$(C>x@>4O4Y#.4Y=C\e(B \e$(C@[<:GO0Z=@4O1n\e(B? ")
344     (wnn-freq-created   "\e$(C:s55H-@O\e(B %s \e$(C@;\e(B \e$(C@[<:G_=@4O4Y\e(B")
345     (wnn-no-match       "\e$(C;g@|0z\e(B \e$(C:s55\e(B %s \e$(C0!\e(B \e$(C8BAv\e(B \e$(C>J=@4O4Y#.4Y=C\e(B \e$(C@[<:GO0Z=@4O1n\e(B? ")
346     (wnn-re-create-freq "\e$(C:s55H-@O\e(B %s \e$(C@;\e(B \e$(C4Y=C\e(B \e$(C@[<:G_=@4O4Y\e(B")
347     (wnn-pseud-bunsetsu "\e$(C@G;g9.@}\e(B")
348     (wnn-register-1     "\e$(C5n7O;g@|8m\e(B:")
349     (wnn-register-2     "\e$(CG0;g8m\e(B")
350     (wnn-no-writable-d  "\e$(C5n7O\e(B \e$(C0!4IGQ\e(B \e$(CAvA$@L\e(B \e$(C>F4U4O4Y\e(B"))))
351 \f
352 ;; <env> ::= [ <proc> <env-id> <lang> <server-type> <wnn-version>
353 ;;             <backend> <tankan> <env-name> <auto-learn> <hinshi> ]
354
355 (defun wnnenv-create (proc env-id &optional server-type wnn-version
356                       backend tankan name)
357   (if name
358       (set (setq name (make-symbol name)) (make-vector 5 nil)))
359   (vector proc env-id server-type wnn-version backend tankan name
360           (make-vector 2 (WNN-const DIC_RDONLY))))
361
362 (defsubst wnnenv-get-proc (env)        (aref env 0))
363 (defsubst wnnenv-get-env-id (env)      (aref env 1))
364 (defsubst wnnenv-get-server-type (env) (aref env 2))
365 (defsubst wnnenv-get-wnn-version (env) (aref env 3))
366 (defsubst wnnenv-get-backend (env)     (aref env 4))
367 (defsubst wnnenv-get-tankan (env)      (aref env 5))
368
369 (defun wnnenv-get-client-file (env name)
370   (let ((hash (intern-soft name (symbol-value (aref env 6)))))
371     (and hash (symbol-value hash))))
372
373 (defun wnnenv-set-client-file (env name)
374   (set (intern (concat wnn-system-name "!" name) (symbol-value (aref env 6)))
375        name))
376
377 (defsubst wnnenv-get-hinshi (env h)    (or (get (aref env 6) h) -1))
378 (defsubst wnnenv-set-hinshi (env h v)  (put (aref env 6) h v))
379
380 (defsubst wnnenv-get-auto-learn (env)  (aref env 7))
381 (defsubst wnnenv-get-notrans (env)     (aref (wnnenv-get-auto-learn env) 0))
382 (defsubst wnnenv-get-bmodify (env)     (aref (wnnenv-get-auto-learn env) 1))
383 (defsubst wnnenv-set-notrans (env v)   (aset (wnnenv-get-auto-learn env) 0 v))
384 (defsubst wnnenv-set-bmodify (env v)   (aset (wnnenv-get-auto-learn env) 1 v))
385
386 (defsubst wnnenv-is-wnn6 (env)         (eq (wnnenv-get-wnn-version env) 'wnn6))
387
388 (defvar wnn-environments nil
389   "Environment for Wnn conversion server")
390
391 ;; <wnn-bunsetsu> ::= [ <env>
392 ;;                      <jirilen> <dic-no> <entry> <freq> <right-now> <hinshi>
393 ;;                      <status> <status-backward> <kangovect> <evaluation>
394 ;;                      <converted> <yomi> <fuzokugo>
395 ;;                      <dai-evaluation> <dai-continue> <change-top>
396 ;;                      <zenkouho-info> <freq-down> <fi-rel> <context> ]
397 ;;
398 ;; <zenkouho-info> ::= [ <pos> <list> <converted> <dai> <prev-b> <nxet-b> ]
399
400 (defsubst wnn-bunsetsu-create (env jirilen dic-no entry freq right-now hinshi
401                                status status-backward kangovect evaluation)
402   (egg-bunsetsu-create (wnnenv-get-backend env)
403                        (vector env jirilen dic-no entry freq right-now hinshi
404                                status status-backward kangovect evaluation
405                                nil nil nil nil nil nil nil nil nil nil)))
406
407 (defsubst wnn-bunsetsu-get-env (b)
408   (aref (egg-bunsetsu-get-info b) 0))
409 (defsubst wnn-bunsetsu-get-jirilen (b)
410   (aref (egg-bunsetsu-get-info b) 1))
411 (defsubst wnn-bunsetsu-get-dic-no (b)
412   (aref (egg-bunsetsu-get-info b) 2))
413 (defsubst wnn-bunsetsu-set-dic-no (b dic)
414   (aset (egg-bunsetsu-get-info b) 2 dic))
415 (defsubst wnn-bunsetsu-get-entry (b)
416   (aref (egg-bunsetsu-get-info b) 3))
417 (defsubst wnn-bunsetsu-set-entry (b ent)
418   (aset (egg-bunsetsu-get-info b) 3 ent))
419 (defsubst wnn-bunsetsu-get-freq (b)
420   (aref (egg-bunsetsu-get-info b) 4))
421 (defsubst wnn-bunsetsu-get-right-now (b)
422   (aref (egg-bunsetsu-get-info b) 5))
423 (defsubst wnn-bunsetsu-get-hinshi (b)
424   (aref (egg-bunsetsu-get-info b) 6))
425 (defsubst wnn-bunsetsu-get-status (b)
426   (aref (egg-bunsetsu-get-info b) 7))
427 (defsubst wnn-bunsetsu-get-status-backward (b)
428   (aref (egg-bunsetsu-get-info b) 8))
429 (defsubst wnn-bunsetsu-get-kangovect (b)
430   (aref (egg-bunsetsu-get-info b) 9))
431 (defsubst wnn-bunsetsu-get-evaluation (b)
432   (aref (egg-bunsetsu-get-info b) 10))
433
434 (defsubst wnn-bunsetsu-get-converted (b)
435   (aref (egg-bunsetsu-get-info b) 11))
436 (defsubst wnn-bunsetsu-set-converted (b cvt)
437   (aset (egg-bunsetsu-get-info b) 11 cvt))
438
439 (defsubst wnn-bunsetsu-get-yomi (b)
440   (aref (egg-bunsetsu-get-info b) 12))
441 (defsubst wnn-bunsetsu-set-yomi (b yomi)
442   (aset (egg-bunsetsu-get-info b) 12 yomi))
443
444 (defsubst wnn-bunsetsu-get-fuzokugo (b)
445   (aref (egg-bunsetsu-get-info b) 13))
446 (defsubst wnn-bunsetsu-set-fuzokugo (b fuzokugo)
447   (aset (egg-bunsetsu-get-info b) 13 fuzokugo))
448
449 (defsubst wnn-bunsetsu-get-dai-evaluation (b)
450   (aref (egg-bunsetsu-get-info b) 14))
451 (defsubst wnn-bunsetsu-set-dai-evaluation (b de)
452   (aset (egg-bunsetsu-get-info b) 14 de))
453
454 (defsubst wnn-bunsetsu-get-dai-continue (b)
455   (aref (egg-bunsetsu-get-info b) 15))
456 (defsubst wnn-bunsetsu-set-dai-continue (b dc)
457   (aset (egg-bunsetsu-get-info b) 15 dc))
458
459 (defsubst wnn-bunsetsu-get-change-top (b)
460   (aref (egg-bunsetsu-get-info b) 16))
461 (defsubst wnn-bunsetsu-set-change-top (b top)
462   (aset (egg-bunsetsu-get-info b) 16 top))
463
464 (defsubst wnn-bunsetsu-get-zenkouho (b)
465   (aref (egg-bunsetsu-get-info b) 17))
466 (defsubst wnn-bunsetsu-set-zenkouho (b z)
467   (aset (egg-bunsetsu-get-info b) 17 z))
468
469 (defsubst wnn-bunsetsu-get-freq-down (b)
470   (aref (egg-bunsetsu-get-info b) 18))
471 (defsubst wnn-bunsetsu-set-freq-down (b fd)
472   (aset (egg-bunsetsu-get-info b) 18 fd))
473
474 (defsubst wnn-bunsetsu-get-fi-rel (b)
475   (aref (egg-bunsetsu-get-info b) 19))
476 (defsubst wnn-bunsetsu-set-fi-rel (b fr)
477   (aset (egg-bunsetsu-get-info b) 19 fr))
478
479 (defsubst wnn-bunsetsu-get-context (b)
480   (aref (egg-bunsetsu-get-info b) 20))
481 (defsubst wnn-bunsetsu-set-context (b c)
482   (aset (egg-bunsetsu-get-info b) 20 c))
483
484 (defsubst wnn-zenkouho-create (pos list converted dai prev-b nxet-b)
485   (vector pos list converted dai prev-b nxet-b))
486
487 (defsubst wnn-bunsetsu-get-zenkouho-pos (b)
488   (aref (wnn-bunsetsu-get-zenkouho b) 0))
489 (defsubst wnn-bunsetsu-set-zenkouho-pos (b p)
490   (aset (wnn-bunsetsu-get-zenkouho b) 0 p))
491
492 (defsubst wnn-bunsetsu-get-zenkouho-list (b)
493   (aref (wnn-bunsetsu-get-zenkouho b) 1))
494 (defsubst wnn-bunsetsu-get-zenkouho-converted (b)
495   (aref (wnn-bunsetsu-get-zenkouho b) 2))
496 (defsubst wnn-bunsetsu-get-zenkouho-dai (b)
497   (aref (wnn-bunsetsu-get-zenkouho b) 3))
498 (defsubst wnn-bunsetsu-get-zenkouho-prev-b (b)
499   (aref (wnn-bunsetsu-get-zenkouho b) 4))
500 (defsubst wnn-bunsetsu-get-zenkouho-next-b (b)
501   (aref (wnn-bunsetsu-get-zenkouho b) 5))
502
503 (defsubst wnn-bunsetsu-connect-prev (bunsetsu)
504   (= (wnn-bunsetsu-get-status bunsetsu) (WNN-const CONNECT)))
505 (defsubst wnn-bunsetsu-connect-next (bunsetsu)
506   (= (wnn-bunsetsu-get-status-backward bunsetsu) (WNN-const CONNECT_BK)))
507
508 (defsubst wnn-context-create (dic-no entry jirilen hinshi fuzokugo
509                               converted freq right-now)
510   (vector dic-no entry jirilen hinshi fuzokugo
511           converted freq right-now
512           (egg-chars-in-period converted 0 (length converted))))
513
514 (defsubst wnn-context-dic-no (context)          (aref context 0))
515 (defsubst wnn-context-entry (context)           (aref context 1))
516 (defsubst wnn-context-jirilen (context)         (aref context 2))
517 (defsubst wnn-context-hinshi (context)          (aref context 3))
518 (defsubst wnn-context-fuzokugo (context)        (aref context 4))
519 (defsubst wnn-context-converted (context)       (aref context 5))
520 (defsubst wnn-context-right-now (context)       (aref context 6))
521 (defsubst wnn-context-set-right-now (context r) (aset context 6 r))
522 (defsubst wnn-context-freq (context)            (aref context 7))
523 (defsubst wnn-context-set-freq (context f)      (aset context 7 f))
524 (defsubst wnn-context-length (context)          (aref context 8))
525
526 (defun wnn-null-context ()
527   (list (wnn-context-create -2 0 0 0 "" "" 0 0)
528         (wnn-context-create -2 0 0 0 "" "" 0 0)))
529
530 (defun wnn-major-bunsetsu-set-context (bunsetsu-list context)
531   (while bunsetsu-list
532     (wnn-bunsetsu-set-context (car bunsetsu-list) context)
533     (setq bunsetsu-list (cdr bunsetsu-list))))
534
535 (defsubst wnn-bunsetsu-equal (bunsetsu-1 bunsetsu-2)
536   (and (= (wnn-bunsetsu-get-dic-no bunsetsu-1)
537           (wnn-bunsetsu-get-dic-no bunsetsu-2))
538        (= (wnn-bunsetsu-get-entry bunsetsu-1)
539           (wnn-bunsetsu-get-entry bunsetsu-2))
540        (= (wnn-bunsetsu-get-kangovect bunsetsu-1)
541           (wnn-bunsetsu-get-kangovect bunsetsu-2))
542        (equal (wnn-bunsetsu-get-converted bunsetsu-1)
543               (wnn-bunsetsu-get-converted bunsetsu-2))
544        (equal (wnn-bunsetsu-get-fuzokugo bunsetsu-1)
545               (wnn-bunsetsu-get-fuzokugo bunsetsu-2))))
546
547 (defun wnn-bunsetsu-list-equal (b1 b2)
548   (while (and b1 b2 (wnn-bunsetsu-equal (car b1) (car b2)))
549     (setq b1 (cdr b1)
550           b2 (cdr b2)))
551   (and (null b1) (null b2)))
552
553 (defun wnn-bunsetsu-list-copy (bunsetsu)
554   (mapcar (lambda (b)
555             (egg-bunsetsu-create (egg-bunsetsu-get-backend b)
556                                  (copy-sequence (egg-bunsetsu-get-info b))))
557           bunsetsu))
558 \f
559 (defvar wnn-server-info-list
560   ;; language    server  port      hostname    proc   coding-system
561   '((Japanese    jserver wnn-jport wnn-jserver "Wnn"  (fixed-euc-jp    fixed-euc-jp))
562     (Chinese-GB  cserver wnn-cport wnn-cserver "cWnn" (fixed-euc-py-cn fixed-euc-zy-cn))
563     (Chinese-CNS tserver wnn-tport wnn-tserver "tWnn" (fixed-euc-py-tw fixed-euc-zy-tw))
564     (Korean      kserver wnn-kport wnn-kserver "kWnn" (fixed-euc-kr    fixed-euc-kr))))
565
566 (defsubst wnn-server-get-info (lang)
567   (or (assq lang wnn-server-info-list)
568       (egg-error "unsupported language: %s" lang)))
569
570 (defsubst wnn-server-language (info)
571   (car info))
572 (defsubst wnn-server-type (info)
573   (nth 1 info))
574 (defsubst wnn-server-port (info)
575   (symbol-value (nth 2 info)))
576 (defsubst wnn-server-hostname (info)
577   (symbol-value (nth 3 info)))
578 (defsubst wnn-server-proc-name (info)
579   (nth 4 info))
580 (defsubst wnn-server-buffer-name (info)
581   (concat " *" (wnn-server-proc-name info) "*"))
582 (defsubst wnn-server-coding-system (info)
583   (nth 5 info))
584
585 (defconst wnn-accept-charset-alist
586   '((Chinese-CNS ascii chinese-sisheng chinese-cns11643-1 chinese-cns11643-2)))
587
588 (defsubst wnn-backend-get-language (backend)
589   (get backend 'language))
590
591 (defsubst wnn-backend-get-source-language (backend)
592   (get backend 'source-language))
593
594 (defsubst wnn-backend-get-converted-language (backend)
595   (get backend 'converted-language))
596
597 (defvar wnn-prev-context nil)
598
599 (defun wnn-start-conversion (backend yomi &optional context)
600   "Convert YOMI string to kanji, and enter conversion mode.
601 Return the list of bunsetsu."
602   (let ((accepts (cdr (assq (wnn-backend-get-source-language backend)
603                             wnn-accept-charset-alist)))
604         env hinshi fuzokugo result)
605     (if accepts
606         (let ((charsets (mapcar 'char-charset (string-to-list yomi))))
607           (while accepts
608              (setq charsets (delq (car accepts) charsets)
609                    accepts (cdr accepts)))
610           (if charsets
611               (egg-error "cannot handle %s" (car charsets)))))
612     (setq env (wnn-get-environment backend))
613     (cond ((eq (car (car-safe  context)) backend)
614            (setq wnn-prev-context (car context)
615                  context (cdr wnn-prev-context)
616                  hinshi (wnn-context-hinshi (nth 1 context))
617                  fuzokugo (wnn-context-fuzokugo (nth 1 context))))
618           ((listp context)
619            (setq wnn-prev-context (cons nil (wnn-null-context))
620                  context (cdr wnn-prev-context)
621                  hinshi (WNN-const BUN_SENTOU)
622                  fuzokugo ""))
623           ((eq (car wnn-prev-context) backend)
624            (setq context (cdr wnn-prev-context)
625                  hinshi (wnnenv-get-hinshi env 'noun)
626                  fuzokugo ""))
627           (t
628            (setq context (wnn-null-context)
629                  hinshi (wnnenv-get-hinshi env 'noun)
630                  fuzokugo "")))
631     (setq result (wnn-renbunsetsu-conversion env yomi hinshi fuzokugo nil
632                                              context))
633     (if (numberp result)
634         (egg-error "%s" (wnnrpc-get-error-message (- result))))
635     result))
636
637 (defun wnn-get-source-language (bunsetsu)
638   (wnn-backend-get-source-language (egg-bunsetsu-get-backend bunsetsu)))
639
640 (defun wnn-get-converted-language (bunsetsu)
641   (wnn-backend-get-converted-language (egg-bunsetsu-get-backend bunsetsu)))
642
643 (defun wnn-get-bunsetsu-converted (bunsetsu)
644   (concat (wnn-bunsetsu-get-converted bunsetsu)
645           (wnn-bunsetsu-get-fuzokugo  bunsetsu)))
646
647 (defun wnn-get-bunsetsu-source (bunsetsu)
648   (concat (wnn-bunsetsu-get-yomi bunsetsu)
649           (wnn-bunsetsu-get-fuzokugo bunsetsu)))
650
651 (defun wnn-get-major-bunsetsu-converted (bunsetsu)
652   (mapconcat 'wnn-get-bunsetsu-converted bunsetsu ""))
653
654 (defun wnn-get-major-bunsetsu-source (bunsetsu)
655   (mapconcat 'wnn-get-bunsetsu-source bunsetsu ""))
656
657 (defun wnn-major-bunsetsu-continue-p (bunsetsu)
658   (wnn-bunsetsu-get-dai-continue bunsetsu))
659
660 (defmacro wnn-uniq-hash-string (uniq-level)
661   `(mapconcat
662     (lambda (b)
663       (concat ,@(cond ((eq uniq-level 'wnn-uniq)
664                        '((number-to-string (wnn-bunsetsu-get-hinshi b))))
665                       ((eq uniq-level 'wnn-uniq-entry)
666                        '((number-to-string (wnn-bunsetsu-get-dic-no b))
667                          "+"
668                          (number-to-string (wnn-bunsetsu-get-entry b)))))
669               "\0"
670               (wnn-bunsetsu-get-converted b)
671               "\0"
672               (wnn-bunsetsu-get-fuzokugo b)))
673     bunsetsu "\0"))
674
675 (defun wnn-uniq-hash (bunsetsu hash-table)
676   (intern (cond ((eq wnn-uniq-level 'wnn-uniq)
677                  (wnn-uniq-hash-string wnn-uniq))
678                 ((eq wnn-uniq-level 'wnn-uniq-entry)
679                  (wnn-uniq-hash-string wnn-uniq-entry))
680                 (t
681                  (wnn-uniq-hash-string nil)))
682           hash-table))
683
684 (defun wnn-uniq-candidates (candidates)
685   (if (eq wnn-uniq-level 'wnn-no-uniq)
686       candidates
687     (let ((hash-table (make-vector (length candidates) 0)))
688       (delq nil (mapcar (lambda (b)
689                           (let ((sym (wnn-uniq-hash b hash-table)))
690                             (if (null (boundp sym))
691                                 (set sym b))))
692                         candidates)))))
693
694 (defsubst wnn-uniq-bunsetsu-equal (bunsetsu-1 bunsetsu-2)
695   (and (or (eq wnn-uniq-level 'wnn-uniq-kanji)
696            (and (eq wnn-uniq-level 'wnn-uniq)
697                 (= (wnn-bunsetsu-get-hinshi bunsetsu-1)
698                    (wnn-bunsetsu-get-hinshi bunsetsu-2)))
699            (and (= (wnn-bunsetsu-get-dic-no bunsetsu-1)
700                    (wnn-bunsetsu-get-dic-no bunsetsu-2))
701                 (= (wnn-bunsetsu-get-entry bunsetsu-1)
702                    (wnn-bunsetsu-get-entry bunsetsu-2))
703                 (or (eq wnn-uniq-level 'wnn-uniq-entry)
704                     (= (wnn-bunsetsu-get-kangovect bunsetsu-1)
705                        (wnn-bunsetsu-get-kangovect bunsetsu-2)))))
706        (equal (wnn-bunsetsu-get-converted bunsetsu-1)
707               (wnn-bunsetsu-get-converted bunsetsu-2))
708        (equal (wnn-bunsetsu-get-fuzokugo bunsetsu-1)
709               (wnn-bunsetsu-get-fuzokugo bunsetsu-2))))
710
711 (defun wnn-uniq-bunsetsu-list-equal (b1 b2)
712   (while (and b1 b2 (wnn-uniq-bunsetsu-equal (car b1) (car b2)))
713     (setq b1 (cdr b1)
714           b2 (cdr b2)))
715   (and (null b1) (null b2)))
716
717 (defun wnn-candidate-pos (bunsetsu candidates)
718   (let ((n 0)
719         pos)
720     (while (and (null pos) candidates)
721       (if (wnn-uniq-bunsetsu-list-equal (car candidates) bunsetsu)
722           (setq pos n)
723         (setq candidates (cdr candidates)
724               n (1+ n))))
725     (or pos -1)))
726
727 (defun wnn-get-candidates-converted (candidates)
728   (mapcar 'wnn-get-major-bunsetsu-converted candidates))
729
730 (defun wnn-set-candidate-info (bunsetsu zenkouho)
731   (wnn-bunsetsu-set-zenkouho (car bunsetsu) zenkouho)
732   (mapcar (lambda (b) (wnn-bunsetsu-set-zenkouho b t)) (cdr bunsetsu)))
733
734 (defun wnn-list-candidates (bunsetsu prev-b next-b major)
735   (let* ((head (car bunsetsu))
736          (backend (egg-bunsetsu-get-backend head))
737          (env (wnn-bunsetsu-get-env head))
738          (yomi (wnn-get-major-bunsetsu-source bunsetsu))
739          (continue (eq (wnn-bunsetsu-get-zenkouho head) t))
740          pos cand converted hinshi fuzokugo v)
741     (if prev-b
742         (setq prev-b (egg-get-bunsetsu-tail prev-b)
743               hinshi (wnn-bunsetsu-get-hinshi prev-b)
744               fuzokugo (wnn-bunsetsu-get-fuzokugo prev-b))
745       (setq hinshi -1
746             fuzokugo ""))
747     (if next-b
748         (setq next-b (car next-b)
749               v (wnn-bunsetsu-get-kangovect next-b)))
750     (if (vectorp (wnn-bunsetsu-get-zenkouho head))
751         (setq pos (wnn-bunsetsu-get-zenkouho-pos head)
752               cand (wnn-bunsetsu-get-zenkouho-list head)))
753     (if (and pos
754              (wnn-bunsetsu-list-equal bunsetsu (nth pos cand))
755              (eq major (wnn-bunsetsu-get-zenkouho-dai head))
756              (eq prev-b (wnn-bunsetsu-get-zenkouho-prev-b head))
757              (eq next-b (wnn-bunsetsu-get-zenkouho-next-b head)))
758         (cons pos (wnn-bunsetsu-get-zenkouho-converted head))
759       (setq cand (wnn-get-bunsetsu-candidates env yomi hinshi fuzokugo v major))
760       (if (numberp cand)
761           (egg-error "%s" (wnnrpc-get-error-message (- cand))))
762       (setq pos (wnn-candidate-pos bunsetsu cand))
763       (cond ((< pos 0)
764              (setq cand (cons (wnn-bunsetsu-list-copy bunsetsu) cand)))
765             ((and (> pos 0)
766                   (null (eq (wnn-bunsetsu-get-zenkouho head) t)))
767              (setq cand (cons (nth pos cand) (delq (nth pos cand) cand)))))
768       (setq cand (wnn-uniq-candidates cand)
769             pos (wnn-candidate-pos bunsetsu cand)
770             converted (wnn-get-candidates-converted cand))
771       (wnn-set-candidate-info bunsetsu
772                               (wnn-zenkouho-create pos cand converted
773                                                    major prev-b next-b))
774       (wnn-add-freq-down head cand)
775       (cons pos converted))))
776
777 (defun wnn-decide-candidate (bunsetsu pos prev-b next-b)
778   (let* ((head (car bunsetsu))
779          (cand-list (wnn-bunsetsu-get-zenkouho-list head))
780          (cand (nth pos cand-list))
781          (c-head (car cand)))
782     (wnn-bunsetsu-set-zenkouho-pos head pos)
783     (wnn-bunsetsu-set-change-top c-head (wnn-bunsetsu-get-change-top head))
784     (wnn-bunsetsu-set-freq-down c-head (wnn-bunsetsu-get-freq-down head))
785     (wnn-merge-fi-rel c-head bunsetsu)
786     (wnn-major-bunsetsu-set-context cand (wnn-bunsetsu-get-context head))
787     (wnn-set-candidate-info cand (wnn-bunsetsu-get-zenkouho head))
788     (if (and prev-b (null wnn-one-level-conversion))
789         (progn
790           (setq prev-b (list (egg-get-bunsetsu-tail prev-b)))
791           (wnn-bunsetsu-set-dai-continue (car prev-b)
792                                          (wnn-bunsetsu-connect-prev c-head))))
793     (if next-b
794         (setq next-b (list (car next-b))))
795     (list cand prev-b next-b)))
796
797 (defun wnn-special-candidate (bunsetsu prev-b next-b major type)
798   (let* ((backend (egg-bunsetsu-get-backend (car bunsetsu)))
799          (lang (get backend 'language))
800          pos cand)
801     (when (and (eq lang (get backend 'source-language))
802                (eq lang (get backend 'converted-language)))
803       (setq pos (and (eq lang (get backend 'source-language))
804                      (eq lang (get backend 'converted-language))
805                      (cond ((eq lang 'Japanese)
806                             (cond ((eq type 'egg-hiragana) -1)
807                                   ((eq type 'egg-katakana) -2)))
808                            ((or (eq lang 'Chinese-GB) (eq lang 'Chinese-CNS))
809                             (cond ((eq type 'egg-pinyin) -1)
810                                   ((eq type 'egg-zhuyin) -1)))
811                            ((eq lang 'Korean)
812                             (cond ((eq type 'egg-hangul) -1))))))
813       (when pos
814         (setq cand (cdr (wnn-list-candidates bunsetsu prev-b next-b major))
815               pos (+ pos (length cand)))
816         (when (and (or (eq lang 'Chinese-GB) (eq lang 'Chinese-CNS)))
817           (let ((converted (nth pos cand)))
818             (cond ((egg-pinyin-syllable converted)
819                    (cond ((eq type 'egg-pinyin)) ; OK
820                          ((eq type 'egg-zhuyin)
821                           (wnn-pinyin-zhuyin-bunsetsu bunsetsu pos lang type))
822                          (t (setq pos nil))))
823                   ((egg-zhuyin-syllable converted)
824                    (cond ((eq type 'egg-pinyin)
825                           (wnn-pinyin-zhuyin-bunsetsu bunsetsu pos lang type))
826                          ((eq type 'egg-zhuyin)) ; OK
827                          (t (setq pos nil))))
828                   (t (setq pos nil))))))
829       (when pos
830         (wnn-decide-candidate bunsetsu pos prev-b next-b)))))
831
832 (defun wnn-pinyin-zhuyin-bunsetsu (bunsetsu pos lang type)
833   (let ((b (nth pos (wnn-bunsetsu-get-zenkouho-list (car bunsetsu))))
834         (encoding (if (eq lang 'Chinese-GB)
835                       (if (eq type 'egg-pinyin)
836                           'fixed-euc-py-cn 'fixed-euc-zy-cn)
837                     (if (eq type 'egg-pinyin)
838                         'fixed-euc-py-tw 'fixed-euc-zy-tw)))
839         (converted (wnn-bunsetsu-get-zenkouho-converted (car bunsetsu)))
840         str)
841     (setcar (nthcdr pos converted)
842             (wnn-pinyin-zhuyin-string (nth pos converted) encoding))
843     (while b
844       (setq str (wnn-bunsetsu-get-converted (car b)))
845       (when str
846         (wnn-bunsetsu-set-converted
847          (car b)
848          (wnn-pinyin-zhuyin-string str encoding)))
849       (setq str (wnn-bunsetsu-get-fuzokugo (car b)))
850       (when str
851         (wnn-bunsetsu-set-fuzokugo
852          (car b)
853          (wnn-pinyin-zhuyin-string str encoding)))
854       (setq b (cdr b)))))
855
856 (defun wnn-pinyin-zhuyin-string (str encoding)
857   (decode-coding-string (encode-coding-string str encoding) encoding))
858
859 (defun wnn-change-bunsetsu-length (bunsetsu prev-b next-b len major)
860   (let ((backend (egg-bunsetsu-get-backend (car bunsetsu)))
861         (env (wnn-bunsetsu-get-env (car bunsetsu)))
862         (tail (egg-get-bunsetsu-tail prev-b))
863         (yomi (wnn-get-major-bunsetsu-source bunsetsu))
864         (context (wnn-bunsetsu-get-context (car bunsetsu)))
865         yomi1 yomi2 hinshi fuzokugo new)
866     (if tail
867         (setq hinshi (wnn-bunsetsu-get-hinshi tail)
868               fuzokugo (wnn-bunsetsu-get-fuzokugo tail))
869       (setq hinshi -1
870             fuzokugo ""))
871     (setq yomi1 (substring yomi 0 len)
872           yomi2 (concat (substring yomi len)
873                         (wnn-get-major-bunsetsu-source next-b)))
874     (setq new (wnn-tanbunsetsu-conversion env yomi1 hinshi fuzokugo nil major))
875     (if (numberp new)
876         (egg-error "%s" (wnnrpc-get-error-message (- new))))
877     (if (and prev-b (null wnn-one-level-conversion))
878         (wnn-bunsetsu-set-dai-continue tail
879                                        (wnn-bunsetsu-connect-prev (car new))))
880     (wnn-bunsetsu-set-change-top (car new) t)
881     (wnn-merge-freq-down (car new) bunsetsu)
882     (wnn-merge-fi-rel (car new) bunsetsu)
883     (wnn-merge-fi-rel (car new) next-b)
884     (wnn-major-bunsetsu-set-context new context)
885     (if (= (length yomi2) 0)
886         (setq next-b nil)
887       (setq tail (egg-get-bunsetsu-tail new)
888             next-b (wnn-renbunsetsu-conversion env yomi2
889                                                (wnn-bunsetsu-get-hinshi tail)
890                                                (wnn-bunsetsu-get-fuzokugo tail)
891                                                nil context))
892       (if (numberp next-b)
893           (egg-error "%s" (wnnrpc-get-error-message (- next-b))))
894       (if (and (null major) (null wnn-one-level-conversion))
895           (wnn-bunsetsu-set-dai-continue
896            tail
897            (wnn-bunsetsu-connect-prev (car next-b)))))
898     (list new prev-b next-b)))
899
900 (defun wnn-add-freq-down (bunsetsu down-list)
901   (let ((freq-down (wnn-bunsetsu-get-freq-down bunsetsu))
902         b-list b pair)
903     (while down-list
904       (setq b-list (car down-list)
905             down-list (cdr down-list))
906       (while b-list
907         (setq b (car b-list)
908               b-list (cdr b-list)
909               pair (cons (wnn-bunsetsu-get-dic-no b)
910                          (wnn-bunsetsu-get-entry b)))
911         (if (and (/= (wnn-bunsetsu-get-right-now b) 0)
912                  (/= (car pair) -1)
913                  (null (member pair freq-down)))
914             (setq freq-down (cons pair freq-down)))))
915     (wnn-bunsetsu-set-freq-down bunsetsu freq-down)))
916
917 (defun wnn-merge-freq-down (bunsetsu b-list)
918   (let ((freq-down0 (wnn-bunsetsu-get-freq-down bunsetsu))
919         freq-down1)
920     (while b-list
921       (setq freq-down1 (wnn-bunsetsu-get-freq-down (car b-list))
922             b-list (cdr b-list))
923       (while freq-down1
924         (if (null (member (car freq-down1) freq-down0))
925             (setq freq-down0 (cons (car freq-down1) freq-down0)))
926         (setq freq-down1 (cdr freq-down1)))
927     (wnn-bunsetsu-set-freq-down bunsetsu freq-down0))))
928
929 (defun wnn-merge-fi-rel (bunsetsu b-list)
930   (let ((fi-rel (cons nil (wnn-bunsetsu-get-fi-rel bunsetsu))))
931     (if (eq bunsetsu (car b-list))
932         (setq b-list (cdr b-list)))
933     (while b-list
934       (nconc fi-rel (wnn-bunsetsu-get-fi-rel (car b-list)))
935       (wnn-bunsetsu-set-fi-rel (car b-list) nil)
936       (setq b-list (cdr b-list)))
937     (wnn-bunsetsu-set-fi-rel bunsetsu (cdr fi-rel))))
938
939 (defun wnn-bunsetsu-combinable-p (bunsetsu1 bunsetsu2)
940   (eq (wnn-bunsetsu-get-env bunsetsu1)
941       (wnn-bunsetsu-get-env bunsetsu2)))
942
943 (defvar wnn-auto-save-dic-count 0)
944
945 (defun wnn-end-conversion (bunsetsu-list abort)
946   (unless abort
947     (let* ((head (car bunsetsu-list))
948            (env (wnn-bunsetsu-get-env head)))
949       (prog1
950           (if (wnnenv-is-wnn6 env)
951               (progn
952                 (wnn-clear-now-flag bunsetsu-list)
953                 (wnn-merge-fi-rel head (cdr bunsetsu-list))
954                 (wnnrpc-set-fi-priority env (wnn-bunsetsu-get-fi-rel head))
955                 (wnn-optimize-in-local bunsetsu-list)
956                 (wnn-optimize-in-server bunsetsu-list))
957             (wnn-clear-now-flag bunsetsu-list)
958             (wnn-count-up-frequency bunsetsu-list))
959         (setq wnn-auto-save-dic-count (1+ wnn-auto-save-dic-count))
960         (when (eq wnn-auto-save-dic-count wnn-auto-save-dictionaries)
961           (wnn-save-dictionaries env)
962           (setq wnn-auto-save-dic-count 0))))))
963
964 (defun wnn-clear-now-flag (bunsetsu-list)
965   (let ((env (wnn-bunsetsu-get-env (car bunsetsu-list))))
966     (while bunsetsu-list
967       (setq fd (wnn-bunsetsu-get-freq-down (car bunsetsu-list))
968             bunsetsu-list (cdr bunsetsu-list))
969       (while fd
970         (wnnrpc-set-frequency env (caar fd) (cdar fd)
971                               (WNN-const IMA_OFF) (WNN-const HINDO_NOP))
972         (setq fd (cdr fd))))))
973
974 (defun wnn-count-up-frequency (bunsetsu-list)
975   (let ((env (wnn-bunsetsu-get-env (car bunsetsu-list)))
976         (context (wnn-null-context))
977         dic-no entry b)
978     (while bunsetsu-list
979       (setq b (car bunsetsu-list)
980             bunsetsu-list (cdr bunsetsu-list)
981             dic-no (wnn-bunsetsu-get-dic-no b)
982             entry (wnn-bunsetsu-get-entry b)
983             context (cons (wnn-context-create dic-no entry
984                                               (wnn-bunsetsu-get-jirilen b)
985                                               (wnn-bunsetsu-get-hinshi b)
986                                               (wnn-bunsetsu-get-fuzokugo b)
987                                               (wnn-bunsetsu-get-converted b)
988                                               (wnn-bunsetsu-get-right-now b)
989                                               (wnn-bunsetsu-get-freq b))
990                           context))
991       (wnnrpc-set-frequency env dic-no entry
992                             (WNN-const IMA_ON) (WNN-const HINDO_INC)))
993     (list (car context) (nth 1 context))))
994
995 (defun wnn-optimize-in-local (bunsetsu-list)
996   (let ((env (wnn-bunsetsu-get-env (car bunsetsu-list)))
997         b prev-b next-b major-top entry hinshi)
998     (setq next-b (car bunsetsu-list)
999           bunsetsu-list (cdr bunsetsu-list))
1000     (cond
1001      ((eq (wnnenv-get-server-type env) 'jserver)
1002       (while next-b
1003         (setq major-top (null (and b (wnn-bunsetsu-get-dai-continue b)))
1004               prev-b b
1005               b next-b
1006               next-b (car bunsetsu-list)
1007               bunsetsu-list (cdr bunsetsu-list)
1008               hinshi (wnn-bunsetsu-get-hinshi b))
1009         (when (or
1010                (and (/= (wnnenv-get-notrans env) (WNN-const DIC_RDONLY))
1011                     (= (wnn-bunsetsu-get-dic-no b) -1)
1012                     (or (= (wnn-bunsetsu-get-entry b) (WNN-const HIRAGANA))
1013                         (= (wnn-bunsetsu-get-entry b) (WNN-const KATAKANA)))
1014                     (>= (wnn-bunsetsu-get-jirilen b) (WNN-const LEARNING_LEN)))
1015                (= (wnn-bunsetsu-get-entry b) (WNN-const IKEIJI_ENTRY)))
1016           (setq entry (wnn-notrans-auto-learning b))
1017           (when (/= entry -1)
1018             (wnn-bunsetsu-set-dic-no b (WNN-const MUHENKAN_DIC))
1019             (wnn-bunsetsu-set-entry b entry)))
1020         (cond
1021          ((and next-b
1022                major-top
1023                (wnn-bunsetsu-get-dai-continue b))
1024           (wnn-adjacent-learning b next-b))
1025          ((and prev-b
1026                (= hinshi (wnnenv-get-hinshi env 'rendaku))
1027                (equal (wnn-bunsetsu-get-fuzokugo prev-b) ""))
1028           (wnn-adjacent-learning prev-b b))
1029          ((and next-b
1030                (= hinshi (wnnenv-get-hinshi env 'settou)))
1031           (wnn-adjacent-learning b next-b))
1032          ((and (/= (wnnenv-get-bmodify env) (WNN-const DIC_RDONLY))
1033                (wnn-bunsetsu-get-change-top b)
1034                next-b
1035                (/= (wnn-bunsetsu-get-hinshi next-b)
1036                    (wnnenv-get-hinshi env 'rendaku))
1037                (/= hinshi (wnnenv-get-hinshi env 'settou)))
1038           (wnn-bmodify-learning b next-b)))))
1039      ((eq (wnnenv-get-server-type env) 'kserver)
1040       ;; Soory, not implemented
1041       nil))))
1042
1043 (defun wnn-notrans-auto-learning (bunsetsu)
1044   (let ((env (wnn-bunsetsu-get-env bunsetsu)))
1045     (wnnrpc-auto-learning env (WNN-const NOTRANS_LEARN)
1046                           (wnn-bunsetsu-get-yomi bunsetsu)
1047                           (wnn-bunsetsu-get-converted bunsetsu)
1048                           ""
1049                           (if (= (wnn-bunsetsu-get-entry bunsetsu)
1050                                  (WNN-const IKEIJI_ENTRY))
1051                               (wnn-bunsetsu-get-hinshi bunsetsu)
1052                             (wnnenv-get-hinshi env 'noun))
1053                           0)))
1054
1055 (defun wnn-adjacent-learning (bunsetsu1 bunsetsu2)
1056   (let ((env (wnn-bunsetsu-get-env bunsetsu1))
1057         (yomi (concat (wnn-bunsetsu-get-yomi bunsetsu1)
1058                       (wnn-bunsetsu-get-yomi bunsetsu2)))
1059         (kanji (concat (wnn-bunsetsu-get-converted bunsetsu1)
1060                        (wnn-bunsetsu-get-converted bunsetsu2)))
1061         (hinshi (wnnenv-get-hinshi env 'noun)))
1062     (if (= (wnnenv-get-bmodify env) (WNN-const DIC_RW))
1063         (wnnrpc-auto-learning env (WNN-const BMODIFY_LEARN)
1064                               yomi kanji "" hinshi 0)
1065       (wnnrpc-temporary-learning env yomi kanji "" hinshi 0))))
1066
1067 (defun wnn-bmodify-learning (bunsetsu1 bunsetsu2)
1068   (let ((env (wnn-bunsetsu-get-env bunsetsu1))
1069         (yomi (concat (wnn-bunsetsu-get-yomi bunsetsu1)
1070                       (wnn-bunsetsu-get-fuzokugo bunsetsu1)
1071                       (wnn-bunsetsu-get-yomi bunsetsu2)))
1072         (kanji (concat (wnn-bunsetsu-get-converted bunsetsu1)
1073                        (wnn-bunsetsu-get-fuzokugo bunsetsu1)
1074                        (wnn-bunsetsu-get-converted bunsetsu2)))
1075         (hinshi (wnn-bunsetsu-get-hinshi bunsetsu2)))
1076     (wnnrpc-auto-learning env (WNN-const BMODIFY_LEARN)
1077                           yomi kanji "" hinshi 0)))
1078
1079 (defun wnn-optimize-in-server (bunsetsu-list)
1080   (let ((env (wnn-bunsetsu-get-env (car bunsetsu-list)))
1081         (context (wnn-bunsetsu-get-context (car bunsetsu-list)))
1082         b)
1083     (when (eq (wnnenv-get-server-type env) 'jserver)
1084       (wnn-context-set-right-now (car context) (WNN-const HINDO_NOP))
1085       (wnn-context-set-freq (car context) (WNN-const HINDO_NOP))
1086       (wnn-context-set-right-now (nth 1 context) (WNN-const HINDO_NOP))
1087       (wnn-context-set-freq (nth 1 context) (WNN-const HINDO_NOP))
1088       (while bunsetsu-list
1089         (setq b (car bunsetsu-list)
1090               bunsetsu-list (cdr bunsetsu-list)
1091               context (cons (wnn-context-create (wnn-bunsetsu-get-dic-no b)
1092                                                 (wnn-bunsetsu-get-entry b)
1093                                                 (wnn-bunsetsu-get-jirilen b)
1094                                                 (wnn-bunsetsu-get-hinshi b)
1095                                                 (wnn-bunsetsu-get-fuzokugo b)
1096                                                 (wnn-bunsetsu-get-converted b)
1097                                                 (WNN-const IMA_ON)
1098                                                 (WNN-const HINDO_INC))
1099                             context)))
1100       (prog1
1101           (list (car context) (nth 1 context))
1102         (wnnrpc-optimize-fi env (nreverse context))))))
1103
1104 (defun wnn-finalize-backend (lang &optional action)
1105   (let* ((save-inhibit-quit inhibit-quit)
1106          (inhibit-quit t)
1107          (server-info (wnn-server-get-info lang))
1108          (server-type (wnn-server-type server-info))
1109          (env-list wnn-environments)
1110          env proc-list saved)
1111     (when server-type
1112       (while env-list
1113         (setq env (car env-list)
1114               env-list (cdr env-list))
1115         (if (eq (wnnenv-get-server-type env) server-type)
1116             (condition-case err
1117                 (progn
1118                   (unless (memq (wnnenv-get-proc env) proc-list)
1119                     (setq proc-list (cons (wnnenv-get-proc env) proc-list)))
1120                   (unless (eq action 'disconnect-only)
1121                     (unless saved
1122                       (setq saved t)
1123                       (message (egg-get-message 'wnn-dict-saving)
1124                                (wnn-server-proc-name server-info)))
1125                     (let ((inhibit-quit save-inhibit-quit))
1126                       (wnn-save-dictionaries env)))
1127                   (unless (eq action 'save-only)
1128                     (wnnrpc-disconnect env)))
1129               ((error quit)
1130                (message "signal %S occured when dictionary saving" err)))))
1131       (if saved
1132           (message (egg-get-message 'wnn-dict-saved)
1133                    (wnn-server-proc-name server-info)))
1134       (unless (eq action 'save-only)
1135         (while proc-list
1136           (if (and (car proc-list)
1137                    (eq (process-status (car proc-list)) 'open))
1138               (wnnrpc-close (car proc-list)))
1139           (setq proc-list (cdr proc-list)))))))
1140
1141 (defun wnn-close (lang)
1142   "Save dictionaries and close the Wnn session."
1143   (interactive (list (wnn-read-active-lang)))
1144   (or (listp lang)
1145       (setq lang (list lang)))
1146   (while lang
1147     (wnn-finalize-backend (car lang))
1148     (setq lang (cdr lang))))
1149
1150 (defun wnn-disconnect (lang)
1151   "Disconnect the Wnn session without dictionary saving."
1152   (interactive (list (wnn-read-active-lang)))
1153   (or (listp lang)
1154       (setq lang (list lang)))
1155   (while lang
1156     (wnn-finalize-backend (car lang) 'disconnect-only)
1157     (setq lang (cdr lang))))
1158
1159 (defun wnn-dictionary-save (lang)
1160   "Save Wnn dictionaries."
1161   (interactive (list (wnn-read-active-lang)))
1162   (or (listp lang)
1163       (setq lang (list lang)))
1164   (while lang
1165     (wnn-finalize-backend (car lang) 'save-only)
1166     (setq lang (cdr lang))))
1167
1168 (defun wnn-read-active-lang ()
1169   (let ((completion-ignore-case t)
1170         (env wnn-environments)
1171         langs server server-list)
1172     (while env
1173       (setq server (wnnenv-get-server-type (car env))
1174             env (cdr env))
1175       (if (null (memq server server-list))
1176           (setq server-list (cons server server-list))))
1177     (setq langs (delq nil
1178                       (mapcar (lambda (info)
1179                                 (if (memq (wnn-server-type info) server-list)
1180                                     (wnn-server-language info)))
1181                               wnn-server-info-list)))
1182     (if (<= (length langs) 1)
1183         langs
1184       (setq langs (cons (cons "All" langs)
1185                         (mapcar (lambda (lang) (cons (symbol-name lang) lang))
1186                                 langs)))
1187       (cdr (assoc (completing-read "language? " langs nil t nil nil "All")
1188                   langs)))))
1189 \f
1190 ;;
1191 (defun wnn-comm-sentinel (proc reason)  ; assume it is close
1192   (let ((inhibit-quit t))
1193     (kill-buffer (process-buffer proc))
1194     ;; delete env from the list.
1195     (setq wnn-environments
1196           (delq nil (mapcar (lambda (env)
1197                               (if (null (eq (wnnenv-get-proc env) proc))
1198                                   env))
1199                               wnn-environments)))))
1200
1201 (defun wnn-open (server-info)
1202   "Establish the connection to WNN server.  Return process object."
1203   ;; Open the session to WNN server,
1204   (let ((save-inhibit-quit inhibit-quit)
1205         (inhibit-quit t)
1206         (server-type (wnn-server-type server-info))
1207         (port (wnn-server-port server-info))
1208         (hostname-list (wnn-server-hostname server-info))
1209         (proc-name (wnn-server-proc-name server-info))
1210         (msg-form "Wnn: connecting to %S at %s...")
1211         (user-name (user-login-name))
1212         buf hostname myname port-off proc result msg)
1213     (unwind-protect
1214         (progn
1215           (setq buf (generate-new-buffer (wnn-server-buffer-name server-info)))
1216           (save-excursion
1217             (set-buffer buf)
1218             (erase-buffer)
1219             (buffer-disable-undo)
1220             (set-buffer-multibyte nil)
1221             (setq egg-fixed-euc (wnn-server-coding-system server-info)))
1222           (or (consp hostname-list)
1223               (setq hostname-list (list hostname-list)))
1224           (while (and hostname-list (null proc))
1225             (setq hostname (or (car hostname-list) "")
1226                   hostname-list (cdr hostname-list)
1227                   myname (if (equal hostname "") "unix" wnn-system-name))
1228             (if (null (string-match ":" hostname))
1229                 (setq port-off 0)
1230               (setq port-off (string-to-int (substring hostname (match-end 0)))
1231                     hostname (substring hostname 0 (match-beginning 0))))
1232             (and (equal hostname "") (setq hostname "localhost"))
1233             (let ((inhibit-quit save-inhibit-quit))
1234               (if (and msg
1235                        (null (y-or-n-p (format "%s failed. Try to %s? "
1236                                                msg hostname))))
1237                   (egg-error "abort connect")))
1238             (setq msg (format "Wnn: connecting to %S at %s..."
1239                               server-type hostname))
1240             (message "%s" msg)
1241             (let ((inhibit-quit save-inhibit-quit))
1242               (condition-case nil
1243                   (setq proc (open-network-stream proc-name buf hostname
1244                                                   (+ port port-off)))
1245                 ((error quit))))
1246             (when proc
1247               (process-kill-without-query proc)
1248               (set-process-coding-system proc 'binary 'binary)
1249               (set-process-sentinel proc 'wnn-comm-sentinel)
1250               (set-marker-insertion-type (process-mark proc) t)
1251               (setq result (wnnrpc-open proc myname user-name))
1252               (when (numberp result)
1253                 (delete-process proc)
1254                 (setq proc nil))))
1255           (cons proc result))
1256       (if proc
1257           (message (concat msg "done"))
1258         (if buf (kill-buffer buf))
1259         (egg-error 'wnn-connect-error)))))
1260
1261 (defvar wnn-envspec-list nil)
1262 (defvar wnn-current-envspec nil)
1263 (defvar wnn-current-envspec-reverse nil)
1264 (defvar wnn-server-type nil)
1265 (defvar wnn-wnn6-server nil)
1266
1267 (defmacro wnn-envspec-conv-param-name-list ()
1268   ''(last-is-first complex okuri-learn okuri
1269      prefix-learn prefix suffix-learn common-learn freq-func
1270      numeric alphabet symbol yuragi rendaku bunsetsugiri muhenkan
1271      fi-relation-learn fi-freq-func))
1272
1273 (defmacro wnn-envspec-conv-param-length ()
1274   (length (wnn-envspec-conv-param-name-list)))
1275
1276 (defun wnn-envspec-create (env-name tankan stickey)
1277   (vector (and env-name (setq env-name (intern env-name)))
1278           (wnn-make-backend egg-language env-name)
1279           tankan stickey nil nil
1280           0 (make-vector (wnn-envspec-conv-param-length) 0)
1281           (list nil) (list nil) (list nil)))
1282
1283 (defsubst wnn-envspec-env-type (spec)           (aref spec 0))
1284 (defsubst wnn-envspec-backend (spec)            (aref spec 1))
1285 (defsubst wnn-envspec-tankan (spec)             (aref spec 2))
1286 (defsubst wnn-envspec-sticky (spec)             (aref spec 3))
1287 (defsubst wnn-envspec-param (spec)              (aref spec 4))
1288 (defsubst wnn-envspec-fuzokugo (spec)           (aref spec 5))
1289 (defsubst wnn-envspec-conv-vmask (spec)         (aref spec 6))
1290 (defsubst wnn-envspec-conv-param (spec)         (aref spec 7))
1291 (defsubst wnn-envspec-dic-list (spec)           (cdr (aref spec 8)))
1292 (defsubst wnn-envspec-fi-dic-list (spec)        (cdr (aref spec 9)))
1293 (defsubst wnn-envspec-autolearn-dic-list (spec) (cdr (aref spec 10)))
1294
1295 (defsubst wnn-envspec-set-param (spec param)
1296   (aset spec 4 param))
1297 (defsubst wnn-envspec-set-fuzokugo (spec fzk)
1298   (aset spec 5 fzk))
1299 (defsubst wnn-envspec-set-conv-vmask (spec val)
1300   (aset spec 6 val))
1301 (defsubst wnn-envspec-set-conv-param (spec num val)
1302   (aset (aref spec 7) num val))
1303 (defsubst wnn-envspec-add-dic-list (spec &rest dic)
1304   (nconc (aref spec 8) (list (apply 'vector dic))))
1305 (defsubst wnn-envspec-add-fi-dic-list (spec &rest dic)
1306   (nconc (aref spec 9) (list (apply 'vector dic))))
1307 (defsubst wnn-envspec-add-autolearn-dic-list (spec type &rest dic)
1308   (nconc (aref spec 10) (list (cons type (apply 'vector dic)))))
1309
1310 (eval-when-compile
1311   (defun wnn-conv-param (param)
1312     (- (wnn-envspec-conv-param-length)
1313        (length (memq param (wnn-envspec-conv-param-name-list))))))
1314
1315 (defmacro define-wnn-conv-param-func ()
1316   (let ((name-list (wnn-envspec-conv-param-name-list))
1317         (defs (list 'progn))
1318         n set get)
1319     (while name-list
1320       (setq n (car name-list)
1321             name-list (cdr name-list)
1322             set (intern (format "wnn-envspec-set-conv-param-%s" n))
1323             get (intern (format "wnn-get-conv-param-%s" n)))
1324       (nconc defs `((defsubst ,set (spec val)
1325                       (wnn-envspec-set-conv-param spec ,(wnn-conv-param n) val)
1326                       (wnn-envspec-set-conv-vmask
1327                        spec (logior (wnn-envspec-conv-vmask spec)
1328                                     ,(lsh 1 (wnn-conv-param n)))))
1329                     (defsubst ,get (param)
1330                       (aref param ,(wnn-conv-param n))))))
1331     defs))
1332
1333 (define-wnn-conv-param-func)
1334
1335 (defmacro wnn-arg-type-error (func)
1336   `(egg-error ,(format "%s: Wrong type argument" func)))
1337
1338 (defun wnn-define-environment (reverse &optional env-name tankan stickey)
1339   "Define a Wnn environment for normal/reverse conversion according
1340 to REVERSE.  ENV-NAME specifies suffix of the Wnn environment name.
1341 Make single character conversion (Tan-Kanji conversion) environment,
1342 if tankan is non-NIL.  Make the environment as sticky, if STICKEY
1343 is non-NIL."
1344   (if (and env-name (null (stringp env-name)))
1345       (wnn-arg-type-error wnn-define-environment))
1346   (setq env-name (if reverse (concat env-name "R") env-name)
1347         wnn-current-envspec (wnn-envspec-create env-name tankan stickey)
1348         wnn-current-envspec-reverse reverse
1349         wnn-envspec-list (nconc wnn-envspec-list
1350                                 (list wnn-current-envspec))))
1351
1352 (defun wnn-set-fuzokugo (filename)
1353   (cond ((equal filename "")
1354          (setq filename nil))
1355         ((null (stringp filename))
1356          (wnn-arg-type-error wnn-set-fuzokugo)))
1357   (wnn-envspec-set-fuzokugo wnn-current-envspec filename))
1358
1359 (defmacro wnn-add-dict-param-check (func
1360                                     dict &optional freq prior drw dmax frw fmax
1361                                     dpass fpass rev)
1362   `(progn
1363      (if (or (and (null (stringp ,dict)) (null (stringp (car-safe ,dict))))
1364              ,@(if freq
1365                    `((and ,freq (null (stringp ,freq))
1366                           (null (stringp (car-safe ,freq))))))
1367              ,@(if prior `((null (integerp ,prior))))
1368              ,@(if drw
1369                    `((null (or (eq ,drw nil) (eq ,drw t)
1370                                (eq ,drw 0) (eq ,drw 1)
1371                                ,@(if dmax
1372                                      `((and wnn-wnn6-server
1373                                             ,@(let ((x `((eq ,drw 2))))
1374                                                 (when (>= dmax 3)
1375                                                   (nconc x `((eq ,drw 3))))
1376                                                 (when (>= dmax 4)
1377                                                   (nconc x `((eq ,drw 4))))
1378                                                 x))))))))
1379              ,@(if frw
1380                    `((null (or (eq ,frw nil) (eq ,frw t)
1381                                (eq ,frw 0) (eq ,frw 1)
1382                                ,@(if fmax
1383                                      `((and wnn-wnn6-server
1384                                             ,@(let ((x `((eq ,frw 2))))
1385                                                 (when (>= fmax 3)
1386                                                   (nconc x `((eq ,frw 3))))
1387                                                 (when (>= fmax 4)
1388                                                   (nconc x `((eq ,frw 4))))
1389                                                 x))))))))
1390              ,@(if dpass `((and ,dpass (null (stringp ,dpass)))))
1391              ,@(if fpass `((and ,fpass (null (stringp ,fpass))))))
1392          (wnn-arg-type-error ,func))
1393      (if (or (equal ,dict "") (equal (car-safe ,dict) ""))
1394          (egg-error ,(format "%s: Dictionary name should not be null." func)))
1395      ,@(if freq
1396            `((if (or (equal ,freq "") (equal (car-safe ,freq) ""))
1397                  (setq ,freq nil))))
1398      ,@(if rev
1399            `((setq ,rev (if ,rev (car ,rev) wnn-current-envspec-reverse))))))
1400
1401 (defmacro wnn-wnn6-env-func (func)
1402   `(or wnn-wnn6-server
1403        (egg-error ,(format "%s is available only on Wnn6" func))))
1404
1405 (defun wnn-add-dict (dict freq priority dict-rw freq-rw
1406                      &optional dict-passwd freq-passwd &rest reverse)
1407   (wnn-add-dict-param-check wnn-add-dict
1408                             dict freq priority dict-rw 4 freq-rw 2
1409                             dict-passwd freq-passwd reverse)
1410   (wnn-envspec-add-dic-list wnn-current-envspec
1411                             dict freq priority dict-rw freq-rw
1412                             dict-passwd freq-passwd reverse))
1413
1414 (defun wnn-add-fisys-dict (dict freq freq-rw &optional freq-passwd)
1415   (wnn-wnn6-env-func wnn-add-fisys-dict)
1416   (wnn-add-dict-param-check wnn-add-fisys-dict
1417                             dict freq nil nil nil freq-rw 3
1418                             nil freq-passwd)
1419   (wnn-envspec-add-fi-dic-list wnn-current-envspec
1420                                dict freq t nil freq-rw nil freq-passwd nil))
1421
1422 (defun wnn-add-fiusr-dict (dict freq dict-rw freq-rw
1423                            &optional dict-passwd freq-passwd)
1424   (wnn-wnn6-env-func wnn-add-fiusr-dict)
1425   (wnn-add-dict-param-check wnn-add-fiusr-dict
1426                             dict freq nil dict-rw 3 freq-rw 3
1427                             dict-passwd freq-passwd)
1428   (wnn-envspec-add-fi-dic-list wnn-current-envspec
1429                                dict freq nil dict-rw freq-rw
1430                                dict-passwd freq-passwd nil))
1431
1432 (defun wnn-add-notrans-dict (dict priority dict-rw
1433                              &optional dict-passwd &rest reverse)
1434   (wnn-wnn6-env-func wnn-add-notrans-dict)
1435   (wnn-add-dict-param-check wnn-add-notrans-dict
1436                             dict nil priority dict-rw nil nil nil
1437                             dict-passwd nil reverse)
1438   (wnn-envspec-add-autolearn-dic-list wnn-current-envspec
1439                                       (WNN-const NOTRANS_LEARN)
1440                                       dict nil priority dict-rw nil
1441                                       dict-passwd nil reverse))
1442
1443 (defun wnn-add-bmodify-dict (dict priority dict-rw
1444                              &optional dict-passwd &rest reverse)
1445   (wnn-wnn6-env-func wnn-add-notrans-dict)
1446   (wnn-add-dict-param-check wnn-add-bmodify-dict
1447                             dict nil priority dict-rw nil nil nil
1448                             dict-passwd nil reverse)
1449   (wnn-envspec-add-autolearn-dic-list wnn-current-envspec
1450                                       (WNN-const BMODIFY_LEARN)
1451                                       dict nil priority dict-rw nil
1452                                       dict-passwd nil reverse))
1453
1454 (defun wnn-set-param (&rest args)
1455   (if (/= (length args) 17)
1456       (egg-error "wnn-set-param: Wrong number of arguments"))
1457   (mapcar (lambda (n)
1458             (if (null (integerp n))
1459                 (wnn-arg-type-error wnn-set-param)))
1460           args)
1461   (wnn-envspec-set-param wnn-current-envspec (apply 'vector args)))
1462
1463 (defmacro wnn-boolean-param-check (func flag)
1464   `(setq ,flag (cond ((or (eq ,flag 0) (eq ,flag nil)) 0)
1465                      ((or (eq ,flag 1) (eq ,flag t))   1)
1466                      (t (wnn-arg-type-error ,func)))))
1467
1468 (defun wnn-set-last-is-first-mode (flag)
1469   (wnn-wnn6-env-func wnn-set-last-is-first-mode)
1470   (wnn-boolean-param-check wnn-set-last-is-first-mode flag)
1471   (wnn-envspec-set-conv-param-last-is-first wnn-current-envspec flag))
1472
1473 (defun wnn-set-complex-conv-mode (flag)
1474   (wnn-wnn6-env-func wnn-set-complex-conv-mode)
1475   (wnn-boolean-param-check wnn-set-complex-conv-mode flag)
1476   (wnn-envspec-set-conv-param-complex wnn-current-envspec flag))
1477
1478 (defun wnn-set-okuri-learn-mode (flag)
1479   (wnn-wnn6-env-func wnn-set-okuri-learn-mode)
1480   (wnn-boolean-param-check wnn-set-okuri-learn-mode flag)
1481   (wnn-envspec-set-conv-param-okuri-learn wnn-current-envspec flag))
1482
1483 (defun wnn-set-okuri-flag (mode)
1484   (wnn-wnn6-env-func wnn-set-okuri-flag)
1485   (setq mode (cond ((or (eq mode -1) (eq mode 'regulation)) -1)
1486                    ((or (eq mode  0) (eq mode 'no))          0)
1487                    ((or (eq mode  1) (eq mode 'yes))         1)
1488                    (t (wnn-arg-type-error wnn-set-okuri-flag))))
1489   (wnn-envspec-set-conv-param-okuri wnn-current-envspec mode))
1490
1491 (defun wnn-set-prefix-learn-mode (flag)
1492   (wnn-wnn6-env-func wnn-set-prefix-learn-mode)
1493   (wnn-boolean-param-check wnn-set-prefix-learn-mode flag)
1494   (wnn-envspec-set-conv-param-prefix-learn wnn-current-envspec flag))
1495
1496 (defun wnn-set-prefix-flag (mode)
1497   (wnn-wnn6-env-func wnn-set-prefix-flag)
1498   (setq mode (cond ((or (eq mode 0) (eq mode 'hiragana)) 0)
1499                    ((or (eq mode 1) (eq mode 'kanji))    1)
1500                    (t (wnn-arg-type-error wnn-set-prefix-flag))))
1501   (wnn-envspec-set-conv-param-prefix wnn-current-envspec mode))
1502
1503 (defun wnn-set-suffix-learn-mode (flag)
1504   (wnn-wnn6-env-func wnn-set-suffix-learn-mode)
1505   (wnn-boolean-param-check wnn-set-suffix-learn-mode flag)
1506   (wnn-envspec-set-conv-param-suffix-learn wnn-current-envspec flag))
1507
1508 (defun wnn-set-common-learn-mode (flag)
1509   (wnn-wnn6-env-func wnn-set-common-learn-mode)
1510   (wnn-boolean-param-check wnn-set-common-learn-mode flag)
1511   (wnn-envspec-set-conv-param-common-learn wnn-current-envspec flag))
1512
1513 (defun wnn-set-freq-func-mode (mode)
1514   (wnn-wnn6-env-func wnn-set-freq-func-mode)
1515   (setq mode (cond ((or (eq mode 0) (eq mode 'not))    0)
1516                    ((or (eq mode 1) (eq mode 'always)) 1)
1517                    ((or (eq mode 2) (eq mode 'high))   2)
1518                    ((or (eq mode 3) (eq mode 'normal)) 3)
1519                    ((or (eq mode 4) (eq mode 'low))    4)
1520                    (t (wnn-arg-type-error wnn-set-freq-func-mode))))
1521   (wnn-envspec-set-conv-param-freq-func wnn-current-envspec mode))
1522
1523 (defun wnn-set-numeric-mode (mode)
1524   (wnn-wnn6-env-func wnn-set-numeric-mode)
1525   (setq mode (cond ((or (eq mode  -2) (eq mode 'han))       -2)
1526                    ((or (eq mode -12) (eq mode 'zen))      -12)
1527                    ((or (eq mode -13) (eq mode 'kan))      -13)
1528                    ((or (eq mode -15) (eq mode 'kansuuji)) -15)
1529                    ((or (eq mode -16) (eq mode 'kanold))   -16)
1530                    ((or (eq mode -17) (eq mode 'hancan))   -17)
1531                    ((or (eq mode -18) (eq mode 'zencan))   -18)
1532                    (t (wnn-arg-type-error wnn-set-numeric-mode))))
1533   (wnn-envspec-set-conv-param-numeric wnn-current-envspec mode))
1534
1535 (defun wnn-set-alphabet-mode (mode)
1536   (wnn-wnn6-env-func wnn-set-alphabet-mode)
1537   (setq mode (cond ((or (eq mode  -4) (eq mode 'han))  -4)
1538                    ((or (eq mode -30) (eq mode 'zen)) -30)
1539                    (t (wnn-arg-type-error wnn-set-alphabet-mode))))
1540   (wnn-envspec-set-conv-param-alphabet wnn-current-envspec mode))
1541
1542 (defun wnn-set-symbol-mode (mode)
1543   (wnn-wnn6-env-func wnn-set-symbol-mode)
1544   (setq mode (cond ((or (eq mode  -5) (eq mode 'han))  -5)
1545                    ((or (eq mode -40) (eq mode 'jis)) -40)
1546                    ((or (eq mode -41) (eq mode 'asc)) -41)
1547                    (t (wnn-arg-type-error wnn-set-symbol-mode))))
1548   (wnn-envspec-set-conv-param-symbol wnn-current-envspec mode))
1549
1550 (defun wnn-set-yuragi-mode (flag)
1551   (wnn-wnn6-env-func wnn-set-yuragi-mode)
1552   (wnn-boolean-param-check wnn-set-yuragi-mode flag)
1553   (wnn-envspec-set-conv-param-yuragi wnn-current-envspec flag))
1554
1555 (defun wnn-set-rendaku-mode (flag)
1556   (wnn-wnn6-env-func wnn-set-rendaku-mode)
1557   (wnn-boolean-param-check wnn-set-rendaku-mode flag)
1558   (wnn-envspec-set-conv-param-rendaku wnn-current-envspec flag))
1559 \f
1560 (defun wnn-renbunsetsu-conversion (env yomi hinshi fuzokugo v context)
1561   (let ((result
1562          (cond
1563           ((wnnenv-get-tankan env)
1564            (wnnrpc-tanbunsetsu-conversion env yomi hinshi fuzokugo v))
1565           ((wnnenv-is-wnn6 env)
1566            (wnnrpc-fi-renbunsetsu-conversion env yomi hinshi fuzokugo v
1567                                              context))
1568           (t
1569            (wnnrpc-renbunsetsu-conversion env yomi hinshi fuzokugo v)))))
1570     (prog1
1571         result
1572       (if wnn-one-level-conversion
1573           (while (consp result)
1574             (wnn-bunsetsu-set-dai-continue (car result) nil)
1575             (setq result (cdr result)))))))
1576
1577 (defun wnn-tanbunsetsu-conversion (env yomi hinshi fuzokugo v major)
1578   (if (or (null major)
1579           wnn-one-level-conversion
1580           (wnnenv-get-tankan env))
1581       (wnnrpc-tanbunsetsu-conversion env yomi hinshi fuzokugo v)
1582     (wnnrpc-daibunsetsu-conversion env yomi hinshi fuzokugo v)))
1583
1584 (defun wnn-get-bunsetsu-candidates (env yomi hinshi fuzokugo v major)
1585   (cond
1586    ((or wnn-one-level-conversion
1587         (wnnenv-get-tankan env))
1588     (let ((result (wnnrpc-get-bunsetsu-candidates env yomi hinshi fuzokugo v)))
1589       (prog1
1590           result
1591         (while (consp result)
1592           (wnn-bunsetsu-set-dai-continue (caar result) nil)
1593           (setq result (cdr result))))))
1594    ((null major)
1595     (wnnrpc-get-bunsetsu-candidates env yomi hinshi fuzokugo v))
1596    (t
1597     (wnnrpc-get-daibunsetsu-candidates env yomi hinshi fuzokugo v))))
1598
1599 (defsubst wnn-filename (p)
1600   (substitute-in-file-name
1601    (if (consp p) (concat wnn-usr-dic-dir "/" (car p)) p)))
1602
1603 (defsubst wnn-client-file-p (filename)
1604   (and (stringp filename)
1605        (= (aref filename 0) ?!)))
1606
1607 (defsubst wnn-client-filename (filename)
1608   (substitute-in-file-name (expand-file-name (substring filename 1) "~")))
1609
1610 (defun wnn-open-file (env filename)
1611   "Open the file FILENAME on the environment ENV.
1612 Return file ID.  NIL means NO-file.
1613 On failure, return negative error code."
1614   (and filename
1615        (if (wnn-client-file-p filename)
1616            (wnnrpc-file-send env (wnn-client-filename filename))
1617          (wnnrpc-file-read env (wnn-filename filename)))))
1618
1619 (defun wnn-create-directory (env path noquery)
1620   "Create directory to the path.  Retun non-NIL value on success."
1621   (if (wnn-client-file-p path)
1622       (let ((local-name (directory-file-name (file-name-directory
1623                                               (wnn-client-filename path)))))
1624         (cond
1625          ((file-directory-p local-name) t)
1626          ((or noquery
1627               (y-or-n-p (format (egg-get-message 'wnn-dir-missing)
1628                                 (file-name-directory path))))
1629           (make-directory local-name t)
1630           (if (file-directory-p local-name)
1631               (progn
1632                 (message (egg-get-message 'wnn-dir-created) path)
1633                 t)
1634             (message (egg-get-message 'wnn-dir-failed) path)
1635             nil))))
1636     (let ((name (directory-file-name (file-name-directory
1637                                       (wnn-filename path))))
1638           create-list)
1639       (setq path name)
1640       (while (and name (/= (wnnrpc-access env name 0) 0))
1641         (setq create-list (cons name create-list)
1642               name (file-name-directory name)
1643               name (and name (directory-file-name name))))
1644       (or (null create-list)
1645           (if (or noquery
1646                   (y-or-n-p (format (egg-get-message 'wnn-dir-missing) path)))
1647               (let ((result 0))
1648                 (while (and (>= result 0) create-list)
1649                   (setq result (wnnrpc-mkdir env (car create-list))
1650                         create-list (cdr create-list)))
1651                 (if (>= result 0)
1652                     (progn
1653                       (message (egg-get-message 'wnn-dir-created) path)
1654                       t)
1655                   (message (egg-get-message 'wnn-dir-failed) path)
1656                   nil)))))))
1657
1658 (defun wnn-file-remove (proc filename passwd)
1659   (let ((result (if (wnn-client-file-p filename)
1660                     (wnnrpc-file-remove-client
1661                      proc (wnn-client-filename filename) passwd)
1662                   (wnnrpc-file-remove proc (wnn-filename filename) passwd))))
1663     (or (= result 0)
1664         (progn
1665           (message (wnnrpc-get-error-message (- result)))
1666           nil))))
1667
1668 (defun wnn-open-dictionary (env fi name rw comment dpasswd fpasswd
1669                                 &optional noquery)
1670   (let ((dic-id (wnn-open-file env name)))
1671     (cond
1672      ((null dic-id)
1673       (message "Wnn: cannot omit dictionary name")
1674       nil)
1675      ((>= dic-id 0) dic-id)
1676      ((or (null rw) (/= dic-id (- (WNN-const NO_EXIST))))
1677       (message (egg-get-message 'wnn-dict-missing-1)
1678                name (wnnrpc-get-error-message (- dic-id)))
1679       nil)
1680      ((and (or noquery
1681                (y-or-n-p (format (egg-get-message 'wnn-dict-missing-2) name)))
1682            (wnn-create-directory env name noquery)
1683            (wnn-create-dictionary env name (wnnrpc-writable-dic-type env fi rw)
1684                                   comment dpasswd fpasswd))
1685       (message (egg-get-message 'wnn-dict-created) name)
1686       (setq dic-id (wnn-open-file env name))
1687       (if (>= dic-id 0)
1688           dic-id
1689         (message "%s" (wnnrpc-get-error-message (- dic-id)))
1690         nil)))))
1691
1692 (defun wnn-create-dictionary (env name type comment dpasswd fpasswd)
1693   "Create a dictionary file on the server or the client depending on name."
1694   (let ((result (if (wnn-client-file-p name)
1695                     (wnnrpc-dic-file-create-client
1696                      env (wnn-client-filename name) type
1697                      comment dpasswd fpasswd)
1698                   (wnnrpc-dic-file-create
1699                    env (wnn-filename name) type comment dpasswd fpasswd))))
1700     (or (= result 0)
1701         (progn
1702           (message (wnnrpc-get-error-message (- result)))
1703           nil))))
1704
1705 (defun wnn-open-frequency (env fi dic-id name rw comment passwd)
1706   (let ((freq-id (wnn-open-file env name)))
1707     (cond
1708      ((null freq-id) -1)
1709      ((>= freq-id 0) freq-id)
1710      ((or (null rw) (/= freq-id (- (WNN-const NO_EXIST))))
1711       (message (egg-get-message 'wnn-freq-missing-1)
1712                name (wnnrpc-get-error-message (- freq-id)))
1713       nil)
1714      ((and (y-or-n-p
1715             (format (egg-get-message 'wnn-freq-missing-2) name))
1716            (wnn-create-directory env name nil)
1717            (wnn-create-frequency env fi dic-id name comment passwd))
1718       (message (egg-get-message 'wnn-freq-created) name)
1719       (setq freq-id (wnn-open-file env name))
1720       (if (>= freq-id 0)
1721           freq-id
1722         (message "%s" (wnnrpc-get-error-message (- dic-id)))
1723         nil)))))
1724
1725 (defun wnn-create-frequency (env fi dic-id name comment passwd)
1726   "Create a frequency file on the server or the client depending on name."
1727   (let ((result (if (wnn-client-file-p name)
1728                     (wnnrpc-hindo-file-create-client
1729                      env fi dic-id (wnn-client-filename name) comment passwd)
1730                   (wnnrpc-hindo-file-create
1731                    env fi dic-id (wnn-filename name) comment passwd))))
1732     (or (= result 0)
1733         (progn
1734           (message (wnnrpc-get-error-message (- result)))
1735           nil))))
1736
1737 (defun wnn-set-dictionary (env fi dic-spec)
1738   ""
1739   (let ((dname (aref dic-spec 0))
1740         (fname (aref dic-spec 1))
1741         (prior (aref dic-spec 2))
1742         (drw   (aref dic-spec 3))
1743         (frw   (aref dic-spec 4))
1744         (dpass (aref dic-spec 5))
1745         (fpass (aref dic-spec 6))
1746         (rev   (aref dic-spec 7))
1747         did fid result)
1748     (cond
1749      ((numberp (setq dpass (wnnrpc-read-passwd-file dpass)))
1750       (message "%s" (wnnrpc-get-error-message (- dpass)))
1751       nil)
1752      ((numberp (setq fpass (if fname (wnnrpc-read-passwd-file fpass) "")))
1753       (message "%s" (wnnrpc-get-error-message (- fpass)))
1754       nil)
1755      ((and (setq did (wnn-open-dictionary env fi dname drw "" dpass fpass))
1756            (setq fid (wnn-open-frequency env fi did fname frw "" fpass)))
1757       (if fi
1758           (setq result (wnnrpc-set-fi-dictionary env did fid prior drw frw
1759                                                  dpass fpass))
1760         (setq drw (cond ((eq drw (WNN-const DIC_GROUP)) (WNN-const DIC_RW))
1761                         ((eq drw (WNN-const DIC_MERGE)) (WNN-const DIC_RDONLY))
1762                         (t drw))
1763               result (wnnrpc-set-dictionary env did fid prior drw frw
1764                                             dpass fpass rev)))
1765       (cond
1766        ((>= result 0) t)
1767        ((or (null frw) (/= result (- (WNN-const NO_MATCH))))
1768         (message "%s (%s): %s"
1769                  dname (if fname fname "")
1770                  (wnnrpc-get-error-message (- result)))
1771         nil)
1772        ((and (y-or-n-p (format (egg-get-message 'wnn-no-match) fname))
1773              (>= (wnnrpc-file-discard env fid) 0)
1774              (wnn-file-remove proc fname fpass)
1775              (wnn-create-frequency env fi did fname "" fpass))
1776         (message (egg-get-message 'wnn-re-create-freq) fname)
1777         (and (>= (setq fid (wnn-open-file env fname)) 0)
1778              (>= (wnnrpc-set-dictionary env
1779                                         did fid prior drw frw
1780                                         dpass fpass rev)
1781                  0))))))))
1782
1783 (defun wnn-temporary-dic-add (env rev)
1784   (let ((result (wnnrpc-temporary-dic-loaded env)))
1785     (if (= result 0)
1786         (wnnrpc-temporary-dic-add env rev)
1787       result)))
1788
1789 (defun wnn-set-autolearn-dictionary (env type dic-spec)
1790   (let ((dname (aref dic-spec 0))
1791         (prior (aref dic-spec 2))
1792         (drw   (aref dic-spec 3))
1793         (dpass (aref dic-spec 5))
1794         (rev   (aref dic-spec 7))
1795         (did (wnnrpc-get-autolearning-dic env type))
1796         result)
1797     (or (numberp drw)
1798         (setq drw (if drw 0 1)))
1799     (cond
1800      ((< did 0)
1801       (message "%s" (wnnrpc-get-error-message (- did)))
1802       nil)
1803      ((> did 0)
1804       (setq result (wnn-temporary-dic-add env rev))
1805       (if (>= result 0)
1806           drw
1807         (message "%s" (wnnrpc-get-error-message (- result)))
1808         nil))
1809      ((numberp (setq dpass (wnnrpc-read-passwd-file dpass)))
1810       (message "%s" (wnnrpc-get-error-message (- dpass)))
1811       nil)
1812      ((setq did (wnn-open-dictionary env nil dname t "" dpass "" t))
1813       (if (and (>= (setq did (wnnrpc-set-dictionary env did -1 prior drw drw
1814                                                     dpass "" rev))
1815                    0)
1816                (>= (setq did (wnnrpc-set-autolearning-dic env type did)) 0)
1817                (>= (setq did (wnn-temporary-dic-add env rev)) 0))
1818           drw
1819         (message "%s" (wnnrpc-get-error-message (- did)))
1820         nil)))))
1821
1822 (defun wnn-search-environment (backend)
1823   (let ((env-list wnn-environments)
1824         env)
1825     (while (and (null env) env-list)
1826       (setq env (and (eq (wnnenv-get-backend (car env-list)) backend)
1827                      (car env-list))
1828             env-list (cdr env-list)))
1829     env))
1830
1831 (defun wnn-v3-eggrc-defines ()
1832   (if (null (fboundp 'set-wnn-reverse))
1833       (progn
1834         (fset 'set-wnn-reverse
1835               (lambda (arg)
1836                 (wnn-define-environment arg
1837                                         (and (or (eq wnn-server-type 'cserver)
1838                                                  (eq wnn-server-type 'tserver))
1839                                              "PZ"))))
1840         (fset 'is-wnn6-server (lambda () wnn-wnn6-server))
1841         (fset 'set-wnn-fuzokugo 'wnn-set-fuzokugo)
1842         (fset 'add-wnn-dict 'wnn-add-dict)
1843         (fset 'set-wnn-param 'wnn-set-param)
1844         (fset 'add-wnn-fisys-dict 'wnn-add-fisys-dict)
1845         (fset 'add-wnn-fiusr-dict 'wnn-add-fiusr-dict)
1846         (fset 'add-wnn-notrans-dict 'wnn-add-notrans-dict)
1847         (fset 'add-wnn-bmodify-dict 'wnn-add-bmodify-dict)
1848         (fset 'set-last-is-first-mode 'wnn-set-last-is-first-mode)
1849         (fset 'set-complex-conv-mode 'wnn-set-complex-conv-mode)
1850         (fset 'set-okuri-flag 'wnn-set-okuri-flag)
1851         (fset 'set-prefix-learn-mode 'wnn-set-prefix-learn-mode)
1852         (fset 'set-suffix-learn-mode 'wnn-set-suffix-learn-mode)
1853         (fset 'set-common-learn-mode 'wnn-set-common-learn-mode)
1854         (fset 'set-yuragi-mode 'wnn-set-yuragi-mode)
1855         (fset 'set-freq-func-mode 'wnn-set-freq-func-mode)
1856         (fset 'set-numeric-mode 'wnn-set-numeric-mode)
1857         (fset 'set-alphabet-mode 'wnn-set-alphabet-mode)
1858         (fset 'set-symbol-mode 'wnn-set-symbol-mode)
1859         (setq wnn-v3-defined t))))
1860
1861 (defun wnn-get-environment (backend)
1862   "Return Wnn Environemt for BACKEND.  If none, create new
1863 environment."
1864   (let ((env (wnn-search-environment backend))
1865         proc error)
1866     (or env
1867         (unwind-protect
1868             (let* ((language (wnn-backend-get-language backend))
1869                    (server-info (wnn-server-get-info language))
1870                    (server-type (wnn-server-type server-info))
1871                    version specs)
1872               (setq proc (wnn-open server-info)
1873                     version (cdr proc)
1874                     proc (car proc)
1875                     wnn-envspec-list nil)
1876               (condition-case err
1877                   (let ((wnn-server-type server-type)
1878                         (wnn-wnn6-server (eq version 'wnn6)))
1879                     (if wnn-use-v3-eggrc
1880                         (wnn-v3-eggrc-defines))
1881                     (egg-load-startup-file 'wnn language))
1882                 (egg-error
1883                  (setq error err)
1884                  (signal (car error) (cdr error))))
1885               (setq specs wnn-envspec-list)
1886               (while specs
1887                 (wnn-create-environment proc server-type version (car specs))
1888                 (setq specs (cdr specs)))
1889               (setq env (wnn-search-environment backend)))
1890           (if (and proc (null env))
1891               (progn
1892                 (wnnrpc-close proc)
1893                 (if error
1894                     (signal (car error) (cdr error))
1895                   (egg-error 'wnn-fail-make-env))))))))
1896
1897 ;; Create a new environment in the conversion server, if necessary.
1898 (defun wnn-create-environment (proc server-type version spec)
1899   (let* ((save-inhibit-quit inhibit-quit)
1900          (inhibit-quit t)
1901          (name (wnn-make-env-name spec))
1902          (backend (wnn-envspec-backend spec))
1903          (tankan (wnn-envspec-tankan spec))
1904          (sticky (wnn-envspec-sticky spec))
1905          (parameter (wnn-envspec-param spec))
1906          (fzk (wnn-envspec-fuzokugo spec))
1907          (dic-list (wnn-envspec-dic-list spec))
1908          (fi-dic-list (wnn-envspec-fi-dic-list spec))
1909          (autolearn-dic-list (wnn-envspec-autolearn-dic-list spec))
1910          exist env-id env fid cvmask param mode type dic-spec)
1911     (condition-case err
1912         (progn
1913           (setq exist (wnnrpc-env-exist proc name)
1914                 env-id (wnnrpc-connect proc name))
1915           (if (< env-id 0)
1916               (egg-error "%s" (wnnrpc-get-error-message (- env-id))))
1917           (setq env (wnnenv-create proc env-id server-type version
1918                                    backend tankan name))
1919           (cond
1920            ((or wnn-force-set-environment (= exist 0))
1921             (let ((inhibit-quit save-inhibit-quit))
1922               (and fzk
1923                    (or (< (setq fid (wnn-open-file env fzk)) 0)
1924                        (< (setq fid (wnnrpc-set-fuzokugo-file env fid)) 0))
1925                    (message "%s" (wnnrpc-get-error-message (- fid))))
1926               (while fi-dic-list
1927                 (wnn-set-dictionary env t (car fi-dic-list))
1928                 (setq fi-dic-list (cdr fi-dic-list)))
1929               (while dic-list
1930                 (wnn-set-dictionary env nil (car dic-list))
1931                 (setq dic-list (cdr dic-list)))
1932               (while autolearn-dic-list
1933                 (setq type (caar autolearn-dic-list)
1934                       dic-spec (cdar autolearn-dic-list)
1935                       autolearn-dic-list (cdr autolearn-dic-list)
1936                       mode (wnn-set-autolearn-dictionary env type dic-spec))
1937                 (if mode
1938                     (if (eq type (WNN-const NOTRANS_LEARN))
1939                         (progn
1940                           (wnnenv-set-notrans env mode)
1941                           (wnn-envspec-set-conv-param-muhenkan spec mode))
1942                       (wnnenv-set-bmodify env mode)
1943                       (wnn-envspec-set-conv-param-bunsetsugiri spec mode))))
1944               (if parameter
1945                   (wnnrpc-set-conversion-parameter env parameter))
1946               (setq cvmask (wnn-envspec-conv-vmask spec)
1947                     param (wnn-envspec-conv-param spec))
1948               (if (/= cvmask 0)
1949                   (wnnrpc-set-conversion-env-param env cvmask param))))
1950            ((eq version 'wnn6)
1951             (wnnenv-set-bmodify env (wnn-get-autolearning-dic-mode
1952                                      env (WNN-const BMODIFY_LEARN)))
1953             (wnnenv-set-notrans env (wnn-get-autolearning-dic-mode
1954                                      env (WNN-const NOTRANS_LEARN)))))
1955           (cond
1956            ((eq (wnnenv-get-server-type env) 'jserver)
1957             (wnn-set-hinshi env 'noun "\e$BL>;l\e(B")
1958             (when (wnnenv-is-wnn6 env)
1959               (wnn-set-hinshi env 'settou "\e$B@\F,8l\e(B(\e$B$*\e(B)")
1960               (wnn-set-hinshi env 'rendaku "\e$BO"By\e(B")))
1961            ((eq (wnnenv-get-server-type env) 'cserver)
1962             (wnn-set-hinshi env 'noun "\e$AFUM(C{\e(B"))
1963            ((eq (wnnenv-get-server-type env) 'tserver)
1964             (wnn-set-hinshi env 'noun "\e$(G_[]WGX\e(B"))
1965            ((eq (wnnenv-get-server-type env) 'kserver)
1966             (wnn-set-hinshi env 'noun "\e$(CY#^r\e(B")))
1967           (if sticky
1968               (wnnrpc-make-env-sticky env)
1969             (wnnrpc-make-env-unsticky env))
1970           (setq wnn-environments (nconc wnn-environments (list env))))
1971       ((egg-error quit)
1972        (if (eq (car err) 'egg-error)
1973            (message "%s" (nth 1 err)))
1974        (if env
1975            (progn
1976              (wnnrpc-disconnect env)
1977              (setq wnn-environments (delq env wnn-environments))))
1978        (if (eq (car err) 'quit)
1979            (signal 'quit (cdr err)))))))
1980
1981 (defun wnn-make-env-name (spec)
1982   (let ((env-type (wnn-envspec-env-type spec)))
1983     (concat wnn-user-name (if env-type (symbol-name env-type) ""))))
1984
1985 (defun wnn-set-hinshi (env sym name)
1986   (let ((hinshi (wnnrpc-hinshi-number (wnnenv-get-proc env) name)))
1987     (if (>= hinshi 0)
1988         (wnnenv-set-hinshi env sym hinshi))))
1989
1990 (defsubst wnn-dicinfo-entry (info)       (aref info 0))
1991 (defsubst wnn-dicinfo-id (info freq)     (aref info (+ 1 freq)))
1992 (defsubst wnn-dicinfo-mode (info freq)   (aref info (+ 3 freq)))
1993 (defsubst wnn-dicinfo-enable (info)      (aref info 5))
1994 (defsubst wnn-dicinfo-nice (info)        (aref info 6))
1995 (defsubst wnn-dicinfo-reverse (info)     (aref info 7))
1996 (defsubst wnn-dicinfo-comment (info)     (aref info 8))
1997 (defsubst wnn-dicinfo-name (info freq)   (aref info (+ 9 freq)))
1998 (defsubst wnn-dicinfo-passwd (info freq) (aref info (+ 11 freq)))
1999 (defsubst wnn-dicinfo-type (info)        (aref info 13))
2000 (defsubst wnn-dicinfo-words (info)       (aref info 14))
2001 (defsubst wnn-dicinfo-local (info freq)  (aref info (+ 15 freq)))
2002
2003 (defun wnn-get-autolearning-dic-mode (env type)
2004   (let* ((dic (wnnrpc-get-autolearning-dic env type))
2005          (info (and (> dic 0) (wnnrpc-get-dictionary-info env (1- dic)))))
2006     (if (vectorp (car-safe info))
2007         (wnn-dicinfo-mode (car info) 0)
2008       (WNN-const DIC_RDONLY))))
2009
2010 (defun wnn-get-dictionary-list-with-environment (env)
2011   (if (wnnenv-is-wnn6 env)
2012       (wnnrpc-get-fi-dictionary-list-with-environment env
2013                                                       (WNN-const DIC_NO_TEMPS))
2014     (wnnrpc-get-dictionary-list-with-environment env)))
2015
2016 (defun wnn-save-dictionaries (env)
2017   (let ((dic-list (wnn-get-dictionary-list-with-environment env))
2018         (result 0) info freq fid name local-name)
2019     (while dic-list
2020       (setq info (car dic-list)
2021             dic-list (cdr dic-list)
2022             freq 0)
2023       (while (<= freq 1)
2024         (setq fid (wnn-dicinfo-id info freq)
2025               name (wnn-dicinfo-name info freq))
2026         (if (and (> fid 0) (= (wnn-dicinfo-mode info freq) 0))
2027             (cond
2028              ((= (wnn-dicinfo-local info freq) 1)
2029               (wnnrpc-write-file env fid name))
2030              ((setq local-name (wnnenv-get-client-file env name))
2031               (wnnrpc-file-receive env fid local-name))
2032              ((and (setq local-name (wnn-file-loaded-client env name fid))
2033                    (file-writable-p local-name))
2034               (wnnrpc-file-receive env fid local-name))))
2035         (setq freq (1+ freq))))))
2036
2037 (defun wnn-file-loaded-client (env name fid)
2038   (let ((len (length wnn-system-name))
2039         local-name)
2040     (and (> (length name) len)
2041          (equal (substring name 0 len) wnn-system-name)
2042          (prog1
2043              (wnn-client-file-p (substring name len))
2044            (setq local-name (wnn-client-filename (substring name len))))
2045          (= (wnnrpc-file-loaded-local (wnnenv-get-proc env) local-name t) fid)
2046          local-name)))
2047
2048 (defun wnn-word-inspection (bunsetsu)
2049   (let ((env (wnn-bunsetsu-get-env bunsetsu))
2050         (converted (wnn-get-bunsetsu-converted bunsetsu))
2051         (yomi (wnn-bunsetsu-get-yomi bunsetsu))
2052         (fuzokugo (wnn-bunsetsu-get-fuzokugo bunsetsu))
2053         (hinshi-no (wnn-bunsetsu-get-hinshi bunsetsu))
2054         (dic-no (wnn-bunsetsu-get-dic-no bunsetsu))
2055         (entry (wnn-bunsetsu-get-entry bunsetsu))
2056         (now (wnn-bunsetsu-get-right-now bunsetsu))
2057         (freq (wnn-bunsetsu-get-freq bunsetsu))
2058         (evaluation (wnn-bunsetsu-get-evaluation bunsetsu))
2059         (evaluation-dai (or (wnn-bunsetsu-get-dai-evaluation bunsetsu) "---"))
2060         (kangovect (wnn-bunsetsu-get-kangovect bunsetsu))
2061         hinsi dic)
2062     (setq hinshi (wnnrpc-hinshi-name (wnnenv-get-proc env) hinshi-no))
2063     (setq dic (if (>= dic-no 0)
2064                   (wnn-dict-name (car (wnnrpc-get-dictionary-info env dic-no)))
2065                 (egg-get-message 'wnn-pseud-bunsetsu)))
2066     (message "%s %s+%s(%s %s:%s Freq:%s%s) S:%s D:%s V:%s "
2067              converted yomi fuzokugo hinshi dic entry
2068              (if (= now 1) "*" " ") freq evaluation evaluation-dai kangovect)))
2069 \f
2070 ;;; not implemented yet (NIY)
2071 (defun wnn-delete-dictionary ()
2072   (dj-delete-dic XXX))
2073
2074 ;;; NIY, might never be implemented
2075 (defun wnn-server-inspect ())
2076
2077 ;;; NIY
2078 (defun wnn-get-conversion-parameter ()
2079   (js-get-parameter))
2080
2081 ;;; Dictionary management (word registration) is not implemented yet.
2082
2083 (defun wnn-find-dictionary-by-id (id dic-list)
2084   (catch 'return
2085     (while dic-list
2086       (let ((dic (car dic-list)))
2087         (if (= (wnndic-get-id dic) id)
2088             (throw 'return dic)
2089           (setq dic-list (cdr dic-list)))))))
2090
2091 (defun wnn-dict-name (dic-info)
2092   (let ((comment (wnndic-get-comment dic-info))
2093         (name (wnndic-get-dictname dic-info)))
2094     (cond ((null (string= comment "")) comment)
2095           ((wnn-client-file-p name) name)
2096           (t (file-name-nondirectory name)))))
2097
2098 (defun wnn-list-writable-dictionaries-byname (env)
2099   (let ((dic-list (wnn-get-dictionary-list-with-environment env))
2100         (w-id-list (wnnrpc-get-writable-dictionary-id-list env)))
2101     (cond ((numberp w-id-list)
2102            (egg-error "%s" (wnnrpc-get-error-message (- w-id-list))))
2103           ((null w-id-list)
2104            (egg-error 'wnn-no-writable-d))
2105           (t
2106            (delq nil
2107                  (mapcar (lambda (id)
2108                            (let ((dic (wnn-find-dictionary-by-id id dic-list)))
2109                              (and dic (cons (wnn-dict-name dic) dic))))
2110                          w-id-list))))))
2111
2112 (defun wnn-word-registration (backend kanji yomi)
2113   (let (env dic dic-id hinshi result)
2114     (if (or (null (eq (egg-get-language 0 kanji)
2115                       (wnn-backend-get-converted-language backend)))
2116             (next-single-property-change 0 'egg-lang kanji)
2117             (null (eq (egg-get-language 0 yomi)
2118                       (wnn-backend-get-source-language backend)))
2119             (next-single-property-change 0 'egg-lang yomi))
2120         (egg-error "word registration: invalid character")
2121       (setq env (wnn-get-environment backend)
2122             dic (wnn-dictionary-select env)
2123             dic-id (wnndic-get-id dic)
2124             hinshi (wnn-hinshi-select env dic-id)
2125             result (wnnrpc-hinshi-number (wnnenv-get-proc env) hinshi))
2126       (or (< result 0)
2127           (setq result (wnnrpc-add-word env dic-id yomi kanji "" result 0)))
2128       (if (>= result 0)
2129           (list hinshi (wnn-dict-name dic))
2130         (egg-error (wnnrpc-get-error-message (- result)))))))
2131
2132 (defun wnn-dictionary-select (env)
2133   (menudiag-select (list 'menu
2134                          (egg-get-message 'wnn-register-1)
2135                          (wnn-list-writable-dictionaries-byname env))))
2136
2137 (defun wnn-hinshi-select (env dic-id)
2138   (menudiag-select (wnn-make-hinshi-menu
2139                     env dic-id "/"
2140                     (egg-get-message 'wnn-register-2))))
2141
2142 (defun wnn-make-hinshi-menu (env dic-id hinshi prompt)
2143   (let ((hinshi-list (wnnrpc-get-hinshi-list env dic-id hinshi)))
2144     (if (numberp hinshi-list)
2145         (egg-error "%s" (wnnrpc-get-error-message (- hinshi-list)))
2146       (list 'menu
2147             (format (if (equal hinshi "/") "%s:" "%s[%s]:")
2148                     prompt
2149                     (substring hinshi 0 (1- (length hinshi))))
2150             (mapcar (lambda (h)
2151                       (if (= (aref h (1- (length h))) ?/)
2152                           (cons h (wnn-make-hinshi-menu env dic-id h prompt))
2153                         h))
2154                     hinshi-list)))))
2155
2156 ;;; setup
2157
2158 (load "egg/wnnrpc")
2159 (run-hooks 'wnn-load-hook)
2160
2161 ;;;###autoload
2162 (defun egg-activate-wnn (&rest arg)
2163   "Activate Wnn backend of Tamago 4."
2164   (apply 'egg-mode (append arg wnn-backend-alist)))
2165
2166 ;;; egg/wnn.el ends here.