blob: dacafcbc0236a2ccd9c93e215402add470151c3a [file] [log] [blame]
Bram Moolenaar4b471622019-01-31 13:48:09 +01001/* 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 * indent.c: Indentation related functions
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
17
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020018static int cin_iscase(char_u *s, int strict);
19static int cin_isscopedecl(char_u *s);
20
Bram Moolenaar4b471622019-01-31 13:48:09 +010021/*
22 * Return TRUE if the string "line" starts with a word from 'cinwords'.
23 */
24 int
25cin_is_cinword(char_u *line)
26{
27 char_u *cinw;
28 char_u *cinw_buf;
29 int cinw_len;
30 int retval = FALSE;
31 int len;
32
33 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
Bram Moolenaar964b3742019-05-24 18:54:09 +020034 cinw_buf = alloc(cinw_len);
Bram Moolenaar4b471622019-01-31 13:48:09 +010035 if (cinw_buf != NULL)
36 {
37 line = skipwhite(line);
38 for (cinw = curbuf->b_p_cinw; *cinw; )
39 {
40 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
41 if (STRNCMP(line, cinw_buf, len) == 0
42 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
43 {
44 retval = TRUE;
45 break;
46 }
47 }
48 vim_free(cinw_buf);
49 }
50 return retval;
51}
52#endif
53
54#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
55
56static char_u *skip_string(char_u *p);
57static pos_T *find_start_rawstring(int ind_maxcomment);
58
59/*
60 * Find the start of a comment, not knowing if we are in a comment right now.
61 * Search starts at w_cursor.lnum and goes backwards.
62 * Return NULL when not inside a comment.
63 */
64 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +010065ind_find_start_comment(void) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +010066{
67 return find_start_comment(curbuf->b_ind_maxcomment);
68}
69
70 pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +010071find_start_comment(int ind_maxcomment) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +010072{
73 pos_T *pos;
74 char_u *line;
75 char_u *p;
76 int cur_maxcomment = ind_maxcomment;
77
78 for (;;)
79 {
80 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
81 if (pos == NULL)
82 break;
83
Bram Moolenaar9c46efd2019-02-04 20:30:18 +010084 // Check if the comment start we found is inside a string.
85 // If it is then restrict the search to below this line and try again.
Bram Moolenaar4b471622019-01-31 13:48:09 +010086 line = ml_get(pos->lnum);
87 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
88 p = skip_string(p);
89 if ((colnr_T)(p - line) <= pos->col)
90 break;
91 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
92 if (cur_maxcomment <= 0)
93 {
94 pos = NULL;
95 break;
96 }
97 }
98 return pos;
99}
100
101/*
102 * Find the start of a comment or raw string, not knowing if we are in a
103 * comment or raw string right now.
104 * Search starts at w_cursor.lnum and goes backwards.
105 * If is_raw is given and returns start of raw_string, sets it to true.
106 * Return NULL when not inside a comment or raw string.
107 * "CORS" -> Comment Or Raw String
108 */
109 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100110ind_find_start_CORS(linenr_T *is_raw) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +0100111{
112 static pos_T comment_pos_copy;
113 pos_T *comment_pos;
114 pos_T *rs_pos;
115
116 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
117 if (comment_pos != NULL)
118 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100119 // Need to make a copy of the static pos in findmatchlimit(),
120 // calling find_start_rawstring() may change it.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100121 comment_pos_copy = *comment_pos;
122 comment_pos = &comment_pos_copy;
123 }
124 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
125
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100126 // If comment_pos is before rs_pos the raw string is inside the comment.
127 // If rs_pos is before comment_pos the comment is inside the raw string.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100128 if (comment_pos == NULL || (rs_pos != NULL
129 && LT_POS(*rs_pos, *comment_pos)))
130 {
131 if (is_raw != NULL && rs_pos != NULL)
132 *is_raw = rs_pos->lnum;
133 return rs_pos;
134 }
135 return comment_pos;
136}
137
138/*
139 * Find the start of a raw string, not knowing if we are in one right now.
140 * Search starts at w_cursor.lnum and goes backwards.
141 * Return NULL when not inside a raw string.
142 */
143 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100144find_start_rawstring(int ind_maxcomment) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +0100145{
146 pos_T *pos;
147 char_u *line;
148 char_u *p;
149 int cur_maxcomment = ind_maxcomment;
150
151 for (;;)
152 {
153 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
154 if (pos == NULL)
155 break;
156
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100157 // Check if the raw string start we found is inside a string.
158 // If it is then restrict the search to below this line and try again.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100159 line = ml_get(pos->lnum);
160 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
161 p = skip_string(p);
162 if ((colnr_T)(p - line) <= pos->col)
163 break;
164 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
165 if (cur_maxcomment <= 0)
166 {
167 pos = NULL;
168 break;
169 }
170 }
171 return pos;
172}
173
174/*
175 * Skip to the end of a "string" and a 'c' character.
176 * If there is no string or character, return argument unmodified.
177 */
178 static char_u *
179skip_string(char_u *p)
180{
181 int i;
182
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100183 // We loop, because strings may be concatenated: "date""time".
Bram Moolenaar4b471622019-01-31 13:48:09 +0100184 for ( ; ; ++p)
185 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100186 if (p[0] == '\'') // 'c' or '\n' or '\000'
Bram Moolenaar4b471622019-01-31 13:48:09 +0100187 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100188 if (!p[1]) // ' at end of line
Bram Moolenaar4b471622019-01-31 13:48:09 +0100189 break;
190 i = 2;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100191 if (p[1] == '\\') // '\n' or '\000'
Bram Moolenaar4b471622019-01-31 13:48:09 +0100192 {
193 ++i;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100194 while (vim_isdigit(p[i - 1])) // '\000'
Bram Moolenaar4b471622019-01-31 13:48:09 +0100195 ++i;
196 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100197 if (p[i] == '\'') // check for trailing '
Bram Moolenaar4b471622019-01-31 13:48:09 +0100198 {
199 p += i;
200 continue;
201 }
202 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100203 else if (p[0] == '"') // start of string
Bram Moolenaar4b471622019-01-31 13:48:09 +0100204 {
205 for (++p; p[0]; ++p)
206 {
207 if (p[0] == '\\' && p[1] != NUL)
208 ++p;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100209 else if (p[0] == '"') // end of string
Bram Moolenaar4b471622019-01-31 13:48:09 +0100210 break;
211 }
212 if (p[0] == '"')
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100213 continue; // continue for another string
Bram Moolenaar4b471622019-01-31 13:48:09 +0100214 }
215 else if (p[0] == 'R' && p[1] == '"')
216 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100217 // Raw string: R"[delim](...)[delim]"
Bram Moolenaar4b471622019-01-31 13:48:09 +0100218 char_u *delim = p + 2;
219 char_u *paren = vim_strchr(delim, '(');
220
221 if (paren != NULL)
222 {
223 size_t delim_len = paren - delim;
224
225 for (p += 3; *p; ++p)
226 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
227 && p[delim_len + 1] == '"')
228 {
229 p += delim_len + 1;
230 break;
231 }
232 if (p[0] == '"')
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100233 continue; // continue for another string
Bram Moolenaar4b471622019-01-31 13:48:09 +0100234 }
235 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100236 break; // no string found
Bram Moolenaar4b471622019-01-31 13:48:09 +0100237 }
238 if (!*p)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100239 --p; // backup from NUL
Bram Moolenaar4b471622019-01-31 13:48:09 +0100240 return p;
241}
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100242#endif // FEAT_CINDENT || FEAT_SYN_HL
Bram Moolenaar4b471622019-01-31 13:48:09 +0100243
244#if defined(FEAT_CINDENT) || defined(PROTO)
245
246/*
247 * Return TRUE if C-indenting is on.
248 */
249 int
250cindent_on(void)
251{
252 return (!p_paste && (curbuf->b_p_cin
253# ifdef FEAT_EVAL
254 || *curbuf->b_p_inde != NUL
255# endif
256 ));
257}
258
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100259// Find result cache for cpp_baseclass
Bram Moolenaar4b471622019-01-31 13:48:09 +0100260typedef struct {
261 int found;
262 lpos_T lpos;
263} cpp_baseclass_cache_T;
264
265/*
266 * Functions for C-indenting.
267 * Most of this originally comes from Eric Fischer.
268 */
269/*
270 * Below "XXX" means that this function may unlock the current line.
271 */
272
273static int cin_isdefault(char_u *);
274static int cin_ispreproc(char_u *);
275static int cin_iscomment(char_u *);
276static int cin_islinecomment(char_u *);
277static int cin_isterminated(char_u *, int, int);
278static int cin_iselse(char_u *);
279static int cin_ends_in(char_u *, char_u *, char_u *);
280static int cin_starts_with(char_u *s, char *word);
281static pos_T *find_match_paren(int);
282static pos_T *find_match_char(int c, int ind_maxparen);
283static int find_last_paren(char_u *l, int start, int end);
284static int find_match(int lookfor, linenr_T ourscope);
285
286/*
287 * Skip over white space and C comments within the line.
288 * Also skip over Perl/shell comments if desired.
289 */
290 static char_u *
291cin_skipcomment(char_u *s)
292{
293 while (*s)
294 {
295 char_u *prev_s = s;
296
297 s = skipwhite(s);
298
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100299 // Perl/shell # comment comment continues until eol. Require a space
300 // before # to avoid recognizing $#array.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100301 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
302 {
303 s += STRLEN(s);
304 break;
305 }
306 if (*s != '/')
307 break;
308 ++s;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100309 if (*s == '/') // slash-slash comment continues till eol
Bram Moolenaar4b471622019-01-31 13:48:09 +0100310 {
311 s += STRLEN(s);
312 break;
313 }
314 if (*s != '*')
315 break;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100316 for (++s; *s; ++s) // skip slash-star comment
Bram Moolenaar4b471622019-01-31 13:48:09 +0100317 if (s[0] == '*' && s[1] == '/')
318 {
319 s += 2;
320 break;
321 }
322 }
323 return s;
324}
325
326/*
327 * Return TRUE if there is no code at *s. White space and comments are
328 * not considered code.
329 */
330 static int
331cin_nocode(char_u *s)
332{
333 return *cin_skipcomment(s) == NUL;
334}
335
336/*
337 * Check previous lines for a "//" line comment, skipping over blank lines.
338 */
339 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100340find_line_comment(void) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +0100341{
342 static pos_T pos;
343 char_u *line;
344 char_u *p;
345
346 pos = curwin->w_cursor;
347 while (--pos.lnum > 0)
348 {
349 line = ml_get(pos.lnum);
350 p = skipwhite(line);
351 if (cin_islinecomment(p))
352 {
353 pos.col = (int)(p - line);
354 return &pos;
355 }
356 if (*p != NUL)
357 break;
358 }
359 return NULL;
360}
361
362/*
363 * Return TRUE if "text" starts with "key:".
364 */
365 static int
366cin_has_js_key(char_u *text)
367{
368 char_u *s = skipwhite(text);
369 int quote = -1;
370
371 if (*s == '\'' || *s == '"')
372 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100373 // can be 'key': or "key":
Bram Moolenaar4b471622019-01-31 13:48:09 +0100374 quote = *s;
375 ++s;
376 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100377 if (!vim_isIDc(*s)) // need at least one ID character
Bram Moolenaar4b471622019-01-31 13:48:09 +0100378 return FALSE;
379
380 while (vim_isIDc(*s))
381 ++s;
382 if (*s == quote)
383 ++s;
384
385 s = cin_skipcomment(s);
386
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100387 // "::" is not a label, it's C++
Bram Moolenaar4b471622019-01-31 13:48:09 +0100388 return (*s == ':' && s[1] != ':');
389}
390
391/*
392 * Check if string matches "label:"; move to character after ':' if true.
393 * "*s" must point to the start of the label, if there is one.
394 */
395 static int
396cin_islabel_skip(char_u **s)
397{
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100398 if (!vim_isIDc(**s)) // need at least one ID character
Bram Moolenaar4b471622019-01-31 13:48:09 +0100399 return FALSE;
400
401 while (vim_isIDc(**s))
402 (*s)++;
403
404 *s = cin_skipcomment(*s);
405
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100406 // "::" is not a label, it's C++
Bram Moolenaar4b471622019-01-31 13:48:09 +0100407 return (**s == ':' && *++*s != ':');
408}
409
410/*
411 * Recognize a label: "label:".
412 * Note: curwin->w_cursor must be where we are looking for the label.
413 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200414 static int
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100415cin_islabel(void) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +0100416{
417 char_u *s;
418
419 s = cin_skipcomment(ml_get_curline());
420
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100421 // Exclude "default" from labels, since it should be indented
422 // like a switch label. Same for C++ scope declarations.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100423 if (cin_isdefault(s))
424 return FALSE;
425 if (cin_isscopedecl(s))
426 return FALSE;
427
428 if (cin_islabel_skip(&s))
429 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100430 // Only accept a label if the previous line is terminated or is a case
431 // label.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100432 pos_T cursor_save;
433 pos_T *trypos;
434 char_u *line;
435
436 cursor_save = curwin->w_cursor;
437 while (curwin->w_cursor.lnum > 1)
438 {
439 --curwin->w_cursor.lnum;
440
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100441 // If we're in a comment or raw string now, skip to the start of
442 // it.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100443 curwin->w_cursor.col = 0;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100444 if ((trypos = ind_find_start_CORS(NULL)) != NULL) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +0100445 curwin->w_cursor = *trypos;
446
447 line = ml_get_curline();
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100448 if (cin_ispreproc(line)) // ignore #defines, #if, etc.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100449 continue;
450 if (*(line = cin_skipcomment(line)) == NUL)
451 continue;
452
453 curwin->w_cursor = cursor_save;
454 if (cin_isterminated(line, TRUE, FALSE)
455 || cin_isscopedecl(line)
456 || cin_iscase(line, TRUE)
457 || (cin_islabel_skip(&line) && cin_nocode(line)))
458 return TRUE;
459 return FALSE;
460 }
461 curwin->w_cursor = cursor_save;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100462 return TRUE; // label at start of file???
Bram Moolenaar4b471622019-01-31 13:48:09 +0100463 }
464 return FALSE;
465}
466
467/*
468 * Recognize structure initialization and enumerations:
469 * "[typedef] [static|public|protected|private] enum"
470 * "[typedef] [static|public|protected|private] = {"
471 */
472 static int
473cin_isinit(void)
474{
475 char_u *s;
476 static char *skip[] = {"static", "public", "protected", "private"};
477
478 s = cin_skipcomment(ml_get_curline());
479
480 if (cin_starts_with(s, "typedef"))
481 s = cin_skipcomment(s + 7);
482
483 for (;;)
484 {
485 int i, l;
486
487 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
488 {
489 l = (int)strlen(skip[i]);
490 if (cin_starts_with(s, skip[i]))
491 {
492 s = cin_skipcomment(s + l);
493 l = 0;
494 break;
495 }
496 }
497 if (l != 0)
498 break;
499 }
500
501 if (cin_starts_with(s, "enum"))
502 return TRUE;
503
504 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
505 return TRUE;
506
507 return FALSE;
508}
509
510/*
511 * Recognize a switch label: "case .*:" or "default:".
512 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200513 static int
Bram Moolenaar4b471622019-01-31 13:48:09 +0100514cin_iscase(
515 char_u *s,
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100516 int strict) // Allow relaxed check of case statement for JS
Bram Moolenaar4b471622019-01-31 13:48:09 +0100517{
518 s = cin_skipcomment(s);
519 if (cin_starts_with(s, "case"))
520 {
521 for (s += 4; *s; ++s)
522 {
523 s = cin_skipcomment(s);
524 if (*s == ':')
525 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100526 if (s[1] == ':') // skip over "::" for C++
Bram Moolenaar4b471622019-01-31 13:48:09 +0100527 ++s;
528 else
529 return TRUE;
530 }
531 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100532 s += 2; // skip over ':'
Bram Moolenaar4b471622019-01-31 13:48:09 +0100533 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100534 return FALSE; // stop at comment
Bram Moolenaar4b471622019-01-31 13:48:09 +0100535 else if (*s == '"')
536 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100537 // JS etc.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100538 if (strict)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100539 return FALSE; // stop at string
Bram Moolenaar4b471622019-01-31 13:48:09 +0100540 else
541 return TRUE;
542 }
543 }
544 return FALSE;
545 }
546
547 if (cin_isdefault(s))
548 return TRUE;
549 return FALSE;
550}
551
552/*
553 * Recognize a "default" switch label.
554 */
555 static int
556cin_isdefault(char_u *s)
557{
558 return (STRNCMP(s, "default", 7) == 0
559 && *(s = cin_skipcomment(s + 7)) == ':'
560 && s[1] != ':');
561}
562
563/*
564 * Recognize a "public/private/protected" scope declaration label.
565 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200566 static int
Bram Moolenaar4b471622019-01-31 13:48:09 +0100567cin_isscopedecl(char_u *s)
568{
569 int i;
570
571 s = cin_skipcomment(s);
572 if (STRNCMP(s, "public", 6) == 0)
573 i = 6;
574 else if (STRNCMP(s, "protected", 9) == 0)
575 i = 9;
576 else if (STRNCMP(s, "private", 7) == 0)
577 i = 7;
578 else
579 return FALSE;
580 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
581}
582
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100583// Maximum number of lines to search back for a "namespace" line.
Bram Moolenaar4b471622019-01-31 13:48:09 +0100584#define FIND_NAMESPACE_LIM 20
585
586/*
587 * Recognize a "namespace" scope declaration.
588 */
589 static int
590cin_is_cpp_namespace(char_u *s)
591{
592 char_u *p;
593 int has_name = FALSE;
594 int has_name_start = FALSE;
595
596 s = cin_skipcomment(s);
597 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
598 {
599 p = cin_skipcomment(skipwhite(s + 9));
600 while (*p != NUL)
601 {
602 if (VIM_ISWHITE(*p))
603 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100604 has_name = TRUE; // found end of a name
Bram Moolenaar4b471622019-01-31 13:48:09 +0100605 p = cin_skipcomment(skipwhite(p));
606 }
607 else if (*p == '{')
608 {
609 break;
610 }
611 else if (vim_iswordc(*p))
612 {
613 has_name_start = TRUE;
614 if (has_name)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100615 return FALSE; // word character after skipping past name
Bram Moolenaar4b471622019-01-31 13:48:09 +0100616 ++p;
617 }
618 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
619 {
620 if (!has_name_start || has_name)
621 return FALSE;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100622 // C++ 17 nested namespace
Bram Moolenaar4b471622019-01-31 13:48:09 +0100623 p += 3;
624 }
625 else
626 {
627 return FALSE;
628 }
629 }
630 return TRUE;
631 }
632 return FALSE;
633}
634
635/*
636 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
637 */
638 static int
639cin_is_cpp_extern_c(char_u *s)
640{
641 char_u *p;
642 int has_string_literal = FALSE;
643
644 s = cin_skipcomment(s);
645 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
646 {
647 p = cin_skipcomment(skipwhite(s + 6));
648 while (*p != NUL)
649 {
650 if (VIM_ISWHITE(*p))
651 {
652 p = cin_skipcomment(skipwhite(p));
653 }
654 else if (*p == '{')
655 {
656 break;
657 }
658 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
659 {
660 if (has_string_literal)
661 return FALSE;
662 has_string_literal = TRUE;
663 p += 3;
664 }
665 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
666 && p[4] == '"')
667 {
668 if (has_string_literal)
669 return FALSE;
670 has_string_literal = TRUE;
671 p += 5;
672 }
673 else
674 {
675 return FALSE;
676 }
677 }
678 return has_string_literal ? TRUE : FALSE;
679 }
680 return FALSE;
681}
682
683/*
684 * Return a pointer to the first non-empty non-comment character after a ':'.
685 * Return NULL if not found.
686 * case 234: a = b;
687 * ^
688 */
689 static char_u *
690after_label(char_u *l)
691{
692 for ( ; *l; ++l)
693 {
694 if (*l == ':')
695 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100696 if (l[1] == ':') // skip over "::" for C++
Bram Moolenaar4b471622019-01-31 13:48:09 +0100697 ++l;
698 else if (!cin_iscase(l + 1, FALSE))
699 break;
700 }
701 else if (*l == '\'' && l[1] && l[2] == '\'')
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100702 l += 2; // skip over 'x'
Bram Moolenaar4b471622019-01-31 13:48:09 +0100703 }
704 if (*l == NUL)
705 return NULL;
706 l = cin_skipcomment(l + 1);
707 if (*l == NUL)
708 return NULL;
709 return l;
710}
711
712/*
713 * Get indent of line "lnum", skipping a label.
714 * Return 0 if there is nothing after the label.
715 */
716 static int
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100717get_indent_nolabel (linenr_T lnum) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +0100718{
719 char_u *l;
720 pos_T fp;
721 colnr_T col;
722 char_u *p;
723
724 l = ml_get(lnum);
725 p = after_label(l);
726 if (p == NULL)
727 return 0;
728
729 fp.col = (colnr_T)(p - l);
730 fp.lnum = lnum;
731 getvcol(curwin, &fp, &col, NULL, NULL);
732 return (int)col;
733}
734
735/*
736 * Find indent for line "lnum", ignoring any case or jump label.
737 * Also return a pointer to the text (after the label) in "pp".
738 * label: if (asdf && asdfasdf)
739 * ^
740 */
741 static int
742skip_label(linenr_T lnum, char_u **pp)
743{
744 char_u *l;
745 int amount;
746 pos_T cursor_save;
747
748 cursor_save = curwin->w_cursor;
749 curwin->w_cursor.lnum = lnum;
750 l = ml_get_curline();
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100751 // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +0100752 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
753 {
754 amount = get_indent_nolabel(lnum);
755 l = after_label(ml_get_curline());
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100756 if (l == NULL) // just in case
Bram Moolenaar4b471622019-01-31 13:48:09 +0100757 l = ml_get_curline();
758 }
759 else
760 {
761 amount = get_indent();
762 l = ml_get_curline();
763 }
764 *pp = l;
765
766 curwin->w_cursor = cursor_save;
767 return amount;
768}
769
770/*
771 * Return the indent of the first variable name after a type in a declaration.
772 * int a, indent of "a"
773 * static struct foo b, indent of "b"
774 * enum bla c, indent of "c"
775 * Returns zero when it doesn't look like a declaration.
776 */
777 static int
778cin_first_id_amount(void)
779{
780 char_u *line, *p, *s;
781 int len;
782 pos_T fp;
783 colnr_T col;
784
785 line = ml_get_curline();
786 p = skipwhite(line);
787 len = (int)(skiptowhite(p) - p);
788 if (len == 6 && STRNCMP(p, "static", 6) == 0)
789 {
790 p = skipwhite(p + 6);
791 len = (int)(skiptowhite(p) - p);
792 }
793 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
794 p = skipwhite(p + 6);
795 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
796 p = skipwhite(p + 4);
797 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
798 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
799 {
800 s = skipwhite(p + len);
801 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
802 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
803 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
804 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
805 p = s;
806 }
807 for (len = 0; vim_isIDc(p[len]); ++len)
808 ;
809 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
810 return 0;
811
812 p = skipwhite(p + len);
813 fp.lnum = curwin->w_cursor.lnum;
814 fp.col = (colnr_T)(p - line);
815 getvcol(curwin, &fp, &col, NULL, NULL);
816 return (int)col;
817}
818
819/*
820 * Return the indent of the first non-blank after an equal sign.
821 * char *foo = "here";
822 * Return zero if no (useful) equal sign found.
823 * Return -1 if the line above "lnum" ends in a backslash.
824 * foo = "asdf\
825 * asdf\
826 * here";
827 */
828 static int
829cin_get_equal_amount(linenr_T lnum)
830{
831 char_u *line;
832 char_u *s;
833 colnr_T col;
834 pos_T fp;
835
836 if (lnum > 1)
837 {
838 line = ml_get(lnum - 1);
839 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
840 return -1;
841 }
842
843 line = s = ml_get(lnum);
844 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
845 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100846 if (cin_iscomment(s)) // ignore comments
Bram Moolenaar4b471622019-01-31 13:48:09 +0100847 s = cin_skipcomment(s);
848 else
849 ++s;
850 }
851 if (*s != '=')
852 return 0;
853
854 s = skipwhite(s + 1);
855 if (cin_nocode(s))
856 return 0;
857
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100858 if (*s == '"') // nice alignment for continued strings
Bram Moolenaar4b471622019-01-31 13:48:09 +0100859 ++s;
860
861 fp.lnum = lnum;
862 fp.col = (colnr_T)(s - line);
863 getvcol(curwin, &fp, &col, NULL, NULL);
864 return (int)col;
865}
866
867/*
868 * Recognize a preprocessor statement: Any line that starts with '#'.
869 */
870 static int
871cin_ispreproc(char_u *s)
872{
873 if (*skipwhite(s) == '#')
874 return TRUE;
875 return FALSE;
876}
877
878/*
879 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
880 * continuation line of a preprocessor statement. Decrease "*lnump" to the
881 * start and return the line in "*pp".
882 * Put the amount of indent in "*amount".
883 */
884 static int
885cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
886{
887 char_u *line = *pp;
888 linenr_T lnum = *lnump;
889 int retval = FALSE;
890 int candidate_amount = *amount;
891
892 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
893 candidate_amount = get_indent_lnum(lnum);
894
895 for (;;)
896 {
897 if (cin_ispreproc(line))
898 {
899 retval = TRUE;
900 *lnump = lnum;
901 break;
902 }
903 if (lnum == 1)
904 break;
905 line = ml_get(--lnum);
906 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
907 break;
908 }
909
910 if (lnum != *lnump)
911 *pp = ml_get(*lnump);
912 if (retval)
913 *amount = candidate_amount;
914 return retval;
915}
916
917/*
918 * Recognize the start of a C or C++ comment.
919 */
920 static int
921cin_iscomment(char_u *p)
922{
923 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
924}
925
926/*
927 * Recognize the start of a "//" comment.
928 */
929 static int
930cin_islinecomment(char_u *p)
931{
932 return (p[0] == '/' && p[1] == '/');
933}
934
935/*
936 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
937 * '}'.
938 * Don't consider "} else" a terminated line.
939 * If a line begins with an "else", only consider it terminated if no unmatched
940 * opening braces follow (handle "else { foo();" correctly).
941 * Return the character terminating the line (ending char's have precedence if
942 * both apply in order to determine initializations).
943 */
944 static int
945cin_isterminated(
946 char_u *s,
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100947 int incl_open, // include '{' at the end as terminator
948 int incl_comma) // recognize a trailing comma
Bram Moolenaar4b471622019-01-31 13:48:09 +0100949{
950 char_u found_start = 0;
951 unsigned n_open = 0;
952 int is_else = FALSE;
953
954 s = cin_skipcomment(s);
955
956 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
957 found_start = *s;
958
959 if (!found_start)
960 is_else = cin_iselse(s);
961
962 while (*s)
963 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +0100964 // skip over comments, "" strings and 'c'haracters
Bram Moolenaar4b471622019-01-31 13:48:09 +0100965 s = skip_string(cin_skipcomment(s));
966 if (*s == '}' && n_open > 0)
967 --n_open;
968 if ((!is_else || n_open == 0)
969 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
970 && cin_nocode(s + 1))
971 return *s;
972 else if (*s == '{')
973 {
974 if (incl_open && cin_nocode(s + 1))
975 return *s;
976 else
977 ++n_open;
978 }
979
980 if (*s)
981 s++;
982 }
983 return found_start;
984}
985
986/*
987 * Recognize the basic picture of a function declaration -- it needs to
988 * have an open paren somewhere and a close paren at the end of the line and
989 * no semicolons anywhere.
990 * When a line ends in a comma we continue looking in the next line.
991 * "sp" points to a string with the line. When looking at other lines it must
992 * be restored to the line. When it's NULL fetch lines here.
993 * "first_lnum" is where we start looking.
994 * "min_lnum" is the line before which we will not be looking.
995 */
996 static int
997cin_isfuncdecl(
998 char_u **sp,
999 linenr_T first_lnum,
1000 linenr_T min_lnum)
1001{
1002 char_u *s;
1003 linenr_T lnum = first_lnum;
1004 linenr_T save_lnum = curwin->w_cursor.lnum;
1005 int retval = FALSE;
1006 pos_T *trypos;
1007 int just_started = TRUE;
1008
1009 if (sp == NULL)
1010 s = ml_get(lnum);
1011 else
1012 s = *sp;
1013
1014 curwin->w_cursor.lnum = lnum;
1015 if (find_last_paren(s, '(', ')')
1016 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
1017 {
1018 lnum = trypos->lnum;
1019 if (lnum < min_lnum)
1020 {
1021 curwin->w_cursor.lnum = save_lnum;
1022 return FALSE;
1023 }
1024
1025 s = ml_get(lnum);
1026 }
1027 curwin->w_cursor.lnum = save_lnum;
1028
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001029 // Ignore line starting with #.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001030 if (cin_ispreproc(s))
1031 return FALSE;
1032
1033 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
1034 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001035 if (cin_iscomment(s)) // ignore comments
Bram Moolenaar4b471622019-01-31 13:48:09 +01001036 s = cin_skipcomment(s);
1037 else if (*s == ':')
1038 {
1039 if (*(s + 1) == ':')
1040 s += 2;
1041 else
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001042 // To avoid a mistake in the following situation:
1043 // A::A(int a, int b)
1044 // : a(0) // <--not a function decl
1045 // , b(0)
1046 // {...
Bram Moolenaar4b471622019-01-31 13:48:09 +01001047 return FALSE;
1048 }
1049 else
1050 ++s;
1051 }
1052 if (*s != '(')
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001053 return FALSE; // ';', ' or " before any () or no '('
Bram Moolenaar4b471622019-01-31 13:48:09 +01001054
1055 while (*s && *s != ';' && *s != '\'' && *s != '"')
1056 {
1057 if (*s == ')' && cin_nocode(s + 1))
1058 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001059 /*
1060 * ')' at the end: may have found a match
Bram Moolenaar4b471622019-01-31 13:48:09 +01001061 * Check for he previous line not to end in a backslash:
1062 * #if defined(x) && \
1063 * defined(y)
1064 */
1065 lnum = first_lnum - 1;
1066 s = ml_get(lnum);
1067 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
1068 retval = TRUE;
1069 goto done;
1070 }
1071 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
1072 {
1073 int comma = (*s == ',');
1074
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001075 // ',' at the end: continue looking in the next line.
1076 // At the end: check for ',' in the next line, for this style:
1077 // func(arg1
1078 // , arg2)
Bram Moolenaar4b471622019-01-31 13:48:09 +01001079 for (;;)
1080 {
1081 if (lnum >= curbuf->b_ml.ml_line_count)
1082 break;
1083 s = ml_get(++lnum);
1084 if (!cin_ispreproc(s))
1085 break;
1086 }
1087 if (lnum >= curbuf->b_ml.ml_line_count)
1088 break;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001089 // Require a comma at end of the line or a comma or ')' at the
1090 // start of next line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001091 s = skipwhite(s);
1092 if (!just_started && (!comma && *s != ',' && *s != ')'))
1093 break;
1094 just_started = FALSE;
1095 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001096 else if (cin_iscomment(s)) // ignore comments
Bram Moolenaar4b471622019-01-31 13:48:09 +01001097 s = cin_skipcomment(s);
1098 else
1099 {
1100 ++s;
1101 just_started = FALSE;
1102 }
1103 }
1104
1105done:
1106 if (lnum != first_lnum && sp != NULL)
1107 *sp = ml_get(first_lnum);
1108
1109 return retval;
1110}
1111
1112 static int
1113cin_isif(char_u *p)
1114{
1115 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
1116}
1117
1118 static int
1119cin_iselse(
1120 char_u *p)
1121{
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001122 if (*p == '}') // accept "} else"
Bram Moolenaar4b471622019-01-31 13:48:09 +01001123 p = cin_skipcomment(p + 1);
1124 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
1125}
1126
1127 static int
1128cin_isdo(char_u *p)
1129{
1130 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
1131}
1132
1133/*
1134 * Check if this is a "while" that should have a matching "do".
1135 * We only accept a "while (condition) ;", with only white space between the
1136 * ')' and ';'. The condition may be spread over several lines.
1137 */
1138 static int
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001139cin_iswhileofdo (char_u *p, linenr_T lnum) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001140{
1141 pos_T cursor_save;
1142 pos_T *trypos;
1143 int retval = FALSE;
1144
1145 p = cin_skipcomment(p);
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001146 if (*p == '}') // accept "} while (cond);"
Bram Moolenaar4b471622019-01-31 13:48:09 +01001147 p = cin_skipcomment(p + 1);
1148 if (cin_starts_with(p, "while"))
1149 {
1150 cursor_save = curwin->w_cursor;
1151 curwin->w_cursor.lnum = lnum;
1152 curwin->w_cursor.col = 0;
1153 p = ml_get_curline();
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001154 while (*p && *p != 'w') // skip any '}', until the 'w' of the "while"
Bram Moolenaar4b471622019-01-31 13:48:09 +01001155 {
1156 ++p;
1157 ++curwin->w_cursor.col;
1158 }
1159 if ((trypos = findmatchlimit(NULL, 0, 0,
1160 curbuf->b_ind_maxparen)) != NULL
1161 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
1162 retval = TRUE;
1163 curwin->w_cursor = cursor_save;
1164 }
1165 return retval;
1166}
1167
1168/*
1169 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
1170 * Return 0 if there is none.
1171 * Otherwise return !0 and update "*poffset" to point to the place where the
1172 * string was found.
1173 */
1174 static int
1175cin_is_if_for_while_before_offset(char_u *line, int *poffset)
1176{
1177 int offset = *poffset;
1178
1179 if (offset-- < 2)
1180 return 0;
1181 while (offset > 2 && VIM_ISWHITE(line[offset]))
1182 --offset;
1183
1184 offset -= 1;
1185 if (!STRNCMP(line + offset, "if", 2))
1186 goto probablyFound;
1187
1188 if (offset >= 1)
1189 {
1190 offset -= 1;
1191 if (!STRNCMP(line + offset, "for", 3))
1192 goto probablyFound;
1193
1194 if (offset >= 2)
1195 {
1196 offset -= 2;
1197 if (!STRNCMP(line + offset, "while", 5))
1198 goto probablyFound;
1199 }
1200 }
1201 return 0;
1202
1203probablyFound:
1204 if (!offset || !vim_isIDc(line[offset - 1]))
1205 {
1206 *poffset = offset;
1207 return 1;
1208 }
1209 return 0;
1210}
1211
1212/*
1213 * Return TRUE if we are at the end of a do-while.
1214 * do
1215 * nothing;
1216 * while (foo
1217 * && bar); <-- here
1218 * Adjust the cursor to the line with "while".
1219 */
1220 static int
1221cin_iswhileofdo_end(int terminated)
1222{
1223 char_u *line;
1224 char_u *p;
1225 char_u *s;
1226 pos_T *trypos;
1227 int i;
1228
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001229 if (terminated != ';') // there must be a ';' at the end
Bram Moolenaar4b471622019-01-31 13:48:09 +01001230 return FALSE;
1231
1232 p = line = ml_get_curline();
1233 while (*p != NUL)
1234 {
1235 p = cin_skipcomment(p);
1236 if (*p == ')')
1237 {
1238 s = skipwhite(p + 1);
1239 if (*s == ';' && cin_nocode(s + 1))
1240 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001241 // Found ");" at end of the line, now check there is "while"
1242 // before the matching '('. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001243 i = (int)(p - line);
1244 curwin->w_cursor.col = i;
1245 trypos = find_match_paren(curbuf->b_ind_maxparen);
1246 if (trypos != NULL)
1247 {
1248 s = cin_skipcomment(ml_get(trypos->lnum));
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001249 if (*s == '}') // accept "} while (cond);"
Bram Moolenaar4b471622019-01-31 13:48:09 +01001250 s = cin_skipcomment(s + 1);
1251 if (cin_starts_with(s, "while"))
1252 {
1253 curwin->w_cursor.lnum = trypos->lnum;
1254 return TRUE;
1255 }
1256 }
1257
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001258 // Searching may have made "line" invalid, get it again.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001259 line = ml_get_curline();
1260 p = line + i;
1261 }
1262 }
1263 if (*p != NUL)
1264 ++p;
1265 }
1266 return FALSE;
1267}
1268
1269 static int
1270cin_isbreak(char_u *p)
1271{
1272 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
1273}
1274
1275/*
1276 * Find the position of a C++ base-class declaration or
1277 * constructor-initialization. eg:
1278 *
1279 * class MyClass :
1280 * baseClass <-- here
1281 * class MyClass : public baseClass,
1282 * anotherBaseClass <-- here (should probably lineup ??)
1283 * MyClass::MyClass(...) :
1284 * baseClass(...) <-- here (constructor-initialization)
1285 *
1286 * This is a lot of guessing. Watch out for "cond ? func() : foo".
1287 */
1288 static int
1289cin_is_cpp_baseclass(
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001290 cpp_baseclass_cache_T *cached) // input and output
Bram Moolenaar4b471622019-01-31 13:48:09 +01001291{
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001292 lpos_T *pos = &cached->lpos; // find position
Bram Moolenaar4b471622019-01-31 13:48:09 +01001293 char_u *s;
1294 int class_or_struct, lookfor_ctor_init, cpp_base_class;
1295 linenr_T lnum = curwin->w_cursor.lnum;
1296 char_u *line = ml_get_curline();
1297
1298 if (pos->lnum <= lnum)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001299 return cached->found; // Use the cached result
Bram Moolenaar4b471622019-01-31 13:48:09 +01001300
1301 pos->col = 0;
1302
1303 s = skipwhite(line);
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001304 if (*s == '#') // skip #define FOO x ? (x) : x
Bram Moolenaar4b471622019-01-31 13:48:09 +01001305 return FALSE;
1306 s = cin_skipcomment(s);
1307 if (*s == NUL)
1308 return FALSE;
1309
1310 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
1311
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001312 // Search for a line starting with '#', empty, ending in ';' or containing
1313 // '{' or '}' and start below it. This handles the following situations:
1314 // a = cond ?
1315 // func() :
1316 // asdf;
1317 // func::foo()
1318 // : something
1319 // {}
1320 // Foo::Foo (int one, int two)
1321 // : something(4),
1322 // somethingelse(3)
1323 // {}
Bram Moolenaar4b471622019-01-31 13:48:09 +01001324 while (lnum > 1)
1325 {
1326 line = ml_get(lnum - 1);
1327 s = skipwhite(line);
1328 if (*s == '#' || *s == NUL)
1329 break;
1330 while (*s != NUL)
1331 {
1332 s = cin_skipcomment(s);
1333 if (*s == '{' || *s == '}'
1334 || (*s == ';' && cin_nocode(s + 1)))
1335 break;
1336 if (*s != NUL)
1337 ++s;
1338 }
1339 if (*s != NUL)
1340 break;
1341 --lnum;
1342 }
1343
1344 pos->lnum = lnum;
1345 line = ml_get(lnum);
1346 s = line;
1347 for (;;)
1348 {
1349 if (*s == NUL)
1350 {
1351 if (lnum == curwin->w_cursor.lnum)
1352 break;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001353 // Continue in the cursor line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001354 line = ml_get(++lnum);
1355 s = line;
1356 }
1357 if (s == line)
1358 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001359 // don't recognize "case (foo):" as a baseclass
Bram Moolenaar4b471622019-01-31 13:48:09 +01001360 if (cin_iscase(s, FALSE))
1361 break;
1362 s = cin_skipcomment(line);
1363 if (*s == NUL)
1364 continue;
1365 }
1366
1367 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
1368 s = skip_string(s) + 1;
1369 else if (s[0] == ':')
1370 {
1371 if (s[1] == ':')
1372 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001373 // skip double colon. It can't be a constructor
1374 // initialization any more
Bram Moolenaar4b471622019-01-31 13:48:09 +01001375 lookfor_ctor_init = FALSE;
1376 s = cin_skipcomment(s + 2);
1377 }
1378 else if (lookfor_ctor_init || class_or_struct)
1379 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001380 // we have something found, that looks like the start of
1381 // cpp-base-class-declaration or constructor-initialization
Bram Moolenaar4b471622019-01-31 13:48:09 +01001382 cpp_base_class = TRUE;
1383 lookfor_ctor_init = class_or_struct = FALSE;
1384 pos->col = 0;
1385 s = cin_skipcomment(s + 1);
1386 }
1387 else
1388 s = cin_skipcomment(s + 1);
1389 }
1390 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
1391 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
1392 {
1393 class_or_struct = TRUE;
1394 lookfor_ctor_init = FALSE;
1395
1396 if (*s == 'c')
1397 s = cin_skipcomment(s + 5);
1398 else
1399 s = cin_skipcomment(s + 6);
1400 }
1401 else
1402 {
1403 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
1404 {
1405 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
1406 }
1407 else if (s[0] == ')')
1408 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001409 // Constructor-initialization is assumed if we come across
1410 // something like "):"
Bram Moolenaar4b471622019-01-31 13:48:09 +01001411 class_or_struct = FALSE;
1412 lookfor_ctor_init = TRUE;
1413 }
1414 else if (s[0] == '?')
1415 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001416 // Avoid seeing '() :' after '?' as constructor init.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001417 return FALSE;
1418 }
1419 else if (!vim_isIDc(s[0]))
1420 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001421 // if it is not an identifier, we are wrong
Bram Moolenaar4b471622019-01-31 13:48:09 +01001422 class_or_struct = FALSE;
1423 lookfor_ctor_init = FALSE;
1424 }
1425 else if (pos->col == 0)
1426 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001427 // it can't be a constructor-initialization any more
Bram Moolenaar4b471622019-01-31 13:48:09 +01001428 lookfor_ctor_init = FALSE;
1429
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001430 // the first statement starts here: lineup with this one...
Bram Moolenaar4b471622019-01-31 13:48:09 +01001431 if (cpp_base_class)
1432 pos->col = (colnr_T)(s - line);
1433 }
1434
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001435 // When the line ends in a comma don't align with it.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001436 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
1437 pos->col = 0;
1438
1439 s = cin_skipcomment(s + 1);
1440 }
1441 }
1442
1443 cached->found = cpp_base_class;
1444 if (cpp_base_class)
1445 pos->lnum = lnum;
1446 return cpp_base_class;
1447}
1448
1449 static int
1450get_baseclass_amount(int col)
1451{
1452 int amount;
1453 colnr_T vcol;
1454 pos_T *trypos;
1455
1456 if (col == 0)
1457 {
1458 amount = get_indent();
1459 if (find_last_paren(ml_get_curline(), '(', ')')
1460 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001461 amount = get_indent_lnum(trypos->lnum); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001462 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
1463 amount += curbuf->b_ind_cpp_baseclass;
1464 }
1465 else
1466 {
1467 curwin->w_cursor.col = col;
1468 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
1469 amount = (int)vcol;
1470 }
1471 if (amount < curbuf->b_ind_cpp_baseclass)
1472 amount = curbuf->b_ind_cpp_baseclass;
1473 return amount;
1474}
1475
1476/*
1477 * Return TRUE if string "s" ends with the string "find", possibly followed by
1478 * white space and comments. Skip strings and comments.
1479 * Ignore "ignore" after "find" if it's not NULL.
1480 */
1481 static int
1482cin_ends_in(char_u *s, char_u *find, char_u *ignore)
1483{
1484 char_u *p = s;
1485 char_u *r;
1486 int len = (int)STRLEN(find);
1487
1488 while (*p != NUL)
1489 {
1490 p = cin_skipcomment(p);
1491 if (STRNCMP(p, find, len) == 0)
1492 {
1493 r = skipwhite(p + len);
1494 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
1495 r = skipwhite(r + STRLEN(ignore));
1496 if (cin_nocode(r))
1497 return TRUE;
1498 }
1499 if (*p != NUL)
1500 ++p;
1501 }
1502 return FALSE;
1503}
1504
1505/*
1506 * Return TRUE when "s" starts with "word" and then a non-ID character.
1507 */
1508 static int
1509cin_starts_with(char_u *s, char *word)
1510{
1511 int l = (int)STRLEN(word);
1512
1513 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
1514}
1515
1516/*
1517 * Skip strings, chars and comments until at or past "trypos".
1518 * Return the column found.
1519 */
1520 static int
1521cin_skip2pos(pos_T *trypos)
1522{
1523 char_u *line;
1524 char_u *p;
1525 char_u *new_p;
1526
1527 p = line = ml_get(trypos->lnum);
1528 while (*p && (colnr_T)(p - line) < trypos->col)
1529 {
1530 if (cin_iscomment(p))
1531 p = cin_skipcomment(p);
1532 else
1533 {
1534 new_p = skip_string(p);
1535 if (new_p == p)
1536 ++p;
1537 else
1538 p = new_p;
1539 }
1540 }
1541 return (int)(p - line);
1542}
1543
1544/*
1545 * Find the '{' at the start of the block we are in.
1546 * Return NULL if no match found.
1547 * Ignore a '{' that is in a comment, makes indenting the next three lines
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001548 * work.
1549 */
Bram Moolenaar4b471622019-01-31 13:48:09 +01001550/* foo() */
1551/* { */
1552/* } */
1553
1554 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001555find_start_brace(void) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001556{
1557 pos_T cursor_save;
1558 pos_T *trypos;
1559 pos_T *pos;
1560 static pos_T pos_copy;
1561
1562 cursor_save = curwin->w_cursor;
1563 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
1564 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001565 pos_copy = *trypos; // copy pos_T, next findmatch will change it
Bram Moolenaar4b471622019-01-31 13:48:09 +01001566 trypos = &pos_copy;
1567 curwin->w_cursor = *trypos;
1568 pos = NULL;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001569 // ignore the { if it's in a // or / * * / comment
Bram Moolenaar4b471622019-01-31 13:48:09 +01001570 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001571 && (pos = ind_find_start_CORS(NULL)) == NULL) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001572 break;
1573 if (pos != NULL)
1574 curwin->w_cursor.lnum = pos->lnum;
1575 }
1576 curwin->w_cursor = cursor_save;
1577 return trypos;
1578}
1579
1580/*
1581 * Find the matching '(', ignoring it if it is in a comment.
1582 * Return NULL if no match found.
1583 */
1584 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001585find_match_paren(int ind_maxparen) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001586{
1587 return find_match_char('(', ind_maxparen);
1588}
1589
1590 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001591find_match_char(int c, int ind_maxparen) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001592{
1593 pos_T cursor_save;
1594 pos_T *trypos;
1595 static pos_T pos_copy;
1596 int ind_maxp_wk;
1597
1598 cursor_save = curwin->w_cursor;
1599 ind_maxp_wk = ind_maxparen;
1600retry:
1601 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
1602 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001603 // check if the ( is in a // comment
Bram Moolenaar4b471622019-01-31 13:48:09 +01001604 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
1605 {
1606 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
1607 if (ind_maxp_wk > 0)
1608 {
1609 curwin->w_cursor = *trypos;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001610 curwin->w_cursor.col = 0; // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001611 goto retry;
1612 }
1613 trypos = NULL;
1614 }
1615 else
1616 {
1617 pos_T *trypos_wk;
1618
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001619 pos_copy = *trypos; // copy trypos, findmatch will change it
Bram Moolenaar4b471622019-01-31 13:48:09 +01001620 trypos = &pos_copy;
1621 curwin->w_cursor = *trypos;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001622 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001623 {
1624 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
1625 - trypos_wk->lnum);
1626 if (ind_maxp_wk > 0)
1627 {
1628 curwin->w_cursor = *trypos_wk;
1629 goto retry;
1630 }
1631 trypos = NULL;
1632 }
1633 }
1634 }
1635 curwin->w_cursor = cursor_save;
1636 return trypos;
1637}
1638
1639/*
1640 * Find the matching '(', ignoring it if it is in a comment or before an
1641 * unmatched {.
1642 * Return NULL if no match found.
1643 */
1644 static pos_T *
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001645find_match_paren_after_brace (int ind_maxparen) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01001646{
1647 pos_T *trypos = find_match_paren(ind_maxparen);
1648
1649 if (trypos != NULL)
1650 {
1651 pos_T *tryposBrace = find_start_brace();
1652
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001653 // If both an unmatched '(' and '{' is found. Ignore the '('
1654 // position if the '{' is further down.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001655 if (tryposBrace != NULL
1656 && (trypos->lnum != tryposBrace->lnum
1657 ? trypos->lnum < tryposBrace->lnum
1658 : trypos->col < tryposBrace->col))
1659 trypos = NULL;
1660 }
1661 return trypos;
1662}
1663
1664/*
1665 * Return ind_maxparen corrected for the difference in line number between the
1666 * cursor position and "startpos". This makes sure that searching for a
1667 * matching paren above the cursor line doesn't find a match because of
1668 * looking a few lines further.
1669 */
1670 static int
1671corr_ind_maxparen(pos_T *startpos)
1672{
1673 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
1674
1675 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
1676 return curbuf->b_ind_maxparen - (int)n;
1677 return curbuf->b_ind_maxparen;
1678}
1679
1680/*
1681 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
1682 * line "l". "l" must point to the start of the line.
1683 */
1684 static int
1685find_last_paren(char_u *l, int start, int end)
1686{
1687 int i;
1688 int retval = FALSE;
1689 int open_count = 0;
1690
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001691 curwin->w_cursor.col = 0; // default is start of line
Bram Moolenaar4b471622019-01-31 13:48:09 +01001692
1693 for (i = 0; l[i] != NUL; i++)
1694 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001695 i = (int)(cin_skipcomment(l + i) - l); // ignore parens in comments
1696 i = (int)(skip_string(l + i) - l); // ignore parens in quotes
Bram Moolenaar4b471622019-01-31 13:48:09 +01001697 if (l[i] == start)
1698 ++open_count;
1699 else if (l[i] == end)
1700 {
1701 if (open_count > 0)
1702 --open_count;
1703 else
1704 {
1705 curwin->w_cursor.col = i;
1706 retval = TRUE;
1707 }
1708 }
1709 }
1710 return retval;
1711}
1712
1713/*
1714 * Parse 'cinoptions' and set the values in "curbuf".
1715 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
1716 */
1717 void
1718parse_cino(buf_T *buf)
1719{
1720 char_u *p;
1721 char_u *l;
1722 char_u *digits;
1723 int n;
1724 int divider;
1725 int fraction = 0;
1726 int sw = (int)get_sw_value(buf);
1727
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001728 // Set the default values.
1729
1730 // Spaces from a block's opening brace the prevailing indent for that
1731 // block should be.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001732 buf->b_ind_level = sw;
1733
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001734 // Spaces from the edge of the line an open brace that's at the end of a
1735 // line is imagined to be.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001736 buf->b_ind_open_imag = 0;
1737
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001738 // Spaces from the prevailing indent for a line that is not preceded by
1739 // an opening brace.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001740 buf->b_ind_no_brace = 0;
1741
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001742 // Column where the first { of a function should be located }.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001743 buf->b_ind_first_open = 0;
1744
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001745 // Spaces from the prevailing indent a leftmost open brace should be
1746 // located.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001747 buf->b_ind_open_extra = 0;
1748
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001749 // Spaces from the matching open brace (real location for one at the left
1750 // edge; imaginary location from one that ends a line) the matching close
1751 // brace should be located.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001752 buf->b_ind_close_extra = 0;
1753
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001754 // Spaces from the edge of the line an open brace sitting in the leftmost
1755 // column is imagined to be.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001756 buf->b_ind_open_left_imag = 0;
1757
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001758 // Spaces jump labels should be shifted to the left if N is non-negative,
1759 // otherwise the jump label will be put to column 1.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001760 buf->b_ind_jump_label = -1;
1761
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001762 // Spaces from the switch() indent a "case xx" label should be located.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001763 buf->b_ind_case = sw;
1764
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001765 // Spaces from the "case xx:" code after a switch() should be located.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001766 buf->b_ind_case_code = sw;
1767
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001768 // Lineup break at end of case in switch() with case label.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001769 buf->b_ind_case_break = 0;
1770
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001771 // Spaces from the class declaration indent a scope declaration label
1772 // should be located.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001773 buf->b_ind_scopedecl = sw;
1774
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001775 // Spaces from the scope declaration label code should be located.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001776 buf->b_ind_scopedecl_code = sw;
1777
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001778 // Amount K&R-style parameters should be indented.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001779 buf->b_ind_param = sw;
1780
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001781 // Amount a function type spec should be indented.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001782 buf->b_ind_func_type = sw;
1783
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001784 // Amount a cpp base class declaration or constructor initialization
1785 // should be indented.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001786 buf->b_ind_cpp_baseclass = sw;
1787
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001788 // additional spaces beyond the prevailing indent a continuation line
1789 // should be located.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001790 buf->b_ind_continuation = sw;
1791
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001792 // Spaces from the indent of the line with an unclosed parentheses.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001793 buf->b_ind_unclosed = sw * 2;
1794
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001795 // Spaces from the indent of the line with an unclosed parentheses, which
1796 // itself is also unclosed.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001797 buf->b_ind_unclosed2 = sw;
1798
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001799 // Suppress ignoring spaces from the indent of a line starting with an
1800 // unclosed parentheses.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001801 buf->b_ind_unclosed_noignore = 0;
1802
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001803 // If the opening paren is the last nonwhite character on the line, and
1804 // b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
1805 // context (for very long lines).
Bram Moolenaar4b471622019-01-31 13:48:09 +01001806 buf->b_ind_unclosed_wrapped = 0;
1807
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001808 // Suppress ignoring white space when lining up with the character after
1809 // an unclosed parentheses.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001810 buf->b_ind_unclosed_whiteok = 0;
1811
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001812 // Indent a closing parentheses under the line start of the matching
1813 // opening parentheses.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001814 buf->b_ind_matching_paren = 0;
1815
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001816 // Indent a closing parentheses under the previous line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001817 buf->b_ind_paren_prev = 0;
1818
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001819 // Extra indent for comments.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001820 buf->b_ind_comment = 0;
1821
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001822 // Spaces from the comment opener when there is nothing after it.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001823 buf->b_ind_in_comment = 3;
1824
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001825 // Boolean: if non-zero, use b_ind_in_comment even if there is something
1826 // after the comment opener.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001827 buf->b_ind_in_comment2 = 0;
1828
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001829 // Max lines to search for an open paren.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001830 buf->b_ind_maxparen = 20;
1831
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001832 // Max lines to search for an open comment.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001833 buf->b_ind_maxcomment = 70;
1834
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001835 // Handle braces for java code.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001836 buf->b_ind_java = 0;
1837
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001838 // Not to confuse JS object properties with labels.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001839 buf->b_ind_js = 0;
1840
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001841 // Handle blocked cases correctly.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001842 buf->b_ind_keep_case_label = 0;
1843
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001844 // Handle C++ namespace.
Bram Moolenaar4b471622019-01-31 13:48:09 +01001845 buf->b_ind_cpp_namespace = 0;
1846
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001847 // Handle continuation lines containing conditions of if(), for() and
1848 // while().
Bram Moolenaar4b471622019-01-31 13:48:09 +01001849 buf->b_ind_if_for_while = 0;
1850
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001851 // indentation for # comments
Bram Moolenaar4b471622019-01-31 13:48:09 +01001852 buf->b_ind_hash_comment = 0;
1853
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001854 // Handle C++ extern "C" or "C++"
Bram Moolenaar4b471622019-01-31 13:48:09 +01001855 buf->b_ind_cpp_extern_c = 0;
1856
1857 for (p = buf->b_p_cino; *p; )
1858 {
1859 l = p++;
1860 if (*p == '-')
1861 ++p;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001862 digits = p; // remember where the digits start
Bram Moolenaar4b471622019-01-31 13:48:09 +01001863 n = getdigits(&p);
1864 divider = 0;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001865 if (*p == '.') // ".5s" means a fraction
Bram Moolenaar4b471622019-01-31 13:48:09 +01001866 {
1867 fraction = atol((char *)++p);
1868 while (VIM_ISDIGIT(*p))
1869 {
1870 ++p;
1871 if (divider)
1872 divider *= 10;
1873 else
1874 divider = 10;
1875 }
1876 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001877 if (*p == 's') // "2s" means two times 'shiftwidth'
Bram Moolenaar4b471622019-01-31 13:48:09 +01001878 {
1879 if (p == digits)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001880 n = sw; // just "s" is one 'shiftwidth'
Bram Moolenaar4b471622019-01-31 13:48:09 +01001881 else
1882 {
1883 n *= sw;
1884 if (divider)
1885 n += (sw * fraction + divider / 2) / divider;
1886 }
1887 ++p;
1888 }
1889 if (l[1] == '-')
1890 n = -n;
1891
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001892 // When adding an entry here, also update the default 'cinoptions' in
1893 // doc/indent.txt, and add explanation for it!
Bram Moolenaar4b471622019-01-31 13:48:09 +01001894 switch (*l)
1895 {
1896 case '>': buf->b_ind_level = n; break;
1897 case 'e': buf->b_ind_open_imag = n; break;
1898 case 'n': buf->b_ind_no_brace = n; break;
1899 case 'f': buf->b_ind_first_open = n; break;
1900 case '{': buf->b_ind_open_extra = n; break;
1901 case '}': buf->b_ind_close_extra = n; break;
1902 case '^': buf->b_ind_open_left_imag = n; break;
1903 case 'L': buf->b_ind_jump_label = n; break;
1904 case ':': buf->b_ind_case = n; break;
1905 case '=': buf->b_ind_case_code = n; break;
1906 case 'b': buf->b_ind_case_break = n; break;
1907 case 'p': buf->b_ind_param = n; break;
1908 case 't': buf->b_ind_func_type = n; break;
1909 case '/': buf->b_ind_comment = n; break;
1910 case 'c': buf->b_ind_in_comment = n; break;
1911 case 'C': buf->b_ind_in_comment2 = n; break;
1912 case 'i': buf->b_ind_cpp_baseclass = n; break;
1913 case '+': buf->b_ind_continuation = n; break;
1914 case '(': buf->b_ind_unclosed = n; break;
1915 case 'u': buf->b_ind_unclosed2 = n; break;
1916 case 'U': buf->b_ind_unclosed_noignore = n; break;
1917 case 'W': buf->b_ind_unclosed_wrapped = n; break;
1918 case 'w': buf->b_ind_unclosed_whiteok = n; break;
1919 case 'm': buf->b_ind_matching_paren = n; break;
1920 case 'M': buf->b_ind_paren_prev = n; break;
1921 case ')': buf->b_ind_maxparen = n; break;
1922 case '*': buf->b_ind_maxcomment = n; break;
1923 case 'g': buf->b_ind_scopedecl = n; break;
1924 case 'h': buf->b_ind_scopedecl_code = n; break;
1925 case 'j': buf->b_ind_java = n; break;
1926 case 'J': buf->b_ind_js = n; break;
1927 case 'l': buf->b_ind_keep_case_label = n; break;
1928 case '#': buf->b_ind_hash_comment = n; break;
1929 case 'N': buf->b_ind_cpp_namespace = n; break;
1930 case 'k': buf->b_ind_if_for_while = n; break;
1931 case 'E': buf->b_ind_cpp_extern_c = n; break;
1932 }
1933 if (*p == ',')
1934 ++p;
1935 }
1936}
1937
1938/*
1939 * Return the desired indent for C code.
1940 * Return -1 if the indent should be left alone (inside a raw string).
1941 */
1942 int
1943get_c_indent(void)
1944{
1945 pos_T cur_curpos;
1946 int amount;
1947 int scope_amount;
1948 int cur_amount = MAXCOL;
1949 colnr_T col;
1950 char_u *theline;
1951 char_u *linecopy;
1952 pos_T *trypos;
1953 pos_T *comment_pos;
1954 pos_T *tryposBrace = NULL;
1955 pos_T tryposCopy;
1956 pos_T our_paren_pos;
1957 char_u *start;
1958 int start_brace;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001959#define BRACE_IN_COL0 1 // '{' is in column 0
1960#define BRACE_AT_START 2 // '{' is at start of line
1961#define BRACE_AT_END 3 // '{' is at end of line
Bram Moolenaar4b471622019-01-31 13:48:09 +01001962 linenr_T ourscope;
1963 char_u *l;
1964 char_u *look;
1965 char_u terminated;
1966 int lookfor;
1967#define LOOKFOR_INITIAL 0
1968#define LOOKFOR_IF 1
1969#define LOOKFOR_DO 2
1970#define LOOKFOR_CASE 3
1971#define LOOKFOR_ANY 4
1972#define LOOKFOR_TERM 5
1973#define LOOKFOR_UNTERM 6
1974#define LOOKFOR_SCOPEDECL 7
1975#define LOOKFOR_NOBREAK 8
1976#define LOOKFOR_CPP_BASECLASS 9
1977#define LOOKFOR_ENUM_OR_INIT 10
1978#define LOOKFOR_JS_KEY 11
1979#define LOOKFOR_COMMA 12
1980
1981 int whilelevel;
1982 linenr_T lnum;
1983 int n;
1984 int iscase;
1985 int lookfor_break;
1986 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001987 int cont_amount = 0; // amount for continuation line
Bram Moolenaar4b471622019-01-31 13:48:09 +01001988 int original_line_islabel;
1989 int added_to_amount = 0;
1990 int js_cur_has_key = 0;
1991 linenr_T raw_string_start = 0;
1992 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
1993
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001994 // make a copy, value is changed below
Bram Moolenaar4b471622019-01-31 13:48:09 +01001995 int ind_continuation = curbuf->b_ind_continuation;
1996
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01001997 // remember where the cursor was when we started
Bram Moolenaar4b471622019-01-31 13:48:09 +01001998 cur_curpos = curwin->w_cursor;
1999
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002000 // if we are at line 1 zero indent is fine, right?
Bram Moolenaar4b471622019-01-31 13:48:09 +01002001 if (cur_curpos.lnum == 1)
2002 return 0;
2003
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002004 // Get a copy of the current contents of the line.
2005 // This is required, because only the most recent line obtained with
2006 // ml_get is valid!
Bram Moolenaar4b471622019-01-31 13:48:09 +01002007 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
2008 if (linecopy == NULL)
2009 return 0;
2010
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002011 // In insert mode and the cursor is on a ')' truncate the line at the
2012 // cursor position. We don't want to line up with the matching '(' when
2013 // inserting new stuff.
2014 // For unknown reasons the cursor might be past the end of the line, thus
2015 // check for that.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002016 if ((State & INSERT)
2017 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
2018 && linecopy[curwin->w_cursor.col] == ')')
2019 linecopy[curwin->w_cursor.col] = NUL;
2020
2021 theline = skipwhite(linecopy);
2022
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002023 // move the cursor to the start of the line
Bram Moolenaar4b471622019-01-31 13:48:09 +01002024
2025 curwin->w_cursor.col = 0;
2026
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002027 original_line_islabel = cin_islabel(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002028
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002029 // If we are inside a raw string don't change the indent.
2030 // Ignore a raw string inside a comment.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002031 comment_pos = ind_find_start_comment();
2032 if (comment_pos != NULL)
2033 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002034 // findmatchlimit() static pos is overwritten, make a copy
Bram Moolenaar4b471622019-01-31 13:48:09 +01002035 tryposCopy = *comment_pos;
2036 comment_pos = &tryposCopy;
2037 }
2038 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
2039 if (trypos != NULL && (comment_pos == NULL
2040 || LT_POS(*trypos, *comment_pos)))
2041 {
2042 amount = -1;
2043 goto laterend;
2044 }
2045
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002046 // #defines and so on always go at the left when included in 'cinkeys'.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002047 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
2048 {
2049 amount = curbuf->b_ind_hash_comment;
2050 goto theend;
2051 }
2052
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002053 // Is it a non-case label? Then that goes at the left margin too unless:
2054 // - JS flag is set.
2055 // - 'L' item has a positive value.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002056 if (original_line_islabel && !curbuf->b_ind_js
2057 && curbuf->b_ind_jump_label < 0)
2058 {
2059 amount = 0;
2060 goto theend;
2061 }
2062
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002063 // If we're inside a "//" comment and there is a "//" comment in a
2064 // previous line, lineup with that one.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002065 if (cin_islinecomment(theline)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002066 && (trypos = find_line_comment()) != NULL) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002067 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002068 // find how indented the line beginning the comment is
Bram Moolenaar4b471622019-01-31 13:48:09 +01002069 getvcol(curwin, trypos, &col, NULL, NULL);
2070 amount = col;
2071 goto theend;
2072 }
2073
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002074 // If we're inside a comment and not looking at the start of the
2075 // comment, try using the 'comments' option.
2076 if (!cin_iscomment(theline) && comment_pos != NULL) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002077 {
2078 int lead_start_len = 2;
2079 int lead_middle_len = 1;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002080 char_u lead_start[COM_MAX_LEN]; // start-comment string
2081 char_u lead_middle[COM_MAX_LEN]; // middle-comment string
2082 char_u lead_end[COM_MAX_LEN]; // end-comment string
Bram Moolenaar4b471622019-01-31 13:48:09 +01002083 char_u *p;
2084 int start_align = 0;
2085 int start_off = 0;
2086 int done = FALSE;
2087
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002088 // find how indented the line beginning the comment is
Bram Moolenaar4b471622019-01-31 13:48:09 +01002089 getvcol(curwin, comment_pos, &col, NULL, NULL);
2090 amount = col;
2091 *lead_start = NUL;
2092 *lead_middle = NUL;
2093
2094 p = curbuf->b_p_com;
2095 while (*p != NUL)
2096 {
2097 int align = 0;
2098 int off = 0;
2099 int what = 0;
2100
2101 while (*p != NUL && *p != ':')
2102 {
2103 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
2104 what = *p++;
2105 else if (*p == COM_LEFT || *p == COM_RIGHT)
2106 align = *p++;
2107 else if (VIM_ISDIGIT(*p) || *p == '-')
2108 off = getdigits(&p);
2109 else
2110 ++p;
2111 }
2112
2113 if (*p == ':')
2114 ++p;
2115 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
2116 if (what == COM_START)
2117 {
2118 STRCPY(lead_start, lead_end);
2119 lead_start_len = (int)STRLEN(lead_start);
2120 start_off = off;
2121 start_align = align;
2122 }
2123 else if (what == COM_MIDDLE)
2124 {
2125 STRCPY(lead_middle, lead_end);
2126 lead_middle_len = (int)STRLEN(lead_middle);
2127 }
2128 else if (what == COM_END)
2129 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002130 // If our line starts with the middle comment string, line it
2131 // up with the comment opener per the 'comments' option.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002132 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
2133 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
2134 {
2135 done = TRUE;
2136 if (curwin->w_cursor.lnum > 1)
2137 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002138 // If the start comment string matches in the previous
2139 // line, use the indent of that line plus offset. If
2140 // the middle comment string matches in the previous
2141 // line, use the indent of that line. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002142 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
2143 if (STRNCMP(look, lead_start, lead_start_len) == 0)
2144 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
2145 else if (STRNCMP(look, lead_middle,
2146 lead_middle_len) == 0)
2147 {
2148 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
2149 break;
2150 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002151 // If the start comment string doesn't match with the
2152 // start of the comment, skip this entry. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002153 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
2154 lead_start, lead_start_len) != 0)
2155 continue;
2156 }
2157 if (start_off != 0)
2158 amount += start_off;
2159 else if (start_align == COM_RIGHT)
2160 amount += vim_strsize(lead_start)
2161 - vim_strsize(lead_middle);
2162 break;
2163 }
2164
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002165 // If our line starts with the end comment string, line it up
2166 // with the middle comment
Bram Moolenaar4b471622019-01-31 13:48:09 +01002167 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
2168 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
2169 {
2170 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002171 // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002172 if (off != 0)
2173 amount += off;
2174 else if (align == COM_RIGHT)
2175 amount += vim_strsize(lead_start)
2176 - vim_strsize(lead_middle);
2177 done = TRUE;
2178 break;
2179 }
2180 }
2181 }
2182
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002183 // If our line starts with an asterisk, line up with the
2184 // asterisk in the comment opener; otherwise, line up
2185 // with the first character of the comment text.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002186 if (done)
2187 ;
2188 else if (theline[0] == '*')
2189 amount += 1;
2190 else
2191 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002192 // If we are more than one line away from the comment opener, take
2193 // the indent of the previous non-empty line. If 'cino' has "CO"
2194 // and we are just below the comment opener and there are any
2195 // white characters after it line up with the text after it;
2196 // otherwise, add the amount specified by "c" in 'cino'
Bram Moolenaar4b471622019-01-31 13:48:09 +01002197 amount = -1;
2198 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
2199 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002200 if (linewhite(lnum)) // skip blank lines
Bram Moolenaar4b471622019-01-31 13:48:09 +01002201 continue;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002202 amount = get_indent_lnum(lnum); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002203 break;
2204 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002205 if (amount == -1) // use the comment opener
Bram Moolenaar4b471622019-01-31 13:48:09 +01002206 {
2207 if (!curbuf->b_ind_in_comment2)
2208 {
2209 start = ml_get(comment_pos->lnum);
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002210 look = start + comment_pos->col + 2; // skip / and *
2211 if (*look != NUL) // if something after it
Bram Moolenaar4b471622019-01-31 13:48:09 +01002212 comment_pos->col = (colnr_T)(skipwhite(look) - start);
2213 }
2214 getvcol(curwin, comment_pos, &col, NULL, NULL);
2215 amount = col;
2216 if (curbuf->b_ind_in_comment2 || *look == NUL)
2217 amount += curbuf->b_ind_in_comment;
2218 }
2219 }
2220 goto theend;
2221 }
2222
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002223 // Are we looking at a ']' that has a match?
Bram Moolenaar4b471622019-01-31 13:48:09 +01002224 if (*skipwhite(theline) == ']'
2225 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
2226 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002227 // align with the line containing the '['.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002228 amount = get_indent_lnum(trypos->lnum);
2229 goto theend;
2230 }
2231
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002232 // Are we inside parentheses or braces? XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002233 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
2234 && curbuf->b_ind_java == 0)
2235 || (tryposBrace = find_start_brace()) != NULL
2236 || trypos != NULL)
2237 {
2238 if (trypos != NULL && tryposBrace != NULL)
2239 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002240 // Both an unmatched '(' and '{' is found. Use the one which is
2241 // closer to the current cursor position, set the other to NULL.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002242 if (trypos->lnum != tryposBrace->lnum
2243 ? trypos->lnum < tryposBrace->lnum
2244 : trypos->col < tryposBrace->col)
2245 trypos = NULL;
2246 else
2247 tryposBrace = NULL;
2248 }
2249
2250 if (trypos != NULL)
2251 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002252 // If the matching paren is more than one line away, use the indent of
2253 // a previous non-empty line that matches the same paren.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002254 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
2255 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002256 // Line up with the start of the matching paren line.
2257 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002258 }
2259 else
2260 {
2261 amount = -1;
2262 our_paren_pos = *trypos;
2263 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
2264 {
2265 l = skipwhite(ml_get(lnum));
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002266 if (cin_nocode(l)) // skip comment lines
Bram Moolenaar4b471622019-01-31 13:48:09 +01002267 continue;
2268 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002269 continue; // ignore #define, #if, etc.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002270 curwin->w_cursor.lnum = lnum;
2271
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002272 // Skip a comment or raw string. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002273 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
2274 {
2275 lnum = trypos->lnum + 1;
2276 continue;
2277 }
2278
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002279 // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002280 if ((trypos = find_match_paren(
2281 corr_ind_maxparen(&cur_curpos))) != NULL
2282 && trypos->lnum == our_paren_pos.lnum
2283 && trypos->col == our_paren_pos.col)
2284 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002285 amount = get_indent_lnum(lnum); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002286
2287 if (theline[0] == ')')
2288 {
2289 if (our_paren_pos.lnum != lnum
2290 && cur_amount > amount)
2291 cur_amount = amount;
2292 amount = -1;
2293 }
2294 break;
2295 }
2296 }
2297 }
2298
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002299 // Line up with line where the matching paren is. XXX
2300 // If the line starts with a '(' or the indent for unclosed
2301 // parentheses is zero, line up with the unclosed parentheses.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002302 if (amount == -1)
2303 {
2304 int ignore_paren_col = 0;
2305 int is_if_for_while = 0;
2306
2307 if (curbuf->b_ind_if_for_while)
2308 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002309 // Look for the outermost opening parenthesis on this line
2310 // and check whether it belongs to an "if", "for" or "while".
Bram Moolenaar4b471622019-01-31 13:48:09 +01002311
2312 pos_T cursor_save = curwin->w_cursor;
2313 pos_T outermost;
2314 char_u *line;
2315
2316 trypos = &our_paren_pos;
2317 do {
2318 outermost = *trypos;
2319 curwin->w_cursor.lnum = outermost.lnum;
2320 curwin->w_cursor.col = outermost.col;
2321
2322 trypos = find_match_paren(curbuf->b_ind_maxparen);
2323 } while (trypos && trypos->lnum == outermost.lnum);
2324
2325 curwin->w_cursor = cursor_save;
2326
2327 line = ml_get(outermost.lnum);
2328
2329 is_if_for_while =
2330 cin_is_if_for_while_before_offset(line, &outermost.col);
2331 }
2332
2333 amount = skip_label(our_paren_pos.lnum, &look);
2334 look = skipwhite(look);
2335 if (*look == '(')
2336 {
2337 linenr_T save_lnum = curwin->w_cursor.lnum;
2338 char_u *line;
2339 int look_col;
2340
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002341 // Ignore a '(' in front of the line that has a match before
2342 // our matching '('.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002343 curwin->w_cursor.lnum = our_paren_pos.lnum;
2344 line = ml_get_curline();
2345 look_col = (int)(look - line);
2346 curwin->w_cursor.col = look_col + 1;
2347 if ((trypos = findmatchlimit(NULL, ')', 0,
2348 curbuf->b_ind_maxparen))
2349 != NULL
2350 && trypos->lnum == our_paren_pos.lnum
2351 && trypos->col < our_paren_pos.col)
2352 ignore_paren_col = trypos->col + 1;
2353
2354 curwin->w_cursor.lnum = save_lnum;
2355 look = ml_get(our_paren_pos.lnum) + look_col;
2356 }
2357 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
2358 && is_if_for_while == 0)
2359 || (!curbuf->b_ind_unclosed_noignore && *look == '('
2360 && ignore_paren_col == 0))
2361 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002362 // If we're looking at a close paren, line up right there;
2363 // otherwise, line up with the next (non-white) character.
2364 // When b_ind_unclosed_wrapped is set and the matching paren is
2365 // the last nonwhite character of the line, use either the
2366 // indent of the current line or the indentation of the next
2367 // outer paren and add b_ind_unclosed_wrapped (for very long
2368 // lines).
Bram Moolenaar4b471622019-01-31 13:48:09 +01002369 if (theline[0] != ')')
2370 {
2371 cur_amount = MAXCOL;
2372 l = ml_get(our_paren_pos.lnum);
2373 if (curbuf->b_ind_unclosed_wrapped
2374 && cin_ends_in(l, (char_u *)"(", NULL))
2375 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002376 // look for opening unmatched paren, indent one level
2377 // for each additional level
Bram Moolenaar4b471622019-01-31 13:48:09 +01002378 n = 1;
2379 for (col = 0; col < our_paren_pos.col; ++col)
2380 {
2381 switch (l[col])
2382 {
2383 case '(':
2384 case '{': ++n;
2385 break;
2386
2387 case ')':
2388 case '}': if (n > 1)
2389 --n;
2390 break;
2391 }
2392 }
2393
2394 our_paren_pos.col = 0;
2395 amount += n * curbuf->b_ind_unclosed_wrapped;
2396 }
2397 else if (curbuf->b_ind_unclosed_whiteok)
2398 our_paren_pos.col++;
2399 else
2400 {
2401 col = our_paren_pos.col + 1;
2402 while (VIM_ISWHITE(l[col]))
2403 col++;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002404 if (l[col] != NUL) // In case of trailing space
Bram Moolenaar4b471622019-01-31 13:48:09 +01002405 our_paren_pos.col = col;
2406 else
2407 our_paren_pos.col++;
2408 }
2409 }
2410
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002411 // Find how indented the paren is, or the character after it
2412 // if we did the above "if".
Bram Moolenaar4b471622019-01-31 13:48:09 +01002413 if (our_paren_pos.col > 0)
2414 {
2415 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
2416 if (cur_amount > (int)col)
2417 cur_amount = col;
2418 }
2419 }
2420
2421 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
2422 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002423 // Line up with the start of the matching paren line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002424 }
2425 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
2426 || (!curbuf->b_ind_unclosed_noignore
2427 && *look == '(' && ignore_paren_col == 0))
2428 {
2429 if (cur_amount != MAXCOL)
2430 amount = cur_amount;
2431 }
2432 else
2433 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002434 // Add b_ind_unclosed2 for each '(' before our matching one,
2435 // but ignore (void) before the line (ignore_paren_col).
Bram Moolenaar4b471622019-01-31 13:48:09 +01002436 col = our_paren_pos.col;
2437 while ((int)our_paren_pos.col > ignore_paren_col)
2438 {
2439 --our_paren_pos.col;
2440 switch (*ml_get_pos(&our_paren_pos))
2441 {
2442 case '(': amount += curbuf->b_ind_unclosed2;
2443 col = our_paren_pos.col;
2444 break;
2445 case ')': amount -= curbuf->b_ind_unclosed2;
2446 col = MAXCOL;
2447 break;
2448 }
2449 }
2450
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002451 // Use b_ind_unclosed once, when the first '(' is not inside
2452 // braces
Bram Moolenaar4b471622019-01-31 13:48:09 +01002453 if (col == MAXCOL)
2454 amount += curbuf->b_ind_unclosed;
2455 else
2456 {
2457 curwin->w_cursor.lnum = our_paren_pos.lnum;
2458 curwin->w_cursor.col = col;
2459 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
2460 != NULL)
2461 amount += curbuf->b_ind_unclosed2;
2462 else
2463 {
2464 if (is_if_for_while)
2465 amount += curbuf->b_ind_if_for_while;
2466 else
2467 amount += curbuf->b_ind_unclosed;
2468 }
2469 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002470 // For a line starting with ')' use the minimum of the two
2471 // positions, to avoid giving it more indent than the previous
2472 // lines:
2473 // func_long_name( if (x
2474 // arg && yy
2475 // ) ^ not here ) ^ not here
Bram Moolenaar4b471622019-01-31 13:48:09 +01002476 if (cur_amount < amount)
2477 amount = cur_amount;
2478 }
2479 }
2480
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002481 // add extra indent for a comment
Bram Moolenaar4b471622019-01-31 13:48:09 +01002482 if (cin_iscomment(theline))
2483 amount += curbuf->b_ind_comment;
2484 }
2485 else
2486 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002487 // We are inside braces, there is a { before this line at the position
2488 // stored in tryposBrace.
2489 // Make a copy of tryposBrace, it may point to pos_copy inside
2490 // find_start_brace(), which may be changed somewhere.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002491 tryposCopy = *tryposBrace;
2492 tryposBrace = &tryposCopy;
2493 trypos = tryposBrace;
2494 ourscope = trypos->lnum;
2495 start = ml_get(ourscope);
2496
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002497 // Now figure out how indented the line is in general.
2498 // If the brace was at the start of the line, we use that;
2499 // otherwise, check out the indentation of the line as
2500 // a whole and then add the "imaginary indent" to that.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002501 look = skipwhite(start);
2502 if (*look == '{')
2503 {
2504 getvcol(curwin, trypos, &col, NULL, NULL);
2505 amount = col;
2506 if (*start == '{')
2507 start_brace = BRACE_IN_COL0;
2508 else
2509 start_brace = BRACE_AT_START;
2510 }
2511 else
2512 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002513 // That opening brace might have been on a continuation
2514 // line. if so, find the start of the line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002515 curwin->w_cursor.lnum = ourscope;
2516
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002517 // Position the cursor over the rightmost paren, so that
2518 // matching it will take us back to the start of the line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002519 lnum = ourscope;
2520 if (find_last_paren(start, '(', ')')
2521 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
2522 != NULL)
2523 lnum = trypos->lnum;
2524
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002525 // It could have been something like
2526 // case 1: if (asdf &&
2527 // ldfd) {
2528 // }
Bram Moolenaar4b471622019-01-31 13:48:09 +01002529 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
2530 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
2531 amount = get_indent();
2532 else if (curbuf->b_ind_js)
2533 amount = get_indent_lnum(lnum);
2534 else
2535 amount = skip_label(lnum, &l);
2536
2537 start_brace = BRACE_AT_END;
2538 }
2539
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002540 // For Javascript check if the line starts with "key:".
Bram Moolenaar4b471622019-01-31 13:48:09 +01002541 if (curbuf->b_ind_js)
2542 js_cur_has_key = cin_has_js_key(theline);
2543
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002544 // If we're looking at a closing brace, that's where
2545 // we want to be. otherwise, add the amount of room
2546 // that an indent is supposed to be.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002547 if (theline[0] == '}')
2548 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002549 // they may want closing braces to line up with something
2550 // other than the open brace. indulge them, if so.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002551 amount += curbuf->b_ind_close_extra;
2552 }
2553 else
2554 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002555 // If we're looking at an "else", try to find an "if"
2556 // to match it with.
2557 // If we're looking at a "while", try to find a "do"
2558 // to match it with.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002559 lookfor = LOOKFOR_INITIAL;
2560 if (cin_iselse(theline))
2561 lookfor = LOOKFOR_IF;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002562 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002563 lookfor = LOOKFOR_DO;
2564 if (lookfor != LOOKFOR_INITIAL)
2565 {
2566 curwin->w_cursor.lnum = cur_curpos.lnum;
2567 if (find_match(lookfor, ourscope) == OK)
2568 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002569 amount = get_indent(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002570 goto theend;
2571 }
2572 }
2573
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002574 // We get here if we are not on an "while-of-do" or "else" (or
2575 // failed to find a matching "if").
2576 // Search backwards for something to line up with.
2577 // First set amount for when we don't find anything.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002578
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002579 // if the '{' is _really_ at the left margin, use the imaginary
2580 // location of a left-margin brace. Otherwise, correct the
2581 // location for b_ind_open_extra.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002582
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002583 if (start_brace == BRACE_IN_COL0) // '{' is in column 0
Bram Moolenaar4b471622019-01-31 13:48:09 +01002584 {
2585 amount = curbuf->b_ind_open_left_imag;
2586 lookfor_cpp_namespace = TRUE;
2587 }
2588 else if (start_brace == BRACE_AT_START &&
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002589 lookfor_cpp_namespace) // '{' is at start
Bram Moolenaar4b471622019-01-31 13:48:09 +01002590 {
2591
2592 lookfor_cpp_namespace = TRUE;
2593 }
2594 else
2595 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002596 if (start_brace == BRACE_AT_END) // '{' is at end of line
Bram Moolenaar4b471622019-01-31 13:48:09 +01002597 {
2598 amount += curbuf->b_ind_open_imag;
2599
2600 l = skipwhite(ml_get_curline());
2601 if (cin_is_cpp_namespace(l))
2602 amount += curbuf->b_ind_cpp_namespace;
2603 else if (cin_is_cpp_extern_c(l))
2604 amount += curbuf->b_ind_cpp_extern_c;
2605 }
2606 else
2607 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002608 // Compensate for adding b_ind_open_extra later.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002609 amount -= curbuf->b_ind_open_extra;
2610 if (amount < 0)
2611 amount = 0;
2612 }
2613 }
2614
2615 lookfor_break = FALSE;
2616
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002617 if (cin_iscase(theline, FALSE)) // it's a switch() label
Bram Moolenaar4b471622019-01-31 13:48:09 +01002618 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002619 lookfor = LOOKFOR_CASE; // find a previous switch() label
Bram Moolenaar4b471622019-01-31 13:48:09 +01002620 amount += curbuf->b_ind_case;
2621 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002622 else if (cin_isscopedecl(theline)) // private:, ...
Bram Moolenaar4b471622019-01-31 13:48:09 +01002623 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002624 lookfor = LOOKFOR_SCOPEDECL; // class decl is this block
Bram Moolenaar4b471622019-01-31 13:48:09 +01002625 amount += curbuf->b_ind_scopedecl;
2626 }
2627 else
2628 {
2629 if (curbuf->b_ind_case_break && cin_isbreak(theline))
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002630 // break; ...
Bram Moolenaar4b471622019-01-31 13:48:09 +01002631 lookfor_break = TRUE;
2632
2633 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002634 // b_ind_level from start of block
Bram Moolenaar4b471622019-01-31 13:48:09 +01002635 amount += curbuf->b_ind_level;
2636 }
2637 scope_amount = amount;
2638 whilelevel = 0;
2639
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002640 // Search backwards. If we find something we recognize, line up
2641 // with that.
2642 //
2643 // If we're looking at an open brace, indent
2644 // the usual amount relative to the conditional
2645 // that opens the block.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002646 curwin->w_cursor = cur_curpos;
2647 for (;;)
2648 {
2649 curwin->w_cursor.lnum--;
2650 curwin->w_cursor.col = 0;
2651
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002652 // If we went all the way back to the start of our scope, line
2653 // up with it.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002654 if (curwin->w_cursor.lnum <= ourscope)
2655 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002656 // We reached end of scope:
2657 // If looking for a enum or structure initialization
2658 // go further back:
2659 // If it is an initializer (enum xxx or xxx =), then
2660 // don't add ind_continuation, otherwise it is a variable
2661 // declaration:
2662 // int x,
2663 // here; <-- add ind_continuation
Bram Moolenaar4b471622019-01-31 13:48:09 +01002664 if (lookfor == LOOKFOR_ENUM_OR_INIT)
2665 {
2666 if (curwin->w_cursor.lnum == 0
2667 || curwin->w_cursor.lnum
2668 < ourscope - curbuf->b_ind_maxparen)
2669 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002670 // nothing found (abuse curbuf->b_ind_maxparen as
2671 // limit) assume terminated line (i.e. a variable
2672 // initialization)
Bram Moolenaar4b471622019-01-31 13:48:09 +01002673 if (cont_amount > 0)
2674 amount = cont_amount;
2675 else if (!curbuf->b_ind_js)
2676 amount += ind_continuation;
2677 break;
2678 }
2679
2680 l = ml_get_curline();
2681
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002682 // If we're in a comment or raw string now, skip to
2683 // the start of it.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002684 trypos = ind_find_start_CORS(NULL);
2685 if (trypos != NULL)
2686 {
2687 curwin->w_cursor.lnum = trypos->lnum + 1;
2688 curwin->w_cursor.col = 0;
2689 continue;
2690 }
2691
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002692 // Skip preprocessor directives and blank lines.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002693 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
2694 &amount))
2695 continue;
2696
2697 if (cin_nocode(l))
2698 continue;
2699
2700 terminated = cin_isterminated(l, FALSE, TRUE);
2701
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002702 // If we are at top level and the line looks like a
2703 // function declaration, we are done
2704 // (it's a variable declaration).
Bram Moolenaar4b471622019-01-31 13:48:09 +01002705 if (start_brace != BRACE_IN_COL0
2706 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
2707 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002708 // if the line is terminated with another ','
2709 // it is a continued variable initialization.
2710 // don't add extra indent.
2711 // TODO: does not work, if a function
2712 // declaration is split over multiple lines:
2713 // cin_isfuncdecl returns FALSE then.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002714 if (terminated == ',')
2715 break;
2716
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002717 // if it es a enum declaration or an assignment,
2718 // we are done.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002719 if (terminated != ';' && cin_isinit())
2720 break;
2721
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002722 // nothing useful found
Bram Moolenaar4b471622019-01-31 13:48:09 +01002723 if (terminated == 0 || terminated == '{')
2724 continue;
2725 }
2726
2727 if (terminated != ';')
2728 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002729 // Skip parens and braces. Position the cursor
2730 // over the rightmost paren, so that matching it
2731 // will take us back to the start of the line.
2732 // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002733 trypos = NULL;
2734 if (find_last_paren(l, '(', ')'))
2735 trypos = find_match_paren(
2736 curbuf->b_ind_maxparen);
2737
2738 if (trypos == NULL && find_last_paren(l, '{', '}'))
2739 trypos = find_start_brace();
2740
2741 if (trypos != NULL)
2742 {
2743 curwin->w_cursor.lnum = trypos->lnum + 1;
2744 curwin->w_cursor.col = 0;
2745 continue;
2746 }
2747 }
2748
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002749 // it's a variable declaration, add indentation
2750 // like in
2751 // int a,
2752 // b;
Bram Moolenaar4b471622019-01-31 13:48:09 +01002753 if (cont_amount > 0)
2754 amount = cont_amount;
2755 else
2756 amount += ind_continuation;
2757 }
2758 else if (lookfor == LOOKFOR_UNTERM)
2759 {
2760 if (cont_amount > 0)
2761 amount = cont_amount;
2762 else
2763 amount += ind_continuation;
2764 }
2765 else
2766 {
2767 if (lookfor != LOOKFOR_TERM
2768 && lookfor != LOOKFOR_CPP_BASECLASS
2769 && lookfor != LOOKFOR_COMMA)
2770 {
2771 amount = scope_amount;
2772 if (theline[0] == '{')
2773 {
2774 amount += curbuf->b_ind_open_extra;
2775 added_to_amount = curbuf->b_ind_open_extra;
2776 }
2777 }
2778
2779 if (lookfor_cpp_namespace)
2780 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002781 // Looking for C++ namespace, need to look further
2782 // back.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002783 if (curwin->w_cursor.lnum == ourscope)
2784 continue;
2785
2786 if (curwin->w_cursor.lnum == 0
2787 || curwin->w_cursor.lnum
2788 < ourscope - FIND_NAMESPACE_LIM)
2789 break;
2790
2791 l = ml_get_curline();
2792
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002793 // If we're in a comment or raw string now, skip
2794 // to the start of it.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002795 trypos = ind_find_start_CORS(NULL);
2796 if (trypos != NULL)
2797 {
2798 curwin->w_cursor.lnum = trypos->lnum + 1;
2799 curwin->w_cursor.col = 0;
2800 continue;
2801 }
2802
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002803 // Skip preprocessor directives and blank lines.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002804 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
2805 &amount))
2806 continue;
2807
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002808 // Finally the actual check for "namespace".
Bram Moolenaar4b471622019-01-31 13:48:09 +01002809 if (cin_is_cpp_namespace(l))
2810 {
2811 amount += curbuf->b_ind_cpp_namespace
2812 - added_to_amount;
2813 break;
2814 }
2815 else if (cin_is_cpp_extern_c(l))
2816 {
2817 amount += curbuf->b_ind_cpp_extern_c
2818 - added_to_amount;
2819 break;
2820 }
2821
2822 if (cin_nocode(l))
2823 continue;
2824 }
2825 }
2826 break;
2827 }
2828
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002829 // If we're in a comment or raw string now, skip to the start
2830 // of it. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002831 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
2832 {
2833 curwin->w_cursor.lnum = trypos->lnum + 1;
2834 curwin->w_cursor.col = 0;
2835 continue;
2836 }
2837
2838 l = ml_get_curline();
2839
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002840 // If this is a switch() label, may line up relative to that.
2841 // If this is a C++ scope declaration, do the same.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002842 iscase = cin_iscase(l, FALSE);
2843 if (iscase || cin_isscopedecl(l))
2844 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002845 // we are only looking for cpp base class
2846 // declaration/initialization any longer
Bram Moolenaar4b471622019-01-31 13:48:09 +01002847 if (lookfor == LOOKFOR_CPP_BASECLASS)
2848 break;
2849
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002850 // When looking for a "do" we are not interested in
2851 // labels.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002852 if (whilelevel > 0)
2853 continue;
2854
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002855 // case xx:
2856 // c = 99 + <- this indent plus continuation
2857 //-> here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01002858 if (lookfor == LOOKFOR_UNTERM
2859 || lookfor == LOOKFOR_ENUM_OR_INIT)
2860 {
2861 if (cont_amount > 0)
2862 amount = cont_amount;
2863 else
2864 amount += ind_continuation;
2865 break;
2866 }
2867
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002868 // case xx: <- line up with this case
2869 // x = 333;
2870 // case yy:
Bram Moolenaar4b471622019-01-31 13:48:09 +01002871 if ( (iscase && lookfor == LOOKFOR_CASE)
2872 || (iscase && lookfor_break)
2873 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
2874 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002875 // Check that this case label is not for another
2876 // switch() XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002877 if ((trypos = find_start_brace()) == NULL
2878 || trypos->lnum == ourscope)
2879 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002880 amount = get_indent(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002881 break;
2882 }
2883 continue;
2884 }
2885
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002886 n = get_indent_nolabel(curwin->w_cursor.lnum); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002887
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002888 // case xx: if (cond) <- line up with this if
2889 // y = y + 1;
2890 // -> s = 99;
2891 //
2892 // case xx:
2893 // if (cond) <- line up with this line
2894 // y = y + 1;
2895 // -> s = 99;
Bram Moolenaar4b471622019-01-31 13:48:09 +01002896 if (lookfor == LOOKFOR_TERM)
2897 {
2898 if (n)
2899 amount = n;
2900
2901 if (!lookfor_break)
2902 break;
2903 }
2904
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002905 // case xx: x = x + 1; <- line up with this x
2906 // -> y = y + 1;
2907 //
2908 // case xx: if (cond) <- line up with this if
2909 // -> y = y + 1;
Bram Moolenaar4b471622019-01-31 13:48:09 +01002910 if (n)
2911 {
2912 amount = n;
2913 l = after_label(ml_get_curline());
2914 if (l != NULL && cin_is_cinword(l))
2915 {
2916 if (theline[0] == '{')
2917 amount += curbuf->b_ind_open_extra;
2918 else
2919 amount += curbuf->b_ind_level
2920 + curbuf->b_ind_no_brace;
2921 }
2922 break;
2923 }
2924
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002925 // Try to get the indent of a statement before the switch
2926 // label. If nothing is found, line up relative to the
2927 // switch label.
2928 // break; <- may line up with this line
2929 // case xx:
2930 // -> y = 1;
2931 scope_amount = get_indent() + (iscase // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002932 ? curbuf->b_ind_case_code
2933 : curbuf->b_ind_scopedecl_code);
2934 lookfor = curbuf->b_ind_case_break
2935 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
2936 continue;
2937 }
2938
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002939 // Looking for a switch() label or C++ scope declaration,
2940 // ignore other lines, skip {}-blocks.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002941 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
2942 {
2943 if (find_last_paren(l, '{', '}')
2944 && (trypos = find_start_brace()) != NULL)
2945 {
2946 curwin->w_cursor.lnum = trypos->lnum + 1;
2947 curwin->w_cursor.col = 0;
2948 }
2949 continue;
2950 }
2951
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002952 // Ignore jump labels with nothing after them.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002953 if (!curbuf->b_ind_js && cin_islabel())
2954 {
2955 l = after_label(ml_get_curline());
2956 if (l == NULL || cin_nocode(l))
2957 continue;
2958 }
2959
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002960 // Ignore #defines, #if, etc.
2961 // Ignore comment and empty lines.
2962 // (need to get the line again, cin_islabel() may have
2963 // unlocked it)
Bram Moolenaar4b471622019-01-31 13:48:09 +01002964 l = ml_get_curline();
2965 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
2966 || cin_nocode(l))
2967 continue;
2968
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002969 // Are we at the start of a cpp base class declaration or
2970 // constructor initialization? XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002971 n = FALSE;
2972 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
2973 {
2974 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
2975 l = ml_get_curline();
2976 }
2977 if (n)
2978 {
2979 if (lookfor == LOOKFOR_UNTERM)
2980 {
2981 if (cont_amount > 0)
2982 amount = cont_amount;
2983 else
2984 amount += ind_continuation;
2985 }
2986 else if (theline[0] == '{')
2987 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002988 // Need to find start of the declaration.
Bram Moolenaar4b471622019-01-31 13:48:09 +01002989 lookfor = LOOKFOR_UNTERM;
2990 ind_continuation = 0;
2991 continue;
2992 }
2993 else
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01002994 // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01002995 amount = get_baseclass_amount(
2996 cache_cpp_baseclass.lpos.col);
2997 break;
2998 }
2999 else if (lookfor == LOOKFOR_CPP_BASECLASS)
3000 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003001 // only look, whether there is a cpp base class
3002 // declaration or initialization before the opening brace.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003003 if (cin_isterminated(l, TRUE, FALSE))
3004 break;
3005 else
3006 continue;
3007 }
3008
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003009 // What happens next depends on the line being terminated.
3010 // If terminated with a ',' only consider it terminating if
3011 // there is another unterminated statement behind, eg:
3012 // 123,
3013 // sizeof
3014 // here
3015 // Otherwise check whether it is a enumeration or structure
3016 // initialisation (not indented) or a variable declaration
3017 // (indented).
Bram Moolenaar4b471622019-01-31 13:48:09 +01003018 terminated = cin_isterminated(l, FALSE, TRUE);
3019
3020 if (js_cur_has_key)
3021 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003022 js_cur_has_key = 0; // only check the first line
Bram Moolenaar4b471622019-01-31 13:48:09 +01003023 if (curbuf->b_ind_js && terminated == ',')
3024 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003025 // For Javascript we might be inside an object:
3026 // key: something, <- align with this
3027 // key: something
3028 // or:
3029 // key: something + <- align with this
3030 // something,
3031 // key: something
Bram Moolenaar4b471622019-01-31 13:48:09 +01003032 lookfor = LOOKFOR_JS_KEY;
3033 }
3034 }
3035 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
3036 {
3037 amount = get_indent();
3038 break;
3039 }
3040 if (lookfor == LOOKFOR_COMMA)
3041 {
3042 if (tryposBrace != NULL && tryposBrace->lnum
3043 >= curwin->w_cursor.lnum)
3044 break;
3045 if (terminated == ',')
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003046 // line below current line is the one that starts a
3047 // (possibly broken) line ending in a comma.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003048 break;
3049 else
3050 {
3051 amount = get_indent();
3052 if (curwin->w_cursor.lnum - 1 == ourscope)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003053 // line above is start of the scope, thus current
3054 // line is the one that stars a (possibly broken)
3055 // line ending in a comma.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003056 break;
3057 }
3058 }
3059
3060 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
3061 && terminated == ','))
3062 {
3063 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
3064 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
3065 amount += ind_continuation;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003066 // if we're in the middle of a paren thing,
3067 // go back to the line that starts it so
3068 // we can get the right prevailing indent
3069 // if ( foo &&
3070 // bar )
3071
3072 // Position the cursor over the rightmost paren, so that
3073 // matching it will take us back to the start of the line.
3074 // Ignore a match before the start of the block.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003075 (void)find_last_paren(l, '(', ')');
3076 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
3077 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
3078 || (trypos->lnum == tryposBrace->lnum
3079 && trypos->col < tryposBrace->col)))
3080 trypos = NULL;
3081
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003082 // If we are looking for ',', we also look for matching
3083 // braces.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003084 if (trypos == NULL && terminated == ','
3085 && find_last_paren(l, '{', '}'))
3086 trypos = find_start_brace();
3087
3088 if (trypos != NULL)
3089 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003090 // Check if we are on a case label now. This is
3091 // handled above.
3092 // case xx: if ( asdf &&
3093 // asdf)
Bram Moolenaar4b471622019-01-31 13:48:09 +01003094 curwin->w_cursor = *trypos;
3095 l = ml_get_curline();
3096 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
3097 {
3098 ++curwin->w_cursor.lnum;
3099 curwin->w_cursor.col = 0;
3100 continue;
3101 }
3102 }
3103
3104 /*
3105 * Skip over continuation lines to find the one to get the
3106 * indent from
3107 * char *usethis = "bla\
3108 * bla",
3109 * here;
3110 */
3111 if (terminated == ',')
3112 {
3113 while (curwin->w_cursor.lnum > 1)
3114 {
3115 l = ml_get(curwin->w_cursor.lnum - 1);
3116 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
3117 break;
3118 --curwin->w_cursor.lnum;
3119 curwin->w_cursor.col = 0;
3120 }
3121 }
3122
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003123 // Get indent and pointer to text for current line,
3124 // ignoring any jump label. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003125 if (curbuf->b_ind_js)
3126 cur_amount = get_indent();
3127 else
3128 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003129 // If this is just above the line we are indenting, and it
3130 // starts with a '{', line it up with this line.
3131 // while (not)
3132 // -> {
3133 // }
Bram Moolenaar4b471622019-01-31 13:48:09 +01003134 if (terminated != ',' && lookfor != LOOKFOR_TERM
3135 && theline[0] == '{')
3136 {
3137 amount = cur_amount;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003138 // Only add b_ind_open_extra when the current line
3139 // doesn't start with a '{', which must have a match
3140 // in the same line (scope is the same). Probably:
3141 // { 1, 2 },
3142 // -> { 3, 4 }
Bram Moolenaar4b471622019-01-31 13:48:09 +01003143 if (*skipwhite(l) != '{')
3144 amount += curbuf->b_ind_open_extra;
3145
3146 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
3147 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003148 // have to look back, whether it is a cpp base
3149 // class declaration or initialization
Bram Moolenaar4b471622019-01-31 13:48:09 +01003150 lookfor = LOOKFOR_CPP_BASECLASS;
3151 continue;
3152 }
3153 break;
3154 }
3155
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003156 // Check if we are after an "if", "while", etc.
3157 // Also allow " } else".
Bram Moolenaar4b471622019-01-31 13:48:09 +01003158 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
3159 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003160 // Found an unterminated line after an if (), line up
3161 // with the last one.
3162 // if (cond)
3163 // 100 +
3164 // -> here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003165 if (lookfor == LOOKFOR_UNTERM
3166 || lookfor == LOOKFOR_ENUM_OR_INIT)
3167 {
3168 if (cont_amount > 0)
3169 amount = cont_amount;
3170 else
3171 amount += ind_continuation;
3172 break;
3173 }
3174
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003175 // If this is just above the line we are indenting, we
3176 // are finished.
3177 // while (not)
3178 // -> here;
3179 // Otherwise this indent can be used when the line
3180 // before this is terminated.
3181 // yyy;
3182 // if (stat)
3183 // while (not)
3184 // xxx;
3185 // -> here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003186 amount = cur_amount;
3187 if (theline[0] == '{')
3188 amount += curbuf->b_ind_open_extra;
3189 if (lookfor != LOOKFOR_TERM)
3190 {
3191 amount += curbuf->b_ind_level
3192 + curbuf->b_ind_no_brace;
3193 break;
3194 }
3195
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003196 // Special trick: when expecting the while () after a
3197 // do, line up with the while()
3198 // do
3199 // x = 1;
3200 // -> here
Bram Moolenaar4b471622019-01-31 13:48:09 +01003201 l = skipwhite(ml_get_curline());
3202 if (cin_isdo(l))
3203 {
3204 if (whilelevel == 0)
3205 break;
3206 --whilelevel;
3207 }
3208
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003209 // When searching for a terminated line, don't use the
3210 // one between the "if" and the matching "else".
3211 // Need to use the scope of this "else". XXX
3212 // If whilelevel != 0 continue looking for a "do {".
Bram Moolenaar4b471622019-01-31 13:48:09 +01003213 if (cin_iselse(l) && whilelevel == 0)
3214 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003215 // If we're looking at "} else", let's make sure we
3216 // find the opening brace of the enclosing scope,
3217 // not the one from "if () {".
Bram Moolenaar4b471622019-01-31 13:48:09 +01003218 if (*l == '}')
3219 curwin->w_cursor.col =
3220 (colnr_T)(l - ml_get_curline()) + 1;
3221
3222 if ((trypos = find_start_brace()) == NULL
3223 || find_match(LOOKFOR_IF, trypos->lnum)
3224 == FAIL)
3225 break;
3226 }
3227 }
3228
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003229 // If we're below an unterminated line that is not an
3230 // "if" or something, we may line up with this line or
3231 // add something for a continuation line, depending on
3232 // the line before this one.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003233 else
3234 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003235 // Found two unterminated lines on a row, line up with
3236 // the last one.
3237 // c = 99 +
3238 // 100 +
3239 // -> here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003240 if (lookfor == LOOKFOR_UNTERM)
3241 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003242 // When line ends in a comma add extra indent
Bram Moolenaar4b471622019-01-31 13:48:09 +01003243 if (terminated == ',')
3244 amount += ind_continuation;
3245 break;
3246 }
3247
3248 if (lookfor == LOOKFOR_ENUM_OR_INIT)
3249 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003250 // Found two lines ending in ',', lineup with the
3251 // lowest one, but check for cpp base class
3252 // declaration/initialization, if it is an
3253 // opening brace or we are looking just for
3254 // enumerations/initializations.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003255 if (terminated == ',')
3256 {
3257 if (curbuf->b_ind_cpp_baseclass == 0)
3258 break;
3259
3260 lookfor = LOOKFOR_CPP_BASECLASS;
3261 continue;
3262 }
3263
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003264 // Ignore unterminated lines in between, but
3265 // reduce indent.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003266 if (amount > cur_amount)
3267 amount = cur_amount;
3268 }
3269 else
3270 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003271 // Found first unterminated line on a row, may
3272 // line up with this line, remember its indent
3273 // 100 +
3274 // -> here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003275 l = ml_get_curline();
3276 amount = cur_amount;
3277
3278 n = (int)STRLEN(l);
3279 if (terminated == ',' && (*skipwhite(l) == ']'
3280 || (n >=2 && l[n - 2] == ']')))
3281 break;
3282
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003283 // If previous line ends in ',', check whether we
3284 // are in an initialization or enum
3285 // struct xxx =
3286 // {
3287 // sizeof a,
3288 // 124 };
3289 // or a normal possible continuation line.
3290 // but only, of no other statement has been found
3291 // yet.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003292 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
3293 {
3294 if (curbuf->b_ind_js)
3295 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003296 // Search for a line ending in a comma
3297 // and line up with the line below it
3298 // (could be the current line).
3299 // some = [
3300 // 1, <- line up here
3301 // 2,
3302 // some = [
3303 // 3 + <- line up here
3304 // 4 *
3305 // 5,
3306 // 6,
Bram Moolenaar4b471622019-01-31 13:48:09 +01003307 if (cin_iscomment(skipwhite(l)))
3308 break;
3309 lookfor = LOOKFOR_COMMA;
3310 trypos = find_match_char('[',
3311 curbuf->b_ind_maxparen);
3312 if (trypos != NULL)
3313 {
3314 if (trypos->lnum
3315 == curwin->w_cursor.lnum - 1)
3316 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003317 // Current line is first inside
3318 // [], line up with it.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003319 break;
3320 }
3321 ourscope = trypos->lnum;
3322 }
3323 }
3324 else
3325 {
3326 lookfor = LOOKFOR_ENUM_OR_INIT;
3327 cont_amount = cin_first_id_amount();
3328 }
3329 }
3330 else
3331 {
3332 if (lookfor == LOOKFOR_INITIAL
3333 && *l != NUL
3334 && l[STRLEN(l) - 1] == '\\')
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003335 // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003336 cont_amount = cin_get_equal_amount(
3337 curwin->w_cursor.lnum);
3338 if (lookfor != LOOKFOR_TERM
3339 && lookfor != LOOKFOR_JS_KEY
3340 && lookfor != LOOKFOR_COMMA
3341 && raw_string_start != curwin->w_cursor.lnum)
3342 lookfor = LOOKFOR_UNTERM;
3343 }
3344 }
3345 }
3346 }
3347
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003348 // Check if we are after a while (cond);
3349 // If so: Ignore until the matching "do".
3350 else if (cin_iswhileofdo_end(terminated)) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003351 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003352 // Found an unterminated line after a while ();, line up
3353 // with the last one.
3354 // while (cond);
3355 // 100 + <- line up with this one
3356 // -> here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003357 if (lookfor == LOOKFOR_UNTERM
3358 || lookfor == LOOKFOR_ENUM_OR_INIT)
3359 {
3360 if (cont_amount > 0)
3361 amount = cont_amount;
3362 else
3363 amount += ind_continuation;
3364 break;
3365 }
3366
3367 if (whilelevel == 0)
3368 {
3369 lookfor = LOOKFOR_TERM;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003370 amount = get_indent(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003371 if (theline[0] == '{')
3372 amount += curbuf->b_ind_open_extra;
3373 }
3374 ++whilelevel;
3375 }
3376
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003377 // We are after a "normal" statement.
3378 // If we had another statement we can stop now and use the
3379 // indent of that other statement.
3380 // Otherwise the indent of the current statement may be used,
3381 // search backwards for the next "normal" statement.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003382 else
3383 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003384 // Skip single break line, if before a switch label. It
3385 // may be lined up with the case label.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003386 if (lookfor == LOOKFOR_NOBREAK
3387 && cin_isbreak(skipwhite(ml_get_curline())))
3388 {
3389 lookfor = LOOKFOR_ANY;
3390 continue;
3391 }
3392
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003393 // Handle "do {" line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003394 if (whilelevel > 0)
3395 {
3396 l = cin_skipcomment(ml_get_curline());
3397 if (cin_isdo(l))
3398 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003399 amount = get_indent(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003400 --whilelevel;
3401 continue;
3402 }
3403 }
3404
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003405 // Found a terminated line above an unterminated line. Add
3406 // the amount for a continuation line.
3407 // x = 1;
3408 // y = foo +
3409 // -> here;
3410 // or
3411 // int x = 1;
3412 // int foo,
3413 // -> here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003414 if (lookfor == LOOKFOR_UNTERM
3415 || lookfor == LOOKFOR_ENUM_OR_INIT)
3416 {
3417 if (cont_amount > 0)
3418 amount = cont_amount;
3419 else
3420 amount += ind_continuation;
3421 break;
3422 }
3423
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003424 // Found a terminated line above a terminated line or "if"
3425 // etc. line. Use the amount of the line below us.
3426 // x = 1; x = 1;
3427 // if (asdf) y = 2;
3428 // while (asdf) ->here;
3429 // here;
3430 // ->foo;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003431 if (lookfor == LOOKFOR_TERM)
3432 {
3433 if (!lookfor_break && whilelevel == 0)
3434 break;
3435 }
3436
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003437 // First line above the one we're indenting is terminated.
3438 // To know what needs to be done look further backward for
3439 // a terminated line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003440 else
3441 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003442 // position the cursor over the rightmost paren, so
3443 // that matching it will take us back to the start of
3444 // the line. Helps for:
3445 // func(asdr,
3446 // asdfasdf);
3447 // here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003448term_again:
3449 l = ml_get_curline();
3450 if (find_last_paren(l, '(', ')')
3451 && (trypos = find_match_paren(
3452 curbuf->b_ind_maxparen)) != NULL)
3453 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003454 // Check if we are on a case label now. This is
3455 // handled above.
3456 // case xx: if ( asdf &&
3457 // asdf)
Bram Moolenaar4b471622019-01-31 13:48:09 +01003458 curwin->w_cursor = *trypos;
3459 l = ml_get_curline();
3460 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
3461 {
3462 ++curwin->w_cursor.lnum;
3463 curwin->w_cursor.col = 0;
3464 continue;
3465 }
3466 }
3467
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003468 // When aligning with the case statement, don't align
3469 // with a statement after it.
3470 // case 1: { <-- don't use this { position
3471 // stat;
3472 // }
3473 // case 2:
3474 // stat;
3475 // }
Bram Moolenaar4b471622019-01-31 13:48:09 +01003476 iscase = (curbuf->b_ind_keep_case_label
3477 && cin_iscase(l, FALSE));
3478
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003479 // Get indent and pointer to text for current line,
3480 // ignoring any jump label.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003481 amount = skip_label(curwin->w_cursor.lnum, &l);
3482
3483 if (theline[0] == '{')
3484 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003485 // See remark above: "Only add b_ind_open_extra.."
Bram Moolenaar4b471622019-01-31 13:48:09 +01003486 l = skipwhite(l);
3487 if (*l == '{')
3488 amount -= curbuf->b_ind_open_extra;
3489 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
3490
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003491 // When a terminated line starts with "else" skip to
3492 // the matching "if":
3493 // else 3;
3494 // indent this;
3495 // Need to use the scope of this "else". XXX
3496 // If whilelevel != 0 continue looking for a "do {".
Bram Moolenaar4b471622019-01-31 13:48:09 +01003497 if (lookfor == LOOKFOR_TERM
3498 && *l != '}'
3499 && cin_iselse(l)
3500 && whilelevel == 0)
3501 {
3502 if ((trypos = find_start_brace()) == NULL
3503 || find_match(LOOKFOR_IF, trypos->lnum)
3504 == FAIL)
3505 break;
3506 continue;
3507 }
3508
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003509 // If we're at the end of a block, skip to the start of
3510 // that block.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003511 l = ml_get_curline();
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003512 if (find_last_paren(l, '{', '}') // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003513 && (trypos = find_start_brace()) != NULL)
3514 {
3515 curwin->w_cursor = *trypos;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003516 // if not "else {" check for terminated again
3517 // but skip block for "} else {"
Bram Moolenaar4b471622019-01-31 13:48:09 +01003518 l = cin_skipcomment(ml_get_curline());
3519 if (*l == '}' || !cin_iselse(l))
3520 goto term_again;
3521 ++curwin->w_cursor.lnum;
3522 curwin->w_cursor.col = 0;
3523 }
3524 }
3525 }
3526 }
3527 }
3528 }
3529
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003530 // add extra indent for a comment
Bram Moolenaar4b471622019-01-31 13:48:09 +01003531 if (cin_iscomment(theline))
3532 amount += curbuf->b_ind_comment;
3533
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003534 // subtract extra left-shift for jump labels
Bram Moolenaar4b471622019-01-31 13:48:09 +01003535 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
3536 amount -= curbuf->b_ind_jump_label;
3537
3538 goto theend;
3539 }
3540
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003541 // ok -- we're not inside any sort of structure at all!
3542 //
3543 // This means we're at the top level, and everything should
3544 // basically just match where the previous line is, except
3545 // for the lines immediately following a function declaration,
3546 // which are K&R-style parameters and need to be indented.
3547 //
3548 // if our line starts with an open brace, forget about any
3549 // prevailing indent and make sure it looks like the start
3550 // of a function
Bram Moolenaar4b471622019-01-31 13:48:09 +01003551
3552 if (theline[0] == '{')
3553 {
3554 amount = curbuf->b_ind_first_open;
3555 goto theend;
3556 }
3557
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003558 // If the NEXT line is a function declaration, the current
3559 // line needs to be indented as a function type spec.
3560 // Don't do this if the current line looks like a comment or if the
3561 // current line is terminated, ie. ends in ';', or if the current line
3562 // contains { or }: "void f() {\n if (1)"
Bram Moolenaar4b471622019-01-31 13:48:09 +01003563 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
3564 && !cin_nocode(theline)
3565 && vim_strchr(theline, '{') == NULL
3566 && vim_strchr(theline, '}') == NULL
3567 && !cin_ends_in(theline, (char_u *)":", NULL)
3568 && !cin_ends_in(theline, (char_u *)",", NULL)
3569 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
3570 cur_curpos.lnum + 1)
3571 && !cin_isterminated(theline, FALSE, TRUE))
3572 {
3573 amount = curbuf->b_ind_func_type;
3574 goto theend;
3575 }
3576
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003577 // search backwards until we find something we recognize
Bram Moolenaar4b471622019-01-31 13:48:09 +01003578 amount = 0;
3579 curwin->w_cursor = cur_curpos;
3580 while (curwin->w_cursor.lnum > 1)
3581 {
3582 curwin->w_cursor.lnum--;
3583 curwin->w_cursor.col = 0;
3584
3585 l = ml_get_curline();
3586
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003587 // If we're in a comment or raw string now, skip to the start
3588 // of it. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003589 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
3590 {
3591 curwin->w_cursor.lnum = trypos->lnum + 1;
3592 curwin->w_cursor.col = 0;
3593 continue;
3594 }
3595
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003596 // Are we at the start of a cpp base class declaration or
3597 // constructor initialization? XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003598 n = FALSE;
3599 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
3600 {
3601 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
3602 l = ml_get_curline();
3603 }
3604 if (n)
3605 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003606 // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003607 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
3608 break;
3609 }
3610
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003611 // Skip preprocessor directives and blank lines.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003612 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
3613 continue;
3614
3615 if (cin_nocode(l))
3616 continue;
3617
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003618 // If the previous line ends in ',', use one level of
3619 // indentation:
3620 // int foo,
3621 // bar;
3622 // do this before checking for '}' in case of eg.
3623 // enum foobar
3624 // {
3625 // ...
3626 // } foo,
3627 // bar;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003628 n = 0;
3629 if (cin_ends_in(l, (char_u *)",", NULL)
3630 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
3631 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003632 // take us back to opening paren
Bram Moolenaar4b471622019-01-31 13:48:09 +01003633 if (find_last_paren(l, '(', ')')
3634 && (trypos = find_match_paren(
3635 curbuf->b_ind_maxparen)) != NULL)
3636 curwin->w_cursor = *trypos;
3637
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003638 /*
3639 * For a line ending in ',' that is a continuation line go
Bram Moolenaar4b471622019-01-31 13:48:09 +01003640 * back to the first line with a backslash:
3641 * char *foo = "bla\
3642 * bla",
3643 * here;
3644 */
3645 while (n == 0 && curwin->w_cursor.lnum > 1)
3646 {
3647 l = ml_get(curwin->w_cursor.lnum - 1);
3648 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
3649 break;
3650 --curwin->w_cursor.lnum;
3651 curwin->w_cursor.col = 0;
3652 }
3653
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003654 amount = get_indent(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003655
3656 if (amount == 0)
3657 amount = cin_first_id_amount();
3658 if (amount == 0)
3659 amount = ind_continuation;
3660 break;
3661 }
3662
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003663 // If the line looks like a function declaration, and we're
3664 // not in a comment, put it the left margin.
3665 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003666 break;
3667 l = ml_get_curline();
3668
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003669 // Finding the closing '}' of a previous function. Put
3670 // current line at the left margin. For when 'cino' has "fs".
Bram Moolenaar4b471622019-01-31 13:48:09 +01003671 if (*skipwhite(l) == '}')
3672 break;
3673
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003674 // (matching {)
3675 // If the previous line ends on '};' (maybe followed by
3676 // comments) align at column 0. For example:
3677 // char *string_array[] = { "foo",
3678 // / * x * / "b};ar" }; / * foobar * /
Bram Moolenaar4b471622019-01-31 13:48:09 +01003679 if (cin_ends_in(l, (char_u *)"};", NULL))
3680 break;
3681
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003682 // If the previous line ends on '[' we are probably in an
3683 // array constant:
3684 // something = [
3685 // 234, <- extra indent
Bram Moolenaar4b471622019-01-31 13:48:09 +01003686 if (cin_ends_in(l, (char_u *)"[", NULL))
3687 {
3688 amount = get_indent() + ind_continuation;
3689 break;
3690 }
3691
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003692 // Find a line only has a semicolon that belongs to a previous
3693 // line ending in '}', e.g. before an #endif. Don't increase
3694 // indent then.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003695 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
3696 {
3697 pos_T curpos_save = curwin->w_cursor;
3698
3699 while (curwin->w_cursor.lnum > 1)
3700 {
3701 look = ml_get(--curwin->w_cursor.lnum);
3702 if (!(cin_nocode(look) || cin_ispreproc_cont(
3703 &look, &curwin->w_cursor.lnum, &amount)))
3704 break;
3705 }
3706 if (curwin->w_cursor.lnum > 0
3707 && cin_ends_in(look, (char_u *)"}", NULL))
3708 break;
3709
3710 curwin->w_cursor = curpos_save;
3711 }
3712
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003713 // If the PREVIOUS line is a function declaration, the current
3714 // line (and the ones that follow) needs to be indented as
3715 // parameters.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003716 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
3717 {
3718 amount = curbuf->b_ind_param;
3719 break;
3720 }
3721
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003722 // If the previous line ends in ';' and the line before the
3723 // previous line ends in ',' or '\', ident to column zero:
3724 // int foo,
3725 // bar;
3726 // indent_to_0 here;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003727 if (cin_ends_in(l, (char_u *)";", NULL))
3728 {
3729 l = ml_get(curwin->w_cursor.lnum - 1);
3730 if (cin_ends_in(l, (char_u *)",", NULL)
3731 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
3732 break;
3733 l = ml_get_curline();
3734 }
3735
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003736 // Doesn't look like anything interesting -- so just
3737 // use the indent of this line.
3738 //
3739 // Position the cursor over the rightmost paren, so that
3740 // matching it will take us back to the start of the line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003741 find_last_paren(l, '(', ')');
3742
3743 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
3744 curwin->w_cursor = *trypos;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003745 amount = get_indent(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003746 break;
3747 }
3748
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003749 // add extra indent for a comment
Bram Moolenaar4b471622019-01-31 13:48:09 +01003750 if (cin_iscomment(theline))
3751 amount += curbuf->b_ind_comment;
3752
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003753 /*
3754 * add extra indent if the previous line ended in a backslash:
Bram Moolenaar4b471622019-01-31 13:48:09 +01003755 * "asdfasdf\
3756 * here";
3757 * char *foo = "asdf\
3758 * here";
3759 */
3760 if (cur_curpos.lnum > 1)
3761 {
3762 l = ml_get(cur_curpos.lnum - 1);
3763 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
3764 {
3765 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
3766 if (cur_amount > 0)
3767 amount = cur_amount;
3768 else if (cur_amount == 0)
3769 amount += ind_continuation;
3770 }
3771 }
3772
3773theend:
3774 if (amount < 0)
3775 amount = 0;
3776
3777laterend:
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003778 // put the cursor back where it belongs
Bram Moolenaar4b471622019-01-31 13:48:09 +01003779 curwin->w_cursor = cur_curpos;
3780
3781 vim_free(linecopy);
3782
3783 return amount;
3784}
3785
3786 static int
3787find_match(int lookfor, linenr_T ourscope)
3788{
3789 char_u *look;
3790 pos_T *theirscope;
3791 char_u *mightbeif;
3792 int elselevel;
3793 int whilelevel;
3794
3795 if (lookfor == LOOKFOR_IF)
3796 {
3797 elselevel = 1;
3798 whilelevel = 0;
3799 }
3800 else
3801 {
3802 elselevel = 0;
3803 whilelevel = 1;
3804 }
3805
3806 curwin->w_cursor.col = 0;
3807
3808 while (curwin->w_cursor.lnum > ourscope + 1)
3809 {
3810 curwin->w_cursor.lnum--;
3811 curwin->w_cursor.col = 0;
3812
3813 look = cin_skipcomment(ml_get_curline());
3814 if (cin_iselse(look)
3815 || cin_isif(look)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003816 || cin_isdo(look) // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003817 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
3818 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003819 // if we've gone outside the braces entirely,
3820 // we must be out of scope...
3821 theirscope = find_start_brace(); // XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003822 if (theirscope == NULL)
3823 break;
3824
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003825 // and if the brace enclosing this is further
3826 // back than the one enclosing the else, we're
3827 // out of luck too.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003828 if (theirscope->lnum < ourscope)
3829 break;
3830
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003831 // and if they're enclosed in a *deeper* brace,
3832 // then we can ignore it because it's in a
3833 // different scope...
Bram Moolenaar4b471622019-01-31 13:48:09 +01003834 if (theirscope->lnum > ourscope)
3835 continue;
3836
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003837 // if it was an "else" (that's not an "else if")
3838 // then we need to go back to another if, so
3839 // increment elselevel
Bram Moolenaar4b471622019-01-31 13:48:09 +01003840 look = cin_skipcomment(ml_get_curline());
3841 if (cin_iselse(look))
3842 {
3843 mightbeif = cin_skipcomment(look + 4);
3844 if (!cin_isif(mightbeif))
3845 ++elselevel;
3846 continue;
3847 }
3848
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003849 // if it was a "while" then we need to go back to
3850 // another "do", so increment whilelevel. XXX
Bram Moolenaar4b471622019-01-31 13:48:09 +01003851 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
3852 {
3853 ++whilelevel;
3854 continue;
3855 }
3856
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003857 // If it's an "if" decrement elselevel
Bram Moolenaar4b471622019-01-31 13:48:09 +01003858 look = cin_skipcomment(ml_get_curline());
3859 if (cin_isif(look))
3860 {
3861 elselevel--;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003862 // When looking for an "if" ignore "while"s that
3863 // get in the way.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003864 if (elselevel == 0 && lookfor == LOOKFOR_IF)
3865 whilelevel = 0;
3866 }
3867
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003868 // If it's a "do" decrement whilelevel
Bram Moolenaar4b471622019-01-31 13:48:09 +01003869 if (cin_isdo(look))
3870 whilelevel--;
3871
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003872 // if we've used up all the elses, then
3873 // this must be the if that we want!
3874 // match the indent level of that if.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003875 if (elselevel <= 0 && whilelevel <= 0)
Bram Moolenaar4b471622019-01-31 13:48:09 +01003876 return OK;
Bram Moolenaar4b471622019-01-31 13:48:09 +01003877 }
3878 }
3879 return FAIL;
3880}
3881
3882# if defined(FEAT_EVAL) || defined(PROTO)
3883/*
3884 * Get indent level from 'indentexpr'.
3885 */
3886 int
3887get_expr_indent(void)
3888{
3889 int indent = -1;
3890 char_u *inde_copy;
3891 pos_T save_pos;
3892 colnr_T save_curswant;
3893 int save_set_curswant;
3894 int save_State;
3895 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
3896 OPT_LOCAL);
3897
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003898 // Save and restore cursor position and curswant, in case it was changed
3899 // via :normal commands
Bram Moolenaar4b471622019-01-31 13:48:09 +01003900 save_pos = curwin->w_cursor;
3901 save_curswant = curwin->w_curswant;
3902 save_set_curswant = curwin->w_set_curswant;
3903 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
3904 if (use_sandbox)
3905 ++sandbox;
3906 ++textlock;
3907
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003908 // Need to make a copy, the 'indentexpr' option could be changed while
3909 // evaluating it.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003910 inde_copy = vim_strsave(curbuf->b_p_inde);
3911 if (inde_copy != NULL)
3912 {
3913 indent = (int)eval_to_number(inde_copy);
3914 vim_free(inde_copy);
3915 }
3916
3917 if (use_sandbox)
3918 --sandbox;
3919 --textlock;
3920
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003921 // Restore the cursor position so that 'indentexpr' doesn't need to.
3922 // Pretend to be in Insert mode, allow cursor past end of line for "o"
3923 // command.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003924 save_State = State;
3925 State = INSERT;
3926 curwin->w_cursor = save_pos;
3927 curwin->w_curswant = save_curswant;
3928 curwin->w_set_curswant = save_set_curswant;
3929 check_cursor();
3930 State = save_State;
3931
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003932 // If there is an error, just keep the current indent.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003933 if (indent < 0)
3934 indent = get_indent();
3935
3936 return indent;
3937}
3938# endif
3939
3940/*
3941 * return TRUE if 'cinkeys' contains the key "keytyped",
3942 * when == '*': Only if key is preceded with '*' (indent before insert)
3943 * when == '!': Only if key is preceded with '!' (don't insert)
3944 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
3945 *
3946 * "keytyped" can have a few special values:
3947 * KEY_OPEN_FORW
3948 * KEY_OPEN_BACK
3949 * KEY_COMPLETE just finished completion.
3950 *
3951 * If line_is_empty is TRUE accept keys with '0' before them.
3952 */
3953 int
3954in_cinkeys(
3955 int keytyped,
3956 int when,
3957 int line_is_empty)
3958{
3959 char_u *look;
3960 int try_match;
3961 int try_match_word;
3962 char_u *p;
3963 char_u *line;
3964 int icase;
3965 int i;
3966
3967 if (keytyped == NUL)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003968 // Can happen with CTRL-Y and CTRL-E on a short line.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003969 return FALSE;
3970
3971#ifdef FEAT_EVAL
3972 if (*curbuf->b_p_inde != NUL)
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003973 look = curbuf->b_p_indk; // 'indentexpr' set: use 'indentkeys'
Bram Moolenaar4b471622019-01-31 13:48:09 +01003974 else
3975#endif
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003976 look = curbuf->b_p_cink; // 'indentexpr' empty: use 'cinkeys'
Bram Moolenaar4b471622019-01-31 13:48:09 +01003977 while (*look)
3978 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003979 // Find out if we want to try a match with this key, depending on
3980 // 'when' and a '*' or '!' before the key.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003981 switch (when)
3982 {
3983 case '*': try_match = (*look == '*'); break;
3984 case '!': try_match = (*look == '!'); break;
3985 default: try_match = (*look != '*'); break;
3986 }
3987 if (*look == '*' || *look == '!')
3988 ++look;
3989
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01003990 // If there is a '0', only accept a match if the line is empty.
3991 // But may still match when typing last char of a word.
Bram Moolenaar4b471622019-01-31 13:48:09 +01003992 if (*look == '0')
3993 {
3994 try_match_word = try_match;
3995 if (!line_is_empty)
3996 try_match = FALSE;
3997 ++look;
3998 }
3999 else
4000 try_match_word = FALSE;
4001
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004002 // does it look like a control character?
Bram Moolenaar4b471622019-01-31 13:48:09 +01004003 if (*look == '^'
4004#ifdef EBCDIC
4005 && (Ctrl_chr(look[1]) != 0)
4006#else
4007 && look[1] >= '?' && look[1] <= '_'
4008#endif
4009 )
4010 {
4011 if (try_match && keytyped == Ctrl_chr(look[1]))
4012 return TRUE;
4013 look += 2;
4014 }
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004015 // 'o' means "o" command, open forward.
4016 // 'O' means "O" command, open backward.
Bram Moolenaar4b471622019-01-31 13:48:09 +01004017 else if (*look == 'o')
4018 {
4019 if (try_match && keytyped == KEY_OPEN_FORW)
4020 return TRUE;
4021 ++look;
4022 }
4023 else if (*look == 'O')
4024 {
4025 if (try_match && keytyped == KEY_OPEN_BACK)
4026 return TRUE;
4027 ++look;
4028 }
4029
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004030 // 'e' means to check for "else" at start of line and just before the
4031 // cursor.
Bram Moolenaar4b471622019-01-31 13:48:09 +01004032 else if (*look == 'e')
4033 {
4034 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
4035 {
4036 p = ml_get_curline();
4037 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
4038 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
4039 return TRUE;
4040 }
4041 ++look;
4042 }
4043
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004044 // ':' only causes an indent if it is at the end of a label or case
4045 // statement, or when it was before typing the ':' (to fix
4046 // class::method for C++).
Bram Moolenaar4b471622019-01-31 13:48:09 +01004047 else if (*look == ':')
4048 {
4049 if (try_match && keytyped == ':')
4050 {
4051 p = ml_get_curline();
4052 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
4053 return TRUE;
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004054 // Need to get the line again after cin_islabel().
Bram Moolenaar4b471622019-01-31 13:48:09 +01004055 p = ml_get_curline();
4056 if (curwin->w_cursor.col > 2
4057 && p[curwin->w_cursor.col - 1] == ':'
4058 && p[curwin->w_cursor.col - 2] == ':')
4059 {
4060 p[curwin->w_cursor.col - 1] = ' ';
4061 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
4062 || cin_islabel());
4063 p = ml_get_curline();
4064 p[curwin->w_cursor.col - 1] = ':';
4065 if (i)
4066 return TRUE;
4067 }
4068 }
4069 ++look;
4070 }
4071
4072
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004073 // Is it a key in <>, maybe?
Bram Moolenaar4b471622019-01-31 13:48:09 +01004074 else if (*look == '<')
4075 {
4076 if (try_match)
4077 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004078 // make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
4079 // <:> and <!> so that people can re-indent on o, O, e, 0, <,
4080 // >, *, : and ! keys if they really really want to.
Bram Moolenaar4b471622019-01-31 13:48:09 +01004081 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
4082 && keytyped == look[1])
4083 return TRUE;
4084
4085 if (keytyped == get_special_key_code(look + 1))
4086 return TRUE;
4087 }
4088 while (*look && *look != '>')
4089 look++;
4090 while (*look == '>')
4091 look++;
4092 }
4093
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004094 // Is it a word: "=word"?
Bram Moolenaar4b471622019-01-31 13:48:09 +01004095 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
4096 {
4097 ++look;
4098 if (*look == '~')
4099 {
4100 icase = TRUE;
4101 ++look;
4102 }
4103 else
4104 icase = FALSE;
4105 p = vim_strchr(look, ',');
4106 if (p == NULL)
4107 p = look + STRLEN(look);
4108 if ((try_match || try_match_word)
4109 && curwin->w_cursor.col >= (colnr_T)(p - look))
4110 {
4111 int match = FALSE;
4112
Bram Moolenaar4b471622019-01-31 13:48:09 +01004113 if (keytyped == KEY_COMPLETE)
4114 {
4115 char_u *s;
4116
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004117 // Just completed a word, check if it starts with "look".
4118 // search back for the start of a word.
Bram Moolenaar4b471622019-01-31 13:48:09 +01004119 line = ml_get_curline();
4120 if (has_mbyte)
4121 {
4122 char_u *n;
4123
4124 for (s = line + curwin->w_cursor.col; s > line; s = n)
4125 {
4126 n = mb_prevptr(line, s);
4127 if (!vim_iswordp(n))
4128 break;
4129 }
4130 }
4131 else
4132 for (s = line + curwin->w_cursor.col; s > line; --s)
4133 if (!vim_iswordc(s[-1]))
4134 break;
4135 if (s + (p - look) <= line + curwin->w_cursor.col
4136 && (icase
4137 ? MB_STRNICMP(s, look, p - look)
4138 : STRNCMP(s, look, p - look)) == 0)
4139 match = TRUE;
4140 }
4141 else
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004142 // TODO: multi-byte
Bram Moolenaar4b471622019-01-31 13:48:09 +01004143 if (keytyped == (int)p[-1] || (icase && keytyped < 256
4144 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
4145 {
4146 line = ml_get_cursor();
4147 if ((curwin->w_cursor.col == (colnr_T)(p - look)
4148 || !vim_iswordc(line[-(p - look) - 1]))
4149 && (icase
4150 ? MB_STRNICMP(line - (p - look), look, p - look)
4151 : STRNCMP(line - (p - look), look, p - look))
4152 == 0)
4153 match = TRUE;
4154 }
4155 if (match && try_match_word && !try_match)
4156 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004157 // "0=word": Check if there are only blanks before the
4158 // word.
Bram Moolenaar4b471622019-01-31 13:48:09 +01004159 if (getwhitecols_curline() !=
4160 (int)(curwin->w_cursor.col - (p - look)))
4161 match = FALSE;
4162 }
4163 if (match)
4164 return TRUE;
4165 }
4166 look = p;
4167 }
4168
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004169 // ok, it's a boring generic character.
Bram Moolenaar4b471622019-01-31 13:48:09 +01004170 else
4171 {
4172 if (try_match && *look == keytyped)
4173 return TRUE;
4174 if (*look != NUL)
4175 ++look;
4176 }
4177
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004178 // Skip over ", ".
Bram Moolenaar4b471622019-01-31 13:48:09 +01004179 look = skip_to_option_part(look);
4180 }
4181 return FALSE;
4182}
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004183#endif // FEAT_CINDENT
Bram Moolenaar4b471622019-01-31 13:48:09 +01004184
4185#if defined(FEAT_LISP) || defined(PROTO)
4186
4187 static int
4188lisp_match(char_u *p)
4189{
4190 char_u buf[LSIZE];
4191 int len;
4192 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
4193
4194 while (*word != NUL)
4195 {
4196 (void)copy_option_part(&word, buf, LSIZE, ",");
4197 len = (int)STRLEN(buf);
4198 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
4199 return TRUE;
4200 }
4201 return FALSE;
4202}
4203
4204/*
4205 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
4206 * The incompatible newer method is quite a bit better at indenting
4207 * code in lisp-like languages than the traditional one; it's still
4208 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
4209 *
4210 * TODO:
4211 * Findmatch() should be adapted for lisp, also to make showmatch
4212 * work correctly: now (v5.3) it seems all C/C++ oriented:
4213 * - it does not recognize the #\( and #\) notations as character literals
4214 * - it doesn't know about comments starting with a semicolon
4215 * - it incorrectly interprets '(' as a character literal
4216 * All this messes up get_lisp_indent in some rare cases.
4217 * Update from Sergey Khorev:
4218 * I tried to fix the first two issues.
4219 */
4220 int
4221get_lisp_indent(void)
4222{
4223 pos_T *pos, realpos, paren;
4224 int amount;
4225 char_u *that;
4226 colnr_T col;
4227 colnr_T firsttry;
4228 int parencount, quotecount;
4229 int vi_lisp;
4230
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004231 // Set vi_lisp to use the vi-compatible method
Bram Moolenaar4b471622019-01-31 13:48:09 +01004232 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
4233
4234 realpos = curwin->w_cursor;
4235 curwin->w_cursor.col = 0;
4236
4237 if ((pos = findmatch(NULL, '(')) == NULL)
4238 pos = findmatch(NULL, '[');
4239 else
4240 {
4241 paren = *pos;
4242 pos = findmatch(NULL, '[');
4243 if (pos == NULL || LT_POSP(pos, &paren))
4244 pos = &paren;
4245 }
4246 if (pos != NULL)
4247 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004248 // Extra trick: Take the indent of the first previous non-white
4249 // line that is at the same () level.
Bram Moolenaar4b471622019-01-31 13:48:09 +01004250 amount = -1;
4251 parencount = 0;
4252
4253 while (--curwin->w_cursor.lnum >= pos->lnum)
4254 {
4255 if (linewhite(curwin->w_cursor.lnum))
4256 continue;
4257 for (that = ml_get_curline(); *that != NUL; ++that)
4258 {
4259 if (*that == ';')
4260 {
4261 while (*(that + 1) != NUL)
4262 ++that;
4263 continue;
4264 }
4265 if (*that == '\\')
4266 {
4267 if (*(that + 1) != NUL)
4268 ++that;
4269 continue;
4270 }
4271 if (*that == '"' && *(that + 1) != NUL)
4272 {
4273 while (*++that && *that != '"')
4274 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004275 // skipping escaped characters in the string
Bram Moolenaar4b471622019-01-31 13:48:09 +01004276 if (*that == '\\')
4277 {
4278 if (*++that == NUL)
4279 break;
4280 if (that[1] == NUL)
4281 {
4282 ++that;
4283 break;
4284 }
4285 }
4286 }
4287 }
4288 if (*that == '(' || *that == '[')
4289 ++parencount;
4290 else if (*that == ')' || *that == ']')
4291 --parencount;
4292 }
4293 if (parencount == 0)
4294 {
4295 amount = get_indent();
4296 break;
4297 }
4298 }
4299
4300 if (amount == -1)
4301 {
4302 curwin->w_cursor.lnum = pos->lnum;
4303 curwin->w_cursor.col = pos->col;
4304 col = pos->col;
4305
4306 that = ml_get_curline();
4307
4308 if (vi_lisp && get_indent() == 0)
4309 amount = 2;
4310 else
4311 {
4312 char_u *line = that;
4313
4314 amount = 0;
4315 while (*that && col)
4316 {
4317 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
4318 col--;
4319 }
4320
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004321 // Some keywords require "body" indenting rules (the
4322 // non-standard-lisp ones are Scheme special forms):
4323 //
4324 // (let ((a 1)) instead (let ((a 1))
4325 // (...)) of (...))
Bram Moolenaar4b471622019-01-31 13:48:09 +01004326
4327 if (!vi_lisp && (*that == '(' || *that == '[')
4328 && lisp_match(that + 1))
4329 amount += 2;
4330 else
4331 {
4332 that++;
4333 amount++;
4334 firsttry = amount;
4335
4336 while (VIM_ISWHITE(*that))
4337 {
4338 amount += lbr_chartabsize(line, that, (colnr_T)amount);
4339 ++that;
4340 }
4341
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004342 if (*that && *that != ';') // not a comment line
Bram Moolenaar4b471622019-01-31 13:48:09 +01004343 {
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004344 // test *that != '(' to accommodate first let/do
4345 // argument if it is more than one line
Bram Moolenaar4b471622019-01-31 13:48:09 +01004346 if (!vi_lisp && *that != '(' && *that != '[')
4347 firsttry++;
4348
4349 parencount = 0;
4350 quotecount = 0;
4351
4352 if (vi_lisp
4353 || (*that != '"'
4354 && *that != '\''
4355 && *that != '#'
4356 && (*that < '0' || *that > '9')))
4357 {
4358 while (*that
4359 && (!VIM_ISWHITE(*that)
4360 || quotecount
4361 || parencount)
4362 && (!((*that == '(' || *that == '[')
4363 && !quotecount
4364 && !parencount
4365 && vi_lisp)))
4366 {
4367 if (*that == '"')
4368 quotecount = !quotecount;
4369 if ((*that == '(' || *that == '[')
4370 && !quotecount)
4371 ++parencount;
4372 if ((*that == ')' || *that == ']')
4373 && !quotecount)
4374 --parencount;
4375 if (*that == '\\' && *(that+1) != NUL)
4376 amount += lbr_chartabsize_adv(
4377 line, &that, (colnr_T)amount);
4378 amount += lbr_chartabsize_adv(
4379 line, &that, (colnr_T)amount);
4380 }
4381 }
4382 while (VIM_ISWHITE(*that))
4383 {
4384 amount += lbr_chartabsize(
4385 line, that, (colnr_T)amount);
4386 that++;
4387 }
4388 if (!*that || *that == ';')
4389 amount = firsttry;
4390 }
4391 }
4392 }
4393 }
4394 }
4395 else
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004396 amount = 0; // no matching '(' or '[' found, use zero indent
Bram Moolenaar4b471622019-01-31 13:48:09 +01004397
4398 curwin->w_cursor = realpos;
4399
4400 return amount;
4401}
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004402#endif // FEAT_LISP
Bram Moolenaar4b471622019-01-31 13:48:09 +01004403
4404#if defined(FEAT_CINDENT) || defined(PROTO)
4405/*
4406 * Do C or expression indenting on the current line.
4407 */
4408 void
4409do_c_expr_indent(void)
4410{
4411# ifdef FEAT_EVAL
4412 if (*curbuf->b_p_inde != NUL)
4413 fixthisline(get_expr_indent);
4414 else
4415# endif
4416 fixthisline(get_c_indent);
4417}
4418#endif
4419
4420#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
4421/*
4422 * Re-indent the current line, based on the current contents of it and the
4423 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
4424 * confused what all the part that handles Control-T is doing that I'm not.
4425 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
4426 */
4427
4428 void
4429fixthisline(int (*get_the_indent)(void))
4430{
4431 int amount = get_the_indent();
4432
4433 if (amount >= 0)
4434 {
4435 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
4436 if (linewhite(curwin->w_cursor.lnum))
Bram Moolenaar9c46efd2019-02-04 20:30:18 +01004437 did_ai = TRUE; // delete the indent if the line stays empty
Bram Moolenaar4b471622019-01-31 13:48:09 +01004438 }
4439}
4440
4441 void
4442fix_indent(void)
4443{
4444 if (p_paste)
4445 return;
4446# ifdef FEAT_LISP
4447 if (curbuf->b_p_lisp && curbuf->b_p_ai)
4448 fixthisline(get_lisp_indent);
4449# endif
4450# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
4451 else
4452# endif
4453# ifdef FEAT_CINDENT
4454 if (cindent_on())
4455 do_c_expr_indent();
4456# endif
4457}
4458
4459#endif
Bram Moolenaare677df82019-09-02 22:31:11 +02004460
4461#if defined(FEAT_VARTABS) || defined(PROTO)
4462
4463/*
4464 * Set the integer values corresponding to the string setting of 'vartabstop'.
4465 * "array" will be set, caller must free it if needed.
4466 */
4467 int
4468tabstop_set(char_u *var, int **array)
4469{
4470 int valcount = 1;
4471 int t;
4472 char_u *cp;
4473
4474 if (var[0] == NUL || (var[0] == '0' && var[1] == NUL))
4475 {
4476 *array = NULL;
4477 return TRUE;
4478 }
4479
4480 for (cp = var; *cp != NUL; ++cp)
4481 {
4482 if (cp == var || cp[-1] == ',')
4483 {
4484 char_u *end;
4485
4486 if (strtol((char *)cp, (char **)&end, 10) <= 0)
4487 {
4488 if (cp != end)
4489 emsg(_(e_positive));
4490 else
4491 emsg(_(e_invarg));
4492 return FALSE;
4493 }
4494 }
4495
4496 if (VIM_ISDIGIT(*cp))
4497 continue;
4498 if (cp[0] == ',' && cp > var && cp[-1] != ',' && cp[1] != NUL)
4499 {
4500 ++valcount;
4501 continue;
4502 }
4503 emsg(_(e_invarg));
4504 return FALSE;
4505 }
4506
4507 *array = ALLOC_MULT(int, valcount + 1);
4508 if (*array == NULL)
4509 return FALSE;
4510 (*array)[0] = valcount;
4511
4512 t = 1;
4513 for (cp = var; *cp != NUL;)
4514 {
4515 (*array)[t++] = atoi((char *)cp);
4516 while (*cp != NUL && *cp != ',')
4517 ++cp;
4518 if (*cp != NUL)
4519 ++cp;
4520 }
4521
4522 return TRUE;
4523}
4524
4525/*
4526 * Calculate the number of screen spaces a tab will occupy.
4527 * If "vts" is set then the tab widths are taken from that array,
4528 * otherwise the value of ts is used.
4529 */
4530 int
4531tabstop_padding(colnr_T col, int ts_arg, int *vts)
4532{
4533 int ts = ts_arg == 0 ? 8 : ts_arg;
4534 int tabcount;
4535 colnr_T tabcol = 0;
4536 int t;
4537 int padding = 0;
4538
4539 if (vts == NULL || vts[0] == 0)
4540 return ts - (col % ts);
4541
4542 tabcount = vts[0];
4543
4544 for (t = 1; t <= tabcount; ++t)
4545 {
4546 tabcol += vts[t];
4547 if (tabcol > col)
4548 {
4549 padding = (int)(tabcol - col);
4550 break;
4551 }
4552 }
4553 if (t > tabcount)
4554 padding = vts[tabcount] - (int)((col - tabcol) % vts[tabcount]);
4555
4556 return padding;
4557}
4558
4559/*
4560 * Find the size of the tab that covers a particular column.
4561 */
4562 int
4563tabstop_at(colnr_T col, int ts, int *vts)
4564{
4565 int tabcount;
4566 colnr_T tabcol = 0;
4567 int t;
4568 int tab_size = 0;
4569
4570 if (vts == 0 || vts[0] == 0)
4571 return ts;
4572
4573 tabcount = vts[0];
4574 for (t = 1; t <= tabcount; ++t)
4575 {
4576 tabcol += vts[t];
4577 if (tabcol > col)
4578 {
4579 tab_size = vts[t];
4580 break;
4581 }
4582 }
4583 if (t > tabcount)
4584 tab_size = vts[tabcount];
4585
4586 return tab_size;
4587}
4588
4589/*
4590 * Find the column on which a tab starts.
4591 */
4592 colnr_T
4593tabstop_start(colnr_T col, int ts, int *vts)
4594{
4595 int tabcount;
4596 colnr_T tabcol = 0;
4597 int t;
4598 int excess;
4599
4600 if (vts == NULL || vts[0] == 0)
4601 return (col / ts) * ts;
4602
4603 tabcount = vts[0];
4604 for (t = 1; t <= tabcount; ++t)
4605 {
4606 tabcol += vts[t];
4607 if (tabcol > col)
4608 return tabcol - vts[t];
4609 }
4610
4611 excess = tabcol % vts[tabcount];
4612 return excess + ((col - excess) / vts[tabcount]) * vts[tabcount];
4613}
4614
4615/*
4616 * Find the number of tabs and spaces necessary to get from one column
4617 * to another.
4618 */
4619 void
4620tabstop_fromto(
4621 colnr_T start_col,
4622 colnr_T end_col,
4623 int ts_arg,
4624 int *vts,
4625 int *ntabs,
4626 int *nspcs)
4627{
4628 int spaces = end_col - start_col;
4629 colnr_T tabcol = 0;
4630 int padding = 0;
4631 int tabcount;
4632 int t;
4633 int ts = ts_arg == 0 ? curbuf->b_p_ts : ts_arg;
4634
4635 if (vts == NULL || vts[0] == 0)
4636 {
4637 int tabs = 0;
4638 int initspc = 0;
4639
4640 initspc = ts - (start_col % ts);
4641 if (spaces >= initspc)
4642 {
4643 spaces -= initspc;
4644 tabs++;
4645 }
4646 tabs += spaces / ts;
4647 spaces -= (spaces / ts) * ts;
4648
4649 *ntabs = tabs;
4650 *nspcs = spaces;
4651 return;
4652 }
4653
4654 // Find the padding needed to reach the next tabstop.
4655 tabcount = vts[0];
4656 for (t = 1; t <= tabcount; ++t)
4657 {
4658 tabcol += vts[t];
4659 if (tabcol > start_col)
4660 {
4661 padding = (int)(tabcol - start_col);
4662 break;
4663 }
4664 }
4665 if (t > tabcount)
4666 padding = vts[tabcount] - (int)((start_col - tabcol) % vts[tabcount]);
4667
4668 // If the space needed is less than the padding no tabs can be used.
4669 if (spaces < padding)
4670 {
4671 *ntabs = 0;
4672 *nspcs = spaces;
4673 return;
4674 }
4675
4676 *ntabs = 1;
4677 spaces -= padding;
4678
4679 // At least one tab has been used. See if any more will fit.
4680 while (spaces != 0 && ++t <= tabcount)
4681 {
4682 padding = vts[t];
4683 if (spaces < padding)
4684 {
4685 *nspcs = spaces;
4686 return;
4687 }
4688 ++*ntabs;
4689 spaces -= padding;
4690 }
4691
4692 *ntabs += spaces / vts[tabcount];
4693 *nspcs = spaces % vts[tabcount];
4694}
4695
4696/*
4697 * See if two tabstop arrays contain the same values.
4698 */
4699 int
4700tabstop_eq(int *ts1, int *ts2)
4701{
4702 int t;
4703
4704 if ((ts1 == 0 && ts2) || (ts1 && ts2 == 0))
4705 return FALSE;
4706 if (ts1 == ts2)
4707 return TRUE;
4708 if (ts1[0] != ts2[0])
4709 return FALSE;
4710
4711 for (t = 1; t <= ts1[0]; ++t)
4712 if (ts1[t] != ts2[t])
4713 return FALSE;
4714
4715 return TRUE;
4716}
4717
4718#if defined(FEAT_BEVAL) || defined(PROTO)
4719/*
4720 * Copy a tabstop array, allocating space for the new array.
4721 */
4722 int *
4723tabstop_copy(int *oldts)
4724{
4725 int *newts;
4726 int t;
4727
4728 if (oldts == NULL)
4729 return NULL;
4730 newts = ALLOC_MULT(int, oldts[0] + 1);
4731 if (newts != NULL)
4732 for (t = 0; t <= oldts[0]; ++t)
4733 newts[t] = oldts[t];
4734 return newts;
4735}
4736#endif
4737
4738/*
4739 * Return a count of the number of tabstops.
4740 */
4741 int
4742tabstop_count(int *ts)
4743{
4744 return ts != NULL ? ts[0] : 0;
4745}
4746
4747/*
4748 * Return the first tabstop, or 8 if there are no tabstops defined.
4749 */
4750 int
4751tabstop_first(int *ts)
4752{
4753 return ts != NULL ? ts[1] : 8;
4754}
4755
4756#endif
4757
4758/*
4759 * Return the effective shiftwidth value for current buffer, using the
4760 * 'tabstop' value when 'shiftwidth' is zero.
4761 */
4762 long
4763get_sw_value(buf_T *buf)
4764{
4765 return get_sw_value_col(buf, 0);
4766}
4767
4768/*
4769 * Idem, using "pos".
4770 */
4771 static long
4772get_sw_value_pos(buf_T *buf, pos_T *pos)
4773{
4774 pos_T save_cursor = curwin->w_cursor;
4775 long sw_value;
4776
4777 curwin->w_cursor = *pos;
4778 sw_value = get_sw_value_col(buf, get_nolist_virtcol());
4779 curwin->w_cursor = save_cursor;
4780 return sw_value;
4781}
4782
4783/*
4784 * Idem, using the first non-black in the current line.
4785 */
4786 long
4787get_sw_value_indent(buf_T *buf)
4788{
4789 pos_T pos = curwin->w_cursor;
4790
4791 pos.col = getwhitecols_curline();
4792 return get_sw_value_pos(buf, &pos);
4793}
4794
4795/*
4796 * Idem, using virtual column "col".
4797 */
4798 long
4799get_sw_value_col(buf_T *buf, colnr_T col UNUSED)
4800{
4801 return buf->b_p_sw ? buf->b_p_sw :
4802 #ifdef FEAT_VARTABS
4803 tabstop_at(col, buf->b_p_ts, buf->b_p_vts_array);
4804 #else
4805 buf->b_p_ts;
4806 #endif
4807}
4808
4809/*
4810 * Return the effective softtabstop value for the current buffer, using the
4811 * 'shiftwidth' value when 'softtabstop' is negative.
4812 */
4813 long
4814get_sts_value(void)
4815{
4816 return curbuf->b_p_sts < 0 ? get_sw_value(curbuf) : curbuf->b_p_sts;
4817}