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