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