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