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