update.
[chise/ids.git] / ids-read.el
1 ;;; ids-read.el --- Reader for IDS-* files
2
3 ;; Copyright (C) 2002, 2003, 2004, 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 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 ;;; Code:
26
27 (require 'ids)
28
29 ;;;###autoload
30 (defun ids-read-buffer (buffer &optional simplify soft)
31   (interactive "bBuffer = \nP")
32   (save-excursion
33     (set-buffer buffer)
34     (goto-char (point-min))
35     (let (line chs ids apparent-ids code char u-char structure)
36       (while (not (eobp))
37         (unless (looking-at ";")
38           (setq line
39                 (split-string
40                  (buffer-substring (point-at-bol)(point-at-eol))
41                  "\t"))
42           (setq chs (car line)
43                 ids (nth 2 line)
44                 apparent-ids (nth 3 line)
45                 u-char nil)
46           (setq apparent-ids
47                 (if (and apparent-ids
48                          (string-match "^@apparent=" apparent-ids))
49                     (substring apparent-ids (match-end 0))))
50           (setq char
51                 (cond
52                  ((string-match "U[-+]\\([0-9A-F]+\\)" chs)
53                   (setq code (string-to-int (match-string 1 chs) 16))
54                   (setq u-char (decode-char '=ucs@unicode code))
55                   (decode-char 'ucs code))
56                  ((string-match "J90-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\)"
57                                 chs)
58                   (decode-char 'japanese-jisx0208-1990
59                                (string-to-int (match-string 1 chs) 16)))
60                  ((string-match
61                    "C\\([1-7]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\)"
62                    chs)
63                   (decode-char
64                    (intern
65                     (concat "chinese-cns11643-" (match-string 1 chs)))
66                    (string-to-int (match-string 2 chs) 16)))
67                  ((string-match "CDP-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\)"
68                                 chs)
69                   (decode-char '=big5-cdp
70                                (string-to-int (match-string 1 chs) 16)))
71                  ((string-match
72                    "HZK\\([0-9][0-9]\\)-\\([0-9A-F][0-9A-F][0-9A-F][0-9A-F]\\)"
73                    chs)
74                   (decode-char (intern
75                                 (format "=hanziku-%d"
76                                         (string-to-int (match-string 1 chs))))
77                                (string-to-int (match-string 2 chs) 16)))
78                  ((string-match "M-\\([0-9]+\\)'" chs)
79                   (setq code (string-to-int (match-string 1 chs)))
80                   (map-char-attribute
81                    (lambda (key val)
82                      (if (and (eq (car val) code)
83                               (eq (nth 1 val) 1)
84                               (null (nthcdr 2 val)))
85                          key))
86                    'morohashi-daikanwa))
87                  ((string-match "M-\\([0-9]+\\)\"" chs)
88                   (setq code (string-to-int (match-string 1 chs)))
89                   (map-char-attribute
90                    (lambda (key val)
91                      (if (and (eq (car val) code)
92                               (eq (nth 1 val) 2)
93                               (null (nthcdr 2 val)))
94                          key))
95                    'morohashi-daikanwa))
96                  ((string-match "M-\\([0-9]+\\)" chs)
97                   (decode-char 'ideograph-daikanwa
98                                (string-to-int (match-string 1 chs))))
99                  ((string-match "MH-\\([0-9]+\\)" chs)
100                   (setq code (string-to-int (match-string 1 chs)))
101                   (map-char-attribute
102                    (lambda (key val)
103                      (if (and (eq (car val) 'ho)
104                               (eq (nth 1 val) code)
105                               (null (nthcdr 2 val)))
106                          key))
107                    'morohashi-daikanwa))
108                  ((string-match "CB\\([0-9]+\\)" chs)
109                   (decode-char 'ideograph-cbeta
110                                (string-to-int (match-string 1 chs))))
111                  ))
112           (when char
113             (when (and (>= (length ids) 3)
114                        (not (string-match "\\?" ids))
115                        (consp (setq structure (ids-parse-string ids simplify))))
116               (when (or (not soft)
117                         (null
118                          (get-char-attribute char 'ideographic-structure)))
119                 (put-char-attribute char
120                                     'ideographic-structure
121                                     (cdr (car structure))))
122               (when (and u-char
123                          (not (eq char u-char))
124                          (or (not soft)
125                              (null
126                               (get-char-attribute
127                                u-char 'ideographic-structure))))
128                 (put-char-attribute
129                  u-char 'ideographic-structure
130                  (ideographic-structure-convert-to-domain
131                   (cdr (car structure)) 'unicode))))
132             (when (and (>= (length apparent-ids) 3)
133                        (consp (setq structure
134                                     (ids-parse-string apparent-ids simplify))))
135               (when (or (not soft)
136                         (null
137                          (get-char-attribute char 'ideographic-structure@apparent)))
138                 (put-char-attribute char
139                                     'ideographic-structure@apparent
140                                     (cdr (car structure)))))
141             )
142           )
143         (forward-line)
144         ))))
145
146 ;;;###autoload
147 (defun ids-read-file (file &optional simplify soft)
148   (interactive "fIDS file = \nP")
149   (with-temp-buffer
150     (insert-file-contents file)
151     (ids-read-buffer (current-buffer) simplify soft)))
152
153
154 ;;; @ End.
155 ;;;
156
157 (provide 'ids-read)
158
159 ;;; ids-read.el ends here