summaryrefslogtreecommitdiff
path: root/setedit/mainsrc/accehtml.cc
blob: 6135b5d59f786b81a9b4c670ce9c64c637f41c14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright (C) 1996-2003 by Salvador E. Tropea (SET),
   see copyrigh file for details */
#define Uses_string

#define Uses_TView
#define Uses_TApplication
#define Uses_TDeskTop
#define Uses_MsgBox
#define Uses_TCEditWindow
#define Uses_TVCodePage
#include <ceditor.h>
#define Uses_SETAppVarious

#include <setapp.h>
#include <ctype.h>
#include <ucdefs.h>

static void ExpandAccents(TCEditor *e);
static void CompactAccents(TCEditor *e);

void HTMLAcc_Entry(Boolean compact)
{
 TView *p;

 p=TApplication::deskTop->current;
 if (!p) return; // Avoid a search
 if (IsAnEditor(p))
   {
    TCEditor *e=((TCEditWindow *)p)->editor;
    if (e->isReadOnly || TCEditor::editorDialog(edActionWOUndo)!=cmYes)
       return;
    if (e->IslineInEdition)
       e->MakeEfectiveLineInEdition();
    e->buffer[e->bufLen]=0;
    e->SetMarker(9);

    if (compact)
       CompactAccents(e);
    else
       ExpandAccents(e);

    /* Jump to the start because the line offset is no longer valid and it can
       mess the drawing */
    e->ResetCursorPosition();
    e->GotoOffSet(e->Markers[9]);
    e->update(ufView);
    e->trackCursor(True);

    /* Flush the undo information */
    e->flushUndoInfo();
   }
 else
   messageBox(__("You must select the editor window first"),mfError | mfOKButton);
}

/*****************************************************************************

  The following table translates my internal fonts numbers to ISO-1 html
strings.

*****************************************************************************/
static char *Remap[]=
{
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0..15
 0,0,0,0,
 "para",   // 20 ¶ pilcrow (paragraph sign)
 "sect",   // 21 § section sign
 0,0,0,0,0,0,0,0,0,0,             // ..31
 0,0,0,0,
 "curren", // 36 ¤ general currency sign
 0,0,0,0,0,0,0,0,0,0,0,           // ..47
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // ..127
 "Ccedil", // 128 capital C, cedilla
 "uuml",   // 129 small u, dieresis or umlaut mark
 "eacute", // 130 é small e, acute accent
 "acirc",  // 131 small a, circumflex accent
 "auml",   // 132 ä mall a, dieresis or umlaut mark 
 "agrave", // 133 small a, grave accent
 "aring",  // 134 small a, ring
 "ccedil", // 135 ç small c, cedilla
 "ecirc",  // 136 ê small e, circumflex accent
 "euml",   // 137 ë small e, dieresis or umlaut mark
 "egrave", // 138 è small e, grave accent
 "iuml",   // 139 ï small i, dieresis or umlaut mark
 "icirc",  // 140 î small i, circumflex accent
 "igrave", // 141 ì small i, grave accent
 "Auml",   // 142 Ä capital A, dieresis or umlaut mark
 "Aring",  // 143 Å capital A, ring
 "Eacute", // 144 É capital E, acute accent
 "aelig",  // 145 small ae diphthong (ligature)
 "AElig",  // 146 capital AE diphthong (ligature)
 "ocirc",  // 147 ô small o, circumflex accent
 "ouml",   // 148 ö small o, dieresis or umlaut mark
 "ograve", // 149 ò small o, grave accent
 "ucirc",  // 150 û small u, circumflex accent
 "ugrave", // 151 ù small u, grave accent
 "yuml",   // 152 small y, dieresis or umlaut mark
 "Ouml",   // 153 Ö capital O, dieresis or umlaut mark
 "Uuml",   // 154 Ü capital U, dieresis or umlaut mark
 "cent",   // 155 cent sign
 "pound",  // 156 pound sterling sign
 "yen",    // 157 yen sign
 0,        // 158 peseta
 0,        // 159 flor¡n
 "aacute", // 160 á small a, acute accent
 "iacute", // 161 í small i, acute accent
 "oacute", // 162 ó small o, acute accent
 "uacute", // 163 ú small u, acute accent
 "ntilde", // 164 small n, tilde
 "Ntilde", // 165 capital N, tilde
 "ordf",   // 166 ª ordinal indicator, feminine
 "ordm",   // 167 º ordinal indicator, masculine
 "iquest", // 168 ¿ inverted question mark
 0,        // 169
 "not",    // 170 ¬ not sign
 "frac12", // 171 ½ fraction one-half
 "frac14", // 172 ¼ fraction one-quarter
 "iexcl",  // 173 ¡ inverted exclamation mark
 "laquo",  // 174 « angle quotation mark, left
 "raquo",  // 175 » angle quotation mark, right
 0,0,0,0,0,           // 176 .. 180
 0,0,0,0,0,0,0,0,0,0, // 181 .. 190
 0,0,0,0,0,0,0,0,0,0, // 191 .. 200
 0,0,0,0,0,0,0,0,0,0, // 201 .. 210
 0,0,0,0,0,0,0,0,0,0, // 211 .. 220
 0,0,0,
 0,        // 224 alfa
 "szlig",  // 225 ß small sharp s, German (sz ligature)
 0,        // 226 gama may.
 0,        // 227 pi
 0,        // 228 sigma may.
 0,        // 229 sigma
 "micro",  // 230 µ micro sign
 0,        // 231 tau
 0,0,0,0,0,0,0,0,0,
 "plusmn", // 241 ± plus-or-minus sign
 0,0,0,0,
 "divide", // 246 ÷ divide sign
 0,
 "deg",    // 248 ° degree sign
 "middot", // 249 · middle dot
 0,0,0,
 "sup2",   // 253 ² superscript two
 0,0,      // ..255
 0,0,0,0,0,
 "yacute", // 261 ý small y, acute accent
 0,
 "Aacute", // 263 Á capital A, acute accent
 0,
 "Iacute", // 265 Í capital I, acute accent
 0,0,
 "Oacute", // 268 Ó capital O, acute accent
 0,0,
 "Uacute", // 271 Ú capital U, acute accent
 "Yacute", // 272 Ý capital Y, acute accent
 0,0,
 "Agrave", // 275 À capital A, grave accent
 "Egrave", // 276 È capital E, grave accent
 "Igrave", // 277 Ì capital I, grave accent
 "Ograve", // 278 Ò capital O, grave accent
 "Ugrave", // 279 Ù capital U, grave accent
 "Euml",   // 280 Ë capital E, dieresis or umlaut mark
 "Iuml",   // 281 Ï capital I, dieresis or umlaut mark
 0,0,
 "atilde", // 284 ã small a, tilde
 "otilde", // 285 õ small o, tilde
 0,
 "Atilde", // 287 Ã capital A, tilde
 "Otilde", // 288 Õ capital O, tilde
 "Acirc",  // 289 Â capital A, circumflex accent
 "Ecirc",  // 290 Ê capital E, circumflex accent
 "Icirc",  // 291 Î capital I, circumflex accent
 "Ocirc",  // 292 Ô capital O, circumflex accent
 "Ucirc",  // 293 Û capital U, circumflex accent
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 "oslash", // 318 ø small o, slash
 0,
 "Oslash", // 320 Ø capital O, slash
 "eth",    // 321 ð small eth, Icelandic
 0,
 "ETH",    // 323 Ð capital Eth, Icelandic
 0,0,0,0,0,0,0,0,
 "thorn",  // 332 þ small thorn, Icelandic
 "THORN",  // 333 Þ capital THORN, Icelandic
 "times",  // 334 × multiply sign
 "reg",    // 335 ® registered sign
 "copy",   // 336 © copyrigh\t sign
 0,
 "brvbar", // 338 ¦ broken (vertical) bar
 0,
 "acute",  // 340 ´ acute accent
 "shy",    // 341 ­ soft hyphen /// macron
 0,
 "frac34", // 343 ¾ fraction three-quarters
 "cedil",  // 344 ¸ cedilla
 "uml",    // 345 ¨ umlaut (dieresis)
 "sup1",   // 346 ¹ superscript one
 "sup3"    // 347 ³ superscript three
};

