#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
static struct yankreg
{
char_u **y_array;
linenr_T y_size;
char_u y_type;
char_u y_op_change;
#ifdef FEAT_VISUAL
colnr_T y_width;
#endif
} y_regs[NUM_REGISTERS];
static struct yankreg *y_current;
static int y_append;
static struct yankreg *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 __ARGS((oparg_T *oap, int amount));
static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
#endif
static int stuff_yank __ARGS((int, char_u *));
static void put_reedit_in_typebuf __ARGS((int silent));
static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
int silent));
static void stuffescaped __ARGS((char_u *arg, int literally));
#ifdef FEAT_MBYTE
static void mb_adjust_opend __ARGS((oparg_T *oap));
#endif
static void free_yank __ARGS((long));
static void free_yank_all __ARGS((void));
static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
#ifdef FEAT_CLIPBOARD
static void copy_yank_reg __ARGS((struct yankreg *reg));
# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
static void may_set_selection __ARGS((void));
# endif
#endif
static void dis_msg __ARGS((char_u *p, int skip_esc));
#ifdef FEAT_VISUAL
static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
#endif
#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
#endif
static int ends_in_white __ARGS((linenr_T lnum));
#ifdef FEAT_COMMENTS
static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
#else
static int fmt_check_par __ARGS((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},
};
int
get_op_type(char1, char2)
int char1;
int char2;
{
int i;
if (char1 == 'r')
return OP_REPLACE;
if (char1 == '~')
return OP_TILDE;
for (i = 0; ; ++i)
if (opchars[i][0] == char1 && opchars[i][1] == char2)
break;
return i;
}
#if defined(FEAT_VISUAL) || defined(PROTO)
int
op_on_lines(op)
int op;
{
return opchars[op][2];
}
#endif
int
get_op_char(optype)
int optype;
{
return opchars[optype][0];
}
int
get_extra_op_char(optype)
int optype;
{
return opchars[optype][1];
}
void
op_shift(oap, curs_top, amount)
oparg_T *oap;
int curs_top;
int amount;
{
long i;
int first_char;
char_u *s;
#ifdef FEAT_VISUAL
int block_col = 0;
#endif
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
#ifdef FEAT_VISUAL
if (oap->block_mode)
block_col = curwin->w_cursor.col;
#endif
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);
#ifdef FEAT_VISUAL
if (oap->block_mode)
{
curwin->w_cursor.lnum = oap->start.lnum;
curwin->w_cursor.col = block_col;
}
else
#endif
if (curs_top)
{
curwin->w_cursor.lnum = oap->start.lnum;
beginline(BL_SOL | BL_FIX);
}
else
--curwin->w_cursor.lnum;
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(left, round, amount, call_changed_bytes)
int left;
int round;
int amount;
int call_changed_bytes;
{
int count;
int i, j;
int p_sw = (int)curbuf->b_p_sw;
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(oap, amount)
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)curbuf->b_p_sw;
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 = amount * p_sw;
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)
bd.textstart += (*mb_ptr2len)(bd.textstart);
else
#endif
++bd.textstart;
}
for ( ; vim_iswhite(*bd.textstart); )
{
incr = lbr_chartabsize_adv(&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);
copy_chars(newp + bd.textcol, (size_t)i, TAB);
copy_spaces(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(&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)
{
incr = lbr_chartabsize(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));
copy_spaces(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(oap, s, b_insert, bdp)
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;
}
}
newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
if (newp == NULL)
continue;
mch_memmove(newp, oldp, (size_t)(offset));
oldp += offset;
copy_spaces(newp + offset, (size_t)spaces);
mch_memmove(newp + offset + spaces, s, (size_t)s_len);
offset += s_len;
if (spaces && !bdp->is_short)
{
copy_spaces(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(oap, how)
oparg_T *oap;
int (*how) __ARGS((void));
{
long i;
char_u *l;
int count;
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)
count = 0;
else
count = how();
if (set_indent(count, 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,
#ifdef FEAT_VISUAL
oap->is_VIsual ? start_lnum + oap->line_count :
#endif
last_changed + 1, 0L);
#ifdef FEAT_VISUAL
else if (oap->is_VIsual)
redraw_curbuf_later(INVERTED);
#endif
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()
{
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(new_line)
char_u *new_line;
{
vim_free(expr_line);
expr_line = new_line;
}
char_u *
get_expr_line()
{
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()
{
if (expr_line == NULL)
return NULL;
return vim_strsave(expr_line);
}
#endif
int
valid_yank_reg(regname, writing)
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 == '_'
#ifdef FEAT_CLIPBOARD
|| regname == '*'
|| regname == '+'
#endif
#ifdef FEAT_DND
|| (!writing && regname == '~')
#endif
)
return TRUE;
return FALSE;
}
void
get_yank_register(regname, writing)
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(regname)
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
#if defined(FEAT_VISUAL) || defined(PROTO)
void *
get_register(name, copy)
int name;
int copy;
{
struct yankreg *reg;
int i;
#ifdef FEAT_CLIPBOARD
if (name == '*' && clip_star.available)
{
if (clip_isautosel())
clip_update_selection();
may_get_selection(name);
}
#endif
get_yank_register(name, 0);
reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
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(name, reg)
int name;
void *reg;
{
get_yank_register(name, 0);
free_yank_all();
*y_current = *(struct yankreg *)reg;
vim_free(reg);
# ifdef FEAT_CLIPBOARD
may_set_selection();
# endif
}
#endif
#if defined(FEAT_MOUSE) || defined(PROTO)
int
yank_register_mline(regname)
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(c)
int c;
{
char_u *p;
static int regname;
struct yankreg *old_y_previous, *old_y_current;
int retval;
if (Recording == FALSE)
{
if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
retval = FAIL;
else
{
Recording = TRUE;
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(regname, p)
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;
}
return OK;
}
static int execreg_lastc = NUL;
int
do_execreg(regname, colon, addcr, silent)
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(silent)
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(s, esc, colon, silent)
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(regname, literally)
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(arg, literally)
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(regname, argp, allocated, errmsg)
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(regname, literally, remcr)
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
&& i == y_current->y_size - 2
&& *y_current->y_array[i + 1] == NUL)))
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(rp)
int *rp;
{
if (*rp == 0 && clip_unnamed)
*rp = '*';
if (!clip_star.available && *rp == '*')
*rp = 0;
if (!clip_plus.available && *rp == '+')
*rp = 0;
}
#endif
int
op_delete(oap)
oparg_T *oap;
{
int n;
linenr_T lnum;
char_u *ptr;
#ifdef FEAT_VISUAL
char_u *newp, *oldp;
struct block_def bd;
#endif
linenr_T old_lcount = curbuf->b_ml.ml_line_count;
int did_yank = FALSE;
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
#ifdef FEAT_VISUAL
&& !oap->is_VIsual
&& !oap->block_mode
#endif
&& oap->line_count > 1
&& oap->op_type == OP_DELETE)
{
ptr = ml_get(oap->end.lnum) + oap->end.col + 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 (oap->regname != 0 || oap->motion_type == MLINE
|| oap->line_count > 1 || oap->use_reg_one)
{
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;
if (op_yank(oap, TRUE, FALSE) == OK)
did_yank = TRUE;
}
if (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;
}
}
}
#ifdef FEAT_VISUAL
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);
copy_spaces(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
#endif
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
#ifdef FEAT_VISUAL
&& !oap->is_VIsual
#endif
)
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
#ifdef FEAT_VISUAL
&& !oap->is_VIsual
#endif
);
}
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);
curwin->w_cursor.col = 0;
(void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
!virtual_op, oap->op_type == OP_DELETE
#ifdef FEAT_VISUAL
&& !oap->is_VIsual
#endif
);
curwin->w_cursor = curpos;
(void)do_join(2, FALSE, FALSE);
}
}
msgmore(curbuf->b_ml.ml_line_count - old_lcount);
#ifdef FEAT_VIRTUALEDIT
setmarks:
#endif
#ifdef FEAT_VISUAL
if (oap->block_mode)
{
curbuf->b_op_end.lnum = oap->end.lnum;
curbuf->b_op_end.col = oap->start.col;
}
else
#endif
curbuf->b_op_end = oap->start;
curbuf->b_op_start = oap->start;
return OK;
}
#ifdef FEAT_MBYTE
static void
mb_adjust_opend(oap)
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(oap, c)
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;
if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
return OK;
#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;
copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
#ifdef FEAT_MBYTE
if (has_mbyte)
{
n = (int)STRLEN(newp);
while (--num_chars >= 0)
n += (*mb_char2bytes)(c, newp + n);
}
else
#endif
copy_chars(newp + STRLEN(newp), (size_t)numc, c);
if (!bd.is_short)
{
copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
STRMOVE(newp + STRLEN(newp), oldp);
}
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
}
}
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(curwin->w_cursor, oap->end))
{
n = gchar_cursor();
if (n != NUL)
{
#ifdef FEAT_MBYTE
if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
{
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 __ARGS((int op_type, pos_T *pos, int length));
void
op_tilde(oap)
oparg_T *oap;
{
pos_T pos;
#ifdef FEAT_VISUAL
struct block_def bd;
#endif
int did_change = FALSE;
if (u_save((linenr_T)(oap->start.lnum - 1),
(linenr_T)(oap->end.lnum + 1)) == FAIL)
return;
pos = oap->start;
#ifdef FEAT_VISUAL
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
#endif
{
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(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
}
}
#ifdef FEAT_VISUAL
if (!did_change && oap->is_VIsual)
redraw_curbuf_later(INVERTED);
#endif
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(op_type, pos, length)
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)
todo -= (*mb_ptr2len)(ml_get_pos(pos)) - 1;
# endif
did_change |= swapchar(op_type, pos);
if (inc(pos) == -1)
break;
}
return did_change;
}
int
swapchar(op_type, pos)
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(oap, count1)
oparg_T *oap;
long count1;
{
long ins_len, pre_textlen = 0;
char_u *firstline, *ins_text;
struct block_def bd;
int i;
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);
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();
}
}
edit(NUL, FALSE, (linenr_T)count1);
if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
return;
if (oap->block_mode)
{
struct block_def bd2;
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 ((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(oap)
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)(skipwhite(firstline) - 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)(skipwhite(firstline) - 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
copy_spaces(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()
{
int i;
for (i = 0; i < NUM_REGISTERS; ++i)
y_regs[i].y_array = NULL;
}
#if defined(EXITFREE) || defined(PROTO)
void
clear_registers()
{
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(n)
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()
{
free_yank(y_current->y_size);
}
int
op_yank(oap, deleting, mess)
oparg_T *oap;
int deleting;
int mess;
{
long y_idx;
struct yankreg *curr;
struct yankreg 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 (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
#ifdef FEAT_VISUAL
&& (!oap->is_VIsual || *p_sel == 'o')
&& !oap->block_mode
#endif
&& 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);
#ifdef FEAT_VISUAL
y_current->y_width = 0;
#endif
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;
}
y_idx = 0;
lnum = oap->start.lnum;
#ifdef FEAT_VISUAL
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--;
}
#endif
for ( ; lnum <= yankendlnum; lnum++, y_idx++)
{
switch (y_current->y_type)
{
#ifdef FEAT_VISUAL
case MBLOCK:
block_prep(oap, &bd, lnum, FALSE);
if (yank_copy_line(&bd, y_idx) == FAIL)
goto fail;
break;
#endif
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 (startcol > endcol
#ifdef FEAT_VIRTUALEDIT
|| is_oneChar
#endif
)
bd.textlen = 0;
else
{
if (endcol == MAXCOL)
endcol = (colnr_T)STRLEN(p);
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;
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 (mess)
{
if (yanktype == MCHAR
#ifdef FEAT_VISUAL
&& !oap->block_mode
#endif
&& yanklines == 1)
yanklines = 0;
if (yanklines > p_report)
{
update_topline_redraw();
if (yanklines == 1)
{
#ifdef FEAT_VISUAL
if (oap->block_mode)
MSG(_("block of 1 line yanked"));
else
#endif
MSG(_("1 line yanked"));
}
#ifdef FEAT_VISUAL
else if (oap->block_mode)
smsg((char_u *)_("block of %ld lines yanked"), yanklines);
#endif
else
smsg((char_u *)_("%ld lines yanked"), yanklines);
}
}
curbuf->b_op_start = oap->start;
curbuf->b_op_end = oap->end;
if (yanktype == MLINE
#ifdef FEAT_VISUAL
&& !oap->block_mode
#endif
)
{
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)))
{
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
else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
{
clip_own_selection(&clip_plus);
clip_gen_set_selection(&clip_plus);
if (!clip_isautosel())
{
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(bd, y_idx)
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;
copy_spaces(pnew, (size_t)bd->startspaces);
pnew += bd->startspaces;
mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
pnew += bd->textlen;
copy_spaces(pnew, (size_t)bd->endspaces);
pnew += bd->endspaces;
*pnew = NUL;
return OK;
}
#ifdef FEAT_CLIPBOARD
static void
copy_yank_reg(reg)
struct yankreg *reg;
{
struct yankreg *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(regname, dir, count, flags)
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;
#ifdef FEAT_VISUAL
int oldlen;
long y_width = 0;
colnr_T vcol;
int delcount;
int incr = 0;
long j;
struct block_def bd;
#endif
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 == '.')
{
(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;
}
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;
#ifdef FEAT_VISUAL
y_width = y_current->y_width;
#endif
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;
}
#ifdef FEAT_VISUAL
if (y_type == MLINE)
{
if (flags & PUT_LINE_SPLIT)
{
if (u_save_cursor() == FAIL)
goto end;
ptr = vim_strsave(ml_get_cursor());
if (ptr == NULL)
goto end;
ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
vim_free(ptr);
ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
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;
}
#endif
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;
}
#ifdef FEAT_VISUAL
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
#endif
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 (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;
#ifdef FEAT_VISUAL
if (y_type == MBLOCK)
{
char 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(&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(&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;
copy_spaces(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)
{
copy_spaces(ptr, (size_t)spaces);
ptr += spaces;
}
}
copy_spaces(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
#endif
{
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)
{
totlen = count * yanklen;
if (totlen)
{
oldp = ml_get(lnum);
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);
curwin->w_cursor.col += (colnr_T)(totlen - 1);
}
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++;
}
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);
adjust_cursor_eol();
}
void
adjust_cursor_eol()
{
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()
{
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))
# endif
;
}
#endif
int
get_register_name(num)
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(eap)
exarg_T *eap;
{
int i, n;
long j;
char_u *p;
struct yankreg *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)
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(p, skip_esc)
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();
}
int
do_join(count, insert_space, save_undo)
long count;
int insert_space;
int save_undo;
{
char_u *curr = 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 (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;
for (t = 0; t < count; ++t)
{
curr = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
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];
copy_spaces(cend, (size_t)(spaces[t]));
}
mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
(long)(cend - newp + spaces[t]));
if (t == 0)
break;
curr = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
if (insert_space && t > 1)
curr = skipwhite(curr);
currsize = (int)STRLEN(curr);
}
ml_replace(curwin->w_cursor.lnum, newp, FALSE);
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);
return ret;
}
#ifdef FEAT_COMMENTS
static int
same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
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(oap, keep_cursor)
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;
#ifdef FEAT_VISUAL
if (oap->is_VIsual)
redraw_curbuf_later(INVERTED);
#endif
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;
}
#ifdef FEAT_VISUAL
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;
}
}
}
#endif
}
#if defined(FEAT_EVAL) || defined(PROTO)
void
op_formatexpr(oap)
oparg_T *oap;
{
# ifdef FEAT_VISUAL
if (oap->is_VIsual)
redraw_curbuf_later(INVERTED);
# endif
if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
op_format(oap, FALSE);
}
int
fex_format(lnum, count, c)
linenr_T lnum;
long count;
int c;
{
int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
OPT_LOCAL);
int r;
set_vim_var_nr(VV_LNUM, lnum);
set_vim_var_nr(VV_COUNT, count);
set_vim_var_char(c);
if (use_sandbox)
++sandbox;
r = eval_to_number(curbuf->b_p_fex);
if (use_sandbox)
--sandbox;
set_vim_var_string(VV_CHAR, NULL, -1);
return r;
}
#endif
void
format_lines(line_count, avoid_fex)
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;
#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
#ifdef FEAT_COMMENTS
&& leader_len == 0
&& next_leader_len == 0
#endif
)
{
if (do_second_indent
&& !lineempty(curwin->w_cursor.lnum + 1))
second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
else if (do_number_indent)
second_indent = get_number_indent(curwin->w_cursor.lnum);
}
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)
#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
(void)del_bytes((long)next_leader_len, FALSE, FALSE);
if (next_leader_len > 0)
mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
(long)-next_leader_len);
#endif
curwin->w_cursor.lnum--;
if (do_join(2, TRUE, 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(lnum)
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(lnum, leader_len, leader_flags, do_comments)
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);
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(lnum)
linenr_T lnum;
{
return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
}
#endif
int
paragraph_start(lnum)
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;
}
#ifdef FEAT_VISUAL
static void
block_prep(oap, bdp, lnum, is_del)
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(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(&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;
}
#endif
#ifdef FEAT_RIGHTLEFT
static void reverse_line __ARGS((char_u *s));
static void
reverse_line(s)
char_u *s;
{
int i, j;
char_u c;
if ((i = (int)STRLEN(s) - 1) <= 0)
return;
curwin->w_cursor.col = i - curwin->w_cursor.col;
for (j = 0; j < i; j++, i--)
{
c = s[i]; s[i] = s[j]; s[j] = c;
}
}
# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
#else
# define RLADDSUBFIX(ptr)
#endif
int
do_addsub(command, Prenum1)
int command;
linenr_T Prenum1;
{
int col;
char_u *buf1;
char_u buf2[NUMBUFLEN];
int hex;
static int hexupper = FALSE;
unsigned long n;
long_u oldn;
char_u *ptr;
int c;
int length = 0;
int todel;
int dohex;
int dooct;
int doalp;
int firstdigit;
int negative;
int subtract;
dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL);
dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL);
doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL);
ptr = ml_get_curline();
RLADDSUBFIX(ptr);
col = curwin->w_cursor.col;
if (dohex)
while (col > 0 && vim_isxdigit(ptr[col]))
--col;
if ( dohex
&& col > 0
&& (ptr[col] == 'X'
|| ptr[col] == 'x')
&& ptr[col - 1] == '0'
&& vim_isxdigit(ptr[col + 1]))
{
--col;
}
else
{
col = curwin->w_cursor.col;
while (ptr[col] != NUL
&& !vim_isdigit(ptr[col])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
++col;
while (col > 0
&& vim_isdigit(ptr[col - 1])
&& !(doalp && ASCII_ISALPHA(ptr[col])))
--col;
}
firstdigit = ptr[col];
RLADDSUBFIX(ptr);
if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
|| u_save_cursor() != OK)
{
beep_flush();
return FAIL;
}
ptr = ml_get_curline();
RLADDSUBFIX(ptr);
if (doalp && ASCII_ISALPHA(firstdigit))
{
if (command == Ctrl_X)
{
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;
(void)del_char(FALSE);
ins_char(firstdigit);
}
else
{
negative = FALSE;
if (col > 0 && ptr[col - 1] == '-')
{
--col;
negative = TRUE;
}
vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
if (hex && negative)
{
++col;
--length;
negative = FALSE;
}
subtract = FALSE;
if (command == Ctrl_X)
subtract ^= TRUE;
if (negative)
subtract ^= TRUE;
oldn = n;
if (subtract)
n -= (unsigned long)Prenum1;
else
n += (unsigned long)Prenum1;
if (!hex)
{
if (subtract)
{
if (n > oldn)
{
n = 1 + (n ^ (unsigned long)-1);
negative ^= TRUE;
}
}
else
{
if (n < oldn)
{
n = (n ^ (unsigned long)-1);
negative ^= TRUE;
}
}
if (n == 0)
negative = FALSE;
}
curwin->w_cursor.col = col;
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)
return FAIL;
ptr = buf1;
if (negative)
{
*ptr++ = '-';
}
if (hex)
{
*ptr++ = '0';
--length;
}
if (hex == 'x' || hex == 'X')
{
*ptr++ = hex;
--length;
}
if (hex == 0)
sprintf((char *)buf2, "%lu", n);
else if (hex == '0')
sprintf((char *)buf2, "%lo", n);
else if (hex && hexupper)
sprintf((char *)buf2, "%lX", n);
else
sprintf((char *)buf2, "%lx", n);
length -= (int)STRLEN(buf2);
if (firstdigit == '0' && !(dooct && hex == 0))
while (length-- > 0)
*ptr++ = '0';
*ptr = NUL;
STRCAT(buf1, buf2);
ins_str(buf1);
vim_free(buf1);
}
--curwin->w_cursor.col;
curwin->w_set_curswant = TRUE;
#ifdef FEAT_RIGHTLEFT
ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
RLADDSUBFIX(ptr);
#endif
return OK;
}
#ifdef FEAT_VIMINFO
int
read_viminfo_register(virp, force)
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;
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;
vim_free(y_current->y_array);
array = y_current->y_array =
(char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
str = skipwhite(skiptowhite(str));
if (STRNCMP(str, "CHAR", 4) == 0)
y_current->y_type = MCHAR;
#ifdef FEAT_VISUAL
else if (STRNCMP(str, "BLOCK", 5) == 0)
y_current->y_type = MBLOCK;
#endif
else
y_current->y_type = MLINE;
str = skipwhite(skiptowhite(str));
#ifdef FEAT_VISUAL
y_current->y_width = getdigits(&str);
#else
(void)getdigits(&str);
#endif
}
while (!(eof = viminfo_readline(virp))
&& (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
{
if (do_it)
{
if (size >= limit)
{
y_current->y_array = (char_u **)
alloc((unsigned)(limit * 2 * sizeof(char_u *)));
for (i = 0; i < limit; i++)
y_current->y_array[i] = array[i];
vim_free(array);
limit *= 2;
array = y_current->y_array;
}
str = viminfo_readstring(virp, 1, TRUE);
if (str != NULL)
array[size++] = str;
else
do_it = FALSE;
}
}
if (do_it)
{
if (size == 0)
{
vim_free(array);
y_current->y_array = NULL;
}
else if (size < limit)
{
y_current->y_array =
(char_u **)alloc((unsigned)(size * sizeof(char_u *)));
for (i = 0; i < size; i++)
y_current->y_array[i] = array[i];
vim_free(array);
}
y_current->y_size = size;
}
return eof;
}
void
write_viminfo_registers(fp)
FILE *fp;
{
int i, j;
char_u *type;
char_u c;
int num_lines;
int max_num_lines;
int max_kbyte;
long len;
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++)
{
if (y_regs[i].y_array == NULL)
continue;
#ifdef FEAT_CLIPBOARD
if (i == STAR_REGISTER || i == PLUS_REGISTER)
continue;
#endif
#ifdef FEAT_DND
if (i == TILDE_REGISTER)
continue;
#endif
num_lines = y_regs[i].y_size;
if (num_lines == 0
|| (num_lines == 1 && y_regs[i].y_type == MCHAR
&& *y_regs[i].y_array[0] == NUL))
continue;
if (max_kbyte > 0)
{
len = 0;
for (j = 0; j < num_lines; j++)
len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
if (len > (long)max_kbyte * 1024L)
continue;
}
switch (y_regs[i].y_type)
{
case MLINE:
type = (char_u *)"LINE";
break;
case MCHAR:
type = (char_u *)"CHAR";
break;
#ifdef FEAT_VISUAL
case MBLOCK:
type = (char_u *)"BLOCK";
break;
#endif
default:
sprintf((char *)IObuff, _("E574: Unknown register type %d"),
y_regs[i].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,
#ifdef FEAT_VISUAL
(int)y_regs[i].y_width
#else
0
#endif
);
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_regs[i].y_array[j]);
}
}
}
#endif
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
void
x11_export_final_selection()
{
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
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;
conv_str = string_convert(&vc, str, &intlen);
len = intlen;
if (conv_str != NULL)
{
vim_free(str);
str = conv_str;
}
convert_setup(&vc, NULL, NULL);
}
}
#endif
XStoreBuffer(dpy, (char *)str, (int)len, 0);
XFlush(dpy);
}
vim_free(str);
}
#endif
void
clip_free_selection(cbd)
VimClipboard *cbd;
{
struct yankreg *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(cbd)
VimClipboard *cbd;
{
struct yankreg *old_y_previous, *old_y_current;
pos_T old_cursor;
#ifdef FEAT_VISUAL
pos_T old_visual;
int old_visual_mode;
#endif
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;
#ifdef FEAT_VISUAL
old_visual = VIsual;
old_visual_mode = VIsual_mode;
#endif
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;
#ifdef FEAT_VISUAL
VIsual = old_visual;
VIsual_mode = old_visual_mode;
#endif
}
else
{
clip_free_selection(cbd);
clip_gen_request_selection(cbd);
}
}
void
clip_yank_selection(type, str, len, cbd)
int type;
char_u *str;
long len;
VimClipboard *cbd;
{
struct yankreg *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);
}
int
clip_convert_selection(str, len, cbd)
char_u **str;
long_u *len;
VimClipboard *cbd;
{
char_u *p;
int lnum;
int i, j;
int_u eolsize;
struct yankreg *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;
}
# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
static void
may_set_selection()
{
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
#endif
#if defined(FEAT_DND) || defined(PROTO)
void
dnd_yank_drag_data(str, len)
char_u *str;
long len;
{
struct yankreg *curr;
curr = y_current;
y_current = &y_regs[TILDE_REGISTER];
free_yank_all();
str_to_reg(y_current, MCHAR, str, len, 0L);
y_current = curr;
}
#endif
#if defined(FEAT_EVAL) || defined(PROTO)
char_u
get_reg_type(regname, reglen)
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
get_yank_register(regname, FALSE);
if (y_current->y_array != NULL)
{
#ifdef FEAT_VISUAL
if (reglen != NULL && y_current->y_type == MBLOCK)
*reglen = y_current->y_width;
#endif
return y_current->y_type;
}
return MAUTO;
}
char_u *
get_reg_contents(regname, allowexpr, expr_src)
int regname;
int allowexpr;
int expr_src;
{
long i;
char_u *retval;
int allocated;
long len;
if (regname == '=')
{
if (allowexpr)
{
if (expr_src)
return get_expr_line_src();
return get_expr_line();
}
return NULL;
}
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)
retval = vim_strsave(retval);
return retval;
}
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
return NULL;
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;
}
void
write_reg_contents(name, str, maxlen, must_append)
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_ex(name, str, maxlen, must_append, yank_type, block_len)
int name;
char_u *str;
int maxlen;
int must_append;
int yank_type;
long block_len;
{
struct yankreg *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;
}
#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 (!valid_yank_reg(name, TRUE))
{
emsg_invreg(name);
return;
}
if (name == '_')
return;
old_y_previous = y_previous;
old_y_current = y_current;
get_yank_register(name, TRUE);
if (!y_append && !must_append)
free_yank_all();
#ifndef FEAT_VISUAL
if (yank_type == MBLOCK)
yank_type = MAUTO;
#endif
if (yank_type == MAUTO)
yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
? MLINE : MCHAR);
str_to_reg(y_current, yank_type, str, len, block_len);
# ifdef FEAT_CLIPBOARD
may_set_selection();
# endif
if (name != '"')
y_previous = old_y_previous;
y_current = old_y_current;
}
#endif
#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
static void
str_to_reg(y_ptr, type, str, len, blocklen)
struct yankreg *y_ptr;
int type;
char_u *str;
long len;
long blocklen;
{
int lnum;
long start;
long i;
int extra;
int newlines;
int extraline = 0;
int append = FALSE;
char_u *s;
char_u **pp;
#ifdef FEAT_VISUAL
long maxlen;
#endif
if (y_ptr->y_array == NULL)
y_ptr->y_size = 0;
newlines = 0;
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;
}
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;
#ifdef FEAT_VISUAL
maxlen = 0;
#endif
for (start = 0; start < len + extraline; start += i + 1)
{
for (i = start; i < len; ++i)
if (str[i] == '\n')
break;
i -= start;
#ifdef FEAT_VISUAL
if (i > maxlen)
maxlen = i;
#endif
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;
# ifdef FEAT_VISUAL
if (type == MBLOCK)
y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
else
y_ptr->y_width = 0;
# endif
}
#endif
void
clear_oparg(oap)
oparg_T *oap;
{
vim_memset(oap, 0, sizeof(oparg_T));
}
static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
static long
line_count_info(line, wc, cc, limit, eol_size)
char_u *line;
long *wc;
long *cc;
long limit;
int eol_size;
{
long i;
long words = 0;
long chars = 0;
int is_word = 0;
for (i = 0; line[i] && i < limit; )
{
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 (line[i] == NUL && i < limit)
{
i += eol_size;
chars += eol_size;
}
*cc += chars;
return i;
}
void
cursor_pos_info()
{
char_u *p;
char_u buf1[50];
char_u buf2[40];
linenr_T lnum;
long byte_count = 0;
long byte_count_cursor = 0;
long char_count = 0;
long char_count_cursor = 0;
long word_count = 0;
long word_count_cursor = 0;
int eol_size;
long last_check = 100000L;
#ifdef FEAT_VISUAL
long line_count_selected = 0;
pos_T min_pos, max_pos;
oparg_T oparg;
struct block_def bd;
#endif
if (curbuf->b_ml.ml_flags & ML_EMPTY)
{
MSG(_(no_lines_msg));
}
else
{
if (get_fileformat(curbuf) == EOL_DOS)
eol_size = 2;
else
eol_size = 1;
#ifdef FEAT_VISUAL
if (VIsual_active)
{
if (lt(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;
}
#endif
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;
}
#ifdef FEAT_VISUAL
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
&& (long)STRLEN(s) < len)
byte_count_cursor -= eol_size;
}
}
else
#endif
{
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,
(long)(curwin->w_cursor.col + 1), eol_size);
}
}
byte_count += line_count_info(ml_get(lnum), &word_count,
&char_count, (long)MAXCOL, eol_size);
}
if (!curbuf->b_p_eol && curbuf->b_p_bin)
byte_count -= eol_size;
#ifdef FEAT_VISUAL
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; %ld of %ld Words; %ld of %ld 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; %ld of %ld Words; %ld of %ld Chars; %ld of %ld 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
#endif
{
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 %ld of %ld; Byte %ld of %ld"),
(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 %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
(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
byte_count = bomb_size();
if (byte_count > 0)
sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
byte_count);
#endif
p = p_shm;
p_shm = (char_u *)"";
msg(IObuff);
p_shm = p;
}
}