(ids-read-buffer): Read M-nnnnn', M-nnnnn" and MH-nnnn.
[chise/ids.git] / ids-read.el
1 ;;; ids-read.el --- Reader for IDS-* files
2
3 ;; Copyright (C) 2002 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)
31   (interactive "bBuffer = \nP")
32   (save-excursion
33     (set-buffer buffer)
34     (goto-char (point-min))
35     (let (line chs ids code 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           (setq char
45                 (cond
46                  ((string-match "U[-+]\\([0-9A-F]+\\)" chs)
47                   (decode-char 'ucs
48                                (string-to-int (match-string 1 chs) 16)))
49                  ((string-match "M-\\([0-9]+\\)'" chs)
50                   (setq code (string-to-int (match-string 1 chs)))
51                   (map-char-attribute
52                    (lambda (key val)
53                      (if (and (eq (car val) code)
54                               (eq (nth 1 val) 1)
55                               (null (nthcdr 2 val)))
56                          key))
57                    'morohashi-daikanwa))
58                  ((string-match "M-\\([0-9]+\\)\"" chs)
59                   (setq code (string-to-int (match-string 1 chs)))
60                   (map-char-attribute
61                    (lambda (key val)
62                      (if (and (eq (car val) code)
63                               (eq (nth 1 val) 2)
64                               (null (nthcdr 2 val)))
65                          key))
66                    'morohashi-daikanwa))
67                  ((string-match "M-\\([0-9]+\\)" chs)
68                   (decode-char 'ideograph-daikanwa
69                                (string-to-int (match-string 1 chs))))
70                  ((string-match "MH-\\([0-9]+\\)" chs)
71                   (setq code (string-to-int (match-string 1 chs)))
72                   (map-char-attribute
73                    (lambda (key val)
74                      (if (and (eq (car val) 'ho)
75                               (eq (nth 1 val) code)
76                               (null (nthcdr 2 val)))
77                          key))
78                    'morohashi-daikanwa))
79                  ((string-match "CB\\([0-9]+\\)" chs)
80                   (decode-char 'ideograph-cbeta
81                                (string-to-int (match-string 1 chs))))
82                  ))
83           (when (and char
84                      (>= (length ids) 3)
85                      (not (string-match "\\?" ids))
86                      (consp (setq structure (ids-parse-string ids simplify))))
87             (put-char-attribute char
88                                 'ideographic-structure
89                                 (cdr (car structure))))
90           )
91         (forward-line)
92         ))))
93
94 ;;;###autoload
95 (defun ids-read-file (file &optional simplify)
96   (interactive "fIDS file = \nP")
97   (with-temp-buffer
98     (insert-file-contents file)
99     (ids-read-buffer (current-buffer) simplify)))
100
101
102 ;;; @ End.
103 ;;;
104
105 (provide 'ids-read)
106
107 ;;; ids-read.el ends here