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