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