diff options
Diffstat (limited to 'src')
34 files changed, 614 insertions, 167 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index bd336a2b3d..c1f2842b52 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -111,6 +111,24 @@ Integer nvim_buf_line_count(Buffer buffer, Error *err) /// - byte count of previous contents /// - deleted_codepoints (if `utf_sizes` is true) /// - deleted_codeunits (if `utf_sizes` is true) +/// - on_bytes: lua callback invoked on change. +/// This callback receives more granular information about the +/// change compared to on_lines. +/// Return `true` to detach. +/// Args: +/// - the string "bytes" +/// - buffer handle +/// - b:changedtick +/// - start row of the changed text (zero-indexed) +/// - start column of the changed text +/// - byte offset of the changed text (from the start of +/// the buffer) +/// - old end row of the changed text +/// - old end column of the changed text +/// - old end byte length of the changed text +/// - new end row of the changed text +/// - new end column of the changed text +/// - new end byte length of the changed text /// - on_changedtick: Lua callback invoked on changedtick /// increment without text change. Args: /// - the string "changedtick" @@ -1407,6 +1425,11 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, /// - hl_group : name of the highlight group used to highlight /// this mark. /// - virt_text : virtual text to link to this mark. +/// - virt_text_pos : positioning of virtual text. Possible +/// values: +/// - "eol": right after eol character (default) +/// - "overlay": display over the specified column, without +/// shifting the underlying text. /// - ephemeral : for use with |nvim_set_decoration_provider| /// callbacks. The mark will only be used for the current /// redraw cycle, and not be permantently stored in the @@ -1456,8 +1479,9 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, uint64_t id = 0; int line2 = -1, hl_id = 0; DecorPriority priority = DECOR_PRIORITY_BASE; - colnr_T col2 = 0; + colnr_T col2 = -1; VirtText virt_text = KV_INITIAL_VALUE; + VirtTextPos virt_text_pos = kVTEndOfLine; bool right_gravity = true; bool end_right_gravity = false; bool end_gravity_set = false; @@ -1526,6 +1550,22 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, if (ERROR_SET(err)) { goto error; } + } else if (strequal("virt_text_pos", k.data)) { + if (v->type != kObjectTypeString) { + api_set_error(err, kErrorTypeValidation, + "virt_text_pos is not a String"); + goto error; + } + String str = v->data.string; + if (strequal("eol", str.data)) { + virt_text_pos = kVTEndOfLine; + } else if (strequal("overlay", str.data)) { + virt_text_pos = kVTOverlay; + } else { + api_set_error(err, kErrorTypeValidation, + "virt_text_pos: invalid value"); + goto error; + } } else if (strequal("ephemeral", k.data)) { ephemeral = api_object_to_bool(*v, "ephemeral", false, err); if (ERROR_SET(err)) { @@ -1567,7 +1607,7 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, // Only error out if they try to set end_right_gravity without // setting end_col or end_line - if (line2 == -1 && col2 == 0 && end_gravity_set) { + if (line2 == -1 && col2 == -1 && end_gravity_set) { api_set_error(err, kErrorTypeValidation, "cannot set end_right_gravity " "without setting end_line or end_col"); @@ -1591,30 +1631,28 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, col2 = 0; } + Decoration *decor = NULL, tmp = { 0 }; + + if (kv_size(virt_text) || priority != DECOR_PRIORITY_BASE) { + // TODO(bfredl): this is a bit sketchy. eventually we should + // have predefined decorations for both marks/ephemerals + decor = ephemeral ? &tmp : xcalloc(1, sizeof(*decor)); + decor->hl_id = hl_id; + decor->virt_text = virt_text; + decor->priority = priority; + decor->virt_text_pos = virt_text_pos; + } else if (hl_id) { + decor = decor_hl(hl_id); + } + // TODO(bfredl): synergize these two branches even more if (ephemeral && decor_state.buf == buf) { - int attr_id = hl_id > 0 ? syn_id2attr(hl_id) : 0; - VirtText *vt_allocated = NULL; - if (kv_size(virt_text)) { - vt_allocated = xmalloc(sizeof *vt_allocated); - *vt_allocated = virt_text; - } - decor_add_ephemeral(attr_id, (int)line, (colnr_T)col, - (int)line2, (colnr_T)col2, priority, vt_allocated); + decor_add_ephemeral((int)line, (int)col, line2, col2, decor, 0); } else { if (ephemeral) { api_set_error(err, kErrorTypeException, "not yet implemented"); goto error; } - Decoration *decor = NULL; - if (kv_size(virt_text)) { - decor = xcalloc(1, sizeof(*decor)); - decor->hl_id = hl_id; - decor->virt_text = virt_text; - } else if (hl_id) { - decor = decor_hl(hl_id); - decor->priority = priority; - } id = extmark_set(buf, (uint64_t)ns_id, id, (int)line, (colnr_T)col, line2, col2, decor, right_gravity, @@ -1930,7 +1968,6 @@ static void fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) curwin->w_cursor.lnum += extra; check_cursor_col(); } else if (extra < 0) { - curwin->w_cursor.lnum = lo; check_cursor(); } else { check_cursor_col(); diff --git a/src/nvim/charset.c b/src/nvim/charset.c index be265e3f27..5ad1fe0dfd 100644 --- a/src/nvim/charset.c +++ b/src/nvim/charset.c @@ -708,7 +708,7 @@ int ptr2cells(const char_u *p) /// @return number of character cells. int vim_strsize(char_u *s) { - return vim_strnsize(s, (int)MAXCOL); + return vim_strnsize(s, MAXCOL); } /// Return the number of character cells string "s[len]" will take on the diff --git a/src/nvim/decoration.c b/src/nvim/decoration.c index f3ee42fab1..a1289f202a 100644 --- a/src/nvim/decoration.c +++ b/src/nvim/decoration.c @@ -188,20 +188,21 @@ bool decor_redraw_start(buf_T *buf, int top_row, DecorState *state) goto next_mark; } - int attr_id = decor->hl_id > 0 ? syn_id2attr(decor->hl_id) : 0; - VirtText *vt = kv_size(decor->virt_text) ? &decor->virt_text : NULL; - HlRange range; if (mark.id&MARKTREE_END_FLAG) { - range = (HlRange){ altpos.row, altpos.col, mark.row, mark.col, - attr_id, decor->priority, vt, false }; + decor_add(state, altpos.row, altpos.col, mark.row, mark.col, + decor, false, 0); } else { - range = (HlRange){ mark.row, mark.col, altpos.row, - altpos.col, attr_id, decor->priority, vt, false }; + if (altpos.row == -1) { + altpos.row = mark.row; + altpos.col = mark.col; + } + decor_add(state, mark.row, mark.col, altpos.row, altpos.col, + decor, false, 0); } - hlrange_activate(range, state); next_mark: if (marktree_itr_node_done(state->itr)) { + marktree_itr_next(buf->b_marktree, state->itr); break; } marktree_itr_next(buf->b_marktree, state->itr); @@ -220,41 +221,39 @@ bool decor_redraw_line(buf_T *buf, int row, DecorState *state) return true; // TODO(bfredl): be more precise } -static void hlrange_activate(HlRange range, DecorState *state) +static void decor_add(DecorState *state, int start_row, int start_col, + int end_row, int end_col, Decoration *decor, bool owned, + DecorPriority priority) { - // Get size before preparing the push, to have the number of elements - size_t s = kv_size(state->active); - - kv_pushp(state->active); + int attr_id = decor->hl_id > 0 ? syn_id2attr(decor->hl_id) : 0; - size_t dest_index = 0; + HlRange range = { start_row, start_col, end_row, end_col, + attr_id, MAX(priority, decor->priority), + kv_size(decor->virt_text) ? &decor->virt_text : NULL, + decor->virt_text_pos, + kv_size(decor->virt_text) && owned, -1 }; - // Determine insertion dest_index - while (dest_index < s) { - HlRange item = kv_A(state->active, dest_index); - if (item.priority > range.priority) { + kv_pushp(state->active); + size_t index; + for (index = kv_size(state->active)-1; index > 0; index--) { + HlRange item = kv_A(state->active, index-1); + if (item.priority <= range.priority) { break; } - - dest_index++; - } - - // Splice - for (size_t index = s; index > dest_index; index--) { kv_A(state->active, index) = kv_A(state->active, index-1); } - - // Insert - kv_A(state->active, dest_index) = range; + kv_A(state->active, index) = range; } -int decor_redraw_col(buf_T *buf, int col, DecorState *state) +int decor_redraw_col(buf_T *buf, int col, int virt_col, DecorState *state) { if (col <= state->col_until) { return state->current; } state->col_until = MAXCOL; while (true) { + // TODO(bfredl): check duplicate entry in "intersection" + // branch mtmark_t mark = marktree_itr_current(state->itr); if (mark.row < 0 || mark.row > state->row) { break; @@ -278,6 +277,11 @@ int decor_redraw_col(buf_T *buf, int col, DecorState *state) } Decoration *decor = item->decor; + if (endpos.row == -1) { + endpos.row = mark.row; + endpos.col = mark.col; + } + if (endpos.row < mark.row || (endpos.row == mark.row && endpos.col <= mark.col)) { if (!kv_size(decor->virt_text)) { @@ -285,12 +289,8 @@ int decor_redraw_col(buf_T *buf, int col, DecorState *state) } } - int attr_id = decor->hl_id > 0 ? syn_id2attr(decor->hl_id) : 0; - VirtText *vt = kv_size(decor->virt_text) ? &decor->virt_text : NULL; - hlrange_activate((HlRange){ mark.row, mark.col, - endpos.row, endpos.col, - attr_id, decor->priority, - vt, false }, state); + decor_add(state, mark.row, mark.col, endpos.row, endpos.col, + decor, false, 0); next_mark: marktree_itr_next(buf->b_marktree, state->itr); @@ -310,7 +310,7 @@ next_mark: if (item.start_row < state->row || (item.start_row == state->row && item.start_col <= col)) { active = true; - if (item.end_row == state->row) { + if (item.end_row == state->row && item.end_col > col) { state->col_until = MIN(state->col_until, item.end_col-1); } } else { @@ -322,8 +322,12 @@ next_mark: if (active && item.attr_id > 0) { attr = hl_combine_attr(attr, item.attr_id); } + if ((item.start_row == state->row && item.start_col <= col) + && item.virt_text && item.virt_col == -1) { + item.virt_col = virt_col; + } if (keep) { - kv_A(state->active, j++) = kv_A(state->active, i); + kv_A(state->active, j++) = item; } else if (item.virt_text_owned) { clear_virttext(item.virt_text); xfree(item.virt_text); @@ -341,21 +345,20 @@ void decor_redraw_end(DecorState *state) VirtText *decor_redraw_virt_text(buf_T *buf, DecorState *state) { - decor_redraw_col(buf, MAXCOL, state); + decor_redraw_col(buf, MAXCOL, MAXCOL, state); for (size_t i = 0; i < kv_size(state->active); i++) { HlRange item = kv_A(state->active, i); - if (item.start_row == state->row && item.virt_text) { + if (item.start_row == state->row && item.virt_text + && item.virt_text_pos == kVTEndOfLine) { return item.virt_text; } } return NULL; } -void decor_add_ephemeral(int attr_id, int start_row, int start_col, - int end_row, int end_col, DecorPriority priority, - VirtText *virt_text) +void decor_add_ephemeral(int start_row, int start_col, int end_row, int end_col, + Decoration *decor, DecorPriority priority) { -hlrange_activate(((HlRange){ start_row, start_col, end_row, end_col, attr_id, - priority, virt_text, virt_text != NULL }), - &decor_state); + decor_add(&decor_state, start_row, start_col, end_row, end_col, decor, true, + priority); } diff --git a/src/nvim/decoration.h b/src/nvim/decoration.h index 2533a641dd..47bd9abbc3 100644 --- a/src/nvim/decoration.h +++ b/src/nvim/decoration.h @@ -18,10 +18,16 @@ typedef kvec_t(VirtTextChunk) VirtText; typedef uint16_t DecorPriority; #define DECOR_PRIORITY_BASE 0x1000 +typedef enum { + kVTEndOfLine, + kVTOverlay, +} VirtTextPos; + struct Decoration { int hl_id; // highlight group VirtText virt_text; + VirtTextPos virt_text_pos; // TODO(bfredl): style, signs, etc DecorPriority priority; bool shared; // shared decoration, don't free @@ -35,7 +41,9 @@ typedef struct { int attr_id; DecorPriority priority; VirtText *virt_text; + VirtTextPos virt_text_pos; bool virt_text_owned; + int virt_col; } HlRange; typedef struct { diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 100e88e261..53717229f6 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -1627,11 +1627,11 @@ static void init_prompt(int cmdchar_todo) ml_append(curbuf->b_ml.ml_line_count, prompt, 0, false); } curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); changed_bytes(curbuf->b_ml.ml_line_count, 0); } if (cmdchar_todo == 'A') { - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); } if (cmdchar_todo == 'I' || curwin->w_cursor.col <= (int)STRLEN(prompt)) { curwin->w_cursor.col = STRLEN(prompt); @@ -6246,9 +6246,10 @@ void auto_format( if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { // "cannot happen" curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - coladvance((colnr_T)MAXCOL); - } else + coladvance(MAXCOL); + } else { check_cursor_col(); + } // Insert mode: If the cursor is now after the end of the line while it // previously wasn't, the line was broken. Because of the rule above we @@ -8432,8 +8433,8 @@ static void ins_left(void) // if 'whichwrap' set for cursor in insert mode may go to previous line. // always break undo when moving upwards/downwards, else undo may break start_arrow(&tpos); - --(curwin->w_cursor.lnum); - coladvance((colnr_T)MAXCOL); + curwin->w_cursor.lnum--; + coladvance(MAXCOL); curwin->w_set_curswant = true; // so we stay at the end } else { vim_beep(BO_CRSR); @@ -8467,7 +8468,7 @@ static void ins_end(int c) tpos = curwin->w_cursor; if (c == K_C_END) curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); curwin->w_curswant = MAXCOL; start_arrow(&tpos); diff --git a/src/nvim/eval.c b/src/nvim/eval.c index d07618d2c0..6d97310c1c 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -176,7 +176,6 @@ static struct vimvar { VV(VV_DYING, "dying", VAR_NUMBER, VV_RO), VV(VV_EXCEPTION, "exception", VAR_STRING, VV_RO), VV(VV_THROWPOINT, "throwpoint", VAR_STRING, VV_RO), - VV(VV_STDERR, "stderr", VAR_NUMBER, VV_RO), VV(VV_REG, "register", VAR_STRING, VV_RO), VV(VV_CMDBANG, "cmdbang", VAR_NUMBER, VV_RO), VV(VV_INSERTMODE, "insertmode", VAR_STRING, VV_RO), @@ -211,13 +210,9 @@ static struct vimvar { VV(VV_OPTION_OLD, "option_old", VAR_STRING, VV_RO), VV(VV_OPTION_TYPE, "option_type", VAR_STRING, VV_RO), VV(VV_ERRORS, "errors", VAR_LIST, 0), - VV(VV_MSGPACK_TYPES, "msgpack_types", VAR_DICT, VV_RO), - VV(VV_EVENT, "event", VAR_DICT, VV_RO), VV(VV_FALSE, "false", VAR_BOOL, VV_RO), VV(VV_TRUE, "true", VAR_BOOL, VV_RO), VV(VV_NULL, "null", VAR_SPECIAL, VV_RO), - VV(VV__NULL_LIST, "_null_list", VAR_LIST, VV_RO), - VV(VV__NULL_DICT, "_null_dict", VAR_DICT, VV_RO), VV(VV_VIM_DID_ENTER, "vim_did_enter", VAR_NUMBER, VV_RO), VV(VV_TESTING, "testing", VAR_NUMBER, 0), VV(VV_TYPE_NUMBER, "t_number", VAR_NUMBER, VV_RO), @@ -227,10 +222,16 @@ static struct vimvar { VV(VV_TYPE_DICT, "t_dict", VAR_NUMBER, VV_RO), VV(VV_TYPE_FLOAT, "t_float", VAR_NUMBER, VV_RO), VV(VV_TYPE_BOOL, "t_bool", VAR_NUMBER, VV_RO), + VV(VV_EVENT, "event", VAR_DICT, VV_RO), VV(VV_ECHOSPACE, "echospace", VAR_NUMBER, VV_RO), + VV(VV_ARGV, "argv", VAR_LIST, VV_RO), VV(VV_EXITING, "exiting", VAR_NUMBER, VV_RO), + // Neovim + VV(VV_STDERR, "stderr", VAR_NUMBER, VV_RO), + VV(VV_MSGPACK_TYPES, "msgpack_types", VAR_DICT, VV_RO), + VV(VV__NULL_LIST, "_null_list", VAR_LIST, VV_RO), + VV(VV__NULL_DICT, "_null_dict", VAR_DICT, VV_RO), VV(VV_LUA, "lua", VAR_PARTIAL, VV_RO), - VV(VV_ARGV, "argv", VAR_LIST, VV_RO), }; #undef VV diff --git a/src/nvim/eval.h b/src/nvim/eval.h index c1891758ea..4f03d5d259 100644 --- a/src/nvim/eval.h +++ b/src/nvim/eval.h @@ -105,7 +105,6 @@ typedef enum { VV_DYING, VV_EXCEPTION, VV_THROWPOINT, - VV_STDERR, VV_REG, VV_CMDBANG, VV_INSERTMODE, @@ -140,13 +139,9 @@ typedef enum { VV_OPTION_OLD, VV_OPTION_TYPE, VV_ERRORS, - VV_MSGPACK_TYPES, - VV_EVENT, VV_FALSE, VV_TRUE, VV_NULL, - VV__NULL_LIST, // List with NULL value. For test purposes only. - VV__NULL_DICT, // Dictionary with NULL value. For test purposes only. VV_VIM_DID_ENTER, VV_TESTING, VV_TYPE_NUMBER, @@ -156,10 +151,16 @@ typedef enum { VV_TYPE_DICT, VV_TYPE_FLOAT, VV_TYPE_BOOL, + VV_EVENT, VV_ECHOSPACE, + VV_ARGV, VV_EXITING, + // Neovim + VV_STDERR, + VV_MSGPACK_TYPES, + VV__NULL_LIST, // List with NULL value. For test purposes only. + VV__NULL_DICT, // Dictionary with NULL value. For test purposes only. VV_LUA, - VV_ARGV, } VimVarIndex; /// All recognized msgpack types diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 952fa35b83..eac0feafcf 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -63,6 +63,7 @@ return { chanclose={args={1, 2}}, chansend={args=2}, char2nr={args={1, 2}}, + charidx={args={2, 3}}, cindent={args=1}, clearmatches={args={0, 1}}, col={args=1}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index d54e49bbd4..8c8e0d568b 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -940,6 +940,52 @@ static void f_char2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr) (const char_u *)tv_get_string(&argvars[0])); } +// "charidx()" function +static void f_charidx(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->vval.v_number = -1; + + if (argvars[0].v_type != VAR_STRING + || argvars[1].v_type != VAR_NUMBER + || (argvars[2].v_type != VAR_UNKNOWN + && argvars[2].v_type != VAR_NUMBER)) { + EMSG(_(e_invarg)); + return; + } + + const char *str = tv_get_string_chk(&argvars[0]); + varnumber_T idx = tv_get_number_chk(&argvars[1], NULL); + if (str == NULL || idx < 0) { + return; + } + int countcc = 0; + if (argvars[2].v_type != VAR_UNKNOWN) { + countcc = (int)tv_get_number(&argvars[2]); + } + if (countcc < 0 || countcc > 1) { + EMSG(_(e_invarg)); + return; + } + + int (*ptr2len)(const char_u *); + if (countcc) { + ptr2len = utf_ptr2len; + } else { + ptr2len = utfc_ptr2len; + } + + const char *p; + int len; + for (p = str, len = 0; p <= str + idx; len++) { + if (*p == NUL) { + return; + } + p += ptr2len((const char_u *)p); + } + + rettv->vval.v_number = len > 0 ? len - 1 : 0; +} + /* * "cindent(lnum)" function */ @@ -4085,7 +4131,9 @@ static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr) #ifdef _WIN64 "win64", #endif +#ifndef CASE_INSENSITIVE_FILENAME "fname_case", +#endif #ifdef HAVE_ACL "acl", #endif diff --git a/src/nvim/eval/typval.h b/src/nvim/eval/typval.h index d8ede1e3ba..6fcb01aace 100644 --- a/src/nvim/eval/typval.h +++ b/src/nvim/eval/typval.h @@ -315,6 +315,7 @@ struct ufunc { int uf_calls; ///< nr of active calls bool uf_cleared; ///< func_clear() was already called garray_T uf_args; ///< arguments + garray_T uf_def_args; ///< default argument expressions garray_T uf_lines; ///< function lines int uf_profiling; ///< true when func is being profiled int uf_prof_initialized; diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index 70c998ef39..689d05e079 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -67,7 +67,7 @@ void func_init(void) /// Get function arguments. static int get_function_args(char_u **argp, char_u endchar, garray_T *newargs, - int *varargs, bool skip) + int *varargs, garray_T *default_args, bool skip) { bool mustend = false; char_u *arg = *argp; @@ -78,12 +78,16 @@ static int get_function_args(char_u **argp, char_u endchar, garray_T *newargs, if (newargs != NULL) { ga_init(newargs, (int)sizeof(char_u *), 3); } + if (default_args != NULL) { + ga_init(default_args, (int)sizeof(char_u *), 3); + } if (varargs != NULL) { *varargs = false; } // Isolate the arguments: "arg1, arg2, ...)" + bool any_default = false; while (*p != endchar) { if (p[0] == '.' && p[1] == '.' && p[2] == '.') { if (varargs != NULL) { @@ -123,6 +127,38 @@ static int get_function_args(char_u **argp, char_u endchar, garray_T *newargs, *p = c; } + if (*skipwhite(p) == '=' && default_args != NULL) { + typval_T rettv; + + any_default = true; + p = skipwhite(p) + 1; + p = skipwhite(p); + char_u *expr = p; + if (eval1(&p, &rettv, false) != FAIL) { + ga_grow(default_args, 1); + + // trim trailing whitespace + while (p > expr && ascii_iswhite(p[-1])) { + p--; + } + c = *p; + *p = NUL; + expr = vim_strsave(expr); + if (expr == NULL) { + *p = c; + goto err_ret; + } + ((char_u **)(default_args->ga_data)) + [default_args->ga_len] = expr; + default_args->ga_len++; + *p = c; + } else { + mustend = true; + } + } else if (any_default) { + EMSG(_("E989: Non-default argument follows default argument")); + mustend = true; + } if (*p == ',') { p++; } else { @@ -149,6 +185,9 @@ err_ret: if (newargs != NULL) { ga_clear_strings(newargs); } + if (default_args != NULL) { + ga_clear_strings(default_args); + } return FAIL; } @@ -195,7 +234,7 @@ int get_lambda_tv(char_u **arg, typval_T *rettv, bool evaluate) bool eval_lavars = false; // First, check if this is a lambda expression. "->" must exists. - ret = get_function_args(&start, '-', NULL, NULL, true); + ret = get_function_args(&start, '-', NULL, NULL, NULL, true); if (ret == FAIL || *start != '>') { return NOTDONE; } @@ -207,7 +246,7 @@ int get_lambda_tv(char_u **arg, typval_T *rettv, bool evaluate) pnewargs = NULL; } *arg = skipwhite(*arg + 1); - ret = get_function_args(arg, '-', pnewargs, &varargs, false); + ret = get_function_args(arg, '-', pnewargs, &varargs, NULL, false); if (ret == FAIL || **arg != '>') { goto errret; } @@ -259,6 +298,7 @@ int get_lambda_tv(char_u **arg, typval_T *rettv, bool evaluate) STRCPY(fp->uf_name, name); hash_add(&func_hashtab, UF2HIKEY(fp)); fp->uf_args = newargs; + ga_init(&fp->uf_def_args, (int)sizeof(char_u *), 1); fp->uf_lines = newlines; if (current_funccal != NULL && eval_lavars) { flags |= FC_CLOSURE; @@ -715,6 +755,7 @@ static bool func_remove(ufunc_T *fp) static void func_clear_items(ufunc_T *fp) { ga_clear_strings(&(fp->uf_args)); + ga_clear_strings(&(fp->uf_def_args)); ga_clear_strings(&(fp->uf_lines)); if (fp->uf_cb_free != NULL) { @@ -857,12 +898,13 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, } // Init a: variables, unless none found (in lambda). - // Set a:0 to "argcount". + // Set a:0 to "argcount" less number of named arguments, if >= 0. // Set a:000 to a list with room for the "..." arguments. init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE); if ((fp->uf_flags & FC_NOARGS) == 0) { add_nr_var(&fc->l_avars, (dictitem_T *)&fc->fixvar[fixvar_idx++], "0", - (varnumber_T)(argcount - fp->uf_args.ga_len)); + (varnumber_T)(argcount >= fp->uf_args.ga_len + ? argcount - fp->uf_args.ga_len : 0)); } fc->l_avars.dv_lock = VAR_FIXED; if ((fp->uf_flags & FC_NOARGS) == 0) { @@ -892,8 +934,11 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, add_nr_var(&fc->l_avars, (dictitem_T *)&fc->fixvar[fixvar_idx++], "lastline", (varnumber_T)lastline); } - for (int i = 0; i < argcount; i++) { + bool default_arg_err = false; + for (int i = 0; i < argcount || i < fp->uf_args.ga_len; i++) { bool addlocal = false; + bool isdefault = false; + typval_T def_rettv; ai = i - fp->uf_args.ga_len; if (ai < 0) { @@ -902,6 +947,21 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, if (islambda) { addlocal = true; } + + // evaluate named argument default expression + isdefault = ai + fp->uf_def_args.ga_len >= 0 && i >= argcount; + if (isdefault) { + char_u *default_expr = NULL; + def_rettv.v_type = VAR_NUMBER; + def_rettv.vval.v_number = -1; + + default_expr = ((char_u **)(fp->uf_def_args.ga_data)) + [ai + fp->uf_def_args.ga_len]; + if (eval1(&default_expr, &def_rettv, true) == FAIL) { + default_arg_err = true; + break; + } + } } else { if ((fp->uf_flags & FC_NOARGS) != 0) { // Bail out if no a: arguments used (in lambda). @@ -922,7 +982,7 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, // Note: the values are copied directly to avoid alloc/free. // "argvars" must have VAR_FIXED for v_lock. - v->di_tv = argvars[i]; + v->di_tv = isdefault ? def_rettv : argvars[i]; v->di_tv.v_lock = VAR_FIXED; if (addlocal) { @@ -1046,7 +1106,9 @@ void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, save_did_emsg = did_emsg; did_emsg = FALSE; - if (islambda) { + if (default_arg_err && (fp->uf_flags & FC_ABORT)) { + did_emsg = true; + } else if (islambda) { char_u *p = *(char_u **)fp->uf_lines.ga_data + 7; // A Lambda always has the command "return {expr}". It is much faster @@ -1490,7 +1552,7 @@ call_func( if (fp->uf_flags & FC_RANGE) { *doesrange = true; } - if (argcount < fp->uf_args.ga_len) { + if (argcount < fp->uf_args.ga_len - fp->uf_def_args.ga_len) { error = ERROR_TOOFEW; } else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len) { error = ERROR_TOOMANY; @@ -1573,6 +1635,11 @@ static void list_func_head(ufunc_T *fp, int indent, bool force) msg_puts(", "); } msg_puts((const char *)FUNCARG(fp, j)); + if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len) { + msg_puts(" = "); + msg_puts(((char **)(fp->uf_def_args.ga_data)) + [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]); + } } if (fp->uf_varargs) { if (j) { @@ -1836,6 +1903,7 @@ void ex_function(exarg_T *eap) char_u *arg; char_u *line_arg = NULL; garray_T newargs; + garray_T default_args; garray_T newlines; int varargs = false; int flags = 0; @@ -2039,7 +2107,8 @@ void ex_function(exarg_T *eap) } } - if (get_function_args(&p, ')', &newargs, &varargs, eap->skip) == FAIL) { + if (get_function_args(&p, ')', &newargs, &varargs, + &default_args, eap->skip) == FAIL) { goto errret_2; } @@ -2458,6 +2527,7 @@ void ex_function(exarg_T *eap) fp->uf_refcount = 1; } fp->uf_args = newargs; + fp->uf_def_args = default_args; fp->uf_lines = newlines; if ((flags & FC_CLOSURE) != 0) { register_closure(fp); @@ -2480,6 +2550,7 @@ void ex_function(exarg_T *eap) erret: ga_clear_strings(&newargs); + ga_clear_strings(&default_args); errret_2: ga_clear_strings(&newlines); ret_free: diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index b220b034dd..103c081143 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -4198,7 +4198,7 @@ skip: // when interactive leave cursor on the match if (!subflags.do_ask) { if (endcolumn) { - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); } else { beginline(BL_WHITE | BL_FIX); } @@ -5020,7 +5020,7 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, if (keep_lang) { flags |= TAG_KEEP_LANG; } - if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK + if (find_tags(IObuff, num_matches, matches, flags, MAXCOL, NULL) == OK && *num_matches > 0) { /* Sort the matches found on the heuristic number that is after the * tag name. */ diff --git a/src/nvim/ex_cmds.lua b/src/nvim/ex_cmds.lua index 10dd7d68ca..2965ea7496 100644 --- a/src/nvim/ex_cmds.lua +++ b/src/nvim/ex_cmds.lua @@ -2521,8 +2521,8 @@ module.cmds = { }, { command='source', - flags=bit.bor(BANG, FILE1, TRLBAR, SBOXOK, CMDWIN), - addr_type='ADDR_NONE', + flags=bit.bor(RANGE, DFLALL, WHOLEFOLD, BANG, FILE1, TRLBAR, SBOXOK, CMDWIN), + addr_type='ADDR_LINES', func='ex_source', }, { diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index bb53f4d373..c4c18c4324 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -29,6 +29,7 @@ #include "nvim/getchar.h" #include "nvim/mark.h" #include "nvim/mbyte.h" +#include "nvim/memline.h" #include "nvim/message.h" #include "nvim/misc1.h" #include "nvim/garray.h" @@ -2526,7 +2527,7 @@ void ex_pyxdo(exarg_T *eap) } } -/// ":source {fname}" +/// ":source [{fname}]" void ex_source(exarg_T *eap) { cmd_source(eap->arg, eap); @@ -2535,7 +2536,7 @@ void ex_source(exarg_T *eap) static void cmd_source(char_u *fname, exarg_T *eap) { if (*fname == NUL) { - EMSG(_(e_argreq)); + cmd_source_buffer(eap); } else if (eap != NULL && eap->forceit) { // ":source!": read Normal mode commands // Need to execute the commands directly. This is required at least @@ -2553,6 +2554,37 @@ static void cmd_source(char_u *fname, exarg_T *eap) } } +typedef struct { + linenr_T curr_lnum; + const linenr_T final_lnum; +} GetBufferLineCookie; + +/// Get one line from the current selection in the buffer. +/// Called by do_cmdline() when it's called from cmd_source_buffer(). +/// +/// @return pointer to allocated line, or NULL for end-of-file or +/// some error. +static char_u *get_buffer_line(int c, void *cookie, int indent, bool do_concat) +{ + GetBufferLineCookie *p = cookie; + if (p->curr_lnum > p->final_lnum) { + return NULL; + } + char_u *curr_line = ml_get(p->curr_lnum); + p->curr_lnum++; + return (char_u *)xstrdup((const char *)curr_line); +} + +static void cmd_source_buffer(exarg_T *eap) +{ + GetBufferLineCookie cookie = { + .curr_lnum = eap->line1, + .final_lnum = eap->line2, + }; + source_using_linegetter((void *)&cookie, get_buffer_line, + ":source (no file)"); +} + /// ":source" and associated commands. /// /// @return address holding the next breakpoint line for a source cookie @@ -2624,10 +2656,9 @@ static char_u *get_str_line(int c, void *cookie, int indent, bool do_concat) return (char_u *)xstrdup(buf); } -/// Executes lines in `src` as Ex commands. -/// -/// @see do_source() -int do_source_str(const char *cmd, const char *traceback_name) +static int source_using_linegetter(void *cookie, + LineGetter fgetline, + const char *traceback_name) { char_u *save_sourcing_name = sourcing_name; linenr_T save_sourcing_lnum = sourcing_lnum; @@ -2642,22 +2673,33 @@ int do_source_str(const char *cmd, const char *traceback_name) } sourcing_lnum = 0; - GetStrLineCookie cookie = { - .buf = (char_u *)cmd, - .offset = 0, - }; const sctx_T save_current_sctx = current_sctx; current_sctx.sc_sid = SID_STR; current_sctx.sc_seq = 0; current_sctx.sc_lnum = save_sourcing_lnum; - int retval = do_cmdline(NULL, get_str_line, (void *)&cookie, + funccal_entry_T entry; + save_funccal(&entry); + int retval = do_cmdline(NULL, fgetline, cookie, DOCMD_VERBOSE | DOCMD_NOWAIT | DOCMD_REPEAT); - current_sctx = save_current_sctx; sourcing_lnum = save_sourcing_lnum; sourcing_name = save_sourcing_name; + current_sctx = save_current_sctx; + restore_funccal(); return retval; } +/// Executes lines in `src` as Ex commands. +/// +/// @see do_source() +int do_source_str(const char *cmd, const char *traceback_name) +{ + GetStrLineCookie cookie = { + .buf = (char_u *)cmd, + .offset = 0, + }; + return source_using_linegetter((void *)&cookie, get_str_line, traceback_name); +} + /// Reads the file `fname` and executes its lines as Ex commands. /// /// This function may be called recursively! @@ -2982,6 +3024,7 @@ void scriptnames_slash_adjust(void) # endif /// Get a pointer to a script name. Used for ":verbose set". +/// Message appended to "Last set from " char_u *get_scriptname(LastSet last_set, bool *should_free) { *should_free = false; @@ -2997,6 +3040,8 @@ char_u *get_scriptname(LastSet last_set, bool *should_free) return (char_u *)_("environment variable"); case SID_ERROR: return (char_u *)_("error handler"); + case SID_WINLAYOUT: + return (char_u *)_("changed window size"); case SID_LUA: return (char_u *)_("Lua"); case SID_API_CLIENT: diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index c66ae13f53..d470bfb418 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -6774,6 +6774,6 @@ static void set_search_match(pos_T *t) t->col = search_match_endcol; if (t->lnum > curbuf->b_ml.ml_line_count) { t->lnum = curbuf->b_ml.ml_line_count; - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); } } diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 22f06941aa..7b8e809de7 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -180,6 +180,11 @@ EXTERN int compl_cont_status INIT(= 0); # define CONT_LOCAL 32 // for ctrl_x_mode 0, ^X^P/^X^N do a local // expansion, (eg use complete=.) +EXTERN char_u *edit_submode INIT(= NULL); // msg for CTRL-X submode +EXTERN char_u *edit_submode_pre INIT(= NULL); // prepended to edit_submode +EXTERN char_u *edit_submode_extra INIT(= NULL); // appended to edit_submode +EXTERN hlf_T edit_submode_highl; // highl. method for extra info + // state for putting characters in the message area EXTERN int cmdmsg_rl INIT(= false); // cmdline is drawn right to left EXTERN int msg_col; @@ -328,9 +333,10 @@ EXTERN int garbage_collect_at_exit INIT(= false); #define SID_ENV -4 // for sourcing environment variable #define SID_ERROR -5 // option was reset because of an error #define SID_NONE -6 // don't set scriptID -#define SID_LUA -7 // for Lua scripts/chunks -#define SID_API_CLIENT -8 // for API clients -#define SID_STR -9 // for sourcing a string +#define SID_WINLAYOUT -7 // changing window size +#define SID_LUA -8 // for Lua scripts/chunks +#define SID_API_CLIENT -9 // for API clients +#define SID_STR -10 // for sourcing a string // Script CTX being sourced or was sourced to define the current function. EXTERN sctx_T current_sctx INIT(= { 0 COMMA 0 COMMA 0 }); @@ -639,10 +645,6 @@ EXTERN int arrow_used; // Normally false, set to true after // to call u_sync() EXTERN bool ins_at_eol INIT(= false); // put cursor after eol when // restarting edit after CTRL-O -EXTERN char_u *edit_submode INIT(= NULL); // msg for CTRL-X submode -EXTERN char_u *edit_submode_pre INIT(= NULL); // prepended to edit_submode -EXTERN char_u *edit_submode_extra INIT(= NULL); // appended to edit_submode -EXTERN hlf_T edit_submode_highl; // highl. method for extra info EXTERN int no_abbr INIT(= true); // true when no abbreviations loaded diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 31dc6b3649..293a4d01db 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -4142,7 +4142,7 @@ void goto_byte(long cnt) if (lnum < 1) { // past the end curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; curwin->w_curswant = MAXCOL; - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); } else { curwin->w_cursor.lnum = lnum; curwin->w_cursor.col = (colnr_T)boff; diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c index fcffe64595..34c43da0f7 100644 --- a/src/nvim/misc1.c +++ b/src/nvim/misc1.c @@ -412,7 +412,7 @@ int plines_win_nofold(win_T *wp, linenr_T lnum) s = ml_get_buf(wp->w_buffer, lnum, FALSE); if (*s == NUL) /* empty line */ return 1; - col = win_linetabsize(wp, s, (colnr_T)MAXCOL); + col = win_linetabsize(wp, s, MAXCOL); // If list mode is on, then the '$' at the end of the line may take up one // extra column. diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 8f22243348..d5c92bc3e6 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -5225,16 +5225,13 @@ static void nv_left(cmdarg_T *cap) * 'h' wraps to previous line if 'whichwrap' has 'h'. * CURS_LEFT wraps to previous line if 'whichwrap' has '<'. */ - if ( (((cap->cmdchar == K_BS - || cap->cmdchar == Ctrl_H) - && vim_strchr(p_ww, 'b') != NULL) - || (cap->cmdchar == 'h' - && vim_strchr(p_ww, 'h') != NULL) - || (cap->cmdchar == K_LEFT - && vim_strchr(p_ww, '<') != NULL)) - && curwin->w_cursor.lnum > 1) { - --(curwin->w_cursor.lnum); - coladvance((colnr_T)MAXCOL); + if ((((cap->cmdchar == K_BS || cap->cmdchar == Ctrl_H) + && vim_strchr(p_ww, 'b') != NULL) + || (cap->cmdchar == 'h' && vim_strchr(p_ww, 'h') != NULL) + || (cap->cmdchar == K_LEFT && vim_strchr(p_ww, '<') != NULL)) + && curwin->w_cursor.lnum > 1) { + curwin->w_cursor.lnum--; + coladvance(MAXCOL); curwin->w_set_curswant = true; // When the NL before the first char has to be deleted we @@ -5792,16 +5789,20 @@ static void nv_percent(cmdarg_T *cap) } else { cap->oap->motion_type = kMTLineWise; setpcmark(); - /* Round up, so CTRL-G will give same value. Watch out for a - * large line count, the line number must not go negative! */ - if (curbuf->b_ml.ml_line_count > 1000000) + // Round up, so 'normal 100%' always jumps at the line line. + // Beyond 21474836 lines, (ml_line_count * 100 + 99) would + // overflow on 32-bits, so use a formula with less accuracy + // to avoid overflows. + if (curbuf->b_ml.ml_line_count >= 21474836) { curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L) / 100L * cap->count0; - else + } else { curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count * cap->count0 + 99L) / 100L; - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) + } + if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; + } beginline(BL_SOL | BL_FIX); } } else { // "%" : go to matching paren @@ -6467,7 +6468,7 @@ static void nv_visual(cmdarg_T *cap) } if (resel_VIsual_vcol == MAXCOL) { curwin->w_curswant = MAXCOL; - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); } else if (VIsual_mode == Ctrl_V) { validate_virtcol(); assert(cap->count0 >= INT_MIN && cap->count0 <= INT_MAX); @@ -7570,6 +7571,12 @@ static void nv_esc(cmdarg_T *cap) got_int = false; /* don't stop executing autocommands et al. */ return; } + } else if (cmdwin_type != 0 && ex_normal_busy) { + // When :normal runs out of characters while in the command line window + // vgetorpeek() will return ESC. Exit the cmdline window to break the + // loop. + cmdwin_result = K_IGNORE; + return; } if (VIsual_active) { @@ -7600,7 +7607,7 @@ void set_cursor_for_append_to_line(void) // Pretend Insert mode here to allow the cursor on the // character past the end of the line State = INSERT; - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); State = save_State; } else { curwin->w_cursor.col += (colnr_T)STRLEN(get_cursor_pos_ptr()); @@ -7988,7 +7995,7 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) * line. */ if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - coladvance((colnr_T)MAXCOL); + coladvance(MAXCOL); } } auto_format(false, true); diff --git a/src/nvim/ops.c b/src/nvim/ops.c index ea52d6a3d3..3038fad894 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -4305,11 +4305,12 @@ format_lines( * tabs and spaces, according to current options */ (void)set_indent(get_indent(), SIN_CHANGED); - /* put cursor on last non-space */ - State = NORMAL; /* don't go past end-of-line */ - coladvance((colnr_T)MAXCOL); - while (curwin->w_cursor.col && ascii_isspace(gchar_cursor())) + // put cursor on last non-space + State = NORMAL; // don't go past end-of-line + coladvance(MAXCOL); + while (curwin->w_cursor.col && ascii_isspace(gchar_cursor())) { dec_cursor(); + } /* do the formatting, without 'showmode' */ State = INSERT; /* for open_line() */ diff --git a/src/nvim/path.c b/src/nvim/path.c index 2de7e00ddb..3e1713fbdd 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -342,7 +342,7 @@ int path_fnamencmp(const char *const fname1, const char *const fname2, p1 += utfc_ptr2len((const char_u *)p1); p2 += utfc_ptr2len((const char_u *)p2); } - return c1 - c2; + return p_fic ? CH_FOLD(c1) - CH_FOLD(c2) : c1 - c2; #else if (p_fic) { return mb_strnicmp((const char_u *)fname1, (const char_u *)fname2, len); diff --git a/src/nvim/pos.h b/src/nvim/pos.h index 8e86ea08c5..b7c4b6ef92 100644 --- a/src/nvim/pos.h +++ b/src/nvim/pos.h @@ -1,6 +1,9 @@ #ifndef NVIM_POS_H #define NVIM_POS_H +// for INT_MAX, LONG_MAX et al. +#include <limits.h> + typedef long linenr_T; // line number type /// Format used to print values which have linenr_T type #define PRIdLINENR "ld" @@ -12,8 +15,8 @@ typedef int colnr_T; /// Maximal (invalid) line number enum { MAXLNUM = 0x7fffffff }; -/// Maximal column number, 31 bits -enum { MAXCOL = 0x7fffffff }; +/// Maximal column number +enum { MAXCOL = INT_MAX }; // Minimum line number enum { MINLNUM = 1 }; // minimum column number diff --git a/src/nvim/screen.c b/src/nvim/screen.c index c16fe46955..20e3cc0a2e 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -143,7 +143,7 @@ long tab_page_click_defs_size = 0; // for line_putchar. Contains the state that needs to be remembered from // putting one character to the next. typedef struct { - const char_u *p; + const char *p; int prev_c; // previous Arabic character int prev_c1; // first composing char for prev_c } LineState; @@ -1857,7 +1857,7 @@ static int compute_foldcolumn(win_T *wp, int col) /// Handles composing chars and arabic shaping state. static int line_putchar(LineState *s, schar_T *dest, int maxcells, bool rl) { - const char_u *p = s->p; + const char_u *p = (char_u *)s->p; int cells = utf_ptr2cells(p); int c_len = utfc_ptr2len(p); int u8c, u8cc[MAX_MCO]; @@ -2870,6 +2870,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, && vcol >= (long)wp->w_virtcol) || (number_only && draw_state > WL_NR)) && filler_todo <= 0) { + draw_virt_text(buf, &col, grid->Columns); grid_put_linebuf(grid, row, 0, col, -grid->Columns, wp->w_p_rl, wp, wp->w_hl_attr_normal, false); // Pretend we have finished updating the window. Except when @@ -3397,7 +3398,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, } if (has_decor && v > 0) { - int extmark_attr = decor_redraw_col(wp->w_buffer, (colnr_T)v-1, + int extmark_attr = decor_redraw_col(wp->w_buffer, (colnr_T)v-1, off, &decor_state); if (extmark_attr != 0) { if (!attr_pri) { @@ -3919,7 +3920,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int i; size_t virt_pos = 0; - LineState s = LINE_STATE((char_u *)""); + LineState s = LINE_STATE(""); int virt_attr = 0; // Make sure alignment is the same regardless @@ -3963,7 +3964,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, if (do_virttext && !delay_virttext) { if (*s.p == NUL) { if (virt_pos < virt_text.size) { - s.p = (char_u *)kv_A(virt_text, virt_pos).text; + s.p = kv_A(virt_text, virt_pos).text; int hl_id = kv_A(virt_text, virt_pos).hl_id; virt_attr = hl_id > 0 ? syn_id2attr(hl_id) : 0; virt_pos++; @@ -4029,6 +4030,8 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, col += n; } } + + draw_virt_text(buf, &col, grid->Columns); grid_put_linebuf(grid, row, 0, col, grid->Columns, wp->w_p_rl, wp, wp->w_hl_attr_normal, false); row++; @@ -4247,7 +4250,10 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, && (grid->Columns == Columns // Window spans the width of the screen, || ui_has(kUIMultigrid)) // or has dedicated grid. && !wp->w_p_rl; // Not right-to-left. - grid_put_linebuf(grid, row, 0, col - boguscols, grid->Columns, wp->w_p_rl, + + int draw_col = col - boguscols; + draw_virt_text(buf, &draw_col, grid->Columns); + grid_put_linebuf(grid, row, 0, draw_col, grid->Columns, wp->w_p_rl, wp, wp->w_hl_attr_normal, wrap); if (wrap) { ScreenGrid *current_grid = grid; @@ -4323,6 +4329,43 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, return row; } +void draw_virt_text(buf_T *buf, int *end_col, int max_col) +{ + DecorState *state = &decor_state; + for (size_t i = 0; i < kv_size(state->active); i++) { + HlRange *item = &kv_A(state->active, i); + if (item->start_row == state->row && item->virt_text + && item->virt_text_pos == kVTOverlay + && item->virt_col >= 0) { + VirtText vt = *item->virt_text; + LineState s = LINE_STATE(""); + int virt_attr = 0; + int col = item->virt_col; + size_t virt_pos = 0; + item->virt_col = -2; // deactivate + + while (col < max_col) { + if (!*s.p) { + if (virt_pos == kv_size(vt)) { + break; + } + s.p = kv_A(vt, virt_pos).text; + int hl_id = kv_A(vt, virt_pos).hl_id; + virt_attr = hl_id > 0 ? syn_id2attr(hl_id) : 0; + virt_pos++; + continue; + } + int cells = line_putchar(&s, &linebuf_char[col], 2, false); + linebuf_attr[col++] = virt_attr; + if (cells > 1) { + linebuf_attr[col++] = virt_attr; + } + } + *end_col = MAX(*end_col, col); + } + } +} + /// Determine if dedicated window grid should be used or the default_grid /// /// If UI did not request multigrid support, draw all windows on the @@ -7109,11 +7152,13 @@ static void win_redr_ruler(win_T *wp, int always) if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count) return; - /* Don't draw the ruler while doing insert-completion, it might overwrite - * the (long) mode message. */ - if (wp == lastwin && lastwin->w_status_height == 0) - if (edit_submode != NULL) + // Don't draw the ruler while doing insert-completion, it might overwrite + // the (long) mode message. + if (wp == lastwin && lastwin->w_status_height == 0) { + if (edit_submode != NULL) { return; + } + } if (*p_ruf) { int save_called_emsg = called_emsg; diff --git a/src/nvim/search.c b/src/nvim/search.c index 2802da6f7f..84b71d56a0 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -2685,8 +2685,9 @@ fwd_word( while (--count >= 0) { /* When inside a range of folded lines, move to the last char of the * last line. */ - if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum)) - coladvance((colnr_T)MAXCOL); + if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum)) { + coladvance(MAXCOL); + } sclass = cls(); /* @@ -2803,8 +2804,9 @@ int end_word(long count, int bigword, int stop, int empty) while (--count >= 0) { /* When inside a range of folded lines, move to the last char of the * last line. */ - if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum)) - coladvance((colnr_T)MAXCOL); + if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum)) { + coladvance(MAXCOL); + } sclass = cls(); if (inc_cursor() == -1) return FAIL; diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 84ca240734..4ea298fba9 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -3152,7 +3152,7 @@ int get_tags(list_T *list, char_u *pat, char_u *buf_fname) bool is_static; ret = find_tags(pat, &num_matches, &matches, - TAG_REGEXP | TAG_NOIC, (int)MAXCOL, buf_fname); + TAG_REGEXP | TAG_NOIC, MAXCOL, buf_fname); if (ret == OK && num_matches > 0) { for (i = 0; i < num_matches; ++i) { int parse_result = parse_match(matches[i], &tp); diff --git a/src/nvim/testdir/test_alot.vim b/src/nvim/testdir/test_alot.vim index 4f056abdc0..daf3c9c110 100644 --- a/src/nvim/testdir/test_alot.vim +++ b/src/nvim/testdir/test_alot.vim @@ -37,7 +37,6 @@ source test_recover.vim source test_scroll_opt.vim source test_sort.vim source test_sha256.vim -source test_statusline.vim source test_suspend.vim source test_syn_attr.vim source test_tabline.vim diff --git a/src/nvim/testdir/test_exit.vim b/src/nvim/testdir/test_exit.vim index 99a401d4a4..bd3e9eb4d4 100644 --- a/src/nvim/testdir/test_exit.vim +++ b/src/nvim/testdir/test_exit.vim @@ -81,3 +81,32 @@ func Test_exiting() endif call delete('Xtestout') endfunc + +" Test for getting the Vim exit code from v:exiting +func Test_exit_code() + call assert_equal(v:null, v:exiting) + + let before =<< trim [CODE] + au QuitPre * call writefile(['qp = ' .. v:exiting], 'Xtestout', 'a') + au ExitPre * call writefile(['ep = ' .. v:exiting], 'Xtestout', 'a') + au VimLeavePre * call writefile(['lp = ' .. v:exiting], 'Xtestout', 'a') + au VimLeave * call writefile(['l = ' .. v:exiting], 'Xtestout', 'a') + [CODE] + + if RunVim(before, ['quit'], '') + call assert_equal(['qp = null', 'ep = null', 'lp = 0', 'l = 0'], readfile('Xtestout')) + endif + call delete('Xtestout') + + if RunVim(before, ['cquit'], '') + call assert_equal(['lp = 1', 'l = 1'], readfile('Xtestout')) + endif + call delete('Xtestout') + + if RunVim(before, ['cquit 4'], '') + call assert_equal(['lp = 4', 'l = 4'], readfile('Xtestout')) + endif + call delete('Xtestout') +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_filetype.vim b/src/nvim/testdir/test_filetype.vim index f9f0ade1f6..6bc5fba5db 100644 --- a/src/nvim/testdir/test_filetype.vim +++ b/src/nvim/testdir/test_filetype.vim @@ -79,6 +79,7 @@ let s:filename_checks = { \ 'bc': ['file.bc'], \ 'bdf': ['file.bdf'], \ 'bib': ['file.bib'], + \ 'beancount': ['file.beancount'], \ 'bindzone': ['named.root'], \ 'blank': ['file.bl'], \ 'bsdl': ['file.bsd', 'file.bsdl'], @@ -427,6 +428,7 @@ let s:filename_checks = { \ 'slrnrc': ['.slrnrc'], \ 'slrnsc': ['file.score'], \ 'sm': ['sendmail.cf'], + \ 'svelte': ['file.svelte'], \ 'smarty': ['file.tpl'], \ 'smcl': ['file.hlp', 'file.ihlp', 'file.smcl'], \ 'smith': ['file.smt', 'file.smith'], diff --git a/src/nvim/testdir/test_functions.vim b/src/nvim/testdir/test_functions.vim index f0c1a1c7f9..5dae8d681a 100644 --- a/src/nvim/testdir/test_functions.vim +++ b/src/nvim/testdir/test_functions.vim @@ -833,6 +833,31 @@ func Test_byte2line_line2byte() bw! endfunc +" Test for charidx() +func Test_charidx() + let a = 'xáb́y' + call assert_equal(0, charidx(a, 0)) + call assert_equal(1, charidx(a, 3)) + call assert_equal(2, charidx(a, 4)) + call assert_equal(3, charidx(a, 7)) + call assert_equal(-1, charidx(a, 8)) + call assert_equal(-1, charidx('', 0)) + + " count composing characters + call assert_equal(0, charidx(a, 0, 1)) + call assert_equal(2, charidx(a, 2, 1)) + call assert_equal(3, charidx(a, 4, 1)) + call assert_equal(5, charidx(a, 7, 1)) + call assert_equal(-1, charidx(a, 8, 1)) + call assert_equal(-1, charidx('', 0, 1)) + + call assert_fails('let x = charidx([], 1)', 'E474:') + call assert_fails('let x = charidx("abc", [])', 'E474:') + call assert_fails('let x = charidx("abc", 1, [])', 'E474:') + call assert_fails('let x = charidx("abc", 1, -1)', 'E474:') + call assert_fails('let x = charidx("abc", 1, 2)', 'E474:') +endfunc + func Test_count() let l = ['a', 'a', 'A', 'b'] call assert_equal(2, count(l, 'a')) diff --git a/src/nvim/testdir/test_options.vim b/src/nvim/testdir/test_options.vim index 5a10c9baa6..1202b842fd 100644 --- a/src/nvim/testdir/test_options.vim +++ b/src/nvim/testdir/test_options.vim @@ -1,5 +1,7 @@ " Test for options +source check.vim + func Test_whichwrap() set whichwrap=b,s call assert_equal('b,s', &whichwrap) @@ -604,6 +606,27 @@ func Test_opt_boolean() set number& endfunc +func Test_opt_winminheight_term() + " See test/functional/legacy/options_spec.lua + CheckRunVimInTerminal + + " The tabline should be taken into account. + let lines =<< trim END + set wmh=0 stal=2 + below sp | wincmd _ + below sp | wincmd _ + below sp | wincmd _ + below sp + END + call writefile(lines, 'Xwinminheight') + let buf = RunVimInTerminal('-S Xwinminheight', #{rows: 11}) + call term_sendkeys(buf, ":set wmh=1\n") + call WaitForAssert({-> assert_match('E36: Not enough room', term_getline(buf, 11))}) + + call StopVimInTerminal(buf) + call delete('Xwinminheight') +endfunc + " Test for setting option value containing spaces with isfname+=32 func Test_isfname_with_options() set isfname+=32 @@ -613,4 +636,23 @@ func Test_isfname_with_options() setlocal keywordprg& endfunc +" Test that resetting laststatus does change scroll option +func Test_opt_reset_scroll() + " See test/functional/legacy/options_spec.lua + CheckRunVimInTerminal + let vimrc =<< trim [CODE] + set scroll=2 + set laststatus=2 + [CODE] + call writefile(vimrc, 'Xscroll') + let buf = RunVimInTerminal('-S Xscroll', {'rows': 16, 'cols': 45}) + call term_sendkeys(buf, ":verbose set scroll?\n") + call WaitForAssert({-> assert_match('Last set.*window size', term_getline(buf, 15))}) + call assert_match('^\s*scroll=7$', term_getline(buf, 14)) + call StopVimInTerminal(buf) + + " clean up + call delete('Xscroll') +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_statusline.vim b/src/nvim/testdir/test_statusline.vim index 4e38f7ebd8..ce2ef4dcd8 100644 --- a/src/nvim/testdir/test_statusline.vim +++ b/src/nvim/testdir/test_statusline.vim @@ -1,12 +1,12 @@ " Test 'statusline' " " Not tested yet: -" %a " %N " %T " %X source view_util.vim +source check.vim source term_util.vim func s:get_statusline() @@ -61,7 +61,19 @@ func Test_statusline_will_be_disabled_with_error() endfunc func Test_statusline() - new Xstatusline + CheckFeature quickfix + + " %a: Argument list ({current} of {max}) + set statusline=%a + call assert_match('^\s*$', s:get_statusline()) + arglocal a1 a2 + rewind + call assert_match('^ (1 of 2)\s*$', s:get_statusline()) + next + call assert_match('^ (2 of 2)\s*$', s:get_statusline()) + e Xstatusline + call assert_match('^ ((2) of 2)\s*$', s:get_statusline()) + only set laststatus=2 set splitbelow diff --git a/src/nvim/testdir/test_user_func.vim b/src/nvim/testdir/test_user_func.vim index 67701ee3ca..e9e181ce3d 100644 --- a/src/nvim/testdir/test_user_func.vim +++ b/src/nvim/testdir/test_user_func.vim @@ -95,6 +95,59 @@ func Test_user_func() enew! endfunc +func Log(val, base = 10) + return log(a:val) / log(a:base) +endfunc + +func Args(mandatory, optional = v:null, ...) + return deepcopy(a:) +endfunc + +func Args2(a = 1, b = 2, c = 3) + return deepcopy(a:) +endfunc + +func MakeBadFunc() + func s:fcn(a, b=1, c) + endfunc +endfunc + +func Test_default_arg() + call assert_equal(1.0, Log(10)) + call assert_equal(log(10), Log(10, exp(1))) + call assert_fails("call Log(1,2,3)", 'E118') + + let res = Args(1) + call assert_equal(res.mandatory, 1) + call assert_equal(res.optional, v:null) + call assert_equal(res['0'], 0) + + let res = Args(1,2) + call assert_equal(res.mandatory, 1) + call assert_equal(res.optional, 2) + call assert_equal(res['0'], 0) + + let res = Args(1,2,3) + call assert_equal(res.mandatory, 1) + call assert_equal(res.optional, 2) + call assert_equal(res['0'], 1) + + call assert_fails("call MakeBadFunc()", 'E989') + call assert_fails("fu F(a=1 ,) | endf", 'E475') + + " Since neovim does not have v:none, the ability to use the default + " argument with the intermediate argument set to v:none has been omitted. + " Therefore, this test is not performed. + " let d = Args2(7, v:none, 9) + " call assert_equal([7, 2, 9], [d.a, d.b, d.c]) + + call assert_equal("\n" + \ .. " function Args2(a = 1, b = 2, c = 3)\n" + \ .. "1 return deepcopy(a:)\n" + \ .. " endfunction", + \ execute('func Args2')) +endfunc + func Test_failed_call_in_try() try | call UnknownFunc() | catch | endtry endfunc diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c index 3e683a4926..6705ab98c2 100644 --- a/src/nvim/tui/input.c +++ b/src/nvim/tui/input.c @@ -557,8 +557,8 @@ HandleState ut_handle_background_color(TermInput *input) static void handle_raw_buffer(TermInput *input, bool force) { - HandleState is_paste; - HandleState is_bc; + HandleState is_paste = kNotApplicable; + HandleState is_bc = kNotApplicable; do { if (!force diff --git a/src/nvim/window.c b/src/nvim/window.c index 00f49724b6..0f717a2f90 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -5501,7 +5501,7 @@ void win_setminheight(void) // loop until there is a 'winminheight' that is possible while (p_wmh > 0) { - const int room = Rows - p_ch; + const int room = Rows - p_ch - tabline_height(); const int needed = frame_minheight(topframe, NULL); if (room >= needed) { break; @@ -5960,9 +5960,17 @@ void win_new_width(win_T *wp, int width) void win_comp_scroll(win_T *wp) { + const long old_w_p_scr = wp->w_p_scr; + wp->w_p_scr = wp->w_height / 2; - if (wp->w_p_scr == 0) + if (wp->w_p_scr == 0) { wp->w_p_scr = 1; + } + if (wp->w_p_scr != old_w_p_scr) { + // Used by "verbose set scroll". + wp->w_p_script_ctx[WV_SCROLL].script_ctx.sc_sid = SID_WINLAYOUT; + wp->w_p_script_ctx[WV_SCROLL].script_ctx.sc_lnum = 0; + } } /* |