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