blob: 89a25b19a5cee4949035bdf72cdd9af32d28169d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020027static yankreg_T y_regs[NUM_REGISTERS];
28
29static yankreg_T *y_current; /* ptr to current yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int y_append; /* TRUE when appending */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020031static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33/*
34 * structure used by block_prep, op_delete and op_yank for blockwise operators
35 * also op_change, op_shift, op_insert, op_replace - AKelly
36 */
37struct block_def
38{
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000039 int startspaces; /* 'extra' cols before first char */
40 int endspaces; /* 'extra' cols after last char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 int textlen; /* chars in block */
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +000042 char_u *textstart; /* pointer to 1st char (partially) in block */
43 colnr_T textcol; /* index of chars (partially) in block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 colnr_T start_vcol; /* start col of 1st char wholly inside block */
45 colnr_T end_vcol; /* start col of 1st char wholly after block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 int is_short; /* TRUE if line is too short to fit in block */
47 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
48 int is_oneChar; /* TRUE if block within one character */
49 int pre_whitesp; /* screen cols of ws before block */
50 int pre_whitesp_c; /* chars of ws before block */
51 colnr_T end_char_vcols; /* number of vcols of post-block char */
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 colnr_T start_char_vcols; /* number of vcols of pre-block char */
53};
54
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void shift_block(oparg_T *oap, int amount);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010056static int stuff_yank(int, char_u *);
57static void put_reedit_in_typebuf(int silent);
58static int put_in_typebuf(char_u *s, int esc, int colon,
59 int silent);
60static void stuffescaped(char_u *arg, int literally);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static void mb_adjust_opend(oparg_T *oap);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static void free_yank_all(void);
63static int yank_copy_line(struct block_def *bd, long y_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_CLIPBOARD
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020065static void copy_yank_reg(yankreg_T *reg);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010066static void may_set_selection(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020068#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
69static int preprocs_left(void);
70#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010071static void dis_msg(char_u *p, int skip_esc);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010072static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int);
73static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +020075static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list);
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static int ends_in_white(linenr_T lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#ifdef FEAT_COMMENTS
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010079static int fmt_check_par(linenr_T, int *, char_u **, int do_comments);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#else
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010081static int fmt_check_par(linenr_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
83
Bram Moolenaarf2732452018-06-03 14:47:35 +020084// Flags for third item in "opchars".
85#define OPF_LINES 1 // operator always works on lines
86#define OPF_CHANGE 2 // operator changes text
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088/*
89 * The names of operators.
90 * IMPORTANT: Index must correspond with defines in vim.h!!!
Bram Moolenaarf2732452018-06-03 14:47:35 +020091 * The third field holds OPF_ flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
93static char opchars[][3] =
94{
Bram Moolenaarf2732452018-06-03 14:47:35 +020095 {NUL, NUL, 0}, // OP_NOP
96 {'d', NUL, OPF_CHANGE}, // OP_DELETE
97 {'y', NUL, 0}, // OP_YANK
98 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
99 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
100 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
101 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
102 {'g', '~', OPF_CHANGE}, // OP_TILDE
103 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
104 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
105 {':', NUL, OPF_LINES}, // OP_COLON
106 {'g', 'U', OPF_CHANGE}, // OP_UPPER
107 {'g', 'u', OPF_CHANGE}, // OP_LOWER
108 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
109 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
110 {'g', '?', OPF_CHANGE}, // OP_ROT13
111 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
112 {'I', NUL, OPF_CHANGE}, // OP_INSERT
113 {'A', NUL, OPF_CHANGE}, // OP_APPEND
114 {'z', 'f', OPF_LINES}, // OP_FOLD
115 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
116 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
117 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
118 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
119 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
120 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
121 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
122 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
123 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
124 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125};
126
Bram Moolenaarc3328162019-07-23 22:15:25 +0200127 yankreg_T *
128get_y_regs(void)
129{
130 return y_regs;
131}
132
133 yankreg_T *
134get_y_current(void)
135{
136 return y_current;
137}
138
139 yankreg_T *
140get_y_previous(void)
141{
142 return y_previous;
143}
144
145 void
146set_y_previous(yankreg_T *yreg)
147{
148 y_previous = yreg;
149}
150
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * Translate a command name into an operator type.
154 * Must only be called with a valid operator name!
155 */
156 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100157get_op_type(int char1, int char2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158{
159 int i;
160
161 if (char1 == 'r') /* ignore second character */
162 return OP_REPLACE;
163 if (char1 == '~') /* when tilde is an operator */
164 return OP_TILDE;
Bram Moolenaard79e5502016-01-10 22:13:02 +0100165 if (char1 == 'g' && char2 == Ctrl_A) /* add */
166 return OP_NR_ADD;
167 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */
168 return OP_NR_SUB;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 for (i = 0; ; ++i)
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (opchars[i][0] == char1 && opchars[i][1] == char2)
172 break;
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100173 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1))
174 {
175 internal_error("get_op_type()");
176 break;
177 }
178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 return i;
180}
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Return TRUE if operator "op" always works on whole lines.
184 */
185 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100186op_on_lines(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
Bram Moolenaarf2732452018-06-03 14:47:35 +0200188 return opchars[op][2] & OPF_LINES;
189}
190
Bram Moolenaar113e1072019-01-20 15:30:40 +0100191#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf2732452018-06-03 14:47:35 +0200192/*
193 * Return TRUE if operator "op" changes text.
194 */
195 int
196op_is_change(int op)
197{
198 return opchars[op][2] & OPF_CHANGE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202/*
203 * Get first operator command character.
204 * Returns 'g' or 'z' if there is another command character.
205 */
206 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100207get_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 return opchars[optype][0];
210}
211
212/*
213 * Get second operator command character.
214 */
215 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100216get_extra_op_char(int optype)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 return opchars[optype][1];
219}
220
221/*
222 * op_shift - handle a shift operation
223 */
224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100225op_shift(oparg_T *oap, int curs_top, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226{
227 long i;
228 int first_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 int block_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231 if (u_save((linenr_T)(oap->start.lnum - 1),
232 (linenr_T)(oap->end.lnum + 1)) == FAIL)
233 return;
234
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (oap->block_mode)
236 block_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
238 for (i = oap->line_count; --i >= 0; )
239 {
240 first_char = *ml_get_curline();
241 if (first_char == NUL) /* empty line */
242 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 else if (oap->block_mode)
244 shift_block(oap, amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 else
246 /* Move the line right if it doesn't start with '#', 'smartindent'
247 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
248#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
249 if (first_char != '#' || !preprocs_left())
250#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000251 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 ++curwin->w_cursor.lnum;
253 }
254
255 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 if (oap->block_mode)
257 {
258 curwin->w_cursor.lnum = oap->start.lnum;
259 curwin->w_cursor.col = block_col;
260 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100261 else if (curs_top) /* put cursor on first line, for ">>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 curwin->w_cursor.lnum = oap->start.lnum;
264 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
265 }
266 else
267 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
268
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +0100269#ifdef FEAT_FOLDING
270 /* The cursor line is not in a closed fold */
271 foldOpenCursor();
272#endif
273
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 if (oap->line_count > p_report)
276 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200277 char *op;
278 char *msg_line_single;
279 char *msg_line_plural;
280
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (oap->op_type == OP_RSHIFT)
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200282 op = ">";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200284 op = "<";
285 msg_line_single = NGETTEXT("%ld line %sed %d time",
286 "%ld line %sed %d times", amount);
287 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
288 "%ld lines %sed %d times", amount);
289 vim_snprintf((char *)IObuff, IOSIZE,
290 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
291 oap->line_count, op, amount);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100292 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 }
294
295 /*
296 * Set "'[" and "']" marks.
297 */
298 curbuf->b_op_start = oap->start;
299 curbuf->b_op_end.lnum = oap->end.lnum;
300 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
301 if (curbuf->b_op_end.col > 0)
302 --curbuf->b_op_end.col;
303}
304
305/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +0100306 * Shift the current line one shiftwidth left (if left != 0) or right
307 * leaves cursor on first blank in the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 */
309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100310shift_line(
311 int left,
312 int round,
313 int amount,
314 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315{
316 int count;
317 int i, j;
Bram Moolenaardac13472019-09-16 21:06:21 +0200318 int sw_val = (int)get_sw_value_indent(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319
320 count = get_indent(); /* get current indent */
321
322 if (round) /* round off indent */
323 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200324 i = count / sw_val; /* number of p_sw rounded down */
325 j = count % sw_val; /* extra spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 if (j && left) /* first remove extra spaces */
327 --amount;
328 if (left)
329 {
330 i -= amount;
331 if (i < 0)
332 i = 0;
333 }
334 else
335 i += amount;
Bram Moolenaardac13472019-09-16 21:06:21 +0200336 count = i * sw_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 }
338 else /* original vi indent */
339 {
340 if (left)
341 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200342 count -= sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (count < 0)
344 count = 0;
345 }
346 else
Bram Moolenaardac13472019-09-16 21:06:21 +0200347 count += sw_val * amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
349
350 /* Set new indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000352 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +0000354 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355}
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357/*
358 * Shift one line of the current block one shiftwidth right or left.
359 * Leaves cursor on first character in block.
360 */
361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100362shift_block(oparg_T *oap, int amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363{
364 int left = (oap->op_type == OP_LSHIFT);
365 int oldstate = State;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000366 int total;
367 char_u *newp, *oldp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 int oldcol = curwin->w_cursor.col;
Bram Moolenaardac13472019-09-16 21:06:21 +0200369 int sw_val = (int)get_sw_value_indent(curbuf);
370 int ts_val = (int)curbuf->b_p_ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 int incr;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000373 colnr_T ws_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 int i = 0, j = 0;
375 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#ifdef FEAT_RIGHTLEFT
377 int old_p_ri = p_ri;
378
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200379 p_ri = 0; /* don't want revins in indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#endif
381
382 State = INSERT; /* don't want REPLACE for State */
383 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
384 if (bd.is_short)
385 return;
386
387 /* total is number of screen columns to be inserted/removed */
Bram Moolenaardac13472019-09-16 21:06:21 +0200388 total = (int)((unsigned)amount * (unsigned)sw_val);
389 if ((total / sw_val) != amount)
Bram Moolenaarbae5a172017-08-06 15:42:06 +0200390 return; /* multiplication overflow */
391
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 oldp = ml_get_curline();
393
394 if (!left)
395 {
396 /*
397 * 1. Get start vcol
398 * 2. Total ws vcols
399 * 3. Divvy into TABs & spp
400 * 4. Construct new string
401 */
402 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
403 ws_vcol = bd.start_vcol - bd.pre_whitesp;
404 if (bd.startspaces)
405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (has_mbyte)
Bram Moolenaar20b4f462016-03-05 17:25:39 +0100407 {
408 if ((*mb_ptr2len)(bd.textstart) == 1)
409 ++bd.textstart;
410 else
411 {
412 ws_vcol = 0;
413 bd.startspaces = 0;
414 }
415 }
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000416 else
Bram Moolenaarbe1138b2009-11-11 16:22:28 +0000417 ++bd.textstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 }
Bram Moolenaar1c465442017-03-12 20:10:05 +0100419 for ( ; VIM_ISWHITE(*bd.textstart); )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200421 /* TODO: is passing bd.textstart for start of the line OK? */
422 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
423 (colnr_T)(bd.start_vcol));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 total += incr;
425 bd.start_vcol += incr;
426 }
427 /* OK, now total=all the VWS reqd, and textstart points at the 1st
428 * non-ws char in the block. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200429#ifdef FEAT_VARTABS
430 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200431 tabstop_fromto(ws_vcol, ws_vcol + total,
432 ts_val, curbuf->b_p_vts_array, &i, &j);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200433 else
434 j = total;
435#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 if (!curbuf->b_p_et)
Bram Moolenaardac13472019-09-16 21:06:21 +0200437 i = ((ws_vcol % ts_val) + total) / ts_val; /* number of tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 if (i)
Bram Moolenaardac13472019-09-16 21:06:21 +0200439 j = ((ws_vcol % ts_val) + total) % ts_val; /* number of spp */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 else
441 j = total;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 /* if we're splitting a TAB, allow for it */
444 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
445 len = (int)STRLEN(bd.textstart) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200446 newp = alloc(bd.textcol + i + j + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 if (newp == NULL)
448 return;
449 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
450 mch_memmove(newp, oldp, (size_t)bd.textcol);
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200451 vim_memset(newp + bd.textcol, TAB, (size_t)i);
452 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 /* the end */
454 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
455 }
456 else /* left */
457 {
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000458 colnr_T destination_col; /* column to which text in block will
459 be shifted */
460 char_u *verbatim_copy_end; /* end of the part of the line which is
461 copied verbatim */
462 colnr_T verbatim_copy_width;/* the (displayed) width of this part
463 of line */
464 unsigned fill; /* nr of spaces that replace a TAB */
465 unsigned new_line_len; /* the length of the line after the
466 block shift */
467 size_t block_space_width;
468 size_t shift_amount;
469 char_u *non_white = bd.textstart;
470 colnr_T non_white_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000472 /*
473 * Firstly, let's find the first non-whitespace character that is
474 * displayed after the block's start column and the character's column
475 * number. Also, let's calculate the width of all the whitespace
476 * characters that are displayed in the block and precede the searched
477 * non-whitespace character.
478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000480 /* If "bd.startspaces" is set, "bd.textstart" points to the character,
481 * the part of which is displayed at the block's beginning. Let's start
482 * searching from the next character. */
483 if (bd.startspaces)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100484 MB_PTR_ADV(non_white);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000485
486 /* The character's column is in "bd.start_vcol". */
487 non_white_col = bd.start_vcol;
488
Bram Moolenaar1c465442017-03-12 20:10:05 +0100489 while (VIM_ISWHITE(*non_white))
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000490 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000492 non_white_col += incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 }
494
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000495 block_space_width = non_white_col - oap->start_vcol;
496 /* We will shift by "total" or "block_space_width", whichever is less.
497 */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000498 shift_amount = (block_space_width < (size_t)total
499 ? block_space_width : (size_t)total);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000500
501 /* The column to which we will shift the text. */
Bram Moolenaar92a990b2009-04-22 15:45:05 +0000502 destination_col = (colnr_T)(non_white_col - shift_amount);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000503
504 /* Now let's find out how much of the beginning of the line we can
505 * reuse without modification. */
506 verbatim_copy_end = bd.textstart;
507 verbatim_copy_width = bd.start_vcol;
508
509 /* If "bd.startspaces" is set, "bd.textstart" points to the character
510 * preceding the block. We have to subtract its width to obtain its
511 * column number. */
512 if (bd.startspaces)
513 verbatim_copy_width -= bd.start_char_vcols;
514 while (verbatim_copy_width < destination_col)
515 {
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 char_u *line = verbatim_copy_end;
517
518 /* TODO: is passing verbatim_copy_end for start of the line OK? */
519 incr = lbr_chartabsize(line, verbatim_copy_end,
520 verbatim_copy_width);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000521 if (verbatim_copy_width + incr > destination_col)
522 break;
523 verbatim_copy_width += incr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100524 MB_PTR_ADV(verbatim_copy_end);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000525 }
526
527 /* If "destination_col" is different from the width of the initial
528 * part of the line that will be copied, it means we encountered a tab
529 * character, which we will have to partly replace with spaces. */
530 fill = destination_col - verbatim_copy_width;
531
532 /* The replacement line will consist of:
533 * - the beginning of the original line up to "verbatim_copy_end",
534 * - "fill" number of spaces,
535 * - the rest of the line, pointed to by non_white. */
536 new_line_len = (unsigned)(verbatim_copy_end - oldp)
537 + fill
538 + (unsigned)STRLEN(non_white) + 1;
539
Bram Moolenaar964b3742019-05-24 18:54:09 +0200540 newp = alloc(new_line_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (newp == NULL)
542 return;
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000543 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200544 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +0000545 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
547 /* replace the line */
548 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
549 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
550 State = oldstate;
551 curwin->w_cursor.col = oldcol;
552#ifdef FEAT_RIGHTLEFT
553 p_ri = old_p_ri;
554#endif
555}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557/*
558 * Insert string "s" (b_insert ? before : after) block :AKelly
559 * Caller must prepare for undo.
560 */
561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100562block_insert(
563 oparg_T *oap,
564 char_u *s,
565 int b_insert,
566 struct block_def *bdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
Bram Moolenaardac13472019-09-16 21:06:21 +0200568 int ts_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 int count = 0; /* extra spaces to replace a cut TAB */
570 int spaces = 0; /* non-zero if cutting a TAB */
571 colnr_T offset; /* pointer along new line */
572 unsigned s_len; /* STRLEN(s) */
573 char_u *newp, *oldp; /* new, old lines */
574 linenr_T lnum; /* loop var */
575 int oldstate = State;
576
577 State = INSERT; /* don't want REPLACE for State */
578 s_len = (unsigned)STRLEN(s);
579
580 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
581 {
582 block_prep(oap, bdp, lnum, TRUE);
583 if (bdp->is_short && b_insert)
584 continue; /* OP_INSERT, line ends before block start */
585
586 oldp = ml_get(lnum);
587
588 if (b_insert)
589 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200590 ts_val = bdp->start_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 spaces = bdp->startspaces;
592 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200593 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 offset = bdp->textcol;
595 }
596 else /* append */
597 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200598 ts_val = bdp->end_char_vcols;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!bdp->is_short) /* spaces = padding after block */
600 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200601 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (spaces != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200603 count = ts_val - 1; /* we're cutting a TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 offset = bdp->textcol + bdp->textlen - (spaces != 0);
605 }
606 else /* spaces = padding to block edge */
607 {
608 /* if $ used, just append to EOL (ie spaces==0) */
609 if (!bdp->is_MAX)
610 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
611 count = spaces;
612 offset = bdp->textcol + bdp->textlen;
613 }
614 }
615
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200616 if (has_mbyte && spaces > 0)
617 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100618 int off;
619
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200620 /* Avoid starting halfway a multi-byte character. */
621 if (b_insert)
622 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100623 off = (*mb_head_off)(oldp, oldp + offset + spaces);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200624 }
625 else
626 {
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100627 off = (*mb_off_next)(oldp, oldp + offset);
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200628 offset += off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200629 }
Bram Moolenaarfc3f23b2014-12-17 18:35:42 +0100630 spaces -= off;
631 count -= off;
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200632 }
Bram Moolenaarb5cf6c32014-08-16 18:36:43 +0200633
Bram Moolenaar964b3742019-05-24 18:54:09 +0200634 newp = alloc(STRLEN(oldp) + s_len + count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (newp == NULL)
636 continue;
637
638 /* copy up to shifted part */
639 mch_memmove(newp, oldp, (size_t)(offset));
640 oldp += offset;
641
642 /* insert pre-padding */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200643 vim_memset(newp + offset, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 /* copy the new text */
646 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
647 offset += s_len;
648
649 if (spaces && !bdp->is_short)
650 {
651 /* insert post-padding */
Bram Moolenaardac13472019-09-16 21:06:21 +0200652 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 /* We're splitting a TAB, don't copy it. */
654 oldp++;
655 /* We allowed for that TAB, remember this now */
656 count++;
657 }
658
659 if (spaces > 0)
660 offset += count;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +0000661 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662
663 ml_replace(lnum, newp, FALSE);
664
665 if (lnum == oap->end.lnum)
666 {
667 /* Set "']" mark to the end of the block instead of the end of
668 * the insert in the first line. */
669 curbuf->b_op_end.lnum = oap->end.lnum;
670 curbuf->b_op_end.col = offset;
671 }
672 } /* for all lnum */
673
674 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
675
676 State = oldstate;
677}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
680/*
681 * op_reindent - handle reindenting a block of lines.
682 */
683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100684op_reindent(oparg_T *oap, int (*how)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686 long i;
687 char_u *l;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200688 int amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 linenr_T first_changed = 0;
690 linenr_T last_changed = 0;
691 linenr_T start_lnum = curwin->w_cursor.lnum;
692
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000693 /* Don't even try when 'modifiable' is off. */
694 if (!curbuf->b_p_ma)
695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100696 emsg(_(e_modifiable));
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000697 return;
698 }
699
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 for (i = oap->line_count; --i >= 0 && !got_int; )
701 {
702 /* it's a slow thing to do, so give feedback so there's no worry that
703 * the computer's just hung. */
704
705 if (i > 1
706 && (i % 50 == 0 || i == oap->line_count - 1)
707 && oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100708 smsg(_("%ld lines to indent... "), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
710 /*
711 * Be vi-compatible: For lisp indenting the first line is not
712 * indented, unless there is only one line.
713 */
714#ifdef FEAT_LISP
715 if (i != oap->line_count - 1 || oap->line_count == 1
716 || how != get_lisp_indent)
717#endif
718 {
719 l = skipwhite(ml_get_curline());
720 if (*l == NUL) /* empty or blank line */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200721 amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 else
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200723 amount = how(); /* get the indent for this line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +0200725 if (amount >= 0 && set_indent(amount, SIN_UNDO))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 /* did change the indent, call changed_lines() later */
728 if (first_changed == 0)
729 first_changed = curwin->w_cursor.lnum;
730 last_changed = curwin->w_cursor.lnum;
731 }
732 }
733 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +0000734 curwin->w_cursor.col = 0; /* make sure it's valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 }
736
737 /* put cursor on first non-blank of indented line */
738 curwin->w_cursor.lnum = start_lnum;
739 beginline(BL_SOL | BL_FIX);
740
741 /* Mark changed lines so that they will be redrawn. When Visual
742 * highlighting was present, need to continue until the last line. When
743 * there is no change still need to remove the Visual highlighting. */
744 if (last_changed != 0)
745 changed_lines(first_changed, 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 oap->is_VIsual ? start_lnum + oap->line_count :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 last_changed + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 else if (oap->is_VIsual)
749 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751 if (oap->line_count > p_report)
752 {
753 i = oap->line_count - (i + 1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100754 smsg(NGETTEXT("%ld line indented ",
Bram Moolenaarda6e8912018-08-21 15:12:14 +0200755 "%ld lines indented ", i), i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757 /* set '[ and '] marks */
758 curbuf->b_op_start = oap->start;
759 curbuf->b_op_end = oap->end;
760}
761#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
762
763#if defined(FEAT_EVAL) || defined(PROTO)
764/*
765 * Keep the last expression line here, for repeating.
766 */
767static char_u *expr_line = NULL;
768
769/*
770 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
771 * Returns '=' when OK, NUL otherwise.
772 */
773 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100774get_expr_register(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
776 char_u *new_line;
777
Bram Moolenaare96a2492019-06-25 04:12:16 +0200778 new_line = getcmdline('=', 0L, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 if (new_line == NULL)
780 return NUL;
781 if (*new_line == NUL) /* use previous line */
782 vim_free(new_line);
783 else
784 set_expr_line(new_line);
785 return '=';
786}
787
788/*
789 * Set the expression for the '=' register.
790 * Argument must be an allocated string.
791 */
792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100793set_expr_line(char_u *new_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 vim_free(expr_line);
796 expr_line = new_line;
797}
798
799/*
800 * Get the result of the '=' register expression.
801 * Returns a pointer to allocated memory, or NULL for failure.
802 */
803 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100804get_expr_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 char_u *expr_copy;
807 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000808 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 if (expr_line == NULL)
811 return NULL;
812
813 /* Make a copy of the expression, because evaluating it may cause it to be
814 * changed. */
815 expr_copy = vim_strsave(expr_line);
816 if (expr_copy == NULL)
817 return NULL;
818
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000819 /* When we are invoked recursively limit the evaluation to 10 levels.
820 * Then return the string as-is. */
821 if (nested >= 10)
822 return expr_copy;
823
824 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000825 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000826 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 vim_free(expr_copy);
828 return rv;
829}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000830
831/*
832 * Get the '=' register expression itself, without evaluating it.
833 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200834 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100835get_expr_line_src(void)
Bram Moolenaarde934d72005-05-22 22:09:40 +0000836{
837 if (expr_line == NULL)
838 return NULL;
839 return vim_strsave(expr_line);
840}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841#endif /* FEAT_EVAL */
842
843/*
844 * Check if 'regname' is a valid name of a yank register.
845 * Note: There is no check for 0 (default register), caller should do this
846 */
847 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100848valid_yank_reg(
849 int regname,
850 int writing) /* if TRUE check for writable registers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851{
852 if ( (regname > 0 && ASCII_ISALNUM(regname))
853 || (!writing && vim_strchr((char_u *)
854#ifdef FEAT_EVAL
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100855 "/.%:="
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#else
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100857 "/.%:"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#endif
859 , regname) != NULL)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +0100860 || regname == '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 || regname == '"'
862 || regname == '-'
863 || regname == '_'
864#ifdef FEAT_CLIPBOARD
865 || regname == '*'
866 || regname == '+'
867#endif
868#ifdef FEAT_DND
869 || (!writing && regname == '~')
870#endif
871 )
872 return TRUE;
873 return FALSE;
874}
875
876/*
877 * Set y_current and y_append, according to the value of "regname".
878 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000879 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 *
881 * If regname is 0 and writing, use register 0
882 * If regname is 0 and reading, use previous register
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100883 *
884 * Return TRUE when the register should be inserted literally (selection or
885 * clipboard).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 */
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100887 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100888get_yank_register(int regname, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
890 int i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100891 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 y_append = FALSE;
894 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
895 {
896 y_current = y_previous;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100897 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 i = regname;
900 if (VIM_ISDIGIT(i))
901 i -= '0';
902 else if (ASCII_ISLOWER(i))
903 i = CharOrdLow(i) + 10;
904 else if (ASCII_ISUPPER(i))
905 {
906 i = CharOrdUp(i) + 10;
907 y_append = TRUE;
908 }
909 else if (regname == '-')
910 i = DELETION_REGISTER;
911#ifdef FEAT_CLIPBOARD
912 /* When selection is not available, use register 0 instead of '*' */
913 else if (clip_star.available && regname == '*')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 i = STAR_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100916 ret = TRUE;
917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 /* When clipboard is not available, use register 0 instead of '+' */
919 else if (clip_plus.available && regname == '+')
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 i = PLUS_REGISTER;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100922 ret = TRUE;
923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#endif
925#ifdef FEAT_DND
926 else if (!writing && regname == '~')
927 i = TILDE_REGISTER;
928#endif
929 else /* not 0-9, a-z, A-Z or '-': use register 0 */
930 i = 0;
931 y_current = &(y_regs[i]);
932 if (writing) /* remember the register we write into for do_put() */
933 y_previous = y_current;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +0100934 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935}
936
Bram Moolenaar8299df92004-07-10 09:47:34 +0000937#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * When "regname" is a clipboard register, obtain the selection. If it's not
940 * available return zero, otherwise return "regname".
941 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000942 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100943may_get_selection(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944{
945 if (regname == '*')
946 {
947 if (!clip_star.available)
948 regname = 0;
949 else
950 clip_get_selection(&clip_star);
951 }
952 else if (regname == '+')
953 {
954 if (!clip_plus.available)
955 regname = 0;
956 else
957 clip_get_selection(&clip_plus);
958 }
959 return regname;
960}
961#endif
962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963/*
964 * Obtain the contents of a "normal" register. The register is made empty.
965 * The returned pointer has allocated memory, use put_register() later.
966 */
967 void *
Bram Moolenaar9b578142016-01-30 19:39:49 +0100968get_register(
969 int name,
970 int copy) /* make a copy, if FALSE make register empty. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +0200972 yankreg_T *reg;
973 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975#ifdef FEAT_CLIPBOARD
976 /* When Visual area changed, may have to update selection. Obtain the
977 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000978 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200980 if (clip_isautosel_star())
981 clip_update_selection(&clip_star);
982 may_get_selection(name);
983 }
984 if (name == '+' && clip_plus.available)
985 {
986 if (clip_isautosel_plus())
987 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 may_get_selection(name);
989 }
990#endif
991
992 get_yank_register(name, 0);
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200993 reg = ALLOC_ONE(yankreg_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (reg != NULL)
995 {
996 *reg = *y_current;
997 if (copy)
998 {
999 /* If we run out of memory some or all of the lines are empty. */
1000 if (reg->y_size == 0)
1001 reg->y_array = NULL;
1002 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001003 reg->y_array = ALLOC_MULT(char_u *, reg->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (reg->y_array != NULL)
1005 {
1006 for (i = 0; i < reg->y_size; ++i)
1007 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
1008 }
1009 }
1010 else
1011 y_current->y_array = NULL;
1012 }
1013 return (void *)reg;
1014}
1015
1016/*
Bram Moolenaar0a307462007-12-01 20:13:05 +00001017 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 */
1019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001020put_register(int name, void *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 get_yank_register(name, 0);
1023 free_yank_all();
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001024 *y_current = *(yankreg_T *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +00001025 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001027#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 /* Send text written to clipboard register to the clipboard. */
1029 may_set_selection();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001032
Bram Moolenaar113e1072019-01-20 15:30:40 +01001033#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
1034 || defined(PROTO)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001036free_register(void *reg)
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001037{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001038 yankreg_T tmp;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001039
1040 tmp = *y_current;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001041 *y_current = *(yankreg_T *)reg;
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01001042 free_yank_all();
1043 vim_free(reg);
1044 *y_current = tmp;
1045}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
1048#if defined(FEAT_MOUSE) || defined(PROTO)
1049/*
1050 * return TRUE if the current yank register has type MLINE
1051 */
1052 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001053yank_register_mline(int regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
1055 if (regname != 0 && !valid_yank_reg(regname, FALSE))
1056 return FALSE;
1057 if (regname == '_') /* black hole is always empty */
1058 return FALSE;
1059 get_yank_register(regname, FALSE);
1060 return (y_current->y_type == MLINE);
1061}
1062#endif
1063
1064/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001065 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001067 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 */
1069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001070do_record(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
Bram Moolenaard55de222007-05-06 13:38:48 +00001072 char_u *p;
1073 static int regname;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001074 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaard55de222007-05-06 13:38:48 +00001075 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001077 if (reg_recording == 0) /* start recording */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02001079 /* registers 0-9, a-z and " are allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1081 retval = FAIL;
1082 else
1083 {
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001084 reg_recording = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 showmode();
1086 regname = c;
1087 retval = OK;
1088 }
1089 }
1090 else /* stop recording */
1091 {
1092 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001093 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1094 * needs to be removed again to put it in a register. exec_reg then
1095 * adds the escaping back later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001097 reg_recording = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001098 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 p = get_recorded();
1100 if (p == NULL)
1101 retval = FAIL;
1102 else
1103 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001104 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1105 vim_unescape_csi(p);
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 /*
1108 * We don't want to change the default register here, so save and
1109 * restore the current register name.
1110 */
1111 old_y_previous = y_previous;
1112 old_y_current = y_current;
1113
1114 retval = stuff_yank(regname, p);
1115
1116 y_previous = old_y_previous;
1117 y_current = old_y_current;
1118 }
1119 }
1120 return retval;
1121}
1122
1123/*
1124 * Stuff string "p" into yank register "regname" as a single line (append if
1125 * uppercase). "p" must have been alloced.
1126 *
1127 * return FAIL for failure, OK otherwise
1128 */
1129 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001130stuff_yank(int regname, char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
1132 char_u *lp;
1133 char_u **pp;
1134
1135 /* check for read-only register */
1136 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1137 {
1138 vim_free(p);
1139 return FAIL;
1140 }
1141 if (regname == '_') /* black hole: don't do anything */
1142 {
1143 vim_free(p);
1144 return OK;
1145 }
1146 get_yank_register(regname, TRUE);
1147 if (y_append && y_current->y_array != NULL)
1148 {
1149 pp = &(y_current->y_array[y_current->y_size - 1]);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001150 lp = alloc(STRLEN(*pp) + STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (lp == NULL)
1152 {
1153 vim_free(p);
1154 return FAIL;
1155 }
1156 STRCPY(lp, *pp);
1157 STRCAT(lp, p);
1158 vim_free(p);
1159 vim_free(*pp);
1160 *pp = lp;
1161 }
1162 else
1163 {
1164 free_yank_all();
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001165 if ((y_current->y_array = ALLOC_ONE(char_u *)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
1167 vim_free(p);
1168 return FAIL;
1169 }
1170 y_current->y_array[0] = p;
1171 y_current->y_size = 1;
1172 y_current->y_type = MCHAR; /* used to be MLINE, why? */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02001173#ifdef FEAT_VIMINFO
1174 y_current->y_time_set = vim_time();
1175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
1177 return OK;
1178}
1179
Bram Moolenaar42b94362009-05-26 16:12:37 +00001180static int execreg_lastc = NUL;
1181
Bram Moolenaarc3328162019-07-23 22:15:25 +02001182 int
1183get_execreg_lastc(void)
1184{
1185 return execreg_lastc;
1186}
1187
1188 void
1189set_execreg_lastc(int lastc)
1190{
1191 execreg_lastc = lastc;
1192}
1193
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194/*
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001195 * Execute a yank register: copy it into the stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 *
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001197 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 */
1199 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001200do_execreg(
1201 int regname,
1202 int colon, /* insert ':' before each line */
1203 int addcr, /* always add '\n' to end of line */
1204 int silent) /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 long i;
1207 char_u *p;
1208 int retval = OK;
1209 int remap;
1210
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001211 // repeat previous one
1212 if (regname == '@')
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001213 {
Bram Moolenaar42b94362009-05-26 16:12:37 +00001214 if (execreg_lastc == NUL)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_("E748: No previously used register"));
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001217 return FAIL;
1218 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001219 regname = execreg_lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001220 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001221 // check for valid regname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001223 {
1224 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001226 }
Bram Moolenaar42b94362009-05-26 16:12:37 +00001227 execreg_lastc = regname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
1229#ifdef FEAT_CLIPBOARD
1230 regname = may_get_selection(regname);
1231#endif
1232
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001233 // black hole: don't stuff anything
1234 if (regname == '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 return OK;
1236
Bram Moolenaar80e737c2019-05-17 19:56:34 +02001237 // use last command line
1238 if (regname == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 if (last_cmdline == NULL)
1241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001242 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 return FAIL;
1244 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001245 // don't keep the cmdline containing @:
1246 VIM_CLEAR(new_last_cmdline);
1247 // Escape all control characters with a CTRL-V
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001248 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001249 (char_u *)"\001\002\003\004\005\006\007"
1250 "\010\011\012\013\014\015\016\017"
1251 "\020\021\022\023\024\025\026\027"
1252 "\030\031\032\033\034\035\036\037",
1253 Ctrl_V, FALSE);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001254 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001255 {
1256 /* When in Visual mode "'<,'>" will be prepended to the command.
1257 * Remove it when it's already there. */
1258 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001259 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001260 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001261 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001262 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001263 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265#ifdef FEAT_EVAL
1266 else if (regname == '=')
1267 {
1268 p = get_expr_line();
1269 if (p == NULL)
1270 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001271 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 vim_free(p);
1273 }
1274#endif
1275 else if (regname == '.') /* use last inserted text */
1276 {
1277 p = get_last_insert_save();
1278 if (p == NULL)
1279 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001280 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 return FAIL;
1282 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001283 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(p);
1285 }
1286 else
1287 {
1288 get_yank_register(regname, FALSE);
1289 if (y_current->y_array == NULL)
1290 return FAIL;
1291
1292 /* Disallow remaping for ":@r". */
1293 remap = colon ? REMAP_NONE : REMAP_YES;
1294
1295 /*
1296 * Insert lines into typeahead buffer, from last one to first one.
1297 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001298 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 for (i = y_current->y_size; --i >= 0; )
1300 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001301 char_u *escaped;
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 /* insert NL between lines and after last line if type is MLINE */
1304 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1305 || addcr)
1306 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001307 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 return FAIL;
1309 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001310 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1311 if (escaped == NULL)
1312 return FAIL;
1313 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1314 vim_free(escaped);
1315 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001317 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 == FAIL)
1319 return FAIL;
1320 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001321 reg_executing = regname == 0 ? '"' : regname; // disable "q" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 return retval;
1324}
1325
1326/*
1327 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1328 * used only after other typeahead has been processed.
1329 */
1330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001331put_reedit_in_typebuf(int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 char_u buf[3];
1334
1335 if (restart_edit != NUL)
1336 {
1337 if (restart_edit == 'V')
1338 {
1339 buf[0] = 'g';
1340 buf[1] = 'R';
1341 buf[2] = NUL;
1342 }
1343 else
1344 {
1345 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1346 buf[1] = NUL;
1347 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001348 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 restart_edit = NUL;
1350 }
1351}
1352
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001353/*
1354 * Insert register contents "s" into the typeahead buffer, so that it will be
1355 * executed again.
1356 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and
1357 * no remapping.
1358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360put_in_typebuf(
1361 char_u *s,
1362 int esc,
1363 int colon, /* add ':' before the line */
1364 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int retval = OK;
1367
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001368 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (colon)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001370 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001372 {
1373 char_u *p;
1374
1375 if (esc)
1376 p = vim_strsave_escape_csi(s);
1377 else
1378 p = s;
1379 if (p == NULL)
1380 retval = FAIL;
1381 else
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001382 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
1383 0, TRUE, silent);
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001384 if (esc)
1385 vim_free(p);
1386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (colon && retval == OK)
Bram Moolenaar38ef43b2010-01-27 16:31:13 +01001388 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 return retval;
1390}
1391
1392/*
1393 * Insert a yank register: copy it into the Read buffer.
1394 * Used by CTRL-R command and middle mouse button in insert mode.
1395 *
1396 * return FAIL for failure, OK otherwise
1397 */
1398 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001399insert_reg(
1400 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001401 int literally_arg) /* insert literally, not as if typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 long i;
1404 int retval = OK;
1405 char_u *arg;
1406 int allocated;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001407 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 /*
1410 * It is possible to get into an endless loop by having CTRL-R a in
1411 * register a and then, in insert mode, doing CTRL-R a.
1412 * If you hit CTRL-C, the loop will be broken here.
1413 */
1414 ui_breakcheck();
1415 if (got_int)
1416 return FAIL;
1417
1418 /* check for valid regname */
1419 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1420 return FAIL;
1421
1422#ifdef FEAT_CLIPBOARD
1423 regname = may_get_selection(regname);
1424#endif
1425
1426 if (regname == '.') /* insert last inserted text */
1427 retval = stuff_inserted(NUL, 1L, TRUE);
1428 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1429 {
1430 if (arg == NULL)
1431 return FAIL;
1432 stuffescaped(arg, literally);
1433 if (allocated)
1434 vim_free(arg);
1435 }
1436 else /* name or number register */
1437 {
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001438 if (get_yank_register(regname, FALSE))
1439 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (y_current->y_array == NULL)
1441 retval = FAIL;
1442 else
1443 {
1444 for (i = 0; i < y_current->y_size; ++i)
1445 {
1446 stuffescaped(y_current->y_array[i], literally);
1447 /*
1448 * Insert a newline between lines and after last line if
1449 * y_type is MLINE.
1450 */
1451 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1452 stuffcharReadbuff('\n');
1453 }
1454 }
1455 }
1456
1457 return retval;
1458}
1459
1460/*
1461 * Stuff a string into the typeahead buffer, such that edit() will insert it
1462 * literally ("literally" TRUE) or interpret is as typed characters.
1463 */
1464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001465stuffescaped(char_u *arg, int literally)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 int c;
1468 char_u *start;
1469
1470 while (*arg != NUL)
1471 {
1472 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1473 * stuff K_SPECIAL to get the effect of a special key when "literally"
1474 * is TRUE. */
1475 start = arg;
1476 while ((*arg >= ' '
1477#ifndef EBCDIC
1478 && *arg < DEL /* EBCDIC: chars above space are normal */
1479#endif
1480 )
1481 || (*arg == K_SPECIAL && !literally))
1482 ++arg;
1483 if (arg > start)
1484 stuffReadbuffLen(start, (long)(arg - start));
1485
1486 /* stuff a single special character */
1487 if (*arg != NUL)
1488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (has_mbyte)
Bram Moolenaar3b1c4852010-07-31 17:59:29 +02001490 c = mb_cptr2char_adv(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 c = *arg++;
1493 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1494 stuffcharReadbuff(Ctrl_V);
1495 stuffcharReadbuff(c);
1496 }
1497 }
1498}
1499
1500/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001501 * If "regname" is a special register, return TRUE and store a pointer to its
1502 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001504 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001505get_spec_reg(
1506 int regname,
1507 char_u **argp,
1508 int *allocated, /* return: TRUE when value was allocated */
1509 int errmsg) /* give error message when failing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 int cnt;
1512
1513 *argp = NULL;
1514 *allocated = FALSE;
1515 switch (regname)
1516 {
1517 case '%': /* file name */
1518 if (errmsg)
1519 check_fname(); /* will give emsg if not set */
1520 *argp = curbuf->b_fname;
1521 return TRUE;
1522
1523 case '#': /* alternate file name */
1524 *argp = getaltfname(errmsg); /* may give emsg if not set */
1525 return TRUE;
1526
1527#ifdef FEAT_EVAL
1528 case '=': /* result of expression */
1529 *argp = get_expr_line();
1530 *allocated = TRUE;
1531 return TRUE;
1532#endif
1533
1534 case ':': /* last command line */
1535 if (last_cmdline == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001536 emsg(_(e_nolastcmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 *argp = last_cmdline;
1538 return TRUE;
1539
1540 case '/': /* last search-pattern */
1541 if (last_search_pat() == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001542 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 *argp = last_search_pat();
1544 return TRUE;
1545
1546 case '.': /* last inserted text */
1547 *argp = get_last_insert_save();
1548 *allocated = TRUE;
1549 if (*argp == NULL && errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001550 emsg(_(e_noinstext));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 return TRUE;
1552
1553#ifdef FEAT_SEARCHPATH
1554 case Ctrl_F: /* Filename under cursor */
1555 case Ctrl_P: /* Path under cursor, expand via "path" */
1556 if (!errmsg)
1557 return FALSE;
1558 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001559 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 *allocated = TRUE;
1561 return TRUE;
1562#endif
1563
1564 case Ctrl_W: /* word under cursor */
1565 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1566 if (!errmsg)
1567 return FALSE;
1568 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1569 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1570 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1571 *allocated = TRUE;
1572 return TRUE;
1573
Bram Moolenaare2c8d832018-05-01 19:24:03 +02001574 case Ctrl_L: /* Line under cursor */
1575 if (!errmsg)
1576 return FALSE;
1577
1578 *argp = ml_get_buf(curwin->w_buffer,
1579 curwin->w_cursor.lnum, FALSE);
1580 return TRUE;
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 case '_': /* black hole: always empty */
1583 *argp = (char_u *)"";
1584 return TRUE;
1585 }
1586
1587 return FALSE;
1588}
1589
1590/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001591 * Paste a yank register into the command line.
1592 * Only for non-special registers.
1593 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * insert_reg() can't be used here, because special characters from the
1595 * register contents will be interpreted as commands.
1596 *
1597 * return FAIL for failure, OK otherwise
1598 */
1599 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001600cmdline_paste_reg(
1601 int regname,
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001602 int literally_arg, /* Insert text literally instead of "as typed" */
Bram Moolenaar9b578142016-01-30 19:39:49 +01001603 int remcr) /* don't add CR characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605 long i;
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001606 int literally = literally_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001608 if (get_yank_register(regname, FALSE))
1609 literally = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (y_current->y_array == NULL)
1611 return FAIL;
1612
1613 for (i = 0; i < y_current->y_size; ++i)
1614 {
1615 cmdline_paste_str(y_current->y_array[i], literally);
1616
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001617 /* Insert ^M between lines and after last line if type is MLINE.
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01001618 * Don't do this when "remcr" is TRUE. */
1619 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 cmdline_paste_str((char_u *)"\r", literally);
1621
1622 /* Check for CTRL-C, in case someone tries to paste a few thousand
1623 * lines and gets bored. */
1624 ui_breakcheck();
1625 if (got_int)
1626 return FAIL;
1627 }
1628 return OK;
1629}
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1632/*
1633 * Adjust the register name pointed to with "rp" for the clipboard being
1634 * used always and the clipboard being available.
1635 */
1636 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001637adjust_clip_reg(int *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001639 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard',
1640 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001641 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0))
1642 {
1643 if (clip_unnamed != 0)
1644 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01001645 ? '+' : '*';
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02001646 else
1647 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available)
1648 ? '+' : '*';
1649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (!clip_star.available && *rp == '*')
1651 *rp = 0;
1652 if (!clip_plus.available && *rp == '+')
1653 *rp = 0;
1654}
1655#endif
1656
1657/*
Bram Moolenaara1891842017-02-04 21:34:31 +01001658 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
1659 */
1660 void
1661shift_delete_registers()
1662{
1663 int n;
1664
1665 y_current = &y_regs[9];
1666 free_yank_all(); /* free register nine */
1667 for (n = 9; n > 1; --n)
1668 y_regs[n] = y_regs[n - 1];
Bram Moolenaar18d90b92017-06-27 15:39:14 +02001669 y_current = &y_regs[1];
1670 if (!y_append)
1671 y_previous = y_current;
Bram Moolenaara1891842017-02-04 21:34:31 +01001672 y_regs[1].y_array = NULL; /* set register one to empty */
1673}
1674
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001675#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001676 static void
1677yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
1678{
1679 static int recursive = FALSE;
1680 dict_T *v_event;
1681 list_T *list;
1682 int n;
1683 char_u buf[NUMBUFLEN + 2];
1684 long reglen = 0;
1685
1686 if (recursive)
1687 return;
1688
1689 v_event = get_vim_var_dict(VV_EVENT);
1690
1691 list = list_alloc();
Bram Moolenaara0221df2018-02-11 15:07:22 +01001692 if (list == NULL)
1693 return;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001694 for (n = 0; n < reg->y_size; n++)
1695 list_append_string(list, reg->y_array[n], -1);
1696 list->lv_lock = VAR_FIXED;
1697 dict_add_list(v_event, "regcontents", list);
1698
1699 buf[0] = (char_u)oap->regname;
1700 buf[1] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001701 dict_add_string(v_event, "regname", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001702
1703 buf[0] = get_op_char(oap->op_type);
1704 buf[1] = get_extra_op_char(oap->op_type);
1705 buf[2] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02001706 dict_add_string(v_event, "operator", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001707
1708 buf[0] = NUL;
1709 buf[1] = NUL;
1710 switch (get_reg_type(oap->regname, &reglen))
1711 {
1712 case MLINE: buf[0] = 'V'; break;
1713 case MCHAR: buf[0] = 'v'; break;
1714 case MBLOCK:
1715 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1716 reglen + 1);
1717 break;
1718 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02001719 dict_add_string(v_event, "regtype", buf);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001720
1721 /* Lock the dictionary and its keys */
1722 dict_set_items_ro(v_event);
1723
1724 recursive = TRUE;
1725 textlock++;
1726 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
1727 textlock--;
1728 recursive = FALSE;
1729
1730 /* Empty the dictionary, v:event is still valid */
1731 dict_free_contents(v_event);
1732 hash_init(&v_event->dv_hashtab);
1733}
Bram Moolenaaree219b02017-12-17 14:55:01 +01001734#endif
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001735
Bram Moolenaara1891842017-02-04 21:34:31 +01001736/*
Bram Moolenaard04b7502010-07-08 22:27:55 +02001737 * Handle a delete operation.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 *
Bram Moolenaard04b7502010-07-08 22:27:55 +02001739 * Return FAIL if undo failed, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 */
1741 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001742op_delete(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743{
1744 int n;
1745 linenr_T lnum;
1746 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 char_u *newp, *oldp;
1748 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1750 int did_yank = FALSE;
1751
1752 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1753 return OK;
1754
1755 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1756 if (oap->empty)
1757 return u_save_cursor();
1758
1759 if (!curbuf->b_p_ma)
1760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001761 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 return FAIL;
1763 }
1764
1765#ifdef FEAT_CLIPBOARD
1766 adjust_clip_reg(&oap->regname);
1767#endif
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (has_mbyte)
1770 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaard04b7502010-07-08 22:27:55 +02001772 /*
1773 * Imitate the strange Vi behaviour: If the delete spans more than one
1774 * line and motion_type == MCHAR and the result is a blank line, make the
1775 * delete linewise. Don't do this for the change command or Visual mode.
1776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 if ( oap->motion_type == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001779 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 && oap->line_count > 1
Bram Moolenaar64a72302012-01-10 13:46:22 +01001781 && oap->motion_force == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 && oap->op_type == OP_DELETE)
1783 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001784 ptr = ml_get(oap->end.lnum) + oap->end.col;
1785 if (*ptr != NUL)
1786 ptr += oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 ptr = skipwhite(ptr);
1788 if (*ptr == NUL && inindent(0))
1789 oap->motion_type = MLINE;
1790 }
1791
Bram Moolenaard04b7502010-07-08 22:27:55 +02001792 /*
1793 * Check for trying to delete (e.g. "D") in an empty line.
1794 * Note: For the change operator it is ok.
1795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if ( oap->motion_type == MCHAR
1797 && oap->line_count == 1
1798 && oap->op_type == OP_DELETE
1799 && *ml_get(oap->start.lnum) == NUL)
1800 {
1801 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001802 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 * 'cpoptions' (Vi compatible).
1804 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001805 if (virtual_op)
1806 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1807 * marks as if it happened. */
1808 goto setmarks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1810 beep_flush();
1811 return OK;
1812 }
1813
Bram Moolenaard04b7502010-07-08 22:27:55 +02001814 /*
1815 * Do a yank of whatever we're about to delete.
1816 * If a yank register was specified, put the deleted text into that
1817 * register. For the black hole register '_' don't yank anything.
1818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (oap->regname != '_')
1820 {
1821 if (oap->regname != 0)
1822 {
1823 /* check for read-only register */
1824 if (!valid_yank_reg(oap->regname, TRUE))
1825 {
1826 beep_flush();
1827 return OK;
1828 }
1829 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1830 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1831 did_yank = TRUE;
1832 }
1833
1834 /*
1835 * Put deleted text into register 1 and shift number registers if the
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001836 * delete contains a line break, or when using a specific operator (Vi
1837 * compatible)
Bram Moolenaar7c821302012-09-05 14:18:45 +02001838 * Use the register name from before adjust_clip_reg() may have
1839 * changed it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 */
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +01001841 if (oap->motion_type == MLINE || oap->line_count > 1
1842 || oap->use_reg_one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
Bram Moolenaara1891842017-02-04 21:34:31 +01001844 shift_delete_registers();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (op_yank(oap, TRUE, FALSE) == OK)
1846 did_yank = TRUE;
1847 }
1848
Bram Moolenaar84298db2012-04-20 13:46:08 +02001849 /* Yank into small delete register when no named register specified
1850 * and the delete is within one line. */
1851 if ((
1852#ifdef FEAT_CLIPBOARD
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001853 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
1854 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
Bram Moolenaar84298db2012-04-20 13:46:08 +02001855#endif
1856 oap->regname == 0) && oap->motion_type != MLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 && oap->line_count == 1)
1858 {
1859 oap->regname = '-';
1860 get_yank_register(oap->regname, TRUE);
1861 if (op_yank(oap, TRUE, FALSE) == OK)
1862 did_yank = TRUE;
1863 oap->regname = 0;
1864 }
1865
1866 /*
1867 * If there's too much stuff to fit in the yank register, then get a
1868 * confirmation before doing the delete. This is crude, but simple.
1869 * And it avoids doing a delete of something we can't put back if we
1870 * want.
1871 */
1872 if (!did_yank)
1873 {
1874 int msg_silent_save = msg_silent;
1875
1876 msg_silent = 0; /* must display the prompt */
1877 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1878 msg_silent = msg_silent_save;
1879 if (n != 'y')
1880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001881 emsg(_(e_abort));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 return FAIL;
1883 }
1884 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001885
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001886#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001887 if (did_yank && has_textyankpost())
1888 yank_do_autocmd(oap, y_current);
1889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
1891
Bram Moolenaard04b7502010-07-08 22:27:55 +02001892 /*
1893 * block mode delete
1894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (oap->block_mode)
1896 {
1897 if (u_save((linenr_T)(oap->start.lnum - 1),
1898 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1899 return FAIL;
1900
1901 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1902 {
1903 block_prep(oap, &bd, lnum, TRUE);
1904 if (bd.textlen == 0) /* nothing to delete */
1905 continue;
1906
1907 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1908 if (lnum == curwin->w_cursor.lnum)
1909 {
1910 curwin->w_cursor.col = bd.textcol + bd.startspaces;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913
Bram Moolenaar8055d172019-05-17 22:57:26 +02001914 // "n" == number of chars deleted
1915 // If we delete a TAB, it may be replaced by several characters.
1916 // Thus the number of characters may increase!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 n = bd.textlen - bd.startspaces - bd.endspaces;
1918 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02001919 newp = alloc(STRLEN(oldp) + 1 - n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (newp == NULL)
1921 continue;
1922 /* copy up to deleted part */
1923 mch_memmove(newp, oldp, (size_t)bd.textcol);
1924 /* insert spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02001925 vim_memset(newp + bd.textcol, ' ',
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 (size_t)(bd.startspaces + bd.endspaces));
1927 /* copy the part after the deleted part */
1928 oldp += bd.textcol + bd.textlen;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00001929 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 /* replace the line */
1931 ml_replace(lnum, newp, FALSE);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001932
1933#ifdef FEAT_TEXT_PROP
1934 if (curbuf->b_has_textprop && n != 0)
Bram Moolenaarf3333b02019-05-19 22:53:40 +02001935 adjust_prop_columns(lnum, bd.textcol, -n, 0);
Bram Moolenaar8055d172019-05-17 22:57:26 +02001936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938
1939 check_cursor_col();
1940 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1941 oap->end.lnum + 1, 0L);
1942 oap->line_count = 0; /* no lines deleted */
1943 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001944 else if (oap->motion_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
1946 if (oap->op_type == OP_CHANGE)
1947 {
1948 /* Delete the lines except the first one. Temporarily move the
1949 * cursor to the next line. Save the current line number, if the
1950 * last line is deleted it may be changed.
1951 */
1952 if (oap->line_count > 1)
1953 {
1954 lnum = curwin->w_cursor.lnum;
1955 ++curwin->w_cursor.lnum;
1956 del_lines((long)(oap->line_count - 1), TRUE);
1957 curwin->w_cursor.lnum = lnum;
1958 }
1959 if (u_save_cursor() == FAIL)
1960 return FAIL;
1961 if (curbuf->b_p_ai) /* don't delete indent */
1962 {
1963 beginline(BL_WHITE); /* cursor on first non-white */
1964 did_ai = TRUE; /* delete the indent when ESC hit */
1965 ai_col = curwin->w_cursor.col;
1966 }
1967 else
1968 beginline(0); /* cursor in column 0 */
1969 truncate_line(FALSE); /* delete the rest of the line */
1970 /* leave cursor past last char in line */
1971 if (oap->line_count > 1)
1972 u_clearline(); /* "U" command not possible after "2cc" */
1973 }
1974 else
1975 {
1976 del_lines(oap->line_count, TRUE);
1977 beginline(BL_WHITE | BL_FIX);
1978 u_clearline(); /* "U" command not possible after "dd" */
1979 }
1980 }
1981 else
1982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (virtual_op)
1984 {
1985 int endcol = 0;
1986
1987 /* For virtualedit: break the tabs that are partly included. */
1988 if (gchar_pos(&oap->start) == '\t')
1989 {
1990 if (u_save_cursor() == FAIL) /* save first line for undo */
1991 return FAIL;
1992 if (oap->line_count == 1)
1993 endcol = getviscol2(oap->end.col, oap->end.coladd);
1994 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1995 oap->start = curwin->w_cursor;
1996 if (oap->line_count == 1)
1997 {
1998 coladvance(endcol);
1999 oap->end.col = curwin->w_cursor.col;
2000 oap->end.coladd = curwin->w_cursor.coladd;
2001 curwin->w_cursor = oap->start;
2002 }
2003 }
2004
2005 /* Break a tab only when it's included in the area. */
2006 if (gchar_pos(&oap->end) == '\t'
2007 && (int)oap->end.coladd < oap->inclusive)
2008 {
2009 /* save last line for undo */
2010 if (u_save((linenr_T)(oap->end.lnum - 1),
2011 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2012 return FAIL;
2013 curwin->w_cursor = oap->end;
2014 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
2015 oap->end = curwin->w_cursor;
2016 curwin->w_cursor = oap->start;
2017 }
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 if (oap->line_count == 1) /* delete characters within one line */
2021 {
2022 if (u_save_cursor() == FAIL) /* save line for undo */
2023 return FAIL;
2024
2025 /* if 'cpoptions' contains '$', display '$' at end of change */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002026 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 && oap->op_type == OP_CHANGE
2028 && oap->end.lnum == curwin->w_cursor.lnum
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002029 && !oap->is_VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 display_dollar(oap->end.col - !oap->inclusive);
2031
2032 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
2033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (virtual_op)
2035 {
2036 /* fix up things for virtualedit-delete:
2037 * break the tabs which are going to get in our way
2038 */
2039 char_u *curline = ml_get_curline();
2040 int len = (int)STRLEN(curline);
2041
2042 if (oap->end.coladd != 0
2043 && (int)oap->end.col >= len - 1
2044 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
2045 n++;
2046 /* Delete at least one char (e.g, when on a control char). */
2047 if (n == 0 && oap->start.coladd != oap->end.coladd)
2048 n = 1;
2049
2050 /* When deleted a char in the line, reset coladd. */
2051 if (gchar_cursor() != NUL)
2052 curwin->w_cursor.coladd = 0;
2053 }
Bram Moolenaard009e862015-06-09 20:20:03 +02002054 (void)del_bytes((long)n, !virtual_op,
2055 oap->op_type == OP_DELETE && !oap->is_VIsual);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 }
2057 else /* delete characters between lines */
2058 {
2059 pos_T curpos;
2060
2061 /* save deleted and changed lines for undo */
2062 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
2063 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
2064 return FAIL;
2065
2066 truncate_line(TRUE); /* delete from cursor to end of line */
2067
2068 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
2069 ++curwin->w_cursor.lnum;
2070 del_lines((long)(oap->line_count - 2), FALSE);
2071
Bram Moolenaard009e862015-06-09 20:20:03 +02002072 /* delete from start of line until op_end */
Bram Moolenaar44286ca2011-07-15 17:51:34 +02002073 n = (oap->end.col + 1 - !oap->inclusive);
Bram Moolenaard009e862015-06-09 20:20:03 +02002074 curwin->w_cursor.col = 0;
2075 (void)del_bytes((long)n, !virtual_op,
2076 oap->op_type == OP_DELETE && !oap->is_VIsual);
2077 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
2078 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 }
2081
2082 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
2083
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002084setmarks:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (oap->block_mode)
2086 {
2087 curbuf->b_op_end.lnum = oap->end.lnum;
2088 curbuf->b_op_end.col = oap->start.col;
2089 }
2090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 curbuf->b_op_end = oap->start;
2092 curbuf->b_op_start = oap->start;
2093
2094 return OK;
2095}
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097/*
2098 * Adjust end of operating area for ending on a multi-byte character.
2099 * Used for deletion.
2100 */
2101 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002102mb_adjust_opend(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 char_u *p;
2105
2106 if (oap->inclusive)
2107 {
2108 p = ml_get(oap->end.lnum);
2109 oap->end.col += mb_tail_off(p, p + oap->end.col);
2110 }
2111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
Bram Moolenaar630afe82018-06-28 19:26:28 +02002113/*
2114 * Replace the character under the cursor with "c".
2115 * This takes care of multi-byte characters.
2116 */
2117 static void
2118replace_character(int c)
2119{
2120 int n = State;
2121
2122 State = REPLACE;
2123 ins_char(c);
2124 State = n;
2125 /* Backup to the replaced character. */
2126 dec_cursor();
2127}
2128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129/*
2130 * Replace a whole area with one character.
2131 */
2132 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002133op_replace(oparg_T *oap, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134{
2135 int n, numc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 int num_chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 char_u *newp, *oldp;
2138 size_t oldlen;
2139 struct block_def bd;
Bram Moolenaard9820532013-11-04 01:41:17 +01002140 char_u *after_p = NULL;
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002141 int had_ctrl_v_cr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
2144 return OK; /* nothing to do */
2145
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002146 if (c == REPLACE_CR_NCHAR)
2147 {
2148 had_ctrl_v_cr = TRUE;
2149 c = CAR;
2150 }
2151 else if (c == REPLACE_NL_NCHAR)
2152 {
2153 had_ctrl_v_cr = TRUE;
2154 c = NL;
2155 }
Bram Moolenaard9820532013-11-04 01:41:17 +01002156
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (has_mbyte)
2158 mb_adjust_opend(oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
2160 if (u_save((linenr_T)(oap->start.lnum - 1),
2161 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2162 return FAIL;
2163
2164 /*
2165 * block mode replace
2166 */
2167 if (oap->block_mode)
2168 {
2169 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2170 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2171 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002172 curwin->w_cursor.col = 0; /* make sure cursor position is valid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2174 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2175 continue; /* nothing to replace */
2176
2177 /* n == number of extra chars required
2178 * If we split a TAB, it may be replaced by several characters.
2179 * Thus the number of characters may increase!
2180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 /* If the range starts in virtual space, count the initial
2182 * coladd offset as part of "startspaces" */
2183 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2184 {
2185 pos_T vpos;
2186
Bram Moolenaara1381de2009-11-03 15:44:21 +00002187 vpos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 getvpos(&vpos, oap->start_vcol);
2189 bd.startspaces += vpos.coladd;
2190 n = bd.startspaces;
2191 }
2192 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 /* allow for pre spaces */
2194 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2195
2196 /* allow for post spp */
2197 n += (bd.endspaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 && !bd.is_oneChar
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2200 /* Figure out how many characters to replace. */
2201 numc = oap->end_vcol - oap->start_vcol + 1;
2202 if (bd.is_short && (!virtual_op || bd.is_MAX))
2203 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /* A double-wide character can be replaced only up to half the
2206 * times. */
2207 if ((*mb_char2cells)(c) > 1)
2208 {
2209 if ((numc & 1) && !bd.is_short)
2210 {
2211 ++bd.endspaces;
2212 ++n;
2213 }
2214 numc = numc / 2;
2215 }
2216
2217 /* Compute bytes needed, move character count to num_chars. */
2218 num_chars = numc;
2219 numc *= (*mb_char2len)(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 /* oldlen includes textlen, so don't double count */
2221 n += numc - bd.textlen;
2222
2223 oldp = ml_get_curline();
2224 oldlen = STRLEN(oldp);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002225 newp = alloc(oldlen + 1 + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 if (newp == NULL)
2227 continue;
2228 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2229 /* copy up to deleted part */
2230 mch_memmove(newp, oldp, (size_t)bd.textcol);
2231 oldp += bd.textcol + bd.textlen;
2232 /* insert pre-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002233 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
Bram Moolenaarf12519d2018-02-06 22:52:49 +01002235 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
2236 * literally. */
Bram Moolenaard9820532013-11-04 01:41:17 +01002237 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002239 if (has_mbyte)
2240 {
2241 n = (int)STRLEN(newp);
2242 while (--num_chars >= 0)
2243 n += (*mb_char2bytes)(c, newp + n);
2244 }
2245 else
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002246 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
Bram Moolenaard9820532013-11-04 01:41:17 +01002247 if (!bd.is_short)
2248 {
2249 /* insert post-spaces */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002250 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
Bram Moolenaard9820532013-11-04 01:41:17 +01002251 /* copy the part after the changed part */
2252 STRMOVE(newp + STRLEN(newp), oldp);
2253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaard9820532013-11-04 01:41:17 +01002257 /* Replacing with \r or \n means splitting the line. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002258 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
Bram Moolenaard9820532013-11-04 01:41:17 +01002259 if (after_p != NULL)
2260 STRMOVE(after_p, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 /* replace the line */
2263 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaard9820532013-11-04 01:41:17 +01002264 if (after_p != NULL)
2265 {
2266 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
2267 appended_lines_mark(curwin->w_cursor.lnum, 1L);
2268 oap->end.lnum++;
2269 vim_free(after_p);
2270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272 }
2273 else
2274 {
2275 /*
2276 * MCHAR and MLINE motion replace.
2277 */
2278 if (oap->motion_type == MLINE)
2279 {
2280 oap->start.col = 0;
2281 curwin->w_cursor.col = 0;
2282 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2283 if (oap->end.col)
2284 --oap->end.col;
2285 }
2286 else if (!oap->inclusive)
2287 dec(&(oap->end));
2288
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002289 while (LTOREQ_POS(curwin->w_cursor, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 n = gchar_cursor();
2292 if (n != NUL)
2293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2295 {
2296 /* This is slow, but it handles replacing a single-byte
2297 * with a multi-byte and the other way around. */
Bram Moolenaardb813952013-03-07 18:50:57 +01002298 if (curwin->w_cursor.lnum == oap->end.lnum)
2299 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
Bram Moolenaar630afe82018-06-28 19:26:28 +02002300 replace_character(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (n == TAB)
2305 {
2306 int end_vcol = 0;
2307
2308 if (curwin->w_cursor.lnum == oap->end.lnum)
2309 {
2310 /* oap->end has to be recalculated when
2311 * the tab breaks */
2312 end_vcol = getviscol2(oap->end.col,
2313 oap->end.coladd);
2314 }
2315 coladvance_force(getviscol());
2316 if (curwin->w_cursor.lnum == oap->end.lnum)
2317 getvpos(&oap->end, end_vcol);
2318 }
Bram Moolenaar630afe82018-06-28 19:26:28 +02002319 PBYTE(curwin->w_cursor, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2323 {
2324 int virtcols = oap->end.coladd;
2325
2326 if (curwin->w_cursor.lnum == oap->start.lnum
2327 && oap->start.col == oap->end.col && oap->start.coladd)
2328 virtcols -= oap->start.coladd;
2329
2330 /* oap->end has been trimmed so it's effectively inclusive;
2331 * as a result an extra +1 must be counted so we don't
2332 * trample the NUL byte. */
2333 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2334 curwin->w_cursor.col -= (virtcols + 1);
2335 for (; virtcols >= 0; virtcols--)
2336 {
Bram Moolenaar630afe82018-06-28 19:26:28 +02002337 if ((*mb_char2len)(c) > 1)
2338 replace_character(c);
2339 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002340 PBYTE(curwin->w_cursor, c);
2341 if (inc(&curwin->w_cursor) == -1)
2342 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 /* Advance to next character, stop at the end of the file. */
2347 if (inc_cursor() == -1)
2348 break;
2349 }
2350 }
2351
2352 curwin->w_cursor = oap->start;
2353 check_cursor();
2354 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2355
2356 /* Set "'[" and "']" marks. */
2357 curbuf->b_op_start = oap->start;
2358 curbuf->b_op_end = oap->end;
2359
2360 return OK;
2361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002363static int swapchars(int op_type, pos_T *pos, int length);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365/*
2366 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2367 */
2368 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002369op_tilde(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370{
2371 pos_T pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 struct block_def bd;
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002373 int did_change = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 if (u_save((linenr_T)(oap->start.lnum - 1),
2376 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2377 return;
2378
2379 pos = oap->start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (oap->block_mode) /* Visual block mode */
2381 {
2382 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2383 {
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002384 int one_change;
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 block_prep(oap, &bd, pos.lnum, FALSE);
2387 pos.col = bd.textcol;
Bram Moolenaar555b3d52008-12-03 12:38:36 +00002388 one_change = swapchars(oap->op_type, &pos, bd.textlen);
2389 did_change |= one_change;
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002390
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002391#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002392 if (netbeans_active() && one_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2395
Bram Moolenaar009b2592004-10-24 19:18:58 +00002396 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2397 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002399 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 if (did_change)
2404 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2405 }
2406 else /* not block mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 if (oap->motion_type == MLINE)
2409 {
2410 oap->start.col = 0;
2411 pos.col = 0;
2412 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2413 if (oap->end.col)
2414 --oap->end.col;
2415 }
2416 else if (!oap->inclusive)
2417 dec(&(oap->end));
2418
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002419 if (pos.lnum == oap->end.lnum)
2420 did_change = swapchars(oap->op_type, &pos,
2421 oap->end.col - pos.col + 1);
2422 else
2423 for (;;)
2424 {
2425 did_change |= swapchars(oap->op_type, &pos,
2426 pos.lnum == oap->end.lnum ? oap->end.col + 1:
2427 (int)STRLEN(ml_get_pos(&pos)));
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002428 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
Bram Moolenaare60c58d2008-02-06 13:44:43 +00002429 break;
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 if (did_change)
2432 {
2433 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2434 0L);
2435#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002436 if (netbeans_active() && did_change)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 char_u *ptr;
2439 int count;
2440
2441 pos = oap->start;
2442 while (pos.lnum < oap->end.lnum)
2443 {
2444 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002445 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002446 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002448 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 pos.col = 0;
2450 pos.lnum++;
2451 }
2452 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2453 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002454 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002456 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458#endif
2459 }
2460 }
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (!did_change && oap->is_VIsual)
2463 /* No change: need to remove the Visual selection */
2464 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /*
2467 * Set '[ and '] marks.
2468 */
2469 curbuf->b_op_start = oap->start;
2470 curbuf->b_op_end = oap->end;
2471
2472 if (oap->line_count > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002473 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02002474 oap->line_count), oap->line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475}
2476
2477/*
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002478 * Invoke swapchar() on "length" bytes at position "pos".
2479 * "pos" is advanced to just after the changed characters.
2480 * "length" is rounded up to include the whole last multi-byte character.
2481 * Also works correctly when the number of bytes changes.
2482 * Returns TRUE if some character was changed.
2483 */
2484 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002485swapchars(int op_type, pos_T *pos, int length)
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002486{
2487 int todo;
2488 int did_change = 0;
2489
2490 for (todo = length; todo > 0; --todo)
2491 {
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002492 if (has_mbyte)
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002493 {
2494 int len = (*mb_ptr2len)(ml_get_pos(pos));
2495
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002496 /* we're counting bytes, not characters */
Bram Moolenaarf17968b2013-08-09 19:48:40 +02002497 if (len > 0)
2498 todo -= len - 1;
2499 }
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002500 did_change |= swapchar(op_type, pos);
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002501 if (inc(pos) == -1) /* at end of file */
2502 break;
2503 }
2504 return did_change;
2505}
2506
2507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 * If op_type == OP_UPPER: make uppercase,
2509 * if op_type == OP_LOWER: make lowercase,
2510 * if op_type == OP_ROT13: do rot13 encoding,
2511 * else swap case of character at 'pos'
2512 * returns TRUE when something actually changed.
2513 */
2514 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002515swapchar(int op_type, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516{
2517 int c;
2518 int nc;
2519
2520 c = gchar_pos(pos);
2521
2522 /* Only do rot13 encoding for ASCII characters. */
2523 if (c >= 0x80 && op_type == OP_ROT13)
2524 return FALSE;
2525
Bram Moolenaarb44df0a2008-01-22 15:02:31 +00002526 if (op_type == OP_UPPER && c == 0xdf
2527 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002528 {
2529 pos_T sp = curwin->w_cursor;
2530
2531 /* Special handling of German sharp s: change to "SS". */
2532 curwin->w_cursor = *pos;
2533 del_char(FALSE);
2534 ins_char('S');
2535 ins_char('S');
2536 curwin->w_cursor = sp;
2537 inc(pos);
2538 }
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2541 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 nc = c;
2543 if (MB_ISLOWER(c))
2544 {
2545 if (op_type == OP_ROT13)
2546 nc = ROT13(c, 'a');
2547 else if (op_type != OP_LOWER)
2548 nc = MB_TOUPPER(c);
2549 }
2550 else if (MB_ISUPPER(c))
2551 {
2552 if (op_type == OP_ROT13)
2553 nc = ROT13(c, 'A');
2554 else if (op_type != OP_UPPER)
2555 nc = MB_TOLOWER(c);
2556 }
2557 if (nc != c)
2558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2560 {
2561 pos_T sp = curwin->w_cursor;
2562
2563 curwin->w_cursor = *pos;
Bram Moolenaard1cb65e2010-08-01 14:22:48 +02002564 /* don't use del_char(), it also removes composing chars */
2565 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 ins_char(nc);
2567 curwin->w_cursor = sp;
2568 }
2569 else
Bram Moolenaar630afe82018-06-28 19:26:28 +02002570 PBYTE(*pos, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 return TRUE;
2572 }
2573 return FALSE;
2574}
2575
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576/*
2577 * op_insert - Insert and append operators for Visual mode.
2578 */
2579 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002580op_insert(oparg_T *oap, long count1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 long ins_len, pre_textlen = 0;
2583 char_u *firstline, *ins_text;
Bram Moolenaar2254a8a2017-09-03 14:03:43 +02002584 colnr_T ind_pre = 0, ind_post;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 struct block_def bd;
2586 int i;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002587 pos_T t1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589 /* edit() changes this - record it for OP_APPEND */
2590 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2591
2592 /* vis block is still marked. Get rid of it now. */
2593 curwin->w_cursor.lnum = oap->start.lnum;
2594 update_screen(INVERTED);
2595
2596 if (oap->block_mode)
2597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 /* When 'virtualedit' is used, need to insert the extra spaces before
2599 * doing block_prep(). When only "block" is used, virtual edit is
2600 * already disabled, but still need it when calling
2601 * coladvance_force(). */
2602 if (curwin->w_cursor.coladd > 0)
2603 {
2604 int old_ve_flags = ve_flags;
2605
2606 ve_flags = VE_ALL;
2607 if (u_save_cursor() == FAIL)
2608 return;
2609 coladvance_force(oap->op_type == OP_APPEND
2610 ? oap->end_vcol + 1 : getviscol());
2611 if (oap->op_type == OP_APPEND)
2612 --curwin->w_cursor.col;
2613 ve_flags = old_ve_flags;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 /* Get the info about the block before entering the text */
2616 block_prep(oap, &bd, oap->start.lnum, TRUE);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002617 /* Get indent information */
2618 ind_pre = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 firstline = ml_get(oap->start.lnum) + bd.textcol;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (oap->op_type == OP_APPEND)
2622 firstline += bd.textlen;
2623 pre_textlen = (long)STRLEN(firstline);
2624 }
2625
2626 if (oap->op_type == OP_APPEND)
2627 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002628 if (oap->block_mode && curwin->w_cursor.coladd == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 /* Move the cursor to the character right of the block. */
2631 curwin->w_set_curswant = TRUE;
2632 while (*ml_get_cursor() != NUL
2633 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2634 ++curwin->w_cursor.col;
2635 if (bd.is_short && !bd.is_MAX)
2636 {
2637 /* First line was too short, make it longer and adjust the
2638 * values in "bd". */
2639 if (u_save_cursor() == FAIL)
2640 return;
2641 for (i = 0; i < bd.endspaces; ++i)
2642 ins_char(' ');
2643 bd.textlen += bd.endspaces;
2644 }
2645 }
2646 else
2647 {
2648 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002649 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 /* Works just like an 'i'nsert on the next character. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002652 if (!LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 && oap->start_vcol != oap->end_vcol)
2654 inc_cursor();
2655 }
2656 }
2657
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002658 t1 = oap->start;
Bram Moolenaar23fa81d2017-02-01 21:50:21 +01002659 (void)edit(NUL, FALSE, (linenr_T)count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002661 /* When a tab was inserted, and the characters in front of the tab
2662 * have been converted to a tab as well, the column of the cursor
2663 * might have actually been reduced, so need to adjust here. */
2664 if (t1.lnum == curbuf->b_op_start_orig.lnum
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002665 && LT_POS(curbuf->b_op_start_orig, t1))
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002666 oap->start = curbuf->b_op_start_orig;
2667
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002668 /* If user has moved off this line, we don't know what to do, so do
2669 * nothing.
2670 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2671 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 return;
2673
2674 if (oap->block_mode)
2675 {
2676 struct block_def bd2;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002677 int did_indent = FALSE;
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002678 size_t len;
2679 int add;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002681 /* If indent kicked in, the firstline might have changed
2682 * but only do that, if the indent actually increased. */
2683 ind_post = (colnr_T)getwhitecols_curline();
2684 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
2685 {
2686 bd.textcol += ind_post - ind_pre;
2687 bd.start_vcol += ind_post - ind_pre;
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002688 did_indent = TRUE;
Bram Moolenaar4ec86dd2017-09-02 23:28:54 +02002689 }
2690
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002691 /* The user may have moved the cursor before inserting something, try
Bram Moolenaar8c87a2b2018-04-10 13:15:47 +02002692 * to adjust the block for that. But only do it, if the difference
2693 * does not come from indent kicking in. */
2694 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
2695 && !bd.is_MAX && !did_indent)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002696 {
2697 if (oap->op_type == OP_INSERT
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002698 && oap->start.col + oap->start.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002699 != curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002700 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002701 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002702 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002703 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002704 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002705 pre_textlen -= t - oap->start_vcol;
2706 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002707 }
2708 else if (oap->op_type == OP_APPEND
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002709 && oap->end.col + oap->end.coladd
Bram Moolenaar4c9a9492014-03-19 18:57:54 +01002710 >= curbuf->b_op_start_orig.col
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002711 + curbuf->b_op_start_orig.coladd)
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002712 {
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002713 int t = getviscol2(curbuf->b_op_start_orig.col,
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002714 curbuf->b_op_start_orig.coladd);
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01002715 oap->start.col = curbuf->b_op_start_orig.col;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002716 /* reset pre_textlen to the value of OP_INSERT */
2717 pre_textlen += bd.textlen;
Bram Moolenaarf2c03d72015-02-03 18:36:44 +01002718 pre_textlen -= t - oap->start_vcol;
2719 oap->start_vcol = t;
Bram Moolenaar3f75e422013-11-11 01:29:22 +01002720 oap->op_type = OP_INSERT;
2721 }
2722 }
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 /*
2725 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002726 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 * Don't do this when "$" used, end-of-line will have changed.
2728 */
2729 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2730 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2731 {
2732 if (oap->op_type == OP_APPEND)
2733 {
2734 pre_textlen += bd2.textlen - bd.textlen;
2735 if (bd2.endspaces)
2736 --bd2.textlen;
2737 }
2738 bd.textcol = bd2.textcol;
2739 bd.textlen = bd2.textlen;
2740 }
2741
2742 /*
2743 * Subsequent calls to ml_get() flush the firstline data - take a
2744 * copy of the required string.
2745 */
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002746 firstline = ml_get(oap->start.lnum);
2747 len = STRLEN(firstline);
2748 add = bd.textcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (oap->op_type == OP_APPEND)
Bram Moolenaar35e802e2018-04-30 17:21:03 +02002750 add += bd.textlen;
2751 if ((size_t)add > len)
2752 firstline += len; // short line, point to the NUL
2753 else
2754 firstline += add;
Bram Moolenaar5e4b9e92012-03-23 14:16:23 +01002755 if (pre_textlen >= 0
2756 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
2758 ins_text = vim_strnsave(firstline, (int)ins_len);
2759 if (ins_text != NULL)
2760 {
2761 /* block handled here */
2762 if (u_save(oap->start.lnum,
2763 (linenr_T)(oap->end.lnum + 1)) == OK)
2764 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2765 &bd);
2766
2767 curwin->w_cursor.col = oap->start.col;
2768 check_cursor();
2769 vim_free(ins_text);
2770 }
2771 }
2772 }
2773}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775/*
2776 * op_change - handle a change operation
2777 *
2778 * return TRUE if edit() returns because of a CTRL-O command
2779 */
2780 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002781op_change(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
2783 colnr_T l;
2784 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 long offset;
2786 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002787 long ins_len;
2788 long pre_textlen = 0;
2789 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 char_u *firstline;
2791 char_u *ins_text, *newp, *oldp;
2792 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 l = oap->start.col;
2795 if (oap->motion_type == MLINE)
2796 {
2797 l = 0;
2798#ifdef FEAT_SMARTINDENT
2799 if (!p_paste && curbuf->b_p_si
2800# ifdef FEAT_CINDENT
2801 && !curbuf->b_p_cin
2802# endif
2803 )
2804 can_si = TRUE; /* It's like opening a new line, do si */
2805#endif
2806 }
2807
2808 /* First delete the text in the region. In an empty buffer only need to
2809 * save for undo */
2810 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2811 {
2812 if (u_save_cursor() == FAIL)
2813 return FALSE;
2814 }
2815 else if (op_delete(oap) == FAIL)
2816 return FALSE;
2817
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002818 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 && !virtual_op)
2820 inc_cursor();
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 /* check for still on same line (<CR> in inserted text meaningless) */
2823 /* skip blank lines too */
2824 if (oap->block_mode)
2825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 /* Add spaces before getting the current line length. */
2827 if (virtual_op && (curwin->w_cursor.coladd > 0
2828 || gchar_cursor() == NUL))
2829 coladvance_force(getviscol());
Bram Moolenaar53241da2007-09-13 20:41:32 +00002830 firstline = ml_get(oap->start.lnum);
2831 pre_textlen = (long)STRLEN(firstline);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002832 pre_indent = (long)getwhitecols(firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 bd.textcol = curwin->w_cursor.col;
2834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
2836#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2837 if (oap->motion_type == MLINE)
2838 fix_indent();
2839#endif
2840
2841 retval = edit(NUL, FALSE, (linenr_T)1);
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002844 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002846 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002848 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002850 /* Auto-indenting may have changed the indent. If the cursor was past
2851 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002853 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02002855 long new_indent = (long)getwhitecols(firstline);
Bram Moolenaar53241da2007-09-13 20:41:32 +00002856
2857 pre_textlen += new_indent - pre_indent;
2858 bd.textcol += new_indent - pre_indent;
2859 }
2860
2861 ins_len = (long)STRLEN(firstline) - pre_textlen;
2862 if (ins_len > 0)
2863 {
2864 /* Subsequent calls to ml_get() flush the firstline data - take a
2865 * copy of the inserted text. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002866 if ((ins_text = alloc(ins_len + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002868 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2870 linenr++)
2871 {
2872 block_prep(oap, &bd, linenr, TRUE);
2873 if (!bd.is_short || virtual_op)
2874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 pos_T vpos;
2876
2877 /* If the block starts in virtual space, count the
2878 * initial coladd offset as part of "startspaces" */
2879 if (bd.is_short)
2880 {
Bram Moolenaara1381de2009-11-03 15:44:21 +00002881 vpos.lnum = linenr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 (void)getvpos(&vpos, oap->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 else
2885 vpos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 oldp = ml_get(linenr);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002887 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (newp == NULL)
2889 continue;
2890 /* copy up to block start */
2891 mch_memmove(newp, oldp, (size_t)bd.textcol);
2892 offset = bd.textcol;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002893 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 offset += vpos.coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2896 offset += ins_len;
2897 oldp += bd.textcol;
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00002898 STRMOVE(newp + offset, oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 ml_replace(linenr, newp, FALSE);
2900 }
2901 }
2902 check_cursor();
2903
2904 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2905 }
2906 vim_free(ins_text);
2907 }
2908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 return retval;
2911}
2912
2913/*
2914 * set all the yank registers to empty (called from main())
2915 */
2916 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002917init_yank(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
2919 int i;
2920
2921 for (i = 0; i < NUM_REGISTERS; ++i)
2922 y_regs[i].y_array = NULL;
2923}
2924
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002925#if defined(EXITFREE) || defined(PROTO)
2926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002927clear_registers(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002928{
2929 int i;
2930
2931 for (i = 0; i < NUM_REGISTERS; ++i)
2932 {
2933 y_current = &y_regs[i];
2934 if (y_current->y_array != NULL)
2935 free_yank_all();
2936 }
2937}
2938#endif
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940/*
2941 * Free "n" lines from the current yank register.
2942 * Called for normal freeing and in case of error.
2943 */
2944 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002945free_yank(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
2947 if (y_current->y_array != NULL)
2948 {
2949 long i;
2950
2951 for (i = n; --i >= 0; )
2952 {
2953#ifdef AMIGA /* only for very slow machines */
2954 if ((i & 1023) == 1023) /* this may take a while */
2955 {
2956 /*
2957 * This message should never cause a hit-return message.
2958 * Overwrite this message with any next message.
2959 */
2960 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002961 smsg(_("freeing %ld lines"), i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 --no_wait_return;
2963 msg_didout = FALSE;
2964 msg_col = 0;
2965 }
2966#endif
2967 vim_free(y_current->y_array[i]);
2968 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01002969 VIM_CLEAR(y_current->y_array);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#ifdef AMIGA
2971 if (n >= 1000)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002972 msg("");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#endif
2974 }
2975}
2976
2977 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002978free_yank_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 free_yank(y_current->y_size);
2981}
2982
2983/*
2984 * Yank the text between "oap->start" and "oap->end" into a yank register.
2985 * If we are to append (uppercase register), we first yank into a new yank
2986 * register and then concatenate the old and the new one (so we keep the old
2987 * one in case of out-of-memory).
2988 *
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02002989 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 */
2991 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002992op_yank(oparg_T *oap, int deleting, int mess)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 long y_idx; /* index in y_array[] */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02002995 yankreg_T *curr; /* copy of y_current */
2996 yankreg_T newreg; /* new yank register when appending */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 char_u **new_ptr;
2998 linenr_T lnum; /* current line number */
2999 long j;
3000 int yanktype = oap->motion_type;
3001 long yanklines = oap->line_count;
3002 linenr_T yankendlnum = oap->end.lnum;
3003 char_u *p;
3004 char_u *pnew;
3005 struct block_def bd;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003006#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003007 int did_star = FALSE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010 /* check for read-only register */
3011 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
3012 {
3013 beep_flush();
3014 return FAIL;
3015 }
3016 if (oap->regname == '_') /* black hole: nothing to do */
3017 return OK;
3018
3019#ifdef FEAT_CLIPBOARD
3020 if (!clip_star.available && oap->regname == '*')
3021 oap->regname = 0;
3022 else if (!clip_plus.available && oap->regname == '+')
3023 oap->regname = 0;
3024#endif
3025
3026 if (!deleting) /* op_delete() already set y_current */
3027 get_yank_register(oap->regname, TRUE);
3028
3029 curr = y_current;
3030 /* append to existing contents */
3031 if (y_append && y_current->y_array != NULL)
3032 y_current = &newreg;
3033 else
3034 free_yank_all(); /* free previously yanked lines */
3035
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00003036 /*
3037 * If the cursor was in column 1 before and after the movement, and the
3038 * operator is not inclusive, the yank is always linewise.
3039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if ( oap->motion_type == MCHAR
3041 && oap->start.col == 0
3042 && !oap->inclusive
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00003044 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 && oap->end.col == 0
3046 && yanklines > 1)
3047 {
3048 yanktype = MLINE;
3049 --yankendlnum;
3050 --yanklines;
3051 }
3052
3053 y_current->y_size = yanklines;
3054 y_current->y_type = yanktype; /* set the yank register type */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 y_current->y_width = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003056 y_current->y_array = lalloc_clear(sizeof(char_u *) * yanklines, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 if (y_current->y_array == NULL)
3058 {
3059 y_current = curr;
3060 return FAIL;
3061 }
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003062#ifdef FEAT_VIMINFO
3063 y_current->y_time_set = vim_time();
3064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
3066 y_idx = 0;
3067 lnum = oap->start.lnum;
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 if (oap->block_mode)
3070 {
3071 /* Visual block mode */
3072 y_current->y_type = MBLOCK; /* set the yank register type */
3073 y_current->y_width = oap->end_vcol - oap->start_vcol;
3074
3075 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
3076 y_current->y_width--;
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
3080 {
3081 switch (y_current->y_type)
3082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 case MBLOCK:
3084 block_prep(oap, &bd, lnum, FALSE);
3085 if (yank_copy_line(&bd, y_idx) == FAIL)
3086 goto fail;
3087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 case MLINE:
3090 if ((y_current->y_array[y_idx] =
3091 vim_strsave(ml_get(lnum))) == NULL)
3092 goto fail;
3093 break;
3094
3095 case MCHAR:
3096 {
3097 colnr_T startcol = 0, endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int is_oneChar = FALSE;
3099 colnr_T cs, ce;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 p = ml_get(lnum);
3102 bd.startspaces = 0;
3103 bd.endspaces = 0;
3104
3105 if (lnum == oap->start.lnum)
3106 {
3107 startcol = oap->start.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (virtual_op)
3109 {
3110 getvcol(curwin, &oap->start, &cs, NULL, &ce);
3111 if (ce != cs && oap->start.coladd > 0)
3112 {
3113 /* Part of a tab selected -- but don't
3114 * double-count it. */
3115 bd.startspaces = (ce - cs + 1)
3116 - oap->start.coladd;
3117 startcol++;
3118 }
3119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121
3122 if (lnum == oap->end.lnum)
3123 {
3124 endcol = oap->end.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (virtual_op)
3126 {
3127 getvcol(curwin, &oap->end, &cs, NULL, &ce);
3128 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 /* Don't add space for double-wide
3130 * char; endcol will be on last byte
3131 * of multi-byte char. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003132 && (*mb_head_off)(p, p + endcol) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134 if (oap->start.lnum == oap->end.lnum
3135 && oap->start.col == oap->end.col)
3136 {
3137 /* Special case: inside a single char */
3138 is_oneChar = TRUE;
3139 bd.startspaces = oap->end.coladd
3140 - oap->start.coladd + oap->inclusive;
3141 endcol = startcol;
3142 }
3143 else
3144 {
3145 bd.endspaces = oap->end.coladd
3146 + oap->inclusive;
3147 endcol -= oap->inclusive;
3148 }
3149 }
3150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 }
Bram Moolenaar18e00d22012-05-18 12:49:40 +02003152 if (endcol == MAXCOL)
3153 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003154 if (startcol > endcol || is_oneChar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 bd.textlen = 0;
3156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 bd.textlen = endcol - startcol + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 bd.textstart = p + startcol;
3159 if (yank_copy_line(&bd, y_idx) == FAIL)
3160 goto fail;
3161 break;
3162 }
3163 /* NOTREACHED */
3164 }
3165 }
3166
3167 if (curr != y_current) /* append the new block to the old block */
3168 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003169 new_ptr = ALLOC_MULT(char_u *, curr->y_size + y_current->y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (new_ptr == NULL)
3171 goto fail;
3172 for (j = 0; j < curr->y_size; ++j)
3173 new_ptr[j] = curr->y_array[j];
3174 vim_free(curr->y_array);
3175 curr->y_array = new_ptr;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003176#ifdef FEAT_VIMINFO
3177 curr->y_time_set = vim_time();
3178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
3181 curr->y_type = MLINE;
3182
Bram Moolenaar4399ef42005-02-12 14:29:27 +00003183 /* Concatenate the last line of the old block with the first line of
3184 * the new block, unless being Vi compatible. */
3185 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02003187 pnew = alloc(STRLEN(curr->y_array[curr->y_size - 1])
3188 + STRLEN(y_current->y_array[0]) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (pnew == NULL)
3190 {
3191 y_idx = y_current->y_size - 1;
3192 goto fail;
3193 }
3194 STRCPY(pnew, curr->y_array[--j]);
3195 STRCAT(pnew, y_current->y_array[0]);
3196 vim_free(curr->y_array[j]);
3197 vim_free(y_current->y_array[0]);
3198 curr->y_array[j++] = pnew;
3199 y_idx = 1;
3200 }
3201 else
3202 y_idx = 0;
3203 while (y_idx < y_current->y_size)
3204 curr->y_array[j++] = y_current->y_array[y_idx++];
3205 curr->y_size = j;
3206 vim_free(y_current->y_array);
3207 y_current = curr;
3208 }
Bram Moolenaar7ec83432014-06-17 18:16:11 +02003209 if (curwin->w_p_rnu)
3210 redraw_later(SOME_VALID); /* cursor moved to start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (mess) /* Display message about yank? */
3212 {
3213 if (yanktype == MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 && yanklines == 1)
3216 yanklines = 0;
3217 /* Some versions of Vi use ">=" here, some don't... */
3218 if (yanklines > p_report)
3219 {
Bram Moolenaare45deb72017-07-16 17:56:16 +02003220 char namebuf[100];
3221
3222 if (oap->regname == NUL)
3223 *namebuf = NUL;
3224 else
3225 vim_snprintf(namebuf, sizeof(namebuf),
Bram Moolenaar60d0e972017-07-16 20:54:34 +02003226 _(" into \"%c"), oap->regname);
Bram Moolenaare45deb72017-07-16 17:56:16 +02003227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 /* redisplay now, so message is not deleted */
3229 update_topline_redraw();
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003230 if (oap->block_mode)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003232 smsg(NGETTEXT("block of %ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003233 "block of %ld lines yanked%s", yanklines),
3234 yanklines, namebuf);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003238 smsg(NGETTEXT("%ld line yanked%s",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003239 "%ld lines yanked%s", yanklines),
3240 yanklines, namebuf);
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
3243 }
3244
3245 /*
3246 * Set "'[" and "']" marks.
3247 */
3248 curbuf->b_op_start = oap->start;
3249 curbuf->b_op_end = oap->end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003250 if (yanktype == MLINE && !oap->block_mode)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003251 {
3252 curbuf->b_op_start.col = 0;
3253 curbuf->b_op_end.col = MAXCOL;
3254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256#ifdef FEAT_CLIPBOARD
3257 /*
3258 * If we were yanking to the '*' register, send result to clipboard.
3259 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3260 * to the '*' register.
3261 */
3262 if (clip_star.available
3263 && (curr == &(y_regs[STAR_REGISTER])
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003264 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003265 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 if (curr != &(y_regs[STAR_REGISTER]))
3268 /* Copy the text from register 0 to the clipboard register. */
3269 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3270
3271 clip_own_selection(&clip_star);
3272 clip_gen_set_selection(&clip_star);
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003273# ifdef FEAT_X11
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003274 did_star = TRUE;
Bram Moolenaar9c52c3a2010-12-08 14:23:15 +01003275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277
3278# ifdef FEAT_X11
3279 /*
3280 * If we were yanking to the '+' register, send result to selection.
3281 * Also copy to the '*' register, in case auto-select is off.
3282 */
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003283 if (clip_plus.available
3284 && (curr == &(y_regs[PLUS_REGISTER])
3285 || (!deleting && oap->regname == 0
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02003286 && ((clip_unnamed | clip_unnamed_saved) &
3287 CLIP_UNNAMED_PLUS))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
Bram Moolenaarbf9680e2010-12-02 21:43:16 +01003289 if (curr != &(y_regs[PLUS_REGISTER]))
3290 /* Copy the text from register 0 to the clipboard register. */
3291 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 clip_own_selection(&clip_plus);
3294 clip_gen_set_selection(&clip_plus);
Bram Moolenaar8bfe07b2017-10-15 21:47:05 +02003295 if (!clip_isautosel_star() && !clip_isautosel_plus()
3296 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3299 clip_own_selection(&clip_star);
3300 clip_gen_set_selection(&clip_star);
3301 }
3302 }
3303# endif
3304#endif
3305
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003306#if defined(FEAT_EVAL)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01003307 if (!deleting && has_textyankpost())
3308 yank_do_autocmd(oap, y_current);
3309#endif
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return OK;
3312
3313fail: /* free the allocated lines */
3314 free_yank(y_idx + 1);
3315 y_current = curr;
3316 return FAIL;
3317}
3318
3319 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003320yank_copy_line(struct block_def *bd, long y_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 char_u *pnew;
3323
3324 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3325 == NULL)
3326 return FAIL;
3327 y_current->y_array[y_idx] = pnew;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003328 vim_memset(pnew, ' ', (size_t)bd->startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 pnew += bd->startspaces;
3330 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3331 pnew += bd->textlen;
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003332 vim_memset(pnew, ' ', (size_t)bd->endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 pnew += bd->endspaces;
3334 *pnew = NUL;
3335 return OK;
3336}
3337
3338#ifdef FEAT_CLIPBOARD
3339/*
3340 * Make a copy of the y_current register to register "reg".
3341 */
3342 static void
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003343copy_yank_reg(yankreg_T *reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02003345 yankreg_T *curr = y_current;
3346 long j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 y_current = reg;
3349 free_yank_all();
3350 *y_current = *curr;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003351 y_current->y_array = lalloc_clear(
Bram Moolenaar51e14382019-05-25 20:21:28 +02003352 sizeof(char_u *) * y_current->y_size, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (y_current->y_array == NULL)
3354 y_current->y_size = 0;
3355 else
3356 for (j = 0; j < y_current->y_size; ++j)
3357 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3358 {
3359 free_yank(j);
3360 y_current->y_size = 0;
3361 break;
3362 }
3363 y_current = curr;
3364}
3365#endif
3366
3367/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003368 * Put contents of register "regname" into the text.
3369 * Caller must check "regname" to be valid!
3370 * "flags": PUT_FIXINDENT make indent look nice
3371 * PUT_CURSEND leave cursor after end of new text
3372 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 */
3374 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003375do_put(
3376 int regname,
3377 int dir, /* BACKWARD for 'P', FORWARD for 'p' */
3378 long count,
3379 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
3381 char_u *ptr;
3382 char_u *newp, *oldp;
3383 int yanklen;
3384 int totlen = 0; /* init for gcc */
3385 linenr_T lnum;
3386 colnr_T col;
3387 long i; /* index in y_array[] */
3388 int y_type;
3389 long y_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 int oldlen;
3391 long y_width = 0;
3392 colnr_T vcol;
3393 int delcount;
3394 int incr = 0;
3395 long j;
3396 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 char_u **y_array = NULL;
3398 long nr_lines = 0;
3399 pos_T new_cursor;
3400 int indent;
3401 int orig_indent = 0; /* init for gcc */
3402 int indent_diff = 0; /* init for gcc */
3403 int first_indent = TRUE;
3404 int lendiff = 0;
3405 pos_T old_pos;
3406 char_u *insert_string = NULL;
3407 int allocated = FALSE;
3408 long cnt;
3409
3410#ifdef FEAT_CLIPBOARD
3411 /* Adjust register name for "unnamed" in 'clipboard'. */
3412 adjust_clip_reg(&regname);
3413 (void)may_get_selection(regname);
3414#endif
3415
3416 if (flags & PUT_FIXINDENT)
3417 orig_indent = get_indent();
3418
3419 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3420 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3421
3422 /*
3423 * Using inserted text works differently, because the register includes
3424 * special characters (newlines, etc.).
3425 */
3426 if (regname == '.')
3427 {
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01003428 if (VIsual_active)
3429 stuffcharReadbuff(VIsual_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3431 (count == -1 ? 'O' : 'i')), count, FALSE);
3432 /* Putting the text is done later, so can't really move the cursor to
3433 * the next character. Use "l" to simulate it. */
3434 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3435 stuffcharReadbuff('l');
3436 return;
3437 }
3438
3439 /*
3440 * For special registers '%' (file name), '#' (alternate file name) and
3441 * ':' (last command line), etc. we have to create a fake yank register.
3442 */
3443 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3444 {
3445 if (insert_string == NULL)
3446 return;
3447 }
3448
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02003449 /* Autocommands may be executed when saving lines for undo. This might
3450 * make "y_array" invalid, so we start undo now to avoid that. */
3451 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
3452 goto end;
Bram Moolenaar27356ad2012-12-12 16:11:36 +01003453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (insert_string != NULL)
3455 {
3456 y_type = MCHAR;
3457#ifdef FEAT_EVAL
3458 if (regname == '=')
3459 {
3460 /* For the = register we need to split the string at NL
Bram Moolenaara554a192011-09-21 17:33:53 +02003461 * characters.
3462 * Loop twice: count the number of lines and save them. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 for (;;)
3464 {
3465 y_size = 0;
3466 ptr = insert_string;
3467 while (ptr != NULL)
3468 {
3469 if (y_array != NULL)
3470 y_array[y_size] = ptr;
3471 ++y_size;
3472 ptr = vim_strchr(ptr, '\n');
3473 if (ptr != NULL)
3474 {
3475 if (y_array != NULL)
3476 *ptr = NUL;
3477 ++ptr;
Bram Moolenaara554a192011-09-21 17:33:53 +02003478 /* A trailing '\n' makes the register linewise. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (*ptr == NUL)
3480 {
3481 y_type = MLINE;
3482 break;
3483 }
3484 }
3485 }
3486 if (y_array != NULL)
3487 break;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003488 y_array = ALLOC_MULT(char_u *, y_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (y_array == NULL)
3490 goto end;
3491 }
3492 }
3493 else
3494#endif
3495 {
3496 y_size = 1; /* use fake one-line yank register */
3497 y_array = &insert_string;
3498 }
3499 }
3500 else
3501 {
3502 get_yank_register(regname, FALSE);
3503
3504 y_type = y_current->y_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 y_width = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 y_size = y_current->y_size;
3507 y_array = y_current->y_array;
3508 }
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (y_type == MLINE)
3511 {
3512 if (flags & PUT_LINE_SPLIT)
3513 {
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003514 char_u *p;
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* "p" or "P" in Visual mode: split the lines to put the text in
3517 * between. */
3518 if (u_save_cursor() == FAIL)
3519 goto end;
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003520 p = ml_get_cursor();
3521 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003522 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003523 ptr = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (ptr == NULL)
3525 goto end;
3526 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3527 vim_free(ptr);
3528
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003529 oldp = ml_get_curline();
3530 p = oldp + curwin->w_cursor.col;
3531 if (dir == FORWARD && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003532 MB_PTR_ADV(p);
Bram Moolenaarc004bc22015-06-19 15:17:55 +02003533 ptr = vim_strnsave(oldp, p - oldp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (ptr == NULL)
3535 goto end;
3536 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3537 ++nr_lines;
3538 dir = FORWARD;
3539 }
3540 if (flags & PUT_LINE_FORWARD)
3541 {
3542 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003543 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 dir = FORWARD;
3545 }
3546 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3547 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3551 y_type = MLINE;
3552
3553 if (y_size == 0 || y_array == NULL)
3554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003555 semsg(_("E353: Nothing in register %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 regname == 0 ? (char_u *)"\"" : transchar(regname));
3557 goto end;
3558 }
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (y_type == MBLOCK)
3561 {
3562 lnum = curwin->w_cursor.lnum + y_size + 1;
3563 if (lnum > curbuf->b_ml.ml_line_count)
3564 lnum = curbuf->b_ml.ml_line_count + 1;
3565 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3566 goto end;
3567 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003568 else if (y_type == MLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 lnum = curwin->w_cursor.lnum;
3571#ifdef FEAT_FOLDING
3572 /* Correct line number for closed fold. Don't move the cursor yet,
3573 * u_save() uses it. */
3574 if (dir == BACKWARD)
3575 (void)hasFolding(lnum, &lnum, NULL);
3576 else
3577 (void)hasFolding(lnum, NULL, &lnum);
3578#endif
3579 if (dir == FORWARD)
3580 ++lnum;
Bram Moolenaar10315b12013-06-29 17:19:28 +02003581 /* In an empty buffer the empty line is going to be replaced, include
3582 * it in the saved lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003583 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 goto end;
3585#ifdef FEAT_FOLDING
3586 if (dir == FORWARD)
3587 curwin->w_cursor.lnum = lnum - 1;
3588 else
3589 curwin->w_cursor.lnum = lnum;
3590 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3591#endif
3592 }
3593 else if (u_save_cursor() == FAIL)
3594 goto end;
3595
3596 yanklen = (int)STRLEN(y_array[0]);
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (ve_flags == VE_ALL && y_type == MCHAR)
3599 {
3600 if (gchar_cursor() == TAB)
3601 {
3602 /* Don't need to insert spaces when "p" on the last position of a
3603 * tab or "P" on the first position. */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003604#ifdef FEAT_VARTABS
3605 int viscol = getviscol();
3606 if (dir == FORWARD
3607 ? tabstop_padding(viscol, curbuf->b_p_ts,
3608 curbuf->b_p_vts_array) != 1
3609 : curwin->w_cursor.coladd > 0)
3610 coladvance_force(viscol);
3611#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (dir == FORWARD
3613 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3614 : curwin->w_cursor.coladd > 0)
3615 coladvance_force(getviscol());
Bram Moolenaar04958cb2018-06-23 19:23:02 +02003616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 else
3618 curwin->w_cursor.coladd = 0;
3619 }
3620 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3621 coladvance_force(getviscol() + (dir == FORWARD));
3622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 lnum = curwin->w_cursor.lnum;
3625 col = curwin->w_cursor.col;
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 /*
3628 * Block mode
3629 */
3630 if (y_type == MBLOCK)
3631 {
Bram Moolenaarc8129962017-01-22 20:04:51 +01003632 int c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 colnr_T endcol2 = 0;
3634
3635 if (dir == FORWARD && c != NUL)
3636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (ve_flags == VE_ALL)
3638 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (has_mbyte)
3643 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003644 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (c != TAB || ve_flags != VE_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 ++curwin->w_cursor.col;
3648 ++col;
3649 }
3650 else
3651 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003654 if (ve_flags == VE_ALL
3655 && (curwin->w_cursor.coladd > 0
3656 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 if (dir == FORWARD && c == NUL)
3659 ++col;
3660 if (dir != FORWARD && c != NUL)
3661 ++curwin->w_cursor.col;
3662 if (c == TAB)
3663 {
3664 if (dir == BACKWARD && curwin->w_cursor.col)
3665 curwin->w_cursor.col--;
3666 if (dir == FORWARD && col - 1 == endcol2)
3667 curwin->w_cursor.col++;
3668 }
3669 }
3670 curwin->w_cursor.coladd = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003671 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 for (i = 0; i < y_size; ++i)
3673 {
3674 int spaces;
3675 char shortline;
3676
3677 bd.startspaces = 0;
3678 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 vcol = 0;
3680 delcount = 0;
3681
3682 /* add a new line */
3683 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3684 {
3685 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3686 (colnr_T)1, FALSE) == FAIL)
3687 break;
3688 ++nr_lines;
3689 }
3690 /* get the old line and advance to the position to insert at */
3691 oldp = ml_get_curline();
3692 oldlen = (int)STRLEN(oldp);
3693 for (ptr = oldp; vcol < col && *ptr; )
3694 {
3695 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003696 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 vcol += incr;
3698 }
3699 bd.textcol = (colnr_T)(ptr - oldp);
3700
3701 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3702
3703 if (vcol < col) /* line too short, padd with spaces */
3704 bd.startspaces = col - vcol;
3705 else if (vcol > col)
3706 {
3707 bd.endspaces = vcol - col;
3708 bd.startspaces = incr - bd.endspaces;
3709 --bd.textcol;
3710 delcount = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (has_mbyte)
3712 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (oldp[bd.textcol] != TAB)
3714 {
3715 /* Only a Tab can be split into spaces. Other
3716 * characters will have to be moved to after the
3717 * block, causing misalignment. */
3718 delcount = 0;
3719 bd.endspaces = 0;
3720 }
3721 }
3722
3723 yanklen = (int)STRLEN(y_array[i]);
3724
3725 /* calculate number of spaces required to fill right side of block*/
3726 spaces = y_width + 1;
3727 for (j = 0; j < yanklen; j++)
Bram Moolenaar597a4222014-06-25 14:39:50 +02003728 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (spaces < 0)
3730 spaces = 0;
3731
3732 /* insert the new text */
3733 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
Bram Moolenaar964b3742019-05-24 18:54:09 +02003734 newp = alloc(totlen + oldlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if (newp == NULL)
3736 break;
3737 /* copy part up to cursor to new line */
3738 ptr = newp;
3739 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3740 ptr += bd.textcol;
3741 /* may insert some spaces before the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003742 vim_memset(ptr, ' ', (size_t)bd.startspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 ptr += bd.startspaces;
3744 /* insert the new text */
3745 for (j = 0; j < count; ++j)
3746 {
3747 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3748 ptr += yanklen;
3749
3750 /* insert block's trailing spaces only if there's text behind */
3751 if ((j < count - 1 || !shortline) && spaces)
3752 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003753 vim_memset(ptr, ' ', (size_t)spaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 ptr += spaces;
3755 }
3756 }
3757 /* may insert some spaces after the new text */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003758 vim_memset(ptr, ' ', (size_t)bd.endspaces);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 ptr += bd.endspaces;
3760 /* move the text after the cursor to the end of the line. */
3761 mch_memmove(ptr, oldp + bd.textcol + delcount,
3762 (size_t)(oldlen - bd.textcol - delcount + 1));
3763 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3764
3765 ++curwin->w_cursor.lnum;
3766 if (i == 0)
3767 curwin->w_cursor.col += bd.startspaces;
3768 }
3769
3770 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3771
3772 /* Set '[ mark. */
3773 curbuf->b_op_start = curwin->w_cursor;
3774 curbuf->b_op_start.lnum = lnum;
3775
3776 /* adjust '] mark */
3777 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3778 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 curbuf->b_op_end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 if (flags & PUT_CURSEND)
3781 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003782 colnr_T len;
3783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 curwin->w_cursor = curbuf->b_op_end;
3785 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003786
3787 /* in Insert mode we might be after the NUL, correct for that */
3788 len = (colnr_T)STRLEN(ml_get_curline());
3789 if (curwin->w_cursor.col > len)
3790 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 else
3793 curwin->w_cursor.lnum = lnum;
3794 }
3795 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797 /*
3798 * Character or Line mode
3799 */
3800 if (y_type == MCHAR)
3801 {
3802 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3803 * char */
3804 if (dir == FORWARD && gchar_cursor() != NUL)
3805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (has_mbyte)
3807 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003808 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 /* put it on the next of the multi-byte character. */
3811 col += bytelen;
3812 if (yanklen)
3813 {
3814 curwin->w_cursor.col += bytelen;
3815 curbuf->b_op_end.col += bytelen;
3816 }
3817 }
3818 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
3820 ++col;
3821 if (yanklen)
3822 {
3823 ++curwin->w_cursor.col;
3824 ++curbuf->b_op_end.col;
3825 }
3826 }
3827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 curbuf->b_op_start = curwin->w_cursor;
3829 }
3830 /*
3831 * Line mode: BACKWARD is the same as FORWARD on the previous line
3832 */
3833 else if (dir == BACKWARD)
3834 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003835 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 /*
3838 * simple case: insert into current line
3839 */
3840 if (y_type == MCHAR && y_size == 1)
3841 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003842 linenr_T end_lnum = 0; /* init for gcc */
Bram Moolenaar9957a102017-01-23 21:53:53 +01003843
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003844 if (VIsual_active)
3845 {
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003846 end_lnum = curbuf->b_visual.vi_end.lnum;
3847 if (end_lnum < curbuf->b_visual.vi_start.lnum)
3848 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003849 }
Bram Moolenaar9957a102017-01-23 21:53:53 +01003850
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003851 do {
3852 totlen = count * yanklen;
3853 if (totlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003855 oldp = ml_get(lnum);
Bram Moolenaar941c12d2017-01-24 19:55:43 +01003856 if (VIsual_active && col > (int)STRLEN(oldp))
3857 {
3858 lnum++;
3859 continue;
3860 }
Bram Moolenaar964b3742019-05-24 18:54:09 +02003861 newp = alloc(STRLEN(oldp) + totlen + 1);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003862 if (newp == NULL)
3863 goto end; /* alloc() gave an error message */
3864 mch_memmove(newp, oldp, (size_t)col);
3865 ptr = newp + col;
3866 for (i = 0; i < count; ++i)
3867 {
3868 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3869 ptr += yanklen;
3870 }
3871 STRMOVE(ptr, oldp + col);
3872 ml_replace(lnum, newp, FALSE);
3873 /* Place cursor on last putted char. */
3874 if (lnum == curwin->w_cursor.lnum)
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003875 {
3876 /* make sure curwin->w_virtcol is updated */
3877 changed_cline_bef_curs();
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003878 curwin->w_cursor.col += (colnr_T)(totlen - 1);
Bram Moolenaar1e42f7a2013-11-21 13:24:41 +01003879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003881 if (VIsual_active)
3882 lnum++;
Bram Moolenaar6a717f12017-01-24 20:47:50 +01003883 } while (VIsual_active && lnum <= end_lnum);
Bram Moolenaarec11aef2013-09-22 15:23:44 +02003884
Bram Moolenaar06e7ce12014-11-19 17:35:39 +01003885 if (VIsual_active) /* reset lnum to the last visual line */
3886 lnum--;
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 curbuf->b_op_end = curwin->w_cursor;
3889 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3890 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3891 ++curwin->w_cursor.col;
3892 changed_bytes(lnum, col);
3893 }
3894 else
3895 {
3896 /*
3897 * Insert at least one line. When y_type is MCHAR, break the first
3898 * line in two.
3899 */
3900 for (cnt = 1; cnt <= count; ++cnt)
3901 {
3902 i = 0;
3903 if (y_type == MCHAR)
3904 {
3905 /*
3906 * Split the current line in two at the insert position.
3907 * First insert y_array[size - 1] in front of second line.
3908 * Then append y_array[0] to first line.
3909 */
3910 lnum = new_cursor.lnum;
3911 ptr = ml_get(lnum) + col;
3912 totlen = (int)STRLEN(y_array[y_size - 1]);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003913 newp = alloc(STRLEN(ptr) + totlen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (newp == NULL)
3915 goto error;
3916 STRCPY(newp, y_array[y_size - 1]);
3917 STRCAT(newp, ptr);
3918 /* insert second line */
3919 ml_append(lnum, newp, (colnr_T)0, FALSE);
3920 vim_free(newp);
3921
3922 oldp = ml_get(lnum);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003923 newp = alloc(col + yanklen + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (newp == NULL)
3925 goto error;
3926 /* copy first part of line */
3927 mch_memmove(newp, oldp, (size_t)col);
3928 /* append to first line */
3929 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3930 ml_replace(lnum, newp, FALSE);
3931
3932 curwin->w_cursor.lnum = lnum;
3933 i = 1;
3934 }
3935
3936 for (; i < y_size; ++i)
3937 {
3938 if ((y_type != MCHAR || i < y_size - 1)
3939 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3940 == FAIL)
3941 goto error;
3942 lnum++;
3943 ++nr_lines;
3944 if (flags & PUT_FIXINDENT)
3945 {
3946 old_pos = curwin->w_cursor;
3947 curwin->w_cursor.lnum = lnum;
3948 ptr = ml_get(lnum);
3949 if (cnt == count && i == y_size - 1)
3950 lendiff = (int)STRLEN(ptr);
3951#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3952 if (*ptr == '#' && preprocs_left())
3953 indent = 0; /* Leave # lines at start */
3954 else
3955#endif
3956 if (*ptr == NUL)
3957 indent = 0; /* Ignore empty lines */
3958 else if (first_indent)
3959 {
3960 indent_diff = orig_indent - get_indent();
3961 indent = orig_indent;
3962 first_indent = FALSE;
3963 }
3964 else if ((indent = get_indent() + indent_diff) < 0)
3965 indent = 0;
3966 (void)set_indent(indent, 0);
3967 curwin->w_cursor = old_pos;
3968 /* remember how many chars were removed */
3969 if (cnt == count && i == y_size - 1)
3970 lendiff -= (int)STRLEN(ml_get(lnum));
3971 }
3972 }
3973 }
3974
3975error:
3976 /* Adjust marks. */
3977 if (y_type == MLINE)
3978 {
3979 curbuf->b_op_start.col = 0;
3980 if (dir == FORWARD)
3981 curbuf->b_op_start.lnum++;
3982 }
Bram Moolenaar82faa252016-06-04 20:14:07 +02003983 /* Skip mark_adjust when adding lines after the last one, there
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003984 * can't be marks there. But still needed in diff mode. */
Bram Moolenaar82faa252016-06-04 20:14:07 +02003985 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003986 < curbuf->b_ml.ml_line_count
3987#ifdef FEAT_DIFF
3988 || curwin->w_p_diff
3989#endif
3990 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003991 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 (linenr_T)MAXLNUM, nr_lines, 0L);
3993
3994 /* note changed text for displaying and folding */
3995 if (y_type == MCHAR)
3996 changed_lines(curwin->w_cursor.lnum, col,
3997 curwin->w_cursor.lnum + 1, nr_lines);
3998 else
3999 changed_lines(curbuf->b_op_start.lnum, 0,
4000 curbuf->b_op_start.lnum, nr_lines);
4001
4002 /* put '] mark at last inserted character */
4003 curbuf->b_op_end.lnum = lnum;
4004 /* correct length for change in indent */
4005 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
4006 if (col > 1)
4007 curbuf->b_op_end.col = col - 1;
4008 else
4009 curbuf->b_op_end.col = 0;
4010
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004011 if (flags & PUT_CURSLINE)
4012 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00004013 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004014 curwin->w_cursor.lnum = lnum;
4015 beginline(BL_WHITE | BL_FIX);
4016 }
4017 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
4019 /* put cursor after inserted text */
4020 if (y_type == MLINE)
4021 {
4022 if (lnum >= curbuf->b_ml.ml_line_count)
4023 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4024 else
4025 curwin->w_cursor.lnum = lnum + 1;
4026 curwin->w_cursor.col = 0;
4027 }
4028 else
4029 {
4030 curwin->w_cursor.lnum = lnum;
4031 curwin->w_cursor.col = col;
4032 }
4033 }
4034 else if (y_type == MLINE)
4035 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004036 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 curwin->w_cursor.col = 0;
4038 if (dir == FORWARD)
4039 ++curwin->w_cursor.lnum;
4040 beginline(BL_WHITE | BL_FIX);
4041 }
4042 else /* put cursor on first inserted character */
4043 curwin->w_cursor = new_cursor;
4044 }
4045 }
4046
4047 msgmore(nr_lines);
4048 curwin->w_set_curswant = TRUE;
4049
4050end:
4051 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004053 if (regname == '=')
4054 vim_free(y_array);
4055
Bram Moolenaar033d8882013-09-25 23:24:57 +02004056 VIsual_active = FALSE;
Bram Moolenaar033d8882013-09-25 23:24:57 +02004057
Bram Moolenaar677ee682005-01-27 14:41:15 +00004058 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004059 adjust_cursor_eol();
4060}
4061
4062/*
4063 * When the cursor is on the NUL past the end of the line and it should not be
4064 * there move it left.
4065 */
4066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004067adjust_cursor_eol(void)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004068{
4069 if (curwin->w_cursor.col > 0
4070 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00004071 && (ve_flags & VE_ONEMORE) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 && !(restart_edit || (State & INSERT)))
4073 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00004074 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00004075 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004078 {
4079 colnr_T scol, ecol;
4080
4081 /* Coladd is set to the width of the last character. */
4082 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4083 curwin->w_cursor.coladd = ecol - scol + 1;
4084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
4086}
4087
4088#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
4089/*
4090 * Return TRUE if lines starting with '#' should be left aligned.
4091 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004092 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004093preprocs_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 return
4096# ifdef FEAT_SMARTINDENT
4097# ifdef FEAT_CINDENT
4098 (curbuf->b_p_si && !curbuf->b_p_cin) ||
4099# else
4100 curbuf->b_p_si
4101# endif
4102# endif
4103# ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004104 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE)
4105 && curbuf->b_ind_hash_comment == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106# endif
4107 ;
4108}
4109#endif
4110
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004111/*
4112 * Return the character name of the register with the given number.
4113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004115get_register_name(int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
4117 if (num == -1)
4118 return '"';
4119 else if (num < 10)
4120 return num + '0';
4121 else if (num == DELETION_REGISTER)
4122 return '-';
4123#ifdef FEAT_CLIPBOARD
4124 else if (num == STAR_REGISTER)
4125 return '*';
4126 else if (num == PLUS_REGISTER)
4127 return '+';
4128#endif
4129 else
4130 {
4131#ifdef EBCDIC
4132 int i;
4133
4134 /* EBCDIC is really braindead ... */
4135 i = 'a' + (num - 10);
4136 if (i > 'i')
4137 i += 7;
4138 if (i > 'r')
4139 i += 8;
4140 return i;
4141#else
4142 return num + 'a' - 10;
4143#endif
4144 }
4145}
4146
4147/*
4148 * ":dis" and ":registers": Display the contents of the yank registers.
4149 */
4150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004151ex_display(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004153 int i, n;
4154 long j;
4155 char_u *p;
4156 yankreg_T *yb;
4157 int name;
4158 int attr;
4159 char_u *arg = eap->arg;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02004160 int clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 if (arg != NULL && *arg == NUL)
4163 arg = NULL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004164 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 /* Highlight title */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004167 msg_puts_title(_("\n--- Registers ---"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
4169 {
4170 name = get_register_name(i);
Bram Moolenaar0818b872010-11-24 14:28:58 +01004171 if (arg != NULL && vim_strchr(arg, name) == NULL
4172#ifdef ONE_CLIPBOARD
4173 /* Star register and plus register contain the same thing. */
4174 && (name != '*' || vim_strchr(arg, '+') == NULL)
4175#endif
4176 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 continue; /* did not ask for this register */
4178
4179#ifdef FEAT_CLIPBOARD
4180 /* Adjust register name for "unnamed" in 'clipboard'.
4181 * When it's a clipboard register, fill it with the current contents
4182 * of the clipboard. */
4183 adjust_clip_reg(&name);
4184 (void)may_get_selection(name);
4185#endif
4186
4187 if (i == -1)
4188 {
4189 if (y_previous != NULL)
4190 yb = y_previous;
4191 else
4192 yb = &(y_regs[0]);
4193 }
4194 else
4195 yb = &(y_regs[i]);
Bram Moolenaarcde547a2009-11-17 11:43:06 +00004196
4197#ifdef FEAT_EVAL
4198 if (name == MB_TOLOWER(redir_reg)
4199 || (redir_reg == '"' && yb == y_previous))
4200 continue; /* do not list register being written to, the
4201 * pointer can be freed */
4202#endif
4203
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (yb->y_array != NULL)
4205 {
4206 msg_putchar('\n');
4207 msg_putchar('"');
4208 msg_putchar(name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004209 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 n = (int)Columns - 6;
4212 for (j = 0; j < yb->y_size && n > 1; ++j)
4213 {
4214 if (j)
4215 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004216 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 n -= 2;
4218 }
4219 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
4220 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004221 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004222 msg_outtrans_len(p, clen);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004223 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 }
4226 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004227 msg_puts_attr("^J", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 out_flush(); /* show one line at a time */
4229 }
4230 ui_breakcheck();
4231 }
4232
4233 /*
4234 * display last inserted text
4235 */
4236 if ((p = get_last_insert()) != NULL
4237 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
4238 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004239 msg_puts("\n\". ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 dis_msg(p, TRUE);
4241 }
4242
4243 /*
4244 * display last command line
4245 */
4246 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
4247 && !got_int)
4248 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004249 msg_puts("\n\": ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 dis_msg(last_cmdline, FALSE);
4251 }
4252
4253 /*
4254 * display current file name
4255 */
4256 if (curbuf->b_fname != NULL
4257 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4258 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004259 msg_puts("\n\"% ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 dis_msg(curbuf->b_fname, FALSE);
4261 }
4262
4263 /*
4264 * display alternate file name
4265 */
4266 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4267 {
4268 char_u *fname;
4269 linenr_T dummy;
4270
4271 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4272 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004273 msg_puts("\n\"# ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 dis_msg(fname, FALSE);
4275 }
4276 }
4277
4278 /*
4279 * display last search pattern
4280 */
4281 if (last_search_pat() != NULL
4282 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4283 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004284 msg_puts("\n\"/ ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 dis_msg(last_search_pat(), FALSE);
4286 }
4287
4288#ifdef FEAT_EVAL
4289 /*
4290 * display last used expression
4291 */
4292 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4293 && !got_int)
4294 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004295 msg_puts("\n\"= ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 dis_msg(expr_line, FALSE);
4297 }
4298#endif
4299}
4300
4301/*
4302 * display a string for do_dis()
4303 * truncate at end of screen line
4304 */
4305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004306dis_msg(
4307 char_u *p,
4308 int skip_esc) /* if TRUE, ignore trailing ESC */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309{
4310 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 n = (int)Columns - 6;
4314 while (*p != NUL
4315 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4316 && (n -= ptr2cells(p)) >= 0)
4317 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004318 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 msg_outtrans_len(p, l);
4321 p += l;
4322 }
4323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 msg_outtrans_len(p++, 1);
4325 }
4326 ui_breakcheck();
4327}
4328
Bram Moolenaar81340392012-06-06 16:12:59 +02004329#if defined(FEAT_COMMENTS) || defined(PROTO)
4330/*
4331 * If "process" is TRUE and the line begins with a comment leader (possibly
4332 * after some white space), return a pointer to the text after it. Put a boolean
4333 * value indicating whether the line ends with an unclosed comment in
4334 * "is_comment".
4335 * line - line to be processed,
4336 * process - if FALSE, will only check whether the line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004337 * comment,
Bram Moolenaar81340392012-06-06 16:12:59 +02004338 * include_space - whether to also skip space following the comment leader,
4339 * is_comment - will indicate whether the current line ends with an unclosed
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004340 * comment.
Bram Moolenaar81340392012-06-06 16:12:59 +02004341 */
Bram Moolenaar025a6b72017-03-12 20:37:21 +01004342 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004343skip_comment(
4344 char_u *line,
4345 int process,
4346 int include_space,
4347 int *is_comment)
Bram Moolenaar81340392012-06-06 16:12:59 +02004348{
4349 char_u *comment_flags = NULL;
4350 int lead_len;
4351 int leader_offset = get_last_leader_offset(line, &comment_flags);
4352
4353 *is_comment = FALSE;
4354 if (leader_offset != -1)
4355 {
4356 /* Let's check whether the line ends with an unclosed comment.
4357 * If the last comment leader has COM_END in flags, there's no comment.
4358 */
4359 while (*comment_flags)
4360 {
4361 if (*comment_flags == COM_END
4362 || *comment_flags == ':')
4363 break;
4364 ++comment_flags;
4365 }
4366 if (*comment_flags != COM_END)
4367 *is_comment = TRUE;
4368 }
4369
4370 if (process == FALSE)
4371 return line;
4372
4373 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
4374
4375 if (lead_len == 0)
4376 return line;
4377
4378 /* Find:
Bram Moolenaar81340392012-06-06 16:12:59 +02004379 * - COM_END,
4380 * - colon,
4381 * whichever comes first.
4382 */
4383 while (*comment_flags)
4384 {
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004385 if (*comment_flags == COM_END
Bram Moolenaar81340392012-06-06 16:12:59 +02004386 || *comment_flags == ':')
Bram Moolenaar81340392012-06-06 16:12:59 +02004387 break;
Bram Moolenaar81340392012-06-06 16:12:59 +02004388 ++comment_flags;
4389 }
4390
4391 /* If we found a colon, it means that we are not processing a line
Bram Moolenaare04a48f2012-06-13 14:01:41 +02004392 * starting with a closing part of a three-part comment. That's good,
4393 * because we don't want to remove those as this would be annoying.
Bram Moolenaar81340392012-06-06 16:12:59 +02004394 */
4395 if (*comment_flags == ':' || *comment_flags == NUL)
4396 line += lead_len;
4397
4398 return line;
4399}
4400#endif
4401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402/*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004403 * Join 'count' lines (minimal 2) at cursor position.
4404 * When "save_undo" is TRUE save lines for undo first.
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004405 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
4406 * leaders should not be removed.
4407 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
4408 * to set those marks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004410 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 */
4412 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004413do_join(
4414 long count,
4415 int insert_space,
4416 int save_undo,
4417 int use_formatoptions UNUSED,
4418 int setmark)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004420 char_u *curr = NULL;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004421 char_u *curr_start = NULL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004422 char_u *cend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 char_u *newp;
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004424 char_u *spaces; /* number of spaces inserted before a line */
Bram Moolenaard43848c2010-07-14 14:28:26 +02004425 int endcurr1 = NUL;
4426 int endcurr2 = NUL;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004427 int currsize = 0; /* size of the current line */
4428 int sumsize = 0; /* size of the long new line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 linenr_T t;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004430 colnr_T col = 0;
4431 int ret = OK;
Bram Moolenaar81340392012-06-06 16:12:59 +02004432#if defined(FEAT_COMMENTS) || defined(PROTO)
Bram Moolenaar802053f2012-06-06 23:08:38 +02004433 int *comments = NULL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004434 int remove_comments = (use_formatoptions == TRUE)
4435 && has_format_option(FO_REMOVE_COMS);
4436 int prev_was_comment;
4437#endif
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004438#ifdef FEAT_TEXT_PROP
4439 textprop_T **prop_lines = NULL;
4440 int *prop_lengths = NULL;
4441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004443 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4444 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004447 /* Allocate an array to store the number of spaces inserted before each
4448 * line. We will use it to pre-compute the length of the new line and the
4449 * proper placement of each original line in the new one. */
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004450 spaces = lalloc_clear(count, TRUE);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004451 if (spaces == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 return FAIL;
Bram Moolenaar81340392012-06-06 16:12:59 +02004453#if defined(FEAT_COMMENTS) || defined(PROTO)
4454 if (remove_comments)
4455 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004456 comments = lalloc_clear(count * sizeof(int), TRUE);
Bram Moolenaar81340392012-06-06 16:12:59 +02004457 if (comments == NULL)
4458 {
4459 vim_free(spaces);
4460 return FAIL;
4461 }
4462 }
4463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
4465 /*
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004466 * Don't move anything yet, just compute the final line length
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004467 * and setup the array of space strings lengths
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004468 * This loops forward over the joined lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004470 for (t = 0; t < count; ++t)
4471 {
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004472 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004473 if (t == 0 && setmark)
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004474 {
4475 /* Set the '[ mark. */
4476 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
4477 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
4478 }
Bram Moolenaar81340392012-06-06 16:12:59 +02004479#if defined(FEAT_COMMENTS) || defined(PROTO)
4480 if (remove_comments)
4481 {
4482 /* We don't want to remove the comment leader if the
4483 * previous line is not a comment. */
4484 if (t > 0 && prev_was_comment)
4485 {
4486
4487 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
4488 &prev_was_comment);
Bram Moolenaar27ba0882012-06-07 21:09:39 +02004489 comments[t] = (int)(new_curr - curr);
Bram Moolenaar81340392012-06-06 16:12:59 +02004490 curr = new_curr;
4491 }
4492 else
4493 curr = skip_comment(curr, FALSE, insert_space,
4494 &prev_was_comment);
4495 }
4496#endif
4497
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004498 if (insert_space && t > 0)
4499 {
4500 curr = skipwhite(curr);
4501 if (*curr != ')' && currsize != 0 && endcurr1 != TAB
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004502 && (!has_format_option(FO_MBYTE_JOIN)
4503 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
4504 && (!has_format_option(FO_MBYTE_JOIN2)
4505 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004506 )
4507 {
4508 /* don't add a space if the line is ending in a space */
4509 if (endcurr1 == ' ')
4510 endcurr1 = endcurr2;
4511 else
4512 ++spaces[t];
4513 /* extra space when 'joinspaces' set and line ends in '.' */
4514 if ( p_js
4515 && (endcurr1 == '.'
4516 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4517 && (endcurr1 == '?' || endcurr1 == '!'))))
4518 ++spaces[t];
4519 }
4520 }
4521 currsize = (int)STRLEN(curr);
4522 sumsize += currsize + spaces[t];
4523 endcurr1 = endcurr2 = NUL;
4524 if (insert_space && currsize > 0)
4525 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004526 if (has_mbyte)
4527 {
4528 cend = curr + currsize;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004529 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004530 endcurr1 = (*mb_ptr2char)(cend);
4531 if (cend > curr)
4532 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004533 MB_PTR_BACK(curr, cend);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004534 endcurr2 = (*mb_ptr2char)(cend);
4535 }
4536 }
4537 else
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004538 {
4539 endcurr1 = *(curr + currsize - 1);
4540 if (currsize > 1)
4541 endcurr2 = *(curr + currsize - 2);
4542 }
4543 }
4544 line_breakcheck();
4545 if (got_int)
4546 {
4547 ret = FAIL;
4548 goto theend;
4549 }
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004552 /* store the column position before last line */
4553 col = sumsize - currsize - spaces[count - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004555 /* allocate the space for the new line */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004556 newp = alloc(sumsize + 1);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02004557 if (newp == NULL)
4558 {
4559 ret = FAIL;
4560 goto theend;
4561 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004562 cend = newp + sumsize;
4563 *cend = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004565#ifdef FEAT_TEXT_PROP
4566 // We need to move properties of the lines that are going to be deleted to
4567 // the new long one.
4568 if (curbuf->b_has_textprop && !text_prop_frozen)
4569 {
4570 // Allocate an array to copy the text properties of joined lines into.
4571 // And another array to store the number of properties in each line.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004572 prop_lines = ALLOC_CLEAR_MULT(textprop_T *, count - 1);
4573 prop_lengths = ALLOC_CLEAR_MULT(int, count - 1);
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004574 if (prop_lengths == NULL)
4575 VIM_CLEAR(prop_lines);
4576 }
4577#endif
4578
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004579 /*
4580 * Move affected lines to the new long one.
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004581 * This loops backwards over the joined lines, including the original line.
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004582 *
4583 * Move marks from each deleted line to the joined line, adjusting the
4584 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4585 * should not really be a problem.
4586 */
4587 for (t = count - 1; ; --t)
4588 {
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004589 int spaces_removed;
4590
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004591 cend -= currsize;
4592 mch_memmove(cend, curr, (size_t)currsize);
4593 if (spaces[t] > 0)
4594 {
4595 cend -= spaces[t];
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02004596 vim_memset(cend, ' ', (size_t)(spaces[t]));
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004597 }
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004598
4599 // If deleting more spaces than adding, the cursor moves no more than
4600 // what is added if it is inside these spaces.
4601 spaces_removed = (curr - curr_start) - spaces[t];
4602
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004603 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01004604 (long)(cend - newp - spaces_removed), spaces_removed);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004605 if (t == 0)
4606 break;
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004607#ifdef FEAT_TEXT_PROP
4608 if (prop_lines != NULL)
4609 adjust_props_for_join(curwin->w_cursor.lnum + t,
4610 prop_lines + t - 1, prop_lengths + t - 1,
4611 (long)(cend - newp - spaces_removed), spaces_removed);
4612#endif
4613
Bram Moolenaar341ad7a2010-10-09 17:23:31 +02004614 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004615#if defined(FEAT_COMMENTS)
Bram Moolenaar81340392012-06-06 16:12:59 +02004616 if (remove_comments)
4617 curr += comments[t - 1];
4618#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004619 if (insert_space && t > 1)
4620 curr = skipwhite(curr);
4621 currsize = (int)STRLEN(curr);
4622 }
Bram Moolenaar80e737c2019-05-17 19:56:34 +02004623
4624#ifdef FEAT_TEXT_PROP
4625 if (prop_lines != NULL)
4626 join_prop_lines(curwin->w_cursor.lnum, newp,
4627 prop_lines, prop_lengths, count);
4628 else
4629#endif
4630 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004632 if (setmark)
4633 {
4634 /* Set the '] mark. */
4635 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
Bram Moolenaar787880a2019-05-17 20:17:40 +02004636 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02004637 }
Bram Moolenaarf92d8a22014-02-11 19:33:07 +01004638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 /* Only report the change in the first line here, del_lines() will report
4640 * the deleted line. */
4641 changed_lines(curwin->w_cursor.lnum, currsize,
4642 curwin->w_cursor.lnum + 1, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004644 * Delete following lines. To do this we move the cursor there
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 * briefly, and then move it back. After del_lines() the cursor may
4646 * have moved up (last line deleted), so the current lnum is kept in t.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 */
4648 t = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 ++curwin->w_cursor.lnum;
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004650 del_lines(count - 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 curwin->w_cursor.lnum = t;
4652
4653 /*
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004654 * Set the cursor column:
4655 * Vi compatible: use the column of the first join
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004656 * vim: use the column of the last join
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004658 curwin->w_cursor.col =
4659 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 check_cursor_col();
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004661
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 curwin->w_set_curswant = TRUE;
4664
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004665theend:
4666 vim_free(spaces);
Bram Moolenaar81340392012-06-06 16:12:59 +02004667#if defined(FEAT_COMMENTS) || defined(PROTO)
4668 if (remove_comments)
4669 vim_free(comments);
4670#endif
Bram Moolenaar893eaab2010-07-10 17:51:46 +02004671 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672}
4673
4674#ifdef FEAT_COMMENTS
4675/*
4676 * Return TRUE if the two comment leaders given are the same. "lnum" is
4677 * the first line. White-space is ignored. Note that the whole of
4678 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4679 */
4680 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004681same_leader(
4682 linenr_T lnum,
4683 int leader1_len,
4684 char_u *leader1_flags,
4685 int leader2_len,
4686 char_u *leader2_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 int idx1 = 0, idx2 = 0;
4689 char_u *p;
4690 char_u *line1;
4691 char_u *line2;
4692
4693 if (leader1_len == 0)
4694 return (leader2_len == 0);
4695
4696 /*
4697 * If first leader has 'f' flag, the lines can be joined only if the
4698 * second line does not have a leader.
4699 * If first leader has 'e' flag, the lines can never be joined.
4700 * If fist leader has 's' flag, the lines can only be joined if there is
4701 * some text after it and the second line has the 'm' flag.
4702 */
4703 if (leader1_flags != NULL)
4704 {
4705 for (p = leader1_flags; *p && *p != ':'; ++p)
4706 {
4707 if (*p == COM_FIRST)
4708 return (leader2_len == 0);
4709 if (*p == COM_END)
4710 return FALSE;
4711 if (*p == COM_START)
4712 {
4713 if (*(ml_get(lnum) + leader1_len) == NUL)
4714 return FALSE;
4715 if (leader2_flags == NULL || leader2_len == 0)
4716 return FALSE;
4717 for (p = leader2_flags; *p && *p != ':'; ++p)
4718 if (*p == COM_MIDDLE)
4719 return TRUE;
4720 return FALSE;
4721 }
4722 }
4723 }
4724
4725 /*
4726 * Get current line and next line, compare the leaders.
4727 * The first line has to be saved, only one line can be locked at a time.
4728 */
4729 line1 = vim_strsave(ml_get(lnum));
4730 if (line1 != NULL)
4731 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004732 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 ;
4734 line2 = ml_get(lnum + 1);
4735 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4736 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004737 if (!VIM_ISWHITE(line2[idx2]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
4739 if (line1[idx1++] != line2[idx2])
4740 break;
4741 }
4742 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004743 while (VIM_ISWHITE(line1[idx1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 ++idx1;
4745 }
4746 vim_free(line1);
4747 }
4748 return (idx2 == leader2_len && idx1 == leader1_len);
4749}
4750#endif
4751
4752/*
Bram Moolenaar66accae2012-01-10 13:44:27 +01004753 * Implementation of the format operator 'gq'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 */
4755 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004756op_format(
4757 oparg_T *oap,
4758 int keep_cursor) /* keep cursor on same text char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 long old_line_count = curbuf->b_ml.ml_line_count;
4761
4762 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4763 * can put it back there. */
4764 curwin->w_cursor = oap->cursor_start;
4765
4766 if (u_save((linenr_T)(oap->start.lnum - 1),
4767 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4768 return;
4769 curwin->w_cursor = oap->start;
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (oap->is_VIsual)
4772 /* When there is no change: need to remove the Visual selection */
4773 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
4775 /* Set '[ mark at the start of the formatted area */
4776 curbuf->b_op_start = oap->start;
4777
4778 /* For "gw" remember the cursor position and put it back below (adjusted
4779 * for joined and split lines). */
4780 if (keep_cursor)
4781 saved_cursor = oap->cursor_start;
4782
Bram Moolenaar81a82092008-03-12 16:27:00 +00004783 format_lines(oap->line_count, keep_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785 /*
4786 * Leave the cursor at the first non-blank of the last formatted line.
4787 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4788 * line, so "." will do the next lines.
4789 */
4790 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4791 ++curwin->w_cursor.lnum;
4792 beginline(BL_WHITE | BL_FIX);
4793 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4794 msgmore(old_line_count);
4795
4796 /* put '] mark on the end of the formatted area */
4797 curbuf->b_op_end = curwin->w_cursor;
4798
4799 if (keep_cursor)
4800 {
4801 curwin->w_cursor = saved_cursor;
4802 saved_cursor.lnum = 0;
4803 }
4804
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (oap->is_VIsual)
4806 {
4807 win_T *wp;
4808
4809 FOR_ALL_WINDOWS(wp)
4810 {
4811 if (wp->w_old_cursor_lnum != 0)
4812 {
4813 /* When lines have been inserted or deleted, adjust the end of
4814 * the Visual area to be redrawn. */
4815 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4816 wp->w_old_cursor_lnum += old_line_count;
4817 else
4818 wp->w_old_visual_lnum += old_line_count;
4819 }
4820 }
4821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822}
4823
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004824#if defined(FEAT_EVAL) || defined(PROTO)
4825/*
4826 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4827 */
4828 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004829op_formatexpr(oparg_T *oap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004830{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004831 if (oap->is_VIsual)
4832 /* When there is no change: need to remove the Visual selection */
4833 redraw_curbuf_later(INVERTED);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004834
Bram Moolenaar700303e2010-07-11 17:35:50 +02004835 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0)
4836 /* As documented: when 'formatexpr' returns non-zero fall back to
4837 * internal formatting. */
4838 op_format(oap, FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004839}
4840
4841 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004842fex_format(
4843 linenr_T lnum,
4844 long count,
4845 int c) /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004846{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004847 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4848 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004849 int r;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004850 char_u *fex;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004851
4852 /*
4853 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004854 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004855 */
4856 set_vim_var_nr(VV_LNUM, lnum);
4857 set_vim_var_nr(VV_COUNT, count);
Bram Moolenaarda9591e2009-09-30 13:17:02 +00004858 set_vim_var_char(c);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004859
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004860 /* Make a copy, the option could be changed while calling it. */
4861 fex = vim_strsave(curbuf->b_p_fex);
4862 if (fex == NULL)
4863 return 0;
4864
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004865 /*
4866 * Evaluate the function.
4867 */
4868 if (use_sandbox)
4869 ++sandbox;
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004870 r = (int)eval_to_number(fex);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004871 if (use_sandbox)
4872 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004873
4874 set_vim_var_string(VV_CHAR, NULL, -1);
Bram Moolenaard77f9d52016-09-04 15:13:39 +02004875 vim_free(fex);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004876
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004877 return r;
4878}
4879#endif
4880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881/*
4882 * Format "line_count" lines, starting at the cursor position.
4883 * When "line_count" is negative, format until the end of the paragraph.
4884 * Lines after the cursor line are saved for undo, caller must have saved the
4885 * first line.
4886 */
4887 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004888format_lines(
4889 linenr_T line_count,
4890 int avoid_fex) /* don't use 'formatexpr' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 int max_len;
4893 int is_not_par; /* current line not part of parag. */
4894 int next_is_not_par; /* next line not part of paragraph */
4895 int is_end_par; /* at end of paragraph */
4896 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4897 int next_is_start_par = FALSE;
4898#ifdef FEAT_COMMENTS
4899 int leader_len = 0; /* leader len of current line */
4900 int next_leader_len; /* leader len of next line */
4901 char_u *leader_flags = NULL; /* flags for leader of current line */
4902 char_u *next_leader_flags; /* flags for leader of next line */
4903 int do_comments; /* format comments */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004904 int do_comments_list = 0; /* format comments with 'n' or '2' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905#endif
4906 int advance = TRUE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02004907 int second_indent = -1; /* indent for second line (comment
4908 * aware) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 int do_second_indent;
4910 int do_number_indent;
4911 int do_trail_white;
4912 int first_par_line = TRUE;
4913 int smd_save;
4914 long count;
4915 int need_set_indent = TRUE; /* set indent of next paragraph */
4916 int force_format = FALSE;
4917 int old_State = State;
4918
4919 /* length of a line to force formatting: 3 * 'tw' */
4920 max_len = comp_textwidth(TRUE) * 3;
4921
4922 /* check for 'q', '2' and '1' in 'formatoptions' */
4923#ifdef FEAT_COMMENTS
4924 do_comments = has_format_option(FO_Q_COMS);
4925#endif
4926 do_second_indent = has_format_option(FO_Q_SECOND);
4927 do_number_indent = has_format_option(FO_Q_NUMBER);
4928 do_trail_white = has_format_option(FO_WHITE_PAR);
4929
4930 /*
4931 * Get info about the previous and current line.
4932 */
4933 if (curwin->w_cursor.lnum > 1)
4934 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4935#ifdef FEAT_COMMENTS
4936 , &leader_len, &leader_flags, do_comments
4937#endif
4938 );
4939 else
4940 is_not_par = TRUE;
4941 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4942#ifdef FEAT_COMMENTS
4943 , &next_leader_len, &next_leader_flags, do_comments
4944#endif
4945 );
4946 is_end_par = (is_not_par || next_is_not_par);
4947 if (!is_end_par && do_trail_white)
4948 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4949
4950 curwin->w_cursor.lnum--;
4951 for (count = line_count; count != 0 && !got_int; --count)
4952 {
4953 /*
4954 * Advance to next paragraph.
4955 */
4956 if (advance)
4957 {
4958 curwin->w_cursor.lnum++;
4959 prev_is_end_par = is_end_par;
4960 is_not_par = next_is_not_par;
4961#ifdef FEAT_COMMENTS
4962 leader_len = next_leader_len;
4963 leader_flags = next_leader_flags;
4964#endif
4965 }
4966
4967 /*
4968 * The last line to be formatted.
4969 */
4970 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4971 {
4972 next_is_not_par = TRUE;
4973#ifdef FEAT_COMMENTS
4974 next_leader_len = 0;
4975 next_leader_flags = NULL;
4976#endif
4977 }
4978 else
4979 {
4980 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4981#ifdef FEAT_COMMENTS
4982 , &next_leader_len, &next_leader_flags, do_comments
4983#endif
4984 );
4985 if (do_number_indent)
4986 next_is_start_par =
4987 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4988 }
4989 advance = TRUE;
4990 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4991 if (!is_end_par && do_trail_white)
4992 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4993
4994 /*
4995 * Skip lines that are not in a paragraph.
4996 */
4997 if (is_not_par)
4998 {
4999 if (line_count < 0)
5000 break;
5001 }
5002 else
5003 {
5004 /*
5005 * For the first line of a paragraph, check indent of second line.
5006 * Don't do this for comments and empty lines.
5007 */
5008 if (first_par_line
5009 && (do_second_indent || do_number_indent)
5010 && prev_is_end_par
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005011 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005013 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005014 {
5015#ifdef FEAT_COMMENTS
5016 if (leader_len == 0 && next_leader_len == 0)
5017 {
5018 /* no comment found */
5019#endif
5020 second_indent =
5021 get_indent_lnum(curwin->w_cursor.lnum + 1);
5022#ifdef FEAT_COMMENTS
5023 }
5024 else
5025 {
5026 second_indent = next_leader_len;
5027 do_comments_list = 1;
5028 }
5029#endif
5030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 else if (do_number_indent)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005032 {
5033#ifdef FEAT_COMMENTS
5034 if (leader_len == 0 && next_leader_len == 0)
5035 {
5036 /* no comment found */
5037#endif
5038 second_indent =
5039 get_number_indent(curwin->w_cursor.lnum);
5040#ifdef FEAT_COMMENTS
5041 }
5042 else
5043 {
5044 /* get_number_indent() is now "comment aware"... */
5045 second_indent =
5046 get_number_indent(curwin->w_cursor.lnum);
5047 do_comments_list = 1;
5048 }
5049#endif
5050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052
5053 /*
5054 * When the comment leader changes, it's the end of the paragraph.
5055 */
5056 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
5057#ifdef FEAT_COMMENTS
5058 || !same_leader(curwin->w_cursor.lnum,
5059 leader_len, leader_flags,
5060 next_leader_len, next_leader_flags)
5061#endif
5062 )
5063 is_end_par = TRUE;
5064
5065 /*
5066 * If we have got to the end of a paragraph, or the line is
5067 * getting long, format it.
5068 */
5069 if (is_end_par || force_format)
5070 {
5071 if (need_set_indent)
5072 /* replace indent in first line with minimal number of
5073 * tabs and spaces, according to current options */
5074 (void)set_indent(get_indent(), SIN_CHANGED);
5075
5076 /* put cursor on last non-space */
5077 State = NORMAL; /* don't go past end-of-line */
5078 coladvance((colnr_T)MAXCOL);
5079 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
5080 dec_cursor();
5081
5082 /* do the formatting, without 'showmode' */
5083 State = INSERT; /* for open_line() */
5084 smd_save = p_smd;
5085 p_smd = FALSE;
5086 insertchar(NUL, INSCHAR_FORMAT
5087#ifdef FEAT_COMMENTS
5088 + (do_comments ? INSCHAR_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005089 + (do_comments && do_comments_list
5090 ? INSCHAR_COM_LIST : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#endif
Bram Moolenaar81a82092008-03-12 16:27:00 +00005092 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 State = old_State;
5094 p_smd = smd_save;
5095 second_indent = -1;
5096 /* at end of par.: need to set indent of next par. */
5097 need_set_indent = is_end_par;
5098 if (is_end_par)
5099 {
5100 /* When called with a negative line count, break at the
5101 * end of the paragraph. */
5102 if (line_count < 0)
5103 break;
5104 first_par_line = TRUE;
5105 }
5106 force_format = FALSE;
5107 }
5108
5109 /*
5110 * When still in same paragraph, join the lines together. But
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005111 * first delete the leader from the second line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 */
5113 if (!is_end_par)
5114 {
5115 advance = FALSE;
5116 curwin->w_cursor.lnum++;
5117 curwin->w_cursor.col = 0;
5118 if (line_count < 0 && u_save_cursor() == FAIL)
Bram Moolenaar893eaab2010-07-10 17:51:46 +02005119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (next_leader_len > 0)
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005122 {
5123 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005125 (long)-next_leader_len, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005126 } else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127#endif
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005128 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
5129 {
Bram Moolenaare2e69e42017-09-02 20:30:35 +02005130 int indent = getwhitecols_curline();
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005131
5132 if (indent > 0)
5133 {
5134 (void)del_bytes(indent, FALSE, FALSE);
5135 mark_col_adjust(curwin->w_cursor.lnum,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01005136 (colnr_T)0, 0L, (long)-indent, 0);
Bram Moolenaar92c2db82013-11-02 23:59:35 +01005137 }
Bram Moolenaar2c019c82013-10-06 17:46:56 +02005138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 curwin->w_cursor.lnum--;
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02005140 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 beep_flush();
5143 break;
5144 }
5145 first_par_line = FALSE;
5146 /* If the line is getting long, format it next time */
5147 if (STRLEN(ml_get_curline()) > (size_t)max_len)
5148 force_format = TRUE;
5149 else
5150 force_format = FALSE;
5151 }
5152 }
5153 line_breakcheck();
5154 }
5155}
5156
5157/*
5158 * Return TRUE if line "lnum" ends in a white character.
5159 */
5160 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005161ends_in_white(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162{
5163 char_u *s = ml_get(lnum);
5164 size_t l;
5165
5166 if (*s == NUL)
5167 return FALSE;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005168 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 * invocation may call function multiple times". */
5170 l = STRLEN(s) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005171 return VIM_ISWHITE(s[l]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172}
5173
5174/*
5175 * Blank lines, and lines containing only the comment leader, are left
5176 * untouched by the formatting. The function returns TRUE in this
5177 * case. It also returns TRUE when a line starts with the end of a comment
5178 * ('e' in comment flags), so that this line is skipped, and not joined to the
5179 * previous line. A new paragraph starts after a blank line, or when the
5180 * comment leader changes -- webb.
5181 */
5182#ifdef FEAT_COMMENTS
5183 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005184fmt_check_par(
5185 linenr_T lnum,
5186 int *leader_len,
5187 char_u **leader_flags,
5188 int do_comments)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 char_u *flags = NULL; /* init for GCC */
5191 char_u *ptr;
5192
5193 ptr = ml_get(lnum);
5194 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02005195 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 else
5197 *leader_len = 0;
5198
5199 if (*leader_len > 0)
5200 {
5201 /*
5202 * Search for 'e' flag in comment leader flags.
5203 */
5204 flags = *leader_flags;
5205 while (*flags && *flags != ':' && *flags != COM_END)
5206 ++flags;
5207 }
5208
5209 return (*skipwhite(ptr + *leader_len) == NUL
5210 || (*leader_len > 0 && *flags == COM_END)
5211 || startPS(lnum, NUL, FALSE));
5212}
5213#else
5214 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005215fmt_check_par(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
5218}
5219#endif
5220
5221/*
5222 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
5223 * previous line is in the same paragraph. Used for auto-formatting.
5224 */
5225 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005226paragraph_start(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
5228 char_u *p;
5229#ifdef FEAT_COMMENTS
5230 int leader_len = 0; /* leader len of current line */
5231 char_u *leader_flags = NULL; /* flags for leader of current line */
5232 int next_leader_len; /* leader len of next line */
5233 char_u *next_leader_flags; /* flags for leader of next line */
5234 int do_comments; /* format comments */
5235#endif
5236
5237 if (lnum <= 1)
5238 return TRUE; /* start of the file */
5239
5240 p = ml_get(lnum - 1);
5241 if (*p == NUL)
5242 return TRUE; /* after empty line */
5243
5244#ifdef FEAT_COMMENTS
5245 do_comments = has_format_option(FO_Q_COMS);
5246#endif
5247 if (fmt_check_par(lnum - 1
5248#ifdef FEAT_COMMENTS
5249 , &leader_len, &leader_flags, do_comments
5250#endif
5251 ))
5252 return TRUE; /* after non-paragraph line */
5253
5254 if (fmt_check_par(lnum
5255#ifdef FEAT_COMMENTS
5256 , &next_leader_len, &next_leader_flags, do_comments
5257#endif
5258 ))
5259 return TRUE; /* "lnum" is not a paragraph line */
5260
5261 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
5262 return TRUE; /* missing trailing space in previous line. */
5263
5264 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
5265 return TRUE; /* numbered item starts in "lnum". */
5266
5267#ifdef FEAT_COMMENTS
5268 if (!same_leader(lnum - 1, leader_len, leader_flags,
5269 next_leader_len, next_leader_flags))
5270 return TRUE; /* change of comment leader. */
5271#endif
5272
5273 return FALSE;
5274}
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276/*
5277 * prepare a few things for block mode yank/delete/tilde
5278 *
5279 * for delete:
5280 * - textlen includes the first/last char to be (partly) deleted
5281 * - start/endspaces is the number of columns that are taken by the
5282 * first/last deleted char minus the number of columns that have to be
Bram Moolenaar9d77dcc2009-03-11 15:28:26 +00005283 * deleted.
5284 * for yank and tilde:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 * - textlen includes the first/last char to be wholly yanked
5286 * - start/endspaces is the number of columns of the first/last yanked char
5287 * that are to be yanked.
5288 */
5289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005290block_prep(
5291 oparg_T *oap,
5292 struct block_def *bdp,
5293 linenr_T lnum,
5294 int is_del)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 int incr = 0;
5297 char_u *pend;
5298 char_u *pstart;
5299 char_u *line;
5300 char_u *prev_pstart;
5301 char_u *prev_pend;
5302
5303 bdp->startspaces = 0;
5304 bdp->endspaces = 0;
5305 bdp->textlen = 0;
5306 bdp->start_vcol = 0;
5307 bdp->end_vcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 bdp->is_short = FALSE;
5309 bdp->is_oneChar = FALSE;
5310 bdp->pre_whitesp = 0;
5311 bdp->pre_whitesp_c = 0;
5312 bdp->end_char_vcols = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 bdp->start_char_vcols = 0;
5314
5315 line = ml_get(lnum);
5316 pstart = line;
5317 prev_pstart = line;
5318 while (bdp->start_vcol < oap->start_vcol && *pstart)
5319 {
5320 /* Count a tab for what it's worth (if list mode not on) */
Bram Moolenaar597a4222014-06-25 14:39:50 +02005321 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 bdp->start_vcol += incr;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005323 if (VIM_ISWHITE(*pstart))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
5325 bdp->pre_whitesp += incr;
5326 bdp->pre_whitesp_c++;
5327 }
5328 else
5329 {
5330 bdp->pre_whitesp = 0;
5331 bdp->pre_whitesp_c = 0;
5332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 prev_pstart = pstart;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005334 MB_PTR_ADV(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336 bdp->start_char_vcols = incr;
5337 if (bdp->start_vcol < oap->start_vcol) /* line too short */
5338 {
5339 bdp->end_vcol = bdp->start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (!is_del || oap->op_type == OP_APPEND)
5342 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
5343 }
5344 else
5345 {
5346 /* notice: this converts partly selected Multibyte characters to
5347 * spaces, too. */
5348 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
5349 if (is_del && bdp->startspaces)
5350 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
5351 pend = pstart;
5352 bdp->end_vcol = bdp->start_vcol;
5353 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
5354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 bdp->is_oneChar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (oap->op_type == OP_INSERT)
5357 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5358 else if (oap->op_type == OP_APPEND)
5359 {
5360 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
5361 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
5362 }
5363 else
5364 {
5365 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
5366 if (is_del && oap->op_type != OP_LSHIFT)
5367 {
5368 /* just putting the sum of those two into
5369 * bdp->startspaces doesn't work for Visual replace,
5370 * so we have to split the tab in two */
5371 bdp->startspaces = bdp->start_char_vcols
5372 - (bdp->start_vcol - oap->start_vcol);
5373 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5374 }
5375 }
5376 }
5377 else
5378 {
5379 prev_pend = pend;
5380 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
5381 {
5382 /* Count a tab for what it's worth (if list mode not on) */
5383 prev_pend = pend;
Bram Moolenaar1dc92332015-01-27 13:22:20 +01005384 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 bdp->end_vcol += incr;
5386 }
5387 if (bdp->end_vcol <= oap->end_vcol
5388 && (!is_del
5389 || oap->op_type == OP_APPEND
5390 || oap->op_type == OP_REPLACE)) /* line too short */
5391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 bdp->is_short = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 /* Alternative: include spaces to fill up the block.
5394 * Disadvantage: can lead to trailing spaces when the line is
5395 * short where the text is put */
5396 /* if (!is_del || oap->op_type == OP_APPEND) */
5397 if (oap->op_type == OP_APPEND || virtual_op)
5398 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00005399 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 else
5401 bdp->endspaces = 0; /* replace doesn't add characters */
5402 }
5403 else if (bdp->end_vcol > oap->end_vcol)
5404 {
5405 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
5406 if (!is_del && bdp->endspaces)
5407 {
5408 bdp->endspaces = incr - bdp->endspaces;
5409 if (pend != pstart)
5410 pend = prev_pend;
5411 }
5412 }
5413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 bdp->end_char_vcols = incr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (is_del && bdp->startspaces)
5416 pstart = prev_pstart;
5417 bdp->textlen = (int)(pend - pstart);
5418 }
5419 bdp->textcol = (colnr_T) (pstart - line);
5420 bdp->textstart = pstart;
5421}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423/*
Bram Moolenaard79e5502016-01-10 22:13:02 +01005424 * Handle the add/subtract operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005427op_addsub(
5428 oparg_T *oap,
5429 linenr_T Prenum1, /* Amount of add/subtract */
5430 int g_cmd) /* was g<c-a>/g<c-x> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
Bram Moolenaard79e5502016-01-10 22:13:02 +01005432 pos_T pos;
5433 struct block_def bd;
5434 int change_cnt = 0;
5435 linenr_T amount = Prenum1;
5436
Bram Moolenaar6b731882018-11-16 20:54:47 +01005437 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
Bram Moolenaarbdace832019-03-02 10:13:42 +01005438 // buffer is not completely updated yet. Postpone updating folds until before
Bram Moolenaar6b731882018-11-16 20:54:47 +01005439 // the call to changed_lines().
5440#ifdef FEAT_FOLDING
5441 disable_fold_update++;
5442#endif
5443
Bram Moolenaard79e5502016-01-10 22:13:02 +01005444 if (!VIsual_active)
5445 {
5446 pos = curwin->w_cursor;
5447 if (u_save_cursor() == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005448 {
5449#ifdef FEAT_FOLDING
5450 disable_fold_update--;
5451#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005452 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005453 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005454 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
Bram Moolenaar6b731882018-11-16 20:54:47 +01005455#ifdef FEAT_FOLDING
5456 disable_fold_update--;
5457#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005458 if (change_cnt)
5459 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
5460 }
5461 else
5462 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005463 int one_change;
5464 int length;
5465 pos_T startpos;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005466
5467 if (u_save((linenr_T)(oap->start.lnum - 1),
5468 (linenr_T)(oap->end.lnum + 1)) == FAIL)
Bram Moolenaar6b731882018-11-16 20:54:47 +01005469 {
5470#ifdef FEAT_FOLDING
5471 disable_fold_update--;
5472#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005473 return;
Bram Moolenaar6b731882018-11-16 20:54:47 +01005474 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005475
5476 pos = oap->start;
5477 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
5478 {
5479 if (oap->block_mode) /* Visual block mode */
5480 {
5481 block_prep(oap, &bd, pos.lnum, FALSE);
5482 pos.col = bd.textcol;
5483 length = bd.textlen;
5484 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005485 else if (oap->motion_type == MLINE)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005486 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005487 curwin->w_cursor.col = 0;
5488 pos.col = 0;
5489 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5490 }
5491 else /* oap->motion_type == MCHAR */
5492 {
Bram Moolenaar5fe6bdf2017-12-05 17:22:12 +01005493 if (pos.lnum == oap->start.lnum && !oap->inclusive)
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005494 dec(&(oap->end));
5495 length = (colnr_T)STRLEN(ml_get(pos.lnum));
5496 pos.col = 0;
5497 if (pos.lnum == oap->start.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005498 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005499 pos.col += oap->start.col;
5500 length -= oap->start.col;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005501 }
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005502 if (pos.lnum == oap->end.lnum)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005503 {
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005504 length = (int)STRLEN(ml_get(oap->end.lnum));
5505 if (oap->end.col >= length)
5506 oap->end.col = length - 1;
5507 length = oap->end.col - pos.col + 1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005508 }
5509 }
5510 one_change = do_addsub(oap->op_type, &pos, length, amount);
5511 if (one_change)
5512 {
5513 /* Remember the start position of the first change. */
5514 if (change_cnt == 0)
5515 startpos = curbuf->b_op_start;
5516 ++change_cnt;
5517 }
5518
5519#ifdef FEAT_NETBEANS_INTG
5520 if (netbeans_active() && one_change)
5521 {
5522 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
5523
5524 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
5525 netbeans_inserted(curbuf, pos.lnum, pos.col,
5526 &ptr[pos.col], length);
5527 }
5528#endif
5529 if (g_cmd && one_change)
5530 amount += Prenum1;
5531 }
Bram Moolenaar6b731882018-11-16 20:54:47 +01005532
5533#ifdef FEAT_FOLDING
5534 disable_fold_update--;
5535#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01005536 if (change_cnt)
5537 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
5538
5539 if (!change_cnt && oap->is_VIsual)
5540 /* No change: need to remove the Visual selection */
5541 redraw_curbuf_later(INVERTED);
5542
5543 /* Set '[ mark if something changed. Keep the last end
5544 * position from do_addsub(). */
5545 if (change_cnt > 0)
5546 curbuf->b_op_start = startpos;
5547
5548 if (change_cnt > p_report)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005549 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005550 change_cnt), change_cnt);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005551 }
5552}
5553
5554/*
5555 * Add or subtract 'Prenum1' from a number in a line
5556 * op_type is OP_NR_ADD or OP_NR_SUB
5557 *
5558 * Returns TRUE if some character was changed.
5559 */
5560 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005561do_addsub(
5562 int op_type,
5563 pos_T *pos,
5564 int length,
5565 linenr_T Prenum1)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005566{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 int col;
5568 char_u *buf1;
5569 char_u buf2[NUMBUFLEN];
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005570 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005572 uvarnumber_T n;
5573 uvarnumber_T oldn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 char_u *ptr;
5575 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 int todel;
5577 int dohex;
5578 int dooct;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005579 int dobin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 int doalp;
5581 int firstdigit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 int subtract;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005583 int negative = FALSE;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02005584 int was_positive = TRUE;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005585 int visual = VIsual_active;
Bram Moolenaar3ec32612015-07-12 15:02:38 +02005586 int did_change = FALSE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005587 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005588 int maxlen = 0;
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005589 pos_T startpos;
5590 pos_T endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
Bram Moolenaardac13472019-09-16 21:06:21 +02005592 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
5593 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
5594 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
5595 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaard79e5502016-01-10 22:13:02 +01005597 curwin->w_cursor = *pos;
5598 ptr = ml_get(pos->lnum);
5599 col = pos->col;
5600
5601 if (*ptr == NUL)
5602 goto theend;
5603
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 /*
5605 * First check if we are on a hexadecimal number, after the "0x".
5606 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005607 if (!VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005609 if (dobin)
5610 while (col > 0 && vim_isbdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005611 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005612 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005613 if (has_mbyte)
5614 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005615 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005616
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005617 if (dohex)
5618 while (col > 0 && vim_isxdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005619 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005620 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005621 if (has_mbyte)
5622 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005623 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005624
5625 if ( dobin
5626 && dohex
5627 && ! ((col > 0
5628 && (ptr[col] == 'X'
5629 || ptr[col] == 'x')
5630 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005631 && (!has_mbyte ||
5632 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005633 && vim_isxdigit(ptr[col + 1]))))
5634 {
5635
5636 /* In case of binary/hexadecimal pattern overlap match, rescan */
5637
Bram Moolenaard79e5502016-01-10 22:13:02 +01005638 col = pos->col;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005639
5640 while (col > 0 && vim_isdigit(ptr[col]))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005641 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005642 col--;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005643 if (has_mbyte)
5644 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005645 }
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005646 }
5647
5648 if (( dohex
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005649 && col > 0
5650 && (ptr[col] == 'X'
5651 || ptr[col] == 'x')
5652 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005653 && (!has_mbyte ||
5654 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005655 && vim_isxdigit(ptr[col + 1])) ||
5656 ( dobin
5657 && col > 0
5658 && (ptr[col] == 'B'
5659 || ptr[col] == 'b')
5660 && ptr[col - 1] == '0'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005661 && (!has_mbyte ||
5662 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005663 && vim_isbdigit(ptr[col + 1])))
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005664 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005665 /* Found hexadecimal or binary number, move to its start. */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005666 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005667 if (has_mbyte)
5668 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005669 }
5670 else
5671 {
5672 /*
5673 * Search forward and then backward to find the start of number.
5674 */
Bram Moolenaard79e5502016-01-10 22:13:02 +01005675 col = pos->col;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005676
5677 while (ptr[col] != NUL
5678 && !vim_isdigit(ptr[col])
5679 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005680 col += MB_PTR2LEN(ptr + col);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005681
5682 while (col > 0
5683 && vim_isdigit(ptr[col - 1])
5684 && !(doalp && ASCII_ISALPHA(ptr[col])))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005685 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005686 --col;
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005687 if (has_mbyte)
5688 col -= (*mb_head_off)(ptr, ptr + col);
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005689 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005690 }
5691 }
5692
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02005693 if (visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005694 {
5695 while (ptr[col] != NUL && length > 0
5696 && !vim_isdigit(ptr[col])
5697 && !(doalp && ASCII_ISALPHA(ptr[col])))
5698 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005699 int mb_len = MB_PTR2LEN(ptr + col);
5700
5701 col += mb_len;
5702 length -= mb_len;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005703 }
5704
5705 if (length == 0)
5706 goto theend;
5707
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005708 if (col > pos->col && ptr[col - 1] == '-'
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005709 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1)))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005710 {
5711 negative = TRUE;
5712 was_positive = FALSE;
5713 }
5714 }
5715
5716 /*
5717 * If a number was found, and saving for undo works, replace the number.
5718 */
5719 firstdigit = ptr[col];
5720 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5721 {
5722 beep_flush();
5723 goto theend;
5724 }
5725
5726 if (doalp && ASCII_ISALPHA(firstdigit))
5727 {
5728 /* decrement or increment alphabetic character */
5729 if (op_type == OP_NR_SUB)
5730 {
5731 if (CharOrd(firstdigit) < Prenum1)
5732 {
5733 if (isupper(firstdigit))
5734 firstdigit = 'A';
5735 else
5736 firstdigit = 'a';
5737 }
5738 else
5739#ifdef EBCDIC
5740 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5741#else
5742 firstdigit -= Prenum1;
5743#endif
5744 }
5745 else
5746 {
5747 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5748 {
5749 if (isupper(firstdigit))
5750 firstdigit = 'Z';
5751 else
5752 firstdigit = 'z';
5753 }
5754 else
5755#ifdef EBCDIC
5756 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5757#else
5758 firstdigit += Prenum1;
5759#endif
5760 }
5761 curwin->w_cursor.col = col;
5762 if (!did_change)
5763 startpos = curwin->w_cursor;
5764 did_change = TRUE;
5765 (void)del_char(FALSE);
5766 ins_char(firstdigit);
5767 endpos = curwin->w_cursor;
5768 curwin->w_cursor.col = col;
5769 }
5770 else
5771 {
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005772 if (col > 0 && ptr[col - 1] == '-'
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005773 && (!has_mbyte ||
5774 !(*mb_head_off)(ptr, ptr + col - 1))
Bram Moolenaarad5ca9b2016-06-20 21:26:08 +02005775 && !visual)
Bram Moolenaard79e5502016-01-10 22:13:02 +01005776 {
5777 /* negative number */
5778 --col;
5779 negative = TRUE;
5780 }
5781 /* get the number value (unsigned) */
5782 if (visual && VIsual_mode != 'V')
5783 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
5784 ? (int)STRLEN(ptr) - col
5785 : length);
5786
5787 vim_str2nr(ptr + col, &pre, &length,
5788 0 + (dobin ? STR2NR_BIN : 0)
5789 + (dooct ? STR2NR_OCT : 0)
5790 + (dohex ? STR2NR_HEX : 0),
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005791 NULL, &n, maxlen, FALSE);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005792
5793 /* ignore leading '-' for hex and octal and bin numbers */
5794 if (pre && negative)
5795 {
5796 ++col;
5797 --length;
5798 negative = FALSE;
5799 }
5800 /* add or subtract */
5801 subtract = FALSE;
5802 if (op_type == OP_NR_SUB)
5803 subtract ^= TRUE;
5804 if (negative)
5805 subtract ^= TRUE;
5806
5807 oldn = n;
5808 if (subtract)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005809 n -= (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005810 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005811 n += (uvarnumber_T)Prenum1;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005812 /* handle wraparound for decimal numbers */
5813 if (!pre)
5814 {
5815 if (subtract)
5816 {
5817 if (n > oldn)
5818 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005819 n = 1 + (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005820 negative ^= TRUE;
5821 }
5822 }
5823 else
5824 {
5825 /* add */
5826 if (n < oldn)
5827 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005828 n = (n ^ (uvarnumber_T)-1);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005829 negative ^= TRUE;
5830 }
5831 }
5832 if (n == 0)
5833 negative = FALSE;
5834 }
5835
5836 if (visual && !was_positive && !negative && col > 0)
5837 {
5838 /* need to remove the '-' */
5839 col--;
5840 length++;
5841 }
5842
5843 /*
5844 * Delete the old number.
5845 */
5846 curwin->w_cursor.col = col;
5847 if (!did_change)
5848 startpos = curwin->w_cursor;
5849 did_change = TRUE;
5850 todel = length;
5851 c = gchar_cursor();
5852 /*
5853 * Don't include the '-' in the length, only the length of the
5854 * part after it is kept the same.
5855 */
5856 if (c == '-')
5857 --length;
5858 while (todel-- > 0)
5859 {
5860 if (c < 0x100 && isalpha(c))
5861 {
5862 if (isupper(c))
5863 hexupper = TRUE;
5864 else
5865 hexupper = FALSE;
5866 }
5867 /* del_char() will mark line needing displaying */
5868 (void)del_char(FALSE);
5869 c = gchar_cursor();
5870 }
5871
5872 /*
5873 * Prepare the leading characters in buf1[].
5874 * When there are many leading zeros it could be very long.
5875 * Allocate a bit too much.
5876 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005877 buf1 = alloc(length + NUMBUFLEN);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005878 if (buf1 == NULL)
5879 goto theend;
5880 ptr = buf1;
Bram Moolenaardc633cf2016-04-23 14:33:19 +02005881 if (negative && (!visual || was_positive))
Bram Moolenaard79e5502016-01-10 22:13:02 +01005882 *ptr++ = '-';
Bram Moolenaard79e5502016-01-10 22:13:02 +01005883 if (pre)
5884 {
5885 *ptr++ = '0';
5886 --length;
5887 }
5888 if (pre == 'b' || pre == 'B' ||
5889 pre == 'x' || pre == 'X')
5890 {
5891 *ptr++ = pre;
5892 --length;
5893 }
5894
5895 /*
5896 * Put the number characters in buf2[].
5897 */
5898 if (pre == 'b' || pre == 'B')
5899 {
5900 int i;
5901 int bit = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005902 int bits = sizeof(uvarnumber_T) * 8;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005903
5904 /* leading zeros */
5905 for (bit = bits; bit > 0; bit--)
5906 if ((n >> (bit - 1)) & 0x1) break;
5907
5908 for (i = 0; bit > 0; bit--)
5909 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
5910
5911 buf2[i] = '\0';
5912 }
5913 else if (pre == 0)
Bram Moolenaarea391762018-04-08 13:07:22 +02005914 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005915 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005916 else if (pre == '0')
Bram Moolenaarea391762018-04-08 13:07:22 +02005917 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005918 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005919 else if (pre && hexupper)
Bram Moolenaarea391762018-04-08 13:07:22 +02005920 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005921 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005922 else
Bram Moolenaarea391762018-04-08 13:07:22 +02005923 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005924 (long_long_u_T)n);
Bram Moolenaard79e5502016-01-10 22:13:02 +01005925 length -= (int)STRLEN(buf2);
5926
5927 /*
5928 * Adjust number of zeros to the new number of digits, so the
5929 * total length of the number remains the same.
5930 * Don't do this when
5931 * the result may look like an octal number.
5932 */
5933 if (firstdigit == '0' && !(dooct && pre == 0))
5934 while (length-- > 0)
5935 *ptr++ = '0';
5936 *ptr = NUL;
5937 STRCAT(buf1, buf2);
5938 ins_str(buf1); /* insert the new number */
5939 vim_free(buf1);
5940 endpos = curwin->w_cursor;
5941 if (did_change && curwin->w_cursor.col)
5942 --curwin->w_cursor.col;
5943 }
5944
Bram Moolenaara52dfae2016-01-10 20:21:57 +01005945 if (did_change)
5946 {
5947 /* set the '[ and '] marks */
5948 curbuf->b_op_start = startpos;
5949 curbuf->b_op_end = endpos;
5950 if (curbuf->b_op_end.col > 0)
5951 --curbuf->b_op_end.col;
5952 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01005953
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005954theend:
5955 if (visual)
5956 curwin->w_cursor = save_cursor;
Bram Moolenaar8e081252016-03-21 23:13:32 +01005957 else if (did_change)
5958 curwin->w_set_curswant = TRUE;
Bram Moolenaar7ae4fbc2016-01-12 21:00:40 +01005959
Bram Moolenaard79e5502016-01-10 22:13:02 +01005960 return did_change;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961}
5962
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5964/*
5965 * SELECTION / PRIMARY ('*')
5966 *
5967 * Text selection stuff that uses the GUI selection register '*'. When using a
5968 * GUI this may be text from another window, otherwise it is the last text we
5969 * had highlighted with VIsual mode. With mouse support, clicking the middle
5970 * button performs the paste, otherwise you will need to do <"*p>. "
5971 * If not under X, it is synonymous with the clipboard register '+'.
5972 *
5973 * X CLIPBOARD ('+')
5974 *
5975 * Text selection stuff that uses the GUI clipboard register '+'.
5976 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5977 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5978 * otherwise you will need to do <"+p>. "
5979 * If not under X, it is synonymous with the selection register '*'.
5980 */
5981
5982/*
5983 * Routine to export any final X selection we had to the environment
Bram Moolenaarf58a8472017-03-05 18:03:04 +01005984 * so that the text is still available after Vim has exited. X selections
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 * only exist while the owning application exists, so we write to the
5986 * permanent (while X runs) store CUT_BUFFER0.
5987 * Dump the CLIPBOARD selection if we own it (it's logically the more
5988 * 'permanent' of the two), otherwise the PRIMARY one.
5989 * For now, use a hard-coded sanity limit of 1Mb of data.
5990 */
Bram Moolenaara6b7a082016-08-10 20:53:05 +02005991#if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005993x11_export_final_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 Display *dpy;
5996 char_u *str = NULL;
5997 long_u len = 0;
5998 int motion_type = -1;
5999
6000# ifdef FEAT_GUI
6001 if (gui.in_use)
6002 dpy = X_DISPLAY;
6003 else
6004# endif
6005# ifdef FEAT_XCLIPBOARD
6006 dpy = xterm_dpy;
6007# else
6008 return;
6009# endif
6010
6011 /* Get selection to export */
6012 if (clip_plus.owned)
6013 motion_type = clip_convert_selection(&str, &len, &clip_plus);
6014 else if (clip_star.owned)
6015 motion_type = clip_convert_selection(&str, &len, &clip_star);
6016
6017 /* Check it's OK */
6018 if (dpy != NULL && str != NULL && motion_type >= 0
6019 && len < 1024*1024 && len > 0)
6020 {
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006021 int ok = TRUE;
6022
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006023 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from
6024 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit
6025 * encoding conversion usually doesn't work, so keep the text as-is.
6026 */
6027 if (has_mbyte)
6028 {
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006029 vimconv_T vc;
6030
6031 vc.vc_type = CONV_NONE;
6032 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK)
6033 {
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01006034 int intlen = len;
6035 char_u *conv_str;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006036
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006037 vc.vc_fail = TRUE;
Bram Moolenaar331dafd2009-11-25 11:38:30 +00006038 conv_str = string_convert(&vc, str, &intlen);
6039 len = intlen;
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006040 if (conv_str != NULL)
6041 {
6042 vim_free(str);
6043 str = conv_str;
6044 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006045 else
6046 {
6047 ok = FALSE;
6048 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006049 convert_setup(&vc, NULL, NULL);
6050 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006051 else
6052 {
6053 ok = FALSE;
6054 }
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00006055 }
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006056
6057 /* Do not store the string if conversion failed. Better to use any
6058 * other selection than garbled text. */
6059 if (ok)
Bram Moolenaare2e663f2013-03-07 18:02:30 +01006060 {
6061 XStoreBuffer(dpy, (char *)str, (int)len, 0);
6062 XFlush(dpy);
6063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 }
6065
6066 vim_free(str);
6067}
6068#endif
6069
6070 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006071clip_free_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006073 yankreg_T *y_ptr = y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 if (cbd == &clip_plus)
6076 y_current = &y_regs[PLUS_REGISTER];
6077 else
6078 y_current = &y_regs[STAR_REGISTER];
6079 free_yank_all();
6080 y_current->y_size = 0;
6081 y_current = y_ptr;
6082}
6083
6084/*
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01006085 * Get the selected text and put it in register '*' or '+'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 */
6087 void
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006088clip_get_selection(Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006090 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 pos_T old_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 pos_T old_visual;
6093 int old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 colnr_T old_curswant;
6095 int old_set_curswant;
6096 pos_T old_op_start, old_op_end;
6097 oparg_T oa;
6098 cmdarg_T ca;
6099
6100 if (cbd->owned)
6101 {
6102 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
6103 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
6104 return;
6105
6106 /* Get the text between clip_star.start & clip_star.end */
6107 old_y_previous = y_previous;
6108 old_y_current = y_current;
6109 old_cursor = curwin->w_cursor;
6110 old_curswant = curwin->w_curswant;
6111 old_set_curswant = curwin->w_set_curswant;
6112 old_op_start = curbuf->b_op_start;
6113 old_op_end = curbuf->b_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 old_visual = VIsual;
6115 old_visual_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 clear_oparg(&oa);
6117 oa.regname = (cbd == &clip_plus ? '+' : '*');
6118 oa.op_type = OP_YANK;
6119 vim_memset(&ca, 0, sizeof(ca));
6120 ca.oap = &oa;
6121 ca.cmdchar = 'y';
6122 ca.count1 = 1;
6123 ca.retval = CA_NO_ADJ_OP_END;
6124 do_pending_operator(&ca, 0, TRUE);
6125 y_previous = old_y_previous;
6126 y_current = old_y_current;
6127 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006128 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 curwin->w_curswant = old_curswant;
6130 curwin->w_set_curswant = old_set_curswant;
6131 curbuf->b_op_start = old_op_start;
6132 curbuf->b_op_end = old_op_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 VIsual = old_visual;
6134 VIsual_mode = old_visual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 }
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02006136 else if (!is_clipboard_needs_update())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
6138 clip_free_selection(cbd);
6139
6140 /* Try to get selected text from another window */
6141 clip_gen_request_selection(cbd);
6142 }
6143}
6144
Bram Moolenaard44347f2011-06-19 01:14:29 +02006145/*
6146 * Convert from the GUI selection string into the '*'/'+' register.
6147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006149clip_yank_selection(
6150 int type,
6151 char_u *str,
6152 long len,
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006153 Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006155 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 if (cbd == &clip_plus)
6158 y_ptr = &y_regs[PLUS_REGISTER];
6159 else
6160 y_ptr = &y_regs[STAR_REGISTER];
6161
6162 clip_free_selection(cbd);
6163
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006164 str_to_reg(y_ptr, type, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165}
6166
6167/*
6168 * Convert the '*'/'+' register into a GUI selection string returned in *str
6169 * with length *len.
6170 * Returns the motion type, or -1 for failure.
6171 */
6172 int
Bram Moolenaar0554fa42019-06-14 21:36:54 +02006173clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
6175 char_u *p;
6176 int lnum;
6177 int i, j;
6178 int_u eolsize;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006179 yankreg_T *y_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 if (cbd == &clip_plus)
6182 y_ptr = &y_regs[PLUS_REGISTER];
6183 else
6184 y_ptr = &y_regs[STAR_REGISTER];
6185
6186#ifdef USE_CRNL
6187 eolsize = 2;
6188#else
6189 eolsize = 1;
6190#endif
6191
6192 *str = NULL;
6193 *len = 0;
6194 if (y_ptr->y_array == NULL)
6195 return -1;
6196
6197 for (i = 0; i < y_ptr->y_size; i++)
6198 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
6199
6200 /*
6201 * Don't want newline character at end of last line if we're in MCHAR mode.
6202 */
6203 if (y_ptr->y_type == MCHAR && *len >= eolsize)
6204 *len -= eolsize;
6205
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006206 p = *str = alloc(*len + 1); // add one to avoid zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (p == NULL)
6208 return -1;
6209 lnum = 0;
6210 for (i = 0, j = 0; i < (int)*len; i++, j++)
6211 {
6212 if (y_ptr->y_array[lnum][j] == '\n')
6213 p[i] = NUL;
6214 else if (y_ptr->y_array[lnum][j] == NUL)
6215 {
6216#ifdef USE_CRNL
6217 p[i++] = '\r';
6218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 p[i] = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 lnum++;
6221 j = -1;
6222 }
6223 else
6224 p[i] = y_ptr->y_array[lnum][j];
6225 }
6226 return y_ptr->y_type;
6227}
6228
6229
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230/*
6231 * If we have written to a clipboard register, send the text to the clipboard.
6232 */
6233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234may_set_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
6237 {
6238 clip_own_selection(&clip_star);
6239 clip_gen_set_selection(&clip_star);
6240 }
6241 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
6242 {
6243 clip_own_selection(&clip_plus);
6244 clip_gen_set_selection(&clip_plus);
6245 }
6246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
6248#endif /* FEAT_CLIPBOARD || PROTO */
6249
6250
6251#if defined(FEAT_DND) || defined(PROTO)
6252/*
6253 * Replace the contents of the '~' register with str.
6254 */
6255 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006256dnd_yank_drag_data(char_u *str, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006258 yankreg_T *curr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
6260 curr = y_current;
6261 y_current = &y_regs[TILDE_REGISTER];
6262 free_yank_all();
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006263 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 y_current = curr;
6265}
6266#endif
6267
6268
6269#if defined(FEAT_EVAL) || defined(PROTO)
6270/*
6271 * Return the type of a register.
6272 * Used for getregtype()
6273 * Returns MAUTO for error.
6274 */
6275 char_u
Bram Moolenaar9b578142016-01-30 19:39:49 +01006276get_reg_type(int regname, long *reglen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 switch (regname)
6279 {
6280 case '%': /* file name */
6281 case '#': /* alternate file name */
6282 case '=': /* expression */
6283 case ':': /* last command line */
6284 case '/': /* last search-pattern */
6285 case '.': /* last inserted text */
6286#ifdef FEAT_SEARCHPATH
6287 case Ctrl_F: /* Filename under cursor */
6288 case Ctrl_P: /* Path under cursor, expand via "path" */
6289#endif
6290 case Ctrl_W: /* word under cursor */
6291 case Ctrl_A: /* WORD (mnemonic All) under cursor */
6292 case '_': /* black hole: always empty */
6293 return MCHAR;
6294 }
6295
6296#ifdef FEAT_CLIPBOARD
6297 regname = may_get_selection(regname);
6298#endif
6299
Bram Moolenaar32b92012014-01-14 12:33:36 +01006300 if (regname != NUL && !valid_yank_reg(regname, FALSE))
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006301 return MAUTO;
Bram Moolenaar32b92012014-01-14 12:33:36 +01006302
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 get_yank_register(regname, FALSE);
6304
6305 if (y_current->y_array != NULL)
6306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (reglen != NULL && y_current->y_type == MBLOCK)
6308 *reglen = y_current->y_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 return y_current->y_type;
6310 }
6311 return MAUTO;
6312}
6313
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006314/*
6315 * When "flags" has GREG_LIST return a list with text "s".
6316 * Otherwise just return "s".
6317 */
6318 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006319getreg_wrap_one_line(char_u *s, int flags)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006320{
6321 if (flags & GREG_LIST)
6322 {
6323 list_T *list = list_alloc();
6324
6325 if (list != NULL)
6326 {
6327 if (list_append_string(list, NULL, -1) == FAIL)
6328 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006329 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006330 return NULL;
6331 }
6332 list->lv_first->li_tv.vval.v_string = s;
6333 }
6334 return (char_u *)list;
6335 }
6336 return s;
6337}
6338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339/*
6340 * Return the contents of a register as a single allocated string.
6341 * Used for "@r" in expressions and for getreg().
6342 * Returns NULL for error.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006343 * Flags:
6344 * GREG_NO_EXPR Do not allow expression register
6345 * GREG_EXPR_SRC For the expression register: return expression itself,
6346 * not the result of its evaluation.
6347 * GREG_LIST Return a list of lines in place of a single string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 */
6349 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006350get_reg_contents(int regname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 long i;
6353 char_u *retval;
6354 int allocated;
6355 long len;
6356
6357 /* Don't allow using an expression register inside an expression */
6358 if (regname == '=')
6359 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006360 if (flags & GREG_NO_EXPR)
6361 return NULL;
6362 if (flags & GREG_EXPR_SRC)
6363 return getreg_wrap_one_line(get_expr_line_src(), flags);
6364 return getreg_wrap_one_line(get_expr_line(), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366
6367 if (regname == '@') /* "@@" is used for unnamed register */
6368 regname = '"';
6369
6370 /* check for valid regname */
6371 if (regname != NUL && !valid_yank_reg(regname, FALSE))
6372 return NULL;
6373
6374#ifdef FEAT_CLIPBOARD
6375 regname = may_get_selection(regname);
6376#endif
6377
6378 if (get_spec_reg(regname, &retval, &allocated, FALSE))
6379 {
6380 if (retval == NULL)
6381 return NULL;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006382 if (allocated)
6383 return getreg_wrap_one_line(retval, flags);
6384 return getreg_wrap_one_line(vim_strsave(retval), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386
6387 get_yank_register(regname, FALSE);
6388 if (y_current->y_array == NULL)
6389 return NULL;
6390
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006391 if (flags & GREG_LIST)
6392 {
6393 list_T *list = list_alloc();
6394 int error = FALSE;
6395
6396 if (list == NULL)
6397 return NULL;
6398 for (i = 0; i < y_current->y_size; ++i)
6399 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
6400 error = TRUE;
6401 if (error)
6402 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006403 list_free(list);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006404 return NULL;
6405 }
6406 return (char_u *)list;
6407 }
6408
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 /*
6410 * Compute length of resulting string.
6411 */
6412 len = 0;
6413 for (i = 0; i < y_current->y_size; ++i)
6414 {
6415 len += (long)STRLEN(y_current->y_array[i]);
6416 /*
6417 * Insert a newline between lines and after last line if
6418 * y_type is MLINE.
6419 */
6420 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6421 ++len;
6422 }
6423
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006424 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
6426 /*
6427 * Copy the lines of the yank register into the string.
6428 */
6429 if (retval != NULL)
6430 {
6431 len = 0;
6432 for (i = 0; i < y_current->y_size; ++i)
6433 {
6434 STRCPY(retval + len, y_current->y_array[i]);
6435 len += (long)STRLEN(retval + len);
6436
6437 /*
6438 * Insert a NL between lines and after the last line if y_type is
6439 * MLINE.
6440 */
6441 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
6442 retval[len++] = '\n';
6443 }
6444 retval[len] = NUL;
6445 }
6446
6447 return retval;
6448}
6449
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006450 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006451init_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006452 int name,
6453 yankreg_T **old_y_previous,
6454 yankreg_T **old_y_current,
6455 int must_append,
6456 int *yank_type UNUSED)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006457{
6458 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
6459 {
6460 emsg_invreg(name);
6461 return FAIL;
6462 }
6463
6464 /* Don't want to change the current (unnamed) register */
6465 *old_y_previous = y_previous;
6466 *old_y_current = y_current;
6467
6468 get_yank_register(name, TRUE);
6469 if (!y_append && !must_append)
6470 free_yank_all();
6471 return OK;
6472}
6473
6474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006475finish_write_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006476 int name,
6477 yankreg_T *old_y_previous,
6478 yankreg_T *old_y_current)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006479{
6480# ifdef FEAT_CLIPBOARD
6481 /* Send text of clipboard register to the clipboard. */
6482 may_set_selection();
6483# endif
6484
6485 /* ':let @" = "val"' should change the meaning of the "" register */
6486 if (name != '"')
6487 y_previous = old_y_previous;
6488 y_current = old_y_current;
6489}
6490
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491/*
6492 * Store string "str" in register "name".
6493 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
6494 * If "must_append" is TRUE, always append to the register. Otherwise append
6495 * if "name" is an uppercase letter.
6496 * Note: "maxlen" and "must_append" don't work for the "/" register.
6497 * Careful: 'str' is modified, you may have to use a copy!
6498 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
6499 */
6500 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006501write_reg_contents(
6502 int name,
6503 char_u *str,
6504 int maxlen,
6505 int must_append)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
6508}
6509
6510 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006511write_reg_contents_lst(
6512 int name,
6513 char_u **strings,
6514 int maxlen UNUSED,
6515 int must_append,
6516 int yank_type,
6517 long block_len)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006518{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006519 yankreg_T *old_y_previous, *old_y_current;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006520
6521 if (name == '/'
6522#ifdef FEAT_EVAL
6523 || name == '='
6524#endif
6525 )
6526 {
6527 char_u *s;
6528
6529 if (strings[0] == NULL)
6530 s = (char_u *)"";
6531 else if (strings[1] != NULL)
6532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006533 emsg(_("E883: search pattern and expression register may not "
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006534 "contain two or more lines"));
6535 return;
6536 }
6537 else
6538 s = strings[0];
6539 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
6540 return;
6541 }
6542
6543 if (name == '_') /* black hole: nothing to do */
6544 return;
6545
6546 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6547 &yank_type) == FAIL)
6548 return;
6549
6550 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
6551
6552 finish_write_reg(name, old_y_previous, old_y_current);
6553}
6554
6555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556write_reg_contents_ex(
6557 int name,
6558 char_u *str,
6559 int maxlen,
6560 int must_append,
6561 int yank_type,
6562 long block_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006564 yankreg_T *old_y_previous, *old_y_current;
6565 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaare7566042005-06-17 22:00:15 +00006567 if (maxlen >= 0)
6568 len = maxlen;
6569 else
6570 len = (long)STRLEN(str);
6571
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 /* Special case: '/' search pattern */
6573 if (name == '/')
6574 {
6575 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
6576 return;
6577 }
6578
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006579 if (name == '#')
6580 {
6581 buf_T *buf;
6582
6583 if (VIM_ISDIGIT(*str))
6584 {
6585 int num = atoi((char *)str);
6586
6587 buf = buflist_findnr(num);
6588 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006589 semsg(_(e_nobufnr), (long)num);
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01006590 }
6591 else
6592 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
6593 TRUE, FALSE, FALSE));
6594 if (buf == NULL)
6595 return;
6596 curwin->w_alt_fnum = buf->b_fnum;
6597 return;
6598 }
6599
Bram Moolenaare7566042005-06-17 22:00:15 +00006600#ifdef FEAT_EVAL
6601 if (name == '=')
6602 {
6603 char_u *p, *s;
6604
6605 p = vim_strnsave(str, (int)len);
6606 if (p == NULL)
6607 return;
6608 if (must_append)
6609 {
6610 s = concat_str(get_expr_line_src(), p);
6611 vim_free(p);
6612 p = s;
Bram Moolenaare7566042005-06-17 22:00:15 +00006613 }
6614 set_expr_line(p);
6615 return;
6616 }
6617#endif
6618
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (name == '_') /* black hole: nothing to do */
6620 return;
6621
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006622 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
6623 &yank_type) == FAIL)
6624 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006626 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006628 finish_write_reg(name, old_y_previous, old_y_current);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630#endif /* FEAT_EVAL */
6631
6632#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
6633/*
6634 * Put a string into a register. When the register is not empty, the string
6635 * is appended.
6636 */
6637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006638str_to_reg(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006639 yankreg_T *y_ptr, /* pointer to yank register */
6640 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */
6641 char_u *str, /* string to put in register */
6642 long len, /* length of string */
6643 long blocklen, /* width of Visual block */
6644 int str_list) /* TRUE if str is char_u ** */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
Bram Moolenaard44347f2011-06-19 01:14:29 +02006646 int type; /* MCHAR, MLINE or MBLOCK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int lnum;
6648 long start;
6649 long i;
6650 int extra;
6651 int newlines; /* number of lines added */
6652 int extraline = 0; /* extra line at the end */
6653 int append = FALSE; /* append to last line in register */
6654 char_u *s;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006655 char_u **ss;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 long maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
Bram Moolenaarcde547a2009-11-17 11:43:06 +00006659 if (y_ptr->y_array == NULL) /* NULL means empty register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 y_ptr->y_size = 0;
6661
Bram Moolenaard44347f2011-06-19 01:14:29 +02006662 if (yank_type == MAUTO)
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006663 type = ((str_list || (len > 0 && (str[len - 1] == NL
6664 || str[len - 1] == CAR)))
Bram Moolenaard44347f2011-06-19 01:14:29 +02006665 ? MLINE : MCHAR);
6666 else
6667 type = yank_type;
6668
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 /*
6670 * Count the number of lines within the string
6671 */
6672 newlines = 0;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006673 if (str_list)
6674 {
6675 for (ss = (char_u **) str; *ss != NULL; ++ss)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 ++newlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006680 for (i = 0; i < len; i++)
6681 if (str[i] == '\n')
6682 ++newlines;
6683 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6684 {
6685 extraline = 1;
6686 ++newlines; /* count extra newline at the end */
6687 }
6688 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6689 {
6690 append = TRUE;
6691 --newlines; /* uncount newline when appending first line */
6692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 }
6694
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006695 /* Without any lines make the register empty. */
6696 if (y_ptr->y_size + newlines == 0)
6697 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01006698 VIM_CLEAR(y_ptr->y_array);
Bram Moolenaar659c94d2015-05-04 20:19:21 +02006699 return;
6700 }
6701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 /*
6703 * Allocate an array to hold the pointers to the new register lines.
6704 * If the register was not empty, move the existing lines to the new array.
6705 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006706 pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (pp == NULL) /* out of memory */
6708 return;
6709 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6710 pp[lnum] = y_ptr->y_array[lnum];
6711 vim_free(y_ptr->y_array);
6712 y_ptr->y_array = pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 maxlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 /*
6716 * Find the end of each line and save it into the array.
6717 */
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006718 if (str_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006720 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
6721 {
Bram Moolenaar121f9bd2014-04-29 15:55:43 +02006722 i = (long)STRLEN(*ss);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006723 pp[lnum] = vim_strnsave(*ss, i);
6724 if (i > maxlen)
6725 maxlen = i;
6726 }
6727 }
6728 else
6729 {
6730 for (start = 0; start < len + extraline; start += i + 1)
6731 {
6732 for (i = start; i < len; ++i) /* find the end of the line */
6733 if (str[i] == '\n')
6734 break;
6735 i -= start; /* i is now length of line */
6736 if (i > maxlen)
6737 maxlen = i;
6738 if (append)
6739 {
6740 --lnum;
6741 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6742 }
6743 else
6744 extra = 0;
Bram Moolenaar964b3742019-05-24 18:54:09 +02006745 s = alloc(i + extra + 1);
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006746 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 break;
Bram Moolenaar5a50c222014-04-02 22:17:10 +02006748 if (extra)
6749 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6750 if (append)
6751 vim_free(y_ptr->y_array[lnum]);
6752 if (i)
6753 mch_memmove(s + extra, str + start, (size_t)i);
6754 extra += i;
6755 s[extra] = NUL;
6756 y_ptr->y_array[lnum++] = s;
6757 while (--extra >= 0)
6758 {
6759 if (*s == NUL)
6760 *s = '\n'; /* replace NUL with newline */
6761 ++s;
6762 }
6763 append = FALSE; /* only first line is appended */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 }
6766 y_ptr->y_type = type;
6767 y_ptr->y_size = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if (type == MBLOCK)
6769 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6770 else
6771 y_ptr->y_width = 0;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006772#ifdef FEAT_VIMINFO
6773 y_ptr->y_time_set = vim_time();
6774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
6776#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6777
6778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006779clear_oparg(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 vim_memset(oap, 0, sizeof(oparg_T));
6782}
6783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006785 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 *
6787 * "Words" are counted by looking for boundaries between non-space and
6788 * space characters. (it seems to produce results that match 'wc'.)
6789 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006790 * Return value is byte count; word count for the line is added to "*wc".
6791 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 *
6793 * The function will only examine the first "limit" characters in the
6794 * line, stopping if it encounters an end-of-line (NUL byte). In that
6795 * case, eol_size will be added to the character count to account for
6796 * the size of the EOL character.
6797 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006798 static varnumber_T
Bram Moolenaar9b578142016-01-30 19:39:49 +01006799line_count_info(
6800 char_u *line,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006801 varnumber_T *wc,
6802 varnumber_T *cc,
6803 varnumber_T limit,
Bram Moolenaar9b578142016-01-30 19:39:49 +01006804 int eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006806 varnumber_T i;
6807 varnumber_T words = 0;
6808 varnumber_T chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 int is_word = 0;
6810
Bram Moolenaar88b1ba12012-06-29 13:34:19 +02006811 for (i = 0; i < limit && line[i] != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
6813 if (is_word)
6814 {
6815 if (vim_isspace(line[i]))
6816 {
6817 words++;
6818 is_word = 0;
6819 }
6820 }
6821 else if (!vim_isspace(line[i]))
6822 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006823 ++chars;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006824 i += (*mb_ptr2len)(line + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
6826
6827 if (is_word)
6828 words++;
6829 *wc += words;
6830
6831 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar1cb7e092011-08-10 12:11:01 +02006832 if (i < limit && line[i] == NUL)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006835 chars += eol_size;
6836 }
6837 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 return i;
6839}
6840
6841/*
6842 * Give some info about the position of the cursor (for "g CTRL-G").
6843 * In Visual mode, give some info about the selected region. (In this case,
6844 * the *_count_cursor variables store running totals for the selection.)
Bram Moolenaared767a22016-01-03 22:49:16 +01006845 * When "dict" is not NULL store the info there instead of showing it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 */
6847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006848cursor_pos_info(dict_T *dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006851 char_u buf1[50];
6852 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 linenr_T lnum;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006854 varnumber_T byte_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006855 varnumber_T bom_count = 0;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006856 varnumber_T byte_count_cursor = 0;
6857 varnumber_T char_count = 0;
6858 varnumber_T char_count_cursor = 0;
6859 varnumber_T word_count = 0;
6860 varnumber_T word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006861 int eol_size;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006862 varnumber_T last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 long line_count_selected = 0;
6864 pos_T min_pos, max_pos;
6865 oparg_T oparg;
6866 struct block_def bd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
6868 /*
6869 * Compute the length of the file in characters.
6870 */
6871 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6872 {
Bram Moolenaared767a22016-01-03 22:49:16 +01006873 if (dict == NULL)
6874 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006875 msg(_(no_lines_msg));
Bram Moolenaared767a22016-01-03 22:49:16 +01006876 return;
6877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
6879 else
6880 {
6881 if (get_fileformat(curbuf) == EOL_DOS)
6882 eol_size = 2;
6883 else
6884 eol_size = 1;
6885
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (VIsual_active)
6887 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006888 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 min_pos = VIsual;
6891 max_pos = curwin->w_cursor;
6892 }
6893 else
6894 {
6895 min_pos = curwin->w_cursor;
6896 max_pos = VIsual;
6897 }
6898 if (*p_sel == 'e' && max_pos.col > 0)
6899 --max_pos.col;
6900
6901 if (VIsual_mode == Ctrl_V)
6902 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00006903#ifdef FEAT_LINEBREAK
6904 char_u * saved_sbr = p_sbr;
6905
6906 /* Make 'sbr' empty for a moment to get the correct size. */
6907 p_sbr = empty_option;
6908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 oparg.is_VIsual = 1;
6910 oparg.block_mode = TRUE;
6911 oparg.op_type = OP_NOP;
6912 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006913 &oparg.start_vcol, &oparg.end_vcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00006914#ifdef FEAT_LINEBREAK
6915 p_sbr = saved_sbr;
6916#endif
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006917 if (curwin->w_curswant == MAXCOL)
6918 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 /* Swap the start, end vcol if needed */
6920 if (oparg.end_vcol < oparg.start_vcol)
6921 {
6922 oparg.end_vcol += oparg.start_vcol;
6923 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6924 oparg.end_vcol -= oparg.start_vcol;
6925 }
6926 }
6927 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6931 {
6932 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006933 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
6935 ui_breakcheck();
6936 if (got_int)
6937 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006938 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 }
6940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 /* Do extra processing for VIsual mode. */
6942 if (VIsual_active
6943 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6944 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006945 char_u *s = NULL;
6946 long len = 0L;
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 switch (VIsual_mode)
6949 {
6950 case Ctrl_V:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 block_prep(&oparg, &bd, lnum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 virtual_op = MAYBE;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006954 s = bd.textstart;
6955 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 break;
6957 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006958 s = ml_get(lnum);
6959 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 break;
6961 case 'v':
6962 {
6963 colnr_T start_col = (lnum == min_pos.lnum)
6964 ? min_pos.col : 0;
6965 colnr_T end_col = (lnum == max_pos.lnum)
6966 ? max_pos.col - start_col + 1 : MAXCOL;
6967
Bram Moolenaardef9e822004-12-31 20:58:58 +00006968 s = ml_get(lnum) + start_col;
6969 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971 break;
6972 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006973 if (s != NULL)
6974 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006975 byte_count_cursor += line_count_info(s, &word_count_cursor,
6976 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006977 if (lnum == curbuf->b_ml.ml_line_count
6978 && !curbuf->b_p_eol
Bram Moolenaar34d72d42015-07-17 14:18:08 +02006979 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006980 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006981 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 {
6986 /* In non-visual mode, check for the line the cursor is on */
6987 if (lnum == curwin->w_cursor.lnum)
6988 {
6989 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006990 char_count_cursor += char_count;
6991 byte_count_cursor = byte_count +
6992 line_count_info(ml_get(lnum),
6993 &word_count_cursor, &char_count_cursor,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006994 (varnumber_T)(curwin->w_cursor.col + 1),
6995 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997 }
6998 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006999 byte_count += line_count_info(ml_get(lnum), &word_count,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007000 &char_count, (varnumber_T)MAXCOL,
7001 eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003
7004 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02007005 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
Bram Moolenaar7c626922005-02-07 22:01:03 +00007006 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
Bram Moolenaared767a22016-01-03 22:49:16 +01007008 if (dict == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007010 if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaared767a22016-01-03 22:49:16 +01007012 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
7013 {
7014 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
7015 &max_pos.col);
7016 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
7017 (long)(oparg.end_vcol - oparg.start_vcol + 1));
7018 }
7019 else
7020 buf1[0] = NUL;
7021
7022 if (char_count_cursor == byte_count_cursor
7023 && char_count == byte_count)
7024 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007025 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007026 buf1, line_count_selected,
7027 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007028 (long_long_T)word_count_cursor,
7029 (long_long_T)word_count,
7030 (long_long_T)byte_count_cursor,
7031 (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007032 else
7033 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007034 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007035 buf1, line_count_selected,
7036 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007037 (long_long_T)word_count_cursor,
7038 (long_long_T)word_count,
7039 (long_long_T)char_count_cursor,
7040 (long_long_T)char_count,
7041 (long_long_T)byte_count_cursor,
7042 (long_long_T)byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 else
Bram Moolenaared767a22016-01-03 22:49:16 +01007045 {
7046 p = ml_get_curline();
7047 validate_virtcol();
7048 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
7049 (int)curwin->w_virtcol + 1);
7050 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
7051 linetabsize(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaared767a22016-01-03 22:49:16 +01007053 if (char_count_cursor == byte_count_cursor
7054 && char_count == byte_count)
7055 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007056 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007057 (char *)buf1, (char *)buf2,
7058 (long)curwin->w_cursor.lnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007060 (long_long_T)word_count_cursor, (long_long_T)word_count,
7061 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007062 else
7063 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007064 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
Bram Moolenaared767a22016-01-03 22:49:16 +01007065 (char *)buf1, (char *)buf2,
7066 (long)curwin->w_cursor.lnum,
Bram Moolenaar7c626922005-02-07 22:01:03 +00007067 (long)curbuf->b_ml.ml_line_count,
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007068 (long_long_T)word_count_cursor, (long_long_T)word_count,
7069 (long_long_T)char_count_cursor, (long_long_T)char_count,
7070 (long_long_T)byte_count_cursor, (long_long_T)byte_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007071 }
7072 }
7073
Bram Moolenaared767a22016-01-03 22:49:16 +01007074 bom_count = bomb_size();
Bram Moolenaardbec7492019-09-13 22:16:21 +02007075 if (dict == NULL && bom_count > 0)
Bram Moolenaarc71982b2016-01-04 21:43:08 +01007076 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE,
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007077 _("(+%lld for BOM)"), (long_long_T)bom_count);
Bram Moolenaared767a22016-01-03 22:49:16 +01007078 if (dict == NULL)
7079 {
Bram Moolenaardbec7492019-09-13 22:16:21 +02007080 // Don't shorten this message, the user asked for it.
Bram Moolenaared767a22016-01-03 22:49:16 +01007081 p = p_shm;
7082 p_shm = (char_u *)"";
Bram Moolenaar32526b32019-01-19 17:43:09 +01007083 msg((char *)IObuff);
Bram Moolenaared767a22016-01-03 22:49:16 +01007084 p_shm = p;
7085 }
7086 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007087#if defined(FEAT_EVAL)
Bram Moolenaared767a22016-01-03 22:49:16 +01007088 if (dict != NULL)
7089 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02007090 dict_add_number(dict, "words", word_count);
7091 dict_add_number(dict, "chars", char_count);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01007092 dict_add_number(dict, "bytes", byte_count + bom_count);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007093 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
7094 byte_count_cursor);
7095 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
7096 char_count_cursor);
7097 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
7098 word_count_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
Bram Moolenaar718272a2016-01-03 22:56:45 +01007100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101}