X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=egg%2Fwnn.el;h=acd5bc6f623d966194def9195d65209ff806d7ad;hb=ccc2c3cf2a993db1166638b047bff13ee5100336;hp=6474eb9330d16dc13dc73acf98d7269296a338fb;hpb=ed251433bb11d5790aa07002880de16b8fedf59c;p=elisp%2Fegg.git diff --git a/egg/wnn.el b/egg/wnn.el index 6474eb9..acd5bc6 100644 --- a/egg/wnn.el +++ b/egg/wnn.el @@ -1,21 +1,23 @@ ;;; egg/wnn.el --- WNN Support (high level interface) in Egg ;;; Input Method Architecture -;; Copyright (C) 1997 Mule Project, Powered by Electrotechnical +;; Copyright (C) 1997, 1998 Mule Project, Powered by Electrotechnical ;; Laboratory, JAPAN. ;; Project Leader: Satoru Tomura ;; Author: NIIBE Yutaka +;; KATAYAMA Yoshio ; Korean, Chinese support. +;; ;; Maintainer: NIIBE Yutaka -;; This file will be part of GNU Emacs (in future). +;; This file is part of EGG. -;; GNU Emacs is free software; you can redistribute it and/or modify +;; EGG is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. -;; GNU Emacs is distributed in the hope that it will be useful, +;; EGG is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. @@ -29,6 +31,14 @@ ;;; Code: +(require 'egg-edep) + +(defgroup wnn nil + "Wnn interface for Tamagotchy" + :group 'egg) + +(defconst wnn-support-languages '(Japanese Chinese-GB Chinese-CNS Korean)) + (eval-when-compile (defmacro WNN-const (c) (cond ((eq c 'BUN_SENTOU) -1) @@ -52,14 +62,16 @@ wnn-decide-candidate wnn-change-bunsetsu-length wnn-end-conversion + wnn-start-reverse-conversion wnn-fini ]) -;; ::= [ ] +;; ::= [ +;; ] -(defsubst wnnenv-create (proc env-id) - (vector proc env-id nil)) +(defsubst wnnenv-create (proc env-id server-type dic-set rev-flag) + (vector proc env-id server-type dic-set rev-flag nil)) (defsubst wnnenv-get-proc (env) (aref env 0)) @@ -67,10 +79,19 @@ (defsubst wnnenv-get-env-id (env) (aref env 1)) -(defsubst wnnenv-get-daibunsetsu-info (env) +(defsubst wnnenv-get-server-type (env) (aref env 2)) + +(defsubst wnnenv-get-dictionary-set (env) + (aref env 3)) + +(defsubst wnnenv-get-reverse-flag (env) + (aref env 4)) + +(defsubst wnnenv-get-daibunsetsu-info (env) + (aref env 5)) (defsubst wnnenv-set-daibunsetsu-info (env d) - (aset env 2 d)) + (aset env 5 d)) ;; ::= [ ;; @@ -132,24 +153,70 @@ (defsubst wnn-bunsetsu-set-zenkouho-pos (bunsetsu zp) (aset bunsetsu 18 zp)) -(defvar wnn-server "localhost" - "Hostname of wnn server") - -(defvar wnn-environment nil +(defvar wnn-environments nil "Environment for WNN kana-kanji conversion") +(defcustom wnn-jserver nil "jserver host list" :group 'wnn :type 'string) +(defcustom wnn-cserver nil "cserver host list" :group 'wnn :type 'string) +(defcustom wnn-tserver nil "tserver host list" :group 'wnn :type 'string) +(defcustom wnn-kserver nil "kserver host list" :group 'wnn :type 'string) + +(defcustom wnn-jport 22273 "jserver port number" :group 'wnn :type 'integer) +(defcustom wnn-cport 22289 "cserver port number" :group 'wnn :type 'integer) +(defcustom wnn-tport 22321 "tserver port number" :group 'wnn :type 'integer) +(defcustom wnn-kport 22305 "kserver port number" :group 'wnn :type 'integer) + +;; The port number should be initialized from $WNNLIB/serverdefs by wnn-init +(defconst wnn-server-info-list + ;; language locale server port stream coding-system hostname + '((Japanese "ja_JP" jserver wnn-jport "Wnn" fixed-euc-jp wnn-jserver) + (Chinese-GB "zh_CN" cserver wnn-cport "cWnn" fixed-euc-cn wnn-cserver) + (Chinese-CNS "zh_TW" tserver wnn-tport "tWnn" fixed-euc-tw wnn-tserver) + (Korean "ko_KR" kserver wnn-kport "kWnn" fixed-euc-kr wnn-kserver))) + +(defsubst wnn-server-get-info (lang) + (assq (or lang its-current-language) wnn-server-info-list)) + +(defsubst wnn-server-language (info) + (car info)) + +(defsubst wnn-server-locale (info) + (nth 1 info)) + +(defsubst wnn-server-type (info) + (nth 2 info)) + +(defsubst wnn-server-port (info) + (symbol-value (nth 3 info))) + +(defsubst wnn-server-stream-name (info) + (nth 4 info)) + +(defsubst wnn-server-buffer-name (info) + (concat " *" (wnn-server-stream-name info) "*")) + +(defsubst wnn-server-coding-system (info) + (nth 5 info)) + +(defsubst wnn-server-hostname (info) + (symbol-value (nth 6 info))) + (defun wnn-init () ) -(defun wnn-start-conversion (yomi) +(defun wnn-start-conversion (yomi &optional language dic-set reverse) "Convert YOMI string to kanji, and enter conversion mode. Return the list of bunsetsu." - (let* ((env (wnn-get-environment wnn-dictionary-specification)) + (let* ((server-info (wnn-server-get-info language)) + (env (wnn-get-environment server-info dic-set reverse)) (result (wnnrpc-renbunsetsu-conversion env yomi (WNN-const BUN_SENTOU) ""))) (wnnenv-set-daibunsetsu-info env (car result)) (cdr result))) +(defun wnn-start-reverse-conversion (yomi &optional language dic-set) + (wnn-start-conversion yomi language dic-set t)) + (defun wnn-get-bunsetsu-converted (bunsetsu) (concat (wnn-bunsetsu-get-converted bunsetsu) (wnn-bunsetsu-get-fuzokugo bunsetsu))) @@ -171,7 +238,7 @@ Return the list of bunsetsu." (let ((hash-table (make-vector 31 0)) ; XXX why 31? (l bunsetsu-list) (i 0) - n sym0 result p b sym) + (n 0) sym0 result p b sym) (setq sym0 (intern (wnn-get-bunsetsu-converted bunsetsu) hash-table)) (while l (setq b (car l) @@ -255,10 +322,8 @@ Return the list of bunsetsu." prev-fuzokugo "") (setq prev-hinshi (wnn-bunsetsu-get-hinshi b0) prev-fuzokugo (wnn-bunsetsu-get-fuzokugo b0))) - (save-match-data - (string-match (concat "^\\(" (make-string len ?.) "\\)\\(.*$\\)") yomi) - (setq yomi1 (match-string 1 yomi)) - (setq yomi2 (match-string 2 yomi))) + (setq yomi1 (substring yomi 0 len) + yomi2 (substring yomi len)) (setq bunsetsu1 (car (wnnrpc-tanbunsetsu-conversion env yomi1 prev-hinshi prev-fuzokugo))) @@ -269,15 +334,14 @@ Return the list of bunsetsu." (list b1 b2) (list b1)))) (if (< 0 (length yomi2)) - ;; RENBUNSETSU? XXX (setq bunsetsu2 - (car (wnnrpc-tanbunsetsu-conversion + (cdr (wnnrpc-renbunsetsu-conversion env yomi2 (wnn-bunsetsu-get-hinshi bunsetsu1) (wnn-bunsetsu-get-fuzokugo bunsetsu1)))) (setq bunsetsu2 nil)) (if bunsetsu2 - (list bunsetsu1 bunsetsu2) + (append (list bunsetsu1) bunsetsu2) (list bunsetsu1)))) @@ -285,91 +349,301 @@ Return the list of bunsetsu." (concat (wnn-bunsetsu-get-yomi bunsetsu) (wnn-bunsetsu-get-fuzokugo bunsetsu))) -(defun wnn-end-conversion (bunsetsu-info-list) - (let ((env (wnn-bunsetsu-get-env (car bunsetsu-info-list)))) - (wnn-update-frequency env bunsetsu-info-list) - (wnnenv-set-daibunsetsu-info env nil))) +(defun wnn-end-conversion (bunsetsu-info-list abort) + (if abort + () + (let ((env (wnn-bunsetsu-get-env (car bunsetsu-info-list)))) + (wnn-update-frequency env bunsetsu-info-list) + (wnnenv-set-daibunsetsu-info env nil)))) (defvar wnn-sticky-environment-flag nil "*Flag which specifies sticky environment.") -(defun wnn-fini () ; XXX - (if (null wnn-environment) - nil - (condition-case nil +(defmacro wnn-for-each-environment (lang env &rest body) + `(let* ((server-info (wnn-server-get-info ,lang)) + (server-type (wnn-server-type server-info)) + (env-list wnn-environments)) + (if server-type + (while env-list + (let ((,env (car env-list))) + ,@body) + (setq env-list (cdr env-list)))))) + +(defun wnn-fini (lang &optional save-only) + (let* ((server-info (wnn-server-get-info lang)) + (server-type (wnn-server-type server-info)) + (l wnn-environments) + new-env-list del-list env proc) + (if server-type (progn - (if wnn-sticky-environment-flag - (wnnrpc-make-env-sticky wnn-environment) - (wnnrpc-make-env-unsticky wnn-environment)) - (wnnrpc-disconnect wnn-environment)) - (error nil)) - (let ((proc (wnnenv-get-proc wnn-environment))) - (if (eq (process-status proc) 'open) - (progn - (wnnrpc-close proc) - (kill-buffer (process-buffer proc)) - (delete-process proc)))) - (setq wnn-environment nil))) + (message "%s の頻度情報・辞書情報を退避しています" + (wnn-server-stream-name server-info)) + (while l + (setq env (car l)) + (if (eq (wnnenv-get-server-type env) server-type) + (condition-case nil + (progn + (wnn-save-dictionaries env) + (if (null save-only) + (progn + (setq del-list (nconc del-list (list env))) + (if wnn-sticky-environment-flag + (wnnrpc-make-env-sticky env) + (wnnrpc-make-env-unsticky env)) + (wnnrpc-disconnect env)))) + (error nil)) + (setq new-env-list (nconc new-env-list (list env)))) + (setq l (cdr l))) + (message "%s の頻度情報・辞書情報を退避しました" + (wnn-server-stream-name server-info)) + (if (null save-only) + (progn + (setq proc (and del-list (wnnenv-get-proc (car del-list)))) + (if (and proc (eq (process-status proc) 'open)) + (progn + (wnnrpc-close proc) + (kill-buffer (process-buffer proc)))) + (setq wnn-environments new-env-list))))))) + +(defun wnn-close (lang) + (interactive (list (wnn-read-active-lang))) + (or (listp lang) + (setq lang (list lang))) + (while lang + (wnn-fini (car lang)) + (setq lang (cdr lang)))) + +(defun wnn-dictionary-save (lang) + (interactive (list (wnn-read-active-lang))) + (or (listp lang) + (setq lang (list lang))) + (while lang + (wnn-fini (car lang) t) + (setq lang (cdr lang)))) + +(defun wnn-read-active-lang () + (let ((completion-ignore-case t) + (env wnn-environments) + langs server server-list) + (while env + (setq server (wnnenv-get-server-type (car env)) + env (cdr env)) + (if (null (member server server-list)) + (setq server-list (cons server server-list)))) + (setq langs (delete nil + (mapcar (lambda (info) + (if (memq (wnn-server-type info) server-list) + (wnn-server-language info))) + wnn-server-info-list))) + (if (<= (length langs) 1) + langs + (setq langs (cons (cons "All" langs) + (mapcar (lambda (lang) (cons (symbol-name lang) lang)) + langs))) + (cdr (assoc (completing-read "language? " langs nil t) langs))))) -;; XXX should be array (index: server) of {C,J,K}server -(defconst wnn-jserver-port 22273) ;; (defun wnn-comm-sentinel (proc reason) ; assume it is close - (kill-buffer (process-buffer proc)) - (delete-process proc) - (setq wnn-environment nil) - (message "WNN: connection closed")) + ; tamago-971009 version + (let ((l wnn-environments) + env l1) + (kill-buffer (process-buffer proc)) + ;; delete env from the list. + (while l + (setq env (car l)) + (if (eq proc (wnnenv-get-proc env)) + (progn + (if l1 + (setcdr l1 (cdr l)) + (setq wnn-environments (cdr l))) + (setq l (cdr l))) + (setq l1 l + l (cdr l)))))) ;; -(defun wnn-open (hostname language) +(defvar wnn-open-message) + +(defun wnn-open (server-info) "Establish the connection to WNN server. Return process object." - ;; Specifying language (jserver/cserver/kserver), - ;; open the session to WNN server, - (let ((buf (generate-new-buffer " *WNN*")) - proc result) - (condition-case result - (setq proc (open-network-stream "WNN" buf hostname wnn-jserver-port)) - (error (progn - (kill-buffer buf) - (signal (car result) (cdr result))))) - (process-kill-without-query proc) - (set-process-coding-system proc 'no-conversion 'no-conversion) - (set-process-sentinel proc 'wnn-comm-sentinel) - (set-marker-insertion-type (process-mark proc) t) + ;; Open the session to WNN server, + (let ((buf (generate-new-buffer (wnn-server-buffer-name server-info))) + (server-type (wnn-server-type server-info)) + (hostname-list (wnn-server-hostname server-info)) + (msg-form "WNN: connecting to %S at %s...") + hostname proc result msg) (save-excursion (set-buffer buf) (erase-buffer) (buffer-disable-undo) - (setq enable-multibyte-characters nil)) - (setq result (wnnrpc-open proc (system-name) (user-login-name))) - (if (< result 0) - (let ((msg (wnnrpc-get-error-message (- result)))) - (delete-process proc) - (kill-buffer buf) - (error "Can't open WNN session (%s %s): %s" hostname language msg)) - proc))) - -(defvar wnn-dictionary-specification - '([2 10 2 45 100 200 5 1 40 0 -100 200 -100 200 80 200 200] - "pubdic/full.fzk" - ["pubdic/kihon.dic" ("kihon.h") 5 nil t] - ["pubdic/setsuji.dic" ("setsuji.h") 5 nil t] - ["pubdic/koyuu.dic" ("koyuu.h") 1 nil t] - ["pubdic/chimei.dic" ("chimei.h") 1 nil t] - ["pubdic/jinmei.dic" ("jinmei.h") 1 nil t] - ["pubdic/special.dic" ("special.h") 5 nil t] - ["pubdic/computer.dic" ("computer.h") 5 nil t] - ["pubdic/symbol.dic" ("symbol.h") 1 nil t] - ["pubdic/tankan.dic" ("tankan.h") 1 nil t] - ["pubdic/bio.dic" ("bio.h") 1 nil t] - ["gerodic/g-jinmei.dic" ("g-jinmei.h") 1 nil t] - ["wnncons/tankan2.dic" ("tankan2.h") 1 nil t] - ["wnncons/tankan3.dic" ("tankan3.h") 1 nil t] - [("ud") nil 5 t t]) - "") - -(defvar wnn-usr-dic-dir (concat "usr/" (user-login-name)) - "*Directory of user dictionary for Wnn.") + (setq egg-fixed-euc (wnn-server-coding-system server-info)) + (set-buffer-multibyte nil)) + (cond + ((null hostname-list) + (setq hostname-list '("localhost"))) + ((null (listp hostname-list)) + (setq hostname-list (list hostname-list)))) + (while (and hostname-list (null proc)) + (setq hostname (car hostname-list) + hostname-list (cdr hostname-list)) + (message msg-form server-type hostname) + (condition-case result + (setq proc (open-network-stream (wnn-server-stream-name server-info) + buf + hostname + (wnn-server-port server-info))) + (error nil)) + (if proc + (progn + (process-kill-without-query proc) + (set-process-coding-system proc 'no-conversion 'no-conversion) + (set-process-sentinel proc 'wnn-comm-sentinel) + (set-marker-insertion-type (process-mark proc) t) + (setq result (wnnrpc-open proc + (if (equal hostname "localhost") + "unix" + (system-name)) + (user-login-name))) + (if (< result 0) + (progn + (delete-process proc) + (setq proc nil + msg (format "Can't open WNN session (%s %S): %s" + hostname + (wnn-server-type server-info) + msg))))))) + (if proc + (progn + (setq wnn-open-message (format (concat msg-form "done") + server-type + hostname)) + proc) + (kill-buffer buf) + (error "%s" (or msg (format "no %S available" server-type)))))) + +(defvar wnn-dictionary-specification-list + '((jserver + (nil nil "" + [2 10 2 45 100 200 5 1 40 -100 200 -100 200 80 200 200 200] + "pubdic/full.fzk" + ["pubdic/kihon.dic" ("kihon.h") 5 nil t] + ["pubdic/setsuji.dic" ("setsuji.h") 5 nil t] + ["pubdic/koyuu.dic" ("koyuu.h") 1 nil t] + ["pubdic/chimei.dic" ("chimei.h") 1 nil t] + ["pubdic/jinmei.dic" ("jinmei.h") 1 nil t] + ["pubdic/special.dic" ("special.h") 5 nil t] + ["pubdic/computer.dic" ("computer.h") 5 nil t] + ["pubdic/symbol.dic" ("symbol.h") 1 nil t] + ["pubdic/tankan.dic" nil 1 nil nil] + ["pubdic/bio.dic" ("bio.h") 1 nil t] + ["gerodic/g-jinmei.dic" ("g-jinmei.h") 1 nil t] + ["wnncons/tankan2.dic" nil 1 nil nil] + ["wnncons/tankan3.dic" nil 1 nil nil] + [("ud") nil 5 t t]) + (nil t "R" + [2 10 2 45 1 80 5 1 50 -20 400 -10 100 -100 200 0 200] + "pubdic/full.fzk" + ["pubdic/kihon.dic" ("kihon.h") 5 nil t] + ["pubdic/setsuji.dic" ("setsuji.h") 5 nil t] + ["pubdic/koyuu.dic" ("koyuu.h") 1 nil t] + ["pubdic/chimei.dic" ("chimei.h") 1 nil t] + ["pubdic/jinmei.dic" ("jinmei.h") 1 nil t] + ["pubdic/special.dic" ("special.h") 5 nil t] + ["pubdic/computer.dic" ("computer.h") 5 nil t] + ["pubdic/symbol.dic" ("symbol.h") 1 nil t] + ["pubdic/tankan.dic" nil 1 nil nil] + ["pubdic/bio.dic" ("bio.h") 1 nil t] + ["gerodic/g-jinmei.dic" ("g-jinmei.h") 1 nil t] + ["wnncons/tankan2.dic" nil 1 nil nil] + ["wnncons/tankan3.dic" nil 1 nil nil] + [("ud") nil 5 t t])) + (cserver + (Q nil "Q" + nil + "sys/full.con" + ["sys/QianMa.dic" nil 1 nil nil]) + (W nil "W" + nil + "sys/full.con" + ["sys/WuBi.dic" nil 1 nil nil]) + (nil nil "PZ" + [1 5 2 750 10 80 10 5 1000 50 0 -200 0 0 0 16 0] + "sys/full.con" + ["sys/level_1.dic" ("level_1.h") 4 nil t] + ["sys/level_2.dic" ("level_2.h") 1 nil t] + ["sys/basic.dic" ("basic.h") 7 nil t] + ["sys/computer.dic" ("computer.h") 4 nil t] + ["sys/cwnn.dic" ("cwnn.h") 4 nil t] + [("ud") nil 5 t t]) + (Q t "QR" + nil + "sys/full.conR" + ["sys/QianMa.dic" nil 1 nil nil]) + (W t "WR" + nil + "sys/full.conR" + ["sys/WuBi.dic" nil 1 nil nil]) + (nil t "PZR" + [1 5 2 750 10 80 10 5 1000 50 0 -200 0 0 0 16 0] + "sys/full.conR" + ["sys/level_1.dic" ("level_1.h") 4 nil t] + ["sys/level_2.dic" ("level_2.h") 1 nil t] + ["sys/basic.dic" ("basic.h") 7 nil t] + ["sys/computer.dic" ("computer.h") 4 nil t] + ["sys/cwnn.dic" ("cwnn.h") 4 nil t] + [("ud") nil 5 t t])) + (tserver + (nil nil "" + [1 5 2 750 10 80 10 5 1000 50 0 -200 0 0 0 16 0] + "sys/full.con" + ["sys/cns_ch.dic" ("cns_ch.h") 4 nil t] + ["sys/cns_wd.dic" ("cns_wd.h") 1 nil t] + [("ud") nil 5 t t]) + (nil t "R" + [1 5 2 750 10 80 10 5 1000 50 0 -200 0 0 0 16 0] + "sys/full.conR" + ["sys/cns_ch.dic" ("cns_ch.h") 4 nil t] + ["sys/cns_wd.dic" ("cns_wd.h") 1 nil t] + [("ud") nil 5 t t])) + (kserver + (nil nil "" + [2 5 2 45 200 80 5 1 40 0 400 -100 400 80 200 2 200] + "sys/full.fzk" + ["sys/hword.dic" ("hword.h") 5 nil t] + ["sys/single.dic" ("single.h") 1 nil t] + [("ud") nil 2 t t]) + (nil t "R" + [2 10 2 45 1 80 5 1 50 -20 400 -10 100 -100 200 0 200] + "sys/full.fzk" + ["sys/hword.dic" ("hword.h") 5 nil t] + ["sys/single.dic" ("single.h") 1 nil t] + [("ud") nil 2 t t])))) + +(defsubst wnn-get-dic-spec (server) + (cdr (assoc server wnn-dictionary-specification-list))) + +(defsubst wnn-dic-spec-dic-set (spec) + (nth 0 spec)) + +(defsubst wnn-dic-spec-reverse (spec) + (nth 1 spec)) + +(defsubst wnn-dic-spec-name (spec) + (nth 2 spec)) + +(defsubst wnn-dic-spec-param (spec) + (nth 3 spec)) + +(defsubst wnn-dic-spec-fuzokugo (spec) + (nth 4 spec)) + +(defsubst wnn-dic-spec-dic-list (spec) + (nthcdr 5 spec)) + + +(defcustom wnn-usr-dic-dir (concat "usr/" (user-login-name)) + "*Directory of user dictionary for Wnn." + :group 'wnn + :type 'string) (defun wnn-filename (p) "" @@ -512,48 +786,83 @@ On failure, return negate-encoded error code." (wnnrpc-open-file proc env-id freqname)) ; XXX: error? -1)) -(defun wnn-get-environment (dic-spec) - "Return WNN Environemt. If none, create new environment. -Take one argument DIC-SPEC for dictionary specification." - (if wnn-environment - wnn-environment - (let ((username (user-login-name)) - (proc (wnn-open wnn-server "ja_JP"))) - (setq wnn-environment - (wnn-create-environment proc username nil dic-spec))))) - -(defun wnn-create-environment (proc username reverse-flag spec) +(defun wnn-get-environment (server-info &optional dic-set reverse) + "Return WNN Environemt for the conversion server specified +by SERVER-INFO. If none, create new environment. Optional +argument DIC-SET specifies dictionary set. Optional argument +REVERSE specifies reverse conversion, if non nil." + (let ((server-type (wnn-server-type server-info)) + (env wnn-environments) + proc spec e s) + (setq reverse (null (null reverse))) + (if (catch 'found + (while env + (setq e (car env)) + (if (and (eq (wnnenv-get-server-type e) server-type) + (eq (wnnenv-get-dictionary-set e) dic-set) + (eq (wnnenv-get-reverse-flag e) reverse)) + (throw 'found t)) + (setq env (cdr env)))) + e + (setq proc (wnn-open server-info) + spec (wnn-get-dic-spec server-type)) + (while spec + (setq s (car spec) + e (wnn-create-environment proc server-type s) + wnn-environments (cons e wnn-environments)) + (if (and (eq (wnn-dic-spec-dic-set s) dic-set) + (eq (wnn-dic-spec-reverse s) reverse)) + (setq env e)) + (setq spec (cdr spec))) + (if (null env) + (error "WNN: environment for %S%s (%s) not found" + server-type + (if dic-set (format "(%S)" dic-set) "") + (if reverse 'reverse 'normal))) + (message "%s" wnn-open-message) + env))) + +(defun wnn-create-environment (proc server-type spec) "" ;; Create new data structure: something like wnn_buf ;; Process, Environment-ID and Daibunsetsu-info. - (let (env env-id parameters) - (setq env-id (wnnrpc-connect proc username)) + (let (env-id parameters filename fuzokugo-fid ret dic-set reverse) + (setq env-id (wnnrpc-connect proc (wnn-make-env-name spec))) (if (< env-id 0) - (let ((msg (wnnrpc-get-error-message (- env-id)))) - (error "Can't connect new WNN environment: %s" msg))) - (setq parameters (car spec)) - (setq spec (cdr spec)) - (let ((filename (wnn-filename (car spec))) - fuzokugo-fid ret) - (setq fuzokugo-fid (wnn-open-file proc env-id filename)) - (if (null fuzokugo-fid) - (setq fuzokugo-fid -1) - (if (< fuzokugo-fid 0) - (let ((msg (wnnrpc-get-error-message (- fuzokugo-fid)))) - (message "WNN: Can't open fuzokugo file (%s): %s" filename msg) - (setq fuzokugo-fid -1)))) - (setq ret (wnnrpc-set-fuzokugo-file proc env-id fuzokugo-fid)) - (if (< ret 0) - (let ((msg (wnnrpc-get-error-message (- ret)))) - (message "WNN: Error on setting fuzokugo (%s): %s" filename msg)))) - (setq spec (cdr spec)) + (error "Can't connect new WNN environment: %s" + (wnnrpc-get-error-message (- env-id)))) + (setq dic-set (wnn-dic-spec-dic-set spec) + reverse (wnn-dic-spec-reverse spec) + parameters (wnn-dic-spec-param spec) + filename (wnn-filename (wnn-dic-spec-fuzokugo spec)) + fuzokugo-fid (wnn-open-file proc env-id filename)) + (if (null fuzokugo-fid) + (setq fuzokugo-fid -1) + (if (< fuzokugo-fid 0) + (progn + (message "WNN: Can't open fuzokugo file (%s): %s" + filename + (wnnrpc-get-error-message (- fuzokugo-fid))) + (setq fuzokugo-fid -1)))) + (setq ret (wnnrpc-set-fuzokugo-file proc env-id fuzokugo-fid)) + (if (< ret 0) + (let ((msg (wnnrpc-get-error-message (- ret)))) + (message "WNN: Error on setting fuzokugo (%s): %s" filename msg))) + (setq spec (wnn-dic-spec-dic-list spec)) (while spec - (let ((dic-spec (car spec))) - (wnn-set-dictionary proc env-id reverse-flag dic-spec) - (setq spec (cdr spec)))) - (wnnrpc-set-conversion-parameters proc env-id parameters) - (setq env (wnnenv-create proc env-id)) - env)) + (wnn-set-dictionary proc env-id reverse (car spec)) + (setq spec (cdr spec))) + (if parameters + (wnnrpc-set-conversion-parameters proc env-id parameters)) + (wnnenv-create proc env-id server-type dic-set reverse))) + +(defvar wnn-user-name nil) + +(defun wnn-make-env-name (spec) + (or wnn-user-name + (setq wnn-user-name (getenv "WNNUSER")) + (setq wnn-user-name (user-login-name))) + (concat wnn-user-name (wnn-dic-spec-name spec))) (defun wnn-update-frequency (env bunsetsu-info-list) (let ((l bunsetsu-info-list)) @@ -589,14 +898,41 @@ Take one argument DIC-SPEC for dictionary specification." ;(defun wnn-set-conversion-mode () ; (jl-set-environment)) -(defun wnn-save-dictionaries () - (for-each-environment - js-dic-list - (while (< i count) - dic => id - js-file-write - hindo => id - js-file-write))) +(defsubst wnn-dicinfo-entry (info) (aref info 0)) +(defsubst wnn-dicinfo-id (info freq) (aref info (+ 1 freq))) +(defsubst wnn-dicinfo-mode (info freq) (aref info (+ 3 freq))) +(defsubst wnn-dicinfo-enable (info) (aref info 5)) +(defsubst wnn-dicinfo-nice (info) (aref info 6)) +(defsubst wnn-dicinfo-reverse (info) (aref info 7)) +(defsubst wnn-dicinfo-comment (info) (aref info 8)) +(defsubst wnn-dicinfo-name (info freq) (aref info (+ 9 freq))) +(defsubst wnn-dicinfo-passwd (info freq) (aref info (+ 11 freq))) +(defsubst wnn-dicinfo-type (info) (aref info 13)) +(defsubst wnn-dicinfo-words (info) (aref info 14)) +(defsubst wnn-dicinfo-local (info freq) (aref info (+ 15 freq))) + +(defun wnn-save-dictionaries (env) + (let ((dic-list (wnnrpc-get-dictionary-list-with-environment env)) + (result 0) info freq) + (while (and dic-list + (>= result 0)) + (setq info (car dic-list) + dic-list (cdr dic-list) + freq 0) + (while (<= freq 1) + (if (and (> (wnn-dicinfo-id info freq) 0) + (= (wnn-dicinfo-mode info freq) 0)) + (if (= (wnn-dicinfo-local info freq) 1) + (setq result (wnnrpc-write-file env + (wnn-dicinfo-id info freq) + (wnn-dicinfo-name info freq))) + (message "WNN: remote dictionary (%s) not supported yet" + (wnn-dicinfo-name info freq)) + (ding) + (sit-for 1))) + (if (< result 0) + (wnnrpc-disconnect env)) + (setq freq (1+ freq)))))) (defun wnn-version (proc) "Return version number string of WNN server." @@ -663,4 +999,17 @@ Take one argument DIC-SPEC for dictionary specification." (wnnrpc-add-word env dic-number yomi kanji comment hinshi-id initial-freq))) +;;; setup + +(require 'egg) +(load "egg/wnnrpc") + +;;;###autoload +(defun egg-activate-wnn (&rest arg) + "Activate Wnn backend of Tamagotchy." + (egg-set-support-languages wnn-support-languages) + (egg-set-conversion-backend wnn-conversion-backend + wnn-support-languages) + (apply 'egg-mode arg)) + ;;; egg/wnn.el ends here.