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