Merged from the latest developing branch.
[MacVim/KaoriYa.git] / src / ops.c
blob82107cc48a04b92353f31ae0ffd076a8cd174b37
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 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 and "reg".
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;
980 vim_free(reg);
982 # ifdef FEAT_CLIPBOARD
983 /* Send text written to clipboard register to the clipboard. */
984 may_set_selection();
985 # endif
987 #endif
989 #if defined(FEAT_MOUSE) || defined(PROTO)
991 * return TRUE if the current yank register has type MLINE
994 yank_register_mline(regname)
995 int regname;
997 if (regname != 0 && !valid_yank_reg(regname, FALSE))
998 return FALSE;
999 if (regname == '_') /* black hole is always empty */
1000 return FALSE;
1001 get_yank_register(regname, FALSE);
1002 return (y_current->y_type == MLINE);
1004 #endif
1007 * Start or stop recording into a yank register.
1009 * Return FAIL for failure, OK otherwise.
1012 do_record(c)
1013 int c;
1015 char_u *p;
1016 static int regname;
1017 struct yankreg *old_y_previous, *old_y_current;
1018 int retval;
1020 if (Recording == FALSE) /* start recording */
1022 /* registers 0-9, a-z and " are allowed */
1023 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1024 retval = FAIL;
1025 else
1027 Recording = TRUE;
1028 showmode();
1029 regname = c;
1030 retval = OK;
1033 else /* stop recording */
1036 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
1037 * needs to be removed again to put it in a register. exec_reg then
1038 * adds the escaping back later.
1040 Recording = FALSE;
1041 MSG("");
1042 p = get_recorded();
1043 if (p == NULL)
1044 retval = FAIL;
1045 else
1047 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1048 vim_unescape_csi(p);
1051 * We don't want to change the default register here, so save and
1052 * restore the current register name.
1054 old_y_previous = y_previous;
1055 old_y_current = y_current;
1057 retval = stuff_yank(regname, p);
1059 y_previous = old_y_previous;
1060 y_current = old_y_current;
1063 return retval;
1067 * Stuff string "p" into yank register "regname" as a single line (append if
1068 * uppercase). "p" must have been alloced.
1070 * return FAIL for failure, OK otherwise
1072 static int
1073 stuff_yank(regname, p)
1074 int regname;
1075 char_u *p;
1077 char_u *lp;
1078 char_u **pp;
1080 /* check for read-only register */
1081 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1083 vim_free(p);
1084 return FAIL;
1086 if (regname == '_') /* black hole: don't do anything */
1088 vim_free(p);
1089 return OK;
1091 get_yank_register(regname, TRUE);
1092 if (y_append && y_current->y_array != NULL)
1094 pp = &(y_current->y_array[y_current->y_size - 1]);
1095 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1096 if (lp == NULL)
1098 vim_free(p);
1099 return FAIL;
1101 STRCPY(lp, *pp);
1102 STRCAT(lp, p);
1103 vim_free(p);
1104 vim_free(*pp);
1105 *pp = lp;
1107 else
1109 free_yank_all();
1110 if ((y_current->y_array =
1111 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1113 vim_free(p);
1114 return FAIL;
1116 y_current->y_array[0] = p;
1117 y_current->y_size = 1;
1118 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1120 return OK;
1124 * execute a yank register: copy it into the stuff buffer
1126 * return FAIL for failure, OK otherwise
1129 do_execreg(regname, colon, addcr, silent)
1130 int regname;
1131 int colon; /* insert ':' before each line */
1132 int addcr; /* always add '\n' to end of line */
1133 int silent; /* set "silent" flag in typeahead buffer */
1135 static int lastc = NUL;
1136 long i;
1137 char_u *p;
1138 int retval = OK;
1139 int remap;
1141 if (regname == '@') /* repeat previous one */
1143 if (lastc == NUL)
1145 EMSG(_("E748: No previously used register"));
1146 return FAIL;
1148 regname = lastc;
1150 /* check for valid regname */
1151 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
1153 emsg_invreg(regname);
1154 return FAIL;
1156 lastc = regname;
1158 #ifdef FEAT_CLIPBOARD
1159 regname = may_get_selection(regname);
1160 #endif
1162 if (regname == '_') /* black hole: don't stuff anything */
1163 return OK;
1165 #ifdef FEAT_CMDHIST
1166 if (regname == ':') /* use last command line */
1168 if (last_cmdline == NULL)
1170 EMSG(_(e_nolastcmd));
1171 return FAIL;
1173 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1174 new_last_cmdline = NULL;
1175 /* Escape all control characters with a CTRL-V */
1176 p = vim_strsave_escaped_ext(last_cmdline,
1177 (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);
1178 if (p != NULL)
1180 /* When in Visual mode "'<,'>" will be prepended to the command.
1181 * Remove it when it's already there. */
1182 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
1183 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
1184 else
1185 retval = put_in_typebuf(p, TRUE, TRUE, silent);
1187 vim_free(p);
1189 #endif
1190 #ifdef FEAT_EVAL
1191 else if (regname == '=')
1193 p = get_expr_line();
1194 if (p == NULL)
1195 return FAIL;
1196 retval = put_in_typebuf(p, TRUE, colon, silent);
1197 vim_free(p);
1199 #endif
1200 else if (regname == '.') /* use last inserted text */
1202 p = get_last_insert_save();
1203 if (p == NULL)
1205 EMSG(_(e_noinstext));
1206 return FAIL;
1208 retval = put_in_typebuf(p, FALSE, colon, silent);
1209 vim_free(p);
1211 else
1213 get_yank_register(regname, FALSE);
1214 if (y_current->y_array == NULL)
1215 return FAIL;
1217 /* Disallow remaping for ":@r". */
1218 remap = colon ? REMAP_NONE : REMAP_YES;
1221 * Insert lines into typeahead buffer, from last one to first one.
1223 put_reedit_in_typebuf(silent);
1224 for (i = y_current->y_size; --i >= 0; )
1226 char_u *escaped;
1228 /* insert NL between lines and after last line if type is MLINE */
1229 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1230 || addcr)
1232 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
1233 return FAIL;
1235 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1236 if (escaped == NULL)
1237 return FAIL;
1238 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1239 vim_free(escaped);
1240 if (retval == FAIL)
1241 return FAIL;
1242 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
1243 == FAIL)
1244 return FAIL;
1246 Exec_reg = TRUE; /* disable the 'q' command */
1248 return retval;
1252 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1253 * used only after other typeahead has been processed.
1255 static void
1256 put_reedit_in_typebuf(silent)
1257 int silent;
1259 char_u buf[3];
1261 if (restart_edit != NUL)
1263 if (restart_edit == 'V')
1265 buf[0] = 'g';
1266 buf[1] = 'R';
1267 buf[2] = NUL;
1269 else
1271 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1272 buf[1] = NUL;
1274 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
1275 restart_edit = NUL;
1279 static int
1280 put_in_typebuf(s, esc, colon, silent)
1281 char_u *s;
1282 int esc; /* Escape CSI characters */
1283 int colon; /* add ':' before the line */
1284 int silent;
1286 int retval = OK;
1288 put_reedit_in_typebuf(silent);
1289 if (colon)
1290 retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, silent);
1291 if (retval == OK)
1293 char_u *p;
1295 if (esc)
1296 p = vim_strsave_escape_csi(s);
1297 else
1298 p = s;
1299 if (p == NULL)
1300 retval = FAIL;
1301 else
1302 retval = ins_typebuf(p, REMAP_YES, 0, TRUE, silent);
1303 if (esc)
1304 vim_free(p);
1306 if (colon && retval == OK)
1307 retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, silent);
1308 return retval;
1312 * Insert a yank register: copy it into the Read buffer.
1313 * Used by CTRL-R command and middle mouse button in insert mode.
1315 * return FAIL for failure, OK otherwise
1318 insert_reg(regname, literally)
1319 int regname;
1320 int literally; /* insert literally, not as if typed */
1322 long i;
1323 int retval = OK;
1324 char_u *arg;
1325 int allocated;
1328 * It is possible to get into an endless loop by having CTRL-R a in
1329 * register a and then, in insert mode, doing CTRL-R a.
1330 * If you hit CTRL-C, the loop will be broken here.
1332 ui_breakcheck();
1333 if (got_int)
1334 return FAIL;
1336 /* check for valid regname */
1337 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1338 return FAIL;
1340 #ifdef FEAT_CLIPBOARD
1341 regname = may_get_selection(regname);
1342 #endif
1344 if (regname == '.') /* insert last inserted text */
1345 retval = stuff_inserted(NUL, 1L, TRUE);
1346 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1348 if (arg == NULL)
1349 return FAIL;
1350 stuffescaped(arg, literally);
1351 if (allocated)
1352 vim_free(arg);
1354 else /* name or number register */
1356 get_yank_register(regname, FALSE);
1357 if (y_current->y_array == NULL)
1358 retval = FAIL;
1359 else
1361 for (i = 0; i < y_current->y_size; ++i)
1363 stuffescaped(y_current->y_array[i], literally);
1365 * Insert a newline between lines and after last line if
1366 * y_type is MLINE.
1368 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1369 stuffcharReadbuff('\n');
1374 return retval;
1378 * Stuff a string into the typeahead buffer, such that edit() will insert it
1379 * literally ("literally" TRUE) or interpret is as typed characters.
1381 static void
1382 stuffescaped(arg, literally)
1383 char_u *arg;
1384 int literally;
1386 int c;
1387 char_u *start;
1389 while (*arg != NUL)
1391 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1392 * stuff K_SPECIAL to get the effect of a special key when "literally"
1393 * is TRUE. */
1394 start = arg;
1395 while ((*arg >= ' '
1396 #ifndef EBCDIC
1397 && *arg < DEL /* EBCDIC: chars above space are normal */
1398 #endif
1400 || (*arg == K_SPECIAL && !literally))
1401 ++arg;
1402 if (arg > start)
1403 stuffReadbuffLen(start, (long)(arg - start));
1405 /* stuff a single special character */
1406 if (*arg != NUL)
1408 #ifdef FEAT_MBYTE
1409 if (has_mbyte)
1410 c = mb_ptr2char_adv(&arg);
1411 else
1412 #endif
1413 c = *arg++;
1414 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1415 stuffcharReadbuff(Ctrl_V);
1416 stuffcharReadbuff(c);
1422 * If "regname" is a special register, return TRUE and store a pointer to its
1423 * value in "argp".
1426 get_spec_reg(regname, argp, allocated, errmsg)
1427 int regname;
1428 char_u **argp;
1429 int *allocated; /* return: TRUE when value was allocated */
1430 int errmsg; /* give error message when failing */
1432 int cnt;
1434 *argp = NULL;
1435 *allocated = FALSE;
1436 switch (regname)
1438 case '%': /* file name */
1439 if (errmsg)
1440 check_fname(); /* will give emsg if not set */
1441 *argp = curbuf->b_fname;
1442 return TRUE;
1444 case '#': /* alternate file name */
1445 *argp = getaltfname(errmsg); /* may give emsg if not set */
1446 return TRUE;
1448 #ifdef FEAT_EVAL
1449 case '=': /* result of expression */
1450 *argp = get_expr_line();
1451 *allocated = TRUE;
1452 return TRUE;
1453 #endif
1455 case ':': /* last command line */
1456 if (last_cmdline == NULL && errmsg)
1457 EMSG(_(e_nolastcmd));
1458 *argp = last_cmdline;
1459 return TRUE;
1461 case '/': /* last search-pattern */
1462 if (last_search_pat() == NULL && errmsg)
1463 EMSG(_(e_noprevre));
1464 *argp = last_search_pat();
1465 return TRUE;
1467 case '.': /* last inserted text */
1468 *argp = get_last_insert_save();
1469 *allocated = TRUE;
1470 if (*argp == NULL && errmsg)
1471 EMSG(_(e_noinstext));
1472 return TRUE;
1474 #ifdef FEAT_SEARCHPATH
1475 case Ctrl_F: /* Filename under cursor */
1476 case Ctrl_P: /* Path under cursor, expand via "path" */
1477 if (!errmsg)
1478 return FALSE;
1479 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
1480 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
1481 *allocated = TRUE;
1482 return TRUE;
1483 #endif
1485 case Ctrl_W: /* word under cursor */
1486 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1487 if (!errmsg)
1488 return FALSE;
1489 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1490 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1491 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1492 *allocated = TRUE;
1493 return TRUE;
1495 case '_': /* black hole: always empty */
1496 *argp = (char_u *)"";
1497 return TRUE;
1500 return FALSE;
1504 * Paste a yank register into the command line.
1505 * Only for non-special registers.
1506 * Used by CTRL-R command in command-line mode
1507 * insert_reg() can't be used here, because special characters from the
1508 * register contents will be interpreted as commands.
1510 * return FAIL for failure, OK otherwise
1513 cmdline_paste_reg(regname, literally, remcr)
1514 int regname;
1515 int literally; /* Insert text literally instead of "as typed" */
1516 int remcr; /* don't add trailing CR */
1518 long i;
1520 get_yank_register(regname, FALSE);
1521 if (y_current->y_array == NULL)
1522 return FAIL;
1524 for (i = 0; i < y_current->y_size; ++i)
1526 cmdline_paste_str(y_current->y_array[i], literally);
1528 /* Insert ^M between lines and after last line if type is MLINE.
1529 * Don't do this when "remcr" is TRUE and the next line is empty. */
1530 if (y_current->y_type == MLINE
1531 || (i < y_current->y_size - 1
1532 && !(remcr
1533 && i == y_current->y_size - 2
1534 && *y_current->y_array[i + 1] == NUL)))
1535 cmdline_paste_str((char_u *)"\r", literally);
1537 /* Check for CTRL-C, in case someone tries to paste a few thousand
1538 * lines and gets bored. */
1539 ui_breakcheck();
1540 if (got_int)
1541 return FAIL;
1543 return OK;
1546 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
1548 * Adjust the register name pointed to with "rp" for the clipboard being
1549 * used always and the clipboard being available.
1551 void
1552 adjust_clip_reg(rp)
1553 int *rp;
1555 /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1556 if (*rp == 0 && clip_unnamed)
1557 *rp = '*';
1558 if (!clip_star.available && *rp == '*')
1559 *rp = 0;
1560 if (!clip_plus.available && *rp == '+')
1561 *rp = 0;
1563 #endif
1566 * op_delete - handle a delete operation
1568 * return FAIL if undo failed, OK otherwise.
1571 op_delete(oap)
1572 oparg_T *oap;
1574 int n;
1575 linenr_T lnum;
1576 char_u *ptr;
1577 #ifdef FEAT_VISUAL
1578 char_u *newp, *oldp;
1579 struct block_def bd;
1580 #endif
1581 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1582 int did_yank = FALSE;
1584 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1585 return OK;
1587 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1588 if (oap->empty)
1589 return u_save_cursor();
1591 if (!curbuf->b_p_ma)
1593 EMSG(_(e_modifiable));
1594 return FAIL;
1597 #ifdef FEAT_CLIPBOARD
1598 adjust_clip_reg(&oap->regname);
1599 #endif
1601 #ifdef FEAT_MBYTE
1602 if (has_mbyte)
1603 mb_adjust_opend(oap);
1604 #endif
1607 * Imitate the strange Vi behaviour: If the delete spans more than one line
1608 * and motion_type == MCHAR and the result is a blank line, make the delete
1609 * linewise. Don't do this for the change command or Visual mode.
1611 if ( oap->motion_type == MCHAR
1612 #ifdef FEAT_VISUAL
1613 && !oap->is_VIsual
1614 && !oap->block_mode
1615 #endif
1616 && oap->line_count > 1
1617 && oap->op_type == OP_DELETE)
1619 ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive;
1620 ptr = skipwhite(ptr);
1621 if (*ptr == NUL && inindent(0))
1622 oap->motion_type = MLINE;
1626 * Check for trying to delete (e.g. "D") in an empty line.
1627 * Note: For the change operator it is ok.
1629 if ( oap->motion_type == MCHAR
1630 && oap->line_count == 1
1631 && oap->op_type == OP_DELETE
1632 && *ml_get(oap->start.lnum) == NUL)
1635 * It's an error to operate on an empty region, when 'E' included in
1636 * 'cpoptions' (Vi compatible).
1638 #ifdef FEAT_VIRTUALEDIT
1639 if (virtual_op)
1640 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1641 * marks as if it happened. */
1642 goto setmarks;
1643 #endif
1644 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1645 beep_flush();
1646 return OK;
1650 * Do a yank of whatever we're about to delete.
1651 * If a yank register was specified, put the deleted text into that register.
1652 * For the black hole register '_' don't yank anything.
1654 if (oap->regname != '_')
1656 if (oap->regname != 0)
1658 /* check for read-only register */
1659 if (!valid_yank_reg(oap->regname, TRUE))
1661 beep_flush();
1662 return OK;
1664 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1665 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1666 did_yank = TRUE;
1670 * Put deleted text into register 1 and shift number registers if the
1671 * delete contains a line break, or when a regname has been specified.
1673 if (oap->regname != 0 || oap->motion_type == MLINE
1674 || oap->line_count > 1 || oap->use_reg_one)
1676 y_current = &y_regs[9];
1677 free_yank_all(); /* free register nine */
1678 for (n = 9; n > 1; --n)
1679 y_regs[n] = y_regs[n - 1];
1680 y_previous = y_current = &y_regs[1];
1681 y_regs[1].y_array = NULL; /* set register one to empty */
1682 if (op_yank(oap, TRUE, FALSE) == OK)
1683 did_yank = TRUE;
1686 /* Yank into small delete register when no register specified and the
1687 * delete is within one line. */
1688 if (oap->regname == 0 && oap->motion_type != MLINE
1689 && oap->line_count == 1)
1691 oap->regname = '-';
1692 get_yank_register(oap->regname, TRUE);
1693 if (op_yank(oap, TRUE, FALSE) == OK)
1694 did_yank = TRUE;
1695 oap->regname = 0;
1699 * If there's too much stuff to fit in the yank register, then get a
1700 * confirmation before doing the delete. This is crude, but simple.
1701 * And it avoids doing a delete of something we can't put back if we
1702 * want.
1704 if (!did_yank)
1706 int msg_silent_save = msg_silent;
1708 msg_silent = 0; /* must display the prompt */
1709 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1710 msg_silent = msg_silent_save;
1711 if (n != 'y')
1713 EMSG(_(e_abort));
1714 return FAIL;
1719 #ifdef FEAT_VISUAL
1721 * block mode delete
1723 if (oap->block_mode)
1725 if (u_save((linenr_T)(oap->start.lnum - 1),
1726 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1727 return FAIL;
1729 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1731 block_prep(oap, &bd, lnum, TRUE);
1732 if (bd.textlen == 0) /* nothing to delete */
1733 continue;
1735 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1736 if (lnum == curwin->w_cursor.lnum)
1738 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1739 # ifdef FEAT_VIRTUALEDIT
1740 curwin->w_cursor.coladd = 0;
1741 # endif
1744 /* n == number of chars deleted
1745 * If we delete a TAB, it may be replaced by several characters.
1746 * Thus the number of characters may increase!
1748 n = bd.textlen - bd.startspaces - bd.endspaces;
1749 oldp = ml_get(lnum);
1750 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1751 if (newp == NULL)
1752 continue;
1753 /* copy up to deleted part */
1754 mch_memmove(newp, oldp, (size_t)bd.textcol);
1755 /* insert spaces */
1756 copy_spaces(newp + bd.textcol,
1757 (size_t)(bd.startspaces + bd.endspaces));
1758 /* copy the part after the deleted part */
1759 oldp += bd.textcol + bd.textlen;
1760 mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
1761 oldp, STRLEN(oldp) + 1);
1762 /* replace the line */
1763 ml_replace(lnum, newp, FALSE);
1766 check_cursor_col();
1767 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1768 oap->end.lnum + 1, 0L);
1769 oap->line_count = 0; /* no lines deleted */
1771 else
1772 #endif
1773 if (oap->motion_type == MLINE)
1775 if (oap->op_type == OP_CHANGE)
1777 /* Delete the lines except the first one. Temporarily move the
1778 * cursor to the next line. Save the current line number, if the
1779 * last line is deleted it may be changed.
1781 if (oap->line_count > 1)
1783 lnum = curwin->w_cursor.lnum;
1784 ++curwin->w_cursor.lnum;
1785 del_lines((long)(oap->line_count - 1), TRUE);
1786 curwin->w_cursor.lnum = lnum;
1788 if (u_save_cursor() == FAIL)
1789 return FAIL;
1790 if (curbuf->b_p_ai) /* don't delete indent */
1792 beginline(BL_WHITE); /* cursor on first non-white */
1793 did_ai = TRUE; /* delete the indent when ESC hit */
1794 ai_col = curwin->w_cursor.col;
1796 else
1797 beginline(0); /* cursor in column 0 */
1798 truncate_line(FALSE); /* delete the rest of the line */
1799 /* leave cursor past last char in line */
1800 if (oap->line_count > 1)
1801 u_clearline(); /* "U" command not possible after "2cc" */
1803 else
1805 del_lines(oap->line_count, TRUE);
1806 beginline(BL_WHITE | BL_FIX);
1807 u_clearline(); /* "U" command not possible after "dd" */
1810 else
1812 #ifdef FEAT_VIRTUALEDIT
1813 if (virtual_op)
1815 int endcol = 0;
1817 /* For virtualedit: break the tabs that are partly included. */
1818 if (gchar_pos(&oap->start) == '\t')
1820 if (u_save_cursor() == FAIL) /* save first line for undo */
1821 return FAIL;
1822 if (oap->line_count == 1)
1823 endcol = getviscol2(oap->end.col, oap->end.coladd);
1824 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1825 oap->start = curwin->w_cursor;
1826 if (oap->line_count == 1)
1828 coladvance(endcol);
1829 oap->end.col = curwin->w_cursor.col;
1830 oap->end.coladd = curwin->w_cursor.coladd;
1831 curwin->w_cursor = oap->start;
1835 /* Break a tab only when it's included in the area. */
1836 if (gchar_pos(&oap->end) == '\t'
1837 && (int)oap->end.coladd < oap->inclusive)
1839 /* save last line for undo */
1840 if (u_save((linenr_T)(oap->end.lnum - 1),
1841 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1842 return FAIL;
1843 curwin->w_cursor = oap->end;
1844 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1845 oap->end = curwin->w_cursor;
1846 curwin->w_cursor = oap->start;
1849 #endif
1851 if (oap->line_count == 1) /* delete characters within one line */
1853 if (u_save_cursor() == FAIL) /* save line for undo */
1854 return FAIL;
1856 /* if 'cpoptions' contains '$', display '$' at end of change */
1857 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1858 && oap->op_type == OP_CHANGE
1859 && oap->end.lnum == curwin->w_cursor.lnum
1860 #ifdef FEAT_VISUAL
1861 && !oap->is_VIsual
1862 #endif
1864 display_dollar(oap->end.col - !oap->inclusive);
1866 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1868 #ifdef FEAT_VIRTUALEDIT
1869 if (virtual_op)
1871 /* fix up things for virtualedit-delete:
1872 * break the tabs which are going to get in our way
1874 char_u *curline = ml_get_curline();
1875 int len = (int)STRLEN(curline);
1877 if (oap->end.coladd != 0
1878 && (int)oap->end.col >= len - 1
1879 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1880 n++;
1881 /* Delete at least one char (e.g, when on a control char). */
1882 if (n == 0 && oap->start.coladd != oap->end.coladd)
1883 n = 1;
1885 /* When deleted a char in the line, reset coladd. */
1886 if (gchar_cursor() != NUL)
1887 curwin->w_cursor.coladd = 0;
1889 #endif
1890 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
1891 #ifdef FEAT_VISUAL
1892 && !oap->is_VIsual
1893 #endif
1896 else /* delete characters between lines */
1898 pos_T curpos;
1900 /* save deleted and changed lines for undo */
1901 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1902 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1903 return FAIL;
1905 truncate_line(TRUE); /* delete from cursor to end of line */
1907 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1908 ++curwin->w_cursor.lnum;
1909 del_lines((long)(oap->line_count - 2), FALSE);
1911 /* delete from start of line until op_end */
1912 curwin->w_cursor.col = 0;
1913 (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
1914 !virtual_op, oap->op_type == OP_DELETE
1915 #ifdef FEAT_VISUAL
1916 && !oap->is_VIsual
1917 #endif
1919 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1921 (void)do_join(FALSE);
1925 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1927 #ifdef FEAT_VIRTUALEDIT
1928 setmarks:
1929 #endif
1930 #ifdef FEAT_VISUAL
1931 if (oap->block_mode)
1933 curbuf->b_op_end.lnum = oap->end.lnum;
1934 curbuf->b_op_end.col = oap->start.col;
1936 else
1937 #endif
1938 curbuf->b_op_end = oap->start;
1939 curbuf->b_op_start = oap->start;
1941 return OK;
1944 #ifdef FEAT_MBYTE
1946 * Adjust end of operating area for ending on a multi-byte character.
1947 * Used for deletion.
1949 static void
1950 mb_adjust_opend(oap)
1951 oparg_T *oap;
1953 char_u *p;
1955 if (oap->inclusive)
1957 p = ml_get(oap->end.lnum);
1958 oap->end.col += mb_tail_off(p, p + oap->end.col);
1961 #endif
1963 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1965 * Replace a whole area with one character.
1968 op_replace(oap, c)
1969 oparg_T *oap;
1970 int c;
1972 int n, numc;
1973 #ifdef FEAT_MBYTE
1974 int num_chars;
1975 #endif
1976 char_u *newp, *oldp;
1977 size_t oldlen;
1978 struct block_def bd;
1980 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1981 return OK; /* nothing to do */
1983 #ifdef FEAT_MBYTE
1984 if (has_mbyte)
1985 mb_adjust_opend(oap);
1986 #endif
1988 if (u_save((linenr_T)(oap->start.lnum - 1),
1989 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1990 return FAIL;
1993 * block mode replace
1995 if (oap->block_mode)
1997 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1998 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
2000 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
2001 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
2002 continue; /* nothing to replace */
2004 /* n == number of extra chars required
2005 * If we split a TAB, it may be replaced by several characters.
2006 * Thus the number of characters may increase!
2008 #ifdef FEAT_VIRTUALEDIT
2009 /* If the range starts in virtual space, count the initial
2010 * coladd offset as part of "startspaces" */
2011 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2013 pos_T vpos;
2015 getvpos(&vpos, oap->start_vcol);
2016 bd.startspaces += vpos.coladd;
2017 n = bd.startspaces;
2019 else
2020 #endif
2021 /* allow for pre spaces */
2022 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2024 /* allow for post spp */
2025 n += (bd.endspaces
2026 #ifdef FEAT_VIRTUALEDIT
2027 && !bd.is_oneChar
2028 #endif
2029 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2030 /* Figure out how many characters to replace. */
2031 numc = oap->end_vcol - oap->start_vcol + 1;
2032 if (bd.is_short && (!virtual_op || bd.is_MAX))
2033 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2035 #ifdef FEAT_MBYTE
2036 /* A double-wide character can be replaced only up to half the
2037 * times. */
2038 if ((*mb_char2cells)(c) > 1)
2040 if ((numc & 1) && !bd.is_short)
2042 ++bd.endspaces;
2043 ++n;
2045 numc = numc / 2;
2048 /* Compute bytes needed, move character count to num_chars. */
2049 num_chars = numc;
2050 numc *= (*mb_char2len)(c);
2051 #endif
2052 /* oldlen includes textlen, so don't double count */
2053 n += numc - bd.textlen;
2055 oldp = ml_get_curline();
2056 oldlen = STRLEN(oldp);
2057 newp = alloc_check((unsigned)oldlen + 1 + n);
2058 if (newp == NULL)
2059 continue;
2060 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2061 /* copy up to deleted part */
2062 mch_memmove(newp, oldp, (size_t)bd.textcol);
2063 oldp += bd.textcol + bd.textlen;
2064 /* insert pre-spaces */
2065 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2066 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2067 #ifdef FEAT_MBYTE
2068 if (has_mbyte)
2070 n = (int)STRLEN(newp);
2071 while (--num_chars >= 0)
2072 n += (*mb_char2bytes)(c, newp + n);
2074 else
2075 #endif
2076 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2077 if (!bd.is_short)
2079 /* insert post-spaces */
2080 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2081 /* copy the part after the changed part */
2082 mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1);
2084 /* replace the line */
2085 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2088 else
2091 * MCHAR and MLINE motion replace.
2093 if (oap->motion_type == MLINE)
2095 oap->start.col = 0;
2096 curwin->w_cursor.col = 0;
2097 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2098 if (oap->end.col)
2099 --oap->end.col;
2101 else if (!oap->inclusive)
2102 dec(&(oap->end));
2104 while (ltoreq(curwin->w_cursor, oap->end))
2106 n = gchar_cursor();
2107 if (n != NUL)
2109 #ifdef FEAT_MBYTE
2110 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2112 /* This is slow, but it handles replacing a single-byte
2113 * with a multi-byte and the other way around. */
2114 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2115 n = State;
2116 State = REPLACE;
2117 ins_char(c);
2118 State = n;
2119 /* Backup to the replaced character. */
2120 dec_cursor();
2122 else
2123 #endif
2125 #ifdef FEAT_VIRTUALEDIT
2126 if (n == TAB)
2128 int end_vcol = 0;
2130 if (curwin->w_cursor.lnum == oap->end.lnum)
2132 /* oap->end has to be recalculated when
2133 * the tab breaks */
2134 end_vcol = getviscol2(oap->end.col,
2135 oap->end.coladd);
2137 coladvance_force(getviscol());
2138 if (curwin->w_cursor.lnum == oap->end.lnum)
2139 getvpos(&oap->end, end_vcol);
2141 #endif
2142 pchar(curwin->w_cursor, c);
2145 #ifdef FEAT_VIRTUALEDIT
2146 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2148 int virtcols = oap->end.coladd;
2150 if (curwin->w_cursor.lnum == oap->start.lnum
2151 && oap->start.col == oap->end.col && oap->start.coladd)
2152 virtcols -= oap->start.coladd;
2154 /* oap->end has been trimmed so it's effectively inclusive;
2155 * as a result an extra +1 must be counted so we don't
2156 * trample the NUL byte. */
2157 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2158 curwin->w_cursor.col -= (virtcols + 1);
2159 for (; virtcols >= 0; virtcols--)
2161 pchar(curwin->w_cursor, c);
2162 if (inc(&curwin->w_cursor) == -1)
2163 break;
2166 #endif
2168 /* Advance to next character, stop at the end of the file. */
2169 if (inc_cursor() == -1)
2170 break;
2174 curwin->w_cursor = oap->start;
2175 check_cursor();
2176 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2178 /* Set "'[" and "']" marks. */
2179 curbuf->b_op_start = oap->start;
2180 curbuf->b_op_end = oap->end;
2182 return OK;
2184 #endif
2187 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2189 void
2190 op_tilde(oap)
2191 oparg_T *oap;
2193 pos_T pos;
2194 #ifdef FEAT_VISUAL
2195 struct block_def bd;
2196 int todo;
2197 #endif
2198 int did_change = 0;
2200 if (u_save((linenr_T)(oap->start.lnum - 1),
2201 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2202 return;
2204 pos = oap->start;
2205 #ifdef FEAT_VISUAL
2206 if (oap->block_mode) /* Visual block mode */
2208 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2210 block_prep(oap, &bd, pos.lnum, FALSE);
2211 pos.col = bd.textcol;
2212 for (todo = bd.textlen; todo > 0; --todo)
2214 # ifdef FEAT_MBYTE
2215 if (has_mbyte)
2216 todo -= (*mb_ptr2len)(ml_get_pos(&pos)) - 1;
2217 # endif
2218 did_change |= swapchar(oap->op_type, &pos);
2219 if (inc(&pos) == -1) /* at end of file */
2220 break;
2222 # ifdef FEAT_NETBEANS_INTG
2223 if (usingNetbeans && did_change)
2225 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2227 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2228 (long)bd.textlen);
2229 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
2230 &ptr[bd.textcol], bd.textlen);
2232 # endif
2234 if (did_change)
2235 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2237 else /* not block mode */
2238 #endif
2240 if (oap->motion_type == MLINE)
2242 oap->start.col = 0;
2243 pos.col = 0;
2244 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2245 if (oap->end.col)
2246 --oap->end.col;
2248 else if (!oap->inclusive)
2249 dec(&(oap->end));
2251 while (ltoreq(pos, oap->end))
2253 did_change |= swapchar(oap->op_type, &pos);
2254 if (inc(&pos) == -1) /* at end of file */
2255 break;
2258 if (did_change)
2260 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2261 0L);
2262 #ifdef FEAT_NETBEANS_INTG
2263 if (usingNetbeans && did_change)
2265 char_u *ptr;
2266 int count;
2268 pos = oap->start;
2269 while (pos.lnum < oap->end.lnum)
2271 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2272 count = (int)STRLEN(ptr) - pos.col;
2273 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2274 netbeans_inserted(curbuf, pos.lnum, pos.col,
2275 &ptr[pos.col], count);
2276 pos.col = 0;
2277 pos.lnum++;
2279 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2280 count = oap->end.col - pos.col + 1;
2281 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
2282 netbeans_inserted(curbuf, pos.lnum, pos.col,
2283 &ptr[pos.col], count);
2285 #endif
2289 #ifdef FEAT_VISUAL
2290 if (!did_change && oap->is_VIsual)
2291 /* No change: need to remove the Visual selection */
2292 redraw_curbuf_later(INVERTED);
2293 #endif
2296 * Set '[ and '] marks.
2298 curbuf->b_op_start = oap->start;
2299 curbuf->b_op_end = oap->end;
2301 if (oap->line_count > p_report)
2303 if (oap->line_count == 1)
2304 MSG(_("1 line changed"));
2305 else
2306 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2311 * If op_type == OP_UPPER: make uppercase,
2312 * if op_type == OP_LOWER: make lowercase,
2313 * if op_type == OP_ROT13: do rot13 encoding,
2314 * else swap case of character at 'pos'
2315 * returns TRUE when something actually changed.
2318 swapchar(op_type, pos)
2319 int op_type;
2320 pos_T *pos;
2322 int c;
2323 int nc;
2325 c = gchar_pos(pos);
2327 /* Only do rot13 encoding for ASCII characters. */
2328 if (c >= 0x80 && op_type == OP_ROT13)
2329 return FALSE;
2331 #ifdef FEAT_MBYTE
2332 if (op_type == OP_UPPER && enc_latin1like && c == 0xdf)
2334 pos_T sp = curwin->w_cursor;
2336 /* Special handling of German sharp s: change to "SS". */
2337 curwin->w_cursor = *pos;
2338 del_char(FALSE);
2339 ins_char('S');
2340 ins_char('S');
2341 curwin->w_cursor = sp;
2342 inc(pos);
2345 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2346 return FALSE;
2347 #endif
2348 nc = c;
2349 if (MB_ISLOWER(c))
2351 if (op_type == OP_ROT13)
2352 nc = ROT13(c, 'a');
2353 else if (op_type != OP_LOWER)
2354 nc = MB_TOUPPER(c);
2356 else if (MB_ISUPPER(c))
2358 if (op_type == OP_ROT13)
2359 nc = ROT13(c, 'A');
2360 else if (op_type != OP_UPPER)
2361 nc = MB_TOLOWER(c);
2363 if (nc != c)
2365 #ifdef FEAT_MBYTE
2366 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2368 pos_T sp = curwin->w_cursor;
2370 curwin->w_cursor = *pos;
2371 del_char(FALSE);
2372 ins_char(nc);
2373 curwin->w_cursor = sp;
2375 else
2376 #endif
2377 pchar(*pos, nc);
2378 return TRUE;
2380 return FALSE;
2383 #if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2385 * op_insert - Insert and append operators for Visual mode.
2387 void
2388 op_insert(oap, count1)
2389 oparg_T *oap;
2390 long count1;
2392 long ins_len, pre_textlen = 0;
2393 char_u *firstline, *ins_text;
2394 struct block_def bd;
2395 int i;
2397 /* edit() changes this - record it for OP_APPEND */
2398 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2400 /* vis block is still marked. Get rid of it now. */
2401 curwin->w_cursor.lnum = oap->start.lnum;
2402 update_screen(INVERTED);
2404 if (oap->block_mode)
2406 #ifdef FEAT_VIRTUALEDIT
2407 /* When 'virtualedit' is used, need to insert the extra spaces before
2408 * doing block_prep(). When only "block" is used, virtual edit is
2409 * already disabled, but still need it when calling
2410 * coladvance_force(). */
2411 if (curwin->w_cursor.coladd > 0)
2413 int old_ve_flags = ve_flags;
2415 ve_flags = VE_ALL;
2416 if (u_save_cursor() == FAIL)
2417 return;
2418 coladvance_force(oap->op_type == OP_APPEND
2419 ? oap->end_vcol + 1 : getviscol());
2420 if (oap->op_type == OP_APPEND)
2421 --curwin->w_cursor.col;
2422 ve_flags = old_ve_flags;
2424 #endif
2425 /* Get the info about the block before entering the text */
2426 block_prep(oap, &bd, oap->start.lnum, TRUE);
2427 firstline = ml_get(oap->start.lnum) + bd.textcol;
2428 if (oap->op_type == OP_APPEND)
2429 firstline += bd.textlen;
2430 pre_textlen = (long)STRLEN(firstline);
2433 if (oap->op_type == OP_APPEND)
2435 if (oap->block_mode
2436 #ifdef FEAT_VIRTUALEDIT
2437 && curwin->w_cursor.coladd == 0
2438 #endif
2441 /* Move the cursor to the character right of the block. */
2442 curwin->w_set_curswant = TRUE;
2443 while (*ml_get_cursor() != NUL
2444 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2445 ++curwin->w_cursor.col;
2446 if (bd.is_short && !bd.is_MAX)
2448 /* First line was too short, make it longer and adjust the
2449 * values in "bd". */
2450 if (u_save_cursor() == FAIL)
2451 return;
2452 for (i = 0; i < bd.endspaces; ++i)
2453 ins_char(' ');
2454 bd.textlen += bd.endspaces;
2457 else
2459 curwin->w_cursor = oap->end;
2460 check_cursor_col();
2462 /* Works just like an 'i'nsert on the next character. */
2463 if (!lineempty(curwin->w_cursor.lnum)
2464 && oap->start_vcol != oap->end_vcol)
2465 inc_cursor();
2469 edit(NUL, FALSE, (linenr_T)count1);
2471 /* if user has moved off this line, we don't know what to do, so do
2472 * nothing */
2473 if (curwin->w_cursor.lnum != oap->start.lnum)
2474 return;
2476 if (oap->block_mode)
2478 struct block_def bd2;
2481 * Spaces and tabs in the indent may have changed to other spaces and
2482 * tabs. Get the starting column again and correct the length.
2483 * Don't do this when "$" used, end-of-line will have changed.
2485 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2486 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2488 if (oap->op_type == OP_APPEND)
2490 pre_textlen += bd2.textlen - bd.textlen;
2491 if (bd2.endspaces)
2492 --bd2.textlen;
2494 bd.textcol = bd2.textcol;
2495 bd.textlen = bd2.textlen;
2499 * Subsequent calls to ml_get() flush the firstline data - take a
2500 * copy of the required string.
2502 firstline = ml_get(oap->start.lnum) + bd.textcol;
2503 if (oap->op_type == OP_APPEND)
2504 firstline += bd.textlen;
2505 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2507 ins_text = vim_strnsave(firstline, (int)ins_len);
2508 if (ins_text != NULL)
2510 /* block handled here */
2511 if (u_save(oap->start.lnum,
2512 (linenr_T)(oap->end.lnum + 1)) == OK)
2513 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2514 &bd);
2516 curwin->w_cursor.col = oap->start.col;
2517 check_cursor();
2518 vim_free(ins_text);
2523 #endif
2526 * op_change - handle a change operation
2528 * return TRUE if edit() returns because of a CTRL-O command
2531 op_change(oap)
2532 oparg_T *oap;
2534 colnr_T l;
2535 int retval;
2536 #ifdef FEAT_VISUALEXTRA
2537 long offset;
2538 linenr_T linenr;
2539 long ins_len;
2540 long pre_textlen = 0;
2541 long pre_indent = 0;
2542 char_u *firstline;
2543 char_u *ins_text, *newp, *oldp;
2544 struct block_def bd;
2545 #endif
2547 l = oap->start.col;
2548 if (oap->motion_type == MLINE)
2550 l = 0;
2551 #ifdef FEAT_SMARTINDENT
2552 if (!p_paste && curbuf->b_p_si
2553 # ifdef FEAT_CINDENT
2554 && !curbuf->b_p_cin
2555 # endif
2557 can_si = TRUE; /* It's like opening a new line, do si */
2558 #endif
2561 /* First delete the text in the region. In an empty buffer only need to
2562 * save for undo */
2563 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2565 if (u_save_cursor() == FAIL)
2566 return FALSE;
2568 else if (op_delete(oap) == FAIL)
2569 return FALSE;
2571 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2572 && !virtual_op)
2573 inc_cursor();
2575 #ifdef FEAT_VISUALEXTRA
2576 /* check for still on same line (<CR> in inserted text meaningless) */
2577 /* skip blank lines too */
2578 if (oap->block_mode)
2580 # ifdef FEAT_VIRTUALEDIT
2581 /* Add spaces before getting the current line length. */
2582 if (virtual_op && (curwin->w_cursor.coladd > 0
2583 || gchar_cursor() == NUL))
2584 coladvance_force(getviscol());
2585 # endif
2586 firstline = ml_get(oap->start.lnum);
2587 pre_textlen = (long)STRLEN(firstline);
2588 pre_indent = (long)(skipwhite(firstline) - firstline);
2589 bd.textcol = curwin->w_cursor.col;
2591 #endif
2593 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2594 if (oap->motion_type == MLINE)
2595 fix_indent();
2596 #endif
2598 retval = edit(NUL, FALSE, (linenr_T)1);
2600 #ifdef FEAT_VISUALEXTRA
2602 * In Visual block mode, handle copying the new text to all lines of the
2603 * block.
2605 if (oap->block_mode && oap->start.lnum != oap->end.lnum)
2607 /* Auto-indenting may have changed the indent. If the cursor was past
2608 * the indent, exclude that indent change from the inserted text. */
2609 firstline = ml_get(oap->start.lnum);
2610 if (bd.textcol > (colnr_T)pre_indent)
2612 long new_indent = (long)(skipwhite(firstline) - firstline);
2614 pre_textlen += new_indent - pre_indent;
2615 bd.textcol += new_indent - pre_indent;
2618 ins_len = (long)STRLEN(firstline) - pre_textlen;
2619 if (ins_len > 0)
2621 /* Subsequent calls to ml_get() flush the firstline data - take a
2622 * copy of the inserted text. */
2623 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2625 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
2626 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2627 linenr++)
2629 block_prep(oap, &bd, linenr, TRUE);
2630 if (!bd.is_short || virtual_op)
2632 # ifdef FEAT_VIRTUALEDIT
2633 pos_T vpos;
2635 /* If the block starts in virtual space, count the
2636 * initial coladd offset as part of "startspaces" */
2637 if (bd.is_short)
2639 linenr_T lnum = curwin->w_cursor.lnum;
2641 curwin->w_cursor.lnum = linenr;
2642 (void)getvpos(&vpos, oap->start_vcol);
2643 curwin->w_cursor.lnum = lnum;
2645 else
2646 vpos.coladd = 0;
2647 # endif
2648 oldp = ml_get(linenr);
2649 newp = alloc_check((unsigned)(STRLEN(oldp)
2650 # ifdef FEAT_VIRTUALEDIT
2651 + vpos.coladd
2652 # endif
2653 + ins_len + 1));
2654 if (newp == NULL)
2655 continue;
2656 /* copy up to block start */
2657 mch_memmove(newp, oldp, (size_t)bd.textcol);
2658 offset = bd.textcol;
2659 # ifdef FEAT_VIRTUALEDIT
2660 copy_spaces(newp + offset, (size_t)vpos.coladd);
2661 offset += vpos.coladd;
2662 # endif
2663 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2664 offset += ins_len;
2665 oldp += bd.textcol;
2666 mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2667 ml_replace(linenr, newp, FALSE);
2670 check_cursor();
2672 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2674 vim_free(ins_text);
2677 #endif
2679 return retval;
2683 * set all the yank registers to empty (called from main())
2685 void
2686 init_yank()
2688 int i;
2690 for (i = 0; i < NUM_REGISTERS; ++i)
2691 y_regs[i].y_array = NULL;
2694 #if defined(EXITFREE) || defined(PROTO)
2695 void
2696 clear_registers()
2698 int i;
2700 for (i = 0; i < NUM_REGISTERS; ++i)
2702 y_current = &y_regs[i];
2703 if (y_current->y_array != NULL)
2704 free_yank_all();
2707 #endif
2710 * Free "n" lines from the current yank register.
2711 * Called for normal freeing and in case of error.
2713 static void
2714 free_yank(n)
2715 long n;
2717 if (y_current->y_array != NULL)
2719 long i;
2721 for (i = n; --i >= 0; )
2723 #ifdef AMIGA /* only for very slow machines */
2724 if ((i & 1023) == 1023) /* this may take a while */
2727 * This message should never cause a hit-return message.
2728 * Overwrite this message with any next message.
2730 ++no_wait_return;
2731 smsg((char_u *)_("freeing %ld lines"), i + 1);
2732 --no_wait_return;
2733 msg_didout = FALSE;
2734 msg_col = 0;
2736 #endif
2737 vim_free(y_current->y_array[i]);
2739 vim_free(y_current->y_array);
2740 y_current->y_array = NULL;
2741 #ifdef AMIGA
2742 if (n >= 1000)
2743 MSG("");
2744 #endif
2748 static void
2749 free_yank_all()
2751 free_yank(y_current->y_size);
2755 * Yank the text between "oap->start" and "oap->end" into a yank register.
2756 * If we are to append (uppercase register), we first yank into a new yank
2757 * register and then concatenate the old and the new one (so we keep the old
2758 * one in case of out-of-memory).
2760 * return FAIL for failure, OK otherwise
2763 op_yank(oap, deleting, mess)
2764 oparg_T *oap;
2765 int deleting;
2766 int mess;
2768 long y_idx; /* index in y_array[] */
2769 struct yankreg *curr; /* copy of y_current */
2770 struct yankreg newreg; /* new yank register when appending */
2771 char_u **new_ptr;
2772 linenr_T lnum; /* current line number */
2773 long j;
2774 int yanktype = oap->motion_type;
2775 long yanklines = oap->line_count;
2776 linenr_T yankendlnum = oap->end.lnum;
2777 char_u *p;
2778 char_u *pnew;
2779 struct block_def bd;
2781 /* check for read-only register */
2782 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2784 beep_flush();
2785 return FAIL;
2787 if (oap->regname == '_') /* black hole: nothing to do */
2788 return OK;
2790 #ifdef FEAT_CLIPBOARD
2791 if (!clip_star.available && oap->regname == '*')
2792 oap->regname = 0;
2793 else if (!clip_plus.available && oap->regname == '+')
2794 oap->regname = 0;
2795 #endif
2797 if (!deleting) /* op_delete() already set y_current */
2798 get_yank_register(oap->regname, TRUE);
2800 curr = y_current;
2801 /* append to existing contents */
2802 if (y_append && y_current->y_array != NULL)
2803 y_current = &newreg;
2804 else
2805 free_yank_all(); /* free previously yanked lines */
2808 * If the cursor was in column 1 before and after the movement, and the
2809 * operator is not inclusive, the yank is always linewise.
2811 if ( oap->motion_type == MCHAR
2812 && oap->start.col == 0
2813 && !oap->inclusive
2814 #ifdef FEAT_VISUAL
2815 && (!oap->is_VIsual || *p_sel == 'o')
2816 && !oap->block_mode
2817 #endif
2818 && oap->end.col == 0
2819 && yanklines > 1)
2821 yanktype = MLINE;
2822 --yankendlnum;
2823 --yanklines;
2826 y_current->y_size = yanklines;
2827 y_current->y_type = yanktype; /* set the yank register type */
2828 #ifdef FEAT_VISUAL
2829 y_current->y_width = 0;
2830 #endif
2831 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2832 yanklines), TRUE);
2834 if (y_current->y_array == NULL)
2836 y_current = curr;
2837 return FAIL;
2840 y_idx = 0;
2841 lnum = oap->start.lnum;
2843 #ifdef FEAT_VISUAL
2844 if (oap->block_mode)
2846 /* Visual block mode */
2847 y_current->y_type = MBLOCK; /* set the yank register type */
2848 y_current->y_width = oap->end_vcol - oap->start_vcol;
2850 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2851 y_current->y_width--;
2853 #endif
2855 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2857 switch (y_current->y_type)
2859 #ifdef FEAT_VISUAL
2860 case MBLOCK:
2861 block_prep(oap, &bd, lnum, FALSE);
2862 if (yank_copy_line(&bd, y_idx) == FAIL)
2863 goto fail;
2864 break;
2865 #endif
2867 case MLINE:
2868 if ((y_current->y_array[y_idx] =
2869 vim_strsave(ml_get(lnum))) == NULL)
2870 goto fail;
2871 break;
2873 case MCHAR:
2875 colnr_T startcol = 0, endcol = MAXCOL;
2876 #ifdef FEAT_VIRTUALEDIT
2877 int is_oneChar = FALSE;
2878 colnr_T cs, ce;
2879 #endif
2880 p = ml_get(lnum);
2881 bd.startspaces = 0;
2882 bd.endspaces = 0;
2884 if (lnum == oap->start.lnum)
2886 startcol = oap->start.col;
2887 #ifdef FEAT_VIRTUALEDIT
2888 if (virtual_op)
2890 getvcol(curwin, &oap->start, &cs, NULL, &ce);
2891 if (ce != cs && oap->start.coladd > 0)
2893 /* Part of a tab selected -- but don't
2894 * double-count it. */
2895 bd.startspaces = (ce - cs + 1)
2896 - oap->start.coladd;
2897 startcol++;
2900 #endif
2903 if (lnum == oap->end.lnum)
2905 endcol = oap->end.col;
2906 #ifdef FEAT_VIRTUALEDIT
2907 if (virtual_op)
2909 getvcol(curwin, &oap->end, &cs, NULL, &ce);
2910 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2911 # ifdef FEAT_MBYTE
2912 /* Don't add space for double-wide
2913 * char; endcol will be on last byte
2914 * of multi-byte char. */
2915 && (*mb_head_off)(p, p + endcol) == 0
2916 # endif
2919 if (oap->start.lnum == oap->end.lnum
2920 && oap->start.col == oap->end.col)
2922 /* Special case: inside a single char */
2923 is_oneChar = TRUE;
2924 bd.startspaces = oap->end.coladd
2925 - oap->start.coladd + oap->inclusive;
2926 endcol = startcol;
2928 else
2930 bd.endspaces = oap->end.coladd
2931 + oap->inclusive;
2932 endcol -= oap->inclusive;
2936 #endif
2938 if (startcol > endcol
2939 #ifdef FEAT_VIRTUALEDIT
2940 || is_oneChar
2941 #endif
2943 bd.textlen = 0;
2944 else
2946 if (endcol == MAXCOL)
2947 endcol = (colnr_T)STRLEN(p);
2948 bd.textlen = endcol - startcol + oap->inclusive;
2950 bd.textstart = p + startcol;
2951 if (yank_copy_line(&bd, y_idx) == FAIL)
2952 goto fail;
2953 break;
2955 /* NOTREACHED */
2959 if (curr != y_current) /* append the new block to the old block */
2961 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2962 (curr->y_size + y_current->y_size)), TRUE);
2963 if (new_ptr == NULL)
2964 goto fail;
2965 for (j = 0; j < curr->y_size; ++j)
2966 new_ptr[j] = curr->y_array[j];
2967 vim_free(curr->y_array);
2968 curr->y_array = new_ptr;
2970 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
2971 curr->y_type = MLINE;
2973 /* Concatenate the last line of the old block with the first line of
2974 * the new block, unless being Vi compatible. */
2975 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
2977 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2978 + STRLEN(y_current->y_array[0]) + 1), TRUE);
2979 if (pnew == NULL)
2981 y_idx = y_current->y_size - 1;
2982 goto fail;
2984 STRCPY(pnew, curr->y_array[--j]);
2985 STRCAT(pnew, y_current->y_array[0]);
2986 vim_free(curr->y_array[j]);
2987 vim_free(y_current->y_array[0]);
2988 curr->y_array[j++] = pnew;
2989 y_idx = 1;
2991 else
2992 y_idx = 0;
2993 while (y_idx < y_current->y_size)
2994 curr->y_array[j++] = y_current->y_array[y_idx++];
2995 curr->y_size = j;
2996 vim_free(y_current->y_array);
2997 y_current = curr;
2999 if (mess) /* Display message about yank? */
3001 if (yanktype == MCHAR
3002 #ifdef FEAT_VISUAL
3003 && !oap->block_mode
3004 #endif
3005 && yanklines == 1)
3006 yanklines = 0;
3007 /* Some versions of Vi use ">=" here, some don't... */
3008 if (yanklines > p_report)
3010 /* redisplay now, so message is not deleted */
3011 update_topline_redraw();
3012 if (yanklines == 1)
3014 #ifdef FEAT_VISUAL
3015 if (oap->block_mode)
3016 MSG(_("block of 1 line yanked"));
3017 else
3018 #endif
3019 MSG(_("1 line yanked"));
3021 #ifdef FEAT_VISUAL
3022 else if (oap->block_mode)
3023 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3024 #endif
3025 else
3026 smsg((char_u *)_("%ld lines yanked"), yanklines);
3031 * Set "'[" and "']" marks.
3033 curbuf->b_op_start = oap->start;
3034 curbuf->b_op_end = oap->end;
3035 if (yanktype == MLINE
3036 #ifdef FEAT_VISUAL
3037 && !oap->block_mode
3038 #endif
3041 curbuf->b_op_start.col = 0;
3042 curbuf->b_op_end.col = MAXCOL;
3045 #ifdef FEAT_CLIPBOARD
3047 * If we were yanking to the '*' register, send result to clipboard.
3048 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3049 * to the '*' register.
3051 if (clip_star.available
3052 && (curr == &(y_regs[STAR_REGISTER])
3053 || (!deleting && oap->regname == 0 && clip_unnamed)))
3055 if (curr != &(y_regs[STAR_REGISTER]))
3056 /* Copy the text from register 0 to the clipboard register. */
3057 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3059 clip_own_selection(&clip_star);
3060 clip_gen_set_selection(&clip_star);
3063 # ifdef FEAT_X11
3065 * If we were yanking to the '+' register, send result to selection.
3066 * Also copy to the '*' register, in case auto-select is off.
3068 else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
3070 /* No need to copy to * register upon 'unnamed' now - see below */
3071 clip_own_selection(&clip_plus);
3072 clip_gen_set_selection(&clip_plus);
3073 if (!clip_isautosel())
3075 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3076 clip_own_selection(&clip_star);
3077 clip_gen_set_selection(&clip_star);
3080 # endif
3081 #endif
3083 return OK;
3085 fail: /* free the allocated lines */
3086 free_yank(y_idx + 1);
3087 y_current = curr;
3088 return FAIL;
3091 static int
3092 yank_copy_line(bd, y_idx)
3093 struct block_def *bd;
3094 long y_idx;
3096 char_u *pnew;
3098 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3099 == NULL)
3100 return FAIL;
3101 y_current->y_array[y_idx] = pnew;
3102 copy_spaces(pnew, (size_t)bd->startspaces);
3103 pnew += bd->startspaces;
3104 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3105 pnew += bd->textlen;
3106 copy_spaces(pnew, (size_t)bd->endspaces);
3107 pnew += bd->endspaces;
3108 *pnew = NUL;
3109 return OK;
3112 #ifdef FEAT_CLIPBOARD
3114 * Make a copy of the y_current register to register "reg".
3116 static void
3117 copy_yank_reg(reg)
3118 struct yankreg *reg;
3120 struct yankreg *curr = y_current;
3121 long j;
3123 y_current = reg;
3124 free_yank_all();
3125 *y_current = *curr;
3126 y_current->y_array = (char_u **)lalloc_clear(
3127 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3128 if (y_current->y_array == NULL)
3129 y_current->y_size = 0;
3130 else
3131 for (j = 0; j < y_current->y_size; ++j)
3132 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3134 free_yank(j);
3135 y_current->y_size = 0;
3136 break;
3138 y_current = curr;
3140 #endif
3143 * Put contents of register "regname" into the text.
3144 * Caller must check "regname" to be valid!
3145 * "flags": PUT_FIXINDENT make indent look nice
3146 * PUT_CURSEND leave cursor after end of new text
3147 * PUT_LINE force linewise put (":put")
3149 void
3150 do_put(regname, dir, count, flags)
3151 int regname;
3152 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3153 long count;
3154 int flags;
3156 char_u *ptr;
3157 char_u *newp, *oldp;
3158 int yanklen;
3159 int totlen = 0; /* init for gcc */
3160 linenr_T lnum;
3161 colnr_T col;
3162 long i; /* index in y_array[] */
3163 int y_type;
3164 long y_size;
3165 #ifdef FEAT_VISUAL
3166 int oldlen;
3167 long y_width = 0;
3168 colnr_T vcol;
3169 int delcount;
3170 int incr = 0;
3171 long j;
3172 struct block_def bd;
3173 #endif
3174 char_u **y_array = NULL;
3175 long nr_lines = 0;
3176 pos_T new_cursor;
3177 int indent;
3178 int orig_indent = 0; /* init for gcc */
3179 int indent_diff = 0; /* init for gcc */
3180 int first_indent = TRUE;
3181 int lendiff = 0;
3182 pos_T old_pos;
3183 char_u *insert_string = NULL;
3184 int allocated = FALSE;
3185 long cnt;
3187 #ifdef FEAT_CLIPBOARD
3188 /* Adjust register name for "unnamed" in 'clipboard'. */
3189 adjust_clip_reg(&regname);
3190 (void)may_get_selection(regname);
3191 #endif
3193 if (flags & PUT_FIXINDENT)
3194 orig_indent = get_indent();
3196 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3197 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3200 * Using inserted text works differently, because the register includes
3201 * special characters (newlines, etc.).
3203 if (regname == '.')
3205 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3206 (count == -1 ? 'O' : 'i')), count, FALSE);
3207 /* Putting the text is done later, so can't really move the cursor to
3208 * the next character. Use "l" to simulate it. */
3209 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3210 stuffcharReadbuff('l');
3211 return;
3215 * For special registers '%' (file name), '#' (alternate file name) and
3216 * ':' (last command line), etc. we have to create a fake yank register.
3218 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3220 if (insert_string == NULL)
3221 return;
3224 if (insert_string != NULL)
3226 y_type = MCHAR;
3227 #ifdef FEAT_EVAL
3228 if (regname == '=')
3230 /* For the = register we need to split the string at NL
3231 * characters. */
3232 /* Loop twice: count the number of lines and save them. */
3233 for (;;)
3235 y_size = 0;
3236 ptr = insert_string;
3237 while (ptr != NULL)
3239 if (y_array != NULL)
3240 y_array[y_size] = ptr;
3241 ++y_size;
3242 ptr = vim_strchr(ptr, '\n');
3243 if (ptr != NULL)
3245 if (y_array != NULL)
3246 *ptr = NUL;
3247 ++ptr;
3248 /* A trailing '\n' makes the string linewise */
3249 if (*ptr == NUL)
3251 y_type = MLINE;
3252 break;
3256 if (y_array != NULL)
3257 break;
3258 y_array = (char_u **)alloc((unsigned)
3259 (y_size * sizeof(char_u *)));
3260 if (y_array == NULL)
3261 goto end;
3264 else
3265 #endif
3267 y_size = 1; /* use fake one-line yank register */
3268 y_array = &insert_string;
3271 else
3273 get_yank_register(regname, FALSE);
3275 y_type = y_current->y_type;
3276 #ifdef FEAT_VISUAL
3277 y_width = y_current->y_width;
3278 #endif
3279 y_size = y_current->y_size;
3280 y_array = y_current->y_array;
3283 #ifdef FEAT_VISUAL
3284 if (y_type == MLINE)
3286 if (flags & PUT_LINE_SPLIT)
3288 /* "p" or "P" in Visual mode: split the lines to put the text in
3289 * between. */
3290 if (u_save_cursor() == FAIL)
3291 goto end;
3292 ptr = vim_strsave(ml_get_cursor());
3293 if (ptr == NULL)
3294 goto end;
3295 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3296 vim_free(ptr);
3298 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3299 if (ptr == NULL)
3300 goto end;
3301 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3302 ++nr_lines;
3303 dir = FORWARD;
3305 if (flags & PUT_LINE_FORWARD)
3307 /* Must be "p" for a Visual block, put lines below the block. */
3308 curwin->w_cursor = curbuf->b_visual.vi_end;
3309 dir = FORWARD;
3311 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3312 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3314 #endif
3316 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3317 y_type = MLINE;
3319 if (y_size == 0 || y_array == NULL)
3321 EMSG2(_("E353: Nothing in register %s"),
3322 regname == 0 ? (char_u *)"\"" : transchar(regname));
3323 goto end;
3326 #ifdef FEAT_VISUAL
3327 if (y_type == MBLOCK)
3329 lnum = curwin->w_cursor.lnum + y_size + 1;
3330 if (lnum > curbuf->b_ml.ml_line_count)
3331 lnum = curbuf->b_ml.ml_line_count + 1;
3332 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3333 goto end;
3335 else
3336 #endif
3337 if (y_type == MLINE)
3339 lnum = curwin->w_cursor.lnum;
3340 #ifdef FEAT_FOLDING
3341 /* Correct line number for closed fold. Don't move the cursor yet,
3342 * u_save() uses it. */
3343 if (dir == BACKWARD)
3344 (void)hasFolding(lnum, &lnum, NULL);
3345 else
3346 (void)hasFolding(lnum, NULL, &lnum);
3347 #endif
3348 if (dir == FORWARD)
3349 ++lnum;
3350 if (u_save(lnum - 1, lnum) == FAIL)
3351 goto end;
3352 #ifdef FEAT_FOLDING
3353 if (dir == FORWARD)
3354 curwin->w_cursor.lnum = lnum - 1;
3355 else
3356 curwin->w_cursor.lnum = lnum;
3357 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3358 #endif
3360 else if (u_save_cursor() == FAIL)
3361 goto end;
3363 yanklen = (int)STRLEN(y_array[0]);
3365 #ifdef FEAT_VIRTUALEDIT
3366 if (ve_flags == VE_ALL && y_type == MCHAR)
3368 if (gchar_cursor() == TAB)
3370 /* Don't need to insert spaces when "p" on the last position of a
3371 * tab or "P" on the first position. */
3372 if (dir == FORWARD
3373 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3374 : curwin->w_cursor.coladd > 0)
3375 coladvance_force(getviscol());
3376 else
3377 curwin->w_cursor.coladd = 0;
3379 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3380 coladvance_force(getviscol() + (dir == FORWARD));
3382 #endif
3384 lnum = curwin->w_cursor.lnum;
3385 col = curwin->w_cursor.col;
3387 #ifdef FEAT_VISUAL
3389 * Block mode
3391 if (y_type == MBLOCK)
3393 char c = gchar_cursor();
3394 colnr_T endcol2 = 0;
3396 if (dir == FORWARD && c != NUL)
3398 #ifdef FEAT_VIRTUALEDIT
3399 if (ve_flags == VE_ALL)
3400 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3401 else
3402 #endif
3403 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3405 #ifdef FEAT_MBYTE
3406 if (has_mbyte)
3407 /* move to start of next multi-byte character */
3408 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
3409 else
3410 #endif
3411 #ifdef FEAT_VIRTUALEDIT
3412 if (c != TAB || ve_flags != VE_ALL)
3413 #endif
3414 ++curwin->w_cursor.col;
3415 ++col;
3417 else
3418 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3420 #ifdef FEAT_VIRTUALEDIT
3421 col += curwin->w_cursor.coladd;
3422 if (ve_flags == VE_ALL
3423 && (curwin->w_cursor.coladd > 0
3424 || endcol2 == curwin->w_cursor.col))
3426 if (dir == FORWARD && c == NUL)
3427 ++col;
3428 if (dir != FORWARD && c != NUL)
3429 ++curwin->w_cursor.col;
3430 if (c == TAB)
3432 if (dir == BACKWARD && curwin->w_cursor.col)
3433 curwin->w_cursor.col--;
3434 if (dir == FORWARD && col - 1 == endcol2)
3435 curwin->w_cursor.col++;
3438 curwin->w_cursor.coladd = 0;
3439 #endif
3440 bd.textcol = 0;
3441 for (i = 0; i < y_size; ++i)
3443 int spaces;
3444 char shortline;
3446 bd.startspaces = 0;
3447 bd.endspaces = 0;
3448 vcol = 0;
3449 delcount = 0;
3451 /* add a new line */
3452 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3454 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3455 (colnr_T)1, FALSE) == FAIL)
3456 break;
3457 ++nr_lines;
3459 /* get the old line and advance to the position to insert at */
3460 oldp = ml_get_curline();
3461 oldlen = (int)STRLEN(oldp);
3462 for (ptr = oldp; vcol < col && *ptr; )
3464 /* Count a tab for what it's worth (if list mode not on) */
3465 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3466 vcol += incr;
3468 bd.textcol = (colnr_T)(ptr - oldp);
3470 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3472 if (vcol < col) /* line too short, padd with spaces */
3473 bd.startspaces = col - vcol;
3474 else if (vcol > col)
3476 bd.endspaces = vcol - col;
3477 bd.startspaces = incr - bd.endspaces;
3478 --bd.textcol;
3479 delcount = 1;
3480 #ifdef FEAT_MBYTE
3481 if (has_mbyte)
3482 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3483 #endif
3484 if (oldp[bd.textcol] != TAB)
3486 /* Only a Tab can be split into spaces. Other
3487 * characters will have to be moved to after the
3488 * block, causing misalignment. */
3489 delcount = 0;
3490 bd.endspaces = 0;
3494 yanklen = (int)STRLEN(y_array[i]);
3496 /* calculate number of spaces required to fill right side of block*/
3497 spaces = y_width + 1;
3498 for (j = 0; j < yanklen; j++)
3499 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3500 if (spaces < 0)
3501 spaces = 0;
3503 /* insert the new text */
3504 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3505 newp = alloc_check((unsigned)totlen + oldlen + 1);
3506 if (newp == NULL)
3507 break;
3508 /* copy part up to cursor to new line */
3509 ptr = newp;
3510 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3511 ptr += bd.textcol;
3512 /* may insert some spaces before the new text */
3513 copy_spaces(ptr, (size_t)bd.startspaces);
3514 ptr += bd.startspaces;
3515 /* insert the new text */
3516 for (j = 0; j < count; ++j)
3518 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3519 ptr += yanklen;
3521 /* insert block's trailing spaces only if there's text behind */
3522 if ((j < count - 1 || !shortline) && spaces)
3524 copy_spaces(ptr, (size_t)spaces);
3525 ptr += spaces;
3528 /* may insert some spaces after the new text */
3529 copy_spaces(ptr, (size_t)bd.endspaces);
3530 ptr += bd.endspaces;
3531 /* move the text after the cursor to the end of the line. */
3532 mch_memmove(ptr, oldp + bd.textcol + delcount,
3533 (size_t)(oldlen - bd.textcol - delcount + 1));
3534 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3536 ++curwin->w_cursor.lnum;
3537 if (i == 0)
3538 curwin->w_cursor.col += bd.startspaces;
3541 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3543 /* Set '[ mark. */
3544 curbuf->b_op_start = curwin->w_cursor;
3545 curbuf->b_op_start.lnum = lnum;
3547 /* adjust '] mark */
3548 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3549 curbuf->b_op_end.col = bd.textcol + totlen - 1;
3550 # ifdef FEAT_VIRTUALEDIT
3551 curbuf->b_op_end.coladd = 0;
3552 # endif
3553 if (flags & PUT_CURSEND)
3555 colnr_T len;
3557 curwin->w_cursor = curbuf->b_op_end;
3558 curwin->w_cursor.col++;
3560 /* in Insert mode we might be after the NUL, correct for that */
3561 len = (colnr_T)STRLEN(ml_get_curline());
3562 if (curwin->w_cursor.col > len)
3563 curwin->w_cursor.col = len;
3565 else
3566 curwin->w_cursor.lnum = lnum;
3568 else
3569 #endif
3572 * Character or Line mode
3574 if (y_type == MCHAR)
3576 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3577 * char */
3578 if (dir == FORWARD && gchar_cursor() != NUL)
3580 #ifdef FEAT_MBYTE
3581 if (has_mbyte)
3583 int bytelen = (*mb_ptr2len)(ml_get_cursor());
3585 /* put it on the next of the multi-byte character. */
3586 col += bytelen;
3587 if (yanklen)
3589 curwin->w_cursor.col += bytelen;
3590 curbuf->b_op_end.col += bytelen;
3593 else
3594 #endif
3596 ++col;
3597 if (yanklen)
3599 ++curwin->w_cursor.col;
3600 ++curbuf->b_op_end.col;
3604 curbuf->b_op_start = curwin->w_cursor;
3607 * Line mode: BACKWARD is the same as FORWARD on the previous line
3609 else if (dir == BACKWARD)
3610 --lnum;
3611 new_cursor = curwin->w_cursor;
3614 * simple case: insert into current line
3616 if (y_type == MCHAR && y_size == 1)
3618 totlen = count * yanklen;
3619 if (totlen)
3621 oldp = ml_get(lnum);
3622 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3623 if (newp == NULL)
3624 goto end; /* alloc() will give error message */
3625 mch_memmove(newp, oldp, (size_t)col);
3626 ptr = newp + col;
3627 for (i = 0; i < count; ++i)
3629 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3630 ptr += yanklen;
3632 mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3633 ml_replace(lnum, newp, FALSE);
3634 /* Put cursor on last putted char. */
3635 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3637 curbuf->b_op_end = curwin->w_cursor;
3638 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3639 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3640 ++curwin->w_cursor.col;
3641 changed_bytes(lnum, col);
3643 else
3646 * Insert at least one line. When y_type is MCHAR, break the first
3647 * line in two.
3649 for (cnt = 1; cnt <= count; ++cnt)
3651 i = 0;
3652 if (y_type == MCHAR)
3655 * Split the current line in two at the insert position.
3656 * First insert y_array[size - 1] in front of second line.
3657 * Then append y_array[0] to first line.
3659 lnum = new_cursor.lnum;
3660 ptr = ml_get(lnum) + col;
3661 totlen = (int)STRLEN(y_array[y_size - 1]);
3662 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3663 if (newp == NULL)
3664 goto error;
3665 STRCPY(newp, y_array[y_size - 1]);
3666 STRCAT(newp, ptr);
3667 /* insert second line */
3668 ml_append(lnum, newp, (colnr_T)0, FALSE);
3669 vim_free(newp);
3671 oldp = ml_get(lnum);
3672 newp = alloc_check((unsigned)(col + yanklen + 1));
3673 if (newp == NULL)
3674 goto error;
3675 /* copy first part of line */
3676 mch_memmove(newp, oldp, (size_t)col);
3677 /* append to first line */
3678 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3679 ml_replace(lnum, newp, FALSE);
3681 curwin->w_cursor.lnum = lnum;
3682 i = 1;
3685 for (; i < y_size; ++i)
3687 if ((y_type != MCHAR || i < y_size - 1)
3688 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3689 == FAIL)
3690 goto error;
3691 lnum++;
3692 ++nr_lines;
3693 if (flags & PUT_FIXINDENT)
3695 old_pos = curwin->w_cursor;
3696 curwin->w_cursor.lnum = lnum;
3697 ptr = ml_get(lnum);
3698 if (cnt == count && i == y_size - 1)
3699 lendiff = (int)STRLEN(ptr);
3700 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3701 if (*ptr == '#' && preprocs_left())
3702 indent = 0; /* Leave # lines at start */
3703 else
3704 #endif
3705 if (*ptr == NUL)
3706 indent = 0; /* Ignore empty lines */
3707 else if (first_indent)
3709 indent_diff = orig_indent - get_indent();
3710 indent = orig_indent;
3711 first_indent = FALSE;
3713 else if ((indent = get_indent() + indent_diff) < 0)
3714 indent = 0;
3715 (void)set_indent(indent, 0);
3716 curwin->w_cursor = old_pos;
3717 /* remember how many chars were removed */
3718 if (cnt == count && i == y_size - 1)
3719 lendiff -= (int)STRLEN(ml_get(lnum));
3724 error:
3725 /* Adjust marks. */
3726 if (y_type == MLINE)
3728 curbuf->b_op_start.col = 0;
3729 if (dir == FORWARD)
3730 curbuf->b_op_start.lnum++;
3732 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3733 (linenr_T)MAXLNUM, nr_lines, 0L);
3735 /* note changed text for displaying and folding */
3736 if (y_type == MCHAR)
3737 changed_lines(curwin->w_cursor.lnum, col,
3738 curwin->w_cursor.lnum + 1, nr_lines);
3739 else
3740 changed_lines(curbuf->b_op_start.lnum, 0,
3741 curbuf->b_op_start.lnum, nr_lines);
3743 /* put '] mark at last inserted character */
3744 curbuf->b_op_end.lnum = lnum;
3745 /* correct length for change in indent */
3746 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3747 if (col > 1)
3748 curbuf->b_op_end.col = col - 1;
3749 else
3750 curbuf->b_op_end.col = 0;
3752 if (flags & PUT_CURSLINE)
3754 /* ":put": put cursor on last inserted line */
3755 curwin->w_cursor.lnum = lnum;
3756 beginline(BL_WHITE | BL_FIX);
3758 else if (flags & PUT_CURSEND)
3760 /* put cursor after inserted text */
3761 if (y_type == MLINE)
3763 if (lnum >= curbuf->b_ml.ml_line_count)
3764 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3765 else
3766 curwin->w_cursor.lnum = lnum + 1;
3767 curwin->w_cursor.col = 0;
3769 else
3771 curwin->w_cursor.lnum = lnum;
3772 curwin->w_cursor.col = col;
3775 else if (y_type == MLINE)
3777 /* put cursor on first non-blank in first inserted line */
3778 curwin->w_cursor.col = 0;
3779 if (dir == FORWARD)
3780 ++curwin->w_cursor.lnum;
3781 beginline(BL_WHITE | BL_FIX);
3783 else /* put cursor on first inserted character */
3784 curwin->w_cursor = new_cursor;
3788 msgmore(nr_lines);
3789 curwin->w_set_curswant = TRUE;
3791 end:
3792 if (allocated)
3793 vim_free(insert_string);
3794 if (regname == '=')
3795 vim_free(y_array);
3797 /* If the cursor is past the end of the line put it at the end. */
3798 adjust_cursor_eol();
3802 * When the cursor is on the NUL past the end of the line and it should not be
3803 * there move it left.
3805 void
3806 adjust_cursor_eol()
3808 if (curwin->w_cursor.col > 0
3809 && gchar_cursor() == NUL
3810 #ifdef FEAT_VIRTUALEDIT
3811 && (ve_flags & VE_ONEMORE) == 0
3812 #endif
3813 && !(restart_edit || (State & INSERT)))
3815 /* Put the cursor on the last character in the line. */
3816 dec_cursor();
3818 #ifdef FEAT_VIRTUALEDIT
3819 if (ve_flags == VE_ALL)
3821 colnr_T scol, ecol;
3823 /* Coladd is set to the width of the last character. */
3824 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3825 curwin->w_cursor.coladd = ecol - scol + 1;
3827 #endif
3831 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3833 * Return TRUE if lines starting with '#' should be left aligned.
3836 preprocs_left()
3838 return
3839 # ifdef FEAT_SMARTINDENT
3840 # ifdef FEAT_CINDENT
3841 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3842 # else
3843 curbuf->b_p_si
3844 # endif
3845 # endif
3846 # ifdef FEAT_CINDENT
3847 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3848 # endif
3851 #endif
3853 /* Return the character name of the register with the given number */
3855 get_register_name(num)
3856 int num;
3858 if (num == -1)
3859 return '"';
3860 else if (num < 10)
3861 return num + '0';
3862 else if (num == DELETION_REGISTER)
3863 return '-';
3864 #ifdef FEAT_CLIPBOARD
3865 else if (num == STAR_REGISTER)
3866 return '*';
3867 else if (num == PLUS_REGISTER)
3868 return '+';
3869 #endif
3870 else
3872 #ifdef EBCDIC
3873 int i;
3875 /* EBCDIC is really braindead ... */
3876 i = 'a' + (num - 10);
3877 if (i > 'i')
3878 i += 7;
3879 if (i > 'r')
3880 i += 8;
3881 return i;
3882 #else
3883 return num + 'a' - 10;
3884 #endif
3889 * ":dis" and ":registers": Display the contents of the yank registers.
3891 void
3892 ex_display(eap)
3893 exarg_T *eap;
3895 int i, n;
3896 long j;
3897 char_u *p;
3898 struct yankreg *yb;
3899 int name;
3900 int attr;
3901 char_u *arg = eap->arg;
3902 #ifdef FEAT_MBYTE
3903 int clen;
3904 #else
3905 # define clen 1
3906 #endif
3908 if (arg != NULL && *arg == NUL)
3909 arg = NULL;
3910 attr = hl_attr(HLF_8);
3912 /* Highlight title */
3913 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3914 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3916 name = get_register_name(i);
3917 if (arg != NULL && vim_strchr(arg, name) == NULL)
3918 continue; /* did not ask for this register */
3920 #ifdef FEAT_CLIPBOARD
3921 /* Adjust register name for "unnamed" in 'clipboard'.
3922 * When it's a clipboard register, fill it with the current contents
3923 * of the clipboard. */
3924 adjust_clip_reg(&name);
3925 (void)may_get_selection(name);
3926 #endif
3928 if (i == -1)
3930 if (y_previous != NULL)
3931 yb = y_previous;
3932 else
3933 yb = &(y_regs[0]);
3935 else
3936 yb = &(y_regs[i]);
3937 if (yb->y_array != NULL)
3939 msg_putchar('\n');
3940 msg_putchar('"');
3941 msg_putchar(name);
3942 MSG_PUTS(" ");
3944 n = (int)Columns - 6;
3945 for (j = 0; j < yb->y_size && n > 1; ++j)
3947 if (j)
3949 MSG_PUTS_ATTR("^J", attr);
3950 n -= 2;
3952 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3954 #ifdef FEAT_MBYTE
3955 clen = (*mb_ptr2len)(p);
3956 #endif
3957 msg_outtrans_len(p, clen);
3958 #ifdef FEAT_MBYTE
3959 p += clen - 1;
3960 #endif
3963 if (n > 1 && yb->y_type == MLINE)
3964 MSG_PUTS_ATTR("^J", attr);
3965 out_flush(); /* show one line at a time */
3967 ui_breakcheck();
3971 * display last inserted text
3973 if ((p = get_last_insert()) != NULL
3974 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3976 MSG_PUTS("\n\". ");
3977 dis_msg(p, TRUE);
3981 * display last command line
3983 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3984 && !got_int)
3986 MSG_PUTS("\n\": ");
3987 dis_msg(last_cmdline, FALSE);
3991 * display current file name
3993 if (curbuf->b_fname != NULL
3994 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3996 MSG_PUTS("\n\"% ");
3997 dis_msg(curbuf->b_fname, FALSE);
4001 * display alternate file name
4003 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4005 char_u *fname;
4006 linenr_T dummy;
4008 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4010 MSG_PUTS("\n\"# ");
4011 dis_msg(fname, FALSE);
4016 * display last search pattern
4018 if (last_search_pat() != NULL
4019 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4021 MSG_PUTS("\n\"/ ");
4022 dis_msg(last_search_pat(), FALSE);
4025 #ifdef FEAT_EVAL
4027 * display last used expression
4029 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4030 && !got_int)
4032 MSG_PUTS("\n\"= ");
4033 dis_msg(expr_line, FALSE);
4035 #endif
4039 * display a string for do_dis()
4040 * truncate at end of screen line
4042 static void
4043 dis_msg(p, skip_esc)
4044 char_u *p;
4045 int skip_esc; /* if TRUE, ignore trailing ESC */
4047 int n;
4048 #ifdef FEAT_MBYTE
4049 int l;
4050 #endif
4052 n = (int)Columns - 6;
4053 while (*p != NUL
4054 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4055 && (n -= ptr2cells(p)) >= 0)
4057 #ifdef FEAT_MBYTE
4058 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
4060 msg_outtrans_len(p, l);
4061 p += l;
4063 else
4064 #endif
4065 msg_outtrans_len(p++, 1);
4067 ui_breakcheck();
4071 * join 'count' lines (minimal 2), including u_save()
4073 void
4074 do_do_join(count, insert_space)
4075 long count;
4076 int insert_space;
4078 colnr_T col = MAXCOL;
4080 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4081 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4082 return;
4084 while (--count > 0)
4086 line_breakcheck();
4087 if (got_int || do_join(insert_space) == FAIL)
4089 beep_flush();
4090 break;
4092 if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4093 col = curwin->w_cursor.col;
4096 /* Vi compatible: use the column of the first join */
4097 if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4098 curwin->w_cursor.col = col;
4100 #if 0
4102 * Need to update the screen if the line where the cursor is became too
4103 * long to fit on the screen.
4105 update_topline_redraw();
4106 #endif
4110 * Join two lines at the cursor position.
4111 * "redraw" is TRUE when the screen should be updated.
4112 * Caller must have setup for undo.
4114 * return FAIL for failure, OK otherwise
4117 do_join(insert_space)
4118 int insert_space;
4120 char_u *curr;
4121 char_u *next, *next_start;
4122 char_u *newp;
4123 int endcurr1, endcurr2;
4124 int currsize; /* size of the current line */
4125 int nextsize; /* size of the next line */
4126 int spaces; /* number of spaces to insert */
4127 linenr_T t;
4129 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4130 return FAIL; /* can't join on last line */
4132 curr = ml_get_curline();
4133 currsize = (int)STRLEN(curr);
4134 endcurr1 = endcurr2 = NUL;
4135 if (insert_space && currsize > 0)
4137 #ifdef FEAT_MBYTE
4138 if (has_mbyte)
4140 next = curr + currsize;
4141 mb_ptr_back(curr, next);
4142 endcurr1 = (*mb_ptr2char)(next);
4143 if (next > curr)
4145 mb_ptr_back(curr, next);
4146 endcurr2 = (*mb_ptr2char)(next);
4149 else
4150 #endif
4152 endcurr1 = *(curr + currsize - 1);
4153 if (currsize > 1)
4154 endcurr2 = *(curr + currsize - 2);
4158 next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4159 spaces = 0;
4160 if (insert_space)
4162 next = skipwhite(next);
4163 if (*next != ')' && currsize != 0 && endcurr1 != TAB
4164 #ifdef FEAT_MBYTE
4165 && (!has_format_option(FO_MBYTE_JOIN)
4166 || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4167 && (!has_format_option(FO_MBYTE_JOIN2)
4168 || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4169 #endif
4172 /* don't add a space if the line is ending in a space */
4173 if (endcurr1 == ' ')
4174 endcurr1 = endcurr2;
4175 else
4176 ++spaces;
4177 /* extra space when 'joinspaces' set and line ends in '.' */
4178 if ( p_js
4179 && (endcurr1 == '.'
4180 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4181 && (endcurr1 == '?' || endcurr1 == '!'))))
4182 ++spaces;
4185 nextsize = (int)STRLEN(next);
4187 newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4188 if (newp == NULL)
4189 return FAIL;
4192 * Insert the next line first, because we already have that pointer.
4193 * Curr has to be obtained again, because getting next will have
4194 * invalidated it.
4196 mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4198 curr = ml_get_curline();
4199 mch_memmove(newp, curr, (size_t)currsize);
4201 copy_spaces(newp + currsize, (size_t)spaces);
4203 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4205 /* Only report the change in the first line here, del_lines() will report
4206 * the deleted line. */
4207 changed_lines(curwin->w_cursor.lnum, currsize,
4208 curwin->w_cursor.lnum + 1, 0L);
4211 * Delete the following line. To do this we move the cursor there
4212 * briefly, and then move it back. After del_lines() the cursor may
4213 * have moved up (last line deleted), so the current lnum is kept in t.
4215 * Move marks from the deleted line to the joined line, adjusting the
4216 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4217 * should not really be a problem.
4219 t = curwin->w_cursor.lnum;
4220 mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4221 (long)(currsize + spaces - (next - next_start)));
4222 ++curwin->w_cursor.lnum;
4223 del_lines(1L, FALSE);
4224 curwin->w_cursor.lnum = t;
4227 * go to first character of the joined line
4229 curwin->w_cursor.col = currsize;
4230 check_cursor_col();
4231 #ifdef FEAT_VIRTUALEDIT
4232 curwin->w_cursor.coladd = 0;
4233 #endif
4234 curwin->w_set_curswant = TRUE;
4236 return OK;
4239 #ifdef FEAT_COMMENTS
4241 * Return TRUE if the two comment leaders given are the same. "lnum" is
4242 * the first line. White-space is ignored. Note that the whole of
4243 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4245 static int
4246 same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4247 linenr_T lnum;
4248 int leader1_len;
4249 char_u *leader1_flags;
4250 int leader2_len;
4251 char_u *leader2_flags;
4253 int idx1 = 0, idx2 = 0;
4254 char_u *p;
4255 char_u *line1;
4256 char_u *line2;
4258 if (leader1_len == 0)
4259 return (leader2_len == 0);
4262 * If first leader has 'f' flag, the lines can be joined only if the
4263 * second line does not have a leader.
4264 * If first leader has 'e' flag, the lines can never be joined.
4265 * If fist leader has 's' flag, the lines can only be joined if there is
4266 * some text after it and the second line has the 'm' flag.
4268 if (leader1_flags != NULL)
4270 for (p = leader1_flags; *p && *p != ':'; ++p)
4272 if (*p == COM_FIRST)
4273 return (leader2_len == 0);
4274 if (*p == COM_END)
4275 return FALSE;
4276 if (*p == COM_START)
4278 if (*(ml_get(lnum) + leader1_len) == NUL)
4279 return FALSE;
4280 if (leader2_flags == NULL || leader2_len == 0)
4281 return FALSE;
4282 for (p = leader2_flags; *p && *p != ':'; ++p)
4283 if (*p == COM_MIDDLE)
4284 return TRUE;
4285 return FALSE;
4291 * Get current line and next line, compare the leaders.
4292 * The first line has to be saved, only one line can be locked at a time.
4294 line1 = vim_strsave(ml_get(lnum));
4295 if (line1 != NULL)
4297 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4299 line2 = ml_get(lnum + 1);
4300 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4302 if (!vim_iswhite(line2[idx2]))
4304 if (line1[idx1++] != line2[idx2])
4305 break;
4307 else
4308 while (vim_iswhite(line1[idx1]))
4309 ++idx1;
4311 vim_free(line1);
4313 return (idx2 == leader2_len && idx1 == leader1_len);
4315 #endif
4318 * implementation of the format operator 'gq'
4320 void
4321 op_format(oap, keep_cursor)
4322 oparg_T *oap;
4323 int keep_cursor; /* keep cursor on same text char */
4325 long old_line_count = curbuf->b_ml.ml_line_count;
4327 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4328 * can put it back there. */
4329 curwin->w_cursor = oap->cursor_start;
4331 if (u_save((linenr_T)(oap->start.lnum - 1),
4332 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4333 return;
4334 curwin->w_cursor = oap->start;
4336 #ifdef FEAT_VISUAL
4337 if (oap->is_VIsual)
4338 /* When there is no change: need to remove the Visual selection */
4339 redraw_curbuf_later(INVERTED);
4340 #endif
4342 /* Set '[ mark at the start of the formatted area */
4343 curbuf->b_op_start = oap->start;
4345 /* For "gw" remember the cursor position and put it back below (adjusted
4346 * for joined and split lines). */
4347 if (keep_cursor)
4348 saved_cursor = oap->cursor_start;
4350 format_lines(oap->line_count);
4353 * Leave the cursor at the first non-blank of the last formatted line.
4354 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4355 * line, so "." will do the next lines.
4357 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4358 ++curwin->w_cursor.lnum;
4359 beginline(BL_WHITE | BL_FIX);
4360 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4361 msgmore(old_line_count);
4363 /* put '] mark on the end of the formatted area */
4364 curbuf->b_op_end = curwin->w_cursor;
4366 if (keep_cursor)
4368 curwin->w_cursor = saved_cursor;
4369 saved_cursor.lnum = 0;
4372 #ifdef FEAT_VISUAL
4373 if (oap->is_VIsual)
4375 win_T *wp;
4377 FOR_ALL_WINDOWS(wp)
4379 if (wp->w_old_cursor_lnum != 0)
4381 /* When lines have been inserted or deleted, adjust the end of
4382 * the Visual area to be redrawn. */
4383 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4384 wp->w_old_cursor_lnum += old_line_count;
4385 else
4386 wp->w_old_visual_lnum += old_line_count;
4390 #endif
4393 #if defined(FEAT_EVAL) || defined(PROTO)
4395 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4397 void
4398 op_formatexpr(oap)
4399 oparg_T *oap;
4401 # ifdef FEAT_VISUAL
4402 if (oap->is_VIsual)
4403 /* When there is no change: need to remove the Visual selection */
4404 redraw_curbuf_later(INVERTED);
4405 # endif
4407 (void)fex_format(oap->start.lnum, oap->line_count, NUL);
4411 fex_format(lnum, count, c)
4412 linenr_T lnum;
4413 long count;
4414 int c; /* character to be inserted */
4416 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4417 OPT_LOCAL);
4418 int r;
4419 #ifdef FEAT_MBYTE
4420 char_u buf[MB_MAXBYTES];
4421 #else
4422 char_u buf[2];
4423 #endif
4426 * Set v:lnum to the first line number and v:count to the number of lines.
4427 * Set v:char to the character to be inserted (can be NUL).
4429 set_vim_var_nr(VV_LNUM, lnum);
4430 set_vim_var_nr(VV_COUNT, count);
4432 #ifdef FEAT_MBYTE
4433 if (has_mbyte)
4434 buf[(*mb_char2bytes)(c, buf)] = NUL;
4435 else
4436 #endif
4438 buf[0] = c;
4439 buf[1] = NUL;
4441 set_vim_var_string(VV_CHAR, buf, -1);
4444 * Evaluate the function.
4446 if (use_sandbox)
4447 ++sandbox;
4448 r = eval_to_number(curbuf->b_p_fex);
4449 if (use_sandbox)
4450 --sandbox;
4452 set_vim_var_string(VV_CHAR, NULL, -1);
4454 return r;
4456 #endif
4459 * Format "line_count" lines, starting at the cursor position.
4460 * When "line_count" is negative, format until the end of the paragraph.
4461 * Lines after the cursor line are saved for undo, caller must have saved the
4462 * first line.
4464 void
4465 format_lines(line_count)
4466 linenr_T line_count;
4468 int max_len;
4469 int is_not_par; /* current line not part of parag. */
4470 int next_is_not_par; /* next line not part of paragraph */
4471 int is_end_par; /* at end of paragraph */
4472 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4473 int next_is_start_par = FALSE;
4474 #ifdef FEAT_COMMENTS
4475 int leader_len = 0; /* leader len of current line */
4476 int next_leader_len; /* leader len of next line */
4477 char_u *leader_flags = NULL; /* flags for leader of current line */
4478 char_u *next_leader_flags; /* flags for leader of next line */
4479 int do_comments; /* format comments */
4480 #endif
4481 int advance = TRUE;
4482 int second_indent = -1;
4483 int do_second_indent;
4484 int do_number_indent;
4485 int do_trail_white;
4486 int first_par_line = TRUE;
4487 int smd_save;
4488 long count;
4489 int need_set_indent = TRUE; /* set indent of next paragraph */
4490 int force_format = FALSE;
4491 int old_State = State;
4493 /* length of a line to force formatting: 3 * 'tw' */
4494 max_len = comp_textwidth(TRUE) * 3;
4496 /* check for 'q', '2' and '1' in 'formatoptions' */
4497 #ifdef FEAT_COMMENTS
4498 do_comments = has_format_option(FO_Q_COMS);
4499 #endif
4500 do_second_indent = has_format_option(FO_Q_SECOND);
4501 do_number_indent = has_format_option(FO_Q_NUMBER);
4502 do_trail_white = has_format_option(FO_WHITE_PAR);
4505 * Get info about the previous and current line.
4507 if (curwin->w_cursor.lnum > 1)
4508 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4509 #ifdef FEAT_COMMENTS
4510 , &leader_len, &leader_flags, do_comments
4511 #endif
4513 else
4514 is_not_par = TRUE;
4515 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4516 #ifdef FEAT_COMMENTS
4517 , &next_leader_len, &next_leader_flags, do_comments
4518 #endif
4520 is_end_par = (is_not_par || next_is_not_par);
4521 if (!is_end_par && do_trail_white)
4522 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4524 curwin->w_cursor.lnum--;
4525 for (count = line_count; count != 0 && !got_int; --count)
4528 * Advance to next paragraph.
4530 if (advance)
4532 curwin->w_cursor.lnum++;
4533 prev_is_end_par = is_end_par;
4534 is_not_par = next_is_not_par;
4535 #ifdef FEAT_COMMENTS
4536 leader_len = next_leader_len;
4537 leader_flags = next_leader_flags;
4538 #endif
4542 * The last line to be formatted.
4544 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4546 next_is_not_par = TRUE;
4547 #ifdef FEAT_COMMENTS
4548 next_leader_len = 0;
4549 next_leader_flags = NULL;
4550 #endif
4552 else
4554 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4555 #ifdef FEAT_COMMENTS
4556 , &next_leader_len, &next_leader_flags, do_comments
4557 #endif
4559 if (do_number_indent)
4560 next_is_start_par =
4561 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4563 advance = TRUE;
4564 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4565 if (!is_end_par && do_trail_white)
4566 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4569 * Skip lines that are not in a paragraph.
4571 if (is_not_par)
4573 if (line_count < 0)
4574 break;
4576 else
4579 * For the first line of a paragraph, check indent of second line.
4580 * Don't do this for comments and empty lines.
4582 if (first_par_line
4583 && (do_second_indent || do_number_indent)
4584 && prev_is_end_par
4585 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4586 #ifdef FEAT_COMMENTS
4587 && leader_len == 0
4588 && next_leader_len == 0
4589 #endif
4592 if (do_second_indent
4593 && !lineempty(curwin->w_cursor.lnum + 1))
4594 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4595 else if (do_number_indent)
4596 second_indent = get_number_indent(curwin->w_cursor.lnum);
4600 * When the comment leader changes, it's the end of the paragraph.
4602 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4603 #ifdef FEAT_COMMENTS
4604 || !same_leader(curwin->w_cursor.lnum,
4605 leader_len, leader_flags,
4606 next_leader_len, next_leader_flags)
4607 #endif
4609 is_end_par = TRUE;
4612 * If we have got to the end of a paragraph, or the line is
4613 * getting long, format it.
4615 if (is_end_par || force_format)
4617 if (need_set_indent)
4618 /* replace indent in first line with minimal number of
4619 * tabs and spaces, according to current options */
4620 (void)set_indent(get_indent(), SIN_CHANGED);
4622 /* put cursor on last non-space */
4623 State = NORMAL; /* don't go past end-of-line */
4624 coladvance((colnr_T)MAXCOL);
4625 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4626 dec_cursor();
4628 /* do the formatting, without 'showmode' */
4629 State = INSERT; /* for open_line() */
4630 smd_save = p_smd;
4631 p_smd = FALSE;
4632 insertchar(NUL, INSCHAR_FORMAT
4633 #ifdef FEAT_COMMENTS
4634 + (do_comments ? INSCHAR_DO_COM : 0)
4635 #endif
4636 , second_indent);
4637 State = old_State;
4638 p_smd = smd_save;
4639 second_indent = -1;
4640 /* at end of par.: need to set indent of next par. */
4641 need_set_indent = is_end_par;
4642 if (is_end_par)
4644 /* When called with a negative line count, break at the
4645 * end of the paragraph. */
4646 if (line_count < 0)
4647 break;
4648 first_par_line = TRUE;
4650 force_format = FALSE;
4654 * When still in same paragraph, join the lines together. But
4655 * first delete the comment leader from the second line.
4657 if (!is_end_par)
4659 advance = FALSE;
4660 curwin->w_cursor.lnum++;
4661 curwin->w_cursor.col = 0;
4662 if (line_count < 0 && u_save_cursor() == FAIL)
4663 break;
4664 #ifdef FEAT_COMMENTS
4665 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
4666 if (next_leader_len > 0)
4667 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4668 (long)-next_leader_len);
4669 #endif
4670 curwin->w_cursor.lnum--;
4671 if (do_join(TRUE) == FAIL)
4673 beep_flush();
4674 break;
4676 first_par_line = FALSE;
4677 /* If the line is getting long, format it next time */
4678 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4679 force_format = TRUE;
4680 else
4681 force_format = FALSE;
4684 line_breakcheck();
4689 * Return TRUE if line "lnum" ends in a white character.
4691 static int
4692 ends_in_white(lnum)
4693 linenr_T lnum;
4695 char_u *s = ml_get(lnum);
4696 size_t l;
4698 if (*s == NUL)
4699 return FALSE;
4700 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4701 * invocation may call function multiple times". */
4702 l = STRLEN(s) - 1;
4703 return vim_iswhite(s[l]);
4707 * Blank lines, and lines containing only the comment leader, are left
4708 * untouched by the formatting. The function returns TRUE in this
4709 * case. It also returns TRUE when a line starts with the end of a comment
4710 * ('e' in comment flags), so that this line is skipped, and not joined to the
4711 * previous line. A new paragraph starts after a blank line, or when the
4712 * comment leader changes -- webb.
4714 #ifdef FEAT_COMMENTS
4715 static int
4716 fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4717 linenr_T lnum;
4718 int *leader_len;
4719 char_u **leader_flags;
4720 int do_comments;
4722 char_u *flags = NULL; /* init for GCC */
4723 char_u *ptr;
4725 ptr = ml_get(lnum);
4726 if (do_comments)
4727 *leader_len = get_leader_len(ptr, leader_flags, FALSE);
4728 else
4729 *leader_len = 0;
4731 if (*leader_len > 0)
4734 * Search for 'e' flag in comment leader flags.
4736 flags = *leader_flags;
4737 while (*flags && *flags != ':' && *flags != COM_END)
4738 ++flags;
4741 return (*skipwhite(ptr + *leader_len) == NUL
4742 || (*leader_len > 0 && *flags == COM_END)
4743 || startPS(lnum, NUL, FALSE));
4745 #else
4746 static int
4747 fmt_check_par(lnum)
4748 linenr_T lnum;
4750 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4752 #endif
4755 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4756 * previous line is in the same paragraph. Used for auto-formatting.
4759 paragraph_start(lnum)
4760 linenr_T lnum;
4762 char_u *p;
4763 #ifdef FEAT_COMMENTS
4764 int leader_len = 0; /* leader len of current line */
4765 char_u *leader_flags = NULL; /* flags for leader of current line */
4766 int next_leader_len; /* leader len of next line */
4767 char_u *next_leader_flags; /* flags for leader of next line */
4768 int do_comments; /* format comments */
4769 #endif
4771 if (lnum <= 1)
4772 return TRUE; /* start of the file */
4774 p = ml_get(lnum - 1);
4775 if (*p == NUL)
4776 return TRUE; /* after empty line */
4778 #ifdef FEAT_COMMENTS
4779 do_comments = has_format_option(FO_Q_COMS);
4780 #endif
4781 if (fmt_check_par(lnum - 1
4782 #ifdef FEAT_COMMENTS
4783 , &leader_len, &leader_flags, do_comments
4784 #endif
4786 return TRUE; /* after non-paragraph line */
4788 if (fmt_check_par(lnum
4789 #ifdef FEAT_COMMENTS
4790 , &next_leader_len, &next_leader_flags, do_comments
4791 #endif
4793 return TRUE; /* "lnum" is not a paragraph line */
4795 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4796 return TRUE; /* missing trailing space in previous line. */
4798 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4799 return TRUE; /* numbered item starts in "lnum". */
4801 #ifdef FEAT_COMMENTS
4802 if (!same_leader(lnum - 1, leader_len, leader_flags,
4803 next_leader_len, next_leader_flags))
4804 return TRUE; /* change of comment leader. */
4805 #endif
4807 return FALSE;
4810 #ifdef FEAT_VISUAL
4812 * prepare a few things for block mode yank/delete/tilde
4814 * for delete:
4815 * - textlen includes the first/last char to be (partly) deleted
4816 * - start/endspaces is the number of columns that are taken by the
4817 * first/last deleted char minus the number of columns that have to be
4818 * deleted. for yank and tilde:
4819 * - textlen includes the first/last char to be wholly yanked
4820 * - start/endspaces is the number of columns of the first/last yanked char
4821 * that are to be yanked.
4823 static void
4824 block_prep(oap, bdp, lnum, is_del)
4825 oparg_T *oap;
4826 struct block_def *bdp;
4827 linenr_T lnum;
4828 int is_del;
4830 int incr = 0;
4831 char_u *pend;
4832 char_u *pstart;
4833 char_u *line;
4834 char_u *prev_pstart;
4835 char_u *prev_pend;
4837 bdp->startspaces = 0;
4838 bdp->endspaces = 0;
4839 bdp->textlen = 0;
4840 bdp->start_vcol = 0;
4841 bdp->end_vcol = 0;
4842 #ifdef FEAT_VISUALEXTRA
4843 bdp->is_short = FALSE;
4844 bdp->is_oneChar = FALSE;
4845 bdp->pre_whitesp = 0;
4846 bdp->pre_whitesp_c = 0;
4847 bdp->end_char_vcols = 0;
4848 #endif
4849 bdp->start_char_vcols = 0;
4851 line = ml_get(lnum);
4852 pstart = line;
4853 prev_pstart = line;
4854 while (bdp->start_vcol < oap->start_vcol && *pstart)
4856 /* Count a tab for what it's worth (if list mode not on) */
4857 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4858 bdp->start_vcol += incr;
4859 #ifdef FEAT_VISUALEXTRA
4860 if (vim_iswhite(*pstart))
4862 bdp->pre_whitesp += incr;
4863 bdp->pre_whitesp_c++;
4865 else
4867 bdp->pre_whitesp = 0;
4868 bdp->pre_whitesp_c = 0;
4870 #endif
4871 prev_pstart = pstart;
4872 mb_ptr_adv(pstart);
4874 bdp->start_char_vcols = incr;
4875 if (bdp->start_vcol < oap->start_vcol) /* line too short */
4877 bdp->end_vcol = bdp->start_vcol;
4878 #ifdef FEAT_VISUALEXTRA
4879 bdp->is_short = TRUE;
4880 #endif
4881 if (!is_del || oap->op_type == OP_APPEND)
4882 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4884 else
4886 /* notice: this converts partly selected Multibyte characters to
4887 * spaces, too. */
4888 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4889 if (is_del && bdp->startspaces)
4890 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4891 pend = pstart;
4892 bdp->end_vcol = bdp->start_vcol;
4893 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
4895 #ifdef FEAT_VISUALEXTRA
4896 bdp->is_oneChar = TRUE;
4897 #endif
4898 if (oap->op_type == OP_INSERT)
4899 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4900 else if (oap->op_type == OP_APPEND)
4902 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4903 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4905 else
4907 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4908 if (is_del && oap->op_type != OP_LSHIFT)
4910 /* just putting the sum of those two into
4911 * bdp->startspaces doesn't work for Visual replace,
4912 * so we have to split the tab in two */
4913 bdp->startspaces = bdp->start_char_vcols
4914 - (bdp->start_vcol - oap->start_vcol);
4915 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4919 else
4921 prev_pend = pend;
4922 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4924 /* Count a tab for what it's worth (if list mode not on) */
4925 prev_pend = pend;
4926 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4927 bdp->end_vcol += incr;
4929 if (bdp->end_vcol <= oap->end_vcol
4930 && (!is_del
4931 || oap->op_type == OP_APPEND
4932 || oap->op_type == OP_REPLACE)) /* line too short */
4934 #ifdef FEAT_VISUALEXTRA
4935 bdp->is_short = TRUE;
4936 #endif
4937 /* Alternative: include spaces to fill up the block.
4938 * Disadvantage: can lead to trailing spaces when the line is
4939 * short where the text is put */
4940 /* if (!is_del || oap->op_type == OP_APPEND) */
4941 if (oap->op_type == OP_APPEND || virtual_op)
4942 bdp->endspaces = oap->end_vcol - bdp->end_vcol
4943 + oap->inclusive;
4944 else
4945 bdp->endspaces = 0; /* replace doesn't add characters */
4947 else if (bdp->end_vcol > oap->end_vcol)
4949 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4950 if (!is_del && bdp->endspaces)
4952 bdp->endspaces = incr - bdp->endspaces;
4953 if (pend != pstart)
4954 pend = prev_pend;
4958 #ifdef FEAT_VISUALEXTRA
4959 bdp->end_char_vcols = incr;
4960 #endif
4961 if (is_del && bdp->startspaces)
4962 pstart = prev_pstart;
4963 bdp->textlen = (int)(pend - pstart);
4965 bdp->textcol = (colnr_T) (pstart - line);
4966 bdp->textstart = pstart;
4968 #endif /* FEAT_VISUAL */
4970 #ifdef FEAT_RIGHTLEFT
4971 static void reverse_line __ARGS((char_u *s));
4973 static void
4974 reverse_line(s)
4975 char_u *s;
4977 int i, j;
4978 char_u c;
4980 if ((i = (int)STRLEN(s) - 1) <= 0)
4981 return;
4983 curwin->w_cursor.col = i - curwin->w_cursor.col;
4984 for (j = 0; j < i; j++, i--)
4986 c = s[i]; s[i] = s[j]; s[j] = c;
4990 # define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4991 #else
4992 # define RLADDSUBFIX(ptr)
4993 #endif
4996 * add or subtract 'Prenum1' from a number in a line
4997 * 'command' is CTRL-A for add, CTRL-X for subtract
4999 * return FAIL for failure, OK otherwise
5002 do_addsub(command, Prenum1)
5003 int command;
5004 linenr_T Prenum1;
5006 int col;
5007 char_u *buf1;
5008 char_u buf2[NUMBUFLEN];
5009 int hex; /* 'X' or 'x': hex; '0': octal */
5010 static int hexupper = FALSE; /* 0xABC */
5011 unsigned long n;
5012 long_u oldn;
5013 char_u *ptr;
5014 int c;
5015 int length = 0; /* character length of the number */
5016 int todel;
5017 int dohex;
5018 int dooct;
5019 int doalp;
5020 int firstdigit;
5021 int negative;
5022 int subtract;
5024 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5025 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5026 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5028 ptr = ml_get_curline();
5029 RLADDSUBFIX(ptr);
5032 * First check if we are on a hexadecimal number, after the "0x".
5034 col = curwin->w_cursor.col;
5035 if (dohex)
5036 while (col > 0 && vim_isxdigit(ptr[col]))
5037 --col;
5038 if ( dohex
5039 && col > 0
5040 && (ptr[col] == 'X'
5041 || ptr[col] == 'x')
5042 && ptr[col - 1] == '0'
5043 && vim_isxdigit(ptr[col + 1]))
5046 * Found hexadecimal number, move to its start.
5048 --col;
5050 else
5053 * Search forward and then backward to find the start of number.
5055 col = curwin->w_cursor.col;
5057 while (ptr[col] != NUL
5058 && !vim_isdigit(ptr[col])
5059 && !(doalp && ASCII_ISALPHA(ptr[col])))
5060 ++col;
5062 while (col > 0
5063 && vim_isdigit(ptr[col - 1])
5064 && !(doalp && ASCII_ISALPHA(ptr[col])))
5065 --col;
5069 * If a number was found, and saving for undo works, replace the number.
5071 firstdigit = ptr[col];
5072 RLADDSUBFIX(ptr);
5073 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5074 || u_save_cursor() != OK)
5076 beep_flush();
5077 return FAIL;
5080 /* get ptr again, because u_save() may have changed it */
5081 ptr = ml_get_curline();
5082 RLADDSUBFIX(ptr);
5084 if (doalp && ASCII_ISALPHA(firstdigit))
5086 /* decrement or increment alphabetic character */
5087 if (command == Ctrl_X)
5089 if (CharOrd(firstdigit) < Prenum1)
5091 if (isupper(firstdigit))
5092 firstdigit = 'A';
5093 else
5094 firstdigit = 'a';
5096 else
5097 #ifdef EBCDIC
5098 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5099 #else
5100 firstdigit -= Prenum1;
5101 #endif
5103 else
5105 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5107 if (isupper(firstdigit))
5108 firstdigit = 'Z';
5109 else
5110 firstdigit = 'z';
5112 else
5113 #ifdef EBCDIC
5114 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5115 #else
5116 firstdigit += Prenum1;
5117 #endif
5119 curwin->w_cursor.col = col;
5120 (void)del_char(FALSE);
5121 ins_char(firstdigit);
5123 else
5125 negative = FALSE;
5126 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5128 --col;
5129 negative = TRUE;
5132 /* get the number value (unsigned) */
5133 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5135 /* ignore leading '-' for hex and octal numbers */
5136 if (hex && negative)
5138 ++col;
5139 --length;
5140 negative = FALSE;
5143 /* add or subtract */
5144 subtract = FALSE;
5145 if (command == Ctrl_X)
5146 subtract ^= TRUE;
5147 if (negative)
5148 subtract ^= TRUE;
5150 oldn = n;
5151 if (subtract)
5152 n -= (unsigned long)Prenum1;
5153 else
5154 n += (unsigned long)Prenum1;
5156 /* handle wraparound for decimal numbers */
5157 if (!hex)
5159 if (subtract)
5161 if (n > oldn)
5163 n = 1 + (n ^ (unsigned long)-1);
5164 negative ^= TRUE;
5167 else /* add */
5169 if (n < oldn)
5171 n = (n ^ (unsigned long)-1);
5172 negative ^= TRUE;
5175 if (n == 0)
5176 negative = FALSE;
5180 * Delete the old number.
5182 curwin->w_cursor.col = col;
5183 todel = length;
5184 c = gchar_cursor();
5186 * Don't include the '-' in the length, only the length of the part
5187 * after it is kept the same.
5189 if (c == '-')
5190 --length;
5191 while (todel-- > 0)
5193 if (c < 0x100 && isalpha(c))
5195 if (isupper(c))
5196 hexupper = TRUE;
5197 else
5198 hexupper = FALSE;
5200 /* del_char() will mark line needing displaying */
5201 (void)del_char(FALSE);
5202 c = gchar_cursor();
5206 * Prepare the leading characters in buf1[].
5207 * When there are many leading zeros it could be very long. Allocate
5208 * a bit too much.
5210 buf1 = alloc((unsigned)length + NUMBUFLEN);
5211 if (buf1 == NULL)
5212 return FAIL;
5213 ptr = buf1;
5214 if (negative)
5216 *ptr++ = '-';
5218 if (hex)
5220 *ptr++ = '0';
5221 --length;
5223 if (hex == 'x' || hex == 'X')
5225 *ptr++ = hex;
5226 --length;
5230 * Put the number characters in buf2[].
5232 if (hex == 0)
5233 sprintf((char *)buf2, "%lu", n);
5234 else if (hex == '0')
5235 sprintf((char *)buf2, "%lo", n);
5236 else if (hex && hexupper)
5237 sprintf((char *)buf2, "%lX", n);
5238 else
5239 sprintf((char *)buf2, "%lx", n);
5240 length -= (int)STRLEN(buf2);
5243 * Adjust number of zeros to the new number of digits, so the
5244 * total length of the number remains the same.
5245 * Don't do this when
5246 * the result may look like an octal number.
5248 if (firstdigit == '0' && !(dooct && hex == 0))
5249 while (length-- > 0)
5250 *ptr++ = '0';
5251 *ptr = NUL;
5252 STRCAT(buf1, buf2);
5253 ins_str(buf1); /* insert the new number */
5254 vim_free(buf1);
5256 --curwin->w_cursor.col;
5257 curwin->w_set_curswant = TRUE;
5258 #ifdef FEAT_RIGHTLEFT
5259 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5260 RLADDSUBFIX(ptr);
5261 #endif
5262 return OK;
5265 #ifdef FEAT_VIMINFO
5267 read_viminfo_register(virp, force)
5268 vir_T *virp;
5269 int force;
5271 int eof;
5272 int do_it = TRUE;
5273 int size;
5274 int limit;
5275 int i;
5276 int set_prev = FALSE;
5277 char_u *str;
5278 char_u **array = NULL;
5280 /* We only get here (hopefully) if line[0] == '"' */
5281 str = virp->vir_line + 1;
5282 if (*str == '"')
5284 set_prev = TRUE;
5285 str++;
5287 if (!ASCII_ISALNUM(*str) && *str != '-')
5289 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5290 return TRUE; /* too many errors, pretend end-of-file */
5291 do_it = FALSE;
5293 get_yank_register(*str++, FALSE);
5294 if (!force && y_current->y_array != NULL)
5295 do_it = FALSE;
5296 size = 0;
5297 limit = 100; /* Optimized for registers containing <= 100 lines */
5298 if (do_it)
5300 if (set_prev)
5301 y_previous = y_current;
5302 vim_free(y_current->y_array);
5303 array = y_current->y_array =
5304 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5305 str = skipwhite(str);
5306 if (STRNCMP(str, "CHAR", 4) == 0)
5307 y_current->y_type = MCHAR;
5308 #ifdef FEAT_VISUAL
5309 else if (STRNCMP(str, "BLOCK", 5) == 0)
5310 y_current->y_type = MBLOCK;
5311 #endif
5312 else
5313 y_current->y_type = MLINE;
5314 /* get the block width; if it's missing we get a zero, which is OK */
5315 str = skipwhite(skiptowhite(str));
5316 #ifdef FEAT_VISUAL
5317 y_current->y_width = getdigits(&str);
5318 #else
5319 (void)getdigits(&str);
5320 #endif
5323 while (!(eof = viminfo_readline(virp))
5324 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5326 if (do_it)
5328 if (size >= limit)
5330 y_current->y_array = (char_u **)
5331 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5332 for (i = 0; i < limit; i++)
5333 y_current->y_array[i] = array[i];
5334 vim_free(array);
5335 limit *= 2;
5336 array = y_current->y_array;
5338 str = viminfo_readstring(virp, 1, TRUE);
5339 if (str != NULL)
5340 array[size++] = str;
5341 else
5342 do_it = FALSE;
5345 if (do_it)
5347 if (size == 0)
5349 vim_free(array);
5350 y_current->y_array = NULL;
5352 else if (size < limit)
5354 y_current->y_array =
5355 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5356 for (i = 0; i < size; i++)
5357 y_current->y_array[i] = array[i];
5358 vim_free(array);
5360 y_current->y_size = size;
5362 return eof;
5365 void
5366 write_viminfo_registers(fp)
5367 FILE *fp;
5369 int i, j;
5370 char_u *type;
5371 char_u c;
5372 int num_lines;
5373 int max_num_lines;
5374 int max_kbyte;
5375 long len;
5377 fprintf(fp, _("\n# Registers:\n"));
5379 /* Get '<' value, use old '"' value if '<' is not found. */
5380 max_num_lines = get_viminfo_parameter('<');
5381 if (max_num_lines < 0)
5382 max_num_lines = get_viminfo_parameter('"');
5383 if (max_num_lines == 0)
5384 return;
5385 max_kbyte = get_viminfo_parameter('s');
5386 if (max_kbyte == 0)
5387 return;
5388 for (i = 0; i < NUM_REGISTERS; i++)
5390 if (y_regs[i].y_array == NULL)
5391 continue;
5392 #ifdef FEAT_CLIPBOARD
5393 /* Skip '*'/'+' register, we don't want them back next time */
5394 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5395 continue;
5396 #endif
5397 #ifdef FEAT_DND
5398 /* Neither do we want the '~' register */
5399 if (i == TILDE_REGISTER)
5400 continue;
5401 #endif
5402 /* Skip empty registers. */
5403 num_lines = y_regs[i].y_size;
5404 if (num_lines == 0
5405 || (num_lines == 1 && y_regs[i].y_type == MCHAR
5406 && STRLEN(y_regs[i].y_array[0]) == 0))
5407 continue;
5409 if (max_kbyte > 0)
5411 /* Skip register if there is more text than the maximum size. */
5412 len = 0;
5413 for (j = 0; j < num_lines; j++)
5414 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
5415 if (len > (long)max_kbyte * 1024L)
5416 continue;
5419 switch (y_regs[i].y_type)
5421 case MLINE:
5422 type = (char_u *)"LINE";
5423 break;
5424 case MCHAR:
5425 type = (char_u *)"CHAR";
5426 break;
5427 #ifdef FEAT_VISUAL
5428 case MBLOCK:
5429 type = (char_u *)"BLOCK";
5430 break;
5431 #endif
5432 default:
5433 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
5434 y_regs[i].y_type);
5435 emsg(IObuff);
5436 type = (char_u *)"LINE";
5437 break;
5439 if (y_previous == &y_regs[i])
5440 fprintf(fp, "\"");
5441 c = get_register_name(i);
5442 fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5443 #ifdef FEAT_VISUAL
5444 (int)y_regs[i].y_width
5445 #else
5447 #endif
5450 /* If max_num_lines < 0, then we save ALL the lines in the register */
5451 if (max_num_lines > 0 && num_lines > max_num_lines)
5452 num_lines = max_num_lines;
5453 for (j = 0; j < num_lines; j++)
5455 putc('\t', fp);
5456 viminfo_writestring(fp, y_regs[i].y_array[j]);
5460 #endif /* FEAT_VIMINFO */
5462 #if defined(FEAT_CLIPBOARD) || defined(PROTO)
5464 * SELECTION / PRIMARY ('*')
5466 * Text selection stuff that uses the GUI selection register '*'. When using a
5467 * GUI this may be text from another window, otherwise it is the last text we
5468 * had highlighted with VIsual mode. With mouse support, clicking the middle
5469 * button performs the paste, otherwise you will need to do <"*p>. "
5470 * If not under X, it is synonymous with the clipboard register '+'.
5472 * X CLIPBOARD ('+')
5474 * Text selection stuff that uses the GUI clipboard register '+'.
5475 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5476 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5477 * otherwise you will need to do <"+p>. "
5478 * If not under X, it is synonymous with the selection register '*'.
5482 * Routine to export any final X selection we had to the environment
5483 * so that the text is still available after vim has exited. X selections
5484 * only exist while the owning application exists, so we write to the
5485 * permanent (while X runs) store CUT_BUFFER0.
5486 * Dump the CLIPBOARD selection if we own it (it's logically the more
5487 * 'permanent' of the two), otherwise the PRIMARY one.
5488 * For now, use a hard-coded sanity limit of 1Mb of data.
5490 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5491 void
5492 x11_export_final_selection()
5494 Display *dpy;
5495 char_u *str = NULL;
5496 long_u len = 0;
5497 int motion_type = -1;
5499 # ifdef FEAT_GUI
5500 if (gui.in_use)
5501 dpy = X_DISPLAY;
5502 else
5503 # endif
5504 # ifdef FEAT_XCLIPBOARD
5505 dpy = xterm_dpy;
5506 # else
5507 return;
5508 # endif
5510 /* Get selection to export */
5511 if (clip_plus.owned)
5512 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5513 else if (clip_star.owned)
5514 motion_type = clip_convert_selection(&str, &len, &clip_star);
5516 /* Check it's OK */
5517 if (dpy != NULL && str != NULL && motion_type >= 0
5518 && len < 1024*1024 && len > 0)
5520 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5521 XFlush(dpy);
5524 vim_free(str);
5526 #endif
5528 void
5529 clip_free_selection(cbd)
5530 VimClipboard *cbd;
5532 struct yankreg *y_ptr = y_current;
5534 if (cbd == &clip_plus)
5535 y_current = &y_regs[PLUS_REGISTER];
5536 else
5537 y_current = &y_regs[STAR_REGISTER];
5538 free_yank_all();
5539 y_current->y_size = 0;
5540 y_current = y_ptr;
5544 * Get the selected text and put it in the gui selection register '*' or '+'.
5546 void
5547 clip_get_selection(cbd)
5548 VimClipboard *cbd;
5550 struct yankreg *old_y_previous, *old_y_current;
5551 pos_T old_cursor;
5552 #ifdef FEAT_VISUAL
5553 pos_T old_visual;
5554 int old_visual_mode;
5555 #endif
5556 colnr_T old_curswant;
5557 int old_set_curswant;
5558 pos_T old_op_start, old_op_end;
5559 oparg_T oa;
5560 cmdarg_T ca;
5562 if (cbd->owned)
5564 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5565 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5566 return;
5568 /* Get the text between clip_star.start & clip_star.end */
5569 old_y_previous = y_previous;
5570 old_y_current = y_current;
5571 old_cursor = curwin->w_cursor;
5572 old_curswant = curwin->w_curswant;
5573 old_set_curswant = curwin->w_set_curswant;
5574 old_op_start = curbuf->b_op_start;
5575 old_op_end = curbuf->b_op_end;
5576 #ifdef FEAT_VISUAL
5577 old_visual = VIsual;
5578 old_visual_mode = VIsual_mode;
5579 #endif
5580 clear_oparg(&oa);
5581 oa.regname = (cbd == &clip_plus ? '+' : '*');
5582 oa.op_type = OP_YANK;
5583 vim_memset(&ca, 0, sizeof(ca));
5584 ca.oap = &oa;
5585 ca.cmdchar = 'y';
5586 ca.count1 = 1;
5587 ca.retval = CA_NO_ADJ_OP_END;
5588 do_pending_operator(&ca, 0, TRUE);
5589 y_previous = old_y_previous;
5590 y_current = old_y_current;
5591 curwin->w_cursor = old_cursor;
5592 changed_cline_bef_curs(); /* need to update w_virtcol et al */
5593 curwin->w_curswant = old_curswant;
5594 curwin->w_set_curswant = old_set_curswant;
5595 curbuf->b_op_start = old_op_start;
5596 curbuf->b_op_end = old_op_end;
5597 #ifdef FEAT_VISUAL
5598 VIsual = old_visual;
5599 VIsual_mode = old_visual_mode;
5600 #endif
5602 else
5604 clip_free_selection(cbd);
5606 /* Try to get selected text from another window */
5607 clip_gen_request_selection(cbd);
5611 /* Convert from the GUI selection string into the '*'/'+' register */
5612 void
5613 clip_yank_selection(type, str, len, cbd)
5614 int type;
5615 char_u *str;
5616 long len;
5617 VimClipboard *cbd;
5619 struct yankreg *y_ptr;
5621 if (cbd == &clip_plus)
5622 y_ptr = &y_regs[PLUS_REGISTER];
5623 else
5624 y_ptr = &y_regs[STAR_REGISTER];
5626 clip_free_selection(cbd);
5628 str_to_reg(y_ptr, type, str, len, 0L);
5632 * Convert the '*'/'+' register into a GUI selection string returned in *str
5633 * with length *len.
5634 * Returns the motion type, or -1 for failure.
5637 clip_convert_selection(str, len, cbd)
5638 char_u **str;
5639 long_u *len;
5640 VimClipboard *cbd;
5642 char_u *p;
5643 int lnum;
5644 int i, j;
5645 int_u eolsize;
5646 struct yankreg *y_ptr;
5648 if (cbd == &clip_plus)
5649 y_ptr = &y_regs[PLUS_REGISTER];
5650 else
5651 y_ptr = &y_regs[STAR_REGISTER];
5653 #ifdef USE_CRNL
5654 eolsize = 2;
5655 #else
5656 eolsize = 1;
5657 #endif
5659 *str = NULL;
5660 *len = 0;
5661 if (y_ptr->y_array == NULL)
5662 return -1;
5664 for (i = 0; i < y_ptr->y_size; i++)
5665 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5668 * Don't want newline character at end of last line if we're in MCHAR mode.
5670 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5671 *len -= eolsize;
5673 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5674 if (p == NULL)
5675 return -1;
5676 lnum = 0;
5677 for (i = 0, j = 0; i < (int)*len; i++, j++)
5679 if (y_ptr->y_array[lnum][j] == '\n')
5680 p[i] = NUL;
5681 else if (y_ptr->y_array[lnum][j] == NUL)
5683 #ifdef USE_CRNL
5684 p[i++] = '\r';
5685 #endif
5686 #ifdef USE_CR
5687 p[i] = '\r';
5688 #else
5689 p[i] = '\n';
5690 #endif
5691 lnum++;
5692 j = -1;
5694 else
5695 p[i] = y_ptr->y_array[lnum][j];
5697 return y_ptr->y_type;
5701 # if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5703 * If we have written to a clipboard register, send the text to the clipboard.
5705 static void
5706 may_set_selection()
5708 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5710 clip_own_selection(&clip_star);
5711 clip_gen_set_selection(&clip_star);
5713 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5715 clip_own_selection(&clip_plus);
5716 clip_gen_set_selection(&clip_plus);
5719 # endif
5721 #endif /* FEAT_CLIPBOARD || PROTO */
5724 #if defined(FEAT_DND) || defined(PROTO)
5726 * Replace the contents of the '~' register with str.
5728 void
5729 dnd_yank_drag_data(str, len)
5730 char_u *str;
5731 long len;
5733 struct yankreg *curr;
5735 curr = y_current;
5736 y_current = &y_regs[TILDE_REGISTER];
5737 free_yank_all();
5738 str_to_reg(y_current, MCHAR, str, len, 0L);
5739 y_current = curr;
5741 #endif
5744 #if defined(FEAT_EVAL) || defined(PROTO)
5746 * Return the type of a register.
5747 * Used for getregtype()
5748 * Returns MAUTO for error.
5750 char_u
5751 get_reg_type(regname, reglen)
5752 int regname;
5753 long *reglen;
5755 switch (regname)
5757 case '%': /* file name */
5758 case '#': /* alternate file name */
5759 case '=': /* expression */
5760 case ':': /* last command line */
5761 case '/': /* last search-pattern */
5762 case '.': /* last inserted text */
5763 #ifdef FEAT_SEARCHPATH
5764 case Ctrl_F: /* Filename under cursor */
5765 case Ctrl_P: /* Path under cursor, expand via "path" */
5766 #endif
5767 case Ctrl_W: /* word under cursor */
5768 case Ctrl_A: /* WORD (mnemonic All) under cursor */
5769 case '_': /* black hole: always empty */
5770 return MCHAR;
5773 #ifdef FEAT_CLIPBOARD
5774 regname = may_get_selection(regname);
5775 #endif
5777 /* Should we check for a valid name? */
5778 get_yank_register(regname, FALSE);
5780 if (y_current->y_array != NULL)
5782 #ifdef FEAT_VISUAL
5783 if (reglen != NULL && y_current->y_type == MBLOCK)
5784 *reglen = y_current->y_width;
5785 #endif
5786 return y_current->y_type;
5788 return MAUTO;
5792 * Return the contents of a register as a single allocated string.
5793 * Used for "@r" in expressions and for getreg().
5794 * Returns NULL for error.
5796 char_u *
5797 get_reg_contents(regname, allowexpr, expr_src)
5798 int regname;
5799 int allowexpr; /* allow "=" register */
5800 int expr_src; /* get expression for "=" register */
5802 long i;
5803 char_u *retval;
5804 int allocated;
5805 long len;
5807 /* Don't allow using an expression register inside an expression */
5808 if (regname == '=')
5810 if (allowexpr)
5812 if (expr_src)
5813 return get_expr_line_src();
5814 return get_expr_line();
5816 return NULL;
5819 if (regname == '@') /* "@@" is used for unnamed register */
5820 regname = '"';
5822 /* check for valid regname */
5823 if (regname != NUL && !valid_yank_reg(regname, FALSE))
5824 return NULL;
5826 #ifdef FEAT_CLIPBOARD
5827 regname = may_get_selection(regname);
5828 #endif
5830 if (get_spec_reg(regname, &retval, &allocated, FALSE))
5832 if (retval == NULL)
5833 return NULL;
5834 if (!allocated)
5835 retval = vim_strsave(retval);
5836 return retval;
5839 get_yank_register(regname, FALSE);
5840 if (y_current->y_array == NULL)
5841 return NULL;
5844 * Compute length of resulting string.
5846 len = 0;
5847 for (i = 0; i < y_current->y_size; ++i)
5849 len += (long)STRLEN(y_current->y_array[i]);
5851 * Insert a newline between lines and after last line if
5852 * y_type is MLINE.
5854 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5855 ++len;
5858 retval = lalloc(len + 1, TRUE);
5861 * Copy the lines of the yank register into the string.
5863 if (retval != NULL)
5865 len = 0;
5866 for (i = 0; i < y_current->y_size; ++i)
5868 STRCPY(retval + len, y_current->y_array[i]);
5869 len += (long)STRLEN(retval + len);
5872 * Insert a NL between lines and after the last line if y_type is
5873 * MLINE.
5875 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5876 retval[len++] = '\n';
5878 retval[len] = NUL;
5881 return retval;
5885 * Store string "str" in register "name".
5886 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5887 * If "must_append" is TRUE, always append to the register. Otherwise append
5888 * if "name" is an uppercase letter.
5889 * Note: "maxlen" and "must_append" don't work for the "/" register.
5890 * Careful: 'str' is modified, you may have to use a copy!
5891 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5893 void
5894 write_reg_contents(name, str, maxlen, must_append)
5895 int name;
5896 char_u *str;
5897 int maxlen;
5898 int must_append;
5900 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5903 void
5904 write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5905 int name;
5906 char_u *str;
5907 int maxlen;
5908 int must_append;
5909 int yank_type;
5910 long block_len;
5912 struct yankreg *old_y_previous, *old_y_current;
5913 long len;
5915 if (maxlen >= 0)
5916 len = maxlen;
5917 else
5918 len = (long)STRLEN(str);
5920 /* Special case: '/' search pattern */
5921 if (name == '/')
5923 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5924 return;
5927 #ifdef FEAT_EVAL
5928 if (name == '=')
5930 char_u *p, *s;
5932 p = vim_strnsave(str, (int)len);
5933 if (p == NULL)
5934 return;
5935 if (must_append)
5937 s = concat_str(get_expr_line_src(), p);
5938 vim_free(p);
5939 p = s;
5942 set_expr_line(p);
5943 return;
5945 #endif
5947 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
5949 emsg_invreg(name);
5950 return;
5953 if (name == '_') /* black hole: nothing to do */
5954 return;
5956 /* Don't want to change the current (unnamed) register */
5957 old_y_previous = y_previous;
5958 old_y_current = y_current;
5960 get_yank_register(name, TRUE);
5961 if (!y_append && !must_append)
5962 free_yank_all();
5963 #ifndef FEAT_VISUAL
5964 /* Just in case - make sure we don't use MBLOCK */
5965 if (yank_type == MBLOCK)
5966 yank_type = MAUTO;
5967 #endif
5968 if (yank_type == MAUTO)
5969 yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5970 ? MLINE : MCHAR);
5971 str_to_reg(y_current, yank_type, str, len, block_len);
5973 # ifdef FEAT_CLIPBOARD
5974 /* Send text of clipboard register to the clipboard. */
5975 may_set_selection();
5976 # endif
5978 /* ':let @" = "val"' should change the meaning of the "" register */
5979 if (name != '"')
5980 y_previous = old_y_previous;
5981 y_current = old_y_current;
5983 #endif /* FEAT_EVAL */
5985 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5987 * Put a string into a register. When the register is not empty, the string
5988 * is appended.
5990 static void
5991 str_to_reg(y_ptr, type, str, len, blocklen)
5992 struct yankreg *y_ptr; /* pointer to yank register */
5993 int type; /* MCHAR, MLINE or MBLOCK */
5994 char_u *str; /* string to put in register */
5995 long len; /* length of string */
5996 long blocklen; /* width of Visual block */
5998 int lnum;
5999 long start;
6000 long i;
6001 int extra;
6002 int newlines; /* number of lines added */
6003 int extraline = 0; /* extra line at the end */
6004 int append = FALSE; /* append to last line in register */
6005 char_u *s;
6006 char_u **pp;
6007 #ifdef FEAT_VISUAL
6008 long maxlen;
6009 #endif
6011 if (y_ptr->y_array == NULL) /* NULL means emtpy register */
6012 y_ptr->y_size = 0;
6015 * Count the number of lines within the string
6017 newlines = 0;
6018 for (i = 0; i < len; i++)
6019 if (str[i] == '\n')
6020 ++newlines;
6021 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6023 extraline = 1;
6024 ++newlines; /* count extra newline at the end */
6026 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6028 append = TRUE;
6029 --newlines; /* uncount newline when appending first line */
6033 * Allocate an array to hold the pointers to the new register lines.
6034 * If the register was not empty, move the existing lines to the new array.
6036 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6037 * sizeof(char_u *), TRUE);
6038 if (pp == NULL) /* out of memory */
6039 return;
6040 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6041 pp[lnum] = y_ptr->y_array[lnum];
6042 vim_free(y_ptr->y_array);
6043 y_ptr->y_array = pp;
6044 #ifdef FEAT_VISUAL
6045 maxlen = 0;
6046 #endif
6049 * Find the end of each line and save it into the array.
6051 for (start = 0; start < len + extraline; start += i + 1)
6053 for (i = start; i < len; ++i) /* find the end of the line */
6054 if (str[i] == '\n')
6055 break;
6056 i -= start; /* i is now length of line */
6057 #ifdef FEAT_VISUAL
6058 if (i > maxlen)
6059 maxlen = i;
6060 #endif
6061 if (append)
6063 --lnum;
6064 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6066 else
6067 extra = 0;
6068 s = alloc((unsigned)(i + extra + 1));
6069 if (s == NULL)
6070 break;
6071 if (extra)
6072 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6073 if (append)
6074 vim_free(y_ptr->y_array[lnum]);
6075 if (i)
6076 mch_memmove(s + extra, str + start, (size_t)i);
6077 extra += i;
6078 s[extra] = NUL;
6079 y_ptr->y_array[lnum++] = s;
6080 while (--extra >= 0)
6082 if (*s == NUL)
6083 *s = '\n'; /* replace NUL with newline */
6084 ++s;
6086 append = FALSE; /* only first line is appended */
6088 y_ptr->y_type = type;
6089 y_ptr->y_size = lnum;
6090 # ifdef FEAT_VISUAL
6091 if (type == MBLOCK)
6092 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6093 else
6094 y_ptr->y_width = 0;
6095 # endif
6097 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6099 void
6100 clear_oparg(oap)
6101 oparg_T *oap;
6103 vim_memset(oap, 0, sizeof(oparg_T));
6106 static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
6109 * Count the number of bytes, characters and "words" in a line.
6111 * "Words" are counted by looking for boundaries between non-space and
6112 * space characters. (it seems to produce results that match 'wc'.)
6114 * Return value is byte count; word count for the line is added to "*wc".
6115 * Char count is added to "*cc".
6117 * The function will only examine the first "limit" characters in the
6118 * line, stopping if it encounters an end-of-line (NUL byte). In that
6119 * case, eol_size will be added to the character count to account for
6120 * the size of the EOL character.
6122 static long
6123 line_count_info(line, wc, cc, limit, eol_size)
6124 char_u *line;
6125 long *wc;
6126 long *cc;
6127 long limit;
6128 int eol_size;
6130 long i;
6131 long words = 0;
6132 long chars = 0;
6133 int is_word = 0;
6135 for (i = 0; line[i] && i < limit; )
6137 if (is_word)
6139 if (vim_isspace(line[i]))
6141 words++;
6142 is_word = 0;
6145 else if (!vim_isspace(line[i]))
6146 is_word = 1;
6147 ++chars;
6148 #ifdef FEAT_MBYTE
6149 i += (*mb_ptr2len)(line + i);
6150 #else
6151 ++i;
6152 #endif
6155 if (is_word)
6156 words++;
6157 *wc += words;
6159 /* Add eol_size if the end of line was reached before hitting limit. */
6160 if (line[i] == NUL && i < limit)
6162 i += eol_size;
6163 chars += eol_size;
6165 *cc += chars;
6166 return i;
6170 * Give some info about the position of the cursor (for "g CTRL-G").
6171 * In Visual mode, give some info about the selected region. (In this case,
6172 * the *_count_cursor variables store running totals for the selection.)
6174 void
6175 cursor_pos_info()
6177 char_u *p;
6178 char_u buf1[50];
6179 char_u buf2[40];
6180 linenr_T lnum;
6181 long byte_count = 0;
6182 long byte_count_cursor = 0;
6183 long char_count = 0;
6184 long char_count_cursor = 0;
6185 long word_count = 0;
6186 long word_count_cursor = 0;
6187 int eol_size;
6188 long last_check = 100000L;
6189 #ifdef FEAT_VISUAL
6190 long line_count_selected = 0;
6191 pos_T min_pos, max_pos;
6192 oparg_T oparg;
6193 struct block_def bd;
6194 #endif
6197 * Compute the length of the file in characters.
6199 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6201 MSG(_(no_lines_msg));
6203 else
6205 if (get_fileformat(curbuf) == EOL_DOS)
6206 eol_size = 2;
6207 else
6208 eol_size = 1;
6210 #ifdef FEAT_VISUAL
6211 if (VIsual_active)
6213 if (lt(VIsual, curwin->w_cursor))
6215 min_pos = VIsual;
6216 max_pos = curwin->w_cursor;
6218 else
6220 min_pos = curwin->w_cursor;
6221 max_pos = VIsual;
6223 if (*p_sel == 'e' && max_pos.col > 0)
6224 --max_pos.col;
6226 if (VIsual_mode == Ctrl_V)
6228 oparg.is_VIsual = 1;
6229 oparg.block_mode = TRUE;
6230 oparg.op_type = OP_NOP;
6231 getvcols(curwin, &min_pos, &max_pos,
6232 &oparg.start_vcol, &oparg.end_vcol);
6233 if (curwin->w_curswant == MAXCOL)
6234 oparg.end_vcol = MAXCOL;
6235 /* Swap the start, end vcol if needed */
6236 if (oparg.end_vcol < oparg.start_vcol)
6238 oparg.end_vcol += oparg.start_vcol;
6239 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6240 oparg.end_vcol -= oparg.start_vcol;
6243 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6245 #endif
6247 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6249 /* Check for a CTRL-C every 100000 characters. */
6250 if (byte_count > last_check)
6252 ui_breakcheck();
6253 if (got_int)
6254 return;
6255 last_check = byte_count + 100000L;
6258 #ifdef FEAT_VISUAL
6259 /* Do extra processing for VIsual mode. */
6260 if (VIsual_active
6261 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6263 char_u *s = NULL;
6264 long len = 0L;
6266 switch (VIsual_mode)
6268 case Ctrl_V:
6269 # ifdef FEAT_VIRTUALEDIT
6270 virtual_op = virtual_active();
6271 # endif
6272 block_prep(&oparg, &bd, lnum, 0);
6273 # ifdef FEAT_VIRTUALEDIT
6274 virtual_op = MAYBE;
6275 # endif
6276 s = bd.textstart;
6277 len = (long)bd.textlen;
6278 break;
6279 case 'V':
6280 s = ml_get(lnum);
6281 len = MAXCOL;
6282 break;
6283 case 'v':
6285 colnr_T start_col = (lnum == min_pos.lnum)
6286 ? min_pos.col : 0;
6287 colnr_T end_col = (lnum == max_pos.lnum)
6288 ? max_pos.col - start_col + 1 : MAXCOL;
6290 s = ml_get(lnum) + start_col;
6291 len = end_col;
6293 break;
6295 if (s != NULL)
6297 byte_count_cursor += line_count_info(s, &word_count_cursor,
6298 &char_count_cursor, len, eol_size);
6299 if (lnum == curbuf->b_ml.ml_line_count
6300 && !curbuf->b_p_eol
6301 && curbuf->b_p_bin
6302 && (long)STRLEN(s) < len)
6303 byte_count_cursor -= eol_size;
6306 else
6307 #endif
6309 /* In non-visual mode, check for the line the cursor is on */
6310 if (lnum == curwin->w_cursor.lnum)
6312 word_count_cursor += word_count;
6313 char_count_cursor += char_count;
6314 byte_count_cursor = byte_count +
6315 line_count_info(ml_get(lnum),
6316 &word_count_cursor, &char_count_cursor,
6317 (long)(curwin->w_cursor.col + 1), eol_size);
6320 /* Add to the running totals */
6321 byte_count += line_count_info(ml_get(lnum), &word_count,
6322 &char_count, (long)MAXCOL, eol_size);
6325 /* Correction for when last line doesn't have an EOL. */
6326 if (!curbuf->b_p_eol && curbuf->b_p_bin)
6327 byte_count -= eol_size;
6329 #ifdef FEAT_VISUAL
6330 if (VIsual_active)
6332 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
6334 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
6335 &max_pos.col);
6336 sprintf((char *)buf1, _("%ld Cols; "),
6337 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6339 else
6340 buf1[0] = NUL;
6342 if (char_count_cursor == byte_count_cursor
6343 && char_count == byte_count)
6344 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
6345 buf1, line_count_selected,
6346 (long)curbuf->b_ml.ml_line_count,
6347 word_count_cursor, word_count,
6348 byte_count_cursor, byte_count);
6349 else
6350 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6351 buf1, line_count_selected,
6352 (long)curbuf->b_ml.ml_line_count,
6353 word_count_cursor, word_count,
6354 char_count_cursor, char_count,
6355 byte_count_cursor, byte_count);
6357 else
6358 #endif
6360 p = ml_get_curline();
6361 validate_virtcol();
6362 col_print(buf1, (int)curwin->w_cursor.col + 1,
6363 (int)curwin->w_virtcol + 1);
6364 col_print(buf2, (int)STRLEN(p), linetabsize(p));
6366 if (char_count_cursor == byte_count_cursor
6367 && char_count == byte_count)
6368 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
6369 (char *)buf1, (char *)buf2,
6370 (long)curwin->w_cursor.lnum,
6371 (long)curbuf->b_ml.ml_line_count,
6372 word_count_cursor, word_count,
6373 byte_count_cursor, byte_count);
6374 else
6375 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6376 (char *)buf1, (char *)buf2,
6377 (long)curwin->w_cursor.lnum,
6378 (long)curbuf->b_ml.ml_line_count,
6379 word_count_cursor, word_count,
6380 char_count_cursor, char_count,
6381 byte_count_cursor, byte_count);
6384 #ifdef FEAT_MBYTE
6385 byte_count = bomb_size();
6386 if (byte_count > 0)
6387 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
6388 byte_count);
6389 #endif
6390 /* Don't shorten this message, the user asked for it. */
6391 p = p_shm;
6392 p_shm = (char_u *)"";
6393 msg(IObuff);
6394 p_shm = p;