aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/nvim/CMakeLists.txt1
-rw-r--r--src/nvim/api/buffer.c4
-rw-r--r--src/nvim/api/private/defs.h1
-rw-r--r--src/nvim/api/private/helpers.h12
-rw-r--r--src/nvim/api/ui.c17
-rw-r--r--src/nvim/api/vim.c25
-rw-r--r--src/nvim/autocmd.c5
-rw-r--r--src/nvim/buffer_updates.c32
-rw-r--r--src/nvim/change.c1
-rw-r--r--src/nvim/decoration_provider.c32
-rw-r--r--src/nvim/edit.c3797
-rw-r--r--src/nvim/edit.h20
-rw-r--r--src/nvim/eval/funcs.c65
-rw-r--r--src/nvim/eval/userfunc.c1
-rw-r--r--src/nvim/ex_cmds.c33
-rw-r--r--src/nvim/fileio.c3
-rw-r--r--src/nvim/generators/gen_api_dispatch.lua4
-rw-r--r--src/nvim/getchar.c1
-rw-r--r--src/nvim/globals.h4
-rw-r--r--src/nvim/grid_defs.h9
-rw-r--r--src/nvim/highlight.c66
-rw-r--r--src/nvim/highlight_group.c2
-rw-r--r--src/nvim/insexpand.c3886
-rw-r--r--src/nvim/insexpand.h18
-rw-r--r--src/nvim/lua/executor.c6
-rw-r--r--src/nvim/main.c3
-rw-r--r--src/nvim/memline.c208
-rw-r--r--src/nvim/memory.c2
-rw-r--r--src/nvim/msgpack_rpc/channel.c9
-rw-r--r--src/nvim/msgpack_rpc/unpacker.c208
-rw-r--r--src/nvim/msgpack_rpc/unpacker.h7
-rw-r--r--src/nvim/normal.c7
-rw-r--r--src/nvim/option.c1
-rw-r--r--src/nvim/os/stdpaths.c4
-rw-r--r--src/nvim/popupmnu.c1
-rw-r--r--src/nvim/screen.c1
-rw-r--r--src/nvim/search.c5
-rw-r--r--src/nvim/spell.c3
-rw-r--r--src/nvim/state.c2
-rw-r--r--src/nvim/tag.c3
-rw-r--r--src/nvim/testdir/setup.vim1
-rw-r--r--src/nvim/testdir/term_util.vim2
-rw-r--r--src/nvim/testdir/test_clientserver.vim87
-rw-r--r--src/nvim/testdir/test_ex_mode.vim4
-rw-r--r--src/nvim/testdir/test_excmd.vim120
-rw-r--r--src/nvim/testdir/test_expand.vim9
-rw-r--r--src/nvim/testdir/test_functions.vim20
-rw-r--r--src/nvim/testdir/test_options.vim9
-rw-r--r--src/nvim/testdir/test_startup.vim242
-rw-r--r--src/nvim/testdir/test_textformat.vim54
-rw-r--r--src/nvim/testdir/test_trycatch.vim6
-rw-r--r--src/nvim/testdir/test_window_cmd.vim1
-rw-r--r--src/nvim/ui.c6
-rw-r--r--src/nvim/ui_client.c137
-rw-r--r--src/nvim/ui_client.h6
55 files changed, 4983 insertions, 4230 deletions
diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt
index 360993de68..384e672529 100755
--- a/src/nvim/CMakeLists.txt
+++ b/src/nvim/CMakeLists.txt
@@ -160,7 +160,6 @@ list(REMOVE_ITEM NVIM_SOURCES ${to_remove})
set(CONV_SOURCES
lua/treesitter.c
mbyte.c
- memline.c
regexp.c
screen.c
search.c
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index 806b649ce6..5e54d0ec9a 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -538,9 +538,9 @@ void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, Integer start_row, In
Integer end_row, Integer end_col, ArrayOf(String) replacement, Error *err)
FUNC_API_SINCE(7)
{
- FIXED_TEMP_ARRAY(scratch, 1);
+ MAXSIZE_TEMP_ARRAY(scratch, 1);
if (replacement.size == 0) {
- scratch.items[0] = STRING_OBJ(STATIC_CSTR_AS_STRING(""));
+ ADD_C(scratch, STRING_OBJ(STATIC_CSTR_AS_STRING("")));
replacement = scratch;
}
diff --git a/src/nvim/api/private/defs.h b/src/nvim/api/private/defs.h
index b1e0dd364c..9c7e59e4b3 100644
--- a/src/nvim/api/private/defs.h
+++ b/src/nvim/api/private/defs.h
@@ -36,6 +36,7 @@ typedef enum {
kMessageTypeRequest = 0,
kMessageTypeResponse = 1,
kMessageTypeNotification = 2,
+ kMessageTypeRedrawEvent = 3,
} MessageType;
/// Mask for all internal calls
diff --git a/src/nvim/api/private/helpers.h b/src/nvim/api/private/helpers.h
index a4348d8b44..1441da853c 100644
--- a/src/nvim/api/private/helpers.h
+++ b/src/nvim/api/private/helpers.h
@@ -74,18 +74,18 @@
#define ADD_C(array, item) \
kv_push_c(array, item)
-#define FIXED_TEMP_ARRAY(name, fixsize) \
- Array name = ARRAY_DICT_INIT; \
- Object name##__items[fixsize]; \
- name.size = fixsize; \
- name.items = name##__items; \
-
#define MAXSIZE_TEMP_ARRAY(name, maxsize) \
Array name = ARRAY_DICT_INIT; \
Object name##__items[maxsize]; \
name.capacity = maxsize; \
name.items = name##__items; \
+#define MAXSIZE_TEMP_DICT(name, maxsize) \
+ Dictionary name = ARRAY_DICT_INIT; \
+ KeyValuePair name##__items[maxsize]; \
+ name.capacity = maxsize; \
+ name.items = name##__items; \
+
#define cbuf_as_string(d, s) ((String) { .data = d, .size = s })
#define STATIC_CSTR_AS_STRING(s) ((String) { .data = s, .size = sizeof(s) - 1 })
diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c
index 54ce838b9b..aa7bed1132 100644
--- a/src/nvim/api/ui.c
+++ b/src/nvim/api/ui.c
@@ -748,8 +748,10 @@ static void remote_ui_hl_attr_define(UI *ui, Integer id, HlAttrs rgb_attrs, HlAt
UIData *data = ui->data;
Array args = data->call_buf;
ADD_C(args, INTEGER_OBJ(id));
- ADD_C(args, DICTIONARY_OBJ(hlattrs2dict(rgb_attrs, true)));
- ADD_C(args, DICTIONARY_OBJ(hlattrs2dict(cterm_attrs, false)));
+ MAXSIZE_TEMP_DICT(rgb, 16);
+ MAXSIZE_TEMP_DICT(cterm, 16);
+ ADD_C(args, DICTIONARY_OBJ(hlattrs2dict(&rgb, rgb_attrs, true)));
+ ADD_C(args, DICTIONARY_OBJ(hlattrs2dict(&cterm, cterm_attrs, false)));
if (ui->ui_ext[kUIHlState]) {
ADD_C(args, ARRAY_OBJ(info));
@@ -758,9 +760,6 @@ static void remote_ui_hl_attr_define(UI *ui, Integer id, HlAttrs rgb_attrs, HlAt
}
push_call(ui, "hl_attr_define", args);
- // TODO(bfredl): could be elided
- api_free_dictionary(kv_A(args, 1).data.dictionary);
- api_free_dictionary(kv_A(args, 2).data.dictionary);
}
static void remote_ui_highlight_set(UI *ui, int id)
@@ -772,11 +771,9 @@ static void remote_ui_highlight_set(UI *ui, int id)
return;
}
data->hl_id = id;
- Dictionary hl = hlattrs2dict(syn_attr2entry(id), ui->rgb);
-
- ADD_C(args, DICTIONARY_OBJ(hl));
+ MAXSIZE_TEMP_DICT(dict, 16);
+ ADD_C(args, DICTIONARY_OBJ(hlattrs2dict(&dict, syn_attr2entry(id), ui->rgb)));
push_call(ui, "highlight_set", args);
- api_free_dictionary(kv_A(args, 0).data.dictionary);
}
/// "true" cursor used only for input focus
@@ -963,7 +960,7 @@ static Array translate_contents(UI *ui, Array contents)
Array new_item = ARRAY_DICT_INIT;
int attr = (int)item.items[0].data.integer;
if (attr) {
- Dictionary rgb_attrs = hlattrs2dict(syn_attr2entry(attr), ui->rgb);
+ Dictionary rgb_attrs = hlattrs2dict(NULL, syn_attr2entry(attr), ui->rgb);
ADD(new_item, DICTIONARY_OBJ(rgb_attrs));
} else {
ADD(new_item, DICTIONARY_OBJ((Dictionary)ARRAY_DICT_INIT));
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 56516b2ac7..256482fb38 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -37,6 +37,7 @@
#include "nvim/highlight.h"
#include "nvim/highlight_defs.h"
#include "nvim/highlight_group.h"
+#include "nvim/insexpand.h"
#include "nvim/lua/executor.h"
#include "nvim/mapping.h"
#include "nvim/mark.h"
@@ -472,10 +473,10 @@ Object nvim_exec_lua(String code, Array args, Error *err)
Object nvim_notify(String msg, Integer log_level, Dictionary opts, Error *err)
FUNC_API_SINCE(7)
{
- FIXED_TEMP_ARRAY(args, 3);
- args.items[0] = STRING_OBJ(msg);
- args.items[1] = INTEGER_OBJ(log_level);
- args.items[2] = DICTIONARY_OBJ(opts);
+ MAXSIZE_TEMP_ARRAY(args, 3);
+ ADD_C(args, STRING_OBJ(msg));
+ ADD_C(args, INTEGER_OBJ(log_level));
+ ADD_C(args, DICTIONARY_OBJ(opts));
return nlua_exec(STATIC_CSTR_AS_STRING("return vim.notify(...)"), args, err);
}
@@ -1009,10 +1010,10 @@ static void term_write(char *buf, size_t size, void *data)
if (cb == LUA_NOREF) {
return;
}
- FIXED_TEMP_ARRAY(args, 3);
- args.items[0] = INTEGER_OBJ((Integer)chan->id);
- args.items[1] = BUFFER_OBJ(terminal_buf(chan->term));
- args.items[2] = STRING_OBJ(((String){ .data = buf, .size = size }));
+ MAXSIZE_TEMP_ARRAY(args, 3);
+ ADD_C(args, INTEGER_OBJ((Integer)chan->id));
+ ADD_C(args, BUFFER_OBJ(terminal_buf(chan->term)));
+ ADD_C(args, STRING_OBJ(((String){ .data = buf, .size = size })));
textlock++;
nlua_call_ref(cb, "input", args, false, NULL);
textlock--;
@@ -2256,3 +2257,11 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error *
return result;
}
+
+void nvim_error_event(uint64_t channel_id, Integer lvl, String data)
+ FUNC_API_REMOTE_ONLY
+{
+ // TODO(bfredl): consider printing message to user, as will be relevant
+ // if we fork nvim processes as async workers
+ ELOG("async error on channel %" PRId64 ": %s", channel_id, data.size ? data.data : "");
+}
diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c
index d51079b515..d364881084 100644
--- a/src/nvim/autocmd.c
+++ b/src/nvim/autocmd.c
@@ -18,6 +18,7 @@
#include "nvim/ex_getln.h"
#include "nvim/fileio.h"
#include "nvim/getchar.h"
+#include "nvim/insexpand.h"
#include "nvim/lua/executor.h"
#include "nvim/map.h"
#include "nvim/option.h"
@@ -2051,8 +2052,8 @@ static bool call_autocmd_callback(const AutoCmd *ac, const AutoPatCmd *apc)
break;
}
- FIXED_TEMP_ARRAY(args, 1);
- args.items[0] = DICTIONARY_OBJ(data);
+ MAXSIZE_TEMP_ARRAY(args, 1);
+ ADD_C(args, DICTIONARY_OBJ(data));
Object result = nlua_call_ref(callback.data.luaref, NULL, args, true, NULL);
if (result.type == kObjectTypeBoolean) {
diff --git a/src/nvim/buffer_updates.c b/src/nvim/buffer_updates.c
index 47b88945c7..14973502ab 100644
--- a/src/nvim/buffer_updates.c
+++ b/src/nvim/buffer_updates.c
@@ -316,23 +316,23 @@ void buf_updates_send_splice(buf_T *buf, int start_row, colnr_T start_col, bcoun
BufUpdateCallbacks cb = kv_A(buf->update_callbacks, i);
bool keep = true;
if (cb.on_bytes != LUA_NOREF && (cb.preview || !cmdpreview)) {
- FIXED_TEMP_ARRAY(args, 11);
+ MAXSIZE_TEMP_ARRAY(args, 11);
// the first argument is always the buffer handle
- args.items[0] = BUFFER_OBJ(buf->handle);
+ ADD_C(args, BUFFER_OBJ(buf->handle));
// next argument is b:changedtick
- args.items[1] = INTEGER_OBJ(buf_get_changedtick(buf));
-
- args.items[2] = INTEGER_OBJ(start_row);
- args.items[3] = INTEGER_OBJ(start_col);
- args.items[4] = INTEGER_OBJ(start_byte);
- args.items[5] = INTEGER_OBJ(old_row);
- args.items[6] = INTEGER_OBJ(old_col);
- args.items[7] = INTEGER_OBJ(old_byte);
- args.items[8] = INTEGER_OBJ(new_row);
- args.items[9] = INTEGER_OBJ(new_col);
- args.items[10] = INTEGER_OBJ(new_byte);
+ ADD_C(args, INTEGER_OBJ(buf_get_changedtick(buf)));
+
+ ADD_C(args, INTEGER_OBJ(start_row));
+ ADD_C(args, INTEGER_OBJ(start_col));
+ ADD_C(args, INTEGER_OBJ(start_byte));
+ ADD_C(args, INTEGER_OBJ(old_row));
+ ADD_C(args, INTEGER_OBJ(old_col));
+ ADD_C(args, INTEGER_OBJ(old_byte));
+ ADD_C(args, INTEGER_OBJ(new_row));
+ ADD_C(args, INTEGER_OBJ(new_col));
+ ADD_C(args, INTEGER_OBJ(new_byte));
textlock++;
Object res = nlua_call_ref(cb.on_bytes, "bytes", args, true, NULL);
@@ -361,13 +361,13 @@ void buf_updates_changedtick(buf_T *buf)
BufUpdateCallbacks cb = kv_A(buf->update_callbacks, i);
bool keep = true;
if (cb.on_changedtick != LUA_NOREF) {
- FIXED_TEMP_ARRAY(args, 2);
+ MAXSIZE_TEMP_ARRAY(args, 2);
// the first argument is always the buffer handle
- args.items[0] = BUFFER_OBJ(buf->handle);
+ ADD_C(args, BUFFER_OBJ(buf->handle));
// next argument is b:changedtick
- args.items[1] = INTEGER_OBJ(buf_get_changedtick(buf));
+ ADD_C(args, INTEGER_OBJ(buf_get_changedtick(buf)));
textlock++;
Object res = nlua_call_ref(cb.on_changedtick, "changedtick",
diff --git a/src/nvim/change.c b/src/nvim/change.c
index 4568b71fd9..22975d75d9 100644
--- a/src/nvim/change.c
+++ b/src/nvim/change.c
@@ -17,6 +17,7 @@
#include "nvim/fold.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
+#include "nvim/insexpand.h"
#include "nvim/mark.h"
#include "nvim/memline.h"
#include "nvim/move.h"
diff --git a/src/nvim/decoration_provider.c b/src/nvim/decoration_provider.c
index 0f6a260247..04d875c4e3 100644
--- a/src/nvim/decoration_provider.c
+++ b/src/nvim/decoration_provider.c
@@ -63,9 +63,9 @@ void decor_providers_start(DecorProviders *providers, int type, char **err)
bool active;
if (p->redraw_start != LUA_NOREF) {
- FIXED_TEMP_ARRAY(args, 2);
- args.items[0] = INTEGER_OBJ((int)display_tick);
- args.items[1] = INTEGER_OBJ(type);
+ MAXSIZE_TEMP_ARRAY(args, 2);
+ ADD_C(args, INTEGER_OBJ((int)display_tick));
+ ADD_C(args, INTEGER_OBJ(type));
active = decor_provider_invoke(p->ns_id, "start", p->redraw_start, args, true, err);
} else {
active = true;
@@ -96,12 +96,12 @@ void decor_providers_invoke_win(win_T *wp, DecorProviders *providers,
for (size_t k = 0; k < kv_size(*providers); k++) {
DecorProvider *p = kv_A(*providers, k);
if (p && p->redraw_win != LUA_NOREF) {
- FIXED_TEMP_ARRAY(args, 4);
- args.items[0] = WINDOW_OBJ(wp->handle);
- args.items[1] = BUFFER_OBJ(wp->w_buffer->handle);
+ MAXSIZE_TEMP_ARRAY(args, 4);
+ ADD_C(args, WINDOW_OBJ(wp->handle));
+ ADD_C(args, BUFFER_OBJ(wp->w_buffer->handle));
// TODO(bfredl): we are not using this, but should be first drawn line?
- args.items[2] = INTEGER_OBJ(wp->w_topline - 1);
- args.items[3] = INTEGER_OBJ(knownmax);
+ ADD_C(args, INTEGER_OBJ(wp->w_topline - 1));
+ ADD_C(args, INTEGER_OBJ(knownmax));
if (decor_provider_invoke(p->ns_id, "win", p->redraw_win, args, true, err)) {
kvi_push(*line_providers, p);
}
@@ -124,10 +124,10 @@ void providers_invoke_line(win_T *wp, DecorProviders *providers, int row, bool *
for (size_t k = 0; k < kv_size(*providers); k++) {
DecorProvider *p = kv_A(*providers, k);
if (p && p->redraw_line != LUA_NOREF) {
- FIXED_TEMP_ARRAY(args, 3);
- args.items[0] = WINDOW_OBJ(wp->handle);
- args.items[1] = BUFFER_OBJ(wp->w_buffer->handle);
- args.items[2] = INTEGER_OBJ(row);
+ MAXSIZE_TEMP_ARRAY(args, 3);
+ ADD_C(args, WINDOW_OBJ(wp->handle));
+ ADD_C(args, BUFFER_OBJ(wp->w_buffer->handle));
+ ADD_C(args, INTEGER_OBJ(row));
if (decor_provider_invoke(p->ns_id, "line", p->redraw_line, args, true, err)) {
*has_decor = true;
} else {
@@ -150,8 +150,8 @@ void decor_providers_invoke_buf(buf_T *buf, DecorProviders *providers, char **er
for (size_t i = 0; i < kv_size(*providers); i++) {
DecorProvider *p = kv_A(*providers, i);
if (p && p->redraw_buf != LUA_NOREF) {
- FIXED_TEMP_ARRAY(args, 1);
- args.items[0] = BUFFER_OBJ(buf->handle);
+ MAXSIZE_TEMP_ARRAY(args, 1);
+ ADD_C(args, BUFFER_OBJ(buf->handle));
decor_provider_invoke(p->ns_id, "buf", p->redraw_buf, args, true, err);
}
}
@@ -167,8 +167,8 @@ void decor_providers_invoke_end(DecorProviders *providers, char **err)
for (size_t i = 0; i < kv_size(*providers); i++) {
DecorProvider *p = kv_A(*providers, i);
if (p && p->active && p->redraw_end != LUA_NOREF) {
- FIXED_TEMP_ARRAY(args, 1);
- args.items[0] = INTEGER_OBJ((int)display_tick);
+ MAXSIZE_TEMP_ARRAY(args, 1);
+ ADD_C(args, INTEGER_OBJ((int)display_tick));
decor_provider_invoke(p->ns_id, "end", p->redraw_end, args, true, err);
}
}
diff --git a/src/nvim/edit.c b/src/nvim/edit.c
index 0571e71cb5..6658f69dda 100644
--- a/src/nvim/edit.c
+++ b/src/nvim/edit.c
@@ -18,7 +18,6 @@
#include "nvim/digraph.h"
#include "nvim/edit.h"
#include "nvim/eval.h"
-#include "nvim/eval/typval.h"
#include "nvim/event/loop.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_getln.h"
@@ -29,6 +28,7 @@
#include "nvim/highlight_group.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
+#include "nvim/insexpand.h"
#include "nvim/keycodes.h"
#include "nvim/main.h"
#include "nvim/mapping.h"
@@ -48,180 +48,18 @@
#include "nvim/plines.h"
#include "nvim/popupmnu.h"
#include "nvim/quickfix.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/syntax.h"
-#include "nvim/tag.h"
#include "nvim/terminal.h"
#include "nvim/ui.h"
#include "nvim/undo.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]
-#define CTRL_X_MODE_LINE_OR_EVAL(m) \
- ((m) == CTRL_X_WHOLE_LINE || (m) == CTRL_X_EVAL)
-
-// 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",
-};
-
-static char e_hitend[] = N_("Hit end of paragraph");
-static char e_compldel[] = N_("E840: Completion function deleted text");
-
-/*
- * 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
-};
-
-/*
- * 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 int 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 int compl_no_insert = FALSE; /* FALSE: select & insert
- TRUE: noinsert */
-static int compl_no_select = FALSE; /* FALSE: select & insert
- TRUE: noselect */
-
-static bool compl_used_match; // Selected one of the matches.
- // When false the match was edited or using
- // the longest common string.
-
-static int compl_was_interrupted = FALSE; /* didn't finish finding
- completions. */
-
-static 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 int pum_selected_item = -1;
-
-/// state for pum_ext_select_item.
-struct {
- bool active;
- int item;
- bool insert;
- bool finish;
-} pum_want;
-
typedef struct insert_state {
VimState state;
cmdarg_T *ca;
@@ -253,8 +91,6 @@ typedef struct insert_state {
#define BACKSPACE_WORD_NOT_SPACE 3
#define BACKSPACE_LINE 4
-static size_t spell_bad_len = 0; // length of located bad word
-
static colnr_T Insstart_textlen; // length of line when insert started
static colnr_T Insstart_blank_vcol; // vcol for first inserted blank
static bool update_Insstart_orig = true; // set Insstart_orig to Insstart
@@ -521,7 +357,7 @@ static int insert_check(VimState *state)
// If typed something may trigger CursorHoldI again.
if (s->c != K_EVENT
// but not in CTRL-X mode, a script can't restore the state
- && ctrl_x_mode == CTRL_X_NORMAL) {
+ && ctrl_x_mode_normal()) {
did_cursorhold = false;
}
@@ -532,8 +368,8 @@ static int insert_check(VimState *state)
if (can_cindent
&& cindent_on()
- && ctrl_x_mode == CTRL_X_NORMAL
- && !compl_started) {
+ && ctrl_x_mode_normal()
+ && !ins_compl_active()) {
insert_do_cindent(s);
}
@@ -551,7 +387,7 @@ static int insert_check(VimState *state)
Insstart_orig = Insstart;
}
- if (stop_insert_mode && !compl_started) {
+ if (stop_insert_mode && !ins_compl_active()) {
// ":stopinsert" used
s->count = 0;
return 0; // exit insert mode
@@ -688,29 +524,25 @@ static int insert_execute(VimState *state, int key)
// Special handling of keys while the popup menu is visible or wanted
// and the cursor is still in the completed word. Only when there is
// a match, skip this when no matches were found.
- if (compl_started
+ if (ins_compl_active()
&& pum_wanted()
- && curwin->w_cursor.col >= compl_col
- && (compl_shown_match == NULL
- || compl_shown_match != compl_shown_match->cp_next)) {
+ && curwin->w_cursor.col >= ins_compl_col()
+ && ins_compl_has_shown_match()) {
// BS: Delete one character from "compl_leader".
if ((s->c == K_BS || s->c == Ctrl_H)
- && curwin->w_cursor.col > compl_col
+ && curwin->w_cursor.col > ins_compl_col()
&& (s->c = ins_compl_bs()) == NUL) {
return 1; // continue
}
// When no match was selected or it was edited.
- if (!compl_used_match) {
+ if (!ins_compl_used_match()) {
// CTRL-L: Add one character from the current match to
// "compl_leader". Except when at the original match and
// there is nothing to add, CTRL-L works like CTRL-P then.
if (s->c == Ctrl_L
- && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
- || (compl_shown_match != NULL
- && compl_shown_match->cp_str != NULL
- && (int)STRLEN(compl_shown_match->cp_str)
- > curwin->w_cursor.col - compl_col))) {
+ && (!ctrl_x_mode_line_or_eval()
+ || ins_compl_long_shown_match())) {
ins_compl_addfrommatch();
return 1; // continue
}
@@ -736,7 +568,7 @@ static int insert_execute(VimState *state, int key)
// Pressing CTRL-Y selects the current match. When
// compl_enter_selects is set the Enter key does the same.
if ((s->c == Ctrl_Y
- || (compl_enter_selects
+ || (ins_compl_enter_selects()
&& (s->c == CAR || s->c == K_KENTER || s->c == NL)))
&& stop_arrow() == OK) {
ins_compl_delete();
@@ -747,7 +579,7 @@ static int insert_execute(VimState *state, int key)
// Prepare for or stop CTRL-X mode. This doesn't do completion, but it does
// fix up the text when finishing completion.
- compl_get_longest = false;
+ ins_compl_init_get_longest();
if (ins_compl_prep(s->c)) {
return 1; // continue
}
@@ -779,8 +611,7 @@ static int insert_execute(VimState *state, int key)
s->c = do_digraph(s->c);
- if ((s->c == Ctrl_V || s->c == Ctrl_Q)
- && (ctrl_x_mode == CTRL_X_CMDLINE || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X)) {
+ if ((s->c == Ctrl_V || s->c == Ctrl_Q) && ctrl_x_mode_cmdline()) {
insert_do_complete(s);
return 1;
}
@@ -791,8 +622,7 @@ static int insert_execute(VimState *state, int key)
return 1; // continue
}
- if (cindent_on()
- && ctrl_x_mode == 0) {
+ if (cindent_on() && ctrl_x_mode_none()) {
// A key name preceded by a bang means this key is not to be
// inserted. Skip ahead to the re-indenting below.
// A key name preceded by a star means that indenting has to be
@@ -873,7 +703,7 @@ static int insert_handle_key(InsertState *s)
goto normalchar; // insert CTRL-Z as normal char
case Ctrl_O: // execute one command
- if (ctrl_x_mode == CTRL_X_OMNI) {
+ if (ctrl_x_mode_omni()) {
insert_do_complete(s);
break;
}
@@ -946,14 +776,14 @@ static int insert_handle_key(InsertState *s)
break;
case Ctrl_D: // Make indent one shiftwidth smaller.
- if (ctrl_x_mode == CTRL_X_PATH_DEFINES) {
+ if (ctrl_x_mode_path_defines()) {
insert_do_complete(s);
break;
}
FALLTHROUGH;
case Ctrl_T: // Make indent one shiftwidth greater.
- if (s->c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) {
+ if (s->c == Ctrl_T && ctrl_x_mode_thesaurus()) {
if (check_compl_option(false)) {
insert_do_complete(s);
}
@@ -992,7 +822,7 @@ static int insert_handle_key(InsertState *s)
case Ctrl_U: // delete all inserted text in current line
// CTRL-X CTRL-U completes with 'completefunc'.
- if (ctrl_x_mode == CTRL_X_FUNCTION) {
+ if (ctrl_x_mode_function()) {
insert_do_complete(s);
} else {
s->did_backspace = ins_bs(s->c, BACKSPACE_LINE, &s->inserted_space);
@@ -1157,7 +987,7 @@ check_pum:
FALLTHROUGH;
case TAB: // TAB or Complete patterns along path
- if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) {
+ if (ctrl_x_mode_path_patterns()) {
insert_do_complete(s);
break;
}
@@ -1205,7 +1035,7 @@ check_pum:
break;
case Ctrl_K: // digraph or keyword completion
- if (ctrl_x_mode == CTRL_X_DICTIONARY) {
+ if (ctrl_x_mode_dictionary()) {
if (check_compl_option(true)) {
insert_do_complete(s);
}
@@ -1223,7 +1053,7 @@ check_pum:
break;
case Ctrl_RSB: // Tag name completion after ^X
- if (ctrl_x_mode != CTRL_X_TAGS) {
+ if (!ctrl_x_mode_tags()) {
goto normalchar;
} else {
insert_do_complete(s);
@@ -1231,7 +1061,7 @@ check_pum:
break;
case Ctrl_F: // File name completion after ^X
- if (ctrl_x_mode != CTRL_X_FILES) {
+ if (!ctrl_x_mode_files()) {
goto normalchar;
} else {
insert_do_complete(s);
@@ -1240,7 +1070,7 @@ check_pum:
case 's': // Spelling completion after ^X
case Ctrl_S:
- if (ctrl_x_mode != CTRL_X_SPELL) {
+ if (!ctrl_x_mode_spell()) {
goto normalchar;
} else {
insert_do_complete(s);
@@ -1248,7 +1078,7 @@ check_pum:
break;
case Ctrl_L: // Whole line completion after ^X
- if (ctrl_x_mode != CTRL_X_WHOLE_LINE) {
+ if (!ctrl_x_mode_whole_line()) {
goto normalchar;
}
FALLTHROUGH;
@@ -1258,8 +1088,7 @@ check_pum:
// if 'complete' is empty then plain ^P is no longer special,
// but it is under other ^X modes
if (*curbuf->b_p_cpt == NUL
- && (ctrl_x_mode == CTRL_X_NORMAL
- || ctrl_x_mode == CTRL_X_WHOLE_LINE)
+ && (ctrl_x_mode_normal() || ctrl_x_mode_whole_line())
&& !(compl_cont_status & CONT_LOCAL)) {
goto normalchar;
}
@@ -1409,7 +1238,7 @@ bool edit(int cmdchar, bool startln, long count)
// Don't allow changes in the buffer while editing the cmdline. The
// caller of getcmdline() may get confused.
// Don't allow recursive insert mode when busy with completion.
- if (textlock != 0 || compl_started || compl_busy || pum_visible()) {
+ if (textlock != 0 || ins_compl_active() || compl_busy || pum_visible()) {
emsg(_(e_textlock));
return false;
}
@@ -1425,6 +1254,11 @@ bool edit(int cmdchar, bool startln, long count)
return s->c == Ctrl_O;
}
+bool ins_need_undo_get(void)
+{
+ return ins_need_undo;
+}
+
/// Redraw for Insert mode.
/// This is postponed until getting the next character to make '$' in the 'cpo'
/// option work correctly.
@@ -1432,7 +1266,7 @@ bool edit(int cmdchar, bool startln, long count)
/// inserting sequences of characters (e.g., for CTRL-R).
///
/// @param ready not busy with something
-static void ins_redraw(bool ready)
+void ins_redraw(bool ready)
{
if (char_avail()) {
return;
@@ -2019,3523 +1853,6 @@ static bool del_char_after_col(int limit_col)
return true;
}
-/*
- * CTRL-X pressed in Insert mode.
- */
-static void ins_ctrl_x(void)
-{
- if (ctrl_x_mode != CTRL_X_CMDLINE && ctrl_x_mode != CTRL_X_CMDLINE_CTRL_X) {
- // 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();
-}
-
-// 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.
-static 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
-static 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 + ins_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 + ins_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;
-}
-
-// 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;
- }
-}
-
-/*
- * Start completion for the complete() function.
- * "startcol" is where the matched text starts (1 is first column).
- * "list" is 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 != CTRL_X_NORMAL) {
- 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();
-}
-
-/* "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.
-static 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;
-}
-
-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(ctrl_x_mode)) {
- 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,
- &regmatch, 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(ctrl_x_mode)) {
- 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;
-}
-
-static 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;
-}
-
-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(ins_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))
-}
-
-// Return Insert completion mode name string
-static char_u *ins_compl_mode(void)
-{
- if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET || ctrl_x_mode == CTRL_X_SCROLL || compl_started) {
- return (char_u *)ctrl_x_mode_names[ctrl_x_mode & ~CTRL_X_WANT_IDENT];
- }
- return (char_u *)"";
-}
-
-/*
- * 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.
- */
-static 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 != CTRL_X_OMNI)
- || ctrl_x_mode == CTRL_X_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 == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_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 + ins_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 ins_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.
- */
-static 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.
- */
-static 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;
-static 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 == CTRL_X_NOT_DEFINED_YET
- || (ctrl_x_mode == CTRL_X_NORMAL && !compl_started)) {
- compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
- compl_used_match = true;
- }
-
- if (ctrl_x_mode == CTRL_X_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 != CTRL_X_NORMAL) {
- // We're already in CTRL-X mode, do we stay in it?
- if (!vim_is_ctrl_x_key(c)) {
- if (ctrl_x_mode == CTRL_X_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 == CTRL_X_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 && 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 && 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 + ins_compl_len());
- } else if (compl_first_match != NULL) {
- ins_bytes(compl_orig_text + ins_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 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);
- }
-}
-
-/// 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);
-}
-
-/// 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 };
- int l_ctrl_x_mode = ctrl_x_mode;
-
- 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;
-
- assert(l_ctrl_x_mode == ctrl_x_mode);
-
- // For ^N/^P pick a new entry from e_cpt if compl_started is off,
- // or if found_all says this entry is done. For ^X^L only use the
- // entries from 'complete' that look in loaded buffers.
- if ((l_ctrl_x_mode == CTRL_X_NORMAL
- || CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))
- && (!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 == CTRL_X_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(l_ctrl_x_mode)) {
- 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
- | (l_ctrl_x_mode != CTRL_X_NORMAL ? 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(l_ctrl_x_mode) || word-wise search that
- // has added a word that was at the beginning of the line.
- if (CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode)
- || (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(l_ctrl_x_mode)) {
- 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);
- 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 l_ctrl_x_mode == CTRL_X_NORMAL) or when we've found a new match
- if ((l_ctrl_x_mode != CTRL_X_NORMAL
- && !CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))
- || 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 ((l_ctrl_x_mode != CTRL_X_NORMAL
- && !CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))
- || 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 ((l_ctrl_x_mode == CTRL_X_NORMAL
- || CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))
- && *e_cpt == NUL) { // Got to end of 'complete'
- found_new_match = FAIL;
- }
-
- i = -1; // total of matches, unknown
- if (found_new_match == FAIL
- || (l_ctrl_x_mode != CTRL_X_NORMAL
- && !CTRL_X_MODE_LINE_OR_EVAL(l_ctrl_x_mode))) {
- 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.
-static 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().
-static void ins_compl_insert(int in_compl_func)
-{
- ins_bytes(compl_shown_match->cp_str + ins_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;
- }
-}
-
-// 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;
-}
-
-/// 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(int allow_get_expansion, int count, int insert_match, int 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 + ins_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 + ins_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
-// "frequency" specifies out of how many calls we actually check.
-// "in_compl_func" is TRUE when called from complete_check(), don't set
-// compl_curr_match.
-void ins_compl_check_keys(int frequency, int 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.
- */
-static 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 == CTRL_X_NORMAL
- || ctrl_x_mode == CTRL_X_PATH_PATTERNS
- || ctrl_x_mode == CTRL_X_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(ctrl_x_mode)) {
- 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 != CTRL_X_NORMAL) {
- // 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 == CTRL_X_NORMAL
- || (ctrl_x_mode & CTRL_X_WANT_IDENT
- && !thesaurus_func_complete(ctrl_x_mode))) {
- if ((compl_cont_status & CONT_SOL)
- || ctrl_x_mode == CTRL_X_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(ctrl_x_mode)) {
- 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 == CTRL_X_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 || ctrl_x_mode == CTRL_X_CMDLINE_CTRL_X) {
- 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 == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_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 == CTRL_X_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 == CTRL_X_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(ctrl_x_mode)) {
- // 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 != CTRL_X_NORMAL
- && ctrl_x_mode != CTRL_X_PATH_PATTERNS
- && ctrl_x_mode != CTRL_X_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;
-}
-
-/*
- * 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 == CTRL_X_DICTIONARY
- || ctrl_x_mode == CTRL_X_THESAURUS) {
- break;
- }
- FALLTHROUGH;
- case '~':
- if (!p_magic) { // quote these only if magic is set
- break;
- }
- FALLTHROUGH;
- case '\\':
- if (ctrl_x_mode == CTRL_X_DICTIONARY
- || ctrl_x_mode == CTRL_X_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;
-}
-
/// Next character is interpreted literally.
/// A one, two or three digit decimal number is interpreted as its byte value.
/// If one or two digits are entered, the next character is given to vungetc().
@@ -6500,7 +2817,7 @@ static void redo_literal(int c)
/// For undo/redo it resembles hitting the <ESC> key.
///
/// @param end_insert_pos can be NULL
-static void start_arrow(pos_T *end_insert_pos)
+void start_arrow(pos_T *end_insert_pos)
{
start_arrow_common(end_insert_pos, true);
}
@@ -6546,19 +2863,6 @@ static void check_spell_redraw(void)
}
/*
- * 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);
- }
-}
-
-/*
* stop_arrow() is called before a change is made in insert mode.
* If an arrow key has been used, start a new insertion.
* Returns FAIL if undo is impossible, shouldn't insert then.
@@ -6748,9 +3052,7 @@ void set_last_insert(int c)
void free_last_insert(void)
{
XFREE_CLEAR(last_insert);
- XFREE_CLEAR(compl_orig_text);
}
-
#endif
/*
@@ -9016,7 +5318,7 @@ static bool ins_tab(void)
/// Handle CR or NL in insert mode.
///
/// @return false when it can't undo.
-static bool ins_eol(int c)
+bool ins_eol(int c)
{
if (echeck_abbr(c + ABBR_OFF)) {
return true;
@@ -9180,7 +5482,7 @@ static int ins_ctrl_ey(int tc)
{
int c = tc;
- if (ctrl_x_mode == CTRL_X_SCROLL) {
+ if (ctrl_x_mode_scroll()) {
if (c == Ctrl_Y) {
scrolldown_clamp();
} else {
@@ -9353,8 +5655,13 @@ static char_u *do_insert_char_pre(int c)
return res;
}
+bool can_cindent_get(void)
+{
+ return can_cindent;
+}
+
/// Trigger "event" and take care of fixing undo.
-static int ins_apply_autocmds(event_T event)
+int ins_apply_autocmds(event_T event)
{
varnumber_T tick = buf_get_changedtick(curbuf);
int r;
@@ -9370,21 +5677,3 @@ static int ins_apply_autocmds(event_T event)
return r;
}
-
-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;
-}
diff --git a/src/nvim/edit.h b/src/nvim/edit.h
index 894e23ee9f..eda6d8c9db 100644
--- a/src/nvim/edit.h
+++ b/src/nvim/edit.h
@@ -1,27 +1,9 @@
#ifndef NVIM_EDIT_H
#define NVIM_EDIT_H
+#include "nvim/autocmd.h"
#include "nvim/vim.h"
-/*
- * Array indexes used for cptext argument of ins_compl_add().
- */
-#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
-
-// 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;
-
typedef int (*IndentGetter)(void);
// Values for in_cinkeys()
diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c
index 80586caf8e..c58dbcd620 100644
--- a/src/nvim/eval/funcs.c
+++ b/src/nvim/eval/funcs.c
@@ -36,6 +36,7 @@
#include "nvim/indent.h"
#include "nvim/indent_c.h"
#include "nvim/input.h"
+#include "nvim/insexpand.h"
#include "nvim/lua/executor.h"
#include "nvim/macros.h"
#include "nvim/mapping.h"
@@ -1054,64 +1055,6 @@ static void f_col(typval_T *argvars, typval_T *rettv, FunPtr fptr)
get_col(argvars, rettv, false);
}
-/// "complete()" function
-static void f_complete(typval_T *argvars, typval_T *rettv, FunPtr fptr)
-{
- if ((State & MODE_INSERT) == 0) {
- emsg(_("E785: complete() can only be used in Insert mode"));
- return;
- }
-
- // Check for undo allowed here, because if something was already inserted
- // the line was already saved for undo and this check isn't done.
- if (!undo_allowed(curbuf)) {
- return;
- }
-
- if (argvars[1].v_type != VAR_LIST) {
- emsg(_(e_invarg));
- } else {
- const colnr_T startcol = tv_get_number_chk(&argvars[0], NULL);
- if (startcol > 0) {
- set_completion(startcol - 1, argvars[1].vval.v_list);
- }
- }
-}
-
-/// "complete_add()" function
-static void f_complete_add(typval_T *argvars, typval_T *rettv, FunPtr fptr)
-{
- rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, false);
-}
-
-/// "complete_check()" function
-static void f_complete_check(typval_T *argvars, typval_T *rettv, FunPtr fptr)
-{
- int saved = RedrawingDisabled;
-
- RedrawingDisabled = 0;
- ins_compl_check_keys(0, true);
- rettv->vval.v_number = compl_interrupted;
- RedrawingDisabled = saved;
-}
-
-/// "complete_info()" function
-static void f_complete_info(typval_T *argvars, typval_T *rettv, FunPtr fptr)
-{
- tv_dict_alloc_ret(rettv);
-
- list_T *what_list = NULL;
-
- if (argvars[0].v_type != VAR_UNKNOWN) {
- if (argvars[0].v_type != VAR_LIST) {
- emsg(_(e_listreq));
- return;
- }
- what_list = argvars[0].vval.v_list;
- }
- get_complete_info(what_list, rettv->vval.v_dict);
-}
-
/// "confirm(message, buttons[, default [, type]])" function
static void f_confirm(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
@@ -2048,6 +1991,12 @@ static void f_exepath(typval_T *argvars, typval_T *rettv, FunPtr fptr)
(void)os_can_exe(tv_get_string(&argvars[0]), &path, true);
+#ifdef BACKSLASH_IN_FILENAME
+ if (path != NULL) {
+ slash_adjust((char_u *)path);
+ }
+#endif
+
rettv->v_type = VAR_STRING;
rettv->vval.v_string = path;
}
diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c
index c2579944e4..d9102c07a5 100644
--- a/src/nvim/eval/userfunc.c
+++ b/src/nvim/eval/userfunc.c
@@ -16,6 +16,7 @@
#include "nvim/fileio.h"
#include "nvim/getchar.h"
#include "nvim/globals.h"
+#include "nvim/insexpand.h"
#include "nvim/lua/executor.h"
#include "nvim/os/input.h"
#include "nvim/regexp.h"
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c
index 28e1893b31..550fe1eb25 100644
--- a/src/nvim/ex_cmds.c
+++ b/src/nvim/ex_cmds.c
@@ -1583,22 +1583,39 @@ char *make_filter_cmd(char *cmd, char *itmp, char *otmp)
if (otmp != NULL) {
len += STRLEN(otmp) + STRLEN(p_srr) + 2; // two extra spaces (" "),
}
+
+ const char *const cmd_args = strchr(cmd, ' ');
+ len += (is_pwsh && cmd_args)
+ ? STRLEN(" -ArgumentList ") + 2 // two extra quotes
+ : 0;
+
char *const buf = xmalloc(len);
-#if defined(UNIX)
- // Put delimiters around the command (for concatenated commands) when
- // redirecting input and/or output.
if (is_pwsh) {
xstrlcpy(buf, "Start-Process ", len);
- xstrlcat(buf, cmd, len);
+ if (cmd_args == NULL) {
+ xstrlcat(buf, cmd, len);
+ } else {
+ xstrlcpy(buf + STRLEN(buf), cmd, (size_t)(cmd_args - cmd + 1));
+ xstrlcat(buf, " -ArgumentList \"", len);
+ xstrlcat(buf, cmd_args + 1, len); // +1 to skip the leading space.
+ xstrlcat(buf, "\"", len);
+ }
+#if defined(UNIX)
+ // Put delimiters around the command (for concatenated commands) when
+ // redirecting input and/or output.
} else if (itmp != NULL || otmp != NULL) {
char *fmt = is_fish_shell ? "begin; %s; end"
: "(%s)";
vim_snprintf(buf, len, fmt, cmd);
+#endif
+ // For shells that don't understand braces around commands, at least allow
+ // the use of commands in a pipe.
} else {
xstrlcpy(buf, cmd, len);
}
+#if defined(UNIX)
if (itmp != NULL) {
if (is_pwsh) {
xstrlcat(buf, " -RedirectStandardInput ", len - 1);
@@ -1608,14 +1625,6 @@ char *make_filter_cmd(char *cmd, char *itmp, char *otmp)
xstrlcat(buf, itmp, len - 1);
}
#else
- // For shells that don't understand braces around commands, at least allow
- // the use of commands in a pipe.
- if (is_pwsh) {
- xstrlcpy(buf, "Start-Process ", len);
- xstrlcat(buf, cmd, len);
- } else {
- xstrlcpy(buf, cmd, len);
- }
if (itmp != NULL) {
// If there is a pipe, we have to put the '<' in front of it.
// Don't do this when 'shellquote' is not empty, otherwise the
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c
index 6782465ef1..0fce7fe2c8 100644
--- a/src/nvim/fileio.c
+++ b/src/nvim/fileio.c
@@ -5296,6 +5296,9 @@ static void vim_mktempdir(void)
char user[40] = { 0 };
(void)os_get_username(user, sizeof(user));
+ // Usernames may contain slashes! #19240
+ memchrsub(user, '/', '_', sizeof(user));
+ memchrsub(user, '\\', '_', sizeof(user));
// Make sure the umask doesn't remove the executable bit.
// "repl" has been reported to use "0177".
diff --git a/src/nvim/generators/gen_api_dispatch.lua b/src/nvim/generators/gen_api_dispatch.lua
index 4cf282770d..b167767f7a 100644
--- a/src/nvim/generators/gen_api_dispatch.lua
+++ b/src/nvim/generators/gen_api_dispatch.lua
@@ -91,7 +91,7 @@ local deprecated_aliases = require("api.dispatch_deprecated")
for _,f in ipairs(shallowcopy(functions)) do
local ismethod = false
if startswith(f.name, "nvim_") then
- if startswith(f.name, "nvim__") then
+ if startswith(f.name, "nvim__") or f.name == "nvim_error_event" then
f.since = -1
elseif f.since == nil then
print("Function "..f.name.." lacks since field.\n")
@@ -149,7 +149,7 @@ local exported_attributes = {'name', 'return_type', 'method',
'since', 'deprecated_since'}
local exported_functions = {}
for _,f in ipairs(functions) do
- if not startswith(f.name, "nvim__") then
+ if not (startswith(f.name, "nvim__") or f.name == "nvim_error_event") then
local f_exported = {}
for _,attr in ipairs(exported_attributes) do
f_exported[attr] = f[attr]
diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c
index 00372d4f3d..2d10ad7ddb 100644
--- a/src/nvim/getchar.c
+++ b/src/nvim/getchar.c
@@ -21,6 +21,7 @@
#include "nvim/garray.h"
#include "nvim/getchar.h"
#include "nvim/input.h"
+#include "nvim/insexpand.h"
#include "nvim/keycodes.h"
#include "nvim/lua/executor.h"
#include "nvim/main.h"
diff --git a/src/nvim/globals.h b/src/nvim/globals.h
index 8d896aef31..e2667257b8 100644
--- a/src/nvim/globals.h
+++ b/src/nvim/globals.h
@@ -163,10 +163,6 @@ EXTERN colnr_T dollar_vcol INIT(= -1);
// by the match.)
EXTERN int compl_length INIT(= 0);
-// Set when character typed while looking for matches and it means we should
-// stop looking for matches.
-EXTERN int compl_interrupted INIT(= false);
-
// Set when doing something for completion that may call edit() recursively,
// which is not allowed. Also used to disable folding during completion
EXTERN bool compl_busy INIT(= false);
diff --git a/src/nvim/grid_defs.h b/src/nvim/grid_defs.h
index 1571340849..9252b8a371 100644
--- a/src/nvim/grid_defs.h
+++ b/src/nvim/grid_defs.h
@@ -128,4 +128,13 @@ typedef struct {
const char *start; ///< Location where region starts.
} StlClickRecord;
+typedef struct {
+ int args[3];
+ int icell;
+ int ncells;
+ int coloff;
+ int cur_attr;
+ int clear_width;
+} GridLineEvent;
+
#endif // NVIM_GRID_DEFS_H
diff --git a/src/nvim/highlight.c b/src/nvim/highlight.c
index 0f20eb1905..6226130322 100644
--- a/src/nvim/highlight.c
+++ b/src/nvim/highlight.c
@@ -183,10 +183,10 @@ int ns_get_hl(NS ns_id, int hl_id, bool link, bool nodefault)
bool valid_cache = it.version >= p->hl_valid;
if (!valid_cache && p->hl_def != LUA_NOREF && !recursive) {
- FIXED_TEMP_ARRAY(args, 3);
- args.items[0] = INTEGER_OBJ((Integer)ns_id);
- args.items[1] = STRING_OBJ(cstr_to_string((char *)syn_id2name(hl_id)));
- args.items[2] = BOOLEAN_OBJ(link);
+ MAXSIZE_TEMP_ARRAY(args, 3);
+ ADD_C(args, INTEGER_OBJ((Integer)ns_id));
+ ADD_C(args, STRING_OBJ(cstr_to_string((char *)syn_id2name(hl_id))));
+ ADD_C(args, BOOLEAN_OBJ(link));
// TODO(bfredl): preload the "global" attr dict?
Error err = ERROR_INIT;
@@ -719,93 +719,107 @@ Dictionary hl_get_attr_by_id(Integer attr_id, Boolean rgb, Error *err)
return dic;
}
- return hlattrs2dict(syn_attr2entry((int)attr_id), rgb);
+ return hlattrs2dict(NULL, syn_attr2entry((int)attr_id), rgb);
}
/// Converts an HlAttrs into Dictionary
///
+/// @param[out] hl optional pre-allocated dictionary for return value
+/// if present, must be allocated with at least 16 elements!
/// @param[in] aep data to convert
/// @param use_rgb use 'gui*' settings if true, else resorts to 'cterm*'
-Dictionary hlattrs2dict(HlAttrs ae, bool use_rgb)
+Dictionary hlattrs2dict(Dictionary *hl_alloc, HlAttrs ae, bool use_rgb)
{
- Dictionary hl = ARRAY_DICT_INIT;
int mask = use_rgb ? ae.rgb_ae_attr : ae.cterm_ae_attr;
+ Dictionary hl = ARRAY_DICT_INIT;
+ if (hl_alloc) {
+ hl = *hl_alloc;
+ } else {
+ kv_ensure_space(hl, 16);
+ }
if (mask & HL_BOLD) {
- PUT(hl, "bold", BOOLEAN_OBJ(true));
+ PUT_C(hl, "bold", BOOLEAN_OBJ(true));
}
if (mask & HL_STANDOUT) {
- PUT(hl, "standout", BOOLEAN_OBJ(true));
+ PUT_C(hl, "standout", BOOLEAN_OBJ(true));
}
if (mask & HL_UNDERLINE) {
- PUT(hl, "underline", BOOLEAN_OBJ(true));
+ PUT_C(hl, "underline", BOOLEAN_OBJ(true));
}
if (mask & HL_UNDERCURL) {
- PUT(hl, "undercurl", BOOLEAN_OBJ(true));
+ PUT_C(hl, "undercurl", BOOLEAN_OBJ(true));
}
if (mask & HL_UNDERDOUBLE) {
- PUT(hl, "underdouble", BOOLEAN_OBJ(true));
+ PUT_C(hl, "underdouble", BOOLEAN_OBJ(true));
}
if (mask & HL_UNDERDOTTED) {
- PUT(hl, "underdotted", BOOLEAN_OBJ(true));
+ PUT_C(hl, "underdotted", BOOLEAN_OBJ(true));
}
if (mask & HL_UNDERDASHED) {
- PUT(hl, "underdashed", BOOLEAN_OBJ(true));
+ PUT_C(hl, "underdashed", BOOLEAN_OBJ(true));
}
if (mask & HL_ITALIC) {
- PUT(hl, "italic", BOOLEAN_OBJ(true));
+ PUT_C(hl, "italic", BOOLEAN_OBJ(true));
}
if (mask & HL_INVERSE) {
- PUT(hl, "reverse", BOOLEAN_OBJ(true));
+ PUT_C(hl, "reverse", BOOLEAN_OBJ(true));
}
if (mask & HL_STRIKETHROUGH) {
- PUT(hl, "strikethrough", BOOLEAN_OBJ(true));
+ PUT_C(hl, "strikethrough", BOOLEAN_OBJ(true));
}
if (use_rgb) {
if (mask & HL_FG_INDEXED) {
- PUT(hl, "fg_indexed", BOOLEAN_OBJ(true));
+ PUT_C(hl, "fg_indexed", BOOLEAN_OBJ(true));
}
if (mask & HL_BG_INDEXED) {
- PUT(hl, "bg_indexed", BOOLEAN_OBJ(true));
+ PUT_C(hl, "bg_indexed", BOOLEAN_OBJ(true));
}
if (ae.rgb_fg_color != -1) {
- PUT(hl, "foreground", INTEGER_OBJ(ae.rgb_fg_color));
+ PUT_C(hl, "foreground", INTEGER_OBJ(ae.rgb_fg_color));
}
if (ae.rgb_bg_color != -1) {
- PUT(hl, "background", INTEGER_OBJ(ae.rgb_bg_color));
+ PUT_C(hl, "background", INTEGER_OBJ(ae.rgb_bg_color));
}
if (ae.rgb_sp_color != -1) {
- PUT(hl, "special", INTEGER_OBJ(ae.rgb_sp_color));
+ PUT_C(hl, "special", INTEGER_OBJ(ae.rgb_sp_color));
}
} else {
if (ae.cterm_fg_color != 0) {
- PUT(hl, "foreground", INTEGER_OBJ(ae.cterm_fg_color - 1));
+ PUT_C(hl, "foreground", INTEGER_OBJ(ae.cterm_fg_color - 1));
}
if (ae.cterm_bg_color != 0) {
- PUT(hl, "background", INTEGER_OBJ(ae.cterm_bg_color - 1));
+ PUT_C(hl, "background", INTEGER_OBJ(ae.cterm_bg_color - 1));
}
}
if (ae.hl_blend > -1) {
- PUT(hl, "blend", INTEGER_OBJ(ae.hl_blend));
+ PUT_C(hl, "blend", INTEGER_OBJ(ae.hl_blend));
}
- return hl;
+ if (hl_alloc) {
+ *hl_alloc = hl;
+ return hl;
+ } else {
+ Dictionary allocated = copy_dictionary(hl);
+ kv_destroy(hl);
+ return allocated;
+ }
}
HlAttrs dict2hlattrs(Dict(highlight) *dict, bool use_rgb, int *link_id, Error *err)
diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c
index d958b7b344..5027454222 100644
--- a/src/nvim/highlight_group.c
+++ b/src/nvim/highlight_group.c
@@ -1409,7 +1409,7 @@ Dictionary get_global_hl_defs(void)
Dictionary attrs = ARRAY_DICT_INIT;
HlGroup *h = &hl_table[i - 1];
if (h->sg_attr > 0) {
- attrs = hlattrs2dict(syn_attr2entry(h->sg_attr), true);
+ attrs = hlattrs2dict(NULL, syn_attr2entry(h->sg_attr), true);
} else if (h->sg_link > 0) {
const char *link = (const char *)hl_table[h->sg_link - 1].sg_name;
PUT(attrs, "link", STRING_OBJ(cstr_to_string(link)));
diff --git a/src/nvim/insexpand.c b/src/nvim/insexpand.c
new file mode 100644
index 0000000000..141759de81
--- /dev/null
+++ b/src/nvim/insexpand.c
@@ -0,0 +1,3886 @@
+// 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/undo.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,
+ &regmatch, 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.
+static 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.
+static 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();
+}
+
+/// "complete()" function
+void f_complete(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ if ((State & MODE_INSERT) == 0) {
+ emsg(_("E785: complete() can only be used in Insert mode"));
+ return;
+ }
+
+ // Check for undo allowed here, because if something was already inserted
+ // the line was already saved for undo and this check isn't done.
+ if (!undo_allowed(curbuf)) {
+ return;
+ }
+
+ if (argvars[1].v_type != VAR_LIST) {
+ emsg(_(e_invarg));
+ } else {
+ const colnr_T startcol = (colnr_T)tv_get_number_chk(&argvars[0], NULL);
+ if (startcol > 0) {
+ set_completion(startcol - 1, argvars[1].vval.v_list);
+ }
+ }
+}
+
+/// "complete_add()" function
+void f_complete_add(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0, false);
+}
+
+/// "complete_check()" function
+void f_complete_check(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ int saved = RedrawingDisabled;
+
+ RedrawingDisabled = 0;
+ ins_compl_check_keys(0, true);
+ rettv->vval.v_number = ins_compl_interrupted();
+ RedrawingDisabled = saved;
+}
+
+/// 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
+static 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))
+}
+
+/// "complete_info()" function
+void f_complete_info(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ tv_dict_alloc_ret(rettv);
+
+ list_T *what_list = NULL;
+
+ if (argvars[0].v_type != VAR_UNKNOWN) {
+ if (argvars[0].v_type != VAR_LIST) {
+ emsg(_(e_listreq));
+ return;
+ }
+ what_list = argvars[0].vval.v_list;
+ }
+ get_complete_info(what_list, rettv->vval.v_dict);
+}
+
+/// 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);
+ }
+}
diff --git a/src/nvim/insexpand.h b/src/nvim/insexpand.h
new file mode 100644
index 0000000000..def7f49078
--- /dev/null
+++ b/src/nvim/insexpand.h
@@ -0,0 +1,18 @@
+#ifndef NVIM_INSEXPAND_H
+#define NVIM_INSEXPAND_H
+
+#include "nvim/eval/funcs.h"
+#include "nvim/vim.h"
+
+/// state for pum_ext_select_item.
+EXTERN struct {
+ bool active;
+ int item;
+ bool insert;
+ bool finish;
+} pum_want;
+
+#ifdef INCLUDE_GENERATED_DECLARATIONS
+# include "insexpand.h.generated.h"
+#endif
+#endif // NVIM_INSEXPAND_H
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index ad03ebd1ed..fcbf36c711 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -416,9 +416,9 @@ static int nlua_wait(lua_State *lstate)
LOOP_PROCESS_EVENTS_UNTIL(&main_loop,
loop_events,
(int)timeout,
- is_function ? nlua_wait_condition(lstate,
- &pcall_status,
- &callback_result) : false || got_int);
+ got_int || (is_function ? nlua_wait_condition(lstate,
+ &pcall_status,
+ &callback_result) : false));
// Stop dummy timer
time_watcher_stop(tw);
diff --git a/src/nvim/main.c b/src/nvim/main.c
index b06b9630e2..494ff0b4af 100644
--- a/src/nvim/main.c
+++ b/src/nvim/main.c
@@ -28,6 +28,7 @@
#include "nvim/highlight_group.h"
#include "nvim/iconv.h"
#include "nvim/if_cscope.h"
+#include "nvim/insexpand.h"
#include "nvim/lua/executor.h"
#include "nvim/main.h"
#include "nvim/mapping.h"
@@ -202,7 +203,6 @@ void early_init(mparm_T *paramp)
set_lang_var(); // set v:lang and v:ctype
init_signs();
- ui_comp_syn_init();
}
#ifdef MAKE_LIB
@@ -320,6 +320,7 @@ int main(int argc, char **argv)
no_wait_return = true;
init_highlight(true, false); // Default highlight groups.
+ ui_comp_syn_init();
TIME_MSG("init highlight");
// Set the break level after the terminal is initialized.
diff --git a/src/nvim/memline.c b/src/nvim/memline.c
index 5f74440747..1ea5e2ccdc 100644
--- a/src/nvim/memline.c
+++ b/src/nvim/memline.c
@@ -309,7 +309,7 @@ int ml_open(buf_T *buf)
if (!buf->b_spell) {
b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0;
- b0p->b0_flags = get_fileformat(buf) + 1;
+ b0p->b0_flags = (uint8_t)(get_fileformat(buf) + 1);
set_b0_fname(b0p, buf);
(void)os_get_username((char *)b0p->b0_uname, B0_UNAME_SIZE);
b0p->b0_uname[B0_UNAME_SIZE - 1] = NUL;
@@ -359,7 +359,7 @@ int ml_open(buf_T *buf)
dp = hp->bh_data;
dp->db_index[0] = --dp->db_txt_start; // at end of block
- dp->db_free -= 1 + INDEX_SIZE;
+ dp->db_free -= 1 + (unsigned)INDEX_SIZE;
dp->db_line_count = 1;
*((char_u *)dp + dp->db_txt_start) = NUL; // empty line
@@ -711,7 +711,7 @@ static void set_b0_dir_flag(ZERO_BL *b0p, buf_T *buf)
if (same_directory(buf->b_ml.ml_mfp->mf_fname, (char_u *)buf->b_ffname)) {
b0p->b0_flags |= B0_SAME_DIR;
} else {
- b0p->b0_flags &= ~B0_SAME_DIR;
+ b0p->b0_flags &= (uint8_t) ~B0_SAME_DIR;
}
}
@@ -723,7 +723,7 @@ static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf)
n = (int)STRLEN(buf->b_p_fenc);
if ((int)STRLEN(b0p->b0_fname) + n + 1 > size) {
- b0p->b0_flags &= ~B0_HAS_FENC;
+ b0p->b0_flags &= (uint8_t) ~B0_HAS_FENC;
} else {
memmove((char *)b0p->b0_fname + size - n,
(char *)buf->b_p_fenc, (size_t)n);
@@ -750,7 +750,6 @@ void ml_recover(bool checkext)
DATA_BL *dp;
infoptr_T *ip;
blocknr_T bnum;
- int page_count;
int len;
bool directly;
linenr_T lnum;
@@ -971,7 +970,7 @@ void ml_recover(bool checkext)
int fnsize = B0_FNAME_SIZE_NOCRYPT;
for (p = b0p->b0_fname + fnsize; p > b0p->b0_fname && p[-1] != NUL; p--) {}
- b0_fenc = vim_strnsave(p, b0p->b0_fname + fnsize - p);
+ b0_fenc = vim_strnsave(p, (size_t)(b0p->b0_fname + fnsize - p));
}
mf_put(mfp, hp, false, false); // release block 0
@@ -1004,11 +1003,11 @@ void ml_recover(bool checkext)
}
unchanged(curbuf, true, true);
- bnum = 1; // start with block 1
- page_count = 1; // which is 1 page
- lnum = 0; // append after line 0 in curbuf
+ bnum = 1; // start with block 1
+ unsigned page_count = 1; // which is 1 page
+ lnum = 0; // append after line 0 in curbuf
line_count = 0;
- idx = 0; // start with first index in block 1
+ idx = 0; // start with first index in block 1
error = 0;
buf->b_ml.ml_stack_top = 0; // -V1048
buf->b_ml.ml_stack = NULL;
@@ -1091,7 +1090,7 @@ void ml_recover(bool checkext)
bnum = pp->pb_pointer[idx].pe_bnum;
line_count = pp->pb_pointer[idx].pe_line_count;
- page_count = pp->pb_pointer[idx].pe_page_count;
+ page_count = (unsigned)pp->pb_pointer[idx].pe_page_count;
idx = 0;
continue;
}
@@ -1518,7 +1517,7 @@ static time_t swapfile_info(char_u *fname)
if (os_fileinfo((char *)fname, &file_info)) {
#ifdef UNIX
// print name of owner of the file
- if (os_get_uname(file_info.stat.st_uid, uname, B0_UNAME_SIZE) == OK) {
+ if (os_get_uname((uv_uid_t)file_info.stat.st_uid, uname, B0_UNAME_SIZE) == OK) {
msg_puts(_(" owned by: "));
msg_outtrans(uname);
msg_puts(_(" dated: "));
@@ -1970,12 +1969,9 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
int line_count; // number of indexes in current block
int offset;
int from, to;
- int space_needed; // space needed for new line
- int page_size;
int page_count;
int db_idx; // index for lnum in data block
bhdr_T *hp;
- memfile_T *mfp;
DATA_BL *dp;
PTR_BL *pp;
infoptr_T *ip;
@@ -1992,10 +1988,10 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
if (len == 0) {
len = (colnr_T)STRLEN(line) + 1; // space needed for the text
}
- space_needed = len + INDEX_SIZE; // space needed for text + index
+ int space_needed = len + (int)INDEX_SIZE; // space needed for text + index
- mfp = buf->b_ml.ml_mfp;
- page_size = mfp->mf_page_size;
+ memfile_T *mfp = buf->b_ml.ml_mfp;
+ int page_size = (int)mfp->mf_page_size;
/*
* find the data block containing the previous line
@@ -2053,9 +2049,9 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
/*
* Insert new line in existing data block, or in data block allocated above.
*/
- dp->db_txt_start -= len;
- dp->db_free -= space_needed;
- ++(dp->db_line_count);
+ dp->db_txt_start -= (unsigned)len;
+ dp->db_free -= (unsigned)space_needed;
+ dp->db_line_count++;
/*
* move the text of the lines that follow to the front
@@ -2067,17 +2063,17 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
* This will become the character just after the new line.
*/
if (db_idx < 0) {
- offset = dp->db_txt_end;
+ offset = (int)dp->db_txt_end;
} else {
offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK);
}
memmove((char *)dp + dp->db_txt_start,
(char *)dp + dp->db_txt_start + len,
- (size_t)(offset - (dp->db_txt_start + len)));
- for (i = line_count - 1; i > db_idx; --i) {
- dp->db_index[i + 1] = dp->db_index[i] - len;
+ (size_t)offset - (dp->db_txt_start + (size_t)len));
+ for (i = line_count - 1; i > db_idx; i--) {
+ dp->db_index[i + 1] = dp->db_index[i] - (unsigned)len;
}
- dp->db_index[db_idx + 1] = offset - len;
+ dp->db_index[db_idx + 1] = (unsigned)(offset - len);
} else { // add line at the end
dp->db_index[db_idx + 1] = dp->db_txt_start;
}
@@ -2107,7 +2103,7 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
* The line counts in the pointer blocks have already been adjusted by
* ml_find_line().
*/
- long line_count_left, line_count_right;
+ int line_count_left, line_count_right;
int page_count_left, page_count_right;
bhdr_T *hp_left;
bhdr_T *hp_right;
@@ -2142,9 +2138,9 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
in_left = false; // put new line in right block
// space_needed does not change
} else {
- data_moved = ((dp->db_index[db_idx]) & DB_INDEX_MASK) -
- dp->db_txt_start;
- total_moved = data_moved + lines_moved * INDEX_SIZE;
+ data_moved = (int)(((dp->db_index[db_idx]) & DB_INDEX_MASK) -
+ dp->db_txt_start);
+ total_moved = data_moved + lines_moved * (int)INDEX_SIZE;
if ((int)dp->db_free + total_moved >= space_needed) {
in_left = true; // put new line in left block
space_needed = total_moved;
@@ -2155,7 +2151,7 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
}
}
- page_count = ((space_needed + HEADER_SIZE) + page_size - 1) / page_size;
+ page_count = ((space_needed + (int)HEADER_SIZE) + page_size - 1) / page_size;
hp_new = ml_new_data(mfp, newfile, page_count);
if (db_idx < 0) { // left block is new
hp_left = hp_new;
@@ -2172,15 +2168,15 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
dp_left = hp_left->bh_data;
bnum_left = hp_left->bh_bnum;
bnum_right = hp_right->bh_bnum;
- page_count_left = hp_left->bh_page_count;
- page_count_right = hp_right->bh_page_count;
+ page_count_left = (int)hp_left->bh_page_count;
+ page_count_right = (int)hp_right->bh_page_count;
/*
* May move the new line into the right/new block.
*/
if (!in_left) {
- dp_right->db_txt_start -= len;
- dp_right->db_free -= len + INDEX_SIZE;
+ dp_right->db_txt_start -= (unsigned)len;
+ dp_right->db_free -= (unsigned)len + (unsigned)INDEX_SIZE;
dp_right->db_index[0] = dp_right->db_txt_start;
if (mark) {
dp_right->db_index[0] |= DB_MARKED;
@@ -2196,21 +2192,21 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
if (lines_moved) {
/*
*/
- dp_right->db_txt_start -= data_moved;
- dp_right->db_free -= total_moved;
+ dp_right->db_txt_start -= (unsigned)data_moved;
+ dp_right->db_free -= (unsigned)total_moved;
memmove((char *)dp_right + dp_right->db_txt_start,
(char *)dp_left + dp_left->db_txt_start,
(size_t)data_moved);
- offset = dp_right->db_txt_start - dp_left->db_txt_start;
- dp_left->db_txt_start += data_moved;
- dp_left->db_free += total_moved;
+ offset = (int)(dp_right->db_txt_start - dp_left->db_txt_start);
+ dp_left->db_txt_start += (unsigned)data_moved;
+ dp_left->db_free += (unsigned)total_moved;
/*
* update indexes in the new block
*/
for (to = line_count_right, from = db_idx + 1;
- from < line_count_left; ++from, ++to) {
- dp_right->db_index[to] = dp->db_index[from] + offset;
+ from < line_count_left; from++, to++) {
+ dp_right->db_index[to] = dp->db_index[from] + (unsigned)offset;
}
line_count_right += lines_moved;
line_count_left -= lines_moved;
@@ -2220,8 +2216,8 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
* May move the new line into the left (old or new) block.
*/
if (in_left) {
- dp_left->db_txt_start -= len;
- dp_left->db_free -= len + INDEX_SIZE;
+ dp_left->db_txt_start -= (unsigned)len;
+ dp_left->db_free -= (unsigned)len + (unsigned)INDEX_SIZE;
dp_left->db_index[line_count_left] = dp_left->db_txt_start;
if (mark) {
dp_left->db_index[line_count_left] |= DB_MARKED;
@@ -2369,8 +2365,8 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b
memmove(&pp_new->pb_pointer[0],
&pp->pb_pointer[pb_idx + 1],
(size_t)(total_moved) * sizeof(PTR_EN));
- pp_new->pb_count = total_moved;
- pp->pb_count -= total_moved - 1;
+ pp_new->pb_count = (uint16_t)total_moved;
+ pp->pb_count = (uint16_t)(pp->pb_count - (total_moved - 1));
pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right;
pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right;
pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right;
@@ -2436,12 +2432,12 @@ void ml_add_deleted_len_buf(buf_T *buf, char_u *ptr, ssize_t len)
return;
}
if (len == -1) {
- len = STRLEN(ptr);
+ len = (ssize_t)STRLEN(ptr);
}
- curbuf->deleted_bytes += len + 1;
- curbuf->deleted_bytes2 += len + 1;
+ curbuf->deleted_bytes += (size_t)len + 1;
+ curbuf->deleted_bytes2 += (size_t)len + 1;
if (curbuf->update_need_codepoints) {
- mb_utflen(ptr, len, &curbuf->deleted_codepoints,
+ mb_utflen(ptr, (size_t)len, &curbuf->deleted_codepoints,
&curbuf->deleted_codeunits);
curbuf->deleted_codepoints++; // NL char
curbuf->deleted_codeunits++;
@@ -2525,7 +2521,6 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message)
int count; // number of entries in block
int idx;
int stack_idx;
- int text_start;
int line_start;
long line_size;
int i;
@@ -2568,17 +2563,16 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message)
dp = hp->bh_data;
// compute line count before the delete
- count = (long)(buf->b_ml.ml_locked_high)
- - (long)(buf->b_ml.ml_locked_low) + 2;
+ count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 2;
idx = lnum - buf->b_ml.ml_locked_low;
--buf->b_ml.ml_line_count;
line_start = ((dp->db_index[idx]) & DB_INDEX_MASK);
if (idx == 0) { // first line in block, text at the end
- line_size = dp->db_txt_end - line_start;
+ line_size = dp->db_txt_end - (unsigned)line_start;
} else {
- line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start;
+ line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - (unsigned)line_start;
}
// Line should always have an NL char internally (represented as NUL),
@@ -2636,24 +2630,20 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message)
}
CHECK(stack_idx < 0, _("deleted block 1?"));
} else {
- /*
- * delete the text by moving the next lines forwards
- */
- text_start = dp->db_txt_start;
+ // delete the text by moving the next lines forwards
+ int text_start = (int)dp->db_txt_start;
memmove((char *)dp + text_start + line_size,
(char *)dp + text_start, (size_t)(line_start - text_start));
- /*
- * delete the index by moving the next indexes backwards
- * Adjust the indexes for the text movement.
- */
- for (i = idx; i < count - 1; ++i) {
- dp->db_index[i] = dp->db_index[i + 1] + line_size;
+ // delete the index by moving the next indexes backwards
+ // Adjust the indexes for the text movement.
+ for (i = idx; i < count - 1; i++) {
+ dp->db_index[i] = dp->db_index[i + 1] + (unsigned)line_size;
}
- dp->db_free += line_size + INDEX_SIZE;
- dp->db_txt_start += line_size;
- --(dp->db_line_count);
+ dp->db_free += (unsigned)line_size + (unsigned)INDEX_SIZE;
+ dp->db_txt_start += (unsigned)line_size;
+ dp->db_line_count--;
/*
* mark the block dirty and make sure it is in the file (for recovery)
@@ -2823,9 +2813,9 @@ static void ml_flush_line(buf_T *buf)
start = ((dp->db_index[idx]) & DB_INDEX_MASK);
old_line = (char_u *)dp + start;
if (idx == 0) { // line is last in block
- old_len = dp->db_txt_end - start;
+ old_len = (int)dp->db_txt_end - start;
} else { // text of previous line follows
- old_len = (dp->db_index[idx - 1] & DB_INDEX_MASK) - start;
+ old_len = (int)(dp->db_index[idx - 1] & DB_INDEX_MASK) - start;
}
new_len = (colnr_T)STRLEN(new_line) + 1;
extra = new_len - old_len; // negative if lines gets smaller
@@ -2840,18 +2830,18 @@ static void ml_flush_line(buf_T *buf)
// move text of following lines
memmove((char *)dp + dp->db_txt_start - extra,
(char *)dp + dp->db_txt_start,
- (size_t)(start - dp->db_txt_start));
+ (size_t)(start - (int)dp->db_txt_start));
// adjust pointers of this and following lines
- for (i = idx + 1; i < count; ++i) {
- dp->db_index[i] -= extra;
+ for (i = idx + 1; i < count; i++) {
+ dp->db_index[i] -= (unsigned)extra;
}
}
- dp->db_index[idx] -= extra;
+ dp->db_index[idx] -= (unsigned)extra;
// adjust free space
- dp->db_free -= extra;
- dp->db_txt_start -= extra;
+ dp->db_free -= (unsigned)extra;
+ dp->db_txt_start -= (unsigned)extra;
// copy new line into the data block
memmove(old_line - extra, new_line, (size_t)new_len);
@@ -2866,7 +2856,7 @@ static void ml_flush_line(buf_T *buf)
// Don't forget to copy the mark!
// How about handling errors???
(void)ml_append_int(buf, lnum, new_line, new_len, false,
- (dp->db_index[idx] & DB_MARKED));
+ (int)(dp->db_index[idx] & DB_MARKED));
(void)ml_delete_int(buf, lnum, false);
}
}
@@ -2886,8 +2876,8 @@ static bhdr_T *ml_new_data(memfile_T *mfp, bool negative, int page_count)
bhdr_T *hp = mf_new(mfp, negative, (unsigned)page_count);
DATA_BL *dp = hp->bh_data;
dp->db_id = DATA_ID;
- dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size;
- dp->db_free = dp->db_txt_start - HEADER_SIZE;
+ dp->db_txt_start = dp->db_txt_end = (unsigned)page_count * mfp->mf_page_size;
+ dp->db_free = dp->db_txt_start - (unsigned)HEADER_SIZE;
dp->db_line_count = 0;
return hp;
@@ -2900,7 +2890,7 @@ static bhdr_T *ml_new_ptr(memfile_T *mfp)
PTR_BL *pp = hp->bh_data;
pp->pb_id = PTR_ID;
pp->pb_count = 0;
- pp->pb_count_max = (mfp->mf_page_size - sizeof(PTR_BL)) / sizeof(PTR_EN) + 1;
+ pp->pb_count_max = (uint16_t)((mfp->mf_page_size - sizeof(PTR_BL)) / sizeof(PTR_EN) + 1);
return hp;
}
@@ -3000,7 +2990,7 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action)
* search downwards in the tree until a data block is found
*/
for (;;) {
- if ((hp = mf_get(mfp, bnum, page_count)) == NULL) {
+ if ((hp = mf_get(mfp, bnum, (unsigned)page_count)) == NULL) {
goto error_noblock;
}
@@ -3110,7 +3100,7 @@ static int ml_add_stack(buf_T *buf)
CHECK(top > 0, _("Stack size increases")); // more than 5 levels???
buf->b_ml.ml_stack_size += STACK_INCR;
- size_t new_size = sizeof(infoptr_T) * buf->b_ml.ml_stack_size;
+ size_t new_size = sizeof(infoptr_T) * (size_t)buf->b_ml.ml_stack_size;
buf->b_ml.ml_stack = xrealloc(buf->b_ml.ml_stack, new_size);
}
@@ -3179,7 +3169,7 @@ int resolve_symlink(const char_u *fname, char_u *buf)
return FAIL;
}
- ret = readlink((char *)tmp, (char *)buf, MAXPATHL - 1);
+ ret = (int)readlink((char *)tmp, (char *)buf, MAXPATHL - 1);
if (ret <= 0) {
if (errno == EINVAL || errno == ENOENT) {
// Found non-symlink or not existing file, stop here.
@@ -3294,9 +3284,9 @@ char_u *get_file_in_dir(char_u *fname, char_u *dname)
} else {
save_char = *tail;
*tail = NUL;
- t = (char_u *)concat_fnames((char *)fname, (char *)dname + 2, TRUE);
- *tail = save_char;
- retval = (char_u *)concat_fnames((char *)t, (char *)tail, TRUE);
+ t = (char_u *)concat_fnames((char *)fname, (char *)dname + 2, true);
+ *tail = (uint8_t)save_char;
+ retval = (char_u *)concat_fnames((char *)t, (char *)tail, true);
xfree(t);
}
} else {
@@ -3798,8 +3788,7 @@ void ml_setflags(buf_T *buf)
if (hp->bh_bnum == 0) {
b0p = hp->bh_data;
b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0;
- b0p->b0_flags = (b0p->b0_flags & ~B0_FF_MASK)
- | (get_fileformat(buf) + 1);
+ b0p->b0_flags = (uint8_t)((b0p->b0_flags & ~B0_FF_MASK) | (uint8_t)(get_fileformat(buf) + 1));
add_b0_fenc(b0p, buf);
hp->bh_flags |= BH_DIRTY;
mf_sync(buf->b_ml.ml_mfp, MFS_ZERO);
@@ -3887,7 +3876,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
if (buf->b_ml.ml_usedchunks + 1 >= buf->b_ml.ml_numchunks) {
buf->b_ml.ml_numchunks = buf->b_ml.ml_numchunks * 3 / 2;
buf->b_ml.ml_chunksize = xrealloc(buf->b_ml.ml_chunksize,
- sizeof(chunksize_T) * buf->b_ml.ml_numchunks);
+ sizeof(chunksize_T) * (size_t)buf->b_ml.ml_numchunks);
}
if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MAXL) {
@@ -3898,8 +3887,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
memmove(buf->b_ml.ml_chunksize + curix + 1,
buf->b_ml.ml_chunksize + curix,
- (buf->b_ml.ml_usedchunks - curix) *
- sizeof(chunksize_T));
+ (size_t)(buf->b_ml.ml_usedchunks - curix) * sizeof(chunksize_T));
// Compute length of first half of lines in the split chunk
size = 0;
linecnt = 0;
@@ -3910,12 +3898,11 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
return;
}
dp = hp->bh_data;
- count = (long)(buf->b_ml.ml_locked_high) -
- (long)(buf->b_ml.ml_locked_low) + 1;
+ count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1;
idx = curline - buf->b_ml.ml_locked_low;
curline = buf->b_ml.ml_locked_high + 1;
if (idx == 0) { // first line in block, text at the end
- text_end = dp->db_txt_end;
+ text_end = (int)dp->db_txt_end;
} else {
text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
}
@@ -3928,7 +3915,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
idx = count - 1;
linecnt += rest;
}
- size += text_end - ((dp->db_index[idx]) & DB_INDEX_MASK);
+ size += text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK);
}
buf->b_ml.ml_chunksize[curix].mlcs_numlines = linecnt;
buf->b_ml.ml_chunksize[curix + 1].mlcs_numlines -= linecnt;
@@ -3959,11 +3946,10 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
}
dp = hp->bh_data;
if (dp->db_line_count == 1) {
- rest = dp->db_txt_end - dp->db_txt_start;
+ rest = (int)(dp->db_txt_end - dp->db_txt_start);
} else {
- rest =
- ((dp->db_index[dp->db_line_count - 2]) & DB_INDEX_MASK)
- - dp->db_txt_start;
+ rest = (int)((dp->db_index[dp->db_line_count - 2]) & DB_INDEX_MASK)
+ - (int)dp->db_txt_start;
}
curchnk->mlcs_totalsize = rest;
curchnk->mlcs_numlines = 1;
@@ -3982,7 +3968,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
} else if (curix == 0 && curchnk->mlcs_numlines <= 0) {
buf->b_ml.ml_usedchunks--;
memmove(buf->b_ml.ml_chunksize, buf->b_ml.ml_chunksize + 1,
- buf->b_ml.ml_usedchunks * sizeof(chunksize_T));
+ (size_t)buf->b_ml.ml_usedchunks * sizeof(chunksize_T));
return;
} else if (curix == 0 || (curchnk->mlcs_numlines > 10
&& (curchnk->mlcs_numlines +
@@ -3998,8 +3984,7 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
if (curix < buf->b_ml.ml_usedchunks) {
memmove(buf->b_ml.ml_chunksize + curix,
buf->b_ml.ml_chunksize + curix + 1,
- (buf->b_ml.ml_usedchunks - curix) *
- sizeof(chunksize_T));
+ (size_t)(buf->b_ml.ml_usedchunks - curix) * sizeof(chunksize_T));
}
return;
}
@@ -4049,7 +4034,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
if (lnum == 0 || buf->b_ml.ml_line_lnum < lnum || !no_ff) {
ml_flush_line(curbuf);
} else if (can_cache && buf->b_ml.ml_line_offset > 0) {
- return buf->b_ml.ml_line_offset;
+ return (long)buf->b_ml.ml_line_offset;
}
if (buf->b_ml.ml_usedchunks == -1
@@ -4071,7 +4056,8 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
* special because it will never qualify
*/
curline = 1;
- curix = size = 0;
+ curix = 0;
+ size = 0;
while (curix < buf->b_ml.ml_usedchunks - 1
&& ((lnum != 0
&& lnum >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines)
@@ -4093,11 +4079,10 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
return -1;
}
dp = hp->bh_data;
- count = (long)(buf->b_ml.ml_locked_high) -
- (long)(buf->b_ml.ml_locked_low) + 1;
+ count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1;
start_idx = idx = curline - buf->b_ml.ml_locked_low;
if (idx == 0) { // first line in block, text at the end
- text_end = dp->db_txt_end;
+ text_end = (int)dp->db_txt_end;
} else {
text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
}
@@ -4123,7 +4108,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
idx++;
}
}
- len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK);
+ len = text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK);
size += len;
if (offset != 0 && size >= offset) {
if (size + ffdos == offset) {
@@ -4132,7 +4117,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
*offp = offset - size + len;
} else {
*offp = offset - size + len
- - (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK));
+ - (text_end - (int)((dp->db_index[idx - 1]) & DB_INDEX_MASK));
}
curline += idx - start_idx + extra;
if (curline > buf->b_ml.ml_line_count) {
@@ -4158,7 +4143,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
}
if (can_cache && size > 0) {
- buf->b_ml.ml_line_offset = size;
+ buf->b_ml.ml_line_offset = (size_t)size;
}
return size;
@@ -4168,14 +4153,13 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
void goto_byte(long cnt)
{
long boff = cnt;
- linenr_T lnum;
ml_flush_line(curbuf); // cached line may be dirty
setpcmark();
if (boff) {
boff--;
}
- lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff, false);
+ linenr_T lnum = (linenr_T)ml_find_line_or_offset(curbuf, (linenr_T)0, &boff, false);
if (lnum < 1) { // past the end
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
curwin->w_curswant = MAXCOL;
diff --git a/src/nvim/memory.c b/src/nvim/memory.c
index 4d5cf047f9..a74fcf6137 100644
--- a/src/nvim/memory.c
+++ b/src/nvim/memory.c
@@ -14,6 +14,7 @@
#include "nvim/eval.h"
#include "nvim/highlight.h"
#include "nvim/highlight_group.h"
+#include "nvim/insexpand.h"
#include "nvim/lua/executor.h"
#include "nvim/mapping.h"
#include "nvim/memfile.h"
@@ -710,6 +711,7 @@ void free_all_mem(void)
free_search_patterns();
free_old_sub();
free_last_insert();
+ free_insexpand_stuff();
free_prev_shellcmd();
free_regexp_stuff();
free_tag_stuff();
diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c
index de01443313..1ca68979f4 100644
--- a/src/nvim/msgpack_rpc/channel.c
+++ b/src/nvim/msgpack_rpc/channel.c
@@ -239,7 +239,14 @@ static void parse_msgpack(Channel *channel)
{
Unpacker *p = channel->rpc.unpacker;
while (unpacker_advance(p)) {
- if (p->type == kMessageTypeResponse) {
+ if (p->type == kMessageTypeRedrawEvent) {
+ if (p->grid_line_event) {
+ ui_client_event_raw_line(p->grid_line_event);
+ } else if (p->ui_handler.fn != NULL && p->result.type == kObjectTypeArray) {
+ p->ui_handler.fn(p->result.data.array);
+ arena_mem_free(arena_finish(&p->arena), &p->reuse_blk);
+ }
+ } else if (p->type == kMessageTypeResponse) {
ChannelCallFrame *frame = kv_last(channel->rpc.call_stack);
if (p->request_id != frame->request_id) {
char buf[256];
diff --git a/src/nvim/msgpack_rpc/unpacker.c b/src/nvim/msgpack_rpc/unpacker.c
index 26c1843026..d73557ec11 100644
--- a/src/nvim/msgpack_rpc/unpacker.c
+++ b/src/nvim/msgpack_rpc/unpacker.c
@@ -6,6 +6,7 @@
#include "nvim/memory.h"
#include "nvim/msgpack_rpc/helpers.h"
#include "nvim/msgpack_rpc/unpacker.h"
+#include "nvim/ui_client.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "msgpack_rpc/unpacker.c.generated.h"
@@ -280,10 +281,20 @@ error:
// is relatively small, just ~10 bytes + the method name. Thus we can simply refuse
// to advance the stream beyond the header until it can be parsed in its entirety.
//
-// Of course, later on, we want to specialize state 2 into sub-states depending
+// Later on, we want to specialize state 2 into more sub-states depending
// on the specific method. "nvim_exec_lua" should just decode direct into lua
-// objects, and "redraw/grid_line" should use a hand-rolled decoder to avoid
-// a blizzard of small objects for each screen cell.
+// objects. For the moment "redraw/grid_line" uses a hand-rolled decoder,
+// to avoid a blizzard of small objects for each screen cell.
+//
+// <0>[2, "redraw", <10>[{11}["method", <12>[args], <12>[args], ...], <11>[...], ...]]
+//
+// Where [args] gets unpacked as an Array. Note: first {11} is not saved as a state.
+//
+// When method is "grid_line", we furthermore decode a cell at a time like:
+//
+// <0>[2, "redraw", <10>[{11}["grid_line", <13>[g, r, c, [<14>[cell], <14>[cell], ...]], ...], <11>[...], ...]]
+//
+// where [cell] is [char, repeat, attr], where 'repeat' and 'attr' is optional
bool unpacker_advance(Unpacker *p)
{
@@ -292,8 +303,27 @@ bool unpacker_advance(Unpacker *p)
if (!unpacker_parse_header(p)) {
return false;
}
- p->state = p->type == kMessageTypeResponse ? 1 : 2;
- arena_start(&p->arena, &p->reuse_blk);
+ if (p->type == kMessageTypeNotification && p->handler.fn == handle_ui_client_redraw) {
+ p->type = kMessageTypeRedrawEvent;
+ p->state = 10;
+ } else {
+ p->state = p->type == kMessageTypeResponse ? 1 : 2;
+ arena_start(&p->arena, &p->reuse_blk);
+ }
+ }
+
+ if (p->state >= 10 && p->state != 12) {
+ if (!unpacker_parse_redraw(p)) {
+ return false;
+ }
+
+ if (p->state == 14) {
+ // grid_line event already unpacked
+ goto done;
+ } else {
+ // unpack other ui events using mpack_parse()
+ arena_start(&p->arena, &p->reuse_blk);
+ }
}
int result;
@@ -310,13 +340,173 @@ rerun:
return false;
}
- if (p->state == 1) {
+done:
+ switch (p->state) {
+ case 1:
p->error = p->result;
p->state = 2;
goto rerun;
- } else {
- assert(p->state == 2);
+ case 2:
p->state = 0;
+ return true;
+ case 12:
+ case 14:
+ p->ncalls--;
+ if (p->ncalls > 0) {
+ p->state = (p->state == 14) ? 13 : 12;
+ } else if (p->nevents > 0) {
+ p->state = 11;
+ } else {
+ p->state = 0;
+ }
+ return true;
+ default:
+ abort();
+ }
+}
+
+bool unpacker_parse_redraw(Unpacker *p)
+{
+ mpack_token_t tok;
+ int result;
+
+ const char *data = p->read_ptr;
+ size_t size = p->read_size;
+ GridLineEvent *g = p->grid_line_event;
+
+#define NEXT_TYPE(tok, typ) \
+ result = mpack_rtoken(&data, &size, &tok); \
+ if (result == MPACK_EOF) { \
+ return false; \
+ } else if (result || (tok.type != typ \
+ && !(typ == MPACK_TOKEN_STR && tok.type == MPACK_TOKEN_BIN) \
+ && !(typ == MPACK_TOKEN_SINT && tok.type == MPACK_TOKEN_UINT))) { \
+ p->state = -1; \
+ return false; \
+ }
+
+redo:
+ switch (p->state) {
+ case 10:
+ NEXT_TYPE(tok, MPACK_TOKEN_ARRAY);
+ p->nevents = (int)tok.length;
+ FALLTHROUGH;
+
+ case 11:
+ NEXT_TYPE(tok, MPACK_TOKEN_ARRAY);
+ p->ncalls = (int)tok.length;
+
+ if (p->ncalls-- == 0) {
+ p->state = -1;
+ return false;
+ }
+
+ NEXT_TYPE(tok, MPACK_TOKEN_STR);
+ if (tok.length > size) {
+ return false;
+ }
+
+ p->ui_handler = ui_client_get_redraw_handler(data, tok.length, NULL);
+ data += tok.length;
+ size -= tok.length;
+
+ p->nevents--;
+ p->read_ptr = data;
+ p->read_size = size;
+ if (p->ui_handler.fn != ui_client_event_grid_line) {
+ p->state = 12;
+ if (p->grid_line_event) {
+ arena_mem_free(arena_finish(&p->arena), &p->reuse_blk);
+ p->grid_line_event = NULL;
+ }
+ return true;
+ } else {
+ p->state = 13;
+ arena_start(&p->arena, &p->reuse_blk);
+ p->grid_line_event = arena_alloc(&p->arena, sizeof *p->grid_line_event, true);
+ g = p->grid_line_event;
+ FALLTHROUGH;
+ }
+
+ case 13:
+ NEXT_TYPE(tok, MPACK_TOKEN_ARRAY);
+ int eventarrsize = (int)tok.length;
+ if (eventarrsize != 4) {
+ p->state = -1;
+ return false;
+ }
+
+ for (int i = 0; i < 3; i++) {
+ NEXT_TYPE(tok, MPACK_TOKEN_UINT);
+ g->args[i] = (int)tok.data.value.lo;
+ }
+
+ NEXT_TYPE(tok, MPACK_TOKEN_ARRAY);
+ g->ncells = (int)tok.length;
+ g->icell = 0;
+ g->coloff = 0;
+ g->cur_attr = -1;
+
+ p->read_ptr = data;
+ p->read_size = size;
+ p->state = 14;
+ FALLTHROUGH;
+
+ case 14:
+ assert(g->icell < g->ncells);
+
+ NEXT_TYPE(tok, MPACK_TOKEN_ARRAY);
+ int cellarrsize = (int)tok.length;
+ if (cellarrsize < 1 || cellarrsize > 3) {
+ p->state = -1;
+ return false;
+ }
+
+ NEXT_TYPE(tok, MPACK_TOKEN_STR);
+ if (tok.length > size) {
+ return false;
+ }
+
+ const char *cellbuf = data;
+ size_t cellsize = tok.length;
+ data += cellsize;
+ size -= cellsize;
+
+ if (cellarrsize >= 2) {
+ NEXT_TYPE(tok, MPACK_TOKEN_SINT);
+ g->cur_attr = (int)tok.data.value.lo;
+ }
+
+ int repeat = 1;
+ if (cellarrsize >= 3) {
+ NEXT_TYPE(tok, MPACK_TOKEN_UINT);
+ repeat = (int)tok.data.value.lo;
+ }
+
+ g->clear_width = 0;
+ if (g->icell == g->ncells - 1 && cellsize == 1 && cellbuf[0] == ' ' && repeat > 1) {
+ g->clear_width = repeat;
+ } else {
+ for (int r = 0; r < repeat; r++) {
+ if (g->coloff >= (int)grid_line_buf_size) {
+ p->state = -1;
+ return false;
+ }
+ memcpy(grid_line_buf_char[g->coloff], cellbuf, cellsize);
+ grid_line_buf_char[g->coloff][cellsize] = NUL;
+ grid_line_buf_attr[g->coloff++] = g->cur_attr;
+ }
+ }
+
+ g->icell++;
+ p->read_ptr = data;
+ p->read_size = size;
+ if (g->icell == g->ncells) {
+ return true;
+ }
+ goto redo;
+
+ default:
+ abort();
}
- return true;
}
diff --git a/src/nvim/msgpack_rpc/unpacker.h b/src/nvim/msgpack_rpc/unpacker.h
index e0dc6f0a68..f39439be63 100644
--- a/src/nvim/msgpack_rpc/unpacker.h
+++ b/src/nvim/msgpack_rpc/unpacker.h
@@ -32,8 +32,13 @@ struct Unpacker {
Error unpack_error;
Arena arena;
- // one lenght free-list of reusable blocks
+ // one length free-list of reusable blocks
ArenaMem reuse_blk;
+
+ int nevents;
+ int ncalls;
+ UIClientHandler ui_handler;
+ GridLineEvent *grid_line_event;
};
// unrecovareble error. unpack_error should be set!
diff --git a/src/nvim/normal.c b/src/nvim/normal.c
index b675abfb7d..a842bb20a9 100644
--- a/src/nvim/normal.c
+++ b/src/nvim/normal.c
@@ -1908,6 +1908,13 @@ bool do_mouse(oparg_T *oap, int c, int dir, long count, bool fixindent)
StlClickDefinition *click_defs = in_status_line ? wp->w_status_click_defs
: wp->w_winbar_click_defs;
+ if (in_status_line && global_stl_height() > 0) {
+ // global statusline is displayed for the current window,
+ // and spans the whole screen.
+ click_defs = curwin->w_status_click_defs;
+ click_col = mouse_col;
+ }
+
if (click_defs != NULL) {
switch (click_defs[click_col].type) {
case kStlClickDisabled:
diff --git a/src/nvim/option.c b/src/nvim/option.c
index 571d47e38e..6bbea8e2ae 100644
--- a/src/nvim/option.c
+++ b/src/nvim/option.c
@@ -49,6 +49,7 @@
#include "nvim/highlight.h"
#include "nvim/highlight_group.h"
#include "nvim/indent_c.h"
+#include "nvim/insexpand.h"
#include "nvim/keycodes.h"
#include "nvim/macros.h"
#include "nvim/mapping.h"
diff --git a/src/nvim/os/stdpaths.c b/src/nvim/os/stdpaths.c
index 5576e7ba07..59d315d44c 100644
--- a/src/nvim/os/stdpaths.c
+++ b/src/nvim/os/stdpaths.c
@@ -115,6 +115,10 @@ char *get_xdg_home(const XDGVarType idx)
#else
dir = concat_fnames_realloc(dir, "nvim", true);
#endif
+
+#ifdef BACKSLASH_IN_FILENAME
+ slash_adjust((char_u *)dir);
+#endif
}
return dir;
}
diff --git a/src/nvim/popupmnu.c b/src/nvim/popupmnu.c
index 841277f8f3..5b1b80935e 100644
--- a/src/nvim/popupmnu.c
+++ b/src/nvim/popupmnu.c
@@ -16,6 +16,7 @@
#include "nvim/edit.h"
#include "nvim/eval/typval.h"
#include "nvim/ex_cmds.h"
+#include "nvim/insexpand.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/menu.h"
diff --git a/src/nvim/screen.c b/src/nvim/screen.c
index de837720c1..374a840144 100644
--- a/src/nvim/screen.c
+++ b/src/nvim/screen.c
@@ -92,6 +92,7 @@
#include "nvim/highlight.h"
#include "nvim/highlight_group.h"
#include "nvim/indent.h"
+#include "nvim/insexpand.h"
#include "nvim/lib/kvec.h"
#include "nvim/log.h"
#include "nvim/lua/executor.h"
diff --git a/src/nvim/search.c b/src/nvim/search.c
index f3061b4dc4..e1a5d0214e 100644
--- a/src/nvim/search.c
+++ b/src/nvim/search.c
@@ -27,6 +27,7 @@
#include "nvim/getchar.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
+#include "nvim/insexpand.h"
#include "nvim/main.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
@@ -5849,7 +5850,7 @@ exit_matched:
if (action == ACTION_EXPAND) {
ins_compl_check_keys(30, false);
}
- if (got_int || compl_interrupted) {
+ if (got_int || ins_compl_interrupted()) {
break;
}
@@ -5911,7 +5912,7 @@ exit_matched:
}
} else if (!found
&& action != ACTION_EXPAND) {
- if (got_int || compl_interrupted) {
+ if (got_int || ins_compl_interrupted()) {
emsg(_(e_interr));
} else if (type == FIND_DEFINE) {
emsg(_("E388: Couldn't find definition"));
diff --git a/src/nvim/spell.c b/src/nvim/spell.c
index 2aadc2258e..8bc9cda247 100644
--- a/src/nvim/spell.c
+++ b/src/nvim/spell.c
@@ -95,6 +95,7 @@
#include "nvim/getchar.h"
#include "nvim/hashtab.h"
#include "nvim/input.h"
+#include "nvim/insexpand.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
@@ -7012,7 +7013,7 @@ void spell_dump_compl(char_u *pat, int ic, Direction *dir, int dumpflags_arg)
arridx[0] = 0;
curi[0] = 1;
while (depth >= 0 && !got_int
- && (pat == NULL || !compl_interrupted)) {
+ && (pat == NULL || !ins_compl_interrupted())) {
if (curi[depth] > byts[arridx[depth]]) {
// Done all bytes at this node, go up one level.
--depth;
diff --git a/src/nvim/state.c b/src/nvim/state.c
index 6475105192..be8017ea83 100644
--- a/src/nvim/state.c
+++ b/src/nvim/state.c
@@ -5,10 +5,10 @@
#include "nvim/ascii.h"
#include "nvim/autocmd.h"
-#include "nvim/edit.h"
#include "nvim/eval.h"
#include "nvim/ex_docmd.h"
#include "nvim/getchar.h"
+#include "nvim/insexpand.h"
#include "nvim/lib/kvec.h"
#include "nvim/log.h"
#include "nvim/main.h"
diff --git a/src/nvim/tag.c b/src/nvim/tag.c
index 28b3b6c1ef..de6635514c 100644
--- a/src/nvim/tag.c
+++ b/src/nvim/tag.c
@@ -26,6 +26,7 @@
#include "nvim/garray.h"
#include "nvim/if_cscope.h"
#include "nvim/input.h"
+#include "nvim/insexpand.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
#include "nvim/memory.h"
@@ -1648,7 +1649,7 @@ int find_tags(char_u *pat, int *num_matches, char_u ***matchesp, int flags, int
if ((flags & TAG_INS_COMP)) { // Double brackets for gcc
ins_compl_check_keys(30, false);
}
- if (got_int || compl_interrupted) {
+ if (got_int || ins_compl_interrupted()) {
stop_searching = true;
break;
}
diff --git a/src/nvim/testdir/setup.vim b/src/nvim/testdir/setup.vim
index e6c0762729..9d6fc5e526 100644
--- a/src/nvim/testdir/setup.vim
+++ b/src/nvim/testdir/setup.vim
@@ -4,6 +4,7 @@ if exists('s:did_load')
set complete=.,w,b,u,t,i
set directory&
set directory^=.
+ set display=
set fillchars=vert:\|,fold:-
set formatoptions=tcq
set fsync
diff --git a/src/nvim/testdir/term_util.vim b/src/nvim/testdir/term_util.vim
index 3a838a3a1f..5ff09e25b4 100644
--- a/src/nvim/testdir/term_util.vim
+++ b/src/nvim/testdir/term_util.vim
@@ -9,3 +9,5 @@ func CanRunVimInTerminal()
" Nvim: always false, we use Lua screen-tests instead.
return 0
endfunc
+
+" vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/nvim/testdir/test_clientserver.vim b/src/nvim/testdir/test_clientserver.vim
index db62fe5fa6..edf36b413b 100644
--- a/src/nvim/testdir/test_clientserver.vim
+++ b/src/nvim/testdir/test_clientserver.vim
@@ -1,16 +1,12 @@
" Tests for the +clientserver feature.
-if !has('job') || !has('clientserver')
- throw 'Skipped: job and/or clientserver feature missing'
-endif
+source check.vim
+CheckFeature job
+CheckFeature clientserver
source shared.vim
-func Test_client_server()
- let cmd = GetVimCommand()
- if cmd == ''
- return
- endif
+func Check_X11_Connection()
if has('x11')
if empty($DISPLAY)
throw 'Skipped: $DISPLAY is not set'
@@ -19,11 +15,19 @@ func Test_client_server()
call remote_send('xxx', '')
catch
if v:exception =~ 'E240:'
- throw 'Skipped: no connection to the X server'
+ throw 'Skipped: no connection to the X server'
endif
" ignore other errors
endtry
endif
+endfunc
+
+func Test_client_server()
+ let cmd = GetVimCommand()
+ if cmd == ''
+ return
+ endif
+ call Check_X11_Connection()
let name = 'XVIMTEST'
let cmd .= ' --servername ' . name
@@ -34,6 +38,14 @@ func Test_client_server()
" When using valgrind it takes much longer.
call WaitForAssert({-> assert_match(name, serverlist())})
+ if !has('win32')
+ if RunVim([], [], '--serverlist >Xtest_serverlist')
+ let lines = readfile('Xtest_serverlist')
+ call assert_true(index(lines, 'XVIMTEST') >= 0)
+ endif
+ call delete('Xtest_serverlist')
+ endif
+
eval name->remote_foreground()
call remote_send(name, ":let testvar = 'yes'\<CR>")
@@ -81,6 +93,10 @@ func Test_client_server()
endif
let g:testvar = 'myself'
call assert_equal('myself', remote_expr(v:servername, 'testvar'))
+ call remote_send(v:servername, ":let g:testvar2 = 75\<CR>")
+ call feedkeys('', 'x')
+ call assert_equal(75, g:testvar2)
+ call assert_fails('let v = remote_expr(v:servername, "/2")', 'E449:')
call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
call assert_equal('got it', g:myserverid->remote_read(2))
@@ -101,6 +117,55 @@ func Test_client_server()
call assert_equal('another', g:peek_result)
call assert_equal('another', remote_read(g:myserverid, 2))
+ if !has('gui_running')
+ " In GUI vim, the following tests display a dialog box
+
+ let cmd = GetVimProg() .. ' --servername ' .. name
+
+ " Run a separate instance to send a command to the server
+ call remote_expr(name, 'execute("only")')
+ call system(cmd .. ' --remote-send ":new Xfile<CR>"')
+ call assert_equal('2', remote_expr(name, 'winnr("$")'))
+ call assert_equal('Xfile', remote_expr(name, 'winbufnr(1)->bufname()'))
+ call remote_expr(name, 'execute("only")')
+
+ " Invoke a remote-expr. On MS-Windows, the returned value has a carriage
+ " return.
+ let l = system(cmd .. ' --remote-expr "2 + 2"')
+ call assert_equal(['4'], split(l, "\n"))
+
+ " Edit multiple files using --remote
+ call system(cmd .. ' --remote Xfile1 Xfile2 Xfile3')
+ call assert_equal("Xfile1\nXfile2\nXfile3\n", remote_expr(name, 'argv()'))
+ eval name->remote_send(":%bw!\<CR>")
+
+ " Edit files in separate tab pages
+ call system(cmd .. ' --remote-tab Xfile1 Xfile2 Xfile3')
+ call assert_equal('3', remote_expr(name, 'tabpagenr("$")'))
+ call assert_equal('Xfile2', remote_expr(name, 'bufname(tabpagebuflist(2)[0])'))
+ eval name->remote_send(":%bw!\<CR>")
+
+ " Edit a file using --remote-wait
+ eval name->remote_send(":source $VIMRUNTIME/plugin/rrhelper.vim\<CR>")
+ call system(cmd .. ' --remote-wait +enew Xfile1')
+ call assert_equal("Xfile1", remote_expr(name, 'bufname("#")'))
+ eval name->remote_send(":%bw!\<CR>")
+
+ " Edit files using --remote-tab-wait
+ call system(cmd .. ' --remote-tabwait +tabonly\|enew Xfile1 Xfile2')
+ call assert_equal('1', remote_expr(name, 'tabpagenr("$")'))
+ eval name->remote_send(":%bw!\<CR>")
+
+ " Error cases
+ if v:lang == "C" || v:lang =~ '^[Ee]n'
+ let l = split(system(cmd .. ' --remote +pwd'), "\n")
+ call assert_equal("Argument missing after: \"+pwd\"", l[1])
+ endif
+ let l = system(cmd .. ' --remote-expr "abcd"')
+ call assert_match('^E449: ', l)
+ endif
+
+ eval name->remote_send(":%bw!\<CR>")
eval name->remote_send(":qa!\<CR>")
try
call WaitForAssert({-> assert_equal("dead", job_status(job))})
@@ -111,8 +176,8 @@ func Test_client_server()
endif
endtry
- call assert_fails("let x=remote_peek([])", 'E730:')
- call assert_fails("let x=remote_read('vim10')", 'E277:')
+ call assert_fails("let x = remote_peek([])", 'E730:')
+ call assert_fails("let x = remote_read('vim10')", 'E277:')
endfunc
" Uncomment this line to get a debugging log
diff --git a/src/nvim/testdir/test_ex_mode.vim b/src/nvim/testdir/test_ex_mode.vim
index 122572f32a..c06d143425 100644
--- a/src/nvim/testdir/test_ex_mode.vim
+++ b/src/nvim/testdir/test_ex_mode.vim
@@ -155,9 +155,7 @@ endfunc
func Test_Ex_echo_backslash()
throw 'Skipped: Nvim only supports Vim Ex mode'
" This test works only when the language is English
- if v:lang != "C" && v:lang !~ '^[Ee]n'
- return
- endif
+ CheckEnglish
let bsl = '\\\\'
let bsl2 = '\\\'
call assert_fails('call feedkeys("Qecho " .. bsl .. "\nvisual\n", "xt")',
diff --git a/src/nvim/testdir/test_excmd.vim b/src/nvim/testdir/test_excmd.vim
index 7d581d5efd..f844999d8d 100644
--- a/src/nvim/testdir/test_excmd.vim
+++ b/src/nvim/testdir/test_excmd.vim
@@ -242,49 +242,58 @@ func Test_confirm_cmd()
CheckNotGui
CheckRunVimInTerminal
- call writefile(['foo1'], 'foo')
- call writefile(['bar1'], 'bar')
+ call writefile(['foo1'], 'Xfoo')
+ call writefile(['bar1'], 'Xbar')
" Test for saving all the modified buffers
- let buf = RunVimInTerminal('', {'rows': 20})
- call term_sendkeys(buf, ":set nomore\n")
- call term_sendkeys(buf, ":new foo\n")
- call term_sendkeys(buf, ":call setline(1, 'foo2')\n")
- call term_sendkeys(buf, ":new bar\n")
- call term_sendkeys(buf, ":call setline(1, 'bar2')\n")
- call term_sendkeys(buf, ":wincmd b\n")
+ let lines =<< trim END
+ set nomore
+ new Xfoo
+ call setline(1, 'foo2')
+ new Xbar
+ call setline(1, 'bar2')
+ wincmd b
+ END
+ call writefile(lines, 'Xscript')
+ let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
call term_sendkeys(buf, ":confirm qall\n")
call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
call term_sendkeys(buf, "A")
call StopVimInTerminal(buf)
- call assert_equal(['foo2'], readfile('foo'))
- call assert_equal(['bar2'], readfile('bar'))
+ call assert_equal(['foo2'], readfile('Xfoo'))
+ call assert_equal(['bar2'], readfile('Xbar'))
" Test for discarding all the changes to modified buffers
- let buf = RunVimInTerminal('', {'rows': 20})
- call term_sendkeys(buf, ":set nomore\n")
- call term_sendkeys(buf, ":new foo\n")
- call term_sendkeys(buf, ":call setline(1, 'foo3')\n")
- call term_sendkeys(buf, ":new bar\n")
- call term_sendkeys(buf, ":call setline(1, 'bar3')\n")
- call term_sendkeys(buf, ":wincmd b\n")
+ let lines =<< trim END
+ set nomore
+ new Xfoo
+ call setline(1, 'foo3')
+ new Xbar
+ call setline(1, 'bar3')
+ wincmd b
+ END
+ call writefile(lines, 'Xscript')
+ let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
call term_sendkeys(buf, ":confirm qall\n")
call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
call term_sendkeys(buf, "D")
call StopVimInTerminal(buf)
- call assert_equal(['foo2'], readfile('foo'))
- call assert_equal(['bar2'], readfile('bar'))
+ call assert_equal(['foo2'], readfile('Xfoo'))
+ call assert_equal(['bar2'], readfile('Xbar'))
" Test for saving and discarding changes to some buffers
- let buf = RunVimInTerminal('', {'rows': 20})
- call term_sendkeys(buf, ":set nomore\n")
- call term_sendkeys(buf, ":new foo\n")
- call term_sendkeys(buf, ":call setline(1, 'foo4')\n")
- call term_sendkeys(buf, ":new bar\n")
- call term_sendkeys(buf, ":call setline(1, 'bar4')\n")
- call term_sendkeys(buf, ":wincmd b\n")
+ let lines =<< trim END
+ set nomore
+ new Xfoo
+ call setline(1, 'foo4')
+ new Xbar
+ call setline(1, 'bar4')
+ wincmd b
+ END
+ call writefile(lines, 'Xscript')
+ let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
call term_sendkeys(buf, ":confirm qall\n")
call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
call term_sendkeys(buf, "N")
@@ -292,11 +301,12 @@ func Test_confirm_cmd()
call term_sendkeys(buf, "Y")
call StopVimInTerminal(buf)
- call assert_equal(['foo4'], readfile('foo'))
- call assert_equal(['bar2'], readfile('bar'))
+ call assert_equal(['foo4'], readfile('Xfoo'))
+ call assert_equal(['bar2'], readfile('Xbar'))
- call delete('foo')
- call delete('bar')
+ call delete('Xscript')
+ call delete('Xfoo')
+ call delete('Xbar')
endfunc
func Test_confirm_cmd_cancel()
@@ -304,10 +314,13 @@ func Test_confirm_cmd_cancel()
CheckRunVimInTerminal
" Test for closing a window with a modified buffer
- let buf = RunVimInTerminal('', {'rows': 20})
- call term_sendkeys(buf, ":set nomore\n")
- call term_sendkeys(buf, ":new\n")
- call term_sendkeys(buf, ":call setline(1, 'abc')\n")
+ let lines =<< trim END
+ set nomore
+ new
+ call setline(1, 'abc')
+ END
+ call writefile(lines, 'Xscript')
+ let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
call term_sendkeys(buf, ":confirm close\n")
call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
\ term_getline(buf, 20))}, 1000)
@@ -320,6 +333,43 @@ func Test_confirm_cmd_cancel()
call WaitForAssert({-> assert_match('^ *0,0-1 All$',
\ term_getline(buf, 20))}, 1000)
call StopVimInTerminal(buf)
+ call delete('Xscript')
+endfunc
+
+" The ":confirm" prompt was sometimes used with the terminal in cooked mode.
+" This test verifies that a "\<CR>" character is NOT required to respond to a
+" prompt from the ":conf q" and ":conf wq" commands.
+func Test_confirm_q_wq()
+ CheckNotGui
+ CheckRunVimInTerminal
+
+ call writefile(['foo'], 'Xfoo')
+
+ let lines =<< trim END
+ set hidden nomore
+ call setline(1, 'abc')
+ edit Xfoo
+ END
+ call writefile(lines, 'Xscript')
+ let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
+ call term_sendkeys(buf, ":confirm q\n")
+ call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
+ \ term_getline(buf, 20))}, 1000)
+ call term_sendkeys(buf, 'C')
+ call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
+ \ term_getline(buf, 20))}, 1000)
+
+ call term_sendkeys(buf, ":edit Xfoo\n")
+ call term_sendkeys(buf, ":confirm wq\n")
+ call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
+ \ term_getline(buf, 20))}, 1000)
+ call term_sendkeys(buf, 'C')
+ call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
+ \ term_getline(buf, 20))}, 1000)
+ call StopVimInTerminal(buf)
+
+ call delete('Xscript')
+ call delete('Xfoo')
endfunc
func Test_confirm_write_ro()
diff --git a/src/nvim/testdir/test_expand.vim b/src/nvim/testdir/test_expand.vim
index d86fea4f45..383921bb82 100644
--- a/src/nvim/testdir/test_expand.vim
+++ b/src/nvim/testdir/test_expand.vim
@@ -84,6 +84,15 @@ func Test_expandcmd()
let $FOO="blue\tsky"
call setline(1, "$FOO")
call assert_equal("grep pat blue\tsky", expandcmd('grep pat <cfile>'))
+
+ " Test for expression expansion `=
+ let $FOO= "blue"
+ call assert_equal("blue sky", expandcmd("`=$FOO .. ' sky'`"))
+
+ " Test for env variable with spaces
+ let $FOO= "foo bar baz"
+ call assert_equal("e foo bar baz", expandcmd("e $FOO"))
+
unlet $FOO
close!
endfunc
diff --git a/src/nvim/testdir/test_functions.vim b/src/nvim/testdir/test_functions.vim
index 9b8d740efb..c11e7b4fea 100644
--- a/src/nvim/testdir/test_functions.vim
+++ b/src/nvim/testdir/test_functions.vim
@@ -547,6 +547,7 @@ func Save_mode()
return ''
endfunc
+" Test for the mode() function
func Test_mode()
new
call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
@@ -717,6 +718,8 @@ func Test_mode()
call assert_equal('c-c', g:current_modes)
call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
call assert_equal('c-cv', g:current_modes)
+ " call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
+ " call assert_equal('c-ce', g:current_modes)
" How to test Ex mode?
" Test mode in operatorfunc (it used to be Operator-pending).
@@ -1262,6 +1265,23 @@ func Test_inputlist()
call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx')
call assert_equal(0, c)
+ " Use backspace to delete characters in the prompt
+ call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
+ call assert_equal(2, c)
+
+ " Use mouse to make a selection
+ " call test_setmouse(&lines - 3, 2)
+ call nvim_input_mouse('left', 'press', '', 0, &lines - 4, 1) " set mouse position
+ call getchar() " discard mouse event but keep mouse position
+ call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
+ call assert_equal(1, c)
+ " Mouse click outside of the list
+ " call test_setmouse(&lines - 6, 2)
+ call nvim_input_mouse('left', 'press', '', 0, &lines - 7, 1) " set mouse position
+ call getchar() " discard mouse event but keep mouse position
+ call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
+ call assert_equal(-2, c)
+
call assert_fails('call inputlist("")', 'E686:')
endfunc
diff --git a/src/nvim/testdir/test_options.vim b/src/nvim/testdir/test_options.vim
index 1f003041e6..ccf9c5c35e 100644
--- a/src/nvim/testdir/test_options.vim
+++ b/src/nvim/testdir/test_options.vim
@@ -1,6 +1,7 @@
" Test for options
source check.vim
+source view_util.vim
func Test_whichwrap()
set whichwrap=b,s
@@ -801,6 +802,14 @@ func Test_rightleftcmd()
set rightleft&
endfunc
+" Test for the "debug" option
+func Test_debug_option()
+ set debug=beep
+ exe "normal \<C-c>"
+ call assert_equal('Beep!', Screenline(&lines))
+ set debug&
+endfunc
+
" Test for setting option values using v:false and v:true
func Test_opt_boolean()
set number&
diff --git a/src/nvim/testdir/test_startup.vim b/src/nvim/testdir/test_startup.vim
index d830f5216d..39fafbf7b4 100644
--- a/src/nvim/testdir/test_startup.vim
+++ b/src/nvim/testdir/test_startup.vim
@@ -12,6 +12,9 @@ func Test_startup_script()
source $VIMRUNTIME/defaults.vim
call assert_equal(0, &compatible)
+ " Restore some options, so that the following tests doesn't break
+ set nomore
+ set noshowmode
endfunc
" Verify the order in which plugins are loaded:
@@ -814,6 +817,25 @@ func Test_v_argv()
call assert_equal(['arg1', '--cmd', 'echo v:argv', '--cmd', 'q'']'], list[idx:])
endfunc
+" Test for the "-r" recovery mode option
+func Test_r_arg()
+ throw 'Skipped: Nvim has different directories'
+ " Can't catch the output of gvim.
+ CheckNotGui
+ CheckUnix
+ CheckEnglish
+ let cmd = GetVimCommand()
+ " There can be swap files anywhere, only check for the headers.
+ let expected =<< trim END
+ Swap files found:.*
+ In current directory:.*
+ In directory \~/tmp:.*
+ In directory /var/tmp:.*
+ In directory /tmp:.*
+ END
+ call assert_match(join(expected, ""), system(cmd .. " -r")->substitute("[\r\n]\\+", '', ''))
+endfunc
+
" Test for the '-t' option to jump to a tag
func Test_t_arg()
let before =<< trim [CODE]
@@ -824,6 +846,7 @@ func Test_t_arg()
call writefile([s], "Xtestout")
qall
[CODE]
+
call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
\ "first\tXfile1\t/^ \\zsfirst$/",
\ "second\tXfile1\t/^ \\zssecond$/",
@@ -890,6 +913,138 @@ func Test_x_arg()
call delete('Xtest_x_arg')
endfunc
+" Test for entering the insert mode on startup
+func Test_start_insertmode()
+ throw "Skipped: Nvim does not support setting 'insertmode'"
+ let before =<< trim [CODE]
+ set insertmode
+ [CODE]
+ let after =<< trim [CODE]
+ call writefile(['insertmode=' .. &insertmode], 'Xtestout')
+ qall
+ [CODE]
+ if RunVim(before, after, '')
+ call assert_equal(['insertmode=1'], readfile('Xtestout'))
+ call delete('Xtestout')
+ endif
+endfunc
+
+" Test for enabling the binary mode on startup
+func Test_b_arg()
+ let after =<< trim [CODE]
+ call writefile(['binary=' .. &binary], 'Xtestout')
+ qall
+ [CODE]
+ if RunVim([], after, '-b')
+ call assert_equal(['binary=1'], readfile('Xtestout'))
+ call delete('Xtestout')
+ endif
+endfunc
+
+" Test for enabling the lisp mode on startup
+func Test_l_arg()
+ let after =<< trim [CODE]
+ let s = 'lisp=' .. &lisp .. ', showmatch=' .. &showmatch
+ call writefile([s], 'Xtestout')
+ qall
+ [CODE]
+ if RunVim([], after, '-l')
+ call assert_equal(['lisp=1, showmatch=1'], readfile('Xtestout'))
+ call delete('Xtestout')
+ endif
+endfunc
+
+" Test for specifying a non-existing vimrc file using "-u"
+func Test_missing_vimrc()
+ if !CanRunVimInTerminal()
+ throw 'Skipped: cannot run vim in terminal'
+ endif
+ let after =<< trim [CODE]
+ call assert_match('^E282:', v:errmsg)
+ call writefile(v:errors, 'Xtestout')
+ [CODE]
+ call writefile(after, 'Xafter')
+
+ let cmd = GetVimCommandCleanTerm() . ' -u Xvimrc_missing -S Xafter'
+ let buf = term_start(cmd, {'term_rows' : 10})
+ call WaitForAssert({-> assert_equal("running", term_getstatus(buf))})
+ call term_wait(buf)
+ call term_sendkeys(buf, "\n:")
+ call term_wait(buf)
+ call WaitForAssert({-> assert_match(':', term_getline(buf, 10))})
+ call StopVimInTerminal(buf)
+ call assert_equal([], readfile('Xtestout'))
+ call delete('Xafter')
+ call delete('Xtestout')
+endfunc
+
+" Test for using the $VIMINIT environment variable
+func Test_VIMINIT()
+ let after =<< trim [CODE]
+ call assert_equal(1, exists('viminit_found'))
+ call assert_equal('yes', viminit_found)
+ call writefile(v:errors, 'Xtestout')
+ qall
+ [CODE]
+ call writefile(after, 'Xafter')
+ " let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "set enc=utf8"'
+ let cmd = GetVimProg() . ' -S Xafter --cmd "set enc=utf8"'
+ call setenv('VIMINIT', 'let viminit_found="yes"')
+ exe "silent !" . cmd
+ call assert_equal([], readfile('Xtestout'))
+ call delete('Xtestout')
+ call delete('Xafter')
+endfunc
+
+" Test for using the $EXINIT environment variable
+func Test_EXINIT()
+ let after =<< trim [CODE]
+ call assert_equal(1, exists('exinit_found'))
+ call assert_equal('yes', exinit_found)
+ call writefile(v:errors, 'Xtestout')
+ qall
+ [CODE]
+ call writefile(after, 'Xafter')
+ " let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "set enc=utf8"'
+ let cmd = GetVimProg() . ' -S Xafter --cmd "set enc=utf8"'
+ call setenv('EXINIT', 'let exinit_found="yes"')
+ exe "silent !" . cmd
+ call assert_equal([], readfile('Xtestout'))
+ call delete('Xtestout')
+ call delete('Xafter')
+endfunc
+
+" Test for using the 'exrc' option
+func Test_exrc()
+ let after =<< trim [CODE]
+ call assert_equal(1, &exrc)
+ call assert_equal(1, &secure)
+ call assert_equal(37, exrc_found)
+ call writefile(v:errors, 'Xtestout')
+ qall
+ [CODE]
+ call mkdir('Xdir')
+ call writefile(['let exrc_found=37'], 'Xdir/.exrc')
+ call writefile(after, 'Xdir/Xafter')
+ " let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "cd Xdir" --cmd "set enc=utf8 exrc secure"'
+ let cmd = GetVimProg() . ' -S Xafter --cmd "cd Xdir" --cmd "set enc=utf8 exrc secure"'
+ exe "silent !" . cmd
+ call assert_equal([], readfile('Xdir/Xtestout'))
+ call delete('Xdir', 'rf')
+endfunc
+
+" Test for starting Vim with a non-terminal as input/output
+func Test_io_not_a_terminal()
+ throw 'Skipped: Nvim does not support --ttyfail'
+ " Can't catch the output of gvim.
+ CheckNotGui
+ CheckUnix
+ CheckEnglish
+ let l = systemlist(GetVimProg() .. ' --ttyfail')
+ call assert_equal(['Vim: Warning: Output is not to a terminal',
+ \ 'Vim: Warning: Input is not from a terminal'], l)
+endfunc
+
" Test for --not-a-term avoiding escape codes.
func Test_not_a_term()
CheckUnix
@@ -948,6 +1103,93 @@ func Test_w_arg()
call delete('Xscriptin')
endfunc
+" Test for the "-s scriptin" argument
+func Test_s_arg()
+ " Can't catch the output of gvim.
+ CheckNotGui
+ CheckEnglish
+ " Test for failing to open the script input file.
+ let m = system(GetVimCommand() .. " -s abcxyz")
+ " call assert_equal("Cannot open for reading: \"abcxyz\"\n", m)
+ call assert_equal("Cannot open for reading: \"abcxyz\": no such file or directory\n", m)
+
+ call writefile([], 'Xinput')
+ let m = system(GetVimCommand() .. " -s Xinput -s Xinput")
+ call assert_equal("Attempt to open script file again: \"-s Xinput\"\n", m)
+ call delete('Xinput')
+endfunc
+
+" Test for the "-n" (no swap file) argument
+func Test_n_arg()
+ let after =<< trim [CODE]
+ call assert_equal(0, &updatecount)
+ call writefile(v:errors, 'Xtestout')
+ qall
+ [CODE]
+ if RunVim([], after, '-n')
+ call assert_equal([], readfile('Xtestout'))
+ call delete('Xtestout')
+ endif
+endfunc
+
+" Test for the "-h" (help) argument
+func Test_h_arg()
+ throw 'Skipped: Nvim has different output for "-h" argument'
+ " Can't catch the output of gvim.
+ CheckNotGui
+ let l = systemlist(GetVimProg() .. ' -h')
+ call assert_match('^VIM - Vi IMproved', l[0])
+ let l = systemlist(GetVimProg() .. ' -?')
+ call assert_match('^VIM - Vi IMproved', l[0])
+endfunc
+
+" Test for the "-F" (farsi) argument
+func Test_F_arg()
+ throw 'Skipped: Nvim does not recognize "-F" argument'
+ " Can't catch the output of gvim.
+ CheckNotGui
+ let l = systemlist(GetVimProg() .. ' -F')
+ call assert_match('^E27:', l[0])
+endfunc
+
+" Test for the "-E" (improved Ex mode) argument
+func Test_E_arg()
+ let after =<< trim [CODE]
+ call assert_equal('cv', mode(1))
+ call writefile(v:errors, 'Xtestout')
+ qall
+ [CODE]
+ if RunVim([], after, '-E')
+ call assert_equal([], readfile('Xtestout'))
+ call delete('Xtestout')
+ endif
+endfunc
+
+" Test for the "-D" (debugger) argument
+func Test_D_arg()
+ CheckRunVimInTerminal
+
+ let cmd = GetVimCommandCleanTerm() .. ' -D'
+ let buf = term_start(cmd, {'term_rows' : 10})
+ call WaitForAssert({-> assert_equal("running", term_getstatus(buf))})
+
+ call WaitForAssert({-> assert_equal('Entering Debug mode. Type "cont" to continue.',
+ \ term_getline(buf, 7))})
+ call WaitForAssert({-> assert_equal('>', term_getline(buf, 10))})
+
+ call StopVimInTerminal(buf)
+endfunc
+
+" Test for too many edit argument errors
+func Test_too_many_edit_args()
+ throw 'Skipped: N/A'
+ " Can't catch the output of gvim.
+ CheckNotGui
+ CheckEnglish
+ let l = systemlist(GetVimProg() .. ' - -')
+ call assert_match('^Too many edit arguments: "-"', l[1])
+endfunc
+
" Test starting vim with various names: vim, ex, view, evim, etc.
func Test_progname()
CheckUnix
diff --git a/src/nvim/testdir/test_textformat.vim b/src/nvim/testdir/test_textformat.vim
index 970f5ae0d0..1d1f20d91a 100644
--- a/src/nvim/testdir/test_textformat.vim
+++ b/src/nvim/testdir/test_textformat.vim
@@ -1260,6 +1260,41 @@ func Test_comment_nested()
%bw!
endfunc
+" Test for a space character in 'comments' setting
+func Test_comment_space()
+ new
+ setlocal comments=b:\ > fo+=ro
+ exe "normal i> B\nD\<C-C>ggOA\<C-C>joC"
+ exe "normal Go > F\nH\<C-C>kOE\<C-C>joG"
+ let expected =<< trim END
+ A
+ > B
+ C
+ D
+ > E
+ > F
+ > G
+ > H
+ END
+ call assert_equal(expected, getline(1, '$'))
+ %bw!
+endfunc
+
+" Test for the 'O' flag in 'comments'
+func Test_comment_O()
+ new
+ setlocal comments=Ob:* fo+=ro
+ exe "normal i* B\nD\<C-C>kOA\<C-C>joC"
+ let expected =<< trim END
+ A
+ * B
+ * C
+ * D
+ END
+ call assert_equal(expected, getline(1, '$'))
+ %bw!
+endfunc
+
" Test for 'a' and 'w' flags in 'formatoptions'
func Test_fo_a_w()
new
@@ -1299,6 +1334,25 @@ func Test_fo_a_w()
%bw!
endfunc
+" Test for 'j' flag in 'formatoptions'
+func Test_fo_j()
+ new
+ setlocal fo+=j comments=://
+ call setline(1, ['i++; // comment1', ' // comment2'])
+ normal J
+ call assert_equal('i++; // comment1 comment2', getline(1))
+ setlocal fo-=j
+ call setline(1, ['i++; // comment1', ' // comment2'])
+ normal J
+ call assert_equal('i++; // comment1 // comment2', getline(1))
+ " Test with nested comments
+ setlocal fo+=j comments=n:>,n:)
+ call setline(1, ['i++; > ) > ) comment1', ' > ) comment2'])
+ normal J
+ call assert_equal('i++; > ) > ) comment1 comment2', getline(1))
+ %bw!
+endfunc
+
" Test for formatting lines using gq in visual mode
func Test_visual_gq_format()
new
diff --git a/src/nvim/testdir/test_trycatch.vim b/src/nvim/testdir/test_trycatch.vim
index 598402fafe..646594e482 100644
--- a/src/nvim/testdir/test_trycatch.vim
+++ b/src/nvim/testdir/test_trycatch.vim
@@ -2014,11 +2014,11 @@ endfunc
" Test for verbose messages with :try :catch, and :finally {{{1
func Test_try_catch_verbose()
" This test works only when the language is English
- if v:lang != "C" && v:lang !~ '^[Ee]n'
- return
- endif
+ CheckEnglish
set verbose=14
+
+ " Test for verbose messages displayed when an exception is caught
redir => msg
try
echo i
diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim
index 7decac2c36..3bfff0a577 100644
--- a/src/nvim/testdir/test_window_cmd.vim
+++ b/src/nvim/testdir/test_window_cmd.vim
@@ -1360,7 +1360,6 @@ func Test_win_move_separator()
endfunc
func Test_win_move_statusline()
- redraw " This test fails in Nvim without a redraw to clear messages.
edit a
leftabove split b
let h = winheight(0)
diff --git a/src/nvim/ui.c b/src/nvim/ui.c
index a49e9df9ee..e958f02e32 100644
--- a/src/nvim/ui.c
+++ b/src/nvim/ui.c
@@ -348,7 +348,8 @@ void ui_attach_impl(UI *ui, uint64_t chanid)
if (ui_count == MAX_UI_COUNT) {
abort();
}
- if (!ui->ui_ext[kUIMultigrid] && !ui->ui_ext[kUIFloatDebug]) {
+ if (!ui->ui_ext[kUIMultigrid] && !ui->ui_ext[kUIFloatDebug]
+ && !ui_client_channel_id) {
ui_comp_attach(ui);
}
@@ -502,6 +503,9 @@ handle_T ui_cursor_grid(void)
void ui_flush(void)
{
+ if (!ui_active()) {
+ return;
+ }
cmdline_ui_flush();
win_ui_flush();
msg_ext_ui_flush();
diff --git a/src/nvim/ui_client.c b/src/nvim/ui_client.c
index be01538f67..09e971f03f 100644
--- a/src/nvim/ui_client.c
+++ b/src/nvim/ui_client.c
@@ -22,11 +22,6 @@
# include "ui_events_client.generated.h"
#endif
-// Temporary buffer for converting a single grid_line event
-static size_t buf_size = 0;
-static schar_T *buf_char = NULL;
-static sattr_T *buf_attr = NULL;
-
void ui_client_init(uint64_t chan)
{
Array args = ARRAY_DICT_INIT;
@@ -46,37 +41,23 @@ void ui_client_init(uint64_t chan)
ui_client_channel_id = chan;
}
-/// Handler for "redraw" events sent by the NVIM server
-///
-/// This function will be called by handle_request (in msgpack_rpc/channel.c)
-/// The individual ui_events sent by the server are individually handled
-/// by their respective handlers defined in ui_events_client.generated.h
-///
-/// @note The "flush" event is called only once and only after handling all
-/// the other events
-/// @param channel_id: The id of the rpc channel
-/// @param uidata: The dense array containing the ui_events sent by the server
-/// @param[out] err Error details, if any
-Object handle_ui_client_redraw(uint64_t channel_id, Array args, Error *error)
+UIClientHandler ui_client_get_redraw_handler(const char *name, size_t name_len, Error *error)
{
- for (size_t i = 0; i < args.size; i++) {
- Array call = args.items[i].data.array;
- String name = call.items[0].data.string;
-
- int hash = ui_client_handler_hash(name.data, name.size);
- if (hash < 0) {
- ELOG("No ui client handler for %s", name.size ? name.data : "<empty>");
- continue;
- }
- UIClientHandler handler = event_handlers[hash];
-
- // fprintf(stderr, "%s: %zu\n", name.data, call.size-1);
- DLOG("Invoke ui client handler for %s", name.data);
- for (size_t j = 1; j < call.size; j++) {
- handler.fn(call.items[j].data.array);
- }
+ int hash = ui_client_handler_hash(name, name_len);
+ if (hash < 0) {
+ return (UIClientHandler){ NULL, NULL };
}
+ return event_handlers[hash];
+}
+/// Placeholder for _sync_ requests with 'redraw' method name
+///
+/// async 'redraw' events, which are expected when nvim acts as an ui client.
+/// get handled in msgpack_rpc/unpacker.c and directy dispatched to handlers
+/// of specific ui events, like ui_client_event_grid_resize and so on.
+Object handle_ui_client_redraw(uint64_t channel_id, Array args, Error *error)
+{
+ api_set_error(error, kErrorTypeValidation, "'redraw' cannot be sent as a request");
return NIL;
}
@@ -120,88 +101,30 @@ void ui_client_event_grid_resize(Array args)
Integer height = args.items[2].data.integer;
ui_call_grid_resize(grid, width, height);
- if (buf_size < (size_t)width) {
- xfree(buf_char);
- xfree(buf_attr);
- buf_size = (size_t)width;
- buf_char = xmalloc(buf_size * sizeof(schar_T));
- buf_attr = xmalloc(buf_size * sizeof(sattr_T));
+ if (grid_line_buf_size < (size_t)width) {
+ xfree(grid_line_buf_char);
+ xfree(grid_line_buf_attr);
+ grid_line_buf_size = (size_t)width;
+ grid_line_buf_char = xmalloc(grid_line_buf_size * sizeof(schar_T));
+ grid_line_buf_attr = xmalloc(grid_line_buf_size * sizeof(sattr_T));
}
}
void ui_client_event_grid_line(Array args)
+ FUNC_ATTR_NORETURN
{
- if (args.size < 4
- || args.items[0].type != kObjectTypeInteger
- || args.items[1].type != kObjectTypeInteger
- || args.items[2].type != kObjectTypeInteger
- || args.items[3].type != kObjectTypeArray) {
- goto error;
- }
+ abort(); // unreachable
+}
- Integer grid = args.items[0].data.integer;
- Integer row = args.items[1].data.integer;
- Integer startcol = args.items[2].data.integer;
- Array cells = args.items[3].data.array;
+void ui_client_event_raw_line(GridLineEvent *g)
+{
+ int grid = g->args[0], row = g->args[1], startcol = g->args[2];
+ Integer endcol = startcol + g->coloff;
+ Integer clearcol = endcol + g->clear_width;
// TODO(hlpr98): Accommodate other LineFlags when included in grid_line
LineFlags lineflags = 0;
- size_t j = 0;
- int cur_attr = 0;
- int clear_attr = 0;
- int clear_width = 0;
- for (size_t i = 0; i < cells.size; i++) {
- if (cells.items[i].type != kObjectTypeArray) {
- goto error;
- }
- Array cell = cells.items[i].data.array;
-
- if (cell.size < 1 || cell.items[0].type != kObjectTypeString) {
- goto error;
- }
- String sstring = cell.items[0].data.string;
-
- char *schar = sstring.data;
- int repeat = 1;
- if (cell.size >= 2) {
- if (cell.items[1].type != kObjectTypeInteger
- || cell.items[1].data.integer < 0) {
- goto error;
- }
- cur_attr = (int)cell.items[1].data.integer;
- }
-
- if (cell.size >= 3) {
- if (cell.items[2].type != kObjectTypeInteger
- || cell.items[2].data.integer < 0) {
- goto error;
- }
- repeat = (int)cell.items[2].data.integer;
- }
-
- if (i == cells.size - 1 && sstring.size == 1 && sstring.data[0] == ' ' && repeat > 1) {
- clear_width = repeat;
- break;
- }
-
- for (int r = 0; r < repeat; r++) {
- if (j >= buf_size) {
- goto error; // _YIKES_
- }
- STRLCPY(buf_char[j], schar, sizeof(schar_T));
- buf_attr[j++] = cur_attr;
- }
- }
-
- Integer endcol = startcol + (int)j;
- Integer clearcol = endcol + clear_width;
- clear_attr = cur_attr;
-
- ui_call_raw_line(grid, row, startcol, endcol, clearcol, clear_attr, lineflags,
- (const schar_T *)buf_char, (const sattr_T *)buf_attr);
- return;
-
-error:
- ELOG("Error handling ui event 'grid_line'");
+ ui_call_raw_line(grid, row, startcol, endcol, clearcol, g->cur_attr, lineflags,
+ (const schar_T *)grid_line_buf_char, grid_line_buf_attr);
}
diff --git a/src/nvim/ui_client.h b/src/nvim/ui_client.h
index 41d9fa6227..311dafaa0b 100644
--- a/src/nvim/ui_client.h
+++ b/src/nvim/ui_client.h
@@ -2,12 +2,18 @@
#define NVIM_UI_CLIENT_H
#include "nvim/api/private/defs.h"
+#include "nvim/grid_defs.h"
typedef struct {
const char *name;
void (*fn)(Array args);
} UIClientHandler;
+// Temporary buffer for converting a single grid_line event
+EXTERN size_t grid_line_buf_size INIT(= 0);
+EXTERN schar_T *grid_line_buf_char INIT(= NULL);
+EXTERN sattr_T *grid_line_buf_attr INIT(= NULL);
+
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ui_client.h.generated.h"