blob: 9b2b6442a61b0092077b4c59ec69f38a21ed1407 [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;
1733 int n;
1734 int divider;
1735 int fraction = 0;
1736 int sw = (int)get_sw_value(buf);
1737
1738 // Set the default values.
1739
1740 // Spaces from a block's opening brace the prevailing indent for that
1741 // block should be.
1742 buf->b_ind_level = sw;
1743
1744 // Spaces from the edge of the line an open brace that's at the end of a
1745 // line is imagined to be.
1746 buf->b_ind_open_imag = 0;
1747
1748 // Spaces from the prevailing indent for a line that is not preceded by
1749 // an opening brace.
1750 buf->b_ind_no_brace = 0;
1751
1752 // Column where the first { of a function should be located }.
1753 buf->b_ind_first_open = 0;
1754
1755 // Spaces from the prevailing indent a leftmost open brace should be
1756 // located.
1757 buf->b_ind_open_extra = 0;
1758
1759 // Spaces from the matching open brace (real location for one at the left
1760 // edge; imaginary location from one that ends a line) the matching close
1761 // brace should be located.
1762 buf->b_ind_close_extra = 0;
1763
1764 // Spaces from the edge of the line an open brace sitting in the leftmost
1765 // column is imagined to be.
1766 buf->b_ind_open_left_imag = 0;
1767
1768 // Spaces jump labels should be shifted to the left if N is non-negative,
1769 // otherwise the jump label will be put to column 1.
1770 buf->b_ind_jump_label = -1;
1771
1772 // Spaces from the switch() indent a "case xx" label should be located.
1773 buf->b_ind_case = sw;
1774
1775 // Spaces from the "case xx:" code after a switch() should be located.
1776 buf->b_ind_case_code = sw;
1777
1778 // Lineup break at end of case in switch() with case label.
1779 buf->b_ind_case_break = 0;
1780
1781 // Spaces from the class declaration indent a scope declaration label
1782 // should be located.
1783 buf->b_ind_scopedecl = sw;
1784
1785 // Spaces from the scope declaration label code should be located.
1786 buf->b_ind_scopedecl_code = sw;
1787
1788 // Amount K&R-style parameters should be indented.
1789 buf->b_ind_param = sw;
1790
1791 // Amount a function type spec should be indented.
1792 buf->b_ind_func_type = sw;
1793
1794 // Amount a cpp base class declaration or constructor initialization
1795 // should be indented.
1796 buf->b_ind_cpp_baseclass = sw;
1797
1798 // additional spaces beyond the prevailing indent a continuation line
1799 // should be located.
1800 buf->b_ind_continuation = sw;
1801
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001802 // Spaces from the indent of the line with an unclosed parenthesis.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001803 buf->b_ind_unclosed = sw * 2;
1804
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001805 // Spaces from the indent of the line with an unclosed parenthesis, which
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001806 // itself is also unclosed.
1807 buf->b_ind_unclosed2 = sw;
1808
1809 // Suppress ignoring spaces from the indent of a line starting with an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001810 // unclosed parenthesis.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001811 buf->b_ind_unclosed_noignore = 0;
1812
1813 // If the opening paren is the last nonwhite character on the line, and
1814 // b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
1815 // context (for very long lines).
1816 buf->b_ind_unclosed_wrapped = 0;
1817
1818 // Suppress ignoring white space when lining up with the character after
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001819 // an unclosed parenthesis.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001820 buf->b_ind_unclosed_whiteok = 0;
1821
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001822 // Indent a closing parenthesis under the line start of the matching
1823 // opening parenthesis.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001824 buf->b_ind_matching_paren = 0;
1825
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001826 // Indent a closing parenthesis under the previous line.
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001827 buf->b_ind_paren_prev = 0;
1828
1829 // Extra indent for comments.
1830 buf->b_ind_comment = 0;
1831
1832 // Spaces from the comment opener when there is nothing after it.
1833 buf->b_ind_in_comment = 3;
1834
1835 // Boolean: if non-zero, use b_ind_in_comment even if there is something
1836 // after the comment opener.
1837 buf->b_ind_in_comment2 = 0;
1838
1839 // Max lines to search for an open paren.
1840 buf->b_ind_maxparen = 20;
1841
1842 // Max lines to search for an open comment.
1843 buf->b_ind_maxcomment = 70;
1844
1845 // Handle braces for java code.
1846 buf->b_ind_java = 0;
1847
1848 // Not to confuse JS object properties with labels.
1849 buf->b_ind_js = 0;
1850
1851 // Handle blocked cases correctly.
1852 buf->b_ind_keep_case_label = 0;
1853
1854 // Handle C++ namespace.
1855 buf->b_ind_cpp_namespace = 0;
1856
Bram Moolenaarc9471b12023-05-09 15:00:00 +01001857 // Handle continuation lines containing conditions of if (), for () and
1858 // while ().
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001859 buf->b_ind_if_for_while = 0;
1860
1861 // indentation for # comments
1862 buf->b_ind_hash_comment = 0;
1863
1864 // Handle C++ extern "C" or "C++"
1865 buf->b_ind_cpp_extern_c = 0;
1866
Bram Moolenaard881b512020-05-31 17:49:30 +02001867 // Handle C #pragma directives
1868 buf->b_ind_pragma = 0;
1869
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001870 for (p = buf->b_p_cino; *p; )
1871 {
1872 l = p++;
1873 if (*p == '-')
1874 ++p;
1875 digits = p; // remember where the digits start
1876 n = getdigits(&p);
1877 divider = 0;
1878 if (*p == '.') // ".5s" means a fraction
1879 {
1880 fraction = atol((char *)++p);
1881 while (VIM_ISDIGIT(*p))
1882 {
1883 ++p;
1884 if (divider)
1885 divider *= 10;
1886 else
1887 divider = 10;
1888 }
1889 }
1890 if (*p == 's') // "2s" means two times 'shiftwidth'
1891 {
1892 if (p == digits)
1893 n = sw; // just "s" is one 'shiftwidth'
1894 else
1895 {
1896 n *= sw;
1897 if (divider)
1898 n += (sw * fraction + divider / 2) / divider;
1899 }
1900 ++p;
1901 }
1902 if (l[1] == '-')
1903 n = -n;
1904
1905 // When adding an entry here, also update the default 'cinoptions' in
1906 // doc/indent.txt, and add explanation for it!
1907 switch (*l)
1908 {
1909 case '>': buf->b_ind_level = n; break;
1910 case 'e': buf->b_ind_open_imag = n; break;
1911 case 'n': buf->b_ind_no_brace = n; break;
1912 case 'f': buf->b_ind_first_open = n; break;
1913 case '{': buf->b_ind_open_extra = n; break;
1914 case '}': buf->b_ind_close_extra = n; break;
1915 case '^': buf->b_ind_open_left_imag = n; break;
1916 case 'L': buf->b_ind_jump_label = n; break;
1917 case ':': buf->b_ind_case = n; break;
1918 case '=': buf->b_ind_case_code = n; break;
1919 case 'b': buf->b_ind_case_break = n; break;
1920 case 'p': buf->b_ind_param = n; break;
1921 case 't': buf->b_ind_func_type = n; break;
1922 case '/': buf->b_ind_comment = n; break;
1923 case 'c': buf->b_ind_in_comment = n; break;
1924 case 'C': buf->b_ind_in_comment2 = n; break;
1925 case 'i': buf->b_ind_cpp_baseclass = n; break;
1926 case '+': buf->b_ind_continuation = n; break;
1927 case '(': buf->b_ind_unclosed = n; break;
1928 case 'u': buf->b_ind_unclosed2 = n; break;
1929 case 'U': buf->b_ind_unclosed_noignore = n; break;
1930 case 'W': buf->b_ind_unclosed_wrapped = n; break;
1931 case 'w': buf->b_ind_unclosed_whiteok = n; break;
1932 case 'm': buf->b_ind_matching_paren = n; break;
1933 case 'M': buf->b_ind_paren_prev = n; break;
1934 case ')': buf->b_ind_maxparen = n; break;
1935 case '*': buf->b_ind_maxcomment = n; break;
1936 case 'g': buf->b_ind_scopedecl = n; break;
1937 case 'h': buf->b_ind_scopedecl_code = n; break;
1938 case 'j': buf->b_ind_java = n; break;
1939 case 'J': buf->b_ind_js = n; break;
1940 case 'l': buf->b_ind_keep_case_label = n; break;
1941 case '#': buf->b_ind_hash_comment = n; break;
1942 case 'N': buf->b_ind_cpp_namespace = n; break;
1943 case 'k': buf->b_ind_if_for_while = n; break;
1944 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaard881b512020-05-31 17:49:30 +02001945 case 'P': buf->b_ind_pragma = n; break;
Bram Moolenaar14c01f82019-10-09 22:53:08 +02001946 }
1947 if (*p == ',')
1948 ++p;
1949 }
1950}
1951
1952 static int
1953find_match(int lookfor, linenr_T ourscope)
1954{
1955 char_u *look;
1956 pos_T *theirscope;
1957 char_u *mightbeif;
1958 int elselevel;
1959 int whilelevel;
1960
1961 if (lookfor == LOOKFOR_IF)
1962 {
1963 elselevel = 1;
1964 whilelevel = 0;
1965 }
1966 else
1967 {
1968 elselevel = 0;
1969 whilelevel = 1;
1970 }
1971
1972 curwin->w_cursor.col = 0;
1973
1974 while (curwin->w_cursor.lnum > ourscope + 1)
1975 {
1976 curwin->w_cursor.lnum--;
1977 curwin->w_cursor.col = 0;
1978
1979 look = cin_skipcomment(ml_get_curline());
1980 if (cin_iselse(look)
1981 || cin_isif(look)
1982 || cin_isdo(look) // XXX
1983 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
1984 {
1985 // if we've gone outside the braces entirely,
1986 // we must be out of scope...
1987 theirscope = find_start_brace(); // XXX
1988 if (theirscope == NULL)
1989 break;
1990
1991 // and if the brace enclosing this is further
1992 // back than the one enclosing the else, we're
1993 // out of luck too.
1994 if (theirscope->lnum < ourscope)
1995 break;
1996
1997 // and if they're enclosed in a *deeper* brace,
1998 // then we can ignore it because it's in a
1999 // different scope...
2000 if (theirscope->lnum > ourscope)
2001 continue;
2002
2003 // if it was an "else" (that's not an "else if")
2004 // then we need to go back to another if, so
2005 // increment elselevel
2006 look = cin_skipcomment(ml_get_curline());
2007 if (cin_iselse(look))
2008 {
2009 mightbeif = cin_skipcomment(look + 4);
2010 if (!cin_isif(mightbeif))
2011 ++elselevel;
2012 continue;
2013 }
2014
2015 // if it was a "while" then we need to go back to
2016 // another "do", so increment whilelevel. XXX
2017 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
2018 {
2019 ++whilelevel;
2020 continue;
2021 }
2022
2023 // If it's an "if" decrement elselevel
2024 look = cin_skipcomment(ml_get_curline());
2025 if (cin_isif(look))
2026 {
2027 elselevel--;
2028 // When looking for an "if" ignore "while"s that
2029 // get in the way.
2030 if (elselevel == 0 && lookfor == LOOKFOR_IF)
2031 whilelevel = 0;
2032 }
2033
2034 // If it's a "do" decrement whilelevel
2035 if (cin_isdo(look))
2036 whilelevel--;
2037
2038 // if we've used up all the elses, then
2039 // this must be the if that we want!
2040 // match the indent level of that if.
2041 if (elselevel <= 0 && whilelevel <= 0)
2042 return OK;
2043 }
2044 }
2045 return FAIL;
2046}
2047
2048/*
2049 * Return the desired indent for C code.
2050 * Return -1 if the indent should be left alone (inside a raw string).
2051 */
2052 int
2053get_c_indent(void)
2054{
2055 pos_T cur_curpos;
2056 int amount;
2057 int scope_amount;
2058 int cur_amount = MAXCOL;
2059 colnr_T col;
2060 char_u *theline;
2061 char_u *linecopy;
2062 pos_T *trypos;
2063 pos_T *comment_pos;
2064 pos_T *tryposBrace = NULL;
2065 pos_T tryposCopy;
2066 pos_T our_paren_pos;
2067 char_u *start;
2068 int start_brace;
2069#define BRACE_IN_COL0 1 // '{' is in column 0
2070#define BRACE_AT_START 2 // '{' is at start of line
2071#define BRACE_AT_END 3 // '{' is at end of line
2072 linenr_T ourscope;
2073 char_u *l;
2074 char_u *look;
2075 char_u terminated;
2076 int lookfor;
2077 int whilelevel;
2078 linenr_T lnum;
2079 int n;
2080 int iscase;
2081 int lookfor_break;
2082 int lookfor_cpp_namespace = FALSE;
2083 int cont_amount = 0; // amount for continuation line
2084 int original_line_islabel;
2085 int added_to_amount = 0;
2086 int js_cur_has_key = 0;
2087 linenr_T raw_string_start = 0;
2088 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
2089
2090 // make a copy, value is changed below
2091 int ind_continuation = curbuf->b_ind_continuation;
2092
2093 // remember where the cursor was when we started
2094 cur_curpos = curwin->w_cursor;
2095
2096 // if we are at line 1 zero indent is fine, right?
2097 if (cur_curpos.lnum == 1)
2098 return 0;
2099
2100 // Get a copy of the current contents of the line.
2101 // This is required, because only the most recent line obtained with
2102 // ml_get is valid!
2103 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
2104 if (linecopy == NULL)
2105 return 0;
2106
2107 // In insert mode and the cursor is on a ')' truncate the line at the
2108 // cursor position. We don't want to line up with the matching '(' when
2109 // inserting new stuff.
2110 // For unknown reasons the cursor might be past the end of the line, thus
2111 // check for that.
Bram Moolenaar24959102022-05-07 20:01:16 +01002112 if ((State & MODE_INSERT)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002113 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
2114 && linecopy[curwin->w_cursor.col] == ')')
2115 linecopy[curwin->w_cursor.col] = NUL;
2116
2117 theline = skipwhite(linecopy);
2118
2119 // move the cursor to the start of the line
2120
2121 curwin->w_cursor.col = 0;
2122
2123 original_line_islabel = cin_islabel(); // XXX
2124
2125 // If we are inside a raw string don't change the indent.
2126 // Ignore a raw string inside a comment.
2127 comment_pos = ind_find_start_comment();
2128 if (comment_pos != NULL)
2129 {
2130 // findmatchlimit() static pos is overwritten, make a copy
2131 tryposCopy = *comment_pos;
2132 comment_pos = &tryposCopy;
2133 }
2134 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
2135 if (trypos != NULL && (comment_pos == NULL
2136 || LT_POS(*trypos, *comment_pos)))
2137 {
2138 amount = -1;
2139 goto laterend;
2140 }
2141
Bram Moolenaard881b512020-05-31 17:49:30 +02002142 // #defines and so on go at the left when included in 'cinkeys',
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002143 // excluding pragmas when customized in 'cinoptions'
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002144 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
2145 {
Bram Moolenaard881b512020-05-31 17:49:30 +02002146 char_u *directive = skipwhite(theline + 1);
2147 if (curbuf->b_ind_pragma == 0 || STRNCMP(directive, "pragma", 6) != 0)
2148 {
2149 amount = curbuf->b_ind_hash_comment;
2150 goto theend;
2151 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002152 }
2153
2154 // Is it a non-case label? Then that goes at the left margin too unless:
2155 // - JS flag is set.
2156 // - 'L' item has a positive value.
2157 if (original_line_islabel && !curbuf->b_ind_js
2158 && curbuf->b_ind_jump_label < 0)
2159 {
2160 amount = 0;
2161 goto theend;
2162 }
2163
2164 // If we're inside a "//" comment and there is a "//" comment in a
2165 // previous line, lineup with that one.
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002166 if (cin_islinecomment(theline))
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002167 {
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002168 pos_T linecomment_pos;
2169
2170 trypos = find_line_comment(); // XXX
2171 if (trypos == NULL && curwin->w_cursor.lnum > 1)
2172 {
2173 // There may be a statement before the comment, search from the end
2174 // of the line for a comment start.
2175 linecomment_pos.col =
2176 check_linecomment(ml_get(curwin->w_cursor.lnum - 1));
2177 if (linecomment_pos.col != MAXCOL)
2178 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002179 trypos = &linecomment_pos;
2180 trypos->lnum = curwin->w_cursor.lnum - 1;
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002181 }
2182 }
2183 if (trypos != NULL)
2184 {
2185 // find how indented the line beginning the comment is
2186 getvcol(curwin, trypos, &col, NULL, NULL);
2187 amount = col;
2188 goto theend;
2189 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002190 }
2191
2192 // If we're inside a comment and not looking at the start of the
2193 // comment, try using the 'comments' option.
2194 if (!cin_iscomment(theline) && comment_pos != NULL) // XXX
2195 {
2196 int lead_start_len = 2;
2197 int lead_middle_len = 1;
2198 char_u lead_start[COM_MAX_LEN]; // start-comment string
2199 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
2200 char_u lead_end[COM_MAX_LEN]; // end-comment string
2201 char_u *p;
2202 int start_align = 0;
2203 int start_off = 0;
2204 int done = FALSE;
2205
2206 // find how indented the line beginning the comment is
2207 getvcol(curwin, comment_pos, &col, NULL, NULL);
2208 amount = col;
2209 *lead_start = NUL;
2210 *lead_middle = NUL;
2211
2212 p = curbuf->b_p_com;
2213 while (*p != NUL)
2214 {
2215 int align = 0;
2216 int off = 0;
2217 int what = 0;
2218
2219 while (*p != NUL && *p != ':')
2220 {
2221 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
2222 what = *p++;
2223 else if (*p == COM_LEFT || *p == COM_RIGHT)
2224 align = *p++;
2225 else if (VIM_ISDIGIT(*p) || *p == '-')
2226 off = getdigits(&p);
2227 else
2228 ++p;
2229 }
2230
2231 if (*p == ':')
2232 ++p;
2233 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2234 if (what == COM_START)
2235 {
2236 STRCPY(lead_start, lead_end);
2237 lead_start_len = (int)STRLEN(lead_start);
2238 start_off = off;
2239 start_align = align;
2240 }
2241 else if (what == COM_MIDDLE)
2242 {
2243 STRCPY(lead_middle, lead_end);
2244 lead_middle_len = (int)STRLEN(lead_middle);
2245 }
2246 else if (what == COM_END)
2247 {
2248 // If our line starts with the middle comment string, line it
2249 // up with the comment opener per the 'comments' option.
2250 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
2251 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
2252 {
2253 done = TRUE;
2254 if (curwin->w_cursor.lnum > 1)
2255 {
2256 // If the start comment string matches in the previous
2257 // line, use the indent of that line plus offset. If
2258 // the middle comment string matches in the previous
2259 // line, use the indent of that line. XXX
2260 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
2261 if (STRNCMP(look, lead_start, lead_start_len) == 0)
2262 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
2263 else if (STRNCMP(look, lead_middle,
2264 lead_middle_len) == 0)
2265 {
2266 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
2267 break;
2268 }
2269 // If the start comment string doesn't match with the
2270 // start of the comment, skip this entry. XXX
zeertzjq122dea72022-07-27 15:48:45 +01002271 else if (STRNCMP(ml_get(comment_pos->lnum)
2272 + comment_pos->col,
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002273 lead_start, lead_start_len) != 0)
2274 continue;
2275 }
2276 if (start_off != 0)
2277 amount += start_off;
2278 else if (start_align == COM_RIGHT)
2279 amount += vim_strsize(lead_start)
2280 - vim_strsize(lead_middle);
2281 break;
2282 }
2283
2284 // If our line starts with the end comment string, line it up
2285 // with the middle comment
2286 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
2287 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
2288 {
2289 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
2290 // XXX
2291 if (off != 0)
2292 amount += off;
2293 else if (align == COM_RIGHT)
2294 amount += vim_strsize(lead_start)
2295 - vim_strsize(lead_middle);
2296 done = TRUE;
2297 break;
2298 }
2299 }
2300 }
2301
2302 // If our line starts with an asterisk, line up with the
2303 // asterisk in the comment opener; otherwise, line up
2304 // with the first character of the comment text.
2305 if (done)
2306 ;
2307 else if (theline[0] == '*')
2308 amount += 1;
2309 else
2310 {
2311 // If we are more than one line away from the comment opener, take
2312 // the indent of the previous non-empty line. If 'cino' has "CO"
2313 // and we are just below the comment opener and there are any
2314 // white characters after it line up with the text after it;
2315 // otherwise, add the amount specified by "c" in 'cino'
2316 amount = -1;
2317 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
2318 {
2319 if (linewhite(lnum)) // skip blank lines
2320 continue;
2321 amount = get_indent_lnum(lnum); // XXX
2322 break;
2323 }
2324 if (amount == -1) // use the comment opener
2325 {
2326 if (!curbuf->b_ind_in_comment2)
2327 {
2328 start = ml_get(comment_pos->lnum);
2329 look = start + comment_pos->col + 2; // skip / and *
2330 if (*look != NUL) // if something after it
2331 comment_pos->col = (colnr_T)(skipwhite(look) - start);
2332 }
2333 getvcol(curwin, comment_pos, &col, NULL, NULL);
2334 amount = col;
2335 if (curbuf->b_ind_in_comment2 || *look == NUL)
2336 amount += curbuf->b_ind_in_comment;
2337 }
2338 }
2339 goto theend;
2340 }
2341
2342 // Are we looking at a ']' that has a match?
2343 if (*skipwhite(theline) == ']'
2344 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
2345 {
2346 // align with the line containing the '['.
2347 amount = get_indent_lnum(trypos->lnum);
2348 goto theend;
2349 }
2350
2351 // Are we inside parentheses or braces? XXX
2352 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
2353 && curbuf->b_ind_java == 0)
2354 || (tryposBrace = find_start_brace()) != NULL
2355 || trypos != NULL)
2356 {
2357 if (trypos != NULL && tryposBrace != NULL)
2358 {
2359 // Both an unmatched '(' and '{' is found. Use the one which is
2360 // closer to the current cursor position, set the other to NULL.
2361 if (trypos->lnum != tryposBrace->lnum
2362 ? trypos->lnum < tryposBrace->lnum
2363 : trypos->col < tryposBrace->col)
2364 trypos = NULL;
2365 else
2366 tryposBrace = NULL;
2367 }
2368
2369 if (trypos != NULL)
2370 {
2371 // If the matching paren is more than one line away, use the indent of
2372 // a previous non-empty line that matches the same paren.
2373 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
2374 {
2375 // Line up with the start of the matching paren line.
2376 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX
2377 }
2378 else
2379 {
2380 amount = -1;
2381 our_paren_pos = *trypos;
2382 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
2383 {
2384 l = skipwhite(ml_get(lnum));
2385 if (cin_nocode(l)) // skip comment lines
2386 continue;
2387 if (cin_ispreproc_cont(&l, &lnum, &amount))
2388 continue; // ignore #define, #if, etc.
2389 curwin->w_cursor.lnum = lnum;
2390
2391 // Skip a comment or raw string. XXX
2392 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
2393 {
2394 lnum = trypos->lnum + 1;
2395 continue;
2396 }
2397
2398 // XXX
2399 if ((trypos = find_match_paren(
2400 corr_ind_maxparen(&cur_curpos))) != NULL
2401 && trypos->lnum == our_paren_pos.lnum
2402 && trypos->col == our_paren_pos.col)
2403 {
2404 amount = get_indent_lnum(lnum); // XXX
2405
2406 if (theline[0] == ')')
2407 {
2408 if (our_paren_pos.lnum != lnum
2409 && cur_amount > amount)
2410 cur_amount = amount;
2411 amount = -1;
2412 }
2413 break;
2414 }
2415 }
2416 }
2417
2418 // Line up with line where the matching paren is. XXX
2419 // If the line starts with a '(' or the indent for unclosed
2420 // parentheses is zero, line up with the unclosed parentheses.
2421 if (amount == -1)
2422 {
2423 int ignore_paren_col = 0;
2424 int is_if_for_while = 0;
2425
2426 if (curbuf->b_ind_if_for_while)
2427 {
2428 // Look for the outermost opening parenthesis on this line
2429 // and check whether it belongs to an "if", "for" or "while".
2430
2431 pos_T cursor_save = curwin->w_cursor;
2432 pos_T outermost;
2433 char_u *line;
2434
2435 trypos = &our_paren_pos;
2436 do {
2437 outermost = *trypos;
2438 curwin->w_cursor.lnum = outermost.lnum;
2439 curwin->w_cursor.col = outermost.col;
2440
2441 trypos = find_match_paren(curbuf->b_ind_maxparen);
2442 } while (trypos && trypos->lnum == outermost.lnum);
2443
2444 curwin->w_cursor = cursor_save;
2445
2446 line = ml_get(outermost.lnum);
2447
2448 is_if_for_while =
2449 cin_is_if_for_while_before_offset(line, &outermost.col);
2450 }
2451
2452 amount = skip_label(our_paren_pos.lnum, &look);
2453 look = skipwhite(look);
2454 if (*look == '(')
2455 {
2456 linenr_T save_lnum = curwin->w_cursor.lnum;
2457 char_u *line;
2458 int look_col;
2459
2460 // Ignore a '(' in front of the line that has a match before
2461 // our matching '('.
2462 curwin->w_cursor.lnum = our_paren_pos.lnum;
2463 line = ml_get_curline();
2464 look_col = (int)(look - line);
2465 curwin->w_cursor.col = look_col + 1;
2466 if ((trypos = findmatchlimit(NULL, ')', 0,
2467 curbuf->b_ind_maxparen))
2468 != NULL
2469 && trypos->lnum == our_paren_pos.lnum
2470 && trypos->col < our_paren_pos.col)
2471 ignore_paren_col = trypos->col + 1;
2472
2473 curwin->w_cursor.lnum = save_lnum;
2474 look = ml_get(our_paren_pos.lnum) + look_col;
2475 }
2476 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
2477 && is_if_for_while == 0)
2478 || (!curbuf->b_ind_unclosed_noignore && *look == '('
2479 && ignore_paren_col == 0))
2480 {
2481 // If we're looking at a close paren, line up right there;
2482 // otherwise, line up with the next (non-white) character.
2483 // When b_ind_unclosed_wrapped is set and the matching paren is
2484 // the last nonwhite character of the line, use either the
2485 // indent of the current line or the indentation of the next
2486 // outer paren and add b_ind_unclosed_wrapped (for very long
2487 // lines).
2488 if (theline[0] != ')')
2489 {
2490 cur_amount = MAXCOL;
2491 l = ml_get(our_paren_pos.lnum);
2492 if (curbuf->b_ind_unclosed_wrapped
2493 && cin_ends_in(l, (char_u *)"(", NULL))
2494 {
2495 // look for opening unmatched paren, indent one level
2496 // for each additional level
2497 n = 1;
2498 for (col = 0; col < our_paren_pos.col; ++col)
2499 {
2500 switch (l[col])
2501 {
2502 case '(':
2503 case '{': ++n;
2504 break;
2505
2506 case ')':
2507 case '}': if (n > 1)
2508 --n;
2509 break;
2510 }
2511 }
2512
2513 our_paren_pos.col = 0;
2514 amount += n * curbuf->b_ind_unclosed_wrapped;
2515 }
2516 else if (curbuf->b_ind_unclosed_whiteok)
2517 our_paren_pos.col++;
2518 else
2519 {
2520 col = our_paren_pos.col + 1;
2521 while (VIM_ISWHITE(l[col]))
2522 col++;
2523 if (l[col] != NUL) // In case of trailing space
2524 our_paren_pos.col = col;
2525 else
2526 our_paren_pos.col++;
2527 }
2528 }
2529
2530 // Find how indented the paren is, or the character after it
2531 // if we did the above "if".
2532 if (our_paren_pos.col > 0)
2533 {
2534 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
2535 if (cur_amount > (int)col)
2536 cur_amount = col;
2537 }
2538 }
2539
2540 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
2541 {
2542 // Line up with the start of the matching paren line.
2543 }
2544 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
2545 || (!curbuf->b_ind_unclosed_noignore
2546 && *look == '(' && ignore_paren_col == 0))
2547 {
2548 if (cur_amount != MAXCOL)
2549 amount = cur_amount;
2550 }
2551 else
2552 {
2553 // Add b_ind_unclosed2 for each '(' before our matching one,
2554 // but ignore (void) before the line (ignore_paren_col).
2555 col = our_paren_pos.col;
2556 while ((int)our_paren_pos.col > ignore_paren_col)
2557 {
2558 --our_paren_pos.col;
2559 switch (*ml_get_pos(&our_paren_pos))
2560 {
2561 case '(': amount += curbuf->b_ind_unclosed2;
2562 col = our_paren_pos.col;
2563 break;
2564 case ')': amount -= curbuf->b_ind_unclosed2;
2565 col = MAXCOL;
2566 break;
2567 }
2568 }
2569
2570 // Use b_ind_unclosed once, when the first '(' is not inside
2571 // braces
2572 if (col == MAXCOL)
2573 amount += curbuf->b_ind_unclosed;
2574 else
2575 {
2576 curwin->w_cursor.lnum = our_paren_pos.lnum;
2577 curwin->w_cursor.col = col;
2578 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
2579 != NULL)
2580 amount += curbuf->b_ind_unclosed2;
2581 else
2582 {
2583 if (is_if_for_while)
2584 amount += curbuf->b_ind_if_for_while;
2585 else
2586 amount += curbuf->b_ind_unclosed;
2587 }
2588 }
2589 // For a line starting with ')' use the minimum of the two
2590 // positions, to avoid giving it more indent than the previous
2591 // lines:
2592 // func_long_name( if (x
2593 // arg && yy
2594 // ) ^ not here ) ^ not here
2595 if (cur_amount < amount)
2596 amount = cur_amount;
2597 }
2598 }
2599
2600 // add extra indent for a comment
2601 if (cin_iscomment(theline))
2602 amount += curbuf->b_ind_comment;
2603 }
2604 else
2605 {
2606 // We are inside braces, there is a { before this line at the position
2607 // stored in tryposBrace.
2608 // Make a copy of tryposBrace, it may point to pos_copy inside
2609 // find_start_brace(), which may be changed somewhere.
2610 tryposCopy = *tryposBrace;
2611 tryposBrace = &tryposCopy;
2612 trypos = tryposBrace;
2613 ourscope = trypos->lnum;
2614 start = ml_get(ourscope);
2615
2616 // Now figure out how indented the line is in general.
2617 // If the brace was at the start of the line, we use that;
2618 // otherwise, check out the indentation of the line as
2619 // a whole and then add the "imaginary indent" to that.
2620 look = skipwhite(start);
2621 if (*look == '{')
2622 {
2623 getvcol(curwin, trypos, &col, NULL, NULL);
2624 amount = col;
2625 if (*start == '{')
2626 start_brace = BRACE_IN_COL0;
2627 else
2628 start_brace = BRACE_AT_START;
2629 }
2630 else
2631 {
2632 // That opening brace might have been on a continuation
2633 // line. if so, find the start of the line.
2634 curwin->w_cursor.lnum = ourscope;
2635
2636 // Position the cursor over the rightmost paren, so that
2637 // matching it will take us back to the start of the line.
2638 lnum = ourscope;
2639 if (find_last_paren(start, '(', ')')
2640 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
2641 != NULL)
2642 lnum = trypos->lnum;
2643
2644 // It could have been something like
2645 // case 1: if (asdf &&
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002646 // condition) {
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002647 // }
2648 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
2649 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
2650 amount = get_indent();
2651 else if (curbuf->b_ind_js)
2652 amount = get_indent_lnum(lnum);
2653 else
2654 amount = skip_label(lnum, &l);
2655
2656 start_brace = BRACE_AT_END;
2657 }
2658
2659 // For Javascript check if the line starts with "key:".
2660 if (curbuf->b_ind_js)
2661 js_cur_has_key = cin_has_js_key(theline);
2662
2663 // If we're looking at a closing brace, that's where
2664 // we want to be. otherwise, add the amount of room
2665 // that an indent is supposed to be.
2666 if (theline[0] == '}')
2667 {
2668 // they may want closing braces to line up with something
2669 // other than the open brace. indulge them, if so.
2670 amount += curbuf->b_ind_close_extra;
2671 }
2672 else
2673 {
2674 // If we're looking at an "else", try to find an "if"
2675 // to match it with.
2676 // If we're looking at a "while", try to find a "do"
2677 // to match it with.
2678 lookfor = LOOKFOR_INITIAL;
2679 if (cin_iselse(theline))
2680 lookfor = LOOKFOR_IF;
2681 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) // XXX
2682 lookfor = LOOKFOR_DO;
2683 if (lookfor != LOOKFOR_INITIAL)
2684 {
2685 curwin->w_cursor.lnum = cur_curpos.lnum;
2686 if (find_match(lookfor, ourscope) == OK)
2687 {
2688 amount = get_indent(); // XXX
2689 goto theend;
2690 }
2691 }
2692
2693 // We get here if we are not on an "while-of-do" or "else" (or
2694 // failed to find a matching "if").
2695 // Search backwards for something to line up with.
2696 // First set amount for when we don't find anything.
2697
2698 // if the '{' is _really_ at the left margin, use the imaginary
2699 // location of a left-margin brace. Otherwise, correct the
2700 // location for b_ind_open_extra.
2701
2702 if (start_brace == BRACE_IN_COL0) // '{' is in column 0
2703 {
2704 amount = curbuf->b_ind_open_left_imag;
2705 lookfor_cpp_namespace = TRUE;
2706 }
2707 else if (start_brace == BRACE_AT_START &&
2708 lookfor_cpp_namespace) // '{' is at start
2709 {
2710
2711 lookfor_cpp_namespace = TRUE;
2712 }
2713 else
2714 {
2715 if (start_brace == BRACE_AT_END) // '{' is at end of line
2716 {
2717 amount += curbuf->b_ind_open_imag;
2718
2719 l = skipwhite(ml_get_curline());
2720 if (cin_is_cpp_namespace(l))
2721 amount += curbuf->b_ind_cpp_namespace;
2722 else if (cin_is_cpp_extern_c(l))
2723 amount += curbuf->b_ind_cpp_extern_c;
2724 }
2725 else
2726 {
2727 // Compensate for adding b_ind_open_extra later.
2728 amount -= curbuf->b_ind_open_extra;
2729 if (amount < 0)
2730 amount = 0;
2731 }
2732 }
2733
2734 lookfor_break = FALSE;
2735
2736 if (cin_iscase(theline, FALSE)) // it's a switch() label
2737 {
2738 lookfor = LOOKFOR_CASE; // find a previous switch() label
2739 amount += curbuf->b_ind_case;
2740 }
2741 else if (cin_isscopedecl(theline)) // private:, ...
2742 {
2743 lookfor = LOOKFOR_SCOPEDECL; // class decl is this block
2744 amount += curbuf->b_ind_scopedecl;
2745 }
2746 else
2747 {
2748 if (curbuf->b_ind_case_break && cin_isbreak(theline))
2749 // break; ...
2750 lookfor_break = TRUE;
2751
2752 lookfor = LOOKFOR_INITIAL;
2753 // b_ind_level from start of block
2754 amount += curbuf->b_ind_level;
2755 }
2756 scope_amount = amount;
2757 whilelevel = 0;
2758
2759 // Search backwards. If we find something we recognize, line up
2760 // with that.
2761 //
2762 // If we're looking at an open brace, indent
2763 // the usual amount relative to the conditional
2764 // that opens the block.
2765 curwin->w_cursor = cur_curpos;
2766 for (;;)
2767 {
2768 curwin->w_cursor.lnum--;
2769 curwin->w_cursor.col = 0;
2770
2771 // If we went all the way back to the start of our scope, line
2772 // up with it.
2773 if (curwin->w_cursor.lnum <= ourscope)
2774 {
2775 // We reached end of scope:
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002776 // If looking for an enum or structure initialization
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002777 // go further back:
2778 // If it is an initializer (enum xxx or xxx =), then
2779 // don't add ind_continuation, otherwise it is a variable
2780 // declaration:
2781 // int x,
2782 // here; <-- add ind_continuation
2783 if (lookfor == LOOKFOR_ENUM_OR_INIT)
2784 {
2785 if (curwin->w_cursor.lnum == 0
2786 || curwin->w_cursor.lnum
2787 < ourscope - curbuf->b_ind_maxparen)
2788 {
2789 // nothing found (abuse curbuf->b_ind_maxparen as
2790 // limit) assume terminated line (i.e. a variable
2791 // initialization)
2792 if (cont_amount > 0)
2793 amount = cont_amount;
2794 else if (!curbuf->b_ind_js)
2795 amount += ind_continuation;
2796 break;
2797 }
2798
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002799 // If we're in a comment or raw string now, skip to
2800 // the start of it.
2801 trypos = ind_find_start_CORS(NULL);
2802 if (trypos != NULL)
2803 {
2804 curwin->w_cursor.lnum = trypos->lnum + 1;
2805 curwin->w_cursor.col = 0;
2806 continue;
2807 }
2808
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002809 l = ml_get_curline();
2810
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002811 // Skip preprocessor directives and blank lines.
2812 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
2813 &amount))
2814 continue;
2815
2816 if (cin_nocode(l))
2817 continue;
2818
2819 terminated = cin_isterminated(l, FALSE, TRUE);
2820
2821 // If we are at top level and the line looks like a
2822 // function declaration, we are done
2823 // (it's a variable declaration).
2824 if (start_brace != BRACE_IN_COL0
2825 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
2826 {
2827 // if the line is terminated with another ','
2828 // it is a continued variable initialization.
2829 // don't add extra indent.
2830 // TODO: does not work, if a function
2831 // declaration is split over multiple lines:
2832 // cin_isfuncdecl returns FALSE then.
2833 if (terminated == ',')
2834 break;
2835
Bram Moolenaar32aa1022019-11-02 22:54:41 +01002836 // if it is an enum declaration or an assignment,
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002837 // we are done.
2838 if (terminated != ';' && cin_isinit())
2839 break;
2840
2841 // nothing useful found
2842 if (terminated == 0 || terminated == '{')
2843 continue;
2844 }
2845
2846 if (terminated != ';')
2847 {
2848 // Skip parens and braces. Position the cursor
2849 // over the rightmost paren, so that matching it
2850 // will take us back to the start of the line.
2851 // XXX
2852 trypos = NULL;
2853 if (find_last_paren(l, '(', ')'))
2854 trypos = find_match_paren(
2855 curbuf->b_ind_maxparen);
2856
2857 if (trypos == NULL && find_last_paren(l, '{', '}'))
2858 trypos = find_start_brace();
2859
2860 if (trypos != NULL)
2861 {
2862 curwin->w_cursor.lnum = trypos->lnum + 1;
2863 curwin->w_cursor.col = 0;
2864 continue;
2865 }
2866 }
2867
2868 // it's a variable declaration, add indentation
2869 // like in
2870 // int a,
2871 // b;
2872 if (cont_amount > 0)
2873 amount = cont_amount;
2874 else
2875 amount += ind_continuation;
2876 }
2877 else if (lookfor == LOOKFOR_UNTERM)
2878 {
2879 if (cont_amount > 0)
2880 amount = cont_amount;
2881 else
2882 amount += ind_continuation;
2883 }
2884 else
2885 {
2886 if (lookfor != LOOKFOR_TERM
2887 && lookfor != LOOKFOR_CPP_BASECLASS
2888 && lookfor != LOOKFOR_COMMA)
2889 {
2890 amount = scope_amount;
2891 if (theline[0] == '{')
2892 {
2893 amount += curbuf->b_ind_open_extra;
2894 added_to_amount = curbuf->b_ind_open_extra;
2895 }
2896 }
2897
2898 if (lookfor_cpp_namespace)
2899 {
2900 // Looking for C++ namespace, need to look further
2901 // back.
2902 if (curwin->w_cursor.lnum == ourscope)
2903 continue;
2904
2905 if (curwin->w_cursor.lnum == 0
2906 || curwin->w_cursor.lnum
2907 < ourscope - FIND_NAMESPACE_LIM)
2908 break;
2909
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002910 // If we're in a comment or raw string now, skip
2911 // to the start of it.
2912 trypos = ind_find_start_CORS(NULL);
2913 if (trypos != NULL)
2914 {
2915 curwin->w_cursor.lnum = trypos->lnum + 1;
2916 curwin->w_cursor.col = 0;
2917 continue;
2918 }
2919
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01002920 l = ml_get_curline();
2921
Bram Moolenaar14c01f82019-10-09 22:53:08 +02002922 // Skip preprocessor directives and blank lines.
2923 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
2924 &amount))
2925 continue;
2926
2927 // Finally the actual check for "namespace".
2928 if (cin_is_cpp_namespace(l))
2929 {
2930 amount += curbuf->b_ind_cpp_namespace
2931 - added_to_amount;
2932 break;
2933 }
2934 else if (cin_is_cpp_extern_c(l))
2935 {
2936 amount += curbuf->b_ind_cpp_extern_c
2937 - added_to_amount;
2938 break;
2939 }
2940
2941 if (cin_nocode(l))
2942 continue;
2943 }
2944 }
2945 break;
2946 }
2947
2948 // If we're in a comment or raw string now, skip to the start
2949 // of it. XXX
2950 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
2951 {
2952 curwin->w_cursor.lnum = trypos->lnum + 1;
2953 curwin->w_cursor.col = 0;
2954 continue;
2955 }
2956
2957 l = ml_get_curline();
2958
2959 // If this is a switch() label, may line up relative to that.
2960 // If this is a C++ scope declaration, do the same.
2961 iscase = cin_iscase(l, FALSE);
2962 if (iscase || cin_isscopedecl(l))
2963 {
2964 // we are only looking for cpp base class
2965 // declaration/initialization any longer
2966 if (lookfor == LOOKFOR_CPP_BASECLASS)
2967 break;
2968
2969 // When looking for a "do" we are not interested in
2970 // labels.
2971 if (whilelevel > 0)
2972 continue;
2973
2974 // case xx:
2975 // c = 99 + <- this indent plus continuation
2976 //-> here;
2977 if (lookfor == LOOKFOR_UNTERM
2978 || lookfor == LOOKFOR_ENUM_OR_INIT)
2979 {
2980 if (cont_amount > 0)
2981 amount = cont_amount;
2982 else
2983 amount += ind_continuation;
2984 break;
2985 }
2986
2987 // case xx: <- line up with this case
2988 // x = 333;
2989 // case yy:
2990 if ( (iscase && lookfor == LOOKFOR_CASE)
2991 || (iscase && lookfor_break)
2992 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
2993 {
2994 // Check that this case label is not for another
2995 // switch() XXX
2996 if ((trypos = find_start_brace()) == NULL
2997 || trypos->lnum == ourscope)
2998 {
2999 amount = get_indent(); // XXX
3000 break;
3001 }
3002 continue;
3003 }
3004
3005 n = get_indent_nolabel(curwin->w_cursor.lnum); // XXX
3006
3007 // case xx: if (cond) <- line up with this if
3008 // y = y + 1;
3009 // -> s = 99;
3010 //
3011 // case xx:
3012 // if (cond) <- line up with this line
3013 // y = y + 1;
3014 // -> s = 99;
3015 if (lookfor == LOOKFOR_TERM)
3016 {
3017 if (n)
3018 amount = n;
3019
3020 if (!lookfor_break)
3021 break;
3022 }
3023
3024 // case xx: x = x + 1; <- line up with this x
3025 // -> y = y + 1;
3026 //
3027 // case xx: if (cond) <- line up with this if
3028 // -> y = y + 1;
3029 if (n)
3030 {
3031 amount = n;
3032 l = after_label(ml_get_curline());
3033 if (l != NULL && cin_is_cinword(l))
3034 {
3035 if (theline[0] == '{')
3036 amount += curbuf->b_ind_open_extra;
3037 else
3038 amount += curbuf->b_ind_level
3039 + curbuf->b_ind_no_brace;
3040 }
3041 break;
3042 }
3043
3044 // Try to get the indent of a statement before the switch
3045 // label. If nothing is found, line up relative to the
3046 // switch label.
3047 // break; <- may line up with this line
3048 // case xx:
3049 // -> y = 1;
3050 scope_amount = get_indent() + (iscase // XXX
3051 ? curbuf->b_ind_case_code
3052 : curbuf->b_ind_scopedecl_code);
3053 lookfor = curbuf->b_ind_case_break
3054 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
3055 continue;
3056 }
3057
3058 // Looking for a switch() label or C++ scope declaration,
3059 // ignore other lines, skip {}-blocks.
3060 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
3061 {
3062 if (find_last_paren(l, '{', '}')
3063 && (trypos = find_start_brace()) != NULL)
3064 {
3065 curwin->w_cursor.lnum = trypos->lnum + 1;
3066 curwin->w_cursor.col = 0;
3067 }
3068 continue;
3069 }
3070
3071 // Ignore jump labels with nothing after them.
3072 if (!curbuf->b_ind_js && cin_islabel())
3073 {
3074 l = after_label(ml_get_curline());
3075 if (l == NULL || cin_nocode(l))
3076 continue;
3077 }
3078
3079 // Ignore #defines, #if, etc.
3080 // Ignore comment and empty lines.
3081 // (need to get the line again, cin_islabel() may have
3082 // unlocked it)
3083 l = ml_get_curline();
3084 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
3085 || cin_nocode(l))
3086 continue;
3087
3088 // Are we at the start of a cpp base class declaration or
3089 // constructor initialization? XXX
3090 n = FALSE;
3091 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
3092 {
3093 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
3094 l = ml_get_curline();
3095 }
3096 if (n)
3097 {
3098 if (lookfor == LOOKFOR_UNTERM)
3099 {
3100 if (cont_amount > 0)
3101 amount = cont_amount;
3102 else
3103 amount += ind_continuation;
3104 }
3105 else if (theline[0] == '{')
3106 {
3107 // Need to find start of the declaration.
3108 lookfor = LOOKFOR_UNTERM;
3109 ind_continuation = 0;
3110 continue;
3111 }
3112 else
3113 // XXX
3114 amount = get_baseclass_amount(
3115 cache_cpp_baseclass.lpos.col);
3116 break;
3117 }
3118 else if (lookfor == LOOKFOR_CPP_BASECLASS)
3119 {
3120 // only look, whether there is a cpp base class
3121 // declaration or initialization before the opening brace.
3122 if (cin_isterminated(l, TRUE, FALSE))
3123 break;
3124 else
3125 continue;
3126 }
3127
3128 // What happens next depends on the line being terminated.
3129 // If terminated with a ',' only consider it terminating if
3130 // there is another unterminated statement behind, eg:
3131 // 123,
3132 // sizeof
3133 // here
Bram Moolenaar32aa1022019-11-02 22:54:41 +01003134 // Otherwise check whether it is an enumeration or structure
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003135 // initialisation (not indented) or a variable declaration
3136 // (indented).
3137 terminated = cin_isterminated(l, FALSE, TRUE);
3138
3139 if (js_cur_has_key)
3140 {
3141 js_cur_has_key = 0; // only check the first line
3142 if (curbuf->b_ind_js && terminated == ',')
3143 {
3144 // For Javascript we might be inside an object:
3145 // key: something, <- align with this
3146 // key: something
3147 // or:
3148 // key: something + <- align with this
3149 // something,
3150 // key: something
3151 lookfor = LOOKFOR_JS_KEY;
3152 }
3153 }
3154 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
3155 {
3156 amount = get_indent();
3157 break;
3158 }
3159 if (lookfor == LOOKFOR_COMMA)
3160 {
3161 if (tryposBrace != NULL && tryposBrace->lnum
3162 >= curwin->w_cursor.lnum)
3163 break;
3164 if (terminated == ',')
3165 // line below current line is the one that starts a
3166 // (possibly broken) line ending in a comma.
3167 break;
3168 else
3169 {
3170 amount = get_indent();
3171 if (curwin->w_cursor.lnum - 1 == ourscope)
3172 // line above is start of the scope, thus current
3173 // line is the one that stars a (possibly broken)
3174 // line ending in a comma.
3175 break;
3176 }
3177 }
3178
3179 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
3180 && terminated == ','))
3181 {
3182 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
3183 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
3184 amount += ind_continuation;
3185 // if we're in the middle of a paren thing,
3186 // go back to the line that starts it so
3187 // we can get the right prevailing indent
3188 // if ( foo &&
3189 // bar )
3190
3191 // Position the cursor over the rightmost paren, so that
3192 // matching it will take us back to the start of the line.
3193 // Ignore a match before the start of the block.
3194 (void)find_last_paren(l, '(', ')');
3195 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
3196 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
3197 || (trypos->lnum == tryposBrace->lnum
3198 && trypos->col < tryposBrace->col)))
3199 trypos = NULL;
3200
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01003201 l = ml_get_curline();
3202
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003203 // If we are looking for ',', we also look for matching
3204 // braces.
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01003205 if (trypos == NULL && terminated == ',')
3206 {
3207 if (find_last_paren(l, '{', '}'))
3208 trypos = find_start_brace();
3209 l = ml_get_curline();
3210 }
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003211
3212 if (trypos != NULL)
3213 {
3214 // Check if we are on a case label now. This is
3215 // handled above.
3216 // case xx: if ( asdf &&
3217 // asdf)
3218 curwin->w_cursor = *trypos;
3219 l = ml_get_curline();
3220 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
3221 {
3222 ++curwin->w_cursor.lnum;
3223 curwin->w_cursor.col = 0;
3224 continue;
3225 }
3226 }
3227
3228 // Skip over continuation lines to find the one to get the
3229 // indent from
3230 // char *usethis = "bla{backslash}
3231 // bla",
3232 // here;
3233 if (terminated == ',')
3234 {
3235 while (curwin->w_cursor.lnum > 1)
3236 {
3237 l = ml_get(curwin->w_cursor.lnum - 1);
3238 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
3239 break;
3240 --curwin->w_cursor.lnum;
3241 curwin->w_cursor.col = 0;
3242 }
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01003243 l = ml_get_curline();
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003244 }
3245
3246 // Get indent and pointer to text for current line,
3247 // ignoring any jump label. XXX
3248 if (curbuf->b_ind_js)
3249 cur_amount = get_indent();
3250 else
3251 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
3252 // If this is just above the line we are indenting, and it
3253 // starts with a '{', line it up with this line.
3254 // while (not)
3255 // -> {
3256 // }
3257 if (terminated != ',' && lookfor != LOOKFOR_TERM
3258 && theline[0] == '{')
3259 {
3260 amount = cur_amount;
3261 // Only add b_ind_open_extra when the current line
3262 // doesn't start with a '{', which must have a match
3263 // in the same line (scope is the same). Probably:
3264 // { 1, 2 },
3265 // -> { 3, 4 }
3266 if (*skipwhite(l) != '{')
3267 amount += curbuf->b_ind_open_extra;
3268
3269 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
3270 {
3271 // have to look back, whether it is a cpp base
3272 // class declaration or initialization
3273 lookfor = LOOKFOR_CPP_BASECLASS;
3274 continue;
3275 }
3276 break;
3277 }
3278
3279 // Check if we are after an "if", "while", etc.
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003280 // Also allow "} else".
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003281 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
3282 {
3283 // Found an unterminated line after an if (), line up
3284 // with the last one.
3285 // if (cond)
3286 // 100 +
3287 // -> here;
3288 if (lookfor == LOOKFOR_UNTERM
3289 || lookfor == LOOKFOR_ENUM_OR_INIT)
3290 {
3291 if (cont_amount > 0)
3292 amount = cont_amount;
3293 else
3294 amount += ind_continuation;
3295 break;
3296 }
3297
3298 // If this is just above the line we are indenting, we
3299 // are finished.
3300 // while (not)
3301 // -> here;
3302 // Otherwise this indent can be used when the line
3303 // before this is terminated.
3304 // yyy;
3305 // if (stat)
3306 // while (not)
3307 // xxx;
3308 // -> here;
3309 amount = cur_amount;
3310 if (theline[0] == '{')
3311 amount += curbuf->b_ind_open_extra;
3312 if (lookfor != LOOKFOR_TERM)
3313 {
3314 amount += curbuf->b_ind_level
3315 + curbuf->b_ind_no_brace;
3316 break;
3317 }
3318
3319 // Special trick: when expecting the while () after a
Bram Moolenaarc9471b12023-05-09 15:00:00 +01003320 // do, line up with the while ()
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003321 // do
3322 // x = 1;
3323 // -> here
3324 l = skipwhite(ml_get_curline());
3325 if (cin_isdo(l))
3326 {
3327 if (whilelevel == 0)
3328 break;
3329 --whilelevel;
3330 }
3331
3332 // When searching for a terminated line, don't use the
3333 // one between the "if" and the matching "else".
3334 // Need to use the scope of this "else". XXX
3335 // If whilelevel != 0 continue looking for a "do {".
3336 if (cin_iselse(l) && whilelevel == 0)
3337 {
3338 // If we're looking at "} else", let's make sure we
3339 // find the opening brace of the enclosing scope,
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003340 // not the one from "if (condition) {".
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003341 if (*l == '}')
3342 curwin->w_cursor.col =
3343 (colnr_T)(l - ml_get_curline()) + 1;
3344
3345 if ((trypos = find_start_brace()) == NULL
3346 || find_match(LOOKFOR_IF, trypos->lnum)
3347 == FAIL)
3348 break;
3349 }
3350 }
3351
3352 // If we're below an unterminated line that is not an
3353 // "if" or something, we may line up with this line or
3354 // add something for a continuation line, depending on
3355 // the line before this one.
3356 else
3357 {
3358 // Found two unterminated lines on a row, line up with
3359 // the last one.
3360 // c = 99 +
3361 // 100 +
3362 // -> here;
3363 if (lookfor == LOOKFOR_UNTERM)
3364 {
3365 // When line ends in a comma add extra indent
3366 if (terminated == ',')
3367 amount += ind_continuation;
3368 break;
3369 }
3370
3371 if (lookfor == LOOKFOR_ENUM_OR_INIT)
3372 {
3373 // Found two lines ending in ',', lineup with the
3374 // lowest one, but check for cpp base class
3375 // declaration/initialization, if it is an
3376 // opening brace or we are looking just for
3377 // enumerations/initializations.
3378 if (terminated == ',')
3379 {
3380 if (curbuf->b_ind_cpp_baseclass == 0)
3381 break;
3382
3383 lookfor = LOOKFOR_CPP_BASECLASS;
3384 continue;
3385 }
3386
3387 // Ignore unterminated lines in between, but
3388 // reduce indent.
3389 if (amount > cur_amount)
3390 amount = cur_amount;
3391 }
3392 else
3393 {
3394 // Found first unterminated line on a row, may
3395 // line up with this line, remember its indent
3396 // 100 +
3397 // -> here;
3398 l = ml_get_curline();
3399 amount = cur_amount;
3400
3401 n = (int)STRLEN(l);
3402 if (terminated == ',' && (*skipwhite(l) == ']'
3403 || (n >=2 && l[n - 2] == ']')))
3404 break;
3405
3406 // If previous line ends in ',', check whether we
3407 // are in an initialization or enum
3408 // struct xxx =
3409 // {
3410 // sizeof a,
3411 // 124 };
3412 // or a normal possible continuation line.
3413 // but only, of no other statement has been found
3414 // yet.
3415 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
3416 {
3417 if (curbuf->b_ind_js)
3418 {
3419 // Search for a line ending in a comma
3420 // and line up with the line below it
3421 // (could be the current line).
3422 // some = [
3423 // 1, <- line up here
3424 // 2,
3425 // some = [
3426 // 3 + <- line up here
3427 // 4 *
3428 // 5,
3429 // 6,
3430 if (cin_iscomment(skipwhite(l)))
3431 break;
3432 lookfor = LOOKFOR_COMMA;
3433 trypos = find_match_char('[',
3434 curbuf->b_ind_maxparen);
3435 if (trypos != NULL)
3436 {
3437 if (trypos->lnum
3438 == curwin->w_cursor.lnum - 1)
3439 {
3440 // Current line is first inside
3441 // [], line up with it.
3442 break;
3443 }
3444 ourscope = trypos->lnum;
3445 }
3446 }
3447 else
3448 {
3449 lookfor = LOOKFOR_ENUM_OR_INIT;
3450 cont_amount = cin_first_id_amount();
3451 }
3452 }
3453 else
3454 {
3455 if (lookfor == LOOKFOR_INITIAL
3456 && *l != NUL
3457 && l[STRLEN(l) - 1] == '\\')
3458 // XXX
3459 cont_amount = cin_get_equal_amount(
3460 curwin->w_cursor.lnum);
3461 if (lookfor != LOOKFOR_TERM
3462 && lookfor != LOOKFOR_JS_KEY
3463 && lookfor != LOOKFOR_COMMA
3464 && raw_string_start != curwin->w_cursor.lnum)
3465 lookfor = LOOKFOR_UNTERM;
3466 }
3467 }
3468 }
3469 }
3470
3471 // Check if we are after a while (cond);
3472 // If so: Ignore until the matching "do".
3473 else if (cin_iswhileofdo_end(terminated)) // XXX
3474 {
3475 // Found an unterminated line after a while ();, line up
3476 // with the last one.
3477 // while (cond);
3478 // 100 + <- line up with this one
3479 // -> here;
3480 if (lookfor == LOOKFOR_UNTERM
3481 || lookfor == LOOKFOR_ENUM_OR_INIT)
3482 {
3483 if (cont_amount > 0)
3484 amount = cont_amount;
3485 else
3486 amount += ind_continuation;
3487 break;
3488 }
3489
3490 if (whilelevel == 0)
3491 {
3492 lookfor = LOOKFOR_TERM;
3493 amount = get_indent(); // XXX
3494 if (theline[0] == '{')
3495 amount += curbuf->b_ind_open_extra;
3496 }
3497 ++whilelevel;
3498 }
3499
3500 // We are after a "normal" statement.
3501 // If we had another statement we can stop now and use the
3502 // indent of that other statement.
3503 // Otherwise the indent of the current statement may be used,
3504 // search backwards for the next "normal" statement.
3505 else
3506 {
3507 // Skip single break line, if before a switch label. It
3508 // may be lined up with the case label.
3509 if (lookfor == LOOKFOR_NOBREAK
3510 && cin_isbreak(skipwhite(ml_get_curline())))
3511 {
3512 lookfor = LOOKFOR_ANY;
3513 continue;
3514 }
3515
3516 // Handle "do {" line.
3517 if (whilelevel > 0)
3518 {
3519 l = cin_skipcomment(ml_get_curline());
3520 if (cin_isdo(l))
3521 {
3522 amount = get_indent(); // XXX
3523 --whilelevel;
3524 continue;
3525 }
3526 }
3527
3528 // Found a terminated line above an unterminated line. Add
3529 // the amount for a continuation line.
3530 // x = 1;
3531 // y = foo +
3532 // -> here;
3533 // or
3534 // int x = 1;
3535 // int foo,
3536 // -> here;
3537 if (lookfor == LOOKFOR_UNTERM
3538 || lookfor == LOOKFOR_ENUM_OR_INIT)
3539 {
3540 if (cont_amount > 0)
3541 amount = cont_amount;
3542 else
3543 amount += ind_continuation;
3544 break;
3545 }
3546
3547 // Found a terminated line above a terminated line or "if"
3548 // etc. line. Use the amount of the line below us.
3549 // x = 1; x = 1;
3550 // if (asdf) y = 2;
3551 // while (asdf) ->here;
3552 // here;
3553 // ->foo;
3554 if (lookfor == LOOKFOR_TERM)
3555 {
3556 if (!lookfor_break && whilelevel == 0)
3557 break;
3558 }
3559
3560 // First line above the one we're indenting is terminated.
3561 // To know what needs to be done look further backward for
3562 // a terminated line.
3563 else
3564 {
3565 // position the cursor over the rightmost paren, so
3566 // that matching it will take us back to the start of
3567 // the line. Helps for:
3568 // func(asdr,
3569 // asdfasdf);
3570 // here;
3571term_again:
3572 l = ml_get_curline();
3573 if (find_last_paren(l, '(', ')')
3574 && (trypos = find_match_paren(
3575 curbuf->b_ind_maxparen)) != NULL)
3576 {
3577 // Check if we are on a case label now. This is
3578 // handled above.
3579 // case xx: if ( asdf &&
3580 // asdf)
3581 curwin->w_cursor = *trypos;
3582 l = ml_get_curline();
3583 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
3584 {
3585 ++curwin->w_cursor.lnum;
3586 curwin->w_cursor.col = 0;
3587 continue;
3588 }
3589 }
3590
3591 // When aligning with the case statement, don't align
3592 // with a statement after it.
3593 // case 1: { <-- don't use this { position
3594 // stat;
3595 // }
3596 // case 2:
3597 // stat;
3598 // }
3599 iscase = (curbuf->b_ind_keep_case_label
3600 && cin_iscase(l, FALSE));
3601
3602 // Get indent and pointer to text for current line,
3603 // ignoring any jump label.
3604 amount = skip_label(curwin->w_cursor.lnum, &l);
3605
3606 if (theline[0] == '{')
3607 amount += curbuf->b_ind_open_extra;
3608 // See remark above: "Only add b_ind_open_extra.."
3609 l = skipwhite(l);
3610 if (*l == '{')
3611 amount -= curbuf->b_ind_open_extra;
3612 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
3613
3614 // When a terminated line starts with "else" skip to
3615 // the matching "if":
3616 // else 3;
3617 // indent this;
3618 // Need to use the scope of this "else". XXX
3619 // If whilelevel != 0 continue looking for a "do {".
3620 if (lookfor == LOOKFOR_TERM
3621 && *l != '}'
3622 && cin_iselse(l)
3623 && whilelevel == 0)
3624 {
3625 if ((trypos = find_start_brace()) == NULL
3626 || find_match(LOOKFOR_IF, trypos->lnum)
3627 == FAIL)
3628 break;
3629 continue;
3630 }
3631
3632 // If we're at the end of a block, skip to the start of
3633 // that block.
3634 l = ml_get_curline();
3635 if (find_last_paren(l, '{', '}') // XXX
3636 && (trypos = find_start_brace()) != NULL)
3637 {
3638 curwin->w_cursor = *trypos;
3639 // if not "else {" check for terminated again
3640 // but skip block for "} else {"
3641 l = cin_skipcomment(ml_get_curline());
3642 if (*l == '}' || !cin_iselse(l))
3643 goto term_again;
3644 ++curwin->w_cursor.lnum;
3645 curwin->w_cursor.col = 0;
3646 }
3647 }
3648 }
3649 }
3650 }
3651 }
3652
3653 // add extra indent for a comment
3654 if (cin_iscomment(theline))
3655 amount += curbuf->b_ind_comment;
3656
3657 // subtract extra left-shift for jump labels
3658 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
3659 amount -= curbuf->b_ind_jump_label;
3660
3661 goto theend;
3662 }
3663
3664 // ok -- we're not inside any sort of structure at all!
3665 //
3666 // This means we're at the top level, and everything should
3667 // basically just match where the previous line is, except
3668 // for the lines immediately following a function declaration,
3669 // which are K&R-style parameters and need to be indented.
3670 //
3671 // if our line starts with an open brace, forget about any
3672 // prevailing indent and make sure it looks like the start
3673 // of a function
3674
3675 if (theline[0] == '{')
3676 {
3677 amount = curbuf->b_ind_first_open;
3678 goto theend;
3679 }
3680
3681 // If the NEXT line is a function declaration, the current
3682 // line needs to be indented as a function type spec.
3683 // Don't do this if the current line looks like a comment or if the
3684 // current line is terminated, ie. ends in ';', or if the current line
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00003685 // contains { or }: "void f(condition) {\n if (1)"
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003686 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
3687 && !cin_nocode(theline)
3688 && vim_strchr(theline, '{') == NULL
3689 && vim_strchr(theline, '}') == NULL
3690 && !cin_ends_in(theline, (char_u *)":", NULL)
3691 && !cin_ends_in(theline, (char_u *)",", NULL)
3692 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
3693 cur_curpos.lnum + 1)
3694 && !cin_isterminated(theline, FALSE, TRUE))
3695 {
3696 amount = curbuf->b_ind_func_type;
3697 goto theend;
3698 }
3699
3700 // search backwards until we find something we recognize
3701 amount = 0;
3702 curwin->w_cursor = cur_curpos;
3703 while (curwin->w_cursor.lnum > 1)
3704 {
3705 curwin->w_cursor.lnum--;
3706 curwin->w_cursor.col = 0;
3707
3708 l = ml_get_curline();
3709
3710 // If we're in a comment or raw string now, skip to the start
3711 // of it. XXX
3712 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
3713 {
3714 curwin->w_cursor.lnum = trypos->lnum + 1;
3715 curwin->w_cursor.col = 0;
3716 continue;
3717 }
3718
3719 // Are we at the start of a cpp base class declaration or
3720 // constructor initialization? XXX
3721 n = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01003722 if (curbuf->b_ind_cpp_baseclass != 0)
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003723 {
3724 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
3725 l = ml_get_curline();
3726 }
3727 if (n)
3728 {
3729 // XXX
3730 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
3731 break;
3732 }
3733
3734 // Skip preprocessor directives and blank lines.
3735 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
3736 continue;
3737
3738 if (cin_nocode(l))
3739 continue;
3740
3741 // If the previous line ends in ',', use one level of
3742 // indentation:
3743 // int foo,
3744 // bar;
3745 // do this before checking for '}' in case of eg.
3746 // enum foobar
3747 // {
3748 // ...
3749 // } foo,
3750 // bar;
3751 n = 0;
3752 if (cin_ends_in(l, (char_u *)",", NULL)
3753 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
3754 {
3755 // take us back to opening paren
3756 if (find_last_paren(l, '(', ')')
3757 && (trypos = find_match_paren(
3758 curbuf->b_ind_maxparen)) != NULL)
3759 curwin->w_cursor = *trypos;
3760
3761 // For a line ending in ',' that is a continuation line go
3762 // back to the first line with a backslash:
3763 // char *foo = "bla{backslash}
3764 // bla",
3765 // here;
3766 while (n == 0 && curwin->w_cursor.lnum > 1)
3767 {
3768 l = ml_get(curwin->w_cursor.lnum - 1);
3769 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
3770 break;
3771 --curwin->w_cursor.lnum;
3772 curwin->w_cursor.col = 0;
3773 }
3774
3775 amount = get_indent(); // XXX
3776
3777 if (amount == 0)
3778 amount = cin_first_id_amount();
3779 if (amount == 0)
3780 amount = ind_continuation;
3781 break;
3782 }
3783
3784 // If the line looks like a function declaration, and we're
3785 // not in a comment, put it the left margin.
3786 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) // XXX
3787 break;
3788 l = ml_get_curline();
3789
3790 // Finding the closing '}' of a previous function. Put
3791 // current line at the left margin. For when 'cino' has "fs".
3792 if (*skipwhite(l) == '}')
3793 break;
3794
3795 // (matching {)
3796 // If the previous line ends on '};' (maybe followed by
3797 // comments) align at column 0. For example:
3798 // char *string_array[] = { "foo",
3799 // / * x * / "b};ar" }; / * foobar * /
3800 if (cin_ends_in(l, (char_u *)"};", NULL))
3801 break;
3802
3803 // If the previous line ends on '[' we are probably in an
3804 // array constant:
3805 // something = [
3806 // 234, <- extra indent
3807 if (cin_ends_in(l, (char_u *)"[", NULL))
3808 {
3809 amount = get_indent() + ind_continuation;
3810 break;
3811 }
3812
3813 // Find a line only has a semicolon that belongs to a previous
3814 // line ending in '}', e.g. before an #endif. Don't increase
3815 // indent then.
3816 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
3817 {
3818 pos_T curpos_save = curwin->w_cursor;
3819
3820 while (curwin->w_cursor.lnum > 1)
3821 {
3822 look = ml_get(--curwin->w_cursor.lnum);
3823 if (!(cin_nocode(look) || cin_ispreproc_cont(
3824 &look, &curwin->w_cursor.lnum, &amount)))
3825 break;
3826 }
3827 if (curwin->w_cursor.lnum > 0
3828 && cin_ends_in(look, (char_u *)"}", NULL))
3829 break;
3830
3831 curwin->w_cursor = curpos_save;
3832 }
3833
3834 // If the PREVIOUS line is a function declaration, the current
3835 // line (and the ones that follow) needs to be indented as
3836 // parameters.
3837 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
3838 {
3839 amount = curbuf->b_ind_param;
3840 break;
3841 }
3842
3843 // If the previous line ends in ';' and the line before the
3844 // previous line ends in ',' or '\', ident to column zero:
3845 // int foo,
3846 // bar;
3847 // indent_to_0 here;
3848 if (cin_ends_in(l, (char_u *)";", NULL))
3849 {
3850 l = ml_get(curwin->w_cursor.lnum - 1);
3851 if (cin_ends_in(l, (char_u *)",", NULL)
3852 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
3853 break;
3854 l = ml_get_curline();
3855 }
3856
3857 // Doesn't look like anything interesting -- so just
3858 // use the indent of this line.
3859 //
3860 // Position the cursor over the rightmost paren, so that
3861 // matching it will take us back to the start of the line.
3862 find_last_paren(l, '(', ')');
3863
3864 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
3865 curwin->w_cursor = *trypos;
3866 amount = get_indent(); // XXX
3867 break;
3868 }
3869
3870 // add extra indent for a comment
3871 if (cin_iscomment(theline))
3872 amount += curbuf->b_ind_comment;
3873
3874 // add extra indent if the previous line ended in a backslash:
3875 // "asdfasdf{backslash}
3876 // here";
3877 // char *foo = "asdf{backslash}
3878 // here";
3879 if (cur_curpos.lnum > 1)
3880 {
3881 l = ml_get(cur_curpos.lnum - 1);
3882 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
3883 {
3884 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
3885 if (cur_amount > 0)
3886 amount = cur_amount;
3887 else if (cur_amount == 0)
3888 amount += ind_continuation;
3889 }
3890 }
3891
3892theend:
3893 if (amount < 0)
3894 amount = 0;
3895
3896laterend:
3897 // put the cursor back where it belongs
3898 curwin->w_cursor = cur_curpos;
3899
3900 vim_free(linecopy);
3901
3902 return amount;
3903}
3904
3905/*
3906 * return TRUE if 'cinkeys' contains the key "keytyped",
3907 * when == '*': Only if key is preceded with '*' (indent before insert)
3908 * when == '!': Only if key is preceded with '!' (don't insert)
3909 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
3910 *
3911 * "keytyped" can have a few special values:
3912 * KEY_OPEN_FORW
3913 * KEY_OPEN_BACK
3914 * KEY_COMPLETE just finished completion.
3915 *
3916 * If line_is_empty is TRUE accept keys with '0' before them.
3917 */
3918 int
3919in_cinkeys(
3920 int keytyped,
3921 int when,
3922 int line_is_empty)
3923{
3924 char_u *look;
3925 int try_match;
3926 int try_match_word;
3927 char_u *p;
3928 char_u *line;
3929 int icase;
3930 int i;
3931
3932 if (keytyped == NUL)
3933 // Can happen with CTRL-Y and CTRL-E on a short line.
3934 return FALSE;
3935
3936#ifdef FEAT_EVAL
3937 if (*curbuf->b_p_inde != NUL)
3938 look = curbuf->b_p_indk; // 'indentexpr' set: use 'indentkeys'
3939 else
3940#endif
3941 look = curbuf->b_p_cink; // 'indentexpr' empty: use 'cinkeys'
3942 while (*look)
3943 {
3944 // Find out if we want to try a match with this key, depending on
3945 // 'when' and a '*' or '!' before the key.
3946 switch (when)
3947 {
3948 case '*': try_match = (*look == '*'); break;
3949 case '!': try_match = (*look == '!'); break;
3950 default: try_match = (*look != '*'); break;
3951 }
3952 if (*look == '*' || *look == '!')
3953 ++look;
3954
3955 // If there is a '0', only accept a match if the line is empty.
3956 // But may still match when typing last char of a word.
3957 if (*look == '0')
3958 {
3959 try_match_word = try_match;
3960 if (!line_is_empty)
3961 try_match = FALSE;
3962 ++look;
3963 }
3964 else
3965 try_match_word = FALSE;
3966
3967 // does it look like a control character?
Bram Moolenaar424bcae2022-01-31 14:59:41 +00003968 if (*look == '^' && look[1] >= '?' && look[1] <= '_')
Bram Moolenaar14c01f82019-10-09 22:53:08 +02003969 {
3970 if (try_match && keytyped == Ctrl_chr(look[1]))
3971 return TRUE;
3972 look += 2;
3973 }
3974 // 'o' means "o" command, open forward.
3975 // 'O' means "O" command, open backward.
3976 else if (*look == 'o')
3977 {
3978 if (try_match && keytyped == KEY_OPEN_FORW)
3979 return TRUE;
3980 ++look;
3981 }
3982 else if (*look == 'O')
3983 {
3984 if (try_match && keytyped == KEY_OPEN_BACK)
3985 return TRUE;
3986 ++look;
3987 }
3988
3989 // 'e' means to check for "else" at start of line and just before the
3990 // cursor.
3991 else if (*look == 'e')
3992 {
3993 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
3994 {
3995 p = ml_get_curline();
3996 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
3997 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
3998 return TRUE;
3999 }
4000 ++look;
4001 }
4002
4003 // ':' only causes an indent if it is at the end of a label or case
4004 // statement, or when it was before typing the ':' (to fix
4005 // class::method for C++).
4006 else if (*look == ':')
4007 {
4008 if (try_match && keytyped == ':')
4009 {
4010 p = ml_get_curline();
4011 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
4012 return TRUE;
4013 // Need to get the line again after cin_islabel().
4014 p = ml_get_curline();
4015 if (curwin->w_cursor.col > 2
4016 && p[curwin->w_cursor.col - 1] == ':'
4017 && p[curwin->w_cursor.col - 2] == ':')
4018 {
4019 p[curwin->w_cursor.col - 1] = ' ';
4020 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
4021 || cin_islabel());
4022 p = ml_get_curline();
4023 p[curwin->w_cursor.col - 1] = ':';
4024 if (i)
4025 return TRUE;
4026 }
4027 }
4028 ++look;
4029 }
4030
4031
4032 // Is it a key in <>, maybe?
4033 else if (*look == '<')
4034 {
4035 if (try_match)
4036 {
4037 // make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
4038 // <:> and <!> so that people can re-indent on o, O, e, 0, <,
4039 // >, *, : and ! keys if they really really want to.
4040 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
4041 && keytyped == look[1])
4042 return TRUE;
4043
4044 if (keytyped == get_special_key_code(look + 1))
4045 return TRUE;
4046 }
4047 while (*look && *look != '>')
4048 look++;
4049 while (*look == '>')
4050 look++;
4051 }
4052
4053 // Is it a word: "=word"?
4054 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
4055 {
4056 ++look;
4057 if (*look == '~')
4058 {
4059 icase = TRUE;
4060 ++look;
4061 }
4062 else
4063 icase = FALSE;
4064 p = vim_strchr(look, ',');
4065 if (p == NULL)
4066 p = look + STRLEN(look);
4067 if ((try_match || try_match_word)
4068 && curwin->w_cursor.col >= (colnr_T)(p - look))
4069 {
4070 int match = FALSE;
4071
4072 if (keytyped == KEY_COMPLETE)
4073 {
4074 char_u *s;
4075
4076 // Just completed a word, check if it starts with "look".
4077 // search back for the start of a word.
4078 line = ml_get_curline();
4079 if (has_mbyte)
4080 {
4081 char_u *n;
4082
4083 for (s = line + curwin->w_cursor.col; s > line; s = n)
4084 {
4085 n = mb_prevptr(line, s);
4086 if (!vim_iswordp(n))
4087 break;
4088 }
4089 }
4090 else
4091 for (s = line + curwin->w_cursor.col; s > line; --s)
4092 if (!vim_iswordc(s[-1]))
4093 break;
4094 if (s + (p - look) <= line + curwin->w_cursor.col
4095 && (icase
4096 ? MB_STRNICMP(s, look, p - look)
4097 : STRNCMP(s, look, p - look)) == 0)
4098 match = TRUE;
4099 }
4100 else
4101 // TODO: multi-byte
4102 if (keytyped == (int)p[-1] || (icase && keytyped < 256
4103 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
4104 {
4105 line = ml_get_cursor();
4106 if ((curwin->w_cursor.col == (colnr_T)(p - look)
4107 || !vim_iswordc(line[-(p - look) - 1]))
4108 && (icase
4109 ? MB_STRNICMP(line - (p - look), look, p - look)
4110 : STRNCMP(line - (p - look), look, p - look))
4111 == 0)
4112 match = TRUE;
4113 }
4114 if (match && try_match_word && !try_match)
4115 {
4116 // "0=word": Check if there are only blanks before the
4117 // word.
4118 if (getwhitecols_curline() !=
4119 (int)(curwin->w_cursor.col - (p - look)))
4120 match = FALSE;
4121 }
4122 if (match)
4123 return TRUE;
4124 }
4125 look = p;
4126 }
4127
4128 // ok, it's a boring generic character.
4129 else
4130 {
4131 if (try_match && *look == keytyped)
4132 return TRUE;
4133 if (*look != NUL)
4134 ++look;
4135 }
4136
4137 // Skip over ", ".
4138 look = skip_to_option_part(look);
4139 }
4140 return FALSE;
4141}
4142
4143/*
4144 * Do C or expression indenting on the current line.
4145 */
4146 void
4147do_c_expr_indent(void)
4148{
K.Takata161b6ac2022-11-14 15:31:07 +00004149#ifdef FEAT_EVAL
Bram Moolenaar14c01f82019-10-09 22:53:08 +02004150 if (*curbuf->b_p_inde != NUL)
4151 fixthisline(get_expr_indent);
4152 else
K.Takata161b6ac2022-11-14 15:31:07 +00004153#endif
Bram Moolenaar14c01f82019-10-09 22:53:08 +02004154 fixthisline(get_c_indent);
4155}
Bram Moolenaar14c01f82019-10-09 22:53:08 +02004156
4157#if defined(FEAT_EVAL) || defined(PROTO)
4158/*
4159 * "cindent(lnum)" function
4160 */
4161 void
4162f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
4163{
Bram Moolenaar14c01f82019-10-09 22:53:08 +02004164 pos_T pos;
4165 linenr_T lnum;
4166
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004167 if (in_vim9script() && check_for_lnum_arg(argvars, 0) == FAIL)
4168 return;
4169
Bram Moolenaar14c01f82019-10-09 22:53:08 +02004170 pos = curwin->w_cursor;
4171 lnum = tv_get_lnum(argvars);
4172 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4173 {
4174 curwin->w_cursor.lnum = lnum;
4175 rettv->vval.v_number = get_c_indent();
4176 curwin->w_cursor = pos;
4177 }
4178 else
Bram Moolenaar14c01f82019-10-09 22:53:08 +02004179 rettv->vval.v_number = -1;
4180}
4181#endif