Initial Commit
[packages] / mule-packages / mule-ucs / lisp / unicode.el
1 ;;; unicode.el --- for UNICODE special features
2
3 ;; Copyright (C) 1997-2001 Miyashita Hisashi
4
5 ;; Keywords: mule, multilingual, 
6 ;;           character set, coding-system, ISO10646, Unicode
7
8 ;; This file is part of Mule-UCS
9
10 ;; Mule-UCS is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; Mule-UCS is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;; Comment:
26 ;;  This module supports unicode translations.
27
28 (require 'mucs)
29 (require 'trans-util)
30
31 (require 'un-data)
32
33 ;;; ucs-generic character elisp representation
34 ;;   (1) ... a simple number.
35 ;;   (2) ... a string that shows a number in c-format.
36 ;;           E.g., "0x611A"
37 ;; If any other representations are converted to elisp
38 ;; representation, normalize it to (1) form.
39 ;; Why introduce (2)?  Because there is no compatible format
40 ;; on hexadecimal literal between Emacs20 and XEmacs...x_x;
41
42 (defun ucs-generic-elisp-representation (x)
43   (if (numberp x)
44       x
45     (if (stringp x)
46         (c-notated-string-to-number x)
47       (error "Invalid ucs-generic represenation:%S" x))))
48
49 ;;;
50 ;;; UCS generic type definition.
51 ;;;
52 (mucs-define-type
53  'ucs-generic
54  (function ucs-generic-elisp-representation)
55  (function identity))
56
57 ;;;
58 ;;; translation generator
59 ;;; (work in progress...)
60
61 ;;;
62 ;;; en/decode-char backends.
63 ;;;
64
65 (defun ucs-representation-encoding-backend (char representation restriction)
66   (logior (lsh (charset-id (char-charset char)) 16)
67           (char-codepoint char))
68   (mucs-convert 'emacs-char-to-ucs-codepoint-conversion
69                 char))
70
71 (defun ucs-representation-decoding-backend (representation object restriction)
72   (if (numberp object)
73       (mucs-convert 'ucs-codepoint-to-emacs-char-conversion
74                     object)
75     (signal 'wrong-type-argument
76             (list 'numberp object))))
77
78 (mucs-register-representation-encoding-backend
79  'ucs nil
80  (function ucs-representation-encoding-backend))
81
82 (mucs-register-representation-decoding-backend
83  'ucs nil
84  (function ucs-representation-decoding-backend))
85
86 ;;;
87 ;;; Unicode or its transformation format support function.
88 ;;; (obsoleted by encode-char/decode-char)
89 ;;;
90
91 (defun ucs-to-char (codepoint)
92   "Convert Unicode codepoint to an XEmacs character.
93 CODE should be a non-negative integer.
94
95 If `codepoint' cannot be represented as an XEmacs character--that is,
96 Mule-UCS doesn't know about any registered translation from it to some
97 character in the multiple-character-set non-Unified Mule model, return nil.
98 Beware that *many* Unicode codepoints have no representation in the Mule
99 model, and since the Mule-UCS coding systems have the same backend as does
100 this function, they will tend to trash data.  "
101   (ucs-representation-decoding-backend 'ucs codepoint nil))
102
103 (defun char-to-ucs (char)
104   "Convert character to Unicode codepoint. "
105   (ucs-representation-encoding-backend char 'ucs nil))
106
107 ;;;
108 ;;; Commands for editing.
109 ;;;
110
111 (defun insert-ucs-character (codepoint)
112   "Insert character which is converted from give UCS codepoint."
113   (interactive "nUCS codepoint:")
114   (insert (or (ucs-representation-decoding-backend 'ucs codepoint nil)
115               (error "Invalid or cannot translate:U+%X"
116                      codepoint))))
117
118 (provide 'mule-ucs-unicode)
119 (provide 'unicode)
120 ;;; unicode.el ends here.
121
122