blob: b444145be32e1cda7a55a87bd440f1f4c1cfac0d [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
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010014// Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
15// Only use it for small word lists!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020016#if 0
17# define SPELL_PRINTTREE
18#endif
19
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010020// Use SPELL_COMPRESS_ALLWAYS for debugging: compress the word tree after
21// adding a word. Only use it for small word lists!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020022#if 0
23# define SPELL_COMPRESS_ALLWAYS
24#endif
25
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010026// Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
27// specific word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020028#if 0
29# define DEBUG_TRIEWALK
30#endif
31
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010032#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.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020036
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010037#define MAXREGIONS 8 // Number of regions supported.
Bram Moolenaar2993ac52018-02-10 14:12:43 +010038
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010039// 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?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020041typedef int idx_T;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020042
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020043typedef int salfirst_T;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020044
45/*
46 * Structure used to store words and other info for one language, loaded from
47 * a .spl file.
48 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
49 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
50 *
51 * The "byts" array stores the possible bytes in each tree node, preceded by
52 * the number of possible bytes, sorted on byte value:
53 * <len> <byte1> <byte2> ...
54 * The "idxs" array stores the index of the child node corresponding to the
55 * byte in "byts".
56 * Exception: when the byte is zero, the word may end here and "idxs" holds
57 * the flags, region mask and affixID for the word. There may be several
58 * zeros in sequence for alternative flag/region/affixID combinations.
59 */
60typedef struct slang_S slang_T;
61struct slang_S
62{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010063 slang_T *sl_next; // next language
64 char_u *sl_name; // language name "en", "en.rare", "nl", etc.
65 char_u *sl_fname; // name of .spl file
66 int sl_add; // TRUE if it's a .add file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020067
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010068 char_u *sl_fbyts; // case-folded word bytes
69 idx_T *sl_fidxs; // case-folded word indexes
70 char_u *sl_kbyts; // keep-case word bytes
71 idx_T *sl_kidxs; // keep-case word indexes
72 char_u *sl_pbyts; // prefix tree word bytes
73 idx_T *sl_pidxs; // prefix tree word indexes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020074
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010075 char_u *sl_info; // infotext string or NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020076
Bram Moolenaar2993ac52018-02-10 14:12:43 +010077 char_u sl_regions[MAXREGIONS * 2 + 1];
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010078 // table with up to 8 region names plus NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020079
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010080 char_u *sl_midword; // MIDWORD string or NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020081
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010082 hashtab_T sl_wordcount; // hashtable with word count, wordcount_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020083
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010084 int sl_compmax; // COMPOUNDWORDMAX (default: MAXWLEN)
85 int sl_compminlen; // COMPOUNDMIN (default: 0)
86 int sl_compsylmax; // COMPOUNDSYLMAX (default: MAXWLEN)
87 int sl_compoptions; // COMP_* flags
88 garray_T sl_comppat; // CHECKCOMPOUNDPATTERN items
89 regprog_T *sl_compprog; // COMPOUNDRULE turned into a regexp progrm
90 // (NULL when no compounding)
91 char_u *sl_comprules; // all COMPOUNDRULE concatenated (or NULL)
92 char_u *sl_compstartflags; // flags for first compound word
93 char_u *sl_compallflags; // all flags for compound words
94 char_u sl_nobreak; // When TRUE: no spaces between words
95 char_u *sl_syllable; // SYLLABLE repeatable chars or NULL
96 garray_T sl_syl_items; // syllable items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020097
Bram Moolenaar9bf703d2019-11-30 19:44:38 +010098 int sl_prefixcnt; // number of items in "sl_prefprog"
99 regprog_T **sl_prefprog; // table with regprogs for prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200100
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100101 garray_T sl_rep; // list of fromto_T entries from REP lines
102 short sl_rep_first[256]; // indexes where byte first appears, -1 if
103 // there is none
104 garray_T sl_sal; // list of salitem_T entries from SAL lines
105 salfirst_T sl_sal_first[256]; // indexes where byte first appears, -1 if
106 // there is none
107 int sl_followup; // SAL followup
108 int sl_collapse; // SAL collapse_result
109 int sl_rem_accents; // SAL remove_accents
110 int sl_sofo; // SOFOFROM and SOFOTO instead of SAL items:
111 // "sl_sal_first" maps chars, when has_mbyte
112 // "sl_sal" is a list of wide char lists.
113 garray_T sl_repsal; // list of fromto_T entries from REPSAL lines
114 short sl_repsal_first[256]; // sl_rep_first for REPSAL lines
115 int sl_nosplitsugs; // don't suggest splitting a word
116 int sl_nocompoundsugs; // don't suggest compounding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200117
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100118 // Info from the .sug file. Loaded on demand.
119 time_t sl_sugtime; // timestamp for .sug file
120 char_u *sl_sbyts; // soundfolded word bytes
121 idx_T *sl_sidxs; // soundfolded word indexes
122 buf_T *sl_sugbuf; // buffer with word number table
123 int sl_sugloaded; // TRUE when .sug file was loaded or failed to
124 // load
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200125
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100126 int sl_has_map; // TRUE if there is a MAP line
127 hashtab_T sl_map_hash; // MAP for multi-byte chars
128 int sl_map_array[256]; // MAP for first 256 chars
129 hashtab_T sl_sounddone; // table with soundfolded words that have
130 // handled, see add_sound_suggest()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200131};
132
133#ifdef VMS
134# define SPL_FNAME_TMPL "%s_%s.spl"
135# define SPL_FNAME_ADD "_add."
136# define SPL_FNAME_ASCII "_ascii."
137#else
138# define SPL_FNAME_TMPL "%s.%s.spl"
139# define SPL_FNAME_ADD ".add."
140# define SPL_FNAME_ASCII ".ascii."
141#endif
142
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100143// Flags used for a word. Only the lowest byte can be used, the region byte
144// comes above it.
145#define WF_REGION 0x01 // region byte follows
146#define WF_ONECAP 0x02 // word with one capital (or all capitals)
147#define WF_ALLCAP 0x04 // word must be all capitals
148#define WF_RARE 0x08 // rare word
149#define WF_BANNED 0x10 // bad word
150#define WF_AFX 0x20 // affix ID follows
151#define WF_FIXCAP 0x40 // keep-case word, allcap not allowed
152#define WF_KEEPCAP 0x80 // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200153
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200154#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
155
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100156// for <flags2>, shifted up one byte to be used in wn_flags
157#define WF_HAS_AFF 0x0100 // word includes affix
158#define WF_NEEDCOMP 0x0200 // word only valid in compound
159#define WF_NOSUGGEST 0x0400 // word not to be suggested
160#define WF_COMPROOT 0x0800 // already compounded word, COMPOUNDROOT
161#define WF_NOCOMPBEF 0x1000 // no compounding before this word
162#define WF_NOCOMPAFT 0x2000 // no compounding after this word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200163
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100164// flags for <pflags>
165#define WFP_RARE 0x01 // rare prefix
166#define WFP_NC 0x02 // prefix is not combining
167#define WFP_UP 0x04 // to-upper prefix
168#define WFP_COMPPERMIT 0x08 // prefix with COMPOUNDPERMITFLAG
169#define WFP_COMPFORBID 0x10 // prefix with COMPOUNDFORBIDFLAG
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200170
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100171// Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
172// byte) and prefcondnr (two bytes).
173#define WF_RAREPFX (WFP_RARE << 24) // rare postponed prefix
174#define WF_PFX_NC (WFP_NC << 24) // non-combining postponed prefix
175#define WF_PFX_UP (WFP_UP << 24) // to-upper postponed prefix
176#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) // postponed prefix with
177 // COMPOUNDPERMITFLAG
178#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) // postponed prefix with
179 // COMPOUNDFORBIDFLAG
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200180
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100181// flags for <compoptions>
182#define COMP_CHECKDUP 1 // CHECKCOMPOUNDDUP
183#define COMP_CHECKREP 2 // CHECKCOMPOUNDREP
184#define COMP_CHECKCASE 4 // CHECKCOMPOUNDCASE
185#define COMP_CHECKTRIPLE 8 // CHECKCOMPOUNDTRIPLE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200186
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100187// Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
188// si_repsal, sl_rep, and si_sal. Not for sl_sal!
189// One replacement: from "ft_from" to "ft_to".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200190typedef struct fromto_S
191{
192 char_u *ft_from;
193 char_u *ft_to;
194} fromto_T;
195
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100196// Info from "SAL" entries in ".aff" file used in sl_sal.
197// The info is split for quick processing by spell_soundfold().
198// Note that "sm_oneof" and "sm_rules" point into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200199typedef struct salitem_S
200{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100201 char_u *sm_lead; // leading letters
202 int sm_leadlen; // length of "sm_lead"
203 char_u *sm_oneof; // letters from () or NULL
204 char_u *sm_rules; // rules like ^, $, priority
205 char_u *sm_to; // replacement.
206 int *sm_lead_w; // wide character copy of "sm_lead"
207 int *sm_oneof_w; // wide character copy of "sm_oneof"
208 int *sm_to_w; // wide character copy of "sm_to"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200209} salitem_T;
210
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100211// Values for SP_*ERROR are negative, positive values are used by
212// read_cnt_string().
213#define SP_TRUNCERROR -1 // spell file truncated error
214#define SP_FORMERROR -2 // format error in spell file
215#define SP_OTHERERROR -3 // other error while reading spell file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200216
217/*
218 * Structure used in "b_langp", filled from 'spelllang'.
219 */
220typedef struct langp_S
221{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100222 slang_T *lp_slang; // info for this language
223 slang_T *lp_sallang; // language used for sound folding or NULL
224 slang_T *lp_replang; // language used for REP items or NULL
225 int lp_region; // bitmask for region or REGION_ALL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200226} langp_T;
227
228#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
229
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100230#define VIMSUGMAGIC "VIMsug" // string at start of Vim .sug file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200231#define VIMSUGMAGICL 6
232#define VIMSUGVERSION 1
233
234/*
235 * The tables used for recognizing word characters according to spelling.
236 * These are only used for the first 256 characters of 'encoding'.
237 */
238typedef struct spelltab_S
239{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100240 char_u st_isw[256]; // flags: is word char
241 char_u st_isu[256]; // flags: is uppercase char
242 char_u st_fold[256]; // chars: folded case
243 char_u st_upper[256]; // chars: upper case
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200244} spelltab_T;
245
246/*
247 * Use our own character-case definitions, because the current locale may
248 * differ from what the .spl file uses.
249 * These must not be called with negative number!
250 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100251#if defined(HAVE_WCHAR_H)
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100252# include <wchar.h> // for towupper() and towlower()
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100253#endif
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100254// Multi-byte implementation. For Unicode we can call utf_*(), but don't do
255// that for ASCII, because we don't want to use 'casemap' here. Otherwise use
256// the "w" library function for characters above 255 if available.
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100257#ifdef HAVE_TOWLOWER
258# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200259 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100260#else
261# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200262 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100263#endif
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200264
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100265#ifdef HAVE_TOWUPPER
266# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200267 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100268#else
269# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200270 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100271#endif
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200272
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100273#ifdef HAVE_ISWUPPER
274# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200275 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100276#else
277# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200278 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200279#endif
280
281#ifdef FEAT_SPELL
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100282// First language that is loaded, start of the linked list of loaded
283// languages.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200284# ifdef IN_SPELL_C
285# define SPELL_EXTERN
286# define SPELL_INIT(x) x
287# else
288# define SPELL_EXTERN extern
289# define SPELL_INIT(x)
290# endif
291
292SPELL_EXTERN slang_T *first_lang SPELL_INIT(= NULL);
293
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100294// file used for "zG" and "zW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200295SPELL_EXTERN char_u *int_wordlist SPELL_INIT(= NULL);
296
297
298SPELL_EXTERN char e_format[] SPELL_INIT(= N_("E759: Format error in spell file"));
299
300SPELL_EXTERN spelltab_T spelltab;
301SPELL_EXTERN int did_set_spelltab;
302
Bram Moolenaar08cc3742019-08-11 22:51:14 +0200303// Values for "what" argument of spell_add_word()
304#define SPELL_ADD_GOOD 0
305#define SPELL_ADD_BAD 1
306#define SPELL_ADD_RARE 2
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200307
308typedef struct wordcount_S
309{
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100310 short_u wc_count; // nr of times word was seen
311 char_u wc_word[1]; // word, actually longer
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200312} wordcount_T;
313
314#define WC_KEY_OFF offsetof(wordcount_T, wc_word)
315#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
316#define MAXWORDCOUNT 0xffff
317
Bram Moolenaar9bf703d2019-11-30 19:44:38 +0100318// Remember what "z?" replaced.
Bram Moolenaar46a426c2019-09-27 12:41:56 +0200319SPELL_EXTERN char_u *repl_from SPELL_INIT(= NULL);
320SPELL_EXTERN char_u *repl_to SPELL_INIT(= NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200321#endif