Mercurial > vim
comparison src/indent.c @ 18265:fe5afdc03bd2 v8.1.2127
patch 8.1.2127: the indent.c file is a bit big
Commit: https://github.com/vim/vim/commit/14c01f83487d5c53192297a710eda2b8a4ab17c9
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Oct 9 22:53:08 2019 +0200
patch 8.1.2127: the indent.c file is a bit big
Problem: The indent.c file is a bit big.
Solution: Move C-indent code a a new cindent.c file. Move other
indent-related code to indent.c. (Yegappan Lakshmanan,
closes #5031)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 09 Oct 2019 23:00:04 +0200 |
parents | 079e10a49ea1 |
children | e9e60e0088b5 |
comparison
equal
deleted
inserted
replaced
18264:5202d9b99bee | 18265:fe5afdc03bd2 |
---|---|
11 * indent.c: Indentation related functions | 11 * indent.c: Indentation related functions |
12 */ | 12 */ |
13 | 13 |
14 #include "vim.h" | 14 #include "vim.h" |
15 | 15 |
16 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) | 16 #if defined(FEAT_VARTABS) || defined(PROTO) |
17 | 17 |
18 static int cin_iscase(char_u *s, int strict); | 18 /* |
19 static int cin_isscopedecl(char_u *s); | 19 * Set the integer values corresponding to the string setting of 'vartabstop'. |
20 | 20 * "array" will be set, caller must free it if needed. |
21 /* | |
22 * Return TRUE if the string "line" starts with a word from 'cinwords'. | |
23 */ | 21 */ |
24 int | 22 int |
25 cin_is_cinword(char_u *line) | 23 tabstop_set(char_u *var, int **array) |
26 { | 24 { |
27 char_u *cinw; | 25 int valcount = 1; |
28 char_u *cinw_buf; | 26 int t; |
29 int cinw_len; | 27 char_u *cp; |
28 | |
29 if (var[0] == NUL || (var[0] == '0' && var[1] == NUL)) | |
30 { | |
31 *array = NULL; | |
32 return TRUE; | |
33 } | |
34 | |
35 for (cp = var; *cp != NUL; ++cp) | |
36 { | |
37 if (cp == var || cp[-1] == ',') | |
38 { | |
39 char_u *end; | |
40 | |
41 if (strtol((char *)cp, (char **)&end, 10) <= 0) | |
42 { | |
43 if (cp != end) | |
44 emsg(_(e_positive)); | |
45 else | |
46 emsg(_(e_invarg)); | |
47 return FALSE; | |
48 } | |
49 } | |
50 | |
51 if (VIM_ISDIGIT(*cp)) | |
52 continue; | |
53 if (cp[0] == ',' && cp > var && cp[-1] != ',' && cp[1] != NUL) | |
54 { | |
55 ++valcount; | |
56 continue; | |
57 } | |
58 emsg(_(e_invarg)); | |
59 return FALSE; | |
60 } | |
61 | |
62 *array = ALLOC_MULT(int, valcount + 1); | |
63 if (*array == NULL) | |
64 return FALSE; | |
65 (*array)[0] = valcount; | |
66 | |
67 t = 1; | |
68 for (cp = var; *cp != NUL;) | |
69 { | |
70 (*array)[t++] = atoi((char *)cp); | |
71 while (*cp != NUL && *cp != ',') | |
72 ++cp; | |
73 if (*cp != NUL) | |
74 ++cp; | |
75 } | |
76 | |
77 return TRUE; | |
78 } | |
79 | |
80 /* | |
81 * Calculate the number of screen spaces a tab will occupy. | |
82 * If "vts" is set then the tab widths are taken from that array, | |
83 * otherwise the value of ts is used. | |
84 */ | |
85 int | |
86 tabstop_padding(colnr_T col, int ts_arg, int *vts) | |
87 { | |
88 int ts = ts_arg == 0 ? 8 : ts_arg; | |
89 int tabcount; | |
90 colnr_T tabcol = 0; | |
91 int t; | |
92 int padding = 0; | |
93 | |
94 if (vts == NULL || vts[0] == 0) | |
95 return ts - (col % ts); | |
96 | |
97 tabcount = vts[0]; | |
98 | |
99 for (t = 1; t <= tabcount; ++t) | |
100 { | |
101 tabcol += vts[t]; | |
102 if (tabcol > col) | |
103 { | |
104 padding = (int)(tabcol - col); | |
105 break; | |
106 } | |
107 } | |
108 if (t > tabcount) | |
109 padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]); | |
110 | |
111 return padding; | |
112 } | |
113 | |
114 /* | |
115 * Find the size of the tab that covers a particular column. | |
116 */ | |
117 int | |
118 tabstop_at(colnr_T col, int ts, int *vts) | |
119 { | |
120 int tabcount; | |
121 colnr_T tabcol = 0; | |
122 int t; | |
123 int tab_size = 0; | |
124 | |
125 if (vts == 0 || vts[0] == 0) | |
126 return ts; | |
127 | |
128 tabcount = vts[0]; | |
129 for (t = 1; t <= tabcount; ++t) | |
130 { | |
131 tabcol += vts[t]; | |
132 if (tabcol > col) | |
133 { | |
134 tab_size = vts[t]; | |
135 break; | |
136 } | |
137 } | |
138 if (t > tabcount) | |
139 tab_size = vts[tabcount]; | |
140 | |
141 return tab_size; | |
142 } | |
143 | |
144 /* | |
145 * Find the column on which a tab starts. | |
146 */ | |
147 colnr_T | |
148 tabstop_start(colnr_T col, int ts, int *vts) | |
149 { | |
150 int tabcount; | |
151 colnr_T tabcol = 0; | |
152 int t; | |
153 int excess; | |
154 | |
155 if (vts == NULL || vts[0] == 0) | |
156 return (col / ts) * ts; | |
157 | |
158 tabcount = vts[0]; | |
159 for (t = 1; t <= tabcount; ++t) | |
160 { | |
161 tabcol += vts[t]; | |
162 if (tabcol > col) | |
163 return tabcol - vts[t]; | |
164 } | |
165 | |
166 excess = tabcol % vts[tabcount]; | |
167 return excess + ((col - excess) / vts[tabcount]) * vts[tabcount]; | |
168 } | |
169 | |
170 /* | |
171 * Find the number of tabs and spaces necessary to get from one column | |
172 * to another. | |
173 */ | |
174 void | |
175 tabstop_fromto( | |
176 colnr_T start_col, | |
177 colnr_T end_col, | |
178 int ts_arg, | |
179 int *vts, | |
180 int *ntabs, | |
181 int *nspcs) | |
182 { | |
183 int spaces = end_col - start_col; | |
184 colnr_T tabcol = 0; | |
185 int padding = 0; | |
186 int tabcount; | |
187 int t; | |
188 int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg; | |
189 | |
190 if (vts == NULL || vts[0] == 0) | |
191 { | |
192 int tabs = 0; | |
193 int initspc = 0; | |
194 | |
195 initspc = ts - (start_col % ts); | |
196 if (spaces >= initspc) | |
197 { | |
198 spaces -= initspc; | |
199 tabs++; | |
200 } | |
201 tabs += spaces / ts; | |
202 spaces -= (spaces / ts) * ts; | |
203 | |
204 *ntabs = tabs; | |
205 *nspcs = spaces; | |
206 return; | |
207 } | |
208 | |
209 // Find the padding needed to reach the next tabstop. | |
210 tabcount = vts[0]; | |
211 for (t = 1; t <= tabcount; ++t) | |
212 { | |
213 tabcol += vts[t]; | |
214 if (tabcol > start_col) | |
215 { | |
216 padding = (int)(tabcol - start_col); | |
217 break; | |
218 } | |
219 } | |
220 if (t > tabcount) | |
221 padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]); | |
222 | |
223 // If the space needed is less than the padding no tabs can be used. | |
224 if (spaces < padding) | |
225 { | |
226 *ntabs = 0; | |
227 *nspcs = spaces; | |
228 return; | |
229 } | |
230 | |
231 *ntabs = 1; | |
232 spaces -= padding; | |
233 | |
234 // At least one tab has been used. See if any more will fit. | |
235 while (spaces != 0 && ++t <= tabcount) | |
236 { | |
237 padding = vts[t]; | |
238 if (spaces < padding) | |
239 { | |
240 *nspcs = spaces; | |
241 return; | |
242 } | |
243 ++*ntabs; | |
244 spaces -= padding; | |
245 } | |
246 | |
247 *ntabs += spaces / vts[tabcount]; | |
248 *nspcs = spaces % vts[tabcount]; | |
249 } | |
250 | |
251 /* | |
252 * See if two tabstop arrays contain the same values. | |
253 */ | |
254 static int | |
255 tabstop_eq(int *ts1, int *ts2) | |
256 { | |
257 int t; | |
258 | |
259 if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0)) | |
260 return FALSE; | |
261 if (ts1 == ts2) | |
262 return TRUE; | |
263 if (ts1[0] != ts2[0]) | |
264 return FALSE; | |
265 | |
266 for (t = 1; t <= ts1[0]; ++t) | |
267 if (ts1[t] != ts2[t]) | |
268 return FALSE; | |
269 | |
270 return TRUE; | |
271 } | |
272 | |
273 # if defined(FEAT_BEVAL) || defined(PROTO) | |
274 /* | |
275 * Copy a tabstop array, allocating space for the new array. | |
276 */ | |
277 int * | |
278 tabstop_copy(int *oldts) | |
279 { | |
280 int *newts; | |
281 int t; | |
282 | |
283 if (oldts == NULL) | |
284 return NULL; | |
285 newts = ALLOC_MULT(int, oldts[0] + 1); | |
286 if (newts != NULL) | |
287 for (t = 0; t <= oldts[0]; ++t) | |
288 newts[t] = oldts[t]; | |
289 return newts; | |
290 } | |
291 # endif | |
292 | |
293 /* | |
294 * Return a count of the number of tabstops. | |
295 */ | |
296 int | |
297 tabstop_count(int *ts) | |
298 { | |
299 return ts != NULL ? ts[0] : 0; | |
300 } | |
301 | |
302 /* | |
303 * Return the first tabstop, or 8 if there are no tabstops defined. | |
304 */ | |
305 int | |
306 tabstop_first(int *ts) | |
307 { | |
308 return ts != NULL ? ts[1] : 8; | |
309 } | |
310 | |
311 #endif | |
312 | |
313 /* | |
314 * Return the effective shiftwidth value for current buffer, using the | |
315 * 'tabstop' value when 'shiftwidth' is zero. | |
316 */ | |
317 long | |
318 get_sw_value(buf_T *buf) | |
319 { | |
320 return get_sw_value_col(buf, 0); | |
321 } | |
322 | |
323 /* | |
324 * Idem, using "pos". | |
325 */ | |
326 static long | |
327 get_sw_value_pos(buf_T *buf, pos_T *pos) | |
328 { | |
329 pos_T save_cursor = curwin->w_cursor; | |
330 long sw_value; | |
331 | |
332 curwin->w_cursor = *pos; | |
333 sw_value = get_sw_value_col(buf, get_nolist_virtcol()); | |
334 curwin->w_cursor = save_cursor; | |
335 return sw_value; | |
336 } | |
337 | |
338 /* | |
339 * Idem, using the first non-black in the current line. | |
340 */ | |
341 long | |
342 get_sw_value_indent(buf_T *buf) | |
343 { | |
344 pos_T pos = curwin->w_cursor; | |
345 | |
346 pos.col = getwhitecols_curline(); | |
347 return get_sw_value_pos(buf, &pos); | |
348 } | |
349 | |
350 /* | |
351 * Idem, using virtual column "col". | |
352 */ | |
353 long | |
354 get_sw_value_col(buf_T *buf, colnr_T col UNUSED) | |
355 { | |
356 return buf->b_p_sw ? buf->b_p_sw : | |
357 #ifdef FEAT_VARTABS | |
358 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array); | |
359 #else | |
360 buf->b_p_ts; | |
361 #endif | |
362 } | |
363 | |
364 /* | |
365 * Return the effective softtabstop value for the current buffer, using the | |
366 * 'shiftwidth' value when 'softtabstop' is negative. | |
367 */ | |
368 long | |
369 get_sts_value(void) | |
370 { | |
371 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts; | |
372 } | |
373 | |
374 /* | |
375 * Count the size (in window cells) of the indent in the current line. | |
376 */ | |
377 int | |
378 get_indent(void) | |
379 { | |
380 #ifdef FEAT_VARTABS | |
381 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts, | |
382 curbuf->b_p_vts_array, FALSE); | |
383 #else | |
384 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE); | |
385 #endif | |
386 } | |
387 | |
388 /* | |
389 * Count the size (in window cells) of the indent in line "lnum". | |
390 */ | |
391 int | |
392 get_indent_lnum(linenr_T lnum) | |
393 { | |
394 #ifdef FEAT_VARTABS | |
395 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts, | |
396 curbuf->b_p_vts_array, FALSE); | |
397 #else | |
398 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE); | |
399 #endif | |
400 } | |
401 | |
402 #if defined(FEAT_FOLDING) || defined(PROTO) | |
403 /* | |
404 * Count the size (in window cells) of the indent in line "lnum" of buffer | |
405 * "buf". | |
406 */ | |
407 int | |
408 get_indent_buf(buf_T *buf, linenr_T lnum) | |
409 { | |
410 # ifdef FEAT_VARTABS | |
411 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE), | |
412 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE); | |
413 # else | |
414 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE); | |
415 # endif | |
416 } | |
417 #endif | |
418 | |
419 /* | |
420 * count the size (in window cells) of the indent in line "ptr", with | |
421 * 'tabstop' at "ts" | |
422 */ | |
423 int | |
424 get_indent_str( | |
425 char_u *ptr, | |
426 int ts, | |
427 int list) // if TRUE, count only screen size for tabs | |
428 { | |
429 int count = 0; | |
430 | |
431 for ( ; *ptr; ++ptr) | |
432 { | |
433 if (*ptr == TAB) | |
434 { | |
435 if (!list || lcs_tab1) // count a tab for what it is worth | |
436 count += ts - (count % ts); | |
437 else | |
438 // In list mode, when tab is not set, count screen char width | |
439 // for Tab, displays: ^I | |
440 count += ptr2cells(ptr); | |
441 } | |
442 else if (*ptr == ' ') | |
443 ++count; // count a space for one | |
444 else | |
445 break; | |
446 } | |
447 return count; | |
448 } | |
449 | |
450 #ifdef FEAT_VARTABS | |
451 /* | |
452 * Count the size (in window cells) of the indent in line "ptr", using | |
453 * variable tabstops. | |
454 * if "list" is TRUE, count only screen size for tabs. | |
455 */ | |
456 int | |
457 get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list) | |
458 { | |
459 int count = 0; | |
460 | |
461 for ( ; *ptr; ++ptr) | |
462 { | |
463 if (*ptr == TAB) // count a tab for what it is worth | |
464 { | |
465 if (!list || lcs_tab1) | |
466 count += tabstop_padding(count, ts, vts); | |
467 else | |
468 // In list mode, when tab is not set, count screen char width | |
469 // for Tab, displays: ^I | |
470 count += ptr2cells(ptr); | |
471 } | |
472 else if (*ptr == ' ') | |
473 ++count; // count a space for one | |
474 else | |
475 break; | |
476 } | |
477 return count; | |
478 } | |
479 #endif | |
480 | |
481 /* | |
482 * Set the indent of the current line. | |
483 * Leaves the cursor on the first non-blank in the line. | |
484 * Caller must take care of undo. | |
485 * "flags": | |
486 * SIN_CHANGED: call changed_bytes() if the line was changed. | |
487 * SIN_INSERT: insert the indent in front of the line. | |
488 * SIN_UNDO: save line for undo before changing it. | |
489 * Returns TRUE if the line was changed. | |
490 */ | |
491 int | |
492 set_indent( | |
493 int size, // measured in spaces | |
494 int flags) | |
495 { | |
496 char_u *p; | |
497 char_u *newline; | |
498 char_u *oldline; | |
499 char_u *s; | |
500 int todo; | |
501 int ind_len; // measured in characters | |
502 int line_len; | |
503 int doit = FALSE; | |
504 int ind_done = 0; // measured in spaces | |
505 #ifdef FEAT_VARTABS | |
506 int ind_col = 0; | |
507 #endif | |
508 int tab_pad; | |
30 int retval = FALSE; | 509 int retval = FALSE; |
31 int len; | 510 int orig_char_len = -1; // number of initial whitespace chars when |
32 | 511 // 'et' and 'pi' are both set |
33 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1; | 512 |
34 cinw_buf = alloc(cinw_len); | 513 // First check if there is anything to do and compute the number of |
35 if (cinw_buf != NULL) | 514 // characters needed for the indent. |
36 { | 515 todo = size; |
37 line = skipwhite(line); | 516 ind_len = 0; |
38 for (cinw = curbuf->b_p_cinw; *cinw; ) | 517 p = oldline = ml_get_curline(); |
39 { | 518 |
40 len = copy_option_part(&cinw, cinw_buf, cinw_len, ","); | 519 // Calculate the buffer size for the new indent, and check to see if it |
41 if (STRNCMP(line, cinw_buf, len) == 0 | 520 // isn't already set |
42 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1]))) | 521 |
522 // if 'expandtab' isn't set: use TABs; if both 'expandtab' and | |
523 // 'preserveindent' are set count the number of characters at the | |
524 // beginning of the line to be copied | |
525 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi)) | |
526 { | |
527 // If 'preserveindent' is set then reuse as much as possible of | |
528 // the existing indent structure for the new indent | |
529 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
530 { | |
531 ind_done = 0; | |
532 | |
533 // count as many characters as we can use | |
534 while (todo > 0 && VIM_ISWHITE(*p)) | |
43 { | 535 { |
44 retval = TRUE; | 536 if (*p == TAB) |
537 { | |
538 #ifdef FEAT_VARTABS | |
539 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, | |
540 curbuf->b_p_vts_array); | |
541 #else | |
542 tab_pad = (int)curbuf->b_p_ts | |
543 - (ind_done % (int)curbuf->b_p_ts); | |
544 #endif | |
545 // stop if this tab will overshoot the target | |
546 if (todo < tab_pad) | |
547 break; | |
548 todo -= tab_pad; | |
549 ++ind_len; | |
550 ind_done += tab_pad; | |
551 } | |
552 else | |
553 { | |
554 --todo; | |
555 ++ind_len; | |
556 ++ind_done; | |
557 } | |
558 ++p; | |
559 } | |
560 | |
561 #ifdef FEAT_VARTABS | |
562 // These diverge from this point. | |
563 ind_col = ind_done; | |
564 #endif | |
565 // Set initial number of whitespace chars to copy if we are | |
566 // preserving indent but expandtab is set | |
567 if (curbuf->b_p_et) | |
568 orig_char_len = ind_len; | |
569 | |
570 // Fill to next tabstop with a tab, if possible | |
571 #ifdef FEAT_VARTABS | |
572 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, | |
573 curbuf->b_p_vts_array); | |
574 #else | |
575 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
576 #endif | |
577 if (todo >= tab_pad && orig_char_len == -1) | |
578 { | |
579 doit = TRUE; | |
580 todo -= tab_pad; | |
581 ++ind_len; | |
582 // ind_done += tab_pad; | |
583 #ifdef FEAT_VARTABS | |
584 ind_col += tab_pad; | |
585 #endif | |
586 } | |
587 } | |
588 | |
589 // count tabs required for indent | |
590 #ifdef FEAT_VARTABS | |
591 for (;;) | |
592 { | |
593 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, | |
594 curbuf->b_p_vts_array); | |
595 if (todo < tab_pad) | |
45 break; | 596 break; |
597 if (*p != TAB) | |
598 doit = TRUE; | |
599 else | |
600 ++p; | |
601 todo -= tab_pad; | |
602 ++ind_len; | |
603 ind_col += tab_pad; | |
604 } | |
605 #else | |
606 while (todo >= (int)curbuf->b_p_ts) | |
607 { | |
608 if (*p != TAB) | |
609 doit = TRUE; | |
610 else | |
611 ++p; | |
612 todo -= (int)curbuf->b_p_ts; | |
613 ++ind_len; | |
614 // ind_done += (int)curbuf->b_p_ts; | |
615 } | |
616 #endif | |
617 } | |
618 // count spaces required for indent | |
619 while (todo > 0) | |
620 { | |
621 if (*p != ' ') | |
622 doit = TRUE; | |
623 else | |
624 ++p; | |
625 --todo; | |
626 ++ind_len; | |
627 // ++ind_done; | |
628 } | |
629 | |
630 // Return if the indent is OK already. | |
631 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT)) | |
632 return FALSE; | |
633 | |
634 // Allocate memory for the new line. | |
635 if (flags & SIN_INSERT) | |
636 p = oldline; | |
637 else | |
638 p = skipwhite(p); | |
639 line_len = (int)STRLEN(p) + 1; | |
640 | |
641 // If 'preserveindent' and 'expandtab' are both set keep the original | |
642 // characters and allocate accordingly. We will fill the rest with spaces | |
643 // after the if (!curbuf->b_p_et) below. | |
644 if (orig_char_len != -1) | |
645 { | |
646 newline = alloc(orig_char_len + size - ind_done + line_len); | |
647 if (newline == NULL) | |
648 return FALSE; | |
649 todo = size - ind_done; | |
650 ind_len = orig_char_len + todo; // Set total length of indent in | |
651 // characters, which may have been | |
652 // undercounted until now | |
653 p = oldline; | |
654 s = newline; | |
655 while (orig_char_len > 0) | |
656 { | |
657 *s++ = *p++; | |
658 orig_char_len--; | |
659 } | |
660 | |
661 // Skip over any additional white space (useful when newindent is less | |
662 // than old) | |
663 while (VIM_ISWHITE(*p)) | |
664 ++p; | |
665 | |
666 } | |
667 else | |
668 { | |
669 todo = size; | |
670 newline = alloc(ind_len + line_len); | |
671 if (newline == NULL) | |
672 return FALSE; | |
673 s = newline; | |
674 } | |
675 | |
676 // Put the characters in the new line. | |
677 // if 'expandtab' isn't set: use TABs | |
678 if (!curbuf->b_p_et) | |
679 { | |
680 // If 'preserveindent' is set then reuse as much as possible of | |
681 // the existing indent structure for the new indent | |
682 if (!(flags & SIN_INSERT) && curbuf->b_p_pi) | |
683 { | |
684 p = oldline; | |
685 ind_done = 0; | |
686 | |
687 while (todo > 0 && VIM_ISWHITE(*p)) | |
688 { | |
689 if (*p == TAB) | |
690 { | |
691 #ifdef FEAT_VARTABS | |
692 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, | |
693 curbuf->b_p_vts_array); | |
694 #else | |
695 tab_pad = (int)curbuf->b_p_ts | |
696 - (ind_done % (int)curbuf->b_p_ts); | |
697 #endif | |
698 // stop if this tab will overshoot the target | |
699 if (todo < tab_pad) | |
700 break; | |
701 todo -= tab_pad; | |
702 ind_done += tab_pad; | |
703 } | |
704 else | |
705 { | |
706 --todo; | |
707 ++ind_done; | |
708 } | |
709 *s++ = *p++; | |
46 } | 710 } |
47 } | 711 |
48 vim_free(cinw_buf); | 712 // Fill to next tabstop with a tab, if possible |
49 } | 713 #ifdef FEAT_VARTABS |
714 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, | |
715 curbuf->b_p_vts_array); | |
716 #else | |
717 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
718 #endif | |
719 if (todo >= tab_pad) | |
720 { | |
721 *s++ = TAB; | |
722 todo -= tab_pad; | |
723 #ifdef FEAT_VARTABS | |
724 ind_done += tab_pad; | |
725 #endif | |
726 } | |
727 | |
728 p = skipwhite(p); | |
729 } | |
730 | |
731 #ifdef FEAT_VARTABS | |
732 for (;;) | |
733 { | |
734 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, | |
735 curbuf->b_p_vts_array); | |
736 if (todo < tab_pad) | |
737 break; | |
738 *s++ = TAB; | |
739 todo -= tab_pad; | |
740 ind_done += tab_pad; | |
741 } | |
742 #else | |
743 while (todo >= (int)curbuf->b_p_ts) | |
744 { | |
745 *s++ = TAB; | |
746 todo -= (int)curbuf->b_p_ts; | |
747 } | |
748 #endif | |
749 } | |
750 while (todo > 0) | |
751 { | |
752 *s++ = ' '; | |
753 --todo; | |
754 } | |
755 mch_memmove(s, p, (size_t)line_len); | |
756 | |
757 // Replace the line (unless undo fails). | |
758 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK) | |
759 { | |
760 ml_replace(curwin->w_cursor.lnum, newline, FALSE); | |
761 if (flags & SIN_CHANGED) | |
762 changed_bytes(curwin->w_cursor.lnum, 0); | |
763 | |
764 // Correct saved cursor position if it is in this line. | |
765 if (saved_cursor.lnum == curwin->w_cursor.lnum) | |
766 { | |
767 if (saved_cursor.col >= (colnr_T)(p - oldline)) | |
768 // cursor was after the indent, adjust for the number of | |
769 // bytes added/removed | |
770 saved_cursor.col += ind_len - (colnr_T)(p - oldline); | |
771 else if (saved_cursor.col >= (colnr_T)(s - newline)) | |
772 // cursor was in the indent, and is now after it, put it back | |
773 // at the start of the indent (replacing spaces with TAB) | |
774 saved_cursor.col = (colnr_T)(s - newline); | |
775 } | |
776 #ifdef FEAT_TEXT_PROP | |
777 { | |
778 int added = ind_len - (colnr_T)(p - oldline); | |
779 | |
780 // When increasing indent this behaves like spaces were inserted at | |
781 // the old indent, when decreasing indent it behaves like spaces | |
782 // were deleted at the new indent. | |
783 adjust_prop_columns(curwin->w_cursor.lnum, | |
784 (colnr_T)(added > 0 ? (p - oldline) : ind_len), added, 0); | |
785 } | |
786 #endif | |
787 retval = TRUE; | |
788 } | |
789 else | |
790 vim_free(newline); | |
791 | |
792 curwin->w_cursor.col = ind_len; | |
50 return retval; | 793 return retval; |
51 } | 794 } |
52 #endif | 795 |
53 | 796 /* |
54 #if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL) | 797 * Return the indent of the current line after a number. Return -1 if no |
55 | 798 * number was found. Used for 'n' in 'formatoptions': numbered list. |
56 static char_u *skip_string(char_u *p); | 799 * Since a pattern is used it can actually handle more than numbers. |
57 static pos_T *find_start_rawstring(int ind_maxcomment); | |
58 | |
59 /* | |
60 * Find the start of a comment, not knowing if we are in a comment right now. | |
61 * Search starts at w_cursor.lnum and goes backwards. | |
62 * Return NULL when not inside a comment. | |
63 */ | |
64 static pos_T * | |
65 ind_find_start_comment(void) // XXX | |
66 { | |
67 return find_start_comment(curbuf->b_ind_maxcomment); | |
68 } | |
69 | |
70 pos_T * | |
71 find_start_comment(int ind_maxcomment) // XXX | |
72 { | |
73 pos_T *pos; | |
74 char_u *line; | |
75 char_u *p; | |
76 int cur_maxcomment = ind_maxcomment; | |
77 | |
78 for (;;) | |
79 { | |
80 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment); | |
81 if (pos == NULL) | |
82 break; | |
83 | |
84 // Check if the comment start we found is inside a string. | |
85 // If it is then restrict the search to below this line and try again. | |
86 line = ml_get(pos->lnum); | |
87 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) | |
88 p = skip_string(p); | |
89 if ((colnr_T)(p - line) <= pos->col) | |
90 break; | |
91 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; | |
92 if (cur_maxcomment <= 0) | |
93 { | |
94 pos = NULL; | |
95 break; | |
96 } | |
97 } | |
98 return pos; | |
99 } | |
100 | |
101 /* | |
102 * Find the start of a comment or raw string, not knowing if we are in a | |
103 * comment or raw string right now. | |
104 * Search starts at w_cursor.lnum and goes backwards. | |
105 * If is_raw is given and returns start of raw_string, sets it to true. | |
106 * Return NULL when not inside a comment or raw string. | |
107 * "CORS" -> Comment Or Raw String | |
108 */ | |
109 static pos_T * | |
110 ind_find_start_CORS(linenr_T *is_raw) // XXX | |
111 { | |
112 static pos_T comment_pos_copy; | |
113 pos_T *comment_pos; | |
114 pos_T *rs_pos; | |
115 | |
116 comment_pos = find_start_comment(curbuf->b_ind_maxcomment); | |
117 if (comment_pos != NULL) | |
118 { | |
119 // Need to make a copy of the static pos in findmatchlimit(), | |
120 // calling find_start_rawstring() may change it. | |
121 comment_pos_copy = *comment_pos; | |
122 comment_pos = &comment_pos_copy; | |
123 } | |
124 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment); | |
125 | |
126 // If comment_pos is before rs_pos the raw string is inside the comment. | |
127 // If rs_pos is before comment_pos the comment is inside the raw string. | |
128 if (comment_pos == NULL || (rs_pos != NULL | |
129 && LT_POS(*rs_pos, *comment_pos))) | |
130 { | |
131 if (is_raw != NULL && rs_pos != NULL) | |
132 *is_raw = rs_pos->lnum; | |
133 return rs_pos; | |
134 } | |
135 return comment_pos; | |
136 } | |
137 | |
138 /* | |
139 * Find the start of a raw string, not knowing if we are in one right now. | |
140 * Search starts at w_cursor.lnum and goes backwards. | |
141 * Return NULL when not inside a raw string. | |
142 */ | |
143 static pos_T * | |
144 find_start_rawstring(int ind_maxcomment) // XXX | |
145 { | |
146 pos_T *pos; | |
147 char_u *line; | |
148 char_u *p; | |
149 int cur_maxcomment = ind_maxcomment; | |
150 | |
151 for (;;) | |
152 { | |
153 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment); | |
154 if (pos == NULL) | |
155 break; | |
156 | |
157 // Check if the raw string start we found is inside a string. | |
158 // If it is then restrict the search to below this line and try again. | |
159 line = ml_get(pos->lnum); | |
160 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) | |
161 p = skip_string(p); | |
162 if ((colnr_T)(p - line) <= pos->col) | |
163 break; | |
164 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; | |
165 if (cur_maxcomment <= 0) | |
166 { | |
167 pos = NULL; | |
168 break; | |
169 } | |
170 } | |
171 return pos; | |
172 } | |
173 | |
174 /* | |
175 * Skip to the end of a "string" and a 'c' character. | |
176 * If there is no string or character, return argument unmodified. | |
177 */ | |
178 static char_u * | |
179 skip_string(char_u *p) | |
180 { | |
181 int i; | |
182 | |
183 // We loop, because strings may be concatenated: "date""time". | |
184 for ( ; ; ++p) | |
185 { | |
186 if (p[0] == '\'') // 'c' or '\n' or '\000' | |
187 { | |
188 if (!p[1]) // ' at end of line | |
189 break; | |
190 i = 2; | |
191 if (p[1] == '\\') // '\n' or '\000' | |
192 { | |
193 ++i; | |
194 while (vim_isdigit(p[i - 1])) // '\000' | |
195 ++i; | |
196 } | |
197 if (p[i] == '\'') // check for trailing ' | |
198 { | |
199 p += i; | |
200 continue; | |
201 } | |
202 } | |
203 else if (p[0] == '"') // start of string | |
204 { | |
205 for (++p; p[0]; ++p) | |
206 { | |
207 if (p[0] == '\\' && p[1] != NUL) | |
208 ++p; | |
209 else if (p[0] == '"') // end of string | |
210 break; | |
211 } | |
212 if (p[0] == '"') | |
213 continue; // continue for another string | |
214 } | |
215 else if (p[0] == 'R' && p[1] == '"') | |
216 { | |
217 // Raw string: R"[delim](...)[delim]" | |
218 char_u *delim = p + 2; | |
219 char_u *paren = vim_strchr(delim, '('); | |
220 | |
221 if (paren != NULL) | |
222 { | |
223 size_t delim_len = paren - delim; | |
224 | |
225 for (p += 3; *p; ++p) | |
226 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 | |
227 && p[delim_len + 1] == '"') | |
228 { | |
229 p += delim_len + 1; | |
230 break; | |
231 } | |
232 if (p[0] == '"') | |
233 continue; // continue for another string | |
234 } | |
235 } | |
236 break; // no string found | |
237 } | |
238 if (!*p) | |
239 --p; // backup from NUL | |
240 return p; | |
241 } | |
242 #endif // FEAT_CINDENT || FEAT_SYN_HL | |
243 | |
244 #if defined(FEAT_CINDENT) || defined(PROTO) | |
245 | |
246 /* | |
247 * Return TRUE if C-indenting is on. | |
248 */ | 800 */ |
249 int | 801 int |
250 cindent_on(void) | 802 get_number_indent(linenr_T lnum) |
251 { | 803 { |
252 return (!p_paste && (curbuf->b_p_cin | 804 colnr_T col; |
253 # ifdef FEAT_EVAL | 805 pos_T pos; |
254 || *curbuf->b_p_inde != NUL | 806 |
807 regmatch_T regmatch; | |
808 int lead_len = 0; // length of comment leader | |
809 | |
810 if (lnum > curbuf->b_ml.ml_line_count) | |
811 return -1; | |
812 pos.lnum = 0; | |
813 | |
814 // In format_lines() (i.e. not insert mode), fo+=q is needed too... | |
815 if ((State & INSERT) || has_format_option(FO_Q_COMS)) | |
816 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); | |
817 | |
818 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); | |
819 if (regmatch.regprog != NULL) | |
820 { | |
821 regmatch.rm_ic = FALSE; | |
822 | |
823 // vim_regexec() expects a pointer to a line. This lets us | |
824 // start matching for the flp beyond any comment leader... | |
825 if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) | |
826 { | |
827 pos.lnum = lnum; | |
828 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); | |
829 pos.coladd = 0; | |
830 } | |
831 vim_regfree(regmatch.regprog); | |
832 } | |
833 | |
834 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) | |
835 return -1; | |
836 getvcol(curwin, &pos, &col, NULL, NULL); | |
837 return (int)col; | |
838 } | |
839 | |
840 #if defined(FEAT_LINEBREAK) || defined(PROTO) | |
841 /* | |
842 * Return appropriate space number for breakindent, taking influencing | |
843 * parameters into account. Window must be specified, since it is not | |
844 * necessarily always the current one. | |
845 */ | |
846 int | |
847 get_breakindent_win( | |
848 win_T *wp, | |
849 char_u *line) // start of the line | |
850 { | |
851 static int prev_indent = 0; // cached indent value | |
852 static long prev_ts = 0L; // cached tabstop value | |
853 static char_u *prev_line = NULL; // cached pointer to line | |
854 static varnumber_T prev_tick = 0; // changedtick of cached value | |
855 # ifdef FEAT_VARTABS | |
856 static int *prev_vts = NULL; // cached vartabs values | |
255 # endif | 857 # endif |
256 )); | 858 int bri = 0; |
257 } | 859 // window width minus window margin space, i.e. what rests for text |
258 | 860 const int eff_wwidth = wp->w_width |
259 // Find result cache for cpp_baseclass | 861 - ((wp->w_p_nu || wp->w_p_rnu) |
260 typedef struct { | 862 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL) |
261 int found; | 863 ? number_width(wp) + 1 : 0); |
262 lpos_T lpos; | 864 |
263 } cpp_baseclass_cache_T; | 865 // used cached indent, unless pointer or 'tabstop' changed |
264 | 866 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts |
265 /* | 867 || prev_tick != CHANGEDTICK(wp->w_buffer) |
266 * Functions for C-indenting. | 868 # ifdef FEAT_VARTABS |
267 * Most of this originally comes from Eric Fischer. | 869 || prev_vts != wp->w_buffer->b_p_vts_array |
268 */ | 870 # endif |
269 /* | 871 ) |
270 * Below "XXX" means that this function may unlock the current line. | 872 { |
271 */ | 873 prev_line = line; |
272 | 874 prev_ts = wp->w_buffer->b_p_ts; |
273 static int cin_isdefault(char_u *); | 875 prev_tick = CHANGEDTICK(wp->w_buffer); |
274 static int cin_ispreproc(char_u *); | 876 # ifdef FEAT_VARTABS |
275 static int cin_iscomment(char_u *); | 877 prev_vts = wp->w_buffer->b_p_vts_array; |
276 static int cin_islinecomment(char_u *); | 878 prev_indent = get_indent_str_vtab(line, |
277 static int cin_isterminated(char_u *, int, int); | 879 (int)wp->w_buffer->b_p_ts, |
278 static int cin_iselse(char_u *); | 880 wp->w_buffer->b_p_vts_array, wp->w_p_list); |
279 static int cin_ends_in(char_u *, char_u *, char_u *); | 881 # else |
280 static int cin_starts_with(char_u *s, char *word); | 882 prev_indent = get_indent_str(line, |
281 static pos_T *find_match_paren(int); | 883 (int)wp->w_buffer->b_p_ts, wp->w_p_list); |
282 static pos_T *find_match_char(int c, int ind_maxparen); | 884 # endif |
283 static int find_last_paren(char_u *l, int start, int end); | 885 } |
284 static int find_match(int lookfor, linenr_T ourscope); | 886 bri = prev_indent + wp->w_p_brishift; |
285 | 887 |
286 /* | 888 // indent minus the length of the showbreak string |
287 * Skip over white space and C comments within the line. | 889 if (wp->w_p_brisbr) |
288 * Also skip over Perl/shell comments if desired. | 890 bri -= vim_strsize(p_sbr); |
289 */ | 891 |
290 static char_u * | 892 // Add offset for number column, if 'n' is in 'cpoptions' |
291 cin_skipcomment(char_u *s) | 893 bri += win_col_off2(wp); |
292 { | 894 |
293 while (*s) | 895 // never indent past left window margin |
294 { | 896 if (bri < 0) |
295 char_u *prev_s = s; | 897 bri = 0; |
296 | 898 // always leave at least bri_min characters on the left, |
297 s = skipwhite(s); | 899 // if text width is sufficient |
298 | 900 else if (bri > eff_wwidth - wp->w_p_brimin) |
299 // Perl/shell # comment comment continues until eol. Require a space | 901 bri = (eff_wwidth - wp->w_p_brimin < 0) |
300 // before # to avoid recognizing $#array. | 902 ? 0 : eff_wwidth - wp->w_p_brimin; |
301 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#') | 903 |
302 { | 904 return bri; |
303 s += STRLEN(s); | 905 } |
304 break; | 906 #endif |
305 } | 907 |
306 if (*s != '/') | 908 /* |
307 break; | 909 * When extra == 0: Return TRUE if the cursor is before or on the first |
308 ++s; | 910 * non-blank in the line. |
309 if (*s == '/') // slash-slash comment continues till eol | 911 * When extra == 1: Return TRUE if the cursor is before the first non-blank in |
310 { | 912 * the line. |
311 s += STRLEN(s); | 913 */ |
312 break; | 914 int |
313 } | 915 inindent(int extra) |
314 if (*s != '*') | 916 { |
315 break; | 917 char_u *ptr; |
316 for (++s; *s; ++s) // skip slash-star comment | 918 colnr_T col; |
317 if (s[0] == '*' && s[1] == '/') | 919 |
318 { | 920 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col) |
319 s += 2; | 921 ++ptr; |
320 break; | 922 if (col >= curwin->w_cursor.col + extra) |
321 } | |
322 } | |
323 return s; | |
324 } | |
325 | |
326 /* | |
327 * Return TRUE if there is no code at *s. White space and comments are | |
328 * not considered code. | |
329 */ | |
330 static int | |
331 cin_nocode(char_u *s) | |
332 { | |
333 return *cin_skipcomment(s) == NUL; | |
334 } | |
335 | |
336 /* | |
337 * Check previous lines for a "//" line comment, skipping over blank lines. | |
338 */ | |
339 static pos_T * | |
340 find_line_comment(void) // XXX | |
341 { | |
342 static pos_T pos; | |
343 char_u *line; | |
344 char_u *p; | |
345 | |
346 pos = curwin->w_cursor; | |
347 while (--pos.lnum > 0) | |
348 { | |
349 line = ml_get(pos.lnum); | |
350 p = skipwhite(line); | |
351 if (cin_islinecomment(p)) | |
352 { | |
353 pos.col = (int)(p - line); | |
354 return &pos; | |
355 } | |
356 if (*p != NUL) | |
357 break; | |
358 } | |
359 return NULL; | |
360 } | |
361 | |
362 /* | |
363 * Return TRUE if "text" starts with "key:". | |
364 */ | |
365 static int | |
366 cin_has_js_key(char_u *text) | |
367 { | |
368 char_u *s = skipwhite(text); | |
369 int quote = -1; | |
370 | |
371 if (*s == '\'' || *s == '"') | |
372 { | |
373 // can be 'key': or "key": | |
374 quote = *s; | |
375 ++s; | |
376 } | |
377 if (!vim_isIDc(*s)) // need at least one ID character | |
378 return FALSE; | |
379 | |
380 while (vim_isIDc(*s)) | |
381 ++s; | |
382 if (*s == quote) | |
383 ++s; | |
384 | |
385 s = cin_skipcomment(s); | |
386 | |
387 // "::" is not a label, it's C++ | |
388 return (*s == ':' && s[1] != ':'); | |
389 } | |
390 | |
391 /* | |
392 * Check if string matches "label:"; move to character after ':' if true. | |
393 * "*s" must point to the start of the label, if there is one. | |
394 */ | |
395 static int | |
396 cin_islabel_skip(char_u **s) | |
397 { | |
398 if (!vim_isIDc(**s)) // need at least one ID character | |
399 return FALSE; | |
400 | |
401 while (vim_isIDc(**s)) | |
402 (*s)++; | |
403 | |
404 *s = cin_skipcomment(*s); | |
405 | |
406 // "::" is not a label, it's C++ | |
407 return (**s == ':' && *++*s != ':'); | |
408 } | |
409 | |
410 /* | |
411 * Recognize a label: "label:". | |
412 * Note: curwin->w_cursor must be where we are looking for the label. | |
413 */ | |
414 static int | |
415 cin_islabel(void) // XXX | |
416 { | |
417 char_u *s; | |
418 | |
419 s = cin_skipcomment(ml_get_curline()); | |
420 | |
421 // Exclude "default" from labels, since it should be indented | |
422 // like a switch label. Same for C++ scope declarations. | |
423 if (cin_isdefault(s)) | |
424 return FALSE; | |
425 if (cin_isscopedecl(s)) | |
426 return FALSE; | |
427 | |
428 if (cin_islabel_skip(&s)) | |
429 { | |
430 // Only accept a label if the previous line is terminated or is a case | |
431 // label. | |
432 pos_T cursor_save; | |
433 pos_T *trypos; | |
434 char_u *line; | |
435 | |
436 cursor_save = curwin->w_cursor; | |
437 while (curwin->w_cursor.lnum > 1) | |
438 { | |
439 --curwin->w_cursor.lnum; | |
440 | |
441 // If we're in a comment or raw string now, skip to the start of | |
442 // it. | |
443 curwin->w_cursor.col = 0; | |
444 if ((trypos = ind_find_start_CORS(NULL)) != NULL) // XXX | |
445 curwin->w_cursor = *trypos; | |
446 | |
447 line = ml_get_curline(); | |
448 if (cin_ispreproc(line)) // ignore #defines, #if, etc. | |
449 continue; | |
450 if (*(line = cin_skipcomment(line)) == NUL) | |
451 continue; | |
452 | |
453 curwin->w_cursor = cursor_save; | |
454 if (cin_isterminated(line, TRUE, FALSE) | |
455 || cin_isscopedecl(line) | |
456 || cin_iscase(line, TRUE) | |
457 || (cin_islabel_skip(&line) && cin_nocode(line))) | |
458 return TRUE; | |
459 return FALSE; | |
460 } | |
461 curwin->w_cursor = cursor_save; | |
462 return TRUE; // label at start of file??? | |
463 } | |
464 return FALSE; | |
465 } | |
466 | |
467 /* | |
468 * Recognize structure initialization and enumerations: | |
469 * "[typedef] [static|public|protected|private] enum" | |
470 * "[typedef] [static|public|protected|private] = {" | |
471 */ | |
472 static int | |
473 cin_isinit(void) | |
474 { | |
475 char_u *s; | |
476 static char *skip[] = {"static", "public", "protected", "private"}; | |
477 | |
478 s = cin_skipcomment(ml_get_curline()); | |
479 | |
480 if (cin_starts_with(s, "typedef")) | |
481 s = cin_skipcomment(s + 7); | |
482 | |
483 for (;;) | |
484 { | |
485 int i, l; | |
486 | |
487 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i) | |
488 { | |
489 l = (int)strlen(skip[i]); | |
490 if (cin_starts_with(s, skip[i])) | |
491 { | |
492 s = cin_skipcomment(s + l); | |
493 l = 0; | |
494 break; | |
495 } | |
496 } | |
497 if (l != 0) | |
498 break; | |
499 } | |
500 | |
501 if (cin_starts_with(s, "enum")) | |
502 return TRUE; | 923 return TRUE; |
503 | |
504 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{")) | |
505 return TRUE; | |
506 | |
507 return FALSE; | |
508 } | |
509 | |
510 /* | |
511 * Recognize a switch label: "case .*:" or "default:". | |
512 */ | |
513 static int | |
514 cin_iscase( | |
515 char_u *s, | |
516 int strict) // Allow relaxed check of case statement for JS | |
517 { | |
518 s = cin_skipcomment(s); | |
519 if (cin_starts_with(s, "case")) | |
520 { | |
521 for (s += 4; *s; ++s) | |
522 { | |
523 s = cin_skipcomment(s); | |
524 if (*s == ':') | |
525 { | |
526 if (s[1] == ':') // skip over "::" for C++ | |
527 ++s; | |
528 else | |
529 return TRUE; | |
530 } | |
531 if (*s == '\'' && s[1] && s[2] == '\'') | |
532 s += 2; // skip over ':' | |
533 else if (*s == '/' && (s[1] == '*' || s[1] == '/')) | |
534 return FALSE; // stop at comment | |
535 else if (*s == '"') | |
536 { | |
537 // JS etc. | |
538 if (strict) | |
539 return FALSE; // stop at string | |
540 else | |
541 return TRUE; | |
542 } | |
543 } | |
544 return FALSE; | |
545 } | |
546 | |
547 if (cin_isdefault(s)) | |
548 return TRUE; | |
549 return FALSE; | |
550 } | |
551 | |
552 /* | |
553 * Recognize a "default" switch label. | |
554 */ | |
555 static int | |
556 cin_isdefault(char_u *s) | |
557 { | |
558 return (STRNCMP(s, "default", 7) == 0 | |
559 && *(s = cin_skipcomment(s + 7)) == ':' | |
560 && s[1] != ':'); | |
561 } | |
562 | |
563 /* | |
564 * Recognize a "public/private/protected" scope declaration label. | |
565 */ | |
566 static int | |
567 cin_isscopedecl(char_u *s) | |
568 { | |
569 int i; | |
570 | |
571 s = cin_skipcomment(s); | |
572 if (STRNCMP(s, "public", 6) == 0) | |
573 i = 6; | |
574 else if (STRNCMP(s, "protected", 9) == 0) | |
575 i = 9; | |
576 else if (STRNCMP(s, "private", 7) == 0) | |
577 i = 7; | |
578 else | 924 else |
579 return FALSE; | 925 return FALSE; |
580 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':'); | 926 } |
581 } | 927 |
582 | 928 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
583 // Maximum number of lines to search back for a "namespace" line. | 929 /* |
584 #define FIND_NAMESPACE_LIM 20 | 930 * op_reindent - handle reindenting a block of lines. |
585 | 931 */ |
586 /* | 932 void |
587 * Recognize a "namespace" scope declaration. | 933 op_reindent(oparg_T *oap, int (*how)(void)) |
588 */ | 934 { |
589 static int | 935 long i; |
590 cin_is_cpp_namespace(char_u *s) | 936 char_u *l; |
591 { | 937 int amount; |
592 char_u *p; | 938 linenr_T first_changed = 0; |
593 int has_name = FALSE; | 939 linenr_T last_changed = 0; |
594 int has_name_start = FALSE; | 940 linenr_T start_lnum = curwin->w_cursor.lnum; |
595 | 941 |
596 s = cin_skipcomment(s); | 942 // Don't even try when 'modifiable' is off. |
597 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) | 943 if (!curbuf->b_p_ma) |
598 { | 944 { |
599 p = cin_skipcomment(skipwhite(s + 9)); | 945 emsg(_(e_modifiable)); |
600 while (*p != NUL) | 946 return; |
601 { | 947 } |
602 if (VIM_ISWHITE(*p)) | 948 |
949 for (i = oap->line_count; --i >= 0 && !got_int; ) | |
950 { | |
951 // it's a slow thing to do, so give feedback so there's no worry that | |
952 // the computer's just hung. | |
953 | |
954 if (i > 1 | |
955 && (i % 50 == 0 || i == oap->line_count - 1) | |
956 && oap->line_count > p_report) | |
957 smsg(_("%ld lines to indent... "), i); | |
958 | |
959 // Be vi-compatible: For lisp indenting the first line is not | |
960 // indented, unless there is only one line. | |
961 # ifdef FEAT_LISP | |
962 if (i != oap->line_count - 1 || oap->line_count == 1 | |
963 || how != get_lisp_indent) | |
964 # endif | |
965 { | |
966 l = skipwhite(ml_get_curline()); | |
967 if (*l == NUL) // empty or blank line | |
968 amount = 0; | |
969 else | |
970 amount = how(); // get the indent for this line | |
971 | |
972 if (amount >= 0 && set_indent(amount, SIN_UNDO)) | |
603 { | 973 { |
604 has_name = TRUE; // found end of a name | 974 // did change the indent, call changed_lines() later |
605 p = cin_skipcomment(skipwhite(p)); | 975 if (first_changed == 0) |
976 first_changed = curwin->w_cursor.lnum; | |
977 last_changed = curwin->w_cursor.lnum; | |
606 } | 978 } |
607 else if (*p == '{') | 979 } |
980 ++curwin->w_cursor.lnum; | |
981 curwin->w_cursor.col = 0; // make sure it's valid | |
982 } | |
983 | |
984 // put cursor on first non-blank of indented line | |
985 curwin->w_cursor.lnum = start_lnum; | |
986 beginline(BL_SOL | BL_FIX); | |
987 | |
988 // Mark changed lines so that they will be redrawn. When Visual | |
989 // highlighting was present, need to continue until the last line. When | |
990 // there is no change still need to remove the Visual highlighting. | |
991 if (last_changed != 0) | |
992 changed_lines(first_changed, 0, | |
993 oap->is_VIsual ? start_lnum + oap->line_count : | |
994 last_changed + 1, 0L); | |
995 else if (oap->is_VIsual) | |
996 redraw_curbuf_later(INVERTED); | |
997 | |
998 if (oap->line_count > p_report) | |
999 { | |
1000 i = oap->line_count - (i + 1); | |
1001 smsg(NGETTEXT("%ld line indented ", | |
1002 "%ld lines indented ", i), i); | |
1003 } | |
1004 // set '[ and '] marks | |
1005 curbuf->b_op_start = oap->start; | |
1006 curbuf->b_op_end = oap->end; | |
1007 } | |
1008 #endif // defined(FEAT_LISP) || defined(FEAT_CINDENT) | |
1009 | |
1010 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO) | |
1011 /* | |
1012 * Return TRUE if lines starting with '#' should be left aligned. | |
1013 */ | |
1014 int | |
1015 preprocs_left(void) | |
1016 { | |
1017 return | |
1018 # ifdef FEAT_SMARTINDENT | |
1019 # ifdef FEAT_CINDENT | |
1020 (curbuf->b_p_si && !curbuf->b_p_cin) || | |
1021 # else | |
1022 curbuf->b_p_si | |
1023 # endif | |
1024 # endif | |
1025 # ifdef FEAT_CINDENT | |
1026 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE) | |
1027 && curbuf->b_ind_hash_comment == 0) | |
1028 # endif | |
1029 ; | |
1030 } | |
1031 #endif | |
1032 | |
1033 #ifdef FEAT_SMARTINDENT | |
1034 /* | |
1035 * Try to do some very smart auto-indenting. | |
1036 * Used when inserting a "normal" character. | |
1037 */ | |
1038 void | |
1039 ins_try_si(int c) | |
1040 { | |
1041 pos_T *pos, old_pos; | |
1042 char_u *ptr; | |
1043 int i; | |
1044 int temp; | |
1045 | |
1046 // do some very smart indenting when entering '{' or '}' | |
1047 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) | |
1048 { | |
1049 // for '}' set indent equal to indent of line containing matching '{' | |
1050 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) | |
1051 { | |
1052 old_pos = curwin->w_cursor; | |
1053 // If the matching '{' has a ')' immediately before it (ignoring | |
1054 // white-space), then line up with the start of the line | |
1055 // containing the matching '(' if there is one. This handles the | |
1056 // case where an "if (..\n..) {" statement continues over multiple | |
1057 // lines -- webb | |
1058 ptr = ml_get(pos->lnum); | |
1059 i = pos->col; | |
1060 if (i > 0) // skip blanks before '{' | |
1061 while (--i > 0 && VIM_ISWHITE(ptr[i])) | |
1062 ; | |
1063 curwin->w_cursor.lnum = pos->lnum; | |
1064 curwin->w_cursor.col = i; | |
1065 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) | |
1066 curwin->w_cursor = *pos; | |
1067 i = get_indent(); | |
1068 curwin->w_cursor = old_pos; | |
1069 if (State & VREPLACE_FLAG) | |
1070 change_indent(INDENT_SET, i, FALSE, NUL, TRUE); | |
1071 else | |
1072 (void)set_indent(i, SIN_CHANGED); | |
1073 } | |
1074 else if (curwin->w_cursor.col > 0) | |
1075 { | |
1076 // when inserting '{' after "O" reduce indent, but not | |
1077 // more than indent of previous line | |
1078 temp = TRUE; | |
1079 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) | |
608 { | 1080 { |
609 break; | 1081 old_pos = curwin->w_cursor; |
1082 i = get_indent(); | |
1083 while (curwin->w_cursor.lnum > 1) | |
1084 { | |
1085 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); | |
1086 | |
1087 // ignore empty lines and lines starting with '#'. | |
1088 if (*ptr != '#' && *ptr != NUL) | |
1089 break; | |
1090 } | |
1091 if (get_indent() >= i) | |
1092 temp = FALSE; | |
1093 curwin->w_cursor = old_pos; | |
610 } | 1094 } |
611 else if (vim_iswordc(*p)) | 1095 if (temp) |
1096 shift_line(TRUE, FALSE, 1, TRUE); | |
1097 } | |
1098 } | |
1099 | |
1100 // set indent of '#' always to 0 | |
1101 if (curwin->w_cursor.col > 0 && can_si && c == '#') | |
1102 { | |
1103 // remember current indent for next line | |
1104 old_indent = get_indent(); | |
1105 (void)set_indent(0, SIN_CHANGED); | |
1106 } | |
1107 | |
1108 // Adjust ai_col, the char at this position can be deleted. | |
1109 if (ai_col > curwin->w_cursor.col) | |
1110 ai_col = curwin->w_cursor.col; | |
1111 } | |
1112 #endif | |
1113 | |
1114 /* | |
1115 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). | |
1116 * Keep the cursor on the same character. | |
1117 * type == INDENT_INC increase indent (for CTRL-T or <Tab>) | |
1118 * type == INDENT_DEC decrease indent (for CTRL-D) | |
1119 * type == INDENT_SET set indent to "amount" | |
1120 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). | |
1121 */ | |
1122 void | |
1123 change_indent( | |
1124 int type, | |
1125 int amount, | |
1126 int round, | |
1127 int replaced, // replaced character, put on replace stack | |
1128 int call_changed_bytes) // call changed_bytes() | |
1129 { | |
1130 int vcol; | |
1131 int last_vcol; | |
1132 int insstart_less; // reduction for Insstart.col | |
1133 int new_cursor_col; | |
1134 int i; | |
1135 char_u *ptr; | |
1136 int save_p_list; | |
1137 int start_col; | |
1138 colnr_T vc; | |
1139 colnr_T orig_col = 0; // init for GCC | |
1140 char_u *new_line, *orig_line = NULL; // init for GCC | |
1141 | |
1142 // VREPLACE mode needs to know what the line was like before changing | |
1143 if (State & VREPLACE_FLAG) | |
1144 { | |
1145 orig_line = vim_strsave(ml_get_curline()); // Deal with NULL below | |
1146 orig_col = curwin->w_cursor.col; | |
1147 } | |
1148 | |
1149 // for the following tricks we don't want list mode | |
1150 save_p_list = curwin->w_p_list; | |
1151 curwin->w_p_list = FALSE; | |
1152 vc = getvcol_nolist(&curwin->w_cursor); | |
1153 vcol = vc; | |
1154 | |
1155 // For Replace mode we need to fix the replace stack later, which is only | |
1156 // possible when the cursor is in the indent. Remember the number of | |
1157 // characters before the cursor if it's possible. | |
1158 start_col = curwin->w_cursor.col; | |
1159 | |
1160 // determine offset from first non-blank | |
1161 new_cursor_col = curwin->w_cursor.col; | |
1162 beginline(BL_WHITE); | |
1163 new_cursor_col -= curwin->w_cursor.col; | |
1164 | |
1165 insstart_less = curwin->w_cursor.col; | |
1166 | |
1167 // If the cursor is in the indent, compute how many screen columns the | |
1168 // cursor is to the left of the first non-blank. | |
1169 if (new_cursor_col < 0) | |
1170 vcol = get_indent() - vcol; | |
1171 | |
1172 if (new_cursor_col > 0) // can't fix replace stack | |
1173 start_col = -1; | |
1174 | |
1175 // Set the new indent. The cursor will be put on the first non-blank. | |
1176 if (type == INDENT_SET) | |
1177 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0); | |
1178 else | |
1179 { | |
1180 int save_State = State; | |
1181 | |
1182 // Avoid being called recursively. | |
1183 if (State & VREPLACE_FLAG) | |
1184 State = INSERT; | |
1185 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes); | |
1186 State = save_State; | |
1187 } | |
1188 insstart_less -= curwin->w_cursor.col; | |
1189 | |
1190 // Try to put cursor on same character. | |
1191 // If the cursor is at or after the first non-blank in the line, | |
1192 // compute the cursor column relative to the column of the first | |
1193 // non-blank character. | |
1194 // If we are not in insert mode, leave the cursor on the first non-blank. | |
1195 // If the cursor is before the first non-blank, position it relative | |
1196 // to the first non-blank, counted in screen columns. | |
1197 if (new_cursor_col >= 0) | |
1198 { | |
1199 // When changing the indent while the cursor is touching it, reset | |
1200 // Insstart_col to 0. | |
1201 if (new_cursor_col == 0) | |
1202 insstart_less = MAXCOL; | |
1203 new_cursor_col += curwin->w_cursor.col; | |
1204 } | |
1205 else if (!(State & INSERT)) | |
1206 new_cursor_col = curwin->w_cursor.col; | |
1207 else | |
1208 { | |
1209 // Compute the screen column where the cursor should be. | |
1210 vcol = get_indent() - vcol; | |
1211 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol); | |
1212 | |
1213 // Advance the cursor until we reach the right screen column. | |
1214 vcol = last_vcol = 0; | |
1215 new_cursor_col = -1; | |
1216 ptr = ml_get_curline(); | |
1217 while (vcol <= (int)curwin->w_virtcol) | |
1218 { | |
1219 last_vcol = vcol; | |
1220 if (has_mbyte && new_cursor_col >= 0) | |
1221 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); | |
1222 else | |
1223 ++new_cursor_col; | |
1224 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol); | |
1225 } | |
1226 vcol = last_vcol; | |
1227 | |
1228 // May need to insert spaces to be able to position the cursor on | |
1229 // the right screen column. | |
1230 if (vcol != (int)curwin->w_virtcol) | |
1231 { | |
1232 curwin->w_cursor.col = (colnr_T)new_cursor_col; | |
1233 i = (int)curwin->w_virtcol - vcol; | |
1234 ptr = alloc(i + 1); | |
1235 if (ptr != NULL) | |
612 { | 1236 { |
613 has_name_start = TRUE; | 1237 new_cursor_col += i; |
614 if (has_name) | 1238 ptr[i] = NUL; |
615 return FALSE; // word character after skipping past name | 1239 while (--i >= 0) |
616 ++p; | 1240 ptr[i] = ' '; |
1241 ins_str(ptr); | |
1242 vim_free(ptr); | |
617 } | 1243 } |
618 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2])) | 1244 } |
1245 | |
1246 // When changing the indent while the cursor is in it, reset | |
1247 // Insstart_col to 0. | |
1248 insstart_less = MAXCOL; | |
1249 } | |
1250 | |
1251 curwin->w_p_list = save_p_list; | |
1252 | |
1253 if (new_cursor_col <= 0) | |
1254 curwin->w_cursor.col = 0; | |
1255 else | |
1256 curwin->w_cursor.col = (colnr_T)new_cursor_col; | |
1257 curwin->w_set_curswant = TRUE; | |
1258 changed_cline_bef_curs(); | |
1259 | |
1260 // May have to adjust the start of the insert. | |
1261 if (State & INSERT) | |
1262 { | |
1263 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) | |
1264 { | |
1265 if ((int)Insstart.col <= insstart_less) | |
1266 Insstart.col = 0; | |
1267 else | |
1268 Insstart.col -= insstart_less; | |
1269 } | |
1270 if ((int)ai_col <= insstart_less) | |
1271 ai_col = 0; | |
1272 else | |
1273 ai_col -= insstart_less; | |
1274 } | |
1275 | |
1276 // For REPLACE mode, may have to fix the replace stack, if it's possible. | |
1277 // If the number of characters before the cursor decreased, need to pop a | |
1278 // few characters from the replace stack. | |
1279 // If the number of characters before the cursor increased, need to push a | |
1280 // few NULs onto the replace stack. | |
1281 if (REPLACE_NORMAL(State) && start_col >= 0) | |
1282 { | |
1283 while (start_col > (int)curwin->w_cursor.col) | |
1284 { | |
1285 replace_join(0); // remove a NUL from the replace stack | |
1286 --start_col; | |
1287 } | |
1288 while (start_col < (int)curwin->w_cursor.col || replaced) | |
1289 { | |
1290 replace_push(NUL); | |
1291 if (replaced) | |
619 { | 1292 { |
620 if (!has_name_start || has_name) | 1293 replace_push(replaced); |
621 return FALSE; | 1294 replaced = NUL; |
622 // C++ 17 nested namespace | 1295 } |
623 p += 3; | 1296 ++start_col; |
1297 } | |
1298 } | |
1299 | |
1300 // For VREPLACE mode, we also have to fix the replace stack. In this case | |
1301 // it is always possible because we backspace over the whole line and then | |
1302 // put it back again the way we wanted it. | |
1303 if (State & VREPLACE_FLAG) | |
1304 { | |
1305 // If orig_line didn't allocate, just return. At least we did the job, | |
1306 // even if you can't backspace. | |
1307 if (orig_line == NULL) | |
1308 return; | |
1309 | |
1310 // Save new line | |
1311 new_line = vim_strsave(ml_get_curline()); | |
1312 if (new_line == NULL) | |
1313 return; | |
1314 | |
1315 // We only put back the new line up to the cursor | |
1316 new_line[curwin->w_cursor.col] = NUL; | |
1317 | |
1318 // Put back original line | |
1319 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); | |
1320 curwin->w_cursor.col = orig_col; | |
1321 | |
1322 // Backspace from cursor to start of line | |
1323 backspace_until_column(0); | |
1324 | |
1325 // Insert new stuff into line again | |
1326 ins_bytes(new_line); | |
1327 | |
1328 vim_free(new_line); | |
1329 } | |
1330 } | |
1331 | |
1332 /* | |
1333 * Copy the indent from ptr to the current line (and fill to size) | |
1334 * Leaves the cursor on the first non-blank in the line. | |
1335 * Returns TRUE if the line was changed. | |
1336 */ | |
1337 int | |
1338 copy_indent(int size, char_u *src) | |
1339 { | |
1340 char_u *p = NULL; | |
1341 char_u *line = NULL; | |
1342 char_u *s; | |
1343 int todo; | |
1344 int ind_len; | |
1345 int line_len = 0; | |
1346 int tab_pad; | |
1347 int ind_done; | |
1348 int round; | |
1349 #ifdef FEAT_VARTABS | |
1350 int ind_col; | |
1351 #endif | |
1352 | |
1353 // Round 1: compute the number of characters needed for the indent | |
1354 // Round 2: copy the characters. | |
1355 for (round = 1; round <= 2; ++round) | |
1356 { | |
1357 todo = size; | |
1358 ind_len = 0; | |
1359 ind_done = 0; | |
1360 #ifdef FEAT_VARTABS | |
1361 ind_col = 0; | |
1362 #endif | |
1363 s = src; | |
1364 | |
1365 // Count/copy the usable portion of the source line | |
1366 while (todo > 0 && VIM_ISWHITE(*s)) | |
1367 { | |
1368 if (*s == TAB) | |
1369 { | |
1370 #ifdef FEAT_VARTABS | |
1371 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, | |
1372 curbuf->b_p_vts_array); | |
1373 #else | |
1374 tab_pad = (int)curbuf->b_p_ts | |
1375 - (ind_done % (int)curbuf->b_p_ts); | |
1376 #endif | |
1377 // Stop if this tab will overshoot the target | |
1378 if (todo < tab_pad) | |
1379 break; | |
1380 todo -= tab_pad; | |
1381 ind_done += tab_pad; | |
1382 #ifdef FEAT_VARTABS | |
1383 ind_col += tab_pad; | |
1384 #endif | |
624 } | 1385 } |
625 else | 1386 else |
626 { | 1387 { |
1388 --todo; | |
1389 ++ind_done; | |
1390 #ifdef FEAT_VARTABS | |
1391 ++ind_col; | |
1392 #endif | |
1393 } | |
1394 ++ind_len; | |
1395 if (p != NULL) | |
1396 *p++ = *s; | |
1397 ++s; | |
1398 } | |
1399 | |
1400 // Fill to next tabstop with a tab, if possible | |
1401 #ifdef FEAT_VARTABS | |
1402 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts, | |
1403 curbuf->b_p_vts_array); | |
1404 #else | |
1405 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts); | |
1406 #endif | |
1407 if (todo >= tab_pad && !curbuf->b_p_et) | |
1408 { | |
1409 todo -= tab_pad; | |
1410 ++ind_len; | |
1411 #ifdef FEAT_VARTABS | |
1412 ind_col += tab_pad; | |
1413 #endif | |
1414 if (p != NULL) | |
1415 *p++ = TAB; | |
1416 } | |
1417 | |
1418 // Add tabs required for indent | |
1419 if (!curbuf->b_p_et) | |
1420 { | |
1421 #ifdef FEAT_VARTABS | |
1422 for (;;) | |
1423 { | |
1424 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts, | |
1425 curbuf->b_p_vts_array); | |
1426 if (todo < tab_pad) | |
1427 break; | |
1428 todo -= tab_pad; | |
1429 ++ind_len; | |
1430 ind_col += tab_pad; | |
1431 if (p != NULL) | |
1432 *p++ = TAB; | |
1433 } | |
1434 #else | |
1435 while (todo >= (int)curbuf->b_p_ts) | |
1436 { | |
1437 todo -= (int)curbuf->b_p_ts; | |
1438 ++ind_len; | |
1439 if (p != NULL) | |
1440 *p++ = TAB; | |
1441 } | |
1442 #endif | |
1443 } | |
1444 | |
1445 // Count/add spaces required for indent | |
1446 while (todo > 0) | |
1447 { | |
1448 --todo; | |
1449 ++ind_len; | |
1450 if (p != NULL) | |
1451 *p++ = ' '; | |
1452 } | |
1453 | |
1454 if (p == NULL) | |
1455 { | |
1456 // Allocate memory for the result: the copied indent, new indent | |
1457 // and the rest of the line. | |
1458 line_len = (int)STRLEN(ml_get_curline()) + 1; | |
1459 line = alloc(ind_len + line_len); | |
1460 if (line == NULL) | |
627 return FALSE; | 1461 return FALSE; |
628 } | 1462 p = line; |
629 } | 1463 } |
630 return TRUE; | 1464 } |
631 } | 1465 |
632 return FALSE; | 1466 // Append the original line |
633 } | 1467 mch_memmove(p, ml_get_curline(), (size_t)line_len); |
634 | 1468 |
635 /* | 1469 // Replace the line |
636 * Recognize a `extern "C"` or `extern "C++"` linkage specifications. | 1470 ml_replace(curwin->w_cursor.lnum, line, FALSE); |
637 */ | 1471 |
638 static int | 1472 // Put the cursor after the indent. |
639 cin_is_cpp_extern_c(char_u *s) | 1473 curwin->w_cursor.col = ind_len; |
640 { | 1474 return TRUE; |
641 char_u *p; | 1475 } |
642 int has_string_literal = FALSE; | 1476 |
643 | 1477 /* |
644 s = cin_skipcomment(s); | 1478 * ":retab". |
645 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) | 1479 */ |
646 { | 1480 void |
647 p = cin_skipcomment(skipwhite(s + 6)); | 1481 ex_retab(exarg_T *eap) |
648 while (*p != NUL) | 1482 { |
649 { | 1483 linenr_T lnum; |
650 if (VIM_ISWHITE(*p)) | 1484 int got_tab = FALSE; |
1485 long num_spaces = 0; | |
1486 long num_tabs; | |
1487 long len; | |
1488 long col; | |
1489 long vcol; | |
1490 long start_col = 0; /* For start of white-space string */ | |
1491 long start_vcol = 0; /* For start of white-space string */ | |
1492 long old_len; | |
1493 char_u *ptr; | |
1494 char_u *new_line = (char_u *)1; /* init to non-NULL */ | |
1495 int did_undo; /* called u_save for current line */ | |
1496 #ifdef FEAT_VARTABS | |
1497 int *new_vts_array = NULL; | |
1498 char_u *new_ts_str; /* string value of tab argument */ | |
1499 #else | |
1500 int temp; | |
1501 int new_ts; | |
1502 #endif | |
1503 int save_list; | |
1504 linenr_T first_line = 0; /* first changed line */ | |
1505 linenr_T last_line = 0; /* last changed line */ | |
1506 | |
1507 save_list = curwin->w_p_list; | |
1508 curwin->w_p_list = 0; /* don't want list mode here */ | |
1509 | |
1510 #ifdef FEAT_VARTABS | |
1511 new_ts_str = eap->arg; | |
1512 if (!tabstop_set(eap->arg, &new_vts_array)) | |
1513 return; | |
1514 while (vim_isdigit(*(eap->arg)) || *(eap->arg) == ',') | |
1515 ++(eap->arg); | |
1516 | |
1517 // This ensures that either new_vts_array and new_ts_str are freshly | |
1518 // allocated, or new_vts_array points to an existing array and new_ts_str | |
1519 // is null. | |
1520 if (new_vts_array == NULL) | |
1521 { | |
1522 new_vts_array = curbuf->b_p_vts_array; | |
1523 new_ts_str = NULL; | |
1524 } | |
1525 else | |
1526 new_ts_str = vim_strnsave(new_ts_str, eap->arg - new_ts_str); | |
1527 #else | |
1528 new_ts = getdigits(&(eap->arg)); | |
1529 if (new_ts < 0) | |
1530 { | |
1531 emsg(_(e_positive)); | |
1532 return; | |
1533 } | |
1534 if (new_ts == 0) | |
1535 new_ts = curbuf->b_p_ts; | |
1536 #endif | |
1537 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum) | |
1538 { | |
1539 ptr = ml_get(lnum); | |
1540 col = 0; | |
1541 vcol = 0; | |
1542 did_undo = FALSE; | |
1543 for (;;) | |
1544 { | |
1545 if (VIM_ISWHITE(ptr[col])) | |
651 { | 1546 { |
652 p = cin_skipcomment(skipwhite(p)); | 1547 if (!got_tab && num_spaces == 0) |
653 } | 1548 { |
654 else if (*p == '{') | 1549 /* First consecutive white-space */ |
655 { | 1550 start_vcol = vcol; |
656 break; | 1551 start_col = col; |
657 } | 1552 } |
658 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"') | 1553 if (ptr[col] == ' ') |
659 { | 1554 num_spaces++; |
660 if (has_string_literal) | 1555 else |
661 return FALSE; | 1556 got_tab = TRUE; |
662 has_string_literal = TRUE; | |
663 p += 3; | |
664 } | |
665 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+' | |
666 && p[4] == '"') | |
667 { | |
668 if (has_string_literal) | |
669 return FALSE; | |
670 has_string_literal = TRUE; | |
671 p += 5; | |
672 } | 1557 } |
673 else | 1558 else |
674 { | 1559 { |
675 return FALSE; | 1560 if (got_tab || (eap->forceit && num_spaces > 1)) |
676 } | |
677 } | |
678 return has_string_literal ? TRUE : FALSE; | |
679 } | |
680 return FALSE; | |
681 } | |
682 | |
683 /* | |
684 * Return a pointer to the first non-empty non-comment character after a ':'. | |
685 * Return NULL if not found. | |
686 * case 234: a = b; | |
687 * ^ | |
688 */ | |
689 static char_u * | |
690 after_label(char_u *l) | |
691 { | |
692 for ( ; *l; ++l) | |
693 { | |
694 if (*l == ':') | |
695 { | |
696 if (l[1] == ':') // skip over "::" for C++ | |
697 ++l; | |
698 else if (!cin_iscase(l + 1, FALSE)) | |
699 break; | |
700 } | |
701 else if (*l == '\'' && l[1] && l[2] == '\'') | |
702 l += 2; // skip over 'x' | |
703 } | |
704 if (*l == NUL) | |
705 return NULL; | |
706 l = cin_skipcomment(l + 1); | |
707 if (*l == NUL) | |
708 return NULL; | |
709 return l; | |
710 } | |
711 | |
712 /* | |
713 * Get indent of line "lnum", skipping a label. | |
714 * Return 0 if there is nothing after the label. | |
715 */ | |
716 static int | |
717 get_indent_nolabel (linenr_T lnum) // XXX | |
718 { | |
719 char_u *l; | |
720 pos_T fp; | |
721 colnr_T col; | |
722 char_u *p; | |
723 | |
724 l = ml_get(lnum); | |
725 p = after_label(l); | |
726 if (p == NULL) | |
727 return 0; | |
728 | |
729 fp.col = (colnr_T)(p - l); | |
730 fp.lnum = lnum; | |
731 getvcol(curwin, &fp, &col, NULL, NULL); | |
732 return (int)col; | |
733 } | |
734 | |
735 /* | |
736 * Find indent for line "lnum", ignoring any case or jump label. | |
737 * Also return a pointer to the text (after the label) in "pp". | |
738 * label: if (asdf && asdfasdf) | |
739 * ^ | |
740 */ | |
741 static int | |
742 skip_label(linenr_T lnum, char_u **pp) | |
743 { | |
744 char_u *l; | |
745 int amount; | |
746 pos_T cursor_save; | |
747 | |
748 cursor_save = curwin->w_cursor; | |
749 curwin->w_cursor.lnum = lnum; | |
750 l = ml_get_curline(); | |
751 // XXX | |
752 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel()) | |
753 { | |
754 amount = get_indent_nolabel(lnum); | |
755 l = after_label(ml_get_curline()); | |
756 if (l == NULL) // just in case | |
757 l = ml_get_curline(); | |
758 } | |
759 else | |
760 { | |
761 amount = get_indent(); | |
762 l = ml_get_curline(); | |
763 } | |
764 *pp = l; | |
765 | |
766 curwin->w_cursor = cursor_save; | |
767 return amount; | |
768 } | |
769 | |
770 /* | |
771 * Return the indent of the first variable name after a type in a declaration. | |
772 * int a, indent of "a" | |
773 * static struct foo b, indent of "b" | |
774 * enum bla c, indent of "c" | |
775 * Returns zero when it doesn't look like a declaration. | |
776 */ | |
777 static int | |
778 cin_first_id_amount(void) | |
779 { | |
780 char_u *line, *p, *s; | |
781 int len; | |
782 pos_T fp; | |
783 colnr_T col; | |
784 | |
785 line = ml_get_curline(); | |
786 p = skipwhite(line); | |
787 len = (int)(skiptowhite(p) - p); | |
788 if (len == 6 && STRNCMP(p, "static", 6) == 0) | |
789 { | |
790 p = skipwhite(p + 6); | |
791 len = (int)(skiptowhite(p) - p); | |
792 } | |
793 if (len == 6 && STRNCMP(p, "struct", 6) == 0) | |
794 p = skipwhite(p + 6); | |
795 else if (len == 4 && STRNCMP(p, "enum", 4) == 0) | |
796 p = skipwhite(p + 4); | |
797 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0) | |
798 || (len == 6 && STRNCMP(p, "signed", 6) == 0)) | |
799 { | |
800 s = skipwhite(p + len); | |
801 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3])) | |
802 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4])) | |
803 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5])) | |
804 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4]))) | |
805 p = s; | |
806 } | |
807 for (len = 0; vim_isIDc(p[len]); ++len) | |
808 ; | |
809 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p)) | |
810 return 0; | |
811 | |
812 p = skipwhite(p + len); | |
813 fp.lnum = curwin->w_cursor.lnum; | |
814 fp.col = (colnr_T)(p - line); | |
815 getvcol(curwin, &fp, &col, NULL, NULL); | |
816 return (int)col; | |
817 } | |
818 | |
819 /* | |
820 * Return the indent of the first non-blank after an equal sign. | |
821 * char *foo = "here"; | |
822 * Return zero if no (useful) equal sign found. | |
823 * Return -1 if the line above "lnum" ends in a backslash. | |
824 * foo = "asdf\ | |
825 * asdf\ | |
826 * here"; | |
827 */ | |
828 static int | |
829 cin_get_equal_amount(linenr_T lnum) | |
830 { | |
831 char_u *line; | |
832 char_u *s; | |
833 colnr_T col; | |
834 pos_T fp; | |
835 | |
836 if (lnum > 1) | |
837 { | |
838 line = ml_get(lnum - 1); | |
839 if (*line != NUL && line[STRLEN(line) - 1] == '\\') | |
840 return -1; | |
841 } | |
842 | |
843 line = s = ml_get(lnum); | |
844 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL) | |
845 { | |
846 if (cin_iscomment(s)) // ignore comments | |
847 s = cin_skipcomment(s); | |
848 else | |
849 ++s; | |
850 } | |
851 if (*s != '=') | |
852 return 0; | |
853 | |
854 s = skipwhite(s + 1); | |
855 if (cin_nocode(s)) | |
856 return 0; | |
857 | |
858 if (*s == '"') // nice alignment for continued strings | |
859 ++s; | |
860 | |
861 fp.lnum = lnum; | |
862 fp.col = (colnr_T)(s - line); | |
863 getvcol(curwin, &fp, &col, NULL, NULL); | |
864 return (int)col; | |
865 } | |
866 | |
867 /* | |
868 * Recognize a preprocessor statement: Any line that starts with '#'. | |
869 */ | |
870 static int | |
871 cin_ispreproc(char_u *s) | |
872 { | |
873 if (*skipwhite(s) == '#') | |
874 return TRUE; | |
875 return FALSE; | |
876 } | |
877 | |
878 /* | |
879 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a | |
880 * continuation line of a preprocessor statement. Decrease "*lnump" to the | |
881 * start and return the line in "*pp". | |
882 * Put the amount of indent in "*amount". | |
883 */ | |
884 static int | |
885 cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) | |
886 { | |
887 char_u *line = *pp; | |
888 linenr_T lnum = *lnump; | |
889 int retval = FALSE; | |
890 int candidate_amount = *amount; | |
891 | |
892 if (*line != NUL && line[STRLEN(line) - 1] == '\\') | |
893 candidate_amount = get_indent_lnum(lnum); | |
894 | |
895 for (;;) | |
896 { | |
897 if (cin_ispreproc(line)) | |
898 { | |
899 retval = TRUE; | |
900 *lnump = lnum; | |
901 break; | |
902 } | |
903 if (lnum == 1) | |
904 break; | |
905 line = ml_get(--lnum); | |
906 if (*line == NUL || line[STRLEN(line) - 1] != '\\') | |
907 break; | |
908 } | |
909 | |
910 if (lnum != *lnump) | |
911 *pp = ml_get(*lnump); | |
912 if (retval) | |
913 *amount = candidate_amount; | |
914 return retval; | |
915 } | |
916 | |
917 /* | |
918 * Recognize the start of a C or C++ comment. | |
919 */ | |
920 static int | |
921 cin_iscomment(char_u *p) | |
922 { | |
923 return (p[0] == '/' && (p[1] == '*' || p[1] == '/')); | |
924 } | |
925 | |
926 /* | |
927 * Recognize the start of a "//" comment. | |
928 */ | |
929 static int | |
930 cin_islinecomment(char_u *p) | |
931 { | |
932 return (p[0] == '/' && p[1] == '/'); | |
933 } | |
934 | |
935 /* | |
936 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or | |
937 * '}'. | |
938 * Don't consider "} else" a terminated line. | |
939 * If a line begins with an "else", only consider it terminated if no unmatched | |
940 * opening braces follow (handle "else { foo();" correctly). | |
941 * Return the character terminating the line (ending char's have precedence if | |
942 * both apply in order to determine initializations). | |
943 */ | |
944 static int | |
945 cin_isterminated( | |
946 char_u *s, | |
947 int incl_open, // include '{' at the end as terminator | |
948 int incl_comma) // recognize a trailing comma | |
949 { | |
950 char_u found_start = 0; | |
951 unsigned n_open = 0; | |
952 int is_else = FALSE; | |
953 | |
954 s = cin_skipcomment(s); | |
955 | |
956 if (*s == '{' || (*s == '}' && !cin_iselse(s))) | |
957 found_start = *s; | |
958 | |
959 if (!found_start) | |
960 is_else = cin_iselse(s); | |
961 | |
962 while (*s) | |
963 { | |
964 // skip over comments, "" strings and 'c'haracters | |
965 s = skip_string(cin_skipcomment(s)); | |
966 if (*s == '}' && n_open > 0) | |
967 --n_open; | |
968 if ((!is_else || n_open == 0) | |
969 && (*s == ';' || *s == '}' || (incl_comma && *s == ',')) | |
970 && cin_nocode(s + 1)) | |
971 return *s; | |
972 else if (*s == '{') | |
973 { | |
974 if (incl_open && cin_nocode(s + 1)) | |
975 return *s; | |
976 else | |
977 ++n_open; | |
978 } | |
979 | |
980 if (*s) | |
981 s++; | |
982 } | |
983 return found_start; | |
984 } | |
985 | |
986 /* | |
987 * Recognize the basic picture of a function declaration -- it needs to | |
988 * have an open paren somewhere and a close paren at the end of the line and | |
989 * no semicolons anywhere. | |
990 * When a line ends in a comma we continue looking in the next line. | |
991 * "sp" points to a string with the line. When looking at other lines it must | |
992 * be restored to the line. When it's NULL fetch lines here. | |
993 * "first_lnum" is where we start looking. | |
994 * "min_lnum" is the line before which we will not be looking. | |
995 */ | |
996 static int | |
997 cin_isfuncdecl( | |
998 char_u **sp, | |
999 linenr_T first_lnum, | |
1000 linenr_T min_lnum) | |
1001 { | |
1002 char_u *s; | |
1003 linenr_T lnum = first_lnum; | |
1004 linenr_T save_lnum = curwin->w_cursor.lnum; | |
1005 int retval = FALSE; | |
1006 pos_T *trypos; | |
1007 int just_started = TRUE; | |
1008 | |
1009 if (sp == NULL) | |
1010 s = ml_get(lnum); | |
1011 else | |
1012 s = *sp; | |
1013 | |
1014 curwin->w_cursor.lnum = lnum; | |
1015 if (find_last_paren(s, '(', ')') | |
1016 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) | |
1017 { | |
1018 lnum = trypos->lnum; | |
1019 if (lnum < min_lnum) | |
1020 { | |
1021 curwin->w_cursor.lnum = save_lnum; | |
1022 return FALSE; | |
1023 } | |
1024 | |
1025 s = ml_get(lnum); | |
1026 } | |
1027 curwin->w_cursor.lnum = save_lnum; | |
1028 | |
1029 // Ignore line starting with #. | |
1030 if (cin_ispreproc(s)) | |
1031 return FALSE; | |
1032 | |
1033 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"') | |
1034 { | |
1035 if (cin_iscomment(s)) // ignore comments | |
1036 s = cin_skipcomment(s); | |
1037 else if (*s == ':') | |
1038 { | |
1039 if (*(s + 1) == ':') | |
1040 s += 2; | |
1041 else | |
1042 // To avoid a mistake in the following situation: | |
1043 // A::A(int a, int b) | |
1044 // : a(0) // <--not a function decl | |
1045 // , b(0) | |
1046 // {... | |
1047 return FALSE; | |
1048 } | |
1049 else | |
1050 ++s; | |
1051 } | |
1052 if (*s != '(') | |
1053 return FALSE; // ';', ' or " before any () or no '(' | |
1054 | |
1055 while (*s && *s != ';' && *s != '\'' && *s != '"') | |
1056 { | |
1057 if (*s == ')' && cin_nocode(s + 1)) | |
1058 { | |
1059 /* | |
1060 * ')' at the end: may have found a match | |
1061 * Check for he previous line not to end in a backslash: | |
1062 * #if defined(x) && \ | |
1063 * defined(y) | |
1064 */ | |
1065 lnum = first_lnum - 1; | |
1066 s = ml_get(lnum); | |
1067 if (*s == NUL || s[STRLEN(s) - 1] != '\\') | |
1068 retval = TRUE; | |
1069 goto done; | |
1070 } | |
1071 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s)) | |
1072 { | |
1073 int comma = (*s == ','); | |
1074 | |
1075 // ',' at the end: continue looking in the next line. | |
1076 // At the end: check for ',' in the next line, for this style: | |
1077 // func(arg1 | |
1078 // , arg2) | |
1079 for (;;) | |
1080 { | |
1081 if (lnum >= curbuf->b_ml.ml_line_count) | |
1082 break; | |
1083 s = ml_get(++lnum); | |
1084 if (!cin_ispreproc(s)) | |
1085 break; | |
1086 } | |
1087 if (lnum >= curbuf->b_ml.ml_line_count) | |
1088 break; | |
1089 // Require a comma at end of the line or a comma or ')' at the | |
1090 // start of next line. | |
1091 s = skipwhite(s); | |
1092 if (!just_started && (!comma && *s != ',' && *s != ')')) | |
1093 break; | |
1094 just_started = FALSE; | |
1095 } | |
1096 else if (cin_iscomment(s)) // ignore comments | |
1097 s = cin_skipcomment(s); | |
1098 else | |
1099 { | |
1100 ++s; | |
1101 just_started = FALSE; | |
1102 } | |
1103 } | |
1104 | |
1105 done: | |
1106 if (lnum != first_lnum && sp != NULL) | |
1107 *sp = ml_get(first_lnum); | |
1108 | |
1109 return retval; | |
1110 } | |
1111 | |
1112 static int | |
1113 cin_isif(char_u *p) | |
1114 { | |
1115 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2])); | |
1116 } | |
1117 | |
1118 static int | |
1119 cin_iselse( | |
1120 char_u *p) | |
1121 { | |
1122 if (*p == '}') // accept "} else" | |
1123 p = cin_skipcomment(p + 1); | |
1124 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4])); | |
1125 } | |
1126 | |
1127 static int | |
1128 cin_isdo(char_u *p) | |
1129 { | |
1130 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2])); | |
1131 } | |
1132 | |
1133 /* | |
1134 * Check if this is a "while" that should have a matching "do". | |
1135 * We only accept a "while (condition) ;", with only white space between the | |
1136 * ')' and ';'. The condition may be spread over several lines. | |
1137 */ | |
1138 static int | |
1139 cin_iswhileofdo (char_u *p, linenr_T lnum) // XXX | |
1140 { | |
1141 pos_T cursor_save; | |
1142 pos_T *trypos; | |
1143 int retval = FALSE; | |
1144 | |
1145 p = cin_skipcomment(p); | |
1146 if (*p == '}') // accept "} while (cond);" | |
1147 p = cin_skipcomment(p + 1); | |
1148 if (cin_starts_with(p, "while")) | |
1149 { | |
1150 cursor_save = curwin->w_cursor; | |
1151 curwin->w_cursor.lnum = lnum; | |
1152 curwin->w_cursor.col = 0; | |
1153 p = ml_get_curline(); | |
1154 while (*p && *p != 'w') // skip any '}', until the 'w' of the "while" | |
1155 { | |
1156 ++p; | |
1157 ++curwin->w_cursor.col; | |
1158 } | |
1159 if ((trypos = findmatchlimit(NULL, 0, 0, | |
1160 curbuf->b_ind_maxparen)) != NULL | |
1161 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';') | |
1162 retval = TRUE; | |
1163 curwin->w_cursor = cursor_save; | |
1164 } | |
1165 return retval; | |
1166 } | |
1167 | |
1168 /* | |
1169 * Check whether in "p" there is an "if", "for" or "while" before "*poffset". | |
1170 * Return 0 if there is none. | |
1171 * Otherwise return !0 and update "*poffset" to point to the place where the | |
1172 * string was found. | |
1173 */ | |
1174 static int | |
1175 cin_is_if_for_while_before_offset(char_u *line, int *poffset) | |
1176 { | |
1177 int offset = *poffset; | |
1178 | |
1179 if (offset-- < 2) | |
1180 return 0; | |
1181 while (offset > 2 && VIM_ISWHITE(line[offset])) | |
1182 --offset; | |
1183 | |
1184 offset -= 1; | |
1185 if (!STRNCMP(line + offset, "if", 2)) | |
1186 goto probablyFound; | |
1187 | |
1188 if (offset >= 1) | |
1189 { | |
1190 offset -= 1; | |
1191 if (!STRNCMP(line + offset, "for", 3)) | |
1192 goto probablyFound; | |
1193 | |
1194 if (offset >= 2) | |
1195 { | |
1196 offset -= 2; | |
1197 if (!STRNCMP(line + offset, "while", 5)) | |
1198 goto probablyFound; | |
1199 } | |
1200 } | |
1201 return 0; | |
1202 | |
1203 probablyFound: | |
1204 if (!offset || !vim_isIDc(line[offset - 1])) | |
1205 { | |
1206 *poffset = offset; | |
1207 return 1; | |
1208 } | |
1209 return 0; | |
1210 } | |
1211 | |
1212 /* | |
1213 * Return TRUE if we are at the end of a do-while. | |
1214 * do | |
1215 * nothing; | |
1216 * while (foo | |
1217 * && bar); <-- here | |
1218 * Adjust the cursor to the line with "while". | |
1219 */ | |
1220 static int | |
1221 cin_iswhileofdo_end(int terminated) | |
1222 { | |
1223 char_u *line; | |
1224 char_u *p; | |
1225 char_u *s; | |
1226 pos_T *trypos; | |
1227 int i; | |
1228 | |
1229 if (terminated != ';') // there must be a ';' at the end | |
1230 return FALSE; | |
1231 | |
1232 p = line = ml_get_curline(); | |
1233 while (*p != NUL) | |
1234 { | |
1235 p = cin_skipcomment(p); | |
1236 if (*p == ')') | |
1237 { | |
1238 s = skipwhite(p + 1); | |
1239 if (*s == ';' && cin_nocode(s + 1)) | |
1240 { | |
1241 // Found ");" at end of the line, now check there is "while" | |
1242 // before the matching '('. XXX | |
1243 i = (int)(p - line); | |
1244 curwin->w_cursor.col = i; | |
1245 trypos = find_match_paren(curbuf->b_ind_maxparen); | |
1246 if (trypos != NULL) | |
1247 { | 1561 { |
1248 s = cin_skipcomment(ml_get(trypos->lnum)); | 1562 /* Retabulate this string of white-space */ |
1249 if (*s == '}') // accept "} while (cond);" | 1563 |
1250 s = cin_skipcomment(s + 1); | 1564 /* len is virtual length of white string */ |
1251 if (cin_starts_with(s, "while")) | 1565 len = num_spaces = vcol - start_vcol; |
1566 num_tabs = 0; | |
1567 if (!curbuf->b_p_et) | |
1252 { | 1568 { |
1253 curwin->w_cursor.lnum = trypos->lnum; | 1569 #ifdef FEAT_VARTABS |
1254 return TRUE; | 1570 int t, s; |
1571 | |
1572 tabstop_fromto(start_vcol, vcol, | |
1573 curbuf->b_p_ts, new_vts_array, &t, &s); | |
1574 num_tabs = t; | |
1575 num_spaces = s; | |
1576 #else | |
1577 temp = new_ts - (start_vcol % new_ts); | |
1578 if (num_spaces >= temp) | |
1579 { | |
1580 num_spaces -= temp; | |
1581 num_tabs++; | |
1582 } | |
1583 num_tabs += num_spaces / new_ts; | |
1584 num_spaces -= (num_spaces / new_ts) * new_ts; | |
1585 #endif | |
1586 } | |
1587 if (curbuf->b_p_et || got_tab || | |
1588 (num_spaces + num_tabs < len)) | |
1589 { | |
1590 if (did_undo == FALSE) | |
1591 { | |
1592 did_undo = TRUE; | |
1593 if (u_save((linenr_T)(lnum - 1), | |
1594 (linenr_T)(lnum + 1)) == FAIL) | |
1595 { | |
1596 new_line = NULL; /* flag out-of-memory */ | |
1597 break; | |
1598 } | |
1599 } | |
1600 | |
1601 /* len is actual number of white characters used */ | |
1602 len = num_spaces + num_tabs; | |
1603 old_len = (long)STRLEN(ptr); | |
1604 new_line = alloc(old_len - col + start_col + len + 1); | |
1605 if (new_line == NULL) | |
1606 break; | |
1607 if (start_col > 0) | |
1608 mch_memmove(new_line, ptr, (size_t)start_col); | |
1609 mch_memmove(new_line + start_col + len, | |
1610 ptr + col, (size_t)(old_len - col + 1)); | |
1611 ptr = new_line + start_col; | |
1612 for (col = 0; col < len; col++) | |
1613 ptr[col] = (col < num_tabs) ? '\t' : ' '; | |
1614 ml_replace(lnum, new_line, FALSE); | |
1615 if (first_line == 0) | |
1616 first_line = lnum; | |
1617 last_line = lnum; | |
1618 ptr = new_line; | |
1619 col = start_col + len; | |
1255 } | 1620 } |
1256 } | 1621 } |
1257 | 1622 got_tab = FALSE; |
1258 // Searching may have made "line" invalid, get it again. | 1623 num_spaces = 0; |
1259 line = ml_get_curline(); | |
1260 p = line + i; | |
1261 } | 1624 } |
1262 } | 1625 if (ptr[col] == NUL) |
1263 if (*p != NUL) | 1626 break; |
1264 ++p; | 1627 vcol += chartabsize(ptr + col, (colnr_T)vcol); |
1265 } | 1628 if (has_mbyte) |
1266 return FALSE; | 1629 col += (*mb_ptr2len)(ptr + col); |
1267 } | 1630 else |
1268 | 1631 ++col; |
1269 static int | 1632 } |
1270 cin_isbreak(char_u *p) | 1633 if (new_line == NULL) /* out of memory */ |
1271 { | |
1272 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5])); | |
1273 } | |
1274 | |
1275 /* | |
1276 * Find the position of a C++ base-class declaration or | |
1277 * constructor-initialization. eg: | |
1278 * | |
1279 * class MyClass : | |
1280 * baseClass <-- here | |
1281 * class MyClass : public baseClass, | |
1282 * anotherBaseClass <-- here (should probably lineup ??) | |
1283 * MyClass::MyClass(...) : | |
1284 * baseClass(...) <-- here (constructor-initialization) | |
1285 * | |
1286 * This is a lot of guessing. Watch out for "cond ? func() : foo". | |
1287 */ | |
1288 static int | |
1289 cin_is_cpp_baseclass( | |
1290 cpp_baseclass_cache_T *cached) // input and output | |
1291 { | |
1292 lpos_T *pos = &cached->lpos; // find position | |
1293 char_u *s; | |
1294 int class_or_struct, lookfor_ctor_init, cpp_base_class; | |
1295 linenr_T lnum = curwin->w_cursor.lnum; | |
1296 char_u *line = ml_get_curline(); | |
1297 | |
1298 if (pos->lnum <= lnum) | |
1299 return cached->found; // Use the cached result | |
1300 | |
1301 pos->col = 0; | |
1302 | |
1303 s = skipwhite(line); | |
1304 if (*s == '#') // skip #define FOO x ? (x) : x | |
1305 return FALSE; | |
1306 s = cin_skipcomment(s); | |
1307 if (*s == NUL) | |
1308 return FALSE; | |
1309 | |
1310 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | |
1311 | |
1312 // Search for a line starting with '#', empty, ending in ';' or containing | |
1313 // '{' or '}' and start below it. This handles the following situations: | |
1314 // a = cond ? | |
1315 // func() : | |
1316 // asdf; | |
1317 // func::foo() | |
1318 // : something | |
1319 // {} | |
1320 // Foo::Foo (int one, int two) | |
1321 // : something(4), | |
1322 // somethingelse(3) | |
1323 // {} | |
1324 while (lnum > 1) | |
1325 { | |
1326 line = ml_get(lnum - 1); | |
1327 s = skipwhite(line); | |
1328 if (*s == '#' || *s == NUL) | |
1329 break; | 1634 break; |
1330 while (*s != NUL) | 1635 line_breakcheck(); |
1331 { | 1636 } |
1332 s = cin_skipcomment(s); | 1637 if (got_int) |
1333 if (*s == '{' || *s == '}' | 1638 emsg(_(e_interr)); |
1334 || (*s == ';' && cin_nocode(s + 1))) | 1639 |
1335 break; | 1640 #ifdef FEAT_VARTABS |
1336 if (*s != NUL) | 1641 // If a single value was given then it can be considered equal to |
1337 ++s; | 1642 // either the value of 'tabstop' or the value of 'vartabstop'. |
1338 } | 1643 if (tabstop_count(curbuf->b_p_vts_array) == 0 |
1339 if (*s != NUL) | 1644 && tabstop_count(new_vts_array) == 1 |
1340 break; | 1645 && curbuf->b_p_ts == tabstop_first(new_vts_array)) |
1341 --lnum; | 1646 ; /* not changed */ |
1342 } | 1647 else if (tabstop_count(curbuf->b_p_vts_array) > 0 |
1343 | 1648 && tabstop_eq(curbuf->b_p_vts_array, new_vts_array)) |
1344 pos->lnum = lnum; | 1649 ; /* not changed */ |
1345 line = ml_get(lnum); | 1650 else |
1346 s = line; | 1651 redraw_curbuf_later(NOT_VALID); |
1347 for (;;) | 1652 #else |
1348 { | 1653 if (curbuf->b_p_ts != new_ts) |
1349 if (*s == NUL) | 1654 redraw_curbuf_later(NOT_VALID); |
1350 { | 1655 #endif |
1351 if (lnum == curwin->w_cursor.lnum) | 1656 if (first_line != 0) |
1352 break; | 1657 changed_lines(first_line, 0, last_line + 1, 0L); |
1353 // Continue in the cursor line. | 1658 |
1354 line = ml_get(++lnum); | 1659 curwin->w_p_list = save_list; /* restore 'list' */ |
1355 s = line; | 1660 |
1356 } | 1661 #ifdef FEAT_VARTABS |
1357 if (s == line) | 1662 if (new_ts_str != NULL) /* set the new tabstop */ |
1358 { | 1663 { |
1359 // don't recognize "case (foo):" as a baseclass | 1664 // If 'vartabstop' is in use or if the value given to retab has more |
1360 if (cin_iscase(s, FALSE)) | 1665 // than one tabstop then update 'vartabstop'. |
1361 break; | 1666 int *old_vts_ary = curbuf->b_p_vts_array; |
1362 s = cin_skipcomment(line); | 1667 |
1363 if (*s == NUL) | 1668 if (tabstop_count(old_vts_ary) > 0 || tabstop_count(new_vts_array) > 1) |
1364 continue; | 1669 { |
1365 } | 1670 set_string_option_direct((char_u *)"vts", -1, new_ts_str, |
1366 | 1671 OPT_FREE|OPT_LOCAL, 0); |
1367 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"')) | 1672 curbuf->b_p_vts_array = new_vts_array; |
1368 s = skip_string(s) + 1; | 1673 vim_free(old_vts_ary); |
1369 else if (s[0] == ':') | |
1370 { | |
1371 if (s[1] == ':') | |
1372 { | |
1373 // skip double colon. It can't be a constructor | |
1374 // initialization any more | |
1375 lookfor_ctor_init = FALSE; | |
1376 s = cin_skipcomment(s + 2); | |
1377 } | |
1378 else if (lookfor_ctor_init || class_or_struct) | |
1379 { | |
1380 // we have something found, that looks like the start of | |
1381 // cpp-base-class-declaration or constructor-initialization | |
1382 cpp_base_class = TRUE; | |
1383 lookfor_ctor_init = class_or_struct = FALSE; | |
1384 pos->col = 0; | |
1385 s = cin_skipcomment(s + 1); | |
1386 } | |
1387 else | |
1388 s = cin_skipcomment(s + 1); | |
1389 } | |
1390 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5])) | |
1391 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6]))) | |
1392 { | |
1393 class_or_struct = TRUE; | |
1394 lookfor_ctor_init = FALSE; | |
1395 | |
1396 if (*s == 'c') | |
1397 s = cin_skipcomment(s + 5); | |
1398 else | |
1399 s = cin_skipcomment(s + 6); | |
1400 } | 1674 } |
1401 else | 1675 else |
1402 { | 1676 { |
1403 if (s[0] == '{' || s[0] == '}' || s[0] == ';') | 1677 // 'vartabstop' wasn't in use and a single value was given to |
1404 { | 1678 // retab then update 'tabstop'. |
1405 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE; | 1679 curbuf->b_p_ts = tabstop_first(new_vts_array); |
1406 } | 1680 vim_free(new_vts_array); |
1407 else if (s[0] == ')') | 1681 } |
1408 { | 1682 vim_free(new_ts_str); |
1409 // Constructor-initialization is assumed if we come across | 1683 } |
1410 // something like "):" | 1684 #else |
1411 class_or_struct = FALSE; | 1685 curbuf->b_p_ts = new_ts; |
1412 lookfor_ctor_init = TRUE; | 1686 #endif |
1413 } | 1687 coladvance(curwin->w_curswant); |
1414 else if (s[0] == '?') | 1688 |
1415 { | 1689 u_clearline(); |
1416 // Avoid seeing '() :' after '?' as constructor init. | 1690 } |
1417 return FALSE; | 1691 |
1418 } | 1692 #if (defined(FEAT_CINDENT) && defined(FEAT_EVAL)) || defined(PROTO) |
1419 else if (!vim_isIDc(s[0])) | |
1420 { | |
1421 // if it is not an identifier, we are wrong | |
1422 class_or_struct = FALSE; | |
1423 lookfor_ctor_init = FALSE; | |
1424 } | |
1425 else if (pos->col == 0) | |
1426 { | |
1427 // it can't be a constructor-initialization any more | |
1428 lookfor_ctor_init = FALSE; | |
1429 | |
1430 // the first statement starts here: lineup with this one... | |
1431 if (cpp_base_class) | |
1432 pos->col = (colnr_T)(s - line); | |
1433 } | |
1434 | |
1435 // When the line ends in a comma don't align with it. | |
1436 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1)) | |
1437 pos->col = 0; | |
1438 | |
1439 s = cin_skipcomment(s + 1); | |
1440 } | |
1441 } | |
1442 | |
1443 cached->found = cpp_base_class; | |
1444 if (cpp_base_class) | |
1445 pos->lnum = lnum; | |
1446 return cpp_base_class; | |
1447 } | |
1448 | |
1449 static int | |
1450 get_baseclass_amount(int col) | |
1451 { | |
1452 int amount; | |
1453 colnr_T vcol; | |
1454 pos_T *trypos; | |
1455 | |
1456 if (col == 0) | |
1457 { | |
1458 amount = get_indent(); | |
1459 if (find_last_paren(ml_get_curline(), '(', ')') | |
1460 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) | |
1461 amount = get_indent_lnum(trypos->lnum); // XXX | |
1462 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL)) | |
1463 amount += curbuf->b_ind_cpp_baseclass; | |
1464 } | |
1465 else | |
1466 { | |
1467 curwin->w_cursor.col = col; | |
1468 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); | |
1469 amount = (int)vcol; | |
1470 } | |
1471 if (amount < curbuf->b_ind_cpp_baseclass) | |
1472 amount = curbuf->b_ind_cpp_baseclass; | |
1473 return amount; | |
1474 } | |
1475 | |
1476 /* | |
1477 * Return TRUE if string "s" ends with the string "find", possibly followed by | |
1478 * white space and comments. Skip strings and comments. | |
1479 * Ignore "ignore" after "find" if it's not NULL. | |
1480 */ | |
1481 static int | |
1482 cin_ends_in(char_u *s, char_u *find, char_u *ignore) | |
1483 { | |
1484 char_u *p = s; | |
1485 char_u *r; | |
1486 int len = (int)STRLEN(find); | |
1487 | |
1488 while (*p != NUL) | |
1489 { | |
1490 p = cin_skipcomment(p); | |
1491 if (STRNCMP(p, find, len) == 0) | |
1492 { | |
1493 r = skipwhite(p + len); | |
1494 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0) | |
1495 r = skipwhite(r + STRLEN(ignore)); | |
1496 if (cin_nocode(r)) | |
1497 return TRUE; | |
1498 } | |
1499 if (*p != NUL) | |
1500 ++p; | |
1501 } | |
1502 return FALSE; | |
1503 } | |
1504 | |
1505 /* | |
1506 * Return TRUE when "s" starts with "word" and then a non-ID character. | |
1507 */ | |
1508 static int | |
1509 cin_starts_with(char_u *s, char *word) | |
1510 { | |
1511 int l = (int)STRLEN(word); | |
1512 | |
1513 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l])); | |
1514 } | |
1515 | |
1516 /* | |
1517 * Skip strings, chars and comments until at or past "trypos". | |
1518 * Return the column found. | |
1519 */ | |
1520 static int | |
1521 cin_skip2pos(pos_T *trypos) | |
1522 { | |
1523 char_u *line; | |
1524 char_u *p; | |
1525 char_u *new_p; | |
1526 | |
1527 p = line = ml_get(trypos->lnum); | |
1528 while (*p && (colnr_T)(p - line) < trypos->col) | |
1529 { | |
1530 if (cin_iscomment(p)) | |
1531 p = cin_skipcomment(p); | |
1532 else | |
1533 { | |
1534 new_p = skip_string(p); | |
1535 if (new_p == p) | |
1536 ++p; | |
1537 else | |
1538 p = new_p; | |
1539 } | |
1540 } | |
1541 return (int)(p - line); | |
1542 } | |
1543 | |
1544 /* | |
1545 * Find the '{' at the start of the block we are in. | |
1546 * Return NULL if no match found. | |
1547 * Ignore a '{' that is in a comment, makes indenting the next three lines | |
1548 * work. | |
1549 */ | |
1550 /* foo() */ | |
1551 /* { */ | |
1552 /* } */ | |
1553 | |
1554 static pos_T * | |
1555 find_start_brace(void) // XXX | |
1556 { | |
1557 pos_T cursor_save; | |
1558 pos_T *trypos; | |
1559 pos_T *pos; | |
1560 static pos_T pos_copy; | |
1561 | |
1562 cursor_save = curwin->w_cursor; | |
1563 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL) | |
1564 { | |
1565 pos_copy = *trypos; // copy pos_T, next findmatch will change it | |
1566 trypos = &pos_copy; | |
1567 curwin->w_cursor = *trypos; | |
1568 pos = NULL; | |
1569 // ignore the { if it's in a // or / * * / comment | |
1570 if ((colnr_T)cin_skip2pos(trypos) == trypos->col | |
1571 && (pos = ind_find_start_CORS(NULL)) == NULL) // XXX | |
1572 break; | |
1573 if (pos != NULL) | |
1574 curwin->w_cursor.lnum = pos->lnum; | |
1575 } | |
1576 curwin->w_cursor = cursor_save; | |
1577 return trypos; | |
1578 } | |
1579 | |
1580 /* | |
1581 * Find the matching '(', ignoring it if it is in a comment. | |
1582 * Return NULL if no match found. | |
1583 */ | |
1584 static pos_T * | |
1585 find_match_paren(int ind_maxparen) // XXX | |
1586 { | |
1587 return find_match_char('(', ind_maxparen); | |
1588 } | |
1589 | |
1590 static pos_T * | |
1591 find_match_char(int c, int ind_maxparen) // XXX | |
1592 { | |
1593 pos_T cursor_save; | |
1594 pos_T *trypos; | |
1595 static pos_T pos_copy; | |
1596 int ind_maxp_wk; | |
1597 | |
1598 cursor_save = curwin->w_cursor; | |
1599 ind_maxp_wk = ind_maxparen; | |
1600 retry: | |
1601 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL) | |
1602 { | |
1603 // check if the ( is in a // comment | |
1604 if ((colnr_T)cin_skip2pos(trypos) > trypos->col) | |
1605 { | |
1606 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum); | |
1607 if (ind_maxp_wk > 0) | |
1608 { | |
1609 curwin->w_cursor = *trypos; | |
1610 curwin->w_cursor.col = 0; // XXX | |
1611 goto retry; | |
1612 } | |
1613 trypos = NULL; | |
1614 } | |
1615 else | |
1616 { | |
1617 pos_T *trypos_wk; | |
1618 | |
1619 pos_copy = *trypos; // copy trypos, findmatch will change it | |
1620 trypos = &pos_copy; | |
1621 curwin->w_cursor = *trypos; | |
1622 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) // XXX | |
1623 { | |
1624 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum | |
1625 - trypos_wk->lnum); | |
1626 if (ind_maxp_wk > 0) | |
1627 { | |
1628 curwin->w_cursor = *trypos_wk; | |
1629 goto retry; | |
1630 } | |
1631 trypos = NULL; | |
1632 } | |
1633 } | |
1634 } | |
1635 curwin->w_cursor = cursor_save; | |
1636 return trypos; | |
1637 } | |
1638 | |
1639 /* | |
1640 * Find the matching '(', ignoring it if it is in a comment or before an | |
1641 * unmatched {. | |
1642 * Return NULL if no match found. | |
1643 */ | |
1644 static pos_T * | |
1645 find_match_paren_after_brace (int ind_maxparen) // XXX | |
1646 { | |
1647 pos_T *trypos = find_match_paren(ind_maxparen); | |
1648 | |
1649 if (trypos != NULL) | |
1650 { | |
1651 pos_T *tryposBrace = find_start_brace(); | |
1652 | |
1653 // If both an unmatched '(' and '{' is found. Ignore the '(' | |
1654 // position if the '{' is further down. | |
1655 if (tryposBrace != NULL | |
1656 && (trypos->lnum != tryposBrace->lnum | |
1657 ? trypos->lnum < tryposBrace->lnum | |
1658 : trypos->col < tryposBrace->col)) | |
1659 trypos = NULL; | |
1660 } | |
1661 return trypos; | |
1662 } | |
1663 | |
1664 /* | |
1665 * Return ind_maxparen corrected for the difference in line number between the | |
1666 * cursor position and "startpos". This makes sure that searching for a | |
1667 * matching paren above the cursor line doesn't find a match because of | |
1668 * looking a few lines further. | |
1669 */ | |
1670 static int | |
1671 corr_ind_maxparen(pos_T *startpos) | |
1672 { | |
1673 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum; | |
1674 | |
1675 if (n > 0 && n < curbuf->b_ind_maxparen / 2) | |
1676 return curbuf->b_ind_maxparen - (int)n; | |
1677 return curbuf->b_ind_maxparen; | |
1678 } | |
1679 | |
1680 /* | |
1681 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in | |
1682 * line "l". "l" must point to the start of the line. | |
1683 */ | |
1684 static int | |
1685 find_last_paren(char_u *l, int start, int end) | |
1686 { | |
1687 int i; | |
1688 int retval = FALSE; | |
1689 int open_count = 0; | |
1690 | |
1691 curwin->w_cursor.col = 0; // default is start of line | |
1692 | |
1693 for (i = 0; l[i] != NUL; i++) | |
1694 { | |
1695 i = (int)(cin_skipcomment(l + i) - l); // ignore parens in comments | |
1696 i = (int)(skip_string(l + i) - l); // ignore parens in quotes | |
1697 if (l[i] == start) | |
1698 ++open_count; | |
1699 else if (l[i] == end) | |
1700 { | |
1701 if (open_count > 0) | |
1702 --open_count; | |
1703 else | |
1704 { | |
1705 curwin->w_cursor.col = i; | |
1706 retval = TRUE; | |
1707 } | |
1708 } | |
1709 } | |
1710 return retval; | |
1711 } | |
1712 | |
1713 /* | |
1714 * Parse 'cinoptions' and set the values in "curbuf". | |
1715 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes. | |
1716 */ | |
1717 void | |
1718 parse_cino(buf_T *buf) | |
1719 { | |
1720 char_u *p; | |
1721 char_u *l; | |
1722 char_u *digits; | |
1723 int n; | |
1724 int divider; | |
1725 int fraction = 0; | |
1726 int sw = (int)get_sw_value(buf); | |
1727 | |
1728 // Set the default values. | |
1729 | |
1730 // Spaces from a block's opening brace the prevailing indent for that | |
1731 // block should be. | |
1732 buf->b_ind_level = sw; | |
1733 | |
1734 // Spaces from the edge of the line an open brace that's at the end of a | |
1735 // line is imagined to be. | |
1736 buf->b_ind_open_imag = 0; | |
1737 | |
1738 // Spaces from the prevailing indent for a line that is not preceded by | |
1739 // an opening brace. | |
1740 buf->b_ind_no_brace = 0; | |
1741 | |
1742 // Column where the first { of a function should be located }. | |
1743 buf->b_ind_first_open = 0; | |
1744 | |
1745 // Spaces from the prevailing indent a leftmost open brace should be | |
1746 // located. | |
1747 buf->b_ind_open_extra = 0; | |
1748 | |
1749 // Spaces from the matching open brace (real location for one at the left | |
1750 // edge; imaginary location from one that ends a line) the matching close | |
1751 // brace should be located. | |
1752 buf->b_ind_close_extra = 0; | |
1753 | |
1754 // Spaces from the edge of the line an open brace sitting in the leftmost | |
1755 // column is imagined to be. | |
1756 buf->b_ind_open_left_imag = 0; | |
1757 | |
1758 // Spaces jump labels should be shifted to the left if N is non-negative, | |
1759 // otherwise the jump label will be put to column 1. | |
1760 buf->b_ind_jump_label = -1; | |
1761 | |
1762 // Spaces from the switch() indent a "case xx" label should be located. | |
1763 buf->b_ind_case = sw; | |
1764 | |
1765 // Spaces from the "case xx:" code after a switch() should be located. | |
1766 buf->b_ind_case_code = sw; | |
1767 | |
1768 // Lineup break at end of case in switch() with case label. | |
1769 buf->b_ind_case_break = 0; | |
1770 | |
1771 // Spaces from the class declaration indent a scope declaration label | |
1772 // should be located. | |
1773 buf->b_ind_scopedecl = sw; | |
1774 | |
1775 // Spaces from the scope declaration label code should be located. | |
1776 buf->b_ind_scopedecl_code = sw; | |
1777 | |
1778 // Amount K&R-style parameters should be indented. | |
1779 buf->b_ind_param = sw; | |
1780 | |
1781 // Amount a function type spec should be indented. | |
1782 buf->b_ind_func_type = sw; | |
1783 | |
1784 // Amount a cpp base class declaration or constructor initialization | |
1785 // should be indented. | |
1786 buf->b_ind_cpp_baseclass = sw; | |
1787 | |
1788 // additional spaces beyond the prevailing indent a continuation line | |
1789 // should be located. | |
1790 buf->b_ind_continuation = sw; | |
1791 | |
1792 // Spaces from the indent of the line with an unclosed parentheses. | |
1793 buf->b_ind_unclosed = sw * 2; | |
1794 | |
1795 // Spaces from the indent of the line with an unclosed parentheses, which | |
1796 // itself is also unclosed. | |
1797 buf->b_ind_unclosed2 = sw; | |
1798 | |
1799 // Suppress ignoring spaces from the indent of a line starting with an | |
1800 // unclosed parentheses. | |
1801 buf->b_ind_unclosed_noignore = 0; | |
1802 | |
1803 // If the opening paren is the last nonwhite character on the line, and | |
1804 // b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer | |
1805 // context (for very long lines). | |
1806 buf->b_ind_unclosed_wrapped = 0; | |
1807 | |
1808 // Suppress ignoring white space when lining up with the character after | |
1809 // an unclosed parentheses. | |
1810 buf->b_ind_unclosed_whiteok = 0; | |
1811 | |
1812 // Indent a closing parentheses under the line start of the matching | |
1813 // opening parentheses. | |
1814 buf->b_ind_matching_paren = 0; | |
1815 | |
1816 // Indent a closing parentheses under the previous line. | |
1817 buf->b_ind_paren_prev = 0; | |
1818 | |
1819 // Extra indent for comments. | |
1820 buf->b_ind_comment = 0; | |
1821 | |
1822 // Spaces from the comment opener when there is nothing after it. | |
1823 buf->b_ind_in_comment = 3; | |
1824 | |
1825 // Boolean: if non-zero, use b_ind_in_comment even if there is something | |
1826 // after the comment opener. | |
1827 buf->b_ind_in_comment2 = 0; | |
1828 | |
1829 // Max lines to search for an open paren. | |
1830 buf->b_ind_maxparen = 20; | |
1831 | |
1832 // Max lines to search for an open comment. | |
1833 buf->b_ind_maxcomment = 70; | |
1834 | |
1835 // Handle braces for java code. | |
1836 buf->b_ind_java = 0; | |
1837 | |
1838 // Not to confuse JS object properties with labels. | |
1839 buf->b_ind_js = 0; | |
1840 | |
1841 // Handle blocked cases correctly. | |
1842 buf->b_ind_keep_case_label = 0; | |
1843 | |
1844 // Handle C++ namespace. | |
1845 buf->b_ind_cpp_namespace = 0; | |
1846 | |
1847 // Handle continuation lines containing conditions of if(), for() and | |
1848 // while(). | |
1849 buf->b_ind_if_for_while = 0; | |
1850 | |
1851 // indentation for # comments | |
1852 buf->b_ind_hash_comment = 0; | |
1853 | |
1854 // Handle C++ extern "C" or "C++" | |
1855 buf->b_ind_cpp_extern_c = 0; | |
1856 | |
1857 for (p = buf->b_p_cino; *p; ) | |
1858 { | |
1859 l = p++; | |
1860 if (*p == '-') | |
1861 ++p; | |
1862 digits = p; // remember where the digits start | |
1863 n = getdigits(&p); | |
1864 divider = 0; | |
1865 if (*p == '.') // ".5s" means a fraction | |
1866 { | |
1867 fraction = atol((char *)++p); | |
1868 while (VIM_ISDIGIT(*p)) | |
1869 { | |
1870 ++p; | |
1871 if (divider) | |
1872 divider *= 10; | |
1873 else | |
1874 divider = 10; | |
1875 } | |
1876 } | |
1877 if (*p == 's') // "2s" means two times 'shiftwidth' | |
1878 { | |
1879 if (p == digits) | |
1880 n = sw; // just "s" is one 'shiftwidth' | |
1881 else | |
1882 { | |
1883 n *= sw; | |
1884 if (divider) | |
1885 n += (sw * fraction + divider / 2) / divider; | |
1886 } | |
1887 ++p; | |
1888 } | |
1889 if (l[1] == '-') | |
1890 n = -n; | |
1891 | |
1892 // When adding an entry here, also update the default 'cinoptions' in | |
1893 // doc/indent.txt, and add explanation for it! | |
1894 switch (*l) | |
1895 { | |
1896 case '>': buf->b_ind_level = n; break; | |
1897 case 'e': buf->b_ind_open_imag = n; break; | |
1898 case 'n': buf->b_ind_no_brace = n; break; | |
1899 case 'f': buf->b_ind_first_open = n; break; | |
1900 case '{': buf->b_ind_open_extra = n; break; | |
1901 case '}': buf->b_ind_close_extra = n; break; | |
1902 case '^': buf->b_ind_open_left_imag = n; break; | |
1903 case 'L': buf->b_ind_jump_label = n; break; | |
1904 case ':': buf->b_ind_case = n; break; | |
1905 case '=': buf->b_ind_case_code = n; break; | |
1906 case 'b': buf->b_ind_case_break = n; break; | |
1907 case 'p': buf->b_ind_param = n; break; | |
1908 case 't': buf->b_ind_func_type = n; break; | |
1909 case '/': buf->b_ind_comment = n; break; | |
1910 case 'c': buf->b_ind_in_comment = n; break; | |
1911 case 'C': buf->b_ind_in_comment2 = n; break; | |
1912 case 'i': buf->b_ind_cpp_baseclass = n; break; | |
1913 case '+': buf->b_ind_continuation = n; break; | |
1914 case '(': buf->b_ind_unclosed = n; break; | |
1915 case 'u': buf->b_ind_unclosed2 = n; break; | |
1916 case 'U': buf->b_ind_unclosed_noignore = n; break; | |
1917 case 'W': buf->b_ind_unclosed_wrapped = n; break; | |
1918 case 'w': buf->b_ind_unclosed_whiteok = n; break; | |
1919 case 'm': buf->b_ind_matching_paren = n; break; | |
1920 case 'M': buf->b_ind_paren_prev = n; break; | |
1921 case ')': buf->b_ind_maxparen = n; break; | |
1922 case '*': buf->b_ind_maxcomment = n; break; | |
1923 case 'g': buf->b_ind_scopedecl = n; break; | |
1924 case 'h': buf->b_ind_scopedecl_code = n; break; | |
1925 case 'j': buf->b_ind_java = n; break; | |
1926 case 'J': buf->b_ind_js = n; break; | |
1927 case 'l': buf->b_ind_keep_case_label = n; break; | |
1928 case '#': buf->b_ind_hash_comment = n; break; | |
1929 case 'N': buf->b_ind_cpp_namespace = n; break; | |
1930 case 'k': buf->b_ind_if_for_while = n; break; | |
1931 case 'E': buf->b_ind_cpp_extern_c = n; break; | |
1932 } | |
1933 if (*p == ',') | |
1934 ++p; | |
1935 } | |
1936 } | |
1937 | |
1938 /* | |
1939 * Return the desired indent for C code. | |
1940 * Return -1 if the indent should be left alone (inside a raw string). | |
1941 */ | |
1942 int | |
1943 get_c_indent(void) | |
1944 { | |
1945 pos_T cur_curpos; | |
1946 int amount; | |
1947 int scope_amount; | |
1948 int cur_amount = MAXCOL; | |
1949 colnr_T col; | |
1950 char_u *theline; | |
1951 char_u *linecopy; | |
1952 pos_T *trypos; | |
1953 pos_T *comment_pos; | |
1954 pos_T *tryposBrace = NULL; | |
1955 pos_T tryposCopy; | |
1956 pos_T our_paren_pos; | |
1957 char_u *start; | |
1958 int start_brace; | |
1959 #define BRACE_IN_COL0 1 // '{' is in column 0 | |
1960 #define BRACE_AT_START 2 // '{' is at start of line | |
1961 #define BRACE_AT_END 3 // '{' is at end of line | |
1962 linenr_T ourscope; | |
1963 char_u *l; | |
1964 char_u *look; | |
1965 char_u terminated; | |
1966 int lookfor; | |
1967 #define LOOKFOR_INITIAL 0 | |
1968 #define LOOKFOR_IF 1 | |
1969 #define LOOKFOR_DO 2 | |
1970 #define LOOKFOR_CASE 3 | |
1971 #define LOOKFOR_ANY 4 | |
1972 #define LOOKFOR_TERM 5 | |
1973 #define LOOKFOR_UNTERM 6 | |
1974 #define LOOKFOR_SCOPEDECL 7 | |
1975 #define LOOKFOR_NOBREAK 8 | |
1976 #define LOOKFOR_CPP_BASECLASS 9 | |
1977 #define LOOKFOR_ENUM_OR_INIT 10 | |
1978 #define LOOKFOR_JS_KEY 11 | |
1979 #define LOOKFOR_COMMA 12 | |
1980 | |
1981 int whilelevel; | |
1982 linenr_T lnum; | |
1983 int n; | |
1984 int iscase; | |
1985 int lookfor_break; | |
1986 int lookfor_cpp_namespace = FALSE; | |
1987 int cont_amount = 0; // amount for continuation line | |
1988 int original_line_islabel; | |
1989 int added_to_amount = 0; | |
1990 int js_cur_has_key = 0; | |
1991 linenr_T raw_string_start = 0; | |
1992 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } }; | |
1993 | |
1994 // make a copy, value is changed below | |
1995 int ind_continuation = curbuf->b_ind_continuation; | |
1996 | |
1997 // remember where the cursor was when we started | |
1998 cur_curpos = curwin->w_cursor; | |
1999 | |
2000 // if we are at line 1 zero indent is fine, right? | |
2001 if (cur_curpos.lnum == 1) | |
2002 return 0; | |
2003 | |
2004 // Get a copy of the current contents of the line. | |
2005 // This is required, because only the most recent line obtained with | |
2006 // ml_get is valid! | |
2007 linecopy = vim_strsave(ml_get(cur_curpos.lnum)); | |
2008 if (linecopy == NULL) | |
2009 return 0; | |
2010 | |
2011 // In insert mode and the cursor is on a ')' truncate the line at the | |
2012 // cursor position. We don't want to line up with the matching '(' when | |
2013 // inserting new stuff. | |
2014 // For unknown reasons the cursor might be past the end of the line, thus | |
2015 // check for that. | |
2016 if ((State & INSERT) | |
2017 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy) | |
2018 && linecopy[curwin->w_cursor.col] == ')') | |
2019 linecopy[curwin->w_cursor.col] = NUL; | |
2020 | |
2021 theline = skipwhite(linecopy); | |
2022 | |
2023 // move the cursor to the start of the line | |
2024 | |
2025 curwin->w_cursor.col = 0; | |
2026 | |
2027 original_line_islabel = cin_islabel(); // XXX | |
2028 | |
2029 // If we are inside a raw string don't change the indent. | |
2030 // Ignore a raw string inside a comment. | |
2031 comment_pos = ind_find_start_comment(); | |
2032 if (comment_pos != NULL) | |
2033 { | |
2034 // findmatchlimit() static pos is overwritten, make a copy | |
2035 tryposCopy = *comment_pos; | |
2036 comment_pos = &tryposCopy; | |
2037 } | |
2038 trypos = find_start_rawstring(curbuf->b_ind_maxcomment); | |
2039 if (trypos != NULL && (comment_pos == NULL | |
2040 || LT_POS(*trypos, *comment_pos))) | |
2041 { | |
2042 amount = -1; | |
2043 goto laterend; | |
2044 } | |
2045 | |
2046 // #defines and so on always go at the left when included in 'cinkeys'. | |
2047 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE))) | |
2048 { | |
2049 amount = curbuf->b_ind_hash_comment; | |
2050 goto theend; | |
2051 } | |
2052 | |
2053 // Is it a non-case label? Then that goes at the left margin too unless: | |
2054 // - JS flag is set. | |
2055 // - 'L' item has a positive value. | |
2056 if (original_line_islabel && !curbuf->b_ind_js | |
2057 && curbuf->b_ind_jump_label < 0) | |
2058 { | |
2059 amount = 0; | |
2060 goto theend; | |
2061 } | |
2062 | |
2063 // If we're inside a "//" comment and there is a "//" comment in a | |
2064 // previous line, lineup with that one. | |
2065 if (cin_islinecomment(theline) | |
2066 && (trypos = find_line_comment()) != NULL) // XXX | |
2067 { | |
2068 // find how indented the line beginning the comment is | |
2069 getvcol(curwin, trypos, &col, NULL, NULL); | |
2070 amount = col; | |
2071 goto theend; | |
2072 } | |
2073 | |
2074 // If we're inside a comment and not looking at the start of the | |
2075 // comment, try using the 'comments' option. | |
2076 if (!cin_iscomment(theline) && comment_pos != NULL) // XXX | |
2077 { | |
2078 int lead_start_len = 2; | |
2079 int lead_middle_len = 1; | |
2080 char_u lead_start[COM_MAX_LEN]; // start-comment string | |
2081 char_u lead_middle[COM_MAX_LEN]; // middle-comment string | |
2082 char_u lead_end[COM_MAX_LEN]; // end-comment string | |
2083 char_u *p; | |
2084 int start_align = 0; | |
2085 int start_off = 0; | |
2086 int done = FALSE; | |
2087 | |
2088 // find how indented the line beginning the comment is | |
2089 getvcol(curwin, comment_pos, &col, NULL, NULL); | |
2090 amount = col; | |
2091 *lead_start = NUL; | |
2092 *lead_middle = NUL; | |
2093 | |
2094 p = curbuf->b_p_com; | |
2095 while (*p != NUL) | |
2096 { | |
2097 int align = 0; | |
2098 int off = 0; | |
2099 int what = 0; | |
2100 | |
2101 while (*p != NUL && *p != ':') | |
2102 { | |
2103 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE) | |
2104 what = *p++; | |
2105 else if (*p == COM_LEFT || *p == COM_RIGHT) | |
2106 align = *p++; | |
2107 else if (VIM_ISDIGIT(*p) || *p == '-') | |
2108 off = getdigits(&p); | |
2109 else | |
2110 ++p; | |
2111 } | |
2112 | |
2113 if (*p == ':') | |
2114 ++p; | |
2115 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ","); | |
2116 if (what == COM_START) | |
2117 { | |
2118 STRCPY(lead_start, lead_end); | |
2119 lead_start_len = (int)STRLEN(lead_start); | |
2120 start_off = off; | |
2121 start_align = align; | |
2122 } | |
2123 else if (what == COM_MIDDLE) | |
2124 { | |
2125 STRCPY(lead_middle, lead_end); | |
2126 lead_middle_len = (int)STRLEN(lead_middle); | |
2127 } | |
2128 else if (what == COM_END) | |
2129 { | |
2130 // If our line starts with the middle comment string, line it | |
2131 // up with the comment opener per the 'comments' option. | |
2132 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0 | |
2133 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0) | |
2134 { | |
2135 done = TRUE; | |
2136 if (curwin->w_cursor.lnum > 1) | |
2137 { | |
2138 // If the start comment string matches in the previous | |
2139 // line, use the indent of that line plus offset. If | |
2140 // the middle comment string matches in the previous | |
2141 // line, use the indent of that line. XXX | |
2142 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1)); | |
2143 if (STRNCMP(look, lead_start, lead_start_len) == 0) | |
2144 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
2145 else if (STRNCMP(look, lead_middle, | |
2146 lead_middle_len) == 0) | |
2147 { | |
2148 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
2149 break; | |
2150 } | |
2151 // If the start comment string doesn't match with the | |
2152 // start of the comment, skip this entry. XXX | |
2153 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col, | |
2154 lead_start, lead_start_len) != 0) | |
2155 continue; | |
2156 } | |
2157 if (start_off != 0) | |
2158 amount += start_off; | |
2159 else if (start_align == COM_RIGHT) | |
2160 amount += vim_strsize(lead_start) | |
2161 - vim_strsize(lead_middle); | |
2162 break; | |
2163 } | |
2164 | |
2165 // If our line starts with the end comment string, line it up | |
2166 // with the middle comment | |
2167 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0 | |
2168 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0) | |
2169 { | |
2170 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); | |
2171 // XXX | |
2172 if (off != 0) | |
2173 amount += off; | |
2174 else if (align == COM_RIGHT) | |
2175 amount += vim_strsize(lead_start) | |
2176 - vim_strsize(lead_middle); | |
2177 done = TRUE; | |
2178 break; | |
2179 } | |
2180 } | |
2181 } | |
2182 | |
2183 // If our line starts with an asterisk, line up with the | |
2184 // asterisk in the comment opener; otherwise, line up | |
2185 // with the first character of the comment text. | |
2186 if (done) | |
2187 ; | |
2188 else if (theline[0] == '*') | |
2189 amount += 1; | |
2190 else | |
2191 { | |
2192 // If we are more than one line away from the comment opener, take | |
2193 // the indent of the previous non-empty line. If 'cino' has "CO" | |
2194 // and we are just below the comment opener and there are any | |
2195 // white characters after it line up with the text after it; | |
2196 // otherwise, add the amount specified by "c" in 'cino' | |
2197 amount = -1; | |
2198 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum) | |
2199 { | |
2200 if (linewhite(lnum)) // skip blank lines | |
2201 continue; | |
2202 amount = get_indent_lnum(lnum); // XXX | |
2203 break; | |
2204 } | |
2205 if (amount == -1) // use the comment opener | |
2206 { | |
2207 if (!curbuf->b_ind_in_comment2) | |
2208 { | |
2209 start = ml_get(comment_pos->lnum); | |
2210 look = start + comment_pos->col + 2; // skip / and * | |
2211 if (*look != NUL) // if something after it | |
2212 comment_pos->col = (colnr_T)(skipwhite(look) - start); | |
2213 } | |
2214 getvcol(curwin, comment_pos, &col, NULL, NULL); | |
2215 amount = col; | |
2216 if (curbuf->b_ind_in_comment2 || *look == NUL) | |
2217 amount += curbuf->b_ind_in_comment; | |
2218 } | |
2219 } | |
2220 goto theend; | |
2221 } | |
2222 | |
2223 // Are we looking at a ']' that has a match? | |
2224 if (*skipwhite(theline) == ']' | |
2225 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL) | |
2226 { | |
2227 // align with the line containing the '['. | |
2228 amount = get_indent_lnum(trypos->lnum); | |
2229 goto theend; | |
2230 } | |
2231 | |
2232 // Are we inside parentheses or braces? XXX | |
2233 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL | |
2234 && curbuf->b_ind_java == 0) | |
2235 || (tryposBrace = find_start_brace()) != NULL | |
2236 || trypos != NULL) | |
2237 { | |
2238 if (trypos != NULL && tryposBrace != NULL) | |
2239 { | |
2240 // Both an unmatched '(' and '{' is found. Use the one which is | |
2241 // closer to the current cursor position, set the other to NULL. | |
2242 if (trypos->lnum != tryposBrace->lnum | |
2243 ? trypos->lnum < tryposBrace->lnum | |
2244 : trypos->col < tryposBrace->col) | |
2245 trypos = NULL; | |
2246 else | |
2247 tryposBrace = NULL; | |
2248 } | |
2249 | |
2250 if (trypos != NULL) | |
2251 { | |
2252 // If the matching paren is more than one line away, use the indent of | |
2253 // a previous non-empty line that matches the same paren. | |
2254 if (theline[0] == ')' && curbuf->b_ind_paren_prev) | |
2255 { | |
2256 // Line up with the start of the matching paren line. | |
2257 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX | |
2258 } | |
2259 else | |
2260 { | |
2261 amount = -1; | |
2262 our_paren_pos = *trypos; | |
2263 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum) | |
2264 { | |
2265 l = skipwhite(ml_get(lnum)); | |
2266 if (cin_nocode(l)) // skip comment lines | |
2267 continue; | |
2268 if (cin_ispreproc_cont(&l, &lnum, &amount)) | |
2269 continue; // ignore #define, #if, etc. | |
2270 curwin->w_cursor.lnum = lnum; | |
2271 | |
2272 // Skip a comment or raw string. XXX | |
2273 if ((trypos = ind_find_start_CORS(NULL)) != NULL) | |
2274 { | |
2275 lnum = trypos->lnum + 1; | |
2276 continue; | |
2277 } | |
2278 | |
2279 // XXX | |
2280 if ((trypos = find_match_paren( | |
2281 corr_ind_maxparen(&cur_curpos))) != NULL | |
2282 && trypos->lnum == our_paren_pos.lnum | |
2283 && trypos->col == our_paren_pos.col) | |
2284 { | |
2285 amount = get_indent_lnum(lnum); // XXX | |
2286 | |
2287 if (theline[0] == ')') | |
2288 { | |
2289 if (our_paren_pos.lnum != lnum | |
2290 && cur_amount > amount) | |
2291 cur_amount = amount; | |
2292 amount = -1; | |
2293 } | |
2294 break; | |
2295 } | |
2296 } | |
2297 } | |
2298 | |
2299 // Line up with line where the matching paren is. XXX | |
2300 // If the line starts with a '(' or the indent for unclosed | |
2301 // parentheses is zero, line up with the unclosed parentheses. | |
2302 if (amount == -1) | |
2303 { | |
2304 int ignore_paren_col = 0; | |
2305 int is_if_for_while = 0; | |
2306 | |
2307 if (curbuf->b_ind_if_for_while) | |
2308 { | |
2309 // Look for the outermost opening parenthesis on this line | |
2310 // and check whether it belongs to an "if", "for" or "while". | |
2311 | |
2312 pos_T cursor_save = curwin->w_cursor; | |
2313 pos_T outermost; | |
2314 char_u *line; | |
2315 | |
2316 trypos = &our_paren_pos; | |
2317 do { | |
2318 outermost = *trypos; | |
2319 curwin->w_cursor.lnum = outermost.lnum; | |
2320 curwin->w_cursor.col = outermost.col; | |
2321 | |
2322 trypos = find_match_paren(curbuf->b_ind_maxparen); | |
2323 } while (trypos && trypos->lnum == outermost.lnum); | |
2324 | |
2325 curwin->w_cursor = cursor_save; | |
2326 | |
2327 line = ml_get(outermost.lnum); | |
2328 | |
2329 is_if_for_while = | |
2330 cin_is_if_for_while_before_offset(line, &outermost.col); | |
2331 } | |
2332 | |
2333 amount = skip_label(our_paren_pos.lnum, &look); | |
2334 look = skipwhite(look); | |
2335 if (*look == '(') | |
2336 { | |
2337 linenr_T save_lnum = curwin->w_cursor.lnum; | |
2338 char_u *line; | |
2339 int look_col; | |
2340 | |
2341 // Ignore a '(' in front of the line that has a match before | |
2342 // our matching '('. | |
2343 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
2344 line = ml_get_curline(); | |
2345 look_col = (int)(look - line); | |
2346 curwin->w_cursor.col = look_col + 1; | |
2347 if ((trypos = findmatchlimit(NULL, ')', 0, | |
2348 curbuf->b_ind_maxparen)) | |
2349 != NULL | |
2350 && trypos->lnum == our_paren_pos.lnum | |
2351 && trypos->col < our_paren_pos.col) | |
2352 ignore_paren_col = trypos->col + 1; | |
2353 | |
2354 curwin->w_cursor.lnum = save_lnum; | |
2355 look = ml_get(our_paren_pos.lnum) + look_col; | |
2356 } | |
2357 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0 | |
2358 && is_if_for_while == 0) | |
2359 || (!curbuf->b_ind_unclosed_noignore && *look == '(' | |
2360 && ignore_paren_col == 0)) | |
2361 { | |
2362 // If we're looking at a close paren, line up right there; | |
2363 // otherwise, line up with the next (non-white) character. | |
2364 // When b_ind_unclosed_wrapped is set and the matching paren is | |
2365 // the last nonwhite character of the line, use either the | |
2366 // indent of the current line or the indentation of the next | |
2367 // outer paren and add b_ind_unclosed_wrapped (for very long | |
2368 // lines). | |
2369 if (theline[0] != ')') | |
2370 { | |
2371 cur_amount = MAXCOL; | |
2372 l = ml_get(our_paren_pos.lnum); | |
2373 if (curbuf->b_ind_unclosed_wrapped | |
2374 && cin_ends_in(l, (char_u *)"(", NULL)) | |
2375 { | |
2376 // look for opening unmatched paren, indent one level | |
2377 // for each additional level | |
2378 n = 1; | |
2379 for (col = 0; col < our_paren_pos.col; ++col) | |
2380 { | |
2381 switch (l[col]) | |
2382 { | |
2383 case '(': | |
2384 case '{': ++n; | |
2385 break; | |
2386 | |
2387 case ')': | |
2388 case '}': if (n > 1) | |
2389 --n; | |
2390 break; | |
2391 } | |
2392 } | |
2393 | |
2394 our_paren_pos.col = 0; | |
2395 amount += n * curbuf->b_ind_unclosed_wrapped; | |
2396 } | |
2397 else if (curbuf->b_ind_unclosed_whiteok) | |
2398 our_paren_pos.col++; | |
2399 else | |
2400 { | |
2401 col = our_paren_pos.col + 1; | |
2402 while (VIM_ISWHITE(l[col])) | |
2403 col++; | |
2404 if (l[col] != NUL) // In case of trailing space | |
2405 our_paren_pos.col = col; | |
2406 else | |
2407 our_paren_pos.col++; | |
2408 } | |
2409 } | |
2410 | |
2411 // Find how indented the paren is, or the character after it | |
2412 // if we did the above "if". | |
2413 if (our_paren_pos.col > 0) | |
2414 { | |
2415 getvcol(curwin, &our_paren_pos, &col, NULL, NULL); | |
2416 if (cur_amount > (int)col) | |
2417 cur_amount = col; | |
2418 } | |
2419 } | |
2420 | |
2421 if (theline[0] == ')' && curbuf->b_ind_matching_paren) | |
2422 { | |
2423 // Line up with the start of the matching paren line. | |
2424 } | |
2425 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0) | |
2426 || (!curbuf->b_ind_unclosed_noignore | |
2427 && *look == '(' && ignore_paren_col == 0)) | |
2428 { | |
2429 if (cur_amount != MAXCOL) | |
2430 amount = cur_amount; | |
2431 } | |
2432 else | |
2433 { | |
2434 // Add b_ind_unclosed2 for each '(' before our matching one, | |
2435 // but ignore (void) before the line (ignore_paren_col). | |
2436 col = our_paren_pos.col; | |
2437 while ((int)our_paren_pos.col > ignore_paren_col) | |
2438 { | |
2439 --our_paren_pos.col; | |
2440 switch (*ml_get_pos(&our_paren_pos)) | |
2441 { | |
2442 case '(': amount += curbuf->b_ind_unclosed2; | |
2443 col = our_paren_pos.col; | |
2444 break; | |
2445 case ')': amount -= curbuf->b_ind_unclosed2; | |
2446 col = MAXCOL; | |
2447 break; | |
2448 } | |
2449 } | |
2450 | |
2451 // Use b_ind_unclosed once, when the first '(' is not inside | |
2452 // braces | |
2453 if (col == MAXCOL) | |
2454 amount += curbuf->b_ind_unclosed; | |
2455 else | |
2456 { | |
2457 curwin->w_cursor.lnum = our_paren_pos.lnum; | |
2458 curwin->w_cursor.col = col; | |
2459 if (find_match_paren_after_brace(curbuf->b_ind_maxparen) | |
2460 != NULL) | |
2461 amount += curbuf->b_ind_unclosed2; | |
2462 else | |
2463 { | |
2464 if (is_if_for_while) | |
2465 amount += curbuf->b_ind_if_for_while; | |
2466 else | |
2467 amount += curbuf->b_ind_unclosed; | |
2468 } | |
2469 } | |
2470 // For a line starting with ')' use the minimum of the two | |
2471 // positions, to avoid giving it more indent than the previous | |
2472 // lines: | |
2473 // func_long_name( if (x | |
2474 // arg && yy | |
2475 // ) ^ not here ) ^ not here | |
2476 if (cur_amount < amount) | |
2477 amount = cur_amount; | |
2478 } | |
2479 } | |
2480 | |
2481 // add extra indent for a comment | |
2482 if (cin_iscomment(theline)) | |
2483 amount += curbuf->b_ind_comment; | |
2484 } | |
2485 else | |
2486 { | |
2487 // We are inside braces, there is a { before this line at the position | |
2488 // stored in tryposBrace. | |
2489 // Make a copy of tryposBrace, it may point to pos_copy inside | |
2490 // find_start_brace(), which may be changed somewhere. | |
2491 tryposCopy = *tryposBrace; | |
2492 tryposBrace = &tryposCopy; | |
2493 trypos = tryposBrace; | |
2494 ourscope = trypos->lnum; | |
2495 start = ml_get(ourscope); | |
2496 | |
2497 // Now figure out how indented the line is in general. | |
2498 // If the brace was at the start of the line, we use that; | |
2499 // otherwise, check out the indentation of the line as | |
2500 // a whole and then add the "imaginary indent" to that. | |
2501 look = skipwhite(start); | |
2502 if (*look == '{') | |
2503 { | |
2504 getvcol(curwin, trypos, &col, NULL, NULL); | |
2505 amount = col; | |
2506 if (*start == '{') | |
2507 start_brace = BRACE_IN_COL0; | |
2508 else | |
2509 start_brace = BRACE_AT_START; | |
2510 } | |
2511 else | |
2512 { | |
2513 // That opening brace might have been on a continuation | |
2514 // line. if so, find the start of the line. | |
2515 curwin->w_cursor.lnum = ourscope; | |
2516 | |
2517 // Position the cursor over the rightmost paren, so that | |
2518 // matching it will take us back to the start of the line. | |
2519 lnum = ourscope; | |
2520 if (find_last_paren(start, '(', ')') | |
2521 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) | |
2522 != NULL) | |
2523 lnum = trypos->lnum; | |
2524 | |
2525 // It could have been something like | |
2526 // case 1: if (asdf && | |
2527 // ldfd) { | |
2528 // } | |
2529 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label) | |
2530 && cin_iscase(skipwhite(ml_get_curline()), FALSE)) | |
2531 amount = get_indent(); | |
2532 else if (curbuf->b_ind_js) | |
2533 amount = get_indent_lnum(lnum); | |
2534 else | |
2535 amount = skip_label(lnum, &l); | |
2536 | |
2537 start_brace = BRACE_AT_END; | |
2538 } | |
2539 | |
2540 // For Javascript check if the line starts with "key:". | |
2541 if (curbuf->b_ind_js) | |
2542 js_cur_has_key = cin_has_js_key(theline); | |
2543 | |
2544 // If we're looking at a closing brace, that's where | |
2545 // we want to be. otherwise, add the amount of room | |
2546 // that an indent is supposed to be. | |
2547 if (theline[0] == '}') | |
2548 { | |
2549 // they may want closing braces to line up with something | |
2550 // other than the open brace. indulge them, if so. | |
2551 amount += curbuf->b_ind_close_extra; | |
2552 } | |
2553 else | |
2554 { | |
2555 // If we're looking at an "else", try to find an "if" | |
2556 // to match it with. | |
2557 // If we're looking at a "while", try to find a "do" | |
2558 // to match it with. | |
2559 lookfor = LOOKFOR_INITIAL; | |
2560 if (cin_iselse(theline)) | |
2561 lookfor = LOOKFOR_IF; | |
2562 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) // XXX | |
2563 lookfor = LOOKFOR_DO; | |
2564 if (lookfor != LOOKFOR_INITIAL) | |
2565 { | |
2566 curwin->w_cursor.lnum = cur_curpos.lnum; | |
2567 if (find_match(lookfor, ourscope) == OK) | |
2568 { | |
2569 amount = get_indent(); // XXX | |
2570 goto theend; | |
2571 } | |
2572 } | |
2573 | |
2574 // We get here if we are not on an "while-of-do" or "else" (or | |
2575 // failed to find a matching "if"). | |
2576 // Search backwards for something to line up with. | |
2577 // First set amount for when we don't find anything. | |
2578 | |
2579 // if the '{' is _really_ at the left margin, use the imaginary | |
2580 // location of a left-margin brace. Otherwise, correct the | |
2581 // location for b_ind_open_extra. | |
2582 | |
2583 if (start_brace == BRACE_IN_COL0) // '{' is in column 0 | |
2584 { | |
2585 amount = curbuf->b_ind_open_left_imag; | |
2586 lookfor_cpp_namespace = TRUE; | |
2587 } | |
2588 else if (start_brace == BRACE_AT_START && | |
2589 lookfor_cpp_namespace) // '{' is at start | |
2590 { | |
2591 | |
2592 lookfor_cpp_namespace = TRUE; | |
2593 } | |
2594 else | |
2595 { | |
2596 if (start_brace == BRACE_AT_END) // '{' is at end of line | |
2597 { | |
2598 amount += curbuf->b_ind_open_imag; | |
2599 | |
2600 l = skipwhite(ml_get_curline()); | |
2601 if (cin_is_cpp_namespace(l)) | |
2602 amount += curbuf->b_ind_cpp_namespace; | |
2603 else if (cin_is_cpp_extern_c(l)) | |
2604 amount += curbuf->b_ind_cpp_extern_c; | |
2605 } | |
2606 else | |
2607 { | |
2608 // Compensate for adding b_ind_open_extra later. | |
2609 amount -= curbuf->b_ind_open_extra; | |
2610 if (amount < 0) | |
2611 amount = 0; | |
2612 } | |
2613 } | |
2614 | |
2615 lookfor_break = FALSE; | |
2616 | |
2617 if (cin_iscase(theline, FALSE)) // it's a switch() label | |
2618 { | |
2619 lookfor = LOOKFOR_CASE; // find a previous switch() label | |
2620 amount += curbuf->b_ind_case; | |
2621 } | |
2622 else if (cin_isscopedecl(theline)) // private:, ... | |
2623 { | |
2624 lookfor = LOOKFOR_SCOPEDECL; // class decl is this block | |
2625 amount += curbuf->b_ind_scopedecl; | |
2626 } | |
2627 else | |
2628 { | |
2629 if (curbuf->b_ind_case_break && cin_isbreak(theline)) | |
2630 // break; ... | |
2631 lookfor_break = TRUE; | |
2632 | |
2633 lookfor = LOOKFOR_INITIAL; | |
2634 // b_ind_level from start of block | |
2635 amount += curbuf->b_ind_level; | |
2636 } | |
2637 scope_amount = amount; | |
2638 whilelevel = 0; | |
2639 | |
2640 // Search backwards. If we find something we recognize, line up | |
2641 // with that. | |
2642 // | |
2643 // If we're looking at an open brace, indent | |
2644 // the usual amount relative to the conditional | |
2645 // that opens the block. | |
2646 curwin->w_cursor = cur_curpos; | |
2647 for (;;) | |
2648 { | |
2649 curwin->w_cursor.lnum--; | |
2650 curwin->w_cursor.col = 0; | |
2651 | |
2652 // If we went all the way back to the start of our scope, line | |
2653 // up with it. | |
2654 if (curwin->w_cursor.lnum <= ourscope) | |
2655 { | |
2656 // We reached end of scope: | |
2657 // If looking for a enum or structure initialization | |
2658 // go further back: | |
2659 // If it is an initializer (enum xxx or xxx =), then | |
2660 // don't add ind_continuation, otherwise it is a variable | |
2661 // declaration: | |
2662 // int x, | |
2663 // here; <-- add ind_continuation | |
2664 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
2665 { | |
2666 if (curwin->w_cursor.lnum == 0 | |
2667 || curwin->w_cursor.lnum | |
2668 < ourscope - curbuf->b_ind_maxparen) | |
2669 { | |
2670 // nothing found (abuse curbuf->b_ind_maxparen as | |
2671 // limit) assume terminated line (i.e. a variable | |
2672 // initialization) | |
2673 if (cont_amount > 0) | |
2674 amount = cont_amount; | |
2675 else if (!curbuf->b_ind_js) | |
2676 amount += ind_continuation; | |
2677 break; | |
2678 } | |
2679 | |
2680 l = ml_get_curline(); | |
2681 | |
2682 // If we're in a comment or raw string now, skip to | |
2683 // the start of it. | |
2684 trypos = ind_find_start_CORS(NULL); | |
2685 if (trypos != NULL) | |
2686 { | |
2687 curwin->w_cursor.lnum = trypos->lnum + 1; | |
2688 curwin->w_cursor.col = 0; | |
2689 continue; | |
2690 } | |
2691 | |
2692 // Skip preprocessor directives and blank lines. | |
2693 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, | |
2694 &amount)) | |
2695 continue; | |
2696 | |
2697 if (cin_nocode(l)) | |
2698 continue; | |
2699 | |
2700 terminated = cin_isterminated(l, FALSE, TRUE); | |
2701 | |
2702 // If we are at top level and the line looks like a | |
2703 // function declaration, we are done | |
2704 // (it's a variable declaration). | |
2705 if (start_brace != BRACE_IN_COL0 | |
2706 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) | |
2707 { | |
2708 // if the line is terminated with another ',' | |
2709 // it is a continued variable initialization. | |
2710 // don't add extra indent. | |
2711 // TODO: does not work, if a function | |
2712 // declaration is split over multiple lines: | |
2713 // cin_isfuncdecl returns FALSE then. | |
2714 if (terminated == ',') | |
2715 break; | |
2716 | |
2717 // if it es a enum declaration or an assignment, | |
2718 // we are done. | |
2719 if (terminated != ';' && cin_isinit()) | |
2720 break; | |
2721 | |
2722 // nothing useful found | |
2723 if (terminated == 0 || terminated == '{') | |
2724 continue; | |
2725 } | |
2726 | |
2727 if (terminated != ';') | |
2728 { | |
2729 // Skip parens and braces. Position the cursor | |
2730 // over the rightmost paren, so that matching it | |
2731 // will take us back to the start of the line. | |
2732 // XXX | |
2733 trypos = NULL; | |
2734 if (find_last_paren(l, '(', ')')) | |
2735 trypos = find_match_paren( | |
2736 curbuf->b_ind_maxparen); | |
2737 | |
2738 if (trypos == NULL && find_last_paren(l, '{', '}')) | |
2739 trypos = find_start_brace(); | |
2740 | |
2741 if (trypos != NULL) | |
2742 { | |
2743 curwin->w_cursor.lnum = trypos->lnum + 1; | |
2744 curwin->w_cursor.col = 0; | |
2745 continue; | |
2746 } | |
2747 } | |
2748 | |
2749 // it's a variable declaration, add indentation | |
2750 // like in | |
2751 // int a, | |
2752 // b; | |
2753 if (cont_amount > 0) | |
2754 amount = cont_amount; | |
2755 else | |
2756 amount += ind_continuation; | |
2757 } | |
2758 else if (lookfor == LOOKFOR_UNTERM) | |
2759 { | |
2760 if (cont_amount > 0) | |
2761 amount = cont_amount; | |
2762 else | |
2763 amount += ind_continuation; | |
2764 } | |
2765 else | |
2766 { | |
2767 if (lookfor != LOOKFOR_TERM | |
2768 && lookfor != LOOKFOR_CPP_BASECLASS | |
2769 && lookfor != LOOKFOR_COMMA) | |
2770 { | |
2771 amount = scope_amount; | |
2772 if (theline[0] == '{') | |
2773 { | |
2774 amount += curbuf->b_ind_open_extra; | |
2775 added_to_amount = curbuf->b_ind_open_extra; | |
2776 } | |
2777 } | |
2778 | |
2779 if (lookfor_cpp_namespace) | |
2780 { | |
2781 // Looking for C++ namespace, need to look further | |
2782 // back. | |
2783 if (curwin->w_cursor.lnum == ourscope) | |
2784 continue; | |
2785 | |
2786 if (curwin->w_cursor.lnum == 0 | |
2787 || curwin->w_cursor.lnum | |
2788 < ourscope - FIND_NAMESPACE_LIM) | |
2789 break; | |
2790 | |
2791 l = ml_get_curline(); | |
2792 | |
2793 // If we're in a comment or raw string now, skip | |
2794 // to the start of it. | |
2795 trypos = ind_find_start_CORS(NULL); | |
2796 if (trypos != NULL) | |
2797 { | |
2798 curwin->w_cursor.lnum = trypos->lnum + 1; | |
2799 curwin->w_cursor.col = 0; | |
2800 continue; | |
2801 } | |
2802 | |
2803 // Skip preprocessor directives and blank lines. | |
2804 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, | |
2805 &amount)) | |
2806 continue; | |
2807 | |
2808 // Finally the actual check for "namespace". | |
2809 if (cin_is_cpp_namespace(l)) | |
2810 { | |
2811 amount += curbuf->b_ind_cpp_namespace | |
2812 - added_to_amount; | |
2813 break; | |
2814 } | |
2815 else if (cin_is_cpp_extern_c(l)) | |
2816 { | |
2817 amount += curbuf->b_ind_cpp_extern_c | |
2818 - added_to_amount; | |
2819 break; | |
2820 } | |
2821 | |
2822 if (cin_nocode(l)) | |
2823 continue; | |
2824 } | |
2825 } | |
2826 break; | |
2827 } | |
2828 | |
2829 // If we're in a comment or raw string now, skip to the start | |
2830 // of it. XXX | |
2831 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL) | |
2832 { | |
2833 curwin->w_cursor.lnum = trypos->lnum + 1; | |
2834 curwin->w_cursor.col = 0; | |
2835 continue; | |
2836 } | |
2837 | |
2838 l = ml_get_curline(); | |
2839 | |
2840 // If this is a switch() label, may line up relative to that. | |
2841 // If this is a C++ scope declaration, do the same. | |
2842 iscase = cin_iscase(l, FALSE); | |
2843 if (iscase || cin_isscopedecl(l)) | |
2844 { | |
2845 // we are only looking for cpp base class | |
2846 // declaration/initialization any longer | |
2847 if (lookfor == LOOKFOR_CPP_BASECLASS) | |
2848 break; | |
2849 | |
2850 // When looking for a "do" we are not interested in | |
2851 // labels. | |
2852 if (whilelevel > 0) | |
2853 continue; | |
2854 | |
2855 // case xx: | |
2856 // c = 99 + <- this indent plus continuation | |
2857 //-> here; | |
2858 if (lookfor == LOOKFOR_UNTERM | |
2859 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
2860 { | |
2861 if (cont_amount > 0) | |
2862 amount = cont_amount; | |
2863 else | |
2864 amount += ind_continuation; | |
2865 break; | |
2866 } | |
2867 | |
2868 // case xx: <- line up with this case | |
2869 // x = 333; | |
2870 // case yy: | |
2871 if ( (iscase && lookfor == LOOKFOR_CASE) | |
2872 || (iscase && lookfor_break) | |
2873 || (!iscase && lookfor == LOOKFOR_SCOPEDECL)) | |
2874 { | |
2875 // Check that this case label is not for another | |
2876 // switch() XXX | |
2877 if ((trypos = find_start_brace()) == NULL | |
2878 || trypos->lnum == ourscope) | |
2879 { | |
2880 amount = get_indent(); // XXX | |
2881 break; | |
2882 } | |
2883 continue; | |
2884 } | |
2885 | |
2886 n = get_indent_nolabel(curwin->w_cursor.lnum); // XXX | |
2887 | |
2888 // case xx: if (cond) <- line up with this if | |
2889 // y = y + 1; | |
2890 // -> s = 99; | |
2891 // | |
2892 // case xx: | |
2893 // if (cond) <- line up with this line | |
2894 // y = y + 1; | |
2895 // -> s = 99; | |
2896 if (lookfor == LOOKFOR_TERM) | |
2897 { | |
2898 if (n) | |
2899 amount = n; | |
2900 | |
2901 if (!lookfor_break) | |
2902 break; | |
2903 } | |
2904 | |
2905 // case xx: x = x + 1; <- line up with this x | |
2906 // -> y = y + 1; | |
2907 // | |
2908 // case xx: if (cond) <- line up with this if | |
2909 // -> y = y + 1; | |
2910 if (n) | |
2911 { | |
2912 amount = n; | |
2913 l = after_label(ml_get_curline()); | |
2914 if (l != NULL && cin_is_cinword(l)) | |
2915 { | |
2916 if (theline[0] == '{') | |
2917 amount += curbuf->b_ind_open_extra; | |
2918 else | |
2919 amount += curbuf->b_ind_level | |
2920 + curbuf->b_ind_no_brace; | |
2921 } | |
2922 break; | |
2923 } | |
2924 | |
2925 // Try to get the indent of a statement before the switch | |
2926 // label. If nothing is found, line up relative to the | |
2927 // switch label. | |
2928 // break; <- may line up with this line | |
2929 // case xx: | |
2930 // -> y = 1; | |
2931 scope_amount = get_indent() + (iscase // XXX | |
2932 ? curbuf->b_ind_case_code | |
2933 : curbuf->b_ind_scopedecl_code); | |
2934 lookfor = curbuf->b_ind_case_break | |
2935 ? LOOKFOR_NOBREAK : LOOKFOR_ANY; | |
2936 continue; | |
2937 } | |
2938 | |
2939 // Looking for a switch() label or C++ scope declaration, | |
2940 // ignore other lines, skip {}-blocks. | |
2941 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL) | |
2942 { | |
2943 if (find_last_paren(l, '{', '}') | |
2944 && (trypos = find_start_brace()) != NULL) | |
2945 { | |
2946 curwin->w_cursor.lnum = trypos->lnum + 1; | |
2947 curwin->w_cursor.col = 0; | |
2948 } | |
2949 continue; | |
2950 } | |
2951 | |
2952 // Ignore jump labels with nothing after them. | |
2953 if (!curbuf->b_ind_js && cin_islabel()) | |
2954 { | |
2955 l = after_label(ml_get_curline()); | |
2956 if (l == NULL || cin_nocode(l)) | |
2957 continue; | |
2958 } | |
2959 | |
2960 // Ignore #defines, #if, etc. | |
2961 // Ignore comment and empty lines. | |
2962 // (need to get the line again, cin_islabel() may have | |
2963 // unlocked it) | |
2964 l = ml_get_curline(); | |
2965 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount) | |
2966 || cin_nocode(l)) | |
2967 continue; | |
2968 | |
2969 // Are we at the start of a cpp base class declaration or | |
2970 // constructor initialization? XXX | |
2971 n = FALSE; | |
2972 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0) | |
2973 { | |
2974 n = cin_is_cpp_baseclass(&cache_cpp_baseclass); | |
2975 l = ml_get_curline(); | |
2976 } | |
2977 if (n) | |
2978 { | |
2979 if (lookfor == LOOKFOR_UNTERM) | |
2980 { | |
2981 if (cont_amount > 0) | |
2982 amount = cont_amount; | |
2983 else | |
2984 amount += ind_continuation; | |
2985 } | |
2986 else if (theline[0] == '{') | |
2987 { | |
2988 // Need to find start of the declaration. | |
2989 lookfor = LOOKFOR_UNTERM; | |
2990 ind_continuation = 0; | |
2991 continue; | |
2992 } | |
2993 else | |
2994 // XXX | |
2995 amount = get_baseclass_amount( | |
2996 cache_cpp_baseclass.lpos.col); | |
2997 break; | |
2998 } | |
2999 else if (lookfor == LOOKFOR_CPP_BASECLASS) | |
3000 { | |
3001 // only look, whether there is a cpp base class | |
3002 // declaration or initialization before the opening brace. | |
3003 if (cin_isterminated(l, TRUE, FALSE)) | |
3004 break; | |
3005 else | |
3006 continue; | |
3007 } | |
3008 | |
3009 // What happens next depends on the line being terminated. | |
3010 // If terminated with a ',' only consider it terminating if | |
3011 // there is another unterminated statement behind, eg: | |
3012 // 123, | |
3013 // sizeof | |
3014 // here | |
3015 // Otherwise check whether it is a enumeration or structure | |
3016 // initialisation (not indented) or a variable declaration | |
3017 // (indented). | |
3018 terminated = cin_isterminated(l, FALSE, TRUE); | |
3019 | |
3020 if (js_cur_has_key) | |
3021 { | |
3022 js_cur_has_key = 0; // only check the first line | |
3023 if (curbuf->b_ind_js && terminated == ',') | |
3024 { | |
3025 // For Javascript we might be inside an object: | |
3026 // key: something, <- align with this | |
3027 // key: something | |
3028 // or: | |
3029 // key: something + <- align with this | |
3030 // something, | |
3031 // key: something | |
3032 lookfor = LOOKFOR_JS_KEY; | |
3033 } | |
3034 } | |
3035 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l)) | |
3036 { | |
3037 amount = get_indent(); | |
3038 break; | |
3039 } | |
3040 if (lookfor == LOOKFOR_COMMA) | |
3041 { | |
3042 if (tryposBrace != NULL && tryposBrace->lnum | |
3043 >= curwin->w_cursor.lnum) | |
3044 break; | |
3045 if (terminated == ',') | |
3046 // line below current line is the one that starts a | |
3047 // (possibly broken) line ending in a comma. | |
3048 break; | |
3049 else | |
3050 { | |
3051 amount = get_indent(); | |
3052 if (curwin->w_cursor.lnum - 1 == ourscope) | |
3053 // line above is start of the scope, thus current | |
3054 // line is the one that stars a (possibly broken) | |
3055 // line ending in a comma. | |
3056 break; | |
3057 } | |
3058 } | |
3059 | |
3060 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM | |
3061 && terminated == ',')) | |
3062 { | |
3063 if (lookfor != LOOKFOR_ENUM_OR_INIT && | |
3064 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '[')) | |
3065 amount += ind_continuation; | |
3066 // if we're in the middle of a paren thing, | |
3067 // go back to the line that starts it so | |
3068 // we can get the right prevailing indent | |
3069 // if ( foo && | |
3070 // bar ) | |
3071 | |
3072 // Position the cursor over the rightmost paren, so that | |
3073 // matching it will take us back to the start of the line. | |
3074 // Ignore a match before the start of the block. | |
3075 (void)find_last_paren(l, '(', ')'); | |
3076 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos)); | |
3077 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum | |
3078 || (trypos->lnum == tryposBrace->lnum | |
3079 && trypos->col < tryposBrace->col))) | |
3080 trypos = NULL; | |
3081 | |
3082 // If we are looking for ',', we also look for matching | |
3083 // braces. | |
3084 if (trypos == NULL && terminated == ',' | |
3085 && find_last_paren(l, '{', '}')) | |
3086 trypos = find_start_brace(); | |
3087 | |
3088 if (trypos != NULL) | |
3089 { | |
3090 // Check if we are on a case label now. This is | |
3091 // handled above. | |
3092 // case xx: if ( asdf && | |
3093 // asdf) | |
3094 curwin->w_cursor = *trypos; | |
3095 l = ml_get_curline(); | |
3096 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) | |
3097 { | |
3098 ++curwin->w_cursor.lnum; | |
3099 curwin->w_cursor.col = 0; | |
3100 continue; | |
3101 } | |
3102 } | |
3103 | |
3104 /* | |
3105 * Skip over continuation lines to find the one to get the | |
3106 * indent from | |
3107 * char *usethis = "bla\ | |
3108 * bla", | |
3109 * here; | |
3110 */ | |
3111 if (terminated == ',') | |
3112 { | |
3113 while (curwin->w_cursor.lnum > 1) | |
3114 { | |
3115 l = ml_get(curwin->w_cursor.lnum - 1); | |
3116 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
3117 break; | |
3118 --curwin->w_cursor.lnum; | |
3119 curwin->w_cursor.col = 0; | |
3120 } | |
3121 } | |
3122 | |
3123 // Get indent and pointer to text for current line, | |
3124 // ignoring any jump label. XXX | |
3125 if (curbuf->b_ind_js) | |
3126 cur_amount = get_indent(); | |
3127 else | |
3128 cur_amount = skip_label(curwin->w_cursor.lnum, &l); | |
3129 // If this is just above the line we are indenting, and it | |
3130 // starts with a '{', line it up with this line. | |
3131 // while (not) | |
3132 // -> { | |
3133 // } | |
3134 if (terminated != ',' && lookfor != LOOKFOR_TERM | |
3135 && theline[0] == '{') | |
3136 { | |
3137 amount = cur_amount; | |
3138 // Only add b_ind_open_extra when the current line | |
3139 // doesn't start with a '{', which must have a match | |
3140 // in the same line (scope is the same). Probably: | |
3141 // { 1, 2 }, | |
3142 // -> { 3, 4 } | |
3143 if (*skipwhite(l) != '{') | |
3144 amount += curbuf->b_ind_open_extra; | |
3145 | |
3146 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js) | |
3147 { | |
3148 // have to look back, whether it is a cpp base | |
3149 // class declaration or initialization | |
3150 lookfor = LOOKFOR_CPP_BASECLASS; | |
3151 continue; | |
3152 } | |
3153 break; | |
3154 } | |
3155 | |
3156 // Check if we are after an "if", "while", etc. | |
3157 // Also allow " } else". | |
3158 if (cin_is_cinword(l) || cin_iselse(skipwhite(l))) | |
3159 { | |
3160 // Found an unterminated line after an if (), line up | |
3161 // with the last one. | |
3162 // if (cond) | |
3163 // 100 + | |
3164 // -> here; | |
3165 if (lookfor == LOOKFOR_UNTERM | |
3166 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
3167 { | |
3168 if (cont_amount > 0) | |
3169 amount = cont_amount; | |
3170 else | |
3171 amount += ind_continuation; | |
3172 break; | |
3173 } | |
3174 | |
3175 // If this is just above the line we are indenting, we | |
3176 // are finished. | |
3177 // while (not) | |
3178 // -> here; | |
3179 // Otherwise this indent can be used when the line | |
3180 // before this is terminated. | |
3181 // yyy; | |
3182 // if (stat) | |
3183 // while (not) | |
3184 // xxx; | |
3185 // -> here; | |
3186 amount = cur_amount; | |
3187 if (theline[0] == '{') | |
3188 amount += curbuf->b_ind_open_extra; | |
3189 if (lookfor != LOOKFOR_TERM) | |
3190 { | |
3191 amount += curbuf->b_ind_level | |
3192 + curbuf->b_ind_no_brace; | |
3193 break; | |
3194 } | |
3195 | |
3196 // Special trick: when expecting the while () after a | |
3197 // do, line up with the while() | |
3198 // do | |
3199 // x = 1; | |
3200 // -> here | |
3201 l = skipwhite(ml_get_curline()); | |
3202 if (cin_isdo(l)) | |
3203 { | |
3204 if (whilelevel == 0) | |
3205 break; | |
3206 --whilelevel; | |
3207 } | |
3208 | |
3209 // When searching for a terminated line, don't use the | |
3210 // one between the "if" and the matching "else". | |
3211 // Need to use the scope of this "else". XXX | |
3212 // If whilelevel != 0 continue looking for a "do {". | |
3213 if (cin_iselse(l) && whilelevel == 0) | |
3214 { | |
3215 // If we're looking at "} else", let's make sure we | |
3216 // find the opening brace of the enclosing scope, | |
3217 // not the one from "if () {". | |
3218 if (*l == '}') | |
3219 curwin->w_cursor.col = | |
3220 (colnr_T)(l - ml_get_curline()) + 1; | |
3221 | |
3222 if ((trypos = find_start_brace()) == NULL | |
3223 || find_match(LOOKFOR_IF, trypos->lnum) | |
3224 == FAIL) | |
3225 break; | |
3226 } | |
3227 } | |
3228 | |
3229 // If we're below an unterminated line that is not an | |
3230 // "if" or something, we may line up with this line or | |
3231 // add something for a continuation line, depending on | |
3232 // the line before this one. | |
3233 else | |
3234 { | |
3235 // Found two unterminated lines on a row, line up with | |
3236 // the last one. | |
3237 // c = 99 + | |
3238 // 100 + | |
3239 // -> here; | |
3240 if (lookfor == LOOKFOR_UNTERM) | |
3241 { | |
3242 // When line ends in a comma add extra indent | |
3243 if (terminated == ',') | |
3244 amount += ind_continuation; | |
3245 break; | |
3246 } | |
3247 | |
3248 if (lookfor == LOOKFOR_ENUM_OR_INIT) | |
3249 { | |
3250 // Found two lines ending in ',', lineup with the | |
3251 // lowest one, but check for cpp base class | |
3252 // declaration/initialization, if it is an | |
3253 // opening brace or we are looking just for | |
3254 // enumerations/initializations. | |
3255 if (terminated == ',') | |
3256 { | |
3257 if (curbuf->b_ind_cpp_baseclass == 0) | |
3258 break; | |
3259 | |
3260 lookfor = LOOKFOR_CPP_BASECLASS; | |
3261 continue; | |
3262 } | |
3263 | |
3264 // Ignore unterminated lines in between, but | |
3265 // reduce indent. | |
3266 if (amount > cur_amount) | |
3267 amount = cur_amount; | |
3268 } | |
3269 else | |
3270 { | |
3271 // Found first unterminated line on a row, may | |
3272 // line up with this line, remember its indent | |
3273 // 100 + | |
3274 // -> here; | |
3275 l = ml_get_curline(); | |
3276 amount = cur_amount; | |
3277 | |
3278 n = (int)STRLEN(l); | |
3279 if (terminated == ',' && (*skipwhite(l) == ']' | |
3280 || (n >=2 && l[n - 2] == ']'))) | |
3281 break; | |
3282 | |
3283 // If previous line ends in ',', check whether we | |
3284 // are in an initialization or enum | |
3285 // struct xxx = | |
3286 // { | |
3287 // sizeof a, | |
3288 // 124 }; | |
3289 // or a normal possible continuation line. | |
3290 // but only, of no other statement has been found | |
3291 // yet. | |
3292 if (lookfor == LOOKFOR_INITIAL && terminated == ',') | |
3293 { | |
3294 if (curbuf->b_ind_js) | |
3295 { | |
3296 // Search for a line ending in a comma | |
3297 // and line up with the line below it | |
3298 // (could be the current line). | |
3299 // some = [ | |
3300 // 1, <- line up here | |
3301 // 2, | |
3302 // some = [ | |
3303 // 3 + <- line up here | |
3304 // 4 * | |
3305 // 5, | |
3306 // 6, | |
3307 if (cin_iscomment(skipwhite(l))) | |
3308 break; | |
3309 lookfor = LOOKFOR_COMMA; | |
3310 trypos = find_match_char('[', | |
3311 curbuf->b_ind_maxparen); | |
3312 if (trypos != NULL) | |
3313 { | |
3314 if (trypos->lnum | |
3315 == curwin->w_cursor.lnum - 1) | |
3316 { | |
3317 // Current line is first inside | |
3318 // [], line up with it. | |
3319 break; | |
3320 } | |
3321 ourscope = trypos->lnum; | |
3322 } | |
3323 } | |
3324 else | |
3325 { | |
3326 lookfor = LOOKFOR_ENUM_OR_INIT; | |
3327 cont_amount = cin_first_id_amount(); | |
3328 } | |
3329 } | |
3330 else | |
3331 { | |
3332 if (lookfor == LOOKFOR_INITIAL | |
3333 && *l != NUL | |
3334 && l[STRLEN(l) - 1] == '\\') | |
3335 // XXX | |
3336 cont_amount = cin_get_equal_amount( | |
3337 curwin->w_cursor.lnum); | |
3338 if (lookfor != LOOKFOR_TERM | |
3339 && lookfor != LOOKFOR_JS_KEY | |
3340 && lookfor != LOOKFOR_COMMA | |
3341 && raw_string_start != curwin->w_cursor.lnum) | |
3342 lookfor = LOOKFOR_UNTERM; | |
3343 } | |
3344 } | |
3345 } | |
3346 } | |
3347 | |
3348 // Check if we are after a while (cond); | |
3349 // If so: Ignore until the matching "do". | |
3350 else if (cin_iswhileofdo_end(terminated)) // XXX | |
3351 { | |
3352 // Found an unterminated line after a while ();, line up | |
3353 // with the last one. | |
3354 // while (cond); | |
3355 // 100 + <- line up with this one | |
3356 // -> here; | |
3357 if (lookfor == LOOKFOR_UNTERM | |
3358 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
3359 { | |
3360 if (cont_amount > 0) | |
3361 amount = cont_amount; | |
3362 else | |
3363 amount += ind_continuation; | |
3364 break; | |
3365 } | |
3366 | |
3367 if (whilelevel == 0) | |
3368 { | |
3369 lookfor = LOOKFOR_TERM; | |
3370 amount = get_indent(); // XXX | |
3371 if (theline[0] == '{') | |
3372 amount += curbuf->b_ind_open_extra; | |
3373 } | |
3374 ++whilelevel; | |
3375 } | |
3376 | |
3377 // We are after a "normal" statement. | |
3378 // If we had another statement we can stop now and use the | |
3379 // indent of that other statement. | |
3380 // Otherwise the indent of the current statement may be used, | |
3381 // search backwards for the next "normal" statement. | |
3382 else | |
3383 { | |
3384 // Skip single break line, if before a switch label. It | |
3385 // may be lined up with the case label. | |
3386 if (lookfor == LOOKFOR_NOBREAK | |
3387 && cin_isbreak(skipwhite(ml_get_curline()))) | |
3388 { | |
3389 lookfor = LOOKFOR_ANY; | |
3390 continue; | |
3391 } | |
3392 | |
3393 // Handle "do {" line. | |
3394 if (whilelevel > 0) | |
3395 { | |
3396 l = cin_skipcomment(ml_get_curline()); | |
3397 if (cin_isdo(l)) | |
3398 { | |
3399 amount = get_indent(); // XXX | |
3400 --whilelevel; | |
3401 continue; | |
3402 } | |
3403 } | |
3404 | |
3405 // Found a terminated line above an unterminated line. Add | |
3406 // the amount for a continuation line. | |
3407 // x = 1; | |
3408 // y = foo + | |
3409 // -> here; | |
3410 // or | |
3411 // int x = 1; | |
3412 // int foo, | |
3413 // -> here; | |
3414 if (lookfor == LOOKFOR_UNTERM | |
3415 || lookfor == LOOKFOR_ENUM_OR_INIT) | |
3416 { | |
3417 if (cont_amount > 0) | |
3418 amount = cont_amount; | |
3419 else | |
3420 amount += ind_continuation; | |
3421 break; | |
3422 } | |
3423 | |
3424 // Found a terminated line above a terminated line or "if" | |
3425 // etc. line. Use the amount of the line below us. | |
3426 // x = 1; x = 1; | |
3427 // if (asdf) y = 2; | |
3428 // while (asdf) ->here; | |
3429 // here; | |
3430 // ->foo; | |
3431 if (lookfor == LOOKFOR_TERM) | |
3432 { | |
3433 if (!lookfor_break && whilelevel == 0) | |
3434 break; | |
3435 } | |
3436 | |
3437 // First line above the one we're indenting is terminated. | |
3438 // To know what needs to be done look further backward for | |
3439 // a terminated line. | |
3440 else | |
3441 { | |
3442 // position the cursor over the rightmost paren, so | |
3443 // that matching it will take us back to the start of | |
3444 // the line. Helps for: | |
3445 // func(asdr, | |
3446 // asdfasdf); | |
3447 // here; | |
3448 term_again: | |
3449 l = ml_get_curline(); | |
3450 if (find_last_paren(l, '(', ')') | |
3451 && (trypos = find_match_paren( | |
3452 curbuf->b_ind_maxparen)) != NULL) | |
3453 { | |
3454 // Check if we are on a case label now. This is | |
3455 // handled above. | |
3456 // case xx: if ( asdf && | |
3457 // asdf) | |
3458 curwin->w_cursor = *trypos; | |
3459 l = ml_get_curline(); | |
3460 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)) | |
3461 { | |
3462 ++curwin->w_cursor.lnum; | |
3463 curwin->w_cursor.col = 0; | |
3464 continue; | |
3465 } | |
3466 } | |
3467 | |
3468 // When aligning with the case statement, don't align | |
3469 // with a statement after it. | |
3470 // case 1: { <-- don't use this { position | |
3471 // stat; | |
3472 // } | |
3473 // case 2: | |
3474 // stat; | |
3475 // } | |
3476 iscase = (curbuf->b_ind_keep_case_label | |
3477 && cin_iscase(l, FALSE)); | |
3478 | |
3479 // Get indent and pointer to text for current line, | |
3480 // ignoring any jump label. | |
3481 amount = skip_label(curwin->w_cursor.lnum, &l); | |
3482 | |
3483 if (theline[0] == '{') | |
3484 amount += curbuf->b_ind_open_extra; | |
3485 // See remark above: "Only add b_ind_open_extra.." | |
3486 l = skipwhite(l); | |
3487 if (*l == '{') | |
3488 amount -= curbuf->b_ind_open_extra; | |
3489 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM; | |
3490 | |
3491 // When a terminated line starts with "else" skip to | |
3492 // the matching "if": | |
3493 // else 3; | |
3494 // indent this; | |
3495 // Need to use the scope of this "else". XXX | |
3496 // If whilelevel != 0 continue looking for a "do {". | |
3497 if (lookfor == LOOKFOR_TERM | |
3498 && *l != '}' | |
3499 && cin_iselse(l) | |
3500 && whilelevel == 0) | |
3501 { | |
3502 if ((trypos = find_start_brace()) == NULL | |
3503 || find_match(LOOKFOR_IF, trypos->lnum) | |
3504 == FAIL) | |
3505 break; | |
3506 continue; | |
3507 } | |
3508 | |
3509 // If we're at the end of a block, skip to the start of | |
3510 // that block. | |
3511 l = ml_get_curline(); | |
3512 if (find_last_paren(l, '{', '}') // XXX | |
3513 && (trypos = find_start_brace()) != NULL) | |
3514 { | |
3515 curwin->w_cursor = *trypos; | |
3516 // if not "else {" check for terminated again | |
3517 // but skip block for "} else {" | |
3518 l = cin_skipcomment(ml_get_curline()); | |
3519 if (*l == '}' || !cin_iselse(l)) | |
3520 goto term_again; | |
3521 ++curwin->w_cursor.lnum; | |
3522 curwin->w_cursor.col = 0; | |
3523 } | |
3524 } | |
3525 } | |
3526 } | |
3527 } | |
3528 } | |
3529 | |
3530 // add extra indent for a comment | |
3531 if (cin_iscomment(theline)) | |
3532 amount += curbuf->b_ind_comment; | |
3533 | |
3534 // subtract extra left-shift for jump labels | |
3535 if (curbuf->b_ind_jump_label > 0 && original_line_islabel) | |
3536 amount -= curbuf->b_ind_jump_label; | |
3537 | |
3538 goto theend; | |
3539 } | |
3540 | |
3541 // ok -- we're not inside any sort of structure at all! | |
3542 // | |
3543 // This means we're at the top level, and everything should | |
3544 // basically just match where the previous line is, except | |
3545 // for the lines immediately following a function declaration, | |
3546 // which are K&R-style parameters and need to be indented. | |
3547 // | |
3548 // if our line starts with an open brace, forget about any | |
3549 // prevailing indent and make sure it looks like the start | |
3550 // of a function | |
3551 | |
3552 if (theline[0] == '{') | |
3553 { | |
3554 amount = curbuf->b_ind_first_open; | |
3555 goto theend; | |
3556 } | |
3557 | |
3558 // If the NEXT line is a function declaration, the current | |
3559 // line needs to be indented as a function type spec. | |
3560 // Don't do this if the current line looks like a comment or if the | |
3561 // current line is terminated, ie. ends in ';', or if the current line | |
3562 // contains { or }: "void f() {\n if (1)" | |
3563 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count | |
3564 && !cin_nocode(theline) | |
3565 && vim_strchr(theline, '{') == NULL | |
3566 && vim_strchr(theline, '}') == NULL | |
3567 && !cin_ends_in(theline, (char_u *)":", NULL) | |
3568 && !cin_ends_in(theline, (char_u *)",", NULL) | |
3569 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1, | |
3570 cur_curpos.lnum + 1) | |
3571 && !cin_isterminated(theline, FALSE, TRUE)) | |
3572 { | |
3573 amount = curbuf->b_ind_func_type; | |
3574 goto theend; | |
3575 } | |
3576 | |
3577 // search backwards until we find something we recognize | |
3578 amount = 0; | |
3579 curwin->w_cursor = cur_curpos; | |
3580 while (curwin->w_cursor.lnum > 1) | |
3581 { | |
3582 curwin->w_cursor.lnum--; | |
3583 curwin->w_cursor.col = 0; | |
3584 | |
3585 l = ml_get_curline(); | |
3586 | |
3587 // If we're in a comment or raw string now, skip to the start | |
3588 // of it. XXX | |
3589 if ((trypos = ind_find_start_CORS(NULL)) != NULL) | |
3590 { | |
3591 curwin->w_cursor.lnum = trypos->lnum + 1; | |
3592 curwin->w_cursor.col = 0; | |
3593 continue; | |
3594 } | |
3595 | |
3596 // Are we at the start of a cpp base class declaration or | |
3597 // constructor initialization? XXX | |
3598 n = FALSE; | |
3599 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{') | |
3600 { | |
3601 n = cin_is_cpp_baseclass(&cache_cpp_baseclass); | |
3602 l = ml_get_curline(); | |
3603 } | |
3604 if (n) | |
3605 { | |
3606 // XXX | |
3607 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col); | |
3608 break; | |
3609 } | |
3610 | |
3611 // Skip preprocessor directives and blank lines. | |
3612 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)) | |
3613 continue; | |
3614 | |
3615 if (cin_nocode(l)) | |
3616 continue; | |
3617 | |
3618 // If the previous line ends in ',', use one level of | |
3619 // indentation: | |
3620 // int foo, | |
3621 // bar; | |
3622 // do this before checking for '}' in case of eg. | |
3623 // enum foobar | |
3624 // { | |
3625 // ... | |
3626 // } foo, | |
3627 // bar; | |
3628 n = 0; | |
3629 if (cin_ends_in(l, (char_u *)",", NULL) | |
3630 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\')) | |
3631 { | |
3632 // take us back to opening paren | |
3633 if (find_last_paren(l, '(', ')') | |
3634 && (trypos = find_match_paren( | |
3635 curbuf->b_ind_maxparen)) != NULL) | |
3636 curwin->w_cursor = *trypos; | |
3637 | |
3638 /* | |
3639 * For a line ending in ',' that is a continuation line go | |
3640 * back to the first line with a backslash: | |
3641 * char *foo = "bla\ | |
3642 * bla", | |
3643 * here; | |
3644 */ | |
3645 while (n == 0 && curwin->w_cursor.lnum > 1) | |
3646 { | |
3647 l = ml_get(curwin->w_cursor.lnum - 1); | |
3648 if (*l == NUL || l[STRLEN(l) - 1] != '\\') | |
3649 break; | |
3650 --curwin->w_cursor.lnum; | |
3651 curwin->w_cursor.col = 0; | |
3652 } | |
3653 | |
3654 amount = get_indent(); // XXX | |
3655 | |
3656 if (amount == 0) | |
3657 amount = cin_first_id_amount(); | |
3658 if (amount == 0) | |
3659 amount = ind_continuation; | |
3660 break; | |
3661 } | |
3662 | |
3663 // If the line looks like a function declaration, and we're | |
3664 // not in a comment, put it the left margin. | |
3665 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) // XXX | |
3666 break; | |
3667 l = ml_get_curline(); | |
3668 | |
3669 // Finding the closing '}' of a previous function. Put | |
3670 // current line at the left margin. For when 'cino' has "fs". | |
3671 if (*skipwhite(l) == '}') | |
3672 break; | |
3673 | |
3674 // (matching {) | |
3675 // If the previous line ends on '};' (maybe followed by | |
3676 // comments) align at column 0. For example: | |
3677 // char *string_array[] = { "foo", | |
3678 // / * x * / "b};ar" }; / * foobar * / | |
3679 if (cin_ends_in(l, (char_u *)"};", NULL)) | |
3680 break; | |
3681 | |
3682 // If the previous line ends on '[' we are probably in an | |
3683 // array constant: | |
3684 // something = [ | |
3685 // 234, <- extra indent | |
3686 if (cin_ends_in(l, (char_u *)"[", NULL)) | |
3687 { | |
3688 amount = get_indent() + ind_continuation; | |
3689 break; | |
3690 } | |
3691 | |
3692 // Find a line only has a semicolon that belongs to a previous | |
3693 // line ending in '}', e.g. before an #endif. Don't increase | |
3694 // indent then. | |
3695 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1)) | |
3696 { | |
3697 pos_T curpos_save = curwin->w_cursor; | |
3698 | |
3699 while (curwin->w_cursor.lnum > 1) | |
3700 { | |
3701 look = ml_get(--curwin->w_cursor.lnum); | |
3702 if (!(cin_nocode(look) || cin_ispreproc_cont( | |
3703 &look, &curwin->w_cursor.lnum, &amount))) | |
3704 break; | |
3705 } | |
3706 if (curwin->w_cursor.lnum > 0 | |
3707 && cin_ends_in(look, (char_u *)"}", NULL)) | |
3708 break; | |
3709 | |
3710 curwin->w_cursor = curpos_save; | |
3711 } | |
3712 | |
3713 // If the PREVIOUS line is a function declaration, the current | |
3714 // line (and the ones that follow) needs to be indented as | |
3715 // parameters. | |
3716 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0)) | |
3717 { | |
3718 amount = curbuf->b_ind_param; | |
3719 break; | |
3720 } | |
3721 | |
3722 // If the previous line ends in ';' and the line before the | |
3723 // previous line ends in ',' or '\', ident to column zero: | |
3724 // int foo, | |
3725 // bar; | |
3726 // indent_to_0 here; | |
3727 if (cin_ends_in(l, (char_u *)";", NULL)) | |
3728 { | |
3729 l = ml_get(curwin->w_cursor.lnum - 1); | |
3730 if (cin_ends_in(l, (char_u *)",", NULL) | |
3731 || (*l != NUL && l[STRLEN(l) - 1] == '\\')) | |
3732 break; | |
3733 l = ml_get_curline(); | |
3734 } | |
3735 | |
3736 // Doesn't look like anything interesting -- so just | |
3737 // use the indent of this line. | |
3738 // | |
3739 // Position the cursor over the rightmost paren, so that | |
3740 // matching it will take us back to the start of the line. | |
3741 find_last_paren(l, '(', ')'); | |
3742 | |
3743 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL) | |
3744 curwin->w_cursor = *trypos; | |
3745 amount = get_indent(); // XXX | |
3746 break; | |
3747 } | |
3748 | |
3749 // add extra indent for a comment | |
3750 if (cin_iscomment(theline)) | |
3751 amount += curbuf->b_ind_comment; | |
3752 | |
3753 /* | |
3754 * add extra indent if the previous line ended in a backslash: | |
3755 * "asdfasdf\ | |
3756 * here"; | |
3757 * char *foo = "asdf\ | |
3758 * here"; | |
3759 */ | |
3760 if (cur_curpos.lnum > 1) | |
3761 { | |
3762 l = ml_get(cur_curpos.lnum - 1); | |
3763 if (*l != NUL && l[STRLEN(l) - 1] == '\\') | |
3764 { | |
3765 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1); | |
3766 if (cur_amount > 0) | |
3767 amount = cur_amount; | |
3768 else if (cur_amount == 0) | |
3769 amount += ind_continuation; | |
3770 } | |
3771 } | |
3772 | |
3773 theend: | |
3774 if (amount < 0) | |
3775 amount = 0; | |
3776 | |
3777 laterend: | |
3778 // put the cursor back where it belongs | |
3779 curwin->w_cursor = cur_curpos; | |
3780 | |
3781 vim_free(linecopy); | |
3782 | |
3783 return amount; | |
3784 } | |
3785 | |
3786 static int | |
3787 find_match(int lookfor, linenr_T ourscope) | |
3788 { | |
3789 char_u *look; | |
3790 pos_T *theirscope; | |
3791 char_u *mightbeif; | |
3792 int elselevel; | |
3793 int whilelevel; | |
3794 | |
3795 if (lookfor == LOOKFOR_IF) | |
3796 { | |
3797 elselevel = 1; | |
3798 whilelevel = 0; | |
3799 } | |
3800 else | |
3801 { | |
3802 elselevel = 0; | |
3803 whilelevel = 1; | |
3804 } | |
3805 | |
3806 curwin->w_cursor.col = 0; | |
3807 | |
3808 while (curwin->w_cursor.lnum > ourscope + 1) | |
3809 { | |
3810 curwin->w_cursor.lnum--; | |
3811 curwin->w_cursor.col = 0; | |
3812 | |
3813 look = cin_skipcomment(ml_get_curline()); | |
3814 if (cin_iselse(look) | |
3815 || cin_isif(look) | |
3816 || cin_isdo(look) // XXX | |
3817 || cin_iswhileofdo(look, curwin->w_cursor.lnum)) | |
3818 { | |
3819 // if we've gone outside the braces entirely, | |
3820 // we must be out of scope... | |
3821 theirscope = find_start_brace(); // XXX | |
3822 if (theirscope == NULL) | |
3823 break; | |
3824 | |
3825 // and if the brace enclosing this is further | |
3826 // back than the one enclosing the else, we're | |
3827 // out of luck too. | |
3828 if (theirscope->lnum < ourscope) | |
3829 break; | |
3830 | |
3831 // and if they're enclosed in a *deeper* brace, | |
3832 // then we can ignore it because it's in a | |
3833 // different scope... | |
3834 if (theirscope->lnum > ourscope) | |
3835 continue; | |
3836 | |
3837 // if it was an "else" (that's not an "else if") | |
3838 // then we need to go back to another if, so | |
3839 // increment elselevel | |
3840 look = cin_skipcomment(ml_get_curline()); | |
3841 if (cin_iselse(look)) | |
3842 { | |
3843 mightbeif = cin_skipcomment(look + 4); | |
3844 if (!cin_isif(mightbeif)) | |
3845 ++elselevel; | |
3846 continue; | |
3847 } | |
3848 | |
3849 // if it was a "while" then we need to go back to | |
3850 // another "do", so increment whilelevel. XXX | |
3851 if (cin_iswhileofdo(look, curwin->w_cursor.lnum)) | |
3852 { | |
3853 ++whilelevel; | |
3854 continue; | |
3855 } | |
3856 | |
3857 // If it's an "if" decrement elselevel | |
3858 look = cin_skipcomment(ml_get_curline()); | |
3859 if (cin_isif(look)) | |
3860 { | |
3861 elselevel--; | |
3862 // When looking for an "if" ignore "while"s that | |
3863 // get in the way. | |
3864 if (elselevel == 0 && lookfor == LOOKFOR_IF) | |
3865 whilelevel = 0; | |
3866 } | |
3867 | |
3868 // If it's a "do" decrement whilelevel | |
3869 if (cin_isdo(look)) | |
3870 whilelevel--; | |
3871 | |
3872 // if we've used up all the elses, then | |
3873 // this must be the if that we want! | |
3874 // match the indent level of that if. | |
3875 if (elselevel <= 0 && whilelevel <= 0) | |
3876 return OK; | |
3877 } | |
3878 } | |
3879 return FAIL; | |
3880 } | |
3881 | |
3882 # if defined(FEAT_EVAL) || defined(PROTO) | |
3883 /* | 1693 /* |
3884 * Get indent level from 'indentexpr'. | 1694 * Get indent level from 'indentexpr'. |
3885 */ | 1695 */ |
3886 int | 1696 int |
3887 get_expr_indent(void) | 1697 get_expr_indent(void) |
3933 if (indent < 0) | 1743 if (indent < 0) |
3934 indent = get_indent(); | 1744 indent = get_indent(); |
3935 | 1745 |
3936 return indent; | 1746 return indent; |
3937 } | 1747 } |
3938 # endif | 1748 #endif |
3939 | |
3940 /* | |
3941 * return TRUE if 'cinkeys' contains the key "keytyped", | |
3942 * when == '*': Only if key is preceded with '*' (indent before insert) | |
3943 * when == '!': Only if key is preceded with '!' (don't insert) | |
3944 * when == ' ': Only if key is not preceded with '*'(indent afterwards) | |
3945 * | |
3946 * "keytyped" can have a few special values: | |
3947 * KEY_OPEN_FORW | |
3948 * KEY_OPEN_BACK | |
3949 * KEY_COMPLETE just finished completion. | |
3950 * | |
3951 * If line_is_empty is TRUE accept keys with '0' before them. | |
3952 */ | |
3953 int | |
3954 in_cinkeys( | |
3955 int keytyped, | |
3956 int when, | |
3957 int line_is_empty) | |
3958 { | |
3959 char_u *look; | |
3960 int try_match; | |
3961 int try_match_word; | |
3962 char_u *p; | |
3963 char_u *line; | |
3964 int icase; | |
3965 int i; | |
3966 | |
3967 if (keytyped == NUL) | |
3968 // Can happen with CTRL-Y and CTRL-E on a short line. | |
3969 return FALSE; | |
3970 | |
3971 #ifdef FEAT_EVAL | |
3972 if (*curbuf->b_p_inde != NUL) | |
3973 look = curbuf->b_p_indk; // 'indentexpr' set: use 'indentkeys' | |
3974 else | |
3975 #endif | |
3976 look = curbuf->b_p_cink; // 'indentexpr' empty: use 'cinkeys' | |
3977 while (*look) | |
3978 { | |
3979 // Find out if we want to try a match with this key, depending on | |
3980 // 'when' and a '*' or '!' before the key. | |
3981 switch (when) | |
3982 { | |
3983 case '*': try_match = (*look == '*'); break; | |
3984 case '!': try_match = (*look == '!'); break; | |
3985 default: try_match = (*look != '*'); break; | |
3986 } | |
3987 if (*look == '*' || *look == '!') | |
3988 ++look; | |
3989 | |
3990 // If there is a '0', only accept a match if the line is empty. | |
3991 // But may still match when typing last char of a word. | |
3992 if (*look == '0') | |
3993 { | |
3994 try_match_word = try_match; | |
3995 if (!line_is_empty) | |
3996 try_match = FALSE; | |
3997 ++look; | |
3998 } | |
3999 else | |
4000 try_match_word = FALSE; | |
4001 | |
4002 // does it look like a control character? | |
4003 if (*look == '^' | |
4004 #ifdef EBCDIC | |
4005 && (Ctrl_chr(look[1]) != 0) | |
4006 #else | |
4007 && look[1] >= '?' && look[1] <= '_' | |
4008 #endif | |
4009 ) | |
4010 { | |
4011 if (try_match && keytyped == Ctrl_chr(look[1])) | |
4012 return TRUE; | |
4013 look += 2; | |
4014 } | |
4015 // 'o' means "o" command, open forward. | |
4016 // 'O' means "O" command, open backward. | |
4017 else if (*look == 'o') | |
4018 { | |
4019 if (try_match && keytyped == KEY_OPEN_FORW) | |
4020 return TRUE; | |
4021 ++look; | |
4022 } | |
4023 else if (*look == 'O') | |
4024 { | |
4025 if (try_match && keytyped == KEY_OPEN_BACK) | |
4026 return TRUE; | |
4027 ++look; | |
4028 } | |
4029 | |
4030 // 'e' means to check for "else" at start of line and just before the | |
4031 // cursor. | |
4032 else if (*look == 'e') | |
4033 { | |
4034 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) | |
4035 { | |
4036 p = ml_get_curline(); | |
4037 if (skipwhite(p) == p + curwin->w_cursor.col - 4 && | |
4038 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) | |
4039 return TRUE; | |
4040 } | |
4041 ++look; | |
4042 } | |
4043 | |
4044 // ':' only causes an indent if it is at the end of a label or case | |
4045 // statement, or when it was before typing the ':' (to fix | |
4046 // class::method for C++). | |
4047 else if (*look == ':') | |
4048 { | |
4049 if (try_match && keytyped == ':') | |
4050 { | |
4051 p = ml_get_curline(); | |
4052 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel()) | |
4053 return TRUE; | |
4054 // Need to get the line again after cin_islabel(). | |
4055 p = ml_get_curline(); | |
4056 if (curwin->w_cursor.col > 2 | |
4057 && p[curwin->w_cursor.col - 1] == ':' | |
4058 && p[curwin->w_cursor.col - 2] == ':') | |
4059 { | |
4060 p[curwin->w_cursor.col - 1] = ' '; | |
4061 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) | |
4062 || cin_islabel()); | |
4063 p = ml_get_curline(); | |
4064 p[curwin->w_cursor.col - 1] = ':'; | |
4065 if (i) | |
4066 return TRUE; | |
4067 } | |
4068 } | |
4069 ++look; | |
4070 } | |
4071 | |
4072 | |
4073 // Is it a key in <>, maybe? | |
4074 else if (*look == '<') | |
4075 { | |
4076 if (try_match) | |
4077 { | |
4078 // make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, | |
4079 // <:> and <!> so that people can re-indent on o, O, e, 0, <, | |
4080 // >, *, : and ! keys if they really really want to. | |
4081 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL | |
4082 && keytyped == look[1]) | |
4083 return TRUE; | |
4084 | |
4085 if (keytyped == get_special_key_code(look + 1)) | |
4086 return TRUE; | |
4087 } | |
4088 while (*look && *look != '>') | |
4089 look++; | |
4090 while (*look == '>') | |
4091 look++; | |
4092 } | |
4093 | |
4094 // Is it a word: "=word"? | |
4095 else if (*look == '=' && look[1] != ',' && look[1] != NUL) | |
4096 { | |
4097 ++look; | |
4098 if (*look == '~') | |
4099 { | |
4100 icase = TRUE; | |
4101 ++look; | |
4102 } | |
4103 else | |
4104 icase = FALSE; | |
4105 p = vim_strchr(look, ','); | |
4106 if (p == NULL) | |
4107 p = look + STRLEN(look); | |
4108 if ((try_match || try_match_word) | |
4109 && curwin->w_cursor.col >= (colnr_T)(p - look)) | |
4110 { | |
4111 int match = FALSE; | |
4112 | |
4113 if (keytyped == KEY_COMPLETE) | |
4114 { | |
4115 char_u *s; | |
4116 | |
4117 // Just completed a word, check if it starts with "look". | |
4118 // search back for the start of a word. | |
4119 line = ml_get_curline(); | |
4120 if (has_mbyte) | |
4121 { | |
4122 char_u *n; | |
4123 | |
4124 for (s = line + curwin->w_cursor.col; s > line; s = n) | |
4125 { | |
4126 n = mb_prevptr(line, s); | |
4127 if (!vim_iswordp(n)) | |
4128 break; | |
4129 } | |
4130 } | |
4131 else | |
4132 for (s = line + curwin->w_cursor.col; s > line; --s) | |
4133 if (!vim_iswordc(s[-1])) | |
4134 break; | |
4135 if (s + (p - look) <= line + curwin->w_cursor.col | |
4136 && (icase | |
4137 ? MB_STRNICMP(s, look, p - look) | |
4138 : STRNCMP(s, look, p - look)) == 0) | |
4139 match = TRUE; | |
4140 } | |
4141 else | |
4142 // TODO: multi-byte | |
4143 if (keytyped == (int)p[-1] || (icase && keytyped < 256 | |
4144 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) | |
4145 { | |
4146 line = ml_get_cursor(); | |
4147 if ((curwin->w_cursor.col == (colnr_T)(p - look) | |
4148 || !vim_iswordc(line[-(p - look) - 1])) | |
4149 && (icase | |
4150 ? MB_STRNICMP(line - (p - look), look, p - look) | |
4151 : STRNCMP(line - (p - look), look, p - look)) | |
4152 == 0) | |
4153 match = TRUE; | |
4154 } | |
4155 if (match && try_match_word && !try_match) | |
4156 { | |
4157 // "0=word": Check if there are only blanks before the | |
4158 // word. | |
4159 if (getwhitecols_curline() != | |
4160 (int)(curwin->w_cursor.col - (p - look))) | |
4161 match = FALSE; | |
4162 } | |
4163 if (match) | |
4164 return TRUE; | |
4165 } | |
4166 look = p; | |
4167 } | |
4168 | |
4169 // ok, it's a boring generic character. | |
4170 else | |
4171 { | |
4172 if (try_match && *look == keytyped) | |
4173 return TRUE; | |
4174 if (*look != NUL) | |
4175 ++look; | |
4176 } | |
4177 | |
4178 // Skip over ", ". | |
4179 look = skip_to_option_part(look); | |
4180 } | |
4181 return FALSE; | |
4182 } | |
4183 #endif // FEAT_CINDENT | |
4184 | 1749 |
4185 #if defined(FEAT_LISP) || defined(PROTO) | 1750 #if defined(FEAT_LISP) || defined(PROTO) |
4186 | 1751 |
4187 static int | 1752 static int |
4188 lisp_match(char_u *p) | 1753 lisp_match(char_u *p) |
4399 | 1964 |
4400 return amount; | 1965 return amount; |
4401 } | 1966 } |
4402 #endif // FEAT_LISP | 1967 #endif // FEAT_LISP |
4403 | 1968 |
4404 #if defined(FEAT_CINDENT) || defined(PROTO) | |
4405 /* | |
4406 * Do C or expression indenting on the current line. | |
4407 */ | |
4408 void | |
4409 do_c_expr_indent(void) | |
4410 { | |
4411 # ifdef FEAT_EVAL | |
4412 if (*curbuf->b_p_inde != NUL) | |
4413 fixthisline(get_expr_indent); | |
4414 else | |
4415 # endif | |
4416 fixthisline(get_c_indent); | |
4417 } | |
4418 #endif | |
4419 | |
4420 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) | 1969 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
4421 /* | 1970 /* |
4422 * Re-indent the current line, based on the current contents of it and the | 1971 * Re-indent the current line, based on the current contents of it and the |
4423 * surrounding lines. Fixing the cursor position seems really easy -- I'm very | 1972 * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
4424 * confused what all the part that handles Control-T is doing that I'm not. | 1973 * confused what all the part that handles Control-T is doing that I'm not. |
4453 # ifdef FEAT_CINDENT | 2002 # ifdef FEAT_CINDENT |
4454 if (cindent_on()) | 2003 if (cindent_on()) |
4455 do_c_expr_indent(); | 2004 do_c_expr_indent(); |
4456 # endif | 2005 # endif |
4457 } | 2006 } |
4458 | 2007 #endif |
4459 #endif | 2008 |
4460 | 2009 #if defined(FEAT_EVAL) || defined(PROTO) |
4461 #if defined(FEAT_VARTABS) || defined(PROTO) | 2010 /* |
4462 | 2011 * "indent()" function |
4463 /* | |
4464 * Set the integer values corresponding to the string setting of 'vartabstop'. | |
4465 * "array" will be set, caller must free it if needed. | |
4466 */ | |
4467 int | |
4468 tabstop_set(char_u *var, int **array) | |
4469 { | |
4470 int valcount = 1; | |
4471 int t; | |
4472 char_u *cp; | |
4473 | |
4474 if (var[0] == NUL || (var[0] == '0' && var[1] == NUL)) | |
4475 { | |
4476 *array = NULL; | |
4477 return TRUE; | |
4478 } | |
4479 | |
4480 for (cp = var; *cp != NUL; ++cp) | |
4481 { | |
4482 if (cp == var || cp[-1] == ',') | |
4483 { | |
4484 char_u *end; | |
4485 | |
4486 if (strtol((char *)cp, (char **)&end, 10) <= 0) | |
4487 { | |
4488 if (cp != end) | |
4489 emsg(_(e_positive)); | |
4490 else | |
4491 emsg(_(e_invarg)); | |
4492 return FALSE; | |
4493 } | |
4494 } | |
4495 | |
4496 if (VIM_ISDIGIT(*cp)) | |
4497 continue; | |
4498 if (cp[0] == ',' && cp > var && cp[-1] != ',' && cp[1] != NUL) | |
4499 { | |
4500 ++valcount; | |
4501 continue; | |
4502 } | |
4503 emsg(_(e_invarg)); | |
4504 return FALSE; | |
4505 } | |
4506 | |
4507 *array = ALLOC_MULT(int, valcount + 1); | |
4508 if (*array == NULL) | |
4509 return FALSE; | |
4510 (*array)[0] = valcount; | |
4511 | |
4512 t = 1; | |
4513 for (cp = var; *cp != NUL;) | |
4514 { | |
4515 (*array)[t++] = atoi((char *)cp); | |
4516 while (*cp != NUL && *cp != ',') | |
4517 ++cp; | |
4518 if (*cp != NUL) | |
4519 ++cp; | |
4520 } | |
4521 | |
4522 return TRUE; | |
4523 } | |
4524 | |
4525 /* | |
4526 * Calculate the number of screen spaces a tab will occupy. | |
4527 * If "vts" is set then the tab widths are taken from that array, | |
4528 * otherwise the value of ts is used. | |
4529 */ | |
4530 int | |
4531 tabstop_padding(colnr_T col, int ts_arg, int *vts) | |
4532 { | |
4533 int ts = ts_arg == 0 ? 8 : ts_arg; | |
4534 int tabcount; | |
4535 colnr_T tabcol = 0; | |
4536 int t; | |
4537 int padding = 0; | |
4538 | |
4539 if (vts == NULL || vts[0] == 0) | |
4540 return ts - (col % ts); | |
4541 | |
4542 tabcount = vts[0]; | |
4543 | |
4544 for (t = 1; t <= tabcount; ++t) | |
4545 { | |
4546 tabcol += vts[t]; | |
4547 if (tabcol > col) | |
4548 { | |
4549 padding = (int)(tabcol - col); | |
4550 break; | |
4551 } | |
4552 } | |
4553 if (t > tabcount) | |
4554 padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]); | |
4555 | |
4556 return padding; | |
4557 } | |
4558 | |
4559 /* | |
4560 * Find the size of the tab that covers a particular column. | |
4561 */ | |
4562 int | |
4563 tabstop_at(colnr_T col, int ts, int *vts) | |
4564 { | |
4565 int tabcount; | |
4566 colnr_T tabcol = 0; | |
4567 int t; | |
4568 int tab_size = 0; | |
4569 | |
4570 if (vts == 0 || vts[0] == 0) | |
4571 return ts; | |
4572 | |
4573 tabcount = vts[0]; | |
4574 for (t = 1; t <= tabcount; ++t) | |
4575 { | |
4576 tabcol += vts[t]; | |
4577 if (tabcol > col) | |
4578 { | |
4579 tab_size = vts[t]; | |
4580 break; | |
4581 } | |
4582 } | |
4583 if (t > tabcount) | |
4584 tab_size = vts[tabcount]; | |
4585 | |
4586 return tab_size; | |
4587 } | |
4588 | |
4589 /* | |
4590 * Find the column on which a tab starts. | |
4591 */ | |
4592 colnr_T | |
4593 tabstop_start(colnr_T col, int ts, int *vts) | |
4594 { | |
4595 int tabcount; | |
4596 colnr_T tabcol = 0; | |
4597 int t; | |
4598 int excess; | |
4599 | |
4600 if (vts == NULL || vts[0] == 0) | |
4601 return (col / ts) * ts; | |
4602 | |
4603 tabcount = vts[0]; | |
4604 for (t = 1; t <= tabcount; ++t) | |
4605 { | |
4606 tabcol += vts[t]; | |
4607 if (tabcol > col) | |
4608 return tabcol - vts[t]; | |
4609 } | |
4610 | |
4611 excess = tabcol % vts[tabcount]; | |
4612 return excess + ((col - excess) / vts[tabcount]) * vts[tabcount]; | |
4613 } | |
4614 | |
4615 /* | |
4616 * Find the number of tabs and spaces necessary to get from one column | |
4617 * to another. | |
4618 */ | 2012 */ |
4619 void | 2013 void |
4620 tabstop_fromto( | 2014 f_indent(typval_T *argvars, typval_T *rettv) |
4621 colnr_T start_col, | 2015 { |
4622 colnr_T end_col, | 2016 linenr_T lnum; |
4623 int ts_arg, | 2017 |
4624 int *vts, | 2018 lnum = tv_get_lnum(argvars); |
4625 int *ntabs, | 2019 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
4626 int *nspcs) | 2020 rettv->vval.v_number = get_indent_lnum(lnum); |
4627 { | 2021 else |
4628 int spaces = end_col - start_col; | 2022 rettv->vval.v_number = -1; |
4629 colnr_T tabcol = 0; | 2023 } |
4630 int padding = 0; | 2024 |
4631 int tabcount; | 2025 /* |
4632 int t; | 2026 * "lispindent(lnum)" function |
4633 int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg; | 2027 */ |
4634 | 2028 void |
4635 if (vts == NULL || vts[0] == 0) | 2029 f_lispindent(typval_T *argvars UNUSED, typval_T *rettv) |
4636 { | 2030 { |
4637 int tabs = 0; | 2031 #ifdef FEAT_LISP |
4638 int initspc = 0; | 2032 pos_T pos; |
4639 | 2033 linenr_T lnum; |
4640 initspc = ts - (start_col % ts); | 2034 |
4641 if (spaces >= initspc) | 2035 pos = curwin->w_cursor; |
4642 { | 2036 lnum = tv_get_lnum(argvars); |
4643 spaces -= initspc; | 2037 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count) |
4644 tabs++; | 2038 { |
4645 } | 2039 curwin->w_cursor.lnum = lnum; |
4646 tabs += spaces / ts; | 2040 rettv->vval.v_number = get_lisp_indent(); |
4647 spaces -= (spaces / ts) * ts; | 2041 curwin->w_cursor = pos; |
4648 | 2042 } |
4649 *ntabs = tabs; | 2043 else |
4650 *nspcs = spaces; | 2044 #endif |
4651 return; | 2045 rettv->vval.v_number = -1; |
4652 } | 2046 } |
4653 | 2047 #endif |
4654 // Find the padding needed to reach the next tabstop. | |
4655 tabcount = vts[0]; | |
4656 for (t = 1; t <= tabcount; ++t) | |
4657 { | |
4658 tabcol += vts[t]; | |
4659 if (tabcol > start_col) | |
4660 { | |
4661 padding = (int)(tabcol - start_col); | |
4662 break; | |
4663 } | |
4664 } | |
4665 if (t > tabcount) | |
4666 padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]); | |
4667 | |
4668 // If the space needed is less than the padding no tabs can be used. | |
4669 if (spaces < padding) | |
4670 { | |
4671 *ntabs = 0; | |
4672 *nspcs = spaces; | |
4673 return; | |
4674 } | |
4675 | |
4676 *ntabs = 1; | |
4677 spaces -= padding; | |
4678 | |
4679 // At least one tab has been used. See if any more will fit. | |
4680 while (spaces != 0 && ++t <= tabcount) | |
4681 { | |
4682 padding = vts[t]; | |
4683 if (spaces < padding) | |
4684 { | |
4685 *nspcs = spaces; | |
4686 return; | |
4687 } | |
4688 ++*ntabs; | |
4689 spaces -= padding; | |
4690 } | |
4691 | |
4692 *ntabs += spaces / vts[tabcount]; | |
4693 *nspcs = spaces % vts[tabcount]; | |
4694 } | |
4695 | |
4696 /* | |
4697 * See if two tabstop arrays contain the same values. | |
4698 */ | |
4699 int | |
4700 tabstop_eq(int *ts1, int *ts2) | |
4701 { | |
4702 int t; | |
4703 | |
4704 if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0)) | |
4705 return FALSE; | |
4706 if (ts1 == ts2) | |
4707 return TRUE; | |
4708 if (ts1[0] != ts2[0]) | |
4709 return FALSE; | |
4710 | |
4711 for (t = 1; t <= ts1[0]; ++t) | |
4712 if (ts1[t] != ts2[t]) | |
4713 return FALSE; | |
4714 | |
4715 return TRUE; | |
4716 } | |
4717 | |
4718 #if defined(FEAT_BEVAL) || defined(PROTO) | |
4719 /* | |
4720 * Copy a tabstop array, allocating space for the new array. | |
4721 */ | |
4722 int * | |
4723 tabstop_copy(int *oldts) | |
4724 { | |
4725 int *newts; | |
4726 int t; | |
4727 | |
4728 if (oldts == NULL) | |
4729 return NULL; | |
4730 newts = ALLOC_MULT(int, oldts[0] + 1); | |
4731 if (newts != NULL) | |
4732 for (t = 0; t <= oldts[0]; ++t) | |
4733 newts[t] = oldts[t]; | |
4734 return newts; | |
4735 } | |
4736 #endif | |
4737 | |
4738 /* | |
4739 * Return a count of the number of tabstops. | |
4740 */ | |
4741 int | |
4742 tabstop_count(int *ts) | |
4743 { | |
4744 return ts != NULL ? ts[0] : 0; | |
4745 } | |
4746 | |
4747 /* | |
4748 * Return the first tabstop, or 8 if there are no tabstops defined. | |
4749 */ | |
4750 int | |
4751 tabstop_first(int *ts) | |
4752 { | |
4753 return ts != NULL ? ts[1] : 8; | |
4754 } | |
4755 | |
4756 #endif | |
4757 | |
4758 /* | |
4759 * Return the effective shiftwidth value for current buffer, using the | |
4760 * 'tabstop' value when 'shiftwidth' is zero. | |
4761 */ | |
4762 long | |
4763 get_sw_value(buf_T *buf) | |
4764 { | |
4765 return get_sw_value_col(buf, 0); | |
4766 } | |
4767 | |
4768 /* | |
4769 * Idem, using "pos". | |
4770 */ | |
4771 static long | |
4772 get_sw_value_pos(buf_T *buf, pos_T *pos) | |
4773 { | |
4774 pos_T save_cursor = curwin->w_cursor; | |
4775 long sw_value; | |
4776 | |
4777 curwin->w_cursor = *pos; | |
4778 sw_value = get_sw_value_col(buf, get_nolist_virtcol()); | |
4779 curwin->w_cursor = save_cursor; | |
4780 return sw_value; | |
4781 } | |
4782 | |
4783 /* | |
4784 * Idem, using the first non-black in the current line. | |
4785 */ | |
4786 long | |
4787 get_sw_value_indent(buf_T *buf) | |
4788 { | |
4789 pos_T pos = curwin->w_cursor; | |
4790 | |
4791 pos.col = getwhitecols_curline(); | |
4792 return get_sw_value_pos(buf, &pos); | |
4793 } | |
4794 | |
4795 /* | |
4796 * Idem, using virtual column "col". | |
4797 */ | |
4798 long | |
4799 get_sw_value_col(buf_T *buf, colnr_T col UNUSED) | |
4800 { | |
4801 return buf->b_p_sw ? buf->b_p_sw : | |
4802 #ifdef FEAT_VARTABS | |
4803 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array); | |
4804 #else | |
4805 buf->b_p_ts; | |
4806 #endif | |
4807 } | |
4808 | |
4809 /* | |
4810 * Return the effective softtabstop value for the current buffer, using the | |
4811 * 'shiftwidth' value when 'softtabstop' is negative. | |
4812 */ | |
4813 long | |
4814 get_sts_value(void) | |
4815 { | |
4816 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts; | |
4817 } |