Mercurial > vim
annotate src/ops.c @ 5901:10fc95f48546 v7.4.293
updated for version 7.4.293
Problem: It is not possible to ignore composing characters at a specific
point in a pattern.
Solution: Add the %C item.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Tue, 13 May 2014 19:37:29 +0200 |
parents | 88b0571de432 |
children | 6d984caa0409 |
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) | |
5828 | 116 static void str_to_reg __ARGS((struct yankreg *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list)); |
7 | 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) | |
5848 | 1982 (void)do_join(2, FALSE, 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. |
5848 | 4326 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment |
4327 * leaders should not be removed. | |
4328 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected | |
4329 * to set those marks. | |
7 | 4330 * |
1217 | 4331 * return FAIL for failure, OK otherwise |
7 | 4332 */ |
4333 int | |
5848 | 4334 do_join(count, insert_space, save_undo, use_formatoptions, setmark) |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4335 long count; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4336 int insert_space; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4337 int save_undo; |
3562 | 4338 int use_formatoptions UNUSED; |
5848 | 4339 int setmark; |
7 | 4340 { |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4341 char_u *curr = NULL; |
2597 | 4342 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
|
4343 char_u *cend; |
7 | 4344 char_u *newp; |
2597 | 4345 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
|
4346 int endcurr1 = NUL; |
3e4574a4b627
Fix a few compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2298
diff
changeset
|
4347 int endcurr2 = NUL; |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4348 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
|
4349 int sumsize = 0; /* size of the long new line */ |
7 | 4350 linenr_T t; |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4351 colnr_T col = 0; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4352 int ret = OK; |
3562 | 4353 #if defined(FEAT_COMMENTS) || defined(PROTO) |
3574 | 4354 int *comments = NULL; |
3562 | 4355 int remove_comments = (use_formatoptions == TRUE) |
4356 && has_format_option(FO_REMOVE_COMS); | |
4357 int prev_was_comment; | |
4358 #endif | |
4359 | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4360 |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4361 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
|
4362 (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
|
4363 return FAIL; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4364 |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4365 /* 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
|
4366 * 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
|
4367 * 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
|
4368 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
|
4369 if (spaces == NULL) |
7 | 4370 return FAIL; |
3562 | 4371 #if defined(FEAT_COMMENTS) || defined(PROTO) |
4372 if (remove_comments) | |
4373 { | |
4374 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE); | |
4375 if (comments == NULL) | |
4376 { | |
4377 vim_free(spaces); | |
4378 return FAIL; | |
4379 } | |
4380 } | |
4381 #endif | |
7 | 4382 |
4383 /* | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4384 * 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
|
4385 * and setup the array of space strings lengths |
7 | 4386 */ |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4387 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
|
4388 { |
2597 | 4389 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t)); |
5848 | 4390 if (t == 0 && setmark) |
5664 | 4391 { |
4392 /* Set the '[ mark. */ | |
4393 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum; | |
4394 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr); | |
4395 } | |
3562 | 4396 #if defined(FEAT_COMMENTS) || defined(PROTO) |
4397 if (remove_comments) | |
4398 { | |
4399 /* We don't want to remove the comment leader if the | |
4400 * previous line is not a comment. */ | |
4401 if (t > 0 && prev_was_comment) | |
4402 { | |
4403 | |
4404 char_u *new_curr = skip_comment(curr, TRUE, insert_space, | |
4405 &prev_was_comment); | |
3576 | 4406 comments[t] = (int)(new_curr - curr); |
3562 | 4407 curr = new_curr; |
4408 } | |
4409 else | |
4410 curr = skip_comment(curr, FALSE, insert_space, | |
4411 &prev_was_comment); | |
4412 } | |
4413 #endif | |
4414 | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4415 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
|
4416 { |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4417 curr = skipwhite(curr); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4418 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
|
4419 #ifdef FEAT_MBYTE |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4420 && (!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
|
4421 || (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
|
4422 && (!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
|
4423 || 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
|
4424 #endif |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4425 ) |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4426 { |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4427 /* 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
|
4428 if (endcurr1 == ' ') |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4429 endcurr1 = endcurr2; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4430 else |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4431 ++spaces[t]; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4432 /* 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
|
4433 if ( p_js |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4434 && (endcurr1 == '.' |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4435 || (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
|
4436 && (endcurr1 == '?' || endcurr1 == '!')))) |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4437 ++spaces[t]; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4438 } |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4439 } |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4440 currsize = (int)STRLEN(curr); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4441 sumsize += currsize + spaces[t]; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4442 endcurr1 = endcurr2 = NUL; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4443 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
|
4444 { |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4445 #ifdef FEAT_MBYTE |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4446 if (has_mbyte) |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4447 { |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4448 cend = curr + currsize; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4449 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
|
4450 endcurr1 = (*mb_ptr2char)(cend); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4451 if (cend > curr) |
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 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
|
4454 endcurr2 = (*mb_ptr2char)(cend); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4455 } |
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 else |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4458 #endif |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4459 { |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4460 endcurr1 = *(curr + currsize - 1); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4461 if (currsize > 1) |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4462 endcurr2 = *(curr + currsize - 2); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4463 } |
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 line_breakcheck(); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4466 if (got_int) |
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 ret = FAIL; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4469 goto theend; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4470 } |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4471 } |
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 /* 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
|
4474 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
|
4475 |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4476 /* 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
|
4477 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
|
4478 cend = newp + sumsize; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4479 *cend = 0; |
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 /* |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4482 * 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
|
4483 * |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4484 * 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
|
4485 * 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
|
4486 * 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
|
4487 */ |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4488 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
|
4489 { |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4490 cend -= currsize; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4491 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
|
4492 if (spaces[t] > 0) |
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 cend -= spaces[t]; |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4495 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
|
4496 } |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4497 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t, |
2597 | 4498 (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
|
4499 if (t == 0) |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4500 break; |
2597 | 4501 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1)); |
3562 | 4502 #if defined(FEAT_COMMENTS) || defined(PROTO) |
4503 if (remove_comments) | |
4504 curr += comments[t - 1]; | |
4505 #endif | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4506 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
|
4507 curr = skipwhite(curr); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4508 currsize = (int)STRLEN(curr); |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4509 } |
7 | 4510 ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
4511 | |
5848 | 4512 if (setmark) |
4513 { | |
4514 /* Set the '] mark. */ | |
4515 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum; | |
4516 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp); | |
4517 } | |
5664 | 4518 |
7 | 4519 /* Only report the change in the first line here, del_lines() will report |
4520 * the deleted line. */ | |
4521 changed_lines(curwin->w_cursor.lnum, currsize, | |
4522 curwin->w_cursor.lnum + 1, 0L); | |
4523 | |
4524 /* | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4525 * Delete following lines. To do this we move the cursor there |
7 | 4526 * briefly, and then move it back. After del_lines() the cursor may |
4527 * have moved up (last line deleted), so the current lnum is kept in t. | |
4528 */ | |
4529 t = curwin->w_cursor.lnum; | |
4530 ++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
|
4531 del_lines(count - 1, FALSE); |
7 | 4532 curwin->w_cursor.lnum = t; |
4533 | |
4534 /* | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4535 * Set the cursor column: |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4536 * 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
|
4537 * vim: use the column of the last join |
7 | 4538 */ |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4539 curwin->w_cursor.col = |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4540 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col); |
7 | 4541 check_cursor_col(); |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4542 |
7 | 4543 #ifdef FEAT_VIRTUALEDIT |
4544 curwin->w_cursor.coladd = 0; | |
4545 #endif | |
4546 curwin->w_set_curswant = TRUE; | |
4547 | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4548 theend: |
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4549 vim_free(spaces); |
3562 | 4550 #if defined(FEAT_COMMENTS) || defined(PROTO) |
4551 if (remove_comments) | |
4552 vim_free(comments); | |
4553 #endif | |
2294
2209060c340d
Make joining a range of lines much faster. (Milan Vancura)
Bram Moolenaar <bram@vim.org>
parents:
2289
diff
changeset
|
4554 return ret; |
7 | 4555 } |
4556 | |
4557 #ifdef FEAT_COMMENTS | |
4558 /* | |
4559 * Return TRUE if the two comment leaders given are the same. "lnum" is | |
4560 * the first line. White-space is ignored. Note that the whole of | |
4561 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb | |
4562 */ | |
4563 static int | |
4564 same_leader(lnum, leader1_len, leader1_flags, leader2_len, leader2_flags) | |
4565 linenr_T lnum; | |
4566 int leader1_len; | |
4567 char_u *leader1_flags; | |
4568 int leader2_len; | |
4569 char_u *leader2_flags; | |
4570 { | |
4571 int idx1 = 0, idx2 = 0; | |
4572 char_u *p; | |
4573 char_u *line1; | |
4574 char_u *line2; | |
4575 | |
4576 if (leader1_len == 0) | |
4577 return (leader2_len == 0); | |
4578 | |
4579 /* | |
4580 * If first leader has 'f' flag, the lines can be joined only if the | |
4581 * second line does not have a leader. | |
4582 * If first leader has 'e' flag, the lines can never be joined. | |
4583 * If fist leader has 's' flag, the lines can only be joined if there is | |
4584 * some text after it and the second line has the 'm' flag. | |
4585 */ | |
4586 if (leader1_flags != NULL) | |
4587 { | |
4588 for (p = leader1_flags; *p && *p != ':'; ++p) | |
4589 { | |
4590 if (*p == COM_FIRST) | |
4591 return (leader2_len == 0); | |
4592 if (*p == COM_END) | |
4593 return FALSE; | |
4594 if (*p == COM_START) | |
4595 { | |
4596 if (*(ml_get(lnum) + leader1_len) == NUL) | |
4597 return FALSE; | |
4598 if (leader2_flags == NULL || leader2_len == 0) | |
4599 return FALSE; | |
4600 for (p = leader2_flags; *p && *p != ':'; ++p) | |
4601 if (*p == COM_MIDDLE) | |
4602 return TRUE; | |
4603 return FALSE; | |
4604 } | |
4605 } | |
4606 } | |
4607 | |
4608 /* | |
4609 * Get current line and next line, compare the leaders. | |
4610 * The first line has to be saved, only one line can be locked at a time. | |
4611 */ | |
4612 line1 = vim_strsave(ml_get(lnum)); | |
4613 if (line1 != NULL) | |
4614 { | |
4615 for (idx1 = 0; vim_iswhite(line1[idx1]); ++idx1) | |
4616 ; | |
4617 line2 = ml_get(lnum + 1); | |
4618 for (idx2 = 0; idx2 < leader2_len; ++idx2) | |
4619 { | |
4620 if (!vim_iswhite(line2[idx2])) | |
4621 { | |
4622 if (line1[idx1++] != line2[idx2]) | |
4623 break; | |
4624 } | |
4625 else | |
4626 while (vim_iswhite(line1[idx1])) | |
4627 ++idx1; | |
4628 } | |
4629 vim_free(line1); | |
4630 } | |
4631 return (idx2 == leader2_len && idx1 == leader1_len); | |
4632 } | |
4633 #endif | |
4634 | |
4635 /* | |
3252 | 4636 * Implementation of the format operator 'gq'. |
7 | 4637 */ |
4638 void | |
4639 op_format(oap, keep_cursor) | |
4640 oparg_T *oap; | |
4641 int keep_cursor; /* keep cursor on same text char */ | |
4642 { | |
4643 long old_line_count = curbuf->b_ml.ml_line_count; | |
4644 | |
4645 /* Place the cursor where the "gq" or "gw" command was given, so that "u" | |
4646 * can put it back there. */ | |
4647 curwin->w_cursor = oap->cursor_start; | |
4648 | |
4649 if (u_save((linenr_T)(oap->start.lnum - 1), | |
4650 (linenr_T)(oap->end.lnum + 1)) == FAIL) | |
4651 return; | |
4652 curwin->w_cursor = oap->start; | |
4653 | |
4654 if (oap->is_VIsual) | |
4655 /* When there is no change: need to remove the Visual selection */ | |
4656 redraw_curbuf_later(INVERTED); | |
4657 | |
4658 /* Set '[ mark at the start of the formatted area */ | |
4659 curbuf->b_op_start = oap->start; | |
4660 | |
4661 /* For "gw" remember the cursor position and put it back below (adjusted | |
4662 * for joined and split lines). */ | |
4663 if (keep_cursor) | |
4664 saved_cursor = oap->cursor_start; | |
4665 | |
1563 | 4666 format_lines(oap->line_count, keep_cursor); |
7 | 4667 |
4668 /* | |
4669 * Leave the cursor at the first non-blank of the last formatted line. | |
4670 * If the cursor was moved one line back (e.g. with "Q}") go to the next | |
4671 * line, so "." will do the next lines. | |
4672 */ | |
4673 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) | |
4674 ++curwin->w_cursor.lnum; | |
4675 beginline(BL_WHITE | BL_FIX); | |
4676 old_line_count = curbuf->b_ml.ml_line_count - old_line_count; | |
4677 msgmore(old_line_count); | |
4678 | |
4679 /* put '] mark on the end of the formatted area */ | |
4680 curbuf->b_op_end = curwin->w_cursor; | |
4681 | |
4682 if (keep_cursor) | |
4683 { | |
4684 curwin->w_cursor = saved_cursor; | |
4685 saved_cursor.lnum = 0; | |
4686 } | |
4687 | |
4688 if (oap->is_VIsual) | |
4689 { | |
4690 win_T *wp; | |
4691 | |
4692 FOR_ALL_WINDOWS(wp) | |
4693 { | |
4694 if (wp->w_old_cursor_lnum != 0) | |
4695 { | |
4696 /* When lines have been inserted or deleted, adjust the end of | |
4697 * the Visual area to be redrawn. */ | |
4698 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum) | |
4699 wp->w_old_cursor_lnum += old_line_count; | |
4700 else | |
4701 wp->w_old_visual_lnum += old_line_count; | |
4702 } | |
4703 } | |
4704 } | |
4705 } | |
4706 | |
667 | 4707 #if defined(FEAT_EVAL) || defined(PROTO) |
4708 /* | |
4709 * Implementation of the format operator 'gq' for when using 'formatexpr'. | |
4710 */ | |
4711 void | |
4712 op_formatexpr(oap) | |
4713 oparg_T *oap; | |
4714 { | |
4715 if (oap->is_VIsual) | |
4716 /* When there is no change: need to remove the Visual selection */ | |
4717 redraw_curbuf_later(INVERTED); | |
4718 | |
2298
a3562a127cf6
When 'formatexpr' evaluates to non-zero fall back to internal formatting, also
Bram Moolenaar <bram@vim.org>
parents:
2294
diff
changeset
|
4719 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
|
4720 /* 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
|
4721 * internal formatting. */ |
a3562a127cf6
When 'formatexpr' evaluates to non-zero fall back to internal formatting, also
Bram Moolenaar <bram@vim.org>
parents:
2294
diff
changeset
|
4722 op_format(oap, FALSE); |
667 | 4723 } |
4724 | |
4725 int | |
844 | 4726 fex_format(lnum, count, c) |
667 | 4727 linenr_T lnum; |
4728 long count; | |
844 | 4729 int c; /* character to be inserted */ |
667 | 4730 { |
681 | 4731 int use_sandbox = was_set_insecurely((char_u *)"formatexpr", |
4732 OPT_LOCAL); | |
667 | 4733 int r; |
4734 | |
4735 /* | |
4736 * Set v:lnum to the first line number and v:count to the number of lines. | |
844 | 4737 * Set v:char to the character to be inserted (can be NUL). |
667 | 4738 */ |
4739 set_vim_var_nr(VV_LNUM, lnum); | |
4740 set_vim_var_nr(VV_COUNT, count); | |
1969 | 4741 set_vim_var_char(c); |
844 | 4742 |
667 | 4743 /* |
4744 * Evaluate the function. | |
4745 */ | |
4746 if (use_sandbox) | |
4747 ++sandbox; | |
4748 r = eval_to_number(curbuf->b_p_fex); | |
4749 if (use_sandbox) | |
4750 --sandbox; | |
844 | 4751 |
4752 set_vim_var_string(VV_CHAR, NULL, -1); | |
4753 | |
667 | 4754 return r; |
4755 } | |
4756 #endif | |
4757 | |
7 | 4758 /* |
4759 * Format "line_count" lines, starting at the cursor position. | |
4760 * When "line_count" is negative, format until the end of the paragraph. | |
4761 * Lines after the cursor line are saved for undo, caller must have saved the | |
4762 * first line. | |
4763 */ | |
4764 void | |
1563 | 4765 format_lines(line_count, avoid_fex) |
7 | 4766 linenr_T line_count; |
1563 | 4767 int avoid_fex; /* don't use 'formatexpr' */ |
7 | 4768 { |
4769 int max_len; | |
4770 int is_not_par; /* current line not part of parag. */ | |
4771 int next_is_not_par; /* next line not part of paragraph */ | |
4772 int is_end_par; /* at end of paragraph */ | |
4773 int prev_is_end_par = FALSE;/* prev. line not part of parag. */ | |
4774 int next_is_start_par = FALSE; | |
4775 #ifdef FEAT_COMMENTS | |
4776 int leader_len = 0; /* leader len of current line */ | |
4777 int next_leader_len; /* leader len of next line */ | |
4778 char_u *leader_flags = NULL; /* flags for leader of current line */ | |
4779 char_u *next_leader_flags; /* flags for leader of next line */ | |
4780 int do_comments; /* format comments */ | |
3584 | 4781 int do_comments_list = 0; /* format comments with 'n' or '2' */ |
7 | 4782 #endif |
4783 int advance = TRUE; | |
3584 | 4784 int second_indent = -1; /* indent for second line (comment |
4785 * aware) */ | |
7 | 4786 int do_second_indent; |
4787 int do_number_indent; | |
4788 int do_trail_white; | |
4789 int first_par_line = TRUE; | |
4790 int smd_save; | |
4791 long count; | |
4792 int need_set_indent = TRUE; /* set indent of next paragraph */ | |
4793 int force_format = FALSE; | |
4794 int old_State = State; | |
4795 | |
4796 /* length of a line to force formatting: 3 * 'tw' */ | |
4797 max_len = comp_textwidth(TRUE) * 3; | |
4798 | |
4799 /* check for 'q', '2' and '1' in 'formatoptions' */ | |
4800 #ifdef FEAT_COMMENTS | |
4801 do_comments = has_format_option(FO_Q_COMS); | |
4802 #endif | |
4803 do_second_indent = has_format_option(FO_Q_SECOND); | |
4804 do_number_indent = has_format_option(FO_Q_NUMBER); | |
4805 do_trail_white = has_format_option(FO_WHITE_PAR); | |
4806 | |
4807 /* | |
4808 * Get info about the previous and current line. | |
4809 */ | |
4810 if (curwin->w_cursor.lnum > 1) | |
4811 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1 | |
4812 #ifdef FEAT_COMMENTS | |
4813 , &leader_len, &leader_flags, do_comments | |
4814 #endif | |
4815 ); | |
4816 else | |
4817 is_not_par = TRUE; | |
4818 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum | |
4819 #ifdef FEAT_COMMENTS | |
4820 , &next_leader_len, &next_leader_flags, do_comments | |
4821 #endif | |
4822 ); | |
4823 is_end_par = (is_not_par || next_is_not_par); | |
4824 if (!is_end_par && do_trail_white) | |
4825 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1); | |
4826 | |
4827 curwin->w_cursor.lnum--; | |
4828 for (count = line_count; count != 0 && !got_int; --count) | |
4829 { | |
4830 /* | |
4831 * Advance to next paragraph. | |
4832 */ | |
4833 if (advance) | |
4834 { | |
4835 curwin->w_cursor.lnum++; | |
4836 prev_is_end_par = is_end_par; | |
4837 is_not_par = next_is_not_par; | |
4838 #ifdef FEAT_COMMENTS | |
4839 leader_len = next_leader_len; | |
4840 leader_flags = next_leader_flags; | |
4841 #endif | |
4842 } | |
4843 | |
4844 /* | |
4845 * The last line to be formatted. | |
4846 */ | |
4847 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) | |
4848 { | |
4849 next_is_not_par = TRUE; | |
4850 #ifdef FEAT_COMMENTS | |
4851 next_leader_len = 0; | |
4852 next_leader_flags = NULL; | |
4853 #endif | |
4854 } | |
4855 else | |
4856 { | |
4857 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1 | |
4858 #ifdef FEAT_COMMENTS | |
4859 , &next_leader_len, &next_leader_flags, do_comments | |
4860 #endif | |
4861 ); | |
4862 if (do_number_indent) | |
4863 next_is_start_par = | |
4864 (get_number_indent(curwin->w_cursor.lnum + 1) > 0); | |
4865 } | |
4866 advance = TRUE; | |
4867 is_end_par = (is_not_par || next_is_not_par || next_is_start_par); | |
4868 if (!is_end_par && do_trail_white) | |
4869 is_end_par = !ends_in_white(curwin->w_cursor.lnum); | |
4870 | |
4871 /* | |
4872 * Skip lines that are not in a paragraph. | |
4873 */ | |
4874 if (is_not_par) | |
4875 { | |
4876 if (line_count < 0) | |
4877 break; | |
4878 } | |
4879 else | |
4880 { | |
4881 /* | |
4882 * For the first line of a paragraph, check indent of second line. | |
4883 * Don't do this for comments and empty lines. | |
4884 */ | |
4885 if (first_par_line | |
4886 && (do_second_indent || do_number_indent) | |
4887 && prev_is_end_par | |
3584 | 4888 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
4889 { | |
4890 if (do_second_indent && !lineempty(curwin->w_cursor.lnum + 1)) | |
4891 { | |
4892 #ifdef FEAT_COMMENTS | |
4893 if (leader_len == 0 && next_leader_len == 0) | |
4894 { | |
4895 /* no comment found */ | |
4896 #endif | |
4897 second_indent = | |
4898 get_indent_lnum(curwin->w_cursor.lnum + 1); | |
7 | 4899 #ifdef FEAT_COMMENTS |
3584 | 4900 } |
4901 else | |
4902 { | |
4903 second_indent = next_leader_len; | |
4904 do_comments_list = 1; | |
4905 } | |
4906 #endif | |
4907 } | |
7 | 4908 else if (do_number_indent) |
3584 | 4909 { |
4910 #ifdef FEAT_COMMENTS | |
4911 if (leader_len == 0 && next_leader_len == 0) | |
4912 { | |
4913 /* no comment found */ | |
4914 #endif | |
4915 second_indent = | |
4916 get_number_indent(curwin->w_cursor.lnum); | |
4917 #ifdef FEAT_COMMENTS | |
4918 } | |
4919 else | |
4920 { | |
4921 /* get_number_indent() is now "comment aware"... */ | |
4922 second_indent = | |
4923 get_number_indent(curwin->w_cursor.lnum); | |
4924 do_comments_list = 1; | |
4925 } | |
4926 #endif | |
4927 } | |
7 | 4928 } |
4929 | |
4930 /* | |
4931 * When the comment leader changes, it's the end of the paragraph. | |
4932 */ | |
4933 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count | |
4934 #ifdef FEAT_COMMENTS | |
4935 || !same_leader(curwin->w_cursor.lnum, | |
4936 leader_len, leader_flags, | |
4937 next_leader_len, next_leader_flags) | |
4938 #endif | |
4939 ) | |
4940 is_end_par = TRUE; | |
4941 | |
4942 /* | |
4943 * If we have got to the end of a paragraph, or the line is | |
4944 * getting long, format it. | |
4945 */ | |
4946 if (is_end_par || force_format) | |
4947 { | |
4948 if (need_set_indent) | |
4949 /* replace indent in first line with minimal number of | |
4950 * tabs and spaces, according to current options */ | |
4951 (void)set_indent(get_indent(), SIN_CHANGED); | |
4952 | |
4953 /* put cursor on last non-space */ | |
4954 State = NORMAL; /* don't go past end-of-line */ | |
4955 coladvance((colnr_T)MAXCOL); | |
4956 while (curwin->w_cursor.col && vim_isspace(gchar_cursor())) | |
4957 dec_cursor(); | |
4958 | |
4959 /* do the formatting, without 'showmode' */ | |
4960 State = INSERT; /* for open_line() */ | |
4961 smd_save = p_smd; | |
4962 p_smd = FALSE; | |
4963 insertchar(NUL, INSCHAR_FORMAT | |
4964 #ifdef FEAT_COMMENTS | |
4965 + (do_comments ? INSCHAR_DO_COM : 0) | |
3584 | 4966 + (do_comments && do_comments_list |
4967 ? INSCHAR_COM_LIST : 0) | |
7 | 4968 #endif |
1563 | 4969 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent); |
7 | 4970 State = old_State; |
4971 p_smd = smd_save; | |
4972 second_indent = -1; | |
4973 /* at end of par.: need to set indent of next par. */ | |
4974 need_set_indent = is_end_par; | |
4975 if (is_end_par) | |
4976 { | |
4977 /* When called with a negative line count, break at the | |
4978 * end of the paragraph. */ | |
4979 if (line_count < 0) | |
4980 break; | |
4981 first_par_line = TRUE; | |
4982 } | |
4983 force_format = FALSE; | |
4984 } | |
4985 | |
4986 /* | |
4987 * When still in same paragraph, join the lines together. But | |
5403 | 4988 * first delete the leader from the second line. |
7 | 4989 */ |
4990 if (!is_end_par) | |
4991 { | |
4992 advance = FALSE; | |
4993 curwin->w_cursor.lnum++; | |
4994 curwin->w_cursor.col = 0; | |
4995 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
|
4996 break; |
7 | 4997 #ifdef FEAT_COMMENTS |
4998 if (next_leader_len > 0) | |
5403 | 4999 { |
5000 (void)del_bytes((long)next_leader_len, FALSE, FALSE); | |
7 | 5001 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, |
5002 (long)-next_leader_len); | |
5403 | 5003 } else |
5004 #endif | |
5005 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */ | |
5006 { | |
5007 char_u *p = ml_get_curline(); | |
5415 | 5008 int indent = (int)(skipwhite(p) - p); |
5403 | 5009 |
5010 if (indent > 0) | |
5011 { | |
5012 (void)del_bytes(indent, FALSE, FALSE); | |
5013 mark_col_adjust(curwin->w_cursor.lnum, | |
5014 (colnr_T)0, 0L, (long)-indent); | |
5415 | 5015 } |
5403 | 5016 } |
7 | 5017 curwin->w_cursor.lnum--; |
5848 | 5018 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL) |
7 | 5019 { |
5020 beep_flush(); | |
5021 break; | |
5022 } | |
5023 first_par_line = FALSE; | |
5024 /* If the line is getting long, format it next time */ | |
5025 if (STRLEN(ml_get_curline()) > (size_t)max_len) | |
5026 force_format = TRUE; | |
5027 else | |
5028 force_format = FALSE; | |
5029 } | |
5030 } | |
5031 line_breakcheck(); | |
5032 } | |
5033 } | |
5034 | |
5035 /* | |
5036 * Return TRUE if line "lnum" ends in a white character. | |
5037 */ | |
5038 static int | |
5039 ends_in_white(lnum) | |
5040 linenr_T lnum; | |
5041 { | |
5042 char_u *s = ml_get(lnum); | |
5043 size_t l; | |
5044 | |
5045 if (*s == NUL) | |
5046 return FALSE; | |
5047 /* Don't use STRLEN() inside vim_iswhite(), SAS/C complains: "macro | |
5048 * invocation may call function multiple times". */ | |
5049 l = STRLEN(s) - 1; | |
5050 return vim_iswhite(s[l]); | |
5051 } | |
5052 | |
5053 /* | |
5054 * Blank lines, and lines containing only the comment leader, are left | |
5055 * untouched by the formatting. The function returns TRUE in this | |
5056 * case. It also returns TRUE when a line starts with the end of a comment | |
5057 * ('e' in comment flags), so that this line is skipped, and not joined to the | |
5058 * previous line. A new paragraph starts after a blank line, or when the | |
5059 * comment leader changes -- webb. | |
5060 */ | |
5061 #ifdef FEAT_COMMENTS | |
5062 static int | |
5063 fmt_check_par(lnum, leader_len, leader_flags, do_comments) | |
5064 linenr_T lnum; | |
5065 int *leader_len; | |
5066 char_u **leader_flags; | |
5067 int do_comments; | |
5068 { | |
5069 char_u *flags = NULL; /* init for GCC */ | |
5070 char_u *ptr; | |
5071 | |
5072 ptr = ml_get(lnum); | |
5073 if (do_comments) | |
3562 | 5074 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE); |
7 | 5075 else |
5076 *leader_len = 0; | |
5077 | |
5078 if (*leader_len > 0) | |
5079 { | |
5080 /* | |
5081 * Search for 'e' flag in comment leader flags. | |
5082 */ | |
5083 flags = *leader_flags; | |
5084 while (*flags && *flags != ':' && *flags != COM_END) | |
5085 ++flags; | |
5086 } | |
5087 | |
5088 return (*skipwhite(ptr + *leader_len) == NUL | |
5089 || (*leader_len > 0 && *flags == COM_END) | |
5090 || startPS(lnum, NUL, FALSE)); | |
5091 } | |
5092 #else | |
5093 static int | |
5094 fmt_check_par(lnum) | |
5095 linenr_T lnum; | |
5096 { | |
5097 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE)); | |
5098 } | |
5099 #endif | |
5100 | |
5101 /* | |
5102 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the | |
5103 * previous line is in the same paragraph. Used for auto-formatting. | |
5104 */ | |
5105 int | |
5106 paragraph_start(lnum) | |
5107 linenr_T lnum; | |
5108 { | |
5109 char_u *p; | |
5110 #ifdef FEAT_COMMENTS | |
5111 int leader_len = 0; /* leader len of current line */ | |
5112 char_u *leader_flags = NULL; /* flags for leader of current line */ | |
5113 int next_leader_len; /* leader len of next line */ | |
5114 char_u *next_leader_flags; /* flags for leader of next line */ | |
5115 int do_comments; /* format comments */ | |
5116 #endif | |
5117 | |
5118 if (lnum <= 1) | |
5119 return TRUE; /* start of the file */ | |
5120 | |
5121 p = ml_get(lnum - 1); | |
5122 if (*p == NUL) | |
5123 return TRUE; /* after empty line */ | |
5124 | |
5125 #ifdef FEAT_COMMENTS | |
5126 do_comments = has_format_option(FO_Q_COMS); | |
5127 #endif | |
5128 if (fmt_check_par(lnum - 1 | |
5129 #ifdef FEAT_COMMENTS | |
5130 , &leader_len, &leader_flags, do_comments | |
5131 #endif | |
5132 )) | |
5133 return TRUE; /* after non-paragraph line */ | |
5134 | |
5135 if (fmt_check_par(lnum | |
5136 #ifdef FEAT_COMMENTS | |
5137 , &next_leader_len, &next_leader_flags, do_comments | |
5138 #endif | |
5139 )) | |
5140 return TRUE; /* "lnum" is not a paragraph line */ | |
5141 | |
5142 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) | |
5143 return TRUE; /* missing trailing space in previous line. */ | |
5144 | |
5145 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) | |
5146 return TRUE; /* numbered item starts in "lnum". */ | |
5147 | |
5148 #ifdef FEAT_COMMENTS | |
5149 if (!same_leader(lnum - 1, leader_len, leader_flags, | |
5150 next_leader_len, next_leader_flags)) | |
5151 return TRUE; /* change of comment leader. */ | |
5152 #endif | |
5153 | |
5154 return FALSE; | |
5155 } | |
5156 | |
5157 /* | |
5158 * prepare a few things for block mode yank/delete/tilde | |
5159 * | |
5160 * for delete: | |
5161 * - textlen includes the first/last char to be (partly) deleted | |
5162 * - start/endspaces is the number of columns that are taken by the | |
5163 * first/last deleted char minus the number of columns that have to be | |
1839 | 5164 * deleted. |
5165 * for yank and tilde: | |
7 | 5166 * - textlen includes the first/last char to be wholly yanked |
5167 * - start/endspaces is the number of columns of the first/last yanked char | |
5168 * that are to be yanked. | |
5169 */ | |
5170 static void | |
5171 block_prep(oap, bdp, lnum, is_del) | |
5172 oparg_T *oap; | |
5173 struct block_def *bdp; | |
5174 linenr_T lnum; | |
5175 int is_del; | |
5176 { | |
5177 int incr = 0; | |
5178 char_u *pend; | |
5179 char_u *pstart; | |
5180 char_u *line; | |
5181 char_u *prev_pstart; | |
5182 char_u *prev_pend; | |
5183 | |
5184 bdp->startspaces = 0; | |
5185 bdp->endspaces = 0; | |
5186 bdp->textlen = 0; | |
5187 bdp->start_vcol = 0; | |
5188 bdp->end_vcol = 0; | |
5189 #ifdef FEAT_VISUALEXTRA | |
5190 bdp->is_short = FALSE; | |
5191 bdp->is_oneChar = FALSE; | |
5192 bdp->pre_whitesp = 0; | |
5193 bdp->pre_whitesp_c = 0; | |
5194 bdp->end_char_vcols = 0; | |
5195 #endif | |
5196 bdp->start_char_vcols = 0; | |
5197 | |
5198 line = ml_get(lnum); | |
5199 pstart = line; | |
5200 prev_pstart = line; | |
5201 while (bdp->start_vcol < oap->start_vcol && *pstart) | |
5202 { | |
5203 /* Count a tab for what it's worth (if list mode not on) */ | |
5204 incr = lbr_chartabsize(pstart, (colnr_T)bdp->start_vcol); | |
5205 bdp->start_vcol += incr; | |
5206 #ifdef FEAT_VISUALEXTRA | |
5207 if (vim_iswhite(*pstart)) | |
5208 { | |
5209 bdp->pre_whitesp += incr; | |
5210 bdp->pre_whitesp_c++; | |
5211 } | |
5212 else | |
5213 { | |
5214 bdp->pre_whitesp = 0; | |
5215 bdp->pre_whitesp_c = 0; | |
5216 } | |
5217 #endif | |
5218 prev_pstart = pstart; | |
39 | 5219 mb_ptr_adv(pstart); |
7 | 5220 } |
5221 bdp->start_char_vcols = incr; | |
5222 if (bdp->start_vcol < oap->start_vcol) /* line too short */ | |
5223 { | |
5224 bdp->end_vcol = bdp->start_vcol; | |
5225 #ifdef FEAT_VISUALEXTRA | |
5226 bdp->is_short = TRUE; | |
5227 #endif | |
5228 if (!is_del || oap->op_type == OP_APPEND) | |
5229 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1; | |
5230 } | |
5231 else | |
5232 { | |
5233 /* notice: this converts partly selected Multibyte characters to | |
5234 * spaces, too. */ | |
5235 bdp->startspaces = bdp->start_vcol - oap->start_vcol; | |
5236 if (is_del && bdp->startspaces) | |
5237 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces; | |
5238 pend = pstart; | |
5239 bdp->end_vcol = bdp->start_vcol; | |
5240 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */ | |
5241 { | |
5242 #ifdef FEAT_VISUALEXTRA | |
5243 bdp->is_oneChar = TRUE; | |
5244 #endif | |
5245 if (oap->op_type == OP_INSERT) | |
5246 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; | |
5247 else if (oap->op_type == OP_APPEND) | |
5248 { | |
5249 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1; | |
5250 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; | |
5251 } | |
5252 else | |
5253 { | |
5254 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1; | |
5255 if (is_del && oap->op_type != OP_LSHIFT) | |
5256 { | |
5257 /* just putting the sum of those two into | |
5258 * bdp->startspaces doesn't work for Visual replace, | |
5259 * so we have to split the tab in two */ | |
5260 bdp->startspaces = bdp->start_char_vcols | |
5261 - (bdp->start_vcol - oap->start_vcol); | |
5262 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; | |
5263 } | |
5264 } | |
5265 } | |
5266 else | |
5267 { | |
5268 prev_pend = pend; | |
5269 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL) | |
5270 { | |
5271 /* Count a tab for what it's worth (if list mode not on) */ | |
5272 prev_pend = pend; | |
5273 incr = lbr_chartabsize_adv(&pend, (colnr_T)bdp->end_vcol); | |
5274 bdp->end_vcol += incr; | |
5275 } | |
5276 if (bdp->end_vcol <= oap->end_vcol | |
5277 && (!is_del | |
5278 || oap->op_type == OP_APPEND | |
5279 || oap->op_type == OP_REPLACE)) /* line too short */ | |
5280 { | |
5281 #ifdef FEAT_VISUALEXTRA | |
5282 bdp->is_short = TRUE; | |
5283 #endif | |
5284 /* Alternative: include spaces to fill up the block. | |
5285 * Disadvantage: can lead to trailing spaces when the line is | |
5286 * short where the text is put */ | |
5287 /* if (!is_del || oap->op_type == OP_APPEND) */ | |
5288 if (oap->op_type == OP_APPEND || virtual_op) | |
5289 bdp->endspaces = oap->end_vcol - bdp->end_vcol | |
593 | 5290 + oap->inclusive; |
7 | 5291 else |
5292 bdp->endspaces = 0; /* replace doesn't add characters */ | |
5293 } | |
5294 else if (bdp->end_vcol > oap->end_vcol) | |
5295 { | |
5296 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; | |
5297 if (!is_del && bdp->endspaces) | |
5298 { | |
5299 bdp->endspaces = incr - bdp->endspaces; | |
5300 if (pend != pstart) | |
5301 pend = prev_pend; | |
5302 } | |
5303 } | |
5304 } | |
5305 #ifdef FEAT_VISUALEXTRA | |
5306 bdp->end_char_vcols = incr; | |
5307 #endif | |
5308 if (is_del && bdp->startspaces) | |
5309 pstart = prev_pstart; | |
5310 bdp->textlen = (int)(pend - pstart); | |
5311 } | |
5312 bdp->textcol = (colnr_T) (pstart - line); | |
5313 bdp->textstart = pstart; | |
5314 } | |
5315 | |
5316 #ifdef FEAT_RIGHTLEFT | |
5317 static void reverse_line __ARGS((char_u *s)); | |
5318 | |
5319 static void | |
5320 reverse_line(s) | |
5321 char_u *s; | |
5322 { | |
5323 int i, j; | |
5324 char_u c; | |
5325 | |
5326 if ((i = (int)STRLEN(s) - 1) <= 0) | |
5327 return; | |
5328 | |
5329 curwin->w_cursor.col = i - curwin->w_cursor.col; | |
5330 for (j = 0; j < i; j++, i--) | |
5331 { | |
5332 c = s[i]; s[i] = s[j]; s[j] = c; | |
5333 } | |
5334 } | |
5335 | |
5336 # define RLADDSUBFIX(ptr) if (curwin->w_p_rl) reverse_line(ptr); | |
5337 #else | |
5338 # define RLADDSUBFIX(ptr) | |
5339 #endif | |
5340 | |
5341 /* | |
5342 * add or subtract 'Prenum1' from a number in a line | |
5343 * 'command' is CTRL-A for add, CTRL-X for subtract | |
5344 * | |
5345 * return FAIL for failure, OK otherwise | |
5346 */ | |
5347 int | |
5348 do_addsub(command, Prenum1) | |
5349 int command; | |
5350 linenr_T Prenum1; | |
5351 { | |
5352 int col; | |
5353 char_u *buf1; | |
5354 char_u buf2[NUMBUFLEN]; | |
5355 int hex; /* 'X' or 'x': hex; '0': octal */ | |
5356 static int hexupper = FALSE; /* 0xABC */ | |
835 | 5357 unsigned long n; |
7 | 5358 long_u oldn; |
5359 char_u *ptr; | |
5360 int c; | |
5361 int length = 0; /* character length of the number */ | |
5362 int todel; | |
5363 int dohex; | |
5364 int dooct; | |
5365 int doalp; | |
5366 int firstdigit; | |
5367 int negative; | |
5368 int subtract; | |
5369 | |
5370 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */ | |
5371 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */ | |
5372 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */ | |
5373 | |
5374 ptr = ml_get_curline(); | |
5375 RLADDSUBFIX(ptr); | |
5376 | |
5377 /* | |
5378 * First check if we are on a hexadecimal number, after the "0x". | |
5379 */ | |
5380 col = curwin->w_cursor.col; | |
5381 if (dohex) | |
5382 while (col > 0 && vim_isxdigit(ptr[col])) | |
5383 --col; | |
5384 if ( dohex | |
5385 && col > 0 | |
5386 && (ptr[col] == 'X' | |
5387 || ptr[col] == 'x') | |
5388 && ptr[col - 1] == '0' | |
5389 && vim_isxdigit(ptr[col + 1])) | |
5390 { | |
5391 /* | |
5392 * Found hexadecimal number, move to its start. | |
5393 */ | |
5394 --col; | |
5395 } | |
5396 else | |
5397 { | |
5398 /* | |
5399 * Search forward and then backward to find the start of number. | |
5400 */ | |
5401 col = curwin->w_cursor.col; | |
5402 | |
5403 while (ptr[col] != NUL | |
5404 && !vim_isdigit(ptr[col]) | |
5405 && !(doalp && ASCII_ISALPHA(ptr[col]))) | |
5406 ++col; | |
5407 | |
5408 while (col > 0 | |
5409 && vim_isdigit(ptr[col - 1]) | |
5410 && !(doalp && ASCII_ISALPHA(ptr[col]))) | |
5411 --col; | |
5412 } | |
5413 | |
5414 /* | |
5415 * If a number was found, and saving for undo works, replace the number. | |
5416 */ | |
5417 firstdigit = ptr[col]; | |
5418 RLADDSUBFIX(ptr); | |
5419 if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit))) | |
5420 || u_save_cursor() != OK) | |
5421 { | |
5422 beep_flush(); | |
5423 return FAIL; | |
5424 } | |
5425 | |
5426 /* get ptr again, because u_save() may have changed it */ | |
5427 ptr = ml_get_curline(); | |
5428 RLADDSUBFIX(ptr); | |
5429 | |
5430 if (doalp && ASCII_ISALPHA(firstdigit)) | |
5431 { | |
5432 /* decrement or increment alphabetic character */ | |
5433 if (command == Ctrl_X) | |
5434 { | |
5435 if (CharOrd(firstdigit) < Prenum1) | |
5436 { | |
5437 if (isupper(firstdigit)) | |
5438 firstdigit = 'A'; | |
5439 else | |
5440 firstdigit = 'a'; | |
5441 } | |
5442 else | |
5443 #ifdef EBCDIC | |
5444 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1); | |
5445 #else | |
5446 firstdigit -= Prenum1; | |
5447 #endif | |
5448 } | |
5449 else | |
5450 { | |
5451 if (26 - CharOrd(firstdigit) - 1 < Prenum1) | |
5452 { | |
5453 if (isupper(firstdigit)) | |
5454 firstdigit = 'Z'; | |
5455 else | |
5456 firstdigit = 'z'; | |
5457 } | |
5458 else | |
5459 #ifdef EBCDIC | |
5460 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1); | |
5461 #else | |
5462 firstdigit += Prenum1; | |
5463 #endif | |
5464 } | |
5465 curwin->w_cursor.col = col; | |
5466 (void)del_char(FALSE); | |
5467 ins_char(firstdigit); | |
5468 } | |
5469 else | |
5470 { | |
5471 negative = FALSE; | |
5472 if (col > 0 && ptr[col - 1] == '-') /* negative number */ | |
5473 { | |
5474 --col; | |
5475 negative = TRUE; | |
5476 } | |
5477 | |
5478 /* get the number value (unsigned) */ | |
5479 vim_str2nr(ptr + col, &hex, &length, dooct, dohex, NULL, &n); | |
5480 | |
5481 /* ignore leading '-' for hex and octal numbers */ | |
5482 if (hex && negative) | |
5483 { | |
5484 ++col; | |
5485 --length; | |
5486 negative = FALSE; | |
5487 } | |
5488 | |
5489 /* add or subtract */ | |
5490 subtract = FALSE; | |
5491 if (command == Ctrl_X) | |
5492 subtract ^= TRUE; | |
5493 if (negative) | |
5494 subtract ^= TRUE; | |
5495 | |
5496 oldn = n; | |
5497 if (subtract) | |
5498 n -= (unsigned long)Prenum1; | |
5499 else | |
5500 n += (unsigned long)Prenum1; | |
5501 | |
5502 /* handle wraparound for decimal numbers */ | |
5503 if (!hex) | |
5504 { | |
5505 if (subtract) | |
5506 { | |
5507 if (n > oldn) | |
5508 { | |
5509 n = 1 + (n ^ (unsigned long)-1); | |
5510 negative ^= TRUE; | |
5511 } | |
5512 } | |
5513 else /* add */ | |
5514 { | |
5515 if (n < oldn) | |
5516 { | |
5517 n = (n ^ (unsigned long)-1); | |
5518 negative ^= TRUE; | |
5519 } | |
5520 } | |
5521 if (n == 0) | |
5522 negative = FALSE; | |
5523 } | |
5524 | |
5525 /* | |
5526 * Delete the old number. | |
5527 */ | |
5528 curwin->w_cursor.col = col; | |
5529 todel = length; | |
5530 c = gchar_cursor(); | |
5531 /* | |
5532 * Don't include the '-' in the length, only the length of the part | |
5533 * after it is kept the same. | |
5534 */ | |
5535 if (c == '-') | |
5536 --length; | |
5537 while (todel-- > 0) | |
5538 { | |
5539 if (c < 0x100 && isalpha(c)) | |
5540 { | |
5541 if (isupper(c)) | |
5542 hexupper = TRUE; | |
5543 else | |
5544 hexupper = FALSE; | |
5545 } | |
5546 /* del_char() will mark line needing displaying */ | |
5547 (void)del_char(FALSE); | |
5548 c = gchar_cursor(); | |
5549 } | |
5550 | |
5551 /* | |
5552 * Prepare the leading characters in buf1[]. | |
5553 * When there are many leading zeros it could be very long. Allocate | |
5554 * a bit too much. | |
5555 */ | |
5556 buf1 = alloc((unsigned)length + NUMBUFLEN); | |
5557 if (buf1 == NULL) | |
5558 return FAIL; | |
5559 ptr = buf1; | |
5560 if (negative) | |
5561 { | |
5562 *ptr++ = '-'; | |
5563 } | |
5564 if (hex) | |
5565 { | |
5566 *ptr++ = '0'; | |
5567 --length; | |
5568 } | |
5569 if (hex == 'x' || hex == 'X') | |
5570 { | |
5571 *ptr++ = hex; | |
5572 --length; | |
5573 } | |
5574 | |
5575 /* | |
5576 * Put the number characters in buf2[]. | |
5577 */ | |
5578 if (hex == 0) | |
5579 sprintf((char *)buf2, "%lu", n); | |
5580 else if (hex == '0') | |
5581 sprintf((char *)buf2, "%lo", n); | |
5582 else if (hex && hexupper) | |
5583 sprintf((char *)buf2, "%lX", n); | |
5584 else | |
5585 sprintf((char *)buf2, "%lx", n); | |
5586 length -= (int)STRLEN(buf2); | |
5587 | |
5588 /* | |
39 | 5589 * Adjust number of zeros to the new number of digits, so the |
5590 * total length of the number remains the same. | |
5591 * Don't do this when | |
5592 * the result may look like an octal number. | |
7 | 5593 */ |
39 | 5594 if (firstdigit == '0' && !(dooct && hex == 0)) |
7 | 5595 while (length-- > 0) |
5596 *ptr++ = '0'; | |
5597 *ptr = NUL; | |
5598 STRCAT(buf1, buf2); | |
5599 ins_str(buf1); /* insert the new number */ | |
5600 vim_free(buf1); | |
5601 } | |
5602 --curwin->w_cursor.col; | |
5603 curwin->w_set_curswant = TRUE; | |
5604 #ifdef FEAT_RIGHTLEFT | |
5605 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE); | |
5606 RLADDSUBFIX(ptr); | |
5607 #endif | |
5608 return OK; | |
5609 } | |
5610 | |
5611 #ifdef FEAT_VIMINFO | |
5612 int | |
5613 read_viminfo_register(virp, force) | |
5614 vir_T *virp; | |
5615 int force; | |
5616 { | |
5617 int eof; | |
5618 int do_it = TRUE; | |
5619 int size; | |
5620 int limit; | |
5621 int i; | |
5622 int set_prev = FALSE; | |
5623 char_u *str; | |
5624 char_u **array = NULL; | |
5625 | |
5626 /* We only get here (hopefully) if line[0] == '"' */ | |
5627 str = virp->vir_line + 1; | |
1893 | 5628 |
5629 /* If the line starts with "" this is the y_previous register. */ | |
7 | 5630 if (*str == '"') |
5631 { | |
5632 set_prev = TRUE; | |
5633 str++; | |
5634 } | |
1893 | 5635 |
7 | 5636 if (!ASCII_ISALNUM(*str) && *str != '-') |
5637 { | |
5638 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line)) | |
5639 return TRUE; /* too many errors, pretend end-of-file */ | |
5640 do_it = FALSE; | |
5641 } | |
5642 get_yank_register(*str++, FALSE); | |
5643 if (!force && y_current->y_array != NULL) | |
5644 do_it = FALSE; | |
1893 | 5645 |
5646 if (*str == '@') | |
5647 { | |
5648 /* "x@: register x used for @@ */ | |
5649 if (force || execreg_lastc == NUL) | |
5650 execreg_lastc = str[-1]; | |
5651 } | |
5652 | |
7 | 5653 size = 0; |
5654 limit = 100; /* Optimized for registers containing <= 100 lines */ | |
5655 if (do_it) | |
5656 { | |
5657 if (set_prev) | |
5658 y_previous = y_current; | |
5659 vim_free(y_current->y_array); | |
5660 array = y_current->y_array = | |
5661 (char_u **)alloc((unsigned)(limit * sizeof(char_u *))); | |
1893 | 5662 str = skipwhite(skiptowhite(str)); |
7 | 5663 if (STRNCMP(str, "CHAR", 4) == 0) |
5664 y_current->y_type = MCHAR; | |
5665 else if (STRNCMP(str, "BLOCK", 5) == 0) | |
5666 y_current->y_type = MBLOCK; | |
5667 else | |
5668 y_current->y_type = MLINE; | |
5669 /* get the block width; if it's missing we get a zero, which is OK */ | |
5670 str = skipwhite(skiptowhite(str)); | |
5671 y_current->y_width = getdigits(&str); | |
5672 } | |
5673 | |
5674 while (!(eof = viminfo_readline(virp)) | |
5675 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<')) | |
5676 { | |
5677 if (do_it) | |
5678 { | |
5679 if (size >= limit) | |
5680 { | |
5681 y_current->y_array = (char_u **) | |
5682 alloc((unsigned)(limit * 2 * sizeof(char_u *))); | |
5683 for (i = 0; i < limit; i++) | |
5684 y_current->y_array[i] = array[i]; | |
5685 vim_free(array); | |
5686 limit *= 2; | |
5687 array = y_current->y_array; | |
5688 } | |
5689 str = viminfo_readstring(virp, 1, TRUE); | |
5690 if (str != NULL) | |
5691 array[size++] = str; | |
5692 else | |
5693 do_it = FALSE; | |
5694 } | |
5695 } | |
5696 if (do_it) | |
5697 { | |
5698 if (size == 0) | |
5699 { | |
5700 vim_free(array); | |
5701 y_current->y_array = NULL; | |
5702 } | |
5703 else if (size < limit) | |
5704 { | |
5705 y_current->y_array = | |
5706 (char_u **)alloc((unsigned)(size * sizeof(char_u *))); | |
5707 for (i = 0; i < size; i++) | |
5708 y_current->y_array[i] = array[i]; | |
5709 vim_free(array); | |
5710 } | |
5711 y_current->y_size = size; | |
5712 } | |
5713 return eof; | |
5714 } | |
5715 | |
5716 void | |
5717 write_viminfo_registers(fp) | |
5718 FILE *fp; | |
5719 { | |
5720 int i, j; | |
5721 char_u *type; | |
5722 char_u c; | |
5723 int num_lines; | |
5724 int max_num_lines; | |
5725 int max_kbyte; | |
5726 long len; | |
5727 | |
2278
0b3be97064e5
Various small fixes from Dominique Pelle.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5728 fputs(_("\n# Registers:\n"), fp); |
7 | 5729 |
5730 /* Get '<' value, use old '"' value if '<' is not found. */ | |
5731 max_num_lines = get_viminfo_parameter('<'); | |
5732 if (max_num_lines < 0) | |
5733 max_num_lines = get_viminfo_parameter('"'); | |
5734 if (max_num_lines == 0) | |
5735 return; | |
5736 max_kbyte = get_viminfo_parameter('s'); | |
5737 if (max_kbyte == 0) | |
5738 return; | |
1893 | 5739 |
7 | 5740 for (i = 0; i < NUM_REGISTERS; i++) |
5741 { | |
5742 if (y_regs[i].y_array == NULL) | |
5743 continue; | |
5744 #ifdef FEAT_CLIPBOARD | |
5745 /* Skip '*'/'+' register, we don't want them back next time */ | |
5746 if (i == STAR_REGISTER || i == PLUS_REGISTER) | |
5747 continue; | |
5748 #endif | |
5749 #ifdef FEAT_DND | |
5750 /* Neither do we want the '~' register */ | |
5751 if (i == TILDE_REGISTER) | |
5752 continue; | |
5753 #endif | |
55 | 5754 /* Skip empty registers. */ |
7 | 5755 num_lines = y_regs[i].y_size; |
55 | 5756 if (num_lines == 0 |
5757 || (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
|
5758 && *y_regs[i].y_array[0] == NUL)) |
55 | 5759 continue; |
5760 | |
7 | 5761 if (max_kbyte > 0) |
5762 { | |
5763 /* Skip register if there is more text than the maximum size. */ | |
5764 len = 0; | |
5765 for (j = 0; j < num_lines; j++) | |
835 | 5766 len += (long)STRLEN(y_regs[i].y_array[j]) + 1L; |
7 | 5767 if (len > (long)max_kbyte * 1024L) |
5768 continue; | |
5769 } | |
5770 | |
5771 switch (y_regs[i].y_type) | |
5772 { | |
5773 case MLINE: | |
5774 type = (char_u *)"LINE"; | |
5775 break; | |
5776 case MCHAR: | |
5777 type = (char_u *)"CHAR"; | |
5778 break; | |
5779 case MBLOCK: | |
5780 type = (char_u *)"BLOCK"; | |
5781 break; | |
5782 default: | |
5783 sprintf((char *)IObuff, _("E574: Unknown register type %d"), | |
274 | 5784 y_regs[i].y_type); |
7 | 5785 emsg(IObuff); |
5786 type = (char_u *)"LINE"; | |
5787 break; | |
5788 } | |
5789 if (y_previous == &y_regs[i]) | |
5790 fprintf(fp, "\""); | |
5791 c = get_register_name(i); | |
1893 | 5792 fprintf(fp, "\"%c", c); |
5793 if (c == execreg_lastc) | |
5794 fprintf(fp, "@"); | |
5735 | 5795 fprintf(fp, "\t%s\t%d\n", type, (int)y_regs[i].y_width); |
7 | 5796 |
5797 /* If max_num_lines < 0, then we save ALL the lines in the register */ | |
5798 if (max_num_lines > 0 && num_lines > max_num_lines) | |
5799 num_lines = max_num_lines; | |
5800 for (j = 0; j < num_lines; j++) | |
5801 { | |
5802 putc('\t', fp); | |
5803 viminfo_writestring(fp, y_regs[i].y_array[j]); | |
5804 } | |
5805 } | |
5806 } | |
5807 #endif /* FEAT_VIMINFO */ | |
5808 | |
5809 #if defined(FEAT_CLIPBOARD) || defined(PROTO) | |
5810 /* | |
5811 * SELECTION / PRIMARY ('*') | |
5812 * | |
5813 * Text selection stuff that uses the GUI selection register '*'. When using a | |
5814 * GUI this may be text from another window, otherwise it is the last text we | |
5815 * had highlighted with VIsual mode. With mouse support, clicking the middle | |
5816 * button performs the paste, otherwise you will need to do <"*p>. " | |
5817 * If not under X, it is synonymous with the clipboard register '+'. | |
5818 * | |
5819 * X CLIPBOARD ('+') | |
5820 * | |
5821 * Text selection stuff that uses the GUI clipboard register '+'. | |
5822 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection. | |
5823 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed", | |
5824 * otherwise you will need to do <"+p>. " | |
5825 * If not under X, it is synonymous with the selection register '*'. | |
5826 */ | |
5827 | |
5828 /* | |
5829 * Routine to export any final X selection we had to the environment | |
5830 * so that the text is still available after vim has exited. X selections | |
5831 * only exist while the owning application exists, so we write to the | |
5832 * permanent (while X runs) store CUT_BUFFER0. | |
5833 * Dump the CLIPBOARD selection if we own it (it's logically the more | |
5834 * 'permanent' of the two), otherwise the PRIMARY one. | |
5835 * For now, use a hard-coded sanity limit of 1Mb of data. | |
5836 */ | |
5837 #if defined(FEAT_X11) && defined(FEAT_CLIPBOARD) | |
5838 void | |
5839 x11_export_final_selection() | |
5840 { | |
5841 Display *dpy; | |
5842 char_u *str = NULL; | |
5843 long_u len = 0; | |
5844 int motion_type = -1; | |
5845 | |
5846 # ifdef FEAT_GUI | |
5847 if (gui.in_use) | |
5848 dpy = X_DISPLAY; | |
5849 else | |
5850 # endif | |
5851 # ifdef FEAT_XCLIPBOARD | |
5852 dpy = xterm_dpy; | |
5853 # else | |
5854 return; | |
5855 # endif | |
5856 | |
5857 /* Get selection to export */ | |
5858 if (clip_plus.owned) | |
5859 motion_type = clip_convert_selection(&str, &len, &clip_plus); | |
5860 else if (clip_star.owned) | |
5861 motion_type = clip_convert_selection(&str, &len, &clip_star); | |
5862 | |
5863 /* Check it's OK */ | |
5864 if (dpy != NULL && str != NULL && motion_type >= 0 | |
5865 && len < 1024*1024 && len > 0) | |
5866 { | |
1924 | 5867 #ifdef FEAT_MBYTE |
4201 | 5868 int ok = TRUE; |
5869 | |
1924 | 5870 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from |
5871 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit | |
5872 * encoding conversion usually doesn't work, so keep the text as-is. | |
5873 */ | |
5874 if (has_mbyte) | |
5875 { | |
5876 vimconv_T vc; | |
5877 | |
5878 vc.vc_type = CONV_NONE; | |
5879 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK) | |
5880 { | |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2007
diff
changeset
|
5881 int intlen = len; |
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2007
diff
changeset
|
5882 char_u *conv_str; |
2007 | 5883 |
4201 | 5884 vc.vc_fail = TRUE; |
2007 | 5885 conv_str = string_convert(&vc, str, &intlen); |
5886 len = intlen; | |
1924 | 5887 if (conv_str != NULL) |
5888 { | |
5889 vim_free(str); | |
5890 str = conv_str; | |
5891 } | |
4201 | 5892 else |
5893 { | |
5894 ok = FALSE; | |
5895 } | |
1924 | 5896 convert_setup(&vc, NULL, NULL); |
5897 } | |
4201 | 5898 else |
5899 { | |
5900 ok = FALSE; | |
5901 } | |
1924 | 5902 } |
4201 | 5903 |
5904 /* Do not store the string if conversion failed. Better to use any | |
5905 * other selection than garbled text. */ | |
5906 if (ok) | |
5907 #endif | |
5908 { | |
5909 XStoreBuffer(dpy, (char *)str, (int)len, 0); | |
5910 XFlush(dpy); | |
5911 } | |
7 | 5912 } |
5913 | |
5914 vim_free(str); | |
5915 } | |
5916 #endif | |
5917 | |
5918 void | |
5919 clip_free_selection(cbd) | |
5920 VimClipboard *cbd; | |
5921 { | |
5922 struct yankreg *y_ptr = y_current; | |
5923 | |
5924 if (cbd == &clip_plus) | |
5925 y_current = &y_regs[PLUS_REGISTER]; | |
5926 else | |
5927 y_current = &y_regs[STAR_REGISTER]; | |
5928 free_yank_all(); | |
5929 y_current->y_size = 0; | |
5930 y_current = y_ptr; | |
5931 } | |
5932 | |
5933 /* | |
5934 * Get the selected text and put it in the gui selection register '*' or '+'. | |
5935 */ | |
5936 void | |
5937 clip_get_selection(cbd) | |
5938 VimClipboard *cbd; | |
5939 { | |
5940 struct yankreg *old_y_previous, *old_y_current; | |
5941 pos_T old_cursor; | |
5942 pos_T old_visual; | |
5943 int old_visual_mode; | |
5944 colnr_T old_curswant; | |
5945 int old_set_curswant; | |
5946 pos_T old_op_start, old_op_end; | |
5947 oparg_T oa; | |
5948 cmdarg_T ca; | |
5949 | |
5950 if (cbd->owned) | |
5951 { | |
5952 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL) | |
5953 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL)) | |
5954 return; | |
5955 | |
5956 /* Get the text between clip_star.start & clip_star.end */ | |
5957 old_y_previous = y_previous; | |
5958 old_y_current = y_current; | |
5959 old_cursor = curwin->w_cursor; | |
5960 old_curswant = curwin->w_curswant; | |
5961 old_set_curswant = curwin->w_set_curswant; | |
5962 old_op_start = curbuf->b_op_start; | |
5963 old_op_end = curbuf->b_op_end; | |
5964 old_visual = VIsual; | |
5965 old_visual_mode = VIsual_mode; | |
5966 clear_oparg(&oa); | |
5967 oa.regname = (cbd == &clip_plus ? '+' : '*'); | |
5968 oa.op_type = OP_YANK; | |
5969 vim_memset(&ca, 0, sizeof(ca)); | |
5970 ca.oap = &oa; | |
5971 ca.cmdchar = 'y'; | |
5972 ca.count1 = 1; | |
5973 ca.retval = CA_NO_ADJ_OP_END; | |
5974 do_pending_operator(&ca, 0, TRUE); | |
5975 y_previous = old_y_previous; | |
5976 y_current = old_y_current; | |
5977 curwin->w_cursor = old_cursor; | |
688 | 5978 changed_cline_bef_curs(); /* need to update w_virtcol et al */ |
7 | 5979 curwin->w_curswant = old_curswant; |
5980 curwin->w_set_curswant = old_set_curswant; | |
5981 curbuf->b_op_start = old_op_start; | |
5982 curbuf->b_op_end = old_op_end; | |
5983 VIsual = old_visual; | |
5984 VIsual_mode = old_visual_mode; | |
5985 } | |
5986 else | |
5987 { | |
5988 clip_free_selection(cbd); | |
5989 | |
5990 /* Try to get selected text from another window */ | |
5991 clip_gen_request_selection(cbd); | |
5992 } | |
5993 } | |
5994 | |
2896 | 5995 /* |
5996 * Convert from the GUI selection string into the '*'/'+' register. | |
5997 */ | |
7 | 5998 void |
5999 clip_yank_selection(type, str, len, cbd) | |
6000 int type; | |
6001 char_u *str; | |
6002 long len; | |
6003 VimClipboard *cbd; | |
6004 { | |
6005 struct yankreg *y_ptr; | |
6006 | |
6007 if (cbd == &clip_plus) | |
6008 y_ptr = &y_regs[PLUS_REGISTER]; | |
6009 else | |
6010 y_ptr = &y_regs[STAR_REGISTER]; | |
6011 | |
6012 clip_free_selection(cbd); | |
6013 | |
5798 | 6014 str_to_reg(y_ptr, type, str, len, 0L, FALSE); |
7 | 6015 } |
6016 | |
6017 /* | |
6018 * Convert the '*'/'+' register into a GUI selection string returned in *str | |
6019 * with length *len. | |
6020 * Returns the motion type, or -1 for failure. | |
6021 */ | |
6022 int | |
6023 clip_convert_selection(str, len, cbd) | |
6024 char_u **str; | |
6025 long_u *len; | |
6026 VimClipboard *cbd; | |
6027 { | |
6028 char_u *p; | |
6029 int lnum; | |
6030 int i, j; | |
6031 int_u eolsize; | |
6032 struct yankreg *y_ptr; | |
6033 | |
6034 if (cbd == &clip_plus) | |
6035 y_ptr = &y_regs[PLUS_REGISTER]; | |
6036 else | |
6037 y_ptr = &y_regs[STAR_REGISTER]; | |
6038 | |
6039 #ifdef USE_CRNL | |
6040 eolsize = 2; | |
6041 #else | |
6042 eolsize = 1; | |
6043 #endif | |
6044 | |
6045 *str = NULL; | |
6046 *len = 0; | |
6047 if (y_ptr->y_array == NULL) | |
6048 return -1; | |
6049 | |
6050 for (i = 0; i < y_ptr->y_size; i++) | |
6051 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize; | |
6052 | |
6053 /* | |
6054 * Don't want newline character at end of last line if we're in MCHAR mode. | |
6055 */ | |
6056 if (y_ptr->y_type == MCHAR && *len >= eolsize) | |
6057 *len -= eolsize; | |
6058 | |
6059 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */ | |
6060 if (p == NULL) | |
6061 return -1; | |
6062 lnum = 0; | |
6063 for (i = 0, j = 0; i < (int)*len; i++, j++) | |
6064 { | |
6065 if (y_ptr->y_array[lnum][j] == '\n') | |
6066 p[i] = NUL; | |
6067 else if (y_ptr->y_array[lnum][j] == NUL) | |
6068 { | |
6069 #ifdef USE_CRNL | |
6070 p[i++] = '\r'; | |
6071 #endif | |
6072 #ifdef USE_CR | |
6073 p[i] = '\r'; | |
6074 #else | |
6075 p[i] = '\n'; | |
6076 #endif | |
6077 lnum++; | |
6078 j = -1; | |
6079 } | |
6080 else | |
6081 p[i] = y_ptr->y_array[lnum][j]; | |
6082 } | |
6083 return y_ptr->y_type; | |
6084 } | |
6085 | |
6086 | |
6087 /* | |
6088 * If we have written to a clipboard register, send the text to the clipboard. | |
6089 */ | |
6090 static void | |
6091 may_set_selection() | |
6092 { | |
6093 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available) | |
6094 { | |
6095 clip_own_selection(&clip_star); | |
6096 clip_gen_set_selection(&clip_star); | |
6097 } | |
6098 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available) | |
6099 { | |
6100 clip_own_selection(&clip_plus); | |
6101 clip_gen_set_selection(&clip_plus); | |
6102 } | |
6103 } | |
6104 | |
6105 #endif /* FEAT_CLIPBOARD || PROTO */ | |
6106 | |
6107 | |
6108 #if defined(FEAT_DND) || defined(PROTO) | |
6109 /* | |
6110 * Replace the contents of the '~' register with str. | |
6111 */ | |
6112 void | |
6113 dnd_yank_drag_data(str, len) | |
6114 char_u *str; | |
6115 long len; | |
6116 { | |
6117 struct yankreg *curr; | |
6118 | |
6119 curr = y_current; | |
6120 y_current = &y_regs[TILDE_REGISTER]; | |
6121 free_yank_all(); | |
5798 | 6122 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE); |
7 | 6123 y_current = curr; |
6124 } | |
6125 #endif | |
6126 | |
6127 | |
6128 #if defined(FEAT_EVAL) || defined(PROTO) | |
6129 /* | |
6130 * Return the type of a register. | |
6131 * Used for getregtype() | |
6132 * Returns MAUTO for error. | |
6133 */ | |
6134 char_u | |
6135 get_reg_type(regname, reglen) | |
6136 int regname; | |
6137 long *reglen; | |
6138 { | |
6139 switch (regname) | |
6140 { | |
6141 case '%': /* file name */ | |
6142 case '#': /* alternate file name */ | |
6143 case '=': /* expression */ | |
6144 case ':': /* last command line */ | |
6145 case '/': /* last search-pattern */ | |
6146 case '.': /* last inserted text */ | |
6147 #ifdef FEAT_SEARCHPATH | |
6148 case Ctrl_F: /* Filename under cursor */ | |
6149 case Ctrl_P: /* Path under cursor, expand via "path" */ | |
6150 #endif | |
6151 case Ctrl_W: /* word under cursor */ | |
6152 case Ctrl_A: /* WORD (mnemonic All) under cursor */ | |
6153 case '_': /* black hole: always empty */ | |
6154 return MCHAR; | |
6155 } | |
6156 | |
6157 #ifdef FEAT_CLIPBOARD | |
6158 regname = may_get_selection(regname); | |
6159 #endif | |
6160 | |
5596 | 6161 if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
6162 return MAUTO; | |
6163 | |
7 | 6164 get_yank_register(regname, FALSE); |
6165 | |
6166 if (y_current->y_array != NULL) | |
6167 { | |
6168 if (reglen != NULL && y_current->y_type == MBLOCK) | |
6169 *reglen = y_current->y_width; | |
6170 return y_current->y_type; | |
6171 } | |
6172 return MAUTO; | |
6173 } | |
6174 | |
5796 | 6175 static char_u *getreg_wrap_one_line __ARGS((char_u *s, int flags)); |
6176 | |
6177 /* | |
6178 * When "flags" has GREG_LIST return a list with text "s". | |
6179 * Otherwise just return "s". | |
6180 */ | |
6181 static char_u * | |
6182 getreg_wrap_one_line(s, flags) | |
6183 char_u *s; | |
6184 int flags; | |
6185 { | |
6186 if (flags & GREG_LIST) | |
6187 { | |
6188 list_T *list = list_alloc(); | |
6189 | |
6190 if (list != NULL) | |
6191 { | |
6192 if (list_append_string(list, NULL, -1) == FAIL) | |
6193 { | |
6194 list_free(list, TRUE); | |
6195 return NULL; | |
6196 } | |
6197 list->lv_first->li_tv.vval.v_string = s; | |
6198 } | |
6199 return (char_u *)list; | |
6200 } | |
6201 return s; | |
6202 } | |
6203 | |
7 | 6204 /* |
6205 * Return the contents of a register as a single allocated string. | |
6206 * Used for "@r" in expressions and for getreg(). | |
6207 * Returns NULL for error. | |
5796 | 6208 * Flags: |
6209 * GREG_NO_EXPR Do not allow expression register | |
6210 * GREG_EXPR_SRC For the expression register: return expression itself, | |
6211 * not the result of its evaluation. | |
6212 * GREG_LIST Return a list of lines in place of a single string. | |
7 | 6213 */ |
6214 char_u * | |
5796 | 6215 get_reg_contents(regname, flags) |
7 | 6216 int regname; |
5796 | 6217 int flags; |
7 | 6218 { |
6219 long i; | |
6220 char_u *retval; | |
6221 int allocated; | |
6222 long len; | |
6223 | |
6224 /* Don't allow using an expression register inside an expression */ | |
6225 if (regname == '=') | |
6226 { | |
5796 | 6227 if (flags & GREG_NO_EXPR) |
6228 return NULL; | |
6229 if (flags & GREG_EXPR_SRC) | |
6230 return getreg_wrap_one_line(get_expr_line_src(), flags); | |
6231 return getreg_wrap_one_line(get_expr_line(), flags); | |
7 | 6232 } |
6233 | |
6234 if (regname == '@') /* "@@" is used for unnamed register */ | |
6235 regname = '"'; | |
6236 | |
6237 /* check for valid regname */ | |
6238 if (regname != NUL && !valid_yank_reg(regname, FALSE)) | |
6239 return NULL; | |
6240 | |
6241 #ifdef FEAT_CLIPBOARD | |
6242 regname = may_get_selection(regname); | |
6243 #endif | |
6244 | |
6245 if (get_spec_reg(regname, &retval, &allocated, FALSE)) | |
6246 { | |
6247 if (retval == NULL) | |
6248 return NULL; | |
5796 | 6249 if (allocated) |
6250 return getreg_wrap_one_line(retval, flags); | |
6251 return getreg_wrap_one_line(vim_strsave(retval), flags); | |
7 | 6252 } |
6253 | |
6254 get_yank_register(regname, FALSE); | |
6255 if (y_current->y_array == NULL) | |
6256 return NULL; | |
6257 | |
5796 | 6258 if (flags & GREG_LIST) |
6259 { | |
6260 list_T *list = list_alloc(); | |
6261 int error = FALSE; | |
6262 | |
6263 if (list == NULL) | |
6264 return NULL; | |
6265 for (i = 0; i < y_current->y_size; ++i) | |
6266 if (list_append_string(list, y_current->y_array[i], -1) == FAIL) | |
6267 error = TRUE; | |
6268 if (error) | |
6269 { | |
6270 list_free(list, TRUE); | |
6271 return NULL; | |
6272 } | |
6273 return (char_u *)list; | |
6274 } | |
6275 | |
7 | 6276 /* |
6277 * Compute length of resulting string. | |
6278 */ | |
6279 len = 0; | |
6280 for (i = 0; i < y_current->y_size; ++i) | |
6281 { | |
6282 len += (long)STRLEN(y_current->y_array[i]); | |
6283 /* | |
6284 * Insert a newline between lines and after last line if | |
6285 * y_type is MLINE. | |
6286 */ | |
6287 if (y_current->y_type == MLINE || i < y_current->y_size - 1) | |
6288 ++len; | |
6289 } | |
6290 | |
6291 retval = lalloc(len + 1, TRUE); | |
6292 | |
6293 /* | |
6294 * Copy the lines of the yank register into the string. | |
6295 */ | |
6296 if (retval != NULL) | |
6297 { | |
6298 len = 0; | |
6299 for (i = 0; i < y_current->y_size; ++i) | |
6300 { | |
6301 STRCPY(retval + len, y_current->y_array[i]); | |
6302 len += (long)STRLEN(retval + len); | |
6303 | |
6304 /* | |
6305 * Insert a NL between lines and after the last line if y_type is | |
6306 * MLINE. | |
6307 */ | |
6308 if (y_current->y_type == MLINE || i < y_current->y_size - 1) | |
6309 retval[len++] = '\n'; | |
6310 } | |
6311 retval[len] = NUL; | |
6312 } | |
6313 | |
6314 return retval; | |
6315 } | |
6316 | |
5798 | 6317 static int |
6318 init_write_reg(name, old_y_previous, old_y_current, must_append, yank_type) | |
6319 int name; | |
6320 struct yankreg **old_y_previous; | |
6321 struct yankreg **old_y_current; | |
6322 int must_append; | |
6323 int *yank_type UNUSED; | |
6324 { | |
6325 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */ | |
6326 { | |
6327 emsg_invreg(name); | |
6328 return FAIL; | |
6329 } | |
6330 | |
6331 /* Don't want to change the current (unnamed) register */ | |
6332 *old_y_previous = y_previous; | |
6333 *old_y_current = y_current; | |
6334 | |
6335 get_yank_register(name, TRUE); | |
6336 if (!y_append && !must_append) | |
6337 free_yank_all(); | |
6338 return OK; | |
6339 } | |
6340 | |
6341 static void | |
6342 finish_write_reg(name, old_y_previous, old_y_current) | |
6343 int name; | |
6344 struct yankreg *old_y_previous; | |
6345 struct yankreg *old_y_current; | |
6346 { | |
6347 # ifdef FEAT_CLIPBOARD | |
6348 /* Send text of clipboard register to the clipboard. */ | |
6349 may_set_selection(); | |
6350 # endif | |
6351 | |
6352 /* ':let @" = "val"' should change the meaning of the "" register */ | |
6353 if (name != '"') | |
6354 y_previous = old_y_previous; | |
6355 y_current = old_y_current; | |
6356 } | |
6357 | |
7 | 6358 /* |
6359 * Store string "str" in register "name". | |
6360 * "maxlen" is the maximum number of bytes to use, -1 for all bytes. | |
6361 * If "must_append" is TRUE, always append to the register. Otherwise append | |
6362 * if "name" is an uppercase letter. | |
6363 * Note: "maxlen" and "must_append" don't work for the "/" register. | |
6364 * Careful: 'str' is modified, you may have to use a copy! | |
6365 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise. | |
6366 */ | |
6367 void | |
6368 write_reg_contents(name, str, maxlen, must_append) | |
6369 int name; | |
6370 char_u *str; | |
6371 int maxlen; | |
6372 int must_append; | |
6373 { | |
6374 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L); | |
6375 } | |
6376 | |
6377 void | |
5798 | 6378 write_reg_contents_lst(name, strings, maxlen, must_append, yank_type, block_len) |
6379 int name; | |
6380 char_u **strings; | |
6381 int maxlen UNUSED; | |
6382 int must_append; | |
6383 int yank_type; | |
6384 long block_len; | |
6385 { | |
6386 struct yankreg *old_y_previous, *old_y_current; | |
6387 | |
6388 if (name == '/' | |
6389 #ifdef FEAT_EVAL | |
6390 || name == '=' | |
6391 #endif | |
6392 ) | |
6393 { | |
6394 char_u *s; | |
6395 | |
6396 if (strings[0] == NULL) | |
6397 s = (char_u *)""; | |
6398 else if (strings[1] != NULL) | |
6399 { | |
6400 EMSG(_("E883: search pattern and expression register may not " | |
6401 "contain two or more lines")); | |
6402 return; | |
6403 } | |
6404 else | |
6405 s = strings[0]; | |
6406 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len); | |
6407 return; | |
6408 } | |
6409 | |
6410 if (name == '_') /* black hole: nothing to do */ | |
6411 return; | |
6412 | |
6413 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, | |
6414 &yank_type) == FAIL) | |
6415 return; | |
6416 | |
6417 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE); | |
6418 | |
6419 finish_write_reg(name, old_y_previous, old_y_current); | |
6420 } | |
6421 | |
6422 void | |
7 | 6423 write_reg_contents_ex(name, str, maxlen, must_append, yank_type, block_len) |
6424 int name; | |
6425 char_u *str; | |
6426 int maxlen; | |
6427 int must_append; | |
6428 int yank_type; | |
6429 long block_len; | |
6430 { | |
6431 struct yankreg *old_y_previous, *old_y_current; | |
6432 long len; | |
6433 | |
336 | 6434 if (maxlen >= 0) |
6435 len = maxlen; | |
6436 else | |
6437 len = (long)STRLEN(str); | |
6438 | |
7 | 6439 /* Special case: '/' search pattern */ |
6440 if (name == '/') | |
6441 { | |
6442 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE); | |
6443 return; | |
6444 } | |
6445 | |
336 | 6446 #ifdef FEAT_EVAL |
6447 if (name == '=') | |
6448 { | |
6449 char_u *p, *s; | |
6450 | |
6451 p = vim_strnsave(str, (int)len); | |
6452 if (p == NULL) | |
6453 return; | |
6454 if (must_append) | |
6455 { | |
6456 s = concat_str(get_expr_line_src(), p); | |
6457 vim_free(p); | |
6458 p = s; | |
6459 } | |
6460 set_expr_line(p); | |
6461 return; | |
6462 } | |
6463 #endif | |
6464 | |
7 | 6465 if (name == '_') /* black hole: nothing to do */ |
6466 return; | |
6467 | |
5798 | 6468 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, |
6469 &yank_type) == FAIL) | |
6470 return; | |
6471 | |
6472 str_to_reg(y_current, yank_type, str, len, block_len, FALSE); | |
6473 | |
6474 finish_write_reg(name, old_y_previous, old_y_current); | |
7 | 6475 } |
6476 #endif /* FEAT_EVAL */ | |
6477 | |
6478 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) | |
6479 /* | |
6480 * Put a string into a register. When the register is not empty, the string | |
6481 * is appended. | |
6482 */ | |
6483 static void | |
5798 | 6484 str_to_reg(y_ptr, yank_type, str, len, blocklen, str_list) |
7 | 6485 struct yankreg *y_ptr; /* pointer to yank register */ |
2896 | 6486 int yank_type; /* MCHAR, MLINE, MBLOCK, MAUTO */ |
7 | 6487 char_u *str; /* string to put in register */ |
6488 long len; /* length of string */ | |
6489 long blocklen; /* width of Visual block */ | |
5798 | 6490 int str_list; /* TRUE if str is char_u ** */ |
7 | 6491 { |
2896 | 6492 int type; /* MCHAR, MLINE or MBLOCK */ |
7 | 6493 int lnum; |
6494 long start; | |
6495 long i; | |
6496 int extra; | |
6497 int newlines; /* number of lines added */ | |
6498 int extraline = 0; /* extra line at the end */ | |
6499 int append = FALSE; /* append to last line in register */ | |
6500 char_u *s; | |
5798 | 6501 char_u **ss; |
7 | 6502 char_u **pp; |
6503 long maxlen; | |
6504 | |
2000 | 6505 if (y_ptr->y_array == NULL) /* NULL means empty register */ |
7 | 6506 y_ptr->y_size = 0; |
6507 | |
2896 | 6508 if (yank_type == MAUTO) |
5798 | 6509 type = ((str_list || (len > 0 && (str[len - 1] == NL |
6510 || str[len - 1] == CAR))) | |
2896 | 6511 ? MLINE : MCHAR); |
6512 else | |
6513 type = yank_type; | |
6514 | |
7 | 6515 /* |
6516 * Count the number of lines within the string | |
6517 */ | |
6518 newlines = 0; | |
5798 | 6519 if (str_list) |
6520 { | |
6521 for (ss = (char_u **) str; *ss != NULL; ++ss) | |
7 | 6522 ++newlines; |
5798 | 6523 } |
6524 else | |
6525 { | |
6526 for (i = 0; i < len; i++) | |
6527 if (str[i] == '\n') | |
6528 ++newlines; | |
6529 if (type == MCHAR || len == 0 || str[len - 1] != '\n') | |
6530 { | |
6531 extraline = 1; | |
6532 ++newlines; /* count extra newline at the end */ | |
6533 } | |
6534 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) | |
6535 { | |
6536 append = TRUE; | |
6537 --newlines; /* uncount newline when appending first line */ | |
6538 } | |
7 | 6539 } |
6540 | |
6541 /* | |
6542 * Allocate an array to hold the pointers to the new register lines. | |
6543 * If the register was not empty, move the existing lines to the new array. | |
6544 */ | |
6545 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines) | |
6546 * sizeof(char_u *), TRUE); | |
6547 if (pp == NULL) /* out of memory */ | |
6548 return; | |
6549 for (lnum = 0; lnum < y_ptr->y_size; ++lnum) | |
6550 pp[lnum] = y_ptr->y_array[lnum]; | |
6551 vim_free(y_ptr->y_array); | |
6552 y_ptr->y_array = pp; | |
6553 maxlen = 0; | |
6554 | |
6555 /* | |
6556 * Find the end of each line and save it into the array. | |
6557 */ | |
5798 | 6558 if (str_list) |
6559 { | |
6560 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) | |
7 | 6561 { |
5856 | 6562 i = (long)STRLEN(*ss); |
5798 | 6563 pp[lnum] = vim_strnsave(*ss, i); |
6564 if (i > maxlen) | |
6565 maxlen = i; | |
7 | 6566 } |
5798 | 6567 } |
6568 else | |
6569 { | |
6570 for (start = 0; start < len + extraline; start += i + 1) | |
7 | 6571 { |
5798 | 6572 for (i = start; i < len; ++i) /* find the end of the line */ |
6573 if (str[i] == '\n') | |
6574 break; | |
6575 i -= start; /* i is now length of line */ | |
6576 if (i > maxlen) | |
6577 maxlen = i; | |
6578 if (append) | |
6579 { | |
6580 --lnum; | |
6581 extra = (int)STRLEN(y_ptr->y_array[lnum]); | |
6582 } | |
6583 else | |
6584 extra = 0; | |
6585 s = alloc((unsigned)(i + extra + 1)); | |
6586 if (s == NULL) | |
6587 break; | |
6588 if (extra) | |
6589 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra); | |
6590 if (append) | |
6591 vim_free(y_ptr->y_array[lnum]); | |
6592 if (i) | |
6593 mch_memmove(s + extra, str + start, (size_t)i); | |
6594 extra += i; | |
6595 s[extra] = NUL; | |
6596 y_ptr->y_array[lnum++] = s; | |
6597 while (--extra >= 0) | |
6598 { | |
6599 if (*s == NUL) | |
6600 *s = '\n'; /* replace NUL with newline */ | |
6601 ++s; | |
6602 } | |
6603 append = FALSE; /* only first line is appended */ | |
7 | 6604 } |
6605 } | |
6606 y_ptr->y_type = type; | |
6607 y_ptr->y_size = lnum; | |
6608 if (type == MBLOCK) | |
6609 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); | |
6610 else | |
6611 y_ptr->y_width = 0; | |
6612 } | |
6613 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */ | |
6614 | |
6615 void | |
6616 clear_oparg(oap) | |
6617 oparg_T *oap; | |
6618 { | |
6619 vim_memset(oap, 0, sizeof(oparg_T)); | |
6620 } | |
6621 | |
161 | 6622 static long line_count_info __ARGS((char_u *line, long *wc, long *cc, long limit, int eol_size)); |
7 | 6623 |
6624 /* | |
161 | 6625 * Count the number of bytes, characters and "words" in a line. |
7 | 6626 * |
6627 * "Words" are counted by looking for boundaries between non-space and | |
6628 * space characters. (it seems to produce results that match 'wc'.) | |
6629 * | |
161 | 6630 * Return value is byte count; word count for the line is added to "*wc". |
6631 * Char count is added to "*cc". | |
7 | 6632 * |
6633 * The function will only examine the first "limit" characters in the | |
6634 * line, stopping if it encounters an end-of-line (NUL byte). In that | |
6635 * case, eol_size will be added to the character count to account for | |
6636 * the size of the EOL character. | |
6637 */ | |
6638 static long | |
161 | 6639 line_count_info(line, wc, cc, limit, eol_size) |
7 | 6640 char_u *line; |
6641 long *wc; | |
161 | 6642 long *cc; |
7 | 6643 long limit; |
6644 int eol_size; | |
6645 { | |
161 | 6646 long i; |
6647 long words = 0; | |
6648 long chars = 0; | |
7 | 6649 int is_word = 0; |
6650 | |
3626 | 6651 for (i = 0; i < limit && line[i] != NUL; ) |
7 | 6652 { |
6653 if (is_word) | |
6654 { | |
6655 if (vim_isspace(line[i])) | |
6656 { | |
6657 words++; | |
6658 is_word = 0; | |
6659 } | |
6660 } | |
6661 else if (!vim_isspace(line[i])) | |
6662 is_word = 1; | |
161 | 6663 ++chars; |
6664 #ifdef FEAT_MBYTE | |
474 | 6665 i += (*mb_ptr2len)(line + i); |
161 | 6666 #else |
6667 ++i; | |
6668 #endif | |
7 | 6669 } |
6670 | |
6671 if (is_word) | |
6672 words++; | |
6673 *wc += words; | |
6674 | |
6675 /* Add eol_size if the end of line was reached before hitting limit. */ | |
2996 | 6676 if (i < limit && line[i] == NUL) |
161 | 6677 { |
7 | 6678 i += eol_size; |
161 | 6679 chars += eol_size; |
6680 } | |
6681 *cc += chars; | |
7 | 6682 return i; |
6683 } | |
6684 | |
6685 /* | |
6686 * Give some info about the position of the cursor (for "g CTRL-G"). | |
6687 * In Visual mode, give some info about the selected region. (In this case, | |
6688 * the *_count_cursor variables store running totals for the selection.) | |
6689 */ | |
6690 void | |
6691 cursor_pos_info() | |
6692 { | |
6693 char_u *p; | |
274 | 6694 char_u buf1[50]; |
6695 char_u buf2[40]; | |
7 | 6696 linenr_T lnum; |
161 | 6697 long byte_count = 0; |
6698 long byte_count_cursor = 0; | |
7 | 6699 long char_count = 0; |
6700 long char_count_cursor = 0; | |
161 | 6701 long word_count = 0; |
6702 long word_count_cursor = 0; | |
7 | 6703 int eol_size; |
6704 long last_check = 100000L; | |
6705 long line_count_selected = 0; | |
6706 pos_T min_pos, max_pos; | |
6707 oparg_T oparg; | |
6708 struct block_def bd; | |
6709 | |
6710 /* | |
6711 * Compute the length of the file in characters. | |
6712 */ | |
6713 if (curbuf->b_ml.ml_flags & ML_EMPTY) | |
6714 { | |
6715 MSG(_(no_lines_msg)); | |
6716 } | |
6717 else | |
6718 { | |
6719 if (get_fileformat(curbuf) == EOL_DOS) | |
6720 eol_size = 2; | |
6721 else | |
6722 eol_size = 1; | |
6723 | |
6724 if (VIsual_active) | |
6725 { | |
6726 if (lt(VIsual, curwin->w_cursor)) | |
6727 { | |
6728 min_pos = VIsual; | |
6729 max_pos = curwin->w_cursor; | |
6730 } | |
6731 else | |
6732 { | |
6733 min_pos = curwin->w_cursor; | |
6734 max_pos = VIsual; | |
6735 } | |
6736 if (*p_sel == 'e' && max_pos.col > 0) | |
6737 --max_pos.col; | |
6738 | |
6739 if (VIsual_mode == Ctrl_V) | |
6740 { | |
1866 | 6741 #ifdef FEAT_LINEBREAK |
6742 char_u * saved_sbr = p_sbr; | |
6743 | |
6744 /* Make 'sbr' empty for a moment to get the correct size. */ | |
6745 p_sbr = empty_option; | |
6746 #endif | |
7 | 6747 oparg.is_VIsual = 1; |
6748 oparg.block_mode = TRUE; | |
6749 oparg.op_type = OP_NOP; | |
6750 getvcols(curwin, &min_pos, &max_pos, | |
688 | 6751 &oparg.start_vcol, &oparg.end_vcol); |
1866 | 6752 #ifdef FEAT_LINEBREAK |
6753 p_sbr = saved_sbr; | |
6754 #endif | |
688 | 6755 if (curwin->w_curswant == MAXCOL) |
6756 oparg.end_vcol = MAXCOL; | |
7 | 6757 /* Swap the start, end vcol if needed */ |
6758 if (oparg.end_vcol < oparg.start_vcol) | |
6759 { | |
6760 oparg.end_vcol += oparg.start_vcol; | |
6761 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol; | |
6762 oparg.end_vcol -= oparg.start_vcol; | |
6763 } | |
6764 } | |
6765 line_count_selected = max_pos.lnum - min_pos.lnum + 1; | |
6766 } | |
6767 | |
6768 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) | |
6769 { | |
6770 /* Check for a CTRL-C every 100000 characters. */ | |
161 | 6771 if (byte_count > last_check) |
7 | 6772 { |
6773 ui_breakcheck(); | |
6774 if (got_int) | |
6775 return; | |
161 | 6776 last_check = byte_count + 100000L; |
7 | 6777 } |
6778 | |
6779 /* Do extra processing for VIsual mode. */ | |
6780 if (VIsual_active | |
6781 && lnum >= min_pos.lnum && lnum <= max_pos.lnum) | |
6782 { | |
45 | 6783 char_u *s = NULL; |
6784 long len = 0L; | |
6785 | |
7 | 6786 switch (VIsual_mode) |
6787 { | |
6788 case Ctrl_V: | |
5735 | 6789 #ifdef FEAT_VIRTUALEDIT |
7 | 6790 virtual_op = virtual_active(); |
5735 | 6791 #endif |
7 | 6792 block_prep(&oparg, &bd, lnum, 0); |
5735 | 6793 #ifdef FEAT_VIRTUALEDIT |
7 | 6794 virtual_op = MAYBE; |
5735 | 6795 #endif |
45 | 6796 s = bd.textstart; |
6797 len = (long)bd.textlen; | |
7 | 6798 break; |
6799 case 'V': | |
45 | 6800 s = ml_get(lnum); |
6801 len = MAXCOL; | |
7 | 6802 break; |
6803 case 'v': | |
6804 { | |
6805 colnr_T start_col = (lnum == min_pos.lnum) | |
6806 ? min_pos.col : 0; | |
6807 colnr_T end_col = (lnum == max_pos.lnum) | |
6808 ? max_pos.col - start_col + 1 : MAXCOL; | |
6809 | |
45 | 6810 s = ml_get(lnum) + start_col; |
6811 len = end_col; | |
7 | 6812 } |
6813 break; | |
6814 } | |
45 | 6815 if (s != NULL) |
6816 { | |
161 | 6817 byte_count_cursor += line_count_info(s, &word_count_cursor, |
6818 &char_count_cursor, len, eol_size); | |
45 | 6819 if (lnum == curbuf->b_ml.ml_line_count |
6820 && !curbuf->b_p_eol | |
6821 && curbuf->b_p_bin | |
50 | 6822 && (long)STRLEN(s) < len) |
161 | 6823 byte_count_cursor -= eol_size; |
45 | 6824 } |
7 | 6825 } |
6826 else | |
6827 { | |
6828 /* In non-visual mode, check for the line the cursor is on */ | |
6829 if (lnum == curwin->w_cursor.lnum) | |
6830 { | |
6831 word_count_cursor += word_count; | |
161 | 6832 char_count_cursor += char_count; |
6833 byte_count_cursor = byte_count + | |
6834 line_count_info(ml_get(lnum), | |
6835 &word_count_cursor, &char_count_cursor, | |
7 | 6836 (long)(curwin->w_cursor.col + 1), eol_size); |
6837 } | |
6838 } | |
6839 /* Add to the running totals */ | |
161 | 6840 byte_count += line_count_info(ml_get(lnum), &word_count, |
6841 &char_count, (long)MAXCOL, eol_size); | |
7 | 6842 } |
6843 | |
6844 /* Correction for when last line doesn't have an EOL. */ | |
6845 if (!curbuf->b_p_eol && curbuf->b_p_bin) | |
161 | 6846 byte_count -= eol_size; |
7 | 6847 |
6848 if (VIsual_active) | |
6849 { | |
688 | 6850 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL) |
7 | 6851 { |
6852 getvcols(curwin, &min_pos, &max_pos, &min_pos.col, | |
688 | 6853 &max_pos.col); |
1869 | 6854 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "), |
7 | 6855 (long)(oparg.end_vcol - oparg.start_vcol + 1)); |
6856 } | |
6857 else | |
6858 buf1[0] = NUL; | |
6859 | |
161 | 6860 if (char_count_cursor == byte_count_cursor |
274 | 6861 && char_count == byte_count) |
1869 | 6862 vim_snprintf((char *)IObuff, IOSIZE, |
6863 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Bytes"), | |
7 | 6864 buf1, line_count_selected, |
6865 (long)curbuf->b_ml.ml_line_count, | |
6866 word_count_cursor, word_count, | |
161 | 6867 byte_count_cursor, byte_count); |
6868 else | |
1869 | 6869 vim_snprintf((char *)IObuff, IOSIZE, |
6870 _("Selected %s%ld of %ld Lines; %ld of %ld Words; %ld of %ld Chars; %ld of %ld Bytes"), | |
161 | 6871 buf1, line_count_selected, |
6872 (long)curbuf->b_ml.ml_line_count, | |
6873 word_count_cursor, word_count, | |
6874 char_count_cursor, char_count, | |
6875 byte_count_cursor, byte_count); | |
7 | 6876 } |
6877 else | |
6878 { | |
6879 p = ml_get_curline(); | |
6880 validate_virtcol(); | |
1869 | 6881 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1, |
7 | 6882 (int)curwin->w_virtcol + 1); |
1869 | 6883 col_print(buf2, sizeof(buf2), (int)STRLEN(p), linetabsize(p)); |
7 | 6884 |
161 | 6885 if (char_count_cursor == byte_count_cursor |
6886 && char_count == byte_count) | |
1869 | 6887 vim_snprintf((char *)IObuff, IOSIZE, |
6888 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Byte %ld of %ld"), | |
7 | 6889 (char *)buf1, (char *)buf2, |
6890 (long)curwin->w_cursor.lnum, | |
6891 (long)curbuf->b_ml.ml_line_count, | |
6892 word_count_cursor, word_count, | |
161 | 6893 byte_count_cursor, byte_count); |
6894 else | |
1869 | 6895 vim_snprintf((char *)IObuff, IOSIZE, |
6896 _("Col %s of %s; Line %ld of %ld; Word %ld of %ld; Char %ld of %ld; Byte %ld of %ld"), | |
161 | 6897 (char *)buf1, (char *)buf2, |
6898 (long)curwin->w_cursor.lnum, | |
6899 (long)curbuf->b_ml.ml_line_count, | |
6900 word_count_cursor, word_count, | |
6901 char_count_cursor, char_count, | |
6902 byte_count_cursor, byte_count); | |
7 | 6903 } |
6904 | |
6905 #ifdef FEAT_MBYTE | |
161 | 6906 byte_count = bomb_size(); |
6907 if (byte_count > 0) | |
7 | 6908 sprintf((char *)IObuff + STRLEN(IObuff), _("(+%ld for BOM)"), |
161 | 6909 byte_count); |
7 | 6910 #endif |
6911 /* Don't shorten this message, the user asked for it. */ | |
6912 p = p_shm; | |
6913 p_shm = (char_u *)""; | |
6914 msg(IObuff); | |
6915 p_shm = p; | |
6916 } | |
6917 } |