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