((ids-find-chars-including-components): Add check code to avoid
[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 ; get-char-attribute
29               component 'ideographic-products)))
30     (unless (memq product ret)
31       (put-char-attribute component 'ideographic-products
32                           (cons product ret)))
33     (when (setq ret (char-feature component 'ideographic-structure))
34       (ids-index-store-structure product ret))))
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 ;;;###autoload
72 (defun char-component-variants (char)
73   (let (dest ret uchr)
74     (cond
75      ((setq ret (char-feature char '<-ideographic-component-forms))
76       (dolist (c ret)
77         (setq dest (union dest (char-component-variants c)))))
78      ((setq ret (get-char-attribute char '->ucs-unified))
79       (setq dest (cons char ret))
80       (dolist (c dest)
81         (setq dest (union dest
82                           (get-char-attribute
83                            c '->ideographic-component-forms))))
84       )
85      ((and (setq ret (get-char-attribute char '=>ucs))
86            (setq uchr (decode-char '=ucs ret)))
87       (setq dest (cons uchr (char-variants uchr)))
88       (dolist (c dest)
89         (setq dest (union dest
90                           (get-char-attribute
91                            c '->ideographic-component-forms))))
92       )
93      (t
94       (map-char-family (lambda (c)
95                          (unless (memq c dest)
96                            (setq dest (cons c dest)))
97                          (setq dest
98                                (union dest
99                                       (get-char-attribute
100                                        c '->ideographic-component-forms)))
101                          nil)
102                        char)))
103     dest))
104
105 ;;;###autoload
106 (defun ideographic-products-find (&rest components)
107   (if (stringp (car components))
108       (setq components (car components)))
109   (let ((len (length components))
110         (i 1)
111         dest products)
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)))))
116     (setq dest products)
117     (while (and
118             (< i len)
119             (progn
120               (setq products nil)
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)))
128       (setq i (1+ i)))
129     products))
130
131
132 (defun ideographic-structure-char= (c1 c2)
133   (or (eq c1 c2)
134       (and c1 c2
135            (let ((m1 (char-ucs c1))
136                  (m2 (char-ucs c2)))
137              (or (and m1 m2
138                       (eq m1 m2))
139                  (some (lambda (b2)
140                          (unless (characterp b2)
141                            (setq b2 (find-char b2)))
142                          (and b2
143                               (ideographic-structure-char= c1 b2)))
144                        (get-char-attribute
145                         c2 '<-ideographic-component-forms))
146                  (progn
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)))
153                    (when (or m1 m2)
154                      (ideographic-structure-char= m1 m2))))))))
155
156 (defun ideographic-structure-member-compare-components (component s-component)
157   (let (ret)
158     (cond ((char-ref= component s-component #'ideographic-structure-char=))
159           ((listp s-component)
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)))))
164
165 ;;;###autoload
166 (defun ideographic-structure-member (component structure)
167   "Return non-nil if COMPONENT is included in STRUCTURE."
168   (or (memq component structure)
169       (progn
170         (setq structure (cdr structure))
171         (ideographic-structure-member-compare-components
172          component (car structure)))
173       (progn
174         (setq structure (cdr structure))
175         (ideographic-structure-member-compare-components
176          component (car structure)))
177       (progn
178         (setq structure (cdr structure))
179         (and (car structure)
180              (ideographic-structure-member-compare-components
181               component (car structure))))))
182
183
184 ;;;###autoload
185 (defun ideographic-structure-repertoire-p (structure components)
186   "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
187   (and structure
188        (let (ret s-component)
189          (catch 'tag
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)))
195              (unless (cond
196                       ((listp s-component)
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=))
202                       ((setq ret
203                              (get-char-attribute s-component
204                                                  'ideographic-structure))
205                        (ideographic-structure-repertoire-p ret components)))
206                (throw 'tag nil)))
207            t))))
208
209
210 (defvar ids-find-result-buffer "*ids-chars*")
211
212 (defun ids-find-format-line (c v)
213   (format "%c\t%s\t%s\n"
214           c
215           (or (let ((ucs (or (char-ucs c)
216                              (encode-char c 'ucs))))
217                 (if ucs
218                     (cond ((<= ucs #xFFFF)
219                            (format "    U+%04X" ucs))
220                           ((<= ucs #x10FFFF)
221                            (format "U-%08X" ucs)))))
222               "          ")
223           (or (ideographic-structure-to-ids v)
224               v)))
225
226 ;;;###autoload
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)
232     (erase-buffer)
233     (let (is)
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))
239                      components)
240           (insert (ids-find-format-line c is))
241           )
242         )
243       ;; (forward-line -1)
244       )
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)
252 ;;     (erase-buffer)
253 ;;     (map-char-attribute
254 ;;      (lambda (c v)
255 ;;        (when (every (lambda (p)
256 ;;                       (ideographic-structure-member p v))
257 ;;                     components)
258 ;;          (insert (ids-find-format-line c v)))
259 ;;        nil)
260 ;;      'ideographic-structure)
261 ;;     (goto-char (point-min)))
262 ;;   (view-buffer ids-find-result-buffer))
263
264 ;;;###autoload
265 (define-obsolete-function-alias 'ideographic-structure-search-chars
266   'ids-find-chars-including-components)
267
268 ;;;###autoload
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)
276     (erase-buffer)
277     (let (ucs jis)
278       (map-char-attribute
279        (lambda (c v)
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))
285
286
287 ;;; @ End.
288 ;;;
289
290 (provide 'ids-find)
291
292 ;;; ids-find.el ends here