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 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())
306 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
307 # include "vimio.h" /* for lseek(), must be before vim.h */
312 #if defined(FEAT_SPELL) || defined(PROTO)
318 #ifndef UNIX /* it's in os_unix.h for Unix */
319 # include <time.h> /* for time_t */
322 #define MAXWLEN 250 /* Assume max. word len is this many bytes.
323 Some places assume a word length fits in a
324 byte, thus it can't be above 255. */
326 /* Type used for indexes in the word tree need to be at least 4 bytes. If int
327 * is 8 bytes we could use something smaller, but what? */
334 /* Flags used for a word. Only the lowest byte can be used, the region byte
336 #define WF_REGION 0x01 /* region byte follows */
337 #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
338 #define WF_ALLCAP 0x04 /* word must be all capitals */
339 #define WF_RARE 0x08 /* rare word */
340 #define WF_BANNED 0x10 /* bad word */
341 #define WF_AFX 0x20 /* affix ID follows */
342 #define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
343 #define WF_KEEPCAP 0x80 /* keep-case word */
345 /* for <flags2>, shifted up one byte to be used in wn_flags */
346 #define WF_HAS_AFF 0x0100 /* word includes affix */
347 #define WF_NEEDCOMP 0x0200 /* word only valid in compound */
348 #define WF_NOSUGGEST 0x0400 /* word not to be suggested */
349 #define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
350 #define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
351 #define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
353 /* only used for su_badflags */
354 #define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
356 #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
358 /* flags for <pflags> */
359 #define WFP_RARE 0x01 /* rare prefix */
360 #define WFP_NC 0x02 /* prefix is not combining */
361 #define WFP_UP 0x04 /* to-upper prefix */
362 #define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
363 #define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
365 /* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
366 * byte) and prefcondnr (two bytes). */
367 #define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
368 #define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
369 #define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
370 #define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
371 * COMPOUNDPERMITFLAG */
372 #define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
373 * COMPOUNDFORBIDFLAG */
376 /* flags for <compoptions> */
377 #define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
378 #define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
379 #define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
380 #define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
382 /* Special byte values for <byte>. Some are only used in the tree for
383 * postponed prefixes, some only in the other trees. This is a bit messy... */
384 #define BY_NOFLAGS 0 /* end of word without flags or region; for
385 * postponed prefix: no <pflags> */
386 #define BY_INDEX 1 /* child is shared, index follows */
387 #define BY_FLAGS 2 /* end of word, <flags> byte follows; for
388 * postponed prefix: <pflags> follows */
389 #define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
390 * follow; never used in prefix tree */
391 #define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
393 /* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
394 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
395 * One replacement: from "ft_from" to "ft_to". */
396 typedef struct fromto_S
402 /* Info from "SAL" entries in ".aff" file used in sl_sal.
403 * The info is split for quick processing by spell_soundfold().
404 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
405 typedef struct salitem_S
407 char_u
*sm_lead
; /* leading letters */
408 int sm_leadlen
; /* length of "sm_lead" */
409 char_u
*sm_oneof
; /* letters from () or NULL */
410 char_u
*sm_rules
; /* rules like ^, $, priority */
411 char_u
*sm_to
; /* replacement. */
413 int *sm_lead_w
; /* wide character copy of "sm_lead" */
414 int *sm_oneof_w
; /* wide character copy of "sm_oneof" */
415 int *sm_to_w
; /* wide character copy of "sm_to" */
420 typedef int salfirst_T
;
422 typedef short salfirst_T
;
425 /* Values for SP_*ERROR are negative, positive values are used by
426 * read_cnt_string(). */
427 #define SP_TRUNCERROR -1 /* spell file truncated error */
428 #define SP_FORMERROR -2 /* format error in spell file */
429 #define SP_OTHERERROR -3 /* other error while reading spell file */
432 * Structure used to store words and other info for one language, loaded from
434 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
435 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
437 * The "byts" array stores the possible bytes in each tree node, preceded by
438 * the number of possible bytes, sorted on byte value:
439 * <len> <byte1> <byte2> ...
440 * The "idxs" array stores the index of the child node corresponding to the
442 * Exception: when the byte is zero, the word may end here and "idxs" holds
443 * the flags, region mask and affixID for the word. There may be several
444 * zeros in sequence for alternative flag/region/affixID combinations.
446 typedef struct slang_S slang_T
;
449 slang_T
*sl_next
; /* next language */
450 char_u
*sl_name
; /* language name "en", "en.rare", "nl", etc. */
451 char_u
*sl_fname
; /* name of .spl file */
452 int sl_add
; /* TRUE if it's a .add file. */
454 char_u
*sl_fbyts
; /* case-folded word bytes */
455 idx_T
*sl_fidxs
; /* case-folded word indexes */
456 char_u
*sl_kbyts
; /* keep-case word bytes */
457 idx_T
*sl_kidxs
; /* keep-case word indexes */
458 char_u
*sl_pbyts
; /* prefix tree word bytes */
459 idx_T
*sl_pidxs
; /* prefix tree word indexes */
461 char_u
*sl_info
; /* infotext string or NULL */
463 char_u sl_regions
[17]; /* table with up to 8 region names plus NUL */
465 char_u
*sl_midword
; /* MIDWORD string or NULL */
467 hashtab_T sl_wordcount
; /* hashtable with word count, wordcount_T */
469 int sl_compmax
; /* COMPOUNDWORDMAX (default: MAXWLEN) */
470 int sl_compminlen
; /* COMPOUNDMIN (default: 0) */
471 int sl_compsylmax
; /* COMPOUNDSYLMAX (default: MAXWLEN) */
472 int sl_compoptions
; /* COMP_* flags */
473 garray_T sl_comppat
; /* CHECKCOMPOUNDPATTERN items */
474 regprog_T
*sl_compprog
; /* COMPOUNDRULE turned into a regexp progrm
475 * (NULL when no compounding) */
476 char_u
*sl_compstartflags
; /* flags for first compound word */
477 char_u
*sl_compallflags
; /* all flags for compound words */
478 char_u sl_nobreak
; /* When TRUE: no spaces between words */
479 char_u
*sl_syllable
; /* SYLLABLE repeatable chars or NULL */
480 garray_T sl_syl_items
; /* syllable items */
482 int sl_prefixcnt
; /* number of items in "sl_prefprog" */
483 regprog_T
**sl_prefprog
; /* table with regprogs for prefixes */
485 garray_T sl_rep
; /* list of fromto_T entries from REP lines */
486 short sl_rep_first
[256]; /* indexes where byte first appears, -1 if
488 garray_T sl_sal
; /* list of salitem_T entries from SAL lines */
489 salfirst_T sl_sal_first
[256]; /* indexes where byte first appears, -1 if
491 int sl_followup
; /* SAL followup */
492 int sl_collapse
; /* SAL collapse_result */
493 int sl_rem_accents
; /* SAL remove_accents */
494 int sl_sofo
; /* SOFOFROM and SOFOTO instead of SAL items:
495 * "sl_sal_first" maps chars, when has_mbyte
496 * "sl_sal" is a list of wide char lists. */
497 garray_T sl_repsal
; /* list of fromto_T entries from REPSAL lines */
498 short sl_repsal_first
[256]; /* sl_rep_first for REPSAL lines */
499 int sl_nosplitsugs
; /* don't suggest splitting a word */
501 /* Info from the .sug file. Loaded on demand. */
502 time_t sl_sugtime
; /* timestamp for .sug file */
503 char_u
*sl_sbyts
; /* soundfolded word bytes */
504 idx_T
*sl_sidxs
; /* soundfolded word indexes */
505 buf_T
*sl_sugbuf
; /* buffer with word number table */
506 int sl_sugloaded
; /* TRUE when .sug file was loaded or failed to
509 int sl_has_map
; /* TRUE if there is a MAP line */
511 hashtab_T sl_map_hash
; /* MAP for multi-byte chars */
512 int sl_map_array
[256]; /* MAP for first 256 chars */
514 char_u sl_map_array
[256]; /* MAP for first 256 chars */
516 hashtab_T sl_sounddone
; /* table with soundfolded words that have
517 handled, see add_sound_suggest() */
520 /* First language that is loaded, start of the linked list of loaded
522 static slang_T
*first_lang
= NULL
;
524 /* Flags used in .spl file for soundsalike flags. */
525 #define SAL_F0LLOWUP 1
526 #define SAL_COLLAPSE 2
527 #define SAL_REM_ACCENTS 4
530 * Structure used in "b_langp", filled from 'spelllang'.
532 typedef struct langp_S
534 slang_T
*lp_slang
; /* info for this language */
535 slang_T
*lp_sallang
; /* language used for sound folding or NULL */
536 slang_T
*lp_replang
; /* language used for REP items or NULL */
537 int lp_region
; /* bitmask for region or REGION_ALL */
540 #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
542 #define REGION_ALL 0xff /* word valid in all regions */
544 #define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
545 #define VIMSPELLMAGICL 8
546 #define VIMSPELLVERSION 50
548 #define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
549 #define VIMSUGMAGICL 6
550 #define VIMSUGVERSION 1
552 /* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
553 #define SN_REGION 0 /* <regionname> section */
554 #define SN_CHARFLAGS 1 /* charflags section */
555 #define SN_MIDWORD 2 /* <midword> section */
556 #define SN_PREFCOND 3 /* <prefcond> section */
557 #define SN_REP 4 /* REP items section */
558 #define SN_SAL 5 /* SAL items section */
559 #define SN_SOFO 6 /* soundfolding section */
560 #define SN_MAP 7 /* MAP items section */
561 #define SN_COMPOUND 8 /* compound words section */
562 #define SN_SYLLABLE 9 /* syllable section */
563 #define SN_NOBREAK 10 /* NOBREAK section */
564 #define SN_SUGFILE 11 /* timestamp for .sug file */
565 #define SN_REPSAL 12 /* REPSAL items section */
566 #define SN_WORDS 13 /* common words */
567 #define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
568 #define SN_INFO 15 /* info section */
569 #define SN_END 255 /* end of sections */
571 #define SNF_REQUIRED 1 /* <sectionflags>: required section */
573 /* Result values. Lower number is accepted over higher one. */
580 /* file used for "zG" and "zW" */
581 static char_u
*int_wordlist
= NULL
;
583 typedef struct wordcount_S
585 short_u wc_count
; /* nr of times word was seen */
586 char_u wc_word
[1]; /* word, actually longer */
589 static wordcount_T dumwc
;
590 #define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
591 #define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
592 #define MAXWORDCOUNT 0xffff
595 * Information used when looking for suggestions.
597 typedef struct suginfo_S
599 garray_T su_ga
; /* suggestions, contains "suggest_T" */
600 int su_maxcount
; /* max. number of suggestions displayed */
601 int su_maxscore
; /* maximum score for adding to su_ga */
602 int su_sfmaxscore
; /* idem, for when doing soundfold words */
603 garray_T su_sga
; /* like su_ga, sound-folded scoring */
604 char_u
*su_badptr
; /* start of bad word in line */
605 int su_badlen
; /* length of detected bad word in line */
606 int su_badflags
; /* caps flags for bad word */
607 char_u su_badword
[MAXWLEN
]; /* bad word truncated at su_badlen */
608 char_u su_fbadword
[MAXWLEN
]; /* su_badword case-folded */
609 char_u su_sal_badword
[MAXWLEN
]; /* su_badword soundfolded */
610 hashtab_T su_banned
; /* table with banned words */
611 slang_T
*su_sallang
; /* default language for sound folding */
614 /* One word suggestion. Used in "si_ga". */
615 typedef struct suggest_S
617 char_u
*st_word
; /* suggested word, allocated string */
618 int st_wordlen
; /* STRLEN(st_word) */
619 int st_orglen
; /* length of replaced text */
620 int st_score
; /* lower is better */
621 int st_altscore
; /* used when st_score compares equal */
622 int st_salscore
; /* st_score is for soundalike */
623 int st_had_bonus
; /* bonus already included in score */
624 slang_T
*st_slang
; /* language used for sound folding */
627 #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
629 /* TRUE if a word appears in the list of banned words. */
630 #define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
632 /* Number of suggestions kept when cleaning up. we need to keep more than
633 * what is displayed, because when rescore_suggestions() is called the score
634 * may change and wrong suggestions may be removed later. */
635 #define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
637 /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
638 * of suggestions that are not going to be displayed. */
639 #define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
641 /* score for various changes */
642 #define SCORE_SPLIT 149 /* split bad word */
643 #define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
644 #define SCORE_ICASE 52 /* slightly different case */
645 #define SCORE_REGION 200 /* word is for different region */
646 #define SCORE_RARE 180 /* rare word */
647 #define SCORE_SWAP 75 /* swap two characters */
648 #define SCORE_SWAP3 110 /* swap two characters in three */
649 #define SCORE_REP 65 /* REP replacement */
650 #define SCORE_SUBST 93 /* substitute a character */
651 #define SCORE_SIMILAR 33 /* substitute a similar character */
652 #define SCORE_SUBCOMP 33 /* substitute a composing character */
653 #define SCORE_DEL 94 /* delete a character */
654 #define SCORE_DELDUP 66 /* delete a duplicated character */
655 #define SCORE_DELCOMP 28 /* delete a composing character */
656 #define SCORE_INS 96 /* insert a character */
657 #define SCORE_INSDUP 67 /* insert a duplicate character */
658 #define SCORE_INSCOMP 30 /* insert a composing character */
659 #define SCORE_NONWORD 103 /* change non-word to word char */
661 #define SCORE_FILE 30 /* suggestion from a file */
662 #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
663 * 350 allows for about three changes. */
665 #define SCORE_COMMON1 30 /* subtracted for words seen before */
666 #define SCORE_COMMON2 40 /* subtracted for words often seen */
667 #define SCORE_COMMON3 50 /* subtracted for words very often seen */
668 #define SCORE_THRES2 10 /* word count threshold for COMMON2 */
669 #define SCORE_THRES3 100 /* word count threshold for COMMON3 */
671 /* When trying changed soundfold words it becomes slow when trying more than
672 * two changes. With less then two changes it's slightly faster but we miss a
673 * few good suggestions. In rare cases we need to try three of four changes.
675 #define SCORE_SFMAX1 200 /* maximum score for first try */
676 #define SCORE_SFMAX2 300 /* maximum score for second try */
677 #define SCORE_SFMAX3 400 /* maximum score for third try */
679 #define SCORE_BIG SCORE_INS * 3 /* big difference */
680 #define SCORE_MAXMAX 999999 /* accept any score */
681 #define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
683 /* for spell_edit_score_limit() we need to know the minimum value of
684 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
685 #define SCORE_EDIT_MIN SCORE_SIMILAR
688 * Structure to store info for word matching.
690 typedef struct matchinf_S
692 langp_T
*mi_lp
; /* info for language and region */
694 /* pointers to original text to be checked */
695 char_u
*mi_word
; /* start of word being checked */
696 char_u
*mi_end
; /* end of matching word so far */
697 char_u
*mi_fend
; /* next char to be added to mi_fword */
698 char_u
*mi_cend
; /* char after what was used for
701 /* case-folded text */
702 char_u mi_fword
[MAXWLEN
+ 1]; /* mi_word case-folded */
703 int mi_fwordlen
; /* nr of valid bytes in mi_fword */
705 /* for when checking word after a prefix */
706 int mi_prefarridx
; /* index in sl_pidxs with list of
708 int mi_prefcnt
; /* number of entries at mi_prefarridx */
709 int mi_prefixlen
; /* byte length of prefix */
711 int mi_cprefixlen
; /* byte length of prefix in original
714 # define mi_cprefixlen mi_prefixlen /* it's the same value */
717 /* for when checking a compound word */
718 int mi_compoff
; /* start of following word offset */
719 char_u mi_compflags
[MAXWLEN
]; /* flags for compound words used */
720 int mi_complen
; /* nr of compound words used */
721 int mi_compextra
; /* nr of COMPOUNDROOT words */
724 int mi_result
; /* result so far: SP_BAD, SP_OK, etc. */
725 int mi_capflags
; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
726 buf_T
*mi_buf
; /* buffer being checked */
729 int mi_result2
; /* "mi_resul" without following word */
730 char_u
*mi_end2
; /* "mi_end" without following word */
734 * The tables used for recognizing word characters according to spelling.
735 * These are only used for the first 256 characters of 'encoding'.
737 typedef struct spelltab_S
739 char_u st_isw
[256]; /* flags: is word char */
740 char_u st_isu
[256]; /* flags: is uppercase char */
741 char_u st_fold
[256]; /* chars: folded case */
742 char_u st_upper
[256]; /* chars: upper case */
745 static spelltab_T spelltab
;
746 static int did_set_spelltab
;
749 #define CF_UPPER 0x02
751 static void clear_spell_chartab
__ARGS((spelltab_T
*sp
));
752 static int set_spell_finish
__ARGS((spelltab_T
*new_st
));
753 static int spell_iswordp
__ARGS((char_u
*p
, buf_T
*buf
));
754 static int spell_iswordp_nmw
__ARGS((char_u
*p
));
756 static int spell_iswordp_w
__ARGS((int *p
, buf_T
*buf
));
758 static int write_spell_prefcond
__ARGS((FILE *fd
, garray_T
*gap
));
761 * For finding suggestions: At each node in the tree these states are tried:
765 STATE_START
= 0, /* At start of node check for NUL bytes (goodword
766 * ends); if badword ends there is a match, otherwise
767 * try splitting word. */
768 STATE_NOPREFIX
, /* try without prefix */
769 STATE_SPLITUNDO
, /* Undo splitting. */
770 STATE_ENDNUL
, /* Past NUL bytes at start of the node. */
771 STATE_PLAIN
, /* Use each byte of the node. */
772 STATE_DEL
, /* Delete a byte from the bad word. */
773 STATE_INS_PREP
, /* Prepare for inserting bytes. */
774 STATE_INS
, /* Insert a byte in the bad word. */
775 STATE_SWAP
, /* Swap two bytes. */
776 STATE_UNSWAP
, /* Undo swap two characters. */
777 STATE_SWAP3
, /* Swap two characters over three. */
778 STATE_UNSWAP3
, /* Undo Swap two characters over three. */
779 STATE_UNROT3L
, /* Undo rotate three characters left */
780 STATE_UNROT3R
, /* Undo rotate three characters right */
781 STATE_REP_INI
, /* Prepare for using REP items. */
782 STATE_REP
, /* Use matching REP items from the .aff file. */
783 STATE_REP_UNDO
, /* Undo a REP item replacement. */
784 STATE_FINAL
/* End of this node. */
788 * Struct to keep the state at each level in suggest_try_change().
790 typedef struct trystate_S
792 state_T ts_state
; /* state at this level, STATE_ */
793 int ts_score
; /* score */
794 idx_T ts_arridx
; /* index in tree array, start of node */
795 short ts_curi
; /* index in list of child nodes */
796 char_u ts_fidx
; /* index in fword[], case-folded bad word */
797 char_u ts_fidxtry
; /* ts_fidx at which bytes may be changed */
798 char_u ts_twordlen
; /* valid length of tword[] */
799 char_u ts_prefixdepth
; /* stack depth for end of prefix or
800 * PFD_PREFIXTREE or PFD_NOPREFIX */
801 char_u ts_flags
; /* TSF_ flags */
803 char_u ts_tcharlen
; /* number of bytes in tword character */
804 char_u ts_tcharidx
; /* current byte index in tword character */
805 char_u ts_isdiff
; /* DIFF_ values */
806 char_u ts_fcharstart
; /* index in fword where badword char started */
808 char_u ts_prewordlen
; /* length of word in "preword[]" */
809 char_u ts_splitoff
; /* index in "tword" after last split */
810 char_u ts_splitfidx
; /* "ts_fidx" at word split */
811 char_u ts_complen
; /* nr of compound words used */
812 char_u ts_compsplit
; /* index for "compflags" where word was spit */
813 char_u ts_save_badflags
; /* su_badflags saved here */
814 char_u ts_delidx
; /* index in fword for char that was deleted,
815 valid when "ts_flags" has TSF_DIDDEL */
818 /* values for ts_isdiff */
819 #define DIFF_NONE 0 /* no different byte (yet) */
820 #define DIFF_YES 1 /* different byte found */
821 #define DIFF_INSERT 2 /* inserting character */
823 /* values for ts_flags */
824 #define TSF_PREFIXOK 1 /* already checked that prefix is OK */
825 #define TSF_DIDSPLIT 2 /* tried split at this point */
826 #define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
828 /* special values ts_prefixdepth */
829 #define PFD_NOPREFIX 0xff /* not using prefixes */
830 #define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
831 #define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
833 /* mode values for find_word */
834 #define FIND_FOLDWORD 0 /* find word case-folded */
835 #define FIND_KEEPWORD 1 /* find keep-case word */
836 #define FIND_PREFIX 2 /* find word after prefix */
837 #define FIND_COMPOUND 3 /* find case-folded compound word */
838 #define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
840 static slang_T
*slang_alloc
__ARGS((char_u
*lang
));
841 static void slang_free
__ARGS((slang_T
*lp
));
842 static void slang_clear
__ARGS((slang_T
*lp
));
843 static void slang_clear_sug
__ARGS((slang_T
*lp
));
844 static void find_word
__ARGS((matchinf_T
*mip
, int mode
));
845 static int can_compound
__ARGS((slang_T
*slang
, char_u
*word
, char_u
*flags
));
846 static int valid_word_prefix
__ARGS((int totprefcnt
, int arridx
, int flags
, char_u
*word
, slang_T
*slang
, int cond_req
));
847 static void find_prefix
__ARGS((matchinf_T
*mip
, int mode
));
848 static int fold_more
__ARGS((matchinf_T
*mip
));
849 static int spell_valid_case
__ARGS((int wordflags
, int treeflags
));
850 static int no_spell_checking
__ARGS((win_T
*wp
));
851 static void spell_load_lang
__ARGS((char_u
*lang
));
852 static char_u
*spell_enc
__ARGS((void));
853 static void int_wordlist_spl
__ARGS((char_u
*fname
));
854 static void spell_load_cb
__ARGS((char_u
*fname
, void *cookie
));
855 static slang_T
*spell_load_file
__ARGS((char_u
*fname
, char_u
*lang
, slang_T
*old_lp
, int silent
));
856 static int get2c
__ARGS((FILE *fd
));
857 static int get3c
__ARGS((FILE *fd
));
858 static int get4c
__ARGS((FILE *fd
));
859 static time_t get8c
__ARGS((FILE *fd
));
860 static char_u
*read_cnt_string
__ARGS((FILE *fd
, int cnt_bytes
, int *lenp
));
861 static char_u
*read_string
__ARGS((FILE *fd
, int cnt
));
862 static int read_region_section
__ARGS((FILE *fd
, slang_T
*slang
, int len
));
863 static int read_charflags_section
__ARGS((FILE *fd
));
864 static int read_prefcond_section
__ARGS((FILE *fd
, slang_T
*lp
));
865 static int read_rep_section
__ARGS((FILE *fd
, garray_T
*gap
, short *first
));
866 static int read_sal_section
__ARGS((FILE *fd
, slang_T
*slang
));
867 static int read_words_section
__ARGS((FILE *fd
, slang_T
*lp
, int len
));
868 static void count_common_word
__ARGS((slang_T
*lp
, char_u
*word
, int len
, int count
));
869 static int score_wordcount_adj
__ARGS((slang_T
*slang
, int score
, char_u
*word
, int split
));
870 static int read_sofo_section
__ARGS((FILE *fd
, slang_T
*slang
));
871 static int read_compound
__ARGS((FILE *fd
, slang_T
*slang
, int len
));
872 static int byte_in_str
__ARGS((char_u
*str
, int byte
));
873 static int init_syl_tab
__ARGS((slang_T
*slang
));
874 static int count_syllables
__ARGS((slang_T
*slang
, char_u
*word
));
875 static int set_sofo
__ARGS((slang_T
*lp
, char_u
*from
, char_u
*to
));
876 static void set_sal_first
__ARGS((slang_T
*lp
));
878 static int *mb_str2wide
__ARGS((char_u
*s
));
880 static int spell_read_tree
__ARGS((FILE *fd
, char_u
**bytsp
, idx_T
**idxsp
, int prefixtree
, int prefixcnt
));
881 static idx_T read_tree_node
__ARGS((FILE *fd
, char_u
*byts
, idx_T
*idxs
, int maxidx
, int startidx
, int prefixtree
, int maxprefcondnr
));
882 static void clear_midword
__ARGS((buf_T
*buf
));
883 static void use_midword
__ARGS((slang_T
*lp
, buf_T
*buf
));
884 static int find_region
__ARGS((char_u
*rp
, char_u
*region
));
885 static int captype
__ARGS((char_u
*word
, char_u
*end
));
886 static int badword_captype
__ARGS((char_u
*word
, char_u
*end
));
887 static void spell_reload_one
__ARGS((char_u
*fname
, int added_word
));
888 static void set_spell_charflags
__ARGS((char_u
*flags
, int cnt
, char_u
*upp
));
889 static int set_spell_chartab
__ARGS((char_u
*fol
, char_u
*low
, char_u
*upp
));
890 static int spell_casefold
__ARGS((char_u
*p
, int len
, char_u
*buf
, int buflen
));
891 static int check_need_cap
__ARGS((linenr_T lnum
, colnr_T col
));
892 static void spell_find_suggest
__ARGS((char_u
*badptr
, int badlen
, suginfo_T
*su
, int maxcount
, int banbadword
, int need_cap
, int interactive
));
894 static void spell_suggest_expr
__ARGS((suginfo_T
*su
, char_u
*expr
));
896 static void spell_suggest_file
__ARGS((suginfo_T
*su
, char_u
*fname
));
897 static void spell_suggest_intern
__ARGS((suginfo_T
*su
, int interactive
));
898 static void suggest_load_files
__ARGS((void));
899 static void tree_count_words
__ARGS((char_u
*byts
, idx_T
*idxs
));
900 static void spell_find_cleanup
__ARGS((suginfo_T
*su
));
901 static void onecap_copy
__ARGS((char_u
*word
, char_u
*wcopy
, int upper
));
902 static void allcap_copy
__ARGS((char_u
*word
, char_u
*wcopy
));
903 static void suggest_try_special
__ARGS((suginfo_T
*su
));
904 static void suggest_try_change
__ARGS((suginfo_T
*su
));
905 static void suggest_trie_walk
__ARGS((suginfo_T
*su
, langp_T
*lp
, char_u
*fword
, int soundfold
));
906 static void go_deeper
__ARGS((trystate_T
*stack
, int depth
, int score_add
));
908 static int nofold_len
__ARGS((char_u
*fword
, int flen
, char_u
*word
));
910 static void find_keepcap_word
__ARGS((slang_T
*slang
, char_u
*fword
, char_u
*kword
));
911 static void score_comp_sal
__ARGS((suginfo_T
*su
));
912 static void score_combine
__ARGS((suginfo_T
*su
));
913 static int stp_sal_score
__ARGS((suggest_T
*stp
, suginfo_T
*su
, slang_T
*slang
, char_u
*badsound
));
914 static void suggest_try_soundalike_prep
__ARGS((void));
915 static void suggest_try_soundalike
__ARGS((suginfo_T
*su
));
916 static void suggest_try_soundalike_finish
__ARGS((void));
917 static void add_sound_suggest
__ARGS((suginfo_T
*su
, char_u
*goodword
, int score
, langp_T
*lp
));
918 static int soundfold_find
__ARGS((slang_T
*slang
, char_u
*word
));
919 static void make_case_word
__ARGS((char_u
*fword
, char_u
*cword
, int flags
));
920 static void set_map_str
__ARGS((slang_T
*lp
, char_u
*map
));
921 static int similar_chars
__ARGS((slang_T
*slang
, int c1
, int c2
));
922 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
));
923 static void check_suggestions
__ARGS((suginfo_T
*su
, garray_T
*gap
));
924 static void add_banned
__ARGS((suginfo_T
*su
, char_u
*word
));
925 static void rescore_suggestions
__ARGS((suginfo_T
*su
));
926 static void rescore_one
__ARGS((suginfo_T
*su
, suggest_T
*stp
));
927 static int cleanup_suggestions
__ARGS((garray_T
*gap
, int maxscore
, int keep
));
928 static void spell_soundfold
__ARGS((slang_T
*slang
, char_u
*inword
, int folded
, char_u
*res
));
929 static void spell_soundfold_sofo
__ARGS((slang_T
*slang
, char_u
*inword
, char_u
*res
));
930 static void spell_soundfold_sal
__ARGS((slang_T
*slang
, char_u
*inword
, char_u
*res
));
932 static void spell_soundfold_wsal
__ARGS((slang_T
*slang
, char_u
*inword
, char_u
*res
));
934 static int soundalike_score
__ARGS((char_u
*goodsound
, char_u
*badsound
));
935 static int spell_edit_score
__ARGS((slang_T
*slang
, char_u
*badword
, char_u
*goodword
));
936 static int spell_edit_score_limit
__ARGS((slang_T
*slang
, char_u
*badword
, char_u
*goodword
, int limit
));
938 static int spell_edit_score_limit_w
__ARGS((slang_T
*slang
, char_u
*badword
, char_u
*goodword
, int limit
));
940 static void dump_word
__ARGS((slang_T
*slang
, char_u
*word
, char_u
*pat
, int *dir
, int round
, int flags
, linenr_T lnum
));
941 static linenr_T dump_prefixes
__ARGS((slang_T
*slang
, char_u
*word
, char_u
*pat
, int *dir
, int round
, int flags
, linenr_T startlnum
));
942 static buf_T
*open_spellbuf
__ARGS((void));
943 static void close_spellbuf
__ARGS((buf_T
*buf
));
946 * Use our own character-case definitions, because the current locale may
947 * differ from what the .spl file uses.
948 * These must not be called with negative number!
951 /* Non-multi-byte implementation. */
952 # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
953 # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
954 # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
956 # if defined(HAVE_WCHAR_H)
957 # include <wchar.h> /* for towupper() and towlower() */
959 /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
960 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
961 * the "w" library function for characters above 255 if available. */
962 # ifdef HAVE_TOWLOWER
963 # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
964 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
966 # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
967 : (c) < 256 ? spelltab.st_fold[c] : (c))
970 # ifdef HAVE_TOWUPPER
971 # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
972 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
974 # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
975 : (c) < 256 ? spelltab.st_upper[c] : (c))
978 # ifdef HAVE_ISWUPPER
979 # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
980 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
982 # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
983 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
988 static char *e_format
= N_("E759: Format error in spell file");
989 static char *e_spell_trunc
= N_("E758: Truncated spell file");
990 static char *e_afftrailing
= N_("Trailing text in %s line %d: %s");
991 static char *e_affname
= N_("Affix name too long in %s line %d: %s");
992 static char *e_affform
= N_("E761: Format error in affix file FOL, LOW or UPP");
993 static char *e_affrange
= N_("E762: Character in FOL, LOW or UPP is out of range");
994 static char *msg_compressing
= N_("Compressing word tree...");
996 /* Remember what "z?" replaced. */
997 static char_u
*repl_from
= NULL
;
998 static char_u
*repl_to
= NULL
;
1001 * Main spell-checking function.
1002 * "ptr" points to a character that could be the start of a word.
1003 * "*attrp" is set to the highlight index for a badly spelled word. For a
1004 * non-word or when it's OK it remains unchanged.
1005 * This must only be called when 'spelllang' is not empty.
1007 * "capcol" is used to check for a Capitalised word after the end of a
1008 * sentence. If it's zero then perform the check. Return the column where to
1009 * check next, or -1 when no sentence end was found. If it's NULL then don't
1012 * Returns the length of the word in bytes, also when it's OK, so that the
1013 * caller can skip over the word.
1016 spell_check(wp
, ptr
, attrp
, capcol
, docount
)
1017 win_T
*wp
; /* current window */
1020 int *capcol
; /* column to check for Capital */
1021 int docount
; /* count good words */
1023 matchinf_T mi
; /* Most things are put in "mi" so that it can
1024 be passed to functions quickly. */
1025 int nrlen
= 0; /* found a number first */
1027 int wrongcaplen
= 0;
1029 int count_word
= docount
;
1031 /* A word never starts at a space or a control character. Return quickly
1032 * then, skipping over the character. */
1036 /* Return here when loading language files failed. */
1037 if (wp
->w_buffer
->b_langp
.ga_len
== 0)
1040 vim_memset(&mi
, 0, sizeof(matchinf_T
));
1042 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
1043 * 0X99FF. But always do check spelling to find "3GPP" and "11
1045 if (*ptr
>= '0' && *ptr
<= '9')
1047 if (*ptr
== '0' && (ptr
[1] == 'x' || ptr
[1] == 'X'))
1048 mi
.mi_end
= skiphex(ptr
+ 2);
1050 mi
.mi_end
= skipdigits(ptr
);
1051 nrlen
= (int)(mi
.mi_end
- ptr
);
1054 /* Find the normal end of the word (until the next non-word character). */
1057 if (spell_iswordp(mi
.mi_fend
, wp
->w_buffer
))
1061 mb_ptr_adv(mi
.mi_fend
);
1062 } while (*mi
.mi_fend
!= NUL
&& spell_iswordp(mi
.mi_fend
, wp
->w_buffer
));
1064 if (capcol
!= NULL
&& *capcol
== 0 && wp
->w_buffer
->b_cap_prog
!= NULL
)
1066 /* Check word starting with capital letter. */
1068 if (!SPELL_ISUPPER(c
))
1069 wrongcaplen
= (int)(mi
.mi_fend
- ptr
);
1075 /* We always use the characters up to the next non-word character,
1076 * also for bad words. */
1077 mi
.mi_end
= mi
.mi_fend
;
1079 /* Check caps type later. */
1080 mi
.mi_buf
= wp
->w_buffer
;
1082 /* case-fold the word with one non-word character, so that we can check
1083 * for the word end. */
1084 if (*mi
.mi_fend
!= NUL
)
1085 mb_ptr_adv(mi
.mi_fend
);
1087 (void)spell_casefold(ptr
, (int)(mi
.mi_fend
- ptr
), mi
.mi_fword
,
1089 mi
.mi_fwordlen
= (int)STRLEN(mi
.mi_fword
);
1091 /* The word is bad unless we recognize it. */
1092 mi
.mi_result
= SP_BAD
;
1093 mi
.mi_result2
= SP_BAD
;
1096 * Loop over the languages specified in 'spelllang'.
1097 * We check them all, because a word may be matched longer in another
1100 for (lpi
= 0; lpi
< wp
->w_buffer
->b_langp
.ga_len
; ++lpi
)
1102 mi
.mi_lp
= LANGP_ENTRY(wp
->w_buffer
->b_langp
, lpi
);
1104 /* If reloading fails the language is still in the list but everything
1105 * has been cleared. */
1106 if (mi
.mi_lp
->lp_slang
->sl_fidxs
== NULL
)
1109 /* Check for a matching word in case-folded words. */
1110 find_word(&mi
, FIND_FOLDWORD
);
1112 /* Check for a matching word in keep-case words. */
1113 find_word(&mi
, FIND_KEEPWORD
);
1115 /* Check for matching prefixes. */
1116 find_prefix(&mi
, FIND_FOLDWORD
);
1118 /* For a NOBREAK language, may want to use a word without a following
1119 * word as a backup. */
1120 if (mi
.mi_lp
->lp_slang
->sl_nobreak
&& mi
.mi_result
== SP_BAD
1121 && mi
.mi_result2
!= SP_BAD
)
1123 mi
.mi_result
= mi
.mi_result2
;
1124 mi
.mi_end
= mi
.mi_end2
;
1127 /* Count the word in the first language where it's found to be OK. */
1128 if (count_word
&& mi
.mi_result
== SP_OK
)
1130 count_common_word(mi
.mi_lp
->lp_slang
, ptr
,
1131 (int)(mi
.mi_end
- ptr
), 1);
1136 if (mi
.mi_result
!= SP_OK
)
1138 /* If we found a number skip over it. Allows for "42nd". Do flag
1139 * rare and local words, e.g., "3GPP". */
1142 if (mi
.mi_result
== SP_BAD
|| mi
.mi_result
== SP_BANNED
)
1146 /* When we are at a non-word character there is no error, just
1147 * skip over the character (try looking for a word after it). */
1148 else if (!spell_iswordp_nmw(ptr
))
1150 if (capcol
!= NULL
&& wp
->w_buffer
->b_cap_prog
!= NULL
)
1152 regmatch_T regmatch
;
1154 /* Check for end of sentence. */
1155 regmatch
.regprog
= wp
->w_buffer
->b_cap_prog
;
1156 regmatch
.rm_ic
= FALSE
;
1157 if (vim_regexec(®match
, ptr
, 0))
1158 *capcol
= (int)(regmatch
.endp
[0] - ptr
);
1163 return (*mb_ptr2len
)(ptr
);
1167 else if (mi
.mi_end
== ptr
)
1168 /* Always include at least one character. Required for when there
1169 * is a mixup in "midword". */
1170 mb_ptr_adv(mi
.mi_end
);
1171 else if (mi
.mi_result
== SP_BAD
1172 && LANGP_ENTRY(wp
->w_buffer
->b_langp
, 0)->lp_slang
->sl_nobreak
)
1175 int save_result
= mi
.mi_result
;
1177 /* First language in 'spelllang' is NOBREAK. Find first position
1178 * at which any word would be valid. */
1179 mi
.mi_lp
= LANGP_ENTRY(wp
->w_buffer
->b_langp
, 0);
1180 if (mi
.mi_lp
->lp_slang
->sl_fidxs
!= NULL
)
1190 mi
.mi_compoff
= (int)(fp
- mi
.mi_fword
);
1191 find_word(&mi
, FIND_COMPOUND
);
1192 if (mi
.mi_result
!= SP_BAD
)
1198 mi
.mi_result
= save_result
;
1202 if (mi
.mi_result
== SP_BAD
|| mi
.mi_result
== SP_BANNED
)
1204 else if (mi
.mi_result
== SP_RARE
)
1210 if (wrongcaplen
> 0 && (mi
.mi_result
== SP_OK
|| mi
.mi_result
== SP_RARE
))
1212 /* Report SpellCap only when the word isn't badly spelled. */
1217 return (int)(mi
.mi_end
- ptr
);
1221 * Check if the word at "mip->mi_word" is in the tree.
1222 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1223 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1224 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1227 * For a match mip->mi_result is updated.
1230 find_word(mip
, mode
)
1235 int endlen
[MAXWLEN
]; /* length at possible word endings */
1236 idx_T endidx
[MAXWLEN
]; /* possible word endings */
1249 slang_T
*slang
= mip
->mi_lp
->lp_slang
;
1257 if (mode
== FIND_KEEPWORD
|| mode
== FIND_KEEPCOMPOUND
)
1259 /* Check for word with matching case in keep-case tree. */
1261 flen
= 9999; /* no case folding, always enough bytes */
1262 byts
= slang
->sl_kbyts
;
1263 idxs
= slang
->sl_kidxs
;
1265 if (mode
== FIND_KEEPCOMPOUND
)
1266 /* Skip over the previously found word(s). */
1267 wlen
+= mip
->mi_compoff
;
1271 /* Check for case-folded in case-folded tree. */
1272 ptr
= mip
->mi_fword
;
1273 flen
= mip
->mi_fwordlen
; /* available case-folded bytes */
1274 byts
= slang
->sl_fbyts
;
1275 idxs
= slang
->sl_fidxs
;
1277 if (mode
== FIND_PREFIX
)
1279 /* Skip over the prefix. */
1280 wlen
= mip
->mi_prefixlen
;
1281 flen
-= mip
->mi_prefixlen
;
1283 else if (mode
== FIND_COMPOUND
)
1285 /* Skip over the previously found word(s). */
1286 wlen
= mip
->mi_compoff
;
1287 flen
-= mip
->mi_compoff
;
1293 return; /* array is empty */
1296 * Repeat advancing in the tree until:
1297 * - there is a byte that doesn't match,
1298 * - we reach the end of the tree,
1299 * - or we reach the end of the line.
1303 if (flen
<= 0 && *mip
->mi_fend
!= NUL
)
1304 flen
= fold_more(mip
);
1306 len
= byts
[arridx
++];
1308 /* If the first possible byte is a zero the word could end here.
1309 * Remember this index, we first check for the longest word. */
1310 if (byts
[arridx
] == 0)
1312 if (endidxcnt
== MAXWLEN
)
1314 /* Must be a corrupted spell file. */
1318 endlen
[endidxcnt
] = wlen
;
1319 endidx
[endidxcnt
++] = arridx
++;
1322 /* Skip over the zeros, there can be several flag/region
1324 while (len
> 0 && byts
[arridx
] == 0)
1330 break; /* no children, word must end here */
1333 /* Stop looking at end of the line. */
1334 if (ptr
[wlen
] == NUL
)
1337 /* Perform a binary search in the list of accepted bytes. */
1339 if (c
== TAB
) /* <Tab> is handled like <Space> */
1342 hi
= arridx
+ len
- 1;
1348 else if (byts
[m
] < c
)
1357 /* Stop if there is no matching byte. */
1358 if (hi
< lo
|| byts
[lo
] != c
)
1361 /* Continue at the child (if there is one). */
1366 /* One space in the good word may stand for several spaces in the
1372 if (flen
<= 0 && *mip
->mi_fend
!= NUL
)
1373 flen
= fold_more(mip
);
1374 if (ptr
[wlen
] != ' ' && ptr
[wlen
] != TAB
)
1383 * Verify that one of the possible endings is valid. Try the longest
1386 while (endidxcnt
> 0)
1389 arridx
= endidx
[endidxcnt
];
1390 wlen
= endlen
[endidxcnt
];
1393 if ((*mb_head_off
)(ptr
, ptr
+ wlen
) > 0)
1394 continue; /* not at first byte of character */
1396 if (spell_iswordp(ptr
+ wlen
, mip
->mi_buf
))
1398 if (slang
->sl_compprog
== NULL
&& !slang
->sl_nobreak
)
1399 continue; /* next char is a word character */
1404 /* The prefix flag is before compound flags. Once a valid prefix flag
1405 * has been found we try compound flags. */
1406 prefix_found
= FALSE
;
1409 if (mode
!= FIND_KEEPWORD
&& has_mbyte
)
1411 /* Compute byte length in original word, length may change
1412 * when folding case. This can be slow, take a shortcut when the
1413 * case-folded word is equal to the keep-case word. */
1415 if (STRNCMP(ptr
, p
, wlen
) != 0)
1417 for (s
= ptr
; s
< ptr
+ wlen
; mb_ptr_adv(s
))
1419 wlen
= (int)(p
- mip
->mi_word
);
1424 /* Check flags and region. For FIND_PREFIX check the condition and
1426 * Repeat this if there are more flags/region alternatives until there
1429 for (len
= byts
[arridx
- 1]; len
> 0 && byts
[arridx
] == 0;
1432 flags
= idxs
[arridx
];
1434 /* For the fold-case tree check that the case of the checked word
1435 * matches with what the word in the tree requires.
1436 * For keep-case tree the case is always right. For prefixes we
1437 * don't bother to check. */
1438 if (mode
== FIND_FOLDWORD
)
1440 if (mip
->mi_cend
!= mip
->mi_word
+ wlen
)
1442 /* mi_capflags was set for a different word length, need
1443 * to do it again. */
1444 mip
->mi_cend
= mip
->mi_word
+ wlen
;
1445 mip
->mi_capflags
= captype(mip
->mi_word
, mip
->mi_cend
);
1448 if (mip
->mi_capflags
== WF_KEEPCAP
1449 || !spell_valid_case(mip
->mi_capflags
, flags
))
1453 /* When mode is FIND_PREFIX the word must support the prefix:
1454 * check the prefix ID and the condition. Do that for the list at
1455 * mip->mi_prefarridx that find_prefix() filled. */
1456 else if (mode
== FIND_PREFIX
&& !prefix_found
)
1458 c
= valid_word_prefix(mip
->mi_prefcnt
, mip
->mi_prefarridx
,
1460 mip
->mi_word
+ mip
->mi_cprefixlen
, slang
,
1465 /* Use the WF_RARE flag for a rare prefix. */
1468 prefix_found
= TRUE
;
1471 if (slang
->sl_nobreak
)
1473 if ((mode
== FIND_COMPOUND
|| mode
== FIND_KEEPCOMPOUND
)
1474 && (flags
& WF_BANNED
) == 0)
1476 /* NOBREAK: found a valid following word. That's all we
1477 * need to know, so return. */
1478 mip
->mi_result
= SP_OK
;
1483 else if ((mode
== FIND_COMPOUND
|| mode
== FIND_KEEPCOMPOUND
1486 /* If there is no compound flag or the word is shorter than
1487 * COMPOUNDMIN reject it quickly.
1488 * Makes you wonder why someone puts a compound flag on a word
1489 * that's too short... Myspell compatibility requires this
1491 if (((unsigned)flags
>> 24) == 0
1492 || wlen
- mip
->mi_compoff
< slang
->sl_compminlen
)
1495 /* For multi-byte chars check character length against
1498 && slang
->sl_compminlen
> 0
1499 && mb_charlen_len(mip
->mi_word
+ mip
->mi_compoff
,
1500 wlen
- mip
->mi_compoff
) < slang
->sl_compminlen
)
1504 /* Limit the number of compound words to COMPOUNDWORDMAX if no
1505 * maximum for syllables is specified. */
1506 if (!word_ends
&& mip
->mi_complen
+ mip
->mi_compextra
+ 2
1508 && slang
->sl_compsylmax
== MAXWLEN
)
1511 /* Don't allow compounding on a side where an affix was added,
1512 * unless COMPOUNDPERMITFLAG was used. */
1513 if (mip
->mi_complen
> 0 && (flags
& WF_NOCOMPBEF
))
1515 if (!word_ends
&& (flags
& WF_NOCOMPAFT
))
1518 /* Quickly check if compounding is possible with this flag. */
1519 if (!byte_in_str(mip
->mi_complen
== 0
1520 ? slang
->sl_compstartflags
1521 : slang
->sl_compallflags
,
1522 ((unsigned)flags
>> 24)))
1525 if (mode
== FIND_COMPOUND
)
1529 /* Need to check the caps type of the appended compound
1532 if (has_mbyte
&& STRNCMP(ptr
, mip
->mi_word
,
1533 mip
->mi_compoff
) != 0)
1535 /* case folding may have changed the length */
1537 for (s
= ptr
; s
< ptr
+ mip
->mi_compoff
; mb_ptr_adv(s
))
1542 p
= mip
->mi_word
+ mip
->mi_compoff
;
1543 capflags
= captype(p
, mip
->mi_word
+ wlen
);
1544 if (capflags
== WF_KEEPCAP
|| (capflags
== WF_ALLCAP
1545 && (flags
& WF_FIXCAP
) != 0))
1548 if (capflags
!= WF_ALLCAP
)
1550 /* When the character before the word is a word
1551 * character we do not accept a Onecap word. We do
1552 * accept a no-caps word, even when the dictionary
1553 * word specifies ONECAP. */
1554 mb_ptr_back(mip
->mi_word
, p
);
1555 if (spell_iswordp_nmw(p
)
1556 ? capflags
== WF_ONECAP
1557 : (flags
& WF_ONECAP
) != 0
1558 && capflags
!= WF_ONECAP
)
1563 /* If the word ends the sequence of compound flags of the
1564 * words must match with one of the COMPOUNDRULE items and
1565 * the number of syllables must not be too large. */
1566 mip
->mi_compflags
[mip
->mi_complen
] = ((unsigned)flags
>> 24);
1567 mip
->mi_compflags
[mip
->mi_complen
+ 1] = NUL
;
1570 char_u fword
[MAXWLEN
];
1572 if (slang
->sl_compsylmax
< MAXWLEN
)
1574 /* "fword" is only needed for checking syllables. */
1575 if (ptr
== mip
->mi_word
)
1576 (void)spell_casefold(ptr
, wlen
, fword
, MAXWLEN
);
1578 vim_strncpy(fword
, ptr
, endlen
[endidxcnt
]);
1580 if (!can_compound(slang
, fword
, mip
->mi_compflags
))
1585 /* Check NEEDCOMPOUND: can't use word without compounding. */
1586 else if (flags
& WF_NEEDCOMP
)
1589 nobreak_result
= SP_OK
;
1593 int save_result
= mip
->mi_result
;
1594 char_u
*save_end
= mip
->mi_end
;
1595 langp_T
*save_lp
= mip
->mi_lp
;
1598 /* Check that a valid word follows. If there is one and we
1599 * are compounding, it will set "mi_result", thus we are
1600 * always finished here. For NOBREAK we only check that a
1601 * valid word follows.
1603 if (slang
->sl_nobreak
)
1604 mip
->mi_result
= SP_BAD
;
1606 /* Find following word in case-folded tree. */
1607 mip
->mi_compoff
= endlen
[endidxcnt
];
1609 if (has_mbyte
&& mode
== FIND_KEEPWORD
)
1611 /* Compute byte length in case-folded word from "wlen":
1612 * byte length in keep-case word. Length may change when
1613 * folding case. This can be slow, take a shortcut when
1614 * the case-folded word is equal to the keep-case word. */
1616 if (STRNCMP(ptr
, p
, wlen
) != 0)
1618 for (s
= ptr
; s
< ptr
+ wlen
; mb_ptr_adv(s
))
1620 mip
->mi_compoff
= (int)(p
- mip
->mi_fword
);
1624 c
= mip
->mi_compoff
;
1626 if (flags
& WF_COMPROOT
)
1627 ++mip
->mi_compextra
;
1629 /* For NOBREAK we need to try all NOBREAK languages, at least
1630 * to find the ".add" file(s). */
1631 for (lpi
= 0; lpi
< mip
->mi_buf
->b_langp
.ga_len
; ++lpi
)
1633 if (slang
->sl_nobreak
)
1635 mip
->mi_lp
= LANGP_ENTRY(mip
->mi_buf
->b_langp
, lpi
);
1636 if (mip
->mi_lp
->lp_slang
->sl_fidxs
== NULL
1637 || !mip
->mi_lp
->lp_slang
->sl_nobreak
)
1641 find_word(mip
, FIND_COMPOUND
);
1643 /* When NOBREAK any word that matches is OK. Otherwise we
1644 * need to find the longest match, thus try with keep-case
1645 * and prefix too. */
1646 if (!slang
->sl_nobreak
|| mip
->mi_result
== SP_BAD
)
1648 /* Find following word in keep-case tree. */
1649 mip
->mi_compoff
= wlen
;
1650 find_word(mip
, FIND_KEEPCOMPOUND
);
1652 #if 0 /* Disabled, a prefix must not appear halfway a compound word,
1653 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1654 postponed prefix. */
1655 if (!slang
->sl_nobreak
|| mip
->mi_result
== SP_BAD
)
1657 /* Check for following word with prefix. */
1658 mip
->mi_compoff
= c
;
1659 find_prefix(mip
, FIND_COMPOUND
);
1664 if (!slang
->sl_nobreak
)
1668 if (flags
& WF_COMPROOT
)
1669 --mip
->mi_compextra
;
1670 mip
->mi_lp
= save_lp
;
1672 if (slang
->sl_nobreak
)
1674 nobreak_result
= mip
->mi_result
;
1675 mip
->mi_result
= save_result
;
1676 mip
->mi_end
= save_end
;
1680 if (mip
->mi_result
== SP_OK
)
1686 if (flags
& WF_BANNED
)
1688 else if (flags
& WF_REGION
)
1691 if ((mip
->mi_lp
->lp_region
& (flags
>> 16)) != 0)
1696 else if (flags
& WF_RARE
)
1701 /* Always use the longest match and the best result. For NOBREAK
1702 * we separately keep the longest match without a following good
1703 * word as a fall-back. */
1704 if (nobreak_result
== SP_BAD
)
1706 if (mip
->mi_result2
> res
)
1708 mip
->mi_result2
= res
;
1709 mip
->mi_end2
= mip
->mi_word
+ wlen
;
1711 else if (mip
->mi_result2
== res
1712 && mip
->mi_end2
< mip
->mi_word
+ wlen
)
1713 mip
->mi_end2
= mip
->mi_word
+ wlen
;
1715 else if (mip
->mi_result
> res
)
1717 mip
->mi_result
= res
;
1718 mip
->mi_end
= mip
->mi_word
+ wlen
;
1720 else if (mip
->mi_result
== res
&& mip
->mi_end
< mip
->mi_word
+ wlen
)
1721 mip
->mi_end
= mip
->mi_word
+ wlen
;
1723 if (mip
->mi_result
== SP_OK
)
1727 if (mip
->mi_result
== SP_OK
)
1733 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1734 * does not have too many syllables.
1737 can_compound(slang
, word
, flags
)
1742 regmatch_T regmatch
;
1744 char_u uflags
[MAXWLEN
* 2];
1749 if (slang
->sl_compprog
== NULL
)
1754 /* Need to convert the single byte flags to utf8 characters. */
1756 for (i
= 0; flags
[i
] != NUL
; ++i
)
1757 p
+= mb_char2bytes(flags
[i
], p
);
1764 regmatch
.regprog
= slang
->sl_compprog
;
1765 regmatch
.rm_ic
= FALSE
;
1766 if (!vim_regexec(®match
, p
, 0))
1769 /* Count the number of syllables. This may be slow, do it last. If there
1770 * are too many syllables AND the number of compound words is above
1771 * COMPOUNDWORDMAX then compounding is not allowed. */
1772 if (slang
->sl_compsylmax
< MAXWLEN
1773 && count_syllables(slang
, word
) > slang
->sl_compsylmax
)
1774 return (int)STRLEN(flags
) < slang
->sl_compmax
;
1779 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1780 * ID in "flags" for the word "word".
1781 * The WF_RAREPFX flag is included in the return value for a rare prefix.
1784 valid_word_prefix(totprefcnt
, arridx
, flags
, word
, slang
, cond_req
)
1785 int totprefcnt
; /* nr of prefix IDs */
1786 int arridx
; /* idx in sl_pidxs[] */
1790 int cond_req
; /* only use prefixes with a condition */
1795 regmatch_T regmatch
;
1798 prefid
= (unsigned)flags
>> 24;
1799 for (prefcnt
= totprefcnt
- 1; prefcnt
>= 0; --prefcnt
)
1801 pidx
= slang
->sl_pidxs
[arridx
+ prefcnt
];
1803 /* Check the prefix ID. */
1804 if (prefid
!= (pidx
& 0xff))
1807 /* Check if the prefix doesn't combine and the word already has a
1809 if ((flags
& WF_HAS_AFF
) && (pidx
& WF_PFX_NC
))
1812 /* Check the condition, if there is one. The condition index is
1813 * stored in the two bytes above the prefix ID byte. */
1814 rp
= slang
->sl_prefprog
[((unsigned)pidx
>> 8) & 0xffff];
1817 regmatch
.regprog
= rp
;
1818 regmatch
.rm_ic
= FALSE
;
1819 if (!vim_regexec(®match
, word
, 0))
1825 /* It's a match! Return the WF_ flags. */
1832 * Check if the word at "mip->mi_word" has a matching prefix.
1833 * If it does, then check the following word.
1835 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1836 * prefix in a compound word.
1838 * For a match mip->mi_result is updated.
1841 find_prefix(mip
, mode
)
1852 slang_T
*slang
= mip
->mi_lp
->lp_slang
;
1856 byts
= slang
->sl_pbyts
;
1858 return; /* array is empty */
1860 /* We use the case-folded word here, since prefixes are always
1862 ptr
= mip
->mi_fword
;
1863 flen
= mip
->mi_fwordlen
; /* available case-folded bytes */
1864 if (mode
== FIND_COMPOUND
)
1866 /* Skip over the previously found word(s). */
1867 ptr
+= mip
->mi_compoff
;
1868 flen
-= mip
->mi_compoff
;
1870 idxs
= slang
->sl_pidxs
;
1873 * Repeat advancing in the tree until:
1874 * - there is a byte that doesn't match,
1875 * - we reach the end of the tree,
1876 * - or we reach the end of the line.
1880 if (flen
== 0 && *mip
->mi_fend
!= NUL
)
1881 flen
= fold_more(mip
);
1883 len
= byts
[arridx
++];
1885 /* If the first possible byte is a zero the prefix could end here.
1886 * Check if the following word matches and supports the prefix. */
1887 if (byts
[arridx
] == 0)
1889 /* There can be several prefixes with different conditions. We
1890 * try them all, since we don't know which one will give the
1891 * longest match. The word is the same each time, pass the list
1892 * of possible prefixes to find_word(). */
1893 mip
->mi_prefarridx
= arridx
;
1894 mip
->mi_prefcnt
= len
;
1895 while (len
> 0 && byts
[arridx
] == 0)
1900 mip
->mi_prefcnt
-= len
;
1902 /* Find the word that comes after the prefix. */
1903 mip
->mi_prefixlen
= wlen
;
1904 if (mode
== FIND_COMPOUND
)
1905 /* Skip over the previously found word(s). */
1906 mip
->mi_prefixlen
+= mip
->mi_compoff
;
1911 /* Case-folded length may differ from original length. */
1912 mip
->mi_cprefixlen
= nofold_len(mip
->mi_fword
,
1913 mip
->mi_prefixlen
, mip
->mi_word
);
1916 mip
->mi_cprefixlen
= mip
->mi_prefixlen
;
1918 find_word(mip
, FIND_PREFIX
);
1922 break; /* no children, word must end here */
1925 /* Stop looking at end of the line. */
1926 if (ptr
[wlen
] == NUL
)
1929 /* Perform a binary search in the list of accepted bytes. */
1932 hi
= arridx
+ len
- 1;
1938 else if (byts
[m
] < c
)
1947 /* Stop if there is no matching byte. */
1948 if (hi
< lo
|| byts
[lo
] != c
)
1951 /* Continue at the child (if there is one). */
1959 * Need to fold at least one more character. Do until next non-word character
1960 * for efficiency. Include the non-word character too.
1961 * Return the length of the folded chars in bytes.
1973 mb_ptr_adv(mip
->mi_fend
);
1974 } while (*mip
->mi_fend
!= NUL
&& spell_iswordp(mip
->mi_fend
, mip
->mi_buf
));
1976 /* Include the non-word character so that we can check for the word end. */
1977 if (*mip
->mi_fend
!= NUL
)
1978 mb_ptr_adv(mip
->mi_fend
);
1980 (void)spell_casefold(p
, (int)(mip
->mi_fend
- p
),
1981 mip
->mi_fword
+ mip
->mi_fwordlen
,
1982 MAXWLEN
- mip
->mi_fwordlen
);
1983 flen
= (int)STRLEN(mip
->mi_fword
+ mip
->mi_fwordlen
);
1984 mip
->mi_fwordlen
+= flen
;
1989 * Check case flags for a word. Return TRUE if the word has the requested
1993 spell_valid_case(wordflags
, treeflags
)
1994 int wordflags
; /* flags for the checked word. */
1995 int treeflags
; /* flags for the word in the spell tree */
1997 return ((wordflags
== WF_ALLCAP
&& (treeflags
& WF_FIXCAP
) == 0)
1998 || ((treeflags
& (WF_ALLCAP
| WF_KEEPCAP
)) == 0
1999 && ((treeflags
& WF_ONECAP
) == 0
2000 || (wordflags
& WF_ONECAP
) != 0)));
2004 * Return TRUE if spell checking is not enabled.
2007 no_spell_checking(wp
)
2010 if (!wp
->w_p_spell
|| *wp
->w_buffer
->b_p_spl
== NUL
2011 || wp
->w_buffer
->b_langp
.ga_len
== 0)
2013 EMSG(_("E756: Spell checking is not enabled"));
2020 * Move to next spell error.
2021 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2022 * "curline" is TRUE to find word under/after cursor in the same line.
2023 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2024 * to after badly spelled word before the cursor.
2025 * Return 0 if not found, length of the badly spelled word otherwise.
2028 spell_move_to(wp
, dir
, allwords
, curline
, attrp
)
2030 int dir
; /* FORWARD or BACKWARD */
2031 int allwords
; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
2033 hlf_T
*attrp
; /* return: attributes of bad word or NULL
2034 (only when "dir" is FORWARD) */
2045 int has_syntax
= syntax_present(wp
->w_buffer
);
2053 int found_one
= FALSE
;
2054 int wrapped
= FALSE
;
2056 if (no_spell_checking(wp
))
2060 * Start looking for bad word at the start of the line, because we can't
2061 * start halfway a word, we don't know where it starts or ends.
2063 * When searching backwards, we continue in the line to find the last
2064 * bad word (in the cursor line: before the cursor).
2066 * We concatenate the start of the next line, so that wrapped words work
2067 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2070 lnum
= wp
->w_cursor
.lnum
;
2071 clearpos(&found_pos
);
2075 line
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
2077 len
= (int)STRLEN(line
);
2078 if (buflen
< len
+ MAXWLEN
+ 2)
2081 buflen
= len
+ MAXWLEN
+ 2;
2082 buf
= alloc(buflen
);
2087 /* In first line check first word for Capital. */
2091 /* For checking first word with a capital skip white space. */
2093 capcol
= (int)(skipwhite(line
) - line
);
2094 else if (curline
&& wp
== curwin
)
2096 /* For spellbadword(): check if first word needs a capital. */
2097 col
= (int)(skipwhite(line
) - line
);
2098 if (check_need_cap(lnum
, col
))
2101 /* Need to get the line again, may have looked at the previous
2103 line
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
2106 /* Copy the line into "buf" and append the start of the next line if
2109 if (lnum
< wp
->w_buffer
->b_ml
.ml_line_count
)
2110 spell_cat_line(buf
+ STRLEN(buf
),
2111 ml_get_buf(wp
->w_buffer
, lnum
+ 1, FALSE
), MAXWLEN
);
2117 /* When searching backward don't search after the cursor. Unless
2118 * we wrapped around the end of the buffer. */
2120 && lnum
== wp
->w_cursor
.lnum
2122 && (colnr_T
)(p
- buf
) >= wp
->w_cursor
.col
)
2127 len
= spell_check(wp
, p
, &attr
, &capcol
, FALSE
);
2129 if (attr
!= HLF_COUNT
)
2131 /* We found a bad word. Check the attribute. */
2132 if (allwords
|| attr
== HLF_SPB
)
2134 /* When searching forward only accept a bad word after
2137 || lnum
!= wp
->w_cursor
.lnum
2138 || (lnum
== wp
->w_cursor
.lnum
2140 || (colnr_T
)(curline
? p
- buf
+ len
2142 > wp
->w_cursor
.col
)))
2147 col
= (int)(p
- buf
);
2148 (void)syn_get_id(wp
, lnum
, (colnr_T
)col
,
2160 found_pos
.lnum
= lnum
;
2161 found_pos
.col
= (int)(p
- buf
);
2162 #ifdef FEAT_VIRTUALEDIT
2163 found_pos
.coladd
= 0;
2167 /* No need to search further. */
2168 wp
->w_cursor
= found_pos
;
2175 /* Insert mode completion: put cursor after
2177 found_pos
.col
+= len
;
2186 /* advance to character after the word */
2191 if (dir
== BACKWARD
&& found_pos
.lnum
!= 0)
2193 /* Use the last match in the line (before the cursor). */
2194 wp
->w_cursor
= found_pos
;
2200 break; /* only check cursor line */
2202 /* Advance to next line. */
2203 if (dir
== BACKWARD
)
2205 /* If we are back at the starting line and searched it again there
2206 * is no match, give up. */
2207 if (lnum
== wp
->w_cursor
.lnum
&& wrapped
)
2213 break; /* at first line and 'nowrapscan' */
2216 /* Wrap around to the end of the buffer. May search the
2217 * starting line again and accept the last match. */
2218 lnum
= wp
->w_buffer
->b_ml
.ml_line_count
;
2220 if (!shortmess(SHM_SEARCH
))
2221 give_warning((char_u
*)_(top_bot_msg
), TRUE
);
2227 if (lnum
< wp
->w_buffer
->b_ml
.ml_line_count
)
2230 break; /* at first line and 'nowrapscan' */
2233 /* Wrap around to the start of the buffer. May search the
2234 * starting line again and accept the first match. */
2237 if (!shortmess(SHM_SEARCH
))
2238 give_warning((char_u
*)_(bot_top_msg
), TRUE
);
2241 /* If we are back at the starting line and there is no match then
2243 if (lnum
== wp
->w_cursor
.lnum
&& !found_one
)
2246 /* Skip the characters at the start of the next line that were
2247 * included in a match crossing line boundaries. */
2248 if (attr
== HLF_COUNT
)
2249 skip
= (int)(p
- endp
);
2253 /* Capcol skips over the inserted space. */
2256 /* But after empty line check first word in next line */
2257 if (*skipwhite(line
) == NUL
)
2269 * For spell checking: concatenate the start of the following line "line" into
2270 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2273 spell_cat_line(buf
, line
, maxlen
)
2281 p
= skipwhite(line
);
2282 while (vim_strchr((char_u
*)"*#/\"\t", *p
) != NULL
)
2283 p
= skipwhite(p
+ 1);
2288 vim_strncpy(buf
+ 1, line
, maxlen
- 2);
2289 n
= (int)(p
- line
);
2292 vim_memset(buf
+ 1, ' ', n
);
2297 * Structure used for the cookie argument of do_in_runtimepath().
2299 typedef struct spelload_S
2301 char_u sl_lang
[MAXWLEN
+ 1]; /* language name */
2302 slang_T
*sl_slang
; /* resulting slang_T struct */
2303 int sl_nobreak
; /* NOBREAK language found */
2307 * Load word list(s) for "lang" from Vim spell file(s).
2308 * "lang" must be the language without the region: e.g., "en".
2311 spell_load_lang(lang
)
2314 char_u fname_enc
[85];
2321 /* Copy the language name to pass it to spell_load_cb() as a cookie.
2322 * It's truncated when an error is detected. */
2323 STRCPY(sl
.sl_lang
, lang
);
2325 sl
.sl_nobreak
= FALSE
;
2328 /* We may retry when no spell file is found for the language, an
2329 * autocommand may load it then. */
2330 for (round
= 1; round
<= 2; ++round
)
2334 * Find the first spell file for "lang" in 'runtimepath' and load it.
2336 vim_snprintf((char *)fname_enc
, sizeof(fname_enc
) - 5,
2337 "spell/%s.%s.spl", lang
, spell_enc());
2338 r
= do_in_runtimepath(fname_enc
, FALSE
, spell_load_cb
, &sl
);
2340 if (r
== FAIL
&& *sl
.sl_lang
!= NUL
)
2342 /* Try loading the ASCII version. */
2343 vim_snprintf((char *)fname_enc
, sizeof(fname_enc
) - 5,
2344 "spell/%s.ascii.spl", lang
);
2345 r
= do_in_runtimepath(fname_enc
, FALSE
, spell_load_cb
, &sl
);
2348 if (r
== FAIL
&& *sl
.sl_lang
!= NUL
&& round
== 1
2349 && apply_autocmds(EVENT_SPELLFILEMISSING
, lang
,
2350 curbuf
->b_fname
, FALSE
, curbuf
))
2362 smsg((char_u
*)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2363 lang
, spell_enc(), lang
);
2365 else if (sl
.sl_slang
!= NULL
)
2367 /* At least one file was loaded, now load ALL the additions. */
2368 STRCPY(fname_enc
+ STRLEN(fname_enc
) - 3, "add.spl");
2369 do_in_runtimepath(fname_enc
, TRUE
, spell_load_cb
, &sl
);
2374 * Return the encoding used for spell checking: Use 'encoding', except that we
2375 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2382 if (STRLEN(p_enc
) < 60 && STRCMP(p_enc
, "iso-8859-15") != 0)
2385 return (char_u
*)"latin1";
2389 * Get the name of the .spl file for the internal wordlist into
2390 * "fname[MAXPATHL]".
2393 int_wordlist_spl(fname
)
2396 vim_snprintf((char *)fname
, MAXPATHL
, "%s.%s.spl",
2397 int_wordlist
, spell_enc());
2401 * Allocate a new slang_T for language "lang". "lang" can be NULL.
2402 * Caller must fill "sl_next".
2410 lp
= (slang_T
*)alloc_clear(sizeof(slang_T
));
2414 lp
->sl_name
= vim_strsave(lang
);
2415 ga_init2(&lp
->sl_rep
, sizeof(fromto_T
), 10);
2416 ga_init2(&lp
->sl_repsal
, sizeof(fromto_T
), 10);
2417 lp
->sl_compmax
= MAXWLEN
;
2418 lp
->sl_compsylmax
= MAXWLEN
;
2419 hash_init(&lp
->sl_wordcount
);
2426 * Free the contents of an slang_T and the structure itself.
2432 vim_free(lp
->sl_name
);
2433 vim_free(lp
->sl_fname
);
2439 * Clear an slang_T so that the file can be reloaded.
2451 vim_free(lp
->sl_fbyts
);
2452 lp
->sl_fbyts
= NULL
;
2453 vim_free(lp
->sl_kbyts
);
2454 lp
->sl_kbyts
= NULL
;
2455 vim_free(lp
->sl_pbyts
);
2456 lp
->sl_pbyts
= NULL
;
2458 vim_free(lp
->sl_fidxs
);
2459 lp
->sl_fidxs
= NULL
;
2460 vim_free(lp
->sl_kidxs
);
2461 lp
->sl_kidxs
= NULL
;
2462 vim_free(lp
->sl_pidxs
);
2463 lp
->sl_pidxs
= NULL
;
2465 for (round
= 1; round
<= 2; ++round
)
2467 gap
= round
== 1 ? &lp
->sl_rep
: &lp
->sl_repsal
;
2468 while (gap
->ga_len
> 0)
2470 ftp
= &((fromto_T
*)gap
->ga_data
)[--gap
->ga_len
];
2471 vim_free(ftp
->ft_from
);
2472 vim_free(ftp
->ft_to
);
2480 /* "ga_len" is set to 1 without adding an item for latin1 */
2481 if (gap
->ga_data
!= NULL
)
2482 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2483 for (i
= 0; i
< gap
->ga_len
; ++i
)
2484 vim_free(((int **)gap
->ga_data
)[i
]);
2487 /* SAL items: free salitem_T items */
2488 while (gap
->ga_len
> 0)
2490 smp
= &((salitem_T
*)gap
->ga_data
)[--gap
->ga_len
];
2491 vim_free(smp
->sm_lead
);
2492 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2493 vim_free(smp
->sm_to
);
2495 vim_free(smp
->sm_lead_w
);
2496 vim_free(smp
->sm_oneof_w
);
2497 vim_free(smp
->sm_to_w
);
2502 for (i
= 0; i
< lp
->sl_prefixcnt
; ++i
)
2503 vim_free(lp
->sl_prefprog
[i
]);
2504 lp
->sl_prefixcnt
= 0;
2505 vim_free(lp
->sl_prefprog
);
2506 lp
->sl_prefprog
= NULL
;
2508 vim_free(lp
->sl_info
);
2511 vim_free(lp
->sl_midword
);
2512 lp
->sl_midword
= NULL
;
2514 vim_free(lp
->sl_compprog
);
2515 vim_free(lp
->sl_compstartflags
);
2516 vim_free(lp
->sl_compallflags
);
2517 lp
->sl_compprog
= NULL
;
2518 lp
->sl_compstartflags
= NULL
;
2519 lp
->sl_compallflags
= NULL
;
2521 vim_free(lp
->sl_syllable
);
2522 lp
->sl_syllable
= NULL
;
2523 ga_clear(&lp
->sl_syl_items
);
2525 ga_clear_strings(&lp
->sl_comppat
);
2527 hash_clear_all(&lp
->sl_wordcount
, WC_KEY_OFF
);
2528 hash_init(&lp
->sl_wordcount
);
2531 hash_clear_all(&lp
->sl_map_hash
, 0);
2534 /* Clear info from .sug file. */
2535 slang_clear_sug(lp
);
2537 lp
->sl_compmax
= MAXWLEN
;
2538 lp
->sl_compminlen
= 0;
2539 lp
->sl_compsylmax
= MAXWLEN
;
2540 lp
->sl_regions
[0] = NUL
;
2544 * Clear the info from the .sug file in "lp".
2550 vim_free(lp
->sl_sbyts
);
2551 lp
->sl_sbyts
= NULL
;
2552 vim_free(lp
->sl_sidxs
);
2553 lp
->sl_sidxs
= NULL
;
2554 close_spellbuf(lp
->sl_sugbuf
);
2555 lp
->sl_sugbuf
= NULL
;
2556 lp
->sl_sugloaded
= FALSE
;
2561 * Load one spell file and store the info into a slang_T.
2562 * Invoked through do_in_runtimepath().
2565 spell_load_cb(fname
, cookie
)
2569 spelload_T
*slp
= (spelload_T
*)cookie
;
2572 slang
= spell_load_file(fname
, slp
->sl_lang
, NULL
, FALSE
);
2575 /* When a previously loaded file has NOBREAK also use it for the
2577 if (slp
->sl_nobreak
&& slang
->sl_add
)
2578 slang
->sl_nobreak
= TRUE
;
2579 else if (slang
->sl_nobreak
)
2580 slp
->sl_nobreak
= TRUE
;
2582 slp
->sl_slang
= slang
;
2587 * Load one spell file and store the info into a slang_T.
2589 * This is invoked in three ways:
2590 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2591 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2592 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2593 * points to the existing slang_T.
2594 * - Just after writing a .spl file; it's read back to produce the .sug file.
2595 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2597 * Returns the slang_T the spell file was loaded into. NULL for error.
2600 spell_load_file(fname
, lang
, old_lp
, silent
)
2604 int silent
; /* no error if file doesn't exist */
2607 char_u buf
[VIMSPELLMAGICL
];
2612 char_u
*save_sourcing_name
= sourcing_name
;
2613 linenr_T save_sourcing_lnum
= sourcing_lnum
;
2618 fd
= mch_fopen((char *)fname
, "r");
2622 EMSG2(_(e_notopen
), fname
);
2623 else if (p_verbose
> 2)
2626 smsg((char_u
*)e_notopen
, fname
);
2634 smsg((char_u
*)_("Reading spell file \"%s\""), fname
);
2640 lp
= slang_alloc(lang
);
2644 /* Remember the file name, used to reload the file when it's updated. */
2645 lp
->sl_fname
= vim_strsave(fname
);
2646 if (lp
->sl_fname
== NULL
)
2649 /* Check for .add.spl. */
2650 lp
->sl_add
= strstr((char *)gettail(fname
), ".add.") != NULL
;
2655 /* Set sourcing_name, so that error messages mention the file name. */
2656 sourcing_name
= fname
;
2660 * <HEADER>: <fileID>
2662 for (i
= 0; i
< VIMSPELLMAGICL
; ++i
)
2663 buf
[i
] = getc(fd
); /* <fileID> */
2664 if (STRNCMP(buf
, VIMSPELLMAGIC
, VIMSPELLMAGICL
) != 0)
2666 EMSG(_("E757: This does not look like a spell file"));
2669 c
= getc(fd
); /* <versionnr> */
2670 if (c
< VIMSPELLVERSION
)
2672 EMSG(_("E771: Old spell file, needs to be updated"));
2675 else if (c
> VIMSPELLVERSION
)
2677 EMSG(_("E772: Spell file is for newer version of Vim"));
2683 * <SECTIONS>: <section> ... <sectionend>
2684 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2688 n
= getc(fd
); /* <sectionID> or <sectionend> */
2691 c
= getc(fd
); /* <sectionflags> */
2692 len
= get4c(fd
); /* <sectionlen> */
2700 lp
->sl_info
= read_string(fd
, len
); /* <infotext> */
2701 if (lp
->sl_info
== NULL
)
2706 res
= read_region_section(fd
, lp
, len
);
2710 res
= read_charflags_section(fd
);
2714 lp
->sl_midword
= read_string(fd
, len
); /* <midword> */
2715 if (lp
->sl_midword
== NULL
)
2720 res
= read_prefcond_section(fd
, lp
);
2724 res
= read_rep_section(fd
, &lp
->sl_rep
, lp
->sl_rep_first
);
2728 res
= read_rep_section(fd
, &lp
->sl_repsal
, lp
->sl_repsal_first
);
2732 res
= read_sal_section(fd
, lp
);
2736 res
= read_sofo_section(fd
, lp
);
2740 p
= read_string(fd
, len
); /* <mapstr> */
2748 res
= read_words_section(fd
, lp
, len
);
2752 lp
->sl_sugtime
= get8c(fd
); /* <timestamp> */
2755 case SN_NOSPLITSUGS
:
2756 lp
->sl_nosplitsugs
= TRUE
; /* <timestamp> */
2760 res
= read_compound(fd
, lp
, len
);
2764 lp
->sl_nobreak
= TRUE
;
2768 lp
->sl_syllable
= read_string(fd
, len
); /* <syllable> */
2769 if (lp
->sl_syllable
== NULL
)
2771 if (init_syl_tab(lp
) == FAIL
)
2776 /* Unsupported section. When it's required give an error
2777 * message. When it's not required skip the contents. */
2778 if (c
& SNF_REQUIRED
)
2780 EMSG(_("E770: Unsupported section in spell file"));
2789 if (res
== SP_FORMERROR
)
2794 if (res
== SP_TRUNCERROR
)
2797 EMSG(_(e_spell_trunc
));
2800 if (res
== SP_OTHERERROR
)
2805 res
= spell_read_tree(fd
, &lp
->sl_fbyts
, &lp
->sl_fidxs
, FALSE
, 0);
2810 res
= spell_read_tree(fd
, &lp
->sl_kbyts
, &lp
->sl_kidxs
, FALSE
, 0);
2815 res
= spell_read_tree(fd
, &lp
->sl_pbyts
, &lp
->sl_pidxs
, TRUE
,
2820 /* For a new file link it in the list of spell files. */
2821 if (old_lp
== NULL
&& lang
!= NULL
)
2823 lp
->sl_next
= first_lang
;
2831 /* truncating the name signals the error to spell_load_lang() */
2833 if (lp
!= NULL
&& old_lp
== NULL
)
2840 sourcing_name
= save_sourcing_name
;
2841 sourcing_lnum
= save_sourcing_lnum
;
2847 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2856 n
= (n
<< 8) + getc(fd
);
2861 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2870 n
= (n
<< 8) + getc(fd
);
2871 n
= (n
<< 8) + getc(fd
);
2876 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2885 n
= (n
<< 8) + getc(fd
);
2886 n
= (n
<< 8) + getc(fd
);
2887 n
= (n
<< 8) + getc(fd
);
2892 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2901 for (i
= 0; i
< 8; ++i
)
2902 n
= (n
<< 8) + getc(fd
);
2907 * Read a length field from "fd" in "cnt_bytes" bytes.
2908 * Allocate memory, read the string into it and add a NUL at the end.
2909 * Returns NULL when the count is zero.
2910 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2914 read_cnt_string(fd
, cnt_bytes
, cntp
)
2923 /* read the length bytes, MSB first */
2924 for (i
= 0; i
< cnt_bytes
; ++i
)
2925 cnt
= (cnt
<< 8) + getc(fd
);
2928 *cntp
= SP_TRUNCERROR
;
2933 return NULL
; /* nothing to read, return NULL */
2935 str
= read_string(fd
, cnt
);
2937 *cntp
= SP_OTHERERROR
;
2942 * Read a string of length "cnt" from "fd" into allocated memory.
2943 * Returns NULL when out of memory.
2946 read_string(fd
, cnt
)
2953 /* allocate memory */
2954 str
= alloc((unsigned)cnt
+ 1);
2957 /* Read the string. Doesn't check for truncated file. */
2958 for (i
= 0; i
< cnt
; ++i
)
2966 * Read SN_REGION: <regionname> ...
2967 * Return SP_*ERROR flags.
2970 read_region_section(fd
, lp
, len
)
2978 return SP_FORMERROR
;
2979 for (i
= 0; i
< len
; ++i
)
2980 lp
->sl_regions
[i
] = getc(fd
); /* <regionname> */
2981 lp
->sl_regions
[len
] = NUL
;
2986 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2987 * <folcharslen> <folchars>
2988 * Return SP_*ERROR flags.
2991 read_charflags_section(fd
)
2996 int flagslen
, follen
;
2998 /* <charflagslen> <charflags> */
2999 flags
= read_cnt_string(fd
, 1, &flagslen
);
3003 /* <folcharslen> <folchars> */
3004 fol
= read_cnt_string(fd
, 2, &follen
);
3011 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3012 if (flags
!= NULL
&& fol
!= NULL
)
3013 set_spell_charflags(flags
, flagslen
, fol
);
3018 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3019 if ((flags
== NULL
) != (fol
== NULL
))
3020 return SP_FORMERROR
;
3025 * Read SN_PREFCOND section.
3026 * Return SP_*ERROR flags.
3029 read_prefcond_section(fd
, lp
)
3037 char_u buf
[MAXWLEN
+ 1];
3039 /* <prefcondcnt> <prefcond> ... */
3040 cnt
= get2c(fd
); /* <prefcondcnt> */
3042 return SP_FORMERROR
;
3044 lp
->sl_prefprog
= (regprog_T
**)alloc_clear(
3045 (unsigned)sizeof(regprog_T
*) * cnt
);
3046 if (lp
->sl_prefprog
== NULL
)
3047 return SP_OTHERERROR
;
3048 lp
->sl_prefixcnt
= cnt
;
3050 for (i
= 0; i
< cnt
; ++i
)
3052 /* <prefcond> : <condlen> <condstr> */
3053 n
= getc(fd
); /* <condlen> */
3054 if (n
< 0 || n
>= MAXWLEN
)
3055 return SP_FORMERROR
;
3057 /* When <condlen> is zero we have an empty condition. Otherwise
3058 * compile the regexp program used to check for the condition. */
3061 buf
[0] = '^'; /* always match at one position only */
3064 *p
++ = getc(fd
); /* <condstr> */
3066 lp
->sl_prefprog
[i
] = vim_regcomp(buf
, RE_MAGIC
+ RE_STRING
);
3073 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
3074 * Return SP_*ERROR flags.
3077 read_rep_section(fd
, gap
, first
)
3086 cnt
= get2c(fd
); /* <repcount> */
3088 return SP_TRUNCERROR
;
3090 if (ga_grow(gap
, cnt
) == FAIL
)
3091 return SP_OTHERERROR
;
3093 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3094 for (; gap
->ga_len
< cnt
; ++gap
->ga_len
)
3096 ftp
= &((fromto_T
*)gap
->ga_data
)[gap
->ga_len
];
3097 ftp
->ft_from
= read_cnt_string(fd
, 1, &i
);
3101 return SP_FORMERROR
;
3102 ftp
->ft_to
= read_cnt_string(fd
, 1, &i
);
3105 vim_free(ftp
->ft_from
);
3108 return SP_FORMERROR
;
3112 /* Fill the first-index table. */
3113 for (i
= 0; i
< 256; ++i
)
3115 for (i
= 0; i
< gap
->ga_len
; ++i
)
3117 ftp
= &((fromto_T
*)gap
->ga_data
)[i
];
3118 if (first
[*ftp
->ft_from
] == -1)
3119 first
[*ftp
->ft_from
] = i
;
3125 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3126 * Return SP_*ERROR flags.
3129 read_sal_section(fd
, slang
)
3141 slang
->sl_sofo
= FALSE
;
3143 i
= getc(fd
); /* <salflags> */
3144 if (i
& SAL_F0LLOWUP
)
3145 slang
->sl_followup
= TRUE
;
3146 if (i
& SAL_COLLAPSE
)
3147 slang
->sl_collapse
= TRUE
;
3148 if (i
& SAL_REM_ACCENTS
)
3149 slang
->sl_rem_accents
= TRUE
;
3151 cnt
= get2c(fd
); /* <salcount> */
3153 return SP_TRUNCERROR
;
3155 gap
= &slang
->sl_sal
;
3156 ga_init2(gap
, sizeof(salitem_T
), 10);
3157 if (ga_grow(gap
, cnt
+ 1) == FAIL
)
3158 return SP_OTHERERROR
;
3160 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3161 for (; gap
->ga_len
< cnt
; ++gap
->ga_len
)
3163 smp
= &((salitem_T
*)gap
->ga_data
)[gap
->ga_len
];
3164 ccnt
= getc(fd
); /* <salfromlen> */
3166 return SP_TRUNCERROR
;
3167 if ((p
= alloc(ccnt
+ 2)) == NULL
)
3168 return SP_OTHERERROR
;
3171 /* Read up to the first special char into sm_lead. */
3172 for (i
= 0; i
< ccnt
; ++i
)
3174 c
= getc(fd
); /* <salfrom> */
3175 if (vim_strchr((char_u
*)"0123456789(-<^$", c
) != NULL
)
3179 smp
->sm_leadlen
= (int)(p
- smp
->sm_lead
);
3182 /* Put (abc) chars in sm_oneof, if any. */
3186 for (++i
; i
< ccnt
; ++i
)
3188 c
= getc(fd
); /* <salfrom> */
3198 smp
->sm_oneof
= NULL
;
3200 /* Any following chars go in sm_rules. */
3203 /* store the char we got while checking for end of sm_lead */
3205 for (++i
; i
< ccnt
; ++i
)
3206 *p
++ = getc(fd
); /* <salfrom> */
3209 /* <saltolen> <salto> */
3210 smp
->sm_to
= read_cnt_string(fd
, 1, &ccnt
);
3213 vim_free(smp
->sm_lead
);
3220 /* convert the multi-byte strings to wide char strings */
3221 smp
->sm_lead_w
= mb_str2wide(smp
->sm_lead
);
3222 smp
->sm_leadlen
= mb_charlen(smp
->sm_lead
);
3223 if (smp
->sm_oneof
== NULL
)
3224 smp
->sm_oneof_w
= NULL
;
3226 smp
->sm_oneof_w
= mb_str2wide(smp
->sm_oneof
);
3227 if (smp
->sm_to
== NULL
)
3228 smp
->sm_to_w
= NULL
;
3230 smp
->sm_to_w
= mb_str2wide(smp
->sm_to
);
3231 if (smp
->sm_lead_w
== NULL
3232 || (smp
->sm_oneof_w
== NULL
&& smp
->sm_oneof
!= NULL
)
3233 || (smp
->sm_to_w
== NULL
&& smp
->sm_to
!= NULL
))
3235 vim_free(smp
->sm_lead
);
3236 vim_free(smp
->sm_to
);
3237 vim_free(smp
->sm_lead_w
);
3238 vim_free(smp
->sm_oneof_w
);
3239 vim_free(smp
->sm_to_w
);
3240 return SP_OTHERERROR
;
3246 if (gap
->ga_len
> 0)
3248 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3249 * that we need to check the index every time. */
3250 smp
= &((salitem_T
*)gap
->ga_data
)[gap
->ga_len
];
3251 if ((p
= alloc(1)) == NULL
)
3252 return SP_OTHERERROR
;
3255 smp
->sm_leadlen
= 0;
3256 smp
->sm_oneof
= NULL
;
3262 smp
->sm_lead_w
= mb_str2wide(smp
->sm_lead
);
3263 smp
->sm_leadlen
= 0;
3264 smp
->sm_oneof_w
= NULL
;
3265 smp
->sm_to_w
= NULL
;
3271 /* Fill the first-index table. */
3272 set_sal_first(slang
);
3278 * Read SN_WORDS: <word> ...
3279 * Return SP_*ERROR flags.
3282 read_words_section(fd
, lp
, len
)
3289 char_u word
[MAXWLEN
];
3293 /* Read one word at a time. */
3299 if (i
== MAXWLEN
- 1)
3300 return SP_FORMERROR
;
3303 /* Init the count to 10. */
3304 count_common_word(lp
, word
, -1, 10);
3311 * Add a word to the hashtable of common words.
3312 * If it's already there then the counter is increased.
3315 count_common_word(lp
, word
, len
, count
)
3318 int len
; /* word length, -1 for upto NUL */
3319 int count
; /* 1 to count once, 10 to init */
3324 char_u buf
[MAXWLEN
];
3331 vim_strncpy(buf
, word
, len
);
3335 hash
= hash_hash(p
);
3336 hi
= hash_lookup(&lp
->sl_wordcount
, p
, hash
);
3337 if (HASHITEM_EMPTY(hi
))
3339 wc
= (wordcount_T
*)alloc((unsigned)(sizeof(wordcount_T
) + STRLEN(p
)));
3342 STRCPY(wc
->wc_word
, p
);
3343 wc
->wc_count
= count
;
3344 hash_add_item(&lp
->sl_wordcount
, hi
, wc
->wc_word
, hash
);
3349 if ((wc
->wc_count
+= count
) < (unsigned)count
) /* check for overflow */
3350 wc
->wc_count
= MAXWORDCOUNT
;
3355 * Adjust the score of common words.
3358 score_wordcount_adj(slang
, score
, word
, split
)
3362 int split
; /* word was split, less bonus */
3369 hi
= hash_find(&slang
->sl_wordcount
, word
);
3370 if (!HASHITEM_EMPTY(hi
))
3373 if (wc
->wc_count
< SCORE_THRES2
)
3374 bonus
= SCORE_COMMON1
;
3375 else if (wc
->wc_count
< SCORE_THRES3
)
3376 bonus
= SCORE_COMMON2
;
3378 bonus
= SCORE_COMMON3
;
3380 newscore
= score
- bonus
/ 2;
3382 newscore
= score
- bonus
;
3391 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3392 * Return SP_*ERROR flags.
3395 read_sofo_section(fd
, slang
)
3403 slang
->sl_sofo
= TRUE
;
3405 /* <sofofromlen> <sofofrom> */
3406 from
= read_cnt_string(fd
, 2, &cnt
);
3410 /* <sofotolen> <sofoto> */
3411 to
= read_cnt_string(fd
, 2, &cnt
);
3418 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3419 if (from
!= NULL
&& to
!= NULL
)
3420 res
= set_sofo(slang
, from
, to
);
3421 else if (from
!= NULL
|| to
!= NULL
)
3422 res
= SP_FORMERROR
; /* only one of two strings is an error */
3432 * Read the compound section from the .spl file:
3433 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
3434 * Returns SP_*ERROR flags.
3437 read_compound(fd
, slang
, len
)
3453 return SP_FORMERROR
; /* need at least two bytes */
3456 c
= getc(fd
); /* <compmax> */
3459 slang
->sl_compmax
= c
;
3462 c
= getc(fd
); /* <compminlen> */
3465 slang
->sl_compminlen
= c
;
3468 c
= getc(fd
); /* <compsylmax> */
3471 slang
->sl_compsylmax
= c
;
3473 c
= getc(fd
); /* <compoptions> */
3475 ungetc(c
, fd
); /* be backwards compatible with Vim 7.0b */
3479 c
= getc(fd
); /* only use the lower byte for now */
3481 slang
->sl_compoptions
= c
;
3483 gap
= &slang
->sl_comppat
;
3484 c
= get2c(fd
); /* <comppatcount> */
3486 ga_init2(gap
, sizeof(char_u
*), c
);
3487 if (ga_grow(gap
, c
) == OK
)
3490 ((char_u
**)(gap
->ga_data
))[gap
->ga_len
++] =
3491 read_cnt_string(fd
, 1, &cnt
);
3492 /* <comppatlen> <comppattext> */
3499 return SP_FORMERROR
;
3501 /* Turn the COMPOUNDRULE items into a regexp pattern:
3502 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
3503 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3504 * Conversion to utf-8 may double the size. */
3510 pat
= alloc((unsigned)c
);
3512 return SP_OTHERERROR
;
3514 /* We also need a list of all flags that can appear at the start and one
3516 cp
= alloc(todo
+ 1);
3520 return SP_OTHERERROR
;
3522 slang
->sl_compstartflags
= cp
;
3525 ap
= alloc(todo
+ 1);
3529 return SP_OTHERERROR
;
3531 slang
->sl_compallflags
= ap
;
3542 c
= getc(fd
); /* <compflags> */
3544 /* Add all flags to "sl_compallflags". */
3545 if (vim_strchr((char_u
*)"+*[]/", c
) == NULL
3546 && !byte_in_str(slang
->sl_compallflags
, c
))
3554 /* At start of item: copy flags to "sl_compstartflags". For a
3555 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3562 if (!byte_in_str(slang
->sl_compstartflags
, c
))
3571 if (c
== '/') /* slash separates two items */
3577 else /* normal char, "[abc]" and '*' are copied as-is */
3579 if (c
== '+' || c
== '~')
3580 *pp
++ = '\\'; /* "a+" becomes "a\+" */
3583 pp
+= mb_char2bytes(c
, pp
);
3595 slang
->sl_compprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
+ RE_STRICT
);
3597 if (slang
->sl_compprog
== NULL
)
3598 return SP_FORMERROR
;
3604 * Return TRUE if byte "n" appears in "str".
3605 * Like strchr() but independent of locale.
3614 for (p
= str
; *p
!= NUL
; ++p
)
3620 #define SY_MAXLEN 30
3621 typedef struct syl_item_S
3623 char_u sy_chars
[SY_MAXLEN
]; /* the sequence of chars */
3628 * Truncate "slang->sl_syllable" at the first slash and put the following items
3629 * in "slang->sl_syl_items".
3640 ga_init2(&slang
->sl_syl_items
, sizeof(syl_item_T
), 4);
3641 p
= vim_strchr(slang
->sl_syllable
, '/');
3645 if (*p
== NUL
) /* trailing slash */
3648 p
= vim_strchr(p
, '/');
3654 return SP_FORMERROR
;
3655 if (ga_grow(&slang
->sl_syl_items
, 1) == FAIL
)
3656 return SP_OTHERERROR
;
3657 syl
= ((syl_item_T
*)slang
->sl_syl_items
.ga_data
)
3658 + slang
->sl_syl_items
.ga_len
++;
3659 vim_strncpy(syl
->sy_chars
, s
, l
);
3666 * Count the number of syllables in "word".
3667 * When "word" contains spaces the syllables after the last space are counted.
3668 * Returns zero if syllables are not defines.
3671 count_syllables(slang
, word
)
3683 if (slang
->sl_syllable
== NULL
)
3686 for (p
= word
; *p
!= NUL
; p
+= len
)
3688 /* When running into a space reset counter. */
3696 /* Find longest match of syllable items. */
3698 for (i
= 0; i
< slang
->sl_syl_items
.ga_len
; ++i
)
3700 syl
= ((syl_item_T
*)slang
->sl_syl_items
.ga_data
) + i
;
3701 if (syl
->sy_len
> len
3702 && STRNCMP(p
, syl
->sy_chars
, syl
->sy_len
) == 0)
3705 if (len
!= 0) /* found a match, count syllable */
3712 /* No recognized syllable item, at least a syllable char then? */
3715 len
= (*mb_ptr2len
)(p
);
3720 if (vim_strchr(slang
->sl_syllable
, c
) == NULL
)
3721 skip
= FALSE
; /* No, search for next syllable */
3724 ++cnt
; /* Yes, count it */
3725 skip
= TRUE
; /* don't count following syllable chars */
3733 * Set the SOFOFROM and SOFOTO items in language "lp".
3734 * Returns SP_*ERROR flags when there is something wrong.
3737 set_sofo(lp
, from
, to
)
3753 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3754 * characters. The index is the low byte of the character.
3755 * The list contains from-to pairs with a terminating NUL.
3756 * sl_sal_first[] is used for latin1 "from" characters. */
3758 ga_init2(gap
, sizeof(int *), 1);
3759 if (ga_grow(gap
, 256) == FAIL
)
3760 return SP_OTHERERROR
;
3761 vim_memset(gap
->ga_data
, 0, sizeof(int *) * 256);
3764 /* First count the number of items for each list. Temporarily use
3765 * sl_sal_first[] for this. */
3766 for (p
= from
, s
= to
; *p
!= NUL
&& *s
!= NUL
; )
3768 c
= mb_cptr2char_adv(&p
);
3771 ++lp
->sl_sal_first
[c
& 0xff];
3773 if (*p
!= NUL
|| *s
!= NUL
) /* lengths differ */
3774 return SP_FORMERROR
;
3776 /* Allocate the lists. */
3777 for (i
= 0; i
< 256; ++i
)
3778 if (lp
->sl_sal_first
[i
] > 0)
3780 p
= alloc(sizeof(int) * (lp
->sl_sal_first
[i
] * 2 + 1));
3782 return SP_OTHERERROR
;
3783 ((int **)gap
->ga_data
)[i
] = (int *)p
;
3787 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3789 vim_memset(lp
->sl_sal_first
, 0, sizeof(salfirst_T
) * 256);
3790 for (p
= from
, s
= to
; *p
!= NUL
&& *s
!= NUL
; )
3792 c
= mb_cptr2char_adv(&p
);
3793 i
= mb_cptr2char_adv(&s
);
3796 /* Append the from-to chars at the end of the list with
3798 inp
= ((int **)gap
->ga_data
)[c
& 0xff];
3801 *inp
++ = c
; /* from char */
3802 *inp
++ = i
; /* to char */
3803 *inp
++ = NUL
; /* NUL at the end */
3806 /* mapping byte to char is done in sl_sal_first[] */
3807 lp
->sl_sal_first
[c
] = i
;
3813 /* mapping bytes to bytes is done in sl_sal_first[] */
3814 if (STRLEN(from
) != STRLEN(to
))
3815 return SP_FORMERROR
;
3817 for (i
= 0; to
[i
] != NUL
; ++i
)
3818 lp
->sl_sal_first
[from
[i
]] = to
[i
];
3819 lp
->sl_sal
.ga_len
= 1; /* indicates we have soundfolding */
3826 * Fill the first-index table for "lp".
3836 garray_T
*gap
= &lp
->sl_sal
;
3838 sfirst
= lp
->sl_sal_first
;
3839 for (i
= 0; i
< 256; ++i
)
3841 smp
= (salitem_T
*)gap
->ga_data
;
3842 for (i
= 0; i
< gap
->ga_len
; ++i
)
3846 /* Use the lowest byte of the first character. For latin1 it's
3847 * the character, for other encodings it should differ for most
3849 c
= *smp
[i
].sm_lead_w
& 0xff;
3852 c
= *smp
[i
].sm_lead
;
3853 if (sfirst
[c
] == -1)
3861 /* Make sure all entries with this byte are following each
3862 * other. Move the ones that are in the wrong position. Do
3863 * keep the same ordering! */
3864 while (i
+ 1 < gap
->ga_len
3865 && (*smp
[i
+ 1].sm_lead_w
& 0xff) == c
)
3866 /* Skip over entry with same index byte. */
3869 for (n
= 1; i
+ n
< gap
->ga_len
; ++n
)
3870 if ((*smp
[i
+ n
].sm_lead_w
& 0xff) == c
)
3874 /* Move entry with same index byte after the entries
3875 * we already found. */
3879 mch_memmove(smp
+ i
+ 1, smp
+ i
,
3880 sizeof(salitem_T
) * n
);
3891 * Turn a multi-byte string into a wide character string.
3892 * Return it in allocated memory (NULL for out-of-memory)
3902 res
= (int *)alloc(sizeof(int) * (mb_charlen(s
) + 1));
3905 for (p
= s
; *p
!= NUL
; )
3906 res
[i
++] = mb_ptr2char_adv(&p
);
3914 * Read a tree from the .spl or .sug file.
3915 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3916 * This is skipped when the tree has zero length.
3917 * Returns zero when OK, SP_ value for an error.
3920 spell_read_tree(fd
, bytsp
, idxsp
, prefixtree
, prefixcnt
)
3924 int prefixtree
; /* TRUE for the prefix tree */
3925 int prefixcnt
; /* when "prefixtree" is TRUE: prefix count */
3932 /* The tree size was computed when writing the file, so that we can
3933 * allocate it as one long block. <nodecount> */
3936 return SP_TRUNCERROR
;
3939 /* Allocate the byte array. */
3940 bp
= lalloc((long_u
)len
, TRUE
);
3942 return SP_OTHERERROR
;
3945 /* Allocate the index array. */
3946 ip
= (idx_T
*)lalloc_clear((long_u
)(len
* sizeof(int)), TRUE
);
3948 return SP_OTHERERROR
;
3951 /* Recursively read the tree and store it in the array. */
3952 idx
= read_tree_node(fd
, bp
, ip
, len
, 0, prefixtree
, prefixcnt
);
3960 * Read one row of siblings from the spell file and store it in the byte array
3961 * "byts" and index array "idxs". Recursively read the children.
3963 * NOTE: The code here must match put_node()!
3965 * Returns the index (>= 0) following the siblings.
3966 * Returns SP_TRUNCERROR if the file is shorter than expected.
3967 * Returns SP_FORMERROR if there is a format error.
3970 read_tree_node(fd
, byts
, idxs
, maxidx
, startidx
, prefixtree
, maxprefcondnr
)
3974 int maxidx
; /* size of arrays */
3975 idx_T startidx
; /* current index in "byts" and "idxs" */
3976 int prefixtree
; /* TRUE for reading PREFIXTREE */
3977 int maxprefcondnr
; /* maximum for <prefcondnr> */
3982 idx_T idx
= startidx
;
3985 #define SHARED_MASK 0x8000000
3987 len
= getc(fd
); /* <siblingcount> */
3989 return SP_TRUNCERROR
;
3991 if (startidx
+ len
>= maxidx
)
3992 return SP_FORMERROR
;
3995 /* Read the byte values, flag/region bytes and shared indexes. */
3996 for (i
= 1; i
<= len
; ++i
)
3998 c
= getc(fd
); /* <byte> */
4000 return SP_TRUNCERROR
;
4001 if (c
<= BY_SPECIAL
)
4003 if (c
== BY_NOFLAGS
&& !prefixtree
)
4005 /* No flags, all regions. */
4009 else if (c
!= BY_INDEX
)
4013 /* Read the optional pflags byte, the prefix ID and the
4014 * condition nr. In idxs[] store the prefix ID in the low
4015 * byte, the condition index shifted up 8 bits, the flags
4016 * shifted up 24 bits. */
4018 c
= getc(fd
) << 24; /* <pflags> */
4022 c
|= getc(fd
); /* <affixID> */
4024 n
= get2c(fd
); /* <prefcondnr> */
4025 if (n
>= maxprefcondnr
)
4026 return SP_FORMERROR
;
4029 else /* c must be BY_FLAGS or BY_FLAGS2 */
4031 /* Read flags and optional region and prefix ID. In
4032 * idxs[] the flags go in the low two bytes, region above
4033 * that and prefix ID above the region. */
4035 c
= getc(fd
); /* <flags> */
4036 if (c2
== BY_FLAGS2
)
4037 c
= (getc(fd
) << 8) + c
; /* <flags2> */
4039 c
= (getc(fd
) << 16) + c
; /* <region> */
4041 c
= (getc(fd
) << 24) + c
; /* <affixID> */
4047 else /* c == BY_INDEX */
4051 if (n
< 0 || n
>= maxidx
)
4052 return SP_FORMERROR
;
4053 idxs
[idx
] = n
+ SHARED_MASK
;
4054 c
= getc(fd
); /* <xbyte> */
4060 /* Recursively read the children for non-shared siblings.
4061 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4062 * remove SHARED_MASK) */
4063 for (i
= 1; i
<= len
; ++i
)
4064 if (byts
[startidx
+ i
] != 0)
4066 if (idxs
[startidx
+ i
] & SHARED_MASK
)
4067 idxs
[startidx
+ i
] &= ~SHARED_MASK
;
4070 idxs
[startidx
+ i
] = idx
;
4071 idx
= read_tree_node(fd
, byts
, idxs
, maxidx
, idx
,
4072 prefixtree
, maxprefcondnr
);
4082 * Parse 'spelllang' and set buf->b_langp accordingly.
4083 * Returns NULL if it's OK, an error message otherwise.
4086 did_set_spelllang(buf
)
4092 char_u region_cp
[3];
4097 char_u lang
[MAXWLEN
+ 1];
4098 char_u spf_name
[MAXPATHL
];
4103 char_u
*use_region
= NULL
;
4104 int dont_use_region
= FALSE
;
4105 int nobreak
= FALSE
;
4108 static int recursive
= FALSE
;
4109 char_u
*ret_msg
= NULL
;
4112 /* We don't want to do this recursively. May happen when a language is
4113 * not available and the SpellFileMissing autocommand opens a new buffer
4114 * in which 'spell' is set. */
4119 ga_init2(&ga
, sizeof(langp_T
), 2);
4122 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4123 * it under our fingers. */
4124 spl_copy
= vim_strsave(buf
->b_p_spl
);
4125 if (spl_copy
== NULL
)
4128 /* loop over comma separated language names. */
4129 for (splp
= spl_copy
; *splp
!= NUL
; )
4131 /* Get one language name. */
4132 copy_option_part(&splp
, lang
, MAXWLEN
, ",");
4135 len
= (int)STRLEN(lang
);
4137 /* If the name ends in ".spl" use it as the name of the spell file.
4138 * If there is a region name let "region" point to it and remove it
4140 if (len
> 4 && fnamecmp(lang
+ len
- 4, ".spl") == 0)
4144 /* Locate a region and remove it from the file name. */
4145 p
= vim_strchr(gettail(lang
), '_');
4146 if (p
!= NULL
&& ASCII_ISALPHA(p
[1]) && ASCII_ISALPHA(p
[2])
4147 && !ASCII_ISALPHA(p
[3]))
4149 vim_strncpy(region_cp
, p
+ 1, 2);
4150 mch_memmove(p
, p
+ 3, len
- (p
- lang
) - 2);
4155 dont_use_region
= TRUE
;
4157 /* Check if we loaded this language before. */
4158 for (slang
= first_lang
; slang
!= NULL
; slang
= slang
->sl_next
)
4159 if (fullpathcmp(lang
, slang
->sl_fname
, FALSE
) == FPC_SAME
)
4165 if (len
> 3 && lang
[len
- 3] == '_')
4167 region
= lang
+ len
- 2;
4172 dont_use_region
= TRUE
;
4174 /* Check if we loaded this language before. */
4175 for (slang
= first_lang
; slang
!= NULL
; slang
= slang
->sl_next
)
4176 if (STRICMP(lang
, slang
->sl_name
) == 0)
4182 /* If the region differs from what was used before then don't
4183 * use it for 'spellfile'. */
4184 if (use_region
!= NULL
&& STRCMP(region
, use_region
) != 0)
4185 dont_use_region
= TRUE
;
4186 use_region
= region
;
4189 /* If not found try loading the language now. */
4193 (void)spell_load_file(lang
, lang
, NULL
, FALSE
);
4196 spell_load_lang(lang
);
4198 /* SpellFileMissing autocommands may do anything, including
4199 * destroying the buffer we are using... */
4200 if (!buf_valid(buf
))
4202 ret_msg
= (char_u
*)"E797: SpellFileMissing autocommand deleted buffer";
4210 * Loop over the languages, there can be several files for "lang".
4212 for (slang
= first_lang
; slang
!= NULL
; slang
= slang
->sl_next
)
4213 if (filename
? fullpathcmp(lang
, slang
->sl_fname
, FALSE
) == FPC_SAME
4214 : STRICMP(lang
, slang
->sl_name
) == 0)
4216 region_mask
= REGION_ALL
;
4217 if (!filename
&& region
!= NULL
)
4219 /* find region in sl_regions */
4220 c
= find_region(slang
->sl_regions
, region
);
4221 if (c
== REGION_ALL
)
4225 if (*slang
->sl_regions
!= NUL
)
4226 /* This addition file is for other regions. */
4230 /* This is probably an error. Give a warning and
4231 * accept the words anyway. */
4233 _("Warning: region %s not supported"),
4237 region_mask
= 1 << c
;
4240 if (region_mask
!= 0)
4242 if (ga_grow(&ga
, 1) == FAIL
)
4245 ret_msg
= e_outofmem
;
4248 LANGP_ENTRY(ga
, ga
.ga_len
)->lp_slang
= slang
;
4249 LANGP_ENTRY(ga
, ga
.ga_len
)->lp_region
= region_mask
;
4251 use_midword(slang
, buf
);
4252 if (slang
->sl_nobreak
)
4258 /* round 0: load int_wordlist, if possible.
4259 * round 1: load first name in 'spellfile'.
4260 * round 2: load second name in 'spellfile.
4263 for (round
= 0; round
== 0 || *spf
!= NUL
; ++round
)
4267 /* Internal wordlist, if there is one. */
4268 if (int_wordlist
== NULL
)
4270 int_wordlist_spl(spf_name
);
4274 /* One entry in 'spellfile'. */
4275 copy_option_part(&spf
, spf_name
, MAXPATHL
- 5, ",");
4276 STRCAT(spf_name
, ".spl");
4278 /* If it was already found above then skip it. */
4279 for (c
= 0; c
< ga
.ga_len
; ++c
)
4281 p
= LANGP_ENTRY(ga
, c
)->lp_slang
->sl_fname
;
4282 if (p
!= NULL
&& fullpathcmp(spf_name
, p
, FALSE
) == FPC_SAME
)
4289 /* Check if it was loaded already. */
4290 for (slang
= first_lang
; slang
!= NULL
; slang
= slang
->sl_next
)
4291 if (fullpathcmp(spf_name
, slang
->sl_fname
, FALSE
) == FPC_SAME
)
4295 /* Not loaded, try loading it now. The language name includes the
4296 * region name, the region is ignored otherwise. for int_wordlist
4297 * use an arbitrary name. */
4299 STRCPY(lang
, "internal wordlist");
4302 vim_strncpy(lang
, gettail(spf_name
), MAXWLEN
);
4303 p
= vim_strchr(lang
, '.');
4305 *p
= NUL
; /* truncate at ".encoding.add" */
4307 slang
= spell_load_file(spf_name
, lang
, NULL
, TRUE
);
4309 /* If one of the languages has NOBREAK we assume the addition
4310 * files also have this. */
4311 if (slang
!= NULL
&& nobreak
)
4312 slang
->sl_nobreak
= TRUE
;
4314 if (slang
!= NULL
&& ga_grow(&ga
, 1) == OK
)
4316 region_mask
= REGION_ALL
;
4317 if (use_region
!= NULL
&& !dont_use_region
)
4319 /* find region in sl_regions */
4320 c
= find_region(slang
->sl_regions
, use_region
);
4321 if (c
!= REGION_ALL
)
4322 region_mask
= 1 << c
;
4323 else if (*slang
->sl_regions
!= NUL
)
4324 /* This spell file is for other regions. */
4328 if (region_mask
!= 0)
4330 LANGP_ENTRY(ga
, ga
.ga_len
)->lp_slang
= slang
;
4331 LANGP_ENTRY(ga
, ga
.ga_len
)->lp_sallang
= NULL
;
4332 LANGP_ENTRY(ga
, ga
.ga_len
)->lp_replang
= NULL
;
4333 LANGP_ENTRY(ga
, ga
.ga_len
)->lp_region
= region_mask
;
4335 use_midword(slang
, buf
);
4340 /* Everything is fine, store the new b_langp value. */
4341 ga_clear(&buf
->b_langp
);
4344 /* For each language figure out what language to use for sound folding and
4345 * REP items. If the language doesn't support it itself use another one
4346 * with the same name. E.g. for "en-math" use "en". */
4347 for (i
= 0; i
< ga
.ga_len
; ++i
)
4349 lp
= LANGP_ENTRY(ga
, i
);
4352 if (lp
->lp_slang
->sl_sal
.ga_len
> 0)
4353 /* language does sound folding itself */
4354 lp
->lp_sallang
= lp
->lp_slang
;
4356 /* find first similar language that does sound folding */
4357 for (j
= 0; j
< ga
.ga_len
; ++j
)
4359 lp2
= LANGP_ENTRY(ga
, j
);
4360 if (lp2
->lp_slang
->sl_sal
.ga_len
> 0
4361 && STRNCMP(lp
->lp_slang
->sl_name
,
4362 lp2
->lp_slang
->sl_name
, 2) == 0)
4364 lp
->lp_sallang
= lp2
->lp_slang
;
4370 if (lp
->lp_slang
->sl_rep
.ga_len
> 0)
4371 /* language has REP items itself */
4372 lp
->lp_replang
= lp
->lp_slang
;
4374 /* find first similar language that has REP items */
4375 for (j
= 0; j
< ga
.ga_len
; ++j
)
4377 lp2
= LANGP_ENTRY(ga
, j
);
4378 if (lp2
->lp_slang
->sl_rep
.ga_len
> 0
4379 && STRNCMP(lp
->lp_slang
->sl_name
,
4380 lp2
->lp_slang
->sl_name
, 2) == 0)
4382 lp
->lp_replang
= lp2
->lp_slang
;
4395 * Clear the midword characters for buffer "buf".
4401 vim_memset(buf
->b_spell_ismw
, 0, 256);
4403 vim_free(buf
->b_spell_ismw_mb
);
4404 buf
->b_spell_ismw_mb
= NULL
;
4409 * Use the "sl_midword" field of language "lp" for buffer "buf".
4410 * They add up to any currently used midword characters.
4413 use_midword(lp
, buf
)
4419 if (lp
->sl_midword
== NULL
) /* there aren't any */
4422 for (p
= lp
->sl_midword
; *p
!= NUL
; )
4430 l
= (*mb_ptr2len
)(p
);
4431 if (c
< 256 && l
<= 2)
4432 buf
->b_spell_ismw
[c
] = TRUE
;
4433 else if (buf
->b_spell_ismw_mb
== NULL
)
4434 /* First multi-byte char in "b_spell_ismw_mb". */
4435 buf
->b_spell_ismw_mb
= vim_strnsave(p
, l
);
4438 /* Append multi-byte chars to "b_spell_ismw_mb". */
4439 n
= (int)STRLEN(buf
->b_spell_ismw_mb
);
4440 bp
= vim_strnsave(buf
->b_spell_ismw_mb
, n
+ l
);
4443 vim_free(buf
->b_spell_ismw_mb
);
4444 buf
->b_spell_ismw_mb
= bp
;
4445 vim_strncpy(bp
+ n
, p
, l
);
4452 buf
->b_spell_ismw
[*p
++] = TRUE
;
4456 * Find the region "region[2]" in "rp" (points to "sl_regions").
4457 * Each region is simply stored as the two characters of it's name.
4458 * Returns the index if found (first is 0), REGION_ALL if not found.
4461 find_region(rp
, region
)
4467 for (i
= 0; ; i
+= 2)
4471 if (rp
[i
] == region
[0] && rp
[i
+ 1] == region
[1])
4478 * Return case type of word:
4482 * WoRd wOrd WF_KEEPCAP
4487 char_u
*end
; /* When NULL use up to NUL byte. */
4493 int past_second
= FALSE
; /* past second word char */
4495 /* find first letter */
4496 for (p
= word
; !spell_iswordp_nmw(p
); mb_ptr_adv(p
))
4497 if (end
== NULL
? *p
== NUL
: p
>= end
)
4498 return 0; /* only non-word characters, illegal word */
4501 c
= mb_ptr2char_adv(&p
);
4505 firstcap
= allcap
= SPELL_ISUPPER(c
);
4508 * Need to check all letters to find a word with mixed upper/lower.
4509 * But a word with an upper char only at start is a ONECAP.
4511 for ( ; end
== NULL
? *p
!= NUL
: p
< end
; mb_ptr_adv(p
))
4512 if (spell_iswordp_nmw(p
))
4515 if (!SPELL_ISUPPER(c
))
4517 /* UUl -> KEEPCAP */
4518 if (past_second
&& allcap
)
4523 /* UlU -> KEEPCAP */
4536 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4537 * capital. So that make_case_word() can turn WOrd into Word.
4538 * Add ALLCAP for "WOrD".
4541 badword_captype(word
, end
)
4545 int flags
= captype(word
, end
);
4551 if (flags
& WF_KEEPCAP
)
4553 /* Count the number of UPPER and lower case letters. */
4556 for (p
= word
; p
< end
; mb_ptr_adv(p
))
4559 if (SPELL_ISUPPER(c
))
4569 /* If there are more UPPER than lower case letters suggest an
4570 * ALLCAP word. Otherwise, if the first letter is UPPER then
4571 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4572 * require three upper case letters. */
4578 if (u
>= 2 && l
>= 2) /* maCARONI maCAroni */
4584 # if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4586 * Free all languages.
4593 char_u fname
[MAXPATHL
];
4595 /* Go through all buffers and handle 'spelllang'. */
4596 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
4597 ga_clear(&buf
->b_langp
);
4599 while (first_lang
!= NULL
)
4602 first_lang
= slang
->sl_next
;
4606 if (int_wordlist
!= NULL
)
4608 /* Delete the internal wordlist and its .spl file */
4609 mch_remove(int_wordlist
);
4610 int_wordlist_spl(fname
);
4612 vim_free(int_wordlist
);
4613 int_wordlist
= NULL
;
4616 init_spell_chartab();
4620 vim_free(repl_from
);
4625 # if defined(FEAT_MBYTE) || defined(PROTO)
4627 * Clear all spelling tables and reload them.
4628 * Used after 'encoding' is set and when ":mkspell" was used.
4636 /* Initialize the table for spell_iswordp(). */
4637 init_spell_chartab();
4639 /* Unload all allocated memory. */
4642 /* Go through all buffers and handle 'spelllang'. */
4643 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
4645 /* Only load the wordlists when 'spelllang' is set and there is a
4646 * window for this buffer in which 'spell' is set. */
4647 if (*buf
->b_p_spl
!= NUL
)
4650 if (wp
->w_buffer
== buf
&& wp
->w_p_spell
)
4652 (void)did_set_spelllang(buf
);
4653 # ifdef FEAT_WINDOWS
4663 * Reload the spell file "fname" if it's loaded.
4666 spell_reload_one(fname
, added_word
)
4668 int added_word
; /* invoked through "zg" */
4673 for (slang
= first_lang
; slang
!= NULL
; slang
= slang
->sl_next
)
4675 if (fullpathcmp(fname
, slang
->sl_fname
, FALSE
) == FPC_SAME
)
4678 if (spell_load_file(fname
, NULL
, slang
, FALSE
) == NULL
)
4679 /* reloading failed, clear the language */
4681 redraw_all_later(SOME_VALID
);
4686 /* When "zg" was used and the file wasn't loaded yet, should redo
4687 * 'spelllang' to load it now. */
4688 if (added_word
&& !didit
)
4689 did_set_spelllang(curbuf
);
4694 * Functions for ":mkspell".
4697 #define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
4700 * Main structure to store the contents of a ".aff" file.
4702 typedef struct afffile_S
4704 char_u
*af_enc
; /* "SET", normalized, alloc'ed string or NULL */
4705 int af_flagtype
; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
4706 unsigned af_rare
; /* RARE ID for rare word */
4707 unsigned af_keepcase
; /* KEEPCASE ID for keep-case word */
4708 unsigned af_bad
; /* BAD ID for banned word */
4709 unsigned af_needaffix
; /* NEEDAFFIX ID */
4710 unsigned af_circumfix
; /* CIRCUMFIX ID */
4711 unsigned af_needcomp
; /* NEEDCOMPOUND ID */
4712 unsigned af_comproot
; /* COMPOUNDROOT ID */
4713 unsigned af_compforbid
; /* COMPOUNDFORBIDFLAG ID */
4714 unsigned af_comppermit
; /* COMPOUNDPERMITFLAG ID */
4715 unsigned af_nosuggest
; /* NOSUGGEST ID */
4716 int af_pfxpostpone
; /* postpone prefixes without chop string and
4718 hashtab_T af_pref
; /* hashtable for prefixes, affheader_T */
4719 hashtab_T af_suff
; /* hashtable for suffixes, affheader_T */
4720 hashtab_T af_comp
; /* hashtable for compound flags, compitem_T */
4723 #define AFT_CHAR 0 /* flags are one character */
4724 #define AFT_LONG 1 /* flags are two characters */
4725 #define AFT_CAPLONG 2 /* flags are one or two characters */
4726 #define AFT_NUM 3 /* flags are numbers, comma separated */
4728 typedef struct affentry_S affentry_T
;
4729 /* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4732 affentry_T
*ae_next
; /* next affix with same name/number */
4733 char_u
*ae_chop
; /* text to chop off basic word (can be NULL) */
4734 char_u
*ae_add
; /* text to add to basic word (can be NULL) */
4735 char_u
*ae_flags
; /* flags on the affix (can be NULL) */
4736 char_u
*ae_cond
; /* condition (NULL for ".") */
4737 regprog_T
*ae_prog
; /* regexp program for ae_cond or NULL */
4738 char ae_compforbid
; /* COMPOUNDFORBIDFLAG found */
4739 char ae_comppermit
; /* COMPOUNDPERMITFLAG found */
4743 # define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4745 # define AH_KEY_LEN 7 /* 6 digits + NUL */
4748 /* Affix header from ".aff" file. Used for af_pref and af_suff. */
4749 typedef struct affheader_S
4751 char_u ah_key
[AH_KEY_LEN
]; /* key for hashtab == name of affix */
4752 unsigned ah_flag
; /* affix name as number, uses "af_flagtype" */
4753 int ah_newID
; /* prefix ID after renumbering; 0 if not used */
4754 int ah_combine
; /* suffix may combine with prefix */
4755 int ah_follows
; /* another affix block should be following */
4756 affentry_T
*ah_first
; /* first affix entry */
4759 #define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4761 /* Flag used in compound items. */
4762 typedef struct compitem_S
4764 char_u ci_key
[AH_KEY_LEN
]; /* key for hashtab == name of compound */
4765 unsigned ci_flag
; /* affix name as number, uses "af_flagtype" */
4766 int ci_newID
; /* affix ID after renumbering. */
4769 #define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4772 * Structure that is used to store the items in the word tree. This avoids
4773 * the need to keep track of each allocated thing, everything is freed all at
4774 * once after ":mkspell" is done.
4776 #define SBLOCKSIZE 16000 /* size of sb_data */
4777 typedef struct sblock_S sblock_T
;
4780 sblock_T
*sb_next
; /* next block in list */
4781 int sb_used
; /* nr of bytes already in use */
4782 char_u sb_data
[1]; /* data, actually longer */
4786 * A node in the tree.
4788 typedef struct wordnode_S wordnode_T
;
4791 union /* shared to save space */
4793 char_u hashkey
[6]; /* the hash key, only used while compressing */
4794 int index
; /* index in written nodes (valid after first
4797 union /* shared to save space */
4799 wordnode_T
*next
; /* next node with same hash key */
4800 wordnode_T
*wnode
; /* parent node that will write this node */
4802 wordnode_T
*wn_child
; /* child (next byte in word) */
4803 wordnode_T
*wn_sibling
; /* next sibling (alternate byte in word,
4805 int wn_refs
; /* Nr. of references to this node. Only
4806 relevant for first node in a list of
4807 siblings, in following siblings it is
4809 char_u wn_byte
; /* Byte for this node. NUL for word end */
4811 /* Info for when "wn_byte" is NUL.
4812 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4813 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4814 * "wn_region" the LSW of the wordnr. */
4815 char_u wn_affixID
; /* supported/required prefix ID or 0 */
4816 short_u wn_flags
; /* WF_ flags */
4817 short wn_region
; /* region mask */
4819 #ifdef SPELL_PRINTTREE
4820 int wn_nr
; /* sequence nr for printing */
4824 #define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4826 #define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
4829 * Info used while reading the spell files.
4831 typedef struct spellinfo_S
4833 wordnode_T
*si_foldroot
; /* tree with case-folded words */
4834 long si_foldwcount
; /* nr of words in si_foldroot */
4836 wordnode_T
*si_keeproot
; /* tree with keep-case words */
4837 long si_keepwcount
; /* nr of words in si_keeproot */
4839 wordnode_T
*si_prefroot
; /* tree with postponed prefixes */
4841 long si_sugtree
; /* creating the soundfolding trie */
4843 sblock_T
*si_blocks
; /* memory blocks used */
4844 long si_blocks_cnt
; /* memory blocks allocated */
4845 long si_compress_cnt
; /* words to add before lowering
4846 compression limit */
4847 wordnode_T
*si_first_free
; /* List of nodes that have been freed during
4848 compression, linked by "wn_child" field. */
4849 long si_free_count
; /* number of nodes in si_first_free */
4850 #ifdef SPELL_PRINTTREE
4851 int si_wordnode_nr
; /* sequence nr for nodes */
4853 buf_T
*si_spellbuf
; /* buffer used to store soundfold word table */
4855 int si_ascii
; /* handling only ASCII words */
4856 int si_add
; /* addition file */
4857 int si_clear_chartab
; /* when TRUE clear char tables */
4858 int si_region
; /* region mask */
4859 vimconv_T si_conv
; /* for conversion to 'encoding' */
4860 int si_memtot
; /* runtime memory used */
4861 int si_verbose
; /* verbose messages */
4862 int si_msg_count
; /* number of words added since last message */
4863 char_u
*si_info
; /* info text chars or NULL */
4864 int si_region_count
; /* number of regions supported (1 when there
4866 char_u si_region_name
[16]; /* region names; used only if
4867 * si_region_count > 1) */
4869 garray_T si_rep
; /* list of fromto_T entries from REP lines */
4870 garray_T si_repsal
; /* list of fromto_T entries from REPSAL lines */
4871 garray_T si_sal
; /* list of fromto_T entries from SAL lines */
4872 char_u
*si_sofofr
; /* SOFOFROM text */
4873 char_u
*si_sofoto
; /* SOFOTO text */
4874 int si_nosugfile
; /* NOSUGFILE item found */
4875 int si_nosplitsugs
; /* NOSPLITSUGS item found */
4876 int si_followup
; /* soundsalike: ? */
4877 int si_collapse
; /* soundsalike: ? */
4878 hashtab_T si_commonwords
; /* hashtable for common words */
4879 time_t si_sugtime
; /* timestamp for .sug file */
4880 int si_rem_accents
; /* soundsalike: remove accents */
4881 garray_T si_map
; /* MAP info concatenated */
4882 char_u
*si_midword
; /* MIDWORD chars or NULL */
4883 int si_compmax
; /* max nr of words for compounding */
4884 int si_compminlen
; /* minimal length for compounding */
4885 int si_compsylmax
; /* max nr of syllables for compounding */
4886 int si_compoptions
; /* COMP_ flags */
4887 garray_T si_comppat
; /* CHECKCOMPOUNDPATTERN items, each stored as
4889 char_u
*si_compflags
; /* flags used for compounding */
4890 char_u si_nobreak
; /* NOBREAK */
4891 char_u
*si_syllable
; /* syllable string */
4892 garray_T si_prefcond
; /* table with conditions for postponed
4893 * prefixes, each stored as a string */
4894 int si_newprefID
; /* current value for ah_newID */
4895 int si_newcompID
; /* current value for compound ID */
4898 static afffile_T
*spell_read_aff
__ARGS((spellinfo_T
*spin
, char_u
*fname
));
4899 static void aff_process_flags
__ARGS((afffile_T
*affile
, affentry_T
*entry
));
4900 static int spell_info_item
__ARGS((char_u
*s
));
4901 static unsigned affitem2flag
__ARGS((int flagtype
, char_u
*item
, char_u
*fname
, int lnum
));
4902 static unsigned get_affitem
__ARGS((int flagtype
, char_u
**pp
));
4903 static void process_compflags
__ARGS((spellinfo_T
*spin
, afffile_T
*aff
, char_u
*compflags
));
4904 static void check_renumber
__ARGS((spellinfo_T
*spin
));
4905 static int flag_in_afflist
__ARGS((int flagtype
, char_u
*afflist
, unsigned flag
));
4906 static void aff_check_number
__ARGS((int spinval
, int affval
, char *name
));
4907 static void aff_check_string
__ARGS((char_u
*spinval
, char_u
*affval
, char *name
));
4908 static int str_equal
__ARGS((char_u
*s1
, char_u
*s2
));
4909 static void add_fromto
__ARGS((spellinfo_T
*spin
, garray_T
*gap
, char_u
*from
, char_u
*to
));
4910 static int sal_to_bool
__ARGS((char_u
*s
));
4911 static int has_non_ascii
__ARGS((char_u
*s
));
4912 static void spell_free_aff
__ARGS((afffile_T
*aff
));
4913 static int spell_read_dic
__ARGS((spellinfo_T
*spin
, char_u
*fname
, afffile_T
*affile
));
4914 static int get_affix_flags
__ARGS((afffile_T
*affile
, char_u
*afflist
));
4915 static int get_pfxlist
__ARGS((afffile_T
*affile
, char_u
*afflist
, char_u
*store_afflist
));
4916 static void get_compflags
__ARGS((afffile_T
*affile
, char_u
*afflist
, char_u
*store_afflist
));
4917 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
));
4918 static int spell_read_wordfile
__ARGS((spellinfo_T
*spin
, char_u
*fname
));
4919 static void *getroom
__ARGS((spellinfo_T
*spin
, size_t len
, int align
));
4920 static char_u
*getroom_save
__ARGS((spellinfo_T
*spin
, char_u
*s
));
4921 static void free_blocks
__ARGS((sblock_T
*bl
));
4922 static wordnode_T
*wordtree_alloc
__ARGS((spellinfo_T
*spin
));
4923 static int store_word
__ARGS((spellinfo_T
*spin
, char_u
*word
, int flags
, int region
, char_u
*pfxlist
, int need_affix
));
4924 static int tree_add_word
__ARGS((spellinfo_T
*spin
, char_u
*word
, wordnode_T
*tree
, int flags
, int region
, int affixID
));
4925 static wordnode_T
*get_wordnode
__ARGS((spellinfo_T
*spin
));
4926 static int deref_wordnode
__ARGS((spellinfo_T
*spin
, wordnode_T
*node
));
4927 static void free_wordnode
__ARGS((spellinfo_T
*spin
, wordnode_T
*n
));
4928 static void wordtree_compress
__ARGS((spellinfo_T
*spin
, wordnode_T
*root
));
4929 static int node_compress
__ARGS((spellinfo_T
*spin
, wordnode_T
*node
, hashtab_T
*ht
, int *tot
));
4930 static int node_equal
__ARGS((wordnode_T
*n1
, wordnode_T
*n2
));
4931 static void put_sugtime
__ARGS((spellinfo_T
*spin
, FILE *fd
));
4932 static int write_vim_spell
__ARGS((spellinfo_T
*spin
, char_u
*fname
));
4933 static void clear_node
__ARGS((wordnode_T
*node
));
4934 static int put_node
__ARGS((FILE *fd
, wordnode_T
*node
, int index
, int regionmask
, int prefixtree
));
4935 static void spell_make_sugfile
__ARGS((spellinfo_T
*spin
, char_u
*wfname
));
4936 static int sug_filltree
__ARGS((spellinfo_T
*spin
, slang_T
*slang
));
4937 static int sug_maketable
__ARGS((spellinfo_T
*spin
));
4938 static int sug_filltable
__ARGS((spellinfo_T
*spin
, wordnode_T
*node
, int startwordnr
, garray_T
*gap
));
4939 static int offset2bytes
__ARGS((int nr
, char_u
*buf
));
4940 static int bytes2offset
__ARGS((char_u
**pp
));
4941 static void sug_write
__ARGS((spellinfo_T
*spin
, char_u
*fname
));
4942 static void mkspell
__ARGS((int fcount
, char_u
**fnames
, int ascii
, int overwrite
, int added_word
));
4943 static void spell_message
__ARGS((spellinfo_T
*spin
, char_u
*str
));
4944 static void init_spellfile
__ARGS((void));
4946 /* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4947 * but it must be negative to indicate the prefix tree to tree_add_word().
4948 * Use a negative number with the lower 8 bits zero. */
4949 #define PFX_FLAGS -256
4951 /* flags for "condit" argument of store_aff_word() */
4952 #define CONDIT_COMB 1 /* affix must combine */
4953 #define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4954 #define CONDIT_SUF 4 /* add a suffix for matching flags */
4955 #define CONDIT_AFF 8 /* word already has an affix */
4958 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4960 static long compress_start
= 30000; /* memory / SBLOCKSIZE */
4961 static long compress_inc
= 100; /* memory / SBLOCKSIZE */
4962 static long compress_added
= 500000; /* word count */
4964 #ifdef SPELL_PRINTTREE
4966 * For debugging the tree code: print the current tree in a (more or less)
4967 * readable format, so that we can see what happens when adding a word and/or
4968 * compressing the tree.
4969 * Based on code from Olaf Seibert.
4971 #define PRINTLINESIZE 1000
4972 #define PRINTWIDTH 6
4974 #define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4975 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4977 static char line1
[PRINTLINESIZE
];
4978 static char line2
[PRINTLINESIZE
];
4979 static char line3
[PRINTLINESIZE
];
4982 spell_clear_flags(wordnode_T
*node
)
4986 for (np
= node
; np
!= NULL
; np
= np
->wn_sibling
)
4988 np
->wn_u1
.index
= FALSE
;
4989 spell_clear_flags(np
->wn_child
);
4994 spell_print_node(wordnode_T
*node
, int depth
)
4996 if (node
->wn_u1
.index
)
4998 /* Done this node before, print the reference. */
4999 PRINTSOME(line1
, depth
, "(%d)", node
->wn_nr
, 0);
5000 PRINTSOME(line2
, depth
, " ", 0, 0);
5001 PRINTSOME(line3
, depth
, " ", 0, 0);
5008 node
->wn_u1
.index
= TRUE
;
5010 if (node
->wn_byte
!= NUL
)
5012 if (node
->wn_child
!= NULL
)
5013 PRINTSOME(line1
, depth
, " %c -> ", node
->wn_byte
, 0);
5015 /* Cannot happen? */
5016 PRINTSOME(line1
, depth
, " %c ???", node
->wn_byte
, 0);
5019 PRINTSOME(line1
, depth
, " $ ", 0, 0);
5021 PRINTSOME(line2
, depth
, "%d/%d ", node
->wn_nr
, node
->wn_refs
);
5023 if (node
->wn_sibling
!= NULL
)
5024 PRINTSOME(line3
, depth
, " | ", 0, 0);
5026 PRINTSOME(line3
, depth
, " ", 0, 0);
5028 if (node
->wn_byte
== NUL
)
5035 /* do the children */
5036 if (node
->wn_byte
!= NUL
&& node
->wn_child
!= NULL
)
5037 spell_print_node(node
->wn_child
, depth
+ 1);
5039 /* do the siblings */
5040 if (node
->wn_sibling
!= NULL
)
5042 /* get rid of all parent details except | */
5043 STRCPY(line1
, line3
);
5044 STRCPY(line2
, line3
);
5045 spell_print_node(node
->wn_sibling
, depth
);
5051 spell_print_tree(wordnode_T
*root
)
5055 /* Clear the "wn_u1.index" fields, used to remember what has been
5057 spell_clear_flags(root
);
5059 /* Recursively print the tree. */
5060 spell_print_node(root
, 0);
5063 #endif /* SPELL_PRINTTREE */
5066 * Read the affix file "fname".
5067 * Returns an afffile_T, NULL for complete failure.
5070 spell_read_aff(spin
, fname
)
5076 char_u rline
[MAXLINELEN
];
5079 #define MAXITEMCNT 30
5080 char_u
*(items
[MAXITEMCNT
]);
5084 affheader_T
*cur_aff
= NULL
;
5085 int did_postpone_prefix
= FALSE
;
5095 int found_map
= FALSE
;
5098 int compminlen
= 0; /* COMPOUNDMIN value */
5099 int compsylmax
= 0; /* COMPOUNDSYLMAX value */
5100 int compoptions
= 0; /* COMP_ flags */
5101 int compmax
= 0; /* COMPOUNDWORDMAX value */
5102 char_u
*compflags
= NULL
; /* COMPOUNDFLAG and COMPOUNDRULE
5104 char_u
*midword
= NULL
; /* MIDWORD value */
5105 char_u
*syllable
= NULL
; /* SYLLABLE value */
5106 char_u
*sofofrom
= NULL
; /* SOFOFROM value */
5107 char_u
*sofoto
= NULL
; /* SOFOTO value */
5112 fd
= mch_fopen((char *)fname
, "r");
5115 EMSG2(_(e_notopen
), fname
);
5119 vim_snprintf((char *)IObuff
, IOSIZE
, _("Reading affix file %s ..."), fname
);
5120 spell_message(spin
, IObuff
);
5122 /* Only do REP lines when not done in another .aff file already. */
5123 do_rep
= spin
->si_rep
.ga_len
== 0;
5125 /* Only do REPSAL lines when not done in another .aff file already. */
5126 do_repsal
= spin
->si_repsal
.ga_len
== 0;
5128 /* Only do SAL lines when not done in another .aff file already. */
5129 do_sal
= spin
->si_sal
.ga_len
== 0;
5131 /* Only do MAP lines when not done in another .aff file already. */
5132 do_mapline
= spin
->si_map
.ga_len
== 0;
5135 * Allocate and init the afffile_T structure.
5137 aff
= (afffile_T
*)getroom(spin
, sizeof(afffile_T
), TRUE
);
5143 hash_init(&aff
->af_pref
);
5144 hash_init(&aff
->af_suff
);
5145 hash_init(&aff
->af_comp
);
5148 * Read all the lines in the file one by one.
5150 while (!vim_fgets(rline
, MAXLINELEN
, fd
) && !got_int
)
5155 /* Skip comment lines. */
5159 /* Convert from "SET" to 'encoding' when needed. */
5162 if (spin
->si_conv
.vc_type
!= CONV_NONE
)
5164 pc
= string_convert(&spin
->si_conv
, rline
, NULL
);
5167 smsg((char_u
*)_("Conversion failure for word in %s line %d: %s"),
5168 fname
, lnum
, rline
);
5180 /* Split the line up in white separated items. Put a NUL after each
5185 while (*p
!= NUL
&& *p
<= ' ') /* skip white space and CR/NL */
5189 if (itemcnt
== MAXITEMCNT
) /* too many items */
5191 items
[itemcnt
++] = p
;
5192 /* A few items have arbitrary text argument, don't split them. */
5193 if (itemcnt
== 2 && spell_info_item(items
[0]))
5194 while (*p
>= ' ' || *p
== TAB
) /* skip until CR/NL */
5197 while (*p
> ' ') /* skip until white space or CR/NL */
5204 /* Handle non-empty lines. */
5207 if (STRCMP(items
[0], "SET") == 0 && itemcnt
== 2
5208 && aff
->af_enc
== NULL
)
5211 /* Setup for conversion from "ENC" to 'encoding'. */
5212 aff
->af_enc
= enc_canonize(items
[1]);
5213 if (aff
->af_enc
!= NULL
&& !spin
->si_ascii
5214 && convert_setup(&spin
->si_conv
, aff
->af_enc
,
5216 smsg((char_u
*)_("Conversion in %s not supported: from %s to %s"),
5217 fname
, aff
->af_enc
, p_enc
);
5218 spin
->si_conv
.vc_fail
= TRUE
;
5220 smsg((char_u
*)_("Conversion in %s not supported"), fname
);
5223 else if (STRCMP(items
[0], "FLAG") == 0 && itemcnt
== 2
5224 && aff
->af_flagtype
== AFT_CHAR
)
5226 if (STRCMP(items
[1], "long") == 0)
5227 aff
->af_flagtype
= AFT_LONG
;
5228 else if (STRCMP(items
[1], "num") == 0)
5229 aff
->af_flagtype
= AFT_NUM
;
5230 else if (STRCMP(items
[1], "caplong") == 0)
5231 aff
->af_flagtype
= AFT_CAPLONG
;
5233 smsg((char_u
*)_("Invalid value for FLAG in %s line %d: %s"),
5234 fname
, lnum
, items
[1]);
5235 if (aff
->af_rare
!= 0
5236 || aff
->af_keepcase
!= 0
5238 || aff
->af_needaffix
!= 0
5239 || aff
->af_circumfix
!= 0
5240 || aff
->af_needcomp
!= 0
5241 || aff
->af_comproot
!= 0
5242 || aff
->af_nosuggest
!= 0
5243 || compflags
!= NULL
5244 || aff
->af_suff
.ht_used
> 0
5245 || aff
->af_pref
.ht_used
> 0)
5246 smsg((char_u
*)_("FLAG after using flags in %s line %d: %s"),
5247 fname
, lnum
, items
[1]);
5249 else if (spell_info_item(items
[0]))
5251 p
= (char_u
*)getroom(spin
,
5252 (spin
->si_info
== NULL
? 0 : STRLEN(spin
->si_info
))
5254 + STRLEN(items
[1]) + 3, FALSE
);
5257 if (spin
->si_info
!= NULL
)
5259 STRCPY(p
, spin
->si_info
);
5262 STRCAT(p
, items
[0]);
5264 STRCAT(p
, items
[1]);
5268 else if (STRCMP(items
[0], "MIDWORD") == 0 && itemcnt
== 2
5271 midword
= getroom_save(spin
, items
[1]);
5273 else if (STRCMP(items
[0], "TRY") == 0 && itemcnt
== 2)
5275 /* ignored, we look in the tree for what chars may appear */
5277 /* TODO: remove "RAR" later */
5278 else if ((STRCMP(items
[0], "RAR") == 0
5279 || STRCMP(items
[0], "RARE") == 0) && itemcnt
== 2
5280 && aff
->af_rare
== 0)
5282 aff
->af_rare
= affitem2flag(aff
->af_flagtype
, items
[1],
5285 /* TODO: remove "KEP" later */
5286 else if ((STRCMP(items
[0], "KEP") == 0
5287 || STRCMP(items
[0], "KEEPCASE") == 0) && itemcnt
== 2
5288 && aff
->af_keepcase
== 0)
5290 aff
->af_keepcase
= affitem2flag(aff
->af_flagtype
, items
[1],
5293 else if (STRCMP(items
[0], "BAD") == 0 && itemcnt
== 2
5294 && aff
->af_bad
== 0)
5296 aff
->af_bad
= affitem2flag(aff
->af_flagtype
, items
[1],
5299 else if (STRCMP(items
[0], "NEEDAFFIX") == 0 && itemcnt
== 2
5300 && aff
->af_needaffix
== 0)
5302 aff
->af_needaffix
= affitem2flag(aff
->af_flagtype
, items
[1],
5305 else if (STRCMP(items
[0], "CIRCUMFIX") == 0 && itemcnt
== 2
5306 && aff
->af_circumfix
== 0)
5308 aff
->af_circumfix
= affitem2flag(aff
->af_flagtype
, items
[1],
5311 else if (STRCMP(items
[0], "NOSUGGEST") == 0 && itemcnt
== 2
5312 && aff
->af_nosuggest
== 0)
5314 aff
->af_nosuggest
= affitem2flag(aff
->af_flagtype
, items
[1],
5317 else if (STRCMP(items
[0], "NEEDCOMPOUND") == 0 && itemcnt
== 2
5318 && aff
->af_needcomp
== 0)
5320 aff
->af_needcomp
= affitem2flag(aff
->af_flagtype
, items
[1],
5323 else if (STRCMP(items
[0], "COMPOUNDROOT") == 0 && itemcnt
== 2
5324 && aff
->af_comproot
== 0)
5326 aff
->af_comproot
= affitem2flag(aff
->af_flagtype
, items
[1],
5329 else if (STRCMP(items
[0], "COMPOUNDFORBIDFLAG") == 0
5330 && itemcnt
== 2 && aff
->af_compforbid
== 0)
5332 aff
->af_compforbid
= affitem2flag(aff
->af_flagtype
, items
[1],
5334 if (aff
->af_pref
.ht_used
> 0)
5335 smsg((char_u
*)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5338 else if (STRCMP(items
[0], "COMPOUNDPERMITFLAG") == 0
5339 && itemcnt
== 2 && aff
->af_comppermit
== 0)
5341 aff
->af_comppermit
= affitem2flag(aff
->af_flagtype
, items
[1],
5343 if (aff
->af_pref
.ht_used
> 0)
5344 smsg((char_u
*)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5347 else if (STRCMP(items
[0], "COMPOUNDFLAG") == 0 && itemcnt
== 2
5348 && compflags
== NULL
)
5350 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
5351 * "Na" into "Na+", "1234" into "1234+". */
5352 p
= getroom(spin
, STRLEN(items
[1]) + 2, FALSE
);
5355 STRCPY(p
, items
[1]);
5360 else if (STRCMP(items
[0], "COMPOUNDRULE") == 0 && itemcnt
== 2)
5362 /* Concatenate this string to previously defined ones, using a
5363 * slash to separate them. */
5364 l
= (int)STRLEN(items
[1]) + 1;
5365 if (compflags
!= NULL
)
5366 l
+= (int)STRLEN(compflags
) + 1;
5367 p
= getroom(spin
, l
, FALSE
);
5370 if (compflags
!= NULL
)
5372 STRCPY(p
, compflags
);
5375 STRCAT(p
, items
[1]);
5379 else if (STRCMP(items
[0], "COMPOUNDWORDMAX") == 0 && itemcnt
== 2
5382 compmax
= atoi((char *)items
[1]);
5384 smsg((char_u
*)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
5385 fname
, lnum
, items
[1]);
5387 else if (STRCMP(items
[0], "COMPOUNDMIN") == 0 && itemcnt
== 2
5390 compminlen
= atoi((char *)items
[1]);
5391 if (compminlen
== 0)
5392 smsg((char_u
*)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5393 fname
, lnum
, items
[1]);
5395 else if (STRCMP(items
[0], "COMPOUNDSYLMAX") == 0 && itemcnt
== 2
5398 compsylmax
= atoi((char *)items
[1]);
5399 if (compsylmax
== 0)
5400 smsg((char_u
*)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5401 fname
, lnum
, items
[1]);
5403 else if (STRCMP(items
[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt
== 1)
5405 compoptions
|= COMP_CHECKDUP
;
5407 else if (STRCMP(items
[0], "CHECKCOMPOUNDREP") == 0 && itemcnt
== 1)
5409 compoptions
|= COMP_CHECKREP
;
5411 else if (STRCMP(items
[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt
== 1)
5413 compoptions
|= COMP_CHECKCASE
;
5415 else if (STRCMP(items
[0], "CHECKCOMPOUNDTRIPLE") == 0
5418 compoptions
|= COMP_CHECKTRIPLE
;
5420 else if (STRCMP(items
[0], "CHECKCOMPOUNDPATTERN") == 0
5423 if (atoi((char *)items
[1]) == 0)
5424 smsg((char_u
*)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5425 fname
, lnum
, items
[1]);
5427 else if (STRCMP(items
[0], "CHECKCOMPOUNDPATTERN") == 0
5430 garray_T
*gap
= &spin
->si_comppat
;
5433 /* Only add the couple if it isn't already there. */
5434 for (i
= 0; i
< gap
->ga_len
- 1; i
+= 2)
5435 if (STRCMP(((char_u
**)(gap
->ga_data
))[i
], items
[1]) == 0
5436 && STRCMP(((char_u
**)(gap
->ga_data
))[i
+ 1],
5439 if (i
>= gap
->ga_len
&& ga_grow(gap
, 2) == OK
)
5441 ((char_u
**)(gap
->ga_data
))[gap
->ga_len
++]
5442 = getroom_save(spin
, items
[1]);
5443 ((char_u
**)(gap
->ga_data
))[gap
->ga_len
++]
5444 = getroom_save(spin
, items
[2]);
5447 else if (STRCMP(items
[0], "SYLLABLE") == 0 && itemcnt
== 2
5448 && syllable
== NULL
)
5450 syllable
= getroom_save(spin
, items
[1]);
5452 else if (STRCMP(items
[0], "NOBREAK") == 0 && itemcnt
== 1)
5454 spin
->si_nobreak
= TRUE
;
5456 else if (STRCMP(items
[0], "NOSPLITSUGS") == 0 && itemcnt
== 1)
5458 spin
->si_nosplitsugs
= TRUE
;
5460 else if (STRCMP(items
[0], "NOSUGFILE") == 0 && itemcnt
== 1)
5462 spin
->si_nosugfile
= TRUE
;
5464 else if (STRCMP(items
[0], "PFXPOSTPONE") == 0 && itemcnt
== 1)
5466 aff
->af_pfxpostpone
= TRUE
;
5468 else if ((STRCMP(items
[0], "PFX") == 0
5469 || STRCMP(items
[0], "SFX") == 0)
5474 char_u key
[AH_KEY_LEN
];
5476 if (*items
[0] == 'P')
5481 /* Myspell allows the same affix name to be used multiple
5482 * times. The affix files that do this have an undocumented
5483 * "S" flag on all but the last block, thus we check for that
5484 * and store it in ah_follows. */
5485 vim_strncpy(key
, items
[1], AH_KEY_LEN
- 1);
5486 hi
= hash_find(tp
, key
);
5487 if (!HASHITEM_EMPTY(hi
))
5489 cur_aff
= HI2AH(hi
);
5490 if (cur_aff
->ah_combine
!= (*items
[2] == 'Y'))
5491 smsg((char_u
*)_("Different combining flag in continued affix block in %s line %d: %s"),
5492 fname
, lnum
, items
[1]);
5493 if (!cur_aff
->ah_follows
)
5494 smsg((char_u
*)_("Duplicate affix in %s line %d: %s"),
5495 fname
, lnum
, items
[1]);
5499 /* New affix letter. */
5500 cur_aff
= (affheader_T
*)getroom(spin
,
5501 sizeof(affheader_T
), TRUE
);
5502 if (cur_aff
== NULL
)
5504 cur_aff
->ah_flag
= affitem2flag(aff
->af_flagtype
, items
[1],
5506 if (cur_aff
->ah_flag
== 0 || STRLEN(items
[1]) >= AH_KEY_LEN
)
5508 if (cur_aff
->ah_flag
== aff
->af_bad
5509 || cur_aff
->ah_flag
== aff
->af_rare
5510 || cur_aff
->ah_flag
== aff
->af_keepcase
5511 || cur_aff
->ah_flag
== aff
->af_needaffix
5512 || cur_aff
->ah_flag
== aff
->af_circumfix
5513 || cur_aff
->ah_flag
== aff
->af_nosuggest
5514 || cur_aff
->ah_flag
== aff
->af_needcomp
5515 || cur_aff
->ah_flag
== aff
->af_comproot
)
5516 smsg((char_u
*)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
5517 fname
, lnum
, items
[1]);
5518 STRCPY(cur_aff
->ah_key
, items
[1]);
5519 hash_add(tp
, cur_aff
->ah_key
);
5521 cur_aff
->ah_combine
= (*items
[2] == 'Y');
5524 /* Check for the "S" flag, which apparently means that another
5525 * block with the same affix name is following. */
5526 if (itemcnt
> lasti
&& STRCMP(items
[lasti
], "S") == 0)
5529 cur_aff
->ah_follows
= TRUE
;
5532 cur_aff
->ah_follows
= FALSE
;
5534 /* Myspell allows extra text after the item, but that might
5535 * mean mistakes go unnoticed. Require a comment-starter. */
5536 if (itemcnt
> lasti
&& *items
[lasti
] != '#')
5537 smsg((char_u
*)_(e_afftrailing
), fname
, lnum
, items
[lasti
]);
5539 if (STRCMP(items
[2], "Y") != 0 && STRCMP(items
[2], "N") != 0)
5540 smsg((char_u
*)_("Expected Y or N in %s line %d: %s"),
5541 fname
, lnum
, items
[2]);
5543 if (*items
[0] == 'P' && aff
->af_pfxpostpone
)
5545 if (cur_aff
->ah_newID
== 0)
5547 /* Use a new number in the .spl file later, to be able
5548 * to handle multiple .aff files. */
5549 check_renumber(spin
);
5550 cur_aff
->ah_newID
= ++spin
->si_newprefID
;
5552 /* We only really use ah_newID if the prefix is
5553 * postponed. We know that only after handling all
5555 did_postpone_prefix
= FALSE
;
5558 /* Did use the ID in a previous block. */
5559 did_postpone_prefix
= TRUE
;
5562 aff_todo
= atoi((char *)items
[3]);
5564 else if ((STRCMP(items
[0], "PFX") == 0
5565 || STRCMP(items
[0], "SFX") == 0)
5567 && STRCMP(cur_aff
->ah_key
, items
[1]) == 0
5570 affentry_T
*aff_entry
;
5574 /* Myspell allows extra text after the item, but that might
5575 * mean mistakes go unnoticed. Require a comment-starter.
5576 * Hunspell uses a "-" item. */
5577 if (itemcnt
> lasti
&& *items
[lasti
] != '#'
5578 && (STRCMP(items
[lasti
], "-") != 0
5579 || itemcnt
!= lasti
+ 1))
5580 smsg((char_u
*)_(e_afftrailing
), fname
, lnum
, items
[lasti
]);
5582 /* New item for an affix letter. */
5584 aff_entry
= (affentry_T
*)getroom(spin
,
5585 sizeof(affentry_T
), TRUE
);
5586 if (aff_entry
== NULL
)
5589 if (STRCMP(items
[2], "0") != 0)
5590 aff_entry
->ae_chop
= getroom_save(spin
, items
[2]);
5591 if (STRCMP(items
[3], "0") != 0)
5593 aff_entry
->ae_add
= getroom_save(spin
, items
[3]);
5595 /* Recognize flags on the affix: abcd/XYZ */
5596 aff_entry
->ae_flags
= vim_strchr(aff_entry
->ae_add
, '/');
5597 if (aff_entry
->ae_flags
!= NULL
)
5599 *aff_entry
->ae_flags
++ = NUL
;
5600 aff_process_flags(aff
, aff_entry
);
5604 /* Don't use an affix entry with non-ASCII characters when
5605 * "spin->si_ascii" is TRUE. */
5606 if (!spin
->si_ascii
|| !(has_non_ascii(aff_entry
->ae_chop
)
5607 || has_non_ascii(aff_entry
->ae_add
)))
5609 aff_entry
->ae_next
= cur_aff
->ah_first
;
5610 cur_aff
->ah_first
= aff_entry
;
5612 if (STRCMP(items
[4], ".") != 0)
5614 char_u buf
[MAXLINELEN
];
5616 aff_entry
->ae_cond
= getroom_save(spin
, items
[4]);
5617 if (*items
[0] == 'P')
5618 sprintf((char *)buf
, "^%s", items
[4]);
5620 sprintf((char *)buf
, "%s$", items
[4]);
5621 aff_entry
->ae_prog
= vim_regcomp(buf
,
5622 RE_MAGIC
+ RE_STRING
+ RE_STRICT
);
5623 if (aff_entry
->ae_prog
== NULL
)
5624 smsg((char_u
*)_("Broken condition in %s line %d: %s"),
5625 fname
, lnum
, items
[4]);
5628 /* For postponed prefixes we need an entry in si_prefcond
5629 * for the condition. Use an existing one if possible.
5630 * Can't be done for an affix with flags, ignoring
5631 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
5632 if (*items
[0] == 'P' && aff
->af_pfxpostpone
5633 && aff_entry
->ae_flags
== NULL
)
5635 /* When the chop string is one lower-case letter and
5636 * the add string ends in the upper-case letter we set
5637 * the "upper" flag, clear "ae_chop" and remove the
5638 * letters from "ae_add". The condition must either
5639 * be empty or start with the same letter. */
5640 if (aff_entry
->ae_chop
!= NULL
5641 && aff_entry
->ae_add
!= NULL
5643 && aff_entry
->ae_chop
[(*mb_ptr2len
)(
5644 aff_entry
->ae_chop
)] == NUL
5646 && aff_entry
->ae_chop
[1] == NUL
5652 c
= PTR2CHAR(aff_entry
->ae_chop
);
5653 c_up
= SPELL_TOUPPER(c
);
5655 && (aff_entry
->ae_cond
== NULL
5656 || PTR2CHAR(aff_entry
->ae_cond
) == c
))
5658 p
= aff_entry
->ae_add
5659 + STRLEN(aff_entry
->ae_add
);
5660 mb_ptr_back(aff_entry
->ae_add
, p
);
5661 if (PTR2CHAR(p
) == c_up
)
5664 aff_entry
->ae_chop
= NULL
;
5667 /* The condition is matched with the
5668 * actual word, thus must check for the
5669 * upper-case letter. */
5670 if (aff_entry
->ae_cond
!= NULL
)
5672 char_u buf
[MAXLINELEN
];
5676 onecap_copy(items
[4], buf
, TRUE
);
5677 aff_entry
->ae_cond
= getroom_save(
5682 *aff_entry
->ae_cond
= c_up
;
5683 if (aff_entry
->ae_cond
!= NULL
)
5685 sprintf((char *)buf
, "^%s",
5686 aff_entry
->ae_cond
);
5687 vim_free(aff_entry
->ae_prog
);
5688 aff_entry
->ae_prog
= vim_regcomp(
5689 buf
, RE_MAGIC
+ RE_STRING
);
5696 if (aff_entry
->ae_chop
== NULL
5697 && aff_entry
->ae_flags
== NULL
)
5703 /* Find a previously used condition. */
5704 for (idx
= spin
->si_prefcond
.ga_len
- 1; idx
>= 0;
5707 p
= ((char_u
**)spin
->si_prefcond
.ga_data
)[idx
];
5708 if (str_equal(p
, aff_entry
->ae_cond
))
5711 if (idx
< 0 && ga_grow(&spin
->si_prefcond
, 1) == OK
)
5713 /* Not found, add a new condition. */
5714 idx
= spin
->si_prefcond
.ga_len
++;
5715 pp
= ((char_u
**)spin
->si_prefcond
.ga_data
)
5717 if (aff_entry
->ae_cond
== NULL
)
5720 *pp
= getroom_save(spin
,
5721 aff_entry
->ae_cond
);
5724 /* Add the prefix to the prefix tree. */
5725 if (aff_entry
->ae_add
== NULL
)
5728 p
= aff_entry
->ae_add
;
5730 /* PFX_FLAGS is a negative number, so that
5731 * tree_add_word() knows this is the prefix tree. */
5733 if (!cur_aff
->ah_combine
)
5737 if (aff_entry
->ae_comppermit
)
5738 n
|= WFP_COMPPERMIT
;
5739 if (aff_entry
->ae_compforbid
)
5740 n
|= WFP_COMPFORBID
;
5741 tree_add_word(spin
, p
, spin
->si_prefroot
, n
,
5742 idx
, cur_aff
->ah_newID
);
5743 did_postpone_prefix
= TRUE
;
5746 /* Didn't actually use ah_newID, backup si_newprefID. */
5747 if (aff_todo
== 0 && !did_postpone_prefix
)
5749 --spin
->si_newprefID
;
5750 cur_aff
->ah_newID
= 0;
5755 else if (STRCMP(items
[0], "FOL") == 0 && itemcnt
== 2
5758 fol
= vim_strsave(items
[1]);
5760 else if (STRCMP(items
[0], "LOW") == 0 && itemcnt
== 2
5763 low
= vim_strsave(items
[1]);
5765 else if (STRCMP(items
[0], "UPP") == 0 && itemcnt
== 2
5768 upp
= vim_strsave(items
[1]);
5770 else if ((STRCMP(items
[0], "REP") == 0
5771 || STRCMP(items
[0], "REPSAL") == 0)
5774 /* Ignore REP/REPSAL count */;
5775 if (!isdigit(*items
[1]))
5776 smsg((char_u
*)_("Expected REP(SAL) count in %s line %d"),
5779 else if ((STRCMP(items
[0], "REP") == 0
5780 || STRCMP(items
[0], "REPSAL") == 0)
5783 /* REP/REPSAL item */
5784 /* Myspell ignores extra arguments, we require it starts with
5785 * # to detect mistakes. */
5786 if (itemcnt
> 3 && items
[3][0] != '#')
5787 smsg((char_u
*)_(e_afftrailing
), fname
, lnum
, items
[3]);
5788 if (items
[0][3] == 'S' ? do_repsal
: do_rep
)
5790 /* Replace underscore with space (can't include a space
5792 for (p
= items
[1]; *p
!= NUL
; mb_ptr_adv(p
))
5795 for (p
= items
[2]; *p
!= NUL
; mb_ptr_adv(p
))
5798 add_fromto(spin
, items
[0][3] == 'S'
5800 : &spin
->si_rep
, items
[1], items
[2]);
5803 else if (STRCMP(items
[0], "MAP") == 0 && itemcnt
== 2)
5805 /* MAP item or count */
5808 /* First line contains the count. */
5810 if (!isdigit(*items
[1]))
5811 smsg((char_u
*)_("Expected MAP count in %s line %d"),
5814 else if (do_mapline
)
5818 /* Check that every character appears only once. */
5819 for (p
= items
[1]; *p
!= NUL
; )
5822 c
= mb_ptr2char_adv(&p
);
5826 if ((spin
->si_map
.ga_len
> 0
5827 && vim_strchr(spin
->si_map
.ga_data
, c
)
5829 || vim_strchr(p
, c
) != NULL
)
5830 smsg((char_u
*)_("Duplicate character in MAP in %s line %d"),
5834 /* We simply concatenate all the MAP strings, separated by
5836 ga_concat(&spin
->si_map
, items
[1]);
5837 ga_append(&spin
->si_map
, '/');
5840 /* Accept "SAL from to" and "SAL from to # comment". */
5841 else if (STRCMP(items
[0], "SAL") == 0
5842 && (itemcnt
== 3 || (itemcnt
> 3 && items
[3][0] == '#')))
5846 /* SAL item (sounds-a-like)
5847 * Either one of the known keys or a from-to pair. */
5848 if (STRCMP(items
[1], "followup") == 0)
5849 spin
->si_followup
= sal_to_bool(items
[2]);
5850 else if (STRCMP(items
[1], "collapse_result") == 0)
5851 spin
->si_collapse
= sal_to_bool(items
[2]);
5852 else if (STRCMP(items
[1], "remove_accents") == 0)
5853 spin
->si_rem_accents
= sal_to_bool(items
[2]);
5855 /* when "to" is "_" it means empty */
5856 add_fromto(spin
, &spin
->si_sal
, items
[1],
5857 STRCMP(items
[2], "_") == 0 ? (char_u
*)""
5861 else if (STRCMP(items
[0], "SOFOFROM") == 0 && itemcnt
== 2
5862 && sofofrom
== NULL
)
5864 sofofrom
= getroom_save(spin
, items
[1]);
5866 else if (STRCMP(items
[0], "SOFOTO") == 0 && itemcnt
== 2
5869 sofoto
= getroom_save(spin
, items
[1]);
5871 else if (STRCMP(items
[0], "COMMON") == 0)
5875 for (i
= 1; i
< itemcnt
; ++i
)
5877 if (HASHITEM_EMPTY(hash_find(&spin
->si_commonwords
,
5880 p
= vim_strsave(items
[i
]);
5883 hash_add(&spin
->si_commonwords
, p
);
5888 smsg((char_u
*)_("Unrecognized or duplicate item in %s line %d: %s"),
5889 fname
, lnum
, items
[0]);
5893 if (fol
!= NULL
|| low
!= NULL
|| upp
!= NULL
)
5895 if (spin
->si_clear_chartab
)
5897 /* Clear the char type tables, don't want to use any of the
5898 * currently used spell properties. */
5899 init_spell_chartab();
5900 spin
->si_clear_chartab
= FALSE
;
5904 * Don't write a word table for an ASCII file, so that we don't check
5905 * for conflicts with a word table that matches 'encoding'.
5906 * Don't write one for utf-8 either, we use utf_*() and
5907 * mb_get_class(), the list of chars in the file will be incomplete.
5915 if (fol
== NULL
|| low
== NULL
|| upp
== NULL
)
5916 smsg((char_u
*)_("Missing FOL/LOW/UPP line in %s"), fname
);
5918 (void)set_spell_chartab(fol
, low
, upp
);
5926 /* Use compound specifications of the .aff file for the spell info. */
5929 aff_check_number(spin
->si_compmax
, compmax
, "COMPOUNDWORDMAX");
5930 spin
->si_compmax
= compmax
;
5933 if (compminlen
!= 0)
5935 aff_check_number(spin
->si_compminlen
, compminlen
, "COMPOUNDMIN");
5936 spin
->si_compminlen
= compminlen
;
5939 if (compsylmax
!= 0)
5941 if (syllable
== NULL
)
5942 smsg((char_u
*)_("COMPOUNDSYLMAX used without SYLLABLE"));
5943 aff_check_number(spin
->si_compsylmax
, compsylmax
, "COMPOUNDSYLMAX");
5944 spin
->si_compsylmax
= compsylmax
;
5947 if (compoptions
!= 0)
5949 aff_check_number(spin
->si_compoptions
, compoptions
, "COMPOUND options");
5950 spin
->si_compoptions
|= compoptions
;
5953 if (compflags
!= NULL
)
5954 process_compflags(spin
, aff
, compflags
);
5956 /* Check that we didn't use too many renumbered flags. */
5957 if (spin
->si_newcompID
< spin
->si_newprefID
)
5959 if (spin
->si_newcompID
== 127 || spin
->si_newcompID
== 255)
5960 MSG(_("Too many postponed prefixes"));
5961 else if (spin
->si_newprefID
== 0 || spin
->si_newprefID
== 127)
5962 MSG(_("Too many compound flags"));
5964 MSG(_("Too many posponed prefixes and/or compound flags"));
5967 if (syllable
!= NULL
)
5969 aff_check_string(spin
->si_syllable
, syllable
, "SYLLABLE");
5970 spin
->si_syllable
= syllable
;
5973 if (sofofrom
!= NULL
|| sofoto
!= NULL
)
5975 if (sofofrom
== NULL
|| sofoto
== NULL
)
5976 smsg((char_u
*)_("Missing SOFO%s line in %s"),
5977 sofofrom
== NULL
? "FROM" : "TO", fname
);
5978 else if (spin
->si_sal
.ga_len
> 0)
5979 smsg((char_u
*)_("Both SAL and SOFO lines in %s"), fname
);
5982 aff_check_string(spin
->si_sofofr
, sofofrom
, "SOFOFROM");
5983 aff_check_string(spin
->si_sofoto
, sofoto
, "SOFOTO");
5984 spin
->si_sofofr
= sofofrom
;
5985 spin
->si_sofoto
= sofoto
;
5989 if (midword
!= NULL
)
5991 aff_check_string(spin
->si_midword
, midword
, "MIDWORD");
5992 spin
->si_midword
= midword
;
6001 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6002 * ae_flags to ae_comppermit and ae_compforbid.
6005 aff_process_flags(affile
, entry
)
6013 if (entry
->ae_flags
!= NULL
6014 && (affile
->af_compforbid
!= 0 || affile
->af_comppermit
!= 0))
6016 for (p
= entry
->ae_flags
; *p
!= NUL
; )
6019 flag
= get_affitem(affile
->af_flagtype
, &p
);
6020 if (flag
== affile
->af_comppermit
|| flag
== affile
->af_compforbid
)
6022 mch_memmove(prevp
, p
, STRLEN(p
) + 1);
6024 if (flag
== affile
->af_comppermit
)
6025 entry
->ae_comppermit
= TRUE
;
6027 entry
->ae_compforbid
= TRUE
;
6029 if (affile
->af_flagtype
== AFT_NUM
&& *p
== ',')
6032 if (*entry
->ae_flags
== NUL
)
6033 entry
->ae_flags
= NULL
; /* nothing left */
6038 * Return TRUE if "s" is the name of an info item in the affix file.
6044 return STRCMP(s
, "NAME") == 0
6045 || STRCMP(s
, "HOME") == 0
6046 || STRCMP(s
, "VERSION") == 0
6047 || STRCMP(s
, "AUTHOR") == 0
6048 || STRCMP(s
, "EMAIL") == 0
6049 || STRCMP(s
, "COPYRIGHT") == 0;
6053 * Turn an affix flag name into a number, according to the FLAG type.
6054 * returns zero for failure.
6057 affitem2flag(flagtype
, item
, fname
, lnum
)
6066 res
= get_affitem(flagtype
, &p
);
6069 if (flagtype
== AFT_NUM
)
6070 smsg((char_u
*)_("Flag is not a number in %s line %d: %s"),
6073 smsg((char_u
*)_("Illegal flag in %s line %d: %s"),
6078 smsg((char_u
*)_(e_affname
), fname
, lnum
, item
);
6086 * Get one affix name from "*pp" and advance the pointer.
6087 * Returns zero for an error, still advances the pointer then.
6090 get_affitem(flagtype
, pp
)
6096 if (flagtype
== AFT_NUM
)
6098 if (!VIM_ISDIGIT(**pp
))
6100 ++*pp
; /* always advance, avoid getting stuck */
6103 res
= getdigits(pp
);
6108 res
= mb_ptr2char_adv(pp
);
6112 if (flagtype
== AFT_LONG
|| (flagtype
== AFT_CAPLONG
6113 && res
>= 'A' && res
<= 'Z'))
6118 res
= mb_ptr2char_adv(pp
) + (res
<< 16);
6120 res
= *(*pp
)++ + (res
<< 16);
6128 * Process the "compflags" string used in an affix file and append it to
6129 * spin->si_compflags.
6130 * The processing involves changing the affix names to ID numbers, so that
6131 * they fit in one byte.
6134 process_compflags(spin
, aff
, compflags
)
6146 char_u key
[AH_KEY_LEN
];
6149 /* Make room for the old and the new compflags, concatenated with a / in
6150 * between. Processing it makes it shorter, but we don't know by how
6151 * much, thus allocate the maximum. */
6152 len
= (int)STRLEN(compflags
) + 1;
6153 if (spin
->si_compflags
!= NULL
)
6154 len
+= (int)STRLEN(spin
->si_compflags
) + 1;
6155 p
= getroom(spin
, len
, FALSE
);
6158 if (spin
->si_compflags
!= NULL
)
6160 STRCPY(p
, spin
->si_compflags
);
6163 spin
->si_compflags
= p
;
6166 for (p
= compflags
; *p
!= NUL
; )
6168 if (vim_strchr((char_u
*)"/*+[]", *p
) != NULL
)
6169 /* Copy non-flag characters directly. */
6173 /* First get the flag number, also checks validity. */
6175 flag
= get_affitem(aff
->af_flagtype
, &p
);
6178 /* Find the flag in the hashtable. If it was used before, use
6179 * the existing ID. Otherwise add a new entry. */
6180 vim_strncpy(key
, prevp
, p
- prevp
);
6181 hi
= hash_find(&aff
->af_comp
, key
);
6182 if (!HASHITEM_EMPTY(hi
))
6183 id
= HI2CI(hi
)->ci_newID
;
6186 ci
= (compitem_T
*)getroom(spin
, sizeof(compitem_T
), TRUE
);
6189 STRCPY(ci
->ci_key
, key
);
6191 /* Avoid using a flag ID that has a special meaning in a
6192 * regexp (also inside []). */
6195 check_renumber(spin
);
6196 id
= spin
->si_newcompID
--;
6197 } while (vim_strchr((char_u
*)"/+*[]\\-^", id
) != NULL
);
6199 hash_add(&aff
->af_comp
, ci
->ci_key
);
6203 if (aff
->af_flagtype
== AFT_NUM
&& *p
== ',')
6212 * Check that the new IDs for postponed affixes and compounding don't overrun
6213 * each other. We have almost 255 available, but start at 0-127 to avoid
6214 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6215 * When that is used up an error message is given.
6218 check_renumber(spin
)
6221 if (spin
->si_newprefID
== spin
->si_newcompID
&& spin
->si_newcompID
< 128)
6223 spin
->si_newprefID
= 127;
6224 spin
->si_newcompID
= 255;
6229 * Return TRUE if flag "flag" appears in affix list "afflist".
6232 flag_in_afflist(flagtype
, afflist
, flag
)
6243 return vim_strchr(afflist
, flag
) != NULL
;
6247 for (p
= afflist
; *p
!= NUL
; )
6250 n
= mb_ptr2char_adv(&p
);
6254 if ((flagtype
== AFT_LONG
|| (n
>= 'A' && n
<= 'Z'))
6257 n
= mb_ptr2char_adv(&p
) + (n
<< 16);
6259 n
= *p
++ + (n
<< 16);
6267 for (p
= afflist
; *p
!= NUL
; )
6272 if (*p
!= NUL
) /* skip over comma */
6281 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6284 aff_check_number(spinval
, affval
, name
)
6289 if (spinval
!= 0 && spinval
!= affval
)
6290 smsg((char_u
*)_("%s value differs from what is used in another .aff file"), name
);
6294 * Give a warning when "spinval" and "affval" strings are set and not the same.
6297 aff_check_string(spinval
, affval
, name
)
6302 if (spinval
!= NULL
&& STRCMP(spinval
, affval
) != 0)
6303 smsg((char_u
*)_("%s value differs from what is used in another .aff file"), name
);
6307 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6315 if (s1
== NULL
|| s2
== NULL
)
6317 return STRCMP(s1
, s2
) == 0;
6321 * Add a from-to item to "gap". Used for REP and SAL items.
6322 * They are stored case-folded.
6325 add_fromto(spin
, gap
, from
, to
)
6332 char_u word
[MAXWLEN
];
6334 if (ga_grow(gap
, 1) == OK
)
6336 ftp
= ((fromto_T
*)gap
->ga_data
) + gap
->ga_len
;
6337 (void)spell_casefold(from
, (int)STRLEN(from
), word
, MAXWLEN
);
6338 ftp
->ft_from
= getroom_save(spin
, word
);
6339 (void)spell_casefold(to
, (int)STRLEN(to
), word
, MAXWLEN
);
6340 ftp
->ft_to
= getroom_save(spin
, word
);
6346 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6352 return STRCMP(s
, "1") == 0 || STRCMP(s
, "true") == 0;
6356 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6357 * When "s" is NULL FALSE is returned.
6366 for (p
= s
; *p
!= NUL
; ++p
)
6373 * Free the structure filled by spell_read_aff().
6385 vim_free(aff
->af_enc
);
6387 /* All this trouble to free the "ae_prog" items... */
6388 for (ht
= &aff
->af_pref
; ; ht
= &aff
->af_suff
)
6390 todo
= (int)ht
->ht_used
;
6391 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6393 if (!HASHITEM_EMPTY(hi
))
6397 for (ae
= ah
->ah_first
; ae
!= NULL
; ae
= ae
->ae_next
)
6398 vim_free(ae
->ae_prog
);
6401 if (ht
== &aff
->af_suff
)
6405 hash_clear(&aff
->af_pref
);
6406 hash_clear(&aff
->af_suff
);
6407 hash_clear(&aff
->af_comp
);
6411 * Read dictionary file "fname".
6412 * Returns OK or FAIL;
6415 spell_read_dic(spin
, fname
, affile
)
6421 char_u line
[MAXLINELEN
];
6424 char_u store_afflist
[MAXWLEN
];
6437 char_u message
[MAXLINELEN
+ MAXWLEN
];
6444 fd
= mch_fopen((char *)fname
, "r");
6447 EMSG2(_(e_notopen
), fname
);
6451 /* The hashtable is only used to detect duplicated words. */
6454 vim_snprintf((char *)IObuff
, IOSIZE
,
6455 _("Reading dictionary file %s ..."), fname
);
6456 spell_message(spin
, IObuff
);
6458 /* start with a message for the first line */
6459 spin
->si_msg_count
= 999999;
6461 /* Read and ignore the first line: word count. */
6462 (void)vim_fgets(line
, MAXLINELEN
, fd
);
6463 if (!vim_isdigit(*skipwhite(line
)))
6464 EMSG2(_("E760: No word count in %s"), fname
);
6467 * Read all the lines in the file one by one.
6468 * The words are converted to 'encoding' here, before being added to
6471 while (!vim_fgets(line
, MAXLINELEN
, fd
) && !got_int
)
6475 if (line
[0] == '#' || line
[0] == '/')
6476 continue; /* comment line */
6478 /* Remove CR, LF and white space from the end. White space halfway
6479 * the word is kept to allow e.g., "et al.". */
6480 l
= (int)STRLEN(line
);
6481 while (l
> 0 && line
[l
- 1] <= ' ')
6484 continue; /* empty line */
6488 /* Convert from "SET" to 'encoding' when needed. */
6489 if (spin
->si_conv
.vc_type
!= CONV_NONE
)
6491 pc
= string_convert(&spin
->si_conv
, line
, NULL
);
6494 smsg((char_u
*)_("Conversion failure for word in %s line %d: %s"),
6507 /* Truncate the word at the "/", set "afflist" to what follows.
6508 * Replace "\/" by "/" and "\\" by "\". */
6510 for (p
= w
; *p
!= NUL
; mb_ptr_adv(p
))
6512 if (*p
== '\\' && (p
[1] == '\\' || p
[1] == '/'))
6513 mch_memmove(p
, p
+ 1, STRLEN(p
));
6522 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6523 if (spin
->si_ascii
&& has_non_ascii(w
))
6530 /* This takes time, print a message every 10000 words. */
6531 if (spin
->si_verbose
&& spin
->si_msg_count
> 10000)
6533 spin
->si_msg_count
= 0;
6534 vim_snprintf((char *)message
, sizeof(message
),
6535 _("line %6d, word %6d - %s"),
6536 lnum
, spin
->si_foldwcount
+ spin
->si_keepwcount
, w
);
6538 msg_puts_long_attr(message
, 0);
6545 /* Store the word in the hashtable to be able to find duplicates. */
6546 dw
= (char_u
*)getroom_save(spin
, w
);
6554 hash
= hash_hash(dw
);
6555 hi
= hash_lookup(&ht
, dw
, hash
);
6556 if (!HASHITEM_EMPTY(hi
))
6559 smsg((char_u
*)_("Duplicate word in %s line %d: %s"),
6561 else if (duplicate
== 0)
6562 smsg((char_u
*)_("First duplicate word in %s line %d: %s"),
6567 hash_add_item(&ht
, hi
, dw
, hash
);
6570 store_afflist
[0] = NUL
;
6573 if (afflist
!= NULL
)
6575 /* Extract flags from the affix list. */
6576 flags
|= get_affix_flags(affile
, afflist
);
6578 if (affile
->af_needaffix
!= 0 && flag_in_afflist(
6579 affile
->af_flagtype
, afflist
, affile
->af_needaffix
))
6582 if (affile
->af_pfxpostpone
)
6583 /* Need to store the list of prefix IDs with the word. */
6584 pfxlen
= get_pfxlist(affile
, afflist
, store_afflist
);
6586 if (spin
->si_compflags
!= NULL
)
6587 /* Need to store the list of compound flags with the word.
6588 * Concatenate them to the list of prefix IDs. */
6589 get_compflags(affile
, afflist
, store_afflist
+ pfxlen
);
6592 /* Add the word to the word tree(s). */
6593 if (store_word(spin
, dw
, flags
, spin
->si_region
,
6594 store_afflist
, need_affix
) == FAIL
)
6597 if (afflist
!= NULL
)
6599 /* Find all matching suffixes and add the resulting words.
6600 * Additionally do matching prefixes that combine. */
6601 if (store_aff_word(spin
, dw
, afflist
, affile
,
6602 &affile
->af_suff
, &affile
->af_pref
,
6603 CONDIT_SUF
, flags
, store_afflist
, pfxlen
) == FAIL
)
6606 /* Find all matching prefixes and add the resulting words. */
6607 if (store_aff_word(spin
, dw
, afflist
, affile
,
6608 &affile
->af_pref
, NULL
,
6609 CONDIT_SUF
, flags
, store_afflist
, pfxlen
) == FAIL
)
6617 smsg((char_u
*)_("%d duplicate word(s) in %s"), duplicate
, fname
);
6618 if (spin
->si_ascii
&& non_ascii
> 0)
6619 smsg((char_u
*)_("Ignored %d word(s) with non-ASCII characters in %s"),
6628 * Check for affix flags in "afflist" that are turned into word flags.
6632 get_affix_flags(affile
, afflist
)
6638 if (affile
->af_keepcase
!= 0 && flag_in_afflist(
6639 affile
->af_flagtype
, afflist
, affile
->af_keepcase
))
6640 flags
|= WF_KEEPCAP
| WF_FIXCAP
;
6641 if (affile
->af_rare
!= 0 && flag_in_afflist(
6642 affile
->af_flagtype
, afflist
, affile
->af_rare
))
6644 if (affile
->af_bad
!= 0 && flag_in_afflist(
6645 affile
->af_flagtype
, afflist
, affile
->af_bad
))
6647 if (affile
->af_needcomp
!= 0 && flag_in_afflist(
6648 affile
->af_flagtype
, afflist
, affile
->af_needcomp
))
6649 flags
|= WF_NEEDCOMP
;
6650 if (affile
->af_comproot
!= 0 && flag_in_afflist(
6651 affile
->af_flagtype
, afflist
, affile
->af_comproot
))
6652 flags
|= WF_COMPROOT
;
6653 if (affile
->af_nosuggest
!= 0 && flag_in_afflist(
6654 affile
->af_flagtype
, afflist
, affile
->af_nosuggest
))
6655 flags
|= WF_NOSUGGEST
;
6660 * Get the list of prefix IDs from the affix list "afflist".
6661 * Used for PFXPOSTPONE.
6662 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6663 * and return the number of affixes.
6666 get_pfxlist(affile
, afflist
, store_afflist
)
6669 char_u
*store_afflist
;
6675 char_u key
[AH_KEY_LEN
];
6678 for (p
= afflist
; *p
!= NUL
; )
6681 if (get_affitem(affile
->af_flagtype
, &p
) != 0)
6683 /* A flag is a postponed prefix flag if it appears in "af_pref"
6684 * and it's ID is not zero. */
6685 vim_strncpy(key
, prevp
, p
- prevp
);
6686 hi
= hash_find(&affile
->af_pref
, key
);
6687 if (!HASHITEM_EMPTY(hi
))
6689 id
= HI2AH(hi
)->ah_newID
;
6691 store_afflist
[cnt
++] = id
;
6694 if (affile
->af_flagtype
== AFT_NUM
&& *p
== ',')
6698 store_afflist
[cnt
] = NUL
;
6703 * Get the list of compound IDs from the affix list "afflist" that are used
6704 * for compound words.
6705 * Puts the flags in "store_afflist[]".
6708 get_compflags(affile
, afflist
, store_afflist
)
6711 char_u
*store_afflist
;
6716 char_u key
[AH_KEY_LEN
];
6719 for (p
= afflist
; *p
!= NUL
; )
6722 if (get_affitem(affile
->af_flagtype
, &p
) != 0)
6724 /* A flag is a compound flag if it appears in "af_comp". */
6725 vim_strncpy(key
, prevp
, p
- prevp
);
6726 hi
= hash_find(&affile
->af_comp
, key
);
6727 if (!HASHITEM_EMPTY(hi
))
6728 store_afflist
[cnt
++] = HI2CI(hi
)->ci_newID
;
6730 if (affile
->af_flagtype
== AFT_NUM
&& *p
== ',')
6734 store_afflist
[cnt
] = NUL
;
6738 * Apply affixes to a word and store the resulting words.
6739 * "ht" is the hashtable with affentry_T that need to be applied, either
6740 * prefixes or suffixes.
6741 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6742 * the resulting words for combining affixes.
6744 * Returns FAIL when out of memory.
6747 store_aff_word(spin
, word
, afflist
, affile
, ht
, xht
, condit
, flags
,
6749 spellinfo_T
*spin
; /* spell info */
6750 char_u
*word
; /* basic word start */
6751 char_u
*afflist
; /* list of names of supported affixes */
6755 int condit
; /* CONDIT_SUF et al. */
6756 int flags
; /* flags for the word */
6757 char_u
*pfxlist
; /* list of prefix IDs */
6758 int pfxlen
; /* nr of flags in "pfxlist" for prefixes, rest
6759 * is compound flags */
6765 regmatch_T regmatch
;
6766 char_u newword
[MAXWLEN
];
6771 char_u
*use_pfxlist
;
6774 char_u store_afflist
[MAXWLEN
];
6775 char_u pfx_pfxlist
[MAXWLEN
];
6776 size_t wordlen
= STRLEN(word
);
6779 todo
= (int)ht
->ht_used
;
6780 for (hi
= ht
->ht_array
; todo
> 0 && retval
== OK
; ++hi
)
6782 if (!HASHITEM_EMPTY(hi
))
6787 /* Check that the affix combines, if required, and that the word
6788 * supports this affix. */
6789 if (((condit
& CONDIT_COMB
) == 0 || ah
->ah_combine
)
6790 && flag_in_afflist(affile
->af_flagtype
, afflist
,
6793 /* Loop over all affix entries with this name. */
6794 for (ae
= ah
->ah_first
; ae
!= NULL
; ae
= ae
->ae_next
)
6796 /* Check the condition. It's not logical to match case
6797 * here, but it is required for compatibility with
6799 * Another requirement from Myspell is that the chop
6800 * string is shorter than the word itself.
6801 * For prefixes, when "PFXPOSTPONE" was used, only do
6802 * prefixes with a chop string and/or flags.
6803 * When a previously added affix had CIRCUMFIX this one
6804 * must have it too, if it had not then this one must not
6805 * have one either. */
6806 regmatch
.regprog
= ae
->ae_prog
;
6807 regmatch
.rm_ic
= FALSE
;
6808 if ((xht
!= NULL
|| !affile
->af_pfxpostpone
6809 || ae
->ae_chop
!= NULL
6810 || ae
->ae_flags
!= NULL
)
6811 && (ae
->ae_chop
== NULL
6812 || STRLEN(ae
->ae_chop
) < wordlen
)
6813 && (ae
->ae_prog
== NULL
6814 || vim_regexec(®match
, word
, (colnr_T
)0))
6815 && (((condit
& CONDIT_CFIX
) == 0)
6816 == ((condit
& CONDIT_AFF
) == 0
6817 || ae
->ae_flags
== NULL
6818 || !flag_in_afflist(affile
->af_flagtype
,
6819 ae
->ae_flags
, affile
->af_circumfix
))))
6821 /* Match. Remove the chop and add the affix. */
6824 /* prefix: chop/add at the start of the word */
6825 if (ae
->ae_add
== NULL
)
6828 STRCPY(newword
, ae
->ae_add
);
6830 if (ae
->ae_chop
!= NULL
)
6832 /* Skip chop string. */
6836 i
= mb_charlen(ae
->ae_chop
);
6842 p
+= STRLEN(ae
->ae_chop
);
6848 /* suffix: chop/add at the end of the word */
6849 STRCPY(newword
, word
);
6850 if (ae
->ae_chop
!= NULL
)
6852 /* Remove chop string. */
6853 p
= newword
+ STRLEN(newword
);
6854 i
= (int)MB_CHARLEN(ae
->ae_chop
);
6856 mb_ptr_back(newword
, p
);
6859 if (ae
->ae_add
!= NULL
)
6860 STRCAT(newword
, ae
->ae_add
);
6864 use_pfxlist
= pfxlist
;
6865 use_pfxlen
= pfxlen
;
6867 use_condit
= condit
| CONDIT_COMB
| CONDIT_AFF
;
6868 if (ae
->ae_flags
!= NULL
)
6870 /* Extract flags from the affix list. */
6871 use_flags
|= get_affix_flags(affile
, ae
->ae_flags
);
6873 if (affile
->af_needaffix
!= 0 && flag_in_afflist(
6874 affile
->af_flagtype
, ae
->ae_flags
,
6875 affile
->af_needaffix
))
6878 /* When there is a CIRCUMFIX flag the other affix
6879 * must also have it and we don't add the word
6880 * with one affix. */
6881 if (affile
->af_circumfix
!= 0 && flag_in_afflist(
6882 affile
->af_flagtype
, ae
->ae_flags
,
6883 affile
->af_circumfix
))
6885 use_condit
|= CONDIT_CFIX
;
6886 if ((condit
& CONDIT_CFIX
) == 0)
6890 if (affile
->af_pfxpostpone
6891 || spin
->si_compflags
!= NULL
)
6893 if (affile
->af_pfxpostpone
)
6894 /* Get prefix IDS from the affix list. */
6895 use_pfxlen
= get_pfxlist(affile
,
6896 ae
->ae_flags
, store_afflist
);
6899 use_pfxlist
= store_afflist
;
6901 /* Combine the prefix IDs. Avoid adding the
6903 for (i
= 0; i
< pfxlen
; ++i
)
6905 for (j
= 0; j
< use_pfxlen
; ++j
)
6906 if (pfxlist
[i
] == use_pfxlist
[j
])
6908 if (j
== use_pfxlen
)
6909 use_pfxlist
[use_pfxlen
++] = pfxlist
[i
];
6912 if (spin
->si_compflags
!= NULL
)
6913 /* Get compound IDS from the affix list. */
6914 get_compflags(affile
, ae
->ae_flags
,
6915 use_pfxlist
+ use_pfxlen
);
6917 /* Combine the list of compound flags.
6918 * Concatenate them to the prefix IDs list.
6919 * Avoid adding the same ID twice. */
6920 for (i
= pfxlen
; pfxlist
[i
] != NUL
; ++i
)
6922 for (j
= use_pfxlen
;
6923 use_pfxlist
[j
] != NUL
; ++j
)
6924 if (pfxlist
[i
] == use_pfxlist
[j
])
6926 if (use_pfxlist
[j
] == NUL
)
6928 use_pfxlist
[j
++] = pfxlist
[i
];
6929 use_pfxlist
[j
] = NUL
;
6935 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
6936 * use the compound flags. */
6937 if (use_pfxlist
!= NULL
&& ae
->ae_compforbid
)
6939 vim_strncpy(pfx_pfxlist
, use_pfxlist
, use_pfxlen
);
6940 use_pfxlist
= pfx_pfxlist
;
6943 /* When there are postponed prefixes... */
6944 if (spin
->si_prefroot
!= NULL
6945 && spin
->si_prefroot
->wn_sibling
!= NULL
)
6947 /* ... add a flag to indicate an affix was used. */
6948 use_flags
|= WF_HAS_AFF
;
6950 /* ... don't use a prefix list if combining
6951 * affixes is not allowed. But do use the
6952 * compound flags after them. */
6953 if (!ah
->ah_combine
&& use_pfxlist
!= NULL
)
6954 use_pfxlist
+= use_pfxlen
;
6957 /* When compounding is supported and there is no
6958 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6959 * side where the affix is applied. */
6960 if (spin
->si_compflags
!= NULL
&& !ae
->ae_comppermit
)
6963 use_flags
|= WF_NOCOMPAFT
;
6965 use_flags
|= WF_NOCOMPBEF
;
6968 /* Store the modified word. */
6969 if (store_word(spin
, newword
, use_flags
,
6970 spin
->si_region
, use_pfxlist
,
6971 need_affix
) == FAIL
)
6974 /* When added a prefix or a first suffix and the affix
6975 * has flags may add a(nother) suffix. RECURSIVE! */
6976 if ((condit
& CONDIT_SUF
) && ae
->ae_flags
!= NULL
)
6977 if (store_aff_word(spin
, newword
, ae
->ae_flags
,
6978 affile
, &affile
->af_suff
, xht
,
6979 use_condit
& (xht
== NULL
6980 ? ~0 : ~CONDIT_SUF
),
6981 use_flags
, use_pfxlist
, pfxlen
) == FAIL
)
6984 /* When added a suffix and combining is allowed also
6985 * try adding a prefix additionally. Both for the
6986 * word flags and for the affix flags. RECURSIVE! */
6987 if (xht
!= NULL
&& ah
->ah_combine
)
6989 if (store_aff_word(spin
, newword
,
6991 xht
, NULL
, use_condit
,
6992 use_flags
, use_pfxlist
,
6994 || (ae
->ae_flags
!= NULL
6995 && store_aff_word(spin
, newword
,
6996 ae
->ae_flags
, affile
,
6997 xht
, NULL
, use_condit
,
6998 use_flags
, use_pfxlist
,
7012 * Read a file with a list of words.
7015 spell_read_wordfile(spin
, fname
)
7021 char_u rline
[MAXLINELEN
];
7027 int did_word
= FALSE
;
7035 fd
= mch_fopen((char *)fname
, "r");
7038 EMSG2(_(e_notopen
), fname
);
7042 vim_snprintf((char *)IObuff
, IOSIZE
, _("Reading word file %s ..."), fname
);
7043 spell_message(spin
, IObuff
);
7046 * Read all the lines in the file one by one.
7048 while (!vim_fgets(rline
, MAXLINELEN
, fd
) && !got_int
)
7053 /* Skip comment lines. */
7057 /* Remove CR, LF and white space from the end. */
7058 l
= (int)STRLEN(rline
);
7059 while (l
> 0 && rline
[l
- 1] <= ' ')
7062 continue; /* empty or blank line */
7065 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
7068 if (spin
->si_conv
.vc_type
!= CONV_NONE
)
7070 pc
= string_convert(&spin
->si_conv
, rline
, NULL
);
7073 smsg((char_u
*)_("Conversion failure for word in %s line %d: %s"),
7074 fname
, lnum
, rline
);
7089 if (STRNCMP(line
, "encoding=", 9) == 0)
7091 if (spin
->si_conv
.vc_type
!= CONV_NONE
)
7092 smsg((char_u
*)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7093 fname
, lnum
, line
- 1);
7095 smsg((char_u
*)_("/encoding= line after word ignored in %s line %d: %s"),
7096 fname
, lnum
, line
- 1);
7102 /* Setup for conversion to 'encoding'. */
7104 enc
= enc_canonize(line
);
7105 if (enc
!= NULL
&& !spin
->si_ascii
7106 && convert_setup(&spin
->si_conv
, enc
,
7108 smsg((char_u
*)_("Conversion in %s not supported: from %s to %s"),
7109 fname
, line
, p_enc
);
7111 spin
->si_conv
.vc_fail
= TRUE
;
7113 smsg((char_u
*)_("Conversion in %s not supported"), fname
);
7119 if (STRNCMP(line
, "regions=", 8) == 0)
7121 if (spin
->si_region_count
> 1)
7122 smsg((char_u
*)_("Duplicate /regions= line ignored in %s line %d: %s"),
7127 if (STRLEN(line
) > 16)
7128 smsg((char_u
*)_("Too many regions in %s line %d: %s"),
7132 spin
->si_region_count
= (int)STRLEN(line
) / 2;
7133 STRCPY(spin
->si_region_name
, line
);
7135 /* Adjust the mask for a word valid in all regions. */
7136 spin
->si_region
= (1 << spin
->si_region_count
) - 1;
7142 smsg((char_u
*)_("/ line ignored in %s line %d: %s"),
7143 fname
, lnum
, line
- 1);
7148 regionmask
= spin
->si_region
;
7150 /* Check for flags and region after a slash. */
7151 p
= vim_strchr(line
, '/');
7157 if (*p
== '=') /* keep-case word */
7158 flags
|= WF_KEEPCAP
| WF_FIXCAP
;
7159 else if (*p
== '!') /* Bad, bad, wicked word. */
7161 else if (*p
== '?') /* Rare word. */
7163 else if (VIM_ISDIGIT(*p
)) /* region number(s) */
7165 if ((flags
& WF_REGION
) == 0) /* first one */
7170 if (l
> spin
->si_region_count
)
7172 smsg((char_u
*)_("Invalid region nr in %s line %d: %s"),
7176 regionmask
|= 1 << (l
- 1);
7180 smsg((char_u
*)_("Unrecognized flags in %s line %d: %s"),
7188 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7189 if (spin
->si_ascii
&& has_non_ascii(line
))
7195 /* Normal word: store it. */
7196 if (store_word(spin
, line
, flags
, regionmask
, NULL
, FALSE
) == FAIL
)
7207 if (spin
->si_ascii
&& non_ascii
> 0)
7209 vim_snprintf((char *)IObuff
, IOSIZE
,
7210 _("Ignored %d words with non-ASCII characters"), non_ascii
);
7211 spell_message(spin
, IObuff
);
7218 * Get part of an sblock_T, "len" bytes long.
7219 * This avoids calling free() for every little struct we use (and keeping
7221 * The memory is cleared to all zeros.
7222 * Returns NULL when out of memory.
7225 getroom(spin
, len
, align
)
7227 size_t len
; /* length needed */
7228 int align
; /* align for pointer */
7231 sblock_T
*bl
= spin
->si_blocks
;
7233 if (align
&& bl
!= NULL
)
7234 /* Round size up for alignment. On some systems structures need to be
7235 * aligned to the size of a pointer (e.g., SPARC). */
7236 bl
->sb_used
= (bl
->sb_used
+ sizeof(char *) - 1)
7237 & ~(sizeof(char *) - 1);
7239 if (bl
== NULL
|| bl
->sb_used
+ len
> SBLOCKSIZE
)
7241 /* Allocate a block of memory. This is not freed until much later. */
7242 bl
= (sblock_T
*)alloc_clear((unsigned)(sizeof(sblock_T
) + SBLOCKSIZE
));
7245 bl
->sb_next
= spin
->si_blocks
;
7246 spin
->si_blocks
= bl
;
7248 ++spin
->si_blocks_cnt
;
7251 p
= bl
->sb_data
+ bl
->sb_used
;
7252 bl
->sb_used
+= (int)len
;
7258 * Make a copy of a string into memory allocated with getroom().
7261 getroom_save(spin
, s
)
7267 sc
= (char_u
*)getroom(spin
, STRLEN(s
) + 1, FALSE
);
7275 * Free the list of allocated sblock_T.
7292 * Allocate the root of a word tree.
7295 wordtree_alloc(spin
)
7298 return (wordnode_T
*)getroom(spin
, sizeof(wordnode_T
), TRUE
);
7302 * Store a word in the tree(s).
7303 * Always store it in the case-folded tree. For a keep-case word this is
7304 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7305 * used to find suggestions.
7306 * For a keep-case word also store it in the keep-case tree.
7307 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7311 store_word(spin
, word
, flags
, region
, pfxlist
, need_affix
)
7314 int flags
; /* extra flags, WF_BANNED */
7315 int region
; /* supported region(s) */
7316 char_u
*pfxlist
; /* list of prefix IDs or NULL */
7317 int need_affix
; /* only store word with affix ID */
7319 int len
= (int)STRLEN(word
);
7320 int ct
= captype(word
, word
+ len
);
7321 char_u foldword
[MAXWLEN
];
7325 (void)spell_casefold(word
, len
, foldword
, MAXWLEN
);
7326 for (p
= pfxlist
; res
== OK
; ++p
)
7328 if (!need_affix
|| (p
!= NULL
&& *p
!= NUL
))
7329 res
= tree_add_word(spin
, foldword
, spin
->si_foldroot
, ct
| flags
,
7330 region
, p
== NULL
? 0 : *p
);
7331 if (p
== NULL
|| *p
== NUL
)
7334 ++spin
->si_foldwcount
;
7336 if (res
== OK
&& (ct
== WF_KEEPCAP
|| (flags
& WF_KEEPCAP
)))
7338 for (p
= pfxlist
; res
== OK
; ++p
)
7340 if (!need_affix
|| (p
!= NULL
&& *p
!= NUL
))
7341 res
= tree_add_word(spin
, word
, spin
->si_keeproot
, flags
,
7342 region
, p
== NULL
? 0 : *p
);
7343 if (p
== NULL
|| *p
== NUL
)
7346 ++spin
->si_keepwcount
;
7352 * Add word "word" to a word tree at "root".
7353 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
7354 * "rare" and "region" is the condition nr.
7355 * Returns FAIL when out of memory.
7358 tree_add_word(spin
, word
, root
, flags
, region
, affixID
)
7366 wordnode_T
*node
= root
;
7368 wordnode_T
*copyp
, **copyprev
;
7369 wordnode_T
**prev
= NULL
;
7372 /* Add each byte of the word to the tree, including the NUL at the end. */
7375 /* When there is more than one reference to this node we need to make
7376 * a copy, so that we can modify it. Copy the whole list of siblings
7377 * (we don't optimize for a partly shared list of siblings). */
7378 if (node
!= NULL
&& node
->wn_refs
> 1)
7382 for (copyp
= node
; copyp
!= NULL
; copyp
= copyp
->wn_sibling
)
7384 /* Allocate a new node and copy the info. */
7385 np
= get_wordnode(spin
);
7388 np
->wn_child
= copyp
->wn_child
;
7389 if (np
->wn_child
!= NULL
)
7390 ++np
->wn_child
->wn_refs
; /* child gets extra ref */
7391 np
->wn_byte
= copyp
->wn_byte
;
7392 if (np
->wn_byte
== NUL
)
7394 np
->wn_flags
= copyp
->wn_flags
;
7395 np
->wn_region
= copyp
->wn_region
;
7396 np
->wn_affixID
= copyp
->wn_affixID
;
7399 /* Link the new node in the list, there will be one ref. */
7401 if (copyprev
!= NULL
)
7403 copyprev
= &np
->wn_sibling
;
7405 /* Let "node" point to the head of the copied list. */
7411 /* Look for the sibling that has the same character. They are sorted
7412 * on byte value, thus stop searching when a sibling is found with a
7413 * higher byte value. For zero bytes (end of word) the sorting is
7414 * done on flags and then on affixID. */
7416 && (node
->wn_byte
< word
[i
]
7417 || (node
->wn_byte
== NUL
7419 ? node
->wn_affixID
< (unsigned)affixID
7420 : (node
->wn_flags
< (unsigned)(flags
& WN_MASK
)
7421 || (node
->wn_flags
== (flags
& WN_MASK
)
7422 && (spin
->si_sugtree
7423 ? (node
->wn_region
& 0xffff) < region
7425 < (unsigned)affixID
)))))))
7427 prev
= &node
->wn_sibling
;
7431 || node
->wn_byte
!= word
[i
]
7435 || node
->wn_flags
!= (flags
& WN_MASK
)
7436 || node
->wn_affixID
!= affixID
)))
7438 /* Allocate a new node. */
7439 np
= get_wordnode(spin
);
7442 np
->wn_byte
= word
[i
];
7444 /* If "node" is NULL this is a new child or the end of the sibling
7445 * list: ref count is one. Otherwise use ref count of sibling and
7446 * make ref count of sibling one (matters when inserting in front
7447 * of the list of siblings). */
7452 np
->wn_refs
= node
->wn_refs
;
7456 np
->wn_sibling
= node
;
7462 node
->wn_flags
= flags
;
7463 node
->wn_region
|= region
;
7464 node
->wn_affixID
= affixID
;
7467 prev
= &node
->wn_child
;
7470 #ifdef SPELL_PRINTTREE
7471 smsg("Added \"%s\"", word
);
7472 spell_print_tree(root
->wn_sibling
);
7475 /* count nr of words added since last message */
7476 ++spin
->si_msg_count
;
7478 if (spin
->si_compress_cnt
> 1)
7480 if (--spin
->si_compress_cnt
== 1)
7481 /* Did enough words to lower the block count limit. */
7482 spin
->si_blocks_cnt
+= compress_inc
;
7486 * When we have allocated lots of memory we need to compress the word tree
7487 * to free up some room. But compression is slow, and we might actually
7488 * need that room, thus only compress in the following situations:
7489 * 1. When not compressed before (si_compress_cnt == 0): when using
7490 * "compress_start" blocks.
7491 * 2. When compressed before and used "compress_inc" blocks before
7492 * adding "compress_added" words (si_compress_cnt > 1).
7493 * 3. When compressed before, added "compress_added" words
7494 * (si_compress_cnt == 1) and the number of free nodes drops below the
7495 * maximum word length.
7497 #ifndef SPELL_PRINTTREE
7498 if (spin
->si_compress_cnt
== 1
7499 ? spin
->si_free_count
< MAXWLEN
7500 : spin
->si_blocks_cnt
>= compress_start
)
7503 /* Decrement the block counter. The effect is that we compress again
7504 * when the freed up room has been used and another "compress_inc"
7505 * blocks have been allocated. Unless "compress_added" words have
7506 * been added, then the limit is put back again. */
7507 spin
->si_blocks_cnt
-= compress_inc
;
7508 spin
->si_compress_cnt
= compress_added
;
7510 if (spin
->si_verbose
)
7513 msg_puts((char_u
*)_(msg_compressing
));
7520 /* Compress both trees. Either they both have many nodes, which makes
7521 * compression useful, or one of them is small, which means
7522 * compression goes fast. But when filling the souldfold word tree
7523 * there is no keep-case tree. */
7524 wordtree_compress(spin
, spin
->si_foldroot
);
7526 wordtree_compress(spin
, spin
->si_keeproot
);
7533 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7544 if (!VIM_ISDIGIT(*p
))
7546 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7547 start
= (getdigits(&p
) * 10) / (SBLOCKSIZE
/ 102);
7551 if (!VIM_ISDIGIT(*p
))
7553 incr
= (getdigits(&p
) * 102) / (SBLOCKSIZE
/ 10);
7557 if (!VIM_ISDIGIT(*p
))
7559 added
= getdigits(&p
) * 1024;
7563 if (start
== 0 || incr
== 0 || added
== 0 || incr
> start
)
7566 compress_start
= start
;
7567 compress_inc
= incr
;
7568 compress_added
= added
;
7574 * Get a wordnode_T, either from the list of previously freed nodes or
7575 * allocate a new one.
7583 if (spin
->si_first_free
== NULL
)
7584 n
= (wordnode_T
*)getroom(spin
, sizeof(wordnode_T
), TRUE
);
7587 n
= spin
->si_first_free
;
7588 spin
->si_first_free
= n
->wn_child
;
7589 vim_memset(n
, 0, sizeof(wordnode_T
));
7590 --spin
->si_free_count
;
7592 #ifdef SPELL_PRINTTREE
7593 n
->wn_nr
= ++spin
->si_wordnode_nr
;
7599 * Decrement the reference count on a node (which is the head of a list of
7600 * siblings). If the reference count becomes zero free the node and its
7602 * Returns the number of nodes actually freed.
7605 deref_wordnode(spin
, node
)
7612 if (--node
->wn_refs
== 0)
7614 for (np
= node
; np
!= NULL
; np
= np
->wn_sibling
)
7616 if (np
->wn_child
!= NULL
)
7617 cnt
+= deref_wordnode(spin
, np
->wn_child
);
7618 free_wordnode(spin
, np
);
7621 ++cnt
; /* length field */
7627 * Free a wordnode_T for re-use later.
7628 * Only the "wn_child" field becomes invalid.
7631 free_wordnode(spin
, n
)
7635 n
->wn_child
= spin
->si_first_free
;
7636 spin
->si_first_free
= n
;
7637 ++spin
->si_free_count
;
7641 * Compress a tree: find tails that are identical and can be shared.
7644 wordtree_compress(spin
, root
)
7653 /* Skip the root itself, it's not actually used. The first sibling is the
7654 * start of the tree. */
7655 if (root
->wn_sibling
!= NULL
)
7658 n
= node_compress(spin
, root
->wn_sibling
, &ht
, &tot
);
7660 #ifndef SPELL_PRINTTREE
7661 if (spin
->si_verbose
|| p_verbose
> 2)
7665 perc
= (tot
- n
) / (tot
/ 100);
7669 perc
= (tot
- n
) * 100 / tot
;
7670 vim_snprintf((char *)IObuff
, IOSIZE
,
7671 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7672 n
, tot
, tot
- n
, perc
);
7673 spell_message(spin
, IObuff
);
7675 #ifdef SPELL_PRINTTREE
7676 spell_print_tree(root
->wn_sibling
);
7683 * Compress a node, its siblings and its children, depth first.
7684 * Returns the number of compressed nodes.
7687 node_compress(spin
, node
, ht
, tot
)
7691 int *tot
; /* total count of nodes before compressing,
7692 incremented while going through the tree */
7704 * Go through the list of siblings. Compress each child and then try
7705 * finding an identical child to replace it.
7706 * Note that with "child" we mean not just the node that is pointed to,
7707 * but the whole list of siblings of which the child node is the first.
7709 for (np
= node
; np
!= NULL
&& !got_int
; np
= np
->wn_sibling
)
7712 if ((child
= np
->wn_child
) != NULL
)
7714 /* Compress the child first. This fills hashkey. */
7715 compressed
+= node_compress(spin
, child
, ht
, tot
);
7717 /* Try to find an identical child. */
7718 hash
= hash_hash(child
->wn_u1
.hashkey
);
7719 hi
= hash_lookup(ht
, child
->wn_u1
.hashkey
, hash
);
7720 if (!HASHITEM_EMPTY(hi
))
7722 /* There are children we encountered before with a hash value
7723 * identical to the current child. Now check if there is one
7724 * that is really identical. */
7725 for (tp
= HI2WN(hi
); tp
!= NULL
; tp
= tp
->wn_u2
.next
)
7726 if (node_equal(child
, tp
))
7728 /* Found one! Now use that child in place of the
7729 * current one. This means the current child and all
7730 * its siblings is unlinked from the tree. */
7732 compressed
+= deref_wordnode(spin
, child
);
7738 /* No other child with this hash value equals the child of
7739 * the node, add it to the linked list after the first
7742 child
->wn_u2
.next
= tp
->wn_u2
.next
;
7743 tp
->wn_u2
.next
= child
;
7747 /* No other child has this hash value, add it to the
7749 hash_add_item(ht
, hi
, child
->wn_u1
.hashkey
, hash
);
7752 *tot
+= len
+ 1; /* add one for the node that stores the length */
7755 * Make a hash key for the node and its siblings, so that we can quickly
7756 * find a lookalike node. This must be done after compressing the sibling
7757 * list, otherwise the hash key would become invalid by the compression.
7759 node
->wn_u1
.hashkey
[0] = len
;
7761 for (np
= node
; np
!= NULL
; np
= np
->wn_sibling
)
7763 if (np
->wn_byte
== NUL
)
7764 /* end node: use wn_flags, wn_region and wn_affixID */
7765 n
= np
->wn_flags
+ (np
->wn_region
<< 8) + (np
->wn_affixID
<< 16);
7767 /* byte node: use the byte value and the child pointer */
7768 n
= (unsigned)(np
->wn_byte
+ ((long_u
)np
->wn_child
<< 8));
7772 /* Avoid NUL bytes, it terminates the hash key. */
7774 node
->wn_u1
.hashkey
[1] = n
== 0 ? 1 : n
;
7775 n
= (nr
>> 8) & 0xff;
7776 node
->wn_u1
.hashkey
[2] = n
== 0 ? 1 : n
;
7777 n
= (nr
>> 16) & 0xff;
7778 node
->wn_u1
.hashkey
[3] = n
== 0 ? 1 : n
;
7779 n
= (nr
>> 24) & 0xff;
7780 node
->wn_u1
.hashkey
[4] = n
== 0 ? 1 : n
;
7781 node
->wn_u1
.hashkey
[5] = NUL
;
7783 /* Check for CTRL-C pressed now and then. */
7790 * Return TRUE when two nodes have identical siblings and children.
7800 for (p1
= n1
, p2
= n2
; p1
!= NULL
&& p2
!= NULL
;
7801 p1
= p1
->wn_sibling
, p2
= p2
->wn_sibling
)
7802 if (p1
->wn_byte
!= p2
->wn_byte
7803 || (p1
->wn_byte
== NUL
7804 ? (p1
->wn_flags
!= p2
->wn_flags
7805 || p1
->wn_region
!= p2
->wn_region
7806 || p1
->wn_affixID
!= p2
->wn_affixID
)
7807 : (p1
->wn_child
!= p2
->wn_child
)))
7810 return p1
== NULL
&& p2
== NULL
;
7814 * Write a number to file "fd", MSB first, in "len" bytes.
7817 put_bytes(fd
, nr
, len
)
7824 for (i
= len
- 1; i
>= 0; --i
)
7825 putc((int)(nr
>> (i
* 8)), fd
);
7829 # if (_MSC_VER <= 1200)
7830 /* This line is required for VC6 without the service pack. Also see the
7831 * matching #pragma below. */
7832 # pragma optimize("", off)
7837 * Write spin->si_sugtime to file "fd".
7840 put_sugtime(spin
, fd
)
7847 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7848 * can't use put_bytes() here. */
7849 for (i
= 7; i
>= 0; --i
)
7850 if (i
+ 1 > sizeof(time_t))
7851 /* ">>" doesn't work well when shifting more bits than avail */
7855 c
= (unsigned)spin
->si_sugtime
>> (i
* 8);
7861 # if (_MSC_VER <= 1200)
7862 # pragma optimize("", on)
7870 rep_compare
__ARGS((const void *s1
, const void *s2
));
7873 * Function given to qsort() to sort the REP items on "from" string.
7883 fromto_T
*p1
= (fromto_T
*)s1
;
7884 fromto_T
*p2
= (fromto_T
*)s2
;
7886 return STRCMP(p1
->ft_from
, p2
->ft_from
);
7890 * Write the Vim .spl file "fname".
7891 * Return FAIL or OK;
7894 write_vim_spell(spin
, fname
)
7911 fd
= mch_fopen((char *)fname
, "w");
7914 EMSG2(_(e_notopen
), fname
);
7918 /* <HEADER>: <fileID> <versionnr> */
7920 if (fwrite(VIMSPELLMAGIC
, VIMSPELLMAGICL
, (size_t)1, fd
) != 1)
7925 putc(VIMSPELLVERSION
, fd
); /* <versionnr> */
7928 * <SECTIONS>: <section> ... <sectionend>
7931 /* SN_INFO: <infotext> */
7932 if (spin
->si_info
!= NULL
)
7934 putc(SN_INFO
, fd
); /* <sectionID> */
7935 putc(0, fd
); /* <sectionflags> */
7937 i
= (int)STRLEN(spin
->si_info
);
7938 put_bytes(fd
, (long_u
)i
, 4); /* <sectionlen> */
7939 fwrite(spin
->si_info
, (size_t)i
, (size_t)1, fd
); /* <infotext> */
7942 /* SN_REGION: <regionname> ...
7943 * Write the region names only if there is more than one. */
7944 if (spin
->si_region_count
> 1)
7946 putc(SN_REGION
, fd
); /* <sectionID> */
7947 putc(SNF_REQUIRED
, fd
); /* <sectionflags> */
7948 l
= spin
->si_region_count
* 2;
7949 put_bytes(fd
, (long_u
)l
, 4); /* <sectionlen> */
7950 fwrite(spin
->si_region_name
, (size_t)l
, (size_t)1, fd
);
7951 /* <regionname> ... */
7952 regionmask
= (1 << spin
->si_region_count
) - 1;
7957 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7959 * The table with character flags and the table for case folding.
7960 * This makes sure the same characters are recognized as word characters
7961 * when generating an when using a spell file.
7962 * Skip this for ASCII, the table may conflict with the one used for
7964 * Also skip this for an .add.spl file, the main spell file must contain
7965 * the table (avoids that it conflicts). File is shorter too.
7967 if (!spin
->si_ascii
&& !spin
->si_add
)
7969 char_u folchars
[128 * 8];
7972 putc(SN_CHARFLAGS
, fd
); /* <sectionID> */
7973 putc(SNF_REQUIRED
, fd
); /* <sectionflags> */
7975 /* Form the <folchars> string first, we need to know its length. */
7977 for (i
= 128; i
< 256; ++i
)
7981 l
+= mb_char2bytes(spelltab
.st_fold
[i
], folchars
+ l
);
7984 folchars
[l
++] = spelltab
.st_fold
[i
];
7986 put_bytes(fd
, (long_u
)(1 + 128 + 2 + l
), 4); /* <sectionlen> */
7988 fputc(128, fd
); /* <charflagslen> */
7989 for (i
= 128; i
< 256; ++i
)
7992 if (spelltab
.st_isw
[i
])
7994 if (spelltab
.st_isu
[i
])
7996 fputc(flags
, fd
); /* <charflags> */
7999 put_bytes(fd
, (long_u
)l
, 2); /* <folcharslen> */
8000 fwrite(folchars
, (size_t)l
, (size_t)1, fd
); /* <folchars> */
8003 /* SN_MIDWORD: <midword> */
8004 if (spin
->si_midword
!= NULL
)
8006 putc(SN_MIDWORD
, fd
); /* <sectionID> */
8007 putc(SNF_REQUIRED
, fd
); /* <sectionflags> */
8009 i
= (int)STRLEN(spin
->si_midword
);
8010 put_bytes(fd
, (long_u
)i
, 4); /* <sectionlen> */
8011 fwrite(spin
->si_midword
, (size_t)i
, (size_t)1, fd
); /* <midword> */
8014 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8015 if (spin
->si_prefcond
.ga_len
> 0)
8017 putc(SN_PREFCOND
, fd
); /* <sectionID> */
8018 putc(SNF_REQUIRED
, fd
); /* <sectionflags> */
8020 l
= write_spell_prefcond(NULL
, &spin
->si_prefcond
);
8021 put_bytes(fd
, (long_u
)l
, 4); /* <sectionlen> */
8023 write_spell_prefcond(fd
, &spin
->si_prefcond
);
8026 /* SN_REP: <repcount> <rep> ...
8027 * SN_SAL: <salflags> <salcount> <sal> ...
8028 * SN_REPSAL: <repcount> <rep> ... */
8030 /* round 1: SN_REP section
8031 * round 2: SN_SAL section (unless SN_SOFO is used)
8032 * round 3: SN_REPSAL section */
8033 for (round
= 1; round
<= 3; ++round
)
8036 gap
= &spin
->si_rep
;
8037 else if (round
== 2)
8039 /* Don't write SN_SAL when using a SN_SOFO section */
8040 if (spin
->si_sofofr
!= NULL
&& spin
->si_sofoto
!= NULL
)
8042 gap
= &spin
->si_sal
;
8045 gap
= &spin
->si_repsal
;
8047 /* Don't write the section if there are no items. */
8048 if (gap
->ga_len
== 0)
8051 /* Sort the REP/REPSAL items. */
8053 qsort(gap
->ga_data
, (size_t)gap
->ga_len
,
8054 sizeof(fromto_T
), rep_compare
);
8056 i
= round
== 1 ? SN_REP
: (round
== 2 ? SN_SAL
: SN_REPSAL
);
8057 putc(i
, fd
); /* <sectionID> */
8059 /* This is for making suggestions, section is not required. */
8060 putc(0, fd
); /* <sectionflags> */
8062 /* Compute the length of what follows. */
8063 l
= 2; /* count <repcount> or <salcount> */
8064 for (i
= 0; i
< gap
->ga_len
; ++i
)
8066 ftp
= &((fromto_T
*)gap
->ga_data
)[i
];
8067 l
+= 1 + (int)STRLEN(ftp
->ft_from
); /* count <*fromlen> and <*from> */
8068 l
+= 1 + (int)STRLEN(ftp
->ft_to
); /* count <*tolen> and <*to> */
8071 ++l
; /* count <salflags> */
8072 put_bytes(fd
, (long_u
)l
, 4); /* <sectionlen> */
8077 if (spin
->si_followup
)
8079 if (spin
->si_collapse
)
8081 if (spin
->si_rem_accents
)
8082 i
|= SAL_REM_ACCENTS
;
8083 putc(i
, fd
); /* <salflags> */
8086 put_bytes(fd
, (long_u
)gap
->ga_len
, 2); /* <repcount> or <salcount> */
8087 for (i
= 0; i
< gap
->ga_len
; ++i
)
8089 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8090 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8091 ftp
= &((fromto_T
*)gap
->ga_data
)[i
];
8092 for (rr
= 1; rr
<= 2; ++rr
)
8094 p
= rr
== 1 ? ftp
->ft_from
: ftp
->ft_to
;
8097 fwrite(p
, l
, (size_t)1, fd
);
8103 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8104 * This is for making suggestions, section is not required. */
8105 if (spin
->si_sofofr
!= NULL
&& spin
->si_sofoto
!= NULL
)
8107 putc(SN_SOFO
, fd
); /* <sectionID> */
8108 putc(0, fd
); /* <sectionflags> */
8110 l
= (int)STRLEN(spin
->si_sofofr
);
8111 put_bytes(fd
, (long_u
)(l
+ STRLEN(spin
->si_sofoto
) + 4), 4);
8114 put_bytes(fd
, (long_u
)l
, 2); /* <sofofromlen> */
8115 fwrite(spin
->si_sofofr
, l
, (size_t)1, fd
); /* <sofofrom> */
8117 l
= (int)STRLEN(spin
->si_sofoto
);
8118 put_bytes(fd
, (long_u
)l
, 2); /* <sofotolen> */
8119 fwrite(spin
->si_sofoto
, l
, (size_t)1, fd
); /* <sofoto> */
8122 /* SN_WORDS: <word> ...
8123 * This is for making suggestions, section is not required. */
8124 if (spin
->si_commonwords
.ht_used
> 0)
8126 putc(SN_WORDS
, fd
); /* <sectionID> */
8127 putc(0, fd
); /* <sectionflags> */
8129 /* round 1: count the bytes
8130 * round 2: write the bytes */
8131 for (round
= 1; round
<= 2; ++round
)
8137 todo
= (int)spin
->si_commonwords
.ht_used
;
8138 for (hi
= spin
->si_commonwords
.ht_array
; todo
> 0; ++hi
)
8139 if (!HASHITEM_EMPTY(hi
))
8141 l
= (int)STRLEN(hi
->hi_key
) + 1;
8143 if (round
== 2) /* <word> */
8144 fwrite(hi
->hi_key
, (size_t)l
, (size_t)1, fd
);
8148 put_bytes(fd
, (long_u
)len
, 4); /* <sectionlen> */
8153 * This is for making suggestions, section is not required. */
8154 if (spin
->si_map
.ga_len
> 0)
8156 putc(SN_MAP
, fd
); /* <sectionID> */
8157 putc(0, fd
); /* <sectionflags> */
8158 l
= spin
->si_map
.ga_len
;
8159 put_bytes(fd
, (long_u
)l
, 4); /* <sectionlen> */
8160 fwrite(spin
->si_map
.ga_data
, (size_t)l
, (size_t)1, fd
);
8164 /* SN_SUGFILE: <timestamp>
8165 * This is used to notify that a .sug file may be available and at the
8166 * same time allows for checking that a .sug file that is found matches
8167 * with this .spl file. That's because the word numbers must be exactly
8169 if (!spin
->si_nosugfile
8170 && (spin
->si_sal
.ga_len
> 0
8171 || (spin
->si_sofofr
!= NULL
&& spin
->si_sofoto
!= NULL
)))
8173 putc(SN_SUGFILE
, fd
); /* <sectionID> */
8174 putc(0, fd
); /* <sectionflags> */
8175 put_bytes(fd
, (long_u
)8, 4); /* <sectionlen> */
8177 /* Set si_sugtime and write it to the file. */
8178 spin
->si_sugtime
= time(NULL
);
8179 put_sugtime(spin
, fd
); /* <timestamp> */
8182 /* SN_NOSPLITSUGS: nothing
8183 * This is used to notify that no suggestions with word splits are to be
8185 if (spin
->si_nosplitsugs
)
8187 putc(SN_NOSPLITSUGS
, fd
); /* <sectionID> */
8188 putc(0, fd
); /* <sectionflags> */
8189 put_bytes(fd
, (long_u
)0, 4); /* <sectionlen> */
8192 /* SN_COMPOUND: compound info.
8193 * We don't mark it required, when not supported all compound words will
8195 if (spin
->si_compflags
!= NULL
)
8197 putc(SN_COMPOUND
, fd
); /* <sectionID> */
8198 putc(0, fd
); /* <sectionflags> */
8200 l
= (int)STRLEN(spin
->si_compflags
);
8201 for (i
= 0; i
< spin
->si_comppat
.ga_len
; ++i
)
8202 l
+= (int)STRLEN(((char_u
**)(spin
->si_comppat
.ga_data
))[i
]) + 1;
8203 put_bytes(fd
, (long_u
)(l
+ 7), 4); /* <sectionlen> */
8205 putc(spin
->si_compmax
, fd
); /* <compmax> */
8206 putc(spin
->si_compminlen
, fd
); /* <compminlen> */
8207 putc(spin
->si_compsylmax
, fd
); /* <compsylmax> */
8208 putc(0, fd
); /* for Vim 7.0b compatibility */
8209 putc(spin
->si_compoptions
, fd
); /* <compoptions> */
8210 put_bytes(fd
, (long_u
)spin
->si_comppat
.ga_len
, 2);
8211 /* <comppatcount> */
8212 for (i
= 0; i
< spin
->si_comppat
.ga_len
; ++i
)
8214 p
= ((char_u
**)(spin
->si_comppat
.ga_data
))[i
];
8215 putc((int)STRLEN(p
), fd
); /* <comppatlen> */
8216 fwrite(p
, (size_t)STRLEN(p
), (size_t)1, fd
);/* <comppattext> */
8219 fwrite(spin
->si_compflags
, (size_t)STRLEN(spin
->si_compflags
),
8223 /* SN_NOBREAK: NOBREAK flag */
8224 if (spin
->si_nobreak
)
8226 putc(SN_NOBREAK
, fd
); /* <sectionID> */
8227 putc(0, fd
); /* <sectionflags> */
8229 /* It's empty, the presence of the section flags the feature. */
8230 put_bytes(fd
, (long_u
)0, 4); /* <sectionlen> */
8233 /* SN_SYLLABLE: syllable info.
8234 * We don't mark it required, when not supported syllables will not be
8236 if (spin
->si_syllable
!= NULL
)
8238 putc(SN_SYLLABLE
, fd
); /* <sectionID> */
8239 putc(0, fd
); /* <sectionflags> */
8241 l
= (int)STRLEN(spin
->si_syllable
);
8242 put_bytes(fd
, (long_u
)l
, 4); /* <sectionlen> */
8243 fwrite(spin
->si_syllable
, (size_t)l
, (size_t)1, fd
); /* <syllable> */
8246 /* end of <SECTIONS> */
8247 putc(SN_END
, fd
); /* <sectionend> */
8251 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
8253 spin
->si_memtot
= 0;
8254 for (round
= 1; round
<= 3; ++round
)
8257 tree
= spin
->si_foldroot
->wn_sibling
;
8258 else if (round
== 2)
8259 tree
= spin
->si_keeproot
->wn_sibling
;
8261 tree
= spin
->si_prefroot
->wn_sibling
;
8263 /* Clear the index and wnode fields in the tree. */
8266 /* Count the number of nodes. Needed to be able to allocate the
8267 * memory when reading the nodes. Also fills in index for shared
8269 nodecount
= put_node(NULL
, tree
, 0, regionmask
, round
== 3);
8271 /* number of nodes in 4 bytes */
8272 put_bytes(fd
, (long_u
)nodecount
, 4); /* <nodecount> */
8273 spin
->si_memtot
+= nodecount
+ nodecount
* sizeof(int);
8275 /* Write the nodes. */
8276 (void)put_node(fd
, tree
, 0, regionmask
, round
== 3);
8279 /* Write another byte to check for errors. */
8280 if (putc(0, fd
) == EOF
)
8283 if (fclose(fd
) == EOF
)
8290 * Clear the index and wnode fields of "node", it siblings and its
8291 * children. This is needed because they are a union with other items to save
8301 for (np
= node
; np
!= NULL
; np
= np
->wn_sibling
)
8303 np
->wn_u1
.index
= 0;
8304 np
->wn_u2
.wnode
= NULL
;
8306 if (np
->wn_byte
!= NUL
)
8307 clear_node(np
->wn_child
);
8313 * Dump a word tree at node "node".
8315 * This first writes the list of possible bytes (siblings). Then for each
8316 * byte recursively write the children.
8318 * NOTE: The code here must match the code in read_tree_node(), since
8319 * assumptions are made about the indexes (so that we don't have to write them
8322 * Returns the number of nodes used.
8325 put_node(fd
, node
, idx
, regionmask
, prefixtree
)
8326 FILE *fd
; /* NULL when only counting */
8330 int prefixtree
; /* TRUE for PREFIXTREE */
8333 int siblingcount
= 0;
8337 /* If "node" is zero the tree is empty. */
8341 /* Store the index where this node is written. */
8342 node
->wn_u1
.index
= idx
;
8344 /* Count the number of siblings. */
8345 for (np
= node
; np
!= NULL
; np
= np
->wn_sibling
)
8348 /* Write the sibling count. */
8350 putc(siblingcount
, fd
); /* <siblingcount> */
8352 /* Write each sibling byte and optionally extra info. */
8353 for (np
= node
; np
!= NULL
; np
= np
->wn_sibling
)
8355 if (np
->wn_byte
== 0)
8359 /* For a NUL byte (end of word) write the flags etc. */
8362 /* In PREFIXTREE write the required affixID and the
8363 * associated condition nr (stored in wn_region). The
8364 * byte value is misused to store the "rare" and "not
8365 * combining" flags */
8366 if (np
->wn_flags
== (short_u
)PFX_FLAGS
)
8367 putc(BY_NOFLAGS
, fd
); /* <byte> */
8370 putc(BY_FLAGS
, fd
); /* <byte> */
8371 putc(np
->wn_flags
, fd
); /* <pflags> */
8373 putc(np
->wn_affixID
, fd
); /* <affixID> */
8374 put_bytes(fd
, (long_u
)np
->wn_region
, 2); /* <prefcondnr> */
8378 /* For word trees we write the flag/region items. */
8379 flags
= np
->wn_flags
;
8380 if (regionmask
!= 0 && np
->wn_region
!= regionmask
)
8382 if (np
->wn_affixID
!= 0)
8386 /* word without flags or region */
8387 putc(BY_NOFLAGS
, fd
); /* <byte> */
8391 if (np
->wn_flags
>= 0x100)
8393 putc(BY_FLAGS2
, fd
); /* <byte> */
8394 putc(flags
, fd
); /* <flags> */
8395 putc((unsigned)flags
>> 8, fd
); /* <flags2> */
8399 putc(BY_FLAGS
, fd
); /* <byte> */
8400 putc(flags
, fd
); /* <flags> */
8402 if (flags
& WF_REGION
)
8403 putc(np
->wn_region
, fd
); /* <region> */
8405 putc(np
->wn_affixID
, fd
); /* <affixID> */
8412 if (np
->wn_child
->wn_u1
.index
!= 0
8413 && np
->wn_child
->wn_u2
.wnode
!= node
)
8415 /* The child is written elsewhere, write the reference. */
8418 putc(BY_INDEX
, fd
); /* <byte> */
8420 put_bytes(fd
, (long_u
)np
->wn_child
->wn_u1
.index
, 3);
8423 else if (np
->wn_child
->wn_u2
.wnode
== NULL
)
8424 /* We will write the child below and give it an index. */
8425 np
->wn_child
->wn_u2
.wnode
= node
;
8428 if (putc(np
->wn_byte
, fd
) == EOF
) /* <byte> or <xbyte> */
8436 /* Space used in the array when reading: one for each sibling and one for
8438 newindex
+= siblingcount
+ 1;
8440 /* Recursively dump the children of each sibling. */
8441 for (np
= node
; np
!= NULL
; np
= np
->wn_sibling
)
8442 if (np
->wn_byte
!= 0 && np
->wn_child
->wn_u2
.wnode
== node
)
8443 newindex
= put_node(fd
, np
->wn_child
, newindex
, regionmask
,
8451 * ":mkspell [-ascii] outfile infile ..."
8452 * ":mkspell [-ascii] addfile"
8460 char_u
*arg
= eap
->arg
;
8463 if (STRNCMP(arg
, "-ascii", 6) == 0)
8466 arg
= skipwhite(arg
+ 6);
8469 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8470 if (get_arglist_exp(arg
, &fcount
, &fnames
) == OK
)
8472 mkspell(fcount
, fnames
, ascii
, eap
->forceit
, FALSE
);
8473 FreeWild(fcount
, fnames
);
8478 * Create the .sug file.
8479 * Uses the soundfold info in "spin".
8480 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8483 spell_make_sugfile(spin
, wfname
)
8487 char_u fname
[MAXPATHL
];
8490 int free_slang
= FALSE
;
8493 * Read back the .spl file that was written. This fills the required
8494 * info for soundfolding. This also uses less memory than the
8495 * pointer-linked version of the trie. And it avoids having two versions
8496 * of the code for the soundfolding stuff.
8497 * It might have been done already by spell_reload_one().
8499 for (slang
= first_lang
; slang
!= NULL
; slang
= slang
->sl_next
)
8500 if (fullpathcmp(wfname
, slang
->sl_fname
, FALSE
) == FPC_SAME
)
8504 spell_message(spin
, (char_u
*)_("Reading back spell file..."));
8505 slang
= spell_load_file(wfname
, NULL
, NULL
, FALSE
);
8512 * Clear the info in "spin" that is used.
8514 spin
->si_blocks
= NULL
;
8515 spin
->si_blocks_cnt
= 0;
8516 spin
->si_compress_cnt
= 0; /* will stay at 0 all the time*/
8517 spin
->si_free_count
= 0;
8518 spin
->si_first_free
= NULL
;
8519 spin
->si_foldwcount
= 0;
8522 * Go through the trie of good words, soundfold each word and add it to
8523 * the soundfold trie.
8525 spell_message(spin
, (char_u
*)_("Performing soundfolding..."));
8526 if (sug_filltree(spin
, slang
) == FAIL
)
8530 * Create the table which links each soundfold word with a list of the
8531 * good words it may come from. Creates buffer "spin->si_spellbuf".
8532 * This also removes the wordnr from the NUL byte entries to make
8533 * compression possible.
8535 if (sug_maketable(spin
) == FAIL
)
8538 smsg((char_u
*)_("Number of words after soundfolding: %ld"),
8539 (long)spin
->si_spellbuf
->b_ml
.ml_line_count
);
8542 * Compress the soundfold trie.
8544 spell_message(spin
, (char_u
*)_(msg_compressing
));
8545 wordtree_compress(spin
, spin
->si_foldroot
);
8548 * Write the .sug file.
8549 * Make the file name by changing ".spl" to ".sug".
8551 STRCPY(fname
, wfname
);
8552 len
= (int)STRLEN(fname
);
8553 fname
[len
- 2] = 'u';
8554 fname
[len
- 1] = 'g';
8555 sug_write(spin
, fname
);
8560 free_blocks(spin
->si_blocks
);
8561 close_spellbuf(spin
->si_spellbuf
);
8565 * Build the soundfold trie for language "slang".
8568 sug_filltree(spin
, slang
)
8575 idx_T arridx
[MAXWLEN
];
8577 char_u tword
[MAXWLEN
];
8578 char_u tsalword
[MAXWLEN
];
8581 unsigned words_done
= 0;
8582 int wordcount
[MAXWLEN
];
8584 /* We use si_foldroot for the souldfolded trie. */
8585 spin
->si_foldroot
= wordtree_alloc(spin
);
8586 if (spin
->si_foldroot
== NULL
)
8589 /* let tree_add_word() know we're adding to the soundfolded tree */
8590 spin
->si_sugtree
= TRUE
;
8593 * Go through the whole case-folded tree, soundfold each word and put it
8596 byts
= slang
->sl_fbyts
;
8597 idxs
= slang
->sl_fidxs
;
8604 while (depth
>= 0 && !got_int
)
8606 if (curi
[depth
] > byts
[arridx
[depth
]])
8608 /* Done all bytes at this node, go up one level. */
8609 idxs
[arridx
[depth
]] = wordcount
[depth
];
8611 wordcount
[depth
- 1] += wordcount
[depth
];
8619 /* Do one more byte at this node. */
8620 n
= arridx
[depth
] + curi
[depth
];
8626 /* Sound-fold the word. */
8628 spell_soundfold(slang
, tword
, TRUE
, tsalword
);
8630 /* We use the "flags" field for the MSB of the wordnr,
8631 * "region" for the LSB of the wordnr. */
8632 if (tree_add_word(spin
, tsalword
, spin
->si_foldroot
,
8633 words_done
>> 16, words_done
& 0xffff,
8640 /* Reset the block count each time to avoid compression
8642 spin
->si_blocks_cnt
= 0;
8644 /* Skip over any other NUL bytes (same word with different
8646 while (byts
[n
+ 1] == 0)
8654 /* Normal char, go one level deeper. */
8656 arridx
[depth
] = idxs
[n
];
8658 wordcount
[depth
] = 0;
8663 smsg((char_u
*)_("Total number of words: %d"), words_done
);
8669 * Make the table that links each word in the soundfold trie to the words it
8670 * can be produced from.
8671 * This is not unlike lines in a file, thus use a memfile to be able to access
8672 * the table efficiently.
8673 * Returns FAIL when out of memory.
8682 /* Allocate a buffer, open a memline for it and create the swap file
8683 * (uses a temp file, not a .swp file). */
8684 spin
->si_spellbuf
= open_spellbuf();
8685 if (spin
->si_spellbuf
== NULL
)
8688 /* Use a buffer to store the line info, avoids allocating many small
8689 * pieces of memory. */
8690 ga_init2(&ga
, 1, 100);
8692 /* recursively go through the tree */
8693 if (sug_filltable(spin
, spin
->si_foldroot
->wn_sibling
, 0, &ga
) == -1)
8701 * Fill the table for one node and its children.
8702 * Returns the wordnr at the start of the node.
8703 * Returns -1 when out of memory.
8706 sug_filltable(spin
, node
, startwordnr
, gap
)
8710 garray_T
*gap
; /* place to store line of numbers */
8713 int wordnr
= startwordnr
;
8717 for (p
= node
; p
!= NULL
; p
= p
->wn_sibling
)
8719 if (p
->wn_byte
== NUL
)
8723 for (np
= p
; np
!= NULL
&& np
->wn_byte
== NUL
; np
= np
->wn_sibling
)
8725 if (ga_grow(gap
, 10) == FAIL
)
8728 nr
= (np
->wn_flags
<< 16) + (np
->wn_region
& 0xffff);
8729 /* Compute the offset from the previous nr and store the
8730 * offset in a way that it takes a minimum number of bytes.
8731 * It's a bit like utf-8, but without the need to mark
8732 * following bytes. */
8735 gap
->ga_len
+= offset2bytes(nr
,
8736 (char_u
*)gap
->ga_data
+ gap
->ga_len
);
8739 /* add the NUL byte */
8740 ((char_u
*)gap
->ga_data
)[gap
->ga_len
++] = NUL
;
8742 if (ml_append_buf(spin
->si_spellbuf
, (linenr_T
)wordnr
,
8743 gap
->ga_data
, gap
->ga_len
, TRUE
) == FAIL
)
8747 /* Remove extra NUL entries, we no longer need them. We don't
8748 * bother freeing the nodes, the won't be reused anyway. */
8749 while (p
->wn_sibling
!= NULL
&& p
->wn_sibling
->wn_byte
== NUL
)
8750 p
->wn_sibling
= p
->wn_sibling
->wn_sibling
;
8752 /* Clear the flags on the remaining NUL node, so that compression
8753 * works a lot better. */
8759 wordnr
= sug_filltable(spin
, p
->wn_child
, wordnr
, gap
);
8768 * Convert an offset into a minimal number of bytes.
8769 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8773 offset2bytes(nr
, buf
)
8780 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8788 if (b4
> 1 || b3
> 0x1f) /* 4 bytes */
8796 if (b3
> 1 || b2
> 0x3f ) /* 3 bytes */
8803 if (b2
> 1 || b1
> 0x7f ) /* 2 bytes */
8815 * Opposite of offset2bytes().
8816 * "pp" points to the bytes and is advanced over it.
8817 * Returns the offset.
8828 if ((c
& 0x80) == 0x00) /* 1 byte */
8832 else if ((c
& 0xc0) == 0x80) /* 2 bytes */
8834 nr
= (c
& 0x3f) - 1;
8835 nr
= nr
* 255 + (*p
++ - 1);
8837 else if ((c
& 0xe0) == 0xc0) /* 3 bytes */
8839 nr
= (c
& 0x1f) - 1;
8840 nr
= nr
* 255 + (*p
++ - 1);
8841 nr
= nr
* 255 + (*p
++ - 1);
8845 nr
= (c
& 0x0f) - 1;
8846 nr
= nr
* 255 + (*p
++ - 1);
8847 nr
= nr
* 255 + (*p
++ - 1);
8848 nr
= nr
* 255 + (*p
++ - 1);
8856 * Write the .sug file in "fname".
8859 sug_write(spin
, fname
)
8871 /* Create the file. Note that an existing file is silently overwritten! */
8872 fd
= mch_fopen((char *)fname
, "w");
8875 EMSG2(_(e_notopen
), fname
);
8879 vim_snprintf((char *)IObuff
, IOSIZE
,
8880 _("Writing suggestion file %s ..."), fname
);
8881 spell_message(spin
, IObuff
);
8884 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8886 if (fwrite(VIMSUGMAGIC
, VIMSUGMAGICL
, (size_t)1, fd
) != 1) /* <fileID> */
8891 putc(VIMSUGVERSION
, fd
); /* <versionnr> */
8893 /* Write si_sugtime to the file. */
8894 put_sugtime(spin
, fd
); /* <timestamp> */
8899 spin
->si_memtot
= 0;
8900 tree
= spin
->si_foldroot
->wn_sibling
;
8902 /* Clear the index and wnode fields in the tree. */
8905 /* Count the number of nodes. Needed to be able to allocate the
8906 * memory when reading the nodes. Also fills in index for shared
8908 nodecount
= put_node(NULL
, tree
, 0, 0, FALSE
);
8910 /* number of nodes in 4 bytes */
8911 put_bytes(fd
, (long_u
)nodecount
, 4); /* <nodecount> */
8912 spin
->si_memtot
+= nodecount
+ nodecount
* sizeof(int);
8914 /* Write the nodes. */
8915 (void)put_node(fd
, tree
, 0, 0, FALSE
);
8918 * <SUGTABLE>: <sugwcount> <sugline> ...
8920 wcount
= spin
->si_spellbuf
->b_ml
.ml_line_count
;
8921 put_bytes(fd
, (long_u
)wcount
, 4); /* <sugwcount> */
8923 for (lnum
= 1; lnum
<= (linenr_T
)wcount
; ++lnum
)
8925 /* <sugline>: <sugnr> ... NUL */
8926 line
= ml_get_buf(spin
->si_spellbuf
, lnum
, FALSE
);
8927 len
= (int)STRLEN(line
) + 1;
8928 if (fwrite(line
, (size_t)len
, (size_t)1, fd
) == 0)
8933 spin
->si_memtot
+= len
;
8936 /* Write another byte to check for errors. */
8937 if (putc(0, fd
) == EOF
)
8940 vim_snprintf((char *)IObuff
, IOSIZE
,
8941 _("Estimated runtime memory use: %d bytes"), spin
->si_memtot
);
8942 spell_message(spin
, IObuff
);
8945 /* close the file */
8950 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8951 * list and only contains text lines. Can use a swapfile to reduce memory
8953 * Most other fields are invalid! Esp. watch out for string options being
8954 * NULL and there is no undo info.
8955 * Returns NULL when out of memory.
8962 buf
= (buf_T
*)alloc_clear(sizeof(buf_T
));
8965 buf
->b_spell
= TRUE
;
8966 buf
->b_p_swf
= TRUE
; /* may create a swap file */
8968 ml_open_file(buf
); /* create swap file now */
8974 * Close the buffer used for spell info.
8982 ml_close(buf
, TRUE
);
8989 * Create a Vim spell file from one or more word lists.
8990 * "fnames[0]" is the output file name.
8991 * "fnames[fcount - 1]" is the last input file name.
8992 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8993 * and ".spl" is appended to make the output file name.
8996 mkspell(fcount
, fnames
, ascii
, overwrite
, added_word
)
8999 int ascii
; /* -ascii argument given */
9000 int overwrite
; /* overwrite existing output file */
9001 int added_word
; /* invoked through "zg" */
9003 char_u fname
[MAXPATHL
];
9004 char_u wfname
[MAXPATHL
];
9007 afffile_T
*(afile
[8]);
9014 vim_memset(&spin
, 0, sizeof(spin
));
9015 spin
.si_verbose
= !added_word
;
9016 spin
.si_ascii
= ascii
;
9017 spin
.si_followup
= TRUE
;
9018 spin
.si_rem_accents
= TRUE
;
9019 ga_init2(&spin
.si_rep
, (int)sizeof(fromto_T
), 20);
9020 ga_init2(&spin
.si_repsal
, (int)sizeof(fromto_T
), 20);
9021 ga_init2(&spin
.si_sal
, (int)sizeof(fromto_T
), 20);
9022 ga_init2(&spin
.si_map
, (int)sizeof(char_u
), 100);
9023 ga_init2(&spin
.si_comppat
, (int)sizeof(char_u
*), 20);
9024 ga_init2(&spin
.si_prefcond
, (int)sizeof(char_u
*), 50);
9025 hash_init(&spin
.si_commonwords
);
9026 spin
.si_newcompID
= 127; /* start compound ID at first maximum */
9028 /* default: fnames[0] is output file, following are input files */
9029 innames
= &fnames
[1];
9030 incount
= fcount
- 1;
9034 len
= (int)STRLEN(fnames
[0]);
9035 if (fcount
== 1 && len
> 4 && STRCMP(fnames
[0] + len
- 4, ".add") == 0)
9037 /* For ":mkspell path/en.latin1.add" output file is
9038 * "path/en.latin1.add.spl". */
9039 innames
= &fnames
[0];
9041 vim_snprintf((char *)wfname
, sizeof(wfname
), "%s.spl", fnames
[0]);
9043 else if (fcount
== 1)
9045 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9046 innames
= &fnames
[0];
9048 vim_snprintf((char *)wfname
, sizeof(wfname
), "%s.%s.spl", fnames
[0],
9049 spin
.si_ascii
? (char_u
*)"ascii" : spell_enc());
9051 else if (len
> 4 && STRCMP(fnames
[0] + len
- 4, ".spl") == 0)
9053 /* Name ends in ".spl", use as the file name. */
9054 vim_strncpy(wfname
, fnames
[0], sizeof(wfname
) - 1);
9057 /* Name should be language, make the file name from it. */
9058 vim_snprintf((char *)wfname
, sizeof(wfname
), "%s.%s.spl", fnames
[0],
9059 spin
.si_ascii
? (char_u
*)"ascii" : spell_enc());
9061 /* Check for .ascii.spl. */
9062 if (strstr((char *)gettail(wfname
), ".ascii.") != NULL
)
9063 spin
.si_ascii
= TRUE
;
9065 /* Check for .add.spl. */
9066 if (strstr((char *)gettail(wfname
), ".add.") != NULL
)
9071 EMSG(_(e_invarg
)); /* need at least output and input names */
9072 else if (vim_strchr(gettail(wfname
), '_') != NULL
)
9073 EMSG(_("E751: Output file name must not have region name"));
9074 else if (incount
> 8)
9075 EMSG(_("E754: Only up to 8 regions supported"));
9078 /* Check for overwriting before doing things that may take a lot of
9080 if (!overwrite
&& mch_stat((char *)wfname
, &st
) >= 0)
9085 if (mch_isdir(wfname
))
9087 EMSG2(_(e_isadir2
), wfname
);
9092 * Init the aff and dic pointers.
9093 * Get the region names if there are more than 2 arguments.
9095 for (i
= 0; i
< incount
; ++i
)
9101 len
= (int)STRLEN(innames
[i
]);
9102 if (STRLEN(gettail(innames
[i
])) < 5
9103 || innames
[i
][len
- 3] != '_')
9105 EMSG2(_("E755: Invalid region in %s"), innames
[i
]);
9108 spin
.si_region_name
[i
* 2] = TOLOWER_ASC(innames
[i
][len
- 2]);
9109 spin
.si_region_name
[i
* 2 + 1] =
9110 TOLOWER_ASC(innames
[i
][len
- 1]);
9113 spin
.si_region_count
= incount
;
9115 spin
.si_foldroot
= wordtree_alloc(&spin
);
9116 spin
.si_keeproot
= wordtree_alloc(&spin
);
9117 spin
.si_prefroot
= wordtree_alloc(&spin
);
9118 if (spin
.si_foldroot
== NULL
9119 || spin
.si_keeproot
== NULL
9120 || spin
.si_prefroot
== NULL
)
9122 free_blocks(spin
.si_blocks
);
9126 /* When not producing a .add.spl file clear the character table when
9127 * we encounter one in the .aff file. This means we dump the current
9128 * one in the .spl file if the .aff file doesn't define one. That's
9129 * better than guessing the contents, the table will match a
9130 * previously loaded spell file. */
9132 spin
.si_clear_chartab
= TRUE
;
9135 * Read all the .aff and .dic files.
9136 * Text is converted to 'encoding'.
9137 * Words are stored in the case-folded and keep-case trees.
9139 for (i
= 0; i
< incount
&& !error
; ++i
)
9141 spin
.si_conv
.vc_type
= CONV_NONE
;
9142 spin
.si_region
= 1 << i
;
9144 vim_snprintf((char *)fname
, sizeof(fname
), "%s.aff", innames
[i
]);
9145 if (mch_stat((char *)fname
, &st
) >= 0)
9147 /* Read the .aff file. Will init "spin->si_conv" based on the
9149 afile
[i
] = spell_read_aff(&spin
, fname
);
9150 if (afile
[i
] == NULL
)
9154 /* Read the .dic file and store the words in the trees. */
9155 vim_snprintf((char *)fname
, sizeof(fname
), "%s.dic",
9157 if (spell_read_dic(&spin
, fname
, afile
[i
]) == FAIL
)
9163 /* No .aff file, try reading the file as a word list. Store
9164 * the words in the trees. */
9165 if (spell_read_wordfile(&spin
, innames
[i
]) == FAIL
)
9170 /* Free any conversion stuff. */
9171 convert_setup(&spin
.si_conv
, NULL
, NULL
);
9175 if (spin
.si_compflags
!= NULL
&& spin
.si_nobreak
)
9176 MSG(_("Warning: both compounding and NOBREAK specified"));
9178 if (!error
&& !got_int
)
9181 * Combine tails in the tree.
9183 spell_message(&spin
, (char_u
*)_(msg_compressing
));
9184 wordtree_compress(&spin
, spin
.si_foldroot
);
9185 wordtree_compress(&spin
, spin
.si_keeproot
);
9186 wordtree_compress(&spin
, spin
.si_prefroot
);
9189 if (!error
&& !got_int
)
9192 * Write the info in the spell file.
9194 vim_snprintf((char *)IObuff
, IOSIZE
,
9195 _("Writing spell file %s ..."), wfname
);
9196 spell_message(&spin
, IObuff
);
9198 error
= write_vim_spell(&spin
, wfname
) == FAIL
;
9200 spell_message(&spin
, (char_u
*)_("Done!"));
9201 vim_snprintf((char *)IObuff
, IOSIZE
,
9202 _("Estimated runtime memory use: %d bytes"), spin
.si_memtot
);
9203 spell_message(&spin
, IObuff
);
9206 * If the file is loaded need to reload it.
9209 spell_reload_one(wfname
, added_word
);
9212 /* Free the allocated memory. */
9213 ga_clear(&spin
.si_rep
);
9214 ga_clear(&spin
.si_repsal
);
9215 ga_clear(&spin
.si_sal
);
9216 ga_clear(&spin
.si_map
);
9217 ga_clear(&spin
.si_comppat
);
9218 ga_clear(&spin
.si_prefcond
);
9219 hash_clear_all(&spin
.si_commonwords
, 0);
9221 /* Free the .aff file structures. */
9222 for (i
= 0; i
< incount
; ++i
)
9223 if (afile
[i
] != NULL
)
9224 spell_free_aff(afile
[i
]);
9226 /* Free all the bits and pieces at once. */
9227 free_blocks(spin
.si_blocks
);
9230 * If there is soundfolding info and no NOSUGFILE item create the
9231 * .sug file with the soundfolded word trie.
9233 if (spin
.si_sugtime
!= 0 && !error
&& !got_int
)
9234 spell_make_sugfile(&spin
, wfname
);
9240 * Display a message for spell file processing when 'verbose' is set or using
9241 * ":mkspell". "str" can be IObuff.
9244 spell_message(spin
, str
)
9248 if (spin
->si_verbose
|| p_verbose
> 2)
9250 if (!spin
->si_verbose
)
9254 if (!spin
->si_verbose
)
9260 * ":[count]spellgood {word}"
9261 * ":[count]spellwrong {word}"
9262 * ":[count]spellundo {word}"
9268 spell_add_word(eap
->arg
, (int)STRLEN(eap
->arg
), eap
->cmdidx
== CMD_spellwrong
,
9269 eap
->forceit
? 0 : (int)eap
->line2
,
9270 eap
->cmdidx
== CMD_spellundo
);
9274 * Add "word[len]" to 'spellfile' as a good or bad word.
9277 spell_add_word(word
, len
, bad
, idx
, undo
)
9281 int idx
; /* "zG" and "zW": zero, otherwise index in
9283 int undo
; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
9287 int new_spf
= FALSE
;
9289 char_u fnamebuf
[MAXPATHL
];
9290 char_u line
[MAXWLEN
* 2];
9291 long fpos
, fpos_next
= 0;
9295 if (idx
== 0) /* use internal wordlist */
9297 if (int_wordlist
== NULL
)
9299 int_wordlist
= vim_tempname('s');
9300 if (int_wordlist
== NULL
)
9303 fname
= int_wordlist
;
9307 /* If 'spellfile' isn't set figure out a good default value. */
9308 if (*curbuf
->b_p_spf
== NUL
)
9314 if (*curbuf
->b_p_spf
== NUL
)
9316 EMSG2(_(e_notset
), "spellfile");
9320 for (spf
= curbuf
->b_p_spf
, i
= 1; *spf
!= NUL
; ++i
)
9322 copy_option_part(&spf
, fnamebuf
, MAXPATHL
, ",");
9327 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx
);
9332 /* Check that the user isn't editing the .add file somewhere. */
9333 buf
= buflist_findname_exp(fnamebuf
);
9334 if (buf
!= NULL
&& buf
->b_ml
.ml_mfp
== NULL
)
9336 if (buf
!= NULL
&& bufIsChanged(buf
))
9338 EMSG(_(e_bufloaded
));
9347 /* When the word appears as good word we need to remove that one,
9348 * since its flags sort before the one with WF_BANNED. */
9349 fd
= mch_fopen((char *)fname
, "r");
9352 while (!vim_fgets(line
, MAXWLEN
* 2, fd
))
9355 fpos_next
= ftell(fd
);
9356 if (STRNCMP(word
, line
, len
) == 0
9357 && (line
[len
] == '/' || line
[len
] < ' '))
9359 /* Found duplicate word. Remove it by writing a '#' at
9360 * the start of the line. Mixing reading and writing
9361 * doesn't work for all systems, close the file first. */
9363 fd
= mch_fopen((char *)fname
, "r+");
9366 if (fseek(fd
, fpos
, SEEK_SET
) == 0)
9371 home_replace(NULL
, fname
, NameBuff
, MAXPATHL
, TRUE
);
9372 smsg((char_u
*)_("Word removed from %s"), NameBuff
);
9375 fseek(fd
, fpos_next
, SEEK_SET
);
9384 fd
= mch_fopen((char *)fname
, "a");
9385 if (fd
== NULL
&& new_spf
)
9389 /* We just initialized the 'spellfile' option and can't open the
9390 * file. We may need to create the "spell" directory first. We
9391 * already checked the runtime directory is writable in
9392 * init_spellfile(). */
9393 if (!dir_of_file_exists(fname
) && (p
= gettail_sep(fname
)) != fname
)
9397 /* The directory doesn't exist. Try creating it and opening
9398 * the file again. */
9400 vim_mkdir(fname
, 0755);
9402 fd
= mch_fopen((char *)fname
, "a");
9407 EMSG2(_(e_notopen
), fname
);
9411 fprintf(fd
, "%.*s/!\n", len
, word
);
9413 fprintf(fd
, "%.*s\n", len
, word
);
9416 home_replace(NULL
, fname
, NameBuff
, MAXPATHL
, TRUE
);
9417 smsg((char_u
*)_("Word added to %s"), NameBuff
);
9423 /* Update the .add.spl file. */
9424 mkspell(1, &fname
, FALSE
, TRUE
, TRUE
);
9426 /* If the .add file is edited somewhere, reload it. */
9428 buf_reload(buf
, buf
->b_orig_mode
);
9430 redraw_all_later(SOME_VALID
);
9435 * Initialize 'spellfile' for the current buffer.
9440 char_u buf
[MAXPATHL
];
9446 char_u
*lstart
= curbuf
->b_p_spl
;
9448 if (*curbuf
->b_p_spl
!= NUL
&& curbuf
->b_langp
.ga_len
> 0)
9450 /* Find the end of the language name. Exclude the region. If there
9451 * is a path separator remember the start of the tail. */
9452 for (lend
= curbuf
->b_p_spl
; *lend
!= NUL
9453 && vim_strchr((char_u
*)",._", *lend
) == NULL
; ++lend
)
9454 if (vim_ispathsep(*lend
))
9460 /* Loop over all entries in 'runtimepath'. Use the first one where we
9461 * are allowed to write. */
9466 /* Use directory of an entry with path, e.g., for
9467 * "/dir/lg.utf-8.spl" use "/dir". */
9468 vim_strncpy(buf
, curbuf
->b_p_spl
, lstart
- curbuf
->b_p_spl
- 1);
9470 /* Copy the path from 'runtimepath' to buf[]. */
9471 copy_option_part(&rtp
, buf
, MAXPATHL
, ",");
9472 if (filewritable(buf
) == 2)
9474 /* Use the first language name from 'spelllang' and the
9475 * encoding used in the first loaded .spl file. */
9477 vim_strncpy(buf
, curbuf
->b_p_spl
, lend
- curbuf
->b_p_spl
);
9480 /* Create the "spell" directory if it doesn't exist yet. */
9481 l
= (int)STRLEN(buf
);
9482 vim_snprintf((char *)buf
+ l
, MAXPATHL
- l
, "/spell");
9483 if (!filewritable(buf
) != 2)
9484 vim_mkdir(buf
, 0755);
9486 l
= (int)STRLEN(buf
);
9487 vim_snprintf((char *)buf
+ l
, MAXPATHL
- l
,
9488 "/%.*s", (int)(lend
- lstart
), lstart
);
9490 l
= (int)STRLEN(buf
);
9491 fname
= LANGP_ENTRY(curbuf
->b_langp
, 0)->lp_slang
->sl_fname
;
9492 vim_snprintf((char *)buf
+ l
, MAXPATHL
- l
, ".%s.add",
9494 && strstr((char *)gettail(fname
), ".ascii.") != NULL
9495 ? (char_u
*)"ascii" : spell_enc());
9496 set_option_value((char_u
*)"spellfile", 0L, buf
, OPT_LOCAL
);
9506 * Init the chartab used for spelling for ASCII.
9507 * EBCDIC is not supported!
9510 clear_spell_chartab(sp
)
9515 /* Init everything to FALSE. */
9516 vim_memset(sp
->st_isw
, FALSE
, sizeof(sp
->st_isw
));
9517 vim_memset(sp
->st_isu
, FALSE
, sizeof(sp
->st_isu
));
9518 for (i
= 0; i
< 256; ++i
)
9521 sp
->st_upper
[i
] = i
;
9524 /* We include digits. A word shouldn't start with a digit, but handling
9525 * that is done separately. */
9526 for (i
= '0'; i
<= '9'; ++i
)
9527 sp
->st_isw
[i
] = TRUE
;
9528 for (i
= 'A'; i
<= 'Z'; ++i
)
9530 sp
->st_isw
[i
] = TRUE
;
9531 sp
->st_isu
[i
] = TRUE
;
9532 sp
->st_fold
[i
] = i
+ 0x20;
9534 for (i
= 'a'; i
<= 'z'; ++i
)
9536 sp
->st_isw
[i
] = TRUE
;
9537 sp
->st_upper
[i
] = i
- 0x20;
9542 * Init the chartab used for spelling. Only depends on 'encoding'.
9543 * Called once while starting up and when 'encoding' changes.
9544 * The default is to use isalpha(), but the spell file should define the word
9545 * characters to make it possible that 'encoding' differs from the current
9546 * locale. For utf-8 we don't use isalpha() but our own functions.
9549 init_spell_chartab()
9553 did_set_spelltab
= FALSE
;
9554 clear_spell_chartab(&spelltab
);
9558 /* DBCS: assume double-wide characters are word characters. */
9559 for (i
= 128; i
<= 255; ++i
)
9560 if (MB_BYTE2LEN(i
) == 2)
9561 spelltab
.st_isw
[i
] = TRUE
;
9565 for (i
= 128; i
< 256; ++i
)
9567 spelltab
.st_isu
[i
] = utf_isupper(i
);
9568 spelltab
.st_isw
[i
] = spelltab
.st_isu
[i
] || utf_islower(i
);
9569 spelltab
.st_fold
[i
] = utf_fold(i
);
9570 spelltab
.st_upper
[i
] = utf_toupper(i
);
9576 /* Rough guess: use locale-dependent library functions. */
9577 for (i
= 128; i
< 256; ++i
)
9581 spelltab
.st_isw
[i
] = TRUE
;
9582 spelltab
.st_isu
[i
] = TRUE
;
9583 spelltab
.st_fold
[i
] = MB_TOLOWER(i
);
9585 else if (MB_ISLOWER(i
))
9587 spelltab
.st_isw
[i
] = TRUE
;
9588 spelltab
.st_upper
[i
] = MB_TOUPPER(i
);
9595 * Set the spell character tables from strings in the affix file.
9598 set_spell_chartab(fol
, low
, upp
)
9603 /* We build the new tables here first, so that we can compare with the
9606 char_u
*pf
= fol
, *pl
= low
, *pu
= upp
;
9609 clear_spell_chartab(&new_st
);
9613 if (*pl
== NUL
|| *pu
== NUL
)
9619 f
= mb_ptr2char_adv(&pf
);
9620 l
= mb_ptr2char_adv(&pl
);
9621 u
= mb_ptr2char_adv(&pu
);
9627 /* Every character that appears is a word character. */
9629 new_st
.st_isw
[f
] = TRUE
;
9631 new_st
.st_isw
[l
] = TRUE
;
9633 new_st
.st_isw
[u
] = TRUE
;
9635 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9637 if (l
< 256 && l
!= f
)
9641 EMSG(_(e_affrange
));
9644 new_st
.st_fold
[l
] = f
;
9647 /* if "UPP" and "FOL" are not the same the "UPP" char needs
9648 * case-folding, it's upper case and the "UPP" is the upper case of
9650 if (u
< 256 && u
!= f
)
9654 EMSG(_(e_affrange
));
9657 new_st
.st_fold
[u
] = f
;
9658 new_st
.st_isu
[u
] = TRUE
;
9659 new_st
.st_upper
[f
] = u
;
9663 if (*pl
!= NUL
|| *pu
!= NUL
)
9669 return set_spell_finish(&new_st
);
9673 * Set the spell character tables from strings in the .spl file.
9676 set_spell_charflags(flags
, cnt
, fol
)
9678 int cnt
; /* length of "flags" */
9681 /* We build the new tables here first, so that we can compare with the
9688 clear_spell_chartab(&new_st
);
9690 for (i
= 0; i
< 128; ++i
)
9694 new_st
.st_isw
[i
+ 128] = (flags
[i
] & CF_WORD
) != 0;
9695 new_st
.st_isu
[i
+ 128] = (flags
[i
] & CF_UPPER
) != 0;
9701 c
= mb_ptr2char_adv(&p
);
9705 new_st
.st_fold
[i
+ 128] = c
;
9706 if (i
+ 128 != c
&& new_st
.st_isu
[i
+ 128] && c
< 256)
9707 new_st
.st_upper
[c
] = i
+ 128;
9711 (void)set_spell_finish(&new_st
);
9715 set_spell_finish(new_st
)
9720 if (did_set_spelltab
)
9722 /* check that it's the same table */
9723 for (i
= 0; i
< 256; ++i
)
9725 if (spelltab
.st_isw
[i
] != new_st
->st_isw
[i
]
9726 || spelltab
.st_isu
[i
] != new_st
->st_isu
[i
]
9727 || spelltab
.st_fold
[i
] != new_st
->st_fold
[i
]
9728 || spelltab
.st_upper
[i
] != new_st
->st_upper
[i
])
9730 EMSG(_("E763: Word characters differ between spell files"));
9737 /* copy the new spelltab into the one being used */
9739 did_set_spelltab
= TRUE
;
9746 * Return TRUE if "p" points to a word character.
9747 * As a special case we see "midword" characters as word character when it is
9748 * followed by a word character. This finds they'there but not 'they there'.
9749 * Thus this only works properly when past the first character of the word.
9752 spell_iswordp(p
, buf
)
9754 buf_T
*buf
; /* buffer used */
9763 l
= MB_BYTE2LEN(*p
);
9767 /* be quick for ASCII */
9768 if (buf
->b_spell_ismw
[*p
])
9770 s
= p
+ 1; /* skip a mid-word character */
9771 l
= MB_BYTE2LEN(*s
);
9777 if (c
< 256 ? buf
->b_spell_ismw
[c
]
9778 : (buf
->b_spell_ismw_mb
!= NULL
9779 && vim_strchr(buf
->b_spell_ismw_mb
, c
) != NULL
))
9782 l
= MB_BYTE2LEN(*s
);
9788 return mb_get_class(s
) >= 2;
9789 return spelltab
.st_isw
[c
];
9793 return spelltab
.st_isw
[buf
->b_spell_ismw
[*p
] ? p
[1] : p
[0]];
9797 * Return TRUE if "p" points to a word character.
9798 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9801 spell_iswordp_nmw(p
)
9811 return mb_get_class(p
) >= 2;
9812 return spelltab
.st_isw
[c
];
9815 return spelltab
.st_isw
[*p
];
9820 * Return TRUE if "p" points to a word character.
9821 * Wide version of spell_iswordp().
9824 spell_iswordp_w(p
, buf
)
9830 if (*p
< 256 ? buf
->b_spell_ismw
[*p
]
9831 : (buf
->b_spell_ismw_mb
!= NULL
9832 && vim_strchr(buf
->b_spell_ismw_mb
, *p
) != NULL
))
9840 return utf_class(*s
) >= 2;
9842 return dbcs_class((unsigned)*s
>> 8, *s
& 0xff) >= 2;
9845 return spelltab
.st_isw
[*s
];
9850 * Write the table with prefix conditions to the .spl file.
9851 * When "fd" is NULL only count the length of what is written.
9854 write_spell_prefcond(fd
, gap
)
9864 put_bytes(fd
, (long_u
)gap
->ga_len
, 2); /* <prefcondcnt> */
9866 totlen
= 2 + gap
->ga_len
; /* length of <prefcondcnt> and <condlen> bytes */
9868 for (i
= 0; i
< gap
->ga_len
; ++i
)
9870 /* <prefcond> : <condlen> <condstr> */
9871 p
= ((char_u
**)gap
->ga_data
)[i
];
9874 len
= (int)STRLEN(p
);
9878 fwrite(p
, (size_t)len
, (size_t)1, fd
);
9882 else if (fd
!= NULL
)
9890 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9891 * Uses the character definitions from the .spl file.
9892 * When using a multi-byte 'encoding' the length may change!
9893 * Returns FAIL when something wrong.
9896 spell_casefold(str
, len
, buf
, buflen
)
9907 return FAIL
; /* result will not fit */
9917 /* Fold one character at a time. */
9918 for (p
= str
; p
< str
+ len
; )
9920 if (outi
+ MB_MAXBYTES
> buflen
)
9925 c
= mb_cptr2char_adv(&p
);
9926 outi
+= mb_char2bytes(SPELL_TOFOLD(c
), buf
+ outi
);
9933 /* Be quick for non-multibyte encodings. */
9934 for (i
= 0; i
< len
; ++i
)
9935 buf
[i
] = spelltab
.st_fold
[str
[i
]];
9942 /* values for sps_flags */
9945 #define SPS_DOUBLE 4
9947 static int sps_flags
= SPS_BEST
; /* flags from 'spellsuggest' */
9948 static int sps_limit
= 9999; /* max nr of suggestions given */
9951 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
9952 * Sets "sps_flags" and "sps_limit".
9959 char_u buf
[MAXPATHL
];
9965 for (p
= p_sps
; *p
!= NUL
; )
9967 copy_option_part(&p
, buf
, MAXPATHL
, ",");
9970 if (VIM_ISDIGIT(*buf
))
9973 sps_limit
= getdigits(&s
);
9974 if (*s
!= NUL
&& !VIM_ISDIGIT(*s
))
9977 else if (STRCMP(buf
, "best") == 0)
9979 else if (STRCMP(buf
, "fast") == 0)
9981 else if (STRCMP(buf
, "double") == 0)
9983 else if (STRNCMP(buf
, "expr:", 5) != 0
9984 && STRNCMP(buf
, "file:", 5) != 0)
9987 if (f
== -1 || (sps_flags
!= 0 && f
!= 0))
9989 sps_flags
= SPS_BEST
;
9998 sps_flags
= SPS_BEST
;
10004 * "z?": Find badly spelled word under or after the cursor.
10005 * Give suggestions for the properly spelled word.
10006 * In Visual mode use the highlighted word as the bad word.
10007 * When "count" is non-zero use that suggestion.
10010 spell_suggest(count
)
10014 pos_T prev_cursor
= curwin
->w_cursor
;
10015 char_u wcopy
[MAXWLEN
+ 2];
10024 int selected
= count
;
10027 if (no_spell_checking(curwin
))
10033 /* Use the Visually selected text as the bad word. But reject
10034 * a multi-line selection. */
10035 if (curwin
->w_cursor
.lnum
!= VIsual
.lnum
)
10040 badlen
= (int)curwin
->w_cursor
.col
- (int)VIsual
.col
;
10044 curwin
->w_cursor
.col
= VIsual
.col
;
10050 /* Find the start of the badly spelled word. */
10051 if (spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, NULL
) == 0
10052 || curwin
->w_cursor
.col
> prev_cursor
.col
)
10054 /* No bad word or it starts after the cursor: use the word under the
10056 curwin
->w_cursor
= prev_cursor
;
10057 line
= ml_get_curline();
10058 p
= line
+ curwin
->w_cursor
.col
;
10059 /* Backup to before start of word. */
10060 while (p
> line
&& spell_iswordp_nmw(p
))
10061 mb_ptr_back(line
, p
);
10062 /* Forward to start of word. */
10063 while (*p
!= NUL
&& !spell_iswordp_nmw(p
))
10066 if (!spell_iswordp_nmw(p
)) /* No word found. */
10071 curwin
->w_cursor
.col
= (colnr_T
)(p
- line
);
10074 /* Get the word and its length. */
10076 /* Figure out if the word should be capitalised. */
10077 need_cap
= check_need_cap(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
);
10079 line
= ml_get_curline();
10081 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10082 * 'spellsuggest', whatever is smaller. */
10083 if (sps_limit
> (int)Rows
- 2)
10084 limit
= (int)Rows
- 2;
10087 spell_find_suggest(line
+ curwin
->w_cursor
.col
, badlen
, &sug
, limit
,
10088 TRUE
, need_cap
, TRUE
);
10090 if (sug
.su_ga
.ga_len
== 0)
10091 MSG(_("Sorry, no suggestions"));
10092 else if (count
> 0)
10094 if (count
> sug
.su_ga
.ga_len
)
10095 smsg((char_u
*)_("Sorry, only %ld suggestions"),
10096 (long)sug
.su_ga
.ga_len
);
10100 vim_free(repl_from
);
10105 #ifdef FEAT_RIGHTLEFT
10106 /* When 'rightleft' is set the list is drawn right-left. */
10107 cmdmsg_rl
= curwin
->w_p_rl
;
10109 msg_col
= Columns
- 1;
10112 /* List the suggestions. */
10114 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
10115 lines_left
= Rows
; /* avoid more prompt */
10116 vim_snprintf((char *)IObuff
, IOSIZE
, _("Change \"%.*s\" to:"),
10117 sug
.su_badlen
, sug
.su_badptr
);
10118 #ifdef FEAT_RIGHTLEFT
10119 if (cmdmsg_rl
&& STRNCMP(IObuff
, "Change", 6) == 0)
10121 /* And now the rabbit from the high hat: Avoid showing the
10122 * untranslated message rightleft. */
10123 vim_snprintf((char *)IObuff
, IOSIZE
, ":ot \"%.*s\" egnahC",
10124 sug
.su_badlen
, sug
.su_badptr
);
10132 for (i
= 0; i
< sug
.su_ga
.ga_len
; ++i
)
10134 stp
= &SUG(sug
.su_ga
, i
);
10136 /* The suggested word may replace only part of the bad word, add
10137 * the not replaced part. */
10138 STRCPY(wcopy
, stp
->st_word
);
10139 if (sug
.su_badlen
> stp
->st_orglen
)
10140 vim_strncpy(wcopy
+ stp
->st_wordlen
,
10141 sug
.su_badptr
+ stp
->st_orglen
,
10142 sug
.su_badlen
- stp
->st_orglen
);
10143 vim_snprintf((char *)IObuff
, IOSIZE
, "%2d", i
+ 1);
10144 #ifdef FEAT_RIGHTLEFT
10150 vim_snprintf((char *)IObuff
, IOSIZE
, " \"%s\"", wcopy
);
10153 /* The word may replace more than "su_badlen". */
10154 if (sug
.su_badlen
< stp
->st_orglen
)
10156 vim_snprintf((char *)IObuff
, IOSIZE
, _(" < \"%.*s\""),
10157 stp
->st_orglen
, sug
.su_badptr
);
10163 /* Add the score. */
10164 if (sps_flags
& (SPS_DOUBLE
| SPS_BEST
))
10165 vim_snprintf((char *)IObuff
, IOSIZE
, " (%s%d - %d)",
10166 stp
->st_salscore
? "s " : "",
10167 stp
->st_score
, stp
->st_altscore
);
10169 vim_snprintf((char *)IObuff
, IOSIZE
, " (%d)",
10171 #ifdef FEAT_RIGHTLEFT
10173 /* Mirror the numbers, but keep the leading space. */
10174 rl_mirror(IObuff
+ 1);
10182 #ifdef FEAT_RIGHTLEFT
10186 /* Ask for choice. */
10187 selected
= prompt_for_number(&mouse_used
);
10189 selected
-= lines_left
;
10190 lines_left
= Rows
; /* avoid more prompt */
10193 if (selected
> 0 && selected
<= sug
.su_ga
.ga_len
&& u_save_cursor() == OK
)
10195 /* Save the from and to text for :spellrepall. */
10196 stp
= &SUG(sug
.su_ga
, selected
- 1);
10197 if (sug
.su_badlen
> stp
->st_orglen
)
10199 /* Replacing less than "su_badlen", append the remainder to
10201 repl_from
= vim_strnsave(sug
.su_badptr
, sug
.su_badlen
);
10202 vim_snprintf((char *)IObuff
, IOSIZE
, "%s%.*s", stp
->st_word
,
10203 sug
.su_badlen
- stp
->st_orglen
,
10204 sug
.su_badptr
+ stp
->st_orglen
);
10205 repl_to
= vim_strsave(IObuff
);
10209 /* Replacing su_badlen or more, use the whole word. */
10210 repl_from
= vim_strnsave(sug
.su_badptr
, stp
->st_orglen
);
10211 repl_to
= vim_strsave(stp
->st_word
);
10214 /* Replace the word. */
10215 p
= alloc((unsigned)STRLEN(line
) - stp
->st_orglen
+ stp
->st_wordlen
+ 1);
10218 c
= (int)(sug
.su_badptr
- line
);
10219 mch_memmove(p
, line
, c
);
10220 STRCPY(p
+ c
, stp
->st_word
);
10221 STRCAT(p
, sug
.su_badptr
+ stp
->st_orglen
);
10222 ml_replace(curwin
->w_cursor
.lnum
, p
, FALSE
);
10223 curwin
->w_cursor
.col
= c
;
10225 /* For redo we use a change-word command. */
10227 AppendToRedobuff((char_u
*)"ciw");
10228 AppendToRedobuffLit(p
+ c
,
10229 stp
->st_wordlen
+ sug
.su_badlen
- stp
->st_orglen
);
10230 AppendCharToRedobuff(ESC
);
10232 /* After this "p" may be invalid. */
10233 changed_bytes(curwin
->w_cursor
.lnum
, c
);
10237 curwin
->w_cursor
= prev_cursor
;
10239 spell_find_cleanup(&sug
);
10243 * Check if the word at line "lnum" column "col" is required to start with a
10244 * capital. This uses 'spellcapcheck' of the current buffer.
10247 check_need_cap(lnum
, col
)
10251 int need_cap
= FALSE
;
10253 char_u
*line_copy
= NULL
;
10256 regmatch_T regmatch
;
10258 if (curbuf
->b_cap_prog
== NULL
)
10261 line
= ml_get_curline();
10263 if ((int)(skipwhite(line
) - line
) >= (int)col
)
10265 /* At start of line, check if previous line is empty or sentence
10271 line
= ml_get(lnum
- 1);
10272 if (*skipwhite(line
) == NUL
)
10276 /* Append a space in place of the line break. */
10277 line_copy
= concat_str(line
, (char_u
*)" ");
10279 endcol
= (colnr_T
)STRLEN(line
);
10288 /* Check if sentence ends before the bad word. */
10289 regmatch
.regprog
= curbuf
->b_cap_prog
;
10290 regmatch
.rm_ic
= FALSE
;
10294 mb_ptr_back(line
, p
);
10295 if (p
== line
|| spell_iswordp_nmw(p
))
10297 if (vim_regexec(®match
, p
, 0)
10298 && regmatch
.endp
[0] == line
+ endcol
)
10306 vim_free(line_copy
);
10317 ex_spellrepall(eap
)
10320 pos_T pos
= curwin
->w_cursor
;
10325 int save_ws
= p_ws
;
10326 linenr_T prev_lnum
= 0;
10328 if (repl_from
== NULL
|| repl_to
== NULL
)
10330 EMSG(_("E752: No previous spell replacement"));
10333 addlen
= (int)(STRLEN(repl_to
) - STRLEN(repl_from
));
10335 frompat
= alloc((unsigned)STRLEN(repl_from
) + 7);
10336 if (frompat
== NULL
)
10338 sprintf((char *)frompat
, "\\V\\<%s\\>", repl_from
);
10343 curwin
->w_cursor
.lnum
= 0;
10346 if (do_search(NULL
, '/', frompat
, 1L, SEARCH_KEEP
) == 0
10347 || u_save_cursor() == FAIL
)
10350 /* Only replace when the right word isn't there yet. This happens
10351 * when changing "etc" to "etc.". */
10352 line
= ml_get_curline();
10353 if (addlen
<= 0 || STRNCMP(line
+ curwin
->w_cursor
.col
,
10354 repl_to
, STRLEN(repl_to
)) != 0)
10356 p
= alloc((unsigned)STRLEN(line
) + addlen
+ 1);
10359 mch_memmove(p
, line
, curwin
->w_cursor
.col
);
10360 STRCPY(p
+ curwin
->w_cursor
.col
, repl_to
);
10361 STRCAT(p
, line
+ curwin
->w_cursor
.col
+ STRLEN(repl_from
));
10362 ml_replace(curwin
->w_cursor
.lnum
, p
, FALSE
);
10363 changed_bytes(curwin
->w_cursor
.lnum
, curwin
->w_cursor
.col
);
10365 if (curwin
->w_cursor
.lnum
!= prev_lnum
)
10368 prev_lnum
= curwin
->w_cursor
.lnum
;
10372 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(repl_to
);
10376 curwin
->w_cursor
= pos
;
10379 if (sub_nsubs
== 0)
10380 EMSG2(_("E753: Not found: %s"), repl_from
);
10386 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10387 * a list of allocated strings.
10390 spell_suggest_list(gap
, word
, maxcount
, need_cap
, interactive
)
10393 int maxcount
; /* maximum nr of suggestions */
10394 int need_cap
; /* 'spellcapcheck' matched */
10402 spell_find_suggest(word
, 0, &sug
, maxcount
, FALSE
, need_cap
, interactive
);
10404 /* Make room in "gap". */
10405 ga_init2(gap
, sizeof(char_u
*), sug
.su_ga
.ga_len
+ 1);
10406 if (ga_grow(gap
, sug
.su_ga
.ga_len
) == OK
)
10408 for (i
= 0; i
< sug
.su_ga
.ga_len
; ++i
)
10410 stp
= &SUG(sug
.su_ga
, i
);
10412 /* The suggested word may replace only part of "word", add the not
10413 * replaced part. */
10414 wcopy
= alloc(stp
->st_wordlen
10415 + (unsigned)STRLEN(sug
.su_badptr
+ stp
->st_orglen
) + 1);
10418 STRCPY(wcopy
, stp
->st_word
);
10419 STRCPY(wcopy
+ stp
->st_wordlen
, sug
.su_badptr
+ stp
->st_orglen
);
10420 ((char_u
**)gap
->ga_data
)[gap
->ga_len
++] = wcopy
;
10424 spell_find_cleanup(&sug
);
10428 * Find spell suggestions for the word at the start of "badptr".
10429 * Return the suggestions in "su->su_ga".
10430 * The maximum number of suggestions is "maxcount".
10431 * Note: does use info for the current window.
10432 * This is based on the mechanisms of Aspell, but completely reimplemented.
10435 spell_find_suggest(badptr
, badlen
, su
, maxcount
, banbadword
, need_cap
, interactive
)
10437 int badlen
; /* length of bad word or 0 if unknown */
10440 int banbadword
; /* don't include badword in suggestions */
10441 int need_cap
; /* word should start with capital */
10444 hlf_T attr
= HLF_COUNT
;
10445 char_u buf
[MAXPATHL
];
10447 int do_combine
= FALSE
;
10450 static int expr_busy
= FALSE
;
10457 * Set the info in "*su".
10459 vim_memset(su
, 0, sizeof(suginfo_T
));
10460 ga_init2(&su
->su_ga
, (int)sizeof(suggest_T
), 10);
10461 ga_init2(&su
->su_sga
, (int)sizeof(suggest_T
), 10);
10462 if (*badptr
== NUL
)
10464 hash_init(&su
->su_banned
);
10466 su
->su_badptr
= badptr
;
10468 su
->su_badlen
= badlen
;
10470 su
->su_badlen
= spell_check(curwin
, su
->su_badptr
, &attr
, NULL
, FALSE
);
10471 su
->su_maxcount
= maxcount
;
10472 su
->su_maxscore
= SCORE_MAXINIT
;
10474 if (su
->su_badlen
>= MAXWLEN
)
10475 su
->su_badlen
= MAXWLEN
- 1; /* just in case */
10476 vim_strncpy(su
->su_badword
, su
->su_badptr
, su
->su_badlen
);
10477 (void)spell_casefold(su
->su_badptr
, su
->su_badlen
,
10478 su
->su_fbadword
, MAXWLEN
);
10479 /* get caps flags for bad word */
10480 su
->su_badflags
= badword_captype(su
->su_badptr
,
10481 su
->su_badptr
+ su
->su_badlen
);
10483 su
->su_badflags
|= WF_ONECAP
;
10485 /* Find the default language for sound folding. We simply use the first
10486 * one in 'spelllang' that supports sound folding. That's good for when
10487 * using multiple files for one language, it's not that bad when mixing
10488 * languages (e.g., "pl,en"). */
10489 for (i
= 0; i
< curbuf
->b_langp
.ga_len
; ++i
)
10491 lp
= LANGP_ENTRY(curbuf
->b_langp
, i
);
10492 if (lp
->lp_sallang
!= NULL
)
10494 su
->su_sallang
= lp
->lp_sallang
;
10499 /* Soundfold the bad word with the default sound folding, so that we don't
10500 * have to do this many times. */
10501 if (su
->su_sallang
!= NULL
)
10502 spell_soundfold(su
->su_sallang
, su
->su_fbadword
, TRUE
,
10503 su
->su_sal_badword
);
10505 /* If the word is not capitalised and spell_check() doesn't consider the
10506 * word to be bad then it might need to be capitalised. Add a suggestion
10508 c
= PTR2CHAR(su
->su_badptr
);
10509 if (!SPELL_ISUPPER(c
) && attr
== HLF_COUNT
)
10511 make_case_word(su
->su_badword
, buf
, WF_ONECAP
);
10512 add_suggestion(su
, &su
->su_ga
, buf
, su
->su_badlen
, SCORE_ICASE
,
10513 0, TRUE
, su
->su_sallang
, FALSE
);
10516 /* Ban the bad word itself. It may appear in another region. */
10518 add_banned(su
, su
->su_badword
);
10520 /* Make a copy of 'spellsuggest', because the expression may change it. */
10521 sps_copy
= vim_strsave(p_sps
);
10522 if (sps_copy
== NULL
)
10525 /* Loop over the items in 'spellsuggest'. */
10526 for (p
= sps_copy
; *p
!= NUL
; )
10528 copy_option_part(&p
, buf
, MAXPATHL
, ",");
10530 if (STRNCMP(buf
, "expr:", 5) == 0)
10533 /* Evaluate an expression. Skip this when called recursively,
10534 * when using spellsuggest() in the expression. */
10538 spell_suggest_expr(su
, buf
+ 5);
10543 else if (STRNCMP(buf
, "file:", 5) == 0)
10544 /* Use list of suggestions in a file. */
10545 spell_suggest_file(su
, buf
+ 5);
10548 /* Use internal method. */
10549 spell_suggest_intern(su
, interactive
);
10550 if (sps_flags
& SPS_DOUBLE
)
10555 vim_free(sps_copy
);
10558 /* Combine the two list of suggestions. This must be done last,
10559 * because sorting changes the order again. */
10565 * Find suggestions by evaluating expression "expr".
10568 spell_suggest_expr(su
, expr
)
10577 /* The work is split up in a few parts to avoid having to export
10579 * First evaluate the expression and get the resulting list. */
10580 list
= eval_spell_expr(su
->su_badword
, expr
);
10583 /* Loop over the items in the list. */
10584 for (li
= list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
10585 if (li
->li_tv
.v_type
== VAR_LIST
)
10587 /* Get the word and the score from the items. */
10588 score
= get_spellword(li
->li_tv
.vval
.v_list
, &p
);
10589 if (score
>= 0 && score
<= su
->su_maxscore
)
10590 add_suggestion(su
, &su
->su_ga
, p
, su
->su_badlen
,
10591 score
, 0, TRUE
, su
->su_sallang
, FALSE
);
10596 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10597 check_suggestions(su
, &su
->su_ga
);
10598 (void)cleanup_suggestions(&su
->su_ga
, su
->su_maxscore
, su
->su_maxcount
);
10603 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
10606 spell_suggest_file(su
, fname
)
10611 char_u line
[MAXWLEN
* 2];
10614 char_u cword
[MAXWLEN
];
10616 /* Open the file. */
10617 fd
= mch_fopen((char *)fname
, "r");
10620 EMSG2(_(e_notopen
), fname
);
10624 /* Read it line by line. */
10625 while (!vim_fgets(line
, MAXWLEN
* 2, fd
) && !got_int
)
10629 p
= vim_strchr(line
, '/');
10631 continue; /* No Tab found, just skip the line. */
10633 if (STRICMP(su
->su_badword
, line
) == 0)
10635 /* Match! Isolate the good word, until CR or NL. */
10636 for (len
= 0; p
[len
] >= ' '; ++len
)
10640 /* If the suggestion doesn't have specific case duplicate the case
10641 * of the bad word. */
10642 if (captype(p
, NULL
) == 0)
10644 make_case_word(p
, cword
, su
->su_badflags
);
10648 add_suggestion(su
, &su
->su_ga
, p
, su
->su_badlen
,
10649 SCORE_FILE
, 0, TRUE
, su
->su_sallang
, FALSE
);
10655 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10656 check_suggestions(su
, &su
->su_ga
);
10657 (void)cleanup_suggestions(&su
->su_ga
, su
->su_maxscore
, su
->su_maxcount
);
10661 * Find suggestions for the internal method indicated by "sps_flags".
10664 spell_suggest_intern(su
, interactive
)
10669 * Load the .sug file(s) that are available and not done yet.
10671 suggest_load_files();
10674 * 1. Try special cases, such as repeating a word: "the the" -> "the".
10676 * Set a maximum score to limit the combination of operations that is
10679 suggest_try_special(su
);
10682 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10683 * from the .aff file and inserting a space (split the word).
10685 suggest_try_change(su
);
10687 /* For the resulting top-scorers compute the sound-a-like score. */
10688 if (sps_flags
& SPS_DOUBLE
)
10689 score_comp_sal(su
);
10692 * 3. Try finding sound-a-like words.
10694 if ((sps_flags
& SPS_FAST
) == 0)
10696 if (sps_flags
& SPS_BEST
)
10697 /* Adjust the word score for the suggestions found so far for how
10698 * they sounds like. */
10699 rescore_suggestions(su
);
10702 * While going throught the soundfold tree "su_maxscore" is the score
10703 * for the soundfold word, limits the changes that are being tried,
10704 * and "su_sfmaxscore" the rescored score, which is set by
10705 * cleanup_suggestions().
10706 * First find words with a small edit distance, because this is much
10707 * faster and often already finds the top-N suggestions. If we didn't
10708 * find many suggestions try again with a higher edit distance.
10709 * "sl_sounddone" is used to avoid doing the same word twice.
10711 suggest_try_soundalike_prep();
10712 su
->su_maxscore
= SCORE_SFMAX1
;
10713 su
->su_sfmaxscore
= SCORE_MAXINIT
* 3;
10714 suggest_try_soundalike(su
);
10715 if (su
->su_ga
.ga_len
< SUG_CLEAN_COUNT(su
))
10717 /* We didn't find enough matches, try again, allowing more
10718 * changes to the soundfold word. */
10719 su
->su_maxscore
= SCORE_SFMAX2
;
10720 suggest_try_soundalike(su
);
10721 if (su
->su_ga
.ga_len
< SUG_CLEAN_COUNT(su
))
10723 /* Still didn't find enough matches, try again, allowing even
10724 * more changes to the soundfold word. */
10725 su
->su_maxscore
= SCORE_SFMAX3
;
10726 suggest_try_soundalike(su
);
10729 su
->su_maxscore
= su
->su_sfmaxscore
;
10730 suggest_try_soundalike_finish();
10733 /* When CTRL-C was hit while searching do show the results. Only clear
10734 * got_int when using a command, not for spellsuggest(). */
10736 if (interactive
&& got_int
)
10742 if ((sps_flags
& SPS_DOUBLE
) == 0 && su
->su_ga
.ga_len
!= 0)
10744 if (sps_flags
& SPS_BEST
)
10745 /* Adjust the word score for how it sounds like. */
10746 rescore_suggestions(su
);
10748 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10749 check_suggestions(su
, &su
->su_ga
);
10750 (void)cleanup_suggestions(&su
->su_ga
, su
->su_maxscore
, su
->su_maxcount
);
10755 * Load the .sug files for languages that have one and weren't loaded yet.
10758 suggest_load_files()
10765 char_u buf
[MAXWLEN
];
10773 /* Do this for all languages that support sound folding. */
10774 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
10776 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
10777 slang
= lp
->lp_slang
;
10778 if (slang
->sl_sugtime
!= 0 && !slang
->sl_sugloaded
)
10780 /* Change ".spl" to ".sug" and open the file. When the file isn't
10781 * found silently skip it. Do set "sl_sugloaded" so that we
10782 * don't try again and again. */
10783 slang
->sl_sugloaded
= TRUE
;
10785 dotp
= vim_strrchr(slang
->sl_fname
, '.');
10786 if (dotp
== NULL
|| fnamecmp(dotp
, ".spl") != 0)
10788 STRCPY(dotp
, ".sug");
10789 fd
= mch_fopen((char *)slang
->sl_fname
, "r");
10794 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10796 for (i
= 0; i
< VIMSUGMAGICL
; ++i
)
10797 buf
[i
] = getc(fd
); /* <fileID> */
10798 if (STRNCMP(buf
, VIMSUGMAGIC
, VIMSUGMAGICL
) != 0)
10800 EMSG2(_("E778: This does not look like a .sug file: %s"),
10804 c
= getc(fd
); /* <versionnr> */
10805 if (c
< VIMSUGVERSION
)
10807 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
10811 else if (c
> VIMSUGVERSION
)
10813 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
10818 /* Check the timestamp, it must be exactly the same as the one in
10819 * the .spl file. Otherwise the word numbers won't match. */
10820 timestamp
= get8c(fd
); /* <timestamp> */
10821 if (timestamp
!= slang
->sl_sugtime
)
10823 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
10829 * <SUGWORDTREE>: <wordtree>
10830 * Read the trie with the soundfolded words.
10832 if (spell_read_tree(fd
, &slang
->sl_sbyts
, &slang
->sl_sidxs
,
10836 EMSG2(_("E782: error while reading .sug file: %s"),
10838 slang_clear_sug(slang
);
10843 * <SUGTABLE>: <sugwcount> <sugline> ...
10845 * Read the table with word numbers. We use a file buffer for
10846 * this, because it's so much like a file with lines. Makes it
10847 * possible to swap the info and save on memory use.
10849 slang
->sl_sugbuf
= open_spellbuf();
10850 if (slang
->sl_sugbuf
== NULL
)
10853 wcount
= get4c(fd
);
10857 /* Read all the wordnr lists into the buffer, one NUL terminated
10858 * list per line. */
10859 ga_init2(&ga
, 1, 100);
10860 for (wordnr
= 0; wordnr
< wcount
; ++wordnr
)
10865 c
= getc(fd
); /* <sugline> */
10866 if (c
< 0 || ga_grow(&ga
, 1) == FAIL
)
10868 ((char_u
*)ga
.ga_data
)[ga
.ga_len
++] = c
;
10872 if (ml_append_buf(slang
->sl_sugbuf
, (linenr_T
)wordnr
,
10873 ga
.ga_data
, ga
.ga_len
, TRUE
) == FAIL
)
10879 * Need to put word counts in the word tries, so that we can find
10880 * a word by its number.
10882 tree_count_words(slang
->sl_fbyts
, slang
->sl_fidxs
);
10883 tree_count_words(slang
->sl_sbyts
, slang
->sl_sidxs
);
10888 STRCPY(dotp
, ".spl");
10895 * Fill in the wordcount fields for a trie.
10896 * Returns the total number of words.
10899 tree_count_words(byts
, idxs
)
10904 idx_T arridx
[MAXWLEN
];
10908 int wordcount
[MAXWLEN
];
10914 while (depth
>= 0 && !got_int
)
10916 if (curi
[depth
] > byts
[arridx
[depth
]])
10918 /* Done all bytes at this node, go up one level. */
10919 idxs
[arridx
[depth
]] = wordcount
[depth
];
10921 wordcount
[depth
- 1] += wordcount
[depth
];
10928 /* Do one more byte at this node. */
10929 n
= arridx
[depth
] + curi
[depth
];
10935 /* End of word, count it. */
10936 ++wordcount
[depth
];
10938 /* Skip over any other NUL bytes (same word with different
10940 while (byts
[n
+ 1] == 0)
10948 /* Normal char, go one level deeper to count the words. */
10950 arridx
[depth
] = idxs
[n
];
10952 wordcount
[depth
] = 0;
10959 * Free the info put in "*su" by spell_find_suggest().
10962 spell_find_cleanup(su
)
10967 /* Free the suggestions. */
10968 for (i
= 0; i
< su
->su_ga
.ga_len
; ++i
)
10969 vim_free(SUG(su
->su_ga
, i
).st_word
);
10970 ga_clear(&su
->su_ga
);
10971 for (i
= 0; i
< su
->su_sga
.ga_len
; ++i
)
10972 vim_free(SUG(su
->su_sga
, i
).st_word
);
10973 ga_clear(&su
->su_sga
);
10975 /* Free the banned words. */
10976 hash_clear_all(&su
->su_banned
, 0);
10980 * Make a copy of "word", with the first letter upper or lower cased, to
10981 * "wcopy[MAXWLEN]". "word" must not be empty.
10982 * The result is NUL terminated.
10985 onecap_copy(word
, wcopy
, upper
)
10988 int upper
; /* TRUE: first letter made upper case */
10997 c
= mb_cptr2char_adv(&p
);
11002 c
= SPELL_TOUPPER(c
);
11004 c
= SPELL_TOFOLD(c
);
11007 l
= mb_char2bytes(c
, wcopy
);
11014 vim_strncpy(wcopy
+ l
, p
, MAXWLEN
- l
- 1);
11018 * Make a copy of "word" with all the letters upper cased into
11019 * "wcopy[MAXWLEN]". The result is NUL terminated.
11022 allcap_copy(word
, wcopy
)
11031 for (s
= word
; *s
!= NUL
; )
11035 c
= mb_cptr2char_adv(&s
);
11041 /* We only change ß to SS when we are certain latin1 is used. It
11042 * would cause weird errors in other 8-bit encodings. */
11043 if (enc_latin1like
&& c
== 0xdf)
11046 if (d
- wcopy
>= MAXWLEN
- 1)
11052 c
= SPELL_TOUPPER(c
);
11057 if (d
- wcopy
>= MAXWLEN
- MB_MAXBYTES
)
11059 d
+= mb_char2bytes(c
, d
);
11064 if (d
- wcopy
>= MAXWLEN
- 1)
11073 * Try finding suggestions by recognizing specific situations.
11076 suggest_try_special(su
)
11082 char_u word
[MAXWLEN
];
11085 * Recognize a word that is repeated: "the the".
11087 p
= skiptowhite(su
->su_fbadword
);
11088 len
= p
- su
->su_fbadword
;
11090 if (STRLEN(p
) == len
&& STRNCMP(su
->su_fbadword
, p
, len
) == 0)
11092 /* Include badflags: if the badword is onecap or allcap
11093 * use that for the goodword too: "The the" -> "The". */
11094 c
= su
->su_fbadword
[len
];
11095 su
->su_fbadword
[len
] = NUL
;
11096 make_case_word(su
->su_fbadword
, word
, su
->su_badflags
);
11097 su
->su_fbadword
[len
] = c
;
11099 /* Give a soundalike score of 0, compute the score as if deleting one
11101 add_suggestion(su
, &su
->su_ga
, word
, su
->su_badlen
,
11102 RESCORE(SCORE_REP
, 0), 0, TRUE
, su
->su_sallang
, FALSE
);
11107 * Try finding suggestions by adding/removing/swapping letters.
11110 suggest_try_change(su
)
11113 char_u fword
[MAXWLEN
]; /* copy of the bad word, case-folded */
11119 /* We make a copy of the case-folded bad word, so that we can modify it
11120 * to find matches (esp. REP items). Append some more text, changing
11121 * chars after the bad word may help. */
11122 STRCPY(fword
, su
->su_fbadword
);
11123 n
= (int)STRLEN(fword
);
11124 p
= su
->su_badptr
+ su
->su_badlen
;
11125 (void)spell_casefold(p
, (int)STRLEN(p
), fword
+ n
, MAXWLEN
- n
);
11127 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
11129 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
11131 /* If reloading a spell file fails it's still in the list but
11132 * everything has been cleared. */
11133 if (lp
->lp_slang
->sl_fbyts
== NULL
)
11136 /* Try it for this language. Will add possible suggestions. */
11137 suggest_trie_walk(su
, lp
, fword
, FALSE
);
11141 /* Check the maximum score, if we go over it we won't try this change. */
11142 #define TRY_DEEPER(su, stack, depth, add) \
11143 (stack[depth].ts_score + (add) < su->su_maxscore)
11146 * Try finding suggestions by adding/removing/swapping letters.
11148 * This uses a state machine. At each node in the tree we try various
11149 * operations. When trying if an operation works "depth" is increased and the
11150 * stack[] is used to store info. This allows combinations, thus insert one
11151 * character, replace one and delete another. The number of changes is
11152 * limited by su->su_maxscore.
11154 * After implementing this I noticed an article by Kemal Oflazer that
11155 * describes something similar: "Error-tolerant Finite State Recognition with
11156 * Applications to Morphological Analysis and Spelling Correction" (1996).
11157 * The implementation in the article is simplified and requires a stack of
11158 * unknown depth. The implementation here only needs a stack depth equal to
11159 * the length of the word.
11161 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11162 * The mechanism is the same, but we find a match with a sound-folded word
11163 * that comes from one or more original words. Each of these words may be
11164 * added, this is done by add_sound_suggest().
11166 * the prefix tree or the keep-case tree
11168 * anything to do with upper and lower case
11169 * anything to do with word or non-word characters ("spell_iswordp()")
11171 * word flags (rare, region, compounding)
11172 * word splitting for now
11173 * "similar_chars()"
11174 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11177 suggest_trie_walk(su
, lp
, fword
, soundfold
)
11183 char_u tword
[MAXWLEN
]; /* good word collected so far */
11184 trystate_T stack
[MAXWLEN
];
11185 char_u preword
[MAXWLEN
* 3]; /* word found with proper case;
11186 * concatanation of prefix compound
11187 * words and split word. NUL terminated
11188 * when going deeper but not when coming
11190 char_u compflags
[MAXWLEN
]; /* compound flags, one for each word */
11194 char_u
*byts
, *fbyts
, *pbyts
;
11195 idx_T
*idxs
, *fidxs
, *pidxs
;
11206 int repextra
= 0; /* extra bytes in fword[] from REP item */
11207 slang_T
*slang
= lp
->lp_slang
;
11210 #ifdef DEBUG_TRIEWALK
11211 /* Stores the name of the change made at each level. */
11212 char_u changename
[MAXWLEN
][80];
11214 int breakcheckcount
= 1000;
11218 * Go through the whole case-fold tree, try changes at each node.
11219 * "tword[]" contains the word collected from nodes in the tree.
11220 * "fword[]" the word we are trying to match with (initially the bad
11225 vim_memset(sp
, 0, sizeof(trystate_T
));
11230 /* Going through the soundfold tree. */
11231 byts
= fbyts
= slang
->sl_sbyts
;
11232 idxs
= fidxs
= slang
->sl_sidxs
;
11235 sp
->ts_prefixdepth
= PFD_NOPREFIX
;
11236 sp
->ts_state
= STATE_START
;
11241 * When there are postponed prefixes we need to use these first. At
11242 * the end of the prefix we continue in the case-fold tree.
11244 fbyts
= slang
->sl_fbyts
;
11245 fidxs
= slang
->sl_fidxs
;
11246 pbyts
= slang
->sl_pbyts
;
11247 pidxs
= slang
->sl_pidxs
;
11252 sp
->ts_prefixdepth
= PFD_PREFIXTREE
;
11253 sp
->ts_state
= STATE_NOPREFIX
; /* try without prefix first */
11259 sp
->ts_prefixdepth
= PFD_NOPREFIX
;
11260 sp
->ts_state
= STATE_START
;
11265 * Loop to find all suggestions. At each round we either:
11266 * - For the current state try one operation, advance "ts_curi",
11267 * increase "depth".
11268 * - When a state is done go to the next, set "ts_state".
11269 * - When all states are tried decrease "depth".
11271 while (depth
>= 0 && !got_int
)
11273 sp
= &stack
[depth
];
11274 switch (sp
->ts_state
)
11277 case STATE_NOPREFIX
:
11279 * Start of node: Deal with NUL bytes, which means
11280 * tword[] may end here.
11282 arridx
= sp
->ts_arridx
; /* current node in the tree */
11283 len
= byts
[arridx
]; /* bytes in this node */
11284 arridx
+= sp
->ts_curi
; /* index of current byte */
11286 if (sp
->ts_prefixdepth
== PFD_PREFIXTREE
)
11288 /* Skip over the NUL bytes, we use them later. */
11289 for (n
= 0; n
< len
&& byts
[arridx
+ n
] == 0; ++n
)
11293 /* Always past NUL bytes now. */
11294 n
= (int)sp
->ts_state
;
11295 sp
->ts_state
= STATE_ENDNUL
;
11296 sp
->ts_save_badflags
= su
->su_badflags
;
11298 /* At end of a prefix or at start of prefixtree: check for
11299 * following word. */
11300 if (byts
[arridx
] == 0 || n
== (int)STATE_NOPREFIX
)
11302 /* Set su->su_badflags to the caps type at this position.
11303 * Use the caps type until here for the prefix itself. */
11306 n
= nofold_len(fword
, sp
->ts_fidx
, su
->su_badptr
);
11310 flags
= badword_captype(su
->su_badptr
, su
->su_badptr
+ n
);
11311 su
->su_badflags
= badword_captype(su
->su_badptr
+ n
,
11312 su
->su_badptr
+ su
->su_badlen
);
11313 #ifdef DEBUG_TRIEWALK
11314 sprintf(changename
[depth
], "prefix");
11316 go_deeper(stack
, depth
, 0);
11318 sp
= &stack
[depth
];
11319 sp
->ts_prefixdepth
= depth
- 1;
11324 /* Move the prefix to preword[] with the right case
11325 * and make find_keepcap_word() works. */
11326 tword
[sp
->ts_twordlen
] = NUL
;
11327 make_case_word(tword
+ sp
->ts_splitoff
,
11328 preword
+ sp
->ts_prewordlen
, flags
);
11329 sp
->ts_prewordlen
= (char_u
)STRLEN(preword
);
11330 sp
->ts_splitoff
= sp
->ts_twordlen
;
11335 if (sp
->ts_curi
> len
|| byts
[arridx
] != 0)
11337 /* Past bytes in node and/or past NUL bytes. */
11338 sp
->ts_state
= STATE_ENDNUL
;
11339 sp
->ts_save_badflags
= su
->su_badflags
;
11344 * End of word in tree.
11346 ++sp
->ts_curi
; /* eat one NUL byte */
11348 flags
= (int)idxs
[arridx
];
11350 /* Skip words with the NOSUGGEST flag. */
11351 if (flags
& WF_NOSUGGEST
)
11354 fword_ends
= (fword
[sp
->ts_fidx
] == NUL
11356 ? vim_iswhite(fword
[sp
->ts_fidx
])
11357 : !spell_iswordp(fword
+ sp
->ts_fidx
, curbuf
)));
11358 tword
[sp
->ts_twordlen
] = NUL
;
11360 if (sp
->ts_prefixdepth
<= PFD_NOTSPECIAL
11361 && (sp
->ts_flags
& TSF_PREFIXOK
) == 0)
11363 /* There was a prefix before the word. Check that the prefix
11364 * can be used with this word. */
11365 /* Count the length of the NULs in the prefix. If there are
11366 * none this must be the first try without a prefix. */
11367 n
= stack
[sp
->ts_prefixdepth
].ts_arridx
;
11369 for (c
= 0; c
< len
&& pbyts
[n
+ c
] == 0; ++c
)
11373 c
= valid_word_prefix(c
, n
, flags
,
11374 tword
+ sp
->ts_splitoff
, slang
, FALSE
);
11378 /* Use the WF_RARE flag for a rare prefix. */
11379 if (c
& WF_RAREPFX
)
11382 /* Tricky: when checking for both prefix and compounding
11383 * we run into the prefix flag first.
11384 * Remember that it's OK, so that we accept the prefix
11385 * when arriving at a compound flag. */
11386 sp
->ts_flags
|= TSF_PREFIXOK
;
11390 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11391 * appending another compound word below. */
11392 if (sp
->ts_complen
== sp
->ts_compsplit
&& fword_ends
11393 && (flags
& WF_NEEDCOMP
))
11394 goodword_ends
= FALSE
;
11396 goodword_ends
= TRUE
;
11399 compound_ok
= TRUE
;
11400 if (sp
->ts_complen
> sp
->ts_compsplit
)
11402 if (slang
->sl_nobreak
)
11404 /* There was a word before this word. When there was no
11405 * change in this word (it was correct) add the first word
11406 * as a suggestion. If this word was corrected too, we
11407 * need to check if a correct word follows. */
11408 if (sp
->ts_fidx
- sp
->ts_splitfidx
11409 == sp
->ts_twordlen
- sp
->ts_splitoff
11410 && STRNCMP(fword
+ sp
->ts_splitfidx
,
11411 tword
+ sp
->ts_splitoff
,
11412 sp
->ts_fidx
- sp
->ts_splitfidx
) == 0)
11414 preword
[sp
->ts_prewordlen
] = NUL
;
11415 newscore
= score_wordcount_adj(slang
, sp
->ts_score
,
11416 preword
+ sp
->ts_prewordlen
,
11417 sp
->ts_prewordlen
> 0);
11418 /* Add the suggestion if the score isn't too bad. */
11419 if (newscore
<= su
->su_maxscore
)
11420 add_suggestion(su
, &su
->su_ga
, preword
,
11421 sp
->ts_splitfidx
- repextra
,
11422 newscore
, 0, FALSE
,
11423 lp
->lp_sallang
, FALSE
);
11429 /* There was a compound word before this word. If this
11430 * word does not support compounding then give up
11431 * (splitting is tried for the word without compound
11433 if (((unsigned)flags
>> 24) == 0
11434 || sp
->ts_twordlen
- sp
->ts_splitoff
11435 < slang
->sl_compminlen
)
11438 /* For multi-byte chars check character length against
11441 && slang
->sl_compminlen
> 0
11442 && mb_charlen(tword
+ sp
->ts_splitoff
)
11443 < slang
->sl_compminlen
)
11447 compflags
[sp
->ts_complen
] = ((unsigned)flags
>> 24);
11448 compflags
[sp
->ts_complen
+ 1] = NUL
;
11449 vim_strncpy(preword
+ sp
->ts_prewordlen
,
11450 tword
+ sp
->ts_splitoff
,
11451 sp
->ts_twordlen
- sp
->ts_splitoff
);
11453 while (*skiptowhite(p
) != NUL
)
11454 p
= skipwhite(skiptowhite(p
));
11455 if (fword_ends
&& !can_compound(slang
, p
,
11456 compflags
+ sp
->ts_compsplit
))
11457 /* Compound is not allowed. But it may still be
11458 * possible if we add another (short) word. */
11459 compound_ok
= FALSE
;
11461 /* Get pointer to last char of previous word. */
11462 p
= preword
+ sp
->ts_prewordlen
;
11463 mb_ptr_back(preword
, p
);
11468 * Form the word with proper case in preword.
11469 * If there is a word from a previous split, append.
11470 * For the soundfold tree don't change the case, simply append.
11473 STRCPY(preword
+ sp
->ts_prewordlen
, tword
+ sp
->ts_splitoff
);
11474 else if (flags
& WF_KEEPCAP
)
11475 /* Must find the word in the keep-case tree. */
11476 find_keepcap_word(slang
, tword
+ sp
->ts_splitoff
,
11477 preword
+ sp
->ts_prewordlen
);
11480 /* Include badflags: If the badword is onecap or allcap
11481 * use that for the goodword too. But if the badword is
11482 * allcap and it's only one char long use onecap. */
11483 c
= su
->su_badflags
;
11484 if ((c
& WF_ALLCAP
)
11486 && su
->su_badlen
== (*mb_ptr2len
)(su
->su_badptr
)
11488 && su
->su_badlen
== 1
11494 /* When appending a compound word after a word character don't
11496 if (p
!= NULL
&& spell_iswordp_nmw(p
))
11498 make_case_word(tword
+ sp
->ts_splitoff
,
11499 preword
+ sp
->ts_prewordlen
, c
);
11504 /* Don't use a banned word. It may appear again as a good
11505 * word, thus remember it. */
11506 if (flags
& WF_BANNED
)
11508 add_banned(su
, preword
+ sp
->ts_prewordlen
);
11511 if ((sp
->ts_complen
== sp
->ts_compsplit
11512 && WAS_BANNED(su
, preword
+ sp
->ts_prewordlen
))
11513 || WAS_BANNED(su
, preword
))
11515 if (slang
->sl_compprog
== NULL
)
11517 /* the word so far was banned but we may try compounding */
11518 goodword_ends
= FALSE
;
11523 if (!soundfold
) /* soundfold words don't have flags */
11525 if ((flags
& WF_REGION
)
11526 && (((unsigned)flags
>> 16) & lp
->lp_region
) == 0)
11527 newscore
+= SCORE_REGION
;
11528 if (flags
& WF_RARE
)
11529 newscore
+= SCORE_RARE
;
11531 if (!spell_valid_case(su
->su_badflags
,
11532 captype(preword
+ sp
->ts_prewordlen
, NULL
)))
11533 newscore
+= SCORE_ICASE
;
11536 /* TODO: how about splitting in the soundfold tree? */
11539 && sp
->ts_fidx
>= sp
->ts_fidxtry
11542 /* The badword also ends: add suggestions. */
11543 #ifdef DEBUG_TRIEWALK
11544 if (soundfold
&& STRCMP(preword
, "smwrd") == 0)
11548 /* print the stack of changes that brought us here */
11549 smsg("------ %s -------", fword
);
11550 for (j
= 0; j
< depth
; ++j
)
11551 smsg("%s", changename
[j
]);
11556 /* For soundfolded words we need to find the original
11557 * words, the edit distance and then add them. */
11558 add_sound_suggest(su
, preword
, sp
->ts_score
, lp
);
11562 /* Give a penalty when changing non-word char to word
11563 * char, e.g., "thes," -> "these". */
11564 p
= fword
+ sp
->ts_fidx
;
11565 mb_ptr_back(fword
, p
);
11566 if (!spell_iswordp(p
, curbuf
))
11568 p
= preword
+ STRLEN(preword
);
11569 mb_ptr_back(preword
, p
);
11570 if (spell_iswordp(p
, curbuf
))
11571 newscore
+= SCORE_NONWORD
;
11574 /* Give a bonus to words seen before. */
11575 score
= score_wordcount_adj(slang
,
11576 sp
->ts_score
+ newscore
,
11577 preword
+ sp
->ts_prewordlen
,
11578 sp
->ts_prewordlen
> 0);
11580 /* Add the suggestion if the score isn't too bad. */
11581 if (score
<= su
->su_maxscore
)
11583 add_suggestion(su
, &su
->su_ga
, preword
,
11584 sp
->ts_fidx
- repextra
,
11585 score
, 0, FALSE
, lp
->lp_sallang
, FALSE
);
11587 if (su
->su_badflags
& WF_MIXCAP
)
11589 /* We really don't know if the word should be
11590 * upper or lower case, add both. */
11591 c
= captype(preword
, NULL
);
11592 if (c
== 0 || c
== WF_ALLCAP
)
11594 make_case_word(tword
+ sp
->ts_splitoff
,
11595 preword
+ sp
->ts_prewordlen
,
11596 c
== 0 ? WF_ALLCAP
: 0);
11598 add_suggestion(su
, &su
->su_ga
, preword
,
11599 sp
->ts_fidx
- repextra
,
11600 score
+ SCORE_ICASE
, 0, FALSE
,
11601 lp
->lp_sallang
, FALSE
);
11609 * Try word split and/or compounding.
11611 if ((sp
->ts_fidx
>= sp
->ts_fidxtry
|| fword_ends
)
11613 /* Don't split halfway a character. */
11614 && (!has_mbyte
|| sp
->ts_tcharlen
== 0)
11621 /* If past the end of the bad word don't try a split.
11622 * Otherwise try changing the next word. E.g., find
11623 * suggestions for "the the" where the second "the" is
11624 * different. It's done like a split.
11625 * TODO: word split for soundfold words */
11626 try_split
= (sp
->ts_fidx
- repextra
< su
->su_badlen
)
11629 /* Get here in several situations:
11630 * 1. The word in the tree ends:
11631 * If the word allows compounding try that. Otherwise try
11632 * a split by inserting a space. For both check that a
11633 * valid words starts at fword[sp->ts_fidx].
11634 * For NOBREAK do like compounding to be able to check if
11635 * the next word is valid.
11636 * 2. The badword does end, but it was due to a change (e.g.,
11637 * a swap). No need to split, but do check that the
11638 * following word is valid.
11639 * 3. The badword and the word in the tree end. It may still
11640 * be possible to compound another (short) word.
11642 try_compound
= FALSE
;
11644 && slang
->sl_compprog
!= NULL
11645 && ((unsigned)flags
>> 24) != 0
11646 && sp
->ts_twordlen
- sp
->ts_splitoff
11647 >= slang
->sl_compminlen
11650 || slang
->sl_compminlen
== 0
11651 || mb_charlen(tword
+ sp
->ts_splitoff
)
11652 >= slang
->sl_compminlen
)
11654 && (slang
->sl_compsylmax
< MAXWLEN
11655 || sp
->ts_complen
+ 1 - sp
->ts_compsplit
11656 < slang
->sl_compmax
)
11657 && (byte_in_str(sp
->ts_complen
== sp
->ts_compsplit
11658 ? slang
->sl_compstartflags
11659 : slang
->sl_compallflags
,
11660 ((unsigned)flags
>> 24))))
11662 try_compound
= TRUE
;
11663 compflags
[sp
->ts_complen
] = ((unsigned)flags
>> 24);
11664 compflags
[sp
->ts_complen
+ 1] = NUL
;
11667 /* For NOBREAK we never try splitting, it won't make any word
11669 if (slang
->sl_nobreak
)
11670 try_compound
= TRUE
;
11672 /* If we could add a compound word, and it's also possible to
11673 * split at this point, do the split first and set
11674 * TSF_DIDSPLIT to avoid doing it again. */
11675 else if (!fword_ends
11677 && (sp
->ts_flags
& TSF_DIDSPLIT
) == 0)
11679 try_compound
= FALSE
;
11680 sp
->ts_flags
|= TSF_DIDSPLIT
;
11681 --sp
->ts_curi
; /* do the same NUL again */
11682 compflags
[sp
->ts_complen
] = NUL
;
11685 sp
->ts_flags
&= ~TSF_DIDSPLIT
;
11687 if (try_split
|| try_compound
)
11689 if (!try_compound
&& (!fword_ends
|| !goodword_ends
))
11691 /* If we're going to split need to check that the
11692 * words so far are valid for compounding. If there
11693 * is only one word it must not have the NEEDCOMPOUND
11695 if (sp
->ts_complen
== sp
->ts_compsplit
11696 && (flags
& WF_NEEDCOMP
))
11699 while (*skiptowhite(p
) != NUL
)
11700 p
= skipwhite(skiptowhite(p
));
11701 if (sp
->ts_complen
> sp
->ts_compsplit
11702 && !can_compound(slang
, p
,
11703 compflags
+ sp
->ts_compsplit
))
11706 if (slang
->sl_nosplitsugs
)
11707 newscore
+= SCORE_SPLIT_NO
;
11709 newscore
+= SCORE_SPLIT
;
11711 /* Give a bonus to words seen before. */
11712 newscore
= score_wordcount_adj(slang
, newscore
,
11713 preword
+ sp
->ts_prewordlen
, TRUE
);
11716 if (TRY_DEEPER(su
, stack
, depth
, newscore
))
11718 go_deeper(stack
, depth
, newscore
);
11719 #ifdef DEBUG_TRIEWALK
11720 if (!try_compound
&& !fword_ends
)
11721 sprintf(changename
[depth
], "%.*s-%s: split",
11722 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
);
11724 sprintf(changename
[depth
], "%.*s-%s: compound",
11725 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
);
11727 /* Save things to be restored at STATE_SPLITUNDO. */
11728 sp
->ts_save_badflags
= su
->su_badflags
;
11729 sp
->ts_state
= STATE_SPLITUNDO
;
11732 sp
= &stack
[depth
];
11734 /* Append a space to preword when splitting. */
11735 if (!try_compound
&& !fword_ends
)
11736 STRCAT(preword
, " ");
11737 sp
->ts_prewordlen
= (char_u
)STRLEN(preword
);
11738 sp
->ts_splitoff
= sp
->ts_twordlen
;
11739 sp
->ts_splitfidx
= sp
->ts_fidx
;
11741 /* If the badword has a non-word character at this
11742 * position skip it. That means replacing the
11743 * non-word character with a space. Always skip a
11744 * character when the word ends. But only when the
11745 * good word can end. */
11746 if (((!try_compound
&& !spell_iswordp_nmw(fword
11749 && fword
[sp
->ts_fidx
] != NUL
11756 l
= MB_BYTE2LEN(fword
[sp
->ts_fidx
]);
11762 /* Copy the skipped character to preword. */
11763 mch_memmove(preword
+ sp
->ts_prewordlen
,
11764 fword
+ sp
->ts_fidx
, l
);
11765 sp
->ts_prewordlen
+= l
;
11766 preword
[sp
->ts_prewordlen
] = NUL
;
11769 sp
->ts_score
-= SCORE_SPLIT
- SCORE_SUBST
;
11773 /* When compounding include compound flag in
11774 * compflags[] (already set above). When splitting we
11775 * may start compounding over again. */
11779 sp
->ts_compsplit
= sp
->ts_complen
;
11780 sp
->ts_prefixdepth
= PFD_NOPREFIX
;
11782 /* set su->su_badflags to the caps type at this
11786 n
= nofold_len(fword
, sp
->ts_fidx
, su
->su_badptr
);
11790 su
->su_badflags
= badword_captype(su
->su_badptr
+ n
,
11791 su
->su_badptr
+ su
->su_badlen
);
11793 /* Restart at top of the tree. */
11796 /* If there are postponed prefixes, try these too. */
11801 sp
->ts_prefixdepth
= PFD_PREFIXTREE
;
11802 sp
->ts_state
= STATE_NOPREFIX
;
11809 case STATE_SPLITUNDO
:
11810 /* Undo the changes done for word split or compound word. */
11811 su
->su_badflags
= sp
->ts_save_badflags
;
11813 /* Continue looking for NUL bytes. */
11814 sp
->ts_state
= STATE_START
;
11816 /* In case we went into the prefix tree. */
11822 /* Past the NUL bytes in the node. */
11823 su
->su_badflags
= sp
->ts_save_badflags
;
11824 if (fword
[sp
->ts_fidx
] == NUL
11826 && sp
->ts_tcharlen
== 0
11830 /* The badword ends, can't use STATE_PLAIN. */
11831 sp
->ts_state
= STATE_DEL
;
11834 sp
->ts_state
= STATE_PLAIN
;
11839 * Go over all possible bytes at this node, add each to tword[]
11840 * and use child node. "ts_curi" is the index.
11842 arridx
= sp
->ts_arridx
;
11843 if (sp
->ts_curi
> byts
[arridx
])
11845 /* Done all bytes at this node, do next state. When still at
11846 * already changed bytes skip the other tricks. */
11847 if (sp
->ts_fidx
>= sp
->ts_fidxtry
)
11848 sp
->ts_state
= STATE_DEL
;
11850 sp
->ts_state
= STATE_FINAL
;
11854 arridx
+= sp
->ts_curi
++;
11857 /* Normal byte, go one level deeper. If it's not equal to the
11858 * byte in the bad word adjust the score. But don't even try
11859 * when the byte was already changed. And don't try when we
11860 * just deleted this byte, accepting it is always cheaper then
11861 * delete + substitute. */
11862 if (c
== fword
[sp
->ts_fidx
]
11864 || (sp
->ts_tcharlen
> 0 && sp
->ts_isdiff
!= DIFF_NONE
)
11869 newscore
= SCORE_SUBST
;
11871 || (sp
->ts_fidx
>= sp
->ts_fidxtry
11872 && ((sp
->ts_flags
& TSF_DIDDEL
) == 0
11873 || c
!= fword
[sp
->ts_delidx
])))
11874 && TRY_DEEPER(su
, stack
, depth
, newscore
))
11876 go_deeper(stack
, depth
, newscore
);
11877 #ifdef DEBUG_TRIEWALK
11879 sprintf(changename
[depth
], "%.*s-%s: subst %c to %c",
11880 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
11881 fword
[sp
->ts_fidx
], c
);
11883 sprintf(changename
[depth
], "%.*s-%s: accept %c",
11884 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
11885 fword
[sp
->ts_fidx
]);
11888 sp
= &stack
[depth
];
11890 tword
[sp
->ts_twordlen
++] = c
;
11891 sp
->ts_arridx
= idxs
[arridx
];
11893 if (newscore
== SCORE_SUBST
)
11894 sp
->ts_isdiff
= DIFF_YES
;
11897 /* Multi-byte characters are a bit complicated to
11898 * handle: They differ when any of the bytes differ
11899 * and then their length may also differ. */
11900 if (sp
->ts_tcharlen
== 0)
11903 sp
->ts_tcharidx
= 0;
11904 sp
->ts_tcharlen
= MB_BYTE2LEN(c
);
11905 sp
->ts_fcharstart
= sp
->ts_fidx
- 1;
11906 sp
->ts_isdiff
= (newscore
!= 0)
11907 ? DIFF_YES
: DIFF_NONE
;
11909 else if (sp
->ts_isdiff
== DIFF_INSERT
)
11910 /* When inserting trail bytes don't advance in the
11913 if (++sp
->ts_tcharidx
== sp
->ts_tcharlen
)
11915 /* Last byte of character. */
11916 if (sp
->ts_isdiff
== DIFF_YES
)
11918 /* Correct ts_fidx for the byte length of the
11919 * character (we didn't check that before). */
11920 sp
->ts_fidx
= sp
->ts_fcharstart
11922 fword
[sp
->ts_fcharstart
]);
11924 /* For changing a composing character adjust
11925 * the score from SCORE_SUBST to
11926 * SCORE_SUBCOMP. */
11928 && utf_iscomposing(
11931 - sp
->ts_tcharlen
))
11932 && utf_iscomposing(
11934 + sp
->ts_fcharstart
)))
11936 SCORE_SUBST
- SCORE_SUBCOMP
;
11938 /* For a similar character adjust score from
11939 * SCORE_SUBST to SCORE_SIMILAR. */
11940 else if (!soundfold
11941 && slang
->sl_has_map
11942 && similar_chars(slang
,
11945 - sp
->ts_tcharlen
),
11947 + sp
->ts_fcharstart
)))
11949 SCORE_SUBST
- SCORE_SIMILAR
;
11951 else if (sp
->ts_isdiff
== DIFF_INSERT
11952 && sp
->ts_twordlen
> sp
->ts_tcharlen
)
11954 p
= tword
+ sp
->ts_twordlen
- sp
->ts_tcharlen
;
11955 c
= mb_ptr2char(p
);
11956 if (enc_utf8
&& utf_iscomposing(c
))
11958 /* Inserting a composing char doesn't
11959 * count that much. */
11960 sp
->ts_score
-= SCORE_INS
- SCORE_INSCOMP
;
11964 /* If the previous character was the same,
11965 * thus doubling a character, give a bonus
11966 * to the score. Also for the soundfold
11967 * tree (might seem illogical but does
11968 * give better scores). */
11969 mb_ptr_back(tword
, p
);
11970 if (c
== mb_ptr2char(p
))
11971 sp
->ts_score
-= SCORE_INS
11976 /* Starting a new char, reset the length. */
11977 sp
->ts_tcharlen
= 0;
11983 /* If we found a similar char adjust the score.
11984 * We do this after calling go_deeper() because
11988 && slang
->sl_has_map
11989 && similar_chars(slang
,
11990 c
, fword
[sp
->ts_fidx
- 1]))
11991 sp
->ts_score
-= SCORE_SUBST
- SCORE_SIMILAR
;
11999 /* When past the first byte of a multi-byte char don't try
12000 * delete/insert/swap a character. */
12001 if (has_mbyte
&& sp
->ts_tcharlen
> 0)
12003 sp
->ts_state
= STATE_FINAL
;
12008 * Try skipping one character in the bad word (delete it).
12010 sp
->ts_state
= STATE_INS_PREP
;
12012 if (soundfold
&& sp
->ts_fidx
== 0 && fword
[sp
->ts_fidx
] == '*')
12013 /* Deleting a vowel at the start of a word counts less, see
12014 * soundalike_score(). */
12015 newscore
= 2 * SCORE_DEL
/ 3;
12017 newscore
= SCORE_DEL
;
12018 if (fword
[sp
->ts_fidx
] != NUL
12019 && TRY_DEEPER(su
, stack
, depth
, newscore
))
12021 go_deeper(stack
, depth
, newscore
);
12022 #ifdef DEBUG_TRIEWALK
12023 sprintf(changename
[depth
], "%.*s-%s: delete %c",
12024 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
12025 fword
[sp
->ts_fidx
]);
12029 /* Remember what character we deleted, so that we can avoid
12030 * inserting it again. */
12031 stack
[depth
].ts_flags
|= TSF_DIDDEL
;
12032 stack
[depth
].ts_delidx
= sp
->ts_fidx
;
12034 /* Advance over the character in fword[]. Give a bonus to the
12035 * score if the same character is following "nn" -> "n". It's
12036 * a bit illogical for soundfold tree but it does give better
12041 c
= mb_ptr2char(fword
+ sp
->ts_fidx
);
12042 stack
[depth
].ts_fidx
+= MB_BYTE2LEN(fword
[sp
->ts_fidx
]);
12043 if (enc_utf8
&& utf_iscomposing(c
))
12044 stack
[depth
].ts_score
-= SCORE_DEL
- SCORE_DELCOMP
;
12045 else if (c
== mb_ptr2char(fword
+ stack
[depth
].ts_fidx
))
12046 stack
[depth
].ts_score
-= SCORE_DEL
- SCORE_DELDUP
;
12051 ++stack
[depth
].ts_fidx
;
12052 if (fword
[sp
->ts_fidx
] == fword
[sp
->ts_fidx
+ 1])
12053 stack
[depth
].ts_score
-= SCORE_DEL
- SCORE_DELDUP
;
12059 case STATE_INS_PREP
:
12060 if (sp
->ts_flags
& TSF_DIDDEL
)
12062 /* If we just deleted a byte then inserting won't make sense,
12063 * a substitute is always cheaper. */
12064 sp
->ts_state
= STATE_SWAP
;
12068 /* skip over NUL bytes */
12072 if (sp
->ts_curi
> byts
[n
])
12074 /* Only NUL bytes at this node, go to next state. */
12075 sp
->ts_state
= STATE_SWAP
;
12078 if (byts
[n
+ sp
->ts_curi
] != NUL
)
12080 /* Found a byte to insert. */
12081 sp
->ts_state
= STATE_INS
;
12091 /* Insert one byte. Repeat this for each possible byte at this
12094 if (sp
->ts_curi
> byts
[n
])
12096 /* Done all bytes at this node, go to next state. */
12097 sp
->ts_state
= STATE_SWAP
;
12101 /* Do one more byte at this node, but:
12102 * - Skip NUL bytes.
12103 * - Skip the byte if it's equal to the byte in the word,
12104 * accepting that byte is always better.
12106 n
+= sp
->ts_curi
++;
12108 if (soundfold
&& sp
->ts_twordlen
== 0 && c
== '*')
12109 /* Inserting a vowel at the start of a word counts less,
12110 * see soundalike_score(). */
12111 newscore
= 2 * SCORE_INS
/ 3;
12113 newscore
= SCORE_INS
;
12114 if (c
!= fword
[sp
->ts_fidx
]
12115 && TRY_DEEPER(su
, stack
, depth
, newscore
))
12117 go_deeper(stack
, depth
, newscore
);
12118 #ifdef DEBUG_TRIEWALK
12119 sprintf(changename
[depth
], "%.*s-%s: insert %c",
12120 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
12124 sp
= &stack
[depth
];
12125 tword
[sp
->ts_twordlen
++] = c
;
12126 sp
->ts_arridx
= idxs
[n
];
12130 fl
= MB_BYTE2LEN(c
);
12133 /* There are following bytes for the same character.
12134 * We must find all bytes before trying
12135 * delete/insert/swap/etc. */
12136 sp
->ts_tcharlen
= fl
;
12137 sp
->ts_tcharidx
= 1;
12138 sp
->ts_isdiff
= DIFF_INSERT
;
12146 /* If the previous character was the same, thus doubling a
12147 * character, give a bonus to the score. Also for
12148 * soundfold words (illogical but does give a better
12150 if (sp
->ts_twordlen
>= 2
12151 && tword
[sp
->ts_twordlen
- 2] == c
)
12152 sp
->ts_score
-= SCORE_INS
- SCORE_INSDUP
;
12159 * Swap two bytes in the bad word: "12" -> "21".
12160 * We change "fword" here, it's changed back afterwards at
12163 p
= fword
+ sp
->ts_fidx
;
12167 /* End of word, can't swap or replace. */
12168 sp
->ts_state
= STATE_FINAL
;
12172 /* Don't swap if the first character is not a word character.
12173 * SWAP3 etc. also don't make sense then. */
12174 if (!soundfold
&& !spell_iswordp(p
, curbuf
))
12176 sp
->ts_state
= STATE_REP_INI
;
12183 n
= mb_cptr2len(p
);
12184 c
= mb_ptr2char(p
);
12187 else if (!soundfold
&& !spell_iswordp(p
+ n
, curbuf
))
12188 c2
= c
; /* don't swap non-word char */
12190 c2
= mb_ptr2char(p
+ n
);
12197 else if (!soundfold
&& !spell_iswordp(p
+ 1, curbuf
))
12198 c2
= c
; /* don't swap non-word char */
12203 /* When the second character is NUL we can't swap. */
12206 sp
->ts_state
= STATE_REP_INI
;
12210 /* When characters are identical, swap won't do anything.
12211 * Also get here if the second char is not a word character. */
12214 sp
->ts_state
= STATE_SWAP3
;
12217 if (c2
!= NUL
&& TRY_DEEPER(su
, stack
, depth
, SCORE_SWAP
))
12219 go_deeper(stack
, depth
, SCORE_SWAP
);
12220 #ifdef DEBUG_TRIEWALK
12221 sprintf(changename
[depth
], "%.*s-%s: swap %c and %c",
12222 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
12225 sp
->ts_state
= STATE_UNSWAP
;
12230 fl
= mb_char2len(c2
);
12231 mch_memmove(p
, p
+ n
, fl
);
12232 mb_char2bytes(c
, p
+ fl
);
12233 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ n
+ fl
;
12240 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ 2;
12244 /* If this swap doesn't work then SWAP3 won't either. */
12245 sp
->ts_state
= STATE_REP_INI
;
12249 /* Undo the STATE_SWAP swap: "21" -> "12". */
12250 p
= fword
+ sp
->ts_fidx
;
12254 n
= MB_BYTE2LEN(*p
);
12255 c
= mb_ptr2char(p
+ n
);
12256 mch_memmove(p
+ MB_BYTE2LEN(p
[n
]), p
, n
);
12257 mb_char2bytes(c
, p
);
12269 /* Swap two bytes, skipping one: "123" -> "321". We change
12270 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12271 p
= fword
+ sp
->ts_fidx
;
12275 n
= mb_cptr2len(p
);
12276 c
= mb_ptr2char(p
);
12277 fl
= mb_cptr2len(p
+ n
);
12278 c2
= mb_ptr2char(p
+ n
);
12279 if (!soundfold
&& !spell_iswordp(p
+ n
+ fl
, curbuf
))
12280 c3
= c
; /* don't swap non-word char */
12282 c3
= mb_ptr2char(p
+ n
+ fl
);
12289 if (!soundfold
&& !spell_iswordp(p
+ 2, curbuf
))
12290 c3
= c
; /* don't swap non-word char */
12295 /* When characters are identical: "121" then SWAP3 result is
12296 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12297 * same as SWAP on next char: "112". Thus skip all swapping.
12298 * Also skip when c3 is NUL.
12299 * Also get here when the third character is not a word character.
12300 * Second character may any char: "a.b" -> "b.a" */
12301 if (c
== c3
|| c3
== NUL
)
12303 sp
->ts_state
= STATE_REP_INI
;
12306 if (TRY_DEEPER(su
, stack
, depth
, SCORE_SWAP3
))
12308 go_deeper(stack
, depth
, SCORE_SWAP3
);
12309 #ifdef DEBUG_TRIEWALK
12310 sprintf(changename
[depth
], "%.*s-%s: swap3 %c and %c",
12311 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
12314 sp
->ts_state
= STATE_UNSWAP3
;
12319 tl
= mb_char2len(c3
);
12320 mch_memmove(p
, p
+ n
+ fl
, tl
);
12321 mb_char2bytes(c2
, p
+ tl
);
12322 mb_char2bytes(c
, p
+ fl
+ tl
);
12323 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ n
+ fl
+ tl
;
12330 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ 3;
12334 sp
->ts_state
= STATE_REP_INI
;
12337 case STATE_UNSWAP3
:
12338 /* Undo STATE_SWAP3: "321" -> "123" */
12339 p
= fword
+ sp
->ts_fidx
;
12343 n
= MB_BYTE2LEN(*p
);
12344 c2
= mb_ptr2char(p
+ n
);
12345 fl
= MB_BYTE2LEN(p
[n
]);
12346 c
= mb_ptr2char(p
+ n
+ fl
);
12347 tl
= MB_BYTE2LEN(p
[n
+ fl
]);
12348 mch_memmove(p
+ fl
+ tl
, p
, n
);
12349 mb_char2bytes(c
, p
);
12350 mb_char2bytes(c2
, p
+ tl
);
12362 if (!soundfold
&& !spell_iswordp(p
, curbuf
))
12364 /* Middle char is not a word char, skip the rotate. First and
12365 * third char were already checked at swap and swap3. */
12366 sp
->ts_state
= STATE_REP_INI
;
12370 /* Rotate three characters left: "123" -> "231". We change
12371 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12372 if (TRY_DEEPER(su
, stack
, depth
, SCORE_SWAP3
))
12374 go_deeper(stack
, depth
, SCORE_SWAP3
);
12375 #ifdef DEBUG_TRIEWALK
12376 p
= fword
+ sp
->ts_fidx
;
12377 sprintf(changename
[depth
], "%.*s-%s: rotate left %c%c%c",
12378 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
12381 sp
->ts_state
= STATE_UNROT3L
;
12383 p
= fword
+ sp
->ts_fidx
;
12387 n
= mb_cptr2len(p
);
12388 c
= mb_ptr2char(p
);
12389 fl
= mb_cptr2len(p
+ n
);
12390 fl
+= mb_cptr2len(p
+ n
+ fl
);
12391 mch_memmove(p
, p
+ n
, fl
);
12392 mb_char2bytes(c
, p
+ fl
);
12393 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ n
+ fl
;
12402 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ 3;
12406 sp
->ts_state
= STATE_REP_INI
;
12409 case STATE_UNROT3L
:
12410 /* Undo ROT3L: "231" -> "123" */
12411 p
= fword
+ sp
->ts_fidx
;
12415 n
= MB_BYTE2LEN(*p
);
12416 n
+= MB_BYTE2LEN(p
[n
]);
12417 c
= mb_ptr2char(p
+ n
);
12418 tl
= MB_BYTE2LEN(p
[n
]);
12419 mch_memmove(p
+ tl
, p
, n
);
12420 mb_char2bytes(c
, p
);
12431 /* Rotate three bytes right: "123" -> "312". We change "fword"
12432 * here, it's changed back afterwards at STATE_UNROT3R. */
12433 if (TRY_DEEPER(su
, stack
, depth
, SCORE_SWAP3
))
12435 go_deeper(stack
, depth
, SCORE_SWAP3
);
12436 #ifdef DEBUG_TRIEWALK
12437 p
= fword
+ sp
->ts_fidx
;
12438 sprintf(changename
[depth
], "%.*s-%s: rotate right %c%c%c",
12439 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
12442 sp
->ts_state
= STATE_UNROT3R
;
12444 p
= fword
+ sp
->ts_fidx
;
12448 n
= mb_cptr2len(p
);
12449 n
+= mb_cptr2len(p
+ n
);
12450 c
= mb_ptr2char(p
+ n
);
12451 tl
= mb_cptr2len(p
+ n
);
12452 mch_memmove(p
+ tl
, p
, n
);
12453 mb_char2bytes(c
, p
);
12454 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ n
+ tl
;
12463 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ 3;
12467 sp
->ts_state
= STATE_REP_INI
;
12470 case STATE_UNROT3R
:
12471 /* Undo ROT3R: "312" -> "123" */
12472 p
= fword
+ sp
->ts_fidx
;
12476 c
= mb_ptr2char(p
);
12477 tl
= MB_BYTE2LEN(*p
);
12478 n
= MB_BYTE2LEN(p
[tl
]);
12479 n
+= MB_BYTE2LEN(p
[tl
+ n
]);
12480 mch_memmove(p
, p
+ tl
, n
);
12481 mb_char2bytes(c
, p
+ n
);
12493 case STATE_REP_INI
:
12494 /* Check if matching with REP items from the .aff file would work.
12496 * - there are no REP items and we are not in the soundfold trie
12497 * - the score is going to be too high anyway
12498 * - already applied a REP item or swapped here */
12499 if ((lp
->lp_replang
== NULL
&& !soundfold
)
12500 || sp
->ts_score
+ SCORE_REP
>= su
->su_maxscore
12501 || sp
->ts_fidx
< sp
->ts_fidxtry
)
12503 sp
->ts_state
= STATE_FINAL
;
12507 /* Use the first byte to quickly find the first entry that may
12508 * match. If the index is -1 there is none. */
12510 sp
->ts_curi
= slang
->sl_repsal_first
[fword
[sp
->ts_fidx
]];
12512 sp
->ts_curi
= lp
->lp_replang
->sl_rep_first
[fword
[sp
->ts_fidx
]];
12514 if (sp
->ts_curi
< 0)
12516 sp
->ts_state
= STATE_FINAL
;
12520 sp
->ts_state
= STATE_REP
;
12524 /* Try matching with REP items from the .aff file. For each match
12525 * replace the characters and check if the resulting word is
12527 p
= fword
+ sp
->ts_fidx
;
12530 gap
= &slang
->sl_repsal
;
12532 gap
= &lp
->lp_replang
->sl_rep
;
12533 while (sp
->ts_curi
< gap
->ga_len
)
12535 ftp
= (fromto_T
*)gap
->ga_data
+ sp
->ts_curi
++;
12536 if (*ftp
->ft_from
!= *p
)
12538 /* past possible matching entries */
12539 sp
->ts_curi
= gap
->ga_len
;
12542 if (STRNCMP(ftp
->ft_from
, p
, STRLEN(ftp
->ft_from
)) == 0
12543 && TRY_DEEPER(su
, stack
, depth
, SCORE_REP
))
12545 go_deeper(stack
, depth
, SCORE_REP
);
12546 #ifdef DEBUG_TRIEWALK
12547 sprintf(changename
[depth
], "%.*s-%s: replace %s with %s",
12548 sp
->ts_twordlen
, tword
, fword
+ sp
->ts_fidx
,
12549 ftp
->ft_from
, ftp
->ft_to
);
12551 /* Need to undo this afterwards. */
12552 sp
->ts_state
= STATE_REP_UNDO
;
12554 /* Change the "from" to the "to" string. */
12556 fl
= (int)STRLEN(ftp
->ft_from
);
12557 tl
= (int)STRLEN(ftp
->ft_to
);
12560 mch_memmove(p
+ tl
, p
+ fl
, STRLEN(p
+ fl
) + 1);
12561 repextra
+= tl
- fl
;
12563 mch_memmove(p
, ftp
->ft_to
, tl
);
12564 stack
[depth
].ts_fidxtry
= sp
->ts_fidx
+ tl
;
12566 stack
[depth
].ts_tcharlen
= 0;
12572 if (sp
->ts_curi
>= gap
->ga_len
&& sp
->ts_state
== STATE_REP
)
12573 /* No (more) matches. */
12574 sp
->ts_state
= STATE_FINAL
;
12578 case STATE_REP_UNDO
:
12579 /* Undo a REP replacement and continue with the next one. */
12581 gap
= &slang
->sl_repsal
;
12583 gap
= &lp
->lp_replang
->sl_rep
;
12584 ftp
= (fromto_T
*)gap
->ga_data
+ sp
->ts_curi
- 1;
12585 fl
= (int)STRLEN(ftp
->ft_from
);
12586 tl
= (int)STRLEN(ftp
->ft_to
);
12587 p
= fword
+ sp
->ts_fidx
;
12590 mch_memmove(p
+ fl
, p
+ tl
, STRLEN(p
+ tl
) + 1);
12591 repextra
-= tl
- fl
;
12593 mch_memmove(p
, ftp
->ft_from
, fl
);
12594 sp
->ts_state
= STATE_REP
;
12598 /* Did all possible states at this level, go up one level. */
12601 if (depth
>= 0 && stack
[depth
].ts_prefixdepth
== PFD_PREFIXTREE
)
12603 /* Continue in or go back to the prefix tree. */
12608 /* Don't check for CTRL-C too often, it takes time. */
12609 if (--breakcheckcount
== 0)
12612 breakcheckcount
= 1000;
12620 * Go one level deeper in the tree.
12623 go_deeper(stack
, depth
, score_add
)
12628 stack
[depth
+ 1] = stack
[depth
];
12629 stack
[depth
+ 1].ts_state
= STATE_START
;
12630 stack
[depth
+ 1].ts_score
= stack
[depth
].ts_score
+ score_add
;
12631 stack
[depth
+ 1].ts_curi
= 1; /* start just after length byte */
12632 stack
[depth
+ 1].ts_flags
= 0;
12637 * Case-folding may change the number of bytes: Count nr of chars in
12638 * fword[flen] and return the byte length of that many chars in "word".
12641 nofold_len(fword
, flen
, word
)
12649 for (p
= fword
; p
< fword
+ flen
; mb_ptr_adv(p
))
12651 for (p
= word
; i
> 0; mb_ptr_adv(p
))
12653 return (int)(p
- word
);
12658 * "fword" is a good word with case folded. Find the matching keep-case
12659 * words and put it in "kword".
12660 * Theoretically there could be several keep-case words that result in the
12661 * same case-folded word, but we only find one...
12664 find_keepcap_word(slang
, fword
, kword
)
12669 char_u uword
[MAXWLEN
]; /* "fword" in upper-case */
12673 /* The following arrays are used at each depth in the tree. */
12674 idx_T arridx
[MAXWLEN
];
12675 int round
[MAXWLEN
];
12676 int fwordidx
[MAXWLEN
];
12677 int uwordidx
[MAXWLEN
];
12678 int kwordlen
[MAXWLEN
];
12686 char_u
*byts
= slang
->sl_kbyts
; /* array with bytes of the words */
12687 idx_T
*idxs
= slang
->sl_kidxs
; /* array with indexes */
12691 /* array is empty: "cannot happen" */
12696 /* Make an all-cap version of "fword". */
12697 allcap_copy(fword
, uword
);
12700 * Each character needs to be tried both case-folded and upper-case.
12701 * All this gets very complicated if we keep in mind that changing case
12702 * may change the byte length of a multi-byte character...
12712 if (fword
[fwordidx
[depth
]] == NUL
)
12714 /* We are at the end of "fword". If the tree allows a word to end
12715 * here we have found a match. */
12716 if (byts
[arridx
[depth
] + 1] == 0)
12718 kword
[kwordlen
[depth
]] = NUL
;
12722 /* kword is getting too long, continue one level up */
12725 else if (++round
[depth
] > 2)
12727 /* tried both fold-case and upper-case character, continue one
12734 * round[depth] == 1: Try using the folded-case character.
12735 * round[depth] == 2: Try using the upper-case character.
12740 flen
= mb_cptr2len(fword
+ fwordidx
[depth
]);
12741 ulen
= mb_cptr2len(uword
+ uwordidx
[depth
]);
12746 if (round
[depth
] == 1)
12748 p
= fword
+ fwordidx
[depth
];
12753 p
= uword
+ uwordidx
[depth
];
12757 for (tryidx
= arridx
[depth
]; l
> 0; --l
)
12759 /* Perform a binary search in the list of accepted bytes. */
12760 len
= byts
[tryidx
++];
12763 hi
= tryidx
+ len
- 1;
12769 else if (byts
[m
] < c
)
12778 /* Stop if there is no matching byte. */
12779 if (hi
< lo
|| byts
[lo
] != c
)
12782 /* Continue at the child (if there is one). */
12789 * Found the matching char. Copy it to "kword" and go a
12792 if (round
[depth
] == 1)
12794 STRNCPY(kword
+ kwordlen
[depth
], fword
+ fwordidx
[depth
],
12796 kwordlen
[depth
+ 1] = kwordlen
[depth
] + flen
;
12800 STRNCPY(kword
+ kwordlen
[depth
], uword
+ uwordidx
[depth
],
12802 kwordlen
[depth
+ 1] = kwordlen
[depth
] + ulen
;
12804 fwordidx
[depth
+ 1] = fwordidx
[depth
] + flen
;
12805 uwordidx
[depth
+ 1] = uwordidx
[depth
] + ulen
;
12808 arridx
[depth
] = tryidx
;
12814 /* Didn't find it: "cannot happen". */
12819 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12827 char_u badsound
[MAXWLEN
];
12834 if (ga_grow(&su
->su_sga
, su
->su_ga
.ga_len
) == FAIL
)
12837 /* Use the sound-folding of the first language that supports it. */
12838 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
12840 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
12841 if (lp
->lp_slang
->sl_sal
.ga_len
> 0)
12843 /* soundfold the bad word */
12844 spell_soundfold(lp
->lp_slang
, su
->su_fbadword
, TRUE
, badsound
);
12846 for (i
= 0; i
< su
->su_ga
.ga_len
; ++i
)
12848 stp
= &SUG(su
->su_ga
, i
);
12850 /* Case-fold the suggested word, sound-fold it and compute the
12851 * sound-a-like score. */
12852 score
= stp_sal_score(stp
, su
, lp
->lp_slang
, badsound
);
12853 if (score
< SCORE_MAXMAX
)
12855 /* Add the suggestion. */
12856 sstp
= &SUG(su
->su_sga
, su
->su_sga
.ga_len
);
12857 sstp
->st_word
= vim_strsave(stp
->st_word
);
12858 if (sstp
->st_word
!= NULL
)
12860 sstp
->st_wordlen
= stp
->st_wordlen
;
12861 sstp
->st_score
= score
;
12862 sstp
->st_altscore
= 0;
12863 sstp
->st_orglen
= stp
->st_orglen
;
12864 ++su
->su_sga
.ga_len
;
12874 * Combine the list of suggestions in su->su_ga and su->su_sga.
12875 * They are intwined.
12888 char_u badsound
[MAXWLEN
];
12891 slang_T
*slang
= NULL
;
12893 /* Add the alternate score to su_ga. */
12894 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
12896 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
12897 if (lp
->lp_slang
->sl_sal
.ga_len
> 0)
12899 /* soundfold the bad word */
12900 slang
= lp
->lp_slang
;
12901 spell_soundfold(slang
, su
->su_fbadword
, TRUE
, badsound
);
12903 for (i
= 0; i
< su
->su_ga
.ga_len
; ++i
)
12905 stp
= &SUG(su
->su_ga
, i
);
12906 stp
->st_altscore
= stp_sal_score(stp
, su
, slang
, badsound
);
12907 if (stp
->st_altscore
== SCORE_MAXMAX
)
12908 stp
->st_score
= (stp
->st_score
* 3 + SCORE_BIG
) / 4;
12910 stp
->st_score
= (stp
->st_score
* 3
12911 + stp
->st_altscore
) / 4;
12912 stp
->st_salscore
= FALSE
;
12918 if (slang
== NULL
) /* Using "double" without sound folding. */
12920 (void)cleanup_suggestions(&su
->su_ga
, su
->su_maxscore
,
12925 /* Add the alternate score to su_sga. */
12926 for (i
= 0; i
< su
->su_sga
.ga_len
; ++i
)
12928 stp
= &SUG(su
->su_sga
, i
);
12929 stp
->st_altscore
= spell_edit_score(slang
,
12930 su
->su_badword
, stp
->st_word
);
12931 if (stp
->st_score
== SCORE_MAXMAX
)
12932 stp
->st_score
= (SCORE_BIG
* 7 + stp
->st_altscore
) / 8;
12934 stp
->st_score
= (stp
->st_score
* 7 + stp
->st_altscore
) / 8;
12935 stp
->st_salscore
= TRUE
;
12938 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12939 * for both lists. */
12940 check_suggestions(su
, &su
->su_ga
);
12941 (void)cleanup_suggestions(&su
->su_ga
, su
->su_maxscore
, su
->su_maxcount
);
12942 check_suggestions(su
, &su
->su_sga
);
12943 (void)cleanup_suggestions(&su
->su_sga
, su
->su_maxscore
, su
->su_maxcount
);
12945 ga_init2(&ga
, (int)sizeof(suginfo_T
), 1);
12946 if (ga_grow(&ga
, su
->su_ga
.ga_len
+ su
->su_sga
.ga_len
) == FAIL
)
12950 for (i
= 0; i
< su
->su_ga
.ga_len
|| i
< su
->su_sga
.ga_len
; ++i
)
12952 /* round 1: get a suggestion from su_ga
12953 * round 2: get a suggestion from su_sga */
12954 for (round
= 1; round
<= 2; ++round
)
12956 gap
= round
== 1 ? &su
->su_ga
: &su
->su_sga
;
12957 if (i
< gap
->ga_len
)
12959 /* Don't add a word if it's already there. */
12960 p
= SUG(*gap
, i
).st_word
;
12961 for (j
= 0; j
< ga
.ga_len
; ++j
)
12962 if (STRCMP(stp
[j
].st_word
, p
) == 0)
12964 if (j
== ga
.ga_len
)
12965 stp
[ga
.ga_len
++] = SUG(*gap
, i
);
12972 ga_clear(&su
->su_ga
);
12973 ga_clear(&su
->su_sga
);
12975 /* Truncate the list to the number of suggestions that will be displayed. */
12976 if (ga
.ga_len
> su
->su_maxcount
)
12978 for (i
= su
->su_maxcount
; i
< ga
.ga_len
; ++i
)
12979 vim_free(stp
[i
].st_word
);
12980 ga
.ga_len
= su
->su_maxcount
;
12987 * For the goodword in "stp" compute the soundalike score compared to the
12991 stp_sal_score(stp
, su
, slang
, badsound
)
12995 char_u
*badsound
; /* sound-folded badword */
13000 char_u badsound2
[MAXWLEN
];
13001 char_u fword
[MAXWLEN
];
13002 char_u goodsound
[MAXWLEN
];
13003 char_u goodword
[MAXWLEN
];
13006 lendiff
= (int)(su
->su_badlen
- stp
->st_orglen
);
13011 /* soundfold the bad word with more characters following */
13012 (void)spell_casefold(su
->su_badptr
, stp
->st_orglen
, fword
, MAXWLEN
);
13014 /* When joining two words the sound often changes a lot. E.g., "t he"
13015 * sounds like "t h" while "the" sounds like "@". Avoid that by
13016 * removing the space. Don't do it when the good word also contains a
13018 if (vim_iswhite(su
->su_badptr
[su
->su_badlen
])
13019 && *skiptowhite(stp
->st_word
) == NUL
)
13020 for (p
= fword
; *(p
= skiptowhite(p
)) != NUL
; )
13021 mch_memmove(p
, p
+ 1, STRLEN(p
));
13023 spell_soundfold(slang
, fword
, TRUE
, badsound2
);
13029 /* Add part of the bad word to the good word, so that we soundfold
13030 * what replaces the bad word. */
13031 STRCPY(goodword
, stp
->st_word
);
13032 vim_strncpy(goodword
+ stp
->st_wordlen
,
13033 su
->su_badptr
+ su
->su_badlen
- lendiff
, lendiff
);
13037 pgood
= stp
->st_word
;
13039 /* Sound-fold the word and compute the score for the difference. */
13040 spell_soundfold(slang
, pgood
, FALSE
, goodsound
);
13042 return soundalike_score(goodsound
, pbad
);
13045 /* structure used to store soundfolded words that add_sound_suggest() has
13046 * handled already. */
13049 short sft_score
; /* lowest score used */
13050 char_u sft_word
[1]; /* soundfolded word, actually longer */
13053 static sftword_T dumsft
;
13054 #define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13055 #define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13058 * Prepare for calling suggest_try_soundalike().
13061 suggest_try_soundalike_prep()
13067 /* Do this for all languages that support sound folding and for which a
13068 * .sug file has been loaded. */
13069 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
13071 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
13072 slang
= lp
->lp_slang
;
13073 if (slang
->sl_sal
.ga_len
> 0 && slang
->sl_sbyts
!= NULL
)
13074 /* prepare the hashtable used by add_sound_suggest() */
13075 hash_init(&slang
->sl_sounddone
);
13080 * Find suggestions by comparing the word in a sound-a-like form.
13081 * Note: This doesn't support postponed prefixes.
13084 suggest_try_soundalike(su
)
13087 char_u salword
[MAXWLEN
];
13092 /* Do this for all languages that support sound folding and for which a
13093 * .sug file has been loaded. */
13094 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
13096 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
13097 slang
= lp
->lp_slang
;
13098 if (slang
->sl_sal
.ga_len
> 0 && slang
->sl_sbyts
!= NULL
)
13100 /* soundfold the bad word */
13101 spell_soundfold(slang
, su
->su_fbadword
, TRUE
, salword
);
13103 /* try all kinds of inserts/deletes/swaps/etc. */
13104 /* TODO: also soundfold the next words, so that we can try joining
13106 suggest_trie_walk(su
, lp
, salword
, TRUE
);
13112 * Finish up after calling suggest_try_soundalike().
13115 suggest_try_soundalike_finish()
13123 /* Do this for all languages that support sound folding and for which a
13124 * .sug file has been loaded. */
13125 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
13127 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
13128 slang
= lp
->lp_slang
;
13129 if (slang
->sl_sal
.ga_len
> 0 && slang
->sl_sbyts
!= NULL
)
13131 /* Free the info about handled words. */
13132 todo
= (int)slang
->sl_sounddone
.ht_used
;
13133 for (hi
= slang
->sl_sounddone
.ht_array
; todo
> 0; ++hi
)
13134 if (!HASHITEM_EMPTY(hi
))
13136 vim_free(HI2SFT(hi
));
13140 /* Clear the hashtable, it may also be used by another region. */
13141 hash_clear(&slang
->sl_sounddone
);
13142 hash_init(&slang
->sl_sounddone
);
13148 * A match with a soundfolded word is found. Add the good word(s) that
13149 * produce this soundfolded word.
13152 add_sound_suggest(su
, goodword
, score
, lp
)
13155 int score
; /* soundfold score */
13158 slang_T
*slang
= lp
->lp_slang
; /* language for sound folding */
13162 char_u theword
[MAXWLEN
];
13178 * It's very well possible that the same soundfold word is found several
13179 * times with different scores. Since the following is quite slow only do
13180 * the words that have a better score than before. Use a hashtable to
13181 * remember the words that have been done.
13183 hash
= hash_hash(goodword
);
13184 hi
= hash_lookup(&slang
->sl_sounddone
, goodword
, hash
);
13185 if (HASHITEM_EMPTY(hi
))
13187 sft
= (sftword_T
*)alloc((unsigned)(sizeof(sftword_T
)
13188 + STRLEN(goodword
)));
13191 sft
->sft_score
= score
;
13192 STRCPY(sft
->sft_word
, goodword
);
13193 hash_add_item(&slang
->sl_sounddone
, hi
, sft
->sft_word
, hash
);
13199 if (score
>= sft
->sft_score
)
13201 sft
->sft_score
= score
;
13205 * Find the word nr in the soundfold tree.
13207 sfwordnr
= soundfold_find(slang
, goodword
);
13210 EMSG2(_(e_intern2
), "add_sound_suggest()");
13215 * go over the list of good words that produce this soundfold word
13217 nrline
= ml_get_buf(slang
->sl_sugbuf
, (linenr_T
)(sfwordnr
+ 1), FALSE
);
13219 while (*nrline
!= NUL
)
13221 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13222 * previous wordnr. */
13223 orgnr
+= bytes2offset(&nrline
);
13225 byts
= slang
->sl_fbyts
;
13226 idxs
= slang
->sl_fidxs
;
13228 /* Lookup the word "orgnr" one of the two tries. */
13235 if (wordcount
== orgnr
&& byts
[n
+ 1] == NUL
)
13236 break; /* found end of word */
13238 if (byts
[n
+ 1] == NUL
)
13241 /* skip over the NUL bytes */
13242 for ( ; byts
[n
+ i
] == NUL
; ++i
)
13243 if (i
> byts
[n
]) /* safety check */
13245 STRCPY(theword
+ wlen
, "BAD");
13249 /* One of the siblings must have the word. */
13250 for ( ; i
< byts
[n
]; ++i
)
13252 wc
= idxs
[idxs
[n
+ i
]]; /* nr of words under this byte */
13253 if (wordcount
+ wc
> orgnr
)
13258 theword
[wlen
++] = byts
[n
+ i
];
13262 theword
[wlen
] = NUL
;
13264 /* Go over the possible flags and regions. */
13265 for (; i
<= byts
[n
] && byts
[n
+ i
] == NUL
; ++i
)
13267 char_u cword
[MAXWLEN
];
13269 int flags
= (int)idxs
[n
+ i
];
13271 /* Skip words with the NOSUGGEST flag */
13272 if (flags
& WF_NOSUGGEST
)
13275 if (flags
& WF_KEEPCAP
)
13277 /* Must find the word in the keep-case tree. */
13278 find_keepcap_word(slang
, theword
, cword
);
13283 flags
|= su
->su_badflags
;
13284 if ((flags
& WF_CAPMASK
) != 0)
13286 /* Need to fix case according to "flags". */
13287 make_case_word(theword
, cword
, flags
);
13294 /* Add the suggestion. */
13295 if (sps_flags
& SPS_DOUBLE
)
13297 /* Add the suggestion if the score isn't too bad. */
13298 if (score
<= su
->su_maxscore
)
13299 add_suggestion(su
, &su
->su_sga
, p
, su
->su_badlen
,
13300 score
, 0, FALSE
, slang
, FALSE
);
13304 /* Add a penalty for words in another region. */
13305 if ((flags
& WF_REGION
)
13306 && (((unsigned)flags
>> 16) & lp
->lp_region
) == 0)
13307 goodscore
= SCORE_REGION
;
13311 /* Add a small penalty for changing the first letter from
13312 * lower to upper case. Helps for "tath" -> "Kath", which is
13313 * less common thatn "tath" -> "path". Don't do it when the
13314 * letter is the same, that has already been counted. */
13316 if (SPELL_ISUPPER(gc
))
13318 bc
= PTR2CHAR(su
->su_badword
);
13319 if (!SPELL_ISUPPER(bc
)
13320 && SPELL_TOFOLD(bc
) != SPELL_TOFOLD(gc
))
13321 goodscore
+= SCORE_ICASE
/ 2;
13324 /* Compute the score for the good word. This only does letter
13325 * insert/delete/swap/replace. REP items are not considered,
13326 * which may make the score a bit higher.
13327 * Use a limit for the score to make it work faster. Use
13328 * MAXSCORE(), because RESCORE() will change the score.
13329 * If the limit is very high then the iterative method is
13330 * inefficient, using an array is quicker. */
13331 limit
= MAXSCORE(su
->su_sfmaxscore
- goodscore
, score
);
13332 if (limit
> SCORE_LIMITMAX
)
13333 goodscore
+= spell_edit_score(slang
, su
->su_badword
, p
);
13335 goodscore
+= spell_edit_score_limit(slang
, su
->su_badword
,
13338 /* When going over the limit don't bother to do the rest. */
13339 if (goodscore
< SCORE_MAXMAX
)
13341 /* Give a bonus to words seen before. */
13342 goodscore
= score_wordcount_adj(slang
, goodscore
, p
, FALSE
);
13344 /* Add the suggestion if the score isn't too bad. */
13345 goodscore
= RESCORE(goodscore
, score
);
13346 if (goodscore
<= su
->su_sfmaxscore
)
13347 add_suggestion(su
, &su
->su_ga
, p
, su
->su_badlen
,
13348 goodscore
, score
, TRUE
, slang
, TRUE
);
13352 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
13357 * Find word "word" in fold-case tree for "slang" and return the word number.
13360 soundfold_find(slang
, word
)
13368 char_u
*ptr
= word
;
13373 byts
= slang
->sl_sbyts
;
13374 idxs
= slang
->sl_sidxs
;
13378 /* First byte is the number of possible bytes. */
13379 len
= byts
[arridx
++];
13381 /* If the first possible byte is a zero the word could end here.
13382 * If the word ends we found the word. If not skip the NUL bytes. */
13384 if (byts
[arridx
] == NUL
)
13389 /* Skip over the zeros, there can be several. */
13390 while (len
> 0 && byts
[arridx
] == NUL
)
13396 return -1; /* no children, word should have ended here */
13400 /* If the word ends we didn't find it. */
13404 /* Perform a binary search in the list of accepted bytes. */
13405 if (c
== TAB
) /* <Tab> is handled like <Space> */
13407 while (byts
[arridx
] < c
)
13409 /* The word count is in the first idxs[] entry of the child. */
13410 wordnr
+= idxs
[idxs
[arridx
]];
13412 if (--len
== 0) /* end of the bytes, didn't find it */
13415 if (byts
[arridx
] != c
) /* didn't find the byte */
13418 /* Continue at the child (if there is one). */
13419 arridx
= idxs
[arridx
];
13422 /* One space in the good word may stand for several spaces in the
13425 while (ptr
[wlen
] == ' ' || ptr
[wlen
] == TAB
)
13433 * Copy "fword" to "cword", fixing case according to "flags".
13436 make_case_word(fword
, cword
, flags
)
13441 if (flags
& WF_ALLCAP
)
13442 /* Make it all upper-case */
13443 allcap_copy(fword
, cword
);
13444 else if (flags
& WF_ONECAP
)
13445 /* Make the first letter upper-case */
13446 onecap_copy(fword
, cword
, TRUE
);
13448 /* Use goodword as-is. */
13449 STRCPY(cword
, fword
);
13453 * Use map string "map" for languages "lp".
13456 set_map_str(lp
, map
)
13467 lp
->sl_has_map
= FALSE
;
13470 lp
->sl_has_map
= TRUE
;
13472 /* Init the array and hash tables empty. */
13473 for (i
= 0; i
< 256; ++i
)
13474 lp
->sl_map_array
[i
] = 0;
13476 hash_init(&lp
->sl_map_hash
);
13480 * The similar characters are stored separated with slashes:
13481 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13482 * before the same slash. For characters above 255 sl_map_hash is used.
13484 for (p
= map
; *p
!= NUL
; )
13487 c
= mb_cptr2char_adv(&p
);
13499 /* Characters above 255 don't fit in sl_map_array[], put them in
13500 * the hash table. Each entry is the char, a NUL the headchar and
13504 int cl
= mb_char2len(c
);
13505 int headcl
= mb_char2len(headc
);
13510 b
= alloc((unsigned)(cl
+ headcl
+ 2));
13513 mb_char2bytes(c
, b
);
13515 mb_char2bytes(headc
, b
+ cl
+ 1);
13516 b
[cl
+ 1 + headcl
] = NUL
;
13517 hash
= hash_hash(b
);
13518 hi
= hash_lookup(&lp
->sl_map_hash
, b
, hash
);
13519 if (HASHITEM_EMPTY(hi
))
13520 hash_add_item(&lp
->sl_map_hash
, hi
, b
, hash
);
13523 /* This should have been checked when generating the .spl
13525 EMSG(_("E783: duplicate char in MAP entry"));
13531 lp
->sl_map_array
[c
] = headc
;
13537 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13538 * lines in the .aff file.
13541 similar_chars(slang
, c1
, c2
)
13548 char_u buf
[MB_MAXBYTES
];
13553 buf
[mb_char2bytes(c1
, buf
)] = 0;
13554 hi
= hash_find(&slang
->sl_map_hash
, buf
);
13555 if (HASHITEM_EMPTY(hi
))
13558 m1
= mb_ptr2char(hi
->hi_key
+ STRLEN(hi
->hi_key
) + 1);
13562 m1
= slang
->sl_map_array
[c1
];
13570 buf
[mb_char2bytes(c2
, buf
)] = 0;
13571 hi
= hash_find(&slang
->sl_map_hash
, buf
);
13572 if (HASHITEM_EMPTY(hi
))
13575 m2
= mb_ptr2char(hi
->hi_key
+ STRLEN(hi
->hi_key
) + 1);
13579 m2
= slang
->sl_map_array
[c2
];
13585 * Add a suggestion to the list of suggestions.
13586 * For a suggestion that is already in the list the lowest score is remembered.
13589 add_suggestion(su
, gap
, goodword
, badlenarg
, score
, altscore
, had_bonus
,
13592 garray_T
*gap
; /* either su_ga or su_sga */
13594 int badlenarg
; /* len of bad word replaced with "goodword" */
13597 int had_bonus
; /* value for st_had_bonus */
13598 slang_T
*slang
; /* language for sound folding */
13599 int maxsf
; /* su_maxscore applies to soundfold score,
13600 su_sfmaxscore to the total score. */
13602 int goodlen
; /* len of goodword changed */
13603 int badlen
; /* len of bad word changed */
13607 char_u
*pgood
, *pbad
;
13609 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13610 * "thee the" is added next to changing the first "the" the "thee". */
13611 pgood
= goodword
+ STRLEN(goodword
);
13612 pbad
= su
->su_badptr
+ badlenarg
;
13615 goodlen
= (int)(pgood
- goodword
);
13616 badlen
= (int)(pbad
- su
->su_badptr
);
13617 if (goodlen
<= 0 || badlen
<= 0)
13619 mb_ptr_back(goodword
, pgood
);
13620 mb_ptr_back(su
->su_badptr
, pbad
);
13624 if (mb_ptr2char(pgood
) != mb_ptr2char(pbad
))
13629 if (*pgood
!= *pbad
)
13633 if (badlen
== 0 && goodlen
== 0)
13634 /* goodword doesn't change anything; may happen for "the the" changing
13635 * the first "the" to itself. */
13638 if (gap
->ga_len
== 0)
13642 /* Check if the word is already there. Also check the length that is
13643 * being replaced "thes," -> "these" is a different suggestion from
13644 * "thes" -> "these". */
13645 stp
= &SUG(*gap
, 0);
13646 for (i
= gap
->ga_len
; --i
>= 0; ++stp
)
13647 if (stp
->st_wordlen
== goodlen
13648 && stp
->st_orglen
== badlen
13649 && STRNCMP(stp
->st_word
, goodword
, goodlen
) == 0)
13652 * Found it. Remember the word with the lowest score.
13654 if (stp
->st_slang
== NULL
)
13655 stp
->st_slang
= slang
;
13657 new_sug
.st_score
= score
;
13658 new_sug
.st_altscore
= altscore
;
13659 new_sug
.st_had_bonus
= had_bonus
;
13661 if (stp
->st_had_bonus
!= had_bonus
)
13663 /* Only one of the two had the soundalike score computed.
13664 * Need to do that for the other one now, otherwise the
13665 * scores can't be compared. This happens because
13666 * suggest_try_change() doesn't compute the soundalike
13667 * word to keep it fast, while some special methods set
13668 * the soundalike score to zero. */
13670 rescore_one(su
, stp
);
13673 new_sug
.st_word
= stp
->st_word
;
13674 new_sug
.st_wordlen
= stp
->st_wordlen
;
13675 new_sug
.st_slang
= stp
->st_slang
;
13676 new_sug
.st_orglen
= badlen
;
13677 rescore_one(su
, &new_sug
);
13681 if (stp
->st_score
> new_sug
.st_score
)
13683 stp
->st_score
= new_sug
.st_score
;
13684 stp
->st_altscore
= new_sug
.st_altscore
;
13685 stp
->st_had_bonus
= new_sug
.st_had_bonus
;
13691 if (i
< 0 && ga_grow(gap
, 1) == OK
)
13693 /* Add a suggestion. */
13694 stp
= &SUG(*gap
, gap
->ga_len
);
13695 stp
->st_word
= vim_strnsave(goodword
, goodlen
);
13696 if (stp
->st_word
!= NULL
)
13698 stp
->st_wordlen
= goodlen
;
13699 stp
->st_score
= score
;
13700 stp
->st_altscore
= altscore
;
13701 stp
->st_had_bonus
= had_bonus
;
13702 stp
->st_orglen
= badlen
;
13703 stp
->st_slang
= slang
;
13706 /* If we have too many suggestions now, sort the list and keep
13707 * the best suggestions. */
13708 if (gap
->ga_len
> SUG_MAX_COUNT(su
))
13711 su
->su_sfmaxscore
= cleanup_suggestions(gap
,
13712 su
->su_sfmaxscore
, SUG_CLEAN_COUNT(su
));
13715 i
= su
->su_maxscore
;
13716 su
->su_maxscore
= cleanup_suggestions(gap
,
13717 su
->su_maxscore
, SUG_CLEAN_COUNT(su
));
13725 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13726 * for split words, such as "the the". Remove these from the list here.
13729 check_suggestions(su
, gap
)
13731 garray_T
*gap
; /* either su_ga or su_sga */
13735 char_u longword
[MAXWLEN
+ 1];
13739 stp
= &SUG(*gap
, 0);
13740 for (i
= gap
->ga_len
- 1; i
>= 0; --i
)
13742 /* Need to append what follows to check for "the the". */
13743 STRCPY(longword
, stp
[i
].st_word
);
13744 len
= stp
[i
].st_wordlen
;
13745 vim_strncpy(longword
+ len
, su
->su_badptr
+ stp
[i
].st_orglen
,
13748 (void)spell_check(curwin
, longword
, &attr
, NULL
, FALSE
);
13749 if (attr
!= HLF_COUNT
)
13751 /* Remove this entry. */
13752 vim_free(stp
[i
].st_word
);
13754 if (i
< gap
->ga_len
)
13755 mch_memmove(stp
+ i
, stp
+ i
+ 1,
13756 sizeof(suggest_T
) * (gap
->ga_len
- i
));
13763 * Add a word to be banned.
13766 add_banned(su
, word
)
13774 hash
= hash_hash(word
);
13775 hi
= hash_lookup(&su
->su_banned
, word
, hash
);
13776 if (HASHITEM_EMPTY(hi
))
13778 s
= vim_strsave(word
);
13780 hash_add_item(&su
->su_banned
, hi
, s
, hash
);
13785 * Recompute the score for all suggestions if sound-folding is possible. This
13786 * is slow, thus only done for the final results.
13789 rescore_suggestions(su
)
13794 if (su
->su_sallang
!= NULL
)
13795 for (i
= 0; i
< su
->su_ga
.ga_len
; ++i
)
13796 rescore_one(su
, &SUG(su
->su_ga
, i
));
13800 * Recompute the score for one suggestion if sound-folding is possible.
13803 rescore_one(su
, stp
)
13807 slang_T
*slang
= stp
->st_slang
;
13808 char_u sal_badword
[MAXWLEN
];
13811 /* Only rescore suggestions that have no sal score yet and do have a
13813 if (slang
!= NULL
&& slang
->sl_sal
.ga_len
> 0 && !stp
->st_had_bonus
)
13815 if (slang
== su
->su_sallang
)
13816 p
= su
->su_sal_badword
;
13819 spell_soundfold(slang
, su
->su_fbadword
, TRUE
, sal_badword
);
13823 stp
->st_altscore
= stp_sal_score(stp
, su
, slang
, p
);
13824 if (stp
->st_altscore
== SCORE_MAXMAX
)
13825 stp
->st_altscore
= SCORE_BIG
;
13826 stp
->st_score
= RESCORE(stp
->st_score
, stp
->st_altscore
);
13827 stp
->st_had_bonus
= TRUE
;
13832 #ifdef __BORLANDC__
13835 sug_compare
__ARGS((const void *s1
, const void *s2
));
13838 * Function given to qsort() to sort the suggestions on st_score.
13839 * First on "st_score", then "st_altscore" then alphabetically.
13842 #ifdef __BORLANDC__
13845 sug_compare(s1
, s2
)
13849 suggest_T
*p1
= (suggest_T
*)s1
;
13850 suggest_T
*p2
= (suggest_T
*)s2
;
13851 int n
= p1
->st_score
- p2
->st_score
;
13855 n
= p1
->st_altscore
- p2
->st_altscore
;
13857 n
= STRICMP(p1
->st_word
, p2
->st_word
);
13863 * Cleanup the suggestions:
13865 * - Remove words that won't be displayed.
13866 * Returns the maximum score in the list or "maxscore" unmodified.
13869 cleanup_suggestions(gap
, maxscore
, keep
)
13872 int keep
; /* nr of suggestions to keep */
13874 suggest_T
*stp
= &SUG(*gap
, 0);
13877 /* Sort the list. */
13878 qsort(gap
->ga_data
, (size_t)gap
->ga_len
, sizeof(suggest_T
), sug_compare
);
13880 /* Truncate the list to the number of suggestions that will be displayed. */
13881 if (gap
->ga_len
> keep
)
13883 for (i
= keep
; i
< gap
->ga_len
; ++i
)
13884 vim_free(stp
[i
].st_word
);
13885 gap
->ga_len
= keep
;
13886 return stp
[keep
- 1].st_score
;
13891 #if defined(FEAT_EVAL) || defined(PROTO)
13893 * Soundfold a string, for soundfold().
13894 * Result is in allocated memory, NULL for an error.
13897 eval_soundfold(word
)
13901 char_u sound
[MAXWLEN
];
13904 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
13905 /* Use the sound-folding of the first language that supports it. */
13906 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
; ++lpi
)
13908 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
13909 if (lp
->lp_slang
->sl_sal
.ga_len
> 0)
13911 /* soundfold the word */
13912 spell_soundfold(lp
->lp_slang
, word
, FALSE
, sound
);
13913 return vim_strsave(sound
);
13917 /* No language with sound folding, return word as-is. */
13918 return vim_strsave(word
);
13923 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
13925 * There are many ways to turn a word into a sound-a-like representation. The
13926 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13927 * swedish name matching - survey and test of different algorithms" by Klas
13930 * We support two methods:
13931 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13932 * 2. SAL items define a more advanced sound-folding (and much slower).
13935 spell_soundfold(slang
, inword
, folded
, res
)
13938 int folded
; /* "inword" is already case-folded */
13941 char_u fword
[MAXWLEN
];
13944 if (slang
->sl_sofo
)
13945 /* SOFOFROM and SOFOTO used */
13946 spell_soundfold_sofo(slang
, inword
, res
);
13949 /* SAL items used. Requires the word to be case-folded. */
13954 (void)spell_casefold(inword
, (int)STRLEN(inword
), fword
, MAXWLEN
);
13960 spell_soundfold_wsal(slang
, word
, res
);
13963 spell_soundfold_sal(slang
, word
, res
);
13968 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13972 spell_soundfold_sofo(slang
, inword
, res
)
13987 /* The sl_sal_first[] table contains the translation for chars up to
13988 * 255, sl_sal the rest. */
13989 for (s
= inword
; *s
!= NUL
; )
13991 c
= mb_cptr2char_adv(&s
);
13992 if (enc_utf8
? utf_class(c
) == 0 : vim_iswhite(c
))
13995 c
= slang
->sl_sal_first
[c
];
13998 ip
= ((int **)slang
->sl_sal
.ga_data
)[c
& 0xff];
13999 if (ip
== NULL
) /* empty list, can't match */
14002 for (;;) /* find "c" in the list */
14004 if (*ip
== 0) /* not found */
14009 if (*ip
== c
) /* match! */
14018 if (c
!= NUL
&& c
!= prevc
)
14020 ri
+= mb_char2bytes(c
, res
+ ri
);
14021 if (ri
+ MB_MAXBYTES
> MAXWLEN
)
14030 /* The sl_sal_first[] table contains the translation. */
14031 for (s
= inword
; (c
= *s
) != NUL
; ++s
)
14033 if (vim_iswhite(c
))
14036 c
= slang
->sl_sal_first
[c
];
14037 if (c
!= NUL
&& (ri
== 0 || res
[ri
- 1] != c
))
14046 spell_soundfold_sal(slang
, inword
, res
)
14052 char_u word
[MAXWLEN
];
14053 char_u
*s
= inword
;
14067 /* Remove accents, if wanted. We actually remove all non-word characters.
14068 * But keep white space. We need a copy, the word may be changed here. */
14069 if (slang
->sl_rem_accents
)
14074 if (vim_iswhite(*s
))
14081 if (spell_iswordp_nmw(s
))
14091 smp
= (salitem_T
*)slang
->sl_sal
.ga_data
;
14094 * This comes from Aspell phonet.cpp. Converted from C++ to C.
14095 * Changed to keep spaces.
14097 i
= reslen
= z
= 0;
14098 while ((c
= word
[i
]) != NUL
)
14100 /* Start with the first rule that has the character in the word. */
14101 n
= slang
->sl_sal_first
[c
];
14106 /* check all rules for the same letter */
14107 for (; (s
= smp
[n
].sm_lead
)[0] == c
; ++n
)
14109 /* Quickly skip entries that don't match the word. Most
14110 * entries are less then three chars, optimize for that. */
14111 k
= smp
[n
].sm_leadlen
;
14114 if (word
[i
+ 1] != s
[1])
14118 for (j
= 2; j
< k
; ++j
)
14119 if (word
[i
+ j
] != s
[j
])
14126 if ((pf
= smp
[n
].sm_oneof
) != NULL
)
14128 /* Check for match with one of the chars in "sm_oneof". */
14129 while (*pf
!= NUL
&& *pf
!= word
[i
+ k
])
14135 s
= smp
[n
].sm_rules
;
14136 pri
= 5; /* default priority */
14140 while (*s
== '-' && k
> 1)
14147 if (VIM_ISDIGIT(*s
))
14149 /* determine priority */
14153 if (*s
== '^' && *(s
+ 1) == '^')
14158 && (i
== 0 || !(word
[i
- 1] == ' '
14159 || spell_iswordp(word
+ i
- 1, curbuf
)))
14160 && (*(s
+ 1) != '$'
14161 || (!spell_iswordp(word
+ i
+ k0
, curbuf
))))
14162 || (*s
== '$' && i
> 0
14163 && spell_iswordp(word
+ i
- 1, curbuf
)
14164 && (!spell_iswordp(word
+ i
+ k0
, curbuf
))))
14166 /* search for followup rules, if: */
14167 /* followup and k > 1 and NO '-' in searchstring */
14168 c0
= word
[i
+ k
- 1];
14169 n0
= slang
->sl_sal_first
[c0
];
14171 if (slang
->sl_followup
&& k
> 1 && n0
>= 0
14172 && p0
!= '-' && word
[i
+ k
] != NUL
)
14174 /* test follow-up rule for "word[i + k]" */
14175 for ( ; (s
= smp
[n0
].sm_lead
)[0] == c0
; ++n0
)
14177 /* Quickly skip entries that don't match the word.
14179 k0
= smp
[n0
].sm_leadlen
;
14182 if (word
[i
+ k
] != s
[1])
14186 pf
= word
+ i
+ k
+ 1;
14187 for (j
= 2; j
< k0
; ++j
)
14196 if ((pf
= smp
[n0
].sm_oneof
) != NULL
)
14198 /* Check for match with one of the chars in
14200 while (*pf
!= NUL
&& *pf
!= word
[i
+ k0
])
14208 s
= smp
[n0
].sm_rules
;
14211 /* "k0" gets NOT reduced because
14212 * "if (k0 == k)" */
14217 if (VIM_ISDIGIT(*s
))
14224 /* *s == '^' cuts */
14226 && !spell_iswordp(word
+ i
+ k0
,
14230 /* this is just a piece of the string */
14234 /* priority too low */
14236 /* rule fits; stop search */
14241 if (p0
>= pri
&& smp
[n0
].sm_lead
[0] == c0
)
14245 /* replace string */
14249 pf
= smp
[n
].sm_rules
;
14250 p0
= (vim_strchr(pf
, '<') != NULL
) ? 1 : 0;
14251 if (p0
== 1 && z
== 0)
14253 /* rule with '<' is used */
14254 if (reslen
> 0 && *s
!= NUL
&& (res
[reslen
- 1] == c
14255 || res
[reslen
- 1] == *s
))
14260 while (*s
!= NUL
&& word
[i
+ k0
] != NUL
)
14267 mch_memmove(word
+ i
+ k0
, word
+ i
+ k
,
14268 STRLEN(word
+ i
+ k
) + 1);
14270 /* new "actual letter" */
14275 /* no '<' rule used */
14278 while (*s
!= NUL
&& s
[1] != NUL
&& reslen
< MAXWLEN
)
14280 if (reslen
== 0 || res
[reslen
- 1] != *s
)
14281 res
[reslen
++] = *s
;
14284 /* new "actual letter" */
14286 if (strstr((char *)pf
, "^^") != NULL
)
14290 mch_memmove(word
, word
+ i
+ 1,
14291 STRLEN(word
+ i
+ 1) + 1);
14300 else if (vim_iswhite(c
))
14308 if (k
&& !p0
&& reslen
< MAXWLEN
&& c
!= NUL
14309 && (!slang
->sl_collapse
|| reslen
== 0
14310 || res
[reslen
- 1] != c
))
14311 /* condense only double letters */
14325 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14326 * Multi-byte version of spell_soundfold().
14329 spell_soundfold_wsal(slang
, inword
, res
)
14334 salitem_T
*smp
= (salitem_T
*)slang
->sl_sal
.ga_data
;
14352 int did_white
= FALSE
;
14355 * Convert the multi-byte string to a wide-character string.
14356 * Remove accents, if wanted. We actually remove all non-word characters.
14357 * But keep white space.
14360 for (s
= inword
; *s
!= NUL
; )
14363 c
= mb_cptr2char_adv(&s
);
14364 if (slang
->sl_rem_accents
)
14366 if (enc_utf8
? utf_class(c
) == 0 : vim_iswhite(c
))
14376 if (!spell_iswordp_nmw(t
))
14385 * This comes from Aspell phonet.cpp.
14386 * Converted from C++ to C. Added support for multi-byte chars.
14387 * Changed to keep spaces.
14389 i
= reslen
= z
= 0;
14390 while ((c
= word
[i
]) != NUL
)
14392 /* Start with the first rule that has the character in the word. */
14393 n
= slang
->sl_sal_first
[c
& 0xff];
14398 /* check all rules for the same index byte */
14399 for (; ((ws
= smp
[n
].sm_lead_w
)[0] & 0xff) == (c
& 0xff); ++n
)
14401 /* Quickly skip entries that don't match the word. Most
14402 * entries are less then three chars, optimize for that. */
14405 k
= smp
[n
].sm_leadlen
;
14408 if (word
[i
+ 1] != ws
[1])
14412 for (j
= 2; j
< k
; ++j
)
14413 if (word
[i
+ j
] != ws
[j
])
14420 if ((pf
= smp
[n
].sm_oneof_w
) != NULL
)
14422 /* Check for match with one of the chars in "sm_oneof". */
14423 while (*pf
!= NUL
&& *pf
!= word
[i
+ k
])
14429 s
= smp
[n
].sm_rules
;
14430 pri
= 5; /* default priority */
14434 while (*s
== '-' && k
> 1)
14441 if (VIM_ISDIGIT(*s
))
14443 /* determine priority */
14447 if (*s
== '^' && *(s
+ 1) == '^')
14452 && (i
== 0 || !(word
[i
- 1] == ' '
14453 || spell_iswordp_w(word
+ i
- 1, curbuf
)))
14454 && (*(s
+ 1) != '$'
14455 || (!spell_iswordp_w(word
+ i
+ k0
, curbuf
))))
14456 || (*s
== '$' && i
> 0
14457 && spell_iswordp_w(word
+ i
- 1, curbuf
)
14458 && (!spell_iswordp_w(word
+ i
+ k0
, curbuf
))))
14460 /* search for followup rules, if: */
14461 /* followup and k > 1 and NO '-' in searchstring */
14462 c0
= word
[i
+ k
- 1];
14463 n0
= slang
->sl_sal_first
[c0
& 0xff];
14465 if (slang
->sl_followup
&& k
> 1 && n0
>= 0
14466 && p0
!= '-' && word
[i
+ k
] != NUL
)
14468 /* Test follow-up rule for "word[i + k]"; loop over
14469 * all entries with the same index byte. */
14470 for ( ; ((ws
= smp
[n0
].sm_lead_w
)[0] & 0xff)
14471 == (c0
& 0xff); ++n0
)
14473 /* Quickly skip entries that don't match the word.
14477 k0
= smp
[n0
].sm_leadlen
;
14480 if (word
[i
+ k
] != ws
[1])
14484 pf
= word
+ i
+ k
+ 1;
14485 for (j
= 2; j
< k0
; ++j
)
14486 if (*pf
++ != ws
[j
])
14494 if ((pf
= smp
[n0
].sm_oneof_w
) != NULL
)
14496 /* Check for match with one of the chars in
14498 while (*pf
!= NUL
&& *pf
!= word
[i
+ k0
])
14506 s
= smp
[n0
].sm_rules
;
14509 /* "k0" gets NOT reduced because
14510 * "if (k0 == k)" */
14515 if (VIM_ISDIGIT(*s
))
14522 /* *s == '^' cuts */
14524 && !spell_iswordp_w(word
+ i
+ k0
,
14528 /* this is just a piece of the string */
14532 /* priority too low */
14534 /* rule fits; stop search */
14539 if (p0
>= pri
&& (smp
[n0
].sm_lead_w
[0] & 0xff)
14544 /* replace string */
14545 ws
= smp
[n
].sm_to_w
;
14546 s
= smp
[n
].sm_rules
;
14547 p0
= (vim_strchr(s
, '<') != NULL
) ? 1 : 0;
14548 if (p0
== 1 && z
== 0)
14550 /* rule with '<' is used */
14551 if (reslen
> 0 && ws
!= NULL
&& *ws
!= NUL
14552 && (wres
[reslen
- 1] == c
14553 || wres
[reslen
- 1] == *ws
))
14559 while (*ws
!= NUL
&& word
[i
+ k0
] != NUL
)
14561 word
[i
+ k0
] = *ws
;
14566 mch_memmove(word
+ i
+ k0
, word
+ i
+ k
,
14567 sizeof(int) * (STRLEN(word
+ i
+ k
) + 1));
14569 /* new "actual letter" */
14574 /* no '<' rule used */
14578 while (*ws
!= NUL
&& ws
[1] != NUL
14579 && reslen
< MAXWLEN
)
14581 if (reslen
== 0 || wres
[reslen
- 1] != *ws
)
14582 wres
[reslen
++] = *ws
;
14585 /* new "actual letter" */
14590 if (strstr((char *)s
, "^^") != NULL
)
14593 wres
[reslen
++] = c
;
14594 mch_memmove(word
, word
+ i
+ 1,
14595 sizeof(int) * (STRLEN(word
+ i
+ 1) + 1));
14604 else if (vim_iswhite(c
))
14612 if (k
&& !p0
&& reslen
< MAXWLEN
&& c
!= NUL
14613 && (!slang
->sl_collapse
|| reslen
== 0
14614 || wres
[reslen
- 1] != c
))
14615 /* condense only double letters */
14616 wres
[reslen
++] = c
;
14624 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14626 for (n
= 0; n
< reslen
; ++n
)
14628 l
+= mb_char2bytes(wres
[n
], res
+ l
);
14629 if (l
+ MB_MAXBYTES
> MAXWLEN
)
14637 * Compute a score for two sound-a-like words.
14638 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14639 * Instead of a generic loop we write out the code. That keeps it fast by
14640 * avoiding checks that will not be possible.
14643 soundalike_score(goodstart
, badstart
)
14644 char_u
*goodstart
; /* sound-folded good word */
14645 char_u
*badstart
; /* sound-folded bad word */
14647 char_u
*goodsound
= goodstart
;
14648 char_u
*badsound
= badstart
;
14656 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14657 * counted so much, vowels halfway the word aren't counted at all. */
14658 if ((*badsound
== '*' || *goodsound
== '*') && *badsound
!= *goodsound
)
14660 if (badsound
[1] == goodsound
[1]
14661 || (badsound
[1] != NUL
14662 && goodsound
[1] != NUL
14663 && badsound
[2] == goodsound
[2]))
14665 /* handle like a substitute */
14669 score
= 2 * SCORE_DEL
/ 3;
14670 if (*badsound
== '*')
14677 goodlen
= (int)STRLEN(goodsound
);
14678 badlen
= (int)STRLEN(badsound
);
14680 /* Return quickly if the lengths are too different to be fixed by two
14682 n
= goodlen
- badlen
;
14683 if (n
< -2 || n
> 2)
14684 return SCORE_MAXMAX
;
14688 pl
= goodsound
; /* goodsound is longest */
14693 pl
= badsound
; /* badsound is longest */
14697 /* Skip over the identical part. */
14698 while (*pl
== *ps
&& *pl
!= NUL
)
14709 * Must delete two characters from "pl".
14711 ++pl
; /* first delete */
14717 /* strings must be equal after second delete */
14718 if (STRCMP(pl
+ 1, ps
) == 0)
14719 return score
+ SCORE_DEL
* 2;
14721 /* Failed to compare. */
14727 * Minimal one delete from "pl" required.
14733 while (*pl2
== *ps2
)
14735 if (*pl2
== NUL
) /* reached the end */
14736 return score
+ SCORE_DEL
;
14741 /* 2: delete then swap, then rest must be equal */
14742 if (pl2
[0] == ps2
[1] && pl2
[1] == ps2
[0]
14743 && STRCMP(pl2
+ 2, ps2
+ 2) == 0)
14744 return score
+ SCORE_DEL
+ SCORE_SWAP
;
14746 /* 3: delete then substitute, then the rest must be equal */
14747 if (STRCMP(pl2
+ 1, ps2
+ 1) == 0)
14748 return score
+ SCORE_DEL
+ SCORE_SUBST
;
14750 /* 4: first swap then delete */
14751 if (pl
[0] == ps
[1] && pl
[1] == ps
[0])
14753 pl2
= pl
+ 2; /* swap, skip two chars */
14755 while (*pl2
== *ps2
)
14760 /* delete a char and then strings must be equal */
14761 if (STRCMP(pl2
+ 1, ps2
) == 0)
14762 return score
+ SCORE_SWAP
+ SCORE_DEL
;
14765 /* 5: first substitute then delete */
14766 pl2
= pl
+ 1; /* substitute, skip one char */
14768 while (*pl2
== *ps2
)
14773 /* delete a char and then strings must be equal */
14774 if (STRCMP(pl2
+ 1, ps2
) == 0)
14775 return score
+ SCORE_SUBST
+ SCORE_DEL
;
14777 /* Failed to compare. */
14782 * Lenghts are equal, thus changes must result in same length: An
14783 * insert is only possible in combination with a delete.
14784 * 1: check if for identical strings
14790 if (pl
[0] == ps
[1] && pl
[1] == ps
[0])
14792 pl2
= pl
+ 2; /* swap, skip two chars */
14794 while (*pl2
== *ps2
)
14796 if (*pl2
== NUL
) /* reached the end */
14797 return score
+ SCORE_SWAP
;
14801 /* 3: swap and swap again */
14802 if (pl2
[0] == ps2
[1] && pl2
[1] == ps2
[0]
14803 && STRCMP(pl2
+ 2, ps2
+ 2) == 0)
14804 return score
+ SCORE_SWAP
+ SCORE_SWAP
;
14806 /* 4: swap and substitute */
14807 if (STRCMP(pl2
+ 1, ps2
+ 1) == 0)
14808 return score
+ SCORE_SWAP
+ SCORE_SUBST
;
14811 /* 5: substitute */
14814 while (*pl2
== *ps2
)
14816 if (*pl2
== NUL
) /* reached the end */
14817 return score
+ SCORE_SUBST
;
14822 /* 6: substitute and swap */
14823 if (pl2
[0] == ps2
[1] && pl2
[1] == ps2
[0]
14824 && STRCMP(pl2
+ 2, ps2
+ 2) == 0)
14825 return score
+ SCORE_SUBST
+ SCORE_SWAP
;
14827 /* 7: substitute and substitute */
14828 if (STRCMP(pl2
+ 1, ps2
+ 1) == 0)
14829 return score
+ SCORE_SUBST
+ SCORE_SUBST
;
14831 /* 8: insert then delete */
14834 while (*pl2
== *ps2
)
14839 if (STRCMP(pl2
+ 1, ps2
) == 0)
14840 return score
+ SCORE_INS
+ SCORE_DEL
;
14842 /* 9: delete then insert */
14845 while (*pl2
== *ps2
)
14850 if (STRCMP(pl2
, ps2
+ 1) == 0)
14851 return score
+ SCORE_INS
+ SCORE_DEL
;
14853 /* Failed to compare. */
14857 return SCORE_MAXMAX
;
14861 * Compute the "edit distance" to turn "badword" into "goodword". The less
14862 * deletes/inserts/substitutes/swaps are required the lower the score.
14864 * The algorithm is described by Du and Chang, 1992.
14865 * The implementation of the algorithm comes from Aspell editdist.cpp,
14866 * edit_distance(). It has been converted from C++ to C and modified to
14867 * support multi-byte characters.
14870 spell_edit_score(slang
, badword
, goodword
)
14876 int badlen
, goodlen
; /* lengths including NUL */
14883 int wbadword
[MAXWLEN
];
14884 int wgoodword
[MAXWLEN
];
14888 /* Get the characters from the multi-byte strings and put them in an
14889 * int array for easy access. */
14890 for (p
= badword
, badlen
= 0; *p
!= NUL
; )
14891 wbadword
[badlen
++] = mb_cptr2char_adv(&p
);
14892 wbadword
[badlen
++] = 0;
14893 for (p
= goodword
, goodlen
= 0; *p
!= NUL
; )
14894 wgoodword
[goodlen
++] = mb_cptr2char_adv(&p
);
14895 wgoodword
[goodlen
++] = 0;
14900 badlen
= (int)STRLEN(badword
) + 1;
14901 goodlen
= (int)STRLEN(goodword
) + 1;
14904 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14905 #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
14906 cnt
= (int *)lalloc((long_u
)(sizeof(int) * (badlen
+ 1) * (goodlen
+ 1)),
14909 return 0; /* out of memory */
14912 for (j
= 1; j
<= goodlen
; ++j
)
14913 CNT(0, j
) = CNT(0, j
- 1) + SCORE_INS
;
14915 for (i
= 1; i
<= badlen
; ++i
)
14917 CNT(i
, 0) = CNT(i
- 1, 0) + SCORE_DEL
;
14918 for (j
= 1; j
<= goodlen
; ++j
)
14923 bc
= wbadword
[i
- 1];
14924 gc
= wgoodword
[j
- 1];
14929 bc
= badword
[i
- 1];
14930 gc
= goodword
[j
- 1];
14933 CNT(i
, j
) = CNT(i
- 1, j
- 1);
14936 /* Use a better score when there is only a case difference. */
14937 if (SPELL_TOFOLD(bc
) == SPELL_TOFOLD(gc
))
14938 CNT(i
, j
) = SCORE_ICASE
+ CNT(i
- 1, j
- 1);
14941 /* For a similar character use SCORE_SIMILAR. */
14943 && slang
->sl_has_map
14944 && similar_chars(slang
, gc
, bc
))
14945 CNT(i
, j
) = SCORE_SIMILAR
+ CNT(i
- 1, j
- 1);
14947 CNT(i
, j
) = SCORE_SUBST
+ CNT(i
- 1, j
- 1);
14950 if (i
> 1 && j
> 1)
14955 pbc
= wbadword
[i
- 2];
14956 pgc
= wgoodword
[j
- 2];
14961 pbc
= badword
[i
- 2];
14962 pgc
= goodword
[j
- 2];
14964 if (bc
== pgc
&& pbc
== gc
)
14966 t
= SCORE_SWAP
+ CNT(i
- 2, j
- 2);
14971 t
= SCORE_DEL
+ CNT(i
- 1, j
);
14974 t
= SCORE_INS
+ CNT(i
, j
- 1);
14981 i
= CNT(badlen
- 1, goodlen
- 1);
14994 * Like spell_edit_score(), but with a limit on the score to make it faster.
14995 * May return SCORE_MAXMAX when the score is higher than "limit".
14997 * This uses a stack for the edits still to be tried.
14998 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14999 * for multi-byte characters.
15002 spell_edit_score_limit(slang
, badword
, goodword
, limit
)
15008 limitscore_T stack
[10]; /* allow for over 3 * 2 edits */
15019 /* Multi-byte characters require a bit more work, use a different function
15020 * to avoid testing "has_mbyte" quite often. */
15022 return spell_edit_score_limit_w(slang
, badword
, goodword
, limit
);
15026 * The idea is to go from start to end over the words. So long as
15027 * characters are equal just continue, this always gives the lowest score.
15028 * When there is a difference try several alternatives. Each alternative
15029 * increases "score" for the edit distance. Some of the alternatives are
15030 * pushed unto a stack and tried later, some are tried right away. At the
15031 * end of the word the score for one alternative is known. The lowest
15032 * possible score is stored in "minscore".
15038 minscore
= limit
+ 1;
15042 /* Skip over an equal part, score remains the same. */
15047 if (bc
!= gc
) /* stop at a char that's different */
15049 if (bc
== NUL
) /* both words end */
15051 if (score
< minscore
)
15053 goto pop
; /* do next alternative */
15059 if (gc
== NUL
) /* goodword ends, delete badword chars */
15063 if ((score
+= SCORE_DEL
) >= minscore
)
15064 goto pop
; /* do next alternative */
15065 } while (badword
[++bi
] != NUL
);
15068 else if (bc
== NUL
) /* badword ends, insert badword chars */
15072 if ((score
+= SCORE_INS
) >= minscore
)
15073 goto pop
; /* do next alternative */
15074 } while (goodword
[++gi
] != NUL
);
15077 else /* both words continue */
15079 /* If not close to the limit, perform a change. Only try changes
15080 * that may lead to a lower score than "minscore".
15081 * round 0: try deleting a char from badword
15082 * round 1: try inserting a char in badword */
15083 for (round
= 0; round
<= 1; ++round
)
15085 score_off
= score
+ (round
== 0 ? SCORE_DEL
: SCORE_INS
);
15086 if (score_off
< minscore
)
15088 if (score_off
+ SCORE_EDIT_MIN
>= minscore
)
15090 /* Near the limit, rest of the words must match. We
15091 * can check that right now, no need to push an item
15092 * onto the stack. */
15093 bi2
= bi
+ 1 - round
;
15095 while (goodword
[gi2
] == badword
[bi2
])
15097 if (goodword
[gi2
] == NUL
)
15099 minscore
= score_off
;
15108 /* try deleting/inserting a character later */
15109 stack
[stackidx
].badi
= bi
+ 1 - round
;
15110 stack
[stackidx
].goodi
= gi
+ round
;
15111 stack
[stackidx
].score
= score_off
;
15117 if (score
+ SCORE_SWAP
< minscore
)
15119 /* If swapping two characters makes a match then the
15120 * substitution is more expensive, thus there is no need to
15122 if (gc
== badword
[bi
+ 1] && bc
== goodword
[gi
+ 1])
15124 /* Swap two characters, that is: skip them. */
15127 score
+= SCORE_SWAP
;
15132 /* Substitute one character for another which is the same
15133 * thing as deleting a character from both goodword and badword.
15134 * Use a better score when there is only a case difference. */
15135 if (SPELL_TOFOLD(bc
) == SPELL_TOFOLD(gc
))
15136 score
+= SCORE_ICASE
;
15139 /* For a similar character use SCORE_SIMILAR. */
15141 && slang
->sl_has_map
15142 && similar_chars(slang
, gc
, bc
))
15143 score
+= SCORE_SIMILAR
;
15145 score
+= SCORE_SUBST
;
15148 if (score
< minscore
)
15150 /* Do the substitution. */
15158 * Get here to try the next alternative, pop it from the stack.
15160 if (stackidx
== 0) /* stack is empty, finished */
15163 /* pop an item from the stack */
15165 gi
= stack
[stackidx
].goodi
;
15166 bi
= stack
[stackidx
].badi
;
15167 score
= stack
[stackidx
].score
;
15170 /* When the score goes over "limit" it may actually be much higher.
15171 * Return a very large number to avoid going below the limit when giving a
15173 if (minscore
> limit
)
15174 return SCORE_MAXMAX
;
15180 * Multi-byte version of spell_edit_score_limit().
15181 * Keep it in sync with the above!
15184 spell_edit_score_limit_w(slang
, badword
, goodword
, limit
)
15190 limitscore_T stack
[10]; /* allow for over 3 * 2 edits */
15200 int wbadword
[MAXWLEN
];
15201 int wgoodword
[MAXWLEN
];
15203 /* Get the characters from the multi-byte strings and put them in an
15204 * int array for easy access. */
15206 for (p
= badword
; *p
!= NUL
; )
15207 wbadword
[bi
++] = mb_cptr2char_adv(&p
);
15208 wbadword
[bi
++] = 0;
15210 for (p
= goodword
; *p
!= NUL
; )
15211 wgoodword
[gi
++] = mb_cptr2char_adv(&p
);
15212 wgoodword
[gi
++] = 0;
15215 * The idea is to go from start to end over the words. So long as
15216 * characters are equal just continue, this always gives the lowest score.
15217 * When there is a difference try several alternatives. Each alternative
15218 * increases "score" for the edit distance. Some of the alternatives are
15219 * pushed unto a stack and tried later, some are tried right away. At the
15220 * end of the word the score for one alternative is known. The lowest
15221 * possible score is stored in "minscore".
15227 minscore
= limit
+ 1;
15231 /* Skip over an equal part, score remains the same. */
15235 gc
= wgoodword
[gi
];
15237 if (bc
!= gc
) /* stop at a char that's different */
15239 if (bc
== NUL
) /* both words end */
15241 if (score
< minscore
)
15243 goto pop
; /* do next alternative */
15249 if (gc
== NUL
) /* goodword ends, delete badword chars */
15253 if ((score
+= SCORE_DEL
) >= minscore
)
15254 goto pop
; /* do next alternative */
15255 } while (wbadword
[++bi
] != NUL
);
15258 else if (bc
== NUL
) /* badword ends, insert badword chars */
15262 if ((score
+= SCORE_INS
) >= minscore
)
15263 goto pop
; /* do next alternative */
15264 } while (wgoodword
[++gi
] != NUL
);
15267 else /* both words continue */
15269 /* If not close to the limit, perform a change. Only try changes
15270 * that may lead to a lower score than "minscore".
15271 * round 0: try deleting a char from badword
15272 * round 1: try inserting a char in badword */
15273 for (round
= 0; round
<= 1; ++round
)
15275 score_off
= score
+ (round
== 0 ? SCORE_DEL
: SCORE_INS
);
15276 if (score_off
< minscore
)
15278 if (score_off
+ SCORE_EDIT_MIN
>= minscore
)
15280 /* Near the limit, rest of the words must match. We
15281 * can check that right now, no need to push an item
15282 * onto the stack. */
15283 bi2
= bi
+ 1 - round
;
15285 while (wgoodword
[gi2
] == wbadword
[bi2
])
15287 if (wgoodword
[gi2
] == NUL
)
15289 minscore
= score_off
;
15298 /* try deleting a character from badword later */
15299 stack
[stackidx
].badi
= bi
+ 1 - round
;
15300 stack
[stackidx
].goodi
= gi
+ round
;
15301 stack
[stackidx
].score
= score_off
;
15307 if (score
+ SCORE_SWAP
< minscore
)
15309 /* If swapping two characters makes a match then the
15310 * substitution is more expensive, thus there is no need to
15312 if (gc
== wbadword
[bi
+ 1] && bc
== wgoodword
[gi
+ 1])
15314 /* Swap two characters, that is: skip them. */
15317 score
+= SCORE_SWAP
;
15322 /* Substitute one character for another which is the same
15323 * thing as deleting a character from both goodword and badword.
15324 * Use a better score when there is only a case difference. */
15325 if (SPELL_TOFOLD(bc
) == SPELL_TOFOLD(gc
))
15326 score
+= SCORE_ICASE
;
15329 /* For a similar character use SCORE_SIMILAR. */
15331 && slang
->sl_has_map
15332 && similar_chars(slang
, gc
, bc
))
15333 score
+= SCORE_SIMILAR
;
15335 score
+= SCORE_SUBST
;
15338 if (score
< minscore
)
15340 /* Do the substitution. */
15348 * Get here to try the next alternative, pop it from the stack.
15350 if (stackidx
== 0) /* stack is empty, finished */
15353 /* pop an item from the stack */
15355 gi
= stack
[stackidx
].goodi
;
15356 bi
= stack
[stackidx
].badi
;
15357 score
= stack
[stackidx
].score
;
15360 /* When the score goes over "limit" it may actually be much higher.
15361 * Return a very large number to avoid going below the limit when giving a
15363 if (minscore
> limit
)
15364 return SCORE_MAXMAX
;
15381 if (no_spell_checking(curwin
))
15385 for (lpi
= 0; lpi
< curbuf
->b_langp
.ga_len
&& !got_int
; ++lpi
)
15387 lp
= LANGP_ENTRY(curbuf
->b_langp
, lpi
);
15388 msg_puts((char_u
*)"file: ");
15389 msg_puts(lp
->lp_slang
->sl_fname
);
15391 p
= lp
->lp_slang
->sl_info
;
15401 #define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15402 #define DUMPFLAG_COUNT 2 /* include word count */
15403 #define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
15404 #define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15405 #define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
15414 buf_T
*buf
= curbuf
;
15416 if (no_spell_checking(curwin
))
15419 /* Create a new empty buffer by splitting the window. */
15420 do_cmdline_cmd((char_u
*)"new");
15421 if (!bufempty() || !buf_valid(buf
))
15424 spell_dump_compl(buf
, NULL
, 0, NULL
, eap
->forceit
? DUMPFLAG_COUNT
: 0);
15426 /* Delete the empty line that we started with. */
15427 if (curbuf
->b_ml
.ml_line_count
> 1)
15428 ml_delete(curbuf
->b_ml
.ml_line_count
, FALSE
);
15430 redraw_later(NOT_VALID
);
15434 * Go through all possible words and:
15435 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15436 * "ic" and "dir" are not used.
15437 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15440 spell_dump_compl(buf
, pat
, ic
, dir
, dumpflags_arg
)
15441 buf_T
*buf
; /* buffer with spell checking */
15442 char_u
*pat
; /* leading part of the word */
15443 int ic
; /* ignore case */
15444 int *dir
; /* direction for adding matches */
15445 int dumpflags_arg
; /* DUMPFLAG_* */
15449 idx_T arridx
[MAXWLEN
];
15451 char_u word
[MAXWLEN
];
15460 char_u
*region_names
= NULL
; /* region names being used */
15461 int do_region
= TRUE
; /* dump region names and numbers */
15464 int dumpflags
= dumpflags_arg
;
15467 /* When ignoring case or when the pattern starts with capital pass this on
15468 * to dump_word(). */
15472 dumpflags
|= DUMPFLAG_ICASE
;
15475 n
= captype(pat
, NULL
);
15476 if (n
== WF_ONECAP
)
15477 dumpflags
|= DUMPFLAG_ONECAP
;
15478 else if (n
== WF_ALLCAP
15480 && (int)STRLEN(pat
) > mb_ptr2len(pat
)
15482 && (int)STRLEN(pat
) > 1
15485 dumpflags
|= DUMPFLAG_ALLCAP
;
15489 /* Find out if we can support regions: All languages must support the same
15490 * regions or none at all. */
15491 for (lpi
= 0; lpi
< buf
->b_langp
.ga_len
; ++lpi
)
15493 lp
= LANGP_ENTRY(buf
->b_langp
, lpi
);
15494 p
= lp
->lp_slang
->sl_regions
;
15497 if (region_names
== NULL
) /* first language with regions */
15499 else if (STRCMP(region_names
, p
) != 0)
15501 do_region
= FALSE
; /* region names are different */
15507 if (do_region
&& region_names
!= NULL
)
15511 vim_snprintf((char *)IObuff
, IOSIZE
, "/regions=%s", region_names
);
15512 ml_append(lnum
++, IObuff
, (colnr_T
)0, FALSE
);
15519 * Loop over all files loaded for the entries in 'spelllang'.
15521 for (lpi
= 0; lpi
< buf
->b_langp
.ga_len
; ++lpi
)
15523 lp
= LANGP_ENTRY(buf
->b_langp
, lpi
);
15524 slang
= lp
->lp_slang
;
15525 if (slang
->sl_fbyts
== NULL
) /* reloading failed */
15530 vim_snprintf((char *)IObuff
, IOSIZE
, "# file: %s", slang
->sl_fname
);
15531 ml_append(lnum
++, IObuff
, (colnr_T
)0, FALSE
);
15534 /* When matching with a pattern and there are no prefixes only use
15535 * parts of the tree that match "pat". */
15536 if (pat
!= NULL
&& slang
->sl_pbyts
== NULL
)
15537 patlen
= (int)STRLEN(pat
);
15541 /* round 1: case-folded tree
15542 * round 2: keep-case tree */
15543 for (round
= 1; round
<= 2; ++round
)
15547 dumpflags
&= ~DUMPFLAG_KEEPCASE
;
15548 byts
= slang
->sl_fbyts
;
15549 idxs
= slang
->sl_fidxs
;
15553 dumpflags
|= DUMPFLAG_KEEPCASE
;
15554 byts
= slang
->sl_kbyts
;
15555 idxs
= slang
->sl_kidxs
;
15558 continue; /* array is empty */
15563 while (depth
>= 0 && !got_int
15564 && (pat
== NULL
|| !compl_interrupted
))
15566 if (curi
[depth
] > byts
[arridx
[depth
]])
15568 /* Done all bytes at this node, go up one level. */
15571 ins_compl_check_keys(50);
15575 /* Do one more byte at this node. */
15576 n
= arridx
[depth
] + curi
[depth
];
15581 /* End of word, deal with the word.
15582 * Don't use keep-case words in the fold-case tree,
15583 * they will appear in the keep-case tree.
15584 * Only use the word when the region matches. */
15585 flags
= (int)idxs
[n
];
15586 if ((round
== 2 || (flags
& WF_KEEPCAP
) == 0)
15587 && (flags
& WF_NEEDCOMP
) == 0
15589 || (flags
& WF_REGION
) == 0
15590 || (((unsigned)flags
>> 16)
15591 & lp
->lp_region
) != 0))
15595 flags
&= ~WF_REGION
;
15597 /* Dump the basic word if there is no prefix or
15598 * when it's the first one. */
15599 c
= (unsigned)flags
>> 24;
15600 if (c
== 0 || curi
[depth
] == 2)
15602 dump_word(slang
, word
, pat
, dir
,
15603 dumpflags
, flags
, lnum
);
15608 /* Apply the prefix, if there is one. */
15610 lnum
= dump_prefixes(slang
, word
, pat
, dir
,
15611 dumpflags
, flags
, lnum
);
15616 /* Normal char, go one level deeper. */
15618 arridx
[depth
] = idxs
[n
];
15621 /* Check if this characters matches with the pattern.
15622 * If not skip the whole tree below it.
15623 * Always ignore case here, dump_word() will check
15624 * proper case later. This isn't exactly right when
15625 * length changes for multi-byte characters with
15626 * ignore case... */
15627 if (depth
<= patlen
15628 && MB_STRNICMP(word
, pat
, depth
) != 0)
15638 * Dump one word: apply case modifications and append a line to the buffer.
15639 * When "lnum" is zero add insert mode completion.
15642 dump_word(slang
, word
, pat
, dir
, dumpflags
, wordflags
, lnum
)
15651 int keepcap
= FALSE
;
15654 char_u cword
[MAXWLEN
];
15655 char_u badword
[MAXWLEN
+ 10];
15657 int flags
= wordflags
;
15659 if (dumpflags
& DUMPFLAG_ONECAP
)
15660 flags
|= WF_ONECAP
;
15661 if (dumpflags
& DUMPFLAG_ALLCAP
)
15662 flags
|= WF_ALLCAP
;
15664 if ((dumpflags
& DUMPFLAG_KEEPCASE
) == 0 && (flags
& WF_CAPMASK
) != 0)
15666 /* Need to fix case according to "flags". */
15667 make_case_word(word
, cword
, flags
);
15673 if ((dumpflags
& DUMPFLAG_KEEPCASE
)
15674 && ((captype(word
, NULL
) & WF_KEEPCAP
) == 0
15675 || (flags
& WF_FIXCAP
) != 0))
15682 /* Add flags and regions after a slash. */
15683 if ((flags
& (WF_BANNED
| WF_RARE
| WF_REGION
)) || keepcap
)
15685 STRCPY(badword
, p
);
15686 STRCAT(badword
, "/");
15688 STRCAT(badword
, "=");
15689 if (flags
& WF_BANNED
)
15690 STRCAT(badword
, "!");
15691 else if (flags
& WF_RARE
)
15692 STRCAT(badword
, "?");
15693 if (flags
& WF_REGION
)
15694 for (i
= 0; i
< 7; ++i
)
15695 if (flags
& (0x10000 << i
))
15696 sprintf((char *)badword
+ STRLEN(badword
), "%d", i
+ 1);
15700 if (dumpflags
& DUMPFLAG_COUNT
)
15704 /* Include the word count for ":spelldump!". */
15705 hi
= hash_find(&slang
->sl_wordcount
, tw
);
15706 if (!HASHITEM_EMPTY(hi
))
15708 vim_snprintf((char *)IObuff
, IOSIZE
, "%s\t%d",
15709 tw
, HI2WC(hi
)->wc_count
);
15714 ml_append(lnum
, p
, (colnr_T
)0, FALSE
);
15716 else if (((dumpflags
& DUMPFLAG_ICASE
)
15717 ? MB_STRNICMP(p
, pat
, STRLEN(pat
)) == 0
15718 : STRNCMP(p
, pat
, STRLEN(pat
)) == 0)
15719 && ins_compl_add_infercase(p
, (int)STRLEN(p
),
15720 p_ic
, NULL
, *dir
, 0) == OK
)
15721 /* if dir was BACKWARD then honor it just once */
15726 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15727 * "word" and append a line to the buffer.
15728 * When "lnum" is zero add insert mode completion.
15729 * Return the updated line number.
15732 dump_prefixes(slang
, word
, pat
, dir
, dumpflags
, flags
, startlnum
)
15734 char_u
*word
; /* case-folded word */
15738 int flags
; /* flags with prefix ID */
15739 linenr_T startlnum
;
15741 idx_T arridx
[MAXWLEN
];
15743 char_u prefix
[MAXWLEN
];
15744 char_u word_up
[MAXWLEN
];
15745 int has_word_up
= FALSE
;
15749 linenr_T lnum
= startlnum
;
15755 /* If the word starts with a lower-case letter make the word with an
15756 * upper-case letter in word_up[]. */
15757 c
= PTR2CHAR(word
);
15758 if (SPELL_TOUPPER(c
) != c
)
15760 onecap_copy(word
, word_up
, TRUE
);
15761 has_word_up
= TRUE
;
15764 byts
= slang
->sl_pbyts
;
15765 idxs
= slang
->sl_pidxs
;
15766 if (byts
!= NULL
) /* array not is empty */
15769 * Loop over all prefixes, building them byte-by-byte in prefix[].
15770 * When at the end of a prefix check that it supports "flags".
15775 while (depth
>= 0 && !got_int
)
15779 if (curi
[depth
] > len
)
15781 /* Done all bytes at this node, go up one level. */
15787 /* Do one more byte at this node. */
15793 /* End of prefix, find out how many IDs there are. */
15794 for (i
= 1; i
< len
; ++i
)
15795 if (byts
[n
+ i
] != 0)
15797 curi
[depth
] += i
- 1;
15799 c
= valid_word_prefix(i
, n
, flags
, word
, slang
, FALSE
);
15802 vim_strncpy(prefix
+ depth
, word
, MAXWLEN
- depth
- 1);
15803 dump_word(slang
, prefix
, pat
, dir
, dumpflags
,
15804 (c
& WF_RAREPFX
) ? (flags
| WF_RARE
)
15810 /* Check for prefix that matches the word when the
15811 * first letter is upper-case, but only if the prefix has
15815 c
= valid_word_prefix(i
, n
, flags
, word_up
, slang
,
15819 vim_strncpy(prefix
+ depth
, word_up
,
15820 MAXWLEN
- depth
- 1);
15821 dump_word(slang
, prefix
, pat
, dir
, dumpflags
,
15822 (c
& WF_RAREPFX
) ? (flags
| WF_RARE
)
15831 /* Normal char, go one level deeper. */
15832 prefix
[depth
++] = c
;
15833 arridx
[depth
] = idxs
[n
];
15844 * Move "p" to the end of word "start".
15845 * Uses the spell-checking word characters.
15848 spell_to_word_end(start
, buf
)
15854 while (*p
!= NUL
&& spell_iswordp(p
, buf
))
15859 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
15861 * For Insert mode completion CTRL-X s:
15862 * Find start of the word in front of column "startcol".
15863 * We don't check if it is badly spelled, with completion we can only change
15864 * the word in front of the cursor.
15865 * Returns the column number of the word.
15868 spell_word_start(startcol
)
15875 if (no_spell_checking(curwin
))
15878 /* Find a word character before "startcol". */
15879 line
= ml_get_curline();
15880 for (p
= line
+ startcol
; p
> line
; )
15882 mb_ptr_back(line
, p
);
15883 if (spell_iswordp_nmw(p
))
15887 /* Go back to start of the word. */
15890 col
= (int)(p
- line
);
15891 mb_ptr_back(line
, p
);
15892 if (!spell_iswordp(p
, curbuf
))
15901 * Need to check for 'spellcapcheck' now, the word is removed before
15902 * expand_spelling() is called. Therefore the ugly global variable.
15904 static int spell_expand_need_cap
;
15907 spell_expand_check_cap(col
)
15910 spell_expand_need_cap
= check_need_cap(curwin
->w_cursor
.lnum
, col
);
15914 * Get list of spelling suggestions.
15915 * Used for Insert mode completion CTRL-X ?.
15916 * Returns the number of matches. The matches are in "matchp[]", array of
15917 * allocated strings.
15921 expand_spelling(lnum
, col
, pat
, matchp
)
15929 spell_suggest_list(&ga
, pat
, 100, spell_expand_need_cap
, TRUE
);
15930 *matchp
= ga
.ga_data
;
15935 #endif /* FEAT_SPELL */