/
spellsuggest.c
4493 lines (4111 loc) · 119 KB
/
spellsuggest.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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* spellsuggest.c: functions for spelling suggestions
*/
#include "vim.h"
#if defined(FEAT_SPELL) || defined(PROTO)
/*
* Use this to adjust the score after finding suggestions, based on the
* suggested word sounding like the bad word. This is much faster than doing
* it for every possible suggestion.
* Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
* vs "ht") and goes down in the list.
* Used when 'spellsuggest' is set to "best".
*/
#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
/*
* Do the opposite: based on a maximum end score and a known sound score,
* compute the maximum word score that can be used.
*/
#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
// only used for su_badflags
#define WF_MIXCAP 0x20 // mix of upper and lower case: macaRONI
/*
* Information used when looking for suggestions.
*/
typedef struct suginfo_S
{
garray_T su_ga; // suggestions, contains "suggest_T"
int su_maxcount; // max. number of suggestions displayed
int su_maxscore; // maximum score for adding to su_ga
int su_sfmaxscore; // idem, for when doing soundfold words
garray_T su_sga; // like su_ga, sound-folded scoring
char_u *su_badptr; // start of bad word in line
int su_badlen; // length of detected bad word in line
int su_badflags; // caps flags for bad word
char_u su_badword[MAXWLEN]; // bad word truncated at su_badlen
char_u su_fbadword[MAXWLEN]; // su_badword case-folded
char_u su_sal_badword[MAXWLEN]; // su_badword soundfolded
hashtab_T su_banned; // table with banned words
slang_T *su_sallang; // default language for sound folding
} suginfo_T;
// One word suggestion. Used in "si_ga".
typedef struct suggest_S
{
char_u *st_word; // suggested word, allocated string
int st_wordlen; // STRLEN(st_word)
int st_orglen; // length of replaced text
int st_score; // lower is better
int st_altscore; // used when st_score compares equal
int st_salscore; // st_score is for soundalike
int st_had_bonus; // bonus already included in score
slang_T *st_slang; // language used for sound folding
} suggest_T;
#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
// TRUE if a word appears in the list of banned words.
#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
// Number of suggestions kept when cleaning up. We need to keep more than
// what is displayed, because when rescore_suggestions() is called the score
// may change and wrong suggestions may be removed later.
#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
// Threshold for sorting and cleaning up suggestions. Don't want to keep lots
// of suggestions that are not going to be displayed.
#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
// score for various changes
#define SCORE_SPLIT 149 // split bad word
#define SCORE_SPLIT_NO 249 // split bad word with NOSPLITSUGS
#define SCORE_ICASE 52 // slightly different case
#define SCORE_REGION 200 // word is for different region
#define SCORE_RARE 180 // rare word
#define SCORE_SWAP 75 // swap two characters
#define SCORE_SWAP3 110 // swap two characters in three
#define SCORE_REP 65 // REP replacement
#define SCORE_SUBST 93 // substitute a character
#define SCORE_SIMILAR 33 // substitute a similar character
#define SCORE_SUBCOMP 33 // substitute a composing character
#define SCORE_DEL 94 // delete a character
#define SCORE_DELDUP 66 // delete a duplicated character
#define SCORE_DELCOMP 28 // delete a composing character
#define SCORE_INS 96 // insert a character
#define SCORE_INSDUP 67 // insert a duplicate character
#define SCORE_INSCOMP 30 // insert a composing character
#define SCORE_NONWORD 103 // change non-word to word char
#define SCORE_FILE 30 // suggestion from a file
#define SCORE_MAXINIT 350 // Initial maximum score: higher == slower.
// 350 allows for about three changes.
#define SCORE_COMMON1 30 // subtracted for words seen before
#define SCORE_COMMON2 40 // subtracted for words often seen
#define SCORE_COMMON3 50 // subtracted for words very often seen
#define SCORE_THRES2 10 // word count threshold for COMMON2
#define SCORE_THRES3 100 // word count threshold for COMMON3
// When trying changed soundfold words it becomes slow when trying more than
// two changes. With less than two changes it's slightly faster but we miss a
// few good suggestions. In rare cases we need to try three of four changes.
#define SCORE_SFMAX1 200 // maximum score for first try
#define SCORE_SFMAX2 300 // maximum score for second try
#define SCORE_SFMAX3 400 // maximum score for third try
#define SCORE_BIG SCORE_INS * 3 // big difference
#define SCORE_MAXMAX 999999 // accept any score
#define SCORE_LIMITMAX 350 // for spell_edit_score_limit()
// for spell_edit_score_limit() we need to know the minimum value of
// SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS
#define SCORE_EDIT_MIN SCORE_SIMILAR
/*
* For finding suggestions: At each node in the tree these states are tried:
*/
typedef enum
{
STATE_START = 0, // At start of node check for NUL bytes (goodword
// ends); if badword ends there is a match, otherwise
// try splitting word.
STATE_NOPREFIX, // try without prefix
STATE_SPLITUNDO, // Undo splitting.
STATE_ENDNUL, // Past NUL bytes at start of the node.
STATE_PLAIN, // Use each byte of the node.
STATE_DEL, // Delete a byte from the bad word.
STATE_INS_PREP, // Prepare for inserting bytes.
STATE_INS, // Insert a byte in the bad word.
STATE_SWAP, // Swap two bytes.
STATE_UNSWAP, // Undo swap two characters.
STATE_SWAP3, // Swap two characters over three.
STATE_UNSWAP3, // Undo Swap two characters over three.
STATE_UNROT3L, // Undo rotate three characters left
STATE_UNROT3R, // Undo rotate three characters right
STATE_REP_INI, // Prepare for using REP items.
STATE_REP, // Use matching REP items from the .aff file.
STATE_REP_UNDO, // Undo a REP item replacement.
STATE_FINAL // End of this node.
} state_T;
/*
* Struct to keep the state at each level in suggest_try_change().
*/
typedef struct trystate_S
{
state_T ts_state; // state at this level, STATE_
int ts_score; // score
idx_T ts_arridx; // index in tree array, start of node
short ts_curi; // index in list of child nodes
char_u ts_fidx; // index in fword[], case-folded bad word
char_u ts_fidxtry; // ts_fidx at which bytes may be changed
char_u ts_twordlen; // valid length of tword[]
char_u ts_prefixdepth; // stack depth for end of prefix or
// PFD_PREFIXTREE or PFD_NOPREFIX
char_u ts_flags; // TSF_ flags
char_u ts_tcharlen; // number of bytes in tword character
char_u ts_tcharidx; // current byte index in tword character
char_u ts_isdiff; // DIFF_ values
char_u ts_fcharstart; // index in fword where badword char started
char_u ts_prewordlen; // length of word in "preword[]"
char_u ts_splitoff; // index in "tword" after last split
char_u ts_splitfidx; // "ts_fidx" at word split
char_u ts_complen; // nr of compound words used
char_u ts_compsplit; // index for "compflags" where word was spit
char_u ts_save_badflags; // su_badflags saved here
char_u ts_delidx; // index in fword for char that was deleted,
// valid when "ts_flags" has TSF_DIDDEL
} trystate_T;
// values for ts_isdiff
#define DIFF_NONE 0 // no different byte (yet)
#define DIFF_YES 1 // different byte found
#define DIFF_INSERT 2 // inserting character
// values for ts_flags
#define TSF_PREFIXOK 1 // already checked that prefix is OK
#define TSF_DIDSPLIT 2 // tried split at this point
#define TSF_DIDDEL 4 // did a delete, "ts_delidx" has index
// special values ts_prefixdepth
#define PFD_NOPREFIX 0xff // not using prefixes
#define PFD_PREFIXTREE 0xfe // walking through the prefix tree
#define PFD_NOTSPECIAL 0xfd // highest value that's not special
static void spell_find_suggest(char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive);
#ifdef FEAT_EVAL
static void spell_suggest_expr(suginfo_T *su, char_u *expr);
#endif
static void spell_suggest_file(suginfo_T *su, char_u *fname);
static void spell_suggest_intern(suginfo_T *su, int interactive);
static void spell_find_cleanup(suginfo_T *su);
static void suggest_try_special(suginfo_T *su);
static void suggest_try_change(suginfo_T *su);
static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold);
static void go_deeper(trystate_T *stack, int depth, int score_add);
static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword);
static void score_comp_sal(suginfo_T *su);
static void score_combine(suginfo_T *su);
static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound);
static void suggest_try_soundalike_prep(void);
static void suggest_try_soundalike(suginfo_T *su);
static void suggest_try_soundalike_finish(void);
static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp);
static int soundfold_find(slang_T *slang, char_u *word);
static int similar_chars(slang_T *slang, int c1, int c2);
static void add_suggestion(suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf);
static void check_suggestions(suginfo_T *su, garray_T *gap);
static void add_banned(suginfo_T *su, char_u *word);
static void rescore_suggestions(suginfo_T *su);
static void rescore_one(suginfo_T *su, suggest_T *stp);
static int cleanup_suggestions(garray_T *gap, int maxscore, int keep);
static int soundalike_score(char_u *goodsound, char_u *badsound);
static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword);
static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit);
static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit);
/*
* Return TRUE when the sequence of flags in "compflags" plus "flag" can
* possibly form a valid compounded word. This also checks the COMPOUNDRULE
* lines if they don't contain wildcards.
*/
static int
can_be_compound(
trystate_T *sp,
slang_T *slang,
char_u *compflags,
int flag)
{
// If the flag doesn't appear in sl_compstartflags or sl_compallflags
// then it can't possibly compound.
if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
? slang->sl_compstartflags : slang->sl_compallflags, flag))
return FALSE;
// If there are no wildcards, we can check if the flags collected so far
// possibly can form a match with COMPOUNDRULE patterns. This only
// makes sense when we have two or more words.
if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
{
int v;
compflags[sp->ts_complen] = flag;
compflags[sp->ts_complen + 1] = NUL;
v = match_compoundrule(slang, compflags + sp->ts_compsplit);
compflags[sp->ts_complen] = NUL;
return v;
}
return TRUE;
}
/*
* Adjust the score of common words.
*/
static int
score_wordcount_adj(
slang_T *slang,
int score,
char_u *word,
int split) // word was split, less bonus
{
hashitem_T *hi;
wordcount_T *wc;
int bonus;
int newscore;
hi = hash_find(&slang->sl_wordcount, word);
if (!HASHITEM_EMPTY(hi))
{
wc = HI2WC(hi);
if (wc->wc_count < SCORE_THRES2)
bonus = SCORE_COMMON1;
else if (wc->wc_count < SCORE_THRES3)
bonus = SCORE_COMMON2;
else
bonus = SCORE_COMMON3;
if (split)
newscore = score - bonus / 2;
else
newscore = score - bonus;
if (newscore < 0)
return 0;
return newscore;
}
return score;
}
/*
* Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
* capital. So that make_case_word() can turn WOrd into Word.
* Add ALLCAP for "WOrD".
*/
static int
badword_captype(char_u *word, char_u *end)
{
int flags = captype(word, end);
int c;
int l, u;
int first;
char_u *p;
if (flags & WF_KEEPCAP)
{
// Count the number of UPPER and lower case letters.
l = u = 0;
first = FALSE;
for (p = word; p < end; MB_PTR_ADV(p))
{
c = PTR2CHAR(p);
if (SPELL_ISUPPER(c))
{
++u;
if (p == word)
first = TRUE;
}
else
++l;
}
// If there are more UPPER than lower case letters suggest an
// ALLCAP word. Otherwise, if the first letter is UPPER then
// suggest ONECAP. Exception: "ALl" most likely should be "All",
// require three upper case letters.
if (u > l && u > 2)
flags |= WF_ALLCAP;
else if (first)
flags |= WF_ONECAP;
if (u >= 2 && l >= 2) // maCARONI maCAroni
flags |= WF_MIXCAP;
}
return flags;
}
/*
* Opposite of offset2bytes().
* "pp" points to the bytes and is advanced over it.
* Returns the offset.
*/
static int
bytes2offset(char_u **pp)
{
char_u *p = *pp;
int nr;
int c;
c = *p++;
if ((c & 0x80) == 0x00) // 1 byte
{
nr = c - 1;
}
else if ((c & 0xc0) == 0x80) // 2 bytes
{
nr = (c & 0x3f) - 1;
nr = nr * 255 + (*p++ - 1);
}
else if ((c & 0xe0) == 0xc0) // 3 bytes
{
nr = (c & 0x1f) - 1;
nr = nr * 255 + (*p++ - 1);
nr = nr * 255 + (*p++ - 1);
}
else // 4 bytes
{
nr = (c & 0x0f) - 1;
nr = nr * 255 + (*p++ - 1);
nr = nr * 255 + (*p++ - 1);
nr = nr * 255 + (*p++ - 1);
}
*pp = p;
return nr;
}
// values for sps_flags
#define SPS_BEST 1
#define SPS_FAST 2
#define SPS_DOUBLE 4
static int sps_flags = SPS_BEST; // flags from 'spellsuggest'
static int sps_limit = 9999; // max nr of suggestions given
/*
* Check the 'spellsuggest' option. Return FAIL if it's wrong.
* Sets "sps_flags" and "sps_limit".
*/
int
spell_check_sps(void)
{
char_u *p;
char_u *s;
char_u buf[MAXPATHL];
int f;
sps_flags = 0;
sps_limit = 9999;
for (p = p_sps; *p != NUL; )
{
copy_option_part(&p, buf, MAXPATHL, ",");
f = 0;
if (VIM_ISDIGIT(*buf))
{
s = buf;
sps_limit = getdigits(&s);
if (*s != NUL && !VIM_ISDIGIT(*s))
f = -1;
}
else if (STRCMP(buf, "best") == 0)
f = SPS_BEST;
else if (STRCMP(buf, "fast") == 0)
f = SPS_FAST;
else if (STRCMP(buf, "double") == 0)
f = SPS_DOUBLE;
else if (STRNCMP(buf, "expr:", 5) != 0
&& STRNCMP(buf, "file:", 5) != 0)
f = -1;
if (f == -1 || (sps_flags != 0 && f != 0))
{
sps_flags = SPS_BEST;
sps_limit = 9999;
return FAIL;
}
if (f != 0)
sps_flags = f;
}
if (sps_flags == 0)
sps_flags = SPS_BEST;
return OK;
}
/*
* "z=": Find badly spelled word under or after the cursor.
* Give suggestions for the properly spelled word.
* In Visual mode use the highlighted word as the bad word.
* When "count" is non-zero use that suggestion.
*/
void
spell_suggest(int count)
{
char_u *line;
pos_T prev_cursor = curwin->w_cursor;
char_u wcopy[MAXWLEN + 2];
char_u *p;
int i;
int c;
suginfo_T sug;
suggest_T *stp;
int mouse_used;
int need_cap;
int limit;
int selected = count;
int badlen = 0;
int msg_scroll_save = msg_scroll;
int wo_spell_save = curwin->w_p_spell;
if (!curwin->w_p_spell)
{
did_set_spelllang(curwin);
curwin->w_p_spell = TRUE;
}
if (*curwin->w_s->b_p_spl == NUL)
{
emsg(_(e_spell_checking_is_not_possible));
return;
}
if (VIsual_active)
{
// Use the Visually selected text as the bad word. But reject
// a multi-line selection.
if (curwin->w_cursor.lnum != VIsual.lnum)
{
vim_beep(BO_SPELL);
return;
}
badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
if (badlen < 0)
badlen = -badlen;
else
curwin->w_cursor.col = VIsual.col;
++badlen;
end_visual_mode();
}
// Find the start of the badly spelled word.
else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
|| curwin->w_cursor.col > prev_cursor.col)
{
// No bad word or it starts after the cursor: use the word under the
// cursor.
curwin->w_cursor = prev_cursor;
line = ml_get_curline();
p = line + curwin->w_cursor.col;
// Backup to before start of word.
while (p > line && spell_iswordp_nmw(p, curwin))
MB_PTR_BACK(line, p);
// Forward to start of word.
while (*p != NUL && !spell_iswordp_nmw(p, curwin))
MB_PTR_ADV(p);
if (!spell_iswordp_nmw(p, curwin)) // No word found.
{
beep_flush();
return;
}
curwin->w_cursor.col = (colnr_T)(p - line);
}
// Get the word and its length.
// Figure out if the word should be capitalised.
need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
// Make a copy of current line since autocommands may free the line.
line = vim_strsave(ml_get_curline());
if (line == NULL)
goto skip;
// Get the list of suggestions. Limit to 'lines' - 2 or the number in
// 'spellsuggest', whatever is smaller.
if (sps_limit > (int)Rows - 2)
limit = (int)Rows - 2;
else
limit = sps_limit;
spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
TRUE, need_cap, TRUE);
if (sug.su_ga.ga_len == 0)
msg(_("Sorry, no suggestions"));
else if (count > 0)
{
if (count > sug.su_ga.ga_len)
smsg(_("Sorry, only %ld suggestions"), (long)sug.su_ga.ga_len);
}
else
{
#ifdef FEAT_RIGHTLEFT
// When 'rightleft' is set the list is drawn right-left.
cmdmsg_rl = curwin->w_p_rl;
if (cmdmsg_rl)
msg_col = Columns - 1;
#endif
// List the suggestions.
msg_start();
msg_row = Rows - 1; // for when 'cmdheight' > 1
lines_left = Rows; // avoid more prompt
vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
sug.su_badlen, sug.su_badptr);
#ifdef FEAT_RIGHTLEFT
if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
{
// And now the rabbit from the high hat: Avoid showing the
// untranslated message rightleft.
vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
sug.su_badlen, sug.su_badptr);
}
#endif
msg_puts((char *)IObuff);
msg_clr_eos();
msg_putchar('\n');
msg_scroll = TRUE;
for (i = 0; i < sug.su_ga.ga_len; ++i)
{
stp = &SUG(sug.su_ga, i);
// The suggested word may replace only part of the bad word, add
// the not replaced part.
vim_strncpy(wcopy, stp->st_word, MAXWLEN);
if (sug.su_badlen > stp->st_orglen)
vim_strncpy(wcopy + stp->st_wordlen,
sug.su_badptr + stp->st_orglen,
sug.su_badlen - stp->st_orglen);
vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
#ifdef FEAT_RIGHTLEFT
if (cmdmsg_rl)
rl_mirror(IObuff);
#endif
msg_puts((char *)IObuff);
vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
msg_puts((char *)IObuff);
// The word may replace more than "su_badlen".
if (sug.su_badlen < stp->st_orglen)
{
vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
stp->st_orglen, sug.su_badptr);
msg_puts((char *)IObuff);
}
if (p_verbose > 0)
{
// Add the score.
if (sps_flags & (SPS_DOUBLE | SPS_BEST))
vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
stp->st_salscore ? "s " : "",
stp->st_score, stp->st_altscore);
else
vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
stp->st_score);
#ifdef FEAT_RIGHTLEFT
if (cmdmsg_rl)
// Mirror the numbers, but keep the leading space.
rl_mirror(IObuff + 1);
#endif
msg_advance(30);
msg_puts((char *)IObuff);
}
msg_putchar('\n');
}
#ifdef FEAT_RIGHTLEFT
cmdmsg_rl = FALSE;
msg_col = 0;
#endif
// Ask for choice.
selected = prompt_for_number(&mouse_used);
if (mouse_used)
selected -= lines_left;
lines_left = Rows; // avoid more prompt
// don't delay for 'smd' in normal_cmd()
msg_scroll = msg_scroll_save;
}
if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
{
// Save the from and to text for :spellrepall.
VIM_CLEAR(repl_from);
VIM_CLEAR(repl_to);
stp = &SUG(sug.su_ga, selected - 1);
if (sug.su_badlen > stp->st_orglen)
{
// Replacing less than "su_badlen", append the remainder to
// repl_to.
repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
sug.su_badlen - stp->st_orglen,
sug.su_badptr + stp->st_orglen);
repl_to = vim_strsave(IObuff);
}
else
{
// Replacing su_badlen or more, use the whole word.
repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
repl_to = vim_strsave(stp->st_word);
}
// Replace the word.
p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
if (p != NULL)
{
c = (int)(sug.su_badptr - line);
mch_memmove(p, line, c);
STRCPY(p + c, stp->st_word);
STRCAT(p, sug.su_badptr + stp->st_orglen);
// For redo we use a change-word command.
ResetRedobuff();
AppendToRedobuff((char_u *)"ciw");
AppendToRedobuffLit(p + c,
stp->st_wordlen + sug.su_badlen - stp->st_orglen);
AppendCharToRedobuff(ESC);
// "p" may be freed here
ml_replace(curwin->w_cursor.lnum, p, FALSE);
curwin->w_cursor.col = c;
changed_bytes(curwin->w_cursor.lnum, c);
}
}
else
curwin->w_cursor = prev_cursor;
spell_find_cleanup(&sug);
skip:
vim_free(line);
curwin->w_p_spell = wo_spell_save;
}
/*
* Find spell suggestions for "word". Return them in the growarray "*gap" as
* a list of allocated strings.
*/
void
spell_suggest_list(
garray_T *gap,
char_u *word,
int maxcount, // maximum nr of suggestions
int need_cap, // 'spellcapcheck' matched
int interactive)
{
suginfo_T sug;
int i;
suggest_T *stp;
char_u *wcopy;
spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
// Make room in "gap".
ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
if (ga_grow(gap, sug.su_ga.ga_len) == OK)
{
for (i = 0; i < sug.su_ga.ga_len; ++i)
{
stp = &SUG(sug.su_ga, i);
// The suggested word may replace only part of "word", add the not
// replaced part.
wcopy = alloc(stp->st_wordlen
+ (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
if (wcopy == NULL)
break;
STRCPY(wcopy, stp->st_word);
STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
}
}
spell_find_cleanup(&sug);
}
/*
* Find spell suggestions for the word at the start of "badptr".
* Return the suggestions in "su->su_ga".
* The maximum number of suggestions is "maxcount".
* Note: does use info for the current window.
* This is based on the mechanisms of Aspell, but completely reimplemented.
*/
static void
spell_find_suggest(
char_u *badptr,
int badlen, // length of bad word or 0 if unknown
suginfo_T *su,
int maxcount,
int banbadword, // don't include badword in suggestions
int need_cap, // word should start with capital
int interactive)
{
hlf_T attr = HLF_COUNT;
char_u buf[MAXPATHL];
char_u *p;
int do_combine = FALSE;
char_u *sps_copy;
#ifdef FEAT_EVAL
static int expr_busy = FALSE;
#endif
int c;
int i;
langp_T *lp;
int did_intern = FALSE;
// Set the info in "*su".
CLEAR_POINTER(su);
ga_init2(&su->su_ga, sizeof(suggest_T), 10);
ga_init2(&su->su_sga, sizeof(suggest_T), 10);
if (*badptr == NUL)
return;
hash_init(&su->su_banned);
su->su_badptr = badptr;
if (badlen != 0)
su->su_badlen = badlen;
else
su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
su->su_maxcount = maxcount;
su->su_maxscore = SCORE_MAXINIT;
if (su->su_badlen >= MAXWLEN)
su->su_badlen = MAXWLEN - 1; // just in case
vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
(void)spell_casefold(curwin, su->su_badptr, su->su_badlen,
su->su_fbadword, MAXWLEN);
// TODO: make this work if the case-folded text is longer than the original
// text. Currently an illegal byte causes wrong pointer computations.
su->su_fbadword[su->su_badlen] = NUL;
// get caps flags for bad word
su->su_badflags = badword_captype(su->su_badptr,
su->su_badptr + su->su_badlen);
if (need_cap)
su->su_badflags |= WF_ONECAP;
// Find the default language for sound folding. We simply use the first
// one in 'spelllang' that supports sound folding. That's good for when
// using multiple files for one language, it's not that bad when mixing
// languages (e.g., "pl,en").
for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
{
lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
if (lp->lp_sallang != NULL)
{
su->su_sallang = lp->lp_sallang;
break;
}
}
// Soundfold the bad word with the default sound folding, so that we don't
// have to do this many times.
if (su->su_sallang != NULL)
spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
su->su_sal_badword);
// If the word is not capitalised and spell_check() doesn't consider the
// word to be bad then it might need to be capitalised. Add a suggestion
// for that.
c = PTR2CHAR(su->su_badptr);
if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
{
make_case_word(su->su_badword, buf, WF_ONECAP);
add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
0, TRUE, su->su_sallang, FALSE);
}
// Ban the bad word itself. It may appear in another region.
if (banbadword)
add_banned(su, su->su_badword);
// Make a copy of 'spellsuggest', because the expression may change it.
sps_copy = vim_strsave(p_sps);
if (sps_copy == NULL)
return;
// Loop over the items in 'spellsuggest'.
for (p = sps_copy; *p != NUL; )
{
copy_option_part(&p, buf, MAXPATHL, ",");
if (STRNCMP(buf, "expr:", 5) == 0)
{
#ifdef FEAT_EVAL
// Evaluate an expression. Skip this when called recursively,
// when using spellsuggest() in the expression.
if (!expr_busy)
{
expr_busy = TRUE;
spell_suggest_expr(su, buf + 5);
expr_busy = FALSE;
}
#endif
}
else if (STRNCMP(buf, "file:", 5) == 0)
// Use list of suggestions in a file.
spell_suggest_file(su, buf + 5);
else if (!did_intern)
{
// Use internal method once.
spell_suggest_intern(su, interactive);
if (sps_flags & SPS_DOUBLE)
do_combine = TRUE;
did_intern = TRUE;
}
}
vim_free(sps_copy);
if (do_combine)
// Combine the two list of suggestions. This must be done last,
// because sorting changes the order again.
score_combine(su);
}
#ifdef FEAT_EVAL
/*
* Find suggestions by evaluating expression "expr".
*/
static void
spell_suggest_expr(suginfo_T *su, char_u *expr)
{
list_T *list;
listitem_T *li;
int score;
char_u *p;
// The work is split up in a few parts to avoid having to export
// suginfo_T.
// First evaluate the expression and get the resulting list.
list = eval_spell_expr(su->su_badword, expr);
if (list != NULL)
{
// Loop over the items in the list.
FOR_ALL_LIST_ITEMS(list, li)
if (li->li_tv.v_type == VAR_LIST)
{
// Get the word and the score from the items.
score = get_spellword(li->li_tv.vval.v_list, &p);
if (score >= 0 && score <= su->su_maxscore)
add_suggestion(su, &su->su_ga, p, su->su_badlen,
score, 0, TRUE, su->su_sallang, FALSE);
}
list_unref(list);
}
// Remove bogus suggestions, sort and truncate at "maxcount".
check_suggestions(su, &su->su_ga);
(void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
}
#endif
/*
* Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
*/
static void
spell_suggest_file(suginfo_T *su, char_u *fname)
{
FILE *fd;
char_u line[MAXWLEN * 2];
char_u *p;
int len;
char_u cword[MAXWLEN];
// Open the file.
fd = mch_fopen((char *)fname, "r");
if (fd == NULL)
{
semsg(_(e_cant_open_file_str), fname);
return;
}
// Read it line by line.
while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
{
line_breakcheck();
p = vim_strchr(line, '/');
if (p == NULL)
continue; // No Tab found, just skip the line.
*p++ = NUL;
if (STRICMP(su->su_badword, line) == 0)
{
// Match! Isolate the good word, until CR or NL.
for (len = 0; p[len] >= ' '; ++len)
;
p[len] = NUL;
// If the suggestion doesn't have specific case duplicate the case
// of the bad word.
if (captype(p, NULL) == 0)
{
make_case_word(p, cword, su->su_badflags);
p = cword;
}
add_suggestion(su, &su->su_ga, p, su->su_badlen,
SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
}
}
fclose(fd);
// Remove bogus suggestions, sort and truncate at "maxcount".
check_suggestions(su, &su->su_ga);
(void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
}
/*
* Find suggestions for the internal method indicated by "sps_flags".
*/
static void
spell_suggest_intern(suginfo_T *su, int interactive)
{
// Load the .sug file(s) that are available and not done yet.
suggest_load_files();
// 1. Try special cases, such as repeating a word: "the the" -> "the".
//
// Set a maximum score to limit the combination of operations that is
// tried.
suggest_try_special(su);
// 2. Try inserting/deleting/swapping/changing a letter, use REP entries
// from the .aff file and inserting a space (split the word).
suggest_try_change(su);
// For the resulting top-scorers compute the sound-a-like score.
if (sps_flags & SPS_DOUBLE)
score_comp_sal(su);