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