xref: /vim-8.2.3635/src/ops.c (revision 18fefddc)
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
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
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
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
121 get_op_char(int optype)
122 {
123     return opchars[optype][0];
124 }
125 
126 /*
127  * Get second operator command character.
128  */
129     int
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 *
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
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
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
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
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
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
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
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
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 /*
3309  * Handle the "g@" operator: call 'operatorfunc'.
3310  */
3311     static void
3312 op_function(oparg_T *oap UNUSED)
3313 {
3314 #ifdef FEAT_EVAL
3315     typval_T	argv[2];
3316     int		save_virtual_op = virtual_op;
3317     pos_T	orig_start = curbuf->b_op_start;
3318     pos_T	orig_end = curbuf->b_op_end;
3319 
3320     if (*p_opfunc == NUL)
3321 	emsg(_("E774: 'operatorfunc' is empty"));
3322     else
3323     {
3324 	// Set '[ and '] marks to text to be operated on.
3325 	curbuf->b_op_start = oap->start;
3326 	curbuf->b_op_end = oap->end;
3327 	if (oap->motion_type != MLINE && !oap->inclusive)
3328 	    // Exclude the end position.
3329 	    decl(&curbuf->b_op_end);
3330 
3331 	argv[0].v_type = VAR_STRING;
3332 	if (oap->block_mode)
3333 	    argv[0].vval.v_string = (char_u *)"block";
3334 	else if (oap->motion_type == MLINE)
3335 	    argv[0].vval.v_string = (char_u *)"line";
3336 	else
3337 	    argv[0].vval.v_string = (char_u *)"char";
3338 	argv[1].v_type = VAR_UNKNOWN;
3339 
3340 	// Reset virtual_op so that 'virtualedit' can be changed in the
3341 	// function.
3342 	virtual_op = MAYBE;
3343 
3344 	(void)call_func_noret(p_opfunc, 1, argv);
3345 
3346 	virtual_op = save_virtual_op;
3347 	if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
3348 	{
3349 	    curbuf->b_op_start = orig_start;
3350 	    curbuf->b_op_end = orig_end;
3351 	}
3352     }
3353 #else
3354     emsg(_("E775: Eval feature not available"));
3355 #endif
3356 }
3357 
3358 /*
3359  * Calculate start/end virtual columns for operating in block mode.
3360  */
3361     static void
3362 get_op_vcol(
3363     oparg_T	*oap,
3364     colnr_T	redo_VIsual_vcol,
3365     int		initial)    // when TRUE adjust position for 'selectmode'
3366 {
3367     colnr_T	    start, end;
3368 
3369     if (VIsual_mode != Ctrl_V
3370 	    || (!initial && oap->end.col < curwin->w_width))
3371 	return;
3372 
3373     oap->block_mode = TRUE;
3374 
3375     // prevent from moving onto a trail byte
3376     if (has_mbyte)
3377 	mb_adjustpos(curwin->w_buffer, &oap->end);
3378 
3379     getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
3380 
3381     if (!redo_VIsual_busy)
3382     {
3383 	getvvcol(curwin, &(oap->end), &start, NULL, &end);
3384 
3385 	if (start < oap->start_vcol)
3386 	    oap->start_vcol = start;
3387 	if (end > oap->end_vcol)
3388 	{
3389 	    if (initial && *p_sel == 'e' && start >= 1
3390 				    && start - 1 >= oap->end_vcol)
3391 		oap->end_vcol = start - 1;
3392 	    else
3393 		oap->end_vcol = end;
3394 	}
3395     }
3396 
3397     // if '$' was used, get oap->end_vcol from longest line
3398     if (curwin->w_curswant == MAXCOL)
3399     {
3400 	curwin->w_cursor.col = MAXCOL;
3401 	oap->end_vcol = 0;
3402 	for (curwin->w_cursor.lnum = oap->start.lnum;
3403 		curwin->w_cursor.lnum <= oap->end.lnum;
3404 					++curwin->w_cursor.lnum)
3405 	{
3406 	    getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
3407 	    if (end > oap->end_vcol)
3408 		oap->end_vcol = end;
3409 	}
3410     }
3411     else if (redo_VIsual_busy)
3412 	oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
3413     // Correct oap->end.col and oap->start.col to be the
3414     // upper-left and lower-right corner of the block area.
3415     //
3416     // (Actually, this does convert column positions into character
3417     // positions)
3418     curwin->w_cursor.lnum = oap->end.lnum;
3419     coladvance(oap->end_vcol);
3420     oap->end = curwin->w_cursor;
3421 
3422     curwin->w_cursor = oap->start;
3423     coladvance(oap->start_vcol);
3424     oap->start = curwin->w_cursor;
3425 }
3426 
3427 /*
3428  * Handle an operator after Visual mode or when the movement is finished.
3429  * "gui_yank" is true when yanking text for the clipboard.
3430  */
3431     void
3432 do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
3433 {
3434     oparg_T	*oap = cap->oap;
3435     pos_T	old_cursor;
3436     int		empty_region_error;
3437     int		restart_edit_save;
3438 #ifdef FEAT_LINEBREAK
3439     int		lbr_saved = curwin->w_p_lbr;
3440 #endif
3441 
3442     // The visual area is remembered for redo
3443     static int	    redo_VIsual_mode = NUL; // 'v', 'V', or Ctrl-V
3444     static linenr_T redo_VIsual_line_count; // number of lines
3445     static colnr_T  redo_VIsual_vcol;	    // number of cols or end column
3446     static long	    redo_VIsual_count;	    // count for Visual operator
3447     static int	    redo_VIsual_arg;	    // extra argument
3448     int		    include_line_break = FALSE;
3449 
3450 #if defined(FEAT_CLIPBOARD)
3451     // Yank the visual area into the GUI selection register before we operate
3452     // on it and lose it forever.
3453     // Don't do it if a specific register was specified, so that ""x"*P works.
3454     // This could call do_pending_operator() recursively, but that's OK
3455     // because gui_yank will be TRUE for the nested call.
3456     if ((clip_star.available || clip_plus.available)
3457 	    && oap->op_type != OP_NOP
3458 	    && !gui_yank
3459 	    && VIsual_active
3460 	    && !redo_VIsual_busy
3461 	    && oap->regname == 0)
3462 	clip_auto_select();
3463 #endif
3464     old_cursor = curwin->w_cursor;
3465 
3466     // If an operation is pending, handle it...
3467     if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
3468     {
3469 	// Yank can be redone when 'y' is in 'cpoptions', but not when yanking
3470 	// for the clipboard.
3471 	int	redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
3472 
3473 #ifdef FEAT_LINEBREAK
3474 	// Avoid a problem with unwanted linebreaks in block mode.
3475 	if (curwin->w_p_lbr)
3476 	    curwin->w_valid &= ~VALID_VIRTCOL;
3477 	curwin->w_p_lbr = FALSE;
3478 #endif
3479 	oap->is_VIsual = VIsual_active;
3480 	if (oap->motion_force == 'V')
3481 	    oap->motion_type = MLINE;
3482 	else if (oap->motion_force == 'v')
3483 	{
3484 	    // If the motion was linewise, "inclusive" will not have been set.
3485 	    // Use "exclusive" to be consistent.  Makes "dvj" work nice.
3486 	    if (oap->motion_type == MLINE)
3487 		oap->inclusive = FALSE;
3488 	    // If the motion already was characterwise, toggle "inclusive"
3489 	    else if (oap->motion_type == MCHAR)
3490 		oap->inclusive = !oap->inclusive;
3491 	    oap->motion_type = MCHAR;
3492 	}
3493 	else if (oap->motion_force == Ctrl_V)
3494 	{
3495 	    // Change line- or characterwise motion into Visual block mode.
3496 	    if (!VIsual_active)
3497 	    {
3498 		VIsual_active = TRUE;
3499 		VIsual = oap->start;
3500 	    }
3501 	    VIsual_mode = Ctrl_V;
3502 	    VIsual_select = FALSE;
3503 	    VIsual_reselect = FALSE;
3504 	}
3505 
3506 	// Only redo yank when 'y' flag is in 'cpoptions'.
3507 	// Never redo "zf" (define fold).
3508 	if ((redo_yank || oap->op_type != OP_YANK)
3509 		&& ((!VIsual_active || oap->motion_force)
3510 		    // Also redo Operator-pending Visual mode mappings
3511 		    || (VIsual_active
3512 			  && (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3513 						  && oap->op_type != OP_COLON))
3514 		&& cap->cmdchar != 'D'
3515 #ifdef FEAT_FOLDING
3516 		&& oap->op_type != OP_FOLD
3517 		&& oap->op_type != OP_FOLDOPEN
3518 		&& oap->op_type != OP_FOLDOPENREC
3519 		&& oap->op_type != OP_FOLDCLOSE
3520 		&& oap->op_type != OP_FOLDCLOSEREC
3521 		&& oap->op_type != OP_FOLDDEL
3522 		&& oap->op_type != OP_FOLDDELREC
3523 #endif
3524 		)
3525 	{
3526 	    prep_redo(oap->regname, cap->count0,
3527 		    get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
3528 		    oap->motion_force, cap->cmdchar, cap->nchar);
3529 	    if (cap->cmdchar == '/' || cap->cmdchar == '?') // was a search
3530 	    {
3531 		// If 'cpoptions' does not contain 'r', insert the search
3532 		// pattern to really repeat the same command.
3533 		if (vim_strchr(p_cpo, CPO_REDO) == NULL)
3534 		    AppendToRedobuffLit(cap->searchbuf, -1);
3535 		AppendToRedobuff(NL_STR);
3536 	    }
3537 	    else if (cap->cmdchar == ':' || cap->cmdchar == K_COMMAND)
3538 	    {
3539 		// do_cmdline() has stored the first typed line in
3540 		// "repeat_cmdline".  When several lines are typed repeating
3541 		// won't be possible.
3542 		if (repeat_cmdline == NULL)
3543 		    ResetRedobuff();
3544 		else
3545 		{
3546 		    AppendToRedobuffLit(repeat_cmdline, -1);
3547 		    AppendToRedobuff(NL_STR);
3548 		    VIM_CLEAR(repeat_cmdline);
3549 		}
3550 	    }
3551 	}
3552 
3553 	if (redo_VIsual_busy)
3554 	{
3555 	    // Redo of an operation on a Visual area. Use the same size from
3556 	    // redo_VIsual_line_count and redo_VIsual_vcol.
3557 	    oap->start = curwin->w_cursor;
3558 	    curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
3559 	    if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3560 		curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3561 	    VIsual_mode = redo_VIsual_mode;
3562 	    if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
3563 	    {
3564 		if (VIsual_mode == 'v')
3565 		{
3566 		    if (redo_VIsual_line_count <= 1)
3567 		    {
3568 			validate_virtcol();
3569 			curwin->w_curswant =
3570 				     curwin->w_virtcol + redo_VIsual_vcol - 1;
3571 		    }
3572 		    else
3573 			curwin->w_curswant = redo_VIsual_vcol;
3574 		}
3575 		else
3576 		{
3577 		    curwin->w_curswant = MAXCOL;
3578 		}
3579 		coladvance(curwin->w_curswant);
3580 	    }
3581 	    cap->count0 = redo_VIsual_count;
3582 	    if (redo_VIsual_count != 0)
3583 		cap->count1 = redo_VIsual_count;
3584 	    else
3585 		cap->count1 = 1;
3586 	}
3587 	else if (VIsual_active)
3588 	{
3589 	    if (!gui_yank)
3590 	    {
3591 		// Save the current VIsual area for '< and '> marks, and "gv"
3592 		curbuf->b_visual.vi_start = VIsual;
3593 		curbuf->b_visual.vi_end = curwin->w_cursor;
3594 		curbuf->b_visual.vi_mode = VIsual_mode;
3595 		restore_visual_mode();
3596 		curbuf->b_visual.vi_curswant = curwin->w_curswant;
3597 # ifdef FEAT_EVAL
3598 		curbuf->b_visual_mode_eval = VIsual_mode;
3599 # endif
3600 	    }
3601 
3602 	    // In Select mode, a linewise selection is operated upon like a
3603 	    // characterwise selection.
3604 	    // Special case: gH<Del> deletes the last line.
3605 	    if (VIsual_select && VIsual_mode == 'V'
3606 					    && cap->oap->op_type != OP_DELETE)
3607 	    {
3608 		if (LT_POS(VIsual, curwin->w_cursor))
3609 		{
3610 		    VIsual.col = 0;
3611 		    curwin->w_cursor.col =
3612 			       (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
3613 		}
3614 		else
3615 		{
3616 		    curwin->w_cursor.col = 0;
3617 		    VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
3618 		}
3619 		VIsual_mode = 'v';
3620 	    }
3621 	    // If 'selection' is "exclusive", backup one character for
3622 	    // charwise selections.
3623 	    else if (VIsual_mode == 'v')
3624 		include_line_break = unadjust_for_sel();
3625 
3626 	    oap->start = VIsual;
3627 	    if (VIsual_mode == 'V')
3628 	    {
3629 		oap->start.col = 0;
3630 		oap->start.coladd = 0;
3631 	    }
3632 	}
3633 
3634 	// Set oap->start to the first position of the operated text, oap->end
3635 	// to the end of the operated text.  w_cursor is equal to oap->start.
3636 	if (LT_POS(oap->start, curwin->w_cursor))
3637 	{
3638 #ifdef FEAT_FOLDING
3639 	    // Include folded lines completely.
3640 	    if (!VIsual_active)
3641 	    {
3642 		if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
3643 		    oap->start.col = 0;
3644 		if ((curwin->w_cursor.col > 0 || oap->inclusive
3645 						  || oap->motion_type == MLINE)
3646 			&& hasFolding(curwin->w_cursor.lnum, NULL,
3647 						      &curwin->w_cursor.lnum))
3648 		    curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
3649 	    }
3650 #endif
3651 	    oap->end = curwin->w_cursor;
3652 	    curwin->w_cursor = oap->start;
3653 
3654 	    // w_virtcol may have been updated; if the cursor goes back to its
3655 	    // previous position w_virtcol becomes invalid and isn't updated
3656 	    // automatically.
3657 	    curwin->w_valid &= ~VALID_VIRTCOL;
3658 	}
3659 	else
3660 	{
3661 #ifdef FEAT_FOLDING
3662 	    // Include folded lines completely.
3663 	    if (!VIsual_active && oap->motion_type == MLINE)
3664 	    {
3665 		if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
3666 									NULL))
3667 		    curwin->w_cursor.col = 0;
3668 		if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
3669 		    oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
3670 	    }
3671 #endif
3672 	    oap->end = oap->start;
3673 	    oap->start = curwin->w_cursor;
3674 	}
3675 
3676 	// Just in case lines were deleted that make the position invalid.
3677 	check_pos(curwin->w_buffer, &oap->end);
3678 	oap->line_count = oap->end.lnum - oap->start.lnum + 1;
3679 
3680 	// Set "virtual_op" before resetting VIsual_active.
3681 	virtual_op = virtual_active();
3682 
3683 	if (VIsual_active || redo_VIsual_busy)
3684 	{
3685 	    get_op_vcol(oap, redo_VIsual_vcol, TRUE);
3686 
3687 	    if (!redo_VIsual_busy && !gui_yank)
3688 	    {
3689 		// Prepare to reselect and redo Visual: this is based on the
3690 		// size of the Visual text
3691 		resel_VIsual_mode = VIsual_mode;
3692 		if (curwin->w_curswant == MAXCOL)
3693 		    resel_VIsual_vcol = MAXCOL;
3694 		else
3695 		{
3696 		    if (VIsual_mode != Ctrl_V)
3697 			getvvcol(curwin, &(oap->end),
3698 						  NULL, NULL, &oap->end_vcol);
3699 		    if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
3700 		    {
3701 			if (VIsual_mode != Ctrl_V)
3702 			    getvvcol(curwin, &(oap->start),
3703 						&oap->start_vcol, NULL, NULL);
3704 			resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
3705 		    }
3706 		    else
3707 			resel_VIsual_vcol = oap->end_vcol;
3708 		}
3709 		resel_VIsual_line_count = oap->line_count;
3710 	    }
3711 
3712 	    // can't redo yank (unless 'y' is in 'cpoptions') and ":"
3713 	    if ((redo_yank || oap->op_type != OP_YANK)
3714 		    && oap->op_type != OP_COLON
3715 #ifdef FEAT_FOLDING
3716 		    && oap->op_type != OP_FOLD
3717 		    && oap->op_type != OP_FOLDOPEN
3718 		    && oap->op_type != OP_FOLDOPENREC
3719 		    && oap->op_type != OP_FOLDCLOSE
3720 		    && oap->op_type != OP_FOLDCLOSEREC
3721 		    && oap->op_type != OP_FOLDDEL
3722 		    && oap->op_type != OP_FOLDDELREC
3723 #endif
3724 		    && oap->motion_force == NUL
3725 		    )
3726 	    {
3727 		// Prepare for redoing.  Only use the nchar field for "r",
3728 		// otherwise it might be the second char of the operator.
3729 		if (cap->cmdchar == 'g' && (cap->nchar == 'n'
3730 							|| cap->nchar == 'N'))
3731 		    prep_redo(oap->regname, cap->count0,
3732 			    get_op_char(oap->op_type),
3733 			    get_extra_op_char(oap->op_type),
3734 			    oap->motion_force, cap->cmdchar, cap->nchar);
3735 		else if (cap->cmdchar != ':' && cap->cmdchar != K_COMMAND)
3736 		{
3737 		    int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
3738 
3739 		    // reverse what nv_replace() did
3740 		    if (nchar == REPLACE_CR_NCHAR)
3741 			nchar = CAR;
3742 		    else if (nchar == REPLACE_NL_NCHAR)
3743 			nchar = NL;
3744 		    prep_redo(oap->regname, 0L, NUL, 'v',
3745 					get_op_char(oap->op_type),
3746 					get_extra_op_char(oap->op_type),
3747 					nchar);
3748 		}
3749 		if (!redo_VIsual_busy)
3750 		{
3751 		    redo_VIsual_mode = resel_VIsual_mode;
3752 		    redo_VIsual_vcol = resel_VIsual_vcol;
3753 		    redo_VIsual_line_count = resel_VIsual_line_count;
3754 		    redo_VIsual_count = cap->count0;
3755 		    redo_VIsual_arg = cap->arg;
3756 		}
3757 	    }
3758 
3759 	    // oap->inclusive defaults to TRUE.
3760 	    // If oap->end is on a NUL (empty line) oap->inclusive becomes
3761 	    // FALSE.  This makes "d}P" and "v}dP" work the same.
3762 	    if (oap->motion_force == NUL || oap->motion_type == MLINE)
3763 		oap->inclusive = TRUE;
3764 	    if (VIsual_mode == 'V')
3765 		oap->motion_type = MLINE;
3766 	    else
3767 	    {
3768 		oap->motion_type = MCHAR;
3769 		if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
3770 			&& (include_line_break || !virtual_op))
3771 		{
3772 		    oap->inclusive = FALSE;
3773 		    // Try to include the newline, unless it's an operator
3774 		    // that works on lines only.
3775 		    if (*p_sel != 'o'
3776 			    && !op_on_lines(oap->op_type)
3777 			    && oap->end.lnum < curbuf->b_ml.ml_line_count)
3778 		    {
3779 			++oap->end.lnum;
3780 			oap->end.col = 0;
3781 			oap->end.coladd = 0;
3782 			++oap->line_count;
3783 		    }
3784 		}
3785 	    }
3786 
3787 	    redo_VIsual_busy = FALSE;
3788 
3789 	    // Switch Visual off now, so screen updating does
3790 	    // not show inverted text when the screen is redrawn.
3791 	    // With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
3792 	    // no screen redraw, so it is done here to remove the inverted
3793 	    // part.
3794 	    if (!gui_yank)
3795 	    {
3796 		VIsual_active = FALSE;
3797 		setmouse();
3798 		mouse_dragging = 0;
3799 		may_clear_cmdline();
3800 		if ((oap->op_type == OP_YANK
3801 			    || oap->op_type == OP_COLON
3802 			    || oap->op_type == OP_FUNCTION
3803 			    || oap->op_type == OP_FILTER)
3804 			&& oap->motion_force == NUL)
3805 		{
3806 #ifdef FEAT_LINEBREAK
3807 		    // make sure redrawing is correct
3808 		    curwin->w_p_lbr = lbr_saved;
3809 #endif
3810 		    redraw_curbuf_later(INVERTED);
3811 		}
3812 	    }
3813 	}
3814 
3815 	// Include the trailing byte of a multi-byte char.
3816 	if (has_mbyte && oap->inclusive)
3817 	{
3818 	    int		l;
3819 
3820 	    l = (*mb_ptr2len)(ml_get_pos(&oap->end));
3821 	    if (l > 1)
3822 		oap->end.col += l - 1;
3823 	}
3824 	curwin->w_set_curswant = TRUE;
3825 
3826 	// oap->empty is set when start and end are the same.  The inclusive
3827 	// flag affects this too, unless yanking and the end is on a NUL.
3828 	oap->empty = (oap->motion_type == MCHAR
3829 		    && (!oap->inclusive
3830 			|| (oap->op_type == OP_YANK
3831 			    && gchar_pos(&oap->end) == NUL))
3832 		    && EQUAL_POS(oap->start, oap->end)
3833 		    && !(virtual_op && oap->start.coladd != oap->end.coladd));
3834 	// For delete, change and yank, it's an error to operate on an
3835 	// empty region, when 'E' included in 'cpoptions' (Vi compatible).
3836 	empty_region_error = (oap->empty
3837 				&& vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
3838 
3839 	// Force a redraw when operating on an empty Visual region, when
3840 	// 'modifiable is off or creating a fold.
3841 	if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
3842 #ifdef FEAT_FOLDING
3843 		    || oap->op_type == OP_FOLD
3844 #endif
3845 		    ))
3846 	{
3847 #ifdef FEAT_LINEBREAK
3848 	    curwin->w_p_lbr = lbr_saved;
3849 #endif
3850 	    redraw_curbuf_later(INVERTED);
3851 	}
3852 
3853 	// If the end of an operator is in column one while oap->motion_type
3854 	// is MCHAR and oap->inclusive is FALSE, we put op_end after the last
3855 	// character in the previous line. If op_start is on or before the
3856 	// first non-blank in the line, the operator becomes linewise
3857 	// (strange, but that's the way vi does it).
3858 	if (	   oap->motion_type == MCHAR
3859 		&& oap->inclusive == FALSE
3860 		&& !(cap->retval & CA_NO_ADJ_OP_END)
3861 		&& oap->end.col == 0
3862 		&& (!oap->is_VIsual || *p_sel == 'o')
3863 		&& !oap->block_mode
3864 		&& oap->line_count > 1)
3865 	{
3866 	    oap->end_adjusted = TRUE;	    // remember that we did this
3867 	    --oap->line_count;
3868 	    --oap->end.lnum;
3869 	    if (inindent(0))
3870 		oap->motion_type = MLINE;
3871 	    else
3872 	    {
3873 		oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
3874 		if (oap->end.col)
3875 		{
3876 		    --oap->end.col;
3877 		    oap->inclusive = TRUE;
3878 		}
3879 	    }
3880 	}
3881 	else
3882 	    oap->end_adjusted = FALSE;
3883 
3884 	switch (oap->op_type)
3885 	{
3886 	case OP_LSHIFT:
3887 	case OP_RSHIFT:
3888 	    op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
3889 	    auto_format(FALSE, TRUE);
3890 	    break;
3891 
3892 	case OP_JOIN_NS:
3893 	case OP_JOIN:
3894 	    if (oap->line_count < 2)
3895 		oap->line_count = 2;
3896 	    if (curwin->w_cursor.lnum + oap->line_count - 1 >
3897 						   curbuf->b_ml.ml_line_count)
3898 		beep_flush();
3899 	    else
3900 	    {
3901 		(void)do_join(oap->line_count, oap->op_type == OP_JOIN,
3902 							    TRUE, TRUE, TRUE);
3903 		auto_format(FALSE, TRUE);
3904 	    }
3905 	    break;
3906 
3907 	case OP_DELETE:
3908 	    VIsual_reselect = FALSE;	    // don't reselect now
3909 	    if (empty_region_error)
3910 	    {
3911 		vim_beep(BO_OPER);
3912 		CancelRedo();
3913 	    }
3914 	    else
3915 	    {
3916 		(void)op_delete(oap);
3917 		// save cursor line for undo if it wasn't saved yet
3918 		if (oap->motion_type == MLINE && has_format_option(FO_AUTO)
3919 						      && u_save_cursor() == OK)
3920 		    auto_format(FALSE, TRUE);
3921 	    }
3922 	    break;
3923 
3924 	case OP_YANK:
3925 	    if (empty_region_error)
3926 	    {
3927 		if (!gui_yank)
3928 		{
3929 		    vim_beep(BO_OPER);
3930 		    CancelRedo();
3931 		}
3932 	    }
3933 	    else
3934 	    {
3935 #ifdef FEAT_LINEBREAK
3936 		curwin->w_p_lbr = lbr_saved;
3937 #endif
3938 		oap->excl_tr_ws = cap->cmdchar == 'z';
3939 		(void)op_yank(oap, FALSE, !gui_yank);
3940 	    }
3941 	    check_cursor_col();
3942 	    break;
3943 
3944 	case OP_CHANGE:
3945 	    VIsual_reselect = FALSE;	    // don't reselect now
3946 	    if (empty_region_error)
3947 	    {
3948 		vim_beep(BO_OPER);
3949 		CancelRedo();
3950 	    }
3951 	    else
3952 	    {
3953 		// This is a new edit command, not a restart.  Need to
3954 		// remember it to make 'insertmode' work with mappings for
3955 		// Visual mode.  But do this only once and not when typed and
3956 		// 'insertmode' isn't set.
3957 		if (p_im || !KeyTyped)
3958 		    restart_edit_save = restart_edit;
3959 		else
3960 		    restart_edit_save = 0;
3961 		restart_edit = 0;
3962 #ifdef FEAT_LINEBREAK
3963 		// Restore linebreak, so that when the user edits it looks as
3964 		// before.
3965 		curwin->w_p_lbr = lbr_saved;
3966 #endif
3967 		// Reset finish_op now, don't want it set inside edit().
3968 		finish_op = FALSE;
3969 		if (op_change(oap))	// will call edit()
3970 		    cap->retval |= CA_COMMAND_BUSY;
3971 		if (restart_edit == 0)
3972 		    restart_edit = restart_edit_save;
3973 	    }
3974 	    break;
3975 
3976 	case OP_FILTER:
3977 	    if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
3978 		AppendToRedobuff((char_u *)"!\r");  // use any last used !cmd
3979 	    else
3980 		bangredo = TRUE;    // do_bang() will put cmd in redo buffer
3981 	    // FALLTHROUGH
3982 
3983 	case OP_INDENT:
3984 	case OP_COLON:
3985 
3986 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
3987 	    // If 'equalprg' is empty, do the indenting internally.
3988 	    if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
3989 	    {
3990 # ifdef FEAT_LISP
3991 		if (curbuf->b_p_lisp)
3992 		{
3993 		    op_reindent(oap, get_lisp_indent);
3994 		    break;
3995 		}
3996 # endif
3997 # ifdef FEAT_CINDENT
3998 		op_reindent(oap,
3999 #  ifdef FEAT_EVAL
4000 			*curbuf->b_p_inde != NUL ? get_expr_indent :
4001 #  endif
4002 			    get_c_indent);
4003 		break;
4004 # endif
4005 	    }
4006 #endif
4007 
4008 	    op_colon(oap);
4009 	    break;
4010 
4011 	case OP_TILDE:
4012 	case OP_UPPER:
4013 	case OP_LOWER:
4014 	case OP_ROT13:
4015 	    if (empty_region_error)
4016 	    {
4017 		vim_beep(BO_OPER);
4018 		CancelRedo();
4019 	    }
4020 	    else
4021 		op_tilde(oap);
4022 	    check_cursor_col();
4023 	    break;
4024 
4025 	case OP_FORMAT:
4026 #if defined(FEAT_EVAL)
4027 	    if (*curbuf->b_p_fex != NUL)
4028 		op_formatexpr(oap);	// use expression
4029 	    else
4030 #endif
4031 	    {
4032 		if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
4033 		    op_colon(oap);		// use external command
4034 		else
4035 		    op_format(oap, FALSE);	// use internal function
4036 	    }
4037 	    break;
4038 	case OP_FORMAT2:
4039 	    op_format(oap, TRUE);	// use internal function
4040 	    break;
4041 
4042 	case OP_FUNCTION:
4043 #ifdef FEAT_LINEBREAK
4044 	    // Restore linebreak, so that when the user edits it looks as
4045 	    // before.
4046 	    curwin->w_p_lbr = lbr_saved;
4047 #endif
4048 	    op_function(oap);		// call 'operatorfunc'
4049 	    break;
4050 
4051 	case OP_INSERT:
4052 	case OP_APPEND:
4053 	    VIsual_reselect = FALSE;	// don't reselect now
4054 	    if (empty_region_error)
4055 	    {
4056 		vim_beep(BO_OPER);
4057 		CancelRedo();
4058 	    }
4059 	    else
4060 	    {
4061 		// This is a new edit command, not a restart.  Need to
4062 		// remember it to make 'insertmode' work with mappings for
4063 		// Visual mode.  But do this only once.
4064 		restart_edit_save = restart_edit;
4065 		restart_edit = 0;
4066 #ifdef FEAT_LINEBREAK
4067 		// Restore linebreak, so that when the user edits it looks as
4068 		// before.
4069 		curwin->w_p_lbr = lbr_saved;
4070 #endif
4071 		op_insert(oap, cap->count1);
4072 #ifdef FEAT_LINEBREAK
4073 		// Reset linebreak, so that formatting works correctly.
4074 		curwin->w_p_lbr = FALSE;
4075 #endif
4076 
4077 		// TODO: when inserting in several lines, should format all
4078 		// the lines.
4079 		auto_format(FALSE, TRUE);
4080 
4081 		if (restart_edit == 0)
4082 		    restart_edit = restart_edit_save;
4083 		else
4084 		    cap->retval |= CA_COMMAND_BUSY;
4085 	    }
4086 	    break;
4087 
4088 	case OP_REPLACE:
4089 	    VIsual_reselect = FALSE;	// don't reselect now
4090 	    if (empty_region_error)
4091 	    {
4092 		vim_beep(BO_OPER);
4093 		CancelRedo();
4094 	    }
4095 	    else
4096 	    {
4097 #ifdef FEAT_LINEBREAK
4098 		// Restore linebreak, so that when the user edits it looks as
4099 		// before.
4100 		curwin->w_p_lbr = lbr_saved;
4101 #endif
4102 		op_replace(oap, cap->nchar);
4103 	    }
4104 	    break;
4105 
4106 #ifdef FEAT_FOLDING
4107 	case OP_FOLD:
4108 	    VIsual_reselect = FALSE;	// don't reselect now
4109 	    foldCreate(oap->start.lnum, oap->end.lnum);
4110 	    break;
4111 
4112 	case OP_FOLDOPEN:
4113 	case OP_FOLDOPENREC:
4114 	case OP_FOLDCLOSE:
4115 	case OP_FOLDCLOSEREC:
4116 	    VIsual_reselect = FALSE;	// don't reselect now
4117 	    opFoldRange(oap->start.lnum, oap->end.lnum,
4118 		    oap->op_type == OP_FOLDOPEN
4119 					    || oap->op_type == OP_FOLDOPENREC,
4120 		    oap->op_type == OP_FOLDOPENREC
4121 					  || oap->op_type == OP_FOLDCLOSEREC,
4122 					  oap->is_VIsual);
4123 	    break;
4124 
4125 	case OP_FOLDDEL:
4126 	case OP_FOLDDELREC:
4127 	    VIsual_reselect = FALSE;	// don't reselect now
4128 	    deleteFold(oap->start.lnum, oap->end.lnum,
4129 			       oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
4130 	    break;
4131 #endif
4132 	case OP_NR_ADD:
4133 	case OP_NR_SUB:
4134 	    if (empty_region_error)
4135 	    {
4136 		vim_beep(BO_OPER);
4137 		CancelRedo();
4138 	    }
4139 	    else
4140 	    {
4141 		VIsual_active = TRUE;
4142 #ifdef FEAT_LINEBREAK
4143 		curwin->w_p_lbr = lbr_saved;
4144 #endif
4145 		op_addsub(oap, cap->count1, redo_VIsual_arg);
4146 		VIsual_active = FALSE;
4147 	    }
4148 	    check_cursor_col();
4149 	    break;
4150 	default:
4151 	    clearopbeep(oap);
4152 	}
4153 	virtual_op = MAYBE;
4154 	if (!gui_yank)
4155 	{
4156 	    // if 'sol' not set, go back to old column for some commands
4157 	    if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
4158 		    && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
4159 						|| oap->op_type == OP_DELETE))
4160 	    {
4161 #ifdef FEAT_LINEBREAK
4162 		curwin->w_p_lbr = FALSE;
4163 #endif
4164 		coladvance(curwin->w_curswant = old_col);
4165 	    }
4166 	}
4167 	else
4168 	{
4169 	    curwin->w_cursor = old_cursor;
4170 	}
4171 	oap->block_mode = FALSE;
4172 	clearop(oap);
4173 	motion_force = NUL;
4174     }
4175 #ifdef FEAT_LINEBREAK
4176     curwin->w_p_lbr = lbr_saved;
4177 #endif
4178 }
4179