- Add `=decomposition@hanyo-denshi' for representative (detailed)
[chise/xemacs-chise.git.1] / lisp / utf-2000 / chise-subr.el
1 ;;; chise-subr.el --- basic lisp subroutines for XEmacs CHISE
2
3 ;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
4 ;;   2010, 2011, 2012, 2013 MORIOKA Tomohiko.
5
6 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Keywords: CHISE, Character Database, ISO/IEC 10646, UCS, Unicode, MULE.
8
9 ;; This file is part of XEmacs CHISE.
10
11 ;; XEmacs CHISE is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; XEmacs CHISE is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs CHISE; see the file COPYING.  If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (defvar char-db-feature-domains
29   ;; (let (dest str len ret domain)
30   ;;   (dolist (fn (char-attribute-list))
31   ;;     (setq str (symbol-name fn))
32   ;;     (when (string-match "^ideographic-radical@\\([^*]+\\)$" str)
33   ;;       (setq domain (substring str (match-beginning 1)))
34   ;;       (when (> (setq len (length domain)) 0)
35   ;;         (setq ret (read-from-string domain))
36   ;;         (when (= (cdr ret) len)
37   ;;           (setq domain (car ret))
38   ;;           (unless (memq domain dest)
39   ;;             (push domain dest))))))
40   ;;   (sort dest #'string<))
41   '(ucs ucs/compat daikanwa cns gt jis jis/alt jis/a jis/b
42         jis-x0212 jis-x0213 cdp shinjigen
43         r030 r140 misc unknown))
44
45 (defconst charset-id-=adobe-japan1-0 (charset-id '=adobe-japan1-0))
46 (defconst charset-id-=adobe-japan1-6 (charset-id '=adobe-japan1-6))
47 (defconst charset-id-==adobe-japan1-0 (charset-id '==adobe-japan1-0))
48 (defconst charset-id-==adobe-japan1-6 (charset-id '==adobe-japan1-6))
49 ;; (defconst charset-id-=>>>adobe-japan1-0 (charset-id '=>>>adobe-japan1-0))
50 ;; (defconst charset-id-=>>>adobe-japan1-6 (charset-id '=>>>adobe-japan1-6))
51 (defconst charset-id-=>>adobe-japan1-0 (charset-id '=>>adobe-japan1-0))
52 (defconst charset-id-=>>adobe-japan1-6 (charset-id '=>>adobe-japan1-6))
53
54 (defun charset-id-adobe-japan1-p (id)
55   (or (and (<= charset-id-=adobe-japan1-0 id)
56            (<= id charset-id-=adobe-japan1-6))
57       (and (<= charset-id-==adobe-japan1-0 id)
58            (<= id charset-id-==adobe-japan1-6))
59       ;; (and (<= charset-id-=>>>adobe-japan1-0 id)
60       ;;      (<= id charset-id-=>>>adobe-japan1-6))
61       (and (<= charset-id-=>>adobe-japan1-0 id)
62            (<= id charset-id-=>>adobe-japan1-6))
63       ))
64
65
66 ;;; @ feature name
67 ;;;
68
69 ;;;###autoload
70 (defun expand-char-feature-name (feature domain)
71   (if domain
72       (intern (format "%s@%s" feature domain))
73     feature))
74
75 ;;;###autoload
76 (defun char-attribute-name< (ka kb)
77   "Return t if symbol KA is less than KB in feature-name sorting order."
78   (cond
79    ((and (symbolp ka)
80          (eq (aref (symbol-name ka) 0) ?*))
81     (cond ((and (symbolp kb)
82                 (eq (aref (symbol-name kb) 0) ?*))
83            (string< (symbol-name ka)
84                     (symbol-name kb))
85            ))
86     )
87    ((and (symbolp kb)
88          (eq (aref (symbol-name kb) 0) ?*))
89     t)
90    ((eq '->denotational kb)
91     t)
92    ((eq '->subsumptive kb)
93     (not (eq '->denotational ka)))
94    ((eq '->denotational ka)
95     nil)
96    ((eq '->subsumptive ka)
97     nil)
98    ((and (symbolp ka)
99          (string-match "^->" (symbol-name ka)))
100     (cond ((and (symbolp kb)
101                 (string-match "^->" (symbol-name kb)))
102            (string< (symbol-name ka)
103                     (symbol-name kb))
104            ))
105     )
106    ((and (symbolp kb)
107          (string-match "^->" (symbol-name kb)))
108     t)
109    ((and (symbolp ka)
110          (string-match "^<-" (symbol-name ka)))
111     (cond ((symbolp kb)
112            (cond ((string-match "^<-" (symbol-name kb))
113                   (string< (symbol-name ka)
114                            (symbol-name kb))
115                   )
116                  ;; ((string-match "^->" (symbol-name kb))
117                  ;;  t)
118                  )))
119     )
120    ((and (symbolp kb)
121          (string-match "^<-" (symbol-name kb)))
122     t
123     ;; (not (string-match "^->" (symbol-name ka)))
124     )
125    ((find-charset ka)
126     (if (find-charset kb)
127         (let (a-ir b-ir a-id b-id)
128           (if (setq a-ir (charset-property ka 'iso-ir))
129               (if (setq b-ir (charset-property kb 'iso-ir))
130                   (cond
131                    ((= a-ir b-ir)
132                     (< (charset-id ka)(charset-id kb))
133                     )
134                    ((= a-ir 177)
135                     t)
136                    ((= b-ir 177)
137                     nil)
138                    ((< a-ir
139                        b-ir)
140                     ))
141                 (cond
142                  ((= a-ir 177)
143                   t)
144                  ((and (setq b-id (charset-id kb))
145                        (charset-id-adobe-japan1-p b-id))
146                   nil)
147                  (t)))
148             (if (setq b-ir (charset-property kb 'iso-ir))
149                 (cond
150                  ((= b-ir 177)
151                   nil)
152                  ((and (setq a-id (charset-id ka))
153                        (charset-id-adobe-japan1-p a-id))
154                   t)
155                  (t nil))
156               (cond
157                ((and (setq a-id (charset-id ka))
158                      (charset-id-adobe-japan1-p a-id))
159                 (if (and (setq b-id (charset-id kb))
160                          (charset-id-adobe-japan1-p b-id))
161                     (< a-id b-id)
162                   t))
163                ((and (setq b-id (charset-id kb))
164                      (charset-id-adobe-japan1-p b-id))
165                 nil)
166                (t
167                 (< (charset-id ka)(charset-id kb))
168                 )))))
169       nil)
170     )
171    ((find-charset kb))
172    ((symbolp ka)
173     (cond ((symbolp kb)
174            (string< (symbol-name ka)
175                     (symbol-name kb)))
176           (t)))
177    ((symbolp kb)
178     nil)))
179
180
181 ;;; @ char feature
182 ;;;
183
184 ;;;###autoload
185 (defun char-ucs (char)
186   "Return code-point of UCS."
187   (or (encode-char char '=ucs 'defined-only)
188       (char-feature char '=>ucs)))
189
190 ;;;###autoload
191 (defun char-id (char)
192   (logand (char-int char) #x3FFFFFFF))
193
194
195 ;;; @ char hierarchy
196 ;;;
197
198 ;;;###autoload
199 (defun map-char-family (function char &optional ignore-sisters)
200   (let ((rest (list char))
201         ret checked)
202     (catch 'tag
203       (while rest
204         (unless (memq (car rest) checked)
205           (if (setq ret (funcall function (car rest)))
206               (throw 'tag ret))
207           (setq checked (cons (car rest) checked)
208                 rest (append rest
209                              (get-char-attribute (car rest) '->subsumptive)
210                              (get-char-attribute (car rest) '->denotational)
211                              (get-char-attribute (car rest) '->identical)))
212           (unless ignore-sisters
213             (setq rest (append rest
214                                (get-char-attribute (car rest) '<-subsumptive)
215                                (get-char-attribute (car rest) '<-denotational)))))
216         (setq rest (cdr rest))))))
217
218
219 ;;;###autoload
220 (defun define-char-before (char-spec next-char)
221   "Define CHAR-SPEC and insert it before NEXT-CHAR." 
222   (let (mother sisters rest)
223     (when (and (or (characterp next-char)
224                    (setq next-char (find-char next-char)))
225                (setq mother (get-char-attribute next-char '<-subsumptive))
226                (setq mother (car mother))
227                (setq sisters (get-char-attribute mother '->subsumptive)))
228       (if (eq (car sisters) next-char)
229           (setq sisters (cons (define-char char-spec) sisters))
230         (setq rest sisters)
231         (while (and (cdr rest)
232                     (not (eq (nth 1 rest) next-char)))
233           (setq rest (cdr rest)))
234         (if (null rest)
235             (setq sisters (cons (define-char char-spec) sisters))
236           (setcdr rest (cons (define-char char-spec) (cdr rest)))))
237       (put-char-attribute mother '->subsumptive sisters))))
238
239 ;;;###autoload
240 (defun define-char-after (prev-char char-spec)
241   "Define CHAR-SPEC and insert it after PREV-CHAR." 
242   (let (mother sisters rest)
243     (when (and (or (characterp prev-char)
244                    (setq prev-char (find-char prev-char)))
245                (setq mother (get-char-attribute prev-char '<-subsumptive))
246                (setq mother (car mother))
247                (setq sisters (get-char-attribute mother '->subsumptive)))
248       (setq rest sisters)
249       (while (and rest
250                   (not (eq (car rest) prev-char)))
251         (setq rest (cdr rest)))
252       (if (null rest)
253           (setq sisters (cons (define-char char-spec) sisters))
254         (setcdr rest (cons (define-char char-spec) (cdr rest))))
255       (put-char-attribute mother '->subsumptive sisters))))
256
257
258 ;;; @ string
259 ;;;
260
261 ;;;###autoload
262 (defun chise-string< (string1 string2 accessors)
263   (let ((len1 (length string1))
264         (len2 (length string2))
265         len
266         (i 0)
267         c1 c2
268         rest func
269         v1 v2)
270     (setq len (min len1 len2))
271     (catch 'tag
272       (while (< i len)
273         (setq c1 (aref string1 i)
274               c2 (aref string2 i))
275         (setq rest accessors)
276         (while (and rest
277                     (setq func (car rest))
278                     (setq v1 (funcall func c1)
279                           v2 (funcall func c2))
280                     (eq v1 v2))
281           (setq rest (cdr rest)))
282         (if v1
283             (if v2
284                 (cond ((< v1 v2)
285                        (throw 'tag t))
286                       ((> v1 v2)
287                        (throw 'tag nil)))
288               (throw 'tag nil))
289           (if v2
290               (throw 'tag t)))
291         (setq i (1+ i)))
292       (< len1 len2))))
293
294
295 ;;; @ end
296 ;;;
297
298 (provide 'chise-subr)
299
300 ;;; chise-subr.el ends here