#include "vim.h"
#define DELETION_REGISTER 36
#ifdef FEAT_CLIPBOARD
# define STAR_REGISTER 37
# ifdef FEAT_X11
# define PLUS_REGISTER 38
# else
# define PLUS_REGISTER STAR_REGISTER
# endif
#endif
#ifdef FEAT_DND
# define TILDE_REGISTER (PLUS_REGISTER + 1)
#endif
#ifdef FEAT_CLIPBOARD
# ifdef FEAT_DND
# define NUM_REGISTERS (TILDE_REGISTER + 1)
# else
# define NUM_REGISTERS (PLUS_REGISTER + 1)
# endif
#else
# define NUM_REGISTERS 37
#endif
typedef struct
{
char_u **y_array;
linenr_T y_size;
char_u y_type;
char_u y_op_change;
colnr_T y_width;
#ifdef FEAT_VIMINFO
time_t y_time_set;
#endif
} yankreg_T;
static yankreg_T y_regs[NUM_REGISTERS];
static yankreg_T *y_current;
static int y_append;
static yankreg_T *y_previous = NULL;
struct block_def
{
int startspaces;
int endspaces;
int textlen;
char_u *textstart;
colnr_T textcol;
colnr_T start_vcol;
colnr_T end_vcol;
#ifdef FEAT_VISUALEXTRA
int is_short;
int is_MAX;
int is_oneChar;
int pre_whitesp;
int pre_whitesp_c;
colnr_T end_char_vcols;
#endif
colnr_T start_char_vcols;
};
#ifdef FEAT_VISUALEXTRA
static void shift_block(oparg_T *oap, int amount);
static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp);
#endif
static int stuff_yank(int, char_u *);
static void put_reedit_in_typebuf(int silent);
static int put_in_typebuf(char_u *s, int esc, int colon,
int silent);
static void stuffescaped(char_u *arg, int literally);
#ifdef FEAT_MBYTE
static void mb_adjust_opend(oparg_T *oap);
#endif
static void free_yank(long);
static void free_yank_all(void);
static int yank_copy_line(struct block_def *bd, long y_idx);
#ifdef FEAT_CLIPBOARD
static void copy_yank_reg(yankreg_T *reg);
static void may_set_selection(void);
#endif
static void dis_msg(char_u *p, int skip_esc);
static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
#endif
static int ends_in_white(linenr_T lnum);
#ifdef FEAT_COMMENTS
static int same_leader(linenr_T lnum, int, char_u *, int, char_u *);
static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
#else
static int fmt_check_par(linenr_T);
#endif
static char opchars[][3] =
{
{NUL, NUL, FALSE},
{'d', NUL, FALSE},
{'y', NUL, FALSE},
{'c', NUL, FALSE},
{'<', NUL, TRUE},
{'>', NUL, TRUE},
{'!', NUL, TRUE},
{'g', '~', FALSE},
{'=', NUL, TRUE},
{'g', 'q', TRUE},
{':', NUL, TRUE},
{'g', 'U', FALSE},
{'g', 'u', FALSE},
{'J', NUL, TRUE},
{'g', 'J', TRUE},
{'g', '?', FALSE},
{'r', NUL, FALSE},
{'I', NUL, FALSE},
{'A', NUL, FALSE},
{'z', 'f', TRUE},
{'z', 'o', TRUE},
{'z', 'O', TRUE},
{'z', 'c', TRUE},
{'z', 'C', TRUE},
{'z', 'd', TRUE},
{'z', 'D', TRUE},
{'g', 'w', TRUE},
{'g', '@', FALSE},
{Ctrl_A, NUL, FALSE},
{Ctrl_X, NUL, FALSE},
};
int
get_op_type(int char1, int char2)
{
int i;
if (char1 == 'r')
return OP_REPLACE;
if (char1 == '~')
return OP_TILDE;
if (char1 == 'g' && char2 == Ctrl_A)
return OP_NR_ADD;
if (char1 == 'g' && char2 == Ctrl_X)
return OP_NR_SUB;
for (i = 0; ; ++i)
if (opchars[i][0] == char1 && opchars[i][1] == char2)
break;
return i;
}
int
op_on_lines(int op)
{
return opchars[op][2];
}
int
get_op_char(int optype)
{
return opchars[optype][0];
}
int
get_extra_op_char(int optype)
{
return opchars[optype][1];
}
void
op_shift(oparg_T *oap, int curs_top, int amount)
{
long i;
int first_char;
char_u *s;
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)
curwin->w_cursor.col = 0;
#ifdef FEAT_VISUALEXTRA
else if (oap->block_mode)
shift_block(oap, amount);
#endif
else
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
if (first_char != '#' || !preprocs_left())
#endif
{
shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
}
++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;
}
else if (curs_top)
{
curwin->w_cursor.lnum = oap->start.lnum;
beginline(BL_SOL | BL_FIX);
}
else
--curwin->w_cursor.lnum;
#ifdef FEAT_FOLDING
foldOpenCursor();
#endif
if (oap->line_count > p_report)
{
if (oap->op_type == OP_RSHIFT)
s = (char_u *)">";
else
s = (char_u *)"<";
if (oap->line_count == 1)
{
if (amount == 1)
sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
else
sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
}
else
{
if (amount == 1)
sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
oap->line_count, s);
else
sprintf((char *)IObuff, _("%ld lines %sed %d times"),
oap->line_count, s, amount);
}
msg(IObuff);
}
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;
}
void
shift_line(
int left,
int round,
int amount,
int call_changed_bytes)
{
int count;
int i, j;
int p_sw = (int)get_sw_value(curbuf);
count = get_indent();
if (round)
{
i = count / p_sw;
j = count % p_sw;
if (j && left)
--amount;
if (left)
{
i -= amount;
if (i < 0)
i = 0;
}
else
i += amount;
count = i * p_sw;
}
else
{
if (left)
{
count -= p_sw * amount;
if (count < 0)
count = 0;
}
else
count += p_sw * amount;
}
#ifdef FEAT_VREPLACE
if (State & VREPLACE_FLAG)
change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
else
#endif
(void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
}
#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
static void
shift_block(oparg_T *oap, int amount)
{
int left = (oap->op_type == OP_LSHIFT);
int oldstate = State;
int total;
char_u *newp, *oldp;
int oldcol = curwin->w_cursor.col;
int p_sw = (int)get_sw_value(curbuf);
int p_ts = (int)curbuf->b_p_ts;
struct block_def bd;
int incr;
colnr_T ws_vcol;
int i = 0, j = 0;
int len;
#ifdef FEAT_RIGHTLEFT
int old_p_ri = p_ri;
p_ri = 0;
#endif
State = INSERT;
block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
if (bd.is_short)
return;
total = (int)((unsigned)amount * (unsigned)p_sw);
if ((total / p_sw) != amount)
return;
oldp = ml_get_curline();
if (!left)
{
total += bd.pre_whitesp;
ws_vcol = bd.start_vcol - bd.pre_whitesp;
if (bd.startspaces)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
if ((*mb_ptr2len)(bd.textstart) == 1)
++bd.textstart;
else
{
ws_vcol = 0;
bd.startspaces = 0;
}
}
else
#endif
++bd.textstart;
}
for ( ; VIM_ISWHITE(*bd.textstart); )
{
incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
(colnr_T)(bd.start_vcol));
total += incr;
bd.start_vcol += incr;
}
if (!curbuf->b_p_et)
i = ((ws_vcol % p_ts) + total) / p_ts;
if (i)
j = ((ws_vcol % p_ts) + total) % p_ts;
else
j = total;
bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
len = (int)STRLEN(bd.textstart) + 1;
newp = alloc_check((unsigned)(bd.textcol + i + j + len));
if (newp == NULL)
return;
vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
mch_memmove(newp, oldp, (size_t)bd.textcol);
vim_memset(newp + bd.textcol, TAB, (size_t)i);
vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
}
else
{
colnr_T destination_col;
char_u *verbatim_copy_end;
colnr_T verbatim_copy_width;
unsigned fill;
unsigned new_line_len;
size_t block_space_width;
size_t shift_amount;
char_u *non_white = bd.textstart;
colnr_T non_white_col;
if (bd.startspaces)
MB_PTR_ADV(non_white);
non_white_col = bd.start_vcol;
while (VIM_ISWHITE(*non_white))
{
incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
non_white_col += incr;
}
block_space_width = non_white_col - oap->start_vcol;
shift_amount = (block_space_width < (size_t)total
? block_space_width : (size_t)total);
destination_col = (colnr_T)(non_white_col - shift_amount);
verbatim_copy_end = bd.textstart;
verbatim_copy_width = bd.start_vcol;
if (bd.startspaces)
verbatim_copy_width -= bd.start_char_vcols;
while (verbatim_copy_width < destination_col)
{
char_u *line = verbatim_copy_end;
incr = lbr_chartabsize(line, verbatim_copy_end,
verbatim_copy_width);
if (verbatim_copy_width + incr > destination_col)
break;
verbatim_copy_width += incr;
MB_PTR_ADV(verbatim_copy_end);
}
fill = destination_col - verbatim_copy_width;
new_line_len = (unsigned)(verbatim_copy_end - oldp)
+ fill
+ (unsigned)STRLEN(non_white) + 1;
newp = alloc_check(new_line_len);
if (newp == NULL)
return;
mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
}
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
}
#endif
#ifdef FEAT_VISUALEXTRA
static void
block_insert(
oparg_T *oap,
char_u *s,
int b_insert,
struct block_def *bdp)
{
int p_ts;
int count = 0;
int spaces = 0;
colnr_T offset;
unsigned s_len;
char_u *newp, *oldp;
linenr_T lnum;
int oldstate = State;
State = INSERT;
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;
oldp = ml_get(lnum);
if (b_insert)
{
p_ts = bdp->start_char_vcols;
spaces = bdp->startspaces;
if (spaces != 0)
count = p_ts - 1;
offset = bdp->textcol;
}
else
{
p_ts = bdp->end_char_vcols;
if (!bdp->is_short)
{
spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
if (spaces != 0)
count = p_ts - 1;
offset = bdp->textcol + bdp->textlen - (spaces != 0);
}
else
{
if (!bdp->is_MAX)
spaces = (oap->end_vcol - bdp->end_vcol) + 1;
count = spaces;
offset = bdp->textcol + bdp->textlen;
}
}
#ifdef FEAT_MBYTE
if (has_mbyte && spaces > 0)
{
int off;
if (b_insert)
{
off = (*mb_head_off)(oldp, oldp + offset + spaces);
}
else
{
off = (*mb_off_next)(oldp, oldp + offset);
offset += off;
}
spaces -= off;
count -= off;
}
#endif
newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
if (newp == NULL)
continue;
mch_memmove(newp, oldp, (size_t)(offset));
oldp += offset;
vim_memset(newp + offset, ' ', (size_t)spaces);
mch_memmove(newp + offset + spaces, s, (size_t)s_len);
offset += s_len;
if (spaces && !bdp->is_short)
{
vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces));
oldp++;
count++;
}
if (spaces > 0)
offset += count;
STRMOVE(newp + offset, oldp);
ml_replace(lnum, newp, FALSE);
if (lnum == oap->end.lnum)
{
curbuf->b_op_end.lnum = oap->end.lnum;
curbuf->b_op_end.col = offset;
}
}
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
State = oldstate;
}
#endif
#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
void
op_reindent(oparg_T *oap, int (*how)(void))
{
long i;
char_u *l;
int amount;
linenr_T first_changed = 0;
linenr_T last_changed = 0;
linenr_T start_lnum = curwin->w_cursor.lnum;
if (!curbuf->b_p_ma)
{
EMSG(_(e_modifiable));
return;
}
for (i = oap->line_count; --i >= 0 && !got_int; )
{
if (i > 1
&& (i % 50 == 0 || i == oap->line_count - 1)
&& oap->line_count > p_report)
smsg((char_u *)_("%ld lines to indent... "), i);
#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)
amount = 0;
else
amount = how();
if (amount >= 0 && set_indent(amount, SIN_UNDO))
{
if (first_changed == 0)
first_changed = curwin->w_cursor.lnum;
last_changed = curwin->w_cursor.lnum;
}
}
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
}
curwin->w_cursor.lnum = start_lnum;
beginline(BL_SOL | BL_FIX);
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);
if (i == 1)
MSG(_("1 line indented "));
else
smsg((char_u *)_("%ld lines indented "), i);
}
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
}
#endif
#if defined(FEAT_EVAL) || defined(PROTO)
static char_u *expr_line = NULL;
int
get_expr_register(void)
{
char_u *new_line;
new_line = getcmdline('=', 0L, 0);
if (new_line == NULL)
return NUL;
if (*new_line == NUL)
vim_free(new_line);
else
set_expr_line(new_line);
return '=';
}
void
set_expr_line(char_u *new_line)
{
vim_free(expr_line);
expr_line = new_line;
}
char_u *
get_expr_line(void)
{
char_u *expr_copy;
char_u *rv;
static int nested = 0;
if (expr_line == NULL)
return NULL;
expr_copy = vim_strsave(expr_line);
if (expr_copy == NULL)
return NULL;
if (nested >= 10)
return expr_copy;
++nested;
rv = eval_to_string(expr_copy, NULL, TRUE);
--nested;
vim_free(expr_copy);
return rv;
}
char_u *
get_expr_line_src(void)
{
if (expr_line == NULL)
return NULL;
return vim_strsave(expr_line);
}
#endif
int
valid_yank_reg(
int regname,
int writing)
{
if ( (regname > 0 && ASCII_ISALNUM(regname))
|| (!writing && vim_strchr((char_u *)
#ifdef FEAT_EVAL
"/.%:="
#else
"/.%:"
#endif
, regname) != NULL)
|| regname == '#'
|| regname == '"'
|| regname == '-'
|| regname == '_'
#ifdef FEAT_CLIPBOARD
|| regname == '*'
|| regname == '+'
#endif
#ifdef FEAT_DND
|| (!writing && regname == '~')
#endif
)
return TRUE;
return FALSE;
}
void
get_yank_register(int regname, int writing)
{
int i;
y_append = FALSE;
if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
{
y_current = y_previous;
return;
}
i = regname;
if (VIM_ISDIGIT(i))
i -= '0';
else if (ASCII_ISLOWER(i))
i = CharOrdLow(i) + 10;
else if (ASCII_ISUPPER(i))
{
i = CharOrdUp(i) + 10;
y_append = TRUE;
}
else if (regname == '-')
i = DELETION_REGISTER;
#ifdef FEAT_CLIPBOARD
else if (clip_star.available && regname == '*')
i = STAR_REGISTER;
else if (clip_plus.available && regname == '+')
i = PLUS_REGISTER;
#endif
#ifdef FEAT_DND
else if (!writing && regname == '~')
i = TILDE_REGISTER;
#endif
else
i = 0;
y_current = &(y_regs[i]);
if (writing)
y_previous = y_current;
}
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
int
may_get_selection(int regname)
{
if (regname == '*')
{
if (!clip_star.available)
regname = 0;
else
clip_get_selection(&clip_star);
}
else if (regname == '+')
{
if (!clip_plus.available)
regname = 0;
else
clip_get_selection(&clip_plus);
}
return regname;
}
#endif
void *
get_register(
int name,
int copy)
{
yankreg_T *reg;
int i;
#ifdef FEAT_CLIPBOARD
if (name == '*' && clip_star.available)
{
if (clip_isautosel_star())
clip_update_selection(&clip_star);
may_get_selection(name);
}
if (name == '+' && clip_plus.available)
{
if (clip_isautosel_plus())
clip_update_selection(&clip_plus);
may_get_selection(name);
}
#endif
get_yank_register(name, 0);
reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T));
if (reg != NULL)
{
*reg = *y_current;
if (copy)
{
if (reg->y_size == 0)
reg->y_array = NULL;
else
reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
* reg->y_size));
if (reg->y_array != NULL)
{
for (i = 0; i < reg->y_size; ++i)
reg->y_array[i] = vim_strsave(y_current->y_array[i]);
}
}
else
y_current->y_array = NULL;
}
return (void *)reg;
}
void
put_register(int name, void *reg)
{
get_yank_register(name, 0);
free_yank_all();
*y_current = *(yankreg_T *)reg;
vim_free(reg);
#ifdef FEAT_CLIPBOARD
may_set_selection();
#endif
}
void
free_register(void *reg)
{
yankreg_T tmp;
tmp = *y_current;
*y_current = *(yankreg_T *)reg;
free_yank_all();
vim_free(reg);
*y_current = tmp;
}
#if defined(FEAT_MOUSE) || defined(PROTO)
int
yank_register_mline(int regname)
{
if (regname != 0 && !valid_yank_reg(regname, FALSE))
return FALSE;
if (regname == '_')
return FALSE;
get_yank_register(regname, FALSE);
return (y_current->y_type == MLINE);
}
#endif
int
do_record(int c)
{
char_u *p;
static int regname;
yankreg_T *old_y_previous, *old_y_current;
int retval;
if (Recording == FALSE)
{
if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
retval = FAIL;
else
{
Recording = c;
showmode();
regname = c;
retval = OK;
}
}
else
{
Recording = FALSE;
MSG("");
p = get_recorded();
if (p == NULL)
retval = FAIL;
else
{
vim_unescape_csi(p);
old_y_previous = y_previous;
old_y_current = y_current;
retval = stuff_yank(regname, p);
y_previous = old_y_previous;
y_current = old_y_current;
}
}
return retval;
}
static int
stuff_yank(int regname, char_u *p)
{
char_u *lp;
char_u **pp;
if (regname != 0 && !valid_yank_reg(regname, TRUE))
{
vim_free(p);
return FAIL;
}
if (regname == '_')
{
vim_free(p);
return OK;
}
get_yank_register(regname, TRUE);
if (y_append && y_current->y_array != NULL)
{
pp = &(y_current->y_array[y_current->y_size - 1]);
lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
if (lp == NULL)
{
vim_free(p);
return FAIL;
}
STRCPY(lp, *pp);
STRCAT(lp, p);
vim_free(p);
vim_free(*pp);
*pp = lp;
}
else
{
free_yank_all();
if ((y_current->y_array =
(char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
{
vim_free(p);
return FAIL;
}
y_current->y_array[0] = p;
y_current->y_size = 1;
y_current->y_type = MCHAR;
y_current->y_op_change = 0;
#ifdef FEAT_VIMINFO
y_current->y_time_set = vim_time();
#endif
}
return OK;
}
static int execreg_lastc = NUL;
int
do_execreg(
int regname,
int colon,
int addcr,
int silent)
{
long i;
char_u *p;
int retval = OK;
int remap;
if (regname == '@')
{
if (execreg_lastc == NUL)
{
EMSG(_("E748: No previously used register"));
return FAIL;
}
regname = execreg_lastc;
}
if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
{
emsg_invreg(regname);
return FAIL;
}
execreg_lastc = regname;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (regname == '_')
return OK;
#ifdef FEAT_CMDHIST
if (regname == ':')
{
if (last_cmdline == NULL)
{
EMSG(_(e_nolastcmd));
return FAIL;
}
vim_free(new_last_cmdline);
new_last_cmdline = NULL;
p = vim_strsave_escaped_ext(last_cmdline,
(char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
if (p != NULL)
{
if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
else
retval = put_in_typebuf(p, TRUE, TRUE, silent);
}
vim_free(p);
}
#endif
#ifdef FEAT_EVAL
else if (regname == '=')
{
p = get_expr_line();
if (p == NULL)
return FAIL;
retval = put_in_typebuf(p, TRUE, colon, silent);
vim_free(p);
}
#endif
else if (regname == '.')
{
p = get_last_insert_save();
if (p == NULL)
{
EMSG(_(e_noinstext));
return FAIL;
}
retval = put_in_typebuf(p, FALSE, colon, silent);
vim_free(p);
}
else
{
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
return FAIL;
remap = colon ? REMAP_NONE : REMAP_YES;
put_reedit_in_typebuf(silent);
for (i = y_current->y_size; --i >= 0; )
{
char_u *escaped;
if (y_current->y_type == MLINE || i < y_current->y_size - 1
|| addcr)
{
if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
return FAIL;
}
escaped = vim_strsave_escape_csi(y_current->y_array[i]);
if (escaped == NULL)
return FAIL;
retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
vim_free(escaped);
if (retval == FAIL)
return FAIL;
if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
== FAIL)
return FAIL;
}
Exec_reg = TRUE;
}
return retval;
}
static void
put_reedit_in_typebuf(int silent)
{
char_u buf[3];
if (restart_edit != NUL)
{
if (restart_edit == 'V')
{
buf[0] = 'g';
buf[1] = 'R';
buf[2] = NUL;
}
else
{
buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
buf[1] = NUL;
}
if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
restart_edit = NUL;
}
}
static int
put_in_typebuf(
char_u *s,
int esc,
int colon,
int silent)
{
int retval = OK;
put_reedit_in_typebuf(silent);
if (colon)
retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
if (retval == OK)
{
char_u *p;
if (esc)
p = vim_strsave_escape_csi(s);
else
p = s;
if (p == NULL)
retval = FAIL;
else
retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
0, TRUE, silent);
if (esc)
vim_free(p);
}
if (colon && retval == OK)
retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
return retval;
}
int
insert_reg(
int regname,
int literally)
{
long i;
int retval = OK;
char_u *arg;
int allocated;
ui_breakcheck();
if (got_int)
return FAIL;
if (regname != NUL && !valid_yank_reg(regname, FALSE))
return FAIL;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (regname == '.')
retval = stuff_inserted(NUL, 1L, TRUE);
else if (get_spec_reg(regname, &arg, &allocated, TRUE))
{
if (arg == NULL)
return FAIL;
stuffescaped(arg, literally);
if (allocated)
vim_free(arg);
}
else
{
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
retval = FAIL;
else
{
for (i = 0; i < y_current->y_size; ++i)
{
stuffescaped(y_current->y_array[i], literally);
if (y_current->y_type == MLINE || i < y_current->y_size - 1)
stuffcharReadbuff('\n');
}
}
}
return retval;
}
static void
stuffescaped(char_u *arg, int literally)
{
int c;
char_u *start;
while (*arg != NUL)
{
start = arg;
while ((*arg >= ' '
#ifndef EBCDIC
&& *arg < DEL
#endif
)
|| (*arg == K_SPECIAL && !literally))
++arg;
if (arg > start)
stuffReadbuffLen(start, (long)(arg - start));
if (*arg != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
c = mb_cptr2char_adv(&arg);
else
#endif
c = *arg++;
if (literally && ((c < ' ' && c != TAB) || c == DEL))
stuffcharReadbuff(Ctrl_V);
stuffcharReadbuff(c);
}
}
}
int
get_spec_reg(
int regname,
char_u **argp,
int *allocated,
int errmsg)
{
int cnt;
*argp = NULL;
*allocated = FALSE;
switch (regname)
{
case '%':
if (errmsg)
check_fname();
*argp = curbuf->b_fname;
return TRUE;
case '#':
*argp = getaltfname(errmsg);
return TRUE;
#ifdef FEAT_EVAL
case '=':
*argp = get_expr_line();
*allocated = TRUE;
return TRUE;
#endif
case ':':
if (last_cmdline == NULL && errmsg)
EMSG(_(e_nolastcmd));
*argp = last_cmdline;
return TRUE;
case '/':
if (last_search_pat() == NULL && errmsg)
EMSG(_(e_noprevre));
*argp = last_search_pat();
return TRUE;
case '.':
*argp = get_last_insert_save();
*allocated = TRUE;
if (*argp == NULL && errmsg)
EMSG(_(e_noinstext));
return TRUE;
#ifdef FEAT_SEARCHPATH
case Ctrl_F:
case Ctrl_P:
if (!errmsg)
return FALSE;
*argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
| (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
*allocated = TRUE;
return TRUE;
#endif
case Ctrl_W:
case Ctrl_A:
if (!errmsg)
return FALSE;
cnt = find_ident_under_cursor(argp, regname == Ctrl_W
? (FIND_IDENT|FIND_STRING) : FIND_STRING);
*argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
*allocated = TRUE;
return TRUE;
case '_':
*argp = (char_u *)"";
return TRUE;
}
return FALSE;
}
int
cmdline_paste_reg(
int regname,
int literally,
int remcr)
{
long i;
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
return FAIL;
for (i = 0; i < y_current->y_size; ++i)
{
cmdline_paste_str(y_current->y_array[i], literally);
if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
cmdline_paste_str((char_u *)"\r", literally);
ui_breakcheck();
if (got_int)
return FAIL;
}
return OK;
}
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
void
adjust_clip_reg(int *rp)
{
if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
{
if (clip_unnamed != 0)
*rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
? '+' : '*';
else
*rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
? '+' : '*';
}
if (!clip_star.available && *rp == '*')
*rp = 0;
if (!clip_plus.available && *rp == '+')
*rp = 0;
}
#endif
void
shift_delete_registers()
{
int n;
y_current = &y_regs[9];
free_yank_all();
for (n = 9; n > 1; --n)
y_regs[n] = y_regs[n - 1];
y_previous = y_current = &y_regs[1];
y_regs[1].y_array = NULL;
}
int
op_delete(oparg_T *oap)
{
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;
int orig_regname = oap->regname;
if (curbuf->b_ml.ml_flags & ML_EMPTY)
return OK;
if (oap->empty)
return u_save_cursor();
if (!curbuf->b_p_ma)
{
EMSG(_(e_modifiable));
return FAIL;
}
#ifdef FEAT_CLIPBOARD
adjust_clip_reg(&oap->regname);
#endif
#ifdef FEAT_MBYTE
if (has_mbyte)
mb_adjust_opend(oap);
#endif
if ( oap->motion_type == MCHAR
&& !oap->is_VIsual
&& !oap->block_mode
&& oap->line_count > 1
&& oap->motion_force == NUL
&& oap->op_type == OP_DELETE)
{
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;
}
if ( oap->motion_type == MCHAR
&& oap->line_count == 1
&& oap->op_type == OP_DELETE
&& *ml_get(oap->start.lnum) == NUL)
{
#ifdef FEAT_VIRTUALEDIT
if (virtual_op)
goto setmarks;
#endif
if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
beep_flush();
return OK;
}
if (oap->regname != '_')
{
if (oap->regname != 0)
{
if (!valid_yank_reg(oap->regname, TRUE))
{
beep_flush();
return OK;
}
get_yank_register(oap->regname, TRUE);
if (op_yank(oap, TRUE, FALSE) == OK)
did_yank = TRUE;
}
if (orig_regname != 0 || 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;
}
if ((
#ifdef FEAT_CLIPBOARD
((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
#endif
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;
}
if (!did_yank)
{
int msg_silent_save = msg_silent;
msg_silent = 0;
n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
msg_silent = msg_silent_save;
if (n != 'y')
{
EMSG(_(e_abort));
return FAIL;
}
}
}
if (oap->block_mode)
{
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return FAIL;
for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
{
block_prep(oap, &bd, lnum, TRUE);
if (bd.textlen == 0)
continue;
if (lnum == curwin->w_cursor.lnum)
{
curwin->w_cursor.col = bd.textcol + bd.startspaces;
# ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
# endif
}
n = bd.textlen - bd.startspaces - bd.endspaces;
oldp = ml_get(lnum);
newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
if (newp == NULL)
continue;
mch_memmove(newp, oldp, (size_t)bd.textcol);
vim_memset(newp + bd.textcol, ' ',
(size_t)(bd.startspaces + bd.endspaces));
oldp += bd.textcol + bd.textlen;
STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
ml_replace(lnum, newp, FALSE);
}
check_cursor_col();
changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
oap->end.lnum + 1, 0L);
oap->line_count = 0;
}
else if (oap->motion_type == MLINE)
{
if (oap->op_type == OP_CHANGE)
{
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)
{
beginline(BL_WHITE);
did_ai = TRUE;
ai_col = curwin->w_cursor.col;
}
else
beginline(0);
truncate_line(FALSE);
if (oap->line_count > 1)
u_clearline();
}
else
{
del_lines(oap->line_count, TRUE);
beginline(BL_WHITE | BL_FIX);
u_clearline();
}
}
else
{
#ifdef FEAT_VIRTUALEDIT
if (virtual_op)
{
int endcol = 0;
if (gchar_pos(&oap->start) == '\t')
{
if (u_save_cursor() == FAIL)
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;
}
}
if (gchar_pos(&oap->end) == '\t'
&& (int)oap->end.coladd < oap->inclusive)
{
if (u_save((linenr_T)(oap->end.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return FAIL;
curwin->w_cursor = oap->end;
coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
oap->end = curwin->w_cursor;
curwin->w_cursor = oap->start;
}
}
#endif
if (oap->line_count == 1)
{
if (u_save_cursor() == FAIL)
return FAIL;
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);
n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
#ifdef FEAT_VIRTUALEDIT
if (virtual_op)
{
char_u *curline = ml_get_curline();
int len = (int)STRLEN(curline);
if (oap->end.coladd != 0
&& (int)oap->end.col >= len - 1
&& !(oap->start.coladd && (int)oap->end.col >= len - 1))
n++;
if (n == 0 && oap->start.coladd != oap->end.coladd)
n = 1;
if (gchar_cursor() != NUL)
curwin->w_cursor.coladd = 0;
}
#endif
(void)del_bytes((long)n, !virtual_op,
oap->op_type == OP_DELETE && !oap->is_VIsual);
}
else
{
pos_T curpos;
if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
(linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
return FAIL;
truncate_line(TRUE);
curpos = curwin->w_cursor;
++curwin->w_cursor.lnum;
del_lines((long)(oap->line_count - 2), FALSE);
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;
(void)do_join(2, FALSE, FALSE, FALSE, FALSE);
}
}
msgmore(curbuf->b_ml.ml_line_count - old_lcount);
#ifdef FEAT_VIRTUALEDIT
setmarks:
#endif
if (oap->block_mode)
{
curbuf->b_op_end.lnum = oap->end.lnum;
curbuf->b_op_end.col = oap->start.col;
}
else
curbuf->b_op_end = oap->start;
curbuf->b_op_start = oap->start;
return OK;
}
#ifdef FEAT_MBYTE
static void
mb_adjust_opend(oparg_T *oap)
{
char_u *p;
if (oap->inclusive)
{
p = ml_get(oap->end.lnum);
oap->end.col += mb_tail_off(p, p + oap->end.col);
}
}
#endif
#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
int
op_replace(oparg_T *oap, int c)
{
int n, numc;
#ifdef FEAT_MBYTE
int num_chars;
#endif
char_u *newp, *oldp;
size_t oldlen;
struct block_def bd;
char_u *after_p = NULL;
int had_ctrl_v_cr = (c == -1 || c == -2);
if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
return OK;
if (had_ctrl_v_cr)
c = (c == -1 ? '\r' : '\n');
#ifdef FEAT_MBYTE
if (has_mbyte)
mb_adjust_opend(oap);
#endif
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return FAIL;
if (oap->block_mode)
{
bd.is_MAX = (curwin->w_curswant == MAXCOL);
for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
{
curwin->w_cursor.col = 0;
block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
continue;
#ifdef FEAT_VIRTUALEDIT
if (virtual_op && bd.is_short && *bd.textstart == NUL)
{
pos_T vpos;
vpos.lnum = curwin->w_cursor.lnum;
getvpos(&vpos, oap->start_vcol);
bd.startspaces += vpos.coladd;
n = bd.startspaces;
}
else
#endif
n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
n += (bd.endspaces
#ifdef FEAT_VIRTUALEDIT
&& !bd.is_oneChar
#endif
&& bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
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;
#ifdef FEAT_MBYTE
if ((*mb_char2cells)(c) > 1)
{
if ((numc & 1) && !bd.is_short)
{
++bd.endspaces;
++n;
}
numc = numc / 2;
}
num_chars = numc;
numc *= (*mb_char2len)(c);
#endif
n += numc - bd.textlen;
oldp = ml_get_curline();
oldlen = STRLEN(oldp);
newp = alloc_check((unsigned)oldlen + 1 + n);
if (newp == NULL)
continue;
vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
mch_memmove(newp, oldp, (size_t)bd.textcol);
oldp += bd.textcol + bd.textlen;
vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = (int)STRLEN(newp);
while (--num_chars >= 0)
n += (*mb_char2bytes)(c, newp + n);
}
else
#endif
vim_memset(newp + STRLEN(newp), c, (size_t)numc);
if (!bd.is_short)
{
vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
STRMOVE(newp + STRLEN(newp), oldp);
}
}
else
{
after_p = alloc_check(
(unsigned)(oldlen + 1 + n - STRLEN(newp)));
if (after_p != NULL)
STRMOVE(after_p, oldp);
}
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);
}
}
}
else
{
if (oap->motion_type == MLINE)
{
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));
while (LTOREQ_POS(curwin->w_cursor, oap->end))
{
n = gchar_cursor();
if (n != NUL)
{
#ifdef FEAT_MBYTE
if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
{
if (curwin->w_cursor.lnum == oap->end.lnum)
oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
n = State;
State = REPLACE;
ins_char(c);
State = n;
dec_cursor();
}
else
#endif
{
#ifdef FEAT_VIRTUALEDIT
if (n == TAB)
{
int end_vcol = 0;
if (curwin->w_cursor.lnum == oap->end.lnum)
{
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);
}
#endif
PCHAR(curwin->w_cursor, c);
}
}
#ifdef FEAT_VIRTUALEDIT
else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
{
int virtcols = oap->end.coladd;
if (curwin->w_cursor.lnum == oap->start.lnum
&& oap->start.col == oap->end.col && oap->start.coladd)
virtcols -= oap->start.coladd;
coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
curwin->w_cursor.col -= (virtcols + 1);
for (; virtcols >= 0; virtcols--)
{
PCHAR(curwin->w_cursor, c);
if (inc(&curwin->w_cursor) == -1)
break;
}
}
#endif
if (inc_cursor() == -1)
break;
}
}
curwin->w_cursor = oap->start;
check_cursor();
changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
return OK;
}
#endif
static int swapchars(int op_type, pos_T *pos, int length);
void
op_tilde(oparg_T *oap)
{
pos_T pos;
struct block_def bd;
int did_change = FALSE;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
pos = oap->start;
if (oap->block_mode)
{
for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
{
int one_change;
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)
{
char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
netbeans_removed(curbuf, pos.lnum, bd.textcol,
(long)bd.textlen);
netbeans_inserted(curbuf, pos.lnum, bd.textcol,
&ptr[bd.textcol], bd.textlen);
}
#endif
}
if (did_change)
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
}
else
{
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));
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)));
if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
break;
}
if (did_change)
{
changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
0L);
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active() && did_change)
{
char_u *ptr;
int count;
pos = oap->start;
while (pos.lnum < oap->end.lnum)
{
ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
count = (int)STRLEN(ptr) - pos.col;
netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
netbeans_inserted(curbuf, pos.lnum, pos.col,
&ptr[pos.col], count);
pos.col = 0;
pos.lnum++;
}
ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
count = oap->end.col - pos.col + 1;
netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
netbeans_inserted(curbuf, pos.lnum, pos.col,
&ptr[pos.col], count);
}
#endif
}
}
if (!did_change && oap->is_VIsual)
redraw_curbuf_later(INVERTED);
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
if (oap->line_count > p_report)
{
if (oap->line_count == 1)
MSG(_("1 line changed"));
else
smsg((char_u *)_("%ld lines changed"), oap->line_count);
}
}
static int
swapchars(int op_type, pos_T *pos, int length)
{
int todo;
int did_change = 0;
for (todo = length; todo > 0; --todo)
{
# ifdef FEAT_MBYTE
if (has_mbyte)
{
int len = (*mb_ptr2len)(ml_get_pos(pos));
if (len > 0)
todo -= len - 1;
}
# endif
did_change |= swapchar(op_type, pos);
if (inc(pos) == -1)
break;
}
return did_change;
}
int
swapchar(int op_type, pos_T *pos)
{
int c;
int nc;
c = gchar_pos(pos);
if (c >= 0x80 && op_type == OP_ROT13)
return FALSE;
#ifdef FEAT_MBYTE
if (op_type == OP_UPPER && c == 0xdf
&& (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
{
pos_T sp = curwin->w_cursor;
curwin->w_cursor = *pos;
del_char(FALSE);
ins_char('S');
ins_char('S');
curwin->w_cursor = sp;
inc(pos);
}
if (enc_dbcs != 0 && c >= 0x100)
return FALSE;
#endif
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)
{
#ifdef FEAT_MBYTE
if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
{
pos_T sp = curwin->w_cursor;
curwin->w_cursor = *pos;
del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
ins_char(nc);
curwin->w_cursor = sp;
}
else
#endif
PCHAR(*pos, nc);
return TRUE;
}
return FALSE;
}
#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
void
op_insert(oparg_T *oap, long count1)
{
long ins_len, pre_textlen = 0;
char_u *firstline, *ins_text;
colnr_T ind_pre = 0, ind_post;
struct block_def bd;
int i;
pos_T t1;
bd.is_MAX = (curwin->w_curswant == MAXCOL);
curwin->w_cursor.lnum = oap->start.lnum;
update_screen(INVERTED);
if (oap->block_mode)
{
#ifdef FEAT_VIRTUALEDIT
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;
}
#endif
block_prep(oap, &bd, oap->start.lnum, TRUE);
ind_pre = (colnr_T)getwhitecols_curline();
firstline = ml_get(oap->start.lnum) + bd.textcol;
if (oap->op_type == OP_APPEND)
firstline += bd.textlen;
pre_textlen = (long)STRLEN(firstline);
}
if (oap->op_type == OP_APPEND)
{
if (oap->block_mode
#ifdef FEAT_VIRTUALEDIT
&& curwin->w_cursor.coladd == 0
#endif
)
{
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)
{
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;
check_cursor_col();
if (!LINEEMPTY(curwin->w_cursor.lnum)
&& oap->start_vcol != oap->end_vcol)
inc_cursor();
}
}
t1 = oap->start;
(void)edit(NUL, FALSE, (linenr_T)count1);
if (t1.lnum == curbuf->b_op_start_orig.lnum
&& LT_POS(curbuf->b_op_start_orig, t1))
oap->start = curbuf->b_op_start_orig;
if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
return;
if (oap->block_mode)
{
struct block_def bd2;
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;
}
if (oap->start.lnum == curbuf->b_op_start_orig.lnum && !bd.is_MAX)
{
if (oap->op_type == OP_INSERT
&& oap->start.col
#ifdef FEAT_VIRTUALEDIT
+ oap->start.coladd
#endif
!= curbuf->b_op_start_orig.col
#ifdef FEAT_VIRTUALEDIT
+ curbuf->b_op_start_orig.coladd
#endif
)
{
int t = getviscol2(curbuf->b_op_start_orig.col,
curbuf->b_op_start_orig.coladd);
oap->start.col = curbuf->b_op_start_orig.col;
pre_textlen -= t - oap->start_vcol;
oap->start_vcol = t;
}
else if (oap->op_type == OP_APPEND
&& oap->end.col
#ifdef FEAT_VIRTUALEDIT
+ oap->end.coladd
#endif
>= curbuf->b_op_start_orig.col
#ifdef FEAT_VIRTUALEDIT
+ curbuf->b_op_start_orig.coladd
#endif
)
{
int t = getviscol2(curbuf->b_op_start_orig.col,
curbuf->b_op_start_orig.coladd);
oap->start.col = curbuf->b_op_start_orig.col;
pre_textlen += bd.textlen;
pre_textlen -= t - oap->start_vcol;
oap->start_vcol = t;
oap->op_type = OP_INSERT;
}
}
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;
}
firstline = ml_get(oap->start.lnum) + bd.textcol;
if (oap->op_type == OP_APPEND)
firstline += bd.textlen;
if (pre_textlen >= 0
&& (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
{
ins_text = vim_strnsave(firstline, (int)ins_len);
if (ins_text != NULL)
{
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);
}
}
}
}
#endif
int
op_change(oparg_T *oap)
{
colnr_T l;
int retval;
#ifdef FEAT_VISUALEXTRA
long offset;
linenr_T linenr;
long ins_len;
long pre_textlen = 0;
long pre_indent = 0;
char_u *firstline;
char_u *ins_text, *newp, *oldp;
struct block_def bd;
#endif
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;
#endif
}
if (curbuf->b_ml.ml_flags & ML_EMPTY)
{
if (u_save_cursor() == FAIL)
return FALSE;
}
else if (op_delete(oap) == FAIL)
return FALSE;
if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
&& !virtual_op)
inc_cursor();
#ifdef FEAT_VISUALEXTRA
if (oap->block_mode)
{
# ifdef FEAT_VIRTUALEDIT
if (virtual_op && (curwin->w_cursor.coladd > 0
|| gchar_cursor() == NUL))
coladvance_force(getviscol());
# endif
firstline = ml_get(oap->start.lnum);
pre_textlen = (long)STRLEN(firstline);
pre_indent = (long)getwhitecols(firstline);
bd.textcol = curwin->w_cursor.col;
}
#endif
#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
if (oap->motion_type == MLINE)
fix_indent();
#endif
retval = edit(NUL, FALSE, (linenr_T)1);
#ifdef FEAT_VISUALEXTRA
if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
{
firstline = ml_get(oap->start.lnum);
if (bd.textcol > (colnr_T)pre_indent)
{
long new_indent = (long)getwhitecols(firstline);
pre_textlen += new_indent - pre_indent;
bd.textcol += new_indent - pre_indent;
}
ins_len = (long)STRLEN(firstline) - pre_textlen;
if (ins_len > 0)
{
if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
{
vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
linenr++)
{
block_prep(oap, &bd, linenr, TRUE);
if (!bd.is_short || virtual_op)
{
# ifdef FEAT_VIRTUALEDIT
pos_T vpos;
if (bd.is_short)
{
vpos.lnum = linenr;
(void)getvpos(&vpos, oap->start_vcol);
}
else
vpos.coladd = 0;
# endif
oldp = ml_get(linenr);
newp = alloc_check((unsigned)(STRLEN(oldp)
# ifdef FEAT_VIRTUALEDIT
+ vpos.coladd
# endif
+ ins_len + 1));
if (newp == NULL)
continue;
mch_memmove(newp, oldp, (size_t)bd.textcol);
offset = bd.textcol;
# ifdef FEAT_VIRTUALEDIT
vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
offset += vpos.coladd;
# endif
mch_memmove(newp + offset, ins_text, (size_t)ins_len);
offset += ins_len;
oldp += bd.textcol;
STRMOVE(newp + offset, oldp);
ml_replace(linenr, newp, FALSE);
}
}
check_cursor();
changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
}
vim_free(ins_text);
}
}
#endif
return retval;
}
void
init_yank(void)
{
int i;
for (i = 0; i < NUM_REGISTERS; ++i)
y_regs[i].y_array = NULL;
}
#if defined(EXITFREE) || defined(PROTO)
void
clear_registers(void)
{
int i;
for (i = 0; i < NUM_REGISTERS; ++i)
{
y_current = &y_regs[i];
if (y_current->y_array != NULL)
free_yank_all();
}
}
#endif
static void
free_yank(long n)
{
if (y_current->y_array != NULL)
{
long i;
for (i = n; --i >= 0; )
{
#ifdef AMIGA
if ((i & 1023) == 1023)
{
++no_wait_return;
smsg((char_u *)_("freeing %ld lines"), i + 1);
--no_wait_return;
msg_didout = FALSE;
msg_col = 0;
}
#endif
vim_free(y_current->y_array[i]);
}
vim_free(y_current->y_array);
y_current->y_array = NULL;
#ifdef AMIGA
if (n >= 1000)
MSG("");
#endif
}
}
static void
free_yank_all(void)
{
free_yank(y_current->y_size);
}
int
op_yank(oparg_T *oap, int deleting, int mess)
{
long y_idx;
yankreg_T *curr;
yankreg_T newreg;
char_u **new_ptr;
linenr_T lnum;
long j;
int yanktype = oap->motion_type;
long yanklines = oap->line_count;
linenr_T yankendlnum = oap->end.lnum;
char_u *p;
char_u *pnew;
struct block_def bd;
#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
int did_star = FALSE;
#endif
if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
{
beep_flush();
return FAIL;
}
if (oap->regname == '_')
return OK;
#ifdef FEAT_CLIPBOARD
if (!clip_star.available && oap->regname == '*')
oap->regname = 0;
else if (!clip_plus.available && oap->regname == '+')
oap->regname = 0;
#endif
if (!deleting)
get_yank_register(oap->regname, TRUE);
curr = y_current;
if (y_append && y_current->y_array != NULL)
y_current = &newreg;
else
free_yank_all();
if ( oap->motion_type == MCHAR
&& oap->start.col == 0
&& !oap->inclusive
&& (!oap->is_VIsual || *p_sel == 'o')
&& !oap->block_mode
&& oap->end.col == 0
&& yanklines > 1)
{
yanktype = MLINE;
--yankendlnum;
--yanklines;
}
y_current->y_size = yanklines;
y_current->y_type = yanktype;
y_current->y_op_change = (oap->op_type == OP_CHANGE) && inindent(0);
y_current->y_width = 0;
y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
yanklines), TRUE);
if (y_current->y_array == NULL)
{
y_current = curr;
return FAIL;
}
#ifdef FEAT_VIMINFO
y_current->y_time_set = vim_time();
#endif
y_idx = 0;
lnum = oap->start.lnum;
if (oap->block_mode)
{
y_current->y_type = MBLOCK;
y_current->y_width = oap->end_vcol - oap->start_vcol;
if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
y_current->y_width--;
}
for ( ; lnum <= yankendlnum; lnum++, y_idx++)
{
switch (y_current->y_type)
{
case MBLOCK:
block_prep(oap, &bd, lnum, FALSE);
if (yank_copy_line(&bd, y_idx) == FAIL)
goto fail;
break;
case MLINE:
if ((y_current->y_array[y_idx] =
vim_strsave(ml_get(lnum))) == NULL)
goto fail;
break;
case MCHAR:
{
colnr_T startcol = 0, endcol = MAXCOL;
#ifdef FEAT_VIRTUALEDIT
int is_oneChar = FALSE;
colnr_T cs, ce;
#endif
p = ml_get(lnum);
bd.startspaces = 0;
bd.endspaces = 0;
if (lnum == oap->start.lnum)
{
startcol = oap->start.col;
#ifdef FEAT_VIRTUALEDIT
if (virtual_op)
{
getvcol(curwin, &oap->start, &cs, NULL, &ce);
if (ce != cs && oap->start.coladd > 0)
{
bd.startspaces = (ce - cs + 1)
- oap->start.coladd;
startcol++;
}
}
#endif
}
if (lnum == oap->end.lnum)
{
endcol = oap->end.col;
#ifdef FEAT_VIRTUALEDIT
if (virtual_op)
{
getvcol(curwin, &oap->end, &cs, NULL, &ce);
if (p[endcol] == NUL || (cs + oap->end.coladd < ce
# ifdef FEAT_MBYTE
&& (*mb_head_off)(p, p + endcol) == 0
# endif
))
{
if (oap->start.lnum == oap->end.lnum
&& oap->start.col == oap->end.col)
{
is_oneChar = TRUE;
bd.startspaces = oap->end.coladd
- oap->start.coladd + oap->inclusive;
endcol = startcol;
}
else
{
bd.endspaces = oap->end.coladd
+ oap->inclusive;
endcol -= oap->inclusive;
}
}
}
#endif
}
if (endcol == MAXCOL)
endcol = (colnr_T)STRLEN(p);
if (startcol > endcol
#ifdef FEAT_VIRTUALEDIT
|| is_oneChar
#endif
)
bd.textlen = 0;
else
{
bd.textlen = endcol - startcol + oap->inclusive;
}
bd.textstart = p + startcol;
if (yank_copy_line(&bd, y_idx) == FAIL)
goto fail;
break;
}
}
}
if (curr != y_current)
{
new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
(curr->y_size + y_current->y_size)), TRUE);
if (new_ptr == NULL)
goto fail;
for (j = 0; j < curr->y_size; ++j)
new_ptr[j] = curr->y_array[j];
vim_free(curr->y_array);
curr->y_array = new_ptr;
#ifdef FEAT_VIMINFO
curr->y_time_set = vim_time();
#endif
if (yanktype == MLINE)
curr->y_type = MLINE;
if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
{
pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
+ STRLEN(y_current->y_array[0]) + 1), TRUE);
if (pnew == NULL)
{
y_idx = y_current->y_size - 1;
goto fail;
}
STRCPY(pnew, curr->y_array[--j]);
STRCAT(pnew, y_current->y_array[0]);
vim_free(curr->y_array[j]);
vim_free(y_current->y_array[0]);
curr->y_array[j++] = pnew;
y_idx = 1;
}
else
y_idx = 0;
while (y_idx < y_current->y_size)
curr->y_array[j++] = y_current->y_array[y_idx++];
curr->y_size = j;
vim_free(y_current->y_array);
y_current = curr;
}
if (curwin->w_p_rnu)
redraw_later(SOME_VALID);
if (mess)
{
if (yanktype == MCHAR
&& !oap->block_mode
&& yanklines == 1)
yanklines = 0;
if (yanklines > p_report)
{
char namebuf[100];
if (oap->regname == NUL)
*namebuf = NUL;
else
vim_snprintf(namebuf, sizeof(namebuf),
_(" into \"%c"), oap->regname);
update_topline_redraw();
if (yanklines == 1)
{
if (oap->block_mode)
smsg((char_u *)_("block of 1 line yanked%s"), namebuf);
else
smsg((char_u *)_("1 line yanked%s"), namebuf);
}
else if (oap->block_mode)
smsg((char_u *)_("block of %ld lines yanked%s"),
yanklines, namebuf);
else
smsg((char_u *)_("%ld lines yanked%s"), yanklines,
namebuf);
}
}
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
if (yanktype == MLINE && !oap->block_mode)
{
curbuf->b_op_start.col = 0;
curbuf->b_op_end.col = MAXCOL;
}
#ifdef FEAT_CLIPBOARD
if (clip_star.available
&& (curr == &(y_regs[STAR_REGISTER])
|| (!deleting && oap->regname == 0
&& ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
{
if (curr != &(y_regs[STAR_REGISTER]))
copy_yank_reg(&(y_regs[STAR_REGISTER]));
clip_own_selection(&clip_star);
clip_gen_set_selection(&clip_star);
# ifdef FEAT_X11
did_star = TRUE;
# endif
}
# ifdef FEAT_X11
if (clip_plus.available
&& (curr == &(y_regs[PLUS_REGISTER])
|| (!deleting && oap->regname == 0
&& ((clip_unnamed | clip_unnamed_saved) &
CLIP_UNNAMED_PLUS))))
{
if (curr != &(y_regs[PLUS_REGISTER]))
copy_yank_reg(&(y_regs[PLUS_REGISTER]));
clip_own_selection(&clip_plus);
clip_gen_set_selection(&clip_plus);
if (!clip_isautosel_star() && !clip_isautosel_plus()
&& !did_star && curr == &(y_regs[PLUS_REGISTER]))
{
copy_yank_reg(&(y_regs[STAR_REGISTER]));
clip_own_selection(&clip_star);
clip_gen_set_selection(&clip_star);
}
}
# endif
#endif
return OK;
fail:
free_yank(y_idx + 1);
y_current = curr;
return FAIL;
}
static int
yank_copy_line(struct block_def *bd, long y_idx)
{
char_u *pnew;
if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
== NULL)
return FAIL;
y_current->y_array[y_idx] = pnew;
vim_memset(pnew, ' ', (size_t)bd->startspaces);
pnew += bd->startspaces;
mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
pnew += bd->textlen;
vim_memset(pnew, ' ', (size_t)bd->endspaces);
pnew += bd->endspaces;
*pnew = NUL;
return OK;
}
#ifdef FEAT_CLIPBOARD
static void
copy_yank_reg(yankreg_T *reg)
{
yankreg_T *curr = y_current;
long j;
y_current = reg;
free_yank_all();
*y_current = *curr;
y_current->y_array = (char_u **)lalloc_clear(
(long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
if (y_current->y_array == NULL)
y_current->y_size = 0;
else
for (j = 0; j < y_current->y_size; ++j)
if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
{
free_yank(j);
y_current->y_size = 0;
break;
}
y_current = curr;
}
#endif
void
do_put(
int regname,
int dir,
long count,
int flags)
{
char_u *ptr;
char_u *newp, *oldp;
int yanklen;
int totlen = 0;
linenr_T lnum;
colnr_T col;
long i;
int y_type;
long y_size;
int oldlen;
long y_width = 0;
colnr_T vcol;
int delcount;
int incr = 0;
long j;
struct block_def bd;
char_u **y_array = NULL;
long nr_lines = 0;
pos_T new_cursor;
int indent;
int orig_indent = 0;
int indent_diff = 0;
int first_indent = TRUE;
int lendiff = 0;
pos_T old_pos;
char_u *insert_string = NULL;
int allocated = FALSE;
long cnt;
#ifdef FEAT_CLIPBOARD
adjust_clip_reg(®name);
(void)may_get_selection(regname);
#endif
if (flags & PUT_FIXINDENT)
orig_indent = get_indent();
curbuf->b_op_start = curwin->w_cursor;
curbuf->b_op_end = curwin->w_cursor;
if (regname == '.')
{
if (VIsual_active)
stuffcharReadbuff(VIsual_mode);
(void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
(count == -1 ? 'O' : 'i')), count, FALSE);
if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
stuffcharReadbuff('l');
return;
}
if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
{
if (insert_string == NULL)
return;
}
#ifdef FEAT_AUTOCMD
u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1);
#endif
if (insert_string != NULL)
{
y_type = MCHAR;
#ifdef FEAT_EVAL
if (regname == '=')
{
for (;;)
{
y_size = 0;
ptr = insert_string;
while (ptr != NULL)
{
if (y_array != NULL)
y_array[y_size] = ptr;
++y_size;
ptr = vim_strchr(ptr, '\n');
if (ptr != NULL)
{
if (y_array != NULL)
*ptr = NUL;
++ptr;
if (*ptr == NUL)
{
y_type = MLINE;
break;
}
}
}
if (y_array != NULL)
break;
y_array = (char_u **)alloc((unsigned)
(y_size * sizeof(char_u *)));
if (y_array == NULL)
goto end;
}
}
else
#endif
{
y_size = 1;
y_array = &insert_string;
}
}
else
{
get_yank_register(regname, FALSE);
y_type = y_current->y_type;
y_width = y_current->y_width;
y_size = y_current->y_size;
if (Unix2003_compat && y_size > 1 && y_type == MCHAR && curwin->w_cursor.col == 0 && y_current->y_op_change) {
y_type = MLINE;
}
y_array = y_current->y_array;
}
if (y_type == MLINE)
{
if (flags & PUT_LINE_SPLIT)
{
char_u *p;
if (u_save_cursor() == FAIL)
goto end;
p = ml_get_cursor();
if (dir == FORWARD && *p != NUL)
MB_PTR_ADV(p);
ptr = vim_strsave(p);
if (ptr == NULL)
goto end;
ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
vim_free(ptr);
oldp = ml_get_curline();
p = oldp + curwin->w_cursor.col;
if (dir == FORWARD && *p != NUL)
MB_PTR_ADV(p);
ptr = vim_strnsave(oldp, p - oldp);
if (ptr == NULL)
goto end;
ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
++nr_lines;
dir = FORWARD;
}
if (flags & PUT_LINE_FORWARD)
{
curwin->w_cursor = curbuf->b_visual.vi_end;
dir = FORWARD;
}
curbuf->b_op_start = curwin->w_cursor;
curbuf->b_op_end = curwin->w_cursor;
}
if (flags & PUT_LINE)
y_type = MLINE;
if (y_size == 0 || y_array == NULL)
{
EMSG2(_("E353: Nothing in register %s"),
regname == 0 ? (char_u *)"\"" : transchar(regname));
goto end;
}
if (y_type == MBLOCK)
{
lnum = curwin->w_cursor.lnum + y_size + 1;
if (lnum > curbuf->b_ml.ml_line_count)
lnum = curbuf->b_ml.ml_line_count + 1;
if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
goto end;
}
else if (y_type == MLINE)
{
lnum = curwin->w_cursor.lnum;
#ifdef FEAT_FOLDING
if (dir == BACKWARD)
(void)hasFolding(lnum, &lnum, NULL);
else
(void)hasFolding(lnum, NULL, &lnum);
#endif
if (dir == FORWARD)
++lnum;
if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
goto end;
#ifdef FEAT_FOLDING
if (dir == FORWARD)
curwin->w_cursor.lnum = lnum - 1;
else
curwin->w_cursor.lnum = lnum;
curbuf->b_op_start = curwin->w_cursor;
#endif
}
else if (u_save_cursor() == FAIL)
goto end;
yanklen = (int)STRLEN(y_array[0]);
#ifdef FEAT_VIRTUALEDIT
if (ve_flags == VE_ALL && y_type == MCHAR)
{
if (gchar_cursor() == TAB)
{
if (dir == FORWARD
? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
: curwin->w_cursor.coladd > 0)
coladvance_force(getviscol());
else
curwin->w_cursor.coladd = 0;
}
else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
coladvance_force(getviscol() + (dir == FORWARD));
}
#endif
lnum = curwin->w_cursor.lnum;
col = curwin->w_cursor.col;
if (y_type == MBLOCK)
{
int c = gchar_cursor();
colnr_T endcol2 = 0;
if (dir == FORWARD && c != NUL)
{
#ifdef FEAT_VIRTUALEDIT
if (ve_flags == VE_ALL)
getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
else
#endif
getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
#ifdef FEAT_MBYTE
if (has_mbyte)
curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
else
#endif
#ifdef FEAT_VIRTUALEDIT
if (c != TAB || ve_flags != VE_ALL)
#endif
++curwin->w_cursor.col;
++col;
}
else
getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
#ifdef FEAT_VIRTUALEDIT
col += curwin->w_cursor.coladd;
if (ve_flags == VE_ALL
&& (curwin->w_cursor.coladd > 0
|| endcol2 == curwin->w_cursor.col))
{
if (dir == FORWARD && c == NUL)
++col;
if (dir != FORWARD && c != NUL)
++curwin->w_cursor.col;
if (c == TAB)
{
if (dir == BACKWARD && curwin->w_cursor.col)
curwin->w_cursor.col--;
if (dir == FORWARD && col - 1 == endcol2)
curwin->w_cursor.col++;
}
}
curwin->w_cursor.coladd = 0;
#endif
bd.textcol = 0;
for (i = 0; i < y_size; ++i)
{
int spaces;
char shortline;
bd.startspaces = 0;
bd.endspaces = 0;
vcol = 0;
delcount = 0;
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
{
if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
(colnr_T)1, FALSE) == FAIL)
break;
++nr_lines;
}
oldp = ml_get_curline();
oldlen = (int)STRLEN(oldp);
for (ptr = oldp; vcol < col && *ptr; )
{
incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
vcol += incr;
}
bd.textcol = (colnr_T)(ptr - oldp);
shortline = (vcol < col) || (vcol == col && !*ptr) ;
if (vcol < col)
bd.startspaces = col - vcol;
else if (vcol > col)
{
bd.endspaces = vcol - col;
bd.startspaces = incr - bd.endspaces;
--bd.textcol;
delcount = 1;
#ifdef FEAT_MBYTE
if (has_mbyte)
bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
#endif
if (oldp[bd.textcol] != TAB)
{
delcount = 0;
bd.endspaces = 0;
}
}
yanklen = (int)STRLEN(y_array[i]);
spaces = y_width + 1;
for (j = 0; j < yanklen; j++)
spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
if (spaces < 0)
spaces = 0;
totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
newp = alloc_check((unsigned)totlen + oldlen + 1);
if (newp == NULL)
break;
ptr = newp;
mch_memmove(ptr, oldp, (size_t)bd.textcol);
ptr += bd.textcol;
vim_memset(ptr, ' ', (size_t)bd.startspaces);
ptr += bd.startspaces;
for (j = 0; j < count; ++j)
{
mch_memmove(ptr, y_array[i], (size_t)yanklen);
ptr += yanklen;
if ((j < count - 1 || !shortline) && spaces)
{
vim_memset(ptr, ' ', (size_t)spaces);
ptr += spaces;
}
}
vim_memset(ptr, ' ', (size_t)bd.endspaces);
ptr += bd.endspaces;
mch_memmove(ptr, oldp + bd.textcol + delcount,
(size_t)(oldlen - bd.textcol - delcount + 1));
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
++curwin->w_cursor.lnum;
if (i == 0)
curwin->w_cursor.col += bd.startspaces;
}
changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
curbuf->b_op_start = curwin->w_cursor;
curbuf->b_op_start.lnum = lnum;
curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
curbuf->b_op_end.col = bd.textcol + totlen - 1;
# ifdef FEAT_VIRTUALEDIT
curbuf->b_op_end.coladd = 0;
# endif
if (flags & PUT_CURSEND)
{
colnr_T len;
curwin->w_cursor = curbuf->b_op_end;
curwin->w_cursor.col++;
len = (colnr_T)STRLEN(ml_get_curline());
if (curwin->w_cursor.col > len)
curwin->w_cursor.col = len;
}
else
curwin->w_cursor.lnum = lnum;
}
else
{
if (y_type == MCHAR)
{
if (dir == FORWARD && gchar_cursor() != NUL)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int bytelen = (*mb_ptr2len)(ml_get_cursor());
col += bytelen;
if (yanklen)
{
curwin->w_cursor.col += bytelen;
curbuf->b_op_end.col += bytelen;
}
}
else
#endif
{
++col;
if (yanklen)
{
++curwin->w_cursor.col;
++curbuf->b_op_end.col;
}
}
}
curbuf->b_op_start = curwin->w_cursor;
}
else if (dir == BACKWARD)
--lnum;
new_cursor = curwin->w_cursor;
if (y_type == MCHAR && y_size == 1)
{
linenr_T end_lnum = 0;
if (VIsual_active)
{
end_lnum = curbuf->b_visual.vi_end.lnum;
if (end_lnum < curbuf->b_visual.vi_start.lnum)
end_lnum = curbuf->b_visual.vi_start.lnum;
}
do {
totlen = count * yanklen;
if (totlen > 0)
{
oldp = ml_get(lnum);
if (VIsual_active && col > (int)STRLEN(oldp))
{
lnum++;
continue;
}
newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
if (newp == NULL)
goto end;
mch_memmove(newp, oldp, (size_t)col);
ptr = newp + col;
for (i = 0; i < count; ++i)
{
mch_memmove(ptr, y_array[0], (size_t)yanklen);
ptr += yanklen;
}
STRMOVE(ptr, oldp + col);
ml_replace(lnum, newp, FALSE);
if (lnum == curwin->w_cursor.lnum)
{
changed_cline_bef_curs();
curwin->w_cursor.col += (colnr_T)(totlen - 1);
}
}
if (VIsual_active)
lnum++;
} while (VIsual_active && lnum <= end_lnum);
if (VIsual_active)
lnum--;
curbuf->b_op_end = curwin->w_cursor;
if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
++curwin->w_cursor.col;
changed_bytes(lnum, col);
}
else
{
for (cnt = 1; cnt <= count; ++cnt)
{
i = 0;
if (y_type == MCHAR)
{
lnum = new_cursor.lnum;
ptr = ml_get(lnum) + col;
totlen = (int)STRLEN(y_array[y_size - 1]);
newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
if (newp == NULL)
goto error;
STRCPY(newp, y_array[y_size - 1]);
STRCAT(newp, ptr);
ml_append(lnum, newp, (colnr_T)0, FALSE);
vim_free(newp);
oldp = ml_get(lnum);
newp = alloc_check((unsigned)(col + yanklen + 1));
if (newp == NULL)
goto error;
mch_memmove(newp, oldp, (size_t)col);
mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
ml_replace(lnum, newp, FALSE);
curwin->w_cursor.lnum = lnum;
i = 1;
}
for (; i < y_size; ++i)
{
if ((y_type != MCHAR || i < y_size - 1)
&& ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
== FAIL)
goto error;
lnum++;
++nr_lines;
if (flags & PUT_FIXINDENT)
{
old_pos = curwin->w_cursor;
curwin->w_cursor.lnum = lnum;
ptr = ml_get(lnum);
if (cnt == count && i == y_size - 1)
lendiff = (int)STRLEN(ptr);
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
if (*ptr == '#' && preprocs_left())
indent = 0;
else
#endif
if (*ptr == NUL)
indent = 0;
else if (first_indent)
{
indent_diff = orig_indent - get_indent();
indent = orig_indent;
first_indent = FALSE;
}
else if ((indent = get_indent() + indent_diff) < 0)
indent = 0;
(void)set_indent(indent, 0);
curwin->w_cursor = old_pos;
if (cnt == count && i == y_size - 1)
lendiff -= (int)STRLEN(ml_get(lnum));
}
}
}
error:
if (y_type == MLINE)
{
curbuf->b_op_start.col = 0;
if (dir == FORWARD)
curbuf->b_op_start.lnum++;
}
if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
< curbuf->b_ml.ml_line_count
#ifdef FEAT_DIFF
|| curwin->w_p_diff
#endif
)
mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
(linenr_T)MAXLNUM, nr_lines, 0L);
if (y_type == MCHAR)
changed_lines(curwin->w_cursor.lnum, col,
curwin->w_cursor.lnum + 1, nr_lines);
else
changed_lines(curbuf->b_op_start.lnum, 0,
curbuf->b_op_start.lnum, nr_lines);
curbuf->b_op_end.lnum = lnum;
col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
if (col > 1)
curbuf->b_op_end.col = col - 1;
else
curbuf->b_op_end.col = 0;
if (flags & PUT_CURSLINE)
{
curwin->w_cursor.lnum = lnum;
beginline(BL_WHITE | BL_FIX);
}
else if (flags & PUT_CURSEND)
{
if (y_type == MLINE)
{
if (lnum >= curbuf->b_ml.ml_line_count)
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
else
curwin->w_cursor.lnum = lnum + 1;
curwin->w_cursor.col = 0;
}
else
{
curwin->w_cursor.lnum = lnum;
curwin->w_cursor.col = col;
}
}
else if (y_type == MLINE)
{
curwin->w_cursor.col = 0;
if (dir == FORWARD)
++curwin->w_cursor.lnum;
beginline(BL_WHITE | BL_FIX);
}
else if (dir == BACKWARD && Unix2003_compat)
{
curwin->w_cursor.lnum = lnum;
curwin->w_cursor.col = col > 0 ? (col - 1) : 0;
}
else
curwin->w_cursor = new_cursor;
}
}
msgmore(nr_lines);
curwin->w_set_curswant = TRUE;
end:
if (allocated)
vim_free(insert_string);
if (regname == '=')
vim_free(y_array);
VIsual_active = FALSE;
adjust_cursor_eol();
}
void
adjust_cursor_eol(void)
{
if (curwin->w_cursor.col > 0
&& gchar_cursor() == NUL
#ifdef FEAT_VIRTUALEDIT
&& (ve_flags & VE_ONEMORE) == 0
#endif
&& !(restart_edit || (State & INSERT)))
{
dec_cursor();
#ifdef FEAT_VIRTUALEDIT
if (ve_flags == VE_ALL)
{
colnr_T scol, ecol;
getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
curwin->w_cursor.coladd = ecol - scol + 1;
}
#endif
}
}
#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
int
preprocs_left(void)
{
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
;
}
#endif
int
get_register_name(int num)
{
if (num == -1)
return '"';
else if (num < 10)
return num + '0';
else if (num == DELETION_REGISTER)
return '-';
#ifdef FEAT_CLIPBOARD
else if (num == STAR_REGISTER)
return '*';
else if (num == PLUS_REGISTER)
return '+';
#endif
else
{
#ifdef EBCDIC
int i;
i = 'a' + (num - 10);
if (i > 'i')
i += 7;
if (i > 'r')
i += 8;
return i;
#else
return num + 'a' - 10;
#endif
}
}
void
ex_display(exarg_T *eap)
{
int i, n;
long j;
char_u *p;
yankreg_T *yb;
int name;
int attr;
char_u *arg = eap->arg;
#ifdef FEAT_MBYTE
int clen;
#else
# define clen 1
#endif
if (arg != NULL && *arg == NUL)
arg = NULL;
attr = HL_ATTR(HLF_8);
MSG_PUTS_TITLE(_("\n--- Registers ---"));
for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
{
name = get_register_name(i);
if (arg != NULL && vim_strchr(arg, name) == NULL
#ifdef ONE_CLIPBOARD
&& (name != '*' || vim_strchr(arg, '+') == NULL)
#endif
)
continue;
#ifdef FEAT_CLIPBOARD
adjust_clip_reg(&name);
(void)may_get_selection(name);
#endif
if (i == -1)
{
if (y_previous != NULL)
yb = y_previous;
else
yb = &(y_regs[0]);
}
else
yb = &(y_regs[i]);
#ifdef FEAT_EVAL
if (name == MB_TOLOWER(redir_reg)
|| (redir_reg == '"' && yb == y_previous))
continue;
#endif
if (yb->y_array != NULL)
{
msg_putchar('\n');
msg_putchar('"');
msg_putchar(name);
MSG_PUTS(" ");
n = (int)Columns - 6;
for (j = 0; j < yb->y_size && n > 1; ++j)
{
if (j)
{
MSG_PUTS_ATTR("^J", attr);
n -= 2;
}
for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
{
#ifdef FEAT_MBYTE
clen = (*mb_ptr2len)(p);
#endif
msg_outtrans_len(p, clen);
#ifdef FEAT_MBYTE
p += clen - 1;
#endif
}
}
if (n > 1 && yb->y_type == MLINE)
MSG_PUTS_ATTR("^J", attr);
out_flush();
}
ui_breakcheck();
}
if ((p = get_last_insert()) != NULL
&& (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
{
MSG_PUTS("\n\". ");
dis_msg(p, TRUE);
}
if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
&& !got_int)
{
MSG_PUTS("\n\": ");
dis_msg(last_cmdline, FALSE);
}
if (curbuf->b_fname != NULL
&& (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
{
MSG_PUTS("\n\"% ");
dis_msg(curbuf->b_fname, FALSE);
}
if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
{
char_u *fname;
linenr_T dummy;
if (buflist_name_nr(0, &fname, &dummy) != FAIL)
{
MSG_PUTS("\n\"# ");
dis_msg(fname, FALSE);
}
}
if (last_search_pat() != NULL
&& (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
{
MSG_PUTS("\n\"/ ");
dis_msg(last_search_pat(), FALSE);
}
#ifdef FEAT_EVAL
if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
&& !got_int)
{
MSG_PUTS("\n\"= ");
dis_msg(expr_line, FALSE);
}
#endif
}
static void
dis_msg(
char_u *p,
int skip_esc)
{
int n;
#ifdef FEAT_MBYTE
int l;
#endif
n = (int)Columns - 6;
while (*p != NUL
&& !(*p == ESC && skip_esc && *(p + 1) == NUL)
&& (n -= ptr2cells(p)) >= 0)
{
#ifdef FEAT_MBYTE
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
{
msg_outtrans_len(p, l);
p += l;
}
else
#endif
msg_outtrans_len(p++, 1);
}
ui_breakcheck();
}
#if defined(FEAT_COMMENTS) || defined(PROTO)
char_u *
skip_comment(
char_u *line,
int process,
int include_space,
int *is_comment)
{
char_u *comment_flags = NULL;
int lead_len;
int leader_offset = get_last_leader_offset(line, &comment_flags);
*is_comment = FALSE;
if (leader_offset != -1)
{
while (*comment_flags)
{
if (*comment_flags == COM_END
|| *comment_flags == ':')
break;
++comment_flags;
}
if (*comment_flags != COM_END)
*is_comment = TRUE;
}
if (process == FALSE)
return line;
lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
if (lead_len == 0)
return line;
while (*comment_flags)
{
if (*comment_flags == COM_END
|| *comment_flags == ':')
{
break;
}
++comment_flags;
}
if (*comment_flags == ':' || *comment_flags == NUL)
line += lead_len;
return line;
}
#endif
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;
int endcurr1 = NUL;
int endcurr2 = NUL;
int currsize = 0;
int sumsize = 0;
linenr_T t;
colnr_T col = 0;
int ret = OK;
#if defined(FEAT_COMMENTS) || defined(PROTO)
int *comments = NULL;
int remove_comments = (use_formatoptions == TRUE)
&& has_format_option(FO_REMOVE_COMS);
int prev_was_comment;
#endif
if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
(linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
return FAIL;
spaces = lalloc_clear((long_u)count, TRUE);
if (spaces == NULL)
return FAIL;
#if defined(FEAT_COMMENTS) || defined(PROTO)
if (remove_comments)
{
comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE);
if (comments == NULL)
{
vim_free(spaces);
return FAIL;
}
}
#endif
for (t = 0; t < count; ++t)
{
curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
if (t == 0 && setmark)
{
curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
}
#if defined(FEAT_COMMENTS) || defined(PROTO)
if (remove_comments)
{
if (t > 0 && prev_was_comment)
{
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);
}
#endif
if (insert_space && t > 0)
{
curr = skipwhite(curr);
if (*curr != ')' && currsize != 0 && endcurr1 != TAB
#ifdef FEAT_MBYTE
&& (!has_format_option(FO_MBYTE_JOIN)
|| (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
&& (!has_format_option(FO_MBYTE_JOIN2)
|| mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
#endif
)
{
if (endcurr1 == ' ')
endcurr1 = endcurr2;
else
++spaces[t];
if ( p_js
&& (endcurr1 == '.'
|| (vim_strchr(p_cpo, CPO_JOINSP) == NULL
&& (endcurr1 == '?' || endcurr1 == '!'))))
++spaces[t];
}
}
currsize = (int)STRLEN(curr);
sumsize += currsize + spaces[t];
endcurr1 = endcurr2 = NUL;
if (insert_space && currsize > 0)
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
cend = curr + currsize;
MB_PTR_BACK(curr, cend);
endcurr1 = (*mb_ptr2char)(cend);
if (cend > curr)
{
MB_PTR_BACK(curr, cend);
endcurr2 = (*mb_ptr2char)(cend);
}
}
else
#endif
{
endcurr1 = *(curr + currsize - 1);
if (currsize > 1)
endcurr2 = *(curr + currsize - 2);
}
}
line_breakcheck();
if (got_int)
{
ret = FAIL;
goto theend;
}
}
col = sumsize - currsize - spaces[count - 1];
newp = alloc_check((unsigned)(sumsize + 1));
cend = newp + sumsize;
*cend = 0;
for (t = count - 1; ; --t)
{
cend -= currsize;
mch_memmove(cend, curr, (size_t)currsize);
if (spaces[t] > 0)
{
cend -= spaces[t];
vim_memset(cend, ' ', (size_t)(spaces[t]));
}
mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
(long)(cend - newp + spaces[t] - (curr - curr_start)));
if (t == 0)
break;
curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
#if defined(FEAT_COMMENTS) || defined(PROTO)
if (remove_comments)
curr += comments[t - 1];
#endif
if (insert_space && t > 1)
curr = skipwhite(curr);
currsize = (int)STRLEN(curr);
}
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
if (setmark)
{
curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp);
}
changed_lines(curwin->w_cursor.lnum, currsize,
curwin->w_cursor.lnum + 1, 0L);
t = curwin->w_cursor.lnum;
++curwin->w_cursor.lnum;
del_lines(count - 1, FALSE);
curwin->w_cursor.lnum = t;
curwin->w_cursor.col =
(vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
check_cursor_col();
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
curwin->w_set_curswant = TRUE;
theend:
vim_free(spaces);
#if defined(FEAT_COMMENTS) || defined(PROTO)
if (remove_comments)
vim_free(comments);
#endif
return ret;
}
#ifdef FEAT_COMMENTS
static int
same_leader(
linenr_T lnum,
int leader1_len,
char_u *leader1_flags,
int leader2_len,
char_u *leader2_flags)
{
int idx1 = 0, idx2 = 0;
char_u *p;
char_u *line1;
char_u *line2;
if (leader1_len == 0)
return (leader2_len == 0);
if (leader1_flags != NULL)
{
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;
}
}
}
line1 = vim_strsave(ml_get(lnum));
if (line1 != NULL)
{
for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
;
line2 = ml_get(lnum + 1);
for (idx2 = 0; idx2 < leader2_len; ++idx2)
{
if (!VIM_ISWHITE(line2[idx2]))
{
if (line1[idx1++] != line2[idx2])
break;
}
else
while (VIM_ISWHITE(line1[idx1]))
++idx1;
}
vim_free(line1);
}
return (idx2 == leader2_len && idx1 == leader1_len);
}
#endif
void
op_format(
oparg_T *oap,
int keep_cursor)
{
long old_line_count = curbuf->b_ml.ml_line_count;
curwin->w_cursor = oap->cursor_start;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
curwin->w_cursor = oap->start;
if (oap->is_VIsual)
redraw_curbuf_later(INVERTED);
curbuf->b_op_start = oap->start;
if (keep_cursor)
saved_cursor = oap->cursor_start;
format_lines(oap->line_count, keep_cursor);
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);
curbuf->b_op_end = curwin->w_cursor;
if (keep_cursor)
{
curwin->w_cursor = saved_cursor;
saved_cursor.lnum = 0;
}
if (oap->is_VIsual)
{
win_T *wp;
FOR_ALL_WINDOWS(wp)
{
if (wp->w_old_cursor_lnum != 0)
{
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;
}
}
}
}
#if defined(FEAT_EVAL) || defined(PROTO)
void
op_formatexpr(oparg_T *oap)
{
if (oap->is_VIsual)
redraw_curbuf_later(INVERTED);
if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
op_format(oap, FALSE);
}
int
fex_format(
linenr_T lnum,
long count,
int c)
{
int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
OPT_LOCAL);
int r;
char_u *fex;
set_vim_var_nr(VV_LNUM, lnum);
set_vim_var_nr(VV_COUNT, count);
set_vim_var_char(c);
fex = vim_strsave(curbuf->b_p_fex);
if (fex == NULL)
return 0;
if (use_sandbox)
++sandbox;
r = (int)eval_to_number(fex);
if (use_sandbox)
--sandbox;
set_vim_var_string(VV_CHAR, NULL, -1);
vim_free(fex);
return r;
}
#endif
void
format_lines(
linenr_T line_count,
int avoid_fex)
{
int max_len;
int is_not_par;
int next_is_not_par;
int is_end_par;
int prev_is_end_par = FALSE;
int next_is_start_par = FALSE;
#ifdef FEAT_COMMENTS
int leader_len = 0;
int next_leader_len;
char_u *leader_flags = NULL;
char_u *next_leader_flags;
int do_comments;
int do_comments_list = 0;
#endif
int advance = TRUE;
int second_indent = -1;
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;
int force_format = FALSE;
int old_State = State;
max_len = comp_textwidth(TRUE) * 3;
#ifdef FEAT_COMMENTS
do_comments = has_format_option(FO_Q_COMS);
#endif
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);
if (curwin->w_cursor.lnum > 1)
is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
#ifdef FEAT_COMMENTS
, &leader_len, &leader_flags, do_comments
#endif
);
else
is_not_par = TRUE;
next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
#ifdef FEAT_COMMENTS
, &next_leader_len, &next_leader_flags, do_comments
#endif
);
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)
{
if (advance)
{
curwin->w_cursor.lnum++;
prev_is_end_par = is_end_par;
is_not_par = next_is_not_par;
#ifdef FEAT_COMMENTS
leader_len = next_leader_len;
leader_flags = next_leader_flags;
#endif
}
if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
{
next_is_not_par = TRUE;
#ifdef FEAT_COMMENTS
next_leader_len = 0;
next_leader_flags = NULL;
#endif
}
else
{
next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
#ifdef FEAT_COMMENTS
, &next_leader_len, &next_leader_flags, do_comments
#endif
);
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);
if (is_not_par)
{
if (line_count < 0)
break;
}
else
{
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))
{
#ifdef FEAT_COMMENTS
if (leader_len == 0 && next_leader_len == 0)
{
#endif
second_indent =
get_indent_lnum(curwin->w_cursor.lnum + 1);
#ifdef FEAT_COMMENTS
}
else
{
second_indent = next_leader_len;
do_comments_list = 1;
}
#endif
}
else if (do_number_indent)
{
#ifdef FEAT_COMMENTS
if (leader_len == 0 && next_leader_len == 0)
{
#endif
second_indent =
get_number_indent(curwin->w_cursor.lnum);
#ifdef FEAT_COMMENTS
}
else
{
second_indent =
get_number_indent(curwin->w_cursor.lnum);
do_comments_list = 1;
}
#endif
}
}
if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
#ifdef FEAT_COMMENTS
|| !same_leader(curwin->w_cursor.lnum,
leader_len, leader_flags,
next_leader_len, next_leader_flags)
#endif
)
is_end_par = TRUE;
if (is_end_par || force_format)
{
if (need_set_indent)
(void)set_indent(get_indent(), SIN_CHANGED);
State = NORMAL;
coladvance((colnr_T)MAXCOL);
while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
dec_cursor();
State = INSERT;
smd_save = p_smd;
p_smd = FALSE;
insertchar(NUL, INSCHAR_FORMAT
#ifdef FEAT_COMMENTS
+ (do_comments ? INSCHAR_DO_COM : 0)
+ (do_comments && do_comments_list
? INSCHAR_COM_LIST : 0)
#endif
+ (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
State = old_State;
p_smd = smd_save;
second_indent = -1;
need_set_indent = is_end_par;
if (is_end_par)
{
if (line_count < 0)
break;
first_par_line = TRUE;
}
force_format = FALSE;
}
if (!is_end_par)
{
advance = FALSE;
curwin->w_cursor.lnum++;
curwin->w_cursor.col = 0;
if (line_count < 0 && u_save_cursor() == FAIL)
break;
#ifdef FEAT_COMMENTS
if (next_leader_len > 0)
{
(void)del_bytes((long)next_leader_len, FALSE, FALSE);
mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
(long)-next_leader_len);
} else
#endif
if (second_indent > 0)
{
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);
}
}
curwin->w_cursor.lnum--;
if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
{
beep_flush();
break;
}
first_par_line = FALSE;
if (STRLEN(ml_get_curline()) > (size_t)max_len)
force_format = TRUE;
else
force_format = FALSE;
}
}
line_breakcheck();
}
}
static int
ends_in_white(linenr_T lnum)
{
char_u *s = ml_get(lnum);
size_t l;
if (*s == NUL)
return FALSE;
l = STRLEN(s) - 1;
return VIM_ISWHITE(s[l]);
}
#ifdef FEAT_COMMENTS
static int
fmt_check_par(
linenr_T lnum,
int *leader_len,
char_u **leader_flags,
int do_comments)
{
char_u *flags = NULL;
char_u *ptr;
ptr = ml_get(lnum);
if (do_comments)
*leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
else
*leader_len = 0;
if (*leader_len > 0)
{
flags = *leader_flags;
while (*flags && *flags != ':' && *flags != COM_END)
++flags;
}
return (*skipwhite(ptr + *leader_len) == NUL
|| (*leader_len > 0 && *flags == COM_END)
|| startPS(lnum, NUL, FALSE));
}
#else
static int
fmt_check_par(linenr_T lnum)
{
return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
}
#endif
int
paragraph_start(linenr_T lnum)
{
char_u *p;
#ifdef FEAT_COMMENTS
int leader_len = 0;
char_u *leader_flags = NULL;
int next_leader_len;
char_u *next_leader_flags;
int do_comments;
#endif
if (lnum <= 1)
return TRUE;
p = ml_get(lnum - 1);
if (*p == NUL)
return TRUE;
#ifdef FEAT_COMMENTS
do_comments = has_format_option(FO_Q_COMS);
#endif
if (fmt_check_par(lnum - 1
#ifdef FEAT_COMMENTS
, &leader_len, &leader_flags, do_comments
#endif
))
return TRUE;
if (fmt_check_par(lnum
#ifdef FEAT_COMMENTS
, &next_leader_len, &next_leader_flags, do_comments
#endif
))
return TRUE;
if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
return TRUE;
if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
return TRUE;
#ifdef FEAT_COMMENTS
if (!same_leader(lnum - 1, leader_len, leader_flags,
next_leader_len, next_leader_flags))
return TRUE;
#endif
return FALSE;
}
static 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;
bdp->startspaces = 0;
bdp->endspaces = 0;
bdp->textlen = 0;
bdp->start_vcol = 0;
bdp->end_vcol = 0;
#ifdef FEAT_VISUALEXTRA
bdp->is_short = FALSE;
bdp->is_oneChar = FALSE;
bdp->pre_whitesp = 0;
bdp->pre_whitesp_c = 0;
bdp->end_char_vcols = 0;
#endif
bdp->start_char_vcols = 0;
line = ml_get(lnum);
pstart = line;
prev_pstart = line;
while (bdp->start_vcol < oap->start_vcol && *pstart)
{
incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
bdp->start_vcol += incr;
#ifdef FEAT_VISUALEXTRA
if (VIM_ISWHITE(*pstart))
{
bdp->pre_whitesp += incr;
bdp->pre_whitesp_c++;
}
else
{
bdp->pre_whitesp = 0;
bdp->pre_whitesp_c = 0;
}
#endif
prev_pstart = pstart;
MB_PTR_ADV(pstart);
}
bdp->start_char_vcols = incr;
if (bdp->start_vcol < oap->start_vcol)
{
bdp->end_vcol = bdp->start_vcol;
#ifdef FEAT_VISUALEXTRA
bdp->is_short = TRUE;
#endif
if (!is_del || oap->op_type == OP_APPEND)
bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
}
else
{
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)
{
#ifdef FEAT_VISUALEXTRA
bdp->is_oneChar = TRUE;
#endif
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)
{
bdp->startspaces = bdp->start_char_vcols
- (bdp->start_vcol - oap->start_vcol);
bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
}
}
}
else
{
prev_pend = pend;
while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
{
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))
{
#ifdef FEAT_VISUALEXTRA
bdp->is_short = TRUE;
#endif
if (oap->op_type == OP_APPEND || virtual_op)
bdp->endspaces = oap->end_vcol - bdp->end_vcol
+ oap->inclusive;
else
bdp->endspaces = 0;
}
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;
}
}
}
#ifdef FEAT_VISUALEXTRA
bdp->end_char_vcols = incr;
#endif
if (is_del && bdp->startspaces)
pstart = prev_pstart;
bdp->textlen = (int)(pend - pstart);
}
bdp->textcol = (colnr_T) (pstart - line);
bdp->textstart = pstart;
}
void
op_addsub(
oparg_T *oap,
linenr_T Prenum1,
int g_cmd)
{
pos_T pos;
struct block_def bd;
int change_cnt = 0;
linenr_T amount = Prenum1;
if (!VIsual_active)
{
pos = curwin->w_cursor;
if (u_save_cursor() == FAIL)
return;
change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
if (change_cnt)
changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
}
else
{
int one_change;
int length;
pos_T startpos;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
pos = oap->start;
for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
{
if (oap->block_mode)
{
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
{
if (!oap->inclusive)
dec(&(oap->end));
length = (colnr_T)STRLEN(ml_get(pos.lnum));
pos.col = 0;
if (pos.lnum == oap->start.lnum)
{
pos.col += oap->start.col;
length -= oap->start.col;
}
if (pos.lnum == oap->end.lnum)
{
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;
}
}
one_change = do_addsub(oap->op_type, &pos, length, amount);
if (one_change)
{
if (change_cnt == 0)
startpos = curbuf->b_op_start;
++change_cnt;
}
#ifdef FEAT_NETBEANS_INTG
if (netbeans_active() && one_change)
{
char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
netbeans_inserted(curbuf, pos.lnum, pos.col,
&ptr[pos.col], length);
}
#endif
if (g_cmd && one_change)
amount += Prenum1;
}
if (change_cnt)
changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
if (!change_cnt && oap->is_VIsual)
redraw_curbuf_later(INVERTED);
if (change_cnt > 0)
curbuf->b_op_start = startpos;
if (change_cnt > p_report)
{
if (change_cnt == 1)
MSG(_("1 line changed"));
else
smsg((char_u *)_("%ld lines changed"), change_cnt);
}
}
}
static int
do_addsub(
int op_type,
pos_T *pos,
int length,
linenr_T Prenum1)
{
int col;
char_u *buf1;
char_u buf2[NUMBUFLEN];
int pre;
static int hexupper = FALSE;
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;
dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL);
dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL);
dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL);
doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL);
curwin->w_cursor = *pos;
ptr = ml_get(pos->lnum);
col = pos->col;
if (*ptr == NUL)
goto theend;
if (!VIsual_active)
{
if (dobin)
while (col > 0 && vim_isbdigit(ptr[col]))
{
--col;
#ifdef FEAT_MBYTE
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
#endif
}
if (dohex)
while (col > 0 && vim_isxdigit(ptr[col]))
{
--col;
#ifdef FEAT_MBYTE
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
#endif
}
if ( dobin
&& dohex
&& ! ((col > 0
&& (ptr[col] == 'X'
|| ptr[col] == 'x')
&& ptr[col - 1] == '0'
#ifdef FEAT_MBYTE
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
#endif
&& vim_isxdigit(ptr[col + 1]))))
{
col = pos->col;
while (col > 0 && vim_isdigit(ptr[col]))
{
col--;
#ifdef FEAT_MBYTE
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
#endif
}
}
if (( dohex
&& col > 0
&& (ptr[col] == 'X'
|| ptr[col] == 'x')
&& ptr[col - 1] == '0'
#ifdef FEAT_MBYTE
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
#endif
&& vim_isxdigit(ptr[col + 1])) ||
( dobin
&& col > 0
&& (ptr[col] == 'B'
|| ptr[col] == 'b')
&& ptr[col - 1] == '0'
#ifdef FEAT_MBYTE
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
#endif
&& vim_isbdigit(ptr[col + 1])))
{
--col;
#ifdef FEAT_MBYTE
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
#endif
}
else
{
col = pos->col;
while (ptr[col] != NUL
&& !vim_isdigit(ptr[col])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
col += MB_PTR2LEN(ptr + col);
while (col > 0
&& vim_isdigit(ptr[col - 1])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
{
--col;
#ifdef FEAT_MBYTE
if (has_mbyte)
col -= (*mb_head_off)(ptr, ptr + col);
#endif
}
}
}
if (visual)
{
while (ptr[col] != NUL && length > 0
&& !vim_isdigit(ptr[col])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
{
int mb_len = MB_PTR2LEN(ptr + col);
col += mb_len;
length -= mb_len;
}
if (length == 0)
goto theend;
if (col > pos->col && ptr[col - 1] == '-'
#ifdef FEAT_MBYTE
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
#endif
)
{
negative = TRUE;
was_positive = FALSE;
}
}
firstdigit = ptr[col];
if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
{
beep_flush();
goto theend;
}
if (doalp && ASCII_ISALPHA(firstdigit))
{
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
{
if (26 - CharOrd(firstdigit) - 1 < Prenum1)
{
if (isupper(firstdigit))
firstdigit = 'Z';
else
firstdigit = 'z';
}
else
#ifdef EBCDIC
firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
#else
firstdigit += Prenum1;
#endif
}
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;
}
else
{
if (col > 0 && ptr[col - 1] == '-'
#ifdef FEAT_MBYTE
&& (!has_mbyte ||
!(*mb_head_off)(ptr, ptr + col - 1))
#endif
&& !visual)
{
--col;
negative = TRUE;
}
if (visual && VIsual_mode != 'V')
maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
? (int)STRLEN(ptr) - col
: length);
vim_str2nr(ptr + col, &pre, &length,
0 + (dobin ? STR2NR_BIN : 0)
+ (dooct ? STR2NR_OCT : 0)
+ (dohex ? STR2NR_HEX : 0),
NULL, &n, maxlen);
if (pre && negative)
{
++col;
--length;
negative = FALSE;
}
subtract = FALSE;
if (op_type == OP_NR_SUB)
subtract ^= TRUE;
if (negative)
subtract ^= TRUE;
oldn = n;
if (subtract)
n -= (uvarnumber_T)Prenum1;
else
n += (uvarnumber_T)Prenum1;
if (!pre)
{
if (subtract)
{
if (n > oldn)
{
n = 1 + (n ^ (uvarnumber_T)-1);
negative ^= TRUE;
}
}
else
{
if (n < oldn)
{
n = (n ^ (uvarnumber_T)-1);
negative ^= TRUE;
}
}
if (n == 0)
negative = FALSE;
}
if (visual && !was_positive && !negative && col > 0)
{
col--;
length++;
}
curwin->w_cursor.col = col;
if (!did_change)
startpos = curwin->w_cursor;
did_change = TRUE;
todel = length;
c = gchar_cursor();
if (c == '-')
--length;
while (todel-- > 0)
{
if (c < 0x100 && isalpha(c))
{
if (isupper(c))
hexupper = TRUE;
else
hexupper = FALSE;
}
(void)del_char(FALSE);
c = gchar_cursor();
}
buf1 = alloc((unsigned)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')
{
*ptr++ = pre;
--length;
}
if (pre == 'b' || pre == 'B')
{
int i;
int bit = 0;
int bits = sizeof(uvarnumber_T) * 8;
for (bit = bits; bit > 0; bit--)
if ((n >> (bit - 1)) & 0x1) break;
for (i = 0; bit > 0; bit--)
buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
buf2[i] = '\0';
}
else if (pre == 0)
vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", n);
else if (pre == '0')
vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", n);
else if (pre && hexupper)
vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", n);
else
vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", n);
length -= (int)STRLEN(buf2);
if (firstdigit == '0' && !(dooct && pre == 0))
while (length-- > 0)
*ptr++ = '0';
*ptr = NUL;
STRCAT(buf1, buf2);
ins_str(buf1);
vim_free(buf1);
endpos = curwin->w_cursor;
if (did_change && curwin->w_cursor.col)
--curwin->w_cursor.col;
}
if (did_change)
{
curbuf->b_op_start = startpos;
curbuf->b_op_end = endpos;
if (curbuf->b_op_end.col > 0)
--curbuf->b_op_end.col;
}
theend:
if (visual)
curwin->w_cursor = save_cursor;
else if (did_change)
curwin->w_set_curswant = TRUE;
return did_change;
}
#ifdef FEAT_VIMINFO
static yankreg_T *y_read_regs = NULL;
#define REG_PREVIOUS 1
#define REG_EXEC 2
void
prepare_viminfo_registers(void)
{
y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS
* (int)sizeof(yankreg_T));
}
void
finish_viminfo_registers(void)
{
int i;
int j;
if (y_read_regs != NULL)
{
for (i = 0; i < NUM_REGISTERS; ++i)
if (y_read_regs[i].y_array != NULL)
{
for (j = 0; j < y_read_regs[i].y_size; j++)
vim_free(y_read_regs[i].y_array[j]);
vim_free(y_read_regs[i].y_array);
}
vim_free(y_read_regs);
y_read_regs = NULL;
}
}
int
read_viminfo_register(vir_T *virp, int force)
{
int eof;
int do_it = TRUE;
int size;
int limit;
int i;
int set_prev = FALSE;
char_u *str;
char_u **array = NULL;
int new_type = MCHAR;
colnr_T new_width = 0;
str = virp->vir_line + 1;
if (*str == '"')
{
set_prev = TRUE;
str++;
}
if (!ASCII_ISALNUM(*str) && *str != '-')
{
if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
return TRUE;
do_it = FALSE;
}
get_yank_register(*str++, FALSE);
if (!force && y_current->y_array != NULL)
do_it = FALSE;
if (*str == '@')
{
if (force || execreg_lastc == NUL)
execreg_lastc = str[-1];
}
size = 0;
limit = 100;
if (do_it)
{
if (set_prev)
y_previous = y_current;
array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
str = skipwhite(skiptowhite(str));
if (STRNCMP(str, "CHAR", 4) == 0)
new_type = MCHAR;
else if (STRNCMP(str, "BLOCK", 5) == 0)
new_type = MBLOCK;
else
new_type = MLINE;
str = skipwhite(skiptowhite(str));
new_width = getdigits(&str);
}
while (!(eof = viminfo_readline(virp))
&& (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
{
if (do_it)
{
if (size == limit)
{
char_u **new_array = (char_u **)
alloc((unsigned)(limit * 2 * sizeof(char_u *)));
if (new_array == NULL)
{
do_it = FALSE;
break;
}
for (i = 0; i < limit; i++)
new_array[i] = array[i];
vim_free(array);
array = new_array;
limit *= 2;
}
str = viminfo_readstring(virp, 1, TRUE);
if (str != NULL)
array[size++] = str;
else
do_it = FALSE;
}
}
if (do_it)
{
for (i = 0; i < y_current->y_size; i++)
vim_free(y_current->y_array[i]);
vim_free(y_current->y_array);
y_current->y_type = new_type;
y_current->y_width = new_width;
y_current->y_size = size;
y_current->y_time_set = 0;
if (size == 0)
{
y_current->y_array = NULL;
}
else
{
y_current->y_array =
(char_u **)alloc((unsigned)(size * sizeof(char_u *)));
for (i = 0; i < size; i++)
{
if (y_current->y_array == NULL)
vim_free(array[i]);
else
y_current->y_array[i] = array[i];
}
}
}
else
{
for (i = 0; i < size; i++)
vim_free(array[i]);
}
vim_free(array);
return eof;
}
void
handle_viminfo_register(garray_T *values, int force)
{
bval_T *vp = (bval_T *)values->ga_data;
int flags;
int name;
int type;
int linecount;
int width;
time_t timestamp;
yankreg_T *y_ptr;
int i;
if (values->ga_len < 6
|| vp[0].bv_type != BVAL_NR
|| vp[1].bv_type != BVAL_NR
|| vp[2].bv_type != BVAL_NR
|| vp[3].bv_type != BVAL_NR
|| vp[4].bv_type != BVAL_NR
|| vp[5].bv_type != BVAL_NR)
return;
flags = vp[0].bv_nr;
name = vp[1].bv_nr;
if (name < 0 || name >= NUM_REGISTERS)
return;
type = vp[2].bv_nr;
if (type != MCHAR && type != MLINE && type != MBLOCK)
return;
linecount = vp[3].bv_nr;
if (values->ga_len < 6 + linecount)
return;
width = vp[4].bv_nr;
if (width < 0)
return;
if (y_read_regs != NULL)
y_ptr = &y_read_regs[name];
else
y_ptr = &y_regs[name];
timestamp = (time_t)vp[5].bv_nr;
if (y_ptr->y_array != NULL && !force
&& (timestamp == 0 || y_ptr->y_time_set > timestamp))
return;
if (y_ptr->y_array != NULL)
for (i = 0; i < y_ptr->y_size; i++)
vim_free(y_ptr->y_array[i]);
vim_free(y_ptr->y_array);
if (y_read_regs == NULL)
{
if (flags & REG_PREVIOUS)
y_previous = y_ptr;
if ((flags & REG_EXEC) && (force || execreg_lastc == NUL))
execreg_lastc = get_register_name(name);
}
y_ptr->y_type = type;
y_ptr->y_width = width;
y_ptr->y_size = linecount;
y_ptr->y_time_set = timestamp;
if (linecount == 0)
y_ptr->y_array = NULL;
else
{
y_ptr->y_array =
(char_u **)alloc((unsigned)(linecount * sizeof(char_u *)));
for (i = 0; i < linecount; i++)
{
if (vp[i + 6].bv_allocated)
{
y_ptr->y_array[i] = vp[i + 6].bv_string;
vp[i + 6].bv_string = NULL;
}
else
y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string);
}
}
}
void
write_viminfo_registers(FILE *fp)
{
int i, j;
char_u *type;
char_u c;
int num_lines;
int max_num_lines;
int max_kbyte;
long len;
yankreg_T *y_ptr;
fputs(_("\n# Registers:\n"), fp);
max_num_lines = get_viminfo_parameter('<');
if (max_num_lines < 0)
max_num_lines = get_viminfo_parameter('"');
if (max_num_lines == 0)
return;
max_kbyte = get_viminfo_parameter('s');
if (max_kbyte == 0)
return;
for (i = 0; i < NUM_REGISTERS; i++)
{
#ifdef FEAT_CLIPBOARD
if (i == STAR_REGISTER || i == PLUS_REGISTER)
continue;
#endif
#ifdef FEAT_DND
if (i == TILDE_REGISTER)
continue;
#endif
if (y_read_regs != NULL
&& y_read_regs[i].y_array != NULL
&& (y_regs[i].y_array == NULL ||
y_read_regs[i].y_time_set > y_regs[i].y_time_set))
y_ptr = &y_read_regs[i];
else if (y_regs[i].y_array == NULL)
continue;
else
y_ptr = &y_regs[i];
num_lines = y_ptr->y_size;
if (num_lines == 0
|| (num_lines == 1 && y_ptr->y_type == MCHAR
&& *y_ptr->y_array[0] == NUL))
continue;
if (max_kbyte > 0)
{
len = 0;
for (j = 0; j < num_lines; j++)
len += (long)STRLEN(y_ptr->y_array[j]) + 1L;
if (len > (long)max_kbyte * 1024L)
continue;
}
switch (y_ptr->y_type)
{
case MLINE:
type = (char_u *)"LINE";
break;
case MCHAR:
type = (char_u *)"CHAR";
break;
case MBLOCK:
type = (char_u *)"BLOCK";
break;
default:
sprintf((char *)IObuff, _("E574: Unknown register type %d"),
y_ptr->y_type);
emsg(IObuff);
type = (char_u *)"LINE";
break;
}
if (y_previous == &y_regs[i])
fprintf(fp, "\"");
c = get_register_name(i);
fprintf(fp, "\"%c", c);
if (c == execreg_lastc)
fprintf(fp, "@");
fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width);
if (max_num_lines > 0 && num_lines > max_num_lines)
num_lines = max_num_lines;
for (j = 0; j < num_lines; j++)
{
putc('\t', fp);
viminfo_writestring(fp, y_ptr->y_array[j]);
}
{
int flags = 0;
int remaining;
if (y_previous == &y_regs[i])
flags |= REG_PREVIOUS;
if (c == execreg_lastc)
flags |= REG_EXEC;
fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags,
i, y_ptr->y_type, num_lines, (int)y_ptr->y_width,
(long)y_ptr->y_time_set);
remaining = LSIZE - 71;
for (j = 0; j < num_lines; j++)
{
putc(',', fp);
--remaining;
remaining = barline_writestring(fp, y_ptr->y_array[j],
remaining);
}
putc('\n', fp);
}
}
}
#endif
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
void
x11_export_final_selection(void)
{
Display *dpy;
char_u *str = NULL;
long_u len = 0;
int motion_type = -1;
# ifdef FEAT_GUI
if (gui.in_use)
dpy = X_DISPLAY;
else
# endif
# ifdef FEAT_XCLIPBOARD
dpy = xterm_dpy;
# else
return;
# endif
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);
if (dpy != NULL && str != NULL && motion_type >= 0
&& len < 1024*1024 && len > 0)
{
#ifdef FEAT_MBYTE
int ok = TRUE;
if (has_mbyte)
{
vimconv_T vc;
vc.vc_type = CONV_NONE;
if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
{
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);
}
else
{
ok = FALSE;
}
}
if (ok)
#endif
{
XStoreBuffer(dpy, (char *)str, (int)len, 0);
XFlush(dpy);
}
}
vim_free(str);
}
#endif
void
clip_free_selection(VimClipboard *cbd)
{
yankreg_T *y_ptr = y_current;
if (cbd == &clip_plus)
y_current = &y_regs[PLUS_REGISTER];
else
y_current = &y_regs[STAR_REGISTER];
free_yank_all();
y_current->y_size = 0;
y_current = y_ptr;
}
void
clip_get_selection(VimClipboard *cbd)
{
yankreg_T *old_y_previous, *old_y_current;
pos_T old_cursor;
pos_T old_visual;
int old_visual_mode;
colnr_T old_curswant;
int old_set_curswant;
pos_T old_op_start, old_op_end;
oparg_T oa;
cmdarg_T ca;
if (cbd->owned)
{
if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
|| (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
return;
old_y_previous = y_previous;
old_y_current = y_current;
old_cursor = curwin->w_cursor;
old_curswant = curwin->w_curswant;
old_set_curswant = curwin->w_set_curswant;
old_op_start = curbuf->b_op_start;
old_op_end = curbuf->b_op_end;
old_visual = VIsual;
old_visual_mode = VIsual_mode;
clear_oparg(&oa);
oa.regname = (cbd == &clip_plus ? '+' : '*');
oa.op_type = OP_YANK;
vim_memset(&ca, 0, sizeof(ca));
ca.oap = &oa;
ca.cmdchar = 'y';
ca.count1 = 1;
ca.retval = CA_NO_ADJ_OP_END;
do_pending_operator(&ca, 0, TRUE);
y_previous = old_y_previous;
y_current = old_y_current;
curwin->w_cursor = old_cursor;
changed_cline_bef_curs();
curwin->w_curswant = old_curswant;
curwin->w_set_curswant = old_set_curswant;
curbuf->b_op_start = old_op_start;
curbuf->b_op_end = old_op_end;
VIsual = old_visual;
VIsual_mode = old_visual_mode;
}
else if (!is_clipboard_needs_update())
{
clip_free_selection(cbd);
clip_gen_request_selection(cbd);
}
}
void
clip_yank_selection(
int type,
char_u *str,
long len,
VimClipboard *cbd)
{
yankreg_T *y_ptr;
if (cbd == &clip_plus)
y_ptr = &y_regs[PLUS_REGISTER];
else
y_ptr = &y_regs[STAR_REGISTER];
clip_free_selection(cbd);
str_to_reg(y_ptr, type, str, len, 0L, FALSE);
}
int
clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd)
{
char_u *p;
int lnum;
int i, j;
int_u eolsize;
yankreg_T *y_ptr;
if (cbd == &clip_plus)
y_ptr = &y_regs[PLUS_REGISTER];
else
y_ptr = &y_regs[STAR_REGISTER];
#ifdef USE_CRNL
eolsize = 2;
#else
eolsize = 1;
#endif
*str = NULL;
*len = 0;
if (y_ptr->y_array == NULL)
return -1;
for (i = 0; i < y_ptr->y_size; i++)
*len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
if (y_ptr->y_type == MCHAR && *len >= eolsize)
*len -= eolsize;
p = *str = lalloc(*len + 1, TRUE);
if (p == NULL)
return -1;
lnum = 0;
for (i = 0, j = 0; i < (int)*len; i++, j++)
{
if (y_ptr->y_array[lnum][j] == '\n')
p[i] = NUL;
else if (y_ptr->y_array[lnum][j] == NUL)
{
#ifdef USE_CRNL
p[i++] = '\r';
#endif
#ifdef USE_CR
p[i] = '\r';
#else
p[i] = '\n';
#endif
lnum++;
j = -1;
}
else
p[i] = y_ptr->y_array[lnum][j];
}
return y_ptr->y_type;
}
static void
may_set_selection(void)
{
if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
{
clip_own_selection(&clip_star);
clip_gen_set_selection(&clip_star);
}
else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
{
clip_own_selection(&clip_plus);
clip_gen_set_selection(&clip_plus);
}
}
#endif
#if defined(FEAT_DND) || defined(PROTO)
void
dnd_yank_drag_data(char_u *str, long len)
{
yankreg_T *curr;
curr = y_current;
y_current = &y_regs[TILDE_REGISTER];
free_yank_all();
str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
y_current = curr;
}
#endif
#if defined(FEAT_EVAL) || defined(PROTO)
char_u
get_reg_type(int regname, long *reglen)
{
switch (regname)
{
case '%':
case '#':
case '=':
case ':':
case '/':
case '.':
#ifdef FEAT_SEARCHPATH
case Ctrl_F:
case Ctrl_P:
#endif
case Ctrl_W:
case Ctrl_A:
case '_':
return MCHAR;
}
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (regname != NUL && !valid_yank_reg(regname, FALSE))
return MAUTO;
get_yank_register(regname, FALSE);
if (y_current->y_array != NULL)
{
if (reglen != NULL && y_current->y_type == MBLOCK)
*reglen = y_current->y_width;
return y_current->y_type;
}
return MAUTO;
}
static char_u *getreg_wrap_one_line(char_u *s, int flags);
static char_u *
getreg_wrap_one_line(char_u *s, int flags)
{
if (flags & GREG_LIST)
{
list_T *list = list_alloc();
if (list != NULL)
{
if (list_append_string(list, NULL, -1) == FAIL)
{
list_free(list);
return NULL;
}
list->lv_first->li_tv.vval.v_string = s;
}
return (char_u *)list;
}
return s;
}
char_u *
get_reg_contents(int regname, int flags)
{
long i;
char_u *retval;
int allocated;
long len;
if (regname == '=')
{
if (flags & GREG_NO_EXPR)
return NULL;
if (flags & GREG_EXPR_SRC)
return getreg_wrap_one_line(get_expr_line_src(), flags);
return getreg_wrap_one_line(get_expr_line(), flags);
}
if (regname == '@')
regname = '"';
if (regname != NUL && !valid_yank_reg(regname, FALSE))
return NULL;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (get_spec_reg(regname, &retval, &allocated, FALSE))
{
if (retval == NULL)
return NULL;
if (allocated)
return getreg_wrap_one_line(retval, flags);
return getreg_wrap_one_line(vim_strsave(retval), flags);
}
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
return NULL;
if (flags & GREG_LIST)
{
list_T *list = list_alloc();
int error = FALSE;
if (list == NULL)
return NULL;
for (i = 0; i < y_current->y_size; ++i)
if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
error = TRUE;
if (error)
{
list_free(list);
return NULL;
}
return (char_u *)list;
}
len = 0;
for (i = 0; i < y_current->y_size; ++i)
{
len += (long)STRLEN(y_current->y_array[i]);
if (y_current->y_type == MLINE || i < y_current->y_size - 1)
++len;
}
retval = lalloc(len + 1, TRUE);
if (retval != NULL)
{
len = 0;
for (i = 0; i < y_current->y_size; ++i)
{
STRCPY(retval + len, y_current->y_array[i]);
len += (long)STRLEN(retval + len);
if (y_current->y_type == MLINE || i < y_current->y_size - 1)
retval[len++] = '\n';
}
retval[len] = NUL;
}
return retval;
}
static int
init_write_reg(
int name,
yankreg_T **old_y_previous,
yankreg_T **old_y_current,
int must_append,
int *yank_type UNUSED)
{
if (!valid_yank_reg(name, TRUE))
{
emsg_invreg(name);
return FAIL;
}
*old_y_previous = y_previous;
*old_y_current = y_current;
get_yank_register(name, TRUE);
if (!y_append && !must_append)
free_yank_all();
return OK;
}
static void
finish_write_reg(
int name,
yankreg_T *old_y_previous,
yankreg_T *old_y_current)
{
# ifdef FEAT_CLIPBOARD
may_set_selection();
# endif
if (name != '"')
y_previous = old_y_previous;
y_current = old_y_current;
}
void
write_reg_contents(
int name,
char_u *str,
int maxlen,
int must_append)
{
write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
}
void
write_reg_contents_lst(
int name,
char_u **strings,
int maxlen UNUSED,
int must_append,
int yank_type,
long block_len)
{
yankreg_T *old_y_previous, *old_y_current;
if (name == '/'
#ifdef FEAT_EVAL
|| name == '='
#endif
)
{
char_u *s;
if (strings[0] == NULL)
s = (char_u *)"";
else if (strings[1] != NULL)
{
EMSG(_("E883: search pattern and expression register may not "
"contain two or more lines"));
return;
}
else
s = strings[0];
write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
return;
}
if (name == '_')
return;
if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
&yank_type) == FAIL)
return;
str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
finish_write_reg(name, old_y_previous, old_y_current);
}
void
write_reg_contents_ex(
int name,
char_u *str,
int maxlen,
int must_append,
int yank_type,
long block_len)
{
yankreg_T *old_y_previous, *old_y_current;
long len;
if (maxlen >= 0)
len = maxlen;
else
len = (long)STRLEN(str);
if (name == '/')
{
set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
return;
}
if (name == '#')
{
buf_T *buf;
if (VIM_ISDIGIT(*str))
{
int num = atoi((char *)str);
buf = buflist_findnr(num);
if (buf == NULL)
EMSGN(_(e_nobufnr), (long)num);
}
else
buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
TRUE, FALSE, FALSE));
if (buf == NULL)
return;
curwin->w_alt_fnum = buf->b_fnum;
return;
}
#ifdef FEAT_EVAL
if (name == '=')
{
char_u *p, *s;
p = vim_strnsave(str, (int)len);
if (p == NULL)
return;
if (must_append)
{
s = concat_str(get_expr_line_src(), p);
vim_free(p);
p = s;
}
set_expr_line(p);
return;
}
#endif
if (name == '_')
return;
if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
&yank_type) == FAIL)
return;
str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
finish_write_reg(name, old_y_previous, old_y_current);
}
#endif
#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
static void
str_to_reg(
yankreg_T *y_ptr,
int yank_type,
char_u *str,
long len,
long blocklen,
int str_list)
{
int type;
int lnum;
long start;
long i;
int extra;
int newlines;
int extraline = 0;
int append = FALSE;
char_u *s;
char_u **ss;
char_u **pp;
long maxlen;
if (y_ptr->y_array == NULL)
y_ptr->y_size = 0;
if (yank_type == MAUTO)
type = ((str_list || (len > 0 && (str[len - 1] == NL
|| str[len - 1] == CAR)))
? MLINE : MCHAR);
else
type = yank_type;
newlines = 0;
if (str_list)
{
for (ss = (char_u **) str; *ss != NULL; ++ss)
++newlines;
}
else
{
for (i = 0; i < len; i++)
if (str[i] == '\n')
++newlines;
if (type == MCHAR || len == 0 || str[len - 1] != '\n')
{
extraline = 1;
++newlines;
}
if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
{
append = TRUE;
--newlines;
}
}
if (y_ptr->y_size + newlines == 0)
{
vim_free(y_ptr->y_array);
y_ptr->y_array = NULL;
return;
}
pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
* sizeof(char_u *), TRUE);
if (pp == NULL)
return;
for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
pp[lnum] = y_ptr->y_array[lnum];
vim_free(y_ptr->y_array);
y_ptr->y_array = pp;
maxlen = 0;
if (str_list)
{
for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
{
i = (long)STRLEN(*ss);
pp[lnum] = vim_strnsave(*ss, i);
if (i > maxlen)
maxlen = i;
}
}
else
{
for (start = 0; start < len + extraline; start += i + 1)
{
for (i = start; i < len; ++i)
if (str[i] == '\n')
break;
i -= start;
if (i > maxlen)
maxlen = i;
if (append)
{
--lnum;
extra = (int)STRLEN(y_ptr->y_array[lnum]);
}
else
extra = 0;
s = alloc((unsigned)(i + extra + 1));
if (s == NULL)
break;
if (extra)
mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
if (append)
vim_free(y_ptr->y_array[lnum]);
if (i)
mch_memmove(s + extra, str + start, (size_t)i);
extra += i;
s[extra] = NUL;
y_ptr->y_array[lnum++] = s;
while (--extra >= 0)
{
if (*s == NUL)
*s = '\n';
++s;
}
append = FALSE;
}
}
y_ptr->y_type = type;
y_ptr->y_size = lnum;
if (type == MBLOCK)
y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
else
y_ptr->y_width = 0;
#ifdef FEAT_VIMINFO
y_ptr->y_time_set = vim_time();
#endif
}
#endif
void
clear_oparg(oparg_T *oap)
{
vim_memset(oap, 0, sizeof(oparg_T));
}
static varnumber_T line_count_info(char_u *line, varnumber_T *wc, varnumber_T *cc, varnumber_T limit, int eol_size);
static varnumber_T
line_count_info(
char_u *line,
varnumber_T *wc,
varnumber_T *cc,
varnumber_T limit,
int eol_size)
{
varnumber_T i;
varnumber_T words = 0;
varnumber_T chars = 0;
int is_word = 0;
for (i = 0; i < limit && line[i] != NUL; )
{
if (is_word)
{
if (vim_isspace(line[i]))
{
words++;
is_word = 0;
}
}
else if (!vim_isspace(line[i]))
is_word = 1;
++chars;
#ifdef FEAT_MBYTE
i += (*mb_ptr2len)(line + i);
#else
++i;
#endif
}
if (is_word)
words++;
*wc += words;
if (i < limit && line[i] == NUL)
{
i += eol_size;
chars += eol_size;
}
*cc += chars;
return i;
}
void
cursor_pos_info(dict_T *dict)
{
char_u *p;
char_u buf1[50];
char_u buf2[40];
linenr_T lnum;
varnumber_T byte_count = 0;
#ifdef FEAT_MBYTE
varnumber_T bom_count = 0;
#endif
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;
int eol_size;
varnumber_T last_check = 100000L;
long line_count_selected = 0;
pos_T min_pos, max_pos;
oparg_T oparg;
struct block_def bd;
if (curbuf->b_ml.ml_flags & ML_EMPTY)
{
if (dict == NULL)
{
MSG(_(no_lines_msg));
return;
}
}
else
{
if (get_fileformat(curbuf) == EOL_DOS)
eol_size = 2;
else
eol_size = 1;
if (VIsual_active)
{
if (LT_POS(VIsual, curwin->w_cursor))
{
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)
{
#ifdef FEAT_LINEBREAK
char_u * saved_sbr = p_sbr;
p_sbr = empty_option;
#endif
oparg.is_VIsual = 1;
oparg.block_mode = TRUE;
oparg.op_type = OP_NOP;
getvcols(curwin, &min_pos, &max_pos,
&oparg.start_vcol, &oparg.end_vcol);
#ifdef FEAT_LINEBREAK
p_sbr = saved_sbr;
#endif
if (curwin->w_curswant == MAXCOL)
oparg.end_vcol = MAXCOL;
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)
{
if (byte_count > last_check)
{
ui_breakcheck();
if (got_int)
return;
last_check = byte_count + 100000L;
}
if (VIsual_active
&& lnum >= min_pos.lnum && lnum <= max_pos.lnum)
{
char_u *s = NULL;
long len = 0L;
switch (VIsual_mode)
{
case Ctrl_V:
#ifdef FEAT_VIRTUALEDIT
virtual_op = virtual_active();
#endif
block_prep(&oparg, &bd, lnum, 0);
#ifdef FEAT_VIRTUALEDIT
virtual_op = MAYBE;
#endif
s = bd.textstart;
len = (long)bd.textlen;
break;
case 'V':
s = ml_get(lnum);
len = MAXCOL;
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;
s = ml_get(lnum) + start_col;
len = end_col;
}
break;
}
if (s != NULL)
{
byte_count_cursor += line_count_info(s, &word_count_cursor,
&char_count_cursor, len, eol_size);
if (lnum == curbuf->b_ml.ml_line_count
&& !curbuf->b_p_eol
&& (curbuf->b_p_bin || !curbuf->b_p_fixeol)
&& (long)STRLEN(s) < len)
byte_count_cursor -= eol_size;
}
}
else
{
if (lnum == curwin->w_cursor.lnum)
{
word_count_cursor += word_count;
char_count_cursor += char_count;
byte_count_cursor = byte_count +
line_count_info(ml_get(lnum),
&word_count_cursor, &char_count_cursor,
(varnumber_T)(curwin->w_cursor.col + 1),
eol_size);
}
}
byte_count += line_count_info(ml_get(lnum), &word_count,
&char_count, (varnumber_T)MAXCOL,
eol_size);
}
if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
byte_count -= eol_size;
if (dict == NULL)
{
if (VIsual_active)
{
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,
_("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
buf1, line_count_selected,
(long)curbuf->b_ml.ml_line_count,
word_count_cursor, word_count,
byte_count_cursor, byte_count);
else
vim_snprintf((char *)IObuff, IOSIZE,
_("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"),
buf1, line_count_selected,
(long)curbuf->b_ml.ml_line_count,
word_count_cursor, word_count,
char_count_cursor, char_count,
byte_count_cursor, byte_count);
}
else
{
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,
_("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
(char *)buf1, (char *)buf2,
(long)curwin->w_cursor.lnum,
(long)curbuf->b_ml.ml_line_count,
word_count_cursor, word_count,
byte_count_cursor, byte_count);
else
vim_snprintf((char *)IObuff, IOSIZE,
_("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
(char *)buf1, (char *)buf2,
(long)curwin->w_cursor.lnum,
(long)curbuf->b_ml.ml_line_count,
word_count_cursor, word_count,
char_count_cursor, char_count,
byte_count_cursor, byte_count);
}
}
#ifdef FEAT_MBYTE
bom_count = bomb_size();
if (bom_count > 0)
vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
_("(+%ld for BOM)"), bom_count);
#endif
if (dict == NULL)
{
p = p_shm;
p_shm = (char_u *)"";
msg(IObuff);
p_shm = p;
}
}
#if defined(FEAT_EVAL)
if (dict != NULL)
{
dict_add_nr_str(dict, "words", word_count, NULL);
dict_add_nr_str(dict, "chars", char_count, NULL);
dict_add_nr_str(dict, "bytes", byte_count
# ifdef FEAT_MBYTE
+ bom_count
# endif
, NULL);
dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
byte_count_cursor, NULL);
dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars",
char_count_cursor, NULL);
dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words",
word_count_cursor, NULL);
}
#endif
}