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