(functional-ideographic-structure-to-apparent-structure): Use
[chise/ids.git] / ids-find.el
1 ;;; ids-find.el --- search utility based on Ideographic-structures ;; -*- coding: utf-8-mcs-er -*-
2
3 ;; Copyright (C) 2002, 2003, 2005, 2006, 2007, 2017, 2020 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 (&optional in-memory)
51   (interactive)
52   (map-char-attribute
53    (lambda (c v)
54      (ids-index-store-structure c v)
55      nil)
56    'ideographic-structure)
57   (map-char-attribute
58    (lambda (c v)
59      (ids-index-store-structure c v)
60      nil)
61    'ideographic-structure@apparent)
62   (unless in-memory
63     (save-char-attribute-table 'ideographic-products)))
64
65
66 (mount-char-attribute-table 'ideographic-products)
67
68 ;;;###autoload
69 (defun ids-find-all-products (char)
70   (let (dest)
71     (dolist (cell (char-feature char 'ideographic-products))
72       (unless (memq cell dest)
73         (setq dest (cons cell dest)))
74       (setq dest (union dest (ids-find-all-products cell))))
75     dest))
76
77 (defun of-component-features ()
78   (let (dest)
79     (dolist (feature (char-attribute-list))
80       (when (string-match "^<-.*[@/]component\\(/[^*/]+\\)*$"
81                           (symbol-name feature))
82         (push feature dest)))
83     (list* '<-mistakable '->mistakable
84            '<-formed '->formed
85            '<-same '->same
86            '<-original '->original
87            '<-ancient '->ancient
88            dest)))
89
90 (defun to-component-features ()
91   (let (dest)
92     (dolist (feature (char-attribute-list))
93       (when (string-match "^->.*[@/]component\\(/[^*/]+\\)*$"
94                           (symbol-name feature))
95         (push feature dest)))
96     dest))
97
98 ;;;###autoload
99 (defun char-component-variants (char)
100   (let ((dest (list char))
101         ret uchr)
102     (dolist (feature (to-component-features))
103       (if (setq ret (get-char-attribute char feature))
104           (dolist (c ret)
105             (setq dest (union dest (char-component-variants c))))))
106     (cond
107      ;; ((setq ret (some (lambda (feature)
108      ;;                    (get-char-attribute char feature))
109      ;;                  (to-component-features)))
110      ;;  (dolist (c ret)
111      ;;    (setq dest (union dest (char-component-variants c))))
112      ;;  )
113      ((setq ret (get-char-attribute char '->ucs-unified))
114       (setq dest (cons char ret))
115       (dolist (c dest)
116         (setq dest (union dest
117                           (some (lambda (feature)
118                                   (get-char-attribute c feature))
119                                 (of-component-features))
120                           )))
121       )
122      ((and (setq ret (get-char-attribute char '=>ucs))
123            (setq uchr (decode-char '=ucs ret)))
124       (setq dest (cons uchr (char-variants uchr)))
125       (dolist (c dest)
126         (setq dest (union dest
127                           (some (lambda (feature)
128                                   (get-char-attribute c feature))
129                                 (of-component-features))
130                           )))
131       )
132      (t
133       (map-char-family
134        (lambda (c)
135          (unless (memq c dest)
136            (setq dest (cons c dest)))
137          (setq dest
138                (union dest
139                       (some (lambda (feature)
140                               (char-feature c feature))
141                             (of-component-features))
142                       ))
143          nil)
144        char)
145       ))
146     dest))
147
148 ;;;###autoload
149 (defun ideographic-products-find (&rest components)
150   (if (stringp (car components))
151       (setq components (string-to-char-list (car components))))
152   (let (dest products)
153     (dolist (variant (char-component-variants (car components)))
154       (setq products
155             (union products
156                    (get-char-attribute variant 'ideographic-products))))
157     (setq dest products)
158     (while (and dest
159                 (setq components (cdr components)))
160       (setq products nil)
161       (dolist (variant (char-component-variants (car components)))
162         (setq products
163               (union products
164                      (get-char-attribute variant 'ideographic-products))))
165       (setq dest (intersection dest products)))
166     dest))
167
168 (defun ideograph-find-products-with-variants (components &optional ignored-chars)
169   (if (stringp components)
170       (setq components (string-to-char-list components)))
171   (let (dest products)
172     (dolist (variant (char-component-variants (car components)))
173       (setq products
174             (union products
175                    (set-difference
176                     (get-char-attribute variant 'ideographic-products)
177                     ignored-chars))))
178     (setq dest products)
179     (while (and dest
180                 (setq components (cdr components)))
181       (setq products nil)
182       (dolist (variant (char-component-variants (car components)))
183         (setq products
184               (union products
185                      (set-difference
186                       (get-char-attribute variant 'ideographic-products)
187                       ignored-chars))))
188       (setq dest (intersection dest products)))
189     dest))
190
191 (defun ideograph-find-products (components &optional ignored-chars)
192   (if (stringp components)
193       (setq components (string-to-char-list components)))
194   (let (dest products)
195     ;; (dolist (variant (char-component-variants (car components)))
196     ;;   (setq products
197     ;;         (union products
198     ;;                (get-char-attribute variant 'ideographic-products))))
199     ;; (setq dest products)
200     (setq dest (get-char-attribute (car components) 'ideographic-products))
201     (while (and dest
202                 (setq components (cdr components)))
203       ;; (setq products nil)
204       ;; (dolist (variant (char-component-variants (car components)))
205       ;;   (setq products
206       ;;         (union products
207       ;;                (get-char-attribute variant 'ideographic-products))))
208       (setq products (get-char-attribute (car components) 'ideographic-products))
209       (setq dest (intersection dest products)))
210     dest))
211
212
213 (defun ideographic-structure-char= (c1 c2)
214   (or (eq c1 c2)
215       (and c1 c2
216            (let ((m1 (char-ucs c1))
217                  (m2 (char-ucs c2)))
218              (or (and m1 m2
219                       (eq m1 m2))
220                  (memq c1 (char-component-variants c2)))))))
221
222 (defun ideographic-structure-member-compare-components (component s-component)
223   (let (ret)
224     (cond ((char-ref= component s-component #'ideographic-structure-char=))
225           ((listp s-component)
226            (if (setq ret (assq 'ideographic-structure s-component))
227                (ideographic-structure-member component (cdr ret))))
228           ((setq ret (get-char-attribute s-component 'ideographic-structure))
229            (ideographic-structure-member component ret)))))
230
231 ;;;###autoload
232 (defun ideographic-structure-member (component structure)
233   "Return non-nil if COMPONENT is included in STRUCTURE."
234   (or (memq component structure)
235       (progn
236         (setq structure (cdr structure))
237         (ideographic-structure-member-compare-components
238          component (car structure)))
239       (progn
240         (setq structure (cdr structure))
241         (ideographic-structure-member-compare-components
242          component (car structure)))
243       (progn
244         (setq structure (cdr structure))
245         (and (car structure)
246              (ideographic-structure-member-compare-components
247               component (car structure))))))
248
249
250 ;;;###autoload
251 (defun ideographic-structure-repertoire-p (structure components)
252   "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
253   (and structure
254        (let (ret s-component)
255          (catch 'tag
256            (while (setq structure (cdr structure))
257              (setq s-component (car structure))
258              (unless (characterp s-component)
259                (if (setq ret (find-char s-component))
260                    (setq s-component ret)))
261              (unless (cond
262                       ((listp s-component)
263                        (if (setq ret (assq 'ideographic-structure s-component))
264                            (ideographic-structure-repertoire-p
265                             (cdr ret) components)))
266                       ((member* s-component components
267                                 :test #'ideographic-structure-char=))
268                       ((setq ret
269                              (get-char-attribute s-component
270                                                  'ideographic-structure))
271                        (ideographic-structure-repertoire-p ret components)))
272                (throw 'tag nil)))
273            t))))
274
275
276 (defvar ids-find-result-buffer "*ids-chars*")
277
278 (defun ids-find-format-line (c v)
279   (format "%c\t%s\t%s\n"
280           c
281           (or (let ((ucs (or (char-ucs c)
282                              (encode-char c 'ucs))))
283                 (if ucs
284                     (cond ((<= ucs #xFFFF)
285                            (format "    U+%04X" ucs))
286                           ((<= ucs #x10FFFF)
287                            (format "U-%08X" ucs)))))
288               "          ")
289           (or (ideographic-structure-to-ids v)
290               v)))
291
292 (defun ids-insert-chars-including-components* (components
293                                                &optional level ignored-chars)
294   (unless level
295     (setq level 0))
296   (let (is i as bs)
297     (dolist (c (sort (copy-tree (ideograph-find-products components
298                                                          ignored-chars))
299                      (lambda (a b)
300                        (if (setq as (char-total-strokes a))
301                            (if (setq bs (char-total-strokes b))
302                                (if (= as bs)
303                                    (ideograph-char< a b)
304                                  (< as bs))
305                              t)
306                          (ideograph-char< a b)))))
307       (unless (memq c ignored-chars)
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         (setq ignored-chars
315               (ids-insert-chars-including-components*
316                (char-to-string c) (1+ level)
317                (cons c ignored-chars))))
318       )
319     )
320   ignored-chars)
321
322 (defun ids-insert-chars-including-components (components
323                                               &optional level ignored-chars)
324   (unless level
325     (setq level 0))
326   (setq ignored-chars
327         (nreverse
328          (ids-insert-chars-including-components* components
329                                                  level ignored-chars)))
330   (let (is i as bs)
331     (dolist (c ignored-chars)
332       (dolist (vc (char-component-variants c))
333         (unless (memq vc ignored-chars)
334           (when (setq is (get-char-attribute vc 'ideographic-structure))
335             (setq i 0)
336             (while (< i level)
337               (insert "\t")
338               (setq i (1+ i)))
339             (insert (ids-find-format-line vc is))
340             (setq ignored-chars
341                   (ids-insert-chars-including-components*
342                    (char-to-string vc) (1+ level)
343                    (cons vc ignored-chars)))))))
344     (dolist (c (sort (copy-tree (ideograph-find-products-with-variants
345                                  components ignored-chars))
346                      (lambda (a b)
347                        (if (setq as (char-total-strokes a))
348                            (if (setq bs (char-total-strokes b))
349                                (if (= as bs)
350                                    (ideograph-char< a b)
351                                  (< as bs))
352                              t)
353                          (ideograph-char< a b)))))
354       (unless (memq c ignored-chars)
355         (setq is (get-char-attribute c 'ideographic-structure))
356         (setq i 0)
357         (while (< i level)
358           (insert "\t")
359           (setq i (1+ i)))
360         (insert (ids-find-format-line c is))
361         (setq ignored-chars
362               (ids-insert-chars-including-components*
363                (char-to-string c) (1+ level)
364                (cons c ignored-chars))))
365       )
366     )
367   ignored-chars)
368
369 ;;;###autoload
370 (defun ids-find-chars-including-components (components)
371   "Search Ideographs whose structures have COMPONENTS."
372   (interactive "sComponents : ")
373   (with-current-buffer (get-buffer-create ids-find-result-buffer)
374     (setq buffer-read-only nil)
375     (erase-buffer)
376     (ids-insert-chars-including-components components 0 nil)
377     ;; (let ((ignored-chars
378     ;;        (nreverse
379     ;;         (ids-insert-chars-including-components components 0 nil
380     ;;                                                #'ideograph-find-products)))
381     ;;       rest)
382     ;;   (setq rest ignored-chars)
383     ;;   ;; (dolist (c rest)
384     ;;   ;;   (setq ignored-chars
385     ;;   ;;         (union ignored-chars
386     ;;   ;;                (ids-insert-chars-including-components
387     ;;   ;;                 (list c) 0 ignored-chars
388     ;;   ;;                 #'ideograph-find-products-with-variants))))
389     ;;   (ids-insert-chars-including-components components 0 ignored-chars
390     ;;                                          #'ideograph-find-products-with-variants))
391     (goto-char (point-min)))
392   (view-buffer ids-find-result-buffer))
393
394 ;;;###autoload
395 (define-obsolete-function-alias 'ideographic-structure-search-chars
396   'ids-find-chars-including-components)
397
398 ;;;###autoload
399 (defun ids-find-chars-covered-by-components (components)
400   "Search Ideographs which structures are consisted by subsets of COMPONENTS."
401   (interactive "sComponents: ")
402   (if (stringp components)
403       (setq components (string-to-char-list components)))
404   (with-current-buffer (get-buffer-create ids-find-result-buffer)
405     (setq buffer-read-only nil)
406     (erase-buffer)
407     (map-char-attribute
408      (lambda (c v)
409        (when (ideographic-structure-repertoire-p v components)
410          (insert (ids-find-format-line c v))))
411      'ideographic-structure)
412     (goto-char (point-min)))
413   (view-buffer ids-find-result-buffer))
414
415
416 (defun ideographic-structure-merge-components-alist (ca1 ca2)
417   (let ((dest-alist ca1)
418         ret)
419     (dolist (cell ca2)
420       (if (setq ret (assq (car cell) dest-alist))
421           (setcdr ret (+ (cdr ret)(cdr cell)))
422         (setq dest-alist (cons cell dest-alist))))
423     dest-alist))
424
425 (defun ideographic-structure-to-components-alist (structure)
426   (apply #'ideographic-structure-to-components-alist* structure))
427
428 (defun ideographic-structure-to-components-alist* (operator component1 component2
429                                                             &optional component3
430                                                             &rest opts)
431   (let (dest-alist ret)
432     (setq dest-alist
433           (cond ((characterp component1)
434                  (unless (encode-char component1 'ascii)
435                    (list (cons component1 1)))
436                  )
437                 ((setq ret (assq 'ideographic-structure component1))
438                  (ideographic-structure-to-components-alist (cdr ret))
439                  )
440                 ((setq ret (find-char component1))
441                  (list (cons ret 1))
442                  )))
443     (setq dest-alist
444           (ideographic-structure-merge-components-alist
445            dest-alist
446            (cond ((characterp component2)
447                   (unless (encode-char component2 'ascii)
448                     (list (cons component2 1)))
449                   )
450                  ((setq ret (assq 'ideographic-structure component2))
451                   (ideographic-structure-to-components-alist (cdr ret))
452                   )
453                  ((setq ret (find-char component2))
454                   (list (cons ret 1))
455                   ))))
456     (if (memq operator '(?\u2FF2 ?\u2FF3))
457         (ideographic-structure-merge-components-alist
458          dest-alist
459          (cond ((characterp component3)
460                 (unless (encode-char component3 'ascii)
461                   (list (cons component3 1)))
462                 )
463                ((setq ret (assq 'ideographic-structure component3))
464                 (ideographic-structure-to-components-alist (cdr ret))
465                 )
466                ((setq ret (find-char component3))
467                 (list (cons ret 1))
468                 )))
469       dest-alist)))
470
471 (defun ids-find-merge-variables (ve1 ve2)
472   (cond ((eq ve1 t)
473          ve2)
474         ((eq ve2 t)
475          ve1)
476         (t
477          (let ((dest-alist ve1)
478                (rest ve2)
479                cell ret)
480            (while (and rest
481                        (setq cell (car rest))
482                        (if (setq ret (assq (car cell) ve1))
483                            (eq (cdr ret)(cdr cell))
484                          (setq dest-alist (cons cell dest-alist))))
485              (setq rest (cdr rest)))
486            (if rest
487                nil
488              dest-alist)))))
489
490 ;;;###autoload
491 (defun ideographic-structure-equal (structure1 structure2)
492   (let (dest-alist ret)
493     (and (setq dest-alist (ideographic-structure-character=
494                            (car structure1)(car structure2)))
495          (setq ret (ideographic-structure-character=
496                     (nth 1 structure1)(nth 1 structure2)))
497          (setq dest-alist (ids-find-merge-variables dest-alist ret))
498          (setq ret (ideographic-structure-character=
499                     (nth 2 structure1)(nth 2 structure2)))
500          (setq dest-alist (ids-find-merge-variables dest-alist ret))
501          (if (memq (car structure1) '(?\u2FF2 ?\u2FF3))
502              (and (setq ret (ideographic-structure-character=
503                              (nth 3 structure1)(nth 3 structure2)))
504                   (setq dest-alist (ids-find-merge-variables dest-alist ret)))
505            dest-alist))))
506
507 ;;;###autoload
508 (defun ideographic-structure-character= (c1 c2)
509   (let (ret ret2)
510     (cond ((characterp c1)
511            (cond ((encode-char c1 'ascii)
512                   (list (cons c1 c2))
513                   )
514                  ((characterp c2)
515                   (if (encode-char c2 'ascii)
516                       (list (cons c2 c1))
517                     (eq c1 c2))
518                   )
519                  ((setq ret2 (find-char c2))
520                   (eq c1 ret2)
521                   )
522                  ((setq ret2 (assq 'ideographic-structure c2))
523                   (and (setq ret (get-char-attribute c1 'ideographic-structure))
524                        (ideographic-structure-equal ret (cdr ret2)))
525                   ))
526            )
527           ((setq ret (assq 'ideographic-structure c1))
528            (cond ((characterp c2)
529                   (if (encode-char c2 'ascii)
530                       (list (cons c2 c1))
531                     (and (setq ret2 (get-char-attribute c2 'ideographic-structure))
532                          (ideographic-structure-equal (cdr ret) ret2)))
533                   )
534                  ((setq ret2 (find-char c2))
535                   (and (setq ret2 (get-char-attribute ret2 'ideographic-structure))
536                        (ideographic-structure-equal (cdr ret) ret2))
537                   )
538                  ((setq ret2 (assq 'ideographic-structure c2))
539                   (ideographic-structure-equal (cdr ret)(cdr ret2))
540                   ))
541            )
542           ((setq ret (find-char c1))
543            (cond ((characterp c2)
544                   (if (encode-char c2 'ascii)
545                       (list (cons c2 c1))
546                     (eq ret c2))
547                   )
548                  ((setq ret2 (find-char c2))
549                   (eq ret ret2)
550                   )
551                  ((setq ret2 (assq 'ideographic-structure c2))
552                   (and (setq ret (get-char-attribute ret 'ideographic-structure))
553                        (ideographic-structure-equal ret (cdr ret2))
554                        )))))))
555
556 ;;;###autoload
557 (defun ideographic-structure-find-chars (structure)
558   (apply #'ideographic-structure-find-chars* structure))
559
560 (defun ideographic-structure-find-chars* (operator component1 component2
561                                                    &optional component3)
562   (let ((comp-alist (ideographic-structure-to-components-alist*
563                      operator component1 component2 component3))
564         c1 c2 c3
565         ret pl str
566         var-alist)
567     (dolist (pc (caar
568                  (sort (mapcar (lambda (cell)
569                                  (if (setq ret (get-char-attribute
570                                                 (car cell) 'ideographic-products))
571                                      (cons ret (length ret))
572                                    (cons nil 0)))
573                                comp-alist)
574                        (lambda (a b)
575                          (< (cdr a)(cdr b))))))
576       (when (and (setq str (get-char-attribute pc 'ideographic-structure))
577                  (setq var-alist
578                        (ideographic-structure-character= (car str) operator))
579                  (setq c1 (nth 1 str))
580                  (setq ret (ideographic-structure-character= c1 component1))
581                  (setq var-alist (ids-find-merge-variables var-alist ret))
582                  (setq c2 (nth 2 str))
583                  (setq ret (ideographic-structure-character= c2 component2))
584                  (setq var-alist (ids-find-merge-variables var-alist ret))
585                  (cond ((memq (car str) '(?\u2FF2 ?\u2FF3))
586                         (setq c3 (nth 3 str))
587                         (and (setq ret (ideographic-structure-character=
588                                         c3 component3))
589                              (ids-find-merge-variables var-alist ret))
590                         )
591                        (t var-alist)))
592         (setq pl (cons pc pl))
593         ))
594     pl))
595
596 ;;;###autoload
597 (defun ideographic-char-count-components (char component)
598   (let ((dest 0)
599         structure)
600     (cond ((eq char component)
601            1)
602           ((setq structure (get-char-attribute char 'ideographic-structure))
603            (dolist (cell (ideographic-structure-to-components-alist structure))
604              (setq dest
605                    (+ dest
606                       (if (eq (car cell) char)
607                           (cdr cell)
608                         (* (ideographic-char-count-components (car cell) component)
609                            (cdr cell))))))
610            dest)
611           (t
612            0))))
613
614
615 ;;;###autoload
616 (defun ideographic-character-get-structure (character)
617   "Return ideographic-structure of CHARACTER.
618 CHARACTER can be a character or char-spec."
619   (let (ret)
620     (cond ((characterp character)
621            (get-char-attribute character 'ideographic-structure)
622            )
623           ((setq ret (assq 'ideographic-structure character))
624            (cdr ret)
625            )
626           ((setq ret (find-char character))
627            (get-char-attribute ret 'ideographic-structure)
628            ))))
629
630 ;;;###autoload
631 (defun ideographic-char-match-component (char component)
632   "Return non-nil if character CHAR has COMPONENT in ideographic-structure.
633 COMPONENT can be a character or char-spec."
634   (or (ideographic-structure-character= char component)
635       (let ((str (ideographic-character-get-structure char)))
636         (and str
637              (or (ideographic-char-match-component (nth 1 str) component)
638                  (ideographic-char-match-component (nth 2 str) component)
639                  (if (memq (car str) '(?\u2FF2 ?\u2FF3))
640                      (ideographic-char-match-component (nth 3 str) component)))))))
641
642 (defun ideographic-structure-char< (a b)
643   (let ((sa (get-char-attribute a 'ideographic-structure))
644         (sb (get-char-attribute b 'ideographic-structure))
645         tsa tsb)
646     (cond (sa
647            (cond (sb
648                   (setq tsa (char-total-strokes a)
649                         tsb (char-total-strokes b))
650                   (if tsa
651                       (if tsb
652                           (or (< tsa tsb)
653                               (and (= tsa tsb)
654                                    (ideograph-char< a b)))
655                         t)
656                     (if tsb
657                         nil
658                       (ideograph-char< a b))))
659                  (t
660                   nil))
661            )
662           (t
663            (cond (sb
664                   t)
665                  (t
666                   (setq tsa (char-total-strokes a)
667                         tsb (char-total-strokes b))
668                   (if tsa
669                       (if tsb
670                           (or (< tsa tsb)
671                               (and (= tsa tsb)
672                                    (ideograph-char< a b)))
673                         t)
674                     (if tsb
675                         nil
676                       (ideograph-char< a b)))
677                   ))
678            ))
679     ))
680
681 (defun ideographic-chars-to-is-a-tree (chars)
682   (let (comp char products others dest rest
683              la lb)
684     (setq chars (sort chars #'ideographic-structure-char<))
685     (while chars
686       (setq comp (pop chars)
687             rest chars
688             products nil
689             others nil)
690       (while rest
691         (setq char (pop rest))
692         (cond
693          ((ideographic-char-match-component char comp)
694           (push char products)
695           )
696          (t
697           (push char others)
698           )))
699       (push (cons comp
700                   ;; (nreverse products)
701                   (if products
702                       (sort (ideographic-chars-to-is-a-tree products)
703                             (lambda (a b)
704                               (setq la (length (cdr a))
705                                     lb (length (cdr b)))
706                               (or (> la lb)
707                                   (and (= la lb)
708                                        (ideograph-char< (car a) (car b))
709                                        ;; (progn
710                                        ;;   (setq tsa (char-total-strokes (car a))
711                                        ;;         tsb (char-total-strokes (car b)))
712                                        ;;   (if tsa
713                                        ;;       (if tsb
714                                        ;;           (or (< tsa tsb)
715                                        ;;               (and (= tsa tsb)
716                                        ;;                    (ideograph-char<
717                                        ;;                     (car a) (car b))))
718                                        ;;         t)
719                                        ;;     (if tsb
720                                        ;;         nil
721                                        ;;       (ideograph-char< (car a) (car b)))))
722                                        ))))
723                     nil)
724                   )
725             dest)
726       (setq chars others))
727     dest))
728
729 (defun ids-find-chars-including-ids* (operator component1 component2
730                                                &optional component3)
731   (let ((comp-alist (ideographic-structure-to-components-alist*
732                      operator component1 component2 component3))
733         (comp-spec
734          (list (list* 'ideographic-structure
735                       operator component1 component2
736                       (if component3
737                           (list component3)))))
738         ret str rest)
739     (dolist (pc (caar
740                  (sort (mapcar (lambda (cell)
741                                  (if (setq ret (get-char-attribute
742                                                 (car cell) 'ideographic-products))
743                                      (cons ret (length ret))
744                                    (cons nil 0)))
745                                comp-alist)
746                        (lambda (a b)
747                          (< (cdr a)(cdr b))))))
748       (when (and (every (lambda (cell)
749                           (>= (ideographic-char-count-components pc (car cell))
750                               (cdr cell)))
751                         comp-alist)
752                  (or (ideographic-char-match-component pc comp-spec)
753                      (and (setq str (get-char-attribute pc 'ideographic-structure))
754                           (ideographic-char-match-component
755                            (list
756                             (cons
757                              'ideographic-structure
758                              (functional-ideographic-structure-to-apparent-structure
759                               str)))
760                            comp-spec))))
761         (push pc rest)))
762     (ideographic-chars-to-is-a-tree rest)))
763
764 (defun ids-find-chars-including-ids (structure)
765   (if (characterp structure)
766       (setq structure (get-char-attribute structure 'ideographic-structure)))
767   (apply #'ids-find-chars-including-ids* structure))
768
769 (defun functional-ideographic-structure-to-apparent-structure (structure)
770   (ideographic-structure-compact
771    (let (enc enc-str enc2-str new-str)
772      (cond
773       ((eq (car structure) ?⿸)
774        (setq enc (nth 1 structure))
775        (when (setq enc-str
776                    (cond ((characterp enc)
777                           (get-char-attribute enc 'ideographic-structure)
778                           )
779                          ((consp enc)
780                           (cdr (assq 'ideographic-structure enc))
781                           )))
782          (cond
783           ((eq (car enc-str) ?⿰)
784            (list ?⿰ (nth 1 enc-str)
785                  (list (list 'ideographic-structure
786                              ?⿱
787                              (nth 2 enc-str)
788                              (nth 2 structure))))
789            )
790           ((and (eq (car enc-str) ?⿲)
791                 (memq (char-ucs (nth 1 enc-str)) '(#x4EBB #x2E85))
792                 (eq (nth 2 enc-str) ?丨))
793            (list ?⿰
794                  (decode-char '=big5-cdp #x8B7A)
795                  (list (list 'ideographic-structure
796                              ?⿱
797                              (nth 3 enc-str)
798                              (nth 2 structure))))
799            )
800           ((eq (car enc-str) ?⿱)
801            (list ?⿱ (nth 1 enc-str)
802                  (list
803                   (cons 'ideographic-structure
804                         (or (functional-ideographic-structure-to-apparent-structure
805                              (setq new-str
806                                    (list
807                                     (cond
808                                      ((characterp (nth 2 enc-str))
809                                       (if (or (eq (encode-char
810                                                    (nth 2 enc-str)
811                                                    '=>ucs@component)
812                                                   #x20087)
813                                               (eq (encode-char
814                                                    (nth 2 enc-str)
815                                                    '=>ucs@component)
816                                                   #x5382)
817                                               (eq (encode-char
818                                                    (nth 2 enc-str)
819                                                    '=>ucs@component)
820                                                   #x4E06)
821                                               (eq (encode-char
822                                                    (nth 2 enc-str)
823                                                    '=big5-cdp)
824                                                   #x89CE)
825                                               (eq (encode-char
826                                                    (nth 2 enc-str)
827                                                    '=>big5-cdp)
828                                                   #x88E2)
829                                               (eq (encode-char
830                                                    (nth 2 enc-str)
831                                                    '=big5-cdp)
832                                                   #x88AD)
833                                               (eq (or (encode-char
834                                                        (nth 2 enc-str)
835                                                        '=>big5-cdp)
836                                                       (encode-char
837                                                        (nth 2 enc-str)
838                                                        '=big5-cdp-itaiji-001))
839                                                   #x8766)
840                                               (eq (car
841                                                    (get-char-attribute
842                                                     (nth 2 enc-str)
843                                                     'ideographic-structure))
844                                                   ?⿸))
845                                           ?⿸
846                                         ?⿰))
847                                      ((eq (car
848                                            (cdr
849                                             (assq 'ideographic-structure
850                                                   (nth 2 enc-str))))
851                                           ?⿸)
852                                       ?⿸)
853                                      (t
854                                       ?⿰))
855                                     (nth 2 enc-str)
856                                     (nth 2 structure)
857                                     )))
858                             new-str))))
859            )
860           ((eq (car enc-str) ?⿸)
861            (list ?⿸ (nth 1 enc-str)
862                  (list
863                   (cons 'ideographic-structure
864                         (setq new-str
865                               (list
866                                (cond
867                                 ((characterp (nth 2 enc-str))
868                                  (if (memq (char-ucs (nth 2 enc-str))
869                                            '(#x5F73))
870                                      ?⿰
871                                    ?⿱)
872                                  )
873                                 (t
874                                  ?⿱))
875                                (nth 2 enc-str)
876                                (nth 2 structure))))))
877            )))
878        )
879       ((eq (car structure) ?⿹)
880        (setq enc (nth 1 structure))
881        (when (setq enc-str
882                    (cond ((characterp enc)
883                           (get-char-attribute enc 'ideographic-structure)
884                           )
885                          ((consp enc)
886                           (cdr (assq 'ideographic-structure enc))
887                           )))
888          (cond
889           ((eq (car enc-str) ?⿰)
890            (list ?⿰
891                  (list (list 'ideographic-structure
892                              ?⿱
893                              (nth 1 enc-str)
894                              (nth 2 structure)))
895                  (nth 2 enc-str))
896            )))
897        )
898       ((eq (get-char-attribute (car structure) '=ucs-itaiji-001) #x2FF6)
899        (setq enc (nth 1 structure))
900        (when (setq enc-str
901                    (cond ((characterp enc)
902                           (get-char-attribute enc 'ideographic-structure)
903                           )
904                          ((consp enc)
905                           (cdr (assq 'ideographic-structure enc))
906                           )))
907          (cond
908           ((eq (car enc-str) ?⿺)
909            (list ?⿺
910                  (list (list 'ideographic-structure
911                              ?⿱
912                              (nth 2 structure)
913                              (nth 1 enc-str)))
914                  (nth 2 enc-str))
915            )
916           ((eq (car enc-str) ?⿱)
917            (list ?⿱
918                  (list (list 'ideographic-structure
919                              ?⿰
920                              (nth 2 structure)
921                              (nth 1 enc-str)))
922                  (nth 2 enc-str))
923            ))
924          )
925        )
926       ((eq (car structure) ?⿴)
927        (setq enc (nth 1 structure))
928        (when (setq enc-str
929                    (cond ((characterp enc)
930                           (get-char-attribute enc 'ideographic-structure)
931                           )
932                          ((consp enc)
933                           (cdr (assq 'ideographic-structure enc))
934                           )))
935          (cond
936           ((eq (car enc-str) ?⿱)
937            (cond
938             ((and (characterp (nth 2 enc-str))
939                   (or (memq (char-ucs (nth 2 enc-str)) '(#x56D7 #x5F51 #x897F))
940                       (eq (char-feature (nth 2 enc-str) '=>big5-cdp)
941                           #x87A5)))
942              (list ?⿱
943                    (nth 1 enc-str)
944                    (list (list 'ideographic-structure
945                                ?⿴
946                                (nth 2 enc-str)
947                                (nth 2 structure)))
948                    )
949              )
950             ((and (characterp (nth 2 enc-str))
951                   (eq (char-ucs (nth 2 enc-str)) #x51F5))
952              (list ?⿱
953                    (nth 1 enc-str)
954                    (list (list 'ideographic-structure
955                                ?⿶
956                                (nth 2 enc-str)
957                                (nth 2 structure)))
958                    )
959              )      
960             ((and (characterp (nth 1 enc-str))
961                   (eq (char-feature (nth 1 enc-str) '=>ucs@component)
962                       #x300E6))
963              (list ?⿱
964                    (list (list 'ideographic-structure
965                                ?⿵
966                                (nth 1 enc-str)
967                                (nth 2 structure)))
968                    (nth 2 enc-str))
969              )
970             (t
971              (list ?⿳
972                    (nth 1 enc-str)
973                    (nth 2 structure)
974                    (nth 2 enc-str))
975              ))
976            ))
977          )
978        )
979       ((eq (car structure) ?⿶)
980        (setq enc (nth 1 structure))
981        (when (setq enc-str
982                    (cond ((characterp enc)
983                           (get-char-attribute enc 'ideographic-structure)
984                           )
985                          ((consp enc)
986                           (cdr (assq 'ideographic-structure enc))
987                           )))
988          (cond
989           ((eq (car enc-str) ?⿱)
990            (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
991            (when (and enc2-str
992                       (eq (car enc2-str) ?⿰))
993              (list ?⿱
994                    (list (list 'ideographic-structure
995                                ?⿲
996                                (nth 1 enc2-str)
997                                (nth 2 structure)
998                                (nth 2 enc2-str)))
999                    (nth 2 enc-str)))
1000            )
1001           ((eq (car enc-str) ?⿳)
1002            (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
1003            (when (and enc2-str
1004                       (eq (car enc2-str) ?⿰))
1005              (list ?⿳
1006                    (list (list 'ideographic-structure
1007                                ?⿲
1008                                (nth 1 enc2-str)
1009                                (nth 2 structure)
1010                                (nth 2 enc2-str)))
1011                    (nth 2 enc-str)
1012                    (nth 3 enc-str)))
1013            )
1014           ((eq (car enc-str) ?⿲)
1015            (list ?⿲
1016                  (nth 1 enc-str)
1017                  (list (list 'ideographic-structure
1018                              ?⿱
1019                              (nth 2 structure)
1020                              (nth 2 enc-str)))
1021                  (nth 3 enc-str))
1022            )
1023           ((eq (car enc-str) ?⿴)
1024            (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
1025            (when (and enc2-str
1026                       (eq (car enc2-str) ?⿰))
1027              (list ?⿲
1028                    (nth 1 enc2-str)
1029                    (list (list 'ideographic-structure
1030                                ?⿱
1031                                (nth 2 structure)
1032                                (nth 2 enc-str)))
1033                    (nth 2 enc2-str)))
1034            )))
1035        )
1036       ((eq (car structure) ?⿵)
1037        (setq enc (nth 1 structure))
1038        (when (setq enc-str
1039                    (cond ((characterp enc)
1040                           (get-char-attribute enc 'ideographic-structure)
1041                           )
1042                          ((consp enc)
1043                           (cdr (assq 'ideographic-structure enc))
1044                           )))
1045          (cond
1046           ((eq (car enc-str) ?⿱)
1047            (setq enc2-str (ideographic-character-get-structure (nth 2 enc-str)))
1048            (when (and enc2-str
1049                       (eq (car enc2-str) ?⿰))
1050              (list ?⿱
1051                    (nth 1 enc-str)
1052                    (list (list 'ideographic-structure
1053                                ?⿲
1054                                (nth 1 enc2-str)
1055                                (nth 2 structure)
1056                                (nth 2 enc2-str)))))
1057            )
1058           ((eq (car enc-str) ?⿳)
1059            (setq enc2-str (ideographic-character-get-structure (nth 3 enc-str)))
1060            (when (and enc2-str
1061                       (eq (car enc2-str) ?⿰))
1062              (list ?⿳
1063                    (nth 1 enc-str)
1064                    (nth 2 enc-str)
1065                    (list (list 'ideographic-structure
1066                                ?⿲
1067                                (nth 1 enc2-str)
1068                                (nth 2 structure)
1069                                (nth 2 enc2-str)))))
1070            )
1071           ((eq (car enc-str) ?⿲)
1072            (list ?⿲
1073                  (nth 1 enc-str)
1074                  (list (list 'ideographic-structure
1075                              ?⿱
1076                              (nth 2 enc-str)
1077                              (nth 2 structure)))
1078                  (nth 3 enc-str))
1079            )
1080           ((eq (car enc-str) ?⿴)
1081            (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
1082            (when (and enc2-str
1083                       (eq (car enc2-str) ?⿰))
1084              (list ?⿲
1085                    (nth 1 enc2-str)
1086                    (list (list 'ideographic-structure
1087                                ?⿱
1088                                (nth 2 enc-str)
1089                                (nth 2 structure)))
1090                    (nth 2 enc2-str)))
1091            )))
1092        )
1093       ((eq (car structure) ?⿻)
1094        (setq enc (nth 1 structure))
1095        (when (setq enc-str
1096                    (cond ((characterp enc)
1097                           (get-char-attribute enc 'ideographic-structure)
1098                           )
1099                          ((consp enc)
1100                           (cdr (assq 'ideographic-structure enc))
1101                           )))
1102          (cond
1103           ((eq (car enc-str) ?⿱)
1104            (list ?⿳
1105                  (nth 1 enc-str)
1106                  (nth 2 structure)
1107                  (nth 2 enc-str))
1108            )))
1109        ))
1110      )))
1111
1112 ;;;###autoload
1113 (defun ideographic-structure-compact (structure)
1114   (let ((rest structure)
1115         cell
1116         ret dest sub)
1117     (while rest
1118       (setq cell (pop rest))
1119       (cond
1120        ((and (consp cell)
1121              (cond ((setq ret (assq 'ideographic-structure cell))
1122                     (setq sub (cdr ret))
1123                     )
1124                    ((atom (car cell))
1125                     (setq sub cell)
1126                     )))
1127         (setq cell
1128               (if (setq ret (ideographic-structure-find-chars sub))
1129                   (car ret)
1130                 (list (cons 'ideographic-structure sub))))
1131         ))
1132       (setq dest (cons cell dest)))
1133     (nreverse dest)))
1134
1135 (defun ideographic-structure-compare-functional-and-apparent (structure
1136                                                               &optional char)
1137   (let (enc enc-str enc2-str new-str new-str-c f-res a-res code ret)
1138     (cond
1139      ((eq (car structure) ?⿸)
1140       (setq enc (nth 1 structure))
1141       (when (setq enc-str
1142                   (cond ((characterp enc)
1143                          (get-char-attribute enc 'ideographic-structure)
1144                          )
1145                         ((consp enc)
1146                          (cdr (assq 'ideographic-structure enc))
1147                          )))
1148         (cond
1149          ((eq (car enc-str) ?⿰)
1150           (setq f-res (ids-find-chars-including-ids enc-str))
1151           (setq new-str (list ?⿱
1152                               (nth 2 enc-str)
1153                               (nth 2 structure)))
1154           (setq new-str-c
1155                 (if (setq ret (ideographic-structure-find-chars new-str))
1156                     (car ret)
1157                   (list (cons 'ideographic-structure new-str))))
1158           (setq a-res (ids-find-chars-including-ids new-str))
1159           (list enc
1160                 f-res
1161                 new-str-c
1162                 a-res
1163                 (list ?⿰ (nth 1 enc-str) new-str-c)
1164                 111)
1165           )
1166          ((and (eq (car enc-str) ?⿲)
1167                (memq (char-ucs (nth 1 enc-str)) '(#x4EBB #x2E85))
1168                (eq (nth 2 enc-str) ?丨))
1169           (setq f-res (ids-find-chars-including-ids enc-str))
1170           (setq new-str (list ?⿱
1171                               (nth 3 enc-str)
1172                               (nth 2 structure)))
1173           (setq new-str-c
1174                 (if (setq ret (ideographic-structure-find-chars new-str))
1175                     (car ret)
1176                   (list (cons 'ideographic-structure new-str))))
1177           (setq a-res (ids-find-chars-including-ids new-str))
1178           (list enc
1179                 f-res
1180                 new-str-c
1181                 a-res
1182                 (list ?⿰ (decode-char '=big5-cdp #x8B7A) new-str-c)
1183                 112)
1184           )
1185          ((eq (car enc-str) ?⿱)
1186           (setq f-res (ids-find-chars-including-ids enc-str))
1187           (setq new-str
1188                 (list
1189                  (cond
1190                   ((characterp (nth 2 enc-str))
1191                    (if (or (eq (encode-char (nth 2 enc-str) '=>ucs@component)
1192                                #x20087)
1193                            (eq (encode-char (nth 2 enc-str) '=>ucs@component)
1194                                #x5382)
1195                            (eq (encode-char (nth 2 enc-str) '=>ucs@component)
1196                                #x4E06)
1197                            (eq (encode-char (nth 2 enc-str) '=big5-cdp)
1198                                #x89CE)
1199                            (eq (encode-char (nth 2 enc-str) '=>big5-cdp)
1200                                #x88E2)
1201                            (eq (encode-char (nth 2 enc-str) '=big5-cdp)
1202                                #x88AD)
1203                            (eq (or (encode-char (nth 2 enc-str) '=>big5-cdp)
1204                                    (encode-char (nth 2 enc-str) '=big5-cdp-itaiji-001))
1205                                #x8766)
1206                            (eq (car (get-char-attribute (nth 2 enc-str)
1207                                                         'ideographic-structure))
1208                                ?⿸))
1209                        ?⿸
1210                      ?⿰))
1211                   ((eq (car (cdr (assq 'ideographic-structure (nth 2 enc-str))))
1212                        ?⿸)
1213                    ?⿸)
1214                   (t
1215                    ?⿰))
1216                  (nth 2 enc-str)
1217                  (nth 2 structure)))
1218           (setq new-str-c
1219                 (if (setq ret (ideographic-structure-find-chars new-str))
1220                     (car ret)
1221                   (list (cons 'ideographic-structure new-str))))
1222           (setq a-res (ids-find-chars-including-ids new-str))
1223           (list enc
1224                 f-res
1225                 new-str-c
1226                 a-res
1227                 (list ?⿱ (nth 1 enc-str) new-str-c)
1228                 (if (eq (car new-str) ?⿸)
1229                     121
1230                   122))
1231           )
1232          ((eq (car enc-str) ?⿸)
1233           (setq f-res (ids-find-chars-including-ids enc-str))
1234           (setq new-str (list (cond
1235                                ((characterp (nth 2 enc-str))
1236                                 (if (memq (char-ucs (nth 2 enc-str))
1237                                           '(#x5F73))
1238                                     ?⿰
1239                                   ?⿱)
1240                                 )
1241                                (t
1242                                 ?⿱))
1243                               (nth 2 enc-str)
1244                               (nth 2 structure)))
1245           (setq new-str-c
1246                 (if (setq ret (ideographic-structure-find-chars new-str))
1247                     (car ret)
1248                   (list (cons 'ideographic-structure new-str))))
1249           (setq a-res (ids-find-chars-including-ids new-str))
1250           (list enc
1251                 f-res
1252                 new-str-c
1253                 a-res
1254                 (list ?⿸ (nth 1 enc-str) new-str-c)
1255                 (if (eq (car new-str) ?⿰)
1256                     131
1257                   132))
1258           )))
1259       )
1260      ((eq (car structure) ?⿹)
1261       (setq enc (nth 1 structure))
1262       (when (setq enc-str
1263                   (cond ((characterp enc)
1264                          (get-char-attribute enc 'ideographic-structure)
1265                          )
1266                         ((consp enc)
1267                          (cdr (assq 'ideographic-structure enc))
1268                          )))
1269         (cond
1270          ((eq (car enc-str) ?⿰)
1271           (setq f-res (ids-find-chars-including-ids enc-str))
1272           (setq new-str (list ?⿱
1273                               (nth 1 enc-str)
1274                               (nth 2 structure)))
1275           (setq new-str-c
1276                 (if (setq ret (ideographic-structure-find-chars new-str))
1277                     (car ret)
1278                   (list (cons 'ideographic-structure new-str))))
1279           (setq a-res (ids-find-chars-including-ids new-str))
1280           (list enc
1281                 f-res
1282                 new-str-c
1283                 a-res
1284                 (list ?⿰ new-str-c (nth 2 enc-str))
1285                 210)
1286           )))
1287       )
1288      ((eq (get-char-attribute (car structure) '=ucs-itaiji-001) #x2FF6)
1289       (setq enc (nth 1 structure))
1290       (when (setq enc-str
1291                   (cond ((characterp enc)
1292                          (get-char-attribute enc 'ideographic-structure)
1293                          )
1294                         ((consp enc)
1295                          (cdr (assq 'ideographic-structure enc))
1296                          )))
1297         (cond
1298          ((eq (car enc-str) ?⿺)
1299           (setq f-res (ids-find-chars-including-ids enc-str))
1300           (setq new-str (list ?⿱
1301                               (nth 2 structure)
1302                               (nth 1 enc-str)))
1303           (setq new-str-c
1304                 (if (setq ret (ideographic-structure-find-chars new-str))
1305                     (car ret)
1306                   (list (cons 'ideographic-structure new-str))))
1307           (setq a-res (ids-find-chars-including-ids new-str))
1308           (list enc
1309                 f-res
1310                 new-str-c
1311                 a-res
1312                 (list ?⿺ new-str-c (nth 2 enc-str))
1313                 310)
1314           )
1315          ((eq (car enc-str) ?⿱)
1316           (setq f-res (ids-find-chars-including-ids enc-str))
1317           (setq new-str (list ?⿰
1318                               (nth 2 structure)
1319                               (nth 1 enc-str)))
1320           (setq new-str-c
1321                 (if (setq ret (ideographic-structure-find-chars new-str))
1322                     (car ret)
1323                   (list (cons 'ideographic-structure new-str))))
1324           (setq a-res (ids-find-chars-including-ids new-str))
1325           (list enc
1326                 f-res
1327                 new-str-c
1328                 a-res
1329                 (list ?⿱ new-str-c (nth 2 enc-str))
1330                 320)
1331           ))
1332         )
1333       )
1334      ((eq (car structure) ?⿴)
1335       (setq enc (nth 1 structure))
1336       (when (setq enc-str
1337                   (cond ((characterp enc)
1338                          (get-char-attribute enc 'ideographic-structure)
1339                          )
1340                         ((consp enc)
1341                          (cdr (assq 'ideographic-structure enc))
1342                          )))
1343         (cond
1344          ((eq (car enc-str) ?⿱)
1345           (cond
1346            ((and (characterp (nth 2 enc-str))
1347                  (or (memq (char-ucs (nth 2 enc-str)) '(#x56D7 #x5F51 #x897F))
1348                      (eq (char-feature (nth 2 enc-str) '=>big5-cdp)
1349                          #x87A5)))
1350             (setq f-res (ids-find-chars-including-ids enc-str))
1351             (setq new-str (list ?⿴
1352                                 (nth 2 enc-str)
1353                                 (nth 2 structure)))
1354             (setq new-str-c
1355                   (if (setq ret (ideographic-structure-find-chars new-str))
1356                       (car ret)
1357                     (list (cons 'ideographic-structure new-str))))
1358             (setq a-res (ids-find-chars-including-ids new-str))
1359             (list enc
1360                   f-res
1361                   new-str-c
1362                   a-res
1363                   (list ?⿱ (nth 1 enc-str) new-str-c)
1364                   411)
1365             )
1366            ((and (characterp (nth 2 enc-str))
1367                  (eq (char-ucs (nth 2 enc-str)) #x51F5))
1368             (setq f-res (ids-find-chars-including-ids enc-str))
1369             (setq new-str (list ?⿶
1370                                 (nth 2 enc-str)
1371                                 (nth 2 structure)))
1372             (setq new-str-c
1373                   (if (setq ret (ideographic-structure-find-chars new-str))
1374                       (car ret)
1375                     (list (cons 'ideographic-structure new-str))))
1376             (setq a-res (ids-find-chars-including-ids new-str))
1377             (list enc
1378                   f-res
1379                   new-str-c
1380                   a-res
1381                   (list ?⿱ (nth 1 enc-str) new-str-c)
1382                   412)
1383             )       
1384            ((and (characterp (nth 1 enc-str))
1385                  (eq (char-feature (nth 1 enc-str) '=>ucs@component)
1386                      #x300E6))
1387             (setq f-res (ids-find-chars-including-ids enc-str))
1388             (setq new-str (list ?⿵
1389                                 (nth 1 enc-str)
1390                                 (nth 2 structure)))
1391             (setq new-str-c
1392                   (if (setq ret (ideographic-structure-find-chars new-str))
1393                       (car ret)
1394                     (list (cons 'ideographic-structure new-str))))
1395             (setq a-res (ids-find-chars-including-ids new-str))
1396             (list enc
1397                   f-res
1398                   new-str-c
1399                   a-res
1400                   (list ?⿱ new-str-c (nth 2 enc-str))
1401                   413)
1402             )
1403            (t
1404             (setq f-res (ids-find-chars-including-ids enc-str))
1405             (list enc
1406                   f-res
1407                   new-str
1408                   nil
1409                   (list ?⿳
1410                         (nth 1 enc-str)
1411                         (nth 2 structure)
1412                         (nth 2 enc-str))
1413                   414)
1414             ))
1415           ))
1416         )
1417       )
1418      ((eq (car structure) ?⿶)
1419       (setq enc (nth 1 structure))
1420       (when (setq enc-str
1421                   (cond ((characterp enc)
1422                          (get-char-attribute enc 'ideographic-structure)
1423                          )
1424                         ((consp enc)
1425                          (cdr (assq 'ideographic-structure enc))
1426                          )))
1427         (cond
1428          ((eq (car enc-str) ?⿱)
1429           (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
1430           (when (and enc2-str
1431                      (eq (car enc2-str) ?⿰))
1432             (setq f-res (ids-find-chars-including-ids enc-str))
1433             (setq new-str (list ?⿲
1434                                 (nth 1 enc2-str)
1435                                 (nth 2 structure)
1436                                 (nth 2 enc2-str)))
1437             (setq new-str-c
1438                   (if (setq ret (ideographic-structure-find-chars new-str))
1439                       (car ret)
1440                     (list (cons 'ideographic-structure new-str))))
1441             (setq a-res (ids-find-chars-including-ids new-str))
1442             (list enc
1443                   f-res
1444                   new-str-c
1445                   a-res
1446                   (list ?⿱ new-str-c (nth 2 enc-str))
1447                   511)
1448             )
1449           )
1450          ((eq (car enc-str) ?⿳)
1451           (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
1452           (when (and enc2-str
1453                      (eq (car enc2-str) ?⿰))
1454             (setq f-res (ids-find-chars-including-ids enc-str))
1455             (setq new-str (list ?⿲
1456                                 (nth 1 enc2-str)
1457                                 (nth 2 structure)
1458                                 (nth 2 enc2-str)))
1459             (setq new-str-c
1460                   (if (setq ret (ideographic-structure-find-chars new-str))
1461                       (car ret)
1462                     (list (cons 'ideographic-structure new-str))))
1463             (setq a-res (ids-find-chars-including-ids new-str))
1464             (list enc
1465                   f-res
1466                   new-str-c
1467                   a-res
1468                   (list ?⿳ new-str-c (nth 2 enc-str) (nth 3 enc-str))
1469                   512)
1470             )
1471           )
1472          ((eq (car enc-str) ?⿲)
1473           (setq f-res (ids-find-chars-including-ids enc-str))
1474           (setq new-str (list ?⿱
1475                               (nth 2 structure)
1476                               (nth 2 enc-str)))
1477           (setq new-str-c
1478                 (if (setq ret (ideographic-structure-find-chars new-str))
1479                     (car ret)
1480                   (list (cons 'ideographic-structure new-str))))
1481           (setq a-res (ids-find-chars-including-ids new-str))
1482           (list enc
1483                 f-res
1484                 new-str-c
1485                 a-res
1486                 (list ?⿲ (nth 1 enc-str) new-str-c (nth 3 enc-str))
1487                 520)
1488           )
1489          ((eq (car enc-str) ?⿴)
1490           (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
1491           (when (and enc2-str
1492                      (eq (car enc2-str) ?⿰))
1493             (setq f-res (ids-find-chars-including-ids enc-str))
1494             (setq new-str (list ?⿱
1495                                 (nth 2 structure)
1496                                 (nth 2 enc-str)))
1497             (setq new-str-c
1498                   (if (setq ret (ideographic-structure-find-chars new-str))
1499                       (car ret)
1500                     (list (cons 'ideographic-structure new-str))))
1501             (setq a-res (ids-find-chars-including-ids new-str))
1502             (list enc
1503                   f-res
1504                   new-str-c
1505                   a-res
1506                   (list ?⿲ (nth 1 enc2-str) new-str-c (nth 2 enc2-str))
1507                   530)
1508             )
1509           )))
1510       )
1511      ((eq (car structure) ?⿵)
1512       (setq enc (nth 1 structure))
1513       (when (setq enc-str
1514                   (cond ((characterp enc)
1515                          (get-char-attribute enc 'ideographic-structure)
1516                          )
1517                         ((consp enc)
1518                          (cdr (assq 'ideographic-structure enc))
1519                          )))
1520         (cond
1521          ((eq (car enc-str) ?⿱)
1522           (setq enc2-str (ideographic-character-get-structure (nth 2 enc-str)))
1523           (when (and enc2-str
1524                      (eq (car enc2-str) ?⿰))
1525             (setq f-res (ids-find-chars-including-ids enc-str))
1526             (setq new-str (list ?⿲
1527                                 (nth 1 enc2-str)
1528                                 (nth 2 structure)
1529                                 (nth 2 enc2-str)))
1530             (setq new-str-c
1531                   (if (setq ret (ideographic-structure-find-chars new-str))
1532                       (car ret)
1533                     (list (cons 'ideographic-structure new-str))))
1534             (setq a-res (ids-find-chars-including-ids new-str))
1535             (list enc
1536                   f-res
1537                   new-str-c
1538                   a-res
1539                   (list ?⿱ (nth 1 enc-str) new-str-c)
1540                   611)
1541             )
1542           )
1543          ((eq (car enc-str) ?⿳)
1544           (setq enc2-str (ideographic-character-get-structure (nth 3 enc-str)))
1545           (when (and enc2-str
1546                      (eq (car enc2-str) ?⿰))
1547             (setq f-res (ids-find-chars-including-ids enc-str))
1548             (setq new-str (list ?⿲
1549                                 (nth 1 enc2-str)
1550                                 (nth 2 structure)
1551                                 (nth 2 enc2-str)))
1552             (setq new-str-c
1553                   (if (setq ret (ideographic-structure-find-chars new-str))
1554                       (car ret)
1555                     (list (cons 'ideographic-structure new-str))))
1556             (setq a-res (ids-find-chars-including-ids new-str))
1557             (list enc
1558                   f-res
1559                   new-str-c
1560                   a-res
1561                   (list ?⿳ (nth 1 enc-str) (nth 2 enc-str) new-str-c)
1562                   612)
1563             )
1564           )
1565          ((eq (car enc-str) ?⿲)
1566           (setq f-res (ids-find-chars-including-ids enc-str))
1567           (setq new-str (list ?⿱
1568                               (nth 2 enc-str)
1569                               (nth 2 structure)))
1570           (setq new-str-c
1571                 (if (setq ret (ideographic-structure-find-chars new-str))
1572                     (car ret)
1573                   (list (cons 'ideographic-structure new-str))))
1574           (setq a-res (ids-find-chars-including-ids new-str))
1575           (list enc
1576                 f-res
1577                 new-str-c
1578                 a-res
1579                 (list ?⿲ (nth 1 enc-str) new-str-c (nth 3 enc-str))
1580                 620)
1581           )
1582          ((eq (car enc-str) ?⿴)
1583           (setq enc2-str (ideographic-character-get-structure (nth 1 enc-str)))
1584           (when (and enc2-str
1585                      (eq (car enc2-str) ?⿰))
1586             (setq f-res (ids-find-chars-including-ids enc-str))
1587             (setq new-str (list ?⿱
1588                                 (nth 2 enc-str)
1589                                 (nth 2 structure)))
1590             (setq new-str-c
1591                   (if (setq ret (ideographic-structure-find-chars new-str))
1592                       (car ret)
1593                     (list (cons 'ideographic-structure new-str))))
1594             (setq a-res (ids-find-chars-including-ids new-str))
1595             (list enc
1596                   f-res
1597                   new-str-c
1598                   a-res
1599                   (list ?⿲ (nth 1 enc2-str) new-str-c (nth 2 enc2-str))
1600                   630)
1601             )
1602           )))
1603       )
1604      ((eq (car structure) ?⿻)
1605       (setq enc (nth 1 structure))
1606       (when (setq enc-str
1607                   (cond ((characterp enc)
1608                          (get-char-attribute enc 'ideographic-structure)
1609                          )
1610                         ((consp enc)
1611                          (cdr (assq 'ideographic-structure enc))
1612                          )))
1613         (cond
1614          ((eq (car enc-str) ?⿱)
1615           (setq f-res (ids-find-chars-including-ids enc-str))
1616           (list enc
1617                 f-res
1618                 new-str
1619                 nil
1620                 (list ?⿳
1621                       (nth 1 enc-str)
1622                       (nth 2 structure)
1623                       (nth 2 enc-str))
1624                 911)
1625           )))
1626       ))
1627     ))
1628
1629
1630 ;;; @ End.
1631 ;;;
1632
1633 (provide 'ids-find)
1634
1635 ;;; ids-find.el ends here