blob: 18c96446c2f32c4a81524914baff80120d4bd177 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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.
8 */
9
10/*
11 * spell.h: common code for spell checking, used by spell.c and spellfile.c.
12 */
13
14/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
15 * Only use it for small word lists! */
16#if 0
17# define SPELL_PRINTTREE
18#endif
19
20/* Use SPELL_COMPRESS_ALLWAYS for debugging: compress the word tree after
21 * adding a word. Only use it for small word lists! */
22#if 0
23# define SPELL_COMPRESS_ALLWAYS
24#endif
25
26/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
27 * specific word. */
28#if 0
29# define DEBUG_TRIEWALK
30#endif
31
32#define MAXWLEN 254 /* Assume max. word len is this many bytes.
33 Some places assume a word length fits in a
34 byte, thus it can't be above 255.
35 Must be >= PFD_NOTSPECIAL. */
36
Bram Moolenaar2993ac52018-02-10 14:12:43 +010037#define MAXREGIONS 8 /* Number of regions supported. */
38
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020039/* Type used for indexes in the word tree need to be at least 4 bytes. If int
40 * is 8 bytes we could use something smaller, but what? */
41#if VIM_SIZEOF_INT > 3
42typedef int idx_T;
43#else
44typedef long idx_T;
45#endif
46
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020047typedef int salfirst_T;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020048
49/*
50 * Structure used to store words and other info for one language, loaded from
51 * a .spl file.
52 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
53 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
54 *
55 * The "byts" array stores the possible bytes in each tree node, preceded by
56 * the number of possible bytes, sorted on byte value:
57 * <len> <byte1> <byte2> ...
58 * The "idxs" array stores the index of the child node corresponding to the
59 * byte in "byts".
60 * Exception: when the byte is zero, the word may end here and "idxs" holds
61 * the flags, region mask and affixID for the word. There may be several
62 * zeros in sequence for alternative flag/region/affixID combinations.
63 */
64typedef struct slang_S slang_T;
65struct slang_S
66{
67 slang_T *sl_next; /* next language */
68 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
69 char_u *sl_fname; /* name of .spl file */
70 int sl_add; /* TRUE if it's a .add file. */
71
72 char_u *sl_fbyts; /* case-folded word bytes */
73 idx_T *sl_fidxs; /* case-folded word indexes */
74 char_u *sl_kbyts; /* keep-case word bytes */
75 idx_T *sl_kidxs; /* keep-case word indexes */
76 char_u *sl_pbyts; /* prefix tree word bytes */
77 idx_T *sl_pidxs; /* prefix tree word indexes */
78
79 char_u *sl_info; /* infotext string or NULL */
80
Bram Moolenaar2993ac52018-02-10 14:12:43 +010081 char_u sl_regions[MAXREGIONS * 2 + 1];
82 /* table with up to 8 region names plus NUL */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020083
84 char_u *sl_midword; /* MIDWORD string or NULL */
85
86 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
87
88 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
89 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
90 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
91 int sl_compoptions; /* COMP_* flags */
92 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
93 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
94 * (NULL when no compounding) */
95 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
96 char_u *sl_compstartflags; /* flags for first compound word */
97 char_u *sl_compallflags; /* all flags for compound words */
98 char_u sl_nobreak; /* When TRUE: no spaces between words */
99 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
100 garray_T sl_syl_items; /* syllable items */
101
102 int sl_prefixcnt; /* number of items in "sl_prefprog" */
103 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
104
105 garray_T sl_rep; /* list of fromto_T entries from REP lines */
106 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
107 there is none */
108 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
109 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
110 there is none */
111 int sl_followup; /* SAL followup */
112 int sl_collapse; /* SAL collapse_result */
113 int sl_rem_accents; /* SAL remove_accents */
114 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
115 * "sl_sal_first" maps chars, when has_mbyte
116 * "sl_sal" is a list of wide char lists. */
117 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
118 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
119 int sl_nosplitsugs; /* don't suggest splitting a word */
120 int sl_nocompoundsugs; /* don't suggest compounding */
121
122 /* Info from the .sug file. Loaded on demand. */
123 time_t sl_sugtime; /* timestamp for .sug file */
124 char_u *sl_sbyts; /* soundfolded word bytes */
125 idx_T *sl_sidxs; /* soundfolded word indexes */
126 buf_T *sl_sugbuf; /* buffer with word number table */
127 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
128 load */
129
130 int sl_has_map; /* TRUE if there is a MAP line */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200131 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
132 int sl_map_array[256]; /* MAP for first 256 chars */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200133 hashtab_T sl_sounddone; /* table with soundfolded words that have
134 handled, see add_sound_suggest() */
135};
136
137#ifdef VMS
138# define SPL_FNAME_TMPL "%s_%s.spl"
139# define SPL_FNAME_ADD "_add."
140# define SPL_FNAME_ASCII "_ascii."
141#else
142# define SPL_FNAME_TMPL "%s.%s.spl"
143# define SPL_FNAME_ADD ".add."
144# define SPL_FNAME_ASCII ".ascii."
145#endif
146
147/* Flags used for a word. Only the lowest byte can be used, the region byte
148 * comes above it. */
149#define WF_REGION 0x01 /* region byte follows */
150#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
151#define WF_ALLCAP 0x04 /* word must be all capitals */
152#define WF_RARE 0x08 /* rare word */
153#define WF_BANNED 0x10 /* bad word */
154#define WF_AFX 0x20 /* affix ID follows */
155#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
156#define WF_KEEPCAP 0x80 /* keep-case word */
157
158/* for <flags2>, shifted up one byte to be used in wn_flags */
159#define WF_HAS_AFF 0x0100 /* word includes affix */
160#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
161#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
162#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
163#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
164#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
165
166/* flags for <pflags> */
167#define WFP_RARE 0x01 /* rare prefix */
168#define WFP_NC 0x02 /* prefix is not combining */
169#define WFP_UP 0x04 /* to-upper prefix */
170#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
171#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
172
173/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
174 * byte) and prefcondnr (two bytes). */
175#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
176#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
177#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
178#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
179 * COMPOUNDPERMITFLAG */
180#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
181 * COMPOUNDFORBIDFLAG */
182
183/* flags for <compoptions> */
184#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
185#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
186#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
187#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
188
189/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
190 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
191 * One replacement: from "ft_from" to "ft_to". */
192typedef struct fromto_S
193{
194 char_u *ft_from;
195 char_u *ft_to;
196} fromto_T;
197
198/* Info from "SAL" entries in ".aff" file used in sl_sal.
199 * The info is split for quick processing by spell_soundfold().
200 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
201typedef struct salitem_S
202{
203 char_u *sm_lead; /* leading letters */
204 int sm_leadlen; /* length of "sm_lead" */
205 char_u *sm_oneof; /* letters from () or NULL */
206 char_u *sm_rules; /* rules like ^, $, priority */
207 char_u *sm_to; /* replacement. */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200208 int *sm_lead_w; /* wide character copy of "sm_lead" */
209 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
210 int *sm_to_w; /* wide character copy of "sm_to" */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200211} salitem_T;
212
213/* Values for SP_*ERROR are negative, positive values are used by
214 * read_cnt_string(). */
215#define SP_TRUNCERROR -1 /* spell file truncated error */
216#define SP_FORMERROR -2 /* format error in spell file */
217#define SP_OTHERERROR -3 /* other error while reading spell file */
218
219/*
220 * Structure used in "b_langp", filled from 'spelllang'.
221 */
222typedef struct langp_S
223{
224 slang_T *lp_slang; /* info for this language */
225 slang_T *lp_sallang; /* language used for sound folding or NULL */
226 slang_T *lp_replang; /* language used for REP items or NULL */
227 int lp_region; /* bitmask for region or REGION_ALL */
228} langp_T;
229
230#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
231
232#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
233#define VIMSUGMAGICL 6
234#define VIMSUGVERSION 1
235
236/*
237 * The tables used for recognizing word characters according to spelling.
238 * These are only used for the first 256 characters of 'encoding'.
239 */
240typedef struct spelltab_S
241{
242 char_u st_isw[256]; /* flags: is word char */
243 char_u st_isu[256]; /* flags: is uppercase char */
244 char_u st_fold[256]; /* chars: folded case */
245 char_u st_upper[256]; /* chars: upper case */
246} spelltab_T;
247
248/*
249 * Use our own character-case definitions, because the current locale may
250 * differ from what the .spl file uses.
251 * These must not be called with negative number!
252 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100253#if defined(HAVE_WCHAR_H)
254# include <wchar.h> /* for towupper() and towlower() */
255#endif
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200256/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
257 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
258 * the "w" library function for characters above 255 if available. */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100259#ifdef HAVE_TOWLOWER
260# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200261 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100262#else
263# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200264 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100265#endif
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200266
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100267#ifdef HAVE_TOWUPPER
268# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200269 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100270#else
271# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200272 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100273#endif
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200274
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100275#ifdef HAVE_ISWUPPER
276# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200277 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100278#else
279# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200280 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200281#endif
282
283#ifdef FEAT_SPELL
284/* First language that is loaded, start of the linked list of loaded
285 * languages. */
286# ifdef IN_SPELL_C
287# define SPELL_EXTERN
288# define SPELL_INIT(x) x
289# else
290# define SPELL_EXTERN extern
291# define SPELL_INIT(x)
292# endif
293
294SPELL_EXTERN slang_T *first_lang SPELL_INIT(= NULL);
295
296/* file used for "zG" and "zW" */
297SPELL_EXTERN char_u *int_wordlist SPELL_INIT(= NULL);
298
299
300SPELL_EXTERN char e_format[] SPELL_INIT(= N_("E759: Format error in spell file"));
301
302SPELL_EXTERN spelltab_T spelltab;
303SPELL_EXTERN int did_set_spelltab;
304
305#endif