Fix <torkel@hpc2n.umu.se>'s name.
[chise/xemacs-chise.git-] / src / character.h
1 /* Header for character representation.
2    Copyright (C) 1999,2000 MORIOKA Tomohiko
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #ifndef _XEMACS_CHARACTER_H
22 #define _XEMACS_CHARACTER_H
23
24 #if !defined(MULE) /* unibyte representation */
25 # include "char-1byte.h"
26 #elif !defined(CHAR_IS_UCS4) /* leading-byte representation */
27 # include "char-lb.h"
28 #else /* CHAR_IS_UCS4 */
29 # include "char-ucs.h"
30 #endif /* CHAR_IS_UCS4 */
31
32 /********************************/
33 /*                              */
34 /*   Interface for characters   */
35 /*                              */
36 /********************************/
37 /*
38    valid_char_p (ch):
39         Return whether the given Emchar is valid.
40
41    CHARP (ch):
42         Return whether the given Lisp_Object is a character.
43
44    CHECK_CHAR_COERCE_INT (ch):
45         Signal an error if CH is not a valid character or integer
46         Lisp_Object.
47         If CH is an integer Lisp_Object, convert it to a character
48         Lisp_Object, but merely by repackaging, without performing
49         tests for char validity.
50
51 Functions/macros when MULE is defined:
52
53    CHAR_ASCII_P (ch):
54         Return whether the given Emchar is ASCII.
55
56    MAKE_CHAR (CHARSET, B1, B2):
57         Return a character whose coded-charset is CHARSET and
58         position-codes are B1 and B2.  1 byte character ignores B2.
59
60    BREAKUP_CHAR (ch, charset, B1, B2):
61         Break up the given Emchar, and store found coded-charset and
62         position-codes to CHARSET, B1 and B2.
63
64    CHAR_CHARSET (CH):
65         Return coded-charset object of Emchar CH.
66
67    CHAR_LEADING_BYTE (CH):
68         Return Charset-ID of Emchar CH.
69 */
70
71 #define CHAR_INTP(x) (INTP (x) && valid_char_p (XINT (x)))
72
73 #define CHAR_OR_CHAR_INTP(x) (CHARP (x) || CHAR_INTP (x))
74
75 #ifdef ERROR_CHECK_TYPECHECK
76
77 INLINE_HEADER Emchar XCHAR_OR_CHAR_INT (Lisp_Object obj);
78 INLINE_HEADER Emchar
79 XCHAR_OR_CHAR_INT (Lisp_Object obj)
80 {
81   assert (CHAR_OR_CHAR_INTP (obj));
82   return CHARP (obj) ? XCHAR (obj) : XINT (obj);
83 }
84
85 #else
86
87 #define XCHAR_OR_CHAR_INT(obj) (CHARP ((obj)) ? XCHAR ((obj)) : XINT ((obj)))
88
89 #endif
90
91 #define CHECK_CHAR_COERCE_INT(x) do {           \
92   if (CHARP (x))                                \
93      ;                                          \
94   else if (CHAR_INTP (x))                       \
95     x = make_char (XINT (x));                   \
96   else                                          \
97     x = wrong_type_argument (Qcharacterp, x);   \
98 } while (0)
99
100
101 #define CHARC_CHARSET(cc)       ((cc).charset)
102 #define CHARC_CHARSET_ID(cc)    XCHARSET_ID (CHARC_CHARSET (cc))
103 #define CHARC_CODE_POINT(cc)    ((cc).code_point)
104 #define CHARC_COLUMNS(cc)       CHARSET_COLUMNS (XCHARSET (CHARC_CHARSET (cc)))
105
106 INLINE_HEADER Emchar CHARC_TO_CHAR (Charc cc);
107 INLINE_HEADER Emchar
108 CHARC_TO_CHAR (Charc cc)
109 {
110   return DECODE_CHAR (cc.charset, cc.code_point);
111 }
112
113 INLINE_HEADER int CHARC_EQ (Charc cc1, Charc cc2);
114 INLINE_HEADER int
115 CHARC_EQ (Charc cc1, Charc cc2)
116 {
117   return EQ (cc1.charset, cc2.charset) && (cc1.code_point == cc2.code_point);
118 }
119
120 INLINE_HEADER int CHARC_ASCII_EQ (Charc cc, int ch);
121 INLINE_HEADER int
122 CHARC_ASCII_EQ (Charc cc, int ch)
123 {
124   return EQ (cc.charset, Vcharset_ascii) && (cc.code_point == ch);
125 }
126
127 INLINE_HEADER int CHARC_IS_SPACE (Charc cc);
128 INLINE_HEADER int
129 CHARC_IS_SPACE (Charc cc)
130 {
131   return (EQ (cc.charset, Vcharset_ascii) ||
132           EQ (cc.charset, Vcharset_control_1) ||
133           EQ (cc.charset, Vcharset_latin_iso8859_1))
134     && isspace (cc.code_point);
135 }
136
137 INLINE_HEADER Charc ASCII_TO_CHARC (int ch);
138 INLINE_HEADER Charc
139 ASCII_TO_CHARC (int ch)
140 {
141   Charc cc;
142
143   cc.charset = Vcharset_ascii;
144   cc.code_point = ch;
145   return cc;
146 }
147
148
149 typedef struct
150 {
151   Dynarr_declare (Charc);
152 } Charc_dynarr;
153
154 #endif /* _XEMACS_CHARACTER_H */