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