charclass.c revision d522f475
1d522f475Smrg/* $XTermId: charclass.c,v 1.20 2008/01/31 01:07:23 tom Exp $ */
2d522f475Smrg
3d522f475Smrg/*
4d522f475Smrg * Compact and efficient reimplementation of the
5d522f475Smrg * xterm character class mechanism for large character sets
6d522f475Smrg *
7d522f475Smrg * Markus Kuhn -- mkuhn@acm.org -- 2000-07-03
8d522f475Smrg *
9d522f475Smrg * Xterm allows users to select entire words with a double-click on the left
10d522f475Smrg * mouse button.  Opinions might differ on what type of characters are part of
11d522f475Smrg * separate words, therefore xterm allows users to configure a class code for
12d522f475Smrg * each 8-bit character.  Words are maximum length sequences of neighboring
13d522f475Smrg * characters with identical class code.  Extending this mechanism to Unicode
14d522f475Smrg * naively would create an at least 2^16 entries (128 kB) long class code
15d522f475Smrg * table.
16d522f475Smrg *
17d522f475Smrg * Instead, we transform the character class table into a list of intervals,
18d522f475Smrg * that will be accessed via a linear search.  Changes made to the table by the
19d522f475Smrg * user will be appended.  A special class code IDENT (default) marks
20d522f475Smrg * characters who have their code number as the class code.
21d522f475Smrg *
22d522f475Smrg * We could alternatively use a sorted table of non-overlapping intervals that
23d522f475Smrg * can be accessed via binary search, but merging in new intervals is
24d522f475Smrg * significantly more hassle and not worth the effort here.
25d522f475Smrg */
26d522f475Smrg
27d522f475Smrg#include <xterm.h>
28d522f475Smrg#include <charclass.h>
29d522f475Smrg
30d522f475Smrg#if OPT_WIDE_CHARS
31d522f475Smrg
32d522f475Smrgstatic struct classentry {
33d522f475Smrg    int cclass;
34d522f475Smrg    int first;
35d522f475Smrg    int last;
36d522f475Smrg} *classtab;
37d522f475Smrg
38d522f475Smrg/*
39d522f475Smrg * Special convention for classtab[0]:
40d522f475Smrg * - classtab[0].cclass is the allocated number of entries in classtab
41d522f475Smrg * - classtab[0].first = 1 (first used entry in classtab)
42d522f475Smrg * - classtab[0].last is the last used entry in classtab
43d522f475Smrg */
44d522f475Smrg
45d522f475Smrgint
46d522f475SmrgSetCharacterClassRange(int low, int high, int value)
47d522f475Smrg{
48d522f475Smrg    if (high < low)
49d522f475Smrg	return -1;		/* nothing to do */
50d522f475Smrg
51d522f475Smrg    /* make sure we have at least one free entry left at table end */
52d522f475Smrg    if (classtab[0].last > classtab[0].cclass - 2) {
53d522f475Smrg	classtab[0].cclass += 5 + classtab[0].cclass / 4;
54d522f475Smrg	classtab = TypeRealloc(struct classentry, classtab[0].cclass, classtab);
55d522f475Smrg	if (!classtab)
56d522f475Smrg	    abort();
57d522f475Smrg    }
58d522f475Smrg
59d522f475Smrg    /* simply append new interval to end of interval array */
60d522f475Smrg    classtab[0].last++;
61d522f475Smrg    classtab[classtab[0].last].first = low;
62d522f475Smrg    classtab[classtab[0].last].last = high;
63d522f475Smrg    classtab[classtab[0].last].cclass = value;
64d522f475Smrg
65d522f475Smrg    return 0;
66d522f475Smrg}
67d522f475Smrg
68d522f475Smrgtypedef enum {
69d522f475Smrg    IDENT = -1,
70d522f475Smrg    ALNUM = 48,
71d522f475Smrg    CNTRL = 1,
72d522f475Smrg    BLANK = 32
73d522f475Smrg} Classes;
74d522f475Smrg
75d522f475Smrgvoid
76d522f475Smrginit_classtab(void)
77d522f475Smrg{
78d522f475Smrg    const int size = 50;
79d522f475Smrg
80d522f475Smrg    classtab = TypeMallocN(struct classentry, size);
81d522f475Smrg    if (!classtab)
82d522f475Smrg	abort();
83d522f475Smrg    classtab[0].cclass = size;
84d522f475Smrg    classtab[0].first = 1;
85d522f475Smrg    classtab[0].last = 0;
86d522f475Smrg
87d522f475Smrg    /* old xterm default classes */
88d522f475Smrg    SetCharacterClassRange(0, 0, BLANK);
89d522f475Smrg    SetCharacterClassRange(1, 31, CNTRL);
90d522f475Smrg    SetCharacterClassRange('\t', '\t', BLANK);
91d522f475Smrg    SetCharacterClassRange('0', '9', ALNUM);
92d522f475Smrg    SetCharacterClassRange('A', 'Z', ALNUM);
93d522f475Smrg    SetCharacterClassRange('_', '_', ALNUM);
94d522f475Smrg    SetCharacterClassRange('a', 'z', ALNUM);
95d522f475Smrg    SetCharacterClassRange(127, 159, CNTRL);
96d522f475Smrg    SetCharacterClassRange(160, 191, IDENT);
97d522f475Smrg    SetCharacterClassRange(192, 255, ALNUM);
98d522f475Smrg    SetCharacterClassRange(215, 215, IDENT);
99d522f475Smrg    SetCharacterClassRange(247, 247, IDENT);
100d522f475Smrg
101d522f475Smrg    /* added Unicode classes */
102d522f475Smrg    SetCharacterClassRange(0x0100, 0xffdf, ALNUM);	/* mostly characters */
103d522f475Smrg    SetCharacterClassRange(0x037e, 0x037e, IDENT);	/* Greek question mark */
104d522f475Smrg    SetCharacterClassRange(0x0387, 0x0387, IDENT);	/* Greek ano teleia */
105d522f475Smrg    SetCharacterClassRange(0x055a, 0x055f, IDENT);	/* Armenian punctuation */
106d522f475Smrg    SetCharacterClassRange(0x0589, 0x0589, IDENT);	/* Armenian full stop */
107d522f475Smrg    SetCharacterClassRange(0x0700, 0x070d, IDENT);	/* Syriac punctuation */
108d522f475Smrg    SetCharacterClassRange(0x104a, 0x104f, IDENT);	/* Myanmar punctuation */
109d522f475Smrg    SetCharacterClassRange(0x10fb, 0x10fb, IDENT);	/* Georgian punctuation */
110d522f475Smrg    SetCharacterClassRange(0x1361, 0x1368, IDENT);	/* Ethiopic punctuation */
111d522f475Smrg    SetCharacterClassRange(0x166d, 0x166e, IDENT);	/* Canadian Syl. punctuation */
112d522f475Smrg    SetCharacterClassRange(0x17d4, 0x17dc, IDENT);	/* Khmer punctuation */
113d522f475Smrg    SetCharacterClassRange(0x1800, 0x180a, IDENT);	/* Mongolian punctuation */
114d522f475Smrg    SetCharacterClassRange(0x2000, 0x200a, BLANK);	/* spaces */
115d522f475Smrg    SetCharacterClassRange(0x200b, 0x27ff, IDENT);	/* punctuation and symbols */
116d522f475Smrg    SetCharacterClassRange(0x2070, 0x207f, 0x2070);	/* superscript */
117d522f475Smrg    SetCharacterClassRange(0x2080, 0x208f, 0x2080);	/* subscript */
118d522f475Smrg    SetCharacterClassRange(0x3000, 0x3000, BLANK);	/* ideographic space */
119d522f475Smrg    SetCharacterClassRange(0x3001, 0x3020, IDENT);	/* ideographic punctuation */
120d522f475Smrg    SetCharacterClassRange(0x3040, 0x309f, 0x3040);	/* Hiragana */
121d522f475Smrg    SetCharacterClassRange(0x30a0, 0x30ff, 0x30a0);	/* Katakana */
122d522f475Smrg    SetCharacterClassRange(0x3300, 0x9fff, 0x4e00);	/* CJK Ideographs */
123d522f475Smrg    SetCharacterClassRange(0xac00, 0xd7a3, 0xac00);	/* Hangul Syllables */
124d522f475Smrg    SetCharacterClassRange(0xf900, 0xfaff, 0x4e00);	/* CJK Ideographs */
125d522f475Smrg    SetCharacterClassRange(0xfe30, 0xfe6b, IDENT);	/* punctuation forms */
126d522f475Smrg    SetCharacterClassRange(0xff00, 0xff0f, IDENT);	/* half/fullwidth ASCII */
127d522f475Smrg    SetCharacterClassRange(0xff1a, 0xff20, IDENT);	/* half/fullwidth ASCII */
128d522f475Smrg    SetCharacterClassRange(0xff3b, 0xff40, IDENT);	/* half/fullwidth ASCII */
129d522f475Smrg    SetCharacterClassRange(0xff5b, 0xff64, IDENT);	/* half/fullwidth ASCII */
130d522f475Smrg
131d522f475Smrg    return;
132d522f475Smrg}
133d522f475Smrg
134d522f475Smrgint
135d522f475SmrgCharacterClass(int c)
136d522f475Smrg{
137d522f475Smrg    int i, cclass = IDENT;
138d522f475Smrg
139d522f475Smrg    for (i = classtab[0].first; i <= classtab[0].last; i++)
140d522f475Smrg	if (classtab[i].first <= c && classtab[i].last >= c)
141d522f475Smrg	    cclass = classtab[i].cclass;
142d522f475Smrg
143d522f475Smrg    if (cclass < 0)
144d522f475Smrg	cclass = c;
145d522f475Smrg
146d522f475Smrg    return cclass;
147d522f475Smrg}
148d522f475Smrg
149d522f475Smrg#ifdef NO_LEAKS
150d522f475Smrgvoid
151d522f475Smrgnoleaks_CharacterClass(void)
152d522f475Smrg{
153d522f475Smrg    if (classtab != 0) {
154d522f475Smrg	free(classtab);
155d522f475Smrg	classtab = 0;
156d522f475Smrg    }
157d522f475Smrg}
158d522f475Smrg#endif
159d522f475Smrg
160d522f475Smrg#endif /* OPT_WIDE_CHARS */
161