1 ;;; ids-find.el --- search utility based on Ideographic-structures
3 ;; Copyright (C) 2002,2003,2005 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: Kanji, Ideographs, search, IDS, CHISE, UCS, Unicode
8 ;; This file is a part of Tomoyo-Tools.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (defun ids-index-store-char (product component)
28 (let ((ret (get-char-attribute ; char-feature
29 component 'ideographic-products)))
30 (unless (memq product ret)
31 (put-char-attribute component 'ideographic-products
33 ;; (when ret (setq ret (get-char-attribute ; char-feature
34 ;; component 'ideographic-structure))
35 ;; (ids-index-store-structure product ret))
38 (defun ids-index-store-structure (product structure)
40 (dolist (cell (cdr structure))
42 (setq cell (plist-get cell :char)))
43 (cond ((characterp cell)
44 (ids-index-store-char product cell))
45 ((setq ret (assq 'ideographic-structure cell))
46 (ids-index-store-structure product (cdr ret)))
47 ;; ((setq ret (find-char cell))
48 ;; (ids-index-store-char product ret))
52 (defun ids-update-index ()
56 (ids-index-store-structure c v)
58 'ideographic-structure)
59 (save-char-attribute-table 'ideographic-products))
62 (mount-char-attribute-table 'ideographic-products)
65 (defun ids-find-all-products (char)
67 (dolist (cell (char-feature char 'ideographic-products))
68 (unless (memq cell dest)
69 (setq dest (cons cell dest)))
70 (setq dest (union dest (ids-find-all-products cell))))
73 (defun of-component-features ()
75 (dolist (feature (char-attribute-list))
76 (when (string-match "^<-.*[@/]component\\(/[^*/]+\\)*$"
77 (symbol-name feature))
79 (cons '<-ideographic-component-forms
82 (defun to-component-features ()
84 (dolist (feature (char-attribute-list))
85 (when (string-match "^->.*[@/]component\\(/[^*/]+\\)*$"
86 (symbol-name feature))
88 (cons '->ideographic-component-forms
92 (defun char-component-variants (char)
93 (let ((dest (list char))
96 ((setq ret (some (lambda (feature)
97 (get-char-attribute char feature))
98 (to-component-features)))
100 (setq dest (union dest (char-component-variants c))))
102 ((setq ret (get-char-attribute char '->ucs-unified))
103 (setq dest (cons char ret))
105 (setq dest (union dest
106 (some (lambda (feature)
107 (get-char-attribute char feature))
108 (of-component-features))
111 ((and (setq ret (get-char-attribute char '=>ucs))
112 (setq uchr (decode-char '=ucs ret)))
113 (setq dest (cons uchr (char-variants uchr)))
115 (setq dest (union dest
116 (some (lambda (feature)
117 (get-char-attribute char feature))
118 (of-component-features))
124 (unless (memq c dest)
125 (setq dest (cons c dest)))
128 (some (lambda (feature)
129 (char-feature c feature))
130 (of-component-features))
138 (defun ideographic-products-find (&rest components)
139 (if (stringp (car components))
140 (setq components (car components)))
141 (let ((len (length components))
144 (dolist (variant (char-component-variants (elt components 0)))
145 (dolist (product (get-char-attribute variant 'ideographic-products))
146 (unless (memq product products)
147 (setq products (cons product products)))))
153 (dolist (variant (char-component-variants (elt components i)))
154 (dolist (product (get-char-attribute
155 variant 'ideographic-products))
156 (unless (memq product products)
157 (when (memq product dest)
158 (setq products (cons product products))))))
159 (setq dest products)))
164 (defun ideographic-structure-char= (c1 c2)
167 (let ((m1 (char-ucs c1))
171 (some (lambda (feature)
173 (unless (characterp b2)
174 (setq b2 (find-char b2)))
176 (ideographic-structure-char= c1 b2)))
177 (char-feature c2 feature)
178 ;; (get-char-attribute
179 ;; c2 '<-ideographic-component-forms)
181 (of-component-features))
183 (setq m1 (car (get-char-attribute c1 '<-radical))
184 m2 (car (get-char-attribute c2 '<-radical)))
185 (unless (characterp m1)
186 (setq m1 (find-char m1)))
187 (unless (characterp m2)
188 (setq m2 (find-char m2)))
190 (ideographic-structure-char= m1 m2))))))))
192 (defun ideographic-structure-member-compare-components (component s-component)
194 (cond ((char-ref= component s-component #'ideographic-structure-char=))
196 (if (setq ret (assq 'ideographic-structure s-component))
197 (ideographic-structure-member component (cdr ret))))
198 ((setq ret (get-char-attribute s-component 'ideographic-structure))
199 (ideographic-structure-member component ret)))))
202 (defun ideographic-structure-member (component structure)
203 "Return non-nil if COMPONENT is included in STRUCTURE."
204 (or (memq component structure)
206 (setq structure (cdr structure))
207 (ideographic-structure-member-compare-components
208 component (car structure)))
210 (setq structure (cdr structure))
211 (ideographic-structure-member-compare-components
212 component (car structure)))
214 (setq structure (cdr structure))
216 (ideographic-structure-member-compare-components
217 component (car structure))))))
221 (defun ideographic-structure-repertoire-p (structure components)
222 "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
224 (let (ret s-component)
226 (while (setq structure (cdr structure))
227 (setq s-component (car structure))
228 (unless (characterp s-component)
229 (if (setq ret (find-char s-component))
230 (setq s-component ret)))
233 (if (setq ret (assq 'ideographic-structure s-component))
234 (ideographic-structure-repertoire-p
235 (cdr ret) components)))
236 ((member* s-component components
237 :test #'ideographic-structure-char=))
239 (get-char-attribute s-component
240 'ideographic-structure))
241 (ideographic-structure-repertoire-p ret components)))
246 (defvar ids-find-result-buffer "*ids-chars*")
248 (defun ids-find-format-line (c v)
249 (format "%c\t%s\t%s\n"
251 (or (let ((ucs (or (char-ucs c)
252 (encode-char c 'ucs))))
254 (cond ((<= ucs #xFFFF)
255 (format " U+%04X" ucs))
257 (format "U-%08X" ucs)))))
259 (or (ideographic-structure-to-ids v)
262 (defun ids-insert-chars-including-components (components level)
264 (dolist (c (ideographic-products-find components))
265 (setq is (char-feature c 'ideographic-structure))
266 ;; to avoid problems caused by wrong indexes
267 (when (every (lambda (cc)
268 (ideographic-structure-member cc is))
270 ;;(ids-insert-chars-including-components (char-to-string c) (1+ level))
275 (insert (ids-find-format-line c is))
277 (ids-insert-chars-including-components
278 (char-to-string c) (1+ level))
283 (defun ids-find-chars-including-components (components)
284 "Search Ideographs whose structures have COMPONENTS."
285 (interactive "sComponents : ")
286 (with-current-buffer (get-buffer-create ids-find-result-buffer)
287 (setq buffer-read-only nil)
289 (ids-insert-chars-including-components components 0)
291 ;; (dolist (c (ideographic-products-find components))
292 ;; (setq is (char-feature c 'ideographic-structure))
293 ;; ;; to avoid problems caused by wrong indexes
294 ;; ;; (when (every (lambda (cc)
295 ;; ;; (ideographic-structure-member cc is))
297 ;; (dolist (dc (ideographic-products-find (char-to-string c)))
298 ;; (setq dis (char-feature dc 'ideographic-structure))
299 ;; ;; ;; to avoid problems caused by wrong indexes
300 ;; ;; (when (every (lambda (dcc)
301 ;; ;; (ideographic-structure-member dcc is))
304 ;; (insert (ids-find-format-line dc dis))
308 ;; (insert (ids-find-format-line c is))
313 (goto-char (point-min)))
314 (view-buffer ids-find-result-buffer))
315 ;; (defun ids-find-chars-including-components (components)
316 ;; "Search Ideographs whose structures have COMPONENTS."
317 ;; (interactive "sComponents : ")
318 ;; (with-current-buffer (get-buffer-create ids-find-result-buffer)
319 ;; (setq buffer-read-only nil)
321 ;; (map-char-attribute
323 ;; (when (every (lambda (p)
324 ;; (ideographic-structure-member p v))
326 ;; (insert (ids-find-format-line c v)))
328 ;; 'ideographic-structure)
329 ;; (goto-char (point-min)))
330 ;; (view-buffer ids-find-result-buffer))
333 (define-obsolete-function-alias 'ideographic-structure-search-chars
334 'ids-find-chars-including-components)
337 (defun ids-find-chars-covered-by-components (components)
338 "Search Ideographs which structures are consisted by subsets of COMPONENTS."
339 (interactive "sComponents: ")
340 (if (stringp components)
341 (setq components (string-to-char-list components)))
342 (with-current-buffer (get-buffer-create ids-find-result-buffer)
343 (setq buffer-read-only nil)
348 (when (ideographic-structure-repertoire-p v components)
349 (insert (ids-find-format-line c v))))
350 'ideographic-structure))
351 (goto-char (point-min)))
352 (view-buffer ids-find-result-buffer))
360 ;;; ids-find.el ends here