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