blob: 3fe69fc75e752243ef8b8f415ae50c71889eeb35 [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{
930 static struct yankreg *reg;
931 int i;
932
933#ifdef FEAT_CLIPBOARD
934 /* When Visual area changed, may have to update selection. Obtain the
935 * selection too. */
936 if (name == '*' && clip_star.available && clip_isautosel())
937 {
938 clip_update_selection();
939 may_get_selection(name);
940 }
941#endif
942
943 get_yank_register(name, 0);
944 reg = (struct yankreg *)alloc((unsigned)sizeof(struct yankreg));
945 if (reg != NULL)
946 {
947 *reg = *y_current;
948 if (copy)
949 {
950 /* If we run out of memory some or all of the lines are empty. */
951 if (reg->y_size == 0)
952 reg->y_array = NULL;
953 else
954 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *)
955 * reg->y_size));
956 if (reg->y_array != NULL)
957 {
958 for (i = 0; i < reg->y_size; ++i)
959 reg->y_array[i] = vim_strsave(y_current->y_array[i]);
960 }
961 }
962 else
963 y_current->y_array = NULL;
964 }
965 return (void *)reg;
966}
967
968/*
969 * Put "reg" into register "name". Free any previous contents.
970 */
971 void
972put_register(name, reg)
973 int name;
974 void *reg;
975{
976 get_yank_register(name, 0);
977 free_yank_all();
978 *y_current = *(struct yankreg *)reg;
979
980# ifdef FEAT_CLIPBOARD
981 /* Send text written to clipboard register to the clipboard. */
982 may_set_selection();
983# endif
984}
985#endif
986
987#if defined(FEAT_MOUSE) || defined(PROTO)
988/*
989 * return TRUE if the current yank register has type MLINE
990 */
991 int
992yank_register_mline(regname)
993 int regname;
994{
995 if (regname != 0 && !valid_yank_reg(regname, FALSE))
996 return FALSE;
997 if (regname == '_') /* black hole is always empty */
998 return FALSE;
999 get_yank_register(regname, FALSE);
1000 return (y_current->y_type == MLINE);
1001}
1002#endif
1003
1004/*
1005 * start or stop recording into a yank register
1006 *
1007 * return FAIL for failure, OK otherwise
1008 */
1009 int
1010do_record(c)
1011 int c;
1012{
1013 char_u *p;
1014 static int regname;
1015 struct yankreg *old_y_previous, *old_y_current;
1016 int retval;
1017
1018 if (Recording == FALSE) /* start recording */
1019 {
1020 /* registers 0-9, a-z and " are allowed */
1021 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
1022 retval = FAIL;
1023 else
1024 {
1025 Recording = TRUE;
1026 showmode();
1027 regname = c;
1028 retval = OK;
1029 }
1030 }
1031 else /* stop recording */
1032 {
1033 /*
1034 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, so
1035 * that the register can be put into the typeahead buffer without
1036 * translation.
1037 */
1038 Recording = FALSE;
1039 MSG("");
1040 p = get_recorded();
1041 if (p == NULL)
1042 retval = FAIL;
1043 else
1044 {
Bram Moolenaar81b85872007-03-04 20:22:01 +00001045 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */
1046 vim_unescape_csi(p);
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 /*
1049 * We don't want to change the default register here, so save and
1050 * restore the current register name.
1051 */
1052 old_y_previous = y_previous;
1053 old_y_current = y_current;
1054
1055 retval = stuff_yank(regname, p);
1056
1057 y_previous = old_y_previous;
1058 y_current = old_y_current;
1059 }
1060 }
1061 return retval;
1062}
1063
1064/*
1065 * Stuff string "p" into yank register "regname" as a single line (append if
1066 * uppercase). "p" must have been alloced.
1067 *
1068 * return FAIL for failure, OK otherwise
1069 */
1070 static int
1071stuff_yank(regname, p)
1072 int regname;
1073 char_u *p;
1074{
1075 char_u *lp;
1076 char_u **pp;
1077
1078 /* check for read-only register */
1079 if (regname != 0 && !valid_yank_reg(regname, TRUE))
1080 {
1081 vim_free(p);
1082 return FAIL;
1083 }
1084 if (regname == '_') /* black hole: don't do anything */
1085 {
1086 vim_free(p);
1087 return OK;
1088 }
1089 get_yank_register(regname, TRUE);
1090 if (y_append && y_current->y_array != NULL)
1091 {
1092 pp = &(y_current->y_array[y_current->y_size - 1]);
1093 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE);
1094 if (lp == NULL)
1095 {
1096 vim_free(p);
1097 return FAIL;
1098 }
1099 STRCPY(lp, *pp);
1100 STRCAT(lp, p);
1101 vim_free(p);
1102 vim_free(*pp);
1103 *pp = lp;
1104 }
1105 else
1106 {
1107 free_yank_all();
1108 if ((y_current->y_array =
1109 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL)
1110 {
1111 vim_free(p);
1112 return FAIL;
1113 }
1114 y_current->y_array[0] = p;
1115 y_current->y_size = 1;
1116 y_current->y_type = MCHAR; /* used to be MLINE, why? */
1117 }
1118 return OK;
1119}
1120
1121/*
1122 * execute a yank register: copy it into the stuff buffer
1123 *
1124 * return FAIL for failure, OK otherwise
1125 */
1126 int
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001127do_execreg(regname, colon, addcr, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 int regname;
1129 int colon; /* insert ':' before each line */
1130 int addcr; /* always add '\n' to end of line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001131 int silent; /* set "silent" flag in typeahead buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 static int lastc = NUL;
1134 long i;
1135 char_u *p;
1136 int retval = OK;
1137 int remap;
1138
1139 if (regname == '@') /* repeat previous one */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001140 {
1141 if (lastc == NUL)
1142 {
1143 EMSG(_("E748: No previously used register"));
1144 return FAIL;
1145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 regname = lastc;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 /* check for valid regname */
1149 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001150 {
1151 emsg_invreg(regname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 return FAIL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 lastc = regname;
1155
1156#ifdef FEAT_CLIPBOARD
1157 regname = may_get_selection(regname);
1158#endif
1159
1160 if (regname == '_') /* black hole: don't stuff anything */
1161 return OK;
1162
1163#ifdef FEAT_CMDHIST
1164 if (regname == ':') /* use last command line */
1165 {
1166 if (last_cmdline == NULL)
1167 {
1168 EMSG(_(e_nolastcmd));
1169 return FAIL;
1170 }
1171 vim_free(new_last_cmdline); /* don't keep the cmdline containing @: */
1172 new_last_cmdline = NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001173 /* Escape all control characters with a CTRL-V */
1174 p = vim_strsave_escaped_ext(last_cmdline,
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001175 (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 +00001176 if (p != NULL)
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001177 {
1178 /* When in Visual mode "'<,'>" will be prepended to the command.
1179 * Remove it when it's already there. */
1180 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001181 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001182 else
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001183 retval = put_in_typebuf(p, TRUE, TRUE, silent);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00001184 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001185 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187#endif
1188#ifdef FEAT_EVAL
1189 else if (regname == '=')
1190 {
1191 p = get_expr_line();
1192 if (p == NULL)
1193 return FAIL;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001194 retval = put_in_typebuf(p, TRUE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 vim_free(p);
1196 }
1197#endif
1198 else if (regname == '.') /* use last inserted text */
1199 {
1200 p = get_last_insert_save();
1201 if (p == NULL)
1202 {
1203 EMSG(_(e_noinstext));
1204 return FAIL;
1205 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001206 retval = put_in_typebuf(p, FALSE, colon, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 vim_free(p);
1208 }
1209 else
1210 {
1211 get_yank_register(regname, FALSE);
1212 if (y_current->y_array == NULL)
1213 return FAIL;
1214
1215 /* Disallow remaping for ":@r". */
1216 remap = colon ? REMAP_NONE : REMAP_YES;
1217
1218 /*
1219 * Insert lines into typeahead buffer, from last one to first one.
1220 */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001221 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 for (i = y_current->y_size; --i >= 0; )
1223 {
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001224 char_u *escaped;
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 /* insert NL between lines and after last line if type is MLINE */
1227 if (y_current->y_type == MLINE || i < y_current->y_size - 1
1228 || addcr)
1229 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001230 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 return FAIL;
1232 }
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001233 escaped = vim_strsave_escape_csi(y_current->y_array[i]);
1234 if (escaped == NULL)
1235 return FAIL;
1236 retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
1237 vim_free(escaped);
1238 if (retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 return FAIL;
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001240 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 == FAIL)
1242 return FAIL;
1243 }
1244 Exec_reg = TRUE; /* disable the 'q' command */
1245 }
1246 return retval;
1247}
1248
1249/*
1250 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
1251 * used only after other typeahead has been processed.
1252 */
1253 static void
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001254put_reedit_in_typebuf(silent)
1255 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256{
1257 char_u buf[3];
1258
1259 if (restart_edit != NUL)
1260 {
1261 if (restart_edit == 'V')
1262 {
1263 buf[0] = 'g';
1264 buf[1] = 'R';
1265 buf[2] = NUL;
1266 }
1267 else
1268 {
1269 buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
1270 buf[1] = NUL;
1271 }
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001272 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 restart_edit = NUL;
1274 }
1275}
1276
1277 static int
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001278put_in_typebuf(s, esc, colon, silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 char_u *s;
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001280 int esc; /* Escape CSI characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 int colon; /* add ':' before the line */
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001282 int silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int retval = OK;
1285
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001286 put_reedit_in_typebuf(silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (colon)
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001288 retval = ins_typebuf((char_u *)"\n", REMAP_YES, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (retval == OK)
Bram Moolenaar7f6ca072007-02-27 16:21:38 +00001290 {
1291 char_u *p;
1292
1293 if (esc)
1294 p = vim_strsave_escape_csi(s);
1295 else
1296 p = s;
1297 if (p == NULL)
1298 retval = FAIL;
1299 else
1300 retval = ins_typebuf(p, REMAP_YES, 0, TRUE, silent);
1301 if (esc)
1302 vim_free(p);
1303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (colon && retval == OK)
Bram Moolenaard333d1e2006-11-07 17:43:47 +00001305 retval = ins_typebuf((char_u *)":", REMAP_YES, 0, TRUE, silent);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 return retval;
1307}
1308
1309/*
1310 * Insert a yank register: copy it into the Read buffer.
1311 * Used by CTRL-R command and middle mouse button in insert mode.
1312 *
1313 * return FAIL for failure, OK otherwise
1314 */
1315 int
1316insert_reg(regname, literally)
1317 int regname;
1318 int literally; /* insert literally, not as if typed */
1319{
1320 long i;
1321 int retval = OK;
1322 char_u *arg;
1323 int allocated;
1324
1325 /*
1326 * It is possible to get into an endless loop by having CTRL-R a in
1327 * register a and then, in insert mode, doing CTRL-R a.
1328 * If you hit CTRL-C, the loop will be broken here.
1329 */
1330 ui_breakcheck();
1331 if (got_int)
1332 return FAIL;
1333
1334 /* check for valid regname */
1335 if (regname != NUL && !valid_yank_reg(regname, FALSE))
1336 return FAIL;
1337
1338#ifdef FEAT_CLIPBOARD
1339 regname = may_get_selection(regname);
1340#endif
1341
1342 if (regname == '.') /* insert last inserted text */
1343 retval = stuff_inserted(NUL, 1L, TRUE);
1344 else if (get_spec_reg(regname, &arg, &allocated, TRUE))
1345 {
1346 if (arg == NULL)
1347 return FAIL;
1348 stuffescaped(arg, literally);
1349 if (allocated)
1350 vim_free(arg);
1351 }
1352 else /* name or number register */
1353 {
1354 get_yank_register(regname, FALSE);
1355 if (y_current->y_array == NULL)
1356 retval = FAIL;
1357 else
1358 {
1359 for (i = 0; i < y_current->y_size; ++i)
1360 {
1361 stuffescaped(y_current->y_array[i], literally);
1362 /*
1363 * Insert a newline between lines and after last line if
1364 * y_type is MLINE.
1365 */
1366 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
1367 stuffcharReadbuff('\n');
1368 }
1369 }
1370 }
1371
1372 return retval;
1373}
1374
1375/*
1376 * Stuff a string into the typeahead buffer, such that edit() will insert it
1377 * literally ("literally" TRUE) or interpret is as typed characters.
1378 */
1379 static void
1380stuffescaped(arg, literally)
1381 char_u *arg;
1382 int literally;
1383{
1384 int c;
1385 char_u *start;
1386
1387 while (*arg != NUL)
1388 {
1389 /* Stuff a sequence of normal ASCII characters, that's fast. Also
1390 * stuff K_SPECIAL to get the effect of a special key when "literally"
1391 * is TRUE. */
1392 start = arg;
1393 while ((*arg >= ' '
1394#ifndef EBCDIC
1395 && *arg < DEL /* EBCDIC: chars above space are normal */
1396#endif
1397 )
1398 || (*arg == K_SPECIAL && !literally))
1399 ++arg;
1400 if (arg > start)
1401 stuffReadbuffLen(start, (long)(arg - start));
1402
1403 /* stuff a single special character */
1404 if (*arg != NUL)
1405 {
1406#ifdef FEAT_MBYTE
1407 if (has_mbyte)
1408 c = mb_ptr2char_adv(&arg);
1409 else
1410#endif
1411 c = *arg++;
1412 if (literally && ((c < ' ' && c != TAB) || c == DEL))
1413 stuffcharReadbuff(Ctrl_V);
1414 stuffcharReadbuff(c);
1415 }
1416 }
1417}
1418
1419/*
1420 * If "regname" is a special register, return a pointer to its value.
1421 */
Bram Moolenaar8299df92004-07-10 09:47:34 +00001422 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423get_spec_reg(regname, argp, allocated, errmsg)
1424 int regname;
1425 char_u **argp;
1426 int *allocated;
1427 int errmsg; /* give error message when failing */
1428{
1429 int cnt;
1430
1431 *argp = NULL;
1432 *allocated = FALSE;
1433 switch (regname)
1434 {
1435 case '%': /* file name */
1436 if (errmsg)
1437 check_fname(); /* will give emsg if not set */
1438 *argp = curbuf->b_fname;
1439 return TRUE;
1440
1441 case '#': /* alternate file name */
1442 *argp = getaltfname(errmsg); /* may give emsg if not set */
1443 return TRUE;
1444
1445#ifdef FEAT_EVAL
1446 case '=': /* result of expression */
1447 *argp = get_expr_line();
1448 *allocated = TRUE;
1449 return TRUE;
1450#endif
1451
1452 case ':': /* last command line */
1453 if (last_cmdline == NULL && errmsg)
1454 EMSG(_(e_nolastcmd));
1455 *argp = last_cmdline;
1456 return TRUE;
1457
1458 case '/': /* last search-pattern */
1459 if (last_search_pat() == NULL && errmsg)
1460 EMSG(_(e_noprevre));
1461 *argp = last_search_pat();
1462 return TRUE;
1463
1464 case '.': /* last inserted text */
1465 *argp = get_last_insert_save();
1466 *allocated = TRUE;
1467 if (*argp == NULL && errmsg)
1468 EMSG(_(e_noinstext));
1469 return TRUE;
1470
1471#ifdef FEAT_SEARCHPATH
1472 case Ctrl_F: /* Filename under cursor */
1473 case Ctrl_P: /* Path under cursor, expand via "path" */
1474 if (!errmsg)
1475 return FALSE;
1476 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001477 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 *allocated = TRUE;
1479 return TRUE;
1480#endif
1481
1482 case Ctrl_W: /* word under cursor */
1483 case Ctrl_A: /* WORD (mnemonic All) under cursor */
1484 if (!errmsg)
1485 return FALSE;
1486 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
1487 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
1488 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
1489 *allocated = TRUE;
1490 return TRUE;
1491
1492 case '_': /* black hole: always empty */
1493 *argp = (char_u *)"";
1494 return TRUE;
1495 }
1496
1497 return FALSE;
1498}
1499
1500/*
Bram Moolenaar8299df92004-07-10 09:47:34 +00001501 * Paste a yank register into the command line.
1502 * Only for non-special registers.
1503 * Used by CTRL-R command in command-line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 * insert_reg() can't be used here, because special characters from the
1505 * register contents will be interpreted as commands.
1506 *
1507 * return FAIL for failure, OK otherwise
1508 */
1509 int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001510cmdline_paste_reg(regname, literally, remcr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 int regname;
1512 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001513 int remcr; /* don't add trailing CR */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
1517 get_yank_register(regname, FALSE);
1518 if (y_current->y_array == NULL)
1519 return FAIL;
1520
1521 for (i = 0; i < y_current->y_size; ++i)
1522 {
1523 cmdline_paste_str(y_current->y_array[i], literally);
1524
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001525 /* Insert ^M between lines and after last line if type is MLINE.
1526 * Don't do this when "remcr" is TRUE and the next line is empty. */
1527 if (y_current->y_type == MLINE
1528 || (i < y_current->y_size - 1
1529 && !(remcr
1530 && i == y_current->y_size - 2
1531 && *y_current->y_array[i + 1] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 cmdline_paste_str((char_u *)"\r", literally);
1533
1534 /* Check for CTRL-C, in case someone tries to paste a few thousand
1535 * lines and gets bored. */
1536 ui_breakcheck();
1537 if (got_int)
1538 return FAIL;
1539 }
1540 return OK;
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1544/*
1545 * Adjust the register name pointed to with "rp" for the clipboard being
1546 * used always and the clipboard being available.
1547 */
1548 void
1549adjust_clip_reg(rp)
1550 int *rp;
1551{
1552 /* If no reg. specified, and "unnamed" is in 'clipboard', use '*' reg. */
1553 if (*rp == 0 && clip_unnamed)
1554 *rp = '*';
1555 if (!clip_star.available && *rp == '*')
1556 *rp = 0;
1557 if (!clip_plus.available && *rp == '+')
1558 *rp = 0;
1559}
1560#endif
1561
1562/*
1563 * op_delete - handle a delete operation
1564 *
1565 * return FAIL if undo failed, OK otherwise.
1566 */
1567 int
1568op_delete(oap)
1569 oparg_T *oap;
1570{
1571 int n;
1572 linenr_T lnum;
1573 char_u *ptr;
1574#ifdef FEAT_VISUAL
1575 char_u *newp, *oldp;
1576 struct block_def bd;
1577#endif
1578 linenr_T old_lcount = curbuf->b_ml.ml_line_count;
1579 int did_yank = FALSE;
1580
1581 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */
1582 return OK;
1583
1584 /* Nothing to delete, return here. Do prepare undo, for op_change(). */
1585 if (oap->empty)
1586 return u_save_cursor();
1587
1588 if (!curbuf->b_p_ma)
1589 {
1590 EMSG(_(e_modifiable));
1591 return FAIL;
1592 }
1593
1594#ifdef FEAT_CLIPBOARD
1595 adjust_clip_reg(&oap->regname);
1596#endif
1597
1598#ifdef FEAT_MBYTE
1599 if (has_mbyte)
1600 mb_adjust_opend(oap);
1601#endif
1602
1603/*
1604 * Imitate the strange Vi behaviour: If the delete spans more than one line
1605 * and motion_type == MCHAR and the result is a blank line, make the delete
1606 * linewise. Don't do this for the change command or Visual mode.
1607 */
1608 if ( oap->motion_type == MCHAR
1609#ifdef FEAT_VISUAL
1610 && !oap->is_VIsual
Bram Moolenaarec2dad62005-01-02 11:36:03 +00001611 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612#endif
1613 && oap->line_count > 1
1614 && oap->op_type == OP_DELETE)
1615 {
1616 ptr = ml_get(oap->end.lnum) + oap->end.col + oap->inclusive;
1617 ptr = skipwhite(ptr);
1618 if (*ptr == NUL && inindent(0))
1619 oap->motion_type = MLINE;
1620 }
1621
1622/*
1623 * Check for trying to delete (e.g. "D") in an empty line.
1624 * Note: For the change operator it is ok.
1625 */
1626 if ( oap->motion_type == MCHAR
1627 && oap->line_count == 1
1628 && oap->op_type == OP_DELETE
1629 && *ml_get(oap->start.lnum) == NUL)
1630 {
1631 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001632 * It's an error to operate on an empty region, when 'E' included in
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 * 'cpoptions' (Vi compatible).
1634 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001635#ifdef FEAT_VIRTUALEDIT
1636 if (virtual_op)
1637 /* Virtual editing: Nothing gets deleted, but we set the '[ and ']
1638 * marks as if it happened. */
1639 goto setmarks;
1640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL)
1642 beep_flush();
1643 return OK;
1644 }
1645
1646/*
1647 * Do a yank of whatever we're about to delete.
1648 * If a yank register was specified, put the deleted text into that register.
1649 * For the black hole register '_' don't yank anything.
1650 */
1651 if (oap->regname != '_')
1652 {
1653 if (oap->regname != 0)
1654 {
1655 /* check for read-only register */
1656 if (!valid_yank_reg(oap->regname, TRUE))
1657 {
1658 beep_flush();
1659 return OK;
1660 }
1661 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */
1662 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */
1663 did_yank = TRUE;
1664 }
1665
1666 /*
1667 * Put deleted text into register 1 and shift number registers if the
1668 * delete contains a line break, or when a regname has been specified.
1669 */
1670 if (oap->regname != 0 || oap->motion_type == MLINE
1671 || oap->line_count > 1 || oap->use_reg_one)
1672 {
1673 y_current = &y_regs[9];
1674 free_yank_all(); /* free register nine */
1675 for (n = 9; n > 1; --n)
1676 y_regs[n] = y_regs[n - 1];
1677 y_previous = y_current = &y_regs[1];
1678 y_regs[1].y_array = NULL; /* set register one to empty */
1679 if (op_yank(oap, TRUE, FALSE) == OK)
1680 did_yank = TRUE;
1681 }
1682
1683 /* Yank into small delete register when no register specified and the
1684 * delete is within one line. */
1685 if (oap->regname == 0 && oap->motion_type != MLINE
1686 && oap->line_count == 1)
1687 {
1688 oap->regname = '-';
1689 get_yank_register(oap->regname, TRUE);
1690 if (op_yank(oap, TRUE, FALSE) == OK)
1691 did_yank = TRUE;
1692 oap->regname = 0;
1693 }
1694
1695 /*
1696 * If there's too much stuff to fit in the yank register, then get a
1697 * confirmation before doing the delete. This is crude, but simple.
1698 * And it avoids doing a delete of something we can't put back if we
1699 * want.
1700 */
1701 if (!did_yank)
1702 {
1703 int msg_silent_save = msg_silent;
1704
1705 msg_silent = 0; /* must display the prompt */
1706 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE);
1707 msg_silent = msg_silent_save;
1708 if (n != 'y')
1709 {
1710 EMSG(_(e_abort));
1711 return FAIL;
1712 }
1713 }
1714 }
1715
1716#ifdef FEAT_VISUAL
1717/*
1718 * block mode delete
1719 */
1720 if (oap->block_mode)
1721 {
1722 if (u_save((linenr_T)(oap->start.lnum - 1),
1723 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1724 return FAIL;
1725
1726 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum)
1727 {
1728 block_prep(oap, &bd, lnum, TRUE);
1729 if (bd.textlen == 0) /* nothing to delete */
1730 continue;
1731
1732 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */
1733 if (lnum == curwin->w_cursor.lnum)
1734 {
1735 curwin->w_cursor.col = bd.textcol + bd.startspaces;
1736# ifdef FEAT_VIRTUALEDIT
1737 curwin->w_cursor.coladd = 0;
1738# endif
1739 }
1740
1741 /* n == number of chars deleted
1742 * If we delete a TAB, it may be replaced by several characters.
1743 * Thus the number of characters may increase!
1744 */
1745 n = bd.textlen - bd.startspaces - bd.endspaces;
1746 oldp = ml_get(lnum);
1747 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n);
1748 if (newp == NULL)
1749 continue;
1750 /* copy up to deleted part */
1751 mch_memmove(newp, oldp, (size_t)bd.textcol);
1752 /* insert spaces */
1753 copy_spaces(newp + bd.textcol,
1754 (size_t)(bd.startspaces + bd.endspaces));
1755 /* copy the part after the deleted part */
1756 oldp += bd.textcol + bd.textlen;
1757 mch_memmove(newp + bd.textcol + bd.startspaces + bd.endspaces,
1758 oldp, STRLEN(oldp) + 1);
1759 /* replace the line */
1760 ml_replace(lnum, newp, FALSE);
1761 }
1762
1763 check_cursor_col();
1764 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1765 oap->end.lnum + 1, 0L);
1766 oap->line_count = 0; /* no lines deleted */
1767 }
1768 else
1769#endif
1770 if (oap->motion_type == MLINE)
1771 {
1772 if (oap->op_type == OP_CHANGE)
1773 {
1774 /* Delete the lines except the first one. Temporarily move the
1775 * cursor to the next line. Save the current line number, if the
1776 * last line is deleted it may be changed.
1777 */
1778 if (oap->line_count > 1)
1779 {
1780 lnum = curwin->w_cursor.lnum;
1781 ++curwin->w_cursor.lnum;
1782 del_lines((long)(oap->line_count - 1), TRUE);
1783 curwin->w_cursor.lnum = lnum;
1784 }
1785 if (u_save_cursor() == FAIL)
1786 return FAIL;
1787 if (curbuf->b_p_ai) /* don't delete indent */
1788 {
1789 beginline(BL_WHITE); /* cursor on first non-white */
1790 did_ai = TRUE; /* delete the indent when ESC hit */
1791 ai_col = curwin->w_cursor.col;
1792 }
1793 else
1794 beginline(0); /* cursor in column 0 */
1795 truncate_line(FALSE); /* delete the rest of the line */
1796 /* leave cursor past last char in line */
1797 if (oap->line_count > 1)
1798 u_clearline(); /* "U" command not possible after "2cc" */
1799 }
1800 else
1801 {
1802 del_lines(oap->line_count, TRUE);
1803 beginline(BL_WHITE | BL_FIX);
1804 u_clearline(); /* "U" command not possible after "dd" */
1805 }
1806 }
1807 else
1808 {
1809#ifdef FEAT_VIRTUALEDIT
1810 if (virtual_op)
1811 {
1812 int endcol = 0;
1813
1814 /* For virtualedit: break the tabs that are partly included. */
1815 if (gchar_pos(&oap->start) == '\t')
1816 {
1817 if (u_save_cursor() == FAIL) /* save first line for undo */
1818 return FAIL;
1819 if (oap->line_count == 1)
1820 endcol = getviscol2(oap->end.col, oap->end.coladd);
1821 coladvance_force(getviscol2(oap->start.col, oap->start.coladd));
1822 oap->start = curwin->w_cursor;
1823 if (oap->line_count == 1)
1824 {
1825 coladvance(endcol);
1826 oap->end.col = curwin->w_cursor.col;
1827 oap->end.coladd = curwin->w_cursor.coladd;
1828 curwin->w_cursor = oap->start;
1829 }
1830 }
1831
1832 /* Break a tab only when it's included in the area. */
1833 if (gchar_pos(&oap->end) == '\t'
1834 && (int)oap->end.coladd < oap->inclusive)
1835 {
1836 /* save last line for undo */
1837 if (u_save((linenr_T)(oap->end.lnum - 1),
1838 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1839 return FAIL;
1840 curwin->w_cursor = oap->end;
1841 coladvance_force(getviscol2(oap->end.col, oap->end.coladd));
1842 oap->end = curwin->w_cursor;
1843 curwin->w_cursor = oap->start;
1844 }
1845 }
1846#endif
1847
1848 if (oap->line_count == 1) /* delete characters within one line */
1849 {
1850 if (u_save_cursor() == FAIL) /* save line for undo */
1851 return FAIL;
1852
1853 /* if 'cpoptions' contains '$', display '$' at end of change */
1854 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL
1855 && oap->op_type == OP_CHANGE
1856 && oap->end.lnum == curwin->w_cursor.lnum
1857#ifdef FEAT_VISUAL
1858 && !oap->is_VIsual
1859#endif
1860 )
1861 display_dollar(oap->end.col - !oap->inclusive);
1862
1863 n = oap->end.col - oap->start.col + 1 - !oap->inclusive;
1864
1865#ifdef FEAT_VIRTUALEDIT
1866 if (virtual_op)
1867 {
1868 /* fix up things for virtualedit-delete:
1869 * break the tabs which are going to get in our way
1870 */
1871 char_u *curline = ml_get_curline();
1872 int len = (int)STRLEN(curline);
1873
1874 if (oap->end.coladd != 0
1875 && (int)oap->end.col >= len - 1
1876 && !(oap->start.coladd && (int)oap->end.col >= len - 1))
1877 n++;
1878 /* Delete at least one char (e.g, when on a control char). */
1879 if (n == 0 && oap->start.coladd != oap->end.coladd)
1880 n = 1;
1881
1882 /* When deleted a char in the line, reset coladd. */
1883 if (gchar_cursor() != NUL)
1884 curwin->w_cursor.coladd = 0;
1885 }
1886#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +00001887 (void)del_bytes((long)n, !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001888#ifdef FEAT_VISUAL
1889 && !oap->is_VIsual
1890#endif
1891 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 else /* delete characters between lines */
1894 {
1895 pos_T curpos;
1896
1897 /* save deleted and changed lines for undo */
1898 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
1899 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL)
1900 return FAIL;
1901
1902 truncate_line(TRUE); /* delete from cursor to end of line */
1903
1904 curpos = curwin->w_cursor; /* remember curwin->w_cursor */
1905 ++curwin->w_cursor.lnum;
1906 del_lines((long)(oap->line_count - 2), FALSE);
1907
1908 /* delete from start of line until op_end */
1909 curwin->w_cursor.col = 0;
1910 (void)del_bytes((long)(oap->end.col + 1 - !oap->inclusive),
Bram Moolenaarca003e12006-03-17 23:19:38 +00001911 !virtual_op, oap->op_type == OP_DELETE
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001912#ifdef FEAT_VISUAL
1913 && !oap->is_VIsual
1914#endif
1915 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 curwin->w_cursor = curpos; /* restore curwin->w_cursor */
1917
1918 (void)do_join(FALSE);
1919 }
1920 }
1921
1922 msgmore(curbuf->b_ml.ml_line_count - old_lcount);
1923
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001924#ifdef FEAT_VIRTUALEDIT
1925setmarks:
1926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef FEAT_VISUAL
1928 if (oap->block_mode)
1929 {
1930 curbuf->b_op_end.lnum = oap->end.lnum;
1931 curbuf->b_op_end.col = oap->start.col;
1932 }
1933 else
1934#endif
1935 curbuf->b_op_end = oap->start;
1936 curbuf->b_op_start = oap->start;
1937
1938 return OK;
1939}
1940
1941#ifdef FEAT_MBYTE
1942/*
1943 * Adjust end of operating area for ending on a multi-byte character.
1944 * Used for deletion.
1945 */
1946 static void
1947mb_adjust_opend(oap)
1948 oparg_T *oap;
1949{
1950 char_u *p;
1951
1952 if (oap->inclusive)
1953 {
1954 p = ml_get(oap->end.lnum);
1955 oap->end.col += mb_tail_off(p, p + oap->end.col);
1956 }
1957}
1958#endif
1959
1960#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
1961/*
1962 * Replace a whole area with one character.
1963 */
1964 int
1965op_replace(oap, c)
1966 oparg_T *oap;
1967 int c;
1968{
1969 int n, numc;
1970#ifdef FEAT_MBYTE
1971 int num_chars;
1972#endif
1973 char_u *newp, *oldp;
1974 size_t oldlen;
1975 struct block_def bd;
1976
1977 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty)
1978 return OK; /* nothing to do */
1979
1980#ifdef FEAT_MBYTE
1981 if (has_mbyte)
1982 mb_adjust_opend(oap);
1983#endif
1984
1985 if (u_save((linenr_T)(oap->start.lnum - 1),
1986 (linenr_T)(oap->end.lnum + 1)) == FAIL)
1987 return FAIL;
1988
1989 /*
1990 * block mode replace
1991 */
1992 if (oap->block_mode)
1993 {
1994 bd.is_MAX = (curwin->w_curswant == MAXCOL);
1995 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum)
1996 {
1997 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE);
1998 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX))
1999 continue; /* nothing to replace */
2000
2001 /* n == number of extra chars required
2002 * If we split a TAB, it may be replaced by several characters.
2003 * Thus the number of characters may increase!
2004 */
2005#ifdef FEAT_VIRTUALEDIT
2006 /* If the range starts in virtual space, count the initial
2007 * coladd offset as part of "startspaces" */
2008 if (virtual_op && bd.is_short && *bd.textstart == NUL)
2009 {
2010 pos_T vpos;
2011
2012 getvpos(&vpos, oap->start_vcol);
2013 bd.startspaces += vpos.coladd;
2014 n = bd.startspaces;
2015 }
2016 else
2017#endif
2018 /* allow for pre spaces */
2019 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0);
2020
2021 /* allow for post spp */
2022 n += (bd.endspaces
2023#ifdef FEAT_VIRTUALEDIT
2024 && !bd.is_oneChar
2025#endif
2026 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0;
2027 /* Figure out how many characters to replace. */
2028 numc = oap->end_vcol - oap->start_vcol + 1;
2029 if (bd.is_short && (!virtual_op || bd.is_MAX))
2030 numc -= (oap->end_vcol - bd.end_vcol) + 1;
2031
2032#ifdef FEAT_MBYTE
2033 /* A double-wide character can be replaced only up to half the
2034 * times. */
2035 if ((*mb_char2cells)(c) > 1)
2036 {
2037 if ((numc & 1) && !bd.is_short)
2038 {
2039 ++bd.endspaces;
2040 ++n;
2041 }
2042 numc = numc / 2;
2043 }
2044
2045 /* Compute bytes needed, move character count to num_chars. */
2046 num_chars = numc;
2047 numc *= (*mb_char2len)(c);
2048#endif
2049 /* oldlen includes textlen, so don't double count */
2050 n += numc - bd.textlen;
2051
2052 oldp = ml_get_curline();
2053 oldlen = STRLEN(oldp);
2054 newp = alloc_check((unsigned)oldlen + 1 + n);
2055 if (newp == NULL)
2056 continue;
2057 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n));
2058 /* copy up to deleted part */
2059 mch_memmove(newp, oldp, (size_t)bd.textcol);
2060 oldp += bd.textcol + bd.textlen;
2061 /* insert pre-spaces */
2062 copy_spaces(newp + bd.textcol, (size_t)bd.startspaces);
2063 /* insert replacement chars CHECK FOR ALLOCATED SPACE */
2064#ifdef FEAT_MBYTE
2065 if (has_mbyte)
2066 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002067 n = (int)STRLEN(newp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 while (--num_chars >= 0)
2069 n += (*mb_char2bytes)(c, newp + n);
2070 }
2071 else
2072#endif
2073 copy_chars(newp + STRLEN(newp), (size_t)numc, c);
2074 if (!bd.is_short)
2075 {
2076 /* insert post-spaces */
2077 copy_spaces(newp + STRLEN(newp), (size_t)bd.endspaces);
2078 /* copy the part after the changed part */
2079 mch_memmove(newp + STRLEN(newp), oldp, STRLEN(oldp) + 1);
2080 }
2081 /* replace the line */
2082 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
2083 }
2084 }
2085 else
2086 {
2087 /*
2088 * MCHAR and MLINE motion replace.
2089 */
2090 if (oap->motion_type == MLINE)
2091 {
2092 oap->start.col = 0;
2093 curwin->w_cursor.col = 0;
2094 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2095 if (oap->end.col)
2096 --oap->end.col;
2097 }
2098 else if (!oap->inclusive)
2099 dec(&(oap->end));
2100
2101 while (ltoreq(curwin->w_cursor, oap->end))
2102 {
2103 n = gchar_cursor();
2104 if (n != NUL)
2105 {
2106#ifdef FEAT_MBYTE
2107 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1)
2108 {
2109 /* This is slow, but it handles replacing a single-byte
2110 * with a multi-byte and the other way around. */
2111 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n);
2112 n = State;
2113 State = REPLACE;
2114 ins_char(c);
2115 State = n;
2116 /* Backup to the replaced character. */
2117 dec_cursor();
2118 }
2119 else
2120#endif
2121 {
2122#ifdef FEAT_VIRTUALEDIT
2123 if (n == TAB)
2124 {
2125 int end_vcol = 0;
2126
2127 if (curwin->w_cursor.lnum == oap->end.lnum)
2128 {
2129 /* oap->end has to be recalculated when
2130 * the tab breaks */
2131 end_vcol = getviscol2(oap->end.col,
2132 oap->end.coladd);
2133 }
2134 coladvance_force(getviscol());
2135 if (curwin->w_cursor.lnum == oap->end.lnum)
2136 getvpos(&oap->end, end_vcol);
2137 }
2138#endif
2139 pchar(curwin->w_cursor, c);
2140 }
2141 }
2142#ifdef FEAT_VIRTUALEDIT
2143 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum)
2144 {
2145 int virtcols = oap->end.coladd;
2146
2147 if (curwin->w_cursor.lnum == oap->start.lnum
2148 && oap->start.col == oap->end.col && oap->start.coladd)
2149 virtcols -= oap->start.coladd;
2150
2151 /* oap->end has been trimmed so it's effectively inclusive;
2152 * as a result an extra +1 must be counted so we don't
2153 * trample the NUL byte. */
2154 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1);
2155 curwin->w_cursor.col -= (virtcols + 1);
2156 for (; virtcols >= 0; virtcols--)
2157 {
2158 pchar(curwin->w_cursor, c);
2159 if (inc(&curwin->w_cursor) == -1)
2160 break;
2161 }
2162 }
2163#endif
2164
2165 /* Advance to next character, stop at the end of the file. */
2166 if (inc_cursor() == -1)
2167 break;
2168 }
2169 }
2170
2171 curwin->w_cursor = oap->start;
2172 check_cursor();
2173 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L);
2174
2175 /* Set "'[" and "']" marks. */
2176 curbuf->b_op_start = oap->start;
2177 curbuf->b_op_end = oap->end;
2178
2179 return OK;
2180}
2181#endif
2182
2183/*
2184 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?".
2185 */
2186 void
2187op_tilde(oap)
2188 oparg_T *oap;
2189{
2190 pos_T pos;
2191#ifdef FEAT_VISUAL
2192 struct block_def bd;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002193 int todo;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 int did_change = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 if (u_save((linenr_T)(oap->start.lnum - 1),
2198 (linenr_T)(oap->end.lnum + 1)) == FAIL)
2199 return;
2200
2201 pos = oap->start;
2202#ifdef FEAT_VISUAL
2203 if (oap->block_mode) /* Visual block mode */
2204 {
2205 for (; pos.lnum <= oap->end.lnum; ++pos.lnum)
2206 {
2207 block_prep(oap, &bd, pos.lnum, FALSE);
2208 pos.col = bd.textcol;
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002209 for (todo = bd.textlen; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002211# ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002212 if (has_mbyte)
2213 todo -= (*mb_ptr2len)(ml_get_pos(&pos)) - 1;
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 did_change |= swapchar(oap->op_type, &pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 if (inc(&pos) == -1) /* at end of file */
2217 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219# ifdef FEAT_NETBEANS_INTG
2220 if (usingNetbeans && did_change)
2221 {
2222 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2223
Bram Moolenaar009b2592004-10-24 19:18:58 +00002224 netbeans_removed(curbuf, pos.lnum, bd.textcol,
2225 (long)bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 netbeans_inserted(curbuf, pos.lnum, bd.textcol,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002227 &ptr[bd.textcol], bd.textlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229# endif
2230 }
2231 if (did_change)
2232 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L);
2233 }
2234 else /* not block mode */
2235#endif
2236 {
2237 if (oap->motion_type == MLINE)
2238 {
2239 oap->start.col = 0;
2240 pos.col = 0;
2241 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
2242 if (oap->end.col)
2243 --oap->end.col;
2244 }
2245 else if (!oap->inclusive)
2246 dec(&(oap->end));
2247
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002248 while (ltoreq(pos, oap->end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
2250 did_change |= swapchar(oap->op_type, &pos);
2251 if (inc(&pos) == -1) /* at end of file */
2252 break;
2253 }
Bram Moolenaar60a795a2005-09-16 21:55:43 +00002254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (did_change)
2256 {
2257 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1,
2258 0L);
2259#ifdef FEAT_NETBEANS_INTG
2260 if (usingNetbeans && did_change)
2261 {
2262 char_u *ptr;
2263 int count;
2264
2265 pos = oap->start;
2266 while (pos.lnum < oap->end.lnum)
2267 {
2268 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002269 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002270 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002272 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 pos.col = 0;
2274 pos.lnum++;
2275 }
2276 ptr = ml_get_buf(curbuf, pos.lnum, FALSE);
2277 count = oap->end.col - pos.col + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002278 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00002280 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282#endif
2283 }
2284 }
2285
2286#ifdef FEAT_VISUAL
2287 if (!did_change && oap->is_VIsual)
2288 /* No change: need to remove the Visual selection */
2289 redraw_curbuf_later(INVERTED);
2290#endif
2291
2292 /*
2293 * Set '[ and '] marks.
2294 */
2295 curbuf->b_op_start = oap->start;
2296 curbuf->b_op_end = oap->end;
2297
2298 if (oap->line_count > p_report)
2299 {
2300 if (oap->line_count == 1)
2301 MSG(_("1 line changed"));
2302 else
2303 smsg((char_u *)_("%ld lines changed"), oap->line_count);
2304 }
2305}
2306
2307/*
2308 * If op_type == OP_UPPER: make uppercase,
2309 * if op_type == OP_LOWER: make lowercase,
2310 * if op_type == OP_ROT13: do rot13 encoding,
2311 * else swap case of character at 'pos'
2312 * returns TRUE when something actually changed.
2313 */
2314 int
2315swapchar(op_type, pos)
2316 int op_type;
2317 pos_T *pos;
2318{
2319 int c;
2320 int nc;
2321
2322 c = gchar_pos(pos);
2323
2324 /* Only do rot13 encoding for ASCII characters. */
2325 if (c >= 0x80 && op_type == OP_ROT13)
2326 return FALSE;
2327
2328#ifdef FEAT_MBYTE
Bram Moolenaar6f16eb82005-08-23 21:02:42 +00002329 if (op_type == OP_UPPER && enc_latin1like && c == 0xdf)
2330 {
2331 pos_T sp = curwin->w_cursor;
2332
2333 /* Special handling of German sharp s: change to "SS". */
2334 curwin->w_cursor = *pos;
2335 del_char(FALSE);
2336 ins_char('S');
2337 ins_char('S');
2338 curwin->w_cursor = sp;
2339 inc(pos);
2340 }
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */
2343 return FALSE;
2344#endif
2345 nc = c;
2346 if (MB_ISLOWER(c))
2347 {
2348 if (op_type == OP_ROT13)
2349 nc = ROT13(c, 'a');
2350 else if (op_type != OP_LOWER)
2351 nc = MB_TOUPPER(c);
2352 }
2353 else if (MB_ISUPPER(c))
2354 {
2355 if (op_type == OP_ROT13)
2356 nc = ROT13(c, 'A');
2357 else if (op_type != OP_UPPER)
2358 nc = MB_TOLOWER(c);
2359 }
2360 if (nc != c)
2361 {
2362#ifdef FEAT_MBYTE
2363 if (enc_utf8 && (c >= 0x80 || nc >= 0x80))
2364 {
2365 pos_T sp = curwin->w_cursor;
2366
2367 curwin->w_cursor = *pos;
2368 del_char(FALSE);
2369 ins_char(nc);
2370 curwin->w_cursor = sp;
2371 }
2372 else
2373#endif
2374 pchar(*pos, nc);
2375 return TRUE;
2376 }
2377 return FALSE;
2378}
2379
2380#if defined(FEAT_VISUALEXTRA) || defined(PROTO)
2381/*
2382 * op_insert - Insert and append operators for Visual mode.
2383 */
2384 void
2385op_insert(oap, count1)
2386 oparg_T *oap;
2387 long count1;
2388{
2389 long ins_len, pre_textlen = 0;
2390 char_u *firstline, *ins_text;
2391 struct block_def bd;
2392 int i;
2393
2394 /* edit() changes this - record it for OP_APPEND */
2395 bd.is_MAX = (curwin->w_curswant == MAXCOL);
2396
2397 /* vis block is still marked. Get rid of it now. */
2398 curwin->w_cursor.lnum = oap->start.lnum;
2399 update_screen(INVERTED);
2400
2401 if (oap->block_mode)
2402 {
2403#ifdef FEAT_VIRTUALEDIT
2404 /* When 'virtualedit' is used, need to insert the extra spaces before
2405 * doing block_prep(). When only "block" is used, virtual edit is
2406 * already disabled, but still need it when calling
2407 * coladvance_force(). */
2408 if (curwin->w_cursor.coladd > 0)
2409 {
2410 int old_ve_flags = ve_flags;
2411
2412 ve_flags = VE_ALL;
2413 if (u_save_cursor() == FAIL)
2414 return;
2415 coladvance_force(oap->op_type == OP_APPEND
2416 ? oap->end_vcol + 1 : getviscol());
2417 if (oap->op_type == OP_APPEND)
2418 --curwin->w_cursor.col;
2419 ve_flags = old_ve_flags;
2420 }
2421#endif
2422 /* Get the info about the block before entering the text */
2423 block_prep(oap, &bd, oap->start.lnum, TRUE);
2424 firstline = ml_get(oap->start.lnum) + bd.textcol;
2425 if (oap->op_type == OP_APPEND)
2426 firstline += bd.textlen;
2427 pre_textlen = (long)STRLEN(firstline);
2428 }
2429
2430 if (oap->op_type == OP_APPEND)
2431 {
2432 if (oap->block_mode
2433#ifdef FEAT_VIRTUALEDIT
2434 && curwin->w_cursor.coladd == 0
2435#endif
2436 )
2437 {
2438 /* Move the cursor to the character right of the block. */
2439 curwin->w_set_curswant = TRUE;
2440 while (*ml_get_cursor() != NUL
2441 && (curwin->w_cursor.col < bd.textcol + bd.textlen))
2442 ++curwin->w_cursor.col;
2443 if (bd.is_short && !bd.is_MAX)
2444 {
2445 /* First line was too short, make it longer and adjust the
2446 * values in "bd". */
2447 if (u_save_cursor() == FAIL)
2448 return;
2449 for (i = 0; i < bd.endspaces; ++i)
2450 ins_char(' ');
2451 bd.textlen += bd.endspaces;
2452 }
2453 }
2454 else
2455 {
2456 curwin->w_cursor = oap->end;
Bram Moolenaar6a584dc2006-06-20 18:29:34 +00002457 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 /* Works just like an 'i'nsert on the next character. */
2460 if (!lineempty(curwin->w_cursor.lnum)
2461 && oap->start_vcol != oap->end_vcol)
2462 inc_cursor();
2463 }
2464 }
2465
2466 edit(NUL, FALSE, (linenr_T)count1);
2467
2468 /* if user has moved off this line, we don't know what to do, so do
2469 * nothing */
2470 if (curwin->w_cursor.lnum != oap->start.lnum)
2471 return;
2472
2473 if (oap->block_mode)
2474 {
2475 struct block_def bd2;
2476
2477 /*
2478 * Spaces and tabs in the indent may have changed to other spaces and
2479 * tabs. Get the starting column again and correct the lenght.
2480 * Don't do this when "$" used, end-of-line will have changed.
2481 */
2482 block_prep(oap, &bd2, oap->start.lnum, TRUE);
2483 if (!bd.is_MAX || bd2.textlen < bd.textlen)
2484 {
2485 if (oap->op_type == OP_APPEND)
2486 {
2487 pre_textlen += bd2.textlen - bd.textlen;
2488 if (bd2.endspaces)
2489 --bd2.textlen;
2490 }
2491 bd.textcol = bd2.textcol;
2492 bd.textlen = bd2.textlen;
2493 }
2494
2495 /*
2496 * Subsequent calls to ml_get() flush the firstline data - take a
2497 * copy of the required string.
2498 */
2499 firstline = ml_get(oap->start.lnum) + bd.textcol;
2500 if (oap->op_type == OP_APPEND)
2501 firstline += bd.textlen;
2502 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2503 {
2504 ins_text = vim_strnsave(firstline, (int)ins_len);
2505 if (ins_text != NULL)
2506 {
2507 /* block handled here */
2508 if (u_save(oap->start.lnum,
2509 (linenr_T)(oap->end.lnum + 1)) == OK)
2510 block_insert(oap, ins_text, (oap->op_type == OP_INSERT),
2511 &bd);
2512
2513 curwin->w_cursor.col = oap->start.col;
2514 check_cursor();
2515 vim_free(ins_text);
2516 }
2517 }
2518 }
2519}
2520#endif
2521
2522/*
2523 * op_change - handle a change operation
2524 *
2525 * return TRUE if edit() returns because of a CTRL-O command
2526 */
2527 int
2528op_change(oap)
2529 oparg_T *oap;
2530{
2531 colnr_T l;
2532 int retval;
2533#ifdef FEAT_VISUALEXTRA
2534 long offset;
2535 linenr_T linenr;
2536 long ins_len, pre_textlen = 0;
2537 char_u *firstline;
2538 char_u *ins_text, *newp, *oldp;
2539 struct block_def bd;
2540#endif
2541
2542 l = oap->start.col;
2543 if (oap->motion_type == MLINE)
2544 {
2545 l = 0;
2546#ifdef FEAT_SMARTINDENT
2547 if (!p_paste && curbuf->b_p_si
2548# ifdef FEAT_CINDENT
2549 && !curbuf->b_p_cin
2550# endif
2551 )
2552 can_si = TRUE; /* It's like opening a new line, do si */
2553#endif
2554 }
2555
2556 /* First delete the text in the region. In an empty buffer only need to
2557 * save for undo */
2558 if (curbuf->b_ml.ml_flags & ML_EMPTY)
2559 {
2560 if (u_save_cursor() == FAIL)
2561 return FALSE;
2562 }
2563 else if (op_delete(oap) == FAIL)
2564 return FALSE;
2565
2566 if ((l > curwin->w_cursor.col) && !lineempty(curwin->w_cursor.lnum)
2567 && !virtual_op)
2568 inc_cursor();
2569
2570#ifdef FEAT_VISUALEXTRA
2571 /* check for still on same line (<CR> in inserted text meaningless) */
2572 /* skip blank lines too */
2573 if (oap->block_mode)
2574 {
2575# ifdef FEAT_VIRTUALEDIT
2576 /* Add spaces before getting the current line length. */
2577 if (virtual_op && (curwin->w_cursor.coladd > 0
2578 || gchar_cursor() == NUL))
2579 coladvance_force(getviscol());
2580# endif
2581 pre_textlen = (long)STRLEN(ml_get(oap->start.lnum));
2582 bd.textcol = curwin->w_cursor.col;
2583 }
2584#endif
2585
2586#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2587 if (oap->motion_type == MLINE)
2588 fix_indent();
2589#endif
2590
2591 retval = edit(NUL, FALSE, (linenr_T)1);
2592
2593#ifdef FEAT_VISUALEXTRA
2594 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002595 * In Visual block mode, handle copying the new text to all lines of the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * block.
2597 */
2598 if (oap->block_mode && oap->start.lnum != oap->end.lnum)
2599 {
2600 firstline = ml_get(oap->start.lnum);
2601 /*
2602 * Subsequent calls to ml_get() flush the firstline data - take a
2603 * copy of the required bit.
2604 */
2605 if ((ins_len = (long)STRLEN(firstline) - pre_textlen) > 0)
2606 {
2607 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL)
2608 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002609 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum;
2611 linenr++)
2612 {
2613 block_prep(oap, &bd, linenr, TRUE);
2614 if (!bd.is_short || virtual_op)
2615 {
2616# ifdef FEAT_VIRTUALEDIT
2617 pos_T vpos;
2618
2619 /* If the block starts in virtual space, count the
2620 * initial coladd offset as part of "startspaces" */
2621 if (bd.is_short)
2622 {
2623 linenr_T lnum = curwin->w_cursor.lnum;
2624
2625 curwin->w_cursor.lnum = linenr;
2626 (void)getvpos(&vpos, oap->start_vcol);
2627 curwin->w_cursor.lnum = lnum;
2628 }
2629 else
2630 vpos.coladd = 0;
2631# endif
2632 oldp = ml_get(linenr);
2633 newp = alloc_check((unsigned)(STRLEN(oldp)
2634# ifdef FEAT_VIRTUALEDIT
2635 + vpos.coladd
2636# endif
2637 + ins_len + 1));
2638 if (newp == NULL)
2639 continue;
2640 /* copy up to block start */
2641 mch_memmove(newp, oldp, (size_t)bd.textcol);
2642 offset = bd.textcol;
2643# ifdef FEAT_VIRTUALEDIT
2644 copy_spaces(newp + offset, (size_t)vpos.coladd);
2645 offset += vpos.coladd;
2646# endif
2647 mch_memmove(newp + offset, ins_text, (size_t)ins_len);
2648 offset += ins_len;
2649 oldp += bd.textcol;
2650 mch_memmove(newp + offset, oldp, STRLEN(oldp) + 1);
2651 ml_replace(linenr, newp, FALSE);
2652 }
2653 }
2654 check_cursor();
2655
2656 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L);
2657 }
2658 vim_free(ins_text);
2659 }
2660 }
2661#endif
2662
2663 return retval;
2664}
2665
2666/*
2667 * set all the yank registers to empty (called from main())
2668 */
2669 void
2670init_yank()
2671{
2672 int i;
2673
2674 for (i = 0; i < NUM_REGISTERS; ++i)
2675 y_regs[i].y_array = NULL;
2676}
2677
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00002678#if defined(EXITFREE) || defined(PROTO)
2679 void
2680clear_registers()
2681{
2682 int i;
2683
2684 for (i = 0; i < NUM_REGISTERS; ++i)
2685 {
2686 y_current = &y_regs[i];
2687 if (y_current->y_array != NULL)
2688 free_yank_all();
2689 }
2690}
2691#endif
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693/*
2694 * Free "n" lines from the current yank register.
2695 * Called for normal freeing and in case of error.
2696 */
2697 static void
2698free_yank(n)
2699 long n;
2700{
2701 if (y_current->y_array != NULL)
2702 {
2703 long i;
2704
2705 for (i = n; --i >= 0; )
2706 {
2707#ifdef AMIGA /* only for very slow machines */
2708 if ((i & 1023) == 1023) /* this may take a while */
2709 {
2710 /*
2711 * This message should never cause a hit-return message.
2712 * Overwrite this message with any next message.
2713 */
2714 ++no_wait_return;
2715 smsg((char_u *)_("freeing %ld lines"), i + 1);
2716 --no_wait_return;
2717 msg_didout = FALSE;
2718 msg_col = 0;
2719 }
2720#endif
2721 vim_free(y_current->y_array[i]);
2722 }
2723 vim_free(y_current->y_array);
2724 y_current->y_array = NULL;
2725#ifdef AMIGA
2726 if (n >= 1000)
2727 MSG("");
2728#endif
2729 }
2730}
2731
2732 static void
2733free_yank_all()
2734{
2735 free_yank(y_current->y_size);
2736}
2737
2738/*
2739 * Yank the text between "oap->start" and "oap->end" into a yank register.
2740 * If we are to append (uppercase register), we first yank into a new yank
2741 * register and then concatenate the old and the new one (so we keep the old
2742 * one in case of out-of-memory).
2743 *
2744 * return FAIL for failure, OK otherwise
2745 */
2746 int
2747op_yank(oap, deleting, mess)
2748 oparg_T *oap;
2749 int deleting;
2750 int mess;
2751{
2752 long y_idx; /* index in y_array[] */
2753 struct yankreg *curr; /* copy of y_current */
2754 struct yankreg newreg; /* new yank register when appending */
2755 char_u **new_ptr;
2756 linenr_T lnum; /* current line number */
2757 long j;
2758 int yanktype = oap->motion_type;
2759 long yanklines = oap->line_count;
2760 linenr_T yankendlnum = oap->end.lnum;
2761 char_u *p;
2762 char_u *pnew;
2763 struct block_def bd;
2764
2765 /* check for read-only register */
2766 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
2767 {
2768 beep_flush();
2769 return FAIL;
2770 }
2771 if (oap->regname == '_') /* black hole: nothing to do */
2772 return OK;
2773
2774#ifdef FEAT_CLIPBOARD
2775 if (!clip_star.available && oap->regname == '*')
2776 oap->regname = 0;
2777 else if (!clip_plus.available && oap->regname == '+')
2778 oap->regname = 0;
2779#endif
2780
2781 if (!deleting) /* op_delete() already set y_current */
2782 get_yank_register(oap->regname, TRUE);
2783
2784 curr = y_current;
2785 /* append to existing contents */
2786 if (y_append && y_current->y_array != NULL)
2787 y_current = &newreg;
2788 else
2789 free_yank_all(); /* free previously yanked lines */
2790
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00002791 /*
2792 * If the cursor was in column 1 before and after the movement, and the
2793 * operator is not inclusive, the yank is always linewise.
2794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if ( oap->motion_type == MCHAR
2796 && oap->start.col == 0
2797 && !oap->inclusive
2798#ifdef FEAT_VISUAL
2799 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaarec2dad62005-01-02 11:36:03 +00002800 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801#endif
2802 && oap->end.col == 0
2803 && yanklines > 1)
2804 {
2805 yanktype = MLINE;
2806 --yankendlnum;
2807 --yanklines;
2808 }
2809
2810 y_current->y_size = yanklines;
2811 y_current->y_type = yanktype; /* set the yank register type */
2812#ifdef FEAT_VISUAL
2813 y_current->y_width = 0;
2814#endif
2815 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) *
2816 yanklines), TRUE);
2817
2818 if (y_current->y_array == NULL)
2819 {
2820 y_current = curr;
2821 return FAIL;
2822 }
2823
2824 y_idx = 0;
2825 lnum = oap->start.lnum;
2826
2827#ifdef FEAT_VISUAL
2828 if (oap->block_mode)
2829 {
2830 /* Visual block mode */
2831 y_current->y_type = MBLOCK; /* set the yank register type */
2832 y_current->y_width = oap->end_vcol - oap->start_vcol;
2833
2834 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
2835 y_current->y_width--;
2836 }
2837#endif
2838
2839 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
2840 {
2841 switch (y_current->y_type)
2842 {
2843#ifdef FEAT_VISUAL
2844 case MBLOCK:
2845 block_prep(oap, &bd, lnum, FALSE);
2846 if (yank_copy_line(&bd, y_idx) == FAIL)
2847 goto fail;
2848 break;
2849#endif
2850
2851 case MLINE:
2852 if ((y_current->y_array[y_idx] =
2853 vim_strsave(ml_get(lnum))) == NULL)
2854 goto fail;
2855 break;
2856
2857 case MCHAR:
2858 {
2859 colnr_T startcol = 0, endcol = MAXCOL;
2860#ifdef FEAT_VIRTUALEDIT
2861 int is_oneChar = FALSE;
2862 colnr_T cs, ce;
2863#endif
2864 p = ml_get(lnum);
2865 bd.startspaces = 0;
2866 bd.endspaces = 0;
2867
2868 if (lnum == oap->start.lnum)
2869 {
2870 startcol = oap->start.col;
2871#ifdef FEAT_VIRTUALEDIT
2872 if (virtual_op)
2873 {
2874 getvcol(curwin, &oap->start, &cs, NULL, &ce);
2875 if (ce != cs && oap->start.coladd > 0)
2876 {
2877 /* Part of a tab selected -- but don't
2878 * double-count it. */
2879 bd.startspaces = (ce - cs + 1)
2880 - oap->start.coladd;
2881 startcol++;
2882 }
2883 }
2884#endif
2885 }
2886
2887 if (lnum == oap->end.lnum)
2888 {
2889 endcol = oap->end.col;
2890#ifdef FEAT_VIRTUALEDIT
2891 if (virtual_op)
2892 {
2893 getvcol(curwin, &oap->end, &cs, NULL, &ce);
2894 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
2895# ifdef FEAT_MBYTE
2896 /* Don't add space for double-wide
2897 * char; endcol will be on last byte
2898 * of multi-byte char. */
2899 && (*mb_head_off)(p, p + endcol) == 0
2900# endif
2901 ))
2902 {
2903 if (oap->start.lnum == oap->end.lnum
2904 && oap->start.col == oap->end.col)
2905 {
2906 /* Special case: inside a single char */
2907 is_oneChar = TRUE;
2908 bd.startspaces = oap->end.coladd
2909 - oap->start.coladd + oap->inclusive;
2910 endcol = startcol;
2911 }
2912 else
2913 {
2914 bd.endspaces = oap->end.coladd
2915 + oap->inclusive;
2916 endcol -= oap->inclusive;
2917 }
2918 }
2919 }
2920#endif
2921 }
2922 if (startcol > endcol
2923#ifdef FEAT_VIRTUALEDIT
2924 || is_oneChar
2925#endif
2926 )
2927 bd.textlen = 0;
2928 else
2929 {
2930 if (endcol == MAXCOL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002931 endcol = (colnr_T)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 bd.textlen = endcol - startcol + oap->inclusive;
2933 }
2934 bd.textstart = p + startcol;
2935 if (yank_copy_line(&bd, y_idx) == FAIL)
2936 goto fail;
2937 break;
2938 }
2939 /* NOTREACHED */
2940 }
2941 }
2942
2943 if (curr != y_current) /* append the new block to the old block */
2944 {
2945 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) *
2946 (curr->y_size + y_current->y_size)), TRUE);
2947 if (new_ptr == NULL)
2948 goto fail;
2949 for (j = 0; j < curr->y_size; ++j)
2950 new_ptr[j] = curr->y_array[j];
2951 vim_free(curr->y_array);
2952 curr->y_array = new_ptr;
2953
2954 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */
2955 curr->y_type = MLINE;
2956
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002957 /* Concatenate the last line of the old block with the first line of
2958 * the new block, unless being Vi compatible. */
2959 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1])
2962 + STRLEN(y_current->y_array[0]) + 1), TRUE);
2963 if (pnew == NULL)
2964 {
2965 y_idx = y_current->y_size - 1;
2966 goto fail;
2967 }
2968 STRCPY(pnew, curr->y_array[--j]);
2969 STRCAT(pnew, y_current->y_array[0]);
2970 vim_free(curr->y_array[j]);
2971 vim_free(y_current->y_array[0]);
2972 curr->y_array[j++] = pnew;
2973 y_idx = 1;
2974 }
2975 else
2976 y_idx = 0;
2977 while (y_idx < y_current->y_size)
2978 curr->y_array[j++] = y_current->y_array[y_idx++];
2979 curr->y_size = j;
2980 vim_free(y_current->y_array);
2981 y_current = curr;
2982 }
2983 if (mess) /* Display message about yank? */
2984 {
2985 if (yanktype == MCHAR
2986#ifdef FEAT_VISUAL
2987 && !oap->block_mode
2988#endif
2989 && yanklines == 1)
2990 yanklines = 0;
2991 /* Some versions of Vi use ">=" here, some don't... */
2992 if (yanklines > p_report)
2993 {
2994 /* redisplay now, so message is not deleted */
2995 update_topline_redraw();
2996 if (yanklines == 1)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00002997 {
2998#ifdef FEAT_VISUAL
2999 if (oap->block_mode)
3000 MSG(_("block of 1 line yanked"));
3001 else
3002#endif
3003 MSG(_("1 line yanked"));
3004 }
3005#ifdef FEAT_VISUAL
3006 else if (oap->block_mode)
3007 smsg((char_u *)_("block of %ld lines yanked"), yanklines);
3008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 else
3010 smsg((char_u *)_("%ld lines yanked"), yanklines);
3011 }
3012 }
3013
3014 /*
3015 * Set "'[" and "']" marks.
3016 */
3017 curbuf->b_op_start = oap->start;
3018 curbuf->b_op_end = oap->end;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003019 if (yanktype == MLINE
3020#ifdef FEAT_VISUAL
3021 && !oap->block_mode
3022#endif
3023 )
3024 {
3025 curbuf->b_op_start.col = 0;
3026 curbuf->b_op_end.col = MAXCOL;
3027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028
3029#ifdef FEAT_CLIPBOARD
3030 /*
3031 * If we were yanking to the '*' register, send result to clipboard.
3032 * If no register was specified, and "unnamed" in 'clipboard', make a copy
3033 * to the '*' register.
3034 */
3035 if (clip_star.available
3036 && (curr == &(y_regs[STAR_REGISTER])
3037 || (!deleting && oap->regname == 0 && clip_unnamed)))
3038 {
3039 if (curr != &(y_regs[STAR_REGISTER]))
3040 /* Copy the text from register 0 to the clipboard register. */
3041 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3042
3043 clip_own_selection(&clip_star);
3044 clip_gen_set_selection(&clip_star);
3045 }
3046
3047# ifdef FEAT_X11
3048 /*
3049 * If we were yanking to the '+' register, send result to selection.
3050 * Also copy to the '*' register, in case auto-select is off.
3051 */
3052 else if (clip_plus.available && curr == &(y_regs[PLUS_REGISTER]))
3053 {
3054 /* No need to copy to * register upon 'unnamed' now - see below */
3055 clip_own_selection(&clip_plus);
3056 clip_gen_set_selection(&clip_plus);
3057 if (!clip_isautosel())
3058 {
3059 copy_yank_reg(&(y_regs[STAR_REGISTER]));
3060 clip_own_selection(&clip_star);
3061 clip_gen_set_selection(&clip_star);
3062 }
3063 }
3064# endif
3065#endif
3066
3067 return OK;
3068
3069fail: /* free the allocated lines */
3070 free_yank(y_idx + 1);
3071 y_current = curr;
3072 return FAIL;
3073}
3074
3075 static int
3076yank_copy_line(bd, y_idx)
3077 struct block_def *bd;
3078 long y_idx;
3079{
3080 char_u *pnew;
3081
3082 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
3083 == NULL)
3084 return FAIL;
3085 y_current->y_array[y_idx] = pnew;
3086 copy_spaces(pnew, (size_t)bd->startspaces);
3087 pnew += bd->startspaces;
3088 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
3089 pnew += bd->textlen;
3090 copy_spaces(pnew, (size_t)bd->endspaces);
3091 pnew += bd->endspaces;
3092 *pnew = NUL;
3093 return OK;
3094}
3095
3096#ifdef FEAT_CLIPBOARD
3097/*
3098 * Make a copy of the y_current register to register "reg".
3099 */
3100 static void
3101copy_yank_reg(reg)
3102 struct yankreg *reg;
3103{
3104 struct yankreg *curr = y_current;
3105 long j;
3106
3107 y_current = reg;
3108 free_yank_all();
3109 *y_current = *curr;
3110 y_current->y_array = (char_u **)lalloc_clear(
3111 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE);
3112 if (y_current->y_array == NULL)
3113 y_current->y_size = 0;
3114 else
3115 for (j = 0; j < y_current->y_size; ++j)
3116 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
3117 {
3118 free_yank(j);
3119 y_current->y_size = 0;
3120 break;
3121 }
3122 y_current = curr;
3123}
3124#endif
3125
3126/*
Bram Moolenaar677ee682005-01-27 14:41:15 +00003127 * Put contents of register "regname" into the text.
3128 * Caller must check "regname" to be valid!
3129 * "flags": PUT_FIXINDENT make indent look nice
3130 * PUT_CURSEND leave cursor after end of new text
3131 * PUT_LINE force linewise put (":put")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 */
3133 void
3134do_put(regname, dir, count, flags)
3135 int regname;
3136 int dir; /* BACKWARD for 'P', FORWARD for 'p' */
3137 long count;
3138 int flags;
3139{
3140 char_u *ptr;
3141 char_u *newp, *oldp;
3142 int yanklen;
3143 int totlen = 0; /* init for gcc */
3144 linenr_T lnum;
3145 colnr_T col;
3146 long i; /* index in y_array[] */
3147 int y_type;
3148 long y_size;
3149#ifdef FEAT_VISUAL
3150 int oldlen;
3151 long y_width = 0;
3152 colnr_T vcol;
3153 int delcount;
3154 int incr = 0;
3155 long j;
3156 struct block_def bd;
3157#endif
3158 char_u **y_array = NULL;
3159 long nr_lines = 0;
3160 pos_T new_cursor;
3161 int indent;
3162 int orig_indent = 0; /* init for gcc */
3163 int indent_diff = 0; /* init for gcc */
3164 int first_indent = TRUE;
3165 int lendiff = 0;
3166 pos_T old_pos;
3167 char_u *insert_string = NULL;
3168 int allocated = FALSE;
3169 long cnt;
3170
3171#ifdef FEAT_CLIPBOARD
3172 /* Adjust register name for "unnamed" in 'clipboard'. */
3173 adjust_clip_reg(&regname);
3174 (void)may_get_selection(regname);
3175#endif
3176
3177 if (flags & PUT_FIXINDENT)
3178 orig_indent = get_indent();
3179
3180 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3181 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3182
3183 /*
3184 * Using inserted text works differently, because the register includes
3185 * special characters (newlines, etc.).
3186 */
3187 if (regname == '.')
3188 {
3189 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
3190 (count == -1 ? 'O' : 'i')), count, FALSE);
3191 /* Putting the text is done later, so can't really move the cursor to
3192 * the next character. Use "l" to simulate it. */
3193 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
3194 stuffcharReadbuff('l');
3195 return;
3196 }
3197
3198 /*
3199 * For special registers '%' (file name), '#' (alternate file name) and
3200 * ':' (last command line), etc. we have to create a fake yank register.
3201 */
3202 if (get_spec_reg(regname, &insert_string, &allocated, TRUE))
3203 {
3204 if (insert_string == NULL)
3205 return;
3206 }
3207
3208 if (insert_string != NULL)
3209 {
3210 y_type = MCHAR;
3211#ifdef FEAT_EVAL
3212 if (regname == '=')
3213 {
3214 /* For the = register we need to split the string at NL
3215 * characters. */
3216 /* Loop twice: count the number of lines and save them. */
3217 for (;;)
3218 {
3219 y_size = 0;
3220 ptr = insert_string;
3221 while (ptr != NULL)
3222 {
3223 if (y_array != NULL)
3224 y_array[y_size] = ptr;
3225 ++y_size;
3226 ptr = vim_strchr(ptr, '\n');
3227 if (ptr != NULL)
3228 {
3229 if (y_array != NULL)
3230 *ptr = NUL;
3231 ++ptr;
3232 /* A trailing '\n' makes the string linewise */
3233 if (*ptr == NUL)
3234 {
3235 y_type = MLINE;
3236 break;
3237 }
3238 }
3239 }
3240 if (y_array != NULL)
3241 break;
3242 y_array = (char_u **)alloc((unsigned)
3243 (y_size * sizeof(char_u *)));
3244 if (y_array == NULL)
3245 goto end;
3246 }
3247 }
3248 else
3249#endif
3250 {
3251 y_size = 1; /* use fake one-line yank register */
3252 y_array = &insert_string;
3253 }
3254 }
3255 else
3256 {
3257 get_yank_register(regname, FALSE);
3258
3259 y_type = y_current->y_type;
3260#ifdef FEAT_VISUAL
3261 y_width = y_current->y_width;
3262#endif
3263 y_size = y_current->y_size;
3264 y_array = y_current->y_array;
3265 }
3266
3267#ifdef FEAT_VISUAL
3268 if (y_type == MLINE)
3269 {
3270 if (flags & PUT_LINE_SPLIT)
3271 {
3272 /* "p" or "P" in Visual mode: split the lines to put the text in
3273 * between. */
3274 if (u_save_cursor() == FAIL)
3275 goto end;
3276 ptr = vim_strsave(ml_get_cursor());
3277 if (ptr == NULL)
3278 goto end;
3279 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
3280 vim_free(ptr);
3281
3282 ptr = vim_strnsave(ml_get_curline(), curwin->w_cursor.col);
3283 if (ptr == NULL)
3284 goto end;
3285 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
3286 ++nr_lines;
3287 dir = FORWARD;
3288 }
3289 if (flags & PUT_LINE_FORWARD)
3290 {
3291 /* Must be "p" for a Visual block, put lines below the block. */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003292 curwin->w_cursor = curbuf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 dir = FORWARD;
3294 }
3295 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */
3296 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */
3297 }
3298#endif
3299
3300 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */
3301 y_type = MLINE;
3302
3303 if (y_size == 0 || y_array == NULL)
3304 {
3305 EMSG2(_("E353: Nothing in register %s"),
3306 regname == 0 ? (char_u *)"\"" : transchar(regname));
3307 goto end;
3308 }
3309
3310#ifdef FEAT_VISUAL
3311 if (y_type == MBLOCK)
3312 {
3313 lnum = curwin->w_cursor.lnum + y_size + 1;
3314 if (lnum > curbuf->b_ml.ml_line_count)
3315 lnum = curbuf->b_ml.ml_line_count + 1;
3316 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
3317 goto end;
3318 }
3319 else
3320#endif
3321 if (y_type == MLINE)
3322 {
3323 lnum = curwin->w_cursor.lnum;
3324#ifdef FEAT_FOLDING
3325 /* Correct line number for closed fold. Don't move the cursor yet,
3326 * u_save() uses it. */
3327 if (dir == BACKWARD)
3328 (void)hasFolding(lnum, &lnum, NULL);
3329 else
3330 (void)hasFolding(lnum, NULL, &lnum);
3331#endif
3332 if (dir == FORWARD)
3333 ++lnum;
3334 if (u_save(lnum - 1, lnum) == FAIL)
3335 goto end;
3336#ifdef FEAT_FOLDING
3337 if (dir == FORWARD)
3338 curwin->w_cursor.lnum = lnum - 1;
3339 else
3340 curwin->w_cursor.lnum = lnum;
3341 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */
3342#endif
3343 }
3344 else if (u_save_cursor() == FAIL)
3345 goto end;
3346
3347 yanklen = (int)STRLEN(y_array[0]);
3348
3349#ifdef FEAT_VIRTUALEDIT
3350 if (ve_flags == VE_ALL && y_type == MCHAR)
3351 {
3352 if (gchar_cursor() == TAB)
3353 {
3354 /* Don't need to insert spaces when "p" on the last position of a
3355 * tab or "P" on the first position. */
3356 if (dir == FORWARD
3357 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1
3358 : curwin->w_cursor.coladd > 0)
3359 coladvance_force(getviscol());
3360 else
3361 curwin->w_cursor.coladd = 0;
3362 }
3363 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
3364 coladvance_force(getviscol() + (dir == FORWARD));
3365 }
3366#endif
3367
3368 lnum = curwin->w_cursor.lnum;
3369 col = curwin->w_cursor.col;
3370
3371#ifdef FEAT_VISUAL
3372 /*
3373 * Block mode
3374 */
3375 if (y_type == MBLOCK)
3376 {
3377 char c = gchar_cursor();
3378 colnr_T endcol2 = 0;
3379
3380 if (dir == FORWARD && c != NUL)
3381 {
3382#ifdef FEAT_VIRTUALEDIT
3383 if (ve_flags == VE_ALL)
3384 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3385 else
3386#endif
3387 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
3388
3389#ifdef FEAT_MBYTE
3390 if (has_mbyte)
3391 /* move to start of next multi-byte character */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003392 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 else
3394#endif
3395#ifdef FEAT_VIRTUALEDIT
3396 if (c != TAB || ve_flags != VE_ALL)
3397#endif
3398 ++curwin->w_cursor.col;
3399 ++col;
3400 }
3401 else
3402 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
3403
3404#ifdef FEAT_VIRTUALEDIT
3405 col += curwin->w_cursor.coladd;
3406 if (ve_flags == VE_ALL && curwin->w_cursor.coladd > 0)
3407 {
3408 if (dir == FORWARD && c == NUL)
3409 ++col;
3410 if (dir != FORWARD && c != NUL)
3411 ++curwin->w_cursor.col;
3412 if (c == TAB)
3413 {
3414 if (dir == BACKWARD && curwin->w_cursor.col)
3415 curwin->w_cursor.col--;
3416 if (dir == FORWARD && col - 1 == endcol2)
3417 curwin->w_cursor.col++;
3418 }
3419 }
3420 curwin->w_cursor.coladd = 0;
3421#endif
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003422 bd.textcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 for (i = 0; i < y_size; ++i)
3424 {
3425 int spaces;
3426 char shortline;
3427
3428 bd.startspaces = 0;
3429 bd.endspaces = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 vcol = 0;
3431 delcount = 0;
3432
3433 /* add a new line */
3434 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3435 {
3436 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
3437 (colnr_T)1, FALSE) == FAIL)
3438 break;
3439 ++nr_lines;
3440 }
3441 /* get the old line and advance to the position to insert at */
3442 oldp = ml_get_curline();
3443 oldlen = (int)STRLEN(oldp);
3444 for (ptr = oldp; vcol < col && *ptr; )
3445 {
3446 /* Count a tab for what it's worth (if list mode not on) */
3447 incr = lbr_chartabsize_adv(&ptr, (colnr_T)vcol);
3448 vcol += incr;
3449 }
3450 bd.textcol = (colnr_T)(ptr - oldp);
3451
3452 shortline = (vcol < col) || (vcol == col && !*ptr) ;
3453
3454 if (vcol < col) /* line too short, padd with spaces */
3455 bd.startspaces = col - vcol;
3456 else if (vcol > col)
3457 {
3458 bd.endspaces = vcol - col;
3459 bd.startspaces = incr - bd.endspaces;
3460 --bd.textcol;
3461 delcount = 1;
3462#ifdef FEAT_MBYTE
3463 if (has_mbyte)
3464 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
3465#endif
3466 if (oldp[bd.textcol] != TAB)
3467 {
3468 /* Only a Tab can be split into spaces. Other
3469 * characters will have to be moved to after the
3470 * block, causing misalignment. */
3471 delcount = 0;
3472 bd.endspaces = 0;
3473 }
3474 }
3475
3476 yanklen = (int)STRLEN(y_array[i]);
3477
3478 /* calculate number of spaces required to fill right side of block*/
3479 spaces = y_width + 1;
3480 for (j = 0; j < yanklen; j++)
3481 spaces -= lbr_chartabsize(&y_array[i][j], 0);
3482 if (spaces < 0)
3483 spaces = 0;
3484
3485 /* insert the new text */
3486 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
3487 newp = alloc_check((unsigned)totlen + oldlen + 1);
3488 if (newp == NULL)
3489 break;
3490 /* copy part up to cursor to new line */
3491 ptr = newp;
3492 mch_memmove(ptr, oldp, (size_t)bd.textcol);
3493 ptr += bd.textcol;
3494 /* may insert some spaces before the new text */
3495 copy_spaces(ptr, (size_t)bd.startspaces);
3496 ptr += bd.startspaces;
3497 /* insert the new text */
3498 for (j = 0; j < count; ++j)
3499 {
3500 mch_memmove(ptr, y_array[i], (size_t)yanklen);
3501 ptr += yanklen;
3502
3503 /* insert block's trailing spaces only if there's text behind */
3504 if ((j < count - 1 || !shortline) && spaces)
3505 {
3506 copy_spaces(ptr, (size_t)spaces);
3507 ptr += spaces;
3508 }
3509 }
3510 /* may insert some spaces after the new text */
3511 copy_spaces(ptr, (size_t)bd.endspaces);
3512 ptr += bd.endspaces;
3513 /* move the text after the cursor to the end of the line. */
3514 mch_memmove(ptr, oldp + bd.textcol + delcount,
3515 (size_t)(oldlen - bd.textcol - delcount + 1));
3516 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
3517
3518 ++curwin->w_cursor.lnum;
3519 if (i == 0)
3520 curwin->w_cursor.col += bd.startspaces;
3521 }
3522
3523 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
3524
3525 /* Set '[ mark. */
3526 curbuf->b_op_start = curwin->w_cursor;
3527 curbuf->b_op_start.lnum = lnum;
3528
3529 /* adjust '] mark */
3530 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
3531 curbuf->b_op_end.col = bd.textcol + totlen - 1;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003532# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 curbuf->b_op_end.coladd = 0;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003534# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (flags & PUT_CURSEND)
3536 {
Bram Moolenaar12dec752006-07-23 20:37:09 +00003537 colnr_T len;
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 curwin->w_cursor = curbuf->b_op_end;
3540 curwin->w_cursor.col++;
Bram Moolenaar12dec752006-07-23 20:37:09 +00003541
3542 /* in Insert mode we might be after the NUL, correct for that */
3543 len = (colnr_T)STRLEN(ml_get_curline());
3544 if (curwin->w_cursor.col > len)
3545 curwin->w_cursor.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547 else
3548 curwin->w_cursor.lnum = lnum;
3549 }
3550 else
3551#endif
3552 {
3553 /*
3554 * Character or Line mode
3555 */
3556 if (y_type == MCHAR)
3557 {
3558 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next
3559 * char */
3560 if (dir == FORWARD && gchar_cursor() != NUL)
3561 {
3562#ifdef FEAT_MBYTE
3563 if (has_mbyte)
3564 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003565 int bytelen = (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
3567 /* put it on the next of the multi-byte character. */
3568 col += bytelen;
3569 if (yanklen)
3570 {
3571 curwin->w_cursor.col += bytelen;
3572 curbuf->b_op_end.col += bytelen;
3573 }
3574 }
3575 else
3576#endif
3577 {
3578 ++col;
3579 if (yanklen)
3580 {
3581 ++curwin->w_cursor.col;
3582 ++curbuf->b_op_end.col;
3583 }
3584 }
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 curbuf->b_op_start = curwin->w_cursor;
3587 }
3588 /*
3589 * Line mode: BACKWARD is the same as FORWARD on the previous line
3590 */
3591 else if (dir == BACKWARD)
3592 --lnum;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003593 new_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
3595 /*
3596 * simple case: insert into current line
3597 */
3598 if (y_type == MCHAR && y_size == 1)
3599 {
3600 totlen = count * yanklen;
3601 if (totlen)
3602 {
3603 oldp = ml_get(lnum);
3604 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1));
3605 if (newp == NULL)
3606 goto end; /* alloc() will give error message */
3607 mch_memmove(newp, oldp, (size_t)col);
3608 ptr = newp + col;
3609 for (i = 0; i < count; ++i)
3610 {
3611 mch_memmove(ptr, y_array[0], (size_t)yanklen);
3612 ptr += yanklen;
3613 }
3614 mch_memmove(ptr, oldp + col, STRLEN(oldp + col) + 1);
3615 ml_replace(lnum, newp, FALSE);
3616 /* Put cursor on last putted char. */
3617 curwin->w_cursor.col += (colnr_T)(totlen - 1);
3618 }
3619 curbuf->b_op_end = curwin->w_cursor;
3620 /* For "CTRL-O p" in Insert mode, put cursor after last char */
3621 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
3622 ++curwin->w_cursor.col;
3623 changed_bytes(lnum, col);
3624 }
3625 else
3626 {
3627 /*
3628 * Insert at least one line. When y_type is MCHAR, break the first
3629 * line in two.
3630 */
3631 for (cnt = 1; cnt <= count; ++cnt)
3632 {
3633 i = 0;
3634 if (y_type == MCHAR)
3635 {
3636 /*
3637 * Split the current line in two at the insert position.
3638 * First insert y_array[size - 1] in front of second line.
3639 * Then append y_array[0] to first line.
3640 */
3641 lnum = new_cursor.lnum;
3642 ptr = ml_get(lnum) + col;
3643 totlen = (int)STRLEN(y_array[y_size - 1]);
3644 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1));
3645 if (newp == NULL)
3646 goto error;
3647 STRCPY(newp, y_array[y_size - 1]);
3648 STRCAT(newp, ptr);
3649 /* insert second line */
3650 ml_append(lnum, newp, (colnr_T)0, FALSE);
3651 vim_free(newp);
3652
3653 oldp = ml_get(lnum);
3654 newp = alloc_check((unsigned)(col + yanklen + 1));
3655 if (newp == NULL)
3656 goto error;
3657 /* copy first part of line */
3658 mch_memmove(newp, oldp, (size_t)col);
3659 /* append to first line */
3660 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
3661 ml_replace(lnum, newp, FALSE);
3662
3663 curwin->w_cursor.lnum = lnum;
3664 i = 1;
3665 }
3666
3667 for (; i < y_size; ++i)
3668 {
3669 if ((y_type != MCHAR || i < y_size - 1)
3670 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
3671 == FAIL)
3672 goto error;
3673 lnum++;
3674 ++nr_lines;
3675 if (flags & PUT_FIXINDENT)
3676 {
3677 old_pos = curwin->w_cursor;
3678 curwin->w_cursor.lnum = lnum;
3679 ptr = ml_get(lnum);
3680 if (cnt == count && i == y_size - 1)
3681 lendiff = (int)STRLEN(ptr);
3682#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
3683 if (*ptr == '#' && preprocs_left())
3684 indent = 0; /* Leave # lines at start */
3685 else
3686#endif
3687 if (*ptr == NUL)
3688 indent = 0; /* Ignore empty lines */
3689 else if (first_indent)
3690 {
3691 indent_diff = orig_indent - get_indent();
3692 indent = orig_indent;
3693 first_indent = FALSE;
3694 }
3695 else if ((indent = get_indent() + indent_diff) < 0)
3696 indent = 0;
3697 (void)set_indent(indent, 0);
3698 curwin->w_cursor = old_pos;
3699 /* remember how many chars were removed */
3700 if (cnt == count && i == y_size - 1)
3701 lendiff -= (int)STRLEN(ml_get(lnum));
3702 }
3703 }
3704 }
3705
3706error:
3707 /* Adjust marks. */
3708 if (y_type == MLINE)
3709 {
3710 curbuf->b_op_start.col = 0;
3711 if (dir == FORWARD)
3712 curbuf->b_op_start.lnum++;
3713 }
3714 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
3715 (linenr_T)MAXLNUM, nr_lines, 0L);
3716
3717 /* note changed text for displaying and folding */
3718 if (y_type == MCHAR)
3719 changed_lines(curwin->w_cursor.lnum, col,
3720 curwin->w_cursor.lnum + 1, nr_lines);
3721 else
3722 changed_lines(curbuf->b_op_start.lnum, 0,
3723 curbuf->b_op_start.lnum, nr_lines);
3724
3725 /* put '] mark at last inserted character */
3726 curbuf->b_op_end.lnum = lnum;
3727 /* correct length for change in indent */
3728 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
3729 if (col > 1)
3730 curbuf->b_op_end.col = col - 1;
3731 else
3732 curbuf->b_op_end.col = 0;
3733
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003734 if (flags & PUT_CURSLINE)
3735 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00003736 /* ":put": put cursor on last inserted line */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003737 curwin->w_cursor.lnum = lnum;
3738 beginline(BL_WHITE | BL_FIX);
3739 }
3740 else if (flags & PUT_CURSEND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 /* put cursor after inserted text */
3743 if (y_type == MLINE)
3744 {
3745 if (lnum >= curbuf->b_ml.ml_line_count)
3746 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3747 else
3748 curwin->w_cursor.lnum = lnum + 1;
3749 curwin->w_cursor.col = 0;
3750 }
3751 else
3752 {
3753 curwin->w_cursor.lnum = lnum;
3754 curwin->w_cursor.col = col;
3755 }
3756 }
3757 else if (y_type == MLINE)
3758 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003759 /* put cursor on first non-blank in first inserted line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 curwin->w_cursor.col = 0;
3761 if (dir == FORWARD)
3762 ++curwin->w_cursor.lnum;
3763 beginline(BL_WHITE | BL_FIX);
3764 }
3765 else /* put cursor on first inserted character */
3766 curwin->w_cursor = new_cursor;
3767 }
3768 }
3769
3770 msgmore(nr_lines);
3771 curwin->w_set_curswant = TRUE;
3772
3773end:
3774 if (allocated)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 vim_free(insert_string);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003776 if (regname == '=')
3777 vim_free(y_array);
3778
Bram Moolenaar677ee682005-01-27 14:41:15 +00003779 /* If the cursor is past the end of the line put it at the end. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003780 adjust_cursor_eol();
3781}
3782
3783/*
3784 * When the cursor is on the NUL past the end of the line and it should not be
3785 * there move it left.
3786 */
3787 void
3788adjust_cursor_eol()
3789{
3790 if (curwin->w_cursor.col > 0
3791 && gchar_cursor() == NUL
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00003792#ifdef FEAT_VIRTUALEDIT
3793 && (ve_flags & VE_ONEMORE) == 0
3794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 && !(restart_edit || (State & INSERT)))
3796 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00003797 /* Put the cursor on the last character in the line. */
Bram Moolenaara5fac542005-10-12 20:58:49 +00003798 dec_cursor();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003799
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#ifdef FEAT_VIRTUALEDIT
3801 if (ve_flags == VE_ALL)
Bram Moolenaara5792f52005-11-23 21:25:05 +00003802 {
3803 colnr_T scol, ecol;
3804
3805 /* Coladd is set to the width of the last character. */
3806 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
3807 curwin->w_cursor.coladd = ecol - scol + 1;
3808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809#endif
3810 }
3811}
3812
3813#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO)
3814/*
3815 * Return TRUE if lines starting with '#' should be left aligned.
3816 */
3817 int
3818preprocs_left()
3819{
3820 return
3821# ifdef FEAT_SMARTINDENT
3822# ifdef FEAT_CINDENT
3823 (curbuf->b_p_si && !curbuf->b_p_cin) ||
3824# else
3825 curbuf->b_p_si
3826# endif
3827# endif
3828# ifdef FEAT_CINDENT
3829 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE))
3830# endif
3831 ;
3832}
3833#endif
3834
3835/* Return the character name of the register with the given number */
3836 int
3837get_register_name(num)
3838 int num;
3839{
3840 if (num == -1)
3841 return '"';
3842 else if (num < 10)
3843 return num + '0';
3844 else if (num == DELETION_REGISTER)
3845 return '-';
3846#ifdef FEAT_CLIPBOARD
3847 else if (num == STAR_REGISTER)
3848 return '*';
3849 else if (num == PLUS_REGISTER)
3850 return '+';
3851#endif
3852 else
3853 {
3854#ifdef EBCDIC
3855 int i;
3856
3857 /* EBCDIC is really braindead ... */
3858 i = 'a' + (num - 10);
3859 if (i > 'i')
3860 i += 7;
3861 if (i > 'r')
3862 i += 8;
3863 return i;
3864#else
3865 return num + 'a' - 10;
3866#endif
3867 }
3868}
3869
3870/*
3871 * ":dis" and ":registers": Display the contents of the yank registers.
3872 */
3873 void
3874ex_display(eap)
3875 exarg_T *eap;
3876{
3877 int i, n;
3878 long j;
3879 char_u *p;
3880 struct yankreg *yb;
3881 int name;
3882 int attr;
3883 char_u *arg = eap->arg;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003884#ifdef FEAT_MBYTE
3885 int clen;
3886#else
3887# define clen 1
3888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 if (arg != NULL && *arg == NUL)
3891 arg = NULL;
3892 attr = hl_attr(HLF_8);
3893
3894 /* Highlight title */
3895 MSG_PUTS_TITLE(_("\n--- Registers ---"));
3896 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
3897 {
3898 name = get_register_name(i);
3899 if (arg != NULL && vim_strchr(arg, name) == NULL)
3900 continue; /* did not ask for this register */
3901
3902#ifdef FEAT_CLIPBOARD
3903 /* Adjust register name for "unnamed" in 'clipboard'.
3904 * When it's a clipboard register, fill it with the current contents
3905 * of the clipboard. */
3906 adjust_clip_reg(&name);
3907 (void)may_get_selection(name);
3908#endif
3909
3910 if (i == -1)
3911 {
3912 if (y_previous != NULL)
3913 yb = y_previous;
3914 else
3915 yb = &(y_regs[0]);
3916 }
3917 else
3918 yb = &(y_regs[i]);
3919 if (yb->y_array != NULL)
3920 {
3921 msg_putchar('\n');
3922 msg_putchar('"');
3923 msg_putchar(name);
3924 MSG_PUTS(" ");
3925
3926 n = (int)Columns - 6;
3927 for (j = 0; j < yb->y_size && n > 1; ++j)
3928 {
3929 if (j)
3930 {
3931 MSG_PUTS_ATTR("^J", attr);
3932 n -= 2;
3933 }
3934 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p)
3935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003937 clen = (*mb_ptr2len)(p);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003938#endif
3939 msg_outtrans_len(p, clen);
3940#ifdef FEAT_MBYTE
3941 p += clen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942#endif
3943 }
3944 }
3945 if (n > 1 && yb->y_type == MLINE)
3946 MSG_PUTS_ATTR("^J", attr);
3947 out_flush(); /* show one line at a time */
3948 }
3949 ui_breakcheck();
3950 }
3951
3952 /*
3953 * display last inserted text
3954 */
3955 if ((p = get_last_insert()) != NULL
3956 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int)
3957 {
3958 MSG_PUTS("\n\". ");
3959 dis_msg(p, TRUE);
3960 }
3961
3962 /*
3963 * display last command line
3964 */
3965 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
3966 && !got_int)
3967 {
3968 MSG_PUTS("\n\": ");
3969 dis_msg(last_cmdline, FALSE);
3970 }
3971
3972 /*
3973 * display current file name
3974 */
3975 if (curbuf->b_fname != NULL
3976 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3977 {
3978 MSG_PUTS("\n\"% ");
3979 dis_msg(curbuf->b_fname, FALSE);
3980 }
3981
3982 /*
3983 * display alternate file name
3984 */
3985 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
3986 {
3987 char_u *fname;
3988 linenr_T dummy;
3989
3990 if (buflist_name_nr(0, &fname, &dummy) != FAIL)
3991 {
3992 MSG_PUTS("\n\"# ");
3993 dis_msg(fname, FALSE);
3994 }
3995 }
3996
3997 /*
3998 * display last search pattern
3999 */
4000 if (last_search_pat() != NULL
4001 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int)
4002 {
4003 MSG_PUTS("\n\"/ ");
4004 dis_msg(last_search_pat(), FALSE);
4005 }
4006
4007#ifdef FEAT_EVAL
4008 /*
4009 * display last used expression
4010 */
4011 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
4012 && !got_int)
4013 {
4014 MSG_PUTS("\n\"= ");
4015 dis_msg(expr_line, FALSE);
4016 }
4017#endif
4018}
4019
4020/*
4021 * display a string for do_dis()
4022 * truncate at end of screen line
4023 */
4024 static void
4025dis_msg(p, skip_esc)
4026 char_u *p;
4027 int skip_esc; /* if TRUE, ignore trailing ESC */
4028{
4029 int n;
4030#ifdef FEAT_MBYTE
4031 int l;
4032#endif
4033
4034 n = (int)Columns - 6;
4035 while (*p != NUL
4036 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
4037 && (n -= ptr2cells(p)) >= 0)
4038 {
4039#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004040 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
4042 msg_outtrans_len(p, l);
4043 p += l;
4044 }
4045 else
4046#endif
4047 msg_outtrans_len(p++, 1);
4048 }
4049 ui_breakcheck();
4050}
4051
4052/*
4053 * join 'count' lines (minimal 2), including u_save()
4054 */
4055 void
4056do_do_join(count, insert_space)
4057 long count;
4058 int insert_space;
4059{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004060 colnr_T col = MAXCOL;
4061
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
4063 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL)
4064 return;
4065
4066 while (--count > 0)
4067 {
4068 line_breakcheck();
4069 if (got_int || do_join(insert_space) == FAIL)
4070 {
4071 beep_flush();
4072 break;
4073 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004074 if (col == MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4075 col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004078 /* Vi compatible: use the column of the first join */
4079 if (col != MAXCOL && vim_strchr(p_cpo, CPO_JOINCOL) != NULL)
4080 curwin->w_cursor.col = col;
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#if 0
4083 /*
4084 * Need to update the screen if the line where the cursor is became too
4085 * long to fit on the screen.
4086 */
4087 update_topline_redraw();
4088#endif
4089}
4090
4091/*
4092 * Join two lines at the cursor position.
4093 * "redraw" is TRUE when the screen should be updated.
4094 * Caller must have setup for undo.
4095 *
4096 * return FAIL for failure, OK ohterwise
4097 */
4098 int
4099do_join(insert_space)
4100 int insert_space;
4101{
4102 char_u *curr;
4103 char_u *next, *next_start;
4104 char_u *newp;
4105 int endcurr1, endcurr2;
4106 int currsize; /* size of the current line */
4107 int nextsize; /* size of the next line */
4108 int spaces; /* number of spaces to insert */
4109 linenr_T t;
4110
4111 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4112 return FAIL; /* can't join on last line */
4113
4114 curr = ml_get_curline();
4115 currsize = (int)STRLEN(curr);
4116 endcurr1 = endcurr2 = NUL;
4117 if (insert_space && currsize > 0)
4118 {
4119#ifdef FEAT_MBYTE
4120 if (has_mbyte)
4121 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004122 next = curr + currsize;
4123 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 endcurr1 = (*mb_ptr2char)(next);
4125 if (next > curr)
4126 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004127 mb_ptr_back(curr, next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 endcurr2 = (*mb_ptr2char)(next);
4129 }
4130 }
4131 else
4132#endif
4133 {
4134 endcurr1 = *(curr + currsize - 1);
4135 if (currsize > 1)
4136 endcurr2 = *(curr + currsize - 2);
4137 }
4138 }
4139
4140 next = next_start = ml_get((linenr_T)(curwin->w_cursor.lnum + 1));
4141 spaces = 0;
4142 if (insert_space)
4143 {
4144 next = skipwhite(next);
4145 if (*next != ')' && currsize != 0 && endcurr1 != TAB
4146#ifdef FEAT_MBYTE
4147 && (!has_format_option(FO_MBYTE_JOIN)
4148 || (mb_ptr2char(next) < 0x100 && endcurr1 < 0x100))
4149 && (!has_format_option(FO_MBYTE_JOIN2)
4150 || mb_ptr2char(next) < 0x100 || endcurr1 < 0x100)
4151#endif
4152 )
4153 {
4154 /* don't add a space if the line is ending in a space */
4155 if (endcurr1 == ' ')
4156 endcurr1 = endcurr2;
4157 else
4158 ++spaces;
4159 /* extra space when 'joinspaces' set and line ends in '.' */
4160 if ( p_js
4161 && (endcurr1 == '.'
4162 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4163 && (endcurr1 == '?' || endcurr1 == '!'))))
4164 ++spaces;
4165 }
4166 }
4167 nextsize = (int)STRLEN(next);
4168
4169 newp = alloc_check((unsigned)(currsize + nextsize + spaces + 1));
4170 if (newp == NULL)
4171 return FAIL;
4172
4173 /*
4174 * Insert the next line first, because we already have that pointer.
4175 * Curr has to be obtained again, because getting next will have
4176 * invalidated it.
4177 */
4178 mch_memmove(newp + currsize + spaces, next, (size_t)(nextsize + 1));
4179
4180 curr = ml_get_curline();
4181 mch_memmove(newp, curr, (size_t)currsize);
4182
4183 copy_spaces(newp + currsize, (size_t)spaces);
4184
4185 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
4186
4187 /* Only report the change in the first line here, del_lines() will report
4188 * the deleted line. */
4189 changed_lines(curwin->w_cursor.lnum, currsize,
4190 curwin->w_cursor.lnum + 1, 0L);
4191
4192 /*
4193 * Delete the following line. To do this we move the cursor there
4194 * briefly, and then move it back. After del_lines() the cursor may
4195 * have moved up (last line deleted), so the current lnum is kept in t.
4196 *
4197 * Move marks from the deleted line to the joined line, adjusting the
4198 * column. This is not Vi compatible, but Vi deletes the marks, thus that
4199 * should not really be a problem.
4200 */
4201 t = curwin->w_cursor.lnum;
4202 mark_col_adjust(t + 1, (colnr_T)0, (linenr_T)-1,
4203 (long)(currsize + spaces - (next - next_start)));
4204 ++curwin->w_cursor.lnum;
4205 del_lines(1L, FALSE);
4206 curwin->w_cursor.lnum = t;
4207
4208 /*
4209 * go to first character of the joined line
4210 */
4211 curwin->w_cursor.col = currsize;
4212 check_cursor_col();
4213#ifdef FEAT_VIRTUALEDIT
4214 curwin->w_cursor.coladd = 0;
4215#endif
4216 curwin->w_set_curswant = TRUE;
4217
4218 return OK;
4219}
4220
4221#ifdef FEAT_COMMENTS
4222/*
4223 * Return TRUE if the two comment leaders given are the same. "lnum" is
4224 * the first line. White-space is ignored. Note that the whole of
4225 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb
4226 */
4227 static int
4228same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags)
4229 linenr_T lnum;
4230 int leader1_len;
4231 char_u *leader1_flags;
4232 int leader2_len;
4233 char_u *leader2_flags;
4234{
4235 int idx1 = 0, idx2 = 0;
4236 char_u *p;
4237 char_u *line1;
4238 char_u *line2;
4239
4240 if (leader1_len == 0)
4241 return (leader2_len == 0);
4242
4243 /*
4244 * If first leader has 'f' flag, the lines can be joined only if the
4245 * second line does not have a leader.
4246 * If first leader has 'e' flag, the lines can never be joined.
4247 * If fist leader has 's' flag, the lines can only be joined if there is
4248 * some text after it and the second line has the 'm' flag.
4249 */
4250 if (leader1_flags != NULL)
4251 {
4252 for (p = leader1_flags; *p && *p != ':'; ++p)
4253 {
4254 if (*p == COM_FIRST)
4255 return (leader2_len == 0);
4256 if (*p == COM_END)
4257 return FALSE;
4258 if (*p == COM_START)
4259 {
4260 if (*(ml_get(lnum) + leader1_len) == NUL)
4261 return FALSE;
4262 if (leader2_flags == NULL || leader2_len == 0)
4263 return FALSE;
4264 for (p = leader2_flags; *p && *p != ':'; ++p)
4265 if (*p == COM_MIDDLE)
4266 return TRUE;
4267 return FALSE;
4268 }
4269 }
4270 }
4271
4272 /*
4273 * Get current line and next line, compare the leaders.
4274 * The first line has to be saved, only one line can be locked at a time.
4275 */
4276 line1 = vim_strsave(ml_get(lnum));
4277 if (line1 != NULL)
4278 {
4279 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1)
4280 ;
4281 line2 = ml_get(lnum + 1);
4282 for (idx2 = 0; idx2 < leader2_len; ++idx2)
4283 {
4284 if (!vim_iswhite(line2[idx2]))
4285 {
4286 if (line1[idx1++] != line2[idx2])
4287 break;
4288 }
4289 else
4290 while (vim_iswhite(line1[idx1]))
4291 ++idx1;
4292 }
4293 vim_free(line1);
4294 }
4295 return (idx2 == leader2_len && idx1 == leader1_len);
4296}
4297#endif
4298
4299/*
4300 * implementation of the format operator 'gq'
4301 */
4302 void
4303op_format(oap, keep_cursor)
4304 oparg_T *oap;
4305 int keep_cursor; /* keep cursor on same text char */
4306{
4307 long old_line_count = curbuf->b_ml.ml_line_count;
4308
4309 /* Place the cursor where the "gq" or "gw" command was given, so that "u"
4310 * can put it back there. */
4311 curwin->w_cursor = oap->cursor_start;
4312
4313 if (u_save((linenr_T)(oap->start.lnum - 1),
4314 (linenr_T)(oap->end.lnum + 1)) == FAIL)
4315 return;
4316 curwin->w_cursor = oap->start;
4317
4318#ifdef FEAT_VISUAL
4319 if (oap->is_VIsual)
4320 /* When there is no change: need to remove the Visual selection */
4321 redraw_curbuf_later(INVERTED);
4322#endif
4323
4324 /* Set '[ mark at the start of the formatted area */
4325 curbuf->b_op_start = oap->start;
4326
4327 /* For "gw" remember the cursor position and put it back below (adjusted
4328 * for joined and split lines). */
4329 if (keep_cursor)
4330 saved_cursor = oap->cursor_start;
4331
4332 format_lines(oap->line_count);
4333
4334 /*
4335 * Leave the cursor at the first non-blank of the last formatted line.
4336 * If the cursor was moved one line back (e.g. with "Q}") go to the next
4337 * line, so "." will do the next lines.
4338 */
4339 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4340 ++curwin->w_cursor.lnum;
4341 beginline(BL_WHITE | BL_FIX);
4342 old_line_count = curbuf->b_ml.ml_line_count - old_line_count;
4343 msgmore(old_line_count);
4344
4345 /* put '] mark on the end of the formatted area */
4346 curbuf->b_op_end = curwin->w_cursor;
4347
4348 if (keep_cursor)
4349 {
4350 curwin->w_cursor = saved_cursor;
4351 saved_cursor.lnum = 0;
4352 }
4353
4354#ifdef FEAT_VISUAL
4355 if (oap->is_VIsual)
4356 {
4357 win_T *wp;
4358
4359 FOR_ALL_WINDOWS(wp)
4360 {
4361 if (wp->w_old_cursor_lnum != 0)
4362 {
4363 /* When lines have been inserted or deleted, adjust the end of
4364 * the Visual area to be redrawn. */
4365 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum)
4366 wp->w_old_cursor_lnum += old_line_count;
4367 else
4368 wp->w_old_visual_lnum += old_line_count;
4369 }
4370 }
4371 }
4372#endif
4373}
4374
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004375#if defined(FEAT_EVAL) || defined(PROTO)
4376/*
4377 * Implementation of the format operator 'gq' for when using 'formatexpr'.
4378 */
4379 void
4380op_formatexpr(oap)
4381 oparg_T *oap;
4382{
4383# ifdef FEAT_VISUAL
4384 if (oap->is_VIsual)
4385 /* When there is no change: need to remove the Visual selection */
4386 redraw_curbuf_later(INVERTED);
4387# endif
4388
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004389 (void)fex_format(oap->start.lnum, oap->line_count, NUL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004390}
4391
4392 int
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004393fex_format(lnum, count, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004394 linenr_T lnum;
4395 long count;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004396 int c; /* character to be inserted */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004397{
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004398 int use_sandbox = was_set_insecurely((char_u *)"formatexpr",
4399 OPT_LOCAL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004400 int r;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004401#ifdef FEAT_MBYTE
4402 char_u buf[MB_MAXBYTES];
4403#else
4404 char_u buf[2];
4405#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004406
4407 /*
4408 * Set v:lnum to the first line number and v:count to the number of lines.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004409 * Set v:char to the character to be inserted (can be NUL).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004410 */
4411 set_vim_var_nr(VV_LNUM, lnum);
4412 set_vim_var_nr(VV_COUNT, count);
4413
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004414#ifdef FEAT_MBYTE
4415 if (has_mbyte)
4416 buf[(*mb_char2bytes)(c, buf)] = NUL;
4417 else
4418#endif
4419 {
4420 buf[0] = c;
4421 buf[1] = NUL;
4422 }
4423 set_vim_var_string(VV_CHAR, buf, -1);
4424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004425 /*
4426 * Evaluate the function.
4427 */
4428 if (use_sandbox)
4429 ++sandbox;
4430 r = eval_to_number(curbuf->b_p_fex);
4431 if (use_sandbox)
4432 --sandbox;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004433
4434 set_vim_var_string(VV_CHAR, NULL, -1);
4435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004436 return r;
4437}
4438#endif
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440/*
4441 * Format "line_count" lines, starting at the cursor position.
4442 * When "line_count" is negative, format until the end of the paragraph.
4443 * Lines after the cursor line are saved for undo, caller must have saved the
4444 * first line.
4445 */
4446 void
4447format_lines(line_count)
4448 linenr_T line_count;
4449{
4450 int max_len;
4451 int is_not_par; /* current line not part of parag. */
4452 int next_is_not_par; /* next line not part of paragraph */
4453 int is_end_par; /* at end of paragraph */
4454 int prev_is_end_par = FALSE;/* prev. line not part of parag. */
4455 int next_is_start_par = FALSE;
4456#ifdef FEAT_COMMENTS
4457 int leader_len = 0; /* leader len of current line */
4458 int next_leader_len; /* leader len of next line */
4459 char_u *leader_flags = NULL; /* flags for leader of current line */
4460 char_u *next_leader_flags; /* flags for leader of next line */
4461 int do_comments; /* format comments */
4462#endif
4463 int advance = TRUE;
4464 int second_indent = -1;
4465 int do_second_indent;
4466 int do_number_indent;
4467 int do_trail_white;
4468 int first_par_line = TRUE;
4469 int smd_save;
4470 long count;
4471 int need_set_indent = TRUE; /* set indent of next paragraph */
4472 int force_format = FALSE;
4473 int old_State = State;
4474
4475 /* length of a line to force formatting: 3 * 'tw' */
4476 max_len = comp_textwidth(TRUE) * 3;
4477
4478 /* check for 'q', '2' and '1' in 'formatoptions' */
4479#ifdef FEAT_COMMENTS
4480 do_comments = has_format_option(FO_Q_COMS);
4481#endif
4482 do_second_indent = has_format_option(FO_Q_SECOND);
4483 do_number_indent = has_format_option(FO_Q_NUMBER);
4484 do_trail_white = has_format_option(FO_WHITE_PAR);
4485
4486 /*
4487 * Get info about the previous and current line.
4488 */
4489 if (curwin->w_cursor.lnum > 1)
4490 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1
4491#ifdef FEAT_COMMENTS
4492 , &leader_len, &leader_flags, do_comments
4493#endif
4494 );
4495 else
4496 is_not_par = TRUE;
4497 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum
4498#ifdef FEAT_COMMENTS
4499 , &next_leader_len, &next_leader_flags, do_comments
4500#endif
4501 );
4502 is_end_par = (is_not_par || next_is_not_par);
4503 if (!is_end_par && do_trail_white)
4504 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1);
4505
4506 curwin->w_cursor.lnum--;
4507 for (count = line_count; count != 0 && !got_int; --count)
4508 {
4509 /*
4510 * Advance to next paragraph.
4511 */
4512 if (advance)
4513 {
4514 curwin->w_cursor.lnum++;
4515 prev_is_end_par = is_end_par;
4516 is_not_par = next_is_not_par;
4517#ifdef FEAT_COMMENTS
4518 leader_len = next_leader_len;
4519 leader_flags = next_leader_flags;
4520#endif
4521 }
4522
4523 /*
4524 * The last line to be formatted.
4525 */
4526 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4527 {
4528 next_is_not_par = TRUE;
4529#ifdef FEAT_COMMENTS
4530 next_leader_len = 0;
4531 next_leader_flags = NULL;
4532#endif
4533 }
4534 else
4535 {
4536 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1
4537#ifdef FEAT_COMMENTS
4538 , &next_leader_len, &next_leader_flags, do_comments
4539#endif
4540 );
4541 if (do_number_indent)
4542 next_is_start_par =
4543 (get_number_indent(curwin->w_cursor.lnum + 1) > 0);
4544 }
4545 advance = TRUE;
4546 is_end_par = (is_not_par || next_is_not_par || next_is_start_par);
4547 if (!is_end_par && do_trail_white)
4548 is_end_par = !ends_in_white(curwin->w_cursor.lnum);
4549
4550 /*
4551 * Skip lines that are not in a paragraph.
4552 */
4553 if (is_not_par)
4554 {
4555 if (line_count < 0)
4556 break;
4557 }
4558 else
4559 {
4560 /*
4561 * For the first line of a paragraph, check indent of second line.
4562 * Don't do this for comments and empty lines.
4563 */
4564 if (first_par_line
4565 && (do_second_indent || do_number_indent)
4566 && prev_is_end_par
4567 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
4568#ifdef FEAT_COMMENTS
4569 && leader_len == 0
4570 && next_leader_len == 0
4571#endif
4572 )
4573 {
4574 if (do_second_indent
4575 && !lineempty(curwin->w_cursor.lnum + 1))
4576 second_indent = get_indent_lnum(curwin->w_cursor.lnum + 1);
4577 else if (do_number_indent)
4578 second_indent = get_number_indent(curwin->w_cursor.lnum);
4579 }
4580
4581 /*
4582 * When the comment leader changes, it's the end of the paragraph.
4583 */
4584 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count
4585#ifdef FEAT_COMMENTS
4586 || !same_leader(curwin->w_cursor.lnum,
4587 leader_len, leader_flags,
4588 next_leader_len, next_leader_flags)
4589#endif
4590 )
4591 is_end_par = TRUE;
4592
4593 /*
4594 * If we have got to the end of a paragraph, or the line is
4595 * getting long, format it.
4596 */
4597 if (is_end_par || force_format)
4598 {
4599 if (need_set_indent)
4600 /* replace indent in first line with minimal number of
4601 * tabs and spaces, according to current options */
4602 (void)set_indent(get_indent(), SIN_CHANGED);
4603
4604 /* put cursor on last non-space */
4605 State = NORMAL; /* don't go past end-of-line */
4606 coladvance((colnr_T)MAXCOL);
4607 while (curwin->w_cursor.col && vim_isspace(gchar_cursor()))
4608 dec_cursor();
4609
4610 /* do the formatting, without 'showmode' */
4611 State = INSERT; /* for open_line() */
4612 smd_save = p_smd;
4613 p_smd = FALSE;
4614 insertchar(NUL, INSCHAR_FORMAT
4615#ifdef FEAT_COMMENTS
4616 + (do_comments ? INSCHAR_DO_COM : 0)
4617#endif
4618 , second_indent);
4619 State = old_State;
4620 p_smd = smd_save;
4621 second_indent = -1;
4622 /* at end of par.: need to set indent of next par. */
4623 need_set_indent = is_end_par;
4624 if (is_end_par)
4625 {
4626 /* When called with a negative line count, break at the
4627 * end of the paragraph. */
4628 if (line_count < 0)
4629 break;
4630 first_par_line = TRUE;
4631 }
4632 force_format = FALSE;
4633 }
4634
4635 /*
4636 * When still in same paragraph, join the lines together. But
4637 * first delete the comment leader from the second line.
4638 */
4639 if (!is_end_par)
4640 {
4641 advance = FALSE;
4642 curwin->w_cursor.lnum++;
4643 curwin->w_cursor.col = 0;
4644 if (line_count < 0 && u_save_cursor() == FAIL)
4645 break;
4646#ifdef FEAT_COMMENTS
Bram Moolenaare3226be2005-12-18 22:10:00 +00004647 (void)del_bytes((long)next_leader_len, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (next_leader_len > 0)
4649 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
4650 (long)-next_leader_len);
4651#endif
4652 curwin->w_cursor.lnum--;
4653 if (do_join(TRUE) == FAIL)
4654 {
4655 beep_flush();
4656 break;
4657 }
4658 first_par_line = FALSE;
4659 /* If the line is getting long, format it next time */
4660 if (STRLEN(ml_get_curline()) > (size_t)max_len)
4661 force_format = TRUE;
4662 else
4663 force_format = FALSE;
4664 }
4665 }
4666 line_breakcheck();
4667 }
4668}
4669
4670/*
4671 * Return TRUE if line "lnum" ends in a white character.
4672 */
4673 static int
4674ends_in_white(lnum)
4675 linenr_T lnum;
4676{
4677 char_u *s = ml_get(lnum);
4678 size_t l;
4679
4680 if (*s == NUL)
4681 return FALSE;
4682 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro
4683 * invocation may call function multiple times". */
4684 l = STRLEN(s) - 1;
4685 return vim_iswhite(s[l]);
4686}
4687
4688/*
4689 * Blank lines, and lines containing only the comment leader, are left
4690 * untouched by the formatting. The function returns TRUE in this
4691 * case. It also returns TRUE when a line starts with the end of a comment
4692 * ('e' in comment flags), so that this line is skipped, and not joined to the
4693 * previous line. A new paragraph starts after a blank line, or when the
4694 * comment leader changes -- webb.
4695 */
4696#ifdef FEAT_COMMENTS
4697 static int
4698fmt_check_par(lnum, leader_len, leader_flags, do_comments)
4699 linenr_T lnum;
4700 int *leader_len;
4701 char_u **leader_flags;
4702 int do_comments;
4703{
4704 char_u *flags = NULL; /* init for GCC */
4705 char_u *ptr;
4706
4707 ptr = ml_get(lnum);
4708 if (do_comments)
4709 *leader_len = get_leader_len(ptr, leader_flags, FALSE);
4710 else
4711 *leader_len = 0;
4712
4713 if (*leader_len > 0)
4714 {
4715 /*
4716 * Search for 'e' flag in comment leader flags.
4717 */
4718 flags = *leader_flags;
4719 while (*flags && *flags != ':' && *flags != COM_END)
4720 ++flags;
4721 }
4722
4723 return (*skipwhite(ptr + *leader_len) == NUL
4724 || (*leader_len > 0 && *flags == COM_END)
4725 || startPS(lnum, NUL, FALSE));
4726}
4727#else
4728 static int
4729fmt_check_par(lnum)
4730 linenr_T lnum;
4731{
4732 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE));
4733}
4734#endif
4735
4736/*
4737 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the
4738 * previous line is in the same paragraph. Used for auto-formatting.
4739 */
4740 int
4741paragraph_start(lnum)
4742 linenr_T lnum;
4743{
4744 char_u *p;
4745#ifdef FEAT_COMMENTS
4746 int leader_len = 0; /* leader len of current line */
4747 char_u *leader_flags = NULL; /* flags for leader of current line */
4748 int next_leader_len; /* leader len of next line */
4749 char_u *next_leader_flags; /* flags for leader of next line */
4750 int do_comments; /* format comments */
4751#endif
4752
4753 if (lnum <= 1)
4754 return TRUE; /* start of the file */
4755
4756 p = ml_get(lnum - 1);
4757 if (*p == NUL)
4758 return TRUE; /* after empty line */
4759
4760#ifdef FEAT_COMMENTS
4761 do_comments = has_format_option(FO_Q_COMS);
4762#endif
4763 if (fmt_check_par(lnum - 1
4764#ifdef FEAT_COMMENTS
4765 , &leader_len, &leader_flags, do_comments
4766#endif
4767 ))
4768 return TRUE; /* after non-paragraph line */
4769
4770 if (fmt_check_par(lnum
4771#ifdef FEAT_COMMENTS
4772 , &next_leader_len, &next_leader_flags, do_comments
4773#endif
4774 ))
4775 return TRUE; /* "lnum" is not a paragraph line */
4776
4777 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1))
4778 return TRUE; /* missing trailing space in previous line. */
4779
4780 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0))
4781 return TRUE; /* numbered item starts in "lnum". */
4782
4783#ifdef FEAT_COMMENTS
4784 if (!same_leader(lnum - 1, leader_len, leader_flags,
4785 next_leader_len, next_leader_flags))
4786 return TRUE; /* change of comment leader. */
4787#endif
4788
4789 return FALSE;
4790}
4791
4792#ifdef FEAT_VISUAL
4793/*
4794 * prepare a few things for block mode yank/delete/tilde
4795 *
4796 * for delete:
4797 * - textlen includes the first/last char to be (partly) deleted
4798 * - start/endspaces is the number of columns that are taken by the
4799 * first/last deleted char minus the number of columns that have to be
4800 * deleted. for yank and tilde:
4801 * - textlen includes the first/last char to be wholly yanked
4802 * - start/endspaces is the number of columns of the first/last yanked char
4803 * that are to be yanked.
4804 */
4805 static void
4806block_prep(oap, bdp, lnum, is_del)
4807 oparg_T *oap;
4808 struct block_def *bdp;
4809 linenr_T lnum;
4810 int is_del;
4811{
4812 int incr = 0;
4813 char_u *pend;
4814 char_u *pstart;
4815 char_u *line;
4816 char_u *prev_pstart;
4817 char_u *prev_pend;
4818
4819 bdp->startspaces = 0;
4820 bdp->endspaces = 0;
4821 bdp->textlen = 0;
4822 bdp->start_vcol = 0;
4823 bdp->end_vcol = 0;
4824#ifdef FEAT_VISUALEXTRA
4825 bdp->is_short = FALSE;
4826 bdp->is_oneChar = FALSE;
4827 bdp->pre_whitesp = 0;
4828 bdp->pre_whitesp_c = 0;
4829 bdp->end_char_vcols = 0;
4830#endif
4831 bdp->start_char_vcols = 0;
4832
4833 line = ml_get(lnum);
4834 pstart = line;
4835 prev_pstart = line;
4836 while (bdp->start_vcol < oap->start_vcol && *pstart)
4837 {
4838 /* Count a tab for what it's worth (if list mode not on) */
4839 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol);
4840 bdp->start_vcol += incr;
4841#ifdef FEAT_VISUALEXTRA
4842 if (vim_iswhite(*pstart))
4843 {
4844 bdp->pre_whitesp += incr;
4845 bdp->pre_whitesp_c++;
4846 }
4847 else
4848 {
4849 bdp->pre_whitesp = 0;
4850 bdp->pre_whitesp_c = 0;
4851 }
4852#endif
4853 prev_pstart = pstart;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004854 mb_ptr_adv(pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 bdp->start_char_vcols = incr;
4857 if (bdp->start_vcol < oap->start_vcol) /* line too short */
4858 {
4859 bdp->end_vcol = bdp->start_vcol;
4860#ifdef FEAT_VISUALEXTRA
4861 bdp->is_short = TRUE;
4862#endif
4863 if (!is_del || oap->op_type == OP_APPEND)
4864 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1;
4865 }
4866 else
4867 {
4868 /* notice: this converts partly selected Multibyte characters to
4869 * spaces, too. */
4870 bdp->startspaces = bdp->start_vcol - oap->start_vcol;
4871 if (is_del && bdp->startspaces)
4872 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces;
4873 pend = pstart;
4874 bdp->end_vcol = bdp->start_vcol;
4875 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */
4876 {
4877#ifdef FEAT_VISUALEXTRA
4878 bdp->is_oneChar = TRUE;
4879#endif
4880 if (oap->op_type == OP_INSERT)
4881 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4882 else if (oap->op_type == OP_APPEND)
4883 {
4884 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1;
4885 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces;
4886 }
4887 else
4888 {
4889 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1;
4890 if (is_del && oap->op_type != OP_LSHIFT)
4891 {
4892 /* just putting the sum of those two into
4893 * bdp->startspaces doesn't work for Visual replace,
4894 * so we have to split the tab in two */
4895 bdp->startspaces = bdp->start_char_vcols
4896 - (bdp->start_vcol - oap->start_vcol);
4897 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4898 }
4899 }
4900 }
4901 else
4902 {
4903 prev_pend = pend;
4904 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL)
4905 {
4906 /* Count a tab for what it's worth (if list mode not on) */
4907 prev_pend = pend;
4908 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol);
4909 bdp->end_vcol += incr;
4910 }
4911 if (bdp->end_vcol <= oap->end_vcol
4912 && (!is_del
4913 || oap->op_type == OP_APPEND
4914 || oap->op_type == OP_REPLACE)) /* line too short */
4915 {
4916#ifdef FEAT_VISUALEXTRA
4917 bdp->is_short = TRUE;
4918#endif
4919 /* Alternative: include spaces to fill up the block.
4920 * Disadvantage: can lead to trailing spaces when the line is
4921 * short where the text is put */
4922 /* if (!is_del || oap->op_type == OP_APPEND) */
4923 if (oap->op_type == OP_APPEND || virtual_op)
4924 bdp->endspaces = oap->end_vcol - bdp->end_vcol
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +00004925 + oap->inclusive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
4927 bdp->endspaces = 0; /* replace doesn't add characters */
4928 }
4929 else if (bdp->end_vcol > oap->end_vcol)
4930 {
4931 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1;
4932 if (!is_del && bdp->endspaces)
4933 {
4934 bdp->endspaces = incr - bdp->endspaces;
4935 if (pend != pstart)
4936 pend = prev_pend;
4937 }
4938 }
4939 }
4940#ifdef FEAT_VISUALEXTRA
4941 bdp->end_char_vcols = incr;
4942#endif
4943 if (is_del && bdp->startspaces)
4944 pstart = prev_pstart;
4945 bdp->textlen = (int)(pend - pstart);
4946 }
4947 bdp->textcol = (colnr_T) (pstart - line);
4948 bdp->textstart = pstart;
4949}
4950#endif /* FEAT_VISUAL */
4951
4952#ifdef FEAT_RIGHTLEFT
4953static void reverse_line __ARGS((char_u *s));
4954
4955 static void
4956reverse_line(s)
4957 char_u *s;
4958{
4959 int i, j;
4960 char_u c;
4961
4962 if ((i = (int)STRLEN(s) - 1) <= 0)
4963 return;
4964
4965 curwin->w_cursor.col = i - curwin->w_cursor.col;
4966 for (j = 0; j < i; j++, i--)
4967 {
4968 c = s[i]; s[i] = s[j]; s[j] = c;
4969 }
4970}
4971
4972# define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr);
4973#else
4974# define RLADDSUBFIX(ptr)
4975#endif
4976
4977/*
4978 * add or subtract 'Prenum1' from a number in a line
4979 * 'command' is CTRL-A for add, CTRL-X for subtract
4980 *
4981 * return FAIL for failure, OK otherwise
4982 */
4983 int
4984do_addsub(command, Prenum1)
4985 int command;
4986 linenr_T Prenum1;
4987{
4988 int col;
4989 char_u *buf1;
4990 char_u buf2[NUMBUFLEN];
4991 int hex; /* 'X' or 'x': hex; '0': octal */
4992 static int hexupper = FALSE; /* 0xABC */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004993 unsigned long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 long_u oldn;
4995 char_u *ptr;
4996 int c;
4997 int length = 0; /* character length of the number */
4998 int todel;
4999 int dohex;
5000 int dooct;
5001 int doalp;
5002 int firstdigit;
5003 int negative;
5004 int subtract;
5005
5006 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
5007 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
5008 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
5009
5010 ptr = ml_get_curline();
5011 RLADDSUBFIX(ptr);
5012
5013 /*
5014 * First check if we are on a hexadecimal number, after the "0x".
5015 */
5016 col = curwin->w_cursor.col;
5017 if (dohex)
5018 while (col > 0 && vim_isxdigit(ptr[col]))
5019 --col;
5020 if ( dohex
5021 && col > 0
5022 && (ptr[col] == 'X'
5023 || ptr[col] == 'x')
5024 && ptr[col - 1] == '0'
5025 && vim_isxdigit(ptr[col + 1]))
5026 {
5027 /*
5028 * Found hexadecimal number, move to its start.
5029 */
5030 --col;
5031 }
5032 else
5033 {
5034 /*
5035 * Search forward and then backward to find the start of number.
5036 */
5037 col = curwin->w_cursor.col;
5038
5039 while (ptr[col] != NUL
5040 && !vim_isdigit(ptr[col])
5041 && !(doalp && ASCII_ISALPHA(ptr[col])))
5042 ++col;
5043
5044 while (col > 0
5045 && vim_isdigit(ptr[col - 1])
5046 && !(doalp && ASCII_ISALPHA(ptr[col])))
5047 --col;
5048 }
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /*
5051 * If a number was found, and saving for undo works, replace the number.
5052 */
5053 firstdigit = ptr[col];
5054 RLADDSUBFIX(ptr);
5055 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
5056 || u_save_cursor() != OK)
5057 {
5058 beep_flush();
5059 return FAIL;
5060 }
5061
5062 /* get ptr again, because u_save() may have changed it */
5063 ptr = ml_get_curline();
5064 RLADDSUBFIX(ptr);
5065
5066 if (doalp && ASCII_ISALPHA(firstdigit))
5067 {
5068 /* decrement or increment alphabetic character */
5069 if (command == Ctrl_X)
5070 {
5071 if (CharOrd(firstdigit) < Prenum1)
5072 {
5073 if (isupper(firstdigit))
5074 firstdigit = 'A';
5075 else
5076 firstdigit = 'a';
5077 }
5078 else
5079#ifdef EBCDIC
5080 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1);
5081#else
5082 firstdigit -= Prenum1;
5083#endif
5084 }
5085 else
5086 {
5087 if (26 - CharOrd(firstdigit) - 1 < Prenum1)
5088 {
5089 if (isupper(firstdigit))
5090 firstdigit = 'Z';
5091 else
5092 firstdigit = 'z';
5093 }
5094 else
5095#ifdef EBCDIC
5096 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1);
5097#else
5098 firstdigit += Prenum1;
5099#endif
5100 }
5101 curwin->w_cursor.col = col;
5102 (void)del_char(FALSE);
5103 ins_char(firstdigit);
5104 }
5105 else
5106 {
5107 negative = FALSE;
5108 if (col > 0 && ptr[col - 1] == '-') /* negative number */
5109 {
5110 --col;
5111 negative = TRUE;
5112 }
5113
5114 /* get the number value (unsigned) */
5115 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n);
5116
5117 /* ignore leading '-' for hex and octal numbers */
5118 if (hex && negative)
5119 {
5120 ++col;
5121 --length;
5122 negative = FALSE;
5123 }
5124
5125 /* add or subtract */
5126 subtract = FALSE;
5127 if (command == Ctrl_X)
5128 subtract ^= TRUE;
5129 if (negative)
5130 subtract ^= TRUE;
5131
5132 oldn = n;
5133 if (subtract)
5134 n -= (unsigned long)Prenum1;
5135 else
5136 n += (unsigned long)Prenum1;
5137
5138 /* handle wraparound for decimal numbers */
5139 if (!hex)
5140 {
5141 if (subtract)
5142 {
5143 if (n > oldn)
5144 {
5145 n = 1 + (n ^ (unsigned long)-1);
5146 negative ^= TRUE;
5147 }
5148 }
5149 else /* add */
5150 {
5151 if (n < oldn)
5152 {
5153 n = (n ^ (unsigned long)-1);
5154 negative ^= TRUE;
5155 }
5156 }
5157 if (n == 0)
5158 negative = FALSE;
5159 }
5160
5161 /*
5162 * Delete the old number.
5163 */
5164 curwin->w_cursor.col = col;
5165 todel = length;
5166 c = gchar_cursor();
5167 /*
5168 * Don't include the '-' in the length, only the length of the part
5169 * after it is kept the same.
5170 */
5171 if (c == '-')
5172 --length;
5173 while (todel-- > 0)
5174 {
5175 if (c < 0x100 && isalpha(c))
5176 {
5177 if (isupper(c))
5178 hexupper = TRUE;
5179 else
5180 hexupper = FALSE;
5181 }
5182 /* del_char() will mark line needing displaying */
5183 (void)del_char(FALSE);
5184 c = gchar_cursor();
5185 }
5186
5187 /*
5188 * Prepare the leading characters in buf1[].
5189 * When there are many leading zeros it could be very long. Allocate
5190 * a bit too much.
5191 */
5192 buf1 = alloc((unsigned)length + NUMBUFLEN);
5193 if (buf1 == NULL)
5194 return FAIL;
5195 ptr = buf1;
5196 if (negative)
5197 {
5198 *ptr++ = '-';
5199 }
5200 if (hex)
5201 {
5202 *ptr++ = '0';
5203 --length;
5204 }
5205 if (hex == 'x' || hex == 'X')
5206 {
5207 *ptr++ = hex;
5208 --length;
5209 }
5210
5211 /*
5212 * Put the number characters in buf2[].
5213 */
5214 if (hex == 0)
5215 sprintf((char *)buf2, "%lu", n);
5216 else if (hex == '0')
5217 sprintf((char *)buf2, "%lo", n);
5218 else if (hex && hexupper)
5219 sprintf((char *)buf2, "%lX", n);
5220 else
5221 sprintf((char *)buf2, "%lx", n);
5222 length -= (int)STRLEN(buf2);
5223
5224 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005225 * Adjust number of zeros to the new number of digits, so the
5226 * total length of the number remains the same.
5227 * Don't do this when
5228 * the result may look like an octal number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005230 if (firstdigit == '0' && !(dooct && hex == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 while (length-- > 0)
5232 *ptr++ = '0';
5233 *ptr = NUL;
5234 STRCAT(buf1, buf2);
5235 ins_str(buf1); /* insert the new number */
5236 vim_free(buf1);
5237 }
5238 --curwin->w_cursor.col;
5239 curwin->w_set_curswant = TRUE;
5240#ifdef FEAT_RIGHTLEFT
5241 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
5242 RLADDSUBFIX(ptr);
5243#endif
5244 return OK;
5245}
5246
5247#ifdef FEAT_VIMINFO
5248 int
5249read_viminfo_register(virp, force)
5250 vir_T *virp;
5251 int force;
5252{
5253 int eof;
5254 int do_it = TRUE;
5255 int size;
5256 int limit;
5257 int i;
5258 int set_prev = FALSE;
5259 char_u *str;
5260 char_u **array = NULL;
5261
5262 /* We only get here (hopefully) if line[0] == '"' */
5263 str = virp->vir_line + 1;
5264 if (*str == '"')
5265 {
5266 set_prev = TRUE;
5267 str++;
5268 }
5269 if (!ASCII_ISALNUM(*str) && *str != '-')
5270 {
5271 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line))
5272 return TRUE; /* too many errors, pretend end-of-file */
5273 do_it = FALSE;
5274 }
5275 get_yank_register(*str++, FALSE);
5276 if (!force && y_current->y_array != NULL)
5277 do_it = FALSE;
5278 size = 0;
5279 limit = 100; /* Optimized for registers containing <= 100 lines */
5280 if (do_it)
5281 {
5282 if (set_prev)
5283 y_previous = y_current;
5284 vim_free(y_current->y_array);
5285 array = y_current->y_array =
5286 (char_u **)alloc((unsigned)(limit * sizeof(char_u *)));
5287 str = skipwhite(str);
5288 if (STRNCMP(str, "CHAR", 4) == 0)
5289 y_current->y_type = MCHAR;
5290#ifdef FEAT_VISUAL
5291 else if (STRNCMP(str, "BLOCK", 5) == 0)
5292 y_current->y_type = MBLOCK;
5293#endif
5294 else
5295 y_current->y_type = MLINE;
5296 /* get the block width; if it's missing we get a zero, which is OK */
5297 str = skipwhite(skiptowhite(str));
5298#ifdef FEAT_VISUAL
5299 y_current->y_width = getdigits(&str);
5300#else
5301 (void)getdigits(&str);
5302#endif
5303 }
5304
5305 while (!(eof = viminfo_readline(virp))
5306 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<'))
5307 {
5308 if (do_it)
5309 {
5310 if (size >= limit)
5311 {
5312 y_current->y_array = (char_u **)
5313 alloc((unsigned)(limit * 2 * sizeof(char_u *)));
5314 for (i = 0; i < limit; i++)
5315 y_current->y_array[i] = array[i];
5316 vim_free(array);
5317 limit *= 2;
5318 array = y_current->y_array;
5319 }
5320 str = viminfo_readstring(virp, 1, TRUE);
5321 if (str != NULL)
5322 array[size++] = str;
5323 else
5324 do_it = FALSE;
5325 }
5326 }
5327 if (do_it)
5328 {
5329 if (size == 0)
5330 {
5331 vim_free(array);
5332 y_current->y_array = NULL;
5333 }
5334 else if (size < limit)
5335 {
5336 y_current->y_array =
5337 (char_u **)alloc((unsigned)(size * sizeof(char_u *)));
5338 for (i = 0; i < size; i++)
5339 y_current->y_array[i] = array[i];
5340 vim_free(array);
5341 }
5342 y_current->y_size = size;
5343 }
5344 return eof;
5345}
5346
5347 void
5348write_viminfo_registers(fp)
5349 FILE *fp;
5350{
5351 int i, j;
5352 char_u *type;
5353 char_u c;
5354 int num_lines;
5355 int max_num_lines;
5356 int max_kbyte;
5357 long len;
5358
5359 fprintf(fp, _("\n# Registers:\n"));
5360
5361 /* Get '<' value, use old '"' value if '<' is not found. */
5362 max_num_lines = get_viminfo_parameter('<');
5363 if (max_num_lines < 0)
5364 max_num_lines = get_viminfo_parameter('"');
5365 if (max_num_lines == 0)
5366 return;
5367 max_kbyte = get_viminfo_parameter('s');
5368 if (max_kbyte == 0)
5369 return;
5370 for (i = 0; i < NUM_REGISTERS; i++)
5371 {
5372 if (y_regs[i].y_array == NULL)
5373 continue;
5374#ifdef FEAT_CLIPBOARD
5375 /* Skip '*'/'+' register, we don't want them back next time */
5376 if (i == STAR_REGISTER || i == PLUS_REGISTER)
5377 continue;
5378#endif
5379#ifdef FEAT_DND
5380 /* Neither do we want the '~' register */
5381 if (i == TILDE_REGISTER)
5382 continue;
5383#endif
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005384 /* Skip empty registers. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 num_lines = y_regs[i].y_size;
Bram Moolenaard7ee7ce2005-01-03 21:02:03 +00005386 if (num_lines == 0
5387 || (num_lines == 1 && y_regs[i].y_type == MCHAR
5388 && STRLEN(y_regs[i].y_array[0]) == 0))
5389 continue;
5390
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 if (max_kbyte > 0)
5392 {
5393 /* Skip register if there is more text than the maximum size. */
5394 len = 0;
5395 for (j = 0; j < num_lines; j++)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005396 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 if (len > (long)max_kbyte * 1024L)
5398 continue;
5399 }
5400
5401 switch (y_regs[i].y_type)
5402 {
5403 case MLINE:
5404 type = (char_u *)"LINE";
5405 break;
5406 case MCHAR:
5407 type = (char_u *)"CHAR";
5408 break;
5409#ifdef FEAT_VISUAL
5410 case MBLOCK:
5411 type = (char_u *)"BLOCK";
5412 break;
5413#endif
5414 default:
5415 sprintf((char *)IObuff, _("E574: Unknown register type %d"),
Bram Moolenaar555b2802005-05-19 21:08:39 +00005416 y_regs[i].y_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 emsg(IObuff);
5418 type = (char_u *)"LINE";
5419 break;
5420 }
5421 if (y_previous == &y_regs[i])
5422 fprintf(fp, "\"");
5423 c = get_register_name(i);
5424 fprintf(fp, "\"%c\t%s\t%d\n", c, type,
5425#ifdef FEAT_VISUAL
5426 (int)y_regs[i].y_width
5427#else
5428 0
5429#endif
5430 );
5431
5432 /* If max_num_lines < 0, then we save ALL the lines in the register */
5433 if (max_num_lines > 0 && num_lines > max_num_lines)
5434 num_lines = max_num_lines;
5435 for (j = 0; j < num_lines; j++)
5436 {
5437 putc('\t', fp);
5438 viminfo_writestring(fp, y_regs[i].y_array[j]);
5439 }
5440 }
5441}
5442#endif /* FEAT_VIMINFO */
5443
5444#if defined(FEAT_CLIPBOARD) || defined(PROTO)
5445/*
5446 * SELECTION / PRIMARY ('*')
5447 *
5448 * Text selection stuff that uses the GUI selection register '*'. When using a
5449 * GUI this may be text from another window, otherwise it is the last text we
5450 * had highlighted with VIsual mode. With mouse support, clicking the middle
5451 * button performs the paste, otherwise you will need to do <"*p>. "
5452 * If not under X, it is synonymous with the clipboard register '+'.
5453 *
5454 * X CLIPBOARD ('+')
5455 *
5456 * Text selection stuff that uses the GUI clipboard register '+'.
5457 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection.
5458 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed",
5459 * otherwise you will need to do <"+p>. "
5460 * If not under X, it is synonymous with the selection register '*'.
5461 */
5462
5463/*
5464 * Routine to export any final X selection we had to the environment
5465 * so that the text is still available after vim has exited. X selections
5466 * only exist while the owning application exists, so we write to the
5467 * permanent (while X runs) store CUT_BUFFER0.
5468 * Dump the CLIPBOARD selection if we own it (it's logically the more
5469 * 'permanent' of the two), otherwise the PRIMARY one.
5470 * For now, use a hard-coded sanity limit of 1Mb of data.
5471 */
5472#if defined(FEAT_X11) && defined(FEAT_CLIPBOARD)
5473 void
5474x11_export_final_selection()
5475{
5476 Display *dpy;
5477 char_u *str = NULL;
5478 long_u len = 0;
5479 int motion_type = -1;
5480
5481# ifdef FEAT_GUI
5482 if (gui.in_use)
5483 dpy = X_DISPLAY;
5484 else
5485# endif
5486# ifdef FEAT_XCLIPBOARD
5487 dpy = xterm_dpy;
5488# else
5489 return;
5490# endif
5491
5492 /* Get selection to export */
5493 if (clip_plus.owned)
5494 motion_type = clip_convert_selection(&str, &len, &clip_plus);
5495 else if (clip_star.owned)
5496 motion_type = clip_convert_selection(&str, &len, &clip_star);
5497
5498 /* Check it's OK */
5499 if (dpy != NULL && str != NULL && motion_type >= 0
5500 && len < 1024*1024 && len > 0)
5501 {
5502 XStoreBuffer(dpy, (char *)str, (int)len, 0);
5503 XFlush(dpy);
5504 }
5505
5506 vim_free(str);
5507}
5508#endif
5509
5510 void
5511clip_free_selection(cbd)
5512 VimClipboard *cbd;
5513{
5514 struct yankreg *y_ptr = y_current;
5515
5516 if (cbd == &clip_plus)
5517 y_current = &y_regs[PLUS_REGISTER];
5518 else
5519 y_current = &y_regs[STAR_REGISTER];
5520 free_yank_all();
5521 y_current->y_size = 0;
5522 y_current = y_ptr;
5523}
5524
5525/*
5526 * Get the selected text and put it in the gui selection register '*' or '+'.
5527 */
5528 void
5529clip_get_selection(cbd)
5530 VimClipboard *cbd;
5531{
5532 struct yankreg *old_y_previous, *old_y_current;
5533 pos_T old_cursor;
5534#ifdef FEAT_VISUAL
5535 pos_T old_visual;
5536 int old_visual_mode;
5537#endif
5538 colnr_T old_curswant;
5539 int old_set_curswant;
5540 pos_T old_op_start, old_op_end;
5541 oparg_T oa;
5542 cmdarg_T ca;
5543
5544 if (cbd->owned)
5545 {
5546 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL)
5547 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL))
5548 return;
5549
5550 /* Get the text between clip_star.start & clip_star.end */
5551 old_y_previous = y_previous;
5552 old_y_current = y_current;
5553 old_cursor = curwin->w_cursor;
5554 old_curswant = curwin->w_curswant;
5555 old_set_curswant = curwin->w_set_curswant;
5556 old_op_start = curbuf->b_op_start;
5557 old_op_end = curbuf->b_op_end;
5558#ifdef FEAT_VISUAL
5559 old_visual = VIsual;
5560 old_visual_mode = VIsual_mode;
5561#endif
5562 clear_oparg(&oa);
5563 oa.regname = (cbd == &clip_plus ? '+' : '*');
5564 oa.op_type = OP_YANK;
5565 vim_memset(&ca, 0, sizeof(ca));
5566 ca.oap = &oa;
5567 ca.cmdchar = 'y';
5568 ca.count1 = 1;
5569 ca.retval = CA_NO_ADJ_OP_END;
5570 do_pending_operator(&ca, 0, TRUE);
5571 y_previous = old_y_previous;
5572 y_current = old_y_current;
5573 curwin->w_cursor = old_cursor;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00005574 changed_cline_bef_curs(); /* need to update w_virtcol et al */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 curwin->w_curswant = old_curswant;
5576 curwin->w_set_curswant = old_set_curswant;
5577 curbuf->b_op_start = old_op_start;
5578 curbuf->b_op_end = old_op_end;
5579#ifdef FEAT_VISUAL
5580 VIsual = old_visual;
5581 VIsual_mode = old_visual_mode;
5582#endif
5583 }
5584 else
5585 {
5586 clip_free_selection(cbd);
5587
5588 /* Try to get selected text from another window */
5589 clip_gen_request_selection(cbd);
5590 }
5591}
5592
5593/* Convert from the GUI selection string into the '*'/'+' register */
5594 void
5595clip_yank_selection(type, str, len, cbd)
5596 int type;
5597 char_u *str;
5598 long len;
5599 VimClipboard *cbd;
5600{
5601 struct yankreg *y_ptr;
5602
5603 if (cbd == &clip_plus)
5604 y_ptr = &y_regs[PLUS_REGISTER];
5605 else
5606 y_ptr = &y_regs[STAR_REGISTER];
5607
5608 clip_free_selection(cbd);
5609
5610 str_to_reg(y_ptr, type, str, len, 0L);
5611}
5612
5613/*
5614 * Convert the '*'/'+' register into a GUI selection string returned in *str
5615 * with length *len.
5616 * Returns the motion type, or -1 for failure.
5617 */
5618 int
5619clip_convert_selection(str, len, cbd)
5620 char_u **str;
5621 long_u *len;
5622 VimClipboard *cbd;
5623{
5624 char_u *p;
5625 int lnum;
5626 int i, j;
5627 int_u eolsize;
5628 struct yankreg *y_ptr;
5629
5630 if (cbd == &clip_plus)
5631 y_ptr = &y_regs[PLUS_REGISTER];
5632 else
5633 y_ptr = &y_regs[STAR_REGISTER];
5634
5635#ifdef USE_CRNL
5636 eolsize = 2;
5637#else
5638 eolsize = 1;
5639#endif
5640
5641 *str = NULL;
5642 *len = 0;
5643 if (y_ptr->y_array == NULL)
5644 return -1;
5645
5646 for (i = 0; i < y_ptr->y_size; i++)
5647 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize;
5648
5649 /*
5650 * Don't want newline character at end of last line if we're in MCHAR mode.
5651 */
5652 if (y_ptr->y_type == MCHAR && *len >= eolsize)
5653 *len -= eolsize;
5654
5655 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */
5656 if (p == NULL)
5657 return -1;
5658 lnum = 0;
5659 for (i = 0, j = 0; i < (int)*len; i++, j++)
5660 {
5661 if (y_ptr->y_array[lnum][j] == '\n')
5662 p[i] = NUL;
5663 else if (y_ptr->y_array[lnum][j] == NUL)
5664 {
5665#ifdef USE_CRNL
5666 p[i++] = '\r';
5667#endif
5668#ifdef USE_CR
5669 p[i] = '\r';
5670#else
5671 p[i] = '\n';
5672#endif
5673 lnum++;
5674 j = -1;
5675 }
5676 else
5677 p[i] = y_ptr->y_array[lnum][j];
5678 }
5679 return y_ptr->y_type;
5680}
5681
5682
5683# if defined(FEAT_VISUAL) || defined(FEAT_EVAL)
5684/*
5685 * If we have written to a clipboard register, send the text to the clipboard.
5686 */
5687 static void
5688may_set_selection()
5689{
5690 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available)
5691 {
5692 clip_own_selection(&clip_star);
5693 clip_gen_set_selection(&clip_star);
5694 }
5695 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available)
5696 {
5697 clip_own_selection(&clip_plus);
5698 clip_gen_set_selection(&clip_plus);
5699 }
5700}
5701# endif
5702
5703#endif /* FEAT_CLIPBOARD || PROTO */
5704
5705
5706#if defined(FEAT_DND) || defined(PROTO)
5707/*
5708 * Replace the contents of the '~' register with str.
5709 */
5710 void
5711dnd_yank_drag_data(str, len)
5712 char_u *str;
5713 long len;
5714{
5715 struct yankreg *curr;
5716
5717 curr = y_current;
5718 y_current = &y_regs[TILDE_REGISTER];
5719 free_yank_all();
5720 str_to_reg(y_current, MCHAR, str, len, 0L);
5721 y_current = curr;
5722}
5723#endif
5724
5725
5726#if defined(FEAT_EVAL) || defined(PROTO)
5727/*
5728 * Return the type of a register.
5729 * Used for getregtype()
5730 * Returns MAUTO for error.
5731 */
5732 char_u
5733get_reg_type(regname, reglen)
5734 int regname;
5735 long *reglen;
5736{
5737 switch (regname)
5738 {
5739 case '%': /* file name */
5740 case '#': /* alternate file name */
5741 case '=': /* expression */
5742 case ':': /* last command line */
5743 case '/': /* last search-pattern */
5744 case '.': /* last inserted text */
5745#ifdef FEAT_SEARCHPATH
5746 case Ctrl_F: /* Filename under cursor */
5747 case Ctrl_P: /* Path under cursor, expand via "path" */
5748#endif
5749 case Ctrl_W: /* word under cursor */
5750 case Ctrl_A: /* WORD (mnemonic All) under cursor */
5751 case '_': /* black hole: always empty */
5752 return MCHAR;
5753 }
5754
5755#ifdef FEAT_CLIPBOARD
5756 regname = may_get_selection(regname);
5757#endif
5758
5759 /* Should we check for a valid name? */
5760 get_yank_register(regname, FALSE);
5761
5762 if (y_current->y_array != NULL)
5763 {
5764#ifdef FEAT_VISUAL
5765 if (reglen != NULL && y_current->y_type == MBLOCK)
5766 *reglen = y_current->y_width;
5767#endif
5768 return y_current->y_type;
5769 }
5770 return MAUTO;
5771}
5772
5773/*
5774 * Return the contents of a register as a single allocated string.
5775 * Used for "@r" in expressions and for getreg().
5776 * Returns NULL for error.
5777 */
5778 char_u *
Bram Moolenaarde934d72005-05-22 22:09:40 +00005779get_reg_contents(regname, allowexpr, expr_src)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 int regname;
Bram Moolenaarde934d72005-05-22 22:09:40 +00005781 int allowexpr; /* allow "=" register */
5782 int expr_src; /* get expression for "=" register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784 long i;
5785 char_u *retval;
5786 int allocated;
5787 long len;
5788
5789 /* Don't allow using an expression register inside an expression */
5790 if (regname == '=')
5791 {
5792 if (allowexpr)
Bram Moolenaarde934d72005-05-22 22:09:40 +00005793 {
5794 if (expr_src)
5795 return get_expr_line_src();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 return get_expr_line();
Bram Moolenaarde934d72005-05-22 22:09:40 +00005797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 return NULL;
5799 }
5800
5801 if (regname == '@') /* "@@" is used for unnamed register */
5802 regname = '"';
5803
5804 /* check for valid regname */
5805 if (regname != NUL && !valid_yank_reg(regname, FALSE))
5806 return NULL;
5807
5808#ifdef FEAT_CLIPBOARD
5809 regname = may_get_selection(regname);
5810#endif
5811
5812 if (get_spec_reg(regname, &retval, &allocated, FALSE))
5813 {
5814 if (retval == NULL)
5815 return NULL;
5816 if (!allocated)
5817 retval = vim_strsave(retval);
5818 return retval;
5819 }
5820
5821 get_yank_register(regname, FALSE);
5822 if (y_current->y_array == NULL)
5823 return NULL;
5824
5825 /*
5826 * Compute length of resulting string.
5827 */
5828 len = 0;
5829 for (i = 0; i < y_current->y_size; ++i)
5830 {
5831 len += (long)STRLEN(y_current->y_array[i]);
5832 /*
5833 * Insert a newline between lines and after last line if
5834 * y_type is MLINE.
5835 */
5836 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5837 ++len;
5838 }
5839
5840 retval = lalloc(len + 1, TRUE);
5841
5842 /*
5843 * Copy the lines of the yank register into the string.
5844 */
5845 if (retval != NULL)
5846 {
5847 len = 0;
5848 for (i = 0; i < y_current->y_size; ++i)
5849 {
5850 STRCPY(retval + len, y_current->y_array[i]);
5851 len += (long)STRLEN(retval + len);
5852
5853 /*
5854 * Insert a NL between lines and after the last line if y_type is
5855 * MLINE.
5856 */
5857 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
5858 retval[len++] = '\n';
5859 }
5860 retval[len] = NUL;
5861 }
5862
5863 return retval;
5864}
5865
5866/*
5867 * Store string "str" in register "name".
5868 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
5869 * If "must_append" is TRUE, always append to the register. Otherwise append
5870 * if "name" is an uppercase letter.
5871 * Note: "maxlen" and "must_append" don't work for the "/" register.
5872 * Careful: 'str' is modified, you may have to use a copy!
5873 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
5874 */
5875 void
5876write_reg_contents(name, str, maxlen, must_append)
5877 int name;
5878 char_u *str;
5879 int maxlen;
5880 int must_append;
5881{
5882 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
5883}
5884
5885 void
5886write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len)
5887 int name;
5888 char_u *str;
5889 int maxlen;
5890 int must_append;
5891 int yank_type;
5892 long block_len;
5893{
5894 struct yankreg *old_y_previous, *old_y_current;
5895 long len;
5896
Bram Moolenaare7566042005-06-17 22:00:15 +00005897 if (maxlen >= 0)
5898 len = maxlen;
5899 else
5900 len = (long)STRLEN(str);
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 /* Special case: '/' search pattern */
5903 if (name == '/')
5904 {
5905 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
5906 return;
5907 }
5908
Bram Moolenaare7566042005-06-17 22:00:15 +00005909#ifdef FEAT_EVAL
5910 if (name == '=')
5911 {
5912 char_u *p, *s;
5913
5914 p = vim_strnsave(str, (int)len);
5915 if (p == NULL)
5916 return;
5917 if (must_append)
5918 {
5919 s = concat_str(get_expr_line_src(), p);
5920 vim_free(p);
5921 p = s;
5922
5923 }
5924 set_expr_line(p);
5925 return;
5926 }
5927#endif
5928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */
5930 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005931 emsg_invreg(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 return;
5933 }
5934
5935 if (name == '_') /* black hole: nothing to do */
5936 return;
5937
5938 /* Don't want to change the current (unnamed) register */
5939 old_y_previous = y_previous;
5940 old_y_current = y_current;
5941
5942 get_yank_register(name, TRUE);
5943 if (!y_append && !must_append)
5944 free_yank_all();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945#ifndef FEAT_VISUAL
5946 /* Just in case - make sure we don't use MBLOCK */
5947 if (yank_type == MBLOCK)
5948 yank_type = MAUTO;
5949#endif
5950 if (yank_type == MAUTO)
5951 yank_type = ((len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r'))
5952 ? MLINE : MCHAR);
5953 str_to_reg(y_current, yank_type, str, len, block_len);
5954
5955# ifdef FEAT_CLIPBOARD
5956 /* Send text of clipboard register to the clipboard. */
5957 may_set_selection();
5958# endif
5959
5960 /* ':let @" = "val"' should change the meaning of the "" register */
5961 if (name != '"')
5962 y_previous = old_y_previous;
5963 y_current = old_y_current;
5964}
5965#endif /* FEAT_EVAL */
5966
5967#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
5968/*
5969 * Put a string into a register. When the register is not empty, the string
5970 * is appended.
5971 */
5972 static void
5973str_to_reg(y_ptr, type, str, len, blocklen)
5974 struct yankreg *y_ptr; /* pointer to yank register */
5975 int type; /* MCHAR, MLINE or MBLOCK */
5976 char_u *str; /* string to put in register */
5977 long len; /* length of string */
5978 long blocklen; /* width of Visual block */
5979{
5980 int lnum;
5981 long start;
5982 long i;
5983 int extra;
5984 int newlines; /* number of lines added */
5985 int extraline = 0; /* extra line at the end */
5986 int append = FALSE; /* append to last line in register */
5987 char_u *s;
5988 char_u **pp;
5989#ifdef FEAT_VISUAL
5990 long maxlen;
5991#endif
5992
5993 if (y_ptr->y_array == NULL) /* NULL means emtpy register */
5994 y_ptr->y_size = 0;
5995
5996 /*
5997 * Count the number of lines within the string
5998 */
5999 newlines = 0;
6000 for (i = 0; i < len; i++)
6001 if (str[i] == '\n')
6002 ++newlines;
6003 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
6004 {
6005 extraline = 1;
6006 ++newlines; /* count extra newline at the end */
6007 }
6008 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
6009 {
6010 append = TRUE;
6011 --newlines; /* uncount newline when appending first line */
6012 }
6013
6014 /*
6015 * Allocate an array to hold the pointers to the new register lines.
6016 * If the register was not empty, move the existing lines to the new array.
6017 */
6018 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines)
6019 * sizeof(char_u *), TRUE);
6020 if (pp == NULL) /* out of memory */
6021 return;
6022 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
6023 pp[lnum] = y_ptr->y_array[lnum];
6024 vim_free(y_ptr->y_array);
6025 y_ptr->y_array = pp;
6026#ifdef FEAT_VISUAL
6027 maxlen = 0;
6028#endif
6029
6030 /*
6031 * Find the end of each line and save it into the array.
6032 */
6033 for (start = 0; start < len + extraline; start += i + 1)
6034 {
6035 for (i = start; i < len; ++i) /* find the end of the line */
6036 if (str[i] == '\n')
6037 break;
6038 i -= start; /* i is now length of line */
6039#ifdef FEAT_VISUAL
6040 if (i > maxlen)
6041 maxlen = i;
6042#endif
6043 if (append)
6044 {
6045 --lnum;
6046 extra = (int)STRLEN(y_ptr->y_array[lnum]);
6047 }
6048 else
6049 extra = 0;
6050 s = alloc((unsigned)(i + extra + 1));
6051 if (s == NULL)
6052 break;
6053 if (extra)
6054 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
6055 if (append)
6056 vim_free(y_ptr->y_array[lnum]);
6057 if (i)
6058 mch_memmove(s + extra, str + start, (size_t)i);
6059 extra += i;
6060 s[extra] = NUL;
6061 y_ptr->y_array[lnum++] = s;
6062 while (--extra >= 0)
6063 {
6064 if (*s == NUL)
6065 *s = '\n'; /* replace NUL with newline */
6066 ++s;
6067 }
6068 append = FALSE; /* only first line is appended */
6069 }
6070 y_ptr->y_type = type;
6071 y_ptr->y_size = lnum;
6072# ifdef FEAT_VISUAL
6073 if (type == MBLOCK)
6074 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
6075 else
6076 y_ptr->y_width = 0;
6077# endif
6078}
6079#endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */
6080
6081 void
6082clear_oparg(oap)
6083 oparg_T *oap;
6084{
6085 vim_memset(oap, 0, sizeof(oparg_T));
6086}
6087
Bram Moolenaar7c626922005-02-07 22:01:03 +00006088static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089
6090/*
Bram Moolenaar7c626922005-02-07 22:01:03 +00006091 * Count the number of bytes, characters and "words" in a line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 *
6093 * "Words" are counted by looking for boundaries between non-space and
6094 * space characters. (it seems to produce results that match 'wc'.)
6095 *
Bram Moolenaar7c626922005-02-07 22:01:03 +00006096 * Return value is byte count; word count for the line is added to "*wc".
6097 * Char count is added to "*cc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 *
6099 * The function will only examine the first "limit" characters in the
6100 * line, stopping if it encounters an end-of-line (NUL byte). In that
6101 * case, eol_size will be added to the character count to account for
6102 * the size of the EOL character.
6103 */
6104 static long
Bram Moolenaar7c626922005-02-07 22:01:03 +00006105line_count_info(line, wc, cc, limit, eol_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 char_u *line;
6107 long *wc;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006108 long *cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 long limit;
6110 int eol_size;
6111{
Bram Moolenaar7c626922005-02-07 22:01:03 +00006112 long i;
6113 long words = 0;
6114 long chars = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 int is_word = 0;
6116
Bram Moolenaar7c626922005-02-07 22:01:03 +00006117 for (i = 0; line[i] && i < limit; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
6119 if (is_word)
6120 {
6121 if (vim_isspace(line[i]))
6122 {
6123 words++;
6124 is_word = 0;
6125 }
6126 }
6127 else if (!vim_isspace(line[i]))
6128 is_word = 1;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006129 ++chars;
6130#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006131 i += (*mb_ptr2len)(line + i);
Bram Moolenaar7c626922005-02-07 22:01:03 +00006132#else
6133 ++i;
6134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 }
6136
6137 if (is_word)
6138 words++;
6139 *wc += words;
6140
6141 /* Add eol_size if the end of line was reached before hitting limit. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006142 if (line[i] == NUL && i < limit)
6143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 i += eol_size;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006145 chars += eol_size;
6146 }
6147 *cc += chars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 return i;
6149}
6150
6151/*
6152 * Give some info about the position of the cursor (for "g CTRL-G").
6153 * In Visual mode, give some info about the selected region. (In this case,
6154 * the *_count_cursor variables store running totals for the selection.)
6155 */
6156 void
6157cursor_pos_info()
6158{
6159 char_u *p;
Bram Moolenaar555b2802005-05-19 21:08:39 +00006160 char_u buf1[50];
6161 char_u buf2[40];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 linenr_T lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006163 long byte_count = 0;
6164 long byte_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 long char_count = 0;
6166 long char_count_cursor = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 long word_count = 0;
6168 long word_count_cursor = 0;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006169 int eol_size;
6170 long last_check = 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171#ifdef FEAT_VISUAL
6172 long line_count_selected = 0;
6173 pos_T min_pos, max_pos;
6174 oparg_T oparg;
6175 struct block_def bd;
6176#endif
6177
6178 /*
6179 * Compute the length of the file in characters.
6180 */
6181 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6182 {
6183 MSG(_(no_lines_msg));
6184 }
6185 else
6186 {
6187 if (get_fileformat(curbuf) == EOL_DOS)
6188 eol_size = 2;
6189 else
6190 eol_size = 1;
6191
6192#ifdef FEAT_VISUAL
6193 if (VIsual_active)
6194 {
6195 if (lt(VIsual, curwin->w_cursor))
6196 {
6197 min_pos = VIsual;
6198 max_pos = curwin->w_cursor;
6199 }
6200 else
6201 {
6202 min_pos = curwin->w_cursor;
6203 max_pos = VIsual;
6204 }
6205 if (*p_sel == 'e' && max_pos.col > 0)
6206 --max_pos.col;
6207
6208 if (VIsual_mode == Ctrl_V)
6209 {
6210 oparg.is_VIsual = 1;
6211 oparg.block_mode = TRUE;
6212 oparg.op_type = OP_NOP;
6213 getvcols(curwin, &min_pos, &max_pos,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006214 &oparg.start_vcol, &oparg.end_vcol);
6215 if (curwin->w_curswant == MAXCOL)
6216 oparg.end_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 /* Swap the start, end vcol if needed */
6218 if (oparg.end_vcol < oparg.start_vcol)
6219 {
6220 oparg.end_vcol += oparg.start_vcol;
6221 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol;
6222 oparg.end_vcol -= oparg.start_vcol;
6223 }
6224 }
6225 line_count_selected = max_pos.lnum - min_pos.lnum + 1;
6226 }
6227#endif
6228
6229 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6230 {
6231 /* Check for a CTRL-C every 100000 characters. */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006232 if (byte_count > last_check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 ui_breakcheck();
6235 if (got_int)
6236 return;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006237 last_check = byte_count + 100000L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
6239
6240#ifdef FEAT_VISUAL
6241 /* Do extra processing for VIsual mode. */
6242 if (VIsual_active
6243 && lnum >= min_pos.lnum && lnum <= max_pos.lnum)
6244 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00006245 char_u *s = NULL;
6246 long len = 0L;
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 switch (VIsual_mode)
6249 {
6250 case Ctrl_V:
6251# ifdef FEAT_VIRTUALEDIT
6252 virtual_op = virtual_active();
6253# endif
6254 block_prep(&oparg, &bd, lnum, 0);
6255# ifdef FEAT_VIRTUALEDIT
6256 virtual_op = MAYBE;
6257# endif
Bram Moolenaardef9e822004-12-31 20:58:58 +00006258 s = bd.textstart;
6259 len = (long)bd.textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 break;
6261 case 'V':
Bram Moolenaardef9e822004-12-31 20:58:58 +00006262 s = ml_get(lnum);
6263 len = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 break;
6265 case 'v':
6266 {
6267 colnr_T start_col = (lnum == min_pos.lnum)
6268 ? min_pos.col : 0;
6269 colnr_T end_col = (lnum == max_pos.lnum)
6270 ? max_pos.col - start_col + 1 : MAXCOL;
6271
Bram Moolenaardef9e822004-12-31 20:58:58 +00006272 s = ml_get(lnum) + start_col;
6273 len = end_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 }
6275 break;
6276 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00006277 if (s != NULL)
6278 {
Bram Moolenaar7c626922005-02-07 22:01:03 +00006279 byte_count_cursor += line_count_info(s, &word_count_cursor,
6280 &char_count_cursor, len, eol_size);
Bram Moolenaardef9e822004-12-31 20:58:58 +00006281 if (lnum == curbuf->b_ml.ml_line_count
6282 && !curbuf->b_p_eol
6283 && curbuf->b_p_bin
Bram Moolenaarec2dad62005-01-02 11:36:03 +00006284 && (long)STRLEN(s) < len)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006285 byte_count_cursor -= eol_size;
Bram Moolenaardef9e822004-12-31 20:58:58 +00006286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
6288 else
6289#endif
6290 {
6291 /* In non-visual mode, check for the line the cursor is on */
6292 if (lnum == curwin->w_cursor.lnum)
6293 {
6294 word_count_cursor += word_count;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006295 char_count_cursor += char_count;
6296 byte_count_cursor = byte_count +
6297 line_count_info(ml_get(lnum),
6298 &word_count_cursor, &char_count_cursor,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 (long)(curwin->w_cursor.col + 1), eol_size);
6300 }
6301 }
6302 /* Add to the running totals */
Bram Moolenaar7c626922005-02-07 22:01:03 +00006303 byte_count += line_count_info(ml_get(lnum), &word_count,
6304 &char_count, (long)MAXCOL, eol_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306
6307 /* Correction for when last line doesn't have an EOL. */
6308 if (!curbuf->b_p_eol && curbuf->b_p_bin)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006309 byte_count -= eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
6311#ifdef FEAT_VISUAL
6312 if (VIsual_active)
6313 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006314 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
6316 getvcols(curwin, &min_pos, &max_pos, &min_pos.col,
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00006317 &max_pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 sprintf((char *)buf1, _("%ld Cols; "),
6319 (long)(oparg.end_vcol - oparg.start_vcol + 1));
6320 }
6321 else
6322 buf1[0] = NUL;
6323
Bram Moolenaar7c626922005-02-07 22:01:03 +00006324 if (char_count_cursor == byte_count_cursor
Bram Moolenaar555b2802005-05-19 21:08:39 +00006325 && char_count == byte_count)
Bram Moolenaar7c626922005-02-07 22:01:03 +00006326 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 buf1, line_count_selected,
6328 (long)curbuf->b_ml.ml_line_count,
6329 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006330 byte_count_cursor, byte_count);
6331 else
6332 sprintf((char *)IObuff, _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"),
6333 buf1, line_count_selected,
6334 (long)curbuf->b_ml.ml_line_count,
6335 word_count_cursor, word_count,
6336 char_count_cursor, char_count,
6337 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339 else
6340#endif
6341 {
6342 p = ml_get_curline();
6343 validate_virtcol();
6344 col_print(buf1, (int)curwin->w_cursor.col + 1,
6345 (int)curwin->w_virtcol + 1);
6346 col_print(buf2, (int)STRLEN(p), linetabsize(p));
6347
Bram Moolenaar7c626922005-02-07 22:01:03 +00006348 if (char_count_cursor == byte_count_cursor
6349 && char_count == byte_count)
6350 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 +00006351 (char *)buf1, (char *)buf2,
6352 (long)curwin->w_cursor.lnum,
6353 (long)curbuf->b_ml.ml_line_count,
6354 word_count_cursor, word_count,
Bram Moolenaar7c626922005-02-07 22:01:03 +00006355 byte_count_cursor, byte_count);
6356 else
6357 sprintf((char *)IObuff, _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"),
6358 (char *)buf1, (char *)buf2,
6359 (long)curwin->w_cursor.lnum,
6360 (long)curbuf->b_ml.ml_line_count,
6361 word_count_cursor, word_count,
6362 char_count_cursor, char_count,
6363 byte_count_cursor, byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 }
6365
6366#ifdef FEAT_MBYTE
Bram Moolenaar7c626922005-02-07 22:01:03 +00006367 byte_count = bomb_size();
6368 if (byte_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"),
Bram Moolenaar7c626922005-02-07 22:01:03 +00006370 byte_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371#endif
6372 /* Don't shorten this message, the user asked for it. */
6373 p = p_shm;
6374 p_shm = (char_u *)"";
6375 msg(IObuff);
6376 p_shm = p;
6377 }
6378}