1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * spell.c: code for spell checking
13 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
34 * There are two word trees: one with case-folded words and one with words in
35 * original case. The second one is only used for keep-case words and is
38 * There is one additional tree for when not all prefixes are applied when
39 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
44 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
52 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
56 /* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
57 * Only use it for small word lists! */
59 # define SPELL_PRINTTREE
62 /* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
65 # define DEBUG_TRIEWALK
69 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
72 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
74 * Used when 'spellsuggest' is set to "best".
76 #define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
79 * Do the opposite: based on a maximum end score and a known sound score,
80 * compute the maximum word score that can be used.
82 #define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
85 * Vim spell file format: <HEADER>
91 * <HEADER>: <fileID> <versionnr>
93 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
106 * <SECTIONS>: <section> ... <sectionend>
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
115 * <sectionlen> 4 bytes length of section contents, MSB first
117 * <sectionend> 1 byte SN_END
120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
126 * First <regionname> is region 1.
128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
139 * in the middle of a word.
141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
143 * <prefcond> : <condlen> <condstr>
144 * <condlen> 1 byte Length of <condstr>.
145 * <condstr> N bytes Condition for the prefix.
147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
163 * <salcount> 2 bytes number of <sal> items following
164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
179 * sectionID == SN_NOSPLITSUGS: nothing
181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
186 * separated by slashes.
188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
207 * <LWORDTREE>: <wordtree>
209 * <KWORDTREE>: <wordtree>
211 * <PREFIXTREE>: <wordtree>
214 * <wordtree>: <nodecount> <nodedata> ...
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
218 * <nodedata>: <siblingcount> <sibling> ...
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
223 * <sibling>: <byte> [ <nodeidx> <xbyte>
224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
230 * For PREFIXTREE <affixID> and
231 * <prefcondnr> follow.
232 * BY_FLAGS: End of word, <flags> follow.
233 * For PREFIXTREE <pflags>, <affixID>
234 * and <prefcondnr> follow.
235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
237 * BY_INDEX: Child of sibling is shared, <nodeidx>
238 * and <xbyte> follow.
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
242 * <xbyte> 1 byte byte value of the sibling.
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
251 * WF_REGION <region> follows
252 * WF_AFX <affixID> follows
254 * <flags2> 1 byte Bitmask of:
255 * WF_HAS_AFF >> 8 word includes affix
256 * WF_NEEDCOMP >> 8 word only valid in compound
257 * WF_NOSUGGEST >> 8 word not used for suggestions
258 * WF_COMPROOT >> 8 word already a compound
259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
271 * <affixID> 1 byte ID of affix that can be used with this word. In
272 * PREFIXTREE used for the required prefix ID.
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
277 * All text characters are in 'encoding', but stored as single bytes.
281 * Vim .sug file format: <SUGHEADER>
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
295 * <SUGTABLE>: <sugwcount> <sugline> ...
297 * <sugwcount> 4 bytes number of <sugline> following
299 * <sugline>: <sugnr> ... NUL
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
308 #if defined(FEAT_SPELL) || defined(PROTO)
310 #ifndef UNIX /* it's in os_unix.h for Unix */
311 # include <time.h> /* for time_t */
314 #define MAXWLEN 250 /* Assume max. word len is this many bytes.
315 Some places assume a word length fits in a
316 byte, thus it can't be above 255. */
318 /* Type used for indexes in the word tree need to be at least 4 bytes. If int
319 * is 8 bytes we could use something smaller, but what? */
327 # define SPL_FNAME_TMPL "%s_%s.spl"
328 # define SPL_FNAME_ADD "_add."
329 # define SPL_FNAME_ASCII "_ascii."
331 # define SPL_FNAME_TMPL "%s.%s.spl"
332 # define SPL_FNAME_ADD ".add."
333 # define SPL_FNAME_ASCII ".ascii."
336 /* Flags used for a word. Only the lowest byte can be used, the region byte
338 #define WF_REGION 0x01 /* region byte follows */
339 #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
340 #define WF_ALLCAP 0x04 /* word must be all capitals */
341 #define WF_RARE 0x08 /* rare word */
342 #define WF_BANNED 0x10 /* bad word */
343 #define WF_AFX 0x20 /* affix ID follows */
344 #define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
345 #define WF_KEEPCAP 0x80 /* keep-case word */
347 /* for <flags2>, shifted up one byte to be used in wn_flags */
348 #define WF_HAS_AFF 0x0100 /* word includes affix */
349 #define WF_NEEDCOMP 0x0200 /* word only valid in compound */
350 #define WF_NOSUGGEST 0x0400 /* word not to be suggested */
351 #define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
352 #define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
353 #define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
355 /* only used for su_badflags */
356 #define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
358 #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
360 /* flags for <pflags> */
361 #define WFP_RARE 0x01 /* rare prefix */
362 #define WFP_NC 0x02 /* prefix is not combining */
363 #define WFP_UP 0x04 /* to-upper prefix */
364 #define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
365 #define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
367 /* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
368 * byte) and prefcondnr (two bytes). */
369 #define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
370 #define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
371 #define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
372 #define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
373 * COMPOUNDPERMITFLAG */
374 #define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
375 * COMPOUNDFORBIDFLAG */
378 /* flags for <compoptions> */
379 #define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
380 #define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
381 #define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
382 #define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
384 /* Special byte values for <byte>. Some are only used in the tree for
385 * postponed prefixes, some only in the other trees. This is a bit messy... */
386 #define BY_NOFLAGS 0 /* end of word without flags or region; for
387 * postponed prefix: no <pflags> */
388 #define BY_INDEX 1 /* child is shared, index follows */
389 #define BY_FLAGS 2 /* end of word, <flags> byte follows; for
390 * postponed prefix: <pflags> follows */
391 #define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
392 * follow; never used in prefix tree */
393 #define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
395 /* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
396 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
397 * One replacement: from "ft_from" to "ft_to". */
398 typedef struct fromto_S
404 /* Info from "SAL" entries in ".aff" file used in sl_sal.
405 * The info is split for quick processing by spell_soundfold().
406 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
407 typedef struct salitem_S
409 char_u *sm_lead; /* leading letters */
410 int sm_leadlen; /* length of "sm_lead" */
411 char_u *sm_oneof; /* letters from () or NULL */
412 char_u *sm_rules; /* rules like ^, $, priority */
413 char_u *sm_to; /* replacement. */
415 int *sm_lead_w; /* wide character copy of "sm_lead" */
416 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
417 int *sm_to_w; /* wide character copy of "sm_to" */
422 typedef int salfirst_T;
424 typedef short salfirst_T;
427 /* Values for SP_*ERROR are negative, positive values are used by
428 * read_cnt_string(). */
429 #define SP_TRUNCERROR -1 /* spell file truncated error */
430 #define SP_FORMERROR -2 /* format error in spell file */
431 #define SP_OTHERERROR -3 /* other error while reading spell file */
434 * Structure used to store words and other info for one language, loaded from
436 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
437 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
439 * The "byts" array stores the possible bytes in each tree node, preceded by
440 * the number of possible bytes, sorted on byte value:
441 * <len> <byte1> <byte2> ...
442 * The "idxs" array stores the index of the child node corresponding to the
444 * Exception: when the byte is zero, the word may end here and "idxs" holds
445 * the flags, region mask and affixID for the word. There may be several
446 * zeros in sequence for alternative flag/region/affixID combinations.
448 typedef struct slang_S slang_T;
451 slang_T *sl_next; /* next language */
452 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
453 char_u *sl_fname; /* name of .spl file */
454 int sl_add; /* TRUE if it's a .add file. */
456 char_u *sl_fbyts; /* case-folded word bytes */
457 idx_T *sl_fidxs; /* case-folded word indexes */
458 char_u *sl_kbyts; /* keep-case word bytes */
459 idx_T *sl_kidxs; /* keep-case word indexes */
460 char_u *sl_pbyts; /* prefix tree word bytes */
461 idx_T *sl_pidxs; /* prefix tree word indexes */
463 char_u *sl_info; /* infotext string or NULL */
465 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
467 char_u *sl_midword; /* MIDWORD string or NULL */
469 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
471 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
472 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
473 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
474 int sl_compoptions; /* COMP_* flags */
475 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
476 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
477 * (NULL when no compounding) */
478 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
479 char_u *sl_compstartflags; /* flags for first compound word */
480 char_u *sl_compallflags; /* all flags for compound words */
481 char_u sl_nobreak; /* When TRUE: no spaces between words */
482 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
483 garray_T sl_syl_items; /* syllable items */
485 int sl_prefixcnt; /* number of items in "sl_prefprog" */
486 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
488 garray_T sl_rep; /* list of fromto_T entries from REP lines */
489 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
491 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
492 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
494 int sl_followup; /* SAL followup */
495 int sl_collapse; /* SAL collapse_result */
496 int sl_rem_accents; /* SAL remove_accents */
497 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
498 * "sl_sal_first" maps chars, when has_mbyte
499 * "sl_sal" is a list of wide char lists. */
500 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
501 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
502 int sl_nosplitsugs; /* don't suggest splitting a word */
504 /* Info from the .sug file. Loaded on demand. */
505 time_t sl_sugtime; /* timestamp for .sug file */
506 char_u *sl_sbyts; /* soundfolded word bytes */
507 idx_T *sl_sidxs; /* soundfolded word indexes */
508 buf_T *sl_sugbuf; /* buffer with word number table */
509 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
512 int sl_has_map; /* TRUE if there is a MAP line */
514 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
515 int sl_map_array[256]; /* MAP for first 256 chars */
517 char_u sl_map_array[256]; /* MAP for first 256 chars */
519 hashtab_T sl_sounddone; /* table with soundfolded words that have
520 handled, see add_sound_suggest() */
523 /* First language that is loaded, start of the linked list of loaded
525 static slang_T *first_lang = NULL;
527 /* Flags used in .spl file for soundsalike flags. */
528 #define SAL_F0LLOWUP 1
529 #define SAL_COLLAPSE 2
530 #define SAL_REM_ACCENTS 4
533 * Structure used in "b_langp", filled from 'spelllang'.
535 typedef struct langp_S
537 slang_T *lp_slang; /* info for this language */
538 slang_T *lp_sallang; /* language used for sound folding or NULL */
539 slang_T *lp_replang; /* language used for REP items or NULL */
540 int lp_region; /* bitmask for region or REGION_ALL */
543 #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
545 #define REGION_ALL 0xff /* word valid in all regions */
547 #define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
548 #define VIMSPELLMAGICL 8
549 #define VIMSPELLVERSION 50
551 #define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
552 #define VIMSUGMAGICL 6
553 #define VIMSUGVERSION 1
555 /* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
556 #define SN_REGION 0 /* <regionname> section */
557 #define SN_CHARFLAGS 1 /* charflags section */
558 #define SN_MIDWORD 2 /* <midword> section */
559 #define SN_PREFCOND 3 /* <prefcond> section */
560 #define SN_REP 4 /* REP items section */
561 #define SN_SAL 5 /* SAL items section */
562 #define SN_SOFO 6 /* soundfolding section */
563 #define SN_MAP 7 /* MAP items section */
564 #define SN_COMPOUND 8 /* compound words section */
565 #define SN_SYLLABLE 9 /* syllable section */
566 #define SN_NOBREAK 10 /* NOBREAK section */
567 #define SN_SUGFILE 11 /* timestamp for .sug file */
568 #define SN_REPSAL 12 /* REPSAL items section */
569 #define SN_WORDS 13 /* common words */
570 #define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
571 #define SN_INFO 15 /* info section */
572 #define SN_END 255 /* end of sections */
574 #define SNF_REQUIRED 1 /* <sectionflags>: required section */
576 /* Result values. Lower number is accepted over higher one. */
583 /* file used for "zG" and "zW" */
584 static char_u *int_wordlist = NULL;
586 typedef struct wordcount_S
588 short_u wc_count; /* nr of times word was seen */
589 char_u wc_word[1]; /* word, actually longer */
592 static wordcount_T dumwc;
593 #define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
594 #define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
595 #define MAXWORDCOUNT 0xffff
598 * Information used when looking for suggestions.
600 typedef struct suginfo_S
602 garray_T su_ga; /* suggestions, contains "suggest_T" */
603 int su_maxcount; /* max. number of suggestions displayed */
604 int su_maxscore; /* maximum score for adding to su_ga */
605 int su_sfmaxscore; /* idem, for when doing soundfold words */
606 garray_T su_sga; /* like su_ga, sound-folded scoring */
607 char_u *su_badptr; /* start of bad word in line */
608 int su_badlen; /* length of detected bad word in line */
609 int su_badflags; /* caps flags for bad word */
610 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
611 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
612 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
613 hashtab_T su_banned; /* table with banned words */
614 slang_T *su_sallang; /* default language for sound folding */
617 /* One word suggestion. Used in "si_ga". */
618 typedef struct suggest_S
620 char_u *st_word; /* suggested word, allocated string */
621 int st_wordlen; /* STRLEN(st_word) */
622 int st_orglen; /* length of replaced text */
623 int st_score; /* lower is better */
624 int st_altscore; /* used when st_score compares equal */
625 int st_salscore; /* st_score is for soundalike */
626 int st_had_bonus; /* bonus already included in score */
627 slang_T *st_slang; /* language used for sound folding */
630 #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
632 /* TRUE if a word appears in the list of banned words. */
633 #define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
635 /* Number of suggestions kept when cleaning up. We need to keep more than
636 * what is displayed, because when rescore_suggestions() is called the score
637 * may change and wrong suggestions may be removed later. */
638 #define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
640 /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
641 * of suggestions that are not going to be displayed. */
642 #define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
644 /* score for various changes */
645 #define SCORE_SPLIT 149 /* split bad word */
646 #define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
647 #define SCORE_ICASE 52 /* slightly different case */
648 #define SCORE_REGION 200 /* word is for different region */
649 #define SCORE_RARE 180 /* rare word */
650 #define SCORE_SWAP 75 /* swap two characters */
651 #define SCORE_SWAP3 110 /* swap two characters in three */
652 #define SCORE_REP 65 /* REP replacement */
653 #define SCORE_SUBST 93 /* substitute a character */
654 #define SCORE_SIMILAR 33 /* substitute a similar character */
655 #define SCORE_SUBCOMP 33 /* substitute a composing character */
656 #define SCORE_DEL 94 /* delete a character */
657 #define SCORE_DELDUP 66 /* delete a duplicated character */
658 #define SCORE_DELCOMP 28 /* delete a composing character */
659 #define SCORE_INS 96 /* insert a character */
660 #define SCORE_INSDUP 67 /* insert a duplicate character */
661 #define SCORE_INSCOMP 30 /* insert a composing character */
662 #define SCORE_NONWORD 103 /* change non-word to word char */
664 #define SCORE_FILE 30 /* suggestion from a file */
665 #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
666 * 350 allows for about three changes. */
668 #define SCORE_COMMON1 30 /* subtracted for words seen before */
669 #define SCORE_COMMON2 40 /* subtracted for words often seen */
670 #define SCORE_COMMON3 50 /* subtracted for words very often seen */
671 #define SCORE_THRES2 10 /* word count threshold for COMMON2 */
672 #define SCORE_THRES3 100 /* word count threshold for COMMON3 */
674 /* When trying changed soundfold words it becomes slow when trying more than
675 * two changes. With less then two changes it's slightly faster but we miss a
676 * few good suggestions. In rare cases we need to try three of four changes.
678 #define SCORE_SFMAX1 200 /* maximum score for first try */
679 #define SCORE_SFMAX2 300 /* maximum score for second try */
680 #define SCORE_SFMAX3 400 /* maximum score for third try */
682 #define SCORE_BIG SCORE_INS * 3 /* big difference */
683 #define SCORE_MAXMAX 999999 /* accept any score */
684 #define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
686 /* for spell_edit_score_limit() we need to know the minimum value of
687 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
688 #define SCORE_EDIT_MIN SCORE_SIMILAR
691 * Structure to store info for word matching.
693 typedef struct matchinf_S
695 langp_T *mi_lp; /* info for language and region */
697 /* pointers to original text to be checked */
698 char_u *mi_word; /* start of word being checked */
699 char_u *mi_end; /* end of matching word so far */
700 char_u *mi_fend; /* next char to be added to mi_fword */
701 char_u *mi_cend; /* char after what was used for
704 /* case-folded text */
705 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
706 int mi_fwordlen; /* nr of valid bytes in mi_fword */
708 /* for when checking word after a prefix */
709 int mi_prefarridx; /* index in sl_pidxs with list of
711 int mi_prefcnt; /* number of entries at mi_prefarridx */
712 int mi_prefixlen; /* byte length of prefix */
714 int mi_cprefixlen; /* byte length of prefix in original
717 # define mi_cprefixlen mi_prefixlen /* it's the same value */
720 /* for when checking a compound word */
721 int mi_compoff; /* start of following word offset */
722 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
723 int mi_complen; /* nr of compound words used */
724 int mi_compextra; /* nr of COMPOUNDROOT words */
727 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
728 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
729 win_T *mi_win; /* buffer being checked */
732 int mi_result2; /* "mi_resul" without following word */
733 char_u *mi_end2; /* "mi_end" without following word */
737 * The tables used for recognizing word characters according to spelling.
738 * These are only used for the first 256 characters of 'encoding'.
740 typedef struct spelltab_S
742 char_u st_isw[256]; /* flags: is word char */
743 char_u st_isu[256]; /* flags: is uppercase char */
744 char_u st_fold[256]; /* chars: folded case */
745 char_u st_upper[256]; /* chars: upper case */
748 static spelltab_T spelltab;
749 static int did_set_spelltab;
752 #define CF_UPPER 0x02
754 static void clear_spell_chartab __ARGS((spelltab_T *sp));
755 static int set_spell_finish __ARGS((spelltab_T *new_st));
756 static int spell_iswordp __ARGS((char_u *p, win_T *wp));
757 static int spell_iswordp_nmw __ARGS((char_u *p));
759 static int spell_mb_isword_class __ARGS((int cl));
760 static int spell_iswordp_w __ARGS((int *p, win_T *wp));
762 static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
765 * For finding suggestions: At each node in the tree these states are tried:
769 STATE_START = 0, /* At start of node check for NUL bytes (goodword
770 * ends); if badword ends there is a match, otherwise
771 * try splitting word. */
772 STATE_NOPREFIX, /* try without prefix */
773 STATE_SPLITUNDO, /* Undo splitting. */
774 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
775 STATE_PLAIN, /* Use each byte of the node. */
776 STATE_DEL, /* Delete a byte from the bad word. */
777 STATE_INS_PREP, /* Prepare for inserting bytes. */
778 STATE_INS, /* Insert a byte in the bad word. */
779 STATE_SWAP, /* Swap two bytes. */
780 STATE_UNSWAP, /* Undo swap two characters. */
781 STATE_SWAP3, /* Swap two characters over three. */
782 STATE_UNSWAP3, /* Undo Swap two characters over three. */
783 STATE_UNROT3L, /* Undo rotate three characters left */
784 STATE_UNROT3R, /* Undo rotate three characters right */
785 STATE_REP_INI, /* Prepare for using REP items. */
786 STATE_REP, /* Use matching REP items from the .aff file. */
787 STATE_REP_UNDO, /* Undo a REP item replacement. */
788 STATE_FINAL /* End of this node. */
792 * Struct to keep the state at each level in suggest_try_change().
794 typedef struct trystate_S
796 state_T ts_state; /* state at this level, STATE_ */
797 int ts_score; /* score */
798 idx_T ts_arridx; /* index in tree array, start of node */
799 short ts_curi; /* index in list of child nodes */
800 char_u ts_fidx; /* index in fword[], case-folded bad word */
801 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
802 char_u ts_twordlen; /* valid length of tword[] */
803 char_u ts_prefixdepth; /* stack depth for end of prefix or
804 * PFD_PREFIXTREE or PFD_NOPREFIX */
805 char_u ts_flags; /* TSF_ flags */
807 char_u ts_tcharlen; /* number of bytes in tword character */
808 char_u ts_tcharidx; /* current byte index in tword character */
809 char_u ts_isdiff; /* DIFF_ values */
810 char_u ts_fcharstart; /* index in fword where badword char started */
812 char_u ts_prewordlen; /* length of word in "preword[]" */
813 char_u ts_splitoff; /* index in "tword" after last split */
814 char_u ts_splitfidx; /* "ts_fidx" at word split */
815 char_u ts_complen; /* nr of compound words used */
816 char_u ts_compsplit; /* index for "compflags" where word was spit */
817 char_u ts_save_badflags; /* su_badflags saved here */
818 char_u ts_delidx; /* index in fword for char that was deleted,
819 valid when "ts_flags" has TSF_DIDDEL */
822 /* values for ts_isdiff */
823 #define DIFF_NONE 0 /* no different byte (yet) */
824 #define DIFF_YES 1 /* different byte found */
825 #define DIFF_INSERT 2 /* inserting character */
827 /* values for ts_flags */
828 #define TSF_PREFIXOK 1 /* already checked that prefix is OK */
829 #define TSF_DIDSPLIT 2 /* tried split at this point */
830 #define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
832 /* special values ts_prefixdepth */
833 #define PFD_NOPREFIX 0xff /* not using prefixes */
834 #define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
835 #define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
837 /* mode values for find_word */
838 #define FIND_FOLDWORD 0 /* find word case-folded */
839 #define FIND_KEEPWORD 1 /* find keep-case word */
840 #define FIND_PREFIX 2 /* find word after prefix */
841 #define FIND_COMPOUND 3 /* find case-folded compound word */
842 #define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
844 static slang_T *slang_alloc __ARGS((char_u *lang));
845 static void slang_free __ARGS((slang_T *lp));
846 static void slang_clear __ARGS((slang_T *lp));
847 static void slang_clear_sug __ARGS((slang_T *lp));
848 static void find_word __ARGS((matchinf_T *mip, int mode));
849 static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
850 static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
851 static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
852 static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
853 static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
854 static void find_prefix __ARGS((matchinf_T *mip, int mode));
855 static int fold_more __ARGS((matchinf_T *mip));
856 static int spell_valid_case __ARGS((int wordflags, int treeflags));
857 static int no_spell_checking __ARGS((win_T *wp));
858 static void spell_load_lang __ARGS((char_u *lang));
859 static char_u *spell_enc __ARGS((void));
860 static void int_wordlist_spl __ARGS((char_u *fname));
861 static void spell_load_cb __ARGS((char_u *fname, void *cookie));
862 static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
863 static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
864 static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
865 static int read_charflags_section __ARGS((FILE *fd));
866 static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
867 static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
868 static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
869 static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
870 static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
871 static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
872 static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
873 static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
874 static int byte_in_str __ARGS((char_u *str, int byte));
875 static int init_syl_tab __ARGS((slang_T *slang));
876 static int count_syllables __ARGS((slang_T *slang, char_u *word));
877 static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
878 static void set_sal_first __ARGS((slang_T *lp));
880 static int *mb_str2wide __ARGS((char_u *s));
882 static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
883 static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
884 static void clear_midword __ARGS((win_T *buf));
885 static void use_midword __ARGS((slang_T *lp, win_T *buf));
886 static int find_region __ARGS((char_u *rp, char_u *region));
887 static int captype __ARGS((char_u *word, char_u *end));
888 static int badword_captype __ARGS((char_u *word, char_u *end));
889 static void spell_reload_one __ARGS((char_u *fname, int added_word));
890 static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
891 static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
892 static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
893 static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
894 static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
896 static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
898 static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
899 static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
900 static void suggest_load_files __ARGS((void));
901 static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
902 static void spell_find_cleanup __ARGS((suginfo_T *su));
903 static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
904 static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
905 static void suggest_try_special __ARGS((suginfo_T *su));
906 static void suggest_try_change __ARGS((suginfo_T *su));
907 static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
908 static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
910 static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
912 static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
913 static void score_comp_sal __ARGS((suginfo_T *su));
914 static void score_combine __ARGS((suginfo_T *su));
915 static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
916 static void suggest_try_soundalike_prep __ARGS((void));
917 static void suggest_try_soundalike __ARGS((suginfo_T *su));
918 static void suggest_try_soundalike_finish __ARGS((void));
919 static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
920 static int soundfold_find __ARGS((slang_T *slang, char_u *word));
921 static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
922 static void set_map_str __ARGS((slang_T *lp, char_u *map));
923 static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
924 static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
925 static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
926 static void add_banned __ARGS((suginfo_T *su, char_u *word));
927 static void rescore_suggestions __ARGS((suginfo_T *su));
928 static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
929 static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
930 static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
931 static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
932 static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
934 static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
936 static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
937 static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
938 static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
940 static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
942 static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
943 static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
944 static buf_T *open_spellbuf __ARGS((void));
945 static void close_spellbuf __ARGS((buf_T *buf));
948 * Use our own character-case definitions, because the current locale may
949 * differ from what the .spl file uses.
950 * These must not be called with negative number!
953 /* Non-multi-byte implementation. */
954 # define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
955 # define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
956 # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
958 # if defined(HAVE_WCHAR_H)
959 # include <wchar.h> /* for towupper() and towlower() */
961 /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
962 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
963 * the "w" library function for characters above 255 if available. */
964 # ifdef HAVE_TOWLOWER
965 # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
966 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
968 # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
969 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
972 # ifdef HAVE_TOWUPPER
973 # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
974 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
976 # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
977 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
980 # ifdef HAVE_ISWUPPER
981 # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
982 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
984 # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
985 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
990 static char *e_format = N_("E759: Format error in spell file");
991 static char *e_spell_trunc = N_("E758: Truncated spell file");
992 static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
993 static char *e_affname = N_("Affix name too long in %s line %d: %s");
994 static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
995 static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
996 static char *msg_compressing = N_("Compressing word tree...");
998 /* Remember what "z?" replaced. */
999 static char_u *repl_from = NULL;
1000 static char_u *repl_to = NULL;
1003 * Main spell-checking function.
1004 * "ptr" points to a character that could be the start of a word.
1005 * "*attrp" is set to the highlight index for a badly spelled word. For a
1006 * non-word or when it's OK it remains unchanged.
1007 * This must only be called when 'spelllang' is not empty.
1009 * "capcol" is used to check for a Capitalised word after the end of a
1010 * sentence. If it's zero then perform the check. Return the column where to
1011 * check next, or -1 when no sentence end was found. If it's NULL then don't
1014 * Returns the length of the word in bytes, also when it's OK, so that the
1015 * caller can skip over the word.
1018 spell_check(wp, ptr, attrp, capcol, docount)
1019 win_T *wp; /* current window */
1022 int *capcol; /* column to check for Capital */
1023 int docount; /* count good words */
1025 matchinf_T mi; /* Most things are put in "mi" so that it can
1026 be passed to functions quickly. */
1027 int nrlen = 0; /* found a number first */
1029 int wrongcaplen = 0;
1031 int count_word = docount;
1033 /* A word never starts at a space or a control character. Return quickly
1034 * then, skipping over the character. */
1038 /* Return here when loading language files failed. */
1039 if (wp->w_s->b_langp.ga_len == 0)
1042 vim_memset(&mi, 0, sizeof(matchinf_T));
1044 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
1045 * 0X99FF. But always do check spelling to find "3GPP" and "11
1047 if (*ptr >= '0' && *ptr <= '9')
1049 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1050 mi.mi_end = skiphex(ptr + 2);
1052 mi.mi_end = skipdigits(ptr);
1053 nrlen = (int)(mi.mi_end - ptr);
1056 /* Find the normal end of the word (until the next non-word character). */
1059 if (spell_iswordp(mi.mi_fend, wp))
1063 mb_ptr_adv(mi.mi_fend);
1064 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
1066 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
1068 /* Check word starting with capital letter. */
1070 if (!SPELL_ISUPPER(c))
1071 wrongcaplen = (int)(mi.mi_fend - ptr);
1077 /* We always use the characters up to the next non-word character,
1078 * also for bad words. */
1079 mi.mi_end = mi.mi_fend;
1081 /* Check caps type later. */
1086 /* case-fold the word with one non-word character, so that we can check
1087 * for the word end. */
1088 if (*mi.mi_fend != NUL)
1089 mb_ptr_adv(mi.mi_fend);
1091 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1093 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
1095 /* The word is bad unless we recognize it. */
1096 mi.mi_result = SP_BAD;
1097 mi.mi_result2 = SP_BAD;
1100 * Loop over the languages specified in 'spelllang'.
1101 * We check them all, because a word may be matched longer in another
1104 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
1106 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
1108 /* If reloading fails the language is still in the list but everything
1109 * has been cleared. */
1110 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1113 /* Check for a matching word in case-folded words. */
1114 find_word(&mi, FIND_FOLDWORD);
1116 /* Check for a matching word in keep-case words. */
1117 find_word(&mi, FIND_KEEPWORD);
1119 /* Check for matching prefixes. */
1120 find_prefix(&mi, FIND_FOLDWORD);
1122 /* For a NOBREAK language, may want to use a word without a following
1123 * word as a backup. */
1124 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1125 && mi.mi_result2 != SP_BAD)
1127 mi.mi_result = mi.mi_result2;
1128 mi.mi_end = mi.mi_end2;
1131 /* Count the word in the first language where it's found to be OK. */
1132 if (count_word && mi.mi_result == SP_OK)
1134 count_common_word(mi.mi_lp->lp_slang, ptr,
1135 (int)(mi.mi_end - ptr), 1);
1140 if (mi.mi_result != SP_OK)
1142 /* If we found a number skip over it. Allows for "42nd". Do flag
1143 * rare and local words, e.g., "3GPP". */
1146 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1150 /* When we are at a non-word character there is no error, just
1151 * skip over the character (try looking for a word after it). */
1152 else if (!spell_iswordp_nmw(ptr))
1154 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
1156 regmatch_T regmatch;
1158 /* Check for end of sentence. */
1159 regmatch.regprog = wp->w_s->b_cap_prog;
1160 regmatch.rm_ic = FALSE;
1161 if (vim_regexec(®match, ptr, 0))
1162 *capcol = (int)(regmatch.endp[0] - ptr);
1167 return (*mb_ptr2len)(ptr);
1171 else if (mi.mi_end == ptr)
1172 /* Always include at least one character. Required for when there
1173 * is a mixup in "midword". */
1174 mb_ptr_adv(mi.mi_end);
1175 else if (mi.mi_result == SP_BAD
1176 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
1179 int save_result = mi.mi_result;
1181 /* First language in 'spelllang' is NOBREAK. Find first position
1182 * at which any word would be valid. */
1183 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
1184 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
1194 mi.mi_compoff = (int)(fp - mi.mi_fword);
1195 find_word(&mi, FIND_COMPOUND);
1196 if (mi.mi_result != SP_BAD)
1202 mi.mi_result = save_result;
1206 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1208 else if (mi.mi_result == SP_RARE)
1214 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1216 /* Report SpellCap only when the word isn't badly spelled. */
1221 return (int)(mi.mi_end - ptr);
1225 * Check if the word at "mip->mi_word" is in the tree.
1226 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1227 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1228 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1231 * For a match mip->mi_result is updated.
1234 find_word(mip, mode)
1239 int endlen[MAXWLEN]; /* length at possible word endings */
1240 idx_T endidx[MAXWLEN]; /* possible word endings */
1253 slang_T *slang = mip->mi_lp->lp_slang;
1261 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
1263 /* Check for word with matching case in keep-case tree. */
1265 flen = 9999; /* no case folding, always enough bytes */
1266 byts = slang->sl_kbyts;
1267 idxs = slang->sl_kidxs;
1269 if (mode == FIND_KEEPCOMPOUND)
1270 /* Skip over the previously found word(s). */
1271 wlen += mip->mi_compoff;
1275 /* Check for case-folded in case-folded tree. */
1276 ptr = mip->mi_fword;
1277 flen = mip->mi_fwordlen; /* available case-folded bytes */
1278 byts = slang->sl_fbyts;
1279 idxs = slang->sl_fidxs;
1281 if (mode == FIND_PREFIX)
1283 /* Skip over the prefix. */
1284 wlen = mip->mi_prefixlen;
1285 flen -= mip->mi_prefixlen;
1287 else if (mode == FIND_COMPOUND)
1289 /* Skip over the previously found word(s). */
1290 wlen = mip->mi_compoff;
1291 flen -= mip->mi_compoff;
1297 return; /* array is empty */
1300 * Repeat advancing in the tree until:
1301 * - there is a byte that doesn't match,
1302 * - we reach the end of the tree,
1303 * - or we reach the end of the line.
1307 if (flen <= 0 && *mip->mi_fend != NUL)
1308 flen = fold_more(mip);
1310 len = byts[arridx++];
1312 /* If the first possible byte is a zero the word could end here.
1313 * Remember this index, we first check for the longest word. */
1314 if (byts[arridx] == 0)
1316 if (endidxcnt == MAXWLEN)
1318 /* Must be a corrupted spell file. */
1322 endlen[endidxcnt] = wlen;
1323 endidx[endidxcnt++] = arridx++;
1326 /* Skip over the zeros, there can be several flag/region
1328 while (len > 0 && byts[arridx] == 0)
1334 break; /* no children, word must end here */
1337 /* Stop looking at end of the line. */
1338 if (ptr[wlen] == NUL)
1341 /* Perform a binary search in the list of accepted bytes. */
1343 if (c == TAB) /* <Tab> is handled like <Space> */
1346 hi = arridx + len - 1;
1352 else if (byts[m] < c)
1361 /* Stop if there is no matching byte. */
1362 if (hi < lo || byts[lo] != c)
1365 /* Continue at the child (if there is one). */
1370 /* One space in the good word may stand for several spaces in the
1376 if (flen <= 0 && *mip->mi_fend != NUL)
1377 flen = fold_more(mip);
1378 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1387 * Verify that one of the possible endings is valid. Try the longest
1390 while (endidxcnt > 0)
1393 arridx = endidx[endidxcnt];
1394 wlen = endlen[endidxcnt];
1397 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1398 continue; /* not at first byte of character */
1400 if (spell_iswordp(ptr + wlen, mip->mi_win))
1402 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
1403 continue; /* next char is a word character */
1408 /* The prefix flag is before compound flags. Once a valid prefix flag
1409 * has been found we try compound flags. */
1410 prefix_found = FALSE;
1413 if (mode != FIND_KEEPWORD && has_mbyte)
1415 /* Compute byte length in original word, length may change
1416 * when folding case. This can be slow, take a shortcut when the
1417 * case-folded word is equal to the keep-case word. */
1419 if (STRNCMP(ptr, p, wlen) != 0)
1421 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1423 wlen = (int)(p - mip->mi_word);
1428 /* Check flags and region. For FIND_PREFIX check the condition and
1430 * Repeat this if there are more flags/region alternatives until there
1433 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1436 flags = idxs[arridx];
1438 /* For the fold-case tree check that the case of the checked word
1439 * matches with what the word in the tree requires.
1440 * For keep-case tree the case is always right. For prefixes we
1441 * don't bother to check. */
1442 if (mode == FIND_FOLDWORD)
1444 if (mip->mi_cend != mip->mi_word + wlen)
1446 /* mi_capflags was set for a different word length, need
1447 * to do it again. */
1448 mip->mi_cend = mip->mi_word + wlen;
1449 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
1452 if (mip->mi_capflags == WF_KEEPCAP
1453 || !spell_valid_case(mip->mi_capflags, flags))
1457 /* When mode is FIND_PREFIX the word must support the prefix:
1458 * check the prefix ID and the condition. Do that for the list at
1459 * mip->mi_prefarridx that find_prefix() filled. */
1460 else if (mode == FIND_PREFIX && !prefix_found)
1462 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
1464 mip->mi_word + mip->mi_cprefixlen, slang,
1469 /* Use the WF_RARE flag for a rare prefix. */
1472 prefix_found = TRUE;
1475 if (slang->sl_nobreak)
1477 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1478 && (flags & WF_BANNED) == 0)
1480 /* NOBREAK: found a valid following word. That's all we
1481 * need to know, so return. */
1482 mip->mi_result = SP_OK;
1487 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1490 /* If there is no compound flag or the word is shorter than
1491 * COMPOUNDMIN reject it quickly.
1492 * Makes you wonder why someone puts a compound flag on a word
1493 * that's too short... Myspell compatibility requires this
1495 if (((unsigned)flags >> 24) == 0
1496 || wlen - mip->mi_compoff < slang->sl_compminlen)
1499 /* For multi-byte chars check character length against
1502 && slang->sl_compminlen > 0
1503 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1504 wlen - mip->mi_compoff) < slang->sl_compminlen)
1508 /* Limit the number of compound words to COMPOUNDWORDMAX if no
1509 * maximum for syllables is specified. */
1510 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1512 && slang->sl_compsylmax == MAXWLEN)
1515 /* Don't allow compounding on a side where an affix was added,
1516 * unless COMPOUNDPERMITFLAG was used. */
1517 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1519 if (!word_ends && (flags & WF_NOCOMPAFT))
1522 /* Quickly check if compounding is possible with this flag. */
1523 if (!byte_in_str(mip->mi_complen == 0
1524 ? slang->sl_compstartflags
1525 : slang->sl_compallflags,
1526 ((unsigned)flags >> 24)))
1529 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1530 * discard the compound word. */
1531 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1534 if (mode == FIND_COMPOUND)
1538 /* Need to check the caps type of the appended compound
1541 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1542 mip->mi_compoff) != 0)
1544 /* case folding may have changed the length */
1546 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1551 p = mip->mi_word + mip->mi_compoff;
1552 capflags = captype(p, mip->mi_word + wlen);
1553 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1554 && (flags & WF_FIXCAP) != 0))
1557 if (capflags != WF_ALLCAP)
1559 /* When the character before the word is a word
1560 * character we do not accept a Onecap word. We do
1561 * accept a no-caps word, even when the dictionary
1562 * word specifies ONECAP. */
1563 mb_ptr_back(mip->mi_word, p);
1564 if (spell_iswordp_nmw(p)
1565 ? capflags == WF_ONECAP
1566 : (flags & WF_ONECAP) != 0
1567 && capflags != WF_ONECAP)
1572 /* If the word ends the sequence of compound flags of the
1573 * words must match with one of the COMPOUNDRULE items and
1574 * the number of syllables must not be too large. */
1575 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1576 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1579 char_u fword[MAXWLEN];
1581 if (slang->sl_compsylmax < MAXWLEN)
1583 /* "fword" is only needed for checking syllables. */
1584 if (ptr == mip->mi_word)
1585 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1587 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1589 if (!can_compound(slang, fword, mip->mi_compflags))
1592 else if (slang->sl_comprules != NULL
1593 && !match_compoundrule(slang, mip->mi_compflags))
1594 /* The compound flags collected so far do not match any
1595 * COMPOUNDRULE, discard the compounded word. */
1599 /* Check NEEDCOMPOUND: can't use word without compounding. */
1600 else if (flags & WF_NEEDCOMP)
1603 nobreak_result = SP_OK;
1607 int save_result = mip->mi_result;
1608 char_u *save_end = mip->mi_end;
1609 langp_T *save_lp = mip->mi_lp;
1612 /* Check that a valid word follows. If there is one and we
1613 * are compounding, it will set "mi_result", thus we are
1614 * always finished here. For NOBREAK we only check that a
1615 * valid word follows.
1617 if (slang->sl_nobreak)
1618 mip->mi_result = SP_BAD;
1620 /* Find following word in case-folded tree. */
1621 mip->mi_compoff = endlen[endidxcnt];
1623 if (has_mbyte && mode == FIND_KEEPWORD)
1625 /* Compute byte length in case-folded word from "wlen":
1626 * byte length in keep-case word. Length may change when
1627 * folding case. This can be slow, take a shortcut when
1628 * the case-folded word is equal to the keep-case word. */
1630 if (STRNCMP(ptr, p, wlen) != 0)
1632 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1634 mip->mi_compoff = (int)(p - mip->mi_fword);
1638 c = mip->mi_compoff;
1640 if (flags & WF_COMPROOT)
1641 ++mip->mi_compextra;
1643 /* For NOBREAK we need to try all NOBREAK languages, at least
1644 * to find the ".add" file(s). */
1645 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
1647 if (slang->sl_nobreak)
1649 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
1650 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1651 || !mip->mi_lp->lp_slang->sl_nobreak)
1655 find_word(mip, FIND_COMPOUND);
1657 /* When NOBREAK any word that matches is OK. Otherwise we
1658 * need to find the longest match, thus try with keep-case
1659 * and prefix too. */
1660 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1662 /* Find following word in keep-case tree. */
1663 mip->mi_compoff = wlen;
1664 find_word(mip, FIND_KEEPCOMPOUND);
1666 #if 0 /* Disabled, a prefix must not appear halfway a compound word,
1667 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1668 postponed prefix. */
1669 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1671 /* Check for following word with prefix. */
1672 mip->mi_compoff = c;
1673 find_prefix(mip, FIND_COMPOUND);
1678 if (!slang->sl_nobreak)
1682 if (flags & WF_COMPROOT)
1683 --mip->mi_compextra;
1684 mip->mi_lp = save_lp;
1686 if (slang->sl_nobreak)
1688 nobreak_result = mip->mi_result;
1689 mip->mi_result = save_result;
1690 mip->mi_end = save_end;
1694 if (mip->mi_result == SP_OK)
1700 if (flags & WF_BANNED)
1702 else if (flags & WF_REGION)
1705 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
1710 else if (flags & WF_RARE)
1715 /* Always use the longest match and the best result. For NOBREAK
1716 * we separately keep the longest match without a following good
1717 * word as a fall-back. */
1718 if (nobreak_result == SP_BAD)
1720 if (mip->mi_result2 > res)
1722 mip->mi_result2 = res;
1723 mip->mi_end2 = mip->mi_word + wlen;
1725 else if (mip->mi_result2 == res
1726 && mip->mi_end2 < mip->mi_word + wlen)
1727 mip->mi_end2 = mip->mi_word + wlen;
1729 else if (mip->mi_result > res)
1731 mip->mi_result = res;
1732 mip->mi_end = mip->mi_word + wlen;
1734 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
1735 mip->mi_end = mip->mi_word + wlen;
1737 if (mip->mi_result == SP_OK)
1741 if (mip->mi_result == SP_OK)
1747 * Return TRUE if there is a match between the word ptr[wlen] and
1748 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1750 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1751 * end of ptr[wlen] and the second part matches after it.
1754 match_checkcompoundpattern(ptr, wlen, gap)
1757 garray_T *gap; /* &sl_comppat */
1763 for (i = 0; i + 1 < gap->ga_len; i += 2)
1765 p = ((char_u **)gap->ga_data)[i + 1];
1766 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1768 /* Second part matches at start of following compound word, now
1769 * check if first part matches at end of previous word. */
1770 p = ((char_u **)gap->ga_data)[i];
1771 len = (int)STRLEN(p);
1772 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1780 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1781 * does not have too many syllables.
1784 can_compound(slang, word, flags)
1789 regmatch_T regmatch;
1791 char_u uflags[MAXWLEN * 2];
1796 if (slang->sl_compprog == NULL)
1801 /* Need to convert the single byte flags to utf8 characters. */
1803 for (i = 0; flags[i] != NUL; ++i)
1804 p += mb_char2bytes(flags[i], p);
1811 regmatch.regprog = slang->sl_compprog;
1812 regmatch.rm_ic = FALSE;
1813 if (!vim_regexec(®match, p, 0))
1816 /* Count the number of syllables. This may be slow, do it last. If there
1817 * are too many syllables AND the number of compound words is above
1818 * COMPOUNDWORDMAX then compounding is not allowed. */
1819 if (slang->sl_compsylmax < MAXWLEN
1820 && count_syllables(slang, word) > slang->sl_compsylmax)
1821 return (int)STRLEN(flags) < slang->sl_compmax;
1826 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1827 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1828 * lines if they don't contain wildcards.
1831 can_be_compound(sp, slang, compflags, flag)
1837 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1838 * then it can't possibly compound. */
1839 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1840 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1843 /* If there are no wildcards, we can check if the flags collected so far
1844 * possibly can form a match with COMPOUNDRULE patterns. This only
1845 * makes sense when we have two or more words. */
1846 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1850 compflags[sp->ts_complen] = flag;
1851 compflags[sp->ts_complen + 1] = NUL;
1852 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1853 compflags[sp->ts_complen] = NUL;
1862 * Return TRUE if the compound flags in compflags[] match the start of any
1863 * compound rule. This is used to stop trying a compound if the flags
1864 * collected so far can't possibly match any compound rule.
1865 * Caller must check that slang->sl_comprules is not NULL.
1868 match_compoundrule(slang, compflags)
1876 /* loop over all the COMPOUNDRULE entries */
1877 for (p = slang->sl_comprules; *p != NUL; ++p)
1879 /* loop over the flags in the compound word we have made, match
1880 * them against the current rule entry */
1885 /* found a rule that matches for the flags we have so far */
1887 if (*p == '/' || *p == NUL)
1888 break; /* end of rule, it's too short */
1893 /* compare against all the flags in [] */
1895 while (*p != ']' && *p != NUL)
1899 break; /* none matches */
1902 break; /* flag of word doesn't match flag in pattern */
1906 /* Skip to the next "/", where the next pattern starts. */
1907 p = vim_strchr(p, '/');
1912 /* Checked all the rules and none of them match the flags, so there
1913 * can't possibly be a compound starting with these flags. */
1918 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1919 * ID in "flags" for the word "word".
1920 * The WF_RAREPFX flag is included in the return value for a rare prefix.
1923 valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
1924 int totprefcnt; /* nr of prefix IDs */
1925 int arridx; /* idx in sl_pidxs[] */
1929 int cond_req; /* only use prefixes with a condition */
1934 regmatch_T regmatch;
1937 prefid = (unsigned)flags >> 24;
1938 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1940 pidx = slang->sl_pidxs[arridx + prefcnt];
1942 /* Check the prefix ID. */
1943 if (prefid != (pidx & 0xff))
1946 /* Check if the prefix doesn't combine and the word already has a
1948 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1951 /* Check the condition, if there is one. The condition index is
1952 * stored in the two bytes above the prefix ID byte. */
1953 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1956 regmatch.regprog = rp;
1957 regmatch.rm_ic = FALSE;
1958 if (!vim_regexec(®match, word, 0))
1964 /* It's a match! Return the WF_ flags. */
1971 * Check if the word at "mip->mi_word" has a matching prefix.
1972 * If it does, then check the following word.
1974 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1975 * prefix in a compound word.
1977 * For a match mip->mi_result is updated.
1980 find_prefix(mip, mode)
1991 slang_T *slang = mip->mi_lp->lp_slang;
1995 byts = slang->sl_pbyts;
1997 return; /* array is empty */
1999 /* We use the case-folded word here, since prefixes are always
2001 ptr = mip->mi_fword;
2002 flen = mip->mi_fwordlen; /* available case-folded bytes */
2003 if (mode == FIND_COMPOUND)
2005 /* Skip over the previously found word(s). */
2006 ptr += mip->mi_compoff;
2007 flen -= mip->mi_compoff;
2009 idxs = slang->sl_pidxs;
2012 * Repeat advancing in the tree until:
2013 * - there is a byte that doesn't match,
2014 * - we reach the end of the tree,
2015 * - or we reach the end of the line.
2019 if (flen == 0 && *mip->mi_fend != NUL)
2020 flen = fold_more(mip);
2022 len = byts[arridx++];
2024 /* If the first possible byte is a zero the prefix could end here.
2025 * Check if the following word matches and supports the prefix. */
2026 if (byts[arridx] == 0)
2028 /* There can be several prefixes with different conditions. We
2029 * try them all, since we don't know which one will give the
2030 * longest match. The word is the same each time, pass the list
2031 * of possible prefixes to find_word(). */
2032 mip->mi_prefarridx = arridx;
2033 mip->mi_prefcnt = len;
2034 while (len > 0 && byts[arridx] == 0)
2039 mip->mi_prefcnt -= len;
2041 /* Find the word that comes after the prefix. */
2042 mip->mi_prefixlen = wlen;
2043 if (mode == FIND_COMPOUND)
2044 /* Skip over the previously found word(s). */
2045 mip->mi_prefixlen += mip->mi_compoff;
2050 /* Case-folded length may differ from original length. */
2051 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2052 mip->mi_prefixlen, mip->mi_word);
2055 mip->mi_cprefixlen = mip->mi_prefixlen;
2057 find_word(mip, FIND_PREFIX);
2061 break; /* no children, word must end here */
2064 /* Stop looking at end of the line. */
2065 if (ptr[wlen] == NUL)
2068 /* Perform a binary search in the list of accepted bytes. */
2071 hi = arridx + len - 1;
2077 else if (byts[m] < c)
2086 /* Stop if there is no matching byte. */
2087 if (hi < lo || byts[lo] != c)
2090 /* Continue at the child (if there is one). */
2098 * Need to fold at least one more character. Do until next non-word character
2099 * for efficiency. Include the non-word character too.
2100 * Return the length of the folded chars in bytes.
2112 mb_ptr_adv(mip->mi_fend);
2113 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
2115 /* Include the non-word character so that we can check for the word end. */
2116 if (*mip->mi_fend != NUL)
2117 mb_ptr_adv(mip->mi_fend);
2119 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2120 mip->mi_fword + mip->mi_fwordlen,
2121 MAXWLEN - mip->mi_fwordlen);
2122 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
2123 mip->mi_fwordlen += flen;
2128 * Check case flags for a word. Return TRUE if the word has the requested
2132 spell_valid_case(wordflags, treeflags)
2133 int wordflags; /* flags for the checked word. */
2134 int treeflags; /* flags for the word in the spell tree */
2136 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
2137 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
2138 && ((treeflags & WF_ONECAP) == 0
2139 || (wordflags & WF_ONECAP) != 0)));
2143 * Return TRUE if spell checking is not enabled.
2146 no_spell_checking(wp)
2149 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2150 || wp->w_s->b_langp.ga_len == 0)
2152 EMSG(_("E756: Spell checking is not enabled"));
2159 * Move to next spell error.
2160 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2161 * "curline" is TRUE to find word under/after cursor in the same line.
2162 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2163 * to after badly spelled word before the cursor.
2164 * Return 0 if not found, length of the badly spelled word otherwise.
2167 spell_move_to(wp, dir, allwords, curline, attrp)
2169 int dir; /* FORWARD or BACKWARD */
2170 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
2172 hlf_T *attrp; /* return: attributes of bad word or NULL
2173 (only when "dir" is FORWARD) */
2184 int has_syntax = syntax_present(wp);
2192 int found_one = FALSE;
2193 int wrapped = FALSE;
2195 if (no_spell_checking(wp))
2199 * Start looking for bad word at the start of the line, because we can't
2200 * start halfway a word, we don't know where it starts or ends.
2202 * When searching backwards, we continue in the line to find the last
2203 * bad word (in the cursor line: before the cursor).
2205 * We concatenate the start of the next line, so that wrapped words work
2206 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2209 lnum = wp->w_cursor.lnum;
2210 clearpos(&found_pos);
2214 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2216 len = (int)STRLEN(line);
2217 if (buflen < len + MAXWLEN + 2)
2220 buflen = len + MAXWLEN + 2;
2221 buf = alloc(buflen);
2226 /* In first line check first word for Capital. */
2230 /* For checking first word with a capital skip white space. */
2232 capcol = (int)(skipwhite(line) - line);
2233 else if (curline && wp == curwin)
2235 /* For spellbadword(): check if first word needs a capital. */
2236 col = (int)(skipwhite(line) - line);
2237 if (check_need_cap(lnum, col))
2240 /* Need to get the line again, may have looked at the previous
2242 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2245 /* Copy the line into "buf" and append the start of the next line if
2248 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2249 spell_cat_line(buf + STRLEN(buf),
2250 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
2256 /* When searching backward don't search after the cursor. Unless
2257 * we wrapped around the end of the buffer. */
2259 && lnum == wp->w_cursor.lnum
2261 && (colnr_T)(p - buf) >= wp->w_cursor.col)
2266 len = spell_check(wp, p, &attr, &capcol, FALSE);
2268 if (attr != HLF_COUNT)
2270 /* We found a bad word. Check the attribute. */
2271 if (allwords || attr == HLF_SPB)
2273 /* When searching forward only accept a bad word after
2276 || lnum != wp->w_cursor.lnum
2277 || (lnum == wp->w_cursor.lnum
2279 || (colnr_T)(curline ? p - buf + len
2281 > wp->w_cursor.col)))
2286 col = (int)(p - buf);
2287 (void)syn_get_id(wp, lnum, (colnr_T)col,
2288 FALSE, &can_spell, FALSE);
2299 found_pos.lnum = lnum;
2300 found_pos.col = (int)(p - buf);
2301 #ifdef FEAT_VIRTUALEDIT
2302 found_pos.coladd = 0;
2306 /* No need to search further. */
2307 wp->w_cursor = found_pos;
2314 /* Insert mode completion: put cursor after
2316 found_pos.col += len;
2325 /* advance to character after the word */
2330 if (dir == BACKWARD && found_pos.lnum != 0)
2332 /* Use the last match in the line (before the cursor). */
2333 wp->w_cursor = found_pos;
2339 break; /* only check cursor line */
2341 /* Advance to next line. */
2342 if (dir == BACKWARD)
2344 /* If we are back at the starting line and searched it again there
2345 * is no match, give up. */
2346 if (lnum == wp->w_cursor.lnum && wrapped)
2352 break; /* at first line and 'nowrapscan' */
2355 /* Wrap around to the end of the buffer. May search the
2356 * starting line again and accept the last match. */
2357 lnum = wp->w_buffer->b_ml.ml_line_count;
2359 if (!shortmess(SHM_SEARCH))
2360 give_warning((char_u *)_(top_bot_msg), TRUE);
2366 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2369 break; /* at first line and 'nowrapscan' */
2372 /* Wrap around to the start of the buffer. May search the
2373 * starting line again and accept the first match. */
2376 if (!shortmess(SHM_SEARCH))
2377 give_warning((char_u *)_(bot_top_msg), TRUE);
2380 /* If we are back at the starting line and there is no match then
2382 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
2385 /* Skip the characters at the start of the next line that were
2386 * included in a match crossing line boundaries. */
2387 if (attr == HLF_COUNT)
2388 skip = (int)(p - endp);
2392 /* Capcol skips over the inserted space. */
2395 /* But after empty line check first word in next line */
2396 if (*skipwhite(line) == NUL)
2408 * For spell checking: concatenate the start of the following line "line" into
2409 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2410 * Keep the blanks at the start of the next line, this is used in win_line()
2411 * to skip those bytes if the word was OK.
2414 spell_cat_line(buf, line, maxlen)
2422 p = skipwhite(line);
2423 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2424 p = skipwhite(p + 1);
2428 /* Only worth concatenating if there is something else than spaces to
2430 n = (int)(p - line) + 1;
2433 vim_memset(buf, ' ', n);
2434 vim_strncpy(buf + n, p, maxlen - 1 - n);
2440 * Structure used for the cookie argument of do_in_runtimepath().
2442 typedef struct spelload_S
2444 char_u sl_lang[MAXWLEN + 1]; /* language name */
2445 slang_T *sl_slang; /* resulting slang_T struct */
2446 int sl_nobreak; /* NOBREAK language found */
2450 * Load word list(s) for "lang" from Vim spell file(s).
2451 * "lang" must be the language without the region: e.g., "en".
2454 spell_load_lang(lang)
2457 char_u fname_enc[85];
2464 /* Copy the language name to pass it to spell_load_cb() as a cookie.
2465 * It's truncated when an error is detected. */
2466 STRCPY(sl.sl_lang, lang);
2468 sl.sl_nobreak = FALSE;
2471 /* We may retry when no spell file is found for the language, an
2472 * autocommand may load it then. */
2473 for (round = 1; round <= 2; ++round)
2477 * Find the first spell file for "lang" in 'runtimepath' and load it.
2479 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2486 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2488 if (r == FAIL && *sl.sl_lang != NUL)
2490 /* Try loading the ASCII version. */
2491 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2493 "spell/%s_ascii.spl",
2495 "spell/%s.ascii.spl",
2498 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2501 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2502 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2503 curbuf->b_fname, FALSE, curbuf))
2517 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
2519 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2521 lang, spell_enc(), lang);
2523 else if (sl.sl_slang != NULL)
2525 /* At least one file was loaded, now load ALL the additions. */
2526 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
2527 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
2532 * Return the encoding used for spell checking: Use 'encoding', except that we
2533 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2540 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2543 return (char_u *)"latin1";
2547 * Get the name of the .spl file for the internal wordlist into
2548 * "fname[MAXPATHL]".
2551 int_wordlist_spl(fname)
2554 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
2555 int_wordlist, spell_enc());
2559 * Allocate a new slang_T for language "lang". "lang" can be NULL.
2560 * Caller must fill "sl_next".
2568 lp = (slang_T *)alloc_clear(sizeof(slang_T));
2572 lp->sl_name = vim_strsave(lang);
2573 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
2574 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
2575 lp->sl_compmax = MAXWLEN;
2576 lp->sl_compsylmax = MAXWLEN;
2577 hash_init(&lp->sl_wordcount);
2584 * Free the contents of an slang_T and the structure itself.
2590 vim_free(lp->sl_name);
2591 vim_free(lp->sl_fname);
2597 * Clear an slang_T so that the file can be reloaded.
2609 vim_free(lp->sl_fbyts);
2610 lp->sl_fbyts = NULL;
2611 vim_free(lp->sl_kbyts);
2612 lp->sl_kbyts = NULL;
2613 vim_free(lp->sl_pbyts);
2614 lp->sl_pbyts = NULL;
2616 vim_free(lp->sl_fidxs);
2617 lp->sl_fidxs = NULL;
2618 vim_free(lp->sl_kidxs);
2619 lp->sl_kidxs = NULL;
2620 vim_free(lp->sl_pidxs);
2621 lp->sl_pidxs = NULL;
2623 for (round = 1; round <= 2; ++round)
2625 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2626 while (gap->ga_len > 0)
2628 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2629 vim_free(ftp->ft_from);
2630 vim_free(ftp->ft_to);
2638 /* "ga_len" is set to 1 without adding an item for latin1 */
2639 if (gap->ga_data != NULL)
2640 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2641 for (i = 0; i < gap->ga_len; ++i)
2642 vim_free(((int **)gap->ga_data)[i]);
2645 /* SAL items: free salitem_T items */
2646 while (gap->ga_len > 0)
2648 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2649 vim_free(smp->sm_lead);
2650 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2651 vim_free(smp->sm_to);
2653 vim_free(smp->sm_lead_w);
2654 vim_free(smp->sm_oneof_w);
2655 vim_free(smp->sm_to_w);
2660 for (i = 0; i < lp->sl_prefixcnt; ++i)
2661 vim_free(lp->sl_prefprog[i]);
2662 lp->sl_prefixcnt = 0;
2663 vim_free(lp->sl_prefprog);
2664 lp->sl_prefprog = NULL;
2666 vim_free(lp->sl_info);
2669 vim_free(lp->sl_midword);
2670 lp->sl_midword = NULL;
2672 vim_free(lp->sl_compprog);
2673 vim_free(lp->sl_comprules);
2674 vim_free(lp->sl_compstartflags);
2675 vim_free(lp->sl_compallflags);
2676 lp->sl_compprog = NULL;
2677 lp->sl_comprules = NULL;
2678 lp->sl_compstartflags = NULL;
2679 lp->sl_compallflags = NULL;
2681 vim_free(lp->sl_syllable);
2682 lp->sl_syllable = NULL;
2683 ga_clear(&lp->sl_syl_items);
2685 ga_clear_strings(&lp->sl_comppat);
2687 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2688 hash_init(&lp->sl_wordcount);
2691 hash_clear_all(&lp->sl_map_hash, 0);
2694 /* Clear info from .sug file. */
2695 slang_clear_sug(lp);
2697 lp->sl_compmax = MAXWLEN;
2698 lp->sl_compminlen = 0;
2699 lp->sl_compsylmax = MAXWLEN;
2700 lp->sl_regions[0] = NUL;
2704 * Clear the info from the .sug file in "lp".
2710 vim_free(lp->sl_sbyts);
2711 lp->sl_sbyts = NULL;
2712 vim_free(lp->sl_sidxs);
2713 lp->sl_sidxs = NULL;
2714 close_spellbuf(lp->sl_sugbuf);
2715 lp->sl_sugbuf = NULL;
2716 lp->sl_sugloaded = FALSE;
2721 * Load one spell file and store the info into a slang_T.
2722 * Invoked through do_in_runtimepath().
2725 spell_load_cb(fname, cookie)
2729 spelload_T *slp = (spelload_T *)cookie;
2732 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2735 /* When a previously loaded file has NOBREAK also use it for the
2737 if (slp->sl_nobreak && slang->sl_add)
2738 slang->sl_nobreak = TRUE;
2739 else if (slang->sl_nobreak)
2740 slp->sl_nobreak = TRUE;
2742 slp->sl_slang = slang;
2747 * Load one spell file and store the info into a slang_T.
2749 * This is invoked in three ways:
2750 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2751 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2752 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2753 * points to the existing slang_T.
2754 * - Just after writing a .spl file; it's read back to produce the .sug file.
2755 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2757 * Returns the slang_T the spell file was loaded into. NULL for error.
2760 spell_load_file(fname, lang, old_lp, silent)
2764 int silent; /* no error if file doesn't exist */
2767 char_u buf[VIMSPELLMAGICL];
2772 char_u *save_sourcing_name = sourcing_name;
2773 linenr_T save_sourcing_lnum = sourcing_lnum;
2778 fd = mch_fopen((char *)fname, "r");
2782 EMSG2(_(e_notopen), fname);
2783 else if (p_verbose > 2)
2786 smsg((char_u *)e_notopen, fname);
2794 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2800 lp = slang_alloc(lang);
2804 /* Remember the file name, used to reload the file when it's updated. */
2805 lp->sl_fname = vim_strsave(fname);
2806 if (lp->sl_fname == NULL)
2809 /* Check for .add.spl (_add.spl for VMS). */
2810 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
2815 /* Set sourcing_name, so that error messages mention the file name. */
2816 sourcing_name = fname;
2820 * <HEADER>: <fileID>
2822 for (i = 0; i < VIMSPELLMAGICL; ++i)
2823 buf[i] = getc(fd); /* <fileID> */
2824 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2826 EMSG(_("E757: This does not look like a spell file"));
2829 c = getc(fd); /* <versionnr> */
2830 if (c < VIMSPELLVERSION)
2832 EMSG(_("E771: Old spell file, needs to be updated"));
2835 else if (c > VIMSPELLVERSION)
2837 EMSG(_("E772: Spell file is for newer version of Vim"));
2843 * <SECTIONS>: <section> ... <sectionend>
2844 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2848 n = getc(fd); /* <sectionID> or <sectionend> */
2851 c = getc(fd); /* <sectionflags> */
2852 len = get4c(fd); /* <sectionlen> */
2860 lp->sl_info = read_string(fd, len); /* <infotext> */
2861 if (lp->sl_info == NULL)
2866 res = read_region_section(fd, lp, len);
2870 res = read_charflags_section(fd);
2874 lp->sl_midword = read_string(fd, len); /* <midword> */
2875 if (lp->sl_midword == NULL)
2880 res = read_prefcond_section(fd, lp);
2884 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2888 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
2892 res = read_sal_section(fd, lp);
2896 res = read_sofo_section(fd, lp);
2900 p = read_string(fd, len); /* <mapstr> */
2908 res = read_words_section(fd, lp, len);
2912 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
2915 case SN_NOSPLITSUGS:
2916 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2920 res = read_compound(fd, lp, len);
2924 lp->sl_nobreak = TRUE;
2928 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2929 if (lp->sl_syllable == NULL)
2931 if (init_syl_tab(lp) == FAIL)
2936 /* Unsupported section. When it's required give an error
2937 * message. When it's not required skip the contents. */
2938 if (c & SNF_REQUIRED)
2940 EMSG(_("E770: Unsupported section in spell file"));
2949 if (res == SP_FORMERROR)
2954 if (res == SP_TRUNCERROR)
2957 EMSG(_(e_spell_trunc));
2960 if (res == SP_OTHERERROR)
2965 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2970 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2975 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2980 /* For a new file link it in the list of spell files. */
2981 if (old_lp == NULL && lang != NULL)
2983 lp->sl_next = first_lang;
2991 /* truncating the name signals the error to spell_load_lang() */
2993 if (lp != NULL && old_lp == NULL)
3000 sourcing_name = save_sourcing_name;
3001 sourcing_lnum = save_sourcing_lnum;
3007 * Read a length field from "fd" in "cnt_bytes" bytes.
3008 * Allocate memory, read the string into it and add a NUL at the end.
3009 * Returns NULL when the count is zero.
3010 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
3014 read_cnt_string(fd, cnt_bytes, cntp)
3023 /* read the length bytes, MSB first */
3024 for (i = 0; i < cnt_bytes; ++i)
3025 cnt = (cnt << 8) + getc(fd);
3028 *cntp = SP_TRUNCERROR;
3033 return NULL; /* nothing to read, return NULL */
3035 str = read_string(fd, cnt);
3037 *cntp = SP_OTHERERROR;
3042 * Read SN_REGION: <regionname> ...
3043 * Return SP_*ERROR flags.
3046 read_region_section(fd, lp, len)
3054 return SP_FORMERROR;
3055 for (i = 0; i < len; ++i)
3056 lp->sl_regions[i] = getc(fd); /* <regionname> */
3057 lp->sl_regions[len] = NUL;
3062 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3063 * <folcharslen> <folchars>
3064 * Return SP_*ERROR flags.
3067 read_charflags_section(fd)
3072 int flagslen, follen;
3074 /* <charflagslen> <charflags> */
3075 flags = read_cnt_string(fd, 1, &flagslen);
3079 /* <folcharslen> <folchars> */
3080 fol = read_cnt_string(fd, 2, &follen);
3087 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3088 if (flags != NULL && fol != NULL)
3089 set_spell_charflags(flags, flagslen, fol);
3094 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3095 if ((flags == NULL) != (fol == NULL))
3096 return SP_FORMERROR;
3101 * Read SN_PREFCOND section.
3102 * Return SP_*ERROR flags.
3105 read_prefcond_section(fd, lp)
3113 char_u buf[MAXWLEN + 1];
3115 /* <prefcondcnt> <prefcond> ... */
3116 cnt = get2c(fd); /* <prefcondcnt> */
3118 return SP_FORMERROR;
3120 lp->sl_prefprog = (regprog_T **)alloc_clear(
3121 (unsigned)sizeof(regprog_T *) * cnt);
3122 if (lp->sl_prefprog == NULL)
3123 return SP_OTHERERROR;
3124 lp->sl_prefixcnt = cnt;
3126 for (i = 0; i < cnt; ++i)
3128 /* <prefcond> : <condlen> <condstr> */
3129 n = getc(fd); /* <condlen> */
3130 if (n < 0 || n >= MAXWLEN)
3131 return SP_FORMERROR;
3133 /* When <condlen> is zero we have an empty condition. Otherwise
3134 * compile the regexp program used to check for the condition. */
3137 buf[0] = '^'; /* always match at one position only */
3140 *p++ = getc(fd); /* <condstr> */
3142 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3149 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
3150 * Return SP_*ERROR flags.
3153 read_rep_section(fd, gap, first)
3162 cnt = get2c(fd); /* <repcount> */
3164 return SP_TRUNCERROR;
3166 if (ga_grow(gap, cnt) == FAIL)
3167 return SP_OTHERERROR;
3169 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3170 for (; gap->ga_len < cnt; ++gap->ga_len)
3172 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3173 ftp->ft_from = read_cnt_string(fd, 1, &i);
3177 return SP_FORMERROR;
3178 ftp->ft_to = read_cnt_string(fd, 1, &i);
3181 vim_free(ftp->ft_from);
3184 return SP_FORMERROR;
3188 /* Fill the first-index table. */
3189 for (i = 0; i < 256; ++i)
3191 for (i = 0; i < gap->ga_len; ++i)
3193 ftp = &((fromto_T *)gap->ga_data)[i];
3194 if (first[*ftp->ft_from] == -1)
3195 first[*ftp->ft_from] = i;
3201 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3202 * Return SP_*ERROR flags.
3205 read_sal_section(fd, slang)
3217 slang->sl_sofo = FALSE;
3219 i = getc(fd); /* <salflags> */
3220 if (i & SAL_F0LLOWUP)
3221 slang->sl_followup = TRUE;
3222 if (i & SAL_COLLAPSE)
3223 slang->sl_collapse = TRUE;
3224 if (i & SAL_REM_ACCENTS)
3225 slang->sl_rem_accents = TRUE;
3227 cnt = get2c(fd); /* <salcount> */
3229 return SP_TRUNCERROR;
3231 gap = &slang->sl_sal;
3232 ga_init2(gap, sizeof(salitem_T), 10);
3233 if (ga_grow(gap, cnt + 1) == FAIL)
3234 return SP_OTHERERROR;
3236 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3237 for (; gap->ga_len < cnt; ++gap->ga_len)
3239 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3240 ccnt = getc(fd); /* <salfromlen> */
3242 return SP_TRUNCERROR;
3243 if ((p = alloc(ccnt + 2)) == NULL)
3244 return SP_OTHERERROR;
3247 /* Read up to the first special char into sm_lead. */
3248 for (i = 0; i < ccnt; ++i)
3250 c = getc(fd); /* <salfrom> */
3251 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3255 smp->sm_leadlen = (int)(p - smp->sm_lead);
3258 /* Put (abc) chars in sm_oneof, if any. */
3262 for (++i; i < ccnt; ++i)
3264 c = getc(fd); /* <salfrom> */
3274 smp->sm_oneof = NULL;
3276 /* Any following chars go in sm_rules. */
3279 /* store the char we got while checking for end of sm_lead */
3281 for (++i; i < ccnt; ++i)
3282 *p++ = getc(fd); /* <salfrom> */
3285 /* <saltolen> <salto> */
3286 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3289 vim_free(smp->sm_lead);
3296 /* convert the multi-byte strings to wide char strings */
3297 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3298 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3299 if (smp->sm_oneof == NULL)
3300 smp->sm_oneof_w = NULL;
3302 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3303 if (smp->sm_to == NULL)
3304 smp->sm_to_w = NULL;
3306 smp->sm_to_w = mb_str2wide(smp->sm_to);
3307 if (smp->sm_lead_w == NULL
3308 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3309 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3311 vim_free(smp->sm_lead);
3312 vim_free(smp->sm_to);
3313 vim_free(smp->sm_lead_w);
3314 vim_free(smp->sm_oneof_w);
3315 vim_free(smp->sm_to_w);
3316 return SP_OTHERERROR;
3322 if (gap->ga_len > 0)
3324 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3325 * that we need to check the index every time. */
3326 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3327 if ((p = alloc(1)) == NULL)
3328 return SP_OTHERERROR;
3331 smp->sm_leadlen = 0;
3332 smp->sm_oneof = NULL;
3338 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3339 smp->sm_leadlen = 0;
3340 smp->sm_oneof_w = NULL;
3341 smp->sm_to_w = NULL;
3347 /* Fill the first-index table. */
3348 set_sal_first(slang);
3354 * Read SN_WORDS: <word> ...
3355 * Return SP_*ERROR flags.
3358 read_words_section(fd, lp, len)
3366 char_u word[MAXWLEN];
3370 /* Read one word at a time. */
3375 return SP_TRUNCERROR;
3379 if (i == MAXWLEN - 1)
3380 return SP_FORMERROR;
3383 /* Init the count to 10. */
3384 count_common_word(lp, word, -1, 10);
3391 * Add a word to the hashtable of common words.
3392 * If it's already there then the counter is increased.
3395 count_common_word(lp, word, len, count)
3398 int len; /* word length, -1 for upto NUL */
3399 int count; /* 1 to count once, 10 to init */
3404 char_u buf[MAXWLEN];
3411 vim_strncpy(buf, word, len);
3415 hash = hash_hash(p);
3416 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3417 if (HASHITEM_EMPTY(hi))
3419 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
3422 STRCPY(wc->wc_word, p);
3423 wc->wc_count = count;
3424 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3429 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3430 wc->wc_count = MAXWORDCOUNT;
3435 * Adjust the score of common words.
3438 score_wordcount_adj(slang, score, word, split)
3442 int split; /* word was split, less bonus */
3449 hi = hash_find(&slang->sl_wordcount, word);
3450 if (!HASHITEM_EMPTY(hi))
3453 if (wc->wc_count < SCORE_THRES2)
3454 bonus = SCORE_COMMON1;
3455 else if (wc->wc_count < SCORE_THRES3)
3456 bonus = SCORE_COMMON2;
3458 bonus = SCORE_COMMON3;
3460 newscore = score - bonus / 2;
3462 newscore = score - bonus;
3471 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3472 * Return SP_*ERROR flags.
3475 read_sofo_section(fd, slang)
3483 slang->sl_sofo = TRUE;
3485 /* <sofofromlen> <sofofrom> */
3486 from = read_cnt_string(fd, 2, &cnt);
3490 /* <sofotolen> <sofoto> */
3491 to = read_cnt_string(fd, 2, &cnt);
3498 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3499 if (from != NULL && to != NULL)
3500 res = set_sofo(slang, from, to);
3501 else if (from != NULL || to != NULL)
3502 res = SP_FORMERROR; /* only one of two strings is an error */
3512 * Read the compound section from the .spl file:
3513 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
3514 * Returns SP_*ERROR flags.
3517 read_compound(fd, slang, len)
3534 return SP_FORMERROR; /* need at least two bytes */
3537 c = getc(fd); /* <compmax> */
3540 slang->sl_compmax = c;
3543 c = getc(fd); /* <compminlen> */
3546 slang->sl_compminlen = c;
3549 c = getc(fd); /* <compsylmax> */
3552 slang->sl_compsylmax = c;
3554 c = getc(fd); /* <compoptions> */
3556 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3560 c = getc(fd); /* only use the lower byte for now */
3562 slang->sl_compoptions = c;
3564 gap = &slang->sl_comppat;
3565 c = get2c(fd); /* <comppatcount> */
3567 ga_init2(gap, sizeof(char_u *), c);
3568 if (ga_grow(gap, c) == OK)
3571 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3572 read_cnt_string(fd, 1, &cnt);
3573 /* <comppatlen> <comppattext> */
3580 return SP_FORMERROR;
3582 /* Turn the COMPOUNDRULE items into a regexp pattern:
3583 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
3584 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3585 * Conversion to utf-8 may double the size. */
3591 pat = alloc((unsigned)c);
3593 return SP_OTHERERROR;
3595 /* We also need a list of all flags that can appear at the start and one
3597 cp = alloc(todo + 1);
3601 return SP_OTHERERROR;
3603 slang->sl_compstartflags = cp;
3606 ap = alloc(todo + 1);
3610 return SP_OTHERERROR;
3612 slang->sl_compallflags = ap;
3615 /* And a list of all patterns in their original form, for checking whether
3616 * compounding may work in match_compoundrule(). This is freed when we
3617 * encounter a wildcard, the check doesn't work then. */
3618 crp = alloc(todo + 1);
3619 slang->sl_comprules = crp;
3629 c = getc(fd); /* <compflags> */
3633 return SP_TRUNCERROR;
3636 /* Add all flags to "sl_compallflags". */
3637 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
3638 && !byte_in_str(slang->sl_compallflags, c))
3646 /* At start of item: copy flags to "sl_compstartflags". For a
3647 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3654 if (!byte_in_str(slang->sl_compstartflags, c))
3664 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3667 if (c == '?' || c == '+' || c == '*')
3669 vim_free(slang->sl_comprules);
3670 slang->sl_comprules = NULL;
3677 if (c == '/') /* slash separates two items */
3683 else /* normal char, "[abc]" and '*' are copied as-is */
3685 if (c == '?' || c == '+' || c == '~')
3686 *pp++ = '\\'; /* "a?" becomes "a\?", "a+" becomes "a\+" */
3689 pp += mb_char2bytes(c, pp);
3704 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3706 if (slang->sl_compprog == NULL)
3707 return SP_FORMERROR;
3713 * Return TRUE if byte "n" appears in "str".
3714 * Like strchr() but independent of locale.
3723 for (p = str; *p != NUL; ++p)
3729 #define SY_MAXLEN 30
3730 typedef struct syl_item_S
3732 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3737 * Truncate "slang->sl_syllable" at the first slash and put the following items
3738 * in "slang->sl_syl_items".
3749 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3750 p = vim_strchr(slang->sl_syllable, '/');
3754 if (*p == NUL) /* trailing slash */
3757 p = vim_strchr(p, '/');
3763 return SP_FORMERROR;
3764 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
3765 return SP_OTHERERROR;
3766 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3767 + slang->sl_syl_items.ga_len++;
3768 vim_strncpy(syl->sy_chars, s, l);
3775 * Count the number of syllables in "word".
3776 * When "word" contains spaces the syllables after the last space are counted.
3777 * Returns zero if syllables are not defines.
3780 count_syllables(slang, word)
3792 if (slang->sl_syllable == NULL)
3795 for (p = word; *p != NUL; p += len)
3797 /* When running into a space reset counter. */
3805 /* Find longest match of syllable items. */
3807 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3809 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3810 if (syl->sy_len > len
3811 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3814 if (len != 0) /* found a match, count syllable */
3821 /* No recognized syllable item, at least a syllable char then? */
3824 len = (*mb_ptr2len)(p);
3829 if (vim_strchr(slang->sl_syllable, c) == NULL)
3830 skip = FALSE; /* No, search for next syllable */
3833 ++cnt; /* Yes, count it */
3834 skip = TRUE; /* don't count following syllable chars */
3842 * Set the SOFOFROM and SOFOTO items in language "lp".
3843 * Returns SP_*ERROR flags when there is something wrong.
3846 set_sofo(lp, from, to)
3862 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3863 * characters. The index is the low byte of the character.
3864 * The list contains from-to pairs with a terminating NUL.
3865 * sl_sal_first[] is used for latin1 "from" characters. */
3867 ga_init2(gap, sizeof(int *), 1);
3868 if (ga_grow(gap, 256) == FAIL)
3869 return SP_OTHERERROR;
3870 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3873 /* First count the number of items for each list. Temporarily use
3874 * sl_sal_first[] for this. */
3875 for (p = from, s = to; *p != NUL && *s != NUL; )
3877 c = mb_cptr2char_adv(&p);
3880 ++lp->sl_sal_first[c & 0xff];
3882 if (*p != NUL || *s != NUL) /* lengths differ */
3883 return SP_FORMERROR;
3885 /* Allocate the lists. */
3886 for (i = 0; i < 256; ++i)
3887 if (lp->sl_sal_first[i] > 0)
3889 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3891 return SP_OTHERERROR;
3892 ((int **)gap->ga_data)[i] = (int *)p;
3896 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3898 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3899 for (p = from, s = to; *p != NUL && *s != NUL; )
3901 c = mb_cptr2char_adv(&p);
3902 i = mb_cptr2char_adv(&s);
3905 /* Append the from-to chars at the end of the list with
3907 inp = ((int **)gap->ga_data)[c & 0xff];
3910 *inp++ = c; /* from char */
3911 *inp++ = i; /* to char */
3912 *inp++ = NUL; /* NUL at the end */
3915 /* mapping byte to char is done in sl_sal_first[] */
3916 lp->sl_sal_first[c] = i;
3922 /* mapping bytes to bytes is done in sl_sal_first[] */
3923 if (STRLEN(from) != STRLEN(to))
3924 return SP_FORMERROR;
3926 for (i = 0; to[i] != NUL; ++i)
3927 lp->sl_sal_first[from[i]] = to[i];
3928 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3935 * Fill the first-index table for "lp".
3945 garray_T *gap = &lp->sl_sal;
3947 sfirst = lp->sl_sal_first;
3948 for (i = 0; i < 256; ++i)
3950 smp = (salitem_T *)gap->ga_data;
3951 for (i = 0; i < gap->ga_len; ++i)
3955 /* Use the lowest byte of the first character. For latin1 it's
3956 * the character, for other encodings it should differ for most
3958 c = *smp[i].sm_lead_w & 0xff;
3961 c = *smp[i].sm_lead;
3962 if (sfirst[c] == -1)
3970 /* Make sure all entries with this byte are following each
3971 * other. Move the ones that are in the wrong position. Do
3972 * keep the same ordering! */
3973 while (i + 1 < gap->ga_len
3974 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3975 /* Skip over entry with same index byte. */
3978 for (n = 1; i + n < gap->ga_len; ++n)
3979 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3983 /* Move entry with same index byte after the entries
3984 * we already found. */
3988 mch_memmove(smp + i + 1, smp + i,
3989 sizeof(salitem_T) * n);
4000 * Turn a multi-byte string into a wide character string.
4001 * Return it in allocated memory (NULL for out-of-memory)
4011 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
4014 for (p = s; *p != NUL; )
4015 res[i++] = mb_ptr2char_adv(&p);
4023 * Read a tree from the .spl or .sug file.
4024 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4025 * This is skipped when the tree has zero length.
4026 * Returns zero when OK, SP_ value for an error.
4029 spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4033 int prefixtree; /* TRUE for the prefix tree */
4034 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4041 /* The tree size was computed when writing the file, so that we can
4042 * allocate it as one long block. <nodecount> */
4045 return SP_TRUNCERROR;
4048 /* Allocate the byte array. */
4049 bp = lalloc((long_u)len, TRUE);
4051 return SP_OTHERERROR;
4054 /* Allocate the index array. */
4055 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4057 return SP_OTHERERROR;
4060 /* Recursively read the tree and store it in the array. */
4061 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4069 * Read one row of siblings from the spell file and store it in the byte array
4070 * "byts" and index array "idxs". Recursively read the children.
4072 * NOTE: The code here must match put_node()!
4074 * Returns the index (>= 0) following the siblings.
4075 * Returns SP_TRUNCERROR if the file is shorter than expected.
4076 * Returns SP_FORMERROR if there is a format error.
4079 read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
4083 int maxidx; /* size of arrays */
4084 idx_T startidx; /* current index in "byts" and "idxs" */
4085 int prefixtree; /* TRUE for reading PREFIXTREE */
4086 int maxprefcondnr; /* maximum for <prefcondnr> */
4091 idx_T idx = startidx;
4094 #define SHARED_MASK 0x8000000
4096 len = getc(fd); /* <siblingcount> */
4098 return SP_TRUNCERROR;
4100 if (startidx + len >= maxidx)
4101 return SP_FORMERROR;
4104 /* Read the byte values, flag/region bytes and shared indexes. */
4105 for (i = 1; i <= len; ++i)
4107 c = getc(fd); /* <byte> */
4109 return SP_TRUNCERROR;
4110 if (c <= BY_SPECIAL)
4112 if (c == BY_NOFLAGS && !prefixtree)
4114 /* No flags, all regions. */
4118 else if (c != BY_INDEX)
4122 /* Read the optional pflags byte, the prefix ID and the
4123 * condition nr. In idxs[] store the prefix ID in the low
4124 * byte, the condition index shifted up 8 bits, the flags
4125 * shifted up 24 bits. */
4127 c = getc(fd) << 24; /* <pflags> */
4131 c |= getc(fd); /* <affixID> */
4133 n = get2c(fd); /* <prefcondnr> */
4134 if (n >= maxprefcondnr)
4135 return SP_FORMERROR;
4138 else /* c must be BY_FLAGS or BY_FLAGS2 */
4140 /* Read flags and optional region and prefix ID. In
4141 * idxs[] the flags go in the low two bytes, region above
4142 * that and prefix ID above the region. */
4144 c = getc(fd); /* <flags> */
4145 if (c2 == BY_FLAGS2)
4146 c = (getc(fd) << 8) + c; /* <flags2> */
4148 c = (getc(fd) << 16) + c; /* <region> */
4150 c = (getc(fd) << 24) + c; /* <affixID> */
4156 else /* c == BY_INDEX */
4160 if (n < 0 || n >= maxidx)
4161 return SP_FORMERROR;
4162 idxs[idx] = n + SHARED_MASK;
4163 c = getc(fd); /* <xbyte> */
4169 /* Recursively read the children for non-shared siblings.
4170 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4171 * remove SHARED_MASK) */
4172 for (i = 1; i <= len; ++i)
4173 if (byts[startidx + i] != 0)
4175 if (idxs[startidx + i] & SHARED_MASK)
4176 idxs[startidx + i] &= ~SHARED_MASK;
4179 idxs[startidx + i] = idx;
4180 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
4181 prefixtree, maxprefcondnr);
4191 * Parse 'spelllang' and set w_s->b_langp accordingly.
4192 * Returns NULL if it's OK, an error message otherwise.
4195 did_set_spelllang(wp)
4201 char_u region_cp[3];
4206 char_u lang[MAXWLEN + 1];
4207 char_u spf_name[MAXPATHL];
4212 char_u *use_region = NULL;
4213 int dont_use_region = FALSE;
4214 int nobreak = FALSE;
4217 static int recursive = FALSE;
4218 char_u *ret_msg = NULL;
4221 /* We don't want to do this recursively. May happen when a language is
4222 * not available and the SpellFileMissing autocommand opens a new buffer
4223 * in which 'spell' is set. */
4228 ga_init2(&ga, sizeof(langp_T), 2);
4231 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4232 * it under our fingers. */
4233 spl_copy = vim_strsave(wp->w_s->b_p_spl);
4234 if (spl_copy == NULL)
4237 /* loop over comma separated language names. */
4238 for (splp = spl_copy; *splp != NUL; )
4240 /* Get one language name. */
4241 copy_option_part(&splp, lang, MAXWLEN, ",");
4243 len = (int)STRLEN(lang);
4245 /* If the name ends in ".spl" use it as the name of the spell file.
4246 * If there is a region name let "region" point to it and remove it
4248 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4252 /* Locate a region and remove it from the file name. */
4253 p = vim_strchr(gettail(lang), '_');
4254 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4255 && !ASCII_ISALPHA(p[3]))
4257 vim_strncpy(region_cp, p + 1, 2);
4258 mch_memmove(p, p + 3, len - (p - lang) - 2);
4263 dont_use_region = TRUE;
4265 /* Check if we loaded this language before. */
4266 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4267 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
4273 if (len > 3 && lang[len - 3] == '_')
4275 region = lang + len - 2;
4280 dont_use_region = TRUE;
4282 /* Check if we loaded this language before. */
4283 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4284 if (STRICMP(lang, slang->sl_name) == 0)
4290 /* If the region differs from what was used before then don't
4291 * use it for 'spellfile'. */
4292 if (use_region != NULL && STRCMP(region, use_region) != 0)
4293 dont_use_region = TRUE;
4294 use_region = region;
4297 /* If not found try loading the language now. */
4301 (void)spell_load_file(lang, lang, NULL, FALSE);
4304 spell_load_lang(lang);
4306 /* SpellFileMissing autocommands may do anything, including
4307 * destroying the buffer we are using... */
4308 if (!buf_valid(wp->w_buffer))
4310 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4318 * Loop over the languages, there can be several files for "lang".
4320 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4321 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4322 : STRICMP(lang, slang->sl_name) == 0)
4324 region_mask = REGION_ALL;
4325 if (!filename && region != NULL)
4327 /* find region in sl_regions */
4328 c = find_region(slang->sl_regions, region);
4329 if (c == REGION_ALL)
4333 if (*slang->sl_regions != NUL)
4334 /* This addition file is for other regions. */
4338 /* This is probably an error. Give a warning and
4339 * accept the words anyway. */
4341 _("Warning: region %s not supported"),
4345 region_mask = 1 << c;
4348 if (region_mask != 0)
4350 if (ga_grow(&ga, 1) == FAIL)
4353 ret_msg = e_outofmem;
4356 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4357 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4359 use_midword(slang, wp);
4360 if (slang->sl_nobreak)
4366 /* round 0: load int_wordlist, if possible.
4367 * round 1: load first name in 'spellfile'.
4368 * round 2: load second name in 'spellfile.
4370 spf = curwin->w_s->b_p_spf;
4371 for (round = 0; round == 0 || *spf != NUL; ++round)
4375 /* Internal wordlist, if there is one. */
4376 if (int_wordlist == NULL)
4378 int_wordlist_spl(spf_name);
4382 /* One entry in 'spellfile'. */
4383 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4384 STRCAT(spf_name, ".spl");
4386 /* If it was already found above then skip it. */
4387 for (c = 0; c < ga.ga_len; ++c)
4389 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4390 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
4397 /* Check if it was loaded already. */
4398 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4399 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
4403 /* Not loaded, try loading it now. The language name includes the
4404 * region name, the region is ignored otherwise. for int_wordlist
4405 * use an arbitrary name. */
4407 STRCPY(lang, "internal wordlist");
4410 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
4411 p = vim_strchr(lang, '.');
4413 *p = NUL; /* truncate at ".encoding.add" */
4415 slang = spell_load_file(spf_name, lang, NULL, TRUE);
4417 /* If one of the languages has NOBREAK we assume the addition
4418 * files also have this. */
4419 if (slang != NULL && nobreak)
4420 slang->sl_nobreak = TRUE;
4422 if (slang != NULL && ga_grow(&ga, 1) == OK)
4424 region_mask = REGION_ALL;
4425 if (use_region != NULL && !dont_use_region)
4427 /* find region in sl_regions */
4428 c = find_region(slang->sl_regions, use_region);
4429 if (c != REGION_ALL)
4430 region_mask = 1 << c;
4431 else if (*slang->sl_regions != NUL)
4432 /* This spell file is for other regions. */
4436 if (region_mask != 0)
4438 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4439 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4440 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
4441 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4443 use_midword(slang, wp);
4448 /* Everything is fine, store the new b_langp value. */
4449 ga_clear(&wp->w_s->b_langp);
4450 wp->w_s->b_langp = ga;
4452 /* For each language figure out what language to use for sound folding and
4453 * REP items. If the language doesn't support it itself use another one
4454 * with the same name. E.g. for "en-math" use "en". */
4455 for (i = 0; i < ga.ga_len; ++i)
4457 lp = LANGP_ENTRY(ga, i);
4460 if (lp->lp_slang->sl_sal.ga_len > 0)
4461 /* language does sound folding itself */
4462 lp->lp_sallang = lp->lp_slang;
4464 /* find first similar language that does sound folding */
4465 for (j = 0; j < ga.ga_len; ++j)
4467 lp2 = LANGP_ENTRY(ga, j);
4468 if (lp2->lp_slang->sl_sal.ga_len > 0
4469 && STRNCMP(lp->lp_slang->sl_name,
4470 lp2->lp_slang->sl_name, 2) == 0)
4472 lp->lp_sallang = lp2->lp_slang;
4478 if (lp->lp_slang->sl_rep.ga_len > 0)
4479 /* language has REP items itself */
4480 lp->lp_replang = lp->lp_slang;
4482 /* find first similar language that has REP items */
4483 for (j = 0; j < ga.ga_len; ++j)
4485 lp2 = LANGP_ENTRY(ga, j);
4486 if (lp2->lp_slang->sl_rep.ga_len > 0
4487 && STRNCMP(lp->lp_slang->sl_name,
4488 lp2->lp_slang->sl_name, 2) == 0)
4490 lp->lp_replang = lp2->lp_slang;
4503 * Clear the midword characters for buffer "buf".
4509 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
4511 vim_free(wp->w_s->b_spell_ismw_mb);
4512 wp->w_s->b_spell_ismw_mb = NULL;
4517 * Use the "sl_midword" field of language "lp" for buffer "buf".
4518 * They add up to any currently used midword characters.
4527 if (lp->sl_midword == NULL) /* there aren't any */
4530 for (p = lp->sl_midword; *p != NUL; )
4538 l = (*mb_ptr2len)(p);
4539 if (c < 256 && l <= 2)
4540 wp->w_s->b_spell_ismw[c] = TRUE;
4541 else if (wp->w_s->b_spell_ismw_mb == NULL)
4542 /* First multi-byte char in "b_spell_ismw_mb". */
4543 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
4546 /* Append multi-byte chars to "b_spell_ismw_mb". */
4547 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4548 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
4551 vim_free(wp->w_s->b_spell_ismw_mb);
4552 wp->w_s->b_spell_ismw_mb = bp;
4553 vim_strncpy(bp + n, p, l);
4560 wp->w_s->b_spell_ismw[*p++] = TRUE;
4564 * Find the region "region[2]" in "rp" (points to "sl_regions").
4565 * Each region is simply stored as the two characters of it's name.
4566 * Returns the index if found (first is 0), REGION_ALL if not found.
4569 find_region(rp, region)
4575 for (i = 0; ; i += 2)
4579 if (rp[i] == region[0] && rp[i + 1] == region[1])
4586 * Return case type of word:
4590 * WoRd wOrd WF_KEEPCAP
4595 char_u *end; /* When NULL use up to NUL byte. */
4601 int past_second = FALSE; /* past second word char */
4603 /* find first letter */
4604 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
4605 if (end == NULL ? *p == NUL : p >= end)
4606 return 0; /* only non-word characters, illegal word */
4609 c = mb_ptr2char_adv(&p);
4613 firstcap = allcap = SPELL_ISUPPER(c);
4616 * Need to check all letters to find a word with mixed upper/lower.
4617 * But a word with an upper char only at start is a ONECAP.
4619 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
4620 if (spell_iswordp_nmw(p))
4623 if (!SPELL_ISUPPER(c))
4625 /* UUl -> KEEPCAP */
4626 if (past_second && allcap)
4631 /* UlU -> KEEPCAP */
4644 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4645 * capital. So that make_case_word() can turn WOrd into Word.
4646 * Add ALLCAP for "WOrD".
4649 badword_captype(word, end)
4653 int flags = captype(word, end);
4659 if (flags & WF_KEEPCAP)
4661 /* Count the number of UPPER and lower case letters. */
4664 for (p = word; p < end; mb_ptr_adv(p))
4667 if (SPELL_ISUPPER(c))
4677 /* If there are more UPPER than lower case letters suggest an
4678 * ALLCAP word. Otherwise, if the first letter is UPPER then
4679 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4680 * require three upper case letters. */
4686 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4692 # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4694 * Free all languages.
4701 char_u fname[MAXPATHL];
4703 /* Go through all buffers and handle 'spelllang'. <VN> */
4704 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4705 ga_clear(&buf->b_s.b_langp);
4707 while (first_lang != NULL)
4710 first_lang = slang->sl_next;
4714 if (int_wordlist != NULL)
4716 /* Delete the internal wordlist and its .spl file */
4717 mch_remove(int_wordlist);
4718 int_wordlist_spl(fname);
4720 vim_free(int_wordlist);
4721 int_wordlist = NULL;
4724 init_spell_chartab();
4728 vim_free(repl_from);
4733 # if defined(FEAT_MBYTE) || defined(PROTO)
4735 * Clear all spelling tables and reload them.
4736 * Used after 'encoding' is set and when ":mkspell" was used.
4743 /* Initialize the table for spell_iswordp(). */
4744 init_spell_chartab();
4746 /* Unload all allocated memory. */
4749 /* Go through all buffers and handle 'spelllang'. */
4750 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4752 /* Only load the wordlists when 'spelllang' is set and there is a
4753 * window for this buffer in which 'spell' is set. */
4754 if (*wp->w_s->b_p_spl != NUL)
4758 (void)did_set_spelllang(wp);
4759 # ifdef FEAT_WINDOWS
4769 * Reload the spell file "fname" if it's loaded.
4772 spell_reload_one(fname, added_word)
4774 int added_word; /* invoked through "zg" */
4779 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4781 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
4784 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
4785 /* reloading failed, clear the language */
4787 redraw_all_later(SOME_VALID);
4792 /* When "zg" was used and the file wasn't loaded yet, should redo
4793 * 'spelllang' to load it now. */
4794 if (added_word && !didit)
4795 did_set_spelllang(curwin);
4800 * Functions for ":mkspell".
4803 #define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
4806 * Main structure to store the contents of a ".aff" file.
4808 typedef struct afffile_S
4810 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
4811 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
4812 unsigned af_rare; /* RARE ID for rare word */
4813 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
4814 unsigned af_bad; /* BAD ID for banned word */
4815 unsigned af_needaffix; /* NEEDAFFIX ID */
4816 unsigned af_circumfix; /* CIRCUMFIX ID */
4817 unsigned af_needcomp; /* NEEDCOMPOUND ID */
4818 unsigned af_comproot; /* COMPOUNDROOT ID */
4819 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4820 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
4821 unsigned af_nosuggest; /* NOSUGGEST ID */
4822 int af_pfxpostpone; /* postpone prefixes without chop string and
4824 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4825 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
4826 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
4829 #define AFT_CHAR 0 /* flags are one character */
4830 #define AFT_LONG 1 /* flags are two characters */
4831 #define AFT_CAPLONG 2 /* flags are one or two characters */
4832 #define AFT_NUM 3 /* flags are numbers, comma separated */
4834 typedef struct affentry_S affentry_T;
4835 /* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4838 affentry_T *ae_next; /* next affix with same name/number */
4839 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4840 char_u *ae_add; /* text to add to basic word (can be NULL) */
4841 char_u *ae_flags; /* flags on the affix (can be NULL) */
4842 char_u *ae_cond; /* condition (NULL for ".") */
4843 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
4844 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4845 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
4849 # define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4851 # define AH_KEY_LEN 7 /* 6 digits + NUL */
4854 /* Affix header from ".aff" file. Used for af_pref and af_suff. */
4855 typedef struct affheader_S
4857 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4858 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4859 int ah_newID; /* prefix ID after renumbering; 0 if not used */
4860 int ah_combine; /* suffix may combine with prefix */
4861 int ah_follows; /* another affix block should be following */
4862 affentry_T *ah_first; /* first affix entry */
4865 #define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4867 /* Flag used in compound items. */
4868 typedef struct compitem_S
4870 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4871 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4872 int ci_newID; /* affix ID after renumbering. */
4875 #define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4878 * Structure that is used to store the items in the word tree. This avoids
4879 * the need to keep track of each allocated thing, everything is freed all at
4880 * once after ":mkspell" is done.
4881 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4882 * "sb_data" is correct for systems where pointers must be aligned on
4883 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
4885 #define SBLOCKSIZE 16000 /* size of sb_data */
4886 typedef struct sblock_S sblock_T;
4889 int sb_used; /* nr of bytes already in use */
4890 sblock_T *sb_next; /* next block in list */
4891 char_u sb_data[1]; /* data, actually longer */
4895 * A node in the tree.
4897 typedef struct wordnode_S wordnode_T;
4900 union /* shared to save space */
4902 char_u hashkey[6]; /* the hash key, only used while compressing */
4903 int index; /* index in written nodes (valid after first
4906 union /* shared to save space */
4908 wordnode_T *next; /* next node with same hash key */
4909 wordnode_T *wnode; /* parent node that will write this node */
4911 wordnode_T *wn_child; /* child (next byte in word) */
4912 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4914 int wn_refs; /* Nr. of references to this node. Only
4915 relevant for first node in a list of
4916 siblings, in following siblings it is
4918 char_u wn_byte; /* Byte for this node. NUL for word end */
4920 /* Info for when "wn_byte" is NUL.
4921 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4922 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4923 * "wn_region" the LSW of the wordnr. */
4924 char_u wn_affixID; /* supported/required prefix ID or 0 */
4925 short_u wn_flags; /* WF_ flags */
4926 short wn_region; /* region mask */
4928 #ifdef SPELL_PRINTTREE
4929 int wn_nr; /* sequence nr for printing */
4933 #define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4935 #define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
4938 * Info used while reading the spell files.
4940 typedef struct spellinfo_S
4942 wordnode_T *si_foldroot; /* tree with case-folded words */
4943 long si_foldwcount; /* nr of words in si_foldroot */
4945 wordnode_T *si_keeproot; /* tree with keep-case words */
4946 long si_keepwcount; /* nr of words in si_keeproot */
4948 wordnode_T *si_prefroot; /* tree with postponed prefixes */
4950 long si_sugtree; /* creating the soundfolding trie */
4952 sblock_T *si_blocks; /* memory blocks used */
4953 long si_blocks_cnt; /* memory blocks allocated */
4954 int si_did_emsg; /* TRUE when ran out of memory */
4956 long si_compress_cnt; /* words to add before lowering
4957 compression limit */
4958 wordnode_T *si_first_free; /* List of nodes that have been freed during
4959 compression, linked by "wn_child" field. */
4960 long si_free_count; /* number of nodes in si_first_free */
4961 #ifdef SPELL_PRINTTREE
4962 int si_wordnode_nr; /* sequence nr for nodes */
4964 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
4966 int si_ascii; /* handling only ASCII words */
4967 int si_add; /* addition file */
4968 int si_clear_chartab; /* when TRUE clear char tables */
4969 int si_region; /* region mask */
4970 vimconv_T si_conv; /* for conversion to 'encoding' */
4971 int si_memtot; /* runtime memory used */
4972 int si_verbose; /* verbose messages */
4973 int si_msg_count; /* number of words added since last message */
4974 char_u *si_info; /* info text chars or NULL */
4975 int si_region_count; /* number of regions supported (1 when there
4977 char_u si_region_name[17]; /* region names; used only if
4978 * si_region_count > 1) */
4980 garray_T si_rep; /* list of fromto_T entries from REP lines */
4981 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
4982 garray_T si_sal; /* list of fromto_T entries from SAL lines */
4983 char_u *si_sofofr; /* SOFOFROM text */
4984 char_u *si_sofoto; /* SOFOTO text */
4985 int si_nosugfile; /* NOSUGFILE item found */
4986 int si_nosplitsugs; /* NOSPLITSUGS item found */
4987 int si_followup; /* soundsalike: ? */
4988 int si_collapse; /* soundsalike: ? */
4989 hashtab_T si_commonwords; /* hashtable for common words */
4990 time_t si_sugtime; /* timestamp for .sug file */
4991 int si_rem_accents; /* soundsalike: remove accents */
4992 garray_T si_map; /* MAP info concatenated */
4993 char_u *si_midword; /* MIDWORD chars or NULL */
4994 int si_compmax; /* max nr of words for compounding */
4995 int si_compminlen; /* minimal length for compounding */
4996 int si_compsylmax; /* max nr of syllables for compounding */
4997 int si_compoptions; /* COMP_ flags */
4998 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
5000 char_u *si_compflags; /* flags used for compounding */
5001 char_u si_nobreak; /* NOBREAK */
5002 char_u *si_syllable; /* syllable string */
5003 garray_T si_prefcond; /* table with conditions for postponed
5004 * prefixes, each stored as a string */
5005 int si_newprefID; /* current value for ah_newID */
5006 int si_newcompID; /* current value for compound ID */
5009 static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
5010 static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
5011 static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
5012 static int spell_info_item __ARGS((char_u *s));
5013 static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
5014 static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
5015 static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
5016 static void check_renumber __ARGS((spellinfo_T *spin));
5017 static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
5018 static void aff_check_number __ARGS((int spinval, int affval, char *name));
5019 static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
5020 static int str_equal __ARGS((char_u *s1, char_u *s2));
5021 static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
5022 static int sal_to_bool __ARGS((char_u *s));
5023 static int has_non_ascii __ARGS((char_u *s));
5024 static void spell_free_aff __ARGS((afffile_T *aff));
5025 static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
5026 static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
5027 static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
5028 static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
5029 static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen));
5030 static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5031 static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5032 static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
5033 static void free_blocks __ARGS((sblock_T *bl));
5034 static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
5035 static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix));
5036 static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
5037 static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
5038 static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
5039 static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5040 static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5041 static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
5042 static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
5043 static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
5044 static void clear_node __ARGS((wordnode_T *node));
5045 static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
5046 static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5047 static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5048 static int sug_maketable __ARGS((spellinfo_T *spin));
5049 static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5050 static int offset2bytes __ARGS((int nr, char_u *buf));
5051 static int bytes2offset __ARGS((char_u **pp));
5052 static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
5053 static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
5054 static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
5055 static void init_spellfile __ARGS((void));
5057 /* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5058 * but it must be negative to indicate the prefix tree to tree_add_word().
5059 * Use a negative number with the lower 8 bits zero. */
5060 #define PFX_FLAGS -256
5062 /* flags for "condit" argument of store_aff_word() */
5063 #define CONDIT_COMB 1 /* affix must combine */
5064 #define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5065 #define CONDIT_SUF 4 /* add a suffix for matching flags */
5066 #define CONDIT_AFF 8 /* word already has an affix */
5069 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5071 static long compress_start = 30000; /* memory / SBLOCKSIZE */
5072 static long compress_inc = 100; /* memory / SBLOCKSIZE */
5073 static long compress_added = 500000; /* word count */
5075 #ifdef SPELL_PRINTTREE
5077 * For debugging the tree code: print the current tree in a (more or less)
5078 * readable format, so that we can see what happens when adding a word and/or
5079 * compressing the tree.
5080 * Based on code from Olaf Seibert.
5082 #define PRINTLINESIZE 1000
5083 #define PRINTWIDTH 6
5085 #define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5086 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5088 static char line1[PRINTLINESIZE];
5089 static char line2[PRINTLINESIZE];
5090 static char line3[PRINTLINESIZE];
5093 spell_clear_flags(wordnode_T *node)
5097 for (np = node; np != NULL; np = np->wn_sibling)
5099 np->wn_u1.index = FALSE;
5100 spell_clear_flags(np->wn_child);
5105 spell_print_node(wordnode_T *node, int depth)
5107 if (node->wn_u1.index)
5109 /* Done this node before, print the reference. */
5110 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5111 PRINTSOME(line2, depth, " ", 0, 0);
5112 PRINTSOME(line3, depth, " ", 0, 0);
5119 node->wn_u1.index = TRUE;
5121 if (node->wn_byte != NUL)
5123 if (node->wn_child != NULL)
5124 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5126 /* Cannot happen? */
5127 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5130 PRINTSOME(line1, depth, " $ ", 0, 0);
5132 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5134 if (node->wn_sibling != NULL)
5135 PRINTSOME(line3, depth, " | ", 0, 0);
5137 PRINTSOME(line3, depth, " ", 0, 0);
5139 if (node->wn_byte == NUL)
5146 /* do the children */
5147 if (node->wn_byte != NUL && node->wn_child != NULL)
5148 spell_print_node(node->wn_child, depth + 1);
5150 /* do the siblings */
5151 if (node->wn_sibling != NULL)
5153 /* get rid of all parent details except | */
5154 STRCPY(line1, line3);
5155 STRCPY(line2, line3);
5156 spell_print_node(node->wn_sibling, depth);
5162 spell_print_tree(wordnode_T *root)
5166 /* Clear the "wn_u1.index" fields, used to remember what has been
5168 spell_clear_flags(root);
5170 /* Recursively print the tree. */
5171 spell_print_node(root, 0);
5174 #endif /* SPELL_PRINTTREE */
5177 * Read the affix file "fname".
5178 * Returns an afffile_T, NULL for complete failure.
5181 spell_read_aff(spin, fname)
5187 char_u rline[MAXLINELEN];
5190 #define MAXITEMCNT 30
5191 char_u *(items[MAXITEMCNT]);
5195 affheader_T *cur_aff = NULL;
5196 int did_postpone_prefix = FALSE;
5206 int found_map = FALSE;
5209 int compminlen = 0; /* COMPOUNDMIN value */
5210 int compsylmax = 0; /* COMPOUNDSYLMAX value */
5211 int compoptions = 0; /* COMP_ flags */
5212 int compmax = 0; /* COMPOUNDWORDMAX value */
5213 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
5215 char_u *midword = NULL; /* MIDWORD value */
5216 char_u *syllable = NULL; /* SYLLABLE value */
5217 char_u *sofofrom = NULL; /* SOFOFROM value */
5218 char_u *sofoto = NULL; /* SOFOTO value */
5223 fd = mch_fopen((char *)fname, "r");
5226 EMSG2(_(e_notopen), fname);
5230 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5231 spell_message(spin, IObuff);
5233 /* Only do REP lines when not done in another .aff file already. */
5234 do_rep = spin->si_rep.ga_len == 0;
5236 /* Only do REPSAL lines when not done in another .aff file already. */
5237 do_repsal = spin->si_repsal.ga_len == 0;
5239 /* Only do SAL lines when not done in another .aff file already. */
5240 do_sal = spin->si_sal.ga_len == 0;
5242 /* Only do MAP lines when not done in another .aff file already. */
5243 do_mapline = spin->si_map.ga_len == 0;
5246 * Allocate and init the afffile_T structure.
5248 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
5254 hash_init(&aff->af_pref);
5255 hash_init(&aff->af_suff);
5256 hash_init(&aff->af_comp);
5259 * Read all the lines in the file one by one.
5261 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5266 /* Skip comment lines. */
5270 /* Convert from "SET" to 'encoding' when needed. */
5273 if (spin->si_conv.vc_type != CONV_NONE)
5275 pc = string_convert(&spin->si_conv, rline, NULL);
5278 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5279 fname, lnum, rline);
5291 /* Split the line up in white separated items. Put a NUL after each
5296 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5300 if (itemcnt == MAXITEMCNT) /* too many items */
5302 items[itemcnt++] = p;
5303 /* A few items have arbitrary text argument, don't split them. */
5304 if (itemcnt == 2 && spell_info_item(items[0]))
5305 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5308 while (*p > ' ') /* skip until white space or CR/NL */
5315 /* Handle non-empty lines. */
5318 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
5321 /* Setup for conversion from "ENC" to 'encoding'. */
5322 aff->af_enc = enc_canonize(items[1]);
5323 if (aff->af_enc != NULL && !spin->si_ascii
5324 && convert_setup(&spin->si_conv, aff->af_enc,
5326 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5327 fname, aff->af_enc, p_enc);
5328 spin->si_conv.vc_fail = TRUE;
5330 smsg((char_u *)_("Conversion in %s not supported"), fname);
5333 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
5334 && aff->af_flagtype == AFT_CHAR)
5336 if (STRCMP(items[1], "long") == 0)
5337 aff->af_flagtype = AFT_LONG;
5338 else if (STRCMP(items[1], "num") == 0)
5339 aff->af_flagtype = AFT_NUM;
5340 else if (STRCMP(items[1], "caplong") == 0)
5341 aff->af_flagtype = AFT_CAPLONG;
5343 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5344 fname, lnum, items[1]);
5345 if (aff->af_rare != 0
5346 || aff->af_keepcase != 0
5348 || aff->af_needaffix != 0
5349 || aff->af_circumfix != 0
5350 || aff->af_needcomp != 0
5351 || aff->af_comproot != 0
5352 || aff->af_nosuggest != 0
5353 || compflags != NULL
5354 || aff->af_suff.ht_used > 0
5355 || aff->af_pref.ht_used > 0)
5356 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5357 fname, lnum, items[1]);
5359 else if (spell_info_item(items[0]))
5361 p = (char_u *)getroom(spin,
5362 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5364 + STRLEN(items[1]) + 3, FALSE);
5367 if (spin->si_info != NULL)
5369 STRCPY(p, spin->si_info);
5372 STRCAT(p, items[0]);
5374 STRCAT(p, items[1]);
5378 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
5381 midword = getroom_save(spin, items[1]);
5383 else if (is_aff_rule(items, itemcnt, "TRY", 2))
5385 /* ignored, we look in the tree for what chars may appear */
5387 /* TODO: remove "RAR" later */
5388 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5389 || is_aff_rule(items, itemcnt, "RARE", 2))
5390 && aff->af_rare == 0)
5392 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
5395 /* TODO: remove "KEP" later */
5396 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5397 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
5398 && aff->af_keepcase == 0)
5400 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
5403 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5404 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5405 && aff->af_bad == 0)
5407 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5410 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
5411 && aff->af_needaffix == 0)
5413 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5416 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
5417 && aff->af_circumfix == 0)
5419 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5422 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
5423 && aff->af_nosuggest == 0)
5425 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5428 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5429 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
5430 && aff->af_needcomp == 0)
5432 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5435 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
5436 && aff->af_comproot == 0)
5438 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5441 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5442 && aff->af_compforbid == 0)
5444 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5446 if (aff->af_pref.ht_used > 0)
5447 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5450 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5451 && aff->af_comppermit == 0)
5453 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5455 if (aff->af_pref.ht_used > 0)
5456 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5459 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
5460 && compflags == NULL)
5462 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
5463 * "Na" into "Na+", "1234" into "1234+". */
5464 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
5467 STRCPY(p, items[1]);
5472 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5474 /* We don't use the count, but do check that it's a number and
5475 * not COMPOUNDRULE mistyped. */
5476 if (atoi((char *)items[1]) == 0)
5477 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5478 fname, lnum, items[1]);
5480 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
5482 /* Don't use the first rule if it is a number. */
5483 if (compflags != NULL || *skipdigits(items[1]) != NUL)
5485 /* Concatenate this string to previously defined ones,
5486 * using a slash to separate them. */
5487 l = (int)STRLEN(items[1]) + 1;
5488 if (compflags != NULL)
5489 l += (int)STRLEN(compflags) + 1;
5490 p = getroom(spin, l, FALSE);
5493 if (compflags != NULL)
5495 STRCPY(p, compflags);
5498 STRCAT(p, items[1]);
5503 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
5506 compmax = atoi((char *)items[1]);
5508 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
5509 fname, lnum, items[1]);
5511 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
5514 compminlen = atoi((char *)items[1]);
5515 if (compminlen == 0)
5516 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5517 fname, lnum, items[1]);
5519 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
5522 compsylmax = atoi((char *)items[1]);
5523 if (compsylmax == 0)
5524 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5525 fname, lnum, items[1]);
5527 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
5529 compoptions |= COMP_CHECKDUP;
5531 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
5533 compoptions |= COMP_CHECKREP;
5535 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
5537 compoptions |= COMP_CHECKCASE;
5539 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
5541 compoptions |= COMP_CHECKTRIPLE;
5543 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
5545 if (atoi((char *)items[1]) == 0)
5546 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5547 fname, lnum, items[1]);
5549 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
5551 garray_T *gap = &spin->si_comppat;
5554 /* Only add the couple if it isn't already there. */
5555 for (i = 0; i < gap->ga_len - 1; i += 2)
5556 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5557 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5560 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5562 ((char_u **)(gap->ga_data))[gap->ga_len++]
5563 = getroom_save(spin, items[1]);
5564 ((char_u **)(gap->ga_data))[gap->ga_len++]
5565 = getroom_save(spin, items[2]);
5568 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
5569 && syllable == NULL)
5571 syllable = getroom_save(spin, items[1]);
5573 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
5575 spin->si_nobreak = TRUE;
5577 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
5579 spin->si_nosplitsugs = TRUE;
5581 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
5583 spin->si_nosugfile = TRUE;
5585 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
5587 aff->af_pfxpostpone = TRUE;
5589 else if ((STRCMP(items[0], "PFX") == 0
5590 || STRCMP(items[0], "SFX") == 0)
5595 char_u key[AH_KEY_LEN];
5597 if (*items[0] == 'P')
5602 /* Myspell allows the same affix name to be used multiple
5603 * times. The affix files that do this have an undocumented
5604 * "S" flag on all but the last block, thus we check for that
5605 * and store it in ah_follows. */
5606 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5607 hi = hash_find(tp, key);
5608 if (!HASHITEM_EMPTY(hi))
5610 cur_aff = HI2AH(hi);
5611 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5612 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5613 fname, lnum, items[1]);
5614 if (!cur_aff->ah_follows)
5615 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5616 fname, lnum, items[1]);
5620 /* New affix letter. */
5621 cur_aff = (affheader_T *)getroom(spin,
5622 sizeof(affheader_T), TRUE);
5623 if (cur_aff == NULL)
5625 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5627 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5629 if (cur_aff->ah_flag == aff->af_bad
5630 || cur_aff->ah_flag == aff->af_rare
5631 || cur_aff->ah_flag == aff->af_keepcase
5632 || cur_aff->ah_flag == aff->af_needaffix
5633 || cur_aff->ah_flag == aff->af_circumfix
5634 || cur_aff->ah_flag == aff->af_nosuggest
5635 || cur_aff->ah_flag == aff->af_needcomp
5636 || cur_aff->ah_flag == aff->af_comproot)
5637 smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
5638 fname, lnum, items[1]);
5639 STRCPY(cur_aff->ah_key, items[1]);
5640 hash_add(tp, cur_aff->ah_key);
5642 cur_aff->ah_combine = (*items[2] == 'Y');
5645 /* Check for the "S" flag, which apparently means that another
5646 * block with the same affix name is following. */
5647 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5650 cur_aff->ah_follows = TRUE;
5653 cur_aff->ah_follows = FALSE;
5655 /* Myspell allows extra text after the item, but that might
5656 * mean mistakes go unnoticed. Require a comment-starter. */
5657 if (itemcnt > lasti && *items[lasti] != '#')
5658 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
5660 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
5661 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5662 fname, lnum, items[2]);
5664 if (*items[0] == 'P' && aff->af_pfxpostpone)
5666 if (cur_aff->ah_newID == 0)
5668 /* Use a new number in the .spl file later, to be able
5669 * to handle multiple .aff files. */
5670 check_renumber(spin);
5671 cur_aff->ah_newID = ++spin->si_newprefID;
5673 /* We only really use ah_newID if the prefix is
5674 * postponed. We know that only after handling all
5676 did_postpone_prefix = FALSE;
5679 /* Did use the ID in a previous block. */
5680 did_postpone_prefix = TRUE;
5683 aff_todo = atoi((char *)items[3]);
5685 else if ((STRCMP(items[0], "PFX") == 0
5686 || STRCMP(items[0], "SFX") == 0)
5688 && STRCMP(cur_aff->ah_key, items[1]) == 0
5691 affentry_T *aff_entry;
5695 /* Myspell allows extra text after the item, but that might
5696 * mean mistakes go unnoticed. Require a comment-starter.
5697 * Hunspell uses a "-" item. */
5698 if (itemcnt > lasti && *items[lasti] != '#'
5699 && (STRCMP(items[lasti], "-") != 0
5700 || itemcnt != lasti + 1))
5701 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
5703 /* New item for an affix letter. */
5705 aff_entry = (affentry_T *)getroom(spin,
5706 sizeof(affentry_T), TRUE);
5707 if (aff_entry == NULL)
5710 if (STRCMP(items[2], "0") != 0)
5711 aff_entry->ae_chop = getroom_save(spin, items[2]);
5712 if (STRCMP(items[3], "0") != 0)
5714 aff_entry->ae_add = getroom_save(spin, items[3]);
5716 /* Recognize flags on the affix: abcd/XYZ */
5717 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5718 if (aff_entry->ae_flags != NULL)
5720 *aff_entry->ae_flags++ = NUL;
5721 aff_process_flags(aff, aff_entry);
5725 /* Don't use an affix entry with non-ASCII characters when
5726 * "spin->si_ascii" is TRUE. */
5727 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
5728 || has_non_ascii(aff_entry->ae_add)))
5730 aff_entry->ae_next = cur_aff->ah_first;
5731 cur_aff->ah_first = aff_entry;
5733 if (STRCMP(items[4], ".") != 0)
5735 char_u buf[MAXLINELEN];
5737 aff_entry->ae_cond = getroom_save(spin, items[4]);
5738 if (*items[0] == 'P')
5739 sprintf((char *)buf, "^%s", items[4]);
5741 sprintf((char *)buf, "%s$", items[4]);
5742 aff_entry->ae_prog = vim_regcomp(buf,
5743 RE_MAGIC + RE_STRING + RE_STRICT);
5744 if (aff_entry->ae_prog == NULL)
5745 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5746 fname, lnum, items[4]);
5749 /* For postponed prefixes we need an entry in si_prefcond
5750 * for the condition. Use an existing one if possible.
5751 * Can't be done for an affix with flags, ignoring
5752 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
5753 if (*items[0] == 'P' && aff->af_pfxpostpone
5754 && aff_entry->ae_flags == NULL)
5756 /* When the chop string is one lower-case letter and
5757 * the add string ends in the upper-case letter we set
5758 * the "upper" flag, clear "ae_chop" and remove the
5759 * letters from "ae_add". The condition must either
5760 * be empty or start with the same letter. */
5761 if (aff_entry->ae_chop != NULL
5762 && aff_entry->ae_add != NULL
5764 && aff_entry->ae_chop[(*mb_ptr2len)(
5765 aff_entry->ae_chop)] == NUL
5767 && aff_entry->ae_chop[1] == NUL
5773 c = PTR2CHAR(aff_entry->ae_chop);
5774 c_up = SPELL_TOUPPER(c);
5776 && (aff_entry->ae_cond == NULL
5777 || PTR2CHAR(aff_entry->ae_cond) == c))
5779 p = aff_entry->ae_add
5780 + STRLEN(aff_entry->ae_add);
5781 mb_ptr_back(aff_entry->ae_add, p);
5782 if (PTR2CHAR(p) == c_up)
5785 aff_entry->ae_chop = NULL;
5788 /* The condition is matched with the
5789 * actual word, thus must check for the
5790 * upper-case letter. */
5791 if (aff_entry->ae_cond != NULL)
5793 char_u buf[MAXLINELEN];
5797 onecap_copy(items[4], buf, TRUE);
5798 aff_entry->ae_cond = getroom_save(
5803 *aff_entry->ae_cond = c_up;
5804 if (aff_entry->ae_cond != NULL)
5806 sprintf((char *)buf, "^%s",
5807 aff_entry->ae_cond);
5808 vim_free(aff_entry->ae_prog);
5809 aff_entry->ae_prog = vim_regcomp(
5810 buf, RE_MAGIC + RE_STRING);
5817 if (aff_entry->ae_chop == NULL
5818 && aff_entry->ae_flags == NULL)
5824 /* Find a previously used condition. */
5825 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5828 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5829 if (str_equal(p, aff_entry->ae_cond))
5832 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5834 /* Not found, add a new condition. */
5835 idx = spin->si_prefcond.ga_len++;
5836 pp = ((char_u **)spin->si_prefcond.ga_data)
5838 if (aff_entry->ae_cond == NULL)
5841 *pp = getroom_save(spin,
5842 aff_entry->ae_cond);
5845 /* Add the prefix to the prefix tree. */
5846 if (aff_entry->ae_add == NULL)
5849 p = aff_entry->ae_add;
5851 /* PFX_FLAGS is a negative number, so that
5852 * tree_add_word() knows this is the prefix tree. */
5854 if (!cur_aff->ah_combine)
5858 if (aff_entry->ae_comppermit)
5859 n |= WFP_COMPPERMIT;
5860 if (aff_entry->ae_compforbid)
5861 n |= WFP_COMPFORBID;
5862 tree_add_word(spin, p, spin->si_prefroot, n,
5863 idx, cur_aff->ah_newID);
5864 did_postpone_prefix = TRUE;
5867 /* Didn't actually use ah_newID, backup si_newprefID. */
5868 if (aff_todo == 0 && !did_postpone_prefix)
5870 --spin->si_newprefID;
5871 cur_aff->ah_newID = 0;
5876 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
5878 fol = vim_strsave(items[1]);
5880 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
5882 low = vim_strsave(items[1]);
5884 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
5886 upp = vim_strsave(items[1]);
5888 else if (is_aff_rule(items, itemcnt, "REP", 2)
5889 || is_aff_rule(items, itemcnt, "REPSAL", 2))
5891 /* Ignore REP/REPSAL count */;
5892 if (!isdigit(*items[1]))
5893 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
5896 else if ((STRCMP(items[0], "REP") == 0
5897 || STRCMP(items[0], "REPSAL") == 0)
5900 /* REP/REPSAL item */
5901 /* Myspell ignores extra arguments, we require it starts with
5902 * # to detect mistakes. */
5903 if (itemcnt > 3 && items[3][0] != '#')
5904 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
5905 if (items[0][3] == 'S' ? do_repsal : do_rep)
5907 /* Replace underscore with space (can't include a space
5909 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5912 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5915 add_fromto(spin, items[0][3] == 'S'
5917 : &spin->si_rep, items[1], items[2]);
5920 else if (is_aff_rule(items, itemcnt, "MAP", 2))
5922 /* MAP item or count */
5925 /* First line contains the count. */
5927 if (!isdigit(*items[1]))
5928 smsg((char_u *)_("Expected MAP count in %s line %d"),
5931 else if (do_mapline)
5935 /* Check that every character appears only once. */
5936 for (p = items[1]; *p != NUL; )
5939 c = mb_ptr2char_adv(&p);
5943 if ((spin->si_map.ga_len > 0
5944 && vim_strchr(spin->si_map.ga_data, c)
5946 || vim_strchr(p, c) != NULL)
5947 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5951 /* We simply concatenate all the MAP strings, separated by
5953 ga_concat(&spin->si_map, items[1]);
5954 ga_append(&spin->si_map, '/');
5957 /* Accept "SAL from to" and "SAL from to #comment". */
5958 else if (is_aff_rule(items, itemcnt, "SAL", 3))
5962 /* SAL item (sounds-a-like)
5963 * Either one of the known keys or a from-to pair. */
5964 if (STRCMP(items[1], "followup") == 0)
5965 spin->si_followup = sal_to_bool(items[2]);
5966 else if (STRCMP(items[1], "collapse_result") == 0)
5967 spin->si_collapse = sal_to_bool(items[2]);
5968 else if (STRCMP(items[1], "remove_accents") == 0)
5969 spin->si_rem_accents = sal_to_bool(items[2]);
5971 /* when "to" is "_" it means empty */
5972 add_fromto(spin, &spin->si_sal, items[1],
5973 STRCMP(items[2], "_") == 0 ? (char_u *)""
5977 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
5978 && sofofrom == NULL)
5980 sofofrom = getroom_save(spin, items[1]);
5982 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
5985 sofoto = getroom_save(spin, items[1]);
5987 else if (STRCMP(items[0], "COMMON") == 0)
5991 for (i = 1; i < itemcnt; ++i)
5993 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5996 p = vim_strsave(items[i]);
5999 hash_add(&spin->si_commonwords, p);
6004 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
6005 fname, lnum, items[0]);
6009 if (fol != NULL || low != NULL || upp != NULL)
6011 if (spin->si_clear_chartab)
6013 /* Clear the char type tables, don't want to use any of the
6014 * currently used spell properties. */
6015 init_spell_chartab();
6016 spin->si_clear_chartab = FALSE;
6020 * Don't write a word table for an ASCII file, so that we don't check
6021 * for conflicts with a word table that matches 'encoding'.
6022 * Don't write one for utf-8 either, we use utf_*() and
6023 * mb_get_class(), the list of chars in the file will be incomplete.
6031 if (fol == NULL || low == NULL || upp == NULL)
6032 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6034 (void)set_spell_chartab(fol, low, upp);
6042 /* Use compound specifications of the .aff file for the spell info. */
6045 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
6046 spin->si_compmax = compmax;
6049 if (compminlen != 0)
6051 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6052 spin->si_compminlen = compminlen;
6055 if (compsylmax != 0)
6057 if (syllable == NULL)
6058 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6059 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6060 spin->si_compsylmax = compsylmax;
6063 if (compoptions != 0)
6065 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6066 spin->si_compoptions |= compoptions;
6069 if (compflags != NULL)
6070 process_compflags(spin, aff, compflags);
6072 /* Check that we didn't use too many renumbered flags. */
6073 if (spin->si_newcompID < spin->si_newprefID)
6075 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
6076 MSG(_("Too many postponed prefixes"));
6077 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
6078 MSG(_("Too many compound flags"));
6080 MSG(_("Too many postponed prefixes and/or compound flags"));
6083 if (syllable != NULL)
6085 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6086 spin->si_syllable = syllable;
6089 if (sofofrom != NULL || sofoto != NULL)
6091 if (sofofrom == NULL || sofoto == NULL)
6092 smsg((char_u *)_("Missing SOFO%s line in %s"),
6093 sofofrom == NULL ? "FROM" : "TO", fname);
6094 else if (spin->si_sal.ga_len > 0)
6095 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
6098 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6099 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6100 spin->si_sofofr = sofofrom;
6101 spin->si_sofoto = sofoto;
6105 if (midword != NULL)
6107 aff_check_string(spin->si_midword, midword, "MIDWORD");
6108 spin->si_midword = midword;
6117 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6118 * a comment is following after item "mincount".
6121 is_aff_rule(items, itemcnt, rulename, mincount)
6127 return (STRCMP(items[0], rulename) == 0
6128 && (itemcnt == mincount
6129 || (itemcnt > mincount && items[mincount][0] == '#')));
6133 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6134 * ae_flags to ae_comppermit and ae_compforbid.
6137 aff_process_flags(affile, entry)
6145 if (entry->ae_flags != NULL
6146 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6148 for (p = entry->ae_flags; *p != NUL; )
6151 flag = get_affitem(affile->af_flagtype, &p);
6152 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6156 if (flag == affile->af_comppermit)
6157 entry->ae_comppermit = TRUE;
6159 entry->ae_compforbid = TRUE;
6161 if (affile->af_flagtype == AFT_NUM && *p == ',')
6164 if (*entry->ae_flags == NUL)
6165 entry->ae_flags = NULL; /* nothing left */
6170 * Return TRUE if "s" is the name of an info item in the affix file.
6176 return STRCMP(s, "NAME") == 0
6177 || STRCMP(s, "HOME") == 0
6178 || STRCMP(s, "VERSION") == 0
6179 || STRCMP(s, "AUTHOR") == 0
6180 || STRCMP(s, "EMAIL") == 0
6181 || STRCMP(s, "COPYRIGHT") == 0;
6185 * Turn an affix flag name into a number, according to the FLAG type.
6186 * returns zero for failure.
6189 affitem2flag(flagtype, item, fname, lnum)
6198 res = get_affitem(flagtype, &p);
6201 if (flagtype == AFT_NUM)
6202 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6205 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6210 smsg((char_u *)_(e_affname), fname, lnum, item);
6218 * Get one affix name from "*pp" and advance the pointer.
6219 * Returns zero for an error, still advances the pointer then.
6222 get_affitem(flagtype, pp)
6228 if (flagtype == AFT_NUM)
6230 if (!VIM_ISDIGIT(**pp))
6232 ++*pp; /* always advance, avoid getting stuck */
6235 res = getdigits(pp);
6240 res = mb_ptr2char_adv(pp);
6244 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
6245 && res >= 'A' && res <= 'Z'))
6250 res = mb_ptr2char_adv(pp) + (res << 16);
6252 res = *(*pp)++ + (res << 16);
6260 * Process the "compflags" string used in an affix file and append it to
6261 * spin->si_compflags.
6262 * The processing involves changing the affix names to ID numbers, so that
6263 * they fit in one byte.
6266 process_compflags(spin, aff, compflags)
6278 char_u key[AH_KEY_LEN];
6281 /* Make room for the old and the new compflags, concatenated with a / in
6282 * between. Processing it makes it shorter, but we don't know by how
6283 * much, thus allocate the maximum. */
6284 len = (int)STRLEN(compflags) + 1;
6285 if (spin->si_compflags != NULL)
6286 len += (int)STRLEN(spin->si_compflags) + 1;
6287 p = getroom(spin, len, FALSE);
6290 if (spin->si_compflags != NULL)
6292 STRCPY(p, spin->si_compflags);
6295 spin->si_compflags = p;
6298 for (p = compflags; *p != NUL; )
6300 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
6301 /* Copy non-flag characters directly. */
6305 /* First get the flag number, also checks validity. */
6307 flag = get_affitem(aff->af_flagtype, &p);
6310 /* Find the flag in the hashtable. If it was used before, use
6311 * the existing ID. Otherwise add a new entry. */
6312 vim_strncpy(key, prevp, p - prevp);
6313 hi = hash_find(&aff->af_comp, key);
6314 if (!HASHITEM_EMPTY(hi))
6315 id = HI2CI(hi)->ci_newID;
6318 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6321 STRCPY(ci->ci_key, key);
6323 /* Avoid using a flag ID that has a special meaning in a
6324 * regexp (also inside []). */
6327 check_renumber(spin);
6328 id = spin->si_newcompID--;
6329 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
6331 hash_add(&aff->af_comp, ci->ci_key);
6335 if (aff->af_flagtype == AFT_NUM && *p == ',')
6344 * Check that the new IDs for postponed affixes and compounding don't overrun
6345 * each other. We have almost 255 available, but start at 0-127 to avoid
6346 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6347 * When that is used up an error message is given.
6350 check_renumber(spin)
6353 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6355 spin->si_newprefID = 127;
6356 spin->si_newcompID = 255;
6361 * Return TRUE if flag "flag" appears in affix list "afflist".
6364 flag_in_afflist(flagtype, afflist, flag)
6375 return vim_strchr(afflist, flag) != NULL;
6379 for (p = afflist; *p != NUL; )
6382 n = mb_ptr2char_adv(&p);
6386 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
6389 n = mb_ptr2char_adv(&p) + (n << 16);
6391 n = *p++ + (n << 16);
6399 for (p = afflist; *p != NUL; )
6404 if (*p != NUL) /* skip over comma */
6413 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6416 aff_check_number(spinval, affval, name)
6421 if (spinval != 0 && spinval != affval)
6422 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6426 * Give a warning when "spinval" and "affval" strings are set and not the same.
6429 aff_check_string(spinval, affval, name)
6434 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6435 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6439 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6447 if (s1 == NULL || s2 == NULL)
6449 return STRCMP(s1, s2) == 0;
6453 * Add a from-to item to "gap". Used for REP and SAL items.
6454 * They are stored case-folded.
6457 add_fromto(spin, gap, from, to)
6464 char_u word[MAXWLEN];
6466 if (ga_grow(gap, 1) == OK)
6468 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
6469 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
6470 ftp->ft_from = getroom_save(spin, word);
6471 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
6472 ftp->ft_to = getroom_save(spin, word);
6478 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6484 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6488 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6489 * When "s" is NULL FALSE is returned.
6498 for (p = s; *p != NUL; ++p)
6505 * Free the structure filled by spell_read_aff().
6517 vim_free(aff->af_enc);
6519 /* All this trouble to free the "ae_prog" items... */
6520 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6522 todo = (int)ht->ht_used;
6523 for (hi = ht->ht_array; todo > 0; ++hi)
6525 if (!HASHITEM_EMPTY(hi))
6529 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6530 vim_free(ae->ae_prog);
6533 if (ht == &aff->af_suff)
6537 hash_clear(&aff->af_pref);
6538 hash_clear(&aff->af_suff);
6539 hash_clear(&aff->af_comp);
6543 * Read dictionary file "fname".
6544 * Returns OK or FAIL;
6547 spell_read_dic(spin, fname, affile)
6553 char_u line[MAXLINELEN];
6556 char_u store_afflist[MAXWLEN];
6569 char_u message[MAXLINELEN + MAXWLEN];
6576 fd = mch_fopen((char *)fname, "r");
6579 EMSG2(_(e_notopen), fname);
6583 /* The hashtable is only used to detect duplicated words. */
6586 vim_snprintf((char *)IObuff, IOSIZE,
6587 _("Reading dictionary file %s ..."), fname);
6588 spell_message(spin, IObuff);
6590 /* start with a message for the first line */
6591 spin->si_msg_count = 999999;
6593 /* Read and ignore the first line: word count. */
6594 (void)vim_fgets(line, MAXLINELEN, fd);
6595 if (!vim_isdigit(*skipwhite(line)))
6596 EMSG2(_("E760: No word count in %s"), fname);
6599 * Read all the lines in the file one by one.
6600 * The words are converted to 'encoding' here, before being added to
6603 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
6607 if (line[0] == '#' || line[0] == '/')
6608 continue; /* comment line */
6610 /* Remove CR, LF and white space from the end. White space halfway
6611 * the word is kept to allow e.g., "et al.". */
6612 l = (int)STRLEN(line);
6613 while (l > 0 && line[l - 1] <= ' ')
6616 continue; /* empty line */
6620 /* Convert from "SET" to 'encoding' when needed. */
6621 if (spin->si_conv.vc_type != CONV_NONE)
6623 pc = string_convert(&spin->si_conv, line, NULL);
6626 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6639 /* Truncate the word at the "/", set "afflist" to what follows.
6640 * Replace "\/" by "/" and "\\" by "\". */
6642 for (p = w; *p != NUL; mb_ptr_adv(p))
6644 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6654 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6655 if (spin->si_ascii && has_non_ascii(w))
6662 /* This takes time, print a message every 10000 words. */
6663 if (spin->si_verbose && spin->si_msg_count > 10000)
6665 spin->si_msg_count = 0;
6666 vim_snprintf((char *)message, sizeof(message),
6667 _("line %6d, word %6d - %s"),
6668 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6670 msg_puts_long_attr(message, 0);
6677 /* Store the word in the hashtable to be able to find duplicates. */
6678 dw = (char_u *)getroom_save(spin, w);
6686 hash = hash_hash(dw);
6687 hi = hash_lookup(&ht, dw, hash);
6688 if (!HASHITEM_EMPTY(hi))
6691 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
6693 else if (duplicate == 0)
6694 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6699 hash_add_item(&ht, hi, dw, hash);
6702 store_afflist[0] = NUL;
6705 if (afflist != NULL)
6707 /* Extract flags from the affix list. */
6708 flags |= get_affix_flags(affile, afflist);
6710 if (affile->af_needaffix != 0 && flag_in_afflist(
6711 affile->af_flagtype, afflist, affile->af_needaffix))
6714 if (affile->af_pfxpostpone)
6715 /* Need to store the list of prefix IDs with the word. */
6716 pfxlen = get_pfxlist(affile, afflist, store_afflist);
6718 if (spin->si_compflags != NULL)
6719 /* Need to store the list of compound flags with the word.
6720 * Concatenate them to the list of prefix IDs. */
6721 get_compflags(affile, afflist, store_afflist + pfxlen);
6724 /* Add the word to the word tree(s). */
6725 if (store_word(spin, dw, flags, spin->si_region,
6726 store_afflist, need_affix) == FAIL)
6729 if (afflist != NULL)
6731 /* Find all matching suffixes and add the resulting words.
6732 * Additionally do matching prefixes that combine. */
6733 if (store_aff_word(spin, dw, afflist, affile,
6734 &affile->af_suff, &affile->af_pref,
6735 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
6738 /* Find all matching prefixes and add the resulting words. */
6739 if (store_aff_word(spin, dw, afflist, affile,
6740 &affile->af_pref, NULL,
6741 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
6749 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
6750 if (spin->si_ascii && non_ascii > 0)
6751 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6760 * Check for affix flags in "afflist" that are turned into word flags.
6764 get_affix_flags(affile, afflist)
6770 if (affile->af_keepcase != 0 && flag_in_afflist(
6771 affile->af_flagtype, afflist, affile->af_keepcase))
6772 flags |= WF_KEEPCAP | WF_FIXCAP;
6773 if (affile->af_rare != 0 && flag_in_afflist(
6774 affile->af_flagtype, afflist, affile->af_rare))
6776 if (affile->af_bad != 0 && flag_in_afflist(
6777 affile->af_flagtype, afflist, affile->af_bad))
6779 if (affile->af_needcomp != 0 && flag_in_afflist(
6780 affile->af_flagtype, afflist, affile->af_needcomp))
6781 flags |= WF_NEEDCOMP;
6782 if (affile->af_comproot != 0 && flag_in_afflist(
6783 affile->af_flagtype, afflist, affile->af_comproot))
6784 flags |= WF_COMPROOT;
6785 if (affile->af_nosuggest != 0 && flag_in_afflist(
6786 affile->af_flagtype, afflist, affile->af_nosuggest))
6787 flags |= WF_NOSUGGEST;
6792 * Get the list of prefix IDs from the affix list "afflist".
6793 * Used for PFXPOSTPONE.
6794 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6795 * and return the number of affixes.
6798 get_pfxlist(affile, afflist, store_afflist)
6801 char_u *store_afflist;
6807 char_u key[AH_KEY_LEN];
6810 for (p = afflist; *p != NUL; )
6813 if (get_affitem(affile->af_flagtype, &p) != 0)
6815 /* A flag is a postponed prefix flag if it appears in "af_pref"
6816 * and it's ID is not zero. */
6817 vim_strncpy(key, prevp, p - prevp);
6818 hi = hash_find(&affile->af_pref, key);
6819 if (!HASHITEM_EMPTY(hi))
6821 id = HI2AH(hi)->ah_newID;
6823 store_afflist[cnt++] = id;
6826 if (affile->af_flagtype == AFT_NUM && *p == ',')
6830 store_afflist[cnt] = NUL;
6835 * Get the list of compound IDs from the affix list "afflist" that are used
6836 * for compound words.
6837 * Puts the flags in "store_afflist[]".
6840 get_compflags(affile, afflist, store_afflist)
6843 char_u *store_afflist;
6848 char_u key[AH_KEY_LEN];
6851 for (p = afflist; *p != NUL; )
6854 if (get_affitem(affile->af_flagtype, &p) != 0)
6856 /* A flag is a compound flag if it appears in "af_comp". */
6857 vim_strncpy(key, prevp, p - prevp);
6858 hi = hash_find(&affile->af_comp, key);
6859 if (!HASHITEM_EMPTY(hi))
6860 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6862 if (affile->af_flagtype == AFT_NUM && *p == ',')
6866 store_afflist[cnt] = NUL;
6870 * Apply affixes to a word and store the resulting words.
6871 * "ht" is the hashtable with affentry_T that need to be applied, either
6872 * prefixes or suffixes.
6873 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6874 * the resulting words for combining affixes.
6876 * Returns FAIL when out of memory.
6879 store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
6881 spellinfo_T *spin; /* spell info */
6882 char_u *word; /* basic word start */
6883 char_u *afflist; /* list of names of supported affixes */
6887 int condit; /* CONDIT_SUF et al. */
6888 int flags; /* flags for the word */
6889 char_u *pfxlist; /* list of prefix IDs */
6890 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6891 * is compound flags */
6897 regmatch_T regmatch;
6898 char_u newword[MAXWLEN];
6903 char_u *use_pfxlist;
6906 char_u store_afflist[MAXWLEN];
6907 char_u pfx_pfxlist[MAXWLEN];
6908 size_t wordlen = STRLEN(word);
6911 todo = (int)ht->ht_used;
6912 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
6914 if (!HASHITEM_EMPTY(hi))
6919 /* Check that the affix combines, if required, and that the word
6920 * supports this affix. */
6921 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6922 && flag_in_afflist(affile->af_flagtype, afflist,
6925 /* Loop over all affix entries with this name. */
6926 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6928 /* Check the condition. It's not logical to match case
6929 * here, but it is required for compatibility with
6931 * Another requirement from Myspell is that the chop
6932 * string is shorter than the word itself.
6933 * For prefixes, when "PFXPOSTPONE" was used, only do
6934 * prefixes with a chop string and/or flags.
6935 * When a previously added affix had CIRCUMFIX this one
6936 * must have it too, if it had not then this one must not
6937 * have one either. */
6938 regmatch.regprog = ae->ae_prog;
6939 regmatch.rm_ic = FALSE;
6940 if ((xht != NULL || !affile->af_pfxpostpone
6941 || ae->ae_chop != NULL
6942 || ae->ae_flags != NULL)
6943 && (ae->ae_chop == NULL
6944 || STRLEN(ae->ae_chop) < wordlen)
6945 && (ae->ae_prog == NULL
6946 || vim_regexec(®match, word, (colnr_T)0))
6947 && (((condit & CONDIT_CFIX) == 0)
6948 == ((condit & CONDIT_AFF) == 0
6949 || ae->ae_flags == NULL
6950 || !flag_in_afflist(affile->af_flagtype,
6951 ae->ae_flags, affile->af_circumfix))))
6953 /* Match. Remove the chop and add the affix. */
6956 /* prefix: chop/add at the start of the word */
6957 if (ae->ae_add == NULL)
6960 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
6962 if (ae->ae_chop != NULL)
6964 /* Skip chop string. */
6968 i = mb_charlen(ae->ae_chop);
6974 p += STRLEN(ae->ae_chop);
6980 /* suffix: chop/add at the end of the word */
6981 vim_strncpy(newword, word, MAXWLEN - 1);
6982 if (ae->ae_chop != NULL)
6984 /* Remove chop string. */
6985 p = newword + STRLEN(newword);
6986 i = (int)MB_CHARLEN(ae->ae_chop);
6988 mb_ptr_back(newword, p);
6991 if (ae->ae_add != NULL)
6992 STRCAT(newword, ae->ae_add);
6996 use_pfxlist = pfxlist;
6997 use_pfxlen = pfxlen;
6999 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
7000 if (ae->ae_flags != NULL)
7002 /* Extract flags from the affix list. */
7003 use_flags |= get_affix_flags(affile, ae->ae_flags);
7005 if (affile->af_needaffix != 0 && flag_in_afflist(
7006 affile->af_flagtype, ae->ae_flags,
7007 affile->af_needaffix))
7010 /* When there is a CIRCUMFIX flag the other affix
7011 * must also have it and we don't add the word
7012 * with one affix. */
7013 if (affile->af_circumfix != 0 && flag_in_afflist(
7014 affile->af_flagtype, ae->ae_flags,
7015 affile->af_circumfix))
7017 use_condit |= CONDIT_CFIX;
7018 if ((condit & CONDIT_CFIX) == 0)
7022 if (affile->af_pfxpostpone
7023 || spin->si_compflags != NULL)
7025 if (affile->af_pfxpostpone)
7026 /* Get prefix IDS from the affix list. */
7027 use_pfxlen = get_pfxlist(affile,
7028 ae->ae_flags, store_afflist);
7031 use_pfxlist = store_afflist;
7033 /* Combine the prefix IDs. Avoid adding the
7035 for (i = 0; i < pfxlen; ++i)
7037 for (j = 0; j < use_pfxlen; ++j)
7038 if (pfxlist[i] == use_pfxlist[j])
7040 if (j == use_pfxlen)
7041 use_pfxlist[use_pfxlen++] = pfxlist[i];
7044 if (spin->si_compflags != NULL)
7045 /* Get compound IDS from the affix list. */
7046 get_compflags(affile, ae->ae_flags,
7047 use_pfxlist + use_pfxlen);
7049 /* Combine the list of compound flags.
7050 * Concatenate them to the prefix IDs list.
7051 * Avoid adding the same ID twice. */
7052 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7054 for (j = use_pfxlen;
7055 use_pfxlist[j] != NUL; ++j)
7056 if (pfxlist[i] == use_pfxlist[j])
7058 if (use_pfxlist[j] == NUL)
7060 use_pfxlist[j++] = pfxlist[i];
7061 use_pfxlist[j] = NUL;
7067 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
7068 * use the compound flags. */
7069 if (use_pfxlist != NULL && ae->ae_compforbid)
7071 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
7072 use_pfxlist = pfx_pfxlist;
7075 /* When there are postponed prefixes... */
7076 if (spin->si_prefroot != NULL
7077 && spin->si_prefroot->wn_sibling != NULL)
7079 /* ... add a flag to indicate an affix was used. */
7080 use_flags |= WF_HAS_AFF;
7082 /* ... don't use a prefix list if combining
7083 * affixes is not allowed. But do use the
7084 * compound flags after them. */
7085 if (!ah->ah_combine && use_pfxlist != NULL)
7086 use_pfxlist += use_pfxlen;
7089 /* When compounding is supported and there is no
7090 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7091 * side where the affix is applied. */
7092 if (spin->si_compflags != NULL && !ae->ae_comppermit)
7095 use_flags |= WF_NOCOMPAFT;
7097 use_flags |= WF_NOCOMPBEF;
7100 /* Store the modified word. */
7101 if (store_word(spin, newword, use_flags,
7102 spin->si_region, use_pfxlist,
7103 need_affix) == FAIL)
7106 /* When added a prefix or a first suffix and the affix
7107 * has flags may add a(nother) suffix. RECURSIVE! */
7108 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7109 if (store_aff_word(spin, newword, ae->ae_flags,
7110 affile, &affile->af_suff, xht,
7111 use_condit & (xht == NULL
7112 ? ~0 : ~CONDIT_SUF),
7113 use_flags, use_pfxlist, pfxlen) == FAIL)
7116 /* When added a suffix and combining is allowed also
7117 * try adding a prefix additionally. Both for the
7118 * word flags and for the affix flags. RECURSIVE! */
7119 if (xht != NULL && ah->ah_combine)
7121 if (store_aff_word(spin, newword,
7123 xht, NULL, use_condit,
7124 use_flags, use_pfxlist,
7126 || (ae->ae_flags != NULL
7127 && store_aff_word(spin, newword,
7128 ae->ae_flags, affile,
7129 xht, NULL, use_condit,
7130 use_flags, use_pfxlist,
7144 * Read a file with a list of words.
7147 spell_read_wordfile(spin, fname)
7153 char_u rline[MAXLINELEN];
7159 int did_word = FALSE;
7167 fd = mch_fopen((char *)fname, "r");
7170 EMSG2(_(e_notopen), fname);
7174 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7175 spell_message(spin, IObuff);
7178 * Read all the lines in the file one by one.
7180 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7185 /* Skip comment lines. */
7189 /* Remove CR, LF and white space from the end. */
7190 l = (int)STRLEN(rline);
7191 while (l > 0 && rline[l - 1] <= ' ')
7194 continue; /* empty or blank line */
7197 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
7200 if (spin->si_conv.vc_type != CONV_NONE)
7202 pc = string_convert(&spin->si_conv, rline, NULL);
7205 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7206 fname, lnum, rline);
7221 if (STRNCMP(line, "encoding=", 9) == 0)
7223 if (spin->si_conv.vc_type != CONV_NONE)
7224 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7225 fname, lnum, line - 1);
7227 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7228 fname, lnum, line - 1);
7234 /* Setup for conversion to 'encoding'. */
7236 enc = enc_canonize(line);
7237 if (enc != NULL && !spin->si_ascii
7238 && convert_setup(&spin->si_conv, enc,
7240 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
7241 fname, line, p_enc);
7243 spin->si_conv.vc_fail = TRUE;
7245 smsg((char_u *)_("Conversion in %s not supported"), fname);
7251 if (STRNCMP(line, "regions=", 8) == 0)
7253 if (spin->si_region_count > 1)
7254 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7259 if (STRLEN(line) > 16)
7260 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7264 spin->si_region_count = (int)STRLEN(line) / 2;
7265 STRCPY(spin->si_region_name, line);
7267 /* Adjust the mask for a word valid in all regions. */
7268 spin->si_region = (1 << spin->si_region_count) - 1;
7274 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7275 fname, lnum, line - 1);
7280 regionmask = spin->si_region;
7282 /* Check for flags and region after a slash. */
7283 p = vim_strchr(line, '/');
7289 if (*p == '=') /* keep-case word */
7290 flags |= WF_KEEPCAP | WF_FIXCAP;
7291 else if (*p == '!') /* Bad, bad, wicked word. */
7293 else if (*p == '?') /* Rare word. */
7295 else if (VIM_ISDIGIT(*p)) /* region number(s) */
7297 if ((flags & WF_REGION) == 0) /* first one */
7302 if (l > spin->si_region_count)
7304 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
7308 regionmask |= 1 << (l - 1);
7312 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7320 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7321 if (spin->si_ascii && has_non_ascii(line))
7327 /* Normal word: store it. */
7328 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
7339 if (spin->si_ascii && non_ascii > 0)
7341 vim_snprintf((char *)IObuff, IOSIZE,
7342 _("Ignored %d words with non-ASCII characters"), non_ascii);
7343 spell_message(spin, IObuff);
7350 * Get part of an sblock_T, "len" bytes long.
7351 * This avoids calling free() for every little struct we use (and keeping
7353 * The memory is cleared to all zeros.
7354 * Returns NULL when out of memory.
7357 getroom(spin, len, align)
7359 size_t len; /* length needed */
7360 int align; /* align for pointer */
7363 sblock_T *bl = spin->si_blocks;
7365 if (align && bl != NULL)
7366 /* Round size up for alignment. On some systems structures need to be
7367 * aligned to the size of a pointer (e.g., SPARC). */
7368 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7369 & ~(sizeof(char *) - 1);
7371 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7373 if (len >= SBLOCKSIZE)
7376 /* Allocate a block of memory. It is not freed until much later. */
7377 bl = (sblock_T *)alloc_clear(
7378 (unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7381 if (!spin->si_did_emsg)
7383 EMSG(_("E845: Insufficient memory, word list will be incomplete"));
7384 spin->si_did_emsg = TRUE;
7388 bl->sb_next = spin->si_blocks;
7389 spin->si_blocks = bl;
7391 ++spin->si_blocks_cnt;
7394 p = bl->sb_data + bl->sb_used;
7395 bl->sb_used += (int)len;
7401 * Make a copy of a string into memory allocated with getroom().
7402 * Returns NULL when out of memory.
7405 getroom_save(spin, s)
7411 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
7419 * Free the list of allocated sblock_T.
7436 * Allocate the root of a word tree.
7437 * Returns NULL when out of memory.
7440 wordtree_alloc(spin)
7443 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
7447 * Store a word in the tree(s).
7448 * Always store it in the case-folded tree. For a keep-case word this is
7449 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7450 * used to find suggestions.
7451 * For a keep-case word also store it in the keep-case tree.
7452 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7456 store_word(spin, word, flags, region, pfxlist, need_affix)
7459 int flags; /* extra flags, WF_BANNED */
7460 int region; /* supported region(s) */
7461 char_u *pfxlist; /* list of prefix IDs or NULL */
7462 int need_affix; /* only store word with affix ID */
7464 int len = (int)STRLEN(word);
7465 int ct = captype(word, word + len);
7466 char_u foldword[MAXWLEN];
7470 (void)spell_casefold(word, len, foldword, MAXWLEN);
7471 for (p = pfxlist; res == OK; ++p)
7473 if (!need_affix || (p != NULL && *p != NUL))
7474 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
7475 region, p == NULL ? 0 : *p);
7476 if (p == NULL || *p == NUL)
7479 ++spin->si_foldwcount;
7481 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
7483 for (p = pfxlist; res == OK; ++p)
7485 if (!need_affix || (p != NULL && *p != NUL))
7486 res = tree_add_word(spin, word, spin->si_keeproot, flags,
7487 region, p == NULL ? 0 : *p);
7488 if (p == NULL || *p == NUL)
7491 ++spin->si_keepwcount;
7497 * Add word "word" to a word tree at "root".
7498 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
7499 * "rare" and "region" is the condition nr.
7500 * Returns FAIL when out of memory.
7503 tree_add_word(spin, word, root, flags, region, affixID)
7511 wordnode_T *node = root;
7513 wordnode_T *copyp, **copyprev;
7514 wordnode_T **prev = NULL;
7517 /* Add each byte of the word to the tree, including the NUL at the end. */
7520 /* When there is more than one reference to this node we need to make
7521 * a copy, so that we can modify it. Copy the whole list of siblings
7522 * (we don't optimize for a partly shared list of siblings). */
7523 if (node != NULL && node->wn_refs > 1)
7527 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7529 /* Allocate a new node and copy the info. */
7530 np = get_wordnode(spin);
7533 np->wn_child = copyp->wn_child;
7534 if (np->wn_child != NULL)
7535 ++np->wn_child->wn_refs; /* child gets extra ref */
7536 np->wn_byte = copyp->wn_byte;
7537 if (np->wn_byte == NUL)
7539 np->wn_flags = copyp->wn_flags;
7540 np->wn_region = copyp->wn_region;
7541 np->wn_affixID = copyp->wn_affixID;
7544 /* Link the new node in the list, there will be one ref. */
7546 if (copyprev != NULL)
7548 copyprev = &np->wn_sibling;
7550 /* Let "node" point to the head of the copied list. */
7556 /* Look for the sibling that has the same character. They are sorted
7557 * on byte value, thus stop searching when a sibling is found with a
7558 * higher byte value. For zero bytes (end of word) the sorting is
7559 * done on flags and then on affixID. */
7561 && (node->wn_byte < word[i]
7562 || (node->wn_byte == NUL
7564 ? node->wn_affixID < (unsigned)affixID
7565 : (node->wn_flags < (unsigned)(flags & WN_MASK)
7566 || (node->wn_flags == (flags & WN_MASK)
7567 && (spin->si_sugtree
7568 ? (node->wn_region & 0xffff) < region
7570 < (unsigned)affixID)))))))
7572 prev = &node->wn_sibling;
7576 || node->wn_byte != word[i]
7580 || node->wn_flags != (flags & WN_MASK)
7581 || node->wn_affixID != affixID)))
7583 /* Allocate a new node. */
7584 np = get_wordnode(spin);
7587 np->wn_byte = word[i];
7589 /* If "node" is NULL this is a new child or the end of the sibling
7590 * list: ref count is one. Otherwise use ref count of sibling and
7591 * make ref count of sibling one (matters when inserting in front
7592 * of the list of siblings). */
7597 np->wn_refs = node->wn_refs;
7602 np->wn_sibling = node;
7608 node->wn_flags = flags;
7609 node->wn_region |= region;
7610 node->wn_affixID = affixID;
7613 prev = &node->wn_child;
7616 #ifdef SPELL_PRINTTREE
7617 smsg("Added \"%s\"", word);
7618 spell_print_tree(root->wn_sibling);
7621 /* count nr of words added since last message */
7622 ++spin->si_msg_count;
7624 if (spin->si_compress_cnt > 1)
7626 if (--spin->si_compress_cnt == 1)
7627 /* Did enough words to lower the block count limit. */
7628 spin->si_blocks_cnt += compress_inc;
7632 * When we have allocated lots of memory we need to compress the word tree
7633 * to free up some room. But compression is slow, and we might actually
7634 * need that room, thus only compress in the following situations:
7635 * 1. When not compressed before (si_compress_cnt == 0): when using
7636 * "compress_start" blocks.
7637 * 2. When compressed before and used "compress_inc" blocks before
7638 * adding "compress_added" words (si_compress_cnt > 1).
7639 * 3. When compressed before, added "compress_added" words
7640 * (si_compress_cnt == 1) and the number of free nodes drops below the
7641 * maximum word length.
7643 #ifndef SPELL_PRINTTREE
7644 if (spin->si_compress_cnt == 1
7645 ? spin->si_free_count < MAXWLEN
7646 : spin->si_blocks_cnt >= compress_start)
7649 /* Decrement the block counter. The effect is that we compress again
7650 * when the freed up room has been used and another "compress_inc"
7651 * blocks have been allocated. Unless "compress_added" words have
7652 * been added, then the limit is put back again. */
7653 spin->si_blocks_cnt -= compress_inc;
7654 spin->si_compress_cnt = compress_added;
7656 if (spin->si_verbose)
7659 msg_puts((char_u *)_(msg_compressing));
7666 /* Compress both trees. Either they both have many nodes, which makes
7667 * compression useful, or one of them is small, which means
7668 * compression goes fast. But when filling the souldfold word tree
7669 * there is no keep-case tree. */
7670 wordtree_compress(spin, spin->si_foldroot);
7672 wordtree_compress(spin, spin->si_keeproot);
7679 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7690 if (!VIM_ISDIGIT(*p))
7692 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7693 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7697 if (!VIM_ISDIGIT(*p))
7699 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7703 if (!VIM_ISDIGIT(*p))
7705 added = getdigits(&p) * 1024;
7709 if (start == 0 || incr == 0 || added == 0 || incr > start)
7712 compress_start = start;
7713 compress_inc = incr;
7714 compress_added = added;
7720 * Get a wordnode_T, either from the list of previously freed nodes or
7721 * allocate a new one.
7722 * Returns NULL when out of memory.
7730 if (spin->si_first_free == NULL)
7731 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
7734 n = spin->si_first_free;
7735 spin->si_first_free = n->wn_child;
7736 vim_memset(n, 0, sizeof(wordnode_T));
7737 --spin->si_free_count;
7739 #ifdef SPELL_PRINTTREE
7741 n->wn_nr = ++spin->si_wordnode_nr;
7747 * Decrement the reference count on a node (which is the head of a list of
7748 * siblings). If the reference count becomes zero free the node and its
7750 * Returns the number of nodes actually freed.
7753 deref_wordnode(spin, node)
7760 if (--node->wn_refs == 0)
7762 for (np = node; np != NULL; np = np->wn_sibling)
7764 if (np->wn_child != NULL)
7765 cnt += deref_wordnode(spin, np->wn_child);
7766 free_wordnode(spin, np);
7769 ++cnt; /* length field */
7775 * Free a wordnode_T for re-use later.
7776 * Only the "wn_child" field becomes invalid.
7779 free_wordnode(spin, n)
7783 n->wn_child = spin->si_first_free;
7784 spin->si_first_free = n;
7785 ++spin->si_free_count;
7789 * Compress a tree: find tails that are identical and can be shared.
7792 wordtree_compress(spin, root)
7801 /* Skip the root itself, it's not actually used. The first sibling is the
7802 * start of the tree. */
7803 if (root->wn_sibling != NULL)
7806 n = node_compress(spin, root->wn_sibling, &ht, &tot);
7808 #ifndef SPELL_PRINTTREE
7809 if (spin->si_verbose || p_verbose > 2)
7813 perc = (tot - n) / (tot / 100);
7817 perc = (tot - n) * 100 / tot;
7818 vim_snprintf((char *)IObuff, IOSIZE,
7819 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7820 n, tot, tot - n, perc);
7821 spell_message(spin, IObuff);
7823 #ifdef SPELL_PRINTTREE
7824 spell_print_tree(root->wn_sibling);
7831 * Compress a node, its siblings and its children, depth first.
7832 * Returns the number of compressed nodes.
7835 node_compress(spin, node, ht, tot)
7839 int *tot; /* total count of nodes before compressing,
7840 incremented while going through the tree */
7852 * Go through the list of siblings. Compress each child and then try
7853 * finding an identical child to replace it.
7854 * Note that with "child" we mean not just the node that is pointed to,
7855 * but the whole list of siblings of which the child node is the first.
7857 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
7860 if ((child = np->wn_child) != NULL)
7862 /* Compress the child first. This fills hashkey. */
7863 compressed += node_compress(spin, child, ht, tot);
7865 /* Try to find an identical child. */
7866 hash = hash_hash(child->wn_u1.hashkey);
7867 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
7868 if (!HASHITEM_EMPTY(hi))
7870 /* There are children we encountered before with a hash value
7871 * identical to the current child. Now check if there is one
7872 * that is really identical. */
7873 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
7874 if (node_equal(child, tp))
7876 /* Found one! Now use that child in place of the
7877 * current one. This means the current child and all
7878 * its siblings is unlinked from the tree. */
7880 compressed += deref_wordnode(spin, child);
7886 /* No other child with this hash value equals the child of
7887 * the node, add it to the linked list after the first
7890 child->wn_u2.next = tp->wn_u2.next;
7891 tp->wn_u2.next = child;
7895 /* No other child has this hash value, add it to the
7897 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
7900 *tot += len + 1; /* add one for the node that stores the length */
7903 * Make a hash key for the node and its siblings, so that we can quickly
7904 * find a lookalike node. This must be done after compressing the sibling
7905 * list, otherwise the hash key would become invalid by the compression.
7907 node->wn_u1.hashkey[0] = len;
7909 for (np = node; np != NULL; np = np->wn_sibling)
7911 if (np->wn_byte == NUL)
7912 /* end node: use wn_flags, wn_region and wn_affixID */
7913 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
7915 /* byte node: use the byte value and the child pointer */
7916 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
7920 /* Avoid NUL bytes, it terminates the hash key. */
7922 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
7923 n = (nr >> 8) & 0xff;
7924 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
7925 n = (nr >> 16) & 0xff;
7926 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
7927 n = (nr >> 24) & 0xff;
7928 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7929 node->wn_u1.hashkey[5] = NUL;
7931 /* Check for CTRL-C pressed now and then. */
7938 * Return TRUE when two nodes have identical siblings and children.
7948 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7949 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7950 if (p1->wn_byte != p2->wn_byte
7951 || (p1->wn_byte == NUL
7952 ? (p1->wn_flags != p2->wn_flags
7953 || p1->wn_region != p2->wn_region
7954 || p1->wn_affixID != p2->wn_affixID)
7955 : (p1->wn_child != p2->wn_child)))
7958 return p1 == NULL && p2 == NULL;
7965 rep_compare __ARGS((const void *s1, const void *s2));
7968 * Function given to qsort() to sort the REP items on "from" string.
7978 fromto_T *p1 = (fromto_T *)s1;
7979 fromto_T *p2 = (fromto_T *)s2;
7981 return STRCMP(p1->ft_from, p2->ft_from);
7985 * Write the Vim .spl file "fname".
7986 * Return FAIL or OK;
7989 write_vim_spell(spin, fname)
8005 size_t fwv = 1; /* collect return value of fwrite() to avoid
8006 warnings from picky compiler */
8008 fd = mch_fopen((char *)fname, "w");
8011 EMSG2(_(e_notopen), fname);
8015 /* <HEADER>: <fileID> <versionnr> */
8017 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
8018 if (fwv != (size_t)1)
8019 /* Catch first write error, don't try writing more. */
8022 putc(VIMSPELLVERSION, fd); /* <versionnr> */
8025 * <SECTIONS>: <section> ... <sectionend>
8028 /* SN_INFO: <infotext> */
8029 if (spin->si_info != NULL)
8031 putc(SN_INFO, fd); /* <sectionID> */
8032 putc(0, fd); /* <sectionflags> */
8034 i = (int)STRLEN(spin->si_info);
8035 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
8036 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
8039 /* SN_REGION: <regionname> ...
8040 * Write the region names only if there is more than one. */
8041 if (spin->si_region_count > 1)
8043 putc(SN_REGION, fd); /* <sectionID> */
8044 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8045 l = spin->si_region_count * 2;
8046 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8047 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
8048 /* <regionname> ... */
8049 regionmask = (1 << spin->si_region_count) - 1;
8054 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8056 * The table with character flags and the table for case folding.
8057 * This makes sure the same characters are recognized as word characters
8058 * when generating an when using a spell file.
8059 * Skip this for ASCII, the table may conflict with the one used for
8061 * Also skip this for an .add.spl file, the main spell file must contain
8062 * the table (avoids that it conflicts). File is shorter too.
8064 if (!spin->si_ascii && !spin->si_add)
8066 char_u folchars[128 * 8];
8069 putc(SN_CHARFLAGS, fd); /* <sectionID> */
8070 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8072 /* Form the <folchars> string first, we need to know its length. */
8074 for (i = 128; i < 256; ++i)
8078 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8081 folchars[l++] = spelltab.st_fold[i];
8083 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8085 fputc(128, fd); /* <charflagslen> */
8086 for (i = 128; i < 256; ++i)
8089 if (spelltab.st_isw[i])
8091 if (spelltab.st_isu[i])
8093 fputc(flags, fd); /* <charflags> */
8096 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
8097 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
8100 /* SN_MIDWORD: <midword> */
8101 if (spin->si_midword != NULL)
8103 putc(SN_MIDWORD, fd); /* <sectionID> */
8104 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8106 i = (int)STRLEN(spin->si_midword);
8107 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
8108 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8112 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8113 if (spin->si_prefcond.ga_len > 0)
8115 putc(SN_PREFCOND, fd); /* <sectionID> */
8116 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8118 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8119 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8121 write_spell_prefcond(fd, &spin->si_prefcond);
8124 /* SN_REP: <repcount> <rep> ...
8125 * SN_SAL: <salflags> <salcount> <sal> ...
8126 * SN_REPSAL: <repcount> <rep> ... */
8128 /* round 1: SN_REP section
8129 * round 2: SN_SAL section (unless SN_SOFO is used)
8130 * round 3: SN_REPSAL section */
8131 for (round = 1; round <= 3; ++round)
8134 gap = &spin->si_rep;
8135 else if (round == 2)
8137 /* Don't write SN_SAL when using a SN_SOFO section */
8138 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8140 gap = &spin->si_sal;
8143 gap = &spin->si_repsal;
8145 /* Don't write the section if there are no items. */
8146 if (gap->ga_len == 0)
8149 /* Sort the REP/REPSAL items. */
8151 qsort(gap->ga_data, (size_t)gap->ga_len,
8152 sizeof(fromto_T), rep_compare);
8154 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8155 putc(i, fd); /* <sectionID> */
8157 /* This is for making suggestions, section is not required. */
8158 putc(0, fd); /* <sectionflags> */
8160 /* Compute the length of what follows. */
8161 l = 2; /* count <repcount> or <salcount> */
8162 for (i = 0; i < gap->ga_len; ++i)
8164 ftp = &((fromto_T *)gap->ga_data)[i];
8165 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8166 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
8169 ++l; /* count <salflags> */
8170 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8175 if (spin->si_followup)
8177 if (spin->si_collapse)
8179 if (spin->si_rem_accents)
8180 i |= SAL_REM_ACCENTS;
8181 putc(i, fd); /* <salflags> */
8184 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8185 for (i = 0; i < gap->ga_len; ++i)
8187 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8188 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8189 ftp = &((fromto_T *)gap->ga_data)[i];
8190 for (rr = 1; rr <= 2; ++rr)
8192 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
8196 fwv &= fwrite(p, l, (size_t)1, fd);
8202 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8203 * This is for making suggestions, section is not required. */
8204 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8206 putc(SN_SOFO, fd); /* <sectionID> */
8207 putc(0, fd); /* <sectionflags> */
8209 l = (int)STRLEN(spin->si_sofofr);
8210 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8213 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8214 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
8216 l = (int)STRLEN(spin->si_sofoto);
8217 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8218 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
8221 /* SN_WORDS: <word> ...
8222 * This is for making suggestions, section is not required. */
8223 if (spin->si_commonwords.ht_used > 0)
8225 putc(SN_WORDS, fd); /* <sectionID> */
8226 putc(0, fd); /* <sectionflags> */
8228 /* round 1: count the bytes
8229 * round 2: write the bytes */
8230 for (round = 1; round <= 2; ++round)
8236 todo = (int)spin->si_commonwords.ht_used;
8237 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8238 if (!HASHITEM_EMPTY(hi))
8240 l = (int)STRLEN(hi->hi_key) + 1;
8242 if (round == 2) /* <word> */
8243 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8247 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8252 * This is for making suggestions, section is not required. */
8253 if (spin->si_map.ga_len > 0)
8255 putc(SN_MAP, fd); /* <sectionID> */
8256 putc(0, fd); /* <sectionflags> */
8257 l = spin->si_map.ga_len;
8258 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8259 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8263 /* SN_SUGFILE: <timestamp>
8264 * This is used to notify that a .sug file may be available and at the
8265 * same time allows for checking that a .sug file that is found matches
8266 * with this .spl file. That's because the word numbers must be exactly
8268 if (!spin->si_nosugfile
8269 && (spin->si_sal.ga_len > 0
8270 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8272 putc(SN_SUGFILE, fd); /* <sectionID> */
8273 putc(0, fd); /* <sectionflags> */
8274 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8276 /* Set si_sugtime and write it to the file. */
8277 spin->si_sugtime = time(NULL);
8278 put_time(fd, spin->si_sugtime); /* <timestamp> */
8281 /* SN_NOSPLITSUGS: nothing
8282 * This is used to notify that no suggestions with word splits are to be
8284 if (spin->si_nosplitsugs)
8286 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8287 putc(0, fd); /* <sectionflags> */
8288 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8291 /* SN_COMPOUND: compound info.
8292 * We don't mark it required, when not supported all compound words will
8294 if (spin->si_compflags != NULL)
8296 putc(SN_COMPOUND, fd); /* <sectionID> */
8297 putc(0, fd); /* <sectionflags> */
8299 l = (int)STRLEN(spin->si_compflags);
8300 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8301 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
8302 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8304 putc(spin->si_compmax, fd); /* <compmax> */
8305 putc(spin->si_compminlen, fd); /* <compminlen> */
8306 putc(spin->si_compsylmax, fd); /* <compsylmax> */
8307 putc(0, fd); /* for Vim 7.0b compatibility */
8308 putc(spin->si_compoptions, fd); /* <compoptions> */
8309 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8310 /* <comppatcount> */
8311 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8313 p = ((char_u **)(spin->si_comppat.ga_data))[i];
8314 putc((int)STRLEN(p), fd); /* <comppatlen> */
8315 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8319 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8323 /* SN_NOBREAK: NOBREAK flag */
8324 if (spin->si_nobreak)
8326 putc(SN_NOBREAK, fd); /* <sectionID> */
8327 putc(0, fd); /* <sectionflags> */
8329 /* It's empty, the presence of the section flags the feature. */
8330 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8333 /* SN_SYLLABLE: syllable info.
8334 * We don't mark it required, when not supported syllables will not be
8336 if (spin->si_syllable != NULL)
8338 putc(SN_SYLLABLE, fd); /* <sectionID> */
8339 putc(0, fd); /* <sectionflags> */
8341 l = (int)STRLEN(spin->si_syllable);
8342 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8343 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8347 /* end of <SECTIONS> */
8348 putc(SN_END, fd); /* <sectionend> */
8352 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
8354 spin->si_memtot = 0;
8355 for (round = 1; round <= 3; ++round)
8358 tree = spin->si_foldroot->wn_sibling;
8359 else if (round == 2)
8360 tree = spin->si_keeproot->wn_sibling;
8362 tree = spin->si_prefroot->wn_sibling;
8364 /* Clear the index and wnode fields in the tree. */
8367 /* Count the number of nodes. Needed to be able to allocate the
8368 * memory when reading the nodes. Also fills in index for shared
8370 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
8372 /* number of nodes in 4 bytes */
8373 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8374 spin->si_memtot += nodecount + nodecount * sizeof(int);
8376 /* Write the nodes. */
8377 (void)put_node(fd, tree, 0, regionmask, round == 3);
8380 /* Write another byte to check for errors (file system full). */
8381 if (putc(0, fd) == EOF)
8384 if (fclose(fd) == EOF)
8387 if (fwv != (size_t)1)
8396 * Clear the index and wnode fields of "node", it siblings and its
8397 * children. This is needed because they are a union with other items to save
8407 for (np = node; np != NULL; np = np->wn_sibling)
8409 np->wn_u1.index = 0;
8410 np->wn_u2.wnode = NULL;
8412 if (np->wn_byte != NUL)
8413 clear_node(np->wn_child);
8419 * Dump a word tree at node "node".
8421 * This first writes the list of possible bytes (siblings). Then for each
8422 * byte recursively write the children.
8424 * NOTE: The code here must match the code in read_tree_node(), since
8425 * assumptions are made about the indexes (so that we don't have to write them
8428 * Returns the number of nodes used.
8431 put_node(fd, node, idx, regionmask, prefixtree)
8432 FILE *fd; /* NULL when only counting */
8436 int prefixtree; /* TRUE for PREFIXTREE */
8439 int siblingcount = 0;
8443 /* If "node" is zero the tree is empty. */
8447 /* Store the index where this node is written. */
8448 node->wn_u1.index = idx;
8450 /* Count the number of siblings. */
8451 for (np = node; np != NULL; np = np->wn_sibling)
8454 /* Write the sibling count. */
8456 putc(siblingcount, fd); /* <siblingcount> */
8458 /* Write each sibling byte and optionally extra info. */
8459 for (np = node; np != NULL; np = np->wn_sibling)
8461 if (np->wn_byte == 0)
8465 /* For a NUL byte (end of word) write the flags etc. */
8468 /* In PREFIXTREE write the required affixID and the
8469 * associated condition nr (stored in wn_region). The
8470 * byte value is misused to store the "rare" and "not
8471 * combining" flags */
8472 if (np->wn_flags == (short_u)PFX_FLAGS)
8473 putc(BY_NOFLAGS, fd); /* <byte> */
8476 putc(BY_FLAGS, fd); /* <byte> */
8477 putc(np->wn_flags, fd); /* <pflags> */
8479 putc(np->wn_affixID, fd); /* <affixID> */
8480 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
8484 /* For word trees we write the flag/region items. */
8485 flags = np->wn_flags;
8486 if (regionmask != 0 && np->wn_region != regionmask)
8488 if (np->wn_affixID != 0)
8492 /* word without flags or region */
8493 putc(BY_NOFLAGS, fd); /* <byte> */
8497 if (np->wn_flags >= 0x100)
8499 putc(BY_FLAGS2, fd); /* <byte> */
8500 putc(flags, fd); /* <flags> */
8501 putc((unsigned)flags >> 8, fd); /* <flags2> */
8505 putc(BY_FLAGS, fd); /* <byte> */
8506 putc(flags, fd); /* <flags> */
8508 if (flags & WF_REGION)
8509 putc(np->wn_region, fd); /* <region> */
8511 putc(np->wn_affixID, fd); /* <affixID> */
8518 if (np->wn_child->wn_u1.index != 0
8519 && np->wn_child->wn_u2.wnode != node)
8521 /* The child is written elsewhere, write the reference. */
8524 putc(BY_INDEX, fd); /* <byte> */
8526 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
8529 else if (np->wn_child->wn_u2.wnode == NULL)
8530 /* We will write the child below and give it an index. */
8531 np->wn_child->wn_u2.wnode = node;
8534 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8542 /* Space used in the array when reading: one for each sibling and one for
8544 newindex += siblingcount + 1;
8546 /* Recursively dump the children of each sibling. */
8547 for (np = node; np != NULL; np = np->wn_sibling)
8548 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8549 newindex = put_node(fd, np->wn_child, newindex, regionmask,
8557 * ":mkspell [-ascii] outfile infile ..."
8558 * ":mkspell [-ascii] addfile"
8566 char_u *arg = eap->arg;
8569 if (STRNCMP(arg, "-ascii", 6) == 0)
8572 arg = skipwhite(arg + 6);
8575 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8576 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8578 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
8579 FreeWild(fcount, fnames);
8584 * Create the .sug file.
8585 * Uses the soundfold info in "spin".
8586 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8589 spell_make_sugfile(spin, wfname)
8593 char_u *fname = NULL;
8596 int free_slang = FALSE;
8599 * Read back the .spl file that was written. This fills the required
8600 * info for soundfolding. This also uses less memory than the
8601 * pointer-linked version of the trie. And it avoids having two versions
8602 * of the code for the soundfolding stuff.
8603 * It might have been done already by spell_reload_one().
8605 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8606 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8610 spell_message(spin, (char_u *)_("Reading back spell file..."));
8611 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8618 * Clear the info in "spin" that is used.
8620 spin->si_blocks = NULL;
8621 spin->si_blocks_cnt = 0;
8622 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8623 spin->si_free_count = 0;
8624 spin->si_first_free = NULL;
8625 spin->si_foldwcount = 0;
8628 * Go through the trie of good words, soundfold each word and add it to
8629 * the soundfold trie.
8631 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8632 if (sug_filltree(spin, slang) == FAIL)
8636 * Create the table which links each soundfold word with a list of the
8637 * good words it may come from. Creates buffer "spin->si_spellbuf".
8638 * This also removes the wordnr from the NUL byte entries to make
8639 * compression possible.
8641 if (sug_maketable(spin) == FAIL)
8644 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8645 (long)spin->si_spellbuf->b_ml.ml_line_count);
8648 * Compress the soundfold trie.
8650 spell_message(spin, (char_u *)_(msg_compressing));
8651 wordtree_compress(spin, spin->si_foldroot);
8654 * Write the .sug file.
8655 * Make the file name by changing ".spl" to ".sug".
8657 fname = alloc(MAXPATHL);
8660 vim_strncpy(fname, wfname, MAXPATHL - 1);
8661 len = (int)STRLEN(fname);
8662 fname[len - 2] = 'u';
8663 fname[len - 1] = 'g';
8664 sug_write(spin, fname);
8670 free_blocks(spin->si_blocks);
8671 close_spellbuf(spin->si_spellbuf);
8675 * Build the soundfold trie for language "slang".
8678 sug_filltree(spin, slang)
8685 idx_T arridx[MAXWLEN];
8687 char_u tword[MAXWLEN];
8688 char_u tsalword[MAXWLEN];
8691 unsigned words_done = 0;
8692 int wordcount[MAXWLEN];
8694 /* We use si_foldroot for the souldfolded trie. */
8695 spin->si_foldroot = wordtree_alloc(spin);
8696 if (spin->si_foldroot == NULL)
8699 /* let tree_add_word() know we're adding to the soundfolded tree */
8700 spin->si_sugtree = TRUE;
8703 * Go through the whole case-folded tree, soundfold each word and put it
8706 byts = slang->sl_fbyts;
8707 idxs = slang->sl_fidxs;
8714 while (depth >= 0 && !got_int)
8716 if (curi[depth] > byts[arridx[depth]])
8718 /* Done all bytes at this node, go up one level. */
8719 idxs[arridx[depth]] = wordcount[depth];
8721 wordcount[depth - 1] += wordcount[depth];
8729 /* Do one more byte at this node. */
8730 n = arridx[depth] + curi[depth];
8736 /* Sound-fold the word. */
8738 spell_soundfold(slang, tword, TRUE, tsalword);
8740 /* We use the "flags" field for the MSB of the wordnr,
8741 * "region" for the LSB of the wordnr. */
8742 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8743 words_done >> 16, words_done & 0xffff,
8750 /* Reset the block count each time to avoid compression
8752 spin->si_blocks_cnt = 0;
8754 /* Skip over any other NUL bytes (same word with different
8756 while (byts[n + 1] == 0)
8764 /* Normal char, go one level deeper. */
8766 arridx[depth] = idxs[n];
8768 wordcount[depth] = 0;
8773 smsg((char_u *)_("Total number of words: %d"), words_done);
8779 * Make the table that links each word in the soundfold trie to the words it
8780 * can be produced from.
8781 * This is not unlike lines in a file, thus use a memfile to be able to access
8782 * the table efficiently.
8783 * Returns FAIL when out of memory.
8792 /* Allocate a buffer, open a memline for it and create the swap file
8793 * (uses a temp file, not a .swp file). */
8794 spin->si_spellbuf = open_spellbuf();
8795 if (spin->si_spellbuf == NULL)
8798 /* Use a buffer to store the line info, avoids allocating many small
8799 * pieces of memory. */
8800 ga_init2(&ga, 1, 100);
8802 /* recursively go through the tree */
8803 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8811 * Fill the table for one node and its children.
8812 * Returns the wordnr at the start of the node.
8813 * Returns -1 when out of memory.
8816 sug_filltable(spin, node, startwordnr, gap)
8820 garray_T *gap; /* place to store line of numbers */
8823 int wordnr = startwordnr;
8827 for (p = node; p != NULL; p = p->wn_sibling)
8829 if (p->wn_byte == NUL)
8833 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8835 if (ga_grow(gap, 10) == FAIL)
8838 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8839 /* Compute the offset from the previous nr and store the
8840 * offset in a way that it takes a minimum number of bytes.
8841 * It's a bit like utf-8, but without the need to mark
8842 * following bytes. */
8845 gap->ga_len += offset2bytes(nr,
8846 (char_u *)gap->ga_data + gap->ga_len);
8849 /* add the NUL byte */
8850 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8852 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8853 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8857 /* Remove extra NUL entries, we no longer need them. We don't
8858 * bother freeing the nodes, the won't be reused anyway. */
8859 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8860 p->wn_sibling = p->wn_sibling->wn_sibling;
8862 /* Clear the flags on the remaining NUL node, so that compression
8863 * works a lot better. */
8869 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8878 * Convert an offset into a minimal number of bytes.
8879 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8883 offset2bytes(nr, buf)
8890 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8898 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8906 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8913 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8925 * Opposite of offset2bytes().
8926 * "pp" points to the bytes and is advanced over it.
8927 * Returns the offset.
8938 if ((c & 0x80) == 0x00) /* 1 byte */
8942 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8944 nr = (c & 0x3f) - 1;
8945 nr = nr * 255 + (*p++ - 1);
8947 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8949 nr = (c & 0x1f) - 1;
8950 nr = nr * 255 + (*p++ - 1);
8951 nr = nr * 255 + (*p++ - 1);
8955 nr = (c & 0x0f) - 1;
8956 nr = nr * 255 + (*p++ - 1);
8957 nr = nr * 255 + (*p++ - 1);
8958 nr = nr * 255 + (*p++ - 1);
8966 * Write the .sug file in "fname".
8969 sug_write(spin, fname)
8981 /* Create the file. Note that an existing file is silently overwritten! */
8982 fd = mch_fopen((char *)fname, "w");
8985 EMSG2(_(e_notopen), fname);
8989 vim_snprintf((char *)IObuff, IOSIZE,
8990 _("Writing suggestion file %s ..."), fname);
8991 spell_message(spin, IObuff);
8994 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8996 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
9001 putc(VIMSUGVERSION, fd); /* <versionnr> */
9003 /* Write si_sugtime to the file. */
9004 put_time(fd, spin->si_sugtime); /* <timestamp> */
9009 spin->si_memtot = 0;
9010 tree = spin->si_foldroot->wn_sibling;
9012 /* Clear the index and wnode fields in the tree. */
9015 /* Count the number of nodes. Needed to be able to allocate the
9016 * memory when reading the nodes. Also fills in index for shared
9018 nodecount = put_node(NULL, tree, 0, 0, FALSE);
9020 /* number of nodes in 4 bytes */
9021 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
9022 spin->si_memtot += nodecount + nodecount * sizeof(int);
9024 /* Write the nodes. */
9025 (void)put_node(fd, tree, 0, 0, FALSE);
9028 * <SUGTABLE>: <sugwcount> <sugline> ...
9030 wcount = spin->si_spellbuf->b_ml.ml_line_count;
9031 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
9033 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
9035 /* <sugline>: <sugnr> ... NUL */
9036 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
9037 len = (int)STRLEN(line) + 1;
9038 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
9043 spin->si_memtot += len;
9046 /* Write another byte to check for errors. */
9047 if (putc(0, fd) == EOF)
9050 vim_snprintf((char *)IObuff, IOSIZE,
9051 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9052 spell_message(spin, IObuff);
9055 /* close the file */
9060 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9061 * list and only contains text lines. Can use a swapfile to reduce memory
9063 * Most other fields are invalid! Esp. watch out for string options being
9064 * NULL and there is no undo info.
9065 * Returns NULL when out of memory.
9072 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9075 buf->b_spell = TRUE;
9076 buf->b_p_swf = TRUE; /* may create a swap file */
9078 ml_open_file(buf); /* create swap file now */
9084 * Close the buffer used for spell info.
9092 ml_close(buf, TRUE);
9099 * Create a Vim spell file from one or more word lists.
9100 * "fnames[0]" is the output file name.
9101 * "fnames[fcount - 1]" is the last input file name.
9102 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9103 * and ".spl" is appended to make the output file name.
9106 mkspell(fcount, fnames, ascii, overwrite, added_word)
9109 int ascii; /* -ascii argument given */
9110 int overwrite; /* overwrite existing output file */
9111 int added_word; /* invoked through "zg" */
9113 char_u *fname = NULL;
9117 afffile_T *(afile[8]);
9124 vim_memset(&spin, 0, sizeof(spin));
9125 spin.si_verbose = !added_word;
9126 spin.si_ascii = ascii;
9127 spin.si_followup = TRUE;
9128 spin.si_rem_accents = TRUE;
9129 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
9130 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
9131 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9132 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
9133 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
9134 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
9135 hash_init(&spin.si_commonwords);
9136 spin.si_newcompID = 127; /* start compound ID at first maximum */
9138 /* default: fnames[0] is output file, following are input files */
9139 innames = &fnames[1];
9140 incount = fcount - 1;
9142 wfname = alloc(MAXPATHL);
9148 len = (int)STRLEN(fnames[0]);
9149 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9151 /* For ":mkspell path/en.latin1.add" output file is
9152 * "path/en.latin1.add.spl". */
9153 innames = &fnames[0];
9155 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
9157 else if (fcount == 1)
9159 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9160 innames = &fnames[0];
9162 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
9163 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9165 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9167 /* Name ends in ".spl", use as the file name. */
9168 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
9171 /* Name should be language, make the file name from it. */
9172 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
9173 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9175 /* Check for .ascii.spl. */
9176 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
9177 spin.si_ascii = TRUE;
9179 /* Check for .add.spl. */
9180 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
9185 EMSG(_(e_invarg)); /* need at least output and input names */
9186 else if (vim_strchr(gettail(wfname), '_') != NULL)
9187 EMSG(_("E751: Output file name must not have region name"));
9188 else if (incount > 8)
9189 EMSG(_("E754: Only up to 8 regions supported"));
9192 /* Check for overwriting before doing things that may take a lot of
9194 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
9199 if (mch_isdir(wfname))
9201 EMSG2(_(e_isadir2), wfname);
9205 fname = alloc(MAXPATHL);
9210 * Init the aff and dic pointers.
9211 * Get the region names if there are more than 2 arguments.
9213 for (i = 0; i < incount; ++i)
9219 len = (int)STRLEN(innames[i]);
9220 if (STRLEN(gettail(innames[i])) < 5
9221 || innames[i][len - 3] != '_')
9223 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9226 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9227 spin.si_region_name[i * 2 + 1] =
9228 TOLOWER_ASC(innames[i][len - 1]);
9231 spin.si_region_count = incount;
9233 spin.si_foldroot = wordtree_alloc(&spin);
9234 spin.si_keeproot = wordtree_alloc(&spin);
9235 spin.si_prefroot = wordtree_alloc(&spin);
9236 if (spin.si_foldroot == NULL
9237 || spin.si_keeproot == NULL
9238 || spin.si_prefroot == NULL)
9240 free_blocks(spin.si_blocks);
9244 /* When not producing a .add.spl file clear the character table when
9245 * we encounter one in the .aff file. This means we dump the current
9246 * one in the .spl file if the .aff file doesn't define one. That's
9247 * better than guessing the contents, the table will match a
9248 * previously loaded spell file. */
9250 spin.si_clear_chartab = TRUE;
9253 * Read all the .aff and .dic files.
9254 * Text is converted to 'encoding'.
9255 * Words are stored in the case-folded and keep-case trees.
9257 for (i = 0; i < incount && !error; ++i)
9259 spin.si_conv.vc_type = CONV_NONE;
9260 spin.si_region = 1 << i;
9262 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
9263 if (mch_stat((char *)fname, &st) >= 0)
9265 /* Read the .aff file. Will init "spin->si_conv" based on the
9267 afile[i] = spell_read_aff(&spin, fname);
9268 if (afile[i] == NULL)
9272 /* Read the .dic file and store the words in the trees. */
9273 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
9275 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
9281 /* No .aff file, try reading the file as a word list. Store
9282 * the words in the trees. */
9283 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
9288 /* Free any conversion stuff. */
9289 convert_setup(&spin.si_conv, NULL, NULL);
9293 if (spin.si_compflags != NULL && spin.si_nobreak)
9294 MSG(_("Warning: both compounding and NOBREAK specified"));
9296 if (!error && !got_int)
9299 * Combine tails in the tree.
9301 spell_message(&spin, (char_u *)_(msg_compressing));
9302 wordtree_compress(&spin, spin.si_foldroot);
9303 wordtree_compress(&spin, spin.si_keeproot);
9304 wordtree_compress(&spin, spin.si_prefroot);
9307 if (!error && !got_int)
9310 * Write the info in the spell file.
9312 vim_snprintf((char *)IObuff, IOSIZE,
9313 _("Writing spell file %s ..."), wfname);
9314 spell_message(&spin, IObuff);
9316 error = write_vim_spell(&spin, wfname) == FAIL;
9318 spell_message(&spin, (char_u *)_("Done!"));
9319 vim_snprintf((char *)IObuff, IOSIZE,
9320 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9321 spell_message(&spin, IObuff);
9324 * If the file is loaded need to reload it.
9327 spell_reload_one(wfname, added_word);
9330 /* Free the allocated memory. */
9331 ga_clear(&spin.si_rep);
9332 ga_clear(&spin.si_repsal);
9333 ga_clear(&spin.si_sal);
9334 ga_clear(&spin.si_map);
9335 ga_clear(&spin.si_comppat);
9336 ga_clear(&spin.si_prefcond);
9337 hash_clear_all(&spin.si_commonwords, 0);
9339 /* Free the .aff file structures. */
9340 for (i = 0; i < incount; ++i)
9341 if (afile[i] != NULL)
9342 spell_free_aff(afile[i]);
9344 /* Free all the bits and pieces at once. */
9345 free_blocks(spin.si_blocks);
9348 * If there is soundfolding info and no NOSUGFILE item create the
9349 * .sug file with the soundfolded word trie.
9351 if (spin.si_sugtime != 0 && !error && !got_int)
9352 spell_make_sugfile(&spin, wfname);
9362 * Display a message for spell file processing when 'verbose' is set or using
9363 * ":mkspell". "str" can be IObuff.
9366 spell_message(spin, str)
9370 if (spin->si_verbose || p_verbose > 2)
9372 if (!spin->si_verbose)
9376 if (!spin->si_verbose)
9382 * ":[count]spellgood {word}"
9383 * ":[count]spellwrong {word}"
9384 * ":[count]spellundo {word}"
9390 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
9391 eap->forceit ? 0 : (int)eap->line2,
9392 eap->cmdidx == CMD_spellundo);
9396 * Add "word[len]" to 'spellfile' as a good or bad word.
9399 spell_add_word(word, len, bad, idx, undo)
9403 int idx; /* "zG" and "zW": zero, otherwise index in
9405 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
9409 int new_spf = FALSE;
9411 char_u *fnamebuf = NULL;
9412 char_u line[MAXWLEN * 2];
9413 long fpos, fpos_next = 0;
9417 if (idx == 0) /* use internal wordlist */
9419 if (int_wordlist == NULL)
9421 int_wordlist = vim_tempname('s');
9422 if (int_wordlist == NULL)
9425 fname = int_wordlist;
9429 /* If 'spellfile' isn't set figure out a good default value. */
9430 if (*curwin->w_s->b_p_spf == NUL)
9436 if (*curwin->w_s->b_p_spf == NUL)
9438 EMSG2(_(e_notset), "spellfile");
9441 fnamebuf = alloc(MAXPATHL);
9442 if (fnamebuf == NULL)
9445 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
9447 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
9452 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
9458 /* Check that the user isn't editing the .add file somewhere. */
9459 buf = buflist_findname_exp(fnamebuf);
9460 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9462 if (buf != NULL && bufIsChanged(buf))
9464 EMSG(_(e_bufloaded));
9474 /* When the word appears as good word we need to remove that one,
9475 * since its flags sort before the one with WF_BANNED. */
9476 fd = mch_fopen((char *)fname, "r");
9479 while (!vim_fgets(line, MAXWLEN * 2, fd))
9482 fpos_next = ftell(fd);
9483 if (STRNCMP(word, line, len) == 0
9484 && (line[len] == '/' || line[len] < ' '))
9486 /* Found duplicate word. Remove it by writing a '#' at
9487 * the start of the line. Mixing reading and writing
9488 * doesn't work for all systems, close the file first. */
9490 fd = mch_fopen((char *)fname, "r+");
9493 if (fseek(fd, fpos, SEEK_SET) == 0)
9498 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9499 smsg((char_u *)_("Word removed from %s"), NameBuff);
9502 fseek(fd, fpos_next, SEEK_SET);
9512 fd = mch_fopen((char *)fname, "a");
9513 if (fd == NULL && new_spf)
9517 /* We just initialized the 'spellfile' option and can't open the
9518 * file. We may need to create the "spell" directory first. We
9519 * already checked the runtime directory is writable in
9520 * init_spellfile(). */
9521 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
9525 /* The directory doesn't exist. Try creating it and opening
9526 * the file again. */
9528 vim_mkdir(fname, 0755);
9530 fd = mch_fopen((char *)fname, "a");
9535 EMSG2(_(e_notopen), fname);
9539 fprintf(fd, "%.*s/!\n", len, word);
9541 fprintf(fd, "%.*s\n", len, word);
9544 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9545 smsg((char_u *)_("Word added to %s"), NameBuff);
9551 /* Update the .add.spl file. */
9552 mkspell(1, &fname, FALSE, TRUE, TRUE);
9554 /* If the .add file is edited somewhere, reload it. */
9556 buf_reload(buf, buf->b_orig_mode);
9558 redraw_all_later(SOME_VALID);
9564 * Initialize 'spellfile' for the current buffer.
9575 char_u *lstart = curbuf->b_s.b_p_spl;
9577 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
9579 buf = alloc(MAXPATHL);
9583 /* Find the end of the language name. Exclude the region. If there
9584 * is a path separator remember the start of the tail. */
9585 for (lend = curwin->w_s->b_p_spl; *lend != NUL
9586 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
9587 if (vim_ispathsep(*lend))
9593 /* Loop over all entries in 'runtimepath'. Use the first one where we
9594 * are allowed to write. */
9599 /* Use directory of an entry with path, e.g., for
9600 * "/dir/lg.utf-8.spl" use "/dir". */
9601 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9602 lstart - curbuf->b_s.b_p_spl - 1);
9604 /* Copy the path from 'runtimepath' to buf[]. */
9605 copy_option_part(&rtp, buf, MAXPATHL, ",");
9606 if (filewritable(buf) == 2)
9608 /* Use the first language name from 'spelllang' and the
9609 * encoding used in the first loaded .spl file. */
9611 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9612 lend - curbuf->b_s.b_p_spl);
9615 /* Create the "spell" directory if it doesn't exist yet. */
9616 l = (int)STRLEN(buf);
9617 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9618 if (filewritable(buf) != 2)
9619 vim_mkdir(buf, 0755);
9621 l = (int)STRLEN(buf);
9622 vim_snprintf((char *)buf + l, MAXPATHL - l,
9623 "/%.*s", (int)(lend - lstart), lstart);
9625 l = (int)STRLEN(buf);
9626 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
9627 ->lp_slang->sl_fname;
9628 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9630 && strstr((char *)gettail(fname), ".ascii.") != NULL
9631 ? (char_u *)"ascii" : spell_enc());
9632 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9644 * Init the chartab used for spelling for ASCII.
9645 * EBCDIC is not supported!
9648 clear_spell_chartab(sp)
9653 /* Init everything to FALSE. */
9654 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9655 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9656 for (i = 0; i < 256; ++i)
9659 sp->st_upper[i] = i;
9662 /* We include digits. A word shouldn't start with a digit, but handling
9663 * that is done separately. */
9664 for (i = '0'; i <= '9'; ++i)
9665 sp->st_isw[i] = TRUE;
9666 for (i = 'A'; i <= 'Z'; ++i)
9668 sp->st_isw[i] = TRUE;
9669 sp->st_isu[i] = TRUE;
9670 sp->st_fold[i] = i + 0x20;
9672 for (i = 'a'; i <= 'z'; ++i)
9674 sp->st_isw[i] = TRUE;
9675 sp->st_upper[i] = i - 0x20;
9680 * Init the chartab used for spelling. Only depends on 'encoding'.
9681 * Called once while starting up and when 'encoding' changes.
9682 * The default is to use isalpha(), but the spell file should define the word
9683 * characters to make it possible that 'encoding' differs from the current
9684 * locale. For utf-8 we don't use isalpha() but our own functions.
9687 init_spell_chartab()
9691 did_set_spelltab = FALSE;
9692 clear_spell_chartab(&spelltab);
9696 /* DBCS: assume double-wide characters are word characters. */
9697 for (i = 128; i <= 255; ++i)
9698 if (MB_BYTE2LEN(i) == 2)
9699 spelltab.st_isw[i] = TRUE;
9703 for (i = 128; i < 256; ++i)
9705 int f = utf_fold(i);
9706 int u = utf_toupper(i);
9708 spelltab.st_isu[i] = utf_isupper(i);
9709 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9710 /* The folded/upper-cased value is different between latin1 and
9711 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9712 * value for utf-8 to avoid this. */
9713 spelltab.st_fold[i] = (f < 256) ? f : i;
9714 spelltab.st_upper[i] = (u < 256) ? u : i;
9720 /* Rough guess: use locale-dependent library functions. */
9721 for (i = 128; i < 256; ++i)
9725 spelltab.st_isw[i] = TRUE;
9726 spelltab.st_isu[i] = TRUE;
9727 spelltab.st_fold[i] = MB_TOLOWER(i);
9729 else if (MB_ISLOWER(i))
9731 spelltab.st_isw[i] = TRUE;
9732 spelltab.st_upper[i] = MB_TOUPPER(i);
9739 * Set the spell character tables from strings in the affix file.
9742 set_spell_chartab(fol, low, upp)
9747 /* We build the new tables here first, so that we can compare with the
9750 char_u *pf = fol, *pl = low, *pu = upp;
9753 clear_spell_chartab(&new_st);
9757 if (*pl == NUL || *pu == NUL)
9763 f = mb_ptr2char_adv(&pf);
9764 l = mb_ptr2char_adv(&pl);
9765 u = mb_ptr2char_adv(&pu);
9771 /* Every character that appears is a word character. */
9773 new_st.st_isw[f] = TRUE;
9775 new_st.st_isw[l] = TRUE;
9777 new_st.st_isw[u] = TRUE;
9779 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9781 if (l < 256 && l != f)
9785 EMSG(_(e_affrange));
9788 new_st.st_fold[l] = f;
9791 /* if "UPP" and "FOL" are not the same the "UPP" char needs
9792 * case-folding, it's upper case and the "UPP" is the upper case of
9794 if (u < 256 && u != f)
9798 EMSG(_(e_affrange));
9801 new_st.st_fold[u] = f;
9802 new_st.st_isu[u] = TRUE;
9803 new_st.st_upper[f] = u;
9807 if (*pl != NUL || *pu != NUL)
9813 return set_spell_finish(&new_st);
9817 * Set the spell character tables from strings in the .spl file.
9820 set_spell_charflags(flags, cnt, fol)
9822 int cnt; /* length of "flags" */
9825 /* We build the new tables here first, so that we can compare with the
9832 clear_spell_chartab(&new_st);
9834 for (i = 0; i < 128; ++i)
9838 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9839 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9845 c = mb_ptr2char_adv(&p);
9849 new_st.st_fold[i + 128] = c;
9850 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9851 new_st.st_upper[c] = i + 128;
9855 (void)set_spell_finish(&new_st);
9859 set_spell_finish(new_st)
9864 if (did_set_spelltab)
9866 /* check that it's the same table */
9867 for (i = 0; i < 256; ++i)
9869 if (spelltab.st_isw[i] != new_st->st_isw[i]
9870 || spelltab.st_isu[i] != new_st->st_isu[i]
9871 || spelltab.st_fold[i] != new_st->st_fold[i]
9872 || spelltab.st_upper[i] != new_st->st_upper[i])
9874 EMSG(_("E763: Word characters differ between spell files"));
9881 /* copy the new spelltab into the one being used */
9883 did_set_spelltab = TRUE;
9890 * Return TRUE if "p" points to a word character.
9891 * As a special case we see "midword" characters as word character when it is
9892 * followed by a word character. This finds they'there but not 'they there'.
9893 * Thus this only works properly when past the first character of the word.
9896 spell_iswordp(p, wp)
9898 win_T *wp; /* buffer used */
9907 l = MB_BYTE2LEN(*p);
9911 /* be quick for ASCII */
9912 if (wp->w_s->b_spell_ismw[*p])
9913 s = p + 1; /* skip a mid-word character */
9918 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9919 : (wp->w_s->b_spell_ismw_mb != NULL
9920 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
9926 return spell_mb_isword_class(mb_get_class(s));
9927 return spelltab.st_isw[c];
9931 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
9935 * Return TRUE if "p" points to a word character.
9936 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9939 spell_iswordp_nmw(p)
9949 return spell_mb_isword_class(mb_get_class(p));
9950 return spelltab.st_isw[c];
9953 return spelltab.st_isw[*p];
9958 * Return TRUE if word class indicates a word character.
9959 * Only for characters above 255.
9960 * Unicode subscript and superscript are not considered word characters.
9963 spell_mb_isword_class(cl)
9966 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9970 * Return TRUE if "p" points to a word character.
9971 * Wide version of spell_iswordp().
9974 spell_iswordp_w(p, wp)
9980 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9981 : (wp->w_s->b_spell_ismw_mb != NULL
9982 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
9990 return spell_mb_isword_class(utf_class(*s));
9992 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9995 return spelltab.st_isw[*s];
10000 * Write the table with prefix conditions to the .spl file.
10001 * When "fd" is NULL only count the length of what is written.
10004 write_spell_prefcond(fd, gap)
10012 size_t x = 1; /* collect return value of fwrite() */
10015 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
10017 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
10019 for (i = 0; i < gap->ga_len; ++i)
10021 /* <prefcond> : <condlen> <condstr> */
10022 p = ((char_u **)gap->ga_data)[i];
10025 len = (int)STRLEN(p);
10029 x &= fwrite(p, (size_t)len, (size_t)1, fd);
10033 else if (fd != NULL)
10041 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
10042 * Uses the character definitions from the .spl file.
10043 * When using a multi-byte 'encoding' the length may change!
10044 * Returns FAIL when something wrong.
10047 spell_casefold(str, len, buf, buflen)
10058 return FAIL; /* result will not fit */
10068 /* Fold one character at a time. */
10069 for (p = str; p < str + len; )
10071 if (outi + MB_MAXBYTES > buflen)
10076 c = mb_cptr2char_adv(&p);
10077 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
10084 /* Be quick for non-multibyte encodings. */
10085 for (i = 0; i < len; ++i)
10086 buf[i] = spelltab.st_fold[str[i]];
10093 /* values for sps_flags */
10096 #define SPS_DOUBLE 4
10098 static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10099 static int sps_limit = 9999; /* max nr of suggestions given */
10102 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
10103 * Sets "sps_flags" and "sps_limit".
10110 char_u buf[MAXPATHL];
10116 for (p = p_sps; *p != NUL; )
10118 copy_option_part(&p, buf, MAXPATHL, ",");
10121 if (VIM_ISDIGIT(*buf))
10124 sps_limit = getdigits(&s);
10125 if (*s != NUL && !VIM_ISDIGIT(*s))
10128 else if (STRCMP(buf, "best") == 0)
10130 else if (STRCMP(buf, "fast") == 0)
10132 else if (STRCMP(buf, "double") == 0)
10134 else if (STRNCMP(buf, "expr:", 5) != 0
10135 && STRNCMP(buf, "file:", 5) != 0)
10138 if (f == -1 || (sps_flags != 0 && f != 0))
10140 sps_flags = SPS_BEST;
10148 if (sps_flags == 0)
10149 sps_flags = SPS_BEST;
10155 * "z?": Find badly spelled word under or after the cursor.
10156 * Give suggestions for the properly spelled word.
10157 * In Visual mode use the highlighted word as the bad word.
10158 * When "count" is non-zero use that suggestion.
10161 spell_suggest(count)
10165 pos_T prev_cursor = curwin->w_cursor;
10166 char_u wcopy[MAXWLEN + 2];
10175 int selected = count;
10177 int msg_scroll_save = msg_scroll;
10179 if (no_spell_checking(curwin))
10185 /* Use the Visually selected text as the bad word. But reject
10186 * a multi-line selection. */
10187 if (curwin->w_cursor.lnum != VIsual.lnum)
10192 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10196 curwin->w_cursor.col = VIsual.col;
10202 /* Find the start of the badly spelled word. */
10203 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
10204 || curwin->w_cursor.col > prev_cursor.col)
10206 /* No bad word or it starts after the cursor: use the word under the
10208 curwin->w_cursor = prev_cursor;
10209 line = ml_get_curline();
10210 p = line + curwin->w_cursor.col;
10211 /* Backup to before start of word. */
10212 while (p > line && spell_iswordp_nmw(p))
10213 mb_ptr_back(line, p);
10214 /* Forward to start of word. */
10215 while (*p != NUL && !spell_iswordp_nmw(p))
10218 if (!spell_iswordp_nmw(p)) /* No word found. */
10223 curwin->w_cursor.col = (colnr_T)(p - line);
10226 /* Get the word and its length. */
10228 /* Figure out if the word should be capitalised. */
10229 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
10231 /* Make a copy of current line since autocommands may free the line. */
10232 line = vim_strsave(ml_get_curline());
10236 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10237 * 'spellsuggest', whatever is smaller. */
10238 if (sps_limit > (int)Rows - 2)
10239 limit = (int)Rows - 2;
10242 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
10243 TRUE, need_cap, TRUE);
10245 if (sug.su_ga.ga_len == 0)
10246 MSG(_("Sorry, no suggestions"));
10247 else if (count > 0)
10249 if (count > sug.su_ga.ga_len)
10250 smsg((char_u *)_("Sorry, only %ld suggestions"),
10251 (long)sug.su_ga.ga_len);
10255 vim_free(repl_from);
10260 #ifdef FEAT_RIGHTLEFT
10261 /* When 'rightleft' is set the list is drawn right-left. */
10262 cmdmsg_rl = curwin->w_p_rl;
10264 msg_col = Columns - 1;
10267 /* List the suggestions. */
10269 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
10270 lines_left = Rows; /* avoid more prompt */
10271 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10272 sug.su_badlen, sug.su_badptr);
10273 #ifdef FEAT_RIGHTLEFT
10274 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10276 /* And now the rabbit from the high hat: Avoid showing the
10277 * untranslated message rightleft. */
10278 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10279 sug.su_badlen, sug.su_badptr);
10287 for (i = 0; i < sug.su_ga.ga_len; ++i)
10289 stp = &SUG(sug.su_ga, i);
10291 /* The suggested word may replace only part of the bad word, add
10292 * the not replaced part. */
10293 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
10294 if (sug.su_badlen > stp->st_orglen)
10295 vim_strncpy(wcopy + stp->st_wordlen,
10296 sug.su_badptr + stp->st_orglen,
10297 sug.su_badlen - stp->st_orglen);
10298 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10299 #ifdef FEAT_RIGHTLEFT
10305 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
10308 /* The word may replace more than "su_badlen". */
10309 if (sug.su_badlen < stp->st_orglen)
10311 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10312 stp->st_orglen, sug.su_badptr);
10318 /* Add the score. */
10319 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
10320 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
10321 stp->st_salscore ? "s " : "",
10322 stp->st_score, stp->st_altscore);
10324 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
10326 #ifdef FEAT_RIGHTLEFT
10328 /* Mirror the numbers, but keep the leading space. */
10329 rl_mirror(IObuff + 1);
10337 #ifdef FEAT_RIGHTLEFT
10341 /* Ask for choice. */
10342 selected = prompt_for_number(&mouse_used);
10344 selected -= lines_left;
10345 lines_left = Rows; /* avoid more prompt */
10346 /* don't delay for 'smd' in normal_cmd() */
10347 msg_scroll = msg_scroll_save;
10350 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10352 /* Save the from and to text for :spellrepall. */
10353 stp = &SUG(sug.su_ga, selected - 1);
10354 if (sug.su_badlen > stp->st_orglen)
10356 /* Replacing less than "su_badlen", append the remainder to
10358 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10359 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10360 sug.su_badlen - stp->st_orglen,
10361 sug.su_badptr + stp->st_orglen);
10362 repl_to = vim_strsave(IObuff);
10366 /* Replacing su_badlen or more, use the whole word. */
10367 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10368 repl_to = vim_strsave(stp->st_word);
10371 /* Replace the word. */
10372 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10373 + stp->st_wordlen + 1);
10376 c = (int)(sug.su_badptr - line);
10377 mch_memmove(p, line, c);
10378 STRCPY(p + c, stp->st_word);
10379 STRCAT(p, sug.su_badptr + stp->st_orglen);
10380 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10381 curwin->w_cursor.col = c;
10383 /* For redo we use a change-word command. */
10385 AppendToRedobuff((char_u *)"ciw");
10386 AppendToRedobuffLit(p + c,
10387 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
10388 AppendCharToRedobuff(ESC);
10390 /* After this "p" may be invalid. */
10391 changed_bytes(curwin->w_cursor.lnum, c);
10395 curwin->w_cursor = prev_cursor;
10397 spell_find_cleanup(&sug);
10403 * Check if the word at line "lnum" column "col" is required to start with a
10404 * capital. This uses 'spellcapcheck' of the current buffer.
10407 check_need_cap(lnum, col)
10411 int need_cap = FALSE;
10413 char_u *line_copy = NULL;
10416 regmatch_T regmatch;
10418 if (curwin->w_s->b_cap_prog == NULL)
10421 line = ml_get_curline();
10423 if ((int)(skipwhite(line) - line) >= (int)col)
10425 /* At start of line, check if previous line is empty or sentence
10431 line = ml_get(lnum - 1);
10432 if (*skipwhite(line) == NUL)
10436 /* Append a space in place of the line break. */
10437 line_copy = concat_str(line, (char_u *)" ");
10439 endcol = (colnr_T)STRLEN(line);
10448 /* Check if sentence ends before the bad word. */
10449 regmatch.regprog = curwin->w_s->b_cap_prog;
10450 regmatch.rm_ic = FALSE;
10454 mb_ptr_back(line, p);
10455 if (p == line || spell_iswordp_nmw(p))
10457 if (vim_regexec(®match, p, 0)
10458 && regmatch.endp[0] == line + endcol)
10466 vim_free(line_copy);
10476 ex_spellrepall(eap)
10477 exarg_T *eap UNUSED;
10479 pos_T pos = curwin->w_cursor;
10484 int save_ws = p_ws;
10485 linenr_T prev_lnum = 0;
10487 if (repl_from == NULL || repl_to == NULL)
10489 EMSG(_("E752: No previous spell replacement"));
10492 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
10494 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
10495 if (frompat == NULL)
10497 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10502 curwin->w_cursor.lnum = 0;
10505 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
10506 || u_save_cursor() == FAIL)
10509 /* Only replace when the right word isn't there yet. This happens
10510 * when changing "etc" to "etc.". */
10511 line = ml_get_curline();
10512 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10513 repl_to, STRLEN(repl_to)) != 0)
10515 p = alloc((unsigned)STRLEN(line) + addlen + 1);
10518 mch_memmove(p, line, curwin->w_cursor.col);
10519 STRCPY(p + curwin->w_cursor.col, repl_to);
10520 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10521 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10522 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
10524 if (curwin->w_cursor.lnum != prev_lnum)
10527 prev_lnum = curwin->w_cursor.lnum;
10531 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
10535 curwin->w_cursor = pos;
10538 if (sub_nsubs == 0)
10539 EMSG2(_("E753: Not found: %s"), repl_from);
10545 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10546 * a list of allocated strings.
10549 spell_suggest_list(gap, word, maxcount, need_cap, interactive)
10552 int maxcount; /* maximum nr of suggestions */
10553 int need_cap; /* 'spellcapcheck' matched */
10561 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
10563 /* Make room in "gap". */
10564 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
10565 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
10567 for (i = 0; i < sug.su_ga.ga_len; ++i)
10569 stp = &SUG(sug.su_ga, i);
10571 /* The suggested word may replace only part of "word", add the not
10572 * replaced part. */
10573 wcopy = alloc(stp->st_wordlen
10574 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
10577 STRCPY(wcopy, stp->st_word);
10578 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10579 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10583 spell_find_cleanup(&sug);
10587 * Find spell suggestions for the word at the start of "badptr".
10588 * Return the suggestions in "su->su_ga".
10589 * The maximum number of suggestions is "maxcount".
10590 * Note: does use info for the current window.
10591 * This is based on the mechanisms of Aspell, but completely reimplemented.
10594 spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
10596 int badlen; /* length of bad word or 0 if unknown */
10599 int banbadword; /* don't include badword in suggestions */
10600 int need_cap; /* word should start with capital */
10603 hlf_T attr = HLF_COUNT;
10604 char_u buf[MAXPATHL];
10606 int do_combine = FALSE;
10609 static int expr_busy = FALSE;
10616 * Set the info in "*su".
10618 vim_memset(su, 0, sizeof(suginfo_T));
10619 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10620 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
10621 if (*badptr == NUL)
10623 hash_init(&su->su_banned);
10625 su->su_badptr = badptr;
10627 su->su_badlen = badlen;
10629 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
10630 su->su_maxcount = maxcount;
10631 su->su_maxscore = SCORE_MAXINIT;
10633 if (su->su_badlen >= MAXWLEN)
10634 su->su_badlen = MAXWLEN - 1; /* just in case */
10635 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10636 (void)spell_casefold(su->su_badptr, su->su_badlen,
10637 su->su_fbadword, MAXWLEN);
10638 /* get caps flags for bad word */
10639 su->su_badflags = badword_captype(su->su_badptr,
10640 su->su_badptr + su->su_badlen);
10642 su->su_badflags |= WF_ONECAP;
10644 /* Find the default language for sound folding. We simply use the first
10645 * one in 'spelllang' that supports sound folding. That's good for when
10646 * using multiple files for one language, it's not that bad when mixing
10647 * languages (e.g., "pl,en"). */
10648 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
10650 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
10651 if (lp->lp_sallang != NULL)
10653 su->su_sallang = lp->lp_sallang;
10658 /* Soundfold the bad word with the default sound folding, so that we don't
10659 * have to do this many times. */
10660 if (su->su_sallang != NULL)
10661 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10662 su->su_sal_badword);
10664 /* If the word is not capitalised and spell_check() doesn't consider the
10665 * word to be bad then it might need to be capitalised. Add a suggestion
10667 c = PTR2CHAR(su->su_badptr);
10668 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
10670 make_case_word(su->su_badword, buf, WF_ONECAP);
10671 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
10672 0, TRUE, su->su_sallang, FALSE);
10675 /* Ban the bad word itself. It may appear in another region. */
10677 add_banned(su, su->su_badword);
10679 /* Make a copy of 'spellsuggest', because the expression may change it. */
10680 sps_copy = vim_strsave(p_sps);
10681 if (sps_copy == NULL)
10684 /* Loop over the items in 'spellsuggest'. */
10685 for (p = sps_copy; *p != NUL; )
10687 copy_option_part(&p, buf, MAXPATHL, ",");
10689 if (STRNCMP(buf, "expr:", 5) == 0)
10692 /* Evaluate an expression. Skip this when called recursively,
10693 * when using spellsuggest() in the expression. */
10697 spell_suggest_expr(su, buf + 5);
10702 else if (STRNCMP(buf, "file:", 5) == 0)
10703 /* Use list of suggestions in a file. */
10704 spell_suggest_file(su, buf + 5);
10707 /* Use internal method. */
10708 spell_suggest_intern(su, interactive);
10709 if (sps_flags & SPS_DOUBLE)
10714 vim_free(sps_copy);
10717 /* Combine the two list of suggestions. This must be done last,
10718 * because sorting changes the order again. */
10724 * Find suggestions by evaluating expression "expr".
10727 spell_suggest_expr(su, expr)
10736 /* The work is split up in a few parts to avoid having to export
10738 * First evaluate the expression and get the resulting list. */
10739 list = eval_spell_expr(su->su_badword, expr);
10742 /* Loop over the items in the list. */
10743 for (li = list->lv_first; li != NULL; li = li->li_next)
10744 if (li->li_tv.v_type == VAR_LIST)
10746 /* Get the word and the score from the items. */
10747 score = get_spellword(li->li_tv.vval.v_list, &p);
10748 if (score >= 0 && score <= su->su_maxscore)
10749 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10750 score, 0, TRUE, su->su_sallang, FALSE);
10755 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10756 check_suggestions(su, &su->su_ga);
10757 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10762 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
10765 spell_suggest_file(su, fname)
10770 char_u line[MAXWLEN * 2];
10773 char_u cword[MAXWLEN];
10775 /* Open the file. */
10776 fd = mch_fopen((char *)fname, "r");
10779 EMSG2(_(e_notopen), fname);
10783 /* Read it line by line. */
10784 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10788 p = vim_strchr(line, '/');
10790 continue; /* No Tab found, just skip the line. */
10792 if (STRICMP(su->su_badword, line) == 0)
10794 /* Match! Isolate the good word, until CR or NL. */
10795 for (len = 0; p[len] >= ' '; ++len)
10799 /* If the suggestion doesn't have specific case duplicate the case
10800 * of the bad word. */
10801 if (captype(p, NULL) == 0)
10803 make_case_word(p, cword, su->su_badflags);
10807 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10808 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
10814 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10815 check_suggestions(su, &su->su_ga);
10816 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10820 * Find suggestions for the internal method indicated by "sps_flags".
10823 spell_suggest_intern(su, interactive)
10828 * Load the .sug file(s) that are available and not done yet.
10830 suggest_load_files();
10833 * 1. Try special cases, such as repeating a word: "the the" -> "the".
10835 * Set a maximum score to limit the combination of operations that is
10838 suggest_try_special(su);
10841 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10842 * from the .aff file and inserting a space (split the word).
10844 suggest_try_change(su);
10846 /* For the resulting top-scorers compute the sound-a-like score. */
10847 if (sps_flags & SPS_DOUBLE)
10848 score_comp_sal(su);
10851 * 3. Try finding sound-a-like words.
10853 if ((sps_flags & SPS_FAST) == 0)
10855 if (sps_flags & SPS_BEST)
10856 /* Adjust the word score for the suggestions found so far for how
10857 * they sounds like. */
10858 rescore_suggestions(su);
10861 * While going through the soundfold tree "su_maxscore" is the score
10862 * for the soundfold word, limits the changes that are being tried,
10863 * and "su_sfmaxscore" the rescored score, which is set by
10864 * cleanup_suggestions().
10865 * First find words with a small edit distance, because this is much
10866 * faster and often already finds the top-N suggestions. If we didn't
10867 * find many suggestions try again with a higher edit distance.
10868 * "sl_sounddone" is used to avoid doing the same word twice.
10870 suggest_try_soundalike_prep();
10871 su->su_maxscore = SCORE_SFMAX1;
10872 su->su_sfmaxscore = SCORE_MAXINIT * 3;
10873 suggest_try_soundalike(su);
10874 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10876 /* We didn't find enough matches, try again, allowing more
10877 * changes to the soundfold word. */
10878 su->su_maxscore = SCORE_SFMAX2;
10879 suggest_try_soundalike(su);
10880 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10882 /* Still didn't find enough matches, try again, allowing even
10883 * more changes to the soundfold word. */
10884 su->su_maxscore = SCORE_SFMAX3;
10885 suggest_try_soundalike(su);
10888 su->su_maxscore = su->su_sfmaxscore;
10889 suggest_try_soundalike_finish();
10892 /* When CTRL-C was hit while searching do show the results. Only clear
10893 * got_int when using a command, not for spellsuggest(). */
10895 if (interactive && got_int)
10901 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
10903 if (sps_flags & SPS_BEST)
10904 /* Adjust the word score for how it sounds like. */
10905 rescore_suggestions(su);
10907 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10908 check_suggestions(su, &su->su_ga);
10909 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10914 * Load the .sug files for languages that have one and weren't loaded yet.
10917 suggest_load_files()
10924 char_u buf[MAXWLEN];
10932 /* Do this for all languages that support sound folding. */
10933 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
10935 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
10936 slang = lp->lp_slang;
10937 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10939 /* Change ".spl" to ".sug" and open the file. When the file isn't
10940 * found silently skip it. Do set "sl_sugloaded" so that we
10941 * don't try again and again. */
10942 slang->sl_sugloaded = TRUE;
10944 dotp = vim_strrchr(slang->sl_fname, '.');
10945 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10947 STRCPY(dotp, ".sug");
10948 fd = mch_fopen((char *)slang->sl_fname, "r");
10953 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10955 for (i = 0; i < VIMSUGMAGICL; ++i)
10956 buf[i] = getc(fd); /* <fileID> */
10957 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10959 EMSG2(_("E778: This does not look like a .sug file: %s"),
10963 c = getc(fd); /* <versionnr> */
10964 if (c < VIMSUGVERSION)
10966 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
10970 else if (c > VIMSUGVERSION)
10972 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
10977 /* Check the timestamp, it must be exactly the same as the one in
10978 * the .spl file. Otherwise the word numbers won't match. */
10979 timestamp = get8ctime(fd); /* <timestamp> */
10980 if (timestamp != slang->sl_sugtime)
10982 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
10988 * <SUGWORDTREE>: <wordtree>
10989 * Read the trie with the soundfolded words.
10991 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10995 EMSG2(_("E782: error while reading .sug file: %s"),
10997 slang_clear_sug(slang);
11002 * <SUGTABLE>: <sugwcount> <sugline> ...
11004 * Read the table with word numbers. We use a file buffer for
11005 * this, because it's so much like a file with lines. Makes it
11006 * possible to swap the info and save on memory use.
11008 slang->sl_sugbuf = open_spellbuf();
11009 if (slang->sl_sugbuf == NULL)
11012 wcount = get4c(fd);
11016 /* Read all the wordnr lists into the buffer, one NUL terminated
11017 * list per line. */
11018 ga_init2(&ga, 1, 100);
11019 for (wordnr = 0; wordnr < wcount; ++wordnr)
11024 c = getc(fd); /* <sugline> */
11025 if (c < 0 || ga_grow(&ga, 1) == FAIL)
11027 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
11031 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
11032 ga.ga_data, ga.ga_len, TRUE) == FAIL)
11038 * Need to put word counts in the word tries, so that we can find
11039 * a word by its number.
11041 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
11042 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
11047 STRCPY(dotp, ".spl");
11054 * Fill in the wordcount fields for a trie.
11055 * Returns the total number of words.
11058 tree_count_words(byts, idxs)
11063 idx_T arridx[MAXWLEN];
11067 int wordcount[MAXWLEN];
11073 while (depth >= 0 && !got_int)
11075 if (curi[depth] > byts[arridx[depth]])
11077 /* Done all bytes at this node, go up one level. */
11078 idxs[arridx[depth]] = wordcount[depth];
11080 wordcount[depth - 1] += wordcount[depth];
11087 /* Do one more byte at this node. */
11088 n = arridx[depth] + curi[depth];
11094 /* End of word, count it. */
11095 ++wordcount[depth];
11097 /* Skip over any other NUL bytes (same word with different
11099 while (byts[n + 1] == 0)
11107 /* Normal char, go one level deeper to count the words. */
11109 arridx[depth] = idxs[n];
11111 wordcount[depth] = 0;
11118 * Free the info put in "*su" by spell_find_suggest().
11121 spell_find_cleanup(su)
11126 /* Free the suggestions. */
11127 for (i = 0; i < su->su_ga.ga_len; ++i)
11128 vim_free(SUG(su->su_ga, i).st_word);
11129 ga_clear(&su->su_ga);
11130 for (i = 0; i < su->su_sga.ga_len; ++i)
11131 vim_free(SUG(su->su_sga, i).st_word);
11132 ga_clear(&su->su_sga);
11134 /* Free the banned words. */
11135 hash_clear_all(&su->su_banned, 0);
11139 * Make a copy of "word", with the first letter upper or lower cased, to
11140 * "wcopy[MAXWLEN]". "word" must not be empty.
11141 * The result is NUL terminated.
11144 onecap_copy(word, wcopy, upper)
11147 int upper; /* TRUE: first letter made upper case */
11156 c = mb_cptr2char_adv(&p);
11161 c = SPELL_TOUPPER(c);
11163 c = SPELL_TOFOLD(c);
11166 l = mb_char2bytes(c, wcopy);
11173 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
11177 * Make a copy of "word" with all the letters upper cased into
11178 * "wcopy[MAXWLEN]". The result is NUL terminated.
11181 allcap_copy(word, wcopy)
11190 for (s = word; *s != NUL; )
11194 c = mb_cptr2char_adv(&s);
11200 /* We only change 0xdf to SS when we are certain latin1 is used. It
11201 * would cause weird errors in other 8-bit encodings. */
11202 if (enc_latin1like && c == 0xdf)
11205 if (d - wcopy >= MAXWLEN - 1)
11211 c = SPELL_TOUPPER(c);
11216 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11218 d += mb_char2bytes(c, d);
11223 if (d - wcopy >= MAXWLEN - 1)
11232 * Try finding suggestions by recognizing specific situations.
11235 suggest_try_special(su)
11241 char_u word[MAXWLEN];
11244 * Recognize a word that is repeated: "the the".
11246 p = skiptowhite(su->su_fbadword);
11247 len = p - su->su_fbadword;
11249 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11251 /* Include badflags: if the badword is onecap or allcap
11252 * use that for the goodword too: "The the" -> "The". */
11253 c = su->su_fbadword[len];
11254 su->su_fbadword[len] = NUL;
11255 make_case_word(su->su_fbadword, word, su->su_badflags);
11256 su->su_fbadword[len] = c;
11258 /* Give a soundalike score of 0, compute the score as if deleting one
11260 add_suggestion(su, &su->su_ga, word, su->su_badlen,
11261 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
11266 * Try finding suggestions by adding/removing/swapping letters.
11269 suggest_try_change(su)
11272 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
11278 /* We make a copy of the case-folded bad word, so that we can modify it
11279 * to find matches (esp. REP items). Append some more text, changing
11280 * chars after the bad word may help. */
11281 STRCPY(fword, su->su_fbadword);
11282 n = (int)STRLEN(fword);
11283 p = su->su_badptr + su->su_badlen;
11284 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
11286 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
11288 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
11290 /* If reloading a spell file fails it's still in the list but
11291 * everything has been cleared. */
11292 if (lp->lp_slang->sl_fbyts == NULL)
11295 /* Try it for this language. Will add possible suggestions. */
11296 suggest_trie_walk(su, lp, fword, FALSE);
11300 /* Check the maximum score, if we go over it we won't try this change. */
11301 #define TRY_DEEPER(su, stack, depth, add) \
11302 (stack[depth].ts_score + (add) < su->su_maxscore)
11305 * Try finding suggestions by adding/removing/swapping letters.
11307 * This uses a state machine. At each node in the tree we try various
11308 * operations. When trying if an operation works "depth" is increased and the
11309 * stack[] is used to store info. This allows combinations, thus insert one
11310 * character, replace one and delete another. The number of changes is
11311 * limited by su->su_maxscore.
11313 * After implementing this I noticed an article by Kemal Oflazer that
11314 * describes something similar: "Error-tolerant Finite State Recognition with
11315 * Applications to Morphological Analysis and Spelling Correction" (1996).
11316 * The implementation in the article is simplified and requires a stack of
11317 * unknown depth. The implementation here only needs a stack depth equal to
11318 * the length of the word.
11320 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11321 * The mechanism is the same, but we find a match with a sound-folded word
11322 * that comes from one or more original words. Each of these words may be
11323 * added, this is done by add_sound_suggest().
11325 * the prefix tree or the keep-case tree
11327 * anything to do with upper and lower case
11328 * anything to do with word or non-word characters ("spell_iswordp()")
11330 * word flags (rare, region, compounding)
11331 * word splitting for now
11332 * "similar_chars()"
11333 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11336 suggest_trie_walk(su, lp, fword, soundfold)
11342 char_u tword[MAXWLEN]; /* good word collected so far */
11343 trystate_T stack[MAXWLEN];
11344 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11345 * concatenation of prefix compound
11346 * words and split word. NUL terminated
11347 * when going deeper but not when coming
11349 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11353 char_u *byts, *fbyts, *pbyts;
11354 idx_T *idxs, *fidxs, *pidxs;
11365 int repextra = 0; /* extra bytes in fword[] from REP item */
11366 slang_T *slang = lp->lp_slang;
11369 #ifdef DEBUG_TRIEWALK
11370 /* Stores the name of the change made at each level. */
11371 char_u changename[MAXWLEN][80];
11373 int breakcheckcount = 1000;
11377 * Go through the whole case-fold tree, try changes at each node.
11378 * "tword[]" contains the word collected from nodes in the tree.
11379 * "fword[]" the word we are trying to match with (initially the bad
11384 vim_memset(sp, 0, sizeof(trystate_T));
11389 /* Going through the soundfold tree. */
11390 byts = fbyts = slang->sl_sbyts;
11391 idxs = fidxs = slang->sl_sidxs;
11394 sp->ts_prefixdepth = PFD_NOPREFIX;
11395 sp->ts_state = STATE_START;
11400 * When there are postponed prefixes we need to use these first. At
11401 * the end of the prefix we continue in the case-fold tree.
11403 fbyts = slang->sl_fbyts;
11404 fidxs = slang->sl_fidxs;
11405 pbyts = slang->sl_pbyts;
11406 pidxs = slang->sl_pidxs;
11411 sp->ts_prefixdepth = PFD_PREFIXTREE;
11412 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11418 sp->ts_prefixdepth = PFD_NOPREFIX;
11419 sp->ts_state = STATE_START;
11424 * Loop to find all suggestions. At each round we either:
11425 * - For the current state try one operation, advance "ts_curi",
11426 * increase "depth".
11427 * - When a state is done go to the next, set "ts_state".
11428 * - When all states are tried decrease "depth".
11430 while (depth >= 0 && !got_int)
11432 sp = &stack[depth];
11433 switch (sp->ts_state)
11436 case STATE_NOPREFIX:
11438 * Start of node: Deal with NUL bytes, which means
11439 * tword[] may end here.
11441 arridx = sp->ts_arridx; /* current node in the tree */
11442 len = byts[arridx]; /* bytes in this node */
11443 arridx += sp->ts_curi; /* index of current byte */
11445 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
11447 /* Skip over the NUL bytes, we use them later. */
11448 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11452 /* Always past NUL bytes now. */
11453 n = (int)sp->ts_state;
11454 sp->ts_state = STATE_ENDNUL;
11455 sp->ts_save_badflags = su->su_badflags;
11457 /* At end of a prefix or at start of prefixtree: check for
11458 * following word. */
11459 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
11461 /* Set su->su_badflags to the caps type at this position.
11462 * Use the caps type until here for the prefix itself. */
11465 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11469 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11470 su->su_badflags = badword_captype(su->su_badptr + n,
11471 su->su_badptr + su->su_badlen);
11472 #ifdef DEBUG_TRIEWALK
11473 sprintf(changename[depth], "prefix");
11475 go_deeper(stack, depth, 0);
11477 sp = &stack[depth];
11478 sp->ts_prefixdepth = depth - 1;
11483 /* Move the prefix to preword[] with the right case
11484 * and make find_keepcap_word() works. */
11485 tword[sp->ts_twordlen] = NUL;
11486 make_case_word(tword + sp->ts_splitoff,
11487 preword + sp->ts_prewordlen, flags);
11488 sp->ts_prewordlen = (char_u)STRLEN(preword);
11489 sp->ts_splitoff = sp->ts_twordlen;
11494 if (sp->ts_curi > len || byts[arridx] != 0)
11496 /* Past bytes in node and/or past NUL bytes. */
11497 sp->ts_state = STATE_ENDNUL;
11498 sp->ts_save_badflags = su->su_badflags;
11503 * End of word in tree.
11505 ++sp->ts_curi; /* eat one NUL byte */
11507 flags = (int)idxs[arridx];
11509 /* Skip words with the NOSUGGEST flag. */
11510 if (flags & WF_NOSUGGEST)
11513 fword_ends = (fword[sp->ts_fidx] == NUL
11515 ? vim_iswhite(fword[sp->ts_fidx])
11516 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
11517 tword[sp->ts_twordlen] = NUL;
11519 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
11520 && (sp->ts_flags & TSF_PREFIXOK) == 0)
11522 /* There was a prefix before the word. Check that the prefix
11523 * can be used with this word. */
11524 /* Count the length of the NULs in the prefix. If there are
11525 * none this must be the first try without a prefix. */
11526 n = stack[sp->ts_prefixdepth].ts_arridx;
11528 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11532 c = valid_word_prefix(c, n, flags,
11533 tword + sp->ts_splitoff, slang, FALSE);
11537 /* Use the WF_RARE flag for a rare prefix. */
11538 if (c & WF_RAREPFX)
11541 /* Tricky: when checking for both prefix and compounding
11542 * we run into the prefix flag first.
11543 * Remember that it's OK, so that we accept the prefix
11544 * when arriving at a compound flag. */
11545 sp->ts_flags |= TSF_PREFIXOK;
11549 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11550 * appending another compound word below. */
11551 if (sp->ts_complen == sp->ts_compsplit && fword_ends
11552 && (flags & WF_NEEDCOMP))
11553 goodword_ends = FALSE;
11555 goodword_ends = TRUE;
11558 compound_ok = TRUE;
11559 if (sp->ts_complen > sp->ts_compsplit)
11561 if (slang->sl_nobreak)
11563 /* There was a word before this word. When there was no
11564 * change in this word (it was correct) add the first word
11565 * as a suggestion. If this word was corrected too, we
11566 * need to check if a correct word follows. */
11567 if (sp->ts_fidx - sp->ts_splitfidx
11568 == sp->ts_twordlen - sp->ts_splitoff
11569 && STRNCMP(fword + sp->ts_splitfidx,
11570 tword + sp->ts_splitoff,
11571 sp->ts_fidx - sp->ts_splitfidx) == 0)
11573 preword[sp->ts_prewordlen] = NUL;
11574 newscore = score_wordcount_adj(slang, sp->ts_score,
11575 preword + sp->ts_prewordlen,
11576 sp->ts_prewordlen > 0);
11577 /* Add the suggestion if the score isn't too bad. */
11578 if (newscore <= su->su_maxscore)
11579 add_suggestion(su, &su->su_ga, preword,
11580 sp->ts_splitfidx - repextra,
11581 newscore, 0, FALSE,
11582 lp->lp_sallang, FALSE);
11588 /* There was a compound word before this word. If this
11589 * word does not support compounding then give up
11590 * (splitting is tried for the word without compound
11592 if (((unsigned)flags >> 24) == 0
11593 || sp->ts_twordlen - sp->ts_splitoff
11594 < slang->sl_compminlen)
11597 /* For multi-byte chars check character length against
11600 && slang->sl_compminlen > 0
11601 && mb_charlen(tword + sp->ts_splitoff)
11602 < slang->sl_compminlen)
11606 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11607 compflags[sp->ts_complen + 1] = NUL;
11608 vim_strncpy(preword + sp->ts_prewordlen,
11609 tword + sp->ts_splitoff,
11610 sp->ts_twordlen - sp->ts_splitoff);
11612 /* Verify CHECKCOMPOUNDPATTERN rules. */
11613 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11614 &slang->sl_comppat))
11615 compound_ok = FALSE;
11620 while (*skiptowhite(p) != NUL)
11621 p = skipwhite(skiptowhite(p));
11622 if (fword_ends && !can_compound(slang, p,
11623 compflags + sp->ts_compsplit))
11624 /* Compound is not allowed. But it may still be
11625 * possible if we add another (short) word. */
11626 compound_ok = FALSE;
11629 /* Get pointer to last char of previous word. */
11630 p = preword + sp->ts_prewordlen;
11631 mb_ptr_back(preword, p);
11636 * Form the word with proper case in preword.
11637 * If there is a word from a previous split, append.
11638 * For the soundfold tree don't change the case, simply append.
11641 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11642 else if (flags & WF_KEEPCAP)
11643 /* Must find the word in the keep-case tree. */
11644 find_keepcap_word(slang, tword + sp->ts_splitoff,
11645 preword + sp->ts_prewordlen);
11648 /* Include badflags: If the badword is onecap or allcap
11649 * use that for the goodword too. But if the badword is
11650 * allcap and it's only one char long use onecap. */
11651 c = su->su_badflags;
11652 if ((c & WF_ALLCAP)
11654 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11656 && su->su_badlen == 1
11662 /* When appending a compound word after a word character don't
11664 if (p != NULL && spell_iswordp_nmw(p))
11666 make_case_word(tword + sp->ts_splitoff,
11667 preword + sp->ts_prewordlen, c);
11672 /* Don't use a banned word. It may appear again as a good
11673 * word, thus remember it. */
11674 if (flags & WF_BANNED)
11676 add_banned(su, preword + sp->ts_prewordlen);
11679 if ((sp->ts_complen == sp->ts_compsplit
11680 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11681 || WAS_BANNED(su, preword))
11683 if (slang->sl_compprog == NULL)
11685 /* the word so far was banned but we may try compounding */
11686 goodword_ends = FALSE;
11691 if (!soundfold) /* soundfold words don't have flags */
11693 if ((flags & WF_REGION)
11694 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
11695 newscore += SCORE_REGION;
11696 if (flags & WF_RARE)
11697 newscore += SCORE_RARE;
11699 if (!spell_valid_case(su->su_badflags,
11700 captype(preword + sp->ts_prewordlen, NULL)))
11701 newscore += SCORE_ICASE;
11704 /* TODO: how about splitting in the soundfold tree? */
11707 && sp->ts_fidx >= sp->ts_fidxtry
11710 /* The badword also ends: add suggestions. */
11711 #ifdef DEBUG_TRIEWALK
11712 if (soundfold && STRCMP(preword, "smwrd") == 0)
11716 /* print the stack of changes that brought us here */
11717 smsg("------ %s -------", fword);
11718 for (j = 0; j < depth; ++j)
11719 smsg("%s", changename[j]);
11724 /* For soundfolded words we need to find the original
11725 * words, the edit distance and then add them. */
11726 add_sound_suggest(su, preword, sp->ts_score, lp);
11728 else if (sp->ts_fidx > 0)
11730 /* Give a penalty when changing non-word char to word
11731 * char, e.g., "thes," -> "these". */
11732 p = fword + sp->ts_fidx;
11733 mb_ptr_back(fword, p);
11734 if (!spell_iswordp(p, curwin))
11736 p = preword + STRLEN(preword);
11737 mb_ptr_back(preword, p);
11738 if (spell_iswordp(p, curwin))
11739 newscore += SCORE_NONWORD;
11742 /* Give a bonus to words seen before. */
11743 score = score_wordcount_adj(slang,
11744 sp->ts_score + newscore,
11745 preword + sp->ts_prewordlen,
11746 sp->ts_prewordlen > 0);
11748 /* Add the suggestion if the score isn't too bad. */
11749 if (score <= su->su_maxscore)
11751 add_suggestion(su, &su->su_ga, preword,
11752 sp->ts_fidx - repextra,
11753 score, 0, FALSE, lp->lp_sallang, FALSE);
11755 if (su->su_badflags & WF_MIXCAP)
11757 /* We really don't know if the word should be
11758 * upper or lower case, add both. */
11759 c = captype(preword, NULL);
11760 if (c == 0 || c == WF_ALLCAP)
11762 make_case_word(tword + sp->ts_splitoff,
11763 preword + sp->ts_prewordlen,
11764 c == 0 ? WF_ALLCAP : 0);
11766 add_suggestion(su, &su->su_ga, preword,
11767 sp->ts_fidx - repextra,
11768 score + SCORE_ICASE, 0, FALSE,
11769 lp->lp_sallang, FALSE);
11777 * Try word split and/or compounding.
11779 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
11781 /* Don't split halfway a character. */
11782 && (!has_mbyte || sp->ts_tcharlen == 0)
11789 /* If past the end of the bad word don't try a split.
11790 * Otherwise try changing the next word. E.g., find
11791 * suggestions for "the the" where the second "the" is
11792 * different. It's done like a split.
11793 * TODO: word split for soundfold words */
11794 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11797 /* Get here in several situations:
11798 * 1. The word in the tree ends:
11799 * If the word allows compounding try that. Otherwise try
11800 * a split by inserting a space. For both check that a
11801 * valid words starts at fword[sp->ts_fidx].
11802 * For NOBREAK do like compounding to be able to check if
11803 * the next word is valid.
11804 * 2. The badword does end, but it was due to a change (e.g.,
11805 * a swap). No need to split, but do check that the
11806 * following word is valid.
11807 * 3. The badword and the word in the tree end. It may still
11808 * be possible to compound another (short) word.
11810 try_compound = FALSE;
11812 && slang->sl_compprog != NULL
11813 && ((unsigned)flags >> 24) != 0
11814 && sp->ts_twordlen - sp->ts_splitoff
11815 >= slang->sl_compminlen
11818 || slang->sl_compminlen == 0
11819 || mb_charlen(tword + sp->ts_splitoff)
11820 >= slang->sl_compminlen)
11822 && (slang->sl_compsylmax < MAXWLEN
11823 || sp->ts_complen + 1 - sp->ts_compsplit
11824 < slang->sl_compmax)
11825 && (can_be_compound(sp, slang,
11826 compflags, ((unsigned)flags >> 24))))
11829 try_compound = TRUE;
11830 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11831 compflags[sp->ts_complen + 1] = NUL;
11834 /* For NOBREAK we never try splitting, it won't make any word
11836 if (slang->sl_nobreak)
11837 try_compound = TRUE;
11839 /* If we could add a compound word, and it's also possible to
11840 * split at this point, do the split first and set
11841 * TSF_DIDSPLIT to avoid doing it again. */
11842 else if (!fword_ends
11844 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11846 try_compound = FALSE;
11847 sp->ts_flags |= TSF_DIDSPLIT;
11848 --sp->ts_curi; /* do the same NUL again */
11849 compflags[sp->ts_complen] = NUL;
11852 sp->ts_flags &= ~TSF_DIDSPLIT;
11854 if (try_split || try_compound)
11856 if (!try_compound && (!fword_ends || !goodword_ends))
11858 /* If we're going to split need to check that the
11859 * words so far are valid for compounding. If there
11860 * is only one word it must not have the NEEDCOMPOUND
11862 if (sp->ts_complen == sp->ts_compsplit
11863 && (flags & WF_NEEDCOMP))
11866 while (*skiptowhite(p) != NUL)
11867 p = skipwhite(skiptowhite(p));
11868 if (sp->ts_complen > sp->ts_compsplit
11869 && !can_compound(slang, p,
11870 compflags + sp->ts_compsplit))
11873 if (slang->sl_nosplitsugs)
11874 newscore += SCORE_SPLIT_NO;
11876 newscore += SCORE_SPLIT;
11878 /* Give a bonus to words seen before. */
11879 newscore = score_wordcount_adj(slang, newscore,
11880 preword + sp->ts_prewordlen, TRUE);
11883 if (TRY_DEEPER(su, stack, depth, newscore))
11885 go_deeper(stack, depth, newscore);
11886 #ifdef DEBUG_TRIEWALK
11887 if (!try_compound && !fword_ends)
11888 sprintf(changename[depth], "%.*s-%s: split",
11889 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11891 sprintf(changename[depth], "%.*s-%s: compound",
11892 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11894 /* Save things to be restored at STATE_SPLITUNDO. */
11895 sp->ts_save_badflags = su->su_badflags;
11896 sp->ts_state = STATE_SPLITUNDO;
11899 sp = &stack[depth];
11901 /* Append a space to preword when splitting. */
11902 if (!try_compound && !fword_ends)
11903 STRCAT(preword, " ");
11904 sp->ts_prewordlen = (char_u)STRLEN(preword);
11905 sp->ts_splitoff = sp->ts_twordlen;
11906 sp->ts_splitfidx = sp->ts_fidx;
11908 /* If the badword has a non-word character at this
11909 * position skip it. That means replacing the
11910 * non-word character with a space. Always skip a
11911 * character when the word ends. But only when the
11912 * good word can end. */
11913 if (((!try_compound && !spell_iswordp_nmw(fword
11916 && fword[sp->ts_fidx] != NUL
11923 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
11929 /* Copy the skipped character to preword. */
11930 mch_memmove(preword + sp->ts_prewordlen,
11931 fword + sp->ts_fidx, l);
11932 sp->ts_prewordlen += l;
11933 preword[sp->ts_prewordlen] = NUL;
11936 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11940 /* When compounding include compound flag in
11941 * compflags[] (already set above). When splitting we
11942 * may start compounding over again. */
11946 sp->ts_compsplit = sp->ts_complen;
11947 sp->ts_prefixdepth = PFD_NOPREFIX;
11949 /* set su->su_badflags to the caps type at this
11953 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11957 su->su_badflags = badword_captype(su->su_badptr + n,
11958 su->su_badptr + su->su_badlen);
11960 /* Restart at top of the tree. */
11963 /* If there are postponed prefixes, try these too. */
11968 sp->ts_prefixdepth = PFD_PREFIXTREE;
11969 sp->ts_state = STATE_NOPREFIX;
11976 case STATE_SPLITUNDO:
11977 /* Undo the changes done for word split or compound word. */
11978 su->su_badflags = sp->ts_save_badflags;
11980 /* Continue looking for NUL bytes. */
11981 sp->ts_state = STATE_START;
11983 /* In case we went into the prefix tree. */
11989 /* Past the NUL bytes in the node. */
11990 su->su_badflags = sp->ts_save_badflags;
11991 if (fword[sp->ts_fidx] == NUL
11993 && sp->ts_tcharlen == 0
11997 /* The badword ends, can't use STATE_PLAIN. */
11998 sp->ts_state = STATE_DEL;
12001 sp->ts_state = STATE_PLAIN;
12006 * Go over all possible bytes at this node, add each to tword[]
12007 * and use child node. "ts_curi" is the index.
12009 arridx = sp->ts_arridx;
12010 if (sp->ts_curi > byts[arridx])
12012 /* Done all bytes at this node, do next state. When still at
12013 * already changed bytes skip the other tricks. */
12014 if (sp->ts_fidx >= sp->ts_fidxtry)
12015 sp->ts_state = STATE_DEL;
12017 sp->ts_state = STATE_FINAL;
12021 arridx += sp->ts_curi++;
12024 /* Normal byte, go one level deeper. If it's not equal to the
12025 * byte in the bad word adjust the score. But don't even try
12026 * when the byte was already changed. And don't try when we
12027 * just deleted this byte, accepting it is always cheaper then
12028 * delete + substitute. */
12029 if (c == fword[sp->ts_fidx]
12031 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
12036 newscore = SCORE_SUBST;
12038 || (sp->ts_fidx >= sp->ts_fidxtry
12039 && ((sp->ts_flags & TSF_DIDDEL) == 0
12040 || c != fword[sp->ts_delidx])))
12041 && TRY_DEEPER(su, stack, depth, newscore))
12043 go_deeper(stack, depth, newscore);
12044 #ifdef DEBUG_TRIEWALK
12046 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12047 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12048 fword[sp->ts_fidx], c);
12050 sprintf(changename[depth], "%.*s-%s: accept %c",
12051 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12052 fword[sp->ts_fidx]);
12055 sp = &stack[depth];
12057 tword[sp->ts_twordlen++] = c;
12058 sp->ts_arridx = idxs[arridx];
12060 if (newscore == SCORE_SUBST)
12061 sp->ts_isdiff = DIFF_YES;
12064 /* Multi-byte characters are a bit complicated to
12065 * handle: They differ when any of the bytes differ
12066 * and then their length may also differ. */
12067 if (sp->ts_tcharlen == 0)
12070 sp->ts_tcharidx = 0;
12071 sp->ts_tcharlen = MB_BYTE2LEN(c);
12072 sp->ts_fcharstart = sp->ts_fidx - 1;
12073 sp->ts_isdiff = (newscore != 0)
12074 ? DIFF_YES : DIFF_NONE;
12076 else if (sp->ts_isdiff == DIFF_INSERT)
12077 /* When inserting trail bytes don't advance in the
12080 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12082 /* Last byte of character. */
12083 if (sp->ts_isdiff == DIFF_YES)
12085 /* Correct ts_fidx for the byte length of the
12086 * character (we didn't check that before). */
12087 sp->ts_fidx = sp->ts_fcharstart
12089 fword[sp->ts_fcharstart]);
12091 /* For changing a composing character adjust
12092 * the score from SCORE_SUBST to
12093 * SCORE_SUBCOMP. */
12095 && utf_iscomposing(
12098 - sp->ts_tcharlen))
12099 && utf_iscomposing(
12101 + sp->ts_fcharstart)))
12103 SCORE_SUBST - SCORE_SUBCOMP;
12105 /* For a similar character adjust score from
12106 * SCORE_SUBST to SCORE_SIMILAR. */
12107 else if (!soundfold
12108 && slang->sl_has_map
12109 && similar_chars(slang,
12112 - sp->ts_tcharlen),
12114 + sp->ts_fcharstart)))
12116 SCORE_SUBST - SCORE_SIMILAR;
12118 else if (sp->ts_isdiff == DIFF_INSERT
12119 && sp->ts_twordlen > sp->ts_tcharlen)
12121 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12122 c = mb_ptr2char(p);
12123 if (enc_utf8 && utf_iscomposing(c))
12125 /* Inserting a composing char doesn't
12126 * count that much. */
12127 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12131 /* If the previous character was the same,
12132 * thus doubling a character, give a bonus
12133 * to the score. Also for the soundfold
12134 * tree (might seem illogical but does
12135 * give better scores). */
12136 mb_ptr_back(tword, p);
12137 if (c == mb_ptr2char(p))
12138 sp->ts_score -= SCORE_INS
12143 /* Starting a new char, reset the length. */
12144 sp->ts_tcharlen = 0;
12150 /* If we found a similar char adjust the score.
12151 * We do this after calling go_deeper() because
12155 && slang->sl_has_map
12156 && similar_chars(slang,
12157 c, fword[sp->ts_fidx - 1]))
12158 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
12166 /* When past the first byte of a multi-byte char don't try
12167 * delete/insert/swap a character. */
12168 if (has_mbyte && sp->ts_tcharlen > 0)
12170 sp->ts_state = STATE_FINAL;
12175 * Try skipping one character in the bad word (delete it).
12177 sp->ts_state = STATE_INS_PREP;
12179 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12180 /* Deleting a vowel at the start of a word counts less, see
12181 * soundalike_score(). */
12182 newscore = 2 * SCORE_DEL / 3;
12184 newscore = SCORE_DEL;
12185 if (fword[sp->ts_fidx] != NUL
12186 && TRY_DEEPER(su, stack, depth, newscore))
12188 go_deeper(stack, depth, newscore);
12189 #ifdef DEBUG_TRIEWALK
12190 sprintf(changename[depth], "%.*s-%s: delete %c",
12191 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12192 fword[sp->ts_fidx]);
12196 /* Remember what character we deleted, so that we can avoid
12197 * inserting it again. */
12198 stack[depth].ts_flags |= TSF_DIDDEL;
12199 stack[depth].ts_delidx = sp->ts_fidx;
12201 /* Advance over the character in fword[]. Give a bonus to the
12202 * score if the same character is following "nn" -> "n". It's
12203 * a bit illogical for soundfold tree but it does give better
12208 c = mb_ptr2char(fword + sp->ts_fidx);
12209 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12210 if (enc_utf8 && utf_iscomposing(c))
12211 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12212 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12213 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12218 ++stack[depth].ts_fidx;
12219 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12220 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12226 case STATE_INS_PREP:
12227 if (sp->ts_flags & TSF_DIDDEL)
12229 /* If we just deleted a byte then inserting won't make sense,
12230 * a substitute is always cheaper. */
12231 sp->ts_state = STATE_SWAP;
12235 /* skip over NUL bytes */
12239 if (sp->ts_curi > byts[n])
12241 /* Only NUL bytes at this node, go to next state. */
12242 sp->ts_state = STATE_SWAP;
12245 if (byts[n + sp->ts_curi] != NUL)
12247 /* Found a byte to insert. */
12248 sp->ts_state = STATE_INS;
12258 /* Insert one byte. Repeat this for each possible byte at this
12261 if (sp->ts_curi > byts[n])
12263 /* Done all bytes at this node, go to next state. */
12264 sp->ts_state = STATE_SWAP;
12268 /* Do one more byte at this node, but:
12269 * - Skip NUL bytes.
12270 * - Skip the byte if it's equal to the byte in the word,
12271 * accepting that byte is always better.
12273 n += sp->ts_curi++;
12275 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12276 /* Inserting a vowel at the start of a word counts less,
12277 * see soundalike_score(). */
12278 newscore = 2 * SCORE_INS / 3;
12280 newscore = SCORE_INS;
12281 if (c != fword[sp->ts_fidx]
12282 && TRY_DEEPER(su, stack, depth, newscore))
12284 go_deeper(stack, depth, newscore);
12285 #ifdef DEBUG_TRIEWALK
12286 sprintf(changename[depth], "%.*s-%s: insert %c",
12287 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12291 sp = &stack[depth];
12292 tword[sp->ts_twordlen++] = c;
12293 sp->ts_arridx = idxs[n];
12297 fl = MB_BYTE2LEN(c);
12300 /* There are following bytes for the same character.
12301 * We must find all bytes before trying
12302 * delete/insert/swap/etc. */
12303 sp->ts_tcharlen = fl;
12304 sp->ts_tcharidx = 1;
12305 sp->ts_isdiff = DIFF_INSERT;
12313 /* If the previous character was the same, thus doubling a
12314 * character, give a bonus to the score. Also for
12315 * soundfold words (illogical but does give a better
12317 if (sp->ts_twordlen >= 2
12318 && tword[sp->ts_twordlen - 2] == c)
12319 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
12326 * Swap two bytes in the bad word: "12" -> "21".
12327 * We change "fword" here, it's changed back afterwards at
12330 p = fword + sp->ts_fidx;
12334 /* End of word, can't swap or replace. */
12335 sp->ts_state = STATE_FINAL;
12339 /* Don't swap if the first character is not a word character.
12340 * SWAP3 etc. also don't make sense then. */
12341 if (!soundfold && !spell_iswordp(p, curwin))
12343 sp->ts_state = STATE_REP_INI;
12350 n = mb_cptr2len(p);
12351 c = mb_ptr2char(p);
12354 else if (!soundfold && !spell_iswordp(p + n, curwin))
12355 c2 = c; /* don't swap non-word char */
12357 c2 = mb_ptr2char(p + n);
12364 else if (!soundfold && !spell_iswordp(p + 1, curwin))
12365 c2 = c; /* don't swap non-word char */
12370 /* When the second character is NUL we can't swap. */
12373 sp->ts_state = STATE_REP_INI;
12377 /* When characters are identical, swap won't do anything.
12378 * Also get here if the second char is not a word character. */
12381 sp->ts_state = STATE_SWAP3;
12384 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12386 go_deeper(stack, depth, SCORE_SWAP);
12387 #ifdef DEBUG_TRIEWALK
12388 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12389 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12392 sp->ts_state = STATE_UNSWAP;
12397 fl = mb_char2len(c2);
12398 mch_memmove(p, p + n, fl);
12399 mb_char2bytes(c, p + fl);
12400 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
12407 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
12411 /* If this swap doesn't work then SWAP3 won't either. */
12412 sp->ts_state = STATE_REP_INI;
12416 /* Undo the STATE_SWAP swap: "21" -> "12". */
12417 p = fword + sp->ts_fidx;
12421 n = MB_BYTE2LEN(*p);
12422 c = mb_ptr2char(p + n);
12423 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12424 mb_char2bytes(c, p);
12436 /* Swap two bytes, skipping one: "123" -> "321". We change
12437 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12438 p = fword + sp->ts_fidx;
12442 n = mb_cptr2len(p);
12443 c = mb_ptr2char(p);
12444 fl = mb_cptr2len(p + n);
12445 c2 = mb_ptr2char(p + n);
12446 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
12447 c3 = c; /* don't swap non-word char */
12449 c3 = mb_ptr2char(p + n + fl);
12456 if (!soundfold && !spell_iswordp(p + 2, curwin))
12457 c3 = c; /* don't swap non-word char */
12462 /* When characters are identical: "121" then SWAP3 result is
12463 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12464 * same as SWAP on next char: "112". Thus skip all swapping.
12465 * Also skip when c3 is NUL.
12466 * Also get here when the third character is not a word character.
12467 * Second character may any char: "a.b" -> "b.a" */
12468 if (c == c3 || c3 == NUL)
12470 sp->ts_state = STATE_REP_INI;
12473 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12475 go_deeper(stack, depth, SCORE_SWAP3);
12476 #ifdef DEBUG_TRIEWALK
12477 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12478 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12481 sp->ts_state = STATE_UNSWAP3;
12486 tl = mb_char2len(c3);
12487 mch_memmove(p, p + n + fl, tl);
12488 mb_char2bytes(c2, p + tl);
12489 mb_char2bytes(c, p + fl + tl);
12490 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12497 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12501 sp->ts_state = STATE_REP_INI;
12504 case STATE_UNSWAP3:
12505 /* Undo STATE_SWAP3: "321" -> "123" */
12506 p = fword + sp->ts_fidx;
12510 n = MB_BYTE2LEN(*p);
12511 c2 = mb_ptr2char(p + n);
12512 fl = MB_BYTE2LEN(p[n]);
12513 c = mb_ptr2char(p + n + fl);
12514 tl = MB_BYTE2LEN(p[n + fl]);
12515 mch_memmove(p + fl + tl, p, n);
12516 mb_char2bytes(c, p);
12517 mb_char2bytes(c2, p + tl);
12529 if (!soundfold && !spell_iswordp(p, curwin))
12531 /* Middle char is not a word char, skip the rotate. First and
12532 * third char were already checked at swap and swap3. */
12533 sp->ts_state = STATE_REP_INI;
12537 /* Rotate three characters left: "123" -> "231". We change
12538 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12539 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12541 go_deeper(stack, depth, SCORE_SWAP3);
12542 #ifdef DEBUG_TRIEWALK
12543 p = fword + sp->ts_fidx;
12544 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12545 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12548 sp->ts_state = STATE_UNROT3L;
12550 p = fword + sp->ts_fidx;
12554 n = mb_cptr2len(p);
12555 c = mb_ptr2char(p);
12556 fl = mb_cptr2len(p + n);
12557 fl += mb_cptr2len(p + n + fl);
12558 mch_memmove(p, p + n, fl);
12559 mb_char2bytes(c, p + fl);
12560 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
12569 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12573 sp->ts_state = STATE_REP_INI;
12576 case STATE_UNROT3L:
12577 /* Undo ROT3L: "231" -> "123" */
12578 p = fword + sp->ts_fidx;
12582 n = MB_BYTE2LEN(*p);
12583 n += MB_BYTE2LEN(p[n]);
12584 c = mb_ptr2char(p + n);
12585 tl = MB_BYTE2LEN(p[n]);
12586 mch_memmove(p + tl, p, n);
12587 mb_char2bytes(c, p);
12598 /* Rotate three bytes right: "123" -> "312". We change "fword"
12599 * here, it's changed back afterwards at STATE_UNROT3R. */
12600 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12602 go_deeper(stack, depth, SCORE_SWAP3);
12603 #ifdef DEBUG_TRIEWALK
12604 p = fword + sp->ts_fidx;
12605 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12606 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12609 sp->ts_state = STATE_UNROT3R;
12611 p = fword + sp->ts_fidx;
12615 n = mb_cptr2len(p);
12616 n += mb_cptr2len(p + n);
12617 c = mb_ptr2char(p + n);
12618 tl = mb_cptr2len(p + n);
12619 mch_memmove(p + tl, p, n);
12620 mb_char2bytes(c, p);
12621 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
12630 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12634 sp->ts_state = STATE_REP_INI;
12637 case STATE_UNROT3R:
12638 /* Undo ROT3R: "312" -> "123" */
12639 p = fword + sp->ts_fidx;
12643 c = mb_ptr2char(p);
12644 tl = MB_BYTE2LEN(*p);
12645 n = MB_BYTE2LEN(p[tl]);
12646 n += MB_BYTE2LEN(p[tl + n]);
12647 mch_memmove(p, p + tl, n);
12648 mb_char2bytes(c, p + n);
12660 case STATE_REP_INI:
12661 /* Check if matching with REP items from the .aff file would work.
12663 * - there are no REP items and we are not in the soundfold trie
12664 * - the score is going to be too high anyway
12665 * - already applied a REP item or swapped here */
12666 if ((lp->lp_replang == NULL && !soundfold)
12667 || sp->ts_score + SCORE_REP >= su->su_maxscore
12668 || sp->ts_fidx < sp->ts_fidxtry)
12670 sp->ts_state = STATE_FINAL;
12674 /* Use the first byte to quickly find the first entry that may
12675 * match. If the index is -1 there is none. */
12677 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12679 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
12681 if (sp->ts_curi < 0)
12683 sp->ts_state = STATE_FINAL;
12687 sp->ts_state = STATE_REP;
12691 /* Try matching with REP items from the .aff file. For each match
12692 * replace the characters and check if the resulting word is
12694 p = fword + sp->ts_fidx;
12697 gap = &slang->sl_repsal;
12699 gap = &lp->lp_replang->sl_rep;
12700 while (sp->ts_curi < gap->ga_len)
12702 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12703 if (*ftp->ft_from != *p)
12705 /* past possible matching entries */
12706 sp->ts_curi = gap->ga_len;
12709 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12710 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12712 go_deeper(stack, depth, SCORE_REP);
12713 #ifdef DEBUG_TRIEWALK
12714 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12715 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12716 ftp->ft_from, ftp->ft_to);
12718 /* Need to undo this afterwards. */
12719 sp->ts_state = STATE_REP_UNDO;
12721 /* Change the "from" to the "to" string. */
12723 fl = (int)STRLEN(ftp->ft_from);
12724 tl = (int)STRLEN(ftp->ft_to);
12727 STRMOVE(p + tl, p + fl);
12728 repextra += tl - fl;
12730 mch_memmove(p, ftp->ft_to, tl);
12731 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12733 stack[depth].ts_tcharlen = 0;
12739 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12740 /* No (more) matches. */
12741 sp->ts_state = STATE_FINAL;
12745 case STATE_REP_UNDO:
12746 /* Undo a REP replacement and continue with the next one. */
12748 gap = &slang->sl_repsal;
12750 gap = &lp->lp_replang->sl_rep;
12751 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12752 fl = (int)STRLEN(ftp->ft_from);
12753 tl = (int)STRLEN(ftp->ft_to);
12754 p = fword + sp->ts_fidx;
12757 STRMOVE(p + fl, p + tl);
12758 repextra -= tl - fl;
12760 mch_memmove(p, ftp->ft_from, fl);
12761 sp->ts_state = STATE_REP;
12765 /* Did all possible states at this level, go up one level. */
12768 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12770 /* Continue in or go back to the prefix tree. */
12775 /* Don't check for CTRL-C too often, it takes time. */
12776 if (--breakcheckcount == 0)
12779 breakcheckcount = 1000;
12787 * Go one level deeper in the tree.
12790 go_deeper(stack, depth, score_add)
12795 stack[depth + 1] = stack[depth];
12796 stack[depth + 1].ts_state = STATE_START;
12797 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
12798 stack[depth + 1].ts_curi = 1; /* start just after length byte */
12799 stack[depth + 1].ts_flags = 0;
12804 * Case-folding may change the number of bytes: Count nr of chars in
12805 * fword[flen] and return the byte length of that many chars in "word".
12808 nofold_len(fword, flen, word)
12816 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12818 for (p = word; i > 0; mb_ptr_adv(p))
12820 return (int)(p - word);
12825 * "fword" is a good word with case folded. Find the matching keep-case
12826 * words and put it in "kword".
12827 * Theoretically there could be several keep-case words that result in the
12828 * same case-folded word, but we only find one...
12831 find_keepcap_word(slang, fword, kword)
12836 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12840 /* The following arrays are used at each depth in the tree. */
12841 idx_T arridx[MAXWLEN];
12842 int round[MAXWLEN];
12843 int fwordidx[MAXWLEN];
12844 int uwordidx[MAXWLEN];
12845 int kwordlen[MAXWLEN];
12853 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
12854 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
12858 /* array is empty: "cannot happen" */
12863 /* Make an all-cap version of "fword". */
12864 allcap_copy(fword, uword);
12867 * Each character needs to be tried both case-folded and upper-case.
12868 * All this gets very complicated if we keep in mind that changing case
12869 * may change the byte length of a multi-byte character...
12879 if (fword[fwordidx[depth]] == NUL)
12881 /* We are at the end of "fword". If the tree allows a word to end
12882 * here we have found a match. */
12883 if (byts[arridx[depth] + 1] == 0)
12885 kword[kwordlen[depth]] = NUL;
12889 /* kword is getting too long, continue one level up */
12892 else if (++round[depth] > 2)
12894 /* tried both fold-case and upper-case character, continue one
12901 * round[depth] == 1: Try using the folded-case character.
12902 * round[depth] == 2: Try using the upper-case character.
12907 flen = mb_cptr2len(fword + fwordidx[depth]);
12908 ulen = mb_cptr2len(uword + uwordidx[depth]);
12913 if (round[depth] == 1)
12915 p = fword + fwordidx[depth];
12920 p = uword + uwordidx[depth];
12924 for (tryidx = arridx[depth]; l > 0; --l)
12926 /* Perform a binary search in the list of accepted bytes. */
12927 len = byts[tryidx++];
12930 hi = tryidx + len - 1;
12936 else if (byts[m] < c)
12945 /* Stop if there is no matching byte. */
12946 if (hi < lo || byts[lo] != c)
12949 /* Continue at the child (if there is one). */
12956 * Found the matching char. Copy it to "kword" and go a
12959 if (round[depth] == 1)
12961 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12963 kwordlen[depth + 1] = kwordlen[depth] + flen;
12967 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12969 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12971 fwordidx[depth + 1] = fwordidx[depth] + flen;
12972 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12975 arridx[depth] = tryidx;
12981 /* Didn't find it: "cannot happen". */
12986 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12994 char_u badsound[MAXWLEN];
13001 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
13004 /* Use the sound-folding of the first language that supports it. */
13005 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
13007 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
13008 if (lp->lp_slang->sl_sal.ga_len > 0)
13010 /* soundfold the bad word */
13011 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
13013 for (i = 0; i < su->su_ga.ga_len; ++i)
13015 stp = &SUG(su->su_ga, i);
13017 /* Case-fold the suggested word, sound-fold it and compute the
13018 * sound-a-like score. */
13019 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
13020 if (score < SCORE_MAXMAX)
13022 /* Add the suggestion. */
13023 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
13024 sstp->st_word = vim_strsave(stp->st_word);
13025 if (sstp->st_word != NULL)
13027 sstp->st_wordlen = stp->st_wordlen;
13028 sstp->st_score = score;
13029 sstp->st_altscore = 0;
13030 sstp->st_orglen = stp->st_orglen;
13031 ++su->su_sga.ga_len;
13041 * Combine the list of suggestions in su->su_ga and su->su_sga.
13042 * They are intwined.
13055 char_u badsound[MAXWLEN];
13058 slang_T *slang = NULL;
13060 /* Add the alternate score to su_ga. */
13061 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
13063 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
13064 if (lp->lp_slang->sl_sal.ga_len > 0)
13066 /* soundfold the bad word */
13067 slang = lp->lp_slang;
13068 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
13070 for (i = 0; i < su->su_ga.ga_len; ++i)
13072 stp = &SUG(su->su_ga, i);
13073 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
13074 if (stp->st_altscore == SCORE_MAXMAX)
13075 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13077 stp->st_score = (stp->st_score * 3
13078 + stp->st_altscore) / 4;
13079 stp->st_salscore = FALSE;
13085 if (slang == NULL) /* Using "double" without sound folding. */
13087 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13092 /* Add the alternate score to su_sga. */
13093 for (i = 0; i < su->su_sga.ga_len; ++i)
13095 stp = &SUG(su->su_sga, i);
13096 stp->st_altscore = spell_edit_score(slang,
13097 su->su_badword, stp->st_word);
13098 if (stp->st_score == SCORE_MAXMAX)
13099 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13101 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13102 stp->st_salscore = TRUE;
13105 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13106 * for both lists. */
13107 check_suggestions(su, &su->su_ga);
13108 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
13109 check_suggestions(su, &su->su_sga);
13110 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13112 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13113 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13117 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13119 /* round 1: get a suggestion from su_ga
13120 * round 2: get a suggestion from su_sga */
13121 for (round = 1; round <= 2; ++round)
13123 gap = round == 1 ? &su->su_ga : &su->su_sga;
13124 if (i < gap->ga_len)
13126 /* Don't add a word if it's already there. */
13127 p = SUG(*gap, i).st_word;
13128 for (j = 0; j < ga.ga_len; ++j)
13129 if (STRCMP(stp[j].st_word, p) == 0)
13131 if (j == ga.ga_len)
13132 stp[ga.ga_len++] = SUG(*gap, i);
13139 ga_clear(&su->su_ga);
13140 ga_clear(&su->su_sga);
13142 /* Truncate the list to the number of suggestions that will be displayed. */
13143 if (ga.ga_len > su->su_maxcount)
13145 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13146 vim_free(stp[i].st_word);
13147 ga.ga_len = su->su_maxcount;
13154 * For the goodword in "stp" compute the soundalike score compared to the
13158 stp_sal_score(stp, su, slang, badsound)
13162 char_u *badsound; /* sound-folded badword */
13167 char_u badsound2[MAXWLEN];
13168 char_u fword[MAXWLEN];
13169 char_u goodsound[MAXWLEN];
13170 char_u goodword[MAXWLEN];
13173 lendiff = (int)(su->su_badlen - stp->st_orglen);
13178 /* soundfold the bad word with more characters following */
13179 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13181 /* When joining two words the sound often changes a lot. E.g., "t he"
13182 * sounds like "t h" while "the" sounds like "@". Avoid that by
13183 * removing the space. Don't do it when the good word also contains a
13185 if (vim_iswhite(su->su_badptr[su->su_badlen])
13186 && *skiptowhite(stp->st_word) == NUL)
13187 for (p = fword; *(p = skiptowhite(p)) != NUL; )
13190 spell_soundfold(slang, fword, TRUE, badsound2);
13194 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
13196 /* Add part of the bad word to the good word, so that we soundfold
13197 * what replaces the bad word. */
13198 STRCPY(goodword, stp->st_word);
13199 vim_strncpy(goodword + stp->st_wordlen,
13200 su->su_badptr + su->su_badlen - lendiff, lendiff);
13204 pgood = stp->st_word;
13206 /* Sound-fold the word and compute the score for the difference. */
13207 spell_soundfold(slang, pgood, FALSE, goodsound);
13209 return soundalike_score(goodsound, pbad);
13212 /* structure used to store soundfolded words that add_sound_suggest() has
13213 * handled already. */
13216 short sft_score; /* lowest score used */
13217 char_u sft_word[1]; /* soundfolded word, actually longer */
13220 static sftword_T dumsft;
13221 #define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13222 #define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13225 * Prepare for calling suggest_try_soundalike().
13228 suggest_try_soundalike_prep()
13234 /* Do this for all languages that support sound folding and for which a
13235 * .sug file has been loaded. */
13236 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
13238 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
13239 slang = lp->lp_slang;
13240 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13241 /* prepare the hashtable used by add_sound_suggest() */
13242 hash_init(&slang->sl_sounddone);
13247 * Find suggestions by comparing the word in a sound-a-like form.
13248 * Note: This doesn't support postponed prefixes.
13251 suggest_try_soundalike(su)
13254 char_u salword[MAXWLEN];
13259 /* Do this for all languages that support sound folding and for which a
13260 * .sug file has been loaded. */
13261 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
13263 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
13264 slang = lp->lp_slang;
13265 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13267 /* soundfold the bad word */
13268 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
13270 /* try all kinds of inserts/deletes/swaps/etc. */
13271 /* TODO: also soundfold the next words, so that we can try joining
13273 suggest_trie_walk(su, lp, salword, TRUE);
13279 * Finish up after calling suggest_try_soundalike().
13282 suggest_try_soundalike_finish()
13290 /* Do this for all languages that support sound folding and for which a
13291 * .sug file has been loaded. */
13292 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
13294 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
13295 slang = lp->lp_slang;
13296 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13298 /* Free the info about handled words. */
13299 todo = (int)slang->sl_sounddone.ht_used;
13300 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13301 if (!HASHITEM_EMPTY(hi))
13303 vim_free(HI2SFT(hi));
13307 /* Clear the hashtable, it may also be used by another region. */
13308 hash_clear(&slang->sl_sounddone);
13309 hash_init(&slang->sl_sounddone);
13315 * A match with a soundfolded word is found. Add the good word(s) that
13316 * produce this soundfolded word.
13319 add_sound_suggest(su, goodword, score, lp)
13322 int score; /* soundfold score */
13325 slang_T *slang = lp->lp_slang; /* language for sound folding */
13329 char_u theword[MAXWLEN];
13345 * It's very well possible that the same soundfold word is found several
13346 * times with different scores. Since the following is quite slow only do
13347 * the words that have a better score than before. Use a hashtable to
13348 * remember the words that have been done.
13350 hash = hash_hash(goodword);
13351 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13352 if (HASHITEM_EMPTY(hi))
13354 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13355 + STRLEN(goodword)));
13358 sft->sft_score = score;
13359 STRCPY(sft->sft_word, goodword);
13360 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13366 if (score >= sft->sft_score)
13368 sft->sft_score = score;
13372 * Find the word nr in the soundfold tree.
13374 sfwordnr = soundfold_find(slang, goodword);
13377 EMSG2(_(e_intern2), "add_sound_suggest()");
13382 * go over the list of good words that produce this soundfold word
13384 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13386 while (*nrline != NUL)
13388 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13389 * previous wordnr. */
13390 orgnr += bytes2offset(&nrline);
13392 byts = slang->sl_fbyts;
13393 idxs = slang->sl_fidxs;
13395 /* Lookup the word "orgnr" one of the two tries. */
13402 if (wordcount == orgnr && byts[n + 1] == NUL)
13403 break; /* found end of word */
13405 if (byts[n + 1] == NUL)
13408 /* skip over the NUL bytes */
13409 for ( ; byts[n + i] == NUL; ++i)
13410 if (i > byts[n]) /* safety check */
13412 STRCPY(theword + wlen, "BAD");
13416 /* One of the siblings must have the word. */
13417 for ( ; i < byts[n]; ++i)
13419 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13420 if (wordcount + wc > orgnr)
13425 theword[wlen++] = byts[n + i];
13429 theword[wlen] = NUL;
13431 /* Go over the possible flags and regions. */
13432 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13434 char_u cword[MAXWLEN];
13436 int flags = (int)idxs[n + i];
13438 /* Skip words with the NOSUGGEST flag */
13439 if (flags & WF_NOSUGGEST)
13442 if (flags & WF_KEEPCAP)
13444 /* Must find the word in the keep-case tree. */
13445 find_keepcap_word(slang, theword, cword);
13450 flags |= su->su_badflags;
13451 if ((flags & WF_CAPMASK) != 0)
13453 /* Need to fix case according to "flags". */
13454 make_case_word(theword, cword, flags);
13461 /* Add the suggestion. */
13462 if (sps_flags & SPS_DOUBLE)
13464 /* Add the suggestion if the score isn't too bad. */
13465 if (score <= su->su_maxscore)
13466 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13467 score, 0, FALSE, slang, FALSE);
13471 /* Add a penalty for words in another region. */
13472 if ((flags & WF_REGION)
13473 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13474 goodscore = SCORE_REGION;
13478 /* Add a small penalty for changing the first letter from
13479 * lower to upper case. Helps for "tath" -> "Kath", which is
13480 * less common thatn "tath" -> "path". Don't do it when the
13481 * letter is the same, that has already been counted. */
13483 if (SPELL_ISUPPER(gc))
13485 bc = PTR2CHAR(su->su_badword);
13486 if (!SPELL_ISUPPER(bc)
13487 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13488 goodscore += SCORE_ICASE / 2;
13491 /* Compute the score for the good word. This only does letter
13492 * insert/delete/swap/replace. REP items are not considered,
13493 * which may make the score a bit higher.
13494 * Use a limit for the score to make it work faster. Use
13495 * MAXSCORE(), because RESCORE() will change the score.
13496 * If the limit is very high then the iterative method is
13497 * inefficient, using an array is quicker. */
13498 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13499 if (limit > SCORE_LIMITMAX)
13500 goodscore += spell_edit_score(slang, su->su_badword, p);
13502 goodscore += spell_edit_score_limit(slang, su->su_badword,
13505 /* When going over the limit don't bother to do the rest. */
13506 if (goodscore < SCORE_MAXMAX)
13508 /* Give a bonus to words seen before. */
13509 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
13511 /* Add the suggestion if the score isn't too bad. */
13512 goodscore = RESCORE(goodscore, score);
13513 if (goodscore <= su->su_sfmaxscore)
13514 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13515 goodscore, score, TRUE, slang, TRUE);
13519 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
13524 * Find word "word" in fold-case tree for "slang" and return the word number.
13527 soundfold_find(slang, word)
13535 char_u *ptr = word;
13540 byts = slang->sl_sbyts;
13541 idxs = slang->sl_sidxs;
13545 /* First byte is the number of possible bytes. */
13546 len = byts[arridx++];
13548 /* If the first possible byte is a zero the word could end here.
13549 * If the word ends we found the word. If not skip the NUL bytes. */
13551 if (byts[arridx] == NUL)
13556 /* Skip over the zeros, there can be several. */
13557 while (len > 0 && byts[arridx] == NUL)
13563 return -1; /* no children, word should have ended here */
13567 /* If the word ends we didn't find it. */
13571 /* Perform a binary search in the list of accepted bytes. */
13572 if (c == TAB) /* <Tab> is handled like <Space> */
13574 while (byts[arridx] < c)
13576 /* The word count is in the first idxs[] entry of the child. */
13577 wordnr += idxs[idxs[arridx]];
13579 if (--len == 0) /* end of the bytes, didn't find it */
13582 if (byts[arridx] != c) /* didn't find the byte */
13585 /* Continue at the child (if there is one). */
13586 arridx = idxs[arridx];
13589 /* One space in the good word may stand for several spaces in the
13592 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13600 * Copy "fword" to "cword", fixing case according to "flags".
13603 make_case_word(fword, cword, flags)
13608 if (flags & WF_ALLCAP)
13609 /* Make it all upper-case */
13610 allcap_copy(fword, cword);
13611 else if (flags & WF_ONECAP)
13612 /* Make the first letter upper-case */
13613 onecap_copy(fword, cword, TRUE);
13615 /* Use goodword as-is. */
13616 STRCPY(cword, fword);
13620 * Use map string "map" for languages "lp".
13623 set_map_str(lp, map)
13634 lp->sl_has_map = FALSE;
13637 lp->sl_has_map = TRUE;
13639 /* Init the array and hash tables empty. */
13640 for (i = 0; i < 256; ++i)
13641 lp->sl_map_array[i] = 0;
13643 hash_init(&lp->sl_map_hash);
13647 * The similar characters are stored separated with slashes:
13648 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13649 * before the same slash. For characters above 255 sl_map_hash is used.
13651 for (p = map; *p != NUL; )
13654 c = mb_cptr2char_adv(&p);
13666 /* Characters above 255 don't fit in sl_map_array[], put them in
13667 * the hash table. Each entry is the char, a NUL the headchar and
13671 int cl = mb_char2len(c);
13672 int headcl = mb_char2len(headc);
13677 b = alloc((unsigned)(cl + headcl + 2));
13680 mb_char2bytes(c, b);
13682 mb_char2bytes(headc, b + cl + 1);
13683 b[cl + 1 + headcl] = NUL;
13684 hash = hash_hash(b);
13685 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13686 if (HASHITEM_EMPTY(hi))
13687 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13690 /* This should have been checked when generating the .spl
13692 EMSG(_("E783: duplicate char in MAP entry"));
13698 lp->sl_map_array[c] = headc;
13704 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13705 * lines in the .aff file.
13708 similar_chars(slang, c1, c2)
13715 char_u buf[MB_MAXBYTES];
13720 buf[mb_char2bytes(c1, buf)] = 0;
13721 hi = hash_find(&slang->sl_map_hash, buf);
13722 if (HASHITEM_EMPTY(hi))
13725 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13729 m1 = slang->sl_map_array[c1];
13737 buf[mb_char2bytes(c2, buf)] = 0;
13738 hi = hash_find(&slang->sl_map_hash, buf);
13739 if (HASHITEM_EMPTY(hi))
13742 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13746 m2 = slang->sl_map_array[c2];
13752 * Add a suggestion to the list of suggestions.
13753 * For a suggestion that is already in the list the lowest score is remembered.
13756 add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13759 garray_T *gap; /* either su_ga or su_sga */
13761 int badlenarg; /* len of bad word replaced with "goodword" */
13764 int had_bonus; /* value for st_had_bonus */
13765 slang_T *slang; /* language for sound folding */
13766 int maxsf; /* su_maxscore applies to soundfold score,
13767 su_sfmaxscore to the total score. */
13769 int goodlen; /* len of goodword changed */
13770 int badlen; /* len of bad word changed */
13774 char_u *pgood, *pbad;
13776 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13777 * "thee the" is added next to changing the first "the" the "thee". */
13778 pgood = goodword + STRLEN(goodword);
13779 pbad = su->su_badptr + badlenarg;
13782 goodlen = (int)(pgood - goodword);
13783 badlen = (int)(pbad - su->su_badptr);
13784 if (goodlen <= 0 || badlen <= 0)
13786 mb_ptr_back(goodword, pgood);
13787 mb_ptr_back(su->su_badptr, pbad);
13791 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13796 if (*pgood != *pbad)
13800 if (badlen == 0 && goodlen == 0)
13801 /* goodword doesn't change anything; may happen for "the the" changing
13802 * the first "the" to itself. */
13805 if (gap->ga_len == 0)
13809 /* Check if the word is already there. Also check the length that is
13810 * being replaced "thes," -> "these" is a different suggestion from
13811 * "thes" -> "these". */
13812 stp = &SUG(*gap, 0);
13813 for (i = gap->ga_len; --i >= 0; ++stp)
13814 if (stp->st_wordlen == goodlen
13815 && stp->st_orglen == badlen
13816 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13819 * Found it. Remember the word with the lowest score.
13821 if (stp->st_slang == NULL)
13822 stp->st_slang = slang;
13824 new_sug.st_score = score;
13825 new_sug.st_altscore = altscore;
13826 new_sug.st_had_bonus = had_bonus;
13828 if (stp->st_had_bonus != had_bonus)
13830 /* Only one of the two had the soundalike score computed.
13831 * Need to do that for the other one now, otherwise the
13832 * scores can't be compared. This happens because
13833 * suggest_try_change() doesn't compute the soundalike
13834 * word to keep it fast, while some special methods set
13835 * the soundalike score to zero. */
13837 rescore_one(su, stp);
13840 new_sug.st_word = stp->st_word;
13841 new_sug.st_wordlen = stp->st_wordlen;
13842 new_sug.st_slang = stp->st_slang;
13843 new_sug.st_orglen = badlen;
13844 rescore_one(su, &new_sug);
13848 if (stp->st_score > new_sug.st_score)
13850 stp->st_score = new_sug.st_score;
13851 stp->st_altscore = new_sug.st_altscore;
13852 stp->st_had_bonus = new_sug.st_had_bonus;
13858 if (i < 0 && ga_grow(gap, 1) == OK)
13860 /* Add a suggestion. */
13861 stp = &SUG(*gap, gap->ga_len);
13862 stp->st_word = vim_strnsave(goodword, goodlen);
13863 if (stp->st_word != NULL)
13865 stp->st_wordlen = goodlen;
13866 stp->st_score = score;
13867 stp->st_altscore = altscore;
13868 stp->st_had_bonus = had_bonus;
13869 stp->st_orglen = badlen;
13870 stp->st_slang = slang;
13873 /* If we have too many suggestions now, sort the list and keep
13874 * the best suggestions. */
13875 if (gap->ga_len > SUG_MAX_COUNT(su))
13878 su->su_sfmaxscore = cleanup_suggestions(gap,
13879 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13881 su->su_maxscore = cleanup_suggestions(gap,
13882 su->su_maxscore, SUG_CLEAN_COUNT(su));
13889 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13890 * for split words, such as "the the". Remove these from the list here.
13893 check_suggestions(su, gap)
13895 garray_T *gap; /* either su_ga or su_sga */
13899 char_u longword[MAXWLEN + 1];
13903 stp = &SUG(*gap, 0);
13904 for (i = gap->ga_len - 1; i >= 0; --i)
13906 /* Need to append what follows to check for "the the". */
13907 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
13908 len = stp[i].st_wordlen;
13909 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13912 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13913 if (attr != HLF_COUNT)
13915 /* Remove this entry. */
13916 vim_free(stp[i].st_word);
13918 if (i < gap->ga_len)
13919 mch_memmove(stp + i, stp + i + 1,
13920 sizeof(suggest_T) * (gap->ga_len - i));
13927 * Add a word to be banned.
13930 add_banned(su, word)
13938 hash = hash_hash(word);
13939 hi = hash_lookup(&su->su_banned, word, hash);
13940 if (HASHITEM_EMPTY(hi))
13942 s = vim_strsave(word);
13944 hash_add_item(&su->su_banned, hi, s, hash);
13949 * Recompute the score for all suggestions if sound-folding is possible. This
13950 * is slow, thus only done for the final results.
13953 rescore_suggestions(su)
13958 if (su->su_sallang != NULL)
13959 for (i = 0; i < su->su_ga.ga_len; ++i)
13960 rescore_one(su, &SUG(su->su_ga, i));
13964 * Recompute the score for one suggestion if sound-folding is possible.
13967 rescore_one(su, stp)
13971 slang_T *slang = stp->st_slang;
13972 char_u sal_badword[MAXWLEN];
13975 /* Only rescore suggestions that have no sal score yet and do have a
13977 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13979 if (slang == su->su_sallang)
13980 p = su->su_sal_badword;
13983 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
13987 stp->st_altscore = stp_sal_score(stp, su, slang, p);
13988 if (stp->st_altscore == SCORE_MAXMAX)
13989 stp->st_altscore = SCORE_BIG;
13990 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13991 stp->st_had_bonus = TRUE;
13996 #ifdef __BORLANDC__
13999 sug_compare __ARGS((const void *s1, const void *s2));
14002 * Function given to qsort() to sort the suggestions on st_score.
14003 * First on "st_score", then "st_altscore" then alphabetically.
14006 #ifdef __BORLANDC__
14009 sug_compare(s1, s2)
14013 suggest_T *p1 = (suggest_T *)s1;
14014 suggest_T *p2 = (suggest_T *)s2;
14015 int n = p1->st_score - p2->st_score;
14019 n = p1->st_altscore - p2->st_altscore;
14021 n = STRICMP(p1->st_word, p2->st_word);
14027 * Cleanup the suggestions:
14029 * - Remove words that won't be displayed.
14030 * Returns the maximum score in the list or "maxscore" unmodified.
14033 cleanup_suggestions(gap, maxscore, keep)
14036 int keep; /* nr of suggestions to keep */
14038 suggest_T *stp = &SUG(*gap, 0);
14041 /* Sort the list. */
14042 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
14044 /* Truncate the list to the number of suggestions that will be displayed. */
14045 if (gap->ga_len > keep)
14047 for (i = keep; i < gap->ga_len; ++i)
14048 vim_free(stp[i].st_word);
14049 gap->ga_len = keep;
14050 return stp[keep - 1].st_score;
14055 #if defined(FEAT_EVAL) || defined(PROTO)
14057 * Soundfold a string, for soundfold().
14058 * Result is in allocated memory, NULL for an error.
14061 eval_soundfold(word)
14065 char_u sound[MAXWLEN];
14068 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
14069 /* Use the sound-folding of the first language that supports it. */
14070 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
14072 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
14073 if (lp->lp_slang->sl_sal.ga_len > 0)
14075 /* soundfold the word */
14076 spell_soundfold(lp->lp_slang, word, FALSE, sound);
14077 return vim_strsave(sound);
14081 /* No language with sound folding, return word as-is. */
14082 return vim_strsave(word);
14087 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14089 * There are many ways to turn a word into a sound-a-like representation. The
14090 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14091 * swedish name matching - survey and test of different algorithms" by Klas
14094 * We support two methods:
14095 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14096 * 2. SAL items define a more advanced sound-folding (and much slower).
14099 spell_soundfold(slang, inword, folded, res)
14102 int folded; /* "inword" is already case-folded */
14105 char_u fword[MAXWLEN];
14108 if (slang->sl_sofo)
14109 /* SOFOFROM and SOFOTO used */
14110 spell_soundfold_sofo(slang, inword, res);
14113 /* SAL items used. Requires the word to be case-folded. */
14118 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
14124 spell_soundfold_wsal(slang, word, res);
14127 spell_soundfold_sal(slang, word, res);
14132 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14136 spell_soundfold_sofo(slang, inword, res)
14151 /* The sl_sal_first[] table contains the translation for chars up to
14152 * 255, sl_sal the rest. */
14153 for (s = inword; *s != NUL; )
14155 c = mb_cptr2char_adv(&s);
14156 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14159 c = slang->sl_sal_first[c];
14162 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14163 if (ip == NULL) /* empty list, can't match */
14166 for (;;) /* find "c" in the list */
14168 if (*ip == 0) /* not found */
14173 if (*ip == c) /* match! */
14182 if (c != NUL && c != prevc)
14184 ri += mb_char2bytes(c, res + ri);
14185 if (ri + MB_MAXBYTES > MAXWLEN)
14194 /* The sl_sal_first[] table contains the translation. */
14195 for (s = inword; (c = *s) != NUL; ++s)
14197 if (vim_iswhite(c))
14200 c = slang->sl_sal_first[c];
14201 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14210 spell_soundfold_sal(slang, inword, res)
14216 char_u word[MAXWLEN];
14217 char_u *s = inword;
14231 /* Remove accents, if wanted. We actually remove all non-word characters.
14232 * But keep white space. We need a copy, the word may be changed here. */
14233 if (slang->sl_rem_accents)
14238 if (vim_iswhite(*s))
14245 if (spell_iswordp_nmw(s))
14253 vim_strncpy(word, s, MAXWLEN - 1);
14255 smp = (salitem_T *)slang->sl_sal.ga_data;
14258 * This comes from Aspell phonet.cpp. Converted from C++ to C.
14259 * Changed to keep spaces.
14261 i = reslen = z = 0;
14262 while ((c = word[i]) != NUL)
14264 /* Start with the first rule that has the character in the word. */
14265 n = slang->sl_sal_first[c];
14270 /* check all rules for the same letter */
14271 for (; (s = smp[n].sm_lead)[0] == c; ++n)
14273 /* Quickly skip entries that don't match the word. Most
14274 * entries are less then three chars, optimize for that. */
14275 k = smp[n].sm_leadlen;
14278 if (word[i + 1] != s[1])
14282 for (j = 2; j < k; ++j)
14283 if (word[i + j] != s[j])
14290 if ((pf = smp[n].sm_oneof) != NULL)
14292 /* Check for match with one of the chars in "sm_oneof". */
14293 while (*pf != NUL && *pf != word[i + k])
14299 s = smp[n].sm_rules;
14300 pri = 5; /* default priority */
14304 while (*s == '-' && k > 1)
14311 if (VIM_ISDIGIT(*s))
14313 /* determine priority */
14317 if (*s == '^' && *(s + 1) == '^')
14322 && (i == 0 || !(word[i - 1] == ' '
14323 || spell_iswordp(word + i - 1, curwin)))
14324 && (*(s + 1) != '$'
14325 || (!spell_iswordp(word + i + k0, curwin))))
14326 || (*s == '$' && i > 0
14327 && spell_iswordp(word + i - 1, curwin)
14328 && (!spell_iswordp(word + i + k0, curwin))))
14330 /* search for followup rules, if: */
14331 /* followup and k > 1 and NO '-' in searchstring */
14332 c0 = word[i + k - 1];
14333 n0 = slang->sl_sal_first[c0];
14335 if (slang->sl_followup && k > 1 && n0 >= 0
14336 && p0 != '-' && word[i + k] != NUL)
14338 /* test follow-up rule for "word[i + k]" */
14339 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
14341 /* Quickly skip entries that don't match the word.
14343 k0 = smp[n0].sm_leadlen;
14346 if (word[i + k] != s[1])
14350 pf = word + i + k + 1;
14351 for (j = 2; j < k0; ++j)
14360 if ((pf = smp[n0].sm_oneof) != NULL)
14362 /* Check for match with one of the chars in
14364 while (*pf != NUL && *pf != word[i + k0])
14372 s = smp[n0].sm_rules;
14375 /* "k0" gets NOT reduced because
14376 * "if (k0 == k)" */
14381 if (VIM_ISDIGIT(*s))
14388 /* *s == '^' cuts */
14390 && !spell_iswordp(word + i + k0,
14394 /* this is just a piece of the string */
14398 /* priority too low */
14400 /* rule fits; stop search */
14405 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
14409 /* replace string */
14413 pf = smp[n].sm_rules;
14414 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
14415 if (p0 == 1 && z == 0)
14417 /* rule with '<' is used */
14418 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14419 || res[reslen - 1] == *s))
14424 while (*s != NUL && word[i + k0] != NUL)
14431 STRMOVE(word + i + k0, word + i + k);
14433 /* new "actual letter" */
14438 /* no '<' rule used */
14441 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
14443 if (reslen == 0 || res[reslen - 1] != *s)
14444 res[reslen++] = *s;
14447 /* new "actual letter" */
14449 if (strstr((char *)pf, "^^") != NULL)
14453 STRMOVE(word, word + i + 1);
14462 else if (vim_iswhite(c))
14470 if (k && !p0 && reslen < MAXWLEN && c != NUL
14471 && (!slang->sl_collapse || reslen == 0
14472 || res[reslen - 1] != c))
14473 /* condense only double letters */
14487 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14488 * Multi-byte version of spell_soundfold().
14491 spell_soundfold_wsal(slang, inword, res)
14496 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
14514 int did_white = FALSE;
14517 * Convert the multi-byte string to a wide-character string.
14518 * Remove accents, if wanted. We actually remove all non-word characters.
14519 * But keep white space.
14522 for (s = inword; *s != NUL; )
14525 c = mb_cptr2char_adv(&s);
14526 if (slang->sl_rem_accents)
14528 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14538 if (!spell_iswordp_nmw(t))
14547 * This comes from Aspell phonet.cpp.
14548 * Converted from C++ to C. Added support for multi-byte chars.
14549 * Changed to keep spaces.
14551 i = reslen = z = 0;
14552 while ((c = word[i]) != NUL)
14554 /* Start with the first rule that has the character in the word. */
14555 n = slang->sl_sal_first[c & 0xff];
14560 /* Check all rules for the same index byte.
14561 * If c is 0x300 need extra check for the end of the array, as
14562 * (c & 0xff) is NUL. */
14563 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14564 && ws[0] != NUL; ++n)
14566 /* Quickly skip entries that don't match the word. Most
14567 * entries are less then three chars, optimize for that. */
14570 k = smp[n].sm_leadlen;
14573 if (word[i + 1] != ws[1])
14577 for (j = 2; j < k; ++j)
14578 if (word[i + j] != ws[j])
14585 if ((pf = smp[n].sm_oneof_w) != NULL)
14587 /* Check for match with one of the chars in "sm_oneof". */
14588 while (*pf != NUL && *pf != word[i + k])
14594 s = smp[n].sm_rules;
14595 pri = 5; /* default priority */
14599 while (*s == '-' && k > 1)
14606 if (VIM_ISDIGIT(*s))
14608 /* determine priority */
14612 if (*s == '^' && *(s + 1) == '^')
14617 && (i == 0 || !(word[i - 1] == ' '
14618 || spell_iswordp_w(word + i - 1, curwin)))
14619 && (*(s + 1) != '$'
14620 || (!spell_iswordp_w(word + i + k0, curwin))))
14621 || (*s == '$' && i > 0
14622 && spell_iswordp_w(word + i - 1, curwin)
14623 && (!spell_iswordp_w(word + i + k0, curwin))))
14625 /* search for followup rules, if: */
14626 /* followup and k > 1 and NO '-' in searchstring */
14627 c0 = word[i + k - 1];
14628 n0 = slang->sl_sal_first[c0 & 0xff];
14630 if (slang->sl_followup && k > 1 && n0 >= 0
14631 && p0 != '-' && word[i + k] != NUL)
14633 /* Test follow-up rule for "word[i + k]"; loop over
14634 * all entries with the same index byte. */
14635 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14636 == (c0 & 0xff); ++n0)
14638 /* Quickly skip entries that don't match the word.
14642 k0 = smp[n0].sm_leadlen;
14645 if (word[i + k] != ws[1])
14649 pf = word + i + k + 1;
14650 for (j = 2; j < k0; ++j)
14651 if (*pf++ != ws[j])
14659 if ((pf = smp[n0].sm_oneof_w) != NULL)
14661 /* Check for match with one of the chars in
14663 while (*pf != NUL && *pf != word[i + k0])
14671 s = smp[n0].sm_rules;
14674 /* "k0" gets NOT reduced because
14675 * "if (k0 == k)" */
14680 if (VIM_ISDIGIT(*s))
14687 /* *s == '^' cuts */
14689 && !spell_iswordp_w(word + i + k0,
14693 /* this is just a piece of the string */
14697 /* priority too low */
14699 /* rule fits; stop search */
14704 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14709 /* replace string */
14710 ws = smp[n].sm_to_w;
14711 s = smp[n].sm_rules;
14712 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14713 if (p0 == 1 && z == 0)
14715 /* rule with '<' is used */
14716 if (reslen > 0 && ws != NULL && *ws != NUL
14717 && (wres[reslen - 1] == c
14718 || wres[reslen - 1] == *ws))
14724 while (*ws != NUL && word[i + k0] != NUL)
14726 word[i + k0] = *ws;
14731 mch_memmove(word + i + k0, word + i + k,
14732 sizeof(int) * (STRLEN(word + i + k) + 1));
14734 /* new "actual letter" */
14739 /* no '<' rule used */
14743 while (*ws != NUL && ws[1] != NUL
14744 && reslen < MAXWLEN)
14746 if (reslen == 0 || wres[reslen - 1] != *ws)
14747 wres[reslen++] = *ws;
14750 /* new "actual letter" */
14755 if (strstr((char *)s, "^^") != NULL)
14758 wres[reslen++] = c;
14759 mch_memmove(word, word + i + 1,
14760 sizeof(int) * (STRLEN(word + i + 1) + 1));
14769 else if (vim_iswhite(c))
14777 if (k && !p0 && reslen < MAXWLEN && c != NUL
14778 && (!slang->sl_collapse || reslen == 0
14779 || wres[reslen - 1] != c))
14780 /* condense only double letters */
14781 wres[reslen++] = c;
14789 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14791 for (n = 0; n < reslen; ++n)
14793 l += mb_char2bytes(wres[n], res + l);
14794 if (l + MB_MAXBYTES > MAXWLEN)
14802 * Compute a score for two sound-a-like words.
14803 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14804 * Instead of a generic loop we write out the code. That keeps it fast by
14805 * avoiding checks that will not be possible.
14808 soundalike_score(goodstart, badstart)
14809 char_u *goodstart; /* sound-folded good word */
14810 char_u *badstart; /* sound-folded bad word */
14812 char_u *goodsound = goodstart;
14813 char_u *badsound = badstart;
14821 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
14822 * counted so much, vowels halfway the word aren't counted at all. */
14823 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14825 if ((badsound[0] == NUL && goodsound[1] == NUL)
14826 || (goodsound[0] == NUL && badsound[1] == NUL))
14827 /* changing word with vowel to word without a sound */
14829 if (badsound[0] == NUL || goodsound[0] == NUL)
14830 /* more than two changes */
14831 return SCORE_MAXMAX;
14833 if (badsound[1] == goodsound[1]
14834 || (badsound[1] != NUL
14835 && goodsound[1] != NUL
14836 && badsound[2] == goodsound[2]))
14838 /* handle like a substitute */
14842 score = 2 * SCORE_DEL / 3;
14843 if (*badsound == '*')
14850 goodlen = (int)STRLEN(goodsound);
14851 badlen = (int)STRLEN(badsound);
14853 /* Return quickly if the lengths are too different to be fixed by two
14855 n = goodlen - badlen;
14856 if (n < -2 || n > 2)
14857 return SCORE_MAXMAX;
14861 pl = goodsound; /* goodsound is longest */
14866 pl = badsound; /* badsound is longest */
14870 /* Skip over the identical part. */
14871 while (*pl == *ps && *pl != NUL)
14882 * Must delete two characters from "pl".
14884 ++pl; /* first delete */
14890 /* strings must be equal after second delete */
14891 if (STRCMP(pl + 1, ps) == 0)
14892 return score + SCORE_DEL * 2;
14894 /* Failed to compare. */
14900 * Minimal one delete from "pl" required.
14906 while (*pl2 == *ps2)
14908 if (*pl2 == NUL) /* reached the end */
14909 return score + SCORE_DEL;
14914 /* 2: delete then swap, then rest must be equal */
14915 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14916 && STRCMP(pl2 + 2, ps2 + 2) == 0)
14917 return score + SCORE_DEL + SCORE_SWAP;
14919 /* 3: delete then substitute, then the rest must be equal */
14920 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
14921 return score + SCORE_DEL + SCORE_SUBST;
14923 /* 4: first swap then delete */
14924 if (pl[0] == ps[1] && pl[1] == ps[0])
14926 pl2 = pl + 2; /* swap, skip two chars */
14928 while (*pl2 == *ps2)
14933 /* delete a char and then strings must be equal */
14934 if (STRCMP(pl2 + 1, ps2) == 0)
14935 return score + SCORE_SWAP + SCORE_DEL;
14938 /* 5: first substitute then delete */
14939 pl2 = pl + 1; /* substitute, skip one char */
14941 while (*pl2 == *ps2)
14946 /* delete a char and then strings must be equal */
14947 if (STRCMP(pl2 + 1, ps2) == 0)
14948 return score + SCORE_SUBST + SCORE_DEL;
14950 /* Failed to compare. */
14955 * Lengths are equal, thus changes must result in same length: An
14956 * insert is only possible in combination with a delete.
14957 * 1: check if for identical strings
14963 if (pl[0] == ps[1] && pl[1] == ps[0])
14965 pl2 = pl + 2; /* swap, skip two chars */
14967 while (*pl2 == *ps2)
14969 if (*pl2 == NUL) /* reached the end */
14970 return score + SCORE_SWAP;
14974 /* 3: swap and swap again */
14975 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14976 && STRCMP(pl2 + 2, ps2 + 2) == 0)
14977 return score + SCORE_SWAP + SCORE_SWAP;
14979 /* 4: swap and substitute */
14980 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
14981 return score + SCORE_SWAP + SCORE_SUBST;
14984 /* 5: substitute */
14987 while (*pl2 == *ps2)
14989 if (*pl2 == NUL) /* reached the end */
14990 return score + SCORE_SUBST;
14995 /* 6: substitute and swap */
14996 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14997 && STRCMP(pl2 + 2, ps2 + 2) == 0)
14998 return score + SCORE_SUBST + SCORE_SWAP;
15000 /* 7: substitute and substitute */
15001 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
15002 return score + SCORE_SUBST + SCORE_SUBST;
15004 /* 8: insert then delete */
15007 while (*pl2 == *ps2)
15012 if (STRCMP(pl2 + 1, ps2) == 0)
15013 return score + SCORE_INS + SCORE_DEL;
15015 /* 9: delete then insert */
15018 while (*pl2 == *ps2)
15023 if (STRCMP(pl2, ps2 + 1) == 0)
15024 return score + SCORE_INS + SCORE_DEL;
15026 /* Failed to compare. */
15030 return SCORE_MAXMAX;
15034 * Compute the "edit distance" to turn "badword" into "goodword". The less
15035 * deletes/inserts/substitutes/swaps are required the lower the score.
15037 * The algorithm is described by Du and Chang, 1992.
15038 * The implementation of the algorithm comes from Aspell editdist.cpp,
15039 * edit_distance(). It has been converted from C++ to C and modified to
15040 * support multi-byte characters.
15043 spell_edit_score(slang, badword, goodword)
15049 int badlen, goodlen; /* lengths including NUL */
15056 int wbadword[MAXWLEN];
15057 int wgoodword[MAXWLEN];
15061 /* Get the characters from the multi-byte strings and put them in an
15062 * int array for easy access. */
15063 for (p = badword, badlen = 0; *p != NUL; )
15064 wbadword[badlen++] = mb_cptr2char_adv(&p);
15065 wbadword[badlen++] = 0;
15066 for (p = goodword, goodlen = 0; *p != NUL; )
15067 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
15068 wgoodword[goodlen++] = 0;
15073 badlen = (int)STRLEN(badword) + 1;
15074 goodlen = (int)STRLEN(goodword) + 1;
15077 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15078 #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
15079 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15082 return 0; /* out of memory */
15085 for (j = 1; j <= goodlen; ++j)
15086 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
15088 for (i = 1; i <= badlen; ++i)
15090 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
15091 for (j = 1; j <= goodlen; ++j)
15096 bc = wbadword[i - 1];
15097 gc = wgoodword[j - 1];
15102 bc = badword[i - 1];
15103 gc = goodword[j - 1];
15106 CNT(i, j) = CNT(i - 1, j - 1);
15109 /* Use a better score when there is only a case difference. */
15110 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15111 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15114 /* For a similar character use SCORE_SIMILAR. */
15116 && slang->sl_has_map
15117 && similar_chars(slang, gc, bc))
15118 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15120 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15123 if (i > 1 && j > 1)
15128 pbc = wbadword[i - 2];
15129 pgc = wgoodword[j - 2];
15134 pbc = badword[i - 2];
15135 pgc = goodword[j - 2];
15137 if (bc == pgc && pbc == gc)
15139 t = SCORE_SWAP + CNT(i - 2, j - 2);
15144 t = SCORE_DEL + CNT(i - 1, j);
15147 t = SCORE_INS + CNT(i, j - 1);
15154 i = CNT(badlen - 1, goodlen - 1);
15167 * Like spell_edit_score(), but with a limit on the score to make it faster.
15168 * May return SCORE_MAXMAX when the score is higher than "limit".
15170 * This uses a stack for the edits still to be tried.
15171 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15172 * for multi-byte characters.
15175 spell_edit_score_limit(slang, badword, goodword, limit)
15181 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15192 /* Multi-byte characters require a bit more work, use a different function
15193 * to avoid testing "has_mbyte" quite often. */
15195 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15199 * The idea is to go from start to end over the words. So long as
15200 * characters are equal just continue, this always gives the lowest score.
15201 * When there is a difference try several alternatives. Each alternative
15202 * increases "score" for the edit distance. Some of the alternatives are
15203 * pushed unto a stack and tried later, some are tried right away. At the
15204 * end of the word the score for one alternative is known. The lowest
15205 * possible score is stored in "minscore".
15211 minscore = limit + 1;
15215 /* Skip over an equal part, score remains the same. */
15220 if (bc != gc) /* stop at a char that's different */
15222 if (bc == NUL) /* both words end */
15224 if (score < minscore)
15226 goto pop; /* do next alternative */
15232 if (gc == NUL) /* goodword ends, delete badword chars */
15236 if ((score += SCORE_DEL) >= minscore)
15237 goto pop; /* do next alternative */
15238 } while (badword[++bi] != NUL);
15241 else if (bc == NUL) /* badword ends, insert badword chars */
15245 if ((score += SCORE_INS) >= minscore)
15246 goto pop; /* do next alternative */
15247 } while (goodword[++gi] != NUL);
15250 else /* both words continue */
15252 /* If not close to the limit, perform a change. Only try changes
15253 * that may lead to a lower score than "minscore".
15254 * round 0: try deleting a char from badword
15255 * round 1: try inserting a char in badword */
15256 for (round = 0; round <= 1; ++round)
15258 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15259 if (score_off < minscore)
15261 if (score_off + SCORE_EDIT_MIN >= minscore)
15263 /* Near the limit, rest of the words must match. We
15264 * can check that right now, no need to push an item
15265 * onto the stack. */
15266 bi2 = bi + 1 - round;
15268 while (goodword[gi2] == badword[bi2])
15270 if (goodword[gi2] == NUL)
15272 minscore = score_off;
15281 /* try deleting/inserting a character later */
15282 stack[stackidx].badi = bi + 1 - round;
15283 stack[stackidx].goodi = gi + round;
15284 stack[stackidx].score = score_off;
15290 if (score + SCORE_SWAP < minscore)
15292 /* If swapping two characters makes a match then the
15293 * substitution is more expensive, thus there is no need to
15295 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15297 /* Swap two characters, that is: skip them. */
15300 score += SCORE_SWAP;
15305 /* Substitute one character for another which is the same
15306 * thing as deleting a character from both goodword and badword.
15307 * Use a better score when there is only a case difference. */
15308 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15309 score += SCORE_ICASE;
15312 /* For a similar character use SCORE_SIMILAR. */
15314 && slang->sl_has_map
15315 && similar_chars(slang, gc, bc))
15316 score += SCORE_SIMILAR;
15318 score += SCORE_SUBST;
15321 if (score < minscore)
15323 /* Do the substitution. */
15331 * Get here to try the next alternative, pop it from the stack.
15333 if (stackidx == 0) /* stack is empty, finished */
15336 /* pop an item from the stack */
15338 gi = stack[stackidx].goodi;
15339 bi = stack[stackidx].badi;
15340 score = stack[stackidx].score;
15343 /* When the score goes over "limit" it may actually be much higher.
15344 * Return a very large number to avoid going below the limit when giving a
15346 if (minscore > limit)
15347 return SCORE_MAXMAX;
15353 * Multi-byte version of spell_edit_score_limit().
15354 * Keep it in sync with the above!
15357 spell_edit_score_limit_w(slang, badword, goodword, limit)
15363 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15373 int wbadword[MAXWLEN];
15374 int wgoodword[MAXWLEN];
15376 /* Get the characters from the multi-byte strings and put them in an
15377 * int array for easy access. */
15379 for (p = badword; *p != NUL; )
15380 wbadword[bi++] = mb_cptr2char_adv(&p);
15381 wbadword[bi++] = 0;
15383 for (p = goodword; *p != NUL; )
15384 wgoodword[gi++] = mb_cptr2char_adv(&p);
15385 wgoodword[gi++] = 0;
15388 * The idea is to go from start to end over the words. So long as
15389 * characters are equal just continue, this always gives the lowest score.
15390 * When there is a difference try several alternatives. Each alternative
15391 * increases "score" for the edit distance. Some of the alternatives are
15392 * pushed unto a stack and tried later, some are tried right away. At the
15393 * end of the word the score for one alternative is known. The lowest
15394 * possible score is stored in "minscore".
15400 minscore = limit + 1;
15404 /* Skip over an equal part, score remains the same. */
15408 gc = wgoodword[gi];
15410 if (bc != gc) /* stop at a char that's different */
15412 if (bc == NUL) /* both words end */
15414 if (score < minscore)
15416 goto pop; /* do next alternative */
15422 if (gc == NUL) /* goodword ends, delete badword chars */
15426 if ((score += SCORE_DEL) >= minscore)
15427 goto pop; /* do next alternative */
15428 } while (wbadword[++bi] != NUL);
15431 else if (bc == NUL) /* badword ends, insert badword chars */
15435 if ((score += SCORE_INS) >= minscore)
15436 goto pop; /* do next alternative */
15437 } while (wgoodword[++gi] != NUL);
15440 else /* both words continue */
15442 /* If not close to the limit, perform a change. Only try changes
15443 * that may lead to a lower score than "minscore".
15444 * round 0: try deleting a char from badword
15445 * round 1: try inserting a char in badword */
15446 for (round = 0; round <= 1; ++round)
15448 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15449 if (score_off < minscore)
15451 if (score_off + SCORE_EDIT_MIN >= minscore)
15453 /* Near the limit, rest of the words must match. We
15454 * can check that right now, no need to push an item
15455 * onto the stack. */
15456 bi2 = bi + 1 - round;
15458 while (wgoodword[gi2] == wbadword[bi2])
15460 if (wgoodword[gi2] == NUL)
15462 minscore = score_off;
15471 /* try deleting a character from badword later */
15472 stack[stackidx].badi = bi + 1 - round;
15473 stack[stackidx].goodi = gi + round;
15474 stack[stackidx].score = score_off;
15480 if (score + SCORE_SWAP < minscore)
15482 /* If swapping two characters makes a match then the
15483 * substitution is more expensive, thus there is no need to
15485 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15487 /* Swap two characters, that is: skip them. */
15490 score += SCORE_SWAP;
15495 /* Substitute one character for another which is the same
15496 * thing as deleting a character from both goodword and badword.
15497 * Use a better score when there is only a case difference. */
15498 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15499 score += SCORE_ICASE;
15502 /* For a similar character use SCORE_SIMILAR. */
15504 && slang->sl_has_map
15505 && similar_chars(slang, gc, bc))
15506 score += SCORE_SIMILAR;
15508 score += SCORE_SUBST;
15511 if (score < minscore)
15513 /* Do the substitution. */
15521 * Get here to try the next alternative, pop it from the stack.
15523 if (stackidx == 0) /* stack is empty, finished */
15526 /* pop an item from the stack */
15528 gi = stack[stackidx].goodi;
15529 bi = stack[stackidx].badi;
15530 score = stack[stackidx].score;
15533 /* When the score goes over "limit" it may actually be much higher.
15534 * Return a very large number to avoid going below the limit when giving a
15536 if (minscore > limit)
15537 return SCORE_MAXMAX;
15547 exarg_T *eap UNUSED;
15553 if (no_spell_checking(curwin))
15557 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
15559 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
15560 msg_puts((char_u *)"file: ");
15561 msg_puts(lp->lp_slang->sl_fname);
15563 p = lp->lp_slang->sl_info;
15573 #define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15574 #define DUMPFLAG_COUNT 2 /* include word count */
15575 #define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
15576 #define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15577 #define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
15586 if (no_spell_checking(curwin))
15589 /* Create a new empty buffer by splitting the window. */
15590 do_cmdline_cmd((char_u *)"new");
15591 if (!bufempty() || !buf_valid(curbuf))
15594 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15596 /* Delete the empty line that we started with. */
15597 if (curbuf->b_ml.ml_line_count > 1)
15598 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15600 redraw_later(NOT_VALID);
15604 * Go through all possible words and:
15605 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15606 * "ic" and "dir" are not used.
15607 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15610 spell_dump_compl(pat, ic, dir, dumpflags_arg)
15611 char_u *pat; /* leading part of the word */
15612 int ic; /* ignore case */
15613 int *dir; /* direction for adding matches */
15614 int dumpflags_arg; /* DUMPFLAG_* */
15618 idx_T arridx[MAXWLEN];
15620 char_u word[MAXWLEN];
15629 char_u *region_names = NULL; /* region names being used */
15630 int do_region = TRUE; /* dump region names and numbers */
15633 int dumpflags = dumpflags_arg;
15636 /* When ignoring case or when the pattern starts with capital pass this on
15637 * to dump_word(). */
15641 dumpflags |= DUMPFLAG_ICASE;
15644 n = captype(pat, NULL);
15645 if (n == WF_ONECAP)
15646 dumpflags |= DUMPFLAG_ONECAP;
15647 else if (n == WF_ALLCAP
15649 && (int)STRLEN(pat) > mb_ptr2len(pat)
15651 && (int)STRLEN(pat) > 1
15654 dumpflags |= DUMPFLAG_ALLCAP;
15658 /* Find out if we can support regions: All languages must support the same
15659 * regions or none at all. */
15660 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
15662 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
15663 p = lp->lp_slang->sl_regions;
15666 if (region_names == NULL) /* first language with regions */
15668 else if (STRCMP(region_names, p) != 0)
15670 do_region = FALSE; /* region names are different */
15676 if (do_region && region_names != NULL)
15680 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15681 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15688 * Loop over all files loaded for the entries in 'spelllang'.
15690 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
15692 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
15693 slang = lp->lp_slang;
15694 if (slang->sl_fbyts == NULL) /* reloading failed */
15699 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15700 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15703 /* When matching with a pattern and there are no prefixes only use
15704 * parts of the tree that match "pat". */
15705 if (pat != NULL && slang->sl_pbyts == NULL)
15706 patlen = (int)STRLEN(pat);
15710 /* round 1: case-folded tree
15711 * round 2: keep-case tree */
15712 for (round = 1; round <= 2; ++round)
15716 dumpflags &= ~DUMPFLAG_KEEPCASE;
15717 byts = slang->sl_fbyts;
15718 idxs = slang->sl_fidxs;
15722 dumpflags |= DUMPFLAG_KEEPCASE;
15723 byts = slang->sl_kbyts;
15724 idxs = slang->sl_kidxs;
15727 continue; /* array is empty */
15732 while (depth >= 0 && !got_int
15733 && (pat == NULL || !compl_interrupted))
15735 if (curi[depth] > byts[arridx[depth]])
15737 /* Done all bytes at this node, go up one level. */
15740 ins_compl_check_keys(50);
15744 /* Do one more byte at this node. */
15745 n = arridx[depth] + curi[depth];
15750 /* End of word, deal with the word.
15751 * Don't use keep-case words in the fold-case tree,
15752 * they will appear in the keep-case tree.
15753 * Only use the word when the region matches. */
15754 flags = (int)idxs[n];
15755 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
15756 && (flags & WF_NEEDCOMP) == 0
15758 || (flags & WF_REGION) == 0
15759 || (((unsigned)flags >> 16)
15760 & lp->lp_region) != 0))
15764 flags &= ~WF_REGION;
15766 /* Dump the basic word if there is no prefix or
15767 * when it's the first one. */
15768 c = (unsigned)flags >> 24;
15769 if (c == 0 || curi[depth] == 2)
15771 dump_word(slang, word, pat, dir,
15772 dumpflags, flags, lnum);
15777 /* Apply the prefix, if there is one. */
15779 lnum = dump_prefixes(slang, word, pat, dir,
15780 dumpflags, flags, lnum);
15785 /* Normal char, go one level deeper. */
15787 arridx[depth] = idxs[n];
15790 /* Check if this characters matches with the pattern.
15791 * If not skip the whole tree below it.
15792 * Always ignore case here, dump_word() will check
15793 * proper case later. This isn't exactly right when
15794 * length changes for multi-byte characters with
15795 * ignore case... */
15796 if (depth <= patlen
15797 && MB_STRNICMP(word, pat, depth) != 0)
15807 * Dump one word: apply case modifications and append a line to the buffer.
15808 * When "lnum" is zero add insert mode completion.
15811 dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
15820 int keepcap = FALSE;
15823 char_u cword[MAXWLEN];
15824 char_u badword[MAXWLEN + 10];
15826 int flags = wordflags;
15828 if (dumpflags & DUMPFLAG_ONECAP)
15829 flags |= WF_ONECAP;
15830 if (dumpflags & DUMPFLAG_ALLCAP)
15831 flags |= WF_ALLCAP;
15833 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
15835 /* Need to fix case according to "flags". */
15836 make_case_word(word, cword, flags);
15842 if ((dumpflags & DUMPFLAG_KEEPCASE)
15843 && ((captype(word, NULL) & WF_KEEPCAP) == 0
15844 || (flags & WF_FIXCAP) != 0))
15851 /* Add flags and regions after a slash. */
15852 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
15854 STRCPY(badword, p);
15855 STRCAT(badword, "/");
15857 STRCAT(badword, "=");
15858 if (flags & WF_BANNED)
15859 STRCAT(badword, "!");
15860 else if (flags & WF_RARE)
15861 STRCAT(badword, "?");
15862 if (flags & WF_REGION)
15863 for (i = 0; i < 7; ++i)
15864 if (flags & (0x10000 << i))
15865 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15869 if (dumpflags & DUMPFLAG_COUNT)
15873 /* Include the word count for ":spelldump!". */
15874 hi = hash_find(&slang->sl_wordcount, tw);
15875 if (!HASHITEM_EMPTY(hi))
15877 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15878 tw, HI2WC(hi)->wc_count);
15883 ml_append(lnum, p, (colnr_T)0, FALSE);
15885 else if (((dumpflags & DUMPFLAG_ICASE)
15886 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15887 : STRNCMP(p, pat, STRLEN(pat)) == 0)
15888 && ins_compl_add_infercase(p, (int)STRLEN(p),
15889 p_ic, NULL, *dir, 0) == OK)
15890 /* if dir was BACKWARD then honor it just once */
15895 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15896 * "word" and append a line to the buffer.
15897 * When "lnum" is zero add insert mode completion.
15898 * Return the updated line number.
15901 dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
15903 char_u *word; /* case-folded word */
15907 int flags; /* flags with prefix ID */
15908 linenr_T startlnum;
15910 idx_T arridx[MAXWLEN];
15912 char_u prefix[MAXWLEN];
15913 char_u word_up[MAXWLEN];
15914 int has_word_up = FALSE;
15918 linenr_T lnum = startlnum;
15924 /* If the word starts with a lower-case letter make the word with an
15925 * upper-case letter in word_up[]. */
15926 c = PTR2CHAR(word);
15927 if (SPELL_TOUPPER(c) != c)
15929 onecap_copy(word, word_up, TRUE);
15930 has_word_up = TRUE;
15933 byts = slang->sl_pbyts;
15934 idxs = slang->sl_pidxs;
15935 if (byts != NULL) /* array not is empty */
15938 * Loop over all prefixes, building them byte-by-byte in prefix[].
15939 * When at the end of a prefix check that it supports "flags".
15944 while (depth >= 0 && !got_int)
15948 if (curi[depth] > len)
15950 /* Done all bytes at this node, go up one level. */
15956 /* Do one more byte at this node. */
15962 /* End of prefix, find out how many IDs there are. */
15963 for (i = 1; i < len; ++i)
15964 if (byts[n + i] != 0)
15966 curi[depth] += i - 1;
15968 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15971 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
15972 dump_word(slang, prefix, pat, dir, dumpflags,
15973 (c & WF_RAREPFX) ? (flags | WF_RARE)
15979 /* Check for prefix that matches the word when the
15980 * first letter is upper-case, but only if the prefix has
15984 c = valid_word_prefix(i, n, flags, word_up, slang,
15988 vim_strncpy(prefix + depth, word_up,
15989 MAXWLEN - depth - 1);
15990 dump_word(slang, prefix, pat, dir, dumpflags,
15991 (c & WF_RAREPFX) ? (flags | WF_RARE)
16000 /* Normal char, go one level deeper. */
16001 prefix[depth++] = c;
16002 arridx[depth] = idxs[n];
16013 * Move "p" to the end of word "start".
16014 * Uses the spell-checking word characters.
16017 spell_to_word_end(start, win)
16023 while (*p != NUL && spell_iswordp(p, win))
16028 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
16030 * For Insert mode completion CTRL-X s:
16031 * Find start of the word in front of column "startcol".
16032 * We don't check if it is badly spelled, with completion we can only change
16033 * the word in front of the cursor.
16034 * Returns the column number of the word.
16037 spell_word_start(startcol)
16044 if (no_spell_checking(curwin))
16047 /* Find a word character before "startcol". */
16048 line = ml_get_curline();
16049 for (p = line + startcol; p > line; )
16051 mb_ptr_back(line, p);
16052 if (spell_iswordp_nmw(p))
16056 /* Go back to start of the word. */
16059 col = (int)(p - line);
16060 mb_ptr_back(line, p);
16061 if (!spell_iswordp(p, curwin))
16070 * Need to check for 'spellcapcheck' now, the word is removed before
16071 * expand_spelling() is called. Therefore the ugly global variable.
16073 static int spell_expand_need_cap;
16076 spell_expand_check_cap(col)
16079 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16083 * Get list of spelling suggestions.
16084 * Used for Insert mode completion CTRL-X ?.
16085 * Returns the number of matches. The matches are in "matchp[]", array of
16086 * allocated strings.
16089 expand_spelling(lnum, pat, matchp)
16090 linenr_T lnum UNUSED;
16096 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
16097 *matchp = ga.ga_data;
16102 #endif /* FEAT_SPELL */