const unsigned LastRemap=sizeof(Remap)/sizeof(char *);

static
void ExpandAccents(TCEditor *e)
{
 // Get the map for the current code page
 ushort *map=TVCodePage::GetTranslate(TVCodePage::GetAppCodePage());

 e->lock();
 e->lockUndo();

 uchar *s=(uchar *)e->buffer;
 char *repl;
 unsigned len=e->bufLen,pos=0,value,index;
 char buf[12];
 buf[0]='&';

 while (pos<len)
   {
    // Convert the char code to the internal representation
    value=map[s[pos]];
    if (value<LastRemap)
      {
       if (Remap[value])
         {
          e->deleteRange((char *)s+pos,(char *)s+pos+1,False);
          repl=Remap[value];
          for (index=0; repl[index]; index++)
              buf[index+1]=repl[index];
          buf[index+1]=';';
          e->insertText(buf,index+2,False);
          // Adjust the len because it changed
          len=e->bufLen;
          // The buffer could be reallocated and hence changed!
          s=(uchar *)e->buffer;
         }
      }
    pos++;
   }
 e->unlockUndo();
 e->unlock();
}

static
void CompactAccents(TCEditor *e)
{
 // Get the map for the current code page
 ushort *map=TVCodePage::GetTranslate(TVCodePage::GetAppCodePage());

 e->lock();
 e->lockUndo();

 uchar *s=(uchar *)e->buffer;
 unsigned len=e->bufLen,pos=0,value,index,lenW,posStart;
 char buf[12];

 while (pos<len)
   {
    if (s[pos]!='&')
      {
       pos++;
       continue;
      }
    posStart=pos;
    // Take the word
    index=0; pos++;
    while (pos<len && TVCodePage::isAlpha(s[pos]) && index<6)
      {
       buf[index++]=s[pos];
       pos++;
      }
    lenW=index+1;
    buf[index]=0;
    if (pos<len)
      {
       if (TVCodePage::isAlpha(s[pos]))
          continue;
       if (s[pos]==';')
          lenW++;
      }

    // Search what's the code for it
    // Could be faster creating a sorted collection with the names.
    for (value=0; value<LastRemap; value++)
        if (Remap[value] && strcmp(Remap[value],buf)==0)
           break;
    if (value==LastRemap) continue;

    // Search the code in this code page
    // Could be faster creating a look-up table
    for (index=0; index<256; index++)
        if ((unsigned)map[index]==value)
           break;
    if (index==256) continue;

    e->deleteRange((char *)s+posStart,(char *)s+posStart+lenW,False);
    buf[0]=index;
    e->insertText(buf,1,False);
    pos-=lenW-1;
    // Adjust the len because it changed
    len=e->bufLen;
    // The buffer could be reallocated and hence changed!
    s=(uchar *)e->buffer;
   }
 e->unlockUndo();
 e->unlock();
}