diff options
Diffstat (limited to 'src/nvim/insexpand.c')
-rw-r--r-- | src/nvim/insexpand.c | 3827 |
1 files changed, 3827 insertions, 0 deletions
diff --git a/src/nvim/insexpand.c b/src/nvim/insexpand.c new file mode 100644 index 0000000000..d3d5ef7261 --- /dev/null +++ b/src/nvim/insexpand.c @@ -0,0 +1,3827 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +// insexpand.c: functions for Insert mode completion + +#include <assert.h> +#include <inttypes.h> +#include <stdbool.h> +#include <string.h> + +#include "nvim/ascii.h" +#include "nvim/buffer.h" +#include "nvim/change.h" +#include "nvim/charset.h" +#include "nvim/cursor.h" +#include "nvim/edit.h" +#include "nvim/eval.h" +#include "nvim/eval/typval.h" +#include "nvim/ex_docmd.h" +#include "nvim/ex_getln.h" +#include "nvim/fileio.h" +#include "nvim/getchar.h" +#include "nvim/indent.h" +#include "nvim/indent_c.h" +#include "nvim/insexpand.h" +#include "nvim/keycodes.h" +#include "nvim/mbyte.h" +#include "nvim/memline.h" +#include "nvim/memory.h" +#include "nvim/message.h" +#include "nvim/move.h" +#include "nvim/option.h" +#include "nvim/os/input.h" +#include "nvim/os/time.h" +#include "nvim/path.h" +#include "nvim/popupmnu.h" +#include "nvim/regexp.h" +#include "nvim/screen.h" +#include "nvim/search.h" +#include "nvim/spell.h" +#include "nvim/state.h" +#include "nvim/strings.h" +#include "nvim/tag.h" +#include "nvim/ui.h" +#include "nvim/vim.h" +#include "nvim/window.h" + +// Definitions used for CTRL-X submode. +// Note: If you change CTRL-X submode, you must also maintain ctrl_x_msgs[] +// and ctrl_x_mode_names[]. + +#define CTRL_X_WANT_IDENT 0x100 + +#define CTRL_X_NORMAL 0 ///< CTRL-N CTRL-P completion, default +#define CTRL_X_NOT_DEFINED_YET 1 +#define CTRL_X_SCROLL 2 +#define CTRL_X_WHOLE_LINE 3 +#define CTRL_X_FILES 4 +#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT) +#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT) +#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT) +#define CTRL_X_FINISHED 8 +#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT) +#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT) +#define CTRL_X_CMDLINE 11 +#define CTRL_X_FUNCTION 12 +#define CTRL_X_OMNI 13 +#define CTRL_X_SPELL 14 +#define CTRL_X_LOCAL_MSG 15 ///< only used in "ctrl_x_msgs" +#define CTRL_X_EVAL 16 ///< for builtin function complete() +#define CTRL_X_CMDLINE_CTRL_X 17 ///< CTRL-X typed in CTRL_X_CMDLINE + +#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] + +/// Message for CTRL-X mode, index is ctrl_x_mode. +static char *ctrl_x_msgs[] = +{ + N_(" Keyword completion (^N^P)"), // CTRL_X_NORMAL, ^P/^N compl. + N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"), + NULL, // CTRL_X_SCROLL: depends on state + N_(" Whole line completion (^L^N^P)"), + N_(" File name completion (^F^N^P)"), + N_(" Tag completion (^]^N^P)"), + N_(" Path pattern completion (^N^P)"), + N_(" Definition completion (^D^N^P)"), + NULL, // CTRL_X_FINISHED + N_(" Dictionary completion (^K^N^P)"), + N_(" Thesaurus completion (^T^N^P)"), + N_(" Command-line completion (^V^N^P)"), + N_(" User defined completion (^U^N^P)"), + N_(" Omni completion (^O^N^P)"), + N_(" Spelling suggestion (s^N^P)"), + N_(" Keyword Local completion (^N^P)"), + NULL, // CTRL_X_EVAL doesn't use msg. + N_(" Command-line completion (^V^N^P)"), +}; + +static char *ctrl_x_mode_names[] = { + "keyword", + "ctrl_x", + "scroll", + "whole_line", + "files", + "tags", + "path_patterns", + "path_defines", + "unknown", // CTRL_X_FINISHED + "dictionary", + "thesaurus", + "cmdline", + "function", + "omni", + "spell", + NULL, // CTRL_X_LOCAL_MSG only used in "ctrl_x_msgs" + "eval", + "cmdline", +}; + +// Array indexes used for cp_text[]. +#define CPT_ABBR 0 ///< "abbr" +#define CPT_MENU 1 ///< "menu" +#define CPT_KIND 2 ///< "kind" +#define CPT_INFO 3 ///< "info" +#define CPT_COUNT 4 ///< Number of entries + +/// Structure used to store one match for insert completion. +typedef struct compl_S compl_T; +struct compl_S { + compl_T *cp_next; + compl_T *cp_prev; + char_u *cp_str; ///< matched text + char_u *(cp_text[CPT_COUNT]); ///< text for the menu + typval_T cp_user_data; + char_u *cp_fname; ///< file containing the match, allocated when + ///< cp_flags has CP_FREE_FNAME + int cp_flags; ///< CP_ values + int cp_number; ///< sequence number +}; + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "insexpand.c.generated.h" +#endif + +/// values for cp_flags +typedef enum { + CP_ORIGINAL_TEXT = 1, ///< the original text when the expansion begun + CP_FREE_FNAME = 2, ///< cp_fname is allocated + CP_CONT_S_IPOS = 4, ///< use CONT_S_IPOS for compl_cont_status + CP_EQUAL = 8, ///< ins_compl_equal() always returns true + CP_ICASE = 16, ///< ins_compl_equal ignores case + CP_FAST = 32, ///< use fast_breakcheck instead of os_breakcheck +} cp_flags_T; + +static char e_hitend[] = N_("Hit end of paragraph"); +static char e_compldel[] = N_("E840: Completion function deleted text"); + +// All the current matches are stored in a list. +// "compl_first_match" points to the start of the list. +// "compl_curr_match" points to the currently selected entry. +// "compl_shown_match" is different from compl_curr_match during +// ins_compl_get_exp(). + +static compl_T *compl_first_match = NULL; +static compl_T *compl_curr_match = NULL; +static compl_T *compl_shown_match = NULL; +static compl_T *compl_old_match = NULL; + +/// After using a cursor key <Enter> selects a match in the popup menu, +/// otherwise it inserts a line break. +static bool compl_enter_selects = false; + +/// When "compl_leader" is not NULL only matches that start with this string +/// are used. +static char_u *compl_leader = NULL; + +static bool compl_get_longest = false; ///< put longest common string in compl_leader + +static bool compl_no_insert = false; ///< false: select & insert + ///< true: noinsert +static bool compl_no_select = false; ///< false: select & insert + ///< true: noselect + +/// Selected one of the matches. When false the match was edited or using the +/// longest common string. +static bool compl_used_match; + +/// didn't finish finding completions. +static bool compl_was_interrupted = false; + +// Set when character typed while looking for matches and it means we should +// stop looking for matches. +static bool compl_interrupted = false; + +static bool 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 bool compl_started = false; + +///< Which Ctrl-X mode are we in? +static int ctrl_x_mode = CTRL_X_NORMAL; + +static int compl_matches = 0; +static char_u *compl_pattern = NULL; +static Direction compl_direction = FORWARD; +static Direction compl_shows_dir = FORWARD; +static int compl_pending = 0; ///< > 1 for postponed CTRL-N +static pos_T compl_startpos; +static colnr_T compl_col = 0; ///< column where the text starts + ///< that is being completed +static char_u *compl_orig_text = NULL; ///< text as it was before + ///< completion started +static int compl_cont_mode = 0; +static expand_T compl_xp; + +static bool compl_opt_refresh_always = false; + +static size_t spell_bad_len = 0; // length of located bad word + +static int pum_selected_item = -1; + +/// CTRL-X pressed in Insert mode. +void ins_ctrl_x(void) +{ + if (!ctrl_x_mode_cmdline()) { + // if the next ^X<> won't ADD nothing, then reset compl_cont_status + if (compl_cont_status & CONT_N_ADDS) { + compl_cont_status |= CONT_INTRPT; + } else { + compl_cont_status = 0; + } + // We're not sure which CTRL-X mode it will be yet + ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; + edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); + edit_submode_pre = NULL; + showmode(); + } else { + // CTRL-X in CTRL-X CTRL-V mode behaves differently to make CTRL-X + // CTRL-V look like CTRL-N + ctrl_x_mode = CTRL_X_CMDLINE_CTRL_X; + } + + may_trigger_modechanged(); +} + +// Functions to check the current CTRL-X mode. + +bool ctrl_x_mode_none(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == 0; +} + +bool ctrl_x_mode_normal(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_NORMAL; +} + +bool ctrl_x_mode_scroll(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_SCROLL; +} + +bool ctrl_x_mode_whole_line(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_WHOLE_LINE; +} + +bool ctrl_x_mode_files(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_FILES; +} + +bool ctrl_x_mode_tags(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_TAGS; +} + +bool ctrl_x_mode_path_patterns(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_PATH_PATTERNS; +} + +bool ctrl_x_mode_path_defines(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_PATH_DEFINES; +} + +bool ctrl_x_mode_dictionary(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_DICTIONARY; +} + +bool ctrl_x_mode_thesaurus(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_THESAURUS; +} + +bool ctrl_x_mode_cmdline(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_CMDLINE || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X; +} + +bool ctrl_x_mode_function(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_FUNCTION; +} + +bool ctrl_x_mode_omni(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_OMNI; +} + +bool ctrl_x_mode_spell(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_SPELL; +} + +static bool ctrl_x_mode_eval(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_EVAL; +} + +bool ctrl_x_mode_line_or_eval(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_WHOLE_LINE || ctrl_x_mode == CTRL_X_EVAL; +} + +/// Whether other than default completion has been selected. +bool ctrl_x_mode_not_default(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode != CTRL_X_NORMAL; +} + +/// Whether CTRL-X was typed without a following character, +/// not including when in CTRL-X CTRL-V mode. +bool ctrl_x_mode_not_defined_yet(void) + FUNC_ATTR_PURE +{ + return ctrl_x_mode == CTRL_X_NOT_DEFINED_YET; +} + +/// Check that the "dict" or "tsr" option can be used. +/// +/// @param dict_opt check "dict" when true, "tsr" when false. +bool check_compl_option(bool dict_opt) +{ + if (dict_opt + ? (*curbuf->b_p_dict == NUL && *p_dict == NUL && !curwin->w_p_spell) + : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL + && *curbuf->b_p_tsrfu == NUL && *p_tsrfu == NUL)) { + ctrl_x_mode = CTRL_X_NORMAL; + edit_submode = NULL; + msg_attr((dict_opt + ? _("'dictionary' option is empty") + : _("'thesaurus' option is empty")), HL_ATTR(HLF_E)); + if (emsg_silent == 0) { + vim_beep(BO_COMPL); + setcursor(); + ui_flush(); + os_delay(2004L, false); + } + return false; + } + return true; +} + +/// Check that the character "c" a valid key to go to or keep us in CTRL-X mode? +/// This depends on the current mode. +/// +/// @param c character to check +bool vim_is_ctrl_x_key(int c) + FUNC_ATTR_WARN_UNUSED_RESULT +{ + // Always allow ^R - let its results then be checked + if (c == Ctrl_R) { + return true; + } + + // Accept <PageUp> and <PageDown> if the popup menu is visible. + if (ins_compl_pum_key(c)) { + return true; + } + + switch (ctrl_x_mode) { + case 0: // Not in any CTRL-X mode + return c == Ctrl_N || c == Ctrl_P || c == Ctrl_X; + case CTRL_X_NOT_DEFINED_YET: + case CTRL_X_CMDLINE_CTRL_X: + return c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E + || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB + || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P + || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V + || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O + || c == Ctrl_S || c == Ctrl_K || c == 's' + || c == Ctrl_Z; + case CTRL_X_SCROLL: + return c == Ctrl_Y || c == Ctrl_E; + case CTRL_X_WHOLE_LINE: + return c == Ctrl_L || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_FILES: + return c == Ctrl_F || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_DICTIONARY: + return c == Ctrl_K || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_THESAURUS: + return c == Ctrl_T || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_TAGS: + return c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_PATH_PATTERNS: + return c == Ctrl_P || c == Ctrl_N; + case CTRL_X_PATH_DEFINES: + return c == Ctrl_D || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_CMDLINE: + return c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N + || c == Ctrl_X; + case CTRL_X_FUNCTION: + return c == Ctrl_U || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_OMNI: + return c == Ctrl_O || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_SPELL: + return c == Ctrl_S || c == Ctrl_P || c == Ctrl_N; + case CTRL_X_EVAL: + return (c == Ctrl_P || c == Ctrl_N); + } + internal_error("vim_is_ctrl_x_key()"); + return false; +} + +/// Check that character "c" is part of the item currently being +/// completed. Used to decide whether to abandon complete mode when the menu +/// is visible. +/// +/// @param c character to check +bool ins_compl_accept_char(int c) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + if (ctrl_x_mode & CTRL_X_WANT_IDENT) { + // When expanding an identifier only accept identifier chars. + return vim_isIDc(c); + } + + switch (ctrl_x_mode) { + case CTRL_X_FILES: + // When expanding file name only accept file name chars. But not + // path separators, so that "proto/<Tab>" expands files in + // "proto", not "proto/" as a whole + return vim_isfilec(c) && !vim_ispathsep(c); + + case CTRL_X_CMDLINE: + case CTRL_X_CMDLINE_CTRL_X: + case CTRL_X_OMNI: + // Command line and Omni completion can work with just about any + // printable character, but do stop at white space. + return vim_isprintc(c) && !ascii_iswhite(c); + + case CTRL_X_WHOLE_LINE: + // For while line completion a space can be part of the line. + return vim_isprintc(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 +/// +/// @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, Direction 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; + 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. + + // Find actual length of completion. + { + const char_u *p = str; + actual_len = 0; + while (*p != NUL) { + MB_PTR_ADV(p); + actual_len++; + } + } + + // Find actual length of original text. + { + const char_u *p = compl_orig_text; + actual_compl_length = 0; + while (*p != NUL) { + MB_PTR_ADV(p); + actual_compl_length++; + } + } + + // "actual_len" may be smaller than "actual_compl_length" when using + // thesaurus, only use the minimum when comparing. + min_len = actual_len < actual_compl_length + ? actual_len : actual_compl_length; + + // Allocate wide character array for the completion and fill it. + int *const wca = xmalloc((size_t)actual_len * sizeof(*wca)); + { + const char_u *p = str; + for (i = 0; i < actual_len; i++) { + wca[i] = mb_ptr2char_adv(&p); + } + } + + // Rule 1: Were any chars converted to lower? + { + const char_u *p = compl_orig_text; + for (i = 0; i < min_len; i++) { + c = mb_ptr2char_adv(&p); + if (mb_islower(c)) { + has_lower = true; + if (mb_isupper(wca[i])) { + // Rule 1 is satisfied. + for (i = actual_compl_length; i < actual_len; i++) { + wca[i] = mb_tolower(wca[i]); + } + break; + } + } + } + } + + // Rule 2: No lower case, 2nd consecutive letter converted to + // upper case. + if (!has_lower) { + const char_u *p = compl_orig_text; + for (i = 0; i < min_len; i++) { + c = mb_ptr2char_adv(&p); + if (was_letter && mb_isupper(c) && mb_islower(wca[i])) { + // Rule 2 is satisfied. + for (i = actual_compl_length; i < actual_len; i++) { + wca[i] = mb_toupper(wca[i]); + } + break; + } + was_letter = mb_islower(c) || mb_isupper(c); + } + } + + // Copy the original case of the part we typed. + { + const char_u *p = compl_orig_text; + for (i = 0; i < min_len; i++) { + c = mb_ptr2char_adv(&p); + if (mb_islower(c)) { + wca[i] = mb_tolower(wca[i]); + } else if (mb_isupper(c)) { + wca[i] = mb_toupper(wca[i]); + } + } + } + + // Generate encoding specific output from wide character array. + // Multi-byte characters can occupy up to five bytes more than + // ASCII characters, and we also need one byte for NUL, so stay + // six bytes away from the edge of IObuff. + { + char_u *p = IObuff; + i = 0; + while (i < actual_len && (p - IObuff + 6) < IOSIZE) { + p += utf_char2bytes(wca[i++], (char *)p); + } + *p = NUL; + } + + xfree(wca); + + str = IObuff; + } + if (cont_s_ipos) { + flags |= CP_CONT_S_IPOS; + } + if (icase) { + flags |= CP_ICASE; + } + + return ins_compl_add(str, len, fname, NULL, false, NULL, 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] 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. +/// @param[in] cptext_allocated If true, will not copy cptext strings. +/// +/// @note Will free strings in case of error. +/// cptext itself will not be freed. +/// @param[in] cdir Completion direction. +/// @param[in] adup True if duplicate matches are to be accepted. +/// +/// @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, char_u *const fname, + char_u *const *const cptext, const bool cptext_allocated, + typval_T *user_data, const Direction cdir, int flags_arg, const bool adup) + FUNC_ATTR_NONNULL_ARG(1) +{ + compl_T *match; + const Direction dir = (cdir == kDirectionNotSet ? compl_direction : cdir); + int flags = flags_arg; + + if (flags & CP_FAST) { + fast_breakcheck(); + } else { + os_breakcheck(); + } +#define FREE_CPTEXT(cptext, cptext_allocated) \ + do { \ + if ((cptext) != NULL && (cptext_allocated)) { \ + for (size_t i = 0; i < CPT_COUNT; i++) { \ + xfree((cptext)[i]); \ + } \ + } \ + } while (0) + if (got_int) { + FREE_CPTEXT(cptext, cptext_allocated); + return FAIL; + } + if (len < 0) { + len = (int)STRLEN(str); + } + + // If the same match is already present, don't add it. + if (compl_first_match != NULL && !adup) { + match = compl_first_match; + do { + if (!(match->cp_flags & CP_ORIGINAL_TEXT) + && STRNCMP(match->cp_str, str, len) == 0 + && match->cp_str[len] == NUL) { + FREE_CPTEXT(cptext, cptext_allocated); + return NOTDONE; + } + match = match->cp_next; + } while (match != NULL && match != compl_first_match); + } + + // Remove any popup menu before changing the list of matches. + ins_compl_del_pum(); + + // Allocate a new match structure. + // Copy the values to the new match structure. + match = xcalloc(1, sizeof(compl_T)); + match->cp_number = -1; + if (flags & CP_ORIGINAL_TEXT) { + match->cp_number = 0; + } + match->cp_str = vim_strnsave(str, (size_t)len); + + // 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) { + match->cp_fname = compl_curr_match->cp_fname; + } else if (fname != NULL) { + match->cp_fname = vim_strsave(fname); + flags |= CP_FREE_FNAME; + } else { + match->cp_fname = NULL; + } + match->cp_flags = flags; + + if (cptext != NULL) { + int i; + + for (i = 0; i < CPT_COUNT; i++) { + if (cptext[i] == NULL) { + continue; + } + if (*cptext[i] != NUL) { + match->cp_text[i] = (cptext_allocated + ? cptext[i] + : (char_u *)xstrdup((char *)cptext[i])); + } else if (cptext_allocated) { + xfree(cptext[i]); + } + } + } + + if (user_data != NULL) { + match->cp_user_data = *user_data; + } + + // Link the new match structure in the list of matches. + if (compl_first_match == NULL) { + match->cp_next = match->cp_prev = NULL; + } else if (dir == FORWARD) { + match->cp_next = compl_curr_match->cp_next; + match->cp_prev = compl_curr_match; + } else { // BACKWARD + match->cp_next = compl_curr_match; + match->cp_prev = compl_curr_match->cp_prev; + } + if (match->cp_next) { + match->cp_next->cp_prev = match; + } + if (match->cp_prev) { + match->cp_prev->cp_next = match; + } else { // if there's nothing before, it is the first match + compl_first_match = match; + } + compl_curr_match = match; + + // Find the longest common string if still doing that. + 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_flags". +/// +/// @param match completion match +/// @param str character string to check +/// @param len length of "str" +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_flags & CP_EQUAL) { + return true; + } + if (match->cp_flags & CP_ICASE) { + return STRNICMP(match->cp_str, str, len) == 0; + } + return STRNCMP(match->cp_str, str, len) == 0; +} + +/// Reduce the longest common string for match "match". +static void ins_compl_longest_match(compl_T *match) +{ + char_u *p, *s; + int c1, c2; + int had_match; + + if (compl_leader == NULL) { + // First match, use it as a whole. + compl_leader = vim_strsave(match->cp_str); + had_match = (curwin->w_cursor.col > compl_col); + ins_compl_delete(); + ins_bytes(compl_leader + get_compl_len()); + ins_redraw(false); + + // When the match isn't there (to avoid matching itself) remove it + // again after redrawing. + if (!had_match) { + ins_compl_delete(); + } + compl_used_match = false; + } else { + // Reduce the text if this match differs from compl_leader. + p = compl_leader; + s = match->cp_str; + while (*p != NUL) { + c1 = utf_ptr2char((char *)p); + c2 = utf_ptr2char((char *)s); + + if ((match->cp_flags & CP_ICASE) + ? (mb_tolower(c1) != mb_tolower(c2)) + : (c1 != c2)) { + break; + } + MB_PTR_ADV(p); + MB_PTR_ADV(s); + } + + if (*p != NUL) { + // Leader was shortened, need to change the inserted text. + *p = NUL; + had_match = (curwin->w_cursor.col > compl_col); + ins_compl_delete(); + ins_bytes(compl_leader + get_compl_len()); + ins_redraw(false); + + // When the match isn't there (to avoid matching itself) remove it + // again after redrawing. + if (!had_match) { + ins_compl_delete(); + } + } + + compl_used_match = false; + } +} + +/// Add an array of matches to the list of matches. +/// Frees matches[]. +static void ins_compl_add_matches(int num_matches, char_u **matches, int icase) + FUNC_ATTR_NONNULL_ALL +{ + int add_r = OK; + Direction dir = compl_direction; + + for (int i = 0; i < num_matches && add_r != FAIL; i++) { + if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, false, NULL, dir, + CP_FAST | (icase ? CP_ICASE : 0), + false)) == OK) { + // If dir was BACKWARD then honor it just once. + dir = FORWARD; + } + } + FreeWild(num_matches, matches); +} + +/// Make the completion list cyclic. +/// Return the number of matches (excluding the original). +static int ins_compl_make_cyclic(void) +{ + compl_T *match; + int count = 0; + + if (compl_first_match != NULL) { + // Find the end of the list. + match = compl_first_match; + // there's always an entry for the compl_orig_text, it doesn't count. + while (match->cp_next != NULL && match->cp_next != compl_first_match) { + match = match->cp_next; + count++; + } + match->cp_next = compl_first_match; + compl_first_match->cp_prev = match; + } + return count; +} + +/// Return whether there currently is a shown match. +bool ins_compl_has_shown_match(void) +{ + return compl_shown_match == NULL || compl_shown_match != compl_shown_match->cp_next; +} + +/// Return whether the shown match is long enough. +bool ins_compl_long_shown_match(void) +{ + return compl_shown_match != NULL && compl_shown_match->cp_str != NULL + && (colnr_T)STRLEN(compl_shown_match->cp_str) > curwin->w_cursor.col - compl_col; +} + +/// Set variables that store noselect and noinsert behavior from the +/// 'completeopt' value. +void completeopt_was_set(void) +{ + compl_no_insert = false; + compl_no_select = false; + if (strstr((char *)p_cot, "noselect") != NULL) { + compl_no_select = true; + } + if (strstr((char *)p_cot, "noinsert") != NULL) { + compl_no_insert = true; + } +} + +/// "compl_match_array" points the currently displayed list of entries in the +/// popup menu. It is NULL when there is no popup menu. +static pumitem_T *compl_match_array = NULL; +static int compl_match_arraysize; + +/// Remove any popup menu. +static void ins_compl_del_pum(void) +{ + if (compl_match_array != NULL) { + pum_undisplay(false); + XFREE_CLEAR(compl_match_array); + } +} + +/// Check if the popup menu should be displayed. +bool pum_wanted(void) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + // "completeopt" must contain "menu" or "menuone" + return vim_strchr((char *)p_cot, 'm') != NULL; +} + +/// Check that there are two or more matches to be shown in the popup menu. +/// One if "completopt" contains "menuone". +static bool pum_enough_matches(void) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + // Don't display the popup menu if there are no matches or there is only + // one (ignoring the original text). + compl_T *comp = compl_first_match; + int i = 0; + do { + if (comp == NULL + || ((comp->cp_flags & CP_ORIGINAL_TEXT) == 0 && ++i == 2)) { + break; + } + comp = comp->cp_next; + } while (comp != compl_first_match); + + if (strstr((char *)p_cot, "menuone") != NULL) { + return i >= 1; + } + return i >= 2; +} + +/// Convert to complete item dict +static dict_T *ins_compl_dict_alloc(compl_T *match) +{ + // { word, abbr, menu, kind, info } + dict_T *dict = tv_dict_alloc_lock(VAR_FIXED); + tv_dict_add_str(dict, S_LEN("word"), EMPTY_IF_NULL(match->cp_str)); + tv_dict_add_str(dict, S_LEN("abbr"), EMPTY_IF_NULL(match->cp_text[CPT_ABBR])); + tv_dict_add_str(dict, S_LEN("menu"), EMPTY_IF_NULL(match->cp_text[CPT_MENU])); + tv_dict_add_str(dict, S_LEN("kind"), EMPTY_IF_NULL(match->cp_text[CPT_KIND])); + tv_dict_add_str(dict, S_LEN("info"), EMPTY_IF_NULL(match->cp_text[CPT_INFO])); + if (match->cp_user_data.v_type == VAR_UNKNOWN) { + tv_dict_add_str(dict, S_LEN("user_data"), ""); + } else { + tv_dict_add_tv(dict, S_LEN("user_data"), &match->cp_user_data); + } + return dict; +} + +static void trigger_complete_changed_event(int cur) +{ + static bool recursive = false; + save_v_event_T save_v_event; + + if (recursive) { + return; + } + + dict_T *v_event = get_v_event(&save_v_event); + if (cur < 0) { + tv_dict_add_dict(v_event, S_LEN("completed_item"), tv_dict_alloc()); + } else { + dict_T *item = ins_compl_dict_alloc(compl_curr_match); + tv_dict_add_dict(v_event, S_LEN("completed_item"), item); + } + pum_set_event_info(v_event); + tv_dict_set_keys_readonly(v_event); + + recursive = true; + textlock++; + apply_autocmds(EVENT_COMPLETECHANGED, NULL, NULL, false, curbuf); + textlock--; + recursive = false; + + restore_v_event(v_event, &save_v_event); +} + +/// Show the popup menu for the list of matches. +/// Also adjusts "compl_shown_match" to an entry that is actually displayed. +void ins_compl_show_pum(void) +{ + compl_T *compl; + compl_T *shown_compl = NULL; + bool did_find_shown_match = false; + bool shown_match_ok = false; + int i; + int cur = -1; + colnr_T col; + int lead_len = 0; + bool array_changed = false; + + if (!pum_wanted() || !pum_enough_matches()) { + return; + } + + // Dirty hard-coded hack: remove any matchparen highlighting. + do_cmdline_cmd("if exists('g:loaded_matchparen')|3match none|endif"); + + // Update the screen before drawing the popup menu over it. + update_screen(0); + + if (compl_match_array == NULL) { + array_changed = true; + // Need to build the popup menu list. + compl_match_arraysize = 0; + compl = compl_first_match; + // If it's user complete function and refresh_always, + // do not use "compl_leader" as prefix filter. + if (ins_compl_need_restart()) { + XFREE_CLEAR(compl_leader); + } + if (compl_leader != NULL) { + lead_len = (int)STRLEN(compl_leader); + } + do { + if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 + && (compl_leader == NULL + || ins_compl_equal(compl, compl_leader, (size_t)lead_len))) { + compl_match_arraysize++; + } + compl = compl->cp_next; + } while (compl != NULL && compl != compl_first_match); + if (compl_match_arraysize == 0) { + return; + } + + assert(compl_match_arraysize >= 0); + compl_match_array = xcalloc((size_t)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 & CP_ORIGINAL_TEXT) { + shown_match_ok = true; + } + + i = 0; + compl = compl_first_match; + do { + if ((compl->cp_flags & CP_ORIGINAL_TEXT) == 0 + && (compl_leader == NULL + || ins_compl_equal(compl, compl_leader, (size_t)lead_len))) { + if (!shown_match_ok) { + if (compl == compl_shown_match || did_find_shown_match) { + // 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. + shown_compl = compl; + } + cur = i; + } + + if (compl->cp_text[CPT_ABBR] != NULL) { + compl_match_array[i].pum_text = + compl->cp_text[CPT_ABBR]; + } else { + compl_match_array[i].pum_text = compl->cp_str; + } + compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; + compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; + if (compl->cp_text[CPT_MENU] != NULL) { + compl_match_array[i++].pum_extra = + compl->cp_text[CPT_MENU]; + } else { + compl_match_array[i++].pum_extra = compl->cp_fname; + } + } + + if (compl == compl_shown_match) { + did_find_shown_match = true; + + // When the original text is the shown match don't set + // compl_shown_match. + 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; + } + } + compl = compl->cp_next; + } while (compl != NULL && compl != compl_first_match); + + if (!shown_match_ok) { // no displayed match at all + cur = -1; + } + } else { + // popup menu already exists, only need to find the current item. + for (i = 0; i < compl_match_arraysize; i++) { + if (compl_match_array[i].pum_text == compl_shown_match->cp_str + || compl_match_array[i].pum_text + == compl_shown_match->cp_text[CPT_ABBR]) { + cur = i; + break; + } + } + } + + // In Replace mode when a $ is displayed at the end of the line only + // part of the screen would be updated. We do need to redraw here. + dollar_vcol = -1; + + // Compute the screen column of the start of the completed text. + // Use the cursor to get all wrapping and other settings right. + col = curwin->w_cursor.col; + curwin->w_cursor.col = compl_col; + pum_selected_item = cur; + pum_display(compl_match_array, compl_match_arraysize, cur, array_changed, 0); + curwin->w_cursor.col = col; + + if (has_event(EVENT_COMPLETECHANGED)) { + trigger_complete_changed_event(cur); + } +} + +#define DICT_FIRST (1) ///< use just first element in "dict" +#define DICT_EXACT (2) ///< "dict" is the exact name of a file + +/// Add any identifiers that match the given pattern in the list of dictionary +/// files "dict_start" to the list of completions. +/// +/// @param flags DICT_FIRST and/or DICT_EXACT +/// @param thesaurus Thesaurus completion +static void ins_compl_dictionaries(char_u *dict_start, char_u *pat, int flags, int thesaurus) +{ + char_u *dict = dict_start; + char_u *ptr; + char_u *buf; + regmatch_T regmatch; + char_u **files; + int count; + int save_p_scs; + Direction dir = compl_direction; + + if (*dict == NUL) { + // When 'dictionary' is empty and spell checking is enabled use + // "spell". + if (!thesaurus && curwin->w_p_spell) { + dict = (char_u *)"spell"; + } else { + return; + } + } + + buf = xmalloc(LSIZE); + regmatch.regprog = NULL; // so that we can goto theend + + // If 'infercase' is set, don't use 'smartcase' here + save_p_scs = p_scs; + if (curbuf->b_p_inf) { + p_scs = false; + } + + // When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern + // to only match at the start of a line. Otherwise just match the + // pattern. Also need to double backslashes. + if (ctrl_x_mode_line_or_eval()) { + char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); + + size_t len = STRLEN(pat_esc) + 10; + ptr = xmalloc(len); + vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); + regmatch.regprog = vim_regcomp((char *)ptr, RE_MAGIC); + xfree(pat_esc); + xfree(ptr); + } else { + regmatch.regprog = vim_regcomp((char *)pat, p_magic ? RE_MAGIC : 0); + if (regmatch.regprog == NULL) { + goto theend; + } + } + + // ignore case depends on 'ignorecase', 'smartcase' and "pat" + regmatch.rm_ic = ignorecase(pat); + while (*dict != NUL && !got_int && !compl_interrupted) { + // copy one dictionary file name into buf + if (flags == DICT_EXACT) { + count = 1; + files = &dict; + } else { + // Expand wildcards in the dictionary name, but do not allow + // backticks (for security, the 'dict' option may have been set in + // a modeline). + copy_option_part((char **)&dict, (char *)buf, LSIZE, ","); + if (!thesaurus && STRCMP(buf, "spell") == 0) { + count = -1; + } else if (vim_strchr((char *)buf, '`') != NULL + || expand_wildcards(1, &buf, &count, &files, + EW_FILE|EW_SILENT) != OK) { + count = 0; + } + } + + if (count == -1) { + // Complete from active spelling. Skip "\<" in the pattern, we + // don't use it as a RE. + if (pat[0] == '\\' && pat[1] == '<') { + ptr = pat + 2; + } else { + ptr = pat; + } + spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); + } else if (count > 0) { // avoid warning for using "files" uninit + ins_compl_files(count, files, thesaurus, flags, + ®match, buf, &dir); + if (flags != DICT_EXACT) { + FreeWild(count, files); + } + } + if (flags != 0) { + break; + } + } + +theend: + p_scs = save_p_scs; + vim_regfree(regmatch.regprog); + xfree(buf); +} + +static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, + regmatch_T *regmatch, char_u *buf, Direction *dir) + FUNC_ATTR_NONNULL_ARG(2, 7) +{ + char_u *ptr; + int i; + FILE *fp; + int add_r; + + for (i = 0; i < count && !got_int && !compl_interrupted; i++) { + fp = os_fopen((char *)files[i], "r"); // open dictionary file + if (flags != DICT_EXACT) { + msg_hist_off = true; // reset in msg_trunc_attr() + vim_snprintf((char *)IObuff, IOSIZE, + _("Scanning dictionary: %s"), (char *)files[i]); + (void)msg_trunc_attr((char *)IObuff, true, HL_ATTR(HLF_R)); + } + + if (fp == NULL) { + continue; + } + + // Read dictionary file line by line. + // Check each line for a match. + while (!got_int && !compl_interrupted + && !vim_fgets(buf, LSIZE, fp)) { + ptr = buf; + while (vim_regexec(regmatch, (char *)buf, (colnr_T)(ptr - buf))) { + ptr = regmatch->startp[0]; + if (ctrl_x_mode_line_or_eval()) { + ptr = find_line_end(ptr); + } else { + ptr = find_word_end(ptr); + } + add_r = ins_compl_add_infercase(regmatch->startp[0], + (int)(ptr - regmatch->startp[0]), + p_ic, files[i], *dir, false); + if (thesaurus) { + char_u *wstart; + + // Add the other matches on the line + ptr = buf; + while (!got_int) { + // Find start of the next word. Skip white + // space and punctuation. + ptr = find_word_start(ptr); + if (*ptr == NUL || *ptr == NL) { + break; + } + wstart = ptr; + + // Find end of the word. + // Japanese words may have characters in + // different classes, only separate words + // with single-byte non-word characters. + while (*ptr != NUL) { + const int l = utfc_ptr2len((char *)ptr); + + if (l < 2 && !vim_iswordc(*ptr)) { + break; + } + ptr += l; + } + + // 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) { + // if dir was BACKWARD then honor it just once + *dir = FORWARD; + } else if (add_r == FAIL) { + break; + } + // avoid expensive call to vim_regexec() when at end + // of line + if (*ptr == '\n' || got_int) { + break; + } + } + line_breakcheck(); + ins_compl_check_keys(50, false); + } + fclose(fp); + } +} + +/// Find the start of the next word. +/// Returns a pointer to the first char of the word. Also stops at a NUL. +char_u *find_word_start(char_u *ptr) + FUNC_ATTR_PURE +{ + while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) { + ptr += utfc_ptr2len((char *)ptr); + } + return ptr; +} + +/// Find the end of the word. Assumes it starts inside a word. +/// Returns a pointer to just after the word. +char_u *find_word_end(char_u *ptr) + FUNC_ATTR_PURE +{ + const int start_class = mb_get_class(ptr); + if (start_class > 1) { + while (*ptr != NUL) { + ptr += utfc_ptr2len((char *)ptr); + if (mb_get_class(ptr) != start_class) { + break; + } + } + } + return ptr; +} + +/// Find the end of the line, omitting CR and NL at the end. +/// Returns a pointer to just after the line. +static char_u *find_line_end(char_u *ptr) +{ + char_u *s; + + s = ptr + STRLEN(ptr); + while (s > ptr && (s[-1] == CAR || s[-1] == NL)) { + s--; + } + return s; +} + +/// Free the list of completions +static void ins_compl_free(void) +{ + compl_T *match; + + XFREE_CLEAR(compl_pattern); + XFREE_CLEAR(compl_leader); + + if (compl_first_match == NULL) { + return; + } + + ins_compl_del_pum(); + pum_clear(); + + compl_curr_match = compl_first_match; + do { + 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 & CP_FREE_FNAME) { + xfree(match->cp_fname); + } + for (int i = 0; i < CPT_COUNT; i++) { + xfree(match->cp_text[i]); + } + tv_clear(&match->cp_user_data); + xfree(match); + } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); + compl_first_match = compl_curr_match = NULL; + compl_shown_match = NULL; + compl_old_match = NULL; +} + +void ins_compl_clear(void) +{ + compl_cont_status = 0; + compl_started = false; + compl_matches = 0; + XFREE_CLEAR(compl_pattern); + XFREE_CLEAR(compl_leader); + edit_submode_extra = NULL; + XFREE_CLEAR(compl_orig_text); + compl_enter_selects = false; + // clear v:completed_item + set_vim_var_dict(VV_COMPLETED_ITEM, tv_dict_alloc_lock(VAR_FIXED)); +} + +/// Check that Insert completion is active. +bool ins_compl_active(void) + FUNC_ATTR_PURE +{ + return compl_started; +} + +/// Selected one of the matches. When false the match was edited or using the +/// longest common string. +bool ins_compl_used_match(void) +{ + return compl_used_match; +} + +/// Initialize get longest common string. +void ins_compl_init_get_longest(void) +{ + compl_get_longest = false; +} + +/// Returns true when insert completion is interrupted. +bool ins_compl_interrupted(void) +{ + return compl_interrupted; +} + +/// Returns true if the <Enter> key selects a match in the completion popup +/// menu. +bool ins_compl_enter_selects(void) +{ + return compl_enter_selects; +} + +/// Return the column where the text starts that is being completed +colnr_T ins_compl_col(void) +{ + return compl_col; +} + +/// Delete one character before the cursor and show the subset of the matches +/// that match the word that is now before the cursor. +/// Returns the character to be used, NUL if the work is done and another char +/// to be got from the user. +int ins_compl_bs(void) +{ + char_u *line = get_cursor_line_ptr(); + char_u *p = line + curwin->w_cursor.col; + MB_PTR_BACK(line, p); + ptrdiff_t p_off = p - line; + + // Stop completion when the whole word was deleted. For Omni completion + // allow the word to be deleted, we won't match everything. + // Respect the 'backspace' option. + if ((int)(p - line) - (int)compl_col < 0 + || ((int)(p - line) - (int)compl_col == 0 && !ctrl_x_mode_omni()) + || ctrl_x_mode_eval() + || (!can_bs(BS_START) && (int)(p - line) - (int)compl_col + - compl_length < 0)) { + return K_BS; + } + + // Deleted more than what was used to find matches or didn't finish + // finding all matches: need to look for matches all over again. + if (curwin->w_cursor.col <= compl_col + compl_length + || ins_compl_need_restart()) { + ins_compl_restart(); + } + + // ins_compl_restart() calls update_screen(0) which may invalidate the pointer + // TODO(bfredl): get rid of random update_screen() calls deep inside completion logic + line = get_cursor_line_ptr(); + + xfree(compl_leader); + compl_leader = vim_strnsave(line + compl_col, (size_t)(p_off - (ptrdiff_t)compl_col)); + ins_compl_new_leader(); + if (compl_shown_match != NULL) { + // Make sure current match is not a hidden item. + compl_curr_match = compl_shown_match; + } + + return NUL; +} + +/// Check that we need to find matches again, ins_compl_restart() is to +/// be called. +static bool ins_compl_need_restart(void) + FUNC_ATTR_PURE +{ + // Return true if we didn't complete finding matches or when the + // "completefunc" returned "always" in the "refresh" dictionary item. + return compl_was_interrupted + || ((ctrl_x_mode_function() || ctrl_x_mode_omni()) + && compl_opt_refresh_always); +} + +/// Called after changing "compl_leader". +/// Show the popup menu with a different set of matches. +/// May also search for matches again if the previous search was interrupted. +static void ins_compl_new_leader(void) +{ + ins_compl_del_pum(); + ins_compl_delete(); + ins_bytes(compl_leader + get_compl_len()); + compl_used_match = false; + + if (compl_started) { + ins_compl_set_original_text(compl_leader); + } else { + spell_bad_len = 0; // need to redetect bad word + // Matches were cleared, need to search for them now. + // Set "compl_restarting" to avoid that the first match is inserted. + compl_restarting = true; + if (ins_complete(Ctrl_N, true) == FAIL) { + compl_cont_status = 0; + } + compl_restarting = false; + } + + compl_enter_selects = !compl_used_match; + + // Show the popup menu with a different set of matches. + ins_compl_show_pum(); + + // Don't let Enter select the original text when there is no popup menu. + // Don't let Enter select when use user function and refresh_always is set + if (compl_match_array == NULL || ins_compl_need_restart()) { + compl_enter_selects = false; + } +} + +/// Return the length of the completion, from the completion start column to +/// the cursor column. Making sure it never goes below zero. +static int get_compl_len(void) +{ + int off = (int)curwin->w_cursor.col - (int)compl_col; + + if (off < 0) { + return 0; + } + return off; +} + +/// Append one character to the match leader. May reduce the number of +/// matches. +void ins_compl_addleader(int c) +{ + int cc; + + if (stop_arrow() == FAIL) { + return; + } + if ((cc = utf_char2len(c)) > 1) { + char buf[MB_MAXBYTES + 1]; + + utf_char2bytes(c, (char *)buf); + buf[cc] = NUL; + ins_char_bytes((char_u *)buf, (size_t)cc); + } else { + ins_char(c); + } + + // If we didn't complete finding matches we must search again. + if (ins_compl_need_restart()) { + ins_compl_restart(); + } + + xfree(compl_leader); + compl_leader = vim_strnsave(get_cursor_line_ptr() + compl_col, + (size_t)(curwin->w_cursor.col - compl_col)); + ins_compl_new_leader(); +} + +/// Setup for finding completions again without leaving CTRL-X mode. Used when +/// BS or a key was typed while still searching for matches. +static void ins_compl_restart(void) +{ + // update screen before restart. + // so if complete is blocked, + // will stay to the last popup menu and reduce flicker + update_screen(0); + ins_compl_free(); + compl_started = false; + compl_matches = 0; + compl_cont_status = 0; + compl_cont_mode = 0; +} + +/// 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 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 & 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 & CP_ORIGINAL_TEXT)) { + xfree(compl_first_match->cp_prev->cp_str); + compl_first_match->cp_prev->cp_str = vim_strsave(str); + } +} + +/// Append one character to the match leader. May reduce the number of +/// matches. +void ins_compl_addfrommatch(void) +{ + char_u *p; + int len = (int)curwin->w_cursor.col - (int)compl_col; + int c; + compl_T *cp; + assert(compl_shown_match != NULL); + p = compl_shown_match->cp_str; + 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 & CP_ORIGINAL_TEXT) { + p = NULL; + for (cp = compl_shown_match->cp_next; cp != NULL + && cp != compl_first_match; cp = cp->cp_next) { + if (compl_leader == NULL + || ins_compl_equal(cp, compl_leader, STRLEN(compl_leader))) { + p = cp->cp_str; + break; + } + } + if (p == NULL || (int)STRLEN(p) <= len) { + return; + } + } else { + return; + } + } + p += len; + c = utf_ptr2char((char *)p); + ins_compl_addleader(c); +} + +/// Prepare for Insert mode completion, or stop it. +/// Called just after typing a character in Insert mode. +/// +/// @param c character that was typed +/// +/// @return true when the character is not to be inserted; +bool ins_compl_prep(int c) +{ + char_u *ptr; + bool retval = false; + const int prev_mode = ctrl_x_mode; + + // Forget any previous 'special' messages if this is actually + // a ^X mode key - bar ^R, in which case we wait to see what it gives us. + if (c != Ctrl_R && vim_is_ctrl_x_key(c)) { + edit_submode_extra = NULL; + } + + // Ignore end of Select mode mapping and mouse scroll buttons. + if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP + || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_EVENT + || c == K_COMMAND || c == K_LUA) { + return retval; + } + + if (ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X && c != Ctrl_X) { + if (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_Z || ins_compl_pum_key(c) + || !vim_is_ctrl_x_key(c)) { + // Not starting another completion mode. + ctrl_x_mode = CTRL_X_CMDLINE; + + // CTRL-X CTRL-Z should stop completion without inserting anything + if (c == Ctrl_Z) { + retval = true; + } + } else { + ctrl_x_mode = CTRL_X_CMDLINE; + + // Other CTRL-X keys first stop completion, then start another + // completion mode. + ins_compl_prep(' '); + ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; + } + } + + // Set "compl_get_longest" when finding the first matches. + if (ctrl_x_mode_not_defined_yet() + || (ctrl_x_mode_normal() && !compl_started)) { + compl_get_longest = (strstr((char *)p_cot, "longest") != NULL); + compl_used_match = true; + } + + if (ctrl_x_mode_not_defined_yet()) { + // We have just typed CTRL-X and aren't quite sure which CTRL-X mode + // it will be yet. Now we decide. + switch (c) { + case Ctrl_E: + case Ctrl_Y: + ctrl_x_mode = CTRL_X_SCROLL; + if (!(State & REPLACE_FLAG)) { + edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); + } else { + edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); + } + edit_submode_pre = NULL; + showmode(); + break; + case Ctrl_L: + ctrl_x_mode = CTRL_X_WHOLE_LINE; + break; + case Ctrl_F: + ctrl_x_mode = CTRL_X_FILES; + break; + case Ctrl_K: + ctrl_x_mode = CTRL_X_DICTIONARY; + break; + case Ctrl_R: + // Simply allow ^R to happen without affecting ^X mode + break; + case Ctrl_T: + ctrl_x_mode = CTRL_X_THESAURUS; + break; + case Ctrl_U: + ctrl_x_mode = CTRL_X_FUNCTION; + break; + case Ctrl_O: + ctrl_x_mode = CTRL_X_OMNI; + break; + case 's': + case Ctrl_S: + ctrl_x_mode = CTRL_X_SPELL; + emsg_off++; // Avoid getting the E756 error twice. + spell_back_to_badword(); + emsg_off--; + break; + case Ctrl_RSB: + ctrl_x_mode = CTRL_X_TAGS; + break; + case Ctrl_I: + case K_S_TAB: + ctrl_x_mode = CTRL_X_PATH_PATTERNS; + break; + case Ctrl_D: + ctrl_x_mode = CTRL_X_PATH_DEFINES; + break; + case Ctrl_V: + case Ctrl_Q: + ctrl_x_mode = CTRL_X_CMDLINE; + break; + case Ctrl_Z: + ctrl_x_mode = CTRL_X_NORMAL; + edit_submode = NULL; + showmode(); + retval = true; + break; + case Ctrl_P: + case Ctrl_N: + // ^X^P means LOCAL expansion if nothing interrupted (eg we + // just started ^X mode, or there were enough ^X's to cancel + // the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) + // do normal expansion when interrupting a different mode (say + // ^X^F^X^P or ^P^X^X^P, see below) + // nothing changes if interrupting mode 0, (eg, the flag + // doesn't change when going to ADDING mode -- Acevedo + if (!(compl_cont_status & CONT_INTRPT)) { + compl_cont_status |= CONT_LOCAL; + } else if (compl_cont_mode != 0) { + compl_cont_status &= ~CONT_LOCAL; + } + FALLTHROUGH; + default: + // If we have typed at least 2 ^X's... for modes != 0, we set + // compl_cont_status = 0 (eg, as if we had just started ^X + // mode). + // For mode 0, we set "compl_cont_mode" to an impossible + // value, in both cases ^X^X can be used to restart the same + // mode (avoiding ADDING mode). + // Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start + // 'complete' and local ^P expansions respectively. + // In mode 0 an extra ^X is needed since ^X^P goes to ADDING + // mode -- Acevedo + if (c == Ctrl_X) { + if (compl_cont_mode != 0) { + compl_cont_status = 0; + } else { + compl_cont_mode = CTRL_X_NOT_DEFINED_YET; + } + } + ctrl_x_mode = CTRL_X_NORMAL; + edit_submode = NULL; + showmode(); + break; + } + } else if (ctrl_x_mode_not_default()) { + // We're already in CTRL-X mode, do we stay in it? + if (!vim_is_ctrl_x_key(c)) { + if (ctrl_x_mode_scroll()) { + ctrl_x_mode = CTRL_X_NORMAL; + } else { + ctrl_x_mode = CTRL_X_FINISHED; + } + edit_submode = NULL; + } + showmode(); + } + + if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) { + // Show error message from attempted keyword completion (probably + // 'Pattern not found') until another key is hit, then go back to + // showing what mode we are in. + showmode(); + if ((ctrl_x_mode_normal() + && c != Ctrl_N + && c != Ctrl_P + && c != Ctrl_R + && !ins_compl_pum_key(c)) + || ctrl_x_mode == CTRL_X_FINISHED) { + // Get here when we have finished typing a sequence of ^N and + // ^P or other completion characters in CTRL-X mode. Free up + // memory that was used, and make sure we can redo the insert. + if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E) { + // If any of the original typed text has been changed, eg when + // ignorecase is set, we must add back-spaces to the redo + // buffer. We add as few as necessary to delete just the part + // of the original text that has changed. + // When using the longest match, edited the match or used + // CTRL-E then don't use the current match. + if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) { + ptr = compl_curr_match->cp_str; + } else { + ptr = NULL; + } + ins_compl_fixRedoBufForLeader(ptr); + } + + bool want_cindent = (can_cindent_get() && cindent_on()); + + // When completing whole lines: fix indent for 'cindent'. + // Otherwise, break line if it's too long. + if (compl_cont_mode == CTRL_X_WHOLE_LINE) { + // re-indent the current line + if (want_cindent) { + do_c_expr_indent(); + want_cindent = false; // don't do it again + } + } else { + int prev_col = curwin->w_cursor.col; + + // put the cursor on the last char, for 'tw' formatting + if (prev_col > 0) { + dec_cursor(); + } + + if (!arrow_used && !ins_need_undo_get() && c != Ctrl_E) { + insertchar(NUL, 0, -1); + } + + if (prev_col > 0 + && get_cursor_line_ptr()[curwin->w_cursor.col] != NUL) { + inc_cursor(); + } + } + + // If the popup menu is displayed pressing CTRL-Y means accepting + // the selection without inserting anything. When + // compl_enter_selects is set the Enter key does the same. + if ((c == Ctrl_Y || (compl_enter_selects + && (c == CAR || c == K_KENTER || c == NL))) + && pum_visible()) { + retval = true; + } + + // CTRL-E means completion is Ended, go back to the typed text. + // but only do this, if the Popup is still visible + if (c == Ctrl_E) { + ins_compl_delete(); + if (compl_leader != NULL) { + ins_bytes(compl_leader + get_compl_len()); + } else if (compl_first_match != NULL) { + ins_bytes(compl_orig_text + get_compl_len()); + } + retval = true; + } + + auto_format(false, true); + + // Trigger the CompleteDonePre event to give scripts a chance to + // act upon the completion before clearing the info, and restore + // ctrl_x_mode, so that complete_info() can be used. + ctrl_x_mode = prev_mode; + ins_apply_autocmds(EVENT_COMPLETEDONEPRE); + + ins_compl_free(); + compl_started = false; + compl_matches = 0; + if (!shortmess(SHM_COMPLETIONMENU)) { + msg_clr_cmdline(); // necessary for "noshowmode" + } + ctrl_x_mode = CTRL_X_NORMAL; + compl_enter_selects = false; + if (edit_submode != NULL) { + edit_submode = NULL; + showmode(); + } + + // Avoid the popup menu remains displayed when leaving the + // command line window. + if (c == Ctrl_C && cmdwin_type != 0) { + update_screen(0); + } + + // Indent now if a key was typed that is in 'cinkeys'. + if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) { + do_c_expr_indent(); + } + // Trigger the CompleteDone event to give scripts a chance to act + // upon the end of completion. + ins_apply_autocmds(EVENT_COMPLETEDONE); + } + } else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) { + // Trigger the CompleteDone event to give scripts a chance to act + // upon the (possibly failed) completion. + ins_apply_autocmds(EVENT_COMPLETEDONE); + } + + may_trigger_modechanged(); + + // reset continue_* if we left expansion-mode, if we stay they'll be + // (re)set properly in ins_complete() + if (!vim_is_ctrl_x_key(c)) { + compl_cont_status = 0; + compl_cont_mode = 0; + } + + return retval; +} + +/// Fix the redo buffer for the completion leader replacing some of the typed +/// text. This inserts backspaces and appends the changed text. +/// "ptr" is the known leader text or NUL. +static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg) +{ + int len; + char_u *p; + char_u *ptr = ptr_arg; + + if (ptr == NULL) { + if (compl_leader != NULL) { + ptr = compl_leader; + } else { + return; // nothing to do + } + } + if (compl_orig_text != NULL) { + p = compl_orig_text; + for (len = 0; p[len] != NUL && p[len] == ptr[len]; len++) {} + if (len > 0) { + len -= utf_head_off(p, p + len); + } + for (p += len; *p != NUL; MB_PTR_ADV(p)) { + AppendCharToRedobuff(K_BS); + } + } else { + len = 0; + } + AppendToRedobuffLit((char *)ptr + len, -1); +} + +/// Loops through the list of windows, loaded-buffers or non-loaded-buffers +/// (depending on flag) starting from buf and looking for a non-scanned +/// buffer (other than curbuf). curbuf is special, if it is called with +/// buf=curbuf then it has to be the first call for a given flag/expansion. +/// +/// Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo +static buf_T *ins_compl_next_buf(buf_T *buf, int flag) +{ + static win_T *wp = NULL; + + if (flag == 'w') { // just windows + if (buf == curbuf || wp == NULL) { // first call for this flag/expansion + wp = curwin; + } + assert(wp); + while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin + && wp->w_buffer->b_scanned) {} + buf = wp->w_buffer; + } else { + // 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' + // (unlisted buffers) + // When completing whole lines skip unloaded buffers. + while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf + && ((flag == 'U' + ? buf->b_p_bl + : (!buf->b_p_bl + || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) + || buf->b_scanned)) {} + } + return buf; +} + +/// Get the user-defined completion function name for completion 'type' +static char_u *get_complete_funcname(int type) +{ + switch (type) { + case CTRL_X_FUNCTION: + return curbuf->b_p_cfu; + case CTRL_X_OMNI: + return curbuf->b_p_ofu; + case CTRL_X_THESAURUS: + return *curbuf->b_p_tsrfu == NUL ? p_tsrfu : curbuf->b_p_tsrfu; + default: + return (char_u *)""; + } +} + +/// Execute user defined complete function 'completefunc' or 'omnifunc', and +/// get matches in "matches". +/// +/// @param type CTRL_X_OMNI or CTRL_X_FUNCTION +static void expand_by_function(int type, char_u *base) +{ + list_T *matchlist = NULL; + dict_T *matchdict = NULL; + char_u *funcname; + pos_T pos; + typval_T rettv; + const int save_State = State; + + assert(curbuf != NULL); + funcname = get_complete_funcname(type); + if (*funcname == NUL) { + return; + } + + // Call 'completefunc' to obtain the list of matches. + typval_T args[3]; + args[0].v_type = VAR_NUMBER; + args[1].v_type = VAR_STRING; + args[2].v_type = VAR_UNKNOWN; + args[0].vval.v_number = 0; + args[1].vval.v_string = base != NULL ? (char *)base : ""; + + pos = curwin->w_cursor; + // Lock the text to avoid weird things from happening. Also disallow + // switching to another window, it should not be needed and may end up in + // Insert mode in another buffer. + textlock++; + + // Call a function, which returns a list or dict. + if (call_vim_function((char *)funcname, 2, args, &rettv) == OK) { + switch (rettv.v_type) { + case VAR_LIST: + matchlist = rettv.vval.v_list; + break; + case VAR_DICT: + matchdict = rettv.vval.v_dict; + break; + case VAR_SPECIAL: + FALLTHROUGH; + default: + // TODO(brammool): Give error message? + tv_clear(&rettv); + break; + } + } + textlock--; + + curwin->w_cursor = pos; // restore the cursor position + validate_cursor(); + if (!equalpos(curwin->w_cursor, pos)) { + emsg(_(e_compldel)); + goto theend; + } + + if (matchlist != NULL) { + ins_compl_add_list(matchlist); + } else if (matchdict != NULL) { + ins_compl_add_dict(matchdict); + } + +theend: + // Restore State, it might have been changed. + State = save_State; + + if (matchdict != NULL) { + tv_dict_unref(matchdict); + } + if (matchlist != NULL) { + tv_list_unref(matchlist); + } +} + +/// Add a match to the list of matches from VimL object +/// +/// @param[in] tv Object to get matches from. +/// @param[in] dir Completion direction. +/// @param[in] fast use fast_breakcheck() instead of os_breakcheck(). +/// +/// @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. +int ins_compl_add_tv(typval_T *const tv, const Direction dir, bool fast) + FUNC_ATTR_NONNULL_ALL +{ + const char *word; + bool dup = false; + bool empty = false; + int flags = fast ? CP_FAST : 0; + char *(cptext[CPT_COUNT]); + typval_T user_data; + + user_data.v_type = VAR_UNKNOWN; + if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) { + word = tv_dict_get_string(tv->vval.v_dict, "word", false); + cptext[CPT_ABBR] = tv_dict_get_string(tv->vval.v_dict, "abbr", true); + cptext[CPT_MENU] = tv_dict_get_string(tv->vval.v_dict, "menu", true); + cptext[CPT_KIND] = tv_dict_get_string(tv->vval.v_dict, "kind", true); + cptext[CPT_INFO] = tv_dict_get_string(tv->vval.v_dict, "info", true); + tv_dict_get_tv(tv->vval.v_dict, "user_data", &user_data); + + 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 = tv_get_string_chk(tv); + memset(cptext, 0, sizeof(cptext)); + } + 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, NULL, + (char_u **)cptext, true, &user_data, dir, flags, dup); +} + +/// Add completions from a list. +static void ins_compl_add_list(list_T *const list) +{ + Direction dir = compl_direction; + + // Go through the List with matches and add each of them. + TV_LIST_ITER(list, li, { + if (ins_compl_add_tv(TV_LIST_ITEM_TV(li), dir, true) == OK) { + // If dir was BACKWARD then honor it just once. + dir = FORWARD; + } else if (did_emsg) { + break; + } + }); +} + +/// Add completions from a dict. +static void ins_compl_add_dict(dict_T *dict) +{ + dictitem_T *di_refresh; + dictitem_T *di_words; + + // Check for optional "refresh" item. + compl_opt_refresh_always = false; + di_refresh = tv_dict_find(dict, S_LEN("refresh")); + if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) { + const char *v = (const char *)di_refresh->di_tv.vval.v_string; + + if (v != NULL && strcmp(v, "always") == 0) { + compl_opt_refresh_always = true; + } + } + + // Add completions from a "words" list. + di_words = tv_dict_find(dict, S_LEN("words")); + if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) { + ins_compl_add_list(di_words->di_tv.vval.v_list); + } +} + +/// Start completion for the complete() function. +/// +/// @param startcol where the matched text starts (1 is first column). +/// @param list the list of matches. +void set_completion(colnr_T startcol, list_T *list) +{ + int flags = CP_ORIGINAL_TEXT; + + // If already doing completions stop it. + if (ctrl_x_mode_not_default()) { + ins_compl_prep(' '); + } + ins_compl_clear(); + ins_compl_free(); + + compl_direction = FORWARD; + if (startcol > curwin->w_cursor.col) { + startcol = curwin->w_cursor.col; + } + compl_col = startcol; + compl_length = (int)curwin->w_cursor.col - (int)startcol; + // compl_pattern doesn't need to be set + compl_orig_text = vim_strnsave(get_cursor_line_ptr() + compl_col, + (size_t)compl_length); + if (p_ic) { + flags |= CP_ICASE; + } + if (ins_compl_add(compl_orig_text, -1, NULL, NULL, false, NULL, 0, + flags | CP_FAST, false) != OK) { + return; + } + + ctrl_x_mode = CTRL_X_EVAL; + + ins_compl_add_list(list); + compl_matches = ins_compl_make_cyclic(); + 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; + + compl_curr_match = compl_first_match; + if (compl_no_insert || compl_no_select) { + ins_complete(K_DOWN, false); + if (compl_no_select) { + ins_complete(K_UP, false); + } + } else { + ins_complete(Ctrl_N, false); + } + compl_enter_selects = compl_no_insert; + + // Lazily show the popup menu, unless we got interrupted. + if (!compl_interrupted) { + show_pum(save_w_wrow, save_w_leftcol); + } + + may_trigger_modechanged(); + ui_flush(); +} + +/// Return Insert completion mode name string +static char_u *ins_compl_mode(void) +{ + if (ctrl_x_mode_not_defined_yet() || ctrl_x_mode_scroll() || compl_started) { + return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT]; + } + return (char_u *)""; +} + +static void ins_compl_update_sequence_numbers(void) +{ + int number = 0; + compl_T *match; + + if (compl_direction == FORWARD) { + // search backwards for the first valid (!= -1) number. + // This should normally succeed already at the first loop + // cycle, so it's fast! + for (match = compl_curr_match->cp_prev; + match != NULL && match != compl_first_match; + match = match->cp_prev) { + if (match->cp_number != -1) { + number = match->cp_number; + break; + } + } + if (match != NULL) { + // go up and assign all numbers which are not assigned yet + for (match = match->cp_next; + match != NULL && match->cp_number == -1; + match = match->cp_next) { + match->cp_number = ++number; + } + } + } else { // BACKWARD + assert(compl_direction == BACKWARD); + // search forwards (upwards) for the first valid (!= -1) + // number. This should normally succeed already at the + // first loop cycle, so it's fast! + for (match = compl_curr_match->cp_next; + match != NULL && match != compl_first_match; + match = match->cp_next) { + if (match->cp_number != -1) { + number = match->cp_number; + break; + } + } + if (match != NULL) { + // go down and assign all numbers which are not + // assigned yet + for (match = match->cp_prev; + match && match->cp_number == -1; + match = match->cp_prev) { + match->cp_number = ++number; + } + } + } +} + +// Get complete information +void get_complete_info(list_T *what_list, dict_T *retdict) +{ +#define CI_WHAT_MODE 0x01 +#define CI_WHAT_PUM_VISIBLE 0x02 +#define CI_WHAT_ITEMS 0x04 +#define CI_WHAT_SELECTED 0x08 +#define CI_WHAT_INSERTED 0x10 +#define CI_WHAT_ALL 0xff + int what_flag; + + if (what_list == NULL) { + what_flag = CI_WHAT_ALL; + } else { + what_flag = 0; + for (listitem_T *item = tv_list_first(what_list) + ; item != NULL + ; item = TV_LIST_ITEM_NEXT(what_list, item)) { + const char *what = tv_get_string(TV_LIST_ITEM_TV(item)); + + if (STRCMP(what, "mode") == 0) { + what_flag |= CI_WHAT_MODE; + } else if (STRCMP(what, "pum_visible") == 0) { + what_flag |= CI_WHAT_PUM_VISIBLE; + } else if (STRCMP(what, "items") == 0) { + what_flag |= CI_WHAT_ITEMS; + } else if (STRCMP(what, "selected") == 0) { + what_flag |= CI_WHAT_SELECTED; + } else if (STRCMP(what, "inserted") == 0) { + what_flag |= CI_WHAT_INSERTED; + } + } + } + + int ret = OK; + if (what_flag & CI_WHAT_MODE) { + ret = tv_dict_add_str(retdict, S_LEN("mode"), + (char *)ins_compl_mode()); + } + + if (ret == OK && (what_flag & CI_WHAT_PUM_VISIBLE)) { + ret = tv_dict_add_nr(retdict, S_LEN("pum_visible"), pum_visible()); + } + + if (ret == OK && (what_flag & CI_WHAT_ITEMS)) { + list_T *li = tv_list_alloc(get_compl_len()); + + ret = tv_dict_add_list(retdict, S_LEN("items"), li); + if (ret == OK && compl_first_match != NULL) { + compl_T *match = compl_first_match; + do { + if (!(match->cp_flags & CP_ORIGINAL_TEXT)) { + dict_T *di = tv_dict_alloc(); + + tv_list_append_dict(li, di); + tv_dict_add_str(di, S_LEN("word"), EMPTY_IF_NULL(match->cp_str)); + tv_dict_add_str(di, S_LEN("abbr"), EMPTY_IF_NULL(match->cp_text[CPT_ABBR])); + tv_dict_add_str(di, S_LEN("menu"), EMPTY_IF_NULL(match->cp_text[CPT_MENU])); + tv_dict_add_str(di, S_LEN("kind"), EMPTY_IF_NULL(match->cp_text[CPT_KIND])); + tv_dict_add_str(di, S_LEN("info"), EMPTY_IF_NULL(match->cp_text[CPT_INFO])); + if (match->cp_user_data.v_type == VAR_UNKNOWN) { + tv_dict_add_str(di, S_LEN("user_data"), ""); + } else { + tv_dict_add_tv(di, S_LEN("user_data"), &match->cp_user_data); + } + } + match = match->cp_next; + } while (match != NULL && match != compl_first_match); + } + } + + if (ret == OK && (what_flag & CI_WHAT_SELECTED)) { + if (compl_curr_match != NULL && compl_curr_match->cp_number == -1) { + ins_compl_update_sequence_numbers(); + } + ret = tv_dict_add_nr(retdict, S_LEN("selected"), + (compl_curr_match != NULL) + ? compl_curr_match->cp_number - 1 : -1); + } + + (void)ret; + // TODO(vim): + // if (ret == OK && (what_flag & CI_WHAT_INSERTED)) +} + +/// Returns true when using a user-defined function for thesaurus completion. +static bool thesaurus_func_complete(int type) +{ + return type == CTRL_X_THESAURUS + && (*curbuf->b_p_tsrfu != NUL || *p_tsrfu != NUL); +} + +/// 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 bool 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; + int save_p_scs; + bool save_p_ws; + int save_p_ic; + int i; + int num_matches; + int len; + int found_new_match; + int type = ctrl_x_mode; + char_u *ptr; + char_u *dict = NULL; + int dict_f = 0; + bool set_match_pos; + pos_T prev_pos = { 0, 0, 0 }; + + assert(curbuf != NULL); + + if (!compl_started) { + FOR_ALL_BUFFERS(buf) { + buf->b_scanned = false; + } + found_all = false; + ins_buf = curbuf; + e_cpt = (compl_cont_status & CONT_LOCAL) + ? (char_u *)"." : curbuf->b_p_cpt; + last_match_pos = first_match_pos = *ini; + } else if (ins_buf != curbuf && !buf_valid(ins_buf)) { + ins_buf = curbuf; // In case the buffer was wiped out. + } + + compl_old_match = compl_curr_match; // remember the last current match + pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; + + // For ^N/^P loop over all the flags/windows/buffers in 'complete' + for (;;) { + found_new_match = FAIL; + set_match_pos = false; + + // 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 ((ctrl_x_mode_normal() || ctrl_x_mode_line_or_eval()) + && (!compl_started || found_all)) { + found_all = false; + while (*e_cpt == ',' || *e_cpt == ' ') { + e_cpt++; + } + if (*e_cpt == '.' && !curbuf->b_scanned) { + ins_buf = curbuf; + first_match_pos = *ini; + // Move the cursor back one character so that ^N can match the + // word immediately after the cursor. + if (ctrl_x_mode_normal() && dec(&first_match_pos) < 0) { + // Move the cursor to after the last character in the + // buffer, so that word at start of buffer is found + // correctly. + first_match_pos.lnum = ins_buf->b_ml.ml_line_count; + first_match_pos.col = (colnr_T)STRLEN(ml_get(first_match_pos.lnum)); + } + 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; + } else if (vim_strchr("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; + 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) { + continue; + } + type = CTRL_X_DICTIONARY; + dict = (char_u *)ins_buf->b_fname; + dict_f = DICT_EXACT; + } + msg_hist_off = true; // reset in msg_trunc_attr() + vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), + ins_buf->b_fname == NULL + ? buf_spname(ins_buf) + : ins_buf->b_sfname == NULL + ? ins_buf->b_fname + : ins_buf->b_sfname); + (void)msg_trunc_attr((char *)IObuff, true, HL_ATTR(HLF_R)); + } else if (*e_cpt == NUL) { + break; + } else { + if (ctrl_x_mode_line_or_eval()) { + type = -1; + } else if (*e_cpt == 'k' || *e_cpt == 's') { + if (*e_cpt == 'k') { + type = CTRL_X_DICTIONARY; + } else { + type = CTRL_X_THESAURUS; + } + if (*++e_cpt != ',' && *e_cpt != NUL) { + dict = e_cpt; + dict_f = DICT_FIRST; + } + } else if (*e_cpt == 'i') { + type = CTRL_X_PATH_PATTERNS; + } else if (*e_cpt == 'd') { + type = CTRL_X_PATH_DEFINES; + } else if (*e_cpt == ']' || *e_cpt == 't') { + msg_hist_off = true; // reset in msg_trunc_attr() + type = CTRL_X_TAGS; + vim_snprintf((char *)IObuff, IOSIZE, "%s", _("Scanning tags.")); + (void)msg_trunc_attr((char *)IObuff, true, HL_ATTR(HLF_R)); + } else { + type = -1; + } + + // in any case e_cpt is advanced to the next entry + (void)copy_option_part((char **)&e_cpt, (char *)IObuff, IOSIZE, ","); + + found_all = true; + if (type == -1) { + continue; + } + } + } + + // If complete() was called then compl_pattern has been reset. + // The following won't work then, bail out. + if (compl_pattern == NULL) { + break; + } + + switch (type) { + case -1: + break; + case CTRL_X_PATH_PATTERNS: + case CTRL_X_PATH_DEFINES: + find_pattern_in_path(compl_pattern, compl_direction, + STRLEN(compl_pattern), false, false, + ((type == CTRL_X_PATH_DEFINES + && !(compl_cont_status & CONT_SOL)) + ? FIND_DEFINE + : FIND_ANY), + 1L, ACTION_EXPAND, 1, MAXLNUM); + break; + + case CTRL_X_DICTIONARY: + case CTRL_X_THESAURUS: + if (thesaurus_func_complete(type)) { + expand_by_function(type, compl_pattern); + } else { + ins_compl_dictionaries(dict != NULL ? dict + : (type == CTRL_X_THESAURUS + ? (*curbuf->b_p_tsr == NUL ? p_tsr : curbuf->b_p_tsr) + : (*curbuf->b_p_dict == + NUL ? p_dict : curbuf->b_p_dict)), + compl_pattern, + dict != NULL ? dict_f : 0, type == CTRL_X_THESAURUS); + } + dict = NULL; + break; + + case CTRL_X_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 + g_tag_at_cursor = true; + if (find_tags(compl_pattern, &num_matches, &matches, + TAG_REGEXP | TAG_NAMES | TAG_NOIC | TAG_INS_COMP + | (ctrl_x_mode_not_default() ? TAG_VERBOSE : 0), + TAG_MANY, (char_u *)curbuf->b_ffname) == OK && num_matches > 0) { + ins_compl_add_matches(num_matches, matches, p_ic); + } + g_tag_at_cursor = false; + p_ic = save_p_ic; + break; + + 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 "~". + tilde_replace(compl_pattern, num_matches, matches); +#ifdef BACKSLASH_IN_FILENAME + if (curbuf->b_p_csl[0] != NUL) { + for (int i = 0; i < num_matches; i++) { + char_u *ptr = matches[i]; + while (*ptr != NUL) { + if (curbuf->b_p_csl[0] == 's' && *ptr == '\\') { + *ptr = '/'; + } else if (curbuf->b_p_csl[0] == 'b' && *ptr == '/') { + *ptr = '\\'; + } + ptr += utfc_ptr2len(ptr); + } + } + } +#endif + ins_compl_add_matches(num_matches, matches, p_fic || p_wic); + } + break; + + case CTRL_X_CMDLINE: + case CTRL_X_CMDLINE_CTRL_X: + if (expand_cmdline(&compl_xp, compl_pattern, + (int)STRLEN(compl_pattern), + &num_matches, &matches) == EXPAND_OK) { + ins_compl_add_matches(num_matches, matches, false); + } + break; + + case CTRL_X_FUNCTION: + case CTRL_X_OMNI: + expand_by_function(type, compl_pattern); + break; + + case CTRL_X_SPELL: + num_matches = expand_spelling(first_match_pos.lnum, + compl_pattern, &matches); + if (num_matches > 0) { + 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 + 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 + save_p_ws = p_ws; + if (ins_buf != curbuf) { + p_ws = false; + } else if (*e_cpt == '.') { + p_ws = true; + } + bool looped_around = false; + for (;;) { + bool cont_s_ipos = false; + + msg_silent++; // Don't want messages for wrapscan. + // ctrl_x_mode_line_or_eval() || word-wise search that + // has added a word that was at the beginning of the line. + if (ctrl_x_mode_line_or_eval() + || (compl_cont_status & CONT_SOL)) { + found_new_match = search_for_exact_line(ins_buf, pos, + compl_direction, + compl_pattern); + } else { + found_new_match = searchit(NULL, ins_buf, pos, NULL, + compl_direction, + compl_pattern, 1L, + SEARCH_KEEP + SEARCH_NFMSG, + RE_LAST, NULL); + } + msg_silent--; + if (!compl_started || set_match_pos) { + // set "compl_started" even on fail + compl_started = true; + first_match_pos = *pos; + last_match_pos = *pos; + set_match_pos = false; + } else if (first_match_pos.lnum == last_match_pos.lnum + && first_match_pos.col == last_match_pos.col) { + found_new_match = FAIL; + } else if ((compl_direction == FORWARD) + && (prev_pos.lnum > pos->lnum + || (prev_pos.lnum == pos->lnum + && prev_pos.col >= pos->col))) { + if (looped_around) { + found_new_match = FAIL; + } else { + looped_around = true; + } + } else if ((compl_direction != FORWARD) + && (prev_pos.lnum < pos->lnum + || (prev_pos.lnum == pos->lnum + && prev_pos.col <= pos->col))) { + if (looped_around) { + found_new_match = FAIL; + } else { + looped_around = true; + } + } + prev_pos = *pos; + 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) { + continue; + } + ptr = ml_get_buf(ins_buf, pos->lnum, false) + pos->col; + if (ctrl_x_mode_line_or_eval()) { + if (compl_cont_status & CONT_ADDING) { + 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 = (char_u *)skipwhite((char *)ptr); + } + } + len = (int)STRLEN(ptr); + } else { + 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)) { + continue; + } + // Find start of next word. + tmp_ptr = find_word_start(tmp_ptr); + } + // Find end of this word. + tmp_ptr = find_word_end(tmp_ptr); + len = (int)(tmp_ptr - ptr); + + if ((compl_cont_status & CONT_ADDING) + && len == compl_length) { + if (pos->lnum < ins_buf->b_ml.ml_line_count) { + // Try next line, if any. the new word will be "join" as if the + // normal command "J" was used. IOSIZE is always greater than + // compl_length, so the next STRNCPY always works -- Acevedo + STRNCPY(IObuff, ptr, len); // NOLINT(runtime/printf) + ptr = ml_get_buf(ins_buf, pos->lnum + 1, false); + tmp_ptr = ptr = (char_u *)skipwhite((char *)ptr); + // Find start of next word. + tmp_ptr = find_word_start(tmp_ptr); + // 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] != ' ') { + IObuff[len++] = ' '; + } + // IObuf =~ "\k.* ", thus len >= 2 + if (p_js + && (IObuff[len - 2] == '.' + || IObuff[len - 2] == '?' + || IObuff[len - 2] == '!')) { + IObuff[len++] = ' '; + } + } + // copy as much as possible of the new word + if (tmp_ptr - ptr >= IOSIZE - len) { + tmp_ptr = ptr + IOSIZE - len - 1; + } + STRLCPY(IObuff + len, ptr, IOSIZE - len); + len += (int)(tmp_ptr - ptr); + cont_s_ipos = true; + } + IObuff[len] = NUL; + ptr = IObuff; + } + if (len == compl_length) { + continue; + } + } + } + if (ins_compl_add_infercase(ptr, len, p_ic, + ins_buf == curbuf ? NULL : (char_u *)ins_buf->b_sfname, + 0, cont_s_ipos) != NOTDONE) { + found_new_match = OK; + break; + } + } + p_scs = save_p_scs; + p_ws = save_p_ws; + } + + // check if compl_curr_match has changed, (e.g. other type of + // expansion added something) + if (type != 0 && compl_curr_match != compl_old_match) { + found_new_match = OK; + } + + // break the loop for specialized modes (use 'complete' just for the + // generic ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new match + if ((ctrl_x_mode_not_default() + && !ctrl_x_mode_line_or_eval()) + || found_new_match != FAIL) { + if (got_int) { + break; + } + // Fill the popup menu as soon as possible. + if (type != -1) { + ins_compl_check_keys(0, false); + } + + if ((ctrl_x_mode_not_default() + && !ctrl_x_mode_line_or_eval()) + || compl_interrupted) { + break; + } + compl_started = true; + } else { + // 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 = true; + + if ((ctrl_x_mode_normal() + || ctrl_x_mode_line_or_eval()) + && *e_cpt == NUL) { // Got to end of 'complete' + found_new_match = FAIL; + } + + i = -1; // total of matches, unknown + if (found_new_match == FAIL + || (ctrl_x_mode_not_default() + && !ctrl_x_mode_line_or_eval())) { + i = ins_compl_make_cyclic(); + } + + if (compl_old_match != NULL) { + // If several matches were added (FORWARD) or the search failed and has + // just been made cyclic then we have to move compl_curr_match to the + // next or previous entry (if any) -- Acevedo + compl_curr_match = compl_direction == FORWARD + ? compl_old_match->cp_next + : compl_old_match->cp_prev; + if (compl_curr_match == NULL) { + compl_curr_match = compl_old_match; + } + } + may_trigger_modechanged(); + + return i; +} + +/// Delete the old text being completed. +void ins_compl_delete(void) +{ + int col; + + // In insert mode: Delete the typed part. + // In replace mode: Put the old characters back, if any. + col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); + if ((int)curwin->w_cursor.col > col) { + if (stop_arrow() == FAIL) { + return; + } + backspace_until_column(col); + } + + // TODO(vim): is this sufficient for redrawing? Redrawing everything + // causes flicker, thus we can't do that. + changed_cline_bef_curs(); + // clear v:completed_item + set_vim_var_dict(VV_COMPLETED_ITEM, tv_dict_alloc_lock(VAR_FIXED)); +} + +/// Insert the new text being completed. +/// "in_compl_func" is true when called from complete_check(). +void ins_compl_insert(bool in_compl_func) +{ + ins_bytes(compl_shown_match->cp_str + get_compl_len()); + 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); + if (!in_compl_func) { + compl_curr_match = compl_shown_match; + } +} + +/// Fill in the next completion in the current direction. +/// If "allow_get_expansion" is true, then we may call ins_compl_get_exp() to +/// get more completions. If it is false, then we just do nothing when there +/// are no more completions in a given direction. The latter case is used when +/// we are still in the middle of finding completions, to allow browsing +/// through the ones found so far. +/// @return the total number of matches, or -1 if still unknown -- webb. +/// +/// compl_curr_match is currently being used by ins_compl_get_exp(), so we use +/// compl_shown_match here. +/// +/// Note that this function may be called recursively once only. First with +/// "allow_get_expansion" true, which calls ins_compl_get_exp(), which in turn +/// calls this function with "allow_get_expansion" false. +/// +/// @param count Repeat completion this many times; should be at least 1 +/// @param insert_match Insert the newly selected match +/// @param in_compl_func Called from complete_check() +static int ins_compl_next(bool allow_get_expansion, int count, bool insert_match, + bool in_compl_func) +{ + int num_matches = -1; + int todo = count; + compl_T *found_compl = NULL; + bool found_end = false; + const bool started = compl_started; + + // When user complete function return -1 for findstart which is next + // time of 'always', compl_shown_match become NULL. + if (compl_shown_match == NULL) { + return -1; + } + + if (compl_leader != NULL + && (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, STRLEN(compl_leader)) + && compl_shown_match->cp_next != NULL + && 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. + if (compl_shows_dir == BACKWARD + && !ins_compl_equal(compl_shown_match, compl_leader, STRLEN(compl_leader)) + && (compl_shown_match->cp_next == NULL + || compl_shown_match->cp_next == compl_first_match)) { + while (!ins_compl_equal(compl_shown_match, compl_leader, STRLEN(compl_leader)) + && compl_shown_match->cp_prev != NULL + && compl_shown_match->cp_prev != compl_first_match) { + compl_shown_match = compl_shown_match->cp_prev; + } + } + } + + if (allow_get_expansion && insert_match + && (!(compl_get_longest || compl_restarting) || compl_used_match)) { + // Delete old text to be replaced + ins_compl_delete(); + } + + // When finding the longest common text we stick at the original text, + // don't let CTRL-N or CTRL-P move to the first match. + bool advance = count != 1 || !allow_get_expansion || !compl_get_longest; + + // When restarting the search don't insert the first match either. + if (compl_restarting) { + advance = false; + compl_restarting = false; + } + + // Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap + // around. + while (--todo >= 0) { + if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) { + compl_shown_match = compl_shown_match->cp_next; + found_end = (compl_first_match != NULL + && (compl_shown_match->cp_next == compl_first_match + || compl_shown_match == compl_first_match)); + } else if (compl_shows_dir == BACKWARD + && compl_shown_match->cp_prev != NULL) { + found_end = (compl_shown_match == compl_first_match); + compl_shown_match = compl_shown_match->cp_prev; + found_end |= (compl_shown_match == compl_first_match); + } else { + if (!allow_get_expansion) { + if (advance) { + if (compl_shows_dir == BACKWARD) { + compl_pending -= todo + 1; + } else { + compl_pending += todo + 1; + } + } + return -1; + } + + if (!compl_no_select && advance) { + if (compl_shows_dir == BACKWARD) { + compl_pending--; + } else { + compl_pending++; + } + } + + // Find matches. + num_matches = ins_compl_get_exp(&compl_startpos); + + // handle any pending completions + while (compl_pending != 0 && compl_direction == compl_shows_dir + && advance) { + if (compl_pending > 0 && compl_shown_match->cp_next != NULL) { + compl_shown_match = compl_shown_match->cp_next; + compl_pending--; + } + if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) { + compl_shown_match = compl_shown_match->cp_prev; + compl_pending++; + } else { + break; + } + } + found_end = false; + } + if ((compl_shown_match->cp_flags & CP_ORIGINAL_TEXT) == 0 + && compl_leader != NULL + && !ins_compl_equal(compl_shown_match, + 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) { + if (found_compl != NULL) { + compl_shown_match = found_compl; + break; + } + todo = 1; // use first usable match after wrapping around + } + } + + // Insert the text of the new completion, or the compl_leader. + if (compl_no_insert && !started) { + ins_bytes(compl_orig_text + get_compl_len()); + compl_used_match = false; + } else if (insert_match) { + if (!compl_get_longest || compl_used_match) { + ins_compl_insert(in_compl_func); + } else { + ins_bytes(compl_leader + get_compl_len()); + } + } else { + compl_used_match = false; + } + + if (!allow_get_expansion) { + // redraw to show the user what was inserted + update_screen(0); + + // display the updated popup menu + ins_compl_show_pum(); + + // Delete old text to be replaced, since we're still searching and + // don't want to match ourselves! + ins_compl_delete(); + } + + // Enter will select a match when the match wasn't inserted and the popup + // menu is visible. + if (compl_no_insert && !started) { + compl_enter_selects = true; + } else { + compl_enter_selects = !insert_match && compl_match_array != NULL; + } + + // Show the file name for the match (if any) + // Truncate the file name to avoid a wait for return. + if (compl_shown_match->cp_fname != NULL) { + char *lead = _("match in file"); + int space = sc_col - vim_strsize(lead) - 2; + char *s; + char *e; + + if (space > 0) { + // We need the tail that fits. With double-byte encoding going + // back from the end is very slow, thus go from the start and keep + // the text that fits in "space" between "s" and "e". + for (s = e = (char *)compl_shown_match->cp_fname; *e != NUL; MB_PTR_ADV(e)) { + space -= ptr2cells(e); + while (space < 0) { + space += ptr2cells(s); + MB_PTR_ADV(s); + } + } + msg_hist_off = true; + vim_snprintf((char *)IObuff, IOSIZE, "%s %s%s", lead, + (char_u *)s > compl_shown_match->cp_fname ? "<" : "", s); + msg((char *)IObuff); + msg_hist_off = false; + redraw_cmdline = false; // don't overwrite! + } + } + + return num_matches; +} + +void pum_ext_select_item(int item, bool insert, bool finish) +{ + if (!pum_visible() || item < -1 || item >= compl_match_arraysize) { + return; + } + pum_want.active = true; + pum_want.item = item; + pum_want.insert = insert; + pum_want.finish = finish; +} + +/// Call this while finding completions, to check whether the user has hit a key +/// that should change the currently displayed completion, or exit completion +/// mode. Also, when compl_pending is not zero, show a completion as soon as +/// possible. -- webb +/// +/// @param frequency specifies out of how many calls we actually check. +/// @param in_compl_func true when called from complete_check(), don't set +/// compl_curr_match. +void ins_compl_check_keys(int frequency, bool in_compl_func) +{ + static int count = 0; + + // Don't check when reading keys from a script, :normal or feedkeys(). + // That would break the test scripts. But do check for keys when called + // from complete_check(). + if (!in_compl_func && (using_script() || ex_normal_busy)) { + return; + } + + // Only do this at regular intervals + if (++count < frequency) { + return; + } + count = 0; + + // Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() + // can't do its work correctly. + int c = vpeekc_any(); + if (c != NUL) { + if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) { + c = safe_vgetc(); // Eat the character + compl_shows_dir = ins_compl_key2dir(c); + (void)ins_compl_next(false, ins_compl_key2count(c), + c != K_UP && c != K_DOWN, in_compl_func); + } else { + // Need to get the character to have KeyTyped set. We'll put it + // back with vungetc() below. But skip K_IGNORE. + c = safe_vgetc(); + if (c != K_IGNORE) { + // Don't interrupt completion when the character wasn't typed, + // e.g., when doing @q to replay keys. + if (c != Ctrl_R && KeyTyped) { + compl_interrupted = true; + } + + vungetc(c); + } + } + } + if (compl_pending != 0 && !got_int && !compl_no_insert) { + int todo = compl_pending > 0 ? compl_pending : -compl_pending; + + compl_pending = 0; + (void)ins_compl_next(false, todo, true, in_compl_func); + } +} + +/// Decide the direction of Insert mode complete from the key typed. +/// Returns BACKWARD or FORWARD. +static int ins_compl_key2dir(int c) +{ + if (c == K_EVENT || c == K_COMMAND || c == K_LUA) { + return pum_want.item < pum_selected_item ? BACKWARD : FORWARD; + } + if (c == Ctrl_P || c == Ctrl_L + || c == K_PAGEUP || c == K_KPAGEUP + || c == K_S_UP || c == K_UP) { + return BACKWARD; + } + return FORWARD; +} + +/// Check that "c" is a valid completion key only while the popup menu is shown +/// +/// @param c character to check +static bool ins_compl_pum_key(int c) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP + || c == K_PAGEDOWN || c == K_KPAGEDOWN + || c == K_S_DOWN || c == K_UP || c == K_DOWN); +} + +/// Decide the number of completions to move forward. +/// Returns 1 for most keys, height of the popup menu for page-up/down keys. +static int ins_compl_key2count(int c) +{ + int h; + + if (c == K_EVENT || c == K_COMMAND || c == K_LUA) { + int offset = pum_want.item - pum_selected_item; + return abs(offset); + } + + if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) { + h = pum_get_height(); + if (h > 3) { + h -= 2; // keep some context + } + return h; + } + return 1; +} + +/// Check that completion with "c" should insert the match, false if only +/// to change the currently selected completion. +/// +/// @param c character to check +static bool ins_compl_use_match(int c) + FUNC_ATTR_CONST FUNC_ATTR_WARN_UNUSED_RESULT +{ + switch (c) { + case K_UP: + case K_DOWN: + case K_PAGEDOWN: + case K_KPAGEDOWN: + case K_S_DOWN: + case K_PAGEUP: + case K_KPAGEUP: + case K_S_UP: + return false; + case K_EVENT: + case K_COMMAND: + case K_LUA: + return pum_want.active && pum_want.insert; + } + return true; +} + +/// Do Insert mode completion. +/// Called when character "c" was typed, which has a meaning for completion. +/// Returns OK if completion was done, FAIL if something failed. +int ins_complete(int c, bool enable_pum) +{ + char_u *line; + int startcol = 0; // column where searched text starts + colnr_T curs_col; // cursor column + int n; + int save_w_wrow; + 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); + + if (!compl_started) { + // First time we hit ^N or ^P (in a row, I mean) + + did_ai = false; + did_si = false; + can_si = false; + can_si_back = false; + if (stop_arrow() == FAIL) { + return FAIL; + } + + line = ml_get(curwin->w_cursor.lnum); + curs_col = curwin->w_cursor.col; + compl_pending = 0; + + // If this same ctrl_x_mode has been interrupted use the text from + // "compl_startpos" to the cursor as a pattern to add a new word + // instead of expand the one before the cursor, in word-wise if + // "compl_startpos" is not in the same line as the cursor then fix it + // (the line has been split because it was longer than 'tw'). if SOL + // is set then skip the previous pattern, a word at the beginning of + // the line has been inserted, we'll look for that -- Acevedo. + if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT + && compl_cont_mode == ctrl_x_mode) { + // it is a continued search + compl_cont_status &= ~CONT_INTRPT; // remove INTRPT + if (ctrl_x_mode_normal() + || ctrl_x_mode_path_patterns() + || ctrl_x_mode_path_defines()) { + if (compl_startpos.lnum != curwin->w_cursor.lnum) { + // line (probably) wrapped, set compl_startpos to the + // first non_blank in the line, if it is not a wordchar + // include it to get a better pattern, but then we don't + // want the "\\<" prefix, check it below. + compl_col = (colnr_T)getwhitecols(line); + compl_startpos.col = compl_col; + compl_startpos.lnum = curwin->w_cursor.lnum; + compl_cont_status &= ~CONT_SOL; // clear SOL if present + } else { + // S_IPOS was set when we inserted a word that was at the + // beginning of the line, which means that we'll go to SOL + // mode but first we need to redefine compl_startpos + if (compl_cont_status & CONT_S_IPOS) { + compl_cont_status |= CONT_SOL; + compl_startpos.col = (colnr_T)((char_u *)skipwhite((char *)line + + compl_length + + compl_startpos.col) - line); + } + compl_col = compl_startpos.col; + } + compl_length = curwin->w_cursor.col - (int)compl_col; + // IObuff is used to add a "word from the next line" would we + // have enough space? just being paranoid +#define MIN_SPACE 75 + if (compl_length > (IOSIZE - MIN_SPACE)) { + compl_cont_status &= ~CONT_SOL; + compl_length = (IOSIZE - MIN_SPACE); + compl_col = curwin->w_cursor.col - compl_length; + } + compl_cont_status |= CONT_ADDING | CONT_N_ADDS; + if (compl_length < 1) { + compl_cont_status &= CONT_LOCAL; + } + } else if (ctrl_x_mode_line_or_eval()) { + compl_cont_status = CONT_ADDING | CONT_N_ADDS; + } else { + compl_cont_status = 0; + } + } else { + compl_cont_status &= CONT_LOCAL; + } + + if (!(compl_cont_status & CONT_ADDING)) { // normal expansion + compl_cont_mode = ctrl_x_mode; + if (ctrl_x_mode_not_default()) { + // Remove LOCAL if ctrl_x_mode != CTRL_X_NORMAL + compl_cont_status = 0; + } + compl_cont_status |= CONT_N_ADDS; + compl_startpos = curwin->w_cursor; + startcol = (int)curs_col; + compl_col = 0; + } + + // Work out completion pattern and original text -- webb + if (ctrl_x_mode_normal() + || (ctrl_x_mode & CTRL_X_WANT_IDENT + && !thesaurus_func_complete(ctrl_x_mode))) { + if ((compl_cont_status & CONT_SOL) || ctrl_x_mode_path_defines()) { + if (!(compl_cont_status & CONT_ADDING)) { + while (--startcol >= 0 && vim_isIDc(line[startcol])) {} + compl_col += ++startcol; + compl_length = curs_col - startcol; + } + if (p_ic) { + compl_pattern = str_foldcase(line + compl_col, compl_length, NULL, 0); + } else { + compl_pattern = vim_strnsave(line + compl_col, (size_t)compl_length); + } + } else if (compl_cont_status & CONT_ADDING) { + char_u *prefix = (char_u *)"\\<"; + + // we need up to 2 extra chars for the prefix + compl_pattern = xmalloc(quote_meta(NULL, line + compl_col, + compl_length) + 2); + if (!vim_iswordp(line + compl_col) + || (compl_col > 0 && (vim_iswordp(mb_prevptr(line, line + compl_col))))) { + prefix = (char_u *)""; + } + STRCPY(compl_pattern, prefix); + (void)quote_meta(compl_pattern + STRLEN(prefix), + line + compl_col, compl_length); + } else if (--startcol < 0 + || !vim_iswordp(mb_prevptr(line, line + startcol + 1))) { + // Match any word of at least two chars + compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); + compl_col += curs_col; + compl_length = 0; + } else { + // Search the point of change class of multibyte character + // or not a word single byte character backward. + startcol -= utf_head_off(line, line + startcol); + int base_class = mb_get_class(line + startcol); + while (--startcol >= 0) { + int head_off = utf_head_off(line, line + startcol); + if (base_class != mb_get_class(line + startcol - head_off)) { + break; + } + startcol -= head_off; + } + compl_col += ++startcol; + compl_length = (int)curs_col - startcol; + if (compl_length == 1) { + // Only match word with at least two chars -- webb + // there's no need to call quote_meta, + // xmalloc(7) is enough -- Acevedo + compl_pattern = xmalloc(7); + STRCPY(compl_pattern, "\\<"); + (void)quote_meta(compl_pattern + 2, line + compl_col, 1); + STRCAT(compl_pattern, "\\k"); + } else { + compl_pattern = xmalloc(quote_meta(NULL, line + compl_col, + compl_length) + 2); + STRCPY(compl_pattern, "\\<"); + (void)quote_meta(compl_pattern + 2, line + compl_col, + compl_length); + } + } + } else if (ctrl_x_mode_line_or_eval()) { + compl_col = (colnr_T)getwhitecols(line); + compl_length = (int)curs_col - (int)compl_col; + if (compl_length < 0) { // cursor in indent: empty pattern + compl_length = 0; + } + if (p_ic) { + compl_pattern = str_foldcase(line + compl_col, compl_length, NULL, 0); + } else { + compl_pattern = vim_strnsave(line + compl_col, (size_t)compl_length); + } + } else if (ctrl_x_mode_files()) { + // Go back to just before the first filename character. + if (startcol > 0) { + char_u *p = line + startcol; + + MB_PTR_BACK(line, p); + while (p > line && vim_isfilec(utf_ptr2char((char *)p))) { + MB_PTR_BACK(line, p); + } + if (p == line && vim_isfilec(utf_ptr2char((char *)p))) { + startcol = 0; + } else { + startcol = (int)(p - line) + 1; + } + } + + compl_col += startcol; + compl_length = (int)curs_col - startcol; + compl_pattern = addstar(line + compl_col, (size_t)compl_length, EXPAND_FILES); + } else if (ctrl_x_mode == CTRL_X_CMDLINE) { + compl_pattern = vim_strnsave(line, (size_t)curs_col); + set_cmd_context(&compl_xp, compl_pattern, + (int)STRLEN(compl_pattern), curs_col, false); + if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL + || compl_xp.xp_context == EXPAND_NOTHING) { + // No completion possible, use an empty pattern to get a + // "pattern not found" message. + compl_col = curs_col; + } else { + compl_col = (int)((char_u *)compl_xp.xp_pattern - compl_pattern); + } + compl_length = curs_col - compl_col; + } else if (ctrl_x_mode_function() || ctrl_x_mode_omni() + || thesaurus_func_complete(ctrl_x_mode)) { + // Call user defined function 'completefunc' with "a:findstart" + // set to 1 to obtain the length of text to use for completion. + char_u *funcname; + pos_T pos; + const int save_State = State; + + // Call 'completefunc' or 'omnifunc' and get pattern length as a string + funcname = get_complete_funcname(ctrl_x_mode); + if (*funcname == NUL) { + semsg(_(e_notset), ctrl_x_mode_function() ? "completefunc" : "omnifunc"); + // restore did_ai, so that adding comment leader works + did_ai = save_did_ai; + return FAIL; + } + + typval_T args[3]; + args[0].v_type = VAR_NUMBER; + args[1].v_type = VAR_STRING; + args[2].v_type = VAR_UNKNOWN; + args[0].vval.v_number = 1; + args[1].vval.v_string = ""; + + pos = curwin->w_cursor; + textlock++; + colnr_T col = (colnr_T)call_func_retnr((char *)funcname, 2, args); + textlock--; + + State = save_State; + curwin->w_cursor = pos; // restore the cursor position + validate_cursor(); + if (!equalpos(curwin->w_cursor, pos)) { + emsg(_(e_compldel)); + return FAIL; + } + + // Return value -2 means the user complete function wants to cancel the + // complete without an error, do the same if the function did not execute + // successfully. + if (col == -2 || aborting()) { + return FAIL; + } + // Return value -3 does the same as -2 and leaves CTRL-X mode. + if (col == -3) { + ctrl_x_mode = CTRL_X_NORMAL; + edit_submode = NULL; + if (!shortmess(SHM_COMPLETIONMENU)) { + msg_clr_cmdline(); + } + return FAIL; + } + + // Reset extended parameters of completion, when start new + // completion. + compl_opt_refresh_always = false; + + if (col < 0) { + col = curs_col; + } + compl_col = col; + if (compl_col > curs_col) { + compl_col = curs_col; + } + + // Setup variables for completion. Need to obtain "line" again, + // it may have become invalid. + line = ml_get(curwin->w_cursor.lnum); + compl_length = curs_col - compl_col; + compl_pattern = vim_strnsave(line + compl_col, (size_t)compl_length); + } else if (ctrl_x_mode_spell()) { + if (spell_bad_len > 0) { + assert(spell_bad_len <= INT_MAX); + compl_col = curs_col - (int)spell_bad_len; + } else { + compl_col = spell_word_start(startcol); + } + if (compl_col >= (colnr_T)startcol) { + compl_length = 0; + compl_col = curs_col; + } else { + spell_expand_check_cap(compl_col); + compl_length = (int)curs_col - compl_col; + } + // Need to obtain "line" again, it may have become invalid. + line = ml_get(curwin->w_cursor.lnum); + compl_pattern = vim_strnsave(line + compl_col, (size_t)compl_length); + } else { + internal_error("ins_complete()"); + return FAIL; + } + + if (compl_cont_status & CONT_ADDING) { + edit_submode_pre = (char_u *)_(" Adding"); + if (ctrl_x_mode_line_or_eval()) { + // Insert a new line, keep indentation but ignore 'comments' + char_u *old = curbuf->b_p_com; + + curbuf->b_p_com = (char_u *)""; + compl_startpos.lnum = curwin->w_cursor.lnum; + compl_startpos.col = compl_col; + ins_eol('\r'); + curbuf->b_p_com = old; + compl_length = 0; + compl_col = curwin->w_cursor.col; + } + } else { + edit_submode_pre = NULL; + compl_startpos.col = compl_col; + } + + if (compl_cont_status & CONT_LOCAL) { + edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); + } else { + edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); + } + + // If any of the original typed text has been changed we need to fix + // the redo buffer. + ins_compl_fixRedoBufForLeader(NULL); + + // Always add completion for the original text. + xfree(compl_orig_text); + compl_orig_text = vim_strnsave(line + compl_col, (size_t)compl_length); + if (p_ic) { + flags |= CP_ICASE; + } + if (ins_compl_add(compl_orig_text, -1, NULL, NULL, false, NULL, 0, + flags, false) != OK) { + XFREE_CLEAR(compl_pattern); + XFREE_CLEAR(compl_orig_text); + return FAIL; + } + + // showmode might reset the internal line pointers, so it must + // be called before line = ml_get(), or when this address is no + // longer needed. -- Acevedo. + edit_submode_extra = (char_u *)_("-- Searching..."); + edit_submode_highl = HLF_COUNT; + showmode(); + edit_submode_extra = NULL; + ui_flush(); + } else if (insert_match && stop_arrow() == FAIL) { + return FAIL; + } + + compl_shown_match = compl_curr_match; + compl_shows_dir = compl_direction; + + // Find next match (and following matches). + save_w_wrow = curwin->w_wrow; + save_w_leftcol = curwin->w_leftcol; + n = ins_compl_next(true, ins_compl_key2count(c), insert_match, false); + + if (n > 1) { // all matches have been found + compl_matches = n; + } + compl_curr_match = compl_shown_match; + compl_direction = compl_shows_dir; + + // Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert + // mode. + if (got_int && !global_busy) { + (void)vgetc(); + got_int = false; + } + + // we found no match if the list has only the "compl_orig_text"-entry + if (compl_first_match == compl_first_match->cp_next) { + edit_submode_extra = (compl_cont_status & CONT_ADDING) + && compl_length > 1 + ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); + edit_submode_highl = HLF_E; + // remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, + // because we couldn't expand anything at first place, but if we used + // ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word + // (such as M in M'exico) if not tried already. -- Acevedo + if (compl_length > 1 + || (compl_cont_status & CONT_ADDING) + || (ctrl_x_mode_not_default() + && !ctrl_x_mode_path_patterns() + && !ctrl_x_mode_path_defines())) { + compl_cont_status &= ~CONT_N_ADDS; + } + } + + if (compl_curr_match->cp_flags & CP_CONT_S_IPOS) { + compl_cont_status |= CONT_S_IPOS; + } else { + compl_cont_status &= ~CONT_S_IPOS; + } + + if (edit_submode_extra == NULL) { + 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) { + edit_submode_extra = (char_u *)_("Word from other line"); + edit_submode_highl = HLF_COUNT; + } else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) { + edit_submode_extra = (char_u *)_("The only match"); + edit_submode_highl = HLF_COUNT; + compl_curr_match->cp_number = 1; + } else { + // Update completion sequence number when needed. + if (compl_curr_match->cp_number == -1) { + ins_compl_update_sequence_numbers(); + } + + // The match should always have a sequence number now, this is + // just a safety check. + if (compl_curr_match->cp_number != -1) { + // Space for 10 text chars. + 2x10-digit no.s = 31. + // Translations may need more than twice that. + static char_u match_ref[81]; + + if (compl_matches > 0) { + vim_snprintf((char *)match_ref, sizeof(match_ref), + _("match %d of %d"), + compl_curr_match->cp_number, compl_matches); + } else { + vim_snprintf((char *)match_ref, sizeof(match_ref), + _("match %d"), + compl_curr_match->cp_number); + } + edit_submode_extra = match_ref; + edit_submode_highl = HLF_R; + if (dollar_vcol >= 0) { + curs_columns(curwin, false); + } + } + } + } + + // Show a message about what (completion) mode we're in. + showmode(); + if (!shortmess(SHM_COMPLETIONMENU)) { + if (edit_submode_extra != NULL) { + if (!p_smd) { + msg_hist_off = true; + msg_attr((const char *)edit_submode_extra, + (edit_submode_highl < HLF_COUNT + ? HL_ATTR(edit_submode_highl) : 0)); + msg_hist_off = false; + } + } else { + msg_clr_cmdline(); // necessary for "noshowmode" + } + } + + // Show the popup menu, unless we got interrupted. + if (enable_pum && !compl_interrupted) { + show_pum(save_w_wrow, save_w_leftcol); + } + compl_was_interrupted = compl_interrupted; + compl_interrupted = false; + + return OK; +} + +static void show_pum(int prev_w_wrow, int prev_w_leftcol) +{ + // RedrawingDisabled may be set when invoked through complete(). + int n = RedrawingDisabled; + RedrawingDisabled = 0; + + // If the cursor moved or the display scrolled we need to remove the pum + // first. + setcursor(); + if (prev_w_wrow != curwin->w_wrow || prev_w_leftcol != curwin->w_leftcol) { + ins_compl_del_pum(); + } + + ins_compl_show_pum(); + setcursor(); + RedrawingDisabled = n; +} + +// Looks in the first "len" chars. of "src" for search-metachars. +// If dest is not NULL the chars. are copied there quoting (with +// a backslash) the metachars, and dest would be NUL terminated. +// Returns the length (needed) of dest +static unsigned quote_meta(char_u *dest, char_u *src, int len) +{ + unsigned m = (unsigned)len + 1; // one extra for the NUL + + for (; --len >= 0; src++) { + switch (*src) { + case '.': + case '*': + case '[': + if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) { + break; + } + FALLTHROUGH; + case '~': + if (!p_magic) { // quote these only if magic is set + break; + } + FALLTHROUGH; + case '\\': + if (ctrl_x_mode_dictionary() || ctrl_x_mode_thesaurus()) { + break; + } + FALLTHROUGH; + case '^': // currently it's not needed. + case '$': + m++; + if (dest != NULL) { + *dest++ = '\\'; + } + break; + } + if (dest != NULL) { + *dest++ = *src; + } + // Copy remaining bytes of a multibyte character. + const int mb_len = utfc_ptr2len((char *)src) - 1; + if (mb_len > 0 && len >= mb_len) { + for (int i = 0; i < mb_len; i++) { + len--; + src++; + if (dest != NULL) { + *dest++ = *src; + } + } + } + } + if (dest != NULL) { + *dest = NUL; + } + + return m; +} + +#if defined(EXITFREE) +void free_insexpand_stuff(void) +{ + XFREE_CLEAR(compl_orig_text); +} +#endif + +/// Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly +/// spelled word, if there is one. +static void spell_back_to_badword(void) +{ + pos_T tpos = curwin->w_cursor; + spell_bad_len = spell_move_to(curwin, BACKWARD, true, true, NULL); + if (curwin->w_cursor.col != tpos.col) { + start_arrow(&tpos); + } +} |