2005-03-21 08:23:33 +00:00
|
|
|
/* vi:set ts=8 sts=4 sw=4:
|
|
|
|
*
|
|
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
|
|
*
|
|
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
|
|
* See README.txt for an overview of the Vim source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* spell.c: code for spell checking
|
2005-03-22 22:54:12 +00:00
|
|
|
*
|
2005-06-04 21:55:20 +00:00
|
|
|
* The spell checking mechanism uses a tree (aka trie). Each node in the tree
|
|
|
|
* has a list of bytes that can appear (siblings). For each byte there is a
|
|
|
|
* pointer to the node with the byte that follows in the word (child).
|
2005-06-14 22:01:04 +00:00
|
|
|
*
|
|
|
|
* A NUL byte is used where the word may end. The bytes are sorted, so that
|
|
|
|
* binary searching can be used and the NUL bytes are at the start. The
|
|
|
|
* number of possible bytes is stored before the list of bytes.
|
|
|
|
*
|
|
|
|
* The tree uses two arrays: "byts" stores the characters, "idxs" stores
|
|
|
|
* either the next index or flags. The tree starts at index 0. For example,
|
|
|
|
* to lookup "vi" this sequence is followed:
|
|
|
|
* i = 0
|
|
|
|
* len = byts[i]
|
|
|
|
* n = where "v" appears in byts[i + 1] to byts[i + len]
|
|
|
|
* i = idxs[n]
|
|
|
|
* len = byts[i]
|
|
|
|
* n = where "i" appears in byts[i + 1] to byts[i + len]
|
|
|
|
* i = idxs[n]
|
|
|
|
* len = byts[i]
|
|
|
|
* find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
|
2005-06-04 21:55:20 +00:00
|
|
|
*
|
|
|
|
* There are two trees: one with case-folded words and one with words in
|
|
|
|
* original case. The second one is only used for keep-case words and is
|
|
|
|
* usually small.
|
|
|
|
*
|
|
|
|
* Thanks to Olaf Seibert for providing an example implementation of this tree
|
|
|
|
* and the compression mechanism.
|
2005-04-19 21:30:25 +00:00
|
|
|
*
|
|
|
|
* Matching involves checking the caps type: Onecap ALLCAP KeepCap.
|
|
|
|
*
|
2005-04-15 21:00:38 +00:00
|
|
|
* Why doesn't Vim use aspell/ispell/myspell/etc.?
|
|
|
|
* See ":help develop-spell".
|
|
|
|
*/
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Use this to let the score depend in how much a suggestion sounds like the
|
2005-06-14 22:01:04 +00:00
|
|
|
* bad word. It's quite slow and only occasionally makes the sorting better.
|
|
|
|
#define SOUNDFOLD_SCORE
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use this to adjust the score after finding suggestions, based on the
|
|
|
|
* suggested word sounding like the bad word. This is much faster than doing
|
|
|
|
* it for every possible suggestion.
|
|
|
|
* Disadvantage: When "the" is typed as "hte" it sounds different and goes
|
|
|
|
* down in the list.
|
|
|
|
#define RESCORE(word_score, sound_score) ((2 * word_score + sound_score) / 3)
|
2005-06-13 22:28:56 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Vim spell file format: <HEADER> <SUGGEST> <LWORDTREE> <KWORDTREE>
|
|
|
|
*
|
|
|
|
* <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
|
|
* <charflagslen> <charflags> <fcharslen> <fchars>
|
|
|
|
*
|
2005-06-13 22:28:56 +00:00
|
|
|
* <fileID> 10 bytes "VIMspell06"
|
2005-06-04 21:55:20 +00:00
|
|
|
* <regioncnt> 1 byte number of regions following (8 supported)
|
2005-06-06 21:50:35 +00:00
|
|
|
* <regionname> 2 bytes Region name: ca, au, etc. Lower case.
|
2005-06-04 21:55:20 +00:00
|
|
|
* First <regionname> is region 1.
|
|
|
|
*
|
|
|
|
* <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
|
|
|
|
* <charflags> N bytes List of flags (first one is for character 128):
|
2005-06-14 22:01:04 +00:00
|
|
|
* 0x01 word character CF_WORD
|
|
|
|
* 0x02 upper-case character CF_UPPER
|
2005-06-04 21:55:20 +00:00
|
|
|
* <fcharslen> 2 bytes Number of bytes in <fchars>.
|
|
|
|
* <fchars> N bytes Folded characters, first one is for character 128.
|
|
|
|
*
|
|
|
|
*
|
2005-06-13 22:28:56 +00:00
|
|
|
* <SUGGEST> : <repcount> <rep> ...
|
|
|
|
* <salflags> <salcount> <sal> ...
|
|
|
|
* <maplen> <mapstr>
|
|
|
|
*
|
|
|
|
* <repcount> 2 bytes number of <rep> items, MSB first.
|
|
|
|
*
|
|
|
|
* <rep> : <repfromlen> <repfrom> <reptolen> <repto>
|
|
|
|
*
|
|
|
|
* <repfromlen> 1 byte length of <repfrom>
|
|
|
|
*
|
|
|
|
* <repfrom> N bytes "from" part of replacement
|
|
|
|
*
|
|
|
|
* <reptolen> 1 byte length of <repto>
|
|
|
|
*
|
|
|
|
* <repto> N bytes "to" part of replacement
|
|
|
|
*
|
|
|
|
* <salflags> 1 byte flags for soundsalike conversion:
|
|
|
|
* SAL_F0LLOWUP
|
|
|
|
* SAL_COLLAPSE
|
|
|
|
* SAL_REM_ACCENTS
|
|
|
|
*
|
|
|
|
* <sal> : <salfromlen> <salfrom> <saltolen> <salto>
|
|
|
|
*
|
|
|
|
* <salfromlen> 1 byte length of <salfrom>
|
|
|
|
*
|
|
|
|
* <salfrom> N bytes "from" part of soundsalike
|
|
|
|
*
|
|
|
|
* <saltolen> 1 byte length of <salto>
|
|
|
|
*
|
|
|
|
* <salto> N bytes "to" part of soundsalike
|
|
|
|
*
|
|
|
|
* <maplen> 2 bytes length of <mapstr>, MSB first
|
2005-06-04 21:55:20 +00:00
|
|
|
*
|
2005-06-13 22:28:56 +00:00
|
|
|
* <mapstr> N bytes String with sequences of similar characters,
|
|
|
|
* separated by slashes.
|
2005-06-04 21:55:20 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* <LWORDTREE>: <wordtree>
|
|
|
|
*
|
|
|
|
* <wordtree>: <nodecount> <nodedata> ...
|
|
|
|
*
|
|
|
|
* <nodecount> 4 bytes Number of nodes following. MSB first.
|
|
|
|
*
|
|
|
|
* <nodedata>: <siblingcount> <sibling> ...
|
|
|
|
*
|
|
|
|
* <siblingcount> 1 byte Number of siblings in this node. The siblings
|
|
|
|
* follow in sorted order.
|
|
|
|
*
|
|
|
|
* <sibling>: <byte> [<nodeidx> <xbyte> | <flags> [<region>]]
|
|
|
|
*
|
|
|
|
* <byte> 1 byte Byte value of the sibling. Special cases:
|
|
|
|
* BY_NOFLAGS: End of word without flags and for all
|
|
|
|
* regions.
|
|
|
|
* BY_FLAGS: End of word, <flags> follow.
|
|
|
|
* BY_INDEX: Child of sibling is shared, <nodeidx>
|
|
|
|
* and <xbyte> follow.
|
|
|
|
*
|
|
|
|
* <nodeidx> 3 bytes Index of child for this sibling, MSB first.
|
|
|
|
*
|
|
|
|
* <xbyte> 1 byte byte value of the sibling.
|
|
|
|
*
|
|
|
|
* <flags> 1 byte bitmask of:
|
|
|
|
* WF_ALLCAP word must have only capitals
|
|
|
|
* WF_ONECAP first char of word must be capital
|
|
|
|
* WF_RARE rare word
|
|
|
|
* WF_REGION <region> follows
|
|
|
|
*
|
|
|
|
* <region> 1 byte Bitmask for regions in which word is valid. When
|
|
|
|
* omitted it's valid in all regions.
|
|
|
|
* Lowest bit is for region 1.
|
|
|
|
*
|
|
|
|
* <KWORDTREE>: <wordtree>
|
|
|
|
*
|
|
|
|
* All text characters are in 'encoding', but stored as single bytes.
|
|
|
|
*/
|
|
|
|
|
2005-03-21 08:23:33 +00:00
|
|
|
#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
|
|
|
|
# include <io.h> /* for lseek(), must be before vim.h */
|
|
|
|
#endif
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
#include "vim.h"
|
|
|
|
|
|
|
|
#if defined(FEAT_SYN_HL) || defined(PROTO)
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
#define MAXWLEN 250 /* Assume max. word len is this many bytes.
|
|
|
|
Some places assume a word length fits in a
|
|
|
|
byte, thus it can't be above 255. */
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Type used for indexes in the word tree need to be at least 3 bytes. If int
|
|
|
|
* is 8 bytes we could use something smaller, but what? */
|
|
|
|
#if SIZEOF_INT > 2
|
|
|
|
typedef int idx_T;
|
|
|
|
#else
|
|
|
|
typedef long idx_T;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Flags used for a word. Only the lowest byte can be used, the region byte
|
|
|
|
* comes above it. */
|
2005-06-04 21:55:20 +00:00
|
|
|
#define WF_REGION 0x01 /* region byte follows */
|
|
|
|
#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
|
|
|
|
#define WF_ALLCAP 0x04 /* word must be all capitals */
|
|
|
|
#define WF_RARE 0x08 /* rare word */
|
2005-06-06 21:50:35 +00:00
|
|
|
#define WF_BANNED 0x10 /* bad word */
|
2005-06-13 22:28:56 +00:00
|
|
|
#define WF_KEEPCAP 0x80 /* keep-case word */
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
#define WF_USED 0x10000 /* Word was found in text. Must be in separate
|
|
|
|
byte before region and flags. */
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
#define BY_NOFLAGS 0 /* end of word without flags or region */
|
|
|
|
#define BY_FLAGS 1 /* end of word, flag byte follows */
|
|
|
|
#define BY_INDEX 2 /* child is shared, index follows */
|
|
|
|
#define BY_SPECIAL BY_INDEX /* hightest special byte value */
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
|
|
|
|
* si_sal and sl_sal.
|
|
|
|
* One replacement: from "ft_from" to "ft_to". */
|
|
|
|
typedef struct fromto_S
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
char_u *ft_from;
|
|
|
|
char_u *ft_to;
|
|
|
|
} fromto_T;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
2005-04-19 21:30:25 +00:00
|
|
|
* Structure used to store words and other info for one language, loaded from
|
|
|
|
* a .spl file.
|
2005-06-04 21:55:20 +00:00
|
|
|
* The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
|
|
|
|
* case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
|
|
|
|
*
|
|
|
|
* The "byts" array stores the possible bytes in each tree node, preceded by
|
|
|
|
* the number of possible bytes, sorted on byte value:
|
|
|
|
* <len> <byte1> <byte2> ...
|
|
|
|
* The "idxs" array stores the index of the child node corresponding to the
|
|
|
|
* byte in "byts".
|
|
|
|
* Exception: when the byte is zero, the word may end here and "idxs" holds
|
|
|
|
* the flags and region for the word. There may be several zeros in sequence
|
|
|
|
* for alternative flag/region combinations.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
typedef struct slang_S slang_T;
|
|
|
|
struct slang_S
|
|
|
|
{
|
|
|
|
slang_T *sl_next; /* next language */
|
|
|
|
char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
|
2005-06-07 21:00:02 +00:00
|
|
|
char_u *sl_fname; /* name of .spl file */
|
2005-06-13 22:28:56 +00:00
|
|
|
int sl_add; /* TRUE if it's a .add file. */
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *sl_fbyts; /* case-folded word bytes */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T *sl_fidxs; /* case-folded word indexes */
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *sl_kbyts; /* keep-case word bytes */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T *sl_kidxs; /* keep-case word indexes */
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
garray_T sl_rep; /* list of fromto_T entries from REP lines */
|
|
|
|
short sl_rep_first[256]; /* indexes where byte first appears, -1 if
|
|
|
|
there is none */
|
|
|
|
garray_T sl_sal; /* list of fromto_T entries from SAL lines */
|
|
|
|
short sl_sal_first[256]; /* indexes where byte first appears, -1 if
|
|
|
|
there is none */
|
|
|
|
int sl_followup; /* SAL followup */
|
|
|
|
int sl_collapse; /* SAL collapse_result */
|
|
|
|
int sl_rem_accents; /* SAL remove_accents */
|
|
|
|
char_u *sl_map; /* string with similar chars from MAP lines */
|
2005-04-15 21:00:38 +00:00
|
|
|
};
|
|
|
|
|
2005-04-19 21:30:25 +00:00
|
|
|
/* First language that is loaded, start of the linked list of loaded
|
|
|
|
* languages. */
|
2005-04-15 21:00:38 +00:00
|
|
|
static slang_T *first_lang = NULL;
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/* Flags used in .spl file for soundsalike flags. */
|
|
|
|
#define SAL_F0LLOWUP 1
|
|
|
|
#define SAL_COLLAPSE 2
|
|
|
|
#define SAL_REM_ACCENTS 4
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
|
|
|
* Structure used in "b_langp", filled from 'spelllang'.
|
|
|
|
*/
|
|
|
|
typedef struct langp_S
|
|
|
|
{
|
|
|
|
slang_T *lp_slang; /* info for this language (NULL for last one) */
|
|
|
|
int lp_region; /* bitmask for region or REGION_ALL */
|
|
|
|
} langp_T;
|
|
|
|
|
|
|
|
#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
#define REGION_ALL 0xff /* word valid in all regions */
|
|
|
|
|
|
|
|
/* Result values. Lower number is accepted over higher one. */
|
|
|
|
#define SP_BANNED -1
|
2005-04-15 21:00:38 +00:00
|
|
|
#define SP_OK 0
|
2005-06-06 21:50:35 +00:00
|
|
|
#define SP_RARE 1
|
|
|
|
#define SP_LOCAL 2
|
|
|
|
#define SP_BAD 3
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
#define VIMSPELLMAGIC "VIMspell06" /* string at start of Vim spell file */
|
2005-04-15 21:00:38 +00:00
|
|
|
#define VIMSPELLMAGICL 10
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Information used when looking for suggestions.
|
|
|
|
*/
|
|
|
|
typedef struct suginfo_S
|
|
|
|
{
|
|
|
|
garray_T su_ga; /* suggestions, contains "suggest_T" */
|
|
|
|
int su_maxscore; /* maximum score for adding to su_ga */
|
|
|
|
int su_icase; /* accept words with wrong case */
|
|
|
|
int su_icase_add; /* add matches while ignoring case */
|
|
|
|
char_u *su_badptr; /* start of bad word in line */
|
|
|
|
int su_badlen; /* length of detected bad word in line */
|
|
|
|
char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
|
|
|
|
char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
|
|
|
|
hashtab_T su_banned; /* table with banned words */
|
|
|
|
#ifdef SOUNDFOLD_SCORE
|
|
|
|
slang_T *su_slang; /* currently used slang_T */
|
|
|
|
char_u su_salword[MAXWLEN]; /* soundfolded badword */
|
|
|
|
#endif
|
|
|
|
} suginfo_T;
|
|
|
|
|
|
|
|
/* One word suggestion. Used in "si_ga". */
|
|
|
|
typedef struct suggest_S
|
|
|
|
{
|
|
|
|
char_u *st_word; /* suggested word, allocated string */
|
|
|
|
int st_orglen; /* length of replaced text */
|
|
|
|
int st_score; /* lower is better */
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef RESCORE
|
|
|
|
int st_had_bonus; /* bonus already included in score */
|
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
} suggest_T;
|
|
|
|
|
|
|
|
#define SUG(sup, i) (((suggest_T *)(sup)->su_ga.ga_data)[i])
|
|
|
|
|
|
|
|
/* Number of suggestions displayed. */
|
|
|
|
#define SUG_PROMPT_COUNT ((int)Rows - 2)
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
|
|
|
|
* called the score may change, thus we need to keep more than what is
|
|
|
|
* displayed. */
|
|
|
|
#define SUG_CLEAN_COUNT (SUG_PROMPT_COUNT < 25 ? 25 : SUG_PROMPT_COUNT)
|
|
|
|
|
|
|
|
/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
|
|
|
|
* of suggestions that are not going to be displayed. */
|
|
|
|
#define SUG_MAX_COUNT (SUG_PROMPT_COUNT + 50)
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* score for various changes */
|
|
|
|
#define SCORE_SPLIT 99 /* split bad word */
|
|
|
|
#define SCORE_ICASE 52 /* slightly different case */
|
|
|
|
#define SCORE_ALLCAP 120 /* need all-cap case */
|
|
|
|
#define SCORE_REGION 70 /* word is for different region */
|
|
|
|
#define SCORE_RARE 180 /* rare word */
|
2005-06-14 22:01:04 +00:00
|
|
|
#define SCORE_NOTUSED 11 /* word not found in text yet */
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* score for edit distance */
|
|
|
|
#define SCORE_SWAP 90 /* swap two characters */
|
|
|
|
#define SCORE_SWAP3 110 /* swap two characters in three */
|
|
|
|
#define SCORE_REP 87 /* REP replacement */
|
|
|
|
#define SCORE_SUBST 93 /* substitute a character */
|
|
|
|
#define SCORE_SIMILAR 33 /* substitute a similar character */
|
2005-06-14 22:01:04 +00:00
|
|
|
#define SCORE_DEL 94 /* delete a character */
|
|
|
|
#define SCORE_INS 96 /* insert a character */
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
|
|
|
|
* 350 allows for about three changes. */
|
|
|
|
#define SCORE_MAXMAX 999999 /* accept any score */
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
|
|
|
* Structure to store info for word matching.
|
|
|
|
*/
|
|
|
|
typedef struct matchinf_S
|
|
|
|
{
|
|
|
|
langp_T *mi_lp; /* info for language and region */
|
2005-04-19 21:30:25 +00:00
|
|
|
|
|
|
|
/* pointers to original text to be checked */
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u *mi_word; /* start of word being checked */
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *mi_end; /* end of matching word */
|
2005-04-19 21:30:25 +00:00
|
|
|
char_u *mi_fend; /* next char to be added to mi_fword */
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *mi_cend; /* char after what was used for
|
|
|
|
mi_capflags */
|
2005-04-19 21:30:25 +00:00
|
|
|
|
|
|
|
/* case-folded text */
|
|
|
|
char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
|
2005-06-04 21:55:20 +00:00
|
|
|
int mi_fwordlen; /* nr of valid bytes in mi_fword */
|
2005-04-19 21:30:25 +00:00
|
|
|
|
|
|
|
/* others */
|
2005-04-15 21:00:38 +00:00
|
|
|
int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
|
2005-06-04 21:55:20 +00:00
|
|
|
int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
|
2005-04-15 21:00:38 +00:00
|
|
|
} matchinf_T;
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/*
|
|
|
|
* The tables used for recognizing word characters according to spelling.
|
|
|
|
* These are only used for the first 256 characters of 'encoding'.
|
|
|
|
*/
|
|
|
|
typedef struct spelltab_S
|
|
|
|
{
|
|
|
|
char_u st_isw[256]; /* flags: is word char */
|
|
|
|
char_u st_isu[256]; /* flags: is uppercase char */
|
|
|
|
char_u st_fold[256]; /* chars: folded case */
|
2005-06-14 22:01:04 +00:00
|
|
|
char_u st_upper[256]; /* chars: upper case */
|
2005-06-06 21:50:35 +00:00
|
|
|
} spelltab_T;
|
|
|
|
|
|
|
|
static spelltab_T spelltab;
|
|
|
|
static int did_set_spelltab;
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
#define CF_WORD 0x01
|
|
|
|
#define CF_UPPER 0x02
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
static void clear_spell_chartab __ARGS((spelltab_T *sp));
|
|
|
|
static int set_spell_finish __ARGS((spelltab_T *new_st));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return TRUE if "p" points to a word character or "c" is a word character
|
|
|
|
* for spelling.
|
|
|
|
* Checking for a word character is done very often, avoid the function call
|
|
|
|
* overhead.
|
|
|
|
*/
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
|
|
|
|
? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
|
|
|
|
#else
|
|
|
|
# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
|
|
|
|
#endif
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Struct to keep the state at each level in spell_try_change().
|
|
|
|
*/
|
|
|
|
typedef struct trystate_S
|
|
|
|
{
|
|
|
|
int ts_state; /* state at this level, STATE_ */
|
|
|
|
int ts_score; /* score */
|
|
|
|
int ts_curi; /* index in list of child nodes */
|
|
|
|
int ts_fidx; /* index in fword[], case-folded bad word */
|
|
|
|
int ts_fidxtry; /* ts_fidx at which bytes may be changed */
|
|
|
|
int ts_twordlen; /* valid length of tword[] */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T ts_arridx; /* index in tree array, start of node */
|
2005-06-13 22:28:56 +00:00
|
|
|
char_u ts_save_prewordlen; /* saved "prewordlen" */
|
|
|
|
int ts_save_splitoff; /* su_splitoff saved here */
|
|
|
|
int ts_save_badflags; /* badflags saved here */
|
|
|
|
} trystate_T;
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
static slang_T *slang_alloc __ARGS((char_u *lang));
|
|
|
|
static void slang_free __ARGS((slang_T *lp));
|
2005-06-07 21:00:02 +00:00
|
|
|
static void slang_clear __ARGS((slang_T *lp));
|
2005-06-04 21:55:20 +00:00
|
|
|
static void find_word __ARGS((matchinf_T *mip, int keepcap));
|
2005-06-13 22:28:56 +00:00
|
|
|
static int spell_valid_case __ARGS((int origflags, int treeflags));
|
2005-06-06 21:50:35 +00:00
|
|
|
static void spell_load_lang __ARGS((char_u *lang));
|
2005-06-07 21:00:02 +00:00
|
|
|
static char_u *spell_enc __ARGS((void));
|
|
|
|
static void spell_load_cb __ARGS((char_u *fname, void *cookie));
|
2005-06-13 22:28:56 +00:00
|
|
|
static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
|
2005-06-14 22:01:04 +00:00
|
|
|
static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx));
|
2005-04-15 21:00:38 +00:00
|
|
|
static int find_region __ARGS((char_u *rp, char_u *region));
|
|
|
|
static int captype __ARGS((char_u *word, char_u *end));
|
2005-06-13 22:28:56 +00:00
|
|
|
static void spell_reload_one __ARGS((char_u *fname, int added_word));
|
2005-06-06 21:50:35 +00:00
|
|
|
static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
|
|
|
|
static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
|
|
|
|
static void write_spell_chartab __ARGS((FILE *fd));
|
|
|
|
static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
|
2005-06-14 22:01:04 +00:00
|
|
|
static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
|
2005-06-13 22:28:56 +00:00
|
|
|
static void spell_try_change __ARGS((suginfo_T *su));
|
|
|
|
static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
|
|
|
|
static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
|
|
|
|
static void spell_try_soundalike __ARGS((suginfo_T *su));
|
|
|
|
static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
|
2005-06-14 22:01:04 +00:00
|
|
|
#if 0
|
2005-06-13 22:28:56 +00:00
|
|
|
static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
|
2005-06-14 22:01:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RESCORE
|
|
|
|
static void add_suggestion __ARGS((suginfo_T *su, char_u *goodword, int use_score, int had_bonus));
|
|
|
|
#else
|
2005-06-13 22:28:56 +00:00
|
|
|
static void add_suggestion __ARGS((suginfo_T *su, char_u *goodword, int use_score));
|
2005-06-14 22:01:04 +00:00
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
static void add_banned __ARGS((suginfo_T *su, char_u *word));
|
|
|
|
static int was_banned __ARGS((suginfo_T *su, char_u *word));
|
|
|
|
static void free_banned __ARGS((suginfo_T *su));
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef RESCORE
|
|
|
|
static void rescore_suggestions __ARGS((suginfo_T *su));
|
|
|
|
#endif
|
|
|
|
static void cleanup_suggestions __ARGS((suginfo_T *su, int keep));
|
2005-06-13 22:28:56 +00:00
|
|
|
static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res));
|
2005-06-14 22:01:04 +00:00
|
|
|
#if defined(RESCORE) || defined(SOUNDFOLD_SCORE)
|
|
|
|
static int spell_sound_score __ARGS((slang_T *slang, char_u *goodword, char_u *badsound));
|
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/*
|
|
|
|
* Use our own character-case definitions, because the current locale may
|
|
|
|
* differ from what the .spl file uses.
|
|
|
|
* These must not be called with negative number!
|
|
|
|
*/
|
|
|
|
#ifndef FEAT_MBYTE
|
|
|
|
/* Non-multi-byte implementation. */
|
|
|
|
# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
|
|
|
|
# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
|
|
|
|
# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
|
|
|
|
#else
|
|
|
|
/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
|
|
|
|
* that for ASCII, because we don't want to use 'casemap' here. Otherwise use
|
|
|
|
* the "w" library function for characters above 255 if available. */
|
|
|
|
# ifdef HAVE_TOWLOWER
|
|
|
|
# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
|
|
|
|
: (c) < 256 ? spelltab.st_fold[c] : towlower(c))
|
|
|
|
# else
|
|
|
|
# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
|
|
|
|
: (c) < 256 ? spelltab.st_fold[c] : (c))
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef HAVE_TOWUPPER
|
|
|
|
# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
|
|
|
|
: (c) < 256 ? spelltab.st_upper[c] : towupper(c))
|
|
|
|
# else
|
|
|
|
# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
|
|
|
|
: (c) < 256 ? spelltab.st_upper[c] : (c))
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef HAVE_ISWUPPER
|
|
|
|
# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
|
|
|
|
: (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
|
|
|
|
# else
|
|
|
|
# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
|
|
|
|
: (c) < 256 ? spelltab.st_isu[c] : (c))
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
static char *e_format = N_("E759: Format error in spell file");
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Main spell-checking function.
|
2005-06-04 21:55:20 +00:00
|
|
|
* "ptr" points to a character that could be the start of a word.
|
2005-04-15 21:00:38 +00:00
|
|
|
* "*attrp" is set to the attributes for a badly spelled word. For a non-word
|
|
|
|
* or when it's OK it remains unchanged.
|
|
|
|
* This must only be called when 'spelllang' is not empty.
|
2005-06-13 22:28:56 +00:00
|
|
|
*
|
|
|
|
* "sug" is normally NULL. When looking for suggestions it points to
|
|
|
|
* suginfo_T. It's passed as a void pointer to keep the struct local.
|
|
|
|
*
|
2005-04-15 21:00:38 +00:00
|
|
|
* Returns the length of the word in bytes, also when it's OK, so that the
|
|
|
|
* caller can skip over the word.
|
|
|
|
*/
|
|
|
|
int
|
2005-06-04 21:55:20 +00:00
|
|
|
spell_check(wp, ptr, attrp)
|
2005-04-15 21:00:38 +00:00
|
|
|
win_T *wp; /* current window */
|
|
|
|
char_u *ptr;
|
|
|
|
int *attrp;
|
|
|
|
{
|
|
|
|
matchinf_T mi; /* Most things are put in "mi" so that it can
|
|
|
|
be passed to functions quickly. */
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/* A word never starts at a space or a control character. Return quickly
|
|
|
|
* then, skipping over the character. */
|
|
|
|
if (*ptr <= ' ')
|
|
|
|
return 1;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* A word starting with a number is always OK. Also skip hexadecimal
|
|
|
|
* numbers 0xFF99 and 0X99FF. */
|
2005-04-15 21:00:38 +00:00
|
|
|
if (*ptr >= '0' && *ptr <= '9')
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
2005-06-08 21:56:31 +00:00
|
|
|
if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
|
|
|
|
mi.mi_end = skiphex(ptr + 2);
|
2005-06-04 21:55:20 +00:00
|
|
|
else
|
|
|
|
mi.mi_end = skipdigits(ptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
/* Find the end of the word. */
|
|
|
|
mi.mi_word = ptr;
|
2005-06-04 21:55:20 +00:00
|
|
|
mi.mi_fend = ptr;
|
2005-06-13 22:28:56 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (SPELL_ISWORDP(mi.mi_fend))
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
/* Make case-folded copy of the characters until the next non-word
|
|
|
|
* character. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
mb_ptr_adv(mi.mi_fend);
|
2005-06-06 21:50:35 +00:00
|
|
|
} while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
2005-04-19 21:30:25 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/* We always use the characters up to the next non-word character,
|
|
|
|
* also for bad words. */
|
|
|
|
mi.mi_end = mi.mi_fend;
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* Check caps type later. */
|
|
|
|
mi.mi_capflags = 0;
|
|
|
|
mi.mi_cend = NULL;
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/* Include one non-word character so that we can check for the
|
|
|
|
* word end. */
|
|
|
|
if (*mi.mi_fend != NUL)
|
|
|
|
mb_ptr_adv(mi.mi_fend);
|
|
|
|
|
|
|
|
(void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
|
|
|
|
MAXWLEN + 1);
|
|
|
|
mi.mi_fwordlen = STRLEN(mi.mi_fword);
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* The word is bad unless we recognize it. */
|
|
|
|
mi.mi_result = SP_BAD;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Loop over the languages specified in 'spelllang'.
|
|
|
|
* We check them all, because a matching word may be longer than an
|
|
|
|
* already found matching word.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
|
|
|
|
mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
|
|
|
|
{
|
|
|
|
/* Check for a matching word in case-folded words. */
|
|
|
|
find_word(&mi, FALSE);
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Check for a matching word in keep-case words. */
|
2005-06-04 21:55:20 +00:00
|
|
|
find_word(&mi, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mi.mi_result != SP_OK)
|
|
|
|
{
|
|
|
|
/* When we are at a non-word character there is no error, just
|
|
|
|
* skip over the character (try looking for a word after it). */
|
2005-06-06 21:50:35 +00:00
|
|
|
if (!SPELL_ISWORDP(ptr))
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
return mb_ptr2len_check(ptr);
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
|
2005-06-04 21:55:20 +00:00
|
|
|
*attrp = highlight_attr[HLF_SPB];
|
|
|
|
else if (mi.mi_result == SP_RARE)
|
|
|
|
*attrp = highlight_attr[HLF_SPR];
|
|
|
|
else
|
|
|
|
*attrp = highlight_attr[HLF_SPL];
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
return (int)(mi.mi_end - ptr);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Check if the word at "mip->mi_word" is in the tree.
|
|
|
|
* When "keepcap" is TRUE check in keep-case word tree.
|
2005-04-19 21:30:25 +00:00
|
|
|
*
|
2005-06-04 21:55:20 +00:00
|
|
|
* For a match mip->mi_result is updated.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static void
|
|
|
|
find_word(mip, keepcap)
|
|
|
|
matchinf_T *mip;
|
|
|
|
int keepcap;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T arridx = 0;
|
2005-06-04 21:55:20 +00:00
|
|
|
int endlen[MAXWLEN]; /* length at possible word endings */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T endidx[MAXWLEN]; /* possible word endings */
|
2005-06-04 21:55:20 +00:00
|
|
|
int endidxcnt = 0;
|
|
|
|
int len;
|
|
|
|
int wlen = 0;
|
|
|
|
int flen;
|
|
|
|
int c;
|
|
|
|
char_u *ptr;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T lo, hi, m;
|
2005-06-04 21:55:20 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
char_u *s;
|
|
|
|
#endif
|
2005-06-06 21:50:35 +00:00
|
|
|
char_u *p;
|
|
|
|
int res = SP_BAD;
|
|
|
|
int valid;
|
2005-06-04 21:55:20 +00:00
|
|
|
slang_T *slang = mip->mi_lp->lp_slang;
|
|
|
|
unsigned flags;
|
|
|
|
char_u *byts;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T *idxs;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (keepcap)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Check for word with matching case in keep-case tree. */
|
|
|
|
ptr = mip->mi_word;
|
|
|
|
flen = 9999; /* no case folding, always enough bytes */
|
|
|
|
byts = slang->sl_kbyts;
|
|
|
|
idxs = slang->sl_kidxs;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check for case-folded in case-folded tree. */
|
|
|
|
ptr = mip->mi_fword;
|
|
|
|
flen = mip->mi_fwordlen; /* available case-folded bytes */
|
|
|
|
byts = slang->sl_fbyts;
|
|
|
|
idxs = slang->sl_fidxs;
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (byts == NULL)
|
|
|
|
return; /* array is empty */
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
2005-06-06 21:50:35 +00:00
|
|
|
* Repeat advancing in the tree until:
|
|
|
|
* - there is a byte that doesn't match,
|
|
|
|
* - we reach the end of the tree,
|
|
|
|
* - or we reach the end of the line.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
for (;;)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
if (flen == 0 && *mip->mi_fend != NUL)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Need to fold at least one more character. Do until next
|
|
|
|
* non-word character for efficiency. */
|
2005-06-06 21:50:35 +00:00
|
|
|
p = mip->mi_fend;
|
2005-06-04 21:55:20 +00:00
|
|
|
do
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
mb_ptr_adv(mip->mi_fend);
|
|
|
|
} while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
|
2005-04-19 21:30:25 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/* Include the non-word character so that we can check for the
|
|
|
|
* word end. */
|
|
|
|
if (*mip->mi_fend != NUL)
|
|
|
|
mb_ptr_adv(mip->mi_fend);
|
|
|
|
|
|
|
|
(void)spell_casefold(p, (int)(mip->mi_fend - p),
|
2005-06-04 21:55:20 +00:00
|
|
|
mip->mi_fword + mip->mi_fwordlen,
|
|
|
|
MAXWLEN - mip->mi_fwordlen);
|
|
|
|
flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
|
|
|
|
mip->mi_fwordlen += flen;
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
len = byts[arridx++];
|
2005-04-19 21:30:25 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* If the first possible byte is a zero the word could end here.
|
|
|
|
* Remember this index, we first check for the longest word. */
|
|
|
|
if (byts[arridx] == 0)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
if (endidxcnt == MAXWLEN)
|
|
|
|
{
|
|
|
|
/* Must be a corrupted spell file. */
|
|
|
|
EMSG(_(e_format));
|
|
|
|
return;
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
endlen[endidxcnt] = wlen;
|
|
|
|
endidx[endidxcnt++] = arridx++;
|
|
|
|
--len;
|
|
|
|
|
|
|
|
/* Skip over the zeros, there can be several flag/region
|
|
|
|
* combinations. */
|
|
|
|
while (len > 0 && byts[arridx] == 0)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
++arridx;
|
|
|
|
--len;
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
if (len == 0)
|
|
|
|
break; /* no children, word must end here */
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Stop looking at end of the line. */
|
|
|
|
if (ptr[wlen] == NUL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Perform a binary search in the list of accepted bytes. */
|
|
|
|
c = ptr[wlen];
|
|
|
|
lo = arridx;
|
|
|
|
hi = arridx + len - 1;
|
|
|
|
while (lo < hi)
|
|
|
|
{
|
|
|
|
m = (lo + hi) / 2;
|
|
|
|
if (byts[m] > c)
|
|
|
|
hi = m - 1;
|
|
|
|
else if (byts[m] < c)
|
|
|
|
lo = m + 1;
|
2005-04-15 21:00:38 +00:00
|
|
|
else
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
lo = hi = m;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
|
|
|
|
/* Stop if there is no matching byte. */
|
|
|
|
if (hi < lo || byts[lo] != c)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Continue at the child (if there is one). */
|
|
|
|
arridx = idxs[lo];
|
|
|
|
++wlen;
|
|
|
|
--flen;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Verify that one of the possible endings is valid. Try the longest
|
|
|
|
* first.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
while (endidxcnt > 0)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
--endidxcnt;
|
|
|
|
arridx = endidx[endidxcnt];
|
|
|
|
wlen = endlen[endidxcnt];
|
2005-04-19 21:30:25 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if ((*mb_head_off)(ptr, ptr + wlen) > 0)
|
|
|
|
continue; /* not at first byte of character */
|
|
|
|
#endif
|
2005-06-06 21:50:35 +00:00
|
|
|
if (SPELL_ISWORDP(ptr + wlen))
|
2005-06-04 21:55:20 +00:00
|
|
|
continue; /* next char is a word character */
|
2005-04-19 21:30:25 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (!keepcap && has_mbyte)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Compute byte length in original word, length may change
|
|
|
|
* when folding case. */
|
|
|
|
p = mip->mi_word;
|
|
|
|
for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
|
|
|
|
mb_ptr_adv(p);
|
|
|
|
wlen = p - mip->mi_word;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
#endif
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Check flags and region. Repeat this if there are more
|
|
|
|
* flags/region alternatives until there is a match. */
|
|
|
|
for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; --len)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
flags = idxs[arridx];
|
2005-06-14 22:01:04 +00:00
|
|
|
|
|
|
|
/* Set a flag for words that were used. The region and case
|
|
|
|
* doesn't matter here, it's only used to rate the suggestions. */
|
|
|
|
idxs[arridx] = flags | WF_USED;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (keepcap)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* For "keepcap" tree the case is always right. */
|
|
|
|
valid = TRUE;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check that the word is in the required case. */
|
|
|
|
if (mip->mi_cend != mip->mi_word + wlen)
|
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
/* mi_capflags was set for a different word length, need
|
|
|
|
* to do it again. */
|
2005-06-04 21:55:20 +00:00
|
|
|
mip->mi_cend = mip->mi_word + wlen;
|
2005-06-13 22:28:56 +00:00
|
|
|
mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
valid = spell_valid_case(mip->mi_capflags, flags);
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (valid)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
if (flags & WF_BANNED)
|
|
|
|
res = SP_BANNED;
|
|
|
|
else if (flags & WF_REGION)
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
/* Check region. */
|
|
|
|
if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
|
|
|
|
res = SP_OK;
|
|
|
|
else
|
|
|
|
res = SP_LOCAL;
|
|
|
|
}
|
|
|
|
else if (flags & WF_RARE)
|
|
|
|
res = SP_RARE;
|
|
|
|
else
|
|
|
|
res = SP_OK;
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
/* Always use the longest match and the best result. */
|
|
|
|
if (mip->mi_result > res)
|
|
|
|
{
|
|
|
|
mip->mi_result = res;
|
|
|
|
mip->mi_end = mip->mi_word + wlen;
|
|
|
|
}
|
|
|
|
else if (mip->mi_result == res
|
|
|
|
&& mip->mi_end < mip->mi_word + wlen)
|
|
|
|
mip->mi_end = mip->mi_word + wlen;
|
|
|
|
|
|
|
|
if (res == SP_OK)
|
|
|
|
break;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
else
|
|
|
|
res = SP_BAD;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
++arridx;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (res == SP_OK)
|
2005-06-04 21:55:20 +00:00
|
|
|
break;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Check case flags for a word. Return TRUE if the word has the requested
|
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spell_valid_case(origflags, treeflags)
|
|
|
|
int origflags; /* flags for the checked word. */
|
|
|
|
int treeflags; /* flags for the word in the spell tree */
|
|
|
|
{
|
|
|
|
return (origflags == WF_ALLCAP
|
|
|
|
|| ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
|
|
|
|
&& ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
|
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Move to next spell error.
|
2005-06-13 22:28:56 +00:00
|
|
|
* "curline" is TRUE for "z?": find word under/after cursor in the same line.
|
2005-06-04 21:55:20 +00:00
|
|
|
* Return OK if found, FAIL otherwise.
|
2005-04-19 21:30:25 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
int
|
2005-06-13 22:28:56 +00:00
|
|
|
spell_move_to(dir, allwords, curline)
|
2005-06-04 21:55:20 +00:00
|
|
|
int dir; /* FORWARD or BACKWARD */
|
|
|
|
int allwords; /* TRUE for "[s" and "]s" */
|
2005-06-13 22:28:56 +00:00
|
|
|
int curline;
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
linenr_T lnum;
|
|
|
|
pos_T found_pos;
|
|
|
|
char_u *line;
|
|
|
|
char_u *p;
|
|
|
|
int attr = 0;
|
|
|
|
int len;
|
|
|
|
int has_syntax = syntax_present(curbuf);
|
|
|
|
int col;
|
|
|
|
int can_spell;
|
2005-04-19 21:30:25 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
EMSG(_("E756: Spell checking not enabled"));
|
|
|
|
return FAIL;
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Start looking for bad word at the start of the line, because we can't
|
|
|
|
* start halfway a word, we don't know where it starts or ends.
|
|
|
|
*
|
|
|
|
* When searching backwards, we continue in the line to find the last
|
|
|
|
* bad word (in the cursor line: before the cursor).
|
|
|
|
*/
|
|
|
|
lnum = curwin->w_cursor.lnum;
|
|
|
|
found_pos.lnum = 0;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
while (!got_int)
|
|
|
|
{
|
2005-04-20 19:37:22 +00:00
|
|
|
line = ml_get(lnum);
|
|
|
|
p = line;
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
while (*p != NUL)
|
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* When searching backward don't search after the cursor. */
|
|
|
|
if (dir == BACKWARD
|
|
|
|
&& lnum == curwin->w_cursor.lnum
|
|
|
|
&& (colnr_T)(p - line) >= curwin->w_cursor.col)
|
|
|
|
break;
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* start of word */
|
|
|
|
len = spell_check(curwin, p, &attr);
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (attr != 0)
|
|
|
|
{
|
|
|
|
/* We found a bad word. Check the attribute. */
|
|
|
|
if (allwords || attr == highlight_attr[HLF_SPB])
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* When searching forward only accept a bad word after
|
|
|
|
* the cursor. */
|
|
|
|
if (dir == BACKWARD
|
|
|
|
|| lnum > curwin->w_cursor.lnum
|
|
|
|
|| (lnum == curwin->w_cursor.lnum
|
2005-06-13 22:28:56 +00:00
|
|
|
&& (colnr_T)(curline ? p - line + len
|
|
|
|
: p - line)
|
2005-06-04 21:55:20 +00:00
|
|
|
> curwin->w_cursor.col))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
if (has_syntax)
|
2005-04-20 19:37:22 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
col = p - line;
|
|
|
|
(void)syn_get_id(lnum, (colnr_T)col,
|
|
|
|
FALSE, &can_spell);
|
|
|
|
|
|
|
|
/* have to get the line again, a multi-line
|
|
|
|
* regexp may make it invalid */
|
|
|
|
line = ml_get(lnum);
|
|
|
|
p = line + col;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
can_spell = TRUE;
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (can_spell)
|
|
|
|
{
|
|
|
|
found_pos.lnum = lnum;
|
|
|
|
found_pos.col = p - line;
|
2005-04-20 19:37:22 +00:00
|
|
|
#ifdef FEAT_VIRTUALEDIT
|
2005-06-04 21:55:20 +00:00
|
|
|
found_pos.coladd = 0;
|
2005-04-20 19:37:22 +00:00
|
|
|
#endif
|
2005-06-04 21:55:20 +00:00
|
|
|
if (dir == FORWARD)
|
|
|
|
{
|
|
|
|
/* No need to search further. */
|
|
|
|
curwin->w_cursor = found_pos;
|
|
|
|
return OK;
|
2005-04-20 19:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
attr = 0;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* advance to character after the word */
|
|
|
|
p += len;
|
|
|
|
if (*p == NUL)
|
|
|
|
break;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
if (curline)
|
|
|
|
return FAIL; /* only check cursor line */
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/* Advance to next line. */
|
2005-04-20 19:37:22 +00:00
|
|
|
if (dir == BACKWARD)
|
|
|
|
{
|
|
|
|
if (found_pos.lnum != 0)
|
|
|
|
{
|
|
|
|
/* Use the last match in the line. */
|
|
|
|
curwin->w_cursor = found_pos;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
if (lnum == 1)
|
|
|
|
return FAIL;
|
|
|
|
--lnum;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lnum == curbuf->b_ml.ml_line_count)
|
|
|
|
return FAIL;
|
|
|
|
++lnum;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
line_breakcheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
return FAIL; /* interrupted */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-06 21:50:35 +00:00
|
|
|
* Load word list(s) for "lang" from Vim spell file(s).
|
2005-06-07 21:00:02 +00:00
|
|
|
* "lang" must be the language without the region: e.g., "en".
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-06 21:50:35 +00:00
|
|
|
static void
|
2005-04-15 21:00:38 +00:00
|
|
|
spell_load_lang(lang)
|
|
|
|
char_u *lang;
|
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
char_u fname_enc[85];
|
2005-04-15 21:00:38 +00:00
|
|
|
int r;
|
2005-06-06 21:50:35 +00:00
|
|
|
char_u langcp[MAXWLEN + 1];
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
/* Copy the language name to pass it to spell_load_cb() as a cookie.
|
2005-06-06 21:50:35 +00:00
|
|
|
* It's truncated when an error is detected. */
|
|
|
|
STRCPY(langcp, lang);
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
/*
|
|
|
|
* Find the first spell file for "lang" in 'runtimepath' and load it.
|
|
|
|
*/
|
|
|
|
vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
|
|
|
|
"spell/%s.%s.spl", lang, spell_enc());
|
|
|
|
r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (r == FAIL && *langcp != NUL)
|
|
|
|
{
|
|
|
|
/* Try loading the ASCII version. */
|
2005-06-07 21:00:02 +00:00
|
|
|
vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
|
2005-05-19 20:53:52 +00:00
|
|
|
"spell/%s.ascii.spl", lang);
|
2005-06-07 21:00:02 +00:00
|
|
|
r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (r == FAIL)
|
|
|
|
smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
|
|
|
|
fname_enc + 6);
|
2005-06-07 21:00:02 +00:00
|
|
|
else if (*langcp != NUL)
|
|
|
|
{
|
|
|
|
/* Load all the additions. */
|
|
|
|
STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
|
|
|
|
do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the encoding used for spell checking: Use 'encoding', except that we
|
|
|
|
* use "latin1" for "latin9". And limit to 60 characters (just in case).
|
|
|
|
*/
|
|
|
|
static char_u *
|
|
|
|
spell_enc()
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
|
|
|
|
return p_enc;
|
|
|
|
#endif
|
|
|
|
return (char_u *)"latin1";
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new slang_T.
|
|
|
|
* Caller must fill "sl_next".
|
|
|
|
*/
|
|
|
|
static slang_T *
|
|
|
|
slang_alloc(lang)
|
|
|
|
char_u *lang;
|
|
|
|
{
|
|
|
|
slang_T *lp;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
lp = (slang_T *)alloc_clear(sizeof(slang_T));
|
2005-04-15 21:00:38 +00:00
|
|
|
if (lp != NULL)
|
|
|
|
{
|
|
|
|
lp->sl_name = vim_strsave(lang);
|
2005-06-13 22:28:56 +00:00
|
|
|
ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
|
|
|
|
ga_init2(&lp->sl_sal, sizeof(fromto_T), 10);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the contents of an slang_T and the structure itself.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
slang_free(lp)
|
|
|
|
slang_T *lp;
|
|
|
|
{
|
|
|
|
vim_free(lp->sl_name);
|
2005-06-07 21:00:02 +00:00
|
|
|
vim_free(lp->sl_fname);
|
|
|
|
slang_clear(lp);
|
|
|
|
vim_free(lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear an slang_T so that the file can be reloaded.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
slang_clear(lp)
|
|
|
|
slang_T *lp;
|
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
garray_T *gap;
|
|
|
|
fromto_T *ftp;
|
|
|
|
int round;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
vim_free(lp->sl_fbyts);
|
2005-06-07 21:00:02 +00:00
|
|
|
lp->sl_fbyts = NULL;
|
2005-06-04 21:55:20 +00:00
|
|
|
vim_free(lp->sl_kbyts);
|
2005-06-07 21:00:02 +00:00
|
|
|
lp->sl_kbyts = NULL;
|
2005-06-04 21:55:20 +00:00
|
|
|
vim_free(lp->sl_fidxs);
|
2005-06-07 21:00:02 +00:00
|
|
|
lp->sl_fidxs = NULL;
|
2005-06-04 21:55:20 +00:00
|
|
|
vim_free(lp->sl_kidxs);
|
2005-06-07 21:00:02 +00:00
|
|
|
lp->sl_kidxs = NULL;
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
for (round = 1; round <= 2; ++round)
|
|
|
|
{
|
|
|
|
gap = round == 1 ? &lp->sl_rep : &lp->sl_sal;
|
|
|
|
while (gap->ga_len > 0)
|
|
|
|
{
|
|
|
|
ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
|
|
|
|
vim_free(ftp->ft_from);
|
|
|
|
vim_free(ftp->ft_to);
|
|
|
|
}
|
|
|
|
ga_clear(gap);
|
|
|
|
}
|
|
|
|
|
|
|
|
vim_free(lp->sl_map);
|
|
|
|
lp->sl_map = NULL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-06 21:50:35 +00:00
|
|
|
* Load one spell file and store the info into a slang_T.
|
2005-04-15 21:00:38 +00:00
|
|
|
* Invoked through do_in_runtimepath().
|
|
|
|
*/
|
|
|
|
static void
|
2005-06-07 21:00:02 +00:00
|
|
|
spell_load_cb(fname, cookie)
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u *fname;
|
2005-06-06 21:50:35 +00:00
|
|
|
void *cookie; /* points to the language name */
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
(void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
|
2005-06-07 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load one spell file and store the info into a slang_T.
|
|
|
|
*
|
|
|
|
* This is invoked in two ways:
|
|
|
|
* - From spell_load_cb() to load a spell file for the first time. "lang" is
|
|
|
|
* the language name, "old_lp" is NULL. Will allocate an slang_T.
|
|
|
|
* - To reload a spell file that was changed. "lang" is NULL and "old_lp"
|
|
|
|
* points to the existing slang_T.
|
2005-06-13 22:28:56 +00:00
|
|
|
* Returns the slang_T the spell file was loaded into. NULL for error.
|
2005-06-07 21:00:02 +00:00
|
|
|
*/
|
2005-06-13 22:28:56 +00:00
|
|
|
static slang_T *
|
|
|
|
spell_load_file(fname, lang, old_lp, silent)
|
2005-06-07 21:00:02 +00:00
|
|
|
char_u *fname;
|
|
|
|
char_u *lang;
|
|
|
|
slang_T *old_lp;
|
2005-06-13 22:28:56 +00:00
|
|
|
int silent; /* no error if file doesn't exist */
|
2005-06-07 21:00:02 +00:00
|
|
|
{
|
2005-04-15 21:00:38 +00:00
|
|
|
FILE *fd;
|
|
|
|
char_u buf[MAXWLEN + 1];
|
|
|
|
char_u *p;
|
|
|
|
int i;
|
2005-06-04 21:55:20 +00:00
|
|
|
int len;
|
2005-04-15 21:00:38 +00:00
|
|
|
int round;
|
|
|
|
char_u *save_sourcing_name = sourcing_name;
|
|
|
|
linenr_T save_sourcing_lnum = sourcing_lnum;
|
2005-04-23 20:42:23 +00:00
|
|
|
int cnt, ccnt;
|
|
|
|
char_u *fol;
|
2005-06-06 21:50:35 +00:00
|
|
|
slang_T *lp = NULL;
|
2005-06-13 22:28:56 +00:00
|
|
|
garray_T *gap;
|
|
|
|
fromto_T *ftp;
|
|
|
|
int rr;
|
|
|
|
short *first;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T idx;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
fd = mch_fopen((char *)fname, "r");
|
2005-04-15 21:00:38 +00:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
if (!silent)
|
|
|
|
EMSG2(_(e_notopen), fname);
|
|
|
|
else if (p_verbose > 2)
|
|
|
|
{
|
|
|
|
verbose_enter();
|
|
|
|
smsg((char_u *)e_notopen, fname);
|
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-04-23 20:42:23 +00:00
|
|
|
goto endFAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-07 21:00:02 +00:00
|
|
|
if (p_verbose > 2)
|
|
|
|
{
|
|
|
|
verbose_enter();
|
|
|
|
smsg((char_u *)_("Reading spell file \"%s\""), fname);
|
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (old_lp == NULL)
|
|
|
|
{
|
|
|
|
lp = slang_alloc(lang);
|
|
|
|
if (lp == NULL)
|
|
|
|
goto endFAIL;
|
|
|
|
|
|
|
|
/* Remember the file name, used to reload the file when it's updated. */
|
|
|
|
lp->sl_fname = vim_strsave(fname);
|
|
|
|
if (lp->sl_fname == NULL)
|
|
|
|
goto endFAIL;
|
|
|
|
|
|
|
|
/* Check for .add.spl. */
|
|
|
|
lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lp = old_lp;
|
2005-06-06 21:50:35 +00:00
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/* Set sourcing_name, so that error messages mention the file name. */
|
|
|
|
sourcing_name = fname;
|
|
|
|
sourcing_lnum = 0;
|
|
|
|
|
2005-04-23 20:42:23 +00:00
|
|
|
/* <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
|
|
* <charflagslen> <charflags> <fcharslen> <fchars> */
|
2005-04-15 21:00:38 +00:00
|
|
|
for (i = 0; i < VIMSPELLMAGICL; ++i)
|
|
|
|
buf[i] = getc(fd); /* <fileID> */
|
|
|
|
if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
|
|
|
|
{
|
|
|
|
EMSG(_("E757: Wrong file ID in spell file"));
|
2005-04-23 20:42:23 +00:00
|
|
|
goto endFAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnt = getc(fd); /* <regioncnt> */
|
2005-04-23 20:42:23 +00:00
|
|
|
if (cnt < 0)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
truncerr:
|
|
|
|
EMSG(_("E758: Truncated spell file"));
|
2005-04-23 20:42:23 +00:00
|
|
|
goto endFAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
if (cnt > 8)
|
|
|
|
{
|
|
|
|
formerr:
|
2005-06-06 21:50:35 +00:00
|
|
|
EMSG(_(e_format));
|
2005-04-23 20:42:23 +00:00
|
|
|
goto endFAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < cnt; ++i)
|
|
|
|
{
|
|
|
|
lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
|
|
|
|
lp->sl_regions[i * 2 + 1] = getc(fd);
|
|
|
|
}
|
|
|
|
lp->sl_regions[cnt * 2] = NUL;
|
|
|
|
|
2005-04-23 20:42:23 +00:00
|
|
|
cnt = getc(fd); /* <charflagslen> */
|
|
|
|
if (cnt > 0)
|
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
p = alloc((unsigned)cnt);
|
2005-04-23 20:42:23 +00:00
|
|
|
if (p == NULL)
|
|
|
|
goto endFAIL;
|
|
|
|
for (i = 0; i < cnt; ++i)
|
|
|
|
p[i] = getc(fd); /* <charflags> */
|
|
|
|
|
|
|
|
ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
|
|
|
|
if (ccnt <= 0)
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
vim_free(p);
|
2005-04-23 20:42:23 +00:00
|
|
|
goto formerr;
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
|
|
|
fol = alloc((unsigned)ccnt + 1);
|
2005-04-23 20:42:23 +00:00
|
|
|
if (fol == NULL)
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
vim_free(p);
|
2005-04-23 20:42:23 +00:00
|
|
|
goto endFAIL;
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
2005-04-23 20:42:23 +00:00
|
|
|
for (i = 0; i < ccnt; ++i)
|
|
|
|
fol[i] = getc(fd); /* <fchars> */
|
|
|
|
fol[i] = NUL;
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Set the word-char flags and fill SPELL_ISUPPER() table. */
|
2005-06-04 21:55:20 +00:00
|
|
|
i = set_spell_charflags(p, cnt, fol);
|
|
|
|
vim_free(p);
|
|
|
|
vim_free(fol);
|
|
|
|
if (i == FAIL)
|
2005-04-23 20:42:23 +00:00
|
|
|
goto formerr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* When <charflagslen> is zero then <fcharlen> must also be zero. */
|
|
|
|
cnt = (getc(fd) << 8) + getc(fd);
|
|
|
|
if (cnt != 0)
|
|
|
|
goto formerr;
|
|
|
|
}
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/* <SUGGEST> : <repcount> <rep> ...
|
|
|
|
* <salflags> <salcount> <sal> ...
|
|
|
|
* <maplen> <mapstr> */
|
|
|
|
for (round = 1; round <= 2; ++round)
|
|
|
|
{
|
|
|
|
if (round == 1)
|
|
|
|
{
|
|
|
|
gap = &lp->sl_rep;
|
|
|
|
first = lp->sl_rep_first;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gap = &lp->sl_sal;
|
|
|
|
first = lp->sl_sal_first;
|
|
|
|
|
|
|
|
i = getc(fd); /* <salflags> */
|
|
|
|
if (i & SAL_F0LLOWUP)
|
|
|
|
lp->sl_followup = TRUE;
|
|
|
|
if (i & SAL_COLLAPSE)
|
|
|
|
lp->sl_collapse = TRUE;
|
|
|
|
if (i & SAL_REM_ACCENTS)
|
|
|
|
lp->sl_rem_accents = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt = (getc(fd) << 8) + getc(fd); /* <repcount> or <salcount> */
|
|
|
|
if (cnt < 0)
|
|
|
|
goto formerr;
|
|
|
|
|
|
|
|
if (ga_grow(gap, cnt) == FAIL)
|
|
|
|
goto endFAIL;
|
|
|
|
for (; gap->ga_len < cnt; ++gap->ga_len)
|
|
|
|
{
|
|
|
|
/* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
|
|
|
|
/* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
|
|
|
|
ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
|
|
|
|
for (rr = 1; rr <= 2; ++rr)
|
|
|
|
{
|
|
|
|
ccnt = getc(fd);
|
|
|
|
if (ccnt < 0)
|
|
|
|
{
|
|
|
|
if (rr == 2)
|
|
|
|
vim_free(ftp->ft_from);
|
|
|
|
goto formerr;
|
|
|
|
}
|
|
|
|
if ((p = alloc(ccnt + 1)) == NULL)
|
|
|
|
{
|
|
|
|
if (rr == 2)
|
|
|
|
vim_free(ftp->ft_from);
|
|
|
|
goto endFAIL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ccnt; ++i)
|
|
|
|
p[i] = getc(fd); /* <repfrom> or <salfrom> */
|
|
|
|
p[i] = NUL;
|
|
|
|
if (rr == 1)
|
|
|
|
ftp->ft_from = p;
|
|
|
|
else
|
|
|
|
ftp->ft_to = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the first-index table. */
|
|
|
|
for (i = 0; i < 256; ++i)
|
|
|
|
first[i] = -1;
|
|
|
|
for (i = 0; i < gap->ga_len; ++i)
|
|
|
|
{
|
|
|
|
ftp = &((fromto_T *)gap->ga_data)[i];
|
|
|
|
if (first[*ftp->ft_from] == -1)
|
|
|
|
first[*ftp->ft_from] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
|
|
|
|
if (cnt < 0)
|
|
|
|
goto formerr;
|
|
|
|
p = alloc(cnt + 1);
|
|
|
|
if (p == NULL)
|
|
|
|
goto endFAIL;
|
|
|
|
for (i = 0; i < cnt; ++i)
|
|
|
|
p[i] = getc(fd); /* <mapstr> */
|
|
|
|
p[i] = NUL;
|
|
|
|
lp->sl_map = p;
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* round 1: <LWORDTREE>
|
|
|
|
* round 2: <KWORDTREE> */
|
|
|
|
for (round = 1; round <= 2; ++round)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* The tree size was computed when writing the file, so that we can
|
|
|
|
* allocate it as one long block. <nodecount> */
|
|
|
|
len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
|
|
if (len < 0)
|
2005-04-15 21:00:38 +00:00
|
|
|
goto truncerr;
|
2005-06-04 21:55:20 +00:00
|
|
|
if (len > 0)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Allocate the byte array. */
|
|
|
|
p = lalloc((long_u)len, TRUE);
|
|
|
|
if (p == NULL)
|
2005-04-23 20:42:23 +00:00
|
|
|
goto endFAIL;
|
2005-06-04 21:55:20 +00:00
|
|
|
if (round == 1)
|
|
|
|
lp->sl_fbyts = p;
|
2005-04-20 19:37:22 +00:00
|
|
|
else
|
2005-06-04 21:55:20 +00:00
|
|
|
lp->sl_kbyts = p;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Allocate the index array. */
|
|
|
|
p = lalloc_clear((long_u)(len * sizeof(int)), TRUE);
|
|
|
|
if (p == NULL)
|
|
|
|
goto endFAIL;
|
|
|
|
if (round == 1)
|
2005-06-14 22:01:04 +00:00
|
|
|
lp->sl_fidxs = (idx_T *)p;
|
2005-04-19 21:30:25 +00:00
|
|
|
else
|
2005-06-14 22:01:04 +00:00
|
|
|
lp->sl_kidxs = (idx_T *)p;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-04-19 21:30:25 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Read the tree and store it in the array. */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx = read_tree(fd,
|
2005-06-04 21:55:20 +00:00
|
|
|
round == 1 ? lp->sl_fbyts : lp->sl_kbyts,
|
|
|
|
round == 1 ? lp->sl_fidxs : lp->sl_kidxs,
|
|
|
|
len, 0);
|
2005-06-14 22:01:04 +00:00
|
|
|
if (idx == -1)
|
2005-06-04 21:55:20 +00:00
|
|
|
goto truncerr;
|
2005-06-14 22:01:04 +00:00
|
|
|
if (idx < 0)
|
2005-06-04 21:55:20 +00:00
|
|
|
goto formerr;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
/* For a new file link it in the list of spell files. */
|
|
|
|
if (old_lp == NULL)
|
|
|
|
{
|
|
|
|
lp->sl_next = first_lang;
|
|
|
|
first_lang = lp;
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
|
2005-04-23 20:42:23 +00:00
|
|
|
goto endOK;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-04-23 20:42:23 +00:00
|
|
|
endFAIL:
|
2005-06-07 21:00:02 +00:00
|
|
|
if (lang != NULL)
|
|
|
|
/* truncating the name signals the error to spell_load_lang() */
|
|
|
|
*lang = NUL;
|
|
|
|
if (lp != NULL && old_lp == NULL)
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
slang_free(lp);
|
2005-06-13 22:28:56 +00:00
|
|
|
lp = NULL;
|
|
|
|
}
|
2005-04-23 20:42:23 +00:00
|
|
|
|
|
|
|
endOK:
|
2005-04-15 21:00:38 +00:00
|
|
|
if (fd != NULL)
|
|
|
|
fclose(fd);
|
|
|
|
sourcing_name = save_sourcing_name;
|
|
|
|
sourcing_lnum = save_sourcing_lnum;
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
return lp;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Read one row of siblings from the spell file and store it in the byte array
|
|
|
|
* "byts" and index array "idxs". Recursively read the children.
|
|
|
|
*
|
|
|
|
* NOTE: The code here must match put_tree().
|
|
|
|
*
|
|
|
|
* Returns the index follosing the siblings.
|
|
|
|
* Returns -1 if the file is shorter than expected.
|
|
|
|
* Returns -2 if there is a format error.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-14 22:01:04 +00:00
|
|
|
static idx_T
|
2005-06-04 21:55:20 +00:00
|
|
|
read_tree(fd, byts, idxs, maxidx, startidx)
|
|
|
|
FILE *fd;
|
|
|
|
char_u *byts;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T *idxs;
|
2005-06-04 21:55:20 +00:00
|
|
|
int maxidx; /* size of arrays */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T startidx; /* current index in "byts" and "idxs" */
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
int n;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T idx = startidx;
|
2005-06-04 21:55:20 +00:00
|
|
|
int c;
|
|
|
|
#define SHARED_MASK 0x8000000
|
|
|
|
|
|
|
|
len = getc(fd); /* <siblingcount> */
|
|
|
|
if (len <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (startidx + len >= maxidx)
|
|
|
|
return -2;
|
|
|
|
byts[idx++] = len;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Read the byte values, flag/region bytes and shared indexes. */
|
|
|
|
for (i = 1; i <= len; ++i)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
c = getc(fd); /* <byte> */
|
|
|
|
if (c < 0)
|
|
|
|
return -1;
|
|
|
|
if (c <= BY_SPECIAL)
|
|
|
|
{
|
|
|
|
if (c == BY_NOFLAGS)
|
|
|
|
{
|
|
|
|
/* No flags, all regions. */
|
|
|
|
idxs[idx] = 0;
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
else if (c == BY_FLAGS)
|
|
|
|
{
|
|
|
|
/* Read flags and option region. */
|
|
|
|
c = getc(fd); /* <flags> */
|
|
|
|
if (c & WF_REGION)
|
|
|
|
c = (getc(fd) << 8) + c; /* <region> */
|
|
|
|
idxs[idx] = c;
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
else /* c == BY_INDEX */
|
|
|
|
{
|
|
|
|
/* <nodeidx> */
|
|
|
|
n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
|
|
if (n < 0 || n >= maxidx)
|
|
|
|
return -2;
|
|
|
|
idxs[idx] = n + SHARED_MASK;
|
|
|
|
c = getc(fd); /* <xbyte> */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
byts[idx++] = c;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Recursively read the children for non-shared siblings.
|
|
|
|
* Skip the end-of-word ones (zero byte value) and the shared ones (and
|
|
|
|
* remove SHARED_MASK) */
|
|
|
|
for (i = 1; i <= len; ++i)
|
|
|
|
if (byts[startidx + i] != 0)
|
|
|
|
{
|
|
|
|
if (idxs[startidx + i] & SHARED_MASK)
|
|
|
|
idxs[startidx + i] &= ~SHARED_MASK;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idxs[startidx + i] = idx;
|
|
|
|
idx = read_tree(fd, byts, idxs, maxidx, idx);
|
|
|
|
if (idx < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
return idx;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse 'spelllang' and set buf->b_langp accordingly.
|
|
|
|
* Returns an error message or NULL.
|
|
|
|
*/
|
|
|
|
char_u *
|
|
|
|
did_set_spelllang(buf)
|
|
|
|
buf_T *buf;
|
|
|
|
{
|
|
|
|
garray_T ga;
|
|
|
|
char_u *lang;
|
|
|
|
char_u *e;
|
|
|
|
char_u *region;
|
|
|
|
int region_mask;
|
|
|
|
slang_T *lp;
|
|
|
|
int c;
|
|
|
|
char_u lbuf[MAXWLEN + 1];
|
2005-06-13 22:28:56 +00:00
|
|
|
char_u spf_name[MAXPATHL];
|
|
|
|
int did_spf = FALSE;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
ga_init2(&ga, sizeof(langp_T), 2);
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/* Get the name of the .spl file associated with 'spellfile'. */
|
|
|
|
if (*buf->b_p_spf == NUL)
|
|
|
|
did_spf = TRUE;
|
|
|
|
else
|
|
|
|
vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
|
|
|
|
buf->b_p_spf);
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/* loop over comma separated languages. */
|
|
|
|
for (lang = buf->b_p_spl; *lang != NUL; lang = e)
|
|
|
|
{
|
|
|
|
e = vim_strchr(lang, ',');
|
|
|
|
if (e == NULL)
|
|
|
|
e = lang + STRLEN(lang);
|
2005-04-17 20:18:43 +00:00
|
|
|
region = NULL;
|
2005-04-15 21:00:38 +00:00
|
|
|
if (e > lang + 2)
|
|
|
|
{
|
|
|
|
if (e - lang >= MAXWLEN)
|
|
|
|
{
|
|
|
|
ga_clear(&ga);
|
|
|
|
return e_invarg;
|
|
|
|
}
|
|
|
|
if (lang[2] == '_')
|
|
|
|
region = lang + 3;
|
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/* Check if we loaded this language before. */
|
2005-04-15 21:00:38 +00:00
|
|
|
for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
|
|
if (STRNICMP(lp->sl_name, lang, 2) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (lp == NULL)
|
|
|
|
{
|
|
|
|
/* Not found, load the language. */
|
2005-06-13 22:28:56 +00:00
|
|
|
vim_strncpy(lbuf, lang, e - lang);
|
2005-04-15 21:00:38 +00:00
|
|
|
if (region != NULL)
|
|
|
|
mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
|
2005-06-06 21:50:35 +00:00
|
|
|
spell_load_lang(lbuf);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/*
|
|
|
|
* Loop over the languages, there can be several files for each.
|
|
|
|
*/
|
|
|
|
for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
|
|
if (STRNICMP(lp->sl_name, lang, 2) == 0)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-08 21:56:31 +00:00
|
|
|
region_mask = REGION_ALL;
|
|
|
|
if (region != NULL)
|
2005-06-06 21:50:35 +00:00
|
|
|
{
|
|
|
|
/* find region in sl_regions */
|
|
|
|
c = find_region(lp->sl_regions, region);
|
|
|
|
if (c == REGION_ALL)
|
|
|
|
{
|
2005-06-08 21:56:31 +00:00
|
|
|
if (!lp->sl_add)
|
|
|
|
{
|
|
|
|
c = *e;
|
|
|
|
*e = NUL;
|
|
|
|
smsg((char_u *)_("Warning: region %s not supported"),
|
2005-06-06 21:50:35 +00:00
|
|
|
lang);
|
2005-06-08 21:56:31 +00:00
|
|
|
*e = c;
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
region_mask = 1 << c;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (ga_grow(&ga, 1) == FAIL)
|
|
|
|
{
|
|
|
|
ga_clear(&ga);
|
|
|
|
return e_outofmem;
|
|
|
|
}
|
|
|
|
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
|
|
|
|
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
|
|
|
|
++ga.ga_len;
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* Check if this is the 'spellfile' spell file. */
|
|
|
|
if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
|
|
|
|
did_spf = TRUE;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*e == ',')
|
|
|
|
++e;
|
|
|
|
}
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
|
|
|
|
* then it's probably loaded above already. Otherwise load it here.
|
|
|
|
*/
|
|
|
|
if (!did_spf)
|
|
|
|
{
|
|
|
|
for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
|
|
if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
|
|
|
|
break;
|
|
|
|
if (lp == NULL)
|
|
|
|
{
|
|
|
|
vim_strncpy(lbuf, gettail(spf_name), 2);
|
|
|
|
lp = spell_load_file(spf_name, lbuf, NULL, TRUE);
|
|
|
|
}
|
|
|
|
if (lp != NULL && ga_grow(&ga, 1) == OK)
|
|
|
|
{
|
|
|
|
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
|
|
|
|
LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
|
|
|
|
++ga.ga_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/* Add a NULL entry to mark the end of the list. */
|
|
|
|
if (ga_grow(&ga, 1) == FAIL)
|
|
|
|
{
|
|
|
|
ga_clear(&ga);
|
|
|
|
return e_outofmem;
|
|
|
|
}
|
|
|
|
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
|
|
|
|
++ga.ga_len;
|
|
|
|
|
|
|
|
/* Everything is fine, store the new b_langp value. */
|
|
|
|
ga_clear(&buf->b_langp);
|
|
|
|
buf->b_langp = ga;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the region "region[2]" in "rp" (points to "sl_regions").
|
|
|
|
* Each region is simply stored as the two characters of it's name.
|
|
|
|
* Returns the index if found, REGION_ALL if not found.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
find_region(rp, region)
|
|
|
|
char_u *rp;
|
|
|
|
char_u *region;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; ; i += 2)
|
|
|
|
{
|
|
|
|
if (rp[i] == NUL)
|
|
|
|
return REGION_ALL;
|
|
|
|
if (rp[i] == region[0] && rp[i + 1] == region[1])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return i / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-13 22:28:56 +00:00
|
|
|
* Return case type of word:
|
2005-04-15 21:00:38 +00:00
|
|
|
* w word 0
|
2005-06-04 21:55:20 +00:00
|
|
|
* Word WF_ONECAP
|
|
|
|
* W WORD WF_ALLCAP
|
|
|
|
* WoRd wOrd WF_KEEPCAP
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
captype(word, end)
|
|
|
|
char_u *word;
|
2005-06-13 22:28:56 +00:00
|
|
|
char_u *end; /* When NULL use up to NUL byte. */
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
char_u *p;
|
|
|
|
int c;
|
|
|
|
int firstcap;
|
|
|
|
int allcap;
|
|
|
|
int past_second = FALSE; /* past second word char */
|
|
|
|
|
|
|
|
/* find first letter */
|
2005-06-06 21:50:35 +00:00
|
|
|
for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
|
2005-06-13 22:28:56 +00:00
|
|
|
if (end == NULL ? *p == NUL : p >= end)
|
2005-04-15 21:00:38 +00:00
|
|
|
return 0; /* only non-word characters, illegal word */
|
|
|
|
#ifdef FEAT_MBYTE
|
2005-06-07 21:00:02 +00:00
|
|
|
if (has_mbyte)
|
|
|
|
c = mb_ptr2char_adv(&p);
|
|
|
|
else
|
2005-04-15 21:00:38 +00:00
|
|
|
#endif
|
2005-06-07 21:00:02 +00:00
|
|
|
c = *p++;
|
2005-06-14 22:01:04 +00:00
|
|
|
firstcap = allcap = SPELL_ISUPPER(c);
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to check all letters to find a word with mixed upper/lower.
|
|
|
|
* But a word with an upper char only at start is a ONECAP.
|
|
|
|
*/
|
2005-06-13 22:28:56 +00:00
|
|
|
for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
|
2005-06-06 21:50:35 +00:00
|
|
|
if (SPELL_ISWORDP(p))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
c = mb_ptr2char(p);
|
|
|
|
#else
|
|
|
|
c = *p;
|
|
|
|
#endif
|
2005-06-14 22:01:04 +00:00
|
|
|
if (!SPELL_ISUPPER(c))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
/* UUl -> KEEPCAP */
|
|
|
|
if (past_second && allcap)
|
2005-06-04 21:55:20 +00:00
|
|
|
return WF_KEEPCAP;
|
2005-04-15 21:00:38 +00:00
|
|
|
allcap = FALSE;
|
|
|
|
}
|
|
|
|
else if (!allcap)
|
|
|
|
/* UlU -> KEEPCAP */
|
2005-06-04 21:55:20 +00:00
|
|
|
return WF_KEEPCAP;
|
2005-04-15 21:00:38 +00:00
|
|
|
past_second = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allcap)
|
2005-06-04 21:55:20 +00:00
|
|
|
return WF_ALLCAP;
|
2005-04-15 21:00:38 +00:00
|
|
|
if (firstcap)
|
2005-06-04 21:55:20 +00:00
|
|
|
return WF_ONECAP;
|
2005-04-15 21:00:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# if defined(FEAT_MBYTE) || defined(PROTO)
|
|
|
|
/*
|
|
|
|
* Clear all spelling tables and reload them.
|
2005-06-06 21:50:35 +00:00
|
|
|
* Used after 'encoding' is set and when ":mkspell" was used.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
spell_reload()
|
|
|
|
{
|
|
|
|
buf_T *buf;
|
|
|
|
slang_T *lp;
|
2005-06-08 21:56:31 +00:00
|
|
|
win_T *wp;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/* Initialize the table for SPELL_ISWORDP(). */
|
2005-04-15 21:00:38 +00:00
|
|
|
init_spell_chartab();
|
|
|
|
|
|
|
|
/* Unload all allocated memory. */
|
|
|
|
while (first_lang != NULL)
|
|
|
|
{
|
|
|
|
lp = first_lang;
|
|
|
|
first_lang = lp->sl_next;
|
|
|
|
slang_free(lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go through all buffers and handle 'spelllang'. */
|
|
|
|
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
|
|
|
|
{
|
|
|
|
ga_clear(&buf->b_langp);
|
2005-06-08 21:56:31 +00:00
|
|
|
|
|
|
|
/* Only load the wordlists when 'spelllang' is set and there is a
|
|
|
|
* window for this buffer in which 'spell' is set. */
|
2005-04-15 21:00:38 +00:00
|
|
|
if (*buf->b_p_spl != NUL)
|
2005-06-08 21:56:31 +00:00
|
|
|
{
|
|
|
|
FOR_ALL_WINDOWS(wp)
|
|
|
|
if (wp->w_buffer == buf && wp->w_p_spell)
|
|
|
|
{
|
|
|
|
(void)did_set_spelllang(buf);
|
|
|
|
# ifdef FEAT_WINDOWS
|
|
|
|
break;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
/*
|
|
|
|
* Reload the spell file "fname" if it's loaded.
|
|
|
|
*/
|
|
|
|
static void
|
2005-06-13 22:28:56 +00:00
|
|
|
spell_reload_one(fname, added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
char_u *fname;
|
2005-06-13 22:28:56 +00:00
|
|
|
int added_word; /* invoked through "zg" */
|
2005-06-07 21:00:02 +00:00
|
|
|
{
|
|
|
|
slang_T *lp;
|
2005-06-13 22:28:56 +00:00
|
|
|
int didit = FALSE;
|
2005-06-07 21:00:02 +00:00
|
|
|
|
|
|
|
for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
|
|
if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
|
|
|
|
{
|
|
|
|
slang_clear(lp);
|
2005-06-13 22:28:56 +00:00
|
|
|
(void)spell_load_file(fname, NULL, lp, FALSE);
|
2005-06-07 21:00:02 +00:00
|
|
|
redraw_all_later(NOT_VALID);
|
2005-06-13 22:28:56 +00:00
|
|
|
didit = TRUE;
|
2005-06-07 21:00:02 +00:00
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* When "zg" was used and the file wasn't loaded yet, should redo
|
|
|
|
* 'spelllang' to get it loaded. */
|
|
|
|
if (added_word && !didit)
|
|
|
|
did_set_spelllang(curbuf);
|
2005-06-07 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions for ":mkspell".
|
|
|
|
*/
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
|
2005-04-15 21:00:38 +00:00
|
|
|
and .dic file. */
|
|
|
|
/*
|
|
|
|
* Main structure to store the contents of a ".aff" file.
|
|
|
|
*/
|
|
|
|
typedef struct afffile_S
|
|
|
|
{
|
|
|
|
char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
|
2005-06-07 21:00:02 +00:00
|
|
|
int af_rar; /* RAR ID for rare word */
|
|
|
|
int af_kep; /* KEP ID for keep-case word */
|
2005-04-15 21:00:38 +00:00
|
|
|
hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
|
|
|
|
hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
|
|
|
|
} afffile_T;
|
|
|
|
|
|
|
|
typedef struct affentry_S affentry_T;
|
|
|
|
/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
|
|
|
|
struct affentry_S
|
|
|
|
{
|
|
|
|
affentry_T *ae_next; /* next affix with same name/number */
|
|
|
|
char_u *ae_chop; /* text to chop off basic word (can be NULL) */
|
|
|
|
char_u *ae_add; /* text to add to basic word (can be NULL) */
|
|
|
|
char_u *ae_cond; /* condition (NULL for ".") */
|
|
|
|
regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
|
|
|
|
};
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Affix header from ".aff" file. Used for af_pref and af_suff. */
|
|
|
|
typedef struct affheader_S
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u ah_key[2]; /* key for hashtable == name of affix entry */
|
|
|
|
int ah_combine; /* suffix may combine with prefix */
|
|
|
|
affentry_T *ah_first; /* first affix entry */
|
|
|
|
} affheader_T;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Structure that is used to store the items in the word tree. This avoids
|
|
|
|
* the need to keep track of each allocated thing, it's freed all at once
|
|
|
|
* after ":mkspell" is done.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
#define SBLOCKSIZE 16000 /* size of sb_data */
|
|
|
|
typedef struct sblock_S sblock_T;
|
|
|
|
struct sblock_S
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
sblock_T *sb_next; /* next block in list */
|
|
|
|
int sb_used; /* nr of bytes already in use */
|
|
|
|
char_u sb_data[1]; /* data, actually longer */
|
2005-04-15 21:00:38 +00:00
|
|
|
};
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* A node in the tree.
|
|
|
|
*/
|
|
|
|
typedef struct wordnode_S wordnode_T;
|
|
|
|
struct wordnode_S
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u wn_hashkey[6]; /* room for the hash key */
|
|
|
|
wordnode_T *wn_next; /* next node with same hash key */
|
|
|
|
wordnode_T *wn_child; /* child (next byte in word) */
|
|
|
|
wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
|
|
|
|
always sorted) */
|
|
|
|
wordnode_T *wn_wnode; /* parent node that will write this node */
|
|
|
|
int wn_index; /* index in written nodes (valid after first
|
|
|
|
round) */
|
|
|
|
char_u wn_byte; /* Byte for this node. NUL for word end */
|
|
|
|
char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
|
|
|
|
char_u wn_region; /* when wn_byte is NUL: region mask */
|
|
|
|
};
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Info used while reading the spell files.
|
|
|
|
*/
|
|
|
|
typedef struct spellinfo_S
|
2005-04-20 19:37:22 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
wordnode_T *si_foldroot; /* tree with case-folded words */
|
|
|
|
wordnode_T *si_keeproot; /* tree with keep-case words */
|
|
|
|
sblock_T *si_blocks; /* memory blocks used */
|
|
|
|
int si_ascii; /* handling only ASCII words */
|
2005-06-07 21:00:02 +00:00
|
|
|
int si_add; /* addition file */
|
2005-06-04 21:55:20 +00:00
|
|
|
int si_region; /* region mask */
|
|
|
|
vimconv_T si_conv; /* for conversion to 'encoding' */
|
2005-06-05 21:54:54 +00:00
|
|
|
int si_memtot; /* runtime memory used */
|
2005-06-07 21:00:02 +00:00
|
|
|
int si_verbose; /* verbose messages */
|
2005-06-08 21:56:31 +00:00
|
|
|
int si_region_count; /* number of regions supported (1 when there
|
|
|
|
are no regions) */
|
|
|
|
char_u si_region_name[16]; /* region names (if count > 1) */
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
garray_T si_rep; /* list of fromto_T entries from REP lines */
|
|
|
|
garray_T si_sal; /* list of fromto_T entries from SAL lines */
|
|
|
|
int si_followup; /* soundsalike: ? */
|
|
|
|
int si_collapse; /* soundsalike: ? */
|
|
|
|
int si_rem_accents; /* soundsalike: remove accents */
|
|
|
|
garray_T si_map; /* MAP info concatenated */
|
2005-06-04 21:55:20 +00:00
|
|
|
} spellinfo_T;
|
|
|
|
|
|
|
|
static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
|
2005-06-13 22:28:56 +00:00
|
|
|
static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
|
|
|
|
static int sal_to_bool __ARGS((char_u *s));
|
2005-04-17 20:18:43 +00:00
|
|
|
static int has_non_ascii __ARGS((char_u *s));
|
2005-06-04 21:55:20 +00:00
|
|
|
static void spell_free_aff __ARGS((afffile_T *aff));
|
|
|
|
static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
|
2005-06-06 21:50:35 +00:00
|
|
|
static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, hashtab_T *ht, hashtab_T *xht, int comb, int flags));
|
2005-06-04 21:55:20 +00:00
|
|
|
static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
|
|
|
|
static void *getroom __ARGS((sblock_T **blp, size_t len));
|
|
|
|
static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
|
|
|
|
static void free_blocks __ARGS((sblock_T *bl));
|
|
|
|
static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
|
2005-06-08 21:56:31 +00:00
|
|
|
static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region));
|
2005-06-04 21:55:20 +00:00
|
|
|
static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, sblock_T **blp));
|
2005-06-07 21:00:02 +00:00
|
|
|
static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
|
2005-06-04 21:55:20 +00:00
|
|
|
static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
|
|
|
|
static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
|
2005-06-08 21:56:31 +00:00
|
|
|
static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
|
2005-06-04 21:55:20 +00:00
|
|
|
static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask));
|
2005-06-13 22:28:56 +00:00
|
|
|
static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
|
2005-06-07 21:00:02 +00:00
|
|
|
static void init_spellfile __ARGS((void));
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
2005-06-13 22:28:56 +00:00
|
|
|
* Read the affix file "fname".
|
2005-06-08 21:56:31 +00:00
|
|
|
* Returns an afffile_T, NULL for complete failure.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
static afffile_T *
|
2005-06-04 21:55:20 +00:00
|
|
|
spell_read_aff(fname, spin)
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u *fname;
|
2005-06-04 21:55:20 +00:00
|
|
|
spellinfo_T *spin;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
afffile_T *aff;
|
|
|
|
char_u rline[MAXLINELEN];
|
|
|
|
char_u *line;
|
|
|
|
char_u *pc = NULL;
|
|
|
|
char_u *(items[6]);
|
|
|
|
int itemcnt;
|
|
|
|
char_u *p;
|
|
|
|
int lnum = 0;
|
|
|
|
affheader_T *cur_aff = NULL;
|
|
|
|
int aff_todo = 0;
|
|
|
|
hashtab_T *tp;
|
2005-04-23 20:42:23 +00:00
|
|
|
char_u *low = NULL;
|
|
|
|
char_u *fol = NULL;
|
|
|
|
char_u *upp = NULL;
|
2005-06-06 21:50:35 +00:00
|
|
|
static char *e_affname = N_("Affix name too long in %s line %d: %s");
|
2005-06-13 22:28:56 +00:00
|
|
|
int do_rep;
|
|
|
|
int do_sal;
|
|
|
|
int do_map;
|
|
|
|
int found_map = FALSE;
|
2005-06-14 22:01:04 +00:00
|
|
|
hashitem_T *hi;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Open the file.
|
|
|
|
*/
|
2005-06-07 21:00:02 +00:00
|
|
|
fd = mch_fopen((char *)fname, "r");
|
2005-04-15 21:00:38 +00:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
EMSG2(_(e_notopen), fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (spin->si_verbose || p_verbose > 2)
|
|
|
|
{
|
|
|
|
if (!spin->si_verbose)
|
|
|
|
verbose_enter();
|
|
|
|
smsg((char_u *)_("Reading affix file %s..."), fname);
|
|
|
|
out_flush();
|
|
|
|
if (!spin->si_verbose)
|
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/* Only do REP lines when not done in another .aff file already. */
|
|
|
|
do_rep = spin->si_rep.ga_len == 0;
|
|
|
|
|
|
|
|
/* Only do SAL lines when not done in another .aff file already. */
|
|
|
|
do_sal = spin->si_sal.ga_len == 0;
|
|
|
|
|
|
|
|
/* Only do MAP lines when not done in another .aff file already. */
|
|
|
|
do_map = spin->si_map.ga_len == 0;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Allocate and init the afffile_T structure.
|
|
|
|
*/
|
|
|
|
aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
|
2005-04-15 21:00:38 +00:00
|
|
|
if (aff == NULL)
|
|
|
|
return NULL;
|
|
|
|
hash_init(&aff->af_pref);
|
|
|
|
hash_init(&aff->af_suff);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read all the lines in the file one by one.
|
|
|
|
*/
|
2005-04-23 20:42:23 +00:00
|
|
|
while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-04-23 20:42:23 +00:00
|
|
|
line_breakcheck();
|
2005-04-15 21:00:38 +00:00
|
|
|
++lnum;
|
|
|
|
|
|
|
|
/* Skip comment lines. */
|
|
|
|
if (*rline == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Convert from "SET" to 'encoding' when needed. */
|
|
|
|
vim_free(pc);
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
2005-06-04 21:55:20 +00:00
|
|
|
if (spin->si_conv.vc_type != CONV_NONE)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
pc = string_convert(&spin->si_conv, rline, NULL);
|
2005-04-23 20:42:23 +00:00
|
|
|
if (pc == NULL)
|
|
|
|
{
|
|
|
|
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
|
|
fname, lnum, rline);
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
line = pc;
|
|
|
|
}
|
|
|
|
else
|
2005-06-07 21:00:02 +00:00
|
|
|
#endif
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
pc = NULL;
|
|
|
|
line = rline;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split the line up in white separated items. Put a NUL after each
|
|
|
|
* item. */
|
|
|
|
itemcnt = 0;
|
|
|
|
for (p = line; ; )
|
|
|
|
{
|
|
|
|
while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
|
|
|
|
++p;
|
|
|
|
if (*p == NUL)
|
|
|
|
break;
|
2005-06-04 21:55:20 +00:00
|
|
|
if (itemcnt == 6) /* too many items */
|
|
|
|
break;
|
2005-04-15 21:00:38 +00:00
|
|
|
items[itemcnt++] = p;
|
2005-06-04 21:55:20 +00:00
|
|
|
while (*p > ' ') /* skip until white space or CR/NL */
|
2005-04-15 21:00:38 +00:00
|
|
|
++p;
|
|
|
|
if (*p == NUL)
|
|
|
|
break;
|
|
|
|
*p++ = NUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle non-empty lines. */
|
|
|
|
if (itemcnt > 0)
|
|
|
|
{
|
|
|
|
if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
|
|
|
|
&& aff->af_enc == NULL)
|
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Setup for conversion from "ENC" to 'encoding'. */
|
|
|
|
aff->af_enc = enc_canonize(items[1]);
|
|
|
|
if (aff->af_enc != NULL && !spin->si_ascii
|
|
|
|
&& convert_setup(&spin->si_conv, aff->af_enc,
|
|
|
|
p_enc) == FAIL)
|
|
|
|
smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
|
|
|
fname, aff->af_enc, p_enc);
|
2005-06-07 21:00:02 +00:00
|
|
|
#else
|
|
|
|
smsg((char_u *)_("Conversion in %s not supported"), fname);
|
|
|
|
#endif
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-05 21:54:54 +00:00
|
|
|
else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
|
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
/* ignored, we always split */
|
2005-06-05 21:54:54 +00:00
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
/* ignored, we look in the tree for what chars may appear */
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
|
|
|
|
&& aff->af_rar == 0)
|
|
|
|
{
|
|
|
|
aff->af_rar = items[1][0];
|
|
|
|
if (items[1][1] != NUL)
|
|
|
|
smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
|
|
}
|
2005-06-07 21:00:02 +00:00
|
|
|
else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
|
|
|
|
&& aff->af_kep == 0)
|
2005-06-06 21:50:35 +00:00
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
aff->af_kep = items[1][0];
|
2005-06-06 21:50:35 +00:00
|
|
|
if (items[1][1] != NUL)
|
|
|
|
smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
else if ((STRCMP(items[0], "PFX") == 0
|
|
|
|
|| STRCMP(items[0], "SFX") == 0)
|
|
|
|
&& aff_todo == 0
|
|
|
|
&& itemcnt == 4)
|
|
|
|
{
|
|
|
|
/* New affix letter. */
|
2005-06-04 21:55:20 +00:00
|
|
|
cur_aff = (affheader_T *)getroom(&spin->si_blocks,
|
|
|
|
sizeof(affheader_T));
|
2005-04-15 21:00:38 +00:00
|
|
|
if (cur_aff == NULL)
|
|
|
|
break;
|
|
|
|
cur_aff->ah_key[0] = *items[1];
|
|
|
|
cur_aff->ah_key[1] = NUL;
|
|
|
|
if (items[1][1] != NUL)
|
2005-06-06 21:50:35 +00:00
|
|
|
smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
2005-04-15 21:00:38 +00:00
|
|
|
if (*items[2] == 'Y')
|
|
|
|
cur_aff->ah_combine = TRUE;
|
2005-06-04 21:55:20 +00:00
|
|
|
else if (*items[2] != 'N')
|
2005-04-15 21:00:38 +00:00
|
|
|
smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
|
|
|
|
fname, lnum, items[2]);
|
|
|
|
if (*items[0] == 'P')
|
|
|
|
tp = &aff->af_pref;
|
|
|
|
else
|
|
|
|
tp = &aff->af_suff;
|
2005-06-04 21:55:20 +00:00
|
|
|
aff_todo = atoi((char *)items[3]);
|
2005-06-14 22:01:04 +00:00
|
|
|
hi = hash_find(tp, cur_aff->ah_key);
|
|
|
|
if (!HASHITEM_EMPTY(hi))
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
2005-04-15 21:00:38 +00:00
|
|
|
smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
|
|
|
|
fname, lnum, items[1]);
|
2005-06-04 21:55:20 +00:00
|
|
|
aff_todo = 0;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
else
|
|
|
|
hash_add(tp, cur_aff->ah_key);
|
|
|
|
}
|
|
|
|
else if ((STRCMP(items[0], "PFX") == 0
|
|
|
|
|| STRCMP(items[0], "SFX") == 0)
|
|
|
|
&& aff_todo > 0
|
|
|
|
&& STRCMP(cur_aff->ah_key, items[1]) == 0
|
|
|
|
&& itemcnt == 5)
|
|
|
|
{
|
|
|
|
affentry_T *aff_entry;
|
|
|
|
|
|
|
|
/* New item for an affix letter. */
|
|
|
|
--aff_todo;
|
2005-06-04 21:55:20 +00:00
|
|
|
aff_entry = (affentry_T *)getroom(&spin->si_blocks,
|
|
|
|
sizeof(affentry_T));
|
2005-04-15 21:00:38 +00:00
|
|
|
if (aff_entry == NULL)
|
|
|
|
break;
|
2005-04-17 20:18:43 +00:00
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
if (STRCMP(items[2], "0") != 0)
|
2005-06-04 21:55:20 +00:00
|
|
|
aff_entry->ae_chop = getroom_save(&spin->si_blocks,
|
|
|
|
items[2]);
|
2005-04-15 21:00:38 +00:00
|
|
|
if (STRCMP(items[3], "0") != 0)
|
2005-06-04 21:55:20 +00:00
|
|
|
aff_entry->ae_add = getroom_save(&spin->si_blocks,
|
|
|
|
items[3]);
|
2005-04-17 20:18:43 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Don't use an affix entry with non-ASCII characters when
|
|
|
|
* "spin->si_ascii" is TRUE. */
|
|
|
|
if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
|
2005-04-17 20:18:43 +00:00
|
|
|
|| has_non_ascii(aff_entry->ae_add)))
|
|
|
|
{
|
|
|
|
aff_entry->ae_next = cur_aff->ah_first;
|
|
|
|
cur_aff->ah_first = aff_entry;
|
2005-06-04 21:55:20 +00:00
|
|
|
|
|
|
|
if (STRCMP(items[4], ".") != 0)
|
|
|
|
{
|
|
|
|
char_u buf[MAXLINELEN];
|
|
|
|
|
|
|
|
aff_entry->ae_cond = getroom_save(&spin->si_blocks,
|
|
|
|
items[4]);
|
|
|
|
if (*items[0] == 'P')
|
|
|
|
sprintf((char *)buf, "^%s", items[4]);
|
|
|
|
else
|
|
|
|
sprintf((char *)buf, "%s$", items[4]);
|
|
|
|
aff_entry->ae_prog = vim_regcomp(buf,
|
|
|
|
RE_MAGIC + RE_STRING);
|
|
|
|
}
|
2005-04-17 20:18:43 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-04-23 20:42:23 +00:00
|
|
|
else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
|
|
|
|
{
|
|
|
|
if (fol != NULL)
|
|
|
|
smsg((char_u *)_("Duplicate FOL in %s line %d"),
|
|
|
|
fname, lnum);
|
|
|
|
else
|
|
|
|
fol = vim_strsave(items[1]);
|
|
|
|
}
|
|
|
|
else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
|
|
|
|
{
|
|
|
|
if (low != NULL)
|
|
|
|
smsg((char_u *)_("Duplicate LOW in %s line %d"),
|
|
|
|
fname, lnum);
|
|
|
|
else
|
|
|
|
low = vim_strsave(items[1]);
|
|
|
|
}
|
|
|
|
else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
|
|
|
|
{
|
|
|
|
if (upp != NULL)
|
|
|
|
smsg((char_u *)_("Duplicate UPP in %s line %d"),
|
|
|
|
fname, lnum);
|
|
|
|
else
|
|
|
|
upp = vim_strsave(items[1]);
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
2005-04-15 21:00:38 +00:00
|
|
|
/* Ignore REP count */;
|
2005-06-13 22:28:56 +00:00
|
|
|
if (!isdigit(*items[1]))
|
|
|
|
smsg((char_u *)_("Expected REP count in %s line %d"),
|
|
|
|
fname, lnum);
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
|
|
|
|
{
|
|
|
|
/* REP item */
|
2005-06-13 22:28:56 +00:00
|
|
|
if (do_rep)
|
|
|
|
add_fromto(spin, &spin->si_rep, items[1], items[2]);
|
|
|
|
}
|
|
|
|
else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
|
|
|
|
{
|
|
|
|
/* MAP item or count */
|
|
|
|
if (!found_map)
|
|
|
|
{
|
|
|
|
/* First line contains the count. */
|
|
|
|
found_map = TRUE;
|
|
|
|
if (!isdigit(*items[1]))
|
|
|
|
smsg((char_u *)_("Expected MAP count in %s line %d"),
|
|
|
|
fname, lnum);
|
|
|
|
}
|
|
|
|
else if (do_map)
|
|
|
|
{
|
|
|
|
/* We simply concatenate all the MAP strings, separated by
|
|
|
|
* slashes. */
|
|
|
|
ga_concat(&spin->si_map, items[1]);
|
|
|
|
ga_append(&spin->si_map, '/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
|
|
|
|
{
|
|
|
|
if (do_sal)
|
|
|
|
{
|
|
|
|
/* SAL item (sounds-a-like)
|
|
|
|
* Either one of the known keys or a from-to pair. */
|
|
|
|
if (STRCMP(items[1], "followup") == 0)
|
|
|
|
spin->si_followup = sal_to_bool(items[2]);
|
|
|
|
else if (STRCMP(items[1], "collapse_result") == 0)
|
|
|
|
spin->si_collapse = sal_to_bool(items[2]);
|
|
|
|
else if (STRCMP(items[1], "remove_accents") == 0)
|
|
|
|
spin->si_rem_accents = sal_to_bool(items[2]);
|
|
|
|
else
|
|
|
|
/* when "to" is "_" it means empty */
|
|
|
|
add_fromto(spin, &spin->si_sal, items[1],
|
|
|
|
STRCMP(items[2], "_") == 0 ? (char_u *)""
|
|
|
|
: items[2]);
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
else
|
2005-04-15 21:00:38 +00:00
|
|
|
smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
|
|
|
|
fname, lnum, items[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-23 20:42:23 +00:00
|
|
|
if (fol != NULL || low != NULL || upp != NULL)
|
|
|
|
{
|
2005-06-08 21:56:31 +00:00
|
|
|
/*
|
|
|
|
* Don't write a word table for an ASCII file, so that we don't check
|
|
|
|
* for conflicts with a word table that matches 'encoding'.
|
2005-06-14 22:01:04 +00:00
|
|
|
* Don't write one for utf-8 either, we use utf_*() and
|
2005-06-08 21:56:31 +00:00
|
|
|
* mb_get_class(), the list of chars in the file will be incomplete.
|
|
|
|
*/
|
|
|
|
if (!spin->si_ascii
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
&& !enc_utf8
|
|
|
|
#endif
|
|
|
|
)
|
2005-04-24 21:58:05 +00:00
|
|
|
{
|
|
|
|
if (fol == NULL || low == NULL || upp == NULL)
|
|
|
|
smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
|
|
|
|
else
|
2005-06-08 21:56:31 +00:00
|
|
|
(void)set_spell_chartab(fol, low, upp);
|
2005-04-24 21:58:05 +00:00
|
|
|
}
|
2005-04-23 20:42:23 +00:00
|
|
|
|
|
|
|
vim_free(fol);
|
|
|
|
vim_free(low);
|
|
|
|
vim_free(upp);
|
|
|
|
}
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
vim_free(pc);
|
|
|
|
fclose(fd);
|
|
|
|
return aff;
|
|
|
|
}
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Add a from-to item to "gap". Used for REP and SAL items.
|
|
|
|
* They are stored case-folded.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
add_fromto(spin, gap, from, to)
|
|
|
|
spellinfo_T *spin;
|
|
|
|
garray_T *gap;
|
|
|
|
char_u *from;
|
|
|
|
char_u *to;
|
|
|
|
{
|
|
|
|
fromto_T *ftp;
|
|
|
|
char_u word[MAXWLEN];
|
|
|
|
|
|
|
|
if (ga_grow(gap, 1) == OK)
|
|
|
|
{
|
|
|
|
ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
|
|
|
|
(void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
|
|
|
|
ftp->ft_from = getroom_save(&spin->si_blocks, word);
|
|
|
|
(void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
|
|
|
|
ftp->ft_to = getroom_save(&spin->si_blocks, word);
|
|
|
|
++gap->ga_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a boolean argument in a SAL line to TRUE or FALSE;
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sal_to_bool(s)
|
|
|
|
char_u *s;
|
|
|
|
{
|
|
|
|
return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 20:18:43 +00:00
|
|
|
/*
|
|
|
|
* Return TRUE if string "s" contains a non-ASCII character (128 or higher).
|
|
|
|
* When "s" is NULL FALSE is returned.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
has_non_ascii(s)
|
|
|
|
char_u *s;
|
|
|
|
{
|
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
if (s != NULL)
|
|
|
|
for (p = s; *p != NUL; ++p)
|
|
|
|
if (*p >= 128)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
|
|
|
* Free the structure filled by spell_read_aff().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spell_free_aff(aff)
|
|
|
|
afffile_T *aff;
|
|
|
|
{
|
|
|
|
hashtab_T *ht;
|
|
|
|
hashitem_T *hi;
|
|
|
|
int todo;
|
|
|
|
affheader_T *ah;
|
2005-06-04 21:55:20 +00:00
|
|
|
affentry_T *ae;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
vim_free(aff->af_enc);
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* All this trouble to foree the "ae_prog" items... */
|
2005-04-15 21:00:38 +00:00
|
|
|
for (ht = &aff->af_pref; ; ht = &aff->af_suff)
|
|
|
|
{
|
|
|
|
todo = ht->ht_used;
|
|
|
|
for (hi = ht->ht_array; todo > 0; ++hi)
|
|
|
|
{
|
|
|
|
if (!HASHITEM_EMPTY(hi))
|
|
|
|
{
|
|
|
|
--todo;
|
|
|
|
ah = HI2AH(hi);
|
2005-06-04 21:55:20 +00:00
|
|
|
for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
|
|
|
vim_free(ae->ae_prog);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ht == &aff->af_suff)
|
|
|
|
break;
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
hash_clear(&aff->af_pref);
|
|
|
|
hash_clear(&aff->af_suff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Read dictionary file "fname".
|
2005-04-15 21:00:38 +00:00
|
|
|
* Returns OK or FAIL;
|
|
|
|
*/
|
|
|
|
static int
|
2005-06-04 21:55:20 +00:00
|
|
|
spell_read_dic(fname, spin, affile)
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u *fname;
|
2005-06-04 21:55:20 +00:00
|
|
|
spellinfo_T *spin;
|
|
|
|
afffile_T *affile;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
hashtab_T ht;
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u line[MAXLINELEN];
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *afflist;
|
|
|
|
char_u *dw;
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u *pc;
|
|
|
|
char_u *w;
|
|
|
|
int l;
|
|
|
|
hash_T hash;
|
|
|
|
hashitem_T *hi;
|
|
|
|
FILE *fd;
|
|
|
|
int lnum = 1;
|
2005-06-04 21:55:20 +00:00
|
|
|
int non_ascii = 0;
|
|
|
|
int retval = OK;
|
|
|
|
char_u message[MAXLINELEN + MAXWLEN];
|
2005-06-06 21:50:35 +00:00
|
|
|
int flags;
|
2005-06-04 21:55:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the file.
|
|
|
|
*/
|
2005-06-07 21:00:02 +00:00
|
|
|
fd = mch_fopen((char *)fname, "r");
|
2005-04-15 21:00:38 +00:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
EMSG2(_(e_notopen), fname);
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* The hashtable is only used to detect duplicated words. */
|
|
|
|
hash_init(&ht);
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (spin->si_verbose || p_verbose > 2)
|
|
|
|
{
|
|
|
|
if (!spin->si_verbose)
|
|
|
|
verbose_enter();
|
|
|
|
smsg((char_u *)_("Reading dictionary file %s..."), fname);
|
|
|
|
out_flush();
|
|
|
|
if (!spin->si_verbose)
|
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/* Read and ignore the first line: word count. */
|
|
|
|
(void)vim_fgets(line, MAXLINELEN, fd);
|
2005-06-14 22:01:04 +00:00
|
|
|
if (!vim_isdigit(*skipwhite(line)))
|
2005-04-15 21:00:38 +00:00
|
|
|
EMSG2(_("E760: No word count in %s"), fname);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read all the lines in the file one by one.
|
|
|
|
* The words are converted to 'encoding' here, before being added to
|
|
|
|
* the hashtable.
|
|
|
|
*/
|
2005-04-23 20:42:23 +00:00
|
|
|
while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-04-23 20:42:23 +00:00
|
|
|
line_breakcheck();
|
2005-04-15 21:00:38 +00:00
|
|
|
++lnum;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Remove CR, LF and white space from the end. White space halfway
|
|
|
|
* the word is kept to allow e.g., "et al.". */
|
2005-04-15 21:00:38 +00:00
|
|
|
l = STRLEN(line);
|
|
|
|
while (l > 0 && line[l - 1] <= ' ')
|
|
|
|
--l;
|
|
|
|
if (l == 0)
|
|
|
|
continue; /* empty line */
|
|
|
|
line[l] = NUL;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* This takes time, print a message now and then. */
|
2005-06-07 21:00:02 +00:00
|
|
|
if (spin->si_verbose && (lnum & 0x3ff) == 0)
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
vim_snprintf((char *)message, sizeof(message),
|
|
|
|
_("line %6d - %s"), lnum, line);
|
|
|
|
msg_start();
|
|
|
|
msg_outtrans_attr(message, 0);
|
|
|
|
msg_clr_eos();
|
|
|
|
msg_didout = FALSE;
|
|
|
|
msg_col = 0;
|
|
|
|
out_flush();
|
|
|
|
}
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/* Find the optional affix names. */
|
2005-06-04 21:55:20 +00:00
|
|
|
afflist = vim_strchr(line, '/');
|
|
|
|
if (afflist != NULL)
|
|
|
|
*afflist++ = NUL;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
|
|
if (spin->si_ascii && has_non_ascii(line))
|
|
|
|
{
|
|
|
|
++non_ascii;
|
2005-04-17 20:18:43 +00:00
|
|
|
continue;
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
2005-04-17 20:18:43 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
2005-04-15 21:00:38 +00:00
|
|
|
/* Convert from "SET" to 'encoding' when needed. */
|
2005-06-04 21:55:20 +00:00
|
|
|
if (spin->si_conv.vc_type != CONV_NONE)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
pc = string_convert(&spin->si_conv, line, NULL);
|
2005-04-23 20:42:23 +00:00
|
|
|
if (pc == NULL)
|
|
|
|
{
|
|
|
|
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
|
|
fname, lnum, line);
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
w = pc;
|
|
|
|
}
|
|
|
|
else
|
2005-06-07 21:00:02 +00:00
|
|
|
#endif
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
pc = NULL;
|
|
|
|
w = line;
|
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Store the word in the hashtable to be able to find duplicates. */
|
|
|
|
dw = (char_u *)getroom_save(&spin->si_blocks, w);
|
2005-04-15 21:00:38 +00:00
|
|
|
if (dw == NULL)
|
2005-06-04 21:55:20 +00:00
|
|
|
retval = FAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
vim_free(pc);
|
2005-06-04 21:55:20 +00:00
|
|
|
if (retval == FAIL)
|
|
|
|
break;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
hash = hash_hash(dw);
|
|
|
|
hi = hash_lookup(&ht, dw, hash);
|
2005-04-15 21:00:38 +00:00
|
|
|
if (!HASHITEM_EMPTY(hi))
|
|
|
|
smsg((char_u *)_("Duplicate word in %s line %d: %s"),
|
2005-06-04 21:55:20 +00:00
|
|
|
fname, lnum, line);
|
2005-04-15 21:00:38 +00:00
|
|
|
else
|
2005-06-04 21:55:20 +00:00
|
|
|
hash_add_item(&ht, hi, dw, hash);
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
flags = 0;
|
|
|
|
if (afflist != NULL)
|
|
|
|
{
|
|
|
|
/* Check for affix name that stands for keep-case word and stands
|
|
|
|
* for rare word (if defined). */
|
2005-06-07 21:00:02 +00:00
|
|
|
if (affile->af_kep != NUL
|
|
|
|
&& vim_strchr(afflist, affile->af_kep) != NULL)
|
2005-06-06 21:50:35 +00:00
|
|
|
flags |= WF_KEEPCAP;
|
|
|
|
if (affile->af_rar != NUL
|
|
|
|
&& vim_strchr(afflist, affile->af_rar) != NULL)
|
|
|
|
flags |= WF_RARE;
|
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Add the word to the word tree(s). */
|
2005-06-08 21:56:31 +00:00
|
|
|
if (store_word(dw, spin, flags, spin->si_region) == FAIL)
|
2005-06-04 21:55:20 +00:00
|
|
|
retval = FAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (afflist != NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Find all matching suffixes and add the resulting words.
|
|
|
|
* Additionally do matching prefixes that combine. */
|
|
|
|
if (store_aff_word(dw, spin, afflist,
|
2005-06-06 21:50:35 +00:00
|
|
|
&affile->af_suff, &affile->af_pref,
|
|
|
|
FALSE, flags) == FAIL)
|
2005-06-04 21:55:20 +00:00
|
|
|
retval = FAIL;
|
|
|
|
|
|
|
|
/* Find all matching prefixes and add the resulting words. */
|
|
|
|
if (store_aff_word(dw, spin, afflist,
|
2005-06-06 21:50:35 +00:00
|
|
|
&affile->af_pref, NULL, FALSE, flags) == FAIL)
|
2005-06-04 21:55:20 +00:00
|
|
|
retval = FAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
|
|
|
|
if (spin->si_ascii && non_ascii > 0)
|
|
|
|
smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
|
|
non_ascii);
|
|
|
|
hash_clear(&ht);
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
return retval;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Apply affixes to a word and store the resulting words.
|
|
|
|
* "ht" is the hashtable with affentry_T that need to be applied, either
|
|
|
|
* prefixes or suffixes.
|
|
|
|
* "xht", when not NULL, is the prefix hashtable, to be used additionally on
|
|
|
|
* the resulting words for combining affixes.
|
|
|
|
*
|
|
|
|
* Returns FAIL when out of memory.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
static int
|
2005-06-06 21:50:35 +00:00
|
|
|
store_aff_word(word, spin, afflist, ht, xht, comb, flags)
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *word; /* basic word start */
|
|
|
|
spellinfo_T *spin; /* spell info */
|
|
|
|
char_u *afflist; /* list of names of supported affixes */
|
|
|
|
hashtab_T *ht;
|
|
|
|
hashtab_T *xht;
|
|
|
|
int comb; /* only use affixes that combine */
|
2005-06-06 21:50:35 +00:00
|
|
|
int flags; /* flags for the word */
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
int todo;
|
2005-04-15 21:00:38 +00:00
|
|
|
hashitem_T *hi;
|
2005-06-04 21:55:20 +00:00
|
|
|
affheader_T *ah;
|
|
|
|
affentry_T *ae;
|
|
|
|
regmatch_T regmatch;
|
|
|
|
char_u newword[MAXWLEN];
|
2005-04-15 21:00:38 +00:00
|
|
|
int retval = OK;
|
2005-06-04 21:55:20 +00:00
|
|
|
int i;
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u *p;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
todo = ht->ht_used;
|
|
|
|
for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
if (!HASHITEM_EMPTY(hi))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
--todo;
|
|
|
|
ah = HI2AH(hi);
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Check that the affix combines, if required, and that the word
|
|
|
|
* supports this affix. */
|
|
|
|
if ((!comb || ah->ah_combine)
|
|
|
|
&& vim_strchr(afflist, *ah->ah_key) != NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Loop over all affix entries with this name. */
|
|
|
|
for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Check the condition. It's not logical to match case
|
|
|
|
* here, but it is required for compatibility with
|
|
|
|
* Myspell. */
|
|
|
|
regmatch.regprog = ae->ae_prog;
|
|
|
|
regmatch.rm_ic = FALSE;
|
|
|
|
if (ae->ae_prog == NULL
|
|
|
|
|| vim_regexec(®match, word, (colnr_T)0))
|
|
|
|
{
|
|
|
|
/* Match. Remove the chop and add the affix. */
|
|
|
|
if (xht == NULL)
|
2005-04-17 20:18:43 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* prefix: chop/add at the start of the word */
|
|
|
|
if (ae->ae_add == NULL)
|
|
|
|
*newword = NUL;
|
|
|
|
else
|
|
|
|
STRCPY(newword, ae->ae_add);
|
|
|
|
p = word;
|
|
|
|
if (ae->ae_chop != NULL)
|
2005-06-07 21:00:02 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Skip chop string. */
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
2005-06-14 22:01:04 +00:00
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
i = mb_charlen(ae->ae_chop);
|
2005-06-14 22:01:04 +00:00
|
|
|
for ( ; i > 0; --i)
|
|
|
|
mb_ptr_adv(p);
|
|
|
|
}
|
2005-06-07 21:00:02 +00:00
|
|
|
else
|
|
|
|
#endif
|
2005-06-14 22:01:04 +00:00
|
|
|
p += STRLEN(ae->ae_chop);
|
2005-06-07 21:00:02 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
STRCAT(newword, p);
|
2005-04-17 20:18:43 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
else
|
2005-04-17 20:18:43 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* suffix: chop/add at the end of the word */
|
|
|
|
STRCPY(newword, word);
|
|
|
|
if (ae->ae_chop != NULL)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Remove chop string. */
|
|
|
|
p = newword + STRLEN(newword);
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
i = mb_charlen(ae->ae_chop);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
i = STRLEN(ae->ae_chop);
|
|
|
|
for ( ; i > 0; --i)
|
2005-06-04 21:55:20 +00:00
|
|
|
mb_ptr_back(newword, p);
|
|
|
|
*p = NUL;
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
if (ae->ae_add != NULL)
|
|
|
|
STRCAT(newword, ae->ae_add);
|
2005-04-17 20:18:43 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Store the modified word. */
|
2005-06-08 21:56:31 +00:00
|
|
|
if (store_word(newword, spin,
|
|
|
|
flags, spin->si_region) == FAIL)
|
2005-06-04 21:55:20 +00:00
|
|
|
retval = FAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* When added a suffix and combining is allowed also
|
|
|
|
* try adding prefixes additionally. */
|
|
|
|
if (xht != NULL && ah->ah_combine)
|
|
|
|
if (store_aff_word(newword, spin, afflist,
|
2005-06-06 21:50:35 +00:00
|
|
|
xht, NULL, TRUE, flags) == FAIL)
|
2005-06-04 21:55:20 +00:00
|
|
|
retval = FAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Read a file with a list of words.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
static int
|
2005-06-04 21:55:20 +00:00
|
|
|
spell_read_wordfile(fname, spin)
|
|
|
|
char_u *fname;
|
|
|
|
spellinfo_T *spin;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
FILE *fd;
|
|
|
|
long lnum = 0;
|
|
|
|
char_u rline[MAXLINELEN];
|
|
|
|
char_u *line;
|
|
|
|
char_u *pc = NULL;
|
|
|
|
int l;
|
|
|
|
int retval = OK;
|
|
|
|
int did_word = FALSE;
|
|
|
|
int non_ascii = 0;
|
2005-06-06 21:50:35 +00:00
|
|
|
int flags;
|
2005-06-08 21:56:31 +00:00
|
|
|
int regionmask;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Open the file.
|
|
|
|
*/
|
2005-06-07 21:00:02 +00:00
|
|
|
fd = mch_fopen((char *)fname, "r");
|
2005-06-04 21:55:20 +00:00
|
|
|
if (fd == NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
EMSG2(_(e_notopen), fname);
|
|
|
|
return FAIL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (spin->si_verbose || p_verbose > 2)
|
|
|
|
{
|
|
|
|
if (!spin->si_verbose)
|
|
|
|
verbose_enter();
|
|
|
|
smsg((char_u *)_("Reading word file %s..."), fname);
|
|
|
|
out_flush();
|
|
|
|
if (!spin->si_verbose)
|
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Read all the lines in the file one by one.
|
|
|
|
*/
|
|
|
|
while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
line_breakcheck();
|
|
|
|
++lnum;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Skip comment lines. */
|
|
|
|
if (*rline == '#')
|
|
|
|
continue;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Remove CR, LF and white space from the end. */
|
|
|
|
l = STRLEN(rline);
|
|
|
|
while (l > 0 && rline[l - 1] <= ' ')
|
|
|
|
--l;
|
|
|
|
if (l == 0)
|
|
|
|
continue; /* empty or blank line */
|
|
|
|
rline[l] = NUL;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Convert from "=encoding={encoding}" to 'encoding' when needed. */
|
|
|
|
vim_free(pc);
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
2005-06-04 21:55:20 +00:00
|
|
|
if (spin->si_conv.vc_type != CONV_NONE)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
pc = string_convert(&spin->si_conv, rline, NULL);
|
|
|
|
if (pc == NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
|
|
fname, lnum, rline);
|
2005-04-15 21:00:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
line = pc;
|
|
|
|
}
|
|
|
|
else
|
2005-06-07 21:00:02 +00:00
|
|
|
#endif
|
2005-06-04 21:55:20 +00:00
|
|
|
{
|
|
|
|
pc = NULL;
|
|
|
|
line = rline;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
flags = 0;
|
2005-06-08 21:56:31 +00:00
|
|
|
regionmask = spin->si_region;
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
if (*line == '/')
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
++line;
|
2005-06-08 21:56:31 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (STRNCMP(line, "encoding=", 9) == 0)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
if (spin->si_conv.vc_type != CONV_NONE)
|
2005-06-08 21:56:31 +00:00
|
|
|
smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
|
|
|
|
fname, lnum, line - 1);
|
2005-06-04 21:55:20 +00:00
|
|
|
else if (did_word)
|
2005-06-08 21:56:31 +00:00
|
|
|
smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
|
|
|
|
fname, lnum, line - 1);
|
2005-04-15 21:00:38 +00:00
|
|
|
else
|
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
char_u *enc;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Setup for conversion to 'encoding'. */
|
2005-06-08 21:56:31 +00:00
|
|
|
line += 10;
|
|
|
|
enc = enc_canonize(line);
|
2005-06-04 21:55:20 +00:00
|
|
|
if (enc != NULL && !spin->si_ascii
|
|
|
|
&& convert_setup(&spin->si_conv, enc,
|
|
|
|
p_enc) == FAIL)
|
|
|
|
smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
2005-06-08 21:56:31 +00:00
|
|
|
fname, line, p_enc);
|
2005-06-04 21:55:20 +00:00
|
|
|
vim_free(enc);
|
2005-06-07 21:00:02 +00:00
|
|
|
#else
|
|
|
|
smsg((char_u *)_("Conversion in %s not supported"), fname);
|
|
|
|
#endif
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
continue;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
|
2005-06-08 21:56:31 +00:00
|
|
|
if (STRNCMP(line, "regions=", 8) == 0)
|
|
|
|
{
|
|
|
|
if (spin->si_region_count > 1)
|
|
|
|
smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
|
|
|
|
fname, lnum, line);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line += 8;
|
|
|
|
if (STRLEN(line) > 16)
|
|
|
|
smsg((char_u *)_("Too many regions in %s line %d: %s"),
|
|
|
|
fname, lnum, line);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spin->si_region_count = STRLEN(line) / 2;
|
|
|
|
STRCPY(spin->si_region_name, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (*line == '=')
|
|
|
|
{
|
|
|
|
/* keep-case word */
|
|
|
|
flags |= WF_KEEPCAP;
|
|
|
|
++line;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*line == '!')
|
|
|
|
{
|
|
|
|
/* Bad, bad, wicked word. */
|
|
|
|
flags |= WF_BANNED;
|
|
|
|
++line;
|
|
|
|
}
|
|
|
|
else if (*line == '?')
|
|
|
|
{
|
|
|
|
/* Rare word. */
|
|
|
|
flags |= WF_RARE;
|
|
|
|
++line;
|
|
|
|
}
|
|
|
|
|
2005-06-08 21:56:31 +00:00
|
|
|
if (VIM_ISDIGIT(*line))
|
|
|
|
{
|
|
|
|
/* region number(s) */
|
|
|
|
regionmask = 0;
|
|
|
|
while (VIM_ISDIGIT(*line))
|
|
|
|
{
|
|
|
|
l = *line - '0';
|
|
|
|
if (l > spin->si_region_count)
|
|
|
|
{
|
|
|
|
smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
|
|
|
|
fname, lnum, line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
regionmask |= 1 << (l - 1);
|
|
|
|
++line;
|
|
|
|
}
|
|
|
|
flags |= WF_REGION;
|
|
|
|
}
|
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
if (flags == 0)
|
|
|
|
{
|
|
|
|
smsg((char_u *)_("/ line ignored in %s line %d: %s"),
|
2005-06-04 21:55:20 +00:00
|
|
|
fname, lnum, line);
|
2005-06-06 21:50:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
|
|
if (spin->si_ascii && has_non_ascii(line))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
++non_ascii;
|
|
|
|
continue;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Normal word: store it. */
|
2005-06-08 21:56:31 +00:00
|
|
|
if (store_word(line, spin, flags, regionmask) == FAIL)
|
2005-04-17 20:18:43 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
retval = FAIL;
|
|
|
|
break;
|
2005-04-17 20:18:43 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
did_word = TRUE;
|
2005-04-17 20:18:43 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
vim_free(pc);
|
|
|
|
fclose(fd);
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
|
|
|
|
{
|
|
|
|
if (p_verbose > 2)
|
|
|
|
verbose_enter();
|
2005-06-04 21:55:20 +00:00
|
|
|
smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
|
|
non_ascii);
|
2005-06-07 21:00:02 +00:00
|
|
|
if (p_verbose > 2)
|
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
return retval;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Get part of an sblock_T, "len" bytes long.
|
|
|
|
* This avoids calling free() for every little struct we use.
|
|
|
|
* The memory is cleared to all zeros.
|
|
|
|
* Returns NULL when out of memory.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static void *
|
|
|
|
getroom(blp, len)
|
|
|
|
sblock_T **blp;
|
|
|
|
size_t len; /* length needed */
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *p;
|
|
|
|
sblock_T *bl = *blp;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Allocate a block of memory. This is not freed until much later. */
|
|
|
|
bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
|
|
|
|
if (bl == NULL)
|
|
|
|
return NULL;
|
|
|
|
bl->sb_next = *blp;
|
|
|
|
*blp = bl;
|
|
|
|
bl->sb_used = 0;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
p = bl->sb_data + bl->sb_used;
|
|
|
|
bl->sb_used += len;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
return p;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-04-23 20:42:23 +00:00
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Make a copy of a string into memory allocated with getroom().
|
2005-04-23 20:42:23 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static char_u *
|
|
|
|
getroom_save(blp, s)
|
|
|
|
sblock_T **blp;
|
|
|
|
char_u *s;
|
2005-04-23 20:42:23 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *sc;
|
2005-04-23 20:42:23 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
sc = (char_u *)getroom(blp, STRLEN(s) + 1);
|
|
|
|
if (sc != NULL)
|
|
|
|
STRCPY(sc, s);
|
|
|
|
return sc;
|
2005-04-23 20:42:23 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Free the list of allocated sblock_T.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
static void
|
2005-06-04 21:55:20 +00:00
|
|
|
free_blocks(bl)
|
|
|
|
sblock_T *bl;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
sblock_T *next;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
while (bl != NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
next = bl->sb_next;
|
|
|
|
vim_free(bl);
|
|
|
|
bl = next;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Allocate the root of a word tree.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static wordnode_T *
|
|
|
|
wordtree_alloc(blp)
|
|
|
|
sblock_T **blp;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Store a word in the tree(s).
|
2005-06-06 21:50:35 +00:00
|
|
|
* Always store it in the case-folded tree. A keep-case word can also be used
|
|
|
|
* with all caps.
|
2005-06-04 21:55:20 +00:00
|
|
|
* For a keep-case word also store it in the keep-case tree.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-04-23 20:42:23 +00:00
|
|
|
static int
|
2005-06-08 21:56:31 +00:00
|
|
|
store_word(word, spin, flags, region)
|
2005-06-04 21:55:20 +00:00
|
|
|
char_u *word;
|
|
|
|
spellinfo_T *spin;
|
2005-06-06 21:50:35 +00:00
|
|
|
int flags; /* extra flags, WF_BANNED */
|
2005-06-08 21:56:31 +00:00
|
|
|
int region; /* supported region(s) */
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
int len = STRLEN(word);
|
|
|
|
int ct = captype(word, word + len);
|
|
|
|
char_u foldword[MAXWLEN];
|
|
|
|
int res;
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
(void)spell_casefold(word, len, foldword, MAXWLEN);
|
|
|
|
res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
|
|
|
|
region, &spin->si_blocks);
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
|
|
|
|
res = tree_add_word(word, spin->si_keeproot, flags,
|
2005-06-08 21:56:31 +00:00
|
|
|
region, &spin->si_blocks);
|
2005-06-04 21:55:20 +00:00
|
|
|
return res;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Add word "word" to a word tree at "root".
|
2005-04-23 20:42:23 +00:00
|
|
|
* Returns FAIL when out of memory.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-04-23 20:42:23 +00:00
|
|
|
static int
|
2005-06-04 21:55:20 +00:00
|
|
|
tree_add_word(word, root, flags, region, blp)
|
|
|
|
char_u *word;
|
|
|
|
wordnode_T *root;
|
|
|
|
int flags;
|
|
|
|
int region;
|
|
|
|
sblock_T **blp;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
wordnode_T *node = root;
|
|
|
|
wordnode_T *np;
|
|
|
|
wordnode_T **prev = NULL;
|
|
|
|
int i;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Add each byte of the word to the tree, including the NUL at the end. */
|
|
|
|
for (i = 0; ; ++i)
|
2005-04-23 20:42:23 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Look for the sibling that has the same character. They are sorted
|
|
|
|
* on byte value, thus stop searching when a sibling is found with a
|
|
|
|
* higher byte value. For zero bytes (end of word) check that the
|
|
|
|
* flags are equal, there is a separate zero byte for each flag value.
|
|
|
|
*/
|
|
|
|
while (node != NULL && (node->wn_byte < word[i]
|
2005-06-06 21:50:35 +00:00
|
|
|
|| (node->wn_byte == 0 && node->wn_flags != (flags & 0xff))))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
prev = &node->wn_sibling;
|
|
|
|
node = *prev;
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
if (node == NULL || node->wn_byte != word[i])
|
2005-04-23 20:42:23 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Allocate a new node. */
|
|
|
|
np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
|
|
|
if (np == NULL)
|
|
|
|
return FAIL;
|
|
|
|
np->wn_byte = word[i];
|
|
|
|
*prev = np;
|
|
|
|
np->wn_sibling = node;
|
|
|
|
node = np;
|
2005-04-23 20:42:23 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (word[i] == NUL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
node->wn_flags = flags;
|
|
|
|
node->wn_region |= region;
|
|
|
|
break;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
prev = &node->wn_child;
|
|
|
|
node = *prev;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-04-23 20:42:23 +00:00
|
|
|
|
|
|
|
return OK;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Compress a tree: find tails that are identical and can be shared.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static void
|
2005-06-07 21:00:02 +00:00
|
|
|
wordtree_compress(root, spin)
|
2005-06-04 21:55:20 +00:00
|
|
|
wordnode_T *root;
|
2005-06-07 21:00:02 +00:00
|
|
|
spellinfo_T *spin;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
hashtab_T ht;
|
|
|
|
int n;
|
|
|
|
int tot = 0;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (root != NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
hash_init(&ht);
|
|
|
|
n = node_compress(root, &ht, &tot);
|
2005-06-07 21:00:02 +00:00
|
|
|
if (spin->si_verbose || p_verbose > 2)
|
|
|
|
{
|
|
|
|
if (!spin->si_verbose)
|
|
|
|
verbose_enter();
|
|
|
|
smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
|
2005-06-04 21:55:20 +00:00
|
|
|
n, tot, (tot - n) * 100 / tot);
|
2005-06-07 21:00:02 +00:00
|
|
|
if (p_verbose > 2)
|
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
hash_clear(&ht);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Compress a node, its siblings and its children, depth first.
|
|
|
|
* Returns the number of compressed nodes.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static int
|
|
|
|
node_compress(node, ht, tot)
|
|
|
|
wordnode_T *node;
|
|
|
|
hashtab_T *ht;
|
|
|
|
int *tot; /* total count of nodes before compressing,
|
|
|
|
incremented while going through the tree */
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
wordnode_T *np;
|
|
|
|
wordnode_T *tp;
|
|
|
|
wordnode_T *child;
|
|
|
|
hash_T hash;
|
|
|
|
hashitem_T *hi;
|
|
|
|
int len = 0;
|
|
|
|
unsigned nr, n;
|
|
|
|
int compressed = 0;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/*
|
|
|
|
* Go through the list of siblings. Compress each child and then try
|
|
|
|
* finding an identical child to replace it.
|
|
|
|
* Note that with "child" we mean not just the node that is pointed to,
|
|
|
|
* but the whole list of siblings, of which the node is the first.
|
|
|
|
*/
|
|
|
|
for (np = node; np != NULL; np = np->wn_sibling)
|
2005-04-19 21:30:25 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
++len;
|
|
|
|
if ((child = np->wn_child) != NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Compress the child. This fills wn_hashkey. */
|
|
|
|
compressed += node_compress(child, ht, tot);
|
|
|
|
|
|
|
|
/* Try to find an identical child. */
|
|
|
|
hash = hash_hash(child->wn_hashkey);
|
|
|
|
hi = hash_lookup(ht, child->wn_hashkey, hash);
|
|
|
|
tp = NULL;
|
|
|
|
if (!HASHITEM_EMPTY(hi))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* There are children with an identical hash value. Now check
|
|
|
|
* if there is one that is really identical. */
|
|
|
|
for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
|
|
|
|
if (node_equal(child, tp))
|
|
|
|
{
|
|
|
|
/* Found one! Now use that child in place of the
|
|
|
|
* current one. This means the current child is
|
|
|
|
* dropped from the tree. */
|
|
|
|
np->wn_child = tp;
|
|
|
|
++compressed;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tp == NULL)
|
|
|
|
{
|
|
|
|
/* No other child with this hash value equals the child of
|
|
|
|
* the node, add it to the linked list after the first
|
|
|
|
* item. */
|
|
|
|
tp = HI2WN(hi);
|
|
|
|
child->wn_next = tp->wn_next;
|
|
|
|
tp->wn_next = child;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
else
|
|
|
|
/* No other child has this hash value, add it to the
|
|
|
|
* hashtable. */
|
|
|
|
hash_add_item(ht, hi, child->wn_hashkey, hash);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-04-19 21:30:25 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
*tot += len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a hash key for the node and its siblings, so that we can quickly
|
|
|
|
* find a lookalike node. This must be done after compressing the sibling
|
|
|
|
* list, otherwise the hash key would become invalid by the compression.
|
|
|
|
*/
|
|
|
|
node->wn_hashkey[0] = len;
|
|
|
|
nr = 0;
|
|
|
|
for (np = node; np != NULL; np = np->wn_sibling)
|
|
|
|
{
|
|
|
|
if (np->wn_byte == NUL)
|
|
|
|
/* end node: only use wn_flags and wn_region */
|
|
|
|
n = np->wn_flags + (np->wn_region << 8);
|
|
|
|
else
|
|
|
|
/* byte node: use the byte value and the child pointer */
|
|
|
|
n = np->wn_byte + ((long_u)np->wn_child << 8);
|
|
|
|
nr = nr * 101 + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Avoid NUL bytes, it terminates the hash key. */
|
|
|
|
n = nr & 0xff;
|
|
|
|
node->wn_hashkey[1] = n == 0 ? 1 : n;
|
|
|
|
n = (nr >> 8) & 0xff;
|
|
|
|
node->wn_hashkey[2] = n == 0 ? 1 : n;
|
|
|
|
n = (nr >> 16) & 0xff;
|
|
|
|
node->wn_hashkey[3] = n == 0 ? 1 : n;
|
|
|
|
n = (nr >> 24) & 0xff;
|
|
|
|
node->wn_hashkey[4] = n == 0 ? 1 : n;
|
|
|
|
node->wn_hashkey[5] = NUL;
|
|
|
|
|
|
|
|
return compressed;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Return TRUE when two nodes have identical siblings and children.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static int
|
|
|
|
node_equal(n1, n2)
|
|
|
|
wordnode_T *n1;
|
|
|
|
wordnode_T *n2;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
wordnode_T *p1;
|
|
|
|
wordnode_T *p2;
|
|
|
|
|
|
|
|
for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
|
|
|
|
p1 = p1->wn_sibling, p2 = p2->wn_sibling)
|
|
|
|
if (p1->wn_byte != p2->wn_byte
|
|
|
|
|| (p1->wn_byte == NUL
|
|
|
|
? (p1->wn_flags != p2->wn_flags
|
|
|
|
|| p1->wn_region != p2->wn_region)
|
|
|
|
: (p1->wn_child != p2->wn_child)))
|
|
|
|
break;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
return p1 == NULL && p2 == NULL;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Write a number to file "fd", MSB first, in "len" bytes.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
void
|
|
|
|
put_bytes(fd, nr, len)
|
|
|
|
FILE *fd;
|
|
|
|
long_u nr;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = len - 1; i >= 0; --i)
|
|
|
|
putc((int)(nr >> (i * 8)), fd);
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
static int
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
_RTLENTRYF
|
|
|
|
#endif
|
|
|
|
rep_compare __ARGS((const void *s1, const void *s2));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function given to qsort() to sort the REP items on "from" string.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
_RTLENTRYF
|
|
|
|
#endif
|
|
|
|
rep_compare(s1, s2)
|
|
|
|
const void *s1;
|
|
|
|
const void *s2;
|
|
|
|
{
|
|
|
|
fromto_T *p1 = (fromto_T *)s1;
|
|
|
|
fromto_T *p2 = (fromto_T *)s2;
|
|
|
|
|
|
|
|
return STRCMP(p1->ft_from, p2->ft_from);
|
|
|
|
}
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
|
|
|
* Write the Vim spell file "fname".
|
|
|
|
*/
|
|
|
|
static void
|
2005-06-08 21:56:31 +00:00
|
|
|
write_vim_spell(fname, spin)
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u *fname;
|
2005-06-04 21:55:20 +00:00
|
|
|
spellinfo_T *spin;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
FILE *fd;
|
|
|
|
int regionmask;
|
2005-04-15 21:00:38 +00:00
|
|
|
int round;
|
2005-06-04 21:55:20 +00:00
|
|
|
wordnode_T *tree;
|
|
|
|
int nodecount;
|
2005-06-13 22:28:56 +00:00
|
|
|
int i;
|
|
|
|
int l;
|
|
|
|
garray_T *gap;
|
|
|
|
fromto_T *ftp;
|
|
|
|
char_u *p;
|
|
|
|
int rr;
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
fd = mch_fopen((char *)fname, "w");
|
2005-06-04 21:55:20 +00:00
|
|
|
if (fd == NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
EMSG2(_(e_notopen), fname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-04-23 20:42:23 +00:00
|
|
|
/* <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
|
|
* <charflagslen> <charflags> <fcharslen> <fchars> */
|
2005-06-04 21:55:20 +00:00
|
|
|
|
|
|
|
/* <fileID> */
|
|
|
|
if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
|
|
|
|
EMSG(_(e_write));
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/* write the region names if there is more than one */
|
2005-06-08 21:56:31 +00:00
|
|
|
if (spin->si_region_count > 1)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-08 21:56:31 +00:00
|
|
|
putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
|
|
|
|
fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
|
|
|
|
(size_t)1, fd);
|
|
|
|
regionmask = (1 << spin->si_region_count) - 1;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
putc(0, fd);
|
|
|
|
regionmask = 0;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Write the table with character flags and table for case folding.
|
2005-04-24 21:58:05 +00:00
|
|
|
* <charflagslen> <charflags> <fcharlen> <fchars>
|
|
|
|
* Skip this for ASCII, the table may conflict with the one used for
|
2005-06-13 22:28:56 +00:00
|
|
|
* 'encoding'.
|
|
|
|
* Also skip this for an .add.spl file, the main spell file must contain
|
|
|
|
* the table (avoids that it conflicts). File is shorter too.
|
|
|
|
*/
|
|
|
|
if (spin->si_ascii || spin->si_add)
|
2005-04-24 21:58:05 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
putc(0, fd);
|
|
|
|
putc(0, fd);
|
|
|
|
putc(0, fd);
|
2005-04-24 21:58:05 +00:00
|
|
|
}
|
|
|
|
else
|
2005-06-04 21:55:20 +00:00
|
|
|
write_spell_chartab(fd);
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/* Sort the REP items. */
|
|
|
|
qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
|
|
|
|
sizeof(fromto_T), rep_compare);
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/* <SUGGEST> : <repcount> <rep> ...
|
|
|
|
* <salflags> <salcount> <sal> ...
|
|
|
|
* <maplen> <mapstr> */
|
|
|
|
for (round = 1; round <= 2; ++round)
|
|
|
|
{
|
|
|
|
if (round == 1)
|
|
|
|
gap = &spin->si_rep;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gap = &spin->si_sal;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
if (spin->si_followup)
|
|
|
|
i |= SAL_F0LLOWUP;
|
|
|
|
if (spin->si_collapse)
|
|
|
|
i |= SAL_COLLAPSE;
|
|
|
|
if (spin->si_rem_accents)
|
|
|
|
i |= SAL_REM_ACCENTS;
|
|
|
|
putc(i, fd); /* <salflags> */
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
|
|
|
|
for (i = 0; i < gap->ga_len; ++i)
|
|
|
|
{
|
|
|
|
/* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
|
|
|
|
/* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
|
|
|
|
ftp = &((fromto_T *)gap->ga_data)[i];
|
|
|
|
for (rr = 1; rr <= 2; ++rr)
|
|
|
|
{
|
|
|
|
p = rr == 1 ? ftp->ft_from : ftp->ft_to;
|
|
|
|
l = STRLEN(p);
|
|
|
|
putc(l, fd);
|
|
|
|
fwrite(p, l, (size_t)1, fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
|
|
|
|
if (spin->si_map.ga_len > 0) /* <mapstr> */
|
|
|
|
fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
|
|
|
|
(size_t)1, fd);
|
2005-06-05 21:54:54 +00:00
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* <LWORDTREE> <KWORDTREE>
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-13 22:28:56 +00:00
|
|
|
spin->si_memtot = 0;
|
2005-06-04 21:55:20 +00:00
|
|
|
for (round = 1; round <= 2; ++round)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot;
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Count the number of nodes. Needed to be able to allocate the
|
|
|
|
* memory when reading the nodes. Also fills in the index for shared
|
|
|
|
* nodes. */
|
|
|
|
nodecount = put_tree(NULL, tree, 0, regionmask);
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* number of nodes in 4 bytes */
|
|
|
|
put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
|
2005-06-05 21:54:54 +00:00
|
|
|
spin->si_memtot += nodecount + nodecount * sizeof(int);
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Write the nodes. */
|
|
|
|
(void)put_tree(fd, tree, 0, regionmask);
|
2005-04-20 19:37:22 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
fclose(fd);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Dump a word tree at node "node".
|
|
|
|
*
|
|
|
|
* This first writes the list of possible bytes (siblings). Then for each
|
|
|
|
* byte recursively write the children.
|
2005-04-15 21:00:38 +00:00
|
|
|
*
|
2005-06-04 21:55:20 +00:00
|
|
|
* NOTE: The code here must match the code in read_tree(), since assumptions
|
|
|
|
* are made about the indexes (so that we don't have to write them in the
|
|
|
|
* file).
|
|
|
|
*
|
|
|
|
* Returns the number of nodes used.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
static int
|
|
|
|
put_tree(fd, node, index, regionmask)
|
|
|
|
FILE *fd; /* NULL when only counting */
|
|
|
|
wordnode_T *node;
|
|
|
|
int index;
|
|
|
|
int regionmask;
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
int newindex = index;
|
|
|
|
int siblingcount = 0;
|
|
|
|
wordnode_T *np;
|
2005-04-15 21:00:38 +00:00
|
|
|
int flags;
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* If "node" is zero the tree is empty. */
|
|
|
|
if (node == NULL)
|
|
|
|
return 0;
|
2005-03-21 08:23:33 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Store the index where this node is written. */
|
|
|
|
node->wn_index = index;
|
2005-03-21 08:23:33 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Count the number of siblings. */
|
|
|
|
for (np = node; np != NULL; np = np->wn_sibling)
|
|
|
|
++siblingcount;
|
2005-03-21 08:23:33 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Write the sibling count. */
|
|
|
|
if (fd != NULL)
|
|
|
|
putc(siblingcount, fd); /* <siblingcount> */
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Write each sibling byte and optionally extra info. */
|
|
|
|
for (np = node; np != NULL; np = np->wn_sibling)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
if (np->wn_byte == 0)
|
2005-04-20 19:37:22 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
if (fd != NULL)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* For a NUL byte (end of word) instead of the byte itself
|
|
|
|
* we write the flag/region items. */
|
|
|
|
flags = np->wn_flags;
|
|
|
|
if (regionmask != 0 && np->wn_region != regionmask)
|
|
|
|
flags |= WF_REGION;
|
|
|
|
if (flags == 0)
|
|
|
|
{
|
|
|
|
/* word without flags or region */
|
|
|
|
putc(BY_NOFLAGS, fd); /* <byte> */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
putc(BY_FLAGS, fd); /* <byte> */
|
|
|
|
putc(flags, fd); /* <flags> */
|
|
|
|
if (flags & WF_REGION)
|
|
|
|
putc(np->wn_region, fd); /* <regionmask> */
|
|
|
|
}
|
2005-04-20 19:37:22 +00:00
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
|
2005-04-20 19:37:22 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
/* The child is written elsewhere, write the reference. */
|
|
|
|
if (fd != NULL)
|
2005-04-20 19:37:22 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
putc(BY_INDEX, fd); /* <byte> */
|
|
|
|
/* <nodeidx> */
|
|
|
|
put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
|
2005-04-20 19:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-04 21:55:20 +00:00
|
|
|
else if (np->wn_child->wn_wnode == NULL)
|
|
|
|
/* We will write the child below and give it an index. */
|
|
|
|
np->wn_child->wn_wnode = node;
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (fd != NULL)
|
|
|
|
if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
|
|
|
|
{
|
|
|
|
EMSG(_(e_write));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Space used in the array when reading: one for each sibling and one for
|
|
|
|
* the count. */
|
|
|
|
newindex += siblingcount + 1;
|
2005-04-20 19:37:22 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Recursively dump the children of each sibling. */
|
|
|
|
for (np = node; np != NULL; np = np->wn_sibling)
|
|
|
|
if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
|
|
|
|
newindex = put_tree(fd, np->wn_child, newindex, regionmask);
|
2005-04-23 20:42:23 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
return newindex;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-06-07 21:00:02 +00:00
|
|
|
* ":mkspell [-ascii] outfile infile ..."
|
|
|
|
* ":mkspell [-ascii] addfile"
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
ex_mkspell(eap)
|
|
|
|
exarg_T *eap;
|
|
|
|
{
|
|
|
|
int fcount;
|
|
|
|
char_u **fnames;
|
2005-06-07 21:00:02 +00:00
|
|
|
char_u *arg = eap->arg;
|
|
|
|
int ascii = FALSE;
|
|
|
|
|
|
|
|
if (STRNCMP(arg, "-ascii", 6) == 0)
|
|
|
|
{
|
|
|
|
ascii = TRUE;
|
|
|
|
arg = skipwhite(arg + 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
|
|
|
|
if (get_arglist_exp(arg, &fcount, &fnames) == OK)
|
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
|
2005-06-07 21:00:02 +00:00
|
|
|
FreeWild(fcount, fnames);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a Vim spell file from one or more word lists.
|
|
|
|
* "fnames[0]" is the output file name.
|
|
|
|
* "fnames[fcount - 1]" is the last input file name.
|
|
|
|
* Exception: when "fnames[0]" ends in ".add" it's used as the input file name
|
|
|
|
* and ".spl" is appended to make the output file name.
|
|
|
|
*/
|
|
|
|
static void
|
2005-06-13 22:28:56 +00:00
|
|
|
mkspell(fcount, fnames, ascii, overwrite, added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
int fcount;
|
|
|
|
char_u **fnames;
|
|
|
|
int ascii; /* -ascii argument given */
|
|
|
|
int overwrite; /* overwrite existing output file */
|
2005-06-13 22:28:56 +00:00
|
|
|
int added_word; /* invoked through "zg" */
|
2005-06-07 21:00:02 +00:00
|
|
|
{
|
2005-04-15 21:00:38 +00:00
|
|
|
char_u fname[MAXPATHL];
|
|
|
|
char_u wfname[MAXPATHL];
|
2005-06-07 21:00:02 +00:00
|
|
|
char_u **innames;
|
|
|
|
int incount;
|
2005-04-15 21:00:38 +00:00
|
|
|
afffile_T *(afile[8]);
|
|
|
|
int i;
|
|
|
|
int len;
|
|
|
|
struct stat st;
|
2005-04-23 20:42:23 +00:00
|
|
|
int error = FALSE;
|
2005-06-04 21:55:20 +00:00
|
|
|
spellinfo_T spin;
|
|
|
|
|
|
|
|
vim_memset(&spin, 0, sizeof(spin));
|
2005-06-13 22:28:56 +00:00
|
|
|
spin.si_verbose = !added_word;
|
2005-06-07 21:00:02 +00:00
|
|
|
spin.si_ascii = ascii;
|
2005-06-13 22:28:56 +00:00
|
|
|
spin.si_followup = TRUE;
|
|
|
|
spin.si_rem_accents = TRUE;
|
|
|
|
ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
|
|
|
|
ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
|
|
|
|
ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
|
2005-04-17 20:18:43 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
/* default: fnames[0] is output file, following are input files */
|
|
|
|
innames = &fnames[1];
|
|
|
|
incount = fcount - 1;
|
|
|
|
|
|
|
|
if (fcount >= 1)
|
2005-04-17 20:18:43 +00:00
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
len = STRLEN(fnames[0]);
|
|
|
|
if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
|
|
|
|
{
|
|
|
|
/* For ":mkspell path/en.latin1.add" output file is
|
|
|
|
* "path/en.latin1.add.spl". */
|
|
|
|
innames = &fnames[0];
|
|
|
|
incount = 1;
|
|
|
|
vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
|
|
|
|
}
|
|
|
|
else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
|
|
|
|
{
|
|
|
|
/* Name ends in ".spl", use as the file name. */
|
2005-06-13 22:28:56 +00:00
|
|
|
vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
|
2005-06-07 21:00:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Name should be language, make the file name from it. */
|
|
|
|
vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
|
|
|
|
spin.si_ascii ? (char_u *)"ascii" : spell_enc());
|
|
|
|
|
|
|
|
/* Check for .ascii.spl. */
|
|
|
|
if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
|
|
|
|
spin.si_ascii = TRUE;
|
|
|
|
|
|
|
|
/* Check for .add.spl. */
|
|
|
|
if (strstr((char *)gettail(wfname), ".add.") != NULL)
|
|
|
|
spin.si_add = TRUE;
|
2005-04-17 20:18:43 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (incount <= 0)
|
2005-04-15 21:00:38 +00:00
|
|
|
EMSG(_(e_invarg)); /* need at least output and input names */
|
2005-06-07 21:00:02 +00:00
|
|
|
else if (incount > 8)
|
2005-04-15 21:00:38 +00:00
|
|
|
EMSG(_("E754: Only up to 8 regions supported"));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check for overwriting before doing things that may take a lot of
|
|
|
|
* time. */
|
2005-06-07 21:00:02 +00:00
|
|
|
if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
EMSG(_(e_exists));
|
2005-06-07 21:00:02 +00:00
|
|
|
return;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-07 21:00:02 +00:00
|
|
|
if (mch_isdir(wfname))
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
EMSG2(_(e_isadir2), wfname);
|
|
|
|
return;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init the aff and dic pointers.
|
|
|
|
* Get the region names if there are more than 2 arguments.
|
|
|
|
*/
|
2005-06-07 21:00:02 +00:00
|
|
|
for (i = 0; i < incount; ++i)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
afile[i] = NULL;
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-06-08 21:56:31 +00:00
|
|
|
if (incount > 1)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
len = STRLEN(innames[i]);
|
|
|
|
if (STRLEN(gettail(innames[i])) < 5
|
|
|
|
|| innames[i][len - 3] != '_')
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-07 21:00:02 +00:00
|
|
|
EMSG2(_("E755: Invalid region in %s"), innames[i]);
|
|
|
|
return;
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-08 21:56:31 +00:00
|
|
|
spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
|
|
|
|
spin.si_region_name[i * 2 + 1] =
|
|
|
|
TOLOWER_ASC(innames[i][len - 1]);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-08 21:56:31 +00:00
|
|
|
spin.si_region_count = incount;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
if (!spin.si_add)
|
|
|
|
/* Clear the char type tables, don't want to use any of the
|
|
|
|
* currently used spell properties. */
|
|
|
|
init_spell_chartab();
|
2005-04-23 20:42:23 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
|
|
|
|
spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
|
|
|
|
if (spin.si_foldroot == NULL || spin.si_keeproot == NULL)
|
|
|
|
{
|
|
|
|
error = TRUE;
|
2005-06-07 21:00:02 +00:00
|
|
|
return;
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
|
|
|
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
|
|
|
* Read all the .aff and .dic files.
|
|
|
|
* Text is converted to 'encoding'.
|
2005-06-04 21:55:20 +00:00
|
|
|
* Words are stored in the case-folded and keep-case trees.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-07 21:00:02 +00:00
|
|
|
for (i = 0; i < incount && !error; ++i)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
2005-06-04 21:55:20 +00:00
|
|
|
spin.si_conv.vc_type = CONV_NONE;
|
2005-06-07 21:00:02 +00:00
|
|
|
spin.si_region = 1 << i;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
|
2005-06-04 21:55:20 +00:00
|
|
|
if (mch_stat((char *)fname, &st) >= 0)
|
|
|
|
{
|
|
|
|
/* Read the .aff file. Will init "spin->si_conv" based on the
|
|
|
|
* "SET" line. */
|
2005-06-07 21:00:02 +00:00
|
|
|
afile[i] = spell_read_aff(fname, &spin);
|
|
|
|
if (afile[i] == NULL)
|
2005-06-04 21:55:20 +00:00
|
|
|
error = TRUE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Read the .dic file and store the words in the trees. */
|
|
|
|
vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
|
2005-06-07 21:00:02 +00:00
|
|
|
innames[i]);
|
|
|
|
if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
|
2005-06-04 21:55:20 +00:00
|
|
|
error = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No .aff file, try reading the file as a word list. Store
|
|
|
|
* the words in the trees. */
|
2005-06-07 21:00:02 +00:00
|
|
|
if (spell_read_wordfile(innames[i], &spin) == FAIL)
|
2005-06-04 21:55:20 +00:00
|
|
|
error = TRUE;
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
2005-04-15 21:00:38 +00:00
|
|
|
/* Free any conversion stuff. */
|
2005-06-04 21:55:20 +00:00
|
|
|
convert_setup(&spin.si_conv, NULL, NULL);
|
2005-06-07 21:00:02 +00:00
|
|
|
#endif
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (!error)
|
2005-04-15 21:00:38 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Remove the dummy NUL from the start of the tree root.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-04 21:55:20 +00:00
|
|
|
spin.si_foldroot = spin.si_foldroot->wn_sibling;
|
|
|
|
spin.si_keeproot = spin.si_keeproot->wn_sibling;
|
2005-04-15 21:00:38 +00:00
|
|
|
|
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Combine tails in the tree.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-13 22:28:56 +00:00
|
|
|
if (!added_word || p_verbose > 2)
|
2005-06-07 21:00:02 +00:00
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
if (added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
verbose_enter();
|
|
|
|
MSG(_("Compressing word tree..."));
|
|
|
|
out_flush();
|
2005-06-13 22:28:56 +00:00
|
|
|
if (added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
verbose_leave();
|
|
|
|
}
|
|
|
|
wordtree_compress(spin.si_foldroot, &spin);
|
|
|
|
wordtree_compress(spin.si_keeproot, &spin);
|
2005-06-04 21:55:20 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
if (!error)
|
|
|
|
{
|
2005-04-15 21:00:38 +00:00
|
|
|
/*
|
2005-06-04 21:55:20 +00:00
|
|
|
* Write the info in the spell file.
|
2005-04-15 21:00:38 +00:00
|
|
|
*/
|
2005-06-13 22:28:56 +00:00
|
|
|
if (!added_word || p_verbose > 2)
|
2005-06-07 21:00:02 +00:00
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
if (added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
verbose_enter();
|
|
|
|
smsg((char_u *)_("Writing spell file %s..."), wfname);
|
|
|
|
out_flush();
|
2005-06-13 22:28:56 +00:00
|
|
|
if (added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
verbose_leave();
|
|
|
|
}
|
|
|
|
|
2005-06-08 21:56:31 +00:00
|
|
|
write_vim_spell(wfname, &spin);
|
2005-06-05 21:54:54 +00:00
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
if (!added_word || p_verbose > 2)
|
2005-06-07 21:00:02 +00:00
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
if (added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
verbose_enter();
|
|
|
|
MSG(_("Done!"));
|
|
|
|
smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
|
2005-06-05 21:54:54 +00:00
|
|
|
spin.si_memtot);
|
2005-06-07 21:00:02 +00:00
|
|
|
out_flush();
|
2005-06-13 22:28:56 +00:00
|
|
|
if (added_word)
|
2005-06-07 21:00:02 +00:00
|
|
|
verbose_leave();
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
/* If the file is loaded need to reload it. */
|
2005-06-13 22:28:56 +00:00
|
|
|
spell_reload_one(wfname, added_word);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
/* Free the allocated memory. */
|
|
|
|
free_blocks(spin.si_blocks);
|
2005-06-13 22:28:56 +00:00
|
|
|
ga_clear(&spin.si_rep);
|
|
|
|
ga_clear(&spin.si_sal);
|
|
|
|
ga_clear(&spin.si_map);
|
2005-06-04 21:55:20 +00:00
|
|
|
|
|
|
|
/* Free the .aff file structures. */
|
2005-06-07 21:00:02 +00:00
|
|
|
for (i = 0; i < incount; ++i)
|
|
|
|
if (afile[i] != NULL)
|
|
|
|
spell_free_aff(afile[i]);
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
2005-06-07 21:00:02 +00:00
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ":spellgood {word}"
|
|
|
|
* ":spellwrong {word}"
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ex_spell(eap)
|
|
|
|
exarg_T *eap;
|
|
|
|
{
|
|
|
|
spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add "word[len]" to 'spellfile' as a good or bad word.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
spell_add_word(word, len, bad)
|
|
|
|
char_u *word;
|
|
|
|
int len;
|
|
|
|
int bad;
|
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
buf_T *buf;
|
|
|
|
|
|
|
|
if (*curbuf->b_p_spf == NUL)
|
|
|
|
init_spellfile();
|
|
|
|
if (*curbuf->b_p_spf == NUL)
|
2005-06-13 22:28:56 +00:00
|
|
|
EMSG(_("E764: 'spellfile' is not set"));
|
2005-06-07 21:00:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check that the user isn't editing the .add file somewhere. */
|
|
|
|
buf = buflist_findname_exp(curbuf->b_p_spf);
|
|
|
|
if (buf != NULL && buf->b_ml.ml_mfp == NULL)
|
|
|
|
buf = NULL;
|
|
|
|
if (buf != NULL && bufIsChanged(buf))
|
|
|
|
EMSG(_(e_bufloaded));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fd = mch_fopen((char *)curbuf->b_p_spf, "a");
|
|
|
|
if (fd == NULL)
|
|
|
|
EMSG2(_(e_notopen), curbuf->b_p_spf);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (bad)
|
|
|
|
fprintf(fd, "/!%.*s\n", len, word);
|
|
|
|
else
|
|
|
|
fprintf(fd, "%.*s\n", len, word);
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
/* Update the .add.spl file. */
|
2005-06-13 22:28:56 +00:00
|
|
|
mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
|
2005-06-07 21:00:02 +00:00
|
|
|
|
|
|
|
/* If the .add file is edited somewhere, reload it. */
|
|
|
|
if (buf != NULL)
|
|
|
|
buf_reload(buf);
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
redraw_all_later(NOT_VALID);
|
2005-06-07 21:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-04-15 21:00:38 +00:00
|
|
|
}
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
/*
|
|
|
|
* Initialize 'spellfile' for the current buffer.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_spellfile()
|
|
|
|
{
|
|
|
|
char_u buf[MAXPATHL];
|
|
|
|
int l;
|
|
|
|
slang_T *sl;
|
|
|
|
char_u *rtp;
|
|
|
|
|
|
|
|
if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
|
|
|
|
{
|
|
|
|
/* Loop over all entries in 'runtimepath'. */
|
|
|
|
rtp = p_rtp;
|
|
|
|
while (*rtp != NUL)
|
|
|
|
{
|
|
|
|
/* Copy the path from 'runtimepath' to buf[]. */
|
|
|
|
copy_option_part(&rtp, buf, MAXPATHL, ",");
|
|
|
|
if (filewritable(buf) == 2)
|
|
|
|
{
|
2005-06-08 21:56:31 +00:00
|
|
|
/* Use the first language name from 'spelllang' and the
|
|
|
|
* encoding used in the first loaded .spl file. */
|
2005-06-07 21:00:02 +00:00
|
|
|
sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
|
|
|
|
l = STRLEN(buf);
|
|
|
|
vim_snprintf((char *)buf + l, MAXPATHL - l,
|
2005-06-08 21:56:31 +00:00
|
|
|
"/spell/%.*s.%s.add",
|
|
|
|
2, curbuf->b_p_spl,
|
2005-06-07 21:00:02 +00:00
|
|
|
strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
|
|
|
|
? (char_u *)"ascii" : spell_enc());
|
|
|
|
set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-22 22:54:12 +00:00
|
|
|
|
2005-06-04 21:55:20 +00:00
|
|
|
|
2005-06-06 21:50:35 +00:00
|
|
|
/*
|
|
|
|
* Init the chartab used for spelling for ASCII.
|
|
|
|
* EBCDIC is not supported!
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
clear_spell_chartab(sp)
|
|
|
|
spelltab_T *sp;
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
int i;
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
/* Init everything to FALSE. */
|
|
|
|
vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
|
|
|
|
vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
|
|
|
|
for (i = 0; i < 256; ++i)
|
2005-06-14 22:01:04 +00:00
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
sp->st_fold[i] = i;
|
2005-06-14 22:01:04 +00:00
|
|
|
sp->st_upper[i] = i;
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
/* We include digits. A word shouldn't start with a digit, but handling
|
|
|
|
* that is done separately. */
|
|
|
|
for (i = '0'; i <= '9'; ++i)
|
|
|
|
sp->st_isw[i] = TRUE;
|
|
|
|
for (i = 'A'; i <= 'Z'; ++i)
|
|
|
|
{
|
|
|
|
sp->st_isw[i] = TRUE;
|
|
|
|
sp->st_isu[i] = TRUE;
|
|
|
|
sp->st_fold[i] = i + 0x20;
|
|
|
|
}
|
|
|
|
for (i = 'a'; i <= 'z'; ++i)
|
2005-06-14 22:01:04 +00:00
|
|
|
{
|
2005-06-06 21:50:35 +00:00
|
|
|
sp->st_isw[i] = TRUE;
|
2005-06-14 22:01:04 +00:00
|
|
|
sp->st_upper[i] = i - 0x20;
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init the chartab used for spelling. Only depends on 'encoding'.
|
|
|
|
* Called once while starting up and when 'encoding' changes.
|
|
|
|
* The default is to use isalpha(), but the spell file should define the word
|
|
|
|
* characters to make it possible that 'encoding' differs from the current
|
|
|
|
* locale.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_spell_chartab()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
did_set_spelltab = FALSE;
|
|
|
|
clear_spell_chartab(&spelltab);
|
|
|
|
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (enc_dbcs)
|
|
|
|
{
|
|
|
|
/* DBCS: assume double-wide characters are word characters. */
|
|
|
|
for (i = 128; i <= 255; ++i)
|
|
|
|
if (MB_BYTE2LEN(i) == 2)
|
|
|
|
spelltab.st_isw[i] = TRUE;
|
|
|
|
}
|
2005-06-14 22:01:04 +00:00
|
|
|
else if (enc_utf8)
|
|
|
|
{
|
|
|
|
for (i = 128; i < 256; ++i)
|
|
|
|
{
|
|
|
|
spelltab.st_isu[i] = utf_isupper(i);
|
|
|
|
spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
|
|
|
|
spelltab.st_fold[i] = utf_fold(i);
|
|
|
|
spelltab.st_upper[i] = utf_toupper(i);
|
|
|
|
}
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Rough guess: use locale-dependent library functions. */
|
2005-06-06 21:50:35 +00:00
|
|
|
for (i = 128; i < 256; ++i)
|
|
|
|
{
|
|
|
|
if (MB_ISUPPER(i))
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
spelltab.st_isw[i] = TRUE;
|
2005-06-06 21:50:35 +00:00
|
|
|
spelltab.st_isu[i] = TRUE;
|
|
|
|
spelltab.st_fold[i] = MB_TOLOWER(i);
|
|
|
|
}
|
2005-06-14 22:01:04 +00:00
|
|
|
else if (MB_ISLOWER(i))
|
|
|
|
{
|
|
|
|
spelltab.st_isw[i] = TRUE;
|
|
|
|
spelltab.st_upper[i] = MB_TOUPPER(i);
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
|
|
|
|
static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the spell character tables from strings in the affix file.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
set_spell_chartab(fol, low, upp)
|
|
|
|
char_u *fol;
|
|
|
|
char_u *low;
|
|
|
|
char_u *upp;
|
|
|
|
{
|
|
|
|
/* We build the new tables here first, so that we can compare with the
|
|
|
|
* previous one. */
|
|
|
|
spelltab_T new_st;
|
|
|
|
char_u *pf = fol, *pl = low, *pu = upp;
|
|
|
|
int f, l, u;
|
|
|
|
|
|
|
|
clear_spell_chartab(&new_st);
|
|
|
|
|
|
|
|
while (*pf != NUL)
|
|
|
|
{
|
|
|
|
if (*pl == NUL || *pu == NUL)
|
|
|
|
{
|
|
|
|
EMSG(_(e_affform));
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
f = mb_ptr2char_adv(&pf);
|
|
|
|
l = mb_ptr2char_adv(&pl);
|
|
|
|
u = mb_ptr2char_adv(&pu);
|
|
|
|
#else
|
|
|
|
f = *pf++;
|
|
|
|
l = *pl++;
|
|
|
|
u = *pu++;
|
|
|
|
#endif
|
|
|
|
/* Every character that appears is a word character. */
|
|
|
|
if (f < 256)
|
|
|
|
new_st.st_isw[f] = TRUE;
|
|
|
|
if (l < 256)
|
|
|
|
new_st.st_isw[l] = TRUE;
|
|
|
|
if (u < 256)
|
|
|
|
new_st.st_isw[u] = TRUE;
|
|
|
|
|
|
|
|
/* if "LOW" and "FOL" are not the same the "LOW" char needs
|
|
|
|
* case-folding */
|
|
|
|
if (l < 256 && l != f)
|
|
|
|
{
|
|
|
|
if (f >= 256)
|
|
|
|
{
|
|
|
|
EMSG(_(e_affrange));
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
new_st.st_fold[l] = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if "UPP" and "FOL" are not the same the "UPP" char needs
|
2005-06-14 22:01:04 +00:00
|
|
|
* case-folding, it's upper case and the "UPP" is the upper case of
|
|
|
|
* "FOL" . */
|
2005-06-06 21:50:35 +00:00
|
|
|
if (u < 256 && u != f)
|
|
|
|
{
|
|
|
|
if (f >= 256)
|
|
|
|
{
|
|
|
|
EMSG(_(e_affrange));
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
new_st.st_fold[u] = f;
|
|
|
|
new_st.st_isu[u] = TRUE;
|
2005-06-14 22:01:04 +00:00
|
|
|
new_st.st_upper[f] = u;
|
2005-06-06 21:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pl != NUL || *pu != NUL)
|
|
|
|
{
|
|
|
|
EMSG(_(e_affform));
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return set_spell_finish(&new_st);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the spell character tables from strings in the .spl file.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
set_spell_charflags(flags, cnt, upp)
|
|
|
|
char_u *flags;
|
|
|
|
int cnt;
|
|
|
|
char_u *upp;
|
|
|
|
{
|
|
|
|
/* We build the new tables here first, so that we can compare with the
|
|
|
|
* previous one. */
|
|
|
|
spelltab_T new_st;
|
|
|
|
int i;
|
|
|
|
char_u *p = upp;
|
2005-06-14 22:01:04 +00:00
|
|
|
int c;
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
clear_spell_chartab(&new_st);
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; ++i)
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
|
|
|
|
new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
if (*p == NUL)
|
|
|
|
return FAIL;
|
|
|
|
#ifdef FEAT_MBYTE
|
2005-06-14 22:01:04 +00:00
|
|
|
c = mb_ptr2char_adv(&p);
|
2005-06-06 21:50:35 +00:00
|
|
|
#else
|
2005-06-14 22:01:04 +00:00
|
|
|
c = *p++;
|
2005-06-06 21:50:35 +00:00
|
|
|
#endif
|
2005-06-14 22:01:04 +00:00
|
|
|
new_st.st_fold[i + 128] = c;
|
|
|
|
if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
|
|
|
|
new_st.st_upper[c] = i + 128;
|
2005-06-06 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return set_spell_finish(&new_st);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_spell_finish(new_st)
|
|
|
|
spelltab_T *new_st;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (did_set_spelltab)
|
|
|
|
{
|
|
|
|
/* check that it's the same table */
|
|
|
|
for (i = 0; i < 256; ++i)
|
|
|
|
{
|
|
|
|
if (spelltab.st_isw[i] != new_st->st_isw[i]
|
|
|
|
|| spelltab.st_isu[i] != new_st->st_isu[i]
|
2005-06-14 22:01:04 +00:00
|
|
|
|| spelltab.st_fold[i] != new_st->st_fold[i]
|
|
|
|
|| spelltab.st_upper[i] != new_st->st_upper[i])
|
2005-06-06 21:50:35 +00:00
|
|
|
{
|
|
|
|
EMSG(_("E763: Word characters differ between spell files"));
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* copy the new spelltab into the one being used */
|
|
|
|
spelltab = *new_st;
|
|
|
|
did_set_spelltab = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the current tables into the .spl file.
|
|
|
|
* This makes sure the same characters are recognized as word characters when
|
|
|
|
* generating an when using a spell file.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
write_spell_chartab(fd)
|
|
|
|
FILE *fd;
|
|
|
|
{
|
|
|
|
char_u charbuf[256 * 4];
|
|
|
|
int len = 0;
|
|
|
|
int flags;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fputc(128, fd); /* <charflagslen> */
|
|
|
|
for (i = 128; i < 256; ++i)
|
|
|
|
{
|
|
|
|
flags = 0;
|
|
|
|
if (spelltab.st_isw[i])
|
2005-06-14 22:01:04 +00:00
|
|
|
flags |= CF_WORD;
|
2005-06-06 21:50:35 +00:00
|
|
|
if (spelltab.st_isu[i])
|
2005-06-14 22:01:04 +00:00
|
|
|
flags |= CF_UPPER;
|
2005-06-06 21:50:35 +00:00
|
|
|
fputc(flags, fd); /* <charflags> */
|
|
|
|
|
2005-06-07 21:00:02 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
charbuf[len++] = spelltab.st_fold[i];
|
2005-06-06 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
|
|
|
|
fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-14 22:01:04 +00:00
|
|
|
* Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
|
|
|
|
* Uses the character definitions from the .spl file.
|
2005-06-06 21:50:35 +00:00
|
|
|
* When using a multi-byte 'encoding' the length may change!
|
|
|
|
* Returns FAIL when something wrong.
|
|
|
|
*/
|
|
|
|
static int
|
2005-06-14 22:01:04 +00:00
|
|
|
spell_casefold(str, len, buf, buflen)
|
|
|
|
char_u *str;
|
2005-06-06 21:50:35 +00:00
|
|
|
int len;
|
|
|
|
char_u *buf;
|
|
|
|
int buflen;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (len >= buflen)
|
|
|
|
{
|
|
|
|
buf[0] = NUL;
|
|
|
|
return FAIL; /* result will not fit */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
int outi = 0;
|
2005-06-14 22:01:04 +00:00
|
|
|
char_u *p;
|
|
|
|
int c;
|
2005-06-06 21:50:35 +00:00
|
|
|
|
|
|
|
/* Fold one character at a time. */
|
2005-06-14 22:01:04 +00:00
|
|
|
for (p = str; p < str + len; )
|
2005-06-06 21:50:35 +00:00
|
|
|
{
|
|
|
|
if (outi + MB_MAXBYTES > buflen)
|
|
|
|
{
|
|
|
|
buf[outi] = NUL;
|
|
|
|
return FAIL;
|
|
|
|
}
|
2005-06-14 22:01:04 +00:00
|
|
|
c = mb_ptr2char_adv(&p);
|
|
|
|
outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
|
2005-06-06 21:50:35 +00:00
|
|
|
}
|
|
|
|
buf[outi] = NUL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Be quick for non-multibyte encodings. */
|
|
|
|
for (i = 0; i < len; ++i)
|
2005-06-14 22:01:04 +00:00
|
|
|
buf[i] = spelltab.st_fold[str[i]];
|
2005-06-06 21:50:35 +00:00
|
|
|
buf[i] = NUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* "z?": Find badly spelled word under or after the cursor.
|
|
|
|
* Give suggestions for the properly spelled word.
|
|
|
|
* This is based on the mechanisms of Aspell, but completely reimplemented.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
spell_suggest()
|
|
|
|
{
|
|
|
|
char_u *line;
|
|
|
|
pos_T prev_cursor = curwin->w_cursor;
|
|
|
|
int attr;
|
|
|
|
char_u wcopy[MAXWLEN + 2];
|
|
|
|
char_u *p;
|
|
|
|
int i;
|
|
|
|
int c;
|
|
|
|
suginfo_T sug;
|
|
|
|
suggest_T *stp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the start of the badly spelled word.
|
|
|
|
*/
|
|
|
|
if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
|
|
|
|
{
|
|
|
|
beep_flush();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the info in "sug".
|
|
|
|
*/
|
|
|
|
vim_memset(&sug, 0, sizeof(sug));
|
|
|
|
ga_init2(&sug.su_ga, (int)sizeof(suggest_T), 10);
|
|
|
|
hash_init(&sug.su_banned);
|
|
|
|
line = ml_get_curline();
|
|
|
|
sug.su_badptr = line + curwin->w_cursor.col;
|
|
|
|
sug.su_badlen = spell_check(curwin, sug.su_badptr, &attr);
|
|
|
|
if (sug.su_badlen >= MAXWLEN)
|
|
|
|
sug.su_badlen = MAXWLEN - 1; /* just in case */
|
|
|
|
vim_strncpy(sug.su_badword, sug.su_badptr, sug.su_badlen);
|
|
|
|
(void)spell_casefold(sug.su_badptr, sug.su_badlen,
|
|
|
|
sug.su_fbadword, MAXWLEN);
|
|
|
|
|
|
|
|
/* Ban the bad word itself. It may appear in another region. */
|
|
|
|
add_banned(&sug, sug.su_badword);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1. Try inserting/deleting/swapping/changing a letter, use REP entries
|
|
|
|
* from the .aff file and inserting a space (split the word).
|
2005-06-14 22:01:04 +00:00
|
|
|
*
|
|
|
|
* Set a maximum score to limit the combination of operations that is
|
|
|
|
* tried.
|
2005-06-13 22:28:56 +00:00
|
|
|
*/
|
|
|
|
sug.su_maxscore = SCORE_MAXINIT;
|
|
|
|
spell_try_change(&sug);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2. Try finding sound-a-like words.
|
2005-06-14 22:01:04 +00:00
|
|
|
*
|
|
|
|
* Only do this when we don't have a lot of suggestions yet, because it's
|
|
|
|
* very slow and often doesn't find new suggestions.
|
2005-06-13 22:28:56 +00:00
|
|
|
*/
|
2005-06-14 22:01:04 +00:00
|
|
|
if (sug.su_ga.ga_len < SUG_CLEAN_COUNT)
|
|
|
|
{
|
|
|
|
/* Allow a higher score now. */
|
2005-06-13 22:28:56 +00:00
|
|
|
sug.su_maxscore = SCORE_MAXMAX;
|
2005-06-14 22:01:04 +00:00
|
|
|
spell_try_soundalike(&sug);
|
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* When CTRL-C was hit while searching do show the results. */
|
2005-06-14 22:01:04 +00:00
|
|
|
ui_breakcheck();
|
2005-06-13 22:28:56 +00:00
|
|
|
if (got_int)
|
|
|
|
{
|
|
|
|
(void)vgetc();
|
|
|
|
got_int = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sug.su_ga.ga_len == 0)
|
|
|
|
MSG(_("Sorry, no suggestions"));
|
|
|
|
else
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef RESCORE
|
|
|
|
/* Do slow but more accurate computation of the word score. */
|
|
|
|
rescore_suggestions(&sug);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Sort the suggestions and truncate at SUG_PROMPT_COUNT. */
|
|
|
|
cleanup_suggestions(&sug, SUG_PROMPT_COUNT);
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* List the suggestions. */
|
|
|
|
msg_start();
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
|
|
|
|
sug.su_badlen, sug.su_badptr);
|
|
|
|
msg_puts(IObuff);
|
|
|
|
msg_clr_eos();
|
|
|
|
msg_putchar('\n');
|
|
|
|
msg_scroll = TRUE;
|
|
|
|
for (i = 0; i < sug.su_ga.ga_len; ++i)
|
|
|
|
{
|
|
|
|
stp = &SUG(&sug, i);
|
|
|
|
|
|
|
|
/* The suggested word may replace only part of the bad word, add
|
|
|
|
* the not replaced part. */
|
|
|
|
STRCPY(wcopy, stp->st_word);
|
|
|
|
if (sug.su_badlen > stp->st_orglen)
|
|
|
|
vim_strncpy(wcopy + STRLEN(wcopy),
|
|
|
|
sug.su_badptr + stp->st_orglen,
|
|
|
|
sug.su_badlen - stp->st_orglen);
|
2005-06-14 22:01:04 +00:00
|
|
|
if (p_verbose > 0)
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\" (%d)"),
|
2005-06-13 22:28:56 +00:00
|
|
|
i + 1, wcopy, stp->st_score);
|
2005-06-14 22:01:04 +00:00
|
|
|
else
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""),
|
|
|
|
i + 1, wcopy);
|
2005-06-13 22:28:56 +00:00
|
|
|
msg_puts(IObuff);
|
|
|
|
lines_left = 3; /* avoid more prompt */
|
|
|
|
msg_putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ask for choice. */
|
|
|
|
i = prompt_for_number();
|
|
|
|
if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor())
|
|
|
|
{
|
|
|
|
/* Replace the word. */
|
|
|
|
stp = &SUG(&sug, i - 1);
|
|
|
|
p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
|
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
c = sug.su_badptr - line;
|
|
|
|
mch_memmove(p, line, c);
|
|
|
|
STRCPY(p + c, stp->st_word);
|
|
|
|
STRCAT(p, sug.su_badptr + stp->st_orglen);
|
|
|
|
ml_replace(curwin->w_cursor.lnum, p, FALSE);
|
|
|
|
curwin->w_cursor.col = c;
|
|
|
|
changed_bytes(curwin->w_cursor.lnum, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
curwin->w_cursor = prev_cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the suggestions. */
|
|
|
|
for (i = 0; i < sug.su_ga.ga_len; ++i)
|
|
|
|
vim_free(SUG(&sug, i).st_word);
|
|
|
|
ga_clear(&sug.su_ga);
|
|
|
|
|
|
|
|
/* Free the banned words. */
|
|
|
|
free_banned(&sug);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-14 22:01:04 +00:00
|
|
|
* Make a copy of "word", with the first letter upper or lower cased, to
|
|
|
|
* "wcopy[MAXWLEN]". "word" must not be empty.
|
|
|
|
* The result is NUL terminated.
|
2005-06-13 22:28:56 +00:00
|
|
|
*/
|
|
|
|
static void
|
2005-06-14 22:01:04 +00:00
|
|
|
onecap_copy(word, wcopy, upper)
|
2005-06-13 22:28:56 +00:00
|
|
|
char_u *word;
|
|
|
|
char_u *wcopy;
|
|
|
|
int upper; /* TRUE: first letter made upper case */
|
|
|
|
{
|
|
|
|
char_u *p;
|
|
|
|
int c;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
p = word;
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
c = mb_ptr2char_adv(&p);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
c = *p++;
|
|
|
|
if (upper)
|
2005-06-14 22:01:04 +00:00
|
|
|
c = SPELL_TOUPPER(c);
|
2005-06-13 22:28:56 +00:00
|
|
|
else
|
2005-06-14 22:01:04 +00:00
|
|
|
c = SPELL_TOFOLD(c);
|
2005-06-13 22:28:56 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
l = mb_char2bytes(c, wcopy);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
l = 1;
|
|
|
|
wcopy[0] = c;
|
|
|
|
}
|
2005-06-14 22:01:04 +00:00
|
|
|
vim_strncpy(wcopy + l, p, MAXWLEN - l);
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-14 22:01:04 +00:00
|
|
|
* Make a copy of "word" with all the letters upper cased into
|
|
|
|
* "wcopy[MAXWLEN]". The result is NUL terminated.
|
2005-06-13 22:28:56 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
allcap_copy(word, wcopy)
|
|
|
|
char_u *word;
|
|
|
|
char_u *wcopy;
|
|
|
|
{
|
|
|
|
char_u *s;
|
|
|
|
char_u *d;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
d = wcopy;
|
|
|
|
for (s = word; *s != NUL; )
|
|
|
|
{
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
c = mb_ptr2char_adv(&s);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
c = *s++;
|
2005-06-14 22:01:04 +00:00
|
|
|
c = SPELL_TOUPPER(c);
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
|
|
|
|
break;
|
|
|
|
d += mb_char2bytes(c, d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (d - wcopy >= MAXWLEN - 1)
|
|
|
|
break;
|
|
|
|
*d++ = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*d = NUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try finding suggestions by adding/removing/swapping letters.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spell_try_change(su)
|
|
|
|
suginfo_T *su;
|
|
|
|
{
|
|
|
|
char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
|
|
|
|
char_u tword[MAXWLEN]; /* good word collected so far */
|
|
|
|
trystate_T stack[MAXWLEN];
|
|
|
|
char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
|
|
|
|
* to for word split) */
|
|
|
|
char_u prewordlen = 0; /* length of word in "preword" */
|
|
|
|
int splitoff = 0; /* index in tword after last split */
|
|
|
|
trystate_T *sp;
|
|
|
|
int newscore;
|
|
|
|
langp_T *lp;
|
|
|
|
char_u *byts;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T *idxs;
|
2005-06-13 22:28:56 +00:00
|
|
|
int depth;
|
|
|
|
int c;
|
|
|
|
int n;
|
|
|
|
int flags;
|
|
|
|
int badflags;
|
|
|
|
garray_T *gap;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T arridx;
|
2005-06-13 22:28:56 +00:00
|
|
|
int len;
|
|
|
|
char_u *p;
|
|
|
|
fromto_T *ftp;
|
|
|
|
int fl, tl;
|
|
|
|
|
|
|
|
/* get caps flags for bad word */
|
|
|
|
badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
|
|
|
|
|
|
|
|
/* We make a copy of the case-folded bad word, so that we can modify it
|
|
|
|
* to find matches (esp. REP items). */
|
|
|
|
STRCPY(fword, su->su_fbadword);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At each node in the tree these states are tried:
|
|
|
|
*/
|
|
|
|
#define STATE_START 0 /* At start of node, check if word may end or
|
|
|
|
* split word. */
|
|
|
|
#define STATE_SPLITUNDO 1 /* Undo word split. */
|
|
|
|
#define STATE_ENDNUL 2 /* Past NUL bytes at start of the node. */
|
|
|
|
#define STATE_PLAIN 3 /* Use each byte of the node. */
|
|
|
|
#define STATE_DEL 4 /* Delete a byte from the bad word. */
|
|
|
|
#define STATE_INS 5 /* Insert a byte in the bad word. */
|
|
|
|
#define STATE_SWAP 6 /* Swap two bytes. */
|
|
|
|
#define STATE_SWAP3A 7 /* Swap two bytes over three. */
|
|
|
|
#define STATE_ROT3L 8 /* Rotate three bytes left */
|
|
|
|
#define STATE_ROT3R 9 /* Rotate three bytes right */
|
|
|
|
#define STATE_ROT_UNDO 10 /* undo rotating */
|
|
|
|
#define STATE_REP_INI 11 /* Prepare for using REP items. */
|
|
|
|
#define STATE_REP 12 /* Use matching REP items from the .aff file. */
|
|
|
|
#define STATE_REP_UNDO 13 /* Undo a REP item replacement. */
|
|
|
|
#define STATE_FINAL 99 /* End of this node. */
|
|
|
|
|
|
|
|
|
|
|
|
for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
|
|
lp->lp_slang != NULL; ++lp)
|
|
|
|
{
|
|
|
|
#ifdef SOUNDFOLD_SCORE
|
|
|
|
su->su_slang = lp->lp_slang;
|
|
|
|
if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
|
|
/* soundfold the bad word */
|
|
|
|
spell_soundfold(lp->lp_slang, su->su_fbadword, su->su_salword);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through the whole case-fold tree, try changes at each node.
|
|
|
|
* "tword[]" contains the word collected from nodes in the tree.
|
|
|
|
* "fword[]" the word we are trying to match with (initially the bad
|
|
|
|
* word).
|
|
|
|
*/
|
|
|
|
byts = lp->lp_slang->sl_fbyts;
|
|
|
|
idxs = lp->lp_slang->sl_fidxs;
|
|
|
|
|
|
|
|
depth = 0;
|
|
|
|
stack[0].ts_state = STATE_START;
|
|
|
|
stack[0].ts_score = 0;
|
|
|
|
stack[0].ts_curi = 1;
|
|
|
|
stack[0].ts_fidx = 0;
|
|
|
|
stack[0].ts_fidxtry = 0;
|
|
|
|
stack[0].ts_twordlen = 0;
|
|
|
|
stack[0].ts_arridx = 0;
|
|
|
|
|
|
|
|
while (depth >= 0 && !got_int)
|
|
|
|
{
|
|
|
|
sp = &stack[depth];
|
|
|
|
switch (sp->ts_state)
|
|
|
|
{
|
|
|
|
case STATE_START:
|
|
|
|
/*
|
|
|
|
* Start of node: Deal with NUL bytes, which means
|
|
|
|
* tword[] may end here.
|
|
|
|
*/
|
|
|
|
arridx = sp->ts_arridx; /* current node in the tree */
|
|
|
|
len = byts[arridx]; /* bytes in this node */
|
|
|
|
arridx += sp->ts_curi; /* index of current byte */
|
|
|
|
|
|
|
|
if (sp->ts_curi > len || (c = byts[arridx]) != 0)
|
|
|
|
{
|
|
|
|
/* Past bytes in node and/or past NUL bytes. */
|
|
|
|
sp->ts_state = STATE_ENDNUL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End of word in tree.
|
|
|
|
*/
|
|
|
|
++sp->ts_curi; /* eat one NUL byte */
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
flags = (int)idxs[arridx];
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Form the word with proper case in preword.
|
|
|
|
* If there is a word from a previous split, append.
|
|
|
|
*/
|
|
|
|
tword[sp->ts_twordlen] = NUL;
|
|
|
|
if (flags & WF_KEEPCAP)
|
|
|
|
/* Must find the word in the keep-case tree. */
|
|
|
|
find_keepcap_word(lp->lp_slang, tword + splitoff,
|
|
|
|
preword + prewordlen);
|
|
|
|
else
|
|
|
|
/* Include badflags: if the badword is onecap or allcap
|
|
|
|
* use that for the goodword too. */
|
|
|
|
make_case_word(tword + splitoff,
|
|
|
|
preword + prewordlen, flags | badflags);
|
|
|
|
|
|
|
|
/* Don't use a banned word. It may appear again as a good
|
|
|
|
* word, thus remember it. */
|
|
|
|
if (flags & WF_BANNED)
|
|
|
|
{
|
|
|
|
add_banned(su, preword + prewordlen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (was_banned(su, preword + prewordlen))
|
|
|
|
break;
|
|
|
|
|
|
|
|
newscore = 0;
|
|
|
|
if ((flags & WF_REGION)
|
|
|
|
&& (((unsigned)flags >> 8) & lp->lp_region) == 0)
|
|
|
|
newscore += SCORE_REGION;
|
|
|
|
if (flags & WF_RARE)
|
|
|
|
newscore += SCORE_RARE;
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Words that were not found in the text get a penalty. */
|
|
|
|
if ((flags & WF_USED) == 0)
|
|
|
|
newscore += SCORE_NOTUSED;
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
if (!spell_valid_case(badflags,
|
|
|
|
captype(preword + prewordlen, NULL)))
|
|
|
|
newscore += SCORE_ICASE;
|
|
|
|
|
|
|
|
if (fword[sp->ts_fidx] == 0)
|
|
|
|
{
|
|
|
|
/* The badword also ends: add suggestions, */
|
2005-06-14 22:01:04 +00:00
|
|
|
add_suggestion(su, preword, sp->ts_score + newscore
|
|
|
|
#ifdef RESCORE
|
|
|
|
, FALSE
|
|
|
|
#endif
|
|
|
|
);
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
else if (sp->ts_fidx >= sp->ts_fidxtry)
|
|
|
|
{
|
|
|
|
/* The word in the tree ends but the badword
|
|
|
|
* continues: try inserting a space and check that a valid
|
|
|
|
* words starts at fword[sp->ts_fidx]. */
|
|
|
|
if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
|
|
|
|
{
|
|
|
|
/* Save things to be restored at STATE_SPLITUNDO. */
|
|
|
|
sp->ts_save_prewordlen = prewordlen;
|
|
|
|
sp->ts_save_badflags = badflags;
|
|
|
|
sp->ts_save_splitoff = splitoff;
|
|
|
|
|
|
|
|
/* Append a space to preword. */
|
|
|
|
STRCAT(preword, " ");
|
|
|
|
prewordlen = STRLEN(preword);
|
|
|
|
splitoff = sp->ts_twordlen;
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* Case-folding may change the number of bytes:
|
|
|
|
* Count nr of chars in fword[sp->ts_fidx] and
|
|
|
|
* advance that many chars in su->su_badptr. */
|
|
|
|
for (p = fword; p < fword + sp->ts_fidx;
|
|
|
|
mb_ptr_adv(p))
|
|
|
|
++i;
|
|
|
|
for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
p = su->su_badptr + sp->ts_fidx;
|
|
|
|
badflags = captype(p, su->su_badptr + su->su_badlen);
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
sp->ts_state = STATE_SPLITUNDO;
|
|
|
|
++depth;
|
|
|
|
/* Restart at top of the tree. */
|
|
|
|
stack[depth].ts_arridx = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_SPLITUNDO:
|
|
|
|
/* Fixup the changes done for word split. */
|
|
|
|
badflags = sp->ts_save_badflags;
|
|
|
|
splitoff = sp->ts_save_splitoff;
|
|
|
|
prewordlen = sp->ts_save_prewordlen;
|
|
|
|
|
|
|
|
/* Continue looking for NUL bytes. */
|
|
|
|
sp->ts_state = STATE_START;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ENDNUL:
|
|
|
|
/* Past the NUL bytes in the node. */
|
|
|
|
if (fword[sp->ts_fidx] == 0)
|
|
|
|
{
|
|
|
|
/* The badword ends, can't use the bytes in this node. */
|
|
|
|
sp->ts_state = STATE_DEL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sp->ts_state = STATE_PLAIN;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
|
|
|
|
case STATE_PLAIN:
|
|
|
|
/*
|
|
|
|
* Go over all possible bytes at this node, add each to
|
|
|
|
* tword[] and use child node. "ts_curi" is the index.
|
|
|
|
*/
|
|
|
|
arridx = sp->ts_arridx;
|
|
|
|
if (sp->ts_curi > byts[arridx])
|
|
|
|
{
|
|
|
|
/* Done all bytes at this node, do next state. When still
|
|
|
|
* at already changed bytes skip the other tricks. */
|
|
|
|
if (sp->ts_fidx >= sp->ts_fidxtry)
|
|
|
|
sp->ts_state = STATE_DEL;
|
|
|
|
else
|
|
|
|
sp->ts_state = STATE_FINAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arridx += sp->ts_curi++;
|
|
|
|
c = byts[arridx];
|
|
|
|
|
|
|
|
/* Normal byte, go one level deeper. If it's not equal to
|
|
|
|
* the byte in the bad word adjust the score. But don't
|
|
|
|
* even try when the byte was already changed. */
|
|
|
|
if (c == fword[sp->ts_fidx])
|
|
|
|
newscore = 0;
|
2005-06-14 22:01:04 +00:00
|
|
|
|
|
|
|
/* TODO: this is too slow and comparing bytes isn't right
|
|
|
|
* for multi-byte characters. */
|
|
|
|
#if 0
|
2005-06-13 22:28:56 +00:00
|
|
|
else if (lp->lp_slang->sl_map != NULL
|
2005-06-14 22:01:04 +00:00
|
|
|
&& similar_chars(lp->lp_slang,
|
2005-06-13 22:28:56 +00:00
|
|
|
c, fword[sp->ts_fidx]))
|
|
|
|
newscore = SCORE_SIMILAR;
|
2005-06-14 22:01:04 +00:00
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
else
|
|
|
|
newscore = SCORE_SUBST;
|
|
|
|
if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
|
|
|
|
&& try_deeper(su, stack, depth, newscore))
|
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
++stack[depth].ts_fidx;
|
|
|
|
tword[stack[depth].ts_twordlen++] = c;
|
|
|
|
stack[depth].ts_arridx = idxs[arridx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DEL:
|
|
|
|
/* Try skipping one byte in the bad word (delete it). */
|
|
|
|
sp->ts_state = STATE_INS;
|
|
|
|
sp->ts_curi = 1;
|
|
|
|
if (fword[sp->ts_fidx] != NUL
|
|
|
|
&& try_deeper(su, stack, depth, SCORE_DEL))
|
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
++stack[depth].ts_fidx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
|
|
|
|
case STATE_INS:
|
|
|
|
/* Insert one byte. Do this for each possible bytes at this
|
|
|
|
* node. */
|
|
|
|
n = sp->ts_arridx;
|
|
|
|
if (sp->ts_curi > byts[n])
|
|
|
|
{
|
|
|
|
/* Done all bytes at this node, do next state. */
|
|
|
|
sp->ts_state = STATE_SWAP;
|
|
|
|
sp->ts_curi = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Do one more byte at this node. */
|
|
|
|
n += sp->ts_curi++;
|
|
|
|
c = byts[n];
|
|
|
|
if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
|
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
tword[stack[depth].ts_twordlen++] = c;
|
|
|
|
stack[depth].ts_arridx = idxs[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_SWAP:
|
|
|
|
/* Swap two bytes: "12" -> "21". This means looking for the
|
|
|
|
* following byte at the current node and the current byte at
|
|
|
|
* its child node. We change "fword" here, it's changed back
|
|
|
|
* afterwards. TODO: should swap characters instead of bytes.
|
|
|
|
* */
|
|
|
|
c = fword[sp->ts_fidx];
|
|
|
|
if (c != NUL && fword[sp->ts_fidx + 1] != NUL
|
|
|
|
&& try_deeper(su, stack, depth, SCORE_SWAP))
|
|
|
|
{
|
|
|
|
sp->ts_state = STATE_SWAP3A;
|
|
|
|
++depth;
|
|
|
|
fword[sp->ts_fidx] = fword[sp->ts_fidx + 1];
|
|
|
|
fword[sp->ts_fidx + 1] = c;
|
|
|
|
stack[depth].ts_fidxtry = sp->ts_fidx + 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* If this swap doesn't work then SWAP3 won't either. */
|
|
|
|
sp->ts_state = STATE_REP_INI;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_SWAP3A:
|
|
|
|
/* First undo the STATE_SWAP swap: "21" -> "12". */
|
|
|
|
c = fword[sp->ts_fidx];
|
|
|
|
fword[sp->ts_fidx] = fword[sp->ts_fidx + 1];
|
|
|
|
fword[sp->ts_fidx + 1] = c;
|
|
|
|
|
|
|
|
/* Swap two bytes, skipping one: "123" -> "321". We change
|
|
|
|
* "fword" here, it's changed back afterwards. TODO: should
|
|
|
|
* swap characters instead of bytes. */
|
|
|
|
c = fword[sp->ts_fidx];
|
|
|
|
if (c != NUL && fword[sp->ts_fidx + 1] != NUL
|
|
|
|
&& fword[sp->ts_fidx + 2] != NUL
|
|
|
|
&& try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
|
|
{
|
|
|
|
sp->ts_state = STATE_ROT3L;
|
|
|
|
++depth;
|
|
|
|
fword[sp->ts_fidx] = fword[sp->ts_fidx + 2];
|
|
|
|
fword[sp->ts_fidx + 2] = c;
|
|
|
|
stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sp->ts_state = STATE_REP_INI;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ROT3L:
|
|
|
|
/* First undo STATE_SWAP3A: "321" -> "123" */
|
|
|
|
c = fword[sp->ts_fidx];
|
|
|
|
fword[sp->ts_fidx] = fword[sp->ts_fidx + 2];
|
|
|
|
fword[sp->ts_fidx + 2] = c;
|
|
|
|
|
|
|
|
/* Rotate three bytes left: "123" -> "231". We change
|
|
|
|
* "fword" here, it's changed back afterwards. TODO: should
|
|
|
|
* swap characters instead of bytes. */
|
|
|
|
if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
|
|
{
|
|
|
|
sp->ts_state = STATE_ROT3R;
|
|
|
|
++depth;
|
|
|
|
c = fword[sp->ts_fidx];
|
|
|
|
fword[sp->ts_fidx] = fword[sp->ts_fidx + 1];
|
|
|
|
fword[sp->ts_fidx + 1] = fword[sp->ts_fidx + 2];
|
|
|
|
fword[sp->ts_fidx + 2] = c;
|
|
|
|
stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sp->ts_state = STATE_REP_INI;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ROT3R:
|
|
|
|
/* First undo STATE_ROT3L: "231" -> "123" */
|
|
|
|
c = fword[sp->ts_fidx + 2];
|
|
|
|
fword[sp->ts_fidx + 2] = fword[sp->ts_fidx + 1];
|
|
|
|
fword[sp->ts_fidx + 1] = fword[sp->ts_fidx];
|
|
|
|
fword[sp->ts_fidx] = c;
|
|
|
|
|
|
|
|
/* Rotate three bytes right: "123" -> "312". We change
|
|
|
|
* "fword" here, it's changed back afterwards. TODO: should
|
|
|
|
* swap characters instead of bytes. */
|
|
|
|
if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
|
|
{
|
|
|
|
sp->ts_state = STATE_ROT_UNDO;
|
|
|
|
++depth;
|
|
|
|
c = fword[sp->ts_fidx + 2];
|
|
|
|
fword[sp->ts_fidx + 2] = fword[sp->ts_fidx + 1];
|
|
|
|
fword[sp->ts_fidx + 1] = fword[sp->ts_fidx];
|
|
|
|
fword[sp->ts_fidx] = c;
|
|
|
|
stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sp->ts_state = STATE_REP_INI;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ROT_UNDO:
|
|
|
|
/* Undo STATE_ROT3R: "312" -> "123" */
|
|
|
|
c = fword[sp->ts_fidx];
|
|
|
|
fword[sp->ts_fidx] = fword[sp->ts_fidx + 1];
|
|
|
|
fword[sp->ts_fidx + 1] = fword[sp->ts_fidx + 2];
|
|
|
|
fword[sp->ts_fidx + 2] = c;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
|
|
|
|
case STATE_REP_INI:
|
|
|
|
/* Check if matching with REP items from the .aff file would
|
|
|
|
* work. Quickly skip if there are no REP items or the score
|
|
|
|
* is going to be too high anyway. */
|
|
|
|
gap = &lp->lp_slang->sl_rep;
|
|
|
|
if (gap->ga_len == 0
|
|
|
|
|| sp->ts_score + SCORE_REP >= su->su_maxscore)
|
|
|
|
{
|
|
|
|
sp->ts_state = STATE_FINAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use the first byte to quickly find the first entry that
|
|
|
|
* matches. If the index is -1 there is none. */
|
|
|
|
sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
|
|
|
|
if (sp->ts_curi < 0)
|
|
|
|
{
|
|
|
|
sp->ts_state = STATE_FINAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp->ts_state = STATE_REP;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
|
|
|
|
case STATE_REP:
|
|
|
|
/* Try matching with REP items from the .aff file. For each
|
|
|
|
* match replace the charactes and check if the resulting word
|
|
|
|
* is valid. */
|
|
|
|
p = fword + sp->ts_fidx;
|
|
|
|
|
|
|
|
gap = &lp->lp_slang->sl_rep;
|
|
|
|
while (sp->ts_curi < gap->ga_len)
|
|
|
|
{
|
|
|
|
ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
|
|
|
|
if (*ftp->ft_from != *p)
|
|
|
|
{
|
|
|
|
/* past possible matching entries */
|
|
|
|
sp->ts_curi = gap->ga_len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
|
|
|
|
&& try_deeper(su, stack, depth, SCORE_REP))
|
|
|
|
{
|
|
|
|
/* Need to undo this afterwards. */
|
|
|
|
sp->ts_state = STATE_REP_UNDO;
|
|
|
|
|
|
|
|
/* Change the "from" to the "to" string. */
|
|
|
|
++depth;
|
|
|
|
fl = STRLEN(ftp->ft_from);
|
|
|
|
tl = STRLEN(ftp->ft_to);
|
|
|
|
if (fl != tl)
|
|
|
|
mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
|
|
|
|
mch_memmove(p, ftp->ft_to, tl);
|
|
|
|
stack[depth].ts_fidxtry = sp->ts_fidx + tl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sp->ts_curi >= gap->ga_len)
|
|
|
|
/* No (more) matches. */
|
|
|
|
sp->ts_state = STATE_FINAL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_REP_UNDO:
|
|
|
|
/* Undo a REP replacement and continue with the next one. */
|
|
|
|
ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
|
|
|
|
+ sp->ts_curi - 1;
|
|
|
|
fl = STRLEN(ftp->ft_from);
|
|
|
|
tl = STRLEN(ftp->ft_to);
|
|
|
|
p = fword + sp->ts_fidx;
|
|
|
|
if (fl != tl)
|
|
|
|
mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
|
|
|
|
mch_memmove(p, ftp->ft_from, fl);
|
|
|
|
sp->ts_state = STATE_REP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Did all possible states at this level, go up one level. */
|
|
|
|
--depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_breakcheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try going one level deeper in the tree.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
try_deeper(su, stack, depth, score_add)
|
|
|
|
suginfo_T *su;
|
|
|
|
trystate_T *stack;
|
|
|
|
int depth;
|
|
|
|
int score_add;
|
|
|
|
{
|
|
|
|
int newscore;
|
|
|
|
|
|
|
|
/* Refuse to go deeper if the scrore is getting too big. */
|
|
|
|
newscore = stack[depth].ts_score + score_add;
|
|
|
|
if (newscore >= su->su_maxscore)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
stack[depth + 1].ts_state = STATE_START;
|
|
|
|
stack[depth + 1].ts_score = newscore;
|
|
|
|
stack[depth + 1].ts_curi = 1; /* start just after length byte */
|
|
|
|
stack[depth + 1].ts_fidx = stack[depth].ts_fidx;
|
|
|
|
stack[depth + 1].ts_fidxtry = stack[depth].ts_fidxtry;
|
|
|
|
stack[depth + 1].ts_twordlen = stack[depth].ts_twordlen;
|
|
|
|
stack[depth + 1].ts_arridx = stack[depth].ts_arridx;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "fword" is a good word with case folded. Find the matching keep-case
|
|
|
|
* words and put it in "kword".
|
|
|
|
* Theoretically there could be several keep-case words that result in the
|
|
|
|
* same case-folded word, but we only find one...
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
find_keepcap_word(slang, fword, kword)
|
|
|
|
slang_T *slang;
|
|
|
|
char_u *fword;
|
|
|
|
char_u *kword;
|
|
|
|
{
|
|
|
|
char_u uword[MAXWLEN]; /* "fword" in upper-case */
|
|
|
|
int depth;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T tryidx;
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* The following arrays are used at each depth in the tree. */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T arridx[MAXWLEN];
|
2005-06-13 22:28:56 +00:00
|
|
|
int round[MAXWLEN];
|
|
|
|
int fwordidx[MAXWLEN];
|
|
|
|
int uwordidx[MAXWLEN];
|
|
|
|
int kwordlen[MAXWLEN];
|
|
|
|
|
|
|
|
int flen, ulen;
|
|
|
|
int l;
|
|
|
|
int len;
|
|
|
|
int c;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T lo, hi, m;
|
2005-06-13 22:28:56 +00:00
|
|
|
char_u *p;
|
|
|
|
char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T *idxs = slang->sl_kidxs; /* array with indexes */
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
if (byts == NULL)
|
|
|
|
{
|
|
|
|
/* array is empty: "cannot happen" */
|
|
|
|
*kword = NUL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make an all-cap version of "fword". */
|
|
|
|
allcap_copy(fword, uword);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each character needs to be tried both case-folded and upper-case.
|
|
|
|
* All this gets very complicated if we keep in mind that changing case
|
|
|
|
* may change the byte length of a multi-byte character...
|
|
|
|
*/
|
|
|
|
depth = 0;
|
|
|
|
arridx[0] = 0;
|
|
|
|
round[0] = 0;
|
|
|
|
fwordidx[0] = 0;
|
|
|
|
uwordidx[0] = 0;
|
|
|
|
kwordlen[0] = 0;
|
|
|
|
while (depth >= 0)
|
|
|
|
{
|
|
|
|
if (fword[fwordidx[depth]] == NUL)
|
|
|
|
{
|
|
|
|
/* We are at the end of "fword". If the tree allows a word to end
|
|
|
|
* here we have found a match. */
|
|
|
|
if (byts[arridx[depth] + 1] == 0)
|
|
|
|
{
|
|
|
|
kword[kwordlen[depth]] = NUL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* kword is getting too long, continue one level up */
|
|
|
|
--depth;
|
|
|
|
}
|
|
|
|
else if (++round[depth] > 2)
|
|
|
|
{
|
|
|
|
/* tried both fold-case and upper-case character, continue one
|
|
|
|
* level up */
|
|
|
|
--depth;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* round[depth] == 1: Try using the folded-case character.
|
|
|
|
* round[depth] == 2: Try using the upper-case character.
|
|
|
|
*/
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
flen = mb_ptr2len_check(fword + fwordidx[depth]);
|
|
|
|
ulen = mb_ptr2len_check(uword + uwordidx[depth]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
ulen = flen = 1;
|
|
|
|
if (round[depth] == 1)
|
|
|
|
{
|
|
|
|
p = fword + fwordidx[depth];
|
|
|
|
l = flen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = uword + uwordidx[depth];
|
|
|
|
l = ulen;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (tryidx = arridx[depth]; l > 0; --l)
|
|
|
|
{
|
|
|
|
/* Perform a binary search in the list of accepted bytes. */
|
|
|
|
len = byts[tryidx++];
|
|
|
|
c = *p++;
|
|
|
|
lo = tryidx;
|
|
|
|
hi = tryidx + len - 1;
|
|
|
|
while (lo < hi)
|
|
|
|
{
|
|
|
|
m = (lo + hi) / 2;
|
|
|
|
if (byts[m] > c)
|
|
|
|
hi = m - 1;
|
|
|
|
else if (byts[m] < c)
|
|
|
|
lo = m + 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lo = hi = m;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop if there is no matching byte. */
|
|
|
|
if (hi < lo || byts[lo] != c)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Continue at the child (if there is one). */
|
|
|
|
tryidx = idxs[lo];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Found the matching char. Copy it to "kword" and go a
|
|
|
|
* level deeper.
|
|
|
|
*/
|
|
|
|
if (round[depth] == 1)
|
|
|
|
{
|
|
|
|
STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
|
|
|
|
flen);
|
|
|
|
kwordlen[depth + 1] = kwordlen[depth] + flen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
|
|
|
|
ulen);
|
|
|
|
kwordlen[depth + 1] = kwordlen[depth] + ulen;
|
|
|
|
}
|
|
|
|
fwordidx[depth + 1] = fwordidx[depth] + flen;
|
|
|
|
uwordidx[depth + 1] = uwordidx[depth] + ulen;
|
|
|
|
|
|
|
|
++depth;
|
|
|
|
arridx[depth] = tryidx;
|
|
|
|
round[depth] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Didn't find it: "cannot happen". */
|
|
|
|
*kword = NUL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find suggestions by comparing the word in a sound-a-like form.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spell_try_soundalike(su)
|
|
|
|
suginfo_T *su;
|
|
|
|
{
|
|
|
|
char_u salword[MAXWLEN];
|
|
|
|
char_u tword[MAXWLEN];
|
|
|
|
char_u tfword[MAXWLEN];
|
|
|
|
char_u tsalword[MAXWLEN];
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T arridx[MAXWLEN];
|
2005-06-13 22:28:56 +00:00
|
|
|
int curi[MAXWLEN];
|
|
|
|
langp_T *lp;
|
|
|
|
char_u *byts;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T *idxs;
|
2005-06-13 22:28:56 +00:00
|
|
|
int depth;
|
|
|
|
int c;
|
2005-06-14 22:01:04 +00:00
|
|
|
idx_T n;
|
2005-06-13 22:28:56 +00:00
|
|
|
int round;
|
|
|
|
int flags;
|
2005-06-14 22:01:04 +00:00
|
|
|
int score, sound_score;
|
|
|
|
char_u *bp, *sp;
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
|
|
lp->lp_slang != NULL; ++lp)
|
|
|
|
{
|
|
|
|
if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
|
|
{
|
|
|
|
/* soundfold the bad word */
|
|
|
|
spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through the whole tree, soundfold each word and compare.
|
|
|
|
* round 1: use the case-folded tree.
|
|
|
|
* round 2: use the keep-case tree.
|
|
|
|
*/
|
|
|
|
for (round = 1; round <= 2; ++round)
|
|
|
|
{
|
|
|
|
if (round == 1)
|
|
|
|
{
|
|
|
|
byts = lp->lp_slang->sl_fbyts;
|
|
|
|
idxs = lp->lp_slang->sl_fidxs;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
byts = lp->lp_slang->sl_kbyts;
|
|
|
|
idxs = lp->lp_slang->sl_kidxs;
|
|
|
|
}
|
|
|
|
|
|
|
|
depth = 0;
|
|
|
|
arridx[0] = 0;
|
|
|
|
curi[0] = 1;
|
|
|
|
while (depth >= 0 && !got_int)
|
|
|
|
{
|
|
|
|
if (curi[depth] > byts[arridx[depth]])
|
|
|
|
/* Done all bytes at this node, go up one level. */
|
|
|
|
--depth;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Do one more byte at this node. */
|
|
|
|
n = arridx[depth] + curi[depth];
|
|
|
|
++curi[depth];
|
|
|
|
c = byts[n];
|
|
|
|
if (c == 0)
|
|
|
|
{
|
|
|
|
/* End of word, deal with the word. */
|
2005-06-14 22:01:04 +00:00
|
|
|
flags = (int)idxs[n];
|
2005-06-13 22:28:56 +00:00
|
|
|
if (round == 2 || (flags & WF_KEEPCAP) == 0)
|
|
|
|
{
|
|
|
|
tword[depth] = NUL;
|
|
|
|
if (round == 1)
|
|
|
|
spell_soundfold(lp->lp_slang,
|
|
|
|
tword, tsalword);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In keep-case tree need to case-fold the
|
|
|
|
* word. */
|
|
|
|
(void)spell_casefold(tword, depth,
|
|
|
|
tfword, MAXWLEN);
|
|
|
|
spell_soundfold(lp->lp_slang,
|
|
|
|
tfword, tsalword);
|
|
|
|
}
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/*
|
|
|
|
* Accept the word if the sound-folded words
|
|
|
|
* are (almost) equal.
|
|
|
|
*/
|
|
|
|
for (bp = salword, sp = tsalword; *bp == *sp;
|
|
|
|
++bp, ++sp)
|
|
|
|
if (*bp == NUL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*bp == *sp)
|
|
|
|
/* equal */
|
|
|
|
sound_score = 0;
|
|
|
|
else if (*bp != NUL && bp[1] != NUL
|
|
|
|
&& *bp == sp[1] && bp[1] == *sp
|
|
|
|
&& STRCMP(bp + 2, sp + 2) == 0)
|
|
|
|
/* swap two bytes */
|
|
|
|
sound_score = SCORE_SWAP;
|
|
|
|
else if (STRCMP(bp + 1, sp) == 0)
|
|
|
|
/* delete byte */
|
|
|
|
sound_score = SCORE_DEL;
|
|
|
|
else if (STRCMP(bp, sp + 1) == 0)
|
|
|
|
/* insert byte */
|
|
|
|
sound_score = SCORE_INS;
|
|
|
|
else if (STRCMP(bp + 1, sp + 1) == 0)
|
|
|
|
/* skip one byte */
|
|
|
|
sound_score = SCORE_SUBST;
|
|
|
|
else
|
|
|
|
/* not equal or similar */
|
|
|
|
sound_score = SCORE_MAXMAX;
|
|
|
|
|
|
|
|
if (sound_score < SCORE_MAXMAX)
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
char_u cword[MAXWLEN];
|
|
|
|
char_u *p;
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
if (round == 1 && flags != 0)
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Need to fix case according to
|
|
|
|
* "flags". */
|
2005-06-13 22:28:56 +00:00
|
|
|
make_case_word(tword, cword, flags);
|
2005-06-14 22:01:04 +00:00
|
|
|
p = cword;
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
else
|
2005-06-14 22:01:04 +00:00
|
|
|
p = tword;
|
|
|
|
|
|
|
|
/* Compute the score. */
|
|
|
|
score = spell_edit_score(su->su_badword, p);
|
|
|
|
#ifdef RESCORE
|
|
|
|
/* give a bonus for the good word sounding
|
|
|
|
* the same as the bad word */
|
|
|
|
add_suggestion(su, tword,
|
|
|
|
RESCORE(score, sound_score),
|
|
|
|
TRUE);
|
|
|
|
#else
|
|
|
|
add_suggestion(su, tword,
|
|
|
|
score + sound_score);
|
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip over other NUL bytes. */
|
|
|
|
while (byts[n + 1] == 0)
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
++curi[depth];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Normal char, go one level deeper. */
|
|
|
|
tword[depth++] = c;
|
|
|
|
arridx[depth] = idxs[n];
|
|
|
|
curi[depth] = 1;
|
|
|
|
}
|
|
|
|
}
|
2005-06-14 22:01:04 +00:00
|
|
|
|
|
|
|
line_breakcheck();
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-06-14 22:01:04 +00:00
|
|
|
* Copy "fword" to "cword", fixing case according to "flags".
|
2005-06-13 22:28:56 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
make_case_word(fword, cword, flags)
|
|
|
|
char_u *fword;
|
|
|
|
char_u *cword;
|
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
if (flags & WF_ALLCAP)
|
|
|
|
/* Make it all upper-case */
|
|
|
|
allcap_copy(fword, cword);
|
|
|
|
else if (flags & WF_ONECAP)
|
|
|
|
/* Make the first letter upper-case */
|
2005-06-14 22:01:04 +00:00
|
|
|
onecap_copy(fword, cword, TRUE);
|
2005-06-13 22:28:56 +00:00
|
|
|
else
|
|
|
|
/* Use goodword as-is. */
|
|
|
|
STRCPY(cword, fword);
|
|
|
|
}
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
#if 0
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Return TRUE if "c1" and "c2" are similar characters according to the MAP
|
|
|
|
* lines in the .aff file.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
similar_chars(slang, c1, c2)
|
|
|
|
slang_T *slang;
|
|
|
|
int c1;
|
|
|
|
int c2;
|
|
|
|
{
|
|
|
|
char_u *p1;
|
|
|
|
char_u *p2;
|
|
|
|
|
|
|
|
/* The similar characters are stored separated with slashes:
|
|
|
|
* "aaa/bbb/ccc/". Search for each character and if the next slash is the
|
|
|
|
* same one they are in the same MAP entry. */
|
|
|
|
p1 = vim_strchr(slang->sl_map, c1);
|
|
|
|
if (p1 == NULL)
|
|
|
|
return FALSE;
|
|
|
|
p2 = vim_strchr(slang->sl_map, c2);
|
|
|
|
if (p2 == NULL)
|
|
|
|
return FALSE;
|
|
|
|
return vim_strchr(p1, '/') == vim_strchr(p2, '/');
|
|
|
|
}
|
2005-06-14 22:01:04 +00:00
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a suggestion to the list of suggestions.
|
|
|
|
* Do not add a duplicate suggestion or suggestions with a bad score.
|
|
|
|
* When "use_score" is not zero it's used, otherwise the score is computed
|
|
|
|
* with spell_edit_score().
|
|
|
|
*/
|
|
|
|
static void
|
2005-06-14 22:01:04 +00:00
|
|
|
add_suggestion(su, goodword, score
|
|
|
|
#ifdef RESCORE
|
|
|
|
, had_bonus
|
|
|
|
#endif
|
|
|
|
)
|
2005-06-13 22:28:56 +00:00
|
|
|
suginfo_T *su;
|
|
|
|
char_u *goodword;
|
2005-06-14 22:01:04 +00:00
|
|
|
int score;
|
|
|
|
#ifdef RESCORE
|
|
|
|
int had_bonus; /* set st_had_bonus */
|
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
|
|
|
suggest_T *stp;
|
|
|
|
int i;
|
|
|
|
#ifdef SOUNDFOLD_SCORE
|
|
|
|
char_u fword[MAXWLEN];
|
|
|
|
char_u salword[MAXWLEN];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check that the word wasn't banned. */
|
|
|
|
if (was_banned(su, goodword))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (score <= su->su_maxscore)
|
|
|
|
{
|
|
|
|
#ifdef SOUNDFOLD_SCORE
|
|
|
|
/* Add to the score when the word sounds differently.
|
|
|
|
* This is slow... */
|
|
|
|
if (su->su_slang->sl_sal.ga_len > 0)
|
2005-06-14 22:01:04 +00:00
|
|
|
score += spell_sound_score(su->su_slang, fword, su->su_salword);
|
2005-06-13 22:28:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check if the word is already there. */
|
|
|
|
stp = &SUG(su, 0);
|
|
|
|
for (i = su->su_ga.ga_len - 1; i >= 0; --i)
|
|
|
|
if (STRCMP(stp[i].st_word, goodword) == 0)
|
|
|
|
{
|
|
|
|
/* Found it. Remember the lowest score. */
|
|
|
|
if (stp[i].st_score > score)
|
2005-06-14 22:01:04 +00:00
|
|
|
{
|
2005-06-13 22:28:56 +00:00
|
|
|
stp[i].st_score = score;
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef RESCORE
|
|
|
|
stp[i].st_had_bonus = had_bonus;
|
|
|
|
#endif
|
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < 0 && ga_grow(&su->su_ga, 1) == OK)
|
|
|
|
{
|
|
|
|
/* Add a suggestion. */
|
|
|
|
stp = &SUG(su, su->su_ga.ga_len);
|
|
|
|
stp->st_word = vim_strsave(goodword);
|
|
|
|
if (stp->st_word != NULL)
|
|
|
|
{
|
|
|
|
stp->st_score = score;
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef RESCORE
|
|
|
|
stp->st_had_bonus = had_bonus;
|
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
stp->st_orglen = su->su_badlen;
|
|
|
|
++su->su_ga.ga_len;
|
|
|
|
|
|
|
|
/* If we have too many suggestions now, sort the list and keep
|
|
|
|
* the best suggestions. */
|
2005-06-14 22:01:04 +00:00
|
|
|
if (su->su_ga.ga_len > SUG_MAX_COUNT)
|
|
|
|
cleanup_suggestions(su, SUG_CLEAN_COUNT);
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a word to be banned.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
add_banned(su, word)
|
|
|
|
suginfo_T *su;
|
|
|
|
char_u *word;
|
|
|
|
{
|
|
|
|
char_u *s = vim_strsave(word);
|
|
|
|
hash_T hash;
|
|
|
|
hashitem_T *hi;
|
|
|
|
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
|
|
|
hash = hash_hash(s);
|
|
|
|
hi = hash_lookup(&su->su_banned, s, hash);
|
|
|
|
if (HASHITEM_EMPTY(hi))
|
|
|
|
hash_add_item(&su->su_banned, hi, s, hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return TRUE if a word appears in the list of banned words.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
was_banned(su, word)
|
|
|
|
suginfo_T *su;
|
|
|
|
char_u *word;
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
hashitem_T *hi = hash_find(&su->su_banned, word);
|
|
|
|
|
|
|
|
return !HASHITEM_EMPTY(hi);
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the banned words in "su".
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
free_banned(su)
|
|
|
|
suginfo_T *su;
|
|
|
|
{
|
|
|
|
int todo;
|
|
|
|
hashitem_T *hi;
|
|
|
|
|
|
|
|
todo = su->su_banned.ht_used;
|
|
|
|
for (hi = su->su_banned.ht_array; todo > 0; ++hi)
|
|
|
|
{
|
|
|
|
if (!HASHITEM_EMPTY(hi))
|
|
|
|
{
|
|
|
|
vim_free(hi->hi_key);
|
|
|
|
--todo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hash_clear(&su->su_banned);
|
|
|
|
}
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef RESCORE
|
|
|
|
/*
|
|
|
|
* Recompute the score if sound-folding is possible. This is slow,
|
|
|
|
* thus only done for the final results.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rescore_suggestions(su)
|
|
|
|
suginfo_T *su;
|
|
|
|
{
|
|
|
|
langp_T *lp;
|
|
|
|
suggest_T *stp;
|
|
|
|
char_u sal_badword[MAXWLEN];
|
|
|
|
int score;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
|
|
lp->lp_slang != NULL; ++lp)
|
|
|
|
{
|
|
|
|
if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
|
|
{
|
|
|
|
/* soundfold the bad word */
|
|
|
|
spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
|
|
|
|
|
|
|
|
for (i = 0; i < su->su_ga.ga_len; ++i)
|
|
|
|
{
|
|
|
|
stp = &SUG(su, i);
|
|
|
|
if (!stp->st_had_bonus)
|
|
|
|
{
|
|
|
|
score = spell_sound_score(lp->lp_slang, stp->st_word,
|
|
|
|
sal_badword);
|
|
|
|
stp->st_score = RESCORE(stp->st_score, score);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
static int
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
_RTLENTRYF
|
|
|
|
#endif
|
|
|
|
sug_compare __ARGS((const void *s1, const void *s2));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function given to qsort() to sort the suggestions on st_score.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
_RTLENTRYF
|
|
|
|
#endif
|
|
|
|
sug_compare(s1, s2)
|
|
|
|
const void *s1;
|
|
|
|
const void *s2;
|
|
|
|
{
|
|
|
|
suggest_T *p1 = (suggest_T *)s1;
|
|
|
|
suggest_T *p2 = (suggest_T *)s2;
|
|
|
|
|
|
|
|
return p1->st_score - p2->st_score;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleanup the suggestions:
|
|
|
|
* - Sort on score.
|
|
|
|
* - Remove words that won't be displayed.
|
|
|
|
*/
|
|
|
|
static void
|
2005-06-14 22:01:04 +00:00
|
|
|
cleanup_suggestions(su, keep)
|
2005-06-13 22:28:56 +00:00
|
|
|
suginfo_T *su;
|
2005-06-14 22:01:04 +00:00
|
|
|
int keep; /* nr of suggestions to keep */
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
|
|
|
suggest_T *stp = &SUG(su, 0);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Sort the list. */
|
|
|
|
qsort(su->su_ga.ga_data, (size_t)su->su_ga.ga_len,
|
|
|
|
sizeof(suggest_T), sug_compare);
|
|
|
|
|
|
|
|
/* Truncate the list to the number of suggestions that will be displayed. */
|
2005-06-14 22:01:04 +00:00
|
|
|
if (su->su_ga.ga_len > keep)
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
for (i = keep; i < su->su_ga.ga_len; ++i)
|
2005-06-13 22:28:56 +00:00
|
|
|
vim_free(stp[i].st_word);
|
2005-06-14 22:01:04 +00:00
|
|
|
su->su_ga.ga_len = keep;
|
|
|
|
su->su_maxscore = stp[keep - 1].st_score;
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
spell_soundfold(slang, inword, res)
|
|
|
|
slang_T *slang;
|
|
|
|
char_u *inword;
|
|
|
|
char_u *res;
|
|
|
|
{
|
|
|
|
fromto_T *ftp;
|
|
|
|
char_u word[MAXWLEN];
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
int l;
|
2005-06-14 22:01:04 +00:00
|
|
|
int found_mbyte = FALSE;
|
2005-06-13 22:28:56 +00:00
|
|
|
#endif
|
|
|
|
char_u *s;
|
|
|
|
char_u *t;
|
|
|
|
int i, j, z;
|
|
|
|
int n, k = 0;
|
|
|
|
int z0;
|
|
|
|
int k0;
|
|
|
|
int n0;
|
|
|
|
int c;
|
|
|
|
int pri;
|
|
|
|
int p0 = -333;
|
|
|
|
int c0;
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
/* Remove accents, if wanted. We actually remove all non-word characters.
|
|
|
|
* But keep white space. */
|
2005-06-13 22:28:56 +00:00
|
|
|
if (slang->sl_rem_accents)
|
|
|
|
{
|
|
|
|
t = word;
|
|
|
|
for (s = inword; *s != NUL; )
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
if (vim_iswhite(*s))
|
|
|
|
*t++ = *s++;
|
2005-06-13 22:28:56 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
2005-06-14 22:01:04 +00:00
|
|
|
else if (has_mbyte)
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
|
|
|
l = mb_ptr2len_check(s);
|
|
|
|
if (SPELL_ISWORDP(s))
|
|
|
|
{
|
|
|
|
mch_memmove(t, s, l);
|
|
|
|
t += l;
|
2005-06-14 22:01:04 +00:00
|
|
|
if (l > 1)
|
|
|
|
found_mbyte = TRUE;
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
s += l;
|
|
|
|
}
|
|
|
|
#endif
|
2005-06-14 22:01:04 +00:00
|
|
|
else
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
|
|
|
if (SPELL_ISWORDP(s))
|
|
|
|
*t++ = *s;
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*t = NUL;
|
|
|
|
}
|
|
|
|
else
|
2005-06-14 22:01:04 +00:00
|
|
|
{
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
for (s = inword; *s != NUL; s += l)
|
|
|
|
if ((l = mb_ptr2len_check(s)) > 1)
|
|
|
|
{
|
|
|
|
found_mbyte = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
STRCPY(word, inword);
|
2005-06-14 22:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
/* If there are multi-byte characters in the word return it as-is, because
|
|
|
|
* the following won't work. */
|
|
|
|
if (found_mbyte)
|
|
|
|
{
|
|
|
|
STRCPY(res, word);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
ftp = (fromto_T *)slang->sl_sal.ga_data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This comes from Aspell phonet.cpp. Converted from C++ to C.
|
2005-06-14 22:01:04 +00:00
|
|
|
* Changed to keep spaces.
|
2005-06-13 22:28:56 +00:00
|
|
|
* TODO: support for multi-byte chars.
|
|
|
|
*/
|
|
|
|
i = j = z = 0;
|
|
|
|
while ((c = word[i]) != NUL)
|
|
|
|
{
|
|
|
|
n = slang->sl_sal_first[c];
|
|
|
|
z0 = 0;
|
|
|
|
|
|
|
|
if (n >= 0)
|
|
|
|
{
|
|
|
|
/* check all rules for the same letter */
|
|
|
|
while (ftp[n].ft_from[0] == c)
|
|
|
|
{
|
|
|
|
/* check whole string */
|
|
|
|
k = 1; /* number of found letters */
|
|
|
|
pri = 5; /* default priority */
|
|
|
|
s = ftp[n].ft_from;
|
|
|
|
s++; /* important for (see below) "*(s-1)" */
|
|
|
|
|
|
|
|
/* Skip over normal letters that match with the word. */
|
|
|
|
while (*s != NUL && word[i + k] == *s
|
|
|
|
&& !vim_isdigit(*s) && strchr("(-<^$", *s) == NULL)
|
|
|
|
{
|
|
|
|
k++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s == '(')
|
|
|
|
{
|
|
|
|
/* check alternate letters in "(..)" */
|
|
|
|
for (t = s + 1; *t != ')' && *t != NUL; ++t)
|
|
|
|
if (*t == word[i + k])
|
|
|
|
{
|
|
|
|
/* match */
|
|
|
|
++k;
|
|
|
|
for (s = t + 1; *s != NUL; ++s)
|
|
|
|
if (*s == ')')
|
|
|
|
{
|
|
|
|
++s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p0 = *s;
|
|
|
|
k0 = k;
|
|
|
|
while (*s == '-' && k > 1)
|
|
|
|
{
|
|
|
|
k--;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (*s == '<')
|
|
|
|
s++;
|
|
|
|
if (vim_isdigit(*s))
|
|
|
|
{
|
|
|
|
/* determine priority */
|
|
|
|
pri = *s - '0';
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (*s == '^' && *(s + 1) == '^')
|
|
|
|
s++;
|
|
|
|
|
|
|
|
if (*s == NUL
|
|
|
|
|| (*s == '^'
|
2005-06-14 22:01:04 +00:00
|
|
|
&& (i == 0 || !(word[i - 1] == ' '
|
|
|
|
|| SPELL_ISWORDP(word + i - 1)))
|
2005-06-13 22:28:56 +00:00
|
|
|
&& (*(s + 1) != '$'
|
|
|
|
|| (!SPELL_ISWORDP(word + i + k0))))
|
|
|
|
|| (*s == '$' && i > 0
|
|
|
|
&& SPELL_ISWORDP(word + i - 1)
|
|
|
|
&& (!SPELL_ISWORDP(word + i + k0))))
|
|
|
|
{
|
|
|
|
/* search for followup rules, if: */
|
|
|
|
/* followup and k > 1 and NO '-' in searchstring */
|
|
|
|
c0 = word[i + k - 1];
|
|
|
|
n0 = slang->sl_sal_first[c0];
|
|
|
|
|
|
|
|
if (slang->sl_followup && k > 1 && n0 >= 0
|
|
|
|
&& p0 != '-' && word[i + k] != NUL)
|
|
|
|
{
|
|
|
|
/* test follow-up rule for "word[i + k]" */
|
|
|
|
while (ftp[n0].ft_from[0] == c0)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* check whole string */
|
|
|
|
k0 = k;
|
|
|
|
p0 = 5;
|
|
|
|
s = ftp[n0].ft_from;
|
|
|
|
s++;
|
|
|
|
while (*s != NUL && word[i+k0] == *s
|
|
|
|
&& !vim_isdigit(*s)
|
|
|
|
&& strchr("(-<^$",*s) == NULL)
|
|
|
|
{
|
|
|
|
k0++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (*s == '(')
|
|
|
|
{
|
|
|
|
/* check alternate letters in "(..)" */
|
|
|
|
for (t = s + 1; *t != ')' && *t != NUL; ++t)
|
|
|
|
if (*t == word[i + k0])
|
|
|
|
{
|
|
|
|
/* match */
|
|
|
|
++k0;
|
|
|
|
for (s = t + 1; *s != NUL; ++s)
|
|
|
|
if (*s == ')')
|
|
|
|
{
|
|
|
|
++s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*s == '-')
|
|
|
|
{
|
|
|
|
/* "k0" gets NOT reduced */
|
|
|
|
/* because "if (k0 == k)" */
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (*s == '<')
|
|
|
|
s++;
|
|
|
|
if (vim_isdigit(*s))
|
|
|
|
{
|
|
|
|
p0 = *s - '0';
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s == NUL
|
|
|
|
/* *s == '^' cuts */
|
|
|
|
|| (*s == '$'
|
|
|
|
&& !SPELL_ISWORDP(word + i + k0)))
|
|
|
|
{
|
|
|
|
if (k0 == k)
|
|
|
|
{
|
|
|
|
/* this is just a piece of the string */
|
|
|
|
++n0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p0 < pri)
|
|
|
|
{
|
|
|
|
/* priority too low */
|
|
|
|
++n0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* rule fits; stop search */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++n0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p0 >= pri && ftp[n0].ft_from[0] == c0)
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* replace string */
|
|
|
|
s = ftp[n].ft_to;
|
|
|
|
p0 = (ftp[n].ft_from[0] != NUL
|
|
|
|
&& vim_strchr(ftp[n].ft_from + 1,
|
|
|
|
'<') != NULL) ? 1 : 0;
|
|
|
|
if (p0 == 1 && z == 0)
|
|
|
|
{
|
|
|
|
/* rule with '<' is used */
|
|
|
|
if (j > 0 && *s != NUL
|
|
|
|
&& (res[j - 1] == c || res[j - 1] == *s))
|
|
|
|
j--;
|
|
|
|
z0 = 1;
|
|
|
|
z = 1;
|
|
|
|
k0 = 0;
|
|
|
|
while (*s != NUL && word[i+k0] != NUL)
|
|
|
|
{
|
|
|
|
word[i + k0] = *s;
|
|
|
|
k0++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (k > k0)
|
|
|
|
mch_memmove(word + i + k0, word + i + k,
|
|
|
|
STRLEN(word + i + k) + 1);
|
|
|
|
|
|
|
|
/* new "actual letter" */
|
|
|
|
c = word[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no '<' rule used */
|
|
|
|
i += k - 1;
|
|
|
|
z = 0;
|
|
|
|
while (*s != NUL && s[1] != NUL && j < MAXWLEN)
|
|
|
|
{
|
|
|
|
if (j == 0 || res[j - 1] != *s)
|
|
|
|
{
|
|
|
|
res[j] = *s;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
/* new "actual letter" */
|
|
|
|
c = *s;
|
|
|
|
if (ftp[n].ft_from[0] != NUL
|
|
|
|
&& strstr((char *)ftp[n].ft_from + 1,
|
|
|
|
"^^") != NULL)
|
|
|
|
{
|
|
|
|
if (c != NUL)
|
|
|
|
{
|
|
|
|
res[j] = c;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
mch_memmove(word, word + i + 1,
|
|
|
|
STRLEN(word + i + 1) + 1);
|
|
|
|
i = 0;
|
|
|
|
z0 = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
2005-06-14 22:01:04 +00:00
|
|
|
else if (vim_iswhite(c))
|
|
|
|
{
|
|
|
|
c = ' ';
|
|
|
|
k = 1;
|
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
if (z0 == 0)
|
|
|
|
{
|
|
|
|
if (k && !p0 && j < MAXWLEN && c != NUL
|
|
|
|
&& (!slang->sl_collapse || j == 0 || res[j - 1] != c))
|
|
|
|
{
|
|
|
|
/* condense only double letters */
|
|
|
|
res[j] = c;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
z = 0;
|
|
|
|
k = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res[j] = NUL;
|
|
|
|
}
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
#if defined(RESCORE) || defined(SOUNDFOLD_SCORE)
|
|
|
|
/*
|
|
|
|
* Return the score for how much words sound different.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spell_sound_score(slang, goodword, badsound)
|
|
|
|
slang_T *slang;
|
|
|
|
char_u *goodword; /* good word */
|
|
|
|
char_u *badsound; /* sound-folded bad word */
|
|
|
|
{
|
|
|
|
char_u fword[MAXWLEN];
|
|
|
|
char_u goodsound[MAXWLEN];
|
|
|
|
int score;
|
|
|
|
|
|
|
|
/* Case-fold the word, needed for sound folding. */
|
|
|
|
(void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN);
|
|
|
|
|
|
|
|
/* sound-fold the good word */
|
|
|
|
spell_soundfold(slang, fword, goodsound);
|
|
|
|
|
|
|
|
/* compute the edit distance-score of the sounds */
|
|
|
|
score = spell_edit_score(badsound, goodsound);
|
|
|
|
|
|
|
|
/* Correction: adding/inserting "*" at the start (word starts with vowel)
|
|
|
|
* shouldn't be counted so much, vowels halfway the word aren't counted at
|
|
|
|
* all. */
|
|
|
|
if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*'))
|
|
|
|
score -= SCORE_DEL / 2;
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-06-13 22:28:56 +00:00
|
|
|
/*
|
|
|
|
* Compute the "edit distance" to turn "badword" into "goodword". The less
|
|
|
|
* deletes/inserts/swaps are required the lower the score.
|
2005-06-14 22:01:04 +00:00
|
|
|
*
|
2005-06-13 22:28:56 +00:00
|
|
|
* The algorithm comes from Aspell editdist.cpp, edit_distance().
|
2005-06-14 22:01:04 +00:00
|
|
|
* It has been converted from C++ to C and modified to support multi-byte
|
|
|
|
* characters.
|
2005-06-13 22:28:56 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
spell_edit_score(badword, goodword)
|
|
|
|
char_u *badword;
|
|
|
|
char_u *goodword;
|
|
|
|
{
|
|
|
|
int *cnt;
|
|
|
|
int badlen, goodlen;
|
|
|
|
int j, i;
|
|
|
|
int t;
|
|
|
|
int bc, gc;
|
2005-06-14 22:01:04 +00:00
|
|
|
int pbc, pgc;
|
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
char_u *p;
|
|
|
|
int wbadword[MAXWLEN];
|
|
|
|
int wgoodword[MAXWLEN];
|
|
|
|
|
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
/* Get the characters from the multi-byte strings and put them in an
|
|
|
|
* int array for easy access. */
|
|
|
|
for (p = badword, badlen = 0; *p != NUL; )
|
|
|
|
wbadword[badlen++] = mb_ptr2char_adv(&p);
|
|
|
|
++badlen;
|
|
|
|
for (p = goodword, goodlen = 0; *p != NUL; )
|
|
|
|
wgoodword[goodlen++] = mb_ptr2char_adv(&p);
|
|
|
|
++goodlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
badlen = STRLEN(badword) + 1;
|
|
|
|
goodlen = STRLEN(goodword) + 1;
|
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
/* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
|
|
|
|
#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
|
|
|
|
cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
|
|
|
|
TRUE);
|
2005-06-14 22:01:04 +00:00
|
|
|
if (cnt == NULL)
|
|
|
|
return 0; /* out of memory */
|
2005-06-13 22:28:56 +00:00
|
|
|
|
|
|
|
CNT(0, 0) = 0;
|
|
|
|
for (j = 1; j <= goodlen; ++j)
|
|
|
|
CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
|
|
|
|
|
|
|
|
for (i = 1; i <= badlen; ++i)
|
|
|
|
{
|
|
|
|
CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
|
|
|
|
for (j = 1; j <= goodlen; ++j)
|
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
bc = wbadword[i - 1];
|
|
|
|
gc = wgoodword[j - 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
bc = badword[i - 1];
|
|
|
|
gc = goodword[j - 1];
|
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
if (bc == gc)
|
|
|
|
CNT(i, j) = CNT(i - 1, j - 1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use a better score when there is only a case difference. */
|
2005-06-14 22:01:04 +00:00
|
|
|
if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
|
2005-06-13 22:28:56 +00:00
|
|
|
CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
|
|
|
|
else
|
|
|
|
CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
|
|
|
|
|
2005-06-14 22:01:04 +00:00
|
|
|
if (i > 1 && j > 1)
|
2005-06-13 22:28:56 +00:00
|
|
|
{
|
2005-06-14 22:01:04 +00:00
|
|
|
#ifdef FEAT_MBYTE
|
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
pbc = wbadword[i - 2];
|
|
|
|
pgc = wgoodword[j - 2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
pbc = badword[i - 2];
|
|
|
|
pgc = goodword[j - 2];
|
|
|
|
}
|
|
|
|
if (bc == pgc && pbc == gc)
|
|
|
|
{
|
|
|
|
t = SCORE_SWAP + CNT(i - 2, j - 2);
|
|
|
|
if (t < CNT(i, j))
|
|
|
|
CNT(i, j) = t;
|
|
|
|
}
|
2005-06-13 22:28:56 +00:00
|
|
|
}
|
|
|
|
t = SCORE_DEL + CNT(i - 1, j);
|
|
|
|
if (t < CNT(i, j))
|
|
|
|
CNT(i, j) = t;
|
|
|
|
t = SCORE_INS + CNT(i, j - 1);
|
|
|
|
if (t < CNT(i, j))
|
|
|
|
CNT(i, j) = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CNT(badlen - 1, goodlen - 1);
|
|
|
|
}
|
2005-06-06 21:50:35 +00:00
|
|
|
|
2005-04-17 20:18:43 +00:00
|
|
|
#endif /* FEAT_SYN_HL */
|