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