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.c1416
1 files changed, 432 insertions, 984 deletions
diff --git a/src/nvim/ops.c b/src/nvim/ops.c
index 3be4536f16..d3a47d77a2 100644
--- a/src/nvim/ops.c
+++ b/src/nvim/ops.c
@@ -1,16 +1,15 @@
// 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
- */
+// ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
+// op_change, op_yank, do_put, do_join
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
+#include "klib/kvec.h"
#include "nvim/ascii.h"
#include "nvim/assert.h"
#include "nvim/buffer.h"
@@ -30,7 +29,6 @@
#include "nvim/globals.h"
#include "nvim/indent.h"
#include "nvim/indent_c.h"
-#include "nvim/lib/kvec.h"
#include "nvim/log.h"
#include "nvim/macros.h"
#include "nvim/mark.h"
@@ -51,6 +49,7 @@
#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/terminal.h"
+#include "nvim/textformat.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
#include "nvim/vim.h"
@@ -66,10 +65,8 @@ static bool clipboard_delay_update = false; // delay clipboard update
static bool clipboard_needs_update = false; // clipboard was updated
static bool clipboard_didwarn = false;
-/*
- * structure used by block_prep, op_delete and op_yank for blockwise operators
- * also op_change, op_shift, op_insert, op_replace - AKelly
- */
+// structure used by block_prep, op_delete and op_yank for blockwise operators
+// also op_change, op_shift, op_insert, op_replace - AKelly
struct block_def {
int startspaces; // 'extra' cols before first char
int endspaces; // 'extra' cols after last char
@@ -78,9 +75,9 @@ struct block_def {
colnr_T textcol; // index of chars (partially) in block
colnr_T start_vcol; // start col of 1st char wholly inside block
colnr_T end_vcol; // start col of 1st char wholly after block
- int is_short; // TRUE if line is too short to fit in block
- int is_MAX; // TRUE if curswant==MAXCOL when starting
- int is_oneChar; // TRUE if block within one character
+ int is_short; // true if line is too short to fit in block
+ int is_MAX; // true if curswant==MAXCOL when starting
+ int is_oneChar; // true if block within one character
int pre_whitesp; // screen cols of ws before block
int pre_whitesp_c; // chars of ws before block
colnr_T end_char_vcols; // number of vcols of post-block char
@@ -95,11 +92,9 @@ struct block_def {
#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 vim.h!!!
+// The third field indicates whether the operator always works on lines.
static char opchars[][3] =
{
{ NUL, NUL, 0 }, // OP_NOP
@@ -181,13 +176,13 @@ int get_op_type(int char1, int char2)
return i;
}
-/// @return TRUE if operator "op" always works on whole lines.
+/// @return true if operator "op" always works on whole lines.
int op_on_lines(int op)
{
return opchars[op][2] & OPF_LINES;
}
-/// @return TRUE if operator "op" changes text.
+/// @return true if operator "op" changes text.
int op_is_change(int op)
{
return opchars[op][2] & OPF_CHANGE;
@@ -224,7 +219,7 @@ void op_shift(oparg_T *oap, int curs_top, int amount)
}
for (i = oap->line_count - 1; i >= 0; i--) {
- first_char = *get_cursor_line_ptr();
+ 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) {
@@ -234,7 +229,7 @@ void op_shift(oparg_T *oap, int curs_top, int amount)
// isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, false);
}
- ++curwin->w_cursor.lnum;
+ curwin->w_cursor.lnum++;
}
if (oap->motion_type == kMTBlockWise) {
@@ -244,7 +239,7 @@ void op_shift(oparg_T *oap, int curs_top, int amount)
curwin->w_cursor.lnum = oap->start.lnum;
beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
} else {
- --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
+ curwin->w_cursor.lnum--; // put cursor on last line, for ":>"
}
// The cursor line is not in a closed fold
foldOpenCursor();
@@ -271,7 +266,7 @@ void op_shift(oparg_T *oap, int curs_top, int amount)
// Set "'[" and "']" marks.
curbuf->b_op_start = oap->start;
curbuf->b_op_end.lnum = oap->end.lnum;
- curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
+ curbuf->b_op_end.col = (colnr_T)strlen(ml_get(oap->end.lnum));
if (curbuf->b_op_end.col > 0) {
curbuf->b_op_end.col--;
}
@@ -355,17 +350,15 @@ static void shift_block(oparg_T *oap, int amount)
return; // multiplication overflow
}
- char_u *const oldp = get_cursor_line_ptr();
+ char_u *const oldp = (char_u *)get_cursor_line_ptr();
int startcol, oldlen, newlen;
if (!left) {
- /*
- * 1. Get start vcol
- * 2. Total ws vcols
- * 3. Divvy into TABs & spp
- * 4. Construct new string
- */
+ // 1. Get start vcol
+ // 2. Total ws vcols
+ // 3. Divvy into TABs & spp
+ // 4. Construct new string
total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
colnr_T ws_vcol = bd.start_vcol - bd.pre_whitesp;
char_u *old_textstart = bd.textstart;
@@ -377,12 +370,20 @@ static void shift_block(oparg_T *oap, int amount)
bd.startspaces = 0;
}
}
- for (; ascii_iswhite(*bd.textstart);) {
- // TODO(fmoralesc): is passing bd.textstart for start of the line OK?
- incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, bd.start_vcol);
+
+ // TODO(vim): is passing bd.textstart for start of the line OK?
+ chartabsize_T cts;
+ init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
+ bd.start_vcol, (char *)bd.textstart, (char *)bd.textstart);
+ while (ascii_iswhite(*cts.cts_ptr)) {
+ incr = lbr_chartabsize_adv(&cts);
total += incr;
- bd.start_vcol += incr;
+ cts.cts_vcol += incr;
}
+ bd.textstart = (char_u *)cts.cts_ptr;
+ bd.start_vcol = cts.cts_vcol;
+ clear_chartabsize_arg(&cts);
+
// OK, now total=all the VWS reqd, and textstart points at the 1st
// non-ws char in the block.
if (!curbuf->b_p_et) {
@@ -419,13 +420,11 @@ static void shift_block(oparg_T *oap, int amount)
// block shift
char_u *non_white = bd.textstart;
- /*
- * Firstly, let's find the first non-whitespace character that is
- * displayed after the block's start column and the character's column
- * number. Also, let's calculate the width of all the whitespace
- * characters that are displayed in the block and precede the searched
- * non-whitespace character.
- */
+ // Firstly, let's find the first non-whitespace character that is
+ // displayed after the block's start column and the character's column
+ // number. Also, let's calculate the width of all the whitespace
+ // characters that are displayed in the block and precede the searched
+ // non-whitespace character.
// If "bd.startspaces" is set, "bd.textstart" points to the character,
// the part of which is displayed at the block's beginning. Let's start
@@ -437,10 +436,16 @@ static void shift_block(oparg_T *oap, int amount)
// The character's column is in "bd.start_vcol".
colnr_T non_white_col = bd.start_vcol;
- while (ascii_iswhite(*non_white)) {
- incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
- non_white_col += incr;
+ chartabsize_T cts;
+ init_chartabsize_arg(&cts, curwin, curwin->w_cursor.lnum,
+ non_white_col, (char *)bd.textstart, (char *)non_white);
+ while (ascii_iswhite(*cts.cts_ptr)) {
+ incr = lbr_chartabsize_adv(&cts);
+ cts.cts_vcol += incr;
}
+ non_white_col = cts.cts_vcol;
+ non_white = (char_u *)cts.cts_ptr;
+ clear_chartabsize_arg(&cts);
const colnr_T block_space_width = non_white_col - oap->start_vcol;
// We will shift by "total" or "block_space_width", whichever is less.
@@ -461,17 +466,19 @@ static void shift_block(oparg_T *oap, int amount)
if (bd.startspaces) {
verbatim_copy_width -= bd.start_char_vcols;
}
- while (verbatim_copy_width < destination_col) {
- char_u *line = verbatim_copy_end;
-
- // TODO: is passing verbatim_copy_end for start of the line OK?
- incr = lbr_chartabsize(line, verbatim_copy_end, verbatim_copy_width);
- if (verbatim_copy_width + incr > destination_col) {
+ init_chartabsize_arg(&cts, curwin, 0, verbatim_copy_width,
+ (char *)bd.textstart, (char *)verbatim_copy_end);
+ while (cts.cts_vcol < destination_col) {
+ incr = lbr_chartabsize(&cts);
+ if (cts.cts_vcol + incr > destination_col) {
break;
}
- verbatim_copy_width += incr;
- MB_PTR_ADV(verbatim_copy_end);
+ cts.cts_vcol += incr;
+ MB_PTR_ADV(cts.cts_ptr);
}
+ verbatim_copy_width = cts.cts_vcol;
+ verbatim_copy_end = (char_u *)cts.cts_ptr;
+ clear_chartabsize_arg(&cts);
// If "destination_col" is different from the width of the initial
// part of the line that will be copied, it means we encountered a tab
@@ -526,7 +533,7 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def
continue; // OP_INSERT, line ends before block start
}
- oldp = ml_get(lnum);
+ oldp = (char_u *)ml_get(lnum);
if (b_insert) {
ts_val = bdp->start_char_vcols;
@@ -555,7 +562,7 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def
if (spaces > 0) {
// avoid copying part of a multi-byte character
- offset -= utf_head_off(oldp, oldp + offset);
+ offset -= utf_head_off((char *)oldp, (char *)oldp + offset);
}
if (spaces < 0) { // can happen when the cursor was moved
spaces = 0;
@@ -651,7 +658,7 @@ void op_reindent(oparg_T *oap, Indenter how)
// indented, unless there is only one line.
if (i != oap->line_count - 1 || oap->line_count == 1
|| how != get_lisp_indent) {
- l = (char_u *)skipwhite((char *)get_cursor_line_ptr());
+ l = (char_u *)skipwhite(get_cursor_line_ptr());
if (*l == NUL) { // empty or blank line
amount = 0;
} else {
@@ -674,15 +681,15 @@ void op_reindent(oparg_T *oap, Indenter how)
curwin->w_cursor.lnum = start_lnum;
beginline(BL_SOL | BL_FIX);
- /* Mark changed lines so that they will be redrawn. When Visual
- * highlighting was present, need to continue until the last line. When
- * there is no change still need to remove the Visual highlighting. */
+ // Mark changed lines so that they will be redrawn. When Visual
+ // 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);
} else if (oap->is_VIsual) {
- redraw_curbuf_later(INVERTED);
+ redraw_curbuf_later(UPD_INVERTED);
}
if (oap->line_count > p_report) {
@@ -698,9 +705,7 @@ void op_reindent(oparg_T *oap, Indenter how)
}
}
-/*
- * Keep the last expression line here, for repeating.
- */
+// Keep the last expression line here, for repeating.
static char_u *expr_line = NULL;
/// Get an expression for the "\"=expr1" or "CTRL-R =expr1"
@@ -717,56 +722,56 @@ int get_expr_register(void)
if (*new_line == NUL) { // use previous line
xfree(new_line);
} else {
- set_expr_line(new_line);
+ set_expr_line((char *)new_line);
}
return '=';
}
/// Set the expression for the '=' register.
/// Argument must be an allocated string.
-void set_expr_line(char_u *new_line)
+void set_expr_line(char *new_line)
{
xfree(expr_line);
- expr_line = new_line;
+ expr_line = (char_u *)new_line;
}
/// Get the result of the '=' register expression.
///
/// @return a pointer to allocated memory, or NULL for failure.
-char_u *get_expr_line(void)
+char *get_expr_line(void)
{
- char_u *expr_copy;
- char_u *rv;
+ char *expr_copy;
+ char *rv;
static int nested = 0;
if (expr_line == NULL) {
return NULL;
}
- /* Make a copy of the expression, because evaluating it may cause it to be
- * changed. */
- expr_copy = vim_strsave(expr_line);
+ // Make a copy of the expression, because evaluating it may cause it to be
+ // changed.
+ expr_copy = xstrdup((char *)expr_line);
- /* When we are invoked recursively limit the evaluation to 10 levels.
- * Then return the string as-is. */
+ // When we are invoked recursively limit the evaluation to 10 levels.
+ // Then return the string as-is.
if (nested >= 10) {
return expr_copy;
}
nested++;
- rv = (char_u *)eval_to_string((char *)expr_copy, NULL, true);
+ rv = eval_to_string(expr_copy, NULL, true);
nested--;
xfree(expr_copy);
return rv;
}
/// Get the '=' register expression itself, without evaluating it.
-char_u *get_expr_line_src(void)
+char *get_expr_line_src(void)
{
if (expr_line == NULL) {
return NULL;
}
- return vim_strsave(expr_line);
+ return xstrdup((char *)expr_line);
}
/// @return whether `regname` is a valid name of a yank register.
@@ -891,7 +896,6 @@ int do_record(int c)
{
char_u *p;
static int regname;
- static bool changed_cmdheight = false;
yankreg_T *old_y_previous;
int retval;
@@ -902,18 +906,11 @@ int do_record(int c)
retval = FAIL;
} else {
reg_recording = c;
+ // TODO(bfredl): showmode based messaging is currently missing with cmdheight=0
showmode();
regname = c;
retval = OK;
- if (!ui_has_messages()) {
- // Enable macro indicator temporarily
- set_option_value("ch", 1L, NULL, 0);
- update_screen(VALID);
-
- changed_cmdheight = true;
- }
-
apply_autocmds(EVENT_RECORDINGENTER, NULL, NULL, false, curbuf);
}
} else { // stop recording
@@ -943,7 +940,7 @@ int do_record(int c)
restore_v_event(dict, &save_v_event);
reg_recorded = reg_recording;
reg_recording = 0;
- if (ui_has(kUIMessages)) {
+ if (p_ch == 0 || ui_has(kUIMessages)) {
showmode();
} else {
msg("");
@@ -959,12 +956,6 @@ int do_record(int c)
y_previous = old_y_previous;
}
-
- if (changed_cmdheight) {
- // Restore cmdheight
- set_option_value("ch", 0L, NULL, 0);
- redraw_all_later(CLEAR);
- }
}
return retval;
}
@@ -997,7 +988,7 @@ static int stuff_yank(int regname, char_u *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_u *lp = xmalloc(STRLEN(*pp) + STRLEN(p) + 1);
+ char_u *lp = xmalloc(strlen(*pp) + STRLEN(p) + 1);
STRCPY(lp, *pp);
// TODO(philix): use xstpcpy() in stuff_yank()
STRCAT(lp, p);
@@ -1065,11 +1056,11 @@ static char_u *execreg_line_continuation(char **lines, size_t *idx)
}
}
ga_append(&ga, NUL);
- char_u *str = vim_strsave(ga.ga_data);
+ char *str = xstrdup(ga.ga_data);
ga_clear(&ga);
*idx = i;
- return str;
+ return (char_u *)str;
}
/// Execute a yank register: copy it into the stuff buffer
@@ -1110,7 +1101,7 @@ 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,
+ p = vim_strsave_escaped_ext((char_u *)last_cmdline,
(char_u *)"\001\002\003\004\005\006\007"
"\010\011\012\013\014\015\016\017"
"\020\021\022\023\024\025\026\027"
@@ -1125,7 +1116,7 @@ int do_execreg(int regname, int colon, int addcr, int silent)
}
xfree(p);
} else if (regname == '=') {
- p = get_expr_line();
+ p = (char_u *)get_expr_line();
if (p == NULL) {
return FAIL;
}
@@ -1148,9 +1139,7 @@ int do_execreg(int regname, int colon, int addcr, int silent)
// Disallow remapping for ":@r".
int remap = colon ? REMAP_NONE : REMAP_YES;
- /*
- * Insert lines into typeahead buffer, from last one to first one.
- */
+ // 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
@@ -1260,11 +1249,9 @@ int insert_reg(int regname, bool literally_arg)
bool allocated;
const bool literally = literally_arg || is_literal_register(regname);
- /*
- * It is possible to get into an endless loop by having CTRL-R a in
- * register a and then, in insert mode, doing CTRL-R a.
- * If you hit CTRL-C, the loop will be broken here.
- */
+ // It is possible to get into an endless loop by having CTRL-R a in
+ // register a and then, in insert mode, doing CTRL-R a.
+ // If you hit CTRL-C, the loop will be broken here.
os_breakcheck();
if (got_int) {
return FAIL;
@@ -1311,34 +1298,6 @@ int insert_reg(int regname, bool literally_arg)
return retval;
}
-/// Stuff a string into the typeahead buffer, such that edit() will insert it
-/// literally ("literally" true) or interpret is as typed characters.
-static void stuffescaped(const char *arg, bool literally)
-{
- while (*arg != NUL) {
- // Stuff a sequence of normal ASCII characters, that's fast. Also
- // stuff K_SPECIAL to get the effect of a special key when "literally"
- // is true.
- const char *const start = arg;
- while ((*arg >= ' ' && *arg < DEL) || ((uint8_t)(*arg) == K_SPECIAL
- && !literally)) {
- arg++;
- }
- if (arg > start) {
- stuffReadbuffLen(start, (arg - start));
- }
-
- // stuff a single special character
- if (*arg != NUL) {
- const int c = mb_cptr2char_adv((const char_u **)&arg);
- if (literally && ((c < ' ' && c != TAB) || c == DEL)) {
- stuffcharReadbuff(Ctrl_V);
- }
- stuffcharReadbuff(c);
- }
- }
-}
-
/// If "regname" is a special register, return true and store a pointer to its
/// value in "argp".
///
@@ -1365,7 +1324,7 @@ bool get_spec_reg(int regname, char **argp, bool *allocated, bool errmsg)
return true;
case '=': // result of expression
- *argp = (char *)get_expr_line();
+ *argp = get_expr_line();
*allocated = true;
return true;
@@ -1373,7 +1332,7 @@ bool get_spec_reg(int regname, char **argp, bool *allocated, bool errmsg)
if (last_cmdline == NULL && errmsg) {
emsg(_(e_nolastcmd));
}
- *argp = (char *)last_cmdline;
+ *argp = last_cmdline;
return true;
case '/': // last search-pattern
@@ -1419,7 +1378,7 @@ bool get_spec_reg(int regname, char **argp, bool *allocated, bool errmsg)
return false;
}
- *argp = (char *)ml_get_buf(curwin->w_buffer, curwin->w_cursor.lnum, false);
+ *argp = ml_get_buf(curwin->w_buffer, curwin->w_cursor.lnum, false);
return true;
case '_': // black hole: always empty
@@ -1458,8 +1417,8 @@ bool cmdline_paste_reg(int regname, bool literally_arg, bool remcr)
cmdline_paste_str((char_u *)"\r", literally);
}
- /* Check for CTRL-C, in case someone tries to paste a few thousand
- * lines and gets bored. */
+ // Check for CTRL-C, in case someone tries to paste a few thousand
+ // lines and gets bored.
os_breakcheck();
if (got_int) {
return FAIL;
@@ -1514,17 +1473,15 @@ int op_delete(oparg_T *oap)
mb_adjust_opend(oap);
- /*
- * Imitate the strange Vi behaviour: If the delete spans more than one
- * line and motion_type == kMTCharWise and the result is a blank line, make the
- * delete linewise. Don't do this for the change command or Visual mode.
- */
+ // Imitate the strange Vi behaviour: If the delete spans more than one
+ // line and motion_type == kMTCharWise and the result is a blank line, make the
+ // delete linewise. Don't do this for the change command or Visual mode.
if (oap->motion_type == kMTCharWise
&& !oap->is_VIsual
&& oap->line_count > 1
&& oap->motion_force == NUL
&& oap->op_type == OP_DELETE) {
- ptr = ml_get(oap->end.lnum) + oap->end.col;
+ ptr = (char_u *)ml_get(oap->end.lnum) + oap->end.col;
if (*ptr != NUL) {
ptr += oap->inclusive;
}
@@ -1534,10 +1491,8 @@ int op_delete(oparg_T *oap)
}
}
- /*
- * Check for trying to delete (e.g. "D") in an empty line.
- * Note: For the change operator it is ok.
- */
+ // Check for trying to delete (e.g. "D") in an empty line.
+ // Note: For the change operator it is ok.
if (oap->motion_type != kMTLineWise
&& oap->line_count == 1
&& oap->op_type == OP_DELETE
@@ -1555,11 +1510,9 @@ int op_delete(oparg_T *oap)
return OK;
}
- /*
- * Do a yank of whatever we're about to delete.
- * If a yank register was specified, put the deleted text into that
- * register. For the black hole register '_' don't yank anything.
- */
+ // Do a yank of whatever we're about to delete.
+ // If a yank register was specified, put the deleted text into that
+ // register. For the black hole register '_' don't yank anything.
if (oap->regname != '_') {
yankreg_T *reg = NULL;
int did_yank = false;
@@ -1577,6 +1530,7 @@ int op_delete(oparg_T *oap)
// Put deleted text into register 1 and shift number registers if the
// delete contains a line break, or when using a specific operator (Vi
// compatible)
+
if (oap->motion_type == kMTLineWise || oap->line_count > 1 || oap->use_reg_one) {
shift_delete_registers(is_append_register(oap->regname));
reg = &y_regs[1];
@@ -1602,9 +1556,7 @@ int op_delete(oparg_T *oap)
}
}
- /*
- * block mode delete
- */
+ // block mode delete
if (oap->motion_type == kMTBlockWise) {
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL) {
@@ -1627,7 +1579,7 @@ int op_delete(oparg_T *oap)
// 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);
+ oldp = (char_u *)ml_get(lnum);
newp = (char_u *)xmalloc(STRLEN(oldp) - (size_t)n + 1);
// copy up to deleted part
memmove(newp, oldp, (size_t)bd.textcol);
@@ -1672,7 +1624,7 @@ int op_delete(oparg_T *oap)
beginline(0); // cursor in column 0
}
- int old_len = (int)STRLEN(ml_get(curwin->w_cursor.lnum));
+ int old_len = (int)strlen(ml_get(curwin->w_cursor.lnum));
truncate_line(false); // delete the rest of the line
extmark_splice_cols(curbuf,
@@ -1745,7 +1697,7 @@ int op_delete(oparg_T *oap)
if (virtual_op) {
// fix up things for virtualedit-delete:
// break the tabs which are going to get in our way
- char_u *curline = get_cursor_line_ptr();
+ char_u *curline = (char_u *)get_cursor_line_ptr();
int len = (int)STRLEN(curline);
if (oap->end.coladd != 0
@@ -1826,7 +1778,7 @@ setmarks:
static void mb_adjust_opend(oparg_T *oap)
{
if (oap->inclusive) {
- char *p = (char *)ml_get(oap->end.lnum);
+ char *p = ml_get(oap->end.lnum);
oap->end.col += utf_cp_tail_off(p, p + oap->end.col);
}
}
@@ -1835,7 +1787,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_u)c;
+ *(ml_get_buf(curbuf, lp.lnum, true) + lp.col) = (char)c;
if (!curbuf_splice_pending) {
extmark_splice_cols(curbuf, (int)lp.lnum - 1, lp.col, 1, 1, kExtmarkUndo);
}
@@ -1883,9 +1835,7 @@ static int op_replace(oparg_T *oap, int c)
return FAIL;
}
- /*
- * block mode replace
- */
+ // block mode replace
if (oap->motion_type == kMTBlockWise) {
bd.is_MAX = (curwin->w_curswant == MAXCOL);
for (; curwin->w_cursor.lnum <= oap->end.lnum; curwin->w_cursor.lnum++) {
@@ -1936,7 +1886,7 @@ static int op_replace(oparg_T *oap, int c)
num_chars = numc;
numc *= utf_char2len(c);
- oldp = get_cursor_line_ptr();
+ oldp = (char_u *)get_cursor_line_ptr();
oldlen = (int)STRLEN(oldp);
size_t newp_size = (size_t)bd.textcol + (size_t)bd.startspaces;
@@ -2000,9 +1950,9 @@ static int op_replace(oparg_T *oap, int c)
if (oap->motion_type == kMTLineWise) {
oap->start.col = 0;
curwin->w_cursor.col = 0;
- oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
+ oap->end.col = (colnr_T)strlen(ml_get(oap->end.lnum));
if (oap->end.col) {
- --oap->end.col;
+ oap->end.col--;
}
} else if (!oap->inclusive) {
dec(&(oap->end));
@@ -2011,10 +1961,12 @@ static int op_replace(oparg_T *oap, int c)
// TODO(bfredl): we could batch all the splicing
// done on the same line, at least
while (ltoreq(curwin->w_cursor, oap->end)) {
+ bool done = false;
+
n = gchar_cursor();
if (n != NUL) {
int new_byte_len = utf_char2len(c);
- int old_byte_len = utfc_ptr2len((char *)get_cursor_pos_ptr());
+ int old_byte_len = utfc_ptr2len(get_cursor_pos_ptr());
if (new_byte_len > 1 || old_byte_len > 1) {
// This is slow, but it handles replacing a single-byte
@@ -2023,6 +1975,7 @@ static int op_replace(oparg_T *oap, int c)
oap->end.col += new_byte_len - old_byte_len;
}
replace_character(c);
+ done = true;
} else {
if (n == TAB) {
int end_vcol = 0;
@@ -2038,9 +1991,14 @@ static int op_replace(oparg_T *oap, int c)
getvpos(&oap->end, end_vcol);
}
}
- pbyte(curwin->w_cursor, c);
+ // with "coladd" set may move to just after a TAB
+ if (gchar_cursor() != NUL) {
+ pbyte(curwin->w_cursor, c);
+ done = true;
+ }
}
- } else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum) {
+ }
+ if (!done && virtual_op && curwin->w_cursor.lnum == oap->end.lnum) {
int virtcols = oap->end.coladd;
if (curwin->w_cursor.lnum == oap->start.lnum
@@ -2090,7 +2048,7 @@ void op_tilde(oparg_T *oap)
{
pos_T pos;
struct block_def bd;
- int did_change = FALSE;
+ int did_change = false;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL) {
@@ -2114,9 +2072,9 @@ void op_tilde(oparg_T *oap)
if (oap->motion_type == kMTLineWise) {
oap->start.col = 0;
pos.col = 0;
- oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
+ oap->end.col = (colnr_T)strlen(ml_get(oap->end.lnum));
if (oap->end.col) {
- --oap->end.col;
+ oap->end.col--;
}
} else if (!oap->inclusive) {
dec(&(oap->end));
@@ -2143,7 +2101,7 @@ void op_tilde(oparg_T *oap)
if (!did_change && oap->is_VIsual) {
// No change: need to remove the Visual selection
- redraw_curbuf_later(INVERTED);
+ redraw_curbuf_later(UPD_INVERTED);
}
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) {
@@ -2165,7 +2123,7 @@ void op_tilde(oparg_T *oap)
/// @param length is rounded up to include the whole last multi-byte character.
/// Also works correctly when the number of bytes changes.
///
-/// @return TRUE if some character was changed.
+/// @return true if some character was changed.
static int swapchars(int op_type, pos_T *pos, int length)
FUNC_ATTR_NONNULL_ALL
{
@@ -2235,7 +2193,7 @@ bool swapchar(int op_type, pos_T *pos)
curwin->w_cursor = *pos;
// don't use del_char(), it also removes composing chars
- del_bytes(utf_ptr2len((char *)get_cursor_pos_ptr()), false, false);
+ del_bytes(utf_ptr2len(get_cursor_pos_ptr()), false, false);
ins_char(nc);
curwin->w_cursor = sp;
} else {
@@ -2250,7 +2208,7 @@ bool swapchar(int op_type, pos_T *pos)
void op_insert(oparg_T *oap, long count1)
{
long ins_len, pre_textlen = 0;
- char_u *firstline, *ins_text;
+ char *firstline, *ins_text;
colnr_T ind_pre_col = 0, ind_post_col;
int ind_pre_vcol = 0, ind_post_vcol = 0;
struct block_def bd;
@@ -2262,7 +2220,8 @@ void op_insert(oparg_T *oap, long count1)
// vis block is still marked. Get rid of it now.
curwin->w_cursor.lnum = oap->start.lnum;
- update_screen(INVERTED);
+ redraw_curbuf_later(UPD_INVERTED);
+ update_screen();
if (oap->motion_type == kMTBlockWise) {
// When 'virtualedit' is used, need to insert the extra spaces before
@@ -2282,7 +2241,7 @@ void op_insert(oparg_T *oap, long count1)
coladvance_force(oap->op_type == OP_APPEND
? oap->end_vcol + 1 : getviscol());
if (oap->op_type == OP_APPEND) {
- --curwin->w_cursor.col;
+ curwin->w_cursor.col--;
}
curwin->w_ve_flags = old_ve_flags;
}
@@ -2296,17 +2255,17 @@ void op_insert(oparg_T *oap, long count1)
if (oap->op_type == OP_APPEND) {
firstline += bd.textlen;
}
- pre_textlen = (long)STRLEN(firstline);
+ pre_textlen = (long)strlen(firstline);
}
if (oap->op_type == OP_APPEND) {
if (oap->motion_type == kMTBlockWise
&& curwin->w_cursor.coladd == 0) {
// Move the cursor to the character right of the block.
- curwin->w_set_curswant = TRUE;
+ curwin->w_set_curswant = true;
while (*get_cursor_pos_ptr() != NUL
&& (curwin->w_cursor.col < bd.textcol + bd.textlen)) {
- ++curwin->w_cursor.col;
+ curwin->w_cursor.col++;
}
if (bd.is_short && !bd.is_MAX) {
// First line was too short, make it longer and adjust the
@@ -2412,19 +2371,17 @@ void op_insert(oparg_T *oap, long count1)
if (oap->op_type == OP_APPEND) {
pre_textlen += bd2.textlen - bd.textlen;
if (bd2.endspaces) {
- --bd2.textlen;
+ bd2.textlen--;
}
}
bd.textcol = bd2.textcol;
bd.textlen = bd2.textlen;
}
- /*
- * Subsequent calls to ml_get() flush the firstline data - take a
- * copy of the required string.
- */
+ // Subsequent calls to ml_get() flush the firstline data - take a
+ // copy of the required string.
firstline = ml_get(oap->start.lnum);
- const size_t len = STRLEN(firstline);
+ const size_t len = strlen(firstline);
colnr_T add = bd.textcol;
colnr_T offset = 0; // offset when cursor was moved in insert mode
if (oap->op_type == OP_APPEND) {
@@ -2446,12 +2403,12 @@ void op_insert(oparg_T *oap, long count1)
} else {
firstline += add;
}
- ins_len = (long)STRLEN(firstline) - pre_textlen - offset;
+ ins_len = (long)strlen(firstline) - pre_textlen - offset;
if (pre_textlen >= 0 && ins_len > 0) {
- ins_text = vim_strnsave(firstline, (size_t)ins_len);
+ ins_text = xstrnsave(firstline, (size_t)ins_len);
// block handled here
if (u_save(oap->start.lnum, (linenr_T)(oap->end.lnum + 1)) == OK) {
- block_insert(oap, ins_text, (oap->op_type == OP_INSERT), &bd);
+ block_insert(oap, (char_u *)ins_text, (oap->op_type == OP_INSERT), &bd);
}
curwin->w_cursor.col = oap->start.col;
@@ -2463,7 +2420,7 @@ void op_insert(oparg_T *oap, long count1)
/// handle a change operation
///
-/// @return TRUE if edit() returns because of a CTRL-O command
+/// @return true if edit() returns because of a CTRL-O command
int op_change(oparg_T *oap)
{
colnr_T l;
@@ -2489,10 +2446,10 @@ int op_change(oparg_T *oap)
// save for undo
if (curbuf->b_ml.ml_flags & ML_EMPTY) {
if (u_save_cursor() == FAIL) {
- return FALSE;
+ return false;
}
} else if (op_delete(oap) == FAIL) {
- return FALSE;
+ return false;
}
if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
@@ -2508,9 +2465,9 @@ int op_change(oparg_T *oap)
|| gchar_cursor() == NUL)) {
coladvance_force(getviscol());
}
- firstline = ml_get(oap->start.lnum);
+ firstline = (char_u *)ml_get(oap->start.lnum);
pre_textlen = (long)STRLEN(firstline);
- pre_indent = (long)getwhitecols(firstline);
+ pre_indent = (long)getwhitecols((char *)firstline);
bd.textcol = curwin->w_cursor.col;
}
@@ -2518,20 +2475,18 @@ int op_change(oparg_T *oap)
fix_indent();
}
- retval = edit(NUL, FALSE, (linenr_T)1);
+ retval = edit(NUL, false, (linenr_T)1);
- /*
- * In Visual block mode, handle copying the new text to all lines of the
- * block.
- * Don't repeat the insert when Insert mode ended with CTRL-C.
- */
+ // In Visual block mode, handle copying the new text to all lines of the
+ // block.
+ // Don't repeat the insert when Insert mode ended with CTRL-C.
if (oap->motion_type == kMTBlockWise
&& oap->start.lnum != oap->end.lnum && !got_int) {
// Auto-indenting may have changed the indent. If the cursor was past
// the indent, exclude that indent change from the inserted text.
- firstline = ml_get(oap->start.lnum);
+ firstline = (char_u *)ml_get(oap->start.lnum);
if (bd.textcol > (colnr_T)pre_indent) {
- long new_indent = (long)getwhitecols(firstline);
+ long new_indent = (long)getwhitecols((char *)firstline);
pre_textlen += new_indent - pre_indent;
bd.textcol += (colnr_T)(new_indent - pre_indent);
@@ -2557,7 +2512,7 @@ int op_change(oparg_T *oap)
} else {
vpos.coladd = 0;
}
- oldp = ml_get(linenr);
+ oldp = (char_u *)ml_get(linenr);
newp = xmalloc(STRLEN(oldp) + (size_t)vpos.coladd
+ (size_t)ins_len + 1);
// copy up to block start
@@ -2701,14 +2656,14 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append)
break;
case kMTLineWise:
- reg->y_array[y_idx] = (char *)vim_strsave(ml_get(lnum));
+ reg->y_array[y_idx] = xstrdup(ml_get(lnum));
break;
case kMTCharWise: {
colnr_T startcol = 0, endcol = MAXCOL;
int is_oneChar = false;
colnr_T cs, ce;
- p = ml_get(lnum);
+ p = (char_u *)ml_get(lnum);
bd.startspaces = 0;
bd.endspaces = 0;
@@ -2717,8 +2672,7 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append)
if (virtual_op) {
getvcol(curwin, &oap->start, &cs, NULL, &ce);
if (ce != cs && oap->start.coladd > 0) {
- /* Part of a tab selected -- but don't
- * double-count it. */
+ // Part of a tab selected -- but don't double-count it.
bd.startspaces = (ce - cs + 1)
- oap->start.coladd;
startcol++;
@@ -2734,7 +2688,7 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append)
// Don't add space for double-wide
// char; endcol will be on last byte
// of multi-byte char.
- && utf_head_off(p, p + endcol) == 0)) {
+ && utf_head_off((char *)p, (char *)p + endcol) == 0)) {
if (oap->start.lnum == oap->end.lnum
&& oap->start.col == oap->end.col) {
// Special case: inside a single char
@@ -2786,8 +2740,8 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append)
// the new block, unless being Vi compatible.