(U-00021430): Fixed.
[chise/ids.git] / ids.el
1 ;;; ids.el --- Parser and utility for Ideographic Description Sequence.
2
3 ;; Copyright (C) 2001,2002,2003 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: IDS, IDC, Ideographs, UCS, Unicode
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 ;;; Commentary:
26
27 ;; Ideographic Description Sequence (IDS) is defined in ISO/IEC
28 ;; 10646-1:2000 Annex F.
29
30 ;;; Code:
31
32 (defun ids-parse-terminal (string)
33   (if (>= (length string) 1)
34       (let* ((chr (aref string 0))
35              (ucs (encode-char chr '=ucs 'defined-only))
36              big5)
37         (unless (and ucs (<= #x2FF0 ucs)(<= ucs #x2FFF))
38           (if (and ucs (<= #xE000 ucs)(<= ucs #xF8FF)
39                    (setq big5 (encode-char chr 'chinese-big5)))
40               (setq chr (decode-char '=big5-cdp big5)))
41           (cons chr
42                 (substring string 1))))))
43
44 (defun ids-parse-op-2 (string)
45   (if (>= (length string) 1)
46       (let* ((chr (aref string 0))
47              (ucs (encode-char chr '=ucs 'defined-only)))
48         (if (or (eq ucs #x2FF0)
49                 (eq ucs #x2FF1)
50                 (and (<= #x2FF4 ucs)(<= ucs #x2FFB)))
51             (cons chr
52                   (substring string 1))))))
53
54 (defun ids-parse-op-3 (string)
55   (if (>= (length string) 1)
56       (let ((chr (aref string 0)))
57         (if (memq chr '(?\u2FF2 ?\u2FF3))
58             (cons chr
59                   (substring string 1))))))
60
61 (defun ids-parse-component (string simplify)
62   (let ((ret (ids-parse-element string simplify))
63         rret)
64     (when ret
65       (if (and simplify
66                (listp (car ret))
67                (setq rret (ideographic-structure-find-char
68                            (cdr (assq 'ideographic-structure (car ret))))))
69           (cons rret (cdr ret))
70         ret))))
71
72 (defun ids-parse-element (string simplify)
73   (let (ret op arg1 arg2 arg3)
74     (cond ((ids-parse-terminal string))
75           ((setq ret (ids-parse-op-2 string))
76            (setq op (car ret))
77            (when (setq ret (ids-parse-component (cdr ret) simplify))
78              (setq arg1 (car ret))
79              (when (setq ret (ids-parse-component (cdr ret) simplify))
80                (setq arg2 (car ret))
81                (cons (list (list 'ideographic-structure op arg1 arg2))
82                      (cdr ret)))))
83           ((setq ret (ids-parse-op-3 string))
84            (setq op (car ret))
85            (when (setq ret (ids-parse-component (cdr ret) simplify))
86              (setq arg1 (car ret))
87              (when (setq ret (ids-parse-component (cdr ret) simplify))
88                (setq arg2 (car ret))
89                (when (setq ret (ids-parse-component (cdr ret) simplify))
90                  (setq arg3 (car ret))
91                  (cons (list (list 'ideographic-structure op arg1 arg2 arg3))
92                        (cdr ret)))))))))
93
94 ;;;###autoload
95 (defun ids-parse-string (ids-string &optional simplify)
96   "Parse IDS-STRING and return the result."
97   (let ((ret (ids-parse-element ids-string simplify)))
98     (if (= (length (cdr ret)) 0)
99         (car ret))))
100
101 ;; (defun ids-format-unit (ids-char)
102 ;;   (let (ret)
103 ;;     (cond ((characterp ids-char)
104 ;;            (char-to-string ids-char))
105 ;;           ((integerp ids-char)
106 ;;            (char-to-string (decode-char 'ucs ids-char)))
107 ;;           ((setq ret (find-char ids-char))
108 ;;            (char-to-string ret))
109 ;;           ((setq ret (assq 'ideographic-structure ids-char))
110 ;;            (ids-format-list (cdr ret))))))
111
112 ;; ;;;###autoload
113 ;; (defun ids-format-list (ids-list)
114 ;;   "Format ideographic-structure IDS-LIST as an IDS-string."
115 ;;   (mapconcat (lambda (cell)
116 ;;                (ids-format-unit
117 ;;                 (if (char-ref-p cell)
118 ;;                     (plist-get cell :char)
119 ;;                   cell)))
120 ;;              ids-list ""))
121                      
122 (define-obsolete-function-alias
123   'ids-format-list 'ideographic-structure-to-ids)
124
125 ;;; @ End.
126 ;;;
127
128 (provide 'ids)
129
130 ;;; ids.el ends here