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