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