aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/ops.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/ops.c')
-rw-r--r--src/nvim/ops.c784
1 files changed, 372 insertions, 412 deletions
diff --git a/src/nvim/ops.c b/src/nvim/ops.c
index 93449f2337..8c8900710d 100644
--- a/src/nvim/ops.c
+++ b/src/nvim/ops.c
@@ -1,6 +1,3 @@
-// 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
-
// ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
// op_change, op_yank, do_put, do_join
@@ -12,10 +9,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
#include "nvim/api/private/defs.h"
-#include "nvim/ascii.h"
-#include "nvim/assert.h"
+#include "nvim/ascii_defs.h"
+#include "nvim/assert_defs.h"
#include "nvim/autocmd.h"
#include "nvim/buffer.h"
#include "nvim/change.h"
@@ -35,11 +33,11 @@
#include "nvim/getchar.h"
#include "nvim/gettext.h"
#include "nvim/globals.h"
-#include "nvim/highlight_defs.h"
+#include "nvim/highlight.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
#include "nvim/keycodes.h"
-#include "nvim/macros.h"
+#include "nvim/macros_defs.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
@@ -50,19 +48,20 @@
#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
+#include "nvim/option_defs.h"
+#include "nvim/option_vars.h"
#include "nvim/os/input.h"
#include "nvim/os/time.h"
#include "nvim/plines.h"
-#include "nvim/screen.h"
#include "nvim/search.h"
#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/terminal.h"
#include "nvim/textformat.h"
-#include "nvim/types.h"
+#include "nvim/types_defs.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
-#include "nvim/vim.h"
+#include "nvim/vim_defs.h"
#include "nvim/window.h"
#include "nvim/yankmap.h"
@@ -114,13 +113,16 @@ struct block_def {
# include "ops.c.generated.h"
#endif
+static const char e_search_pattern_and_expression_register_may_not_contain_two_or_more_lines[]
+ = N_("E883: Search pattern and expression register may not contain two or more lines");
+
// Flags for third item in "opchars".
#define OPF_LINES 1 // operator always works on lines
#define OPF_CHANGE 2 // operator changes text
-// The names of operators.
-// IMPORTANT: Index must correspond with defines in vim.h!!!
-// The third field indicates whether the operator always works on lines.
+/// The names of operators.
+/// IMPORTANT: Index must correspond with defines in ops.h!!!
+/// The third field indicates whether the operator always works on lines.
static char opchars[][3] = {
{ NUL, NUL, 0 }, // OP_NOP
{ 'd', NUL, OPF_CHANGE }, // OP_DELETE
@@ -230,8 +232,7 @@ int get_extra_op_char(int optype)
/// handle a shift operation
void op_shift(oparg_T *oap, int curs_top, int amount)
{
- long i;
- int first_char;
+ int i;
int block_col = 0;
if (u_save((linenr_T)(oap->start.lnum - 1),
@@ -244,7 +245,7 @@ void op_shift(oparg_T *oap, int curs_top, int amount)
}
for (i = oap->line_count - 1; i >= 0; i--) {
- first_char = (uint8_t)(*get_cursor_line_ptr());
+ int first_char = (uint8_t)(*get_cursor_line_ptr());
if (first_char == NUL) { // empty line
curwin->w_cursor.col = 0;
} else if (oap->motion_type == kMTBlockWise) {
@@ -297,7 +298,7 @@ void op_shift(oparg_T *oap, int curs_top, int amount)
}
}
- changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L, true);
+ changed_lines(curbuf, oap->start.lnum, 0, oap->end.lnum + 1, 0, true);
}
/// Shift the current line one shiftwidth left (if left != 0) or right
@@ -306,15 +307,13 @@ void op_shift(oparg_T *oap, int curs_top, int amount)
/// @param call_changed_bytes call changed_bytes()
void shift_line(int left, int round, int amount, int call_changed_bytes)
{
- int count;
- int i, j;
- const int sw_val = (int)get_sw_value_indent(curbuf);
+ const int sw_val = get_sw_value_indent(curbuf);
- count = get_indent(); // get current indent
+ int count = get_indent(); // get current indent
if (round) { // round off indent
- i = count / sw_val; // number of 'shiftwidth' rounded down
- j = count % sw_val; // extra spaces
+ int i = count / sw_val; // number of 'shiftwidth' rounded down
+ int j = count % sw_val; // extra spaces
if (j && left) { // first remove extra spaces
amount--;
}
@@ -354,11 +353,10 @@ static void shift_block(oparg_T *oap, int amount)
const int oldstate = State;
char *newp;
const int oldcol = curwin->w_cursor.col;
- const int sw_val = (int)get_sw_value_indent(curbuf);
+ const int sw_val = get_sw_value_indent(curbuf);
const int ts_val = (int)curbuf->b_p_ts;
struct block_def bd;
int incr;
- int i = 0, j = 0;
const int old_p_ri = p_ri;
p_ri = 0; // don't want revins in indent
@@ -409,40 +407,36 @@ static void shift_block(oparg_T *oap, int amount)
bd.start_vcol = cts.cts_vcol;
clear_chartabsize_arg(&cts);
+ int tabs = 0, spaces = 0;
// OK, now total=all the VWS reqd, and textstart points at the 1st
// non-ws char in the block.
if (!curbuf->b_p_et) {
- tabstop_fromto(ws_vcol, ws_vcol + total, ts_val, curbuf->b_p_vts_array, &i, &j);
+ tabstop_fromto(ws_vcol, ws_vcol + total,
+ ts_val, curbuf->b_p_vts_array, &tabs, &spaces);
} else {
- j = total;
+ spaces = total;
}
// if we're splitting a TAB, allow for it
- int col_pre = bd.pre_whitesp_c - (bd.startspaces != 0);
+ const int col_pre = bd.pre_whitesp_c - (bd.startspaces != 0);
bd.textcol -= col_pre;
- const int len = (int)strlen(bd.textstart) + 1;
- int col = bd.textcol + i + j + len;
- assert(col >= 0);
- newp = xmalloc((size_t)col);
- memset(newp, NUL, (size_t)col);
+
+ const size_t new_line_len // the length of the line after the block shift
+ = (size_t)bd.textcol + (size_t)tabs + (size_t)spaces + strlen(bd.textstart);
+ newp = xmalloc(new_line_len + 1);
memmove(newp, oldp, (size_t)bd.textcol);
startcol = bd.textcol;
oldlen = (int)(bd.textstart - old_textstart) + col_pre;
- newlen = i + j;
- memset(newp + bd.textcol, TAB, (size_t)i);
- memset(newp + bd.textcol + i, ' ', (size_t)j);
- // the end
- memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
+ newlen = tabs + spaces;
+ memset(newp + bd.textcol, TAB, (size_t)tabs);
+ memset(newp + bd.textcol + tabs, ' ', (size_t)spaces);
+ // Note that STRMOVE() copies the trailing NUL.
+ STRMOVE(newp + bd.textcol + tabs + spaces, bd.textstart);
} else { // left
- colnr_T destination_col; // column to which text in block will
- // be shifted
char *verbatim_copy_end; // end of the part of the line which is
// copied verbatim
colnr_T verbatim_copy_width; // the (displayed) width of this part
// of line
- size_t fill; // nr of spaces that replace a TAB
- size_t new_line_len; // the length of the line after the
- // block shift
char *non_white = bd.textstart;
// Firstly, let's find the first non-whitespace character that is
@@ -475,10 +469,10 @@ static void shift_block(oparg_T *oap, int amount)
const colnr_T block_space_width = non_white_col - oap->start_vcol;
// We will shift by "total" or "block_space_width", whichever is less.
const colnr_T shift_amount = block_space_width < total
- ? block_space_width
- : total;
+ ? block_space_width
+ : total;
// The column to which we will shift the text.
- destination_col = non_white_col - shift_amount;
+ const colnr_T destination_col = non_white_col - shift_amount;
// Now let's find out how much of the beginning of the line we can
// reuse without modification.
@@ -509,7 +503,8 @@ static void shift_block(oparg_T *oap, int amount)
// part of the line that will be copied, it means we encountered a tab
// character, which we will have to partly replace with spaces.
assert(destination_col - verbatim_copy_width >= 0);
- fill = (size_t)(destination_col - verbatim_copy_width);
+ const size_t fill // nr of spaces that replace a TAB
+ = (size_t)(destination_col - verbatim_copy_width);
assert(verbatim_copy_end - oldp >= 0);
const size_t verbatim_diff = (size_t)(verbatim_copy_end - oldp);
@@ -517,14 +512,16 @@ static void shift_block(oparg_T *oap, int amount)
// - the beginning of the original line up to "verbatim_copy_end",
// - "fill" number of spaces,
// - the rest of the line, pointed to by non_white.
- new_line_len = verbatim_diff + fill + strlen(non_white) + 1;
+ const size_t new_line_len // the length of the line after the block shift
+ = verbatim_diff + fill + strlen(non_white);
- newp = xmalloc(new_line_len);
+ newp = xmalloc(new_line_len + 1);
startcol = (int)verbatim_diff;
oldlen = bd.textcol + (int)(non_white - bd.textstart) - (int)verbatim_diff;
newlen = (int)fill;
memmove(newp, oldp, verbatim_diff);
memset(newp + verbatim_diff, ' ', fill);
+ // Note that STRMOVE() copies the trailing NUL.
STRMOVE(newp + verbatim_diff + fill, non_white);
}
// replace the line
@@ -548,11 +545,10 @@ static void block_insert(oparg_T *oap, char *s, int b_insert, struct block_def *
colnr_T offset; // pointer along new line
size_t s_len = strlen(s);
char *newp, *oldp; // new, old lines
- linenr_T lnum; // loop var
int oldstate = State;
State = MODE_INSERT; // don't want MODE_REPLACE for State
- for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++) {
+ for (linenr_T lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++) {
block_prep(oap, bdp, lnum, true);
if (bdp->is_short && b_insert) {
continue; // OP_INSERT, line ends before block start
@@ -644,17 +640,15 @@ static void block_insert(oparg_T *oap, char *s, int b_insert, struct block_def *
}
} // for all lnum
- changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L, true);
-
State = oldstate;
+
+ changed_lines(curbuf, oap->start.lnum + 1, 0, oap->end.lnum + 1, 0, true);
}
/// Handle reindenting a block of lines.
void op_reindent(oparg_T *oap, Indenter how)
{
- long i = 0;
- char *l;
- int amount;
+ int i = 0;
linenr_T first_changed = 0;
linenr_T last_changed = 0;
linenr_T start_lnum = curwin->w_cursor.lnum;
@@ -667,8 +661,9 @@ void op_reindent(oparg_T *oap, Indenter how)
// Save for undo. Do this once for all lines, much faster than doing this
// for each line separately, especially when undoing.
- if (u_savecommon(curbuf, start_lnum - 1, start_lnum + (linenr_T)oap->line_count,
- start_lnum + (linenr_T)oap->line_count, false) == OK) {
+ if (u_savecommon(curbuf, start_lnum - 1, start_lnum + oap->line_count,
+ start_lnum + oap->line_count, false) == OK) {
+ int amount;
for (i = oap->line_count - 1; i >= 0 && !got_int; i--) {
// it's a slow thing to do, so give feedback so there's no worry
// that the computer's just hung.
@@ -676,14 +671,14 @@ void op_reindent(oparg_T *oap, Indenter how)
if (i > 1
&& (i % 50 == 0 || i == oap->line_count - 1)
&& oap->line_count > p_report) {
- smsg(_("%" PRId64 " lines to indent... "), (int64_t)i);
+ smsg(0, _("%" PRId64 " lines to indent... "), (int64_t)i);
}
// Be vi-compatible: For lisp indenting the first line is not
// indented, unless there is only one line.
if (i != oap->line_count - 1 || oap->line_count == 1
|| how != get_lisp_indent) {
- l = skipwhite(get_cursor_line_ptr());
+ char *l = skipwhite(get_cursor_line_ptr());
if (*l == NUL) { // empty or blank line
amount = 0;
} else {
@@ -710,18 +705,16 @@ void op_reindent(oparg_T *oap, Indenter how)
// highlighting was present, need to continue until the last line. When
// there is no change still need to remove the Visual highlighting.
if (last_changed != 0) {
- changed_lines(first_changed, 0,
- oap->is_VIsual ? start_lnum + (linenr_T)oap->line_count :
- last_changed + 1, 0L, true);
+ changed_lines(curbuf, first_changed, 0,
+ oap->is_VIsual ? start_lnum + oap->line_count
+ : last_changed + 1, 0, true);
} else if (oap->is_VIsual) {
redraw_curbuf_later(UPD_INVERTED);
}
if (oap->line_count > p_report) {
i = oap->line_count - (i + 1);
- smsg(NGETTEXT("%" PRId64 " line indented ",
- "%" PRId64 " lines indented ", i),
- (int64_t)i);
+ smsg(0, NGETTEXT("%" PRId64 " line indented ", "%" PRId64 " lines indented ", i), (int64_t)i);
}
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) {
// set '[ and '] marks
@@ -738,9 +731,7 @@ static char *expr_line = NULL;
/// @return '=' when OK, NUL otherwise.
int get_expr_register(void)
{
- char *new_line;
-
- new_line = getcmdline('=', 0L, 0, true);
+ char *new_line = getcmdline('=', 0, 0, true);
if (new_line == NULL) {
return NUL;
}
@@ -765,8 +756,6 @@ void set_expr_line(char *new_line)
/// @return a pointer to allocated memory, or NULL for failure.
char *get_expr_line(void)
{
- char *expr_copy;
- char *rv;
static int nested = 0;
if (expr_line == NULL) {
@@ -775,7 +764,7 @@ char *get_expr_line(void)
// Make a copy of the expression, because evaluating it may cause it to be
// changed.
- expr_copy = xstrdup(expr_line);
+ char *expr_copy = xstrdup(expr_line);
// When we are invoked recursively limit the evaluation to 10 levels.
// Then return the string as-is.
@@ -784,7 +773,7 @@ char *get_expr_line(void)
}
nested++;
- rv = eval_to_string(expr_copy, NULL, true);
+ char *rv = eval_to_string(expr_copy, true);
nested--;
xfree(expr_copy);
return rv;
@@ -845,7 +834,7 @@ static int call_userreg_put(const char* urf, int regname, typval_T* out)
char regname_str[5];
int len;
- len = (*utf_char2len)(regname);
+ len = utf_char2len(regname);
regname_str[len] = 0;
utf_char2bytes(regname, regname_str);
@@ -873,7 +862,9 @@ static int call_userreg_put(const char* urf, int regname, typval_T* out)
// Converts a typval returned from the userregfunction to a register.
static void typval_to_yankreg(yankreg_T* yankreg, typval_T* val)
{
- if (!yankreg || !val) return;
+ if (!yankreg || !val) {
+ return;
+ }
char* type;
dict_T* dict;
@@ -901,11 +892,11 @@ static void typval_to_yankreg(yankreg_T* yankreg, typval_T* val)
if (tv_dict_get_tv(dict, "lines", &tv) == OK) {
if (tv.v_type == VAR_STRING) {
- yankreg->y_array = xcalloc(sizeof(char*), 1);
+ yankreg->y_array = (char**) xcalloc(sizeof(char*), 1);
yankreg->y_array[0] = strdup(tv.vval.v_string);
} else if (tv.v_type == VAR_LIST) {
yankreg->y_array =
- xcalloc(sizeof(char*), (size_t) tv_list_len(tv.vval.v_list));
+ (char**) xcalloc(sizeof(char*), (size_t) tv_list_len(tv.vval.v_list));
i = 0;
TV_LIST_ITER_CONST(tv.vval.v_list, li, {
@@ -933,7 +924,7 @@ static void typval_to_yankreg(yankreg_T* yankreg, typval_T* val)
case VAR_LIST:
yankreg->y_type = kMTLineWise;
sz = (size_t) tv_list_len(val->vval.v_list);
- yankreg->y_array = xcalloc(sizeof(char*), sz);
+ yankreg->y_array = (char**) xcalloc(sizeof(char*), sz);
yankreg->y_size = sz;
i = 0;
TV_LIST_ITER_CONST(val->vval.v_list, li, {
@@ -947,7 +938,7 @@ static void typval_to_yankreg(yankreg_T* yankreg, typval_T* val)
yankreg->y_size = 1;
if (val->vval.v_string) {
- yankreg->y_array = xcalloc(sizeof(char*), 1);
+ yankreg->y_array = (char**) xcalloc(sizeof(char*), 1);
yankreg->y_array[0] = strdup(tv_get_string(val));
} else {
yankreg->y_array = NULL;
@@ -962,11 +953,13 @@ static void typval_to_yankreg(yankreg_T* yankreg, typval_T* val)
static void copy_userreg(yankreg_T* into, int regname)
{
- if (!into) return;
-
- if (!curbuf->b_p_urf || strlen(curbuf->b_p_urf) == 0)
+ if (!into) {
return;
+ }
+ if (!curbuf->b_p_urf || strlen(curbuf->b_p_urf) == 0) {
+ return;
+ }
typval_T* ret = xmalloc(sizeof(typval_T));
@@ -1034,15 +1027,6 @@ static bool is_append_register(int regname)
return ASCII_ISUPPER(regname);
}
-/// @see get_yank_register
-/// @returns true when register should be inserted literally
-/// (selection or clipboard)
-static inline bool is_literal_register(int regname)
- FUNC_ATTR_CONST
-{
- return regname == '*' || regname == '+';
-}
-
/// @return a copy of contents in register `name` for use in do_put. Should be
/// freed by caller.
yankreg_T *copy_register(int name)
@@ -1055,7 +1039,7 @@ yankreg_T *copy_register(int name)
if (copy->y_size == 0) {
copy->y_array = NULL;
} else {
- copy->y_array = xcalloc(copy->y_size, sizeof(char *));
+ copy->y_array = (char**) xcalloc(copy->y_size, sizeof(char *));
for (size_t i = 0; i < copy->y_size; i++) {
copy->y_array[i] = xstrdup(reg->y_array[i]);
}
@@ -1081,9 +1065,7 @@ bool yank_register_mline(int regname)
/// @return FAIL for failure, OK otherwise.
int do_record(int c)
{
- char *p;
static int regname;
- yankreg_T *old_y_previous;
int retval;
if (reg_recording == 0) {
@@ -1105,11 +1087,11 @@ int do_record(int c)
dict_T *dict = get_v_event(&save_v_event);
// The recorded text contents.
- p = (char *)get_recorded();
+ char *p = get_recorded();
if (p != NULL) {
// Remove escaping for K_SPECIAL in multi-byte chars.
- vim_unescape_ks((char_u *)p);
- (void)tv_dict_add_str(dict, S_LEN("regcontents"), (const char *)p);
+ vim_unescape_ks(p);
+ (void)tv_dict_add_str(dict, S_LEN("regcontents"), p);
}
// Name of requested register, or empty string for unnamed operation.
@@ -1130,14 +1112,14 @@ int do_record(int c)
if (p_ch == 0 || ui_has(kUIMessages)) {
showmode();
} else {
- msg("");
+ msg("", 0);
}
if (p == NULL) {
retval = FAIL;
} else {
// We don't want to change the default register here, so save and
// restore the current register name.
- old_y_previous = y_previous;
+ yankreg_T *old_y_previous = y_previous;
retval = stuff_yank(regname, p);
@@ -1175,10 +1157,12 @@ static int stuff_yank(int regname, char *p)
yankreg_T *reg = get_yank_register(regname, YREG_YANK);
if (is_append_register(regname) && reg->y_array != NULL) {
char **pp = &(reg->y_array[reg->y_size - 1]);
- char *lp = xmalloc(strlen(*pp) + strlen(p) + 1);
- STRCPY(lp, *pp);
- // TODO(philix): use xstpcpy() in stuff_yank()
- STRCAT(lp, p);
+ const size_t ppl = strlen(*pp);
+ const size_t pl = strlen(p);
+ char *lp = xmalloc(ppl + pl + 1);
+ memcpy(lp, *pp, ppl);
+ memcpy(lp + ppl, p, pl);
+ *(lp + ppl + pl) = NUL;
xfree(p);
xfree(*pp);
*pp = lp;
@@ -1219,13 +1203,11 @@ static char *execreg_line_continuation(char **lines, size_t *idx)
garray_T ga;
ga_init(&ga, (int)sizeof(char), 400);
- char *p;
-
// search backwards to find the first line of this command.
// Any line not starting with \ or "\ is the start of the
// command.
while (--i > 0) {
- p = skipwhite(lines[i]);
+ char *p = skipwhite(lines[i]);
if (*p != '\\' && (p[0] != '"' || p[1] != '\\' || p[2] != ' ')) {
break;
}
@@ -1235,7 +1217,7 @@ static char *execreg_line_continuation(char **lines, size_t *idx)
// join all the lines
ga_concat(&ga, lines[cmd_start]);
for (size_t j = cmd_start + 1; j <= cmd_end; j++) {
- p = skipwhite(lines[j]);
+ char *p = skipwhite(lines[j]);
if (*p == '\\') {
// Adjust the growsize to the current length to
// speed up concatenating many lines.
@@ -1262,7 +1244,6 @@ static char *execreg_line_continuation(char **lines, size_t *idx)
/// @return FAIL for failure, OK otherwise
int do_execreg(int regname, int colon, int addcr, int silent)
{
- char *p;
int retval = OK;
if (regname == '@') { // repeat previous one
@@ -1291,12 +1272,12 @@ int do_execreg(int regname, int colon, int addcr, int silent)
// don't keep the cmdline containing @:
XFREE_CLEAR(new_last_cmdline);
// Escape all control characters with a CTRL-V
- p = vim_strsave_escaped_ext(last_cmdline,
- "\001\002\003\004\005\006\007"
- "\010\011\012\013\014\015\016\017"
- "\020\021\022\023\024\025\026\027"
- "\030\031\032\033\034\035\036\037",
- Ctrl_V, false);
+ char *p = vim_strsave_escaped_ext(last_cmdline,
+ "\001\002\003\004\005\006\007"
+ "\010\011\012\013\014\015\016\017"
+ "\020\021\022\023\024\025\026\027"
+ "\030\031\032\033\034\035\036\037",
+ Ctrl_V, false);
// When in Visual mode "'<,'>" will be prepended to the command.
// Remove it when it's already there.
if (VIsual_active && strncmp(p, "'<,'>", 5) == 0) {
@@ -1306,14 +1287,14 @@ int do_execreg(int regname, int colon, int addcr, int silent)
}
xfree(p);
} else if (regname == '=') {
- p = get_expr_line();
+ char *p = get_expr_line();
if (p == NULL) {
return FAIL;
}
retval = put_in_typebuf(p, true, colon, silent);
xfree(p);
} else if (regname == '.') { // use last inserted text
- p = get_last_insert_save();
+ char *p = get_last_insert_save();
if (p == NULL) {
emsg(_(e_noinstext));
return FAIL;
@@ -1331,7 +1312,6 @@ int do_execreg(int regname, int colon, int addcr, int silent)
// Insert lines into typeahead buffer, from last one to first one.
put_reedit_in_typebuf(silent);
- char *escaped;
for (size_t i = reg->y_size; i-- > 0;) { // from y_size - 1 to 0 included
// insert NL between lines and after last line if type is kMTLineWise
if (reg->y_type == kMTLineWise || i < reg->y_size - 1 || addcr) {
@@ -1344,13 +1324,13 @@ int do_execreg(int regname, int colon, int addcr, int silent)
char *str = reg->y_array[i];
bool free_str = false;
if (colon && i > 0) {
- p = skipwhite(str);
+ char *p = skipwhite(str);
if (*p == '\\' || (p[0] == '"' && p[1] == '\\' && p[2] == ' ')) {
str = execreg_line_continuation(reg->y_array, &i);
free_str = true;
}
}
- escaped = vim_strsave_escape_ks(str);
+ char *escaped = vim_strsave_escape_ks(str);
if (free_str) {
xfree(str);
}
@@ -1373,7 +1353,7 @@ int do_execreg(int regname, int colon, int addcr, int silent)
/// used only after other typeahead has been processed.
static void put_reedit_in_typebuf(int silent)
{
- char_u buf[3];
+ uint8_t buf[3];
if (restart_edit == NUL) {
return;
@@ -1384,7 +1364,7 @@ static void put_reedit_in_typebuf(int silent)
buf[1] = 'R';
buf[2] = NUL;
} else {
- buf[0] = (char_u)(restart_edit == 'I' ? 'i' : restart_edit);
+ buf[0] = (uint8_t)(restart_edit == 'I' ? 'i' : restart_edit);
buf[1] = NUL;
}
if (ins_typebuf((char *)buf, REMAP_NONE, 0, true, silent) == OK) {
@@ -1456,12 +1436,12 @@ int insert_reg(int regname, bool literally_arg)
char *arg;
if (regname == '.') { // Insert last inserted text.
- retval = stuff_inserted(NUL, 1L, true);
+ retval = stuff_inserted(NUL, 1, true);
} else if (get_spec_reg(regname, &arg, &allocated, true)) {
if (arg == NULL) {
return FAIL;
}
- stuffescaped((const char *)arg, literally);
+ stuffescaped(arg, literally);
if (allocated) {
xfree(arg);
}
@@ -1474,9 +1454,9 @@ int insert_reg(int regname, bool literally_arg)
if (regname == '-') {
AppendCharToRedobuff(Ctrl_R);
AppendCharToRedobuff(regname);
- do_put(regname, NULL, BACKWARD, 1L, PUT_CURSEND);
+ do_put(regname, NULL, BACKWARD, 1, PUT_CURSEND);
} else {
- stuffescaped((const char *)reg->y_array[i], literally);
+ stuffescaped(reg->y_array[i], literally);
}
// Insert a newline between lines and after last line if
// y_type is kMTLineWise.
@@ -1583,8 +1563,6 @@ static int eval_yank_userreg(const char *ufn, int regname, yankreg_T *reg)
/// @return true if "regname" is a special register,
bool get_spec_reg(int regname, char **argp, bool *allocated, bool errmsg)
{
- size_t cnt;
-
*argp = NULL;
*allocated = false;
switch (regname) {
@@ -1632,7 +1610,7 @@ bool get_spec_reg(int regname, char **argp, bool *allocated, bool errmsg)
return false;
}
*argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP | (regname == Ctrl_P ? FNAME_EXP : 0),
- 1L, NULL);
+ 1, NULL);
*allocated = true;
return true;
@@ -1641,10 +1619,10 @@ bool get_spec_reg(int regname, char **argp, bool *allocated, bool errmsg)
if (!errmsg) {
return false;
}
- cnt = find_ident_under_cursor(argp, (regname == Ctrl_W
- ? (FIND_IDENT|FIND_STRING)
- : FIND_STRING));
- *argp = cnt ? xstrnsave(*argp, cnt) : NULL;
+ size_t cnt = find_ident_under_cursor(argp, (regname == Ctrl_W
+ ? (FIND_IDENT|FIND_STRING)
+ : FIND_STRING));
+ *argp = cnt ? xmemdupz(*argp, cnt) : NULL;
*allocated = true;
return true;
@@ -1653,7 +1631,7 @@ bool get_spec_reg(int regname, char **argp, bool *allocated, bool errmsg)
return false;
}
- *argp = ml_get_buf(curwin->w_buffer, curwin->w_cursor.lnum, false);
+ *argp = ml_get_buf(curwin->w_buffer, curwin->w_cursor.lnum);
return true;
case '_': // black hole: always empty
@@ -1723,10 +1701,7 @@ static void shift_delete_registers(bool y_append)
/// @return FAIL if undo failed, OK otherwise.
int op_delete(oparg_T *oap)
{
- int n;
linenr_T lnum;
- char *ptr;
- char *newp, *oldp;
struct block_def bd = { 0 };
linenr_T old_lcount = curbuf->b_ml.ml_line_count;
@@ -1759,7 +1734,7 @@ int op_delete(oparg_T *oap)
&& oap->line_count > 1
&& oap->motion_force == NUL
&& oap->op_type == OP_DELETE) {
- ptr = ml_get(oap->end.lnum) + oap->end.col;
+ char *ptr = ml_get(oap->end.lnum) + oap->end.col;
if (*ptr != NUL) {
ptr += oap->inclusive;
}
@@ -1853,12 +1828,12 @@ int op_delete(oparg_T *oap)
curwin->w_cursor.coladd = 0;
}
- // n == number of chars deleted
+ // "n" == number of chars deleted
// If we delete a TAB, it may be replaced by several characters.
// Thus the number of characters may increase!
- n = bd.textlen - bd.startspaces - bd.endspaces;
- oldp = ml_get(lnum);
- newp = xmalloc(strlen(oldp) - (size_t)n + 1);
+ int n = bd.textlen - bd.startspaces - bd.endspaces;
+ char *oldp = ml_get(lnum);
+ char *newp = xmalloc(strlen(oldp) - (size_t)n + 1);
// copy up to deleted part
memmove(newp, oldp, (size_t)bd.textcol);
// insert spaces
@@ -1876,8 +1851,8 @@ int op_delete(oparg_T *oap)
}
check_cursor_col();
- changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
- oap->end.lnum + 1, 0L, true);
+ changed_lines(curbuf, curwin->w_cursor.lnum, curwin->w_cursor.col,
+ oap->end.lnum + 1, 0, true);
oap->line_count = 0; // no lines deleted
} else if (oap->motion_type == kMTLineWise) {
if (oap->op_type == OP_CHANGE) {
@@ -1911,19 +1886,18 @@ int op_delete(oparg_T *oap)
// leave cursor past last char in line
if (oap->line_count > 1) {
- u_clearline(); // "U" command not possible after "2cc"
+ u_clearline(curbuf); // "U" command not possible after "2cc"
}
} else {
del_lines(oap->line_count, true);
beginline(BL_WHITE | BL_FIX);
- u_clearline(); // "U" command not possible after "dd"
+ u_clearline(curbuf); // "U" command not possible after "dd"
}
} else {
if (virtual_op) {
- int endcol = 0;
-
// For virtualedit: break the tabs that are partly included.
if (gchar_pos(&oap->start) == '\t') {
+ int endcol = 0;
if (u_save_cursor() == FAIL) { // save first line for undo
return FAIL;
}
@@ -1970,7 +1944,7 @@ int op_delete(oparg_T *oap)
display_dollar(oap->end.col - !oap->inclusive);
}
- n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
+ int n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
if (virtual_op) {
// fix up things for virtualedit-delete:
@@ -2001,8 +1975,8 @@ int op_delete(oparg_T *oap)
pos_T curpos;
// save deleted and changed lines for undo
- if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
- (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) {
+ if (u_save(curwin->w_cursor.lnum - 1,
+ curwin->w_cursor.lnum + oap->line_count) == FAIL) {
return FAIL;
}
@@ -2019,7 +1993,7 @@ int op_delete(oparg_T *oap)
del_lines(oap->line_count - 2, false);
// delete from start of line until op_end
- n = (oap->end.col + 1 - !oap->inclusive);
+ int n = (oap->end.col + 1 - !oap->inclusive);
curwin->w_cursor.col = 0;
(void)del_bytes((colnr_T)n, !virtual_op,
oap->op_type == OP_DELETE && !oap->is_VIsual);
@@ -2067,7 +2041,7 @@ static void mb_adjust_opend(oparg_T *oap)
static inline void pbyte(pos_T lp, int c)
{
assert(c <= UCHAR_MAX);
- *(ml_get_buf(curbuf, lp.lnum, true) + lp.col) = (char)c;
+ *(ml_get_buf_mut(curbuf, lp.lnum) + lp.col) = (char)c;
if (!curbuf_splice_pending) {
extmark_splice_cols(curbuf, (int)lp.lnum - 1, lp.col, 1, 1, kExtmarkUndo);
}
@@ -2089,10 +2063,7 @@ static void replace_character(int c)
/// Replace a whole area with one character.
static int op_replace(oparg_T *oap, int c)
{
- int n, numc;
- int num_chars;
- char *newp, *oldp;
- colnr_T oldlen;
+ int n;
struct block_def bd;
char *after_p = NULL;
int had_ctrl_v_cr = false;
@@ -2117,6 +2088,11 @@ static int op_replace(oparg_T *oap, int c)
// block mode replace
if (oap->motion_type == kMTBlockWise) {
+ int numc;
+ int num_chars;
+ char *newp;
+ char *oldp;
+ colnr_T oldlen;
bd.is_MAX = (curwin->w_curswant == MAXCOL);
for (; curwin->w_cursor.lnum <= oap->end.lnum; curwin->w_cursor.lnum++) {
curwin->w_cursor.col = 0; // make sure cursor position is valid
@@ -2216,7 +2192,7 @@ static int op_replace(oparg_T *oap, int c)
linenr_T baselnum = curwin->w_cursor.lnum;
if (after_p != NULL) {
ml_append(curwin->w_cursor.lnum++, after_p, (int)after_p_len, false);
- appended_lines_mark(curwin->w_cursor.lnum, 1L);
+ appended_lines_mark(curwin->w_cursor.lnum, 1);
oap->end.lnum++;
xfree(after_p);
}
@@ -2312,7 +2288,7 @@ static int op_replace(oparg_T *oap, int c)
curwin->w_cursor = oap->start;
check_cursor();
- changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L, true);
+ changed_lines(curbuf, oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0, true);
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) {
// Set "'[" and "']" marks.
@@ -2326,7 +2302,6 @@ static int op_replace(oparg_T *oap, int c)
/// Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
void op_tilde(oparg_T *oap)
{
- pos_T pos;
struct block_def bd;
int did_change = false;
@@ -2335,7 +2310,7 @@ void op_tilde(oparg_T *oap)
return;
}
- pos = oap->start;
+ pos_T pos = oap->start;
if (oap->motion_type == kMTBlockWise) { // Visual block mode
for (; pos.lnum <= oap->end.lnum; pos.lnum++) {
int one_change;
@@ -2346,7 +2321,7 @@ void op_tilde(oparg_T *oap)
did_change |= one_change;
}
if (did_change) {
- changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L, true);
+ changed_lines(curbuf, oap->start.lnum, 0, oap->end.lnum + 1, 0, true);
}
} else { // not block mode
if (oap->motion_type == kMTLineWise) {
@@ -2364,18 +2339,18 @@ void op_tilde(oparg_T *oap)
did_change = swapchars(oap->op_type, &pos,
oap->end.col - pos.col + 1);
} else {
- for (;;) {
+ while (true) {
did_change |= swapchars(oap->op_type, &pos,
- pos.lnum == oap->end.lnum ? oap->end.col + 1 :
- (int)strlen(ml_get_pos(&pos)));
+ pos.lnum == oap->end.lnum ? oap->end.col + 1
+ : (int)strlen(ml_get_pos(&pos)));
if (ltoreq(oap->end, pos) || inc(&pos) == -1) {
break;
}
}
}
if (did_change) {
- changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
- 0L, true);
+ changed_lines(curbuf, oap->start.lnum, oap->start.col, oap->end.lnum + 1,
+ 0, true);
}
}
@@ -2391,8 +2366,7 @@ void op_tilde(oparg_T *oap)
}
if (oap->line_count > p_report) {
- smsg(NGETTEXT("%" PRId64 " line changed",
- "%" PRId64 " lines changed", oap->line_count),
+ smsg(0, NGETTEXT("%" PRId64 " line changed", "%" PRId64 " lines changed", oap->line_count),
(int64_t)oap->line_count);
}
}
@@ -2485,15 +2459,12 @@ bool swapchar(int op_type, pos_T *pos)
}
/// Insert and append operators for Visual mode.
-void op_insert(oparg_T *oap, long count1)
+void op_insert(oparg_T *oap, int count1)
{
- long ins_len, pre_textlen = 0;
- char *firstline, *ins_text;
- colnr_T ind_pre_col = 0, ind_post_col;
- int ind_pre_vcol = 0, ind_post_vcol = 0;
+ int pre_textlen = 0;
+ colnr_T ind_pre_col = 0;
+ int ind_pre_vcol = 0;
struct block_def bd;
- int i;
- pos_T t1;
// edit() changes this - record it for OP_APPEND
bd.is_MAX = (curwin->w_curswant == MAXCOL);
@@ -2530,12 +2501,12 @@ void op_insert(oparg_T *oap, long count1)
// Get indent information
ind_pre_col = (colnr_T)getwhitecols_curline();
ind_pre_vcol = get_indent();
- firstline = ml_get(oap->start.lnum) + bd.textcol;
+ char *firstline = ml_get(oap->start.lnum) + bd.textcol;
if (oap->op_type == OP_APPEND) {
firstline += bd.textlen;
}
- pre_textlen = (long)strlen(firstline);
+ pre_textlen = (int)strlen(firstline);
}
if (oap->op_type == OP_APPEND) {
@@ -2553,7 +2524,7 @@ void op_insert(oparg_T *oap, long count1)
if (u_save_cursor() == FAIL) {
return;
}
- for (i = 0; i < bd.endspaces; i++) {
+ for (int i = 0; i < bd.endspaces; i++) {
ins_char(' ');
}
bd.textlen += bd.endspaces;
@@ -2570,7 +2541,7 @@ void op_insert(oparg_T *oap, long count1)
}
}
- t1 = oap->start;
+ pos_T t1 = oap->start;
const pos_T start_insert = curwin->w_cursor;
(void)edit(NUL, false, (linenr_T)count1);
@@ -2590,12 +2561,13 @@ void op_insert(oparg_T *oap, long count1)
}
if (oap->motion_type == kMTBlockWise) {
+ int ind_post_vcol = 0;
struct block_def bd2;
bool did_indent = false;
// if indent kicked in, the firstline might have changed
// but only do that, if the indent actually increased
- ind_post_col = (colnr_T)getwhitecols_curline();
+ colnr_T ind_post_col = (colnr_T)getwhitecols_curline();
if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col) {
bd.textcol += ind_post_col - ind_pre_col;
ind_post_vcol = get_indent();
@@ -2660,7 +2632,7 @@ void op_insert(oparg_T *oap, long count1)
// Subsequent calls to ml_get() flush the firstline data - take a
// copy of the required string.
- firstline = ml_get(oap->start.lnum);
+ char *firstline = ml_get(oap->start.lnum);
const size_t len = strlen(firstline);
colnr_T add = bd.textcol;
colnr_T offset = 0; // offset when cursor was moved in insert mode
@@ -2683,9 +2655,9 @@ void op_insert(oparg_T *oap, long count1)
} else {
firstline += add;
}
- ins_len = (long)strlen(firstline) - pre_textlen - o