| 1 |
;;; skk-exserv.el --- SKK $B%5!<%P!<$N$?$a$N%W%m%0%i%`(B -*- coding: iso-2022-jp -*- |
| 2 |
|
| 3 |
;; Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, |
| 4 |
;; 1997, 1998, 1999, 2000 |
| 5 |
;; Masahiko Sato <[email protected]> |
| 6 |
;; NAKAJIMA Mikio <[email protected]> |
| 7 |
|
| 8 |
;; Author: Masahiko Sato <[email protected]> |
| 9 |
;; Maintainer: NAKAJIMA Mikio <[email protected]> |
| 10 |
;; Keywords: japanese |
| 11 |
|
| 12 |
;; This file is part of Daredevil SKK. |
| 13 |
|
| 14 |
;; This program is free software: you can redistribute it and/or |
| 15 |
;; modify it under the terms of the GNU General Public License as |
| 16 |
;; published by the Free Software Foundation, either version 3 of |
| 17 |
;; the License, or (at your option) any later version. |
| 18 |
|
| 19 |
;; This program is distributed in the hope that it will be |
| 20 |
;; useful, but WITHOUT ANY WARRANTY; without even the implied |
| 21 |
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 22 |
;; PURPOSE. See the GNU General Public License for more details. |
| 23 |
|
| 24 |
;; You should have received a copy of the GNU General Public License |
| 25 |
;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 |
|
| 27 |
;;; Commentary: |
| 28 |
|
| 29 |
;; This file needs eieio package (which provides CLOS like OO |
| 30 |
;; programming) that can be found at; |
| 31 |
|
| 32 |
;; ftp://ftp.ultranet.com/pub/zappo |
| 33 |
|
| 34 |
;;; Code: |
| 35 |
|
| 36 |
(eval-when-compile |
| 37 |
(require 'skk-macs) |
| 38 |
(require 'skk-vars) |
| 39 |
(require 'static)) |
| 40 |
(require 'eieio) |
| 41 |
(require 'skk-exsearch) |
| 42 |
|
| 43 |
(defclass network-search-engine (search-engine) |
| 44 |
((host :initarg :host |
| 45 |
:initform "localhost" |
| 46 |
:documentation "Host name.") |
| 47 |
(service :initarg :service |
| 48 |
:initform nil |
| 49 |
:documentation |
| 50 |
"Name of the service desired, or an integer specifying a port number to connect to.") |
| 51 |
(process-name :initarg :process-name |
| 52 |
:initform nil |
| 53 |
:documentation |
| 54 |
"Name of process. It is modified if necessary to make it unique.") |
| 55 |
(process initarg :process |
| 56 |
:initform nil |
| 57 |
:documentation "Process object that belongs to program.") |
| 58 |
(buffer :initarg :buffer |
| 59 |
:initform nil |
| 60 |
:documentation |
| 61 |
"The buffer (or buffer-name) to associate with the process.\ |
| 62 |
Process output goes at end of that buffer, unless you specify\ |
| 63 |
an output stream or filter function to handle the output.\ |
| 64 |
buffer may be also nil, meaning that this process is not associated\ |
| 65 |
with any buffer.")) |
| 66 |
"A class of Network search engine via TCP connection.") |
| 67 |
|
| 68 |
(defclass dbskkd-engine (network-search-engine) |
| 69 |
((service :initarg :service |
| 70 |
:initform "skkserv") |
| 71 |
(process-name :initarg :process-name :initform "dbskkd") |
| 72 |
(buffer :initarg :buffer |
| 73 |
:initform (lambda () (get-buffer-create " *dbskkd*"))) |
| 74 |
(found :initform 1 |
| 75 |
:documentation "A magic number that indicates the server found a candidate.") |
| 76 |
(not-found :initform 4 |
| 77 |
:documentation |
| 78 |
"A magic number that indicates the server did not find a candidates.") |
| 79 |
(coding-system :initarg :coding-system |
| 80 |
:initform (lambda () (cdr (assoc "euc" skk-coding-system-alist))))) |
| 81 |
"A class of dbskkd type server search engine via TCP connection. |
| 82 |
Output of this type is a line that contains a magic number and |
| 83 |
candidates that are delimited by slash.") |
| 84 |
|
| 85 |
(defvar dbskkd (make-instance dbskkd-engine) |
| 86 |
"*dbskkd server object.") |
| 87 |
|
| 88 |
(defvar skk-exserv-list (list dbskkd) |
| 89 |
"*Add other SKK server objects if you like.") |
| 90 |
|
| 91 |
(defun skk-open-server () |
| 92 |
;; return active server object. |
| 93 |
(while (and (car skk-exserv-list) |
| 94 |
(not (server-opened-p (car skk-exserv-list))) |
| 95 |
(not (open-server (car skk-exserv-list))) |
| 96 |
(setq skk-exserv-list (cdr skk-exserv-list)))) |
| 97 |
(car skk-exserv-list)) |
| 98 |
|
| 99 |
(defmethod server-opened-p ((engine network-search-engine)) |
| 100 |
(with-slots (process) engine |
| 101 |
(and process (eq (process-status process) 'open)))) |
| 102 |
|
| 103 |
(defmethod open-server ((engine network-search-engine)) |
| 104 |
;; Return t if process is opened. |
| 105 |
(with-slots (process coding-system) engine |
| 106 |
(condition-case nil |
| 107 |
(progn |
| 108 |
(setq process |
| 109 |
(open-network-stream |
| 110 |
(oref engine process-name) (oref engine buffer) |
| 111 |
(oref engine host) (oref engine service))) |
| 112 |
(if (not process) |
| 113 |
nil |
| 114 |
(process-kill-without-query process) |
| 115 |
(static-cond |
| 116 |
((featurep 'xemacs) |
| 117 |
(set-process-input-coding-system process coding-system) |
| 118 |
(set-process-output-coding-system process coding-system)) |
| 119 |
(t |
| 120 |
(set-process-coding-system process coding-system coding-system))) |
| 121 |
(oset engine process process) |
| 122 |
(eq (process-status process) 'open))) |
| 123 |
(error nil)))) |
| 124 |
|
| 125 |
(defmethod disconnect-server ((engine dbskkd-engine)) |
| 126 |
(with-slots (process-name) engine |
| 127 |
(if (eq (process-status process-name) 'open) |
| 128 |
(progn |
| 129 |
(process-send-string process-name "0") |
| 130 |
(accept-process-output (get-process process-name)))))) |
| 131 |
|
| 132 |
;;;###autoload |
| 133 |
(defun skk-server-version () |
| 134 |
(interactive) |
| 135 |
(if (interactive-p) |
| 136 |
(message (skk-server-version)) |
| 137 |
(server-version (car skk-exserv-list)))) |
| 138 |
|
| 139 |
(defmethod server-version ((engine dbskkd-engine)) |
| 140 |
(or skk-exserv-list |
| 141 |
(skk-error "Lack of host information of SKK server" |
| 142 |
"SKK $B%5!<%P!<$N%[%9%H>pJs$,$"$j$^$;$s(B" )) |
| 143 |
(or (skk-open-server) |
| 144 |
(skk-error "Cannot open connection to SKK server" |
| 145 |
"SKK $B%5!<%P!<$H%3%M%/%7%g%s$rD%$k$3$H$,$G$-$^$;$s(B")) |
| 146 |
(save-excursion |
| 147 |
(unwind-protect |
| 148 |
(progn |
| 149 |
(set-buffer (oref engine buffer)) |
| 150 |
(let (v) |
| 151 |
(erase-buffer) |
| 152 |
;; $B%5!<%P!<%P!<%8%g%s$rF@$k!#(B |
| 153 |
(process-send-string (oref engine process-name) "2") |
| 154 |
(while (and (server-opened-p engine) (eq (buffer-size) 0)) |
| 155 |
(accept-process-output)) |
| 156 |
(setq v (buffer-string)) |
| 157 |
(erase-buffer) |
| 158 |
;; $B%[%9%HL>$rF@$k!#(B |
| 159 |
(process-send-string (oref engine process-name) "3") |
| 160 |
(while (and (server-opened-p engine) (eq (buffer-size) 0)) |
| 161 |
(accept-process-output)) |
| 162 |
(goto-char (point-min)) |
| 163 |
(format |
| 164 |
(concat "SKK SERVER version %s" |
| 165 |
(if skk-japanese-message-and-error |
| 166 |
"($B%[%9%HL>(B %s)" |
| 167 |
"running on HOST %s")) |
| 168 |
v (buffer-string) ))) |
| 169 |
(erase-buffer)))) |
| 170 |
|
| 171 |
;;;###autoload |
| 172 |
(defun skk-exserv-search () |
| 173 |
(let ((server (skk-open-server))) |
| 174 |
(and server (search-server server)))) |
| 175 |
|
| 176 |
(defmethod search-server ((engine dbskkd-engine)) |
| 177 |
(let ((key |
| 178 |
(if skk-use-numeric-conversion |
| 179 |
(skk-num-compute-henkan-key skk-henkan-key) |
| 180 |
skk-henkan-key)) |
| 181 |
(okurigana (or skk-henkan-okurigana skk-okuri-char))) |
| 182 |
(with-current-buffer (oref engine buffer) |
| 183 |
(let (l) |
| 184 |
(erase-buffer) |
| 185 |
(process-send-string (oref engine process-name) (concat "1" key " ")) |
| 186 |
(while (and (server-opened-p engine) (eq (buffer-size) 0)) |
| 187 |
(accept-process-output)) |
| 188 |
;; found key successfully, so check if a whole line is received. |
| 189 |
(if (and (eq (char-after 1) ?1) (eq (char-after (1- (point-max))) ?\n)) |
| 190 |
(progn |
| 191 |
(goto-char (point-min)) |
| 192 |
(forward-char 2) |
| 193 |
(and (setq l (skk-compute-henkan-lists okurigana)) |
| 194 |
(cond ((and okurigana skk-henkan-okuri-strictly) |
| 195 |
;; $BAw$j2>L>$,F10l$N%(%s%H%j$N$_$rJV$9!#(B |
| 196 |
(nth 2 l)) |
| 197 |
((and okurigana skk-henkan-strict-okuri-precedence) |
| 198 |
(skk-nunion (nth 2 l) (car l))) |
| 199 |
(t (car l)))))))))) |
| 200 |
|
| 201 |
;;;###autoload |
| 202 |
(defun skk-adjust-search-prog-list-for-server-search (&optional non-del) |
| 203 |
;; skk-exserv-list $B$,(B nil $B$G$"$l$P!"(B |
| 204 |
;; skk-search-prog-list $B$+$i(B skk-exserv-search $B$r(B car $B$K;}$D%j%9%H$r>C$9!#(B |
| 205 |
;; non-nil $B$G$"$l$P!"2C$($k!#(B |
| 206 |
(if skk-exserv-list |
| 207 |
(if (null (assq 'skk-exserv-search skk-search-prog-list)) |
| 208 |
;; skk-search-prog-list $B$,(B nil $B$H$$$&$3$H$O$^$:$J$$$@$m$&$,!"G0$N$?(B |
| 209 |
;; $B$a!"(Bsetq $B$7$F$*$/!#(B |
| 210 |
(setq skk-search-prog-list |
| 211 |
;; $BKvHx$KIU$1$k!#KvHx$K$O(B (skk-okuri-search) $B$r;}$C$F$-$?$$?M(B |
| 212 |
;; $B$b$$$k$+$b!#%*%W%7%g%s$GIU$1$k>l=j$rJQ99$9$k$h$&$K$7$?J}$,(B |
| 213 |
;; $BNI$$!)(B |
| 214 |
(nconc skk-search-prog-list (list '(skk-exserv-search))))) |
| 215 |
(if (not non-del) |
| 216 |
(remove-alist 'skk-search-prog-list 'skk-exserv-search)))) |
| 217 |
|
| 218 |
;; (add-hook 'skk-mode-hook 'skk-adjust-search-prog-list-for-server-search) |
| 219 |
(add-hook 'kill-emacs-hook |
| 220 |
(function (lambda () (disconnect-server (car skk-exserv-list))))) |
| 221 |
|
| 222 |
(run-hooks 'skk-exserv-load-hook) |
| 223 |
|
| 224 |
(provide 'skk-exserv) |
| 225 |
|
| 226 |
;; Local Variables: |
| 227 |
;; indent-tabs-mode: nil |
| 228 |
;; End: |
| 229 |
|
| 230 |
;;; skk-exserv.el ends here |