1 /* vi:set ts=8 sts=4 sw=4 noet:
2 *
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_join
13 */
14
15 #include "vim.h"
16
17 static void shift_block(oparg_T *oap, int amount);
18 static void mb_adjust_opend(oparg_T *oap);
19 static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1);
20
21 // Flags for third item in "opchars".
22 #define OPF_LINES 1 // operator always works on lines
23 #define OPF_CHANGE 2 // operator changes text
24
25 /*
26 * The names of operators.
27 * IMPORTANT: Index must correspond with defines in vim.h!!!
28 * The third field holds OPF_ flags.
29 */
30 static char opchars[][3] =
31 {
32 {NUL, NUL, 0}, // OP_NOP
33 {'d', NUL, OPF_CHANGE}, // OP_DELETE
34 {'y', NUL, 0}, // OP_YANK
35 {'c', NUL, OPF_CHANGE}, // OP_CHANGE
36 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT
37 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT
38 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER
39 {'g', '~', OPF_CHANGE}, // OP_TILDE
40 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT
41 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT
42 {':', NUL, OPF_LINES}, // OP_COLON
43 {'g', 'U', OPF_CHANGE}, // OP_UPPER
44 {'g', 'u', OPF_CHANGE}, // OP_LOWER
45 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN
46 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS
47 {'g', '?', OPF_CHANGE}, // OP_ROT13
48 {'r', NUL, OPF_CHANGE}, // OP_REPLACE
49 {'I', NUL, OPF_CHANGE}, // OP_INSERT
50 {'A', NUL, OPF_CHANGE}, // OP_APPEND
51 {'z', 'f', OPF_LINES}, // OP_FOLD
52 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN
53 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC
54 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE
55 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC
56 {'z', 'd', OPF_LINES}, // OP_FOLDDEL
57 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC
58 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2
59 {'g', '@', OPF_CHANGE}, // OP_FUNCTION
60 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD
61 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB
62 };
63
64 /*
65 * Translate a command name into an operator type.
66 * Must only be called with a valid operator name!
67 */
68 int
get_op_type(int char1,int char2)69 get_op_type(int char1, int char2)
70 {
71 int i;
72
73 if (char1 == 'r') // ignore second character
74 return OP_REPLACE;
75 if (char1 == '~') // when tilde is an operator
76 return OP_TILDE;
77 if (char1 == 'g' && char2 == Ctrl_A) // add
78 return OP_NR_ADD;
79 if (char1 == 'g' && char2 == Ctrl_X) // subtract
80 return OP_NR_SUB;
81 if (char1 == 'z' && char2 == 'y') // OP_YANK
82 return OP_YANK;
83 for (i = 0; ; ++i)
84 {
85 if (opchars[i][0] == char1 && opchars[i][1] == char2)
86 break;
87 if (i == (int)ARRAY_LENGTH(opchars) - 1)
88 {
89 internal_error("get_op_type()");
90 break;
91 }
92 }
93 return i;
94 }
95
96 /*
97 * Return TRUE if operator "op" always works on whole lines.
98 */
99 static int
op_on_lines(int op)100 op_on_lines(int op)
101 {
102 return opchars[op][2] & OPF_LINES;
103 }
104
105 #if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
106 /*
107 * Return TRUE if operator "op" changes text.
108 */
109 int
op_is_change(int op)110 op_is_change(int op)
111 {
112 return opchars[op][2] & OPF_CHANGE;
113 }
114 #endif
115
116 /*
117 * Get first operator command character.
118 * Returns 'g' or 'z' if there is another command character.
119 */
120 int
get_op_char(int optype)121 get_op_char(int optype)
122 {
123 return opchars[optype][0];
124 }
125
126 /*
127 * Get second operator command character.
128 */
129 int
get_extra_op_char(int optype)130 get_extra_op_char(int optype)
131 {
132 return opchars[optype][1];
133 }
134
135 /*
136 * op_shift - handle a shift operation
137 */
138 void
op_shift(oparg_T * oap,int curs_top,int amount)139 op_shift(oparg_T *oap, int curs_top, int amount)
140 {
141 long i;
142 int first_char;
143 int block_col = 0;
144
145 if (u_save((linenr_T)(oap->start.lnum - 1),
146 (linenr_T)(oap->end.lnum + 1)) == FAIL)
147 return;
148
149 if (oap->block_mode)
150 block_col = curwin->w_cursor.col;
151
152 for (i = oap->line_count; --i >= 0; )
153 {
154 first_char = *ml_get_curline();
155 if (first_char == NUL) // empty line
156 curwin->w_cursor.col = 0;
157 else if (oap->block_mode)
158 shift_block(oap, amount);
159 else
160 // Move the line right if it doesn't start with '#', 'smartindent'
161 // isn't set or 'cindent' isn't set or '#' isn't in 'cino'.
162 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
163 if (first_char != '#' || !preprocs_left())
164 #endif
165 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE);
166 ++curwin->w_cursor.lnum;
167 }
168
169 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
170 if (oap->block_mode)
171 {
172 curwin->w_cursor.lnum = oap->start.lnum;
173 curwin->w_cursor.col = block_col;
174 }
175 else if (curs_top) // put cursor on first line, for ">>"
176 {
177 curwin->w_cursor.lnum = oap->start.lnum;
178 beginline(BL_SOL | BL_FIX); // shift_line() may have set cursor.col
179 }
180 else
181 --curwin->w_cursor.lnum; // put cursor on last line, for ":>"
182
183 #ifdef FEAT_FOLDING
184 // The cursor line is not in a closed fold
185 foldOpenCursor();
186 #endif
187
188
189 if (oap->line_count > p_report)
190 {
191 char *op;
192 char *msg_line_single;
193 char *msg_line_plural;
194
195 if (oap->op_type == OP_RSHIFT)
196 op = ">";
197 else
198 op = "<";
199 msg_line_single = NGETTEXT("%ld line %sed %d time",
200 "%ld line %sed %d times", amount);
201 msg_line_plural = NGETTEXT("%ld lines %sed %d time",
202 "%ld lines %sed %d times", amount);
203 vim_snprintf((char *)IObuff, IOSIZE,
204 NGETTEXT(msg_line_single, msg_line_plural, oap->line_count),
205 oap->line_count, op, amount);
206 msg_attr_keep((char *)IObuff, 0, TRUE);
207 }
208
209 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
210 {
211 // Set "'[" and "']" marks.
212 curbuf->b_op_start = oap->start;
213 curbuf->b_op_end.lnum = oap->end.lnum;
214 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
215 if (curbuf->b_op_end.col > 0)
216 --curbuf->b_op_end.col;
217 }
218 }
219
220 /*
221 * Shift the current line one shiftwidth left (if left != 0) or right
222 * leaves cursor on first blank in the line.
223 */
224 void
shift_line(int left,int round,int amount,int call_changed_bytes)225 shift_line(
226 int left,
227 int round,
228 int amount,
229 int call_changed_bytes) // call changed_bytes()
230 {
231 int count;
232 int i, j;
233 int sw_val = (int)get_sw_value_indent(curbuf);
234
235 count = get_indent(); // get current indent
236
237 if (round) // round off indent
238 {
239 i = count / sw_val; // number of 'shiftwidth' rounded down
240 j = count % sw_val; // extra spaces
241 if (j && left) // first remove extra spaces
242 --amount;
243 if (left)
244 {
245 i -= amount;
246 if (i < 0)
247 i = 0;
248 }
249 else
250 i += amount;
251 count = i * sw_val;
252 }
253 else // original vi indent
254 {
255 if (left)
256 {
257 count -= sw_val * amount;
258 if (count < 0)
259 count = 0;
260 }
261 else
262 count += sw_val * amount;
263 }
264
265 // Set new indent
266 if (State & VREPLACE_FLAG)
267 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes);
268 else
269 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0);
270 }
271
272 /*
273 * Shift one line of the current block one shiftwidth right or left.
274 * Leaves cursor on first character in block.
275 */
276 static void
shift_block(oparg_T * oap,int amount)277 shift_block(oparg_T *oap, int amount)
278 {
279 int left = (oap->op_type == OP_LSHIFT);
280 int oldstate = State;
281 int total;
282 char_u *newp, *oldp;
283 int oldcol = curwin->w_cursor.col;
284 int sw_val = (int)get_sw_value_indent(curbuf);
285 int ts_val = (int)curbuf->b_p_ts;
286 struct block_def bd;
287 int incr;
288 colnr_T ws_vcol;
289 int i = 0, j = 0;
290 int len;
291 #ifdef FEAT_RIGHTLEFT
292 int old_p_ri = p_ri;
293
294 p_ri = 0; // don't want revins in indent
295 #endif
296
297 State = INSERT; // don't want REPLACE for State
298 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
299 if (bd.is_short)
300 return;
301
302 // total is number of screen columns to be inserted/removed
303 total = (int)((unsigned)amount * (unsigned)sw_val);
304 if ((total / sw_val) != amount)
305 return; // multiplication overflow
306
307 oldp = ml_get_curline();
308
309 if (!left)
310 {
311 /*
312 * 1. Get start vcol
313 * 2. Total ws vcols
314 * 3. Divvy into TABs & spp
315 * 4. Construct new string
316 */
317 total += bd.pre_whitesp; // all virtual WS up to & incl a split TAB
318 ws_vcol = bd.start_vcol - bd.pre_whitesp;
319 if (bd.startspaces)
320 {
321 if (has_mbyte)
322 {
323 if ((*mb_ptr2len)(bd.textstart) == 1)
324 ++bd.textstart;
325 else
326 {
327 ws_vcol = 0;
328 bd.startspaces = 0;
329 }
330 }
331 else
332 ++bd.textstart;
333 }
334 for ( ; VIM_ISWHITE(*bd.textstart); )
335 {
336 // TODO: is passing bd.textstart for start of the line OK?
337 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart,
338 (colnr_T)(bd.start_vcol));
339 total += incr;
340 bd.start_vcol += incr;
341 }
342 // OK, now total=all the VWS reqd, and textstart points at the 1st
343 // non-ws char in the block.
344 #ifdef FEAT_VARTABS
345 if (!curbuf->b_p_et)
346 tabstop_fromto(ws_vcol, ws_vcol + total,
347 ts_val, curbuf->b_p_vts_array, &i, &j);
348 else
349 j = total;
350 #else
351 if (!curbuf->b_p_et)
352 i = ((ws_vcol % ts_val) + total) / ts_val; // number of tabs
353 if (i)
354 j = ((ws_vcol % ts_val) + total) % ts_val; // number of spp
355 else
356 j = total;
357 #endif
358 // if we're splitting a TAB, allow for it
359 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
360 len = (int)STRLEN(bd.textstart) + 1;
361 newp = alloc(bd.textcol + i + j + len);
362 if (newp == NULL)
363 return;
364 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
365 mch_memmove(newp, oldp, (size_t)bd.textcol);
366 vim_memset(newp + bd.textcol, TAB, (size_t)i);
367 vim_memset(newp + bd.textcol + i, ' ', (size_t)j);
368 // the end
369 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
370 }
371 else // left
372 {
373 colnr_T destination_col; // column to which text in block will
374 // be shifted
375 char_u *verbatim_copy_end; // end of the part of the line which is
376 // copied verbatim
377 colnr_T verbatim_copy_width;// the (displayed) width of this part
378 // of line
379 unsigned fill; // nr of spaces that replace a TAB
380 unsigned new_line_len; // the length of the line after the
381 // block shift
382 size_t block_space_width;
383 size_t shift_amount;
384 char_u *non_white = bd.textstart;
385 colnr_T non_white_col;
386
387 /*
388 * Firstly, let's find the first non-whitespace character that is
389 * displayed after the block's start column and the character's column
390 * number. Also, let's calculate the width of all the whitespace
391 * characters that are displayed in the block and precede the searched
392 * non-whitespace character.
393 */
394
395 // If "bd.startspaces" is set, "bd.textstart" points to the character,
396 // the part of which is displayed at the block's beginning. Let's start
397 // searching from the next character.
398 if (bd.startspaces)
399 MB_PTR_ADV(non_white);
400
401 // The character's column is in "bd.start_vcol".
402 non_white_col = bd.start_vcol;
403
404 while (VIM_ISWHITE(*non_white))
405 {
406 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col);
407 non_white_col += incr;
408 }
409
410 block_space_width = non_white_col - oap->start_vcol;
411 // We will shift by "total" or "block_space_width", whichever is less.
412 shift_amount = (block_space_width < (size_t)total
413 ? block_space_width : (size_t)total);
414
415 // The column to which we will shift the text.
416 destination_col = (colnr_T)(non_white_col - shift_amount);
417
418 // Now let's find out how much of the beginning of the line we can
419 // reuse without modification.
420 verbatim_copy_end = bd.textstart;
421 verbatim_copy_width = bd.start_vcol;
422
423 // If "bd.startspaces" is set, "bd.textstart" points to the character
424 // preceding the block. We have to subtract its width to obtain its
425 // column number.
426 if (bd.startspaces)
427 verbatim_copy_width -= bd.start_char_vcols;
428 while (verbatim_copy_width < destination_col)
429 {
430 char_u *line = verbatim_copy_end;
431
432 // TODO: is passing verbatim_copy_end for start of the line OK?
433 incr = lbr_chartabsize(line, verbatim_copy_end,
434 verbatim_copy_width);
435 if (verbatim_copy_width + incr > destination_col)
436 break;
437 verbatim_copy_width += incr;
438 MB_PTR_ADV(verbatim_copy_end);
439 }
440
441 // If "destination_col" is different from the width of the initial
442 // part of the line that will be copied, it means we encountered a tab
443 // character, which we will have to partly replace with spaces.
444 fill = destination_col - verbatim_copy_width;
445
446 // The replacement line will consist of:
447 // - the beginning of the original line up to "verbatim_copy_end",
448 // - "fill" number of spaces,
449 // - the rest of the line, pointed to by non_white.
450 new_line_len = (unsigned)(verbatim_copy_end - oldp)
451 + fill
452 + (unsigned)STRLEN(non_white) + 1;
453
454 newp = alloc(new_line_len);
455 if (newp == NULL)
456 return;
457 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp));
458 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill);
459 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white);
460 }
461 // replace the line
462 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
463 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
464 State = oldstate;
465 curwin->w_cursor.col = oldcol;
466 #ifdef FEAT_RIGHTLEFT
467 p_ri = old_p_ri;
468 #endif
469 }
470
471 /*
472 * Insert string "s" (b_insert ? before : after) block :AKelly
473 * Caller must prepare for undo.
474 */
475 static void
block_insert(oparg_T * oap,char_u * s,int b_insert,struct block_def * bdp)476 block_insert(
477 oparg_T *oap,
478 char_u *s,
479 int b_insert,
480 struct block_def *bdp)
481 {
482 int ts_val;
483 int count = 0; // extra spaces to replace a cut TAB
484 int spaces = 0; // non-zero if cutting a TAB
485 colnr_T offset; // pointer along new line
486 colnr_T startcol; // column where insert starts
487 unsigned s_len; // STRLEN(s)
488 char_u *newp, *oldp; // new, old lines
489 linenr_T lnum; // loop var
490 int oldstate = State;
491
492 State = INSERT; // don't want REPLACE for State
493 s_len = (unsigned)STRLEN(s);
494
495 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
496 {
497 block_prep(oap, bdp, lnum, TRUE);
498 if (bdp->is_short && b_insert)
499 continue; // OP_INSERT, line ends before block start
500
501 oldp = ml_get(lnum);
502
503 if (b_insert)
504 {
505 ts_val = bdp->start_char_vcols;
506 spaces = bdp->startspaces;
507 if (spaces != 0)
508 count = ts_val - 1; // we're cutting a TAB
509 offset = bdp->textcol;
510 }
511 else // append
512 {
513 ts_val = bdp->end_char_vcols;
514 if (!bdp->is_short) // spaces = padding after block
515 {
516 spaces = (bdp->endspaces ? ts_val - bdp->endspaces : 0);
517 if (spaces != 0)
518 count = ts_val - 1; // we're cutting a TAB
519 offset = bdp->textcol + bdp->textlen - (spaces != 0);
520 }
521 else // spaces = padding to block edge
522 {
523 // if $ used, just append to EOL (ie spaces==0)
524 if (!bdp->is_MAX)
525 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
526 count = spaces;
527 offset = bdp->textcol + bdp->textlen;
528 }
529 }
530
531 if (has_mbyte && spaces > 0)
532 {
533 int off;
534
535 // Avoid starting halfway a multi-byte character.
536 if (b_insert)
537 {
538 off = (*mb_head_off)(oldp, oldp + offset + spaces);
539 }
540 else
541 {
542 off = (*mb_off_next)(oldp, oldp + offset);
543 offset += off;
544 }
545 spaces -= off;
546 count -= off;
547 }
548 if (spaces < 0) // can happen when the cursor was moved
549 spaces = 0;
550
551 newp = alloc(STRLEN(oldp) + s_len + count + 1);
552 if (newp == NULL)
553 continue;
554
555 // copy up to shifted part
556 mch_memmove(newp, oldp, (size_t)(offset));
557 oldp += offset;
558
559 // insert pre-padding
560 vim_memset(newp + offset, ' ', (size_t)spaces);
561 startcol = offset + spaces;
562
563 // copy the new text
564 mch_memmove(newp + startcol, s, (size_t)s_len);
565 offset += s_len;
566
567 if (spaces && !bdp->is_short)
568 {
569 // insert post-padding
570 vim_memset(newp + offset + spaces, ' ', (size_t)(ts_val - spaces));
571 // We're splitting a TAB, don't copy it.
572 oldp++;
573 // We allowed for that TAB, remember this now
574 count++;
575 }
576
577 if (spaces > 0)
578 offset += count;
579 STRMOVE(newp + offset, oldp);
580
581 ml_replace(lnum, newp, FALSE);
582
583 if (b_insert)
584 // correct any text properties
585 inserted_bytes(lnum, startcol, s_len);
586
587 if (lnum == oap->end.lnum)
588 {
589 // Set "']" mark to the end of the block instead of the end of
590 // the insert in the first line.
591 curbuf->b_op_end.lnum = oap->end.lnum;
592 curbuf->b_op_end.col = offset;
593 }
594 } // for all lnum
595
596 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
597
598 State = oldstate;
599 }
600
601 /*
602 * Handle a delete operation.
603 *
604 * Return FAIL if undo failed, OK otherwise.
605 */
606 int
op_delete(oparg_T * oap)607 op_delete(oparg_T *oap)
608 {
609 int n;
610 linenr_T lnum;
611 char_u *ptr;
612 char_u *newp, *oldp;
613 struct block_def bd;
614 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
615 int did_yank = FALSE;
616
617 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to do
618 return OK;
619
620 // Nothing to delete, return here. Do prepare undo, for op_change().
621 if (oap->empty)
622 return u_save_cursor();
623
624 if (!curbuf->b_p_ma)
625 {
626 emsg(_(e_cannot_make_changes_modifiable_is_off));
627 return FAIL;
628 }
629
630 #ifdef FEAT_CLIPBOARD
631 adjust_clip_reg(&oap->regname);
632 #endif
633
634 if (has_mbyte)
635 mb_adjust_opend(oap);
636
637 /*
638 * Imitate the strange Vi behaviour: If the delete spans more than one
639 * line and motion_type == MCHAR and the result is a blank line, make the
640 * delete linewise. Don't do this for the change command or Visual mode.
641 */
642 if ( oap->motion_type == MCHAR
643 && !oap->is_VIsual
644 && !oap->block_mode
645 && oap->line_count > 1
646 && oap->motion_force == NUL
647 && oap->op_type == OP_DELETE)
648 {
649 ptr = ml_get(oap->end.lnum) + oap->end.col;
650 if (*ptr != NUL)
651 ptr += oap->inclusive;
652 ptr = skipwhite(ptr);
653 if (*ptr == NUL && inindent(0))
654 oap->motion_type = MLINE;
655 }
656
657 /*
658 * Check for trying to delete (e.g. "D") in an empty line.
659 * Note: For the change operator it is ok.
660 */
661 if ( oap->motion_type == MCHAR
662 && oap->line_count == 1
663 && oap->op_type == OP_DELETE
664 && *ml_get(oap->start.lnum) == NUL)
665 {
666 /*
667 * It's an error to operate on an empty region, when 'E' included in
668 * 'cpoptions' (Vi compatible).
669 */
670 if (virtual_op)
671 // Virtual editing: Nothing gets deleted, but we set the '[ and ']
672 // marks as if it happened.
673 goto setmarks;
674 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
675 beep_flush();
676 return OK;
677 }
678
679 /*
680 * Do a yank of whatever we're about to delete.
681 * If a yank register was specified, put the deleted text into that
682 * register. For the black hole register '_' don't yank anything.
683 */
684 if (oap->regname != '_')
685 {
686 if (oap->regname != 0)
687 {
688 // check for read-only register
689 if (!valid_yank_reg(oap->regname, TRUE))
690 {
691 beep_flush();
692 return OK;
693 }
694 get_yank_register(oap->regname, TRUE); // yank into specif'd reg.
695 if (op_yank(oap, TRUE, FALSE) == OK) // yank without message
696 did_yank = TRUE;
697 }
698 else
699 reset_y_append(); // not appending to unnamed register
700
701 /*
702 * Put deleted text into register 1 and shift number registers if the
703 * delete contains a line break, or when using a specific operator (Vi
704 * compatible)
705 * Use the register name from before adjust_clip_reg() may have
706 * changed it.
707 */
708 if (oap->motion_type == MLINE || oap->line_count > 1
709 || oap->use_reg_one)
710 {
711 shift_delete_registers();
712 if (op_yank(oap, TRUE, FALSE) == OK)
713 did_yank = TRUE;
714 }
715
716 // Yank into small delete register when no named register specified
717 // and the delete is within one line.
718 if ((
719 #ifdef FEAT_CLIPBOARD
720 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') ||
721 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') ||
722 #endif
723 oap->regname == 0) && oap->motion_type != MLINE
724 && oap->line_count == 1)
725 {
726 oap->regname = '-';
727 get_yank_register(oap->regname, TRUE);
728 if (op_yank(oap, TRUE, FALSE) == OK)
729 did_yank = TRUE;
730 oap->regname = 0;
731 }
732
733 /*
734 * If there's too much stuff to fit in the yank register, then get a
735 * confirmation before doing the delete. This is crude, but simple.
736 * And it avoids doing a delete of something we can't put back if we
737 * want.
738 */
739 if (!did_yank)
740 {
741 int msg_silent_save = msg_silent;
742
743 msg_silent = 0; // must display the prompt
744 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
745 msg_silent = msg_silent_save;
746 if (n != 'y')
747 {
748 emsg(_(e_abort));
749 return FAIL;
750 }
751 }
752
753 #if defined(FEAT_EVAL)
754 if (did_yank && has_textyankpost())
755 yank_do_autocmd(oap, get_y_current());
756 #endif
757 }
758
759 /*
760 * block mode delete
761 */
762 if (oap->block_mode)
763 {
764 if (u_save((linenr_T)(oap->start.lnum - 1),
765 (linenr_T)(oap->end.lnum + 1)) == FAIL)
766 return FAIL;
767
768 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
769 {
770 block_prep(oap, &bd, lnum, TRUE);
771 if (bd.textlen == 0) // nothing to delete
772 continue;
773
774 // Adjust cursor position for tab replaced by spaces and 'lbr'.
775 if (lnum == curwin->w_cursor.lnum)
776 {
777 curwin->w_cursor.col = bd.textcol + bd.startspaces;
778 curwin->w_cursor.coladd = 0;
779 }
780
781 // "n" == number of chars deleted
782 // If we delete a TAB, it may be replaced by several characters.
783 // Thus the number of characters may increase!
784 n = bd.textlen - bd.startspaces - bd.endspaces;
785 oldp = ml_get(lnum);
786 newp = alloc(STRLEN(oldp) + 1 - n);
787 if (newp == NULL)
788 continue;
789 // copy up to deleted part
790 mch_memmove(newp, oldp, (size_t)bd.textcol);
791 // insert spaces
792 vim_memset(newp + bd.textcol, ' ',
793 (size_t)(bd.startspaces + bd.endspaces));
794 // copy the part after the deleted part
795 oldp += bd.textcol + bd.textlen;
796 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp);
797 // replace the line
798 ml_replace(lnum, newp, FALSE);
799
800 #ifdef FEAT_PROP_POPUP
801 if (curbuf->b_has_textprop && n != 0)
802 adjust_prop_columns(lnum, bd.textcol, -n, 0);
803 #endif
804 }
805
806 check_cursor_col();
807 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
808 oap->end.lnum + 1, 0L);
809 oap->line_count = 0; // no lines deleted
810 }
811 else if (oap->motion_type == MLINE)
812 {
813 if (oap->op_type == OP_CHANGE)
814 {
815 // Delete the lines except the first one. Temporarily move the
816 // cursor to the next line. Save the current line number, if the
817 // last line is deleted it may be changed.
818 if (oap->line_count > 1)
819 {
820 lnum = curwin->w_cursor.lnum;
821 ++curwin->w_cursor.lnum;
822 del_lines((long)(oap->line_count - 1), TRUE);
823 curwin->w_cursor.lnum = lnum;
824 }
825 if (u_save_cursor() == FAIL)
826 return FAIL;
827 if (curbuf->b_p_ai) // don't delete indent
828 {
829 beginline(BL_WHITE); // cursor on first non-white
830 did_ai = TRUE; // delete the indent when ESC hit
831 ai_col = curwin->w_cursor.col;
832 }
833 else
834 beginline(0); // cursor in column 0
835 truncate_line(FALSE); // delete the rest of the line
836 // leave cursor past last char in line
837 if (oap->line_count > 1)
838 u_clearline(); // "U" command not possible after "2cc"
839 }
840 else
841 {
842 del_lines(oap->line_count, TRUE);
843 beginline(BL_WHITE | BL_FIX);
844 u_clearline(); // "U" command not possible after "dd"
845 }
846 }
847 else
848 {
849 if (virtual_op)
850 {
851 int endcol = 0;
852
853 // For virtualedit: break the tabs that are partly included.
854 if (gchar_pos(&oap->start) == '\t')
855 {
856 if (u_save_cursor() == FAIL) // save first line for undo
857 return FAIL;
858 if (oap->line_count == 1)
859 endcol = getviscol2(oap->end.col, oap->end.coladd);
860 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
861 oap->start = curwin->w_cursor;
862 if (oap->line_count == 1)
863 {
864 coladvance(endcol);
865 oap->end.col = curwin->w_cursor.col;
866 oap->end.coladd = curwin->w_cursor.coladd;
867 curwin->w_cursor = oap->start;
868 }
869 }
870
871 // Break a tab only when it's included in the area.
872 if (gchar_pos(&oap->end) == '\t'
873 && (int)oap->end.coladd < oap->inclusive)
874 {
875 // save last line for undo
876 if (u_save((linenr_T)(oap->end.lnum - 1),
877 (linenr_T)(oap->end.lnum + 1)) == FAIL)
878 return FAIL;
879 curwin->w_cursor = oap->end;
880 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
881 oap->end = curwin->w_cursor;
882 curwin->w_cursor = oap->start;
883 }
884 if (has_mbyte)
885 mb_adjust_opend(oap);
886 }
887
888 if (oap->line_count == 1) // delete characters within one line
889 {
890 if (u_save_cursor() == FAIL) // save line for undo
891 return FAIL;
892
893 // if 'cpoptions' contains '$', display '$' at end of change
894 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
895 && oap->op_type == OP_CHANGE
896 && oap->end.lnum == curwin->w_cursor.lnum
897 && !oap->is_VIsual)
898 display_dollar(oap->end.col - !oap->inclusive);
899
900 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
901
902 if (virtual_op)
903 {
904 // fix up things for virtualedit-delete:
905 // break the tabs which are going to get in our way
906 char_u *curline = ml_get_curline();
907 int len = (int)STRLEN(curline);
908
909 if (oap->end.coladd != 0
910 && (int)oap->end.col >= len - 1
911 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
912 n++;
913 // Delete at least one char (e.g, when on a control char).
914 if (n == 0 && oap->start.coladd != oap->end.coladd)
915 n = 1;
916
917 // When deleted a char in the line, reset coladd.
918 if (gchar_cursor() != NUL)
919 curwin->w_cursor.coladd = 0;
920 }
921 (void)del_bytes((long)n, !virtual_op,
922 oap->op_type == OP_DELETE && !oap->is_VIsual);
923 }
924 else // delete characters between lines
925 {
926 pos_T curpos;
927
928 // save deleted and changed lines for undo
929 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
930 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
931 return FAIL;
932
933 truncate_line(TRUE); // delete from cursor to end of line
934
935 curpos = curwin->w_cursor; // remember curwin->w_cursor
936 ++curwin->w_cursor.lnum;
937 del_lines((long)(oap->line_count - 2), FALSE);
938
939 // delete from start of line until op_end
940 n = (oap->end.col + 1 - !oap->inclusive);
941 curwin->w_cursor.col = 0;
942 (void)del_bytes((long)n, !virtual_op,
943 oap->op_type == OP_DELETE && !oap->is_VIsual);
944 curwin->w_cursor = curpos; // restore curwin->w_cursor
945 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
946 }
947 if (oap->op_type == OP_DELETE)
948 auto_format(FALSE, TRUE);
949 }
950
951 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
952
953 setmarks:
954 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
955 {
956 if (oap->block_mode)
957 {
958 curbuf->b_op_end.lnum = oap->end.lnum;
959 curbuf->b_op_end.col = oap->start.col;
960 }
961 else
962 curbuf->b_op_end = oap->start;
963 curbuf->b_op_start = oap->start;
964 }
965
966 return OK;
967 }
968
969 /*
970 * Adjust end of operating area for ending on a multi-byte character.
971 * Used for deletion.
972 */
973 static void
mb_adjust_opend(oparg_T * oap)974 mb_adjust_opend(oparg_T *oap)
975 {
976 char_u *p;
977
978 if (oap->inclusive)
979 {
980 p = ml_get(oap->end.lnum);
981 oap->end.col += mb_tail_off(p, p + oap->end.col);
982 }
983 }
984
985 /*
986 * Replace the character under the cursor with "c".
987 * This takes care of multi-byte characters.
988 */
989 static void
replace_character(int c)990 replace_character(int c)
991 {
992 int n = State;
993
994 State = REPLACE;
995 ins_char(c);
996 State = n;
997 // Backup to the replaced character.
998 dec_cursor();
999 }
1000
1001 /*
1002 * Replace a whole area with one character.
1003 */
1004 int
op_replace(oparg_T * oap,int c)1005 op_replace(oparg_T *oap, int c)
1006 {
1007 int n, numc;
1008 int num_chars;
1009 char_u *newp, *oldp;
1010 size_t oldlen;
1011 struct block_def bd;
1012 char_u *after_p = NULL;
1013 int had_ctrl_v_cr = FALSE;
1014
1015 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1016 return OK; // nothing to do
1017
1018 if (c == REPLACE_CR_NCHAR)
1019 {
1020 had_ctrl_v_cr = TRUE;
1021 c = CAR;
1022 }
1023 else if (c == REPLACE_NL_NCHAR)
1024 {
1025 had_ctrl_v_cr = TRUE;
1026 c = NL;
1027 }
1028
1029 if (has_mbyte)
1030 mb_adjust_opend(oap);
1031
1032 if (u_save((linenr_T)(oap->start.lnum - 1),
1033 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1034 return FAIL;
1035
1036 /*
1037 * block mode replace
1038 */
1039 if (oap->block_mode)
1040 {
1041 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1042 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1043 {
1044 curwin->w_cursor.col = 0; // make sure cursor position is valid
1045 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1046 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1047 continue; // nothing to replace
1048
1049 // n == number of extra chars required
1050 // If we split a TAB, it may be replaced by several characters.
1051 // Thus the number of characters may increase!
1052 // If the range starts in virtual space, count the initial
1053 // coladd offset as part of "startspaces"
1054 if (virtual_op && bd.is_short && *bd.textstart == NUL)
1055 {
1056 pos_T vpos;
1057
1058 vpos.lnum = curwin->w_cursor.lnum;
1059 getvpos(&vpos, oap->start_vcol);
1060 bd.startspaces += vpos.coladd;
1061 n = bd.startspaces;
1062 }
1063 else
1064 // allow for pre spaces
1065 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1066
1067 // allow for post spp
1068 n += (bd.endspaces
1069 && !bd.is_oneChar
1070 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
1071 // Figure out how many characters to replace.
1072 numc = oap->end_vcol - oap->start_vcol + 1;
1073 if (bd.is_short && (!virtual_op || bd.is_MAX))
1074 numc -= (oap->end_vcol - bd.end_vcol) + 1;
1075
1076 // A double-wide character can be replaced only up to half the
1077 // times.
1078 if ((*mb_char2cells)(c) > 1)
1079 {
1080 if ((numc & 1) && !bd.is_short)
1081 {
1082 ++bd.endspaces;
1083 ++n;
1084 }
1085 numc = numc / 2;
1086 }
1087
1088 // Compute bytes needed, move character count to num_chars.
1089 num_chars = numc;
1090 numc *= (*mb_char2len)(c);
1091 // oldlen includes textlen, so don't double count
1092 n += numc - bd.textlen;
1093
1094 oldp = ml_get_curline();
1095 oldlen = STRLEN(oldp);
1096 newp = alloc(oldlen + 1 + n);
1097 if (newp == NULL)
1098 continue;
1099 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
1100 // copy up to deleted part
1101 mch_memmove(newp, oldp, (size_t)bd.textcol);
1102 oldp += bd.textcol + bd.textlen;
1103 // insert pre-spaces
1104 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces);
1105 // insert replacement chars CHECK FOR ALLOCATED SPACE
1106 // REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR
1107 // literally.
1108 if (had_ctrl_v_cr || (c != '\r' && c != '\n'))
1109 {
1110 if (has_mbyte)
1111 {
1112 n = (int)STRLEN(newp);
1113 while (--num_chars >= 0)
1114 n += (*mb_char2bytes)(c, newp + n);
1115 }
1116 else
1117 vim_memset(newp + STRLEN(newp), c, (size_t)numc);
1118 if (!bd.is_short)
1119 {
1120 // insert post-spaces
1121 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces);
1122 // copy the part after the changed part
1123 STRMOVE(newp + STRLEN(newp), oldp);
1124 }
1125 }
1126 else
1127 {
1128 // Replacing with \r or \n means splitting the line.
1129 after_p = alloc(oldlen + 1 + n - STRLEN(newp));
1130 if (after_p != NULL)
1131 STRMOVE(after_p, oldp);
1132 }
1133 // replace the line
1134 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
1135 if (after_p != NULL)
1136 {
1137 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE);
1138 appended_lines_mark(curwin->w_cursor.lnum, 1L);
1139 oap->end.lnum++;
1140 vim_free(after_p);
1141 }
1142 }
1143 }
1144 else
1145 {
1146 /*
1147 * MCHAR and MLINE motion replace.
1148 */
1149 if (oap->motion_type == MLINE)
1150 {
1151 oap->start.col = 0;
1152 curwin->w_cursor.col = 0;
1153 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1154 if (oap->end.col)
1155 --oap->end.col;
1156 }
1157 else if (!oap->inclusive)
1158 dec(&(oap->end));
1159
1160 while (LTOREQ_POS(curwin->w_cursor, oap->end))
1161 {
1162 n = gchar_cursor();
1163 if (n != NUL)
1164 {
1165 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
1166 {
1167 // This is slow, but it handles replacing a single-byte
1168 // with a multi-byte and the other way around.
1169 if (curwin->w_cursor.lnum == oap->end.lnum)
1170 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
1171 replace_character(c);
1172 }
1173 else
1174 {
1175 if (n == TAB)
1176 {
1177 int end_vcol = 0;
1178
1179 if (curwin->w_cursor.lnum == oap->end.lnum)
1180 {
1181 // oap->end has to be recalculated when
1182 // the tab breaks
1183 end_vcol = getviscol2(oap->end.col,
1184 oap->end.coladd);
1185 }
1186 coladvance_force(getviscol());
1187 if (curwin->w_cursor.lnum == oap->end.lnum)
1188 getvpos(&oap->end, end_vcol);
1189 }
1190 PBYTE(curwin->w_cursor, c);
1191 }
1192 }
1193 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
1194 {
1195 int virtcols = oap->end.coladd;
1196
1197 if (curwin->w_cursor.lnum == oap->start.lnum
1198 && oap->start.col == oap->end.col && oap->start.coladd)
1199 virtcols -= oap->start.coladd;
1200
1201 // oap->end has been trimmed so it's effectively inclusive;
1202 // as a result an extra +1 must be counted so we don't
1203 // trample the NUL byte.
1204 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
1205 curwin->w_cursor.col -= (virtcols + 1);
1206 for (; virtcols >= 0; virtcols--)
1207 {
1208 if ((*mb_char2len)(c) > 1)
1209 replace_character(c);
1210 else
1211 PBYTE(curwin->w_cursor, c);
1212 if (inc(&curwin->w_cursor) == -1)
1213 break;
1214 }
1215 }
1216
1217 // Advance to next character, stop at the end of the file.
1218 if (inc_cursor() == -1)
1219 break;
1220 }
1221 }
1222
1223 curwin->w_cursor = oap->start;
1224 check_cursor();
1225 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
1226
1227 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
1228 {
1229 // Set "'[" and "']" marks.
1230 curbuf->b_op_start = oap->start;
1231 curbuf->b_op_end = oap->end;
1232 }
1233
1234 return OK;
1235 }
1236
1237 static int swapchars(int op_type, pos_T *pos, int length);
1238
1239 /*
1240 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
1241 */
1242 static void
op_tilde(oparg_T * oap)1243 op_tilde(oparg_T *oap)
1244 {
1245 pos_T pos;
1246 struct block_def bd;
1247 int did_change = FALSE;
1248
1249 if (u_save((linenr_T)(oap->start.lnum - 1),
1250 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1251 return;
1252
1253 pos = oap->start;
1254 if (oap->block_mode) // Visual block mode
1255 {
1256 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
1257 {
1258 int one_change;
1259
1260 block_prep(oap, &bd, pos.lnum, FALSE);
1261 pos.col = bd.textcol;
1262 one_change = swapchars(oap->op_type, &pos, bd.textlen);
1263 did_change |= one_change;
1264
1265 #ifdef FEAT_NETBEANS_INTG
1266 if (netbeans_active() && one_change)
1267 {
1268 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1269
1270 netbeans_removed(curbuf, pos.lnum, bd.textcol,
1271 (long)bd.textlen);
1272 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
1273 &ptr[bd.textcol], bd.textlen);
1274 }
1275 #endif
1276 }
1277 if (did_change)
1278 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
1279 }
1280 else // not block mode
1281 {
1282 if (oap->motion_type == MLINE)
1283 {
1284 oap->start.col = 0;
1285 pos.col = 0;
1286 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1287 if (oap->end.col)
1288 --oap->end.col;
1289 }
1290 else if (!oap->inclusive)
1291 dec(&(oap->end));
1292
1293 if (pos.lnum == oap->end.lnum)
1294 did_change = swapchars(oap->op_type, &pos,
1295 oap->end.col - pos.col + 1);
1296 else
1297 for (;;)
1298 {
1299 did_change |= swapchars(oap->op_type, &pos,
1300 pos.lnum == oap->end.lnum ? oap->end.col + 1:
1301 (int)STRLEN(ml_get_pos(&pos)));
1302 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1)
1303 break;
1304 }
1305 if (did_change)
1306 {
1307 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
1308 0L);
1309 #ifdef FEAT_NETBEANS_INTG
1310 if (netbeans_active() && did_change)
1311 {
1312 char_u *ptr;
1313 int count;
1314
1315 pos = oap->start;
1316 while (pos.lnum < oap->end.lnum)
1317 {
1318 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1319 count = (int)STRLEN(ptr) - pos.col;
1320 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
1321 netbeans_inserted(curbuf, pos.lnum, pos.col,
1322 &ptr[pos.col], count);
1323 pos.col = 0;
1324 pos.lnum++;
1325 }
1326 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
1327 count = oap->end.col - pos.col + 1;
1328 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
1329 netbeans_inserted(curbuf, pos.lnum, pos.col,
1330 &ptr[pos.col], count);
1331 }
1332 #endif
1333 }
1334 }
1335
1336 if (!did_change && oap->is_VIsual)
1337 // No change: need to remove the Visual selection
1338 redraw_curbuf_later(INVERTED);
1339
1340 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
1341 {
1342 // Set '[ and '] marks.
1343 curbuf->b_op_start = oap->start;
1344 curbuf->b_op_end = oap->end;
1345 }
1346
1347 if (oap->line_count > p_report)
1348 smsg(NGETTEXT("%ld line changed", "%ld lines changed",
1349 oap->line_count), oap->line_count);
1350 }
1351
1352 /*
1353 * Invoke swapchar() on "length" bytes at position "pos".
1354 * "pos" is advanced to just after the changed characters.
1355 * "length" is rounded up to include the whole last multi-byte character.
1356 * Also works correctly when the number of bytes changes.
1357 * Returns TRUE if some character was changed.
1358 */
1359 static int
swapchars(int op_type,pos_T * pos,int length)1360 swapchars(int op_type, pos_T *pos, int length)
1361 {
1362 int todo;
1363 int did_change = 0;
1364
1365 for (todo = length; todo > 0; --todo)
1366 {
1367 if (has_mbyte)
1368 {
1369 int len = (*mb_ptr2len)(ml_get_pos(pos));
1370
1371 // we're counting bytes, not characters
1372 if (len > 0)
1373 todo -= len - 1;
1374 }
1375 did_change |= swapchar(op_type, pos);
1376 if (inc(pos) == -1) // at end of file
1377 break;
1378 }
1379 return did_change;
1380 }
1381
1382 /*
1383 * If op_type == OP_UPPER: make uppercase,
1384 * if op_type == OP_LOWER: make lowercase,
1385 * if op_type == OP_ROT13: do rot13 encoding,
1386 * else swap case of character at 'pos'
1387 * returns TRUE when something actually changed.
1388 */
1389 int
swapchar(int op_type,pos_T * pos)1390 swapchar(int op_type, pos_T *pos)
1391 {
1392 int c;
1393 int nc;
1394
1395 c = gchar_pos(pos);
1396
1397 // Only do rot13 encoding for ASCII characters.
1398 if (c >= 0x80 && op_type == OP_ROT13)
1399 return FALSE;
1400
1401 if (op_type == OP_UPPER && c == 0xdf
1402 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0))
1403 {
1404 pos_T sp = curwin->w_cursor;
1405
1406 // Special handling of German sharp s: change to "SS".
1407 curwin->w_cursor = *pos;
1408 del_char(FALSE);
1409 ins_char('S');
1410 ins_char('S');
1411 curwin->w_cursor = sp;
1412 inc(pos);
1413 }
1414
1415 if (enc_dbcs != 0 && c >= 0x100) // No lower/uppercase letter
1416 return FALSE;
1417 nc = c;
1418 if (MB_ISLOWER(c))
1419 {
1420 if (op_type == OP_ROT13)
1421 nc = ROT13(c, 'a');
1422 else if (op_type != OP_LOWER)
1423 nc = MB_TOUPPER(c);
1424 }
1425 else if (MB_ISUPPER(c))
1426 {
1427 if (op_type == OP_ROT13)
1428 nc = ROT13(c, 'A');
1429 else if (op_type != OP_UPPER)
1430 nc = MB_TOLOWER(c);
1431 }
1432 if (nc != c)
1433 {
1434 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
1435 {
1436 pos_T sp = curwin->w_cursor;
1437
1438 curwin->w_cursor = *pos;
1439 // don't use del_char(), it also removes composing chars
1440 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE);
1441 ins_char(nc);
1442 curwin->w_cursor = sp;
1443 }
1444 else
1445 PBYTE(*pos, nc);
1446 return TRUE;
1447 }
1448 return FALSE;
1449 }
1450
1451 /*
1452 * op_insert - Insert and append operators for Visual mode.
1453 */
1454 void
op_insert(oparg_T * oap,long count1)1455 op_insert(oparg_T *oap, long count1)
1456 {
1457 long ins_len, pre_textlen = 0;
1458 char_u *firstline, *ins_text;
1459 colnr_T ind_pre = 0, ind_post;
1460 struct block_def bd;
1461 int i;
1462 pos_T t1;
1463 pos_T start_insert;
1464 // offset when cursor was moved in insert mode
1465 int offset = 0;
1466
1467 // edit() changes this - record it for OP_APPEND
1468 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1469
1470 // vis block is still marked. Get rid of it now.
1471 curwin->w_cursor.lnum = oap->start.lnum;
1472 update_screen(INVERTED);
1473
1474 if (oap->block_mode)
1475 {
1476 // When 'virtualedit' is used, need to insert the extra spaces before
1477 // doing block_prep(). When only "block" is used, virtual edit is
1478 // already disabled, but still need it when calling
1479 // coladvance_force().
1480 // coladvance_force() uses get_ve_flags() to get the 'virtualedit'
1481 // state for the current window. To override that state, we need to
1482 // set the window-local value of ve_flags rather than the global value.
1483 if (curwin->w_cursor.coladd > 0)
1484 {
1485 int old_ve_flags = curwin->w_ve_flags;
1486
1487 if (u_save_cursor() == FAIL)
1488 return;
1489
1490 curwin->w_ve_flags = VE_ALL;
1491 coladvance_force(oap->op_type == OP_APPEND
1492 ? oap->end_vcol + 1 : getviscol());
1493 if (oap->op_type == OP_APPEND)
1494 --curwin->w_cursor.col;
1495 curwin->w_ve_flags = old_ve_flags;
1496 }
1497 // Get the info about the block before entering the text
1498 block_prep(oap, &bd, oap->start.lnum, TRUE);
1499 // Get indent information
1500 ind_pre = (colnr_T)getwhitecols_curline();
1501 firstline = ml_get(oap->start.lnum) + bd.textcol;
1502
1503 if (oap->op_type == OP_APPEND)
1504 firstline += bd.textlen;
1505 pre_textlen = (long)STRLEN(firstline);
1506 }
1507
1508 if (oap->op_type == OP_APPEND)
1509 {
1510 if (oap->block_mode && curwin->w_cursor.coladd == 0)
1511 {
1512 // Move the cursor to the character right of the block.
1513 curwin->w_set_curswant = TRUE;
1514 while (*ml_get_cursor() != NUL
1515 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
1516 ++curwin->w_cursor.col;
1517 if (bd.is_short && !bd.is_MAX)
1518 {
1519 // First line was too short, make it longer and adjust the
1520 // values in "bd".
1521 if (u_save_cursor() == FAIL)
1522 return;
1523 for (i = 0; i < bd.endspaces; ++i)
1524 ins_char(' ');
1525 bd.textlen += bd.endspaces;
1526 }
1527 }
1528 else
1529 {
1530 curwin->w_cursor = oap->end;
1531 check_cursor_col();
1532
1533 // Works just like an 'i'nsert on the next character.
1534 if (!LINEEMPTY(curwin->w_cursor.lnum)
1535 && oap->start_vcol != oap->end_vcol)
1536 inc_cursor();
1537 }
1538 }
1539
1540 t1 = oap->start;
1541 start_insert = curwin->w_cursor;
1542 (void)edit(NUL, FALSE, (linenr_T)count1);
1543
1544 // When a tab was inserted, and the characters in front of the tab
1545 // have been converted to a tab as well, the column of the cursor
1546 // might have actually been reduced, so need to adjust here.
1547 if (t1.lnum == curbuf->b_op_start_orig.lnum
1548 && LT_POS(curbuf->b_op_start_orig, t1))
1549 oap->start = curbuf->b_op_start_orig;
1550
1551 // If user has moved off this line, we don't know what to do, so do
1552 // nothing.
1553 // Also don't repeat the insert when Insert mode ended with CTRL-C.
1554 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
1555 return;
1556
1557 if (oap->block_mode)
1558 {
1559 struct block_def bd2;
1560 int did_indent = FALSE;
1561 size_t len;
1562 int add;
1563
1564 // If indent kicked in, the firstline might have changed
1565 // but only do that, if the indent actually increased.
1566 ind_post = (colnr_T)getwhitecols_curline();
1567 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
1568 {
1569 bd.textcol += ind_post - ind_pre;
1570 bd.start_vcol += ind_post - ind_pre;
1571 did_indent = TRUE;
1572 }
1573
1574 // The user may have moved the cursor before inserting something, try
1575 // to adjust the block for that. But only do it, if the difference
1576 // does not come from indent kicking in.
1577 if (oap->start.lnum == curbuf->b_op_start_orig.lnum
1578 && !bd.is_MAX && !did_indent)
1579 {
1580 int t = getviscol2(curbuf->b_op_start_orig.col,
1581 curbuf->b_op_start_orig.coladd);
1582
1583 if (!bd.is_MAX)
1584 {
1585 if (oap->op_type == OP_INSERT
1586 && oap->start.col + oap->start.coladd
1587 != curbuf->b_op_start_orig.col
1588 + curbuf->b_op_start_orig.coladd)
1589 {
1590 oap->start.col = curbuf->b_op_start_orig.col;
1591 pre_textlen -= t - oap->start_vcol;
1592 oap->start_vcol = t;
1593 }
1594 else if (oap->op_type == OP_APPEND
1595 && oap->end.col + oap->end.coladd
1596 >= curbuf->b_op_start_orig.col
1597 + curbuf->b_op_start_orig.coladd)
1598 {
1599 oap->start.col = curbuf->b_op_start_orig.col;
1600 // reset pre_textlen to the value of OP_INSERT
1601 pre_textlen += bd.textlen;
1602 pre_textlen -= t - oap->start_vcol;
1603 oap->start_vcol = t;
1604 oap->op_type = OP_INSERT;
1605 }
1606 }
1607 else if (bd.is_MAX && oap->op_type == OP_APPEND)
1608 {
1609 // reset pre_textlen to the value of OP_INSERT
1610 pre_textlen += bd.textlen;
1611 pre_textlen -= t - oap->start_vcol;
1612 }
1613 }
1614
1615 /*
1616 * Spaces and tabs in the indent may have changed to other spaces and
1617 * tabs. Get the starting column again and correct the length.
1618 * Don't do this when "$" used, end-of-line will have changed.
1619 */
1620 block_prep(oap, &bd2, oap->start.lnum, TRUE);
1621 if (!bd.is_MAX || bd2.textlen < bd.textlen)
1622 {
1623 if (oap->op_type == OP_APPEND)
1624 {
1625 pre_textlen += bd2.textlen - bd.textlen;
1626 if (bd2.endspaces)
1627 --bd2.textlen;
1628 }
1629 bd.textcol = bd2.textcol;
1630 bd.textlen = bd2.textlen;
1631 }
1632
1633 /*
1634 * Subsequent calls to ml_get() flush the firstline data - take a
1635 * copy of the required string.
1636 */
1637 firstline = ml_get(oap->start.lnum);
1638 len = STRLEN(firstline);
1639 add = bd.textcol;
1640 if (oap->op_type == OP_APPEND)
1641 {
1642 add += bd.textlen;
1643 // account for pressing cursor in insert mode when '$' was used
1644 if (bd.is_MAX
1645 && (start_insert.lnum == Insstart.lnum
1646 && start_insert.col > Insstart.col))
1647 {
1648 offset = (start_insert.col - Insstart.col);
1649 add -= offset;
1650 if (oap->end_vcol > offset)
1651 oap->end_vcol -= (offset + 1);
1652 else
1653 // moved outside of the visual block, what to do?
1654 return;
1655 }
1656 }
1657 if ((size_t)add > len)
1658 firstline += len; // short line, point to the NUL
1659 else
1660 firstline += add;
1661 if (pre_textlen >= 0 && (ins_len =
1662 (long)STRLEN(firstline) - pre_textlen - offset) > 0)
1663 {
1664 ins_text = vim_strnsave(firstline, ins_len);
1665 if (ins_text != NULL)
1666 {
1667 // block handled here
1668 if (u_save(oap->start.lnum,
1669 (linenr_T)(oap->end.lnum + 1)) == OK)
1670 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
1671 &bd);
1672
1673 curwin->w_cursor.col = oap->start.col;
1674 check_cursor();
1675 vim_free(ins_text);
1676 }
1677 }
1678 }
1679 }
1680
1681 /*
1682 * op_change - handle a change operation
1683 *
1684 * return TRUE if edit() returns because of a CTRL-O command
1685 */
1686 int
op_change(oparg_T * oap)1687 op_change(oparg_T *oap)
1688 {
1689 colnr_T l;
1690 int retval;
1691 long offset;
1692 linenr_T linenr;
1693 long ins_len;
1694 long pre_textlen = 0;
1695 long pre_indent = 0;
1696 char_u *firstline;
1697 char_u *ins_text, *newp, *oldp;
1698 struct block_def bd;
1699
1700 l = oap->start.col;
1701 if (oap->motion_type == MLINE)
1702 {
1703 l = 0;
1704 #ifdef FEAT_SMARTINDENT
1705 if (!p_paste && curbuf->b_p_si
1706 # ifdef FEAT_CINDENT
1707 && !curbuf->b_p_cin
1708 # endif
1709 )
1710 can_si = TRUE; // It's like opening a new line, do si
1711 #endif
1712 }
1713
1714 // First delete the text in the region. In an empty buffer only need to
1715 // save for undo
1716 if (curbuf->b_ml.ml_flags & ML_EMPTY)
1717 {
1718 if (u_save_cursor() == FAIL)
1719 return FALSE;
1720 }
1721 else if (op_delete(oap) == FAIL)
1722 return FALSE;
1723
1724 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum)
1725 && !virtual_op)
1726 inc_cursor();
1727
1728 // check for still on same line (<CR> in inserted text meaningless)
1729 // skip blank lines too
1730 if (oap->block_mode)
1731 {
1732 // Add spaces before getting the current line length.
1733 if (virtual_op && (curwin->w_cursor.coladd > 0
1734 || gchar_cursor() == NUL))
1735 coladvance_force(getviscol());
1736 firstline = ml_get(oap->start.lnum);
1737 pre_textlen = (long)STRLEN(firstline);
1738 pre_indent = (long)getwhitecols(firstline);
1739 bd.textcol = curwin->w_cursor.col;
1740 }
1741
1742 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1743 if (oap->motion_type == MLINE)
1744 fix_indent();
1745 #endif
1746
1747 retval = edit(NUL, FALSE, (linenr_T)1);
1748
1749 /*
1750 * In Visual block mode, handle copying the new text to all lines of the
1751 * block.
1752 * Don't repeat the insert when Insert mode ended with CTRL-C.
1753 */
1754 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
1755 {
1756 // Auto-indenting may have changed the indent. If the cursor was past
1757 // the indent, exclude that indent change from the inserted text.
1758 firstline = ml_get(oap->start.lnum);
1759 if (bd.textcol > (colnr_T)pre_indent)
1760 {
1761 long new_indent = (long)getwhitecols(firstline);
1762
1763 pre_textlen += new_indent - pre_indent;
1764 bd.textcol += new_indent - pre_indent;
1765 }
1766
1767 ins_len = (long)STRLEN(firstline) - pre_textlen;
1768 if (ins_len > 0)
1769 {
1770 // Subsequent calls to ml_get() flush the firstline data - take a
1771 // copy of the inserted text.
1772 if ((ins_text = alloc(ins_len + 1)) != NULL)
1773 {
1774 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
1775 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
1776 linenr++)
1777 {
1778 block_prep(oap, &bd, linenr, TRUE);
1779 if (!bd.is_short || virtual_op)
1780 {
1781 pos_T vpos;
1782
1783 // If the block starts in virtual space, count the
1784 // initial coladd offset as part of "startspaces"
1785 if (bd.is_short)
1786 {
1787 vpos.lnum = linenr;
1788 (void)getvpos(&vpos, oap->start_vcol);
1789 }
1790 else
1791 vpos.coladd = 0;
1792 oldp = ml_get(linenr);
1793 newp = alloc(STRLEN(oldp) + vpos.coladd + ins_len + 1);
1794 if (newp == NULL)
1795 continue;
1796 // copy up to block start
1797 mch_memmove(newp, oldp, (size_t)bd.textcol);
1798 offset = bd.textcol;
1799 vim_memset(newp + offset, ' ', (size_t)vpos.coladd);
1800 offset += vpos.coladd;
1801 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
1802 offset += ins_len;
1803 oldp += bd.textcol;
1804 STRMOVE(newp + offset, oldp);
1805 ml_replace(linenr, newp, FALSE);
1806 }
1807 }
1808 check_cursor();
1809
1810 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
1811 }
1812 vim_free(ins_text);
1813 }
1814 }
1815 auto_format(FALSE, TRUE);
1816
1817 return retval;
1818 }
1819
1820 /*
1821 * When the cursor is on the NUL past the end of the line and it should not be
1822 * there move it left.
1823 */
1824 void
adjust_cursor_eol(void)1825 adjust_cursor_eol(void)
1826 {
1827 unsigned int cur_ve_flags = get_ve_flags();
1828
1829 if (curwin->w_cursor.col > 0
1830 && gchar_cursor() == NUL
1831 && (cur_ve_flags & VE_ONEMORE) == 0
1832 && !(restart_edit || (State & INSERT)))
1833 {
1834 // Put the cursor on the last character in the line.
1835 dec_cursor();
1836
1837 if (cur_ve_flags == VE_ALL)
1838 {
1839 colnr_T scol, ecol;
1840
1841 // Coladd is set to the width of the last character.
1842 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
1843 curwin->w_cursor.coladd = ecol - scol + 1;
1844 }
1845 }
1846 }
1847
1848 /*
1849 * If "process" is TRUE and the line begins with a comment leader (possibly
1850 * after some white space), return a pointer to the text after it. Put a boolean
1851 * value indicating whether the line ends with an unclosed comment in
1852 * "is_comment".
1853 * line - line to be processed,
1854 * process - if FALSE, will only check whether the line ends with an unclosed
1855 * comment,
1856 * include_space - whether to also skip space following the comment leader,
1857 * is_comment - will indicate whether the current line ends with an unclosed
1858 * comment.
1859 */
1860 char_u *
skip_comment(char_u * line,int process,int include_space,int * is_comment)1861 skip_comment(
1862 char_u *line,
1863 int process,
1864 int include_space,
1865 int *is_comment)
1866 {
1867 char_u *comment_flags = NULL;
1868 int lead_len;
1869 int leader_offset = get_last_leader_offset(line, &comment_flags);
1870
1871 *is_comment = FALSE;
1872 if (leader_offset != -1)
1873 {
1874 // Let's check whether the line ends with an unclosed comment.
1875 // If the last comment leader has COM_END in flags, there's no comment.
1876 while (*comment_flags)
1877 {
1878 if (*comment_flags == COM_END
1879 || *comment_flags == ':')
1880 break;
1881 ++comment_flags;
1882 }
1883 if (*comment_flags != COM_END)
1884 *is_comment = TRUE;
1885 }
1886
1887 if (process == FALSE)
1888 return line;
1889
1890 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space);
1891
1892 if (lead_len == 0)
1893 return line;
1894
1895 // Find:
1896 // - COM_END,
1897 // - colon,
1898 // whichever comes first.
1899 while (*comment_flags)
1900 {
1901 if (*comment_flags == COM_END
1902 || *comment_flags == ':')
1903 break;
1904 ++comment_flags;
1905 }
1906
1907 // If we found a colon, it means that we are not processing a line
1908 // starting with a closing part of a three-part comment. That's good,
1909 // because we don't want to remove those as this would be annoying.
1910 if (*comment_flags == ':' || *comment_flags == NUL)
1911 line += lead_len;
1912
1913 return line;
1914 }
1915
1916 /*
1917 * Join 'count' lines (minimal 2) at cursor position.
1918 * When "save_undo" is TRUE save lines for undo first.
1919 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment
1920 * leaders should not be removed.
1921 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected
1922 * to set those marks.
1923 *
1924 * return FAIL for failure, OK otherwise
1925 */
1926 int
do_join(long count,int insert_space,int save_undo,int use_formatoptions UNUSED,int setmark)1927 do_join(
1928 long count,
1929 int insert_space,
1930 int save_undo,
1931 int use_formatoptions UNUSED,
1932 int setmark)
1933 {
1934 char_u *curr = NULL;
1935 char_u *curr_start = NULL;
1936 char_u *cend;
1937 char_u *newp;
1938 size_t newp_len;
1939 char_u *spaces; // number of spaces inserted before a line
1940 int endcurr1 = NUL;
1941 int endcurr2 = NUL;
1942 int currsize = 0; // size of the current line
1943 int sumsize = 0; // size of the long new line
1944 linenr_T t;
1945 colnr_T col = 0;
1946 int ret = OK;
1947 int *comments = NULL;
1948 int remove_comments = (use_formatoptions == TRUE)
1949 && has_format_option(FO_REMOVE_COMS);
1950 int prev_was_comment;
1951 #ifdef FEAT_PROP_POPUP
1952 int propcount = 0; // number of props over all joined lines
1953 int props_remaining;
1954 #endif
1955
1956 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1957 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
1958 return FAIL;
1959
1960 // Allocate an array to store the number of spaces inserted before each
1961 // line. We will use it to pre-compute the length of the new line and the
1962 // proper placement of each original line in the new one.
1963 spaces = lalloc_clear(count, TRUE);
1964 if (spaces == NULL)
1965 return FAIL;
1966 if (remove_comments)
1967 {
1968 comments = lalloc_clear(count * sizeof(int), TRUE);
1969 if (comments == NULL)
1970 {
1971 vim_free(spaces);
1972 return FAIL;
1973 }
1974 }
1975
1976 /*
1977 * Don't move anything yet, just compute the final line length
1978 * and setup the array of space strings lengths
1979 * This loops forward over the joined lines.
1980 */
1981 for (t = 0; t < count; ++t)
1982 {
1983 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t));
1984 #ifdef FEAT_PROP_POPUP
1985 propcount += count_props((linenr_T) (curwin->w_cursor.lnum + t), t > 0);
1986 #endif
1987 if (t == 0 && setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
1988 {
1989 // Set the '[ mark.
1990 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum;
1991 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr);
1992 }
1993 if (remove_comments)
1994 {
1995 // We don't want to remove the comment leader if the
1996 // previous line is not a comment.
1997 if (t > 0 && prev_was_comment)
1998 {
1999
2000 char_u *new_curr = skip_comment(curr, TRUE, insert_space,
2001 &prev_was_comment);
2002 comments[t] = (int)(new_curr - curr);
2003 curr = new_curr;
2004 }
2005 else
2006 curr = skip_comment(curr, FALSE, insert_space,
2007 &prev_was_comment);
2008 }
2009
2010 if (insert_space && t > 0)
2011 {
2012 curr = skipwhite(curr);
2013 if (*curr != NUL && *curr != ')'
2014 && sumsize != 0 && endcurr1 != TAB
2015 && (!has_format_option(FO_MBYTE_JOIN)
2016 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100))
2017 && (!has_format_option(FO_MBYTE_JOIN2)
2018 || (mb_ptr2char(curr) < 0x100
2019 && !(enc_utf8 && utf_eat_space(endcurr1)))
2020 || (endcurr1 < 0x100
2021 && !(enc_utf8 && utf_eat_space(mb_ptr2char(curr)))))
2022 )
2023 {
2024 // don't add a space if the line is ending in a space
2025 if (endcurr1 == ' ')
2026 endcurr1 = endcurr2;
2027 else
2028 ++spaces[t];
2029 // extra space when 'joinspaces' set and line ends in '.'
2030 if ( p_js
2031 && (endcurr1 == '.'
2032 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
2033 && (endcurr1 == '?' || endcurr1 == '!'))))
2034 ++spaces[t];
2035 }
2036 }
2037 currsize = (int)STRLEN(curr);
2038 sumsize += currsize + spaces[t];
2039 endcurr1 = endcurr2 = NUL;
2040 if (insert_space && currsize > 0)
2041 {
2042 if (has_mbyte)
2043 {
2044 cend = curr + currsize;
2045 MB_PTR_BACK(curr, cend);
2046 endcurr1 = (*mb_ptr2char)(cend);
2047 if (cend > curr)
2048 {
2049 MB_PTR_BACK(curr, cend);
2050 endcurr2 = (*mb_ptr2char)(cend);
2051 }
2052 }
2053 else
2054 {
2055 endcurr1 = *(curr + currsize - 1);
2056 if (currsize > 1)
2057 endcurr2 = *(curr + currsize - 2);
2058 }
2059 }
2060 line_breakcheck();
2061 if (got_int)
2062 {
2063 ret = FAIL;
2064 goto theend;
2065 }
2066 }
2067
2068 // store the column position before last line
2069 col = sumsize - currsize - spaces[count - 1];
2070
2071 // allocate the space for the new line
2072 newp_len = sumsize + 1;
2073 #ifdef FEAT_PROP_POPUP
2074 newp_len += propcount * sizeof(textprop_T);
2075 #endif
2076 newp = alloc(newp_len);
2077 if (newp == NULL)
2078 {
2079 ret = FAIL;
2080 goto theend;
2081 }
2082 cend = newp + sumsize;
2083 *cend = 0;
2084
2085 /*
2086 * Move affected lines to the new long one.
2087 * This loops backwards over the joined lines, including the original line.
2088 *
2089 * Move marks from each deleted line to the joined line, adjusting the
2090 * column. This is not Vi compatible, but Vi deletes the marks, thus that
2091 * should not really be a problem.
2092 */
2093 #ifdef FEAT_PROP_POPUP
2094 props_remaining = propcount;
2095 #endif
2096 for (t = count - 1; ; --t)
2097 {
2098 int spaces_removed;
2099
2100 cend -= currsize;
2101 mch_memmove(cend, curr, (size_t)currsize);
2102
2103 if (spaces[t] > 0)
2104 {
2105 cend -= spaces[t];
2106 vim_memset(cend, ' ', (size_t)(spaces[t]));
2107 }
2108
2109 // If deleting more spaces than adding, the cursor moves no more than
2110 // what is added if it is inside these spaces.
2111 spaces_removed = (curr - curr_start) - spaces[t];
2112
2113 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
2114 (long)(cend - newp - spaces_removed), spaces_removed);
2115 #ifdef FEAT_PROP_POPUP
2116 prepend_joined_props(newp + sumsize + 1, propcount, &props_remaining,
2117 curwin->w_cursor.lnum + t, t == count - 1,
2118 (long)(cend - newp), spaces_removed);
2119 #endif
2120
2121 if (t == 0)
2122 break;
2123 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
2124 if (remove_comments)
2125 curr += comments[t - 1];
2126 if (insert_space && t > 1)
2127 curr = skipwhite(curr);
2128 currsize = (int)STRLEN(curr);
2129 }
2130
2131 ml_replace_len(curwin->w_cursor.lnum, newp, (colnr_T)newp_len, TRUE, FALSE);
2132
2133 if (setmark && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
2134 {
2135 // Set the '] mark.
2136 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum;
2137 curwin->w_buffer->b_op_end.col = (colnr_T)sumsize;
2138 }
2139
2140 // Only report the change in the first line here, del_lines() will report
2141 // the deleted line.
2142 changed_lines(curwin->w_cursor.lnum, currsize,
2143 curwin->w_cursor.lnum + 1, 0L);
2144 /*
2145 * Delete following lines. To do this we move the cursor there
2146 * briefly, and then move it back. After del_lines() the cursor may
2147 * have moved up (last line deleted), so the current lnum is kept in t.
2148 */
2149 t = curwin->w_cursor.lnum;
2150 ++curwin->w_cursor.lnum;
2151 del_lines(count - 1, FALSE);
2152 curwin->w_cursor.lnum = t;
2153
2154 /*
2155 * Set the cursor column:
2156 * Vi compatible: use the column of the first join
2157 * vim: use the column of the last join
2158 */
2159 curwin->w_cursor.col =
2160 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col);
2161 check_cursor_col();
2162
2163 curwin->w_cursor.coladd = 0;
2164 curwin->w_set_curswant = TRUE;
2165
2166 theend:
2167 vim_free(spaces);
2168 if (remove_comments)
2169 vim_free(comments);
2170 return ret;
2171 }
2172
2173 /*
2174 * prepare a few things for block mode yank/delete/tilde
2175 *
2176 * for delete:
2177 * - textlen includes the first/last char to be (partly) deleted
2178 * - start/endspaces is the number of columns that are taken by the
2179 * first/last deleted char minus the number of columns that have to be
2180 * deleted.
2181 * for yank and tilde:
2182 * - textlen includes the first/last char to be wholly yanked
2183 * - start/endspaces is the number of columns of the first/last yanked char
2184 * that are to be yanked.
2185 */
2186 void
block_prep(oparg_T * oap,struct block_def * bdp,linenr_T lnum,int is_del)2187 block_prep(
2188 oparg_T *oap,
2189 struct block_def *bdp,
2190 linenr_T lnum,
2191 int is_del)
2192 {
2193 int incr = 0;
2194 char_u *pend;
2195 char_u *pstart;
2196 char_u *line;
2197 char_u *prev_pstart;
2198 char_u *prev_pend;
2199 #ifdef FEAT_LINEBREAK
2200 int lbr_saved = curwin->w_p_lbr;
2201
2202 // Avoid a problem with unwanted linebreaks in block mode.
2203 curwin->w_p_lbr = FALSE;
2204 #endif
2205 bdp->startspaces = 0;
2206 bdp->endspaces = 0;
2207 bdp->textlen = 0;
2208 bdp->start_vcol = 0;
2209 bdp->end_vcol = 0;
2210 bdp->is_short = FALSE;
2211 bdp->is_oneChar = FALSE;
2212 bdp->pre_whitesp = 0;
2213 bdp->pre_whitesp_c = 0;
2214 bdp->end_char_vcols = 0;
2215 bdp->start_char_vcols = 0;
2216
2217 line = ml_get(lnum);
2218 pstart = line;
2219 prev_pstart = line;
2220 while (bdp->start_vcol < oap->start_vcol && *pstart)
2221 {
2222 // Count a tab for what it's worth (if list mode not on)
2223 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol);
2224 bdp->start_vcol += incr;
2225 if (VIM_ISWHITE(*pstart))
2226 {
2227 bdp->pre_whitesp += incr;
2228 bdp->pre_whitesp_c++;
2229 }
2230 else
2231 {
2232 bdp->pre_whitesp = 0;
2233 bdp->pre_whitesp_c = 0;
2234 }
2235 prev_pstart = pstart;
2236 MB_PTR_ADV(pstart);
2237 }
2238 bdp->start_char_vcols = incr;
2239 if (bdp->start_vcol < oap->start_vcol) // line too short
2240 {
2241 bdp->end_vcol = bdp->start_vcol;
2242 bdp->is_short = TRUE;
2243 if (!is_del || oap->op_type == OP_APPEND)
2244 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
2245 }
2246 else
2247 {
2248 // notice: this converts partly selected Multibyte characters to
2249 // spaces, too.
2250 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
2251 if (is_del && bdp->startspaces)
2252 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
2253 pend = pstart;
2254 bdp->end_vcol = bdp->start_vcol;
2255 if (bdp->end_vcol > oap->end_vcol) // it's all in one character
2256 {
2257 bdp->is_oneChar = TRUE;
2258 if (oap->op_type == OP_INSERT)
2259 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2260 else if (oap->op_type == OP_APPEND)
2261 {
2262 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
2263 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
2264 }
2265 else
2266 {
2267 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
2268 if (is_del && oap->op_type != OP_LSHIFT)
2269 {
2270 // just putting the sum of those two into
2271 // bdp->startspaces doesn't work for Visual replace,
2272 // so we have to split the tab in two
2273 bdp->startspaces = bdp->start_char_vcols
2274 - (bdp->start_vcol - oap->start_vcol);
2275 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2276 }
2277 }
2278 }
2279 else
2280 {
2281 prev_pend = pend;
2282 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
2283 {
2284 // Count a tab for what it's worth (if list mode not on)
2285 prev_pend = pend;
2286 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol);
2287 bdp->end_vcol += incr;
2288 }
2289 if (bdp->end_vcol <= oap->end_vcol
2290 && (!is_del
2291 || oap->op_type == OP_APPEND
2292 || oap->op_type == OP_REPLACE)) // line too short
2293 {
2294 bdp->is_short = TRUE;
2295 // Alternative: include spaces to fill up the block.
2296 // Disadvantage: can lead to trailing spaces when the line is
2297 // short where the text is put
2298 // if (!is_del || oap->op_type == OP_APPEND)
2299 if (oap->op_type == OP_APPEND || virtual_op)
2300 bdp->endspaces = oap->end_vcol - bdp->end_vcol
2301 + oap->inclusive;
2302 else
2303 bdp->endspaces = 0; // replace doesn't add characters
2304 }
2305 else if (bdp->end_vcol > oap->end_vcol)
2306 {
2307 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
2308 if (!is_del && bdp->endspaces)
2309 {
2310 bdp->endspaces = incr - bdp->endspaces;
2311 if (pend != pstart)
2312 pend = prev_pend;
2313 }
2314 }
2315 }
2316 bdp->end_char_vcols = incr;
2317 if (is_del && bdp->startspaces)
2318 pstart = prev_pstart;
2319 bdp->textlen = (int)(pend - pstart);
2320 }
2321 bdp->textcol = (colnr_T) (pstart - line);
2322 bdp->textstart = pstart;
2323 #ifdef FEAT_LINEBREAK
2324 curwin->w_p_lbr = lbr_saved;
2325 #endif
2326 }
2327
2328 /*
2329 * Handle the add/subtract operator.
2330 */
2331 void
op_addsub(oparg_T * oap,linenr_T Prenum1,int g_cmd)2332 op_addsub(
2333 oparg_T *oap,
2334 linenr_T Prenum1, // Amount of add/subtract
2335 int g_cmd) // was g<c-a>/g<c-x>
2336 {
2337 pos_T pos;
2338 struct block_def bd;
2339 int change_cnt = 0;
2340 linenr_T amount = Prenum1;
2341
2342 // do_addsub() might trigger re-evaluation of 'foldexpr' halfway, when the
2343 // buffer is not completely updated yet. Postpone updating folds until before
2344 // the call to changed_lines().
2345 #ifdef FEAT_FOLDING
2346 disable_fold_update++;
2347 #endif
2348
2349 if (!VIsual_active)
2350 {
2351 pos = curwin->w_cursor;
2352 if (u_save_cursor() == FAIL)
2353 {
2354 #ifdef FEAT_FOLDING
2355 disable_fold_update--;
2356 #endif
2357 return;
2358 }
2359 change_cnt = do_addsub(oap->op_type, &pos, 0, amount);
2360 #ifdef FEAT_FOLDING
2361 disable_fold_update--;
2362 #endif
2363 if (change_cnt)
2364 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L);
2365 }
2366 else
2367 {
2368 int one_change;
2369 int length;
2370 pos_T startpos;
2371
2372 if (u_save((linenr_T)(oap->start.lnum - 1),
2373 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2374 {
2375 #ifdef FEAT_FOLDING
2376 disable_fold_update--;
2377 #endif
2378 return;
2379 }
2380
2381 pos = oap->start;
2382 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2383 {
2384 if (oap->block_mode) // Visual block mode
2385 {
2386 block_prep(oap, &bd, pos.lnum, FALSE);
2387 pos.col = bd.textcol;
2388 length = bd.textlen;
2389 }
2390 else if (oap->motion_type == MLINE)
2391 {
2392 curwin->w_cursor.col = 0;
2393 pos.col = 0;
2394 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2395 }
2396 else // oap->motion_type == MCHAR
2397 {
2398 if (pos.lnum == oap->start.lnum && !oap->inclusive)
2399 dec(&(oap->end));
2400 length = (colnr_T)STRLEN(ml_get(pos.lnum));
2401 pos.col = 0;
2402 if (pos.lnum == oap->start.lnum)
2403 {
2404 pos.col += oap->start.col;
2405 length -= oap->start.col;
2406 }
2407 if (pos.lnum == oap->end.lnum)
2408 {
2409 length = (int)STRLEN(ml_get(oap->end.lnum));
2410 if (oap->end.col >= length)
2411 oap->end.col = length - 1;
2412 length = oap->end.col - pos.col + 1;
2413 }
2414 }
2415 one_change = do_addsub(oap->op_type, &pos, length, amount);
2416 if (one_change)
2417 {
2418 // Remember the start position of the first change.
2419 if (change_cnt == 0)
2420 startpos = curbuf->b_op_start;
2421 ++change_cnt;
2422 }
2423
2424 #ifdef FEAT_NETBEANS_INTG
2425 if (netbeans_active() && one_change)
2426 {
2427 char_u *ptr;
2428
2429 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length);
2430 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2431 netbeans_inserted(curbuf, pos.lnum, pos.col,
2432 &ptr[pos.col], length);
2433 }
2434 #endif
2435 if (g_cmd && one_change)
2436 amount += Prenum1;
2437 }
2438
2439 #ifdef FEAT_FOLDING
2440 disable_fold_update--;
2441 #endif
2442 if (change_cnt)
2443 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2444
2445 if (!change_cnt && oap->is_VIsual)
2446 // No change: need to remove the Visual selection
2447 redraw_curbuf_later(INVERTED);
2448
2449 // Set '[ mark if something changed. Keep the last end
2450 // position from do_addsub().
2451 if (change_cnt > 0 && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
2452 curbuf->b_op_start = startpos;
2453
2454 if (change_cnt > p_report)
2455 smsg(NGETTEXT("%d line changed", "%d lines changed",
2456 change_cnt), change_cnt);
2457 }
2458 }
2459
2460 /*
2461 * Add or subtract 'Prenum1' from a number in a line
2462 * op_type is OP_NR_ADD or OP_NR_SUB
2463 *
2464 * Returns TRUE if some character was changed.
2465 */
2466 static int
do_addsub(int op_type,pos_T * pos,int length,linenr_T Prenum1)2467 do_addsub(
2468 int op_type,
2469 pos_T *pos,
2470 int length,
2471 linenr_T Prenum1)
2472 {
2473 int col;
2474 char_u *buf1;
2475 char_u buf2[NUMBUFLEN];
2476 int pre; // 'X'/'x': hex; '0': octal; 'B'/'b': bin
2477 static int hexupper = FALSE; // 0xABC
2478 uvarnumber_T n;
2479 uvarnumber_T oldn;
2480 char_u *ptr;
2481 int c;
2482 int todel;
2483 int do_hex;
2484 int do_oct;
2485 int do_bin;
2486 int do_alpha;
2487 int do_unsigned;
2488 int firstdigit;
2489 int subtract;
2490 int negative = FALSE;
2491 int was_positive = TRUE;
2492 int visual = VIsual_active;
2493 int did_change = FALSE;
2494 pos_T save_cursor = curwin->w_cursor;
2495 int maxlen = 0;
2496 pos_T startpos;
2497 pos_T endpos;
2498 colnr_T save_coladd = 0;
2499
2500 do_hex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); // "heX"
2501 do_oct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); // "Octal"
2502 do_bin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); // "Bin"
2503 do_alpha = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); // "alPha"
2504 do_unsigned = (vim_strchr(curbuf->b_p_nf, 'u') != NULL); // "Unsigned"
2505
2506 if (virtual_active())
2507 {
2508 save_coladd = pos->coladd;
2509 pos->coladd = 0;
2510 }
2511
2512 curwin->w_cursor = *pos;
2513 ptr = ml_get(pos->lnum);
2514 col = pos->col;
2515
2516 if (*ptr == NUL || col + !!save_coladd >= (int)STRLEN(ptr))
2517 goto theend;
2518
2519 /*
2520 * First check if we are on a hexadecimal number, after the "0x".
2521 */
2522 if (!VIsual_active)
2523 {
2524 if (do_bin)
2525 while (col > 0 && vim_isbdigit(ptr[col]))
2526 {
2527 --col;
2528 if (has_mbyte)
2529 col -= (*mb_head_off)(ptr, ptr + col);
2530 }
2531
2532 if (do_hex)
2533 while (col > 0 && vim_isxdigit(ptr[col]))
2534 {
2535 --col;
2536 if (has_mbyte)
2537 col -= (*mb_head_off)(ptr, ptr + col);
2538 }
2539
2540 if ( do_bin
2541 && do_hex
2542 && ! ((col > 0
2543 && (ptr[col] == 'X'
2544 || ptr[col] == 'x')
2545 && ptr[col - 1] == '0'
2546 && (!has_mbyte ||
2547 !(*mb_head_off)(ptr, ptr + col - 1))
2548 && vim_isxdigit(ptr[col + 1]))))
2549 {
2550
2551 // In case of binary/hexadecimal pattern overlap match, rescan
2552
2553 col = pos->col;
2554
2555 while (col > 0 && vim_isdigit(ptr[col]))
2556 {
2557 col--;
2558 if (has_mbyte)
2559 col -= (*mb_head_off)(ptr, ptr + col);
2560 }
2561 }
2562
2563 if (( do_hex
2564 && col > 0
2565 && (ptr[col] == 'X'
2566 || ptr[col] == 'x')
2567 && ptr[col - 1] == '0'
2568 && (!has_mbyte ||
2569 !(*mb_head_off)(ptr, ptr + col - 1))
2570 && vim_isxdigit(ptr[col + 1])) ||
2571 ( do_bin
2572 && col > 0
2573 && (ptr[col] == 'B'
2574 || ptr[col] == 'b')
2575 && ptr[col - 1] == '0'
2576 && (!has_mbyte ||
2577 !(*mb_head_off)(ptr, ptr + col - 1))
2578 && vim_isbdigit(ptr[col + 1])))
2579 {
2580 // Found hexadecimal or binary number, move to its start.
2581 --col;
2582 if (has_mbyte)
2583 col -= (*mb_head_off)(ptr, ptr + col);
2584 }
2585 else
2586 {
2587 /*
2588 * Search forward and then backward to find the start of number.
2589 */
2590 col = pos->col;
2591
2592 while (ptr[col] != NUL
2593 && !vim_isdigit(ptr[col])
2594 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
2595 col += mb_ptr2len(ptr + col);
2596
2597 while (col > 0
2598 && vim_isdigit(ptr[col - 1])
2599 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
2600 {
2601 --col;
2602 if (has_mbyte)
2603 col -= (*mb_head_off)(ptr, ptr + col);
2604 }
2605 }
2606 }
2607
2608 if (visual)
2609 {
2610 while (ptr[col] != NUL && length > 0
2611 && !vim_isdigit(ptr[col])
2612 && !(do_alpha && ASCII_ISALPHA(ptr[col])))
2613 {
2614 int mb_len = mb_ptr2len(ptr + col);
2615
2616 col += mb_len;
2617 length -= mb_len;
2618 }
2619
2620 if (length == 0)
2621 goto theend;
2622
2623 if (col > pos->col && ptr[col - 1] == '-'
2624 && (!has_mbyte || !(*mb_head_off)(ptr, ptr + col - 1))
2625 && !do_unsigned)
2626 {
2627 negative = TRUE;
2628 was_positive = FALSE;
2629 }
2630 }
2631
2632 /*
2633 * If a number was found, and saving for undo works, replace the number.
2634 */
2635 firstdigit = ptr[col];
2636 if (!VIM_ISDIGIT(firstdigit) && !(do_alpha && ASCII_ISALPHA(firstdigit)))
2637 {
2638 beep_flush();
2639 goto theend;
2640 }
2641
2642 if (do_alpha && ASCII_ISALPHA(firstdigit))
2643 {
2644 // decrement or increment alphabetic character
2645 if (op_type == OP_NR_SUB)
2646 {
2647 if (CharOrd(firstdigit) < Prenum1)
2648 {
2649 if (isupper(firstdigit))
2650 firstdigit = 'A';
2651 else
2652 firstdigit = 'a';
2653 }
2654 else
2655 #ifdef EBCDIC
2656 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
2657 #else
2658 firstdigit -= Prenum1;
2659 #endif
2660 }
2661 else
2662 {
2663 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
2664 {
2665 if (isupper(firstdigit))
2666 firstdigit = 'Z';
2667 else
2668 firstdigit = 'z';
2669 }
2670 else
2671 #ifdef EBCDIC
2672 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
2673 #else
2674 firstdigit += Prenum1;
2675 #endif
2676 }
2677 curwin->w_cursor.col = col;
2678 if (!did_change)
2679 startpos = curwin->w_cursor;
2680 did_change = TRUE;
2681 (void)del_char(FALSE);
2682 ins_char(firstdigit);
2683 endpos = curwin->w_cursor;
2684 curwin->w_cursor.col = col;
2685 }
2686 else
2687 {
2688 pos_T save_pos;
2689 int i;
2690
2691 if (col > 0 && ptr[col - 1] == '-'
2692 && (!has_mbyte ||
2693 !(*mb_head_off)(ptr, ptr + col - 1))
2694 && !visual
2695 && !do_unsigned)
2696 {
2697 // negative number
2698 --col;
2699 negative = TRUE;
2700 }
2701 // get the number value (unsigned)
2702 if (visual && VIsual_mode != 'V')
2703 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL
2704 ? (int)STRLEN(ptr) - col
2705 : length);
2706
2707 vim_str2nr(ptr + col, &pre, &length,
2708 0 + (do_bin ? STR2NR_BIN : 0)
2709 + (do_oct ? STR2NR_OCT : 0)
2710 + (do_hex ? STR2NR_HEX : 0),
2711 NULL, &n, maxlen, FALSE);
2712
2713 // ignore leading '-' for hex and octal and bin numbers
2714 if (pre && negative)
2715 {
2716 ++col;
2717 --length;
2718 negative = FALSE;
2719 }
2720 // add or subtract
2721 subtract = FALSE;
2722 if (op_type == OP_NR_SUB)
2723 subtract ^= TRUE;
2724 if (negative)
2725 subtract ^= TRUE;
2726
2727 oldn = n;
2728 if (subtract)
2729 n -= (uvarnumber_T)Prenum1;
2730 else
2731 n += (uvarnumber_T)Prenum1;
2732 // handle wraparound for decimal numbers
2733 if (!pre)
2734 {
2735 if (subtract)
2736 {
2737 if (n > oldn)
2738 {
2739 n = 1 + (n ^ (uvarnumber_T)-1);
2740 negative ^= TRUE;
2741 }
2742 }
2743 else
2744 {
2745 // add
2746 if (n < oldn)
2747 {
2748 n = (n ^ (uvarnumber_T)-1);
2749 negative ^= TRUE;
2750 }
2751 }
2752 if (n == 0)
2753 negative = FALSE;
2754 }
2755
2756 if (do_unsigned && negative)
2757 {
2758 if (subtract)
2759 // sticking at zero.
2760 n = (uvarnumber_T)0;
2761 else
2762 // sticking at 2^64 - 1.
2763 n = (uvarnumber_T)(-1);
2764 negative = FALSE;
2765 }
2766
2767 if (visual && !was_positive && !negative && col > 0)
2768 {
2769 // need to remove the '-'
2770 col--;
2771 length++;
2772 }
2773
2774 /*
2775 * Delete the old number.
2776 */
2777 curwin->w_cursor.col = col;
2778 if (!did_change)
2779 startpos = curwin->w_cursor;
2780 did_change = TRUE;
2781 todel = length;
2782 c = gchar_cursor();
2783 /*
2784 * Don't include the '-' in the length, only the length of the
2785 * part after it is kept the same.
2786 */
2787 if (c == '-')
2788 --length;
2789
2790 save_pos = curwin->w_cursor;
2791 for (i = 0; i < todel; ++i)
2792 {
2793 if (c < 0x100 && isalpha(c))
2794 {
2795 if (isupper(c))
2796 hexupper = TRUE;
2797 else
2798 hexupper = FALSE;
2799 }
2800 inc_cursor();
2801 c = gchar_cursor();
2802 }
2803 curwin->w_cursor = save_pos;
2804
2805 /*
2806 * Prepare the leading characters in buf1[].
2807 * When there are many leading zeros it could be very long.
2808 * Allocate a bit too much.
2809 */
2810 buf1 = alloc(length + NUMBUFLEN);
2811 if (buf1 == NULL)
2812 goto theend;
2813 ptr = buf1;
2814 if (negative && (!visual || was_positive))
2815 *ptr++ = '-';
2816 if (pre)
2817 {
2818 *ptr++ = '0';
2819 --length;
2820 }
2821 if (pre == 'b' || pre == 'B' ||
2822 pre == 'x' || pre == 'X')
2823 {
2824 *ptr++ = pre;
2825 --length;
2826 }
2827
2828 /*
2829 * Put the number characters in buf2[].
2830 */
2831 if (pre == 'b' || pre == 'B')
2832 {
2833 int bit = 0;
2834 int bits = sizeof(uvarnumber_T) * 8;
2835
2836 // leading zeros
2837 for (bit = bits; bit > 0; bit--)
2838 if ((n >> (bit - 1)) & 0x1) break;
2839
2840 for (i = 0; bit > 0; bit--)
2841 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0';
2842
2843 buf2[i] = '\0';
2844 }
2845 else if (pre == 0)
2846 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", (uvarnumber_T)n);
2847 else if (pre == '0')
2848 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", (uvarnumber_T)n);
2849 else if (pre && hexupper)
2850 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", (uvarnumber_T)n);
2851 else
2852 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", (uvarnumber_T)n);
2853 length -= (int)STRLEN(buf2);
2854
2855 /*
2856 * Adjust number of zeros to the new number of digits, so the
2857 * total length of the number remains the same.
2858 * Don't do this when
2859 * the result may look like an octal number.
2860 */
2861 if (firstdigit == '0' && !(do_oct && pre == 0))
2862 while (length-- > 0)
2863 *ptr++ = '0';
2864 *ptr = NUL;
2865
2866 STRCAT(buf1, buf2);
2867
2868 // Insert just after the first character to be removed, so that any
2869 // text properties will be adjusted. Then delete the old number
2870 // afterwards.
2871 save_pos = curwin->w_cursor;
2872 if (todel > 0)
2873 inc_cursor();
2874 ins_str(buf1); // insert the new number
2875 vim_free(buf1);
2876
2877 // del_char() will also mark line needing displaying
2878 if (todel > 0)
2879 {
2880 int bytes_after = (int)STRLEN(ml_get_curline())
2881 - curwin->w_cursor.col;
2882
2883 // Delete the one character before the insert.
2884 curwin->w_cursor = save_pos;
2885 (void)del_char(FALSE);
2886 curwin->w_cursor.col = (colnr_T)(STRLEN(ml_get_curline())
2887 - bytes_after);
2888 --todel;
2889 }
2890 while (todel-- > 0)
2891 (void)del_char(FALSE);
2892
2893 endpos = curwin->w_cursor;
2894 if (did_change && curwin->w_cursor.col)
2895 --curwin->w_cursor.col;
2896 }
2897
2898 if (did_change && (cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
2899 {
2900 // set the '[ and '] marks
2901 curbuf->b_op_start = startpos;
2902 curbuf->b_op_end = endpos;
2903 if (curbuf->b_op_end.col > 0)
2904 --curbuf->b_op_end.col;
2905 }
2906
2907 theend:
2908 if (visual)
2909 curwin->w_cursor = save_cursor;
2910 else if (did_change)
2911 curwin->w_set_curswant = TRUE;
2912 else if (virtual_active())
2913 curwin->w_cursor.coladd = save_coladd;
2914
2915 return did_change;
2916 }
2917
2918 void
clear_oparg(oparg_T * oap)2919 clear_oparg(oparg_T *oap)
2920 {
2921 CLEAR_POINTER(oap);
2922 }
2923
2924 /*
2925 * Count the number of bytes, characters and "words" in a line.
2926 *
2927 * "Words" are counted by looking for boundaries between non-space and
2928 * space characters. (it seems to produce results that match 'wc'.)
2929 *
2930 * Return value is byte count; word count for the line is added to "*wc".
2931 * Char count is added to "*cc".
2932 *
2933 * The function will only examine the first "limit" characters in the
2934 * line, stopping if it encounters an end-of-line (NUL byte). In that
2935 * case, eol_size will be added to the character count to account for
2936 * the size of the EOL character.
2937 */
2938 static varnumber_T
line_count_info(char_u * line,varnumber_T * wc,varnumber_T * cc,varnumber_T limit,int eol_size)2939 line_count_info(
2940 char_u *line,
2941 varnumber_T *wc,
2942 varnumber_T *cc,
2943 varnumber_T limit,
2944 int eol_size)
2945 {
2946 varnumber_T i;
2947 varnumber_T words = 0;
2948 varnumber_T chars = 0;
2949 int is_word = 0;
2950
2951 for (i = 0; i < limit && line[i] != NUL; )
2952 {
2953 if (is_word)
2954 {
2955 if (vim_isspace(line[i]))
2956 {
2957 words++;
2958 is_word = 0;
2959 }
2960 }
2961 else if (!vim_isspace(line[i]))
2962 is_word = 1;
2963 ++chars;
2964 i += (*mb_ptr2len)(line + i);
2965 }
2966
2967 if (is_word)
2968 words++;
2969 *wc += words;
2970
2971 // Add eol_size if the end of line was reached before hitting limit.
2972 if (i < limit && line[i] == NUL)
2973 {
2974 i += eol_size;
2975 chars += eol_size;
2976 }
2977 *cc += chars;
2978 return i;
2979 }
2980
2981 /*
2982 * Give some info about the position of the cursor (for "g CTRL-G").
2983 * In Visual mode, give some info about the selected region. (In this case,
2984 * the *_count_cursor variables store running totals for the selection.)
2985 * When "dict" is not NULL store the info there instead of showing it.
2986 */
2987 void
cursor_pos_info(dict_T * dict)2988 cursor_pos_info(dict_T *dict)
2989 {
2990 char_u *p;
2991 char_u buf1[50];
2992 char_u buf2[40];
2993 linenr_T lnum;
2994 varnumber_T byte_count = 0;
2995 varnumber_T bom_count = 0;
2996 varnumber_T byte_count_cursor = 0;
2997 varnumber_T char_count = 0;
2998 varnumber_T char_count_cursor = 0;
2999 varnumber_T word_count = 0;
3000 varnumber_T word_count_cursor = 0;
3001 int eol_size;
3002 varnumber_T last_check = 100000L;
3003 long line_count_selected = 0;
3004 pos_T min_pos, max_pos;
3005 oparg_T oparg;
3006 struct block_def bd;
3007
3008 /*
3009 * Compute the length of the file in characters.
3010 */
3011 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3012 {
3013 if (dict == NULL)
3014 {
3015 msg(_(no_lines_msg));
3016 return;
3017 }
3018 }
3019 else
3020 {
3021 if (get_fileformat(curbuf) == EOL_DOS)
3022 eol_size = 2;
3023 else
3024 eol_size = 1;
3025
3026 if (VIsual_active)
3027 {
3028 if (LT_POS(VIsual, curwin->w_cursor))
3029 {
3030 min_pos = VIsual;
3031 max_pos = curwin->w_cursor;
3032 }
3033 else
3034 {
3035 min_pos = curwin->w_cursor;
3036 max_pos = VIsual;
3037 }
3038 if (*p_sel == 'e' && max_pos.col > 0)
3039 --max_pos.col;
3040
3041 if (VIsual_mode == Ctrl_V)
3042 {
3043 #ifdef FEAT_LINEBREAK
3044 char_u * saved_sbr = p_sbr;
3045 char_u * saved_w_sbr = curwin->w_p_sbr;
3046
3047 // Make 'sbr' empty for a moment to get the correct size.
3048 p_sbr = empty_option;
3049 curwin->w_p_sbr = empty_option;
3050 #endif
3051 oparg.is_VIsual = 1;
3052 oparg.block_mode = TRUE;
3053 oparg.op_type = OP_NOP;
3054 getvcols(curwin, &min_pos, &max_pos,
3055 &oparg.start_vcol, &oparg.end_vcol);
3056 #ifdef FEAT_LINEBREAK
3057 p_sbr = saved_sbr;
3058 curwin->w_p_sbr = saved_w_sbr;
3059 #endif
3060 if (curwin->w_curswant == MAXCOL)
3061 oparg.end_vcol = MAXCOL;
3062 // Swap the start, end vcol if needed
3063 if (oparg.end_vcol < oparg.start_vcol)
3064 {
3065 oparg.end_vcol += oparg.start_vcol;
3066 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
3067 oparg.end_vcol -= oparg.start_vcol;
3068 }
3069 }
3070 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
3071 }
3072
3073 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
3074 {
3075 // Check for a CTRL-C every 100000 characters.
3076 if (byte_count > last_check)
3077 {
3078 ui_breakcheck();
3079 if (got_int)
3080 return;
3081 last_check = byte_count + 100000L;
3082 }
3083
3084 // Do extra processing for VIsual mode.
3085 if (VIsual_active
3086 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
3087 {
3088 char_u *s = NULL;
3089 long len = 0L;
3090
3091 switch (VIsual_mode)
3092 {
3093 case Ctrl_V:
3094 virtual_op = virtual_active();
3095 block_prep(&oparg, &bd, lnum, 0);
3096 virtual_op = MAYBE;
3097 s = bd.textstart;
3098 len = (long)bd.textlen;
3099 break;
3100 case 'V':
3101 s = ml_get(lnum);
3102 len = MAXCOL;
3103 break;
3104 case 'v':
3105 {
3106 colnr_T start_col = (lnum == min_pos.lnum)
3107 ? min_pos.col : 0;
3108 colnr_T end_col = (lnum == max_pos.lnum)
3109 ? max_pos.col - start_col + 1 : MAXCOL;
3110
3111 s = ml_get(lnum) + start_col;
3112 len = end_col;
3113 }
3114 break;
3115 }
3116 if (s != NULL)
3117 {
3118 byte_count_cursor += line_count_info(s, &word_count_cursor,
3119 &char_count_cursor, len, eol_size);
3120 if (lnum == curbuf->b_ml.ml_line_count
3121 && !curbuf->b_p_eol
3122 && (curbuf->b_p_bin || !curbuf->b_p_fixeol)
3123 && (long)STRLEN(s) < len)
3124 byte_count_cursor -= eol_size;
3125 }
3126 }
3127 else
3128 {
3129 // In non-visual mode, check for the line the cursor is on
3130 if (lnum == curwin->w_cursor.lnum)
3131 {
3132 word_count_cursor += word_count;
3133 char_count_cursor += char_count;
3134 byte_count_cursor = byte_count +
3135 line_count_info(ml_get(lnum),
3136 &word_count_cursor, &char_count_cursor,
3137 (varnumber_T)(curwin->w_cursor.col + 1),
3138 eol_size);
3139 }
3140 }
3141 // Add to the running totals
3142 byte_count += line_count_info(ml_get(lnum), &word_count,
3143 &char_count, (varnumber_T)MAXCOL,
3144 eol_size);
3145 }
3146
3147 // Correction for when last line doesn't have an EOL.
3148 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol))
3149 byte_count -= eol_size;
3150
3151 if (dict == NULL)
3152 {
3153 if (VIsual_active)
3154 {
3155 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
3156 {
3157 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
3158 &max_pos.col);
3159 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "),
3160 (long)(oparg.end_vcol - oparg.start_vcol + 1));
3161 }
3162 else
3163 buf1[0] = NUL;
3164
3165 if (char_count_cursor == byte_count_cursor
3166 && char_count == byte_count)
3167 vim_snprintf((char *)IObuff, IOSIZE,
3168 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"),
3169 buf1, line_count_selected,
3170 (long)curbuf->b_ml.ml_line_count,
3171 (varnumber_T)word_count_cursor,
3172 (varnumber_T)word_count,
3173 (varnumber_T)byte_count_cursor,
3174 (varnumber_T)byte_count);
3175 else
3176 vim_snprintf((char *)IObuff, IOSIZE,
3177 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"),
3178 buf1, line_count_selected,
3179 (long)curbuf->b_ml.ml_line_count,
3180 (varnumber_T)word_count_cursor,
3181 (varnumber_T)word_count,
3182 (varnumber_T)char_count_cursor,
3183 (varnumber_T)char_count,
3184 (varnumber_T)byte_count_cursor,
3185 (varnumber_T)byte_count);
3186 }
3187 else
3188 {
3189 p = ml_get_curline();
3190 validate_virtcol();
3191 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1,
3192 (int)curwin->w_virtcol + 1);
3193 col_print(buf2, sizeof(buf2), (int)STRLEN(p),
3194 linetabsize(p));
3195
3196 if (char_count_cursor == byte_count_cursor
3197 && char_count == byte_count)
3198 vim_snprintf((char *)IObuff, IOSIZE,
3199 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"),
3200 (char *)buf1, (char *)buf2,
3201 (long)curwin->w_cursor.lnum,
3202 (long)curbuf->b_ml.ml_line_count,
3203 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3204 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
3205 else
3206 vim_snprintf((char *)IObuff, IOSIZE,
3207 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"),
3208 (char *)buf1, (char *)buf2,
3209 (long)curwin->w_cursor.lnum,
3210 (long)curbuf->b_ml.ml_line_count,
3211 (varnumber_T)word_count_cursor, (varnumber_T)word_count,
3212 (varnumber_T)char_count_cursor, (varnumber_T)char_count,
3213 (varnumber_T)byte_count_cursor, (varnumber_T)byte_count);
3214 }
3215 }
3216
3217 bom_count = bomb_size();
3218 if (dict == NULL && bom_count > 0)
3219 {
3220 size_t len = STRLEN(IObuff);
3221
3222 vim_snprintf((char *)IObuff + len, IOSIZE - len,
3223 _("(+%lld for BOM)"), (varnumber_T)bom_count);
3224 }
3225 if (dict == NULL)
3226 {
3227 // Don't shorten this message, the user asked for it.
3228 p = p_shm;
3229 p_shm = (char_u *)"";
3230 msg((char *)IObuff);
3231 p_shm = p;
3232 }
3233 }
3234 #if defined(FEAT_EVAL)
3235 if (dict != NULL)
3236 {
3237 dict_add_number(dict, "words", word_count);
3238 dict_add_number(dict, "chars", char_count);
3239 dict_add_number(dict, "bytes", byte_count + bom_count);
3240 dict_add_number(dict, VIsual_active ? "visual_bytes" : "cursor_bytes",
3241 byte_count_cursor);
3242 dict_add_number(dict, VIsual_active ? "visual_chars" : "cursor_chars",
3243 char_count_cursor);
3244 dict_add_number(dict, VIsual_active ? "visual_words" : "cursor_words",
3245 word_count_cursor);
3246 }
3247 #endif
3248 }
3249
3250 /*
3251 * Handle indent and format operators and visual mode ":".
3252 */
3253 static void
op_colon(oparg_T * oap)3254 op_colon(oparg_T *oap)
3255 {
3256 stuffcharReadbuff(':');
3257 if (oap->is_VIsual)
3258 stuffReadbuff((char_u *)"'<,'>");
3259 else
3260 {
3261 // Make the range look nice, so it can be repeated.
3262 if (oap->start.lnum == curwin->w_cursor.lnum)
3263 stuffcharReadbuff('.');
3264 else
3265 stuffnumReadbuff((long)oap->start.lnum);
3266 if (oap->end.lnum != oap->start.lnum)
3267 {
3268 stuffcharReadbuff(',');
3269 if (oap->end.lnum == curwin->w_cursor.lnum)
3270 stuffcharReadbuff('.');
3271 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
3272 stuffcharReadbuff('$');
3273 else if (oap->start.lnum == curwin->w_cursor.lnum)
3274 {
3275 stuffReadbuff((char_u *)".+");
3276 stuffnumReadbuff((long)oap->line_count - 1);
3277 }
3278 else
3279 stuffnumReadbuff((long)oap->end.lnum);
3280 }
3281 }
3282 if (oap->op_type != OP_COLON)
3283 stuffReadbuff((char_u *)"!");
3284 if (oap->op_type == OP_INDENT)
3285 {
3286 #ifndef FEAT_CINDENT
3287 if (*get_equalprg() == NUL)
3288 stuffReadbuff((char_u *)"indent");
3289 else
3290 #endif
3291 stuffReadbuff(get_equalprg());
3292 stuffReadbuff((char_u *)"\n");
3293 }
3294 else if (oap->op_type == OP_FORMAT)
3295 {
3296 if (*curbuf->b_p_fp != NUL)
3297 stuffReadbuff(curbuf->b_p_fp);
3298 else if (*p_fp != NUL)
3299 stuffReadbuff(p_fp);
3300 else
3301 stuffReadbuff((char_u *)"fmt");
3302 stuffReadbuff((char_u *)"\n']");
3303 }
3304
3305 // do_cmdline() does the rest
3306 }
3307
3308 // callback function for 'operatorfunc'
3309 static callback_T opfunc_cb;
3310
3311 /*
3312 * Process the 'operatorfunc' option value.
3313 * Returns OK or FAIL.
3314 */
3315 int
set_operatorfunc_option(void)3316 set_operatorfunc_option(void)
3317 {
3318 return option_set_callback_func(p_opfunc, &opfunc_cb);
3319 }
3320
3321 #if defined(EXITFREE) || defined(PROTO)
3322 void
free_operatorfunc_option(void)3323 free_operatorfunc_option(void)
3324 {
3325 # ifdef FEAT_EVAL
3326 free_callback(&opfunc_cb);
3327 # endif
3328 }
3329 #endif
3330
3331 /*
3332 * Handle the "g@" operator: call 'operatorfunc'.
3333 */
3334 static void
op_function(oparg_T * oap UNUSED)3335 op_function(oparg_T *oap UNUSED)
3336 {
3337 #ifdef FEAT_EVAL
3338 typval_T argv[2];
3339 int save_virtual_op = virtual_op;
3340 int save_finish_op = finish_op;
3341 pos_T orig_start = curbuf->b_op_start;
3342 pos_T orig_end = curbuf->b_op_end;
3343 typval_T rettv;
3344
3345 if (*p_opfunc == NUL)
3346 emsg(_("E774: 'operatorfunc' is empty"));
3347 else
3348 {
3349 // Set '[ and '] marks to text to be operated on.
3350 curbuf->b_op_start = oap->start;
3351 curbuf->b_op_end = oap->end;
3352 if (oap->motion_type != MLINE && !oap->inclusive)
3353 // Exclude the end position.
3354 decl(&curbuf->b_op_end);
3355
3356 argv[0].v_type = VAR_STRING;
3357 if (oap->block_mode)
3358 argv[0].vval.v_string = (char_u *)"block";
3359 else if (oap->motion_type == MLINE)
3360 argv[0].vval.v_string = (char_u *)"line";
3361 else
3362 argv[0].vval.v_string = (char_u *)"char";
3363 argv[1].v_type = VAR_UNKNOWN;
3364
3365 // Reset virtual_op so that 'virtualedit' can be changed in the
3366 // function.
3367 virtual_op = MAYBE;
3368
3369 // Reset finish_op so that mode() returns the right value.
3370 finish_op = FALSE;
3371
3372 if (call_callback(&opfunc_cb, 0, &rettv, 1, argv) != FAIL)
3373 clear_tv(&rettv);
3374
3375 virtual_op = save_virtual_op;
3376 finish_op = save_finish_op;
3377 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
3378 {
3379 curbuf->b_op_start = orig_start;
3380 curbuf->b_op_end = orig_end;
3381 }
3382 }
3383 #else
3384 emsg(_("E775: Eval feature not available"));
3385 #endif
3386 }
3387
3388 /*
3389 * Calculate start/end virtual columns for operating in block mode.
3390 */
3391 static void
get_op_vcol(oparg_T * oap,colnr_T redo_VIsual_vcol,int initial)3392 get_op_vcol(
3393 oparg_T *oap,
3394 colnr_T redo_VIsual_vcol,
3395 int initial) // when TRUE adjust position for 'selectmode'
3396 {
3397 colnr_T start, end;
3398
3399 if (VIsual_mode != Ctrl_V
3400 || (!initial && oap->end.col < curwin->w_width))
3401 return;
3402
3403 oap->block_mode = TRUE;
3404
3405 // prevent from moving onto a trail byte
3406 if (has_mbyte)
3407 mb_adjustpos(curwin->w_buffer, &oap->end);
3408
3409 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3410
3411 if (!redo_VIsual_busy)
3412 {
3413 getvvcol(curwin, &(oap->end), &start, NULL, &end);
3414
3415 if (start < oap->start_vcol)
3416 oap->start_vcol = start;
3417 if (end > oap->end_vcol)
3418 {
3419 if (initial && *p_sel == 'e' && start >= 1
3420 && start - 1 >= oap->end_vcol)
3421 oap->end_vcol = start - 1;
3422 else
3423 oap->end_vcol = end;
3424 }
3425 }
3426
3427 // if '$' was used, get oap->end_vcol from longest line
3428 if (curwin->w_curswant == MAXCOL)
3429 {
3430 curwin->w_cursor.col = MAXCOL;
3431 oap->end_vcol = 0;
3432 for (curwin->w_cursor.lnum = oap->start.lnum;
3433 curwin->w_cursor.lnum <= oap->end.lnum;
3434 ++curwin->w_cursor.lnum)
3435 {
3436 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3437 if (end > oap->end_vcol)
3438 oap->end_vcol = end;
3439 }
3440 }
3441 else if (redo_VIsual_busy)
3442 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3443 // Correct oap->end.col and oap->start.col to be the
3444 // upper-left and lower-right corner of the block area.
3445 //
3446 // (Actually, this does convert column positions into character
3447 // positions)
3448 curwin->w_cursor.lnum = oap->end.lnum;
3449 coladvance(oap->end_vcol);
3450 oap->end = curwin->w_cursor;
3451
3452 curwin->w_cursor = oap->start;
3453 coladvance(oap->start_vcol);
3454 oap->start = curwin->w_cursor;
3455 }
3456
3457 /*
3458 * Handle an operator after Visual mode or when the movement is finished.
3459 * "gui_yank" is true when yanking text for the clipboard.
3460 */
3461 void
do_pending_operator(cmdarg_T * cap,int old_col,int gui_yank)3462 do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3463 {
3464 oparg_T *oap = cap->oap;
3465 pos_T old_cursor;
3466 int empty_region_error;
3467 int restart_edit_save;
3468 #ifdef FEAT_LINEBREAK
3469 int lbr_saved = curwin->w_p_lbr;
3470 #endif
3471
3472 // The visual area is remembered for redo
3473 static int redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3474 static linenr_T redo_VIsual_line_count; // number of lines
3475 static colnr_T redo_VIsual_vcol; // number of cols or end column
3476 static long redo_VIsual_count; // count for Visual operator
3477 static int redo_VIsual_arg; // extra argument
3478 int include_line_break = FALSE;
3479
3480 #if defined(FEAT_CLIPBOARD)
3481 // Yank the visual area into the GUI selection register before we operate
3482 // on it and lose it forever.
3483 // Don't do it if a specific register was specified, so that ""x"*P works.
3484 // This could call do_pending_operator() recursively, but that's OK
3485 // because gui_yank will be TRUE for the nested call.
3486 if ((clip_star.available || clip_plus.available)
3487 && oap->op_type != OP_NOP
3488 && !gui_yank
3489 && VIsual_active
3490 && !redo_VIsual_busy
3491 && oap->regname == 0)
3492 clip_auto_select();
3493 #endif
3494 old_cursor = curwin->w_cursor;
3495
3496 // If an operation is pending, handle it...
3497 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3498 {
3499 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3500 // for the clipboard.
3501 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3502
3503 #ifdef FEAT_LINEBREAK
3504 // Avoid a problem with unwanted linebreaks in block mode.
3505 if (curwin->w_p_lbr)
3506 curwin->w_valid &= ~VALID_VIRTCOL;
3507 curwin->w_p_lbr = FALSE;
3508 #endif
3509 oap->is_VIsual = VIsual_active;
3510 if (oap->motion_force == 'V')
3511 oap->motion_type = MLINE;
3512 else if (oap->motion_force == 'v')
3513 {
3514 // If the motion was linewise, "inclusive" will not have been set.
3515 // Use "exclusive" to be consistent. Makes "dvj" work nice.
3516 if (oap->motion_type == MLINE)
3517 oap->inclusive = FALSE;
3518 // If the motion already was characterwise, toggle "inclusive"
3519 else if (oap->motion_type == MCHAR)
3520 oap->inclusive = !oap->inclusive;
3521 oap->motion_type = MCHAR;
3522 }
3523 else if (oap->motion_force == Ctrl_V)
3524 {
3525 // Change line- or characterwise motion into Visual block mode.
3526 if (!VIsual_active)
3527 {
3528 VIsual_active = TRUE;
3529 VIsual = oap->start;
3530 }
3531 VIsual_mode = Ctrl_V;
3532 VIsual_select = FALSE;
3533 VIsual_reselect = FALSE;
3534 }
3535
3536 // Only redo yank when 'y' flag is in 'cpoptions'.
3537 // Never redo "zf" (define fold).
3538 if ((redo_yank || oap->op_type != OP_YANK)
3539 && ((!VIsual_active || oap->motion_force)
3540 // Also redo Operator-pending Visual mode mappings
3541 || (VIsual_active
3542 && (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3543 && oap->op_type != OP_COLON))
3544 && cap->cmdchar != 'D'
3545 #ifdef FEAT_FOLDING
3546 && oap->op_type != OP_FOLD
3547 && oap->op_type != OP_FOLDOPEN
3548 && oap->op_type != OP_FOLDOPENREC
3549 && oap->op_type != OP_FOLDCLOSE
3550 && oap->op_type != OP_FOLDCLOSEREC
3551 && oap->op_type != OP_FOLDDEL
3552 && oap->op_type != OP_FOLDDELREC
3553 #endif
3554 )
3555 {
3556 prep_redo(oap->regname, cap->count0,
3557 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3558 oap->motion_force, cap->cmdchar, cap->nchar);
3559 if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3560 {
3561 // If 'cpoptions' does not contain 'r', insert the search
3562 // pattern to really repeat the same command.
3563 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3564 AppendToRedobuffLit(cap->searchbuf, -1);
3565 AppendToRedobuff(NL_STR);
3566 }
3567 else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3568 {
3569 // do_cmdline() has stored the first typed line in
3570 // "repeat_cmdline". When several lines are typed repeating
3571 // won't be possible.
3572 if (repeat_cmdline == NULL)
3573 ResetRedobuff();
3574 else
3575 {
3576 AppendToRedobuffLit(repeat_cmdline, -1);
3577 AppendToRedobuff(NL_STR);
3578 VIM_CLEAR(repeat_cmdline);
3579 }
3580 }
3581 }
3582
3583 if (redo_VIsual_busy)
3584 {
3585 // Redo of an operation on a Visual area. Use the same size from
3586 // redo_VIsual_line_count and redo_VIsual_vcol.
3587 oap->start = curwin->w_cursor;
3588 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3589 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3590 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3591 VIsual_mode = redo_VIsual_mode;
3592 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3593 {
3594 if (VIsual_mode == 'v')
3595 {
3596 if (redo_VIsual_line_count <= 1)
3597 {
3598 validate_virtcol();
3599 curwin->w_curswant =
3600 curwin->w_virtcol + redo_VIsual_vcol - 1;
3601 }
3602 else
3603 curwin->w_curswant = redo_VIsual_vcol;
3604 }
3605 else
3606 {
3607 curwin->w_curswant = MAXCOL;
3608 }
3609 coladvance(curwin->w_curswant);
3610 }
3611 cap->count0 = redo_VIsual_count;
3612 if (redo_VIsual_count != 0)
3613 cap->count1 = redo_VIsual_count;
3614 else
3615 cap->count1 = 1;
3616 }
3617 else if (VIsual_active)
3618 {
3619 if (!gui_yank)
3620 {
3621 // Save the current VIsual area for '< and '> marks, and "gv"
3622 curbuf->b_visual.vi_start = VIsual;
3623 curbuf->b_visual.vi_end = curwin->w_cursor;
3624 curbuf->b_visual.vi_mode = VIsual_mode;
3625 restore_visual_mode();
3626 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3627 # ifdef FEAT_EVAL
3628 curbuf->b_visual_mode_eval = VIsual_mode;
3629 # endif
3630 }
3631
3632 // In Select mode, a linewise selection is operated upon like a
3633 // characterwise selection.
3634 // Special case: gH<Del> deletes the last line.
3635 if (VIsual_select && VIsual_mode == 'V'
3636 && cap->oap->op_type != OP_DELETE)
3637 {
3638 if (LT_POS(VIsual, curwin->w_cursor))
3639 {
3640 VIsual.col = 0;
3641 curwin->w_cursor.col =
3642 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3643 }
3644 else
3645 {
3646 curwin->w_cursor.col = 0;
3647 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3648 }
3649 VIsual_mode = 'v';
3650 }
3651 // If 'selection' is "exclusive", backup one character for
3652 // charwise selections.
3653 else if (VIsual_mode == 'v')
3654 include_line_break = unadjust_for_sel();
3655
3656 oap->start = VIsual;
3657 if (VIsual_mode == 'V')
3658 {
3659 oap->start.col = 0;
3660 oap->start.coladd = 0;
3661 }
3662 }
3663
3664 // Set oap->start to the first position of the operated text, oap->end
3665 // to the end of the operated text. w_cursor is equal to oap->start.
3666 if (LT_POS(oap->start, curwin->w_cursor))
3667 {
3668 #ifdef FEAT_FOLDING
3669 // Include folded lines completely.
3670 if (!VIsual_active)
3671 {
3672 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3673 oap->start.col = 0;
3674 if ((curwin->w_cursor.col > 0 || oap->inclusive
3675 || oap->motion_type == MLINE)
3676 && hasFolding(curwin->w_cursor.lnum, NULL,
3677 &curwin->w_cursor.lnum))
3678 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3679 }
3680 #endif
3681 oap->end = curwin->w_cursor;
3682 curwin->w_cursor = oap->start;
3683
3684 // w_virtcol may have been updated; if the cursor goes back to its
3685 // previous position w_virtcol becomes invalid and isn't updated
3686 // automatically.
3687 curwin->w_valid &= ~VALID_VIRTCOL;
3688 }
3689 else
3690 {
3691 #ifdef FEAT_FOLDING
3692 // Include folded lines completely.
3693 if (!VIsual_active && oap->motion_type == MLINE)
3694 {
3695 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3696 NULL))
3697 curwin->w_cursor.col = 0;
3698 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3699 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3700 }
3701 #endif
3702 oap->end = oap->start;
3703 oap->start = curwin->w_cursor;
3704 }
3705
3706 // Just in case lines were deleted that make the position invalid.
3707 check_pos(curwin->w_buffer, &oap->end);
3708 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3709
3710 // Set "virtual_op" before resetting VIsual_active.
3711 virtual_op = virtual_active();
3712
3713 if (VIsual_active || redo_VIsual_busy)
3714 {
3715 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3716
3717 if (!redo_VIsual_busy && !gui_yank)
3718 {
3719 // Prepare to reselect and redo Visual: this is based on the
3720 // size of the Visual text
3721 resel_VIsual_mode = VIsual_mode;
3722 if (curwin->w_curswant == MAXCOL)
3723 resel_VIsual_vcol = MAXCOL;
3724 else
3725 {
3726 if (VIsual_mode != Ctrl_V)
3727 getvvcol(curwin, &(oap->end),
3728 NULL, NULL, &oap->end_vcol);
3729 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3730 {
3731 if (VIsual_mode != Ctrl_V)
3732 getvvcol(curwin, &(oap->start),
3733 &oap->start_vcol, NULL, NULL);
3734 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3735 }
3736 else
3737 resel_VIsual_vcol = oap->end_vcol;
3738 }
3739 resel_VIsual_line_count = oap->line_count;
3740 }
3741
3742 // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3743 if ((redo_yank || oap->op_type != OP_YANK)
3744 && oap->op_type != OP_COLON
3745 #ifdef FEAT_FOLDING
3746 && oap->op_type != OP_FOLD
3747 && oap->op_type != OP_FOLDOPEN
3748 && oap->op_type != OP_FOLDOPENREC
3749 && oap->op_type != OP_FOLDCLOSE
3750 && oap->op_type != OP_FOLDCLOSEREC
3751 && oap->op_type != OP_FOLDDEL
3752 && oap->op_type != OP_FOLDDELREC
3753 #endif
3754 && oap->motion_force == NUL
3755 )
3756 {
3757 // Prepare for redoing. Only use the nchar field for "r",
3758 // otherwise it might be the second char of the operator.
3759 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3760 || cap->nchar == 'N'))
3761 prep_redo(oap->regname, cap->count0,
3762 get_op_char(oap->op_type),
3763 get_extra_op_char(oap->op_type),
3764 oap->motion_force, cap->cmdchar, cap->nchar);
3765 else if (cap->cmdchar != ':' && cap->cmdchar != K_COMMAND)
3766 {
3767 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3768
3769 // reverse what nv_replace() did
3770 if (nchar == REPLACE_CR_NCHAR)
3771 nchar = CAR;
3772 else if (nchar == REPLACE_NL_NCHAR)
3773 nchar = NL;
3774 prep_redo(oap->regname, 0L, NUL, 'v',
3775 get_op_char(oap->op_type),
3776 get_extra_op_char(oap->op_type),
3777 nchar);
3778 }
3779 if (!redo_VIsual_busy)
3780 {
3781 redo_VIsual_mode = resel_VIsual_mode;
3782 redo_VIsual_vcol = resel_VIsual_vcol;
3783 redo_VIsual_line_count = resel_VIsual_line_count;
3784 redo_VIsual_count = cap->count0;
3785 redo_VIsual_arg = cap->arg;
3786 }
3787 }
3788
3789 // oap->inclusive defaults to TRUE.
3790 // If oap->end is on a NUL (empty line) oap->inclusive becomes
3791 // FALSE. This makes "d}P" and "v}dP" work the same.
3792 if (oap->motion_force == NUL || oap->motion_type == MLINE)
3793 oap->inclusive = TRUE;
3794 if (VIsual_mode == 'V')
3795 oap->motion_type = MLINE;
3796 else
3797 {
3798 oap->motion_type = MCHAR;
3799 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3800 && (include_line_break || !virtual_op))
3801 {
3802 oap->inclusive = FALSE;
3803 // Try to include the newline, unless it's an operator
3804 // that works on lines only.
3805 if (*p_sel != 'o'
3806 && !op_on_lines(oap->op_type)
3807 && oap->end.lnum < curbuf->b_ml.ml_line_count)
3808 {
3809 ++oap->end.lnum;
3810 oap->end.col = 0;
3811 oap->end.coladd = 0;
3812 ++oap->line_count;
3813 }
3814 }
3815 }
3816
3817 redo_VIsual_busy = FALSE;
3818
3819 // Switch Visual off now, so screen updating does
3820 // not show inverted text when the screen is redrawn.
3821 // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3822 // no screen redraw, so it is done here to remove the inverted
3823 // part.
3824 if (!gui_yank)
3825 {
3826 VIsual_active = FALSE;
3827 setmouse();
3828 mouse_dragging = 0;
3829 may_clear_cmdline();
3830 if ((oap->op_type == OP_YANK
3831 || oap->op_type == OP_COLON
3832 || oap->op_type == OP_FUNCTION
3833 || oap->op_type == OP_FILTER)
3834 && oap->motion_force == NUL)
3835 {
3836 #ifdef FEAT_LINEBREAK
3837 // make sure redrawing is correct
3838 curwin->w_p_lbr = lbr_saved;
3839 #endif
3840 redraw_curbuf_later(INVERTED);
3841 }
3842 }
3843 }
3844
3845 // Include the trailing byte of a multi-byte char.
3846 if (has_mbyte && oap->inclusive)
3847 {
3848 int l;
3849
3850 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3851 if (l > 1)
3852 oap->end.col += l - 1;
3853 }
3854 curwin->w_set_curswant = TRUE;
3855
3856 // oap->empty is set when start and end are the same. The inclusive
3857 // flag affects this too, unless yanking and the end is on a NUL.
3858 oap->empty = (oap->motion_type == MCHAR
3859 && (!oap->inclusive
3860 || (oap->op_type == OP_YANK
3861 && gchar_pos(&oap->end) == NUL))
3862 && EQUAL_POS(oap->start, oap->end)
3863 && !(virtual_op && oap->start.coladd != oap->end.coladd));
3864 // For delete, change and yank, it's an error to operate on an
3865 // empty region, when 'E' included in 'cpoptions' (Vi compatible).
3866 empty_region_error = (oap->empty
3867 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3868
3869 // Force a redraw when operating on an empty Visual region, when
3870 // 'modifiable is off or creating a fold.
3871 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3872 #ifdef FEAT_FOLDING
3873 || oap->op_type == OP_FOLD
3874 #endif
3875 ))
3876 {
3877 #ifdef FEAT_LINEBREAK
3878 curwin->w_p_lbr = lbr_saved;
3879 #endif
3880 redraw_curbuf_later(INVERTED);
3881 }
3882
3883 // If the end of an operator is in column one while oap->motion_type
3884 // is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3885 // character in the previous line. If op_start is on or before the
3886 // first non-blank in the line, the operator becomes linewise
3887 // (strange, but that's the way vi does it).
3888 if ( oap->motion_type == MCHAR
3889 && oap->inclusive == FALSE
3890 && !(cap->retval & CA_NO_ADJ_OP_END)
3891 && oap->end.col == 0
3892 && (!oap->is_VIsual || *p_sel == 'o')
3893 && !oap->block_mode
3894 && oap->line_count > 1)
3895 {
3896 oap->end_adjusted = TRUE; // remember that we did this
3897 --oap->line_count;
3898 --oap->end.lnum;
3899 if (inindent(0))
3900 oap->motion_type = MLINE;
3901 else
3902 {
3903 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3904 if (oap->end.col)
3905 {
3906 --oap->end.col;
3907 oap->inclusive = TRUE;
3908 }
3909 }
3910 }
3911 else
3912 oap->end_adjusted = FALSE;
3913
3914 switch (oap->op_type)
3915 {
3916 case OP_LSHIFT:
3917 case OP_RSHIFT:
3918 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3919 auto_format(FALSE, TRUE);
3920 break;
3921
3922 case OP_JOIN_NS:
3923 case OP_JOIN:
3924 if (oap->line_count < 2)
3925 oap->line_count = 2;
3926 if (curwin->w_cursor.lnum + oap->line_count - 1 >
3927 curbuf->b_ml.ml_line_count)
3928 beep_flush();
3929 else
3930 {
3931 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3932 TRUE, TRUE, TRUE);
3933 auto_format(FALSE, TRUE);
3934 }
3935 break;
3936
3937 case OP_DELETE:
3938 VIsual_reselect = FALSE; // don't reselect now
3939 if (empty_region_error)
3940 {
3941 vim_beep(BO_OPER);
3942 CancelRedo();
3943 }
3944 else
3945 {
3946 (void)op_delete(oap);
3947 // save cursor line for undo if it wasn't saved yet
3948 if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3949 && u_save_cursor() == OK)
3950 auto_format(FALSE, TRUE);
3951 }
3952 break;
3953
3954 case OP_YANK:
3955 if (empty_region_error)
3956 {
3957 if (!gui_yank)
3958 {
3959 vim_beep(BO_OPER);
3960 CancelRedo();
3961 }
3962 }
3963 else
3964 {
3965 #ifdef FEAT_LINEBREAK
3966 curwin->w_p_lbr = lbr_saved;
3967 #endif
3968 oap->excl_tr_ws = cap->cmdchar == 'z';
3969 (void)op_yank(oap, FALSE, !gui_yank);
3970 }
3971 check_cursor_col();
3972 break;
3973
3974 case OP_CHANGE:
3975 VIsual_reselect = FALSE; // don't reselect now
3976 if (empty_region_error)
3977 {
3978 vim_beep(BO_OPER);
3979 CancelRedo();
3980 }
3981 else
3982 {
3983 // This is a new edit command, not a restart. Need to
3984 // remember it to make 'insertmode' work with mappings for
3985 // Visual mode. But do this only once and not when typed and
3986 // 'insertmode' isn't set.
3987 if (p_im || !KeyTyped)
3988 restart_edit_save = restart_edit;
3989 else
3990 restart_edit_save = 0;
3991 restart_edit = 0;
3992 #ifdef FEAT_LINEBREAK
3993 // Restore linebreak, so that when the user edits it looks as
3994 // before.
3995 curwin->w_p_lbr = lbr_saved;
3996 #endif
3997 // Reset finish_op now, don't want it set inside edit().
3998 finish_op = FALSE;
3999 if (op_change(oap)) // will call edit()
4000 cap->retval |= CA_COMMAND_BUSY;
4001 if (restart_edit == 0)
4002 restart_edit = restart_edit_save;
4003 }
4004 break;
4005
4006 case OP_FILTER:
4007 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
4008 AppendToRedobuff((char_u *)"!\r"); // use any last used !cmd
4009 else
4010 bangredo = TRUE; // do_bang() will put cmd in redo buffer
4011 // FALLTHROUGH
4012
4013 case OP_INDENT:
4014 case OP_COLON:
4015
4016 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
4017 // If 'equalprg' is empty, do the indenting internally.
4018 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
4019 {
4020 # ifdef FEAT_LISP
4021 if (curbuf->b_p_lisp)
4022 {
4023 op_reindent(oap, get_lisp_indent);
4024 break;
4025 }
4026 # endif
4027 # ifdef FEAT_CINDENT
4028 op_reindent(oap,
4029 # ifdef FEAT_EVAL
4030 *curbuf->b_p_inde != NUL ? get_expr_indent :
4031 # endif
4032 get_c_indent);
4033 break;
4034 # endif
4035 }
4036 #endif
4037
4038 op_colon(oap);
4039 break;
4040
4041 case OP_TILDE:
4042 case OP_UPPER:
4043 case OP_LOWER:
4044 case OP_ROT13:
4045 if (empty_region_error)
4046 {
4047 vim_beep(BO_OPER);
4048 CancelRedo();
4049 }
4050 else
4051 op_tilde(oap);
4052 check_cursor_col();
4053 break;
4054
4055 case OP_FORMAT:
4056 #if defined(FEAT_EVAL)
4057 if (*curbuf->b_p_fex != NUL)
4058 op_formatexpr(oap); // use expression
4059 else
4060 #endif
4061 {
4062 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
4063 op_colon(oap); // use external command
4064 else
4065 op_format(oap, FALSE); // use internal function
4066 }
4067 break;
4068 case OP_FORMAT2:
4069 op_format(oap, TRUE); // use internal function
4070 break;
4071
4072 case OP_FUNCTION:
4073 #ifdef FEAT_LINEBREAK
4074 // Restore linebreak, so that when the user edits it looks as
4075 // before.
4076 curwin->w_p_lbr = lbr_saved;
4077 #endif
4078 op_function(oap); // call 'operatorfunc'
4079 break;
4080
4081 case OP_INSERT:
4082 case OP_APPEND:
4083 VIsual_reselect = FALSE; // don't reselect now
4084 if (empty_region_error)
4085 {
4086 vim_beep(BO_OPER);
4087 CancelRedo();
4088 }
4089 else
4090 {
4091 // This is a new edit command, not a restart. Need to
4092 // remember it to make 'insertmode' work with mappings for
4093 // Visual mode. But do this only once.
4094 restart_edit_save = restart_edit;
4095 restart_edit = 0;
4096 #ifdef FEAT_LINEBREAK
4097 // Restore linebreak, so that when the user edits it looks as
4098 // before.
4099 curwin->w_p_lbr = lbr_saved;
4100 #endif
4101 op_insert(oap, cap->count1);
4102 #ifdef FEAT_LINEBREAK
4103 // Reset linebreak, so that formatting works correctly.
4104 curwin->w_p_lbr = FALSE;
4105 #endif
4106
4107 // TODO: when inserting in several lines, should format all
4108 // the lines.
4109 auto_format(FALSE, TRUE);
4110
4111 if (restart_edit == 0)
4112 restart_edit = restart_edit_save;
4113 else
4114 cap->retval |= CA_COMMAND_BUSY;
4115 }
4116 break;
4117
4118 case OP_REPLACE:
4119 VIsual_reselect = FALSE; // don't reselect now
4120 if (empty_region_error)
4121 {
4122 vim_beep(BO_OPER);
4123 CancelRedo();
4124 }
4125 else
4126 {
4127 #ifdef FEAT_LINEBREAK
4128 // Restore linebreak, so that when the user edits it looks as
4129 // before.
4130 curwin->w_p_lbr = lbr_saved;
4131 #endif
4132 op_replace(oap, cap->nchar);
4133 }
4134 break;
4135
4136 #ifdef FEAT_FOLDING
4137 case OP_FOLD:
4138 VIsual_reselect = FALSE; // don't reselect now
4139 foldCreate(oap->start.lnum, oap->end.lnum);
4140 break;
4141
4142 case OP_FOLDOPEN:
4143 case OP_FOLDOPENREC:
4144 case OP_FOLDCLOSE:
4145 case OP_FOLDCLOSEREC:
4146 VIsual_reselect = FALSE; // don't reselect now
4147 opFoldRange(oap->start.lnum, oap->end.lnum,
4148 oap->op_type == OP_FOLDOPEN
4149 || oap->op_type == OP_FOLDOPENREC,
4150 oap->op_type == OP_FOLDOPENREC
4151 || oap->op_type == OP_FOLDCLOSEREC,
4152 oap->is_VIsual);
4153 break;
4154
4155 case OP_FOLDDEL:
4156 case OP_FOLDDELREC:
4157 VIsual_reselect = FALSE; // don't reselect now
4158 deleteFold(oap->start.lnum, oap->end.lnum,
4159 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4160 break;
4161 #endif
4162 case OP_NR_ADD:
4163 case OP_NR_SUB:
4164 if (empty_region_error)
4165 {
4166 vim_beep(BO_OPER);
4167 CancelRedo();
4168 }
4169 else
4170 {
4171 VIsual_active = TRUE;
4172 #ifdef FEAT_LINEBREAK
4173 curwin->w_p_lbr = lbr_saved;
4174 #endif
4175 op_addsub(oap, cap->count1, redo_VIsual_arg);
4176 VIsual_active = FALSE;
4177 }
4178 check_cursor_col();
4179 break;
4180 default:
4181 clearopbeep(oap);
4182 }
4183 virtual_op = MAYBE;
4184 if (!gui_yank)
4185 {
4186 // if 'sol' not set, go back to old column for some commands
4187 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4188 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4189 || oap->op_type == OP_DELETE))
4190 {
4191 #ifdef FEAT_LINEBREAK
4192 curwin->w_p_lbr = FALSE;
4193 #endif
4194 coladvance(curwin->w_curswant = old_col);
4195 }
4196 }
4197 else
4198 {
4199 curwin->w_cursor = old_cursor;
4200 }
4201 oap->block_mode = FALSE;
4202 clearop(oap);
4203 motion_force = NUL;
4204 }
4205 #ifdef FEAT_LINEBREAK
4206 curwin->w_p_lbr = lbr_saved;
4207 #endif
4208 }
4209