Debian Codegraph @

experimental/skk-exsearch.el

raw
1 ;;; skk-exsearch.el --- 外部検索プログラム共瘢雹用 interface -*- coding: iso-2022-jp -*-
2
3 ;; Copyright (C) 2000 NAKAJIMA Mikio <[email protected]>
4
5 ;; Author: NAKAJIMA Mikio <[email protected]>
6 ;; Maintainer: SKK Development Team
7 ;; URL: https://github.com/skk-dev/ddskk
8 ;; Keywords: japanese
9
10 ;; This file is part of Daredevil SKK.
11
12 ;; This program is free software: you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation, either version 3 of
15 ;; the License, or (at your option) any later version.
16
17 ;; This program is distributed in the hope that it will be
18 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
19 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 ;; PURPOSE. See the GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This file needs eieio package (which provides CLOS like OO
28 ;; programming) that can be found at;
29
30 ;; ftp://ftp.ultranet.com/pub/zappo
31
32 ;; This program may be (or may not be) a core engine of external
33 ;; searching program of Daredevil (or some other new branch) SKK.
34
35 ;;; Code:
36
37 (eval-when-compile
38 (require 'skk-macs)
39 (require 'skk-vars))
40 (require 'path-util) ; for exec-installed-p.
41 (require 'eieio)
42
43 (defvar skk-grep-dic
44 "/usr/local/share/skk/SKK-JISYO.L"
45 "*grep search dictionary.")
46
47 (defclass search-engine ()
48 ((program :initarg :program
49 :initform nil
50 :documentation "Program file.")
51 ;;(argument :initarg :argument :initform nil :documentation "")
52 (dictionary :initarg :dictionary
53 :initform nil
54 :documentation "Dictionary file to be searched."))
55 "External search engine superclass.")
56
57 (defclass synchronous-search-engine (search-engine)
58 ((infile :initarg :infile
59 :initform nil
60 :documentation
61 "This is where the program's input comes from. (nil means `/dev/null').")
62 (stderr :initarg :stderr
63 :initform t
64 :documentation "What to do with standard error in the child.\
65 nil (discard standard error output), t (mix it with ordinary output),\
66 or a file name string.")
67 (success-exit-code :initarg :success-exit-code
68 :initform 0
69 :documentation "Numeric exit status of success.")
70 (error-exit-code :initarg :error-exit-code
71 :initform 2
72 :documentation "Numeric exit status of error."))
73 "External synchronous search engine class.")
74
75 (defclass regular-engine (synchronous-search-engine)
76 ((coding-system :initarg :coding-system
77 :initform (lambda () (skk-find-coding-system skk-jisyo-code))))
78 "Regular search engine type.
79 Call program synchronously in separate process.
80 Output of this type is a line that contains candidates delimited by slash.")
81
82
83 (defclass grep-engine (synchronous-search-engine)
84 ((coding-system :initarg :coding-system
85 :initform (lambda () (skk-find-coding-system skk-jisyo-code))))
86 "Regular search engine type for grep search.
87 Call program synchronously in separate process.
88 Output of this type is a line that contains candidates delimited by slash.")
89
90
91 (defclass look-engine (synchronous-search-engine)
92 (())
93 "look type.
94 Call program synchronously in separate process.
95 This type inserts multiple lines to the buffer. Each line contains a candidate.")
96
97 (defmethod setup-synchronous-engine ((engine synchronous-search-engine))
98 (and (slot-exists-p engine 'coding-system)
99 ;; return nil if cdbget executable binary does not installed.
100 (slot-value engine 'program)
101 (with-slots ((code coding-system)) engine
102 (if (and code (fboundp 'modify-coding-system-alist))
103 (modify-coding-system-alist
104 'process (oref engine program) (cons code code))))))
105
106 (defvar cdbget (make-instance regular-engine
107 :program (exec-installed-p "cdbget")
108 :infile "/usr/local/share/skk/SKK-JISYO.L.cdb")
109 "*cdbget search engine object.")
110 (setup-synchronous-engine cdbget)
111
112 (defvar grep (make-instance grep-engine :program
113 (exec-installed-p "grep")
114 :infile skk-grep-dic)
115 "*grep search engine object.")
116
117 (setup-synchronous-engine grep)
118
119 (defvar look (make-instance look-engine :program
120 (exec-installed-p "look"))
121 "*look search engine object.")
122
123
124 (defmethod core-engine ((engine synchronous-search-engine) argument)
125 ;; core search engine
126 (save-excursion
127 (let ((exit-code (apply 'call-process (oref engine program)
128 (oref engine infile)
129 (list t ;output to current buffer.
130 (oref engine stderr))
131 nil
132 argument))
133 error)
134 (cond ((and (= (oref engine success-exit-code) exit-code)
135 (> (buffer-size) 0)))
136 ((>= exit-code (oref engine error-exit-code))
137 (error (buffer-substring-no-properties (point-min) (point-max))))))))
138
139 (defmethod search-engine ((engine regular-engine) &rest argument)
140 (let ((okurigana (or skk-henkan-okurigana skk-okuri-char))
141 l)
142 (with-temp-buffer
143 (if (core-engine engine argument)
144 (progn
145 (forward-char 1)
146 (and (setq l (skk-compute-henkan-lists okurigana))
147 (cond ((and okurigana skk-henkan-okuri-strictly)
148 (nth 2 l))
149 ((and okurigana skk-henkan-strict-okuri-precedence)
150 (skk-nunion (nth 2 l) (car l)))
151 (t (car l)))))))))
152
153 (defmethod search-engine ((engine grep-engine) &rest argument)
154 (let ((okurigana (or skk-henkan-okurigana skk-okuri-char))
155 (tmparg argument)
156 l)
157 (with-temp-buffer
158 (setq argument (cons (concat "^" (car tmparg) " ") nil))
159 (if (core-engine engine argument)
160 (progn
161 (while (not (eq (char-after) ?/))
162 (forward-char 1))
163 (forward-char 1)
164 (and (setq l (skk-compute-henkan-lists okurigana))
165 (cond ((and okurigana skk-henkan-okuri-strictly)
166 (nth 2 l))
167 ((and okurigana skk-henkan-strict-okuri-precedence)
168 (skk-nunion (nth 2 l) (car l)))
169 (t (car l)))))))))
170
171
172
173 (defmethod search-engine ((engine look-engine) &rest argument)
174 (with-temp-buffer
175 (let ((word argument)
176 opt)
177 (and (oref engine dictionary)
178 (nconc argument (list (oref engine dictionary))))
179 (and skk-look-dictionary-order (setq opt "d"))
180 (and skk-look-ignore-case (setq opt (concat "f" opt)))
181 (and skk-look-use-alternate-dictionary
182 (setq opt (concat "a" opt)))
183 (and opt (setq argument (cons (concat "-" opt) argument)))
184 (and skk-look-termination-character
185 (setq argument
186 (cons (list "-t" skk-look-termination-character) argument)))
187 (and (core-engine engine argument)
188 (delete word (split-string (buffer-substring-no-properties
189 (point-min) (1- (point-max)))
190 "\n"))))))
191
192 ;;;###autoload
193 (defun skk-cdbget-search ()
194 (search-engine cdbget (if skk-use-numeric-conversion
195 (skk-num-compute-henkan-key skk-henkan-key)
196 skk-henkan-key)))
197
198 ;;;###autoload
199 (defun skk-grep-search ()
200 (search-engine grep (if skk-use-numeric-conversion
201 (skk-num-compute-henkan-key skk-henkan-key)
202 skk-henkan-key)))
203
204 ;;;###autoload
205 (defun skk-look-search ()
206 (and skk-abbrev-mode
207 (eq (aref skk-henkan-key (1- (length skk-henkan-key))) ?*)
208 (let ((args (substring skk-henkan-key 0 (1- (length skk-henkan-key))))
209 v)
210 (if (not skk-look-use-ispell)
211 (setq v (search-engine look args))
212 (setq v (skk-look-ispell args)))
213 (if (not skk-look-recursive-search)
214 v
215 (let (skk-henkan-key v2 v3)
216 (while v
217 (let ((skk-current-search-prog-list (copy-sequence skk-search-prog-list)))
218 (setq skk-current-search-prog-list
219 (delete '(skk-look) skk-current-search-prog-list))
220 (setq skk-current-search-prog-list
221 (delete '(skk-look-search) skk-current-search-prog-list))
222 (setq skk-henkan-key (car v))
223 (while skk-current-search-prog-list
224 (setq v3 (skk-search)
225 v2 (if (not skk-look-expanded-word-only)
226 (skk-nunion v2 (cons (car v) v3))
227 (if v3
228 (skk-nunion v2 (cons (car v) v3))
229 v2)))))
230 (setq v (cdr v)))
231 v2)))))
232
233 (provide 'skk-exsearch)
234
235 ;; Local Variables:
236 ;; indent-tabs-mode: nil
237 ;; End:
238
239 ;;; skk-exsearch.el ends here