(ideographic-structure-member): Add autoload cookie and DOC-string.
[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-parts (part s-part)
45   (let (ret)
46     (cond ((char-ref= part s-part #'ideographic-structure-char=))
47           ((listp s-part)
48            (if (setq ret (assq 'ideographic-structure s-part))
49                (ideographic-structure-member part (cdr ret))))
50           ((setq ret (get-char-attribute s-part 'ideographic-structure))
51            (ideographic-structure-member part ret)))))
52
53 ;;;###autoload
54 (defun ideographic-structure-member (part structure)
55   "Return non-nil if PART is included in STRUCTURE."
56   (or (progn
57         (setq structure (cdr structure))
58         (ideographic-structure-member-compare-parts part (car structure)))
59       (progn
60         (setq structure (cdr structure))
61         (ideographic-structure-member-compare-parts part (car structure)))
62       (progn
63         (setq structure (cdr structure))
64         (and (car structure)
65              (ideographic-structure-member-compare-parts
66               part (car structure))))))
67
68
69 ;;;###autoload
70 (defun ideographic-structure-repertoire-p (structure parts)
71   "Return non-nil if STRUCTURE can be constructed by a subset of PARTS."
72   (and structure
73        (let (ret s-part)
74          (catch 'tag
75            (while (setq structure (cdr structure))
76              (setq s-part (car structure))
77              (unless (characterp s-part)
78                (if (setq ret (find-char s-part))
79                    (setq s-part ret)))
80              (unless (cond
81                       ((listp s-part)
82                        (if (setq ret (assq 'ideographic-structure s-part))
83                            (ideographic-structure-repertoire-p
84                             (cdr ret) parts)))
85                       ((member* s-part parts
86                                 :test #'ideographic-structure-char=))
87                       ((setq ret
88                              (get-char-attribute s-part
89                                                  'ideographic-structure))
90                        (ideographic-structure-repertoire-p ret parts)))
91                (throw 'tag nil)))
92            t))))
93
94 ;;;###autoload
95 (defun ideographic-structure-search-chars (parts)
96   "Search Ideographs by PARTS."
97   (interactive "sParts : ")
98   (with-current-buffer (get-buffer-create " *ids-chars*")
99     (setq buffer-read-only nil)
100     (erase-buffer)
101     (map-char-attribute
102      (lambda (c v)
103        (when (every
104               (lambda (p)
105                 ;; (member* p v :test #'char-ref=)
106                 (ideographic-structure-member p v))
107               parts)
108          (insert (format "%c\t%s\n"
109                          c
110                          (or (ideographic-structure-to-ids v)
111                              v))))
112        nil)
113      'ideographic-structure)
114     (goto-char (point-min)))
115   (view-buffer " *ids-chars*"))
116
117
118 ;;; @ End.
119 ;;;
120
121 (provide 'ids-find)
122
123 ;;; ids-find.el ends here