(ideographic-structure-member): Add simplified checking.
[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 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 (memq component structure)
64       (progn
65         (setq structure (cdr structure))
66         (ideographic-structure-member-compare-components
67          component (car structure)))
68       (progn
69         (setq structure (cdr structure))
70         (ideographic-structure-member-compare-components
71          component (car structure)))
72       (progn
73         (setq structure (cdr structure))
74         (and (car structure)
75              (ideographic-structure-member-compare-components
76               component (car structure))))))
77
78
79 ;;;###autoload
80 (defun ideographic-structure-repertoire-p (structure components)
81   "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
82   (and structure
83        (let (ret s-component)
84          (catch 'tag
85            (while (setq structure (cdr structure))
86              (setq s-component (car structure))
87              (unless (characterp s-component)
88                (if (setq ret (find-char s-component))
89                    (setq s-component ret)))
90              (unless (cond
91                       ((listp s-component)
92                        (if (setq ret (assq 'ideographic-structure s-component))
93                            (ideographic-structure-repertoire-p
94                             (cdr ret) components)))
95                       ((member* s-component components
96                                 :test #'ideographic-structure-char=))
97                       ((setq ret
98                              (get-char-attribute s-component
99                                                  'ideographic-structure))
100                        (ideographic-structure-repertoire-p ret components)))
101                (throw 'tag nil)))
102            t))))
103
104
105 (defvar ids-find-result-buffer "*ids-chars*")
106
107 (defun ids-find-format-line (c v)
108   (format "%c\t%s\t%s\n"
109           c
110           (or (let ((ucs (or (char-ucs c)
111                              (encode-char c 'ucs))))
112                 (if ucs
113                     (cond ((<= ucs #xFFFF)
114                            (format "    U+%04X" ucs))
115                           ((<= ucs #x10FFFF)
116                            (format "U-%08X" ucs)))))
117               "          ")
118           (or (ideographic-structure-to-ids v)
119               v)))
120
121 ;;;###autoload
122 (defun ids-find-chars-including-components (components)
123   "Search Ideographs whose structures have COMPONENTS."
124   (interactive "sComponents : ")
125   (with-current-buffer (get-buffer-create ids-find-result-buffer)
126     (setq buffer-read-only nil)
127     (erase-buffer)
128     (map-char-attribute
129      (lambda (c v)
130        (when (every (lambda (p)
131                       (ideographic-structure-member p v))
132                     components)
133          (insert (ids-find-format-line c v)))
134        nil)
135      'ideographic-structure)
136     (goto-char (point-min)))
137   (view-buffer ids-find-result-buffer))
138
139 ;;;###autoload
140 (define-obsolete-function-alias 'ideographic-structure-search-chars
141   'ids-find-chars-including-components)
142
143 ;;;###autoload
144 (defun ids-find-chars-covered-by-components (components)
145   "Search Ideographs which structures are consisted by subsets of COMPONENTS."
146   (interactive "sComponents: ")
147   (if (stringp components)
148       (setq components (string-to-char-list components)))
149   (with-current-buffer (get-buffer-create ids-find-result-buffer)
150     (setq buffer-read-only nil)
151     (erase-buffer)
152     (let (ucs jis)
153       (map-char-attribute
154        (lambda (c v)
155          (when (ideographic-structure-repertoire-p v components)
156            (insert (ids-find-format-line c v))))
157        'ideographic-structure))
158     (goto-char (point-min)))
159   (view-buffer ids-find-result-buffer))
160
161
162 ;;; @ End.
163 ;;;
164
165 (provide 'ids-find)
166
167 ;;; ids-find.el ends here