/
spell.c
1514 lines (1332 loc) · 38.6 KB
/
spell.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
/* spell.c */
/* This file contains functions which maintain lists of words, with attributes.
* The list is organized as a tree, which allows it to be searched very quickly.
* Multiple lists can be maintained; the root node of the tree is passed as one
* of the arguments. This type of word list is used for storing the keywords
* in the "syntax" display mode, as well as a real spell-checker.
*/
#include "elvis.h"
#ifdef FEATURE_RCSID
char id_spell[] = "$Id: spell.c,v 1.39 2004/03/21 23:24:41 steve Exp $";
#endif
#define MAXWLEN 100 /* maximum word length */
#if defined(FEATURE_SPELL) || defined(DISPLAY_ANYDESCR)
/* Note that even if the spell-checking feature is disabled, we still need
* the following dictionary management functions, because the syntax display
* mode uses dictionaries for storing keywords.
*/
/* Perform spell-checking on a single letter within a word. Initially (at the
* start of a word), the node should be the top node of a dictionary. For
* each successive letter in the word, the node should be the value returned
* by the previous invocation of spellletter().
*
* Returns a new spell_t node if successful, or NULL if the word is definitely
* not in the list. At the end of the word, you should use the SPELL_IS_GOOD()
* macro to determine whether the word is in the dictionary.
*/
spell_t *spellletter(node, letter)
spell_t *node; /* top of dictionary, or value returned by previous call */
_CHAR_ letter; /* the first/next letter to check */
{
if (!node || letter < node->min || letter > node->max)
return NULL;
return node->link[letter - node->min];
}
/* Perform spell checking on a whole word. If len is 0 then the word is
* assumed to be NUL-terminated; else the word's length is the lesser of len
* or CHARlen(word).
*
* Returns NULL if unknown word, or the spell_t for a known or partial word.
* You should use the SPELL_IS_GOOD() macro to determine whether the return
* value indicates that the word is in the dictionary.
*/
spell_t *spellfindword(node, word, len)
spell_t *node; /* top of the dictionary */
CHAR *word; /* the word to check */
int len; /* maximum length to check, or 0 for any length */
{
/* the default length is determined by looking for a NUL terminator */
if (len == 0)
len = CHARlen(word);
/* check each letter of the word */
for (; len > 0 && *word; len--, word++)
{
/* check the next letter, ignoring case differences */
node = spellletter(node, *word);
if (!node)
break;
}
/* return the final result */
return node;
}
/* Add a word to a dictionary, and return the dictionary's root node.
* Initially (before the first word has been added) the dictionary's root
* node should be NULL. Thereafter, the root node should be whatever this
* function returns.
*
* The 28 least-significant bits of "flags" are unspecified; the calling
* function can use them to store extra information about the word. The 4
* most-significant bits are used for the SPELL_FLAG_COMPLETE, SPELL_FLAG_BAD,
* and SPELL_FLAG_PERSONAL flags. (The last bit is currently unused.)
* spelladdword() always turns on the SPELL_FLAG_COMPLETE bit, so often
* you'll simply leave those 4 bits off.
*
* Later, when you look up a word via spellletter() or spellfindword(), if
* SPELL_IS_GOOD() indicates that the word is in the dictionary, you can use
* (node->flags) to access the word's flags.
*/
spell_t *spelladdword(node, word, flags)
spell_t *node; /* root node of a dictionary */
CHAR *word; /* NULL-terminated word to add */
long flags; /* bitmap of flags */
{
int c, i;
spell_t *newnode;
#ifndef NDEBUG
for (i = 0; word[i]; i++)
assert(!elvspace(word[i]));
assert(i < MAXWLEN);
#endif
#if 0
if (~flags & SPELL_FLAG_COMPLETE)
printf("spelladdword(%s, \"%s\", 0x%lx)\n",
!node?"NULL":node==spellwords?"spellwords":"spelltags",
word, flags);
#endif
/* always set the SPELL_FLAG_COMPLETE flag */
flags |= SPELL_FLAG_COMPLETE;
/* if there is no such node, then create one */
if (!node)
{
node = (spell_t *)safekept(1, sizeof(spell_t));
}
/* if at end of word, then just store the flags and return */
if (!*word)
{
if (node)
node->flags = flags;
return node;
}
/* if this char won't fit in this node, then reallocate node */
c = *word;
if (node->max == 0) /* empty? will this be the first link? */
{
/* link[] already has 1 element, so this will fit */
node->min = node->max = c;
}
else if (c < node->min) /* before current min? */
{
newnode = (spell_t *)safekept(1, sizeof(spell_t) + (node->max - c + 1) * sizeof(spell_t *));
newnode->flags = node->flags;
newnode->min = c;
newnode->max = node->max;
for (i = 0; i <= node->max - node->min; i++)
newnode->link[node->min - c + i] = node->link[i];
safefree(node);
node = newnode;
}
else if (c > node->max) /* after current max? */
{
newnode = (spell_t *)safekept(1, sizeof(spell_t) + (c - node->min + 1) * sizeof(spell_t *));
newnode->flags = node->flags;
newnode->min = node->min;
newnode->max = c;
for (i = 0; i <= node->max - node->min; i++)
newnode->link[i] = node->link[i];
safefree(node);
node = newnode;
}
/* compute the index of this char's node->link[] pointer */
i = c - node->min;
/* recursively add the remainder of the word */
node->link[i] = spelladdword(node->link[i], word + 1, flags);
/* return this node */
return node;
}
/* Discard a spelling dictionary */
void spellfree(node)
spell_t *node; /* root node of a dictionary tree to free */
{
int i;
/* if NULL, then do nothing */
if (!node)
return;
/* recursively free any subtrees */
for (i = 0; i <= node->max - node->min; i++)
spellfree(node->link[i]);
/* free this node */
safefree(node);
}
#endif /* FEATURE_SPELL or DISPLAY_ANYDESCR */
#ifdef FEATURE_SPELL
static void wildspell P_((spell_t *node, CHAR *word, CHAR *partial));
static void savehelp P_((spell_t *node, CHAR *partial));
static void spelldraw P_((CHAR *p, long qty, _char_ font, long offset));
/* font to use to highlight spelling errors */
char spellfont;
/* dictionaries */
spell_t *spelltags; /* spelling words from "tags" files */
spell_t *spellwords; /* spelling words from "words" file, or :words cmd */
/* font-dependent spellcheck limits */
static spellcheck_t spelllimit[128];
static ELVBOOL spellset[128];
/* info about the last value displayed via the "spellshow" option, or returned
* by the last spellfix() function call.
*/
static MARKBUF showbegin, showend; /* extents of the word */
static CHAR showsuggest[100]; /* suggested alternatives */
static long showchange; /* used to detect changes */
/* prepare for doing some spell-checking. This updates the "tags" dictionary
* if there are any user buffers which have "spell" set.
*/
void spellbegin()
{
char *newstamp;
static char oldstamp[20]; /* YYYY-MM-DDThh:mm:ss */
char *path;
char *fullname;
CHAR tagname[MAXWLEN];
BUFFER buf;
int c, i;
FILE *fp;
/* locate font (only required the first time) */
if (!spellfont)
{
spellfont = colorfind(toCHAR("spell"));
colorset(spellfont, toCHAR("underlined"), ElvFalse);
}
/* if no user buffers have "spell" set, then do nothing */
for (buf = buflist((BUFFER)NULL);
buf && (o_internal(buf) || !o_spell(buf));
buf = buflist(buf))
{
}
if (!buf)
return;
/* if timestamp of "tags" hasn't changed, then do nothing */
newstamp = dirtime("tags");
if (!newstamp || !strcmp(newstamp, oldstamp))
return;
strcpy(oldstamp, newstamp);
/* free the old spelltags dictionary, if any */
if (spelltags)
{
spellfree(spelltags);
spelltags = NULL;
}
/* for each file in the elvispath or tagpath... */
for (path = tochar8(o_elvispath ? o_elvispath : o_tags);
path;
path = (path == tochar8(o_tags)) ? NULL : tochar8(o_tags))
{
for (fullname = iopath(path, "tags", ElvTrue);
fullname;
fullname = iopath(NULL, "tags", ElvTrue))
{
/* for each tag in the file... */
fp = fopen(fullname, "r");
if (!fp)
continue;
for (i = 0; (c = getc(fp)) != EOF; )
{
if (c == '\n')
i = 0;
else if (i < 0)
continue;
else if (elvspace(c))
{
tagname[i] = '\0';
if (tagname[0] > '!' && i < MAXWLEN - 1)
spelltags = spelladdword(spelltags, tagname, 0);
i = -1;
}
else if (i < MAXWLEN - 2)
tagname[i++] = c;
}
fclose(fp);
}
}
}
/* This file pointer refers to a list of natural-language words */
static FILE *wordfp;
static long wordsize;
/* Search through the word file for a given word, using a binary search.
* If found, then add the word to the given dictionary. If not found, then
* still add it but with the SPELL_FLAG_BAD bit set. Return TRUE if good,
* FALSE if bad.
*/
ELVBOOL spellsearch(word)
CHAR *word; /* word to look for */
{
long pos, step;
int c;
CHAR *letterptr;
/* if the file hasn't been opened yet, then open it now */
if (!wordfp)
{
/* if no file is specified, then don't bother */
if (!o_spelldict)
return ElvFalse;
/* try to open the file. If error, then clobber the option */
wordfp = fopen(tochar8(o_spelldict), "rb");
if (!wordfp)
{
optputstr(toCHAR("spelldict"), toCHAR(""), ElvFalse);
return ElvFalse;
}
/* get the size of the file */
fseek(wordfp, 0L, SEEK_END);
wordsize = ftell(wordfp);
}
/* perform binary search until within a few bytes of word */
pos = 0;
step = wordsize / 2;
while (step > 100)
{
/* find start of line after seek position */
fseek(wordfp, pos + step, SEEK_SET);
while ((c = getc(wordfp)) != '\n' && c != EOF)
{
}
/* compare to word */
for (letterptr = word;
(c = elvtolower(getc(wordfp))) == *letterptr;
letterptr++)
{
}
/* exact match, by any chance? */
if (*letterptr == '\0' && elvspace(c))
{
spellwords = spelladdword(spellwords, word, 0);
return ElvTrue;
}
/* if EOF or greater then we want to step back; else forward */
if (c != EOF && c < *letterptr)
pos += step;
step /= 2;
}
/* we're close. skip to start of next line, and then read linearly */
fseek(wordfp, pos, SEEK_SET);
if (pos != 0)
{
while ((c = getc(wordfp)) != '\n' && c != EOF)
{
}
}
/* now read each word linearly, comparing to desired word */
letterptr = word;
while ((c = elvtolower(getc(wordfp))) != EOF && c <= *letterptr)
{
if (c == '\r')
continue;
else if (c == '\n')
{
if (*letterptr == '\0')
break;
else
letterptr = word;
}
else if (c == *letterptr)
letterptr++;
else
{
/* mismatch -- skip to next word */
while ((c = getc(wordfp)) != EOF && c != '\n')
{
}
letterptr = word;
}
}
/* if the above search was successful, then *letterptr should point
* to the NUL at the end of the word and c should be non-whitespace.
* Any other result means the word wasn't found.
*/
if (*letterptr || !elvspace(c))
{
spellwords = spelladdword(spellwords, word, SPELL_FLAG_BAD);
return ElvFalse;
}
else
{
spellwords = spelladdword(spellwords, word, 0);
return ElvTrue;
}
}
void spellend()
{
/* if the words file is open, close it */
if (wordfp)
{
fclose(wordfp);
wordfp = NULL;
}
}
/* Given a MARK for the start of a word, perform spell checking on the
* word, and return the result. Move the mark past the end of the word.
*/
spellresult_t spellcheck(mark, tagonly, cursoff)
MARK mark; /* start of word */
ELVBOOL tagonly; /* skip the natural-language dictionary? */
long cursoff; /* offset of cursor */
{
spell_t *tag = spelltags;
spell_t *word = tagonly ? NULL : spellwords;
spell_t *icword = word;
CHAR *cp, letter;
CHAR wordbuf[MAXWLEN], swordbuf[MAXWLEN];
int wlen, slen, i;
spellresult_t result;
long wordoff;
#if 0
disp_t *dm;
/* choose the display mode, via the bufdisplay option */
dm = ...
#endif
/* get the first letter. The character at the mark is assumed to
* be the first letter of the word, even if it isn't a letter.
*/
scanalloc(&cp, mark);
wordoff = markoffset(mark);
#if 0
if (dm->wordletter)
letter = (*dm->wordletter)(cp, ElvTrue);
else
#endif
{
letter = *cp;
scannext(&cp);
}
/* check each character of the word */
wlen = 0;
do
{
/* add this letter to the word buffer */
if (wlen < MAXWLEN)
wordbuf[wlen++] = elvtolower(letter);
/* look up this letter in the relevant dictionaries */
tag = spellletter(tag, letter);
word = spellletter(word, letter);
icword = spellletter(icword, elvtolower(letter));
/* get the next letter */
if (!cp)
letter = '\0';
else if (elvalnum(*cp) || *cp == '_')
{
letter = *cp;
scannext(&cp);
}
else if (*cp == '\'')
{
letter = *cp;
scannext(&cp);
if (!cp || !elvalpha(*cp))
{
if (cp)
scanprev(&cp);
letter = '\0';
}
}
#if 0
else if (dm->wordletter)
letter = (*dm->wordletter)(cp, ElvFalse);
#endif
else
letter = '\0';
} while (letter);
/* Move the mark past the end of the word. We do this regardless of
* whether the word is correct or not. This is slightly complicated
* by the fact that we may have hit the end of the buffer, in which
* case cp would be NULL.
*/
if (cp)
marksetoffset(mark, markoffset(scanmark(&cp)));
else
marksetoffset(mark, o_bufchars(markbuffer(mark)));
scanfree(&cp);
/* if too long to be a valid word, then it must be bad */
if (wlen >= MAXWLEN)
return SPELL_BAD;
wordbuf[wlen] = '\0';
/* Decide whether to use "tag" or "word"/"icword". We want to use
* "tag" if "word"/"icword" is NULL or incomplete; otherwise use
* "word"/"icword".
*/
if (!word || (~word->flags & SPELL_FLAG_COMPLETE))
word = icword;
if (!word || (tag && ((~word->flags & SPELL_FLAG_COMPLETE) || (word->flags & SPELL_FLAG_BAD))))
word = tag;
/* If the cursor is inside or at the end of an unknown or incomplete
* word, then pretend it is known to be "good".
*/
if (wordoff <= cursoff
&& cursoff <= markoffset(mark)
&& (!word || (~word->flags & SPELL_FLAG_COMPLETE)))
return SPELL_GOOD;
/* If unknown, and the natural-language dictionary is allowed, then
* search for the word. Exception: If the cursor is located at the
* end of an incomplete word, then don't try this until the user
* completes the word.
*/
if (!tagonly
&& o_spelldict
&& (!word || (~word->flags & SPELL_FLAG_COMPLETE)))
{
if (!spellsearch(wordbuf) && o_spellsuffix)
{
/* not found as given -- try suffix substitution */
for (cp = o_spellsuffix; *cp; cp++)
{
/* skip whitespace between suffixes */
if (elvspace(*cp))
continue;
/* count the length of this suffix */
for (slen = 0; cp[slen] && cp[slen] != '=' && !elvspace(cp[slen]); slen++)
{
}
/* does the word end with this suffix? */
if (slen < wlen && !CHARncmp(wordbuf + wlen - slen, cp, slen))
{
/* Yes! Use this suffix */
CHARcpy(swordbuf, wordbuf);
if (cp[slen] == '=')
{
for (i = 0; cp[slen + 1 + i] && !elvspace(cp[slen + 1 + i]); i++)
swordbuf[wlen - slen + i] = cp[slen + 1 + i];
swordbuf[wlen - slen + i] = '\0';
}
else
swordbuf[wlen - slen] = '\0';
/* Search for the alternate-suffix word.
* If that word is found and is good,
* then also add the word as it appears
* in the buffer.
*/
word = spellfindword(spellwords,swordbuf,0);
if (SPELL_IS_GOOD(word)
|| spellsearch(swordbuf))
{
spellwords = spelladdword(spellwords, wordbuf, 0);
}
else
{
spellwords = spelladdword(spellwords, wordbuf, SPELL_FLAG_BAD);
}
/* Stop after trying one suffix */
break;
}
/* skip to end of this suffix */
while (*cp && !elvspace(*cp))
cp++;
}
}
word = spellfindword(spellwords, wordbuf, wlen);
}
/* Classify the results */
if (!word)
result = SPELL_UNKNOWN;
else if (~word->flags & SPELL_FLAG_COMPLETE)
result = SPELL_INCOMPLETE;
else if (word->flags & SPELL_FLAG_BAD)
result = SPELL_BAD;
else
result = SPELL_GOOD;
return result;
}
/* Locate each word on the screen, and highlight any that appear to be spelled
* incorrectly. Note that the attributes affect the spelling rules, and also
* that in "markup" display modes, the markups themselves are normally hidden
* and hence won't be spell checked.
*/
void spellhighlight(win)
WINDOW win;
{
MARKBUF mark;
int i, bottom;
int offset;
char newfont, combofont;
long end;
CHAR *cp;
spellresult_t result;
ELVBOOL inputmode; /* are we in input mode? */
/* if spell-checking is disabled, then do nothing */
if (!o_spell(markbuffer(win->cursor)))
return;
/* spell checking makes no sense in "hex" mode. In fact, it is worse
* than useless since a punctuation character's hex value may look like
* a two-letter word on the screen, which confuses the spell checker.
*/
if (win->md == &dmhex)
return;
/* determine whether we're in input mode */
inputmode = (ELVBOOL)((win->state->mapflags & MAP_INPUT) != 0);
/* for each word on the screen... */
scanalloc(&cp, win->di->topmark);
offset = markoffset(win->di->topmark);
end = markoffset(win->di->bottommark);
i = 0;
bottom = (win->di->rows - 1) * win->di->columns;
mark.buffer = markbuffer(win->di->topmark);
for (; cp && offset < end; scannext(&cp), offset++)
{
/* skip if not on screen */
while (i < bottom && win->di->offsets[i] < offset)
i++;
if (i > bottom)
break;
if (win->di->offsets[i] > offset)
continue;
/* Skip if non-alphanumeric */
if (!elvalnum(*cp) && *cp != '_')
#if 0
&& dm->wordletter && !(*dm->wordletter)(cp, ElvTrue))
#endif
continue;
/* skip if font has "graphic" attribute or SPELL_CHECK_NONE */
newfont = (win->di->newfont[i] & 0x7f);
if (colorinfo[(int)newfont].da.bits & COLOR_GRAPHIC
|| spelllimit[(int)newfont] == SPELL_CHECK_NONE)
continue;
/* okay, we have found a word. "cp" points to it, "offset" is
* its offset, and "i" is its position in the screen. Now we
* need to check the status of that word. (This also locates
* the end of the word, by moving "mark".)
*/
mark.offset = offset;
result = spellcheck(&mark,
(ELVBOOL)(spelllimit[(int)newfont] == SPELL_CHECK_TAGONLY),
inputmode ? markoffset(win->cursor) : INFINITY);
/* if word began with a digit, then ignore errors */
if (elvdigit(*cp))
result = SPELL_GOOD;
/* if sidescrolling is enabled, and this word began at the
* left edge of the screen, and that edge isn't the start of
* a line, then what we've really done here is spell-check
* a partial word, not a whole word. To avoid possible false
* alarms, skip this word. (We still had to call spellcheck()
* to find the end of the word.)
*/
if (!o_wrap(win)
&& (i % win->di->columns) == 0
&& win->di->skipped > 0)
{
/* move to the end of the word. */
offset = mark.offset;
scanseek(&cp, &mark);
continue;
}
/* if the word is bad, then highlight it */
if (result == SPELL_BAD
|| result == SPELL_UNKNOWN
|| (result == SPELL_INCOMPLETE && markoffset(win->cursor) != mark.offset))
{
while (i < bottom
&& win->di->offsets[i] >= offset
&& win->di->offsets[i] < mark.offset)
{
newfont = win->di->newfont[i];
if (!(newfont & 0x7f))
newfont |= o_hasfocus(win)
? COLOR_FONT_NORMAL
: COLOR_FONT_IDLE;
combofont = win->di->newfont[i] =
colortmp(newfont, spellfont);
i++;
}
}
/* move to the end of the word. */
offset = mark.offset;
scanseek(&cp, &mark);
}
scanfree(&cp);
}
static BUFFER savebuf; /* buf to append lines to, or NULL to show */
static CHAR saveline[200]; /* buffer, holds a single line of words */
static CHAR savecmd[20]; /* beginning of line: either "" or "try words"*/
static CHAR saveword[MAXWLEN]; /* current word, or partial word */
static CHAR saveflag; /* flag within saveline: either '+' or '-' */
static long savecount; /* number of personal words found */
static void savehelp(node, partial)
spell_t *node; /* node to dump */
CHAR *partial;/* partial word */
{
CHAR flag[2];
MARKBUF end;
int i;
/* if no such node, then do nothing */
if (!node)
return;
/* if this is a personal word, then dump it */
if (node->flags & SPELL_FLAG_PERSONAL)
{
/* mark the end of this word */
*partial = '\0';
/* determine whether the flag is + or - */
if (node->flags & SPELL_FLAG_BAD)
flag[0] = '-';
else
flag[0] = '+';
flag[1] = '\0';
/* if it won't fit on this line, then start a new one */
if (CHARlen(saveline) + (partial - saveword) > 77)
{
CHARcat(saveline, toCHAR("\n"));
if (savebuf)
{
end.buffer = savebuf;
end.offset = o_bufchars(savebuf);
bufreplace(&end, &end, saveline, CHARlen(saveline));
}
else
drawextext(windefault, saveline, CHARlen(saveline));
CHARcpy(saveline, savecmd);
saveflag = '+';
}
/* add the word */
if (*saveline)
CHARcat(saveline, toCHAR(" "));
if (*flag != saveflag)
{
CHARcat(saveline, flag);
saveflag = *flag;
}
CHARcat(saveline, saveword);
savecount++;
}
/* check longer words too */
for (i = node->min; i <= node->max; i++)
{
*partial = i;
savehelp(node->link[*partial - node->min], partial+1);
}
}
/* This function walks through the spellwords tree, and outputs any any
* personal words that it finds there. This is used for both :mkexrc,
* and for listing the words via :word without arguments.
*/
void spellsave(custom)
BUFFER custom; /* buffer to append spell commands to, or NULL */
{
int i, col, len;
spellcheck_t level;
/* use savehelp() to recursively search for personal words */
savebuf = custom;
if (custom)
CHARcpy(savecmd, toCHAR("try words"));
else
*savecmd = '\0';
CHARcpy(saveline, savecmd);
saveflag = '+';
savecount = 0L;
savehelp(spellwords, saveword);
/* finish the last command */
if (savecount > 0)
{
CHARcat(saveline, toCHAR("\n"));
if (custom)
{
bufappend(custom, saveline, 0);
}
else
{
drawextext(windefault, saveline, CHARlen(saveline));
}
}
/* if just displaying, then we're done. */
if (!custom)
return;
/* also save the :check commands */
for (level = SPELL_CHECK_ALL; level <= SPELL_CHECK_NONE; level++)
{
for (i = 1, col = 0; i < colornpermanent; i++)
{
if (spelllimit[i] != level || !spellset[i])
continue;
/* start a new command? */
len = CHARlen(colorinfo[i].name);
if (col == 0 || col + len > 77)
{
if (col > 0)
{
bufappend(custom, toCHAR("\n"), 1);
}
bufappend(custom,
toCHAR(level == SPELL_CHECK_NONE
? "try check -"
: level == SPELL_CHECK_TAGONLY
? "try check +"
: "try check *"),
11);
col = 11;
}
else
{
/* space between words */
bufappend(custom, blanks, 1);
col++;
}
/* add this font name */
bufappend(custom, colorinfo[i].name, len);
}
/* end the line */
if (col > 0)
{
bufappend(custom, toCHAR("\n"), 1);
}
}
}
/* These indicate where any found matches should go */
static CHAR *wildbuf; /* buffer */
static int wildlen; /* size of wildbuf */
static int wildused; /* used portion of wildbuf */
static ELVBOOL wildcap; /* should matching words be capitalized? */
/* Look up a potential corrected spelling. This differs from spellfindword()
* in the way it handles case mismatches, and also it allows ' ' as a wildcard
* meaning "any single character". If the word is found, then it will be
* copied from the "partial" buffer onto the end of the "wildbuf".
*/
static void wildspell(node, word, partial)
spell_t *node; /* tree to search */
CHAR *word; /* partial candidate word to search for */
CHAR *partial; /* partial actual word found */
{
int len, i;
/* if no node, then do nothing */
if (!node)
return;
/* if reached end of word, and it is complete, then use it */
if (!*word)
{
len = (int)(partial - saveline);
if (SPELL_IS_GOOD(node)
&& (int)(partial - saveline) < wildlen - wildused - 2
&& (wildused < len || CHARncmp(saveline, wildbuf + wildused - len, len)))
{
if (wildused != 0)
wildbuf[wildused++] = ' ';
CHARncpy(&wildbuf[wildused], saveline, len);
if (wildcap)
wildbuf[wildused] = elvtoupper(*saveline);
wildused += len;
}
return;
}
/* if ' ' then try every character */
if (*word == ' ')
{
len = node->max;
for (i = node->min; i <= node->max; i++)
{
*partial = (CHAR)i;
if (node->link[*partial - node->min])
wildspell(node->link[*partial - node->min],
word + 1, partial + 1);
}
}
else
{
/* try an exact match for this character */
*partial = *word;
if (*partial >= node->min && *partial <= node->max
&& node->link[*partial - node->min])
wildspell(node->link[*partial - node->min],
word + 1, partial + 1);
/* if lowercase, then try an uppercase version of the same */
if (elvlower(*word))
{
*partial = elvtoupper(*word);
if (*partial >= node->min && *partial <= node->max
&& node->link[*partial - node->min])
wildspell(node->link[*partial - node->min], word + 1, partial + 1);
}
}
}
/* Perform spell-fixing on a whole word.
*
* The result is stored into the "result" CHAR buffer. This will be "" for
* valid words, "?" for invalid words with no suggestions, or a space-delimited
* list of suggestions for invalid words that can be derived from valid ones.
*/
void spellfix(word, result, resultlen, tagonly)
CHAR *word; /* the word to fix */
CHAR *result; /* where to store the result */
int resultlen; /* maximum length of result */
ELVBOOL tagonly; /* ignore natural-language dictionary */
{
int i;
CHAR tmp, tmp2;
int len; /* length of word */
spell_t *dict, *found;
CHAR *wordcopy;
static CHAR *loadeddict;
/* if given "" or number, then return "" */
if (!*word || elvdigit(*word))
{
*result = '\0';
return;
}
/* lookup the word unchanged. If exactly right then return "" */
found = spellfindword(spelltags, word, 0);
if (SPELL_IS_GOOD(found))
{
*result = '\0';
return;
}
if (!tagonly)
{
found = spellfindword(spellwords, word, 0);
if (SPELL_IS_GOOD(found))
{
*result = '\0';
return;
}
}
/* Detect whether the word is capitalized -- meaning the first
* character is uppercase but all other characters are lowercase
*/
wildcap = (ELVBOOL)elvupper(word[0]);
for (len = 1; wildcap && word[len]; len++)
if (elvupper(word[len]))