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