blob: f348a890e99118f9125b9e402028fa3d2f6913cb [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde,
12 * op_change, op_yank, do_put, do_join
13 */
14
15#include "vim.h"
16
17/*
18 * Number of registers.
19 * 0 = unnamed register, for normal yanks and puts
20 * 1..9 = registers '1' to '9', for deletes
21 * 10..35 = registers 'a' to 'z'
22 * 36 = delete register '-'
23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
25 */
26/*
27 * Symbolic names for some registers.
28 */
29#define DELETION_REGISTER 36
30#ifdef FEAT_CLIPBOARD
31# define STAR_REGISTER 37
32# ifdef FEAT_X11
33# define PLUS_REGISTER 38
34# else
35# define PLUS_REGISTER STAR_REGISTER /* there is only one */
36# endif
37#endif
38#ifdef FEAT_DND
39# define TILDE_REGISTER (PLUS_REGISTER + 1)
40#endif
41
42#ifdef FEAT_CLIPBOARD
43# ifdef FEAT_DND
44# define NUM_REGISTERS (TILDE_REGISTER + 1)
45# else
46# define NUM_REGISTERS (PLUS_REGISTER + 1)
47# endif
48#else
49# define NUM_REGISTERS 37
50#endif
51
52/*
53 * Each yank register is an array of pointers to lines.
54 */
55static struct yankreg
56{
57 char_u **y_array; /* pointer to array of line pointers */
58 linenr_T y_size; /* number of lines in y_array */
59 char_u y_type; /* MLINE, MCHAR or MBLOCK */
60#ifdef FEAT_VISUAL
61 colnr_T y_width; /* only set if y_type == MBLOCK */
62#endif
63} y_regs[NUM_REGISTERS];
64
65static struct yankreg *y_current; /* ptr to current yankreg */
66static int y_append; /* TRUE when appending */
67static struct yankreg *y_previous = NULL; /* ptr to last written yankreg */
68
69/*
70 * structure used by block_prep, op_delete and op_yank for blockwise operators
71 * also op_change, op_shift, op_insert, op_replace - AKelly
72 */
73struct block_def
74{
75 int startspaces; /* 'extra' cols of first char */
76 int endspaces; /* 'extra' cols of first char */
77 int textlen; /* chars in block */
78 char_u *textstart; /* pointer to 1st char in block */
79 colnr_T textcol; /* cols of chars (at least part.) in block */
80 colnr_T start_vcol; /* start col of 1st char wholly inside block */
81 colnr_T end_vcol; /* start col of 1st char wholly after block */
82#ifdef FEAT_VISUALEXTRA
83 int is_short; /* TRUE if line is too short to fit in block */
84 int is_MAX; /* TRUE if curswant==MAXCOL when starting */
85 int is_oneChar; /* TRUE if block within one character */
86 int pre_whitesp; /* screen cols of ws before block */
87 int pre_whitesp_c; /* chars of ws before block */
88 colnr_T end_char_vcols; /* number of vcols of post-block char */
89#endif
90 colnr_T start_char_vcols; /* number of vcols of pre-block char */
91};
92
93#ifdef FEAT_VISUALEXTRA
94static void shift_block __ARGS((oparg_T *oap, int amount));
95static void block_insert __ARGS((oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp));
96#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000097static int stuff_yank __ARGS((int, char_u *));
Bram Moolenaard333d1e2006-11-07 17:43:47 +000098static void put_reedit_in_typebuf __ARGS((int silent));
Bram Moolenaar7f6ca072007-02-27 16:21:38 +000099static int put_in_typebuf __ARGS((char_u *s, int esc, int colon,
100 int silent));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void stuffescaped __ARGS((char_u *arg, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#ifdef FEAT_MBYTE
103static void mb_adjust_opend __ARGS((oparg_T *oap));
104#endif
105static void free_yank __ARGS((long));
106static void free_yank_all __ARGS((void));
107static int yank_copy_line __ARGS((struct block_def *bd, long y_idx));
108#ifdef FEAT_CLIPBOARD
109static void copy_yank_reg __ARGS((struct yankreg *reg));
110# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
111static void may_set_selection __ARGS((void));
112# endif
113#endif
114static void dis_msg __ARGS((char_u *p, int skip_esc));
115#ifdef FEAT_VISUAL
116static void block_prep __ARGS((oparg_T *oap, struct block_def *, linenr_T, int));
117#endif
118#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
119static void str_to_reg __ARGS((struct yankreg *y_ptr, int type, char_u *str, long len, long blocklen));
120#endif
121static int ends_in_white __ARGS((linenr_T lnum));
122#ifdef FEAT_COMMENTS
123static int same_leader __ARGS((linenr_T lnum, int, char_u *, int, char_u *));
124static int fmt_check_par __ARGS((linenr_T, int *, char_u **, int do_comments));
125#else
126static int fmt_check_par __ARGS((linenr_T));
127#endif
128
129/*
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.
133 */
134static char opchars[][3] =
135{
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 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000163 {'g', '@', FALSE}, /* OP_FUNCTION */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164};
165
166/*
167 * Translate a command name into an operator type.
168 * Must only be called with a valid operator name!
169 */
170 int
171get_op_type(char1, char2)
172 int char1;
173 int char2;
174{
175 int i;
176
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;
185}
186
187#if defined(FEAT_VISUAL) || defined(PROTO)
188/*
189 * Return TRUE if operator "op" always works on whole lines.
190 */
191 int
192op_on_lines(op)
193 int op;
194{
195 return opchars[op][2];
196}
197#endif
198
199/*
200 * Get first operator command character.
201 * Returns 'g' or 'z' if there is another command character.
202 */
203 int
204get_op_char(optype)
205 int optype;
206{
207 return opchars[optype][0];
208}
209
210/*
211 * Get second operator command character.
212 */
213 int
214get_extra_op_char(optype)
215 int optype;
216{
217 return opchars[optype][1];
218}
219
220/*
221 * op_shift - handle a shift operation
222 */
223 void
224op_shift(oap, curs_top, amount)
225 oparg_T *oap;
226 int curs_top;
227 int amount;
228{
229 long i;
230 int first_char;
231 char_u *s;
232#ifdef FEAT_VISUAL
233 int block_col = 0;
234#endif
235
236 if (u_save((linenr_T)(oap->start.lnum - 1),
237 (linenr_T)(oap->end.lnum + 1)) == FAIL)
238 return;
239
240#ifdef FEAT_VISUAL
241 if (oap->block_mode)
242 block_col = curwin->w_cursor.col;
243#endif
244
245 for (i = oap->line_count; --i >= 0; )
246 {
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
260 {
261 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount);
262 }
263 ++curwin->w_cursor.lnum;
264 }
265
266 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
267
268#ifdef FEAT_VISUAL
269 if (oap->block_mode)
270 {
271 curwin->w_cursor.lnum = oap->start.lnum;
272 curwin->w_cursor.col = block_col;
273 }
274 else
275#endif
276 if (curs_top) /* put cursor on first line, for ">>" */
277 {
278 curwin->w_cursor.lnum = oap->start.lnum;
279 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */
280 }
281 else
282 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */
283
284 if (oap->line_count > p_report)
285 {
286 if (oap->op_type == OP_RSHIFT)
287 s = (char_u *)">";
288 else
289 s = (char_u *)"<";
290 if (oap->line_count == 1)
291 {
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);
296 }
297 else
298 {
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);
305 }
306 msg(IObuff);
307 }
308
309 /*
310 * Set "'[" and "']" marks.
311 */
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;
317}
318
319/*
320 * shift the current line one shiftwidth left (if left != 0) or right
321 * leaves cursor on first blank in the line
322 */
323 void
324shift_line(left, round, amount)
325 int left;
326 int round;
327 int amount;
328{
329 int count;
330 int i, j;
331 int p_sw = (int)curbuf->b_p_sw;
332
333 count = get_indent(); /* get current indent */
334
335 if (round) /* round off indent */
336 {
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)
342 {
343 i -= amount;
344 if (i < 0)
345 i = 0;
346 }
347 else
348 i += amount;
349 count = i * p_sw;
350 }
351 else /* original vi indent */
352 {
353 if (left)
354 {
355 count -= p_sw * amount;
356 if (count < 0)
357 count = 0;
358 }
359 else
360 count += p_sw * amount;
361 }
362
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);
370}
371
372#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
373/*
374 * Shift one line of the current block one shiftwidth right or left.
375 * Leaves cursor on first character in block.
376 */
377 static void
378shift_block(oap, amount)
379 oparg_T *oap;
380 int amount;
381{
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;
395
396#ifdef FEAT_RIGHTLEFT
397 int old_p_ri = p_ri;
398
399 p_ri = 0; /* don't want revins in ident */
400#endif
401
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;
406
407 /* total is number of screen columns to be inserted/removed */
408 total = amount * p_sw;
409 oldp = ml_get_curline();
410
411 if (!left)
412 {
413 /*
414 * 1. Get start vcol
415 * 2. Total ws vcols
416 * 3. Divvy into TABs & spp
417 * 4. Construct new string
418 */
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)
422 {
423#ifdef FEAT_MBYTE
424 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000425 bd.textstart += (*mb_ptr2len)(bd.textstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#endif
427 ++bd.textstart;
428 }
429 for ( ; vim_iswhite(*bd.textstart); )
430 {
431 incr = lbr_chartabsize_adv(&bd.textstart, (colnr_T)(bd.start_vcol));
432 total += incr;
433 bd.start_vcol += incr;
434 }
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);
455 }
456 else /* left */
457 {
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); )
462 {
463 incr = lbr_chartabsize_adv(&midp, (colnr_T)vcol);
464 vcol += incr;
465 }
466 /* internal is the block-internal ws replacing a split TAB */
467 if (vcol > (oap->start_vcol + total))
468 {
469 /* we have to split the TAB *(midp-1) */
470 internal = vcol - (oap->start_vcol + total);
471 }
472 /* if 'expandtab' is not set, use TABs */
473
474 split = bd.startspaces + internal;
475 if (split > 0)
476 {
477 if (!curbuf->b_p_et)
478 {
479 for (ptr = oldp, col = 0; ptr < oldp+bd.textcol; )
480 col += lbr_chartabsize_adv(&ptr, (colnr_T)col);
481
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 */
485 }
486 if (i)
487 j = ((col % p_ts) + split) % p_ts; /* number of spp */
488 else
489 j = split;
490 }
491
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));
496
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))
501 {
502 if (*midp == TAB)
503 i++;
504 else /*space */
505 j++;
506 midp++;
507 }
508 /* We might have an extra TAB worth of spp now! */
509 if (j / p_ts && !curbuf->b_p_et)
510 {
511 i++;
512 j -= p_ts;
513 }
514 copy_chars(newp + bd.textcol, (size_t)i, TAB);
515 copy_spaces(newp + bd.textcol + i, (size_t)j);
516
517 /* the end */
518 mch_memmove(newp + STRLEN(newp), midp, (size_t)STRLEN(midp) + 1);
519 }
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
528}
529#endif
530
531#ifdef FEAT_VISUALEXTRA
532/*
533 * Insert string "s" (b_insert ? before : after) block :AKelly
534 * Caller must prepare for undo.
535 */
536 static void
537block_insert(oap, s, b_insert, bdp)
538 oparg_T *oap;
539 char_u *s;
540 int b_insert;
541 struct block_def *bdp;
542{
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;
551
552 State = INSERT; /* don't want REPLACE for State */
553 s_len = (unsigned)STRLEN(s);
554
555 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++)
556 {
557 block_prep(oap, bdp, lnum, TRUE);
558 if (bdp->is_short && b_insert)
559 continue; /* OP_INSERT, line ends before block start */
560
561 oldp = ml_get(lnum);
562
563 if (b_insert)
564 {
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;
570 }
571 else /* append */
572 {
573 p_ts = bdp->end_char_vcols;
574 if (!bdp->is_short) /* spaces = padding after block */
575 {
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);
580 }
581 else /* spaces = padding to block edge */
582 {
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;
588 }
589 }
590
591 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1);
592 if (newp == NULL)
593 continue;
594
595 /* copy up to shifted part */
596 mch_memmove(newp, oldp, (size_t)(offset));
597 oldp += offset;
598
599 /* insert pre-padding */
600 copy_spaces(newp + offset, (size_t)spaces);
601
602 /* copy the new text */
603 mch_memmove(newp + offset + spaces, s, (size_t)s_len);
604 offset += s_len;
605
606 if (spaces && !bdp->is_short)
607 {
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++;
614 }
615
616 if (spaces > 0)
617 offset += count;
618 mch_memmove(newp + offset, oldp, (size_t)(STRLEN(oldp) + 1));
619
620 ml_replace(lnum, newp, FALSE);
621
622 if (lnum == oap->end.lnum)
623 {
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;
628 }
629 } /* for all lnum */
630
631 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
632
633 State = oldstate;
634}
635#endif
636
637#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
638/*
639 * op_reindent - handle reindenting a block of lines.
640 */
641 void
642op_reindent(oap, how)
643 oparg_T *oap;
644 int (*how) __ARGS((void));
645{
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;
652
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000653 /* Don't even try when 'modifiable' is off. */
654 if (!curbuf->b_p_ma)
655 {
656 EMSG(_(e_modifiable));
657 return;
658 }
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 for (i = oap->line_count; --i >= 0 && !got_int; )
661 {
662 /* it's a slow thing to do, so give feedback so there's no worry that
663 * the computer's just hung. */
664
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);
669
670 /*
671 * Be vi-compatible: For lisp indenting the first line is not
672 * indented, unless there is only one line.
673 */
674#ifdef FEAT_LISP
675 if (i != oap->line_count - 1 || oap->line_count == 1
676 || how != get_lisp_indent)
677#endif
678 {
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 */
684
685 if (set_indent(count, SIN_UNDO))
686 {
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;
691 }
692 }
693 ++curwin->w_cursor.lnum;
694 }
695
696 /* put cursor on first non-blank of indented line */
697 curwin->w_cursor.lnum = start_lnum;
698 beginline(BL_SOL | BL_FIX);
699
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
713
714 if (oap->line_count > p_report)
715 {
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);
721 }
722 /* set '[ and '] marks */
723 curbuf->b_op_start = oap->start;
724 curbuf->b_op_end = oap->end;
725}
726#endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */
727
728#if defined(FEAT_EVAL) || defined(PROTO)
729/*
730 * Keep the last expression line here, for repeating.
731 */
732static char_u *expr_line = NULL;
733
734/*
735 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
736 * Returns '=' when OK, NUL otherwise.
737 */
738 int
739get_expr_register()
740{
741 char_u *new_line;
742
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 '=';
751}
752
753/*
754 * Set the expression for the '=' register.
755 * Argument must be an allocated string.
756 */
757 void
758set_expr_line(new_line)
759 char_u *new_line;
760{
761 vim_free(expr_line);
762 expr_line = new_line;
763}
764
765/*
766 * Get the result of the '=' register expression.
767 * Returns a pointer to allocated memory, or NULL for failure.
768 */
769 char_u *
770get_expr_line()
771{
772 char_u *expr_copy;
773 char_u *rv;
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000774 static int nested = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
776 if (expr_line == NULL)
777 return NULL;
778
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;
784
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000785 /* 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;
789
790 ++nested;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000791 rv = eval_to_string(expr_copy, NULL, TRUE);
Bram Moolenaar21bffa72006-10-06 21:33:16 +0000792 --nested;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 vim_free(expr_copy);
794 return rv;
795}
Bram Moolenaarde934d72005-05-22 22:09:40 +0000796
797/*
798 * Get the '=' register expression itself, without evaluating it.
799 */
800 char_u *
801get_expr_line_src()
802{
803 if (expr_line == NULL)
804 return NULL;
805 return vim_strsave(expr_line);
806}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#endif /* FEAT_EVAL */
808
809/*
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
812 */
813 int
814valid_yank_reg(regname, writing)
815 int regname;
816 int writing; /* if TRUE check for writable registers */
817{
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
836 )
837 return TRUE;
838 return FALSE;
839}
840
841/*
842 * Set y_current and y_append, according to the value of "regname".
843 * Cannot handle the '_' register.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000844 * Must only be called with a valid register name!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *
846 * If regname is 0 and writing, use register 0
847 * If regname is 0 and reading, use previous register
848 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000849 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850get_yank_register(regname, writing)
851 int regname;
852 int writing;
853{
854 int i;
855
856 y_append = FALSE;
857 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
858 {
859 y_current = y_previous;
860 return;
861 }
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))
868 {
869 i = CharOrdUp(i) + 10;
870 y_append = TRUE;
871 }
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;
891}
892
Bram Moolenaar8299df92004-07-10 09:47:34 +0000893#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894/*
895 * When "regname" is a clipboard register, obtain the selection. If it's not
896 * available return zero, otherwise return "regname".
897 */
Bram Moolenaar8299df92004-07-10 09:47:34 +0000898 int
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899may_get_selection(regname)
900 int regname;
901{
902 if (regname == '*')
903 {
904 if (!clip_star.available)
905 regname = 0;
906 else
907 clip_get_selection(&clip_star);
908 }
909 else if (regname == '+')
910 {
911 if (!clip_plus.available)
912 regname = 0;
913 else
914 clip_get_selection(&clip_plus);
915 }
916 return regname;
917}
918#endif
919
920#if defined(FEAT_VISUAL) || defined(PROTO)
921/*
922 * Obtain the contents of a "normal" register. The register is made empty.
923 * The returned pointer has allocated memory, use put_register() later.
924 */
925 void *
926get_register(name, copy)
927 int name;
928 int copy; /* make a copy, if FALSE make register empty. */
929{
Bram Moolenaar0a307462007-12-01 20:13:05 +0000930 struct yankreg *reg;
931 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933#ifdef FEAT_CLIPBOARD
934 /* When Visual area changed, may have to update selection. Obtain the
935 * selection too. */
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000936 if (name == '*' && clip_star.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaarcdfd3e42007-11-08 09:35:50 +0000938 if (clip_isautosel())
939 clip_update_selection();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 may_get_selection(name);
941 }
942#endif
943
944 get_yank_register(name, 0);
945 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
946 if (reg != NULL)
947 {
948 *reg = *y_current;
949 if (copy)
950 {
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)
958 {
959 for (i = 0; i < reg->y_size; ++i)
960 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
961 }
962 }
963 else
964 y_current->y_array = NULL;
965 }
966 return (void *)reg;
967}
968
969/*
Bram Moolenaar0a307462007-12-01 20:13:05 +0000970 * Put "reg" into register "name". Free any previous contents and "reg".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 */
972 void
973put_register(name, reg)
974 int name;
975 void *reg;
976{
977 get_yank_register(name, 0);
978 free_yank_all();
979 *y_current = *(struct yankreg *)reg;
Bram Moolenaar0a307462007-12-01 20:13:05 +0000980 vim_free(reg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982# ifdef FEAT_CLIPBOARD
983 /* Send text written to clipboard register to the clipboard. */
984 may_set_selection();
985# endif
986}
987#endif
988
989#if defined(FEAT_MOUSE) || defined(PROTO)
990/*
991 * return TRUE if the current yank register has type MLINE
992 */
993 int
994yank_register_mline(regname)
995 int regname;
996{
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);
1003}
1004#endif
1005
1006/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001007 * Start or stop recording into a yank register.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 *
Bram Moolenaard55de222007-05-06 13:38:48 +00001009 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 */
1011 int
1012do_record(c)
1013 int c;
1014{
Bram Moolenaard55de222007-05-06 13:38:48 +00001015 char_u *p;
1016 static int regname;
1017 struct yankreg *old_y_previous, *old_y_current;
1018 int retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
1020 if (Recording == FALSE) /* start recording */
1021 {
1022 /* registers 0-9, a-z and " are allowed */
1023 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1024 retval = FAIL;
1025 else
1026 {
1027 Recording = TRUE;
1028 showmode();
1029 regname = c;
1030 retval = OK;
1031 }
1032 }
1033 else /* stop recording */
1034 {
1035 /*
Bram Moolenaard55de222007-05-06 13:38:48 +00001036 * 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.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
1040 Recording = FALSE;
1041 MSG("");
1042 p = get_recorded();
1043 if (p == NULL)
1044 retval = FAIL;
1045 else
1046 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001047 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1048 vim_unescape_csi(p);
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 /*
1051 * We don't want to change the default register here, so save and
1052 * restore the current register name.
1053 */
1054 old_y_previous = y_previous;
1055 old_y_current = y_current;
1056
1057 retval = stuff_yank(regname, p);
1058
1059 y_previous = old_y_previous;
1060 y_current = old_y_current;
1061 }
1062 }
1063 return retval;
1064}
1065
1066/*
1067 * Stuff string "p" into yank register "regname" as a single line (append if
1068 * uppercase). "p" must have been alloced.
1069 *
1070 * return FAIL for failure, OK otherwise
1071 */
1072 static int
1073stuff_yank(regname, p)
1074 int regname;
1075 char_u *p;
1076{
1077 char_u *lp;
1078 char_u **pp;
1079
1080 /* check for read-only register */
1081 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1082 {
1083 vim_free(p);
1084 return FAIL;
1085 }
1086 if (regname == '_') /* black hole: don't do anything */
1087 {
1088 vim_free(p);
1089 return OK;
1090 }
1091 get_yank_register(regname, TRUE);
1092 if (y_append && y_current->y_array != NULL)
1093 {
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)
1097 {
1098 vim_free(p);
1099 return FAIL;
1100 }
1101 STRCPY(lp, *pp);
1102 STRCAT(lp, p);
1103 vim_free(p);
1104 vim_free(*pp);
1105 *pp = lp;
1106 }
1107 else
1108 {
1109 free_yank_all();
1110 if ((y_current->y_array =
1111 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1112 {
1113 vim_free(p);
1114 return FAIL;
1115 }
1116 y_current->y_array[0] = p;
1117 y_current->y_size = 1;
1118 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1119 }
1120 return OK;
1121}
1122
1123/*
1124 * execute a yank register: copy it into the stuff buffer
1125 *
1126 * return FAIL for failure, OK otherwise
1127 */
1128 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001129do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int regname;
1131 int colon; /* insert ':' before each line */
1132 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001133 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
1135 static int lastc = NUL;
1136 long i;
1137 char_u *p;
1138 int retval = OK;
1139 int remap;
1140
1141 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001142 {
1143 if (lastc == NUL)
1144 {
1145 EMSG(_("E748: No previously used register"));
1146 return FAIL;
1147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 regname = lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 /* check for valid regname */
1151 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001152 {
1153 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 lastc = regname;
1157
1158#ifdef FEAT_CLIPBOARD
1159 regname = may_get_selection(regname);
1160#endif
1161
1162 if (regname == '_') /* black hole: don't stuff anything */
1163 return OK;
1164
1165#ifdef FEAT_CMDHIST
1166 if (regname == ':') /* use last command line */
1167 {
1168 if (last_cmdline == NULL)
1169 {
1170 EMSG(_(e_nolastcmd));
1171 return FAIL;
1172 }
1173 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1174 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001175 /* Escape all control characters with a CTRL-V */
1176 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001177 (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);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001178 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001179 {
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)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001183 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001184 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001185 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001186 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001187 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189#endif
1190#ifdef FEAT_EVAL
1191 else if (regname == '=')
1192 {
1193 p = get_expr_line();
1194 if (p == NULL)
1195 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001196 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 vim_free(p);
1198 }
1199#endif
1200 else if (regname == '.') /* use last inserted text */
1201 {
1202 p = get_last_insert_save();
1203 if (p == NULL)
1204 {
1205 EMSG(_(e_noinstext));
1206 return FAIL;
1207 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001208 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 vim_free(p);
1210 }
1211 else
1212 {
1213 get_yank_register(regname, FALSE);
1214 if (y_current->y_array == NULL)
1215 return FAIL;
1216
1217 /* Disallow remaping for ":@r". */
1218 remap = colon ? REMAP_NONE : REMAP_YES;
1219
1220 /*
1221 * Insert lines into typeahead buffer, from last one to first one.
1222 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001223 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 for (i = y_current->y_size; --i >= 0; )
1225 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001226 char_u *escaped;
1227
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 /* 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)
1231 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001232 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 return FAIL;
1234 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001235 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)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001242 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 == FAIL)
1244 return FAIL;
1245 }
1246 Exec_reg = TRUE; /* disable the 'q' command */
1247 }
1248 return retval;
1249}
1250
1251/*
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.
1254 */
1255 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001256put_reedit_in_typebuf(silent)
1257 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
1259 char_u buf[3];
1260
1261 if (restart_edit != NUL)
1262 {
1263 if (restart_edit == 'V')
1264 {
1265 buf[0] = 'g';
1266 buf[1] = 'R';
1267 buf[2] = NUL;
1268 }
1269 else
1270 {
1271 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1272 buf[1] = NUL;
1273 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001274 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 restart_edit = NUL;
1276 }
1277}
1278
1279 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001280put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 char_u *s;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001282 int esc; /* Escape CSI characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001284 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 int retval = OK;
1287
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001288 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (colon)
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001290 retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001292 {
1293 char_u *p;
1294
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);
1305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (colon && retval == OK)
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001307 retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 return retval;
1309}
1310
1311/*
1312 * Insert a yank register: copy it into the Read buffer.
1313 * Used by CTRL-R command and middle mouse button in insert mode.
1314 *
1315 * return FAIL for failure, OK otherwise
1316 */
1317 int
1318insert_reg(regname, literally)
1319 int regname;
1320 int literally; /* insert literally, not as if typed */
1321{
1322 long i;
1323 int retval = OK;
1324 char_u *arg;
1325 int allocated;
1326
1327 /*
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.
1331 */
1332 ui_breakcheck();
1333 if (got_int)
1334 return FAIL;
1335
1336 /* check for valid regname */
1337 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1338 return FAIL;
1339
1340#ifdef FEAT_CLIPBOARD
1341 regname = may_get_selection(regname);
1342#endif
1343
1344 if (regname == '.') /* insert last inserted text */
1345 retval = stuff_inserted(NUL, 1L, TRUE);
1346 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1347 {
1348 if (arg == NULL)
1349 return FAIL;
1350 stuffescaped(arg, literally);
1351 if (allocated)
1352 vim_free(arg);
1353 }
1354 else /* name or number register */
1355 {
1356 get_yank_register(regname, FALSE);
1357 if (y_current->y_array == NULL)
1358 retval = FAIL;
1359 else
1360 {
1361 for (i = 0; i < y_current->y_size; ++i)
1362 {
1363 stuffescaped(y_current->y_array[i], literally);
1364 /*
1365 * Insert a newline between lines and after last line if
1366 * y_type is MLINE.
1367 */
1368 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1369 stuffcharReadbuff('\n');
1370 }
1371 }
1372 }
1373
1374 return retval;
1375}
1376
1377/*
1378 * Stuff a string into the typeahead buffer, such that edit() will insert it
1379 * literally ("literally" TRUE) or interpret is as typed characters.
1380 */
1381 static void
1382stuffescaped(arg, literally)
1383 char_u *arg;
1384 int literally;
1385{
1386 int c;
1387 char_u *start;
1388
1389 while (*arg != NUL)
1390 {
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
1399 )
1400 || (*arg == K_SPECIAL && !literally))
1401 ++arg;
1402 if (arg > start)
1403 stuffReadbuffLen(start, (long)(arg - start));
1404
1405 /* stuff a single special character */
1406 if (*arg != NUL)
1407 {
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);
1417 }
1418 }
1419}
1420
1421/*
Bram Moolenaard55de222007-05-06 13:38:48 +00001422 * If "regname" is a special register, return TRUE and store a pointer to its
1423 * value in "argp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001425 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426get_spec_reg(regname, argp, allocated, errmsg)
1427 int regname;
1428 char_u **argp;
Bram Moolenaard55de222007-05-06 13:38:48 +00001429 int *allocated; /* return: TRUE when value was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 int errmsg; /* give error message when failing */
1431{
1432 int cnt;
1433
1434 *argp = NULL;
1435 *allocated = FALSE;
1436 switch (regname)
1437 {
1438 case '%': /* file name */
1439 if (errmsg)
1440 check_fname(); /* will give emsg if not set */
1441 *argp = curbuf->b_fname;
1442 return TRUE;
1443
1444 case '#': /* alternate file name */
1445 *argp = getaltfname(errmsg); /* may give emsg if not set */
1446 return TRUE;
1447
1448#ifdef FEAT_EVAL
1449 case '=': /* result of expression */
1450 *argp = get_expr_line();
1451 *allocated = TRUE;
1452 return TRUE;
1453#endif
1454
1455 case ':': /* last command line */
1456 if (last_cmdline == NULL && errmsg)
1457 EMSG(_(e_nolastcmd));
1458 *argp = last_cmdline;
1459 return TRUE;
1460
1461 case '/': /* last search-pattern */
1462 if (last_search_pat() == NULL && errmsg)
1463 EMSG(_(e_noprevre));
1464 *argp = last_search_pat();
1465 return TRUE;
1466
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;
1473
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
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001480 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 *allocated = TRUE;
1482 return TRUE;
1483#endif
1484
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;
1494
1495 case '_': /* black hole: always empty */
1496 *argp = (char_u *)"";
1497 return TRUE;
1498 }
1499
1500 return FALSE;
1501}
1502
1503/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001504 * Paste a yank register into the command line.
1505 * Only for non-special registers.
1506 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * insert_reg() can't be used here, because special characters from the
1508 * register contents will be interpreted as commands.
1509 *
1510 * return FAIL for failure, OK otherwise
1511 */
1512 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001513cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 int regname;
1515 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001516 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 get_yank_register(regname, FALSE);
1521 if (y_current->y_array == NULL)
1522 return FAIL;
1523
1524 for (i = 0; i < y_current->y_size; ++i)
1525 {
1526 cmdline_paste_str(y_current->y_array[i], literally);
1527
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001528 /* 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)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 cmdline_paste_str((char_u *)"\r", literally);
1536
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;
1542 }
1543 return OK;
1544}
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1547/*
1548 * Adjust the register name pointed to with "rp" for the clipboard being
1549 * used always and the clipboard being available.
1550 */
1551 void
1552adjust_clip_reg(rp)
1553 int *rp;
1554{
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;
1562}
1563#endif
1564
1565/*
1566 * op_delete - handle a delete operation
1567 *
1568 * return FAIL if undo failed, OK otherwise.
1569 */
1570 int
1571op_delete(oap)
1572 oparg_T *oap;
1573{
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;
1583
1584 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1585 return OK;
1586
1587 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1588 if (oap->empty)
1589 return u_save_cursor();
1590
1591 if (!curbuf->b_p_ma)
1592 {
1593 EMSG(_(e_modifiable));
1594 return FAIL;
1595 }
1596
1597#ifdef FEAT_CLIPBOARD
1598 adjust_clip_reg(&oap->regname);
1599#endif
1600
1601#ifdef FEAT_MBYTE
1602 if (has_mbyte)
1603 mb_adjust_opend(oap);
1604#endif
1605
1606/*
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.
1610 */
1611 if ( oap->motion_type == MCHAR
1612#ifdef FEAT_VISUAL
1613 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001614 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615#endif
1616 && oap->line_count > 1
1617 && oap->op_type == OP_DELETE)
1618 {
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;
1623 }
1624
1625/*
1626 * Check for trying to delete (e.g. "D") in an empty line.
1627 * Note: For the change operator it is ok.
1628 */
1629 if ( oap->motion_type == MCHAR
1630 && oap->line_count == 1
1631 && oap->op_type == OP_DELETE
1632 && *ml_get(oap->start.lnum) == NUL)
1633 {
1634 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001635 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * 'cpoptions' (Vi compatible).
1637 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001638#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
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1645 beep_flush();
1646 return OK;
1647 }
1648
1649/*
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.
1653 */
1654 if (oap->regname != '_')
1655 {
1656 if (oap->regname != 0)
1657 {
1658 /* check for read-only register */
1659 if (!valid_yank_reg(oap->regname, TRUE))
1660 {
1661 beep_flush();
1662 return OK;
1663 }
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;
1667 }
1668
1669 /*
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.
1672 */
1673 if (oap->regname != 0 || oap->motion_type == MLINE
1674 || oap->line_count > 1 || oap->use_reg_one)
1675 {
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;
1684 }
1685
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)
1690 {
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;
1696 }
1697
1698 /*
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.
1703 */
1704 if (!did_yank)
1705 {
1706 int msg_silent_save = msg_silent;
1707
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')
1712 {
1713 EMSG(_(e_abort));
1714 return FAIL;
1715 }
1716 }
1717 }
1718
1719#ifdef FEAT_VISUAL
1720/*
1721 * block mode delete
1722 */
1723 if (oap->block_mode)
1724 {
1725 if (u_save((linenr_T)(oap->start.lnum - 1),
1726 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1727 return FAIL;
1728
1729 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1730 {
1731 block_prep(oap, &bd, lnum, TRUE);
1732 if (bd.textlen == 0) /* nothing to delete */
1733 continue;
1734
1735 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1736 if (lnum == curwin->w_cursor.lnum)
1737 {
1738 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1739# ifdef FEAT_VIRTUALEDIT
1740 curwin->w_cursor.coladd = 0;
1741# endif
1742 }
1743
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!
1747 */
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);
1764 }
1765
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 */
1770 }
1771 else
1772#endif
1773 if (oap->motion_type == MLINE)
1774 {
1775 if (oap->op_type == OP_CHANGE)
1776 {
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.
1780 */
1781 if (oap->line_count > 1)
1782 {
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;
1787 }
1788 if (u_save_cursor() == FAIL)
1789 return FAIL;
1790 if (curbuf->b_p_ai) /* don't delete indent */
1791 {
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;
1795 }
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" */
1802 }
1803 else
1804 {
1805 del_lines(oap->line_count, TRUE);
1806 beginline(BL_WHITE | BL_FIX);
1807 u_clearline(); /* "U" command not possible after "dd" */
1808 }
1809 }
1810 else
1811 {
1812#ifdef FEAT_VIRTUALEDIT
1813 if (virtual_op)
1814 {
1815 int endcol = 0;
1816
1817 /* For virtualedit: break the tabs that are partly included. */
1818 if (gchar_pos(&oap->start) == '\t')
1819 {
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)
1827 {
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;
1832 }
1833 }
1834
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)
1838 {
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;
1847 }
1848 }
1849#endif
1850
1851 if (oap->line_count == 1) /* delete characters within one line */
1852 {
1853 if (u_save_cursor() == FAIL) /* save line for undo */
1854 return FAIL;
1855
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
1863 )
1864 display_dollar(oap->end.col - !oap->inclusive);
1865
1866 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1867
1868#ifdef FEAT_VIRTUALEDIT
1869 if (virtual_op)
1870 {
1871 /* fix up things for virtualedit-delete:
1872 * break the tabs which are going to get in our way
1873 */
1874 char_u *curline = ml_get_curline();
1875 int len = (int)STRLEN(curline);
1876
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;
1884
1885 /* When deleted a char in the line, reset coladd. */
1886 if (gchar_cursor() != NUL)
1887 curwin->w_cursor.coladd = 0;
1888 }
1889#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +00001890 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001891#ifdef FEAT_VISUAL
1892 && !oap->is_VIsual
1893#endif
1894 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 else /* delete characters between lines */
1897 {
1898 pos_T curpos;
1899
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;
1904
1905 truncate_line(TRUE); /* delete from cursor to end of line */
1906
1907 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1908 ++curwin->w_cursor.lnum;
1909 del_lines((long)(oap->line_count - 2), FALSE);
1910
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),
Bram Moolenaarca003e12006-03-17 23:19:38 +00001914 !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001915#ifdef FEAT_VISUAL
1916 && !oap->is_VIsual
1917#endif
1918 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1920
1921 (void)do_join(FALSE);
1922 }
1923 }
1924
1925 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1926
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001927#ifdef FEAT_VIRTUALEDIT
1928setmarks:
1929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930#ifdef FEAT_VISUAL
1931 if (oap->block_mode)
1932 {
1933 curbuf->b_op_end.lnum = oap->end.lnum;
1934 curbuf->b_op_end.col = oap->start.col;
1935 }
1936 else
1937#endif
1938 curbuf->b_op_end = oap->start;
1939 curbuf->b_op_start = oap->start;
1940
1941 return OK;
1942}
1943
1944#ifdef FEAT_MBYTE
1945/*
1946 * Adjust end of operating area for ending on a multi-byte character.
1947 * Used for deletion.
1948 */
1949 static void
1950mb_adjust_opend(oap)
1951 oparg_T *oap;
1952{
1953 char_u *p;
1954
1955 if (oap->inclusive)
1956 {
1957 p = ml_get(oap->end.lnum);
1958 oap->end.col += mb_tail_off(p, p + oap->end.col);
1959 }
1960}
1961#endif
1962
1963#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1964/*
1965 * Replace a whole area with one character.
1966 */
1967 int
1968op_replace(oap, c)
1969 oparg_T *oap;
1970 int c;
1971{
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;
1979
1980 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1981 return OK; /* nothing to do */
1982
1983#ifdef FEAT_MBYTE
1984 if (has_mbyte)
1985 mb_adjust_opend(oap);
1986#endif
1987
1988 if (u_save((linenr_T)(oap->start.lnum - 1),
1989 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1990 return FAIL;
1991
1992 /*
1993 * block mode replace
1994 */
1995 if (oap->block_mode)
1996 {
1997 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1998 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1999 {
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 */
2003
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!
2007 */
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)
2012 {
2013 pos_T vpos;
2014
2015 getvpos(&vpos, oap->start_vcol);
2016 bd.startspaces += vpos.coladd;
2017 n = bd.startspaces;
2018 }
2019 else
2020#endif
2021 /* allow for pre spaces */
2022 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2023
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;
2034
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)
2039 {
2040 if ((numc & 1) && !bd.is_short)
2041 {
2042 ++bd.endspaces;
2043 ++n;
2044 }
2045 numc = numc / 2;
2046 }
2047
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;
2054
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)
2069 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002070 n = (int)STRLEN(newp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 while (--num_chars >= 0)
2072 n += (*mb_char2bytes)(c, newp + n);
2073 }
2074 else
2075#endif
2076 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2077 if (!bd.is_short)
2078 {
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);
2083 }
2084 /* replace the line */
2085 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2086 }
2087 }
2088 else
2089 {
2090 /*
2091 * MCHAR and MLINE motion replace.
2092 */
2093 if (oap->motion_type == MLINE)
2094 {
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;
2100 }
2101 else if (!oap->inclusive)
2102 dec(&(oap->end));
2103
2104 while (ltoreq(curwin->w_cursor, oap->end))
2105 {
2106 n = gchar_cursor();
2107 if (n != NUL)
2108 {
2109#ifdef FEAT_MBYTE
2110 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2111 {
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();
2121 }
2122 else
2123#endif
2124 {
2125#ifdef FEAT_VIRTUALEDIT
2126 if (n == TAB)
2127 {
2128 int end_vcol = 0;
2129
2130 if (curwin->w_cursor.lnum == oap->end.lnum)
2131 {
2132 /* oap->end has to be recalculated when
2133 * the tab breaks */
2134 end_vcol = getviscol2(oap->end.col,
2135 oap->end.coladd);
2136 }
2137 coladvance_force(getviscol());
2138 if (curwin->w_cursor.lnum == oap->end.lnum)
2139 getvpos(&oap->end, end_vcol);
2140 }
2141#endif
2142 pchar(curwin->w_cursor, c);
2143 }
2144 }
2145#ifdef FEAT_VIRTUALEDIT
2146 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2147 {
2148 int virtcols = oap->end.coladd;
2149
2150 if (curwin->w_cursor.lnum == oap->start.lnum
2151 && oap->start.col == oap->end.col && oap->start.coladd)
2152 virtcols -= oap->start.coladd;
2153
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--)
2160 {
2161 pchar(curwin->w_cursor, c);
2162 if (inc(&curwin->w_cursor) == -1)
2163 break;
2164 }
2165 }
2166#endif
2167
2168 /* Advance to next character, stop at the end of the file. */
2169 if (inc_cursor() == -1)
2170 break;
2171 }
2172 }
2173
2174 curwin->w_cursor = oap->start;
2175 check_cursor();
2176 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2177
2178 /* Set "'[" and "']" marks. */
2179 curbuf->b_op_start = oap->start;
2180 curbuf->b_op_end = oap->end;
2181
2182 return OK;
2183}
2184#endif
2185
2186/*
2187 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2188 */
2189 void
2190op_tilde(oap)
2191 oparg_T *oap;
2192{
2193 pos_T pos;
2194#ifdef FEAT_VISUAL
2195 struct block_def bd;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002196 int todo;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 int did_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200 if (u_save((linenr_T)(oap->start.lnum - 1),
2201 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2202 return;
2203
2204 pos = oap->start;
2205#ifdef FEAT_VISUAL
2206 if (oap->block_mode) /* Visual block mode */
2207 {
2208 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2209 {
2210 block_prep(oap, &bd, pos.lnum, FALSE);
2211 pos.col = bd.textcol;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002212 for (todo = bd.textlen; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002214# ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002215 if (has_mbyte)
2216 todo -= (*mb_ptr2len)(ml_get_pos(&pos)) - 1;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 did_change |= swapchar(oap->op_type, &pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (inc(&pos) == -1) /* at end of file */
2220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222# ifdef FEAT_NETBEANS_INTG
2223 if (usingNetbeans && did_change)
2224 {
2225 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2226
Bram Moolenaar009b2592004-10-24 19:18:58 +00002227 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2228 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002230 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232# endif
2233 }
2234 if (did_change)
2235 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2236 }
2237 else /* not block mode */
2238#endif
2239 {
2240 if (oap->motion_type == MLINE)
2241 {
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;
2247 }
2248 else if (!oap->inclusive)
2249 dec(&(oap->end));
2250
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002251 while (ltoreq(pos, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
2253 did_change |= swapchar(oap->op_type, &pos);
2254 if (inc(&pos) == -1) /* at end of file */
2255 break;
2256 }
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (did_change)
2259 {
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)
2264 {
2265 char_u *ptr;
2266 int count;
2267
2268 pos = oap->start;
2269 while (pos.lnum < oap->end.lnum)
2270 {
2271 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002272 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002273 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002275 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 pos.col = 0;
2277 pos.lnum++;
2278 }
2279 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2280 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002281 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002283 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285#endif
2286 }
2287 }
2288
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
2294
2295 /*
2296 * Set '[ and '] marks.
2297 */
2298 curbuf->b_op_start = oap->start;
2299 curbuf->b_op_end = oap->end;
2300
2301 if (oap->line_count > p_report)
2302 {
2303 if (oap->line_count == 1)
2304 MSG(_("1 line changed"));
2305 else
2306 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2307 }
2308}
2309
2310/*
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.
2316 */
2317 int
2318swapchar(op_type, pos)
2319 int op_type;
2320 pos_T *pos;
2321{
2322 int c;
2323 int nc;
2324
2325 c = gchar_pos(pos);
2326
2327 /* Only do rot13 encoding for ASCII characters. */
2328 if (c >= 0x80 && op_type == OP_ROT13)
2329 return FALSE;
2330
2331#ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002332 if (op_type == OP_UPPER && enc_latin1like && c == 0xdf)
2333 {
2334 pos_T sp = curwin->w_cursor;
2335
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);
2343 }
2344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2346 return FALSE;
2347#endif
2348 nc = c;
2349 if (MB_ISLOWER(c))
2350 {
2351 if (op_type == OP_ROT13)
2352 nc = ROT13(c, 'a');
2353 else if (op_type != OP_LOWER)
2354 nc = MB_TOUPPER(c);
2355 }
2356 else if (MB_ISUPPER(c))
2357 {
2358 if (op_type == OP_ROT13)
2359 nc = ROT13(c, 'A');
2360 else if (op_type != OP_UPPER)
2361 nc = MB_TOLOWER(c);
2362 }
2363 if (nc != c)
2364 {
2365#ifdef FEAT_MBYTE
2366 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2367 {
2368 pos_T sp = curwin->w_cursor;
2369
2370 curwin->w_cursor = *pos;
2371 del_char(FALSE);
2372 ins_char(nc);
2373 curwin->w_cursor = sp;
2374 }
2375 else
2376#endif
2377 pchar(*pos, nc);
2378 return TRUE;
2379 }
2380 return FALSE;
2381}
2382
2383#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2384/*
2385 * op_insert - Insert and append operators for Visual mode.
2386 */
2387 void
2388op_insert(oap, count1)
2389 oparg_T *oap;
2390 long count1;
2391{
2392 long ins_len, pre_textlen = 0;
2393 char_u *firstline, *ins_text;
2394 struct block_def bd;
2395 int i;
2396
2397 /* edit() changes this - record it for OP_APPEND */
2398 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2399
2400 /* vis block is still marked. Get rid of it now. */
2401 curwin->w_cursor.lnum = oap->start.lnum;
2402 update_screen(INVERTED);
2403
2404 if (oap->block_mode)
2405 {
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)
2412 {
2413 int old_ve_flags = ve_flags;
2414
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;
2423 }
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);
2431 }
2432
2433 if (oap->op_type == OP_APPEND)
2434 {
2435 if (oap->block_mode
2436#ifdef FEAT_VIRTUALEDIT
2437 && curwin->w_cursor.coladd == 0
2438#endif
2439 )
2440 {
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)
2447 {
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;
2455 }
2456 }
2457 else
2458 {
2459 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002460 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
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();
2466 }
2467 }
2468
2469 edit(NUL, FALSE, (linenr_T)count1);
2470
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002471 /* If user has moved off this line, we don't know what to do, so do
2472 * nothing.
2473 * Also don't repeat the insert when Insert mode ended with CTRL-C. */
2474 if (curwin->w_cursor.lnum != oap->start.lnum || got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 return;
2476
2477 if (oap->block_mode)
2478 {
2479 struct block_def bd2;
2480
2481 /*
2482 * Spaces and tabs in the indent may have changed to other spaces and
Bram Moolenaar53241da2007-09-13 20:41:32 +00002483 * tabs. Get the starting column again and correct the length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 * Don't do this when "$" used, end-of-line will have changed.
2485 */
2486 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2487 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2488 {
2489 if (oap->op_type == OP_APPEND)
2490 {
2491 pre_textlen += bd2.textlen - bd.textlen;
2492 if (bd2.endspaces)
2493 --bd2.textlen;
2494 }
2495 bd.textcol = bd2.textcol;
2496 bd.textlen = bd2.textlen;
2497 }
2498
2499 /*
2500 * Subsequent calls to ml_get() flush the firstline data - take a
2501 * copy of the required string.
2502 */
2503 firstline = ml_get(oap->start.lnum) + bd.textcol;
2504 if (oap->op_type == OP_APPEND)
2505 firstline += bd.textlen;
2506 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2507 {
2508 ins_text = vim_strnsave(firstline, (int)ins_len);
2509 if (ins_text != NULL)
2510 {
2511 /* block handled here */
2512 if (u_save(oap->start.lnum,
2513 (linenr_T)(oap->end.lnum + 1)) == OK)
2514 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2515 &bd);
2516
2517 curwin->w_cursor.col = oap->start.col;
2518 check_cursor();
2519 vim_free(ins_text);
2520 }
2521 }
2522 }
2523}
2524#endif
2525
2526/*
2527 * op_change - handle a change operation
2528 *
2529 * return TRUE if edit() returns because of a CTRL-O command
2530 */
2531 int
2532op_change(oap)
2533 oparg_T *oap;
2534{
2535 colnr_T l;
2536 int retval;
2537#ifdef FEAT_VISUALEXTRA
2538 long offset;
2539 linenr_T linenr;
Bram Moolenaar53241da2007-09-13 20:41:32 +00002540 long ins_len;
2541 long pre_textlen = 0;
2542 long pre_indent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 char_u *firstline;
2544 char_u *ins_text, *newp, *oldp;
2545 struct block_def bd;
2546#endif
2547
2548 l = oap->start.col;
2549 if (oap->motion_type == MLINE)
2550 {
2551 l = 0;
2552#ifdef FEAT_SMARTINDENT
2553 if (!p_paste && curbuf->b_p_si
2554# ifdef FEAT_CINDENT
2555 && !curbuf->b_p_cin
2556# endif
2557 )
2558 can_si = TRUE; /* It's like opening a new line, do si */
2559#endif
2560 }
2561
2562 /* First delete the text in the region. In an empty buffer only need to
2563 * save for undo */
2564 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2565 {
2566 if (u_save_cursor() == FAIL)
2567 return FALSE;
2568 }
2569 else if (op_delete(oap) == FAIL)
2570 return FALSE;
2571
2572 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2573 && !virtual_op)
2574 inc_cursor();
2575
2576#ifdef FEAT_VISUALEXTRA
2577 /* check for still on same line (<CR> in inserted text meaningless) */
2578 /* skip blank lines too */
2579 if (oap->block_mode)
2580 {
2581# ifdef FEAT_VIRTUALEDIT
2582 /* Add spaces before getting the current line length. */
2583 if (virtual_op && (curwin->w_cursor.coladd > 0
2584 || gchar_cursor() == NUL))
2585 coladvance_force(getviscol());
2586# endif
Bram Moolenaar53241da2007-09-13 20:41:32 +00002587 firstline = ml_get(oap->start.lnum);
2588 pre_textlen = (long)STRLEN(firstline);
2589 pre_indent = (long)(skipwhite(firstline) - firstline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 bd.textcol = curwin->w_cursor.col;
2591 }
2592#endif
2593
2594#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2595 if (oap->motion_type == MLINE)
2596 fix_indent();
2597#endif
2598
2599 retval = edit(NUL, FALSE, (linenr_T)1);
2600
2601#ifdef FEAT_VISUALEXTRA
2602 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002603 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 * block.
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002605 * Don't repeat the insert when Insert mode ended with CTRL-C.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 */
Bram Moolenaaraacbb002008-01-03 15:34:40 +00002607 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002609 /* Auto-indenting may have changed the indent. If the cursor was past
2610 * the indent, exclude that indent change from the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 firstline = ml_get(oap->start.lnum);
Bram Moolenaarb8dc4d42007-09-25 12:20:19 +00002612 if (bd.textcol > (colnr_T)pre_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Bram Moolenaar53241da2007-09-13 20:41:32 +00002614 long new_indent = (long)(skipwhite(firstline) - firstline);
2615
2616 pre_textlen += new_indent - pre_indent;
2617 bd.textcol += new_indent - pre_indent;
2618 }
2619
2620 ins_len = (long)STRLEN(firstline) - pre_textlen;
2621 if (ins_len > 0)
2622 {
2623 /* Subsequent calls to ml_get() flush the firstline data - take a
2624 * copy of the inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2626 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002627 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2629 linenr++)
2630 {
2631 block_prep(oap, &bd, linenr, TRUE);
2632 if (!bd.is_short || virtual_op)
2633 {
2634# ifdef FEAT_VIRTUALEDIT
2635 pos_T vpos;
2636
2637 /* If the block starts in virtual space, count the
2638 * initial coladd offset as part of "startspaces" */
2639 if (bd.is_short)
2640 {
2641 linenr_T lnum = curwin->w_cursor.lnum;
2642
2643 curwin->w_cursor.lnum = linenr;
2644 (void)getvpos(&vpos, oap->start_vcol);
2645 curwin->w_cursor.lnum = lnum;
2646 }
2647 else
2648 vpos.coladd = 0;
2649# endif
2650 oldp = ml_get(linenr);
2651 newp = alloc_check((unsigned)(STRLEN(oldp)
2652# ifdef FEAT_VIRTUALEDIT
2653 + vpos.coladd
2654# endif
2655 + ins_len + 1));
2656 if (newp == NULL)
2657 continue;
2658 /* copy up to block start */
2659 mch_memmove(newp, oldp, (size_t)bd.textcol);
2660 offset = bd.textcol;
2661# ifdef FEAT_VIRTUALEDIT
2662 copy_spaces(newp + offset, (size_t)vpos.coladd);
2663 offset += vpos.coladd;
2664# endif
2665 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2666 offset += ins_len;
2667 oldp += bd.textcol;
2668 mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2669 ml_replace(linenr, newp, FALSE);
2670 }
2671 }
2672 check_cursor();
2673
2674 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2675 }
2676 vim_free(ins_text);
2677 }
2678 }
2679#endif
2680
2681 return retval;
2682}
2683
2684/*
2685 * set all the yank registers to empty (called from main())
2686 */
2687 void
2688init_yank()
2689{
2690 int i;
2691
2692 for (i = 0; i < NUM_REGISTERS; ++i)
2693 y_regs[i].y_array = NULL;
2694}
2695
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002696#if defined(EXITFREE) || defined(PROTO)
2697 void
2698clear_registers()
2699{
2700 int i;
2701
2702 for (i = 0; i < NUM_REGISTERS; ++i)
2703 {
2704 y_current = &y_regs[i];
2705 if (y_current->y_array != NULL)
2706 free_yank_all();
2707 }
2708}
2709#endif
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711/*
2712 * Free "n" lines from the current yank register.
2713 * Called for normal freeing and in case of error.
2714 */
2715 static void
2716free_yank(n)
2717 long n;
2718{
2719 if (y_current->y_array != NULL)
2720 {
2721 long i;
2722
2723 for (i = n; --i >= 0; )
2724 {
2725#ifdef AMIGA /* only for very slow machines */
2726 if ((i & 1023) == 1023) /* this may take a while */
2727 {
2728 /*
2729 * This message should never cause a hit-return message.
2730 * Overwrite this message with any next message.
2731 */
2732 ++no_wait_return;
2733 smsg((char_u *)_("freeing %ld lines"), i + 1);
2734 --no_wait_return;
2735 msg_didout = FALSE;
2736 msg_col = 0;
2737 }
2738#endif
2739 vim_free(y_current->y_array[i]);
2740 }
2741 vim_free(y_current->y_array);
2742 y_current->y_array = NULL;
2743#ifdef AMIGA
2744 if (n >= 1000)
2745 MSG("");
2746#endif
2747 }
2748}
2749
2750 static void
2751free_yank_all()
2752{
2753 free_yank(y_current->y_size);
2754}
2755
2756/*
2757 * Yank the text between "oap->start" and "oap->end" into a yank register.
2758 * If we are to append (uppercase register), we first yank into a new yank
2759 * register and then concatenate the old and the new one (so we keep the old
2760 * one in case of out-of-memory).
2761 *
2762 * return FAIL for failure, OK otherwise
2763 */
2764 int
2765op_yank(oap, deleting, mess)
2766 oparg_T *oap;
2767 int deleting;
2768 int mess;
2769{
2770 long y_idx; /* index in y_array[] */
2771 struct yankreg *curr; /* copy of y_current */
2772 struct yankreg newreg; /* new yank register when appending */
2773 char_u **new_ptr;
2774 linenr_T lnum; /* current line number */
2775 long j;
2776 int yanktype = oap->motion_type;
2777 long yanklines = oap->line_count;
2778 linenr_T yankendlnum = oap->end.lnum;
2779 char_u *p;
2780 char_u *pnew;
2781 struct block_def bd;
2782
2783 /* check for read-only register */
2784 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2785 {
2786 beep_flush();
2787 return FAIL;
2788 }
2789 if (oap->regname == '_') /* black hole: nothing to do */
2790 return OK;
2791
2792#ifdef FEAT_CLIPBOARD
2793 if (!clip_star.available && oap->regname == '*')
2794 oap->regname = 0;
2795 else if (!clip_plus.available && oap->regname == '+')
2796 oap->regname = 0;
2797#endif
2798
2799 if (!deleting) /* op_delete() already set y_current */
2800 get_yank_register(oap->regname, TRUE);
2801
2802 curr = y_current;
2803 /* append to existing contents */
2804 if (y_append && y_current->y_array != NULL)
2805 y_current = &newreg;
2806 else
2807 free_yank_all(); /* free previously yanked lines */
2808
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002809 /*
2810 * If the cursor was in column 1 before and after the movement, and the
2811 * operator is not inclusive, the yank is always linewise.
2812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 if ( oap->motion_type == MCHAR
2814 && oap->start.col == 0
2815 && !oap->inclusive
2816#ifdef FEAT_VISUAL
2817 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002818 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#endif
2820 && oap->end.col == 0
2821 && yanklines > 1)
2822 {
2823 yanktype = MLINE;
2824 --yankendlnum;
2825 --yanklines;
2826 }
2827
2828 y_current->y_size = yanklines;
2829 y_current->y_type = yanktype; /* set the yank register type */
2830#ifdef FEAT_VISUAL
2831 y_current->y_width = 0;
2832#endif
2833 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2834 yanklines), TRUE);
2835
2836 if (y_current->y_array == NULL)
2837 {
2838 y_current = curr;
2839 return FAIL;
2840 }
2841
2842 y_idx = 0;
2843 lnum = oap->start.lnum;
2844
2845#ifdef FEAT_VISUAL
2846 if (oap->block_mode)
2847 {
2848 /* Visual block mode */
2849 y_current->y_type = MBLOCK; /* set the yank register type */
2850 y_current->y_width = oap->end_vcol - oap->start_vcol;
2851
2852 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2853 y_current->y_width--;
2854 }
2855#endif
2856
2857 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2858 {
2859 switch (y_current->y_type)
2860 {
2861#ifdef FEAT_VISUAL
2862 case MBLOCK:
2863 block_prep(oap, &bd, lnum, FALSE);
2864 if (yank_copy_line(&bd, y_idx) == FAIL)
2865 goto fail;
2866 break;
2867#endif
2868
2869 case MLINE:
2870 if ((y_current->y_array[y_idx] =
2871 vim_strsave(ml_get(lnum))) == NULL)
2872 goto fail;
2873 break;
2874
2875 case MCHAR:
2876 {
2877 colnr_T startcol = 0, endcol = MAXCOL;
2878#ifdef FEAT_VIRTUALEDIT
2879 int is_oneChar = FALSE;
2880 colnr_T cs, ce;
2881#endif
2882 p = ml_get(lnum);
2883 bd.startspaces = 0;
2884 bd.endspaces = 0;
2885
2886 if (lnum == oap->start.lnum)
2887 {
2888 startcol = oap->start.col;
2889#ifdef FEAT_VIRTUALEDIT
2890 if (virtual_op)
2891 {
2892 getvcol(curwin, &oap->start, &cs, NULL, &ce);
2893 if (ce != cs && oap->start.coladd > 0)
2894 {
2895 /* Part of a tab selected -- but don't
2896 * double-count it. */
2897 bd.startspaces = (ce - cs + 1)
2898 - oap->start.coladd;
2899 startcol++;
2900 }
2901 }
2902#endif
2903 }
2904
2905 if (lnum == oap->end.lnum)
2906 {
2907 endcol = oap->end.col;
2908#ifdef FEAT_VIRTUALEDIT
2909 if (virtual_op)
2910 {
2911 getvcol(curwin, &oap->end, &cs, NULL, &ce);
2912 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2913# ifdef FEAT_MBYTE
2914 /* Don't add space for double-wide
2915 * char; endcol will be on last byte
2916 * of multi-byte char. */
2917 && (*mb_head_off)(p, p + endcol) == 0
2918# endif
2919 ))
2920 {
2921 if (oap->start.lnum == oap->end.lnum
2922 && oap->start.col == oap->end.col)
2923 {
2924 /* Special case: inside a single char */
2925 is_oneChar = TRUE;
2926 bd.startspaces = oap->end.coladd
2927 - oap->start.coladd + oap->inclusive;
2928 endcol = startcol;
2929 }
2930 else
2931 {
2932 bd.endspaces = oap->end.coladd
2933 + oap->inclusive;
2934 endcol -= oap->inclusive;
2935 }
2936 }
2937 }
2938#endif
2939 }
2940 if (startcol > endcol
2941#ifdef FEAT_VIRTUALEDIT
2942 || is_oneChar
2943#endif
2944 )
2945 bd.textlen = 0;
2946 else
2947 {
2948 if (endcol == MAXCOL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002949 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 bd.textlen = endcol - startcol + oap->inclusive;
2951 }
2952 bd.textstart = p + startcol;
2953 if (yank_copy_line(&bd, y_idx) == FAIL)
2954 goto fail;
2955 break;
2956 }
2957 /* NOTREACHED */
2958 }
2959 }
2960
2961 if (curr != y_current) /* append the new block to the old block */
2962 {
2963 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2964 (curr->y_size + y_current->y_size)), TRUE);
2965 if (new_ptr == NULL)
2966 goto fail;
2967 for (j = 0; j < curr->y_size; ++j)
2968 new_ptr[j] = curr->y_array[j];
2969 vim_free(curr->y_array);
2970 curr->y_array = new_ptr;
2971
2972 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
2973 curr->y_type = MLINE;
2974
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002975 /* Concatenate the last line of the old block with the first line of
2976 * the new block, unless being Vi compatible. */
2977 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
2979 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2980 + STRLEN(y_current->y_array[0]) + 1), TRUE);
2981 if (pnew == NULL)
2982 {
2983 y_idx = y_current->y_size - 1;
2984 goto fail;
2985 }
2986 STRCPY(pnew, curr->y_array[--j]);
2987 STRCAT(pnew, y_current->y_array[0]);
2988 vim_free(curr->y_array[j]);
2989 vim_free(y_current->y_array[0]);
2990 curr->y_array[j++] = pnew;
2991 y_idx = 1;
2992 }
2993 else
2994 y_idx = 0;
2995 while (y_idx < y_current->y_size)
2996 curr->y_array[j++] = y_current->y_array[y_idx++];
2997 curr->y_size = j;
2998 vim_free(y_current->y_array);
2999 y_current = curr;
3000 }
3001 if (mess) /* Display message about yank? */
3002 {
3003 if (yanktype == MCHAR
3004#ifdef FEAT_VISUAL
3005 && !oap->block_mode
3006#endif
3007 && yanklines == 1)
3008 yanklines = 0;
3009 /* Some versions of Vi use ">=" here, some don't... */
3010 if (yanklines > p_report)
3011 {
3012 /* redisplay now, so message is not deleted */
3013 update_topline_redraw();
3014 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003015 {
3016#ifdef FEAT_VISUAL
3017 if (oap->block_mode)
3018 MSG(_("block of 1 line yanked"));
3019 else
3020#endif
3021 MSG(_("1 line yanked"));
3022 }
3023#ifdef FEAT_VISUAL
3024 else if (oap->block_mode)
3025 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 else
3028 smsg((char_u *)_("%ld lines yanked"), yanklines);
3029 }
3030 }
3031
3032 /*
3033 * Set "'[" and "']" marks.
3034 */
3035 curbuf->b_op_start = oap->start;
3036 curbuf->b_op_end = oap->end;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003037 if (yanktype == MLINE
3038#ifdef FEAT_VISUAL
3039 && !oap->block_mode
3040#endif
3041 )
3042 {
3043 curbuf->b_op_start.col = 0;
3044 curbuf->b_op_end.col = MAXCOL;
3045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
3047#ifdef FEAT_CLIPBOARD
3048 /*
3049 * If we were yanking to the '*' register, send result to clipboard.
3050 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3051 * to the '*' register.
3052 */
3053 if (clip_star.available
3054 && (curr == &(y_regs[STAR_REGISTER])
3055 || (!deleting && oap->regname == 0 && clip_unnamed)))
3056 {
3057 if (curr != &(y_regs[STAR_REGISTER]))
3058 /* Copy the text from register 0 to the clipboard register. */
3059 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3060
3061 clip_own_selection(&clip_star);
3062 clip_gen_set_selection(&clip_star);
3063 }
3064
3065# ifdef FEAT_X11
3066 /*
3067 * If we were yanking to the '+' register, send result to selection.
3068 * Also copy to the '*' register, in case auto-select is off.
3069 */
3070 else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
3071 {
3072 /* No need to copy to * register upon 'unnamed' now - see below */
3073 clip_own_selection(&clip_plus);
3074 clip_gen_set_selection(&clip_plus);
3075 if (!clip_isautosel())
3076 {
3077 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3078 clip_own_selection(&clip_star);
3079 clip_gen_set_selection(&clip_star);
3080 }
3081 }
3082# endif
3083#endif
3084
3085 return OK;
3086
3087fail: /* free the allocated lines */
3088 free_yank(y_idx + 1);
3089 y_current = curr;
3090 return FAIL;
3091}
3092
3093 static int
3094yank_copy_line(bd, y_idx)
3095 struct block_def *bd;
3096 long y_idx;
3097{
3098 char_u *pnew;
3099
3100 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3101 == NULL)
3102 return FAIL;
3103 y_current->y_array[y_idx] = pnew;
3104 copy_spaces(pnew, (size_t)bd->startspaces);
3105 pnew += bd->startspaces;
3106 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3107 pnew += bd->textlen;
3108 copy_spaces(pnew, (size_t)bd->endspaces);
3109 pnew += bd->endspaces;
3110 *pnew = NUL;
3111 return OK;
3112}
3113
3114#ifdef FEAT_CLIPBOARD
3115/*
3116 * Make a copy of the y_current register to register "reg".
3117 */
3118 static void
3119copy_yank_reg(reg)
3120 struct yankreg *reg;
3121{
3122 struct yankreg *curr = y_current;
3123 long j;
3124
3125 y_current = reg;
3126 free_yank_all();
3127 *y_current = *curr;
3128 y_current->y_array = (char_u **)lalloc_clear(
3129 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3130 if (y_current->y_array == NULL)
3131 y_current->y_size = 0;
3132 else
3133 for (j = 0; j < y_current->y_size; ++j)
3134 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3135 {
3136 free_yank(j);
3137 y_current->y_size = 0;
3138 break;
3139 }
3140 y_current = curr;
3141}
3142#endif
3143
3144/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003145 * Put contents of register "regname" into the text.
3146 * Caller must check "regname" to be valid!
3147 * "flags": PUT_FIXINDENT make indent look nice
3148 * PUT_CURSEND leave cursor after end of new text
3149 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
3151 void
3152do_put(regname, dir, count, flags)
3153 int regname;
3154 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3155 long count;
3156 int flags;
3157{
3158 char_u *ptr;
3159 char_u *newp, *oldp;
3160 int yanklen;
3161 int totlen = 0; /* init for gcc */
3162 linenr_T lnum;
3163 colnr_T col;
3164 long i; /* index in y_array[] */
3165 int y_type;
3166 long y_size;
3167#ifdef FEAT_VISUAL
3168 int oldlen;
3169 long y_width = 0;
3170 colnr_T vcol;
3171 int delcount;
3172 int incr = 0;
3173 long j;
3174 struct block_def bd;
3175#endif
3176 char_u **y_array = NULL;
3177 long nr_lines = 0;
3178 pos_T new_cursor;
3179 int indent;
3180 int orig_indent = 0; /* init for gcc */
3181 int indent_diff = 0; /* init for gcc */
3182 int first_indent = TRUE;
3183 int lendiff = 0;
3184 pos_T old_pos;
3185 char_u *insert_string = NULL;
3186 int allocated = FALSE;
3187 long cnt;
3188
3189#ifdef FEAT_CLIPBOARD
3190 /* Adjust register name for "unnamed" in 'clipboard'. */
3191 adjust_clip_reg(&regname);
3192 (void)may_get_selection(regname);
3193#endif
3194
3195 if (flags & PUT_FIXINDENT)
3196 orig_indent = get_indent();
3197
3198 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3199 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3200
3201 /*
3202 * Using inserted text works differently, because the register includes
3203 * special characters (newlines, etc.).
3204 */
3205 if (regname == '.')
3206 {
3207 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3208 (count == -1 ? 'O' : 'i')), count, FALSE);
3209 /* Putting the text is done later, so can't really move the cursor to
3210 * the next character. Use "l" to simulate it. */
3211 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3212 stuffcharReadbuff('l');
3213 return;
3214 }
3215
3216 /*
3217 * For special registers '%' (file name), '#' (alternate file name) and
3218 * ':' (last command line), etc. we have to create a fake yank register.
3219 */
3220 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3221 {
3222 if (insert_string == NULL)
3223 return;
3224 }
3225
3226 if (insert_string != NULL)
3227 {
3228 y_type = MCHAR;
3229#ifdef FEAT_EVAL
3230 if (regname == '=')
3231 {
3232 /* For the = register we need to split the string at NL
3233 * characters. */
3234 /* Loop twice: count the number of lines and save them. */
3235 for (;;)
3236 {
3237 y_size = 0;
3238 ptr = insert_string;
3239 while (ptr != NULL)
3240 {
3241 if (y_array != NULL)
3242 y_array[y_size] = ptr;
3243 ++y_size;
3244 ptr = vim_strchr(ptr, '\n');
3245 if (ptr != NULL)
3246 {
3247 if (y_array != NULL)
3248 *ptr = NUL;
3249 ++ptr;
3250 /* A trailing '\n' makes the string linewise */
3251 if (*ptr == NUL)
3252 {
3253 y_type = MLINE;
3254 break;
3255 }
3256 }
3257 }
3258 if (y_array != NULL)
3259 break;
3260 y_array = (char_u **)alloc((unsigned)
3261 (y_size * sizeof(char_u *)));
3262 if (y_array == NULL)
3263 goto end;
3264 }
3265 }
3266 else
3267#endif
3268 {
3269 y_size = 1; /* use fake one-line yank register */
3270 y_array = &insert_string;
3271 }
3272 }
3273 else
3274 {
3275 get_yank_register(regname, FALSE);
3276
3277 y_type = y_current->y_type;
3278#ifdef FEAT_VISUAL
3279 y_width = y_current->y_width;
3280#endif
3281 y_size = y_current->y_size;
3282 y_array = y_current->y_array;
3283 }
3284
3285#ifdef FEAT_VISUAL
3286 if (y_type == MLINE)
3287 {
3288 if (flags & PUT_LINE_SPLIT)
3289 {
3290 /* "p" or "P" in Visual mode: split the lines to put the text in
3291 * between. */
3292 if (u_save_cursor() == FAIL)
3293 goto end;
3294 ptr = vim_strsave(ml_get_cursor());
3295 if (ptr == NULL)
3296 goto end;
3297 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3298 vim_free(ptr);
3299
3300 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3301 if (ptr == NULL)
3302 goto end;
3303 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3304 ++nr_lines;
3305 dir = FORWARD;
3306 }
3307 if (flags & PUT_LINE_FORWARD)
3308 {
3309 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003310 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 dir = FORWARD;
3312 }
3313 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3314 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3315 }
3316#endif
3317
3318 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3319 y_type = MLINE;
3320
3321 if (y_size == 0 || y_array == NULL)
3322 {
3323 EMSG2(_("E353: Nothing in register %s"),
3324 regname == 0 ? (char_u *)"\"" : transchar(regname));
3325 goto end;
3326 }
3327
3328#ifdef FEAT_VISUAL
3329 if (y_type == MBLOCK)
3330 {
3331 lnum = curwin->w_cursor.lnum + y_size + 1;
3332 if (lnum > curbuf->b_ml.ml_line_count)
3333 lnum = curbuf->b_ml.ml_line_count + 1;
3334 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3335 goto end;
3336 }
3337 else
3338#endif
3339 if (y_type == MLINE)
3340 {
3341 lnum = curwin->w_cursor.lnum;
3342#ifdef FEAT_FOLDING
3343 /* Correct line number for closed fold. Don't move the cursor yet,
3344 * u_save() uses it. */
3345 if (dir == BACKWARD)
3346 (void)hasFolding(lnum, &lnum, NULL);
3347 else
3348 (void)hasFolding(lnum, NULL, &lnum);
3349#endif
3350 if (dir == FORWARD)
3351 ++lnum;
3352 if (u_save(lnum - 1, lnum) == FAIL)
3353 goto end;
3354#ifdef FEAT_FOLDING
3355 if (dir == FORWARD)
3356 curwin->w_cursor.lnum = lnum - 1;
3357 else
3358 curwin->w_cursor.lnum = lnum;
3359 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3360#endif
3361 }
3362 else if (u_save_cursor() == FAIL)
3363 goto end;
3364
3365 yanklen = (int)STRLEN(y_array[0]);
3366
3367#ifdef FEAT_VIRTUALEDIT
3368 if (ve_flags == VE_ALL && y_type == MCHAR)
3369 {
3370 if (gchar_cursor() == TAB)
3371 {
3372 /* Don't need to insert spaces when "p" on the last position of a
3373 * tab or "P" on the first position. */
3374 if (dir == FORWARD
3375 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3376 : curwin->w_cursor.coladd > 0)
3377 coladvance_force(getviscol());
3378 else
3379 curwin->w_cursor.coladd = 0;
3380 }
3381 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3382 coladvance_force(getviscol() + (dir == FORWARD));
3383 }
3384#endif
3385
3386 lnum = curwin->w_cursor.lnum;
3387 col = curwin->w_cursor.col;
3388
3389#ifdef FEAT_VISUAL
3390 /*
3391 * Block mode
3392 */
3393 if (y_type == MBLOCK)
3394 {
3395 char c = gchar_cursor();
3396 colnr_T endcol2 = 0;
3397
3398 if (dir == FORWARD && c != NUL)
3399 {
3400#ifdef FEAT_VIRTUALEDIT
3401 if (ve_flags == VE_ALL)
3402 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3403 else
3404#endif
3405 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3406
3407#ifdef FEAT_MBYTE
3408 if (has_mbyte)
3409 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003410 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else
3412#endif
3413#ifdef FEAT_VIRTUALEDIT
3414 if (c != TAB || ve_flags != VE_ALL)
3415#endif
3416 ++curwin->w_cursor.col;
3417 ++col;
3418 }
3419 else
3420 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3421
3422#ifdef FEAT_VIRTUALEDIT
3423 col += curwin->w_cursor.coladd;
Bram Moolenaare649ef02007-06-28 20:18:51 +00003424 if (ve_flags == VE_ALL
3425 && (curwin->w_cursor.coladd > 0
3426 || endcol2 == curwin->w_cursor.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
3428 if (dir == FORWARD && c == NUL)
3429 ++col;
3430 if (dir != FORWARD && c != NUL)
3431 ++curwin->w_cursor.col;
3432 if (c == TAB)
3433 {
3434 if (dir == BACKWARD && curwin->w_cursor.col)
3435 curwin->w_cursor.col--;
3436 if (dir == FORWARD && col - 1 == endcol2)
3437 curwin->w_cursor.col++;
3438 }
3439 }
3440 curwin->w_cursor.coladd = 0;
3441#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003442 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 for (i = 0; i < y_size; ++i)
3444 {
3445 int spaces;
3446 char shortline;
3447
3448 bd.startspaces = 0;
3449 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 vcol = 0;
3451 delcount = 0;
3452
3453 /* add a new line */
3454 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3455 {
3456 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3457 (colnr_T)1, FALSE) == FAIL)
3458 break;
3459 ++nr_lines;
3460 }
3461 /* get the old line and advance to the position to insert at */
3462 oldp = ml_get_curline();
3463 oldlen = (int)STRLEN(oldp);
3464 for (ptr = oldp; vcol < col && *ptr; )
3465 {
3466 /* Count a tab for what it's worth (if list mode not on) */
3467 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3468 vcol += incr;
3469 }
3470 bd.textcol = (colnr_T)(ptr - oldp);
3471
3472 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3473
3474 if (vcol < col) /* line too short, padd with spaces */
3475 bd.startspaces = col - vcol;
3476 else if (vcol > col)
3477 {
3478 bd.endspaces = vcol - col;
3479 bd.startspaces = incr - bd.endspaces;
3480 --bd.textcol;
3481 delcount = 1;
3482#ifdef FEAT_MBYTE
3483 if (has_mbyte)
3484 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3485#endif
3486 if (oldp[bd.textcol] != TAB)
3487 {
3488 /* Only a Tab can be split into spaces. Other
3489 * characters will have to be moved to after the
3490 * block, causing misalignment. */
3491 delcount = 0;
3492 bd.endspaces = 0;
3493 }
3494 }
3495
3496 yanklen = (int)STRLEN(y_array[i]);
3497
3498 /* calculate number of spaces required to fill right side of block*/
3499 spaces = y_width + 1;
3500 for (j = 0; j < yanklen; j++)
3501 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3502 if (spaces < 0)
3503 spaces = 0;
3504
3505 /* insert the new text */
3506 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3507 newp = alloc_check((unsigned)totlen + oldlen + 1);
3508 if (newp == NULL)
3509 break;
3510 /* copy part up to cursor to new line */
3511 ptr = newp;
3512 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3513 ptr += bd.textcol;
3514 /* may insert some spaces before the new text */
3515 copy_spaces(ptr, (size_t)bd.startspaces);
3516 ptr += bd.startspaces;
3517 /* insert the new text */
3518 for (j = 0; j < count; ++j)
3519 {
3520 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3521 ptr += yanklen;
3522
3523 /* insert block's trailing spaces only if there's text behind */
3524 if ((j < count - 1 || !shortline) && spaces)
3525 {
3526 copy_spaces(ptr, (size_t)spaces);
3527 ptr += spaces;
3528 }
3529 }
3530 /* may insert some spaces after the new text */
3531 copy_spaces(ptr, (size_t)bd.endspaces);
3532 ptr += bd.endspaces;
3533 /* move the text after the cursor to the end of the line. */
3534 mch_memmove(ptr, oldp + bd.textcol + delcount,
3535 (size_t)(oldlen - bd.textcol - delcount + 1));
3536 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3537
3538 ++curwin->w_cursor.lnum;
3539 if (i == 0)
3540 curwin->w_cursor.col += bd.startspaces;
3541 }
3542
3543 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3544
3545 /* Set '[ mark. */
3546 curbuf->b_op_start = curwin->w_cursor;
3547 curbuf->b_op_start.lnum = lnum;
3548
3549 /* adjust '] mark */
3550 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3551 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003552# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (flags & PUT_CURSEND)
3556 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003557 colnr_T len;
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 curwin->w_cursor = curbuf->b_op_end;
3560 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003561
3562 /* in Insert mode we might be after the NUL, correct for that */
3563 len = (colnr_T)STRLEN(ml_get_curline());
3564 if (curwin->w_cursor.col > len)
3565 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 else
3568 curwin->w_cursor.lnum = lnum;
3569 }
3570 else
3571#endif
3572 {
3573 /*
3574 * Character or Line mode
3575 */
3576 if (y_type == MCHAR)
3577 {
3578 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3579 * char */
3580 if (dir == FORWARD && gchar_cursor() != NUL)
3581 {
3582#ifdef FEAT_MBYTE
3583 if (has_mbyte)
3584 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003585 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 /* put it on the next of the multi-byte character. */
3588 col += bytelen;
3589 if (yanklen)
3590 {
3591 curwin->w_cursor.col += bytelen;
3592 curbuf->b_op_end.col += bytelen;
3593 }
3594 }
3595 else
3596#endif
3597 {
3598 ++col;
3599 if (yanklen)
3600 {
3601 ++curwin->w_cursor.col;
3602 ++curbuf->b_op_end.col;
3603 }
3604 }
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 curbuf->b_op_start = curwin->w_cursor;
3607 }
3608 /*
3609 * Line mode: BACKWARD is the same as FORWARD on the previous line
3610 */
3611 else if (dir == BACKWARD)
3612 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003613 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615 /*
3616 * simple case: insert into current line
3617 */
3618 if (y_type == MCHAR && y_size == 1)
3619 {
3620 totlen = count * yanklen;
3621 if (totlen)
3622 {
3623 oldp = ml_get(lnum);
3624 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3625 if (newp == NULL)
3626 goto end; /* alloc() will give error message */
3627 mch_memmove(newp, oldp, (size_t)col);
3628 ptr = newp + col;
3629 for (i = 0; i < count; ++i)
3630 {
3631 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3632 ptr += yanklen;
3633 }
3634 mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3635 ml_replace(lnum, newp, FALSE);
3636 /* Put cursor on last putted char. */
3637 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3638 }
3639 curbuf->b_op_end = curwin->w_cursor;
3640 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3641 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3642 ++curwin->w_cursor.col;
3643 changed_bytes(lnum, col);
3644 }
3645 else
3646 {
3647 /*
3648 * Insert at least one line. When y_type is MCHAR, break the first
3649 * line in two.
3650 */
3651 for (cnt = 1; cnt <= count; ++cnt)
3652 {
3653 i = 0;
3654 if (y_type == MCHAR)
3655 {
3656 /*
3657 * Split the current line in two at the insert position.
3658 * First insert y_array[size - 1] in front of second line.
3659 * Then append y_array[0] to first line.
3660 */
3661 lnum = new_cursor.lnum;
3662 ptr = ml_get(lnum) + col;
3663 totlen = (int)STRLEN(y_array[y_size - 1]);
3664 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3665 if (newp == NULL)
3666 goto error;
3667 STRCPY(newp, y_array[y_size - 1]);
3668 STRCAT(newp, ptr);
3669 /* insert second line */
3670 ml_append(lnum, newp, (colnr_T)0, FALSE);
3671 vim_free(newp);
3672
3673 oldp = ml_get(lnum);
3674 newp = alloc_check((unsigned)(col + yanklen + 1));
3675 if (newp == NULL)
3676 goto error;
3677 /* copy first part of line */
3678 mch_memmove(newp, oldp, (size_t)col);
3679 /* append to first line */
3680 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3681 ml_replace(lnum, newp, FALSE);
3682
3683 curwin->w_cursor.lnum = lnum;
3684 i = 1;
3685 }
3686
3687 for (; i < y_size; ++i)
3688 {
3689 if ((y_type != MCHAR || i < y_size - 1)
3690 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3691 == FAIL)
3692 goto error;
3693 lnum++;
3694 ++nr_lines;
3695 if (flags & PUT_FIXINDENT)
3696 {
3697 old_pos = curwin->w_cursor;
3698 curwin->w_cursor.lnum = lnum;
3699 ptr = ml_get(lnum);
3700 if (cnt == count && i == y_size - 1)
3701 lendiff = (int)STRLEN(ptr);
3702#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3703 if (*ptr == '#' && preprocs_left())
3704 indent = 0; /* Leave # lines at start */
3705 else
3706#endif
3707 if (*ptr == NUL)
3708 indent = 0; /* Ignore empty lines */
3709 else if (first_indent)
3710 {
3711 indent_diff = orig_indent - get_indent();
3712 indent = orig_indent;
3713 first_indent = FALSE;
3714 }
3715 else if ((indent = get_indent() + indent_diff) < 0)
3716 indent = 0;
3717 (void)set_indent(indent, 0);
3718 curwin->w_cursor = old_pos;
3719 /* remember how many chars were removed */
3720 if (cnt == count && i == y_size - 1)
3721 lendiff -= (int)STRLEN(ml_get(lnum));
3722 }
3723 }
3724 }
3725
3726error:
3727 /* Adjust marks. */
3728 if (y_type == MLINE)
3729 {
3730 curbuf->b_op_start.col = 0;
3731 if (dir == FORWARD)
3732 curbuf->b_op_start.lnum++;
3733 }
3734 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3735 (linenr_T)MAXLNUM, nr_lines, 0L);
3736
3737 /* note changed text for displaying and folding */
3738 if (y_type == MCHAR)
3739 changed_lines(curwin->w_cursor.lnum, col,
3740 curwin->w_cursor.lnum + 1, nr_lines);
3741 else
3742 changed_lines(curbuf->b_op_start.lnum, 0,
3743 curbuf->b_op_start.lnum, nr_lines);
3744
3745 /* put '] mark at last inserted character */
3746 curbuf->b_op_end.lnum = lnum;
3747 /* correct length for change in indent */
3748 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3749 if (col > 1)
3750 curbuf->b_op_end.col = col - 1;
3751 else
3752 curbuf->b_op_end.col = 0;
3753
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003754 if (flags & PUT_CURSLINE)
3755 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003756 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003757 curwin->w_cursor.lnum = lnum;
3758 beginline(BL_WHITE | BL_FIX);
3759 }
3760 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
3762 /* put cursor after inserted text */
3763 if (y_type == MLINE)
3764 {
3765 if (lnum >= curbuf->b_ml.ml_line_count)
3766 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3767 else
3768 curwin->w_cursor.lnum = lnum + 1;
3769 curwin->w_cursor.col = 0;
3770 }
3771 else
3772 {
3773 curwin->w_cursor.lnum = lnum;
3774 curwin->w_cursor.col = col;
3775 }
3776 }
3777 else if (y_type == MLINE)
3778 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003779 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 curwin->w_cursor.col = 0;
3781 if (dir == FORWARD)
3782 ++curwin->w_cursor.lnum;
3783 beginline(BL_WHITE | BL_FIX);
3784 }
3785 else /* put cursor on first inserted character */
3786 curwin->w_cursor = new_cursor;
3787 }
3788 }
3789
3790 msgmore(nr_lines);
3791 curwin->w_set_curswant = TRUE;
3792
3793end:
3794 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003796 if (regname == '=')
3797 vim_free(y_array);
3798
Bram Moolenaar677ee682005-01-27 14:41:15 +00003799 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003800 adjust_cursor_eol();
3801}
3802
3803/*
3804 * When the cursor is on the NUL past the end of the line and it should not be
3805 * there move it left.
3806 */
3807 void
3808adjust_cursor_eol()
3809{
3810 if (curwin->w_cursor.col > 0
3811 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003812#ifdef FEAT_VIRTUALEDIT
3813 && (ve_flags & VE_ONEMORE) == 0
3814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 && !(restart_edit || (State & INSERT)))
3816 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003817 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003818 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820#ifdef FEAT_VIRTUALEDIT
3821 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003822 {
3823 colnr_T scol, ecol;
3824
3825 /* Coladd is set to the width of the last character. */
3826 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3827 curwin->w_cursor.coladd = ecol - scol + 1;
3828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829#endif
3830 }
3831}
3832
3833#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3834/*
3835 * Return TRUE if lines starting with '#' should be left aligned.
3836 */
3837 int
3838preprocs_left()
3839{
3840 return
3841# ifdef FEAT_SMARTINDENT
3842# ifdef FEAT_CINDENT
3843 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3844# else
3845 curbuf->b_p_si
3846# endif
3847# endif
3848# ifdef FEAT_CINDENT
3849 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3850# endif
3851 ;
3852}
3853#endif
3854
3855/* Return the character name of the register with the given number */
3856 int
3857get_register_name(num)
3858 int num;
3859{
3860 if (num == -1)
3861 return '"';
3862 else if (num < 10)
3863 return num + '0';
3864 else if (num == DELETION_REGISTER)
3865 return '-';
3866#ifdef FEAT_CLIPBOARD
3867 else if (num == STAR_REGISTER)
3868 return '*';
3869 else if (num == PLUS_REGISTER)
3870 return '+';
3871#endif
3872 else
3873 {
3874#ifdef EBCDIC
3875 int i;
3876
3877 /* EBCDIC is really braindead ... */
3878 i = 'a' + (num - 10);
3879 if (i > 'i')
3880 i += 7;
3881 if (i > 'r')
3882 i += 8;
3883 return i;
3884#else
3885 return num + 'a' - 10;
3886#endif
3887 }
3888}
3889
3890/*
3891 * ":dis" and ":registers": Display the contents of the yank registers.
3892 */
3893 void
3894ex_display(eap)
3895 exarg_T *eap;
3896{
3897 int i, n;
3898 long j;
3899 char_u *p;
3900 struct yankreg *yb;
3901 int name;
3902 int attr;
3903 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003904#ifdef FEAT_MBYTE
3905 int clen;
3906#else
3907# define clen 1
3908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
3910 if (arg != NULL && *arg == NUL)
3911 arg = NULL;
3912 attr = hl_attr(HLF_8);
3913
3914 /* Highlight title */
3915 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3916 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3917 {
3918 name = get_register_name(i);
3919 if (arg != NULL && vim_strchr(arg, name) == NULL)
3920 continue; /* did not ask for this register */
3921
3922#ifdef FEAT_CLIPBOARD
3923 /* Adjust register name for "unnamed" in 'clipboard'.
3924 * When it's a clipboard register, fill it with the current contents
3925 * of the clipboard. */
3926 adjust_clip_reg(&name);
3927 (void)may_get_selection(name);
3928#endif
3929
3930 if (i == -1)
3931 {
3932 if (y_previous != NULL)
3933 yb = y_previous;
3934 else
3935 yb = &(y_regs[0]);
3936 }
3937 else
3938 yb = &(y_regs[i]);
3939 if (yb->y_array != NULL)
3940 {
3941 msg_putchar('\n');
3942 msg_putchar('"');
3943 msg_putchar(name);
3944 MSG_PUTS(" ");
3945
3946 n = (int)Columns - 6;
3947 for (j = 0; j < yb->y_size && n > 1; ++j)
3948 {
3949 if (j)
3950 {
3951 MSG_PUTS_ATTR("^J", attr);
3952 n -= 2;
3953 }
3954 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003957 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003958#endif
3959 msg_outtrans_len(p, clen);
3960#ifdef FEAT_MBYTE
3961 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962#endif
3963 }
3964 }
3965 if (n > 1 && yb->y_type == MLINE)
3966 MSG_PUTS_ATTR("^J", attr);
3967 out_flush(); /* show one line at a time */
3968 }
3969 ui_breakcheck();
3970 }
3971
3972 /*
3973 * display last inserted text
3974 */
3975 if ((p = get_last_insert()) != NULL
3976 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3977 {
3978 MSG_PUTS("\n\". ");
3979 dis_msg(p, TRUE);
3980 }
3981
3982 /*
3983 * display last command line
3984 */
3985 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3986 && !got_int)
3987 {
3988 MSG_PUTS("\n\": ");
3989 dis_msg(last_cmdline, FALSE);
3990 }
3991
3992 /*
3993 * display current file name
3994 */
3995 if (curbuf->b_fname != NULL
3996 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3997 {
3998 MSG_PUTS("\n\"% ");
3999 dis_msg(curbuf->b_fname, FALSE);
4000 }
4001
4002 /*
4003 * display alternate file name
4004 */
4005 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
4006 {
4007 char_u *fname;
4008 linenr_T dummy;
4009
4010 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
4011 {
4012 MSG_PUTS("\n\"# ");
4013 dis_msg(fname, FALSE);
4014 }
4015 }
4016
4017 /*
4018 * display last search pattern
4019 */
4020 if (last_search_pat() != NULL
4021 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4022 {
4023 MSG_PUTS("\n\"/ ");
4024 dis_msg(last_search_pat(), FALSE);
4025 }
4026
4027#ifdef FEAT_EVAL
4028 /*
4029 * display last used expression
4030 */
4031 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4032 && !got_int)
4033 {
4034 MSG_PUTS("\n\"= ");
4035 dis_msg(expr_line, FALSE);
4036 }
4037#endif
4038}
4039
4040/*
4041 * display a string for do_dis()
4042 * truncate at end of screen line
4043 */
4044 static void
4045dis_msg(p, skip_esc)
4046 char_u *p;
4047 int skip_esc; /* if TRUE, ignore trailing ESC */
4048{
4049 int n;
4050#ifdef FEAT_MBYTE
4051 int l;
4052#endif
4053
4054 n = (int)Columns - 6;
4055 while (*p != NUL
4056 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4057 && (n -= ptr2cells(p)) >= 0)
4058 {
4059#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004060 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
4062 msg_outtrans_len(p, l);
4063 p += l;
4064 }
4065 else
4066#endif
4067 msg_outtrans_len(p++, 1);
4068 }
4069 ui_breakcheck();
4070}
4071
4072/*
4073 * join 'count' lines (minimal 2), including u_save()
4074 */
4075 void
4076do_do_join(count, insert_space)
4077 long count;
4078 int insert_space;
4079{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004080 colnr_T col = MAXCOL;
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4083 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4084 return;
4085
4086 while (--count > 0)
4087 {
4088 line_breakcheck();
4089 if (got_int || do_join(insert_space) == FAIL)
4090 {
4091 beep_flush();
4092 break;
4093 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004094 if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4095 col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004098 /* Vi compatible: use the column of the first join */
4099 if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4100 curwin->w_cursor.col = col;
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#if 0
4103 /*
4104 * Need to update the screen if the line where the cursor is became too
4105 * long to fit on the screen.
4106 */
4107 update_topline_redraw();
4108#endif
4109}
4110
4111/*
4112 * Join two lines at the cursor position.
4113 * "redraw" is TRUE when the screen should be updated.
4114 * Caller must have setup for undo.
4115 *
Bram Moolenaar10c56952007-05-10 18:38:52 +00004116 * return FAIL for failure, OK otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 */
4118 int
4119do_join(insert_space)
4120 int insert_space;
4121{
4122 char_u *curr;
4123 char_u *next, *next_start;
4124 char_u *newp;
4125 int endcurr1, endcurr2;
4126 int currsize; /* size of the current line */
4127 int nextsize; /* size of the next line */
4128 int spaces; /* number of spaces to insert */
4129 linenr_T t;
4130
4131 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4132 return FAIL; /* can't join on last line */
4133
4134 curr = ml_get_curline();
4135 currsize = (int)STRLEN(curr);
4136 endcurr1 = endcurr2 = NUL;
4137 if (insert_space && currsize > 0)
4138 {
4139#ifdef FEAT_MBYTE
4140 if (has_mbyte)
4141 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004142 next = curr + currsize;
4143 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 endcurr1 = (*mb_ptr2char)(next);
4145 if (next > curr)
4146 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004147 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 endcurr2 = (*mb_ptr2char)(next);
4149 }
4150 }
4151 else
4152#endif
4153 {
4154 endcurr1 = *(curr + currsize - 1);
4155 if (currsize > 1)
4156 endcurr2 = *(curr + currsize - 2);
4157 }
4158 }
4159
4160 next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4161 spaces = 0;
4162 if (insert_space)
4163 {
4164 next = skipwhite(next);
4165 if (*next != ')' && currsize != 0 && endcurr1 != TAB
4166#ifdef FEAT_MBYTE
4167 && (!has_format_option(FO_MBYTE_JOIN)
4168 || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4169 && (!has_format_option(FO_MBYTE_JOIN2)
4170 || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4171#endif
4172 )
4173 {
4174 /* don't add a space if the line is ending in a space */
4175 if (endcurr1 == ' ')
4176 endcurr1 = endcurr2;
4177 else
4178 ++spaces;
4179 /* extra space when 'joinspaces' set and line ends in '.' */
4180 if ( p_js
4181 && (endcurr1 == '.'
4182 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4183 && (endcurr1 == '?' || endcurr1 == '!'))))
4184 ++spaces;
4185 }
4186 }
4187 nextsize = (int)STRLEN(next);
4188
4189 newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4190 if (newp == NULL)
4191 return FAIL;
4192
4193 /*
4194 * Insert the next line first, because we already have that pointer.
4195 * Curr has to be obtained again, because getting next will have
4196 * invalidated it.
4197 */
4198 mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4199
4200 curr = ml_get_curline();
4201 mch_memmove(newp, curr, (size_t)currsize);
4202
4203 copy_spaces(newp + currsize, (size_t)spaces);
4204
4205 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4206
4207 /* Only report the change in the first line here, del_lines() will report
4208 * the deleted line. */
4209 changed_lines(curwin->w_cursor.lnum, currsize,
4210 curwin->w_cursor.lnum + 1, 0L);
4211
4212 /*
4213 * Delete the following line. To do this we move the cursor there
4214 * briefly, and then move it back. After del_lines() the cursor may
4215 * have moved up (last line deleted), so the current lnum is kept in t.
4216 *
4217 * Move marks from the deleted line to the joined line, adjusting the
4218 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4219 * should not really be a problem.
4220 */
4221 t = curwin->w_cursor.lnum;
4222 mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4223 (long)(currsize + spaces - (next - next_start)));
4224 ++curwin->w_cursor.lnum;
4225 del_lines(1L, FALSE);
4226 curwin->w_cursor.lnum = t;
4227
4228 /*
4229 * go to first character of the joined line
4230 */
4231 curwin->w_cursor.col = currsize;
4232 check_cursor_col();
4233#ifdef FEAT_VIRTUALEDIT
4234 curwin->w_cursor.coladd = 0;
4235#endif
4236 curwin->w_set_curswant = TRUE;
4237
4238 return OK;
4239}
4240
4241#ifdef FEAT_COMMENTS
4242/*
4243 * Return TRUE if the two comment leaders given are the same. "lnum" is
4244 * the first line. White-space is ignored. Note that the whole of
4245 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4246 */
4247 static int
4248same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4249 linenr_T lnum;
4250 int leader1_len;
4251 char_u *leader1_flags;
4252 int leader2_len;
4253 char_u *leader2_flags;
4254{
4255 int idx1 = 0, idx2 = 0;
4256 char_u *p;
4257 char_u *line1;
4258 char_u *line2;
4259
4260 if (leader1_len == 0)
4261 return (leader2_len == 0);
4262
4263 /*
4264 * If first leader has 'f' flag, the lines can be joined only if the
4265 * second line does not have a leader.
4266 * If first leader has 'e' flag, the lines can never be joined.
4267 * If fist leader has 's' flag, the lines can only be joined if there is
4268 * some text after it and the second line has the 'm' flag.
4269 */
4270 if (leader1_flags != NULL)
4271 {
4272 for (p = leader1_flags; *p && *p != ':'; ++p)
4273 {
4274 if (*p == COM_FIRST)
4275 return (leader2_len == 0);
4276 if (*p == COM_END)
4277 return FALSE;
4278 if (*p == COM_START)
4279 {
4280 if (*(ml_get(lnum) + leader1_len) == NUL)
4281 return FALSE;
4282 if (leader2_flags == NULL || leader2_len == 0)
4283 return FALSE;
4284 for (p = leader2_flags; *p && *p != ':'; ++p)
4285 if (*p == COM_MIDDLE)
4286 return TRUE;
4287 return FALSE;
4288 }
4289 }
4290 }
4291
4292 /*
4293 * Get current line and next line, compare the leaders.
4294 * The first line has to be saved, only one line can be locked at a time.
4295 */
4296 line1 = vim_strsave(ml_get(lnum));
4297 if (line1 != NULL)
4298 {
4299 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4300 ;
4301 line2 = ml_get(lnum + 1);
4302 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4303 {
4304 if (!vim_iswhite(line2[idx2]))
4305 {
4306 if (line1[idx1++] != line2[idx2])
4307 break;
4308 }
4309 else
4310 while (vim_iswhite(line1[idx1]))
4311 ++idx1;
4312 }
4313 vim_free(line1);
4314 }
4315 return (idx2 == leader2_len && idx1 == leader1_len);
4316}
4317#endif
4318
4319/*
4320 * implementation of the format operator 'gq'
4321 */
4322 void
4323op_format(oap, keep_cursor)
4324 oparg_T *oap;
4325 int keep_cursor; /* keep cursor on same text char */
4326{
4327 long old_line_count = curbuf->b_ml.ml_line_count;
4328
4329 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4330 * can put it back there. */
4331 curwin->w_cursor = oap->cursor_start;
4332
4333 if (u_save((linenr_T)(oap->start.lnum - 1),
4334 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4335 return;
4336 curwin->w_cursor = oap->start;
4337
4338#ifdef FEAT_VISUAL
4339 if (oap->is_VIsual)
4340 /* When there is no change: need to remove the Visual selection */
4341 redraw_curbuf_later(INVERTED);
4342#endif
4343
4344 /* Set '[ mark at the start of the formatted area */
4345 curbuf->b_op_start = oap->start;
4346
4347 /* For "gw" remember the cursor position and put it back below (adjusted
4348 * for joined and split lines). */
4349 if (keep_cursor)
4350 saved_cursor = oap->cursor_start;
4351
4352 format_lines(oap->line_count);
4353
4354 /*
4355 * Leave the cursor at the first non-blank of the last formatted line.
4356 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4357 * line, so "." will do the next lines.
4358 */
4359 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4360 ++curwin->w_cursor.lnum;
4361 beginline(BL_WHITE | BL_FIX);
4362 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4363 msgmore(old_line_count);
4364
4365 /* put '] mark on the end of the formatted area */
4366 curbuf->b_op_end = curwin->w_cursor;
4367
4368 if (keep_cursor)
4369 {
4370 curwin->w_cursor = saved_cursor;
4371 saved_cursor.lnum = 0;
4372 }
4373
4374#ifdef FEAT_VISUAL
4375 if (oap->is_VIsual)
4376 {
4377 win_T *wp;
4378
4379 FOR_ALL_WINDOWS(wp)
4380 {
4381 if (wp->w_old_cursor_lnum != 0)
4382 {
4383 /* When lines have been inserted or deleted, adjust the end of
4384 * the Visual area to be redrawn. */
4385 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4386 wp->w_old_cursor_lnum += old_line_count;
4387 else
4388 wp->w_old_visual_lnum += old_line_count;
4389 }
4390 }
4391 }
4392#endif
4393}
4394
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004395#if defined(FEAT_EVAL) || defined(PROTO)
4396/*
4397 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4398 */
4399 void
4400op_formatexpr(oap)
4401 oparg_T *oap;
4402{
4403# ifdef FEAT_VISUAL
4404 if (oap->is_VIsual)
4405 /* When there is no change: need to remove the Visual selection */
4406 redraw_curbuf_later(INVERTED);
4407# endif
4408
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004409 (void)fex_format(oap->start.lnum, oap->line_count, NUL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004410}
4411
4412 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004413fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004414 linenr_T lnum;
4415 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004416 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004417{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004418 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4419 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004420 int r;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004421#ifdef FEAT_MBYTE
4422 char_u buf[MB_MAXBYTES];
4423#else
4424 char_u buf[2];
4425#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004426
4427 /*
4428 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004429 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004430 */
4431 set_vim_var_nr(VV_LNUM, lnum);
4432 set_vim_var_nr(VV_COUNT, count);
4433
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004434#ifdef FEAT_MBYTE
4435 if (has_mbyte)
4436 buf[(*mb_char2bytes)(c, buf)] = NUL;
4437 else
4438#endif
4439 {
4440 buf[0] = c;
4441 buf[1] = NUL;
4442 }
4443 set_vim_var_string(VV_CHAR, buf, -1);
4444
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004445 /*
4446 * Evaluate the function.
4447 */
4448 if (use_sandbox)
4449 ++sandbox;
4450 r = eval_to_number(curbuf->b_p_fex);
4451 if (use_sandbox)
4452 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004453
4454 set_vim_var_string(VV_CHAR, NULL, -1);
4455
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004456 return r;
4457}
4458#endif
4459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460/*
4461 * Format "line_count" lines, starting at the cursor position.
4462 * When "line_count" is negative, format until the end of the paragraph.
4463 * Lines after the cursor line are saved for undo, caller must have saved the
4464 * first line.
4465 */
4466 void
4467format_lines(line_count)
4468 linenr_T line_count;
4469{
4470 int max_len;
4471 int is_not_par; /* current line not part of parag. */
4472 int next_is_not_par; /* next line not part of paragraph */
4473 int is_end_par; /* at end of paragraph */
4474 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4475 int next_is_start_par = FALSE;
4476#ifdef FEAT_COMMENTS
4477 int leader_len = 0; /* leader len of current line */
4478 int next_leader_len; /* leader len of next line */
4479 char_u *leader_flags = NULL; /* flags for leader of current line */
4480 char_u *next_leader_flags; /* flags for leader of next line */
4481 int do_comments; /* format comments */
4482#endif
4483 int advance = TRUE;
4484 int second_indent = -1;
4485 int do_second_indent;
4486 int do_number_indent;
4487 int do_trail_white;
4488 int first_par_line = TRUE;
4489 int smd_save;
4490 long count;
4491 int need_set_indent = TRUE; /* set indent of next paragraph */
4492 int force_format = FALSE;
4493 int old_State = State;
4494
4495 /* length of a line to force formatting: 3 * 'tw' */
4496 max_len = comp_textwidth(TRUE) * 3;
4497
4498 /* check for 'q', '2' and '1' in 'formatoptions' */
4499#ifdef FEAT_COMMENTS
4500 do_comments = has_format_option(FO_Q_COMS);
4501#endif
4502 do_second_indent = has_format_option(FO_Q_SECOND);
4503 do_number_indent = has_format_option(FO_Q_NUMBER);
4504 do_trail_white = has_format_option(FO_WHITE_PAR);
4505
4506 /*
4507 * Get info about the previous and current line.
4508 */
4509 if (curwin->w_cursor.lnum > 1)
4510 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4511#ifdef FEAT_COMMENTS
4512 , &leader_len, &leader_flags, do_comments
4513#endif
4514 );
4515 else
4516 is_not_par = TRUE;
4517 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4518#ifdef FEAT_COMMENTS
4519 , &next_leader_len, &next_leader_flags, do_comments
4520#endif
4521 );
4522 is_end_par = (is_not_par || next_is_not_par);
4523 if (!is_end_par && do_trail_white)
4524 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4525
4526 curwin->w_cursor.lnum--;
4527 for (count = line_count; count != 0 && !got_int; --count)
4528 {
4529 /*
4530 * Advance to next paragraph.
4531 */
4532 if (advance)
4533 {
4534 curwin->w_cursor.lnum++;
4535 prev_is_end_par = is_end_par;
4536 is_not_par = next_is_not_par;
4537#ifdef FEAT_COMMENTS
4538 leader_len = next_leader_len;
4539 leader_flags = next_leader_flags;
4540#endif
4541 }
4542
4543 /*
4544 * The last line to be formatted.
4545 */
4546 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4547 {
4548 next_is_not_par = TRUE;
4549#ifdef FEAT_COMMENTS
4550 next_leader_len = 0;
4551 next_leader_flags = NULL;
4552#endif
4553 }
4554 else
4555 {
4556 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4557#ifdef FEAT_COMMENTS
4558 , &next_leader_len, &next_leader_flags, do_comments
4559#endif
4560 );
4561 if (do_number_indent)
4562 next_is_start_par =
4563 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4564 }
4565 advance = TRUE;
4566 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4567 if (!is_end_par && do_trail_white)
4568 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4569
4570 /*
4571 * Skip lines that are not in a paragraph.
4572 */
4573 if (is_not_par)
4574 {
4575 if (line_count < 0)
4576 break;
4577 }
4578 else
4579 {
4580 /*
4581 * For the first line of a paragraph, check indent of second line.
4582 * Don't do this for comments and empty lines.
4583 */
4584 if (first_par_line
4585 && (do_second_indent || do_number_indent)
4586 && prev_is_end_par
4587 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4588#ifdef FEAT_COMMENTS
4589 && leader_len == 0
4590 && next_leader_len == 0
4591#endif
4592 )
4593 {
4594 if (do_second_indent
4595 && !lineempty(curwin->w_cursor.lnum + 1))
4596 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4597 else if (do_number_indent)
4598 second_indent = get_number_indent(curwin->w_cursor.lnum);
4599 }
4600
4601 /*
4602 * When the comment leader changes, it's the end of the paragraph.
4603 */
4604 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4605#ifdef FEAT_COMMENTS
4606 || !same_leader(curwin->w_cursor.lnum,
4607 leader_len, leader_flags,
4608 next_leader_len, next_leader_flags)
4609#endif
4610 )
4611 is_end_par = TRUE;
4612
4613 /*
4614 * If we have got to the end of a paragraph, or the line is
4615 * getting long, format it.
4616 */
4617 if (is_end_par || force_format)
4618 {
4619 if (need_set_indent)
4620 /* replace indent in first line with minimal number of
4621 * tabs and spaces, according to current options */
4622 (void)set_indent(get_indent(), SIN_CHANGED);
4623
4624 /* put cursor on last non-space */
4625 State = NORMAL; /* don't go past end-of-line */
4626 coladvance((colnr_T)MAXCOL);
4627 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4628 dec_cursor();
4629
4630 /* do the formatting, without 'showmode' */
4631 State = INSERT; /* for open_line() */
4632 smd_save = p_smd;
4633 p_smd = FALSE;
4634 insertchar(NUL, INSCHAR_FORMAT
4635#ifdef FEAT_COMMENTS
4636 + (do_comments ? INSCHAR_DO_COM : 0)
4637#endif
4638 , second_indent);
4639 State = old_State;
4640 p_smd = smd_save;
4641 second_indent = -1;
4642 /* at end of par.: need to set indent of next par. */
4643 need_set_indent = is_end_par;
4644 if (is_end_par)
4645 {
4646 /* When called with a negative line count, break at the
4647 * end of the paragraph. */
4648 if (line_count < 0)
4649 break;
4650 first_par_line = TRUE;
4651 }
4652 force_format = FALSE;
4653 }
4654
4655 /*
4656 * When still in same paragraph, join the lines together. But
4657 * first delete the comment leader from the second line.
4658 */
4659 if (!is_end_par)
4660 {
4661 advance = FALSE;
4662 curwin->w_cursor.lnum++;
4663 curwin->w_cursor.col = 0;
4664 if (line_count < 0 && u_save_cursor() == FAIL)
4665 break;
4666#ifdef FEAT_COMMENTS
Bram Moolenaare3226be2005-12-18 22:10:00 +00004667 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (next_leader_len > 0)
4669 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4670 (long)-next_leader_len);
4671#endif
4672 curwin->w_cursor.lnum--;
4673 if (do_join(TRUE) == FAIL)
4674 {
4675 beep_flush();
4676 break;
4677 }
4678 first_par_line = FALSE;
4679 /* If the line is getting long, format it next time */
4680 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4681 force_format = TRUE;
4682 else
4683 force_format = FALSE;
4684 }
4685 }
4686 line_breakcheck();
4687 }
4688}
4689
4690/*
4691 * Return TRUE if line "lnum" ends in a white character.
4692 */
4693 static int
4694ends_in_white(lnum)
4695 linenr_T lnum;
4696{
4697 char_u *s = ml_get(lnum);
4698 size_t l;
4699
4700 if (*s == NUL)
4701 return FALSE;
4702 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4703 * invocation may call function multiple times". */
4704 l = STRLEN(s) - 1;
4705 return vim_iswhite(s[l]);
4706}
4707
4708/*
4709 * Blank lines, and lines containing only the comment leader, are left
4710 * untouched by the formatting. The function returns TRUE in this
4711 * case. It also returns TRUE when a line starts with the end of a comment
4712 * ('e' in comment flags), so that this line is skipped, and not joined to the
4713 * previous line. A new paragraph starts after a blank line, or when the
4714 * comment leader changes -- webb.
4715 */
4716#ifdef FEAT_COMMENTS
4717 static int
4718fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4719 linenr_T lnum;
4720 int *leader_len;
4721 char_u **leader_flags;
4722 int do_comments;
4723{
4724 char_u *flags = NULL; /* init for GCC */
4725 char_u *ptr;
4726
4727 ptr = ml_get(lnum);
4728 if (do_comments)
4729 *leader_len = get_leader_len(ptr, leader_flags, FALSE);
4730 else
4731 *leader_len = 0;
4732
4733 if (*leader_len > 0)
4734 {
4735 /*
4736 * Search for 'e' flag in comment leader flags.
4737 */
4738 flags = *leader_flags;
4739 while (*flags && *flags != ':' && *flags != COM_END)
4740 ++flags;
4741 }
4742
4743 return (*skipwhite(ptr + *leader_len) == NUL
4744 || (*leader_len > 0 && *flags == COM_END)
4745 || startPS(lnum, NUL, FALSE));
4746}
4747#else
4748 static int
4749fmt_check_par(lnum)
4750 linenr_T lnum;
4751{
4752 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4753}
4754#endif
4755
4756/*
4757 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4758 * previous line is in the same paragraph. Used for auto-formatting.
4759 */
4760 int
4761paragraph_start(lnum)
4762 linenr_T lnum;
4763{
4764 char_u *p;
4765#ifdef FEAT_COMMENTS
4766 int leader_len = 0; /* leader len of current line */
4767 char_u *leader_flags = NULL; /* flags for leader of current line */
4768 int next_leader_len; /* leader len of next line */
4769 char_u *next_leader_flags; /* flags for leader of next line */
4770 int do_comments; /* format comments */
4771#endif
4772
4773 if (lnum <= 1)
4774 return TRUE; /* start of the file */
4775
4776 p = ml_get(lnum - 1);
4777 if (*p == NUL)
4778 return TRUE; /* after empty line */
4779
4780#ifdef FEAT_COMMENTS
4781 do_comments = has_format_option(FO_Q_COMS);
4782#endif
4783 if (fmt_check_par(lnum - 1
4784#ifdef FEAT_COMMENTS
4785 , &leader_len, &leader_flags, do_comments
4786#endif
4787 ))
4788 return TRUE; /* after non-paragraph line */
4789
4790 if (fmt_check_par(lnum
4791#ifdef FEAT_COMMENTS
4792 , &next_leader_len, &next_leader_flags, do_comments
4793#endif
4794 ))
4795 return TRUE; /* "lnum" is not a paragraph line */
4796
4797 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4798 return TRUE; /* missing trailing space in previous line. */
4799
4800 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4801 return TRUE; /* numbered item starts in "lnum". */
4802
4803#ifdef FEAT_COMMENTS
4804 if (!same_leader(lnum - 1, leader_len, leader_flags,
4805 next_leader_len, next_leader_flags))
4806 return TRUE; /* change of comment leader. */
4807#endif
4808
4809 return FALSE;
4810}
4811
4812#ifdef FEAT_VISUAL
4813/*
4814 * prepare a few things for block mode yank/delete/tilde
4815 *
4816 * for delete:
4817 * - textlen includes the first/last char to be (partly) deleted
4818 * - start/endspaces is the number of columns that are taken by the
4819 * first/last deleted char minus the number of columns that have to be
4820 * deleted. for yank and tilde:
4821 * - textlen includes the first/last char to be wholly yanked
4822 * - start/endspaces is the number of columns of the first/last yanked char
4823 * that are to be yanked.
4824 */
4825 static void
4826block_prep(oap, bdp, lnum, is_del)
4827 oparg_T *oap;
4828 struct block_def *bdp;
4829 linenr_T lnum;
4830 int is_del;
4831{
4832 int incr = 0;
4833 char_u *pend;
4834 char_u *pstart;
4835 char_u *line;
4836 char_u *prev_pstart;
4837 char_u *prev_pend;
4838
4839 bdp->startspaces = 0;
4840 bdp->endspaces = 0;
4841 bdp->textlen = 0;
4842 bdp->start_vcol = 0;
4843 bdp->end_vcol = 0;
4844#ifdef FEAT_VISUALEXTRA
4845 bdp->is_short = FALSE;
4846 bdp->is_oneChar = FALSE;
4847 bdp->pre_whitesp = 0;
4848 bdp->pre_whitesp_c = 0;
4849 bdp->end_char_vcols = 0;
4850#endif
4851 bdp->start_char_vcols = 0;
4852
4853 line = ml_get(lnum);
4854 pstart = line;
4855 prev_pstart = line;
4856 while (bdp->start_vcol < oap->start_vcol && *pstart)
4857 {
4858 /* Count a tab for what it's worth (if list mode not on) */
4859 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4860 bdp->start_vcol += incr;
4861#ifdef FEAT_VISUALEXTRA
4862 if (vim_iswhite(*pstart))
4863 {
4864 bdp->pre_whitesp += incr;
4865 bdp->pre_whitesp_c++;
4866 }
4867 else
4868 {
4869 bdp->pre_whitesp = 0;
4870 bdp->pre_whitesp_c = 0;
4871 }
4872#endif
4873 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004874 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 bdp->start_char_vcols = incr;
4877 if (bdp->start_vcol < oap->start_vcol) /* line too short */
4878 {
4879 bdp->end_vcol = bdp->start_vcol;
4880#ifdef FEAT_VISUALEXTRA
4881 bdp->is_short = TRUE;
4882#endif
4883 if (!is_del || oap->op_type == OP_APPEND)
4884 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4885 }
4886 else
4887 {
4888 /* notice: this converts partly selected Multibyte characters to
4889 * spaces, too. */
4890 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4891 if (is_del && bdp->startspaces)
4892 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4893 pend = pstart;
4894 bdp->end_vcol = bdp->start_vcol;
4895 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
4896 {
4897#ifdef FEAT_VISUALEXTRA
4898 bdp->is_oneChar = TRUE;
4899#endif
4900 if (oap->op_type == OP_INSERT)
4901 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4902 else if (oap->op_type == OP_APPEND)
4903 {
4904 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4905 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4906 }
4907 else
4908 {
4909 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4910 if (is_del && oap->op_type != OP_LSHIFT)
4911 {
4912 /* just putting the sum of those two into
4913 * bdp->startspaces doesn't work for Visual replace,
4914 * so we have to split the tab in two */
4915 bdp->startspaces = bdp->start_char_vcols
4916 - (bdp->start_vcol - oap->start_vcol);
4917 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4918 }
4919 }
4920 }
4921 else
4922 {
4923 prev_pend = pend;
4924 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4925 {
4926 /* Count a tab for what it's worth (if list mode not on) */
4927 prev_pend = pend;
4928 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4929 bdp->end_vcol += incr;
4930 }
4931 if (bdp->end_vcol <= oap->end_vcol
4932 && (!is_del
4933 || oap->op_type == OP_APPEND
4934 || oap->op_type == OP_REPLACE)) /* line too short */
4935 {
4936#ifdef FEAT_VISUALEXTRA
4937 bdp->is_short = TRUE;
4938#endif
4939 /* Alternative: include spaces to fill up the block.
4940 * Disadvantage: can lead to trailing spaces when the line is
4941 * short where the text is put */
4942 /* if (!is_del || oap->op_type == OP_APPEND) */
4943 if (oap->op_type == OP_APPEND || virtual_op)
4944 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004945 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 else
4947 bdp->endspaces = 0; /* replace doesn't add characters */
4948 }
4949 else if (bdp->end_vcol > oap->end_vcol)
4950 {
4951 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4952 if (!is_del && bdp->endspaces)
4953 {
4954 bdp->endspaces = incr - bdp->endspaces;
4955 if (pend != pstart)
4956 pend = prev_pend;
4957 }
4958 }
4959 }
4960#ifdef FEAT_VISUALEXTRA
4961 bdp->end_char_vcols = incr;
4962#endif
4963 if (is_del && bdp->startspaces)
4964 pstart = prev_pstart;
4965 bdp->textlen = (int)(pend - pstart);
4966 }
4967 bdp->textcol = (colnr_T) (pstart - line);
4968 bdp->textstart = pstart;
4969}
4970#endif /* FEAT_VISUAL */
4971
4972#ifdef FEAT_RIGHTLEFT
4973static void reverse_line __ARGS((char_u *s));
4974
4975 static void
4976reverse_line(s)
4977 char_u *s;
4978{
4979 int i, j;
4980 char_u c;
4981
4982 if ((i = (int)STRLEN(s) - 1) <= 0)
4983 return;
4984
4985 curwin->w_cursor.col = i - curwin->w_cursor.col;
4986 for (j = 0; j < i; j++, i--)
4987 {
4988 c = s[i]; s[i] = s[j]; s[j] = c;
4989 }
4990}
4991
4992# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4993#else
4994# define RLADDSUBFIX(ptr)
4995#endif
4996
4997/*
4998 * add or subtract 'Prenum1' from a number in a line
4999 * 'command' is CTRL-A for add, CTRL-X for subtract
5000 *
5001 * return FAIL for failure, OK otherwise
5002 */
5003 int
5004do_addsub(command, Prenum1)
5005 int command;
5006 linenr_T Prenum1;
5007{
5008 int col;
5009 char_u *buf1;
5010 char_u buf2[NUMBUFLEN];
5011 int hex; /* 'X' or 'x': hex; '0': octal */
5012 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005013 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 long_u oldn;
5015 char_u *ptr;
5016 int c;
5017 int length = 0; /* character length of the number */
5018 int todel;
5019 int dohex;
5020 int dooct;
5021 int doalp;
5022 int firstdigit;
5023 int negative;
5024 int subtract;
5025
5026 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5027 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5028 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5029
5030 ptr = ml_get_curline();
5031 RLADDSUBFIX(ptr);
5032
5033 /*
5034 * First check if we are on a hexadecimal number, after the "0x".
5035 */
5036 col = curwin->w_cursor.col;
5037 if (dohex)
5038 while (col > 0 && vim_isxdigit(ptr[col]))
5039 --col;
5040 if ( dohex
5041 && col > 0
5042 && (ptr[col] == 'X'
5043 || ptr[col] == 'x')
5044 && ptr[col - 1] == '0'
5045 && vim_isxdigit(ptr[col + 1]))
5046 {
5047 /*
5048 * Found hexadecimal number, move to its start.
5049 */
5050 --col;
5051 }
5052 else
5053 {
5054 /*
5055 * Search forward and then backward to find the start of number.
5056 */
5057 col = curwin->w_cursor.col;
5058
5059 while (ptr[col] != NUL
5060 && !vim_isdigit(ptr[col])
5061 && !(doalp && ASCII_ISALPHA(ptr[col])))
5062 ++col;
5063
5064 while (col > 0
5065 && vim_isdigit(ptr[col - 1])
5066 && !(doalp && ASCII_ISALPHA(ptr[col])))
5067 --col;
5068 }
5069
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 /*
5071 * If a number was found, and saving for undo works, replace the number.
5072 */
5073 firstdigit = ptr[col];
5074 RLADDSUBFIX(ptr);
5075 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5076 || u_save_cursor() != OK)
5077 {
5078 beep_flush();
5079 return FAIL;
5080 }
5081
5082 /* get ptr again, because u_save() may have changed it */
5083 ptr = ml_get_curline();
5084 RLADDSUBFIX(ptr);
5085
5086 if (doalp && ASCII_ISALPHA(firstdigit))
5087 {
5088 /* decrement or increment alphabetic character */
5089 if (command == Ctrl_X)
5090 {
5091 if (CharOrd(firstdigit) < Prenum1)
5092 {
5093 if (isupper(firstdigit))
5094 firstdigit = 'A';
5095 else
5096 firstdigit = 'a';
5097 }
5098 else
5099#ifdef EBCDIC
5100 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5101#else
5102 firstdigit -= Prenum1;
5103#endif
5104 }
5105 else
5106 {
5107 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5108 {
5109 if (isupper(firstdigit))
5110 firstdigit = 'Z';
5111 else
5112 firstdigit = 'z';
5113 }
5114 else
5115#ifdef EBCDIC
5116 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5117#else
5118 firstdigit += Prenum1;
5119#endif
5120 }
5121 curwin->w_cursor.col = col;
5122 (void)del_char(FALSE);
5123 ins_char(firstdigit);
5124 }
5125 else
5126 {
5127 negative = FALSE;
5128 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5129 {
5130 --col;
5131 negative = TRUE;
5132 }
5133
5134 /* get the number value (unsigned) */
5135 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5136
5137 /* ignore leading '-' for hex and octal numbers */
5138 if (hex && negative)
5139 {
5140 ++col;
5141 --length;
5142 negative = FALSE;
5143 }
5144
5145 /* add or subtract */
5146 subtract = FALSE;
5147 if (command == Ctrl_X)
5148 subtract ^= TRUE;
5149 if (negative)
5150 subtract ^= TRUE;
5151
5152 oldn = n;
5153 if (subtract)
5154 n -= (unsigned long)Prenum1;
5155 else
5156 n += (unsigned long)Prenum1;
5157
5158 /* handle wraparound for decimal numbers */
5159 if (!hex)
5160 {
5161 if (subtract)
5162 {
5163 if (n > oldn)
5164 {
5165 n = 1 + (n ^ (unsigned long)-1);
5166 negative ^= TRUE;
5167 }
5168 }
5169 else /* add */
5170 {
5171 if (n < oldn)
5172 {
5173 n = (n ^ (unsigned long)-1);
5174 negative ^= TRUE;
5175 }
5176 }
5177 if (n == 0)
5178 negative = FALSE;
5179 }
5180
5181 /*
5182 * Delete the old number.
5183 */
5184 curwin->w_cursor.col = col;
5185 todel = length;
5186 c = gchar_cursor();
5187 /*
5188 * Don't include the '-' in the length, only the length of the part
5189 * after it is kept the same.
5190 */
5191 if (c == '-')
5192 --length;
5193 while (todel-- > 0)
5194 {
5195 if (c < 0x100 && isalpha(c))
5196 {
5197 if (isupper(c))
5198 hexupper = TRUE;
5199 else
5200 hexupper = FALSE;
5201 }
5202 /* del_char() will mark line needing displaying */
5203 (void)del_char(FALSE);
5204 c = gchar_cursor();
5205 }
5206
5207 /*
5208 * Prepare the leading characters in buf1[].
5209 * When there are many leading zeros it could be very long. Allocate
5210 * a bit too much.
5211 */
5212 buf1 = alloc((unsigned)length + NUMBUFLEN);
5213 if (buf1 == NULL)
5214 return FAIL;
5215 ptr = buf1;
5216 if (negative)
5217 {
5218 *ptr++ = '-';
5219 }
5220 if (hex)
5221 {
5222 *ptr++ = '0';
5223 --length;
5224 }
5225 if (hex == 'x' || hex == 'X')
5226 {
5227 *ptr++ = hex;
5228 --length;
5229 }
5230
5231 /*
5232 * Put the number characters in buf2[].
5233 */
5234 if (hex == 0)
5235 sprintf((char *)buf2, "%lu", n);
5236 else if (hex == '0')
5237 sprintf((char *)buf2, "%lo", n);
5238 else if (hex && hexupper)
5239 sprintf((char *)buf2, "%lX", n);
5240 else
5241 sprintf((char *)buf2, "%lx", n);
5242 length -= (int)STRLEN(buf2);
5243
5244 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005245 * Adjust number of zeros to the new number of digits, so the
5246 * total length of the number remains the same.
5247 * Don't do this when
5248 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005250 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 while (length-- > 0)
5252 *ptr++ = '0';
5253 *ptr = NUL;
5254 STRCAT(buf1, buf2);
5255 ins_str(buf1); /* insert the new number */
5256 vim_free(buf1);
5257 }
5258 --curwin->w_cursor.col;
5259 curwin->w_set_curswant = TRUE;
5260#ifdef FEAT_RIGHTLEFT
5261 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5262 RLADDSUBFIX(ptr);
5263#endif
5264 return OK;
5265}
5266
5267#ifdef FEAT_VIMINFO
5268 int
5269read_viminfo_register(virp, force)
5270 vir_T *virp;
5271 int force;
5272{
5273 int eof;
5274 int do_it = TRUE;
5275 int size;
5276 int limit;
5277 int i;
5278 int set_prev = FALSE;
5279 char_u *str;
5280 char_u **array = NULL;
5281
5282 /* We only get here (hopefully) if line[0] == '"' */
5283 str = virp->vir_line + 1;
5284 if (*str == '"')
5285 {
5286 set_prev = TRUE;
5287 str++;
5288 }
5289 if (!ASCII_ISALNUM(*str) && *str != '-')
5290 {
5291 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5292 return TRUE; /* too many errors, pretend end-of-file */
5293 do_it = FALSE;
5294 }
5295 get_yank_register(*str++, FALSE);
5296 if (!force && y_current->y_array != NULL)
5297 do_it = FALSE;
5298 size = 0;
5299 limit = 100; /* Optimized for registers containing <= 100 lines */
5300 if (do_it)
5301 {
5302 if (set_prev)
5303 y_previous = y_current;
5304 vim_free(y_current->y_array);
5305 array = y_current->y_array =
5306 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5307 str = skipwhite(str);
5308 if (STRNCMP(str, "CHAR", 4) == 0)
5309 y_current->y_type = MCHAR;
5310#ifdef FEAT_VISUAL
5311 else if (STRNCMP(str, "BLOCK", 5) == 0)
5312 y_current->y_type = MBLOCK;
5313#endif
5314 else
5315 y_current->y_type = MLINE;
5316 /* get the block width; if it's missing we get a zero, which is OK */
5317 str = skipwhite(skiptowhite(str));
5318#ifdef FEAT_VISUAL
5319 y_current->y_width = getdigits(&str);
5320#else
5321 (void)getdigits(&str);
5322#endif
5323 }
5324
5325 while (!(eof = viminfo_readline(virp))
5326 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5327 {
5328 if (do_it)
5329 {
5330 if (size >= limit)
5331 {
5332 y_current->y_array = (char_u **)
5333 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5334 for (i = 0; i < limit; i++)
5335 y_current->y_array[i] = array[i];
5336 vim_free(array);
5337 limit *= 2;
5338 array = y_current->y_array;
5339 }
5340 str = viminfo_readstring(virp, 1, TRUE);
5341 if (str != NULL)
5342 array[size++] = str;
5343 else
5344 do_it = FALSE;
5345 }
5346 }
5347 if (do_it)
5348 {
5349 if (size == 0)
5350 {
5351 vim_free(array);
5352 y_current->y_array = NULL;
5353 }
5354 else if (size < limit)
5355 {
5356 y_current->y_array =
5357 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5358 for (i = 0; i < size; i++)
5359 y_current->y_array[i] = array[i];
5360 vim_free(array);
5361 }
5362 y_current->y_size = size;
5363 }
5364 return eof;
5365}
5366
5367 void
5368write_viminfo_registers(fp)
5369 FILE *fp;
5370{
5371 int i, j;
5372 char_u *type;
5373 char_u c;
5374 int num_lines;
5375 int max_num_lines;
5376 int max_kbyte;
5377 long len;
5378
5379 fprintf(fp, _("\n# Registers:\n"));
5380
5381 /* Get '<' value, use old '"' value if '<' is not found. */
5382 max_num_lines = get_viminfo_parameter('<');
5383 if (max_num_lines < 0)
5384 max_num_lines = get_viminfo_parameter('"');
5385 if (max_num_lines == 0)
5386 return;
5387 max_kbyte = get_viminfo_parameter('s');
5388 if (max_kbyte == 0)
5389 return;
5390 for (i = 0; i < NUM_REGISTERS; i++)
5391 {
5392 if (y_regs[i].y_array == NULL)
5393 continue;
5394#ifdef FEAT_CLIPBOARD
5395 /* Skip '*'/'+' register, we don't want them back next time */
5396 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5397 continue;
5398#endif
5399#ifdef FEAT_DND
5400 /* Neither do we want the '~' register */
5401 if (i == TILDE_REGISTER)
5402 continue;
5403#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005404 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005406 if (num_lines == 0
5407 || (num_lines == 1 && y_regs[i].y_type == MCHAR
5408 && STRLEN(y_regs[i].y_array[0]) == 0))
5409 continue;
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 if (max_kbyte > 0)
5412 {
5413 /* Skip register if there is more text than the maximum size. */
5414 len = 0;
5415 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005416 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 if (len > (long)max_kbyte * 1024L)
5418 continue;
5419 }
5420
5421 switch (y_regs[i].y_type)
5422 {
5423 case MLINE:
5424 type = (char_u *)"LINE";
5425 break;
5426 case MCHAR:
5427 type = (char_u *)"CHAR";
5428 break;
5429#ifdef FEAT_VISUAL
5430 case MBLOCK:
5431 type = (char_u *)"BLOCK";
5432 break;
5433#endif
5434 default:
5435 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005436 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 emsg(IObuff);
5438 type = (char_u *)"LINE";
5439 break;
5440 }
5441 if (y_previous == &y_regs[i])
5442 fprintf(fp, "\"");
5443 c = get_register_name(i);
5444 fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5445#ifdef FEAT_VISUAL
5446 (int)y_regs[i].y_width
5447#else
5448 0
5449#endif
5450 );
5451
5452 /* If max_num_lines < 0, then we save ALL the lines in the register */
5453 if (max_num_lines > 0 && num_lines > max_num_lines)
5454 num_lines = max_num_lines;
5455 for (j = 0; j < num_lines; j++)
5456 {
5457 putc('\t', fp);
5458 viminfo_writestring(fp, y_regs[i].y_array[j]);
5459 }
5460 }
5461}
5462#endif /* FEAT_VIMINFO */
5463
5464#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5465/*
5466 * SELECTION / PRIMARY ('*')
5467 *
5468 * Text selection stuff that uses the GUI selection register '*'. When using a
5469 * GUI this may be text from another window, otherwise it is the last text we
5470 * had highlighted with VIsual mode. With mouse support, clicking the middle
5471 * button performs the paste, otherwise you will need to do <"*p>. "
5472 * If not under X, it is synonymous with the clipboard register '+'.
5473 *
5474 * X CLIPBOARD ('+')
5475 *
5476 * Text selection stuff that uses the GUI clipboard register '+'.
5477 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5478 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5479 * otherwise you will need to do <"+p>. "
5480 * If not under X, it is synonymous with the selection register '*'.
5481 */
5482
5483/*
5484 * Routine to export any final X selection we had to the environment
5485 * so that the text is still available after vim has exited. X selections
5486 * only exist while the owning application exists, so we write to the
5487 * permanent (while X runs) store CUT_BUFFER0.
5488 * Dump the CLIPBOARD selection if we own it (it's logically the more
5489 * 'permanent' of the two), otherwise the PRIMARY one.
5490 * For now, use a hard-coded sanity limit of 1Mb of data.
5491 */
5492#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5493 void
5494x11_export_final_selection()
5495{
5496 Display *dpy;
5497 char_u *str = NULL;
5498 long_u len = 0;
5499 int motion_type = -1;
5500
5501# ifdef FEAT_GUI
5502 if (gui.in_use)
5503 dpy = X_DISPLAY;
5504 else
5505# endif
5506# ifdef FEAT_XCLIPBOARD
5507 dpy = xterm_dpy;
5508# else
5509 return;
5510# endif
5511
5512 /* Get selection to export */
5513 if (clip_plus.owned)
5514 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5515 else if (clip_star.owned)
5516 motion_type = clip_convert_selection(&str, &len, &clip_star);
5517
5518 /* Check it's OK */
5519 if (dpy != NULL && str != NULL && motion_type >= 0
5520 && len < 1024*1024 && len > 0)
5521 {
5522 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5523 XFlush(dpy);
5524 }
5525
5526 vim_free(str);
5527}
5528#endif
5529
5530 void
5531clip_free_selection(cbd)
5532 VimClipboard *cbd;
5533{
5534 struct yankreg *y_ptr = y_current;
5535
5536 if (cbd == &clip_plus)
5537 y_current = &y_regs[PLUS_REGISTER];
5538 else
5539 y_current = &y_regs[STAR_REGISTER];
5540 free_yank_all();
5541 y_current->y_size = 0;
5542 y_current = y_ptr;
5543}
5544
5545/*
5546 * Get the selected text and put it in the gui selection register '*' or '+'.
5547 */
5548 void
5549clip_get_selection(cbd)
5550 VimClipboard *cbd;
5551{
5552 struct yankreg *old_y_previous, *old_y_current;
5553 pos_T old_cursor;
5554#ifdef FEAT_VISUAL
5555 pos_T old_visual;
5556 int old_visual_mode;
5557#endif
5558 colnr_T old_curswant;
5559 int old_set_curswant;
5560 pos_T old_op_start, old_op_end;
5561 oparg_T oa;
5562 cmdarg_T ca;
5563
5564 if (cbd->owned)
5565 {
5566 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5567 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5568 return;
5569
5570 /* Get the text between clip_star.start & clip_star.end */
5571 old_y_previous = y_previous;
5572 old_y_current = y_current;
5573 old_cursor = curwin->w_cursor;
5574 old_curswant = curwin->w_curswant;
5575 old_set_curswant = curwin->w_set_curswant;
5576 old_op_start = curbuf->b_op_start;
5577 old_op_end = curbuf->b_op_end;
5578#ifdef FEAT_VISUAL
5579 old_visual = VIsual;
5580 old_visual_mode = VIsual_mode;
5581#endif
5582 clear_oparg(&oa);
5583 oa.regname = (cbd == &clip_plus ? '+' : '*');
5584 oa.op_type = OP_YANK;
5585 vim_memset(&ca, 0, sizeof(ca));
5586 ca.oap = &oa;
5587 ca.cmdchar = 'y';
5588 ca.count1 = 1;
5589 ca.retval = CA_NO_ADJ_OP_END;
5590 do_pending_operator(&ca, 0, TRUE);
5591 y_previous = old_y_previous;
5592 y_current = old_y_current;
5593 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005594 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 curwin->w_curswant = old_curswant;
5596 curwin->w_set_curswant = old_set_curswant;
5597 curbuf->b_op_start = old_op_start;
5598 curbuf->b_op_end = old_op_end;
5599#ifdef FEAT_VISUAL
5600 VIsual = old_visual;
5601 VIsual_mode = old_visual_mode;
5602#endif
5603 }
5604 else
5605 {
5606 clip_free_selection(cbd);
5607
5608 /* Try to get selected text from another window */
5609 clip_gen_request_selection(cbd);
5610 }
5611}
5612
5613/* Convert from the GUI selection string into the '*'/'+' register */
5614 void
5615clip_yank_selection(type, str, len, cbd)
5616 int type;
5617 char_u *str;
5618 long len;
5619 VimClipboard *cbd;
5620{
5621 struct yankreg *y_ptr;
5622
5623 if (cbd == &clip_plus)
5624 y_ptr = &y_regs[PLUS_REGISTER];
5625 else
5626 y_ptr = &y_regs[STAR_REGISTER];
5627
5628 clip_free_selection(cbd);
5629
5630 str_to_reg(y_ptr, type, str, len, 0L);
5631}
5632
5633/*
5634 * Convert the '*'/'+' register into a GUI selection string returned in *str
5635 * with length *len.
5636 * Returns the motion type, or -1 for failure.
5637 */
5638 int
5639clip_convert_selection(str, len, cbd)
5640 char_u **str;
5641 long_u *len;
5642 VimClipboard *cbd;
5643{
5644 char_u *p;
5645 int lnum;
5646 int i, j;
5647 int_u eolsize;
5648 struct yankreg *y_ptr;
5649
5650 if (cbd == &clip_plus)
5651 y_ptr = &y_regs[PLUS_REGISTER];
5652 else
5653 y_ptr = &y_regs[STAR_REGISTER];
5654
5655#ifdef USE_CRNL
5656 eolsize = 2;
5657#else
5658 eolsize = 1;
5659#endif
5660
5661 *str = NULL;
5662 *len = 0;
5663 if (y_ptr->y_array == NULL)
5664 return -1;
5665
5666 for (i = 0; i < y_ptr->y_size; i++)
5667 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5668
5669 /*
5670 * Don't want newline character at end of last line if we're in MCHAR mode.
5671 */
5672 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5673 *len -= eolsize;
5674
5675 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5676 if (p == NULL)
5677 return -1;
5678 lnum = 0;
5679 for (i = 0, j = 0; i < (int)*len; i++, j++)
5680 {
5681 if (y_ptr->y_array[lnum][j] == '\n')
5682 p[i] = NUL;
5683 else if (y_ptr->y_array[lnum][j] == NUL)
5684 {
5685#ifdef USE_CRNL
5686 p[i++] = '\r';
5687#endif
5688#ifdef USE_CR
5689 p[i] = '\r';
5690#else
5691 p[i] = '\n';
5692#endif
5693 lnum++;
5694 j = -1;
5695 }
5696 else
5697 p[i] = y_ptr->y_array[lnum][j];
5698 }
5699 return y_ptr->y_type;
5700}
5701
5702
5703# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5704/*
5705 * If we have written to a clipboard register, send the text to the clipboard.
5706 */
5707 static void
5708may_set_selection()
5709{
5710 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5711 {
5712 clip_own_selection(&clip_star);
5713 clip_gen_set_selection(&clip_star);
5714 }
5715 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5716 {
5717 clip_own_selection(&clip_plus);
5718 clip_gen_set_selection(&clip_plus);
5719 }
5720}
5721# endif
5722
5723#endif /* FEAT_CLIPBOARD || PROTO */
5724
5725
5726#if defined(FEAT_DND) || defined(PROTO)
5727/*
5728 * Replace the contents of the '~' register with str.
5729 */
5730 void
5731dnd_yank_drag_data(str, len)
5732 char_u *str;
5733 long len;
5734{
5735 struct yankreg *curr;
5736
5737 curr = y_current;
5738 y_current = &y_regs[TILDE_REGISTER];
5739 free_yank_all();
5740 str_to_reg(y_current, MCHAR, str, len, 0L);
5741 y_current = curr;
5742}
5743#endif
5744
5745
5746#if defined(FEAT_EVAL) || defined(PROTO)
5747/*
5748 * Return the type of a register.
5749 * Used for getregtype()
5750 * Returns MAUTO for error.
5751 */
5752 char_u
5753get_reg_type(regname, reglen)
5754 int regname;
5755 long *reglen;
5756{
5757 switch (regname)
5758 {
5759 case '%': /* file name */
5760 case '#': /* alternate file name */
5761 case '=': /* expression */
5762 case ':': /* last command line */
5763 case '/': /* last search-pattern */
5764 case '.': /* last inserted text */
5765#ifdef FEAT_SEARCHPATH
5766 case Ctrl_F: /* Filename under cursor */
5767 case Ctrl_P: /* Path under cursor, expand via "path" */
5768#endif
5769 case Ctrl_W: /* word under cursor */
5770 case Ctrl_A: /* WORD (mnemonic All) under cursor */
5771 case '_': /* black hole: always empty */
5772 return MCHAR;
5773 }
5774
5775#ifdef FEAT_CLIPBOARD
5776 regname = may_get_selection(regname);
5777#endif
5778
5779 /* Should we check for a valid name? */
5780 get_yank_register(regname, FALSE);
5781
5782 if (y_current->y_array != NULL)
5783 {
5784#ifdef FEAT_VISUAL
5785 if (reglen != NULL && y_current->y_type == MBLOCK)
5786 *reglen = y_current->y_width;
5787#endif
5788 return y_current->y_type;
5789 }
5790 return MAUTO;
5791}
5792
5793/*
5794 * Return the contents of a register as a single allocated string.
5795 * Used for "@r" in expressions and for getreg().
5796 * Returns NULL for error.
5797 */
5798 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00005799get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00005801 int allowexpr; /* allow "=" register */
5802 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 long i;
5805 char_u *retval;
5806 int allocated;
5807 long len;
5808
5809 /* Don't allow using an expression register inside an expression */
5810 if (regname == '=')
5811 {
5812 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00005813 {
5814 if (expr_src)
5815 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00005817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 return NULL;
5819 }
5820
5821 if (regname == '@') /* "@@" is used for unnamed register */
5822 regname = '"';
5823
5824 /* check for valid regname */
5825 if (regname != NUL && !valid_yank_reg(regname, FALSE))
5826 return NULL;
5827
5828#ifdef FEAT_CLIPBOARD
5829 regname = may_get_selection(regname);
5830#endif
5831
5832 if (get_spec_reg(regname, &retval, &allocated, FALSE))
5833 {
5834 if (retval == NULL)
5835 return NULL;
5836 if (!allocated)
5837 retval = vim_strsave(retval);
5838 return retval;
5839 }
5840
5841 get_yank_register(regname, FALSE);
5842 if (y_current->y_array == NULL)
5843 return NULL;
5844
5845 /*
5846 * Compute length of resulting string.
5847 */
5848 len = 0;
5849 for (i = 0; i < y_current->y_size; ++i)
5850 {
5851 len += (long)STRLEN(y_current->y_array[i]);
5852 /*
5853 * Insert a newline between lines and after last line if
5854 * y_type is MLINE.
5855 */
5856 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5857 ++len;
5858 }
5859
5860 retval = lalloc(len + 1, TRUE);
5861
5862 /*
5863 * Copy the lines of the yank register into the string.
5864 */
5865 if (retval != NULL)
5866 {
5867 len = 0;
5868 for (i = 0; i < y_current->y_size; ++i)
5869 {
5870 STRCPY(retval + len, y_current->y_array[i]);
5871 len += (long)STRLEN(retval + len);
5872
5873 /*
5874 * Insert a NL between lines and after the last line if y_type is
5875 * MLINE.
5876 */
5877 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5878 retval[len++] = '\n';
5879 }
5880 retval[len] = NUL;
5881 }
5882
5883 return retval;
5884}
5885
5886/*
5887 * Store string "str" in register "name".
5888 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5889 * If "must_append" is TRUE, always append to the register. Otherwise append
5890 * if "name" is an uppercase letter.
5891 * Note: "maxlen" and "must_append" don't work for the "/" register.
5892 * Careful: 'str' is modified, you may have to use a copy!
5893 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5894 */
5895 void
5896write_reg_contents(name, str, maxlen, must_append)
5897 int name;
5898 char_u *str;
5899 int maxlen;
5900 int must_append;
5901{
5902 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5903}
5904
5905 void
5906write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5907 int name;
5908 char_u *str;
5909 int maxlen;
5910 int must_append;
5911 int yank_type;
5912 long block_len;
5913{
5914 struct yankreg *old_y_previous, *old_y_current;
5915 long len;
5916
Bram Moolenaare7566042005-06-17 22:00:15 +00005917 if (maxlen >= 0)
5918 len = maxlen;
5919 else
5920 len = (long)STRLEN(str);
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 /* Special case: '/' search pattern */
5923 if (name == '/')
5924 {
5925 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5926 return;
5927 }
5928
Bram Moolenaare7566042005-06-17 22:00:15 +00005929#ifdef FEAT_EVAL
5930 if (name == '=')
5931 {
5932 char_u *p, *s;
5933
5934 p = vim_strnsave(str, (int)len);
5935 if (p == NULL)
5936 return;
5937 if (must_append)
5938 {
5939 s = concat_str(get_expr_line_src(), p);
5940 vim_free(p);
5941 p = s;
5942
5943 }
5944 set_expr_line(p);
5945 return;
5946 }
5947#endif
5948
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
5950 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005951 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 return;
5953 }
5954
5955 if (name == '_') /* black hole: nothing to do */
5956 return;
5957
5958 /* Don't want to change the current (unnamed) register */
5959 old_y_previous = y_previous;
5960 old_y_current = y_current;
5961
5962 get_yank_register(name, TRUE);
5963 if (!y_append && !must_append)
5964 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965#ifndef FEAT_VISUAL
5966 /* Just in case - make sure we don't use MBLOCK */
5967 if (yank_type == MBLOCK)
5968 yank_type = MAUTO;
5969#endif
5970 if (yank_type == MAUTO)
5971 yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5972 ? MLINE : MCHAR);
5973 str_to_reg(y_current, yank_type, str, len, block_len);
5974
5975# ifdef FEAT_CLIPBOARD
5976 /* Send text of clipboard register to the clipboard. */
5977 may_set_selection();
5978# endif
5979
5980 /* ':let @" = "val"' should change the meaning of the "" register */
5981 if (name != '"')
5982 y_previous = old_y_previous;
5983 y_current = old_y_current;
5984}
5985#endif /* FEAT_EVAL */
5986
5987#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5988/*
5989 * Put a string into a register. When the register is not empty, the string
5990 * is appended.
5991 */
5992 static void
5993str_to_reg(y_ptr, type, str, len, blocklen)
5994 struct yankreg *y_ptr; /* pointer to yank register */
5995 int type; /* MCHAR, MLINE or MBLOCK */
5996 char_u *str; /* string to put in register */
5997 long len; /* length of string */
5998 long blocklen; /* width of Visual block */
5999{
6000 int lnum;
6001 long start;
6002 long i;
6003 int extra;
6004 int newlines; /* number of lines added */
6005 int extraline = 0; /* extra line at the end */
6006 int append = FALSE; /* append to last line in register */
6007 char_u *s;
6008 char_u **pp;
6009#ifdef FEAT_VISUAL
6010 long maxlen;
6011#endif
6012
6013 if (y_ptr->y_array == NULL) /* NULL means emtpy register */
6014 y_ptr->y_size = 0;
6015
6016 /*
6017 * Count the number of lines within the string
6018 */
6019 newlines = 0;
6020 for (i = 0; i < len; i++)
6021 if (str[i] == '\n')
6022 ++newlines;
6023 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6024 {
6025 extraline = 1;
6026 ++newlines; /* count extra newline at the end */
6027 }
6028 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6029 {
6030 append = TRUE;
6031 --newlines; /* uncount newline when appending first line */
6032 }
6033
6034 /*
6035 * Allocate an array to hold the pointers to the new register lines.
6036 * If the register was not empty, move the existing lines to the new array.
6037 */
6038 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6039 * sizeof(char_u *), TRUE);
6040 if (pp == NULL) /* out of memory */
6041 return;
6042 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6043 pp[lnum] = y_ptr->y_array[lnum];
6044 vim_free(y_ptr->y_array);
6045 y_ptr->y_array = pp;
6046#ifdef FEAT_VISUAL
6047 maxlen = 0;
6048#endif
6049
6050 /*
6051 * Find the end of each line and save it into the array.
6052 */
6053 for (start = 0; start < len + extraline; start += i + 1)
6054 {
6055 for (i = start; i < len; ++i) /* find the end of the line */
6056 if (str[i] == '\n')
6057 break;
6058 i -= start; /* i is now length of line */
6059#ifdef FEAT_VISUAL
6060 if (i > maxlen)
6061 maxlen = i;
6062#endif
6063 if (append)
6064 {
6065 --lnum;
6066 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6067 }
6068 else
6069 extra = 0;
6070 s = alloc((unsigned)(i + extra + 1));
6071 if (s == NULL)
6072 break;
6073 if (extra)
6074 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6075 if (append)
6076 vim_free(y_ptr->y_array[lnum]);
6077 if (i)
6078 mch_memmove(s + extra, str + start, (size_t)i);
6079 extra += i;
6080 s[extra] = NUL;
6081 y_ptr->y_array[lnum++] = s;
6082 while (--extra >= 0)
6083 {
6084 if (*s == NUL)
6085 *s = '\n'; /* replace NUL with newline */
6086 ++s;
6087 }
6088 append = FALSE; /* only first line is appended */
6089 }
6090 y_ptr->y_type = type;
6091 y_ptr->y_size = lnum;
6092# ifdef FEAT_VISUAL
6093 if (type == MBLOCK)
6094 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6095 else
6096 y_ptr->y_width = 0;
6097# endif
6098}
6099#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6100
6101 void
6102clear_oparg(oap)
6103 oparg_T *oap;
6104{
6105 vim_memset(oap, 0, sizeof(oparg_T));
6106}
6107
Bram Moolenaar7c626922005-02-07 22:01:03 +00006108static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006111 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 *
6113 * "Words" are counted by looking for boundaries between non-space and
6114 * space characters. (it seems to produce results that match 'wc'.)
6115 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006116 * Return value is byte count; word count for the line is added to "*wc".
6117 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 *
6119 * The function will only examine the first "limit" characters in the
6120 * line, stopping if it encounters an end-of-line (NUL byte). In that
6121 * case, eol_size will be added to the character count to account for
6122 * the size of the EOL character.
6123 */
6124 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006125line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 char_u *line;
6127 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006128 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 long limit;
6130 int eol_size;
6131{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006132 long i;
6133 long words = 0;
6134 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 int is_word = 0;
6136
Bram Moolenaar7c626922005-02-07 22:01:03 +00006137 for (i = 0; line[i] && i < limit; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
6139 if (is_word)
6140 {
6141 if (vim_isspace(line[i]))
6142 {
6143 words++;
6144 is_word = 0;
6145 }
6146 }
6147 else if (!vim_isspace(line[i]))
6148 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006149 ++chars;
6150#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006151 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006152#else
6153 ++i;
6154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 }
6156
6157 if (is_word)
6158 words++;
6159 *wc += words;
6160
6161 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006162 if (line[i] == NUL && i < limit)
6163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006165 chars += eol_size;
6166 }
6167 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 return i;
6169}
6170
6171/*
6172 * Give some info about the position of the cursor (for "g CTRL-G").
6173 * In Visual mode, give some info about the selected region. (In this case,
6174 * the *_count_cursor variables store running totals for the selection.)
6175 */
6176 void
6177cursor_pos_info()
6178{
6179 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006180 char_u buf1[50];
6181 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006183 long byte_count = 0;
6184 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 long char_count = 0;
6186 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 long word_count = 0;
6188 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006189 int eol_size;
6190 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#ifdef FEAT_VISUAL
6192 long line_count_selected = 0;
6193 pos_T min_pos, max_pos;
6194 oparg_T oparg;
6195 struct block_def bd;
6196#endif
6197
6198 /*
6199 * Compute the length of the file in characters.
6200 */
6201 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6202 {
6203 MSG(_(no_lines_msg));
6204 }
6205 else
6206 {
6207 if (get_fileformat(curbuf) == EOL_DOS)
6208 eol_size = 2;
6209 else
6210 eol_size = 1;
6211
6212#ifdef FEAT_VISUAL
6213 if (VIsual_active)
6214 {
6215 if (lt(VIsual, curwin->w_cursor))
6216 {
6217 min_pos = VIsual;
6218 max_pos = curwin->w_cursor;
6219 }
6220 else
6221 {
6222 min_pos = curwin->w_cursor;
6223 max_pos = VIsual;
6224 }
6225 if (*p_sel == 'e' && max_pos.col > 0)
6226 --max_pos.col;
6227
6228 if (VIsual_mode == Ctrl_V)
6229 {
6230 oparg.is_VIsual = 1;
6231 oparg.block_mode = TRUE;
6232 oparg.op_type = OP_NOP;
6233 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006234 &oparg.start_vcol, &oparg.end_vcol);
6235 if (curwin->w_curswant == MAXCOL)
6236 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 /* Swap the start, end vcol if needed */
6238 if (oparg.end_vcol < oparg.start_vcol)
6239 {
6240 oparg.end_vcol += oparg.start_vcol;
6241 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6242 oparg.end_vcol -= oparg.start_vcol;
6243 }
6244 }
6245 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6246 }
6247#endif
6248
6249 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6250 {
6251 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006252 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
6254 ui_breakcheck();
6255 if (got_int)
6256 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006257 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 }
6259
6260#ifdef FEAT_VISUAL
6261 /* Do extra processing for VIsual mode. */
6262 if (VIsual_active
6263 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6264 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006265 char_u *s = NULL;
6266 long len = 0L;
6267
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 switch (VIsual_mode)
6269 {
6270 case Ctrl_V:
6271# ifdef FEAT_VIRTUALEDIT
6272 virtual_op = virtual_active();
6273# endif
6274 block_prep(&oparg, &bd, lnum, 0);
6275# ifdef FEAT_VIRTUALEDIT
6276 virtual_op = MAYBE;
6277# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006278 s = bd.textstart;
6279 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 break;
6281 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006282 s = ml_get(lnum);
6283 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 break;
6285 case 'v':
6286 {
6287 colnr_T start_col = (lnum == min_pos.lnum)
6288 ? min_pos.col : 0;
6289 colnr_T end_col = (lnum == max_pos.lnum)
6290 ? max_pos.col - start_col + 1 : MAXCOL;
6291
Bram Moolenaardef9e822004-12-31 20:58:58 +00006292 s = ml_get(lnum) + start_col;
6293 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 }
6295 break;
6296 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006297 if (s != NULL)
6298 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006299 byte_count_cursor += line_count_info(s, &word_count_cursor,
6300 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006301 if (lnum == curbuf->b_ml.ml_line_count
6302 && !curbuf->b_p_eol
6303 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006304 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006305 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
6308 else
6309#endif
6310 {
6311 /* In non-visual mode, check for the line the cursor is on */
6312 if (lnum == curwin->w_cursor.lnum)
6313 {
6314 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006315 char_count_cursor += char_count;
6316 byte_count_cursor = byte_count +
6317 line_count_info(ml_get(lnum),
6318 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 (long)(curwin->w_cursor.col + 1), eol_size);
6320 }
6321 }
6322 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006323 byte_count += line_count_info(ml_get(lnum), &word_count,
6324 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 }
6326
6327 /* Correction for when last line doesn't have an EOL. */
6328 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006329 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331#ifdef FEAT_VISUAL
6332 if (VIsual_active)
6333 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006334 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
6336 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006337 &max_pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 sprintf((char *)buf1, _("%ld Cols; "),
6339 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6340 }
6341 else
6342 buf1[0] = NUL;
6343
Bram Moolenaar7c626922005-02-07 22:01:03 +00006344 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006345 && char_count == byte_count)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006346 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 buf1, line_count_selected,
6348 (long)curbuf->b_ml.ml_line_count,
6349 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006350 byte_count_cursor, byte_count);
6351 else
6352 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6353 buf1, line_count_selected,
6354 (long)curbuf->b_ml.ml_line_count,
6355 word_count_cursor, word_count,
6356 char_count_cursor, char_count,
6357 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 }
6359 else
6360#endif
6361 {
6362 p = ml_get_curline();
6363 validate_virtcol();
6364 col_print(buf1, (int)curwin->w_cursor.col + 1,
6365 (int)curwin->w_virtcol + 1);
6366 col_print(buf2, (int)STRLEN(p), linetabsize(p));
6367
Bram Moolenaar7c626922005-02-07 22:01:03 +00006368 if (char_count_cursor == byte_count_cursor
6369 && char_count == byte_count)
6370 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 (char *)buf1, (char *)buf2,
6372 (long)curwin->w_cursor.lnum,
6373 (long)curbuf->b_ml.ml_line_count,
6374 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006375 byte_count_cursor, byte_count);
6376 else
6377 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6378 (char *)buf1, (char *)buf2,
6379 (long)curwin->w_cursor.lnum,
6380 (long)curbuf->b_ml.ml_line_count,
6381 word_count_cursor, word_count,
6382 char_count_cursor, char_count,
6383 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385
6386#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006387 byte_count = bomb_size();
6388 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006390 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391#endif
6392 /* Don't shorten this message, the user asked for it. */
6393 p = p_shm;
6394 p_shm = (char_u *)"";
6395 msg(IObuff);
6396 p_shm = p;
6397 }
6398}