(ids-index-store-char): Revert to use `char-feature' instead of
[chise/ids.git] / ids-find.el
1 ;;; ids-find.el --- search utility based on Ideographic-structures
2
3 ;; Copyright (C) 2002,2003,2005 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: Kanji, Ideographs, search, IDS, CHISE, UCS, Unicode
7
8 ;; This file is a part of Tomoyo-Tools.
9
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.
14
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.
19
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.
24
25 ;;; Code:
26
27 (defun ids-index-store-char (product component)
28   (let ((ret (char-feature
29               component 'ideographic-products)))
30     (unless (memq product ret)
31       (put-char-attribute component 'ideographic-products
32                           (cons product ret)))
33     (when ret (setq ret (char-feature
34                          component 'ideographic-structure))
35           (ids-index-store-structure product ret))
36     ))
37
38 (defun ids-index-store-structure (product structure)
39   (let (ret)
40     (dolist (cell (cdr structure))
41       (if (char-ref-p cell)
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))
49             ))))
50
51 ;;;###autoload
52 (defun ids-update-index ()
53   (interactive)
54   (map-char-attribute
55    (lambda (c v)
56      (ids-index-store-structure c v)
57      nil)
58    'ideographic-structure)
59   (save-char-attribute-table 'ideographic-products))
60
61
62 (mount-char-attribute-table 'ideographic-products)
63
64 ;;;###autoload
65 (defun ids-find-all-products (char)
66   (let (dest)
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))))
71     dest))
72
73 (defun of-component-features ()
74   (let (dest)
75     (dolist (feature (char-attribute-list))
76       (when (string-match "^<-.*[@/]component\\(/[^*/]+\\)*$"
77                           (symbol-name feature))
78         (push feature dest)))
79     (cons '<-ideographic-component-forms
80           dest)))
81
82 (defun to-component-features ()
83   (let (dest)
84     (dolist (feature (char-attribute-list))
85       (when (string-match "^->.*[@/]component\\(/[^*/]+\\)*$"
86                           (symbol-name feature))
87         (push feature dest)))
88     (cons '->ideographic-component-forms
89           dest)))
90
91 ;;;###autoload
92 (defun char-component-variants (char)
93   (let ((dest (list char))
94         ret uchr)
95     (cond
96      ((setq ret (some (lambda (feature)
97                         (get-char-attribute char feature))
98                       (to-component-features)))
99       (dolist (c ret)
100         (setq dest (union dest (char-component-variants c))))
101       )
102      ((setq ret (get-char-attribute char '->ucs-unified))
103       (setq dest (cons char ret))
104       (dolist (c dest)
105         (setq dest (union dest
106                           (some (lambda (feature)
107                                   (get-char-attribute char feature))
108                                 (of-component-features))
109                           )))
110       )
111      ((and (setq ret (get-char-attribute char '=>ucs))
112            (setq uchr (decode-char '=ucs ret)))
113       (setq dest (cons uchr (char-variants uchr)))
114       (dolist (c dest)
115         (setq dest (union dest
116                           (some (lambda (feature)
117                                   (get-char-attribute char feature))
118                                 (of-component-features))
119                           )))
120       )
121      (t
122       (map-char-family
123        (lambda (c)
124          (unless (memq c dest)
125            (setq dest (cons c dest)))
126          (setq dest
127                (union dest
128                       (some (lambda (feature)
129                               (char-feature c feature))
130                             (of-component-features))
131                       ))
132          nil)
133        char)
134       ))
135     dest))
136
137 ;;;###autoload
138 (defun ideographic-products-find (&rest components)
139   (if (stringp (car components))
140       (setq components (string-to-char-list (car components))))
141   (let (dest products)
142     (dolist (variant (char-component-variants (car components)))
143       (setq products
144             (union products
145                    (get-char-attribute variant 'ideographic-products))))
146     (setq dest products)
147     (while (and dest
148                 (setq components (cdr components)))
149       (setq products nil)
150       (dolist (variant (char-component-variants (car components)))
151       (setq products
152             (union products
153                    (get-char-attribute variant 'ideographic-products))))
154       (setq dest (intersection dest products)))
155     dest))
156 ;; (defun ideographic-products-find (&rest components)
157 ;;   (if (stringp (car components))
158 ;;       (setq components (car components)))
159 ;;   (let ((len (length components))
160 ;;         (i 1)
161 ;;         dest products)
162 ;;     (dolist (variant (char-component-variants (elt components 0)))
163 ;;       (setq products
164 ;;             (union products
165 ;;                    (get-char-attribute variant 'ideographic-products))))
166 ;;     (setq dest products)
167 ;;     (while (and
168 ;;             (< i len)
169 ;;             (progn
170 ;;               (setq products nil)
171 ;;               (dolist (variant (char-component-variants (elt components i)))
172 ;;                 (dolist (product (get-char-attribute
173 ;;                                   variant 'ideographic-products))
174 ;;                   (unless (memq product products)
175 ;;                     (when (memq product dest)
176 ;;                       (setq products (cons product products))))))
177 ;;               (setq dest products)))
178 ;;       (setq i (1+ i)))
179 ;;     products))
180
181
182 (defun ideographic-structure-char= (c1 c2)
183   (or (eq c1 c2)
184       (and c1 c2
185            (let ((m1 (char-ucs c1))
186                  (m2 (char-ucs c2)))
187              (or (and m1 m2
188                       (eq m1 m2))
189                  (some (lambda (feature)
190                          (some (lambda (b2)
191                                  (unless (characterp b2)
192                                    (setq b2 (find-char b2)))
193                                  (and b2
194                                       (ideographic-structure-char= c1 b2)))
195                                (char-feature c2 feature)
196                                ;; (get-char-attribute
197                                ;;  c2 '<-ideographic-component-forms)
198                                ))
199                        (of-component-features))
200                  (progn
201                    (setq m1 (car (get-char-attribute c1 '<-radical))
202                          m2 (car (get-char-attribute c2 '<-radical)))
203                    (unless (characterp m1)
204                      (setq m1 (find-char m1)))
205                    (unless (characterp m2)
206                      (setq m2 (find-char m2)))
207                    (when (or m1 m2)
208                      (ideographic-structure-char= m1 m2))))))))
209
210 (defun ideographic-structure-member-compare-components (component s-component)
211   (let (ret)
212     (cond ((char-ref= component s-component #'ideographic-structure-char=))
213           ((listp s-component)
214            (if (setq ret (assq 'ideographic-structure s-component))
215                (ideographic-structure-member component (cdr ret))))
216           ((setq ret (get-char-attribute s-component 'ideographic-structure))
217            (ideographic-structure-member component ret)))))
218
219 ;;;###autoload
220 (defun ideographic-structure-member (component structure)
221   "Return non-nil if COMPONENT is included in STRUCTURE."
222   (or (memq component structure)
223       (progn
224         (setq structure (cdr structure))
225         (ideographic-structure-member-compare-components
226          component (car structure)))
227       (progn
228         (setq structure (cdr structure))
229         (ideographic-structure-member-compare-components
230          component (car structure)))
231       (progn
232         (setq structure (cdr structure))
233         (and (car structure)
234              (ideographic-structure-member-compare-components
235               component (car structure))))))
236
237
238 ;;;###autoload
239 (defun ideographic-structure-repertoire-p (structure components)
240   "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
241   (and structure
242        (let (ret s-component)
243          (catch 'tag
244            (while (setq structure (cdr structure))
245              (setq s-component (car structure))
246              (unless (characterp s-component)
247                (if (setq ret (find-char s-component))
248                    (setq s-component ret)))
249              (unless (cond
250                       ((listp s-component)
251                        (if (setq ret (assq 'ideographic-structure s-component))
252                            (ideographic-structure-repertoire-p
253                             (cdr ret) components)))
254                       ((member* s-component components
255                                 :test #'ideographic-structure-char=))
256                       ((setq ret
257                              (get-char-attribute s-component
258                                                  'ideographic-structure))
259                        (ideographic-structure-repertoire-p ret components)))
260                (throw 'tag nil)))
261            t))))
262
263
264 (defvar ids-find-result-buffer "*ids-chars*")
265
266 (defun ids-find-format-line (c v)
267   (format "%c\t%s\t%s\n"
268           c
269           (or (let ((ucs (or (char-ucs c)
270                              (encode-char c 'ucs))))
271                 (if ucs
272                     (cond ((<= ucs #xFFFF)
273                            (format "    U+%04X" ucs))
274                           ((<= ucs #x10FFFF)
275                            (format "U-%08X" ucs)))))
276               "          ")
277           (or (ideographic-structure-to-ids v)
278               v)))
279
280 (defun ids-insert-chars-including-components (components
281                                               &optional level ignored-chars)
282   (unless level
283     (setq level 0))
284   (let (is dis i as bs)
285     (dolist (c (sort (ideographic-products-find components)
286                      (lambda (a b)
287                        (if (setq as (char-total-strokes a))
288                            (if (setq bs (char-total-strokes b))
289                                (if (= as bs)
290                                    (ideograph-char< a b)
291                                  (< as bs))
292                              t)
293                          (ideograph-char< a b)))))
294       (unless (memq c ignored-chars)
295         (setq is (char-feature c 'ideographic-structure))
296         (setq i 0)
297         (while (< i level)
298           (insert "\t")
299           (setq i (1+ i)))
300         (insert (ids-find-format-line c is))
301         (setq ignored-chars
302               (ids-insert-chars-including-components
303                (char-to-string c) (1+ level)
304                (cons c ignored-chars))))
305       ))
306   ignored-chars)
307 ;; (defun ids-insert-chars-including-components (components level)
308 ;;   (let (is dis i)
309 ;;     (dolist (c (ideographic-products-find components))
310 ;;       (setq is (char-feature c 'ideographic-structure))
311 ;;       (setq i 0)
312 ;;       (while (< i level)
313 ;;         (insert "\t")
314 ;;         (setq i (1+ i)))
315 ;;       (insert (ids-find-format-line c is))
316 ;;       ;;(forward-line -1)
317 ;;       (ids-insert-chars-including-components
318 ;;        (char-to-string c) (1+ level))
319 ;;       )))
320
321 ;;;###autoload
322 (defun ids-find-chars-including-components (components)
323   "Search Ideographs whose structures have COMPONENTS."
324   (interactive "sComponents : ")
325   (with-current-buffer (get-buffer-create ids-find-result-buffer)
326     (setq buffer-read-only nil)
327     (erase-buffer)
328     (ids-insert-chars-including-components components 0)
329     ;; (let (is dis)
330     ;;   (dolist (c (ideographic-products-find components))
331     ;;     (setq is (char-feature c 'ideographic-structure))
332     ;;     ;; to avoid problems caused by wrong indexes
333     ;;     ;; (when (every (lambda (cc)
334     ;;     ;;                (ideographic-structure-member cc is))
335     ;;     ;;              components)
336     ;;     (dolist (dc (ideographic-products-find (char-to-string c)))
337     ;;       (setq dis (char-feature dc 'ideographic-structure))
338     ;;     ;;     ;; to avoid problems caused by wrong indexes
339     ;;     ;;     (when (every (lambda (dcc)
340     ;;     ;;                    (ideographic-structure-member dcc is))
341     ;;     ;;                  components)
342     ;;       (insert "\t")
343     ;;       (insert (ids-find-format-line dc dis))
344     ;;       (forward-line -1)
345     ;;     ;;       )
346     ;;       )
347     ;;     (insert (ids-find-format-line c is))
348     ;;     (forward-line -1)
349     ;;     ;;   )
350     ;;     )
351     ;;   )
352     (goto-char (point-min)))
353   (view-buffer ids-find-result-buffer))
354 ;; (defun ids-find-chars-including-components (components)
355 ;;   "Search Ideographs whose structures have COMPONENTS."
356 ;;   (interactive "sComponents : ")
357 ;;   (with-current-buffer (get-buffer-create ids-find-result-buffer)
358 ;;     (setq buffer-read-only nil)
359 ;;     (erase-buffer)
360 ;;     (map-char-attribute
361 ;;      (lambda (c v)
362 ;;        (when (every (lambda (p)
363 ;;                       (ideographic-structure-member p v))
364 ;;                     components)
365 ;;          (insert (ids-find-format-line c v)))
366 ;;        nil)
367 ;;      'ideographic-structure)
368 ;;     (goto-char (point-min)))
369 ;;   (view-buffer ids-find-result-buffer))
370
371 ;;;###autoload
372 (define-obsolete-function-alias 'ideographic-structure-search-chars
373   'ids-find-chars-including-components)
374
375 ;;;###autoload
376 (defun ids-find-chars-covered-by-components (components)
377   "Search Ideographs which structures are consisted by subsets of COMPONENTS."
378   (interactive "sComponents: ")
379   (if (stringp components)
380       (setq components (string-to-char-list components)))
381   (with-current-buffer (get-buffer-create ids-find-result-buffer)
382     (setq buffer-read-only nil)
383     (erase-buffer)
384     (let (ucs jis)
385       (map-char-attribute
386        (lambda (c v)
387          (when (ideographic-structure-repertoire-p v components)
388            (insert (ids-find-format-line c v))))
389        'ideographic-structure))
390     (goto-char (point-min)))
391   (view-buffer ids-find-result-buffer))
392
393
394 ;;; @ End.
395 ;;;
396
397 (provide 'ids-find)
398
399 ;;; ids-find.el ends here