forked from mbert/elvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmsyntax.c
1922 lines (1756 loc) · 52.6 KB
/
dmsyntax.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dmsyntax.c */
/* Copyright 1995 by Steve Kirkendall */
#include "elvis.h"
#ifdef FEATURE_RCSID
char id_dmsyntax[] = "$Id: dmsyntax.c,v 2.104 2003/10/19 23:09:33 steve Exp $";
#endif
#ifdef DISPLAY_SYNTAX
/* These are the descriptions and values of some global options */
static OPTDESC globdesc[] =
{
{"includepath", "inc", optsstring, optisstring }
};
static OPTVAL globval[QTY(globdesc)];
#define o_includepath globval[0].value.string
/* This data type is used to denote a token type. Values of this type will
* be used as indices into the cfont[] array, below, to determine which
* font each language element should use. The last symbol in the list must
* be PUNCT, because the declaration of cfont[] depends on this.
*/
typedef enum
{
COMMENT, COMMENT2, STRING0, STRING1, CHARACTER, REGEXP, REGSUB,
KEYWORD, DOC, DOCMARKUP, DOCINDENT, FUNCTION, VARIABLE, NUMBER,
PREP, PREPWORD, PREPQUOTE, OTHER, PUNCT
} TOKENTYPE;
/* This structure stores information about the current language's syntax.
* Each window has its own copy of this, so different windows which happen
* to be in "syntax" mode can each display different languages.
*/
typedef struct
{
/* info about the current parsing state */
TOKENTYPE token; /* used during parsing */
/* info from the "elvis.syn" file */
spell_t *keyword; /* dictionary of keyword names */
CHAR *opkeyword; /* "operator" keyword, or NULL */
CHAR *setargs; /* arguments to ":set" command */
CHAR function; /* character used for function calls */
CHAR backslash; /* string quote character - usually backslash */
CHAR strbegin[2]; /* string start-quote character */
CHAR strend[2]; /* string end-quote character */
CHAR charbegin; /* character quote character */
CHAR charend; /* character quote character */
CHAR preprocessor; /* first character of preprocessor directives */
CHAR pqbegin; /* preprocessor start-quote character */
CHAR pqend; /* preprocessor start-quote character */
CHAR comment[2]; /* start of one-line comment */
CHAR combegin[2]; /* start of multi-line comment */
CHAR comend[2]; /* end of multi-line comment */
CHAR enddoc[20]; /* end of embedded docs ("" if not used) */
ELVBOOL allcaps; /* uppercase words are "other" */
ELVBOOL initialcaps; /* mixed case words starting with upper are "other" */
ELVBOOL mixedcaps; /* mixed case words starting with lower are "other" */
ELVBOOL finalt; /* words ending with "_t" are "other" */
ELVBOOL initialpunct; /* words starting with punctuation */
ELVBOOL ignorecase; /* keywords may be uppercase */
ELVBOOL strnewline; /* literal newline chars allowed in strings */
ELVBOOL strnoindent; /* indentation not allowed in multi-line strings */
ELVBOOL strnoempty; /* empty lines not allowed in multi-line strings */
char mostly; /* font code of variables */
char wordbits[256]; /* which chars can appear in a word */
} SINFO;
#define STARTWORD 0x01 /* char: can start a word */
#define INWORD 0x02 /* char: can occur within a word */
#define DELIMREGEXP 0x04 /* char: can delimit regular expression */
#define ISPREFIX 0x01 /* word: may be first punct of 2-punct word */
#define ISCOMMENT 0x02 /* word: marks the start of a comment */
#define ISKEYWORD 0x04 /* word: is this a keyword? (else tag) */
#define USEREGEXP 0x10 /* either: occurs before possible regexp */
#define USEREGSUB 0x20 /* either: occurs before possible regsub */
#define OPERATOR 0x40 /* either: prefix to operatorXX name */
#define isstartword(si,c) ((si)->wordbits[(CHAR)(c)] & STARTWORD)
#define isinword(si,c) ((si)->wordbits[(CHAR)(c)] & INWORD)
#define isregexp(si,c) ((si)->wordbits[(CHAR)(c)] & DELIMREGEXP)
#define isbeforeregexp(si,c) ((si)->wordbits[(CHAR)(c)] & USEREGEXP)
#define isbeforeregsub(si,c) ((si)->wordbits[(CHAR)(c)] & USEREGSUB)
#define isoperator(si,c) ((si)->wordbits[(CHAR)(c)] & OPERATOR)
#define wordflags(w) ((w)->flags)
#define wordanchor(w) (((w)->flags >> 16) & 0xfff)
#define wordsetanchor(w, a) ((w)->flags = ((w)->flags & ~0x0fff0000) | ((a) << 16))
#define wordfont(w) ((char)((w)->flags >> 8))
#define wordsetfont(w, f) ((w)->flags = ((w)->flags & ~0x0000ff00) | ((f) << 8))
#define wordprefix(w) (wordflags(w) & ISPREFIX)
#define wordcomment(w) (wordflags(w) & ISCOMMENT)
#define wordkeyword(w) (wordflags(w) & ISKEYWORD)
#define wordbeforeregexp(w) (wordflags(w) & USEREGEXP)
#define wordbeforeregsub(w) (wordflags(w) & USEREGSUB)
#define wordoperator(w) (wordflags(w) & OPERATOR)
/* Most anchor values are in the range 1 - 0xffe, but these are special */
#define ANCHOR_NONE 0
#define ANCHOR_FRONT 0xfff
#if USE_PROTOTYPES
static spell_t *iskeyword(CHAR *word, long anchor, ELVBOOL indent);
static spell_t *scankeyword(CHAR **refp, long anchor, ELVBOOL indent);
static void addkeyword(CHAR *word, _char_ font, int anchor, _CHAR_ flags);
static DMINFO *init(WINDOW win);
static void term(DMINFO *info);
static MARK setup(WINDOW win, MARK top, long cursor, MARK bottom, DMINFO *info);
static MARK image(WINDOW w, MARK line, DMINFO *info, void (*draw)(CHAR *p, long qty, _char_ font, long offset));
static CHAR *tagatcursor(WINDOW win, MARK cursor);
static MARK tagload(CHAR *tagname, MARK from);
#endif
/* This array stores the fonts to be used with the TOKENTYPES, above. It is
* initialized by the setup() function each time the screen is redrawn,
* to reflect the values of the "font" options above.
*/
static char cfont[PUNCT + 1];
static char font_keyword;
static SINFO *sinfo;
/* This macro computes a hash value for a word, which is used for looking
* the word up in the SINFO.keyword[] table. The word is known to be at least
* one character long and terminated with a '\0', so word[1] is guaranteed to
* be valid and consistent.
*/
#define KWHASH(word) (((word)[0] & 0x1f) ^ (((word)[1] & 0x03) << 5))
/* This function scans text from a buffer, to determine whether it is a keyword.
* If it is a keyword, then its spell_t structure is returned; else NULL.
* Either way, refp is advanced past the end of the scanned text.
*/
static spell_t *scankeyword(refp, colplusone, indent)
CHAR **refp; /* reference to a scanning variable */
long colplusone;/* current column (1-based), or 0 to ignore column */
ELVBOOL indent; /* preceded only by indentation whitespace? */
{
spell_t *node;
int anchor;
/* look it up, being careful about case sensitivity */
for (node = sinfo->keyword; node && *refp && **refp; scannext(refp))
{
/* if this is a prefix keyword regardless of the next char, or
* a normal keyword and the next char isn't an "inword" char,
* then we've found a match.
*/
if (SPELL_IS_GOOD(node)
&& (!isinword(sinfo, **refp) || wordprefix(node)))
{
/* if anchored, check the column number */
if (colplusone != 0)
{
anchor = wordanchor(node);
switch (anchor)
{
case ANCHOR_NONE:
/* never a problem */
break;
case ANCHOR_FRONT:
/* any leading whitespace is okay, otherwise reject */
if (!indent)
{
goto Continue;
}
break;
default:
/* must be in a specific column */
if (anchor != colplusone)
{
goto Continue;
}
}
}
/* No problems! This is it! */
break;
}
/* Still looking - check the next character */
Continue:
if (sinfo->ignorecase)
node = spellletter(node, elvtolower(**refp));
else
node = spellletter(node, **refp);
}
if (!SPELL_IS_GOOD(node))
return NULL;
/* return the keyword */
return node;
}
/* This function returns a pointer to the hashed keyword description if the
* given word is in fact a keyword. Otherwise it returns NULL.
*/
static spell_t *iskeyword(word, colplusone, indent)
CHAR *word; /* pointer to word */
long colplusone;/* current column (1-based), or 0 to ignore column */
ELVBOOL indent; /* preceded only by indentation whitespace? */
{
spell_t *node;
int anchor;
/* look it up, being careful about case sensitivity */
for (node = sinfo->keyword; node && *word; word++)
{
if (sinfo->ignorecase)
node = spellletter(node, elvtolower(*word));
else
node = spellletter(node, *word);
}
if (!SPELL_IS_GOOD(node))
return NULL;
/* if anchored, check the column number */
if (colplusone != 0)
{
anchor = wordanchor(node);
switch (anchor)
{
case ANCHOR_NONE:
/* never a problem */
break;
case ANCHOR_FRONT:
/* any leading whitespace is okay, otherwise reject */
if (!indent)
{
return NULL;
}
break;
default:
/* must be in a specific column */
if (anchor != colplusone)
{
return NULL;
}
}
}
/* return the keyword */
return node;
}
static void addkeyword(word, font, anchor, flags)
CHAR *word; /* a keyword */
_char_ font; /* font, or '\0' for default */
int anchor; /* columns, or ANCHOR_FRONT, or ANCHOR_NONE */
_CHAR_ flags; /* ISCOMMENT|ISKEYWORD|USEREGEXP|USEREGSUB|OPERATOR */
{
spell_t *keyword; /* spell node of the keyword */
CHAR last;
/* if the last character isn't an "inword" character, then assume it
* should be a "prefix" word.
*/
last = word[CHARlen(word) - 1];
if (!isinword(sinfo, last))
flags |= ISPREFIX;
if (isbeforeregsub(sinfo, last))
flags |= USEREGEXP|USEREGSUB;
else if (isbeforeregexp(sinfo, last))
flags |= USEREGEXP;
/* see if the keyword is already in the list */
keyword = iskeyword(word, 0L, ElvTrue);
if (!keyword)
{
sinfo->keyword = spelladdword(sinfo->keyword, word, 0L);
keyword = iskeyword(word, 0L, ElvTrue);
}
/* set the word's attributes */
if (font)
wordsetfont(keyword, font);
if (anchor != ANCHOR_NONE)
wordsetanchor(keyword, anchor);
wordflags(keyword) |= flags;
}
/* This global function returns the prepchar of a window, or '\0' if the
* window isn't in the syntax display mode, or the syntax doesn't use a
* preprocessor.
*/
CHAR dmspreprocessor(win)
WINDOW win; /* window to be checked */
{
if (win->md != &dmsyntax)
return '\0';
else
return ((SINFO *)win->mi)->preprocessor;
}
#ifdef FEATURE_SMARTARGS
/* This global function implements the smartargs option. It should be called
* immediately after a character has been inserted. If the character appears
* to mark the start of function arguments, then this temporarily inserts
* the formal args after the cursor, as a hint to the user.
*/
void dmssmartargs(win)
WINDOW win; /* window to be checked */
{
CHAR *cp, *build, buf[100];
CHAR *calcarg[2];
long cursoff;
int i;
spell_t *kw;
/* if obviously not a function invocation, then just return NULL */
if (!o_smartargs(markbuffer(win->cursor)) /* smartargs isn't set */
|| win->md != &dmsyntax /* not in syntax mode */
|| win->state->cursor != win->cursor) /* not in main buffer */
return;
/* we'll use this window's syntax */
sinfo = (SINFO *)win->mi;
/* verify that we're at the start of a function's args */
scanalloc(&cp, win->cursor);
if (!scanprev(&cp) || *cp != sinfo->function)
{
scanfree(&cp);
/* not after "function" character */
return;
}
/* skip whitespace between name & function char */
do
{
if (!scanprev(&cp))
{
/* no function name before "function" character */
scanfree(&cp);
return;
}
} while (elvspace(*cp));
/* collect the chars of the function name */
build = &buf[QTY(buf)];
*--build = '\0';
while (build > buf && isinword(sinfo, *cp))
{
*--build = *cp;
if (!scanprev(&cp))
break;
}
scanfree(&cp);
if (!isstartword(sinfo, *build))
/* doesn't look like a name */
return;
/* keywords such as while() look like functions, but should never
* have a tag. Even if they do, this is likely to be more annoying
* than useful, so skip it.
*/
kw = iskeyword(build, 0, ElvTrue);
if (kw && wordkeyword(kw))
return;
/* build a shell command line that returns the function's definition,
* and then run that shell command.
*/
calcarg[0] = build;
calcarg[1] = NULL;
cp = calculate(toCHAR("shell(\"ref -cd \"$1)"), calcarg, CALC_ALL);
if (!cp)
/* calc statement failed */
return;
/* Scan for the function name in the returned string. Note that we're
* sloppy about this -- we don't ensure that word boundaries match. */
i = CHARlen(build);
while (*cp && CHARncmp(cp, build, i))
cp++;
if (!*cp)
/* function name not found in returned string */
return;
/* Skip past the '(' after the name */
for (cp += i; *cp && *cp != sinfo->function; cp++)
{
}
if (!*cp)
/* no '(' found after the function name */
return;
cp++;
/* count the length of the remainder of the line */
for (i = 0; cp[i] && cp[i] != '\n' && cp[i] != '\t'; i++)
{
}
/* Insert that portion of the returned string into the edit buffer
* AFTER the cursor. As the user continues to type more text, this
* argument hint will be overwritten.
*/
if (markoffset(win->cursor) < markoffset(win->state->bottom))
bufreplace(win->cursor, win->state->bottom, NULL, 0);
cursoff = markoffset(win->cursor);
bufreplace(win->cursor, win->cursor, cp, i);
marksetoffset(win->state->bottom, cursoff + i);
marksetoffset(win->cursor, cursoff);
}
#endif /* FEATURE_SMARTARGS */
/* This global function checks whether a given word is a keyword in the current
* language. If the window isn't in "syntax" mode then it always returns
* ElvFalse.
*/
ELVBOOL dmskeyword(win, word)
WINDOW win; /* window to be checked */
CHAR *word; /* the word to look up */
{
spell_t *kw; /* info about a possible keyword */
#if 0
int nupper, nlower, i;
#endif
/* if not in syntax display mode, then just return ElvFalse. */
if (win->md != &dmsyntax)
return ElvFalse;
/* first check real keywords */
sinfo = (SINFO *)win->mi;
kw = iskeyword(word, 0L, ElvTrue);
if (kw != NULL && wordkeyword(kw))
return ElvTrue;
#if 0
/* Check for "other" words */
if (sinfo->initialpunct && !elvalnum(word[0]))
return ElvTrue;
for (i = nupper = nlower = 0; word[i]; i++)
if (elvupper(word[i]))
nupper++;
else if (elvlower(word[i]))
nlower++;
if (sinfo->finalt && i > 2 && !CHARcmp(word + i - 2, toCHAR("_t")))
return ElvTrue;
if (sinfo->allcaps && nlower == 0 && nupper > 0)
return ElvTrue;
if (sinfo->initialcaps && nlower > 0 && elvupper(word[0]))
return ElvTrue;
if (sinfo->mixedcaps && nlower > 0 && nupper > 0)
return ElvTrue;
#endif
/* I guess not */
return ElvFalse;
}
/* start the mode, and allocate dminfo */
static DMINFO *init(win)
WINDOW win;
{
char *str, *path;
CHAR *cp, **values, dummy[1];
spell_t *kw;
int i, j, flags;
/* if this is the first-ever time a window has been initialized to
* this mode, then we have some extra work to do...
*/
if (!dmsyntax.mark2col)
{
/* Inherit some functions from normal mode. */
(*dmnormal.init)(win);
dmsyntax.mark2col = dmnormal.mark2col;
dmsyntax.move = dmnormal.move;
dmsyntax.wordmove = dmnormal.wordmove;
dmsyntax.header = dmnormal.header;
dmsyntax.indent = dmnormal.indent; /* !!! really a good idea? */
dmsyntax.tagnext = dmnormal.tagnext;
/* initialize the mode's global options */
str = getenv("INCLUDE");
#ifdef OSINCLUDEPATH
if (!str)
str = OSINCLUDEPATH;
#endif
optpreset(o_includepath, toCHAR(str), OPT_HIDE);
/* locate the default fonts */
cfont[COMMENT] =
cfont[COMMENT2] = colorfind(toCHAR("comment"));
cfont[STRING0] = cfont[STRING1] = colorfind(toCHAR("string"));
cfont[CHARACTER] = colorfind(toCHAR("char"));
cfont[REGEXP] = colorfind(toCHAR("regexp"));
cfont[REGSUB] = colorfind(toCHAR("regsub"));
font_keyword =
cfont[KEYWORD] = colorfind(toCHAR("keyword"));
cfont[DOC] = colorfind(toCHAR("doc"));
cfont[DOCMARKUP] = colorfind(toCHAR("docmarkup"));
cfont[DOCINDENT] = colorfind(toCHAR("docindent"));
cfont[FUNCTION] = colorfind(toCHAR("function"));
cfont[VARIABLE] = 0; /* set to sinfo->mostly in setup() */
cfont[NUMBER] = colorfind(toCHAR("number"));
cfont[PREP] =
cfont[PREPWORD] = colorfind(toCHAR("prep"));
cfont[PREPQUOTE] = colorfind(toCHAR("prepquote"));
cfont[OTHER] = colorfind(toCHAR("other"));
cfont[PUNCT] = 0; /* generic font for generic chars */
/* set some defaults */
colorset(cfont[COMMENT], toCHAR("italic"), ElvFalse);
colorset(cfont[CHARACTER], toCHAR("like string"), ElvFalse);
colorset(cfont[REGEXP], toCHAR("like string"), ElvFalse);
colorset(cfont[REGSUB], toCHAR("like regexp"), ElvFalse);
colorset(cfont[KEYWORD], toCHAR("bold"), ElvFalse);
colorset(cfont[PREP], toCHAR("like keyword"), ElvFalse);
colorset(cfont[PREPQUOTE], toCHAR("like string"), ElvFalse);
colorset(cfont[OTHER], toCHAR("like keyword"), ElvFalse);
colorset(cfont[DOC], toCHAR("proportional"), ElvFalse);
colorset(cfont[DOCMARKUP], toCHAR("like doc bold"), ElvFalse);
colorset(cfont[DOCINDENT], toCHAR("like doc fixed"), ElvFalse);
/* if no real window, then we're done! */
if (!win)
return NULL;
}
/* if possible, reuse an existing SINFO structure */
#ifdef FEATURE_CACHEDESC
sinfo = (SINFO *)descr_recall(win, SYNTAX_FILE);
if (!sinfo)
#endif
{
/* allocate a SINFO structure for this window */
sinfo = (SINFO *)safealloc(1, sizeof(SINFO));
/* initialize the wordbits[] array to allow letters and digits */
for (i = 0; i < QTY(sinfo->wordbits); i++)
{
sinfo->wordbits[i] = (elvalnum(i) ? (STARTWORD|INWORD) : 0);
}
/* set the "mostly" font code to "variable", for now */
sinfo->mostly = colorfind(toCHAR("variable"));
/* the default string quote is a backslash */
sinfo->backslash = '\\';
/* locate the description in the "elvis.syn" file */
if (descr_open(win, SYNTAX_FILE))
{
while ((values = descr_line()) != NULL)
{
str = tochar8(values[0]);
if (!strcmp(str, "keyword"))
{
for (i = 1; values[i]; i++)
{
addkeyword(values[i], '\0', ANCHOR_NONE, ISKEYWORD);
}
}
else if (!strcmp(str, "font") && values[1])
{
for (i = 2; values[i]; i++)
{
addkeyword(values[i], colorfind(values[1]), ANCHOR_NONE, ISKEYWORD);
}
}
else if (!strcmp(str, "prefix") && values[1])
{
for (i = 1; values[i]; i++)
{
addkeyword(values[i], '\0', ANCHOR_NONE, ISKEYWORD|ISPREFIX);
}
}
else if (!strcmp(str, "anchor") && values[1])
{
if (values[1][0] == '^')
j = ANCHOR_FRONT;
else
j = atoi(tochar8(values[1]));
if (j > 0 && j <= ANCHOR_FRONT)
{
for (i = 2; values[i]; i++)
{
addkeyword(values[i], '\0', j, ISKEYWORD);
}
}
}
else if (!strcmp(str, "comment"))
{
for (i = 1; values[i]; i++)
{
if (values[i + 1] && !elvalnum(*values[i]))
{
CHARncpy(sinfo->combegin, values[i], 2);
CHARncpy(sinfo->comend, values[++i], 2);
}
else
{
CHARncpy(sinfo->comment, values[1], 2);
addkeyword(values[i], '\0', 0, ISCOMMENT|ISKEYWORD);
}
}
}
else if (!strcmp(str, "useregexp")
|| !strcmp(str, "useregsub")
|| !strcmp(str, "operator"))
{
if (!strcmp(str, "useregexp"))
flags = USEREGEXP;
else if (!strcmp(str, "useregsub"))
flags = USEREGEXP|USEREGSUB;
else
flags = OPERATOR;
for (i = 1; values[i]; i++)
{
kw = iskeyword(values[i], 0L, ElvTrue);
if (kw)
{
wordflags(kw) |= flags;
}
else if (elvalpha(values[i][0]))
{
addkeyword(values[i], '\0', ANCHOR_NONE, ISKEYWORD|flags);
}
else /* character list */
{
for (j = 0; values[i][j]; j++)
{
sinfo->wordbits[values[i][j]] |= flags;
}
}
/* if first "operator" keyword
* then remember it for use in
* tag searches.
*/
if (!sinfo->opkeyword
&& flags == OPERATOR
&& elvalpha(values[i][0]))
{
sinfo->opkeyword = CHARdup(values[i]);
}
}
}
else if (!strcmp(str, "string"))
{
long li = 0;
/* find item */
if( sinfo->strbegin[li] )
/* already used */
li = 1;
if (values[1] && values[2])
{
sinfo->strbegin[li] = *values[1];
sinfo->strend[li] = *values[2];
}
else if (values[1] && values[1][1])
{
sinfo->strbegin[li] = values[1][0];
sinfo->strend[li] = values[1][1];
}
else if (values[1])
{
sinfo->strbegin[li] =
sinfo->strend[li] = *values[1];
}
}
else if (!strcmp(str, "backslash"))
{
if (!values[1] || elvalpha(values[1][0]))
sinfo->backslash = '\0';
else
sinfo->backslash = values[1][0];
}
else if (!strcmp(str, "strnewline"))
{
str = tochar8(values[1]);
if (!values[1] || !strcmp(str, "backslash"))
sinfo->strnewline = ElvFalse;
else if (!strcmp(str, "allowed"))
sinfo->strnewline = ElvTrue;
else if (!strcmp(str, "indent"))
sinfo->strnewline = sinfo->strnoindent = ElvTrue;
else if (!strcmp(str, "empty"))
sinfo->strnewline = sinfo->strnoempty = ElvTrue;
/* else unknown newline type */
}
else if (!strcmp(str, "character"))
{
if (values[1] && values[2])
{
sinfo->charbegin = *values[1];
sinfo->charend = *values[2];
}
else if (values[1] && values[1][1])
{
sinfo->charbegin = values[1][0];
sinfo->charend = values[1][1];
}
else if (values[1])
{
sinfo->charbegin =
sinfo->charend = *values[1];
}
}
else if (!strcmp(str, "regexp"))
{
for (i = 1; values[i]; i++)
{
for (j = 0; values[i][j]; j++)
{
sinfo->wordbits[values[i][j]] |= DELIMREGEXP;
}
}
}
else if (!strcmp(str, "preprocessor"))
{
if (values[1])
sinfo->preprocessor = *values[1];
}
else if (!strcmp(str, "prepquote"))
{
if (values[1] && values[2])
{
sinfo->pqbegin = *values[1];
sinfo->pqend = *values[2];
}
else if (values[1] && values[1][1])
{
sinfo->pqbegin = values[1][0];
sinfo->pqend = values[1][1];
}
else if (values[1])
{
sinfo->pqbegin =
sinfo->pqend = *values[1];
}
}
else if (!strcmp(str, "function"))
{
if (values[1])
sinfo->function = *values[1];
}
else if (!strcmp(str, "other"))
{
for (i = 1; values[i]; i++)
{
str = tochar8(values[i]);
if (!strcmp(str, "allcaps"))
sinfo->allcaps = (ELVBOOL)!sinfo->allcaps;
else if (!strcmp(str, "initialcaps"))
sinfo->initialcaps = (ELVBOOL)!sinfo->initialcaps;
else if (!strcmp(str, "mixedcaps"))
sinfo->mixedcaps = (ELVBOOL)!sinfo->mixedcaps;
else if (!strcmp(str, "final_t"))
sinfo->finalt = (ELVBOOL)!sinfo->finalt;
else if (!strcmp(str, "initialpunct"))
sinfo->initialpunct = (ELVBOOL)!sinfo->initialpunct;
/* else unknown type */
}
}
else if (!strcmp(str, "startword"))
{
for (i = 1; values[i]; i++)
{
for (j = 0; values[i][j]; j++)
{
sinfo->wordbits[values[i][j]] |= STARTWORD;
}
}
}
else if (!strcmp(str, "inword"))
{
for (i = 1; values[i]; i++)
{
for (j = 0; values[i][j]; j++)
{
sinfo->wordbits[values[i][j]] |= INWORD;
}
}
}
else if (!strcmp(str, "ignorecase"))
{
if (values[1])
sinfo->ignorecase = calctrue(values[1]);
else
sinfo->ignorecase = ElvTrue;
}
else if (!strcmp(str, "color") && values[1] && values[2])
{
i = colorfind(values[1]);
colorset(i, values[2], ElvFalse);
}
else if (!strcmp(str, "set") && values[1])
{
/* merge this "set" with any previous
* "set" arguments.
*/
if (sinfo->setargs)
{
cp = (CHAR *)safealloc(CHARlen(sinfo->setargs) + CHARlen(values[1]) + 2, sizeof(CHAR));
CHARcpy(cp, sinfo->setargs);
CHARcat(cp, toCHAR(" "));
CHARcat(cp, values[1]);
safefree(sinfo->setargs);
sinfo->setargs = cp;
}
else
{
sinfo->setargs = CHARdup(values[1]);
}
}
else if (!strcmp(str, "mostly") && values[1])
{
/* change the "mostly" font */
sinfo->mostly = colorfind(values[1]);
}
else if (!strcmp(str, "documentation") && values[1])
{
CHARncpy(sinfo->enddoc, values[1],
QTY(sinfo->enddoc) - 1);
}
/* else unknown attribute, or missing args */
}
/* If the string allows literal newlines, and the
* same character is used for both the opening
* quote and the closing quote, then we'll have a
* hard time detecting whether a quote character in
* the buffer marks the start of a string, or the
* end of one. We'll assume that no string
* contains a newline followed by whitespace.
*/
if (sinfo->strnewline
&& ( sinfo->strbegin[0] == sinfo->strend[0] ||
sinfo->strbegin[1] == sinfo->strend[1]
)
&& !sinfo->strnoindent)
{
sinfo->strnoempty = ElvTrue;
}
/* close the description file */
descr_close((void *)sinfo);
/* if taglibrary is set, then search for library tags */
if (o_tagkind)
{
/* for each "tags" file in tagpath... */
for (path = tochar8(o_tags);
(str = iopath(path, "tags", ElvTrue)) != NULL;
path = NULL)
{
/* add its tags to the keywords */
sinfo->keyword = telibrary(str, sinfo->keyword, sinfo->ignorecase, toCHAR("kind"));
}
}
if (o_taglibrary)
{
/* for each "tags" file in elvispath... */
for (path = tochar8(o_elvispath);
(str = iopath(path, "tags", ElvTrue)) != NULL;
path = NULL)
{
/* add its tags to the keywords */
sinfo->keyword = telibrary(str, sinfo->keyword, sinfo->ignorecase, toCHAR("lib"));
}
}
}
}
/* set options for this language, if any */
if (sinfo->setargs)
{
/* must use a copy of the string, since optset() alters it */
cp = CHARdup(sinfo->setargs);
(void)optset(ElvTrue, cp, dummy, 0);
safefree(cp);
}
/* return the window's SINFO structure */
return (DMINFO *)sinfo;
}
/* end the mode, and free the modeinfo */
static void term(info)
DMINFO *info; /* window-specific information about mode */
{
#ifdef FEATURE_CACHEDESC
/* Don't delete it -- it is still used in the descr cache */
#else
/* Okay, delete it */
SINFO *si = (SINFO *)info;
/* free the dictionary */
spellfree(si->keyword);
/* free the "operator" keyword, if any */
if (si->opkeyword)
safefree(si->opkeyword);
/* free the sinfo struct itself */
safefree(si);
#endif
}
/* Choose a line to appear at the top of the screen, and return its mark.
* Also, initialize the info for the next line.
*/
static MARK setup(win, top, cursor, bottom, info)
WINDOW win; /* window to be updated */
MARK top; /* where the image drawing began last time */
long cursor; /* cursor's offset into buffer */
MARK bottom; /* where the image drawing ended last time */
DMINFO *info; /* window-specific information about mode */
{
MARK newtop;
ELVBOOL oddquotes;
CHAR *cp, *enddoc;
CHAR following;
ELVBOOL knowstr, knowcom;
/* Use window's info. Variables use this language's "mostly" font */
sinfo = (SINFO *)info;
cfont[VARIABLE] = sinfo->mostly;
/* use the normal mode's setup function to choose the screen top */
newtop = (*dmnormal.setup)(win, top, cursor, bottom, info);
if (!newtop || markoffset(newtop) >= o_bufchars(markbuffer(newtop)))
return newtop;
/* Does this language support some form of embedded documentation? */
if (*sinfo->enddoc)
{
/* The top line could be a continuation of DOC token. To
* find out, scan backward for a line that starts with the
* DOC character. Exception: If the top line itself starts
* with the DOC character then it is definitely a
* continuation (even if it is just "=cut")
*/
if (scanchar(newtop) == *sinfo->enddoc)
{
sinfo->token = STRING0;
return newtop;
}
/* Scan backward for a DOC markup, or strong evidence that
* we're in code. Strong evidence means any punctuation
* character other than ", ', or (.
*/
scanalloc(&cp, newtop);
do
{
/* skip if "following" isn't at start of line */
following = *cp;
scanprev(&cp);
if (cp && *cp != '\n')
continue;
/* if DOC directive, then we have our answer */
if (following == *sinfo->enddoc)
{
/* The answer is PUNCT if enddoc, or DOC
* otherwise. The only tricky part is that
* if we hit the top of the buffer, then cp
* became NULL. In this case, assume DOC.
*/
if (!cp)
{
sinfo->token = DOC;
scanfree(&cp);
return newtop;
}
scannext(&cp);
for (enddoc = sinfo->enddoc;
*cp == *enddoc;
scannext(&cp), enddoc++)
{
}
if (*enddoc || (cp && elvalnum(*cp)))
sinfo->token = DOC;
else
sinfo->token = PUNCT;
scanfree(&cp);
return newtop;
}
} while (cp && (*cp != '\n' || following != sinfo->comment[0]));
/* Either we hit the top without finding a DOC marker, or
* we found strong evidence that we're in normal code.
* Either way, fall through to normal COMMENT/STRINGn test.