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