Annotation of src/usr.bin/vim/digraph.c, Revision 1.1.1.1
1.1 downsj 1: /* $OpenBSD$ */
2: /* vi:set ts=4 sw=4:
3: *
4: * VIM - Vi IMproved by Bram Moolenaar
5: *
6: * Do ":help uganda" in Vim to read copying and usage conditions.
7: * Do ":help credits" in Vim to see a list of people who contributed.
8: */
9:
10: /*
11: * digraph.c: code for digraphs
12: */
13:
14: #include "vim.h"
15: #include "globals.h"
16: #include "proto.h"
17: #include "option.h"
18:
19: #ifdef DIGRAPHS
20:
21: static int getexactdigraph __ARGS((int, int, int));
22: static void printdigraph __ARGS((char_u *));
23:
24: static char_u (*digraphnew)[3]; /* pointer to added digraphs */
25: static int digraphcount = 0; /* number of added digraphs */
26:
27: #if defined(MSDOS) || defined(WIN32) || defined(OS2)
28: char_u digraphdefault[][3] = /* standard MSDOS digraphs */
29: {{'C', ',', 128}, /* ~@ (SAS C can't handle the real char) */
30: {'u', '"', 129}, /* */
31: {'e', '\'', 130}, /* */
32: {'a', '^', 131}, /* */
33: {'a', '"', 132}, /* */
34: {'a', '`', 133}, /* */
35: {'a', '@', 134}, /* */
36: {'c', ',', 135}, /* ~G (SAS C can't handle the real char) */
37: {'e', '^', 136}, /* ~H (SAS C can't handle the real char) */
38: {'e', '"', 137}, /* */
39: {'e', '`', 138}, /* */
40: {'i', '"', 139}, /* */
41: {'i', '^', 140}, /* */
42: {'i', '`', 141}, /* */
43: {'A', '"', 142}, /* */
44: {'A', '@', 143}, /* */
45: {'E', '\'', 144}, /* */
46: {'a', 'e', 145}, /* */
47: {'A', 'E', 146}, /* */
48: {'o', '^', 147}, /* */
49: {'o', '"', 148}, /* */
50: {'o', '`', 149}, /* */
51: {'u', '^', 150}, /* */
52: {'u', '`', 151}, /* */
53: {'y', '"', 152}, /* */
54: {'O', '"', 153}, /* */
55: {'U', '"', 154}, /* */
56: {'c', '|', 155}, /* */
57: {'$', '$', 156}, /* */
58: {'Y', '-', 157}, /* ~] (SAS C can't handle the real char) */
59: {'P', 't', 158}, /* */
60: {'f', 'f', 159}, /* */
61: {'a', '\'', 160}, /* */
62: {'i', '\'', 161}, /* */
63: {'o', '\'', 162}, /* */
64: {'u', '\'', 163}, /* xx (SAS C can't handle the real char) */
65: {'n', '~', 164}, /* */
66: {'N', '~', 165}, /* */
67: {'a', 'a', 166}, /* */
68: {'o', 'o', 167}, /* */
69: {'~', '?', 168}, /* */
70: {'-', 'a', 169}, /* */
71: {'a', '-', 170}, /* */
72: {'1', '2', 171}, /* */
73: {'1', '4', 172}, /* */
74: {'~', '!', 173}, /* */
75: {'<', '<', 174}, /* */
76: {'>', '>', 175}, /* */
77:
78: {'s', 's', 225}, /* */
79: {'j', 'u', 230}, /* */
80: {'o', '/', 237}, /* */
81: {'+', '-', 241}, /* */
82: {'>', '=', 242}, /* */
83: {'<', '=', 243}, /* */
84: {':', '-', 246}, /* */
85: {'~', '~', 247}, /* */
86: {'~', 'o', 248}, /* */
87: {'2', '2', 253}, /* */
88: {NUL, NUL, NUL}
89: };
90:
91: #else /* !MSDOS && !WIN32 */
92: # ifdef MINT
93: char_u digraphdefault[][3] = /* standard ATARI digraphs */
94: {{'C', ',', 128}, /* ~@ */
95: {'u', '"', 129}, /* */
96: {'e', '\'', 130}, /* */
97: {'a', '^', 131}, /* */
98: {'a', '"', 132}, /* */
99: {'a', '`', 133}, /* */
100: {'a', '@', 134}, /* */
101: {'c', ',', 135}, /* ~G */
102: {'e', '^', 136}, /* ~H */
103: {'e', '"', 137}, /* */
104: {'e', '`', 138}, /* */
105: {'i', '"', 139}, /* */
106: {'i', '^', 140}, /* */
107: {'i', '`', 141}, /* */
108: {'A', '"', 142}, /* */
109: {'A', '@', 143}, /* */
110: {'E', '\'', 144}, /* */
111: {'a', 'e', 145}, /* */
112: {'A', 'E', 146}, /* */
113: {'o', '^', 147}, /* */
114: {'o', '"', 148}, /* */
115: {'o', '`', 149}, /* */
116: {'u', '^', 150}, /* */
117: {'u', '`', 151}, /* */
118: {'y', '"', 152}, /* */
119: {'O', '"', 153}, /* */
120: {'U', '"', 154}, /* */
121: {'c', '|', 155}, /* */
122: {'$', '$', 156}, /* */
123: {'Y', '-', 157}, /* ~] */
124: {'s', 's', 158}, /* */
125: {'f', 'f', 159}, /* */
126: {'a', '\'', 160}, /* */
127: {'i', '\'', 161}, /* */
128: {'o', '\'', 162}, /* */
129: {'u', '\'', 163}, /* */
130: {'n', '~', 164}, /* */
131: {'N', '~', 165}, /* */
132: {'a', 'a', 166}, /* */
133: {'o', 'o', 167}, /* */
134: {'~', '?', 168}, /* */
135: {'-', 'a', 169}, /* */
136: {'a', '-', 170}, /* */
137: {'1', '2', 171}, /* */
138: {'1', '4', 172}, /* */
139: {'~', '!', 173}, /* */
140: {'<', '<', 174}, /* */
141: {'>', '>', 175}, /* */
142: {'j', 'u', 230}, /* */
143: {'o', '/', 237}, /* */
144: {'+', '-', 241}, /* */
145: {'>', '=', 242}, /* */
146: {'<', '=', 243}, /* */
147: {':', '-', 246}, /* */
148: {'~', '~', 247}, /* */
149: {'~', 'o', 248}, /* */
150: {'2', '2', 253}, /* */
151: {NUL, NUL, NUL}
152: };
153:
154: # else /* !MINT */
155: # ifdef _INCLUDE_HPUX_SOURCE
156:
157: char_u digraphdefault[][3] = /* default HPUX digraphs */
158: {{'A', '`', 161}, /* */
159: {'A', '^', 162}, /* */
160: {'E', '`', 163}, /* */
161: {'E', '^', 164}, /* */
162: {'E', '"', 165}, /* */
163: {'I', '^', 166}, /* */
164: {'I', '"', 167}, /* */
165: {'\'', '\'', 168}, /* */
166: {'`', '`', 169}, /* */
167: {'^', '^', 170}, /* */
168: {'"', '"', 171}, /* */
169: {'~', '~', 172}, /* */
170: {'U', '`', 173}, /* */
171: {'U', '^', 174}, /* */
172: {'L', '=', 175}, /* */
173: {'~', '_', 176}, /* */
174: {'Y', '\'', 177}, /* */
175: {'y', '\'', 178}, /* */
176: {'~', 'o', 179}, /* */
177: {'C', ',', 180}, /* */
178: {'c', ',', 181}, /* */
179: {'N', '~', 182}, /* */
180: {'n', '~', 183}, /* */
181: {'~', '!', 184}, /* */
182: {'~', '?', 185}, /* */
183: {'o', 'x', 186}, /* */
184: {'L', '-', 187}, /* */
185: {'Y', '=', 188}, /* */
186: {'p', 'p', 189}, /* */
187: {'f', 'l', 190}, /* */
188: {'c', '|', 191}, /* */
189: {'a', '^', 192}, /* */
190: {'e', '^', 193}, /* */
191: {'o', '^', 194}, /* */
192: {'u', '^', 195}, /* */
193: {'a', '\'', 196}, /* */
194: {'e', '\'', 197}, /* */
195: {'o', '\'', 198}, /* */
196: {'u', '\'', 199}, /* */
197: {'a', '`', 200}, /* */
198: {'e', '`', 201}, /* */
199: {'o', '`', 202}, /* */
200: {'u', '`', 203}, /* */
201: {'a', '"', 204}, /* */
202: {'e', '"', 205}, /* */
203: {'o', '"', 206}, /* */
204: {'u', '"', 207}, /* */
205: {'A', 'o', 208}, /* */
206: {'i', '^', 209}, /* */
207: {'O', '/', 210}, /* */
208: {'A', 'E', 211}, /* */
209: {'a', 'o', 212}, /* */
210: {'i', '\'', 213}, /* */
211: {'o', '/', 214}, /* */
212: {'a', 'e', 215}, /* */
213: {'A', '"', 216}, /* */
214: {'i', '`', 217}, /* */
215: {'O', '"', 218}, /* */
216: {'U', '"', 219}, /* */
217: {'E', '\'', 220}, /* */
218: {'i', '"', 221}, /* */
219: {'s', 's', 222}, /* */
220: {'O', '^', 223}, /* */
221: {'A', '\'', 224}, /* */
222: {'A', '~', 225}, /* */
223: {'a', '~', 226}, /* */
224: {'D', '-', 227}, /* */
225: {'d', '-', 228}, /* */
226: {'I', '\'', 229}, /* */
227: {'I', '`', 230}, /* */
228: {'O', '\'', 231}, /* */
229: {'O', '`', 232}, /* */
230: {'O', '~', 233}, /* */
231: {'o', '~', 234}, /* */
232: {'S', '~', 235}, /* */
233: {'s', '~', 236}, /* */
234: {'U', '\'', 237}, /* */
235: {'Y', '"', 238}, /* */
236: {'y', '"', 239}, /* */
237: {'p', '-', 240}, /* */
238: {'p', '~', 241}, /* */
239: {'~', '.', 242}, /* */
240: {'j', 'u', 243}, /* */
241: {'P', 'p', 244}, /* */
242: {'3', '4', 245}, /* */
243: {'-', '-', 246}, /* */
244: {'1', '4', 247}, /* */
245: {'1', '2', 248}, /* */
246: {'a', '_', 249}, /* */
247: {'o', '_', 250}, /* */
248: {'<', '<', 251}, /* */
249: {'x', 'x', 252}, /* */
250: {'>', '>', 253}, /* */
251: {'+', '-', 254}, /* */
252: {'n', 'u', 255}, /* (char excluded, is EOF on some systems */
253: {NUL, NUL, NUL}
254: };
255:
256: # else /* _INCLUDE_HPUX_SOURCE */
257:
258: char_u digraphdefault[][3] = /* standard ISO digraphs */
259: {{'~', '!', 161}, /* */
260: {'c', '|', 162}, /* */
261: {'$', '$', 163}, /* */
262: {'o', 'x', 164}, /* */
263: {'Y', '-', 165}, /* */
264: {'|', '|', 166}, /* */
265: {'p', 'a', 167}, /* */
266: {'"', '"', 168}, /* */
267: {'c', 'O', 169}, /* */
268: {'a', '-', 170}, /* */
269: {'<', '<', 171}, /* */
270: {'-', ',', 172}, /* */
271: {'-', '-', 173}, /* */
272: {'r', 'O', 174}, /* */
273: {'-', '=', 175}, /* */
274: {'~', 'o', 176}, /* */
275: {'+', '-', 177}, /* */
276: {'2', '2', 178}, /* */
277: {'3', '3', 179}, /* */
278: {'\'', '\'', 180}, /* */
279: {'j', 'u', 181}, /* */
280: {'p', 'p', 182}, /* */
281: {'~', '.', 183}, /* */
282: {',', ',', 184}, /* */
283: {'1', '1', 185}, /* */
284: {'o', '-', 186}, /* */
285: {'>', '>', 187}, /* */
286: {'1', '4', 188}, /* */
287: {'1', '2', 189}, /* */
288: {'3', '4', 190}, /* */
289: {'~', '?', 191}, /* */
290: {'A', '`', 192}, /* */
291: {'A', '\'', 193}, /* */
292: {'A', '^', 194}, /* */
293: {'A', '~', 195}, /* */
294: {'A', '"', 196}, /* */
295: {'A', '@', 197}, /* */
296: {'A', 'E', 198}, /* */
297: {'C', ',', 199}, /* */
298: {'E', '`', 200}, /* */
299: {'E', '\'', 201}, /* */
300: {'E', '^', 202}, /* */
301: {'E', '"', 203}, /* */
302: {'I', '`', 204}, /* */
303: {'I', '\'', 205}, /* */
304: {'I', '^', 206}, /* */
305: {'I', '"', 207}, /* */
306: {'D', '-', 208}, /* */
307: {'N', '~', 209}, /* */
308: {'O', '`', 210}, /* */
309: {'O', '\'', 211}, /* */
310: {'O', '^', 212}, /* */
311: {'O', '~', 213}, /* */
312: {'O', '"', 214}, /* */
313: {'/', '\\', 215}, /* */
314: {'O', '/', 216}, /* */
315: {'U', '`', 217}, /* */
316: {'U', '\'', 218}, /* */
317: {'U', '^', 219}, /* */
318: {'U', '"', 220}, /* */
319: {'Y', '\'', 221}, /* */
320: {'I', 'p', 222}, /* */
321: {'s', 's', 223}, /* */
322: {'a', '`', 224}, /* */
323: {'a', '\'', 225}, /* */
324: {'a', '^', 226}, /* */
325: {'a', '~', 227}, /* */
326: {'a', '"', 228}, /* */
327: {'a', '@', 229}, /* */
328: {'a', 'e', 230}, /* */
329: {'c', ',', 231}, /* */
330: {'e', '`', 232}, /* */
331: {'e', '\'', 233}, /* */
332: {'e', '^', 234}, /* */
333: {'e', '"', 235}, /* */
334: {'i', '`', 236}, /* */
335: {'i', '\'', 237}, /* */
336: {'i', '^', 238}, /* */
337: {'i', '"', 239}, /* */
338: {'d', '-', 240}, /* */
339: {'n', '~', 241}, /* */
340: {'o', '`', 242}, /* */
341: {'o', '\'', 243}, /* */
342: {'o', '^', 244}, /* */
343: {'o', '~', 245}, /* */
344: {'o', '"', 246}, /* */
345: {':', '-', 247}, /* */
346: {'o', '/', 248}, /* */
347: {'u', '`', 249}, /* */
348: {'u', '\'', 250}, /* */
349: {'u', '^', 251}, /* */
350: {'u', '"', 252}, /* */
351: {'y', '\'', 253}, /* */
352: {'i', 'p', 254}, /* */
353: {'y', '"', 255}, /* (char excluded, is EOF on some systems */
354: {NUL, NUL, NUL}
355: };
356:
357: # endif /* _INCLUDE_HPUX_SOURCE */
358: # endif /* !MINT */
359: #endif /* !MSDOS && !WIN32 */
360:
361: /*
362: * handle digraphs after typing a character
363: */
364: int
365: do_digraph(c)
366: int c;
367: {
368: static int backspaced; /* character before K_BS */
369: static int lastchar; /* last typed character */
370:
371: if (c == -1) /* init values */
372: {
373: backspaced = -1;
374: }
375: else if (p_dg)
376: {
377: if (backspaced >= 0)
378: c = getdigraph(backspaced, c, FALSE);
379: backspaced = -1;
380: if ((c == K_BS || c == Ctrl('H')) && lastchar >= 0)
381: backspaced = lastchar;
382: }
383: lastchar = c;
384: return c;
385: }
386:
387: /*
388: * lookup the pair char1, char2 in the digraph tables
389: * if no match, return char2
390: */
391: static int
392: getexactdigraph(char1, char2, meta)
393: int char1;
394: int char2;
395: int meta;
396: {
397: int i;
398: int retval;
399:
400: if (IS_SPECIAL(char1) || IS_SPECIAL(char2))
401: return char2;
402: retval = 0;
403: for (i = 0; ; ++i) /* search added digraphs first */
404: {
405: if (i == digraphcount) /* end of added table, search defaults */
406: {
407: for (i = 0; digraphdefault[i][0] != 0; ++i)
408: if (digraphdefault[i][0] == char1 && digraphdefault[i][1] == char2)
409: {
410: retval = digraphdefault[i][2];
411: break;
412: }
413: break;
414: }
415: if (digraphnew[i][0] == char1 && digraphnew[i][1] == char2)
416: {
417: retval = digraphnew[i][2];
418: break;
419: }
420: }
421:
422: if (retval == 0) /* digraph deleted or not found */
423: {
424: if (char1 == ' ' && meta) /* <space> <char> --> meta-char */
425: return (char2 | 0x80);
426: return char2;
427: }
428: return retval;
429: }
430:
431: /*
432: * Get digraph.
433: * Allow for both char1-char2 and char2-char1
434: */
435: int
436: getdigraph(char1, char2, meta)
437: int char1;
438: int char2;
439: int meta;
440: {
441: int retval;
442:
443: if (((retval = getexactdigraph(char1, char2, meta)) == char2) &&
444: (char1 != char2) &&
445: ((retval = getexactdigraph(char2, char1, meta)) == char1))
446: return char2;
447: return retval;
448: }
449:
450: /*
451: * put the digraphs in the argument string in the digraph table
452: * format: {c1}{c2} char {c1}{c2} char ...
453: */
454: void
455: putdigraph(str)
456: char_u *str;
457: {
458: int char1, char2, n;
459: char_u (*newtab)[3];
460: int i;
461:
462: while (*str)
463: {
464: str = skipwhite(str);
465: if ((char1 = *str++) == 0 || (char2 = *str++) == 0)
466: return;
467: if (char1 == ESC || char2 == ESC)
468: {
469: EMSG("Escape not allowed in digraph");
470: return;
471: }
472: str = skipwhite(str);
473: if (!isdigit(*str))
474: {
475: emsg(e_number);
476: return;
477: }
478: n = getdigits(&str);
479: if (digraphnew) /* search the table for existing entry */
480: {
481: for (i = 0; i < digraphcount; ++i)
482: if (digraphnew[i][0] == char1 && digraphnew[i][1] == char2)
483: {
484: digraphnew[i][2] = n;
485: break;
486: }
487: if (i < digraphcount)
488: continue;
489: }
490: newtab = (char_u (*)[3])alloc(digraphcount * 3 + 3);
491: if (newtab)
492: {
493: vim_memmove((char *)newtab, (char *)digraphnew,
494: (size_t)(digraphcount * 3));
495: vim_free(digraphnew);
496: digraphnew = newtab;
497: digraphnew[digraphcount][0] = char1;
498: digraphnew[digraphcount][1] = char2;
499: digraphnew[digraphcount][2] = n;
500: ++digraphcount;
501: }
502: }
503: }
504:
505: void
506: listdigraphs()
507: {
508: int i;
509:
510: msg_outchar('\n');
511: printdigraph(NULL);
512: for (i = 0; digraphdefault[i][0] && !got_int; ++i)
513: {
514: if (getexactdigraph(digraphdefault[i][0], digraphdefault[i][1],
515: FALSE) == digraphdefault[i][2])
516: printdigraph(digraphdefault[i]);
517: mch_breakcheck();
518: }
519: for (i = 0; i < digraphcount && !got_int; ++i)
520: {
521: printdigraph(digraphnew[i]);
522: mch_breakcheck();
523: }
524: must_redraw = CLEAR; /* clear screen, because some digraphs may be wrong,
525: * in which case we messed up NextScreen */
526: }
527:
528: static void
529: printdigraph(p)
530: char_u *p;
531: {
532: char_u buf[9];
533: static int len;
534:
535: if (p == NULL)
536: len = 0;
537: else if (p[2] != 0)
538: {
539: if (len > Columns - 11)
540: {
541: msg_outchar('\n');
542: len = 0;
543: }
544: if (len)
545: MSG_OUTSTR(" ");
546: sprintf((char *)buf, "%c%c %c %3d", p[0], p[1], p[2], p[2]);
547: msg_outstr(buf);
548: len += 11;
549: }
550: }
551:
552: #endif /* DIGRAPHS */