update.
[chise/ids.git] / ids.el
1 ;;; ids.el --- Parser and utility for Ideographic Description Sequence.
2
3 ;; Copyright (C) 2001,2002,2003,2005 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 (require 'ideograph-util)
33
34 (defun ideographic-structure-find-char (structure)
35   (dolist (product (char-feature (nth 1 structure) 'ideographic-products))
36     (if (equal structure
37                (char-feature product 'ideographic-structure))
38         (return product))))
39
40 (defun ids-parse-terminal (string)
41   (if (>= (length string) 1)
42       (let* ((chr (aref string 0))
43              (ucs (encode-char chr '=ucs 'defined-only))
44              big5)
45         (unless (and ucs (<= #x2FF0 ucs)(<= ucs #x2FFF))
46           (if (and ucs (<= #xE000 ucs)(<= ucs #xF8FF)
47                    (setq big5 (encode-char chr 'chinese-big5)))
48               (setq chr (decode-char '=big5-cdp big5)))
49           (cons chr
50                 (substring string 1))))))
51
52 (defun ids-parse-op-2 (string)
53   (if (>= (length string) 1)
54       (let* ((chr (aref string 0))
55              (ucs (encode-char chr '=ucs 'defined-only)))
56         (if (or (eq ucs #x2FF0)
57                 (eq ucs #x2FF1)
58                 (and (<= #x2FF4 ucs)(<= ucs #x2FFB)))
59             (cons chr
60                   (substring string 1))))))
61
62 (defun ids-parse-op-3 (string)
63   (if (>= (length string) 1)
64       (let ((chr (aref string 0)))
65         (if (memq chr '(?\u2FF2 ?\u2FF3))
66             (cons chr
67                   (substring string 1))))))
68
69 (defun ids-parse-component (string simplify)
70   (let ((ret (ids-parse-element string simplify))
71         rret)
72     (when ret
73       (if (and simplify
74                (listp (car ret))
75                (setq rret (ideographic-structure-find-char
76                            (cdr (assq 'ideographic-structure (car ret))))))
77           (cons rret (cdr ret))
78         ret))))
79
80 (defun ids-parse-element (string simplify)
81   (let (ret op arg1 arg2 arg3)
82     (cond ((ids-parse-terminal string))
83           ((setq ret (ids-parse-op-2 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                (cons (list (list 'ideographic-structure op arg1 arg2))
90                      (cdr ret)))))
91           ((setq ret (ids-parse-op-3 string))
92            (setq op (car ret))
93            (when (setq ret (ids-parse-component (cdr ret) simplify))
94              (setq arg1 (car ret))
95              (when (setq ret (ids-parse-component (cdr ret) simplify))
96                (setq arg2 (car ret))
97                (when (setq ret (ids-parse-component (cdr ret) simplify))
98                  (setq arg3 (car ret))
99                  (cons (list (list 'ideographic-structure op arg1 arg2 arg3))
100                        (cdr ret)))))))))
101
102 ;;;###autoload
103 (defun ids-parse-string (ids-string &optional simplify)
104   "Parse IDS-STRING and return the result."
105   (let ((ret (ids-parse-element ids-string simplify)))
106     (if (= (length (cdr ret)) 0)
107         (car ret))))
108
109 ;; (defun ids-format-unit (ids-char)
110 ;;   (let (ret)
111 ;;     (cond ((characterp ids-char)
112 ;;            (char-to-string ids-char))
113 ;;           ((integerp ids-char)
114 ;;            (char-to-string (decode-char 'ucs ids-char)))
115 ;;           ((setq ret (find-char ids-char))
116 ;;            (char-to-string ret))
117 ;;           ((setq ret (assq 'ideographic-structure ids-char))
118 ;;            (ids-format-list (cdr ret))))))
119
120 ;; ;;;###autoload
121 ;; (defun ids-format-list (ids-list)
122 ;;   "Format ideographic-structure IDS-LIST as an IDS-string."
123 ;;   (mapconcat (lambda (cell)
124 ;;                (ids-format-unit
125 ;;                 (if (char-ref-p cell)
126 ;;                     (plist-get cell :char)
127 ;;                   cell)))
128 ;;              ids-list ""))
129                      
130 (define-obsolete-function-alias
131   'ids-format-list 'ideographic-structure-to-ids)
132
133 ;;; @ End.
134 ;;;
135
136 (provide 'ids)
137
138 ;;; ids.el ends here