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