2016-08-29 22:49:24 +02:00
|
|
|
/* vi:set ts=8 sts=4 sw=4 noet:
|
2004-06-13 20:20:40 +00:00
|
|
|
*
|
|
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
|
|
*
|
|
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
|
|
* See README.txt for an overview of the Vim source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
|
|
|
|
* op_change, op_yank, do_put, do_join
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vim.h"
|
|
|
|
|
2016-01-29 22:47:03 +01:00
|
|
|
static void shift_block(oparg_T *oap, int amount);
|
|
|
|
static void mb_adjust_opend(oparg_T *oap);
|
|
|
|
static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
|
|
|
|
static int ends_in_white(linenr_T lnum);
|
|
|
|
static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2018-06-03 14:47:35 +02:00
|
|
|
// Flags for third item in "opchars".
|
|
|
|
#define OPF_LINES 1 // operator always works on lines
|
|
|
|
#define OPF_CHANGE 2 // operator changes text
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* The names of operators.
|
|
|
|
* IMPORTANT: Index must correspond with defines in vim.h!!!
|
2018-06-03 14:47:35 +02:00
|
|
|
* The third field holds OPF_ flags.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
static char opchars[][3] =
|
|
|
|
{
|
2018-06-03 14:47:35 +02:00
|
|
|
{NUL, NUL, 0}, // OP_NOP
|
|
|
|
{'d', NUL, OPF_CHANGE}, // OP_DELETE
|
|
|
|
{'y', NUL, 0}, // OP_YANK
|
|
|
|
{'c', NUL, OPF_CHANGE}, // OP_CHANGE
|
|
|
|
{'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
|
|
|
|
{'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
|
|
|
|
{'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
|
|
|
|
{'g', '~', OPF_CHANGE}, // OP_TILDE
|
|
|
|
{'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
|
|
|
|
{'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
|
|
|
|
{':', NUL, OPF_LINES}, // OP_COLON
|
|
|
|
{'g', 'U', OPF_CHANGE}, // OP_UPPER
|
|
|
|
{'g', 'u', OPF_CHANGE}, // OP_LOWER
|
|
|
|
{'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
|
|
|
|
{'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
|
|
|
|
{'g', '?', OPF_CHANGE}, // OP_ROT13
|
|
|
|
{'r', NUL, OPF_CHANGE}, // OP_REPLACE
|
|
|
|
{'I', NUL, OPF_CHANGE}, // OP_INSERT
|
|
|
|
{'A', NUL, OPF_CHANGE}, // OP_APPEND
|
|
|
|
{'z', 'f', OPF_LINES}, // OP_FOLD
|
|
|
|
{'z', 'o', OPF_LINES}, // OP_FOLDOPEN
|
|
|
|
{'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
|
|
|
|
{'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
|
|
|
|
{'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
|
|
|
|
{'z', 'd', OPF_LINES}, // OP_FOLDDEL
|
|
|
|
{'z', 'D', OPF_LINES}, // OP_FOLDDELREC
|
|
|
|
{'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
|
|
|
|
{'g', '@', OPF_CHANGE}, // OP_FUNCTION
|
|
|
|
{Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
|
|
|
|
{Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
|
2004-06-13 20:20:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate a command name into an operator type.
|
|
|
|
* Must only be called with a valid operator name!
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 19:39:49 +01:00
|
|
|
get_op_type(int char1, int char2)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (char1 == 'r') /* ignore second character */
|
|
|
|
return OP_REPLACE;
|
|
|
|
if (char1 == '~') /* when tilde is an operator */
|
|
|
|
return OP_TILDE;
|
2016-01-10 22:13:02 +01:00
|
|
|
if (char1 == 'g' && char2 == Ctrl_A) /* add */
|
|
|
|
return OP_NR_ADD;
|
|
|
|
if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
|
|
|
|
return OP_NR_SUB;
|
2004-06-13 20:20:40 +00:00
|
|
|
for (i = 0; ; ++i)
|
2017-12-19 12:27:23 +01:00
|
|
|
{
|
2004-06-13 20:20:40 +00:00
|
|
|
if (opchars[i][0] == char1 && opchars[i][1] == char2)
|
|
|
|
break;
|
2017-12-19 12:27:23 +01:00
|
|
|
if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
|
|
|
|
{
|
|
|
|
internal_error("get_op_type()");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return TRUE if operator "op" always works on whole lines.
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 19:39:49 +01:00
|
|
|
op_on_lines(int op)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2018-06-03 14:47:35 +02:00
|
|
|
return opchars[op][2] & OPF_LINES;
|
|
|
|
}
|
|
|
|
|
2019-01-20 15:30:40 +01:00
|
|
|
#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
|
2018-06-03 14:47:35 +02:00
|
|
|
/*
|
|
|
|
* Return TRUE if operator "op" changes text.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
op_is_change(int op)
|
|
|
|
{
|
|
|
|
return opchars[op][2] & OPF_CHANGE;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-01-20 15:30:40 +01:00
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get first operator command character.
|
|
|
|
* Returns 'g' or 'z' if there is another command character.
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 19:39:49 +01:00
|
|
|
get_op_char(int optype)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
return opchars[optype][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get second operator command character.
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 19:39:49 +01:00
|
|
|
get_extra_op_char(int optype)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
return opchars[optype][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* op_shift - handle a shift operation
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 19:39:49 +01:00
|
|
|
op_shift(oparg_T *oap, int curs_top, int amount)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
long i;
|
|
|
|
int first_char;
|
|
|
|
int block_col = 0;
|
|
|
|
|
|
|
|
if (u_save((linenr_T)(oap->start.lnum - 1),
|
|
|
|
(linenr_T)(oap->end.lnum + 1)) == FAIL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (oap->block_mode)
|
|
|
|
block_col = curwin->w_cursor.col;
|
|
|
|
|
|
|
|
for (i = oap->line_count; --i >= 0; )
|
|
|
|
{
|
|
|
|
first_char = *ml_get_curline();
|
|
|
|
if (first_char == NUL) /* empty line */
|
|
|
|
curwin->w_cursor.col = 0;
|
|
|
|
else if (oap->block_mode)
|
|
|
|
shift_block(oap, amount);
|
|
|
|
else
|
|
|
|
/* Move the line right if it doesn't start with '#', 'smartindent'
|
|
|
|
* isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
|
|
|
|
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
|
|
|
|
if (first_char != '#' || !preprocs_left())
|
|
|
|
#endif
|
2008-01-16 19:03:13 +00:00
|
|
|
shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
++curwin->w_cursor.lnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
|
|
|
|
if (oap->block_mode)
|
|
|
|
{
|
|
|
|
curwin->w_cursor.lnum = oap->start.lnum;
|
|
|
|
curwin->w_cursor.col = block_col;
|
|
|
|
}
|
2014-03-23 15:13:05 +01:00
|
|
|
else if (curs_top) /* put cursor on first line, for ">>" */
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
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 ":>" */
|
|
|
|
|
2017-01-02 14:57:08 +01:00
|
|
|
#ifdef FEAT_FOLDING
|
|
|
|
/* The cursor line is not in a closed fold */
|
|
|
|
foldOpenCursor();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if (oap->line_count > p_report)
|
|
|
|
{
|
2018-08-21 15:12:14 +02:00
|
|
|
char *op;
|
|
|
|
char *msg_line_single;
|
|
|
|
char *msg_line_plural;
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if (oap->op_type == OP_RSHIFT)
|
2018-08-21 15:12:14 +02:00
|
|
|
op = ">";
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
2018-08-21 15:12:14 +02:00
|
|
|
op = "<";
|
|
|
|
msg_line_single = NGETTEXT("%ld line %sed %d time",
|
|
|
|
"%ld line %sed %d times", amount);
|
|
|
|
msg_line_plural = NGETTEXT("%ld lines %sed %d time",
|
|
|
|
"%ld lines %sed %d times", amount);
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE,
|
|
|
|
NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
|
|
|
|
oap->line_count, op, amount);
|
2019-01-19 17:43:09 +01:00
|
|
|
msg((char *)IObuff);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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));
|
|
|
|
if (curbuf->b_op_end.col > 0)
|
|
|
|
--curbuf->b_op_end.col;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-01-11 14:37:20 +01:00
|
|
|
* Shift the current line one shiftwidth left (if left != 0) or right
|
|
|
|
* leaves cursor on first blank in the line.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
void
|
2016-01-30 19:39:49 +01:00
|
|
|
shift_line(
|
|
|
|
int left,
|
|
|
|
int round,
|
|
|
|
int amount,
|
2019-09-28 19:05:57 +02:00
|
|
|
int call_changed_bytes) // call changed_bytes()
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
int i, j;
|
2019-09-16 21:06:21 +02:00
|
|
|
int sw_val = (int)get_sw_value_indent(curbuf);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-28 19:05:57 +02:00
|
|
|
count = get_indent(); // get current indent
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-28 19:05:57 +02:00
|
|
|
if (round) // round off indent
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-28 19:05:57 +02:00
|
|
|
i = count / sw_val; // number of 'shiftwidth' rounded down
|
|
|
|
j = count % sw_val; // extra spaces
|
|
|
|
if (j && left) // first remove extra spaces
|
2004-06-13 20:20:40 +00:00
|
|
|
--amount;
|
|
|
|
if (left)
|
|
|
|
{
|
|
|
|
i -= amount;
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i += amount;
|
2019-09-16 21:06:21 +02:00
|
|
|
count = i * sw_val;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-28 19:05:57 +02:00
|
|
|
else // original vi indent
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (left)
|
|
|
|
{
|
2019-09-16 21:06:21 +02:00
|
|
|
count -= sw_val * amount;
|
2004-06-13 20:20:40 +00:00
|
|
|
if (count < 0)
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
else
|
2019-09-16 21:06:21 +02:00
|
|
|
count += sw_val * amount;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 19:05:57 +02:00
|
|
|
// Set new indent
|
2004-06-13 20:20:40 +00:00
|
|
|
if (State & VREPLACE_FLAG)
|
2008-01-16 19:03:13 +00:00
|
|
|
change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
2008-01-16 19:03:13 +00:00
|
|
|
(void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shift one line of the current block one shiftwidth right or left.
|
|
|
|
* Leaves cursor on first character in block.
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-30 19:39:49 +01:00
|
|
|
shift_block(oparg_T *oap, int amount)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
int left = (oap->op_type == OP_LSHIFT);
|
|
|
|
int oldstate = State;
|
2009-03-11 15:28:26 +00:00
|
|
|
int total;
|
|
|
|
char_u *newp, *oldp;
|
2004-06-13 20:20:40 +00:00
|
|
|
int oldcol = curwin->w_cursor.col;
|
2019-09-16 21:06:21 +02:00
|
|
|
int sw_val = (int)get_sw_value_indent(curbuf);
|
|
|
|
int ts_val = (int)curbuf->b_p_ts;
|
2004-06-13 20:20:40 +00:00
|
|
|
struct block_def bd;
|
|
|
|
int incr;
|
2009-03-11 15:28:26 +00:00
|
|
|
colnr_T ws_vcol;
|
2004-06-13 20:20:40 +00:00
|
|
|
int i = 0, j = 0;
|
|
|
|
int len;
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
|
|
|
int old_p_ri = p_ri;
|
|
|
|
|
2013-05-06 04:24:17 +02:00
|
|
|
p_ri = 0; /* don't want revins in indent */
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
State = INSERT; /* don't want REPLACE for State */
|
|
|
|
block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
|
|
|
|
if (bd.is_short)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* total is number of screen columns to be inserted/removed */
|
2019-09-16 21:06:21 +02:00
|
|
|
total = (int)((unsigned)amount * (unsigned)sw_val);
|
|
|
|
if ((total / sw_val) != amount)
|
2017-08-06 15:42:06 +02:00
|
|
|
return; /* multiplication overflow */
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
oldp = ml_get_curline();
|
|
|
|
|
|
|
|
if (!left)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 1. Get start vcol
|
|
|
|
* 2. Total ws vcols
|
|
|
|
* 3. Divvy into TABs & spp
|
|
|
|
* 4. Construct new string
|
|
|
|
*/
|
|
|
|
total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
|
|
|
|
ws_vcol = bd.start_vcol - bd.pre_whitesp;
|
|
|
|
if (bd.startspaces)
|
|
|
|
{
|
|
|
|
if (has_mbyte)
|
2016-03-05 17:25:39 +01:00
|
|
|
{
|
|
|
|
if ((*mb_ptr2len)(bd.textstart) == 1)
|
|
|
|
++bd.textstart;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ws_vcol = 0;
|
|
|
|
bd.startspaces = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-11-11 16:22:28 +00:00
|
|
|
else
|
|
|
|
++bd.textstart;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2017-03-12 20:10:05 +01:00
|
|
|
for ( ; VIM_ISWHITE(*bd.textstart); )
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2014-06-25 14:39:50 +02:00
|
|
|
/* TODO: is passing bd.textstart for start of the line OK? */
|
|
|
|
incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
|
|
|
|
(colnr_T)(bd.start_vcol));
|
2004-06-13 20:20:40 +00:00
|
|
|
total += incr;
|
|
|
|
bd.start_vcol += incr;
|
|
|
|
}
|
|
|
|
/* OK, now total=all the VWS reqd, and textstart points at the 1st
|
|
|
|
* non-ws char in the block. */
|
2018-06-23 19:23:02 +02:00
|
|
|
#ifdef FEAT_VARTABS
|
|
|
|
if (!curbuf->b_p_et)
|
2019-09-16 21:06:21 +02:00
|
|
|
tabstop_fromto(ws_vcol, ws_vcol + total,
|
|
|
|
ts_val, curbuf->b_p_vts_array, &i, &j);
|
2018-06-23 19:23:02 +02:00
|
|
|
else
|
|
|
|
j = total;
|
|
|
|
#else
|
2004-06-13 20:20:40 +00:00
|
|
|
if (!curbuf->b_p_et)
|
2019-09-16 21:06:21 +02:00
|
|
|
i = ((ws_vcol % ts_val) + total) / ts_val; /* number of tabs */
|
2004-06-13 20:20:40 +00:00
|
|
|
if (i)
|
2019-09-16 21:06:21 +02:00
|
|
|
j = ((ws_vcol % ts_val) + total) % ts_val; /* number of spp */
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
|
|
|
j = total;
|
2018-06-23 19:23:02 +02:00
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
/* if we're splitting a TAB, allow for it */
|
|
|
|
bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
|
|
|
|
len = (int)STRLEN(bd.textstart) + 1;
|
2019-05-24 18:54:09 +02:00
|
|
|
newp = alloc(bd.textcol + i + j + len);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (newp == NULL)
|
|
|
|
return;
|
|
|
|
vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
|
|
|
|
mch_memmove(newp, oldp, (size_t)bd.textcol);
|
2015-07-17 13:22:51 +02:00
|
|
|
vim_memset(newp + bd.textcol, TAB, (size_t)i);
|
|
|
|
vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
|
2004-06-13 20:20:40 +00:00
|
|
|
/* the end */
|
|
|
|
mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
|
|
|
|
}
|
|
|
|
else /* left */
|
|
|
|
{
|
2009-03-11 15:28:26 +00:00
|
|
|
colnr_T destination_col; /* column to which text in block will
|
|
|
|
be shifted */
|
|
|
|
char_u *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 */
|
|
|
|
unsigned fill; /* nr of spaces that replace a TAB */
|
|
|
|
unsigned new_line_len; /* the length of the line after the
|
|
|
|
block shift */
|
|
|
|
size_t block_space_width;
|
|
|
|
size_t shift_amount;
|
|
|
|
char_u *non_white = bd.textstart;
|
|
|
|
colnr_T non_white_col;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2009-03-11 15:28:26 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2009-03-11 15:28:26 +00:00
|
|
|
/* 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
|
|
|
|
* searching from the next character. */
|
|
|
|
if (bd.startspaces)
|
2017-03-12 19:22:36 +01:00
|
|
|
MB_PTR_ADV(non_white);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2009-03-11 15:28:26 +00:00
|
|
|
/* The character's column is in "bd.start_vcol". */
|
|
|
|
non_white_col = bd.start_vcol;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2017-03-12 20:10:05 +01:00
|
|
|
while (VIM_ISWHITE(*non_white))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2014-06-25 14:39:50 +02:00
|
|
|
incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
|
2009-03-11 15:28:26 +00:00
|
|
|
non_white_col += incr;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2009-03-11 15:28:26 +00:00
|
|
|
|
|
|
|
block_space_width = non_white_col - oap->start_vcol;
|
|
|
|
/* We will shift by "total" or "block_space_width", whichever is less.
|
|
|
|
*/
|
2009-04-22 15:45:05 +00:00
|
|
|
shift_amount = (block_space_width < (size_t)total
|
|
|
|
? block_space_width : (size_t)total);
|
2009-03-11 15:28:26 +00:00
|
|
|
|
|
|
|
/* The column to which we will shift the text. */
|
2009-04-22 15:45:05 +00:00
|
|
|
destination_col = (colnr_T)(non_white_col - shift_amount);
|
2009-03-11 15:28:26 +00:00
|
|
|
|
|
|
|
/* Now let's find out how much of the beginning of the line we can
|
|
|
|
* reuse without modification. */
|
|
|
|
verbatim_copy_end = bd.textstart;
|
|
|
|
verbatim_copy_width = bd.start_vcol;
|
|
|
|
|
|
|
|
/* If "bd.startspaces" is set, "bd.textstart" points to the character
|
|
|
|
* preceding the block. We have to subtract its width to obtain its
|
|
|
|
* column number. */
|
|
|
|
if (bd.startspaces)
|
|
|
|
verbatim_copy_width -= bd.start_char_vcols;
|
|
|
|
while (verbatim_copy_width < destination_col)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2014-06-25 14:39:50 +02:00
|
|
|
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);
|
2009-03-11 15:28:26 +00:00
|
|
|
if (verbatim_copy_width + incr > destination_col)
|
|
|
|
break;
|
|
|
|
verbatim_copy_width += incr;
|
2017-03-12 19:22:36 +01:00
|
|
|
MB_PTR_ADV(verbatim_copy_end);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2009-03-11 15:28:26 +00:00
|
|
|
/* 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
|
|
|
|
* character, which we will have to partly replace with spaces. */
|
|
|
|
fill = destination_col - verbatim_copy_width;
|
|
|
|
|
|
|
|
/* The replacement line will consist of:
|
|
|
|
* - 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 = (unsigned)(verbatim_copy_end - oldp)
|
|
|
|
+ fill
|
|
|
|
+ (unsigned)STRLEN(non_white) + 1;
|
|
|
|
|
2019-05-24 18:54:09 +02:00
|
|
|
newp = alloc(new_line_len);
|
2009-03-11 15:28:26 +00:00
|
|
|
if (newp == NULL)
|
|
|
|
return;
|
|
|
|
mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
|
2015-07-17 13:22:51 +02:00
|
|
|
vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
|
2009-03-11 15:28:26 +00:00
|
|
|
STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
/* replace the line */
|
|
|
|
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
|
|
|
|
changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
|
|
|
|
State = oldstate;
|
|
|
|
curwin->w_cursor.col = oldcol;
|
|
|
|
#ifdef FEAT_RIGHTLEFT
|
|
|
|
p_ri = old_p_ri;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert string "s" (b_insert ? before : after) block :AKelly
|
|
|
|
* Caller must prepare for undo.
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-30 19:39:49 +01:00
|
|
|
block_insert(
|
|
|
|
oparg_T *oap,
|
|
|
|
char_u *s,
|
|
|
|
int b_insert,
|
|
|
|
struct block_def *bdp)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-16 21:06:21 +02:00
|
|
|
int ts_val;
|
2004-06-13 20:20:40 +00:00
|
|
|
int count = 0; /* extra spaces to replace a cut TAB */
|
|
|
|
int spaces = 0; /* non-zero if cutting a TAB */
|
|
|
|
colnr_T offset; /* pointer along new line */
|
|
|
|
unsigned s_len; /* STRLEN(s) */
|
|
|
|
char_u *newp, *oldp; /* new, old lines */
|
|
|
|
linenr_T lnum; /* loop var */
|
|
|
|
int oldstate = State;
|
|
|
|
|
|
|
|
State = INSERT; /* don't want REPLACE for State */
|
|
|
|
s_len = (unsigned)STRLEN(s);
|
|
|
|
|
|
|
|
for (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 */
|
|
|
|
|
|
|
|
oldp = ml_get(lnum);
|
|
|
|
|
|
|
|
if (b_insert)
|
|
|
|
{
|
2019-09-16 21:06:21 +02:00
|
|
|
ts_val = bdp->start_char_vcols;
|
2004-06-13 20:20:40 +00:00
|
|
|
spaces = bdp->startspaces;
|
|
|
|
if (spaces != 0)
|
2019-09-16 21:06:21 +02:00
|
|
|
count = ts_val - 1; /* we're cutting a TAB */
|
2004-06-13 20:20:40 +00:00
|
|
|
offset = bdp->textcol;
|
|
|
|
}
|
|
|
|
else /* append */
|
|
|
|
{
|
2019-09-16 21:06:21 +02:00
|
|
|
ts_val = bdp->end_char_vcols;
|
2004-06-13 20:20:40 +00:00
|
|
|
if (!bdp->is_short) /* spaces = padding after block */
|
|
|
|
{
|
2019-09-16 21:06:21 +02:00
|
|
|
spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (spaces != 0)
|
2019-09-16 21:06:21 +02:00
|
|
|
count = ts_val - 1; /* we're cutting a TAB */
|
2004-06-13 20:20:40 +00:00
|
|
|
offset = bdp->textcol + bdp->textlen - (spaces != 0);
|
|
|
|
}
|
|
|
|
else /* spaces = padding to block edge */
|
|
|
|
{
|
|
|
|
/* if $ used, just append to EOL (ie spaces==0) */
|
|
|
|
if (!bdp->is_MAX)
|
|
|
|
spaces = (oap->end_vcol - bdp->end_vcol) + 1;
|
|
|
|
count = spaces;
|
|
|
|
offset = bdp->textcol + bdp->textlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:36:43 +02:00
|
|
|
if (has_mbyte && spaces > 0)
|
|
|
|
{
|
2014-12-17 18:35:42 +01:00
|
|
|
int off;
|
|
|
|
|
2014-08-16 18:36:43 +02:00
|
|
|
/* Avoid starting halfway a multi-byte character. */
|
|
|
|
if (b_insert)
|
|
|
|
{
|
2014-12-17 18:35:42 +01:00
|
|
|
off = (*mb_head_off)(oldp, oldp + offset + spaces);
|
2014-08-16 18:36:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-17 18:35:42 +01:00
|
|
|
off = (*mb_off_next)(oldp, oldp + offset);
|
2014-08-16 18:36:43 +02:00
|
|
|
offset += off;
|
|
|
|
}
|
2014-12-17 18:35:42 +01:00
|
|
|
spaces -= off;
|
|
|
|
count -= off;
|
2014-08-16 18:36:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 18:54:09 +02:00
|
|
|
newp = alloc(STRLEN(oldp) + s_len + count + 1);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (newp == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* copy up to shifted part */
|
|
|
|
mch_memmove(newp, oldp, (size_t)(offset));
|
|
|
|
oldp += offset;
|
|
|
|
|
|
|
|
/* insert pre-padding */
|
2015-07-17 13:22:51 +02:00
|
|
|
vim_memset(newp + offset, ' ', (size_t)spaces);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/* copy the new text */
|
|
|
|
mch_memmove(newp + offset + spaces, s, (size_t)s_len);
|
|
|
|
offset += s_len;
|
|
|
|
|
|
|
|
if (spaces && !bdp->is_short)
|
|
|
|
{
|
|
|
|
/* insert post-padding */
|
2019-09-16 21:06:21 +02:00
|
|
|
vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
|
2004-06-13 20:20:40 +00:00
|
|
|
/* We're splitting a TAB, don't copy it. */
|
|
|
|
oldp++;
|
|
|
|
/* We allowed for that TAB, remember this now */
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spaces > 0)
|
|
|
|
offset += count;
|
2008-06-24 22:09:24 +00:00
|
|
|
STRMOVE(newp + offset, oldp);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
ml_replace(lnum, newp, FALSE);
|
|
|
|
|
|
|
|
if (lnum == oap->end.lnum)
|
|
|
|
{
|
|
|
|
/* Set "']" mark to the end of the block instead of the end of
|
|
|
|
* the insert in the first line. */
|
|
|
|
curbuf->b_op_end.lnum = oap->end.lnum;
|
|
|
|
curbuf->b_op_end.col = offset;
|
|
|
|
}
|
|
|
|
} /* for all lnum */
|
|
|
|
|
|
|
|
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
|
|
|
|
|
|
|
|
State = oldstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
|
|
|
|
/*
|
|
|
|
* op_reindent - handle reindenting a block of lines.
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 19:39:49 +01:00
|
|
|
op_reindent(oparg_T *oap, int (*how)(void))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
long i;
|
|
|
|
char_u *l;
|
2015-07-28 21:17:36 +02:00
|
|
|
int amount;
|
2004-06-13 20:20:40 +00:00
|
|
|
linenr_T first_changed = 0;
|
|
|
|
linenr_T last_changed = 0;
|
|
|
|
linenr_T start_lnum = curwin->w_cursor.lnum;
|
|
|
|
|
2005-03-18 20:25:31 +00:00
|
|
|
/* Don't even try when 'modifiable' is off. */
|
|
|
|
if (!curbuf->b_p_ma)
|
|
|
|
{
|
2019-01-13 23:38:42 +01:00
|
|
|
emsg(_(e_modifiable));
|
2005-03-18 20:25:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
for (i = oap->line_count; --i >= 0 && !got_int; )
|
|
|
|
{
|
|
|
|
/* it's a slow thing to do, so give feedback so there's no worry that
|
|
|
|
* the computer's just hung. */
|
|
|
|
|
|
|
|
if (i > 1
|
|
|
|
&& (i % 50 == 0 || i == oap->line_count - 1)
|
|
|
|
&& oap->line_count > p_report)
|
2019-01-13 23:38:42 +01:00
|
|
|
smsg(_("%ld lines to indent... "), i);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Be vi-compatible: For lisp indenting the first line is not
|
|
|
|
* indented, unless there is only one line.
|
|
|
|
*/
|
|
|
|
#ifdef FEAT_LISP
|
|
|
|
if (i != oap->line_count - 1 || oap->line_count == 1
|
|
|
|
|| how != get_lisp_indent)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
l = skipwhite(ml_get_curline());
|
|
|
|
if (*l == NUL) /* empty or blank line */
|
2015-07-28 21:17:36 +02:00
|
|
|
amount = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
2015-07-28 21:17:36 +02:00
|
|
|
amount = how(); /* get the indent for this line */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2015-07-28 21:17:36 +02:00
|
|
|
if (amount >= 0 && set_indent(amount, SIN_UNDO))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
/* did change the indent, call changed_lines() later */
|
|
|
|
if (first_changed == 0)
|
|
|
|
first_changed = curwin->w_cursor.lnum;
|
|
|
|
last_changed = curwin->w_cursor.lnum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++curwin->w_cursor.lnum;
|
2008-02-25 20:55:22 +00:00
|
|
|
curwin->w_cursor.col = 0; /* make sure it's valid */
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* put cursor on first non-blank of indented line */
|
|
|
|
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. */
|
|
|
|
if (last_changed != 0)
|
|
|
|
changed_lines(first_changed, 0,
|
|
|
|
oap->is_VIsual ? start_lnum + oap->line_count :
|
|
|
|
last_changed + 1, 0L);
|
|
|
|
else if (oap->is_VIsual)
|
|
|
|
redraw_curbuf_later(INVERTED);
|
|
|
|
|
|
|
|
if (oap->line_count > p_report)
|
|
|
|
{
|
|
|
|
i = oap->line_count - (i + 1);
|
2019-01-13 23:38:42 +01:00
|
|
|
smsg(NGETTEXT("%ld line indented ",
|
2018-08-21 15:12:14 +02:00
|
|
|
"%ld lines indented ", i), i);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
/* set '[ and '] marks */
|
|
|
|
curbuf->b_op_start = oap->start;
|
|
|
|
curbuf->b_op_end = oap->end;
|
|
|
|
}
|
|
|
|
#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Stuff a string into the typeahead buffer, such that edit() will insert it
|
|
|
|
* literally ("literally" TRUE) or interpret is as typed characters.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
void
|
2019-09-25 22:37:17 +02:00
|
|
|
stuffescaped(char_u *arg, int literally)
|
2005-05-22 22:09:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int c;
|
|
|
|
char_u *start;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
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. */
|
|
|
|
start = arg;
|
|
|
|
while ((*arg >= ' '
|
|
|
|
#ifndef EBCDIC
|
|
|
|
&& *arg < DEL /* EBCDIC: chars above space are normal */
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
2019-09-25 22:37:17 +02:00
|
|
|
)
|
|
|
|
|| (*arg == K_SPECIAL && !literally))
|
|
|
|
++arg;
|
|
|
|
if (arg > start)
|
|
|
|
stuffReadbuffLen(start, (long)(arg - start));
|
|
|
|
|
|
|
|
/* stuff a single special character */
|
|
|
|
if (*arg != NUL)
|
|
|
|
{
|
|
|
|
if (has_mbyte)
|
|
|
|
c = mb_cptr2char_adv(&arg);
|
|
|
|
else
|
|
|
|
c = *arg++;
|
|
|
|
if (literally && ((c < ' ' && c != TAB) || c == DEL))
|
|
|
|
stuffcharReadbuff(Ctrl_V);
|
|
|
|
stuffcharReadbuff(c);
|
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Handle a delete operation.
|
2018-03-06 19:51:13 +01:00
|
|
|
*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Return FAIL if undo failed, OK otherwise.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2018-03-06 19:51:13 +01:00
|
|
|
int
|
2019-09-25 22:37:17 +02:00
|
|
|
op_delete(oparg_T *oap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int n;
|
|
|
|
linenr_T lnum;
|
|
|
|
char_u *ptr;
|
|
|
|
char_u *newp, *oldp;
|
|
|
|
struct block_def bd;
|
|
|
|
linenr_T old_lcount = curbuf->b_ml.ml_line_count;
|
|
|
|
int did_yank = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
|
|
|
|
return OK;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Nothing to delete, return here. Do prepare undo, for op_change(). */
|
|
|
|
if (oap->empty)
|
|
|
|
return u_save_cursor();
|
|
|
|
|
|
|
|
if (!curbuf->b_p_ma)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
emsg(_(e_modifiable));
|
|
|
|
return FAIL;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
|
|
|
|
#ifdef FEAT_CLIPBOARD
|
|
|
|
adjust_clip_reg(&oap->regname);
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (has_mbyte)
|
|
|
|
mb_adjust_opend(oap);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Imitate the strange Vi behaviour: If the delete spans more than one
|
|
|
|
* line and motion_type == MCHAR 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 == MCHAR
|
|
|
|
&& !oap->is_VIsual
|
|
|
|
&& !oap->block_mode
|
|
|
|
&& oap->line_count > 1
|
|
|
|
&& oap->motion_force == NUL
|
|
|
|
&& oap->op_type == OP_DELETE)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
ptr = ml_get(oap->end.lnum) + oap->end.col;
|
|
|
|
if (*ptr != NUL)
|
|
|
|
ptr += oap->inclusive;
|
|
|
|
ptr = skipwhite(ptr);
|
|
|
|
if (*ptr == NUL && inindent(0))
|
|
|
|
oap->motion_type = MLINE;
|
2012-07-10 16:49:23 +02:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for trying to delete (e.g. "D") in an empty line.
|
|
|
|
* Note: For the change operator it is ok.
|
|
|
|
*/
|
|
|
|
if ( oap->motion_type == MCHAR
|
|
|
|
&& oap->line_count == 1
|
|
|
|
&& oap->op_type == OP_DELETE
|
|
|
|
&& *ml_get(oap->start.lnum) == NUL)
|
2012-07-10 16:49:23 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* It's an error to operate on an empty region, when 'E' included in
|
|
|
|
* 'cpoptions' (Vi compatible).
|
|
|
|
*/
|
|
|
|
if (virtual_op)
|
|
|
|
/* Virtual editing: Nothing gets deleted, but we set the '[ and ']
|
|
|
|
* marks as if it happened. */
|
|
|
|
goto setmarks;
|
|
|
|
if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
|
|
|
|
beep_flush();
|
|
|
|
return OK;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* 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 != '_')
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (oap->regname != 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* check for read-only register */
|
|
|
|
if (!valid_yank_reg(oap->regname, TRUE))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
beep_flush();
|
|
|
|
return OK;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
|
|
|
|
if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
|
|
|
|
did_yank = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
* Use the register name from before adjust_clip_reg() may have
|
|
|
|
* changed it.
|
|
|
|
*/
|
|
|
|
if (oap->motion_type == MLINE || oap->line_count > 1
|
|
|
|
|| oap->use_reg_one)
|
|
|
|
{
|
|
|
|
shift_delete_registers();
|
|
|
|
if (op_yank(oap, TRUE, FALSE) == OK)
|
|
|
|
did_yank = TRUE;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Yank into small delete register when no named register specified
|
|
|
|
* and the delete is within one line. */
|
|
|
|
if ((
|
2014-03-23 15:13:05 +01:00
|
|
|
#ifdef FEAT_CLIPBOARD
|
2019-09-25 22:37:17 +02:00
|
|
|
((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
|
|
|
|
((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
|
2014-03-23 15:13:05 +01:00
|
|
|
#endif
|
2019-09-25 22:37:17 +02:00
|
|
|
oap->regname == 0) && oap->motion_type != MLINE
|
|
|
|
&& oap->line_count == 1)
|
|
|
|
{
|
|
|
|
oap->regname = '-';
|
|
|
|
get_yank_register(oap->regname, TRUE);
|
|
|
|
if (op_yank(oap, TRUE, FALSE) == OK)
|
|
|
|
did_yank = TRUE;
|
|
|
|
oap->regname = 0;
|
|
|
|
}
|
2013-03-13 17:50:25 +01:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* If there's too much stuff to fit in the yank register, then get a
|
|
|
|
* confirmation before doing the delete. This is crude, but simple.
|
|
|
|
* And it avoids doing a delete of something we can't put back if we
|
|
|
|
* want.
|
|
|
|
*/
|
|
|
|
if (!did_yank)
|
|
|
|
{
|
|
|
|
int msg_silent_save = msg_silent;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
msg_silent = 0; /* must display the prompt */
|
|
|
|
n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
|
|
|
|
msg_silent = msg_silent_save;
|
|
|
|
if (n != 'y')
|
|
|
|
{
|
|
|
|
emsg(_(e_abort));
|
|
|
|
return FAIL;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#if defined(FEAT_EVAL)
|
|
|
|
if (did_yank && has_textyankpost())
|
|
|
|
yank_do_autocmd(oap, get_y_current());
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* block mode delete
|
|
|
|
*/
|
|
|
|
if (oap->block_mode)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (u_save((linenr_T)(oap->start.lnum - 1),
|
|
|
|
(linenr_T)(oap->end.lnum + 1)) == FAIL)
|
2004-06-13 20:20:40 +00:00
|
|
|
return FAIL;
|
2019-07-23 22:15:25 +02:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
|
|
|
|
{
|
|
|
|
block_prep(oap, &bd, lnum, TRUE);
|
|
|
|
if (bd.textlen == 0) /* nothing to delete */
|
|
|
|
continue;
|
2019-07-23 22:15:25 +02:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Adjust cursor position for tab replaced by spaces and 'lbr'. */
|
|
|
|
if (lnum == curwin->w_cursor.lnum)
|
|
|
|
{
|
|
|
|
curwin->w_cursor.col = bd.textcol + bd.startspaces;
|
|
|
|
curwin->w_cursor.coladd = 0;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
// "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 = alloc(STRLEN(oldp) + 1 - n);
|
|
|
|
if (newp == NULL)
|
|
|
|
continue;
|
|
|
|
/* copy up to deleted part */
|
|
|
|
mch_memmove(newp, oldp, (size_t)bd.textcol);
|
|
|
|
/* insert spaces */
|
|
|
|
vim_memset(newp + bd.textcol, ' ',
|
|
|
|
(size_t)(bd.startspaces + bd.endspaces));
|
|
|
|
/* copy the part after the deleted part */
|
|
|
|
oldp += bd.textcol + bd.textlen;
|
|
|
|
STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
|
|
|
|
/* replace the line */
|
|
|
|
ml_replace(lnum, newp, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#ifdef FEAT_TEXT_PROP
|
|
|
|
if (curbuf->b_has_textprop && n != 0)
|
|
|
|
adjust_prop_columns(lnum, bd.textcol, -n, 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
2019-09-25 22:37:17 +02:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
check_cursor_col();
|
|
|
|
changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
|
|
|
|
oap->end.lnum + 1, 0L);
|
|
|
|
oap->line_count = 0; /* no lines deleted */
|
|
|
|
}
|
|
|
|
else if (oap->motion_type == MLINE)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (oap->op_type == OP_CHANGE)
|
2005-08-12 19:59:19 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Delete the lines except the first one. Temporarily move the
|
|
|
|
* cursor to the next line. Save the current line number, if the
|
|
|
|
* last line is deleted it may be changed.
|
|
|
|
*/
|
|
|
|
if (oap->line_count > 1)
|
|
|
|
{
|
|
|
|
lnum = curwin->w_cursor.lnum;
|
|
|
|
++curwin->w_cursor.lnum;
|
|
|
|
del_lines((long)(oap->line_count - 1), TRUE);
|
|
|
|
curwin->w_cursor.lnum = lnum;
|
|
|
|
}
|
|
|
|
if (u_save_cursor() == FAIL)
|
|
|
|
return FAIL;
|
|
|
|
if (curbuf->b_p_ai) /* don't delete indent */
|
|
|
|
{
|
|
|
|
beginline(BL_WHITE); /* cursor on first non-white */
|
|
|
|
did_ai = TRUE; /* delete the indent when ESC hit */
|
|
|
|
ai_col = curwin->w_cursor.col;
|
|
|
|
}
|
2005-08-12 19:59:19 +00:00
|
|
|
else
|
2019-09-25 22:37:17 +02:00
|
|
|
beginline(0); /* cursor in column 0 */
|
|
|
|
truncate_line(FALSE); /* delete the rest of the line */
|
|
|
|
/* leave cursor past last char in line */
|
|
|
|
if (oap->line_count > 1)
|
|
|
|
u_clearline(); /* "U" command not possible after "2cc" */
|
2005-08-12 19:59:19 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
del_lines(oap->line_count, TRUE);
|
|
|
|
beginline(BL_WHITE | BL_FIX);
|
|
|
|
u_clearline(); /* "U" command not possible after "dd" */
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (virtual_op)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int endcol = 0;
|
|
|
|
|
|
|
|
/* For virtualedit: break the tabs that are partly included. */
|
|
|
|
if (gchar_pos(&oap->start) == '\t')
|
|
|
|
{
|
|
|
|
if (u_save_cursor() == FAIL) /* save first line for undo */
|
|
|
|
return FAIL;
|
|
|
|
if (oap->line_count == 1)
|
|
|
|
endcol = getviscol2(oap->end.col, oap->end.coladd);
|
|
|
|
coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
|
|
|
|
oap->start = curwin->w_cursor;
|
|
|
|
if (oap->line_count == 1)
|
|
|
|
{
|
|
|
|
coladvance(endcol);
|
|
|
|
oap->end.col = curwin->w_cursor.col;
|
|
|
|
oap->end.coladd = curwin->w_cursor.coladd;
|
|
|
|
curwin->w_cursor = oap->start;
|
|
|
|
}
|
|
|
|
}
|
2007-02-27 16:21:38 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Break a tab only when it's included in the area. */
|
|
|
|
if (gchar_pos(&oap->end) == '\t'
|
|
|
|
&& (int)oap->end.coladd < oap->inclusive)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* save last line for undo */
|
|
|
|
if (u_save((linenr_T)(oap->end.lnum - 1),
|
|
|
|
(linenr_T)(oap->end.lnum + 1)) == FAIL)
|
2004-06-13 20:20:40 +00:00
|
|
|
return FAIL;
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor = oap->end;
|
|
|
|
coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
|
|
|
|
oap->end = curwin->w_cursor;
|
|
|
|
curwin->w_cursor = oap->start;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (oap->line_count == 1) /* delete characters within one line */
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (u_save_cursor() == FAIL) /* save line for undo */
|
|
|
|
return FAIL;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* if 'cpoptions' contains '$', display '$' at end of change */
|
|
|
|
if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
|
|
|
|
&& oap->op_type == OP_CHANGE
|
|
|
|
&& oap->end.lnum == curwin->w_cursor.lnum
|
|
|
|
&& !oap->is_VIsual)
|
|
|
|
display_dollar(oap->end.col - !oap->inclusive);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
|
2007-02-27 16:21:38 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (virtual_op)
|
|
|
|
{
|
|
|
|
/* fix up things for virtualedit-delete:
|
|
|
|
* break the tabs which are going to get in our way
|
|
|
|
*/
|
|
|
|
char_u *curline = ml_get_curline();
|
|
|
|
int len = (int)STRLEN(curline);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (oap->end.coladd != 0
|
|
|
|
&& (int)oap->end.col >= len - 1
|
|
|
|
&& !(oap->start.coladd && (int)oap->end.col >= len - 1))
|
|
|
|
n++;
|
|
|
|
/* Delete at least one char (e.g, when on a control char). */
|
|
|
|
if (n == 0 && oap->start.coladd != oap->end.coladd)
|
|
|
|
n = 1;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* When deleted a char in the line, reset coladd. */
|
|
|
|
if (gchar_cursor() != NUL)
|
|
|
|
curwin->w_cursor.coladd = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
(void)del_bytes((long)n, !virtual_op,
|
|
|
|
oap->op_type == OP_DELETE && !oap->is_VIsual);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else /* delete characters between lines */
|
|
|
|
{
|
|
|
|
pos_T curpos;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* 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)
|
|
|
|
return FAIL;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
truncate_line(TRUE); /* delete from cursor to end of line */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
curpos = curwin->w_cursor; /* remember curwin->w_cursor */
|
|
|
|
++curwin->w_cursor.lnum;
|
|
|
|
del_lines((long)(oap->line_count - 2), FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* delete from start of line until op_end */
|
|
|
|
n = (oap->end.col + 1 - !oap->inclusive);
|
|
|
|
curwin->w_cursor.col = 0;
|
|
|
|
(void)del_bytes((long)n, !virtual_op,
|
|
|
|
oap->op_type == OP_DELETE && !oap->is_VIsual);
|
|
|
|
curwin->w_cursor = curpos; /* restore curwin->w_cursor */
|
|
|
|
(void)do_join(2, FALSE, FALSE, FALSE, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
msgmore(curbuf->b_ml.ml_line_count - old_lcount);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
setmarks:
|
|
|
|
if (oap->block_mode)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
curbuf->b_op_end.lnum = oap->end.lnum;
|
|
|
|
curbuf->b_op_end.col = oap->start.col;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
|
|
|
curbuf->b_op_end = oap->start;
|
|
|
|
curbuf->b_op_start = oap->start;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Adjust end of operating area for ending on a multi-byte character.
|
|
|
|
* Used for deletion.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
static void
|
|
|
|
mb_adjust_opend(oparg_T *oap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
if (oap->inclusive)
|
2014-08-06 18:17:11 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
p = ml_get(oap->end.lnum);
|
|
|
|
oap->end.col += mb_tail_off(p, p + oap->end.col);
|
2014-08-06 18:17:11 +02:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 21:34:31 +01:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Replace the character under the cursor with "c".
|
|
|
|
* This takes care of multi-byte characters.
|
2017-02-04 21:34:31 +01:00
|
|
|
*/
|
2017-12-16 18:27:02 +01:00
|
|
|
static void
|
2019-09-25 22:37:17 +02:00
|
|
|
replace_character(int c)
|
2017-12-16 18:27:02 +01:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int n = State;
|
2017-12-16 18:27:02 +01:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
State = REPLACE;
|
|
|
|
ins_char(c);
|
|
|
|
State = n;
|
|
|
|
/* Backup to the replaced character. */
|
|
|
|
dec_cursor();
|
2017-12-16 18:27:02 +01:00
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Replace a whole area with one character.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
int
|
2019-09-25 22:37:17 +02:00
|
|
|
op_replace(oparg_T *oap, int c)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int n, numc;
|
|
|
|
int num_chars;
|
2004-06-13 20:20:40 +00:00
|
|
|
char_u *newp, *oldp;
|
2019-09-25 22:37:17 +02:00
|
|
|
size_t oldlen;
|
2004-06-13 20:20:40 +00:00
|
|
|
struct block_def bd;
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *after_p = NULL;
|
|
|
|
int had_ctrl_v_cr = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
|
|
|
|
return OK; /* nothing to do */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (c == REPLACE_CR_NCHAR)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
had_ctrl_v_cr = TRUE;
|
|
|
|
c = CAR;
|
|
|
|
}
|
|
|
|
else if (c == REPLACE_NL_NCHAR)
|
|
|
|
{
|
|
|
|
had_ctrl_v_cr = TRUE;
|
|
|
|
c = NL;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_mbyte)
|
|
|
|
mb_adjust_opend(oap);
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (u_save((linenr_T)(oap->start.lnum - 1),
|
|
|
|
(linenr_T)(oap->end.lnum + 1)) == FAIL)
|
|
|
|
return FAIL;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2010-07-08 22:27:55 +02:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* block mode replace
|
2010-07-08 22:27:55 +02:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
if (oap->block_mode)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
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 */
|
|
|
|
block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
|
|
|
|
if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
|
|
|
|
continue; /* nothing to replace */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* n == number of extra chars required
|
|
|
|
* If we split a TAB, it may be replaced by several characters.
|
|
|
|
* Thus the number of characters may increase!
|
|
|
|
*/
|
|
|
|
/* If the range starts in virtual space, count the initial
|
|
|
|
* coladd offset as part of "startspaces" */
|
|
|
|
if (virtual_op && bd.is_short && *bd.textstart == NUL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
pos_T vpos;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
vpos.lnum = curwin->w_cursor.lnum;
|
|
|
|
getvpos(&vpos, oap->start_vcol);
|
|
|
|
bd.startspaces += vpos.coladd;
|
|
|
|
n = bd.startspaces;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
|
|
|
/* allow for pre spaces */
|
|
|
|
n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* allow for post spp */
|
|
|
|
n += (bd.endspaces
|
|
|
|
&& !bd.is_oneChar
|
|
|
|
&& bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
|
|
|
|
/* Figure out how many characters to replace. */
|
|
|
|
numc = oap->end_vcol - oap->start_vcol + 1;
|
|
|
|
if (bd.is_short && (!virtual_op || bd.is_MAX))
|
|
|
|
numc -= (oap->end_vcol - bd.end_vcol) + 1;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* A double-wide character can be replaced only up to half the
|
|
|
|
* times. */
|
|
|
|
if ((*mb_char2cells)(c) > 1)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if ((numc & 1) && !bd.is_short)
|
|
|
|
{
|
|
|
|
++bd.endspaces;
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
numc = numc / 2;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Compute bytes needed, move character count to num_chars. */
|
|
|
|
num_chars = numc;
|
|
|
|
numc *= (*mb_char2len)(c);
|
|
|
|
/* oldlen includes textlen, so don't double count */
|
|
|
|
n += numc - bd.textlen;
|
|
|
|
|
|
|
|
oldp = ml_get_curline();
|
|
|
|
oldlen = STRLEN(oldp);
|
|
|
|
newp = alloc(oldlen + 1 + n);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (newp == NULL)
|
|
|
|
continue;
|
2019-09-25 22:37:17 +02:00
|
|
|
vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
|
2004-06-13 20:20:40 +00:00
|
|
|
/* copy up to deleted part */
|
|
|
|
mch_memmove(newp, oldp, (size_t)bd.textcol);
|
|
|
|
oldp += bd.textcol + bd.textlen;
|
2019-09-25 22:37:17 +02:00
|
|
|
/* insert pre-spaces */
|
|
|
|
vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
|
|
|
|
/* insert replacement chars CHECK FOR ALLOCATED SPACE */
|
|
|
|
/* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
|
|
|
|
* literally. */
|
|
|
|
if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (has_mbyte)
|
|
|
|
{
|
|
|
|
n = (int)STRLEN(newp);
|
|
|
|
while (--num_chars >= 0)
|
|
|
|
n += (*mb_char2bytes)(c, newp + n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
vim_memset(newp + STRLEN(newp), c, (size_t)numc);
|
|
|
|
if (!bd.is_short)
|
|
|
|
{
|
|
|
|
/* insert post-spaces */
|
|
|
|
vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
|
|
|
|
/* copy the part after the changed part */
|
|
|
|
STRMOVE(newp + STRLEN(newp), oldp);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Replacing with \r or \n means splitting the line. */
|
|
|
|
after_p = alloc(oldlen + 1 + n - STRLEN(newp));
|
|
|
|
if (after_p != NULL)
|
|
|
|
STRMOVE(after_p, oldp);
|
|
|
|
}
|
|
|
|
/* replace the line */
|
|
|
|
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
|
|
|
|
if (after_p != NULL)
|
|
|
|
{
|
|
|
|
ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
|
|
|
|
appended_lines_mark(curwin->w_cursor.lnum, 1L);
|
|
|
|
oap->end.lnum++;
|
|
|
|
vim_free(after_p);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* MCHAR and MLINE motion replace.
|
|
|
|
*/
|
|
|
|
if (oap->motion_type == MLINE)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
oap->start.col = 0;
|
|
|
|
curwin->w_cursor.col = 0;
|
|
|
|
oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
|
|
|
|
if (oap->end.col)
|
|
|
|
--oap->end.col;
|
|
|
|
}
|
|
|
|
else if (!oap->inclusive)
|
|
|
|
dec(&(oap->end));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
while (LTOREQ_POS(curwin->w_cursor, oap->end))
|
|
|
|
{
|
|
|
|
n = gchar_cursor();
|
|
|
|
if (n != NUL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* This is slow, but it handles replacing a single-byte
|
|
|
|
* with a multi-byte and the other way around. */
|
|
|
|
if (curwin->w_cursor.lnum == oap->end.lnum)
|
|
|
|
oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
|
|
|
|
replace_character(c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (n == TAB)
|
|
|
|
{
|
|
|
|
int end_vcol = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (curwin->w_cursor.lnum == oap->end.lnum)
|
|
|
|
{
|
|
|
|
/* oap->end has to be recalculated when
|
|
|
|
* the tab breaks */
|
|
|
|
end_vcol = getviscol2(oap->end.col,
|
|
|
|
oap->end.coladd);
|
|
|
|
}
|
|
|
|
coladvance_force(getviscol());
|
|
|
|
if (curwin->w_cursor.lnum == oap->end.lnum)
|
|
|
|
getvpos(&oap->end, end_vcol);
|
|
|
|
}
|
|
|
|
PBYTE(curwin->w_cursor, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int virtcols = oap->end.coladd;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (curwin->w_cursor.lnum == oap->start.lnum
|
|
|
|
&& oap->start.col == oap->end.col && oap->start.coladd)
|
|
|
|
virtcols -= oap->start.coladd;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* oap->end has been trimmed so it's effectively inclusive;
|
|
|
|
* as a result an extra +1 must be counted so we don't
|
|
|
|
* trample the NUL byte. */
|
|
|
|
coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
|
|
|
|
curwin->w_cursor.col -= (virtcols + 1);
|
|
|
|
for (; virtcols >= 0; virtcols--)
|
|
|
|
{
|
|
|
|
if ((*mb_char2len)(c) > 1)
|
|
|
|
replace_character(c);
|
|
|
|
else
|
|
|
|
PBYTE(curwin->w_cursor, c);
|
|
|
|
if (inc(&curwin->w_cursor) == -1)
|
|
|
|
break;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Advance to next character, stop at the end of the file. */
|
|
|
|
if (inc_cursor() == -1)
|
|
|
|
break;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor = oap->start;
|
|
|
|
check_cursor();
|
|
|
|
changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Set "'[" and "']" marks. */
|
2004-06-13 20:20:40 +00:00
|
|
|
curbuf->b_op_start = oap->start;
|
2019-09-25 22:37:17 +02:00
|
|
|
curbuf->b_op_end = oap->end;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
static int swapchars(int op_type, pos_T *pos, int length);
|
2018-06-28 19:26:28 +02:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
void
|
|
|
|
op_tilde(oparg_T *oap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
pos_T pos;
|
2004-06-13 20:20:40 +00:00
|
|
|
struct block_def bd;
|
2019-09-25 22:37:17 +02:00
|
|
|
int did_change = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
if (u_save((linenr_T)(oap->start.lnum - 1),
|
|
|
|
(linenr_T)(oap->end.lnum + 1)) == FAIL)
|
2019-09-25 22:37:17 +02:00
|
|
|
return;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
pos = oap->start;
|
|
|
|
if (oap->block_mode) /* Visual block mode */
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int one_change;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
block_prep(oap, &bd, pos.lnum, FALSE);
|
|
|
|
pos.col = bd.textcol;
|
|
|
|
one_change = swapchars(oap->op_type, &pos, bd.textlen);
|
|
|
|
did_change |= one_change;
|
|
|
|
|
|
|
|
#ifdef FEAT_NETBEANS_INTG
|
|
|
|
if (netbeans_active() && one_change)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
netbeans_removed(curbuf, pos.lnum, bd.textcol,
|
|
|
|
(long)bd.textlen);
|
|
|
|
netbeans_inserted(curbuf, pos.lnum, bd.textcol,
|
|
|
|
&ptr[bd.textcol], bd.textlen);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (did_change)
|
|
|
|
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
|
|
|
|
}
|
|
|
|
else /* not block mode */
|
|
|
|
{
|
|
|
|
if (oap->motion_type == MLINE)
|
|
|
|
{
|
|
|
|
oap->start.col = 0;
|
|
|
|
pos.col = 0;
|
|
|
|
oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
|
|
|
|
if (oap->end.col)
|
|
|
|
--oap->end.col;
|
|
|
|
}
|
|
|
|
else if (!oap->inclusive)
|
|
|
|
dec(&(oap->end));
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2008-02-06 13:44:43 +00:00
|
|
|
if (pos.lnum == oap->end.lnum)
|
|
|
|
did_change = swapchars(oap->op_type, &pos,
|
|
|
|
oap->end.col - pos.col + 1);
|
|
|
|
else
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
did_change |= swapchars(oap->op_type, &pos,
|
|
|
|
pos.lnum == oap->end.lnum ? oap->end.col + 1:
|
|
|
|
(int)STRLEN(ml_get_pos(&pos)));
|
2017-03-12 18:23:53 +01:00
|
|
|
if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
|
2008-02-06 13:44:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
if (did_change)
|
|
|
|
{
|
|
|
|
changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
|
|
|
|
0L);
|
|
|
|
#ifdef FEAT_NETBEANS_INTG
|
2010-05-22 21:34:09 +02:00
|
|
|
if (netbeans_active() && did_change)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
char_u *ptr;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
pos = oap->start;
|
|
|
|
while (pos.lnum < oap->end.lnum)
|
|
|
|
{
|
|
|
|
ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
|
2006-04-17 22:14:47 +00:00
|
|
|
count = (int)STRLEN(ptr) - pos.col;
|
2004-10-24 19:18:58 +00:00
|
|
|
netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
|
2004-06-13 20:20:40 +00:00
|
|
|
netbeans_inserted(curbuf, pos.lnum, pos.col,
|
2010-05-22 21:34:09 +02:00
|
|
|
&ptr[pos.col], count);
|
2004-06-13 20:20:40 +00:00
|
|
|
pos.col = 0;
|
|
|
|
pos.lnum++;
|
|
|
|
}
|
|
|
|
ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
|
|
|
|
count = oap->end.col - pos.col + 1;
|
2004-10-24 19:18:58 +00:00
|
|
|
netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
|
2004-06-13 20:20:40 +00:00
|
|
|
netbeans_inserted(curbuf, pos.lnum, pos.col,
|
2010-05-22 21:34:09 +02:00
|
|
|
&ptr[pos.col], count);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!did_change && oap->is_VIsual)
|
|
|
|
/* No change: need to remove the Visual selection */
|
|
|
|
redraw_curbuf_later(INVERTED);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set '[ and '] marks.
|
|
|
|
*/
|
|
|
|
curbuf->b_op_start = oap->start;
|
|
|
|
curbuf->b_op_end = oap->end;
|
|
|
|
|
|
|
|
if (oap->line_count > p_report)
|
2019-01-13 23:38:42 +01:00
|
|
|
smsg(NGETTEXT("%ld line changed", "%ld lines changed",
|
2018-08-21 15:12:14 +02:00
|
|
|
oap->line_count), oap->line_count);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2008-01-22 15:02:31 +00:00
|
|
|
/*
|
|
|
|
* Invoke swapchar() on "length" bytes at position "pos".
|
|
|
|
* "pos" is advanced to just after the changed characters.
|
|
|
|
* "length" is rounded up to include the whole last multi-byte character.
|
|
|
|
* Also works correctly when the number of bytes changes.
|
|
|
|
* Returns TRUE if some character was changed.
|
|
|
|
*/
|
|
|
|
static int
|
2016-01-30 19:39:49 +01:00
|
|
|
swapchars(int op_type, pos_T *pos, int length)
|
2008-01-22 15:02:31 +00:00
|
|
|
{
|
|
|
|
int todo;
|
|
|
|
int did_change = 0;
|
|
|
|
|
|
|
|
for (todo = length; todo > 0; --todo)
|
|
|
|
{
|
|
|
|
if (has_mbyte)
|
2013-08-09 19:48:40 +02:00
|
|
|
{
|
|
|
|
int len = (*mb_ptr2len)(ml_get_pos(pos));
|
|
|
|
|
2008-01-22 15:02:31 +00:00
|
|
|
/* we're counting bytes, not characters */
|
2013-08-09 19:48:40 +02:00
|
|
|
if (len > 0)
|
|
|
|
todo -= len - 1;
|
|
|
|
}
|
2008-01-22 15:02:31 +00:00
|
|
|
did_change |= swapchar(op_type, pos);
|
|
|
|
if (inc(pos) == -1) /* at end of file */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return did_change;
|
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* If op_type == OP_UPPER: make uppercase,
|
|
|
|
* if op_type == OP_LOWER: make lowercase,
|
|
|
|
* if op_type == OP_ROT13: do rot13 encoding,
|
|
|
|
* else swap case of character at 'pos'
|
|
|
|
* returns TRUE when something actually changed.
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 19:39:49 +01:00
|
|
|
swapchar(int op_type, pos_T *pos)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int nc;
|
|
|
|
|
|
|
|
c = gchar_pos(pos);
|
|
|
|
|
|
|
|
/* Only do rot13 encoding for ASCII characters. */
|
|
|
|
if (c >= 0x80 && op_type == OP_ROT13)
|
|
|
|
return FALSE;
|
|
|
|
|
2008-01-22 15:02:31 +00:00
|
|
|
if (op_type == OP_UPPER && c == 0xdf
|
|
|
|
&& (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
|
2005-08-23 21:02:42 +00:00
|
|
|
{
|
|
|
|
pos_T sp = curwin->w_cursor;
|
|
|
|
|
|
|
|
/* Special handling of German sharp s: change to "SS". */
|
|
|
|
curwin->w_cursor = *pos;
|
|
|
|
del_char(FALSE);
|
|
|
|
ins_char('S');
|
|
|
|
ins_char('S');
|
|
|
|
curwin->w_cursor = sp;
|
|
|
|
inc(pos);
|
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
|
|
|
|
return FALSE;
|
|
|
|
nc = c;
|
|
|
|
if (MB_ISLOWER(c))
|
|
|
|
{
|
|
|
|
if (op_type == OP_ROT13)
|
|
|
|
nc = ROT13(c, 'a');
|
|
|
|
else if (op_type != OP_LOWER)
|
|
|
|
nc = MB_TOUPPER(c);
|
|
|
|
}
|
|
|
|
else if (MB_ISUPPER(c))
|
|
|
|
{
|
|
|
|
if (op_type == OP_ROT13)
|
|
|
|
nc = ROT13(c, 'A');
|
|
|
|
else if (op_type != OP_UPPER)
|
|
|
|
nc = MB_TOLOWER(c);
|
|
|
|
}
|
|
|
|
if (nc != c)
|
|
|
|
{
|
|
|
|
if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
|
|
|
|
{
|
|
|
|
pos_T sp = curwin->w_cursor;
|
|
|
|
|
|
|
|
curwin->w_cursor = *pos;
|
2010-08-01 14:22:48 +02:00
|
|
|
/* don't use del_char(), it also removes composing chars */
|
|
|
|
del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
ins_char(nc);
|
|
|
|
curwin->w_cursor = sp;
|
|
|
|
}
|
|
|
|
else
|
2018-06-28 19:26:28 +02:00
|
|
|
PBYTE(*pos, nc);
|
2004-06-13 20:20:40 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* op_insert - Insert and append operators for Visual mode.
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 19:39:49 +01:00
|
|
|
op_insert(oparg_T *oap, long count1)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
long ins_len, pre_textlen = 0;
|
|
|
|
char_u *firstline, *ins_text;
|
2017-09-03 14:03:43 +02:00
|
|
|
colnr_T ind_pre = 0, ind_post;
|
2004-06-13 20:20:40 +00:00
|
|
|
struct block_def bd;
|
|
|
|
int i;
|
2015-02-03 18:36:44 +01:00
|
|
|
pos_T t1;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/* edit() changes this - record it for OP_APPEND */
|
|
|
|
bd.is_MAX = (curwin->w_curswant == MAXCOL);
|
|
|
|
|
|
|
|
/* vis block is still marked. Get rid of it now. */
|
|
|
|
curwin->w_cursor.lnum = oap->start.lnum;
|
|
|
|
update_screen(INVERTED);
|
|
|
|
|
|
|
|
if (oap->block_mode)
|
|
|
|
{
|
|
|
|
/* When 'virtualedit' is used, need to insert the extra spaces before
|
|
|
|
* doing block_prep(). When only "block" is used, virtual edit is
|
|
|
|
* already disabled, but still need it when calling
|
|
|
|
* coladvance_force(). */
|
|
|
|
if (curwin->w_cursor.coladd > 0)
|
|
|
|
{
|
|
|
|
int old_ve_flags = ve_flags;
|
|
|
|
|
|
|
|
ve_flags = VE_ALL;
|
|
|
|
if (u_save_cursor() == FAIL)
|
|
|
|
return;
|
|
|
|
coladvance_force(oap->op_type == OP_APPEND
|
|
|
|
? oap->end_vcol + 1 : getviscol());
|
|
|
|
if (oap->op_type == OP_APPEND)
|
|
|
|
--curwin->w_cursor.col;
|
|
|
|
ve_flags = old_ve_flags;
|
|
|
|
}
|
|
|
|
/* Get the info about the block before entering the text */
|
|
|
|
block_prep(oap, &bd, oap->start.lnum, TRUE);
|
2017-09-02 20:30:35 +02:00
|
|
|
/* Get indent information */
|
|
|
|
ind_pre = (colnr_T)getwhitecols_curline();
|
2004-06-13 20:20:40 +00:00
|
|
|
firstline = ml_get(oap->start.lnum) + bd.textcol;
|
2017-09-02 20:30:35 +02:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if (oap->op_type == OP_APPEND)
|
|
|
|
firstline += bd.textlen;
|
|
|
|
pre_textlen = (long)STRLEN(firstline);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oap->op_type == OP_APPEND)
|
|
|
|
{
|
2019-01-26 17:28:26 +01:00
|
|
|
if (oap->block_mode && curwin->w_cursor.coladd == 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
/* Move the cursor to the character right of the block. */
|
|
|
|
curwin->w_set_curswant = TRUE;
|
|
|
|
while (*ml_get_cursor() != NUL
|
|
|
|
&& (curwin->w_cursor.col < bd.textcol + bd.textlen))
|
|
|
|
++curwin->w_cursor.col;
|
|
|
|
if (bd.is_short && !bd.is_MAX)
|
|
|
|
{
|
|
|
|
/* First line was too short, make it longer and adjust the
|
|
|
|
* values in "bd". */
|
|
|
|
if (u_save_cursor() == FAIL)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < bd.endspaces; ++i)
|
|
|
|
ins_char(' ');
|
|
|
|
bd.textlen += bd.endspaces;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curwin->w_cursor = oap->end;
|
2006-06-20 18:29:34 +00:00
|
|
|
check_cursor_col();
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/* Works just like an 'i'nsert on the next character. */
|
2017-03-12 18:23:53 +01:00
|
|
|
if (!LINEEMPTY(curwin->w_cursor.lnum)
|
2004-06-13 20:20:40 +00:00
|
|
|
&& oap->start_vcol != oap->end_vcol)
|
|
|
|
inc_cursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:36:44 +01:00
|
|
|
t1 = oap->start;
|
2017-02-01 21:50:21 +01:00
|
|
|
(void)edit(NUL, FALSE, (linenr_T)count1);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2015-02-03 18:36:44 +01:00
|
|
|
/* When a tab was inserted, and the characters in front of the tab
|
|
|
|
* have been converted to a tab as well, the column of the cursor
|
|
|
|
* might have actually been reduced, so need to adjust here. */
|
|
|
|
if (t1.lnum == curbuf->b_op_start_orig.lnum
|
2017-03-12 18:23:53 +01:00
|
|
|
&& LT_POS(curbuf->b_op_start_orig, t1))
|
2015-02-03 18:36:44 +01:00
|
|
|
oap->start = curbuf->b_op_start_orig;
|
|
|
|
|
2008-01-03 15:34:40 +00:00
|
|
|
/* If user has moved off this line, we don't know what to do, so do
|
|
|
|
* nothing.
|
|
|
|
* Also don't repeat the insert when Insert mode ended with CTRL-C. */
|
|
|
|
if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
|
2004-06-13 20:20:40 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (oap->block_mode)
|
|
|
|
{
|
|
|
|
struct block_def bd2;
|
2018-04-10 13:15:47 +02:00
|
|
|
int did_indent = FALSE;
|
2018-04-30 17:21:03 +02:00
|
|
|
size_t len;
|
|
|
|
int add;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2017-09-02 23:28:54 +02:00
|
|
|
/* If indent kicked in, the firstline might have changed
|
|
|
|
* but only do that, if the indent actually increased. */
|
|
|
|
ind_post = (colnr_T)getwhitecols_curline();
|
|
|
|
if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
|
|
|
|
{
|
|
|
|
bd.textcol += ind_post - ind_pre;
|
|
|
|
bd.start_vcol += ind_post - ind_pre;
|
2018-04-10 13:15:47 +02:00
|
|
|
did_indent = TRUE;
|
2017-09-02 23:28:54 +02:00
|
|
|
}
|
|
|
|
|
2013-11-11 01:29:22 +01:00
|
|
|
/* The user may have moved the cursor before inserting something, try
|
2018-04-10 13:15:47 +02:00
|
|
|
* to adjust the block for that. But only do it, if the difference
|
|
|
|
* does not come from indent kicking in. */
|
|
|
|
if (oap->start.lnum == curbuf->b_op_start_orig.lnum
|
|
|
|
&& !bd.is_MAX && !did_indent)
|
2013-11-11 01:29:22 +01:00
|
|
|
{
|
|
|
|
if (oap->op_type == OP_INSERT
|
2019-01-26 17:28:26 +01:00
|
|
|
&& oap->start.col + oap->start.coladd
|
2014-03-19 18:57:54 +01:00
|
|
|
!= curbuf->b_op_start_orig.col
|
2019-01-26 17:28:26 +01:00
|
|
|
+ curbuf->b_op_start_orig.coladd)
|
2013-11-11 01:29:22 +01:00
|
|
|
{
|
2015-02-03 18:36:44 +01:00
|
|
|
int t = getviscol2(curbuf->b_op_start_orig.col,
|
2019-01-26 17:28:26 +01:00
|
|
|
curbuf->b_op_start_orig.coladd);
|
2014-02-22 23:03:55 +01:00
|
|
|
oap->start.col = curbuf->b_op_start_orig.col;
|
2015-02-03 18:36:44 +01:00
|
|
|
pre_textlen -= t - oap->start_vcol;
|
|
|
|
oap->start_vcol = t;
|
2013-11-11 01:29:22 +01:00
|
|
|
}
|
|
|
|
else if (oap->op_type == OP_APPEND
|
2019-01-26 17:28:26 +01:00
|
|
|
&& oap->end.col + oap->end.coladd
|
2014-03-19 18:57:54 +01:00
|
|
|
>= curbuf->b_op_start_orig.col
|
2019-01-26 17:28:26 +01:00
|
|
|
+ curbuf->b_op_start_orig.coladd)
|
2013-11-11 01:29:22 +01:00
|
|
|
{
|
2015-02-03 18:36:44 +01:00
|
|
|
int t = getviscol2(curbuf->b_op_start_orig.col,
|
2019-01-26 17:28:26 +01:00
|
|
|
curbuf->b_op_start_orig.coladd);
|
2014-02-22 23:03:55 +01:00
|
|
|
oap->start.col = curbuf->b_op_start_orig.col;
|
2013-11-11 01:29:22 +01:00
|
|
|
/* reset pre_textlen to the value of OP_INSERT */
|
|
|
|
pre_textlen += bd.textlen;
|
2015-02-03 18:36:44 +01:00
|
|
|
pre_textlen -= t - oap->start_vcol;
|
|
|
|
oap->start_vcol = t;
|
2013-11-11 01:29:22 +01:00
|
|
|
oap->op_type = OP_INSERT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* Spaces and tabs in the indent may have changed to other spaces and
|
2007-09-13 20:41:32 +00:00
|
|
|
* tabs. Get the starting column again and correct the length.
|
2004-06-13 20:20:40 +00:00
|
|
|
* Don't do this when "$" used, end-of-line will have changed.
|
|
|
|
*/
|
|
|
|
block_prep(oap, &bd2, oap->start.lnum, TRUE);
|
|
|
|
if (!bd.is_MAX || bd2.textlen < bd.textlen)
|
|
|
|
{
|
|
|
|
if (oap->op_type == OP_APPEND)
|
|
|
|
{
|
|
|
|
pre_textlen += bd2.textlen - bd.textlen;
|
|
|
|
if (bd2.endspaces)
|
|
|
|
--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.
|
|
|
|
*/
|
2018-04-30 17:21:03 +02:00
|
|
|
firstline = ml_get(oap->start.lnum);
|
|
|
|
len = STRLEN(firstline);
|
|
|
|
add = bd.textcol;
|
2004-06-13 20:20:40 +00:00
|
|
|
if (oap->op_type == OP_APPEND)
|
2018-04-30 17:21:03 +02:00
|
|
|
add += bd.textlen;
|
|
|
|
if ((size_t)add > len)
|
|
|
|
firstline += len; // short line, point to the NUL
|
|
|
|
else
|
|
|
|
firstline += add;
|
2012-03-23 14:16:23 +01:00
|
|
|
if (pre_textlen >= 0
|
|
|
|
&& (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
ins_text = vim_strnsave(firstline, (int)ins_len);
|
|
|
|
if (ins_text != NULL)
|
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
curwin->w_cursor.col = oap->start.col;
|
|
|
|
check_cursor();
|
|
|
|
vim_free(ins_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* op_change - handle a change operation
|
|
|
|
*
|
|
|
|
* return TRUE if edit() returns because of a CTRL-O command
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 19:39:49 +01:00
|
|
|
op_change(oparg_T *oap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
colnr_T l;
|
|
|
|
int retval;
|
|
|
|
long offset;
|
|
|
|
linenr_T linenr;
|
2007-09-13 20:41:32 +00:00
|
|
|
long ins_len;
|
|
|
|
long pre_textlen = 0;
|
|
|
|
long pre_indent = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
char_u *firstline;
|
|
|
|
char_u *ins_text, *newp, *oldp;
|
|
|
|
struct block_def bd;
|
|
|
|
|
|
|
|
l = oap->start.col;
|
|
|
|
if (oap->motion_type == MLINE)
|
|
|
|
{
|
|
|
|
l = 0;
|
|
|
|
#ifdef FEAT_SMARTINDENT
|
|
|
|
if (!p_paste && curbuf->b_p_si
|
|
|
|
# ifdef FEAT_CINDENT
|
|
|
|
&& !curbuf->b_p_cin
|
|
|
|
# endif
|
|
|
|
)
|
|
|
|
can_si = TRUE; /* It's like opening a new line, do si */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First delete the text in the region. In an empty buffer only need to
|
|
|
|
* save for undo */
|
|
|
|
if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
|
|
|
{
|
|
|
|
if (u_save_cursor() == FAIL)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (op_delete(oap) == FAIL)
|
|
|
|
return FALSE;
|
|
|
|
|
2017-03-12 18:23:53 +01:00
|
|
|
if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
|
2004-06-13 20:20:40 +00:00
|
|
|
&& !virtual_op)
|
|
|
|
inc_cursor();
|
|
|
|
|
|
|
|
/* check for still on same line (<CR> in inserted text meaningless) */
|
|
|
|
/* skip blank lines too */
|
|
|
|
if (oap->block_mode)
|
|
|
|
{
|
|
|
|
/* Add spaces before getting the current line length. */
|
|
|
|
if (virtual_op && (curwin->w_cursor.coladd > 0
|
|
|
|
|| gchar_cursor() == NUL))
|
|
|
|
coladvance_force(getviscol());
|
2007-09-13 20:41:32 +00:00
|
|
|
firstline = ml_get(oap->start.lnum);
|
|
|
|
pre_textlen = (long)STRLEN(firstline);
|
2017-09-02 20:30:35 +02:00
|
|
|
pre_indent = (long)getwhitecols(firstline);
|
2004-06-13 20:20:40 +00:00
|
|
|
bd.textcol = curwin->w_cursor.col;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
|
|
|
|
if (oap->motion_type == MLINE)
|
|
|
|
fix_indent();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retval = edit(NUL, FALSE, (linenr_T)1);
|
|
|
|
|
|
|
|
/*
|
2004-12-19 22:46:22 +00:00
|
|
|
* In Visual block mode, handle copying the new text to all lines of the
|
2004-06-13 20:20:40 +00:00
|
|
|
* block.
|
2008-01-03 15:34:40 +00:00
|
|
|
* Don't repeat the insert when Insert mode ended with CTRL-C.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2008-01-03 15:34:40 +00:00
|
|
|
if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2007-09-13 20:41:32 +00:00
|
|
|
/* Auto-indenting may have changed the indent. If the cursor was past
|
|
|
|
* the indent, exclude that indent change from the inserted text. */
|
2004-06-13 20:20:40 +00:00
|
|
|
firstline = ml_get(oap->start.lnum);
|
2007-09-25 12:20:19 +00:00
|
|
|
if (bd.textcol > (colnr_T)pre_indent)
|
2007-09-13 20:41:32 +00:00
|
|
|
{
|
2017-09-02 20:30:35 +02:00
|
|
|
long new_indent = (long)getwhitecols(firstline);
|
2007-09-13 20:41:32 +00:00
|
|
|
|
|
|
|
pre_textlen += new_indent - pre_indent;
|
|
|
|
bd.textcol += new_indent - pre_indent;
|
|
|
|
}
|
|
|
|
|
|
|
|
ins_len = (long)STRLEN(firstline) - pre_textlen;
|
|
|
|
if (ins_len > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2007-09-13 20:41:32 +00:00
|
|
|
/* Subsequent calls to ml_get() flush the firstline data - take a
|
|
|
|
* copy of the inserted text. */
|
2019-05-24 18:54:09 +02:00
|
|
|
if ((ins_text = alloc(ins_len + 1)) != NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2005-07-18 21:58:11 +00:00
|
|
|
vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
|
2004-06-13 20:20:40 +00:00
|
|
|
for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
|
|
|
|
linenr++)
|
|
|
|
{
|
|
|
|
block_prep(oap, &bd, linenr, TRUE);
|
|
|
|
if (!bd.is_short || virtual_op)
|
|
|
|
{
|
|
|
|
pos_T vpos;
|
|
|
|
|
|
|
|
/* If the block starts in virtual space, count the
|
|
|
|
* initial coladd offset as part of "startspaces" */
|
|
|
|
if (bd.is_short)
|
|
|
|
{
|
2009-11-03 15:44:21 +00:00
|
|
|
vpos.lnum = linenr;
|
2004-06-13 20:20:40 +00:00
|
|
|
(void)getvpos(&vpos, oap->start_vcol);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
vpos.coladd = 0;
|
|
|
|
oldp = ml_get(linenr);
|
2019-05-24 18:54:09 +02:00
|
|
|
newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (newp == NULL)
|
|
|
|
continue;
|
|
|
|
/* copy up to block start */
|
|
|
|
mch_memmove(newp, oldp, (size_t)bd.textcol);
|
|
|
|
offset = bd.textcol;
|
2015-07-17 13:22:51 +02:00
|
|
|
vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
|
2004-06-13 20:20:40 +00:00
|
|
|
offset += vpos.coladd;
|
|
|
|
mch_memmove(newp + offset, ins_text, (size_t)ins_len);
|
|
|
|
offset += ins_len;
|
|
|
|
oldp += bd.textcol;
|
2008-06-24 22:09:24 +00:00
|
|
|
STRMOVE(newp + offset, oldp);
|
2004-06-13 20:20:40 +00:00
|
|
|
ml_replace(linenr, newp, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check_cursor();
|
|
|
|
|
|
|
|
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
|
|
|
|
}
|
|
|
|
vim_free(ins_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* When the cursor is on the NUL past the end of the line and it should not be
|
|
|
|
* there move it left.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
void
|
2019-09-25 22:37:17 +02:00
|
|
|
adjust_cursor_eol(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (curwin->w_cursor.col > 0
|
|
|
|
&& gchar_cursor() == NUL
|
|
|
|
&& (ve_flags & VE_ONEMORE) == 0
|
|
|
|
&& !(restart_edit || (State & INSERT)))
|
|
|
|
{
|
|
|
|
/* Put the cursor on the last character in the line. */
|
|
|
|
dec_cursor();
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (ve_flags == VE_ALL)
|
|
|
|
{
|
|
|
|
colnr_T scol, ecol;
|
2005-06-24 23:07:47 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Coladd is set to the width of the last character. */
|
|
|
|
getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
|
|
|
|
curwin->w_cursor.coladd = ecol - scol + 1;
|
|
|
|
}
|
2005-06-24 23:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Return TRUE if lines starting with '#' should be left aligned.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
int
|
|
|
|
preprocs_left(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
return
|
|
|
|
# ifdef FEAT_SMARTINDENT
|
|
|
|
# ifdef FEAT_CINDENT
|
|
|
|
(curbuf->b_p_si && !curbuf->b_p_cin) ||
|
|
|
|
# else
|
|
|
|
curbuf->b_p_si
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
# ifdef FEAT_CINDENT
|
|
|
|
(curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
|
|
|
|
&& curbuf->b_ind_hash_comment == 0)
|
|
|
|
# endif
|
|
|
|
;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* If "process" is TRUE and the line begins with a comment leader (possibly
|
|
|
|
* after some white space), return a pointer to the text after it. Put a boolean
|
|
|
|
* value indicating whether the line ends with an unclosed comment in
|
|
|
|
* "is_comment".
|
|
|
|
* line - line to be processed,
|
|
|
|
* process - if FALSE, will only check whether the line ends with an unclosed
|
|
|
|
* comment,
|
|
|
|
* include_space - whether to also skip space following the comment leader,
|
|
|
|
* is_comment - will indicate whether the current line ends with an unclosed
|
|
|
|
* comment.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *
|
|
|
|
skip_comment(
|
|
|
|
char_u *line,
|
|
|
|
int process,
|
|
|
|
int include_space,
|
|
|
|
int *is_comment)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *comment_flags = NULL;
|
|
|
|
int lead_len;
|
|
|
|
int leader_offset = get_last_leader_offset(line, &comment_flags);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
*is_comment = FALSE;
|
|
|
|
if (leader_offset != -1)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Let's check whether the line ends with an unclosed comment.
|
|
|
|
* If the last comment leader has COM_END in flags, there's no comment.
|
|
|
|
*/
|
|
|
|
while (*comment_flags)
|
|
|
|
{
|
|
|
|
if (*comment_flags == COM_END
|
|
|
|
|| *comment_flags == ':')
|
|
|
|
break;
|
|
|
|
++comment_flags;
|
|
|
|
}
|
|
|
|
if (*comment_flags != COM_END)
|
|
|
|
*is_comment = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (process == FALSE)
|
|
|
|
return line;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (lead_len == 0)
|
|
|
|
return line;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Find:
|
|
|
|
* - COM_END,
|
|
|
|
* - colon,
|
|
|
|
* whichever comes first.
|
2005-12-12 22:02:31 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
while (*comment_flags)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (*comment_flags == COM_END
|
|
|
|
|| *comment_flags == ':')
|
|
|
|
break;
|
|
|
|
++comment_flags;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* If we found a colon, it means that we are not processing a line
|
|
|
|
* starting with a closing part of a three-part comment. That's good,
|
|
|
|
* because we don't want to remove those as this would be annoying.
|
|
|
|
*/
|
|
|
|
if (*comment_flags == ':' || *comment_flags == NUL)
|
|
|
|
line += lead_len;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
return line;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Join 'count' lines (minimal 2) at cursor position.
|
|
|
|
* When "save_undo" is TRUE save lines for undo first.
|
|
|
|
* Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
|
|
|
|
* leaders should not be removed.
|
|
|
|
* When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
|
|
|
|
* to set those marks.
|
|
|
|
*
|
|
|
|
* return FAIL for failure, OK otherwise
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
do_join(
|
|
|
|
long count,
|
|
|
|
int insert_space,
|
|
|
|
int save_undo,
|
|
|
|
int use_formatoptions UNUSED,
|
|
|
|
int setmark)
|
|
|
|
{
|
|
|
|
char_u *curr = NULL;
|
|
|
|
char_u *curr_start = NULL;
|
|
|
|
char_u *cend;
|
|
|
|
char_u *newp;
|
|
|
|
char_u *spaces; /* number of spaces inserted before a line */
|
|
|
|
int endcurr1 = NUL;
|
|
|
|
int endcurr2 = NUL;
|
|
|
|
int currsize = 0; /* size of the current line */
|
|
|
|
int sumsize = 0; /* size of the long new line */
|
|
|
|
linenr_T t;
|
|
|
|
colnr_T col = 0;
|
|
|
|
int ret = OK;
|
|
|
|
int *comments = NULL;
|
|
|
|
int remove_comments = (use_formatoptions == TRUE)
|
|
|
|
&& has_format_option(FO_REMOVE_COMS);
|
|
|
|
int prev_was_comment;
|
|
|
|
#ifdef FEAT_TEXT_PROP
|
|
|
|
textprop_T **prop_lines = NULL;
|
|
|
|
int *prop_lengths = NULL;
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
|
|
|
|
(linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
|
|
|
|
return FAIL;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Allocate an array to store the number of spaces inserted before each
|
|
|
|
* line. We will use it to pre-compute the length of the new line and the
|
|
|
|
* proper placement of each original line in the new one. */
|
|
|
|
spaces = lalloc_clear(count, TRUE);
|
|
|
|
if (spaces == NULL)
|
|
|
|
return FAIL;
|
|
|
|
if (remove_comments)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
comments = lalloc_clear(count * sizeof(int), TRUE);
|
|
|
|
if (comments == NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
vim_free(spaces);
|
|
|
|
return FAIL;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Don't move anything yet, just compute the final line length
|
|
|
|
* and setup the array of space strings lengths
|
|
|
|
* This loops forward over the joined lines.
|
|
|
|
*/
|
|
|
|
for (t = 0; t < count; ++t)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
|
|
|
|
if (t == 0 && setmark)
|
|
|
|
{
|
|
|
|
/* Set the '[ mark. */
|
|
|
|
curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
|
|
|
|
curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
|
|
|
|
}
|
|
|
|
if (remove_comments)
|
|
|
|
{
|
|
|
|
/* We don't want to remove the comment leader if the
|
|
|
|
* previous line is not a comment. */
|
|
|
|
if (t > 0 && prev_was_comment)
|
|
|
|
{
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *new_curr = skip_comment(curr, TRUE, insert_space,
|
|
|
|
&prev_was_comment);
|
|
|
|
comments[t] = (int)(new_curr - curr);
|
|
|
|
curr = new_curr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
curr = skip_comment(curr, FALSE, insert_space,
|
|
|
|
&prev_was_comment);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (insert_space && t > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
curr = skipwhite(curr);
|
|
|
|
if (*curr != ')' && currsize != 0 && endcurr1 != TAB
|
|
|
|
&& (!has_format_option(FO_MBYTE_JOIN)
|
|
|
|
|| (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
|
|
|
|
&& (!has_format_option(FO_MBYTE_JOIN2)
|
|
|
|
|| mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
|
|
|
|
)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* don't add a space if the line is ending in a space */
|
|
|
|
if (endcurr1 == ' ')
|
|
|
|
endcurr1 = endcurr2;
|
|
|
|
else
|
|
|
|
++spaces[t];
|
|
|
|
/* extra space when 'joinspaces' set and line ends in '.' */
|
|
|
|
if ( p_js
|
|
|
|
&& (endcurr1 == '.'
|
|
|
|
|| (vim_strchr(p_cpo, CPO_JOINSP) == NULL
|
|
|
|
&& (endcurr1 == '?' || endcurr1 == '!'))))
|
|
|
|
++spaces[t];
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
currsize = (int)STRLEN(curr);
|
|
|
|
sumsize += currsize + spaces[t];
|
|
|
|
endcurr1 = endcurr2 = NUL;
|
|
|
|
if (insert_space && currsize > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (has_mbyte)
|
2005-03-15 22:43:58 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
cend = curr + currsize;
|
|
|
|
MB_PTR_BACK(curr, cend);
|
|
|
|
endcurr1 = (*mb_ptr2char)(cend);
|
|
|
|
if (cend > curr)
|
|
|
|
{
|
|
|
|
MB_PTR_BACK(curr, cend);
|
|
|
|
endcurr2 = (*mb_ptr2char)(cend);
|
|
|
|
}
|
2005-03-15 22:43:58 +00:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
2018-08-21 15:12:14 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
endcurr1 = *(curr + currsize - 1);
|
|
|
|
if (currsize > 1)
|
|
|
|
endcurr2 = *(curr + currsize - 2);
|
2018-08-21 15:12:14 +02:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
line_breakcheck();
|
|
|
|
if (got_int)
|
|
|
|
{
|
|
|
|
ret = FAIL;
|
|
|
|
goto theend;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* store the column position before last line */
|
|
|
|
col = sumsize - currsize - spaces[count - 1];
|
|
|
|
|
|
|
|
/* allocate the space for the new line */
|
|
|
|
newp = alloc(sumsize + 1);
|
|
|
|
if (newp == NULL)
|
2004-12-09 21:34:53 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
ret = FAIL;
|
|
|
|
goto theend;
|
2004-12-09 21:34:53 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
cend = newp + sumsize;
|
|
|
|
*cend = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#ifdef FEAT_TEXT_PROP
|
|
|
|
// We need to move properties of the lines that are going to be deleted to
|
|
|
|
// the new long one.
|
|
|
|
if (curbuf->b_has_textprop && !text_prop_frozen)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
// Allocate an array to copy the text properties of joined lines into.
|
|
|
|
// And another array to store the number of properties in each line.
|
|
|
|
prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
|
|
|
|
prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
|
|
|
|
if (prop_lengths == NULL)
|
|
|
|
VIM_CLEAR(prop_lines);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Move affected lines to the new long one.
|
|
|
|
* This loops backwards over the joined lines, including the original line.
|
|
|
|
*
|
|
|
|
* Move marks from each deleted line to the joined line, adjusting the
|
|
|
|
* column. This is not Vi compatible, but Vi deletes the marks, thus that
|
|
|
|
* should not really be a problem.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
for (t = count - 1; ; --t)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int spaces_removed;
|
|
|
|
|
|
|
|
cend -= currsize;
|
|
|
|
mch_memmove(cend, curr, (size_t)currsize);
|
|
|
|
if (spaces[t] > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
cend -= spaces[t];
|
|
|
|
vim_memset(cend, ' ', (size_t)(spaces[t]));
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
|
|
|
|
// If deleting more spaces than adding, the cursor moves no more than
|
|
|
|
// what is added if it is inside these spaces.
|
|
|
|
spaces_removed = (curr - curr_start) - spaces[t];
|
|
|
|
|
|
|
|
mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
|
|
|
|
(long)(cend - newp - spaces_removed), spaces_removed);
|
|
|
|
if (t == 0)
|
|
|
|
break;
|
|
|
|
#ifdef FEAT_TEXT_PROP
|
|
|
|
if (prop_lines != NULL)
|
|
|
|
adjust_props_for_join(curwin->w_cursor.lnum + t,
|
|
|
|
prop_lines + t - 1, prop_lengths + t - 1,
|
|
|
|
(long)(cend - newp - spaces_removed), spaces_removed);
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
|
|
|
|
if (remove_comments)
|
|
|
|
curr += comments[t - 1];
|
|
|
|
if (insert_space && t > 1)
|
|
|
|
curr = skipwhite(curr);
|
|
|
|
currsize = (int)STRLEN(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FEAT_TEXT_PROP
|
|
|
|
if (prop_lines != NULL)
|
|
|
|
join_prop_lines(curwin->w_cursor.lnum, newp,
|
|
|
|
prop_lines, prop_lengths, count);
|
|
|
|
else
|
2017-12-16 18:27:02 +01:00
|
|
|
#endif
|
2019-09-25 22:37:17 +02:00
|
|
|
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
|
2017-12-16 18:27:02 +01:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (setmark)
|
|
|
|
{
|
|
|
|
/* Set the '] mark. */
|
|
|
|
curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
|
|
|
|
curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Only report the change in the first line here, del_lines() will report
|
|
|
|
* the deleted line. */
|
|
|
|
changed_lines(curwin->w_cursor.lnum, currsize,
|
|
|
|
curwin->w_cursor.lnum + 1, 0L);
|
|
|
|
/*
|
|
|
|
* Delete following lines. To do this we move the cursor there
|
|
|
|
* briefly, and then move it back. After del_lines() the cursor may
|
|
|
|
* have moved up (last line deleted), so the current lnum is kept in t.
|
|
|
|
*/
|
|
|
|
t = curwin->w_cursor.lnum;
|
|
|
|
++curwin->w_cursor.lnum;
|
|
|
|
del_lines(count - 1, FALSE);
|
|
|
|
curwin->w_cursor.lnum = t;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Set the cursor column:
|
|
|
|
* Vi compatible: use the column of the first join
|
|
|
|
* vim: use the column of the last join
|
|
|
|
*/
|
|
|
|
curwin->w_cursor.col =
|
|
|
|
(vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
|
|
|
|
check_cursor_col();
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor.coladd = 0;
|
|
|
|
curwin->w_set_curswant = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
theend:
|
|
|
|
vim_free(spaces);
|
|
|
|
if (remove_comments)
|
|
|
|
vim_free(comments);
|
|
|
|
return ret;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Return TRUE if the two comment leaders given are the same. "lnum" is
|
|
|
|
* the first line. White-space is ignored. Note that the whole of
|
|
|
|
* 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
static int
|
|
|
|
same_leader(
|
|
|
|
linenr_T lnum,
|
|
|
|
int leader1_len,
|
|
|
|
char_u *leader1_flags,
|
|
|
|
int leader2_len,
|
|
|
|
char_u *leader2_flags)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int idx1 = 0, idx2 = 0;
|
|
|
|
char_u *p;
|
|
|
|
char_u *line1;
|
|
|
|
char_u *line2;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (leader1_len == 0)
|
|
|
|
return (leader2_len == 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* If first leader has 'f' flag, the lines can be joined only if the
|
|
|
|
* second line does not have a leader.
|
|
|
|
* If first leader has 'e' flag, the lines can never be joined.
|
|
|
|
* If fist leader has 's' flag, the lines can only be joined if there is
|
|
|
|
* some text after it and the second line has the 'm' flag.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
if (leader1_flags != NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
for (p = leader1_flags; *p && *p != ':'; ++p)
|
|
|
|
{
|
|
|
|
if (*p == COM_FIRST)
|
|
|
|
return (leader2_len == 0);
|
|
|
|
if (*p == COM_END)
|
|
|
|
return FALSE;
|
|
|
|
if (*p == COM_START)
|
|
|
|
{
|
|
|
|
if (*(ml_get(lnum) + leader1_len) == NUL)
|
|
|
|
return FALSE;
|
|
|
|
if (leader2_flags == NULL || leader2_len == 0)
|
|
|
|
return FALSE;
|
|
|
|
for (p = leader2_flags; *p && *p != ':'; ++p)
|
|
|
|
if (*p == COM_MIDDLE)
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Get current line and next line, compare the leaders.
|
|
|
|
* The first line has to be saved, only one line can be locked at a time.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
line1 = vim_strsave(ml_get(lnum));
|
|
|
|
if (line1 != NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
|
|
|
|
;
|
|
|
|
line2 = ml_get(lnum + 1);
|
|
|
|
for (idx2 = 0; idx2 < leader2_len; ++idx2)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (!VIM_ISWHITE(line2[idx2]))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (line1[idx1++] != line2[idx2])
|
2004-06-13 20:20:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
|
|
|
while (VIM_ISWHITE(line1[idx1]))
|
|
|
|
++idx1;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
vim_free(line1);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
return (idx2 == leader2_len && idx1 == leader1_len);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Implementation of the format operator 'gq'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
op_format(
|
|
|
|
oparg_T *oap,
|
|
|
|
int keep_cursor) /* keep cursor on same text char */
|
|
|
|
{
|
|
|
|
long old_line_count = curbuf->b_ml.ml_line_count;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Place the cursor where the "gq" or "gw" command was given, so that "u"
|
|
|
|
* can put it back there. */
|
|
|
|
curwin->w_cursor = oap->cursor_start;
|
2015-06-19 15:17:55 +02:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (u_save((linenr_T)(oap->start.lnum - 1),
|
|
|
|
(linenr_T)(oap->end.lnum + 1)) == FAIL)
|
|
|
|
return;
|
|
|
|
curwin->w_cursor = oap->start;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (oap->is_VIsual)
|
|
|
|
/* When there is no change: need to remove the Visual selection */
|
|
|
|
redraw_curbuf_later(INVERTED);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Set '[ mark at the start of the formatted area */
|
|
|
|
curbuf->b_op_start = oap->start;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* For "gw" remember the cursor position and put it back below (adjusted
|
|
|
|
* for joined and split lines). */
|
|
|
|
if (keep_cursor)
|
|
|
|
saved_cursor = oap->cursor_start;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
format_lines(oap->line_count, keep_cursor);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Leave the cursor at the first non-blank of the last formatted line.
|
|
|
|
* If the cursor was moved one line back (e.g. with "Q}") go to the next
|
|
|
|
* line, so "." will do the next lines.
|
|
|
|
*/
|
|
|
|
if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
|
|
|
|
++curwin->w_cursor.lnum;
|
|
|
|
beginline(BL_WHITE | BL_FIX);
|
|
|
|
old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
|
|
|
|
msgmore(old_line_count);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* put '] mark on the end of the formatted area */
|
|
|
|
curbuf->b_op_end = curwin->w_cursor;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (keep_cursor)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor = saved_cursor;
|
|
|
|
saved_cursor.lnum = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (oap->is_VIsual)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
win_T *wp;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
FOR_ALL_WINDOWS(wp)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (wp->w_old_cursor_lnum != 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* When lines have been inserted or deleted, adjust the end of
|
|
|
|
* the Visual area to be redrawn. */
|
|
|
|
if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
|
|
|
|
wp->w_old_cursor_lnum += old_line_count;
|
|
|
|
else
|
|
|
|
wp->w_old_visual_lnum += old_line_count;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#if defined(FEAT_EVAL) || defined(PROTO)
|
|
|
|
/*
|
|
|
|
* Implementation of the format operator 'gq' for when using 'formatexpr'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
op_formatexpr(oparg_T *oap)
|
|
|
|
{
|
|
|
|
if (oap->is_VIsual)
|
|
|
|
/* When there is no change: need to remove the Visual selection */
|
|
|
|
redraw_curbuf_later(INVERTED);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
|
|
|
|
/* As documented: when 'formatexpr' returns non-zero fall back to
|
|
|
|
* internal formatting. */
|
|
|
|
op_format(oap, FALSE);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
int
|
|
|
|
fex_format(
|
|
|
|
linenr_T lnum,
|
|
|
|
long count,
|
|
|
|
int c) /* character to be inserted */
|
|
|
|
{
|
|
|
|
int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
|
|
|
|
OPT_LOCAL);
|
|
|
|
int r;
|
|
|
|
char_u *fex;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Set v:lnum to the first line number and v:count to the number of lines.
|
|
|
|
* Set v:char to the character to be inserted (can be NUL).
|
|
|
|
*/
|
|
|
|
set_vim_var_nr(VV_LNUM, lnum);
|
|
|
|
set_vim_var_nr(VV_COUNT, count);
|
|
|
|
set_vim_var_char(c);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Make a copy, the option could be changed while calling it. */
|
|
|
|
fex = vim_strsave(curbuf->b_p_fex);
|
|
|
|
if (fex == NULL)
|
|
|
|
return 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Evaluate the function.
|
|
|
|
*/
|
|
|
|
if (use_sandbox)
|
|
|
|
++sandbox;
|
|
|
|
r = (int)eval_to_number(fex);
|
|
|
|
if (use_sandbox)
|
|
|
|
--sandbox;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
set_vim_var_string(VV_CHAR, NULL, -1);
|
|
|
|
vim_free(fex);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Format "line_count" lines, starting at the cursor position.
|
|
|
|
* When "line_count" is negative, format until the end of the paragraph.
|
|
|
|
* Lines after the cursor line are saved for undo, caller must have saved the
|
|
|
|
* first line.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
format_lines(
|
|
|
|
linenr_T line_count,
|
|
|
|
int avoid_fex) /* don't use 'formatexpr' */
|
|
|
|
{
|
|
|
|
int max_len;
|
|
|
|
int is_not_par; /* current line not part of parag. */
|
|
|
|
int next_is_not_par; /* next line not part of paragraph */
|
|
|
|
int is_end_par; /* at end of paragraph */
|
|
|
|
int prev_is_end_par = FALSE;/* prev. line not part of parag. */
|
|
|
|
int next_is_start_par = FALSE;
|
|
|
|
int leader_len = 0; /* leader len of current line */
|
|
|
|
int next_leader_len; /* leader len of next line */
|
|
|
|
char_u *leader_flags = NULL; /* flags for leader of current line */
|
|
|
|
char_u *next_leader_flags; /* flags for leader of next line */
|
|
|
|
int do_comments; /* format comments */
|
|
|
|
int do_comments_list = 0; /* format comments with 'n' or '2' */
|
|
|
|
int advance = TRUE;
|
|
|
|
int second_indent = -1; /* indent for second line (comment
|
|
|
|
* aware) */
|
|
|
|
int do_second_indent;
|
|
|
|
int do_number_indent;
|
|
|
|
int do_trail_white;
|
|
|
|
int first_par_line = TRUE;
|
|
|
|
int smd_save;
|
|
|
|
long count;
|
|
|
|
int need_set_indent = TRUE; /* set indent of next paragraph */
|
|
|
|
int force_format = FALSE;
|
|
|
|
int old_State = State;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* length of a line to force formatting: 3 * 'tw' */
|
|
|
|
max_len = comp_textwidth(TRUE) * 3;
|
2006-07-23 20:37:09 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* check for 'q', '2' and '1' in 'formatoptions' */
|
|
|
|
do_comments = has_format_option(FO_Q_COMS);
|
|
|
|
do_second_indent = has_format_option(FO_Q_SECOND);
|
|
|
|
do_number_indent = has_format_option(FO_Q_NUMBER);
|
|
|
|
do_trail_white = has_format_option(FO_WHITE_PAR);
|
2006-07-23 20:37:09 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Get info about the previous and current line.
|
|
|
|
*/
|
|
|
|
if (curwin->w_cursor.lnum > 1)
|
|
|
|
is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
|
2019-09-28 19:05:57 +02:00
|
|
|
, &leader_len, &leader_flags, do_comments);
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
2019-09-25 22:37:17 +02:00
|
|
|
is_not_par = TRUE;
|
|
|
|
next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
|
2019-09-28 19:05:57 +02:00
|
|
|
, &next_leader_len, &next_leader_flags, do_comments);
|
2019-09-25 22:37:17 +02:00
|
|
|
is_end_par = (is_not_par || next_is_not_par);
|
|
|
|
if (!is_end_par && do_trail_white)
|
|
|
|
is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
|
|
|
|
|
|
|
|
curwin->w_cursor.lnum--;
|
|
|
|
for (count = line_count; count != 0 && !got_int; --count)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Advance to next paragraph.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
if (advance)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor.lnum++;
|
|
|
|
prev_is_end_par = is_end_par;
|
|
|
|
is_not_par = next_is_not_par;
|
|
|
|
leader_len = next_leader_len;
|
|
|
|
leader_flags = next_leader_flags;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* The last line to be formatted.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
next_is_not_par = TRUE;
|
|
|
|
next_leader_len = 0;
|
|
|
|
next_leader_flags = NULL;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
|
2019-09-28 19:05:57 +02:00
|
|
|
, &next_leader_len, &next_leader_flags, do_comments);
|
2019-09-25 22:37:17 +02:00
|
|
|
if (do_number_indent)
|
|
|
|
next_is_start_par =
|
|
|
|
(get_number_indent(curwin->w_cursor.lnum + 1) > 0);
|
|
|
|
}
|
|
|
|
advance = TRUE;
|
|
|
|
is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
|
|
|
|
if (!is_end_par && do_trail_white)
|
|
|
|
is_end_par = !ends_in_white(curwin->w_cursor.lnum);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip lines that are not in a paragraph.
|
|
|
|
*/
|
|
|
|
if (is_not_par)
|
|
|
|
{
|
|
|
|
if (line_count < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* For the first line of a paragraph, check indent of second line.
|
|
|
|
* Don't do this for comments and empty lines.
|
|
|
|
*/
|
|
|
|
if (first_par_line
|
|
|
|
&& (do_second_indent || do_number_indent)
|
|
|
|
&& prev_is_end_par
|
|
|
|
&& curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
|
|
|
|
{
|
|
|
|
if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
|
|
|
|
{
|
|
|
|
if (leader_len == 0 && next_leader_len == 0)
|
|
|
|
{
|
|
|
|
/* no comment found */
|
|
|
|
second_indent =
|
|
|
|
get_indent_lnum(curwin->w_cursor.lnum + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
second_indent = next_leader_len;
|
|
|
|
do_comments_list = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (do_number_indent)
|
|
|
|
{
|
|
|
|
if (leader_len == 0 && next_leader_len == 0)
|
|
|
|
{
|
|
|
|
/* no comment found */
|
|
|
|
second_indent =
|
|
|
|
get_number_indent(curwin->w_cursor.lnum);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* get_number_indent() is now "comment aware"... */
|
|
|
|
second_indent =
|
|
|
|
get_number_indent(curwin->w_cursor.lnum);
|
|
|
|
do_comments_list = 1;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* When the comment leader changes, it's the end of the paragraph.
|
|
|
|
*/
|
|
|
|
if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
|
|
|
|
|| !same_leader(curwin->w_cursor.lnum,
|
|
|
|
leader_len, leader_flags,
|
2019-09-28 19:05:57 +02:00
|
|
|
next_leader_len, next_leader_flags))
|
2019-09-25 22:37:17 +02:00
|
|
|
is_end_par = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* If we have got to the end of a paragraph, or the line is
|
|
|
|
* getting long, format it.
|
|
|
|
*/
|
|
|
|
if (is_end_par || force_format)
|
2005-02-22 08:49:11 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (need_set_indent)
|
|
|
|
/* replace indent in first line with minimal number of
|
|
|
|
* 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 && vim_isspace(gchar_cursor()))
|
|
|
|
dec_cursor();
|
|
|
|
|
|
|
|
/* do the formatting, without 'showmode' */
|
|
|
|
State = INSERT; /* for open_line() */
|
|
|
|
smd_save = p_smd;
|
|
|
|
p_smd = FALSE;
|
|
|
|
insertchar(NUL, INSCHAR_FORMAT
|
|
|
|
+ (do_comments ? INSCHAR_DO_COM : 0)
|
|
|
|
+ (do_comments && do_comments_list
|
|
|
|
? INSCHAR_COM_LIST : 0)
|
|
|
|
+ (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
|
|
|
|
State = old_State;
|
|
|
|
p_smd = smd_save;
|
|
|
|
second_indent = -1;
|
|
|
|
/* at end of par.: need to set indent of next par. */
|
|
|
|
need_set_indent = is_end_par;
|
|
|
|
if (is_end_par)
|
|
|
|
{
|
|
|
|
/* When called with a negative line count, break at the
|
|
|
|
* end of the paragraph. */
|
|
|
|
if (line_count < 0)
|
|
|
|
break;
|
|
|
|
first_par_line = TRUE;
|
|
|
|
}
|
|
|
|
force_format = FALSE;
|
2005-02-22 08:49:11 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When still in same paragraph, join the lines together. But
|
|
|
|
* first delete the leader from the second line.
|
|
|
|
*/
|
|
|
|
if (!is_end_par)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
advance = FALSE;
|
|
|
|
curwin->w_cursor.lnum++;
|
|
|
|
curwin->w_cursor.col = 0;
|
|
|
|
if (line_count < 0 && u_save_cursor() == FAIL)
|
|
|
|
break;
|
|
|
|
if (next_leader_len > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
(void)del_bytes((long)next_leader_len, FALSE, FALSE);
|
|
|
|
mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
|
2019-09-28 19:05:57 +02:00
|
|
|
(long)-next_leader_len, 0);
|
|
|
|
}
|
|
|
|
else if (second_indent > 0) // the "leader" for FO_Q_SECOND
|
2019-09-25 22:37:17 +02:00
|
|
|
{
|
|
|
|
int indent = getwhitecols_curline();
|
|
|
|
|
|
|
|
if (indent > 0)
|
|
|
|
{
|
|
|
|
(void)del_bytes(indent, FALSE, FALSE);
|
|
|
|
mark_col_adjust(curwin->w_cursor.lnum,
|
|
|
|
(colnr_T)0, 0L, (long)-indent, 0);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor.lnum--;
|
|
|
|
if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
beep_flush();
|
|
|
|
break;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
first_par_line = FALSE;
|
|
|
|
/* If the line is getting long, format it next time */
|
|
|
|
if (STRLEN(ml_get_curline()) > (size_t)max_len)
|
|
|
|
force_format = TRUE;
|
|
|
|
else
|
|
|
|
force_format = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
line_breakcheck();
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2006-04-27 00:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Return TRUE if line "lnum" ends in a white character.
|
2006-04-27 00:02:13 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
static int
|
|
|
|
ends_in_white(linenr_T lnum)
|
2006-04-27 00:02:13 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *s = ml_get(lnum);
|
|
|
|
size_t l;
|
2005-11-23 21:25:05 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (*s == NUL)
|
|
|
|
return FALSE;
|
|
|
|
/* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
|
|
|
|
* invocation may call function multiple times". */
|
|
|
|
l = STRLEN(s) - 1;
|
|
|
|
return VIM_ISWHITE(s[l]);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Blank lines, and lines containing only the comment leader, are left
|
|
|
|
* untouched by the formatting. The function returns TRUE in this
|
|
|
|
* case. It also returns TRUE when a line starts with the end of a comment
|
|
|
|
* ('e' in comment flags), so that this line is skipped, and not joined to the
|
|
|
|
* previous line. A new paragraph starts after a blank line, or when the
|
|
|
|
* comment leader changes -- webb.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-08-20 20:13:45 +02:00
|
|
|
static int
|
2019-09-25 22:37:17 +02:00
|
|
|
fmt_check_par(
|
|
|
|
linenr_T lnum,
|
|
|
|
int *leader_len,
|
|
|
|
char_u **leader_flags,
|
|
|
|
int do_comments)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
char_u *flags = NULL; /* init for GCC */
|
|
|
|
char_u *ptr;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
ptr = ml_get(lnum);
|
|
|
|
if (do_comments)
|
|
|
|
*leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
2019-09-25 22:37:17 +02:00
|
|
|
*leader_len = 0;
|
|
|
|
|
|
|
|
if (*leader_len > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Search for 'e' flag in comment leader flags.
|
|
|
|
*/
|
|
|
|
flags = *leader_flags;
|
|
|
|
while (*flags && *flags != ':' && *flags != COM_END)
|
|
|
|
++flags;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
|
|
|
|
return (*skipwhite(ptr + *leader_len) == NUL
|
|
|
|
|| (*leader_len > 0 && *flags == COM_END)
|
|
|
|
|| startPS(lnum, NUL, FALSE));
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
|
|
|
|
* previous line is in the same paragraph. Used for auto-formatting.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
int
|
|
|
|
paragraph_start(linenr_T lnum)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-06-11 21:04:39 +02:00
|
|
|
char_u *p;
|
2019-09-25 22:37:17 +02:00
|
|
|
int leader_len = 0; /* leader len of current line */
|
|
|
|
char_u *leader_flags = NULL; /* flags for leader of current line */
|
|
|
|
int next_leader_len; /* leader len of next line */
|
|
|
|
char_u *next_leader_flags; /* flags for leader of next line */
|
|
|
|
int do_comments; /* format comments */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (lnum <= 1)
|
|
|
|
return TRUE; /* start of the file */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
p = ml_get(lnum - 1);
|
|
|
|
if (*p == NUL)
|
|
|
|
return TRUE; /* after empty line */
|
2009-11-17 11:43:06 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
do_comments = has_format_option(FO_Q_COMS);
|
2019-09-28 19:05:57 +02:00
|
|
|
if (fmt_check_par(lnum - 1, &leader_len, &leader_flags, do_comments))
|
2019-09-25 22:37:17 +02:00
|
|
|
return TRUE; /* after non-paragraph line */
|
2009-11-17 11:43:06 +00:00
|
|
|
|
2019-09-28 19:05:57 +02:00
|
|
|
if (fmt_check_par(lnum, &next_leader_len, &next_leader_flags, do_comments))
|
2019-09-25 22:37:17 +02:00
|
|
|
return TRUE; /* "lnum" is not a paragraph line */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
|
|
|
|
return TRUE; /* missing trailing space in previous line. */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
|
|
|
|
return TRUE; /* numbered item starts in "lnum". */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (!same_leader(lnum - 1, leader_len, leader_flags,
|
|
|
|
next_leader_len, next_leader_flags))
|
|
|
|
return TRUE; /* change of comment leader. */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* prepare a few things for block mode yank/delete/tilde
|
|
|
|
*
|
|
|
|
* for delete:
|
|
|
|
* - textlen includes the first/last char to be (partly) deleted
|
|
|
|
* - start/endspaces is the number of columns that are taken by the
|
|
|
|
* first/last deleted char minus the number of columns that have to be
|
|
|
|
* deleted.
|
|
|
|
* for yank and tilde:
|
|
|
|
* - textlen includes the first/last char to be wholly yanked
|
|
|
|
* - start/endspaces is the number of columns of the first/last yanked char
|
|
|
|
* that are to be yanked.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
block_prep(
|
|
|
|
oparg_T *oap,
|
|
|
|
struct block_def *bdp,
|
|
|
|
linenr_T lnum,
|
|
|
|
int is_del)
|
|
|
|
{
|
|
|
|
int incr = 0;
|
|
|
|
char_u *pend;
|
|
|
|
char_u *pstart;
|
|
|
|
char_u *line;
|
|
|
|
char_u *prev_pstart;
|
|
|
|
char_u *prev_pend;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
bdp->startspaces = 0;
|
|
|
|
bdp->endspaces = 0;
|
|
|
|
bdp->textlen = 0;
|
|
|
|
bdp->start_vcol = 0;
|
|
|
|
bdp->end_vcol = 0;
|
|
|
|
bdp->is_short = FALSE;
|
|
|
|
bdp->is_oneChar = FALSE;
|
|
|
|
bdp->pre_whitesp = 0;
|
|
|
|
bdp->pre_whitesp_c = 0;
|
|
|
|
bdp->end_char_vcols = 0;
|
|
|
|
bdp->start_char_vcols = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
line = ml_get(lnum);
|
|
|
|
pstart = line;
|
|
|
|
prev_pstart = line;
|
|
|
|
while (bdp->start_vcol < oap->start_vcol && *pstart)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Count a tab for what it's worth (if list mode not on) */
|
|
|
|
incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
|
|
|
|
bdp->start_vcol += incr;
|
|
|
|
if (VIM_ISWHITE(*pstart))
|
|
|
|
{
|
|
|
|
bdp->pre_whitesp += incr;
|
|
|
|
bdp->pre_whitesp_c++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bdp->pre_whitesp = 0;
|
|
|
|
bdp->pre_whitesp_c = 0;
|
|
|
|
}
|
|
|
|
prev_pstart = pstart;
|
|
|
|
MB_PTR_ADV(pstart);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
bdp->start_char_vcols = incr;
|
|
|
|
if (bdp->start_vcol < oap->start_vcol) /* line too short */
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
bdp->end_vcol = bdp->start_vcol;
|
|
|
|
bdp->is_short = TRUE;
|
|
|
|
if (!is_del || oap->op_type == OP_APPEND)
|
|
|
|
bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* notice: this converts partly selected Multibyte characters to
|
|
|
|
* spaces, too. */
|
|
|
|
bdp->startspaces = bdp->start_vcol - oap->start_vcol;
|
|
|
|
if (is_del && bdp->startspaces)
|
|
|
|
bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
|
|
|
|
pend = pstart;
|
|
|
|
bdp->end_vcol = bdp->start_vcol;
|
|
|
|
if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
bdp->is_oneChar = TRUE;
|
|
|
|
if (oap->op_type == OP_INSERT)
|
|
|
|
bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
|
|
|
|
else if (oap->op_type == OP_APPEND)
|
|
|
|
{
|
|
|
|
bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
|
|
|
|
bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
|
|
|
|
if (is_del && oap->op_type != OP_LSHIFT)
|
|
|
|
{
|
|
|
|
/* just putting the sum of those two into
|
|
|
|
* bdp->startspaces doesn't work for Visual replace,
|
|
|
|
* so we have to split the tab in two */
|
|
|
|
bdp->startspaces = bdp->start_char_vcols
|
|
|
|
- (bdp->start_vcol - oap->start_vcol);
|
|
|
|
bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
|
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
2019-09-25 22:37:17 +02:00
|
|
|
{
|
|
|
|
prev_pend = pend;
|
|
|
|
while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
|
|
|
|
{
|
|
|
|
/* Count a tab for what it's worth (if list mode not on) */
|
|
|
|
prev_pend = pend;
|
|
|
|
incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
|
|
|
|
bdp->end_vcol += incr;
|
|
|
|
}
|
|
|
|
if (bdp->end_vcol <= oap->end_vcol
|
|
|
|
&& (!is_del
|
|
|
|
|| oap->op_type == OP_APPEND
|
|
|
|
|| oap->op_type == OP_REPLACE)) /* line too short */
|
|
|
|
{
|
|
|
|
bdp->is_short = TRUE;
|
|
|
|
/* Alternative: include spaces to fill up the block.
|
|
|
|
* Disadvantage: can lead to trailing spaces when the line is
|
|
|
|
* short where the text is put */
|
|
|
|
/* if (!is_del || oap->op_type == OP_APPEND) */
|
|
|
|
if (oap->op_type == OP_APPEND || virtual_op)
|
|
|
|
bdp->endspaces = oap->end_vcol - bdp->end_vcol
|
|
|
|
+ oap->inclusive;
|
|
|
|
else
|
|
|
|
bdp->endspaces = 0; /* replace doesn't add characters */
|
|
|
|
}
|
|
|
|
else if (bdp->end_vcol > oap->end_vcol)
|
|
|
|
{
|
|
|
|
bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
|
|
|
|
if (!is_del && bdp->endspaces)
|
|
|
|
{
|
|
|
|
bdp->endspaces = incr - bdp->endspaces;
|
|
|
|
if (pend != pstart)
|
|
|
|
pend = prev_pend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bdp->end_char_vcols = incr;
|
|
|
|
if (is_del && bdp->startspaces)
|
|
|
|
pstart = prev_pstart;
|
|
|
|
bdp->textlen = (int)(pend - pstart);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
bdp->textcol = (colnr_T) (pstart - line);
|
|
|
|
bdp->textstart = pstart;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:12:59 +02:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Handle the add/subtract operator.
|
2012-06-06 16:12:59 +02:00
|
|
|
*/
|
2004-06-13 20:20:40 +00:00
|
|
|
void
|
2019-09-25 22:37:17 +02:00
|
|
|
op_addsub(
|
|
|
|
oparg_T *oap,
|
|
|
|
linenr_T Prenum1, /* Amount of add/subtract */
|
|
|
|
int g_cmd) /* was g<c-a>/g<c-x> */
|
|
|
|
{
|
|
|
|
pos_T pos;
|
|
|
|
struct block_def bd;
|
|
|
|
int change_cnt = 0;
|
|
|
|
linenr_T amount = Prenum1;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
// do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
|
|
|
|
// buffer is not completely updated yet. Postpone updating folds until before
|
|
|
|
// the call to changed_lines().
|
|
|
|
#ifdef FEAT_FOLDING
|
|
|
|
disable_fold_update++;
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (!VIsual_active)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
pos = curwin->w_cursor;
|
|
|
|
if (u_save_cursor() == FAIL)
|
|
|
|
{
|
|
|
|
#ifdef FEAT_FOLDING
|
|
|
|
disable_fold_update--;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
|
|
|
|
#ifdef FEAT_FOLDING
|
|
|
|
disable_fold_update--;
|
|
|
|
#endif
|
|
|
|
if (change_cnt)
|
|
|
|
changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int one_change;
|
|
|
|
int length;
|
|
|
|
pos_T startpos;
|
2013-03-07 18:02:30 +01:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (u_save((linenr_T)(oap->start.lnum - 1),
|
|
|
|
(linenr_T)(oap->end.lnum + 1)) == FAIL)
|
2009-07-01 16:04:58 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
#ifdef FEAT_FOLDING
|
|
|
|
disable_fold_update--;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2009-07-01 16:04:58 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
pos = oap->start;
|
|
|
|
for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
|
|
|
|
{
|
|
|
|
if (oap->block_mode) /* Visual block mode */
|
2009-07-01 16:04:58 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
block_prep(oap, &bd, pos.lnum, FALSE);
|
|
|
|
pos.col = bd.textcol;
|
|
|
|
length = bd.textlen;
|
|
|
|
}
|
|
|
|
else if (oap->motion_type == MLINE)
|
|
|
|
{
|
|
|
|
curwin->w_cursor.col = 0;
|
|
|
|
pos.col = 0;
|
|
|
|
length = (colnr_T)STRLEN(ml_get(pos.lnum));
|
|
|
|
}
|
|
|
|
else /* oap->motion_type == MCHAR */
|
|
|
|
{
|
|
|
|
if (pos.lnum == oap->start.lnum && !oap->inclusive)
|
|
|
|
dec(&(oap->end));
|
|
|
|
length = (colnr_T)STRLEN(ml_get(pos.lnum));
|
|
|
|
pos.col = 0;
|
|
|
|
if (pos.lnum == oap->start.lnum)
|
2009-07-01 16:04:58 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
pos.col += oap->start.col;
|
|
|
|
length -= oap->start.col;
|
2009-07-01 16:04:58 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
if (pos.lnum == oap->end.lnum)
|
2013-03-07 18:02:30 +01:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
length = (int)STRLEN(ml_get(oap->end.lnum));
|
|
|
|
if (oap->end.col >= length)
|
|
|
|
oap->end.col = length - 1;
|
|
|
|
length = oap->end.col - pos.col + 1;
|
2013-03-07 18:02:30 +01:00
|
|
|
}
|
2009-07-01 16:04:58 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
one_change = do_addsub(oap->op_type, &pos, length, amount);
|
|
|
|
if (one_change)
|
2013-03-07 18:02:30 +01:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Remember the start position of the first change. */
|
|
|
|
if (change_cnt == 0)
|
|
|
|
startpos = curbuf->b_op_start;
|
|
|
|
++change_cnt;
|
2013-03-07 18:02:30 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#ifdef FEAT_NETBEANS_INTG
|
|
|
|
if (netbeans_active() && one_change)
|
|
|
|
{
|
|
|
|
char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
|
|
|
|
netbeans_inserted(curbuf, pos.lnum, pos.col,
|
|
|
|
&ptr[pos.col], length);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
2019-09-25 22:37:17 +02:00
|
|
|
if (g_cmd && one_change)
|
|
|
|
amount += Prenum1;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#ifdef FEAT_FOLDING
|
|
|
|
disable_fold_update--;
|
|
|
|
#endif
|
|
|
|
if (change_cnt)
|
|
|
|
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (!change_cnt && oap->is_VIsual)
|
|
|
|
/* No change: need to remove the Visual selection */
|
|
|
|
redraw_curbuf_later(INVERTED);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Set '[ mark if something changed. Keep the last end
|
|
|
|
* position from do_addsub(). */
|
|
|
|
if (change_cnt > 0)
|
|
|
|
curbuf->b_op_start = startpos;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (change_cnt > p_report)
|
|
|
|
smsg(NGETTEXT("%ld line changed", "%ld lines changed",
|
|
|
|
change_cnt), change_cnt);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 01:14:29 +02:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Add or subtract 'Prenum1' from a number in a line
|
|
|
|
* op_type is OP_NR_ADD or OP_NR_SUB
|
|
|
|
*
|
|
|
|
* Returns TRUE if some character was changed.
|
2011-06-19 01:14:29 +02:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
static int
|
|
|
|
do_addsub(
|
|
|
|
int op_type,
|
|
|
|
pos_T *pos,
|
|
|
|
int length,
|
|
|
|
linenr_T Prenum1)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int col;
|
|
|
|
char_u *buf1;
|
|
|
|
char_u buf2[NUMBUFLEN];
|
|
|
|
int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
|
|
|
|
static int hexupper = FALSE; /* 0xABC */
|
|
|
|
uvarnumber_T n;
|
|
|
|
uvarnumber_T oldn;
|
|
|
|
char_u *ptr;
|
|
|
|
int c;
|
|
|
|
int todel;
|
|
|
|
int dohex;
|
|
|
|
int dooct;
|
|
|
|
int dobin;
|
|
|
|
int doalp;
|
|
|
|
int firstdigit;
|
|
|
|
int subtract;
|
|
|
|
int negative = FALSE;
|
|
|
|
int was_positive = TRUE;
|
|
|
|
int visual = VIsual_active;
|
|
|
|
int did_change = FALSE;
|
|
|
|
pos_T save_cursor = curwin->w_cursor;
|
|
|
|
int maxlen = 0;
|
|
|
|
pos_T startpos;
|
|
|
|
pos_T endpos;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
|
|
|
|
dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
|
|
|
|
dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
|
|
|
|
doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor = *pos;
|
|
|
|
ptr = ml_get(pos->lnum);
|
|
|
|
col = pos->col;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (*ptr == NUL)
|
|
|
|
goto theend;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* First check if we are on a hexadecimal number, after the "0x".
|
|
|
|
*/
|
|
|
|
if (!VIsual_active)
|
|
|
|
{
|
|
|
|
if (dobin)
|
|
|
|
while (col > 0 && vim_isbdigit(ptr[col]))
|
|
|
|
{
|
|
|
|
--col;
|
|
|
|
if (has_mbyte)
|
|
|
|
col -= (*mb_head_off)(ptr, ptr + col);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (dohex)
|
|
|
|
while (col > 0 && vim_isxdigit(ptr[col]))
|
|
|
|
{
|
|
|
|
--col;
|
|
|
|
if (has_mbyte)
|
|
|
|
col -= (*mb_head_off)(ptr, ptr + col);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if ( dobin
|
|
|
|
&& dohex
|
|
|
|
&& ! ((col > 0
|
|
|
|
&& (ptr[col] == 'X'
|
|
|
|
|| ptr[col] == 'x')
|
|
|
|
&& ptr[col - 1] == '0'
|
|
|
|
&& (!has_mbyte ||
|
|
|
|
!(*mb_head_off)(ptr, ptr + col - 1))
|
|
|
|
&& vim_isxdigit(ptr[col + 1]))))
|
|
|
|
{
|
|
|
|
|
|
|
|
/* In case of binary/hexadecimal pattern overlap match, rescan */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
col = pos->col;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
while (col > 0 && vim_isdigit(ptr[col]))
|
|
|
|
{
|
|
|
|
col--;
|
|
|
|
if (has_mbyte)
|
|
|
|
col -= (*mb_head_off)(ptr, ptr + col);
|
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (( dohex
|
|
|
|
&& col > 0
|
|
|
|
&& (ptr[col] == 'X'
|
|
|
|
|| ptr[col] == 'x')
|
|
|
|
&& ptr[col - 1] == '0'
|
|
|
|
&& (!has_mbyte ||
|
|
|
|
!(*mb_head_off)(ptr, ptr + col - 1))
|
|
|
|
&& vim_isxdigit(ptr[col + 1])) ||
|
|
|
|
( dobin
|
|
|
|
&& col > 0
|
|
|
|
&& (ptr[col] == 'B'
|
|
|
|
|| ptr[col] == 'b')
|
|
|
|
&& ptr[col - 1] == '0'
|
|
|
|
&& (!has_mbyte ||
|
|
|
|
!(*mb_head_off)(ptr, ptr + col - 1))
|
|
|
|
&& vim_isbdigit(ptr[col + 1])))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Found hexadecimal or binary number, move to its start. */
|
|
|
|
--col;
|
|
|
|
if (has_mbyte)
|
|
|
|
col -= (*mb_head_off)(ptr, ptr + col);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
2019-09-25 22:37:17 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Search forward and then backward to find the start of number.
|
|
|
|
*/
|
|
|
|
col = pos->col;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
while (ptr[col] != NUL
|
|
|
|
&& !vim_isdigit(ptr[col])
|
|
|
|
&& !(doalp && ASCII_ISALPHA(ptr[col])))
|
|
|
|
col += MB_PTR2LEN(ptr + col);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
while (col > 0
|
|
|
|
&& vim_isdigit(ptr[col - 1])
|
|
|
|
&& !(doalp && ASCII_ISALPHA(ptr[col])))
|
|
|
|
{
|
|
|
|
--col;
|
|
|
|
if (has_mbyte)
|
|
|
|
col -= (*mb_head_off)(ptr, ptr + col);
|
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (visual)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
while (ptr[col] != NUL && length > 0
|
|
|
|
&& !vim_isdigit(ptr[col])
|
|
|
|
&& !(doalp && ASCII_ISALPHA(ptr[col])))
|
|
|
|
{
|
|
|
|
int mb_len = MB_PTR2LEN(ptr + col);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
col += mb_len;
|
|
|
|
length -= mb_len;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (length == 0)
|
|
|
|
goto theend;
|
2014-01-14 12:33:36 +01:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (col > pos->col && ptr[col - 1] == '-'
|
|
|
|
&& (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
|
|
|
|
{
|
|
|
|
negative = TRUE;
|
|
|
|
was_positive = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* If a number was found, and saving for undo works, replace the number.
|
|
|
|
*/
|
|
|
|
firstdigit = ptr[col];
|
|
|
|
if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
beep_flush();
|
|
|
|
goto theend;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (doalp && ASCII_ISALPHA(firstdigit))
|
2014-04-02 19:55:10 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* decrement or increment alphabetic character */
|
|
|
|
if (op_type == OP_NR_SUB)
|
|
|
|
{
|
|
|
|
if (CharOrd(firstdigit) < Prenum1)
|
|
|
|
{
|
|
|
|
if (isupper(firstdigit))
|
|
|
|
firstdigit = 'A';
|
|
|
|
else
|
|
|
|
firstdigit = 'a';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#ifdef EBCDIC
|
|
|
|
firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
|
|
|
|
#else
|
|
|
|
firstdigit -= Prenum1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
2014-04-02 19:55:10 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (26 - CharOrd(firstdigit) - 1 < Prenum1)
|
2014-04-02 19:55:10 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (isupper(firstdigit))
|
|
|
|
firstdigit = 'Z';
|
|
|
|
else
|
|
|
|
firstdigit = 'z';
|
2014-04-02 19:55:10 +02:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
|
|
|
#ifdef EBCDIC
|
|
|
|
firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
|
|
|
|
#else
|
|
|
|
firstdigit += Prenum1;
|
|
|
|
#endif
|
2014-04-02 19:55:10 +02:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
curwin->w_cursor.col = col;
|
|
|
|
if (!did_change)
|
|
|
|
startpos = curwin->w_cursor;
|
|
|
|
did_change = TRUE;
|
|
|
|
(void)del_char(FALSE);
|
|
|
|
ins_char(firstdigit);
|
|
|
|
endpos = curwin->w_cursor;
|
|
|
|
curwin->w_cursor.col = col;
|
2014-04-02 19:55:10 +02:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
if (col > 0 && ptr[col - 1] == '-'
|
|
|
|
&& (!has_mbyte ||
|
|
|
|
!(*mb_head_off)(ptr, ptr + col - 1))
|
|
|
|
&& !visual)
|
|
|
|
{
|
|
|
|
/* negative number */
|
|
|
|
--col;
|
|
|
|
negative = TRUE;
|
|
|
|
}
|
|
|
|
/* get the number value (unsigned) */
|
|
|
|
if (visual && VIsual_mode != 'V')
|
|
|
|
maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
|
|
|
|
? (int)STRLEN(ptr) - col
|
|
|
|
: length);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
vim_str2nr(ptr + col, &pre, &length,
|
|
|
|
0 + (dobin ? STR2NR_BIN : 0)
|
|
|
|
+ (dooct ? STR2NR_OCT : 0)
|
|
|
|
+ (dohex ? STR2NR_HEX : 0),
|
|
|
|
NULL, &n, maxlen, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* ignore leading '-' for hex and octal and bin numbers */
|
|
|
|
if (pre && negative)
|
|
|
|
{
|
|
|
|
++col;
|
|
|
|
--length;
|
|
|
|
negative = FALSE;
|
|
|
|
}
|
|
|
|
/* add or subtract */
|
|
|
|
subtract = FALSE;
|
|
|
|
if (op_type == OP_NR_SUB)
|
|
|
|
subtract ^= TRUE;
|
|
|
|
if (negative)
|
|
|
|
subtract ^= TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
oldn = n;
|
|
|
|
if (subtract)
|
|
|
|
n -= (uvarnumber_T)Prenum1;
|
|
|
|
else
|
|
|
|
n += (uvarnumber_T)Prenum1;
|
|
|
|
/* handle wraparound for decimal numbers */
|
|
|
|
if (!pre)
|
|
|
|
{
|
|
|
|
if (subtract)
|
|
|
|
{
|
|
|
|
if (n > oldn)
|
|
|
|
{
|
|
|
|
n = 1 + (n ^ (uvarnumber_T)-1);
|
|
|
|
negative ^= TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* add */
|
|
|
|
if (n < oldn)
|
|
|
|
{
|
|
|
|
n = (n ^ (uvarnumber_T)-1);
|
|
|
|
negative ^= TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
negative = FALSE;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (visual && !was_positive && !negative && col > 0)
|
|
|
|
{
|
|
|
|
/* need to remove the '-' */
|
|
|
|
col--;
|
|
|
|
length++;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Delete the old number.
|
|
|
|
*/
|
|
|
|
curwin->w_cursor.col = col;
|
|
|
|
if (!did_change)
|
|
|
|
startpos = curwin->w_cursor;
|
|
|
|
did_change = TRUE;
|
|
|
|
todel = length;
|
|
|
|
c = gchar_cursor();
|
|
|
|
/*
|
|
|
|
* Don't include the '-' in the length, only the length of the
|
|
|
|
* part after it is kept the same.
|
|
|
|
*/
|
|
|
|
if (c == '-')
|
|
|
|
--length;
|
|
|
|
while (todel-- > 0)
|
|
|
|
{
|
|
|
|
if (c < 0x100 && isalpha(c))
|
|
|
|
{
|
|
|
|
if (isupper(c))
|
|
|
|
hexupper = TRUE;
|
|
|
|
else
|
|
|
|
hexupper = FALSE;
|
|
|
|
}
|
|
|
|
/* del_char() will mark line needing displaying */
|
|
|
|
(void)del_char(FALSE);
|
|
|
|
c = gchar_cursor();
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Prepare the leading characters in buf1[].
|
|
|
|
* When there are many leading zeros it could be very long.
|
|
|
|
* Allocate a bit too much.
|
|
|
|
*/
|
|
|
|
buf1 = alloc(length + NUMBUFLEN);
|
|
|
|
if (buf1 == NULL)
|
|
|
|
goto theend;
|
|
|
|
ptr = buf1;
|
|
|
|
if (negative && (!visual || was_positive))
|
|
|
|
*ptr++ = '-';
|
|
|
|
if (pre)
|
|
|
|
{
|
|
|
|
*ptr++ = '0';
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
if (pre == 'b' || pre == 'B' ||
|
|
|
|
pre == 'x' || pre == 'X')
|
2014-04-02 19:55:10 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
*ptr++ = pre;
|
|
|
|
--length;
|
2014-04-02 19:55:10 +02:00
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* Put the number characters in buf2[].
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2019-09-25 22:37:17 +02:00
|
|
|
if (pre == 'b' || pre == 'B')
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int bit = 0;
|
|
|
|
int bits = sizeof(uvarnumber_T) * 8;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* leading zeros */
|
|
|
|
for (bit = bits; bit > 0; bit--)
|
|
|
|
if ((n >> (bit - 1)) & 0x1) break;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
for (i = 0; bit > 0; bit--)
|
|
|
|
buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
buf2[i] = '\0';
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
else if (pre == 0)
|
|
|
|
vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
|
|
|
|
(long_long_u_T)n);
|
|
|
|
else if (pre == '0')
|
|
|
|
vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
|
|
|
|
(long_long_u_T)n);
|
|
|
|
else if (pre && hexupper)
|
|
|
|
vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
|
|
|
|
(long_long_u_T)n);
|
|
|
|
else
|
|
|
|
vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
|
|
|
|
(long_long_u_T)n);
|
|
|
|
length -= (int)STRLEN(buf2);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Adjust number of zeros to the new number of digits, so the
|
|
|
|
* total length of the number remains the same.
|
|
|
|
* Don't do this when
|
|
|
|
* the result may look like an octal number.
|
|
|
|
*/
|
|
|
|
if (firstdigit == '0' && !(dooct && pre == 0))
|
|
|
|
while (length-- > 0)
|
|
|
|
*ptr++ = '0';
|
|
|
|
*ptr = NUL;
|
|
|
|
STRCAT(buf1, buf2);
|
|
|
|
ins_str(buf1); /* insert the new number */
|
|
|
|
vim_free(buf1);
|
|
|
|
endpos = curwin->w_cursor;
|
|
|
|
if (did_change && curwin->w_cursor.col)
|
|
|
|
--curwin->w_cursor.col;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
if (did_change)
|
2014-04-02 22:17:10 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
/* set the '[ and '] marks */
|
|
|
|
curbuf->b_op_start = startpos;
|
|
|
|
curbuf->b_op_end = endpos;
|
|
|
|
if (curbuf->b_op_end.col > 0)
|
|
|
|
--curbuf->b_op_end.col;
|
2014-04-02 22:17:10 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
theend:
|
|
|
|
if (visual)
|
|
|
|
curwin->w_cursor = save_cursor;
|
|
|
|
else if (did_change)
|
|
|
|
curwin->w_set_curswant = TRUE;
|
2014-04-02 22:17:10 +02:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
return did_change;
|
2014-04-02 22:17:10 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
2019-09-25 22:37:17 +02:00
|
|
|
* SELECTION / PRIMARY ('*')
|
|
|
|
*
|
|
|
|
* Text selection stuff that uses the GUI selection register '*'. When using a
|
|
|
|
* GUI this may be text from another window, otherwise it is the last text we
|
|
|
|
* had highlighted with VIsual mode. With mouse support, clicking the middle
|
|
|
|
* button performs the paste, otherwise you will need to do <"*p>. "
|
|
|
|
* If not under X, it is synonymous with the clipboard register '+'.
|
|
|
|
*
|
|
|
|
* X CLIPBOARD ('+')
|
|
|
|
*
|
|
|
|
* Text selection stuff that uses the GUI clipboard register '+'.
|
|
|
|
* Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
|
|
|
|
* It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
|
|
|
|
* otherwise you will need to do <"+p>. "
|
|
|
|
* If not under X, it is synonymous with the selection register '*'.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
2014-04-02 22:17:10 +02:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/*
|
|
|
|
* Routine to export any final X selection we had to the environment
|
|
|
|
* so that the text is still available after Vim has exited. X selections
|
|
|
|
* only exist while the owning application exists, so we write to the
|
|
|
|
* permanent (while X runs) store CUT_BUFFER0.
|
|
|
|
* Dump the CLIPBOARD selection if we own it (it's logically the more
|
|
|
|
* 'permanent' of the two), otherwise the PRIMARY one.
|
|
|
|
* For now, use a hard-coded sanity limit of 1Mb of data.
|
|
|
|
*/
|
|
|
|
#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
|
2004-06-13 20:20:40 +00:00
|
|
|
void
|
2019-09-25 22:37:17 +02:00
|
|
|
x11_export_final_selection(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
Display *dpy;
|
|
|
|
char_u *str = NULL;
|
|
|
|
long_u len = 0;
|
|
|
|
int motion_type = -1;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
# ifdef FEAT_GUI
|
|
|
|
if (gui.in_use)
|
|
|
|
dpy = X_DISPLAY;
|
2005-06-17 22:00:15 +00:00
|
|
|
else
|
2019-09-25 22:37:17 +02:00
|
|
|
# endif
|
|
|
|
# ifdef FEAT_XCLIPBOARD
|
|
|
|
dpy = xterm_dpy;
|
|
|
|
# else
|
2004-06-13 20:20:40 +00:00
|
|
|
return;
|
2019-09-25 22:37:17 +02:00
|
|
|
# endif
|
2015-01-27 18:44:16 +01:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Get selection to export */
|
|
|
|
if (clip_plus.owned)
|
|
|
|
motion_type = clip_convert_selection(&str, &len, &clip_plus);
|
|
|
|
else if (clip_star.owned)
|
|
|
|
motion_type = clip_convert_selection(&str, &len, &clip_star);
|
2015-01-27 18:44:16 +01:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* Check it's OK */
|
|
|
|
if (dpy != NULL && str != NULL && motion_type >= 0
|
|
|
|
&& len < 1024*1024 && len > 0)
|
2005-06-17 22:00:15 +00:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int ok = TRUE;
|
2011-06-19 01:14:29 +02:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
/* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
|
|
|
|
* 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
|
|
|
|
* encoding conversion usually doesn't work, so keep the text as-is.
|
|
|
|
*/
|
|
|
|
if (has_mbyte)
|
2014-04-02 22:17:10 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
vimconv_T vc;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-09-25 22:37:17 +02:00
|
|
|
vc.vc_type = CONV_NONE;
|
|
|
|
if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
|
2014-04-02 22:17:10 +02:00
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
int intlen = len;
|
|
|
|
char_u *conv_str;
|
|
|
|
|
|
|
|
vc.vc_fail = TRUE;
|
|
|
|
conv_str = string_convert(&vc, str, &intlen);
|
|
|
|
len = intlen;
|
|
|
|
if (conv_str != NULL)
|
|
|
|
{
|
|
|
|
vim_free(str);
|
|
|
|
str = conv_str;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ok = FALSE;
|
|
|
|
}
|
|
|
|
convert_setup(&vc, NULL, NULL);
|
2014-04-02 22:17:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-25 22:37:17 +02:00
|
|
|
ok = FALSE;
|
2014-04-02 22:17:10 +02:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do not store the string if conversion failed. Better to use any
|
|
|
|
* other selection than garbled text. */
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
XStoreBuffer(dpy, (char *)str, (int)len, 0);
|
|
|
|
XFlush(dpy);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
|
|
|
|
vim_free(str);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2019-09-25 22:37:17 +02:00
|
|
|
#endif
|
|
|
|
#endif /* FEAT_CLIPBOARD || PROTO */
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
void
|
2016-01-30 19:39:49 +01:00
|
|
|
clear_oparg(oparg_T *oap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
vim_memset(oap, 0, sizeof(oparg_T));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-02-07 22:01:03 +00:00
|
|
|
* Count the number of bytes, characters and "words" in a line.
|
2004-06-13 20:20:40 +00:00
|
|
|
*
|
|
|
|
* "Words" are counted by looking for boundaries between non-space and
|
|
|
|
* space characters. (it seems to produce results that match 'wc'.)
|
|
|
|
*
|
2005-02-07 22:01:03 +00:00
|
|
|
* Return value is byte count; word count for the line is added to "*wc".
|
|
|
|
* Char count is added to "*cc".
|
2004-06-13 20:20:40 +00:00
|
|
|
*
|
|
|
|
* The function will only examine the first "limit" characters in the
|
|
|
|
* line, stopping if it encounters an end-of-line (NUL byte). In that
|
|
|
|
* case, eol_size will be added to the character count to account for
|
|
|
|
* the size of the EOL character.
|
|
|
|
*/
|
2016-07-01 18:17:26 +02:00
|
|
|
static varnumber_T
|
2016-01-30 19:39:49 +01:00
|
|
|
line_count_info(
|
|
|
|
char_u *line,
|
2016-07-01 18:17:26 +02:00
|
|
|
varnumber_T *wc,
|
|
|
|
varnumber_T *cc,
|
|
|
|
varnumber_T limit,
|
2016-01-30 19:39:49 +01:00
|
|
|
int eol_size)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-07-01 18:17:26 +02:00
|
|
|
varnumber_T i;
|
|
|
|
varnumber_T words = 0;
|
|
|
|
varnumber_T chars = 0;
|
2004-06-13 20:20:40 +00:00
|
|
|
int is_word = 0;
|
|
|
|
|
2012-06-29 13:34:19 +02:00
|
|
|
for (i = 0; i < limit && line[i] != NUL; )
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (is_word)
|
|
|
|
{
|
|
|
|
if (vim_isspace(line[i]))
|
|
|
|
{
|
|
|
|
words++;
|
|
|
|
is_word = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!vim_isspace(line[i]))
|
|
|
|
is_word = 1;
|
2005-02-07 22:01:03 +00:00
|
|
|
++chars;
|
2005-08-10 21:07:57 +00:00
|
|
|
i += (*mb_ptr2len)(line + i);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_word)
|
|
|
|
words++;
|
|
|
|
*wc += words;
|
|
|
|
|
|
|
|
/* Add eol_size if the end of line was reached before hitting limit. */
|
2011-08-10 12:11:01 +02:00
|
|
|
if (i < limit && line[i] == NUL)
|
2005-02-07 22:01:03 +00:00
|
|
|
{
|
2004-06-13 20:20:40 +00:00
|
|
|
i += eol_size;
|
2005-02-07 22:01:03 +00:00
|
|
|
chars += eol_size;
|
|
|
|
}
|
|
|
|
*cc += chars;
|
2004-06-13 20:20:40 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give some info about the position of the cursor (for "g CTRL-G").
|
|
|
|
* In Visual mode, give some info about the selected region. (In this case,
|
|
|
|
* the *_count_cursor variables store running totals for the selection.)
|
2016-01-03 22:49:16 +01:00
|
|
|
* When "dict" is not NULL store the info there instead of showing it.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
void
|
2016-01-30 19:39:49 +01:00
|
|
|
cursor_pos_info(dict_T *dict)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
char_u *p;
|
2005-05-19 21:08:39 +00:00
|
|
|
char_u buf1[50];
|
|
|
|
char_u buf2[40];
|
2004-06-13 20:20:40 +00:00
|
|
|
linenr_T lnum;
|
2016-07-01 18:17:26 +02:00
|
|
|
varnumber_T byte_count = 0;
|
|
|
|
varnumber_T bom_count = 0;
|
|
|
|
varnumber_T byte_count_cursor = 0;
|
|
|
|
varnumber_T char_count = 0;
|
|
|
|
varnumber_T char_count_cursor = 0;
|
|
|
|
varnumber_T word_count = 0;
|
|
|
|
varnumber_T word_count_cursor = 0;
|
2005-02-07 22:01:03 +00:00
|
|
|
int eol_size;
|
2016-07-01 18:17:26 +02:00
|
|
|
varnumber_T last_check = 100000L;
|
2004-06-13 20:20:40 +00:00
|
|
|
long line_count_selected = 0;
|
|
|
|
pos_T min_pos, max_pos;
|
|
|
|
oparg_T oparg;
|
|
|
|
struct block_def bd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the length of the file in characters.
|
|
|
|
*/
|
|
|
|
if (curbuf->b_ml.ml_flags & ML_EMPTY)
|
|
|
|
{
|
2016-01-03 22:49:16 +01:00
|
|
|
if (dict == NULL)
|
|
|
|
{
|
2019-01-19 17:43:09 +01:00
|
|
|
msg(_(no_lines_msg));
|
2016-01-03 22:49:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (get_fileformat(curbuf) == EOL_DOS)
|
|
|
|
eol_size = 2;
|
|
|
|
else
|
|
|
|
eol_size = 1;
|
|
|
|
|
|
|
|
if (VIsual_active)
|
|
|
|
{
|
2017-03-12 18:23:53 +01:00
|
|
|
if (LT_POS(VIsual, curwin->w_cursor))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
min_pos = VIsual;
|
|
|
|
max_pos = curwin->w_cursor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
min_pos = curwin->w_cursor;
|
|
|
|
max_pos = VIsual;
|
|
|
|
}
|
|
|
|
if (*p_sel == 'e' && max_pos.col > 0)
|
|
|
|
--max_pos.col;
|
|
|
|
|
|
|
|
if (VIsual_mode == Ctrl_V)
|
|
|
|
{
|
2009-04-29 15:41:40 +00:00
|
|
|
#ifdef FEAT_LINEBREAK
|
|
|
|
char_u * saved_sbr = p_sbr;
|
|
|
|
|
|
|
|
/* Make 'sbr' empty for a moment to get the correct size. */
|
|
|
|
p_sbr = empty_option;
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
oparg.is_VIsual = 1;
|
|
|
|
oparg.block_mode = TRUE;
|
|
|
|
oparg.op_type = OP_NOP;
|
|
|
|
getvcols(curwin, &min_pos, &max_pos,
|
2006-02-25 21:52:33 +00:00
|
|
|
&oparg.start_vcol, &oparg.end_vcol);
|
2009-04-29 15:41:40 +00:00
|
|
|
#ifdef FEAT_LINEBREAK
|
|
|
|
p_sbr = saved_sbr;
|
|
|
|
#endif
|
2006-02-25 21:52:33 +00:00
|
|
|
if (curwin->w_curswant == MAXCOL)
|
|
|
|
oparg.end_vcol = MAXCOL;
|
2004-06-13 20:20:40 +00:00
|
|
|
/* Swap the start, end vcol if needed */
|
|
|
|
if (oparg.end_vcol < oparg.start_vcol)
|
|
|
|
{
|
|
|
|
oparg.end_vcol += oparg.start_vcol;
|
|
|
|
oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
|
|
|
|
oparg.end_vcol -= oparg.start_vcol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line_count_selected = max_pos.lnum - min_pos.lnum + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
|
|
|
|
{
|
|
|
|
/* Check for a CTRL-C every 100000 characters. */
|
2005-02-07 22:01:03 +00:00
|
|
|
if (byte_count > last_check)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
ui_breakcheck();
|
|
|
|
if (got_int)
|
|
|
|
return;
|
2005-02-07 22:01:03 +00:00
|
|
|
last_check = byte_count + 100000L;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do extra processing for VIsual mode. */
|
|
|
|
if (VIsual_active
|
|
|
|
&& lnum >= min_pos.lnum && lnum <= max_pos.lnum)
|
|
|
|
{
|
2004-12-31 20:58:58 +00:00
|
|
|
char_u *s = NULL;
|
|
|
|
long len = 0L;
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
switch (VIsual_mode)
|
|
|
|
{
|
|
|
|
case Ctrl_V:
|
|
|
|
virtual_op = virtual_active();
|
|
|
|
block_prep(&oparg, &bd, lnum, 0);
|
|
|
|
virtual_op = MAYBE;
|
2004-12-31 20:58:58 +00:00
|
|
|
s = bd.textstart;
|
|
|
|
len = (long)bd.textlen;
|
2004-06-13 20:20:40 +00:00
|
|
|
break;
|
|
|
|
case 'V':
|
2004-12-31 20:58:58 +00:00
|
|
|
s = ml_get(lnum);
|
|
|
|
len = MAXCOL;
|
2004-06-13 20:20:40 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
{
|
|
|
|
colnr_T start_col = (lnum == min_pos.lnum)
|
|
|
|
? min_pos.col : 0;
|
|
|
|
colnr_T end_col = (lnum == max_pos.lnum)
|
|
|
|
? max_pos.col - start_col + 1 : MAXCOL;
|
|
|
|
|
2004-12-31 20:58:58 +00:00
|
|
|
s = ml_get(lnum) + start_col;
|
|
|
|
len = end_col;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-12-31 20:58:58 +00:00
|
|
|
if (s != NULL)
|
|
|
|
{
|
2005-02-07 22:01:03 +00:00
|
|
|
byte_count_cursor += line_count_info(s, &word_count_cursor,
|
|
|
|
&char_count_cursor, len, eol_size);
|
2004-12-31 20:58:58 +00:00
|
|
|
if (lnum == curbuf->b_ml.ml_line_count
|
|
|
|
&& !curbuf->b_p_eol
|
2015-07-17 14:18:08 +02:00
|
|
|
&& (curbuf->b_p_bin || !curbuf->b_p_fixeol)
|
2005-01-02 11:36:03 +00:00
|
|
|
&& (long)STRLEN(s) < len)
|
2005-02-07 22:01:03 +00:00
|
|
|
byte_count_cursor -= eol_size;
|
2004-12-31 20:58:58 +00:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In non-visual mode, check for the line the cursor is on */
|
|
|
|
if (lnum == curwin->w_cursor.lnum)
|
|
|
|
{
|
|
|
|
word_count_cursor += word_count;
|
2005-02-07 22:01:03 +00:00
|
|
|
char_count_cursor += char_count;
|
|
|
|
byte_count_cursor = byte_count +
|
|
|
|
line_count_info(ml_get(lnum),
|
|
|
|
&word_count_cursor, &char_count_cursor,
|
2016-07-01 18:17:26 +02:00
|
|
|
(varnumber_T)(curwin->w_cursor.col + 1),
|
|
|
|
eol_size);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Add to the running totals */
|
2005-02-07 22:01:03 +00:00
|
|
|
byte_count += line_count_info(ml_get(lnum), &word_count,
|
2016-07-01 18:17:26 +02:00
|
|
|
&char_count, (varnumber_T)MAXCOL,
|
|
|
|
eol_size);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Correction for when last line doesn't have an EOL. */
|
2015-07-17 14:18:08 +02:00
|
|
|
if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
|
2005-02-07 22:01:03 +00:00
|
|
|
byte_count -= eol_size;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2016-01-03 22:49:16 +01:00
|
|
|
if (dict == NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-01-03 22:49:16 +01:00
|
|
|
if (VIsual_active)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-01-03 22:49:16 +01:00
|
|
|
if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
|
|
|
|
{
|
|
|
|
getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
|
|
|
|
&max_pos.col);
|
|
|
|
vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
|
|
|
|
(long)(oparg.end_vcol - oparg.start_vcol + 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buf1[0] = NUL;
|
|
|
|
|
|
|
|
if (char_count_cursor == byte_count_cursor
|
|
|
|
&& char_count == byte_count)
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE,
|
2016-07-01 18:17:26 +02:00
|
|
|
_("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
|
2016-01-03 22:49:16 +01:00
|
|
|
buf1, line_count_selected,
|
|
|
|
(long)curbuf->b_ml.ml_line_count,
|
2019-01-17 17:13:30 +01:00
|
|
|
(long_long_T)word_count_cursor,
|
|
|
|
(long_long_T)word_count,
|
|
|
|
(long_long_T)byte_count_cursor,
|
|
|
|
(long_long_T)byte_count);
|
2016-01-03 22:49:16 +01:00
|
|
|
else
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE,
|
2016-07-01 18:17:26 +02:00
|
|
|
_("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"),
|
2016-01-03 22:49:16 +01:00
|
|
|
buf1, line_count_selected,
|
|
|
|
(long)curbuf->b_ml.ml_line_count,
|
2019-01-17 17:13:30 +01:00
|
|
|
(long_long_T)word_count_cursor,
|
|
|
|
(long_long_T)word_count,
|
|
|
|
(long_long_T)char_count_cursor,
|
|
|
|
(long_long_T)char_count,
|
|
|
|
(long_long_T)byte_count_cursor,
|
|
|
|
(long_long_T)byte_count);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
2016-01-03 22:49:16 +01:00
|
|
|
{
|
|
|
|
p = ml_get_curline();
|
|
|
|
validate_virtcol();
|
|
|
|
col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
|
|
|
|
(int)curwin->w_virtcol + 1);
|
|
|
|
col_print(buf2, sizeof(buf2), (int)STRLEN(p),
|
|
|
|
linetabsize(p));
|
|
|
|
|
|
|
|
if (char_count_cursor == byte_count_cursor
|
|
|
|
&& char_count == byte_count)
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE,
|
2016-07-01 18:17:26 +02:00
|
|
|
_("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
|
2016-01-03 22:49:16 +01:00
|
|
|
(char *)buf1, (char *)buf2,
|
|
|
|
(long)curwin->w_cursor.lnum,
|
2004-06-13 20:20:40 +00:00
|
|
|
(long)curbuf->b_ml.ml_line_count,
|
2019-01-17 17:13:30 +01:00
|
|
|
(long_long_T)word_count_cursor, (long_long_T)word_count,
|
|
|
|
(long_long_T)byte_count_cursor, (long_long_T)byte_count);
|
2016-01-03 22:49:16 +01:00
|
|
|
else
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE,
|
2016-07-01 18:17:26 +02:00
|
|
|
_("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
|
2016-01-03 22:49:16 +01:00
|
|
|
(char *)buf1, (char *)buf2,
|
|
|
|
(long)curwin->w_cursor.lnum,
|
2005-02-07 22:01:03 +00:00
|
|
|
(long)curbuf->b_ml.ml_line_count,
|
2019-01-17 17:13:30 +01:00
|
|
|
(long_long_T)word_count_cursor, (long_long_T)word_count,
|
|
|
|
(long_long_T)char_count_cursor, (long_long_T)char_count,
|
|
|
|
(long_long_T)byte_count_cursor, (long_long_T)byte_count);
|
2016-01-03 22:49:16 +01:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-03 22:49:16 +01:00
|
|
|
bom_count = bomb_size();
|
2019-09-13 22:16:21 +02:00
|
|
|
if (dict == NULL && bom_count > 0)
|
2016-01-04 21:43:08 +01:00
|
|
|
vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
|
2019-01-24 15:54:21 +01:00
|
|
|
_("(+%lld for BOM)"), (long_long_T)bom_count);
|
2016-01-03 22:49:16 +01:00
|
|
|
if (dict == NULL)
|
|
|
|
{
|
2019-09-13 22:16:21 +02:00
|
|
|
// Don't shorten this message, the user asked for it.
|
2016-01-03 22:49:16 +01:00
|
|
|
p = p_shm;
|
|
|
|
p_shm = (char_u *)"";
|
2019-01-19 17:43:09 +01:00
|
|
|
msg((char *)IObuff);
|
2016-01-03 22:49:16 +01:00
|
|
|
p_shm = p;
|
|
|
|
}
|
|
|
|
}
|
2016-01-03 22:56:45 +01:00
|
|
|
#if defined(FEAT_EVAL)
|
2016-01-03 22:49:16 +01:00
|
|
|
if (dict != NULL)
|
|
|
|
{
|
2018-07-08 16:50:37 +02:00
|
|
|
dict_add_number(dict, "words", word_count);
|
|
|
|
dict_add_number(dict, "chars", char_count);
|
2019-01-24 15:54:21 +01:00
|
|
|
dict_add_number(dict, "bytes", byte_count + bom_count);
|
2018-07-08 16:50:37 +02:00
|
|
|
dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
|
|
|
|
byte_count_cursor);
|
|
|
|
dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
|
|
|
|
char_count_cursor);
|
|
|
|
dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
|
|
|
|
word_count_cursor);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2016-01-03 22:56:45 +01:00
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|