Sync up with r21-4-22-chise-0_25-cns7-completed.
[chise/xemacs-chise.git-] / 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 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
46 ;;; @ feature name
47 ;;;
48
49 ;;;###autoload
50 (defun expand-char-feature-name (feature domain)
51   (if domain
52       (intern (format "%s@%s" feature domain))
53     feature))
54
55 ;;;###autoload
56 (defun char-attribute-name< (ka kb)
57   "Return t if symbol KA is less than KB in feature-name sorting order."
58   (cond
59    ((eq '->denotational kb)
60     t)
61    ((eq '->subsumptive kb)
62     (not (eq '->denotational ka)))
63    ((eq '->denotational ka)
64     nil)
65    ((eq '->subsumptive ka)
66     nil)
67    ((and (symbolp ka)
68          (string-match "^->" (symbol-name ka)))
69     (cond ((and (symbolp kb)
70                 (string-match "^->" (symbol-name kb)))
71            (string< (symbol-name ka)
72                     (symbol-name kb))
73            ))
74     )
75    ((and (symbolp kb)
76          (string-match "^->" (symbol-name kb)))
77     t)
78    ((and (symbolp ka)
79          (string-match "^<-" (symbol-name ka)))
80     (cond ((symbolp kb)
81            (cond ((string-match "^<-" (symbol-name kb))
82                   (string< (symbol-name ka)
83                            (symbol-name kb))
84                   )
85                  ;; ((string-match "^->" (symbol-name kb))
86                  ;;  t)
87                  )))
88     )
89    ((and (symbolp kb)
90          (string-match "^<-" (symbol-name kb)))
91     t
92     ;; (not (string-match "^->" (symbol-name ka)))
93     )
94    ((find-charset ka)
95     (if (find-charset kb)
96         (let (a-ir b-ir)
97           (if (setq a-ir (charset-property ka 'iso-ir))
98               (if (setq b-ir (charset-property kb 'iso-ir))
99                   (cond
100                    ((= a-ir b-ir)
101                     (< (charset-id ka)(charset-id kb))
102                     )
103                    ((= a-ir 177)
104                     t)
105                    ((= b-ir 177)
106                     nil)
107                    ((< a-ir
108                        b-ir)
109                     ))
110                 t)
111             (if (charset-property kb 'iso-ir)
112                 nil
113               (< (charset-id ka)(charset-id kb)))))
114       nil)
115     )
116    ((find-charset kb))
117    ((symbolp ka)
118     (cond ((symbolp kb)
119            (string< (symbol-name ka)
120                     (symbol-name kb)))
121           (t)))
122    ((symbolp kb)
123     nil)))
124
125
126 ;;; @ char feature
127 ;;;
128
129 ;;;###autoload
130 (defun char-ucs (char)
131   "Return code-point of UCS."
132   (or (encode-char char '=ucs 'defined-only)
133       (char-feature char '=>ucs)))
134
135 ;;;###autoload
136 (defun char-id (char)
137   (logand (char-int char) #x3FFFFFFF))
138
139
140 ;;; @ char hierarchy
141 ;;;
142
143 ;;;###autoload
144 (defun map-char-family (function char &optional ignore-sisters)
145   (let ((rest (list char))
146         ret checked)
147     (catch 'tag
148       (while rest
149         (unless (memq (car rest) checked)
150           (if (setq ret (funcall function (car rest)))
151               (throw 'tag ret))
152           (setq checked (cons (car rest) checked)
153                 rest (append rest
154                              (get-char-attribute (car rest) '->subsumptive)
155                              (get-char-attribute (car rest) '->denotational)
156                              (get-char-attribute (car rest) '->identical)))
157           (unless ignore-sisters
158             (setq rest (append rest
159                                (get-char-attribute (car rest) '<-subsumptive)
160                                (get-char-attribute (car rest) '<-denotational)))))
161         (setq rest (cdr rest))))))
162
163
164 ;;;###autoload
165 (defun define-char-before (char-spec next-char)
166   "Define CHAR-SPEC and insert it before NEXT-CHAR." 
167   (let (mother sisters rest)
168     (when (and (or (characterp next-char)
169                    (setq next-char (find-char next-char)))
170                (setq mother (get-char-attribute next-char '<-subsumptive))
171                (setq mother (car mother))
172                (setq sisters (get-char-attribute mother '->subsumptive)))
173       (if (eq (car sisters) next-char)
174           (setq sisters (cons (define-char char-spec) sisters))
175         (setq rest sisters)
176         (while (and (cdr rest)
177                     (not (eq (nth 1 rest) next-char)))
178           (setq rest (cdr rest)))
179         (if (null rest)
180             (setq sisters (cons (define-char char-spec) sisters))
181           (setcdr rest (cons (define-char char-spec) (cdr rest)))))
182       (put-char-attribute mother '->subsumptive sisters))))
183
184 ;;;###autoload
185 (defun define-char-after (prev-char char-spec)
186   "Define CHAR-SPEC and insert it after PREV-CHAR." 
187   (let (mother sisters rest)
188     (when (and (or (characterp prev-char)
189                    (setq prev-char (find-char prev-char)))
190                (setq mother (get-char-attribute prev-char '<-subsumptive))
191                (setq mother (car mother))
192                (setq sisters (get-char-attribute mother '->subsumptive)))
193       (setq rest sisters)
194       (while (and rest
195                   (not (eq (car rest) prev-char)))
196         (setq rest (cdr rest)))
197       (if (null rest)
198           (setq sisters (cons (define-char char-spec) sisters))
199         (setcdr rest (cons (define-char char-spec) (cdr rest))))
200       (put-char-attribute mother '->subsumptive sisters))))
201
202
203 ;;; @ string
204 ;;;
205
206 ;;;###autoload
207 (defun chise-string< (string1 string2 accessors)
208   (let ((len1 (length string1))
209         (len2 (length string2))
210         len
211         (i 0)
212         c1 c2
213         rest func
214         v1 v2)
215     (setq len (min len1 len2))
216     (catch 'tag
217       (while (< i len)
218         (setq c1 (aref string1 i)
219               c2 (aref string2 i))
220         (setq rest accessors)
221         (while (and rest
222                     (setq func (car rest))
223                     (setq v1 (funcall func c1)
224                           v2 (funcall func c2))
225                     (eq v1 v2))
226           (setq rest (cdr rest)))
227         (if v1
228             (if v2
229                 (cond ((< v1 v2)
230                        (throw 'tag t))
231                       ((> v1 v2)
232                        (throw 'tag nil)))
233               (throw 'tag nil))
234           (if v2
235               (throw 'tag t)))
236         (setq i (1+ i)))
237       (< len1 len2))))
238
239
240 ;;; @ end
241 ;;;
242
243 (provide 'chise-subr)
244
245 ;;; chise-subr.el ends here