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