#if 0
# define SPELL_PRINTTREE
#endif
#if 0
# define DEBUG_TRIEWALK
#endif
#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
# include "vimio.h"
#endif
#include "vim.h"
#if defined(FEAT_SPELL) || defined(PROTO)
#ifndef UNIX
# include <time.h>
#endif
#define MAXWLEN 250
#if SIZEOF_INT > 3
typedef int idx_T;
#else
typedef long idx_T;
#endif
#define WF_REGION 0x01
#define WF_ONECAP 0x02
#define WF_ALLCAP 0x04
#define WF_RARE 0x08
#define WF_BANNED 0x10
#define WF_AFX 0x20
#define WF_FIXCAP 0x40
#define WF_KEEPCAP 0x80
#define WF_HAS_AFF 0x0100
#define WF_NEEDCOMP 0x0200
#define WF_NOSUGGEST 0x0400
#define WF_COMPROOT 0x0800
#define WF_NOCOMPBEF 0x1000
#define WF_NOCOMPAFT 0x2000
#define WF_MIXCAP 0x20
#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
#define WFP_RARE 0x01
#define WFP_NC 0x02
#define WFP_UP 0x04
#define WFP_COMPPERMIT 0x08
#define WFP_COMPFORBID 0x10
#define WF_RAREPFX (WFP_RARE << 24)
#define WF_PFX_NC (WFP_NC << 24)
#define WF_PFX_UP (WFP_UP << 24)
#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24)
#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24)
#define COMP_CHECKDUP 1
#define COMP_CHECKREP 2
#define COMP_CHECKCASE 4
#define COMP_CHECKTRIPLE 8
#define BY_NOFLAGS 0
#define BY_INDEX 1
#define BY_FLAGS 2
#define BY_FLAGS2 3
#define BY_SPECIAL BY_FLAGS2
typedef struct fromto_S
{
char_u *ft_from;
char_u *ft_to;
} fromto_T;
typedef struct salitem_S
{
char_u *sm_lead;
int sm_leadlen;
char_u *sm_oneof;
char_u *sm_rules;
char_u *sm_to;
#ifdef FEAT_MBYTE
int *sm_lead_w;
int *sm_oneof_w;
int *sm_to_w;
#endif
} salitem_T;
#ifdef FEAT_MBYTE
typedef int salfirst_T;
#else
typedef short salfirst_T;
#endif
#define SP_TRUNCERROR -1
#define SP_FORMERROR -2
#define SP_OTHERERROR -3
typedef struct slang_S slang_T;
struct slang_S
{
slang_T *sl_next;
char_u *sl_name;
char_u *sl_fname;
int sl_add;
char_u *sl_fbyts;
idx_T *sl_fidxs;
char_u *sl_kbyts;
idx_T *sl_kidxs;
char_u *sl_pbyts;
idx_T *sl_pidxs;
char_u *sl_info;
char_u sl_regions[17];
char_u *sl_midword;
hashtab_T sl_wordcount;
int sl_compmax;
int sl_compminlen;
int sl_compsylmax;
int sl_compoptions;
garray_T sl_comppat;
regprog_T *sl_compprog;
char_u *sl_comprules;
char_u *sl_compstartflags;
char_u *sl_compallflags;
char_u sl_nobreak;
char_u *sl_syllable;
garray_T sl_syl_items;
int sl_prefixcnt;
regprog_T **sl_prefprog;
garray_T sl_rep;
short sl_rep_first[256];
garray_T sl_sal;
salfirst_T sl_sal_first[256];
int sl_followup;
int sl_collapse;
int sl_rem_accents;
int sl_sofo;
garray_T sl_repsal;
short sl_repsal_first[256];
int sl_nosplitsugs;
time_t sl_sugtime;
char_u *sl_sbyts;
idx_T *sl_sidxs;
buf_T *sl_sugbuf;
int sl_sugloaded;
int sl_has_map;
#ifdef FEAT_MBYTE
hashtab_T sl_map_hash;
int sl_map_array[256];
#else
char_u sl_map_array[256];
#endif
hashtab_T sl_sounddone;
};
static slang_T *first_lang = NULL;
#define SAL_F0LLOWUP 1
#define SAL_COLLAPSE 2
#define SAL_REM_ACCENTS 4
typedef struct langp_S
{
slang_T *lp_slang;
slang_T *lp_sallang;
slang_T *lp_replang;
int lp_region;
} langp_T;
#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
#define REGION_ALL 0xff
#define VIMSPELLMAGIC "VIMspell"
#define VIMSPELLMAGICL 8
#define VIMSPELLVERSION 50
#define VIMSUGMAGIC "VIMsug"
#define VIMSUGMAGICL 6
#define VIMSUGVERSION 1
#define SN_REGION 0
#define SN_CHARFLAGS 1
#define SN_MIDWORD 2
#define SN_PREFCOND 3
#define SN_REP 4
#define SN_SAL 5
#define SN_SOFO 6
#define SN_MAP 7
#define SN_COMPOUND 8
#define SN_SYLLABLE 9
#define SN_NOBREAK 10
#define SN_SUGFILE 11
#define SN_REPSAL 12
#define SN_WORDS 13
#define SN_NOSPLITSUGS 14
#define SN_INFO 15
#define SN_END 255
#define SNF_REQUIRED 1
#define SP_BANNED -1
#define SP_OK 0
#define SP_RARE 1
#define SP_LOCAL 2
#define SP_BAD 3
static char_u *int_wordlist = NULL;
typedef struct wordcount_S
{
short_u wc_count;
char_u wc_word[1];
} wordcount_T;
static wordcount_T dumwc;
#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
#define MAXWORDCOUNT 0xffff
typedef struct suginfo_S
{
garray_T su_ga;
int su_maxcount;
int su_maxscore;
int su_sfmaxscore;
garray_T su_sga;
char_u *su_badptr;
int su_badlen;
int su_badflags;
char_u su_badword[MAXWLEN];
char_u su_fbadword[MAXWLEN];
char_u su_sal_badword[MAXWLEN];
hashtab_T su_banned;
slang_T *su_sallang;
} suginfo_T;
typedef struct suggest_S
{
char_u *st_word;
int st_wordlen;
int st_orglen;
int st_score;
int st_altscore;
int st_salscore;
int st_had_bonus;
slang_T *st_slang;
} suggest_T;
#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
#define SCORE_SPLIT 149
#define SCORE_SPLIT_NO 249
#define SCORE_ICASE 52
#define SCORE_REGION 200
#define SCORE_RARE 180
#define SCORE_SWAP 75
#define SCORE_SWAP3 110
#define SCORE_REP 65
#define SCORE_SUBST 93
#define SCORE_SIMILAR 33
#define SCORE_SUBCOMP 33
#define SCORE_DEL 94
#define SCORE_DELDUP 66
#define SCORE_DELCOMP 28
#define SCORE_INS 96
#define SCORE_INSDUP 67
#define SCORE_INSCOMP 30
#define SCORE_NONWORD 103
#define SCORE_FILE 30
#define SCORE_MAXINIT 350
#define SCORE_COMMON1 30
#define SCORE_COMMON2 40
#define SCORE_COMMON3 50
#define SCORE_THRES2 10
#define SCORE_THRES3 100
#define SCORE_SFMAX1 200
#define SCORE_SFMAX2 300
#define SCORE_SFMAX3 400
#define SCORE_BIG SCORE_INS * 3
#define SCORE_MAXMAX 999999
#define SCORE_LIMITMAX 350
#define SCORE_EDIT_MIN SCORE_SIMILAR
typedef struct matchinf_S
{
langp_T *mi_lp;
char_u *mi_word;
char_u *mi_end;
char_u *mi_fend;
char_u *mi_cend;
char_u mi_fword[MAXWLEN + 1];
int mi_fwordlen;
int mi_prefarridx;
int mi_prefcnt;
int mi_prefixlen;
#ifdef FEAT_MBYTE
int mi_cprefixlen;
#else
# define mi_cprefixlen mi_prefixlen
#endif
int mi_compoff;
char_u mi_compflags[MAXWLEN];
int mi_complen;
int mi_compextra;
int mi_result;
int mi_capflags;
win_T *mi_win;
int mi_result2;
char_u *mi_end2;
} matchinf_T;
typedef struct spelltab_S
{
char_u st_isw[256];
char_u st_isu[256];
char_u st_fold[256];
char_u st_upper[256];
} spelltab_T;
static spelltab_T spelltab;
static int did_set_spelltab;
#define CF_WORD 0x01
#define CF_UPPER 0x02
static void clear_spell_chartab __ARGS((spelltab_T *sp));
static int set_spell_finish __ARGS((spelltab_T *new_st));
static int spell_iswordp __ARGS((char_u *p, win_T *wp));
static int spell_iswordp_nmw __ARGS((char_u *p));
#ifdef FEAT_MBYTE
static int spell_mb_isword_class __ARGS((int cl));
static int spell_iswordp_w __ARGS((int *p, win_T *wp));
#endif
static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
typedef enum
{
STATE_START = 0,
STATE_NOPREFIX,
STATE_SPLITUNDO,
STATE_ENDNUL,
STATE_PLAIN,
STATE_DEL,
STATE_INS_PREP,
STATE_INS,
STATE_SWAP,
STATE_UNSWAP,
STATE_SWAP3,
STATE_UNSWAP3,
STATE_UNROT3L,
STATE_UNROT3R,
STATE_REP_INI,
STATE_REP,
STATE_REP_UNDO,
STATE_FINAL
} state_T;
typedef struct trystate_S
{
state_T ts_state;
int ts_score;
idx_T ts_arridx;
short ts_curi;
char_u ts_fidx;
char_u ts_fidxtry;
char_u ts_twordlen;
char_u ts_prefixdepth;
char_u ts_flags;
#ifdef FEAT_MBYTE
char_u ts_tcharlen;
char_u ts_tcharidx;
char_u ts_isdiff;
char_u ts_fcharstart;
#endif
char_u ts_prewordlen;
char_u ts_splitoff;
char_u ts_splitfidx;
char_u ts_complen;
char_u ts_compsplit;
char_u ts_save_badflags;
char_u ts_delidx;
} trystate_T;
#define DIFF_NONE 0
#define DIFF_YES 1
#define DIFF_INSERT 2
#define TSF_PREFIXOK 1
#define TSF_DIDSPLIT 2
#define TSF_DIDDEL 4
#define PFD_NOPREFIX 0xff
#define PFD_PREFIXTREE 0xfe
#define PFD_NOTSPECIAL 0xfd
#define FIND_FOLDWORD 0
#define FIND_KEEPWORD 1
#define FIND_PREFIX 2
#define FIND_COMPOUND 3
#define FIND_KEEPCOMPOUND 4
static slang_T *slang_alloc __ARGS((char_u *lang));
static void slang_free __ARGS((slang_T *lp));
static void slang_clear __ARGS((slang_T *lp));
static void slang_clear_sug __ARGS((slang_T *lp));
static void find_word __ARGS((matchinf_T *mip, int mode));
static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
static void find_prefix __ARGS((matchinf_T *mip, int mode));
static int fold_more __ARGS((matchinf_T *mip));
static int spell_valid_case __ARGS((int wordflags, int treeflags));
static int no_spell_checking __ARGS((win_T *wp));
static void spell_load_lang __ARGS((char_u *lang));
static char_u *spell_enc __ARGS((void));
static void int_wordlist_spl __ARGS((char_u *fname));
static void spell_load_cb __ARGS((char_u *fname, void *cookie));
static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
static int read_charflags_section __ARGS((FILE *fd));
static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
static int byte_in_str __ARGS((char_u *str, int byte));
static int init_syl_tab __ARGS((slang_T *slang));
static int count_syllables __ARGS((slang_T *slang, char_u *word));
static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
static void set_sal_first __ARGS((slang_T *lp));
#ifdef FEAT_MBYTE
static int *mb_str2wide __ARGS((char_u *s));
#endif
static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
static void clear_midword __ARGS((win_T *buf));
static void use_midword __ARGS((slang_T *lp, win_T *buf));
static int find_region __ARGS((char_u *rp, char_u *region));
static int captype __ARGS((char_u *word, char_u *end));
static int badword_captype __ARGS((char_u *word, char_u *end));
static void spell_reload_one __ARGS((char_u *fname, int added_word));
static void 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 int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
#ifdef FEAT_EVAL
static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
#endif
static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
static void suggest_load_files __ARGS((void));
static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
static void spell_find_cleanup __ARGS((suginfo_T *su));
static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
static void suggest_try_special __ARGS((suginfo_T *su));
static void suggest_try_change __ARGS((suginfo_T *su));
static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
#ifdef FEAT_MBYTE
static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
#endif
static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
static void score_comp_sal __ARGS((suginfo_T *su));
static void score_combine __ARGS((suginfo_T *su));
static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
static void suggest_try_soundalike_prep __ARGS((void));
static void suggest_try_soundalike __ARGS((suginfo_T *su));
static void suggest_try_soundalike_finish __ARGS((void));
static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
static int soundfold_find __ARGS((slang_T *slang, char_u *word));
static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
static void set_map_str __ARGS((slang_T *lp, char_u *map));
static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
static void add_banned __ARGS((suginfo_T *su, char_u *word));
static void rescore_suggestions __ARGS((suginfo_T *su));
static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
#ifdef FEAT_MBYTE
static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
#endif
static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
#ifdef FEAT_MBYTE
static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
#endif
static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
static buf_T *open_spellbuf __ARGS((void));
static void close_spellbuf __ARGS((buf_T *buf));
#ifndef FEAT_MBYTE
# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
#else
# if defined(HAVE_WCHAR_H)
# include <wchar.h>
# endif
# ifdef HAVE_TOWLOWER
# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
: (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
# else
# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
: (c) < 256 ? (int)spelltab.st_fold[c] : (c))
# endif
# ifdef HAVE_TOWUPPER
# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
: (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
# else
# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
: (c) < 256 ? (int)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] : (FALSE))
# endif
#endif
static char *e_format = N_("E759: Format error in spell file");
static char *e_spell_trunc = N_("E758: Truncated spell file");
static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
static char *e_affname = N_("Affix name too long in %s line %d: %s");
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");
static char *msg_compressing = N_("Compressing word tree...");
static char_u *repl_from = NULL;
static char_u *repl_to = NULL;
int
spell_check(wp, ptr, attrp, capcol, docount)
win_T *wp;
char_u *ptr;
hlf_T *attrp;
int *capcol;
int docount;
{
matchinf_T mi;
int nrlen = 0;
int c;
int wrongcaplen = 0;
int lpi;
int count_word = docount;
if (*ptr <= ' ')
return 1;
if (wp->w_s->b_langp.ga_len == 0)
return 1;
vim_memset(&mi, 0, sizeof(matchinf_T));
if (*ptr >= '0' && *ptr <= '9')
{
if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
mi.mi_end = skiphex(ptr + 2);
else
mi.mi_end = skipdigits(ptr);
nrlen = (int)(mi.mi_end - ptr);
}
mi.mi_word = ptr;
mi.mi_fend = ptr;
if (spell_iswordp(mi.mi_fend, wp))
{
do
{
mb_ptr_adv(mi.mi_fend);
} while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
{
c = PTR2CHAR(ptr);
if (!SPELL_ISUPPER(c))
wrongcaplen = (int)(mi.mi_fend - ptr);
}
}
if (capcol != NULL)
*capcol = -1;
mi.mi_end = mi.mi_fend;
mi.mi_capflags = 0;
mi.mi_cend = NULL;
mi.mi_win = wp;
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 = (int)STRLEN(mi.mi_fword);
mi.mi_result = SP_BAD;
mi.mi_result2 = SP_BAD;
for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
{
mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
continue;
find_word(&mi, FIND_FOLDWORD);
find_word(&mi, FIND_KEEPWORD);
find_prefix(&mi, FIND_FOLDWORD);
if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
&& mi.mi_result2 != SP_BAD)
{
mi.mi_result = mi.mi_result2;
mi.mi_end = mi.mi_end2;
}
if (count_word && mi.mi_result == SP_OK)
{
count_common_word(mi.mi_lp->lp_slang, ptr,
(int)(mi.mi_end - ptr), 1);
count_word = FALSE;
}
}
if (mi.mi_result != SP_OK)
{
if (nrlen > 0)
{
if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
return nrlen;
}
else if (!spell_iswordp_nmw(ptr))
{
if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
{
regmatch_T regmatch;
regmatch.regprog = wp->w_s->b_cap_prog;
regmatch.rm_ic = FALSE;
if (vim_regexec(®match, ptr, 0))
*capcol = (int)(regmatch.endp[0] - ptr);
}
#ifdef FEAT_MBYTE
if (has_mbyte)
return (*mb_ptr2len)(ptr);
#endif
return 1;
}
else if (mi.mi_end == ptr)
mb_ptr_adv(mi.mi_end);
else if (mi.mi_result == SP_BAD
&& LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
{
char_u *p, *fp;
int save_result = mi.mi_result;
mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
{
p = mi.mi_word;
fp = mi.mi_fword;
for (;;)
{
mb_ptr_adv(p);
mb_ptr_adv(fp);
if (p >= mi.mi_end)
break;
mi.mi_compoff = (int)(fp - mi.mi_fword);
find_word(&mi, FIND_COMPOUND);
if (mi.mi_result != SP_BAD)
{
mi.mi_end = p;
break;
}
}
mi.mi_result = save_result;
}
}
if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
*attrp = HLF_SPB;
else if (mi.mi_result == SP_RARE)
*attrp = HLF_SPR;
else
*attrp = HLF_SPL;
}
if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
{
*attrp = HLF_SPC;
return wrongcaplen;
}
return (int)(mi.mi_end - ptr);
}
static void
find_word(mip, mode)
matchinf_T *mip;
int mode;
{
idx_T arridx = 0;
int endlen[MAXWLEN];
idx_T endidx[MAXWLEN];
int endidxcnt = 0;
int len;
int wlen = 0;
int flen;
int c;
char_u *ptr;
idx_T lo, hi, m;
#ifdef FEAT_MBYTE
char_u *s;
#endif
char_u *p;
int res = SP_BAD;
slang_T *slang = mip->mi_lp->lp_slang;
unsigned flags;
char_u *byts;
idx_T *idxs;
int word_ends;
int prefix_found;
int nobreak_result;
if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
{
ptr = mip->mi_word;
flen = 9999;
byts = slang->sl_kbyts;
idxs = slang->sl_kidxs;
if (mode == FIND_KEEPCOMPOUND)
wlen += mip->mi_compoff;
}
else
{
ptr = mip->mi_fword;
flen = mip->mi_fwordlen;
byts = slang->sl_fbyts;
idxs = slang->sl_fidxs;
if (mode == FIND_PREFIX)
{
wlen = mip->mi_prefixlen;
flen -= mip->mi_prefixlen;
}
else if (mode == FIND_COMPOUND)
{
wlen = mip->mi_compoff;
flen -= mip->mi_compoff;
}
}
if (byts == NULL)
return;
for (;;)
{
if (flen <= 0 && *mip->mi_fend != NUL)
flen = fold_more(mip);
len = byts[arridx++];
if (byts[arridx] == 0)
{
if (endidxcnt == MAXWLEN)
{
EMSG(_(e_format));
return;
}
endlen[endidxcnt] = wlen;
endidx[endidxcnt++] = arridx++;
--len;
while (len > 0 && byts[arridx] == 0)
{
++arridx;
--len;
}
if (len == 0)
break;
}
if (ptr[wlen] == NUL)
break;
c = ptr[wlen];
if (c == TAB)
c = ' ';
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;
else
{
lo = hi = m;
break;
}
}
if (hi < lo || byts[lo] != c)
break;
arridx = idxs[lo];
++wlen;
--flen;
if (c == ' ')
{
for (;;)
{
if (flen <= 0 && *mip->mi_fend != NUL)
flen = fold_more(mip);
if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
break;
++wlen;
--flen;
}
}
}
while (endidxcnt > 0)
{
--endidxcnt;
arridx = endidx[endidxcnt];
wlen = endlen[endidxcnt];
#ifdef FEAT_MBYTE
if ((*mb_head_off)(ptr, ptr + wlen) > 0)
continue;
#endif
if (spell_iswordp(ptr + wlen, mip->mi_win))
{
if (slang->sl_compprog == NULL && !slang->sl_nobreak)
continue;
word_ends = FALSE;
}
else
word_ends = TRUE;
prefix_found = FALSE;
#ifdef FEAT_MBYTE
if (mode != FIND_KEEPWORD && has_mbyte)
{
p = mip->mi_word;
if (STRNCMP(ptr, p, wlen) != 0)
{
for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
mb_ptr_adv(p);
wlen = (int)(p - mip->mi_word);
}
}
#endif
res = SP_BAD;
for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
--len, ++arridx)
{
flags = idxs[arridx];
if (mode == FIND_FOLDWORD)
{
if (mip->mi_cend != mip->mi_word + wlen)
{
mip->mi_cend = mip->mi_word + wlen;
mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
}
if (mip->mi_capflags == WF_KEEPCAP
|| !spell_valid_case(mip->mi_capflags, flags))
continue;
}
else if (mode == FIND_PREFIX && !prefix_found)
{
c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
flags,
mip->mi_word + mip->mi_cprefixlen, slang,
FALSE);
if (c == 0)
continue;
if (c & WF_RAREPFX)
flags |= WF_RARE;
prefix_found = TRUE;
}
if (slang->sl_nobreak)
{
if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
&& (flags & WF_BANNED) == 0)
{
mip->mi_result = SP_OK;
break;
}
}
else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
|| !word_ends))
{
if (((unsigned)flags >> 24) == 0
|| wlen - mip->mi_compoff < slang->sl_compminlen)
continue;
#ifdef FEAT_MBYTE
if (has_mbyte
&& slang->sl_compminlen > 0
&& mb_charlen_len(mip->mi_word + mip->mi_compoff,
wlen - mip->mi_compoff) < slang->sl_compminlen)
continue;
#endif
if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
> slang->sl_compmax
&& slang->sl_compsylmax == MAXWLEN)
continue;
if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
continue;
if (!word_ends && (flags & WF_NOCOMPAFT))
continue;
if (!byte_in_str(mip->mi_complen == 0
? slang->sl_compstartflags
: slang->sl_compallflags,
((unsigned)flags >> 24)))
continue;
if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
continue;
if (mode == FIND_COMPOUND)
{
int capflags;
#ifdef FEAT_MBYTE
if (has_mbyte && STRNCMP(ptr, mip->mi_word,
mip->mi_compoff) != 0)
{
p = mip->mi_word;
for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
mb_ptr_adv(p);
}
else
#endif
p = mip->mi_word + mip->mi_compoff;
capflags = captype(p, mip->mi_word + wlen);
if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
&& (flags & WF_FIXCAP) != 0))
continue;
if (capflags != WF_ALLCAP)
{
mb_ptr_back(mip->mi_word, p);
if (spell_iswordp_nmw(p)
? capflags == WF_ONECAP
: (flags & WF_ONECAP) != 0
&& capflags != WF_ONECAP)
continue;
}
}
mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
mip->mi_compflags[mip->mi_complen + 1] = NUL;
if (word_ends)
{
char_u fword[MAXWLEN];
if (slang->sl_compsylmax < MAXWLEN)
{
if (ptr == mip->mi_word)
(void)spell_casefold(ptr, wlen, fword, MAXWLEN);
else
vim_strncpy(fword, ptr, endlen[endidxcnt]);
}
if (!can_compound(slang, fword, mip->mi_compflags))
continue;
}
else if (slang->sl_comprules != NULL
&& !match_compoundrule(slang, mip->mi_compflags))
continue;
}
else if (flags & WF_NEEDCOMP)
continue;
nobreak_result = SP_OK;
if (!word_ends)
{
int save_result = mip->mi_result;
char_u *save_end = mip->mi_end;
langp_T *save_lp = mip->mi_lp;
int lpi;
if (slang->sl_nobreak)
mip->mi_result = SP_BAD;
mip->mi_compoff = endlen[endidxcnt];
#ifdef FEAT_MBYTE
if (has_mbyte && mode == FIND_KEEPWORD)
{
p = mip->mi_fword;
if (STRNCMP(ptr, p, wlen) != 0)
{
for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
mb_ptr_adv(p);
mip->mi_compoff = (int)(p - mip->mi_fword);
}
}
#endif
c = mip->mi_compoff;
++mip->mi_complen;
if (flags & WF_COMPROOT)
++mip->mi_compextra;
for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
{
if (slang->sl_nobreak)
{
mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
if (mip->mi_lp->lp_slang->sl_fidxs == NULL
|| !mip->mi_lp->lp_slang->sl_nobreak)
continue;
}
find_word(mip, FIND_COMPOUND);
if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
{
mip->mi_compoff = wlen;
find_word(mip, FIND_KEEPCOMPOUND);
#if 0
if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
{
mip->mi_compoff = c;
find_prefix(mip, FIND_COMPOUND);
}
#endif
}
if (!slang->sl_nobreak)
break;
}
--mip->mi_complen;
if (flags & WF_COMPROOT)
--mip->mi_compextra;
mip->mi_lp = save_lp;
if (slang->sl_nobreak)
{
nobreak_result = mip->mi_result;
mip->mi_result = save_result;
mip->mi_end = save_end;
}
else
{
if (mip->mi_result == SP_OK)
break;
continue;
}
}
if (flags & WF_BANNED)
res = SP_BANNED;
else if (flags & WF_REGION)
{
if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
res = SP_OK;
else
res = SP_LOCAL;
}
else if (flags & WF_RARE)
res = SP_RARE;
else
res = SP_OK;
if (nobreak_result == SP_BAD)
{
if (mip->mi_result2 > res)
{
mip->mi_result2 = res;
mip->mi_end2 = mip->mi_word + wlen;
}
else if (mip->mi_result2 == res
&& mip->mi_end2 < mip->mi_word + wlen)
mip->mi_end2 = mip->mi_word + wlen;
}
else 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 (mip->mi_result == SP_OK)
break;
}
if (mip->mi_result == SP_OK)
break;
}
}
static int
match_checkcompoundpattern(ptr, wlen, gap)
char_u *ptr;
int wlen;
garray_T *gap;
{
int i;
char_u *p;
int len;
for (i = 0; i + 1 < gap->ga_len; i += 2)
{
p = ((char_u **)gap->ga_data)[i + 1];
if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
{
p = ((char_u **)gap->ga_data)[i];
len = (int)STRLEN(p);
if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
return TRUE;
}
}
return FALSE;
}
static int
can_compound(slang, word, flags)
slang_T *slang;
char_u *word;
char_u *flags;
{
regmatch_T regmatch;
#ifdef FEAT_MBYTE
char_u uflags[MAXWLEN * 2];
int i;
#endif
char_u *p;
if (slang->sl_compprog == NULL)
return FALSE;
#ifdef FEAT_MBYTE
if (enc_utf8)
{
p = uflags;
for (i = 0; flags[i] != NUL; ++i)
p += mb_char2bytes(flags[i], p);
*p = NUL;
p = uflags;
}
else
#endif
p = flags;
regmatch.regprog = slang->sl_compprog;
regmatch.rm_ic = FALSE;
if (!vim_regexec(®match, p, 0))
return FALSE;
if (slang->sl_compsylmax < MAXWLEN
&& count_syllables(slang, word) > slang->sl_compsylmax)
return (int)STRLEN(flags) < slang->sl_compmax;
return TRUE;
}
static int
can_be_compound(sp, slang, compflags, flag)
trystate_T *sp;
slang_T *slang;
char_u *compflags;
int flag;
{
if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
? slang->sl_compstartflags : slang->sl_compallflags, flag))
return FALSE;
if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
{
int v;
compflags[sp->ts_complen] = flag;
compflags[sp->ts_complen + 1] = NUL;
v = match_compoundrule(slang, compflags + sp->ts_compsplit);
compflags[sp->ts_complen] = NUL;
return v;
}
return TRUE;
}
static int
match_compoundrule(slang, compflags)
slang_T *slang;
char_u *compflags;
{
char_u *p;
int i;
int c;
for (p = slang->sl_comprules; *p != NUL; ++p)
{
for (i = 0; ; ++i)
{
c = compflags[i];
if (c == NUL)
return TRUE;
if (*p == '/' || *p == NUL)
break;
if (*p == '[')
{
int match = FALSE;
++p;
while (*p != ']' && *p != NUL)
if (*p++ == c)
match = TRUE;
if (!match)
break;
}
else if (*p != c)
break;
++p;
}
p = vim_strchr(p, '/');
if (p == NULL)
break;
}
return FALSE;
}
static int
valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
int totprefcnt;
int arridx;
int flags;
char_u *word;
slang_T *slang;
int cond_req;
{
int prefcnt;
int pidx;
regprog_T *rp;
regmatch_T regmatch;
int prefid;
prefid = (unsigned)flags >> 24;
for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
{
pidx = slang->sl_pidxs[arridx + prefcnt];
if (prefid != (pidx & 0xff))
continue;
if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
continue;
rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
if (rp != NULL)
{
regmatch.regprog = rp;
regmatch.rm_ic = FALSE;
if (!vim_regexec(®match, word, 0))
continue;
}
else if (cond_req)
continue;
return pidx;
}
return 0;
}
static void
find_prefix(mip, mode)
matchinf_T *mip;
int mode;
{
idx_T arridx = 0;
int len;
int wlen = 0;
int flen;
int c;
char_u *ptr;
idx_T lo, hi, m;
slang_T *slang = mip->mi_lp->lp_slang;
char_u *byts;
idx_T *idxs;
byts = slang->sl_pbyts;
if (byts == NULL)
return;
ptr = mip->mi_fword;
flen = mip->mi_fwordlen;
if (mode == FIND_COMPOUND)
{
ptr += mip->mi_compoff;
flen -= mip->mi_compoff;
}
idxs = slang->sl_pidxs;
for (;;)
{
if (flen == 0 && *mip->mi_fend != NUL)
flen = fold_more(mip);
len = byts[arridx++];
if (byts[arridx] == 0)
{
mip->mi_prefarridx = arridx;
mip->mi_prefcnt = len;
while (len > 0 && byts[arridx] == 0)
{
++arridx;
--len;
}
mip->mi_prefcnt -= len;
mip->mi_prefixlen = wlen;
if (mode == FIND_COMPOUND)
mip->mi_prefixlen += mip->mi_compoff;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
mip->mi_cprefixlen = nofold_len(mip->mi_fword,
mip->mi_prefixlen, mip->mi_word);
}
else
mip->mi_cprefixlen = mip->mi_prefixlen;
#endif
find_word(mip, FIND_PREFIX);
if (len == 0)
break;
}
if (ptr[wlen] == NUL)
break;
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;
else
{
lo = hi = m;
break;
}
}
if (hi < lo || byts[lo] != c)
break;
arridx = idxs[lo];
++wlen;
--flen;
}
}
static int
fold_more(mip)
matchinf_T *mip;
{
int flen;
char_u *p;
p = mip->mi_fend;
do
{
mb_ptr_adv(mip->mi_fend);
} while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
if (*mip->mi_fend != NUL)
mb_ptr_adv(mip->mi_fend);
(void)spell_casefold(p, (int)(mip->mi_fend - p),
mip->mi_fword + mip->mi_fwordlen,
MAXWLEN - mip->mi_fwordlen);
flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
mip->mi_fwordlen += flen;
return flen;
}
static int
spell_valid_case(wordflags, treeflags)
int wordflags;
int treeflags;
{
return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
|| ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
&& ((treeflags & WF_ONECAP) == 0
|| (wordflags & WF_ONECAP) != 0)));
}
static int
no_spell_checking(wp)
win_T *wp;
{
if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
|| wp->w_s->b_langp.ga_len == 0)
{
EMSG(_("E756: Spell checking is not enabled"));
return TRUE;
}
return FALSE;
}
int
spell_move_to(wp, dir, allwords, curline, attrp)
win_T *wp;
int dir;
int allwords;
int curline;
hlf_T *attrp;
{
linenr_T lnum;
pos_T found_pos;
int found_len = 0;
char_u *line;
char_u *p;
char_u *endp;
hlf_T attr;
int len;
# ifdef FEAT_SYN_HL
int has_syntax = syntax_present(wp);
# endif
int col;
int can_spell;
char_u *buf = NULL;
int buflen = 0;
int skip = 0;
int capcol = -1;
int found_one = FALSE;
int wrapped = FALSE;
if (no_spell_checking(wp))
return 0;
lnum = wp->w_cursor.lnum;
clearpos(&found_pos);
while (!got_int)
{
line = ml_get_buf(wp->w_buffer, lnum, FALSE);
len = (int)STRLEN(line);
if (buflen < len + MAXWLEN + 2)
{
vim_free(buf);
buflen = len + MAXWLEN + 2;
buf = alloc(buflen);
if (buf == NULL)
break;
}
if (lnum == 1)
capcol = 0;
if (capcol == 0)
capcol = (int)(skipwhite(line) - line);
else if (curline && wp == curwin)
{
col = (int)(skipwhite(line) - line);
if (check_need_cap(lnum, col))
capcol = col;
line = ml_get_buf(wp->w_buffer, lnum, FALSE);
}
STRCPY(buf, line);
if (lnum < wp->w_buffer->b_ml.ml_line_count)
spell_cat_line(buf + STRLEN(buf),
ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
p = buf + skip;
endp = buf + len;
while (p < endp)
{
if (dir == BACKWARD
&& lnum == wp->w_cursor.lnum
&& !wrapped
&& (colnr_T)(p - buf) >= wp->w_cursor.col)
break;
attr = HLF_COUNT;
len = spell_check(wp, p, &attr, &capcol, FALSE);
if (attr != HLF_COUNT)
{
if (allwords || attr == HLF_SPB)
{
if (dir == BACKWARD
|| lnum != wp->w_cursor.lnum
|| (lnum == wp->w_cursor.lnum
&& (wrapped
|| (colnr_T)(curline ? p - buf + len
: p - buf)
> wp->w_cursor.col)))
{
# ifdef FEAT_SYN_HL
if (has_syntax)
{
col = (int)(p - buf);
(void)syn_get_id(wp, lnum, (colnr_T)col,
FALSE, &can_spell, FALSE);
if (!can_spell)
attr = HLF_COUNT;
}
else
#endif
can_spell = TRUE;
if (can_spell)
{
found_one = TRUE;
found_pos.lnum = lnum;
found_pos.col = (int)(p - buf);
#ifdef FEAT_VIRTUALEDIT
found_pos.coladd = 0;
#endif
if (dir == FORWARD)
{
wp->w_cursor = found_pos;
vim_free(buf);
if (attrp != NULL)
*attrp = attr;
return len;
}
else if (curline)
found_pos.col += len;
found_len = len;
}
}
else
found_one = TRUE;
}
}
p += len;
capcol -= len;
}
if (dir == BACKWARD && found_pos.lnum != 0)
{
wp->w_cursor = found_pos;
vim_free(buf);
return found_len;
}
if (curline)
break;
if (dir == BACKWARD)
{
if (lnum == wp->w_cursor.lnum && wrapped)
break;
if (lnum > 1)
--lnum;
else if (!p_ws)
break;
else
{
lnum = wp->w_buffer->b_ml.ml_line_count;
wrapped = TRUE;
if (!shortmess(SHM_SEARCH))
give_warning((char_u *)_(top_bot_msg), TRUE);
}
capcol = -1;
}
else
{
if (lnum < wp->w_buffer->b_ml.ml_line_count)
++lnum;
else if (!p_ws)
break;
else
{
lnum = 1;
wrapped = TRUE;
if (!shortmess(SHM_SEARCH))
give_warning((char_u *)_(bot_top_msg), TRUE);
}
if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
break;
if (attr == HLF_COUNT)
skip = (int)(p - endp);
else
skip = 0;
--capcol;
if (*skipwhite(line) == NUL)
capcol = 0;
}
line_breakcheck();
}
vim_free(buf);
return 0;
}
void
spell_cat_line(buf, line, maxlen)
char_u *buf;
char_u *line;
int maxlen;
{
char_u *p;
int n;
p = skipwhite(line);
while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
p = skipwhite(p + 1);
if (*p != NUL)
{
n = (int)(p - line) + 1;
if (n < maxlen - 1)
{
vim_memset(buf, ' ', n);
vim_strncpy(buf + n, p, maxlen - 1 - n);
}
}
}
typedef struct spelload_S
{
char_u sl_lang[MAXWLEN + 1];
slang_T *sl_slang;
int sl_nobreak;
} spelload_T;
static void
spell_load_lang(lang)
char_u *lang;
{
char_u fname_enc[85];
int r;
spelload_T sl;
#ifdef FEAT_AUTOCMD
int round;
#endif
STRCPY(sl.sl_lang, lang);
sl.sl_slang = NULL;
sl.sl_nobreak = FALSE;
#ifdef FEAT_AUTOCMD
for (round = 1; round <= 2; ++round)
#endif
{
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, &sl);
if (r == FAIL && *sl.sl_lang != NUL)
{
vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
"spell/%s.ascii.spl", lang);
r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
#ifdef FEAT_AUTOCMD
if (r == FAIL && *sl.sl_lang != NUL && round == 1
&& apply_autocmds(EVENT_SPELLFILEMISSING, lang,
curbuf->b_fname, FALSE, curbuf))
continue;
break;
#endif
}
#ifdef FEAT_AUTOCMD
break;
#endif
}
if (r == FAIL)
{
smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
lang, spell_enc(), lang);
}
else if (sl.sl_slang != NULL)
{
STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
}
}
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";
}
static void
int_wordlist_spl(fname)
char_u *fname;
{
vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
int_wordlist, spell_enc());
}
static slang_T *
slang_alloc(lang)
char_u *lang;
{
slang_T *lp;
lp = (slang_T *)alloc_clear(sizeof(slang_T));
if (lp != NULL)
{
if (lang != NULL)
lp->sl_name = vim_strsave(lang);
ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
lp->sl_compmax = MAXWLEN;
lp->sl_compsylmax = MAXWLEN;
hash_init(&lp->sl_wordcount);
}
return lp;
}
static void
slang_free(lp)
slang_T *lp;
{
vim_free(lp->sl_name);
vim_free(lp->sl_fname);
slang_clear(lp);
vim_free(lp);
}
static void
slang_clear(lp)
slang_T *lp;
{
garray_T *gap;
fromto_T *ftp;
salitem_T *smp;
int i;
int round;
vim_free(lp->sl_fbyts);
lp->sl_fbyts = NULL;
vim_free(lp->sl_kbyts);
lp->sl_kbyts = NULL;
vim_free(lp->sl_pbyts);
lp->sl_pbyts = NULL;
vim_free(lp->sl_fidxs);
lp->sl_fidxs = NULL;
vim_free(lp->sl_kidxs);
lp->sl_kidxs = NULL;
vim_free(lp->sl_pidxs);
lp->sl_pidxs = NULL;
for (round = 1; round <= 2; ++round)
{
gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
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);
}
gap = &lp->sl_sal;
if (lp->sl_sofo)
{
if (gap->ga_data != NULL)
for (i = 0; i < gap->ga_len; ++i)
vim_free(((int **)gap->ga_data)[i]);
}
else
while (gap->ga_len > 0)
{
smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
vim_free(smp->sm_lead);
vim_free(smp->sm_to);
#ifdef FEAT_MBYTE
vim_free(smp->sm_lead_w);
vim_free(smp->sm_oneof_w);
vim_free(smp->sm_to_w);
#endif
}
ga_clear(gap);
for (i = 0; i < lp->sl_prefixcnt; ++i)
vim_free(lp->sl_prefprog[i]);
lp->sl_prefixcnt = 0;
vim_free(lp->sl_prefprog);
lp->sl_prefprog = NULL;
vim_free(lp->sl_info);
lp->sl_info = NULL;
vim_free(lp->sl_midword);
lp->sl_midword = NULL;
vim_free(lp->sl_compprog);
vim_free(lp->sl_comprules);
vim_free(lp->sl_compstartflags);
vim_free(lp->sl_compallflags);
lp->sl_compprog = NULL;
lp->sl_comprules = NULL;
lp->sl_compstartflags = NULL;
lp->sl_compallflags = NULL;
vim_free(lp->sl_syllable);
lp->sl_syllable = NULL;
ga_clear(&lp->sl_syl_items);
ga_clear_strings(&lp->sl_comppat);
hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
hash_init(&lp->sl_wordcount);
#ifdef FEAT_MBYTE
hash_clear_all(&lp->sl_map_hash, 0);
#endif
slang_clear_sug(lp);
lp->sl_compmax = MAXWLEN;
lp->sl_compminlen = 0;
lp->sl_compsylmax = MAXWLEN;
lp->sl_regions[0] = NUL;
}
static void
slang_clear_sug(lp)
slang_T *lp;
{
vim_free(lp->sl_sbyts);
lp->sl_sbyts = NULL;
vim_free(lp->sl_sidxs);
lp->sl_sidxs = NULL;
close_spellbuf(lp->sl_sugbuf);
lp->sl_sugbuf = NULL;
lp->sl_sugloaded = FALSE;
lp->sl_sugtime = 0;
}
static void
spell_load_cb(fname, cookie)
char_u *fname;
void *cookie;
{
spelload_T *slp = (spelload_T *)cookie;
slang_T *slang;
slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
if (slang != NULL)
{
if (slp->sl_nobreak && slang->sl_add)
slang->sl_nobreak = TRUE;
else if (slang->sl_nobreak)
slp->sl_nobreak = TRUE;
slp->sl_slang = slang;
}
}
static slang_T *
spell_load_file(fname, lang, old_lp, silent)
char_u *fname;
char_u *lang;
slang_T *old_lp;
int silent;
{
FILE *fd;
char_u buf[VIMSPELLMAGICL];
char_u *p;
int i;
int n;
int len;
char_u *save_sourcing_name = sourcing_name;
linenr_T save_sourcing_lnum = sourcing_lnum;
slang_T *lp = NULL;
int c = 0;
int res;
fd = mch_fopen((char *)fname, "r");
if (fd == NULL)
{
if (!silent)
EMSG2(_(e_notopen), fname);
else if (p_verbose > 2)
{
verbose_enter();
smsg((char_u *)e_notopen, fname);
verbose_leave();
}
goto endFAIL;
}
if (p_verbose > 2)
{
verbose_enter();
smsg((char_u *)_("Reading spell file \"%s\""), fname);
verbose_leave();
}
if (old_lp == NULL)
{
lp = slang_alloc(lang);
if (lp == NULL)
goto endFAIL;
lp->sl_fname = vim_strsave(fname);
if (lp->sl_fname == NULL)
goto endFAIL;
lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
}
else
lp = old_lp;
sourcing_name = fname;
sourcing_lnum = 0;
for (i = 0; i < VIMSPELLMAGICL; ++i)
buf[i] = getc(fd);
if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
{
EMSG(_("E757: This does not look like a spell file"));
goto endFAIL;
}
c = getc(fd);
if (c < VIMSPELLVERSION)
{
EMSG(_("E771: Old spell file, needs to be updated"));
goto endFAIL;
}
else if (c > VIMSPELLVERSION)
{
EMSG(_("E772: Spell file is for newer version of Vim"));
goto endFAIL;
}
for (;;)
{
n = getc(fd);
if (n == SN_END)
break;
c = getc(fd);
len = get4c(fd);
if (len < 0)
goto truncerr;
res = 0;
switch (n)
{
case SN_INFO:
lp->sl_info = read_string(fd, len);
if (lp->sl_info == NULL)
goto endFAIL;
break;
case SN_REGION:
res = read_region_section(fd, lp, len);
break;
case SN_CHARFLAGS:
res = read_charflags_section(fd);
break;
case SN_MIDWORD:
lp->sl_midword = read_string(fd, len);
if (lp->sl_midword == NULL)
goto endFAIL;
break;
case SN_PREFCOND:
res = read_prefcond_section(fd, lp);
break;
case SN_REP:
res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
break;
case SN_REPSAL:
res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
break;
case SN_SAL:
res = read_sal_section(fd, lp);
break;
case SN_SOFO:
res = read_sofo_section(fd, lp);
break;
case SN_MAP:
p = read_string(fd, len);
if (p == NULL)
goto endFAIL;
set_map_str(lp, p);
vim_free(p);
break;
case SN_WORDS:
res = read_words_section(fd, lp, len);
break;
case SN_SUGFILE:
lp->sl_sugtime = get8ctime(fd);
break;
case SN_NOSPLITSUGS:
lp->sl_nosplitsugs = TRUE;
break;
case SN_COMPOUND:
res = read_compound(fd, lp, len);
break;
case SN_NOBREAK:
lp->sl_nobreak = TRUE;
break;
case SN_SYLLABLE:
lp->sl_syllable = read_string(fd, len);
if (lp->sl_syllable == NULL)
goto endFAIL;
if (init_syl_tab(lp) == FAIL)
goto endFAIL;
break;
default:
if (c & SNF_REQUIRED)
{
EMSG(_("E770: Unsupported section in spell file"));
goto endFAIL;
}
while (--len >= 0)
if (getc(fd) < 0)
goto truncerr;
break;
}
someerror:
if (res == SP_FORMERROR)
{
EMSG(_(e_format));
goto endFAIL;
}
if (res == SP_TRUNCERROR)
{
truncerr:
EMSG(_(e_spell_trunc));
goto endFAIL;
}
if (res == SP_OTHERERROR)
goto endFAIL;
}
res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
if (res != 0)
goto someerror;
res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
if (res != 0)
goto someerror;
res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
lp->sl_prefixcnt);
if (res != 0)
goto someerror;
if (old_lp == NULL && lang != NULL)
{
lp->sl_next = first_lang;
first_lang = lp;
}
goto endOK;
endFAIL:
if (lang != NULL)
*lang = NUL;
if (lp != NULL && old_lp == NULL)
slang_free(lp);
lp = NULL;
endOK:
if (fd != NULL)
fclose(fd);
sourcing_name = save_sourcing_name;
sourcing_lnum = save_sourcing_lnum;
return lp;
}
static char_u *
read_cnt_string(fd, cnt_bytes, cntp)
FILE *fd;
int cnt_bytes;
int *cntp;
{
int cnt = 0;
int i;
char_u *str;
for (i = 0; i < cnt_bytes; ++i)
cnt = (cnt << 8) + getc(fd);
if (cnt < 0)
{
*cntp = SP_TRUNCERROR;
return NULL;
}
*cntp = cnt;
if (cnt == 0)
return NULL;
str = read_string(fd, cnt);
if (str == NULL)
*cntp = SP_OTHERERROR;
return str;
}
static int
read_region_section(fd, lp, len)
FILE *fd;
slang_T *lp;
int len;
{
int i;
if (len > 16)
return SP_FORMERROR;
for (i = 0; i < len; ++i)
lp->sl_regions[i] = getc(fd);
lp->sl_regions[len] = NUL;
return 0;
}
static int
read_charflags_section(fd)
FILE *fd;
{
char_u *flags;
char_u *fol;
int flagslen, follen;
flags = read_cnt_string(fd, 1, &flagslen);
if (flagslen < 0)
return flagslen;
fol = read_cnt_string(fd, 2, &follen);
if (follen < 0)
{
vim_free(flags);
return follen;
}
if (flags != NULL && fol != NULL)
set_spell_charflags(flags, flagslen, fol);
vim_free(flags);
vim_free(fol);
if ((flags == NULL) != (fol == NULL))
return SP_FORMERROR;
return 0;
}
static int
read_prefcond_section(fd, lp)
FILE *fd;
slang_T *lp;
{
int cnt;
int i;
int n;
char_u *p;
char_u buf[MAXWLEN + 1];
cnt = get2c(fd);
if (cnt <= 0)
return SP_FORMERROR;
lp->sl_prefprog = (regprog_T **)alloc_clear(
(unsigned)sizeof(regprog_T *) * cnt);
if (lp->sl_prefprog == NULL)
return SP_OTHERERROR;
lp->sl_prefixcnt = cnt;
for (i = 0; i < cnt; ++i)
{
n = getc(fd);
if (n < 0 || n >= MAXWLEN)
return SP_FORMERROR;
if (n > 0)
{
buf[0] = '^';
p = buf + 1;
while (n-- > 0)
*p++ = getc(fd);
*p = NUL;
lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
}
}
return 0;
}
static int
read_rep_section(fd, gap, first)
FILE *fd;
garray_T *gap;
short *first;
{
int cnt;
fromto_T *ftp;
int i;
cnt = get2c(fd);
if (cnt < 0)
return SP_TRUNCERROR;
if (ga_grow(gap, cnt) == FAIL)
return SP_OTHERERROR;
for (; gap->ga_len < cnt; ++gap->ga_len)
{
ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
ftp->ft_from = read_cnt_string(fd, 1, &i);
if (i < 0)
return i;
if (i == 0)
return SP_FORMERROR;
ftp->ft_to = read_cnt_string(fd, 1, &i);
if (i <= 0)
{
vim_free(ftp->ft_from);
if (i < 0)
return i;
return SP_FORMERROR;
}
}
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;
}
return 0;
}
static int
read_sal_section(fd, slang)
FILE *fd;
slang_T *slang;
{
int i;
int cnt;
garray_T *gap;
salitem_T *smp;
int ccnt;
char_u *p;
int c = NUL;
slang->sl_sofo = FALSE;
i = getc(fd);
if (i & SAL_F0LLOWUP)
slang->sl_followup = TRUE;
if (i & SAL_COLLAPSE)
slang->sl_collapse = TRUE;
if (i & SAL_REM_ACCENTS)
slang->sl_rem_accents = TRUE;
cnt = get2c(fd);
if (cnt < 0)
return SP_TRUNCERROR;
gap = &slang->sl_sal;
ga_init2(gap, sizeof(salitem_T), 10);
if (ga_grow(gap, cnt + 1) == FAIL)
return SP_OTHERERROR;
for (; gap->ga_len < cnt; ++gap->ga_len)
{
smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
ccnt = getc(fd);
if (ccnt < 0)
return SP_TRUNCERROR;
if ((p = alloc(ccnt + 2)) == NULL)
return SP_OTHERERROR;
smp->sm_lead = p;
for (i = 0; i < ccnt; ++i)
{
c = getc(fd);
if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
break;
*p++ = c;
}
smp->sm_leadlen = (int)(p - smp->sm_lead);
*p++ = NUL;
if (c == '(')
{
smp->sm_oneof = p;
for (++i; i < ccnt; ++i)
{
c = getc(fd);
if (c == ')')
break;
*p++ = c;
}
*p++ = NUL;
if (++i < ccnt)
c = getc(fd);
}
else
smp->sm_oneof = NULL;
smp->sm_rules = p;
if (i < ccnt)
*p++ = c;
for (++i; i < ccnt; ++i)
*p++ = getc(fd);
*p++ = NUL;
smp->sm_to = read_cnt_string(fd, 1, &ccnt);
if (ccnt < 0)
{
vim_free(smp->sm_lead);
return ccnt;
}
#ifdef FEAT_MBYTE
if (has_mbyte)
{
smp->sm_lead_w = mb_str2wide(smp->sm_lead);
smp->sm_leadlen = mb_charlen(smp->sm_lead);
if (smp->sm_oneof == NULL)
smp->sm_oneof_w = NULL;
else
smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
if (smp->sm_to == NULL)
smp->sm_to_w = NULL;
else
smp->sm_to_w = mb_str2wide(smp->sm_to);
if (smp->sm_lead_w == NULL
|| (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
|| (smp->sm_to_w == NULL && smp->sm_to != NULL))
{
vim_free(smp->sm_lead);
vim_free(smp->sm_to);
vim_free(smp->sm_lead_w);
vim_free(smp->sm_oneof_w);
vim_free(smp->sm_to_w);
return SP_OTHERERROR;
}
}
#endif
}
if (gap->ga_len > 0)
{
smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
if ((p = alloc(1)) == NULL)
return SP_OTHERERROR;
p[0] = NUL;
smp->sm_lead = p;
smp->sm_leadlen = 0;
smp->sm_oneof = NULL;
smp->sm_rules = p;
smp->sm_to = NULL;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
smp->sm_lead_w = mb_str2wide(smp->sm_lead);
smp->sm_leadlen = 0;
smp->sm_oneof_w = NULL;
smp->sm_to_w = NULL;
}
#endif
++gap->ga_len;
}
set_sal_first(slang);
return 0;
}
static int
read_words_section(fd, lp, len)
FILE *fd;
slang_T *lp;
int len;
{
int done = 0;
int i;
int c;
char_u word[MAXWLEN];
while (done < len)
{
for (i = 0; ; ++i)
{
c = getc(fd);
if (c == EOF)
return SP_TRUNCERROR;
word[i] = c;
if (word[i] == NUL)
break;
if (i == MAXWLEN - 1)
return SP_FORMERROR;
}
count_common_word(lp, word, -1, 10);
done += i + 1;
}
return 0;
}
static void
count_common_word(lp, word, len, count)
slang_T *lp;
char_u *word;
int len;
int count;
{
hash_T hash;
hashitem_T *hi;
wordcount_T *wc;
char_u buf[MAXWLEN];
char_u *p;
if (len == -1)
p = word;
else
{
vim_strncpy(buf, word, len);
p = buf;
}
hash = hash_hash(p);
hi = hash_lookup(&lp->sl_wordcount, p, hash);
if (HASHITEM_EMPTY(hi))
{
wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
if (wc == NULL)
return;
STRCPY(wc->wc_word, p);
wc->wc_count = count;
hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
}
else
{
wc = HI2WC(hi);
if ((wc->wc_count += count) < (unsigned)count)
wc->wc_count = MAXWORDCOUNT;
}
}
static int
score_wordcount_adj(slang, score, word, split)
slang_T *slang;
int score;
char_u *word;
int split;
{
hashitem_T *hi;
wordcount_T *wc;
int bonus;
int newscore;
hi = hash_find(&slang->sl_wordcount, word);
if (!HASHITEM_EMPTY(hi))
{
wc = HI2WC(hi);
if (wc->wc_count < SCORE_THRES2)
bonus = SCORE_COMMON1;
else if (wc->wc_count < SCORE_THRES3)
bonus = SCORE_COMMON2;
else
bonus = SCORE_COMMON3;
if (split)
newscore = score - bonus / 2;
else
newscore = score - bonus;
if (newscore < 0)
return 0;
return newscore;
}
return score;
}
static int
read_sofo_section(fd, slang)
FILE *fd;
slang_T *slang;
{
int cnt;
char_u *from, *to;
int res;
slang->sl_sofo = TRUE;
from = read_cnt_string(fd, 2, &cnt);
if (cnt < 0)
return cnt;
to = read_cnt_string(fd, 2, &cnt);
if (cnt < 0)
{
vim_free(from);
return cnt;
}
if (from != NULL && to != NULL)
res = set_sofo(slang, from, to);
else if (from != NULL || to != NULL)
res = SP_FORMERROR;
else
res = 0;
vim_free(from);
vim_free(to);
return res;
}
static int
read_compound(fd, slang, len)
FILE *fd;
slang_T *slang;
int len;
{
int todo = len;
int c;
int atstart;
char_u *pat;
char_u *pp;
char_u *cp;
char_u *ap;
char_u *crp;
int cnt;
garray_T *gap;
if (todo < 2)
return SP_FORMERROR;
--todo;
c = getc(fd);
if (c < 2)
c = MAXWLEN;
slang->sl_compmax = c;
--todo;
c = getc(fd);
if (c < 1)
c = 0;
slang->sl_compminlen = c;
--todo;
c = getc(fd);
if (c < 1)
c = MAXWLEN;
slang->sl_compsylmax = c;
c = getc(fd);
if (c != 0)
ungetc(c, fd);
else
{
--todo;
c = getc(fd);
--todo;
slang->sl_compoptions = c;
gap = &slang->sl_comppat;
c = get2c(fd);
todo -= 2;
ga_init2(gap, sizeof(char_u *), c);
if (ga_grow(gap, c) == OK)
while (--c >= 0)
{
((char_u **)(gap->ga_data))[gap->ga_len++] =
read_cnt_string(fd, 1, &cnt);
if (cnt < 0)
return cnt;
todo -= cnt + 1;
}
}
if (todo < 0)
return SP_FORMERROR;
c = todo * 2 + 7;
#ifdef FEAT_MBYTE
if (enc_utf8)
c += todo * 2;
#endif
pat = alloc((unsigned)c);
if (pat == NULL)
return SP_OTHERERROR;
cp = alloc(todo + 1);
if (cp == NULL)
{
vim_free(pat);
return SP_OTHERERROR;
}
slang->sl_compstartflags = cp;
*cp = NUL;
ap = alloc(todo + 1);
if (ap == NULL)
{
vim_free(pat);
return SP_OTHERERROR;
}
slang->sl_compallflags = ap;
*ap = NUL;
crp = alloc(todo + 1);
slang->sl_comprules = crp;
pp = pat;
*pp++ = '^';
*pp++ = '\\';
*pp++ = '(';
atstart = 1;
while (todo-- > 0)
{
c = getc(fd);
if (c == EOF)
{
vim_free(pat);
return SP_TRUNCERROR;
}
if (vim_strchr((char_u *)"+*[]/", c) == NULL
&& !byte_in_str(slang->sl_compallflags, c))
{
*ap++ = c;
*ap = NUL;
}
if (atstart != 0)
{
if (c == '[')
atstart = 2;
else if (c == ']')
atstart = 0;
else
{
if (!byte_in_str(slang->sl_compstartflags, c))
{
*cp++ = c;
*cp = NUL;
}
if (atstart == 1)
atstart = 0;
}
}
if (crp != NULL)
{
if (c == '+' || c == '*')
{
vim_free(slang->sl_comprules);
slang->sl_comprules = NULL;
crp = NULL;
}
else
*crp++ = c;
}
if (c == '/')
{
*pp++ = '\\';
*pp++ = '|';
atstart = 1;
}
else
{
if (c == '+' || c == '~')
*pp++ = '\\';
#ifdef FEAT_MBYTE
if (enc_utf8)
pp += mb_char2bytes(c, pp);
else
#endif
*pp++ = c;
}
}
*pp++ = '\\';
*pp++ = ')';
*pp++ = '$';
*pp = NUL;
if (crp != NULL)
*crp = NUL;
slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
vim_free(pat);
if (slang->sl_compprog == NULL)
return SP_FORMERROR;
return 0;
}
static int
byte_in_str(str, n)
char_u *str;
int n;
{
char_u *p;
for (p = str; *p != NUL; ++p)
if (*p == n)
return TRUE;
return FALSE;
}
#define SY_MAXLEN 30
typedef struct syl_item_S
{
char_u sy_chars[SY_MAXLEN];
int sy_len;
} syl_item_T;
static int
init_syl_tab(slang)
slang_T *slang;
{
char_u *p;
char_u *s;
int l;
syl_item_T *syl;
ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
p = vim_strchr(slang->sl_syllable, '/');
while (p != NULL)
{
*p++ = NUL;
if (*p == NUL)
break;
s = p;
p = vim_strchr(p, '/');
if (p == NULL)
l = (int)STRLEN(s);
else
l = (int)(p - s);
if (l >= SY_MAXLEN)
return SP_FORMERROR;
if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
return SP_OTHERERROR;
syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
+ slang->sl_syl_items.ga_len++;
vim_strncpy(syl->sy_chars, s, l);
syl->sy_len = l;
}
return OK;
}
static int
count_syllables(slang, word)
slang_T *slang;
char_u *word;
{
int cnt = 0;
int skip = FALSE;
char_u *p;
int len;
int i;
syl_item_T *syl;
int c;
if (slang->sl_syllable == NULL)
return 0;
for (p = word; *p != NUL; p += len)
{
if (*p == ' ')
{
len = 1;
cnt = 0;
continue;
}
len = 0;
for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
{
syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
if (syl->sy_len > len
&& STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
len = syl->sy_len;
}
if (len != 0)
{
++cnt;
skip = FALSE;
}
else
{
#ifdef FEAT_MBYTE
c = mb_ptr2char(p);
len = (*mb_ptr2len)(p);
#else
c = *p;
len = 1;
#endif
if (vim_strchr(slang->sl_syllable, c) == NULL)
skip = FALSE;
else if (!skip)
{
++cnt;
skip = TRUE;
}
}
}
return cnt;
}
static int
set_sofo(lp, from, to)
slang_T *lp;
char_u *from;
char_u *to;
{
int i;
#ifdef FEAT_MBYTE
garray_T *gap;
char_u *s;
char_u *p;
int c;
int *inp;
if (has_mbyte)
{
gap = &lp->sl_sal;
ga_init2(gap, sizeof(int *), 1);
if (ga_grow(gap, 256) == FAIL)
return SP_OTHERERROR;
vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
gap->ga_len = 256;
for (p = from, s = to; *p != NUL && *s != NUL; )
{
c = mb_cptr2char_adv(&p);
mb_cptr_adv(s);
if (c >= 256)
++lp->sl_sal_first[c & 0xff];
}
if (*p != NUL || *s != NUL)
return SP_FORMERROR;
for (i = 0; i < 256; ++i)
if (lp->sl_sal_first[i] > 0)
{
p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
if (p == NULL)
return SP_OTHERERROR;
((int **)gap->ga_data)[i] = (int *)p;
*(int *)p = 0;
}
vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
for (p = from, s = to; *p != NUL && *s != NUL; )
{
c = mb_cptr2char_adv(&p);
i = mb_cptr2char_adv(&s);
if (c >= 256)
{
inp = ((int **)gap->ga_data)[c & 0xff];
while (*inp != 0)
++inp;
*inp++ = c;
*inp++ = i;
*inp++ = NUL;
}
else
lp->sl_sal_first[c] = i;
}
}
else
#endif
{
if (STRLEN(from) != STRLEN(to))
return SP_FORMERROR;
for (i = 0; to[i] != NUL; ++i)
lp->sl_sal_first[from[i]] = to[i];
lp->sl_sal.ga_len = 1;
}
return 0;
}
static void
set_sal_first(lp)
slang_T *lp;
{
salfirst_T *sfirst;
int i;
salitem_T *smp;
int c;
garray_T *gap = &lp->sl_sal;
sfirst = lp->sl_sal_first;
for (i = 0; i < 256; ++i)
sfirst[i] = -1;
smp = (salitem_T *)gap->ga_data;
for (i = 0; i < gap->ga_len; ++i)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
c = *smp[i].sm_lead_w & 0xff;
else
#endif
c = *smp[i].sm_lead;
if (sfirst[c] == -1)
{
sfirst[c] = i;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int n;
while (i + 1 < gap->ga_len
&& (*smp[i + 1].sm_lead_w & 0xff) == c)
++i;
for (n = 1; i + n < gap->ga_len; ++n)
if ((*smp[i + n].sm_lead_w & 0xff) == c)
{
salitem_T tsal;
++i;
--n;
tsal = smp[i + n];
mch_memmove(smp + i + 1, smp + i,
sizeof(salitem_T) * n);
smp[i] = tsal;
}
}
#endif
}
}
}
#ifdef FEAT_MBYTE
static int *
mb_str2wide(s)
char_u *s;
{
int *res;
char_u *p;
int i = 0;
res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
if (res != NULL)
{
for (p = s; *p != NUL; )
res[i++] = mb_ptr2char_adv(&p);
res[i] = NUL;
}
return res;
}
#endif
static int
spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
FILE *fd;
char_u **bytsp;
idx_T **idxsp;
int prefixtree;
int prefixcnt;
{
int len;
int idx;
char_u *bp;
idx_T *ip;
len = get4c(fd);
if (len < 0)
return SP_TRUNCERROR;
if (len > 0)
{
bp = lalloc((long_u)len, TRUE);
if (bp == NULL)
return SP_OTHERERROR;
*bytsp = bp;
ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
if (ip == NULL)
return SP_OTHERERROR;
*idxsp = ip;
idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
if (idx < 0)
return idx;
}
return 0;
}
static idx_T
read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
FILE *fd;
char_u *byts;
idx_T *idxs;
int maxidx;
idx_T startidx;
int prefixtree;
int maxprefcondnr;
{
int len;
int i;
int n;
idx_T idx = startidx;
int c;
int c2;
#define SHARED_MASK 0x8000000
len = getc(fd);
if (len <= 0)
return SP_TRUNCERROR;
if (startidx + len >= maxidx)
return SP_FORMERROR;
byts[idx++] = len;
for (i = 1; i <= len; ++i)
{
c = getc(fd);
if (c < 0)
return SP_TRUNCERROR;
if (c <= BY_SPECIAL)
{
if (c == BY_NOFLAGS && !prefixtree)
{
idxs[idx] = 0;
c = 0;
}
else if (c != BY_INDEX)
{
if (prefixtree)
{
if (c == BY_FLAGS)
c = getc(fd) << 24;
else
c = 0;
c |= getc(fd);
n = get2c(fd);
if (n >= maxprefcondnr)
return SP_FORMERROR;
c |= (n << 8);
}
else
{
c2 = c;
c = getc(fd);
if (c2 == BY_FLAGS2)
c = (getc(fd) << 8) + c;
if (c & WF_REGION)
c = (getc(fd) << 16) + c;
if (c & WF_AFX)
c = (getc(fd) << 24) + c;
}
idxs[idx] = c;
c = 0;
}
else
{
n = get3c(fd);
if (n < 0 || n >= maxidx)
return SP_FORMERROR;
idxs[idx] = n + SHARED_MASK;
c = getc(fd);
}
}
byts[idx++] = c;
}
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_node(fd, byts, idxs, maxidx, idx,
prefixtree, maxprefcondnr);
if (idx < 0)
break;
}
}
return idx;
}
char_u *
did_set_spelllang(wp)
win_T *wp;
{
garray_T ga;
char_u *splp;
char_u *region;
char_u region_cp[3];
int filename;
int region_mask;
slang_T *slang;
int c;
char_u lang[MAXWLEN + 1];
char_u spf_name[MAXPATHL];
int len;
char_u *p;
int round;
char_u *spf;
char_u *use_region = NULL;
int dont_use_region = FALSE;
int nobreak = FALSE;
int i, j;
langp_T *lp, *lp2;
static int recursive = FALSE;
char_u *ret_msg = NULL;
char_u *spl_copy;
if (recursive)
return NULL;
recursive = TRUE;
ga_init2(&ga, sizeof(langp_T), 2);
clear_midword(wp);
spl_copy = vim_strsave(wp->w_s->b_p_spl);
if (spl_copy == NULL)
goto theend;
for (splp = spl_copy; *splp != NUL; )
{
copy_option_part(&splp, lang, MAXWLEN, ",");
region = NULL;
len = (int)STRLEN(lang);
if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
{
filename = TRUE;
p = vim_strchr(gettail(lang), '_');
if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
&& !ASCII_ISALPHA(p[3]))
{
vim_strncpy(region_cp, p + 1, 2);
mch_memmove(p, p + 3, len - (p - lang) - 2);
len -= 3;
region = region_cp;
}
else
dont_use_region = TRUE;
for (slang = first_lang; slang != NULL; slang = slang->sl_next)
if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
break;
}
else
{
filename = FALSE;
if (len > 3 && lang[len - 3] == '_')
{
region = lang + len - 2;
len -= 3;
lang[len] = NUL;
}
else
dont_use_region = TRUE;
for (slang = first_lang; slang != NULL; slang = slang->sl_next)
if (STRICMP(lang, slang->sl_name) == 0)
break;
}
if (region != NULL)
{
if (use_region != NULL && STRCMP(region, use_region) != 0)
dont_use_region = TRUE;
use_region = region;
}
if (slang == NULL)
{
if (filename)
(void)spell_load_file(lang, lang, NULL, FALSE);
else
{
spell_load_lang(lang);
#ifdef FEAT_AUTOCMD
if (!buf_valid(wp->w_buffer))
{
ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
goto theend;
}
#endif
}
}
for (slang = first_lang; slang != NULL; slang = slang->sl_next)
if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
: STRICMP(lang, slang->sl_name) == 0)
{
region_mask = REGION_ALL;
if (!filename && region != NULL)
{
c = find_region(slang->sl_regions, region);
if (c == REGION_ALL)
{
if (slang->sl_add)
{
if (*slang->sl_regions != NUL)
region_mask = 0;
}
else
smsg((char_u *)
_("Warning: region %s not supported"),
region);
}
else
region_mask = 1 << c;
}
if (region_mask != 0)
{
if (ga_grow(&ga, 1) == FAIL)
{
ga_clear(&ga);
ret_msg = e_outofmem;
goto theend;
}
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
++ga.ga_len;
use_midword(slang, wp);
if (slang->sl_nobreak)
nobreak = TRUE;
}
}
}
spf = curwin->w_s->b_p_spf;
for (round = 0; round == 0 || *spf != NUL; ++round)
{
if (round == 0)
{
if (int_wordlist == NULL)
continue;
int_wordlist_spl(spf_name);
}
else
{
copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
STRCAT(spf_name, ".spl");
for (c = 0; c < ga.ga_len; ++c)
{
p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
break;
}
if (c < ga.ga_len)
continue;
}
for (slang = first_lang; slang != NULL; slang = slang->sl_next)
if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
break;
if (slang == NULL)
{
if (round == 0)
STRCPY(lang, "internal wordlist");
else
{
vim_strncpy(lang, gettail(spf_name), MAXWLEN);
p = vim_strchr(lang, '.');
if (p != NULL)
*p = NUL;
}
slang = spell_load_file(spf_name, lang, NULL, TRUE);
if (slang != NULL && nobreak)
slang->sl_nobreak = TRUE;
}
if (slang != NULL && ga_grow(&ga, 1) == OK)
{
region_mask = REGION_ALL;
if (use_region != NULL && !dont_use_region)
{
c = find_region(slang->sl_regions, use_region);
if (c != REGION_ALL)
region_mask = 1 << c;
else if (*slang->sl_regions != NUL)
region_mask = 0;
}
if (region_mask != 0)
{
LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
++ga.ga_len;
use_midword(slang, wp);
}
}
}
ga_clear(&wp->w_s->b_langp);
wp->w_s->b_langp = ga;
for (i = 0; i < ga.ga_len; ++i)
{
lp = LANGP_ENTRY(ga, i);
if (lp->lp_slang->sl_sal.ga_len > 0)
lp->lp_sallang = lp->lp_slang;
else
for (j = 0; j < ga.ga_len; ++j)
{
lp2 = LANGP_ENTRY(ga, j);
if (lp2->lp_slang->sl_sal.ga_len > 0
&& STRNCMP(lp->lp_slang->sl_name,
lp2->lp_slang->sl_name, 2) == 0)
{
lp->lp_sallang = lp2->lp_slang;
break;
}
}
if (lp->lp_slang->sl_rep.ga_len > 0)
lp->lp_replang = lp->lp_slang;
else
for (j = 0; j < ga.ga_len; ++j)
{
lp2 = LANGP_ENTRY(ga, j);
if (lp2->lp_slang->sl_rep.ga_len > 0
&& STRNCMP(lp->lp_slang->sl_name,
lp2->lp_slang->sl_name, 2) == 0)
{
lp->lp_replang = lp2->lp_slang;
break;
}
}
}
theend:
vim_free(spl_copy);
recursive = FALSE;
return ret_msg;
}
static void
clear_midword(wp)
win_T *wp;
{
vim_memset(wp->w_s->b_spell_ismw, 0, 256);
#ifdef FEAT_MBYTE
vim_free(wp->w_s->b_spell_ismw_mb);
wp->w_s->b_spell_ismw_mb = NULL;
#endif
}
static void
use_midword(lp, wp)
slang_T *lp;
win_T *wp;
{
char_u *p;
if (lp->sl_midword == NULL)
return;
for (p = lp->sl_midword; *p != NUL; )
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int c, l, n;
char_u *bp;
c = mb_ptr2char(p);
l = (*mb_ptr2len)(p);
if (c < 256 && l <= 2)
wp->w_s->b_spell_ismw[c] = TRUE;
else if (wp->w_s->b_spell_ismw_mb == NULL)
wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
else
{
n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
if (bp != NULL)
{
vim_free(wp->w_s->b_spell_ismw_mb);
wp->w_s->b_spell_ismw_mb = bp;
vim_strncpy(bp + n, p, l);
}
}
p += l;
}
else
#endif
wp->w_s->b_spell_ismw[*p++] = TRUE;
}
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;
}
static int
captype(word, end)
char_u *word;
char_u *end;
{
char_u *p;
int c;
int firstcap;
int allcap;
int past_second = FALSE;
for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
if (end == NULL ? *p == NUL : p >= end)
return 0;
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_ptr2char_adv(&p);
else
#endif
c = *p++;
firstcap = allcap = SPELL_ISUPPER(c);
for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
if (spell_iswordp_nmw(p))
{
c = PTR2CHAR(p);
if (!SPELL_ISUPPER(c))
{
if (past_second && allcap)
return WF_KEEPCAP;
allcap = FALSE;
}
else if (!allcap)
return WF_KEEPCAP;
past_second = TRUE;
}
if (allcap)
return WF_ALLCAP;
if (firstcap)
return WF_ONECAP;
return 0;
}
static int
badword_captype(word, end)
char_u *word;
char_u *end;
{
int flags = captype(word, end);
int c;
int l, u;
int first;
char_u *p;
if (flags & WF_KEEPCAP)
{
l = u = 0;
first = FALSE;
for (p = word; p < end; mb_ptr_adv(p))
{
c = PTR2CHAR(p);
if (SPELL_ISUPPER(c))
{
++u;
if (p == word)
first = TRUE;
}
else
++l;
}
if (u > l && u > 2)
flags |= WF_ALLCAP;
else if (first)
flags |= WF_ONECAP;
if (u >= 2 && l >= 2)
flags |= WF_MIXCAP;
}
return flags;
}
# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
void
spell_free_all()
{
slang_T *slang;
buf_T *buf;
char_u fname[MAXPATHL];
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
ga_clear(&buf->b_s.b_langp);
while (first_lang != NULL)
{
slang = first_lang;
first_lang = slang->sl_next;
slang_free(slang);
}
if (int_wordlist != NULL)
{
mch_remove(int_wordlist);
int_wordlist_spl(fname);
mch_remove(fname);
vim_free(int_wordlist);
int_wordlist = NULL;
}
init_spell_chartab();
vim_free(repl_to);
repl_to = NULL;
vim_free(repl_from);
repl_from = NULL;
}
# endif
# if defined(FEAT_MBYTE) || defined(PROTO)
void
spell_reload()
{
win_T *wp;
init_spell_chartab();
spell_free_all();
for (wp = firstwin; wp != NULL; wp = wp->w_next)
{
if (*wp->w_s->b_p_spl != NUL)
{
if (wp->w_p_spell)
{
(void)did_set_spelllang(wp);
# ifdef FEAT_WINDOWS
break;
# endif
}
}
}
}
# endif
static void
spell_reload_one(fname, added_word)
char_u *fname;
int added_word;
{
slang_T *slang;
int didit = FALSE;
for (slang = first_lang; slang != NULL; slang = slang->sl_next)
{
if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
{
slang_clear(slang);
if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
slang_clear(slang);
redraw_all_later(SOME_VALID);
didit = TRUE;
}
}
if (added_word && !didit)
did_set_spelllang(curwin);
}
#define MAXLINELEN 500
typedef struct afffile_S
{
char_u *af_enc;
int af_flagtype;
unsigned af_rare;
unsigned af_keepcase;
unsigned af_bad;
unsigned af_needaffix;
unsigned af_circumfix;
unsigned af_needcomp;
unsigned af_comproot;
unsigned af_compforbid;
unsigned af_comppermit;
unsigned af_nosuggest;
int af_pfxpostpone;
hashtab_T af_pref;
hashtab_T af_suff;
hashtab_T af_comp;
} afffile_T;
#define AFT_CHAR 0
#define AFT_LONG 1
#define AFT_CAPLONG 2
#define AFT_NUM 3
typedef struct affentry_S affentry_T;
struct affentry_S
{
affentry_T *ae_next;
char_u *ae_chop;
char_u *ae_add;
char_u *ae_flags;
char_u *ae_cond;
regprog_T *ae_prog;
char ae_compforbid;
char ae_comppermit;
};
#ifdef FEAT_MBYTE
# define AH_KEY_LEN 17
#else
# define AH_KEY_LEN 7
#endif
typedef struct affheader_S
{
char_u ah_key[AH_KEY_LEN];
unsigned ah_flag;
int ah_newID;
int ah_combine;
int ah_follows;
affentry_T *ah_first;
} affheader_T;
#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
typedef struct compitem_S
{
char_u ci_key[AH_KEY_LEN];
unsigned ci_flag;
int ci_newID;
} compitem_T;
#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
#define SBLOCKSIZE 16000
typedef struct sblock_S sblock_T;
struct sblock_S
{
int sb_used;
sblock_T *sb_next;
char_u sb_data[1];
};
typedef struct wordnode_S wordnode_T;
struct wordnode_S
{
union
{
char_u hashkey[6];
int index;
} wn_u1;
union
{
wordnode_T *next;
wordnode_T *wnode;
} wn_u2;
wordnode_T *wn_child;
wordnode_T *wn_sibling;
int wn_refs;
char_u wn_byte;
char_u wn_affixID;
short_u wn_flags;
short wn_region;
#ifdef SPELL_PRINTTREE
int wn_nr;
#endif
};
#define WN_MASK 0xffff
#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
typedef struct spellinfo_S
{
wordnode_T *si_foldroot;
long si_foldwcount;
wordnode_T *si_keeproot;
long si_keepwcount;
wordnode_T *si_prefroot;
long si_sugtree;
sblock_T *si_blocks;
long si_blocks_cnt;
long si_compress_cnt;
wordnode_T *si_first_free;
long si_free_count;
#ifdef SPELL_PRINTTREE
int si_wordnode_nr;
#endif
buf_T *si_spellbuf;
int si_ascii;
int si_add;
int si_clear_chartab;
int si_region;
vimconv_T si_conv;
int si_memtot;
int si_verbose;
int si_msg_count;
char_u *si_info;
int si_region_count;
char_u si_region_name[16];
garray_T si_rep;
garray_T si_repsal;
garray_T si_sal;
char_u *si_sofofr;
char_u *si_sofoto;
int si_nosugfile;
int si_nosplitsugs;
int si_followup;
int si_collapse;
hashtab_T si_commonwords;
time_t si_sugtime;
int si_rem_accents;
garray_T si_map;
char_u *si_midword;
int si_compmax;
int si_compminlen;
int si_compsylmax;
int si_compoptions;
garray_T si_comppat;
char_u *si_compflags;
char_u si_nobreak;
char_u *si_syllable;
garray_T si_prefcond;
int si_newprefID;
int si_newcompID;
} spellinfo_T;
static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
static int spell_info_item __ARGS((char_u *s));
static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
static void check_renumber __ARGS((spellinfo_T *spin));
static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
static void aff_check_number __ARGS((int spinval, int affval, char *name));
static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
static int str_equal __ARGS((char_u *s1, char_u *s2));
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));
static int has_non_ascii __ARGS((char_u *s));
static void spell_free_aff __ARGS((afffile_T *aff));
static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen));
static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
static void free_blocks __ARGS((sblock_T *bl));
static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix));
static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
static void clear_node __ARGS((wordnode_T *node));
static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
static int sug_maketable __ARGS((spellinfo_T *spin));
static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
static int offset2bytes __ARGS((int nr, char_u *buf));
static int bytes2offset __ARGS((char_u **pp));
static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
static void init_spellfile __ARGS((void));
#define PFX_FLAGS -256
#define CONDIT_COMB 1
#define CONDIT_CFIX 2
#define CONDIT_SUF 4
#define CONDIT_AFF 8
static long compress_start = 30000;
static long compress_inc = 100;
static long compress_added = 500000;
#ifdef SPELL_PRINTTREE
#define PRINTLINESIZE 1000
#define PRINTWIDTH 6
#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
static char line1[PRINTLINESIZE];
static char line2[PRINTLINESIZE];
static char line3[PRINTLINESIZE];
static void
spell_clear_flags(wordnode_T *node)
{
wordnode_T *np;
for (np = node; np != NULL; np = np->wn_sibling)
{
np->wn_u1.index = FALSE;
spell_clear_flags(np->wn_child);
}
}
static void
spell_print_node(wordnode_T *node, int depth)
{
if (node->wn_u1.index)
{
PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
PRINTSOME(line2, depth, " ", 0, 0);
PRINTSOME(line3, depth, " ", 0, 0);
msg(line1);
msg(line2);
msg(line3);
}
else
{
node->wn_u1.index = TRUE;
if (node->wn_byte != NUL)
{
if (node->wn_child != NULL)
PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
else
PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
}
else
PRINTSOME(line1, depth, " $ ", 0, 0);
PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
if (node->wn_sibling != NULL)
PRINTSOME(line3, depth, " | ", 0, 0);
else
PRINTSOME(line3, depth, " ", 0, 0);
if (node->wn_byte == NUL)
{
msg(line1);
msg(line2);
msg(line3);
}
if (node->wn_byte != NUL && node->wn_child != NULL)
spell_print_node(node->wn_child, depth + 1);
if (node->wn_sibling != NULL)
{
STRCPY(line1, line3);
STRCPY(line2, line3);
spell_print_node(node->wn_sibling, depth);
}
}
}
static void
spell_print_tree(wordnode_T *root)
{
if (root != NULL)
{
spell_clear_flags(root);
spell_print_node(root, 0);
}
}
#endif
static afffile_T *
spell_read_aff(spin, fname)
spellinfo_T *spin;
char_u *fname;
{
FILE *fd;
afffile_T *aff;
char_u rline[MAXLINELEN];
char_u *line;
char_u *pc = NULL;
#define MAXITEMCNT 30
char_u *(items[MAXITEMCNT]);
int itemcnt;
char_u *p;
int lnum = 0;
affheader_T *cur_aff = NULL;
int did_postpone_prefix = FALSE;
int aff_todo = 0;
hashtab_T *tp;
char_u *low = NULL;
char_u *fol = NULL;
char_u *upp = NULL;
int do_rep;
int do_repsal;
int do_sal;
int do_mapline;
int found_map = FALSE;
hashitem_T *hi;
int l;
int compminlen = 0;
int compsylmax = 0;
int compoptions = 0;
int compmax = 0;
char_u *compflags = NULL;
char_u *midword = NULL;
char_u *syllable = NULL;
char_u *sofofrom = NULL;
char_u *sofoto = NULL;
fd = mch_fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return NULL;
}
vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
spell_message(spin, IObuff);
do_rep = spin->si_rep.ga_len == 0;
do_repsal = spin->si_repsal.ga_len == 0;
do_sal = spin->si_sal.ga_len == 0;
do_mapline = spin->si_map.ga_len == 0;
aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
if (aff == NULL)
{
fclose(fd);
return NULL;
}
hash_init(&aff->af_pref);
hash_init(&aff->af_suff);
hash_init(&aff->af_comp);
while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
{
line_breakcheck();
++lnum;
if (*rline == '#')
continue;
vim_free(pc);
#ifdef FEAT_MBYTE
if (spin->si_conv.vc_type != CONV_NONE)
{
pc = string_convert(&spin->si_conv, rline, NULL);
if (pc == NULL)
{
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
fname, lnum, rline);
continue;
}
line = pc;
}
else
#endif
{
pc = NULL;
line = rline;
}
itemcnt = 0;
for (p = line; ; )
{
while (*p != NUL && *p <= ' ')
++p;
if (*p == NUL)
break;
if (itemcnt == MAXITEMCNT)
break;
items[itemcnt++] = p;
if (itemcnt == 2 && spell_info_item(items[0]))
while (*p >= ' ' || *p == TAB)
++p;
else
while (*p > ' ')
++p;
if (*p == NUL)
break;
*p++ = NUL;
}
if (itemcnt > 0)
{
if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
{
#ifdef FEAT_MBYTE
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);
spin->si_conv.vc_fail = TRUE;
#else
smsg((char_u *)_("Conversion in %s not supported"), fname);
#endif
}
else if (is_aff_rule(items, itemcnt, "FLAG", 2)
&& aff->af_flagtype == AFT_CHAR)
{
if (STRCMP(items[1], "long") == 0)
aff->af_flagtype = AFT_LONG;
else if (STRCMP(items[1], "num") == 0)
aff->af_flagtype = AFT_NUM;
else if (STRCMP(items[1], "caplong") == 0)
aff->af_flagtype = AFT_CAPLONG;
else
smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
fname, lnum, items[1]);
if (aff->af_rare != 0
|| aff->af_keepcase != 0
|| aff->af_bad != 0
|| aff->af_needaffix != 0
|| aff->af_circumfix != 0
|| aff->af_needcomp != 0
|| aff->af_comproot != 0
|| aff->af_nosuggest != 0
|| compflags != NULL
|| aff->af_suff.ht_used > 0
|| aff->af_pref.ht_used > 0)
smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
fname, lnum, items[1]);
}
else if (spell_info_item(items[0]))
{
p = (char_u *)getroom(spin,
(spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
+ STRLEN(items[0])
+ STRLEN(items[1]) + 3, FALSE);
if (p != NULL)
{
if (spin->si_info != NULL)
{
STRCPY(p, spin->si_info);
STRCAT(p, "\n");
}
STRCAT(p, items[0]);
STRCAT(p, " ");
STRCAT(p, items[1]);
spin->si_info = p;
}
}
else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
&& midword == NULL)
{
midword = getroom_save(spin, items[1]);
}
else if (is_aff_rule(items, itemcnt, "TRY", 2))
{
}
else if ((is_aff_rule(items, itemcnt, "RAR", 2)
|| is_aff_rule(items, itemcnt, "RARE", 2))
&& aff->af_rare == 0)
{
aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if ((is_aff_rule(items, itemcnt, "KEP", 2)
|| is_aff_rule(items, itemcnt, "KEEPCASE", 2))
&& aff->af_keepcase == 0)
{
aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if ((is_aff_rule(items, itemcnt, "BAD", 2)
|| is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
&& aff->af_bad == 0)
{
aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
&& aff->af_needaffix == 0)
{
aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
&& aff->af_circumfix == 0)
{
aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
&& aff->af_nosuggest == 0)
{
aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
|| is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
&& aff->af_needcomp == 0)
{
aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
&& aff->af_comproot == 0)
{
aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
&& aff->af_compforbid == 0)
{
aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
if (aff->af_pref.ht_used > 0)
smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
fname, lnum);
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
&& aff->af_comppermit == 0)
{
aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
if (aff->af_pref.ht_used > 0)
smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
fname, lnum);
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
&& compflags == NULL)
{
p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
if (p != NULL)
{
STRCPY(p, items[1]);
STRCAT(p, "+");
compflags = p;
}
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
{
if (atoi((char *)items[1]) == 0)
smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
fname, lnum, items[1]);
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
{
l = (int)STRLEN(items[1]) + 1;
if (compflags != NULL)
l += (int)STRLEN(compflags) + 1;
p = getroom(spin, l, FALSE);
if (p != NULL)
{
if (compflags != NULL)
{
STRCPY(p, compflags);
STRCAT(p, "/");
}
STRCAT(p, items[1]);
compflags = p;
}
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
&& compmax == 0)
{
compmax = atoi((char *)items[1]);
if (compmax == 0)
smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
fname, lnum, items[1]);
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
&& compminlen == 0)
{
compminlen = atoi((char *)items[1]);
if (compminlen == 0)
smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
fname, lnum, items[1]);
}
else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
&& compsylmax == 0)
{
compsylmax = atoi((char *)items[1]);
if (compsylmax == 0)
smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
fname, lnum, items[1]);
}
else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
{
compoptions |= COMP_CHECKDUP;
}
else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
{
compoptions |= COMP_CHECKREP;
}
else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
{
compoptions |= COMP_CHECKCASE;
}
else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
{
compoptions |= COMP_CHECKTRIPLE;
}
else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
{
if (atoi((char *)items[1]) == 0)
smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
fname, lnum, items[1]);
}
else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
{
garray_T *gap = &spin->si_comppat;
int i;
for (i = 0; i < gap->ga_len - 1; i += 2)
if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
&& STRCMP(((char_u **)(gap->ga_data))[i + 1],
items[2]) == 0)
break;
if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
{
((char_u **)(gap->ga_data))[gap->ga_len++]
= getroom_save(spin, items[1]);
((char_u **)(gap->ga_data))[gap->ga_len++]
= getroom_save(spin, items[2]);
}
}
else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
&& syllable == NULL)
{
syllable = getroom_save(spin, items[1]);
}
else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
{
spin->si_nobreak = TRUE;
}
else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
{
spin->si_nosplitsugs = TRUE;
}
else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
{
spin->si_nosugfile = TRUE;
}
else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
{
aff->af_pfxpostpone = TRUE;
}
else if ((STRCMP(items[0], "PFX") == 0
|| STRCMP(items[0], "SFX") == 0)
&& aff_todo == 0
&& itemcnt >= 4)
{
int lasti = 4;
char_u key[AH_KEY_LEN];
if (*items[0] == 'P')
tp = &aff->af_pref;
else
tp = &aff->af_suff;
vim_strncpy(key, items[1], AH_KEY_LEN - 1);
hi = hash_find(tp, key);
if (!HASHITEM_EMPTY(hi))
{
cur_aff = HI2AH(hi);
if (cur_aff->ah_combine != (*items[2] == 'Y'))
smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
fname, lnum, items[1]);
if (!cur_aff->ah_follows)
smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
fname, lnum, items[1]);
}
else
{
cur_aff = (affheader_T *)getroom(spin,
sizeof(affheader_T), TRUE);
if (cur_aff == NULL)
break;
cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
fname, lnum);
if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
break;
if (cur_aff->ah_flag == aff->af_bad
|| cur_aff->ah_flag == aff->af_rare
|| cur_aff->ah_flag == aff->af_keepcase
|| cur_aff->ah_flag == aff->af_needaffix
|| cur_aff->ah_flag == aff->af_circumfix
|| cur_aff->ah_flag == aff->af_nosuggest
|| cur_aff->ah_flag == aff->af_needcomp
|| cur_aff->ah_flag == aff->af_comproot)
smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
fname, lnum, items[1]);
STRCPY(cur_aff->ah_key, items[1]);
hash_add(tp, cur_aff->ah_key);
cur_aff->ah_combine = (*items[2] == 'Y');
}
if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
{
++lasti;
cur_aff->ah_follows = TRUE;
}
else
cur_aff->ah_follows = FALSE;
if (itemcnt > lasti && *items[lasti] != '#')
smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
fname, lnum, items[2]);
if (*items[0] == 'P' && aff->af_pfxpostpone)
{
if (cur_aff->ah_newID == 0)
{
check_renumber(spin);
cur_aff->ah_newID = ++spin->si_newprefID;
did_postpone_prefix = FALSE;
}
else
did_postpone_prefix = TRUE;
}
aff_todo = atoi((char *)items[3]);
}
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;
int upper = FALSE;
int lasti = 5;
if (itemcnt > lasti && *items[lasti] != '#'
&& (STRCMP(items[lasti], "-") != 0
|| itemcnt != lasti + 1))
smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
--aff_todo;
aff_entry = (affentry_T *)getroom(spin,
sizeof(affentry_T), TRUE);
if (aff_entry == NULL)
break;
if (STRCMP(items[2], "0") != 0)
aff_entry->ae_chop = getroom_save(spin, items[2]);
if (STRCMP(items[3], "0") != 0)
{
aff_entry->ae_add = getroom_save(spin, items[3]);
aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
if (aff_entry->ae_flags != NULL)
{
*aff_entry->ae_flags++ = NUL;
aff_process_flags(aff, aff_entry);
}
}
if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
|| has_non_ascii(aff_entry->ae_add)))
{
aff_entry->ae_next = cur_aff->ah_first;
cur_aff->ah_first = aff_entry;
if (STRCMP(items[4], ".") != 0)
{
char_u buf[MAXLINELEN];
aff_entry->ae_cond = getroom_save(spin, 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 + RE_STRICT);
if (aff_entry->ae_prog == NULL)
smsg((char_u *)_("Broken condition in %s line %d: %s"),
fname, lnum, items[4]);
}
if (*items[0] == 'P' && aff->af_pfxpostpone
&& aff_entry->ae_flags == NULL)
{
if (aff_entry->ae_chop != NULL
&& aff_entry->ae_add != NULL
#ifdef FEAT_MBYTE
&& aff_entry->ae_chop[(*mb_ptr2len)(
aff_entry->ae_chop)] == NUL
#else
&& aff_entry->ae_chop[1] == NUL
#endif
)
{
int c, c_up;
c = PTR2CHAR(aff_entry->ae_chop);
c_up = SPELL_TOUPPER(c);
if (c_up != c
&& (aff_entry->ae_cond == NULL
|| PTR2CHAR(aff_entry->ae_cond) == c))
{
p = aff_entry->ae_add
+ STRLEN(aff_entry->ae_add);
mb_ptr_back(aff_entry->ae_add, p);
if (PTR2CHAR(p) == c_up)
{
upper = TRUE;
aff_entry->ae_chop = NULL;
*p = NUL;
if (aff_entry->ae_cond != NULL)
{
char_u buf[MAXLINELEN];
#ifdef FEAT_MBYTE
if (has_mbyte)
{
onecap_copy(items[4], buf, TRUE);
aff_entry->ae_cond = getroom_save(
spin, buf);
}
else
#endif
*aff_entry->ae_cond = c_up;
if (aff_entry->ae_cond != NULL)
{
sprintf((char *)buf, "^%s",
aff_entry->ae_cond);
vim_free(aff_entry->ae_prog);
aff_entry->ae_prog = vim_regcomp(
buf, RE_MAGIC + RE_STRING);
}
}
}
}
}
if (aff_entry->ae_chop == NULL
&& aff_entry->ae_flags == NULL)
{
int idx;
char_u **pp;
int n;
for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
--idx)
{
p = ((char_u **)spin->si_prefcond.ga_data)[idx];
if (str_equal(p, aff_entry->ae_cond))
break;
}
if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
{
idx = spin->si_prefcond.ga_len++;
pp = ((char_u **)spin->si_prefcond.ga_data)
+ idx;
if (aff_entry->ae_cond == NULL)
*pp = NULL;
else
*pp = getroom_save(spin,
aff_entry->ae_cond);
}
if (aff_entry->ae_add == NULL)
p = (char_u *)"";
else
p = aff_entry->ae_add;
n = PFX_FLAGS;
if (!cur_aff->ah_combine)
n |= WFP_NC;
if (upper)
n |= WFP_UP;
if (aff_entry->ae_comppermit)
n |= WFP_COMPPERMIT;
if (aff_entry->ae_compforbid)
n |= WFP_COMPFORBID;
tree_add_word(spin, p, spin->si_prefroot, n,
idx, cur_aff->ah_newID);
did_postpone_prefix = TRUE;
}
if (aff_todo == 0 && !did_postpone_prefix)
{
--spin->si_newprefID;
cur_aff->ah_newID = 0;
}
}
}
}
else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
{
fol = vim_strsave(items[1]);
}
else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
{
low = vim_strsave(items[1]);
}
else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
{
upp = vim_strsave(items[1]);
}
else if (is_aff_rule(items, itemcnt, "REP", 2)
|| is_aff_rule(items, itemcnt, "REPSAL", 2))
{
;
if (!isdigit(*items[1]))
smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
fname, lnum);
}
else if ((STRCMP(items[0], "REP") == 0
|| STRCMP(items[0], "REPSAL") == 0)
&& itemcnt >= 3)
{
if (itemcnt > 3 && items[3][0] != '#')
smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
if (items[0][3] == 'S' ? do_repsal : do_rep)
{
for (p = items[1]; *p != NUL; mb_ptr_adv(p))
if (*p == '_')
*p = ' ';
for (p = items[2]; *p != NUL; mb_ptr_adv(p))
if (*p == '_')
*p = ' ';
add_fromto(spin, items[0][3] == 'S'
? &spin->si_repsal
: &spin->si_rep, items[1], items[2]);
}
}
else if (is_aff_rule(items, itemcnt, "MAP", 2))
{
if (!found_map)
{
found_map = TRUE;
if (!isdigit(*items[1]))
smsg((char_u *)_("Expected MAP count in %s line %d"),
fname, lnum);
}
else if (do_mapline)
{
int c;
for (p = items[1]; *p != NUL; )
{
#ifdef FEAT_MBYTE
c = mb_ptr2char_adv(&p);
#else
c = *p++;
#endif
if ((spin->si_map.ga_len > 0
&& vim_strchr(spin->si_map.ga_data, c)
!= NULL)
|| vim_strchr(p, c) != NULL)
smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
fname, lnum);
}
ga_concat(&spin->si_map, items[1]);
ga_append(&spin->si_map, '/');
}
}
else if (is_aff_rule(items, itemcnt, "SAL", 3))
{
if (do_sal)
{
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
add_fromto(spin, &spin->si_sal, items[1],
STRCMP(items[2], "_") == 0 ? (char_u *)""
: items[2]);
}
}
else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
&& sofofrom == NULL)
{
sofofrom = getroom_save(spin, items[1]);
}
else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
&& sofoto == NULL)
{
sofoto = getroom_save(spin, items[1]);
}
else if (STRCMP(items[0], "COMMON") == 0)
{
int i;
for (i = 1; i < itemcnt; ++i)
{
if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
items[i])))
{
p = vim_strsave(items[i]);
if (p == NULL)
break;
hash_add(&spin->si_commonwords, p);
}
}
}
else
smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
fname, lnum, items[0]);
}
}
if (fol != NULL || low != NULL || upp != NULL)
{
if (spin->si_clear_chartab)
{
init_spell_chartab();
spin->si_clear_chartab = FALSE;
}
if (!spin->si_ascii
#ifdef FEAT_MBYTE
&& !enc_utf8
#endif
)
{
if (fol == NULL || low == NULL || upp == NULL)
smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
else
(void)set_spell_chartab(fol, low, upp);
}
vim_free(fol);
vim_free(low);
vim_free(upp);
}
if (compmax != 0)
{
aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
spin->si_compmax = compmax;
}
if (compminlen != 0)
{
aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
spin->si_compminlen = compminlen;
}
if (compsylmax != 0)
{
if (syllable == NULL)
smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
spin->si_compsylmax = compsylmax;
}
if (compoptions != 0)
{
aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
spin->si_compoptions |= compoptions;
}
if (compflags != NULL)
process_compflags(spin, aff, compflags);
if (spin->si_newcompID < spin->si_newprefID)
{
if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
MSG(_("Too many postponed prefixes"));
else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
MSG(_("Too many compound flags"));
else
MSG(_("Too many postponed prefixes and/or compound flags"));
}
if (syllable != NULL)
{
aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
spin->si_syllable = syllable;
}
if (sofofrom != NULL || sofoto != NULL)
{
if (sofofrom == NULL || sofoto == NULL)
smsg((char_u *)_("Missing SOFO%s line in %s"),
sofofrom == NULL ? "FROM" : "TO", fname);
else if (spin->si_sal.ga_len > 0)
smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
else
{
aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
spin->si_sofofr = sofofrom;
spin->si_sofoto = sofoto;
}
}
if (midword != NULL)
{
aff_check_string(spin->si_midword, midword, "MIDWORD");
spin->si_midword = midword;
}
vim_free(pc);
fclose(fd);
return aff;
}
static int
is_aff_rule(items, itemcnt, rulename, mincount)
char_u **items;
int itemcnt;
char *rulename;
int mincount;
{
return (STRCMP(items[0], rulename) == 0
&& (itemcnt == mincount
|| (itemcnt > mincount && items[mincount][0] == '#')));
}
static void
aff_process_flags(affile, entry)
afffile_T *affile;
affentry_T *entry;
{
char_u *p;
char_u *prevp;
unsigned flag;
if (entry->ae_flags != NULL
&& (affile->af_compforbid != 0 || affile->af_comppermit != 0))
{
for (p = entry->ae_flags; *p != NUL; )
{
prevp = p;
flag = get_affitem(affile->af_flagtype, &p);
if (flag == affile->af_comppermit || flag == affile->af_compforbid)
{
STRMOVE(prevp, p);
p = prevp;
if (flag == affile->af_comppermit)
entry->ae_comppermit = TRUE;
else
entry->ae_compforbid = TRUE;
}
if (affile->af_flagtype == AFT_NUM && *p == ',')
++p;
}
if (*entry->ae_flags == NUL)
entry->ae_flags = NULL;
}
}
static int
spell_info_item(s)
char_u *s;
{
return STRCMP(s, "NAME") == 0
|| STRCMP(s, "HOME") == 0
|| STRCMP(s, "VERSION") == 0
|| STRCMP(s, "AUTHOR") == 0
|| STRCMP(s, "EMAIL") == 0
|| STRCMP(s, "COPYRIGHT") == 0;
}
static unsigned
affitem2flag(flagtype, item, fname, lnum)
int flagtype;
char_u *item;
char_u *fname;
int lnum;
{
unsigned res;
char_u *p = item;
res = get_affitem(flagtype, &p);
if (res == 0)
{
if (flagtype == AFT_NUM)
smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
fname, lnum, item);
else
smsg((char_u *)_("Illegal flag in %s line %d: %s"),
fname, lnum, item);
}
if (*p != NUL)
{
smsg((char_u *)_(e_affname), fname, lnum, item);
return 0;
}
return res;
}
static unsigned
get_affitem(flagtype, pp)
int flagtype;
char_u **pp;
{
int res;
if (flagtype == AFT_NUM)
{
if (!VIM_ISDIGIT(**pp))
{
++*pp;
return 0;
}
res = getdigits(pp);
}
else
{
#ifdef FEAT_MBYTE
res = mb_ptr2char_adv(pp);
#else
res = *(*pp)++;
#endif
if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
&& res >= 'A' && res <= 'Z'))
{
if (**pp == NUL)
return 0;
#ifdef FEAT_MBYTE
res = mb_ptr2char_adv(pp) + (res << 16);
#else
res = *(*pp)++ + (res << 16);
#endif
}
}
return res;
}
static void
process_compflags(spin, aff, compflags)
spellinfo_T *spin;
afffile_T *aff;
char_u *compflags;
{
char_u *p;
char_u *prevp;
unsigned flag;
compitem_T *ci;
int id;
int len;
char_u *tp;
char_u key[AH_KEY_LEN];
hashitem_T *hi;
len = (int)STRLEN(compflags) + 1;
if (spin->si_compflags != NULL)
len += (int)STRLEN(spin->si_compflags) + 1;
p = getroom(spin, len, FALSE);
if (p == NULL)
return;
if (spin->si_compflags != NULL)
{
STRCPY(p, spin->si_compflags);
STRCAT(p, "/");
}
spin->si_compflags = p;
tp = p + STRLEN(p);
for (p = compflags; *p != NUL; )
{
if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
*tp++ = *p++;
else
{
prevp = p;
flag = get_affitem(aff->af_flagtype, &p);
if (flag != 0)
{
vim_strncpy(key, prevp, p - prevp);
hi = hash_find(&aff->af_comp, key);
if (!HASHITEM_EMPTY(hi))
id = HI2CI(hi)->ci_newID;
else
{
ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
if (ci == NULL)
break;
STRCPY(ci->ci_key, key);
ci->ci_flag = flag;
do
{
check_renumber(spin);
id = spin->si_newcompID--;
} while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
ci->ci_newID = id;
hash_add(&aff->af_comp, ci->ci_key);
}
*tp++ = id;
}
if (aff->af_flagtype == AFT_NUM && *p == ',')
++p;
}
}
*tp = NUL;
}
static void
check_renumber(spin)
spellinfo_T *spin;
{
if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
{
spin->si_newprefID = 127;
spin->si_newcompID = 255;
}
}
static int
flag_in_afflist(flagtype, afflist, flag)
int flagtype;
char_u *afflist;
unsigned flag;
{
char_u *p;
unsigned n;
switch (flagtype)
{
case AFT_CHAR:
return vim_strchr(afflist, flag) != NULL;
case AFT_CAPLONG:
case AFT_LONG:
for (p = afflist; *p != NUL; )
{
#ifdef FEAT_MBYTE
n = mb_ptr2char_adv(&p);
#else
n = *p++;
#endif
if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
&& *p != NUL)
#ifdef FEAT_MBYTE
n = mb_ptr2char_adv(&p) + (n << 16);
#else
n = *p++ + (n << 16);
#endif
if (n == flag)
return TRUE;
}
break;
case AFT_NUM:
for (p = afflist; *p != NUL; )
{
n = getdigits(&p);
if (n == flag)
return TRUE;
if (*p != NUL)
++p;
}
break;
}
return FALSE;
}
static void
aff_check_number(spinval, affval, name)
int spinval;
int affval;
char *name;
{
if (spinval != 0 && spinval != affval)
smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
}
static void
aff_check_string(spinval, affval, name)
char_u *spinval;
char_u *affval;
char *name;
{
if (spinval != NULL && STRCMP(spinval, affval) != 0)
smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
}
static int
str_equal(s1, s2)
char_u *s1;
char_u *s2;
{
if (s1 == NULL || s2 == NULL)
return s1 == s2;
return STRCMP(s1, s2) == 0;
}
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, (int)STRLEN(from), word, MAXWLEN);
ftp->ft_from = getroom_save(spin, word);
(void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
ftp->ft_to = getroom_save(spin, word);
++gap->ga_len;
}
}
static int
sal_to_bool(s)
char_u *s;
{
return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
}
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;
}
static void
spell_free_aff(aff)
afffile_T *aff;
{
hashtab_T *ht;
hashitem_T *hi;
int todo;
affheader_T *ah;
affentry_T *ae;
vim_free(aff->af_enc);
for (ht = &aff->af_pref; ; ht = &aff->af_suff)
{
todo = (int)ht->ht_used;
for (hi = ht->ht_array; todo > 0; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
ah = HI2AH(hi);
for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
vim_free(ae->ae_prog);
}
}
if (ht == &aff->af_suff)
break;
}
hash_clear(&aff->af_pref);
hash_clear(&aff->af_suff);
hash_clear(&aff->af_comp);
}
static int
spell_read_dic(spin, fname, affile)
spellinfo_T *spin;
char_u *fname;
afffile_T *affile;
{
hashtab_T ht;
char_u line[MAXLINELEN];
char_u *p;
char_u *afflist;
char_u store_afflist[MAXWLEN];
int pfxlen;
int need_affix;
char_u *dw;
char_u *pc;
char_u *w;
int l;
hash_T hash;
hashitem_T *hi;
FILE *fd;
int lnum = 1;
int non_ascii = 0;
int retval = OK;
char_u message[MAXLINELEN + MAXWLEN];
int flags;
int duplicate = 0;
fd = mch_fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return FAIL;
}
hash_init(&ht);
vim_snprintf((char *)IObuff, IOSIZE,
_("Reading dictionary file %s ..."), fname);
spell_message(spin, IObuff);
spin->si_msg_count = 999999;
(void)vim_fgets(line, MAXLINELEN, fd);
if (!vim_isdigit(*skipwhite(line)))
EMSG2(_("E760: No word count in %s"), fname);
while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
{
line_breakcheck();
++lnum;
if (line[0] == '#' || line[0] == '/')
continue;
l = (int)STRLEN(line);
while (l > 0 && line[l - 1] <= ' ')
--l;
if (l == 0)
continue;
line[l] = NUL;
#ifdef FEAT_MBYTE
if (spin->si_conv.vc_type != CONV_NONE)
{
pc = string_convert(&spin->si_conv, line, NULL);
if (pc == NULL)
{
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
fname, lnum, line);
continue;
}
w = pc;
}
else
#endif
{
pc = NULL;
w = line;
}
afflist = NULL;
for (p = w; *p != NUL; mb_ptr_adv(p))
{
if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
STRMOVE(p, p + 1);
else if (*p == '/')
{
*p = NUL;
afflist = p + 1;
break;
}
}
if (spin->si_ascii && has_non_ascii(w))
{
++non_ascii;
vim_free(pc);
continue;
}
if (spin->si_verbose && spin->si_msg_count > 10000)
{
spin->si_msg_count = 0;
vim_snprintf((char *)message, sizeof(message),
_("line %6d, word %6d - %s"),
lnum, spin->si_foldwcount + spin->si_keepwcount, w);
msg_start();
msg_puts_long_attr(message, 0);
msg_clr_eos();
msg_didout = FALSE;
msg_col = 0;
out_flush();
}
dw = (char_u *)getroom_save(spin, w);
if (dw == NULL)
{
retval = FAIL;
vim_free(pc);
break;
}
hash = hash_hash(dw);
hi = hash_lookup(&ht, dw, hash);
if (!HASHITEM_EMPTY(hi))
{
if (p_verbose > 0)
smsg((char_u *)_("Duplicate word in %s line %d: %s"),
fname, lnum, dw);
else if (duplicate == 0)
smsg((char_u *)_("First duplicate word in %s line %d: %s"),
fname, lnum, dw);
++duplicate;
}
else
hash_add_item(&ht, hi, dw, hash);
flags = 0;
store_afflist[0] = NUL;
pfxlen = 0;
need_affix = FALSE;
if (afflist != NULL)
{
flags |= get_affix_flags(affile, afflist);
if (affile->af_needaffix != 0 && flag_in_afflist(
affile->af_flagtype, afflist, affile->af_needaffix))
need_affix = TRUE;
if (affile->af_pfxpostpone)
pfxlen = get_pfxlist(affile, afflist, store_afflist);
if (spin->si_compflags != NULL)
get_compflags(affile, afflist, store_afflist + pfxlen);
}
if (store_word(spin, dw, flags, spin->si_region,
store_afflist, need_affix) == FAIL)
retval = FAIL;
if (afflist != NULL)
{
if (store_aff_word(spin, dw, afflist, affile,
&affile->af_suff, &affile->af_pref,
CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
retval = FAIL;
if (store_aff_word(spin, dw, afflist, affile,
&affile->af_pref, NULL,
CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
retval = FAIL;
}
vim_free(pc);
}
if (duplicate > 0)
smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
if (spin->si_ascii && non_ascii > 0)
smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
non_ascii, fname);
hash_clear(&ht);
fclose(fd);
return retval;
}
static int
get_affix_flags(affile, afflist)
afffile_T *affile;
char_u *afflist;
{
int flags = 0;
if (affile->af_keepcase != 0 && flag_in_afflist(
affile->af_flagtype, afflist, affile->af_keepcase))
flags |= WF_KEEPCAP | WF_FIXCAP;
if (affile->af_rare != 0 && flag_in_afflist(
affile->af_flagtype, afflist, affile->af_rare))
flags |= WF_RARE;
if (affile->af_bad != 0 && flag_in_afflist(
affile->af_flagtype, afflist, affile->af_bad))
flags |= WF_BANNED;
if (affile->af_needcomp != 0 && flag_in_afflist(
affile->af_flagtype, afflist, affile->af_needcomp))
flags |= WF_NEEDCOMP;
if (affile->af_comproot != 0 && flag_in_afflist(
affile->af_flagtype, afflist, affile->af_comproot))
flags |= WF_COMPROOT;
if (affile->af_nosuggest != 0 && flag_in_afflist(
affile->af_flagtype, afflist, affile->af_nosuggest))
flags |= WF_NOSUGGEST;
return flags;
}
static int
get_pfxlist(affile, afflist, store_afflist)
afffile_T *affile;
char_u *afflist;
char_u *store_afflist;
{
char_u *p;
char_u *prevp;
int cnt = 0;
int id;
char_u key[AH_KEY_LEN];
hashitem_T *hi;
for (p = afflist; *p != NUL; )
{
prevp = p;
if (get_affitem(affile->af_flagtype, &p) != 0)
{
vim_strncpy(key, prevp, p - prevp);
hi = hash_find(&affile->af_pref, key);
if (!HASHITEM_EMPTY(hi))
{
id = HI2AH(hi)->ah_newID;
if (id != 0)
store_afflist[cnt++] = id;
}
}
if (affile->af_flagtype == AFT_NUM && *p == ',')
++p;
}
store_afflist[cnt] = NUL;
return cnt;
}
static void
get_compflags(affile, afflist, store_afflist)
afffile_T *affile;
char_u *afflist;
char_u *store_afflist;
{
char_u *p;
char_u *prevp;
int cnt = 0;
char_u key[AH_KEY_LEN];
hashitem_T *hi;
for (p = afflist; *p != NUL; )
{
prevp = p;
if (get_affitem(affile->af_flagtype, &p) != 0)
{
vim_strncpy(key, prevp, p - prevp);
hi = hash_find(&affile->af_comp, key);
if (!HASHITEM_EMPTY(hi))
store_afflist[cnt++] = HI2CI(hi)->ci_newID;
}
if (affile->af_flagtype == AFT_NUM && *p == ',')
++p;
}
store_afflist[cnt] = NUL;
}
static int
store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
pfxlist, pfxlen)
spellinfo_T *spin;
char_u *word;
char_u *afflist;
afffile_T *affile;
hashtab_T *ht;
hashtab_T *xht;
int condit;
int flags;
char_u *pfxlist;
int pfxlen;
{
int todo;
hashitem_T *hi;
affheader_T *ah;
affentry_T *ae;
regmatch_T regmatch;
char_u newword[MAXWLEN];
int retval = OK;
int i, j;
char_u *p;
int use_flags;
char_u *use_pfxlist;
int use_pfxlen;
int need_affix;
char_u store_afflist[MAXWLEN];
char_u pfx_pfxlist[MAXWLEN];
size_t wordlen = STRLEN(word);
int use_condit;
todo = (int)ht->ht_used;
for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
ah = HI2AH(hi);
if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
&& flag_in_afflist(affile->af_flagtype, afflist,
ah->ah_flag))
{
for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
{
regmatch.regprog = ae->ae_prog;
regmatch.rm_ic = FALSE;
if ((xht != NULL || !affile->af_pfxpostpone
|| ae->ae_chop != NULL
|| ae->ae_flags != NULL)
&& (ae->ae_chop == NULL
|| STRLEN(ae->ae_chop) < wordlen)
&& (ae->ae_prog == NULL
|| vim_regexec(®match, word, (colnr_T)0))
&& (((condit & CONDIT_CFIX) == 0)
== ((condit & CONDIT_AFF) == 0
|| ae->ae_flags == NULL
|| !flag_in_afflist(affile->af_flagtype,
ae->ae_flags, affile->af_circumfix))))
{
if (xht == NULL)
{
if (ae->ae_add == NULL)
*newword = NUL;
else
STRCPY(newword, ae->ae_add);
p = word;
if (ae->ae_chop != NULL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
i = mb_charlen(ae->ae_chop);
for ( ; i > 0; --i)
mb_ptr_adv(p);
}
else
#endif
p += STRLEN(ae->ae_chop);
}
STRCAT(newword, p);
}
else
{
STRCPY(newword, word);
if (ae->ae_chop != NULL)
{
p = newword + STRLEN(newword);
i = (int)MB_CHARLEN(ae->ae_chop);
for ( ; i > 0; --i)
mb_ptr_back(newword, p);
*p = NUL;
}
if (ae->ae_add != NULL)
STRCAT(newword, ae->ae_add);
}
use_flags = flags;
use_pfxlist = pfxlist;
use_pfxlen = pfxlen;
need_affix = FALSE;
use_condit = condit | CONDIT_COMB | CONDIT_AFF;
if (ae->ae_flags != NULL)
{
use_flags |= get_affix_flags(affile, ae->ae_flags);
if (affile->af_needaffix != 0 && flag_in_afflist(
affile->af_flagtype, ae->ae_flags,
affile->af_needaffix))
need_affix = TRUE;
if (affile->af_circumfix != 0 && flag_in_afflist(
affile->af_flagtype, ae->ae_flags,
affile->af_circumfix))
{
use_condit |= CONDIT_CFIX;
if ((condit & CONDIT_CFIX) == 0)
need_affix = TRUE;
}
if (affile->af_pfxpostpone
|| spin->si_compflags != NULL)
{
if (affile->af_pfxpostpone)
use_pfxlen = get_pfxlist(affile,
ae->ae_flags, store_afflist);
else
use_pfxlen = 0;
use_pfxlist = store_afflist;
for (i = 0; i < pfxlen; ++i)
{
for (j = 0; j < use_pfxlen; ++j)
if (pfxlist[i] == use_pfxlist[j])
break;
if (j == use_pfxlen)
use_pfxlist[use_pfxlen++] = pfxlist[i];
}
if (spin->si_compflags != NULL)
get_compflags(affile, ae->ae_flags,
use_pfxlist + use_pfxlen);
for (i = pfxlen; pfxlist[i] != NUL; ++i)
{
for (j = use_pfxlen;
use_pfxlist[j] != NUL; ++j)
if (pfxlist[i] == use_pfxlist[j])
break;
if (use_pfxlist[j] == NUL)
{
use_pfxlist[j++] = pfxlist[i];
use_pfxlist[j] = NUL;
}
}
}
}
if (use_pfxlist != NULL && ae->ae_compforbid)
{
vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
use_pfxlist = pfx_pfxlist;
}
if (spin->si_prefroot != NULL
&& spin->si_prefroot->wn_sibling != NULL)
{
use_flags |= WF_HAS_AFF;
if (!ah->ah_combine && use_pfxlist != NULL)
use_pfxlist += use_pfxlen;
}
if (spin->si_compflags != NULL && !ae->ae_comppermit)
{
if (xht != NULL)
use_flags |= WF_NOCOMPAFT;
else
use_flags |= WF_NOCOMPBEF;
}
if (store_word(spin, newword, use_flags,
spin->si_region, use_pfxlist,
need_affix) == FAIL)
retval = FAIL;
if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
if (store_aff_word(spin, newword, ae->ae_flags,
affile, &affile->af_suff, xht,
use_condit & (xht == NULL
? ~0 : ~CONDIT_SUF),
use_flags, use_pfxlist, pfxlen) == FAIL)
retval = FAIL;
if (xht != NULL && ah->ah_combine)
{
if (store_aff_word(spin, newword,
afflist, affile,
xht, NULL, use_condit,
use_flags, use_pfxlist,
pfxlen) == FAIL
|| (ae->ae_flags != NULL
&& store_aff_word(spin, newword,
ae->ae_flags, affile,
xht, NULL, use_condit,
use_flags, use_pfxlist,
pfxlen) == FAIL))
retval = FAIL;
}
}
}
}
}
}
return retval;
}
static int
spell_read_wordfile(spin, fname)
spellinfo_T *spin;
char_u *fname;
{
FILE *fd;
long lnum = 0;
char_u rline[MAXLINELEN];
char_u *line;
char_u *pc = NULL;
char_u *p;
int l;
int retval = OK;
int did_word = FALSE;
int non_ascii = 0;
int flags;
int regionmask;
fd = mch_fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return FAIL;
}
vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
spell_message(spin, IObuff);
while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
{
line_breakcheck();
++lnum;
if (*rline == '#')
continue;
l = (int)STRLEN(rline);
while (l > 0 && rline[l - 1] <= ' ')
--l;
if (l == 0)
continue;
rline[l] = NUL;
vim_free(pc);
#ifdef FEAT_MBYTE
if (spin->si_conv.vc_type != CONV_NONE)
{
pc = string_convert(&spin->si_conv, rline, NULL);
if (pc == NULL)
{
smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
fname, lnum, rline);
continue;
}
line = pc;
}
else
#endif
{
pc = NULL;
line = rline;
}
if (*line == '/')
{
++line;
if (STRNCMP(line, "encoding=", 9) == 0)
{
if (spin->si_conv.vc_type != CONV_NONE)
smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
fname, lnum, line - 1);
else if (did_word)
smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
fname, lnum, line - 1);
else
{
#ifdef FEAT_MBYTE
char_u *enc;
line += 9;
enc = enc_canonize(line);
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"),
fname, line, p_enc);
vim_free(enc);
spin->si_conv.vc_fail = TRUE;
#else
smsg((char_u *)_("Conversion in %s not supported"), fname);
#endif
}
continue;
}
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 = (int)STRLEN(line) / 2;
STRCPY(spin->si_region_name, line);
spin->si_region = (1 << spin->si_region_count) - 1;
}
}
continue;
}
smsg((char_u *)_("/ line ignored in %s line %d: %s"),
fname, lnum, line - 1);
continue;
}
flags = 0;
regionmask = spin->si_region;
p = vim_strchr(line, '/');
if (p != NULL)
{
*p++ = NUL;
while (*p != NUL)
{
if (*p == '=')
flags |= WF_KEEPCAP | WF_FIXCAP;
else if (*p == '!')
flags |= WF_BANNED;
else if (*p == '?')
flags |= WF_RARE;
else if (VIM_ISDIGIT(*p))
{
if ((flags & WF_REGION) == 0)
regionmask = 0;
flags |= WF_REGION;
l = *p - '0';
if (l > spin->si_region_count)
{
smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
fname, lnum, p);
break;
}
regionmask |= 1 << (l - 1);
}
else
{
smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
fname, lnum, p);
break;
}
++p;
}
}
if (spin->si_ascii && has_non_ascii(line))
{
++non_ascii;
continue;
}
if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
{
retval = FAIL;
break;
}
did_word = TRUE;
}
vim_free(pc);
fclose(fd);
if (spin->si_ascii && non_ascii > 0)
{
vim_snprintf((char *)IObuff, IOSIZE,
_("Ignored %d words with non-ASCII characters"), non_ascii);
spell_message(spin, IObuff);
}
return retval;
}
static void *
getroom(spin, len, align)
spellinfo_T *spin;
size_t len;
int align;
{
char_u *p;
sblock_T *bl = spin->si_blocks;
if (align && bl != NULL)
bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
& ~(sizeof(char *) - 1);
if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
{
bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
if (bl == NULL)
return NULL;
bl->sb_next = spin->si_blocks;
spin->si_blocks = bl;
bl->sb_used = 0;
++spin->si_blocks_cnt;
}
p = bl->sb_data + bl->sb_used;
bl->sb_used += (int)len;
return p;
}
static char_u *
getroom_save(spin, s)
spellinfo_T *spin;
char_u *s;
{
char_u *sc;
sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
if (sc != NULL)
STRCPY(sc, s);
return sc;
}
static void
free_blocks(bl)
sblock_T *bl;
{
sblock_T *next;
while (bl != NULL)
{
next = bl->sb_next;
vim_free(bl);
bl = next;
}
}
static wordnode_T *
wordtree_alloc(spin)
spellinfo_T *spin;
{
return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
}
static int
store_word(spin, word, flags, region, pfxlist, need_affix)
spellinfo_T *spin;
char_u *word;
int flags;
int region;
char_u *pfxlist;
int need_affix;
{
int len = (int)STRLEN(word);
int ct = captype(word, word + len);
char_u foldword[MAXWLEN];
int res = OK;
char_u *p;
(void)spell_casefold(word, len, foldword, MAXWLEN);
for (p = pfxlist; res == OK; ++p)
{
if (!need_affix || (p != NULL && *p != NUL))
res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
region, p == NULL ? 0 : *p);
if (p == NULL || *p == NUL)
break;
}
++spin->si_foldwcount;
if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
{
for (p = pfxlist; res == OK; ++p)
{
if (!need_affix || (p != NULL && *p != NUL))
res = tree_add_word(spin, word, spin->si_keeproot, flags,
region, p == NULL ? 0 : *p);
if (p == NULL || *p == NUL)
break;
}
++spin->si_keepwcount;
}
return res;
}
static int
tree_add_word(spin, word, root, flags, region, affixID)
spellinfo_T *spin;
char_u *word;
wordnode_T *root;
int flags;
int region;
int affixID;
{
wordnode_T *node = root;
wordnode_T *np;
wordnode_T *copyp, **copyprev;
wordnode_T **prev = NULL;
int i;
for (i = 0; ; ++i)
{
if (node != NULL && node->wn_refs > 1)
{
--node->wn_refs;
copyprev = prev;
for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
{
np = get_wordnode(spin);
if (np == NULL)
return FAIL;
np->wn_child = copyp->wn_child;
if (np->wn_child != NULL)
++np->wn_child->wn_refs;
np->wn_byte = copyp->wn_byte;
if (np->wn_byte == NUL)
{
np->wn_flags = copyp->wn_flags;
np->wn_region = copyp->wn_region;
np->wn_affixID = copyp->wn_affixID;
}
np->wn_refs = 1;
if (copyprev != NULL)
*copyprev = np;
copyprev = &np->wn_sibling;
if (copyp == node)
node = np;
}
}
while (node != NULL
&& (node->wn_byte < word[i]
|| (node->wn_byte == NUL
&& (flags < 0
? node->wn_affixID < (unsigned)affixID
: (node->wn_flags < (unsigned)(flags & WN_MASK)
|| (node->wn_flags == (flags & WN_MASK)
&& (spin->si_sugtree
? (node->wn_region & 0xffff) < region
: node->wn_affixID
< (unsigned)affixID)))))))
{
prev = &node->wn_sibling;
node = *prev;
}
if (node == NULL
|| node->wn_byte != word[i]
|| (word[i] == NUL
&& (flags < 0
|| spin->si_sugtree
|| node->wn_flags != (flags & WN_MASK)
|| node->wn_affixID != affixID)))
{
np = get_wordnode(spin);
if (np == NULL)
return FAIL;
np->wn_byte = word[i];
if (node == NULL)
np->wn_refs = 1;
else
{
np->wn_refs = node->wn_refs;
node->wn_refs = 1;
}
if (prev != NULL)
*prev = np;
np->wn_sibling = node;
node = np;
}
if (word[i] == NUL)
{
node->wn_flags = flags;
node->wn_region |= region;
node->wn_affixID = affixID;
break;
}
prev = &node->wn_child;
node = *prev;
}
#ifdef SPELL_PRINTTREE
smsg("Added \"%s\"", word);
spell_print_tree(root->wn_sibling);
#endif
++spin->si_msg_count;
if (spin->si_compress_cnt > 1)
{
if (--spin->si_compress_cnt == 1)
spin->si_blocks_cnt += compress_inc;
}
#ifndef SPELL_PRINTTREE
if (spin->si_compress_cnt == 1
? spin->si_free_count < MAXWLEN
: spin->si_blocks_cnt >= compress_start)
#endif
{
spin->si_blocks_cnt -= compress_inc;
spin->si_compress_cnt = compress_added;
if (spin->si_verbose)
{
msg_start();
msg_puts((char_u *)_(msg_compressing));
msg_clr_eos();
msg_didout = FALSE;
msg_col = 0;
out_flush();
}
wordtree_compress(spin, spin->si_foldroot);
if (affixID >= 0)
wordtree_compress(spin, spin->si_keeproot);
}
return OK;
}
int
spell_check_msm()
{
char_u *p = p_msm;
long start = 0;
long incr = 0;
long added = 0;
if (!VIM_ISDIGIT(*p))
return FAIL;
start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
if (*p != ',')
return FAIL;
++p;
if (!VIM_ISDIGIT(*p))
return FAIL;
incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
if (*p != ',')
return FAIL;
++p;
if (!VIM_ISDIGIT(*p))
return FAIL;
added = getdigits(&p) * 1024;
if (*p != NUL)
return FAIL;
if (start == 0 || incr == 0 || added == 0 || incr > start)
return FAIL;
compress_start = start;
compress_inc = incr;
compress_added = added;
return OK;
}
static wordnode_T *
get_wordnode(spin)
spellinfo_T *spin;
{
wordnode_T *n;
if (spin->si_first_free == NULL)
n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
else
{
n = spin->si_first_free;
spin->si_first_free = n->wn_child;
vim_memset(n, 0, sizeof(wordnode_T));
--spin->si_free_count;
}
#ifdef SPELL_PRINTTREE
n->wn_nr = ++spin->si_wordnode_nr;
#endif
return n;
}
static int
deref_wordnode(spin, node)
spellinfo_T *spin;
wordnode_T *node;
{
wordnode_T *np;
int cnt = 0;
if (--node->wn_refs == 0)
{
for (np = node; np != NULL; np = np->wn_sibling)
{
if (np->wn_child != NULL)
cnt += deref_wordnode(spin, np->wn_child);
free_wordnode(spin, np);
++cnt;
}
++cnt;
}
return cnt;
}
static void
free_wordnode(spin, n)
spellinfo_T *spin;
wordnode_T *n;
{
n->wn_child = spin->si_first_free;
spin->si_first_free = n;
++spin->si_free_count;
}
static void
wordtree_compress(spin, root)
spellinfo_T *spin;
wordnode_T *root;
{
hashtab_T ht;
int n;
int tot = 0;
int perc;
if (root->wn_sibling != NULL)
{
hash_init(&ht);
n = node_compress(spin, root->wn_sibling, &ht, &tot);
#ifndef SPELL_PRINTTREE
if (spin->si_verbose || p_verbose > 2)
#endif
{
if (tot > 1000000)
perc = (tot - n) / (tot / 100);
else if (tot == 0)
perc = 0;
else
perc = (tot - n) * 100 / tot;
vim_snprintf((char *)IObuff, IOSIZE,
_("Compressed %d of %d nodes; %d (%d%%) remaining"),
n, tot, tot - n, perc);
spell_message(spin, IObuff);
}
#ifdef SPELL_PRINTTREE
spell_print_tree(root->wn_sibling);
#endif
hash_clear(&ht);
}
}
static int
node_compress(spin, node, ht, tot)
spellinfo_T *spin;
wordnode_T *node;
hashtab_T *ht;
int *tot;
{
wordnode_T *np;
wordnode_T *tp;
wordnode_T *child;
hash_T hash;
hashitem_T *hi;
int len = 0;
unsigned nr, n;
int compressed = 0;
for (np = node; np != NULL && !got_int; np = np->wn_sibling)
{
++len;
if ((child = np->wn_child) != NULL)
{
compressed += node_compress(spin, child, ht, tot);
hash = hash_hash(child->wn_u1.hashkey);
hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
if (!HASHITEM_EMPTY(hi))
{
for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
if (node_equal(child, tp))
{
++tp->wn_refs;
compressed += deref_wordnode(spin, child);
np->wn_child = tp;
break;
}
if (tp == NULL)
{
tp = HI2WN(hi);
child->wn_u2.next = tp->wn_u2.next;
tp->wn_u2.next = child;
}
}
else
hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
}
}
*tot += len + 1;
node->wn_u1.hashkey[0] = len;
nr = 0;
for (np = node; np != NULL; np = np->wn_sibling)
{
if (np->wn_byte == NUL)
n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
else
n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
nr = nr * 101 + n;
}
n = nr & 0xff;
node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
n = (nr >> 8) & 0xff;
node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
n = (nr >> 16) & 0xff;
node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
n = (nr >> 24) & 0xff;
node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
node->wn_u1.hashkey[5] = NUL;
fast_breakcheck();
return compressed;
}
static int
node_equal(n1, n2)
wordnode_T *n1;
wordnode_T *n2;
{
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_affixID != p2->wn_affixID)
: (p1->wn_child != p2->wn_child)))
break;
return p1 == NULL && p2 == NULL;
}
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
rep_compare __ARGS((const void *s1, const void *s2));
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);
}
static int
write_vim_spell(spin, fname)
spellinfo_T *spin;
char_u *fname;
{
FILE *fd;
int regionmask;
int round;
wordnode_T *tree;
int nodecount;
int i;
int l;
garray_T *gap;
fromto_T *ftp;
char_u *p;
int rr;
int retval = OK;
size_t fwv = 1;
fd = mch_fopen((char *)fname, "w");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return FAIL;
}
fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
if (fwv != (size_t)1)
goto theend;
putc(VIMSPELLVERSION, fd);
if (spin->si_info != NULL)
{
putc(SN_INFO, fd);
putc(0, fd);
i = (int)STRLEN(spin->si_info);
put_bytes(fd, (long_u)i, 4);
fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd);
}
if (spin->si_region_count > 1)
{
putc(SN_REGION, fd);
putc(SNF_REQUIRED, fd);
l = spin->si_region_count * 2;
put_bytes(fd, (long_u)l, 4);
fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
regionmask = (1 << spin->si_region_count) - 1;
}
else
regionmask = 0;
if (!spin->si_ascii && !spin->si_add)
{
char_u folchars[128 * 8];
int flags;
putc(SN_CHARFLAGS, fd);
putc(SNF_REQUIRED, fd);
l = 0;
for (i = 128; i < 256; ++i)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
else
#endif
folchars[l++] = spelltab.st_fold[i];
}
put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4);
fputc(128, fd);
for (i = 128; i < 256; ++i)
{
flags = 0;
if (spelltab.st_isw[i])
flags |= CF_WORD;
if (spelltab.st_isu[i])
flags |= CF_UPPER;
fputc(flags, fd);
}
put_bytes(fd, (long_u)l, 2);
fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd);
}
if (spin->si_midword != NULL)
{
putc(SN_MIDWORD, fd);
putc(SNF_REQUIRED, fd);
i = (int)STRLEN(spin->si_midword);
put_bytes(fd, (long_u)i, 4);
fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
}
if (spin->si_prefcond.ga_len > 0)
{
putc(SN_PREFCOND, fd);
putc(SNF_REQUIRED, fd);
l = write_spell_prefcond(NULL, &spin->si_prefcond);
put_bytes(fd, (long_u)l, 4);
write_spell_prefcond(fd, &spin->si_prefcond);
}
for (round = 1; round <= 3; ++round)
{
if (round == 1)
gap = &spin->si_rep;
else if (round == 2)
{
if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
continue;
gap = &spin->si_sal;
}
else
gap = &spin->si_repsal;
if (gap->ga_len == 0)
continue;
if (round != 2)
qsort(gap->ga_data, (size_t)gap->ga_len,
sizeof(fromto_T), rep_compare);
i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
putc(i, fd);
putc(0, fd);
l = 2;
for (i = 0; i < gap->ga_len; ++i)
{
ftp = &((fromto_T *)gap->ga_data)[i];
l += 1 + (int)STRLEN(ftp->ft_from);
l += 1 + (int)STRLEN(ftp->ft_to);
}
if (round == 2)
++l;
put_bytes(fd, (long_u)l, 4);
if (round == 2)
{
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);
}
put_bytes(fd, (long_u)gap->ga_len, 2);
for (i = 0; i < gap->ga_len; ++i)
{
ftp = &((fromto_T *)gap->ga_data)[i];
for (rr = 1; rr <= 2; ++rr)
{
p = rr == 1 ? ftp->ft_from : ftp->ft_to;
l = (int)STRLEN(p);
putc(l, fd);
if (l > 0)
fwv &= fwrite(p, l, (size_t)1, fd);
}
}
}
if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
{
putc(SN_SOFO, fd);
putc(0, fd);
l = (int)STRLEN(spin->si_sofofr);
put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
put_bytes(fd, (long_u)l, 2);
fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd);
l = (int)STRLEN(spin->si_sofoto);
put_bytes(fd, (long_u)l, 2);
fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd);
}
if (spin->si_commonwords.ht_used > 0)
{
putc(SN_WORDS, fd);
putc(0, fd);
for (round = 1; round <= 2; ++round)
{
int todo;
int len = 0;
hashitem_T *hi;
todo = (int)spin->si_commonwords.ht_used;
for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
if (!HASHITEM_EMPTY(hi))
{
l = (int)STRLEN(hi->hi_key) + 1;
len += l;
if (round == 2)
fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
--todo;
}
if (round == 1)
put_bytes(fd, (long_u)len, 4);
}
}
if (spin->si_map.ga_len > 0)
{
putc(SN_MAP, fd);
putc(0, fd);
l = spin->si_map.ga_len;
put_bytes(fd, (long_u)l, 4);
fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
}
if (!spin->si_nosugfile
&& (spin->si_sal.ga_len > 0
|| (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
{
putc(SN_SUGFILE, fd);
putc(0, fd);
put_bytes(fd, (long_u)8, 4);
spin->si_sugtime = time(NULL);
put_time(fd, spin->si_sugtime);
}
if (spin->si_nosplitsugs)
{
putc(SN_NOSPLITSUGS, fd);
putc(0, fd);
put_bytes(fd, (long_u)0, 4);
}
if (spin->si_compflags != NULL)
{
putc(SN_COMPOUND, fd);
putc(0, fd);
l = (int)STRLEN(spin->si_compflags);
for (i = 0; i < spin->si_comppat.ga_len; ++i)
l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
put_bytes(fd, (long_u)(l + 7), 4);
putc(spin->si_compmax, fd);
putc(spin->si_compminlen, fd);
putc(spin->si_compsylmax, fd);
putc(0, fd);
putc(spin->si_compoptions, fd);
put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
for (i = 0; i < spin->si_comppat.ga_len; ++i)
{
p = ((char_u **)(spin->si_comppat.ga_data))[i];
putc((int)STRLEN(p), fd);
fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
}
fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
(size_t)1, fd);
}
if (spin->si_nobreak)
{
putc(SN_NOBREAK, fd);
putc(0, fd);
put_bytes(fd, (long_u)0, 4);
}
if (spin->si_syllable != NULL)
{
putc(SN_SYLLABLE, fd);
putc(0, fd);
l = (int)STRLEN(spin->si_syllable);
put_bytes(fd, (long_u)l, 4);
fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
}
putc(SN_END, fd);
spin->si_memtot = 0;
for (round = 1; round <= 3; ++round)
{
if (round == 1)
tree = spin->si_foldroot->wn_sibling;
else if (round == 2)
tree = spin->si_keeproot->wn_sibling;
else
tree = spin->si_prefroot->wn_sibling;
clear_node(tree);
nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
put_bytes(fd, (long_u)nodecount, 4);
spin->si_memtot += nodecount + nodecount * sizeof(int);
(void)put_node(fd, tree, 0, regionmask, round == 3);
}
if (putc(0, fd) == EOF)
retval = FAIL;
theend:
if (fclose(fd) == EOF)
retval = FAIL;
if (fwv != (size_t)1)
retval = FAIL;
if (retval == FAIL)
EMSG(_(e_write));
return retval;
}
static void
clear_node(node)
wordnode_T *node;
{
wordnode_T *np;
if (node != NULL)
for (np = node; np != NULL; np = np->wn_sibling)
{
np->wn_u1.index = 0;
np->wn_u2.wnode = NULL;
if (np->wn_byte != NUL)
clear_node(np->wn_child);
}
}
static int
put_node(fd, node, idx, regionmask, prefixtree)
FILE *fd;
wordnode_T *node;
int idx;
int regionmask;
int prefixtree;
{
int newindex = idx;
int siblingcount = 0;
wordnode_T *np;
int flags;
if (node == NULL)
return 0;
node->wn_u1.index = idx;
for (np = node; np != NULL; np = np->wn_sibling)
++siblingcount;
if (fd != NULL)
putc(siblingcount, fd);
for (np = node; np != NULL; np = np->wn_sibling)
{
if (np->wn_byte == 0)
{
if (fd != NULL)
{
if (prefixtree)
{
if (np->wn_flags == (short_u)PFX_FLAGS)
putc(BY_NOFLAGS, fd);
else
{
putc(BY_FLAGS, fd);
putc(np->wn_flags, fd);
}
putc(np->wn_affixID, fd);
put_bytes(fd, (long_u)np->wn_region, 2);
}
else
{
flags = np->wn_flags;
if (regionmask != 0 && np->wn_region != regionmask)
flags |= WF_REGION;
if (np->wn_affixID != 0)
flags |= WF_AFX;
if (flags == 0)
{
putc(BY_NOFLAGS, fd);
}
else
{
if (np->wn_flags >= 0x100)
{
putc(BY_FLAGS2, fd);
putc(flags, fd);
putc((unsigned)flags >> 8, fd);
}
else
{
putc(BY_FLAGS, fd);
putc(flags, fd);
}
if (flags & WF_REGION)
putc(np->wn_region, fd);
if (flags & WF_AFX)
putc(np->wn_affixID, fd);
}
}
}
}
else
{
if (np->wn_child->wn_u1.index != 0
&& np->wn_child->wn_u2.wnode != node)
{
if (fd != NULL)
{
putc(BY_INDEX, fd);
put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
}
}
else if (np->wn_child->wn_u2.wnode == NULL)
np->wn_child->wn_u2.wnode = node;
if (fd != NULL)
if (putc(np->wn_byte, fd) == EOF)
{
EMSG(_(e_write));
return 0;
}
}
}
newindex += siblingcount + 1;
for (np = node; np != NULL; np = np->wn_sibling)
if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
newindex = put_node(fd, np->wn_child, newindex, regionmask,
prefixtree);
return newindex;
}
void
ex_mkspell(eap)
exarg_T *eap;
{
int fcount;
char_u **fnames;
char_u *arg = eap->arg;
int ascii = FALSE;
if (STRNCMP(arg, "-ascii", 6) == 0)
{
ascii = TRUE;
arg = skipwhite(arg + 6);
}
if (get_arglist_exp(arg, &fcount, &fnames) == OK)
{
mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
FreeWild(fcount, fnames);
}
}
static void
spell_make_sugfile(spin, wfname)
spellinfo_T *spin;
char_u *wfname;
{
char_u fname[MAXPATHL];
int len;
slang_T *slang;
int free_slang = FALSE;
for (slang = first_lang; slang != NULL; slang = slang->sl_next)
if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
break;
if (slang == NULL)
{
spell_message(spin, (char_u *)_("Reading back spell file..."));
slang = spell_load_file(wfname, NULL, NULL, FALSE);
if (slang == NULL)
return;
free_slang = TRUE;
}
spin->si_blocks = NULL;
spin->si_blocks_cnt = 0;
spin->si_compress_cnt = 0;
spin->si_free_count = 0;
spin->si_first_free = NULL;
spin->si_foldwcount = 0;
spell_message(spin, (char_u *)_("Performing soundfolding..."));
if (sug_filltree(spin, slang) == FAIL)
goto theend;
if (sug_maketable(spin) == FAIL)
goto theend;
smsg((char_u *)_("Number of words after soundfolding: %ld"),
(long)spin->si_spellbuf->b_ml.ml_line_count);
spell_message(spin, (char_u *)_(msg_compressing));
wordtree_compress(spin, spin->si_foldroot);
STRCPY(fname, wfname);
len = (int)STRLEN(fname);
fname[len - 2] = 'u';
fname[len - 1] = 'g';
sug_write(spin, fname);
theend:
if (free_slang)
slang_free(slang);
free_blocks(spin->si_blocks);
close_spellbuf(spin->si_spellbuf);
}
static int
sug_filltree(spin, slang)
spellinfo_T *spin;
slang_T *slang;
{
char_u *byts;
idx_T *idxs;
int depth;
idx_T arridx[MAXWLEN];
int curi[MAXWLEN];
char_u tword[MAXWLEN];
char_u tsalword[MAXWLEN];
int c;
idx_T n;
unsigned words_done = 0;
int wordcount[MAXWLEN];
spin->si_foldroot = wordtree_alloc(spin);
if (spin->si_foldroot == NULL)
return FAIL;
spin->si_sugtree = TRUE;
byts = slang->sl_fbyts;
idxs = slang->sl_fidxs;
arridx[0] = 0;
curi[0] = 1;
wordcount[0] = 0;
depth = 0;
while (depth >= 0 && !got_int)
{
if (curi[depth] > byts[arridx[depth]])
{
idxs[arridx[depth]] = wordcount[depth];
if (depth > 0)
wordcount[depth - 1] += wordcount[depth];
--depth;
line_breakcheck();
}
else
{
n = arridx[depth] + curi[depth];
++curi[depth];
c = byts[n];
if (c == 0)
{
tword[depth] = NUL;
spell_soundfold(slang, tword, TRUE, tsalword);
if (tree_add_word(spin, tsalword, spin->si_foldroot,
words_done >> 16, words_done & 0xffff,
0) == FAIL)
return FAIL;
++words_done;
++wordcount[depth];
spin->si_blocks_cnt = 0;
while (byts[n + 1] == 0)
{
++n;
++curi[depth];
}
}
else
{
tword[depth++] = c;
arridx[depth] = idxs[n];
curi[depth] = 1;
wordcount[depth] = 0;
}
}
}
smsg((char_u *)_("Total number of words: %d"), words_done);
return OK;
}
static int
sug_maketable(spin)
spellinfo_T *spin;
{
garray_T ga;
int res = OK;
spin->si_spellbuf = open_spellbuf();
if (spin->si_spellbuf == NULL)
return FAIL;
ga_init2(&ga, 1, 100);
if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
res = FAIL;
ga_clear(&ga);
return res;
}
static int
sug_filltable(spin, node, startwordnr, gap)
spellinfo_T *spin;
wordnode_T *node;
int startwordnr;
garray_T *gap;
{
wordnode_T *p, *np;
int wordnr = startwordnr;
int nr;
int prev_nr;
for (p = node; p != NULL; p = p->wn_sibling)
{
if (p->wn_byte == NUL)
{
gap->ga_len = 0;
prev_nr = 0;
for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
{
if (ga_grow(gap, 10) == FAIL)
return -1;
nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
nr -= prev_nr;
prev_nr += nr;
gap->ga_len += offset2bytes(nr,
(char_u *)gap->ga_data + gap->ga_len);
}
((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
gap->ga_data, gap->ga_len, TRUE) == FAIL)
return -1;
++wordnr;
while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
p->wn_sibling = p->wn_sibling->wn_sibling;
p->wn_flags = 0;
p->wn_region = 0;
}
else
{
wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
if (wordnr == -1)
return -1;
}
}
return wordnr;
}
static int
offset2bytes(nr, buf)
int nr;
char_u *buf;
{
int rem;
int b1, b2, b3, b4;
b1 = nr % 255 + 1;
rem = nr / 255;
b2 = rem % 255 + 1;
rem = rem / 255;
b3 = rem % 255 + 1;
b4 = rem / 255 + 1;
if (b4 > 1 || b3 > 0x1f)
{
buf[0] = 0xe0 + b4;
buf[1] = b3;
buf[2] = b2;
buf[3] = b1;
return 4;
}
if (b3 > 1 || b2 > 0x3f )
{
buf[0] = 0xc0 + b3;
buf[1] = b2;
buf[2] = b1;
return 3;
}
if (b2 > 1 || b1 > 0x7f )
{
buf[0] = 0x80 + b2;
buf[1] = b1;
return 2;
}
buf[0] = b1;
return 1;
}
static int
bytes2offset(pp)
char_u **pp;
{
char_u *p = *pp;
int nr;
int c;
c = *p++;
if ((c & 0x80) == 0x00)
{
nr = c - 1;
}
else if ((c & 0xc0) == 0x80)
{
nr = (c & 0x3f) - 1;
nr = nr * 255 + (*p++ - 1);
}
else if ((c & 0xe0) == 0xc0)
{
nr = (c & 0x1f) - 1;
nr = nr * 255 + (*p++ - 1);
nr = nr * 255 + (*p++ - 1);
}
else
{
nr = (c & 0x0f) - 1;
nr = nr * 255 + (*p++ - 1);
nr = nr * 255 + (*p++ - 1);
nr = nr * 255 + (*p++ - 1);
}
*pp = p;
return nr;
}
static void
sug_write(spin, fname)
spellinfo_T *spin;
char_u *fname;
{
FILE *fd;
wordnode_T *tree;
int nodecount;
int wcount;
char_u *line;
linenr_T lnum;
int len;
fd = mch_fopen((char *)fname, "w");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return;
}
vim_snprintf((char *)IObuff, IOSIZE,
_("Writing suggestion file %s ..."), fname);
spell_message(spin, IObuff);
if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1)
{
EMSG(_(e_write));
goto theend;
}
putc(VIMSUGVERSION, fd);
put_time(fd, spin->si_sugtime);
spin->si_memtot = 0;
tree = spin->si_foldroot->wn_sibling;
clear_node(tree);
nodecount = put_node(NULL, tree, 0, 0, FALSE);
put_bytes(fd, (long_u)nodecount, 4);
spin->si_memtot += nodecount + nodecount * sizeof(int);
(void)put_node(fd, tree, 0, 0, FALSE);
wcount = spin->si_spellbuf->b_ml.ml_line_count;
put_bytes(fd, (long_u)wcount, 4);
for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
{
line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
len = (int)STRLEN(line) + 1;
if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
{
EMSG(_(e_write));
goto theend;
}
spin->si_memtot += len;
}
if (putc(0, fd) == EOF)
EMSG(_(e_write));
vim_snprintf((char *)IObuff, IOSIZE,
_("Estimated runtime memory use: %d bytes"), spin->si_memtot);
spell_message(spin, IObuff);
theend:
fclose(fd);
}
static buf_T *
open_spellbuf()
{
buf_T *buf;
buf = (buf_T *)alloc_clear(sizeof(buf_T));
if (buf != NULL)
{
buf->b_spell = TRUE;
buf->b_p_swf = TRUE;
ml_open(buf);
ml_open_file(buf);
}
return buf;
}
static void
close_spellbuf(buf)
buf_T *buf;
{
if (buf != NULL)
{
ml_close(buf, TRUE);
vim_free(buf);
}
}
static void
mkspell(fcount, fnames, ascii, overwrite, added_word)
int fcount;
char_u **fnames;
int ascii;
int overwrite;
int added_word;
{
char_u fname[MAXPATHL];
char_u wfname[MAXPATHL];
char_u **innames;
int incount;
afffile_T *(afile[8]);
int i;
int len;
struct stat st;
int error = FALSE;
spellinfo_T spin;
vim_memset(&spin, 0, sizeof(spin));
spin.si_verbose = !added_word;
spin.si_ascii = ascii;
spin.si_followup = TRUE;
spin.si_rem_accents = TRUE;
ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
ga_init2(&spin.si_repsal, (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);
ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
hash_init(&spin.si_commonwords);
spin.si_newcompID = 127;
innames = &fnames[1];
incount = fcount - 1;
if (fcount >= 1)
{
len = (int)STRLEN(fnames[0]);
if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
{
innames = &fnames[0];
incount = 1;
vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
}
else if (fcount == 1)
{
innames = &fnames[0];
incount = 1;
vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
spin.si_ascii ? (char_u *)"ascii" : spell_enc());
}
else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
{
vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
}
else
vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
spin.si_ascii ? (char_u *)"ascii" : spell_enc());
if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
spin.si_ascii = TRUE;
if (strstr((char *)gettail(wfname), ".add.") != NULL)
spin.si_add = TRUE;
}
if (incount <= 0)
EMSG(_(e_invarg));
else if (vim_strchr(gettail(wfname), '_') != NULL)
EMSG(_("E751: Output file name must not have region name"));
else if (incount > 8)
EMSG(_("E754: Only up to 8 regions supported"));
else
{
if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
{
EMSG(_(e_exists));
return;
}
if (mch_isdir(wfname))
{
EMSG2(_(e_isadir2), wfname);
return;
}
for (i = 0; i < incount; ++i)
{
afile[i] = NULL;
if (incount > 1)
{
len = (int)STRLEN(innames[i]);
if (STRLEN(gettail(innames[i])) < 5
|| innames[i][len - 3] != '_')
{
EMSG2(_("E755: Invalid region in %s"), innames[i]);
return;
}
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]);
}
}
spin.si_region_count = incount;
spin.si_foldroot = wordtree_alloc(&spin);
spin.si_keeproot = wordtree_alloc(&spin);
spin.si_prefroot = wordtree_alloc(&spin);
if (spin.si_foldroot == NULL
|| spin.si_keeproot == NULL
|| spin.si_prefroot == NULL)
{
free_blocks(spin.si_blocks);
return;
}
if (!spin.si_add)
spin.si_clear_chartab = TRUE;
for (i = 0; i < incount && !error; ++i)
{
spin.si_conv.vc_type = CONV_NONE;
spin.si_region = 1 << i;
vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
if (mch_stat((char *)fname, &st) >= 0)
{
afile[i] = spell_read_aff(&spin, fname);
if (afile[i] == NULL)
error = TRUE;
else
{
vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
innames[i]);
if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
error = TRUE;
}
}
else
{
if (spell_read_wordfile(&spin, innames[i]) == FAIL)
error = TRUE;
}
#ifdef FEAT_MBYTE
convert_setup(&spin.si_conv, NULL, NULL);
#endif
}
if (spin.si_compflags != NULL && spin.si_nobreak)
MSG(_("Warning: both compounding and NOBREAK specified"));
if (!error && !got_int)
{
spell_message(&spin, (char_u *)_(msg_compressing));
wordtree_compress(&spin, spin.si_foldroot);
wordtree_compress(&spin, spin.si_keeproot);
wordtree_compress(&spin, spin.si_prefroot);
}
if (!error && !got_int)
{
vim_snprintf((char *)IObuff, IOSIZE,
_("Writing spell file %s ..."), wfname);
spell_message(&spin, IObuff);
error = write_vim_spell(&spin, wfname) == FAIL;
spell_message(&spin, (char_u *)_("Done!"));
vim_snprintf((char *)IObuff, IOSIZE,
_("Estimated runtime memory use: %d bytes"), spin.si_memtot);
spell_message(&spin, IObuff);
if (!error)
spell_reload_one(wfname, added_word);
}
ga_clear(&spin.si_rep);
ga_clear(&spin.si_repsal);
ga_clear(&spin.si_sal);
ga_clear(&spin.si_map);
ga_clear(&spin.si_comppat);
ga_clear(&spin.si_prefcond);
hash_clear_all(&spin.si_commonwords, 0);
for (i = 0; i < incount; ++i)
if (afile[i] != NULL)
spell_free_aff(afile[i]);
free_blocks(spin.si_blocks);
if (spin.si_sugtime != 0 && !error && !got_int)
spell_make_sugfile(&spin, wfname);
}
}
static void
spell_message(spin, str)
spellinfo_T *spin;
char_u *str;
{
if (spin->si_verbose || p_verbose > 2)
{
if (!spin->si_verbose)
verbose_enter();
MSG(str);
out_flush();
if (!spin->si_verbose)
verbose_leave();
}
}
void
ex_spell(eap)
exarg_T *eap;
{
spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
eap->forceit ? 0 : (int)eap->line2,
eap->cmdidx == CMD_spellundo);
}
void
spell_add_word(word, len, bad, idx, undo)
char_u *word;
int len;
int bad;
int idx;
int undo;
{
FILE *fd = NULL;
buf_T *buf = NULL;
int new_spf = FALSE;
char_u *fname;
char_u fnamebuf[MAXPATHL];
char_u line[MAXWLEN * 2];
long fpos, fpos_next = 0;
int i;
char_u *spf;
if (idx == 0)
{
if (int_wordlist == NULL)
{
int_wordlist = vim_tempname('s');
if (int_wordlist == NULL)
return;
}
fname = int_wordlist;
}
else
{
if (*curwin->w_s->b_p_spf == NUL)
{
init_spellfile();
new_spf = TRUE;
}
if (*curwin->w_s->b_p_spf == NUL)
{
EMSG2(_(e_notset), "spellfile");
return;
}
for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
{
copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
if (i == idx)
break;
if (*spf == NUL)
{
EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
return;
}
}
buf = buflist_findname_exp(fnamebuf);
if (buf != NULL && buf->b_ml.ml_mfp == NULL)
buf = NULL;
if (buf != NULL && bufIsChanged(buf))
{
EMSG(_(e_bufloaded));
return;
}
fname = fnamebuf;
}
if (bad || undo)
{
fd = mch_fopen((char *)fname, "r");
if (fd != NULL)
{
while (!vim_fgets(line, MAXWLEN * 2, fd))
{
fpos = fpos_next;
fpos_next = ftell(fd);
if (STRNCMP(word, line, len) == 0
&& (line[len] == '/' || line[len] < ' '))
{
fclose(fd);
fd = mch_fopen((char *)fname, "r+");
if (fd == NULL)
break;
if (fseek(fd, fpos, SEEK_SET) == 0)
{
fputc('#', fd);
if (undo)
{
home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
smsg((char_u *)_("Word removed from %s"), NameBuff);
}
}
fseek(fd, fpos_next, SEEK_SET);
}
}
if (fd != NULL)
fclose(fd);
}
}
if (!undo)
{
fd = mch_fopen((char *)fname, "a");
if (fd == NULL && new_spf)
{
char_u *p;
if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
{
int c = *p;
*p = NUL;
vim_mkdir(fname, 0755);
*p = c;
fd = mch_fopen((char *)fname, "a");
}
}
if (fd == NULL)
EMSG2(_(e_notopen), fname);
else
{
if (bad)
fprintf(fd, "%.*s/!\n", len, word);
else
fprintf(fd, "%.*s\n", len, word);
fclose(fd);
home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
smsg((char_u *)_("Word added to %s"), NameBuff);
}
}
if (fd != NULL)
{
mkspell(1, &fname, FALSE, TRUE, TRUE);
if (buf != NULL)
buf_reload(buf, buf->b_orig_mode);
redraw_all_later(SOME_VALID);
}
}
static void
init_spellfile()
{
char_u buf[MAXPATHL];
int l;
char_u *fname;
char_u *rtp;
char_u *lend;
int aspath = FALSE;
char_u *lstart = curbuf->b_s.b_p_spl;
if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
{
for (lend = curwin->w_s->b_p_spl; *lend != NUL
&& vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
if (vim_ispathsep(*lend))
{
aspath = TRUE;
lstart = lend + 1;
}
rtp = p_rtp;
while (*rtp != NUL)
{
if (aspath)
vim_strncpy(buf, curbuf->b_s.b_p_spl, lstart - curbuf->b_s.b_p_spl - 1);
else
copy_option_part(&rtp, buf, MAXPATHL, ",");
if (filewritable(buf) == 2)
{
if (aspath)
vim_strncpy(buf, curbuf->b_s.b_p_spl, lend - curbuf->b_s.b_p_spl);
else
{
l = (int)STRLEN(buf);
vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
if (!filewritable(buf) != 2)
vim_mkdir(buf, 0755);
l = (int)STRLEN(buf);
vim_snprintf((char *)buf + l, MAXPATHL - l,
"/%.*s", (int)(lend - lstart), lstart);
}
l = (int)STRLEN(buf);
fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)->lp_slang->sl_fname;
vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
fname != NULL
&& strstr((char *)gettail(fname), ".ascii.") != NULL
? (char_u *)"ascii" : spell_enc());
set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
break;
}
aspath = FALSE;
}
}
}
static void
clear_spell_chartab(sp)
spelltab_T *sp;
{
int i;
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)
{
sp->st_fold[i] = i;
sp->st_upper[i] = i;
}
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)
{
sp->st_isw[i] = TRUE;
sp->st_upper[i] = i - 0x20;
}
}
void
init_spell_chartab()
{
int i;
did_set_spelltab = FALSE;
clear_spell_chartab(&spelltab);
#ifdef FEAT_MBYTE
if (enc_dbcs)
{
for (i = 128; i <= 255; ++i)
if (MB_BYTE2LEN(i) == 2)
spelltab.st_isw[i] = TRUE;
}
else if (enc_utf8)
{
for (i = 128; i < 256; ++i)
{
int f = utf_fold(i);
int u = utf_toupper(i);
spelltab.st_isu[i] = utf_isupper(i);
spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
spelltab.st_fold[i] = (f < 256) ? f : i;
spelltab.st_upper[i] = (u < 256) ? u : i;
}
}
else
#endif
{
for (i = 128; i < 256; ++i)
{
if (MB_ISUPPER(i))
{
spelltab.st_isw[i] = TRUE;
spelltab.st_isu[i] = TRUE;
spelltab.st_fold[i] = MB_TOLOWER(i);
}
else if (MB_ISLOWER(i))
{
spelltab.st_isw[i] = TRUE;
spelltab.st_upper[i] = MB_TOUPPER(i);
}
}
}
}
static int
set_spell_chartab(fol, low, upp)
char_u *fol;
char_u *low;
char_u *upp;
{
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
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 (l < 256 && l != f)
{
if (f >= 256)
{
EMSG(_(e_affrange));
return FAIL;
}
new_st.st_fold[l] = f;
}
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;
new_st.st_upper[f] = u;
}
}
if (*pl != NUL || *pu != NUL)
{
EMSG(_(e_affform));
return FAIL;
}
return set_spell_finish(&new_st);
}
static void
set_spell_charflags(flags, cnt, fol)
char_u *flags;
int cnt;
char_u *fol;
{
spelltab_T new_st;
int i;
char_u *p = fol;
int c;
clear_spell_chartab(&new_st);
for (i = 0; i < 128; ++i)
{
if (i < cnt)
{
new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
}
if (*p != NUL)
{
#ifdef FEAT_MBYTE
c = mb_ptr2char_adv(&p);
#else
c = *p++;
#endif
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;
}
}
(void)set_spell_finish(&new_st);
}
static int
set_spell_finish(new_st)
spelltab_T *new_st;
{
int i;
if (did_set_spelltab)
{
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]
|| spelltab.st_fold[i] != new_st->st_fold[i]
|| spelltab.st_upper[i] != new_st->st_upper[i])
{
EMSG(_("E763: Word characters differ between spell files"));
return FAIL;
}
}
}
else
{
spelltab = *new_st;
did_set_spelltab = TRUE;
}
return OK;
}
static int
spell_iswordp(p, wp)
char_u *p;
win_T *wp;
{
#ifdef FEAT_MBYTE
char_u *s;
int l;
int c;
if (has_mbyte)
{
l = MB_BYTE2LEN(*p);
s = p;
if (l == 1)
{
if (wp->w_s->b_spell_ismw[*p])
{
s = p + 1;
l = MB_BYTE2LEN(*s);
}
}
else
{
c = mb_ptr2char(p);
if (c < 256 ? wp->w_s->b_spell_ismw[c]
: (wp->w_s->b_spell_ismw_mb != NULL
&& vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
{
s = p + l;
l = MB_BYTE2LEN(*s);
}
}
c = mb_ptr2char(s);
if (c > 255)
return spell_mb_isword_class(mb_get_class(s));
return spelltab.st_isw[c];
}
#endif
return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
}
static int
spell_iswordp_nmw(p)
char_u *p;
{
#ifdef FEAT_MBYTE
int c;
if (has_mbyte)
{
c = mb_ptr2char(p);
if (c > 255)
return spell_mb_isword_class(mb_get_class(p));
return spelltab.st_isw[c];
}
#endif
return spelltab.st_isw[*p];
}
#ifdef FEAT_MBYTE
static int
spell_mb_isword_class(cl)
int cl;
{
return cl >= 2 && cl != 0x2070 && cl != 0x2080;
}
static int
spell_iswordp_w(p, wp)
int *p;
win_T *wp;
{
int *s;
if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
: (wp->w_s->b_spell_ismw_mb != NULL
&& vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
s = p + 1;
else
s = p;
if (*s > 255)
{
if (enc_utf8)
return spell_mb_isword_class(utf_class(*s));
if (enc_dbcs)
return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
return 0;
}
return spelltab.st_isw[*s];
}
#endif
static int
write_spell_prefcond(fd, gap)
FILE *fd;
garray_T *gap;
{
int i;
char_u *p;
int len;
int totlen;
size_t x = 1;
if (fd != NULL)
put_bytes(fd, (long_u)gap->ga_len, 2);
totlen = 2 + gap->ga_len;
for (i = 0; i < gap->ga_len; ++i)
{
p = ((char_u **)gap->ga_data)[i];
if (p != NULL)
{
len = (int)STRLEN(p);
if (fd != NULL)
{
fputc(len, fd);
x &= fwrite(p, (size_t)len, (size_t)1, fd);
}
totlen += len;
}
else if (fd != NULL)
fputc(0, fd);
}
return totlen;
}
static int
spell_casefold(str, len, buf, buflen)
char_u *str;
int len;
char_u *buf;
int buflen;
{
int i;
if (len >= buflen)
{
buf[0] = NUL;
return FAIL;
}
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int outi = 0;
char_u *p;
int c;
for (p = str; p < str + len; )
{
if (outi + MB_MAXBYTES > buflen)
{
buf[outi] = NUL;
return FAIL;
}
c = mb_cptr2char_adv(&p);
outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
}
buf[outi] = NUL;
}
else
#endif
{
for (i = 0; i < len; ++i)
buf[i] = spelltab.st_fold[str[i]];
buf[i] = NUL;
}
return OK;
}
#define SPS_BEST 1
#define SPS_FAST 2
#define SPS_DOUBLE 4
static int sps_flags = SPS_BEST;
static int sps_limit = 9999;
int
spell_check_sps()
{
char_u *p;
char_u *s;
char_u buf[MAXPATHL];
int f;
sps_flags = 0;
sps_limit = 9999;
for (p = p_sps; *p != NUL; )
{
copy_option_part(&p, buf, MAXPATHL, ",");
f = 0;
if (VIM_ISDIGIT(*buf))
{
s = buf;
sps_limit = getdigits(&s);
if (*s != NUL && !VIM_ISDIGIT(*s))
f = -1;
}
else if (STRCMP(buf, "best") == 0)
f = SPS_BEST;
else if (STRCMP(buf, "fast") == 0)
f = SPS_FAST;
else if (STRCMP(buf, "double") == 0)
f = SPS_DOUBLE;
else if (STRNCMP(buf, "expr:", 5) != 0
&& STRNCMP(buf, "file:", 5) != 0)
f = -1;
if (f == -1 || (sps_flags != 0 && f != 0))
{
sps_flags = SPS_BEST;
sps_limit = 9999;
return FAIL;
}
if (f != 0)
sps_flags = f;
}
if (sps_flags == 0)
sps_flags = SPS_BEST;
return OK;
}
void
spell_suggest(count)
int count;
{
char_u *line;
pos_T prev_cursor = curwin->w_cursor;
char_u wcopy[MAXWLEN + 2];
char_u *p;
int i;
int c;
suginfo_T sug;
suggest_T *stp;
int mouse_used;
int need_cap;
int limit;
int selected = count;
int badlen = 0;
int msg_scroll_save = msg_scroll;
if (no_spell_checking(curwin))
return;
#ifdef FEAT_VISUAL
if (VIsual_active)
{
if (curwin->w_cursor.lnum != VIsual.lnum)
{
vim_beep();
return;
}
badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
if (badlen < 0)
badlen = -badlen;
else
curwin->w_cursor.col = VIsual.col;
++badlen;
end_visual_mode();
}
else
#endif
if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
|| curwin->w_cursor.col > prev_cursor.col)
{
curwin->w_cursor = prev_cursor;
line = ml_get_curline();
p = line + curwin->w_cursor.col;
while (p > line && spell_iswordp_nmw(p))
mb_ptr_back(line, p);
while (*p != NUL && !spell_iswordp_nmw(p))
mb_ptr_adv(p);
if (!spell_iswordp_nmw(p))
{
beep_flush();
return;
}
curwin->w_cursor.col = (colnr_T)(p - line);
}
need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
line = vim_strsave(ml_get_curline());
if (line == NULL)
goto skip;
if (sps_limit > (int)Rows - 2)
limit = (int)Rows - 2;
else
limit = sps_limit;
spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
TRUE, need_cap, TRUE);
if (sug.su_ga.ga_len == 0)
MSG(_("Sorry, no suggestions"));
else if (count > 0)
{
if (count > sug.su_ga.ga_len)
smsg((char_u *)_("Sorry, only %ld suggestions"),
(long)sug.su_ga.ga_len);
}
else
{
vim_free(repl_from);
repl_from = NULL;
vim_free(repl_to);
repl_to = NULL;
#ifdef FEAT_RIGHTLEFT
cmdmsg_rl = curwin->w_p_rl;
if (cmdmsg_rl)
msg_col = Columns - 1;
#endif
msg_start();
msg_row = Rows - 1;
lines_left = Rows;
vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
sug.su_badlen, sug.su_badptr);
#ifdef FEAT_RIGHTLEFT
if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
{
vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
sug.su_badlen, sug.su_badptr);
}
#endif
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.su_ga, i);
STRCPY(wcopy, stp->st_word);
if (sug.su_badlen > stp->st_orglen)
vim_strncpy(wcopy + stp->st_wordlen,
sug.su_badptr + stp->st_orglen,
sug.su_badlen - stp->st_orglen);
vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
#ifdef FEAT_RIGHTLEFT
if (cmdmsg_rl)
rl_mirror(IObuff);
#endif
msg_puts(IObuff);
vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
msg_puts(IObuff);
if (sug.su_badlen < stp->st_orglen)
{
vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
stp->st_orglen, sug.su_badptr);
msg_puts(IObuff);
}
if (p_verbose > 0)
{
if (sps_flags & (SPS_DOUBLE | SPS_BEST))
vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
stp->st_salscore ? "s " : "",
stp->st_score, stp->st_altscore);
else
vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
stp->st_score);
#ifdef FEAT_RIGHTLEFT
if (cmdmsg_rl)
rl_mirror(IObuff + 1);
#endif
msg_advance(30);
msg_puts(IObuff);
}
msg_putchar('\n');
}
#ifdef FEAT_RIGHTLEFT
cmdmsg_rl = FALSE;
msg_col = 0;
#endif
selected = prompt_for_number(&mouse_used);
if (mouse_used)
selected -= lines_left;
lines_left = Rows;
msg_scroll = msg_scroll_save;
}
if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
{
stp = &SUG(sug.su_ga, selected - 1);
if (sug.su_badlen > stp->st_orglen)
{
repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
sug.su_badlen - stp->st_orglen,
sug.su_badptr + stp->st_orglen);
repl_to = vim_strsave(IObuff);
}
else
{
repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
repl_to = vim_strsave(stp->st_word);
}
p = alloc((unsigned)STRLEN(line) - stp->st_orglen
+ stp->st_wordlen + 1);
if (p != NULL)
{
c = (int)(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;
ResetRedobuff();
AppendToRedobuff((char_u *)"ciw");
AppendToRedobuffLit(p + c,
stp->st_wordlen + sug.su_badlen - stp->st_orglen);
AppendCharToRedobuff(ESC);
changed_bytes(curwin->w_cursor.lnum, c);
}
}
else
curwin->w_cursor = prev_cursor;
spell_find_cleanup(&sug);
skip:
vim_free(line);
}
static int
check_need_cap(lnum, col)
linenr_T lnum;
colnr_T col;
{
int need_cap = FALSE;
char_u *line;
char_u *line_copy = NULL;
char_u *p;
colnr_T endcol;
regmatch_T regmatch;
if (curwin->w_s->b_cap_prog == NULL)
return FALSE;
line = ml_get_curline();
endcol = 0;
if ((int)(skipwhite(line) - line) >= (int)col)
{
if (lnum == 1)
need_cap = TRUE;
else
{
line = ml_get(lnum - 1);
if (*skipwhite(line) == NUL)
need_cap = TRUE;
else
{
line_copy = concat_str(line, (char_u *)" ");
line = line_copy;
endcol = (colnr_T)STRLEN(line);
}
}
}
else
endcol = col;
if (endcol > 0)
{
regmatch.regprog = curwin->w_s->b_cap_prog;
regmatch.rm_ic = FALSE;
p = line + endcol;
for (;;)
{
mb_ptr_back(line, p);
if (p == line || spell_iswordp_nmw(p))
break;
if (vim_regexec(®match, p, 0)
&& regmatch.endp[0] == line + endcol)
{
need_cap = TRUE;
break;
}
}
}
vim_free(line_copy);
return need_cap;
}
void
ex_spellrepall(eap)
exarg_T *eap UNUSED;
{
pos_T pos = curwin->w_cursor;
char_u *frompat;
int addlen;
char_u *line;
char_u *p;
int save_ws = p_ws;
linenr_T prev_lnum = 0;
if (repl_from == NULL || repl_to == NULL)
{
EMSG(_("E752: No previous spell replacement"));
return;
}
addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
frompat = alloc((unsigned)STRLEN(repl_from) + 7);
if (frompat == NULL)
return;
sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
p_ws = FALSE;
sub_nsubs = 0;
sub_nlines = 0;
curwin->w_cursor.lnum = 0;
while (!got_int)
{
if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
|| u_save_cursor() == FAIL)
break;
line = ml_get_curline();
if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
repl_to, STRLEN(repl_to)) != 0)
{
p = alloc((unsigned)STRLEN(line) + addlen + 1);
if (p == NULL)
break;
mch_memmove(p, line, curwin->w_cursor.col);
STRCPY(p + curwin->w_cursor.col, repl_to);
STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
ml_replace(curwin->w_cursor.lnum, p, FALSE);
changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
if (curwin->w_cursor.lnum != prev_lnum)
{
++sub_nlines;
prev_lnum = curwin->w_cursor.lnum;
}
++sub_nsubs;
}
curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
}
p_ws = save_ws;
curwin->w_cursor = pos;
vim_free(frompat);
if (sub_nsubs == 0)
EMSG2(_("E753: Not found: %s"), repl_from);
else
do_sub_msg(FALSE);
}
void
spell_suggest_list(gap, word, maxcount, need_cap, interactive)
garray_T *gap;
char_u *word;
int maxcount;
int need_cap;
int interactive;
{
suginfo_T sug;
int i;
suggest_T *stp;
char_u *wcopy;
spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
if (ga_grow(gap, sug.su_ga.ga_len) == OK)
{
for (i = 0; i < sug.su_ga.ga_len; ++i)
{
stp = &SUG(sug.su_ga, i);
wcopy = alloc(stp->st_wordlen
+ (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
if (wcopy == NULL)
break;
STRCPY(wcopy, stp->st_word);
STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
}
}
spell_find_cleanup(&sug);
}
static void
spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
char_u *badptr;
int badlen;
suginfo_T *su;
int maxcount;
int banbadword;
int need_cap;
int interactive;
{
hlf_T attr = HLF_COUNT;
char_u buf[MAXPATHL];
char_u *p;
int do_combine = FALSE;
char_u *sps_copy;
#ifdef FEAT_EVAL
static int expr_busy = FALSE;
#endif
int c;
int i;
langp_T *lp;
vim_memset(su, 0, sizeof(suginfo_T));
ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
if (*badptr == NUL)
return;
hash_init(&su->su_banned);
su->su_badptr = badptr;
if (badlen != 0)
su->su_badlen = badlen;
else
su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
su->su_maxcount = maxcount;
su->su_maxscore = SCORE_MAXINIT;
if (su->su_badlen >= MAXWLEN)
su->su_badlen = MAXWLEN - 1;
vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
(void)spell_casefold(su->su_badptr, su->su_badlen,
su->su_fbadword, MAXWLEN);
su->su_badflags = badword_captype(su->su_badptr,
su->su_badptr + su->su_badlen);
if (need_cap)
su->su_badflags |= WF_ONECAP;
for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
{
lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
if (lp->lp_sallang != NULL)
{
su->su_sallang = lp->lp_sallang;
break;
}
}
if (su->su_sallang != NULL)
spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
su->su_sal_badword);
c = PTR2CHAR(su->su_badptr);
if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
{
make_case_word(su->su_badword, buf, WF_ONECAP);
add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
0, TRUE, su->su_sallang, FALSE);
}
if (banbadword)
add_banned(su, su->su_badword);
sps_copy = vim_strsave(p_sps);
if (sps_copy == NULL)
return;
for (p = sps_copy; *p != NUL; )
{
copy_option_part(&p, buf, MAXPATHL, ",");
if (STRNCMP(buf, "expr:", 5) == 0)
{
#ifdef FEAT_EVAL
if (!expr_busy)
{
expr_busy = TRUE;
spell_suggest_expr(su, buf + 5);
expr_busy = FALSE;
}
#endif
}
else if (STRNCMP(buf, "file:", 5) == 0)
spell_suggest_file(su, buf + 5);
else
{
spell_suggest_intern(su, interactive);
if (sps_flags & SPS_DOUBLE)
do_combine = TRUE;
}
}
vim_free(sps_copy);
if (do_combine)
score_combine(su);
}
#ifdef FEAT_EVAL
static void
spell_suggest_expr(su, expr)
suginfo_T *su;
char_u *expr;
{
list_T *list;
listitem_T *li;
int score;
char_u *p;
list = eval_spell_expr(su->su_badword, expr);
if (list != NULL)
{
for (li = list->lv_first; li != NULL; li = li->li_next)
if (li->li_tv.v_type == VAR_LIST)
{
score = get_spellword(li->li_tv.vval.v_list, &p);
if (score >= 0 && score <= su->su_maxscore)
add_suggestion(su, &su->su_ga, p, su->su_badlen,
score, 0, TRUE, su->su_sallang, FALSE);
}
list_unref(list);
}
check_suggestions(su, &su->su_ga);
(void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
}
#endif
static void
spell_suggest_file(su, fname)
suginfo_T *su;
char_u *fname;
{
FILE *fd;
char_u line[MAXWLEN * 2];
char_u *p;
int len;
char_u cword[MAXWLEN];
fd = mch_fopen((char *)fname, "r");
if (fd == NULL)
{
EMSG2(_(e_notopen), fname);
return;
}
while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
{
line_breakcheck();
p = vim_strchr(line, '/');
if (p == NULL)
continue;
*p++ = NUL;
if (STRICMP(su->su_badword, line) == 0)
{
for (len = 0; p[len] >= ' '; ++len)
;
p[len] = NUL;
if (captype(p, NULL) == 0)
{
make_case_word(p, cword, su->su_badflags);
p = cword;
}
add_suggestion(su, &su->su_ga, p, su->su_badlen,
SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
}
}
fclose(fd);
check_suggestions(su, &su->su_ga);
(void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
}
static void
spell_suggest_intern(su, interactive)
suginfo_T *su;
int interactive;
{
suggest_load_files();
suggest_try_special(su);
suggest_try_change(su);
if (sps_flags & SPS_DOUBLE)
score_comp_sal(su);
if ((sps_flags & SPS_FAST) == 0)
{
if (sps_flags & SPS_BEST)
rescore_suggestions(su);
suggest_try_soundalike_prep();
su->su_maxscore = SCORE_SFMAX1;
su->su_sfmaxscore = SCORE_MAXINIT * 3;
suggest_try_soundalike(su);
if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
{
su->su_maxscore = SCORE_SFMAX2;
suggest_try_soundalike(su);
if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
{
su->su_maxscore = SCORE_SFMAX3;
suggest_try_soundalike(su);
}
}
su->su_maxscore = su->su_sfmaxscore;
suggest_try_soundalike_finish();
}
ui_breakcheck();
if (interactive && got_int)
{
(void)vgetc();
got_int = FALSE;
}
if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
{
if (sps_flags & SPS_BEST)
rescore_suggestions(su);
check_suggestions(su, &su->su_ga);
(void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
}
}
static void
suggest_load_files()
{
langp_T *lp;
int lpi;
slang_T *slang;
char_u *dotp;
FILE *fd;
char_u buf[MAXWLEN];
int i;
time_t timestamp;
int wcount;
int wordnr;
garray_T ga;
int c;
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
slang = lp->lp_slang;
if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
{
slang->sl_sugloaded = TRUE;
dotp = vim_strrchr(slang->sl_fname, '.');
if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
continue;
STRCPY(dotp, ".sug");
fd = mch_fopen((char *)slang->sl_fname, "r");
if (fd == NULL)
goto nextone;
for (i = 0; i < VIMSUGMAGICL; ++i)
buf[i] = getc(fd);
if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
{
EMSG2(_("E778: This does not look like a .sug file: %s"),
slang->sl_fname);
goto nextone;
}
c = getc(fd);
if (c < VIMSUGVERSION)
{
EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
slang->sl_fname);
goto nextone;
}
else if (c > VIMSUGVERSION)
{
EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
slang->sl_fname);
goto nextone;
}
timestamp = get8ctime(fd);
if (timestamp != slang->sl_sugtime)
{
EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
slang->sl_fname);
goto nextone;
}
if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
FALSE, 0) != 0)
{
someerror:
EMSG2(_("E782: error while reading .sug file: %s"),
slang->sl_fname);
slang_clear_sug(slang);
goto nextone;
}
slang->sl_sugbuf = open_spellbuf();
if (slang->sl_sugbuf == NULL)
goto someerror;
wcount = get4c(fd);
if (wcount < 0)
goto someerror;
ga_init2(&ga, 1, 100);
for (wordnr = 0; wordnr < wcount; ++wordnr)
{
ga.ga_len = 0;
for (;;)
{
c = getc(fd);
if (c < 0 || ga_grow(&ga, 1) == FAIL)
goto someerror;
((char_u *)ga.ga_data)[ga.ga_len++] = c;
if (c == NUL)
break;
}
if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
ga.ga_data, ga.ga_len, TRUE) == FAIL)
goto someerror;
}
ga_clear(&ga);
tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
nextone:
if (fd != NULL)
fclose(fd);
STRCPY(dotp, ".spl");
}
}
}
static void
tree_count_words(byts, idxs)
char_u *byts;
idx_T *idxs;
{
int depth;
idx_T arridx[MAXWLEN];
int curi[MAXWLEN];
int c;
idx_T n;
int wordcount[MAXWLEN];
arridx[0] = 0;
curi[0] = 1;
wordcount[0] = 0;
depth = 0;
while (depth >= 0 && !got_int)
{
if (curi[depth] > byts[arridx[depth]])
{
idxs[arridx[depth]] = wordcount[depth];
if (depth > 0)
wordcount[depth - 1] += wordcount[depth];
--depth;
fast_breakcheck();
}
else
{
n = arridx[depth] + curi[depth];
++curi[depth];
c = byts[n];
if (c == 0)
{
++wordcount[depth];
while (byts[n + 1] == 0)
{
++n;
++curi[depth];
}
}
else
{
++depth;
arridx[depth] = idxs[n];
curi[depth] = 1;
wordcount[depth] = 0;
}
}
}
}
static void
spell_find_cleanup(su)
suginfo_T *su;
{
int i;
for (i = 0; i < su->su_ga.ga_len; ++i)
vim_free(SUG(su->su_ga, i).st_word);
ga_clear(&su->su_ga);
for (i = 0; i < su->su_sga.ga_len; ++i)
vim_free(SUG(su->su_sga, i).st_word);
ga_clear(&su->su_sga);
hash_clear_all(&su->su_banned, 0);
}
static void
onecap_copy(word, wcopy, upper)
char_u *word;
char_u *wcopy;
int upper;
{
char_u *p;
int c;
int l;
p = word;
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_cptr2char_adv(&p);
else
#endif
c = *p++;
if (upper)
c = SPELL_TOUPPER(c);
else
c = SPELL_TOFOLD(c);
#ifdef FEAT_MBYTE
if (has_mbyte)
l = mb_char2bytes(c, wcopy);
else
#endif
{
l = 1;
wcopy[0] = c;
}
vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
}
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_cptr2char_adv(&s);
else
#endif
c = *s++;
#ifdef FEAT_MBYTE
if (enc_latin1like && c == 0xdf)
{
c = 'S';
if (d - wcopy >= MAXWLEN - 1)
break;
*d++ = c;
}
else
#endif
c = SPELL_TOUPPER(c);
#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;
}
static void
suggest_try_special(su)
suginfo_T *su;
{
char_u *p;
size_t len;
int c;
char_u word[MAXWLEN];
p = skiptowhite(su->su_fbadword);
len = p - su->su_fbadword;
p = skipwhite(p);
if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
{
c = su->su_fbadword[len];
su->su_fbadword[len] = NUL;
make_case_word(su->su_fbadword, word, su->su_badflags);
su->su_fbadword[len] = c;
add_suggestion(su, &su->su_ga, word, su->su_badlen,
RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
}
}
static void
suggest_try_change(su)
suginfo_T *su;
{
char_u fword[MAXWLEN];
int n;
char_u *p;
int lpi;
langp_T *lp;
STRCPY(fword, su->su_fbadword);
n = (int)STRLEN(fword);
p = su->su_badptr + su->su_badlen;
(void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
if (lp->lp_slang->sl_fbyts == NULL)
continue;
suggest_trie_walk(su, lp, fword, FALSE);
}
}
#define TRY_DEEPER(su, stack, depth, add) \
(stack[depth].ts_score + (add) < su->su_maxscore)
static void
suggest_trie_walk(su, lp, fword, soundfold)
suginfo_T *su;
langp_T *lp;
char_u *fword;
int soundfold;
{
char_u tword[MAXWLEN];
trystate_T stack[MAXWLEN];
char_u preword[MAXWLEN * 3];
char_u compflags[MAXWLEN];
trystate_T *sp;
int newscore;
int score;
char_u *byts, *fbyts, *pbyts;
idx_T *idxs, *fidxs, *pidxs;
int depth;
int c, c2, c3;
int n = 0;
int flags;
garray_T *gap;
idx_T arridx;
int len;
char_u *p;
fromto_T *ftp;
int fl = 0, tl;
int repextra = 0;
slang_T *slang = lp->lp_slang;
int fword_ends;
int goodword_ends;
#ifdef DEBUG_TRIEWALK
char_u changename[MAXWLEN][80];
#endif
int breakcheckcount = 1000;
int compound_ok;
depth = 0;
sp = &stack[0];
vim_memset(sp, 0, sizeof(trystate_T));
sp->ts_curi = 1;
if (soundfold)
{
byts = fbyts = slang->sl_sbyts;
idxs = fidxs = slang->sl_sidxs;
pbyts = NULL;
pidxs = NULL;
sp->ts_prefixdepth = PFD_NOPREFIX;
sp->ts_state = STATE_START;
}
else
{
fbyts = slang->sl_fbyts;
fidxs = slang->sl_fidxs;
pbyts = slang->sl_pbyts;
pidxs = slang->sl_pidxs;
if (pbyts != NULL)
{
byts = pbyts;
idxs = pidxs;
sp->ts_prefixdepth = PFD_PREFIXTREE;
sp->ts_state = STATE_NOPREFIX;
}
else
{
byts = fbyts;
idxs = fidxs;
sp->ts_prefixdepth = PFD_NOPREFIX;
sp->ts_state = STATE_START;
}
}
while (depth >= 0 && !got_int)
{
sp = &stack[depth];
switch (sp->ts_state)
{
case STATE_START:
case STATE_NOPREFIX:
arridx = sp->ts_arridx;
len = byts[arridx];
arridx += sp->ts_curi;
if (sp->ts_prefixdepth == PFD_PREFIXTREE)
{
for (n = 0; n < len && byts[arridx + n] == 0; ++n)
;
sp->ts_curi += n;
n = (int)sp->ts_state;
sp->ts_state = STATE_ENDNUL;
sp->ts_save_badflags = su->su_badflags;
if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
else
#endif
n = sp->ts_fidx;
flags = badword_captype(su->su_badptr, su->su_badptr + n);
su->su_badflags = badword_captype(su->su_badptr + n,
su->su_badptr + su->su_badlen);
#ifdef DEBUG_TRIEWALK
sprintf(changename[depth], "prefix");
#endif
go_deeper(stack, depth, 0);
++depth;
sp = &stack[depth];
sp->ts_prefixdepth = depth - 1;
byts = fbyts;
idxs = fidxs;
sp->ts_arridx = 0;
tword[sp->ts_twordlen] = NUL;
make_case_word(tword + sp->ts_splitoff,
preword + sp->ts_prewordlen, flags);
sp->ts_prewordlen = (char_u)STRLEN(preword);
sp->ts_splitoff = sp->ts_twordlen;
}
break;
}
if (sp->ts_curi > len || byts[arridx] != 0)
{
sp->ts_state = STATE_ENDNUL;
sp->ts_save_badflags = su->su_badflags;
break;
}
++sp->ts_curi;
flags = (int)idxs[arridx];
if (flags & WF_NOSUGGEST)
break;
fword_ends = (fword[sp->ts_fidx] == NUL
|| (soundfold
? vim_iswhite(fword[sp->ts_fidx])
: !spell_iswordp(fword + sp->ts_fidx, curwin)));
tword[sp->ts_twordlen] = NUL;
if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
&& (sp->ts_flags & TSF_PREFIXOK) == 0)
{
n = stack[sp->ts_prefixdepth].ts_arridx;
len = pbyts[n++];
for (c = 0; c < len && pbyts[n + c] == 0; ++c)
;
if (c > 0)
{
c = valid_word_prefix(c, n, flags,
tword + sp->ts_splitoff, slang, FALSE);
if (c == 0)
break;
if (c & WF_RAREPFX)
flags |= WF_RARE;
sp->ts_flags |= TSF_PREFIXOK;
}
}
if (sp->ts_complen == sp->ts_compsplit && fword_ends
&& (flags & WF_NEEDCOMP))
goodword_ends = FALSE;
else
goodword_ends = TRUE;
p = NULL;
compound_ok = TRUE;
if (sp->ts_complen > sp->ts_compsplit)
{
if (slang->sl_nobreak)
{
if (sp->ts_fidx - sp->ts_splitfidx
== sp->ts_twordlen - sp->ts_splitoff
&& STRNCMP(fword + sp->ts_splitfidx,
tword + sp->ts_splitoff,
sp->ts_fidx - sp->ts_splitfidx) == 0)
{
preword[sp->ts_prewordlen] = NUL;
newscore = score_wordcount_adj(slang, sp->ts_score,
preword + sp->ts_prewordlen,
sp->ts_prewordlen > 0);
if (newscore <= su->su_maxscore)
add_suggestion(su, &su->su_ga, preword,
sp->ts_splitfidx - repextra,
newscore, 0, FALSE,
lp->lp_sallang, FALSE);
break;
}
}
else
{
if (((unsigned)flags >> 24) == 0
|| sp->ts_twordlen - sp->ts_splitoff
< slang->sl_compminlen)
break;
#ifdef FEAT_MBYTE
if (has_mbyte
&& slang->sl_compminlen > 0
&& mb_charlen(tword + sp->ts_splitoff)
< slang->sl_compminlen)
break;
#endif
compflags[sp->ts_complen] = ((unsigned)flags >> 24);
compflags[sp->ts_complen + 1] = NUL;
vim_strncpy(preword + sp->ts_prewordlen,
tword + sp->ts_splitoff,
sp->ts_twordlen - sp->ts_splitoff);
if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
&slang->sl_comppat))
compound_ok = FALSE;
if (compound_ok)
{
p = preword;
while (*skiptowhite(p) != NUL)
p = skipwhite(skiptowhite(p));
if (fword_ends && !can_compound(slang, p,
compflags + sp->ts_compsplit))
compound_ok = FALSE;
}
p = preword + sp->ts_prewordlen;
mb_ptr_back(preword, p);
}
}
if (soundfold)
STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
else if (flags & WF_KEEPCAP)
find_keepcap_word(slang, tword + sp->ts_splitoff,
preword + sp->ts_prewordlen);
else
{
c = su->su_badflags;
if ((c & WF_ALLCAP)
#ifdef FEAT_MBYTE
&& su->su_badlen == (*mb_ptr2len)(su->su_badptr)
#else
&& su->su_badlen == 1
#endif
)
c = WF_ONECAP;
c |= flags;
if (p != NULL && spell_iswordp_nmw(p))
c &= ~WF_ONECAP;
make_case_word(tword + sp->ts_splitoff,
preword + sp->ts_prewordlen, c);
}
if (!soundfold)
{
if (flags & WF_BANNED)
{
add_banned(su, preword + sp->ts_prewordlen);
break;
}
if ((sp->ts_complen == sp->ts_compsplit
&& WAS_BANNED(su, preword + sp->ts_prewordlen))
|| WAS_BANNED(su, preword))
{
if (slang->sl_compprog == NULL)
break;
goodword_ends = FALSE;
}
}
newscore = 0;
if (!soundfold)
{
if ((flags & WF_REGION)
&& (((unsigned)flags >> 16) & lp->lp_region) == 0)
newscore += SCORE_REGION;
if (flags & WF_RARE)
newscore += SCORE_RARE;
if (!spell_valid_case(su->su_badflags,
captype(preword + sp->ts_prewordlen, NULL)))
newscore += SCORE_ICASE;
}
if (fword_ends
&& goodword_ends
&& sp->ts_fidx >= sp->ts_fidxtry
&& compound_ok)
{
#ifdef DEBUG_TRIEWALK
if (soundfold && STRCMP(preword, "smwrd") == 0)
{
int j;
smsg("------ %s -------", fword);
for (j = 0; j < depth; ++j)
smsg("%s", changename[j]);
}
#endif
if (soundfold)
{
add_sound_suggest(su, preword, sp->ts_score, lp);
}
else if (sp->ts_fidx > 0)
{
p = fword + sp->ts_fidx;
mb_ptr_back(fword, p);
if (!spell_iswordp(p, curwin))
{
p = preword + STRLEN(preword);
mb_ptr_back(preword, p);
if (spell_iswordp(p, curwin))
newscore += SCORE_NONWORD;
}
score = score_wordcount_adj(slang,
sp->ts_score + newscore,
preword + sp->ts_prewordlen,
sp->ts_prewordlen > 0);
if (score <= su->su_maxscore)
{
add_suggestion(su, &su->su_ga, preword,
sp->ts_fidx - repextra,
score, 0, FALSE, lp->lp_sallang, FALSE);
if (su->su_badflags & WF_MIXCAP)
{
c = captype(preword, NULL);
if (c == 0 || c == WF_ALLCAP)
{
make_case_word(tword + sp->ts_splitoff,
preword + sp->ts_prewordlen,
c == 0 ? WF_ALLCAP : 0);
add_suggestion(su, &su->su_ga, preword,
sp->ts_fidx - repextra,
score + SCORE_ICASE, 0, FALSE,
lp->lp_sallang, FALSE);
}
}
}
}
}
if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
#ifdef FEAT_MBYTE
&& (!has_mbyte || sp->ts_tcharlen == 0)
#endif
)
{
int try_compound;
int try_split;
try_split = (sp->ts_fidx - repextra < su->su_badlen)
&& !soundfold;
try_compound = FALSE;
if (!soundfold
&& slang->sl_compprog != NULL
&& ((unsigned)flags >> 24) != 0
&& sp->ts_twordlen - sp->ts_splitoff
>= slang->sl_compminlen
#ifdef FEAT_MBYTE
&& (!has_mbyte
|| slang->sl_compminlen == 0
|| mb_charlen(tword + sp->ts_splitoff)
>= slang->sl_compminlen)
#endif
&& (slang->sl_compsylmax < MAXWLEN
|| sp->ts_complen + 1 - sp->ts_compsplit
< slang->sl_compmax)
&& (can_be_compound(sp, slang,
compflags, ((unsigned)flags >> 24))))
{
try_compound = TRUE;
compflags[sp->ts_complen] = ((unsigned)flags >> 24);
compflags[sp->ts_complen + 1] = NUL;
}
if (slang->sl_nobreak)
try_compound = TRUE;
else if (!fword_ends
&& try_compound
&& (sp->ts_flags & TSF_DIDSPLIT) == 0)
{
try_compound = FALSE;
sp->ts_flags |= TSF_DIDSPLIT;
--sp->ts_curi;
compflags[sp->ts_complen] = NUL;
}
else
sp->ts_flags &= ~TSF_DIDSPLIT;
if (try_split || try_compound)
{
if (!try_compound && (!fword_ends || !goodword_ends))
{
if (sp->ts_complen == sp->ts_compsplit
&& (flags & WF_NEEDCOMP))
break;
p = preword;
while (*skiptowhite(p) != NUL)
p = skipwhite(skiptowhite(p));
if (sp->ts_complen > sp->ts_compsplit
&& !can_compound(slang, p,
compflags + sp->ts_compsplit))
break;
if (slang->sl_nosplitsugs)
newscore += SCORE_SPLIT_NO;
else
newscore += SCORE_SPLIT;
newscore = score_wordcount_adj(slang, newscore,
preword + sp->ts_prewordlen, TRUE);
}
if (TRY_DEEPER(su, stack, depth, newscore))
{
go_deeper(stack, depth, newscore);
#ifdef DEBUG_TRIEWALK
if (!try_compound && !fword_ends)
sprintf(changename[depth], "%.*s-%s: split",
sp->ts_twordlen, tword, fword + sp->ts_fidx);
else
sprintf(changename[depth], "%.*s-%s: compound",
sp->ts_twordlen, tword, fword + sp->ts_fidx);
#endif
sp->ts_save_badflags = su->su_badflags;
sp->ts_state = STATE_SPLITUNDO;
++depth;
sp = &stack[depth];
if (!try_compound && !fword_ends)
STRCAT(preword, " ");
sp->ts_prewordlen = (char_u)STRLEN(preword);
sp->ts_splitoff = sp->ts_twordlen;
sp->ts_splitfidx = sp->ts_fidx;
if (((!try_compound && !spell_iswordp_nmw(fword
+ sp->ts_fidx))
|| fword_ends)
&& fword[sp->ts_fidx] != NUL
&& goodword_ends)
{
int l;
#ifdef FEAT_MBYTE
if (has_mbyte)
l = MB_BYTE2LEN(fword[sp->ts_fidx]);
else
#endif
l = 1;
if (fword_ends)
{
mch_memmove(preword + sp->ts_prewordlen,
fword + sp->ts_fidx, l);
sp->ts_prewordlen += l;
preword[sp->ts_prewordlen] = NUL;
}
else
sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
sp->ts_fidx += l;
}
if (try_compound)
++sp->ts_complen;
else
sp->ts_compsplit = sp->ts_complen;
sp->ts_prefixdepth = PFD_NOPREFIX;
#ifdef FEAT_MBYTE
if (has_mbyte)
n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
else
#endif
n = sp->ts_fidx;
su->su_badflags = badword_captype(su->su_badptr + n,
su->su_badptr + su->su_badlen);
sp->ts_arridx = 0;
if (pbyts != NULL)
{
byts = pbyts;
idxs = pidxs;
sp->ts_prefixdepth = PFD_PREFIXTREE;
sp->ts_state = STATE_NOPREFIX;
}
}
}
}
break;
case STATE_SPLITUNDO:
su->su_badflags = sp->ts_save_badflags;
sp->ts_state = STATE_START;
byts = fbyts;
idxs = fidxs;
break;
case STATE_ENDNUL:
su->su_badflags = sp->ts_save_badflags;
if (fword[sp->ts_fidx] == NUL
#ifdef FEAT_MBYTE
&& sp->ts_tcharlen == 0
#endif
)
{
sp->ts_state = STATE_DEL;
break;
}
sp->ts_state = STATE_PLAIN;
case STATE_PLAIN:
arridx = sp->ts_arridx;
if (sp->ts_curi > byts[arridx])
{
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];
if (c == fword[sp->ts_fidx]
#ifdef FEAT_MBYTE
|| (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
#endif
)
newscore = 0;
else
newscore = SCORE_SUBST;
if ((newscore == 0
|| (sp->ts_fidx >= sp->ts_fidxtry
&& ((sp->ts_flags & TSF_DIDDEL) == 0
|| c != fword[sp->ts_delidx])))
&& TRY_DEEPER(su, stack, depth, newscore))
{
go_deeper(stack, depth, newscore);
#ifdef DEBUG_TRIEWALK
if (newscore > 0)
sprintf(changename[depth], "%.*s-%s: subst %c to %c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
fword[sp->ts_fidx], c);
else
sprintf(changename[depth], "%.*s-%s: accept %c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
fword[sp->ts_fidx]);
#endif
++depth;
sp = &stack[depth];
++sp->ts_fidx;
tword[sp->ts_twordlen++] = c;
sp->ts_arridx = idxs[arridx];
#ifdef FEAT_MBYTE
if (newscore == SCORE_SUBST)
sp->ts_isdiff = DIFF_YES;
if (has_mbyte)
{
if (sp->ts_tcharlen == 0)
{
sp->ts_tcharidx = 0;
sp->ts_tcharlen = MB_BYTE2LEN(c);
sp->ts_fcharstart = sp->ts_fidx - 1;
sp->ts_isdiff = (newscore != 0)
? DIFF_YES : DIFF_NONE;
}
else if (sp->ts_isdiff == DIFF_INSERT)
--sp->ts_fidx;
if (++sp->ts_tcharidx == sp->ts_tcharlen)
{
if (sp->ts_isdiff == DIFF_YES)
{
sp->ts_fidx = sp->ts_fcharstart
+ MB_BYTE2LEN(
fword[sp->ts_fcharstart]);
if (enc_utf8
&& utf_iscomposing(
mb_ptr2char(tword
+ sp->ts_twordlen
- sp->ts_tcharlen))
&& utf_iscomposing(
mb_ptr2char(fword
+ sp->ts_fcharstart)))
sp->ts_score -=
SCORE_SUBST - SCORE_SUBCOMP;
else if (!soundfold
&& slang->sl_has_map
&& similar_chars(slang,
mb_ptr2char(tword
+ sp->ts_twordlen
- sp->ts_tcharlen),
mb_ptr2char(fword
+ sp->ts_fcharstart)))
sp->ts_score -=
SCORE_SUBST - SCORE_SIMILAR;
}
else if (sp->ts_isdiff == DIFF_INSERT
&& sp->ts_twordlen > sp->ts_tcharlen)
{
p = tword + sp->ts_twordlen - sp->ts_tcharlen;
c = mb_ptr2char(p);
if (enc_utf8 && utf_iscomposing(c))
{
sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
}
else
{
mb_ptr_back(tword, p);
if (c == mb_ptr2char(p))
sp->ts_score -= SCORE_INS
- SCORE_INSDUP;
}
}
sp->ts_tcharlen = 0;
}
}
else
#endif
{
if (newscore != 0
&& !soundfold
&& slang->sl_has_map
&& similar_chars(slang,
c, fword[sp->ts_fidx - 1]))
sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
}
}
}
break;
case STATE_DEL:
#ifdef FEAT_MBYTE
if (has_mbyte && sp->ts_tcharlen > 0)
{
sp->ts_state = STATE_FINAL;
break;
}
#endif
sp->ts_state = STATE_INS_PREP;
sp->ts_curi = 1;
if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
newscore = 2 * SCORE_DEL / 3;
else
newscore = SCORE_DEL;
if (fword[sp->ts_fidx] != NUL
&& TRY_DEEPER(su, stack, depth, newscore))
{
go_deeper(stack, depth, newscore);
#ifdef DEBUG_TRIEWALK
sprintf(changename[depth], "%.*s-%s: delete %c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
fword[sp->ts_fidx]);
#endif
++depth;
stack[depth].ts_flags |= TSF_DIDDEL;
stack[depth].ts_delidx = sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
c = mb_ptr2char(fword + sp->ts_fidx);
stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
if (enc_utf8 && utf_iscomposing(c))
stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
}
else
#endif
{
++stack[depth].ts_fidx;
if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
}
break;
}
case STATE_INS_PREP:
if (sp->ts_flags & TSF_DIDDEL)
{
sp->ts_state = STATE_SWAP;
break;
}
n = sp->ts_arridx;
for (;;)
{
if (sp->ts_curi > byts[n])
{
sp->ts_state = STATE_SWAP;
break;
}
if (byts[n + sp->ts_curi] != NUL)
{
sp->ts_state = STATE_INS;
break;
}
++sp->ts_curi;
}
break;
case STATE_INS:
n = sp->ts_arridx;
if (sp->ts_curi > byts[n])
{
sp->ts_state = STATE_SWAP;
break;
}
n += sp->ts_curi++;
c = byts[n];
if (soundfold && sp->ts_twordlen == 0 && c == '*')
newscore = 2 * SCORE_INS / 3;
else
newscore = SCORE_INS;
if (c != fword[sp->ts_fidx]
&& TRY_DEEPER(su, stack, depth, newscore))
{
go_deeper(stack, depth, newscore);
#ifdef DEBUG_TRIEWALK
sprintf(changename[depth], "%.*s-%s: insert %c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
c);
#endif
++depth;
sp = &stack[depth];
tword[sp->ts_twordlen++] = c;
sp->ts_arridx = idxs[n];
#ifdef FEAT_MBYTE
if (has_mbyte)
{
fl = MB_BYTE2LEN(c);
if (fl > 1)
{
sp->ts_tcharlen = fl;
sp->ts_tcharidx = 1;
sp->ts_isdiff = DIFF_INSERT;
}
}
else
fl = 1;
if (fl == 1)
#endif
{
if (sp->ts_twordlen >= 2
&& tword[sp->ts_twordlen - 2] == c)
sp->ts_score -= SCORE_INS - SCORE_INSDUP;
}
}
break;
case STATE_SWAP:
p = fword + sp->ts_fidx;
c = *p;
if (c == NUL)
{
sp->ts_state = STATE_FINAL;
break;
}
if (!soundfold && !spell_iswordp(p, curwin))
{
sp->ts_state = STATE_REP_INI;
break;
}
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = mb_cptr2len(p);
c = mb_ptr2char(p);
if (p[n] == NUL)
c2 = NUL;
else if (!soundfold && !spell_iswordp(p + n, curwin))
c2 = c;
else
c2 = mb_ptr2char(p + n);
}
else
#endif
{
if (p[1] == NUL)
c2 = NUL;
else if (!soundfold && !spell_iswordp(p + 1, curwin))
c2 = c;
else
c2 = p[1];
}
if (c2 == NUL)
{
sp->ts_state = STATE_REP_INI;
break;
}
if (c == c2)
{
sp->ts_state = STATE_SWAP3;
break;
}
if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
{
go_deeper(stack, depth, SCORE_SWAP);
#ifdef DEBUG_TRIEWALK
sprintf(changename[depth], "%.*s-%s: swap %c and %c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
c, c2);
#endif
sp->ts_state = STATE_UNSWAP;
++depth;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
fl = mb_char2len(c2);
mch_memmove(p, p + n, fl);
mb_char2bytes(c, p + fl);
stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
}
else
#endif
{
p[0] = c2;
p[1] = c;
stack[depth].ts_fidxtry = sp->ts_fidx + 2;
}
}
else
sp->ts_state = STATE_REP_INI;
break;
case STATE_UNSWAP:
p = fword + sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = MB_BYTE2LEN(*p);
c = mb_ptr2char(p + n);
mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
mb_char2bytes(c, p);
}
else
#endif
{
c = *p;
*p = p[1];
p[1] = c;
}
case STATE_SWAP3:
p = fword + sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = mb_cptr2len(p);
c = mb_ptr2char(p);
fl = mb_cptr2len(p + n);
c2 = mb_ptr2char(p + n);
if (!soundfold && !spell_iswordp(p + n + fl, curwin))
c3 = c;
else
c3 = mb_ptr2char(p + n + fl);
}
else
#endif
{
c = *p;
c2 = p[1];
if (!soundfold && !spell_iswordp(p + 2, curwin))
c3 = c;
else
c3 = p[2];
}
if (c == c3 || c3 == NUL)
{
sp->ts_state = STATE_REP_INI;
break;
}
if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
{
go_deeper(stack, depth, SCORE_SWAP3);
#ifdef DEBUG_TRIEWALK
sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
c, c3);
#endif
sp->ts_state = STATE_UNSWAP3;
++depth;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
tl = mb_char2len(c3);
mch_memmove(p, p + n + fl, tl);
mb_char2bytes(c2, p + tl);
mb_char2bytes(c, p + fl + tl);
stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
}
else
#endif
{
p[0] = p[2];
p[2] = c;
stack[depth].ts_fidxtry = sp->ts_fidx + 3;
}
}
else
sp->ts_state = STATE_REP_INI;
break;
case STATE_UNSWAP3:
p = fword + sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = MB_BYTE2LEN(*p);
c2 = mb_ptr2char(p + n);
fl = MB_BYTE2LEN(p[n]);
c = mb_ptr2char(p + n + fl);
tl = MB_BYTE2LEN(p[n + fl]);
mch_memmove(p + fl + tl, p, n);
mb_char2bytes(c, p);
mb_char2bytes(c2, p + tl);
p = p + tl;
}
else
#endif
{
c = *p;
*p = p[2];
p[2] = c;
++p;
}
if (!soundfold && !spell_iswordp(p, curwin))
{
sp->ts_state = STATE_REP_INI;
break;
}
if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
{
go_deeper(stack, depth, SCORE_SWAP3);
#ifdef DEBUG_TRIEWALK
p = fword + sp->ts_fidx;
sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
p[0], p[1], p[2]);
#endif
sp->ts_state = STATE_UNROT3L;
++depth;
p = fword + sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = mb_cptr2len(p);
c = mb_ptr2char(p);
fl = mb_cptr2len(p + n);
fl += mb_cptr2len(p + n + fl);
mch_memmove(p, p + n, fl);
mb_char2bytes(c, p + fl);
stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
}
else
#endif
{
c = *p;
*p = p[1];
p[1] = p[2];
p[2] = c;
stack[depth].ts_fidxtry = sp->ts_fidx + 3;
}
}
else
sp->ts_state = STATE_REP_INI;
break;
case STATE_UNROT3L:
p = fword + sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = MB_BYTE2LEN(*p);
n += MB_BYTE2LEN(p[n]);
c = mb_ptr2char(p + n);
tl = MB_BYTE2LEN(p[n]);
mch_memmove(p + tl, p, n);
mb_char2bytes(c, p);
}
else
#endif
{
c = p[2];
p[2] = p[1];
p[1] = *p;
*p = c;
}
if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
{
go_deeper(stack, depth, SCORE_SWAP3);
#ifdef DEBUG_TRIEWALK
p = fword + sp->ts_fidx;
sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
p[0], p[1], p[2]);
#endif
sp->ts_state = STATE_UNROT3R;
++depth;
p = fword + sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = mb_cptr2len(p);
n += mb_cptr2len(p + n);
c = mb_ptr2char(p + n);
tl = mb_cptr2len(p + n);
mch_memmove(p + tl, p, n);
mb_char2bytes(c, p);
stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
}
else
#endif
{
c = p[2];
p[2] = p[1];
p[1] = *p;
*p = c;
stack[depth].ts_fidxtry = sp->ts_fidx + 3;
}
}
else
sp->ts_state = STATE_REP_INI;
break;
case STATE_UNROT3R:
p = fword + sp->ts_fidx;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
c = mb_ptr2char(p);
tl = MB_BYTE2LEN(*p);
n = MB_BYTE2LEN(p[tl]);
n += MB_BYTE2LEN(p[tl + n]);
mch_memmove(p, p + tl, n);
mb_char2bytes(c, p + n);
}
else
#endif
{
c = *p;
*p = p[1];
p[1] = p[2];
p[2] = c;
}
case STATE_REP_INI:
if ((lp->lp_replang == NULL && !soundfold)
|| sp->ts_score + SCORE_REP >= su->su_maxscore
|| sp->ts_fidx < sp->ts_fidxtry)
{
sp->ts_state = STATE_FINAL;
break;
}
if (soundfold)
sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
else
sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
if (sp->ts_curi < 0)
{
sp->ts_state = STATE_FINAL;
break;
}
sp->ts_state = STATE_REP;
case STATE_REP:
p = fword + sp->ts_fidx;
if (soundfold)
gap = &slang->sl_repsal;
else
gap = &lp->lp_replang->sl_rep;
while (sp->ts_curi < gap->ga_len)
{
ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
if (*ftp->ft_from != *p)
{
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))
{
go_deeper(stack, depth, SCORE_REP);
#ifdef DEBUG_TRIEWALK
sprintf(changename[depth], "%.*s-%s: replace %s with %s",
sp->ts_twordlen, tword, fword + sp->ts_fidx,
ftp->ft_from, ftp->ft_to);
#endif
sp->ts_state = STATE_REP_UNDO;
++depth;
fl = (int)STRLEN(ftp->ft_from);
tl = (int)STRLEN(ftp->ft_to);
if (fl != tl)
{
STRMOVE(p + tl, p + fl);
repextra += tl - fl;
}
mch_memmove(p, ftp->ft_to, tl);
stack[depth].ts_fidxtry = sp->ts_fidx + tl;
#ifdef FEAT_MBYTE
stack[depth].ts_tcharlen = 0;
#endif
break;
}
}
if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
sp->ts_state = STATE_FINAL;
break;
case STATE_REP_UNDO:
if (soundfold)
gap = &slang->sl_repsal;
else
gap = &lp->lp_replang->sl_rep;
ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
fl = (int)STRLEN(ftp->ft_from);
tl = (int)STRLEN(ftp->ft_to);
p = fword + sp->ts_fidx;
if (fl != tl)
{
STRMOVE(p + fl, p + tl);
repextra -= tl - fl;
}
mch_memmove(p, ftp->ft_from, fl);
sp->ts_state = STATE_REP;
break;
default:
--depth;
if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
{
byts = pbyts;
idxs = pidxs;
}
if (--breakcheckcount == 0)
{
ui_breakcheck();
breakcheckcount = 1000;
}
}
}
}
static void
go_deeper(stack, depth, score_add)
trystate_T *stack;
int depth;
int score_add;
{
stack[depth + 1] = stack[depth];
stack[depth + 1].ts_state = STATE_START;
stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
stack[depth + 1].ts_curi = 1;
stack[depth + 1].ts_flags = 0;
}
#ifdef FEAT_MBYTE
static int
nofold_len(fword, flen, word)
char_u *fword;
int flen;
char_u *word;
{
char_u *p;
int i = 0;
for (p = fword; p < fword + flen; mb_ptr_adv(p))
++i;
for (p = word; i > 0; mb_ptr_adv(p))
--i;
return (int)(p - word);
}
#endif
static void
find_keepcap_word(slang, fword, kword)
slang_T *slang;
char_u *fword;
char_u *kword;
{
char_u uword[MAXWLEN];
int depth;
idx_T tryidx;
idx_T arridx[MAXWLEN];
int round[MAXWLEN];
int fwordidx[MAXWLEN];
int uwordidx[MAXWLEN];
int kwordlen[MAXWLEN];
int flen, ulen;
int l;
int len;
int c;
idx_T lo, hi, m;
char_u *p;
char_u *byts = slang->sl_kbyts;
idx_T *idxs = slang->sl_kidxs;
if (byts == NULL)
{
*kword = NUL;
return;
}
allcap_copy(fword, uword);
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)
{
if (byts[arridx[depth] + 1] == 0)
{
kword[kwordlen[depth]] = NUL;
return;
}
--depth;
}
else if (++round[depth] > 2)
{
--depth;
}
else
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
flen = mb_cptr2len(fword + fwordidx[depth]);
ulen = mb_cptr2len(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)
{
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;
}
}
if (hi < lo || byts[lo] != c)
break;
tryidx = idxs[lo];
}
if (l == 0)
{
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;
}
}
}
*kword = NUL;
}
static void
score_comp_sal(su)
suginfo_T *su;
{
langp_T *lp;
char_u badsound[MAXWLEN];
int i;
suggest_T *stp;
suggest_T *sstp;
int score;
int lpi;
if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
return;
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
if (lp->lp_slang->sl_sal.ga_len > 0)
{
spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
for (i = 0; i < su->su_ga.ga_len; ++i)
{
stp = &SUG(su->su_ga, i);
score = stp_sal_score(stp, su, lp->lp_slang, badsound);
if (score < SCORE_MAXMAX)
{
sstp = &SUG(su->su_sga, su->su_sga.ga_len);
sstp->st_word = vim_strsave(stp->st_word);
if (sstp->st_word != NULL)
{
sstp->st_wordlen = stp->st_wordlen;
sstp->st_score = score;
sstp->st_altscore = 0;
sstp->st_orglen = stp->st_orglen;
++su->su_sga.ga_len;
}
}
}
break;
}
}
}
static void
score_combine(su)
suginfo_T *su;
{
int i;
int j;
garray_T ga;
garray_T *gap;
langp_T *lp;
suggest_T *stp;
char_u *p;
char_u badsound[MAXWLEN];
int round;
int lpi;
slang_T *slang = NULL;
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
if (lp->lp_slang->sl_sal.ga_len > 0)
{
slang = lp->lp_slang;
spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
for (i = 0; i < su->su_ga.ga_len; ++i)
{
stp = &SUG(su->su_ga, i);
stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
if (stp->st_altscore == SCORE_MAXMAX)
stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
else
stp->st_score = (stp->st_score * 3
+ stp->st_altscore) / 4;
stp->st_salscore = FALSE;
}
break;
}
}
if (slang == NULL)
{
(void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
su->su_maxcount);
return;
}
for (i = 0; i < su->su_sga.ga_len; ++i)
{
stp = &SUG(su->su_sga, i);
stp->st_altscore = spell_edit_score(slang,
su->su_badword, stp->st_word);
if (stp->st_score == SCORE_MAXMAX)
stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
else
stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
stp->st_salscore = TRUE;
}
check_suggestions(su, &su->su_ga);
(void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
check_suggestions(su, &su->su_sga);
(void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
ga_init2(&ga, (int)sizeof(suginfo_T), 1);
if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
return;
stp = &SUG(ga, 0);
for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
{
for (round = 1; round <= 2; ++round)
{
gap = round == 1 ? &su->su_ga : &su->su_sga;
if (i < gap->ga_len)
{
p = SUG(*gap, i).st_word;
for (j = 0; j < ga.ga_len; ++j)
if (STRCMP(stp[j].st_word, p) == 0)
break;
if (j == ga.ga_len)
stp[ga.ga_len++] = SUG(*gap, i);
else
vim_free(p);
}
}
}
ga_clear(&su->su_ga);
ga_clear(&su->su_sga);
if (ga.ga_len > su->su_maxcount)
{
for (i = su->su_maxcount; i < ga.ga_len; ++i)
vim_free(stp[i].st_word);
ga.ga_len = su->su_maxcount;
}
su->su_ga = ga;
}
static int
stp_sal_score(stp, su, slang, badsound)
suggest_T *stp;
suginfo_T *su;
slang_T *slang;
char_u *badsound;
{
char_u *p;
char_u *pbad;
char_u *pgood;
char_u badsound2[MAXWLEN];
char_u fword[MAXWLEN];
char_u goodsound[MAXWLEN];
char_u goodword[MAXWLEN];
int lendiff;
lendiff = (int)(su->su_badlen - stp->st_orglen);
if (lendiff >= 0)
pbad = badsound;
else
{
(void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
if (vim_iswhite(su->su_badptr[su->su_badlen])
&& *skiptowhite(stp->st_word) == NUL)
for (p = fword; *(p = skiptowhite(p)) != NUL; )
STRMOVE(p, p + 1);
spell_soundfold(slang, fword, TRUE, badsound2);
pbad = badsound2;
}
if (lendiff > 0)
{
STRCPY(goodword, stp->st_word);
vim_strncpy(goodword + stp->st_wordlen,
su->su_badptr + su->su_badlen - lendiff, lendiff);
pgood = goodword;
}
else
pgood = stp->st_word;
spell_soundfold(slang, pgood, FALSE, goodsound);
return soundalike_score(goodsound, pbad);
}
typedef struct
{
short sft_score;
char_u sft_word[1];
} sftword_T;
static sftword_T dumsft;
#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
static void
suggest_try_soundalike_prep()
{
langp_T *lp;
int lpi;
slang_T *slang;
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
slang = lp->lp_slang;
if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
hash_init(&slang->sl_sounddone);
}
}
static void
suggest_try_soundalike(su)
suginfo_T *su;
{
char_u salword[MAXWLEN];
langp_T *lp;
int lpi;
slang_T *slang;
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
slang = lp->lp_slang;
if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
{
spell_soundfold(slang, su->su_fbadword, TRUE, salword);
suggest_trie_walk(su, lp, salword, TRUE);
}
}
}
static void
suggest_try_soundalike_finish()
{
langp_T *lp;
int lpi;
slang_T *slang;
int todo;
hashitem_T *hi;
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
slang = lp->lp_slang;
if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
{
todo = (int)slang->sl_sounddone.ht_used;
for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
if (!HASHITEM_EMPTY(hi))
{
vim_free(HI2SFT(hi));
--todo;
}
hash_clear(&slang->sl_sounddone);
hash_init(&slang->sl_sounddone);
}
}
}
static void
add_sound_suggest(su, goodword, score, lp)
suginfo_T *su;
char_u *goodword;
int score;
langp_T *lp;
{
slang_T *slang = lp->lp_slang;
int sfwordnr;
char_u *nrline;
int orgnr;
char_u theword[MAXWLEN];
int i;
int wlen;
char_u *byts;
idx_T *idxs;
int n;
int wordcount;
int wc;
int goodscore;
hash_T hash;
hashitem_T *hi;
sftword_T *sft;
int bc, gc;
int limit;
hash = hash_hash(goodword);
hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
if (HASHITEM_EMPTY(hi))
{
sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
+ STRLEN(goodword)));
if (sft != NULL)
{
sft->sft_score = score;
STRCPY(sft->sft_word, goodword);
hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
}
}
else
{
sft = HI2SFT(hi);
if (score >= sft->sft_score)
return;
sft->sft_score = score;
}
sfwordnr = soundfold_find(slang, goodword);
if (sfwordnr < 0)
{
EMSG2(_(e_intern2), "add_sound_suggest()");
return;
}
nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
orgnr = 0;
while (*nrline != NUL)
{
orgnr += bytes2offset(&nrline);
byts = slang->sl_fbyts;
idxs = slang->sl_fidxs;
n = 0;
wlen = 0;
wordcount = 0;
for (;;)
{
i = 1;
if (wordcount == orgnr && byts[n + 1] == NUL)
break;
if (byts[n + 1] == NUL)
++wordcount;
for ( ; byts[n + i] == NUL; ++i)
if (i > byts[n])
{
STRCPY(theword + wlen, "BAD");
goto badword;
}
for ( ; i < byts[n]; ++i)
{
wc = idxs[idxs[n + i]];
if (wordcount + wc > orgnr)
break;
wordcount += wc;
}
theword[wlen++] = byts[n + i];
n = idxs[n + i];
}
badword:
theword[wlen] = NUL;
for (; i <= byts[n] && byts[n + i] == NUL; ++i)
{
char_u cword[MAXWLEN];
char_u *p;
int flags = (int)idxs[n + i];
if (flags & WF_NOSUGGEST)
continue;
if (flags & WF_KEEPCAP)
{
find_keepcap_word(slang, theword, cword);
p = cword;
}
else
{
flags |= su->su_badflags;
if ((flags & WF_CAPMASK) != 0)
{
make_case_word(theword, cword, flags);
p = cword;
}
else
p = theword;
}
if (sps_flags & SPS_DOUBLE)
{
if (score <= su->su_maxscore)
add_suggestion(su, &su->su_sga, p, su->su_badlen,
score, 0, FALSE, slang, FALSE);
}
else
{
if ((flags & WF_REGION)
&& (((unsigned)flags >> 16) & lp->lp_region) == 0)
goodscore = SCORE_REGION;
else
goodscore = 0;
gc = PTR2CHAR(p);
if (SPELL_ISUPPER(gc))
{
bc = PTR2CHAR(su->su_badword);
if (!SPELL_ISUPPER(bc)
&& SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
goodscore += SCORE_ICASE / 2;
}
limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
if (limit > SCORE_LIMITMAX)
goodscore += spell_edit_score(slang, su->su_badword, p);
else
goodscore += spell_edit_score_limit(slang, su->su_badword,
p, limit);
if (goodscore < SCORE_MAXMAX)
{
goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
goodscore = RESCORE(goodscore, score);
if (goodscore <= su->su_sfmaxscore)
add_suggestion(su, &su->su_ga, p, su->su_badlen,
goodscore, score, TRUE, slang, TRUE);
}
}
}
}
}
static int
soundfold_find(slang, word)
slang_T *slang;
char_u *word;
{
idx_T arridx = 0;
int len;
int wlen = 0;
int c;
char_u *ptr = word;
char_u *byts;
idx_T *idxs;
int wordnr = 0;
byts = slang->sl_sbyts;
idxs = slang->sl_sidxs;
for (;;)
{
len = byts[arridx++];
c = ptr[wlen];
if (byts[arridx] == NUL)
{
if (c == NUL)
break;
while (len > 0 && byts[arridx] == NUL)
{
++arridx;
--len;
}
if (len == 0)
return -1;
++wordnr;
}
if (c == NUL)
return -1;
if (c == TAB)
c = ' ';
while (byts[arridx] < c)
{
wordnr += idxs[idxs[arridx]];
++arridx;
if (--len == 0)
return -1;
}
if (byts[arridx] != c)
return -1;
arridx = idxs[arridx];
++wlen;
if (c == ' ')
while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
++wlen;
}
return wordnr;
}
static void
make_case_word(fword, cword, flags)
char_u *fword;
char_u *cword;
int flags;
{
if (flags & WF_ALLCAP)
allcap_copy(fword, cword);
else if (flags & WF_ONECAP)
onecap_copy(fword, cword, TRUE);
else
STRCPY(cword, fword);
}
static void
set_map_str(lp, map)
slang_T *lp;
char_u *map;
{
char_u *p;
int headc = 0;
int c;
int i;
if (*map == NUL)
{
lp->sl_has_map = FALSE;
return;
}
lp->sl_has_map = TRUE;
for (i = 0; i < 256; ++i)
lp->sl_map_array[i] = 0;
#ifdef FEAT_MBYTE
hash_init(&lp->sl_map_hash);
#endif
for (p = map; *p != NUL; )
{
#ifdef FEAT_MBYTE
c = mb_cptr2char_adv(&p);
#else
c = *p++;
#endif
if (c == '/')
headc = 0;
else
{
if (headc == 0)
headc = c;
#ifdef FEAT_MBYTE
if (c >= 256)
{
int cl = mb_char2len(c);
int headcl = mb_char2len(headc);
char_u *b;
hash_T hash;
hashitem_T *hi;
b = alloc((unsigned)(cl + headcl + 2));
if (b == NULL)
return;
mb_char2bytes(c, b);
b[cl] = NUL;
mb_char2bytes(headc, b + cl + 1);
b[cl + 1 + headcl] = NUL;
hash = hash_hash(b);
hi = hash_lookup(&lp->sl_map_hash, b, hash);
if (HASHITEM_EMPTY(hi))
hash_add_item(&lp->sl_map_hash, hi, b, hash);
else
{
EMSG(_("E783: duplicate char in MAP entry"));
vim_free(b);
}
}
else
#endif
lp->sl_map_array[c] = headc;
}
}
}
static int
similar_chars(slang, c1, c2)
slang_T *slang;
int c1;
int c2;
{
int m1, m2;
#ifdef FEAT_MBYTE
char_u buf[MB_MAXBYTES];
hashitem_T *hi;
if (c1 >= 256)
{
buf[mb_char2bytes(c1, buf)] = 0;
hi = hash_find(&slang->sl_map_hash, buf);
if (HASHITEM_EMPTY(hi))
m1 = 0;
else
m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
}
else
#endif
m1 = slang->sl_map_array[c1];
if (m1 == 0)
return FALSE;
#ifdef FEAT_MBYTE
if (c2 >= 256)
{
buf[mb_char2bytes(c2, buf)] = 0;
hi = hash_find(&slang->sl_map_hash, buf);
if (HASHITEM_EMPTY(hi))
m2 = 0;
else
m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
}
else
#endif
m2 = slang->sl_map_array[c2];
return m1 == m2;
}
static void
add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
slang, maxsf)
suginfo_T *su;
garray_T *gap;
char_u *goodword;
int badlenarg;
int score;
int altscore;
int had_bonus;
slang_T *slang;
int maxsf;
{
int goodlen;
int badlen;
suggest_T *stp;
suggest_T new_sug;
int i;
char_u *pgood, *pbad;
pgood = goodword + STRLEN(goodword);
pbad = su->su_badptr + badlenarg;
for (;;)
{
goodlen = (int)(pgood - goodword);
badlen = (int)(pbad - su->su_badptr);
if (goodlen <= 0 || badlen <= 0)
break;
mb_ptr_back(goodword, pgood);
mb_ptr_back(su->su_badptr, pbad);
#ifdef FEAT_MBYTE
if (has_mbyte)
{
if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
break;
}
else
#endif
if (*pgood != *pbad)
break;
}
if (badlen == 0 && goodlen == 0)
return;
if (gap->ga_len == 0)
i = -1;
else
{
stp = &SUG(*gap, 0);
for (i = gap->ga_len; --i >= 0; ++stp)
if (stp->st_wordlen == goodlen
&& stp->st_orglen == badlen
&& STRNCMP(stp->st_word, goodword, goodlen) == 0)
{
if (stp->st_slang == NULL)
stp->st_slang = slang;
new_sug.st_score = score;
new_sug.st_altscore = altscore;
new_sug.st_had_bonus = had_bonus;
if (stp->st_had_bonus != had_bonus)
{
if (had_bonus)
rescore_one(su, stp);
else
{
new_sug.st_word = stp->st_word;
new_sug.st_wordlen = stp->st_wordlen;
new_sug.st_slang = stp->st_slang;
new_sug.st_orglen = badlen;
rescore_one(su, &new_sug);
}
}
if (stp->st_score > new_sug.st_score)
{
stp->st_score = new_sug.st_score;
stp->st_altscore = new_sug.st_altscore;
stp->st_had_bonus = new_sug.st_had_bonus;
}
break;
}
}
if (i < 0 && ga_grow(gap, 1) == OK)
{
stp = &SUG(*gap, gap->ga_len);
stp->st_word = vim_strnsave(goodword, goodlen);
if (stp->st_word != NULL)
{
stp->st_wordlen = goodlen;
stp->st_score = score;
stp->st_altscore = altscore;
stp->st_had_bonus = had_bonus;
stp->st_orglen = badlen;
stp->st_slang = slang;
++gap->ga_len;
if (gap->ga_len > SUG_MAX_COUNT(su))
{
if (maxsf)
su->su_sfmaxscore = cleanup_suggestions(gap,
su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
else
{
i = su->su_maxscore;
su->su_maxscore = cleanup_suggestions(gap,
su->su_maxscore, SUG_CLEAN_COUNT(su));
}
}
}
}
}
static void
check_suggestions(su, gap)
suginfo_T *su;
garray_T *gap;
{
suggest_T *stp;
int i;
char_u longword[MAXWLEN + 1];
int len;
hlf_T attr;
stp = &SUG(*gap, 0);
for (i = gap->ga_len - 1; i >= 0; --i)
{
STRCPY(longword, stp[i].st_word);
len = stp[i].st_wordlen;
vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
MAXWLEN - len);
attr = HLF_COUNT;
(void)spell_check(curwin, longword, &attr, NULL, FALSE);
if (attr != HLF_COUNT)
{
vim_free(stp[i].st_word);
--gap->ga_len;
if (i < gap->ga_len)
mch_memmove(stp + i, stp + i + 1,
sizeof(suggest_T) * (gap->ga_len - i));
}
}
}
static void
add_banned(su, word)
suginfo_T *su;
char_u *word;
{
char_u *s;
hash_T hash;
hashitem_T *hi;
hash = hash_hash(word);
hi = hash_lookup(&su->su_banned, word, hash);
if (HASHITEM_EMPTY(hi))
{
s = vim_strsave(word);
if (s != NULL)
hash_add_item(&su->su_banned, hi, s, hash);
}
}
static void
rescore_suggestions(su)
suginfo_T *su;
{
int i;
if (su->su_sallang != NULL)
for (i = 0; i < su->su_ga.ga_len; ++i)
rescore_one(su, &SUG(su->su_ga, i));
}
static void
rescore_one(su, stp)
suginfo_T *su;
suggest_T *stp;
{
slang_T *slang = stp->st_slang;
char_u sal_badword[MAXWLEN];
char_u *p;
if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
{
if (slang == su->su_sallang)
p = su->su_sal_badword;
else
{
spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
p = sal_badword;
}
stp->st_altscore = stp_sal_score(stp, su, slang, p);
if (stp->st_altscore == SCORE_MAXMAX)
stp->st_altscore = SCORE_BIG;
stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
stp->st_had_bonus = TRUE;
}
}
static int
#ifdef __BORLANDC__
_RTLENTRYF
#endif
sug_compare __ARGS((const void *s1, const void *s2));
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;
int n = p1->st_score - p2->st_score;
if (n == 0)
{
n = p1->st_altscore - p2->st_altscore;
if (n == 0)
n = STRICMP(p1->st_word, p2->st_word);
}
return n;
}
static int
cleanup_suggestions(gap, maxscore, keep)
garray_T *gap;
int maxscore;
int keep;
{
suggest_T *stp = &SUG(*gap, 0);
int i;
qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
if (gap->ga_len > keep)
{
for (i = keep; i < gap->ga_len; ++i)
vim_free(stp[i].st_word);
gap->ga_len = keep;
return stp[keep - 1].st_score;
}
return maxscore;
}
#if defined(FEAT_EVAL) || defined(PROTO)
char_u *
eval_soundfold(word)
char_u *word;
{
langp_T *lp;
char_u sound[MAXWLEN];
int lpi;
if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
if (lp->lp_slang->sl_sal.ga_len > 0)
{
spell_soundfold(lp->lp_slang, word, FALSE, sound);
return vim_strsave(sound);
}
}
return vim_strsave(word);
}
#endif
static void
spell_soundfold(slang, inword, folded, res)
slang_T *slang;
char_u *inword;
int folded;
char_u *res;
{
char_u fword[MAXWLEN];
char_u *word;
if (slang->sl_sofo)
spell_soundfold_sofo(slang, inword, res);
else
{
if (folded)
word = inword;
else
{
(void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
word = fword;
}
#ifdef FEAT_MBYTE
if (has_mbyte)
spell_soundfold_wsal(slang, word, res);
else
#endif
spell_soundfold_sal(slang, word, res);
}
}
static void
spell_soundfold_sofo(slang, inword, res)
slang_T *slang;
char_u *inword;
char_u *res;
{
char_u *s;
int ri = 0;
int c;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int prevc = 0;
int *ip;
for (s = inword; *s != NUL; )
{
c = mb_cptr2char_adv(&s);
if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
c = ' ';
else if (c < 256)
c = slang->sl_sal_first[c];
else
{
ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
if (ip == NULL)
c = NUL;
else
for (;;)
{
if (*ip == 0)
{
c = NUL;
break;
}
if (*ip == c)
{
c = ip[1];
break;
}
ip += 2;
}
}
if (c != NUL && c != prevc)
{
ri += mb_char2bytes(c, res + ri);
if (ri + MB_MAXBYTES > MAXWLEN)
break;
prevc = c;
}
}
}
else
#endif
{
for (s = inword; (c = *s) != NUL; ++s)
{
if (vim_iswhite(c))
c = ' ';
else
c = slang->sl_sal_first[c];
if (c != NUL && (ri == 0 || res[ri - 1] != c))
res[ri++] = c;
}
}
res[ri] = NUL;
}
static void
spell_soundfold_sal(slang, inword, res)
slang_T *slang;
char_u *inword;
char_u *res;
{
salitem_T *smp;
char_u word[MAXWLEN];
char_u *s = inword;
char_u *t;
char_u *pf;
int i, j, z;
int reslen;
int n, k = 0;
int z0;
int k0;
int n0;
int c;
int pri;
int p0 = -333;
int c0;
if (slang->sl_rem_accents)
{
t = word;
while (*s != NUL)
{
if (vim_iswhite(*s))
{
*t++ = ' ';
s = skipwhite(s);
}
else
{
if (spell_iswordp_nmw(s))
*t++ = *s;
++s;
}
}
*t = NUL;
}
else
STRCPY(word, s);
smp = (salitem_T *)slang->sl_sal.ga_data;
i = reslen = z = 0;
while ((c = word[i]) != NUL)
{
n = slang->sl_sal_first[c];
z0 = 0;
if (n >= 0)
{
for (; (s = smp[n].sm_lead)[0] == c; ++n)
{
k = smp[n].sm_leadlen;
if (k > 1)
{
if (word[i + 1] != s[1])
continue;
if (k > 2)
{
for (j = 2; j < k; ++j)
if (word[i + j] != s[j])
break;
if (j < k)
continue;
}
}
if ((pf = smp[n].sm_oneof) != NULL)
{
while (*pf != NUL && *pf != word[i + k])
++pf;
if (*pf == NUL)
continue;
++k;
}
s = smp[n].sm_rules;
pri = 5;
p0 = *s;
k0 = k;
while (*s == '-' && k > 1)
{
k--;
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
pri = *s - '0';
s++;
}
if (*s == '^' && *(s + 1) == '^')
s++;
if (*s == NUL
|| (*s == '^'
&& (i == 0 || !(word[i - 1] == ' '
|| spell_iswordp(word + i - 1, curwin)))
&& (*(s + 1) != '$'
|| (!spell_iswordp(word + i + k0, curwin))))
|| (*s == '$' && i > 0
&& spell_iswordp(word + i - 1, curwin)
&& (!spell_iswordp(word + i + k0, curwin))))
{
c0 = word[i + k - 1];
n0 = slang->sl_sal_first[c0];
if (slang->sl_followup && k > 1 && n0 >= 0
&& p0 != '-' && word[i + k] != NUL)
{
for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
{
k0 = smp[n0].sm_leadlen;
if (k0 > 1)
{
if (word[i + k] != s[1])
continue;
if (k0 > 2)
{
pf = word + i + k + 1;
for (j = 2; j < k0; ++j)
if (*pf++ != s[j])
break;
if (j < k0)
continue;
}
}
k0 += k - 1;
if ((pf = smp[n0].sm_oneof) != NULL)
{
while (*pf != NUL && *pf != word[i + k0])
++pf;
if (*pf == NUL)
continue;
++k0;
}
p0 = 5;
s = smp[n0].sm_rules;
while (*s == '-')
{
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
p0 = *s - '0';
s++;
}
if (*s == NUL
|| (*s == '$'
&& !spell_iswordp(word + i + k0,
curwin)))
{
if (k0 == k)
continue;
if (p0 < pri)
continue;
break;
}
}
if (p0 >= pri && smp[n0].sm_lead[0] == c0)
continue;
}
s = smp[n].sm_to;
if (s == NULL)
s = (char_u *)"";
pf = smp[n].sm_rules;
p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
if (p0 == 1 && z == 0)
{
if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
|| res[reslen - 1] == *s))
reslen--;
z0 = 1;
z = 1;
k0 = 0;
while (*s != NUL && word[i + k0] != NUL)
{
word[i + k0] = *s;
k0++;
s++;
}
if (k > k0)
STRMOVE(word + i + k0, word + i + k);
c = word[i];
}
else
{
i += k - 1;
z = 0;
while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
{
if (reslen == 0 || res[reslen - 1] != *s)
res[reslen++] = *s;
s++;
}
c = *s;
if (strstr((char *)pf, "^^") != NULL)
{
if (c != NUL)
res[reslen++] = c;
STRMOVE(word, word + i + 1);
i = 0;
z0 = 1;
}
}
break;
}
}
}
else if (vim_iswhite(c))
{
c = ' ';
k = 1;
}
if (z0 == 0)
{
if (k && !p0 && reslen < MAXWLEN && c != NUL
&& (!slang->sl_collapse || reslen == 0
|| res[reslen - 1] != c))
res[reslen++] = c;
i++;
z = 0;
k = 0;
}
}
res[reslen] = NUL;
}
#ifdef FEAT_MBYTE
static void
spell_soundfold_wsal(slang, inword, res)
slang_T *slang;
char_u *inword;
char_u *res;
{
salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
int word[MAXWLEN];
int wres[MAXWLEN];
int l;
char_u *s;
int *ws;
char_u *t;
int *pf;
int i, j, z;
int reslen;
int n, k = 0;
int z0;
int k0;
int n0;
int c;
int pri;
int p0 = -333;
int c0;
int did_white = FALSE;
n = 0;
for (s = inword; *s != NUL; )
{
t = s;
c = mb_cptr2char_adv(&s);
if (slang->sl_rem_accents)
{
if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
{
if (did_white)
continue;
c = ' ';
did_white = TRUE;
}
else
{
did_white = FALSE;
if (!spell_iswordp_nmw(t))
continue;
}
}
word[n++] = c;
}
word[n] = NUL;
i = reslen = z = 0;
while ((c = word[i]) != NUL)
{
n = slang->sl_sal_first[c & 0xff];
z0 = 0;
if (n >= 0)
{
for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
&& ws[0] != NUL; ++n)
{
if (c != ws[0])
continue;
k = smp[n].sm_leadlen;
if (k > 1)
{
if (word[i + 1] != ws[1])
continue;
if (k > 2)
{
for (j = 2; j < k; ++j)
if (word[i + j] != ws[j])
break;
if (j < k)
continue;
}
}
if ((pf = smp[n].sm_oneof_w) != NULL)
{
while (*pf != NUL && *pf != word[i + k])
++pf;
if (*pf == NUL)
continue;
++k;
}
s = smp[n].sm_rules;
pri = 5;
p0 = *s;
k0 = k;
while (*s == '-' && k > 1)
{
k--;
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
pri = *s - '0';
s++;
}
if (*s == '^' && *(s + 1) == '^')
s++;
if (*s == NUL
|| (*s == '^'
&& (i == 0 || !(word[i - 1] == ' '
|| spell_iswordp_w(word + i - 1, curwin)))
&& (*(s + 1) != '$'
|| (!spell_iswordp_w(word + i + k0, curwin))))
|| (*s == '$' && i > 0
&& spell_iswordp_w(word + i - 1, curwin)
&& (!spell_iswordp_w(word + i + k0, curwin))))
{
c0 = word[i + k - 1];
n0 = slang->sl_sal_first[c0 & 0xff];
if (slang->sl_followup && k > 1 && n0 >= 0
&& p0 != '-' && word[i + k] != NUL)
{
for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
== (c0 & 0xff); ++n0)
{
if (c0 != ws[0])
continue;
k0 = smp[n0].sm_leadlen;
if (k0 > 1)
{
if (word[i + k] != ws[1])
continue;
if (k0 > 2)
{
pf = word + i + k + 1;
for (j = 2; j < k0; ++j)
if (*pf++ != ws[j])
break;
if (j < k0)
continue;
}
}
k0 += k - 1;
if ((pf = smp[n0].sm_oneof_w) != NULL)
{
while (*pf != NUL && *pf != word[i + k0])
++pf;
if (*pf == NUL)
continue;
++k0;
}
p0 = 5;
s = smp[n0].sm_rules;
while (*s == '-')
{
s++;
}
if (*s == '<')
s++;
if (VIM_ISDIGIT(*s))
{
p0 = *s - '0';
s++;
}
if (*s == NUL
|| (*s == '$'
&& !spell_iswordp_w(word + i + k0,
curwin)))
{
if (k0 == k)
continue;
if (p0 < pri)
continue;
break;
}
}
if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
== (c0 & 0xff))
continue;
}
ws = smp[n].sm_to_w;
s = smp[n].sm_rules;
p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
if (p0 == 1 && z == 0)
{
if (reslen > 0 && ws != NULL && *ws != NUL
&& (wres[reslen - 1] == c
|| wres[reslen - 1] == *ws))
reslen--;
z0 = 1;
z = 1;
k0 = 0;
if (ws != NULL)
while (*ws != NUL && word[i + k0] != NUL)
{
word[i + k0] = *ws;
k0++;
ws++;
}
if (k > k0)
mch_memmove(word + i + k0, word + i + k,
sizeof(int) * (STRLEN(word + i + k) + 1));
c = word[i];
}
else
{
i += k - 1;
z = 0;
if (ws != NULL)
while (*ws != NUL && ws[1] != NUL
&& reslen < MAXWLEN)
{
if (reslen == 0 || wres[reslen - 1] != *ws)
wres[reslen++] = *ws;
ws++;
}
if (ws == NULL)
c = NUL;
else
c = *ws;
if (strstr((char *)s, "^^") != NULL)
{
if (c != NUL)
wres[reslen++] = c;
mch_memmove(word, word + i + 1,
sizeof(int) * (STRLEN(word + i + 1) + 1));
i = 0;
z0 = 1;
}
}
break;
}
}
}
else if (vim_iswhite(c))
{
c = ' ';
k = 1;
}
if (z0 == 0)
{
if (k && !p0 && reslen < MAXWLEN && c != NUL
&& (!slang->sl_collapse || reslen == 0
|| wres[reslen - 1] != c))
wres[reslen++] = c;
i++;
z = 0;
k = 0;
}
}
l = 0;
for (n = 0; n < reslen; ++n)
{
l += mb_char2bytes(wres[n], res + l);
if (l + MB_MAXBYTES > MAXWLEN)
break;
}
res[l] = NUL;
}
#endif
static int
soundalike_score(goodstart, badstart)
char_u *goodstart;
char_u *badstart;
{
char_u *goodsound = goodstart;
char_u *badsound = badstart;
int goodlen;
int badlen;
int n;
char_u *pl, *ps;
char_u *pl2, *ps2;
int score = 0;
if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
{
if ((badsound[0] == NUL && goodsound[1] == NUL)
|| (goodsound[0] == NUL && badsound[1] == NUL))
return SCORE_DEL;
if (badsound[0] == NUL || goodsound[0] == NUL)
return SCORE_MAXMAX;
if (badsound[1] == goodsound[1]
|| (badsound[1] != NUL
&& goodsound[1] != NUL
&& badsound[2] == goodsound[2]))
{
}
else
{
score = 2 * SCORE_DEL / 3;
if (*badsound == '*')
++badsound;
else
++goodsound;
}
}
goodlen = (int)STRLEN(goodsound);
badlen = (int)STRLEN(badsound);
n = goodlen - badlen;
if (n < -2 || n > 2)
return SCORE_MAXMAX;
if (n > 0)
{
pl = goodsound;
ps = badsound;
}
else
{
pl = badsound;
ps = goodsound;
}
while (*pl == *ps && *pl != NUL)
{
++pl;
++ps;
}
switch (n)
{
case -2:
case 2:
++pl;
while (*pl == *ps)
{
++pl;
++ps;
}
if (STRCMP(pl + 1, ps) == 0)
return score + SCORE_DEL * 2;
break;
case -1:
case 1:
pl2 = pl + 1;
ps2 = ps;
while (*pl2 == *ps2)
{
if (*pl2 == NUL)
return score + SCORE_DEL;
++pl2;
++ps2;
}
if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
&& STRCMP(pl2 + 2, ps2 + 2) == 0)
return score + SCORE_DEL + SCORE_SWAP;
if (STRCMP(pl2 + 1, ps2 + 1) == 0)
return score + SCORE_DEL + SCORE_SUBST;
if (pl[0] == ps[1] && pl[1] == ps[0])
{
pl2 = pl + 2;
ps2 = ps + 2;
while (*pl2 == *ps2)
{
++pl2;
++ps2;
}
if (STRCMP(pl2 + 1, ps2) == 0)
return score + SCORE_SWAP + SCORE_DEL;
}
pl2 = pl + 1;
ps2 = ps + 1;
while (*pl2 == *ps2)
{
++pl2;
++ps2;
}
if (STRCMP(pl2 + 1, ps2) == 0)
return score + SCORE_SUBST + SCORE_DEL;
break;
case 0:
if (*pl == NUL)
return score;
if (pl[0] == ps[1] && pl[1] == ps[0])
{
pl2 = pl + 2;
ps2 = ps + 2;
while (*pl2 == *ps2)
{
if (*pl2 == NUL)
return score + SCORE_SWAP;
++pl2;
++ps2;
}
if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
&& STRCMP(pl2 + 2, ps2 + 2) == 0)
return score + SCORE_SWAP + SCORE_SWAP;
if (STRCMP(pl2 + 1, ps2 + 1) == 0)
return score + SCORE_SWAP + SCORE_SUBST;
}
pl2 = pl + 1;
ps2 = ps + 1;
while (*pl2 == *ps2)
{
if (*pl2 == NUL)
return score + SCORE_SUBST;
++pl2;
++ps2;
}
if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
&& STRCMP(pl2 + 2, ps2 + 2) == 0)
return score + SCORE_SUBST + SCORE_SWAP;
if (STRCMP(pl2 + 1, ps2 + 1) == 0)
return score + SCORE_SUBST + SCORE_SUBST;
pl2 = pl;
ps2 = ps + 1;
while (*pl2 == *ps2)
{
++pl2;
++ps2;
}
if (STRCMP(pl2 + 1, ps2) == 0)
return score + SCORE_INS + SCORE_DEL;
pl2 = pl + 1;
ps2 = ps;
while (*pl2 == *ps2)
{
++pl2;
++ps2;
}
if (STRCMP(pl2, ps2 + 1) == 0)
return score + SCORE_INS + SCORE_DEL;
break;
}
return SCORE_MAXMAX;
}
static int
spell_edit_score(slang, badword, goodword)
slang_T *slang;
char_u *badword;
char_u *goodword;
{
int *cnt;
int badlen, goodlen;
int j, i;
int t;
int bc, gc;
int pbc, pgc;
#ifdef FEAT_MBYTE
char_u *p;
int wbadword[MAXWLEN];
int wgoodword[MAXWLEN];
if (has_mbyte)
{
for (p = badword, badlen = 0; *p != NUL; )
wbadword[badlen++] = mb_cptr2char_adv(&p);
wbadword[badlen++] = 0;
for (p = goodword, goodlen = 0; *p != NUL; )
wgoodword[goodlen++] = mb_cptr2char_adv(&p);
wgoodword[goodlen++] = 0;
}
else
#endif
{
badlen = (int)STRLEN(badword) + 1;
goodlen = (int)STRLEN(goodword) + 1;
}
#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
TRUE);
if (cnt == NULL)
return 0;
CNT(0, 0) = 0;
for (j = 1; j <= goodlen; ++j)
CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
for (i = 1; i <= badlen; ++i)
{
CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
for (j = 1; j <= goodlen; ++j)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
bc = wbadword[i - 1];
gc = wgoodword[j - 1];
}
else
#endif
{
bc = badword[i - 1];
gc = goodword[j - 1];
}
if (bc == gc)
CNT(i, j) = CNT(i - 1, j - 1);
else
{
if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
else
{
if (slang != NULL
&& slang->sl_has_map
&& similar_chars(slang, gc, bc))
CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
else
CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
}
if (i > 1 && j > 1)
{
#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;
}
}
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;
}
}
}
i = CNT(badlen - 1, goodlen - 1);
vim_free(cnt);
return i;
}
typedef struct
{
int badi;
int goodi;
int score;
} limitscore_T;
static int
spell_edit_score_limit(slang, badword, goodword, limit)
slang_T *slang;
char_u *badword;
char_u *goodword;
int limit;
{
limitscore_T stack[10];
int stackidx;
int bi, gi;
int bi2, gi2;
int bc, gc;
int score;
int score_off;
int minscore;
int round;
#ifdef FEAT_MBYTE
if (has_mbyte)
return spell_edit_score_limit_w(slang, badword, goodword, limit);
#endif
stackidx = 0;
bi = 0;
gi = 0;
score = 0;
minscore = limit + 1;
for (;;)
{
for (;;)
{
bc = badword[bi];
gc = goodword[gi];
if (bc != gc)
break;
if (bc == NUL)
{
if (score < minscore)
minscore = score;
goto pop;
}
++bi;
++gi;
}
if (gc == NUL)
{
do
{
if ((score += SCORE_DEL) >= minscore)
goto pop;
} while (badword[++bi] != NUL);
minscore = score;
}
else if (bc == NUL)
{
do
{
if ((score += SCORE_INS) >= minscore)
goto pop;
} while (goodword[++gi] != NUL);
minscore = score;
}
else
{
for (round = 0; round <= 1; ++round)
{
score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
if (score_off < minscore)
{
if (score_off + SCORE_EDIT_MIN >= minscore)
{
bi2 = bi + 1 - round;
gi2 = gi + round;
while (goodword[gi2] == badword[bi2])
{
if (goodword[gi2] == NUL)
{
minscore = score_off;
break;
}
++bi2;
++gi2;
}
}
else
{
stack[stackidx].badi = bi + 1 - round;
stack[stackidx].goodi = gi + round;
stack[stackidx].score = score_off;
++stackidx;
}
}
}
if (score + SCORE_SWAP < minscore)
{
if (gc == badword[bi + 1] && bc == goodword[gi + 1])
{
gi += 2;
bi += 2;
score += SCORE_SWAP;
continue;
}
}
if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
score += SCORE_ICASE;
else
{
if (slang != NULL
&& slang->sl_has_map
&& similar_chars(slang, gc, bc))
score += SCORE_SIMILAR;
else
score += SCORE_SUBST;
}
if (score < minscore)
{
++gi;
++bi;
continue;
}
}
pop:
if (stackidx == 0)
break;
--stackidx;
gi = stack[stackidx].goodi;
bi = stack[stackidx].badi;
score = stack[stackidx].score;
}
if (minscore > limit)
return SCORE_MAXMAX;
return minscore;
}
#ifdef FEAT_MBYTE
static int
spell_edit_score_limit_w(slang, badword, goodword, limit)
slang_T *slang;
char_u *badword;
char_u *goodword;
int limit;
{
limitscore_T stack[10];
int stackidx;
int bi, gi;
int bi2, gi2;
int bc, gc;
int score;
int score_off;
int minscore;
int round;
char_u *p;
int wbadword[MAXWLEN];
int wgoodword[MAXWLEN];
bi = 0;
for (p = badword; *p != NUL; )
wbadword[bi++] = mb_cptr2char_adv(&p);
wbadword[bi++] = 0;
gi = 0;
for (p = goodword; *p != NUL; )
wgoodword[gi++] = mb_cptr2char_adv(&p);
wgoodword[gi++] = 0;
stackidx = 0;
bi = 0;
gi = 0;
score = 0;
minscore = limit + 1;
for (;;)
{
for (;;)
{
bc = wbadword[bi];
gc = wgoodword[gi];
if (bc != gc)
break;
if (bc == NUL)
{
if (score < minscore)
minscore = score;
goto pop;
}
++bi;
++gi;
}
if (gc == NUL)
{
do
{
if ((score += SCORE_DEL) >= minscore)
goto pop;
} while (wbadword[++bi] != NUL);
minscore = score;
}
else if (bc == NUL)
{
do
{
if ((score += SCORE_INS) >= minscore)
goto pop;
} while (wgoodword[++gi] != NUL);
minscore = score;
}
else
{
for (round = 0; round <= 1; ++round)
{
score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
if (score_off < minscore)
{
if (score_off + SCORE_EDIT_MIN >= minscore)
{
bi2 = bi + 1 - round;
gi2 = gi + round;
while (wgoodword[gi2] == wbadword[bi2])
{
if (wgoodword[gi2] == NUL)
{
minscore = score_off;
break;
}
++bi2;
++gi2;
}
}
else
{
stack[stackidx].badi = bi + 1 - round;
stack[stackidx].goodi = gi + round;
stack[stackidx].score = score_off;
++stackidx;
}
}
}
if (score + SCORE_SWAP < minscore)
{
if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
{
gi += 2;
bi += 2;
score += SCORE_SWAP;
continue;
}
}
if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
score += SCORE_ICASE;
else
{
if (slang != NULL
&& slang->sl_has_map
&& similar_chars(slang, gc, bc))
score += SCORE_SIMILAR;
else
score += SCORE_SUBST;
}
if (score < minscore)
{
++gi;
++bi;
continue;
}
}
pop:
if (stackidx == 0)
break;
--stackidx;
gi = stack[stackidx].goodi;
bi = stack[stackidx].badi;
score = stack[stackidx].score;
}
if (minscore > limit)
return SCORE_MAXMAX;
return minscore;
}
#endif
void
ex_spellinfo(eap)
exarg_T *eap UNUSED;
{
int lpi;
langp_T *lp;
char_u *p;
if (no_spell_checking(curwin))
return;
msg_start();
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
msg_puts((char_u *)"file: ");
msg_puts(lp->lp_slang->sl_fname);
msg_putchar('\n');
p = lp->lp_slang->sl_info;
if (p != NULL)
{
msg_puts(p);
msg_putchar('\n');
}
}
msg_end();
}
#define DUMPFLAG_KEEPCASE 1
#define DUMPFLAG_COUNT 2
#define DUMPFLAG_ICASE 4
#define DUMPFLAG_ONECAP 8
#define DUMPFLAG_ALLCAP 16
void
ex_spelldump(eap)
exarg_T *eap;
{
if (no_spell_checking(curwin))
return;
do_cmdline_cmd((char_u *)"new");
if (!bufempty() || !buf_valid(curbuf))
return;
spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
if (curbuf->b_ml.ml_line_count > 1)
ml_delete(curbuf->b_ml.ml_line_count, FALSE);
redraw_later(NOT_VALID);
}
void
spell_dump_compl(pat, ic, dir, dumpflags_arg)
char_u *pat;
int ic;
int *dir;
int dumpflags_arg;
{
langp_T *lp;
slang_T *slang;
idx_T arridx[MAXWLEN];
int curi[MAXWLEN];
char_u word[MAXWLEN];
int c;
char_u *byts;
idx_T *idxs;
linenr_T lnum = 0;
int round;
int depth;
int n;
int flags;
char_u *region_names = NULL;
int do_region = TRUE;
char_u *p;
int lpi;
int dumpflags = dumpflags_arg;
int patlen;
if (pat != NULL)
{
if (ic)
dumpflags |= DUMPFLAG_ICASE;
else
{
n = captype(pat, NULL);
if (n == WF_ONECAP)
dumpflags |= DUMPFLAG_ONECAP;
else if (n == WF_ALLCAP
#ifdef FEAT_MBYTE
&& (int)STRLEN(pat) > mb_ptr2len(pat)
#else
&& (int)STRLEN(pat) > 1
#endif
)
dumpflags |= DUMPFLAG_ALLCAP;
}
}
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
p = lp->lp_slang->sl_regions;
if (p[0] != 0)
{
if (region_names == NULL)
region_names = p;
else if (STRCMP(region_names, p) != 0)
{
do_region = FALSE;
break;
}
}
}
if (do_region && region_names != NULL)
{
if (pat == NULL)
{
vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
}
}
else
do_region = FALSE;
for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
{
lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
slang = lp->lp_slang;
if (slang->sl_fbyts == NULL)
continue;
if (pat == NULL)
{
vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
}
if (pat != NULL && slang->sl_pbyts == NULL)
patlen = (int)STRLEN(pat);
else
patlen = -1;
for (round = 1; round <= 2; ++round)
{
if (round == 1)
{
dumpflags &= ~DUMPFLAG_KEEPCASE;
byts = slang->sl_fbyts;
idxs = slang->sl_fidxs;
}
else
{
dumpflags |= DUMPFLAG_KEEPCASE;
byts = slang->sl_kbyts;
idxs = slang->sl_kidxs;
}
if (byts == NULL)
continue;
depth = 0;
arridx[0] = 0;
curi[0] = 1;
while (depth >= 0 && !got_int
&& (pat == NULL || !compl_interrupted))
{
if (curi[depth] > byts[arridx[depth]])
{
--depth;
line_breakcheck();
ins_compl_check_keys(50);
}
else
{
n = arridx[depth] + curi[depth];
++curi[depth];
c = byts[n];
if (c == 0)
{
flags = (int)idxs[n];
if ((round == 2 || (flags & WF_KEEPCAP) == 0)
&& (flags & WF_NEEDCOMP) == 0
&& (do_region
|| (flags & WF_REGION) == 0
|| (((unsigned)flags >> 16)
& lp->lp_region) != 0))
{
word[depth] = NUL;
if (!do_region)
flags &= ~WF_REGION;
c = (unsigned)flags >> 24;
if (c == 0 || curi[depth] == 2)
{
dump_word(slang, word, pat, dir,
dumpflags, flags, lnum);
if (pat == NULL)
++lnum;
}
if (c != 0)
lnum = dump_prefixes(slang, word, pat, dir,
dumpflags, flags, lnum);
}
}
else
{
word[depth++] = c;
arridx[depth] = idxs[n];
curi[depth] = 1;
if (depth <= patlen
&& MB_STRNICMP(word, pat, depth) != 0)
--depth;
}
}
}
}
}
}
static void
dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
slang_T *slang;
char_u *word;
char_u *pat;
int *dir;
int dumpflags;
int wordflags;
linenr_T lnum;
{
int keepcap = FALSE;
char_u *p;
char_u *tw;
char_u cword[MAXWLEN];
char_u badword[MAXWLEN + 10];
int i;
int flags = wordflags;
if (dumpflags & DUMPFLAG_ONECAP)
flags |= WF_ONECAP;
if (dumpflags & DUMPFLAG_ALLCAP)
flags |= WF_ALLCAP;
if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
{
make_case_word(word, cword, flags);
p = cword;
}
else
{
p = word;
if ((dumpflags & DUMPFLAG_KEEPCASE)
&& ((captype(word, NULL) & WF_KEEPCAP) == 0
|| (flags & WF_FIXCAP) != 0))
keepcap = TRUE;
}
tw = p;
if (pat == NULL)
{
if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
{
STRCPY(badword, p);
STRCAT(badword, "/");
if (keepcap)
STRCAT(badword, "=");
if (flags & WF_BANNED)
STRCAT(badword, "!");
else if (flags & WF_RARE)
STRCAT(badword, "?");
if (flags & WF_REGION)
for (i = 0; i < 7; ++i)
if (flags & (0x10000 << i))
sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
p = badword;
}
if (dumpflags & DUMPFLAG_COUNT)
{
hashitem_T *hi;
hi = hash_find(&slang->sl_wordcount, tw);
if (!HASHITEM_EMPTY(hi))
{
vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
tw, HI2WC(hi)->wc_count);
p = IObuff;
}
}
ml_append(lnum, p, (colnr_T)0, FALSE);
}
else if (((dumpflags & DUMPFLAG_ICASE)
? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
: STRNCMP(p, pat, STRLEN(pat)) == 0)
&& ins_compl_add_infercase(p, (int)STRLEN(p),
p_ic, NULL, *dir, 0) == OK)
*dir = FORWARD;
}
static linenr_T
dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
slang_T *slang;
char_u *word;
char_u *pat;
int *dir;
int dumpflags;
int flags;
linenr_T startlnum;
{
idx_T arridx[MAXWLEN];
int curi[MAXWLEN];
char_u prefix[MAXWLEN];
char_u word_up[MAXWLEN];
int has_word_up = FALSE;
int c;
char_u *byts;
idx_T *idxs;
linenr_T lnum = startlnum;
int depth;
int n;
int len;
int i;
c = PTR2CHAR(word);
if (SPELL_TOUPPER(c) != c)
{
onecap_copy(word, word_up, TRUE);
has_word_up = TRUE;
}
byts = slang->sl_pbyts;
idxs = slang->sl_pidxs;
if (byts != NULL)
{
depth = 0;
arridx[0] = 0;
curi[0] = 1;
while (depth >= 0 && !got_int)
{
n = arridx[depth];
len = byts[n];
if (curi[depth] > len)
{
--depth;
line_breakcheck();
}
else
{
n += curi[depth];
++curi[depth];
c = byts[n];
if (c == 0)
{
for (i = 1; i < len; ++i)
if (byts[n + i] != 0)
break;
curi[depth] += i - 1;
c = valid_word_prefix(i, n, flags, word, slang, FALSE);
if (c != 0)
{
vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
dump_word(slang, prefix, pat, dir, dumpflags,
(c & WF_RAREPFX) ? (flags | WF_RARE)
: flags, lnum);
if (lnum != 0)
++lnum;
}
if (has_word_up)
{
c = valid_word_prefix(i, n, flags, word_up, slang,
TRUE);
if (c != 0)
{
vim_strncpy(prefix + depth, word_up,
MAXWLEN - depth - 1);
dump_word(slang, prefix, pat, dir, dumpflags,
(c & WF_RAREPFX) ? (flags | WF_RARE)
: flags, lnum);
if (lnum != 0)
++lnum;
}
}
}
else
{
prefix[depth++] = c;
arridx[depth] = idxs[n];
curi[depth] = 1;
}
}
}
}
return lnum;
}
char_u *
spell_to_word_end(start, win)
char_u *start;
win_T *win;
{
char_u *p = start;
while (*p != NUL && spell_iswordp(p, win))
mb_ptr_adv(p);
return p;
}
#if defined(FEAT_INS_EXPAND) || defined(PROTO)
int
spell_word_start(startcol)
int startcol;
{
char_u *line;
char_u *p;
int col = 0;
if (no_spell_checking(curwin))
return startcol;
line = ml_get_curline();
for (p = line + startcol; p > line; )
{
mb_ptr_back(line, p);
if (spell_iswordp_nmw(p))
break;
}
while (p > line)
{
col = (int)(p - line);
mb_ptr_back(line, p);
if (!spell_iswordp(p, curwin))
break;
col = 0;
}
return col;
}
static int spell_expand_need_cap;
void
spell_expand_check_cap(col)
colnr_T col;
{
spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
}
int
expand_spelling(lnum, pat, matchp)
linenr_T lnum UNUSED;
char_u *pat;
char_u ***matchp;
{
garray_T ga;
spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
*matchp = ga.ga_data;
return ga.ga_len;
}
#endif
#endif