aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/edit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/edit.c')
-rw-r--r--src/nvim/edit.c478
1 files changed, 251 insertions, 227 deletions
diff --git a/src/nvim/edit.c b/src/nvim/edit.c
index 2ac429cf9e..b53f9d0aa9 100644
--- a/src/nvim/edit.c
+++ b/src/nvim/edit.c
@@ -141,19 +141,13 @@ struct compl_S {
compl_T *cp_next;
compl_T *cp_prev;
char_u *cp_str; // matched text
- char cp_icase; // TRUE or FALSE: ignore case
- char cp_equal; // TRUE or FALSE: ins_compl_equal always ok
char_u *(cp_text[CPT_COUNT]); // text for the menu
char_u *cp_fname; // file containing the match, allocated when
- // cp_flags has FREE_FNAME
- int cp_flags; // ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME
+ // cp_flags has CP_FREE_FNAME
+ int cp_flags; // CP_ values
int cp_number; // sequence number
};
-// flags for ins_compl_add()
-#define ORIGINAL_TEXT (1) // the original text when the expansion begun
-#define FREE_FNAME (2)
-
/*
* All the current matches are stored in a list.
* "compl_first_match" points to the start of the list.
@@ -182,18 +176,18 @@ static int compl_no_insert = FALSE; /* FALSE: select & insert
static int compl_no_select = FALSE; /* FALSE: select & insert
TRUE: noselect */
-static int compl_used_match; // Selected one of the matches. When
- // FALSE the match was edited or using
- // the longest common string.
+static bool compl_used_match; // Selected one of the matches.
+ // When false the match was edited or using
+ // the longest common string.
static int compl_was_interrupted = FALSE; /* didn't finish finding
completions. */
static int compl_restarting = FALSE; /* don't insert match */
-/* When the first completion is done "compl_started" is set. When it's
- * FALSE the word to be completed must be located. */
-static int compl_started = FALSE;
+// When the first completion is done "compl_started" is set. When it's
+// false the word to be completed must be located.
+static bool compl_started = false;
// Which Ctrl-X mode are we in?
static int ctrl_x_mode = CTRL_X_NORMAL;
@@ -2062,21 +2056,24 @@ static bool ins_compl_accept_char(int c)
return vim_iswordc(c);
}
-// This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
-// case of the originally typed text is used, and the case of the completed
-// text is inferred, ie this tries to work out what case you probably wanted
-// the rest of the word to be in -- webb
-int ins_compl_add_infercase(char_u *str_arg, int len, int icase, char_u *fname,
- int dir, int flags)
+/// This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
+/// case of the originally typed text is used, and the case of the completed
+/// text is inferred, ie this tries to work out what case you probably wanted
+/// the rest of the word to be in -- webb
+///
+/// @param[in] cont_s_ipos next ^X<> will set initial_pos
+int ins_compl_add_infercase(char_u *str_arg, int len, bool icase, char_u *fname,
+ int dir, bool cont_s_ipos)
+ FUNC_ATTR_NONNULL_ARG(1)
{
char_u *str = str_arg;
int i, c;
int actual_len; /* Take multi-byte characters */
int actual_compl_length; /* into account. */
int min_len;
- int *wca; /* Wide character array. */
- int has_lower = FALSE;
- int was_letter = FALSE;
+ bool has_lower = false;
+ bool was_letter = false;
+ int flags = 0;
if (p_ic && curbuf->b_p_inf && len > 0) {
// Infer case of completed part.
@@ -2108,8 +2105,8 @@ int ins_compl_add_infercase(char_u *str_arg, int len, int icase, char_u *fname,
min_len = actual_len < actual_compl_length
? actual_len : actual_compl_length;
- /* Allocate wide character array for the completion and fill it. */
- wca = xmalloc(actual_len * sizeof(*wca));
+ // Allocate wide character array for the completion and fill it.
+ int *const wca = xmalloc(actual_len * sizeof(*wca));
{
const char_u *p = str;
for (i = 0; i < actual_len; i++) {
@@ -2200,15 +2197,20 @@ int ins_compl_add_infercase(char_u *str_arg, int len, int icase, char_u *fname,
str = IObuff;
}
- return ins_compl_add(str, len, icase, fname, NULL, false, dir, flags,
- false, false);
+ if (cont_s_ipos) {
+ flags |= CP_CONT_S_IPOS;
+ }
+ if (icase) {
+ flags |= CP_ICASE;
+ }
+
+ return ins_compl_add(str, len, fname, NULL, false, dir, flags, false);
}
/// Add a match to the list of matches
///
/// @param[in] str Match to add.
/// @param[in] len Match length, -1 to use #STRLEN.
-/// @param[in] icase Whether case is to be ignored.
/// @param[in] fname File name match comes from. May be NULL.
/// @param[in] cptext Extra text for popup menu. May be NULL. If not NULL,
/// must have exactly #CPT_COUNT items.
@@ -2218,21 +2220,20 @@ int ins_compl_add_infercase(char_u *str_arg, int len, int icase, char_u *fname,
/// cptext itself will not be freed.
/// @param[in] cdir Completion direction.
/// @param[in] adup True if duplicate matches are to be accepted.
-/// @param[in] equal Match is always accepted by ins_compl_equal.
///
/// @return NOTDONE if the given string is already in the list of completions,
/// otherwise it is added to the list and OK is returned. FAIL will be
/// returned in case of error.
static int ins_compl_add(char_u *const str, int len,
- const bool icase, char_u *const fname,
+ char_u *const fname,
char_u *const *const cptext,
const bool cptext_allocated,
- const Direction cdir, int flags, const bool adup,
- int equal)
+ const Direction cdir, int flags_arg, const bool adup)
FUNC_ATTR_NONNULL_ARG(1)
{
compl_T *match;
int dir = (cdir == kDirectionNotSet ? compl_direction : cdir);
+ int flags = flags_arg;
os_breakcheck();
#define FREE_CPTEXT(cptext, cptext_allocated) \
@@ -2257,7 +2258,7 @@ static int ins_compl_add(char_u *const str, int len,
if (compl_first_match != NULL && !adup) {
match = compl_first_match;
do {
- if (!(match->cp_flags & ORIGINAL_TEXT)
+ if (!(match->cp_flags & CP_ORIGINAL_TEXT)
&& STRNCMP(match->cp_str, str, len) == 0
&& match->cp_str[len] == NUL) {
FREE_CPTEXT(cptext, cptext_allocated);
@@ -2276,24 +2277,23 @@ static int ins_compl_add(char_u *const str, int len,
*/
match = xcalloc(1, sizeof(compl_T));
match->cp_number = -1;
- if (flags & ORIGINAL_TEXT)
+ if (flags & CP_ORIGINAL_TEXT) {
match->cp_number = 0;
+ }
match->cp_str = vim_strnsave(str, len);
- match->cp_icase = icase;
- match->cp_equal = equal;
- /* match-fname is:
- * - compl_curr_match->cp_fname if it is a string equal to fname.
- * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
- * - NULL otherwise. --Acevedo */
+ // match-fname is:
+ // - compl_curr_match->cp_fname if it is a string equal to fname.
+ // - a copy of fname, CP_FREE_FNAME is set to free later THE allocated mem.
+ // - NULL otherwise. --Acevedo
if (fname != NULL
&& compl_curr_match != NULL
&& compl_curr_match->cp_fname != NULL
- && STRCMP(fname, compl_curr_match->cp_fname) == 0)
+ && STRCMP(fname, compl_curr_match->cp_fname) == 0) {
match->cp_fname = compl_curr_match->cp_fname;
- else if (fname != NULL) {
+ } else if (fname != NULL) {
match->cp_fname = vim_strsave(fname);
- flags |= FREE_FNAME;
+ flags |= CP_FREE_FNAME;
} else {
match->cp_fname = NULL;
}
@@ -2339,14 +2339,15 @@ static int ins_compl_add(char_u *const str, int len,
/*
* Find the longest common string if still doing that.
*/
- if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
+ if (compl_get_longest && (flags & CP_ORIGINAL_TEXT) == 0) {
ins_compl_longest_match(match);
+ }
return OK;
}
/// Check that "str[len]" matches with "match->cp_str", considering
-/// "match->cp_icase".
+/// "match->cp_flags".
///
/// @param match completion match
/// @param str character string to check
@@ -2354,10 +2355,10 @@ static int ins_compl_add(char_u *const str, int len,
static bool ins_compl_equal(compl_T *match, char_u *str, size_t len)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{
- if (match->cp_equal) {
+ if (match->cp_flags & CP_EQUAL) {
return true;
}
- if (match->cp_icase) {
+ if (match->cp_flags & CP_ICASE) {
return STRNICMP(match->cp_str, str, len) == 0;
}
return STRNCMP(match->cp_str, str, len) == 0;
@@ -2384,7 +2385,7 @@ static void ins_compl_longest_match(compl_T *match)
* again after redrawing. */
if (!had_match)
ins_compl_delete();
- compl_used_match = FALSE;
+ compl_used_match = false;
} else {
/* Reduce the text if this match differs from compl_leader. */
p = compl_leader;
@@ -2393,7 +2394,9 @@ static void ins_compl_longest_match(compl_T *match)
c1 = utf_ptr2char(p);
c2 = utf_ptr2char(s);
- if (match->cp_icase ? (mb_tolower(c1) != mb_tolower(c2)) : (c1 != c2)) {
+ if ((match->cp_flags & CP_ICASE)
+ ? (mb_tolower(c1) != mb_tolower(c2))
+ : (c1 != c2)) {
break;
}
MB_PTR_ADV(p);
@@ -2414,7 +2417,7 @@ static void ins_compl_longest_match(compl_T *match)
ins_compl_delete();
}
- compl_used_match = FALSE;
+ compl_used_match = false;
}
}
@@ -2423,18 +2426,18 @@ static void ins_compl_longest_match(compl_T *match)
* Frees matches[].
*/
static void ins_compl_add_matches(int num_matches, char_u **matches, int icase)
+ FUNC_ATTR_NONNULL_ALL
{
- int i;
int add_r = OK;
int dir = compl_direction;
- for (i = 0; i < num_matches && add_r != FAIL; i++)
- if ((add_r = ins_compl_add(matches[i], -1, icase,
- NULL, NULL, false, dir, 0, false,
- false)) == OK) {
+ for (int i = 0; i < num_matches && add_r != FAIL; i++) {
+ if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, false, dir,
+ icase ? CP_ICASE : 0, false)) == OK) {
// If dir was BACKWARD then honor it just once.
dir = FORWARD;
}
+ }
FreeWild(num_matches, matches);
}
@@ -2484,6 +2487,8 @@ void completeopt_was_set(void)
*/
void set_completion(colnr_T startcol, list_T *list)
{
+ int flags = CP_ORIGINAL_TEXT;
+
// If already doing completions stop it.
if (ctrl_x_mode != CTRL_X_NORMAL) {
ins_compl_prep(' ');
@@ -2499,8 +2504,11 @@ void set_completion(colnr_T startcol, list_T *list)
/* compl_pattern doesn't need to be set */
compl_orig_text = vim_strnsave(get_cursor_line_ptr() + compl_col,
compl_length);
- if (ins_compl_add(compl_orig_text, -1, p_ic, NULL, NULL, false, 0,
- ORIGINAL_TEXT, false, false) != OK) {
+ if (p_ic) {
+ flags |= CP_ICASE;
+ }
+ if (ins_compl_add(compl_orig_text, -1, NULL, NULL, false, 0,
+ flags, false) != OK) {
return;
}
@@ -2508,8 +2516,8 @@ void set_completion(colnr_T startcol, list_T *list)
ins_compl_add_list(list);
compl_matches = ins_compl_make_cyclic();
- compl_started = TRUE;
- compl_used_match = TRUE;
+ compl_started = true;
+ compl_used_match = true;
compl_cont_status = 0;
int save_w_wrow = curwin->w_wrow;
int save_w_leftcol = curwin->w_leftcol;
@@ -2569,7 +2577,8 @@ static bool pum_enough_matches(void)
compl_T *comp = compl_first_match;
int i = 0;
do {
- if (comp == NULL || ((comp->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) {
+ if (comp == NULL
+ || ((comp->cp_flags & CP_ORIGINAL_TEXT) == 0 && ++i == 2)) {
break;
}
comp = comp->cp_next;
@@ -2614,8 +2623,8 @@ void ins_compl_show_pum(void)
{
compl_T *compl;
compl_T *shown_compl = NULL;
- int did_find_shown_match = FALSE;
- int shown_match_ok = FALSE;
+ bool did_find_shown_match = false;
+ bool shown_match_ok = false;
int i;
int cur = -1;
colnr_T col;
@@ -2647,7 +2656,7 @@ void ins_compl_show_pum(void)
lead_len = (int)STRLEN(compl_leader);
}
do {
- if ((compl->cp_flags & ORIGINAL_TEXT) == 0
+ if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
&& (compl_leader == NULL
|| ins_compl_equal(compl, compl_leader, lead_len))) {
compl_match_arraysize++;
@@ -2661,13 +2670,14 @@ void ins_compl_show_pum(void)
compl_match_array = xcalloc(compl_match_arraysize, sizeof(pumitem_T));
/* If the current match is the original text don't find the first
* match after it, don't highlight anything. */
- if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
- shown_match_ok = TRUE;
+ if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) {
+ shown_match_ok = true;
+ }
i = 0;
compl = compl_first_match;
do {
- if ((compl->cp_flags & ORIGINAL_TEXT) == 0
+ if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0
&& (compl_leader == NULL
|| ins_compl_equal(compl, compl_leader, lead_len))) {
if (!shown_match_ok) {
@@ -2675,12 +2685,13 @@ void ins_compl_show_pum(void)
/* This item is the shown match or this is the
* first displayed item after the shown match. */
compl_shown_match = compl;
- did_find_shown_match = TRUE;
- shown_match_ok = TRUE;
- } else
- /* Remember this displayed match for when the
- * shown match is just below it. */
+ did_find_shown_match = true;
+ shown_match_ok = true;
+ } else {
+ // Remember this displayed match for when the
+ // shown match is just below it.
shown_compl = compl;
+ }
cur = i;
}
@@ -2699,18 +2710,19 @@ void ins_compl_show_pum(void)
}
if (compl == compl_shown_match) {
- did_find_shown_match = TRUE;
+ did_find_shown_match = true;
/* When the original text is the shown match don't set
* compl_shown_match. */
- if (compl->cp_flags & ORIGINAL_TEXT)
- shown_match_ok = TRUE;
+ if (compl->cp_flags & CP_ORIGINAL_TEXT) {
+ shown_match_ok = true;
+ }
if (!shown_match_ok && shown_compl != NULL) {
/* The shown match isn't displayed, set it to the
* previously displayed match. */
compl_shown_match = shown_compl;
- shown_match_ok = TRUE;
+ shown_match_ok = true;
}
}
compl = compl->cp_next;
@@ -2882,8 +2894,8 @@ static void ins_compl_files(int count, char_u **files, int thesaurus, int flags,
ptr = find_word_end(ptr);
}
add_r = ins_compl_add_infercase(regmatch->startp[0],
- (int)(ptr - regmatch->startp[0]),
- p_ic, files[i], *dir, 0);
+ (int)(ptr - regmatch->startp[0]),
+ p_ic, files[i], *dir, false);
if (thesaurus) {
char_u *wstart;
@@ -2914,11 +2926,11 @@ static void ins_compl_files(int count, char_u **files, int thesaurus, int flags,
else
ptr = find_word_end(ptr);
- /* Add the word. Skip the regexp match. */
- if (wstart != regmatch->startp[0])
- add_r = ins_compl_add_infercase(wstart,
- (int)(ptr - wstart),
- p_ic, files[i], *dir, 0);
+ // Add the word. Skip the regexp match.
+ if (wstart != regmatch->startp[0]) {
+ add_r = ins_compl_add_infercase(wstart, (int)(ptr - wstart),
+ p_ic, files[i], *dir, false);
+ }
}
}
if (add_r == OK)
@@ -2995,7 +3007,6 @@ static char_u *find_line_end(char_u *ptr)
static void ins_compl_free(void)
{
compl_T *match;
- int i;
XFREE_CLEAR(compl_pattern);
XFREE_CLEAR(compl_leader);
@@ -3011,11 +3022,13 @@ static void ins_compl_free(void)
match = compl_curr_match;
compl_curr_match = compl_curr_match->cp_next;
xfree(match->cp_str);
- /* several entries may use the same fname, free it just once. */
- if (match->cp_flags & FREE_FNAME)
+ // several entries may use the same fname, free it just once.
+ if (match->cp_flags & CP_FREE_FNAME) {
xfree(match->cp_fname);
- for (i = 0; i < CPT_COUNT; ++i)
+ }
+ for (int i = 0; i < CPT_COUNT; i++) {
xfree(match->cp_text[i]);
+ }
xfree(match);
} while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
compl_first_match = compl_curr_match = NULL;
@@ -3026,7 +3039,7 @@ static void ins_compl_free(void)
static void ins_compl_clear(void)
{
compl_cont_status = 0;
- compl_started = FALSE;
+ compl_started = false;
compl_matches = 0;
XFREE_CLEAR(compl_pattern);
XFREE_CLEAR(compl_leader);
@@ -3095,7 +3108,7 @@ void get_complete_info(list_T *what_list, dict_T *retdict)
if (ret == OK && compl_first_match != NULL) {
compl_T *match = compl_first_match;
do {
- if (!(match->cp_flags & ORIGINAL_TEXT)) {
+ if (!(match->cp_flags & CP_ORIGINAL_TEXT)) {
dict_T *di = tv_dict_alloc();
tv_list_append_dict(li, di);
@@ -3202,7 +3215,7 @@ static void ins_compl_new_leader(void)
ins_compl_del_pum();
ins_compl_delete();
ins_bytes(compl_leader + ins_compl_len());
- compl_used_match = FALSE;
+ compl_used_match = false;
if (compl_started) {
ins_compl_set_original_text(compl_leader);
@@ -3283,7 +3296,7 @@ static void ins_compl_restart(void)
* will stay to the last popup menu and reduce flicker */
update_screen(0);
ins_compl_free();
- compl_started = FALSE;
+ compl_started = false;
compl_matches = 0;
compl_cont_status = 0;
compl_cont_mode = 0;
@@ -3293,15 +3306,16 @@ static void ins_compl_restart(void)
* Set the first match, the original text.
*/
static void ins_compl_set_original_text(char_u *str)
+ FUNC_ATTR_NONNULL_ALL
{
// Replace the original text entry.
- // The ORIGINAL_TEXT flag is either at the first item or might possibly be
+ // The CP_ORIGINAL_TEXT flag is either at the first item or might possibly be
// at the last item for backward completion
- if (compl_first_match->cp_flags & ORIGINAL_TEXT) { // safety check
+ if (compl_first_match->cp_flags & CP_ORIGINAL_TEXT) { // safety check
xfree(compl_first_match->cp_str);
compl_first_match->cp_str = vim_strsave(str);
} else if (compl_first_match->cp_prev != NULL
- && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT)) {
+ && (compl_first_match->cp_prev->cp_flags & CP_ORIGINAL_TEXT)) {
xfree(compl_first_match->cp_prev->cp_str);
compl_first_match->cp_prev->cp_str = vim_strsave(str);
}
@@ -3322,7 +3336,7 @@ static void ins_compl_addfrommatch(void)
if ((int)STRLEN(p) <= len) { /* the match is too short */
/* When still at the original match use the first entry that matches
* the leader. */
- if (compl_shown_match->cp_flags & ORIGINAL_TEXT) {
+ if (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) {
p = NULL;
for (cp = compl_shown_match->cp_next; cp != NULL
&& cp != compl_first_match; cp = cp->cp_next) {
@@ -3371,8 +3385,7 @@ static bool ins_compl_prep(int c)
if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
|| (ctrl_x_mode == CTRL_X_NORMAL && !compl_started)) {
compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
- compl_used_match = TRUE;
-
+ compl_used_match = true;
}
if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) {
@@ -3563,7 +3576,7 @@ static bool ins_compl_prep(int c)
auto_format(FALSE, TRUE);
ins_compl_free();
- compl_started = FALSE;
+ compl_started = false;
compl_matches = 0;
if (!shortmess(SHM_COMPLETIONMENU)) {
msg_clr_cmdline(); // necessary for "noshowmode"
@@ -3672,28 +3685,28 @@ static buf_T *ins_compl_next_buf(buf_T *buf, int flag)
}
-/*
- * Execute user defined complete function 'completefunc' or 'omnifunc', and
- * get matches in "matches".
- */
+// Execute user defined complete function 'completefunc' or 'omnifunc', and
+// get matches in "matches".
static void
-expand_by_function (
- int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
+expand_by_function(
+ int type, // CTRL_X_OMNI or CTRL_X_FUNCTION
char_u *base
)
{
- list_T *matchlist = NULL;
- dict_T *matchdict = NULL;
- char_u *funcname;
+ list_T *matchlist = NULL;
+ dict_T *matchdict = NULL;
+ char_u *funcname;
pos_T pos;
- win_T *curwin_save;
- buf_T *curbuf_save;
+ win_T *curwin_save;
+ buf_T *curbuf_save;
typval_T rettv;
const int save_State = State;
+ assert(curbuf != NULL);
funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
- if (*funcname == NUL)
+ if (*funcname == NUL) {
return;
+ }
// Call 'completefunc' to obtain the list of matches.
typval_T args[3];
@@ -3807,10 +3820,9 @@ int ins_compl_add_tv(typval_T *const tv, const Direction dir)
FUNC_ATTR_NONNULL_ALL
{
const char *word;
- bool icase = false;
- bool adup = false;
- bool aempty = false;
- bool aequal = false;
+ bool dup = false;
+ bool empty = false;
+ int flags = 0;
char *(cptext[CPT_COUNT]);
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) {
@@ -3822,46 +3834,47 @@ int ins_compl_add_tv(typval_T *const tv, const Direction dir)
cptext[CPT_USER_DATA] = tv_dict_get_string(tv->vval.v_dict,
"user_data", true);
- icase = (bool)tv_dict_get_number(tv->vval.v_dict, "icase");
- adup = (bool)tv_dict_get_number(tv->vval.v_dict, "dup");
- aempty = (bool)tv_dict_get_number(tv->vval.v_dict, "empty");
- if (tv_dict_get_string(tv->vval.v_dict, "equal", false) != NULL) {
- aequal = tv_dict_get_number(tv->vval.v_dict, "equal");
+ if (tv_dict_get_number(tv->vval.v_dict, "icase")) {
+ flags |= CP_ICASE;
+ }
+ dup = (bool)tv_dict_get_number(tv->vval.v_dict, "dup");
+ empty = (bool)tv_dict_get_number(tv->vval.v_dict, "empty");
+ if (tv_dict_get_string(tv->vval.v_dict, "equal", false) != NULL
+ && tv_dict_get_number(tv->vval.v_dict, "equal")) {
+ flags |= CP_EQUAL;
}
} else {
word = (const char *)tv_get_string_chk(tv);
memset(cptext, 0, sizeof(cptext));
}
- if (word == NULL || (!aempty && *word == NUL)) {
+ if (word == NULL || (!empty && *word == NUL)) {
for (size_t i = 0; i < CPT_COUNT; i++) {
xfree(cptext[i]);
}
return FAIL;
}
- return ins_compl_add((char_u *)word, -1, icase, NULL,
- (char_u **)cptext, true, dir, 0, adup, aequal);
+ return ins_compl_add((char_u *)word, -1, NULL,
+ (char_u **)cptext, true, dir, flags, dup);
}
-/*
- * Get the next expansion(s), using "compl_pattern".
- * The search starts at position "ini" in curbuf and in the direction
- * compl_direction.
- * When "compl_started" is FALSE start at that position, otherwise continue
- * where we stopped searching before.
- * This may return before finding all the matches.
- * Return the total number of matches or -1 if still unknown -- Acevedo
- */
+// Get the next expansion(s), using "compl_pattern".
+// The search starts at position "ini" in curbuf and in the direction
+// compl_direction.
+// When "compl_started" is false start at that position, otherwise continue
+// where we stopped searching before.
+// This may return before finding all the matches.
+// Return the total number of matches or -1 if still unknown -- Acevedo
static int ins_compl_get_exp(pos_T *ini)
{
static pos_T first_match_pos;
static pos_T last_match_pos;
- static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
- static int found_all = FALSE; /* Found all matches of a
- certain type. */
- static buf_T *ins_buf = NULL; /* buffer being scanned */
+ static char_u *e_cpt = (char_u *)""; // curr. entry in 'complete'
+ static int found_all = false; // Found all matches of a
+ // certain type.
+ static buf_T *ins_buf = NULL; // buffer being scanned
- pos_T *pos;
- char_u **matches;
+ pos_T *pos;
+ char_u **matches;
int save_p_scs;
bool save_p_ws;
int save_p_ic;
@@ -3870,12 +3883,14 @@ static int ins_compl_get_exp(pos_T *ini)
int len;
int found_new_match;
int type = ctrl_x_mode;
- char_u *ptr;
- char_u *dict = NULL;
+ char_u *ptr;
+ char_u *dict = NULL;
int dict_f = 0;
int set_match_pos;
int l_ctrl_x_mode = ctrl_x_mode;
+ assert(curbuf != NULL);
+
if (!compl_started) {
FOR_ALL_BUFFERS(buf) {
buf->b_scanned = false;
@@ -3899,9 +3914,9 @@ static int ins_compl_get_exp(pos_T *ini)
assert(l_ctrl_x_mode == ctrl_x_mode);
- /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
- * or if found_all says this entry is done. For ^X^L only use the
- * entries from 'complete' that look in loaded buffers. */
+ // For ^N/^P pick a new entry from e_cpt if compl_started is off,
+ // or if found_all says this entry is done. For ^X^L only use the
+ // entries from 'complete' that look in loaded buffers.
if ((l_ctrl_x_mode == CTRL_X_NORMAL
|| CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))
&& (!compl_started || found_all)) {
@@ -3923,23 +3938,24 @@ static int ins_compl_get_exp(pos_T *ini)
last_match_pos = first_match_pos;
type = 0;
- /* Remember the first match so that the loop stops when we
- * wrap and come back there a second time. */
- set_match_pos = TRUE;
+ // Remember the first match so that the loop stops when we
+ // wrap and come back there a second time.
+ set_match_pos = true;
} else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
&& (ins_buf =
- ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) {
- /* Scan a buffer, but not the current one. */
- if (ins_buf->b_ml.ml_mfp != NULL) { /* loaded buffer */
- compl_started = TRUE;
+ ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) {
+ // Scan a buffer, but not the current one.
+ if (ins_buf->b_ml.ml_mfp != NULL) { // loaded buffer
+ compl_started = true;
first_match_pos.col = last_match_pos.col = 0;
first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
last_match_pos.lnum = 0;
type = 0;
- } else { /* unloaded buffer, scan like dictionary */
- found_all = TRUE;
- if (ins_buf->b_fname == NULL)
+ } else { // unloaded buffer, scan like dictionary
+ found_all = true;
+ if (ins_buf->b_fname == NULL) {
continue;
+ }
type = CTRL_X_DICTIONARY;
dict = ins_buf->b_fname;
dict_f = DICT_EXACT;
@@ -3977,7 +3993,7 @@ static int ins_compl_get_exp(pos_T *ini)
type = -1;
}
- /* in any case e_cpt is advanced to the next entry */
+ // in any case e_cpt is advanced to the next entry
(void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
found_all = TRUE;
@@ -4024,12 +4040,12 @@ static int ins_compl_get_exp(pos_T *ini)
break;
case CTRL_X_TAGS:
- /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
+ // set p_ic according to p_ic, p_scs and pat for find_tags().
save_p_ic = p_ic;
p_ic = ignorecase(compl_pattern);
- /* Find up to TAG_MANY matches. Avoids that an enormous number
- * of matches is found when compl_pattern is empty */
+ // Find up to TAG_MANY matches. Avoids that an enormous number
+ // of matches is found when compl_pattern is empty
if (find_tags(compl_pattern, &num_matches, &matches,
TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP
| (l_ctrl_x_mode != CTRL_X_NORMAL ? TAG_VERBOSE : 0),
@@ -4041,9 +4057,8 @@ static int ins_compl_get_exp(pos_T *ini)
case CTRL_X_FILES:
if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
- EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) {
-
- /* May change home directory back to "~". */
+ EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) {
+ // May change home directory back to "~".
tilde_replace(compl_pattern, num_matches, matches);
ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
}
@@ -4068,29 +4083,26 @@ static int ins_compl_get_exp(pos_T *ini)
ins_compl_add_matches(num_matches, matches, p_ic);
break;
- default: /* normal ^P/^N and ^X^L */
- /*
- * If 'infercase' is set, don't use 'smartcase' here
- */
+ default: // normal ^P/^N and ^X^L
+ // If 'infercase' is set, don't use 'smartcase' here
save_p_scs = p_scs;
assert(ins_buf);
if (ins_buf->b_p_inf)
p_scs = FALSE;
- /* Buffers other than curbuf are scanned from the beginning or the
- * end but never from the middle, thus setting nowrapscan in this
- * buffers is a good idea, on the other hand, we always set
- * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
+ // Buffers other than curbuf are scanned from the beginning or the
+ // end but never from the middle, thus setting nowrapscan in this
+ // buffers is a good idea, on the other hand, we always set
+ // wrapscan for curbuf to avoid missing matches -- Acevedo,Webb
save_p_ws = p_ws;
if (ins_buf != curbuf)
p_ws = false;
else if (*e_cpt == '.')
p_ws = true;
for (;; ) {
- int flags = 0;
-
- ++msg_silent; /* Don't want messages for wrapscan. */
+ bool cont_s_ipos = false;
+ msg_silent++; // Don't want messages for wrapscan.
// CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode) || word-wise search that
// has added a word that was at the beginning of the line.
if (CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode)
@@ -4107,47 +4119,52 @@ static int ins_compl_get_exp(pos_T *ini)
}
msg_silent--;
if (!compl_started || set_match_pos) {
- /* set "compl_started" even on fail */
- compl_started = TRUE;
+ // set "compl_started" even on fail
+ compl_started = true;
first_match_pos = *pos;
last_match_pos = *pos;
- set_match_pos = FALSE;
+ set_match_pos = false;
} else if (first_match_pos.lnum == last_match_pos.lnum
- && first_match_pos.col == last_match_pos.col)
+ && first_match_pos.col == last_match_pos.col) {
found_new_match = FAIL;
+ }
if (found_new_match == FAIL) {
if (ins_buf == curbuf)
found_all = TRUE;
break;
}
- /* when ADDING, the text before the cursor matches, skip it */
- if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
- && ini->lnum == pos->lnum
- && ini->col == pos->col)
+ // when ADDING, the text before the cursor matches, skip it
+ if ((compl_cont_status & CONT_ADDING) && ins_buf == curbuf
+ && ini->lnum == pos->lnum
+ && ini->col == pos->col) {
continue;
- ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
+ }
+ ptr = ml_get_buf(ins_buf, pos->lnum, false) + pos->col;
if (CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode)) {
if (compl_cont_status & CONT_ADDING) {
- if (pos->lnum >= ins_buf->b_ml.ml_line_count)
+ if (pos->lnum >= ins_buf->b_ml.ml_line_count) {
continue;
- ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
- if (!p_paste)
+ }
+ ptr = ml_get_buf(ins_buf, pos->lnum + 1, false);
+ if (!p_paste) {
ptr = skipwhite(ptr);
+ }
}
len = (int)STRLEN(ptr);
} else {
- char_u *tmp_ptr = ptr;
+ char_u *tmp_ptr = ptr;
if (compl_cont_status & CONT_ADDING) {
tmp_ptr += compl_length;
- /* Skip if already inside a word. */
- if (vim_iswordp(tmp_ptr))
+ // Skip if already inside a word.
+ if (vim_iswordp(tmp_ptr)) {
continue;
- /* Find start of next word. */
+ }
+ // Find start of next word.
tmp_ptr = find_word_start(tmp_ptr);
}
- /* Find end of this word. */
+ // Find end of this word.
tmp_ptr = find_word_end(tmp_ptr);
len = (int)(tmp_ptr - ptr);
@@ -4160,15 +4177,16 @@ static int ins_compl_get_exp(pos_T *ini)
STRNCPY(IObuff, ptr, len);
ptr = ml_get_buf(ins_buf, pos->lnum + 1, false);
tmp_ptr = ptr = skipwhite(ptr);
- /* Find start of next word. */
+ // Find start of next word.
tmp_ptr = find_word_start(tmp_ptr);
- /* Find end of next word. */
+ // Find end of next word.
tmp_ptr = find_word_end(tmp_ptr);
if (tmp_ptr > ptr) {
if (*ptr != ')' && IObuff[len - 1] != TAB) {
- if (IObuff[len - 1] != ' ')
+ if (IObuff[len - 1] != ' ') {
IObuff[len++] = ' ';
- /* IObuf =~ "\k.* ", thus len >= 2 */
+ }
+ // IObuf =~ "\k.* ", thus len >= 2
if (p_js
&& (IObuff[len - 2] == '.'
|| IObuff[len - 2] == '?'
@@ -4176,12 +4194,13 @@ static int ins_compl_get_exp(pos_T *ini)
IObuff[len++] = ' ';
}
}
- /* copy as much as possible of the new word */
- if (tmp_ptr - ptr >= IOSIZE - len)
+ // copy as much as possible of the new word
+ if (tmp_ptr - ptr >= IOSIZE - len) {
tmp_ptr = ptr + IOSIZE - len - 1;
- STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
+ }
+ STRLCPY(IObuff + len, ptr, IOSIZE - len);
len += (int)(tmp_ptr - ptr);
- flags |= CONT_S_IPOS;
+ cont_s_ipos = true;
}
IObuff[len] = NUL;
ptr = IObuff;
@@ -4190,9 +4209,9 @@ static int ins_compl_get_exp(pos_T *ini)
continue;
}
}
- if (ins_compl_add_infercase(ptr, len, p_ic,
- ins_buf == curbuf ? NULL : ins_buf->b_sfname,
- 0, flags) != NOTDONE) {
+ if (ins_compl_add_infercase(
+ ptr, len, p_ic, ins_buf == curbuf ? NULL : ins_buf->b_sfname,
+ 0, cont_s_ipos) != NOTDONE) {
found_new_match = OK;
break;
}
@@ -4214,27 +4233,28 @@ static int ins_compl_get_exp(pos_T *ini)
|| found_new_match != FAIL) {
if (got_int)
break;
- /* Fill the popup menu as soon as possible. */
- if (type != -1)
+ // Fill the popup menu as soon as possible.
+ if (type != -1) {
ins_compl_check_keys(0, false);
+ }
if ((l_ctrl_x_mode != CTRL_X_NORMAL
&& !CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))
|| compl_interrupted) {
break;
}
- compl_started = TRUE;
+ compl_started = true;
} else {
- /* Mark a buffer scanned when it has been scanned completely */
+ // Mark a buffer scanned when it has been scanned completely
if (type == 0 || type == CTRL_X_PATH_PATTERNS) {
assert(ins_buf);
ins_buf->b_scanned = true;
}
- compl_started = FALSE;
+ compl_started = false;
}
}
- compl_started = TRUE;
+ compl_started = true;
if ((l_ctrl_x_mode == CTRL_X_NORMAL
|| CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))
@@ -4242,7 +4262,7 @@ static int ins_compl_get_exp(pos_T *ini)
found_new_match = FAIL;
}
- i = -1; /* total of matches, unknown */
+ i = -1; // total of matches, unknown
if (found_new_match == FAIL
|| (l_ctrl_x_mode != CTRL_X_NORMAL
&& !CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))) {
@@ -4263,7 +4283,7 @@ static int ins_compl_get_exp(pos_T *ini)
return i;
}
-/* Delete the old text being completed. */
+// Delete the old text being completed.
static void ins_compl_delete(void)
{
int col;
@@ -4290,10 +4310,7 @@ static void ins_compl_delete(void)
static void ins_compl_insert(int in_compl_func)
{
ins_bytes(compl_shown_match->cp_str + ins_compl_len());
- if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
- compl_used_match = FALSE;
- else
- compl_used_match = TRUE;
+ compl_used_match = !(compl_shown_match->cp_flags & CP_ORIGINAL_TEXT);
dict_T *dict = ins_compl_dict_alloc(compl_shown_match);
set_vim_var_dict(VV_COMPLETED_ITEM, dict);
@@ -4358,7 +4375,7 @@ ins_compl_next (
compl_T *found_compl = NULL;
int found_end = FALSE;
int advance;
- int started = compl_started;
+ const bool started = compl_started;
/* When user complete function return -1 for findstart which is next
* time of 'always', compl_shown_match become NULL. */
@@ -4366,14 +4383,15 @@ ins_compl_next (
return -1;
if (compl_leader != NULL
- && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) {
- /* Set "compl_shown_match" to the actually shown match, it may differ
- * when "compl_leader" is used to omit some of the matches. */
+ && (compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0) {
+ // Set "compl_shown_match" to the actually shown match, it may differ
+ // when "compl_leader" is used to omit some of the matches.
while (!ins_compl_equal(compl_shown_match,
- compl_leader, (int)STRLEN(compl_leader))
+ compl_leader, STRLEN(compl_leader))
&& compl_shown_match->cp_next != NULL
- && compl_shown_match->cp_next != compl_first_match)
+ && compl_shown_match->cp_next != compl_first_match) {
compl_shown_match = compl_shown_match->cp_next;
+ }
/* If we didn't find it searching forward, and compl_shows_dir is
* backward, find the last match. */
@@ -4454,14 +4472,15 @@ ins_compl_next (
}
found_end = FALSE;
}
- if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
+ if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0
&& compl_leader != NULL
&& !ins_compl_equal(compl_shown_match,
- compl_leader, (int)STRLEN(compl_leader)))
- ++todo;
- else
- /* Remember a matching item. */
+ compl_leader, STRLEN(compl_leader))) {
+ todo++;
+ } else {
+ // Remember a matching item.
found_compl = compl_shown_match;
+ }
/* Stop at the end of the list when we found a usable match. */
if (found_end) {
@@ -4476,7 +4495,7 @@ ins_compl_next (
/* Insert the text of the new completion, or the compl_leader. */
if (compl_no_insert && !started) {
ins_bytes(compl_orig_text + ins_compl_len());
- compl_used_match = FALSE;
+ compl_used_match = false;
} else if (insert_match) {
if (!compl_get_longest || compl_used_match) {
ins_compl_insert(in_compl_func);
@@ -4484,7 +4503,7 @@ ins_compl_next (
ins_bytes(compl_leader + ins_compl_len());
}
} else {
- compl_used_match = FALSE;
+ compl_used_match = false;
}
if (!allow_get_expansion) {
@@ -4692,6 +4711,7 @@ static int ins_complete(int c, bool enable_pum)
int save_w_leftcol;
int insert_match;
const bool save_did_ai = did_ai;
+ int flags = CP_ORIGINAL_TEXT;
compl_direction = ins_compl_key2dir(c);
insert_match = ins_compl_use_match(c);
@@ -5019,8 +5039,11 @@ static int ins_complete(int c, bool enable_pum)
/* Always add completion for the original text. */
xfree(compl_orig_text);
compl_orig_text = vim_strnsave(line + compl_col, compl_length);
- if (ins_compl_add(compl_orig_text, -1, p_ic, NULL, NULL, false, 0,
- ORIGINAL_TEXT, false, false) != OK) {
+ if (p_ic) {
+ flags |= CP_ICASE;
+ }
+ if (ins_compl_add(compl_orig_text, -1, NULL, NULL, false, 0,
+ flags, false) != OK) {
XFREE_CLEAR(compl_pattern);
XFREE_CLEAR(compl_orig_text);
return FAIL;
@@ -5081,13 +5104,14 @@ static int ins_complete(int c, bool enable_pum)
}
}
- if (compl_curr_match->cp_flags & CONT_S_IPOS)
+ if (compl_curr_match->cp_flags & CP_CONT_S_IPOS) {
compl_cont_status |= CONT_S_IPOS;
- else
+ } else {
compl_cont_status &= ~CONT_S_IPOS;
+ }
if (edit_submode_extra == NULL) {
- if (compl_curr_match->cp_flags & ORIGINAL_TEXT) {
+ if (compl_curr_match->cp_flags & CP_ORIGINAL_TEXT) {
edit_submode_extra = (char_u *)_("Back at original");
edit_submode_highl = HLF_W;
} else if (compl_cont_status & CONT_S_IPOS) {