Merged from the latest developing branch.
[MacVim/KaoriYa.git] / src / ops.c
blobea5e20bd62666510372b4776c28f7569477da232
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
15 #include "vim.h"
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
27 * Symbolic names for some registers.
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
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
53 * Each yank register is an array of pointers to lines.
55 static struct yankreg
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];
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 */
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
73 struct block_def
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 */
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((int silent));
99 static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
100 int silent));
101 static void stuffescaped __ARGS((char_u *arg, int literally));
102 #ifdef FEAT_MBYTE
103 static void mb_adjust_opend __ARGS((oparg_T *oap));
104 #endif
105 static void free_yank __ARGS((long));
106 static void free_yank_all __ARGS((void));
107 static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
108 #ifdef FEAT_CLIPBOARD
109 static void copy_yank_reg __ARGS((struct yankreg *reg));
110 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
111 static void may_set_selection __ARGS((void));
112 # endif
113 #endif
114 static void dis_msg __ARGS((char_u *p, int skip_esc));
115 #ifdef FEAT_VISUAL
116 static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
117 #endif
118 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
119 static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
120 #endif
121 static int ends_in_white __ARGS((linenr_T lnum));
122 #ifdef FEAT_COMMENTS
123 static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
124 static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
125 #else
126 static int fmt_check_par __ARGS((linenr_T));
127 #endif
130 * The names of operators.
131 * IMPORTANT: Index must correspond with defines in vim.h!!!
132 * The third field indicates whether the operator always works on lines.
134 static char opchars[][3] =
136 {NUL, NUL, FALSE}, /* OP_NOP */
137 {'d', NUL, FALSE}, /* OP_DELETE */
138 {'y', NUL, FALSE}, /* OP_YANK */
139 {'c', NUL, FALSE}, /* OP_CHANGE */
140 {'<', NUL, TRUE}, /* OP_LSHIFT */
141 {'>', NUL, TRUE}, /* OP_RSHIFT */
142 {'!', NUL, TRUE}, /* OP_FILTER */
143 {'g', '~', FALSE}, /* OP_TILDE */
144 {'=', NUL, TRUE}, /* OP_INDENT */
145 {'g', 'q', TRUE}, /* OP_FORMAT */
146 {':', NUL, TRUE}, /* OP_COLON */
147 {'g', 'U', FALSE}, /* OP_UPPER */
148 {'g', 'u', FALSE}, /* OP_LOWER */
149 {'J', NUL, TRUE}, /* DO_JOIN */
150 {'g', 'J', TRUE}, /* DO_JOIN_NS */
151 {'g', '?', FALSE}, /* OP_ROT13 */
152 {'r', NUL, FALSE}, /* OP_REPLACE */
153 {'I', NUL, FALSE}, /* OP_INSERT */
154 {'A', NUL, FALSE}, /* OP_APPEND */
155 {'z', 'f', TRUE}, /* OP_FOLD */
156 {'z', 'o', TRUE}, /* OP_FOLDOPEN */
157 {'z', 'O', TRUE}, /* OP_FOLDOPENREC */
158 {'z', 'c', TRUE}, /* OP_FOLDCLOSE */
159 {'z', 'C', TRUE}, /* OP_FOLDCLOSEREC */
160 {'z', 'd', TRUE}, /* OP_FOLDDEL */
161 {'z', 'D', TRUE}, /* OP_FOLDDELREC */
162 {'g', 'w', TRUE}, /* OP_FORMAT2 */
163 {'g', '@', FALSE}, /* OP_FUNCTION */
167 * Translate a command name into an operator type.
168 * Must only be called with a valid operator name!
171 get_op_type(char1, char2)
172 int char1;
173 int char2;
175 int i;
177 if (char1 == 'r') /* ignore second character */
178 return OP_REPLACE;
179 if (char1 == '~') /* when tilde is an operator */
180 return OP_TILDE;
181 for (i = 0; ; ++i)
182 if (opchars[i][0] == char1 && opchars[i][1] == char2)
183 break;
184 return i;
187 #if defined(FEAT_VISUAL) || defined(PROTO)
189 * Return TRUE if operator "op" always works on whole lines.
192 op_on_lines(op)
193 int op;
195 return opchars[op][2];
197 #endif
200 * Get first operator command character.
201 * Returns 'g' or 'z' if there is another command character.
204 get_op_char(optype)
205 int optype;
207 return opchars[optype][0];
211 * Get second operator command character.
214 get_extra_op_char(optype)
215 int optype;
217 return opchars[optype][1];
221 * op_shift - handle a shift operation
223 void
224 op_shift(oap, curs_top, amount)
225 oparg_T *oap;
226 int curs_top;
227 int amount;
229 long i;
230 int first_char;
231 char_u *s;
232 #ifdef FEAT_VISUAL
233 int block_col = 0;
234 #endif
236 if (u_save((linenr_T)(oap->start.lnum - 1),
237 (linenr_T)(oap->end.lnum + 1)) == FAIL)
238 return;
240 #ifdef FEAT_VISUAL
241 if (oap->block_mode)
242 block_col = curwin->w_cursor.col;
243 #endif
245 for (i = oap->line_count; --i >= 0; )
247 first_char = *ml_get_curline();
248 if (first_char == NUL) /* empty line */
249 curwin->w_cursor.col = 0;
250 #ifdef FEAT_VISUALEXTRA
251 else if (oap->block_mode)
252 shift_block(oap, amount);
253 #endif
254 else
255 /* Move the line right if it doesn't start with '#', 'smartindent'
256 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */
257 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
258 if (first_char != '#' || !preprocs_left())
259 #endif
261 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount);
263 ++curwin->w_cursor.lnum;
266 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
268 #ifdef FEAT_VISUAL
269 if (oap->block_mode)
271 curwin->w_cursor.lnum = oap->start.lnum;
272 curwin->w_cursor.col = block_col;
274 else
275 #endif
276 if (curs_top) /* put cursor on first line, for ">>" */
278 curwin->w_cursor.lnum = oap->start.lnum;
279 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
281 else
282 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
284 if (oap->line_count > p_report)
286 if (oap->op_type == OP_RSHIFT)
287 s = (char_u *)">";
288 else
289 s = (char_u *)"<";
290 if (oap->line_count == 1)
292 if (amount == 1)
293 sprintf((char *)IObuff, _("1 line %sed 1 time"), s);
294 else
295 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount);
297 else
299 if (amount == 1)
300 sprintf((char *)IObuff, _("%ld lines %sed 1 time"),
301 oap->line_count, s);
302 else
303 sprintf((char *)IObuff, _("%ld lines %sed %d times"),
304 oap->line_count, s, amount);
306 msg(IObuff);
310 * Set "'[" and "']" marks.
312 curbuf->b_op_start = oap->start;
313 curbuf->b_op_end.lnum = oap->end.lnum;
314 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
315 if (curbuf->b_op_end.col > 0)
316 --curbuf->b_op_end.col;
320 * shift the current line one shiftwidth left (if left != 0) or right
321 * leaves cursor on first blank in the line
323 void
324 shift_line(left, round, amount)
325 int left;
326 int round;
327 int amount;
329 int count;
330 int i, j;
331 int p_sw = (int)curbuf->b_p_sw;
333 count = get_indent(); /* get current indent */
335 if (round) /* round off indent */
337 i = count / p_sw; /* number of p_sw rounded down */
338 j = count % p_sw; /* extra spaces */
339 if (j && left) /* first remove extra spaces */
340 --amount;
341 if (left)
343 i -= amount;
344 if (i < 0)
345 i = 0;
347 else
348 i += amount;
349 count = i * p_sw;
351 else /* original vi indent */
353 if (left)
355 count -= p_sw * amount;
356 if (count < 0)
357 count = 0;
359 else
360 count += p_sw * amount;
363 /* Set new indent */
364 #ifdef FEAT_VREPLACE
365 if (State & VREPLACE_FLAG)
366 change_indent(INDENT_SET, count, FALSE, NUL);
367 else
368 #endif
369 (void)set_indent(count, SIN_CHANGED);
372 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
374 * Shift one line of the current block one shiftwidth right or left.
375 * Leaves cursor on first character in block.
377 static void
378 shift_block(oap, amount)
379 oparg_T *oap;
380 int amount;
382 int left = (oap->op_type == OP_LSHIFT);
383 int oldstate = State;
384 int total, split;
385 char_u *newp, *oldp, *midp, *ptr;
386 int oldcol = curwin->w_cursor.col;
387 int p_sw = (int)curbuf->b_p_sw;
388 int p_ts = (int)curbuf->b_p_ts;
389 struct block_def bd;
390 int internal = 0;
391 int incr;
392 colnr_T vcol, col = 0, ws_vcol;
393 int i = 0, j = 0;
394 int len;
396 #ifdef FEAT_RIGHTLEFT
397 int old_p_ri = p_ri;
399 p_ri = 0; /* don't want revins in ident */
400 #endif
402 State = INSERT; /* don't want REPLACE for State */
403 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
404 if (bd.is_short)
405 return;
407 /* total is number of screen columns to be inserted/removed */
408 total = amount * p_sw;
409 oldp = ml_get_curline();
411 if (!left)
414 * 1. Get start vcol
415 * 2. Total ws vcols
416 * 3. Divvy into TABs & spp
417 * 4. Construct new string
419 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */
420 ws_vcol = bd.start_vcol - bd.pre_whitesp;
421 if (bd.startspaces)
423 #ifdef FEAT_MBYTE
424 if (has_mbyte)
425 bd.textstart += (*mb_ptr2len)(bd.textstart);
426 #endif
427 ++bd.textstart;
429 for ( ; vim_iswhite(*bd.textstart); )
431 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
432 total += incr;
433 bd.start_vcol += incr;
435 /* OK, now total=all the VWS reqd, and textstart points at the 1st
436 * non-ws char in the block. */
437 if (!curbuf->b_p_et)
438 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */
439 if (i)
440 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */
441 else
442 j = total;
443 /* if we're splitting a TAB, allow for it */
444 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0);
445 len = (int)STRLEN(bd.textstart) + 1;
446 newp = alloc_check((unsigned)(bd.textcol + i + j + len));
447 if (newp == NULL)
448 return;
449 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len));
450 mch_memmove(newp, oldp, (size_t)bd.textcol);
451 copy_chars(newp + bd.textcol, (size_t)i, TAB);
452 copy_spaces(newp + bd.textcol + i, (size_t)j);
453 /* the end */
454 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len);
456 else /* left */
458 vcol = oap->start_vcol;
459 /* walk vcol past ws to be removed */
460 for (midp = oldp + bd.textcol;
461 vcol < (oap->start_vcol + total) && vim_iswhite(*midp); )
463 incr = lbr_chartabsize_adv(&midp, (colnr_T)vcol);
464 vcol += incr;
466 /* internal is the block-internal ws replacing a split TAB */
467 if (vcol > (oap->start_vcol + total))
469 /* we have to split the TAB *(midp-1) */
470 internal = vcol - (oap->start_vcol + total);
472 /* if 'expandtab' is not set, use TABs */
474 split = bd.startspaces + internal;
475 if (split > 0)
477 if (!curbuf->b_p_et)
479 for (ptr = oldp, col = 0; ptr < oldp+bd.textcol; )
480 col += lbr_chartabsize_adv(&ptr, (colnr_T)col);
482 /* col+1 now equals the start col of the first char of the
483 * block (may be < oap.start_vcol if we're splitting a TAB) */
484 i = ((col % p_ts) + split) / p_ts; /* number of tabs */
486 if (i)
487 j = ((col % p_ts) + split) % p_ts; /* number of spp */
488 else
489 j = split;
492 newp = alloc_check(bd.textcol + i + j + (unsigned)STRLEN(midp) + 1);
493 if (newp == NULL)
494 return;
495 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + STRLEN(midp) + 1));
497 /* copy first part we want to keep */
498 mch_memmove(newp, oldp, (size_t)bd.textcol);
499 /* Now copy any TABS and spp to ensure correct alignment! */
500 while (vim_iswhite(*midp))
502 if (*midp == TAB)
503 i++;
504 else /*space */
505 j++;
506 midp++;
508 /* We might have an extra TAB worth of spp now! */
509 if (j / p_ts && !curbuf->b_p_et)
511 i++;
512 j -= p_ts;
514 copy_chars(newp + bd.textcol, (size_t)i, TAB);
515 copy_spaces(newp + bd.textcol + i, (size_t)j);
517 /* the end */
518 mch_memmove(newp + STRLEN(newp), midp, (size_t)STRLEN(midp) + 1);
520 /* replace the line */
521 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
522 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol);
523 State = oldstate;
524 curwin->w_cursor.col = oldcol;
525 #ifdef FEAT_RIGHTLEFT
526 p_ri = old_p_ri;
527 #endif
529 #endif
531 #ifdef FEAT_VISUALEXTRA
533 * Insert string "s" (b_insert ? before : after) block :AKelly
534 * Caller must prepare for undo.
536 static void
537 block_insert(oap, s, b_insert, bdp)
538 oparg_T *oap;
539 char_u *s;
540 int b_insert;
541 struct block_def *bdp;
543 int p_ts;
544 int count = 0; /* extra spaces to replace a cut TAB */
545 int spaces = 0; /* non-zero if cutting a TAB */
546 colnr_T offset; /* pointer along new line */
547 unsigned s_len; /* STRLEN(s) */
548 char_u *newp, *oldp; /* new, old lines */
549 linenr_T lnum; /* loop var */
550 int oldstate = State;
552 State = INSERT; /* don't want REPLACE for State */
553 s_len = (unsigned)STRLEN(s);
555 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
557 block_prep(oap, bdp, lnum, TRUE);
558 if (bdp->is_short && b_insert)
559 continue; /* OP_INSERT, line ends before block start */
561 oldp = ml_get(lnum);
563 if (b_insert)
565 p_ts = bdp->start_char_vcols;
566 spaces = bdp->startspaces;
567 if (spaces != 0)
568 count = p_ts - 1; /* we're cutting a TAB */
569 offset = bdp->textcol;
571 else /* append */
573 p_ts = bdp->end_char_vcols;
574 if (!bdp->is_short) /* spaces = padding after block */
576 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0);
577 if (spaces != 0)
578 count = p_ts - 1; /* we're cutting a TAB */
579 offset = bdp->textcol + bdp->textlen - (spaces != 0);
581 else /* spaces = padding to block edge */
583 /* if $ used, just append to EOL (ie spaces==0) */
584 if (!bdp->is_MAX)
585 spaces = (oap->end_vcol - bdp->end_vcol) + 1;
586 count = spaces;
587 offset = bdp->textcol + bdp->textlen;
591 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
592 if (newp == NULL)
593 continue;
595 /* copy up to shifted part */
596 mch_memmove(newp, oldp, (size_t)(offset));
597 oldp += offset;
599 /* insert pre-padding */
600 copy_spaces(newp + offset, (size_t)spaces);
602 /* copy the new text */
603 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
604 offset += s_len;
606 if (spaces && !bdp->is_short)
608 /* insert post-padding */
609 copy_spaces(newp + offset + spaces, (size_t)(p_ts - spaces));
610 /* We're splitting a TAB, don't copy it. */
611 oldp++;
612 /* We allowed for that TAB, remember this now */
613 count++;
616 if (spaces > 0)
617 offset += count;
618 mch_memmove(newp + offset, oldp, (size_t)(STRLEN(oldp) + 1));
620 ml_replace(lnum, newp, FALSE);
622 if (lnum == oap->end.lnum)
624 /* Set "']" mark to the end of the block instead of the end of
625 * the insert in the first line. */
626 curbuf->b_op_end.lnum = oap->end.lnum;
627 curbuf->b_op_end.col = offset;
629 } /* for all lnum */
631 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
633 State = oldstate;
635 #endif
637 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
639 * op_reindent - handle reindenting a block of lines.
641 void
642 op_reindent(oap, how)
643 oparg_T *oap;
644 int (*how) __ARGS((void));
646 long i;
647 char_u *l;
648 int count;
649 linenr_T first_changed = 0;
650 linenr_T last_changed = 0;
651 linenr_T start_lnum = curwin->w_cursor.lnum;
653 /* Don't even try when 'modifiable' is off. */
654 if (!curbuf->b_p_ma)
656 EMSG(_(e_modifiable));
657 return;
660 for (i = oap->line_count; --i >= 0 && !got_int; )
662 /* it's a slow thing to do, so give feedback so there's no worry that
663 * the computer's just hung. */
665 if (i > 1
666 && (i % 50 == 0 || i == oap->line_count - 1)
667 && oap->line_count > p_report)
668 smsg((char_u *)_("%ld lines to indent... "), i);
671 * Be vi-compatible: For lisp indenting the first line is not
672 * indented, unless there is only one line.
674 #ifdef FEAT_LISP
675 if (i != oap->line_count - 1 || oap->line_count == 1
676 || how != get_lisp_indent)
677 #endif
679 l = skipwhite(ml_get_curline());
680 if (*l == NUL) /* empty or blank line */
681 count = 0;
682 else
683 count = how(); /* get the indent for this line */
685 if (set_indent(count, SIN_UNDO))
687 /* did change the indent, call changed_lines() later */
688 if (first_changed == 0)
689 first_changed = curwin->w_cursor.lnum;
690 last_changed = curwin->w_cursor.lnum;
693 ++curwin->w_cursor.lnum;
696 /* put cursor on first non-blank of indented line */
697 curwin->w_cursor.lnum = start_lnum;
698 beginline(BL_SOL | BL_FIX);
700 /* Mark changed lines so that they will be redrawn. When Visual
701 * highlighting was present, need to continue until the last line. When
702 * there is no change still need to remove the Visual highlighting. */
703 if (last_changed != 0)
704 changed_lines(first_changed, 0,
705 #ifdef FEAT_VISUAL
706 oap->is_VIsual ? start_lnum + oap->line_count :
707 #endif
708 last_changed + 1, 0L);
709 #ifdef FEAT_VISUAL
710 else if (oap->is_VIsual)
711 redraw_curbuf_later(INVERTED);
712 #endif
714 if (oap->line_count > p_report)
716 i = oap->line_count - (i + 1);
717 if (i == 1)
718 MSG(_("1 line indented "));
719 else
720 smsg((char_u *)_("%ld lines indented "), i);
722 /* set '[ and '] marks */
723 curbuf->b_op_start = oap->start;
724 curbuf->b_op_end = oap->end;
726 #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
728 #if defined(FEAT_EVAL) || defined(PROTO)
730 * Keep the last expression line here, for repeating.
732 static char_u *expr_line = NULL;
735 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
736 * Returns '=' when OK, NUL otherwise.
739 get_expr_register()
741 char_u *new_line;
743 new_line = getcmdline('=', 0L, 0);
744 if (new_line == NULL)
745 return NUL;
746 if (*new_line == NUL) /* use previous line */
747 vim_free(new_line);
748 else
749 set_expr_line(new_line);
750 return '=';
754 * Set the expression for the '=' register.
755 * Argument must be an allocated string.
757 void
758 set_expr_line(new_line)
759 char_u *new_line;
761 vim_free(expr_line);
762 expr_line = new_line;
766 * Get the result of the '=' register expression.
767 * Returns a pointer to allocated memory, or NULL for failure.
769 char_u *
770 get_expr_line()
772 char_u *expr_copy;
773 char_u *rv;
774 static int nested = 0;
776 if (expr_line == NULL)
777 return NULL;
779 /* Make a copy of the expression, because evaluating it may cause it to be
780 * changed. */
781 expr_copy = vim_strsave(expr_line);
782 if (expr_copy == NULL)
783 return NULL;
785 /* When we are invoked recursively limit the evaluation to 10 levels.
786 * Then return the string as-is. */
787 if (nested >= 10)
788 return expr_copy;
790 ++nested;
791 rv = eval_to_string(expr_copy, NULL, TRUE);
792 --nested;
793 vim_free(expr_copy);
794 return rv;
798 * Get the '=' register expression itself, without evaluating it.
800 char_u *
801 get_expr_line_src()
803 if (expr_line == NULL)
804 return NULL;
805 return vim_strsave(expr_line);
807 #endif /* FEAT_EVAL */
810 * Check if 'regname' is a valid name of a yank register.
811 * Note: There is no check for 0 (default register), caller should do this
814 valid_yank_reg(regname, writing)
815 int regname;
816 int writing; /* if TRUE check for writable registers */
818 if ( (regname > 0 && ASCII_ISALNUM(regname))
819 || (!writing && vim_strchr((char_u *)
820 #ifdef FEAT_EVAL
821 "/.%#:="
822 #else
823 "/.%#:"
824 #endif
825 , regname) != NULL)
826 || regname == '"'
827 || regname == '-'
828 || regname == '_'
829 #ifdef FEAT_CLIPBOARD
830 || regname == '*'
831 || regname == '+'
832 #endif
833 #ifdef FEAT_DND
834 || (!writing && regname == '~')
835 #endif
837 return TRUE;
838 return FALSE;
842 * Set y_current and y_append, according to the value of "regname".
843 * Cannot handle the '_' register.
844 * Must only be called with a valid register name!
846 * If regname is 0 and writing, use register 0
847 * If regname is 0 and reading, use previous register
849 void
850 get_yank_register(regname, writing)
851 int regname;
852 int writing;
854 int i;
856 y_append = FALSE;
857 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
859 y_current = y_previous;
860 return;
862 i = regname;
863 if (VIM_ISDIGIT(i))
864 i -= '0';
865 else if (ASCII_ISLOWER(i))
866 i = CharOrdLow(i) + 10;
867 else if (ASCII_ISUPPER(i))
869 i = CharOrdUp(i) + 10;
870 y_append = TRUE;
872 else if (regname == '-')
873 i = DELETION_REGISTER;
874 #ifdef FEAT_CLIPBOARD
875 /* When selection is not available, use register 0 instead of '*' */
876 else if (clip_star.available && regname == '*')
877 i = STAR_REGISTER;
878 /* When clipboard is not available, use register 0 instead of '+' */
879 else if (clip_plus.available && regname == '+')
880 i = PLUS_REGISTER;
881 #endif
882 #ifdef FEAT_DND
883 else if (!writing && regname == '~')
884 i = TILDE_REGISTER;
885 #endif
886 else /* not 0-9, a-z, A-Z or '-': use register 0 */
887 i = 0;
888 y_current = &(y_regs[i]);
889 if (writing) /* remember the register we write into for do_put() */
890 y_previous = y_current;
893 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
895 * When "regname" is a clipboard register, obtain the selection. If it's not
896 * available return zero, otherwise return "regname".
899 may_get_selection(regname)
900 int regname;
902 if (regname == '*')
904 if (!clip_star.available)
905 regname = 0;
906 else
907 clip_get_selection(&clip_star);
909 else if (regname == '+')
911 if (!clip_plus.available)
912 regname = 0;
913 else
914 clip_get_selection(&clip_plus);
916 return regname;
918 #endif
920 #if defined(FEAT_VISUAL) || defined(PROTO)
922 * Obtain the contents of a "normal" register. The register is made empty.
923 * The returned pointer has allocated memory, use put_register() later.
925 void *
926 get_register(name, copy)
927 int name;
928 int copy; /* make a copy, if FALSE make register empty. */
930 static struct yankreg *reg;
931 int i;
933 #ifdef FEAT_CLIPBOARD
934 /* When Visual area changed, may have to update selection. Obtain the
935 * selection too. */
936 if (name == '*' && clip_star.available)
938 if (clip_isautosel())
939 clip_update_selection();
940 may_get_selection(name);
942 #endif
944 get_yank_register(name, 0);
945 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
946 if (reg != NULL)
948 *reg = *y_current;
949 if (copy)
951 /* If we run out of memory some or all of the lines are empty. */
952 if (reg->y_size == 0)
953 reg->y_array = NULL;
954 else
955 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
956 * reg->y_size));
957 if (reg->y_array != NULL)
959 for (i = 0; i < reg->y_size; ++i)
960 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
963 else
964 y_current->y_array = NULL;
966 return (void *)reg;
970 * Put "reg" into register "name". Free any previous contents.
972 void
973 put_register(name, reg)
974 int name;
975 void *reg;
977 get_yank_register(name, 0);
978 free_yank_all();
979 *y_current = *(struct yankreg *)reg;
981 # ifdef FEAT_CLIPBOARD
982 /* Send text written to clipboard register to the clipboard. */
983 may_set_selection();
984 # endif
986 #endif
988 #if defined(FEAT_MOUSE) || defined(PROTO)
990 * return TRUE if the current yank register has type MLINE
993 yank_register_mline(regname)
994 int regname;
996 if (regname != 0 && !valid_yank_reg(regname, FALSE))
997 return FALSE;
998 if (regname == '_') /* black hole is always empty */
999 return FALSE;
1000 get_yank_register(regname, FALSE);
1001 return (y_current->y_type == MLINE);
1003 #endif
1006 * Start or stop recording into a yank register.
1008 * Return FAIL for failure, OK otherwise.
1011 do_record(c)
1012 int c;
1014 char_u *p;
1015 static int regname;
1016 struct yankreg *old_y_previous, *old_y_current;
1017 int retval;
1019 if (Recording == FALSE) /* start recording */
1021 /* registers 0-9, a-z and " are allowed */
1022 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1023 retval = FAIL;
1024 else
1026 Recording = TRUE;
1027 showmode();
1028 regname = c;
1029 retval = OK;
1032 else /* stop recording */
1035 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1036 * needs to be removed again to put it in a register. exec_reg then
1037 * adds the escaping back later.
1039 Recording = FALSE;
1040 MSG("");
1041 p = get_recorded();
1042 if (p == NULL)
1043 retval = FAIL;
1044 else
1046 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1047 vim_unescape_csi(p);
1050 * We don't want to change the default register here, so save and
1051 * restore the current register name.
1053 old_y_previous = y_previous;
1054 old_y_current = y_current;
1056 retval = stuff_yank(regname, p);
1058 y_previous = old_y_previous;
1059 y_current = old_y_current;
1062 return retval;
1066 * Stuff string "p" into yank register "regname" as a single line (append if
1067 * uppercase). "p" must have been alloced.
1069 * return FAIL for failure, OK otherwise
1071 static int
1072 stuff_yank(regname, p)
1073 int regname;
1074 char_u *p;
1076 char_u *lp;
1077 char_u **pp;
1079 /* check for read-only register */
1080 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1082 vim_free(p);
1083 return FAIL;
1085 if (regname == '_') /* black hole: don't do anything */
1087 vim_free(p);
1088 return OK;
1090 get_yank_register(regname, TRUE);
1091 if (y_append && y_current->y_array != NULL)
1093 pp = &(y_current->y_array[y_current->y_size - 1]);
1094 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1095 if (lp == NULL)
1097 vim_free(p);
1098 return FAIL;
1100 STRCPY(lp, *pp);
1101 STRCAT(lp, p);
1102 vim_free(p);
1103 vim_free(*pp);
1104 *pp = lp;
1106 else
1108 free_yank_all();
1109 if ((y_current->y_array =
1110 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1112 vim_free(p);
1113 return FAIL;
1115 y_current->y_array[0] = p;
1116 y_current->y_size = 1;
1117 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1119 return OK;
1123 * execute a yank register: copy it into the stuff buffer
1125 * return FAIL for failure, OK otherwise
1128 do_execreg(regname, colon, addcr, silent)
1129 int regname;
1130 int colon; /* insert ':' before each line */
1131 int addcr; /* always add '\n' to end of line */
1132 int silent; /* set "silent" flag in typeahead buffer */
1134 static int lastc = NUL;
1135 long i;
1136 char_u *p;
1137 int retval = OK;
1138 int remap;
1140 if (regname == '@') /* repeat previous one */
1142 if (lastc == NUL)
1144 EMSG(_("E748: No previously used register"));
1145 return FAIL;
1147 regname = lastc;
1149 /* check for valid regname */
1150 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
1152 emsg_invreg(regname);
1153 return FAIL;
1155 lastc = regname;
1157 #ifdef FEAT_CLIPBOARD
1158 regname = may_get_selection(regname);
1159 #endif
1161 if (regname == '_') /* black hole: don't stuff anything */
1162 return OK;
1164 #ifdef FEAT_CMDHIST
1165 if (regname == ':') /* use last command line */
1167 if (last_cmdline == NULL)
1169 EMSG(_(e_nolastcmd));
1170 return FAIL;
1172 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1173 new_last_cmdline = NULL;
1174 /* Escape all control characters with a CTRL-V */
1175 p = vim_strsave_escaped_ext(last_cmdline,
1176 (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);
1177 if (p != NULL)
1179 /* When in Visual mode "'<,'>" will be prepended to the command.
1180 * Remove it when it's already there. */
1181 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
1182 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
1183 else
1184 retval = put_in_typebuf(p, TRUE, TRUE, silent);
1186 vim_free(p);
1188 #endif
1189 #ifdef FEAT_EVAL
1190 else if (regname == '=')
1192 p = get_expr_line();
1193 if (p == NULL)
1194 return FAIL;
1195 retval = put_in_typebuf(p, TRUE, colon, silent);
1196 vim_free(p);
1198 #endif
1199 else if (regname == '.') /* use last inserted text */
1201 p = get_last_insert_save();
1202 if (p == NULL)
1204 EMSG(_(e_noinstext));
1205 return FAIL;
1207 retval = put_in_typebuf(p, FALSE, colon, silent);
1208 vim_free(p);
1210 else
1212 get_yank_register(regname, FALSE);
1213 if (y_current->y_array == NULL)
1214 return FAIL;
1216 /* Disallow remaping for ":@r". */
1217 remap = colon ? REMAP_NONE : REMAP_YES;
1220 * Insert lines into typeahead buffer, from last one to first one.
1222 put_reedit_in_typebuf(silent);
1223 for (i = y_current->y_size; --i >= 0; )
1225 char_u *escaped;
1227 /* insert NL between lines and after last line if type is MLINE */
1228 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1229 || addcr)
1231 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
1232 return FAIL;
1234 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1235 if (escaped == NULL)
1236 return FAIL;
1237 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1238 vim_free(escaped);
1239 if (retval == FAIL)
1240 return FAIL;
1241 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
1242 == FAIL)
1243 return FAIL;
1245 Exec_reg = TRUE; /* disable the 'q' command */
1247 return retval;
1251 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1252 * used only after other typeahead has been processed.
1254 static void
1255 put_reedit_in_typebuf(silent)
1256 int silent;
1258 char_u buf[3];
1260 if (restart_edit != NUL)
1262 if (restart_edit == 'V')
1264 buf[0] = 'g';
1265 buf[1] = 'R';
1266 buf[2] = NUL;
1268 else
1270 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1271 buf[1] = NUL;
1273 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
1274 restart_edit = NUL;
1278 static int
1279 put_in_typebuf(s, esc, colon, silent)
1280 char_u *s;
1281 int esc; /* Escape CSI characters */
1282 int colon; /* add ':' before the line */
1283 int silent;
1285 int retval = OK;
1287 put_reedit_in_typebuf(silent);
1288 if (colon)
1289 retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, silent);
1290 if (retval == OK)
1292 char_u *p;
1294 if (esc)
1295 p = vim_strsave_escape_csi(s);
1296 else
1297 p = s;
1298 if (p == NULL)
1299 retval = FAIL;
1300 else
1301 retval = ins_typebuf(p, REMAP_YES, 0, TRUE, silent);
1302 if (esc)
1303 vim_free(p);
1305 if (colon && retval == OK)
1306 retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, silent);
1307 return retval;
1311 * Insert a yank register: copy it into the Read buffer.
1312 * Used by CTRL-R command and middle mouse button in insert mode.
1314 * return FAIL for failure, OK otherwise
1317 insert_reg(regname, literally)
1318 int regname;
1319 int literally; /* insert literally, not as if typed */
1321 long i;
1322 int retval = OK;
1323 char_u *arg;
1324 int allocated;
1327 * It is possible to get into an endless loop by having CTRL-R a in
1328 * register a and then, in insert mode, doing CTRL-R a.
1329 * If you hit CTRL-C, the loop will be broken here.
1331 ui_breakcheck();
1332 if (got_int)
1333 return FAIL;
1335 /* check for valid regname */
1336 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1337 return FAIL;
1339 #ifdef FEAT_CLIPBOARD
1340 regname = may_get_selection(regname);
1341 #endif
1343 if (regname == '.') /* insert last inserted text */
1344 retval = stuff_inserted(NUL, 1L, TRUE);
1345 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1347 if (arg == NULL)
1348 return FAIL;
1349 stuffescaped(arg, literally);
1350 if (allocated)
1351 vim_free(arg);
1353 else /* name or number register */
1355 get_yank_register(regname, FALSE);
1356 if (y_current->y_array == NULL)
1357 retval = FAIL;
1358 else
1360 for (i = 0; i < y_current->y_size; ++i)
1362 stuffescaped(y_current->y_array[i], literally);
1364 * Insert a newline between lines and after last line if
1365 * y_type is MLINE.
1367 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1368 stuffcharReadbuff('\n');
1373 return retval;
1377 * Stuff a string into the typeahead buffer, such that edit() will insert it
1378 * literally ("literally" TRUE) or interpret is as typed characters.
1380 static void
1381 stuffescaped(arg, literally)
1382 char_u *arg;
1383 int literally;
1385 int c;
1386 char_u *start;
1388 while (*arg != NUL)
1390 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1391 * stuff K_SPECIAL to get the effect of a special key when "literally"
1392 * is TRUE. */
1393 start = arg;
1394 while ((*arg >= ' '
1395 #ifndef EBCDIC
1396 && *arg < DEL /* EBCDIC: chars above space are normal */
1397 #endif
1399 || (*arg == K_SPECIAL && !literally))
1400 ++arg;
1401 if (arg > start)
1402 stuffReadbuffLen(start, (long)(arg - start));
1404 /* stuff a single special character */
1405 if (*arg != NUL)
1407 #ifdef FEAT_MBYTE
1408 if (has_mbyte)
1409 c = mb_ptr2char_adv(&arg);
1410 else
1411 #endif
1412 c = *arg++;
1413 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1414 stuffcharReadbuff(Ctrl_V);
1415 stuffcharReadbuff(c);
1421 * If "regname" is a special register, return TRUE and store a pointer to its
1422 * value in "argp".
1425 get_spec_reg(regname, argp, allocated, errmsg)
1426 int regname;
1427 char_u **argp;
1428 int *allocated; /* return: TRUE when value was allocated */
1429 int errmsg; /* give error message when failing */
1431 int cnt;
1433 *argp = NULL;
1434 *allocated = FALSE;
1435 switch (regname)
1437 case '%': /* file name */
1438 if (errmsg)
1439 check_fname(); /* will give emsg if not set */
1440 *argp = curbuf->b_fname;
1441 return TRUE;
1443 case '#': /* alternate file name */
1444 *argp = getaltfname(errmsg); /* may give emsg if not set */
1445 return TRUE;
1447 #ifdef FEAT_EVAL
1448 case '=': /* result of expression */
1449 *argp = get_expr_line();
1450 *allocated = TRUE;
1451 return TRUE;
1452 #endif
1454 case ':': /* last command line */
1455 if (last_cmdline == NULL && errmsg)
1456 EMSG(_(e_nolastcmd));
1457 *argp = last_cmdline;
1458 return TRUE;
1460 case '/': /* last search-pattern */
1461 if (last_search_pat() == NULL && errmsg)
1462 EMSG(_(e_noprevre));
1463 *argp = last_search_pat();
1464 return TRUE;
1466 case '.': /* last inserted text */
1467 *argp = get_last_insert_save();
1468 *allocated = TRUE;
1469 if (*argp == NULL && errmsg)
1470 EMSG(_(e_noinstext));
1471 return TRUE;
1473 #ifdef FEAT_SEARCHPATH
1474 case Ctrl_F: /* Filename under cursor */
1475 case Ctrl_P: /* Path under cursor, expand via "path" */
1476 if (!errmsg)
1477 return FALSE;
1478 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
1479 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
1480 *allocated = TRUE;
1481 return TRUE;
1482 #endif
1484 case Ctrl_W: /* word under cursor */
1485 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1486 if (!errmsg)
1487 return FALSE;
1488 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1489 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1490 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1491 *allocated = TRUE;
1492 return TRUE;
1494 case '_': /* black hole: always empty */
1495 *argp = (char_u *)"";
1496 return TRUE;
1499 return FALSE;
1503 * Paste a yank register into the command line.
1504 * Only for non-special registers.
1505 * Used by CTRL-R command in command-line mode
1506 * insert_reg() can't be used here, because special characters from the
1507 * register contents will be interpreted as commands.
1509 * return FAIL for failure, OK otherwise
1512 cmdline_paste_reg(regname, literally, remcr)
1513 int regname;
1514 int literally; /* Insert text literally instead of "as typed" */
1515 int remcr; /* don't add trailing CR */
1517 long i;
1519 get_yank_register(regname, FALSE);
1520 if (y_current->y_array == NULL)
1521 return FAIL;
1523 for (i = 0; i < y_current->y_size; ++i)
1525 cmdline_paste_str(y_current->y_array[i], literally);
1527 /* Insert ^M between lines and after last line if type is MLINE.
1528 * Don't do this when "remcr" is TRUE and the next line is empty. */
1529 if (y_current->y_type == MLINE
1530 || (i < y_current->y_size - 1
1531 && !(remcr
1532 && i == y_current->y_size - 2
1533 && *y_current->y_array[i + 1] == NUL)))
1534 cmdline_paste_str((char_u *)"\r", literally);
1536 /* Check for CTRL-C, in case someone tries to paste a few thousand
1537 * lines and gets bored. */
1538 ui_breakcheck();
1539 if (got_int)
1540 return FAIL;
1542 return OK;
1545 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
1547 * Adjust the register name pointed to with "rp" for the clipboard being
1548 * used always and the clipboard being available.
1550 void
1551 adjust_clip_reg(rp)
1552 int *rp;
1554 /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1555 if (*rp == 0 && clip_unnamed)
1556 *rp = '*';
1557 if (!clip_star.available && *rp == '*')
1558 *rp = 0;
1559 if (!clip_plus.available && *rp == '+')
1560 *rp = 0;
1562 #endif
1565 * op_delete - handle a delete operation
1567 * return FAIL if undo failed, OK otherwise.
1570 op_delete(oap)
1571 oparg_T *oap;
1573 int n;
1574 linenr_T lnum;
1575 char_u *ptr;
1576 #ifdef FEAT_VISUAL
1577 char_u *newp, *oldp;
1578 struct block_def bd;
1579 #endif
1580 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1581 int did_yank = FALSE;
1583 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1584 return OK;
1586 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1587 if (oap->empty)
1588 return u_save_cursor();
1590 if (!curbuf->b_p_ma)
1592 EMSG(_(e_modifiable));
1593 return FAIL;
1596 #ifdef FEAT_CLIPBOARD
1597 adjust_clip_reg(&oap->regname);
1598 #endif
1600 #ifdef FEAT_MBYTE
1601 if (has_mbyte)
1602 mb_adjust_opend(oap);
1603 #endif
1606 * Imitate the strange Vi behaviour: If the delete spans more than one line
1607 * and motion_type == MCHAR and the result is a blank line, make the delete
1608 * linewise. Don't do this for the change command or Visual mode.
1610 if ( oap->motion_type == MCHAR
1611 #ifdef FEAT_VISUAL
1612 && !oap->is_VIsual
1613 && !oap->block_mode
1614 #endif
1615 && oap->line_count > 1
1616 && oap->op_type == OP_DELETE)
1618 ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive;
1619 ptr = skipwhite(ptr);
1620 if (*ptr == NUL && inindent(0))
1621 oap->motion_type = MLINE;
1625 * Check for trying to delete (e.g. "D") in an empty line.
1626 * Note: For the change operator it is ok.
1628 if ( oap->motion_type == MCHAR
1629 && oap->line_count == 1
1630 && oap->op_type == OP_DELETE
1631 && *ml_get(oap->start.lnum) == NUL)
1634 * It's an error to operate on an empty region, when 'E' included in
1635 * 'cpoptions' (Vi compatible).
1637 #ifdef FEAT_VIRTUALEDIT
1638 if (virtual_op)
1639 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1640 * marks as if it happened. */
1641 goto setmarks;
1642 #endif
1643 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1644 beep_flush();
1645 return OK;
1649 * Do a yank of whatever we're about to delete.
1650 * If a yank register was specified, put the deleted text into that register.
1651 * For the black hole register '_' don't yank anything.
1653 if (oap->regname != '_')
1655 if (oap->regname != 0)
1657 /* check for read-only register */
1658 if (!valid_yank_reg(oap->regname, TRUE))
1660 beep_flush();
1661 return OK;
1663 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1664 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1665 did_yank = TRUE;
1669 * Put deleted text into register 1 and shift number registers if the
1670 * delete contains a line break, or when a regname has been specified.
1672 if (oap->regname != 0 || oap->motion_type == MLINE
1673 || oap->line_count > 1 || oap->use_reg_one)
1675 y_current = &y_regs[9];
1676 free_yank_all(); /* free register nine */
1677 for (n = 9; n > 1; --n)
1678 y_regs[n] = y_regs[n - 1];
1679 y_previous = y_current = &y_regs[1];
1680 y_regs[1].y_array = NULL; /* set register one to empty */
1681 if (op_yank(oap, TRUE, FALSE) == OK)
1682 did_yank = TRUE;
1685 /* Yank into small delete register when no register specified and the
1686 * delete is within one line. */
1687 if (oap->regname == 0 && oap->motion_type != MLINE
1688 && oap->line_count == 1)
1690 oap->regname = '-';
1691 get_yank_register(oap->regname, TRUE);
1692 if (op_yank(oap, TRUE, FALSE) == OK)
1693 did_yank = TRUE;
1694 oap->regname = 0;
1698 * If there's too much stuff to fit in the yank register, then get a
1699 * confirmation before doing the delete. This is crude, but simple.
1700 * And it avoids doing a delete of something we can't put back if we
1701 * want.
1703 if (!did_yank)
1705 int msg_silent_save = msg_silent;
1707 msg_silent = 0; /* must display the prompt */
1708 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1709 msg_silent = msg_silent_save;
1710 if (n != 'y')
1712 EMSG(_(e_abort));
1713 return FAIL;
1718 #ifdef FEAT_VISUAL
1720 * block mode delete
1722 if (oap->block_mode)
1724 if (u_save((linenr_T)(oap->start.lnum - 1),
1725 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1726 return FAIL;
1728 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1730 block_prep(oap, &bd, lnum, TRUE);
1731 if (bd.textlen == 0) /* nothing to delete */
1732 continue;
1734 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1735 if (lnum == curwin->w_cursor.lnum)
1737 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1738 # ifdef FEAT_VIRTUALEDIT
1739 curwin->w_cursor.coladd = 0;
1740 # endif
1743 /* n == number of chars deleted
1744 * If we delete a TAB, it may be replaced by several characters.
1745 * Thus the number of characters may increase!
1747 n = bd.textlen - bd.startspaces - bd.endspaces;
1748 oldp = ml_get(lnum);
1749 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1750 if (newp == NULL)
1751 continue;
1752 /* copy up to deleted part */
1753 mch_memmove(newp, oldp, (size_t)bd.textcol);
1754 /* insert spaces */
1755 copy_spaces(newp + bd.textcol,
1756 (size_t)(bd.startspaces + bd.endspaces));
1757 /* copy the part after the deleted part */
1758 oldp += bd.textcol + bd.textlen;
1759 mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
1760 oldp, STRLEN(oldp) + 1);
1761 /* replace the line */
1762 ml_replace(lnum, newp, FALSE);
1765 check_cursor_col();
1766 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1767 oap->end.lnum + 1, 0L);
1768 oap->line_count = 0; /* no lines deleted */
1770 else
1771 #endif
1772 if (oap->motion_type == MLINE)
1774 if (oap->op_type == OP_CHANGE)
1776 /* Delete the lines except the first one. Temporarily move the
1777 * cursor to the next line. Save the current line number, if the
1778 * last line is deleted it may be changed.
1780 if (oap->line_count > 1)
1782 lnum = curwin->w_cursor.lnum;
1783 ++curwin->w_cursor.lnum;
1784 del_lines((long)(oap->line_count - 1), TRUE);
1785 curwin->w_cursor.lnum = lnum;
1787 if (u_save_cursor() == FAIL)
1788 return FAIL;
1789 if (curbuf->b_p_ai) /* don't delete indent */
1791 beginline(BL_WHITE); /* cursor on first non-white */
1792 did_ai = TRUE; /* delete the indent when ESC hit */
1793 ai_col = curwin->w_cursor.col;
1795 else
1796 beginline(0); /* cursor in column 0 */
1797 truncate_line(FALSE); /* delete the rest of the line */
1798 /* leave cursor past last char in line */
1799 if (oap->line_count > 1)
1800 u_clearline(); /* "U" command not possible after "2cc" */
1802 else
1804 del_lines(oap->line_count, TRUE);
1805 beginline(BL_WHITE | BL_FIX);
1806 u_clearline(); /* "U" command not possible after "dd" */
1809 else
1811 #ifdef FEAT_VIRTUALEDIT
1812 if (virtual_op)
1814 int endcol = 0;
1816 /* For virtualedit: break the tabs that are partly included. */
1817 if (gchar_pos(&oap->start) == '\t')
1819 if (u_save_cursor() == FAIL) /* save first line for undo */
1820 return FAIL;
1821 if (oap->line_count == 1)
1822 endcol = getviscol2(oap->end.col, oap->end.coladd);
1823 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1824 oap->start = curwin->w_cursor;
1825 if (oap->line_count == 1)
1827 coladvance(endcol);
1828 oap->end.col = curwin->w_cursor.col;
1829 oap->end.coladd = curwin->w_cursor.coladd;
1830 curwin->w_cursor = oap->start;
1834 /* Break a tab only when it's included in the area. */
1835 if (gchar_pos(&oap->end) == '\t'
1836 && (int)oap->end.coladd < oap->inclusive)
1838 /* save last line for undo */
1839 if (u_save((linenr_T)(oap->end.lnum - 1),
1840 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1841 return FAIL;
1842 curwin->w_cursor = oap->end;
1843 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1844 oap->end = curwin->w_cursor;
1845 curwin->w_cursor = oap->start;
1848 #endif
1850 if (oap->line_count == 1) /* delete characters within one line */
1852 if (u_save_cursor() == FAIL) /* save line for undo */
1853 return FAIL;
1855 /* if 'cpoptions' contains '$', display '$' at end of change */
1856 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1857 && oap->op_type == OP_CHANGE
1858 && oap->end.lnum == curwin->w_cursor.lnum
1859 #ifdef FEAT_VISUAL
1860 && !oap->is_VIsual
1861 #endif
1863 display_dollar(oap->end.col - !oap->inclusive);
1865 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1867 #ifdef FEAT_VIRTUALEDIT
1868 if (virtual_op)
1870 /* fix up things for virtualedit-delete:
1871 * break the tabs which are going to get in our way
1873 char_u *curline = ml_get_curline();
1874 int len = (int)STRLEN(curline);
1876 if (oap->end.coladd != 0
1877 && (int)oap->end.col >= len - 1
1878 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1879 n++;
1880 /* Delete at least one char (e.g, when on a control char). */
1881 if (n == 0 && oap->start.coladd != oap->end.coladd)
1882 n = 1;
1884 /* When deleted a char in the line, reset coladd. */
1885 if (gchar_cursor() != NUL)
1886 curwin->w_cursor.coladd = 0;
1888 #endif
1889 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
1890 #ifdef FEAT_VISUAL
1891 && !oap->is_VIsual
1892 #endif
1895 else /* delete characters between lines */
1897 pos_T curpos;
1899 /* save deleted and changed lines for undo */
1900 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1901 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1902 return FAIL;
1904 truncate_line(TRUE); /* delete from cursor to end of line */
1906 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1907 ++curwin->w_cursor.lnum;
1908 del_lines((long)(oap->line_count - 2), FALSE);
1910 /* delete from start of line until op_end */
1911 curwin->w_cursor.col = 0;
1912 (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
1913 !virtual_op, oap->op_type == OP_DELETE
1914 #ifdef FEAT_VISUAL
1915 && !oap->is_VIsual
1916 #endif
1918 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1920 (void)do_join(FALSE);
1924 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1926 #ifdef FEAT_VIRTUALEDIT
1927 setmarks:
1928 #endif
1929 #ifdef FEAT_VISUAL
1930 if (oap->block_mode)
1932 curbuf->b_op_end.lnum = oap->end.lnum;
1933 curbuf->b_op_end.col = oap->start.col;
1935 else
1936 #endif
1937 curbuf->b_op_end = oap->start;
1938 curbuf->b_op_start = oap->start;
1940 return OK;
1943 #ifdef FEAT_MBYTE
1945 * Adjust end of operating area for ending on a multi-byte character.
1946 * Used for deletion.
1948 static void
1949 mb_adjust_opend(oap)
1950 oparg_T *oap;
1952 char_u *p;
1954 if (oap->inclusive)
1956 p = ml_get(oap->end.lnum);
1957 oap->end.col += mb_tail_off(p, p + oap->end.col);
1960 #endif
1962 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1964 * Replace a whole area with one character.
1967 op_replace(oap, c)
1968 oparg_T *oap;
1969 int c;
1971 int n, numc;
1972 #ifdef FEAT_MBYTE
1973 int num_chars;
1974 #endif
1975 char_u *newp, *oldp;
1976 size_t oldlen;
1977 struct block_def bd;
1979 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1980 return OK; /* nothing to do */
1982 #ifdef FEAT_MBYTE
1983 if (has_mbyte)
1984 mb_adjust_opend(oap);
1985 #endif
1987 if (u_save((linenr_T)(oap->start.lnum - 1),
1988 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1989 return FAIL;
1992 * block mode replace
1994 if (oap->block_mode)
1996 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1997 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1999 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2000 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2001 continue; /* nothing to replace */
2003 /* n == number of extra chars required
2004 * If we split a TAB, it may be replaced by several characters.
2005 * Thus the number of characters may increase!
2007 #ifdef FEAT_VIRTUALEDIT
2008 /* If the range starts in virtual space, count the initial
2009 * coladd offset as part of "startspaces" */
2010 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2012 pos_T vpos;
2014 getvpos(&vpos, oap->start_vcol);
2015 bd.startspaces += vpos.coladd;
2016 n = bd.startspaces;
2018 else
2019 #endif
2020 /* allow for pre spaces */
2021 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2023 /* allow for post spp */
2024 n += (bd.endspaces
2025 #ifdef FEAT_VIRTUALEDIT
2026 && !bd.is_oneChar
2027 #endif
2028 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2029 /* Figure out how many characters to replace. */
2030 numc = oap->end_vcol - oap->start_vcol + 1;
2031 if (bd.is_short && (!virtual_op || bd.is_MAX))
2032 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2034 #ifdef FEAT_MBYTE
2035 /* A double-wide character can be replaced only up to half the
2036 * times. */
2037 if ((*mb_char2cells)(c) > 1)
2039 if ((numc & 1) && !bd.is_short)
2041 ++bd.endspaces;
2042 ++n;
2044 numc = numc / 2;
2047 /* Compute bytes needed, move character count to num_chars. */
2048 num_chars = numc;
2049 numc *= (*mb_char2len)(c);
2050 #endif
2051 /* oldlen includes textlen, so don't double count */
2052 n += numc - bd.textlen;
2054 oldp = ml_get_curline();
2055 oldlen = STRLEN(oldp);
2056 newp = alloc_check((unsigned)oldlen + 1 + n);
2057 if (newp == NULL)
2058 continue;
2059 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2060 /* copy up to deleted part */
2061 mch_memmove(newp, oldp, (size_t)bd.textcol);
2062 oldp += bd.textcol + bd.textlen;
2063 /* insert pre-spaces */
2064 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2065 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2066 #ifdef FEAT_MBYTE
2067 if (has_mbyte)
2069 n = (int)STRLEN(newp);
2070 while (--num_chars >= 0)
2071 n += (*mb_char2bytes)(c, newp + n);
2073 else
2074 #endif
2075 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2076 if (!bd.is_short)
2078 /* insert post-spaces */
2079 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2080 /* copy the part after the changed part */
2081 mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1);
2083 /* replace the line */
2084 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2087 else
2090 * MCHAR and MLINE motion replace.
2092 if (oap->motion_type == MLINE)
2094 oap->start.col = 0;
2095 curwin->w_cursor.col = 0;
2096 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2097 if (oap->end.col)
2098 --oap->end.col;
2100 else if (!oap->inclusive)
2101 dec(&(oap->end));
2103 while (ltoreq(curwin->w_cursor, oap->end))
2105 n = gchar_cursor();
2106 if (n != NUL)
2108 #ifdef FEAT_MBYTE
2109 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2111 /* This is slow, but it handles replacing a single-byte
2112 * with a multi-byte and the other way around. */
2113 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2114 n = State;
2115 State = REPLACE;
2116 ins_char(c);
2117 State = n;
2118 /* Backup to the replaced character. */
2119 dec_cursor();
2121 else
2122 #endif
2124 #ifdef FEAT_VIRTUALEDIT
2125 if (n == TAB)
2127 int end_vcol = 0;
2129 if (curwin->w_cursor.lnum == oap->end.lnum)
2131 /* oap->end has to be recalculated when
2132 * the tab breaks */
2133 end_vcol = getviscol2(oap->end.col,
2134 oap->end.coladd);
2136 coladvance_force(getviscol());
2137 if (curwin->w_cursor.lnum == oap->end.lnum)
2138 getvpos(&oap->end, end_vcol);
2140 #endif
2141 pchar(curwin->w_cursor, c);
2144 #ifdef FEAT_VIRTUALEDIT
2145 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2147 int virtcols = oap->end.coladd;
2149 if (curwin->w_cursor.lnum == oap->start.lnum
2150 && oap->start.col == oap->end.col && oap->start.coladd)
2151 virtcols -= oap->start.coladd;
2153 /* oap->end has been trimmed so it's effectively inclusive;
2154 * as a result an extra +1 must be counted so we don't
2155 * trample the NUL byte. */
2156 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2157 curwin->w_cursor.col -= (virtcols + 1);
2158 for (; virtcols >= 0; virtcols--)
2160 pchar(curwin->w_cursor, c);
2161 if (inc(&curwin->w_cursor) == -1)
2162 break;
2165 #endif
2167 /* Advance to next character, stop at the end of the file. */
2168 if (inc_cursor() == -1)
2169 break;
2173 curwin->w_cursor = oap->start;
2174 check_cursor();
2175 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2177 /* Set "'[" and "']" marks. */
2178 curbuf->b_op_start = oap->start;
2179 curbuf->b_op_end = oap->end;
2181 return OK;
2183 #endif
2186 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2188 void
2189 op_tilde(oap)
2190 oparg_T *oap;
2192 pos_T pos;
2193 #ifdef FEAT_VISUAL
2194 struct block_def bd;
2195 int todo;
2196 #endif
2197 int did_change = 0;
2199 if (u_save((linenr_T)(oap->start.lnum - 1),
2200 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2201 return;
2203 pos = oap->start;
2204 #ifdef FEAT_VISUAL
2205 if (oap->block_mode) /* Visual block mode */
2207 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2209 block_prep(oap, &bd, pos.lnum, FALSE);
2210 pos.col = bd.textcol;
2211 for (todo = bd.textlen; todo > 0; --todo)
2213 # ifdef FEAT_MBYTE
2214 if (has_mbyte)
2215 todo -= (*mb_ptr2len)(ml_get_pos(&pos)) - 1;
2216 # endif
2217 did_change |= swapchar(oap->op_type, &pos);
2218 if (inc(&pos) == -1) /* at end of file */
2219 break;
2221 # ifdef FEAT_NETBEANS_INTG
2222 if (usingNetbeans && did_change)
2224 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2226 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2227 (long)bd.textlen);
2228 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
2229 &ptr[bd.textcol], bd.textlen);
2231 # endif
2233 if (did_change)
2234 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2236 else /* not block mode */
2237 #endif
2239 if (oap->motion_type == MLINE)
2241 oap->start.col = 0;
2242 pos.col = 0;
2243 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2244 if (oap->end.col)
2245 --oap->end.col;
2247 else if (!oap->inclusive)
2248 dec(&(oap->end));
2250 while (ltoreq(pos, oap->end))
2252 did_change |= swapchar(oap->op_type, &pos);
2253 if (inc(&pos) == -1) /* at end of file */
2254 break;
2257 if (did_change)
2259 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2260 0L);
2261 #ifdef FEAT_NETBEANS_INTG
2262 if (usingNetbeans && did_change)
2264 char_u *ptr;
2265 int count;
2267 pos = oap->start;
2268 while (pos.lnum < oap->end.lnum)
2270 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2271 count = (int)STRLEN(ptr) - pos.col;
2272 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2273 netbeans_inserted(curbuf, pos.lnum, pos.col,
2274 &ptr[pos.col], count);
2275 pos.col = 0;
2276 pos.lnum++;
2278 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2279 count = oap->end.col - pos.col + 1;
2280 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2281 netbeans_inserted(curbuf, pos.lnum, pos.col,
2282 &ptr[pos.col], count);
2284 #endif
2288 #ifdef FEAT_VISUAL
2289 if (!did_change && oap->is_VIsual)
2290 /* No change: need to remove the Visual selection */
2291 redraw_curbuf_later(INVERTED);
2292 #endif
2295 * Set '[ and '] marks.
2297 curbuf->b_op_start = oap->start;
2298 curbuf->b_op_end = oap->end;
2300 if (oap->line_count > p_report)
2302 if (oap->line_count == 1)
2303 MSG(_("1 line changed"));
2304 else
2305 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2310 * If op_type == OP_UPPER: make uppercase,
2311 * if op_type == OP_LOWER: make lowercase,
2312 * if op_type == OP_ROT13: do rot13 encoding,
2313 * else swap case of character at 'pos'
2314 * returns TRUE when something actually changed.
2317 swapchar(op_type, pos)
2318 int op_type;
2319 pos_T *pos;
2321 int c;
2322 int nc;
2324 c = gchar_pos(pos);
2326 /* Only do rot13 encoding for ASCII characters. */
2327 if (c >= 0x80 && op_type == OP_ROT13)
2328 return FALSE;
2330 #ifdef FEAT_MBYTE
2331 if (op_type == OP_UPPER && enc_latin1like && c == 0xdf)
2333 pos_T sp = curwin->w_cursor;
2335 /* Special handling of German sharp s: change to "SS". */
2336 curwin->w_cursor = *pos;
2337 del_char(FALSE);
2338 ins_char('S');
2339 ins_char('S');
2340 curwin->w_cursor = sp;
2341 inc(pos);
2344 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2345 return FALSE;
2346 #endif
2347 nc = c;
2348 if (MB_ISLOWER(c))
2350 if (op_type == OP_ROT13)
2351 nc = ROT13(c, 'a');
2352 else if (op_type != OP_LOWER)
2353 nc = MB_TOUPPER(c);
2355 else if (MB_ISUPPER(c))
2357 if (op_type == OP_ROT13)
2358 nc = ROT13(c, 'A');
2359 else if (op_type != OP_UPPER)
2360 nc = MB_TOLOWER(c);
2362 if (nc != c)
2364 #ifdef FEAT_MBYTE
2365 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2367 pos_T sp = curwin->w_cursor;
2369 curwin->w_cursor = *pos;
2370 del_char(FALSE);
2371 ins_char(nc);
2372 curwin->w_cursor = sp;
2374 else
2375 #endif
2376 pchar(*pos, nc);
2377 return TRUE;
2379 return FALSE;
2382 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2384 * op_insert - Insert and append operators for Visual mode.
2386 void
2387 op_insert(oap, count1)
2388 oparg_T *oap;
2389 long count1;
2391 long ins_len, pre_textlen = 0;
2392 char_u *firstline, *ins_text;
2393 struct block_def bd;
2394 int i;
2396 /* edit() changes this - record it for OP_APPEND */
2397 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2399 /* vis block is still marked. Get rid of it now. */
2400 curwin->w_cursor.lnum = oap->start.lnum;
2401 update_screen(INVERTED);
2403 if (oap->block_mode)
2405 #ifdef FEAT_VIRTUALEDIT
2406 /* When 'virtualedit' is used, need to insert the extra spaces before
2407 * doing block_prep(). When only "block" is used, virtual edit is
2408 * already disabled, but still need it when calling
2409 * coladvance_force(). */
2410 if (curwin->w_cursor.coladd > 0)
2412 int old_ve_flags = ve_flags;
2414 ve_flags = VE_ALL;
2415 if (u_save_cursor() == FAIL)
2416 return;
2417 coladvance_force(oap->op_type == OP_APPEND
2418 ? oap->end_vcol + 1 : getviscol());
2419 if (oap->op_type == OP_APPEND)
2420 --curwin->w_cursor.col;
2421 ve_flags = old_ve_flags;
2423 #endif
2424 /* Get the info about the block before entering the text */
2425 block_prep(oap, &bd, oap->start.lnum, TRUE);
2426 firstline = ml_get(oap->start.lnum) + bd.textcol;
2427 if (oap->op_type == OP_APPEND)
2428 firstline += bd.textlen;
2429 pre_textlen = (long)STRLEN(firstline);
2432 if (oap->op_type == OP_APPEND)
2434 if (oap->block_mode
2435 #ifdef FEAT_VIRTUALEDIT
2436 && curwin->w_cursor.coladd == 0
2437 #endif
2440 /* Move the cursor to the character right of the block. */
2441 curwin->w_set_curswant = TRUE;
2442 while (*ml_get_cursor() != NUL
2443 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2444 ++curwin->w_cursor.col;
2445 if (bd.is_short && !bd.is_MAX)
2447 /* First line was too short, make it longer and adjust the
2448 * values in "bd". */
2449 if (u_save_cursor() == FAIL)
2450 return;
2451 for (i = 0; i < bd.endspaces; ++i)
2452 ins_char(' ');
2453 bd.textlen += bd.endspaces;
2456 else
2458 curwin->w_cursor = oap->end;
2459 check_cursor_col();
2461 /* Works just like an 'i'nsert on the next character. */
2462 if (!lineempty(curwin->w_cursor.lnum)
2463 && oap->start_vcol != oap->end_vcol)
2464 inc_cursor();
2468 edit(NUL, FALSE, (linenr_T)count1);
2470 /* if user has moved off this line, we don't know what to do, so do
2471 * nothing */
2472 if (curwin->w_cursor.lnum != oap->start.lnum)
2473 return;
2475 if (oap->block_mode)
2477 struct block_def bd2;
2480 * Spaces and tabs in the indent may have changed to other spaces and
2481 * tabs. Get the starting column again and correct the length.
2482 * Don't do this when "$" used, end-of-line will have changed.
2484 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2485 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2487 if (oap->op_type == OP_APPEND)
2489 pre_textlen += bd2.textlen - bd.textlen;
2490 if (bd2.endspaces)
2491 --bd2.textlen;
2493 bd.textcol = bd2.textcol;
2494 bd.textlen = bd2.textlen;
2498 * Subsequent calls to ml_get() flush the firstline data - take a
2499 * copy of the required string.
2501 firstline = ml_get(oap->start.lnum) + bd.textcol;
2502 if (oap->op_type == OP_APPEND)
2503 firstline += bd.textlen;
2504 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2506 ins_text = vim_strnsave(firstline, (int)ins_len);
2507 if (ins_text != NULL)
2509 /* block handled here */
2510 if (u_save(oap->start.lnum,
2511 (linenr_T)(oap->end.lnum + 1)) == OK)
2512 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2513 &bd);
2515 curwin->w_cursor.col = oap->start.col;
2516 check_cursor();
2517 vim_free(ins_text);
2522 #endif
2525 * op_change - handle a change operation
2527 * return TRUE if edit() returns because of a CTRL-O command
2530 op_change(oap)
2531 oparg_T *oap;
2533 colnr_T l;
2534 int retval;
2535 #ifdef FEAT_VISUALEXTRA
2536 long offset;
2537 linenr_T linenr;
2538 long ins_len;
2539 long pre_textlen = 0;
2540 long pre_indent = 0;
2541 char_u *firstline;
2542 char_u *ins_text, *newp, *oldp;
2543 struct block_def bd;
2544 #endif
2546 l = oap->start.col;
2547 if (oap->motion_type == MLINE)
2549 l = 0;
2550 #ifdef FEAT_SMARTINDENT
2551 if (!p_paste && curbuf->b_p_si
2552 # ifdef FEAT_CINDENT
2553 && !curbuf->b_p_cin
2554 # endif
2556 can_si = TRUE; /* It's like opening a new line, do si */
2557 #endif
2560 /* First delete the text in the region. In an empty buffer only need to
2561 * save for undo */
2562 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2564 if (u_save_cursor() == FAIL)
2565 return FALSE;
2567 else if (op_delete(oap) == FAIL)
2568 return FALSE;
2570 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2571 && !virtual_op)
2572 inc_cursor();
2574 #ifdef FEAT_VISUALEXTRA
2575 /* check for still on same line (<CR> in inserted text meaningless) */
2576 /* skip blank lines too */
2577 if (oap->block_mode)
2579 # ifdef FEAT_VIRTUALEDIT
2580 /* Add spaces before getting the current line length. */
2581 if (virtual_op && (curwin->w_cursor.coladd > 0
2582 || gchar_cursor() == NUL))
2583 coladvance_force(getviscol());
2584 # endif
2585 firstline = ml_get(oap->start.lnum);
2586 pre_textlen = (long)STRLEN(firstline);
2587 pre_indent = (long)(skipwhite(firstline) - firstline);
2588 bd.textcol = curwin->w_cursor.col;
2590 #endif
2592 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2593 if (oap->motion_type == MLINE)
2594 fix_indent();
2595 #endif
2597 retval = edit(NUL, FALSE, (linenr_T)1);
2599 #ifdef FEAT_VISUALEXTRA
2601 * In Visual block mode, handle copying the new text to all lines of the
2602 * block.
2604 if (oap->block_mode && oap->start.lnum != oap->end.lnum)
2606 /* Auto-indenting may have changed the indent. If the cursor was past
2607 * the indent, exclude that indent change from the inserted text. */
2608 firstline = ml_get(oap->start.lnum);
2609 if (bd.textcol > (colnr_T)pre_indent)
2611 long new_indent = (long)(skipwhite(firstline) - firstline);
2613 pre_textlen += new_indent - pre_indent;
2614 bd.textcol += new_indent - pre_indent;
2617 ins_len = (long)STRLEN(firstline) - pre_textlen;
2618 if (ins_len > 0)
2620 /* Subsequent calls to ml_get() flush the firstline data - take a
2621 * copy of the inserted text. */
2622 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2624 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
2625 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2626 linenr++)
2628 block_prep(oap, &bd, linenr, TRUE);
2629 if (!bd.is_short || virtual_op)
2631 # ifdef FEAT_VIRTUALEDIT
2632 pos_T vpos;
2634 /* If the block starts in virtual space, count the
2635 * initial coladd offset as part of "startspaces" */
2636 if (bd.is_short)
2638 linenr_T lnum = curwin->w_cursor.lnum;
2640 curwin->w_cursor.lnum = linenr;
2641 (void)getvpos(&vpos, oap->start_vcol);
2642 curwin->w_cursor.lnum = lnum;
2644 else
2645 vpos.coladd = 0;
2646 # endif
2647 oldp = ml_get(linenr);
2648 newp = alloc_check((unsigned)(STRLEN(oldp)
2649 # ifdef FEAT_VIRTUALEDIT
2650 + vpos.coladd
2651 # endif
2652 + ins_len + 1));
2653 if (newp == NULL)
2654 continue;
2655 /* copy up to block start */
2656 mch_memmove(newp, oldp, (size_t)bd.textcol);
2657 offset = bd.textcol;
2658 # ifdef FEAT_VIRTUALEDIT
2659 copy_spaces(newp + offset, (size_t)vpos.coladd);
2660 offset += vpos.coladd;
2661 # endif
2662 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2663 offset += ins_len;
2664 oldp += bd.textcol;
2665 mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2666 ml_replace(linenr, newp, FALSE);
2669 check_cursor();
2671 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2673 vim_free(ins_text);
2676 #endif
2678 return retval;
2682 * set all the yank registers to empty (called from main())
2684 void
2685 init_yank()
2687 int i;
2689 for (i = 0; i < NUM_REGISTERS; ++i)
2690 y_regs[i].y_array = NULL;
2693 #if defined(EXITFREE) || defined(PROTO)
2694 void
2695 clear_registers()
2697 int i;
2699 for (i = 0; i < NUM_REGISTERS; ++i)
2701 y_current = &y_regs[i];
2702 if (y_current->y_array != NULL)
2703 free_yank_all();
2706 #endif
2709 * Free "n" lines from the current yank register.
2710 * Called for normal freeing and in case of error.
2712 static void
2713 free_yank(n)
2714 long n;
2716 if (y_current->y_array != NULL)
2718 long i;
2720 for (i = n; --i >= 0; )
2722 #ifdef AMIGA /* only for very slow machines */
2723 if ((i & 1023) == 1023) /* this may take a while */
2726 * This message should never cause a hit-return message.
2727 * Overwrite this message with any next message.
2729 ++no_wait_return;
2730 smsg((char_u *)_("freeing %ld lines"), i + 1);
2731 --no_wait_return;
2732 msg_didout = FALSE;
2733 msg_col = 0;
2735 #endif
2736 vim_free(y_current->y_array[i]);
2738 vim_free(y_current->y_array);
2739 y_current->y_array = NULL;
2740 #ifdef AMIGA
2741 if (n >= 1000)
2742 MSG("");
2743 #endif
2747 static void
2748 free_yank_all()
2750 free_yank(y_current->y_size);
2754 * Yank the text between "oap->start" and "oap->end" into a yank register.
2755 * If we are to append (uppercase register), we first yank into a new yank
2756 * register and then concatenate the old and the new one (so we keep the old
2757 * one in case of out-of-memory).
2759 * return FAIL for failure, OK otherwise
2762 op_yank(oap, deleting, mess)
2763 oparg_T *oap;
2764 int deleting;
2765 int mess;
2767 long y_idx; /* index in y_array[] */
2768 struct yankreg *curr; /* copy of y_current */
2769 struct yankreg newreg; /* new yank register when appending */
2770 char_u **new_ptr;
2771 linenr_T lnum; /* current line number */
2772 long j;
2773 int yanktype = oap->motion_type;
2774 long yanklines = oap->line_count;
2775 linenr_T yankendlnum = oap->end.lnum;
2776 char_u *p;
2777 char_u *pnew;
2778 struct block_def bd;
2780 /* check for read-only register */
2781 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2783 beep_flush();
2784 return FAIL;
2786 if (oap->regname == '_') /* black hole: nothing to do */
2787 return OK;
2789 #ifdef FEAT_CLIPBOARD
2790 if (!clip_star.available && oap->regname == '*')
2791 oap->regname = 0;
2792 else if (!clip_plus.available && oap->regname == '+')
2793 oap->regname = 0;
2794 #endif
2796 if (!deleting) /* op_delete() already set y_current */
2797 get_yank_register(oap->regname, TRUE);
2799 curr = y_current;
2800 /* append to existing contents */
2801 if (y_append && y_current->y_array != NULL)
2802 y_current = &newreg;
2803 else
2804 free_yank_all(); /* free previously yanked lines */
2807 * If the cursor was in column 1 before and after the movement, and the
2808 * operator is not inclusive, the yank is always linewise.
2810 if ( oap->motion_type == MCHAR
2811 && oap->start.col == 0
2812 && !oap->inclusive
2813 #ifdef FEAT_VISUAL
2814 && (!oap->is_VIsual || *p_sel == 'o')
2815 && !oap->block_mode
2816 #endif
2817 && oap->end.col == 0
2818 && yanklines > 1)
2820 yanktype = MLINE;
2821 --yankendlnum;
2822 --yanklines;
2825 y_current->y_size = yanklines;
2826 y_current->y_type = yanktype; /* set the yank register type */
2827 #ifdef FEAT_VISUAL
2828 y_current->y_width = 0;
2829 #endif
2830 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2831 yanklines), TRUE);
2833 if (y_current->y_array == NULL)
2835 y_current = curr;
2836 return FAIL;
2839 y_idx = 0;
2840 lnum = oap->start.lnum;
2842 #ifdef FEAT_VISUAL
2843 if (oap->block_mode)
2845 /* Visual block mode */
2846 y_current->y_type = MBLOCK; /* set the yank register type */
2847 y_current->y_width = oap->end_vcol - oap->start_vcol;
2849 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2850 y_current->y_width--;
2852 #endif
2854 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2856 switch (y_current->y_type)
2858 #ifdef FEAT_VISUAL
2859 case MBLOCK:
2860 block_prep(oap, &bd, lnum, FALSE);
2861 if (yank_copy_line(&bd, y_idx) == FAIL)
2862 goto fail;
2863 break;
2864 #endif
2866 case MLINE:
2867 if ((y_current->y_array[y_idx] =
2868 vim_strsave(ml_get(lnum))) == NULL)
2869 goto fail;
2870 break;
2872 case MCHAR:
2874 colnr_T startcol = 0, endcol = MAXCOL;
2875 #ifdef FEAT_VIRTUALEDIT
2876 int is_oneChar = FALSE;
2877 colnr_T cs, ce;
2878 #endif
2879 p = ml_get(lnum);
2880 bd.startspaces = 0;
2881 bd.endspaces = 0;
2883 if (lnum == oap->start.lnum)
2885 startcol = oap->start.col;
2886 #ifdef FEAT_VIRTUALEDIT
2887 if (virtual_op)
2889 getvcol(curwin, &oap->start, &cs, NULL, &ce);
2890 if (ce != cs && oap->start.coladd > 0)
2892 /* Part of a tab selected -- but don't
2893 * double-count it. */
2894 bd.startspaces = (ce - cs + 1)
2895 - oap->start.coladd;
2896 startcol++;
2899 #endif
2902 if (lnum == oap->end.lnum)
2904 endcol = oap->end.col;
2905 #ifdef FEAT_VIRTUALEDIT
2906 if (virtual_op)
2908 getvcol(curwin, &oap->end, &cs, NULL, &ce);
2909 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2910 # ifdef FEAT_MBYTE
2911 /* Don't add space for double-wide
2912 * char; endcol will be on last byte
2913 * of multi-byte char. */
2914 && (*mb_head_off)(p, p + endcol) == 0
2915 # endif
2918 if (oap->start.lnum == oap->end.lnum
2919 && oap->start.col == oap->end.col)
2921 /* Special case: inside a single char */
2922 is_oneChar = TRUE;
2923 bd.startspaces = oap->end.coladd
2924 - oap->start.coladd + oap->inclusive;
2925 endcol = startcol;
2927 else
2929 bd.endspaces = oap->end.coladd
2930 + oap->inclusive;
2931 endcol -= oap->inclusive;
2935 #endif
2937 if (startcol > endcol
2938 #ifdef FEAT_VIRTUALEDIT
2939 || is_oneChar
2940 #endif
2942 bd.textlen = 0;
2943 else
2945 if (endcol == MAXCOL)
2946 endcol = (colnr_T)STRLEN(p);
2947 bd.textlen = endcol - startcol + oap->inclusive;
2949 bd.textstart = p + startcol;
2950 if (yank_copy_line(&bd, y_idx) == FAIL)
2951 goto fail;
2952 break;
2954 /* NOTREACHED */
2958 if (curr != y_current) /* append the new block to the old block */
2960 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2961 (curr->y_size + y_current->y_size)), TRUE);
2962 if (new_ptr == NULL)
2963 goto fail;
2964 for (j = 0; j < curr->y_size; ++j)
2965 new_ptr[j] = curr->y_array[j];
2966 vim_free(curr->y_array);
2967 curr->y_array = new_ptr;
2969 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
2970 curr->y_type = MLINE;
2972 /* Concatenate the last line of the old block with the first line of
2973 * the new block, unless being Vi compatible. */
2974 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
2976 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2977 + STRLEN(y_current->y_array[0]) + 1), TRUE);
2978 if (pnew == NULL)
2980 y_idx = y_current->y_size - 1;
2981 goto fail;
2983 STRCPY(pnew, curr->y_array[--j]);
2984 STRCAT(pnew, y_current->y_array[0]);
2985 vim_free(curr->y_array[j]);
2986 vim_free(y_current->y_array[0]);
2987 curr->y_array[j++] = pnew;
2988 y_idx = 1;
2990 else
2991 y_idx = 0;
2992 while (y_idx < y_current->y_size)
2993 curr->y_array[j++] = y_current->y_array[y_idx++];
2994 curr->y_size = j;
2995 vim_free(y_current->y_array);
2996 y_current = curr;
2998 if (mess) /* Display message about yank? */
3000 if (yanktype == MCHAR
3001 #ifdef FEAT_VISUAL
3002 && !oap->block_mode
3003 #endif
3004 && yanklines == 1)
3005 yanklines = 0;
3006 /* Some versions of Vi use ">=" here, some don't... */
3007 if (yanklines > p_report)
3009 /* redisplay now, so message is not deleted */
3010 update_topline_redraw();
3011 if (yanklines == 1)
3013 #ifdef FEAT_VISUAL
3014 if (oap->block_mode)
3015 MSG(_("block of 1 line yanked"));
3016 else
3017 #endif
3018 MSG(_("1 line yanked"));
3020 #ifdef FEAT_VISUAL
3021 else if (oap->block_mode)
3022 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3023 #endif
3024 else
3025 smsg((char_u *)_("%ld lines yanked"), yanklines);
3030 * Set "'[" and "']" marks.
3032 curbuf->b_op_start = oap->start;
3033 curbuf->b_op_end = oap->end;
3034 if (yanktype == MLINE
3035 #ifdef FEAT_VISUAL
3036 && !oap->block_mode
3037 #endif
3040 curbuf->b_op_start.col = 0;
3041 curbuf->b_op_end.col = MAXCOL;
3044 #ifdef FEAT_CLIPBOARD
3046 * If we were yanking to the '*' register, send result to clipboard.
3047 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3048 * to the '*' register.
3050 if (clip_star.available
3051 && (curr == &(y_regs[STAR_REGISTER])
3052 || (!deleting && oap->regname == 0 && clip_unnamed)))
3054 if (curr != &(y_regs[STAR_REGISTER]))
3055 /* Copy the text from register 0 to the clipboard register. */
3056 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3058 clip_own_selection(&clip_star);
3059 clip_gen_set_selection(&clip_star);
3062 # ifdef FEAT_X11
3064 * If we were yanking to the '+' register, send result to selection.
3065 * Also copy to the '*' register, in case auto-select is off.
3067 else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
3069 /* No need to copy to * register upon 'unnamed' now - see below */
3070 clip_own_selection(&clip_plus);
3071 clip_gen_set_selection(&clip_plus);
3072 if (!clip_isautosel())
3074 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3075 clip_own_selection(&clip_star);
3076 clip_gen_set_selection(&clip_star);
3079 # endif
3080 #endif
3082 return OK;
3084 fail: /* free the allocated lines */
3085 free_yank(y_idx + 1);
3086 y_current = curr;
3087 return FAIL;
3090 static int
3091 yank_copy_line(bd, y_idx)
3092 struct block_def *bd;
3093 long y_idx;
3095 char_u *pnew;
3097 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3098 == NULL)
3099 return FAIL;
3100 y_current->y_array[y_idx] = pnew;
3101 copy_spaces(pnew, (size_t)bd->startspaces);
3102 pnew += bd->startspaces;
3103 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3104 pnew += bd->textlen;
3105 copy_spaces(pnew, (size_t)bd->endspaces);
3106 pnew += bd->endspaces;
3107 *pnew = NUL;
3108 return OK;
3111 #ifdef FEAT_CLIPBOARD
3113 * Make a copy of the y_current register to register "reg".
3115 static void
3116 copy_yank_reg(reg)
3117 struct yankreg *reg;
3119 struct yankreg *curr = y_current;
3120 long j;
3122 y_current = reg;
3123 free_yank_all();
3124 *y_current = *curr;
3125 y_current->y_array = (char_u **)lalloc_clear(
3126 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3127 if (y_current->y_array == NULL)
3128 y_current->y_size = 0;
3129 else
3130 for (j = 0; j < y_current->y_size; ++j)
3131 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3133 free_yank(j);
3134 y_current->y_size = 0;
3135 break;
3137 y_current = curr;
3139 #endif
3142 * Put contents of register "regname" into the text.
3143 * Caller must check "regname" to be valid!
3144 * "flags": PUT_FIXINDENT make indent look nice
3145 * PUT_CURSEND leave cursor after end of new text
3146 * PUT_LINE force linewise put (":put")
3148 void
3149 do_put(regname, dir, count, flags)
3150 int regname;
3151 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3152 long count;
3153 int flags;
3155 char_u *ptr;
3156 char_u *newp, *oldp;
3157 int yanklen;
3158 int totlen = 0; /* init for gcc */
3159 linenr_T lnum;
3160 colnr_T col;
3161 long i; /* index in y_array[] */
3162 int y_type;
3163 long y_size;
3164 #ifdef FEAT_VISUAL
3165 int oldlen;
3166 long y_width = 0;
3167 colnr_T vcol;
3168 int delcount;
3169 int incr = 0;
3170 long j;
3171 struct block_def bd;
3172 #endif
3173 char_u **y_array = NULL;
3174 long nr_lines = 0;
3175 pos_T new_cursor;
3176 int indent;
3177 int orig_indent = 0; /* init for gcc */
3178 int indent_diff = 0; /* init for gcc */
3179 int first_indent = TRUE;
3180 int lendiff = 0;
3181 pos_T old_pos;
3182 char_u *insert_string = NULL;
3183 int allocated = FALSE;
3184 long cnt;
3186 #ifdef FEAT_CLIPBOARD
3187 /* Adjust register name for "unnamed" in 'clipboard'. */
3188 adjust_clip_reg(&regname);
3189 (void)may_get_selection(regname);
3190 #endif
3192 if (flags & PUT_FIXINDENT)
3193 orig_indent = get_indent();
3195 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3196 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3199 * Using inserted text works differently, because the register includes
3200 * special characters (newlines, etc.).
3202 if (regname == '.')
3204 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3205 (count == -1 ? 'O' : 'i')), count, FALSE);
3206 /* Putting the text is done later, so can't really move the cursor to
3207 * the next character. Use "l" to simulate it. */
3208 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3209 stuffcharReadbuff('l');
3210 return;
3214 * For special registers '%' (file name), '#' (alternate file name) and
3215 * ':' (last command line), etc. we have to create a fake yank register.
3217 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3219 if (insert_string == NULL)
3220 return;
3223 if (insert_string != NULL)
3225 y_type = MCHAR;
3226 #ifdef FEAT_EVAL
3227 if (regname == '=')
3229 /* For the = register we need to split the string at NL
3230 * characters. */
3231 /* Loop twice: count the number of lines and save them. */
3232 for (;;)
3234 y_size = 0;
3235 ptr = insert_string;
3236 while (ptr != NULL)
3238 if (y_array != NULL)
3239 y_array[y_size] = ptr;
3240 ++y_size;
3241 ptr = vim_strchr(ptr, '\n');
3242 if (ptr != NULL)
3244 if (y_array != NULL)
3245 *ptr = NUL;
3246 ++ptr;
3247 /* A trailing '\n' makes the string linewise */
3248 if (*ptr == NUL)
3250 y_type = MLINE;
3251 break;
3255 if (y_array != NULL)
3256 break;
3257 y_array = (char_u **)alloc((unsigned)
3258 (y_size * sizeof(char_u *)));
3259 if (y_array == NULL)
3260 goto end;
3263 else
3264 #endif
3266 y_size = 1; /* use fake one-line yank register */
3267 y_array = &insert_string;
3270 else
3272 get_yank_register(regname, FALSE);
3274 y_type = y_current->y_type;
3275 #ifdef FEAT_VISUAL
3276 y_width = y_current->y_width;
3277 #endif
3278 y_size = y_current->y_size;
3279 y_array = y_current->y_array;
3282 #ifdef FEAT_VISUAL
3283 if (y_type == MLINE)
3285 if (flags & PUT_LINE_SPLIT)
3287 /* "p" or "P" in Visual mode: split the lines to put the text in
3288 * between. */
3289 if (u_save_cursor() == FAIL)
3290 goto end;
3291 ptr = vim_strsave(ml_get_cursor());
3292 if (ptr == NULL)
3293 goto end;
3294 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3295 vim_free(ptr);
3297 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3298 if (ptr == NULL)
3299 goto end;
3300 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3301 ++nr_lines;
3302 dir = FORWARD;
3304 if (flags & PUT_LINE_FORWARD)
3306 /* Must be "p" for a Visual block, put lines below the block. */
3307 curwin->w_cursor = curbuf->b_visual.vi_end;
3308 dir = FORWARD;
3310 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3311 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3313 #endif
3315 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3316 y_type = MLINE;
3318 if (y_size == 0 || y_array == NULL)
3320 EMSG2(_("E353: Nothing in register %s"),
3321 regname == 0 ? (char_u *)"\"" : transchar(regname));
3322 goto end;
3325 #ifdef FEAT_VISUAL
3326 if (y_type == MBLOCK)
3328 lnum = curwin->w_cursor.lnum + y_size + 1;
3329 if (lnum > curbuf->b_ml.ml_line_count)
3330 lnum = curbuf->b_ml.ml_line_count + 1;
3331 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3332 goto end;
3334 else
3335 #endif
3336 if (y_type == MLINE)
3338 lnum = curwin->w_cursor.lnum;
3339 #ifdef FEAT_FOLDING
3340 /* Correct line number for closed fold. Don't move the cursor yet,
3341 * u_save() uses it. */
3342 if (dir == BACKWARD)
3343 (void)hasFolding(lnum, &lnum, NULL);
3344 else
3345 (void)hasFolding(lnum, NULL, &lnum);
3346 #endif
3347 if (dir == FORWARD)
3348 ++lnum;
3349 if (u_save(lnum - 1, lnum) == FAIL)
3350 goto end;
3351 #ifdef FEAT_FOLDING
3352 if (dir == FORWARD)
3353 curwin->w_cursor.lnum = lnum - 1;
3354 else
3355 curwin->w_cursor.lnum = lnum;
3356 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3357 #endif
3359 else if (u_save_cursor() == FAIL)
3360 goto end;
3362 yanklen = (int)STRLEN(y_array[0]);
3364 #ifdef FEAT_VIRTUALEDIT
3365 if (ve_flags == VE_ALL && y_type == MCHAR)
3367 if (gchar_cursor() == TAB)
3369 /* Don't need to insert spaces when "p" on the last position of a
3370 * tab or "P" on the first position. */
3371 if (dir == FORWARD
3372 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3373 : curwin->w_cursor.coladd > 0)
3374 coladvance_force(getviscol());
3375 else
3376 curwin->w_cursor.coladd = 0;
3378 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3379 coladvance_force(getviscol() + (dir == FORWARD));
3381 #endif
3383 lnum = curwin->w_cursor.lnum;
3384 col = curwin->w_cursor.col;
3386 #ifdef FEAT_VISUAL
3388 * Block mode
3390 if (y_type == MBLOCK)
3392 char c = gchar_cursor();
3393 colnr_T endcol2 = 0;
3395 if (dir == FORWARD && c != NUL)
3397 #ifdef FEAT_VIRTUALEDIT
3398 if (ve_flags == VE_ALL)
3399 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3400 else
3401 #endif
3402 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3404 #ifdef FEAT_MBYTE
3405 if (has_mbyte)
3406 /* move to start of next multi-byte character */
3407 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
3408 else
3409 #endif
3410 #ifdef FEAT_VIRTUALEDIT
3411 if (c != TAB || ve_flags != VE_ALL)
3412 #endif
3413 ++curwin->w_cursor.col;
3414 ++col;
3416 else
3417 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3419 #ifdef FEAT_VIRTUALEDIT
3420 col += curwin->w_cursor.coladd;
3421 if (ve_flags == VE_ALL
3422 && (curwin->w_cursor.coladd > 0
3423 || endcol2 == curwin->w_cursor.col))
3425 if (dir == FORWARD && c == NUL)
3426 ++col;
3427 if (dir != FORWARD && c != NUL)
3428 ++curwin->w_cursor.col;
3429 if (c == TAB)
3431 if (dir == BACKWARD && curwin->w_cursor.col)
3432 curwin->w_cursor.col--;
3433 if (dir == FORWARD && col - 1 == endcol2)
3434 curwin->w_cursor.col++;
3437 curwin->w_cursor.coladd = 0;
3438 #endif
3439 bd.textcol = 0;
3440 for (i = 0; i < y_size; ++i)
3442 int spaces;
3443 char shortline;
3445 bd.startspaces = 0;
3446 bd.endspaces = 0;
3447 vcol = 0;
3448 delcount = 0;
3450 /* add a new line */
3451 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3453 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3454 (colnr_T)1, FALSE) == FAIL)
3455 break;
3456 ++nr_lines;
3458 /* get the old line and advance to the position to insert at */
3459 oldp = ml_get_curline();
3460 oldlen = (int)STRLEN(oldp);
3461 for (ptr = oldp; vcol < col && *ptr; )
3463 /* Count a tab for what it's worth (if list mode not on) */
3464 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3465 vcol += incr;
3467 bd.textcol = (colnr_T)(ptr - oldp);
3469 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3471 if (vcol < col) /* line too short, padd with spaces */
3472 bd.startspaces = col - vcol;
3473 else if (vcol > col)
3475 bd.endspaces = vcol - col;
3476 bd.startspaces = incr - bd.endspaces;
3477 --bd.textcol;
3478 delcount = 1;
3479 #ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3482 #endif
3483 if (oldp[bd.textcol] != TAB)
3485 /* Only a Tab can be split into spaces. Other
3486 * characters will have to be moved to after the
3487 * block, causing misalignment. */
3488 delcount = 0;
3489 bd.endspaces = 0;
3493 yanklen = (int)STRLEN(y_array[i]);
3495 /* calculate number of spaces required to fill right side of block*/
3496 spaces = y_width + 1;
3497 for (j = 0; j < yanklen; j++)
3498 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3499 if (spaces < 0)
3500 spaces = 0;
3502 /* insert the new text */
3503 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3504 newp = alloc_check((unsigned)totlen + oldlen + 1);
3505 if (newp == NULL)
3506 break;
3507 /* copy part up to cursor to new line */
3508 ptr = newp;
3509 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3510 ptr += bd.textcol;
3511 /* may insert some spaces before the new text */
3512 copy_spaces(ptr, (size_t)bd.startspaces);
3513 ptr += bd.startspaces;
3514 /* insert the new text */
3515 for (j = 0; j < count; ++j)
3517 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3518 ptr += yanklen;
3520 /* insert block's trailing spaces only if there's text behind */
3521 if ((j < count - 1 || !shortline) && spaces)
3523 copy_spaces(ptr, (size_t)spaces);
3524 ptr += spaces;
3527 /* may insert some spaces after the new text */
3528 copy_spaces(ptr, (size_t)bd.endspaces);
3529 ptr += bd.endspaces;
3530 /* move the text after the cursor to the end of the line. */
3531 mch_memmove(ptr, oldp + bd.textcol + delcount,
3532 (size_t)(oldlen - bd.textcol - delcount + 1));
3533 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3535 ++curwin->w_cursor.lnum;
3536 if (i == 0)
3537 curwin->w_cursor.col += bd.startspaces;
3540 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3542 /* Set '[ mark. */
3543 curbuf->b_op_start = curwin->w_cursor;
3544 curbuf->b_op_start.lnum = lnum;
3546 /* adjust '] mark */
3547 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3548 curbuf->b_op_end.col = bd.textcol + totlen - 1;
3549 # ifdef FEAT_VIRTUALEDIT
3550 curbuf->b_op_end.coladd = 0;
3551 # endif
3552 if (flags & PUT_CURSEND)
3554 colnr_T len;
3556 curwin->w_cursor = curbuf->b_op_end;
3557 curwin->w_cursor.col++;
3559 /* in Insert mode we might be after the NUL, correct for that */
3560 len = (colnr_T)STRLEN(ml_get_curline());
3561 if (curwin->w_cursor.col > len)
3562 curwin->w_cursor.col = len;
3564 else
3565 curwin->w_cursor.lnum = lnum;
3567 else
3568 #endif
3571 * Character or Line mode
3573 if (y_type == MCHAR)
3575 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3576 * char */
3577 if (dir == FORWARD && gchar_cursor() != NUL)
3579 #ifdef FEAT_MBYTE
3580 if (has_mbyte)
3582 int bytelen = (*mb_ptr2len)(ml_get_cursor());
3584 /* put it on the next of the multi-byte character. */
3585 col += bytelen;
3586 if (yanklen)
3588 curwin->w_cursor.col += bytelen;
3589 curbuf->b_op_end.col += bytelen;
3592 else
3593 #endif
3595 ++col;
3596 if (yanklen)
3598 ++curwin->w_cursor.col;
3599 ++curbuf->b_op_end.col;
3603 curbuf->b_op_start = curwin->w_cursor;
3606 * Line mode: BACKWARD is the same as FORWARD on the previous line
3608 else if (dir == BACKWARD)
3609 --lnum;
3610 new_cursor = curwin->w_cursor;
3613 * simple case: insert into current line
3615 if (y_type == MCHAR && y_size == 1)
3617 totlen = count * yanklen;
3618 if (totlen)
3620 oldp = ml_get(lnum);
3621 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3622 if (newp == NULL)
3623 goto end; /* alloc() will give error message */
3624 mch_memmove(newp, oldp, (size_t)col);
3625 ptr = newp + col;
3626 for (i = 0; i < count; ++i)
3628 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3629 ptr += yanklen;
3631 mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3632 ml_replace(lnum, newp, FALSE);
3633 /* Put cursor on last putted char. */
3634 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3636 curbuf->b_op_end = curwin->w_cursor;
3637 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3638 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3639 ++curwin->w_cursor.col;
3640 changed_bytes(lnum, col);
3642 else
3645 * Insert at least one line. When y_type is MCHAR, break the first
3646 * line in two.
3648 for (cnt = 1; cnt <= count; ++cnt)
3650 i = 0;
3651 if (y_type == MCHAR)
3654 * Split the current line in two at the insert position.
3655 * First insert y_array[size - 1] in front of second line.
3656 * Then append y_array[0] to first line.
3658 lnum = new_cursor.lnum;
3659 ptr = ml_get(lnum) + col;
3660 totlen = (int)STRLEN(y_array[y_size - 1]);
3661 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3662 if (newp == NULL)
3663 goto error;
3664 STRCPY(newp, y_array[y_size - 1]);
3665 STRCAT(newp, ptr);
3666 /* insert second line */
3667 ml_append(lnum, newp, (colnr_T)0, FALSE);
3668 vim_free(newp);
3670 oldp = ml_get(lnum);
3671 newp = alloc_check((unsigned)(col + yanklen + 1));
3672 if (newp == NULL)
3673 goto error;
3674 /* copy first part of line */
3675 mch_memmove(newp, oldp, (size_t)col);
3676 /* append to first line */
3677 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3678 ml_replace(lnum, newp, FALSE);
3680 curwin->w_cursor.lnum = lnum;
3681 i = 1;
3684 for (; i < y_size; ++i)
3686 if ((y_type != MCHAR || i < y_size - 1)
3687 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3688 == FAIL)
3689 goto error;
3690 lnum++;
3691 ++nr_lines;
3692 if (flags & PUT_FIXINDENT)
3694 old_pos = curwin->w_cursor;
3695 curwin->w_cursor.lnum = lnum;
3696 ptr = ml_get(lnum);
3697 if (cnt == count && i == y_size - 1)
3698 lendiff = (int)STRLEN(ptr);
3699 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3700 if (*ptr == '#' && preprocs_left())
3701 indent = 0; /* Leave # lines at start */
3702 else
3703 #endif
3704 if (*ptr == NUL)
3705 indent = 0; /* Ignore empty lines */
3706 else if (first_indent)
3708 indent_diff = orig_indent - get_indent();
3709 indent = orig_indent;
3710 first_indent = FALSE;
3712 else if ((indent = get_indent() + indent_diff) < 0)
3713 indent = 0;
3714 (void)set_indent(indent, 0);
3715 curwin->w_cursor = old_pos;
3716 /* remember how many chars were removed */
3717 if (cnt == count && i == y_size - 1)
3718 lendiff -= (int)STRLEN(ml_get(lnum));
3723 error:
3724 /* Adjust marks. */
3725 if (y_type == MLINE)
3727 curbuf->b_op_start.col = 0;
3728 if (dir == FORWARD)
3729 curbuf->b_op_start.lnum++;
3731 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3732 (linenr_T)MAXLNUM, nr_lines, 0L);
3734 /* note changed text for displaying and folding */
3735 if (y_type == MCHAR)
3736 changed_lines(curwin->w_cursor.lnum, col,
3737 curwin->w_cursor.lnum + 1, nr_lines);
3738 else
3739 changed_lines(curbuf->b_op_start.lnum, 0,
3740 curbuf->b_op_start.lnum, nr_lines);
3742 /* put '] mark at last inserted character */
3743 curbuf->b_op_end.lnum = lnum;
3744 /* correct length for change in indent */
3745 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3746 if (col > 1)
3747 curbuf->b_op_end.col = col - 1;
3748 else
3749 curbuf->b_op_end.col = 0;
3751 if (flags & PUT_CURSLINE)
3753 /* ":put": put cursor on last inserted line */
3754 curwin->w_cursor.lnum = lnum;
3755 beginline(BL_WHITE | BL_FIX);
3757 else if (flags & PUT_CURSEND)
3759 /* put cursor after inserted text */
3760 if (y_type == MLINE)
3762 if (lnum >= curbuf->b_ml.ml_line_count)
3763 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3764 else
3765 curwin->w_cursor.lnum = lnum + 1;
3766 curwin->w_cursor.col = 0;
3768 else
3770 curwin->w_cursor.lnum = lnum;
3771 curwin->w_cursor.col = col;
3774 else if (y_type == MLINE)
3776 /* put cursor on first non-blank in first inserted line */
3777 curwin->w_cursor.col = 0;
3778 if (dir == FORWARD)
3779 ++curwin->w_cursor.lnum;
3780 beginline(BL_WHITE | BL_FIX);
3782 else /* put cursor on first inserted character */
3783 curwin->w_cursor = new_cursor;
3787 msgmore(nr_lines);
3788 curwin->w_set_curswant = TRUE;
3790 end:
3791 if (allocated)
3792 vim_free(insert_string);
3793 if (regname == '=')
3794 vim_free(y_array);
3796 /* If the cursor is past the end of the line put it at the end. */
3797 adjust_cursor_eol();
3801 * When the cursor is on the NUL past the end of the line and it should not be
3802 * there move it left.
3804 void
3805 adjust_cursor_eol()
3807 if (curwin->w_cursor.col > 0
3808 && gchar_cursor() == NUL
3809 #ifdef FEAT_VIRTUALEDIT
3810 && (ve_flags & VE_ONEMORE) == 0
3811 #endif
3812 && !(restart_edit || (State & INSERT)))
3814 /* Put the cursor on the last character in the line. */
3815 dec_cursor();
3817 #ifdef FEAT_VIRTUALEDIT
3818 if (ve_flags == VE_ALL)
3820 colnr_T scol, ecol;
3822 /* Coladd is set to the width of the last character. */
3823 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3824 curwin->w_cursor.coladd = ecol - scol + 1;
3826 #endif
3830 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3832 * Return TRUE if lines starting with '#' should be left aligned.
3835 preprocs_left()
3837 return
3838 # ifdef FEAT_SMARTINDENT
3839 # ifdef FEAT_CINDENT
3840 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3841 # else
3842 curbuf->b_p_si
3843 # endif
3844 # endif
3845 # ifdef FEAT_CINDENT
3846 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3847 # endif
3850 #endif
3852 /* Return the character name of the register with the given number */
3854 get_register_name(num)
3855 int num;
3857 if (num == -1)
3858 return '"';
3859 else if (num < 10)
3860 return num + '0';
3861 else if (num == DELETION_REGISTER)
3862 return '-';
3863 #ifdef FEAT_CLIPBOARD
3864 else if (num == STAR_REGISTER)
3865 return '*';
3866 else if (num == PLUS_REGISTER)
3867 return '+';
3868 #endif
3869 else
3871 #ifdef EBCDIC
3872 int i;
3874 /* EBCDIC is really braindead ... */
3875 i = 'a' + (num - 10);
3876 if (i > 'i')
3877 i += 7;
3878 if (i > 'r')
3879 i += 8;
3880 return i;
3881 #else
3882 return num + 'a' - 10;
3883 #endif
3888 * ":dis" and ":registers": Display the contents of the yank registers.
3890 void
3891 ex_display(eap)
3892 exarg_T *eap;
3894 int i, n;
3895 long j;
3896 char_u *p;
3897 struct yankreg *yb;
3898 int name;
3899 int attr;
3900 char_u *arg = eap->arg;
3901 #ifdef FEAT_MBYTE
3902 int clen;
3903 #else
3904 # define clen 1
3905 #endif
3907 if (arg != NULL && *arg == NUL)
3908 arg = NULL;
3909 attr = hl_attr(HLF_8);
3911 /* Highlight title */
3912 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3913 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3915 name = get_register_name(i);
3916 if (arg != NULL && vim_strchr(arg, name) == NULL)
3917 continue; /* did not ask for this register */
3919 #ifdef FEAT_CLIPBOARD
3920 /* Adjust register name for "unnamed" in 'clipboard'.
3921 * When it's a clipboard register, fill it with the current contents
3922 * of the clipboard. */
3923 adjust_clip_reg(&name);
3924 (void)may_get_selection(name);
3925 #endif
3927 if (i == -1)
3929 if (y_previous != NULL)
3930 yb = y_previous;
3931 else
3932 yb = &(y_regs[0]);
3934 else
3935 yb = &(y_regs[i]);
3936 if (yb->y_array != NULL)
3938 msg_putchar('\n');
3939 msg_putchar('"');
3940 msg_putchar(name);
3941 MSG_PUTS(" ");
3943 n = (int)Columns - 6;
3944 for (j = 0; j < yb->y_size && n > 1; ++j)
3946 if (j)
3948 MSG_PUTS_ATTR("^J", attr);
3949 n -= 2;
3951 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3953 #ifdef FEAT_MBYTE
3954 clen = (*mb_ptr2len)(p);
3955 #endif
3956 msg_outtrans_len(p, clen);
3957 #ifdef FEAT_MBYTE
3958 p += clen - 1;
3959 #endif
3962 if (n > 1 && yb->y_type == MLINE)
3963 MSG_PUTS_ATTR("^J", attr);
3964 out_flush(); /* show one line at a time */
3966 ui_breakcheck();
3970 * display last inserted text
3972 if ((p = get_last_insert()) != NULL
3973 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3975 MSG_PUTS("\n\". ");
3976 dis_msg(p, TRUE);
3980 * display last command line
3982 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3983 && !got_int)
3985 MSG_PUTS("\n\": ");
3986 dis_msg(last_cmdline, FALSE);
3990 * display current file name
3992 if (curbuf->b_fname != NULL
3993 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3995 MSG_PUTS("\n\"% ");
3996 dis_msg(curbuf->b_fname, FALSE);
4000 * display alternate file name
4002 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4004 char_u *fname;
4005 linenr_T dummy;
4007 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4009 MSG_PUTS("\n\"# ");
4010 dis_msg(fname, FALSE);
4015 * display last search pattern
4017 if (last_search_pat() != NULL
4018 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4020 MSG_PUTS("\n\"/ ");
4021 dis_msg(last_search_pat(), FALSE);
4024 #ifdef FEAT_EVAL
4026 * display last used expression
4028 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4029 && !got_int)
4031 MSG_PUTS("\n\"= ");
4032 dis_msg(expr_line, FALSE);
4034 #endif
4038 * display a string for do_dis()
4039 * truncate at end of screen line
4041 static void
4042 dis_msg(p, skip_esc)
4043 char_u *p;
4044 int skip_esc; /* if TRUE, ignore trailing ESC */
4046 int n;
4047 #ifdef FEAT_MBYTE
4048 int l;
4049 #endif
4051 n = (int)Columns - 6;
4052 while (*p != NUL
4053 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4054 && (n -= ptr2cells(p)) >= 0)
4056 #ifdef FEAT_MBYTE
4057 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
4059 msg_outtrans_len(p, l);
4060 p += l;
4062 else
4063 #endif
4064 msg_outtrans_len(p++, 1);
4066 ui_breakcheck();
4070 * join 'count' lines (minimal 2), including u_save()
4072 void
4073 do_do_join(count, insert_space)
4074 long count;
4075 int insert_space;
4077 colnr_T col = MAXCOL;
4079 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4080 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4081 return;
4083 while (--count > 0)
4085 line_breakcheck();
4086 if (got_int || do_join(insert_space) == FAIL)
4088 beep_flush();
4089 break;
4091 if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4092 col = curwin->w_cursor.col;
4095 /* Vi compatible: use the column of the first join */
4096 if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4097 curwin->w_cursor.col = col;
4099 #if 0
4101 * Need to update the screen if the line where the cursor is became too
4102 * long to fit on the screen.
4104 update_topline_redraw();
4105 #endif
4109 * Join two lines at the cursor position.
4110 * "redraw" is TRUE when the screen should be updated.
4111 * Caller must have setup for undo.
4113 * return FAIL for failure, OK otherwise
4116 do_join(insert_space)
4117 int insert_space;
4119 char_u *curr;
4120 char_u *next, *next_start;
4121 char_u *newp;
4122 int endcurr1, endcurr2;
4123 int currsize; /* size of the current line */
4124 int nextsize; /* size of the next line */
4125 int spaces; /* number of spaces to insert */
4126 linenr_T t;
4128 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4129 return FAIL; /* can't join on last line */
4131 curr = ml_get_curline();
4132 currsize = (int)STRLEN(curr);
4133 endcurr1 = endcurr2 = NUL;
4134 if (insert_space && currsize > 0)
4136 #ifdef FEAT_MBYTE
4137 if (has_mbyte)
4139 next = curr + currsize;
4140 mb_ptr_back(curr, next);
4141 endcurr1 = (*mb_ptr2char)(next);
4142 if (next > curr)
4144 mb_ptr_back(curr, next);
4145 endcurr2 = (*mb_ptr2char)(next);
4148 else
4149 #endif
4151 endcurr1 = *(curr + currsize - 1);
4152 if (currsize > 1)
4153 endcurr2 = *(curr + currsize - 2);
4157 next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4158 spaces = 0;
4159 if (insert_space)
4161 next = skipwhite(next);
4162 if (*next != ')' && currsize != 0 && endcurr1 != TAB
4163 #ifdef FEAT_MBYTE
4164 && (!has_format_option(FO_MBYTE_JOIN)
4165 || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4166 && (!has_format_option(FO_MBYTE_JOIN2)
4167 || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4168 #endif
4171 /* don't add a space if the line is ending in a space */
4172 if (endcurr1 == ' ')
4173 endcurr1 = endcurr2;
4174 else
4175 ++spaces;
4176 /* extra space when 'joinspaces' set and line ends in '.' */
4177 if ( p_js
4178 && (endcurr1 == '.'
4179 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4180 && (endcurr1 == '?' || endcurr1 == '!'))))
4181 ++spaces;
4184 nextsize = (int)STRLEN(next);
4186 newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4187 if (newp == NULL)
4188 return FAIL;
4191 * Insert the next line first, because we already have that pointer.
4192 * Curr has to be obtained again, because getting next will have
4193 * invalidated it.
4195 mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4197 curr = ml_get_curline();
4198 mch_memmove(newp, curr, (size_t)currsize);
4200 copy_spaces(newp + currsize, (size_t)spaces);
4202 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4204 /* Only report the change in the first line here, del_lines() will report
4205 * the deleted line. */
4206 changed_lines(curwin->w_cursor.lnum, currsize,
4207 curwin->w_cursor.lnum + 1, 0L);
4210 * Delete the following line. To do this we move the cursor there
4211 * briefly, and then move it back. After del_lines() the cursor may
4212 * have moved up (last line deleted), so the current lnum is kept in t.
4214 * Move marks from the deleted line to the joined line, adjusting the
4215 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4216 * should not really be a problem.
4218 t = curwin->w_cursor.lnum;
4219 mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4220 (long)(currsize + spaces - (next - next_start)));
4221 ++curwin->w_cursor.lnum;
4222 del_lines(1L, FALSE);
4223 curwin->w_cursor.lnum = t;
4226 * go to first character of the joined line
4228 curwin->w_cursor.col = currsize;
4229 check_cursor_col();
4230 #ifdef FEAT_VIRTUALEDIT
4231 curwin->w_cursor.coladd = 0;
4232 #endif
4233 curwin->w_set_curswant = TRUE;
4235 return OK;
4238 #ifdef FEAT_COMMENTS
4240 * Return TRUE if the two comment leaders given are the same. "lnum" is
4241 * the first line. White-space is ignored. Note that the whole of
4242 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4244 static int
4245 same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4246 linenr_T lnum;
4247 int leader1_len;
4248 char_u *leader1_flags;
4249 int leader2_len;
4250 char_u *leader2_flags;
4252 int idx1 = 0, idx2 = 0;
4253 char_u *p;
4254 char_u *line1;
4255 char_u *line2;
4257 if (leader1_len == 0)
4258 return (leader2_len == 0);
4261 * If first leader has 'f' flag, the lines can be joined only if the
4262 * second line does not have a leader.
4263 * If first leader has 'e' flag, the lines can never be joined.
4264 * If fist leader has 's' flag, the lines can only be joined if there is
4265 * some text after it and the second line has the 'm' flag.
4267 if (leader1_flags != NULL)
4269 for (p = leader1_flags; *p && *p != ':'; ++p)
4271 if (*p == COM_FIRST)
4272 return (leader2_len == 0);
4273 if (*p == COM_END)
4274 return FALSE;
4275 if (*p == COM_START)
4277 if (*(ml_get(lnum) + leader1_len) == NUL)
4278 return FALSE;
4279 if (leader2_flags == NULL || leader2_len == 0)
4280 return FALSE;
4281 for (p = leader2_flags; *p && *p != ':'; ++p)
4282 if (*p == COM_MIDDLE)
4283 return TRUE;
4284 return FALSE;
4290 * Get current line and next line, compare the leaders.
4291 * The first line has to be saved, only one line can be locked at a time.
4293 line1 = vim_strsave(ml_get(lnum));
4294 if (line1 != NULL)
4296 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4298 line2 = ml_get(lnum + 1);
4299 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4301 if (!vim_iswhite(line2[idx2]))
4303 if (line1[idx1++] != line2[idx2])
4304 break;
4306 else
4307 while (vim_iswhite(line1[idx1]))
4308 ++idx1;
4310 vim_free(line1);
4312 return (idx2 == leader2_len && idx1 == leader1_len);
4314 #endif
4317 * implementation of the format operator 'gq'
4319 void
4320 op_format(oap, keep_cursor)
4321 oparg_T *oap;
4322 int keep_cursor; /* keep cursor on same text char */
4324 long old_line_count = curbuf->b_ml.ml_line_count;
4326 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4327 * can put it back there. */
4328 curwin->w_cursor = oap->cursor_start;
4330 if (u_save((linenr_T)(oap->start.lnum - 1),
4331 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4332 return;
4333 curwin->w_cursor = oap->start;
4335 #ifdef FEAT_VISUAL
4336 if (oap->is_VIsual)
4337 /* When there is no change: need to remove the Visual selection */
4338 redraw_curbuf_later(INVERTED);
4339 #endif
4341 /* Set '[ mark at the start of the formatted area */
4342 curbuf->b_op_start = oap->start;
4344 /* For "gw" remember the cursor position and put it back below (adjusted
4345 * for joined and split lines). */
4346 if (keep_cursor)
4347 saved_cursor = oap->cursor_start;
4349 format_lines(oap->line_count);
4352 * Leave the cursor at the first non-blank of the last formatted line.
4353 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4354 * line, so "." will do the next lines.
4356 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4357 ++curwin->w_cursor.lnum;
4358 beginline(BL_WHITE | BL_FIX);
4359 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4360 msgmore(old_line_count);
4362 /* put '] mark on the end of the formatted area */
4363 curbuf->b_op_end = curwin->w_cursor;
4365 if (keep_cursor)
4367 curwin->w_cursor = saved_cursor;
4368 saved_cursor.lnum = 0;
4371 #ifdef FEAT_VISUAL
4372 if (oap->is_VIsual)
4374 win_T *wp;
4376 FOR_ALL_WINDOWS(wp)
4378 if (wp->w_old_cursor_lnum != 0)
4380 /* When lines have been inserted or deleted, adjust the end of
4381 * the Visual area to be redrawn. */
4382 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4383 wp->w_old_cursor_lnum += old_line_count;
4384 else
4385 wp->w_old_visual_lnum += old_line_count;
4389 #endif
4392 #if defined(FEAT_EVAL) || defined(PROTO)
4394 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4396 void
4397 op_formatexpr(oap)
4398 oparg_T *oap;
4400 # ifdef FEAT_VISUAL
4401 if (oap->is_VIsual)
4402 /* When there is no change: need to remove the Visual selection */
4403 redraw_curbuf_later(INVERTED);
4404 # endif
4406 (void)fex_format(oap->start.lnum, oap->line_count, NUL);
4410 fex_format(lnum, count, c)
4411 linenr_T lnum;
4412 long count;
4413 int c; /* character to be inserted */
4415 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4416 OPT_LOCAL);
4417 int r;
4418 #ifdef FEAT_MBYTE
4419 char_u buf[MB_MAXBYTES];
4420 #else
4421 char_u buf[2];
4422 #endif
4425 * Set v:lnum to the first line number and v:count to the number of lines.
4426 * Set v:char to the character to be inserted (can be NUL).
4428 set_vim_var_nr(VV_LNUM, lnum);
4429 set_vim_var_nr(VV_COUNT, count);
4431 #ifdef FEAT_MBYTE
4432 if (has_mbyte)
4433 buf[(*mb_char2bytes)(c, buf)] = NUL;
4434 else
4435 #endif
4437 buf[0] = c;
4438 buf[1] = NUL;
4440 set_vim_var_string(VV_CHAR, buf, -1);
4443 * Evaluate the function.
4445 if (use_sandbox)
4446 ++sandbox;
4447 r = eval_to_number(curbuf->b_p_fex);
4448 if (use_sandbox)
4449 --sandbox;
4451 set_vim_var_string(VV_CHAR, NULL, -1);
4453 return r;
4455 #endif
4458 * Format "line_count" lines, starting at the cursor position.
4459 * When "line_count" is negative, format until the end of the paragraph.
4460 * Lines after the cursor line are saved for undo, caller must have saved the
4461 * first line.
4463 void
4464 format_lines(line_count)
4465 linenr_T line_count;
4467 int max_len;
4468 int is_not_par; /* current line not part of parag. */
4469 int next_is_not_par; /* next line not part of paragraph */
4470 int is_end_par; /* at end of paragraph */
4471 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4472 int next_is_start_par = FALSE;
4473 #ifdef FEAT_COMMENTS
4474 int leader_len = 0; /* leader len of current line */
4475 int next_leader_len; /* leader len of next line */
4476 char_u *leader_flags = NULL; /* flags for leader of current line */
4477 char_u *next_leader_flags; /* flags for leader of next line */
4478 int do_comments; /* format comments */
4479 #endif
4480 int advance = TRUE;
4481 int second_indent = -1;
4482 int do_second_indent;
4483 int do_number_indent;
4484 int do_trail_white;
4485 int first_par_line = TRUE;
4486 int smd_save;
4487 long count;
4488 int need_set_indent = TRUE; /* set indent of next paragraph */
4489 int force_format = FALSE;
4490 int old_State = State;
4492 /* length of a line to force formatting: 3 * 'tw' */
4493 max_len = comp_textwidth(TRUE) * 3;
4495 /* check for 'q', '2' and '1' in 'formatoptions' */
4496 #ifdef FEAT_COMMENTS
4497 do_comments = has_format_option(FO_Q_COMS);
4498 #endif
4499 do_second_indent = has_format_option(FO_Q_SECOND);
4500 do_number_indent = has_format_option(FO_Q_NUMBER);
4501 do_trail_white = has_format_option(FO_WHITE_PAR);
4504 * Get info about the previous and current line.
4506 if (curwin->w_cursor.lnum > 1)
4507 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4508 #ifdef FEAT_COMMENTS
4509 , &leader_len, &leader_flags, do_comments
4510 #endif
4512 else
4513 is_not_par = TRUE;
4514 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4515 #ifdef FEAT_COMMENTS
4516 , &next_leader_len, &next_leader_flags, do_comments
4517 #endif
4519 is_end_par = (is_not_par || next_is_not_par);
4520 if (!is_end_par && do_trail_white)
4521 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4523 curwin->w_cursor.lnum--;
4524 for (count = line_count; count != 0 && !got_int; --count)
4527 * Advance to next paragraph.
4529 if (advance)
4531 curwin->w_cursor.lnum++;
4532 prev_is_end_par = is_end_par;
4533 is_not_par = next_is_not_par;
4534 #ifdef FEAT_COMMENTS
4535 leader_len = next_leader_len;
4536 leader_flags = next_leader_flags;
4537 #endif
4541 * The last line to be formatted.
4543 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4545 next_is_not_par = TRUE;
4546 #ifdef FEAT_COMMENTS
4547 next_leader_len = 0;
4548 next_leader_flags = NULL;
4549 #endif
4551 else
4553 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4554 #ifdef FEAT_COMMENTS
4555 , &next_leader_len, &next_leader_flags, do_comments
4556 #endif
4558 if (do_number_indent)
4559 next_is_start_par =
4560 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4562 advance = TRUE;
4563 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4564 if (!is_end_par && do_trail_white)
4565 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4568 * Skip lines that are not in a paragraph.
4570 if (is_not_par)
4572 if (line_count < 0)
4573 break;
4575 else
4578 * For the first line of a paragraph, check indent of second line.
4579 * Don't do this for comments and empty lines.
4581 if (first_par_line
4582 && (do_second_indent || do_number_indent)
4583 && prev_is_end_par
4584 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4585 #ifdef FEAT_COMMENTS
4586 && leader_len == 0
4587 && next_leader_len == 0
4588 #endif
4591 if (do_second_indent
4592 && !lineempty(curwin->w_cursor.lnum + 1))
4593 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4594 else if (do_number_indent)
4595 second_indent = get_number_indent(curwin->w_cursor.lnum);
4599 * When the comment leader changes, it's the end of the paragraph.
4601 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4602 #ifdef FEAT_COMMENTS
4603 || !same_leader(curwin->w_cursor.lnum,
4604 leader_len, leader_flags,
4605 next_leader_len, next_leader_flags)
4606 #endif
4608 is_end_par = TRUE;
4611 * If we have got to the end of a paragraph, or the line is
4612 * getting long, format it.
4614 if (is_end_par || force_format)
4616 if (need_set_indent)
4617 /* replace indent in first line with minimal number of
4618 * tabs and spaces, according to current options */
4619 (void)set_indent(get_indent(), SIN_CHANGED);
4621 /* put cursor on last non-space */
4622 State = NORMAL; /* don't go past end-of-line */
4623 coladvance((colnr_T)MAXCOL);
4624 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4625 dec_cursor();
4627 /* do the formatting, without 'showmode' */
4628 State = INSERT; /* for open_line() */
4629 smd_save = p_smd;
4630 p_smd = FALSE;
4631 insertchar(NUL, INSCHAR_FORMAT
4632 #ifdef FEAT_COMMENTS
4633 + (do_comments ? INSCHAR_DO_COM : 0)
4634 #endif
4635 , second_indent);
4636 State = old_State;
4637 p_smd = smd_save;
4638 second_indent = -1;
4639 /* at end of par.: need to set indent of next par. */
4640 need_set_indent = is_end_par;
4641 if (is_end_par)
4643 /* When called with a negative line count, break at the
4644 * end of the paragraph. */
4645 if (line_count < 0)
4646 break;
4647 first_par_line = TRUE;
4649 force_format = FALSE;
4653 * When still in same paragraph, join the lines together. But
4654 * first delete the comment leader from the second line.
4656 if (!is_end_par)
4658 advance = FALSE;
4659 curwin->w_cursor.lnum++;
4660 curwin->w_cursor.col = 0;
4661 if (line_count < 0 && u_save_cursor() == FAIL)
4662 break;
4663 #ifdef FEAT_COMMENTS
4664 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
4665 if (next_leader_len > 0)
4666 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4667 (long)-next_leader_len);
4668 #endif
4669 curwin->w_cursor.lnum--;
4670 if (do_join(TRUE) == FAIL)
4672 beep_flush();
4673 break;
4675 first_par_line = FALSE;
4676 /* If the line is getting long, format it next time */
4677 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4678 force_format = TRUE;
4679 else
4680 force_format = FALSE;
4683 line_breakcheck();
4688 * Return TRUE if line "lnum" ends in a white character.
4690 static int
4691 ends_in_white(lnum)
4692 linenr_T lnum;
4694 char_u *s = ml_get(lnum);
4695 size_t l;
4697 if (*s == NUL)
4698 return FALSE;
4699 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4700 * invocation may call function multiple times". */
4701 l = STRLEN(s) - 1;
4702 return vim_iswhite(s[l]);
4706 * Blank lines, and lines containing only the comment leader, are left
4707 * untouched by the formatting. The function returns TRUE in this
4708 * case. It also returns TRUE when a line starts with the end of a comment
4709 * ('e' in comment flags), so that this line is skipped, and not joined to the
4710 * previous line. A new paragraph starts after a blank line, or when the
4711 * comment leader changes -- webb.
4713 #ifdef FEAT_COMMENTS
4714 static int
4715 fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4716 linenr_T lnum;
4717 int *leader_len;
4718 char_u **leader_flags;
4719 int do_comments;
4721 char_u *flags = NULL; /* init for GCC */
4722 char_u *ptr;
4724 ptr = ml_get(lnum);
4725 if (do_comments)
4726 *leader_len = get_leader_len(ptr, leader_flags, FALSE);
4727 else
4728 *leader_len = 0;
4730 if (*leader_len > 0)
4733 * Search for 'e' flag in comment leader flags.
4735 flags = *leader_flags;
4736 while (*flags && *flags != ':' && *flags != COM_END)
4737 ++flags;
4740 return (*skipwhite(ptr + *leader_len) == NUL
4741 || (*leader_len > 0 && *flags == COM_END)
4742 || startPS(lnum, NUL, FALSE));
4744 #else
4745 static int
4746 fmt_check_par(lnum)
4747 linenr_T lnum;
4749 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4751 #endif
4754 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4755 * previous line is in the same paragraph. Used for auto-formatting.
4758 paragraph_start(lnum)
4759 linenr_T lnum;
4761 char_u *p;
4762 #ifdef FEAT_COMMENTS
4763 int leader_len = 0; /* leader len of current line */
4764 char_u *leader_flags = NULL; /* flags for leader of current line */
4765 int next_leader_len; /* leader len of next line */
4766 char_u *next_leader_flags; /* flags for leader of next line */
4767 int do_comments; /* format comments */
4768 #endif
4770 if (lnum <= 1)
4771 return TRUE; /* start of the file */
4773 p = ml_get(lnum - 1);
4774 if (*p == NUL)
4775 return TRUE; /* after empty line */
4777 #ifdef FEAT_COMMENTS
4778 do_comments = has_format_option(FO_Q_COMS);
4779 #endif
4780 if (fmt_check_par(lnum - 1
4781 #ifdef FEAT_COMMENTS
4782 , &leader_len, &leader_flags, do_comments
4783 #endif
4785 return TRUE; /* after non-paragraph line */
4787 if (fmt_check_par(lnum
4788 #ifdef FEAT_COMMENTS
4789 , &next_leader_len, &next_leader_flags, do_comments
4790 #endif
4792 return TRUE; /* "lnum" is not a paragraph line */
4794 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4795 return TRUE; /* missing trailing space in previous line. */
4797 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4798 return TRUE; /* numbered item starts in "lnum". */
4800 #ifdef FEAT_COMMENTS
4801 if (!same_leader(lnum - 1, leader_len, leader_flags,
4802 next_leader_len, next_leader_flags))
4803 return TRUE; /* change of comment leader. */
4804 #endif
4806 return FALSE;
4809 #ifdef FEAT_VISUAL
4811 * prepare a few things for block mode yank/delete/tilde
4813 * for delete:
4814 * - textlen includes the first/last char to be (partly) deleted
4815 * - start/endspaces is the number of columns that are taken by the
4816 * first/last deleted char minus the number of columns that have to be
4817 * deleted. for yank and tilde:
4818 * - textlen includes the first/last char to be wholly yanked
4819 * - start/endspaces is the number of columns of the first/last yanked char
4820 * that are to be yanked.
4822 static void
4823 block_prep(oap, bdp, lnum, is_del)
4824 oparg_T *oap;
4825 struct block_def *bdp;
4826 linenr_T lnum;
4827 int is_del;
4829 int incr = 0;
4830 char_u *pend;
4831 char_u *pstart;
4832 char_u *line;
4833 char_u *prev_pstart;
4834 char_u *prev_pend;
4836 bdp->startspaces = 0;
4837 bdp->endspaces = 0;
4838 bdp->textlen = 0;
4839 bdp->start_vcol = 0;
4840 bdp->end_vcol = 0;
4841 #ifdef FEAT_VISUALEXTRA
4842 bdp->is_short = FALSE;
4843 bdp->is_oneChar = FALSE;
4844 bdp->pre_whitesp = 0;
4845 bdp->pre_whitesp_c = 0;
4846 bdp->end_char_vcols = 0;
4847 #endif
4848 bdp->start_char_vcols = 0;
4850 line = ml_get(lnum);
4851 pstart = line;
4852 prev_pstart = line;
4853 while (bdp->start_vcol < oap->start_vcol && *pstart)
4855 /* Count a tab for what it's worth (if list mode not on) */
4856 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4857 bdp->start_vcol += incr;
4858 #ifdef FEAT_VISUALEXTRA
4859 if (vim_iswhite(*pstart))
4861 bdp->pre_whitesp += incr;
4862 bdp->pre_whitesp_c++;
4864 else
4866 bdp->pre_whitesp = 0;
4867 bdp->pre_whitesp_c = 0;
4869 #endif
4870 prev_pstart = pstart;
4871 mb_ptr_adv(pstart);
4873 bdp->start_char_vcols = incr;
4874 if (bdp->start_vcol < oap->start_vcol) /* line too short */
4876 bdp->end_vcol = bdp->start_vcol;
4877 #ifdef FEAT_VISUALEXTRA
4878 bdp->is_short = TRUE;
4879 #endif
4880 if (!is_del || oap->op_type == OP_APPEND)
4881 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4883 else
4885 /* notice: this converts partly selected Multibyte characters to
4886 * spaces, too. */
4887 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4888 if (is_del && bdp->startspaces)
4889 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4890 pend = pstart;
4891 bdp->end_vcol = bdp->start_vcol;
4892 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
4894 #ifdef FEAT_VISUALEXTRA
4895 bdp->is_oneChar = TRUE;
4896 #endif
4897 if (oap->op_type == OP_INSERT)
4898 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4899 else if (oap->op_type == OP_APPEND)
4901 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4902 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4904 else
4906 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4907 if (is_del && oap->op_type != OP_LSHIFT)
4909 /* just putting the sum of those two into
4910 * bdp->startspaces doesn't work for Visual replace,
4911 * so we have to split the tab in two */
4912 bdp->startspaces = bdp->start_char_vcols
4913 - (bdp->start_vcol - oap->start_vcol);
4914 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4918 else
4920 prev_pend = pend;
4921 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4923 /* Count a tab for what it's worth (if list mode not on) */
4924 prev_pend = pend;
4925 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4926 bdp->end_vcol += incr;
4928 if (bdp->end_vcol <= oap->end_vcol
4929 && (!is_del
4930 || oap->op_type == OP_APPEND
4931 || oap->op_type == OP_REPLACE)) /* line too short */
4933 #ifdef FEAT_VISUALEXTRA
4934 bdp->is_short = TRUE;
4935 #endif
4936 /* Alternative: include spaces to fill up the block.
4937 * Disadvantage: can lead to trailing spaces when the line is
4938 * short where the text is put */
4939 /* if (!is_del || oap->op_type == OP_APPEND) */
4940 if (oap->op_type == OP_APPEND || virtual_op)
4941 bdp->endspaces = oap->end_vcol - bdp->end_vcol
4942 + oap->inclusive;
4943 else
4944 bdp->endspaces = 0; /* replace doesn't add characters */
4946 else if (bdp->end_vcol > oap->end_vcol)
4948 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4949 if (!is_del && bdp->endspaces)
4951 bdp->endspaces = incr - bdp->endspaces;
4952 if (pend != pstart)
4953 pend = prev_pend;
4957 #ifdef FEAT_VISUALEXTRA
4958 bdp->end_char_vcols = incr;
4959 #endif
4960 if (is_del && bdp->startspaces)
4961 pstart = prev_pstart;
4962 bdp->textlen = (int)(pend - pstart);
4964 bdp->textcol = (colnr_T) (pstart - line);
4965 bdp->textstart = pstart;
4967 #endif /* FEAT_VISUAL */
4969 #ifdef FEAT_RIGHTLEFT
4970 static void reverse_line __ARGS((char_u *s));
4972 static void
4973 reverse_line(s)
4974 char_u *s;
4976 int i, j;
4977 char_u c;
4979 if ((i = (int)STRLEN(s) - 1) <= 0)
4980 return;
4982 curwin->w_cursor.col = i - curwin->w_cursor.col;
4983 for (j = 0; j < i; j++, i--)
4985 c = s[i]; s[i] = s[j]; s[j] = c;
4989 # define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4990 #else
4991 # define RLADDSUBFIX(ptr)
4992 #endif
4995 * add or subtract 'Prenum1' from a number in a line
4996 * 'command' is CTRL-A for add, CTRL-X for subtract
4998 * return FAIL for failure, OK otherwise
5001 do_addsub(command, Prenum1)
5002 int command;
5003 linenr_T Prenum1;
5005 int col;
5006 char_u *buf1;
5007 char_u buf2[NUMBUFLEN];
5008 int hex; /* 'X' or 'x': hex; '0': octal */
5009 static int hexupper = FALSE; /* 0xABC */
5010 unsigned long n;
5011 long_u oldn;
5012 char_u *ptr;
5013 int c;
5014 int length = 0; /* character length of the number */
5015 int todel;
5016 int dohex;
5017 int dooct;
5018 int doalp;
5019 int firstdigit;
5020 int negative;
5021 int subtract;
5023 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5024 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5025 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5027 ptr = ml_get_curline();
5028 RLADDSUBFIX(ptr);
5031 * First check if we are on a hexadecimal number, after the "0x".
5033 col = curwin->w_cursor.col;
5034 if (dohex)
5035 while (col > 0 && vim_isxdigit(ptr[col]))
5036 --col;
5037 if ( dohex
5038 && col > 0
5039 && (ptr[col] == 'X'
5040 || ptr[col] == 'x')
5041 && ptr[col - 1] == '0'
5042 && vim_isxdigit(ptr[col + 1]))
5045 * Found hexadecimal number, move to its start.
5047 --col;
5049 else
5052 * Search forward and then backward to find the start of number.
5054 col = curwin->w_cursor.col;
5056 while (ptr[col] != NUL
5057 && !vim_isdigit(ptr[col])
5058 && !(doalp && ASCII_ISALPHA(ptr[col])))
5059 ++col;
5061 while (col > 0
5062 && vim_isdigit(ptr[col - 1])
5063 && !(doalp && ASCII_ISALPHA(ptr[col])))
5064 --col;
5068 * If a number was found, and saving for undo works, replace the number.
5070 firstdigit = ptr[col];
5071 RLADDSUBFIX(ptr);
5072 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5073 || u_save_cursor() != OK)
5075 beep_flush();
5076 return FAIL;
5079 /* get ptr again, because u_save() may have changed it */
5080 ptr = ml_get_curline();
5081 RLADDSUBFIX(ptr);
5083 if (doalp && ASCII_ISALPHA(firstdigit))
5085 /* decrement or increment alphabetic character */
5086 if (command == Ctrl_X)
5088 if (CharOrd(firstdigit) < Prenum1)
5090 if (isupper(firstdigit))
5091 firstdigit = 'A';
5092 else
5093 firstdigit = 'a';
5095 else
5096 #ifdef EBCDIC
5097 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5098 #else
5099 firstdigit -= Prenum1;
5100 #endif
5102 else
5104 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5106 if (isupper(firstdigit))
5107 firstdigit = 'Z';
5108 else
5109 firstdigit = 'z';
5111 else
5112 #ifdef EBCDIC
5113 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5114 #else
5115 firstdigit += Prenum1;
5116 #endif
5118 curwin->w_cursor.col = col;
5119 (void)del_char(FALSE);
5120 ins_char(firstdigit);
5122 else
5124 negative = FALSE;
5125 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5127 --col;
5128 negative = TRUE;
5131 /* get the number value (unsigned) */
5132 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5134 /* ignore leading '-' for hex and octal numbers */
5135 if (hex && negative)
5137 ++col;
5138 --length;
5139 negative = FALSE;
5142 /* add or subtract */
5143 subtract = FALSE;
5144 if (command == Ctrl_X)
5145 subtract ^= TRUE;
5146 if (negative)
5147 subtract ^= TRUE;
5149 oldn = n;
5150 if (subtract)
5151 n -= (unsigned long)Prenum1;
5152 else
5153 n += (unsigned long)Prenum1;
5155 /* handle wraparound for decimal numbers */
5156 if (!hex)
5158 if (subtract)
5160 if (n > oldn)
5162 n = 1 + (n ^ (unsigned long)-1);
5163 negative ^= TRUE;
5166 else /* add */
5168 if (n < oldn)
5170 n = (n ^ (unsigned long)-1);
5171 negative ^= TRUE;
5174 if (n == 0)
5175 negative = FALSE;
5179 * Delete the old number.
5181 curwin->w_cursor.col = col;
5182 todel = length;
5183 c = gchar_cursor();
5185 * Don't include the '-' in the length, only the length of the part
5186 * after it is kept the same.
5188 if (c == '-')
5189 --length;
5190 while (todel-- > 0)
5192 if (c < 0x100 && isalpha(c))
5194 if (isupper(c))
5195 hexupper = TRUE;
5196 else
5197 hexupper = FALSE;
5199 /* del_char() will mark line needing displaying */
5200 (void)del_char(FALSE);
5201 c = gchar_cursor();
5205 * Prepare the leading characters in buf1[].
5206 * When there are many leading zeros it could be very long. Allocate
5207 * a bit too much.
5209 buf1 = alloc((unsigned)length + NUMBUFLEN);
5210 if (buf1 == NULL)
5211 return FAIL;
5212 ptr = buf1;
5213 if (negative)
5215 *ptr++ = '-';
5217 if (hex)
5219 *ptr++ = '0';
5220 --length;
5222 if (hex == 'x' || hex == 'X')
5224 *ptr++ = hex;
5225 --length;
5229 * Put the number characters in buf2[].
5231 if (hex == 0)
5232 sprintf((char *)buf2, "%lu", n);
5233 else if (hex == '0')
5234 sprintf((char *)buf2, "%lo", n);
5235 else if (hex && hexupper)
5236 sprintf((char *)buf2, "%lX", n);
5237 else
5238 sprintf((char *)buf2, "%lx", n);
5239 length -= (int)STRLEN(buf2);
5242 * Adjust number of zeros to the new number of digits, so the
5243 * total length of the number remains the same.
5244 * Don't do this when
5245 * the result may look like an octal number.
5247 if (firstdigit == '0' && !(dooct && hex == 0))
5248 while (length-- > 0)
5249 *ptr++ = '0';
5250 *ptr = NUL;
5251 STRCAT(buf1, buf2);
5252 ins_str(buf1); /* insert the new number */
5253 vim_free(buf1);
5255 --curwin->w_cursor.col;
5256 curwin->w_set_curswant = TRUE;
5257 #ifdef FEAT_RIGHTLEFT
5258 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5259 RLADDSUBFIX(ptr);
5260 #endif
5261 return OK;
5264 #ifdef FEAT_VIMINFO
5266 read_viminfo_register(virp, force)
5267 vir_T *virp;
5268 int force;
5270 int eof;
5271 int do_it = TRUE;
5272 int size;
5273 int limit;
5274 int i;
5275 int set_prev = FALSE;
5276 char_u *str;
5277 char_u **array = NULL;
5279 /* We only get here (hopefully) if line[0] == '"' */
5280 str = virp->vir_line + 1;
5281 if (*str == '"')
5283 set_prev = TRUE;
5284 str++;
5286 if (!ASCII_ISALNUM(*str) && *str != '-')
5288 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5289 return TRUE; /* too many errors, pretend end-of-file */
5290 do_it = FALSE;
5292 get_yank_register(*str++, FALSE);
5293 if (!force && y_current->y_array != NULL)
5294 do_it = FALSE;
5295 size = 0;
5296 limit = 100; /* Optimized for registers containing <= 100 lines */
5297 if (do_it)
5299 if (set_prev)
5300 y_previous = y_current;
5301 vim_free(y_current->y_array);
5302 array = y_current->y_array =
5303 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5304 str = skipwhite(str);
5305 if (STRNCMP(str, "CHAR", 4) == 0)
5306 y_current->y_type = MCHAR;
5307 #ifdef FEAT_VISUAL
5308 else if (STRNCMP(str, "BLOCK", 5) == 0)
5309 y_current->y_type = MBLOCK;
5310 #endif
5311 else
5312 y_current->y_type = MLINE;
5313 /* get the block width; if it's missing we get a zero, which is OK */
5314 str = skipwhite(skiptowhite(str));
5315 #ifdef FEAT_VISUAL
5316 y_current->y_width = getdigits(&str);
5317 #else
5318 (void)getdigits(&str);
5319 #endif
5322 while (!(eof = viminfo_readline(virp))
5323 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5325 if (do_it)
5327 if (size >= limit)
5329 y_current->y_array = (char_u **)
5330 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5331 for (i = 0; i < limit; i++)
5332 y_current->y_array[i] = array[i];
5333 vim_free(array);
5334 limit *= 2;
5335 array = y_current->y_array;
5337 str = viminfo_readstring(virp, 1, TRUE);
5338 if (str != NULL)
5339 array[size++] = str;
5340 else
5341 do_it = FALSE;
5344 if (do_it)
5346 if (size == 0)
5348 vim_free(array);
5349 y_current->y_array = NULL;
5351 else if (size < limit)
5353 y_current->y_array =
5354 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5355 for (i = 0; i < size; i++)
5356 y_current->y_array[i] = array[i];
5357 vim_free(array);
5359 y_current->y_size = size;
5361 return eof;
5364 void
5365 write_viminfo_registers(fp)
5366 FILE *fp;
5368 int i, j;
5369 char_u *type;
5370 char_u c;
5371 int num_lines;
5372 int max_num_lines;
5373 int max_kbyte;
5374 long len;
5376 fprintf(fp, _("\n# Registers:\n"));
5378 /* Get '<' value, use old '"' value if '<' is not found. */
5379 max_num_lines = get_viminfo_parameter('<');
5380 if (max_num_lines < 0)
5381 max_num_lines = get_viminfo_parameter('"');
5382 if (max_num_lines == 0)
5383 return;
5384 max_kbyte = get_viminfo_parameter('s');
5385 if (max_kbyte == 0)
5386 return;
5387 for (i = 0; i < NUM_REGISTERS; i++)
5389 if (y_regs[i].y_array == NULL)
5390 continue;
5391 #ifdef FEAT_CLIPBOARD
5392 /* Skip '*'/'+' register, we don't want them back next time */
5393 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5394 continue;
5395 #endif
5396 #ifdef FEAT_DND
5397 /* Neither do we want the '~' register */
5398 if (i == TILDE_REGISTER)
5399 continue;
5400 #endif
5401 /* Skip empty registers. */
5402 num_lines = y_regs[i].y_size;
5403 if (num_lines == 0
5404 || (num_lines == 1 && y_regs[i].y_type == MCHAR
5405 && STRLEN(y_regs[i].y_array[0]) == 0))
5406 continue;
5408 if (max_kbyte > 0)
5410 /* Skip register if there is more text than the maximum size. */
5411 len = 0;
5412 for (j = 0; j < num_lines; j++)
5413 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
5414 if (len > (long)max_kbyte * 1024L)
5415 continue;
5418 switch (y_regs[i].y_type)
5420 case MLINE:
5421 type = (char_u *)"LINE";
5422 break;
5423 case MCHAR:
5424 type = (char_u *)"CHAR";
5425 break;
5426 #ifdef FEAT_VISUAL
5427 case MBLOCK:
5428 type = (char_u *)"BLOCK";
5429 break;
5430 #endif
5431 default:
5432 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
5433 y_regs[i].y_type);
5434 emsg(IObuff);
5435 type = (char_u *)"LINE";
5436 break;
5438 if (y_previous == &y_regs[i])
5439 fprintf(fp, "\"");
5440 c = get_register_name(i);
5441 fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5442 #ifdef FEAT_VISUAL
5443 (int)y_regs[i].y_width
5444 #else
5446 #endif
5449 /* If max_num_lines < 0, then we save ALL the lines in the register */
5450 if (max_num_lines > 0 && num_lines > max_num_lines)
5451 num_lines = max_num_lines;
5452 for (j = 0; j < num_lines; j++)
5454 putc('\t', fp);
5455 viminfo_writestring(fp, y_regs[i].y_array[j]);
5459 #endif /* FEAT_VIMINFO */
5461 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
5463 * SELECTION / PRIMARY ('*')
5465 * Text selection stuff that uses the GUI selection register '*'. When using a
5466 * GUI this may be text from another window, otherwise it is the last text we
5467 * had highlighted with VIsual mode. With mouse support, clicking the middle
5468 * button performs the paste, otherwise you will need to do <"*p>. "
5469 * If not under X, it is synonymous with the clipboard register '+'.
5471 * X CLIPBOARD ('+')
5473 * Text selection stuff that uses the GUI clipboard register '+'.
5474 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5475 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5476 * otherwise you will need to do <"+p>. "
5477 * If not under X, it is synonymous with the selection register '*'.
5481 * Routine to export any final X selection we had to the environment
5482 * so that the text is still available after vim has exited. X selections
5483 * only exist while the owning application exists, so we write to the
5484 * permanent (while X runs) store CUT_BUFFER0.
5485 * Dump the CLIPBOARD selection if we own it (it's logically the more
5486 * 'permanent' of the two), otherwise the PRIMARY one.
5487 * For now, use a hard-coded sanity limit of 1Mb of data.
5489 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5490 void
5491 x11_export_final_selection()
5493 Display *dpy;
5494 char_u *str = NULL;
5495 long_u len = 0;
5496 int motion_type = -1;
5498 # ifdef FEAT_GUI
5499 if (gui.in_use)
5500 dpy = X_DISPLAY;
5501 else
5502 # endif
5503 # ifdef FEAT_XCLIPBOARD
5504 dpy = xterm_dpy;
5505 # else
5506 return;
5507 # endif
5509 /* Get selection to export */
5510 if (clip_plus.owned)
5511 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5512 else if (clip_star.owned)
5513 motion_type = clip_convert_selection(&str, &len, &clip_star);
5515 /* Check it's OK */
5516 if (dpy != NULL && str != NULL && motion_type >= 0
5517 && len < 1024*1024 && len > 0)
5519 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5520 XFlush(dpy);
5523 vim_free(str);
5525 #endif
5527 void
5528 clip_free_selection(cbd)
5529 VimClipboard *cbd;
5531 struct yankreg *y_ptr = y_current;
5533 if (cbd == &clip_plus)
5534 y_current = &y_regs[PLUS_REGISTER];
5535 else
5536 y_current = &y_regs[STAR_REGISTER];
5537 free_yank_all();
5538 y_current->y_size = 0;
5539 y_current = y_ptr;
5543 * Get the selected text and put it in the gui selection register '*' or '+'.
5545 void
5546 clip_get_selection(cbd)
5547 VimClipboard *cbd;
5549 struct yankreg *old_y_previous, *old_y_current;
5550 pos_T old_cursor;
5551 #ifdef FEAT_VISUAL
5552 pos_T old_visual;
5553 int old_visual_mode;
5554 #endif
5555 colnr_T old_curswant;
5556 int old_set_curswant;
5557 pos_T old_op_start, old_op_end;
5558 oparg_T oa;
5559 cmdarg_T ca;
5561 if (cbd->owned)
5563 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5564 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5565 return;
5567 /* Get the text between clip_star.start & clip_star.end */
5568 old_y_previous = y_previous;
5569 old_y_current = y_current;
5570 old_cursor = curwin->w_cursor;
5571 old_curswant = curwin->w_curswant;
5572 old_set_curswant = curwin->w_set_curswant;
5573 old_op_start = curbuf->b_op_start;
5574 old_op_end = curbuf->b_op_end;
5575 #ifdef FEAT_VISUAL
5576 old_visual = VIsual;
5577 old_visual_mode = VIsual_mode;
5578 #endif
5579 clear_oparg(&oa);
5580 oa.regname = (cbd == &clip_plus ? '+' : '*');
5581 oa.op_type = OP_YANK;
5582 vim_memset(&ca, 0, sizeof(ca));
5583 ca.oap = &oa;
5584 ca.cmdchar = 'y';
5585 ca.count1 = 1;
5586 ca.retval = CA_NO_ADJ_OP_END;
5587 do_pending_operator(&ca, 0, TRUE);
5588 y_previous = old_y_previous;
5589 y_current = old_y_current;
5590 curwin->w_cursor = old_cursor;
5591 changed_cline_bef_curs(); /* need to update w_virtcol et al */
5592 curwin->w_curswant = old_curswant;
5593 curwin->w_set_curswant = old_set_curswant;
5594 curbuf->b_op_start = old_op_start;
5595 curbuf->b_op_end = old_op_end;
5596 #ifdef FEAT_VISUAL
5597 VIsual = old_visual;
5598 VIsual_mode = old_visual_mode;
5599 #endif
5601 else
5603 clip_free_selection(cbd);
5605 /* Try to get selected text from another window */
5606 clip_gen_request_selection(cbd);
5610 /* Convert from the GUI selection string into the '*'/'+' register */
5611 void
5612 clip_yank_selection(type, str, len, cbd)
5613 int type;
5614 char_u *str;
5615 long len;
5616 VimClipboard *cbd;
5618 struct yankreg *y_ptr;
5620 if (cbd == &clip_plus)
5621 y_ptr = &y_regs[PLUS_REGISTER];
5622 else
5623 y_ptr = &y_regs[STAR_REGISTER];
5625 clip_free_selection(cbd);
5627 str_to_reg(y_ptr, type, str, len, 0L);
5631 * Convert the '*'/'+' register into a GUI selection string returned in *str
5632 * with length *len.
5633 * Returns the motion type, or -1 for failure.
5636 clip_convert_selection(str, len, cbd)
5637 char_u **str;
5638 long_u *len;
5639 VimClipboard *cbd;
5641 char_u *p;
5642 int lnum;
5643 int i, j;
5644 int_u eolsize;
5645 struct yankreg *y_ptr;
5647 if (cbd == &clip_plus)
5648 y_ptr = &y_regs[PLUS_REGISTER];
5649 else
5650 y_ptr = &y_regs[STAR_REGISTER];
5652 #ifdef USE_CRNL
5653 eolsize = 2;
5654 #else
5655 eolsize = 1;
5656 #endif
5658 *str = NULL;
5659 *len = 0;
5660 if (y_ptr->y_array == NULL)
5661 return -1;
5663 for (i = 0; i < y_ptr->y_size; i++)
5664 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5667 * Don't want newline character at end of last line if we're in MCHAR mode.
5669 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5670 *len -= eolsize;
5672 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5673 if (p == NULL)
5674 return -1;
5675 lnum = 0;
5676 for (i = 0, j = 0; i < (int)*len; i++, j++)
5678 if (y_ptr->y_array[lnum][j] == '\n')
5679 p[i] = NUL;
5680 else if (y_ptr->y_array[lnum][j] == NUL)
5682 #ifdef USE_CRNL
5683 p[i++] = '\r';
5684 #endif
5685 #ifdef USE_CR
5686 p[i] = '\r';
5687 #else
5688 p[i] = '\n';
5689 #endif
5690 lnum++;
5691 j = -1;
5693 else
5694 p[i] = y_ptr->y_array[lnum][j];
5696 return y_ptr->y_type;
5700 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5702 * If we have written to a clipboard register, send the text to the clipboard.
5704 static void
5705 may_set_selection()
5707 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5709 clip_own_selection(&clip_star);
5710 clip_gen_set_selection(&clip_star);
5712 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5714 clip_own_selection(&clip_plus);
5715 clip_gen_set_selection(&clip_plus);
5718 # endif
5720 #endif /* FEAT_CLIPBOARD || PROTO */
5723 #if defined(FEAT_DND) || defined(PROTO)
5725 * Replace the contents of the '~' register with str.
5727 void
5728 dnd_yank_drag_data(str, len)
5729 char_u *str;
5730 long len;
5732 struct yankreg *curr;
5734 curr = y_current;
5735 y_current = &y_regs[TILDE_REGISTER];
5736 free_yank_all();
5737 str_to_reg(y_current, MCHAR, str, len, 0L);
5738 y_current = curr;
5740 #endif
5743 #if defined(FEAT_EVAL) || defined(PROTO)
5745 * Return the type of a register.
5746 * Used for getregtype()
5747 * Returns MAUTO for error.
5749 char_u
5750 get_reg_type(regname, reglen)
5751 int regname;
5752 long *reglen;
5754 switch (regname)
5756 case '%': /* file name */
5757 case '#': /* alternate file name */
5758 case '=': /* expression */
5759 case ':': /* last command line */
5760 case '/': /* last search-pattern */
5761 case '.': /* last inserted text */
5762 #ifdef FEAT_SEARCHPATH
5763 case Ctrl_F: /* Filename under cursor */
5764 case Ctrl_P: /* Path under cursor, expand via "path" */
5765 #endif
5766 case Ctrl_W: /* word under cursor */
5767 case Ctrl_A: /* WORD (mnemonic All) under cursor */
5768 case '_': /* black hole: always empty */
5769 return MCHAR;
5772 #ifdef FEAT_CLIPBOARD
5773 regname = may_get_selection(regname);
5774 #endif
5776 /* Should we check for a valid name? */
5777 get_yank_register(regname, FALSE);
5779 if (y_current->y_array != NULL)
5781 #ifdef FEAT_VISUAL
5782 if (reglen != NULL && y_current->y_type == MBLOCK)
5783 *reglen = y_current->y_width;
5784 #endif
5785 return y_current->y_type;
5787 return MAUTO;
5791 * Return the contents of a register as a single allocated string.
5792 * Used for "@r" in expressions and for getreg().
5793 * Returns NULL for error.
5795 char_u *
5796 get_reg_contents(regname, allowexpr, expr_src)
5797 int regname;
5798 int allowexpr; /* allow "=" register */
5799 int expr_src; /* get expression for "=" register */
5801 long i;
5802 char_u *retval;
5803 int allocated;
5804 long len;
5806 /* Don't allow using an expression register inside an expression */
5807 if (regname == '=')
5809 if (allowexpr)
5811 if (expr_src)
5812 return get_expr_line_src();
5813 return get_expr_line();
5815 return NULL;
5818 if (regname == '@') /* "@@" is used for unnamed register */
5819 regname = '"';
5821 /* check for valid regname */
5822 if (regname != NUL && !valid_yank_reg(regname, FALSE))
5823 return NULL;
5825 #ifdef FEAT_CLIPBOARD
5826 regname = may_get_selection(regname);
5827 #endif
5829 if (get_spec_reg(regname, &retval, &allocated, FALSE))
5831 if (retval == NULL)
5832 return NULL;
5833 if (!allocated)
5834 retval = vim_strsave(retval);
5835 return retval;
5838 get_yank_register(regname, FALSE);
5839 if (y_current->y_array == NULL)
5840 return NULL;
5843 * Compute length of resulting string.
5845 len = 0;
5846 for (i = 0; i < y_current->y_size; ++i)
5848 len += (long)STRLEN(y_current->y_array[i]);
5850 * Insert a newline between lines and after last line if
5851 * y_type is MLINE.
5853 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5854 ++len;
5857 retval = lalloc(len + 1, TRUE);
5860 * Copy the lines of the yank register into the string.
5862 if (retval != NULL)
5864 len = 0;
5865 for (i = 0; i < y_current->y_size; ++i)
5867 STRCPY(retval + len, y_current->y_array[i]);
5868 len += (long)STRLEN(retval + len);
5871 * Insert a NL between lines and after the last line if y_type is
5872 * MLINE.
5874 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5875 retval[len++] = '\n';
5877 retval[len] = NUL;
5880 return retval;
5884 * Store string "str" in register "name".
5885 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5886 * If "must_append" is TRUE, always append to the register. Otherwise append
5887 * if "name" is an uppercase letter.
5888 * Note: "maxlen" and "must_append" don't work for the "/" register.
5889 * Careful: 'str' is modified, you may have to use a copy!
5890 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5892 void
5893 write_reg_contents(name, str, maxlen, must_append)
5894 int name;
5895 char_u *str;
5896 int maxlen;
5897 int must_append;
5899 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5902 void
5903 write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5904 int name;
5905 char_u *str;
5906 int maxlen;
5907 int must_append;
5908 int yank_type;
5909 long block_len;
5911 struct yankreg *old_y_previous, *old_y_current;
5912 long len;
5914 if (maxlen >= 0)
5915 len = maxlen;
5916 else
5917 len = (long)STRLEN(str);
5919 /* Special case: '/' search pattern */
5920 if (name == '/')
5922 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5923 return;
5926 #ifdef FEAT_EVAL
5927 if (name == '=')
5929 char_u *p, *s;
5931 p = vim_strnsave(str, (int)len);
5932 if (p == NULL)
5933 return;
5934 if (must_append)
5936 s = concat_str(get_expr_line_src(), p);
5937 vim_free(p);
5938 p = s;
5941 set_expr_line(p);
5942 return;
5944 #endif
5946 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
5948 emsg_invreg(name);
5949 return;
5952 if (name == '_') /* black hole: nothing to do */
5953 return;
5955 /* Don't want to change the current (unnamed) register */
5956 old_y_previous = y_previous;
5957 old_y_current = y_current;
5959 get_yank_register(name, TRUE);
5960 if (!y_append && !must_append)
5961 free_yank_all();
5962 #ifndef FEAT_VISUAL
5963 /* Just in case - make sure we don't use MBLOCK */
5964 if (yank_type == MBLOCK)
5965 yank_type = MAUTO;
5966 #endif
5967 if (yank_type == MAUTO)
5968 yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5969 ? MLINE : MCHAR);
5970 str_to_reg(y_current, yank_type, str, len, block_len);
5972 # ifdef FEAT_CLIPBOARD
5973 /* Send text of clipboard register to the clipboard. */
5974 may_set_selection();
5975 # endif
5977 /* ':let @" = "val"' should change the meaning of the "" register */
5978 if (name != '"')
5979 y_previous = old_y_previous;
5980 y_current = old_y_current;
5982 #endif /* FEAT_EVAL */
5984 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5986 * Put a string into a register. When the register is not empty, the string
5987 * is appended.
5989 static void
5990 str_to_reg(y_ptr, type, str, len, blocklen)
5991 struct yankreg *y_ptr; /* pointer to yank register */
5992 int type; /* MCHAR, MLINE or MBLOCK */
5993 char_u *str; /* string to put in register */
5994 long len; /* length of string */
5995 long blocklen; /* width of Visual block */
5997 int lnum;
5998 long start;
5999 long i;
6000 int extra;
6001 int newlines; /* number of lines added */
6002 int extraline = 0; /* extra line at the end */
6003 int append = FALSE; /* append to last line in register */
6004 char_u *s;
6005 char_u **pp;
6006 #ifdef FEAT_VISUAL
6007 long maxlen;
6008 #endif
6010 if (y_ptr->y_array == NULL) /* NULL means emtpy register */
6011 y_ptr->y_size = 0;
6014 * Count the number of lines within the string
6016 newlines = 0;
6017 for (i = 0; i < len; i++)
6018 if (str[i] == '\n')
6019 ++newlines;
6020 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6022 extraline = 1;
6023 ++newlines; /* count extra newline at the end */
6025 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6027 append = TRUE;
6028 --newlines; /* uncount newline when appending first line */
6032 * Allocate an array to hold the pointers to the new register lines.
6033 * If the register was not empty, move the existing lines to the new array.
6035 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6036 * sizeof(char_u *), TRUE);
6037 if (pp == NULL) /* out of memory */
6038 return;
6039 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6040 pp[lnum] = y_ptr->y_array[lnum];
6041 vim_free(y_ptr->y_array);
6042 y_ptr->y_array = pp;
6043 #ifdef FEAT_VISUAL
6044 maxlen = 0;
6045 #endif
6048 * Find the end of each line and save it into the array.
6050 for (start = 0; start < len + extraline; start += i + 1)
6052 for (i = start; i < len; ++i) /* find the end of the line */
6053 if (str[i] == '\n')
6054 break;
6055 i -= start; /* i is now length of line */
6056 #ifdef FEAT_VISUAL
6057 if (i > maxlen)
6058 maxlen = i;
6059 #endif
6060 if (append)
6062 --lnum;
6063 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6065 else
6066 extra = 0;
6067 s = alloc((unsigned)(i + extra + 1));
6068 if (s == NULL)
6069 break;
6070 if (extra)
6071 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6072 if (append)
6073 vim_free(y_ptr->y_array[lnum]);
6074 if (i)
6075 mch_memmove(s + extra, str + start, (size_t)i);
6076 extra += i;
6077 s[extra] = NUL;
6078 y_ptr->y_array[lnum++] = s;
6079 while (--extra >= 0)
6081 if (*s == NUL)
6082 *s = '\n'; /* replace NUL with newline */
6083 ++s;
6085 append = FALSE; /* only first line is appended */
6087 y_ptr->y_type = type;
6088 y_ptr->y_size = lnum;
6089 # ifdef FEAT_VISUAL
6090 if (type == MBLOCK)
6091 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6092 else
6093 y_ptr->y_width = 0;
6094 # endif
6096 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6098 void
6099 clear_oparg(oap)
6100 oparg_T *oap;
6102 vim_memset(oap, 0, sizeof(oparg_T));
6105 static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
6108 * Count the number of bytes, characters and "words" in a line.
6110 * "Words" are counted by looking for boundaries between non-space and
6111 * space characters. (it seems to produce results that match 'wc'.)
6113 * Return value is byte count; word count for the line is added to "*wc".
6114 * Char count is added to "*cc".
6116 * The function will only examine the first "limit" characters in the
6117 * line, stopping if it encounters an end-of-line (NUL byte). In that
6118 * case, eol_size will be added to the character count to account for
6119 * the size of the EOL character.
6121 static long
6122 line_count_info(line, wc, cc, limit, eol_size)
6123 char_u *line;
6124 long *wc;
6125 long *cc;
6126 long limit;
6127 int eol_size;
6129 long i;
6130 long words = 0;
6131 long chars = 0;
6132 int is_word = 0;
6134 for (i = 0; line[i] && i < limit; )
6136 if (is_word)
6138 if (vim_isspace(line[i]))
6140 words++;
6141 is_word = 0;
6144 else if (!vim_isspace(line[i]))
6145 is_word = 1;
6146 ++chars;
6147 #ifdef FEAT_MBYTE
6148 i += (*mb_ptr2len)(line + i);
6149 #else
6150 ++i;
6151 #endif
6154 if (is_word)
6155 words++;
6156 *wc += words;
6158 /* Add eol_size if the end of line was reached before hitting limit. */
6159 if (line[i] == NUL && i < limit)
6161 i += eol_size;
6162 chars += eol_size;
6164 *cc += chars;
6165 return i;
6169 * Give some info about the position of the cursor (for "g CTRL-G").
6170 * In Visual mode, give some info about the selected region. (In this case,
6171 * the *_count_cursor variables store running totals for the selection.)
6173 void
6174 cursor_pos_info()
6176 char_u *p;
6177 char_u buf1[50];
6178 char_u buf2[40];
6179 linenr_T lnum;
6180 long byte_count = 0;
6181 long byte_count_cursor = 0;
6182 long char_count = 0;
6183 long char_count_cursor = 0;
6184 long word_count = 0;
6185 long word_count_cursor = 0;
6186 int eol_size;
6187 long last_check = 100000L;
6188 #ifdef FEAT_VISUAL
6189 long line_count_selected = 0;
6190 pos_T min_pos, max_pos;
6191 oparg_T oparg;
6192 struct block_def bd;
6193 #endif
6196 * Compute the length of the file in characters.
6198 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6200 MSG(_(no_lines_msg));
6202 else
6204 if (get_fileformat(curbuf) == EOL_DOS)
6205 eol_size = 2;
6206 else
6207 eol_size = 1;
6209 #ifdef FEAT_VISUAL
6210 if (VIsual_active)
6212 if (lt(VIsual, curwin->w_cursor))
6214 min_pos = VIsual;
6215 max_pos = curwin->w_cursor;
6217 else
6219 min_pos = curwin->w_cursor;
6220 max_pos = VIsual;
6222 if (*p_sel == 'e' && max_pos.col > 0)
6223 --max_pos.col;
6225 if (VIsual_mode == Ctrl_V)
6227 oparg.is_VIsual = 1;
6228 oparg.block_mode = TRUE;
6229 oparg.op_type = OP_NOP;
6230 getvcols(curwin, &min_pos, &max_pos,
6231 &oparg.start_vcol, &oparg.end_vcol);
6232 if (curwin->w_curswant == MAXCOL)
6233 oparg.end_vcol = MAXCOL;
6234 /* Swap the start, end vcol if needed */
6235 if (oparg.end_vcol < oparg.start_vcol)
6237 oparg.end_vcol += oparg.start_vcol;
6238 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6239 oparg.end_vcol -= oparg.start_vcol;
6242 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6244 #endif
6246 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6248 /* Check for a CTRL-C every 100000 characters. */
6249 if (byte_count > last_check)
6251 ui_breakcheck();
6252 if (got_int)
6253 return;
6254 last_check = byte_count + 100000L;
6257 #ifdef FEAT_VISUAL
6258 /* Do extra processing for VIsual mode. */
6259 if (VIsual_active
6260 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6262 char_u *s = NULL;
6263 long len = 0L;
6265 switch (VIsual_mode)
6267 case Ctrl_V:
6268 # ifdef FEAT_VIRTUALEDIT
6269 virtual_op = virtual_active();
6270 # endif
6271 block_prep(&oparg, &bd, lnum, 0);
6272 # ifdef FEAT_VIRTUALEDIT
6273 virtual_op = MAYBE;
6274 # endif
6275 s = bd.textstart;
6276 len = (long)bd.textlen;
6277 break;
6278 case 'V':
6279 s = ml_get(lnum);
6280 len = MAXCOL;
6281 break;
6282 case 'v':
6284 colnr_T start_col = (lnum == min_pos.lnum)
6285 ? min_pos.col : 0;
6286 colnr_T end_col = (lnum == max_pos.lnum)
6287 ? max_pos.col - start_col + 1 : MAXCOL;
6289 s = ml_get(lnum) + start_col;
6290 len = end_col;
6292 break;
6294 if (s != NULL)
6296 byte_count_cursor += line_count_info(s, &word_count_cursor,
6297 &char_count_cursor, len, eol_size);
6298 if (lnum == curbuf->b_ml.ml_line_count
6299 && !curbuf->b_p_eol
6300 && curbuf->b_p_bin
6301 && (long)STRLEN(s) < len)
6302 byte_count_cursor -= eol_size;
6305 else
6306 #endif
6308 /* In non-visual mode, check for the line the cursor is on */
6309 if (lnum == curwin->w_cursor.lnum)
6311 word_count_cursor += word_count;
6312 char_count_cursor += char_count;
6313 byte_count_cursor = byte_count +
6314 line_count_info(ml_get(lnum),
6315 &word_count_cursor, &char_count_cursor,
6316 (long)(curwin->w_cursor.col + 1), eol_size);
6319 /* Add to the running totals */
6320 byte_count += line_count_info(ml_get(lnum), &word_count,
6321 &char_count, (long)MAXCOL, eol_size);
6324 /* Correction for when last line doesn't have an EOL. */
6325 if (!curbuf->b_p_eol && curbuf->b_p_bin)
6326 byte_count -= eol_size;
6328 #ifdef FEAT_VISUAL
6329 if (VIsual_active)
6331 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
6333 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
6334 &max_pos.col);
6335 sprintf((char *)buf1, _("%ld Cols; "),
6336 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6338 else
6339 buf1[0] = NUL;
6341 if (char_count_cursor == byte_count_cursor
6342 && char_count == byte_count)
6343 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
6344 buf1, line_count_selected,
6345 (long)curbuf->b_ml.ml_line_count,
6346 word_count_cursor, word_count,
6347 byte_count_cursor, byte_count);
6348 else
6349 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6350 buf1, line_count_selected,
6351 (long)curbuf->b_ml.ml_line_count,
6352 word_count_cursor, word_count,
6353 char_count_cursor, char_count,
6354 byte_count_cursor, byte_count);
6356 else
6357 #endif
6359 p = ml_get_curline();
6360 validate_virtcol();
6361 col_print(buf1, (int)curwin->w_cursor.col + 1,
6362 (int)curwin->w_virtcol + 1);
6363 col_print(buf2, (int)STRLEN(p), linetabsize(p));
6365 if (char_count_cursor == byte_count_cursor
6366 && char_count == byte_count)
6367 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
6368 (char *)buf1, (char *)buf2,
6369 (long)curwin->w_cursor.lnum,
6370 (long)curbuf->b_ml.ml_line_count,
6371 word_count_cursor, word_count,
6372 byte_count_cursor, byte_count);
6373 else
6374 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6375 (char *)buf1, (char *)buf2,
6376 (long)curwin->w_cursor.lnum,
6377 (long)curbuf->b_ml.ml_line_count,
6378 word_count_cursor, word_count,
6379 char_count_cursor, char_count,
6380 byte_count_cursor, byte_count);
6383 #ifdef FEAT_MBYTE
6384 byte_count = bomb_size();
6385 if (byte_count > 0)
6386 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
6387 byte_count);
6388 #endif
6389 /* Don't shorten this message, the user asked for it. */
6390 p = p_shm;
6391 p_shm = (char_u *)"";
6392 msg(IObuff);
6393 p_shm = p;