(ideographic-structure-char=): Refer `<-ideographic-component-forms'.
[chise/ids.git] / ids-find.el
1 ;;; ids-find.el --- search utility based on Ideographic-structures
2
3 ;; Copyright (C) 2002,2003 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 ideographic-structure-char= (c1 c2)
28   (or (eq c1 c2)
29       (and c1 c2
30            (let ((m1 (char-ucs c1))
31                  (m2 (char-ucs c2)))
32              (or (and m1 m2
33                       (eq m1 m2))
34                  (some (lambda (b2)
35                          (unless (characterp b2)
36                            (setq b2 (find-char b2)))
37                          (and b2
38                               (ideographic-structure-char= c1 b2)))
39                        (get-char-attribute
40                         c2 '<-ideographic-component-forms))
41                  (progn
42                    (setq m1 (car (get-char-attribute c1 '<-radical))
43                          m2 (car (get-char-attribute c2 '<-radical)))
44                    (unless (characterp m1)
45                      (setq m1 (find-char m1)))
46                    (unless (characterp m2)
47                      (setq m2 (find-char m2)))
48                    (when (or m1 m2)
49                      (ideographic-structure-char= m1 m2))))))))
50
51 (defun ideographic-structure-member-compare-components (component s-component)
52   (let (ret)
53     (cond ((char-ref= component s-component #'ideographic-structure-char=))
54           ((listp s-component)
55            (if (setq ret (assq 'ideographic-structure s-component))
56                (ideographic-structure-member component (cdr ret))))
57           ((setq ret (get-char-attribute s-component 'ideographic-structure))
58            (ideographic-structure-member component ret)))))
59
60 ;;;###autoload
61 (defun ideographic-structure-member (component structure)
62   "Return non-nil if COMPONENT is included in STRUCTURE."
63   (or (progn
64         (setq structure (cdr structure))
65         (ideographic-structure-member-compare-components
66          component (car structure)))
67       (progn
68         (setq structure (cdr structure))
69         (ideographic-structure-member-compare-components
70          component (car structure)))
71       (progn
72         (setq structure (cdr structure))
73         (and (car structure)
74              (ideographic-structure-member-compare-components
75               component (car structure))))))
76
77
78 ;;;###autoload
79 (defun ideographic-structure-repertoire-p (structure components)
80   "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
81   (and structure
82        (let (ret s-component)
83          (catch 'tag
84            (while (setq structure (cdr structure))
85              (setq s-component (car structure))
86              (unless (characterp s-component)
87                (if (setq ret (find-char s-component))
88                    (setq s-component ret)))
89              (unless (cond
90                       ((listp s-component)
91                        (if (setq ret (assq 'ideographic-structure s-component))
92                            (ideographic-structure-repertoire-p
93                             (cdr ret) components)))
94                       ((member* s-component components
95                                 :test #'ideographic-structure-char=))
96                       ((setq ret
97                              (get-char-attribute s-component
98                                                  'ideographic-structure))
99                        (ideographic-structure-repertoire-p ret components)))
100                (throw 'tag nil)))
101            t))))
102
103
104 (defvar ids-find-result-buffer "*ids-chars*")
105
106 (defun ids-find-format-line (c v)
107   (format "%c\t%s\t%s\n"
108           c
109           (or (let ((ucs (or (char-ucs c)
110                              (encode-char c 'ucs))))
111                 (if ucs
112                     (cond ((<= ucs #xFFFF)
113                            (format "    U+%04X" ucs))
114                           ((<= ucs #x10FFFF)
115                            (format "U-%08X" ucs)))))
116               "          ")
117           (or (ideographic-structure-to-ids v)
118               v)))
119
120 ;;;###autoload
121 (defun ids-find-chars-including-components (components)
122   "Search Ideographs whose structures have COMPONENTS."
123   (interactive "sComponents : ")
124   (with-current-buffer (get-buffer-create ids-find-result-buffer)
125     (setq buffer-read-only nil)
126     (erase-buffer)
127     (map-char-attribute
128      (lambda (c v)
129        (when (every (lambda (p)
130                       (ideographic-structure-member p v))
131                     components)
132          (insert (ids-find-format-line c v)))
133        nil)
134      'ideographic-structure)
135     (goto-char (point-min)))
136   (view-buffer ids-find-result-buffer))
137
138 ;;;###autoload
139 (define-obsolete-function-alias 'ideographic-structure-search-chars
140   'ids-find-chars-including-components)
141
142 ;;;###autoload
143 (defun ids-find-chars-covered-by-components (components)
144   "Search Ideographs which structures are consisted by subsets of COMPONENTS."
145   (interactive "sComponents: ")
146   (if (stringp components)
147       (setq components (string-to-char-list components)))
148   (with-current-buffer (get-buffer-create ids-find-result-buffer)
149     (setq buffer-read-only nil)
150     (erase-buffer)
151     (let (ucs jis)
152       (map-char-attribute
153        (lambda (c v)
154          (when (ideographic-structure-repertoire-p v components)
155            (insert (ids-find-format-line c v))))
156        'ideographic-structure))
157     (goto-char (point-min)))
158   (view-buffer ids-find-result-buffer))
159
160
161 ;;; @ End.
162 ;;;
163
164 (provide 'ids-find)
165
166 ;;; ids-find.el ends here