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 (char-feature ; get-char-attribute
29 component 'ideographic-products)))
30 (unless (memq product ret)
31 (put-char-attribute component 'ideographic-products
33 (when (setq ret (char-feature component 'ideographic-structure))
34 (ids-index-store-structure product ret))))
36 (defun ids-index-store-structure (product structure)
38 (dolist (cell (cdr structure))
40 (setq cell (plist-get cell :char)))
41 (cond ((characterp cell)
42 (ids-index-store-char product cell))
43 ((setq ret (assq 'ideographic-structure cell))
44 (ids-index-store-structure product (cdr ret)))
45 ((setq ret (find-char cell))
46 (ids-index-store-char product ret))
50 (defun ids-update-index ()
54 (ids-index-store-structure c v)
56 'ideographic-structure)
57 (save-char-attribute-table 'ideographic-products))
60 (mount-char-attribute-table 'ideographic-products)
63 (defun ids-find-all-products (char)
65 (dolist (cell (char-feature char 'ideographic-products))
66 (unless (memq cell dest)
67 (setq dest (cons cell dest)))
68 (setq dest (union dest (ids-find-all-products cell))))
72 (defun char-component-variants (char)
75 ((setq ret (char-feature char '<-ideographic-component-forms))
77 (setq dest (union dest (char-component-variants c)))))
78 ((setq ret (get-char-attribute char '->ucs-unified))
79 (setq dest (cons char ret))
81 (setq dest (union dest
83 c '->ideographic-component-forms))))
85 ((and (setq ret (get-char-attribute char '=>ucs))
86 (setq uchr (decode-char '=ucs ret)))
87 (setq dest (cons uchr (char-variants uchr)))
89 (setq dest (union dest
91 c '->ideographic-component-forms))))
94 (map-char-family (lambda (c)
96 (setq dest (cons c dest)))
100 c '->ideographic-component-forms)))
106 (defun ideographic-products-find (&rest components)
107 (if (stringp (car components))
108 (setq components (car components)))
109 (let ((len (length components))
112 (dolist (variant (char-component-variants (elt components 0)))
113 (dolist (product (get-char-attribute variant 'ideographic-products))
114 (unless (memq product products)
115 (setq products (cons product products)))))
121 (dolist (variant (char-component-variants (elt components i)))
122 (dolist (product (get-char-attribute
123 variant 'ideographic-products))
124 (unless (memq product products)
125 (when (memq product dest)
126 (setq products (cons product products))))))
127 (setq dest products)))
132 (defun ideographic-structure-char= (c1 c2)
135 (let ((m1 (char-ucs c1))
140 (unless (characterp b2)
141 (setq b2 (find-char b2)))
143 (ideographic-structure-char= c1 b2)))
145 c2 '<-ideographic-component-forms))
147 (setq m1 (car (get-char-attribute c1 '<-radical))
148 m2 (car (get-char-attribute c2 '<-radical)))
149 (unless (characterp m1)
150 (setq m1 (find-char m1)))
151 (unless (characterp m2)
152 (setq m2 (find-char m2)))
154 (ideographic-structure-char= m1 m2))))))))
156 (defun ideographic-structure-member-compare-components (component s-component)
158 (cond ((char-ref= component s-component #'ideographic-structure-char=))
160 (if (setq ret (assq 'ideographic-structure s-component))
161 (ideographic-structure-member component (cdr ret))))
162 ((setq ret (get-char-attribute s-component 'ideographic-structure))
163 (ideographic-structure-member component ret)))))
166 (defun ideographic-structure-member (component structure)
167 "Return non-nil if COMPONENT is included in STRUCTURE."
168 (or (memq component structure)
170 (setq structure (cdr structure))
171 (ideographic-structure-member-compare-components
172 component (car structure)))
174 (setq structure (cdr structure))
175 (ideographic-structure-member-compare-components
176 component (car structure)))
178 (setq structure (cdr structure))
180 (ideographic-structure-member-compare-components
181 component (car structure))))))
185 (defun ideographic-structure-repertoire-p (structure components)
186 "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
188 (let (ret s-component)
190 (while (setq structure (cdr structure))
191 (setq s-component (car structure))
192 (unless (characterp s-component)
193 (if (setq ret (find-char s-component))
194 (setq s-component ret)))
197 (if (setq ret (assq 'ideographic-structure s-component))
198 (ideographic-structure-repertoire-p
199 (cdr ret) components)))
200 ((member* s-component components
201 :test #'ideographic-structure-char=))
203 (get-char-attribute s-component
204 'ideographic-structure))
205 (ideographic-structure-repertoire-p ret components)))
210 (defvar ids-find-result-buffer "*ids-chars*")
212 (defun ids-find-format-line (c v)
213 (format "%c\t%s\t%s\n"
215 (or (let ((ucs (or (char-ucs c)
216 (encode-char c 'ucs))))
218 (cond ((<= ucs #xFFFF)
219 (format " U+%04X" ucs))
221 (format "U-%08X" ucs)))))
223 (or (ideographic-structure-to-ids v)
227 (defun ids-find-chars-including-components (components)
228 "Search Ideographs whose structures have COMPONENTS."
229 (interactive "sComponents : ")
230 (with-current-buffer (get-buffer-create ids-find-result-buffer)
231 (setq buffer-read-only nil)
234 (dolist (c (ideographic-products-find components))
235 (setq is (char-feature c 'ideographic-structure))
236 ;; to avoid problems caused by wrong indexes
237 (when (every (lambda (c)
238 (ideographic-structure-member c is))
240 (insert (ids-find-format-line c is))
245 (goto-char (point-min)))
246 (view-buffer ids-find-result-buffer))
247 ;; (defun ids-find-chars-including-components (components)
248 ;; "Search Ideographs whose structures have COMPONENTS."
249 ;; (interactive "sComponents : ")
250 ;; (with-current-buffer (get-buffer-create ids-find-result-buffer)
251 ;; (setq buffer-read-only nil)
253 ;; (map-char-attribute
255 ;; (when (every (lambda (p)
256 ;; (ideographic-structure-member p v))
258 ;; (insert (ids-find-format-line c v)))
260 ;; 'ideographic-structure)
261 ;; (goto-char (point-min)))
262 ;; (view-buffer ids-find-result-buffer))
265 (define-obsolete-function-alias 'ideographic-structure-search-chars
266 'ids-find-chars-including-components)
269 (defun ids-find-chars-covered-by-components (components)
270 "Search Ideographs which structures are consisted by subsets of COMPONENTS."
271 (interactive "sComponents: ")
272 (if (stringp components)
273 (setq components (string-to-char-list components)))
274 (with-current-buffer (get-buffer-create ids-find-result-buffer)
275 (setq buffer-read-only nil)
280 (when (ideographic-structure-repertoire-p v components)
281 (insert (ids-find-format-line c v))))
282 'ideographic-structure))
283 (goto-char (point-min)))
284 (view-buffer ids-find-result-buffer))
292 ;;; ids-find.el ends here