1 ;;; ndeb.el --- Lookup eblook interface
2 ;; Copyright (C) 1999 Lookup Development Team <lookup@ring.gr.jp>
4 ;; Author: Keisuke Nishida <kei@psn.net>
5 ;; Version: $Id: ndeb.el,v 1.3 1999/05/23 17:27:22 knishida Exp $
7 ;; This file is part of Lookup.
9 ;; Lookup is free software; you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2 of the License, or
12 ;; (at your option) any later version.
14 ;; Lookup is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with Lookup; if not, write to the Free Software Foundation,
21 ;; Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 (defconst ndeb-version "1.0")
30 ;:: Customizable variables
34 "Lookup eblook interface."
35 :group 'lookup-agents)
37 (defcustom ndeb-program-name "eblook"
38 "*Program name of eblook."
42 (defcustom ndeb-prompt-string "eblook> "
43 "*Prompt string of eblook."
47 (defcustom ndeb-process-coding-system lookup-process-coding-system
48 "*Coding system for eblook process."
54 ;:: Internal varialbes
57 (defvar ndeb-current-agent nil)
58 (defvar ndeb-current-dictionary nil)
59 (defvar ndeb-current-process nil)
61 (defconst ndeb-method-table
62 '((exact . "exact") (prefix . "word") (suffix . "endword")))
70 ;; (ndeb DIRECTORY :appendix APPENDIX)
72 ;; DIRECTORY - dictionary directory
73 ;; APPENDIX - appendix directory
76 ;; ndeb-process - eblook process related with agent
77 ;; ndeb-dict - last used dictionary
78 ;; ndeb-method - last used value of search-method
79 ;; ndeb-stop - last used value of stop-code
81 (defun ndeb-agent-directory (agent)
82 (let ((dir (lookup-agent-location agent)))
83 (if dir (expand-file-name dir)
84 (error "You should specify a dictionary directory"))))
86 (defun ndeb-agent-appendix (agent)
87 (let ((dir (lookup-agent-option agent ':appendix)))
88 (if dir (expand-file-name dir))))
90 (defun ndeb-agent-coding (agent)
91 (or (lookup-agent-option agent ':coding)
92 ndeb-process-coding-system))
94 (put 'ndeb ':methods '(exact prefix suffix))
95 (put 'ndeb ':gaiji-regexp "<gaiji=\\([^>]*\\)>")
96 (put 'ndeb ':reference-pattern '("<reference>\\(
\e$B"*
\e(B?\\(\\([^<]\\|<gaiji=[^>]*>\\)+\\)\\)</reference=\\([^>]*\\)>" 1 2 4))
98 (put 'ndeb ':headings '(lookup-arrange-gaijis))
100 (put 'ndeb ':arranges
101 '(lookup-arrange-references
102 lookup-arrange-gaijis
103 lookup-arrange-squeezed-references
104 lookup-arrange-default-headings
105 lookup-arrange-fill-lines))
108 '(lookup-adjust-show-gaijis
109 lookup-adjust-check-references
110 lookup-adjust-goto-min))
114 ;; CODE - same as NAME below
115 ;; NAME - given by eblook `list' command
118 ;; :coding - process coding system
119 ;; :stop-code - stop-code used by eblook
121 (defun ndeb-new-dictionary (name title)
122 (lookup-new-dictionary ndeb-current-agent name name title))
124 (defun ndeb-dictionary-coding (dictionary)
125 (or (lookup-dictionary-option dictionary ':coding t)
126 ndeb-process-coding-system))
128 (defun ndeb-dictionary-stopcode (dictionary)
129 (lookup-dictionary-option dictionary ':stop-code t))
133 ;; CODE - entry specific code (e.g. "2c00:340") by eblook `search' command
134 ;; HEADING - given by eblook `search' command
136 (defun ndeb-make-entry (code heading)
137 (lookup-make-entry ndeb-current-dictionary code heading))
143 (put 'ndeb-with-agent 'lisp-indent-function 1)
144 (defmacro ndeb-with-agent (agent &rest body)
145 (` (let ((ndeb-current-agent (, agent))
146 (ndeb-current-process (ndeb-agent-process (, agent))))
149 (put 'ndeb-with-dictionary 'lisp-indent-function 1)
150 (defmacro ndeb-with-dictionary (dictionary &rest body)
151 (` (ndeb-with-agent (lookup-dictionary-agent (, dictionary))
152 (let ((ndeb-current-dictionary (, dictionary)))
153 (unless (eq (, dictionary)
154 (lookup-agent-get-property ndeb-current-agent 'ndeb-dict))
155 ;;
\e$BI,MW$J$H$-$@$1<-=q$r
\e(B select
\e$B$9$k!#
\e(B
156 ;;
\e$B30It%W%m%;%9$H$d$j$H$j$9$k$h$j$3$NJ}$,9bB.$@$m$&$7!"
\e(B
157 ;;
\e$B%G%P%C%0$N$H$-%P%C%U%!$,$4$A$c$4$A$c$9$k$N$O$&$6$C$?$$!#
\e(B
158 (ndeb-process-require
159 (concat "select " (lookup-dictionary-code (, dictionary))))
160 (lookup-agent-put-property ndeb-current-agent 'ndeb-dict
162 ;;
\e$B<-=qKh$KJ8;z%3!<%I$r@_Dj$9$k!#
\e(B
163 (let ((code (ndeb-dictionary-coding (, dictionary))))
165 (set-process-coding-system ndeb-current-process code code))))
168 (defun ndeb-agent-process (agent)
169 (let ((process (lookup-agent-get-property agent 'ndeb-process)))
170 (unless (and process (eq (process-status process) 'run))
171 (if process (lookup-process-kill process))
172 (setq process (ndeb-process-open (ndeb-agent-directory agent)
173 (ndeb-agent-appendix agent)))
174 ;;
\e$B:G=i$K<-=q0lMw$rF@$k$N$KJ8;z%3!<%I$N@_Dj$,I,MW!#
\e(B
175 (let ((coding (ndeb-agent-coding agent)))
177 (set-process-coding-system process coding coding)))
178 ;;
\e$B%3%^%s%I$N<B9TKh$K9T$J$&I,MW$N$"$k=hM}!#
\e(B
179 (let ((ndeb-current-process process))
180 (if lookup-max-hits (ndeb-require-set "max-hits" lookup-max-hits))
181 (if lookup-max-text (ndeb-require-set "max-text" lookup-max-text)))
182 (lookup-agent-put-property agent 'ndeb-process process)
183 (lookup-agent-put-property agent 'ndeb-dict nil)
184 (lookup-agent-put-property agent 'ndeb-method nil)
185 (lookup-agent-put-property agent 'ndeb-stop nil))
188 (defun ndeb-agent-kill-process (agent)
189 (let ((process (lookup-agent-get-property agent 'ndeb-process)))
191 (if (eq (process-status process) 'run)
192 (process-send-string process "quit\n"))
193 (lookup-process-kill process)
194 (lookup-agent-put-property agent 'ndeb-process nil))))
198 ;:: Interface functions
201 (put 'ndeb 'setup 'ndeb-setup)
202 (defun ndeb-setup (agent)
203 (ndeb-with-agent agent
204 (ndeb-process-require "list"
206 (let (name title dicts)
207 (while (re-search-forward "^[^.]+\\. \\([^\t]+\\)\t\\(.*\\)" nil t)
208 (setq name (match-string 1) title (match-string 2))
209 (setq dicts (cons (ndeb-new-dictionary name title) dicts)))
210 (nreverse dicts))))))
212 (put 'ndeb 'clear 'ndeb-clear)
213 (defalias 'ndeb-clear 'ndeb-agent-kill-process)
215 (put 'ndeb 'menu 'ndeb-dictionary-menu)
216 (defun ndeb-dictionary-menu (dictionary)
217 (ndeb-with-dictionary dictionary
218 (ndeb-process-require "menu"
220 (let ((case-fold-search nil) code heading entries)
221 (while (re-search-forward ">\\(.*\\)</[^=]*=\\([^>]*\\)>" nil t)
222 (setq heading (match-string 1) code (match-string 2))
223 (setq entries (cons (ndeb-make-entry code heading) entries)))
224 (nreverse entries))))))
226 (put 'ndeb 'search 'ndeb-dictionary-search)
227 (defun ndeb-dictionary-search (dictionary query)
228 (ndeb-with-dictionary dictionary
229 (let ((method (lookup-query-method query))
230 (last (lookup-agent-get-property ndeb-current-agent 'ndeb-method)))
231 (unless (eq method last)
232 ;;
\e$BI,MW$N$"$k$H$-$@$1
\e(B search-method
\e$B$r@_Dj$9$k!#
\e(Bndeb-dict
\e$B$KF1$8!#
\e(B
233 (ndeb-require-set "search-method"
234 (lookup-assq-ref ndeb-method-table method))
235 (lookup-agent-put-property ndeb-current-agent 'ndeb-method method)))
236 (ndeb-process-require (format "search \"%s\"" (lookup-query-string query))
238 (let (code heading last-code last-heading entries)
239 (while (re-search-forward "^[^.]+\\. \\([^\t]+\\)\t\\(.*\\)" nil t)
240 (setq code (match-string 1) heading (match-string 2))
241 ;;
\e$BF1$8%(%s%H%j$,O"B3$7$F$$$J$$$+%A%'%C%/$9$k!#
\e(B
242 ;;
\e$B$3$l$,$1$C$3$&$"$k$s$@!&!&
\e(B
243 (when (or (not (string= code last-code))
244 (not (string= heading last-heading)))
245 (setq entries (cons (ndeb-make-entry code heading) entries))
246 (setq last-code code last-heading heading)))
247 (nreverse entries))))))
249 (put 'ndeb 'content 'ndeb-dictionary-content)
250 (defun ndeb-dictionary-content (dictionary entry)
251 (ndeb-with-dictionary dictionary
252 (let ((stop (ndeb-dictionary-stopcode dictionary))
253 (last (lookup-agent-get-property ndeb-current-agent 'ndeb-stop)))
254 (unless (eq stop last)
255 ;;
\e$BI,MW$N$"$k$H$-$@$1
\e(B stop-code
\e$B$r@_Dj$9$k!#
\e(Bndeb-dict
\e$B$KF1$8!#
\e(B
256 (ndeb-require-set "stop-code" stop)
257 (lookup-agent-put-property ndeb-current-agent 'ndeb-stop stop)))
258 (ndeb-process-require (concat "content " (lookup-entry-code entry)))))
260 (put 'ndeb 'gaiji 'ndeb-dictionary-gaiji)
261 (defun ndeb-dictionary-gaiji (dictionary code)
262 (ndeb-with-dictionary dictionary
263 (let ((xbm (ndeb-process-require (concat "font " code))))
264 (if (string-match "default_width" xbm)
269 ;:: Internal functions
272 (defun ndeb-require-set (var value)
274 (ndeb-process-require (format "set %s \"%s\"" var value))
275 (ndeb-process-require (format "unset %s" var))))
281 (defun ndeb-process-open (directory appendix)
282 (let* ((args (cons "-q" (cons directory (if appendix (list appendix)))))
283 (buffer (lookup-open-process-buffer (concat " *ndeb+" directory "*")))
284 (process (apply 'start-process "ndeb" buffer ndeb-program-name args)))
285 (process-kill-without-query process)
286 (accept-process-output process)
289 (put 'ndeb-process-require 'lisp-indent-function 1)
290 (defun ndeb-process-require (command &optional filter)
291 (lookup-process-require ndeb-current-process (concat command "\n")
292 (concat "^" ndeb-prompt-string) filter))
296 ;;; ndeb.el ends here