2016-08-29 22:49:24 +02:00
|
|
|
|
/* vi:set ts=8 sts=4 sw=4 noet:
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*
|
|
|
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
|
|
|
*
|
|
|
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
|
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
|
|
|
* See README.txt for an overview of the Vim source code.
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* search.c: code for normal mode searching commands
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vim.h"
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_EVAL
|
2016-01-29 22:47:03 +01:00
|
|
|
|
static void set_vv_searchforward(void);
|
|
|
|
|
static int first_submatch(regmmatch_T *rp);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#endif
|
2016-01-29 22:47:03 +01:00
|
|
|
|
static int check_linecomment(char_u *line);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#ifdef FEAT_FIND_ID
|
2016-01-29 22:47:03 +01:00
|
|
|
|
static void show_pat_in_path(char_u *, int,
|
|
|
|
|
int, int, FILE *, linenr_T *, long);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#endif
|
2020-06-01 17:28:35 +02:00
|
|
|
|
|
|
|
|
|
typedef struct searchstat
|
|
|
|
|
{
|
|
|
|
|
int cur; // current position of found words
|
|
|
|
|
int cnt; // total count of found words
|
|
|
|
|
int exact_match; // TRUE if matched exactly on specified position
|
|
|
|
|
int incomplete; // 0: search was fully completed
|
|
|
|
|
// 1: recomputing was timed out
|
|
|
|
|
// 2: max count exceeded
|
|
|
|
|
int last_maxcount; // the max count of the last search
|
|
|
|
|
} searchstat_T;
|
|
|
|
|
|
|
|
|
|
static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, int recompute, int maxcount, long timeout);
|
|
|
|
|
static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout);
|
|
|
|
|
|
2020-06-01 21:32:45 +02:00
|
|
|
|
#define SEARCH_STAT_DEF_TIMEOUT 40L
|
2020-06-01 17:28:35 +02:00
|
|
|
|
#define SEARCH_STAT_DEF_MAX_COUNT 99
|
|
|
|
|
#define SEARCH_STAT_BUF_LEN 12
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file contains various searching-related routines. These fall into
|
|
|
|
|
* three groups:
|
|
|
|
|
* 1. string searches (for /, ?, n, and N)
|
|
|
|
|
* 2. character searches within a single line (for f, F, t, T, etc)
|
|
|
|
|
* 3. "other" kinds of searches like the '%' command, and 'word' searches.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* String searches
|
|
|
|
|
*
|
|
|
|
|
* The string search functions are divided into two levels:
|
|
|
|
|
* lowest: searchit(); uses an pos_T for starting position and found match.
|
|
|
|
|
* Highest: do_search(); uses curwin->w_cursor; calls searchit().
|
|
|
|
|
*
|
|
|
|
|
* The last search pattern is remembered for repeating the same search.
|
|
|
|
|
* This pattern is shared between the :g, :s, ? and / commands.
|
|
|
|
|
* This is in search_regcomp().
|
|
|
|
|
*
|
|
|
|
|
* The actual string matching is done using a heavily modified version of
|
|
|
|
|
* Henry Spencer's regular expression library. See regexp.c.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Two search patterns are remembered: One for the :substitute command and
|
|
|
|
|
* one for other searches. last_idx points to the one that was used the last
|
|
|
|
|
* time.
|
|
|
|
|
*/
|
2019-07-23 22:15:25 +02:00
|
|
|
|
static spat_T spats[2] =
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
{NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat
|
|
|
|
|
{NULL, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat
|
2004-06-13 20:20:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
static int last_idx = 0; // index in spats[] for RE_LAST
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
static char_u lastc[2] = {NUL, NUL}; // last character searched for
|
|
|
|
|
static int lastcdir = FORWARD; // last direction of character search
|
|
|
|
|
static int last_t_cmd = TRUE; // last search t_cmd
|
2015-08-11 14:26:19 +02:00
|
|
|
|
static char_u lastc_bytes[MB_MAXBYTES + 1];
|
2019-12-05 21:10:38 +01:00
|
|
|
|
static int lastc_bytelen = 1; // >1 for multi-byte char
|
2015-08-11 14:26:19 +02:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// copy of spats[], for keeping the search patterns while executing autocmds
|
2019-07-23 22:15:25 +02:00
|
|
|
|
static spat_T saved_spats[2];
|
2004-06-13 20:20:40 +00:00
|
|
|
|
# ifdef FEAT_SEARCH_EXTRA
|
2018-12-01 21:08:21 +01:00
|
|
|
|
static int saved_spats_last_idx = 0;
|
|
|
|
|
static int saved_spats_no_hlsearch = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
# endif
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
static char_u *mr_pattern = NULL; // pattern used by search_regcomp()
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
2019-12-05 21:10:38 +01:00
|
|
|
|
static int mr_pattern_alloced = FALSE; // mr_pattern was allocated
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_FIND_ID
|
|
|
|
|
/*
|
|
|
|
|
* Type used by find_pattern_in_path() to remember which included files have
|
|
|
|
|
* been searched already.
|
|
|
|
|
*/
|
|
|
|
|
typedef struct SearchedFile
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
FILE *fp; // File pointer
|
|
|
|
|
char_u *name; // Full name of file
|
|
|
|
|
linenr_T lnum; // Line we were up to in file
|
|
|
|
|
int matched; // Found a match in this file
|
2004-06-13 20:20:40 +00:00
|
|
|
|
} SearchedFile;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* translate search pattern for vim_regcomp()
|
|
|
|
|
*
|
|
|
|
|
* pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
|
|
|
|
|
* pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
|
|
|
|
|
* pat_save == RE_BOTH: save pat in both patterns (:global command)
|
|
|
|
|
* pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
|
2007-05-10 18:59:07 +00:00
|
|
|
|
* pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
|
2004-06-13 20:20:40 +00:00
|
|
|
|
* pat_use == RE_LAST: use last used pattern if "pat" is NULL
|
|
|
|
|
* options & SEARCH_HIS: put search string in history
|
|
|
|
|
* options & SEARCH_KEEP: keep previous search pattern
|
|
|
|
|
*
|
|
|
|
|
* returns FAIL if failed, OK otherwise.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
search_regcomp(
|
|
|
|
|
char_u *pat,
|
|
|
|
|
int pat_save,
|
|
|
|
|
int pat_use,
|
|
|
|
|
int options,
|
2019-12-05 21:10:38 +01:00
|
|
|
|
regmmatch_T *regmatch) // return: pattern and ignore-case flag
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
int magic;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
rc_did_emsg = FALSE;
|
2020-12-21 19:59:08 +01:00
|
|
|
|
magic = magic_isset();
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If no pattern given, use a previously defined pattern.
|
|
|
|
|
*/
|
|
|
|
|
if (pat == NULL || *pat == NUL)
|
|
|
|
|
{
|
|
|
|
|
if (pat_use == RE_LAST)
|
|
|
|
|
i = last_idx;
|
|
|
|
|
else
|
|
|
|
|
i = pat_use;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (spats[i].pat == NULL) // pattern was never defined
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (pat_use == RE_SUBST)
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_(e_nopresub));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_(e_noprevre));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
rc_did_emsg = TRUE;
|
|
|
|
|
return FAIL;
|
|
|
|
|
}
|
|
|
|
|
pat = spats[i].pat;
|
|
|
|
|
magic = spats[i].magic;
|
|
|
|
|
no_smartcase = spats[i].no_scs;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else if (options & SEARCH_HIS) // put new pattern in history
|
2004-06-13 20:20:40 +00:00
|
|
|
|
add_to_history(HIST_SEARCH, pat, TRUE, NUL);
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
|
|
|
|
if (mr_pattern_alloced)
|
|
|
|
|
{
|
|
|
|
|
vim_free(mr_pattern);
|
|
|
|
|
mr_pattern_alloced = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
|
|
|
|
|
{
|
|
|
|
|
char_u *rev_pattern;
|
|
|
|
|
|
|
|
|
|
rev_pattern = reverse_text(pat);
|
|
|
|
|
if (rev_pattern == NULL)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
mr_pattern = pat; // out of memory, keep normal pattern.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mr_pattern = rev_pattern;
|
|
|
|
|
mr_pattern_alloced = TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
mr_pattern = pat;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Save the currently used pattern in the appropriate place,
|
|
|
|
|
* unless the pattern should not be remembered.
|
|
|
|
|
*/
|
2020-10-24 20:49:43 +02:00
|
|
|
|
if (!(options & SEARCH_KEEP)
|
|
|
|
|
&& (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// search or global command
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
|
|
|
|
|
save_re_pat(RE_SEARCH, pat, magic);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// substitute or global command
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (pat_save == RE_SUBST || pat_save == RE_BOTH)
|
|
|
|
|
save_re_pat(RE_SUBST, pat, magic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
regmatch->rmm_ic = ignorecase(pat);
|
2005-07-11 22:40:32 +00:00
|
|
|
|
regmatch->rmm_maxcol = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
|
|
|
|
|
if (regmatch->regprog == NULL)
|
|
|
|
|
return FAIL;
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get search pattern used by search_regcomp().
|
|
|
|
|
*/
|
|
|
|
|
char_u *
|
2016-01-30 21:10:09 +01:00
|
|
|
|
get_search_pat(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
return mr_pattern;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-08 20:49:37 +00:00
|
|
|
|
#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Reverse text into allocated memory.
|
|
|
|
|
* Returns the allocated string, NULL when out of memory.
|
|
|
|
|
*/
|
2007-08-08 20:49:37 +00:00
|
|
|
|
char_u *
|
2016-01-30 21:10:09 +01:00
|
|
|
|
reverse_text(char_u *s)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned len;
|
|
|
|
|
unsigned s_i, rev_i;
|
|
|
|
|
char_u *rev;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Reverse the pattern.
|
|
|
|
|
*/
|
|
|
|
|
len = (unsigned)STRLEN(s);
|
|
|
|
|
rev = alloc(len + 1);
|
|
|
|
|
if (rev != NULL)
|
|
|
|
|
{
|
|
|
|
|
rev_i = len;
|
|
|
|
|
for (s_i = 0; s_i < len; ++s_i)
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
{
|
|
|
|
|
int mb_len;
|
|
|
|
|
|
2005-08-10 21:07:57 +00:00
|
|
|
|
mb_len = (*mb_ptr2len)(s + s_i);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
rev_i -= mb_len;
|
|
|
|
|
mch_memmove(rev + rev_i, s + s_i, mb_len);
|
|
|
|
|
s_i += mb_len - 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
rev[--rev_i] = s[s_i];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
rev[len] = NUL;
|
|
|
|
|
}
|
|
|
|
|
return rev;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-12-13 03:17:11 +01:00
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
save_re_pat(int idx, char_u *pat, int magic)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (spats[idx].pat != pat)
|
|
|
|
|
{
|
|
|
|
|
vim_free(spats[idx].pat);
|
|
|
|
|
spats[idx].pat = vim_strsave(pat);
|
|
|
|
|
spats[idx].magic = magic;
|
|
|
|
|
spats[idx].no_scs = no_smartcase;
|
|
|
|
|
last_idx = idx;
|
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// If 'hlsearch' set and search pat changed: need redraw.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (p_hls)
|
2006-03-12 22:10:07 +00:00
|
|
|
|
redraw_all_later(SOME_VALID);
|
2018-04-27 22:53:07 +02:00
|
|
|
|
set_no_hlsearch(FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Save the search patterns, so they can be restored later.
|
|
|
|
|
* Used before/after executing autocommands and user functions.
|
|
|
|
|
*/
|
|
|
|
|
static int save_level = 0;
|
|
|
|
|
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
save_search_patterns(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (save_level++ == 0)
|
|
|
|
|
{
|
|
|
|
|
saved_spats[0] = spats[0];
|
|
|
|
|
if (spats[0].pat != NULL)
|
|
|
|
|
saved_spats[0].pat = vim_strsave(spats[0].pat);
|
|
|
|
|
saved_spats[1] = spats[1];
|
|
|
|
|
if (spats[1].pat != NULL)
|
|
|
|
|
saved_spats[1].pat = vim_strsave(spats[1].pat);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
2018-12-01 21:08:21 +01:00
|
|
|
|
saved_spats_last_idx = last_idx;
|
|
|
|
|
saved_spats_no_hlsearch = no_hlsearch;
|
2018-03-04 18:08:14 +01:00
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
restore_search_patterns(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (--save_level == 0)
|
|
|
|
|
{
|
|
|
|
|
vim_free(spats[0].pat);
|
|
|
|
|
spats[0] = saved_spats[0];
|
2018-03-04 18:08:14 +01:00
|
|
|
|
#if defined(FEAT_EVAL)
|
2008-06-24 22:58:06 +00:00
|
|
|
|
set_vv_searchforward();
|
2018-03-04 18:08:14 +01:00
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
vim_free(spats[1].pat);
|
|
|
|
|
spats[1] = saved_spats[1];
|
2018-03-04 18:08:14 +01:00
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
2018-12-01 21:08:21 +01:00
|
|
|
|
last_idx = saved_spats_last_idx;
|
|
|
|
|
set_no_hlsearch(saved_spats_no_hlsearch);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-25 23:04:51 +00:00
|
|
|
|
#if defined(EXITFREE) || defined(PROTO)
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
free_search_patterns(void)
|
2005-06-25 23:04:51 +00:00
|
|
|
|
{
|
|
|
|
|
vim_free(spats[0].pat);
|
|
|
|
|
vim_free(spats[1].pat);
|
2009-04-22 16:45:21 +00:00
|
|
|
|
|
|
|
|
|
# ifdef FEAT_RIGHTLEFT
|
|
|
|
|
if (mr_pattern_alloced)
|
|
|
|
|
{
|
2010-07-14 16:52:17 +02:00
|
|
|
|
vim_free(mr_pattern);
|
|
|
|
|
mr_pattern_alloced = FALSE;
|
|
|
|
|
mr_pattern = NULL;
|
2009-04-22 16:45:21 +00:00
|
|
|
|
}
|
|
|
|
|
# endif
|
2005-06-25 23:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-10-29 16:40:30 +01:00
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
2018-12-01 21:08:21 +01:00
|
|
|
|
// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
|
|
|
|
|
// searching
|
2019-07-23 22:15:25 +02:00
|
|
|
|
static spat_T saved_last_search_spat;
|
2018-12-01 21:08:21 +01:00
|
|
|
|
static int did_save_last_search_spat = 0;
|
|
|
|
|
static int saved_last_idx = 0;
|
|
|
|
|
static int saved_no_hlsearch = 0;
|
|
|
|
|
|
2017-10-29 16:40:30 +01:00
|
|
|
|
/*
|
|
|
|
|
* Save and restore the search pattern for incremental highlight search
|
|
|
|
|
* feature.
|
|
|
|
|
*
|
2018-11-16 16:21:05 +01:00
|
|
|
|
* It's similar to but different from save_search_patterns() and
|
2017-10-29 16:40:30 +01:00
|
|
|
|
* restore_search_patterns(), because the search pattern must be restored when
|
2018-11-16 16:21:05 +01:00
|
|
|
|
* canceling incremental searching even if it's called inside user functions.
|
2017-10-29 16:40:30 +01:00
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
save_last_search_pattern(void)
|
|
|
|
|
{
|
2020-06-04 20:56:09 +02:00
|
|
|
|
if (++did_save_last_search_spat != 1)
|
|
|
|
|
// nested call, nothing to do
|
|
|
|
|
return;
|
2018-11-30 21:57:55 +01:00
|
|
|
|
|
2017-10-29 16:40:30 +01:00
|
|
|
|
saved_last_search_spat = spats[RE_SEARCH];
|
|
|
|
|
if (spats[RE_SEARCH].pat != NULL)
|
|
|
|
|
saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
|
|
|
|
|
saved_last_idx = last_idx;
|
|
|
|
|
saved_no_hlsearch = no_hlsearch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
restore_last_search_pattern(void)
|
|
|
|
|
{
|
2020-06-04 20:56:09 +02:00
|
|
|
|
if (--did_save_last_search_spat > 0)
|
|
|
|
|
// nested call, nothing to do
|
|
|
|
|
return;
|
|
|
|
|
if (did_save_last_search_spat != 0)
|
2018-11-30 21:57:55 +01:00
|
|
|
|
{
|
2020-06-04 20:56:09 +02:00
|
|
|
|
iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
|
2018-11-30 21:57:55 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 16:40:30 +01:00
|
|
|
|
vim_free(spats[RE_SEARCH].pat);
|
|
|
|
|
spats[RE_SEARCH] = saved_last_search_spat;
|
2018-11-30 21:57:55 +01:00
|
|
|
|
saved_last_search_spat.pat = NULL;
|
2017-10-29 16:40:30 +01:00
|
|
|
|
# if defined(FEAT_EVAL)
|
|
|
|
|
set_vv_searchforward();
|
|
|
|
|
# endif
|
|
|
|
|
last_idx = saved_last_idx;
|
2018-04-27 22:53:07 +02:00
|
|
|
|
set_no_hlsearch(saved_no_hlsearch);
|
2017-10-29 16:40:30 +01:00
|
|
|
|
}
|
2017-11-16 22:20:39 +01:00
|
|
|
|
|
|
|
|
|
char_u *
|
|
|
|
|
last_search_pattern(void)
|
|
|
|
|
{
|
|
|
|
|
return spats[RE_SEARCH].pat;
|
|
|
|
|
}
|
2017-10-29 16:40:30 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Return TRUE when case should be ignored for search pattern "pat".
|
|
|
|
|
* Uses the 'ignorecase' and 'smartcase' options.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
ignorecase(char_u *pat)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2016-08-20 16:57:02 +02:00
|
|
|
|
return ignorecase_opt(pat, p_ic, p_scs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* As ignorecase() put pass the "ic" and "scs" flags.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
ignorecase_opt(char_u *pat, int ic_in, int scs)
|
|
|
|
|
{
|
|
|
|
|
int ic = ic_in;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2016-08-20 16:57:02 +02:00
|
|
|
|
if (ic && !no_smartcase && scs
|
2019-08-21 14:37:09 +02:00
|
|
|
|
&& !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
|
2010-07-11 20:46:53 +02:00
|
|
|
|
ic = !pat_has_uppercase(pat);
|
|
|
|
|
no_smartcase = FALSE;
|
|
|
|
|
|
|
|
|
|
return ic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2015-08-11 14:26:19 +02:00
|
|
|
|
* Return TRUE if pattern "pat" has an uppercase character.
|
2010-07-11 20:46:53 +02:00
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
pat_has_uppercase(char_u *pat)
|
2010-07-11 20:46:53 +02:00
|
|
|
|
{
|
|
|
|
|
char_u *p = pat;
|
|
|
|
|
|
|
|
|
|
while (*p != NUL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2010-07-11 20:46:53 +02:00
|
|
|
|
int l;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2010-07-11 20:46:53 +02:00
|
|
|
|
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
|
|
|
|
|
{
|
|
|
|
|
if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
|
|
|
|
|
return TRUE;
|
|
|
|
|
p += l;
|
|
|
|
|
}
|
2019-01-24 17:18:42 +01:00
|
|
|
|
else if (*p == '\\')
|
2010-07-11 20:46:53 +02:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (p[1] == '_' && p[2] != NUL) // skip "\_X"
|
2010-07-11 20:46:53 +02:00
|
|
|
|
p += 3;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
|
2010-07-11 20:46:53 +02:00
|
|
|
|
p += 3;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else if (p[1] != NUL) // skip "\X"
|
2010-07-11 20:46:53 +02:00
|
|
|
|
p += 2;
|
|
|
|
|
else
|
|
|
|
|
p += 1;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2010-07-11 20:46:53 +02:00
|
|
|
|
else if (MB_ISUPPER(*p))
|
|
|
|
|
return TRUE;
|
|
|
|
|
else
|
|
|
|
|
++p;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2010-07-11 20:46:53 +02:00
|
|
|
|
return FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-20 15:30:40 +01:00
|
|
|
|
#if defined(FEAT_EVAL) || defined(PROTO)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
char_u *
|
2016-01-30 21:10:09 +01:00
|
|
|
|
last_csearch(void)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
{
|
|
|
|
|
return lastc_bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
last_csearch_forward(void)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
{
|
|
|
|
|
return lastcdir == FORWARD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
last_csearch_until(void)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
{
|
|
|
|
|
return last_t_cmd == TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
{
|
|
|
|
|
*lastc = c;
|
|
|
|
|
lastc_bytelen = len;
|
|
|
|
|
if (len)
|
|
|
|
|
memcpy(lastc_bytes, s, len);
|
|
|
|
|
else
|
2020-04-12 19:37:17 +02:00
|
|
|
|
CLEAR_FIELD(lastc_bytes);
|
2015-08-11 14:26:19 +02:00
|
|
|
|
}
|
2019-01-20 15:30:40 +01:00
|
|
|
|
#endif
|
2015-08-11 14:26:19 +02:00
|
|
|
|
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
set_csearch_direction(int cdir)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
{
|
|
|
|
|
lastcdir = cdir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
set_csearch_until(int t_cmd)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
{
|
|
|
|
|
last_t_cmd = t_cmd;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
char_u *
|
2016-01-30 21:10:09 +01:00
|
|
|
|
last_search_pat(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
return spats[last_idx].pat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Reset search direction to forward. For "gd" and "gD" commands.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
reset_search_dir(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
spats[0].off.dir = '/';
|
2008-06-24 22:58:06 +00:00
|
|
|
|
#if defined(FEAT_EVAL)
|
|
|
|
|
set_vv_searchforward();
|
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
|
|
|
|
|
/*
|
|
|
|
|
* Set the last search pattern. For ":let @/ =" and viminfo.
|
|
|
|
|
* Also set the saved search pattern, so that this works in an autocommand.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
set_last_search_pat(
|
|
|
|
|
char_u *s,
|
|
|
|
|
int idx,
|
|
|
|
|
int magic,
|
|
|
|
|
int setlast)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
vim_free(spats[idx].pat);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// An empty string means that nothing should be matched.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (*s == NUL)
|
|
|
|
|
spats[idx].pat = NULL;
|
|
|
|
|
else
|
|
|
|
|
spats[idx].pat = vim_strsave(s);
|
|
|
|
|
spats[idx].magic = magic;
|
|
|
|
|
spats[idx].no_scs = FALSE;
|
|
|
|
|
spats[idx].off.dir = '/';
|
2008-06-24 22:58:06 +00:00
|
|
|
|
#if defined(FEAT_EVAL)
|
|
|
|
|
set_vv_searchforward();
|
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
spats[idx].off.line = FALSE;
|
|
|
|
|
spats[idx].off.end = FALSE;
|
|
|
|
|
spats[idx].off.off = 0;
|
|
|
|
|
if (setlast)
|
|
|
|
|
last_idx = idx;
|
|
|
|
|
if (save_level)
|
|
|
|
|
{
|
|
|
|
|
vim_free(saved_spats[idx].pat);
|
|
|
|
|
saved_spats[idx] = spats[0];
|
|
|
|
|
if (spats[idx].pat == NULL)
|
|
|
|
|
saved_spats[idx].pat = NULL;
|
|
|
|
|
else
|
|
|
|
|
saved_spats[idx].pat = vim_strsave(spats[idx].pat);
|
2019-03-03 14:42:11 +01:00
|
|
|
|
# ifdef FEAT_SEARCH_EXTRA
|
2018-12-01 21:08:21 +01:00
|
|
|
|
saved_spats_last_idx = last_idx;
|
2019-03-03 14:42:11 +01:00
|
|
|
|
# endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
# ifdef FEAT_SEARCH_EXTRA
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// If 'hlsearch' set and search pat changed: need redraw.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (p_hls && idx == last_idx && !no_hlsearch)
|
2006-03-12 22:10:07 +00:00
|
|
|
|
redraw_all_later(SOME_VALID);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
|
|
|
|
/*
|
|
|
|
|
* Get a regexp program for the last used search pattern.
|
|
|
|
|
* This is used for highlighting all matches in a window.
|
|
|
|
|
* Values returned in regmatch->regprog and regmatch->rmm_ic.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
last_pat_prog(regmmatch_T *regmatch)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (spats[last_idx].pat == NULL)
|
|
|
|
|
{
|
|
|
|
|
regmatch->regprog = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
++emsg_off; // So it doesn't beep if bad expr
|
2004-06-13 20:20:40 +00:00
|
|
|
|
(void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
|
|
|
|
|
--emsg_off;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
2014-03-23 15:13:05 +01:00
|
|
|
|
* Lowest level search function.
|
2018-12-23 19:10:09 +01:00
|
|
|
|
* Search for 'count'th occurrence of pattern "pat" in direction "dir".
|
|
|
|
|
* Start at position "pos" and return the found position in "pos".
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*
|
|
|
|
|
* if (options & SEARCH_MSG) == 0 don't give any messages
|
|
|
|
|
* if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
|
|
|
|
|
* if (options & SEARCH_MSG) == SEARCH_MSG give all messages
|
|
|
|
|
* if (options & SEARCH_HIS) put search pattern in history
|
|
|
|
|
* if (options & SEARCH_END) return position at end of match
|
|
|
|
|
* if (options & SEARCH_START) accept match at pos itself
|
|
|
|
|
* if (options & SEARCH_KEEP) keep previous search pattern
|
|
|
|
|
* if (options & SEARCH_FOLD) match only once in a closed fold
|
|
|
|
|
* if (options & SEARCH_PEEK) check for typed char, cancel search
|
2015-12-28 19:20:36 +01:00
|
|
|
|
* if (options & SEARCH_COL) start at pos->col instead of zero
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*
|
|
|
|
|
* Return FAIL (zero) for failure, non-zero for success.
|
|
|
|
|
* When FEAT_EVAL is defined, returns the index of the first matching
|
|
|
|
|
* subpattern plus one; one if there was none.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
searchit(
|
2019-10-18 20:53:34 +02:00
|
|
|
|
win_T *win, // window to search in; can be NULL for a
|
|
|
|
|
// buffer without a window!
|
2016-01-30 21:10:09 +01:00
|
|
|
|
buf_T *buf,
|
|
|
|
|
pos_T *pos,
|
2018-12-23 19:10:09 +01:00
|
|
|
|
pos_T *end_pos, // set to end of the match, unless NULL
|
2016-01-30 21:10:09 +01:00
|
|
|
|
int dir,
|
|
|
|
|
char_u *pat,
|
|
|
|
|
long count,
|
|
|
|
|
int options,
|
2019-10-18 20:53:34 +02:00
|
|
|
|
int pat_use, // which pattern to use when "pat" is empty
|
|
|
|
|
searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
int found;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
linenr_T lnum; // no init to shut up Apollo cc
|
2015-12-28 19:20:36 +01:00
|
|
|
|
colnr_T col;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
regmmatch_T regmatch;
|
|
|
|
|
char_u *ptr;
|
|
|
|
|
colnr_T matchcol;
|
|
|
|
|
lpos_T endpos;
|
2005-01-27 14:41:15 +00:00
|
|
|
|
lpos_T matchpos;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
int loop;
|
|
|
|
|
pos_T start_pos;
|
|
|
|
|
int at_first_line;
|
|
|
|
|
int extra_col;
|
2015-07-10 14:43:35 +02:00
|
|
|
|
int start_char_len;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
int match_ok;
|
|
|
|
|
long nmatched;
|
|
|
|
|
int submatch = 0;
|
2014-11-27 17:29:56 +01:00
|
|
|
|
int first_match = TRUE;
|
2019-12-23 22:59:18 +01:00
|
|
|
|
int called_emsg_before = called_emsg;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
|
|
|
|
int break_loop = FALSE;
|
|
|
|
|
#endif
|
2019-10-18 20:53:34 +02:00
|
|
|
|
linenr_T stop_lnum = 0; // stop after this line number when != 0
|
|
|
|
|
#ifdef FEAT_RELTIME
|
|
|
|
|
proftime_T *tm = NULL; // timeout limit or NULL
|
|
|
|
|
int *timed_out = NULL; // set when timed out or NULL
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (extra_arg != NULL)
|
|
|
|
|
{
|
|
|
|
|
stop_lnum = extra_arg->sa_stop_lnum;
|
|
|
|
|
#ifdef FEAT_RELTIME
|
|
|
|
|
tm = extra_arg->sa_tm;
|
|
|
|
|
timed_out = &extra_arg->sa_timed_out;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
if (search_regcomp(pat, RE_SEARCH, pat_use,
|
|
|
|
|
(options & (SEARCH_HIS + SEARCH_KEEP)), ®match) == FAIL)
|
|
|
|
|
{
|
|
|
|
|
if ((options & SEARCH_MSG) && !rc_did_emsg)
|
2019-01-13 23:38:42 +01:00
|
|
|
|
semsg(_("E383: Invalid search string: %s"), mr_pattern);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-30 00:14:18 +00:00
|
|
|
|
/*
|
|
|
|
|
* find the string
|
|
|
|
|
*/
|
2019-12-05 21:10:38 +01:00
|
|
|
|
do // loop for count
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// When not accepting a match at the start position set "extra_col" to
|
|
|
|
|
// a non-zero value. Don't do that when starting at MAXCOL, since
|
|
|
|
|
// MAXCOL + 1 is zero.
|
2015-07-10 14:43:35 +02:00
|
|
|
|
if (pos->col == MAXCOL)
|
|
|
|
|
start_char_len = 0;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Watch out for the "col" being MAXCOL - 2, used in a closed fold.
|
2015-07-10 14:43:35 +02:00
|
|
|
|
else if (has_mbyte
|
|
|
|
|
&& pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
|
|
|
|
|
&& pos->col < MAXCOL - 2)
|
2014-11-27 17:29:56 +01:00
|
|
|
|
{
|
2018-02-09 18:09:54 +01:00
|
|
|
|
ptr = ml_get_buf(buf, pos->lnum, FALSE);
|
2018-02-09 19:24:01 +01:00
|
|
|
|
if ((int)STRLEN(ptr) <= pos->col)
|
2015-07-10 14:43:35 +02:00
|
|
|
|
start_char_len = 1;
|
2014-11-27 17:29:56 +01:00
|
|
|
|
else
|
2018-02-09 18:09:54 +01:00
|
|
|
|
start_char_len = (*mb_ptr2len)(ptr + pos->col);
|
2014-11-27 17:29:56 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2015-07-10 14:43:35 +02:00
|
|
|
|
start_char_len = 1;
|
|
|
|
|
if (dir == FORWARD)
|
|
|
|
|
{
|
|
|
|
|
if (options & SEARCH_START)
|
|
|
|
|
extra_col = 0;
|
|
|
|
|
else
|
|
|
|
|
extra_col = start_char_len;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (options & SEARCH_START)
|
|
|
|
|
extra_col = start_char_len;
|
|
|
|
|
else
|
|
|
|
|
extra_col = 0;
|
|
|
|
|
}
|
2014-11-27 17:29:56 +01:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
start_pos = *pos; // remember start pos for detecting no match
|
|
|
|
|
found = 0; // default: not found
|
|
|
|
|
at_first_line = TRUE; // default: start in first line
|
|
|
|
|
if (pos->lnum == 0) // correct lnum for when starting in line 0
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
pos->lnum = 1;
|
|
|
|
|
pos->col = 0;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
at_first_line = FALSE; // not in first line now
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Start searching in current line, unless searching backwards and
|
|
|
|
|
* we're in column 0.
|
2007-07-10 11:28:55 +00:00
|
|
|
|
* If we are searching backwards, in column 0, and not including the
|
|
|
|
|
* current position, gain some efficiency by skipping back a line.
|
|
|
|
|
* Otherwise begin the search in the current line.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
2007-07-10 11:28:55 +00:00
|
|
|
|
if (dir == BACKWARD && start_pos.col == 0
|
|
|
|
|
&& (options & SEARCH_START) == 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
lnum = pos->lnum - 1;
|
|
|
|
|
at_first_line = FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
lnum = pos->lnum;
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
|
|
|
|
|
lnum += dir, at_first_line = FALSE)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Stop after checking "stop_lnum", if it's set.
|
2006-02-27 00:08:02 +00:00
|
|
|
|
if (stop_lnum != 0 && (dir == FORWARD
|
|
|
|
|
? lnum > stop_lnum : lnum < stop_lnum))
|
|
|
|
|
break;
|
2008-01-06 19:07:36 +00:00
|
|
|
|
#ifdef FEAT_RELTIME
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Stop after passing the "tm" time limit.
|
2008-01-06 19:07:36 +00:00
|
|
|
|
if (tm != NULL && profile_passed_limit(tm))
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
2006-02-27 00:08:02 +00:00
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
/*
|
2005-01-27 14:41:15 +00:00
|
|
|
|
* Look for a match somewhere in line "lnum".
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
2015-12-28 19:20:36 +01:00
|
|
|
|
col = at_first_line && (options & SEARCH_COL) ? pos->col
|
|
|
|
|
: (colnr_T)0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
nmatched = vim_regexec_multi(®match, win, buf,
|
2015-12-28 19:20:36 +01:00
|
|
|
|
lnum, col,
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#ifdef FEAT_RELTIME
|
2017-06-17 18:44:21 +02:00
|
|
|
|
tm, timed_out
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#else
|
2017-06-17 18:44:21 +02:00
|
|
|
|
NULL, NULL
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#endif
|
|
|
|
|
);
|
2020-10-02 20:36:01 +02:00
|
|
|
|
// vim_regexec_multi() may clear "regprog"
|
|
|
|
|
if (regmatch.regprog == NULL)
|
|
|
|
|
break;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Abort searching on an error (e.g., out of stack).
|
2019-12-23 22:59:18 +01:00
|
|
|
|
if (called_emsg > called_emsg_before
|
2017-06-17 18:44:21 +02:00
|
|
|
|
#ifdef FEAT_RELTIME
|
|
|
|
|
|| (timed_out != NULL && *timed_out)
|
|
|
|
|
#endif
|
|
|
|
|
)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
if (nmatched > 0)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// match may actually be in another line when using \zs
|
2005-01-27 14:41:15 +00:00
|
|
|
|
matchpos = regmatch.startpos[0];
|
2004-06-13 20:20:40 +00:00
|
|
|
|
endpos = regmatch.endpos[0];
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#ifdef FEAT_EVAL
|
2004-06-13 20:20:40 +00:00
|
|
|
|
submatch = first_submatch(®match);
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#endif
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// "lnum" may be past end of buffer for "\n\zs".
|
2006-02-24 23:53:04 +00:00
|
|
|
|
if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
|
|
|
|
|
ptr = (char_u *)"";
|
|
|
|
|
else
|
|
|
|
|
ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Forward search in the first line: match should be after
|
|
|
|
|
* the start position. If not, continue at the end of the
|
|
|
|
|
* match (this is vi compatible) or on the next char.
|
|
|
|
|
*/
|
|
|
|
|
if (dir == FORWARD && at_first_line)
|
|
|
|
|
{
|
|
|
|
|
match_ok = TRUE;
|
|
|
|
|
/*
|
2005-01-27 14:41:15 +00:00
|
|
|
|
* When the match starts in a next line it's certainly
|
|
|
|
|
* past the start position.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
* When match lands on a NUL the cursor will be put
|
|
|
|
|
* one back afterwards, compare with that position,
|
|
|
|
|
* otherwise "/$" will get stuck on end of line.
|
|
|
|
|
*/
|
2005-01-27 14:41:15 +00:00
|
|
|
|
while (matchpos.lnum == 0
|
2014-11-27 17:29:56 +01:00
|
|
|
|
&& ((options & SEARCH_END) && first_match
|
2005-01-27 14:41:15 +00:00
|
|
|
|
? (nmatched == 1
|
|
|
|
|
&& (int)endpos.col - 1
|
2004-06-13 20:20:40 +00:00
|
|
|
|
< (int)start_pos.col + extra_col)
|
2005-01-27 14:41:15 +00:00
|
|
|
|
: ((int)matchpos.col
|
|
|
|
|
- (ptr[matchpos.col] == NUL)
|
|
|
|
|
< (int)start_pos.col + extra_col)))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* If vi-compatible searching, continue at the end
|
|
|
|
|
* of the match, otherwise continue one position
|
|
|
|
|
* forward.
|
|
|
|
|
*/
|
|
|
|
|
if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (nmatched > 1)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// end is in next line, thus no match in
|
|
|
|
|
// this line
|
2004-06-13 20:20:40 +00:00
|
|
|
|
match_ok = FALSE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
matchcol = endpos.col;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// for empty match: advance one char
|
2005-01-27 14:41:15 +00:00
|
|
|
|
if (matchcol == matchpos.col
|
2004-06-13 20:20:40 +00:00
|
|
|
|
&& ptr[matchcol] != NUL)
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
matchcol +=
|
2005-08-10 21:07:57 +00:00
|
|
|
|
(*mb_ptr2len)(ptr + matchcol);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
++matchcol;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-01-27 14:41:15 +00:00
|
|
|
|
matchcol = matchpos.col;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (ptr[matchcol] != NUL)
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
2005-08-10 21:07:57 +00:00
|
|
|
|
matchcol += (*mb_ptr2len)(ptr
|
2004-06-13 20:20:40 +00:00
|
|
|
|
+ matchcol);
|
|
|
|
|
else
|
|
|
|
|
++matchcol;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-03 21:14:29 +02:00
|
|
|
|
if (matchcol == 0 && (options & SEARCH_START))
|
2013-03-19 15:27:48 +01:00
|
|
|
|
break;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (ptr[matchcol] == NUL
|
|
|
|
|
|| (nmatched = vim_regexec_multi(®match,
|
2005-01-27 14:41:15 +00:00
|
|
|
|
win, buf, lnum + matchpos.lnum,
|
2008-01-19 14:59:58 +00:00
|
|
|
|
matchcol,
|
|
|
|
|
#ifdef FEAT_RELTIME
|
2017-06-17 18:44:21 +02:00
|
|
|
|
tm, timed_out
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#else
|
2017-06-17 18:44:21 +02:00
|
|
|
|
NULL, NULL
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#endif
|
|
|
|
|
)) == 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
match_ok = FALSE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-10-02 20:36:01 +02:00
|
|
|
|
// vim_regexec_multi() may clear "regprog"
|
|
|
|
|
if (regmatch.regprog == NULL)
|
|
|
|
|
break;
|
2005-01-27 14:41:15 +00:00
|
|
|
|
matchpos = regmatch.startpos[0];
|
2004-06-13 20:20:40 +00:00
|
|
|
|
endpos = regmatch.endpos[0];
|
|
|
|
|
# ifdef FEAT_EVAL
|
|
|
|
|
submatch = first_submatch(®match);
|
|
|
|
|
# endif
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Need to get the line pointer again, a
|
|
|
|
|
// multi-line search may have made it invalid.
|
2005-01-27 14:41:15 +00:00
|
|
|
|
ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (!match_ok)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (dir == BACKWARD)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Now, if there are multiple matches on this line,
|
|
|
|
|
* we have to get the last one. Or the last one before
|
|
|
|
|
* the cursor, if we're on that line.
|
|
|
|
|
* When putting the new cursor at the end, compare
|
|
|
|
|
* relative to the end of the match.
|
|
|
|
|
*/
|
|
|
|
|
match_ok = FALSE;
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Remember a position that is before the start
|
|
|
|
|
// position, we use it if it's the last match in
|
|
|
|
|
// the line. Always accept a position after
|
|
|
|
|
// wrapping around.
|
2005-01-27 14:41:15 +00:00
|
|
|
|
if (loop
|
|
|
|
|
|| ((options & SEARCH_END)
|
|
|
|
|
? (lnum + regmatch.endpos[0].lnum
|
|
|
|
|
< start_pos.lnum
|
|
|
|
|
|| (lnum + regmatch.endpos[0].lnum
|
|
|
|
|
== start_pos.lnum
|
|
|
|
|
&& (int)regmatch.endpos[0].col - 1
|
2015-07-10 14:43:35 +02:00
|
|
|
|
< (int)start_pos.col
|
|
|
|
|
+ extra_col))
|
2005-01-27 14:41:15 +00:00
|
|
|
|
: (lnum + regmatch.startpos[0].lnum
|
|
|
|
|
< start_pos.lnum
|
|
|
|
|
|| (lnum + regmatch.startpos[0].lnum
|
|
|
|
|
== start_pos.lnum
|
|
|
|
|
&& (int)regmatch.startpos[0].col
|
2015-07-10 14:43:35 +02:00
|
|
|
|
< (int)start_pos.col
|
|
|
|
|
+ extra_col))))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
match_ok = TRUE;
|
2005-01-27 14:41:15 +00:00
|
|
|
|
matchpos = regmatch.startpos[0];
|
2004-06-13 20:20:40 +00:00
|
|
|
|
endpos = regmatch.endpos[0];
|
|
|
|
|
# ifdef FEAT_EVAL
|
|
|
|
|
submatch = first_submatch(®match);
|
|
|
|
|
# endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We found a valid match, now check if there is
|
|
|
|
|
* another one after it.
|
|
|
|
|
* If vi-compatible searching, continue at the end
|
|
|
|
|
* of the match, otherwise continue one position
|
|
|
|
|
* forward.
|
|
|
|
|
*/
|
|
|
|
|
if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (nmatched > 1)
|
|
|
|
|
break;
|
|
|
|
|
matchcol = endpos.col;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// for empty match: advance one char
|
2005-01-27 14:41:15 +00:00
|
|
|
|
if (matchcol == matchpos.col
|
2004-06-13 20:20:40 +00:00
|
|
|
|
&& ptr[matchcol] != NUL)
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
matchcol +=
|
2005-08-10 21:07:57 +00:00
|
|
|
|
(*mb_ptr2len)(ptr + matchcol);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
++matchcol;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Stop when the match is in a next line.
|
2005-01-27 14:41:15 +00:00
|
|
|
|
if (matchpos.lnum > 0)
|
|
|
|
|
break;
|
|
|
|
|
matchcol = matchpos.col;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (ptr[matchcol] != NUL)
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
matchcol +=
|
2005-08-10 21:07:57 +00:00
|
|
|
|
(*mb_ptr2len)(ptr + matchcol);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
++matchcol;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ptr[matchcol] == NUL
|
|
|
|
|
|| (nmatched = vim_regexec_multi(®match,
|
2005-01-27 14:41:15 +00:00
|
|
|
|
win, buf, lnum + matchpos.lnum,
|
2008-01-19 14:59:58 +00:00
|
|
|
|
matchcol,
|
|
|
|
|
#ifdef FEAT_RELTIME
|
2017-06-17 18:44:21 +02:00
|
|
|
|
tm, timed_out
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#else
|
2017-06-17 18:44:21 +02:00
|
|
|
|
NULL, NULL
|
2008-01-19 14:59:58 +00:00
|
|
|
|
#endif
|
|
|
|
|
)) == 0)
|
2018-02-09 16:04:25 +01:00
|
|
|
|
{
|
|
|
|
|
#ifdef FEAT_RELTIME
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// If the search timed out, we did find a match
|
|
|
|
|
// but it might be the wrong one, so that's not
|
|
|
|
|
// OK.
|
2018-02-09 16:04:25 +01:00
|
|
|
|
if (timed_out != NULL && *timed_out)
|
|
|
|
|
match_ok = FALSE;
|
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
2018-02-09 16:04:25 +01:00
|
|
|
|
}
|
2020-10-02 20:36:01 +02:00
|
|
|
|
// vim_regexec_multi() may clear "regprog"
|
|
|
|
|
if (regmatch.regprog == NULL)
|
|
|
|
|
break;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Need to get the line pointer again, a
|
|
|
|
|
// multi-line search may have made it invalid.
|
2005-01-27 14:41:15 +00:00
|
|
|
|
ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If there is only a match after the cursor, skip
|
|
|
|
|
* this match.
|
|
|
|
|
*/
|
|
|
|
|
if (!match_ok)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// With the SEARCH_END option move to the last character
|
|
|
|
|
// of the match. Don't do it for an empty match, end
|
|
|
|
|
// should be same as start then.
|
2013-04-03 21:14:29 +02:00
|
|
|
|
if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
|
2008-02-20 12:43:01 +00:00
|
|
|
|
&& !(matchpos.lnum == endpos.lnum
|
|
|
|
|
&& matchpos.col == endpos.col))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// For a match in the first column, set the position
|
|
|
|
|
// on the NUL in the previous line.
|
2005-01-27 14:41:15 +00:00
|
|
|
|
pos->lnum = lnum + endpos.lnum;
|
2008-02-20 12:43:01 +00:00
|
|
|
|
pos->col = endpos.col;
|
|
|
|
|
if (endpos.col == 0)
|
2006-04-05 20:41:53 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (pos->lnum > 1) // just in case
|
2008-02-20 12:43:01 +00:00
|
|
|
|
{
|
|
|
|
|
--pos->lnum;
|
|
|
|
|
pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
|
|
|
|
|
pos->lnum, FALSE));
|
|
|
|
|
}
|
2006-04-05 20:41:53 +00:00
|
|
|
|
}
|
2008-02-20 12:43:01 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
--pos->col;
|
|
|
|
|
if (has_mbyte
|
|
|
|
|
&& pos->lnum <= buf->b_ml.ml_line_count)
|
|
|
|
|
{
|
|
|
|
|
ptr = ml_get_buf(buf, pos->lnum, FALSE);
|
|
|
|
|
pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-23 19:10:09 +01:00
|
|
|
|
if (end_pos != NULL)
|
|
|
|
|
{
|
|
|
|
|
end_pos->lnum = lnum + matchpos.lnum;
|
|
|
|
|
end_pos->col = matchpos.col;
|
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-01-27 14:41:15 +00:00
|
|
|
|
pos->lnum = lnum + matchpos.lnum;
|
|
|
|
|
pos->col = matchpos.col;
|
2018-12-23 19:10:09 +01:00
|
|
|
|
if (end_pos != NULL)
|
|
|
|
|
{
|
|
|
|
|
end_pos->lnum = lnum + endpos.lnum;
|
|
|
|
|
end_pos->col = endpos.col;
|
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
pos->coladd = 0;
|
2018-12-23 19:10:09 +01:00
|
|
|
|
if (end_pos != NULL)
|
|
|
|
|
end_pos->coladd = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
found = 1;
|
2014-11-27 17:29:56 +01:00
|
|
|
|
first_match = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Set variables used for 'incsearch' highlighting.
|
2005-01-27 14:41:15 +00:00
|
|
|
|
search_match_lines = endpos.lnum - matchpos.lnum;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
search_match_endcol = endpos.col;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
line_breakcheck(); // stop if ctrl-C typed
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (got_int)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Cancel searching if a character was typed. Used for
|
|
|
|
|
// 'incsearch'. Don't check too often, that would slowdown
|
|
|
|
|
// searching too much.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if ((options & SEARCH_PEEK)
|
|
|
|
|
&& ((lnum - pos->lnum) & 0x3f) == 0
|
|
|
|
|
&& char_avail())
|
|
|
|
|
{
|
|
|
|
|
break_loop = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (loop && lnum == start_pos.lnum)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
break; // if second loop, stop where started
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
at_first_line = FALSE;
|
|
|
|
|
|
2020-10-02 20:36:01 +02:00
|
|
|
|
// vim_regexec_multi() may clear "regprog"
|
|
|
|
|
if (regmatch.regprog == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
/*
|
2006-02-27 00:08:02 +00:00
|
|
|
|
* Stop the search if wrapscan isn't set, "stop_lnum" is
|
|
|
|
|
* specified, after an interrupt, after a match and after looping
|
|
|
|
|
* twice.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
2019-12-23 22:59:18 +01:00
|
|
|
|
if (!p_ws || stop_lnum != 0 || got_int
|
|
|
|
|
|| called_emsg > called_emsg_before
|
2017-06-17 18:44:21 +02:00
|
|
|
|
#ifdef FEAT_RELTIME
|
|
|
|
|
|| (timed_out != NULL && *timed_out)
|
|
|
|
|
#endif
|
2009-05-15 19:33:18 +00:00
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
2017-06-17 18:44:21 +02:00
|
|
|
|
|| break_loop
|
2009-05-15 19:33:18 +00:00
|
|
|
|
#endif
|
2017-06-17 18:44:21 +02:00
|
|
|
|
|| found || loop)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If 'wrapscan' is set we continue at the other end of the file.
|
|
|
|
|
* If 'shortmess' does not contain 's', we give a message.
|
|
|
|
|
* This message is also remembered in keep_msg for when the screen
|
|
|
|
|
* is redrawn. The keep_msg is cleared whenever another message is
|
|
|
|
|
* written.
|
|
|
|
|
*/
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (dir == BACKWARD) // start second loop at the other end
|
2004-06-13 20:20:40 +00:00
|
|
|
|
lnum = buf->b_ml.ml_line_count;
|
|
|
|
|
else
|
|
|
|
|
lnum = 1;
|
2005-09-05 22:11:52 +00:00
|
|
|
|
if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
|
|
|
|
|
give_warning((char_u *)_(dir == BACKWARD
|
|
|
|
|
? top_bot_msg : bot_top_msg), TRUE);
|
2019-10-18 20:53:34 +02:00
|
|
|
|
if (extra_arg != NULL)
|
|
|
|
|
extra_arg->sa_wrapped = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2019-12-23 22:59:18 +01:00
|
|
|
|
if (got_int || called_emsg > called_emsg_before
|
2017-06-17 18:44:21 +02:00
|
|
|
|
#ifdef FEAT_RELTIME
|
|
|
|
|
|| (timed_out != NULL && *timed_out)
|
|
|
|
|
#endif
|
2009-05-15 19:33:18 +00:00
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
|
|
|
|
|| break_loop
|
|
|
|
|
#endif
|
|
|
|
|
)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
while (--count > 0 && found); // stop after count matches or no match
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2013-06-08 18:19:48 +02:00
|
|
|
|
vim_regfree(regmatch.regprog);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (!found) // did not find it
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (got_int)
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_(e_interr));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if ((options & SEARCH_MSG) == SEARCH_MSG)
|
|
|
|
|
{
|
|
|
|
|
if (p_ws)
|
2019-01-13 23:38:42 +01:00
|
|
|
|
semsg(_(e_patnotf2), mr_pattern);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if (lnum == 0)
|
2019-01-13 23:38:42 +01:00
|
|
|
|
semsg(_("E384: search hit TOP without match for: %s"),
|
2004-06-13 20:20:40 +00:00
|
|
|
|
mr_pattern);
|
|
|
|
|
else
|
2019-01-13 23:38:42 +01:00
|
|
|
|
semsg(_("E385: search hit BOTTOM without match for: %s"),
|
2004-06-13 20:20:40 +00:00
|
|
|
|
mr_pattern);
|
|
|
|
|
}
|
|
|
|
|
return FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// A pattern like "\n\zs" may go past the last line.
|
2006-02-24 23:53:04 +00:00
|
|
|
|
if (pos->lnum > buf->b_ml.ml_line_count)
|
|
|
|
|
{
|
|
|
|
|
pos->lnum = buf->b_ml.ml_line_count;
|
2006-04-17 22:14:47 +00:00
|
|
|
|
pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
|
2006-02-24 23:53:04 +00:00
|
|
|
|
if (pos->col > 0)
|
|
|
|
|
--pos->col;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return submatch + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_EVAL
|
2008-06-24 22:58:06 +00:00
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
set_search_direction(int cdir)
|
2008-06-24 22:58:06 +00:00
|
|
|
|
{
|
|
|
|
|
spats[0].off.dir = cdir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
set_vv_searchforward(void)
|
2008-06-24 22:58:06 +00:00
|
|
|
|
{
|
|
|
|
|
set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Return the number of the first subpat that matched.
|
2015-12-28 19:20:36 +01:00
|
|
|
|
* Return zero if none of them matched.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
|
|
|
|
static int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
first_submatch(regmmatch_T *rp)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
int submatch;
|
|
|
|
|
|
|
|
|
|
for (submatch = 1; ; ++submatch)
|
|
|
|
|
{
|
|
|
|
|
if (rp->startpos[submatch].lnum >= 0)
|
|
|
|
|
break;
|
|
|
|
|
if (submatch == 9)
|
|
|
|
|
{
|
|
|
|
|
submatch = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return submatch;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Highest level string search function.
|
2007-05-10 18:59:07 +00:00
|
|
|
|
* Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
* If 'dirc' is 0: use previous dir.
|
|
|
|
|
* If 'pat' is NULL or empty : use previous string.
|
|
|
|
|
* If 'options & SEARCH_REV' : go in reverse of previous dir.
|
|
|
|
|
* If 'options & SEARCH_ECHO': echo the search command and handle options
|
|
|
|
|
* If 'options & SEARCH_MSG' : may give error message
|
|
|
|
|
* If 'options & SEARCH_OPT' : interpret optional flags
|
|
|
|
|
* If 'options & SEARCH_HIS' : put search pattern in history
|
|
|
|
|
* If 'options & SEARCH_NOOF': don't add offset to position
|
|
|
|
|
* If 'options & SEARCH_MARK': set previous context mark
|
|
|
|
|
* If 'options & SEARCH_KEEP': keep previous search pattern
|
|
|
|
|
* If 'options & SEARCH_START': accept match at curpos itself
|
|
|
|
|
* If 'options & SEARCH_PEEK': check for typed char, cancel search
|
|
|
|
|
*
|
|
|
|
|
* Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
|
|
|
|
|
* makes the movement linewise without moving the match position.
|
|
|
|
|
*
|
2015-03-05 19:57:49 +01:00
|
|
|
|
* Return 0 for failure, 1 for found, 2 for found and line offset added.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
do_search(
|
2019-12-05 21:10:38 +01:00
|
|
|
|
oparg_T *oap, // can be NULL
|
|
|
|
|
int dirc, // '/' or '?'
|
2020-02-21 21:30:52 +01:00
|
|
|
|
int search_delim, // the delimiter for the search, e.g. '%' in s%regex%replacement%
|
2016-01-30 21:10:09 +01:00
|
|
|
|
char_u *pat,
|
|
|
|
|
long count,
|
|
|
|
|
int options,
|
2019-10-18 20:53:34 +02:00
|
|
|
|
searchit_arg_T *sia) // optional arguments or NULL
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
pos_T pos; // position of the last match
|
2004-06-13 20:20:40 +00:00
|
|
|
|
char_u *searchstr;
|
2019-07-23 22:15:25 +02:00
|
|
|
|
soffset_T old_off;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int retval; // Return value
|
2004-06-13 20:20:40 +00:00
|
|
|
|
char_u *p;
|
|
|
|
|
long c;
|
|
|
|
|
char_u *dircp;
|
|
|
|
|
char_u *strcopy = NULL;
|
|
|
|
|
char_u *ps;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
char_u *msgbuf = NULL;
|
|
|
|
|
size_t len;
|
2019-05-24 22:08:15 +02:00
|
|
|
|
int has_offset = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* A line offset is not remembered, this is vi compatible.
|
|
|
|
|
*/
|
|
|
|
|
if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
|
|
|
|
|
{
|
|
|
|
|
spats[0].off.line = FALSE;
|
|
|
|
|
spats[0].off.off = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Save the values for when (options & SEARCH_KEEP) is used.
|
|
|
|
|
* (there is no "if ()" around this because gcc wants them initialized)
|
|
|
|
|
*/
|
|
|
|
|
old_off = spats[0].off;
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
pos = curwin->w_cursor; // start searching at the cursor position
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Find out the direction of the search.
|
|
|
|
|
*/
|
|
|
|
|
if (dirc == 0)
|
|
|
|
|
dirc = spats[0].off.dir;
|
|
|
|
|
else
|
2008-06-24 22:58:06 +00:00
|
|
|
|
{
|
2004-06-13 20:20:40 +00:00
|
|
|
|
spats[0].off.dir = dirc;
|
2008-06-24 22:58:06 +00:00
|
|
|
|
#if defined(FEAT_EVAL)
|
|
|
|
|
set_vv_searchforward();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (options & SEARCH_REV)
|
|
|
|
|
{
|
2019-02-17 17:44:42 +01:00
|
|
|
|
#ifdef MSWIN
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// There is a bug in the Visual C++ 2.2 compiler which means that
|
|
|
|
|
// dirc always ends up being '/'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
dirc = (dirc == '/') ? '?' : '/';
|
|
|
|
|
#else
|
|
|
|
|
if (dirc == '/')
|
|
|
|
|
dirc = '?';
|
|
|
|
|
else
|
|
|
|
|
dirc = '/';
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_FOLDING
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// If the cursor is in a closed fold, don't find another match in the same
|
|
|
|
|
// fold.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (dirc == '/')
|
|
|
|
|
{
|
|
|
|
|
if (hasFolding(pos.lnum, NULL, &pos.lnum))
|
2019-12-05 21:10:38 +01:00
|
|
|
|
pos.col = MAXCOL - 2; // avoid overflow when adding 1
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (hasFolding(pos.lnum, &pos.lnum, NULL))
|
|
|
|
|
pos.col = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_SEARCH_EXTRA
|
|
|
|
|
/*
|
|
|
|
|
* Turn 'hlsearch' highlighting back on.
|
|
|
|
|
*/
|
|
|
|
|
if (no_hlsearch && !(options & SEARCH_KEEP))
|
|
|
|
|
{
|
2006-03-12 22:10:07 +00:00
|
|
|
|
redraw_all_later(SOME_VALID);
|
2018-04-27 22:53:07 +02:00
|
|
|
|
set_no_hlsearch(FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
|
|
|
|
|
*/
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
2019-10-18 20:53:34 +02:00
|
|
|
|
int show_top_bot_msg = FALSE;
|
2019-05-06 21:37:18 +02:00
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
searchstr = pat;
|
|
|
|
|
dircp = NULL;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// use previous pattern
|
2020-02-21 21:30:52 +01:00
|
|
|
|
if (pat == NULL || *pat == NUL || *pat == search_delim)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (spats[RE_SEARCH].pat == NULL) // no previous pattern
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2016-09-09 21:41:34 +02:00
|
|
|
|
searchstr = spats[RE_SUBST].pat;
|
|
|
|
|
if (searchstr == NULL)
|
2011-02-25 18:38:36 +01:00
|
|
|
|
{
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_(e_noprevre));
|
2011-02-25 18:38:36 +01:00
|
|
|
|
retval = 0;
|
|
|
|
|
goto end_do_search;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// make search_regcomp() use spats[RE_SEARCH].pat
|
2011-02-25 18:38:36 +01:00
|
|
|
|
searchstr = (char_u *)"";
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (pat != NULL && *pat != NUL) // look for (new) offset
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Find end of regular expression.
|
|
|
|
|
* If there is a matching '/' or '?', toss it.
|
|
|
|
|
*/
|
|
|
|
|
ps = strcopy;
|
2020-12-21 19:59:08 +01:00
|
|
|
|
p = skip_regexp_ex(pat, search_delim, magic_isset(),
|
2021-01-04 12:42:13 +01:00
|
|
|
|
&strcopy, NULL, NULL);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (strcopy != ps)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// made a copy of "pat" to change "\?" to "?"
|
2006-04-17 22:14:47 +00:00
|
|
|
|
searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
pat = strcopy;
|
|
|
|
|
searchstr = strcopy;
|
|
|
|
|
}
|
2020-02-21 21:30:52 +01:00
|
|
|
|
if (*p == search_delim)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
dircp = p; // remember where we put the NUL
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*p++ = NUL;
|
|
|
|
|
}
|
|
|
|
|
spats[0].off.line = FALSE;
|
|
|
|
|
spats[0].off.end = FALSE;
|
|
|
|
|
spats[0].off.off = 0;
|
|
|
|
|
/*
|
|
|
|
|
* Check for a line offset or a character offset.
|
|
|
|
|
* For get_address (echo off) we don't check for a character
|
|
|
|
|
* offset, because it is meaningless and the 's' could be a
|
|
|
|
|
* substitute command.
|
|
|
|
|
*/
|
|
|
|
|
if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
|
|
|
|
|
spats[0].off.line = TRUE;
|
|
|
|
|
else if ((options & SEARCH_OPT) &&
|
|
|
|
|
(*p == 'e' || *p == 's' || *p == 'b'))
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (*p == 'e') // end
|
2004-06-13 20:20:40 +00:00
|
|
|
|
spats[0].off.end = SEARCH_END;
|
|
|
|
|
++p;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// 'nr' or '+nr' or '-nr'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
|
|
|
|
|
spats[0].off.off = atol((char *)p);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else if (*p == '-') // single '-'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
spats[0].off.off = -1;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else // single '+'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
spats[0].off.off = 1;
|
|
|
|
|
++p;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
while (VIM_ISDIGIT(*p)) // skip number
|
2004-06-13 20:20:40 +00:00
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// compute length of search command for get_address()
|
2004-06-13 20:20:40 +00:00
|
|
|
|
searchcmdlen += (int)(p - pat);
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
pat = p; // put pat after search command
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 21:44:59 +02:00
|
|
|
|
if ((options & SEARCH_ECHO) && messaging() &&
|
|
|
|
|
!msg_silent &&
|
|
|
|
|
(!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
char_u *trunc;
|
2019-05-24 13:11:47 +02:00
|
|
|
|
char_u off_buf[40];
|
2019-05-24 17:56:14 +02:00
|
|
|
|
size_t off_len = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-05-04 21:08:40 +02:00
|
|
|
|
// Compute msg_row early.
|
|
|
|
|
msg_start();
|
|
|
|
|
|
2019-05-24 13:11:47 +02:00
|
|
|
|
// Get the offset, so we know how long it is.
|
2019-09-02 21:44:59 +02:00
|
|
|
|
if (!cmd_silent &&
|
|
|
|
|
(spats[0].off.line || spats[0].off.end || spats[0].off.off))
|
2019-05-24 13:11:47 +02:00
|
|
|
|
{
|
|
|
|
|
p = off_buf;
|
|
|
|
|
*p++ = dirc;
|
|
|
|
|
if (spats[0].off.end)
|
|
|
|
|
*p++ = 'e';
|
|
|
|
|
else if (!spats[0].off.line)
|
|
|
|
|
*p++ = 's';
|
|
|
|
|
if (spats[0].off.off > 0 || spats[0].off.line)
|
|
|
|
|
*p++ = '+';
|
|
|
|
|
*p = NUL;
|
|
|
|
|
if (spats[0].off.off != 0 || spats[0].off.line)
|
|
|
|
|
sprintf((char *)p, "%ld", spats[0].off.off);
|
|
|
|
|
off_len = STRLEN(off_buf);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (*searchstr == NUL)
|
2018-12-01 13:14:45 +01:00
|
|
|
|
p = spats[0].pat;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
p = searchstr;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
|
2019-09-02 21:44:59 +02:00
|
|
|
|
if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
|
2019-05-04 21:08:40 +02:00
|
|
|
|
{
|
|
|
|
|
// Reserve enough space for the search pattern + offset +
|
2019-05-24 13:11:47 +02:00
|
|
|
|
// search stat. Use all the space available, so that the
|
|
|
|
|
// search state is right aligned. If there is not enough space
|
|
|
|
|
// msg_strtrunc() will shorten in the middle.
|
2019-09-03 22:23:38 +02:00
|
|
|
|
if (msg_scrolled != 0 && !cmd_silent)
|
2019-05-04 21:08:40 +02:00
|
|
|
|
// Use all the columns.
|
|
|
|
|
len = (int)(Rows - msg_row) * Columns - 1;
|
|
|
|
|
else
|
|
|
|
|
// Use up to 'showcmd' column.
|
|
|
|
|
len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
|
2019-05-24 13:11:47 +02:00
|
|
|
|
if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
|
|
|
|
|
len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// Reserve enough space for the search pattern + offset.
|
2019-05-24 13:11:47 +02:00
|
|
|
|
len = STRLEN(p) + off_len + 3;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
|
2020-04-11 21:31:28 +02:00
|
|
|
|
vim_free(msgbuf);
|
2019-05-25 20:21:28 +02:00
|
|
|
|
msgbuf = alloc(len);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (msgbuf != NULL)
|
|
|
|
|
{
|
2019-05-04 21:08:40 +02:00
|
|
|
|
vim_memset(msgbuf, ' ', len);
|
|
|
|
|
msgbuf[len - 1] = NUL;
|
2019-09-02 21:44:59 +02:00
|
|
|
|
// do not fill the msgbuf buffer, if cmd_silent is set, leave it
|
|
|
|
|
// empty for the search_stat feature.
|
|
|
|
|
if (!cmd_silent)
|
2019-05-04 21:08:40 +02:00
|
|
|
|
{
|
2019-09-02 21:44:59 +02:00
|
|
|
|
msgbuf[0] = dirc;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-09-02 21:44:59 +02:00
|
|
|
|
if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
|
|
|
|
|
{
|
|
|
|
|
// Use a space to draw the composing char on.
|
|
|
|
|
msgbuf[1] = ' ';
|
|
|
|
|
mch_memmove(msgbuf + 2, p, STRLEN(p));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
mch_memmove(msgbuf + 1, p, STRLEN(p));
|
|
|
|
|
if (off_len > 0)
|
|
|
|
|
mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-09-02 21:44:59 +02:00
|
|
|
|
trunc = msg_strtrunc(msgbuf, TRUE);
|
|
|
|
|
if (trunc != NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-05-04 21:08:40 +02:00
|
|
|
|
vim_free(msgbuf);
|
2019-09-02 21:44:59 +02:00
|
|
|
|
msgbuf = trunc;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 21:44:59 +02:00
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
|
|
|
|
// The search pattern could be shown on the right in rightleft
|
|
|
|
|
// mode, but the 'ruler' and 'showcmd' area use it too, thus
|
|
|
|
|
// it would be blanked out again very soon. Show it on the
|
|
|
|
|
// left, but do reverse the text.
|
|
|
|
|
if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
|
|
|
|
|
{
|
|
|
|
|
char_u *r;
|
|
|
|
|
size_t pat_len;
|
|
|
|
|
|
|
|
|
|
r = reverse_text(msgbuf);
|
|
|
|
|
if (r != NULL)
|
|
|
|
|
{
|
|
|
|
|
vim_free(msgbuf);
|
|
|
|
|
msgbuf = r;
|
|
|
|
|
// move reversed text to beginning of buffer
|
|
|
|
|
while (*r != NUL && *r == ' ')
|
|
|
|
|
r++;
|
|
|
|
|
pat_len = msgbuf + STRLEN(msgbuf) - r;
|
|
|
|
|
mch_memmove(msgbuf, r, pat_len);
|
|
|
|
|
// overwrite old text
|
|
|
|
|
if ((size_t)(r - msgbuf) >= pat_len)
|
|
|
|
|
vim_memset(r, ' ', pat_len);
|
|
|
|
|
else
|
|
|
|
|
vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
msg_outtrans(msgbuf);
|
|
|
|
|
msg_clr_eos();
|
|
|
|
|
msg_check();
|
|
|
|
|
|
|
|
|
|
gotocmdline(FALSE);
|
|
|
|
|
out_flush();
|
|
|
|
|
msg_nowait = TRUE; // don't wait for this message
|
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If there is a character offset, subtract it from the current
|
|
|
|
|
* position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
|
2004-06-16 11:19:22 +00:00
|
|
|
|
* Skip this if pos.col is near MAXCOL (closed fold).
|
2004-06-13 20:20:40 +00:00
|
|
|
|
* This is not done for a line offset, because then we would not be vi
|
|
|
|
|
* compatible.
|
|
|
|
|
*/
|
2004-06-16 11:19:22 +00:00
|
|
|
|
if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (spats[0].off.off > 0)
|
|
|
|
|
{
|
|
|
|
|
for (c = spats[0].off.off; c; --c)
|
|
|
|
|
if (decl(&pos) == -1)
|
|
|
|
|
break;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (c) // at start of buffer
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
pos.lnum = 0; // allow lnum == 0 here
|
2004-06-13 20:20:40 +00:00
|
|
|
|
pos.col = MAXCOL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (c = spats[0].off.off; c; ++c)
|
|
|
|
|
if (incl(&pos) == -1)
|
|
|
|
|
break;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (c) // at end of buffer
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
pos.lnum = curbuf->b_ml.ml_line_count + 1;
|
|
|
|
|
pos.col = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-16 15:10:30 +01:00
|
|
|
|
c = searchit(curwin, curbuf, &pos, NULL,
|
|
|
|
|
dirc == '/' ? FORWARD : BACKWARD,
|
2004-06-13 20:20:40 +00:00
|
|
|
|
searchstr, count, spats[0].off.end + (options &
|
|
|
|
|
(SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
|
|
|
|
|
+ SEARCH_MSG + SEARCH_START
|
|
|
|
|
+ ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
|
2019-10-18 20:53:34 +02:00
|
|
|
|
RE_LAST, sia);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
if (dircp != NULL)
|
2020-02-21 21:30:52 +01:00
|
|
|
|
*dircp = search_delim; // restore second '/' or '?' for normal_cmd()
|
2019-05-04 21:08:40 +02:00
|
|
|
|
|
|
|
|
|
if (!shortmess(SHM_SEARCH)
|
|
|
|
|
&& ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
|
|
|
|
|
|| (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
|
2019-05-06 21:37:18 +02:00
|
|
|
|
show_top_bot_msg = TRUE;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (c == FAIL)
|
|
|
|
|
{
|
|
|
|
|
retval = 0;
|
|
|
|
|
goto end_do_search;
|
|
|
|
|
}
|
|
|
|
|
if (spats[0].off.end && oap != NULL)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
oap->inclusive = TRUE; // 'e' includes last character
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
retval = 1; // pattern found
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Add character and/or line offset
|
|
|
|
|
*/
|
2006-08-29 15:58:12 +00:00
|
|
|
|
if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-05-24 22:08:15 +02:00
|
|
|
|
pos_T org_pos = pos;
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (spats[0].off.line) // Add the offset to the line number.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
c = pos.lnum + spats[0].off.off;
|
|
|
|
|
if (c < 1)
|
|
|
|
|
pos.lnum = 1;
|
|
|
|
|
else if (c > curbuf->b_ml.ml_line_count)
|
|
|
|
|
pos.lnum = curbuf->b_ml.ml_line_count;
|
|
|
|
|
else
|
|
|
|
|
pos.lnum = c;
|
|
|
|
|
pos.col = 0;
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
retval = 2; // pattern found, line offset added
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else if (pos.col < MAXCOL - 2) // just in case
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// to the right, check for end of file
|
2008-06-24 22:58:06 +00:00
|
|
|
|
c = spats[0].off.off;
|
|
|
|
|
if (c > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2008-06-24 22:58:06 +00:00
|
|
|
|
while (c-- > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (incl(&pos) == -1)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// to the left, check for start of file
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2008-06-24 22:58:06 +00:00
|
|
|
|
while (c++ < 0)
|
|
|
|
|
if (decl(&pos) == -1)
|
|
|
|
|
break;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-24 22:08:15 +02:00
|
|
|
|
if (!EQUAL_POS(pos, org_pos))
|
|
|
|
|
has_offset = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 21:08:40 +02:00
|
|
|
|
// Show [1/15] if 'S' is not in 'shortmess'.
|
|
|
|
|
if ((options & SEARCH_ECHO)
|
|
|
|
|
&& messaging()
|
2019-09-02 21:44:59 +02:00
|
|
|
|
&& !msg_silent
|
2019-05-04 21:08:40 +02:00
|
|
|
|
&& c != FAIL
|
|
|
|
|
&& !shortmess(SHM_SEARCHCOUNT)
|
|
|
|
|
&& msgbuf != NULL)
|
2020-06-01 17:28:35 +02:00
|
|
|
|
cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
|
|
|
|
|
show_top_bot_msg, msgbuf,
|
|
|
|
|
(count != 1 || has_offset
|
2020-05-29 22:49:43 +02:00
|
|
|
|
#ifdef FEAT_FOLDING
|
2020-06-01 17:28:35 +02:00
|
|
|
|
|| (!(fdo_flags & FDO_SEARCH)
|
|
|
|
|
&& hasFolding(curwin->w_cursor.lnum,
|
|
|
|
|
NULL, NULL))
|
2020-05-29 22:49:43 +02:00
|
|
|
|
#endif
|
2020-06-01 17:28:35 +02:00
|
|
|
|
),
|
|
|
|
|
SEARCH_STAT_DEF_MAX_COUNT,
|
|
|
|
|
SEARCH_STAT_DEF_TIMEOUT);
|
2019-05-04 21:08:40 +02:00
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* The search command can be followed by a ';' to do another search.
|
|
|
|
|
* For example: "/pat/;/foo/+3;?bar"
|
|
|
|
|
* This is like doing another search command, except:
|
|
|
|
|
* - The remembered direction '/' or '?' is from the first search.
|
|
|
|
|
* - When an error happens the cursor isn't moved at all.
|
|
|
|
|
* Don't do this when called by get_address() (it handles ';' itself).
|
|
|
|
|
*/
|
|
|
|
|
if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
dirc = *++pat;
|
2020-02-21 21:30:52 +01:00
|
|
|
|
search_delim = dirc;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (dirc != '?' && dirc != '/')
|
|
|
|
|
{
|
|
|
|
|
retval = 0;
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_("E386: Expected '?' or '/' after ';'"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
goto end_do_search;
|
|
|
|
|
}
|
|
|
|
|
++pat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options & SEARCH_MARK)
|
|
|
|
|
setpcmark();
|
|
|
|
|
curwin->w_cursor = pos;
|
|
|
|
|
curwin->w_set_curswant = TRUE;
|
|
|
|
|
|
|
|
|
|
end_do_search:
|
2020-10-24 20:49:43 +02:00
|
|
|
|
if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
spats[0].off = old_off;
|
|
|
|
|
vim_free(strcopy);
|
2019-05-04 21:08:40 +02:00
|
|
|
|
vim_free(msgbuf);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* search_for_exact_line(buf, pos, dir, pat)
|
|
|
|
|
*
|
|
|
|
|
* Search for a line starting with the given pattern (ignoring leading
|
2017-06-05 16:01:59 +02:00
|
|
|
|
* white-space), starting from pos and going in direction "dir". "pos" will
|
2004-06-13 20:20:40 +00:00
|
|
|
|
* contain the position of the match found. Blank lines match only if
|
2017-06-05 16:01:59 +02:00
|
|
|
|
* ADDING is set. If p_ic is set then the pattern must be in lowercase.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
* Return OK for success, or FAIL if no line found.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
search_for_exact_line(
|
|
|
|
|
buf_T *buf,
|
|
|
|
|
pos_T *pos,
|
|
|
|
|
int dir,
|
|
|
|
|
char_u *pat)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
linenr_T start = 0;
|
|
|
|
|
char_u *ptr;
|
|
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
|
|
if (buf->b_ml.ml_line_count == 0)
|
|
|
|
|
return FAIL;
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
pos->lnum += dir;
|
|
|
|
|
if (pos->lnum < 1)
|
|
|
|
|
{
|
|
|
|
|
if (p_ws)
|
|
|
|
|
{
|
|
|
|
|
pos->lnum = buf->b_ml.ml_line_count;
|
|
|
|
|
if (!shortmess(SHM_SEARCH))
|
|
|
|
|
give_warning((char_u *)_(top_bot_msg), TRUE);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pos->lnum = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (pos->lnum > buf->b_ml.ml_line_count)
|
|
|
|
|
{
|
|
|
|
|
if (p_ws)
|
|
|
|
|
{
|
|
|
|
|
pos->lnum = 1;
|
|
|
|
|
if (!shortmess(SHM_SEARCH))
|
|
|
|
|
give_warning((char_u *)_(bot_top_msg), TRUE);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pos->lnum = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pos->lnum == start)
|
|
|
|
|
break;
|
|
|
|
|
if (start == 0)
|
|
|
|
|
start = pos->lnum;
|
|
|
|
|
ptr = ml_get_buf(buf, pos->lnum, FALSE);
|
|
|
|
|
p = skipwhite(ptr);
|
|
|
|
|
pos->col = (colnr_T) (p - ptr);
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// when adding lines the matching line may be empty but it is not
|
|
|
|
|
// ignored because we are interested in the next line -- Acevedo
|
2005-07-29 22:36:03 +00:00
|
|
|
|
if ((compl_cont_status & CONT_ADDING)
|
|
|
|
|
&& !(compl_cont_status & CONT_SOL))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else if (*p != NUL) // ignore empty lines
|
|
|
|
|
{ // expanding lines or words
|
2005-07-29 22:36:03 +00:00
|
|
|
|
if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
|
|
|
|
|
: STRNCMP(p, pat, compl_length)) == 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Character Searches
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Search for a character in a line. If "t_cmd" is FALSE, move to the
|
|
|
|
|
* position of the character, otherwise move to just before the char.
|
|
|
|
|
* Do this "cap->count1" times.
|
|
|
|
|
* Return FAIL or OK.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
searchc(cmdarg_T *cap, int t_cmd)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int c = cap->nchar; // char to search for
|
|
|
|
|
int dir = cap->arg; // TRUE for searching forward
|
|
|
|
|
long count = cap->count1; // repeat count
|
2004-06-13 20:20:40 +00:00
|
|
|
|
int col;
|
|
|
|
|
char_u *p;
|
|
|
|
|
int len;
|
2011-06-26 05:36:34 +02:00
|
|
|
|
int stop = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (c != NUL) // normal search: remember args for repeat
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (!KeyStuffed) // don't remember when redoing
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2015-08-11 14:26:19 +02:00
|
|
|
|
*lastc = c;
|
|
|
|
|
set_csearch_direction(dir);
|
|
|
|
|
set_csearch_until(t_cmd);
|
|
|
|
|
lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (cap->ncharC1 != 0)
|
|
|
|
|
{
|
2015-08-11 14:26:19 +02:00
|
|
|
|
lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
|
|
|
|
|
lastc_bytes + lastc_bytelen);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (cap->ncharC2 != 0)
|
2015-08-11 14:26:19 +02:00
|
|
|
|
lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
|
|
|
|
|
lastc_bytes + lastc_bytelen);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else // repeat previous search
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-01-24 17:18:42 +01:00
|
|
|
|
if (*lastc == NUL && lastc_bytelen == 1)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return FAIL;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (dir) // repeat in opposite direction
|
2004-06-13 20:20:40 +00:00
|
|
|
|
dir = -lastcdir;
|
|
|
|
|
else
|
|
|
|
|
dir = lastcdir;
|
|
|
|
|
t_cmd = last_t_cmd;
|
2015-08-11 14:26:19 +02:00
|
|
|
|
c = *lastc;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
|
2011-06-26 05:36:34 +02:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Force a move of at least one char, so ";" and "," will move the
|
|
|
|
|
// cursor, even if the cursor is right in front of char we are looking
|
|
|
|
|
// at.
|
2011-07-15 13:21:30 +02:00
|
|
|
|
if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
|
2011-06-26 05:36:34 +02:00
|
|
|
|
stop = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-09-16 21:55:43 +00:00
|
|
|
|
if (dir == BACKWARD)
|
|
|
|
|
cap->oap->inclusive = FALSE;
|
|
|
|
|
else
|
|
|
|
|
cap->oap->inclusive = TRUE;
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
p = ml_get_curline();
|
|
|
|
|
col = curwin->w_cursor.col;
|
|
|
|
|
len = (int)STRLEN(p);
|
|
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
{
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if (dir > 0)
|
|
|
|
|
{
|
2005-08-10 21:07:57 +00:00
|
|
|
|
col += (*mb_ptr2len)(p + col);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (col >= len)
|
|
|
|
|
return FAIL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (col == 0)
|
|
|
|
|
return FAIL;
|
|
|
|
|
col -= (*mb_head_off)(p, p + col - 1) + 1;
|
|
|
|
|
}
|
2015-08-11 14:26:19 +02:00
|
|
|
|
if (lastc_bytelen == 1)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2011-06-26 05:36:34 +02:00
|
|
|
|
if (p[col] == c && stop)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2017-03-01 22:17:05 +01:00
|
|
|
|
else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
|
2016-12-01 17:25:20 +01:00
|
|
|
|
&& stop)
|
2017-03-01 22:17:05 +01:00
|
|
|
|
break;
|
2011-06-26 05:36:34 +02:00
|
|
|
|
stop = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if ((col += dir) < 0 || col >= len)
|
|
|
|
|
return FAIL;
|
2011-06-26 05:36:34 +02:00
|
|
|
|
if (p[col] == c && stop)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
2011-06-26 05:36:34 +02:00
|
|
|
|
stop = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (t_cmd)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// backup to before the character (possibly double-byte)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
col -= dir;
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
{
|
|
|
|
|
if (dir < 0)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Landed on the search char which is lastc_bytelen long
|
2015-08-11 14:26:19 +02:00
|
|
|
|
col += lastc_bytelen - 1;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// To previous char, which may be multi-byte.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
col -= (*mb_head_off)(p, p + col);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
curwin->w_cursor.col = col;
|
|
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* "Other" Searches
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* findmatch - find the matching paren or brace
|
|
|
|
|
*
|
|
|
|
|
* Improvement over vi: Braces inside quotes are ignored.
|
|
|
|
|
*/
|
|
|
|
|
pos_T *
|
2016-01-30 21:10:09 +01:00
|
|
|
|
findmatch(oparg_T *oap, int initc)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
return findmatchlimit(oap, initc, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return TRUE if the character before "linep[col]" equals "ch".
|
|
|
|
|
* Return FALSE if "col" is zero.
|
|
|
|
|
* Update "*prevcol" to the column of the previous character, unless "prevcol"
|
|
|
|
|
* is NULL.
|
|
|
|
|
* Handles multibyte string correctly.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
check_prevcol(
|
|
|
|
|
char_u *linep,
|
|
|
|
|
int col,
|
|
|
|
|
int ch,
|
|
|
|
|
int *prevcol)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
--col;
|
|
|
|
|
if (col > 0 && has_mbyte)
|
|
|
|
|
col -= (*mb_head_off)(linep, linep + col);
|
|
|
|
|
if (prevcol)
|
|
|
|
|
*prevcol = col;
|
|
|
|
|
return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 21:17:36 +02:00
|
|
|
|
/*
|
|
|
|
|
* Raw string start is found at linep[startpos.col - 1].
|
|
|
|
|
* Return TRUE if the matching end can be found between startpos and endpos.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
|
2015-07-28 21:17:36 +02:00
|
|
|
|
{
|
|
|
|
|
char_u *p;
|
|
|
|
|
char_u *delim_copy;
|
|
|
|
|
size_t delim_len;
|
|
|
|
|
linenr_T lnum;
|
|
|
|
|
int found = FALSE;
|
|
|
|
|
|
|
|
|
|
for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
|
|
|
|
|
;
|
|
|
|
|
delim_len = (p - linep) - startpos->col - 1;
|
2020-06-12 22:59:11 +02:00
|
|
|
|
delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
|
2015-07-28 21:17:36 +02:00
|
|
|
|
if (delim_copy == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
|
|
|
|
|
{
|
|
|
|
|
char_u *line = ml_get(lnum);
|
|
|
|
|
|
|
|
|
|
for (p = line + (lnum == startpos->lnum
|
|
|
|
|
? startpos->col + 1 : 0); *p; ++p)
|
|
|
|
|
{
|
|
|
|
|
if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
|
|
|
|
|
break;
|
2020-08-04 21:46:18 +02:00
|
|
|
|
if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
|
|
|
|
|
&& p[delim_len + 1] == '"')
|
2015-07-28 21:17:36 +02:00
|
|
|
|
{
|
|
|
|
|
found = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (found)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
vim_free(delim_copy);
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 22:27:22 +01:00
|
|
|
|
/*
|
|
|
|
|
* Check matchpairs option for "*initc".
|
|
|
|
|
* If there is a match set "*initc" to the matching character and "*findc" to
|
|
|
|
|
* the opposite character. Set "*backwards" to the direction.
|
|
|
|
|
* When "switchit" is TRUE swap the direction.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
find_mps_values(
|
|
|
|
|
int *initc,
|
|
|
|
|
int *findc,
|
|
|
|
|
int *backwards,
|
|
|
|
|
int switchit)
|
|
|
|
|
{
|
|
|
|
|
char_u *ptr;
|
|
|
|
|
|
|
|
|
|
ptr = curbuf->b_p_mps;
|
|
|
|
|
while (*ptr != NUL)
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
{
|
|
|
|
|
char_u *prev;
|
|
|
|
|
|
|
|
|
|
if (mb_ptr2char(ptr) == *initc)
|
|
|
|
|
{
|
|
|
|
|
if (switchit)
|
|
|
|
|
{
|
|
|
|
|
*findc = *initc;
|
|
|
|
|
*initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
|
|
|
|
|
*backwards = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
|
|
|
|
|
*backwards = FALSE;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
prev = ptr;
|
|
|
|
|
ptr += mb_ptr2len(ptr) + 1;
|
|
|
|
|
if (mb_ptr2char(ptr) == *initc)
|
|
|
|
|
{
|
|
|
|
|
if (switchit)
|
|
|
|
|
{
|
|
|
|
|
*findc = *initc;
|
|
|
|
|
*initc = mb_ptr2char(prev);
|
|
|
|
|
*backwards = FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*findc = mb_ptr2char(prev);
|
|
|
|
|
*backwards = TRUE;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ptr += mb_ptr2len(ptr);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (*ptr == *initc)
|
|
|
|
|
{
|
|
|
|
|
if (switchit)
|
|
|
|
|
{
|
|
|
|
|
*backwards = TRUE;
|
|
|
|
|
*findc = *initc;
|
|
|
|
|
*initc = ptr[2];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*backwards = FALSE;
|
|
|
|
|
*findc = ptr[2];
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ptr += 2;
|
|
|
|
|
if (*ptr == *initc)
|
|
|
|
|
{
|
|
|
|
|
if (switchit)
|
|
|
|
|
{
|
|
|
|
|
*backwards = FALSE;
|
|
|
|
|
*findc = *initc;
|
|
|
|
|
*initc = ptr[-2];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*backwards = TRUE;
|
|
|
|
|
*findc = ptr[-2];
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
++ptr;
|
|
|
|
|
}
|
|
|
|
|
if (*ptr == ',')
|
|
|
|
|
++ptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* findmatchlimit -- find the matching paren or brace, if it exists within
|
2015-07-28 21:17:36 +02:00
|
|
|
|
* maxtravel lines of the cursor. A maxtravel of 0 means search until falling
|
|
|
|
|
* off the edge of the file.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*
|
|
|
|
|
* "initc" is the character to find a match for. NUL means to find the
|
2015-07-28 21:17:36 +02:00
|
|
|
|
* character at or after the cursor. Special values:
|
|
|
|
|
* '*' look for C-style comment / *
|
|
|
|
|
* '/' look for C-style comment / *, ignoring comment-end
|
|
|
|
|
* '#' look for preprocessor directives
|
|
|
|
|
* 'R' look for raw string start: R"delim(text)delim" (only backwards)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*
|
|
|
|
|
* flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
|
|
|
|
|
* FM_FORWARD search forwards (when initc is '/', '*' or '#')
|
|
|
|
|
* FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
|
|
|
|
|
* FM_SKIPCOMM skip comments (not implemented yet!)
|
2005-09-13 21:20:47 +00:00
|
|
|
|
*
|
2015-07-28 21:17:36 +02:00
|
|
|
|
* "oap" is only used to set oap->motion_type for a linewise motion, it can be
|
2005-09-13 21:20:47 +00:00
|
|
|
|
* NULL
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
pos_T *
|
2016-01-30 21:10:09 +01:00
|
|
|
|
findmatchlimit(
|
|
|
|
|
oparg_T *oap,
|
|
|
|
|
int initc,
|
|
|
|
|
int flags,
|
|
|
|
|
int maxtravel)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
static pos_T pos; // current search position
|
|
|
|
|
int findc = 0; // matching brace
|
2004-06-13 20:20:40 +00:00
|
|
|
|
int c;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int count = 0; // cumulative number of braces
|
|
|
|
|
int backwards = FALSE; // init for gcc
|
|
|
|
|
int raw_string = FALSE; // search for raw string
|
|
|
|
|
int inquote = FALSE; // TRUE when inside quotes
|
|
|
|
|
char_u *linep; // pointer to current line
|
2004-06-13 20:20:40 +00:00
|
|
|
|
char_u *ptr;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int do_quotes; // check for quotes in current line
|
|
|
|
|
int at_start; // do_quotes value at start position
|
|
|
|
|
int hash_dir = 0; // Direction searched for # things
|
|
|
|
|
int comment_dir = 0; // Direction searched for comments
|
|
|
|
|
pos_T match_pos; // Where last slash-star was found
|
|
|
|
|
int start_in_quotes; // start position is in quotes
|
|
|
|
|
int traveled = 0; // how far we've searched so far
|
|
|
|
|
int ignore_cend = FALSE; // ignore comment end
|
|
|
|
|
int cpo_match; // vi compatible matching
|
|
|
|
|
int cpo_bsl; // don't recognize backslashes
|
|
|
|
|
int match_escaped = 0; // search for escaped match
|
|
|
|
|
int dir; // Direction to search
|
|
|
|
|
int comment_col = MAXCOL; // start of / / comment
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int lispcomm = FALSE; // inside of Lisp-style comment
|
|
|
|
|
int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
pos = curwin->w_cursor;
|
2013-08-14 17:45:29 +02:00
|
|
|
|
pos.coladd = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
linep = ml_get(pos.lnum);
|
|
|
|
|
|
|
|
|
|
cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
|
|
|
|
|
cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Direction to search when initc is '/', '*' or '#'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (flags & FM_BACKWARD)
|
|
|
|
|
dir = BACKWARD;
|
|
|
|
|
else if (flags & FM_FORWARD)
|
|
|
|
|
dir = FORWARD;
|
|
|
|
|
else
|
|
|
|
|
dir = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* if initc given, look in the table for the matching character
|
|
|
|
|
* '/' and '*' are special cases: look for start or end of comment.
|
|
|
|
|
* When '/' is used, we ignore running backwards into an star-slash, for
|
|
|
|
|
* "[*" command, we just want to find any comment.
|
|
|
|
|
*/
|
2015-07-28 21:17:36 +02:00
|
|
|
|
if (initc == '/' || initc == '*' || initc == 'R')
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
comment_dir = dir;
|
|
|
|
|
if (initc == '/')
|
|
|
|
|
ignore_cend = TRUE;
|
|
|
|
|
backwards = (dir == FORWARD) ? FALSE : TRUE;
|
2015-07-28 21:17:36 +02:00
|
|
|
|
raw_string = (initc == 'R');
|
2004-06-13 20:20:40 +00:00
|
|
|
|
initc = NUL;
|
|
|
|
|
}
|
|
|
|
|
else if (initc != '#' && initc != NUL)
|
|
|
|
|
{
|
2013-01-17 17:02:05 +01:00
|
|
|
|
find_mps_values(&initc, &findc, &backwards, TRUE);
|
|
|
|
|
if (findc == NUL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-07-28 21:17:36 +02:00
|
|
|
|
/*
|
|
|
|
|
* Either initc is '#', or no initc was given and we need to look
|
|
|
|
|
* under the cursor.
|
|
|
|
|
*/
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (initc == '#')
|
|
|
|
|
{
|
|
|
|
|
hash_dir = dir;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* initc was not given, must look for something to match under
|
|
|
|
|
* or near the cursor.
|
|
|
|
|
* Only check for special things when 'cpo' doesn't have '%'.
|
|
|
|
|
*/
|
|
|
|
|
if (!cpo_match)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Are we before or at #if, #else etc.?
|
2004-06-13 20:20:40 +00:00
|
|
|
|
ptr = skipwhite(linep);
|
|
|
|
|
if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
|
|
|
|
|
{
|
|
|
|
|
ptr = skipwhite(ptr + 1);
|
|
|
|
|
if ( STRNCMP(ptr, "if", 2) == 0
|
|
|
|
|
|| STRNCMP(ptr, "endif", 5) == 0
|
|
|
|
|
|| STRNCMP(ptr, "el", 2) == 0)
|
|
|
|
|
hash_dir = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Are we on a comment?
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if (linep[pos.col] == '/')
|
|
|
|
|
{
|
|
|
|
|
if (linep[pos.col + 1] == '*')
|
|
|
|
|
{
|
|
|
|
|
comment_dir = FORWARD;
|
|
|
|
|
backwards = FALSE;
|
|
|
|
|
pos.col++;
|
|
|
|
|
}
|
|
|
|
|
else if (pos.col > 0 && linep[pos.col - 1] == '*')
|
|
|
|
|
{
|
|
|
|
|
comment_dir = BACKWARD;
|
|
|
|
|
backwards = TRUE;
|
|
|
|
|
pos.col--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (linep[pos.col] == '*')
|
|
|
|
|
{
|
|
|
|
|
if (linep[pos.col + 1] == '/')
|
|
|
|
|
{
|
|
|
|
|
comment_dir = BACKWARD;
|
|
|
|
|
backwards = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (pos.col > 0 && linep[pos.col - 1] == '/')
|
|
|
|
|
{
|
|
|
|
|
comment_dir = FORWARD;
|
|
|
|
|
backwards = FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If we are not on a comment or the # at the start of a line, then
|
|
|
|
|
* look for brace anywhere on this line after the cursor.
|
|
|
|
|
*/
|
|
|
|
|
if (!hash_dir && !comment_dir)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Find the brace under or after the cursor.
|
|
|
|
|
* If beyond the end of the line, use the last character in
|
|
|
|
|
* the line.
|
|
|
|
|
*/
|
|
|
|
|
if (linep[pos.col] == NUL && pos.col)
|
|
|
|
|
--pos.col;
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
2013-01-17 17:02:05 +01:00
|
|
|
|
initc = PTR2CHAR(linep + pos.col);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (initc == NUL)
|
|
|
|
|
break;
|
|
|
|
|
|
2013-01-17 17:02:05 +01:00
|
|
|
|
find_mps_values(&initc, &findc, &backwards, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (findc)
|
|
|
|
|
break;
|
2019-10-06 22:00:13 +02:00
|
|
|
|
pos.col += mb_ptr2len(linep + pos.col);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (!findc)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// no brace in the line, maybe use " #if" then
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (!cpo_match && *skipwhite(linep) == '#')
|
|
|
|
|
hash_dir = 1;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else if (!cpo_bsl)
|
|
|
|
|
{
|
|
|
|
|
int col, bslcnt = 0;
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Set "match_escaped" if there are an odd number of
|
|
|
|
|
// backslashes.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
|
|
|
|
|
bslcnt++;
|
|
|
|
|
match_escaped = (bslcnt & 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (hash_dir)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Look for matching #if, #else, #elif, or #endif
|
|
|
|
|
*/
|
|
|
|
|
if (oap != NULL)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
oap->motion_type = MLINE; // Linewise for this case only
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (initc != '#')
|
|
|
|
|
{
|
|
|
|
|
ptr = skipwhite(skipwhite(linep) + 1);
|
|
|
|
|
if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
|
|
|
|
|
hash_dir = 1;
|
|
|
|
|
else if (STRNCMP(ptr, "endif", 5) == 0)
|
|
|
|
|
hash_dir = -1;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
pos.col = 0;
|
|
|
|
|
while (!got_int)
|
|
|
|
|
{
|
|
|
|
|
if (hash_dir > 0)
|
|
|
|
|
{
|
|
|
|
|
if (pos.lnum == curbuf->b_ml.ml_line_count)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (pos.lnum == 1)
|
|
|
|
|
break;
|
|
|
|
|
pos.lnum += hash_dir;
|
|
|
|
|
linep = ml_get(pos.lnum);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
line_breakcheck(); // check for CTRL-C typed
|
2004-06-13 20:20:40 +00:00
|
|
|
|
ptr = skipwhite(linep);
|
|
|
|
|
if (*ptr != '#')
|
|
|
|
|
continue;
|
|
|
|
|
pos.col = (colnr_T) (ptr - linep);
|
|
|
|
|
ptr = skipwhite(ptr + 1);
|
|
|
|
|
if (hash_dir > 0)
|
|
|
|
|
{
|
|
|
|
|
if (STRNCMP(ptr, "if", 2) == 0)
|
|
|
|
|
count++;
|
|
|
|
|
else if (STRNCMP(ptr, "el", 2) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (count == 0)
|
|
|
|
|
return &pos;
|
|
|
|
|
}
|
|
|
|
|
else if (STRNCMP(ptr, "endif", 5) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (count == 0)
|
|
|
|
|
return &pos;
|
|
|
|
|
count--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (STRNCMP(ptr, "if", 2) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (count == 0)
|
|
|
|
|
return &pos;
|
|
|
|
|
count--;
|
|
|
|
|
}
|
|
|
|
|
else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
|
|
|
|
|
{
|
|
|
|
|
if (count == 0)
|
|
|
|
|
return &pos;
|
|
|
|
|
}
|
|
|
|
|
else if (STRNCMP(ptr, "endif", 5) == 0)
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// This is just guessing: when 'rightleft' is set, search for a matching
|
|
|
|
|
// paren/brace in the other direction.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
|
|
|
|
|
backwards = !backwards;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
do_quotes = -1;
|
|
|
|
|
start_in_quotes = MAYBE;
|
2017-03-12 18:23:53 +01:00
|
|
|
|
CLEAR_POS(&match_pos);
|
2006-03-01 22:09:21 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// backward search: Check if this line contains a single-line comment
|
2004-07-05 15:58:32 +00:00
|
|
|
|
if ((backwards && comment_dir)
|
|
|
|
|
#ifdef FEAT_LISP
|
|
|
|
|
|| lisp
|
|
|
|
|
#endif
|
|
|
|
|
)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
comment_col = check_linecomment(linep);
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
|
|
|
|
if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
lispcomm = TRUE; // find match inside this comment
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
while (!got_int)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Go to the next position, forward or backward. We could use
|
|
|
|
|
* inc() and dec() here, but that is much slower
|
|
|
|
|
*/
|
|
|
|
|
if (backwards)
|
|
|
|
|
{
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// char to match is inside of comment, don't search outside
|
2004-07-05 15:58:32 +00:00
|
|
|
|
if (lispcomm && pos.col < (colnr_T)comment_col)
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (pos.col == 0) // at start of line, go to prev. one
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (pos.lnum == 1) // start of file
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
--pos.lnum;
|
|
|
|
|
|
2006-04-14 20:42:25 +00:00
|
|
|
|
if (maxtravel > 0 && ++traveled > maxtravel)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
linep = ml_get(pos.lnum);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
|
2004-06-13 20:20:40 +00:00
|
|
|
|
do_quotes = -1;
|
|
|
|
|
line_breakcheck();
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Check if this line contains a single-line comment
|
2004-07-05 15:58:32 +00:00
|
|
|
|
if (comment_dir
|
|
|
|
|
#ifdef FEAT_LISP
|
|
|
|
|
|| lisp
|
|
|
|
|
#endif
|
|
|
|
|
)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
comment_col = check_linecomment(linep);
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// skip comment
|
2004-07-05 15:58:32 +00:00
|
|
|
|
if (lisp && comment_col != MAXCOL)
|
|
|
|
|
pos.col = comment_col;
|
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
--pos.col;
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
pos.col -= (*mb_head_off)(linep, linep + pos.col);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else // forward search
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2004-07-05 15:58:32 +00:00
|
|
|
|
if (linep[pos.col] == NUL
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// at end of line, go to next one
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// don't search for match in comment
|
2004-07-05 15:58:32 +00:00
|
|
|
|
|| (lisp && comment_col != MAXCOL
|
|
|
|
|
&& pos.col == (colnr_T)comment_col)
|
|
|
|
|
#endif
|
|
|
|
|
)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// line is exhausted and comment with it,
|
|
|
|
|
// don't search for match in code
|
2004-07-05 15:58:32 +00:00
|
|
|
|
|| lispcomm
|
|
|
|
|
#endif
|
|
|
|
|
)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
++pos.lnum;
|
|
|
|
|
|
|
|
|
|
if (maxtravel && traveled++ > maxtravel)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
linep = ml_get(pos.lnum);
|
|
|
|
|
pos.col = 0;
|
|
|
|
|
do_quotes = -1;
|
|
|
|
|
line_breakcheck();
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (lisp) // find comment pos in new line
|
2004-07-05 15:58:32 +00:00
|
|
|
|
comment_col = check_linecomment(linep);
|
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
2005-08-10 21:07:57 +00:00
|
|
|
|
pos.col += (*mb_ptr2len)(linep + pos.col);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
++pos.col;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
|
|
|
|
|
*/
|
|
|
|
|
if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
|
|
|
|
|
(linep[0] == '{' || linep[0] == '}'))
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (linep[0] == findc && count == 0) // match!
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return &pos;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
break; // out of scope
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (comment_dir)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Note: comments do not nest, and we ignore quotes in them
|
|
|
|
|
// TODO: ignore comment brackets inside strings
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (comment_dir == FORWARD)
|
|
|
|
|
{
|
|
|
|
|
if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
|
|
|
|
|
{
|
|
|
|
|
pos.col++;
|
|
|
|
|
return &pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else // Searching backwards
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* A comment may contain / * or / /, it may also start or end
|
2017-11-12 15:36:38 +01:00
|
|
|
|
* with / * /. Ignore a / * after / / and after *.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
|
|
|
|
if (pos.col == 0)
|
|
|
|
|
continue;
|
2015-07-28 21:17:36 +02:00
|
|
|
|
else if (raw_string)
|
|
|
|
|
{
|
|
|
|
|
if (linep[pos.col - 1] == 'R'
|
|
|
|
|
&& linep[pos.col] == '"'
|
|
|
|
|
&& vim_strchr(linep + pos.col + 1, '(') != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Possible start of raw string. Now that we have the
|
|
|
|
|
// delimiter we can check if it ends before where we
|
|
|
|
|
// started searching, or before the previously found
|
|
|
|
|
// raw string start.
|
2015-07-28 21:17:36 +02:00
|
|
|
|
if (!find_rawstring_end(linep, &pos,
|
|
|
|
|
count > 0 ? &match_pos : &curwin->w_cursor))
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
match_pos = pos;
|
|
|
|
|
match_pos.col--;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
linep = ml_get(pos.lnum); // may have been released
|
2015-07-28 21:17:36 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if ( linep[pos.col - 1] == '/'
|
|
|
|
|
&& linep[pos.col] == '*'
|
2017-11-12 15:36:38 +01:00
|
|
|
|
&& (pos.col == 1 || linep[pos.col - 2] != '*')
|
2004-06-13 20:20:40 +00:00
|
|
|
|
&& (int)pos.col < comment_col)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
match_pos = pos;
|
|
|
|
|
match_pos.col--;
|
|
|
|
|
}
|
|
|
|
|
else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
|
|
|
|
|
{
|
|
|
|
|
if (count > 0)
|
|
|
|
|
pos = match_pos;
|
|
|
|
|
else if (pos.col > 1 && linep[pos.col - 2] == '/'
|
|
|
|
|
&& (int)pos.col <= comment_col)
|
|
|
|
|
pos.col -= 2;
|
|
|
|
|
else if (ignore_cend)
|
|
|
|
|
continue;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
return &pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If smart matching ('cpoptions' does not contain '%'), braces inside
|
|
|
|
|
* of quotes are ignored, but only if there is an even number of
|
|
|
|
|
* quotes in the line.
|
|
|
|
|
*/
|
|
|
|
|
if (cpo_match)
|
|
|
|
|
do_quotes = 0;
|
|
|
|
|
else if (do_quotes == -1)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Count the number of quotes in the line, skipping \" and '"'.
|
|
|
|
|
* Watch out for "\\".
|
|
|
|
|
*/
|
|
|
|
|
at_start = do_quotes;
|
|
|
|
|
for (ptr = linep; *ptr; ++ptr)
|
|
|
|
|
{
|
|
|
|
|
if (ptr == linep + pos.col + backwards)
|
|
|
|
|
at_start = (do_quotes & 1);
|
|
|
|
|
if (*ptr == '"'
|
|
|
|
|
&& (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
|
|
|
|
|
++do_quotes;
|
|
|
|
|
if (*ptr == '\\' && ptr[1] != NUL)
|
|
|
|
|
++ptr;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
do_quotes &= 1; // result is 1 with even number of quotes
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If we find an uneven count, check current line and previous
|
|
|
|
|
* one for a '\' at the end.
|
|
|
|
|
*/
|
|
|
|
|
if (!do_quotes)
|
|
|
|
|
{
|
|
|
|
|
inquote = FALSE;
|
|
|
|
|
if (ptr[-1] == '\\')
|
|
|
|
|
{
|
|
|
|
|
do_quotes = 1;
|
|
|
|
|
if (start_in_quotes == MAYBE)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Do we need to use at_start here?
|
2004-06-13 20:20:40 +00:00
|
|
|
|
inquote = TRUE;
|
|
|
|
|
start_in_quotes = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (backwards)
|
|
|
|
|
inquote = TRUE;
|
|
|
|
|
}
|
|
|
|
|
if (pos.lnum > 1)
|
|
|
|
|
{
|
|
|
|
|
ptr = ml_get(pos.lnum - 1);
|
|
|
|
|
if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
|
|
|
|
|
{
|
|
|
|
|
do_quotes = 1;
|
|
|
|
|
if (start_in_quotes == MAYBE)
|
|
|
|
|
{
|
|
|
|
|
inquote = at_start;
|
|
|
|
|
if (inquote)
|
|
|
|
|
start_in_quotes = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (!backwards)
|
|
|
|
|
inquote = TRUE;
|
|
|
|
|
}
|
2007-07-10 11:09:36 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// ml_get() only keeps one line, need to get linep again
|
2007-07-10 11:09:36 +00:00
|
|
|
|
linep = ml_get(pos.lnum);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (start_in_quotes == MAYBE)
|
|
|
|
|
start_in_quotes = FALSE;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If 'smartmatch' is set:
|
|
|
|
|
* Things inside quotes are ignored by setting 'inquote'. If we
|
|
|
|
|
* find a quote without a preceding '\' invert 'inquote'. At the
|
|
|
|
|
* end of a line not ending in '\' we reset 'inquote'.
|
|
|
|
|
*
|
|
|
|
|
* In lines with an uneven number of quotes (without preceding '\')
|
|
|
|
|
* we do not know which part to ignore. Therefore we only set
|
|
|
|
|
* inquote if the number of quotes in a line is even, unless this
|
|
|
|
|
* line or the previous one ends in a '\'. Complicated, isn't it?
|
|
|
|
|
*/
|
2013-01-17 17:02:05 +01:00
|
|
|
|
c = PTR2CHAR(linep + pos.col);
|
|
|
|
|
switch (c)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
case NUL:
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// at end of line without trailing backslash, reset inquote
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (pos.col == 0 || linep[pos.col - 1] != '\\')
|
|
|
|
|
{
|
|
|
|
|
inquote = FALSE;
|
|
|
|
|
start_in_quotes = FALSE;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '"':
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// a quote that is preceded with an odd number of backslashes is
|
|
|
|
|
// ignored
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (do_quotes)
|
|
|
|
|
{
|
|
|
|
|
int col;
|
|
|
|
|
|
|
|
|
|
for (col = pos.col - 1; col >= 0; --col)
|
|
|
|
|
if (linep[col] != '\\')
|
|
|
|
|
break;
|
|
|
|
|
if ((((int)pos.col - 1 - col) & 1) == 0)
|
|
|
|
|
{
|
|
|
|
|
inquote = !inquote;
|
|
|
|
|
start_in_quotes = FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If smart matching ('cpoptions' does not contain '%'):
|
|
|
|
|
* Skip things in single quotes: 'x' or '\x'. Be careful for single
|
|
|
|
|
* single quotes, eg jon's. Things like '\233' or '\x3f' are not
|
|
|
|
|
* skipped, there is never a brace in them.
|
|
|
|
|
* Ignore this when finding matches for `'.
|
|
|
|
|
*/
|
|
|
|
|
case '\'':
|
|
|
|
|
if (!cpo_match && initc != '\'' && findc != '\'')
|
|
|
|
|
{
|
|
|
|
|
if (backwards)
|
|
|
|
|
{
|
|
|
|
|
if (pos.col > 1)
|
|
|
|
|
{
|
|
|
|
|
if (linep[pos.col - 2] == '\'')
|
|
|
|
|
{
|
|
|
|
|
pos.col -= 2;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (linep[pos.col - 2] == '\\' &&
|
|
|
|
|
pos.col > 2 && linep[pos.col - 3] == '\'')
|
|
|
|
|
{
|
|
|
|
|
pos.col -= 3;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
else if (linep[pos.col + 1]) // forward search
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (linep[pos.col + 1] == '\\' &&
|
|
|
|
|
linep[pos.col + 2] && linep[pos.col + 3] == '\'')
|
|
|
|
|
{
|
|
|
|
|
pos.col += 3;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (linep[pos.col + 2] == '\'')
|
|
|
|
|
{
|
|
|
|
|
pos.col += 2;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// FALLTHROUGH
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
#ifdef FEAT_LISP
|
2004-07-05 15:58:32 +00:00
|
|
|
|
/*
|
|
|
|
|
* For Lisp skip over backslashed (), {} and [].
|
|
|
|
|
* (actually, we skip #\( et al)
|
|
|
|
|
*/
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (curbuf->b_p_lisp
|
|
|
|
|
&& vim_strchr((char_u *)"(){}[]", c) != NULL
|
2004-07-05 15:58:32 +00:00
|
|
|
|
&& pos.col > 1
|
|
|
|
|
&& check_prevcol(linep, pos.col, '\\', NULL)
|
|
|
|
|
&& check_prevcol(linep, pos.col - 1, '#', NULL))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Check for match outside of quotes, and inside of
|
|
|
|
|
// quotes when the start is also inside of quotes.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if ((!inquote || start_in_quotes == TRUE)
|
|
|
|
|
&& (c == initc || c == findc))
|
|
|
|
|
{
|
|
|
|
|
int col, bslcnt = 0;
|
|
|
|
|
|
|
|
|
|
if (!cpo_bsl)
|
|
|
|
|
{
|
|
|
|
|
for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
|
|
|
|
|
bslcnt++;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Only accept a match when 'M' is in 'cpo' or when escaping
|
|
|
|
|
// is what we expect.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (cpo_bsl || (bslcnt & 1) == match_escaped)
|
|
|
|
|
{
|
|
|
|
|
if (c == initc)
|
|
|
|
|
count++;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (count == 0)
|
|
|
|
|
return &pos;
|
|
|
|
|
count--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (comment_dir == BACKWARD && count > 0)
|
|
|
|
|
{
|
|
|
|
|
pos = match_pos;
|
|
|
|
|
return &pos;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
return (pos_T *)NULL; // never found it
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check if line[] contains a / / comment.
|
|
|
|
|
* Return MAXCOL if not, otherwise return the column.
|
|
|
|
|
* TODO: skip strings.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
check_linecomment(char_u *line)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
|
|
p = line;
|
2004-07-05 15:58:32 +00:00
|
|
|
|
#ifdef FEAT_LISP
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// skip Lispish one-line comments
|
2004-07-05 15:58:32 +00:00
|
|
|
|
if (curbuf->b_p_lisp)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (vim_strchr(p, ';') != NULL) // there may be comments
|
2004-07-05 15:58:32 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int in_str = FALSE; // inside of string
|
2004-07-05 15:58:32 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
p = line; // scan from start
|
2005-06-16 21:59:56 +00:00
|
|
|
|
while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
|
2004-07-05 15:58:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (*p == '"')
|
|
|
|
|
{
|
2012-01-10 22:26:17 +01:00
|
|
|
|
if (in_str)
|
2004-07-05 15:58:32 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (*(p - 1) != '\\') // skip escaped quote
|
2012-01-10 22:26:17 +01:00
|
|
|
|
in_str = FALSE;
|
2004-07-05 15:58:32 +00:00
|
|
|
|
}
|
|
|
|
|
else if (p == line || ((p - line) >= 2
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// skip #\" form
|
2004-07-05 15:58:32 +00:00
|
|
|
|
&& *(p - 1) != '\\' && *(p - 2) != '#'))
|
2012-01-10 22:26:17 +01:00
|
|
|
|
in_str = TRUE;
|
2004-07-05 15:58:32 +00:00
|
|
|
|
}
|
2012-01-10 22:26:17 +01:00
|
|
|
|
else if (!in_str && ((p - line) < 2
|
2004-07-05 15:58:32 +00:00
|
|
|
|
|| (*(p - 1) != '\\' && *(p - 2) != '#')))
|
2019-12-05 21:10:38 +01:00
|
|
|
|
break; // found!
|
2004-07-05 15:58:32 +00:00
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
p = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
while ((p = vim_strchr(p, '/')) != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// accept a double /, unless it's preceded with * and followed by *,
|
|
|
|
|
// because * / / * is an end and start of a C comment
|
2008-01-01 14:43:35 +00:00
|
|
|
|
if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
|
return MAXCOL;
|
|
|
|
|
return (int)(p - line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Move cursor briefly to character matching the one under the cursor.
|
|
|
|
|
* Used for Insert mode and "r" command.
|
|
|
|
|
* Show the match only if it is visible on the screen.
|
|
|
|
|
* If there isn't a match, then beep.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
showmatch(
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int c) // char to show match for
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
pos_T *lpos, save_cursor;
|
|
|
|
|
pos_T mpos;
|
|
|
|
|
colnr_T vcol;
|
|
|
|
|
long save_so;
|
|
|
|
|
long save_siso;
|
|
|
|
|
#ifdef CURSOR_SHAPE
|
|
|
|
|
int save_state;
|
|
|
|
|
#endif
|
|
|
|
|
colnr_T save_dollar_vcol;
|
|
|
|
|
char_u *p;
|
2019-01-31 18:26:10 +01:00
|
|
|
|
long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
|
|
|
|
|
long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Only show match for chars in the 'matchpairs' option.
|
|
|
|
|
*/
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// 'matchpairs' is "x:y,x:y"
|
2013-01-17 17:02:05 +01:00
|
|
|
|
for (p = curbuf->b_p_mps; *p != NUL; ++p)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
2013-02-20 18:39:13 +01:00
|
|
|
|
if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
|
2013-01-17 17:02:05 +01:00
|
|
|
|
break;
|
2013-02-20 18:39:13 +01:00
|
|
|
|
#endif
|
2019-10-06 22:00:13 +02:00
|
|
|
|
p += mb_ptr2len(p) + 1;
|
2013-01-17 17:02:05 +01:00
|
|
|
|
if (PTR2CHAR(p) == c
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
|
|
|
|
&& !(curwin->w_p_rl ^ p_ri)
|
|
|
|
|
#endif
|
|
|
|
|
)
|
|
|
|
|
break;
|
2019-10-06 22:00:13 +02:00
|
|
|
|
p += mb_ptr2len(p);
|
2013-01-17 17:02:05 +01:00
|
|
|
|
if (*p == NUL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
|
2015-07-21 17:53:25 +02:00
|
|
|
|
vim_beep(BO_MATCH);
|
2013-02-20 18:39:13 +01:00
|
|
|
|
else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (!curwin->w_p_wrap)
|
|
|
|
|
getvcol(curwin, lpos, NULL, &vcol, NULL);
|
|
|
|
|
if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
|
2017-09-22 15:20:32 +02:00
|
|
|
|
&& vcol < curwin->w_leftcol + curwin->w_width))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
mpos = *lpos; // save the pos, update_screen() may change it
|
2004-06-13 20:20:40 +00:00
|
|
|
|
save_cursor = curwin->w_cursor;
|
2019-01-31 18:26:10 +01:00
|
|
|
|
save_so = *so;
|
|
|
|
|
save_siso = *siso;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Handle "$" in 'cpo': If the ')' is typed on top of the "$",
|
|
|
|
|
// stop displaying the "$".
|
2012-02-04 23:35:00 +01:00
|
|
|
|
if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
|
|
|
|
|
dollar_vcol = -1;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
++curwin->w_virtcol; // do display ')' just before "$"
|
|
|
|
|
update_screen(VALID); // show the new char first
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
|
|
save_dollar_vcol = dollar_vcol;
|
|
|
|
|
#ifdef CURSOR_SHAPE
|
|
|
|
|
save_state = State;
|
|
|
|
|
State = SHOWMATCH;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
ui_cursor_shape(); // may show different cursor shape
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#endif
|
2019-12-05 21:10:38 +01:00
|
|
|
|
curwin->w_cursor = mpos; // move to matching char
|
|
|
|
|
*so = 0; // don't use 'scrolloff' here
|
|
|
|
|
*siso = 0; // don't use 'sidescrolloff' here
|
2004-06-13 20:20:40 +00:00
|
|
|
|
showruler(FALSE);
|
|
|
|
|
setcursor();
|
2019-12-05 21:10:38 +01:00
|
|
|
|
cursor_on(); // make sure that the cursor is shown
|
2018-01-31 20:51:47 +01:00
|
|
|
|
out_flush_cursor(TRUE, FALSE);
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Restore dollar_vcol(), because setcursor() may call curs_rows()
|
|
|
|
|
// which resets it if the matching position is in a previous line
|
|
|
|
|
// and has a higher column number.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
dollar_vcol = save_dollar_vcol;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* brief pause, unless 'm' is present in 'cpo' and a character is
|
|
|
|
|
* available.
|
|
|
|
|
*/
|
|
|
|
|
if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
|
2019-11-17 17:06:33 +01:00
|
|
|
|
ui_delay(p_mat * 100L + 8, TRUE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if (!char_avail())
|
2019-11-17 17:06:33 +01:00
|
|
|
|
ui_delay(p_mat * 100L + 9, FALSE);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
curwin->w_cursor = save_cursor; // restore cursor position
|
2019-01-31 18:26:10 +01:00
|
|
|
|
*so = save_so;
|
|
|
|
|
*siso = save_siso;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#ifdef CURSOR_SHAPE
|
|
|
|
|
State = save_state;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
ui_cursor_shape(); // may show different cursor shape
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 15:23:37 +02:00
|
|
|
|
/*
|
2019-10-26 14:42:09 +02:00
|
|
|
|
* Check if the pattern is zero-width.
|
2019-10-24 15:23:37 +02:00
|
|
|
|
* If move is TRUE, check from the beginning of the buffer, else from position
|
|
|
|
|
* "cur".
|
|
|
|
|
* "direction" is FORWARD or BACKWARD.
|
|
|
|
|
* Returns TRUE, FALSE or -1 for failure.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
|
|
|
|
|
{
|
|
|
|
|
regmmatch_T regmatch;
|
|
|
|
|
int nmatched = 0;
|
|
|
|
|
int result = -1;
|
|
|
|
|
pos_T pos;
|
2019-12-23 22:59:18 +01:00
|
|
|
|
int called_emsg_before = called_emsg;
|
2019-10-24 15:23:37 +02:00
|
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
|
|
if (pattern == NULL)
|
|
|
|
|
pattern = spats[last_idx].pat;
|
|
|
|
|
|
|
|
|
|
if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
|
|
|
|
|
SEARCH_KEEP, ®match) == FAIL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
// init startcol correctly
|
|
|
|
|
regmatch.startpos[0].col = -1;
|
|
|
|
|
// move to match
|
|
|
|
|
if (move)
|
|
|
|
|
{
|
|
|
|
|
CLEAR_POS(&pos);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pos = *cur;
|
|
|
|
|
// accept a match at the cursor position
|
|
|
|
|
flag = SEARCH_START;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
|
|
|
|
|
SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
|
|
|
|
|
{
|
|
|
|
|
// Zero-width pattern should match somewhere, then we can check if
|
|
|
|
|
// start and end are in the same position.
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
regmatch.startpos[0].col++;
|
|
|
|
|
nmatched = vim_regexec_multi(®match, curwin, curbuf,
|
|
|
|
|
pos.lnum, regmatch.startpos[0].col, NULL, NULL);
|
|
|
|
|
if (nmatched != 0)
|
|
|
|
|
break;
|
2020-10-02 20:36:01 +02:00
|
|
|
|
} while (regmatch.regprog != NULL
|
|
|
|
|
&& direction == FORWARD ? regmatch.startpos[0].col < pos.col
|
2019-10-24 15:23:37 +02:00
|
|
|
|
: regmatch.startpos[0].col > pos.col);
|
|
|
|
|
|
2019-12-23 22:59:18 +01:00
|
|
|
|
if (called_emsg == called_emsg_before)
|
2019-10-24 15:23:37 +02:00
|
|
|
|
{
|
|
|
|
|
result = (nmatched != 0
|
|
|
|
|
&& regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
|
|
|
|
|
&& regmatch.startpos[0].col == regmatch.endpos[0].col);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vim_regfree(regmatch.regprog);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-23 15:53:05 +02:00
|
|
|
|
|
2012-07-29 12:55:32 +02:00
|
|
|
|
/*
|
|
|
|
|
* Find next search match under cursor, cursor at end.
|
|
|
|
|
* Used while an operator is pending, and in Visual mode.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
current_search(
|
|
|
|
|
long count,
|
2018-12-23 19:10:09 +01:00
|
|
|
|
int forward) // TRUE for forward, FALSE for backward
|
2012-07-29 12:55:32 +02:00
|
|
|
|
{
|
2018-12-23 19:10:09 +01:00
|
|
|
|
pos_T start_pos; // start position of the pattern match
|
|
|
|
|
pos_T end_pos; // end position of the pattern match
|
|
|
|
|
pos_T orig_pos; // position of the cursor at beginning
|
|
|
|
|
pos_T pos; // position after the pattern
|
2012-07-29 12:55:32 +02:00
|
|
|
|
int i;
|
|
|
|
|
int dir;
|
2018-12-23 19:10:09 +01:00
|
|
|
|
int result; // result of various function calls
|
2012-07-29 12:55:32 +02:00
|
|
|
|
char_u old_p_ws = p_ws;
|
|
|
|
|
int flags = 0;
|
2013-07-17 19:22:13 +02:00
|
|
|
|
pos_T save_VIsual = VIsual;
|
2019-10-24 15:23:37 +02:00
|
|
|
|
int zero_width;
|
2020-10-11 20:44:15 +02:00
|
|
|
|
int skip_first_backward;
|
2012-07-29 12:55:32 +02:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Correct cursor when 'selection' is exclusive
|
2017-03-12 18:23:53 +01:00
|
|
|
|
if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
|
2012-07-29 12:55:32 +02:00
|
|
|
|
dec_cursor();
|
|
|
|
|
|
2020-10-11 20:44:15 +02:00
|
|
|
|
// When searching forward and the cursor is at the start of the Visual
|
|
|
|
|
// area, skip the first search backward, otherwise it doesn't move.
|
|
|
|
|
skip_first_backward = forward && VIsual_active
|
|
|
|
|
&& LT_POS(curwin->w_cursor, VIsual);
|
|
|
|
|
|
2019-10-24 15:23:37 +02:00
|
|
|
|
orig_pos = pos = curwin->w_cursor;
|
2012-07-29 12:55:32 +02:00
|
|
|
|
if (VIsual_active)
|
|
|
|
|
{
|
2019-10-24 15:23:37 +02:00
|
|
|
|
if (forward)
|
|
|
|
|
incl(&pos);
|
|
|
|
|
else
|
|
|
|
|
decl(&pos);
|
2012-07-29 12:55:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Is the pattern is zero-width?, this time, don't care about the direction
|
2019-10-24 15:23:37 +02:00
|
|
|
|
zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
|
2017-09-26 12:28:45 +02:00
|
|
|
|
FORWARD);
|
2019-10-24 15:23:37 +02:00
|
|
|
|
if (zero_width == -1)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
return FAIL; // pattern not found
|
2012-08-08 15:27:57 +02:00
|
|
|
|
|
2012-07-29 12:55:32 +02:00
|
|
|
|
/*
|
|
|
|
|
* The trick is to first search backwards and then search forward again,
|
|
|
|
|
* so that a match at the current cursor position will be correctly
|
2020-10-11 20:44:15 +02:00
|
|
|
|
* captured. When "forward" is false do it the other way around.
|
2012-07-29 12:55:32 +02:00
|
|
|
|
*/
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
|
{
|
|
|
|
|
if (forward)
|
2020-10-11 20:44:15 +02:00
|
|
|
|
{
|
|
|
|
|
if (i == 0 && skip_first_backward)
|
|
|
|
|
continue;
|
2012-07-29 12:55:32 +02:00
|
|
|
|
dir = i;
|
2020-10-11 20:44:15 +02:00
|
|
|
|
}
|
2012-07-29 12:55:32 +02:00
|
|
|
|
else
|
|
|
|
|
dir = !i;
|
2012-08-08 15:27:57 +02:00
|
|
|
|
|
|
|
|
|
flags = 0;
|
2019-10-24 15:23:37 +02:00
|
|
|
|
if (!dir && !zero_width)
|
2012-08-08 15:27:57 +02:00
|
|
|
|
flags = SEARCH_END;
|
2018-12-23 19:10:09 +01:00
|
|
|
|
end_pos = pos;
|
2012-08-08 15:27:57 +02:00
|
|
|
|
|
2019-11-02 23:22:47 +01:00
|
|
|
|
// wrapping should not occur in the first round
|
|
|
|
|
if (i == 0)
|
|
|
|
|
p_ws = FALSE;
|
|
|
|
|
|
2018-12-23 19:10:09 +01:00
|
|
|
|
result = searchit(curwin, curbuf, &pos, &end_pos,
|
|
|
|
|
(dir ? FORWARD : BACKWARD),
|
2012-07-29 12:55:32 +02:00
|
|
|
|
spats[last_idx].pat, (long) (i ? count : 1),
|
2019-10-18 20:53:34 +02:00
|
|
|
|
SEARCH_KEEP | flags, RE_SEARCH, NULL);
|
2012-07-29 12:55:32 +02:00
|
|
|
|
|
2019-11-02 23:22:47 +01:00
|
|
|
|
p_ws = old_p_ws;
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// First search may fail, but then start searching from the
|
|
|
|
|
// beginning of the file (cursor might be on the search match)
|
|
|
|
|
// except when Visual mode is active, so that extending the visual
|
|
|
|
|
// selection works.
|
|
|
|
|
if (i == 1 && !result) // not found, abort
|
2012-07-29 12:55:32 +02:00
|
|
|
|
{
|
|
|
|
|
curwin->w_cursor = orig_pos;
|
|
|
|
|
if (VIsual_active)
|
|
|
|
|
VIsual = save_VIsual;
|
|
|
|
|
return FAIL;
|
|
|
|
|
}
|
2018-12-23 19:10:09 +01:00
|
|
|
|
else if (i == 0 && !result)
|
2012-07-29 12:55:32 +02:00
|
|
|
|
{
|
2017-03-12 18:23:53 +01:00
|
|
|
|
if (forward)
|
2012-07-29 12:55:32 +02:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// try again from start of buffer
|
2017-03-12 18:23:53 +01:00
|
|
|
|
CLEAR_POS(&pos);
|
2012-07-29 12:55:32 +02:00
|
|
|
|
}
|
2017-03-12 18:23:53 +01:00
|
|
|
|
else
|
2012-07-29 12:55:32 +02:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// try again from end of buffer
|
|
|
|
|
// searching backwards, so set pos to last line and col
|
2012-07-29 12:55:32 +02:00
|
|
|
|
pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
|
2012-08-02 21:24:42 +02:00
|
|
|
|
pos.col = (colnr_T)STRLEN(
|
|
|
|
|
ml_get(curwin->w_buffer->b_ml.ml_line_count));
|
2012-07-29 12:55:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start_pos = pos;
|
|
|
|
|
|
|
|
|
|
if (!VIsual_active)
|
|
|
|
|
VIsual = start_pos;
|
|
|
|
|
|
2020-10-11 20:44:15 +02:00
|
|
|
|
// put the cursor after the match
|
2018-12-23 19:10:09 +01:00
|
|
|
|
curwin->w_cursor = end_pos;
|
2019-10-26 14:42:09 +02:00
|
|
|
|
if (LT_POS(VIsual, end_pos) && forward)
|
2020-10-11 20:44:15 +02:00
|
|
|
|
{
|
|
|
|
|
if (skip_first_backward)
|
|
|
|
|
// put the cursor on the start of the match
|
|
|
|
|
curwin->w_cursor = pos;
|
|
|
|
|
else
|
|
|
|
|
// put the cursor on last character of match
|
|
|
|
|
dec_cursor();
|
|
|
|
|
}
|
2020-10-10 16:45:25 +02:00
|
|
|
|
else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
|
2019-10-24 15:23:37 +02:00
|
|
|
|
curwin->w_cursor = pos; // put the cursor on the start of the match
|
2012-07-29 12:55:32 +02:00
|
|
|
|
VIsual_active = TRUE;
|
|
|
|
|
VIsual_mode = 'v';
|
|
|
|
|
|
2019-02-10 21:48:25 +01:00
|
|
|
|
if (*p_sel == 'e')
|
2012-07-29 12:55:32 +02:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Correction for exclusive selection depends on the direction.
|
2019-02-10 21:48:25 +01:00
|
|
|
|
if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
|
|
|
|
|
inc_cursor();
|
|
|
|
|
else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
|
|
|
|
|
inc(&VIsual);
|
2012-07-29 12:55:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_FOLDING
|
|
|
|
|
if (fdo_flags & FDO_SEARCH && KeyTyped)
|
|
|
|
|
foldOpenCursor();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
may_start_select('c');
|
|
|
|
|
setmouse();
|
|
|
|
|
#ifdef FEAT_CLIPBOARD
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Make sure the clipboard gets updated. Needed because start and
|
|
|
|
|
// end are still the same, and the selection needs to be owned
|
2012-07-29 12:55:32 +02:00
|
|
|
|
clip_star.vmode = NUL;
|
|
|
|
|
#endif
|
|
|
|
|
redraw_curbuf_later(INVERTED);
|
|
|
|
|
showmode();
|
|
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
2012-08-23 15:53:05 +02:00
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
|
|
|
|
|
|| defined(PROTO)
|
|
|
|
|
/*
|
|
|
|
|
* return TRUE if line 'lnum' is empty or has white chars only.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-01-30 21:10:09 +01:00
|
|
|
|
linewhite(linenr_T lnum)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
|
|
p = skipwhite(ml_get(lnum));
|
|
|
|
|
return (*p == NUL);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-05-04 21:08:40 +02:00
|
|
|
|
/*
|
|
|
|
|
* Add the search count "[3/19]" to "msgbuf".
|
2020-06-01 17:28:35 +02:00
|
|
|
|
* See update_search_stat() for other arguments.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
cmdline_search_stat(
|
|
|
|
|
int dirc,
|
|
|
|
|
pos_T *pos,
|
|
|
|
|
pos_T *cursor_pos,
|
|
|
|
|
int show_top_bot_msg,
|
|
|
|
|
char_u *msgbuf,
|
|
|
|
|
int recompute,
|
|
|
|
|
int maxcount,
|
|
|
|
|
long timeout)
|
|
|
|
|
{
|
|
|
|
|
searchstat_T stat;
|
|
|
|
|
|
|
|
|
|
update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
|
|
|
|
|
timeout);
|
|
|
|
|
if (stat.cur > 0)
|
|
|
|
|
{
|
|
|
|
|
char t[SEARCH_STAT_BUF_LEN];
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
|
|
|
|
if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
|
|
|
|
|
{
|
|
|
|
|
if (stat.incomplete == 1)
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
|
|
|
|
|
else if (stat.cnt > maxcount && stat.cur > maxcount)
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
|
|
|
|
|
maxcount, maxcount);
|
|
|
|
|
else if (stat.cnt > maxcount)
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
|
|
|
|
|
maxcount, stat.cur);
|
|
|
|
|
else
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
|
|
|
|
|
stat.cnt, stat.cur);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
if (stat.incomplete == 1)
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
|
|
|
|
|
else if (stat.cnt > maxcount && stat.cur > maxcount)
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
|
|
|
|
|
maxcount, maxcount);
|
|
|
|
|
else if (stat.cnt > maxcount)
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
|
|
|
|
|
stat.cur, maxcount);
|
|
|
|
|
else
|
|
|
|
|
vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
|
|
|
|
|
stat.cur, stat.cnt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = STRLEN(t);
|
|
|
|
|
if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
|
|
|
|
|
{
|
|
|
|
|
mch_memmove(t + 2, t, len);
|
|
|
|
|
t[0] = 'W';
|
|
|
|
|
t[1] = ' ';
|
|
|
|
|
len += 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
|
|
|
|
|
if (dirc == '?' && stat.cur == maxcount + 1)
|
|
|
|
|
stat.cur = -1;
|
|
|
|
|
|
|
|
|
|
// keep the message even after redraw, but don't put in history
|
|
|
|
|
msg_hist_off = TRUE;
|
|
|
|
|
give_warning(msgbuf, FALSE);
|
|
|
|
|
msg_hist_off = FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Add the search count information to "stat".
|
|
|
|
|
* "stat" must not be NULL.
|
2019-05-24 22:08:15 +02:00
|
|
|
|
* When "recompute" is TRUE always recompute the numbers.
|
2020-06-01 17:28:35 +02:00
|
|
|
|
* dirc == 0: don't find the next/previous match (only set the result to "stat")
|
|
|
|
|
* dirc == '/': find the next match
|
|
|
|
|
* dirc == '?': find the previous match
|
2019-05-04 21:08:40 +02:00
|
|
|
|
*/
|
|
|
|
|
static void
|
2020-06-01 17:28:35 +02:00
|
|
|
|
update_search_stat(
|
|
|
|
|
int dirc,
|
|
|
|
|
pos_T *pos,
|
|
|
|
|
pos_T *cursor_pos,
|
|
|
|
|
searchstat_T *stat,
|
|
|
|
|
int recompute,
|
|
|
|
|
int maxcount,
|
2020-06-01 18:56:03 +02:00
|
|
|
|
long timeout UNUSED)
|
2019-05-04 21:08:40 +02:00
|
|
|
|
{
|
|
|
|
|
int save_ws = p_ws;
|
|
|
|
|
int wraparound = FALSE;
|
|
|
|
|
pos_T p = (*pos);
|
2020-06-03 22:57:39 +02:00
|
|
|
|
static pos_T lastpos = {0, 0, 0};
|
2019-05-04 21:08:40 +02:00
|
|
|
|
static int cur = 0;
|
|
|
|
|
static int cnt = 0;
|
2020-06-01 17:28:35 +02:00
|
|
|
|
static int exact_match = FALSE;
|
|
|
|
|
static int incomplete = 0;
|
|
|
|
|
static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
static int chgtick = 0;
|
|
|
|
|
static char_u *lastpat = NULL;
|
|
|
|
|
static buf_T *lbuf = NULL;
|
|
|
|
|
#ifdef FEAT_RELTIME
|
|
|
|
|
proftime_T start;
|
|
|
|
|
#endif
|
2020-06-01 17:28:35 +02:00
|
|
|
|
|
|
|
|
|
vim_memset(stat, 0, sizeof(searchstat_T));
|
|
|
|
|
|
|
|
|
|
if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
|
|
|
|
|
{
|
|
|
|
|
stat->cur = cur;
|
|
|
|
|
stat->cnt = cnt;
|
|
|
|
|
stat->exact_match = exact_match;
|
|
|
|
|
stat->incomplete = incomplete;
|
|
|
|
|
stat->last_maxcount = last_maxcount;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
last_maxcount = maxcount;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
|
|
|
|
|
wraparound = ((dirc == '?' && LT_POS(lastpos, p))
|
|
|
|
|
|| (dirc == '/' && LT_POS(p, lastpos)));
|
|
|
|
|
|
|
|
|
|
// If anything relevant changed the count has to be recomputed.
|
|
|
|
|
// MB_STRNICMP ignores case, but we should not ignore case.
|
|
|
|
|
// Unfortunately, there is no MB_STRNICMP function.
|
2020-06-01 17:28:35 +02:00
|
|
|
|
// XXX: above comment should be "no MB_STRCMP function" ?
|
2019-05-04 21:08:40 +02:00
|
|
|
|
if (!(chgtick == CHANGEDTICK(curbuf)
|
|
|
|
|
&& MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
|
|
|
|
|
&& STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
|
2020-06-01 17:28:35 +02:00
|
|
|
|
&& EQUAL_POS(lastpos, *cursor_pos)
|
|
|
|
|
&& lbuf == curbuf) || wraparound || cur < 0
|
|
|
|
|
|| (maxcount > 0 && cur > maxcount) || recompute)
|
2019-05-04 21:08:40 +02:00
|
|
|
|
{
|
|
|
|
|
cur = 0;
|
|
|
|
|
cnt = 0;
|
2020-06-01 17:28:35 +02:00
|
|
|
|
exact_match = FALSE;
|
|
|
|
|
incomplete = 0;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
CLEAR_POS(&lastpos);
|
|
|
|
|
lbuf = curbuf;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 17:28:35 +02:00
|
|
|
|
if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
|
|
|
|
|
&& (dirc == 0 || dirc == '/' ? cur < cnt : cur > 0))
|
|
|
|
|
cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-01 17:28:35 +02:00
|
|
|
|
int done_search = FALSE;
|
|
|
|
|
pos_T endpos = {0, 0, 0};
|
|
|
|
|
|
2019-05-04 21:08:40 +02:00
|
|
|
|
p_ws = FALSE;
|
|
|
|
|
#ifdef FEAT_RELTIME
|
2020-06-01 17:28:35 +02:00
|
|
|
|
if (timeout > 0)
|
|
|
|
|
profile_setlimit(timeout, &start);
|
2019-05-04 21:08:40 +02:00
|
|
|
|
#endif
|
2020-06-01 17:28:35 +02:00
|
|
|
|
while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
|
2019-10-18 20:53:34 +02:00
|
|
|
|
FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
|
2019-05-04 21:08:40 +02:00
|
|
|
|
{
|
2020-06-01 17:28:35 +02:00
|
|
|
|
done_search = TRUE;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
#ifdef FEAT_RELTIME
|
|
|
|
|
// Stop after passing the time limit.
|
2020-06-01 17:28:35 +02:00
|
|
|
|
if (timeout > 0 && profile_passed_limit(&start))
|
2019-05-04 21:08:40 +02:00
|
|
|
|
{
|
2020-06-01 17:28:35 +02:00
|
|
|
|
incomplete = 1;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
cnt++;
|
|
|
|
|
if (LTOREQ_POS(lastpos, p))
|
2020-06-01 17:28:35 +02:00
|
|
|
|
{
|
|
|
|
|
cur = cnt;
|
2020-06-02 22:06:21 +02:00
|
|
|
|
if (LT_POS(p, endpos))
|
2020-06-01 17:28:35 +02:00
|
|
|
|
exact_match = TRUE;
|
|
|
|
|
}
|
2019-05-04 21:08:40 +02:00
|
|
|
|
fast_breakcheck();
|
2020-06-01 17:28:35 +02:00
|
|
|
|
if (maxcount > 0 && cnt > maxcount)
|
|
|
|
|
{
|
|
|
|
|
incomplete = 2; // max count exceeded
|
2019-05-04 21:08:40 +02:00
|
|
|
|
break;
|
2020-06-01 17:28:35 +02:00
|
|
|
|
}
|
2019-05-04 21:08:40 +02:00
|
|
|
|
}
|
|
|
|
|
if (got_int)
|
|
|
|
|
cur = -1; // abort
|
2020-06-01 17:28:35 +02:00
|
|
|
|
if (done_search)
|
2019-05-04 21:08:40 +02:00
|
|
|
|
{
|
2020-06-01 17:28:35 +02:00
|
|
|
|
vim_free(lastpat);
|
|
|
|
|
lastpat = vim_strsave(spats[last_idx].pat);
|
|
|
|
|
chgtick = CHANGEDTICK(curbuf);
|
|
|
|
|
lbuf = curbuf;
|
|
|
|
|
lastpos = p;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-01 17:28:35 +02:00
|
|
|
|
stat->cur = cur;
|
|
|
|
|
stat->cnt = cnt;
|
|
|
|
|
stat->exact_match = exact_match;
|
|
|
|
|
stat->incomplete = incomplete;
|
|
|
|
|
stat->last_maxcount = last_maxcount;
|
2019-05-04 21:08:40 +02:00
|
|
|
|
p_ws = save_ws;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#if defined(FEAT_FIND_ID) || defined(PROTO)
|
|
|
|
|
/*
|
|
|
|
|
* Find identifiers or defines in included files.
|
2011-02-25 18:38:36 +01:00
|
|
|
|
* If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
*/
|
|
|
|
|
void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
find_pattern_in_path(
|
2019-12-05 21:10:38 +01:00
|
|
|
|
char_u *ptr, // pointer to search pattern
|
|
|
|
|
int dir UNUSED, // direction of expansion
|
|
|
|
|
int len, // length of search pattern
|
|
|
|
|
int whole, // match whole words only
|
|
|
|
|
int skip_comments, // don't match inside comments
|
|
|
|
|
int type, // Type of search; are we looking for a type?
|
|
|
|
|
// a macro?
|
2016-01-30 21:10:09 +01:00
|
|
|
|
long count,
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int action, // What to do when we find it
|
|
|
|
|
linenr_T start_lnum, // first line to start searching
|
|
|
|
|
linenr_T end_lnum) // last line for searching
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
SearchedFile *files; // Stack of included files
|
|
|
|
|
SearchedFile *bigger; // When we need more space
|
2004-06-13 20:20:40 +00:00
|
|
|
|
int max_path_depth = 50;
|
|
|
|
|
long match_count = 1;
|
|
|
|
|
|
|
|
|
|
char_u *pat;
|
|
|
|
|
char_u *new_fname;
|
|
|
|
|
char_u *curr_fname = curbuf->b_fname;
|
|
|
|
|
char_u *prev_fname = NULL;
|
|
|
|
|
linenr_T lnum;
|
|
|
|
|
int depth;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
int depth_displayed; // For type==CHECK_PATH
|
2004-06-13 20:20:40 +00:00
|
|
|
|
int old_files;
|
|
|
|
|
int already_searched;
|
|
|
|
|
char_u *file_line;
|
|
|
|
|
char_u *line;
|
|
|
|
|
char_u *p;
|
|
|
|
|
char_u save_char;
|
|
|
|
|
int define_matched;
|
|
|
|
|
regmatch_T regmatch;
|
|
|
|
|
regmatch_T incl_regmatch;
|
|
|
|
|
regmatch_T def_regmatch;
|
|
|
|
|
int matched = FALSE;
|
|
|
|
|
int did_show = FALSE;
|
|
|
|
|
int found = FALSE;
|
|
|
|
|
int i;
|
|
|
|
|
char_u *already = NULL;
|
|
|
|
|
char_u *startp = NULL;
|
2005-09-29 18:26:07 +00:00
|
|
|
|
char_u *inc_opt = NULL;
|
2017-09-16 20:54:51 +02:00
|
|
|
|
#if defined(FEAT_QUICKFIX)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
win_T *curwin_save = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
regmatch.regprog = NULL;
|
|
|
|
|
incl_regmatch.regprog = NULL;
|
|
|
|
|
def_regmatch.regprog = NULL;
|
|
|
|
|
|
|
|
|
|
file_line = alloc(LSIZE);
|
|
|
|
|
if (file_line == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (type != CHECK_PATH && type != FIND_DEFINE
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// when CONT_SOL is set compare "ptr" with the beginning of the line
|
|
|
|
|
// is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
|
2019-08-21 14:37:09 +02:00
|
|
|
|
&& !(compl_cont_status & CONT_SOL))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
pat = alloc(len + 5);
|
|
|
|
|
if (pat == NULL)
|
|
|
|
|
goto fpip_end;
|
|
|
|
|
sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// ignore case according to p_ic, p_scs and pat
|
2004-06-13 20:20:40 +00:00
|
|
|
|
regmatch.rm_ic = ignorecase(pat);
|
2020-12-21 19:59:08 +01:00
|
|
|
|
regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
vim_free(pat);
|
|
|
|
|
if (regmatch.regprog == NULL)
|
|
|
|
|
goto fpip_end;
|
|
|
|
|
}
|
2005-09-29 18:26:07 +00:00
|
|
|
|
inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
|
|
|
|
|
if (*inc_opt != NUL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2020-12-21 19:59:08 +01:00
|
|
|
|
incl_regmatch.regprog = vim_regcomp(inc_opt,
|
|
|
|
|
magic_isset() ? RE_MAGIC : 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (incl_regmatch.regprog == NULL)
|
|
|
|
|
goto fpip_end;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
|
|
|
|
|
{
|
|
|
|
|
def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
|
2020-12-21 19:59:08 +01:00
|
|
|
|
? p_def : curbuf->b_p_def,
|
|
|
|
|
magic_isset() ? RE_MAGIC : 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (def_regmatch.regprog == NULL)
|
|
|
|
|
goto fpip_end;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2019-05-28 23:08:19 +02:00
|
|
|
|
files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (files == NULL)
|
|
|
|
|
goto fpip_end;
|
|
|
|
|
old_files = max_path_depth;
|
|
|
|
|
depth = depth_displayed = -1;
|
|
|
|
|
|
|
|
|
|
lnum = start_lnum;
|
|
|
|
|
if (end_lnum > curbuf->b_ml.ml_line_count)
|
|
|
|
|
end_lnum = curbuf->b_ml.ml_line_count;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (lnum > end_lnum) // do at least one line
|
2004-06-13 20:20:40 +00:00
|
|
|
|
lnum = end_lnum;
|
|
|
|
|
line = ml_get(lnum);
|
|
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
if (incl_regmatch.regprog != NULL
|
|
|
|
|
&& vim_regexec(&incl_regmatch, line, (colnr_T)0))
|
|
|
|
|
{
|
2005-09-29 18:26:07 +00:00
|
|
|
|
char_u *p_fname = (curr_fname == curbuf->b_fname)
|
|
|
|
|
? curbuf->b_ffname : curr_fname;
|
|
|
|
|
|
|
|
|
|
if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Use text from '\zs' to '\ze' (or end) of 'include'.
|
2005-09-29 18:26:07 +00:00
|
|
|
|
new_fname = find_file_name_in_path(incl_regmatch.startp[0],
|
2013-07-03 22:28:36 +02:00
|
|
|
|
(int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
|
2005-09-29 18:26:07 +00:00
|
|
|
|
FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
|
|
|
|
|
else
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Use text after match with 'include'.
|
2005-09-29 18:26:07 +00:00
|
|
|
|
new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
|
2006-02-22 21:25:37 +00:00
|
|
|
|
FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
already_searched = FALSE;
|
|
|
|
|
if (new_fname != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Check whether we have already searched in this file
|
2004-06-13 20:20:40 +00:00
|
|
|
|
for (i = 0;; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i == depth + 1)
|
|
|
|
|
i = old_files;
|
|
|
|
|
if (i == max_path_depth)
|
|
|
|
|
break;
|
2019-05-23 21:35:48 +02:00
|
|
|
|
if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
|
|
|
|
|
& FPC_SAME)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (type != CHECK_PATH &&
|
|
|
|
|
action == ACTION_SHOW_ALL && files[i].matched)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
msg_putchar('\n'); // cursor below last one
|
|
|
|
|
if (!got_int) // don't display if 'q'
|
|
|
|
|
// typed at "--more--"
|
|
|
|
|
// message
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
msg_home_replace_hl(new_fname);
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts(_(" (includes previously listed match)"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
prev_fname = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-10 18:45:26 +01:00
|
|
|
|
VIM_CLEAR(new_fname);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
already_searched = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
|
|
|
|
|
|| (new_fname == NULL && !already_searched)))
|
|
|
|
|
{
|
|
|
|
|
if (did_show)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
msg_putchar('\n'); // cursor below last one
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
gotocmdline(TRUE); // cursor at status line
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts_title(_("--- Included files "));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (action != ACTION_SHOW_ALL)
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts_title(_("not found "));
|
|
|
|
|
msg_puts_title(_("in path ---\n"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
did_show = TRUE;
|
|
|
|
|
while (depth_displayed < depth && !got_int)
|
|
|
|
|
{
|
|
|
|
|
++depth_displayed;
|
|
|
|
|
for (i = 0; i < depth_displayed; i++)
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts(" ");
|
2004-06-13 20:20:40 +00:00
|
|
|
|
msg_home_replace(files[depth_displayed].name);
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts(" -->\n");
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (!got_int) // don't display if 'q' typed
|
|
|
|
|
// for "--more--" message
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
for (i = 0; i <= depth_displayed; i++)
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts(" ");
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (new_fname != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// using "new_fname" is more reliable, e.g., when
|
|
|
|
|
// 'includeexpr' is set.
|
2017-03-16 17:23:31 +01:00
|
|
|
|
msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Isolate the file name.
|
|
|
|
|
* Include the surrounding "" or <> if present.
|
|
|
|
|
*/
|
2012-07-25 13:46:30 +02:00
|
|
|
|
if (inc_opt != NULL
|
|
|
|
|
&& strstr((char *)inc_opt, "\\zs") != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// pattern contains \zs, use the match
|
2012-07-25 13:46:30 +02:00
|
|
|
|
p = incl_regmatch.startp[0];
|
|
|
|
|
i = (int)(incl_regmatch.endp[0]
|
|
|
|
|
- incl_regmatch.startp[0]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// find the file name after the end of the match
|
2012-07-25 13:46:30 +02:00
|
|
|
|
for (p = incl_regmatch.endp[0];
|
|
|
|
|
*p && !vim_isfilec(*p); p++)
|
|
|
|
|
;
|
|
|
|
|
for (i = 0; vim_isfilec(p[i]); i++)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (i == 0)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Nothing found, use the rest of the line.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
p = incl_regmatch.endp[0];
|
2006-04-17 22:14:47 +00:00
|
|
|
|
i = (int)STRLEN(p);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Avoid checking before the start of the line, can
|
|
|
|
|
// happen if \zs appears in the regexp.
|
2012-07-25 13:46:30 +02:00
|
|
|
|
else if (p > line)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
if (p[-1] == '"' || p[-1] == '<')
|
|
|
|
|
{
|
|
|
|
|
--p;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
if (p[i] == '"' || p[i] == '>')
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
save_char = p[i];
|
|
|
|
|
p[i] = NUL;
|
2017-03-16 17:23:31 +01:00
|
|
|
|
msg_outtrans_attr(p, HL_ATTR(HLF_D));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
p[i] = save_char;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (new_fname == NULL && action == ACTION_SHOW_ALL)
|
|
|
|
|
{
|
|
|
|
|
if (already_searched)
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts(_(" (Already listed)"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts(_(" NOT FOUND"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
out_flush(); // output each line directly
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (new_fname != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Push the new file onto the file stack
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (depth + 1 == old_files)
|
|
|
|
|
{
|
2019-05-28 23:08:19 +02:00
|
|
|
|
bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (bigger != NULL)
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i <= depth; i++)
|
|
|
|
|
bigger[i] = files[i];
|
|
|
|
|
for (i = depth + 1; i < old_files + max_path_depth; i++)
|
|
|
|
|
{
|
|
|
|
|
bigger[i].fp = NULL;
|
|
|
|
|
bigger[i].name = NULL;
|
|
|
|
|
bigger[i].lnum = 0;
|
|
|
|
|
bigger[i].matched = FALSE;
|
|
|
|
|
}
|
|
|
|
|
for (i = old_files; i < max_path_depth; i++)
|
|
|
|
|
bigger[i + max_path_depth] = files[i];
|
|
|
|
|
old_files += max_path_depth;
|
|
|
|
|
max_path_depth *= 2;
|
|
|
|
|
vim_free(files);
|
|
|
|
|
files = bigger;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
|
|
|
|
|
== NULL)
|
|
|
|
|
vim_free(new_fname);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (++depth == old_files)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* lalloc() for 'bigger' must have failed above. We
|
|
|
|
|
* will forget one of our already visited files now.
|
|
|
|
|
*/
|
|
|
|
|
vim_free(files[old_files].name);
|
|
|
|
|
++old_files;
|
|
|
|
|
}
|
|
|
|
|
files[depth].name = curr_fname = new_fname;
|
|
|
|
|
files[depth].lnum = 0;
|
|
|
|
|
files[depth].matched = FALSE;
|
|
|
|
|
if (action == ACTION_EXPAND)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
msg_hist_off = TRUE; // reset in msg_trunc_attr()
|
2005-05-19 21:08:39 +00:00
|
|
|
|
vim_snprintf((char*)IObuff, IOSIZE,
|
|
|
|
|
_("Scanning included file: %s"),
|
|
|
|
|
(char *)new_fname);
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2019-08-21 14:37:09 +02:00
|
|
|
|
else if (p_verbose >= 5)
|
2006-03-04 21:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
verbose_enter();
|
2019-01-13 23:38:42 +01:00
|
|
|
|
smsg(_("Searching included file %s"),
|
2006-03-04 21:55:31 +00:00
|
|
|
|
(char *)new_fname);
|
|
|
|
|
verbose_leave();
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Check if the line is a define (type == FIND_DEFINE)
|
|
|
|
|
*/
|
|
|
|
|
p = line;
|
|
|
|
|
search_line:
|
|
|
|
|
define_matched = FALSE;
|
|
|
|
|
if (def_regmatch.regprog != NULL
|
|
|
|
|
&& vim_regexec(&def_regmatch, line, (colnr_T)0))
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Pattern must be first identifier after 'define', so skip
|
|
|
|
|
* to that position before checking for match of pattern. Also
|
|
|
|
|
* don't let it match beyond the end of this identifier.
|
|
|
|
|
*/
|
|
|
|
|
p = def_regmatch.endp[0];
|
|
|
|
|
while (*p && !vim_iswordc(*p))
|
|
|
|
|
p++;
|
|
|
|
|
define_matched = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Look for a match. Don't do this if we are looking for a
|
|
|
|
|
* define and this line didn't match define_prog above.
|
|
|
|
|
*/
|
|
|
|
|
if (def_regmatch.regprog == NULL || define_matched)
|
|
|
|
|
{
|
2019-08-21 14:37:09 +02:00
|
|
|
|
if (define_matched || (compl_cont_status & CONT_SOL))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// compare the first "len" chars from "ptr"
|
2004-06-13 20:20:40 +00:00
|
|
|
|
startp = skipwhite(p);
|
|
|
|
|
if (p_ic)
|
|
|
|
|
matched = !MB_STRNICMP(startp, ptr, len);
|
|
|
|
|
else
|
|
|
|
|
matched = !STRNCMP(startp, ptr, len);
|
|
|
|
|
if (matched && define_matched && whole
|
|
|
|
|
&& vim_iswordc(startp[len]))
|
|
|
|
|
matched = FALSE;
|
|
|
|
|
}
|
|
|
|
|
else if (regmatch.regprog != NULL
|
|
|
|
|
&& vim_regexec(®match, line, (colnr_T)(p - line)))
|
|
|
|
|
{
|
|
|
|
|
matched = TRUE;
|
|
|
|
|
startp = regmatch.startp[0];
|
|
|
|
|
/*
|
|
|
|
|
* Check if the line is not a comment line (unless we are
|
|
|
|
|
* looking for a define). A line starting with "# define"
|
|
|
|
|
* is not considered to be a comment line.
|
|
|
|
|
*/
|
|
|
|
|
if (!define_matched && skip_comments)
|
|
|
|
|
{
|
|
|
|
|
if ((*line != '#' ||
|
|
|
|
|
STRNCMP(skipwhite(line + 1), "define", 6) != 0)
|
2012-06-06 16:12:59 +02:00
|
|
|
|
&& get_leader_len(line, NULL, FALSE, TRUE))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
matched = FALSE;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Also check for a "/ *" or "/ /" before the match.
|
|
|
|
|
* Skips lines like "int backwards; / * normal index
|
|
|
|
|
* * /" when looking for "normal".
|
|
|
|
|
* Note: Doesn't skip "/ *" in comments.
|
|
|
|
|
*/
|
|
|
|
|
p = skipwhite(line);
|
|
|
|
|
if (matched
|
|
|
|
|
|| (p[0] == '/' && p[1] == '*') || p[0] == '*')
|
|
|
|
|
for (p = line; *p && p < startp; ++p)
|
|
|
|
|
{
|
|
|
|
|
if (matched
|
|
|
|
|
&& p[0] == '/'
|
|
|
|
|
&& (p[1] == '*' || p[1] == '/'))
|
|
|
|
|
{
|
|
|
|
|
matched = FALSE;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// After "//" all text is comment
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (p[1] == '/')
|
|
|
|
|
break;
|
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
else if (!matched && p[0] == '*' && p[1] == '/')
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Can find match after "* /".
|
2004-06-13 20:20:40 +00:00
|
|
|
|
matched = TRUE;
|
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (matched)
|
|
|
|
|
{
|
|
|
|
|
if (action == ACTION_EXPAND)
|
|
|
|
|
{
|
2019-04-06 14:22:21 +02:00
|
|
|
|
int cont_s_ipos = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
int add_r;
|
|
|
|
|
char_u *aux;
|
|
|
|
|
|
|
|
|
|
if (depth == -1 && lnum == curwin->w_cursor.lnum)
|
|
|
|
|
break;
|
|
|
|
|
found = TRUE;
|
|
|
|
|
aux = p = startp;
|
2005-07-29 22:36:03 +00:00
|
|
|
|
if (compl_cont_status & CONT_ADDING)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2005-07-29 22:36:03 +00:00
|
|
|
|
p += compl_length;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (vim_iswordp(p))
|
|
|
|
|
goto exit_matched;
|
|
|
|
|
p = find_word_start(p);
|
|
|
|
|
}
|
|
|
|
|
p = find_word_end(p);
|
|
|
|
|
i = (int)(p - aux);
|
|
|
|
|
|
2005-07-29 22:36:03 +00:00
|
|
|
|
if ((compl_cont_status & CONT_ADDING) && i == compl_length)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// IOSIZE > compl_length, so the STRNCPY works
|
2004-06-13 20:20:40 +00:00
|
|
|
|
STRNCPY(IObuff, aux, i);
|
2006-08-29 15:30:07 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Get the next line: when "depth" < 0 from the current
|
|
|
|
|
// buffer, otherwise from the included file. Jump to
|
|
|
|
|
// exit_matched when past the last line.
|
2006-08-29 15:30:07 +00:00
|
|
|
|
if (depth < 0)
|
|
|
|
|
{
|
|
|
|
|
if (lnum >= end_lnum)
|
|
|
|
|
goto exit_matched;
|
|
|
|
|
line = ml_get(++lnum);
|
|
|
|
|
}
|
|
|
|
|
else if (vim_fgets(line = file_line,
|
|
|
|
|
LSIZE, files[depth].fp))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
goto exit_matched;
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// we read a line, set "already" to check this "line" later
|
|
|
|
|
// if depth >= 0 we'll increase files[depth].lnum far
|
2020-12-18 19:49:56 +01:00
|
|
|
|
// below -- Acevedo
|
2004-06-13 20:20:40 +00:00
|
|
|
|
already = aux = p = skipwhite(line);
|
|
|
|
|
p = find_word_start(p);
|
|
|
|
|
p = find_word_end(p);
|
|
|
|
|
if (p > aux)
|
|
|
|
|
{
|
|
|
|
|
if (*aux != ')' && IObuff[i-1] != TAB)
|
|
|
|
|
{
|
|
|
|
|
if (IObuff[i-1] != ' ')
|
|
|
|
|
IObuff[i++] = ' ';
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// IObuf =~ "\(\k\|\i\).* ", thus i >= 2
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (p_js
|
|
|
|
|
&& (IObuff[i-2] == '.'
|
|
|
|
|
|| (vim_strchr(p_cpo, CPO_JOINSP) == NULL
|
|
|
|
|
&& (IObuff[i-2] == '?'
|
|
|
|
|
|| IObuff[i-2] == '!'))))
|
|
|
|
|
IObuff[i++] = ' ';
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// copy as much as possible of the new word
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (p - aux >= IOSIZE - i)
|
|
|
|
|
p = aux + IOSIZE - i - 1;
|
|
|
|
|
STRNCPY(IObuff + i, aux, p - aux);
|
|
|
|
|
i += (int)(p - aux);
|
2019-04-06 14:22:21 +02:00
|
|
|
|
cont_s_ipos = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
IObuff[i] = NUL;
|
|
|
|
|
aux = IObuff;
|
|
|
|
|
|
2005-07-29 22:36:03 +00:00
|
|
|
|
if (i == compl_length)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
goto exit_matched;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-29 14:30:35 +00:00
|
|
|
|
add_r = ins_compl_add_infercase(aux, i, p_ic,
|
2004-06-13 20:20:40 +00:00
|
|
|
|
curr_fname == curbuf->b_fname ? NULL : curr_fname,
|
2019-04-06 14:22:21 +02:00
|
|
|
|
dir, cont_s_ipos);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (add_r == OK)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// if dir was BACKWARD then honor it just once
|
2004-06-13 20:20:40 +00:00
|
|
|
|
dir = FORWARD;
|
2005-08-05 21:35:02 +00:00
|
|
|
|
else if (add_r == FAIL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-08-21 14:37:09 +02:00
|
|
|
|
else if (action == ACTION_SHOW_ALL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
found = TRUE;
|
|
|
|
|
if (!did_show)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
gotocmdline(TRUE); // cursor at status line
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (curr_fname != prev_fname)
|
|
|
|
|
{
|
|
|
|
|
if (did_show)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
msg_putchar('\n'); // cursor below last one
|
|
|
|
|
if (!got_int) // don't display if 'q' typed
|
|
|
|
|
// at "--more--" message
|
2004-06-13 20:20:40 +00:00
|
|
|
|
msg_home_replace_hl(curr_fname);
|
|
|
|
|
prev_fname = curr_fname;
|
|
|
|
|
}
|
|
|
|
|
did_show = TRUE;
|
|
|
|
|
if (!got_int)
|
|
|
|
|
show_pat_in_path(line, type, TRUE, action,
|
|
|
|
|
(depth == -1) ? NULL : files[depth].fp,
|
|
|
|
|
(depth == -1) ? &lnum : &files[depth].lnum,
|
|
|
|
|
match_count++);
|
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Set matched flag for this file and all the ones that
|
|
|
|
|
// include it
|
2004-06-13 20:20:40 +00:00
|
|
|
|
for (i = 0; i <= depth; ++i)
|
|
|
|
|
files[i].matched = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if (--count <= 0)
|
|
|
|
|
{
|
|
|
|
|
found = TRUE;
|
|
|
|
|
if (depth == -1 && lnum == curwin->w_cursor.lnum
|
2017-09-16 20:54:51 +02:00
|
|
|
|
#if defined(FEAT_QUICKFIX)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
&& g_do_tagpreview == 0
|
|
|
|
|
#endif
|
|
|
|
|
)
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_("E387: Match is on current line"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if (action == ACTION_SHOW)
|
|
|
|
|
{
|
|
|
|
|
show_pat_in_path(line, type, did_show, action,
|
|
|
|
|
(depth == -1) ? NULL : files[depth].fp,
|
|
|
|
|
(depth == -1) ? &lnum : &files[depth].lnum, 1L);
|
|
|
|
|
did_show = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEAT_GUI
|
|
|
|
|
need_mouse_correct = TRUE;
|
|
|
|
|
#endif
|
2017-09-16 20:54:51 +02:00
|
|
|
|
#if defined(FEAT_QUICKFIX)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// ":psearch" uses the preview window
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (g_do_tagpreview != 0)
|
|
|
|
|
{
|
|
|
|
|
curwin_save = curwin;
|
2019-08-18 15:25:17 +02:00
|
|
|
|
prepare_tagpreview(TRUE, TRUE, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (action == ACTION_SPLIT)
|
|
|
|
|
{
|
|
|
|
|
if (win_split(0, 0) == FAIL)
|
|
|
|
|
break;
|
2010-09-21 16:56:35 +02:00
|
|
|
|
RESET_BINDING(curwin);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (depth == -1)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// match in current file
|
2017-09-16 20:54:51 +02:00
|
|
|
|
#if defined(FEAT_QUICKFIX)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (g_do_tagpreview != 0)
|
|
|
|
|
{
|
2017-06-05 16:01:59 +02:00
|
|
|
|
if (!GETFILE_SUCCESS(getfile(
|
2017-07-23 22:02:02 +02:00
|
|
|
|
curwin_save->w_buffer->b_fnum, NULL,
|
2017-06-05 16:01:59 +02:00
|
|
|
|
NULL, TRUE, lnum, FALSE)))
|
2019-12-05 21:10:38 +01:00
|
|
|
|
break; // failed to jump to file
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
setpcmark();
|
|
|
|
|
curwin->w_cursor.lnum = lnum;
|
2017-07-23 22:02:02 +02:00
|
|
|
|
check_cursor();
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-05 16:01:59 +02:00
|
|
|
|
if (!GETFILE_SUCCESS(getfile(
|
|
|
|
|
0, files[depth].name, NULL, TRUE,
|
|
|
|
|
files[depth].lnum, FALSE)))
|
2019-12-05 21:10:38 +01:00
|
|
|
|
break; // failed to jump to file
|
|
|
|
|
// autocommands may have changed the lnum, we don't
|
|
|
|
|
// want that here
|
2004-06-13 20:20:40 +00:00
|
|
|
|
curwin->w_cursor.lnum = files[depth].lnum;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (action != ACTION_SHOW)
|
|
|
|
|
{
|
2009-04-22 14:44:41 +00:00
|
|
|
|
curwin->w_cursor.col = (colnr_T)(startp - line);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
curwin->w_set_curswant = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-16 20:54:51 +02:00
|
|
|
|
#if defined(FEAT_QUICKFIX)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (g_do_tagpreview != 0
|
2006-02-17 21:53:23 +00:00
|
|
|
|
&& curwin != curwin_save && win_valid(curwin_save))
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Return cursor to where we were
|
2004-06-13 20:20:40 +00:00
|
|
|
|
validate_cursor();
|
|
|
|
|
redraw_later(VALID);
|
|
|
|
|
win_enter(curwin_save, TRUE);
|
|
|
|
|
}
|
2019-11-30 22:48:27 +01:00
|
|
|
|
# ifdef FEAT_PROP_POPUP
|
2019-08-05 21:52:04 +02:00
|
|
|
|
else if (WIN_IS_POPUP(curwin))
|
|
|
|
|
// can't keep focus in popup window
|
|
|
|
|
win_enter(firstwin, TRUE);
|
|
|
|
|
# endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
exit_matched:
|
|
|
|
|
matched = FALSE;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// look for other matches in the rest of the line if we
|
|
|
|
|
// are not at the end of it already
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (def_regmatch.regprog == NULL
|
|
|
|
|
&& action == ACTION_EXPAND
|
2005-07-29 22:36:03 +00:00
|
|
|
|
&& !(compl_cont_status & CONT_SOL)
|
2009-04-22 14:44:41 +00:00
|
|
|
|
&& *startp != NUL
|
2019-10-06 22:00:13 +02:00
|
|
|
|
&& *(p = startp + mb_ptr2len(startp)) != NUL)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
goto search_line;
|
|
|
|
|
}
|
|
|
|
|
line_breakcheck();
|
|
|
|
|
if (action == ACTION_EXPAND)
|
2016-10-15 17:06:47 +02:00
|
|
|
|
ins_compl_check_keys(30, FALSE);
|
2019-03-30 13:53:47 +01:00
|
|
|
|
if (got_int || ins_compl_interrupted())
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Read the next line. When reading an included file and encountering
|
|
|
|
|
* end-of-file, close the file and continue in the file that included
|
|
|
|
|
* it.
|
|
|
|
|
*/
|
|
|
|
|
while (depth >= 0 && !already
|
|
|
|
|
&& vim_fgets(line = file_line, LSIZE, files[depth].fp))
|
|
|
|
|
{
|
|
|
|
|
fclose(files[depth].fp);
|
|
|
|
|
--old_files;
|
|
|
|
|
files[old_files].name = files[depth].name;
|
|
|
|
|
files[old_files].matched = files[depth].matched;
|
|
|
|
|
--depth;
|
|
|
|
|
curr_fname = (depth == -1) ? curbuf->b_fname
|
|
|
|
|
: files[depth].name;
|
|
|
|
|
if (depth < depth_displayed)
|
|
|
|
|
depth_displayed = depth;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (depth >= 0) // we could read the line
|
2013-07-03 22:28:36 +02:00
|
|
|
|
{
|
2004-06-13 20:20:40 +00:00
|
|
|
|
files[depth].lnum++;
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Remove any CR and LF from the line.
|
2013-07-03 22:28:36 +02:00
|
|
|
|
i = (int)STRLEN(line);
|
|
|
|
|
if (i > 0 && line[i - 1] == '\n')
|
|
|
|
|
line[--i] = NUL;
|
|
|
|
|
if (i > 0 && line[i - 1] == '\r')
|
|
|
|
|
line[--i] = NUL;
|
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if (!already)
|
|
|
|
|
{
|
|
|
|
|
if (++lnum > end_lnum)
|
|
|
|
|
break;
|
|
|
|
|
line = ml_get(lnum);
|
|
|
|
|
}
|
|
|
|
|
already = NULL;
|
|
|
|
|
}
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// End of big for (;;) loop.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Close any files that are still open.
|
2004-06-13 20:20:40 +00:00
|
|
|
|
for (i = 0; i <= depth; i++)
|
|
|
|
|
{
|
|
|
|
|
fclose(files[i].fp);
|
|
|
|
|
vim_free(files[i].name);
|
|
|
|
|
}
|
|
|
|
|
for (i = old_files; i < max_path_depth; i++)
|
|
|
|
|
vim_free(files[i].name);
|
|
|
|
|
vim_free(files);
|
|
|
|
|
|
|
|
|
|
if (type == CHECK_PATH)
|
|
|
|
|
{
|
|
|
|
|
if (!did_show)
|
|
|
|
|
{
|
|
|
|
|
if (action != ACTION_SHOW_ALL)
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg(_("All included files were found"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg(_("No included files"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-21 14:37:09 +02:00
|
|
|
|
else if (!found && action != ACTION_EXPAND)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-03-30 13:53:47 +01:00
|
|
|
|
if (got_int || ins_compl_interrupted())
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_(e_interr));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else if (type == FIND_DEFINE)
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_("E388: Couldn't find definition"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
else
|
2019-01-13 23:38:42 +01:00
|
|
|
|
emsg(_("E389: Couldn't find pattern"));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
|
|
|
|
|
msg_end();
|
|
|
|
|
|
|
|
|
|
fpip_end:
|
|
|
|
|
vim_free(file_line);
|
2013-06-08 18:19:48 +02:00
|
|
|
|
vim_regfree(regmatch.regprog);
|
|
|
|
|
vim_regfree(incl_regmatch.regprog);
|
|
|
|
|
vim_regfree(def_regmatch.regprog);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-01-30 21:10:09 +01:00
|
|
|
|
show_pat_in_path(
|
|
|
|
|
char_u *line,
|
|
|
|
|
int type,
|
|
|
|
|
int did_show,
|
|
|
|
|
int action,
|
|
|
|
|
FILE *fp,
|
|
|
|
|
linenr_T *lnum,
|
|
|
|
|
long count)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
|
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
|
|
if (did_show)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
msg_putchar('\n'); // cursor below last one
|
2006-05-05 21:15:17 +00:00
|
|
|
|
else if (!msg_silent)
|
2019-12-05 21:10:38 +01:00
|
|
|
|
gotocmdline(TRUE); // cursor at status line
|
|
|
|
|
if (got_int) // 'q' typed at "--more--" message
|
2004-06-13 20:20:40 +00:00
|
|
|
|
return;
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
p = line + STRLEN(line) - 1;
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// We used fgets(), so get rid of newline at end
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (p >= line && *p == '\n')
|
|
|
|
|
--p;
|
|
|
|
|
if (p >= line && *p == '\r')
|
|
|
|
|
--p;
|
|
|
|
|
*(p + 1) = NUL;
|
|
|
|
|
}
|
|
|
|
|
if (action == ACTION_SHOW_ALL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
sprintf((char *)IObuff, "%3ld: ", count); // show match nr
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts((char *)IObuff);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
|
|
|
|
|
// Highlight line numbers
|
2019-01-19 17:43:09 +01:00
|
|
|
|
msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
|
|
|
|
|
msg_puts(" ");
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2005-02-22 08:49:11 +00:00
|
|
|
|
msg_prt_line(line, FALSE);
|
2019-12-05 21:10:38 +01:00
|
|
|
|
out_flush(); // show one line at a time
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
2019-12-05 21:10:38 +01:00
|
|
|
|
// Definition continues until line that doesn't end with '\'
|
2004-06-13 20:20:40 +00:00
|
|
|
|
if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
2019-12-05 21:10:38 +01:00
|
|
|
|
if (vim_fgets(line, LSIZE, fp)) // end of file
|
2004-06-13 20:20:40 +00:00
|
|
|
|
break;
|
|
|
|
|
++*lnum;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (++*lnum > curbuf->b_ml.ml_line_count)
|
|
|
|
|
break;
|
|
|
|
|
line = ml_get(*lnum);
|
|
|
|
|
}
|
|
|
|
|
msg_putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef FEAT_VIMINFO
|
2019-10-09 22:01:25 +02:00
|
|
|
|
/*
|
|
|
|
|
* Return the last used search pattern at "idx".
|
|
|
|
|
*/
|
2019-07-23 22:15:25 +02:00
|
|
|
|
spat_T *
|
|
|
|
|
get_spat(int idx)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-07-23 22:15:25 +02:00
|
|
|
|
return &spats[idx];
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 22:01:25 +02:00
|
|
|
|
/*
|
|
|
|
|
* Return the last used search pattern index.
|
|
|
|
|
*/
|
2019-07-23 22:15:25 +02:00
|
|
|
|
int
|
|
|
|
|
get_spat_last_idx(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
|
{
|
2019-07-23 22:15:25 +02:00
|
|
|
|
return last_idx;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
}
|
2019-07-23 22:15:25 +02:00
|
|
|
|
#endif
|
2020-06-01 17:28:35 +02:00
|
|
|
|
|
|
|
|
|
#ifdef FEAT_EVAL
|
|
|
|
|
/*
|
|
|
|
|
* "searchcount()" function
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
f_searchcount(typval_T *argvars, typval_T *rettv)
|
|
|
|
|
{
|
|
|
|
|
pos_T pos = curwin->w_cursor;
|
|
|
|
|
char_u *pattern = NULL;
|
|
|
|
|
int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
|
|
|
|
|
long timeout = SEARCH_STAT_DEF_TIMEOUT;
|
2020-09-05 23:16:00 +02:00
|
|
|
|
int recompute = TRUE;
|
2020-06-01 17:28:35 +02:00
|
|
|
|
searchstat_T stat;
|
|
|
|
|
|
|
|
|
|
if (rettv_dict_alloc(rettv) == FAIL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
|
|
|
|
|
recompute = TRUE;
|
|
|
|
|
|
|
|
|
|
if (argvars[0].v_type != VAR_UNKNOWN)
|
|
|
|
|
{
|
2020-06-03 22:57:39 +02:00
|
|
|
|
dict_T *dict;
|
2020-06-01 17:28:35 +02:00
|
|
|
|
dictitem_T *di;
|
|
|
|
|
listitem_T *li;
|
|
|
|
|
int error = FALSE;
|
|
|
|
|
|
2020-06-03 22:57:39 +02:00
|
|
|
|
if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
|
|
|
|
|
{
|
|
|
|
|
emsg(_(e_dictreq));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dict = argvars[0].vval.v_dict;
|
2020-06-01 17:28:35 +02:00
|
|
|
|
di = dict_find(dict, (char_u *)"timeout", -1);
|
|
|
|
|
if (di != NULL)
|
|
|
|
|
{
|
|
|
|
|
timeout = (long)tv_get_number_chk(&di->di_tv, &error);
|
|
|
|
|
if (error)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
di = dict_find(dict, (char_u *)"maxcount", -1);
|
|
|
|
|
if (di != NULL)
|
|
|
|
|
{
|
|
|
|
|
maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
|
|
|
|
|
if (error)
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-05 21:21:16 +02:00
|
|
|
|
recompute = dict_get_bool(dict, (char_u *)"recompute", recompute);
|
2020-06-01 17:28:35 +02:00
|
|
|
|
di = dict_find(dict, (char_u *)"pattern", -1);
|
|
|
|
|
if (di != NULL)
|
|
|
|
|
{
|
|
|
|
|
pattern = tv_get_string_chk(&di->di_tv);
|
|
|
|
|
if (pattern == NULL)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
di = dict_find(dict, (char_u *)"pos", -1);
|
|
|
|
|
if (di != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (di->di_tv.v_type != VAR_LIST)
|
|
|
|
|
{
|
|
|
|
|
semsg(_(e_invarg2), "pos");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (list_len(di->di_tv.vval.v_list) != 3)
|
|
|
|
|
{
|
|
|
|
|
semsg(_(e_invarg2), "List format should be [lnum, col, off]");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
li = list_find(di->di_tv.vval.v_list, 0L);
|
|
|
|
|
if (li != NULL)
|
|
|
|
|
{
|
|
|
|
|
pos.lnum = tv_get_number_chk(&li->li_tv, &error);
|
|
|
|
|
if (error)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
li = list_find(di->di_tv.vval.v_list, 1L);
|
|
|
|
|
if (li != NULL)
|
|
|
|
|
{
|
|
|
|
|
pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
|
|
|
|
|
if (error)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
li = list_find(di->di_tv.vval.v_list, 2L);
|
|
|
|
|
if (li != NULL)
|
|
|
|
|
{
|
|
|
|
|
pos.coladd = tv_get_number_chk(&li->li_tv, &error);
|
|
|
|
|
if (error)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
save_last_search_pattern();
|
|
|
|
|
if (pattern != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (*pattern == NUL)
|
|
|
|
|
goto the_end;
|
2020-06-01 19:08:54 +02:00
|
|
|
|
vim_free(spats[last_idx].pat);
|
2020-06-01 17:28:35 +02:00
|
|
|
|
spats[last_idx].pat = vim_strsave(pattern);
|
|
|
|
|
}
|
|
|
|
|
if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
|
|
|
|
|
goto the_end; // the previous pattern was never defined
|
|
|
|
|
|
|
|
|
|
update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
|
|
|
|
|
|
|
|
|
|
dict_add_number(rettv->vval.v_dict, "current", stat.cur);
|
|
|
|
|
dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
|
|
|
|
|
dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
|
|
|
|
|
dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
|
|
|
|
|
dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
|
|
|
|
|
|
|
|
|
|
the_end:
|
|
|
|
|
restore_last_search_pattern();
|
|
|
|
|
}
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fuzzy string matching
|
|
|
|
|
*
|
|
|
|
|
* Ported from the lib_fts library authored by Forrest Smith.
|
|
|
|
|
* https://github.com/forrestthewoods/lib_fts/tree/master/code
|
|
|
|
|
*
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* The following blog describes the fuzzy matching algorithm:
|
2020-09-11 22:25:15 +02:00
|
|
|
|
* https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
|
|
|
|
|
*
|
|
|
|
|
* Each matching string is assigned a score. The following factors are checked:
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* - Matched letter
|
|
|
|
|
* - Unmatched letter
|
|
|
|
|
* - Consecutively matched letters
|
|
|
|
|
* - Proximity to start
|
|
|
|
|
* - Letter following a separator (space, underscore)
|
|
|
|
|
* - Uppercase letter following lowercase (aka CamelCase)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
*
|
|
|
|
|
* Matched letters are good. Unmatched letters are bad. Matching near the start
|
|
|
|
|
* is good. Matching the first letter in the middle of a phrase is good.
|
|
|
|
|
* Matching the uppercase letters in camel case entries is good.
|
|
|
|
|
*
|
|
|
|
|
* The score assigned for each factor is explained below.
|
|
|
|
|
* File paths are different from file names. File extensions may be ignorable.
|
|
|
|
|
* Single words care about consecutive matches but not separators or camel
|
|
|
|
|
* case.
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* Score starts at 100
|
2020-09-11 22:25:15 +02:00
|
|
|
|
* Matched letter: +0 points
|
|
|
|
|
* Unmatched letter: -1 point
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* Consecutive match bonus: +15 points
|
|
|
|
|
* First letter bonus: +15 points
|
|
|
|
|
* Separator bonus: +30 points
|
|
|
|
|
* Camel case bonus: +30 points
|
|
|
|
|
* Unmatched leading letter: -5 points (max: -15)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
*
|
|
|
|
|
* There is some nuance to this. Scores don’t have an intrinsic meaning. The
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
|
2020-09-11 22:25:15 +02:00
|
|
|
|
* lower minimum score due to unmatched letter penalty. Longer search patterns
|
|
|
|
|
* have a higher maximum score due to match bonuses.
|
|
|
|
|
*
|
|
|
|
|
* Separator and camel case bonus is worth a LOT. Consecutive matches are worth
|
|
|
|
|
* quite a bit.
|
|
|
|
|
*
|
|
|
|
|
* There is a penalty if you DON’T match the first three letters. Which
|
|
|
|
|
* effectively rewards matching near the start. However there’s no difference
|
|
|
|
|
* in matching between the middle and end.
|
|
|
|
|
*
|
|
|
|
|
* There is not an explicit bonus for an exact match. Unmatched letters receive
|
|
|
|
|
* a penalty. So shorter strings and closer matches are worth more.
|
|
|
|
|
*/
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2020-10-23 16:50:30 +02:00
|
|
|
|
int idx; // used for stable sort
|
2020-09-22 20:33:50 +02:00
|
|
|
|
listitem_T *item;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
int score;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
list_T *lmatchpos;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
} fuzzyItem_T;
|
|
|
|
|
|
2020-10-20 19:01:30 +02:00
|
|
|
|
// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
|
|
|
|
|
// matching a whole word is preferred.
|
|
|
|
|
#define SEQUENTIAL_BONUS 40
|
2020-10-29 18:58:01 +01:00
|
|
|
|
// bonus if match occurs after a path separator
|
|
|
|
|
#define PATH_SEPARATOR_BONUS 30
|
|
|
|
|
// bonus if match occurs after a word separator
|
|
|
|
|
#define WORD_SEPARATOR_BONUS 25
|
2020-09-22 20:33:50 +02:00
|
|
|
|
// bonus if match is uppercase and prev is lower
|
|
|
|
|
#define CAMEL_BONUS 30
|
|
|
|
|
// bonus if the first letter is matched
|
|
|
|
|
#define FIRST_LETTER_BONUS 15
|
|
|
|
|
// penalty applied for every letter in str before the first match
|
|
|
|
|
#define LEADING_LETTER_PENALTY -5
|
|
|
|
|
// maximum penalty for leading letters
|
|
|
|
|
#define MAX_LEADING_LETTER_PENALTY -15
|
|
|
|
|
// penalty for every letter that doesn't match
|
|
|
|
|
#define UNMATCHED_LETTER_PENALTY -1
|
2020-10-23 16:50:30 +02:00
|
|
|
|
// penalty for gap in matching positions (-2 * k)
|
|
|
|
|
#define GAP_PENALTY -2
|
2020-09-22 20:33:50 +02:00
|
|
|
|
// Score for a string that doesn't fuzzy match the pattern
|
|
|
|
|
#define SCORE_NONE -9999
|
|
|
|
|
|
|
|
|
|
#define FUZZY_MATCH_RECURSION_LIMIT 10
|
|
|
|
|
// Maximum number of characters that can be fuzzy matched
|
|
|
|
|
#define MAXMATCHES 256
|
|
|
|
|
|
|
|
|
|
typedef int_u matchidx_T;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Compute a score for a fuzzy matched string. The matching character locations
|
|
|
|
|
* are in 'matches'.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
fuzzy_match_compute_score(
|
|
|
|
|
char_u *str,
|
|
|
|
|
int strSz,
|
|
|
|
|
matchidx_T *matches,
|
|
|
|
|
int numMatches)
|
|
|
|
|
{
|
|
|
|
|
int score;
|
|
|
|
|
int penalty;
|
|
|
|
|
int unmatched;
|
|
|
|
|
int i;
|
|
|
|
|
char_u *p = str;
|
|
|
|
|
matchidx_T sidx = 0;
|
|
|
|
|
|
|
|
|
|
// Initialize score
|
|
|
|
|
score = 100;
|
|
|
|
|
|
|
|
|
|
// Apply leading letter penalty
|
|
|
|
|
penalty = LEADING_LETTER_PENALTY * matches[0];
|
|
|
|
|
if (penalty < MAX_LEADING_LETTER_PENALTY)
|
|
|
|
|
penalty = MAX_LEADING_LETTER_PENALTY;
|
|
|
|
|
score += penalty;
|
|
|
|
|
|
|
|
|
|
// Apply unmatched penalty
|
|
|
|
|
unmatched = strSz - numMatches;
|
|
|
|
|
score += UNMATCHED_LETTER_PENALTY * unmatched;
|
|
|
|
|
|
|
|
|
|
// Apply ordering bonuses
|
|
|
|
|
for (i = 0; i < numMatches; ++i)
|
|
|
|
|
{
|
|
|
|
|
matchidx_T currIdx = matches[i];
|
|
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
|
{
|
|
|
|
|
matchidx_T prevIdx = matches[i - 1];
|
|
|
|
|
|
|
|
|
|
// Sequential
|
|
|
|
|
if (currIdx == (prevIdx + 1))
|
|
|
|
|
score += SEQUENTIAL_BONUS;
|
2020-10-23 16:50:30 +02:00
|
|
|
|
else
|
|
|
|
|
score += GAP_PENALTY * (currIdx - prevIdx);
|
2020-09-22 20:33:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for bonuses based on neighbor character value
|
|
|
|
|
if (currIdx > 0)
|
|
|
|
|
{
|
|
|
|
|
// Camel case
|
2020-09-22 22:08:32 +02:00
|
|
|
|
int neighbor = ' ';
|
2020-09-22 20:33:50 +02:00
|
|
|
|
int curr;
|
|
|
|
|
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
{
|
|
|
|
|
while (sidx < currIdx)
|
|
|
|
|
{
|
|
|
|
|
neighbor = (*mb_ptr2char)(p);
|
2020-10-23 16:50:30 +02:00
|
|
|
|
MB_PTR_ADV(p);
|
2020-09-22 20:33:50 +02:00
|
|
|
|
sidx++;
|
|
|
|
|
}
|
|
|
|
|
curr = (*mb_ptr2char)(p);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
neighbor = str[currIdx - 1];
|
|
|
|
|
curr = str[currIdx];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vim_islower(neighbor) && vim_isupper(curr))
|
|
|
|
|
score += CAMEL_BONUS;
|
|
|
|
|
|
2020-10-29 18:58:01 +01:00
|
|
|
|
// Bonus if the match follows a separator character
|
|
|
|
|
if (neighbor == '/' || neighbor == '\\')
|
|
|
|
|
score += PATH_SEPARATOR_BONUS;
|
|
|
|
|
else if (neighbor == ' ' || neighbor == '_')
|
|
|
|
|
score += WORD_SEPARATOR_BONUS;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// First letter
|
|
|
|
|
score += FIRST_LETTER_BONUS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 16:50:30 +02:00
|
|
|
|
/*
|
|
|
|
|
* Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
|
|
|
|
|
* Return the number of matching characters.
|
|
|
|
|
*/
|
2020-09-11 22:25:15 +02:00
|
|
|
|
static int
|
|
|
|
|
fuzzy_match_recursive(
|
2020-09-22 20:33:50 +02:00
|
|
|
|
char_u *fuzpat,
|
|
|
|
|
char_u *str,
|
|
|
|
|
matchidx_T strIdx,
|
|
|
|
|
int *outScore,
|
|
|
|
|
char_u *strBegin,
|
|
|
|
|
int strLen,
|
|
|
|
|
matchidx_T *srcMatches,
|
|
|
|
|
matchidx_T *matches,
|
|
|
|
|
int maxMatches,
|
|
|
|
|
int nextMatch,
|
|
|
|
|
int *recursionCount)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
|
|
|
|
// Recursion params
|
|
|
|
|
int recursiveMatch = FALSE;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
matchidx_T bestRecursiveMatches[MAXMATCHES];
|
2020-09-11 22:25:15 +02:00
|
|
|
|
int bestRecursiveScore = 0;
|
|
|
|
|
int first_match;
|
|
|
|
|
int matched;
|
|
|
|
|
|
|
|
|
|
// Count recursions
|
|
|
|
|
++*recursionCount;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
|
2020-10-23 16:50:30 +02:00
|
|
|
|
return 0;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
// Detect end of strings
|
|
|
|
|
if (*fuzpat == '\0' || *str == '\0')
|
2020-10-23 16:50:30 +02:00
|
|
|
|
return 0;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
// Loop through fuzpat and str looking for a match
|
|
|
|
|
first_match = TRUE;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
while (*fuzpat != NUL && *str != NUL)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
int c1;
|
|
|
|
|
int c2;
|
|
|
|
|
|
|
|
|
|
c1 = PTR2CHAR(fuzpat);
|
|
|
|
|
c2 = PTR2CHAR(str);
|
|
|
|
|
|
2020-09-11 22:25:15 +02:00
|
|
|
|
// Found match
|
2020-09-22 20:33:50 +02:00
|
|
|
|
if (vim_tolower(c1) == vim_tolower(c2))
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
matchidx_T recursiveMatches[MAXMATCHES];
|
2020-09-11 22:25:15 +02:00
|
|
|
|
int recursiveScore = 0;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
char_u *next_char;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
// Supplied matches buffer was too short
|
|
|
|
|
if (nextMatch >= maxMatches)
|
2020-10-23 16:50:30 +02:00
|
|
|
|
return 0;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
// "Copy-on-Write" srcMatches into matches
|
|
|
|
|
if (first_match && srcMatches)
|
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
|
2020-09-11 22:25:15 +02:00
|
|
|
|
first_match = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recursive call that "skips" this match
|
2020-09-22 20:33:50 +02:00
|
|
|
|
if (has_mbyte)
|
|
|
|
|
next_char = str + (*mb_ptr2len)(str);
|
|
|
|
|
else
|
|
|
|
|
next_char = str + 1;
|
|
|
|
|
if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
|
|
|
|
|
&recursiveScore, strBegin, strLen, matches,
|
|
|
|
|
recursiveMatches,
|
|
|
|
|
sizeof(recursiveMatches)/sizeof(recursiveMatches[0]),
|
|
|
|
|
nextMatch, recursionCount))
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
|
|
|
|
// Pick best recursive score
|
|
|
|
|
if (!recursiveMatch || recursiveScore > bestRecursiveScore)
|
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
memcpy(bestRecursiveMatches, recursiveMatches,
|
|
|
|
|
MAXMATCHES * sizeof(recursiveMatches[0]));
|
2020-09-11 22:25:15 +02:00
|
|
|
|
bestRecursiveScore = recursiveScore;
|
|
|
|
|
}
|
|
|
|
|
recursiveMatch = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Advance
|
2020-09-22 20:33:50 +02:00
|
|
|
|
matches[nextMatch++] = strIdx;
|
|
|
|
|
if (has_mbyte)
|
2020-10-23 16:50:30 +02:00
|
|
|
|
MB_PTR_ADV(fuzpat);
|
2020-09-22 20:33:50 +02:00
|
|
|
|
else
|
|
|
|
|
++fuzpat;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
2020-09-22 20:33:50 +02:00
|
|
|
|
if (has_mbyte)
|
2020-10-23 16:50:30 +02:00
|
|
|
|
MB_PTR_ADV(str);
|
2020-09-22 20:33:50 +02:00
|
|
|
|
else
|
|
|
|
|
++str;
|
|
|
|
|
strIdx++;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine if full fuzpat was matched
|
2020-09-22 20:33:50 +02:00
|
|
|
|
matched = *fuzpat == NUL ? TRUE : FALSE;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
// Calculate score
|
|
|
|
|
if (matched)
|
2020-09-22 20:33:50 +02:00
|
|
|
|
*outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
|
|
|
|
|
nextMatch);
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
// Return best result
|
|
|
|
|
if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
|
|
|
|
|
{
|
|
|
|
|
// Recursive score is better than "this"
|
2020-09-22 20:33:50 +02:00
|
|
|
|
memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
|
2020-09-11 22:25:15 +02:00
|
|
|
|
*outScore = bestRecursiveScore;
|
2020-10-23 16:50:30 +02:00
|
|
|
|
return nextMatch;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
else if (matched)
|
2020-10-23 16:50:30 +02:00
|
|
|
|
return nextMatch; // "this" score is better than recursive
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
2020-10-23 16:50:30 +02:00
|
|
|
|
return 0; // no match
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* fuzzy_match()
|
|
|
|
|
*
|
|
|
|
|
* Performs exhaustive search via recursion to find all possible matches and
|
|
|
|
|
* match with highest score.
|
|
|
|
|
* Scores values have no intrinsic meaning. Possible score range is not
|
|
|
|
|
* normalized and varies with pattern.
|
|
|
|
|
* Recursion is limited internally (default=10) to prevent degenerate cases
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
|
2020-09-22 20:33:50 +02:00
|
|
|
|
* Uses char_u for match indices. Therefore patterns are limited to MAXMATCHES
|
2020-09-11 22:25:15 +02:00
|
|
|
|
* characters.
|
|
|
|
|
*
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* Returns TRUE if 'pat_arg' matches 'str'. Also returns the match score in
|
2020-09-22 20:33:50 +02:00
|
|
|
|
* 'outScore' and the matching character positions in 'matches'.
|
2020-09-11 22:25:15 +02:00
|
|
|
|
*/
|
|
|
|
|
static int
|
2020-09-22 20:33:50 +02:00
|
|
|
|
fuzzy_match(
|
|
|
|
|
char_u *str,
|
2020-10-23 16:50:30 +02:00
|
|
|
|
char_u *pat_arg,
|
|
|
|
|
int matchseq,
|
2020-09-22 20:33:50 +02:00
|
|
|
|
int *outScore,
|
|
|
|
|
matchidx_T *matches,
|
|
|
|
|
int maxMatches)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
|
|
|
|
int recursionCount = 0;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
int len = MB_CHARLEN(str);
|
2020-10-23 16:50:30 +02:00
|
|
|
|
char_u *save_pat;
|
|
|
|
|
char_u *pat;
|
|
|
|
|
char_u *p;
|
|
|
|
|
int complete = FALSE;
|
|
|
|
|
int score = 0;
|
|
|
|
|
int numMatches = 0;
|
|
|
|
|
int matchCount;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
|
|
|
|
*outScore = 0;
|
|
|
|
|
|
2020-10-23 16:50:30 +02:00
|
|
|
|
save_pat = vim_strsave(pat_arg);
|
|
|
|
|
if (save_pat == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
pat = save_pat;
|
|
|
|
|
p = pat;
|
|
|
|
|
|
|
|
|
|
// Try matching each word in 'pat_arg' in 'str'
|
|
|
|
|
while (TRUE)
|
|
|
|
|
{
|
|
|
|
|
if (matchseq)
|
|
|
|
|
complete = TRUE;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Extract one word from the pattern (separated by space)
|
|
|
|
|
p = skipwhite(p);
|
|
|
|
|
if (*p == NUL)
|
|
|
|
|
break;
|
|
|
|
|
pat = p;
|
|
|
|
|
while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
|
|
|
|
|
{
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
MB_PTR_ADV(p);
|
|
|
|
|
else
|
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
if (*p == NUL) // processed all the words
|
|
|
|
|
complete = TRUE;
|
|
|
|
|
*p = NUL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
score = 0;
|
|
|
|
|
recursionCount = 0;
|
|
|
|
|
matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
|
|
|
|
|
matches + numMatches, maxMatches - numMatches,
|
|
|
|
|
0, &recursionCount);
|
|
|
|
|
if (matchCount == 0)
|
|
|
|
|
{
|
|
|
|
|
numMatches = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accumulate the match score and the number of matches
|
|
|
|
|
*outScore += score;
|
|
|
|
|
numMatches += matchCount;
|
|
|
|
|
|
|
|
|
|
if (complete)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// try matching the next word
|
|
|
|
|
++p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vim_free(save_pat);
|
|
|
|
|
return numMatches != 0;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Sort the fuzzy matches in the descending order of the match score.
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* For items with same score, retain the order using the index (stable sort)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
*/
|
|
|
|
|
static int
|
2020-10-23 16:50:30 +02:00
|
|
|
|
fuzzy_match_item_compare(const void *s1, const void *s2)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
|
|
|
|
int v1 = ((fuzzyItem_T *)s1)->score;
|
|
|
|
|
int v2 = ((fuzzyItem_T *)s2)->score;
|
2020-10-23 16:50:30 +02:00
|
|
|
|
int idx1 = ((fuzzyItem_T *)s1)->idx;
|
|
|
|
|
int idx2 = ((fuzzyItem_T *)s2)->idx;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
2020-10-23 16:50:30 +02:00
|
|
|
|
return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2020-09-22 20:33:50 +02:00
|
|
|
|
* Fuzzy search the string 'str' in a list of 'items' and return the matching
|
|
|
|
|
* strings in 'fmatchlist'.
|
2020-10-23 16:50:30 +02:00
|
|
|
|
* If 'matchseq' is TRUE, then for multi-word search strings, match all the
|
|
|
|
|
* words in sequence.
|
2020-09-22 20:33:50 +02:00
|
|
|
|
* If 'items' is a list of strings, then search for 'str' in the list.
|
|
|
|
|
* If 'items' is a list of dicts, then either use 'key' to lookup the string
|
|
|
|
|
* for each item or use 'item_cb' Funcref function to get the string.
|
|
|
|
|
* If 'retmatchpos' is TRUE, then return a list of positions where 'str'
|
|
|
|
|
* matches for each item.
|
2020-09-11 22:25:15 +02:00
|
|
|
|
*/
|
|
|
|
|
static void
|
2020-10-23 16:50:30 +02:00
|
|
|
|
fuzzy_match_in_list(
|
2020-09-22 20:33:50 +02:00
|
|
|
|
list_T *items,
|
|
|
|
|
char_u *str,
|
2020-10-23 16:50:30 +02:00
|
|
|
|
int matchseq,
|
2020-09-22 20:33:50 +02:00
|
|
|
|
char_u *key,
|
|
|
|
|
callback_T *item_cb,
|
|
|
|
|
int retmatchpos,
|
|
|
|
|
list_T *fmatchlist)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
|
|
|
|
long len;
|
|
|
|
|
fuzzyItem_T *ptrs;
|
|
|
|
|
listitem_T *li;
|
|
|
|
|
long i = 0;
|
|
|
|
|
int found_match = FALSE;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
matchidx_T matches[MAXMATCHES];
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
2020-09-22 20:33:50 +02:00
|
|
|
|
len = list_len(items);
|
2020-09-11 22:25:15 +02:00
|
|
|
|
if (len == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-09-22 20:33:50 +02:00
|
|
|
|
ptrs = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
|
2020-09-11 22:25:15 +02:00
|
|
|
|
if (ptrs == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-09-22 20:33:50 +02:00
|
|
|
|
// For all the string items in items, get the fuzzy matching score
|
|
|
|
|
FOR_ALL_LIST_ITEMS(items, li)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
int score;
|
|
|
|
|
char_u *itemstr;
|
|
|
|
|
typval_T rettv;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
2020-10-23 16:50:30 +02:00
|
|
|
|
ptrs[i].idx = i;
|
2020-09-11 22:25:15 +02:00
|
|
|
|
ptrs[i].item = li;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
ptrs[i].score = SCORE_NONE;
|
|
|
|
|
itemstr = NULL;
|
|
|
|
|
rettv.v_type = VAR_UNKNOWN;
|
|
|
|
|
if (li->li_tv.v_type == VAR_STRING) // list of strings
|
|
|
|
|
itemstr = li->li_tv.vval.v_string;
|
|
|
|
|
else if (li->li_tv.v_type == VAR_DICT &&
|
|
|
|
|
(key != NULL || item_cb->cb_name != NULL))
|
|
|
|
|
{
|
|
|
|
|
// For a dict, either use the specified key to lookup the string or
|
|
|
|
|
// use the specified callback function to get the string.
|
|
|
|
|
if (key != NULL)
|
|
|
|
|
itemstr = dict_get_string(li->li_tv.vval.v_dict, key, FALSE);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
typval_T argv[2];
|
|
|
|
|
|
|
|
|
|
// Invoke the supplied callback (if any) to get the dict item
|
|
|
|
|
li->li_tv.vval.v_dict->dv_refcount++;
|
|
|
|
|
argv[0].v_type = VAR_DICT;
|
|
|
|
|
argv[0].vval.v_dict = li->li_tv.vval.v_dict;
|
|
|
|
|
argv[1].v_type = VAR_UNKNOWN;
|
|
|
|
|
if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
|
|
|
|
|
{
|
|
|
|
|
if (rettv.v_type == VAR_STRING)
|
|
|
|
|
itemstr = rettv.vval.v_string;
|
|
|
|
|
}
|
|
|
|
|
dict_unref(li->li_tv.vval.v_dict);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (itemstr != NULL
|
2020-10-23 16:50:30 +02:00
|
|
|
|
&& fuzzy_match(itemstr, str, matchseq, &score, matches,
|
2020-09-22 20:33:50 +02:00
|
|
|
|
sizeof(matches) / sizeof(matches[0])))
|
|
|
|
|
{
|
|
|
|
|
// Copy the list of matching positions in itemstr to a list, if
|
|
|
|
|
// 'retmatchpos' is set.
|
|
|
|
|
if (retmatchpos)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
2020-10-23 16:50:30 +02:00
|
|
|
|
int j = 0;
|
|
|
|
|
char_u *p;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
|
|
|
|
|
ptrs[i].lmatchpos = list_alloc();
|
|
|
|
|
if (ptrs[i].lmatchpos == NULL)
|
|
|
|
|
goto done;
|
2020-10-23 16:50:30 +02:00
|
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
|
while (*p != NUL)
|
2020-09-22 20:33:50 +02:00
|
|
|
|
{
|
2020-10-23 16:50:30 +02:00
|
|
|
|
if (!VIM_ISWHITE(PTR2CHAR(p)))
|
|
|
|
|
{
|
|
|
|
|
if (list_append_number(ptrs[i].lmatchpos,
|
|
|
|
|
matches[j]) == FAIL)
|
|
|
|
|
goto done;
|
|
|
|
|
j++;
|
|
|
|
|
}
|
|
|
|
|
if (has_mbyte)
|
|
|
|
|
MB_PTR_ADV(p);
|
|
|
|
|
else
|
|
|
|
|
++p;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
}
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
2020-09-22 20:33:50 +02:00
|
|
|
|
ptrs[i].score = score;
|
|
|
|
|
found_match = TRUE;
|
|
|
|
|
}
|
2020-09-11 22:25:15 +02:00
|
|
|
|
++i;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
clear_tv(&rettv);
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (found_match)
|
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
list_T *l;
|
|
|
|
|
|
2020-09-11 22:25:15 +02:00
|
|
|
|
// Sort the list by the descending order of the match score
|
|
|
|
|
qsort((void *)ptrs, (size_t)len, sizeof(fuzzyItem_T),
|
2020-10-23 16:50:30 +02:00
|
|
|
|
fuzzy_match_item_compare);
|
2020-09-11 22:25:15 +02:00
|
|
|
|
|
2020-09-22 20:33:50 +02:00
|
|
|
|
// For matchfuzzy(), return a list of matched strings.
|
|
|
|
|
// ['str1', 'str2', 'str3']
|
2021-01-02 18:31:32 +01:00
|
|
|
|
// For matchfuzzypos(), return a list with three items.
|
2020-09-22 20:33:50 +02:00
|
|
|
|
// The first item is a list of matched strings. The second item
|
|
|
|
|
// is a list of lists where each list item is a list of matched
|
2021-01-02 18:31:32 +01:00
|
|
|
|
// character positions. The third item is a list of matching scores.
|
2020-09-22 20:33:50 +02:00
|
|
|
|
// [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
|
|
|
|
|
if (retmatchpos)
|
|
|
|
|
{
|
|
|
|
|
li = list_find(fmatchlist, 0);
|
|
|
|
|
if (li == NULL || li->li_tv.vval.v_list == NULL)
|
|
|
|
|
goto done;
|
|
|
|
|
l = li->li_tv.vval.v_list;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
l = fmatchlist;
|
|
|
|
|
|
|
|
|
|
// Copy the matching strings with a valid score to the return list
|
2020-09-11 22:25:15 +02:00
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
if (ptrs[i].score == SCORE_NONE)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
break;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
list_append_tv(l, &ptrs[i].item->li_tv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// next copy the list of matching positions
|
|
|
|
|
if (retmatchpos)
|
|
|
|
|
{
|
2021-01-02 18:31:32 +01:00
|
|
|
|
li = list_find(fmatchlist, -2);
|
2020-09-22 20:33:50 +02:00
|
|
|
|
if (li == NULL || li->li_tv.vval.v_list == NULL)
|
|
|
|
|
goto done;
|
|
|
|
|
l = li->li_tv.vval.v_list;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
if (ptrs[i].score == SCORE_NONE)
|
|
|
|
|
break;
|
|
|
|
|
if (ptrs[i].lmatchpos != NULL &&
|
|
|
|
|
list_append_list(l, ptrs[i].lmatchpos) == FAIL)
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
2021-01-02 18:31:32 +01:00
|
|
|
|
|
|
|
|
|
// copy the matching scores
|
|
|
|
|
li = list_find(fmatchlist, -1);
|
|
|
|
|
if (li == NULL || li->li_tv.vval.v_list == NULL)
|
|
|
|
|
goto done;
|
|
|
|
|
l = li->li_tv.vval.v_list;
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
if (ptrs[i].score == SCORE_NONE)
|
|
|
|
|
break;
|
|
|
|
|
if (list_append_number(l, ptrs[i].score) == FAIL)
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 20:33:50 +02:00
|
|
|
|
done:
|
2020-09-11 22:25:15 +02:00
|
|
|
|
vim_free(ptrs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2020-09-22 20:33:50 +02:00
|
|
|
|
* Do fuzzy matching. Returns the list of matched strings in 'rettv'.
|
|
|
|
|
* If 'retmatchpos' is TRUE, also returns the matching character positions.
|
2020-09-11 22:25:15 +02:00
|
|
|
|
*/
|
2020-09-22 20:33:50 +02:00
|
|
|
|
static void
|
|
|
|
|
do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
callback_T cb;
|
|
|
|
|
char_u *key = NULL;
|
|
|
|
|
int ret;
|
2020-10-23 16:50:30 +02:00
|
|
|
|
int matchseq = FALSE;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
|
|
|
|
|
CLEAR_POINTER(&cb);
|
|
|
|
|
|
|
|
|
|
// validate and get the arguments
|
|
|
|
|
if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
|
2020-09-11 22:25:15 +02:00
|
|
|
|
{
|
2020-09-22 20:33:50 +02:00
|
|
|
|
semsg(_(e_listarg), retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
|
2020-09-11 22:25:15 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (argvars[1].v_type != VAR_STRING
|
|
|
|
|
|| argvars[1].vval.v_string == NULL)
|
|
|
|
|
{
|
|
|
|
|
semsg(_(e_invarg2), tv_get_string(&argvars[1]));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-22 20:33:50 +02:00
|
|
|
|
|
|
|
|
|
if (argvars[2].v_type != VAR_UNKNOWN)
|
|
|
|
|
{
|
|
|
|
|
dict_T *d;
|
|
|
|
|
dictitem_T *di;
|
|
|
|
|
|
|
|
|
|
if (argvars[2].v_type != VAR_DICT || argvars[2].vval.v_dict == NULL)
|
|
|
|
|
{
|
|
|
|
|
emsg(_(e_dictreq));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To search a dict, either a callback function or a key can be
|
|
|
|
|
// specified.
|
|
|
|
|
d = argvars[2].vval.v_dict;
|
|
|
|
|
if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (di->di_tv.v_type != VAR_STRING
|
|
|
|
|
|| di->di_tv.vval.v_string == NULL
|
|
|
|
|
|| *di->di_tv.vval.v_string == NUL)
|
|
|
|
|
{
|
|
|
|
|
semsg(_(e_invarg2), tv_get_string(&di->di_tv));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
key = tv_get_string(&di->di_tv);
|
|
|
|
|
}
|
|
|
|
|
else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
cb = get_callback(&di->di_tv);
|
|
|
|
|
if (cb.cb_name == NULL)
|
|
|
|
|
{
|
|
|
|
|
semsg(_(e_invargval), "text_cb");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-23 16:50:30 +02:00
|
|
|
|
if ((di = dict_find(d, (char_u *)"matchseq", -1)) != NULL)
|
|
|
|
|
matchseq = TRUE;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the fuzzy matches
|
|
|
|
|
ret = rettv_list_alloc(rettv);
|
|
|
|
|
if (ret != OK)
|
|
|
|
|
goto done;
|
|
|
|
|
if (retmatchpos)
|
|
|
|
|
{
|
|
|
|
|
list_T *l;
|
|
|
|
|
|
2021-01-02 18:31:32 +01:00
|
|
|
|
// For matchfuzzypos(), a list with three items are returned. First
|
|
|
|
|
// item is a list of matching strings, the second item is a list of
|
|
|
|
|
// lists with matching positions within each string and the third item
|
|
|
|
|
// is the list of scores of the matches.
|
|
|
|
|
l = list_alloc();
|
|
|
|
|
if (l == NULL)
|
|
|
|
|
goto done;
|
|
|
|
|
if (list_append_list(rettv->vval.v_list, l) == FAIL)
|
|
|
|
|
goto done;
|
2020-09-22 20:33:50 +02:00
|
|
|
|
l = list_alloc();
|
|
|
|
|
if (l == NULL)
|
|
|
|
|
goto done;
|
|
|
|
|
if (list_append_list(rettv->vval.v_list, l) == FAIL)
|
|
|
|
|
goto done;
|
|
|
|
|
l = list_alloc();
|
|
|
|
|
if (l == NULL)
|
|
|
|
|
goto done;
|
|
|
|
|
if (list_append_list(rettv->vval.v_list, l) == FAIL)
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 16:50:30 +02:00
|
|
|
|
fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
|
|
|
|
|
matchseq, key, &cb, retmatchpos, rettv->vval.v_list);
|
2020-09-22 20:33:50 +02:00
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
free_callback(&cb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* "matchfuzzy()" function
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
f_matchfuzzy(typval_T *argvars, typval_T *rettv)
|
|
|
|
|
{
|
|
|
|
|
do_fuzzymatch(argvars, rettv, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* "matchfuzzypos()" function
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
|
|
|
|
|
{
|
|
|
|
|
do_fuzzymatch(argvars, rettv, TRUE);
|
2020-09-11 22:25:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 17:28:35 +02:00
|
|
|
|
#endif
|