xref: /vim-8.2.3635/src/ops.c (revision 0fa313a7)
1 /* vi:set ts=8 sts=4 sw=4:
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_put, do_join
13  */
14 
15 #include "vim.h"
16 
17 /*
18  * Number of registers.
19  *	0 = unnamed register, for normal yanks and puts
20  *   1..9 = registers '1' to '9', for deletes
21  * 10..35 = registers 'a' to 'z'
22  *     36 = delete register '-'
23  *     37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24  *     38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25  */
26 /*
27  * Symbolic names for some registers.
28  */
29 #define DELETION_REGISTER	36
30 #ifdef FEAT_CLIPBOARD
31 # define STAR_REGISTER		37
32 #  ifdef FEAT_X11
33 #   define PLUS_REGISTER	38
34 #  else
35 #   define PLUS_REGISTER	STAR_REGISTER	    /* there is only one */
36 #  endif
37 #endif
38 #ifdef FEAT_DND
39 # define TILDE_REGISTER		(PLUS_REGISTER + 1)
40 #endif
41 
42 #ifdef FEAT_CLIPBOARD
43 # ifdef FEAT_DND
44 #  define NUM_REGISTERS		(TILDE_REGISTER + 1)
45 # else
46 #  define NUM_REGISTERS		(PLUS_REGISTER + 1)
47 # endif
48 #else
49 # define NUM_REGISTERS		37
50 #endif
51 
52 /*
53  * Each yank register is an array of pointers to lines.
54  */
55 static struct yankreg
56 {
57     char_u	**y_array;	/* pointer to array of line pointers */
58     linenr_T	y_size;		/* number of lines in y_array */
59     char_u	y_type;		/* MLINE, MCHAR or MBLOCK */
60 #ifdef FEAT_VISUAL
61     colnr_T	y_width;	/* only set if y_type == MBLOCK */
62 #endif
63 } y_regs[NUM_REGISTERS];
64 
65 static struct yankreg	*y_current;	    /* ptr to current yankreg */
66 static int		y_append;	    /* TRUE when appending */
67 static struct yankreg	*y_previous = NULL; /* ptr to last written yankreg */
68 
69 /*
70  * structure used by block_prep, op_delete and op_yank for blockwise operators
71  * also op_change, op_shift, op_insert, op_replace - AKelly
72  */
73 struct block_def
74 {
75     int		startspaces;	/* 'extra' cols of first char */
76     int		endspaces;	/* 'extra' cols of first char */
77     int		textlen;	/* chars in block */
78     char_u	*textstart;	/* pointer to 1st char in block */
79     colnr_T	textcol;	/* cols of chars (at least part.) in block */
80     colnr_T	start_vcol;	/* start col of 1st char wholly inside block */
81     colnr_T	end_vcol;	/* start col of 1st char wholly after block */
82 #ifdef FEAT_VISUALEXTRA
83     int		is_short;	/* TRUE if line is too short to fit in block */
84     int		is_MAX;		/* TRUE if curswant==MAXCOL when starting */
85     int		is_oneChar;	/* TRUE if block within one character */
86     int		pre_whitesp;	/* screen cols of ws before block */
87     int		pre_whitesp_c;	/* chars of ws before block */
88     colnr_T	end_char_vcols;	/* number of vcols of post-block char */
89 #endif
90     colnr_T	start_char_vcols; /* number of vcols of pre-block char */
91 };
92 
93 #ifdef FEAT_VISUALEXTRA
94 static void shift_block __ARGS((oparg_T *oap, int amount));
95 static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
96 #endif
97 static int	stuff_yank __ARGS((int, char_u *));
98 static void	put_reedit_in_typebuf __ARGS((void));
99 static int	put_in_typebuf __ARGS((char_u *s, int colon));
100 static void	stuffescaped __ARGS((char_u *arg, int literally));
101 #ifdef FEAT_MBYTE
102 static void	mb_adjust_opend __ARGS((oparg_T *oap));
103 #endif
104 static void	free_yank __ARGS((long));
105 static void	free_yank_all __ARGS((void));
106 static int	yank_copy_line __ARGS((struct block_def *bd, long y_idx));
107 #ifdef FEAT_CLIPBOARD
108 static void	copy_yank_reg __ARGS((struct yankreg *reg));
109 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
110 static void	may_set_selection __ARGS((void));
111 # endif
112 #endif
113 static void	dis_msg __ARGS((char_u *p, int skip_esc));
114 #ifdef FEAT_VISUAL
115 static void	block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
116 #endif
117 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
118 static void	str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
119 #endif
120 static int	ends_in_white __ARGS((linenr_T lnum));
121 #ifdef FEAT_COMMENTS
122 static int	same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
123 static int	fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
124 #else
125 static int	fmt_check_par __ARGS((linenr_T));
126 #endif
127 
128 /*
129  * The names of operators.
130  * IMPORTANT: Index must correspond with defines in vim.h!!!
131  * The third field indicates whether the operator always works on lines.
132  */
133 static char opchars[][3] =
134 {
135     {NUL, NUL, FALSE},	/* OP_NOP */
136     {'d', NUL, FALSE},	/* OP_DELETE */
137     {'y', NUL, FALSE},	/* OP_YANK */
138     {'c', NUL, FALSE},	/* OP_CHANGE */
139     {'<', NUL, TRUE},	/* OP_LSHIFT */
140     {'>', NUL, TRUE},	/* OP_RSHIFT */
141     {'!', NUL, TRUE},	/* OP_FILTER */
142     {'g', '~', FALSE},	/* OP_TILDE */
143     {'=', NUL, TRUE},	/* OP_INDENT */
144     {'g', 'q', TRUE},	/* OP_FORMAT */
145     {':', NUL, TRUE},	/* OP_COLON */
146     {'g', 'U', FALSE},	/* OP_UPPER */
147     {'g', 'u', FALSE},	/* OP_LOWER */
148     {'J', NUL, TRUE},	/* DO_JOIN */
149     {'g', 'J', TRUE},	/* DO_JOIN_NS */
150     {'g', '?', FALSE},	/* OP_ROT13 */
151     {'r', NUL, FALSE},	/* OP_REPLACE */
152     {'I', NUL, FALSE},	/* OP_INSERT */
153     {'A', NUL, FALSE},	/* OP_APPEND */
154     {'z', 'f', TRUE},	/* OP_FOLD */
155     {'z', 'o', TRUE},	/* OP_FOLDOPEN */
156     {'z', 'O', TRUE},	/* OP_FOLDOPENREC */
157     {'z', 'c', TRUE},	/* OP_FOLDCLOSE */
158     {'z', 'C', TRUE},	/* OP_FOLDCLOSEREC */
159     {'z', 'd', TRUE},	/* OP_FOLDDEL */
160     {'z', 'D', TRUE},	/* OP_FOLDDELREC */
161     {'g', 'w', TRUE},	/* OP_FORMAT2 */
162 };
163 
164 /*
165  * Translate a command name into an operator type.
166  * Must only be called with a valid operator name!
167  */
168     int
169 get_op_type(char1, char2)
170     int		char1;
171     int		char2;
172 {
173     int		i;
174 
175     if (char1 == 'r')		/* ignore second character */
176 	return OP_REPLACE;
177     if (char1 == '~')		/* when tilde is an operator */
178 	return OP_TILDE;
179     for (i = 0; ; ++i)
180 	if (opchars[i][0] == char1 && opchars[i][1] == char2)
181 	    break;
182     return i;
183 }
184 
185 #if defined(FEAT_VISUAL) || defined(PROTO)
186 /*
187  * Return TRUE if operator "op" always works on whole lines.
188  */
189     int
190 op_on_lines(op)
191     int op;
192 {
193     return opchars[op][2];
194 }
195 #endif
196 
197 /*
198  * Get first operator command character.
199  * Returns 'g' or 'z' if there is another command character.
200  */
201     int
202 get_op_char(optype)
203     int		optype;
204 {
205     return opchars[optype][0];
206 }
207 
208 /*
209  * Get second operator command character.
210  */
211     int
212 get_extra_op_char(optype)
213     int		optype;
214 {
215     return opchars[optype][1];
216 }
217 
218 /*
219  * op_shift - handle a shift operation
220  */
221     void
222 op_shift(oap, curs_top, amount)
223     oparg_T	    *oap;
224     int		    curs_top;
225     int		    amount;
226 {
227     long	    i;
228     int		    first_char;
229     char_u	    *s;
230 #ifdef FEAT_VISUAL
231     int		    block_col = 0;
232 #endif
233 
234     if (u_save((linenr_T)(oap->start.lnum - 1),
235 				       (linenr_T)(oap->end.lnum + 1)) == FAIL)
236 	return;
237 
238 #ifdef FEAT_VISUAL
239     if (oap->block_mode)
240 	block_col = curwin->w_cursor.col;
241 #endif
242 
243     for (i = oap->line_count; --i >= 0; )
244     {
245 	first_char = *ml_get_curline();
246 	if (first_char == NUL)				/* empty line */
247 	    curwin->w_cursor.col = 0;
248 #ifdef FEAT_VISUALEXTRA
249 	else if (oap->block_mode)
250 	    shift_block(oap, amount);
251 #endif
252 	else
253 	    /* Move the line right if it doesn't start with '#', 'smartindent'
254 	     * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
255 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
256 	    if (first_char != '#' || !preprocs_left())
257 #endif
258 	{
259 	    shift_line(oap->op_type == OP_LSHIFT, p_sr, amount);
260 	}
261 	++curwin->w_cursor.lnum;
262     }
263 
264     changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
265 
266 #ifdef FEAT_VISUAL
267     if (oap->block_mode)
268     {
269 	curwin->w_cursor.lnum = oap->start.lnum;
270 	curwin->w_cursor.col = block_col;
271     }
272     else
273 #endif
274 	if (curs_top)	    /* put cursor on first line, for ">>" */
275     {
276 	curwin->w_cursor.lnum = oap->start.lnum;
277 	beginline(BL_SOL | BL_FIX);   /* shift_line() may have set cursor.col */
278     }
279     else
280 	--curwin->w_cursor.lnum;	/* put cursor on last line, for ":>" */
281 
282     if (oap->line_count > p_report)
283     {
284 	if (oap->op_type == OP_RSHIFT)
285 	    s = (char_u *)">";
286 	else
287 	    s = (char_u *)"<";
288 	if (oap->line_count == 1)
289 	{
290 	    if (amount == 1)
291 		sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
292 	    else
293 		sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
294 	}
295 	else
296 	{
297 	    if (amount == 1)
298 		sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
299 							  oap->line_count, s);
300 	    else
301 		sprintf((char *)IObuff, _("%ld lines %sed %d times"),
302 						  oap->line_count, s, amount);
303 	}
304 	msg(IObuff);
305     }
306 
307     /*
308      * Set "'[" and "']" marks.
309      */
310     curbuf->b_op_start = oap->start;
311     curbuf->b_op_end.lnum = oap->end.lnum;
312     curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
313     if (curbuf->b_op_end.col > 0)
314 	--curbuf->b_op_end.col;
315 }
316 
317 /*
318  * shift the current line one shiftwidth left (if left != 0) or right
319  * leaves cursor on first blank in the line
320  */
321     void
322 shift_line(left, round, amount)
323     int	left;
324     int	round;
325     int	amount;
326 {
327     int		count;
328     int		i, j;
329     int		p_sw = (int)curbuf->b_p_sw;
330 
331     count = get_indent();	/* get current indent */
332 
333     if (round)			/* round off indent */
334     {
335 	i = count / p_sw;	/* number of p_sw rounded down */
336 	j = count % p_sw;	/* extra spaces */
337 	if (j && left)		/* first remove extra spaces */
338 	    --amount;
339 	if (left)
340 	{
341 	    i -= amount;
342 	    if (i < 0)
343 		i = 0;
344 	}
345 	else
346 	    i += amount;
347 	count = i * p_sw;
348     }
349     else		/* original vi indent */
350     {
351 	if (left)
352 	{
353 	    count -= p_sw * amount;
354 	    if (count < 0)
355 		count = 0;
356 	}
357 	else
358 	    count += p_sw * amount;
359     }
360 
361     /* Set new indent */
362 #ifdef FEAT_VREPLACE
363     if (State & VREPLACE_FLAG)
364 	change_indent(INDENT_SET, count, FALSE, NUL);
365     else
366 #endif
367 	(void)set_indent(count, SIN_CHANGED);
368 }
369 
370 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
371 /*
372  * Shift one line of the current block one shiftwidth right or left.
373  * Leaves cursor on first character in block.
374  */
375     static void
376 shift_block(oap, amount)
377     oparg_T	*oap;
378     int		amount;
379 {
380     int			left = (oap->op_type == OP_LSHIFT);
381     int			oldstate = State;
382     int			total, split;
383     char_u		*newp, *oldp, *midp, *ptr;
384     int			oldcol = curwin->w_cursor.col;
385     int			p_sw = (int)curbuf->b_p_sw;
386     int			p_ts = (int)curbuf->b_p_ts;
387     struct block_def	bd;
388     int			internal = 0;
389     int			incr;
390     colnr_T		vcol, col = 0, ws_vcol;
391     int			i = 0, j = 0;
392     int			len;
393 
394 #ifdef FEAT_RIGHTLEFT
395     int			old_p_ri = p_ri;
396 
397     p_ri = 0;			/* don't want revins in ident */
398 #endif
399 
400     State = INSERT;		/* don't want REPLACE for State */
401     block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
402     if (bd.is_short)
403 	return;
404 
405     /* total is number of screen columns to be inserted/removed */
406     total = amount * p_sw;
407     oldp = ml_get_curline();
408 
409     if (!left)
410     {
411 	/*
412 	 *  1. Get start vcol
413 	 *  2. Total ws vcols
414 	 *  3. Divvy into TABs & spp
415 	 *  4. Construct new string
416 	 */
417 	total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
418 	ws_vcol = bd.start_vcol - bd.pre_whitesp;
419 	if (bd.startspaces)
420 	{
421 #ifdef FEAT_MBYTE
422 	    if (has_mbyte)
423 		bd.textstart += (*mb_ptr2len)(bd.textstart);
424 #endif
425 	    ++bd.textstart;
426 	}
427 	for ( ; vim_iswhite(*bd.textstart); )
428 	{
429 	    incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
430 	    total += incr;
431 	    bd.start_vcol += incr;
432 	}
433 	/* OK, now total=all the VWS reqd, and textstart points at the 1st
434 	 * non-ws char in the block. */
435 	if (!curbuf->b_p_et)
436 	    i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
437 	if (i)
438 	    j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
439 	else
440 	    j = total;
441 	/* if we're splitting a TAB, allow for it */
442 	bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
443 	len = (int)STRLEN(bd.textstart) + 1;
444 	newp = alloc_check((unsigned)(bd.textcol + i + j + len));
445 	if (newp == NULL)
446 	    return;
447 	vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
448 	mch_memmove(newp, oldp, (size_t)bd.textcol);
449 	copy_chars(newp + bd.textcol, (size_t)i, TAB);
450 	copy_spaces(newp + bd.textcol + i, (size_t)j);
451 	/* the end */
452 	mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
453     }
454     else /* left */
455     {
456 	vcol = oap->start_vcol;
457 	/* walk vcol past ws to be removed */
458 	for (midp = oldp + bd.textcol;
459 	      vcol < (oap->start_vcol + total) && vim_iswhite(*midp); )
460 	{
461 	    incr = lbr_chartabsize_adv(&midp, (colnr_T)vcol);
462 	    vcol += incr;
463 	}
464 	/* internal is the block-internal ws replacing a split TAB */
465 	if (vcol > (oap->start_vcol + total))
466 	{
467 	    /* we have to split the TAB *(midp-1) */
468 	    internal = vcol - (oap->start_vcol + total);
469 	}
470 	/* if 'expandtab' is not set, use TABs */
471 
472 	split = bd.startspaces + internal;
473 	if (split > 0)
474 	{
475 	    if (!curbuf->b_p_et)
476 	    {
477 		for (ptr = oldp, col = 0; ptr < oldp+bd.textcol; )
478 		    col += lbr_chartabsize_adv(&ptr, (colnr_T)col);
479 
480 		/* col+1 now equals the start col of the first char of the
481 		 * block (may be < oap.start_vcol if we're splitting a TAB) */
482 		i = ((col % p_ts) + split) / p_ts; /* number of tabs */
483 	    }
484 	    if (i)
485 		j = ((col % p_ts) + split) % p_ts; /* number of spp */
486 	    else
487 		j = split;
488 	}
489 
490 	newp = alloc_check(bd.textcol + i + j + (unsigned)STRLEN(midp) + 1);
491 	if (newp == NULL)
492 	    return;
493 	vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + STRLEN(midp) + 1));
494 
495 	/* copy first part we want to keep */
496 	mch_memmove(newp, oldp, (size_t)bd.textcol);
497 	/* Now copy any TABS and spp to ensure correct alignment! */
498 	while (vim_iswhite(*midp))
499 	{
500 	    if (*midp == TAB)
501 		i++;
502 	    else /*space */
503 		j++;
504 	    midp++;
505 	}
506 	/* We might have an extra TAB worth of spp now! */
507 	if (j / p_ts && !curbuf->b_p_et)
508 	{
509 	    i++;
510 	    j -= p_ts;
511 	}
512 	copy_chars(newp + bd.textcol, (size_t)i, TAB);
513 	copy_spaces(newp + bd.textcol + i, (size_t)j);
514 
515 	/* the end */
516 	mch_memmove(newp + STRLEN(newp), midp, (size_t)STRLEN(midp) + 1);
517     }
518     /* replace the line */
519     ml_replace(curwin->w_cursor.lnum, newp, FALSE);
520     changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
521     State = oldstate;
522     curwin->w_cursor.col = oldcol;
523 #ifdef FEAT_RIGHTLEFT
524     p_ri = old_p_ri;
525 #endif
526 }
527 #endif
528 
529 #ifdef FEAT_VISUALEXTRA
530 /*
531  * Insert string "s" (b_insert ? before : after) block :AKelly
532  * Caller must prepare for undo.
533  */
534     static void
535 block_insert(oap, s, b_insert, bdp)
536     oparg_T		*oap;
537     char_u		*s;
538     int			b_insert;
539     struct block_def	*bdp;
540 {
541     int		p_ts;
542     int		count = 0;	/* extra spaces to replace a cut TAB */
543     int		spaces = 0;	/* non-zero if cutting a TAB */
544     colnr_T	offset;		/* pointer along new line */
545     unsigned	s_len;		/* STRLEN(s) */
546     char_u	*newp, *oldp;	/* new, old lines */
547     linenr_T	lnum;		/* loop var */
548     int		oldstate = State;
549 
550     State = INSERT;		/* don't want REPLACE for State */
551     s_len = (unsigned)STRLEN(s);
552 
553     for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
554     {
555 	block_prep(oap, bdp, lnum, TRUE);
556 	if (bdp->is_short && b_insert)
557 	    continue;	/* OP_INSERT, line ends before block start */
558 
559 	oldp = ml_get(lnum);
560 
561 	if (b_insert)
562 	{
563 	    p_ts = bdp->start_char_vcols;
564 	    spaces = bdp->startspaces;
565 	    if (spaces != 0)
566 		count = p_ts - 1; /* we're cutting a TAB */
567 	    offset = bdp->textcol;
568 	}
569 	else /* append */
570 	{
571 	    p_ts = bdp->end_char_vcols;
572 	    if (!bdp->is_short) /* spaces = padding after block */
573 	    {
574 		spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
575 		if (spaces != 0)
576 		    count = p_ts - 1; /* we're cutting a TAB */
577 		offset = bdp->textcol + bdp->textlen - (spaces != 0);
578 	    }
579 	    else /* spaces = padding to block edge */
580 	    {
581 		/* if $ used, just append to EOL (ie spaces==0) */
582 		if (!bdp->is_MAX)
583 		    spaces = (oap->end_vcol - bdp->end_vcol) + 1;
584 		count = spaces;
585 		offset = bdp->textcol + bdp->textlen;
586 	    }
587 	}
588 
589 	newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
590 	if (newp == NULL)
591 	    continue;
592 
593 	/* copy up to shifted part */
594 	mch_memmove(newp, oldp, (size_t)(offset));
595 	oldp += offset;
596 
597 	/* insert pre-padding */
598 	copy_spaces(newp + offset, (size_t)spaces);
599 
600 	/* copy the new text */
601 	mch_memmove(newp + offset + spaces, s, (size_t)s_len);
602 	offset += s_len;
603 
604 	if (spaces && !bdp->is_short)
605 	{
606 	    /* insert post-padding */
607 	    copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
608 	    /* We're splitting a TAB, don't copy it. */
609 	    oldp++;
610 	    /* We allowed for that TAB, remember this now */
611 	    count++;
612 	}
613 
614 	if (spaces > 0)
615 	    offset += count;
616 	mch_memmove(newp + offset, oldp, (size_t)(STRLEN(oldp) + 1));
617 
618 	ml_replace(lnum, newp, FALSE);
619 
620 	if (lnum == oap->end.lnum)
621 	{
622 	    /* Set "']" mark to the end of the block instead of the end of
623 	     * the insert in the first line.  */
624 	    curbuf->b_op_end.lnum = oap->end.lnum;
625 	    curbuf->b_op_end.col = offset;
626 	}
627     } /* for all lnum */
628 
629     changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
630 
631     State = oldstate;
632 }
633 #endif
634 
635 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
636 /*
637  * op_reindent - handle reindenting a block of lines.
638  */
639     void
640 op_reindent(oap, how)
641     oparg_T	*oap;
642     int		(*how) __ARGS((void));
643 {
644     long	i;
645     char_u	*l;
646     int		count;
647     linenr_T	first_changed = 0;
648     linenr_T	last_changed = 0;
649     linenr_T	start_lnum = curwin->w_cursor.lnum;
650 
651     /* Don't even try when 'modifiable' is off. */
652     if (!curbuf->b_p_ma)
653     {
654 	EMSG(_(e_modifiable));
655 	return;
656     }
657 
658     for (i = oap->line_count; --i >= 0 && !got_int; )
659     {
660 	/* it's a slow thing to do, so give feedback so there's no worry that
661 	 * the computer's just hung. */
662 
663 	if (i > 1
664 		&& (i % 50 == 0 || i == oap->line_count - 1)
665 		&& oap->line_count > p_report)
666 	    smsg((char_u *)_("%ld lines to indent... "), i);
667 
668 	/*
669 	 * Be vi-compatible: For lisp indenting the first line is not
670 	 * indented, unless there is only one line.
671 	 */
672 #ifdef FEAT_LISP
673 	if (i != oap->line_count - 1 || oap->line_count == 1
674 						    || how != get_lisp_indent)
675 #endif
676 	{
677 	    l = skipwhite(ml_get_curline());
678 	    if (*l == NUL)		    /* empty or blank line */
679 		count = 0;
680 	    else
681 		count = how();		    /* get the indent for this line */
682 
683 	    if (set_indent(count, SIN_UNDO))
684 	    {
685 		/* did change the indent, call changed_lines() later */
686 		if (first_changed == 0)
687 		    first_changed = curwin->w_cursor.lnum;
688 		last_changed = curwin->w_cursor.lnum;
689 	    }
690 	}
691 	++curwin->w_cursor.lnum;
692     }
693 
694     /* put cursor on first non-blank of indented line */
695     curwin->w_cursor.lnum = start_lnum;
696     beginline(BL_SOL | BL_FIX);
697 
698     /* Mark changed lines so that they will be redrawn.  When Visual
699      * highlighting was present, need to continue until the last line.  When
700      * there is no change still need to remove the Visual highlighting. */
701     if (last_changed != 0)
702 	changed_lines(first_changed, 0,
703 #ifdef FEAT_VISUAL
704 		oap->is_VIsual ? start_lnum + oap->line_count :
705 #endif
706 		last_changed + 1, 0L);
707 #ifdef FEAT_VISUAL
708     else if (oap->is_VIsual)
709 	redraw_curbuf_later(INVERTED);
710 #endif
711 
712     if (oap->line_count > p_report)
713     {
714 	i = oap->line_count - (i + 1);
715 	if (i == 1)
716 	    MSG(_("1 line indented "));
717 	else
718 	    smsg((char_u *)_("%ld lines indented "), i);
719     }
720     /* set '[ and '] marks */
721     curbuf->b_op_start = oap->start;
722     curbuf->b_op_end = oap->end;
723 }
724 #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
725 
726 #if defined(FEAT_EVAL) || defined(PROTO)
727 /*
728  * Keep the last expression line here, for repeating.
729  */
730 static char_u	*expr_line = NULL;
731 
732 /*
733  * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
734  * Returns '=' when OK, NUL otherwise.
735  */
736     int
737 get_expr_register()
738 {
739     char_u	*new_line;
740 
741     new_line = getcmdline('=', 0L, 0);
742     if (new_line == NULL)
743 	return NUL;
744     if (*new_line == NUL)	/* use previous line */
745 	vim_free(new_line);
746     else
747 	set_expr_line(new_line);
748     return '=';
749 }
750 
751 /*
752  * Set the expression for the '=' register.
753  * Argument must be an allocated string.
754  */
755     void
756 set_expr_line(new_line)
757     char_u	*new_line;
758 {
759     vim_free(expr_line);
760     expr_line = new_line;
761 }
762 
763 /*
764  * Get the result of the '=' register expression.
765  * Returns a pointer to allocated memory, or NULL for failure.
766  */
767     char_u *
768 get_expr_line()
769 {
770     char_u	*expr_copy;
771     char_u	*rv;
772 
773     if (expr_line == NULL)
774 	return NULL;
775 
776     /* Make a copy of the expression, because evaluating it may cause it to be
777      * changed. */
778     expr_copy = vim_strsave(expr_line);
779     if (expr_copy == NULL)
780 	return NULL;
781 
782     rv = eval_to_string(expr_copy, NULL);
783     vim_free(expr_copy);
784     return rv;
785 }
786 
787 /*
788  * Get the '=' register expression itself, without evaluating it.
789  */
790     char_u *
791 get_expr_line_src()
792 {
793     if (expr_line == NULL)
794 	return NULL;
795     return vim_strsave(expr_line);
796 }
797 #endif /* FEAT_EVAL */
798 
799 /*
800  * Check if 'regname' is a valid name of a yank register.
801  * Note: There is no check for 0 (default register), caller should do this
802  */
803     int
804 valid_yank_reg(regname, writing)
805     int	    regname;
806     int	    writing;	    /* if TRUE check for writable registers */
807 {
808     if (       (regname > 0 && ASCII_ISALNUM(regname))
809 	    || (!writing && vim_strchr((char_u *)
810 #ifdef FEAT_EVAL
811 				    "/.%#:="
812 #else
813 				    "/.%#:"
814 #endif
815 					, regname) != NULL)
816 	    || regname == '"'
817 	    || regname == '-'
818 	    || regname == '_'
819 #ifdef FEAT_CLIPBOARD
820 	    || regname == '*'
821 	    || regname == '+'
822 #endif
823 #ifdef FEAT_DND
824 	    || (!writing && regname == '~')
825 #endif
826 							)
827 	return TRUE;
828     return FALSE;
829 }
830 
831 /*
832  * Set y_current and y_append, according to the value of "regname".
833  * Cannot handle the '_' register.
834  * Must only be called with a valid register name!
835  *
836  * If regname is 0 and writing, use register 0
837  * If regname is 0 and reading, use previous register
838  */
839     void
840 get_yank_register(regname, writing)
841     int	    regname;
842     int	    writing;
843 {
844     int	    i;
845 
846     y_append = FALSE;
847     if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
848     {
849 	y_current = y_previous;
850 	return;
851     }
852     i = regname;
853     if (VIM_ISDIGIT(i))
854 	i -= '0';
855     else if (ASCII_ISLOWER(i))
856 	i = CharOrdLow(i) + 10;
857     else if (ASCII_ISUPPER(i))
858     {
859 	i = CharOrdUp(i) + 10;
860 	y_append = TRUE;
861     }
862     else if (regname == '-')
863 	i = DELETION_REGISTER;
864 #ifdef FEAT_CLIPBOARD
865     /* When selection is not available, use register 0 instead of '*' */
866     else if (clip_star.available && regname == '*')
867 	i = STAR_REGISTER;
868     /* When clipboard is not available, use register 0 instead of '+' */
869     else if (clip_plus.available && regname == '+')
870 	i = PLUS_REGISTER;
871 #endif
872 #ifdef FEAT_DND
873     else if (!writing && regname == '~')
874 	i = TILDE_REGISTER;
875 #endif
876     else		/* not 0-9, a-z, A-Z or '-': use register 0 */
877 	i = 0;
878     y_current = &(y_regs[i]);
879     if (writing)	/* remember the register we write into for do_put() */
880 	y_previous = y_current;
881 }
882 
883 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
884 /*
885  * When "regname" is a clipboard register, obtain the selection.  If it's not
886  * available return zero, otherwise return "regname".
887  */
888     int
889 may_get_selection(regname)
890     int regname;
891 {
892     if (regname == '*')
893     {
894 	if (!clip_star.available)
895 	    regname = 0;
896 	else
897 	    clip_get_selection(&clip_star);
898     }
899     else if (regname == '+')
900     {
901 	if (!clip_plus.available)
902 	    regname = 0;
903 	else
904 	    clip_get_selection(&clip_plus);
905     }
906     return regname;
907 }
908 #endif
909 
910 #if defined(FEAT_VISUAL) || defined(PROTO)
911 /*
912  * Obtain the contents of a "normal" register. The register is made empty.
913  * The returned pointer has allocated memory, use put_register() later.
914  */
915     void *
916 get_register(name, copy)
917     int		name;
918     int		copy;	/* make a copy, if FALSE make register empty. */
919 {
920     static struct yankreg	*reg;
921     int				i;
922 
923 #ifdef FEAT_CLIPBOARD
924     /* When Visual area changed, may have to update selection.  Obtain the
925      * selection too. */
926     if (name == '*' && clip_star.available && clip_isautosel())
927     {
928 	clip_update_selection();
929 	may_get_selection(name);
930     }
931 #endif
932 
933     get_yank_register(name, 0);
934     reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
935     if (reg != NULL)
936     {
937 	*reg = *y_current;
938 	if (copy)
939 	{
940 	    /* If we run out of memory some or all of the lines are empty. */
941 	    if (reg->y_size == 0)
942 		reg->y_array = NULL;
943 	    else
944 		reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
945 							      * reg->y_size));
946 	    if (reg->y_array != NULL)
947 	    {
948 		for (i = 0; i < reg->y_size; ++i)
949 		    reg->y_array[i] = vim_strsave(y_current->y_array[i]);
950 	    }
951 	}
952 	else
953 	    y_current->y_array = NULL;
954     }
955     return (void *)reg;
956 }
957 
958 /*
959  * Put "reg" into register "name".  Free any previous contents.
960  */
961     void
962 put_register(name, reg)
963     int		name;
964     void	*reg;
965 {
966     get_yank_register(name, 0);
967     free_yank_all();
968     *y_current = *(struct yankreg *)reg;
969 
970 # ifdef FEAT_CLIPBOARD
971     /* Send text written to clipboard register to the clipboard. */
972     may_set_selection();
973 # endif
974 }
975 #endif
976 
977 #if defined(FEAT_MOUSE) || defined(PROTO)
978 /*
979  * return TRUE if the current yank register has type MLINE
980  */
981     int
982 yank_register_mline(regname)
983     int	    regname;
984 {
985     if (regname != 0 && !valid_yank_reg(regname, FALSE))
986 	return FALSE;
987     if (regname == '_')		/* black hole is always empty */
988 	return FALSE;
989     get_yank_register(regname, FALSE);
990     return (y_current->y_type == MLINE);
991 }
992 #endif
993 
994 /*
995  * start or stop recording into a yank register
996  *
997  * return FAIL for failure, OK otherwise
998  */
999     int
1000 do_record(c)
1001     int c;
1002 {
1003     char_u	*p;
1004     static int	regname;
1005     struct yankreg *old_y_previous, *old_y_current;
1006     int		retval;
1007 
1008     if (Recording == FALSE)	    /* start recording */
1009     {
1010 			/* registers 0-9, a-z and " are allowed */
1011 	if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1012 	    retval = FAIL;
1013 	else
1014 	{
1015 	    Recording = TRUE;
1016 	    showmode();
1017 	    regname = c;
1018 	    retval = OK;
1019 	}
1020     }
1021     else			    /* stop recording */
1022     {
1023 	/*
1024 	 * Get the recorded key hits.  K_SPECIAL and CSI will be escaped, so
1025 	 * that the register can be put into the typeahead buffer without
1026 	 * translation.
1027 	 */
1028 	Recording = FALSE;
1029 	MSG("");
1030 	p = get_recorded();
1031 	if (p == NULL)
1032 	    retval = FAIL;
1033 	else
1034 	{
1035 	    /*
1036 	     * We don't want to change the default register here, so save and
1037 	     * restore the current register name.
1038 	     */
1039 	    old_y_previous = y_previous;
1040 	    old_y_current = y_current;
1041 
1042 	    retval = stuff_yank(regname, p);
1043 
1044 	    y_previous = old_y_previous;
1045 	    y_current = old_y_current;
1046 	}
1047     }
1048     return retval;
1049 }
1050 
1051 /*
1052  * Stuff string "p" into yank register "regname" as a single line (append if
1053  * uppercase).	"p" must have been alloced.
1054  *
1055  * return FAIL for failure, OK otherwise
1056  */
1057     static int
1058 stuff_yank(regname, p)
1059     int		regname;
1060     char_u	*p;
1061 {
1062     char_u	*lp;
1063     char_u	**pp;
1064 
1065     /* check for read-only register */
1066     if (regname != 0 && !valid_yank_reg(regname, TRUE))
1067     {
1068 	vim_free(p);
1069 	return FAIL;
1070     }
1071     if (regname == '_')		    /* black hole: don't do anything */
1072     {
1073 	vim_free(p);
1074 	return OK;
1075     }
1076     get_yank_register(regname, TRUE);
1077     if (y_append && y_current->y_array != NULL)
1078     {
1079 	pp = &(y_current->y_array[y_current->y_size - 1]);
1080 	lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1081 	if (lp == NULL)
1082 	{
1083 	    vim_free(p);
1084 	    return FAIL;
1085 	}
1086 	STRCPY(lp, *pp);
1087 	STRCAT(lp, p);
1088 	vim_free(p);
1089 	vim_free(*pp);
1090 	*pp = lp;
1091     }
1092     else
1093     {
1094 	free_yank_all();
1095 	if ((y_current->y_array =
1096 			(char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1097 	{
1098 	    vim_free(p);
1099 	    return FAIL;
1100 	}
1101 	y_current->y_array[0] = p;
1102 	y_current->y_size = 1;
1103 	y_current->y_type = MCHAR;  /* used to be MLINE, why? */
1104     }
1105     return OK;
1106 }
1107 
1108 /*
1109  * execute a yank register: copy it into the stuff buffer
1110  *
1111  * return FAIL for failure, OK otherwise
1112  */
1113     int
1114 do_execreg(regname, colon, addcr)
1115     int	    regname;
1116     int	    colon;		/* insert ':' before each line */
1117     int	    addcr;		/* always add '\n' to end of line */
1118 {
1119     static int	lastc = NUL;
1120     long	i;
1121     char_u	*p;
1122     int		retval = OK;
1123     int		remap;
1124 
1125     if (regname == '@')			/* repeat previous one */
1126     {
1127 	if (lastc == NUL)
1128 	{
1129 	    EMSG(_("E748: No previously used register"));
1130 	    return FAIL;
1131 	}
1132 	regname = lastc;
1133     }
1134 					/* check for valid regname */
1135     if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
1136     {
1137 	emsg_invreg(regname);
1138 	return FAIL;
1139     }
1140     lastc = regname;
1141 
1142 #ifdef FEAT_CLIPBOARD
1143     regname = may_get_selection(regname);
1144 #endif
1145 
1146     if (regname == '_')			/* black hole: don't stuff anything */
1147 	return OK;
1148 
1149 #ifdef FEAT_CMDHIST
1150     if (regname == ':')			/* use last command line */
1151     {
1152 	if (last_cmdline == NULL)
1153 	{
1154 	    EMSG(_(e_nolastcmd));
1155 	    return FAIL;
1156 	}
1157 	vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1158 	new_last_cmdline = NULL;
1159 	/* Escape all control characters with a CTRL-V */
1160 	p = vim_strsave_escaped_ext(last_cmdline,
1161 		(char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE);
1162 	if (p != NULL)
1163 	    retval = put_in_typebuf(p, TRUE);
1164 	vim_free(p);
1165     }
1166 #endif
1167 #ifdef FEAT_EVAL
1168     else if (regname == '=')
1169     {
1170 	p = get_expr_line();
1171 	if (p == NULL)
1172 	    return FAIL;
1173 	retval = put_in_typebuf(p, colon);
1174 	vim_free(p);
1175     }
1176 #endif
1177     else if (regname == '.')		/* use last inserted text */
1178     {
1179 	p = get_last_insert_save();
1180 	if (p == NULL)
1181 	{
1182 	    EMSG(_(e_noinstext));
1183 	    return FAIL;
1184 	}
1185 	retval = put_in_typebuf(p, colon);
1186 	vim_free(p);
1187     }
1188     else
1189     {
1190 	get_yank_register(regname, FALSE);
1191 	if (y_current->y_array == NULL)
1192 	    return FAIL;
1193 
1194 	/* Disallow remaping for ":@r". */
1195 	remap = colon ? REMAP_NONE : REMAP_YES;
1196 
1197 	/*
1198 	 * Insert lines into typeahead buffer, from last one to first one.
1199 	 */
1200 	put_reedit_in_typebuf();
1201 	for (i = y_current->y_size; --i >= 0; )
1202 	{
1203 	    /* insert NL between lines and after last line if type is MLINE */
1204 	    if (y_current->y_type == MLINE || i < y_current->y_size - 1
1205 								     || addcr)
1206 	    {
1207 		if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, FALSE) == FAIL)
1208 		    return FAIL;
1209 	    }
1210 	    if (ins_typebuf(y_current->y_array[i], remap, 0, TRUE, FALSE)
1211 								      == FAIL)
1212 		return FAIL;
1213 	    if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, FALSE)
1214 								      == FAIL)
1215 		return FAIL;
1216 	}
1217 	Exec_reg = TRUE;	/* disable the 'q' command */
1218     }
1219     return retval;
1220 }
1221 
1222 /*
1223  * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1224  * used only after other typeahead has been processed.
1225  */
1226     static void
1227 put_reedit_in_typebuf()
1228 {
1229     char_u	buf[3];
1230 
1231     if (restart_edit != NUL)
1232     {
1233 	if (restart_edit == 'V')
1234 	{
1235 	    buf[0] = 'g';
1236 	    buf[1] = 'R';
1237 	    buf[2] = NUL;
1238 	}
1239 	else
1240 	{
1241 	    buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1242 	    buf[1] = NUL;
1243 	}
1244 	if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE) == OK)
1245 	    restart_edit = NUL;
1246     }
1247 }
1248 
1249     static int
1250 put_in_typebuf(s, colon)
1251     char_u	*s;
1252     int		colon;	    /* add ':' before the line */
1253 {
1254     int		retval = OK;
1255 
1256     put_reedit_in_typebuf();
1257     if (colon)
1258 	retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, FALSE);
1259     if (retval == OK)
1260 	retval = ins_typebuf(s, REMAP_YES, 0, TRUE, FALSE);
1261     if (colon && retval == OK)
1262 	retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, FALSE);
1263     return retval;
1264 }
1265 
1266 /*
1267  * Insert a yank register: copy it into the Read buffer.
1268  * Used by CTRL-R command and middle mouse button in insert mode.
1269  *
1270  * return FAIL for failure, OK otherwise
1271  */
1272     int
1273 insert_reg(regname, literally)
1274     int		regname;
1275     int		literally;	/* insert literally, not as if typed */
1276 {
1277     long	i;
1278     int		retval = OK;
1279     char_u	*arg;
1280     int		allocated;
1281 
1282     /*
1283      * It is possible to get into an endless loop by having CTRL-R a in
1284      * register a and then, in insert mode, doing CTRL-R a.
1285      * If you hit CTRL-C, the loop will be broken here.
1286      */
1287     ui_breakcheck();
1288     if (got_int)
1289 	return FAIL;
1290 
1291     /* check for valid regname */
1292     if (regname != NUL && !valid_yank_reg(regname, FALSE))
1293 	return FAIL;
1294 
1295 #ifdef FEAT_CLIPBOARD
1296     regname = may_get_selection(regname);
1297 #endif
1298 
1299     if (regname == '.')			/* insert last inserted text */
1300 	retval = stuff_inserted(NUL, 1L, TRUE);
1301     else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1302     {
1303 	if (arg == NULL)
1304 	    return FAIL;
1305 	stuffescaped(arg, literally);
1306 	if (allocated)
1307 	    vim_free(arg);
1308     }
1309     else				/* name or number register */
1310     {
1311 	get_yank_register(regname, FALSE);
1312 	if (y_current->y_array == NULL)
1313 	    retval = FAIL;
1314 	else
1315 	{
1316 	    for (i = 0; i < y_current->y_size; ++i)
1317 	    {
1318 		stuffescaped(y_current->y_array[i], literally);
1319 		/*
1320 		 * Insert a newline between lines and after last line if
1321 		 * y_type is MLINE.
1322 		 */
1323 		if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1324 		    stuffcharReadbuff('\n');
1325 	    }
1326 	}
1327     }
1328 
1329     return retval;
1330 }
1331 
1332 /*
1333  * Stuff a string into the typeahead buffer, such that edit() will insert it
1334  * literally ("literally" TRUE) or interpret is as typed characters.
1335  */
1336     static void
1337 stuffescaped(arg, literally)
1338     char_u	*arg;
1339     int		literally;
1340 {
1341     int		c;
1342     char_u	*start;
1343 
1344     while (*arg != NUL)
1345     {
1346 	/* Stuff a sequence of normal ASCII characters, that's fast.  Also
1347 	 * stuff K_SPECIAL to get the effect of a special key when "literally"
1348 	 * is TRUE. */
1349 	start = arg;
1350 	while ((*arg >= ' '
1351 #ifndef EBCDIC
1352 		    && *arg < DEL /* EBCDIC: chars above space are normal */
1353 #endif
1354 		    )
1355 		|| (*arg == K_SPECIAL && !literally))
1356 	    ++arg;
1357 	if (arg > start)
1358 	    stuffReadbuffLen(start, (long)(arg - start));
1359 
1360 	/* stuff a single special character */
1361 	if (*arg != NUL)
1362 	{
1363 #ifdef FEAT_MBYTE
1364 	    if (has_mbyte)
1365 		c = mb_ptr2char_adv(&arg);
1366 	    else
1367 #endif
1368 		c = *arg++;
1369 	    if (literally && ((c < ' ' && c != TAB) || c == DEL))
1370 		stuffcharReadbuff(Ctrl_V);
1371 	    stuffcharReadbuff(c);
1372 	}
1373     }
1374 }
1375 
1376 /*
1377  * If "regname" is a special register, return a pointer to its value.
1378  */
1379     int
1380 get_spec_reg(regname, argp, allocated, errmsg)
1381     int		regname;
1382     char_u	**argp;
1383     int		*allocated;
1384     int		errmsg;		/* give error message when failing */
1385 {
1386     int		cnt;
1387 
1388     *argp = NULL;
1389     *allocated = FALSE;
1390     switch (regname)
1391     {
1392 	case '%':		/* file name */
1393 	    if (errmsg)
1394 		check_fname();	/* will give emsg if not set */
1395 	    *argp = curbuf->b_fname;
1396 	    return TRUE;
1397 
1398 	case '#':		/* alternate file name */
1399 	    *argp = getaltfname(errmsg);	/* may give emsg if not set */
1400 	    return TRUE;
1401 
1402 #ifdef FEAT_EVAL
1403 	case '=':		/* result of expression */
1404 	    *argp = get_expr_line();
1405 	    *allocated = TRUE;
1406 	    return TRUE;
1407 #endif
1408 
1409 	case ':':		/* last command line */
1410 	    if (last_cmdline == NULL && errmsg)
1411 		EMSG(_(e_nolastcmd));
1412 	    *argp = last_cmdline;
1413 	    return TRUE;
1414 
1415 	case '/':		/* last search-pattern */
1416 	    if (last_search_pat() == NULL && errmsg)
1417 		EMSG(_(e_noprevre));
1418 	    *argp = last_search_pat();
1419 	    return TRUE;
1420 
1421 	case '.':		/* last inserted text */
1422 	    *argp = get_last_insert_save();
1423 	    *allocated = TRUE;
1424 	    if (*argp == NULL && errmsg)
1425 		EMSG(_(e_noinstext));
1426 	    return TRUE;
1427 
1428 #ifdef FEAT_SEARCHPATH
1429 	case Ctrl_F:		/* Filename under cursor */
1430 	case Ctrl_P:		/* Path under cursor, expand via "path" */
1431 	    if (!errmsg)
1432 		return FALSE;
1433 	    *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
1434 				| (regname == Ctrl_P ? FNAME_EXP : 0), 1L);
1435 	    *allocated = TRUE;
1436 	    return TRUE;
1437 #endif
1438 
1439 	case Ctrl_W:		/* word under cursor */
1440 	case Ctrl_A:		/* WORD (mnemonic All) under cursor */
1441 	    if (!errmsg)
1442 		return FALSE;
1443 	    cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1444 				   ?  (FIND_IDENT|FIND_STRING) : FIND_STRING);
1445 	    *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1446 	    *allocated = TRUE;
1447 	    return TRUE;
1448 
1449 	case '_':		/* black hole: always empty */
1450 	    *argp = (char_u *)"";
1451 	    return TRUE;
1452     }
1453 
1454     return FALSE;
1455 }
1456 
1457 /*
1458  * Paste a yank register into the command line.
1459  * Only for non-special registers.
1460  * Used by CTRL-R command in command-line mode
1461  * insert_reg() can't be used here, because special characters from the
1462  * register contents will be interpreted as commands.
1463  *
1464  * return FAIL for failure, OK otherwise
1465  */
1466     int
1467 cmdline_paste_reg(regname, literally)
1468     int regname;
1469     int literally;	/* Insert text literally instead of "as typed" */
1470 {
1471     long	i;
1472 
1473     get_yank_register(regname, FALSE);
1474     if (y_current->y_array == NULL)
1475 	return FAIL;
1476 
1477     for (i = 0; i < y_current->y_size; ++i)
1478     {
1479 	cmdline_paste_str(y_current->y_array[i], literally);
1480 
1481 	/* insert ^M between lines and after last line if type is MLINE */
1482 	if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1483 	    cmdline_paste_str((char_u *)"\r", literally);
1484 
1485 	/* Check for CTRL-C, in case someone tries to paste a few thousand
1486 	 * lines and gets bored. */
1487 	ui_breakcheck();
1488 	if (got_int)
1489 	    return FAIL;
1490     }
1491     return OK;
1492 }
1493 
1494 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
1495 /*
1496  * Adjust the register name pointed to with "rp" for the clipboard being
1497  * used always and the clipboard being available.
1498  */
1499     void
1500 adjust_clip_reg(rp)
1501     int		*rp;
1502 {
1503     /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1504     if (*rp == 0 && clip_unnamed)
1505 	*rp = '*';
1506     if (!clip_star.available && *rp == '*')
1507 	*rp = 0;
1508     if (!clip_plus.available && *rp == '+')
1509 	*rp = 0;
1510 }
1511 #endif
1512 
1513 /*
1514  * op_delete - handle a delete operation
1515  *
1516  * return FAIL if undo failed, OK otherwise.
1517  */
1518     int
1519 op_delete(oap)
1520     oparg_T   *oap;
1521 {
1522     int			n;
1523     linenr_T		lnum;
1524     char_u		*ptr;
1525 #ifdef FEAT_VISUAL
1526     char_u		*newp, *oldp;
1527     struct block_def	bd;
1528 #endif
1529     linenr_T		old_lcount = curbuf->b_ml.ml_line_count;
1530     int			did_yank = FALSE;
1531 
1532     if (curbuf->b_ml.ml_flags & ML_EMPTY)	    /* nothing to do */
1533 	return OK;
1534 
1535     /* Nothing to delete, return here.	Do prepare undo, for op_change(). */
1536     if (oap->empty)
1537 	return u_save_cursor();
1538 
1539     if (!curbuf->b_p_ma)
1540     {
1541 	EMSG(_(e_modifiable));
1542 	return FAIL;
1543     }
1544 
1545 #ifdef FEAT_CLIPBOARD
1546     adjust_clip_reg(&oap->regname);
1547 #endif
1548 
1549 #ifdef FEAT_MBYTE
1550     if (has_mbyte)
1551 	mb_adjust_opend(oap);
1552 #endif
1553 
1554 /*
1555  * Imitate the strange Vi behaviour: If the delete spans more than one line
1556  * and motion_type == MCHAR and the result is a blank line, make the delete
1557  * linewise.  Don't do this for the change command or Visual mode.
1558  */
1559     if (       oap->motion_type == MCHAR
1560 #ifdef FEAT_VISUAL
1561 	    && !oap->is_VIsual
1562 	    && !oap->block_mode
1563 #endif
1564 	    && oap->line_count > 1
1565 	    && oap->op_type == OP_DELETE)
1566     {
1567 	ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive;
1568 	ptr = skipwhite(ptr);
1569 	if (*ptr == NUL && inindent(0))
1570 	    oap->motion_type = MLINE;
1571     }
1572 
1573 /*
1574  * Check for trying to delete (e.g. "D") in an empty line.
1575  * Note: For the change operator it is ok.
1576  */
1577     if (       oap->motion_type == MCHAR
1578 	    && oap->line_count == 1
1579 	    && oap->op_type == OP_DELETE
1580 	    && *ml_get(oap->start.lnum) == NUL)
1581     {
1582 	/*
1583 	 * It's an error to operate on an empty region, when 'E' included in
1584 	 * 'cpoptions' (Vi compatible).
1585 	 */
1586 #ifdef FEAT_VIRTUALEDIT
1587 	if (virtual_op)
1588 	    /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1589 	     * marks as if it happened. */
1590 	    goto setmarks;
1591 #endif
1592 	if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1593 	    beep_flush();
1594 	return OK;
1595     }
1596 
1597 /*
1598  * Do a yank of whatever we're about to delete.
1599  * If a yank register was specified, put the deleted text into that register.
1600  * For the black hole register '_' don't yank anything.
1601  */
1602     if (oap->regname != '_')
1603     {
1604 	if (oap->regname != 0)
1605 	{
1606 	    /* check for read-only register */
1607 	    if (!valid_yank_reg(oap->regname, TRUE))
1608 	    {
1609 		beep_flush();
1610 		return OK;
1611 	    }
1612 	    get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1613 	    if (op_yank(oap, TRUE, FALSE) == OK)   /* yank without message */
1614 		did_yank = TRUE;
1615 	}
1616 
1617 	/*
1618 	 * Put deleted text into register 1 and shift number registers if the
1619 	 * delete contains a line break, or when a regname has been specified.
1620 	 */
1621 	if (oap->regname != 0 || oap->motion_type == MLINE
1622 				   || oap->line_count > 1 || oap->use_reg_one)
1623 	{
1624 	    y_current = &y_regs[9];
1625 	    free_yank_all();			/* free register nine */
1626 	    for (n = 9; n > 1; --n)
1627 		y_regs[n] = y_regs[n - 1];
1628 	    y_previous = y_current = &y_regs[1];
1629 	    y_regs[1].y_array = NULL;		/* set register one to empty */
1630 	    if (op_yank(oap, TRUE, FALSE) == OK)
1631 		did_yank = TRUE;
1632 	}
1633 
1634 	/* Yank into small delete register when no register specified and the
1635 	 * delete is within one line. */
1636 	if (oap->regname == 0 && oap->motion_type != MLINE
1637 						      && oap->line_count == 1)
1638 	{
1639 	    oap->regname = '-';
1640 	    get_yank_register(oap->regname, TRUE);
1641 	    if (op_yank(oap, TRUE, FALSE) == OK)
1642 		did_yank = TRUE;
1643 	    oap->regname = 0;
1644 	}
1645 
1646 	/*
1647 	 * If there's too much stuff to fit in the yank register, then get a
1648 	 * confirmation before doing the delete. This is crude, but simple.
1649 	 * And it avoids doing a delete of something we can't put back if we
1650 	 * want.
1651 	 */
1652 	if (!did_yank)
1653 	{
1654 	    int msg_silent_save = msg_silent;
1655 
1656 	    msg_silent = 0;	/* must display the prompt */
1657 	    n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1658 	    msg_silent = msg_silent_save;
1659 	    if (n != 'y')
1660 	    {
1661 		EMSG(_(e_abort));
1662 		return FAIL;
1663 	    }
1664 	}
1665     }
1666 
1667 #ifdef FEAT_VISUAL
1668 /*
1669  * block mode delete
1670  */
1671     if (oap->block_mode)
1672     {
1673 	if (u_save((linenr_T)(oap->start.lnum - 1),
1674 			       (linenr_T)(oap->end.lnum + 1)) == FAIL)
1675 	    return FAIL;
1676 
1677 	for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1678 	{
1679 	    block_prep(oap, &bd, lnum, TRUE);
1680 	    if (bd.textlen == 0)	/* nothing to delete */
1681 		continue;
1682 
1683 	    /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1684 	    if (lnum == curwin->w_cursor.lnum)
1685 	    {
1686 		curwin->w_cursor.col = bd.textcol + bd.startspaces;
1687 # ifdef FEAT_VIRTUALEDIT
1688 		curwin->w_cursor.coladd = 0;
1689 # endif
1690 	    }
1691 
1692 	    /* n == number of chars deleted
1693 	     * If we delete a TAB, it may be replaced by several characters.
1694 	     * Thus the number of characters may increase!
1695 	     */
1696 	    n = bd.textlen - bd.startspaces - bd.endspaces;
1697 	    oldp = ml_get(lnum);
1698 	    newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1699 	    if (newp == NULL)
1700 		continue;
1701 	    /* copy up to deleted part */
1702 	    mch_memmove(newp, oldp, (size_t)bd.textcol);
1703 	    /* insert spaces */
1704 	    copy_spaces(newp + bd.textcol,
1705 				     (size_t)(bd.startspaces + bd.endspaces));
1706 	    /* copy the part after the deleted part */
1707 	    oldp += bd.textcol + bd.textlen;
1708 	    mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
1709 						      oldp, STRLEN(oldp) + 1);
1710 	    /* replace the line */
1711 	    ml_replace(lnum, newp, FALSE);
1712 	}
1713 
1714 	check_cursor_col();
1715 	changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1716 						       oap->end.lnum + 1, 0L);
1717 	oap->line_count = 0;	    /* no lines deleted */
1718     }
1719     else
1720 #endif
1721 	if (oap->motion_type == MLINE)
1722     {
1723 	if (oap->op_type == OP_CHANGE)
1724 	{
1725 	    /* Delete the lines except the first one.  Temporarily move the
1726 	     * cursor to the next line.  Save the current line number, if the
1727 	     * last line is deleted it may be changed.
1728 	     */
1729 	    if (oap->line_count > 1)
1730 	    {
1731 		lnum = curwin->w_cursor.lnum;
1732 		++curwin->w_cursor.lnum;
1733 		del_lines((long)(oap->line_count - 1), TRUE);
1734 		curwin->w_cursor.lnum = lnum;
1735 	    }
1736 	    if (u_save_cursor() == FAIL)
1737 		return FAIL;
1738 	    if (curbuf->b_p_ai)		    /* don't delete indent */
1739 	    {
1740 		beginline(BL_WHITE);	    /* cursor on first non-white */
1741 		did_ai = TRUE;		    /* delete the indent when ESC hit */
1742 		ai_col = curwin->w_cursor.col;
1743 	    }
1744 	    else
1745 		beginline(0);		    /* cursor in column 0 */
1746 	    truncate_line(FALSE);   /* delete the rest of the line */
1747 				    /* leave cursor past last char in line */
1748 	    if (oap->line_count > 1)
1749 		u_clearline();	    /* "U" command not possible after "2cc" */
1750 	}
1751 	else
1752 	{
1753 	    del_lines(oap->line_count, TRUE);
1754 	    beginline(BL_WHITE | BL_FIX);
1755 	    u_clearline();	/* "U" command not possible after "dd" */
1756 	}
1757     }
1758     else
1759     {
1760 #ifdef FEAT_VIRTUALEDIT
1761 	if (virtual_op)
1762 	{
1763 	    int		endcol = 0;
1764 
1765 	    /* For virtualedit: break the tabs that are partly included. */
1766 	    if (gchar_pos(&oap->start) == '\t')
1767 	    {
1768 		if (u_save_cursor() == FAIL)	/* save first line for undo */
1769 		    return FAIL;
1770 		if (oap->line_count == 1)
1771 		    endcol = getviscol2(oap->end.col, oap->end.coladd);
1772 		coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1773 		oap->start = curwin->w_cursor;
1774 		if (oap->line_count == 1)
1775 		{
1776 		    coladvance(endcol);
1777 		    oap->end.col = curwin->w_cursor.col;
1778 		    oap->end.coladd = curwin->w_cursor.coladd;
1779 		    curwin->w_cursor = oap->start;
1780 		}
1781 	    }
1782 
1783 	    /* Break a tab only when it's included in the area. */
1784 	    if (gchar_pos(&oap->end) == '\t'
1785 				     && (int)oap->end.coladd < oap->inclusive)
1786 	    {
1787 		/* save last line for undo */
1788 		if (u_save((linenr_T)(oap->end.lnum - 1),
1789 				       (linenr_T)(oap->end.lnum + 1)) == FAIL)
1790 		    return FAIL;
1791 		curwin->w_cursor = oap->end;
1792 		coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1793 		oap->end = curwin->w_cursor;
1794 		curwin->w_cursor = oap->start;
1795 	    }
1796 	}
1797 #endif
1798 
1799 	if (oap->line_count == 1)	/* delete characters within one line */
1800 	{
1801 	    if (u_save_cursor() == FAIL)	/* save line for undo */
1802 		return FAIL;
1803 
1804 	    /* if 'cpoptions' contains '$', display '$' at end of change */
1805 	    if (	   vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1806 		    && oap->op_type == OP_CHANGE
1807 		    && oap->end.lnum == curwin->w_cursor.lnum
1808 #ifdef FEAT_VISUAL
1809 		    && !oap->is_VIsual
1810 #endif
1811 		    )
1812 		display_dollar(oap->end.col - !oap->inclusive);
1813 
1814 	    n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1815 
1816 #ifdef FEAT_VIRTUALEDIT
1817 	    if (virtual_op)
1818 	    {
1819 		/* fix up things for virtualedit-delete:
1820 		 * break the tabs which are going to get in our way
1821 		 */
1822 		char_u		*curline = ml_get_curline();
1823 		int		len = (int)STRLEN(curline);
1824 
1825 		if (oap->end.coladd != 0
1826 			&& (int)oap->end.col >= len - 1
1827 			&& !(oap->start.coladd && (int)oap->end.col >= len - 1))
1828 		    n++;
1829 		/* Delete at least one char (e.g, when on a control char). */
1830 		if (n == 0 && oap->start.coladd != oap->end.coladd)
1831 		    n = 1;
1832 
1833 		/* When deleted a char in the line, reset coladd. */
1834 		if (gchar_cursor() != NUL)
1835 		    curwin->w_cursor.coladd = 0;
1836 	    }
1837 #endif
1838 	    (void)del_bytes((long)n, restart_edit == NUL && !virtual_op);
1839 	}
1840 	else				/* delete characters between lines */
1841 	{
1842 	    pos_T   curpos;
1843 
1844 	    /* save deleted and changed lines for undo */
1845 	    if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1846 		 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1847 		return FAIL;
1848 
1849 	    truncate_line(TRUE);	/* delete from cursor to end of line */
1850 
1851 	    curpos = curwin->w_cursor;	/* remember curwin->w_cursor */
1852 	    ++curwin->w_cursor.lnum;
1853 	    del_lines((long)(oap->line_count - 2), FALSE);
1854 
1855 	    /* delete from start of line until op_end */
1856 	    curwin->w_cursor.col = 0;
1857 	    (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
1858 				    restart_edit == NUL && !virtual_op);
1859 	    curwin->w_cursor = curpos;		/* restore curwin->w_cursor */
1860 
1861 	    (void)do_join(FALSE);
1862 	}
1863     }
1864 
1865     msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1866 
1867 #ifdef FEAT_VIRTUALEDIT
1868 setmarks:
1869 #endif
1870 #ifdef FEAT_VISUAL
1871     if (oap->block_mode)
1872     {
1873 	curbuf->b_op_end.lnum = oap->end.lnum;
1874 	curbuf->b_op_end.col = oap->start.col;
1875     }
1876     else
1877 #endif
1878 	curbuf->b_op_end = oap->start;
1879     curbuf->b_op_start = oap->start;
1880 
1881     return OK;
1882 }
1883 
1884 #ifdef FEAT_MBYTE
1885 /*
1886  * Adjust end of operating area for ending on a multi-byte character.
1887  * Used for deletion.
1888  */
1889     static void
1890 mb_adjust_opend(oap)
1891     oparg_T	*oap;
1892 {
1893     char_u	*p;
1894 
1895     if (oap->inclusive)
1896     {
1897 	p = ml_get(oap->end.lnum);
1898 	oap->end.col += mb_tail_off(p, p + oap->end.col);
1899     }
1900 }
1901 #endif
1902 
1903 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1904 /*
1905  * Replace a whole area with one character.
1906  */
1907     int
1908 op_replace(oap, c)
1909     oparg_T   *oap;
1910     int		c;
1911 {
1912     int			n, numc;
1913 #ifdef FEAT_MBYTE
1914     int			num_chars;
1915 #endif
1916     char_u		*newp, *oldp;
1917     size_t		oldlen;
1918     struct block_def	bd;
1919 
1920     if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1921 	return OK;	    /* nothing to do */
1922 
1923 #ifdef FEAT_MBYTE
1924     if (has_mbyte)
1925 	mb_adjust_opend(oap);
1926 #endif
1927 
1928     if (u_save((linenr_T)(oap->start.lnum - 1),
1929 				       (linenr_T)(oap->end.lnum + 1)) == FAIL)
1930 	return FAIL;
1931 
1932     /*
1933      * block mode replace
1934      */
1935     if (oap->block_mode)
1936     {
1937 	bd.is_MAX = (curwin->w_curswant == MAXCOL);
1938 	for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1939 	{
1940 	    block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1941 	    if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1942 		continue;	    /* nothing to replace */
1943 
1944 	    /* n == number of extra chars required
1945 	     * If we split a TAB, it may be replaced by several characters.
1946 	     * Thus the number of characters may increase!
1947 	     */
1948 #ifdef FEAT_VIRTUALEDIT
1949 	    /* If the range starts in virtual space, count the initial
1950 	     * coladd offset as part of "startspaces" */
1951 	    if (virtual_op && bd.is_short && *bd.textstart == NUL)
1952 	    {
1953 		pos_T vpos;
1954 
1955 		getvpos(&vpos, oap->start_vcol);
1956 		bd.startspaces += vpos.coladd;
1957 		n = bd.startspaces;
1958 	    }
1959 	    else
1960 #endif
1961 		/* allow for pre spaces */
1962 		n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
1963 
1964 	    /* allow for post spp */
1965 	    n += (bd.endspaces
1966 #ifdef FEAT_VIRTUALEDIT
1967 		    && !bd.is_oneChar
1968 #endif
1969 		    && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
1970 	    /* Figure out how many characters to replace. */
1971 	    numc = oap->end_vcol - oap->start_vcol + 1;
1972 	    if (bd.is_short && (!virtual_op || bd.is_MAX))
1973 		numc -= (oap->end_vcol - bd.end_vcol) + 1;
1974 
1975 #ifdef FEAT_MBYTE
1976 	    /* A double-wide character can be replaced only up to half the
1977 	     * times. */
1978 	    if ((*mb_char2cells)(c) > 1)
1979 	    {
1980 		if ((numc & 1) && !bd.is_short)
1981 		{
1982 		    ++bd.endspaces;
1983 		    ++n;
1984 		}
1985 		numc = numc / 2;
1986 	    }
1987 
1988 	    /* Compute bytes needed, move character count to num_chars. */
1989 	    num_chars = numc;
1990 	    numc *= (*mb_char2len)(c);
1991 #endif
1992 	    /* oldlen includes textlen, so don't double count */
1993 	    n += numc - bd.textlen;
1994 
1995 	    oldp = ml_get_curline();
1996 	    oldlen = STRLEN(oldp);
1997 	    newp = alloc_check((unsigned)oldlen + 1 + n);
1998 	    if (newp == NULL)
1999 		continue;
2000 	    vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2001 	    /* copy up to deleted part */
2002 	    mch_memmove(newp, oldp, (size_t)bd.textcol);
2003 	    oldp += bd.textcol + bd.textlen;
2004 	    /* insert pre-spaces */
2005 	    copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2006 	    /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2007 #ifdef FEAT_MBYTE
2008 	    if (has_mbyte)
2009 	    {
2010 		n = STRLEN(newp);
2011 		while (--num_chars >= 0)
2012 		    n += (*mb_char2bytes)(c, newp + n);
2013 	    }
2014 	    else
2015 #endif
2016 		copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2017 	    if (!bd.is_short)
2018 	    {
2019 		/* insert post-spaces */
2020 		copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2021 		/* copy the part after the changed part */
2022 		mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1);
2023 	    }
2024 	    /* replace the line */
2025 	    ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2026 	}
2027     }
2028     else
2029     {
2030 	/*
2031 	 * MCHAR and MLINE motion replace.
2032 	 */
2033 	if (oap->motion_type == MLINE)
2034 	{
2035 	    oap->start.col = 0;
2036 	    curwin->w_cursor.col = 0;
2037 	    oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2038 	    if (oap->end.col)
2039 		--oap->end.col;
2040 	}
2041 	else if (!oap->inclusive)
2042 	    dec(&(oap->end));
2043 
2044 	while (ltoreq(curwin->w_cursor, oap->end))
2045 	{
2046 	    n = gchar_cursor();
2047 	    if (n != NUL)
2048 	    {
2049 #ifdef FEAT_MBYTE
2050 		if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2051 		{
2052 		    /* This is slow, but it handles replacing a single-byte
2053 		     * with a multi-byte and the other way around. */
2054 		    oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2055 		    n = State;
2056 		    State = REPLACE;
2057 		    ins_char(c);
2058 		    State = n;
2059 		    /* Backup to the replaced character. */
2060 		    dec_cursor();
2061 		}
2062 		else
2063 #endif
2064 		{
2065 #ifdef FEAT_VIRTUALEDIT
2066 		    if (n == TAB)
2067 		    {
2068 			int end_vcol = 0;
2069 
2070 			if (curwin->w_cursor.lnum == oap->end.lnum)
2071 			{
2072 			    /* oap->end has to be recalculated when
2073 			     * the tab breaks */
2074 			    end_vcol = getviscol2(oap->end.col,
2075 							     oap->end.coladd);
2076 			}
2077 			coladvance_force(getviscol());
2078 			if (curwin->w_cursor.lnum == oap->end.lnum)
2079 			    getvpos(&oap->end, end_vcol);
2080 		    }
2081 #endif
2082 		    pchar(curwin->w_cursor, c);
2083 		}
2084 	    }
2085 #ifdef FEAT_VIRTUALEDIT
2086 	    else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2087 	    {
2088 		int virtcols = oap->end.coladd;
2089 
2090 		if (curwin->w_cursor.lnum == oap->start.lnum
2091 			&& oap->start.col == oap->end.col && oap->start.coladd)
2092 		    virtcols -= oap->start.coladd;
2093 
2094 		/* oap->end has been trimmed so it's effectively inclusive;
2095 		 * as a result an extra +1 must be counted so we don't
2096 		 * trample the NUL byte. */
2097 		coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2098 		curwin->w_cursor.col -= (virtcols + 1);
2099 		for (; virtcols >= 0; virtcols--)
2100 		{
2101 		    pchar(curwin->w_cursor, c);
2102 		    if (inc(&curwin->w_cursor) == -1)
2103 			break;
2104 		}
2105 	    }
2106 #endif
2107 
2108 	    /* Advance to next character, stop at the end of the file. */
2109 	    if (inc_cursor() == -1)
2110 		break;
2111 	}
2112     }
2113 
2114     curwin->w_cursor = oap->start;
2115     check_cursor();
2116     changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2117 
2118     /* Set "'[" and "']" marks. */
2119     curbuf->b_op_start = oap->start;
2120     curbuf->b_op_end = oap->end;
2121 
2122     return OK;
2123 }
2124 #endif
2125 
2126 /*
2127  * Handle the (non-standard vi) tilde operator.  Also for "gu", "gU" and "g?".
2128  */
2129     void
2130 op_tilde(oap)
2131     oparg_T	*oap;
2132 {
2133     pos_T		pos;
2134 #ifdef FEAT_VISUAL
2135     struct block_def	bd;
2136     int			done;
2137 #endif
2138     int			did_change = 0;
2139 #ifdef FEAT_MBYTE
2140     colnr_T		col;
2141 #endif
2142 
2143     if (u_save((linenr_T)(oap->start.lnum - 1),
2144 				       (linenr_T)(oap->end.lnum + 1)) == FAIL)
2145 	return;
2146 
2147     pos = oap->start;
2148 #ifdef FEAT_VISUAL
2149     if (oap->block_mode)		    /* Visual block mode */
2150     {
2151 	for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2152 	{
2153 	    block_prep(oap, &bd, pos.lnum, FALSE);
2154 	    pos.col = bd.textcol;
2155 	    for (done = 0; done < bd.textlen; ++done)
2156 	    {
2157 		did_change |= swapchar(oap->op_type, &pos);
2158 # ifdef FEAT_MBYTE
2159 		col = pos.col + 1;
2160 # endif
2161 		if (inc(&pos) == -1)	    /* at end of file */
2162 		    break;
2163 # ifdef FEAT_MBYTE
2164 		if (pos.col > col)
2165 		    /* Count extra bytes of a multi-byte character. */
2166 		    done += pos.col - col;
2167 # endif
2168 	    }
2169 # ifdef FEAT_NETBEANS_INTG
2170 	    if (usingNetbeans && did_change)
2171 	    {
2172 		char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2173 
2174 		netbeans_removed(curbuf, pos.lnum, bd.textcol,
2175 							    (long)bd.textlen);
2176 		netbeans_inserted(curbuf, pos.lnum, bd.textcol,
2177 				    &ptr[bd.textcol], bd.textlen);
2178 	    }
2179 # endif
2180 	}
2181 	if (did_change)
2182 	    changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2183     }
2184     else				    /* not block mode */
2185 #endif
2186     {
2187 	if (oap->motion_type == MLINE)
2188 	{
2189 	    oap->start.col = 0;
2190 	    pos.col = 0;
2191 	    oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2192 	    if (oap->end.col)
2193 		--oap->end.col;
2194 	}
2195 	else if (!oap->inclusive)
2196 	    dec(&(oap->end));
2197 
2198 	while (ltoreq(pos, oap->end))
2199 	{
2200 	    did_change |= swapchar(oap->op_type, &pos);
2201 	    if (inc(&pos) == -1)    /* at end of file */
2202 		break;
2203 	}
2204 	if (did_change)
2205 	{
2206 	    changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2207 									  0L);
2208 #ifdef FEAT_NETBEANS_INTG
2209 	    if (usingNetbeans && did_change)
2210 	    {
2211 		char_u *ptr;
2212 		int count;
2213 
2214 		pos = oap->start;
2215 		while (pos.lnum < oap->end.lnum)
2216 		{
2217 		    ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2218 		    count = STRLEN(ptr) - pos.col;
2219 		    netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2220 		    netbeans_inserted(curbuf, pos.lnum, pos.col,
2221 						 &ptr[pos.col], count);
2222 		    pos.col = 0;
2223 		    pos.lnum++;
2224 		}
2225 		ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2226 		count = oap->end.col - pos.col + 1;
2227 		netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2228 		netbeans_inserted(curbuf, pos.lnum, pos.col,
2229 						 &ptr[pos.col], count);
2230 	    }
2231 #endif
2232 	}
2233     }
2234 
2235 #ifdef FEAT_VISUAL
2236     if (!did_change && oap->is_VIsual)
2237 	/* No change: need to remove the Visual selection */
2238 	redraw_curbuf_later(INVERTED);
2239 #endif
2240 
2241     /*
2242      * Set '[ and '] marks.
2243      */
2244     curbuf->b_op_start = oap->start;
2245     curbuf->b_op_end = oap->end;
2246 
2247     if (oap->line_count > p_report)
2248     {
2249 	if (oap->line_count == 1)
2250 	    MSG(_("1 line changed"));
2251 	else
2252 	    smsg((char_u *)_("%ld lines changed"), oap->line_count);
2253     }
2254 }
2255 
2256 /*
2257  * If op_type == OP_UPPER: make uppercase,
2258  * if op_type == OP_LOWER: make lowercase,
2259  * if op_type == OP_ROT13: do rot13 encoding,
2260  * else swap case of character at 'pos'
2261  * returns TRUE when something actually changed.
2262  */
2263     int
2264 swapchar(op_type, pos)
2265     int	    op_type;
2266     pos_T    *pos;
2267 {
2268     int	    c;
2269     int	    nc;
2270 
2271     c = gchar_pos(pos);
2272 
2273     /* Only do rot13 encoding for ASCII characters. */
2274     if (c >= 0x80 && op_type == OP_ROT13)
2275 	return FALSE;
2276 
2277 #ifdef FEAT_MBYTE
2278     if (enc_dbcs != 0 && c >= 0x100)	/* No lower/uppercase letter */
2279 	return FALSE;
2280 #endif
2281     nc = c;
2282     if (MB_ISLOWER(c))
2283     {
2284 	if (op_type == OP_ROT13)
2285 	    nc = ROT13(c, 'a');
2286 	else if (op_type != OP_LOWER)
2287 	    nc = MB_TOUPPER(c);
2288     }
2289     else if (MB_ISUPPER(c))
2290     {
2291 	if (op_type == OP_ROT13)
2292 	    nc = ROT13(c, 'A');
2293 	else if (op_type != OP_UPPER)
2294 	    nc = MB_TOLOWER(c);
2295     }
2296     if (nc != c)
2297     {
2298 #ifdef FEAT_MBYTE
2299 	if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2300 	{
2301 	    pos_T   sp = curwin->w_cursor;
2302 
2303 	    curwin->w_cursor = *pos;
2304 	    del_char(FALSE);
2305 	    ins_char(nc);
2306 	    curwin->w_cursor = sp;
2307 	}
2308 	else
2309 #endif
2310 	    pchar(*pos, nc);
2311 	return TRUE;
2312     }
2313     return FALSE;
2314 }
2315 
2316 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2317 /*
2318  * op_insert - Insert and append operators for Visual mode.
2319  */
2320     void
2321 op_insert(oap, count1)
2322     oparg_T	*oap;
2323     long	count1;
2324 {
2325     long		ins_len, pre_textlen = 0;
2326     char_u		*firstline, *ins_text;
2327     struct block_def	bd;
2328     int			i;
2329 
2330     /* edit() changes this - record it for OP_APPEND */
2331     bd.is_MAX = (curwin->w_curswant == MAXCOL);
2332 
2333     /* vis block is still marked. Get rid of it now. */
2334     curwin->w_cursor.lnum = oap->start.lnum;
2335     update_screen(INVERTED);
2336 
2337     if (oap->block_mode)
2338     {
2339 #ifdef FEAT_VIRTUALEDIT
2340 	/* When 'virtualedit' is used, need to insert the extra spaces before
2341 	 * doing block_prep().  When only "block" is used, virtual edit is
2342 	 * already disabled, but still need it when calling
2343 	 * coladvance_force(). */
2344 	if (curwin->w_cursor.coladd > 0)
2345 	{
2346 	    int		old_ve_flags = ve_flags;
2347 
2348 	    ve_flags = VE_ALL;
2349 	    if (u_save_cursor() == FAIL)
2350 		return;
2351 	    coladvance_force(oap->op_type == OP_APPEND
2352 					   ? oap->end_vcol + 1 : getviscol());
2353 	    if (oap->op_type == OP_APPEND)
2354 		--curwin->w_cursor.col;
2355 	    ve_flags = old_ve_flags;
2356 	}
2357 #endif
2358 	/* Get the info about the block before entering the text */
2359 	block_prep(oap, &bd, oap->start.lnum, TRUE);
2360 	firstline = ml_get(oap->start.lnum) + bd.textcol;
2361 	if (oap->op_type == OP_APPEND)
2362 	    firstline += bd.textlen;
2363 	pre_textlen = (long)STRLEN(firstline);
2364     }
2365 
2366     if (oap->op_type == OP_APPEND)
2367     {
2368 	if (oap->block_mode
2369 #ifdef FEAT_VIRTUALEDIT
2370 		&& curwin->w_cursor.coladd == 0
2371 #endif
2372 	   )
2373 	{
2374 	    /* Move the cursor to the character right of the block. */
2375 	    curwin->w_set_curswant = TRUE;
2376 	    while (*ml_get_cursor() != NUL
2377 		    && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2378 		++curwin->w_cursor.col;
2379 	    if (bd.is_short && !bd.is_MAX)
2380 	    {
2381 		/* First line was too short, make it longer and adjust the
2382 		 * values in "bd". */
2383 		if (u_save_cursor() == FAIL)
2384 		    return;
2385 		for (i = 0; i < bd.endspaces; ++i)
2386 		    ins_char(' ');
2387 		bd.textlen += bd.endspaces;
2388 	    }
2389 	}
2390 	else
2391 	{
2392 	    curwin->w_cursor = oap->end;
2393 
2394 	    /* Works just like an 'i'nsert on the next character. */
2395 	    if (!lineempty(curwin->w_cursor.lnum)
2396 		    && oap->start_vcol != oap->end_vcol)
2397 		inc_cursor();
2398 	}
2399     }
2400 
2401     edit(NUL, FALSE, (linenr_T)count1);
2402 
2403     /* if user has moved off this line, we don't know what to do, so do
2404      * nothing */
2405     if (curwin->w_cursor.lnum != oap->start.lnum)
2406 	return;
2407 
2408     if (oap->block_mode)
2409     {
2410 	struct block_def	bd2;
2411 
2412 	/*
2413 	 * Spaces and tabs in the indent may have changed to other spaces and
2414 	 * tabs.  Get the starting column again and correct the lenght.
2415 	 * Don't do this when "$" used, end-of-line will have changed.
2416 	 */
2417 	block_prep(oap, &bd2, oap->start.lnum, TRUE);
2418 	if (!bd.is_MAX || bd2.textlen < bd.textlen)
2419 	{
2420 	    if (oap->op_type == OP_APPEND)
2421 	    {
2422 		pre_textlen += bd2.textlen - bd.textlen;
2423 		if (bd2.endspaces)
2424 		    --bd2.textlen;
2425 	    }
2426 	    bd.textcol = bd2.textcol;
2427 	    bd.textlen = bd2.textlen;
2428 	}
2429 
2430 	/*
2431 	 * Subsequent calls to ml_get() flush the firstline data - take a
2432 	 * copy of the required string.
2433 	 */
2434 	firstline = ml_get(oap->start.lnum) + bd.textcol;
2435 	if (oap->op_type == OP_APPEND)
2436 	    firstline += bd.textlen;
2437 	if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2438 	{
2439 	    ins_text = vim_strnsave(firstline, (int)ins_len);
2440 	    if (ins_text != NULL)
2441 	    {
2442 		/* block handled here */
2443 		if (u_save(oap->start.lnum,
2444 					 (linenr_T)(oap->end.lnum + 1)) == OK)
2445 		    block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2446 									 &bd);
2447 
2448 		curwin->w_cursor.col = oap->start.col;
2449 		check_cursor();
2450 		vim_free(ins_text);
2451 	    }
2452 	}
2453     }
2454 }
2455 #endif
2456 
2457 /*
2458  * op_change - handle a change operation
2459  *
2460  * return TRUE if edit() returns because of a CTRL-O command
2461  */
2462     int
2463 op_change(oap)
2464     oparg_T	*oap;
2465 {
2466     colnr_T		l;
2467     int			retval;
2468 #ifdef FEAT_VISUALEXTRA
2469     long		offset;
2470     linenr_T		linenr;
2471     long		ins_len, pre_textlen = 0;
2472     char_u		*firstline;
2473     char_u		*ins_text, *newp, *oldp;
2474     struct block_def	bd;
2475 #endif
2476 
2477     l = oap->start.col;
2478     if (oap->motion_type == MLINE)
2479     {
2480 	l = 0;
2481 #ifdef FEAT_SMARTINDENT
2482 	if (!p_paste && curbuf->b_p_si
2483 # ifdef FEAT_CINDENT
2484 		&& !curbuf->b_p_cin
2485 # endif
2486 		)
2487 	    can_si = TRUE;	/* It's like opening a new line, do si */
2488 #endif
2489     }
2490 
2491     /* First delete the text in the region.  In an empty buffer only need to
2492      * save for undo */
2493     if (curbuf->b_ml.ml_flags & ML_EMPTY)
2494     {
2495 	if (u_save_cursor() == FAIL)
2496 	    return FALSE;
2497     }
2498     else if (op_delete(oap) == FAIL)
2499 	return FALSE;
2500 
2501     if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2502 							 && !virtual_op)
2503 	inc_cursor();
2504 
2505 #ifdef FEAT_VISUALEXTRA
2506     /* check for still on same line (<CR> in inserted text meaningless) */
2507     /* skip blank lines too */
2508     if (oap->block_mode)
2509     {
2510 # ifdef FEAT_VIRTUALEDIT
2511 	/* Add spaces before getting the current line length. */
2512 	if (virtual_op && (curwin->w_cursor.coladd > 0
2513 						    || gchar_cursor() == NUL))
2514 	    coladvance_force(getviscol());
2515 # endif
2516 	pre_textlen = (long)STRLEN(ml_get(oap->start.lnum));
2517 	bd.textcol = curwin->w_cursor.col;
2518     }
2519 #endif
2520 
2521 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2522     if (oap->motion_type == MLINE)
2523 	fix_indent();
2524 #endif
2525 
2526     retval = edit(NUL, FALSE, (linenr_T)1);
2527 
2528 #ifdef FEAT_VISUALEXTRA
2529     /*
2530      * In Visual block mode, handle copying the new text to all lines of the
2531      * block.
2532      */
2533     if (oap->block_mode && oap->start.lnum != oap->end.lnum)
2534     {
2535 	firstline = ml_get(oap->start.lnum);
2536 	/*
2537 	 * Subsequent calls to ml_get() flush the firstline data - take a
2538 	 * copy of the required bit.
2539 	 */
2540 	if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2541 	{
2542 	    if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2543 	    {
2544 		vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
2545 		for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2546 								     linenr++)
2547 		{
2548 		    block_prep(oap, &bd, linenr, TRUE);
2549 		    if (!bd.is_short || virtual_op)
2550 		    {
2551 # ifdef FEAT_VIRTUALEDIT
2552 			pos_T vpos;
2553 
2554 			/* If the block starts in virtual space, count the
2555 			 * initial coladd offset as part of "startspaces" */
2556 			if (bd.is_short)
2557 			{
2558 			    linenr_T lnum = curwin->w_cursor.lnum;
2559 
2560 			    curwin->w_cursor.lnum = linenr;
2561 			    (void)getvpos(&vpos, oap->start_vcol);
2562 			    curwin->w_cursor.lnum = lnum;
2563 			}
2564 			else
2565 			    vpos.coladd = 0;
2566 # endif
2567 			oldp = ml_get(linenr);
2568 			newp = alloc_check((unsigned)(STRLEN(oldp)
2569 # ifdef FEAT_VIRTUALEDIT
2570 							+ vpos.coladd
2571 # endif
2572 							      + ins_len + 1));
2573 			if (newp == NULL)
2574 			    continue;
2575 			/* copy up to block start */
2576 			mch_memmove(newp, oldp, (size_t)bd.textcol);
2577 			offset = bd.textcol;
2578 # ifdef FEAT_VIRTUALEDIT
2579 			copy_spaces(newp + offset, (size_t)vpos.coladd);
2580 			offset += vpos.coladd;
2581 # endif
2582 			mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2583 			offset += ins_len;
2584 			oldp += bd.textcol;
2585 			mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2586 			ml_replace(linenr, newp, FALSE);
2587 		    }
2588 		}
2589 		check_cursor();
2590 
2591 		changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2592 	    }
2593 	    vim_free(ins_text);
2594 	}
2595     }
2596 #endif
2597 
2598     return retval;
2599 }
2600 
2601 /*
2602  * set all the yank registers to empty (called from main())
2603  */
2604     void
2605 init_yank()
2606 {
2607     int		i;
2608 
2609     for (i = 0; i < NUM_REGISTERS; ++i)
2610 	y_regs[i].y_array = NULL;
2611 }
2612 
2613 #if defined(EXITFREE) || defined(PROTO)
2614     void
2615 clear_registers()
2616 {
2617     int		i;
2618 
2619     for (i = 0; i < NUM_REGISTERS; ++i)
2620     {
2621 	y_current = &y_regs[i];
2622 	if (y_current->y_array != NULL)
2623 	    free_yank_all();
2624     }
2625 }
2626 #endif
2627 
2628 /*
2629  * Free "n" lines from the current yank register.
2630  * Called for normal freeing and in case of error.
2631  */
2632     static void
2633 free_yank(n)
2634     long	n;
2635 {
2636     if (y_current->y_array != NULL)
2637     {
2638 	long	    i;
2639 
2640 	for (i = n; --i >= 0; )
2641 	{
2642 #ifdef AMIGA	    /* only for very slow machines */
2643 	    if ((i & 1023) == 1023)  /* this may take a while */
2644 	    {
2645 		/*
2646 		 * This message should never cause a hit-return message.
2647 		 * Overwrite this message with any next message.
2648 		 */
2649 		++no_wait_return;
2650 		smsg((char_u *)_("freeing %ld lines"), i + 1);
2651 		--no_wait_return;
2652 		msg_didout = FALSE;
2653 		msg_col = 0;
2654 	    }
2655 #endif
2656 	    vim_free(y_current->y_array[i]);
2657 	}
2658 	vim_free(y_current->y_array);
2659 	y_current->y_array = NULL;
2660 #ifdef AMIGA
2661 	if (n >= 1000)
2662 	    MSG("");
2663 #endif
2664     }
2665 }
2666 
2667     static void
2668 free_yank_all()
2669 {
2670     free_yank(y_current->y_size);
2671 }
2672 
2673 /*
2674  * Yank the text between "oap->start" and "oap->end" into a yank register.
2675  * If we are to append (uppercase register), we first yank into a new yank
2676  * register and then concatenate the old and the new one (so we keep the old
2677  * one in case of out-of-memory).
2678  *
2679  * return FAIL for failure, OK otherwise
2680  */
2681     int
2682 op_yank(oap, deleting, mess)
2683     oparg_T   *oap;
2684     int	    deleting;
2685     int	    mess;
2686 {
2687     long		y_idx;		/* index in y_array[] */
2688     struct yankreg	*curr;		/* copy of y_current */
2689     struct yankreg	newreg;		/* new yank register when appending */
2690     char_u		**new_ptr;
2691     linenr_T		lnum;		/* current line number */
2692     long		j;
2693     int			yanktype = oap->motion_type;
2694     long		yanklines = oap->line_count;
2695     linenr_T		yankendlnum = oap->end.lnum;
2696     char_u		*p;
2697     char_u		*pnew;
2698     struct block_def	bd;
2699 
2700 				    /* check for read-only register */
2701     if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2702     {
2703 	beep_flush();
2704 	return FAIL;
2705     }
2706     if (oap->regname == '_')	    /* black hole: nothing to do */
2707 	return OK;
2708 
2709 #ifdef FEAT_CLIPBOARD
2710     if (!clip_star.available && oap->regname == '*')
2711 	oap->regname = 0;
2712     else if (!clip_plus.available && oap->regname == '+')
2713 	oap->regname = 0;
2714 #endif
2715 
2716     if (!deleting)		    /* op_delete() already set y_current */
2717 	get_yank_register(oap->regname, TRUE);
2718 
2719     curr = y_current;
2720 				    /* append to existing contents */
2721     if (y_append && y_current->y_array != NULL)
2722 	y_current = &newreg;
2723     else
2724 	free_yank_all();	    /* free previously yanked lines */
2725 
2726 /*
2727  * If the cursor was in column 1 before and after the movement, and the
2728  * operator is not inclusive, the yank is always linewise.
2729  */
2730     if (       oap->motion_type == MCHAR
2731 	    && oap->start.col == 0
2732 	    && !oap->inclusive
2733 #ifdef FEAT_VISUAL
2734 	    && (!oap->is_VIsual || *p_sel == 'o')
2735 	    && !oap->block_mode
2736 #endif
2737 	    && oap->end.col == 0
2738 	    && yanklines > 1)
2739     {
2740 	yanktype = MLINE;
2741 	--yankendlnum;
2742 	--yanklines;
2743     }
2744 
2745     y_current->y_size = yanklines;
2746     y_current->y_type = yanktype;   /* set the yank register type */
2747 #ifdef FEAT_VISUAL
2748     y_current->y_width = 0;
2749 #endif
2750     y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2751 							    yanklines), TRUE);
2752 
2753     if (y_current->y_array == NULL)
2754     {
2755 	y_current = curr;
2756 	return FAIL;
2757     }
2758 
2759     y_idx = 0;
2760     lnum = oap->start.lnum;
2761 
2762 #ifdef FEAT_VISUAL
2763     if (oap->block_mode)
2764     {
2765 	/* Visual block mode */
2766 	y_current->y_type = MBLOCK;	    /* set the yank register type */
2767 	y_current->y_width = oap->end_vcol - oap->start_vcol;
2768 
2769 	if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2770 	    y_current->y_width--;
2771     }
2772 #endif
2773 
2774     for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2775     {
2776 	switch (y_current->y_type)
2777 	{
2778 #ifdef FEAT_VISUAL
2779 	    case MBLOCK:
2780 		block_prep(oap, &bd, lnum, FALSE);
2781 		if (yank_copy_line(&bd, y_idx) == FAIL)
2782 		    goto fail;
2783 		break;
2784 #endif
2785 
2786 	    case MLINE:
2787 		if ((y_current->y_array[y_idx] =
2788 			    vim_strsave(ml_get(lnum))) == NULL)
2789 		    goto fail;
2790 		break;
2791 
2792 	    case MCHAR:
2793 		{
2794 		    colnr_T startcol = 0, endcol = MAXCOL;
2795 #ifdef FEAT_VIRTUALEDIT
2796 		    int is_oneChar = FALSE;
2797 		    colnr_T cs, ce;
2798 #endif
2799 		    p = ml_get(lnum);
2800 		    bd.startspaces = 0;
2801 		    bd.endspaces = 0;
2802 
2803 		    if (lnum == oap->start.lnum)
2804 		    {
2805 			startcol = oap->start.col;
2806 #ifdef FEAT_VIRTUALEDIT
2807 			if (virtual_op)
2808 			{
2809 			    getvcol(curwin, &oap->start, &cs, NULL, &ce);
2810 			    if (ce != cs && oap->start.coladd > 0)
2811 			    {
2812 				/* Part of a tab selected -- but don't
2813 				 * double-count it. */
2814 				bd.startspaces = (ce - cs + 1)
2815 							  - oap->start.coladd;
2816 				startcol++;
2817 			    }
2818 			}
2819 #endif
2820 		    }
2821 
2822 		    if (lnum == oap->end.lnum)
2823 		    {
2824 			endcol = oap->end.col;
2825 #ifdef FEAT_VIRTUALEDIT
2826 			if (virtual_op)
2827 			{
2828 			    getvcol(curwin, &oap->end, &cs, NULL, &ce);
2829 			    if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2830 # ifdef FEAT_MBYTE
2831 					/* Don't add space for double-wide
2832 					 * char; endcol will be on last byte
2833 					 * of multi-byte char. */
2834 					&& (*mb_head_off)(p, p + endcol) == 0
2835 # endif
2836 					))
2837 			    {
2838 				if (oap->start.lnum == oap->end.lnum
2839 					    && oap->start.col == oap->end.col)
2840 				{
2841 				    /* Special case: inside a single char */
2842 				    is_oneChar = TRUE;
2843 				    bd.startspaces = oap->end.coladd
2844 					 - oap->start.coladd + oap->inclusive;
2845 				    endcol = startcol;
2846 				}
2847 				else
2848 				{
2849 				    bd.endspaces = oap->end.coladd
2850 							     + oap->inclusive;
2851 				    endcol -= oap->inclusive;
2852 				}
2853 			    }
2854 			}
2855 #endif
2856 		    }
2857 		    if (startcol > endcol
2858 #ifdef FEAT_VIRTUALEDIT
2859 			    || is_oneChar
2860 #endif
2861 			    )
2862 			bd.textlen = 0;
2863 		    else
2864 		    {
2865 			if (endcol == MAXCOL)
2866 			    endcol = STRLEN(p);
2867 			bd.textlen = endcol - startcol + oap->inclusive;
2868 		    }
2869 		    bd.textstart = p + startcol;
2870 		    if (yank_copy_line(&bd, y_idx) == FAIL)
2871 			goto fail;
2872 		    break;
2873 		}
2874 		/* NOTREACHED */
2875 	}
2876     }
2877 
2878     if (curr != y_current)	/* append the new block to the old block */
2879     {
2880 	new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2881 				   (curr->y_size + y_current->y_size)), TRUE);
2882 	if (new_ptr == NULL)
2883 	    goto fail;
2884 	for (j = 0; j < curr->y_size; ++j)
2885 	    new_ptr[j] = curr->y_array[j];
2886 	vim_free(curr->y_array);
2887 	curr->y_array = new_ptr;
2888 
2889 	if (yanktype == MLINE)	/* MLINE overrides MCHAR and MBLOCK */
2890 	    curr->y_type = MLINE;
2891 
2892 	/* Concatenate the last line of the old block with the first line of
2893 	 * the new block, unless being Vi compatible. */
2894 	if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
2895 	{
2896 	    pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2897 			      + STRLEN(y_current->y_array[0]) + 1), TRUE);
2898 	    if (pnew == NULL)
2899 	    {
2900 		y_idx = y_current->y_size - 1;
2901 		goto fail;
2902 	    }
2903 	    STRCPY(pnew, curr->y_array[--j]);
2904 	    STRCAT(pnew, y_current->y_array[0]);
2905 	    vim_free(curr->y_array[j]);
2906 	    vim_free(y_current->y_array[0]);
2907 	    curr->y_array[j++] = pnew;
2908 	    y_idx = 1;
2909 	}
2910 	else
2911 	    y_idx = 0;
2912 	while (y_idx < y_current->y_size)
2913 	    curr->y_array[j++] = y_current->y_array[y_idx++];
2914 	curr->y_size = j;
2915 	vim_free(y_current->y_array);
2916 	y_current = curr;
2917     }
2918     if (mess)			/* Display message about yank? */
2919     {
2920 	if (yanktype == MCHAR
2921 #ifdef FEAT_VISUAL
2922 		&& !oap->block_mode
2923 #endif
2924 		&& yanklines == 1)
2925 	    yanklines = 0;
2926 	/* Some versions of Vi use ">=" here, some don't...  */
2927 	if (yanklines > p_report)
2928 	{
2929 	    /* redisplay now, so message is not deleted */
2930 	    update_topline_redraw();
2931 	    if (yanklines == 1)
2932 	    {
2933 #ifdef FEAT_VISUAL
2934 		if (oap->block_mode)
2935 		    MSG(_("block of 1 line yanked"));
2936 		else
2937 #endif
2938 		    MSG(_("1 line yanked"));
2939 	    }
2940 #ifdef FEAT_VISUAL
2941 	    else if (oap->block_mode)
2942 		smsg((char_u *)_("block of %ld lines yanked"), yanklines);
2943 #endif
2944 	    else
2945 		smsg((char_u *)_("%ld lines yanked"), yanklines);
2946 	}
2947     }
2948 
2949     /*
2950      * Set "'[" and "']" marks.
2951      */
2952     curbuf->b_op_start = oap->start;
2953     curbuf->b_op_end = oap->end;
2954     if (yanktype == MLINE
2955 #ifdef FEAT_VISUAL
2956 		&& !oap->block_mode
2957 #endif
2958        )
2959     {
2960 	curbuf->b_op_start.col = 0;
2961 	curbuf->b_op_end.col = MAXCOL;
2962     }
2963 
2964 #ifdef FEAT_CLIPBOARD
2965     /*
2966      * If we were yanking to the '*' register, send result to clipboard.
2967      * If no register was specified, and "unnamed" in 'clipboard', make a copy
2968      * to the '*' register.
2969      */
2970     if (clip_star.available
2971 	    && (curr == &(y_regs[STAR_REGISTER])
2972 		|| (!deleting && oap->regname == 0 && clip_unnamed)))
2973     {
2974 	if (curr != &(y_regs[STAR_REGISTER]))
2975 	    /* Copy the text from register 0 to the clipboard register. */
2976 	    copy_yank_reg(&(y_regs[STAR_REGISTER]));
2977 
2978 	clip_own_selection(&clip_star);
2979 	clip_gen_set_selection(&clip_star);
2980     }
2981 
2982 # ifdef FEAT_X11
2983     /*
2984      * If we were yanking to the '+' register, send result to selection.
2985      * Also copy to the '*' register, in case auto-select is off.
2986      */
2987     else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
2988     {
2989 	/* No need to copy to * register upon 'unnamed' now - see below */
2990 	clip_own_selection(&clip_plus);
2991 	clip_gen_set_selection(&clip_plus);
2992 	if (!clip_isautosel())
2993 	{
2994 	    copy_yank_reg(&(y_regs[STAR_REGISTER]));
2995 	    clip_own_selection(&clip_star);
2996 	    clip_gen_set_selection(&clip_star);
2997 	}
2998     }
2999 # endif
3000 #endif
3001 
3002     return OK;
3003 
3004 fail:		/* free the allocated lines */
3005     free_yank(y_idx + 1);
3006     y_current = curr;
3007     return FAIL;
3008 }
3009 
3010     static int
3011 yank_copy_line(bd, y_idx)
3012     struct block_def	*bd;
3013     long		y_idx;
3014 {
3015     char_u	*pnew;
3016 
3017     if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3018 								      == NULL)
3019 	return FAIL;
3020     y_current->y_array[y_idx] = pnew;
3021     copy_spaces(pnew, (size_t)bd->startspaces);
3022     pnew += bd->startspaces;
3023     mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3024     pnew += bd->textlen;
3025     copy_spaces(pnew, (size_t)bd->endspaces);
3026     pnew += bd->endspaces;
3027     *pnew = NUL;
3028     return OK;
3029 }
3030 
3031 #ifdef FEAT_CLIPBOARD
3032 /*
3033  * Make a copy of the y_current register to register "reg".
3034  */
3035     static void
3036 copy_yank_reg(reg)
3037     struct yankreg *reg;
3038 {
3039     struct yankreg	*curr = y_current;
3040     long		j;
3041 
3042     y_current = reg;
3043     free_yank_all();
3044     *y_current = *curr;
3045     y_current->y_array = (char_u **)lalloc_clear(
3046 			(long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3047     if (y_current->y_array == NULL)
3048 	y_current->y_size = 0;
3049     else
3050 	for (j = 0; j < y_current->y_size; ++j)
3051 	    if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3052 	    {
3053 		free_yank(j);
3054 		y_current->y_size = 0;
3055 		break;
3056 	    }
3057     y_current = curr;
3058 }
3059 #endif
3060 
3061 /*
3062  * Put contents of register "regname" into the text.
3063  * Caller must check "regname" to be valid!
3064  * "flags": PUT_FIXINDENT	make indent look nice
3065  *	    PUT_CURSEND		leave cursor after end of new text
3066  *	    PUT_LINE		force linewise put (":put")
3067  */
3068     void
3069 do_put(regname, dir, count, flags)
3070     int		regname;
3071     int		dir;		/* BACKWARD for 'P', FORWARD for 'p' */
3072     long	count;
3073     int		flags;
3074 {
3075     char_u	*ptr;
3076     char_u	*newp, *oldp;
3077     int		yanklen;
3078     int		totlen = 0;		/* init for gcc */
3079     linenr_T	lnum;
3080     colnr_T	col;
3081     long	i;			/* index in y_array[] */
3082     int		y_type;
3083     long	y_size;
3084 #ifdef FEAT_VISUAL
3085     int		oldlen;
3086     long	y_width = 0;
3087     colnr_T	vcol;
3088     int		delcount;
3089     int		incr = 0;
3090     long	j;
3091     struct block_def bd;
3092 #endif
3093     char_u	**y_array = NULL;
3094     long	nr_lines = 0;
3095     pos_T	new_cursor;
3096     int		indent;
3097     int		orig_indent = 0;	/* init for gcc */
3098     int		indent_diff = 0;	/* init for gcc */
3099     int		first_indent = TRUE;
3100     int		lendiff = 0;
3101     pos_T	old_pos;
3102     char_u	*insert_string = NULL;
3103     int		allocated = FALSE;
3104     long	cnt;
3105 
3106 #ifdef FEAT_CLIPBOARD
3107     /* Adjust register name for "unnamed" in 'clipboard'. */
3108     adjust_clip_reg(&regname);
3109     (void)may_get_selection(regname);
3110 #endif
3111 
3112     if (flags & PUT_FIXINDENT)
3113 	orig_indent = get_indent();
3114 
3115     curbuf->b_op_start = curwin->w_cursor;	/* default for '[ mark */
3116     curbuf->b_op_end = curwin->w_cursor;	/* default for '] mark */
3117 
3118     /*
3119      * Using inserted text works differently, because the register includes
3120      * special characters (newlines, etc.).
3121      */
3122     if (regname == '.')
3123     {
3124 	(void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3125 				    (count == -1 ? 'O' : 'i')), count, FALSE);
3126 	/* Putting the text is done later, so can't really move the cursor to
3127 	 * the next character.  Use "l" to simulate it. */
3128 	if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3129 	    stuffcharReadbuff('l');
3130 	return;
3131     }
3132 
3133     /*
3134      * For special registers '%' (file name), '#' (alternate file name) and
3135      * ':' (last command line), etc. we have to create a fake yank register.
3136      */
3137     if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3138     {
3139 	if (insert_string == NULL)
3140 	    return;
3141     }
3142 
3143     if (insert_string != NULL)
3144     {
3145 	y_type = MCHAR;
3146 #ifdef FEAT_EVAL
3147 	if (regname == '=')
3148 	{
3149 	    /* For the = register we need to split the string at NL
3150 	     * characters. */
3151 	    /* Loop twice: count the number of lines and save them. */
3152 	    for (;;)
3153 	    {
3154 		y_size = 0;
3155 		ptr = insert_string;
3156 		while (ptr != NULL)
3157 		{
3158 		    if (y_array != NULL)
3159 			y_array[y_size] = ptr;
3160 		    ++y_size;
3161 		    ptr = vim_strchr(ptr, '\n');
3162 		    if (ptr != NULL)
3163 		    {
3164 			if (y_array != NULL)
3165 			    *ptr = NUL;
3166 			++ptr;
3167 			/* A trailing '\n' makes the string linewise */
3168 			if (*ptr == NUL)
3169 			{
3170 			    y_type = MLINE;
3171 			    break;
3172 			}
3173 		    }
3174 		}
3175 		if (y_array != NULL)
3176 		    break;
3177 		y_array = (char_u **)alloc((unsigned)
3178 						 (y_size * sizeof(char_u *)));
3179 		if (y_array == NULL)
3180 		    goto end;
3181 	    }
3182 	}
3183 	else
3184 #endif
3185 	{
3186 	    y_size = 1;		/* use fake one-line yank register */
3187 	    y_array = &insert_string;
3188 	}
3189     }
3190     else
3191     {
3192 	get_yank_register(regname, FALSE);
3193 
3194 	y_type = y_current->y_type;
3195 #ifdef FEAT_VISUAL
3196 	y_width = y_current->y_width;
3197 #endif
3198 	y_size = y_current->y_size;
3199 	y_array = y_current->y_array;
3200     }
3201 
3202 #ifdef FEAT_VISUAL
3203     if (y_type == MLINE)
3204     {
3205 	if (flags & PUT_LINE_SPLIT)
3206 	{
3207 	    /* "p" or "P" in Visual mode: split the lines to put the text in
3208 	     * between. */
3209 	    if (u_save_cursor() == FAIL)
3210 		goto end;
3211 	    ptr = vim_strsave(ml_get_cursor());
3212 	    if (ptr == NULL)
3213 		goto end;
3214 	    ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3215 	    vim_free(ptr);
3216 
3217 	    ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3218 	    if (ptr == NULL)
3219 		goto end;
3220 	    ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3221 	    ++nr_lines;
3222 	    dir = FORWARD;
3223 	}
3224 	if (flags & PUT_LINE_FORWARD)
3225 	{
3226 	    /* Must be "p" for a Visual block, put lines below the block. */
3227 	    curwin->w_cursor = curbuf->b_visual_end;
3228 	    dir = FORWARD;
3229 	}
3230 	curbuf->b_op_start = curwin->w_cursor;	/* default for '[ mark */
3231 	curbuf->b_op_end = curwin->w_cursor;	/* default for '] mark */
3232     }
3233 #endif
3234 
3235     if (flags & PUT_LINE)	/* :put command or "p" in Visual line mode. */
3236 	y_type = MLINE;
3237 
3238     if (y_size == 0 || y_array == NULL)
3239     {
3240 	EMSG2(_("E353: Nothing in register %s"),
3241 		  regname == 0 ? (char_u *)"\"" : transchar(regname));
3242 	goto end;
3243     }
3244 
3245 #ifdef FEAT_VISUAL
3246     if (y_type == MBLOCK)
3247     {
3248 	lnum = curwin->w_cursor.lnum + y_size + 1;
3249 	if (lnum > curbuf->b_ml.ml_line_count)
3250 	    lnum = curbuf->b_ml.ml_line_count + 1;
3251 	if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3252 	    goto end;
3253     }
3254     else
3255 #endif
3256 	if (y_type == MLINE)
3257     {
3258 	lnum = curwin->w_cursor.lnum;
3259 #ifdef FEAT_FOLDING
3260 	/* Correct line number for closed fold.  Don't move the cursor yet,
3261 	 * u_save() uses it. */
3262 	if (dir == BACKWARD)
3263 	    (void)hasFolding(lnum, &lnum, NULL);
3264 	else
3265 	    (void)hasFolding(lnum, NULL, &lnum);
3266 #endif
3267 	if (dir == FORWARD)
3268 	    ++lnum;
3269 	if (u_save(lnum - 1, lnum) == FAIL)
3270 	    goto end;
3271 #ifdef FEAT_FOLDING
3272 	if (dir == FORWARD)
3273 	    curwin->w_cursor.lnum = lnum - 1;
3274 	else
3275 	    curwin->w_cursor.lnum = lnum;
3276 	curbuf->b_op_start = curwin->w_cursor;	/* for mark_adjust() */
3277 #endif
3278     }
3279     else if (u_save_cursor() == FAIL)
3280 	goto end;
3281 
3282     yanklen = (int)STRLEN(y_array[0]);
3283 
3284 #ifdef FEAT_VIRTUALEDIT
3285     if (ve_flags == VE_ALL && y_type == MCHAR)
3286     {
3287 	if (gchar_cursor() == TAB)
3288 	{
3289 	    /* Don't need to insert spaces when "p" on the last position of a
3290 	     * tab or "P" on the first position. */
3291 	    if (dir == FORWARD
3292 		    ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3293 						: curwin->w_cursor.coladd > 0)
3294 		coladvance_force(getviscol());
3295 	    else
3296 		curwin->w_cursor.coladd = 0;
3297 	}
3298 	else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3299 	    coladvance_force(getviscol() + (dir == FORWARD));
3300     }
3301 #endif
3302 
3303     lnum = curwin->w_cursor.lnum;
3304     col = curwin->w_cursor.col;
3305 
3306 #ifdef FEAT_VISUAL
3307     /*
3308      * Block mode
3309      */
3310     if (y_type == MBLOCK)
3311     {
3312 	char	c = gchar_cursor();
3313 	colnr_T	endcol2 = 0;
3314 
3315 	if (dir == FORWARD && c != NUL)
3316 	{
3317 #ifdef FEAT_VIRTUALEDIT
3318 	    if (ve_flags == VE_ALL)
3319 		getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3320 	    else
3321 #endif
3322 		getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3323 
3324 #ifdef FEAT_MBYTE
3325 	    if (has_mbyte)
3326 		/* move to start of next multi-byte character */
3327 		curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
3328 	    else
3329 #endif
3330 #ifdef FEAT_VIRTUALEDIT
3331 	    if (c != TAB || ve_flags != VE_ALL)
3332 #endif
3333 		++curwin->w_cursor.col;
3334 	    ++col;
3335 	}
3336 	else
3337 	    getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3338 
3339 #ifdef FEAT_VIRTUALEDIT
3340 	col += curwin->w_cursor.coladd;
3341 	if (ve_flags == VE_ALL && curwin->w_cursor.coladd > 0)
3342 	{
3343 	    if (dir == FORWARD && c == NUL)
3344 		++col;
3345 	    if (dir != FORWARD && c != NUL)
3346 		++curwin->w_cursor.col;
3347 	    if (c == TAB)
3348 	    {
3349 		if (dir == BACKWARD && curwin->w_cursor.col)
3350 		    curwin->w_cursor.col--;
3351 		if (dir == FORWARD && col - 1 == endcol2)
3352 		    curwin->w_cursor.col++;
3353 	    }
3354 	}
3355 	curwin->w_cursor.coladd = 0;
3356 #endif
3357 	for (i = 0; i < y_size; ++i)
3358 	{
3359 	    int spaces;
3360 	    char shortline;
3361 
3362 	    bd.startspaces = 0;
3363 	    bd.endspaces = 0;
3364 	    bd.textcol = 0;
3365 	    vcol = 0;
3366 	    delcount = 0;
3367 
3368 	    /* add a new line */
3369 	    if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3370 	    {
3371 		if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3372 						   (colnr_T)1, FALSE) == FAIL)
3373 		    break;
3374 		++nr_lines;
3375 	    }
3376 	    /* get the old line and advance to the position to insert at */
3377 	    oldp = ml_get_curline();
3378 	    oldlen = (int)STRLEN(oldp);
3379 	    for (ptr = oldp; vcol < col && *ptr; )
3380 	    {
3381 		/* Count a tab for what it's worth (if list mode not on) */
3382 		incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3383 		vcol += incr;
3384 	    }
3385 	    bd.textcol = (colnr_T)(ptr - oldp);
3386 
3387 	    shortline = (vcol < col) || (vcol == col && !*ptr) ;
3388 
3389 	    if (vcol < col) /* line too short, padd with spaces */
3390 		bd.startspaces = col - vcol;
3391 	    else if (vcol > col)
3392 	    {
3393 		bd.endspaces = vcol - col;
3394 		bd.startspaces = incr - bd.endspaces;
3395 		--bd.textcol;
3396 		delcount = 1;
3397 #ifdef FEAT_MBYTE
3398 		if (has_mbyte)
3399 		    bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3400 #endif
3401 		if (oldp[bd.textcol] != TAB)
3402 		{
3403 		    /* Only a Tab can be split into spaces.  Other
3404 		     * characters will have to be moved to after the
3405 		     * block, causing misalignment. */
3406 		    delcount = 0;
3407 		    bd.endspaces = 0;
3408 		}
3409 	    }
3410 
3411 	    yanklen = (int)STRLEN(y_array[i]);
3412 
3413 	    /* calculate number of spaces required to fill right side of block*/
3414 	    spaces = y_width + 1;
3415 	    for (j = 0; j < yanklen; j++)
3416 		spaces -= lbr_chartabsize(&y_array[i][j], 0);
3417 	    if (spaces < 0)
3418 		spaces = 0;
3419 
3420 	    /* insert the new text */
3421 	    totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3422 	    newp = alloc_check((unsigned)totlen + oldlen + 1);
3423 	    if (newp == NULL)
3424 		break;
3425 	    /* copy part up to cursor to new line */
3426 	    ptr = newp;
3427 	    mch_memmove(ptr, oldp, (size_t)bd.textcol);
3428 	    ptr += bd.textcol;
3429 	    /* may insert some spaces before the new text */
3430 	    copy_spaces(ptr, (size_t)bd.startspaces);
3431 	    ptr += bd.startspaces;
3432 	    /* insert the new text */
3433 	    for (j = 0; j < count; ++j)
3434 	    {
3435 		mch_memmove(ptr, y_array[i], (size_t)yanklen);
3436 		ptr += yanklen;
3437 
3438 		/* insert block's trailing spaces only if there's text behind */
3439 		if ((j < count - 1 || !shortline) && spaces)
3440 		{
3441 		    copy_spaces(ptr, (size_t)spaces);
3442 		    ptr += spaces;
3443 		}
3444 	    }
3445 	    /* may insert some spaces after the new text */
3446 	    copy_spaces(ptr, (size_t)bd.endspaces);
3447 	    ptr += bd.endspaces;
3448 	    /* move the text after the cursor to the end of the line. */
3449 	    mch_memmove(ptr, oldp + bd.textcol + delcount,
3450 				(size_t)(oldlen - bd.textcol - delcount + 1));
3451 	    ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3452 
3453 	    ++curwin->w_cursor.lnum;
3454 	    if (i == 0)
3455 		curwin->w_cursor.col += bd.startspaces;
3456 	}
3457 
3458 	changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3459 
3460 	/* Set '[ mark. */
3461 	curbuf->b_op_start = curwin->w_cursor;
3462 	curbuf->b_op_start.lnum = lnum;
3463 
3464 	/* adjust '] mark */
3465 	curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3466 	curbuf->b_op_end.col = bd.textcol + totlen - 1;
3467 # ifdef FEAT_VIRTUALEDIT
3468 	curbuf->b_op_end.coladd = 0;
3469 # endif
3470 	if (flags & PUT_CURSEND)
3471 	{
3472 	    curwin->w_cursor = curbuf->b_op_end;
3473 	    curwin->w_cursor.col++;
3474 	}
3475 	else
3476 	    curwin->w_cursor.lnum = lnum;
3477     }
3478     else
3479 #endif
3480     {
3481 	/*
3482 	 * Character or Line mode
3483 	 */
3484 	if (y_type == MCHAR)
3485 	{
3486 	    /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3487 	     * char */
3488 	    if (dir == FORWARD && gchar_cursor() != NUL)
3489 	    {
3490 #ifdef FEAT_MBYTE
3491 		if (has_mbyte)
3492 		{
3493 		    int bytelen = (*mb_ptr2len)(ml_get_cursor());
3494 
3495 		    /* put it on the next of the multi-byte character. */
3496 		    col += bytelen;
3497 		    if (yanklen)
3498 		    {
3499 			curwin->w_cursor.col += bytelen;
3500 			curbuf->b_op_end.col += bytelen;
3501 		    }
3502 		}
3503 		else
3504 #endif
3505 		{
3506 		    ++col;
3507 		    if (yanklen)
3508 		    {
3509 			++curwin->w_cursor.col;
3510 			++curbuf->b_op_end.col;
3511 		    }
3512 		}
3513 	    }
3514 	    new_cursor = curwin->w_cursor;
3515 	    curbuf->b_op_start = curwin->w_cursor;
3516 	}
3517 	/*
3518 	 * Line mode: BACKWARD is the same as FORWARD on the previous line
3519 	 */
3520 	else if (dir == BACKWARD)
3521 	    --lnum;
3522 
3523 	/*
3524 	 * simple case: insert into current line
3525 	 */
3526 	if (y_type == MCHAR && y_size == 1)
3527 	{
3528 	    totlen = count * yanklen;
3529 	    if (totlen)
3530 	    {
3531 		oldp = ml_get(lnum);
3532 		newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3533 		if (newp == NULL)
3534 		    goto end;		/* alloc() will give error message */
3535 		mch_memmove(newp, oldp, (size_t)col);
3536 		ptr = newp + col;
3537 		for (i = 0; i < count; ++i)
3538 		{
3539 		    mch_memmove(ptr, y_array[0], (size_t)yanklen);
3540 		    ptr += yanklen;
3541 		}
3542 		mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3543 		ml_replace(lnum, newp, FALSE);
3544 		/* Put cursor on last putted char. */
3545 		curwin->w_cursor.col += (colnr_T)(totlen - 1);
3546 	    }
3547 	    curbuf->b_op_end = curwin->w_cursor;
3548 	    /* For "CTRL-O p" in Insert mode, put cursor after last char */
3549 	    if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3550 		++curwin->w_cursor.col;
3551 	    changed_bytes(lnum, col);
3552 	}
3553 	else
3554 	{
3555 	    /*
3556 	     * Insert at least one line.  When y_type is MCHAR, break the first
3557 	     * line in two.
3558 	     */
3559 	    for (cnt = 1; cnt <= count; ++cnt)
3560 	    {
3561 		i = 0;
3562 		if (y_type == MCHAR)
3563 		{
3564 		    /*
3565 		     * Split the current line in two at the insert position.
3566 		     * First insert y_array[size - 1] in front of second line.
3567 		     * Then append y_array[0] to first line.
3568 		     */
3569 		    lnum = new_cursor.lnum;
3570 		    ptr = ml_get(lnum) + col;
3571 		    totlen = (int)STRLEN(y_array[y_size - 1]);
3572 		    newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3573 		    if (newp == NULL)
3574 			goto error;
3575 		    STRCPY(newp, y_array[y_size - 1]);
3576 		    STRCAT(newp, ptr);
3577 		    /* insert second line */
3578 		    ml_append(lnum, newp, (colnr_T)0, FALSE);
3579 		    vim_free(newp);
3580 
3581 		    oldp = ml_get(lnum);
3582 		    newp = alloc_check((unsigned)(col + yanklen + 1));
3583 		    if (newp == NULL)
3584 			goto error;
3585 					    /* copy first part of line */
3586 		    mch_memmove(newp, oldp, (size_t)col);
3587 					    /* append to first line */
3588 		    mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3589 		    ml_replace(lnum, newp, FALSE);
3590 
3591 		    curwin->w_cursor.lnum = lnum;
3592 		    i = 1;
3593 		}
3594 
3595 		for (; i < y_size; ++i)
3596 		{
3597 		    if ((y_type != MCHAR || i < y_size - 1)
3598 			    && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3599 								      == FAIL)
3600 			    goto error;
3601 		    lnum++;
3602 		    ++nr_lines;
3603 		    if (flags & PUT_FIXINDENT)
3604 		    {
3605 			old_pos = curwin->w_cursor;
3606 			curwin->w_cursor.lnum = lnum;
3607 			ptr = ml_get(lnum);
3608 			if (cnt == count && i == y_size - 1)
3609 			    lendiff = (int)STRLEN(ptr);
3610 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3611 			if (*ptr == '#' && preprocs_left())
3612 			    indent = 0;     /* Leave # lines at start */
3613 			else
3614 #endif
3615 			     if (*ptr == NUL)
3616 			    indent = 0;     /* Ignore empty lines */
3617 			else if (first_indent)
3618 			{
3619 			    indent_diff = orig_indent - get_indent();
3620 			    indent = orig_indent;
3621 			    first_indent = FALSE;
3622 			}
3623 			else if ((indent = get_indent() + indent_diff) < 0)
3624 			    indent = 0;
3625 			(void)set_indent(indent, 0);
3626 			curwin->w_cursor = old_pos;
3627 			/* remember how many chars were removed */
3628 			if (cnt == count && i == y_size - 1)
3629 			    lendiff -= (int)STRLEN(ml_get(lnum));
3630 		    }
3631 		}
3632 	    }
3633 
3634 error:
3635 	    /* Adjust marks. */
3636 	    if (y_type == MLINE)
3637 	    {
3638 		curbuf->b_op_start.col = 0;
3639 		if (dir == FORWARD)
3640 		    curbuf->b_op_start.lnum++;
3641 	    }
3642 	    mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3643 					     (linenr_T)MAXLNUM, nr_lines, 0L);
3644 
3645 	    /* note changed text for displaying and folding */
3646 	    if (y_type == MCHAR)
3647 		changed_lines(curwin->w_cursor.lnum, col,
3648 					 curwin->w_cursor.lnum + 1, nr_lines);
3649 	    else
3650 		changed_lines(curbuf->b_op_start.lnum, 0,
3651 					   curbuf->b_op_start.lnum, nr_lines);
3652 
3653 	    /* put '] mark at last inserted character */
3654 	    curbuf->b_op_end.lnum = lnum;
3655 	    /* correct length for change in indent */
3656 	    col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3657 	    if (col > 1)
3658 		curbuf->b_op_end.col = col - 1;
3659 	    else
3660 		curbuf->b_op_end.col = 0;
3661 
3662 	    if (flags & PUT_CURSLINE)
3663 	    {
3664 		/* ":put": put cursor on last inserted line */
3665 		curwin->w_cursor.lnum = lnum;
3666 		beginline(BL_WHITE | BL_FIX);
3667 	    }
3668 	    else if (flags & PUT_CURSEND)
3669 	    {
3670 		/* put cursor after inserted text */
3671 		if (y_type == MLINE)
3672 		{
3673 		    if (lnum >= curbuf->b_ml.ml_line_count)
3674 			curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3675 		    else
3676 			curwin->w_cursor.lnum = lnum + 1;
3677 		    curwin->w_cursor.col = 0;
3678 		}
3679 		else
3680 		{
3681 		    curwin->w_cursor.lnum = lnum;
3682 		    curwin->w_cursor.col = col;
3683 		}
3684 	    }
3685 	    else if (y_type == MLINE)
3686 	    {
3687 		/* put cursor on first non-blank in first inserted line */
3688 		curwin->w_cursor.col = 0;
3689 		if (dir == FORWARD)
3690 		    ++curwin->w_cursor.lnum;
3691 		beginline(BL_WHITE | BL_FIX);
3692 	    }
3693 	    else	/* put cursor on first inserted character */
3694 		curwin->w_cursor = new_cursor;
3695 	}
3696     }
3697 
3698     msgmore(nr_lines);
3699     curwin->w_set_curswant = TRUE;
3700 
3701 end:
3702     if (allocated)
3703     {
3704 	vim_free(insert_string);
3705 	if (regname == '=')
3706 	    vim_free(y_array);
3707     }
3708     /* If the cursor is past the end of the line put it at the end. */
3709     if (gchar_cursor() == NUL
3710 	    && curwin->w_cursor.col > 0
3711 	    && !(restart_edit || (State & INSERT)))
3712     {
3713 	--curwin->w_cursor.col;
3714 #ifdef FEAT_VIRTUALEDIT
3715 	if (ve_flags == VE_ALL)
3716 	    ++curwin->w_cursor.coladd;
3717 #endif
3718     }
3719 }
3720 
3721 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3722 /*
3723  * Return TRUE if lines starting with '#' should be left aligned.
3724  */
3725     int
3726 preprocs_left()
3727 {
3728     return
3729 # ifdef FEAT_SMARTINDENT
3730 #  ifdef FEAT_CINDENT
3731 	(curbuf->b_p_si && !curbuf->b_p_cin) ||
3732 #  else
3733 	curbuf->b_p_si
3734 #  endif
3735 # endif
3736 # ifdef FEAT_CINDENT
3737 	(curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3738 # endif
3739 	;
3740 }
3741 #endif
3742 
3743 /* Return the character name of the register with the given number */
3744     int
3745 get_register_name(num)
3746     int num;
3747 {
3748     if (num == -1)
3749 	return '"';
3750     else if (num < 10)
3751 	return num + '0';
3752     else if (num == DELETION_REGISTER)
3753 	return '-';
3754 #ifdef FEAT_CLIPBOARD
3755     else if (num == STAR_REGISTER)
3756 	return '*';
3757     else if (num == PLUS_REGISTER)
3758 	return '+';
3759 #endif
3760     else
3761     {
3762 #ifdef EBCDIC
3763 	int i;
3764 
3765 	/* EBCDIC is really braindead ... */
3766 	i = 'a' + (num - 10);
3767 	if (i > 'i')
3768 	    i += 7;
3769 	if (i > 'r')
3770 	    i += 8;
3771 	return i;
3772 #else
3773 	return num + 'a' - 10;
3774 #endif
3775     }
3776 }
3777 
3778 /*
3779  * ":dis" and ":registers": Display the contents of the yank registers.
3780  */
3781     void
3782 ex_display(eap)
3783     exarg_T	*eap;
3784 {
3785     int			i, n;
3786     long		j;
3787     char_u		*p;
3788     struct yankreg	*yb;
3789     int			name;
3790     int			attr;
3791     char_u		*arg = eap->arg;
3792 #ifdef FEAT_MBYTE
3793     int			clen;
3794 #else
3795 # define clen 1
3796 #endif
3797 
3798     if (arg != NULL && *arg == NUL)
3799 	arg = NULL;
3800     attr = hl_attr(HLF_8);
3801 
3802     /* Highlight title */
3803     MSG_PUTS_TITLE(_("\n--- Registers ---"));
3804     for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3805     {
3806 	name = get_register_name(i);
3807 	if (arg != NULL && vim_strchr(arg, name) == NULL)
3808 	    continue;	    /* did not ask for this register */
3809 
3810 #ifdef FEAT_CLIPBOARD
3811 	/* Adjust register name for "unnamed" in 'clipboard'.
3812 	 * When it's a clipboard register, fill it with the current contents
3813 	 * of the clipboard.  */
3814 	adjust_clip_reg(&name);
3815 	(void)may_get_selection(name);
3816 #endif
3817 
3818 	if (i == -1)
3819 	{
3820 	    if (y_previous != NULL)
3821 		yb = y_previous;
3822 	    else
3823 		yb = &(y_regs[0]);
3824 	}
3825 	else
3826 	    yb = &(y_regs[i]);
3827 	if (yb->y_array != NULL)
3828 	{
3829 	    msg_putchar('\n');
3830 	    msg_putchar('"');
3831 	    msg_putchar(name);
3832 	    MSG_PUTS("   ");
3833 
3834 	    n = (int)Columns - 6;
3835 	    for (j = 0; j < yb->y_size && n > 1; ++j)
3836 	    {
3837 		if (j)
3838 		{
3839 		    MSG_PUTS_ATTR("^J", attr);
3840 		    n -= 2;
3841 		}
3842 		for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3843 		{
3844 #ifdef FEAT_MBYTE
3845 		    clen = (*mb_ptr2len)(p);
3846 #endif
3847 		    msg_outtrans_len(p, clen);
3848 #ifdef FEAT_MBYTE
3849 		    p += clen - 1;
3850 #endif
3851 		}
3852 	    }
3853 	    if (n > 1 && yb->y_type == MLINE)
3854 		MSG_PUTS_ATTR("^J", attr);
3855 	    out_flush();		    /* show one line at a time */
3856 	}
3857 	ui_breakcheck();
3858     }
3859 
3860     /*
3861      * display last inserted text
3862      */
3863     if ((p = get_last_insert()) != NULL
3864 		 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3865     {
3866 	MSG_PUTS("\n\".   ");
3867 	dis_msg(p, TRUE);
3868     }
3869 
3870     /*
3871      * display last command line
3872      */
3873     if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3874 								  && !got_int)
3875     {
3876 	MSG_PUTS("\n\":   ");
3877 	dis_msg(last_cmdline, FALSE);
3878     }
3879 
3880     /*
3881      * display current file name
3882      */
3883     if (curbuf->b_fname != NULL
3884 	    && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3885     {
3886 	MSG_PUTS("\n\"%   ");
3887 	dis_msg(curbuf->b_fname, FALSE);
3888     }
3889 
3890     /*
3891      * display alternate file name
3892      */
3893     if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3894     {
3895 	char_u	    *fname;
3896 	linenr_T    dummy;
3897 
3898 	if (buflist_name_nr(0, &fname, &dummy) != FAIL)
3899 	{
3900 	    MSG_PUTS("\n\"#   ");
3901 	    dis_msg(fname, FALSE);
3902 	}
3903     }
3904 
3905     /*
3906      * display last search pattern
3907      */
3908     if (last_search_pat() != NULL
3909 		 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
3910     {
3911 	MSG_PUTS("\n\"/   ");
3912 	dis_msg(last_search_pat(), FALSE);
3913     }
3914 
3915 #ifdef FEAT_EVAL
3916     /*
3917      * display last used expression
3918      */
3919     if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
3920 								  && !got_int)
3921     {
3922 	MSG_PUTS("\n\"=   ");
3923 	dis_msg(expr_line, FALSE);
3924     }
3925 #endif
3926 }
3927 
3928 /*
3929  * display a string for do_dis()
3930  * truncate at end of screen line
3931  */
3932     static void
3933 dis_msg(p, skip_esc)
3934     char_u	*p;
3935     int		skip_esc;	    /* if TRUE, ignore trailing ESC */
3936 {
3937     int		n;
3938 #ifdef FEAT_MBYTE
3939     int		l;
3940 #endif
3941 
3942     n = (int)Columns - 6;
3943     while (*p != NUL
3944 	    && !(*p == ESC && skip_esc && *(p + 1) == NUL)
3945 	    && (n -= ptr2cells(p)) >= 0)
3946     {
3947 #ifdef FEAT_MBYTE
3948 	if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
3949 	{
3950 	    msg_outtrans_len(p, l);
3951 	    p += l;
3952 	}
3953 	else
3954 #endif
3955 	    msg_outtrans_len(p++, 1);
3956     }
3957     ui_breakcheck();
3958 }
3959 
3960 /*
3961  * join 'count' lines (minimal 2), including u_save()
3962  */
3963     void
3964 do_do_join(count, insert_space)
3965     long    count;
3966     int	    insert_space;
3967 {
3968     colnr_T	col = MAXCOL;
3969 
3970     if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
3971 		    (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
3972 	return;
3973 
3974     while (--count > 0)
3975     {
3976 	line_breakcheck();
3977 	if (got_int || do_join(insert_space) == FAIL)
3978 	{
3979 	    beep_flush();
3980 	    break;
3981 	}
3982 	if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
3983 	    col = curwin->w_cursor.col;
3984     }
3985 
3986     /* Vi compatible: use the column of the first join */
3987     if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
3988 	curwin->w_cursor.col = col;
3989 
3990 #if 0
3991     /*
3992      * Need to update the screen if the line where the cursor is became too
3993      * long to fit on the screen.
3994      */
3995     update_topline_redraw();
3996 #endif
3997 }
3998 
3999 /*
4000  * Join two lines at the cursor position.
4001  * "redraw" is TRUE when the screen should be updated.
4002  * Caller must have setup for undo.
4003  *
4004  * return FAIL for failure, OK ohterwise
4005  */
4006     int
4007 do_join(insert_space)
4008     int		insert_space;
4009 {
4010     char_u	*curr;
4011     char_u	*next, *next_start;
4012     char_u	*newp;
4013     int		endcurr1, endcurr2;
4014     int		currsize;	/* size of the current line */
4015     int		nextsize;	/* size of the next line */
4016     int		spaces;		/* number of spaces to insert */
4017     linenr_T	t;
4018 
4019     if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4020 	return FAIL;		/* can't join on last line */
4021 
4022     curr = ml_get_curline();
4023     currsize = (int)STRLEN(curr);
4024     endcurr1 = endcurr2 = NUL;
4025     if (insert_space && currsize > 0)
4026     {
4027 #ifdef FEAT_MBYTE
4028 	if (has_mbyte)
4029 	{
4030 	    next = curr + currsize;
4031 	    mb_ptr_back(curr, next);
4032 	    endcurr1 = (*mb_ptr2char)(next);
4033 	    if (next > curr)
4034 	    {
4035 		mb_ptr_back(curr, next);
4036 		endcurr2 = (*mb_ptr2char)(next);
4037 	    }
4038 	}
4039 	else
4040 #endif
4041 	{
4042 	    endcurr1 = *(curr + currsize - 1);
4043 	    if (currsize > 1)
4044 		endcurr2 = *(curr + currsize - 2);
4045 	}
4046     }
4047 
4048     next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4049     spaces = 0;
4050     if (insert_space)
4051     {
4052 	next = skipwhite(next);
4053 	if (*next != ')' && currsize != 0 && endcurr1 != TAB
4054 #ifdef FEAT_MBYTE
4055 		&& (!has_format_option(FO_MBYTE_JOIN)
4056 			|| (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4057 		&& (!has_format_option(FO_MBYTE_JOIN2)
4058 			|| mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4059 #endif
4060 		)
4061 	{
4062 	    /* don't add a space if the line is ending in a space */
4063 	    if (endcurr1 == ' ')
4064 		endcurr1 = endcurr2;
4065 	    else
4066 		++spaces;
4067 	    /* extra space when 'joinspaces' set and line ends in '.' */
4068 	    if (       p_js
4069 		    && (endcurr1 == '.'
4070 			|| (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4071 			    && (endcurr1 == '?' || endcurr1 == '!'))))
4072 		++spaces;
4073 	}
4074     }
4075     nextsize = (int)STRLEN(next);
4076 
4077     newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4078     if (newp == NULL)
4079 	return FAIL;
4080 
4081     /*
4082      * Insert the next line first, because we already have that pointer.
4083      * Curr has to be obtained again, because getting next will have
4084      * invalidated it.
4085      */
4086     mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4087 
4088     curr = ml_get_curline();
4089     mch_memmove(newp, curr, (size_t)currsize);
4090 
4091     copy_spaces(newp + currsize, (size_t)spaces);
4092 
4093     ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4094 
4095     /* Only report the change in the first line here, del_lines() will report
4096      * the deleted line. */
4097     changed_lines(curwin->w_cursor.lnum, currsize,
4098 					       curwin->w_cursor.lnum + 1, 0L);
4099 
4100     /*
4101      * Delete the following line. To do this we move the cursor there
4102      * briefly, and then move it back. After del_lines() the cursor may
4103      * have moved up (last line deleted), so the current lnum is kept in t.
4104      *
4105      * Move marks from the deleted line to the joined line, adjusting the
4106      * column.  This is not Vi compatible, but Vi deletes the marks, thus that
4107      * should not really be a problem.
4108      */
4109     t = curwin->w_cursor.lnum;
4110     mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4111 			     (long)(currsize + spaces - (next - next_start)));
4112     ++curwin->w_cursor.lnum;
4113     del_lines(1L, FALSE);
4114     curwin->w_cursor.lnum = t;
4115 
4116     /*
4117      * go to first character of the joined line
4118      */
4119     curwin->w_cursor.col = currsize;
4120     check_cursor_col();
4121 #ifdef FEAT_VIRTUALEDIT
4122     curwin->w_cursor.coladd = 0;
4123 #endif
4124     curwin->w_set_curswant = TRUE;
4125 
4126     return OK;
4127 }
4128 
4129 #ifdef FEAT_COMMENTS
4130 /*
4131  * Return TRUE if the two comment leaders given are the same.  "lnum" is
4132  * the first line.  White-space is ignored.  Note that the whole of
4133  * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4134  */
4135     static int
4136 same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4137     linenr_T lnum;
4138     int	    leader1_len;
4139     char_u  *leader1_flags;
4140     int	    leader2_len;
4141     char_u  *leader2_flags;
4142 {
4143     int	    idx1 = 0, idx2 = 0;
4144     char_u  *p;
4145     char_u  *line1;
4146     char_u  *line2;
4147 
4148     if (leader1_len == 0)
4149 	return (leader2_len == 0);
4150 
4151     /*
4152      * If first leader has 'f' flag, the lines can be joined only if the
4153      * second line does not have a leader.
4154      * If first leader has 'e' flag, the lines can never be joined.
4155      * If fist leader has 's' flag, the lines can only be joined if there is
4156      * some text after it and the second line has the 'm' flag.
4157      */
4158     if (leader1_flags != NULL)
4159     {
4160 	for (p = leader1_flags; *p && *p != ':'; ++p)
4161 	{
4162 	    if (*p == COM_FIRST)
4163 		return (leader2_len == 0);
4164 	    if (*p == COM_END)
4165 		return FALSE;
4166 	    if (*p == COM_START)
4167 	    {
4168 		if (*(ml_get(lnum) + leader1_len) == NUL)
4169 		    return FALSE;
4170 		if (leader2_flags == NULL || leader2_len == 0)
4171 		    return FALSE;
4172 		for (p = leader2_flags; *p && *p != ':'; ++p)
4173 		    if (*p == COM_MIDDLE)
4174 			return TRUE;
4175 		return FALSE;
4176 	    }
4177 	}
4178     }
4179 
4180     /*
4181      * Get current line and next line, compare the leaders.
4182      * The first line has to be saved, only one line can be locked at a time.
4183      */
4184     line1 = vim_strsave(ml_get(lnum));
4185     if (line1 != NULL)
4186     {
4187 	for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4188 	    ;
4189 	line2 = ml_get(lnum + 1);
4190 	for (idx2 = 0; idx2 < leader2_len; ++idx2)
4191 	{
4192 	    if (!vim_iswhite(line2[idx2]))
4193 	    {
4194 		if (line1[idx1++] != line2[idx2])
4195 		    break;
4196 	    }
4197 	    else
4198 		while (vim_iswhite(line1[idx1]))
4199 		    ++idx1;
4200 	}
4201 	vim_free(line1);
4202     }
4203     return (idx2 == leader2_len && idx1 == leader1_len);
4204 }
4205 #endif
4206 
4207 /*
4208  * implementation of the format operator 'gq'
4209  */
4210     void
4211 op_format(oap, keep_cursor)
4212     oparg_T	*oap;
4213     int		keep_cursor;		/* keep cursor on same text char */
4214 {
4215     long	old_line_count = curbuf->b_ml.ml_line_count;
4216 
4217     /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4218      * can put it back there. */
4219     curwin->w_cursor = oap->cursor_start;
4220 
4221     if (u_save((linenr_T)(oap->start.lnum - 1),
4222 				       (linenr_T)(oap->end.lnum + 1)) == FAIL)
4223 	return;
4224     curwin->w_cursor = oap->start;
4225 
4226 #ifdef FEAT_VISUAL
4227     if (oap->is_VIsual)
4228 	/* When there is no change: need to remove the Visual selection */
4229 	redraw_curbuf_later(INVERTED);
4230 #endif
4231 
4232     /* Set '[ mark at the start of the formatted area */
4233     curbuf->b_op_start = oap->start;
4234 
4235     /* For "gw" remember the cursor position and put it back below (adjusted
4236      * for joined and split lines). */
4237     if (keep_cursor)
4238 	saved_cursor = oap->cursor_start;
4239 
4240     format_lines(oap->line_count);
4241 
4242     /*
4243      * Leave the cursor at the first non-blank of the last formatted line.
4244      * If the cursor was moved one line back (e.g. with "Q}") go to the next
4245      * line, so "." will do the next lines.
4246      */
4247     if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4248 	++curwin->w_cursor.lnum;
4249     beginline(BL_WHITE | BL_FIX);
4250     old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4251     msgmore(old_line_count);
4252 
4253     /* put '] mark on the end of the formatted area */
4254     curbuf->b_op_end = curwin->w_cursor;
4255 
4256     if (keep_cursor)
4257     {
4258 	curwin->w_cursor = saved_cursor;
4259 	saved_cursor.lnum = 0;
4260     }
4261 
4262 #ifdef FEAT_VISUAL
4263     if (oap->is_VIsual)
4264     {
4265 	win_T	*wp;
4266 
4267 	FOR_ALL_WINDOWS(wp)
4268 	{
4269 	    if (wp->w_old_cursor_lnum != 0)
4270 	    {
4271 		/* When lines have been inserted or deleted, adjust the end of
4272 		 * the Visual area to be redrawn. */
4273 		if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4274 		    wp->w_old_cursor_lnum += old_line_count;
4275 		else
4276 		    wp->w_old_visual_lnum += old_line_count;
4277 	    }
4278 	}
4279     }
4280 #endif
4281 }
4282 
4283 /*
4284  * Format "line_count" lines, starting at the cursor position.
4285  * When "line_count" is negative, format until the end of the paragraph.
4286  * Lines after the cursor line are saved for undo, caller must have saved the
4287  * first line.
4288  */
4289     void
4290 format_lines(line_count)
4291     linenr_T	line_count;
4292 {
4293     int		max_len;
4294     int		is_not_par;		/* current line not part of parag. */
4295     int		next_is_not_par;	/* next line not part of paragraph */
4296     int		is_end_par;		/* at end of paragraph */
4297     int		prev_is_end_par = FALSE;/* prev. line not part of parag. */
4298     int		next_is_start_par = FALSE;
4299 #ifdef FEAT_COMMENTS
4300     int		leader_len = 0;		/* leader len of current line */
4301     int		next_leader_len;	/* leader len of next line */
4302     char_u	*leader_flags = NULL;	/* flags for leader of current line */
4303     char_u	*next_leader_flags;	/* flags for leader of next line */
4304     int		do_comments;		/* format comments */
4305 #endif
4306     int		advance = TRUE;
4307     int		second_indent = -1;
4308     int		do_second_indent;
4309     int		do_number_indent;
4310     int		do_trail_white;
4311     int		first_par_line = TRUE;
4312     int		smd_save;
4313     long	count;
4314     int		need_set_indent = TRUE;	/* set indent of next paragraph */
4315     int		force_format = FALSE;
4316     int		old_State = State;
4317 
4318     /* length of a line to force formatting: 3 * 'tw' */
4319     max_len = comp_textwidth(TRUE) * 3;
4320 
4321     /* check for 'q', '2' and '1' in 'formatoptions' */
4322 #ifdef FEAT_COMMENTS
4323     do_comments = has_format_option(FO_Q_COMS);
4324 #endif
4325     do_second_indent = has_format_option(FO_Q_SECOND);
4326     do_number_indent = has_format_option(FO_Q_NUMBER);
4327     do_trail_white = has_format_option(FO_WHITE_PAR);
4328 
4329     /*
4330      * Get info about the previous and current line.
4331      */
4332     if (curwin->w_cursor.lnum > 1)
4333 	is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4334 #ifdef FEAT_COMMENTS
4335 				, &leader_len, &leader_flags, do_comments
4336 #endif
4337 				);
4338     else
4339 	is_not_par = TRUE;
4340     next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4341 #ifdef FEAT_COMMENTS
4342 			   , &next_leader_len, &next_leader_flags, do_comments
4343 #endif
4344 				);
4345     is_end_par = (is_not_par || next_is_not_par);
4346     if (!is_end_par && do_trail_white)
4347 	is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4348 
4349     curwin->w_cursor.lnum--;
4350     for (count = line_count; count != 0 && !got_int; --count)
4351     {
4352 	/*
4353 	 * Advance to next paragraph.
4354 	 */
4355 	if (advance)
4356 	{
4357 	    curwin->w_cursor.lnum++;
4358 	    prev_is_end_par = is_end_par;
4359 	    is_not_par = next_is_not_par;
4360 #ifdef FEAT_COMMENTS
4361 	    leader_len = next_leader_len;
4362 	    leader_flags = next_leader_flags;
4363 #endif
4364 	}
4365 
4366 	/*
4367 	 * The last line to be formatted.
4368 	 */
4369 	if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4370 	{
4371 	    next_is_not_par = TRUE;
4372 #ifdef FEAT_COMMENTS
4373 	    next_leader_len = 0;
4374 	    next_leader_flags = NULL;
4375 #endif
4376 	}
4377 	else
4378 	{
4379 	    next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4380 #ifdef FEAT_COMMENTS
4381 			   , &next_leader_len, &next_leader_flags, do_comments
4382 #endif
4383 					);
4384 	    if (do_number_indent)
4385 		next_is_start_par =
4386 			   (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4387 	}
4388 	advance = TRUE;
4389 	is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4390 	if (!is_end_par && do_trail_white)
4391 	    is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4392 
4393 	/*
4394 	 * Skip lines that are not in a paragraph.
4395 	 */
4396 	if (is_not_par)
4397 	{
4398 	    if (line_count < 0)
4399 		break;
4400 	}
4401 	else
4402 	{
4403 	    /*
4404 	     * For the first line of a paragraph, check indent of second line.
4405 	     * Don't do this for comments and empty lines.
4406 	     */
4407 	    if (first_par_line
4408 		    && (do_second_indent || do_number_indent)
4409 		    && prev_is_end_par
4410 		    && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4411 #ifdef FEAT_COMMENTS
4412 		    && leader_len == 0
4413 		    && next_leader_len == 0
4414 #endif
4415 		    )
4416 	    {
4417 		if (do_second_indent
4418 			&& !lineempty(curwin->w_cursor.lnum + 1))
4419 		    second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4420 		else if (do_number_indent)
4421 		    second_indent = get_number_indent(curwin->w_cursor.lnum);
4422 	    }
4423 
4424 	    /*
4425 	     * When the comment leader changes, it's the end of the paragraph.
4426 	     */
4427 	    if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4428 #ifdef FEAT_COMMENTS
4429 		    || !same_leader(curwin->w_cursor.lnum,
4430 					leader_len, leader_flags,
4431 					  next_leader_len, next_leader_flags)
4432 #endif
4433 		    )
4434 		is_end_par = TRUE;
4435 
4436 	    /*
4437 	     * If we have got to the end of a paragraph, or the line is
4438 	     * getting long, format it.
4439 	     */
4440 	    if (is_end_par || force_format)
4441 	    {
4442 		if (need_set_indent)
4443 		    /* replace indent in first line with minimal number of
4444 		     * tabs and spaces, according to current options */
4445 		    (void)set_indent(get_indent(), SIN_CHANGED);
4446 
4447 		/* put cursor on last non-space */
4448 		State = NORMAL;	/* don't go past end-of-line */
4449 		coladvance((colnr_T)MAXCOL);
4450 		while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4451 		    dec_cursor();
4452 
4453 		/* do the formatting, without 'showmode' */
4454 		State = INSERT;	/* for open_line() */
4455 		smd_save = p_smd;
4456 		p_smd = FALSE;
4457 		insertchar(NUL, INSCHAR_FORMAT
4458 #ifdef FEAT_COMMENTS
4459 			+ (do_comments ? INSCHAR_DO_COM : 0)
4460 #endif
4461 			, second_indent);
4462 		State = old_State;
4463 		p_smd = smd_save;
4464 		second_indent = -1;
4465 		/* at end of par.: need to set indent of next par. */
4466 		need_set_indent = is_end_par;
4467 		if (is_end_par)
4468 		{
4469 		    /* When called with a negative line count, break at the
4470 		     * end of the paragraph. */
4471 		    if (line_count < 0)
4472 			break;
4473 		    first_par_line = TRUE;
4474 		}
4475 		force_format = FALSE;
4476 	    }
4477 
4478 	    /*
4479 	     * When still in same paragraph, join the lines together.  But
4480 	     * first delete the comment leader from the second line.
4481 	     */
4482 	    if (!is_end_par)
4483 	    {
4484 		advance = FALSE;
4485 		curwin->w_cursor.lnum++;
4486 		curwin->w_cursor.col = 0;
4487 		if (line_count < 0 && u_save_cursor() == FAIL)
4488 			break;
4489 #ifdef FEAT_COMMENTS
4490 		(void)del_bytes((long)next_leader_len, FALSE);
4491 		if (next_leader_len > 0)
4492 		    mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4493 						      (long)-next_leader_len);
4494 #endif
4495 		curwin->w_cursor.lnum--;
4496 		if (do_join(TRUE) == FAIL)
4497 		{
4498 		    beep_flush();
4499 		    break;
4500 		}
4501 		first_par_line = FALSE;
4502 		/* If the line is getting long, format it next time */
4503 		if (STRLEN(ml_get_curline()) > (size_t)max_len)
4504 		    force_format = TRUE;
4505 		else
4506 		    force_format = FALSE;
4507 	    }
4508 	}
4509 	line_breakcheck();
4510     }
4511 }
4512 
4513 /*
4514  * Return TRUE if line "lnum" ends in a white character.
4515  */
4516     static int
4517 ends_in_white(lnum)
4518     linenr_T	lnum;
4519 {
4520     char_u	*s = ml_get(lnum);
4521     size_t	l;
4522 
4523     if (*s == NUL)
4524 	return FALSE;
4525     /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4526      * invocation may call function multiple times". */
4527     l = STRLEN(s) - 1;
4528     return vim_iswhite(s[l]);
4529 }
4530 
4531 /*
4532  * Blank lines, and lines containing only the comment leader, are left
4533  * untouched by the formatting.  The function returns TRUE in this
4534  * case.  It also returns TRUE when a line starts with the end of a comment
4535  * ('e' in comment flags), so that this line is skipped, and not joined to the
4536  * previous line.  A new paragraph starts after a blank line, or when the
4537  * comment leader changes -- webb.
4538  */
4539 #ifdef FEAT_COMMENTS
4540     static int
4541 fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4542     linenr_T	lnum;
4543     int		*leader_len;
4544     char_u	**leader_flags;
4545     int		do_comments;
4546 {
4547     char_u	*flags = NULL;	    /* init for GCC */
4548     char_u	*ptr;
4549 
4550     ptr = ml_get(lnum);
4551     if (do_comments)
4552 	*leader_len = get_leader_len(ptr, leader_flags, FALSE);
4553     else
4554 	*leader_len = 0;
4555 
4556     if (*leader_len > 0)
4557     {
4558 	/*
4559 	 * Search for 'e' flag in comment leader flags.
4560 	 */
4561 	flags = *leader_flags;
4562 	while (*flags && *flags != ':' && *flags != COM_END)
4563 	    ++flags;
4564     }
4565 
4566     return (*skipwhite(ptr + *leader_len) == NUL
4567 	    || (*leader_len > 0 && *flags == COM_END)
4568 	    || startPS(lnum, NUL, FALSE));
4569 }
4570 #else
4571     static int
4572 fmt_check_par(lnum)
4573     linenr_T	lnum;
4574 {
4575     return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4576 }
4577 #endif
4578 
4579 /*
4580  * Return TRUE when a paragraph starts in line "lnum".  Return FALSE when the
4581  * previous line is in the same paragraph.  Used for auto-formatting.
4582  */
4583     int
4584 paragraph_start(lnum)
4585     linenr_T	lnum;
4586 {
4587     char_u	*p;
4588 #ifdef FEAT_COMMENTS
4589     int		leader_len = 0;		/* leader len of current line */
4590     char_u	*leader_flags = NULL;	/* flags for leader of current line */
4591     int		next_leader_len;	/* leader len of next line */
4592     char_u	*next_leader_flags;	/* flags for leader of next line */
4593     int		do_comments;		/* format comments */
4594 #endif
4595 
4596     if (lnum <= 1)
4597 	return TRUE;		/* start of the file */
4598 
4599     p = ml_get(lnum - 1);
4600     if (*p == NUL)
4601 	return TRUE;		/* after empty line */
4602 
4603 #ifdef FEAT_COMMENTS
4604     do_comments = has_format_option(FO_Q_COMS);
4605 #endif
4606     if (fmt_check_par(lnum - 1
4607 #ifdef FEAT_COMMENTS
4608 				, &leader_len, &leader_flags, do_comments
4609 #endif
4610 		))
4611 	return TRUE;		/* after non-paragraph line */
4612 
4613     if (fmt_check_par(lnum
4614 #ifdef FEAT_COMMENTS
4615 			   , &next_leader_len, &next_leader_flags, do_comments
4616 #endif
4617 		))
4618 	return TRUE;		/* "lnum" is not a paragraph line */
4619 
4620     if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4621 	return TRUE;		/* missing trailing space in previous line. */
4622 
4623     if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4624 	return TRUE;		/* numbered item starts in "lnum". */
4625 
4626 #ifdef FEAT_COMMENTS
4627     if (!same_leader(lnum - 1, leader_len, leader_flags,
4628 					  next_leader_len, next_leader_flags))
4629 	return TRUE;		/* change of comment leader. */
4630 #endif
4631 
4632     return FALSE;
4633 }
4634 
4635 #ifdef FEAT_VISUAL
4636 /*
4637  * prepare a few things for block mode yank/delete/tilde
4638  *
4639  * for delete:
4640  * - textlen includes the first/last char to be (partly) deleted
4641  * - start/endspaces is the number of columns that are taken by the
4642  *   first/last deleted char minus the number of columns that have to be
4643  *   deleted.  for yank and tilde:
4644  * - textlen includes the first/last char to be wholly yanked
4645  * - start/endspaces is the number of columns of the first/last yanked char
4646  *   that are to be yanked.
4647  */
4648     static void
4649 block_prep(oap, bdp, lnum, is_del)
4650     oparg_T		*oap;
4651     struct block_def	*bdp;
4652     linenr_T		lnum;
4653     int			is_del;
4654 {
4655     int		incr = 0;
4656     char_u	*pend;
4657     char_u	*pstart;
4658     char_u	*line;
4659     char_u	*prev_pstart;
4660     char_u	*prev_pend;
4661 
4662     bdp->startspaces = 0;
4663     bdp->endspaces = 0;
4664     bdp->textlen = 0;
4665     bdp->start_vcol = 0;
4666     bdp->end_vcol = 0;
4667 #ifdef FEAT_VISUALEXTRA
4668     bdp->is_short = FALSE;
4669     bdp->is_oneChar = FALSE;
4670     bdp->pre_whitesp = 0;
4671     bdp->pre_whitesp_c = 0;
4672     bdp->end_char_vcols = 0;
4673 #endif
4674     bdp->start_char_vcols = 0;
4675 
4676     line = ml_get(lnum);
4677     pstart = line;
4678     prev_pstart = line;
4679     while (bdp->start_vcol < oap->start_vcol && *pstart)
4680     {
4681 	/* Count a tab for what it's worth (if list mode not on) */
4682 	incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4683 	bdp->start_vcol += incr;
4684 #ifdef FEAT_VISUALEXTRA
4685 	if (vim_iswhite(*pstart))
4686 	{
4687 	    bdp->pre_whitesp += incr;
4688 	    bdp->pre_whitesp_c++;
4689 	}
4690 	else
4691 	{
4692 	    bdp->pre_whitesp = 0;
4693 	    bdp->pre_whitesp_c = 0;
4694 	}
4695 #endif
4696 	prev_pstart = pstart;
4697 	mb_ptr_adv(pstart);
4698     }
4699     bdp->start_char_vcols = incr;
4700     if (bdp->start_vcol < oap->start_vcol)	/* line too short */
4701     {
4702 	bdp->end_vcol = bdp->start_vcol;
4703 #ifdef FEAT_VISUALEXTRA
4704 	bdp->is_short = TRUE;
4705 #endif
4706 	if (!is_del || oap->op_type == OP_APPEND)
4707 	    bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4708     }
4709     else
4710     {
4711 	/* notice: this converts partly selected Multibyte characters to
4712 	 * spaces, too. */
4713 	bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4714 	if (is_del && bdp->startspaces)
4715 	    bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4716 	pend = pstart;
4717 	bdp->end_vcol = bdp->start_vcol;
4718 	if (bdp->end_vcol > oap->end_vcol)	/* it's all in one character */
4719 	{
4720 #ifdef FEAT_VISUALEXTRA
4721 	    bdp->is_oneChar = TRUE;
4722 #endif
4723 	    if (oap->op_type == OP_INSERT)
4724 		bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4725 	    else if (oap->op_type == OP_APPEND)
4726 	    {
4727 		bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4728 		bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4729 	    }
4730 	    else
4731 	    {
4732 		bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4733 		if (is_del && oap->op_type != OP_LSHIFT)
4734 		{
4735 		    /* just putting the sum of those two into
4736 		     * bdp->startspaces doesn't work for Visual replace,
4737 		     * so we have to split the tab in two */
4738 		    bdp->startspaces = bdp->start_char_vcols
4739 					- (bdp->start_vcol - oap->start_vcol);
4740 		    bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4741 		}
4742 	    }
4743 	}
4744 	else
4745 	{
4746 	    prev_pend = pend;
4747 	    while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4748 	    {
4749 		/* Count a tab for what it's worth (if list mode not on) */
4750 		prev_pend = pend;
4751 		incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4752 		bdp->end_vcol += incr;
4753 	    }
4754 	    if (bdp->end_vcol <= oap->end_vcol
4755 		    && (!is_del
4756 			|| oap->op_type == OP_APPEND
4757 			|| oap->op_type == OP_REPLACE)) /* line too short */
4758 	    {
4759 #ifdef FEAT_VISUALEXTRA
4760 		bdp->is_short = TRUE;
4761 #endif
4762 		/* Alternative: include spaces to fill up the block.
4763 		 * Disadvantage: can lead to trailing spaces when the line is
4764 		 * short where the text is put */
4765 		/* if (!is_del || oap->op_type == OP_APPEND) */
4766 		if (oap->op_type == OP_APPEND || virtual_op)
4767 		    bdp->endspaces = oap->end_vcol - bdp->end_vcol
4768 					+ oap->inclusive;
4769 		else
4770 		    bdp->endspaces = 0; /* replace doesn't add characters */
4771 	    }
4772 	    else if (bdp->end_vcol > oap->end_vcol)
4773 	    {
4774 		bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4775 		if (!is_del && bdp->endspaces)
4776 		{
4777 		    bdp->endspaces = incr - bdp->endspaces;
4778 		    if (pend != pstart)
4779 			pend = prev_pend;
4780 		}
4781 	    }
4782 	}
4783 #ifdef FEAT_VISUALEXTRA
4784 	bdp->end_char_vcols = incr;
4785 #endif
4786 	if (is_del && bdp->startspaces)
4787 	    pstart = prev_pstart;
4788 	bdp->textlen = (int)(pend - pstart);
4789     }
4790     bdp->textcol = (colnr_T) (pstart - line);
4791     bdp->textstart = pstart;
4792 }
4793 #endif /* FEAT_VISUAL */
4794 
4795 #ifdef FEAT_RIGHTLEFT
4796 static void reverse_line __ARGS((char_u *s));
4797 
4798     static void
4799 reverse_line(s)
4800     char_u *s;
4801 {
4802     int	    i, j;
4803     char_u  c;
4804 
4805     if ((i = (int)STRLEN(s) - 1) <= 0)
4806 	return;
4807 
4808     curwin->w_cursor.col = i - curwin->w_cursor.col;
4809     for (j = 0; j < i; j++, i--)
4810     {
4811 	c = s[i]; s[i] = s[j]; s[j] = c;
4812     }
4813 }
4814 
4815 # define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4816 #else
4817 # define RLADDSUBFIX(ptr)
4818 #endif
4819 
4820 /*
4821  * add or subtract 'Prenum1' from a number in a line
4822  * 'command' is CTRL-A for add, CTRL-X for subtract
4823  *
4824  * return FAIL for failure, OK otherwise
4825  */
4826     int
4827 do_addsub(command, Prenum1)
4828     int		command;
4829     linenr_T	Prenum1;
4830 {
4831     int		col;
4832     char_u	*buf1;
4833     char_u	buf2[NUMBUFLEN];
4834     int		hex;		/* 'X' or 'x': hex; '0': octal */
4835     static int	hexupper = FALSE;	/* 0xABC */
4836     long_u	n;
4837     long_u	oldn;
4838     char_u	*ptr;
4839     int		c;
4840     int		length = 0;		/* character length of the number */
4841     int		todel;
4842     int		dohex;
4843     int		dooct;
4844     int		doalp;
4845     int		firstdigit;
4846     int		negative;
4847     int		subtract;
4848 
4849     dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL);	/* "heX" */
4850     dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL);	/* "Octal" */
4851     doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL);	/* "alPha" */
4852 
4853     ptr = ml_get_curline();
4854     RLADDSUBFIX(ptr);
4855 
4856     /*
4857      * First check if we are on a hexadecimal number, after the "0x".
4858      */
4859     col = curwin->w_cursor.col;
4860     if (dohex)
4861 	while (col > 0 && vim_isxdigit(ptr[col]))
4862 	    --col;
4863     if (       dohex
4864 	    && col > 0
4865 	    && (ptr[col] == 'X'
4866 		|| ptr[col] == 'x')
4867 	    && ptr[col - 1] == '0'
4868 	    && vim_isxdigit(ptr[col + 1]))
4869     {
4870 	/*
4871 	 * Found hexadecimal number, move to its start.
4872 	 */
4873 	--col;
4874     }
4875     else
4876     {
4877 	/*
4878 	 * Search forward and then backward to find the start of number.
4879 	 */
4880 	col = curwin->w_cursor.col;
4881 
4882 	while (ptr[col] != NUL
4883 		&& !vim_isdigit(ptr[col])
4884 		&& !(doalp && ASCII_ISALPHA(ptr[col])))
4885 	    ++col;
4886 
4887 	while (col > 0
4888 		&& vim_isdigit(ptr[col - 1])
4889 		&& !(doalp && ASCII_ISALPHA(ptr[col])))
4890 	    --col;
4891     }
4892 
4893     /* truncate to max length of a number */
4894     if (length >= NUMBUFLEN - 1)
4895 	length = NUMBUFLEN - 2;
4896 
4897     /*
4898      * If a number was found, and saving for undo works, replace the number.
4899      */
4900     firstdigit = ptr[col];
4901     RLADDSUBFIX(ptr);
4902     if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
4903 	    || u_save_cursor() != OK)
4904     {
4905 	beep_flush();
4906 	return FAIL;
4907     }
4908 
4909     /* get ptr again, because u_save() may have changed it */
4910     ptr = ml_get_curline();
4911     RLADDSUBFIX(ptr);
4912 
4913     if (doalp && ASCII_ISALPHA(firstdigit))
4914     {
4915 	/* decrement or increment alphabetic character */
4916 	if (command == Ctrl_X)
4917 	{
4918 	    if (CharOrd(firstdigit) < Prenum1)
4919 	    {
4920 		if (isupper(firstdigit))
4921 		    firstdigit = 'A';
4922 		else
4923 		    firstdigit = 'a';
4924 	    }
4925 	    else
4926 #ifdef EBCDIC
4927 		firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
4928 #else
4929 		firstdigit -= Prenum1;
4930 #endif
4931 	}
4932 	else
4933 	{
4934 	    if (26 - CharOrd(firstdigit) - 1 < Prenum1)
4935 	    {
4936 		if (isupper(firstdigit))
4937 		    firstdigit = 'Z';
4938 		else
4939 		    firstdigit = 'z';
4940 	    }
4941 	    else
4942 #ifdef EBCDIC
4943 		firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
4944 #else
4945 		firstdigit += Prenum1;
4946 #endif
4947 	}
4948 	curwin->w_cursor.col = col;
4949 	(void)del_char(FALSE);
4950 	ins_char(firstdigit);
4951     }
4952     else
4953     {
4954 	negative = FALSE;
4955 	if (col > 0 && ptr[col - 1] == '-')	    /* negative number */
4956 	{
4957 	    --col;
4958 	    negative = TRUE;
4959 	}
4960 
4961 	/* get the number value (unsigned) */
4962 	vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
4963 
4964 	/* ignore leading '-' for hex and octal numbers */
4965 	if (hex && negative)
4966 	{
4967 	    ++col;
4968 	    --length;
4969 	    negative = FALSE;
4970 	}
4971 
4972 	/* add or subtract */
4973 	subtract = FALSE;
4974 	if (command == Ctrl_X)
4975 	    subtract ^= TRUE;
4976 	if (negative)
4977 	    subtract ^= TRUE;
4978 
4979 	oldn = n;
4980 	if (subtract)
4981 	    n -= (unsigned long)Prenum1;
4982 	else
4983 	    n += (unsigned long)Prenum1;
4984 
4985 	/* handle wraparound for decimal numbers */
4986 	if (!hex)
4987 	{
4988 	    if (subtract)
4989 	    {
4990 		if (n > oldn)
4991 		{
4992 		    n = 1 + (n ^ (unsigned long)-1);
4993 		    negative ^= TRUE;
4994 		}
4995 	    }
4996 	    else /* add */
4997 	    {
4998 		if (n < oldn)
4999 		{
5000 		    n = (n ^ (unsigned long)-1);
5001 		    negative ^= TRUE;
5002 		}
5003 	    }
5004 	    if (n == 0)
5005 		negative = FALSE;
5006 	}
5007 
5008 	/*
5009 	 * Delete the old number.
5010 	 */
5011 	curwin->w_cursor.col = col;
5012 	todel = length;
5013 	c = gchar_cursor();
5014 	/*
5015 	 * Don't include the '-' in the length, only the length of the part
5016 	 * after it is kept the same.
5017 	 */
5018 	if (c == '-')
5019 	    --length;
5020 	while (todel-- > 0)
5021 	{
5022 	    if (c < 0x100 && isalpha(c))
5023 	    {
5024 		if (isupper(c))
5025 		    hexupper = TRUE;
5026 		else
5027 		    hexupper = FALSE;
5028 	    }
5029 	    /* del_char() will mark line needing displaying */
5030 	    (void)del_char(FALSE);
5031 	    c = gchar_cursor();
5032 	}
5033 
5034 	/*
5035 	 * Prepare the leading characters in buf1[].
5036 	 * When there are many leading zeros it could be very long.  Allocate
5037 	 * a bit too much.
5038 	 */
5039 	buf1 = alloc((unsigned)length + NUMBUFLEN);
5040 	if (buf1 == NULL)
5041 	    return FAIL;
5042 	ptr = buf1;
5043 	if (negative)
5044 	{
5045 	    *ptr++ = '-';
5046 	}
5047 	if (hex)
5048 	{
5049 	    *ptr++ = '0';
5050 	    --length;
5051 	}
5052 	if (hex == 'x' || hex == 'X')
5053 	{
5054 	    *ptr++ = hex;
5055 	    --length;
5056 	}
5057 
5058 	/*
5059 	 * Put the number characters in buf2[].
5060 	 */
5061 	if (hex == 0)
5062 	    sprintf((char *)buf2, "%lu", n);
5063 	else if (hex == '0')
5064 	    sprintf((char *)buf2, "%lo", n);
5065 	else if (hex && hexupper)
5066 	    sprintf((char *)buf2, "%lX", n);
5067 	else
5068 	    sprintf((char *)buf2, "%lx", n);
5069 	length -= (int)STRLEN(buf2);
5070 
5071 	/*
5072 	 * Adjust number of zeros to the new number of digits, so the
5073 	 * total length of the number remains the same.
5074 	 * Don't do this when
5075 	 * the result may look like an octal number.
5076 	 */
5077 	if (firstdigit == '0' && !(dooct && hex == 0))
5078 	    while (length-- > 0)
5079 		*ptr++ = '0';
5080 	*ptr = NUL;
5081 	STRCAT(buf1, buf2);
5082 	ins_str(buf1);		/* insert the new number */
5083 	vim_free(buf1);
5084     }
5085     --curwin->w_cursor.col;
5086     curwin->w_set_curswant = TRUE;
5087 #ifdef FEAT_RIGHTLEFT
5088     ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5089     RLADDSUBFIX(ptr);
5090 #endif
5091     return OK;
5092 }
5093 
5094 #ifdef FEAT_VIMINFO
5095     int
5096 read_viminfo_register(virp, force)
5097     vir_T	*virp;
5098     int		force;
5099 {
5100     int		eof;
5101     int		do_it = TRUE;
5102     int		size;
5103     int		limit;
5104     int		i;
5105     int		set_prev = FALSE;
5106     char_u	*str;
5107     char_u	**array = NULL;
5108 
5109     /* We only get here (hopefully) if line[0] == '"' */
5110     str = virp->vir_line + 1;
5111     if (*str == '"')
5112     {
5113 	set_prev = TRUE;
5114 	str++;
5115     }
5116     if (!ASCII_ISALNUM(*str) && *str != '-')
5117     {
5118 	if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5119 	    return TRUE;	/* too many errors, pretend end-of-file */
5120 	do_it = FALSE;
5121     }
5122     get_yank_register(*str++, FALSE);
5123     if (!force && y_current->y_array != NULL)
5124 	do_it = FALSE;
5125     size = 0;
5126     limit = 100;	/* Optimized for registers containing <= 100 lines */
5127     if (do_it)
5128     {
5129 	if (set_prev)
5130 	    y_previous = y_current;
5131 	vim_free(y_current->y_array);
5132 	array = y_current->y_array =
5133 		       (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5134 	str = skipwhite(str);
5135 	if (STRNCMP(str, "CHAR", 4) == 0)
5136 	    y_current->y_type = MCHAR;
5137 #ifdef FEAT_VISUAL
5138 	else if (STRNCMP(str, "BLOCK", 5) == 0)
5139 	    y_current->y_type = MBLOCK;
5140 #endif
5141 	else
5142 	    y_current->y_type = MLINE;
5143 	/* get the block width; if it's missing we get a zero, which is OK */
5144 	str = skipwhite(skiptowhite(str));
5145 #ifdef FEAT_VISUAL
5146 	y_current->y_width = getdigits(&str);
5147 #else
5148 	(void)getdigits(&str);
5149 #endif
5150     }
5151 
5152     while (!(eof = viminfo_readline(virp))
5153 		    && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5154     {
5155 	if (do_it)
5156 	{
5157 	    if (size >= limit)
5158 	    {
5159 		y_current->y_array = (char_u **)
5160 			      alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5161 		for (i = 0; i < limit; i++)
5162 		    y_current->y_array[i] = array[i];
5163 		vim_free(array);
5164 		limit *= 2;
5165 		array = y_current->y_array;
5166 	    }
5167 	    str = viminfo_readstring(virp, 1, TRUE);
5168 	    if (str != NULL)
5169 		array[size++] = str;
5170 	    else
5171 		do_it = FALSE;
5172 	}
5173     }
5174     if (do_it)
5175     {
5176 	if (size == 0)
5177 	{
5178 	    vim_free(array);
5179 	    y_current->y_array = NULL;
5180 	}
5181 	else if (size < limit)
5182 	{
5183 	    y_current->y_array =
5184 			(char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5185 	    for (i = 0; i < size; i++)
5186 		y_current->y_array[i] = array[i];
5187 	    vim_free(array);
5188 	}
5189 	y_current->y_size = size;
5190     }
5191     return eof;
5192 }
5193 
5194     void
5195 write_viminfo_registers(fp)
5196     FILE    *fp;
5197 {
5198     int	    i, j;
5199     char_u  *type;
5200     char_u  c;
5201     int	    num_lines;
5202     int	    max_num_lines;
5203     int	    max_kbyte;
5204     long    len;
5205 
5206     fprintf(fp, _("\n# Registers:\n"));
5207 
5208     /* Get '<' value, use old '"' value if '<' is not found. */
5209     max_num_lines = get_viminfo_parameter('<');
5210     if (max_num_lines < 0)
5211 	max_num_lines = get_viminfo_parameter('"');
5212     if (max_num_lines == 0)
5213 	return;
5214     max_kbyte = get_viminfo_parameter('s');
5215     if (max_kbyte == 0)
5216 	return;
5217     for (i = 0; i < NUM_REGISTERS; i++)
5218     {
5219 	if (y_regs[i].y_array == NULL)
5220 	    continue;
5221 #ifdef FEAT_CLIPBOARD
5222 	/* Skip '*'/'+' register, we don't want them back next time */
5223 	if (i == STAR_REGISTER || i == PLUS_REGISTER)
5224 	    continue;
5225 #endif
5226 #ifdef FEAT_DND
5227 	/* Neither do we want the '~' register */
5228 	if (i == TILDE_REGISTER)
5229 	    continue;
5230 #endif
5231 	/* Skip empty registers. */
5232 	num_lines = y_regs[i].y_size;
5233 	if (num_lines == 0
5234 		|| (num_lines == 1 && y_regs[i].y_type == MCHAR
5235 					&& STRLEN(y_regs[i].y_array[0]) == 0))
5236 	    continue;
5237 
5238 	if (max_kbyte > 0)
5239 	{
5240 	    /* Skip register if there is more text than the maximum size. */
5241 	    len = 0;
5242 	    for (j = 0; j < num_lines; j++)
5243 		len += STRLEN(y_regs[i].y_array[j]) + 1L;
5244 	    if (len > (long)max_kbyte * 1024L)
5245 		continue;
5246 	}
5247 
5248 	switch (y_regs[i].y_type)
5249 	{
5250 	    case MLINE:
5251 		type = (char_u *)"LINE";
5252 		break;
5253 	    case MCHAR:
5254 		type = (char_u *)"CHAR";
5255 		break;
5256 #ifdef FEAT_VISUAL
5257 	    case MBLOCK:
5258 		type = (char_u *)"BLOCK";
5259 		break;
5260 #endif
5261 	    default:
5262 		sprintf((char *)IObuff, _("E574: Unknown register type %d"),
5263 							    y_regs[i].y_type);
5264 		emsg(IObuff);
5265 		type = (char_u *)"LINE";
5266 		break;
5267 	}
5268 	if (y_previous == &y_regs[i])
5269 	    fprintf(fp, "\"");
5270 	c = get_register_name(i);
5271 	fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5272 #ifdef FEAT_VISUAL
5273 		    (int)y_regs[i].y_width
5274 #else
5275 		    0
5276 #endif
5277 		    );
5278 
5279 	/* If max_num_lines < 0, then we save ALL the lines in the register */
5280 	if (max_num_lines > 0 && num_lines > max_num_lines)
5281 	    num_lines = max_num_lines;
5282 	for (j = 0; j < num_lines; j++)
5283 	{
5284 	    putc('\t', fp);
5285 	    viminfo_writestring(fp, y_regs[i].y_array[j]);
5286 	}
5287     }
5288 }
5289 #endif /* FEAT_VIMINFO */
5290 
5291 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
5292 /*
5293  * SELECTION / PRIMARY ('*')
5294  *
5295  * Text selection stuff that uses the GUI selection register '*'.  When using a
5296  * GUI this may be text from another window, otherwise it is the last text we
5297  * had highlighted with VIsual mode.  With mouse support, clicking the middle
5298  * button performs the paste, otherwise you will need to do <"*p>. "
5299  * If not under X, it is synonymous with the clipboard register '+'.
5300  *
5301  * X CLIPBOARD ('+')
5302  *
5303  * Text selection stuff that uses the GUI clipboard register '+'.
5304  * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5305  * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5306  * otherwise you will need to do <"+p>. "
5307  * If not under X, it is synonymous with the selection register '*'.
5308  */
5309 
5310 /*
5311  * Routine to export any final X selection we had to the environment
5312  * so that the text is still available after vim has exited. X selections
5313  * only exist while the owning application exists, so we write to the
5314  * permanent (while X runs) store CUT_BUFFER0.
5315  * Dump the CLIPBOARD selection if we own it (it's logically the more
5316  * 'permanent' of the two), otherwise the PRIMARY one.
5317  * For now, use a hard-coded sanity limit of 1Mb of data.
5318  */
5319 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5320     void
5321 x11_export_final_selection()
5322 {
5323     Display	*dpy;
5324     char_u	*str = NULL;
5325     long_u	len = 0;
5326     int		motion_type = -1;
5327 
5328 # ifdef FEAT_GUI
5329     if (gui.in_use)
5330 	dpy = X_DISPLAY;
5331     else
5332 # endif
5333 # ifdef FEAT_XCLIPBOARD
5334 	dpy = xterm_dpy;
5335 # else
5336 	return;
5337 # endif
5338 
5339     /* Get selection to export */
5340     if (clip_plus.owned)
5341 	motion_type = clip_convert_selection(&str, &len, &clip_plus);
5342     else if (clip_star.owned)
5343 	motion_type = clip_convert_selection(&str, &len, &clip_star);
5344 
5345     /* Check it's OK */
5346     if (dpy != NULL && str != NULL && motion_type >= 0
5347 					       && len < 1024*1024 && len > 0)
5348     {
5349 	XStoreBuffer(dpy, (char *)str, (int)len, 0);
5350 	XFlush(dpy);
5351     }
5352 
5353     vim_free(str);
5354 }
5355 #endif
5356 
5357     void
5358 clip_free_selection(cbd)
5359     VimClipboard	*cbd;
5360 {
5361     struct yankreg *y_ptr = y_current;
5362 
5363     if (cbd == &clip_plus)
5364 	y_current = &y_regs[PLUS_REGISTER];
5365     else
5366 	y_current = &y_regs[STAR_REGISTER];
5367     free_yank_all();
5368     y_current->y_size = 0;
5369     y_current = y_ptr;
5370 }
5371 
5372 /*
5373  * Get the selected text and put it in the gui selection register '*' or '+'.
5374  */
5375     void
5376 clip_get_selection(cbd)
5377     VimClipboard	*cbd;
5378 {
5379     struct yankreg *old_y_previous, *old_y_current;
5380     pos_T	old_cursor;
5381 #ifdef FEAT_VISUAL
5382     pos_T	old_visual;
5383     int		old_visual_mode;
5384 #endif
5385     colnr_T	old_curswant;
5386     int		old_set_curswant;
5387     pos_T	old_op_start, old_op_end;
5388     oparg_T	oa;
5389     cmdarg_T	ca;
5390 
5391     if (cbd->owned)
5392     {
5393 	if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5394 		|| (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5395 	    return;
5396 
5397 	/* Get the text between clip_star.start & clip_star.end */
5398 	old_y_previous = y_previous;
5399 	old_y_current = y_current;
5400 	old_cursor = curwin->w_cursor;
5401 	old_curswant = curwin->w_curswant;
5402 	old_set_curswant = curwin->w_set_curswant;
5403 	old_op_start = curbuf->b_op_start;
5404 	old_op_end = curbuf->b_op_end;
5405 #ifdef FEAT_VISUAL
5406 	old_visual = VIsual;
5407 	old_visual_mode = VIsual_mode;
5408 #endif
5409 	clear_oparg(&oa);
5410 	oa.regname = (cbd == &clip_plus ? '+' : '*');
5411 	oa.op_type = OP_YANK;
5412 	vim_memset(&ca, 0, sizeof(ca));
5413 	ca.oap = &oa;
5414 	ca.cmdchar = 'y';
5415 	ca.count1 = 1;
5416 	ca.retval = CA_NO_ADJ_OP_END;
5417 	do_pending_operator(&ca, 0, TRUE);
5418 	y_previous = old_y_previous;
5419 	y_current = old_y_current;
5420 	curwin->w_cursor = old_cursor;
5421 	curwin->w_curswant = old_curswant;
5422 	curwin->w_set_curswant = old_set_curswant;
5423 	curbuf->b_op_start = old_op_start;
5424 	curbuf->b_op_end = old_op_end;
5425 #ifdef FEAT_VISUAL
5426 	VIsual = old_visual;
5427 	VIsual_mode = old_visual_mode;
5428 #endif
5429     }
5430     else
5431     {
5432 	clip_free_selection(cbd);
5433 
5434 	/* Try to get selected text from another window */
5435 	clip_gen_request_selection(cbd);
5436     }
5437 }
5438 
5439 /* Convert from the GUI selection string into the '*'/'+' register */
5440     void
5441 clip_yank_selection(type, str, len, cbd)
5442     int		type;
5443     char_u	*str;
5444     long	len;
5445     VimClipboard *cbd;
5446 {
5447     struct yankreg *y_ptr;
5448 
5449     if (cbd == &clip_plus)
5450 	y_ptr = &y_regs[PLUS_REGISTER];
5451     else
5452 	y_ptr = &y_regs[STAR_REGISTER];
5453 
5454     clip_free_selection(cbd);
5455 
5456     str_to_reg(y_ptr, type, str, len, 0L);
5457 }
5458 
5459 /*
5460  * Convert the '*'/'+' register into a GUI selection string returned in *str
5461  * with length *len.
5462  * Returns the motion type, or -1 for failure.
5463  */
5464     int
5465 clip_convert_selection(str, len, cbd)
5466     char_u	**str;
5467     long_u	*len;
5468     VimClipboard *cbd;
5469 {
5470     char_u	*p;
5471     int		lnum;
5472     int		i, j;
5473     int_u	eolsize;
5474     struct yankreg *y_ptr;
5475 
5476     if (cbd == &clip_plus)
5477 	y_ptr = &y_regs[PLUS_REGISTER];
5478     else
5479 	y_ptr = &y_regs[STAR_REGISTER];
5480 
5481 #ifdef USE_CRNL
5482     eolsize = 2;
5483 #else
5484     eolsize = 1;
5485 #endif
5486 
5487     *str = NULL;
5488     *len = 0;
5489     if (y_ptr->y_array == NULL)
5490 	return -1;
5491 
5492     for (i = 0; i < y_ptr->y_size; i++)
5493 	*len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5494 
5495     /*
5496      * Don't want newline character at end of last line if we're in MCHAR mode.
5497      */
5498     if (y_ptr->y_type == MCHAR && *len >= eolsize)
5499 	*len -= eolsize;
5500 
5501     p = *str = lalloc(*len + 1, TRUE);	/* add one to avoid zero */
5502     if (p == NULL)
5503 	return -1;
5504     lnum = 0;
5505     for (i = 0, j = 0; i < (int)*len; i++, j++)
5506     {
5507 	if (y_ptr->y_array[lnum][j] == '\n')
5508 	    p[i] = NUL;
5509 	else if (y_ptr->y_array[lnum][j] == NUL)
5510 	{
5511 #ifdef USE_CRNL
5512 	    p[i++] = '\r';
5513 #endif
5514 #ifdef USE_CR
5515 	    p[i] = '\r';
5516 #else
5517 	    p[i] = '\n';
5518 #endif
5519 	    lnum++;
5520 	    j = -1;
5521 	}
5522 	else
5523 	    p[i] = y_ptr->y_array[lnum][j];
5524     }
5525     return y_ptr->y_type;
5526 }
5527 
5528 
5529 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5530 /*
5531  * If we have written to a clipboard register, send the text to the clipboard.
5532  */
5533     static void
5534 may_set_selection()
5535 {
5536     if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5537     {
5538 	clip_own_selection(&clip_star);
5539 	clip_gen_set_selection(&clip_star);
5540     }
5541     else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5542     {
5543 	clip_own_selection(&clip_plus);
5544 	clip_gen_set_selection(&clip_plus);
5545     }
5546 }
5547 # endif
5548 
5549 #endif /* FEAT_CLIPBOARD || PROTO */
5550 
5551 
5552 #if defined(FEAT_DND) || defined(PROTO)
5553 /*
5554  * Replace the contents of the '~' register with str.
5555  */
5556     void
5557 dnd_yank_drag_data(str, len)
5558     char_u	*str;
5559     long	len;
5560 {
5561     struct yankreg *curr;
5562 
5563     curr = y_current;
5564     y_current = &y_regs[TILDE_REGISTER];
5565     free_yank_all();
5566     str_to_reg(y_current, MCHAR, str, len, 0L);
5567     y_current = curr;
5568 }
5569 #endif
5570 
5571 
5572 #if defined(FEAT_EVAL) || defined(PROTO)
5573 /*
5574  * Return the type of a register.
5575  * Used for getregtype()
5576  * Returns MAUTO for error.
5577  */
5578     char_u
5579 get_reg_type(regname, reglen)
5580     int	    regname;
5581     long    *reglen;
5582 {
5583     switch (regname)
5584     {
5585 	case '%':		/* file name */
5586 	case '#':		/* alternate file name */
5587 	case '=':		/* expression */
5588 	case ':':		/* last command line */
5589 	case '/':		/* last search-pattern */
5590 	case '.':		/* last inserted text */
5591 #ifdef FEAT_SEARCHPATH
5592 	case Ctrl_F:		/* Filename under cursor */
5593 	case Ctrl_P:		/* Path under cursor, expand via "path" */
5594 #endif
5595 	case Ctrl_W:		/* word under cursor */
5596 	case Ctrl_A:		/* WORD (mnemonic All) under cursor */
5597 	case '_':		/* black hole: always empty */
5598 	    return MCHAR;
5599     }
5600 
5601 #ifdef FEAT_CLIPBOARD
5602     regname = may_get_selection(regname);
5603 #endif
5604 
5605     /* Should we check for a valid name? */
5606     get_yank_register(regname, FALSE);
5607 
5608     if (y_current->y_array != NULL)
5609     {
5610 #ifdef FEAT_VISUAL
5611 	if (reglen != NULL && y_current->y_type == MBLOCK)
5612 	    *reglen = y_current->y_width;
5613 #endif
5614 	return y_current->y_type;
5615     }
5616     return MAUTO;
5617 }
5618 
5619 /*
5620  * Return the contents of a register as a single allocated string.
5621  * Used for "@r" in expressions and for getreg().
5622  * Returns NULL for error.
5623  */
5624     char_u *
5625 get_reg_contents(regname, allowexpr, expr_src)
5626     int		regname;
5627     int		allowexpr;	/* allow "=" register */
5628     int		expr_src;	/* get expression for "=" register */
5629 {
5630     long	i;
5631     char_u	*retval;
5632     int		allocated;
5633     long	len;
5634 
5635     /* Don't allow using an expression register inside an expression */
5636     if (regname == '=')
5637     {
5638 	if (allowexpr)
5639 	{
5640 	    if (expr_src)
5641 		return get_expr_line_src();
5642 	    return get_expr_line();
5643 	}
5644 	return NULL;
5645     }
5646 
5647     if (regname == '@')	    /* "@@" is used for unnamed register */
5648 	regname = '"';
5649 
5650     /* check for valid regname */
5651     if (regname != NUL && !valid_yank_reg(regname, FALSE))
5652 	return NULL;
5653 
5654 #ifdef FEAT_CLIPBOARD
5655     regname = may_get_selection(regname);
5656 #endif
5657 
5658     if (get_spec_reg(regname, &retval, &allocated, FALSE))
5659     {
5660 	if (retval == NULL)
5661 	    return NULL;
5662 	if (!allocated)
5663 	    retval = vim_strsave(retval);
5664 	return retval;
5665     }
5666 
5667     get_yank_register(regname, FALSE);
5668     if (y_current->y_array == NULL)
5669 	return NULL;
5670 
5671     /*
5672      * Compute length of resulting string.
5673      */
5674     len = 0;
5675     for (i = 0; i < y_current->y_size; ++i)
5676     {
5677 	len += (long)STRLEN(y_current->y_array[i]);
5678 	/*
5679 	 * Insert a newline between lines and after last line if
5680 	 * y_type is MLINE.
5681 	 */
5682 	if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5683 	    ++len;
5684     }
5685 
5686     retval = lalloc(len + 1, TRUE);
5687 
5688     /*
5689      * Copy the lines of the yank register into the string.
5690      */
5691     if (retval != NULL)
5692     {
5693 	len = 0;
5694 	for (i = 0; i < y_current->y_size; ++i)
5695 	{
5696 	    STRCPY(retval + len, y_current->y_array[i]);
5697 	    len += (long)STRLEN(retval + len);
5698 
5699 	    /*
5700 	     * Insert a NL between lines and after the last line if y_type is
5701 	     * MLINE.
5702 	     */
5703 	    if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5704 		retval[len++] = '\n';
5705 	}
5706 	retval[len] = NUL;
5707     }
5708 
5709     return retval;
5710 }
5711 
5712 /*
5713  * Store string "str" in register "name".
5714  * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5715  * If "must_append" is TRUE, always append to the register.  Otherwise append
5716  * if "name" is an uppercase letter.
5717  * Note: "maxlen" and "must_append" don't work for the "/" register.
5718  * Careful: 'str' is modified, you may have to use a copy!
5719  * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5720  */
5721     void
5722 write_reg_contents(name, str, maxlen, must_append)
5723     int		name;
5724     char_u	*str;
5725     int		maxlen;
5726     int		must_append;
5727 {
5728     write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5729 }
5730 
5731     void
5732 write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5733     int		name;
5734     char_u	*str;
5735     int		maxlen;
5736     int		must_append;
5737     int		yank_type;
5738     long	block_len;
5739 {
5740     struct yankreg  *old_y_previous, *old_y_current;
5741     long	    len;
5742 
5743     if (maxlen >= 0)
5744 	len = maxlen;
5745     else
5746 	len = (long)STRLEN(str);
5747 
5748     /* Special case: '/' search pattern */
5749     if (name == '/')
5750     {
5751 	set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5752 	return;
5753     }
5754 
5755 #ifdef FEAT_EVAL
5756     if (name == '=')
5757     {
5758 	char_u	    *p, *s;
5759 
5760 	p = vim_strnsave(str, (int)len);
5761 	if (p == NULL)
5762 	    return;
5763 	if (must_append)
5764 	{
5765 	    s = concat_str(get_expr_line_src(), p);
5766 	    vim_free(p);
5767 	    p = s;
5768 
5769 	}
5770 	set_expr_line(p);
5771 	return;
5772     }
5773 #endif
5774 
5775     if (!valid_yank_reg(name, TRUE))	    /* check for valid reg name */
5776     {
5777 	emsg_invreg(name);
5778 	return;
5779     }
5780 
5781     if (name == '_')	    /* black hole: nothing to do */
5782 	return;
5783 
5784     /* Don't want to change the current (unnamed) register */
5785     old_y_previous = y_previous;
5786     old_y_current = y_current;
5787 
5788     get_yank_register(name, TRUE);
5789     if (!y_append && !must_append)
5790 	free_yank_all();
5791 #ifndef FEAT_VISUAL
5792     /* Just in case - make sure we don't use MBLOCK */
5793     if (yank_type == MBLOCK)
5794 	yank_type = MAUTO;
5795 #endif
5796     if (yank_type == MAUTO)
5797 	yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5798 							     ? MLINE : MCHAR);
5799     str_to_reg(y_current, yank_type, str, len, block_len);
5800 
5801 # ifdef FEAT_CLIPBOARD
5802     /* Send text of clipboard register to the clipboard. */
5803     may_set_selection();
5804 # endif
5805 
5806     /* ':let @" = "val"' should change the meaning of the "" register */
5807     if (name != '"')
5808 	y_previous = old_y_previous;
5809     y_current = old_y_current;
5810 }
5811 #endif	/* FEAT_EVAL */
5812 
5813 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5814 /*
5815  * Put a string into a register.  When the register is not empty, the string
5816  * is appended.
5817  */
5818     static void
5819 str_to_reg(y_ptr, type, str, len, blocklen)
5820     struct yankreg	*y_ptr;		/* pointer to yank register */
5821     int			type;		/* MCHAR, MLINE or MBLOCK */
5822     char_u		*str;		/* string to put in register */
5823     long		len;		/* length of string */
5824     long		blocklen;	/* width of Visual block */
5825 {
5826     int		lnum;
5827     long	start;
5828     long	i;
5829     int		extra;
5830     int		newlines;		/* number of lines added */
5831     int		extraline = 0;		/* extra line at the end */
5832     int		append = FALSE;		/* append to last line in register */
5833     char_u	*s;
5834     char_u	**pp;
5835 #ifdef FEAT_VISUAL
5836     long	maxlen;
5837 #endif
5838 
5839     if (y_ptr->y_array == NULL)		/* NULL means emtpy register */
5840 	y_ptr->y_size = 0;
5841 
5842     /*
5843      * Count the number of lines within the string
5844      */
5845     newlines = 0;
5846     for (i = 0; i < len; i++)
5847 	if (str[i] == '\n')
5848 	    ++newlines;
5849     if (type == MCHAR || len == 0 || str[len - 1] != '\n')
5850     {
5851 	extraline = 1;
5852 	++newlines;	/* count extra newline at the end */
5853     }
5854     if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
5855     {
5856 	append = TRUE;
5857 	--newlines;	/* uncount newline when appending first line */
5858     }
5859 
5860     /*
5861      * Allocate an array to hold the pointers to the new register lines.
5862      * If the register was not empty, move the existing lines to the new array.
5863      */
5864     pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
5865 						    * sizeof(char_u *), TRUE);
5866     if (pp == NULL)	/* out of memory */
5867 	return;
5868     for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
5869 	pp[lnum] = y_ptr->y_array[lnum];
5870     vim_free(y_ptr->y_array);
5871     y_ptr->y_array = pp;
5872 #ifdef FEAT_VISUAL
5873     maxlen = 0;
5874 #endif
5875 
5876     /*
5877      * Find the end of each line and save it into the array.
5878      */
5879     for (start = 0; start < len + extraline; start += i + 1)
5880     {
5881 	for (i = start; i < len; ++i)	/* find the end of the line */
5882 	    if (str[i] == '\n')
5883 		break;
5884 	i -= start;			/* i is now length of line */
5885 #ifdef FEAT_VISUAL
5886 	if (i > maxlen)
5887 	    maxlen = i;
5888 #endif
5889 	if (append)
5890 	{
5891 	    --lnum;
5892 	    extra = (int)STRLEN(y_ptr->y_array[lnum]);
5893 	}
5894 	else
5895 	    extra = 0;
5896 	s = alloc((unsigned)(i + extra + 1));
5897 	if (s == NULL)
5898 	    break;
5899 	if (extra)
5900 	    mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
5901 	if (append)
5902 	    vim_free(y_ptr->y_array[lnum]);
5903 	if (i)
5904 	    mch_memmove(s + extra, str + start, (size_t)i);
5905 	extra += i;
5906 	s[extra] = NUL;
5907 	y_ptr->y_array[lnum++] = s;
5908 	while (--extra >= 0)
5909 	{
5910 	    if (*s == NUL)
5911 		*s = '\n';	    /* replace NUL with newline */
5912 	    ++s;
5913 	}
5914 	append = FALSE;		    /* only first line is appended */
5915     }
5916     y_ptr->y_type = type;
5917     y_ptr->y_size = lnum;
5918 # ifdef FEAT_VISUAL
5919     if (type == MBLOCK)
5920 	y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
5921     else
5922 	y_ptr->y_width = 0;
5923 # endif
5924 }
5925 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
5926 
5927     void
5928 clear_oparg(oap)
5929     oparg_T	*oap;
5930 {
5931     vim_memset(oap, 0, sizeof(oparg_T));
5932 }
5933 
5934 static long	line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
5935 
5936 /*
5937  *  Count the number of bytes, characters and "words" in a line.
5938  *
5939  *  "Words" are counted by looking for boundaries between non-space and
5940  *  space characters.  (it seems to produce results that match 'wc'.)
5941  *
5942  *  Return value is byte count; word count for the line is added to "*wc".
5943  *  Char count is added to "*cc".
5944  *
5945  *  The function will only examine the first "limit" characters in the
5946  *  line, stopping if it encounters an end-of-line (NUL byte).  In that
5947  *  case, eol_size will be added to the character count to account for
5948  *  the size of the EOL character.
5949  */
5950     static long
5951 line_count_info(line, wc, cc, limit, eol_size)
5952     char_u	*line;
5953     long	*wc;
5954     long	*cc;
5955     long	limit;
5956     int		eol_size;
5957 {
5958     long	i;
5959     long	words = 0;
5960     long	chars = 0;
5961     int		is_word = 0;
5962 
5963     for (i = 0; line[i] && i < limit; )
5964     {
5965 	if (is_word)
5966 	{
5967 	    if (vim_isspace(line[i]))
5968 	    {
5969 		words++;
5970 		is_word = 0;
5971 	    }
5972 	}
5973 	else if (!vim_isspace(line[i]))
5974 	    is_word = 1;
5975 	++chars;
5976 #ifdef FEAT_MBYTE
5977 	i += (*mb_ptr2len)(line + i);
5978 #else
5979 	++i;
5980 #endif
5981     }
5982 
5983     if (is_word)
5984 	words++;
5985     *wc += words;
5986 
5987     /* Add eol_size if the end of line was reached before hitting limit. */
5988     if (line[i] == NUL && i < limit)
5989     {
5990 	i += eol_size;
5991 	chars += eol_size;
5992     }
5993     *cc += chars;
5994     return i;
5995 }
5996 
5997 /*
5998  * Give some info about the position of the cursor (for "g CTRL-G").
5999  * In Visual mode, give some info about the selected region.  (In this case,
6000  * the *_count_cursor variables store running totals for the selection.)
6001  */
6002     void
6003 cursor_pos_info()
6004 {
6005     char_u	*p;
6006     char_u	buf1[50];
6007     char_u	buf2[40];
6008     linenr_T	lnum;
6009     long	byte_count = 0;
6010     long	byte_count_cursor = 0;
6011     long	char_count = 0;
6012     long	char_count_cursor = 0;
6013     long	word_count = 0;
6014     long	word_count_cursor = 0;
6015     int		eol_size;
6016     long	last_check = 100000L;
6017 #ifdef FEAT_VISUAL
6018     long	line_count_selected = 0;
6019     pos_T	min_pos, max_pos;
6020     oparg_T	oparg;
6021     struct block_def	bd;
6022 #endif
6023 
6024     /*
6025      * Compute the length of the file in characters.
6026      */
6027     if (curbuf->b_ml.ml_flags & ML_EMPTY)
6028     {
6029 	MSG(_(no_lines_msg));
6030     }
6031     else
6032     {
6033 	if (get_fileformat(curbuf) == EOL_DOS)
6034 	    eol_size = 2;
6035 	else
6036 	    eol_size = 1;
6037 
6038 #ifdef FEAT_VISUAL
6039 	if (VIsual_active)
6040 	{
6041 	    if (lt(VIsual, curwin->w_cursor))
6042 	    {
6043 		min_pos = VIsual;
6044 		max_pos = curwin->w_cursor;
6045 	    }
6046 	    else
6047 	    {
6048 		min_pos = curwin->w_cursor;
6049 		max_pos = VIsual;
6050 	    }
6051 	    if (*p_sel == 'e' && max_pos.col > 0)
6052 		--max_pos.col;
6053 
6054 	    if (VIsual_mode == Ctrl_V)
6055 	    {
6056 		oparg.is_VIsual = 1;
6057 		oparg.block_mode = TRUE;
6058 		oparg.op_type = OP_NOP;
6059 		getvcols(curwin, &min_pos, &max_pos,
6060 			&oparg.start_vcol, &oparg.end_vcol);
6061 		/* Swap the start, end vcol if needed */
6062 		if (oparg.end_vcol < oparg.start_vcol)
6063 		{
6064 		    oparg.end_vcol += oparg.start_vcol;
6065 		    oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6066 		    oparg.end_vcol -= oparg.start_vcol;
6067 		}
6068 	    }
6069 	    line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6070 	}
6071 #endif
6072 
6073 	for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6074 	{
6075 	    /* Check for a CTRL-C every 100000 characters. */
6076 	    if (byte_count > last_check)
6077 	    {
6078 		ui_breakcheck();
6079 		if (got_int)
6080 		    return;
6081 		last_check = byte_count + 100000L;
6082 	    }
6083 
6084 #ifdef FEAT_VISUAL
6085 	    /* Do extra processing for VIsual mode. */
6086 	    if (VIsual_active
6087 		    && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6088 	    {
6089 		char_u	    *s = NULL;
6090 		long	    len = 0L;
6091 
6092 		switch (VIsual_mode)
6093 		{
6094 		    case Ctrl_V:
6095 # ifdef FEAT_VIRTUALEDIT
6096 			virtual_op = virtual_active();
6097 # endif
6098 			block_prep(&oparg, &bd, lnum, 0);
6099 # ifdef FEAT_VIRTUALEDIT
6100 			virtual_op = MAYBE;
6101 # endif
6102 			s = bd.textstart;
6103 			len = (long)bd.textlen;
6104 			break;
6105 		    case 'V':
6106 			s = ml_get(lnum);
6107 			len = MAXCOL;
6108 			break;
6109 		    case 'v':
6110 			{
6111 			    colnr_T start_col = (lnum == min_pos.lnum)
6112 							   ? min_pos.col : 0;
6113 			    colnr_T end_col = (lnum == max_pos.lnum)
6114 				      ? max_pos.col - start_col + 1 : MAXCOL;
6115 
6116 			    s = ml_get(lnum) + start_col;
6117 			    len = end_col;
6118 			}
6119 			break;
6120 		}
6121 		if (s != NULL)
6122 		{
6123 		    byte_count_cursor += line_count_info(s, &word_count_cursor,
6124 					   &char_count_cursor, len, eol_size);
6125 		    if (lnum == curbuf->b_ml.ml_line_count
6126 			    && !curbuf->b_p_eol
6127 			    && curbuf->b_p_bin
6128 			    && (long)STRLEN(s) < len)
6129 			byte_count_cursor -= eol_size;
6130 		}
6131 	    }
6132 	    else
6133 #endif
6134 	    {
6135 		/* In non-visual mode, check for the line the cursor is on */
6136 		if (lnum == curwin->w_cursor.lnum)
6137 		{
6138 		    word_count_cursor += word_count;
6139 		    char_count_cursor += char_count;
6140 		    byte_count_cursor = byte_count +
6141 			line_count_info(ml_get(lnum),
6142 				&word_count_cursor, &char_count_cursor,
6143 				  (long)(curwin->w_cursor.col + 1), eol_size);
6144 		}
6145 	    }
6146 	    /* Add to the running totals */
6147 	    byte_count += line_count_info(ml_get(lnum), &word_count,
6148 					 &char_count, (long)MAXCOL, eol_size);
6149 	}
6150 
6151 	/* Correction for when last line doesn't have an EOL. */
6152 	if (!curbuf->b_p_eol && curbuf->b_p_bin)
6153 	    byte_count -= eol_size;
6154 
6155 #ifdef FEAT_VISUAL
6156 	if (VIsual_active)
6157 	{
6158 	    if (VIsual_mode == Ctrl_V)
6159 	    {
6160 		getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
6161 			&max_pos.col);
6162 		sprintf((char *)buf1, _("%ld Cols; "),
6163 			(long)(oparg.end_vcol - oparg.start_vcol + 1));
6164 	    }
6165 	    else
6166 		buf1[0] = NUL;
6167 
6168 	    if (char_count_cursor == byte_count_cursor
6169 						  && char_count == byte_count)
6170 		sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
6171 			buf1, line_count_selected,
6172 			(long)curbuf->b_ml.ml_line_count,
6173 			word_count_cursor, word_count,
6174 			byte_count_cursor, byte_count);
6175 	    else
6176 		sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6177 			buf1, line_count_selected,
6178 			(long)curbuf->b_ml.ml_line_count,
6179 			word_count_cursor, word_count,
6180 			char_count_cursor, char_count,
6181 			byte_count_cursor, byte_count);
6182 	}
6183 	else
6184 #endif
6185 	{
6186 	    p = ml_get_curline();
6187 	    validate_virtcol();
6188 	    col_print(buf1, (int)curwin->w_cursor.col + 1,
6189 		    (int)curwin->w_virtcol + 1);
6190 	    col_print(buf2, (int)STRLEN(p), linetabsize(p));
6191 
6192 	    if (char_count_cursor == byte_count_cursor
6193 		    && char_count == byte_count)
6194 		sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
6195 		    (char *)buf1, (char *)buf2,
6196 		    (long)curwin->w_cursor.lnum,
6197 		    (long)curbuf->b_ml.ml_line_count,
6198 		    word_count_cursor, word_count,
6199 		    byte_count_cursor, byte_count);
6200 	    else
6201 		sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6202 		    (char *)buf1, (char *)buf2,
6203 		    (long)curwin->w_cursor.lnum,
6204 		    (long)curbuf->b_ml.ml_line_count,
6205 		    word_count_cursor, word_count,
6206 		    char_count_cursor, char_count,
6207 		    byte_count_cursor, byte_count);
6208 	}
6209 
6210 #ifdef FEAT_MBYTE
6211 	byte_count = bomb_size();
6212 	if (byte_count > 0)
6213 	    sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
6214 								  byte_count);
6215 #endif
6216 	/* Don't shorten this message, the user asked for it. */
6217 	p = p_shm;
6218 	p_shm = (char_u *)"";
6219 	msg(IObuff);
6220 	p_shm = p;
6221     }
6222 }
6223