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