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