blob: fbb1081a507f782688278c447e17d1d6f141d285 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
Bram Moolenaar597a4222014-06-25 14:39:50 +020044 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
Bram Moolenaar597a4222014-06-25 14:39:50 +020055 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_str(
65 char_u *ptr,
66 int ts,
67 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 if (*ptr == TAB)
74 {
75 if (!list || lcs_tab1) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020078 /* In list mode, when tab is not set, count screen char width
79 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020080 count += ptr2cells(ptr);
81 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else if (*ptr == ' ')
83 ++count; /* count a space for one */
84 else
85 break;
86 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000088}
89
90/*
91 * Set the indent of the current line.
92 * Leaves the cursor on the first non-blank in the line.
93 * Caller must take care of undo.
94 * "flags":
95 * SIN_CHANGED: call changed_bytes() if the line was changed.
96 * SIN_INSERT: insert the indent in front of the line.
97 * SIN_UNDO: save line for undo before changing it.
98 * Returns TRUE if the line was changed.
99 */
100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100101set_indent(
102 int size, /* measured in spaces */
103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 char_u *p;
106 char_u *newline;
107 char_u *oldline;
108 char_u *s;
109 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 int line_len;
112 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000115 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000116 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119 /*
120 * First check if there is anything to do and compute the number of
121 * characters needed for the indent.
122 */
123 todo = size;
124 ind_len = 0;
125 p = oldline = ml_get_curline();
126
127 /* Calculate the buffer size for the new indent, and check to see if it
128 * isn't already set */
129
Bram Moolenaar5002c292007-07-24 13:26:15 +0000130 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
131 * 'preserveindent' are set count the number of characters at the
132 * beginning of the line to be copied */
133 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 /* If 'preserveindent' is set then reuse as much as possible of
136 * the existing indent structure for the new indent */
137 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
138 {
139 ind_done = 0;
140
141 /* count as many characters as we can use */
142 while (todo > 0 && vim_iswhite(*p))
143 {
144 if (*p == TAB)
145 {
146 tab_pad = (int)curbuf->b_p_ts
147 - (ind_done % (int)curbuf->b_p_ts);
148 /* stop if this tab will overshoot the target */
149 if (todo < tab_pad)
150 break;
151 todo -= tab_pad;
152 ++ind_len;
153 ind_done += tab_pad;
154 }
155 else
156 {
157 --todo;
158 ++ind_len;
159 ++ind_done;
160 }
161 ++p;
162 }
163
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 /* Set initial number of whitespace chars to copy if we are
165 * preserving indent but expandtab is set */
166 if (curbuf->b_p_et)
167 orig_char_len = ind_len;
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* Fill to next tabstop with a tab, if possible */
170 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000171 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 doit = TRUE;
174 todo -= tab_pad;
175 ++ind_len;
176 /* ind_done += tab_pad; */
177 }
178 }
179
180 /* count tabs required for indent */
181 while (todo >= (int)curbuf->b_p_ts)
182 {
183 if (*p != TAB)
184 doit = TRUE;
185 else
186 ++p;
187 todo -= (int)curbuf->b_p_ts;
188 ++ind_len;
189 /* ind_done += (int)curbuf->b_p_ts; */
190 }
191 }
192 /* count spaces required for indent */
193 while (todo > 0)
194 {
195 if (*p != ' ')
196 doit = TRUE;
197 else
198 ++p;
199 --todo;
200 ++ind_len;
201 /* ++ind_done; */
202 }
203
204 /* Return if the indent is OK already. */
205 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
206 return FALSE;
207
208 /* Allocate memory for the new line. */
209 if (flags & SIN_INSERT)
210 p = oldline;
211 else
212 p = skipwhite(p);
213 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214
215 /* If 'preserveindent' and 'expandtab' are both set keep the original
216 * characters and allocate accordingly. We will fill the rest with spaces
217 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 {
220 newline = alloc(orig_char_len + size - ind_done + line_len);
221 if (newline == NULL)
222 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000223 todo = size - ind_done;
224 ind_len = orig_char_len + todo; /* Set total length of indent in
225 * characters, which may have been
226 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 p = oldline;
228 s = newline;
229 while (orig_char_len > 0)
230 {
231 *s++ = *p++;
232 orig_char_len--;
233 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 /* Skip over any additional white space (useful when newindent is less
236 * than old) */
237 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000239
Bram Moolenaar5002c292007-07-24 13:26:15 +0000240 }
241 else
242 {
243 todo = size;
244 newline = alloc(ind_len + line_len);
245 if (newline == NULL)
246 return FALSE;
247 s = newline;
248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* if 'expandtab' isn't set: use TABs */
252 if (!curbuf->b_p_et)
253 {
254 /* If 'preserveindent' is set then reuse as much as possible of
255 * the existing indent structure for the new indent */
256 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
257 {
258 p = oldline;
259 ind_done = 0;
260
261 while (todo > 0 && vim_iswhite(*p))
262 {
263 if (*p == TAB)
264 {
265 tab_pad = (int)curbuf->b_p_ts
266 - (ind_done % (int)curbuf->b_p_ts);
267 /* stop if this tab will overshoot the target */
268 if (todo < tab_pad)
269 break;
270 todo -= tab_pad;
271 ind_done += tab_pad;
272 }
273 else
274 {
275 --todo;
276 ++ind_done;
277 }
278 *s++ = *p++;
279 }
280
281 /* Fill to next tabstop with a tab, if possible */
282 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
283 if (todo >= tab_pad)
284 {
285 *s++ = TAB;
286 todo -= tab_pad;
287 }
288
289 p = skipwhite(p);
290 }
291
292 while (todo >= (int)curbuf->b_p_ts)
293 {
294 *s++ = TAB;
295 todo -= (int)curbuf->b_p_ts;
296 }
297 }
298 while (todo > 0)
299 {
300 *s++ = ' ';
301 --todo;
302 }
303 mch_memmove(s, p, (size_t)line_len);
304
305 /* Replace the line (unless undo fails). */
306 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
307 {
308 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
309 if (flags & SIN_CHANGED)
310 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200311 /* Correct saved cursor position if it is in this line. */
312 if (saved_cursor.lnum == curwin->w_cursor.lnum)
313 {
314 if (saved_cursor.col >= (colnr_T)(p - oldline))
315 /* cursor was after the indent, adjust for the number of
316 * bytes added/removed */
317 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
318 else if (saved_cursor.col >= (colnr_T)(s - newline))
319 /* cursor was in the indent, and is now after it, put it back
320 * at the start of the indent (replacing spaces with TAB) */
321 saved_cursor.col = (colnr_T)(s - newline);
322 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000323 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 else
326 vim_free(newline);
327
328 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000329 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Copy the indent from ptr to the current line (and fill to size)
334 * Leaves the cursor on the first non-blank in the line.
335 * Returns TRUE if the line was changed.
336 */
337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 char_u *p = NULL;
341 char_u *line = NULL;
342 char_u *s;
343 int todo;
344 int ind_len;
345 int line_len = 0;
346 int tab_pad;
347 int ind_done;
348 int round;
349
350 /* Round 1: compute the number of characters needed for the indent
351 * Round 2: copy the characters. */
352 for (round = 1; round <= 2; ++round)
353 {
354 todo = size;
355 ind_len = 0;
356 ind_done = 0;
357 s = src;
358
359 /* Count/copy the usable portion of the source line */
360 while (todo > 0 && vim_iswhite(*s))
361 {
362 if (*s == TAB)
363 {
364 tab_pad = (int)curbuf->b_p_ts
365 - (ind_done % (int)curbuf->b_p_ts);
366 /* Stop if this tab will overshoot the target */
367 if (todo < tab_pad)
368 break;
369 todo -= tab_pad;
370 ind_done += tab_pad;
371 }
372 else
373 {
374 --todo;
375 ++ind_done;
376 }
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = *s;
380 ++s;
381 }
382
383 /* Fill to next tabstop with a tab, if possible */
384 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200385 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
387 todo -= tab_pad;
388 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000389 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 *p++ = TAB;
391 }
392
393 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200394 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 todo -= (int)curbuf->b_p_ts;
397 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 *p++ = TAB;
400 }
401
402 /* Count/add spaces required for indent */
403 while (todo > 0)
404 {
405 --todo;
406 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000407 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 *p++ = ' ';
409 }
410
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 /* Allocate memory for the result: the copied indent, new indent
414 * and the rest of the line. */
415 line_len = (int)STRLEN(ml_get_curline()) + 1;
416 line = alloc(ind_len + line_len);
417 if (line == NULL)
418 return FALSE;
419 p = line;
420 }
421 }
422
423 /* Append the original line */
424 mch_memmove(p, ml_get_curline(), (size_t)line_len);
425
426 /* Replace the line */
427 ml_replace(curwin->w_cursor.lnum, line, FALSE);
428
429 /* Put the cursor after the indent. */
430 curwin->w_cursor.col = ind_len;
431 return TRUE;
432}
433
434/*
435 * Return the indent of the current line after a number. Return -1 if no
436 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 colnr_T col;
443 pos_T pos;
444
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch_T regmatch;
446 int lead_len = 0; /* length of comment leader */
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (lnum > curbuf->b_ml.ml_line_count)
449 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000450 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
452#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
454 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
458 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 /* vim_regexec() expects a pointer to a line. This lets us
463 * start matching for the flp beyond any comment leader... */
464 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 pos.lnum = lnum;
467 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200472 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000474
475 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 getvcol(curwin, &pos, &col, NULL, NULL);
478 return (int)col;
479}
480
Bram Moolenaar597a4222014-06-25 14:39:50 +0200481#if defined(FEAT_LINEBREAK) || defined(PROTO)
482/*
483 * Return appropriate space number for breakindent, taking influencing
484 * parameters into account. Window must be specified, since it is not
485 * necessarily always the current one.
486 */
487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488get_breakindent_win(
489 win_T *wp,
490 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491{
492 static int prev_indent = 0; /* cached indent value */
493 static long prev_ts = 0L; /* cached tabstop value */
494 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100495 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496 int bri = 0;
497 /* window width minus window margin space, i.e. what rests for text */
498 const int eff_wwidth = W_WIDTH(wp)
499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100505 || prev_tick != CHANGEDTICK(wp->w_buffer))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200511 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200513 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514
515 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 if (wp->w_p_brisbr)
517 bri -= vim_strsize(p_sbr);
518
519 /* Add offset for number column, if 'n' is in 'cpoptions' */
520 bri += win_col_off2(wp);
521
522 /* never indent past left window margin */
523 if (bri < 0)
524 bri = 0;
525 /* always leave at least bri_min characters on the left,
526 * if text width is sufficient */
527 else if (bri > eff_wwidth - wp->w_p_brimin)
528 bri = (eff_wwidth - wp->w_p_brimin < 0)
529 ? 0 : eff_wwidth - wp->w_p_brimin;
530
531 return bri;
532}
533#endif
534
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
537
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100538static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
541 * Return TRUE if the string "line" starts with a word from 'cinwords'.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *cinw;
547 char_u *cinw_buf;
548 int cinw_len;
549 int retval = FALSE;
550 int len;
551
552 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
553 cinw_buf = alloc((unsigned)cinw_len);
554 if (cinw_buf != NULL)
555 {
556 line = skipwhite(line);
557 for (cinw = curbuf->b_p_cinw; *cinw; )
558 {
559 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
560 if (STRNCMP(line, cinw_buf, len) == 0
561 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
562 {
563 retval = TRUE;
564 break;
565 }
566 }
567 vim_free(cinw_buf);
568 }
569 return retval;
570}
571#endif
572
573/*
574 * open_line: Add a new line below or above the current line.
575 *
576 * For VREPLACE mode, we only add a new line when we get to the end of the
577 * file, otherwise we just start replacing the next line.
578 *
579 * Caller must take care of undo. Since VREPLACE may affect any number of
580 * lines however, it may call u_save_cursor() again when starting to change a
581 * new line.
582 * "flags": OPENLINE_DELSPACES delete spaces after cursor
583 * OPENLINE_DO_COM format comments
584 * OPENLINE_KEEPTRAIL keep trailing spaces
585 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200586 * OPENLINE_COM_LIST format comments with list or 2nd line indent
587 *
588 * "second_line_indent": indent for after ^^D in Insert mode or if flag
589 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *
591 * Return TRUE for success, FALSE for failure
592 */
593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594open_line(
595 int dir, /* FORWARD or BACKWARD */
596 int flags,
597 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 char_u *saved_line; /* copy of the original line */
600 char_u *next_line = NULL; /* copy of the next line */
601 char_u *p_extra = NULL; /* what goes to next line */
602 int less_cols = 0; /* less columns for mark in new line */
603 int less_cols_off = 0; /* columns to skip for mark adjust */
604 pos_T old_cursor; /* old cursor position */
605 int newcol = 0; /* new cursor column */
606 int newindent = 0; /* auto-indent of the new line */
607 int n;
608 int trunc_line = FALSE; /* truncate current line afterwards */
609 int retval = FALSE; /* return value, default is FAIL */
610#ifdef FEAT_COMMENTS
611 int extra_len = 0; /* length of p_extra string */
612 int lead_len; /* length of comment leader */
613 char_u *lead_flags; /* position in 'comments' for comment leader */
614 char_u *leader = NULL; /* copy of comment leader */
615#endif
616 char_u *allocated = NULL; /* allocated memory */
617#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
618 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
619 char_u *p;
620#endif
621 int saved_char = NUL; /* init for GCC */
622#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
623 pos_T *pos;
624#endif
625#ifdef FEAT_SMARTINDENT
626 int do_si = (!p_paste && curbuf->b_p_si
627# ifdef FEAT_CINDENT
628 && !curbuf->b_p_cin
629# endif
630 );
631 int no_si = FALSE; /* reset did_si afterwards */
632 int first_char = NUL; /* init for GCC */
633#endif
634#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
635 int vreplace_mode;
636#endif
637 int did_append; /* appended a new line */
638 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
639
640 /*
641 * make a copy of the current line so we can mess with it
642 */
643 saved_line = vim_strsave(ml_get_curline());
644 if (saved_line == NULL) /* out of memory! */
645 return FALSE;
646
647#ifdef FEAT_VREPLACE
648 if (State & VREPLACE_FLAG)
649 {
650 /*
651 * With VREPLACE we make a copy of the next line, which we will be
652 * starting to replace. First make the new line empty and let vim play
653 * with the indenting and comment leader to its heart's content. Then
654 * we grab what it ended up putting on the new line, put back the
655 * original line, and call ins_char() to put each new character onto
656 * the line, replacing what was there before and pushing the right
657 * stuff onto the replace stack. -- webb.
658 */
659 if (curwin->w_cursor.lnum < orig_line_count)
660 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
661 else
662 next_line = vim_strsave((char_u *)"");
663 if (next_line == NULL) /* out of memory! */
664 goto theend;
665
666 /*
667 * In VREPLACE mode, a NL replaces the rest of the line, and starts
668 * replacing the next line, so push all of the characters left on the
669 * line onto the replace stack. We'll push any other characters that
670 * might be replaced at the start of the next line (due to autoindent
671 * etc) a bit later.
672 */
673 replace_push(NUL); /* Call twice because BS over NL expects it */
674 replace_push(NUL);
675 p = saved_line + curwin->w_cursor.col;
676 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000677 {
678#ifdef FEAT_MBYTE
679 if (has_mbyte)
680 p += replace_push_mb(p);
681 else
682#endif
683 replace_push(*p++);
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 saved_line[curwin->w_cursor.col] = NUL;
686 }
687#endif
688
689 if ((State & INSERT)
690#ifdef FEAT_VREPLACE
691 && !(State & VREPLACE_FLAG)
692#endif
693 )
694 {
695 p_extra = saved_line + curwin->w_cursor.col;
696#ifdef FEAT_SMARTINDENT
697 if (do_si) /* need first char after new line break */
698 {
699 p = skipwhite(p_extra);
700 first_char = *p;
701 }
702#endif
703#ifdef FEAT_COMMENTS
704 extra_len = (int)STRLEN(p_extra);
705#endif
706 saved_char = *p_extra;
707 *p_extra = NUL;
708 }
709
710 u_clearline(); /* cannot do "U" command when adding lines */
711#ifdef FEAT_SMARTINDENT
712 did_si = FALSE;
713#endif
714 ai_col = 0;
715
716 /*
717 * If we just did an auto-indent, then we didn't type anything on
718 * the prior line, and it should be truncated. Do this even if 'ai' is not
719 * set because automatically inserting a comment leader also sets did_ai.
720 */
721 if (dir == FORWARD && did_ai)
722 trunc_line = TRUE;
723
724 /*
725 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
726 * indent to use for the new line.
727 */
728 if (curbuf->b_p_ai
729#ifdef FEAT_SMARTINDENT
730 || do_si
731#endif
732 )
733 {
734 /*
735 * count white space on current line
736 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200737 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200738 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
739 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741#ifdef FEAT_SMARTINDENT
742 /*
743 * Do smart indenting.
744 * In insert/replace mode (only when dir == FORWARD)
745 * we may move some text to the next line. If it starts with '{'
746 * don't add an indent. Fixes inserting a NL before '{' in line
747 * "if (condition) {"
748 */
749 if (!trunc_line && do_si && *saved_line != NUL
750 && (p_extra == NULL || first_char != '{'))
751 {
752 char_u *ptr;
753 char_u last_char;
754
755 old_cursor = curwin->w_cursor;
756 ptr = saved_line;
757# ifdef FEAT_COMMENTS
758 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200759 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 else
761 lead_len = 0;
762# endif
763 if (dir == FORWARD)
764 {
765 /*
766 * Skip preprocessor directives, unless they are
767 * recognised as comments.
768 */
769 if (
770# ifdef FEAT_COMMENTS
771 lead_len == 0 &&
772# endif
773 ptr[0] == '#')
774 {
775 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
776 ptr = ml_get(--curwin->w_cursor.lnum);
777 newindent = get_indent();
778 }
779# ifdef FEAT_COMMENTS
780 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200781 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 else
783 lead_len = 0;
784 if (lead_len > 0)
785 {
786 /*
787 * This case gets the following right:
788 * \*
789 * * A comment (read '\' as '/').
790 * *\
791 * #define IN_THE_WAY
792 * This should line up here;
793 */
794 p = skipwhite(ptr);
795 if (p[0] == '/' && p[1] == '*')
796 p++;
797 if (p[0] == '*')
798 {
799 for (p++; *p; p++)
800 {
801 if (p[0] == '/' && p[-1] == '*')
802 {
803 /*
804 * End of C comment, indent should line up
805 * with the line containing the start of
806 * the comment
807 */
808 curwin->w_cursor.col = (colnr_T)(p - ptr);
809 if ((pos = findmatch(NULL, NUL)) != NULL)
810 {
811 curwin->w_cursor.lnum = pos->lnum;
812 newindent = get_indent();
813 }
814 }
815 }
816 }
817 }
818 else /* Not a comment line */
819# endif
820 {
821 /* Find last non-blank in line */
822 p = ptr + STRLEN(ptr) - 1;
823 while (p > ptr && vim_iswhite(*p))
824 --p;
825 last_char = *p;
826
827 /*
828 * find the character just before the '{' or ';'
829 */
830 if (last_char == '{' || last_char == ';')
831 {
832 if (p > ptr)
833 --p;
834 while (p > ptr && vim_iswhite(*p))
835 --p;
836 }
837 /*
838 * Try to catch lines that are split over multiple
839 * lines. eg:
840 * if (condition &&
841 * condition) {
842 * Should line up here!
843 * }
844 */
845 if (*p == ')')
846 {
847 curwin->w_cursor.col = (colnr_T)(p - ptr);
848 if ((pos = findmatch(NULL, '(')) != NULL)
849 {
850 curwin->w_cursor.lnum = pos->lnum;
851 newindent = get_indent();
852 ptr = ml_get_curline();
853 }
854 }
855 /*
856 * If last character is '{' do indent, without
857 * checking for "if" and the like.
858 */
859 if (last_char == '{')
860 {
861 did_si = TRUE; /* do indent */
862 no_si = TRUE; /* don't delete it when '{' typed */
863 }
864 /*
865 * Look for "if" and the like, use 'cinwords'.
866 * Don't do this if the previous line ended in ';' or
867 * '}'.
868 */
869 else if (last_char != ';' && last_char != '}'
870 && cin_is_cinword(ptr))
871 did_si = TRUE;
872 }
873 }
874 else /* dir == BACKWARD */
875 {
876 /*
877 * Skip preprocessor directives, unless they are
878 * recognised as comments.
879 */
880 if (
881# ifdef FEAT_COMMENTS
882 lead_len == 0 &&
883# endif
884 ptr[0] == '#')
885 {
886 int was_backslashed = FALSE;
887
888 while ((ptr[0] == '#' || was_backslashed) &&
889 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
890 {
891 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
892 was_backslashed = TRUE;
893 else
894 was_backslashed = FALSE;
895 ptr = ml_get(++curwin->w_cursor.lnum);
896 }
897 if (was_backslashed)
898 newindent = 0; /* Got to end of file */
899 else
900 newindent = get_indent();
901 }
902 p = skipwhite(ptr);
903 if (*p == '}') /* if line starts with '}': do indent */
904 did_si = TRUE;
905 else /* can delete indent when '{' typed */
906 can_si_back = TRUE;
907 }
908 curwin->w_cursor = old_cursor;
909 }
910 if (do_si)
911 can_si = TRUE;
912#endif /* FEAT_SMARTINDENT */
913
914 did_ai = TRUE;
915 }
916
917#ifdef FEAT_COMMENTS
918 /*
919 * Find out if the current line starts with a comment leader.
920 * This may then be inserted in front of the new line.
921 */
922 end_comment_pending = NUL;
923 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200924 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else
926 lead_len = 0;
927 if (lead_len > 0)
928 {
929 char_u *lead_repl = NULL; /* replaces comment leader */
930 int lead_repl_len = 0; /* length of *lead_repl */
931 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
932 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
933 char_u *comment_end = NULL; /* where lead_end has been found */
934 int extra_space = FALSE; /* append extra space */
935 int current_flag;
936 int require_blank = FALSE; /* requires blank after middle */
937 char_u *p2;
938
939 /*
940 * If the comment leader has the start, middle or end flag, it may not
941 * be used or may be replaced with the middle leader.
942 */
943 for (p = lead_flags; *p && *p != ':'; ++p)
944 {
945 if (*p == COM_BLANK)
946 {
947 require_blank = TRUE;
948 continue;
949 }
950 if (*p == COM_START || *p == COM_MIDDLE)
951 {
952 current_flag = *p;
953 if (*p == COM_START)
954 {
955 /*
956 * Doing "O" on a start of comment does not insert leader.
957 */
958 if (dir == BACKWARD)
959 {
960 lead_len = 0;
961 break;
962 }
963
964 /* find start of middle part */
965 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
966 require_blank = FALSE;
967 }
968
969 /*
970 * Isolate the strings of the middle and end leader.
971 */
972 while (*p && p[-1] != ':') /* find end of middle flags */
973 {
974 if (*p == COM_BLANK)
975 require_blank = TRUE;
976 ++p;
977 }
978 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
979
980 while (*p && p[-1] != ':') /* find end of end flags */
981 {
982 /* Check whether we allow automatic ending of comments */
983 if (*p == COM_AUTO_END)
984 end_comment_pending = -1; /* means we want to set it */
985 ++p;
986 }
987 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
988
989 if (end_comment_pending == -1) /* we can set it now */
990 end_comment_pending = lead_end[n - 1];
991
992 /*
993 * If the end of the comment is in the same line, don't use
994 * the comment leader.
995 */
996 if (dir == FORWARD)
997 {
998 for (p = saved_line + lead_len; *p; ++p)
999 if (STRNCMP(p, lead_end, n) == 0)
1000 {
1001 comment_end = p;
1002 lead_len = 0;
1003 break;
1004 }
1005 }
1006
1007 /*
1008 * Doing "o" on a start of comment inserts the middle leader.
1009 */
1010 if (lead_len > 0)
1011 {
1012 if (current_flag == COM_START)
1013 {
1014 lead_repl = lead_middle;
1015 lead_repl_len = (int)STRLEN(lead_middle);
1016 }
1017
1018 /*
1019 * If we have hit RETURN immediately after the start
1020 * comment leader, then put a space after the middle
1021 * comment leader on the next line.
1022 */
1023 if (!vim_iswhite(saved_line[lead_len - 1])
1024 && ((p_extra != NULL
1025 && (int)curwin->w_cursor.col == lead_len)
1026 || (p_extra == NULL
1027 && saved_line[lead_len] == NUL)
1028 || require_blank))
1029 extra_space = TRUE;
1030 }
1031 break;
1032 }
1033 if (*p == COM_END)
1034 {
1035 /*
1036 * Doing "o" on the end of a comment does not insert leader.
1037 * Remember where the end is, might want to use it to find the
1038 * start (for C-comments).
1039 */
1040 if (dir == FORWARD)
1041 {
1042 comment_end = skipwhite(saved_line);
1043 lead_len = 0;
1044 break;
1045 }
1046
1047 /*
1048 * Doing "O" on the end of a comment inserts the middle leader.
1049 * Find the string for the middle leader, searching backwards.
1050 */
1051 while (p > curbuf->b_p_com && *p != ',')
1052 --p;
1053 for (lead_repl = p; lead_repl > curbuf->b_p_com
1054 && lead_repl[-1] != ':'; --lead_repl)
1055 ;
1056 lead_repl_len = (int)(p - lead_repl);
1057
1058 /* We can probably always add an extra space when doing "O" on
1059 * the comment-end */
1060 extra_space = TRUE;
1061
1062 /* Check whether we allow automatic ending of comments */
1063 for (p2 = p; *p2 && *p2 != ':'; p2++)
1064 {
1065 if (*p2 == COM_AUTO_END)
1066 end_comment_pending = -1; /* means we want to set it */
1067 }
1068 if (end_comment_pending == -1)
1069 {
1070 /* Find last character in end-comment string */
1071 while (*p2 && *p2 != ',')
1072 p2++;
1073 end_comment_pending = p2[-1];
1074 }
1075 break;
1076 }
1077 if (*p == COM_FIRST)
1078 {
1079 /*
1080 * Comment leader for first line only: Don't repeat leader
1081 * when using "O", blank out leader when using "o".
1082 */
1083 if (dir == BACKWARD)
1084 lead_len = 0;
1085 else
1086 {
1087 lead_repl = (char_u *)"";
1088 lead_repl_len = 0;
1089 }
1090 break;
1091 }
1092 }
1093 if (lead_len)
1094 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001095 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001096 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001097 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 allocated = leader; /* remember to free it later */
1099
1100 if (leader == NULL)
1101 lead_len = 0;
1102 else
1103 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001104 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106 /*
1107 * Replace leader with lead_repl, right or left adjusted
1108 */
1109 if (lead_repl != NULL)
1110 {
1111 int c = 0;
1112 int off = 0;
1113
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001114 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else if (VIM_ISDIGIT(*p) || *p == '-')
1119 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 else
1121 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 if (c == COM_RIGHT) /* right adjusted leader */
1124 {
1125 /* find last non-white in the leader to line up with */
1126 for (p = leader + lead_len - 1; p > leader
1127 && vim_iswhite(*p); --p)
1128 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001130
1131#ifdef FEAT_MBYTE
1132 /* Compute the length of the replaced characters in
1133 * screen characters, not bytes. */
1134 {
1135 int repl_size = vim_strnsize(lead_repl,
1136 lead_repl_len);
1137 int old_size = 0;
1138 char_u *endp = p;
1139 int l;
1140
1141 while (old_size < repl_size && p > leader)
1142 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001143 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001144 old_size += ptr2cells(p);
1145 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001146 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 if (l != 0)
1148 mch_memmove(endp + l, endp,
1149 (size_t)((leader + lead_len) - endp));
1150 lead_len += l;
1151 }
1152#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (p < leader + lead_repl_len)
1154 p = leader;
1155 else
1156 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1159 if (p + lead_repl_len > leader + lead_len)
1160 p[lead_repl_len] = NUL;
1161
1162 /* blank-out any other chars from the old leader. */
1163 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164 {
1165#ifdef FEAT_MBYTE
1166 int l = mb_head_off(leader, p);
1167
1168 if (l > 1)
1169 {
1170 p -= l;
1171 if (ptr2cells(p) > 1)
1172 {
1173 p[1] = ' ';
1174 --l;
1175 }
1176 mch_memmove(p + 1, p + l + 1,
1177 (size_t)((leader + lead_len) - (p + l + 1)));
1178 lead_len -= l;
1179 *p = ' ';
1180 }
1181 else
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (!vim_iswhite(*p))
1184 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 else /* left adjusted leader */
1188 {
1189 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001190#ifdef FEAT_MBYTE
1191 /* Compute the length of the replaced characters in
1192 * screen characters, not bytes. Move the part that is
1193 * not to be overwritten. */
1194 {
1195 int repl_size = vim_strnsize(lead_repl,
1196 lead_repl_len);
1197 int i;
1198 int l;
1199
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001200 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001202 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001203 if (vim_strnsize(p, i + l) > repl_size)
1204 break;
1205 }
1206 if (i != lead_repl_len)
1207 {
1208 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001209 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001210 lead_len += lead_repl_len - i;
1211 }
1212 }
1213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1215
1216 /* Replace any remaining non-white chars in the old
1217 * leader by spaces. Keep Tabs, the indent must
1218 * remain the same. */
1219 for (p += lead_repl_len; p < leader + lead_len; ++p)
1220 if (!vim_iswhite(*p))
1221 {
1222 /* Don't put a space before a TAB. */
1223 if (p + 1 < leader + lead_len && p[1] == TAB)
1224 {
1225 --lead_len;
1226 mch_memmove(p, p + 1,
1227 (leader + lead_len) - p);
1228 }
1229 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001230 {
1231#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001232 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233
1234 if (l > 1)
1235 {
1236 if (ptr2cells(p) > 1)
1237 {
1238 /* Replace a double-wide char with
1239 * two spaces */
1240 --l;
1241 *p++ = ' ';
1242 }
1243 mch_memmove(p + 1, p + l,
1244 (leader + lead_len) - p);
1245 lead_len -= l - 1;
1246 }
1247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 *p = NUL;
1252 }
1253
1254 /* Recompute the indent, it may have changed. */
1255 if (curbuf->b_p_ai
1256#ifdef FEAT_SMARTINDENT
1257 || do_si
1258#endif
1259 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001260 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
1262 /* Add the indent offset */
1263 if (newindent + off < 0)
1264 {
1265 off = -newindent;
1266 newindent = 0;
1267 }
1268 else
1269 newindent += off;
1270
1271 /* Correct trailing spaces for the shift, so that
1272 * alignment remains equal. */
1273 while (off > 0 && lead_len > 0
1274 && leader[lead_len - 1] == ' ')
1275 {
1276 /* Don't do it when there is a tab before the space */
1277 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1278 break;
1279 --lead_len;
1280 --off;
1281 }
1282
1283 /* If the leader ends in white space, don't add an
1284 * extra space */
1285 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1286 extra_space = FALSE;
1287 leader[lead_len] = NUL;
1288 }
1289
1290 if (extra_space)
1291 {
1292 leader[lead_len++] = ' ';
1293 leader[lead_len] = NUL;
1294 }
1295
1296 newcol = lead_len;
1297
1298 /*
1299 * if a new indent will be set below, remove the indent that
1300 * is in the comment leader
1301 */
1302 if (newindent
1303#ifdef FEAT_SMARTINDENT
1304 || did_si
1305#endif
1306 )
1307 {
1308 while (lead_len && vim_iswhite(*leader))
1309 {
1310 --lead_len;
1311 --newcol;
1312 ++leader;
1313 }
1314 }
1315
1316 }
1317#ifdef FEAT_SMARTINDENT
1318 did_si = can_si = FALSE;
1319#endif
1320 }
1321 else if (comment_end != NULL)
1322 {
1323 /*
1324 * We have finished a comment, so we don't use the leader.
1325 * If this was a C-comment and 'ai' or 'si' is set do a normal
1326 * indent to align with the line containing the start of the
1327 * comment.
1328 */
1329 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1330 (curbuf->b_p_ai
1331#ifdef FEAT_SMARTINDENT
1332 || do_si
1333#endif
1334 ))
1335 {
1336 old_cursor = curwin->w_cursor;
1337 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1338 if ((pos = findmatch(NULL, NUL)) != NULL)
1339 {
1340 curwin->w_cursor.lnum = pos->lnum;
1341 newindent = get_indent();
1342 }
1343 curwin->w_cursor = old_cursor;
1344 }
1345 }
1346 }
1347#endif
1348
1349 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1350 if (p_extra != NULL)
1351 {
1352 *p_extra = saved_char; /* restore char that NUL replaced */
1353
1354 /*
1355 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1356 * non-blank.
1357 *
1358 * When in REPLACE mode, put the deleted blanks on the replace stack,
1359 * preceded by a NUL, so they can be put back when a BS is entered.
1360 */
1361 if (REPLACE_NORMAL(State))
1362 replace_push(NUL); /* end of extra blanks */
1363 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1364 {
1365 while ((*p_extra == ' ' || *p_extra == '\t')
1366#ifdef FEAT_MBYTE
1367 && (!enc_utf8
1368 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1369#endif
1370 )
1371 {
1372 if (REPLACE_NORMAL(State))
1373 replace_push(*p_extra);
1374 ++p_extra;
1375 ++less_cols_off;
1376 }
1377 }
1378 if (*p_extra != NUL)
1379 did_ai = FALSE; /* append some text, don't truncate now */
1380
1381 /* columns for marks adjusted for removed columns */
1382 less_cols = (int)(p_extra - saved_line);
1383 }
1384
1385 if (p_extra == NULL)
1386 p_extra = (char_u *)""; /* append empty line */
1387
1388#ifdef FEAT_COMMENTS
1389 /* concatenate leader and p_extra, if there is a leader */
1390 if (lead_len)
1391 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001392 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1393 {
1394 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001395 int padding = second_line_indent
1396 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001397
1398 /* Here whitespace is inserted after the comment char.
1399 * Below, set_indent(newindent, SIN_INSERT) will insert the
1400 * whitespace needed before the comment char. */
1401 for (i = 0; i < padding; i++)
1402 {
1403 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001404 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001405 newcol++;
1406 }
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 STRCAT(leader, p_extra);
1409 p_extra = leader;
1410 did_ai = TRUE; /* So truncating blanks works with comments */
1411 less_cols -= lead_len;
1412 }
1413 else
1414 end_comment_pending = NUL; /* turns out there was no leader */
1415#endif
1416
1417 old_cursor = curwin->w_cursor;
1418 if (dir == BACKWARD)
1419 --curwin->w_cursor.lnum;
1420#ifdef FEAT_VREPLACE
1421 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1422#endif
1423 {
1424 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1425 == FAIL)
1426 goto theend;
1427 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001428 * with markers.
1429 * Skip mark_adjust when adding a line after the last one, there can't
1430 * be marks there. */
1431 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count)
1432 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 did_append = TRUE;
1434 }
1435#ifdef FEAT_VREPLACE
1436 else
1437 {
1438 /*
1439 * In VREPLACE mode we are starting to replace the next line.
1440 */
1441 curwin->w_cursor.lnum++;
1442 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1443 {
1444 /* In case we NL to a new line, BS to the previous one, and NL
1445 * again, we don't want to save the new line for undo twice.
1446 */
1447 (void)u_save_cursor(); /* errors are ignored! */
1448 vr_lines_changed++;
1449 }
1450 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1451 changed_bytes(curwin->w_cursor.lnum, 0);
1452 curwin->w_cursor.lnum--;
1453 did_append = FALSE;
1454 }
1455#endif
1456
1457 if (newindent
1458#ifdef FEAT_SMARTINDENT
1459 || did_si
1460#endif
1461 )
1462 {
1463 ++curwin->w_cursor.lnum;
1464#ifdef FEAT_SMARTINDENT
1465 if (did_si)
1466 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001467 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001470 newindent -= newindent % sw;
1471 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001474 /* Copy the indent */
1475 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 (void)copy_indent(newindent, saved_line);
1478
1479 /*
1480 * Set the 'preserveindent' option so that any further screwing
1481 * with the line doesn't entirely destroy our efforts to preserve
1482 * it. It gets restored at the function end.
1483 */
1484 curbuf->b_p_pi = TRUE;
1485 }
1486 else
1487 (void)set_indent(newindent, SIN_INSERT);
1488 less_cols -= curwin->w_cursor.col;
1489
1490 ai_col = curwin->w_cursor.col;
1491
1492 /*
1493 * In REPLACE mode, for each character in the new indent, there must
1494 * be a NUL on the replace stack, for when it is deleted with BS
1495 */
1496 if (REPLACE_NORMAL(State))
1497 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1498 replace_push(NUL);
1499 newcol += curwin->w_cursor.col;
1500#ifdef FEAT_SMARTINDENT
1501 if (no_si)
1502 did_si = FALSE;
1503#endif
1504 }
1505
1506#ifdef FEAT_COMMENTS
1507 /*
1508 * In REPLACE mode, for each character in the extra leader, there must be
1509 * a NUL on the replace stack, for when it is deleted with BS.
1510 */
1511 if (REPLACE_NORMAL(State))
1512 while (lead_len-- > 0)
1513 replace_push(NUL);
1514#endif
1515
1516 curwin->w_cursor = old_cursor;
1517
1518 if (dir == FORWARD)
1519 {
1520 if (trunc_line || (State & INSERT))
1521 {
1522 /* truncate current line at cursor */
1523 saved_line[curwin->w_cursor.col] = NUL;
1524 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1525 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1526 truncate_spaces(saved_line);
1527 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1528 saved_line = NULL;
1529 if (did_append)
1530 {
1531 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1532 curwin->w_cursor.lnum + 1, 1L);
1533 did_append = FALSE;
1534
1535 /* Move marks after the line break to the new line. */
1536 if (flags & OPENLINE_MARKFIX)
1537 mark_col_adjust(curwin->w_cursor.lnum,
1538 curwin->w_cursor.col + less_cols_off,
1539 1L, (long)-less_cols);
1540 }
1541 else
1542 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1543 }
1544
1545 /*
1546 * Put the cursor on the new line. Careful: the scrollup() above may
1547 * have moved w_cursor, we must use old_cursor.
1548 */
1549 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1550 }
1551 if (did_append)
1552 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1553
1554 curwin->w_cursor.col = newcol;
1555#ifdef FEAT_VIRTUALEDIT
1556 curwin->w_cursor.coladd = 0;
1557#endif
1558
1559#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1560 /*
1561 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1562 * fixthisline() from doing it (via change_indent()) by telling it we're in
1563 * normal INSERT mode.
1564 */
1565 if (State & VREPLACE_FLAG)
1566 {
1567 vreplace_mode = State; /* So we know to put things right later */
1568 State = INSERT;
1569 }
1570 else
1571 vreplace_mode = 0;
1572#endif
1573#ifdef FEAT_LISP
1574 /*
1575 * May do lisp indenting.
1576 */
1577 if (!p_paste
1578# ifdef FEAT_COMMENTS
1579 && leader == NULL
1580# endif
1581 && curbuf->b_p_lisp
1582 && curbuf->b_p_ai)
1583 {
1584 fixthisline(get_lisp_indent);
1585 p = ml_get_curline();
1586 ai_col = (colnr_T)(skipwhite(p) - p);
1587 }
1588#endif
1589#ifdef FEAT_CINDENT
1590 /*
1591 * May do indenting after opening a new line.
1592 */
1593 if (!p_paste
1594 && (curbuf->b_p_cin
1595# ifdef FEAT_EVAL
1596 || *curbuf->b_p_inde != NUL
1597# endif
1598 )
1599 && in_cinkeys(dir == FORWARD
1600 ? KEY_OPEN_FORW
1601 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1602 {
1603 do_c_expr_indent();
1604 p = ml_get_curline();
1605 ai_col = (colnr_T)(skipwhite(p) - p);
1606 }
1607#endif
1608#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1609 if (vreplace_mode != 0)
1610 State = vreplace_mode;
1611#endif
1612
1613#ifdef FEAT_VREPLACE
1614 /*
1615 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1616 * original line, and inserts the new stuff char by char, pushing old stuff
1617 * onto the replace stack (via ins_char()).
1618 */
1619 if (State & VREPLACE_FLAG)
1620 {
1621 /* Put new line in p_extra */
1622 p_extra = vim_strsave(ml_get_curline());
1623 if (p_extra == NULL)
1624 goto theend;
1625
1626 /* Put back original line */
1627 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1628
1629 /* Insert new stuff into line again */
1630 curwin->w_cursor.col = 0;
1631#ifdef FEAT_VIRTUALEDIT
1632 curwin->w_cursor.coladd = 0;
1633#endif
1634 ins_bytes(p_extra); /* will call changed_bytes() */
1635 vim_free(p_extra);
1636 next_line = NULL;
1637 }
1638#endif
1639
1640 retval = TRUE; /* success! */
1641theend:
1642 curbuf->b_p_pi = saved_pi;
1643 vim_free(saved_line);
1644 vim_free(next_line);
1645 vim_free(allocated);
1646 return retval;
1647}
1648
1649#if defined(FEAT_COMMENTS) || defined(PROTO)
1650/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001651 * get_leader_len() returns the length in bytes of the prefix of the given
1652 * string which introduces a comment. If this string is not a comment then
1653 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * When "flags" is not NULL, it is set to point to the flags of the recognized
1655 * comment leader.
1656 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001657 * If "include_space" is set, include trailing whitespace while calculating the
1658 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
1660 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001661get_leader_len(
1662 char_u *line,
1663 char_u **flags,
1664 int backward,
1665 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001668 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int got_com = FALSE;
1670 int found_one;
1671 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1672 char_u *string; /* pointer to comment string */
1673 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001674 int middle_match_len = 0;
1675 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001676 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar81340392012-06-06 16:12:59 +02001678 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 while (vim_iswhite(line[i])) /* leading white space is ignored */
1680 ++i;
1681
1682 /*
1683 * Repeat to match several nested comment strings.
1684 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001685 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 /*
1688 * scan through the 'comments' option for a match
1689 */
1690 found_one = FALSE;
1691 for (list = curbuf->b_p_com; *list; )
1692 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001693 /* Get one option part into part_buf[]. Advance "list" to next
1694 * one. Put "string" at start of string. */
1695 if (!got_com && flags != NULL)
1696 *flags = list; /* remember where flags started */
1697 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1699 string = vim_strchr(part_buf, ':');
1700 if (string == NULL) /* missing ':', ignore this part */
1701 continue;
1702 *string++ = NUL; /* isolate flags from string */
1703
Bram Moolenaara4271d52011-05-10 13:38:27 +02001704 /* If we found a middle match previously, use that match when this
1705 * is not a middle or end. */
1706 if (middle_match_len != 0
1707 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1708 && vim_strchr(part_buf, COM_END) == NULL)
1709 break;
1710
1711 /* When we already found a nested comment, only accept further
1712 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1714 continue;
1715
Bram Moolenaara4271d52011-05-10 13:38:27 +02001716 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * When string starts with white space, must have some white space
1722 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (vim_iswhite(string[0]))
1725 {
1726 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001727 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 while (vim_iswhite(string[0]))
1729 ++string;
1730 }
1731 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1732 ;
1733 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001734 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaara4271d52011-05-10 13:38:27 +02001736 /* When 'b' flag used, there must be white space or an
1737 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (vim_strchr(part_buf, COM_BLANK) != NULL
1739 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1740 continue;
1741
Bram Moolenaara4271d52011-05-10 13:38:27 +02001742 /* We have found a match, stop searching unless this is a middle
1743 * comment. The middle comment can be a substring of the end
1744 * comment in which case it's better to return the length of the
1745 * end comment and its flags. Thus we keep searching with middle
1746 * and end matches and use an end match if it matches better. */
1747 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1748 {
1749 if (middle_match_len == 0)
1750 {
1751 middle_match_len = j;
1752 saved_flags = prev_list;
1753 }
1754 continue;
1755 }
1756 if (middle_match_len != 0 && j > middle_match_len)
1757 /* Use this match instead of the middle match, since it's a
1758 * longer thus better match. */
1759 middle_match_len = 0;
1760
1761 if (middle_match_len == 0)
1762 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 found_one = TRUE;
1764 break;
1765 }
1766
Bram Moolenaara4271d52011-05-10 13:38:27 +02001767 if (middle_match_len != 0)
1768 {
1769 /* Use the previously found middle match after failing to find a
1770 * match with an end. */
1771 if (!got_com && flags != NULL)
1772 *flags = saved_flags;
1773 i += middle_match_len;
1774 found_one = TRUE;
1775 }
1776
1777 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (!found_one)
1779 break;
1780
Bram Moolenaar81340392012-06-06 16:12:59 +02001781 result = i;
1782
Bram Moolenaara4271d52011-05-10 13:38:27 +02001783 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 while (vim_iswhite(line[i]))
1785 ++i;
1786
Bram Moolenaar81340392012-06-06 16:12:59 +02001787 if (include_space)
1788 result = i;
1789
Bram Moolenaara4271d52011-05-10 13:38:27 +02001790 /* If this comment doesn't nest, stop here. */
1791 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (vim_strchr(part_buf, COM_NEST) == NULL)
1793 break;
1794 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001795 return result;
1796}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001797
Bram Moolenaar81340392012-06-06 16:12:59 +02001798/*
1799 * Return the offset at which the last comment in line starts. If there is no
1800 * comment in the whole line, -1 is returned.
1801 *
1802 * When "flags" is not null, it is set to point to the flags describing the
1803 * recognized comment leader.
1804 */
1805 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001807{
1808 int result = -1;
1809 int i, j;
1810 int lower_check_bound = 0;
1811 char_u *string;
1812 char_u *com_leader;
1813 char_u *com_flags;
1814 char_u *list;
1815 int found_one;
1816 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1817
1818 /*
1819 * Repeat to match several nested comment strings.
1820 */
1821 i = (int)STRLEN(line);
1822 while (--i >= lower_check_bound)
1823 {
1824 /*
1825 * scan through the 'comments' option for a match
1826 */
1827 found_one = FALSE;
1828 for (list = curbuf->b_p_com; *list; )
1829 {
1830 char_u *flags_save = list;
1831
1832 /*
1833 * Get one option part into part_buf[]. Advance list to next one.
1834 * put string at start of string.
1835 */
1836 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1837 string = vim_strchr(part_buf, ':');
1838 if (string == NULL) /* If everything is fine, this cannot actually
1839 * happen. */
1840 {
1841 continue;
1842 }
1843 *string++ = NUL; /* Isolate flags from string. */
1844 com_leader = string;
1845
1846 /*
1847 * Line contents and string must match.
1848 * When string starts with white space, must have some white space
1849 * (but the amount does not need to match, there might be a mix of
1850 * TABs and spaces).
1851 */
1852 if (vim_iswhite(string[0]))
1853 {
1854 if (i == 0 || !vim_iswhite(line[i - 1]))
1855 continue;
1856 while (vim_iswhite(string[0]))
1857 ++string;
1858 }
1859 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1860 /* do nothing */;
1861 if (string[j] != NUL)
1862 continue;
1863
1864 /*
1865 * When 'b' flag used, there must be white space or an
1866 * end-of-line after the string in the line.
1867 */
1868 if (vim_strchr(part_buf, COM_BLANK) != NULL
1869 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1870 {
1871 continue;
1872 }
1873
1874 /*
1875 * We have found a match, stop searching.
1876 */
1877 found_one = TRUE;
1878
1879 if (flags)
1880 *flags = flags_save;
1881 com_flags = flags_save;
1882
1883 break;
1884 }
1885
1886 if (found_one)
1887 {
1888 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1889 int len1, len2, off;
1890
1891 result = i;
1892 /*
1893 * If this comment nests, continue searching.
1894 */
1895 if (vim_strchr(part_buf, COM_NEST) != NULL)
1896 continue;
1897
1898 lower_check_bound = i;
1899
1900 /* Let's verify whether the comment leader found is a substring
1901 * of other comment leaders. If it is, let's adjust the
1902 * lower_check_bound so that we make sure that we have determined
1903 * the comment leader correctly.
1904 */
1905
1906 while (vim_iswhite(*com_leader))
1907 ++com_leader;
1908 len1 = (int)STRLEN(com_leader);
1909
1910 for (list = curbuf->b_p_com; *list; )
1911 {
1912 char_u *flags_save = list;
1913
1914 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1915 if (flags_save == com_flags)
1916 continue;
1917 string = vim_strchr(part_buf2, ':');
1918 ++string;
1919 while (vim_iswhite(*string))
1920 ++string;
1921 len2 = (int)STRLEN(string);
1922 if (len2 == 0)
1923 continue;
1924
1925 /* Now we have to verify whether string ends with a substring
1926 * beginning the com_leader. */
1927 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1928 {
1929 --off;
1930 if (!STRNCMP(string + off, com_leader, len2 - off))
1931 {
1932 if (i - off < lower_check_bound)
1933 lower_check_bound = i - off;
1934 }
1935 }
1936 }
1937 }
1938 }
1939 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941#endif
1942
1943/*
1944 * Return the number of window lines occupied by buffer line "lnum".
1945 */
1946 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001947plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 return plines_win(curwin, lnum, TRUE);
1950}
1951
1952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001953plines_win(
1954 win_T *wp,
1955 linenr_T lnum,
1956 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958#if defined(FEAT_DIFF) || defined(PROTO)
1959 /* Check for filler lines above this buffer line. When folded the result
1960 * is one line anyway. */
1961 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1962}
1963
1964 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001965plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 return plines_win_nofill(curwin, lnum, TRUE);
1968}
1969
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971plines_win_nofill(
1972 win_T *wp,
1973 linenr_T lnum,
1974 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976#endif
1977 int lines;
1978
1979 if (!wp->w_p_wrap)
1980 return 1;
1981
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001982#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (wp->w_width == 0)
1984 return 1;
1985#endif
1986
1987#ifdef FEAT_FOLDING
1988 /* A folded lines is handled just like an empty line. */
1989 /* NOTE: Caller must handle lines that are MAYBE folded. */
1990 if (lineFolded(wp, lnum) == TRUE)
1991 return 1;
1992#endif
1993
1994 lines = plines_win_nofold(wp, lnum);
1995 if (winheight > 0 && lines > wp->w_height)
1996 return (int)wp->w_height;
1997 return lines;
1998}
1999
2000/*
2001 * Return number of window lines physical line "lnum" will occupy in window
2002 * "wp". Does not care about folding, 'wrap' or 'diff'.
2003 */
2004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 char_u *s;
2008 long col;
2009 int width;
2010
2011 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2012 if (*s == NUL) /* empty line */
2013 return 1;
2014 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2015
2016 /*
2017 * If list mode is on, then the '$' at the end of the line may take up one
2018 * extra column.
2019 */
2020 if (wp->w_p_list && lcs_eol != NUL)
2021 col += 1;
2022
2023 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002024 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 */
2026 width = W_WIDTH(wp) - win_col_off(wp);
2027 if (width <= 0)
2028 return 32000;
2029 if (col <= width)
2030 return 1;
2031 col -= width;
2032 width += win_col_off2(wp);
2033 return (col + (width - 1)) / width + 1;
2034}
2035
2036/*
2037 * Like plines_win(), but only reports the number of physical screen lines
2038 * used from the start of the line to the given column number.
2039 */
2040 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002041plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
2043 long col;
2044 char_u *s;
2045 int lines = 0;
2046 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002047 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049#ifdef FEAT_DIFF
2050 /* Check for filler lines above this buffer line. When folded the result
2051 * is one line anyway. */
2052 lines = diff_check_fill(wp, lnum);
2053#endif
2054
2055 if (!wp->w_p_wrap)
2056 return lines + 1;
2057
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002058#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (wp->w_width == 0)
2060 return lines + 1;
2061#endif
2062
Bram Moolenaar597a4222014-06-25 14:39:50 +02002063 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 col = 0;
2066 while (*s != NUL && --column >= 0)
2067 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002068 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002069 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071
2072 /*
2073 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2074 * INSERT mode, then col must be adjusted so that it represents the last
2075 * screen position of the TAB. This only fixes an error when the TAB wraps
2076 * from one screen line to the next (when 'columns' is not a multiple of
2077 * 'ts') -- webb.
2078 */
2079 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002080 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002083 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 */
2085 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002086 if (width <= 0)
2087 return 9999;
2088
2089 lines += 1;
2090 if (col > width)
2091 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2092 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093}
2094
2095 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 int count = 0;
2099
2100 while (first <= last)
2101 {
2102#ifdef FEAT_FOLDING
2103 int x;
2104
2105 /* Check if there are any really folded lines, but also included lines
2106 * that are maybe folded. */
2107 x = foldedCount(wp, first, NULL);
2108 if (x > 0)
2109 {
2110 ++count; /* count 1 for "+-- folded" line */
2111 first += x;
2112 }
2113 else
2114#endif
2115 {
2116#ifdef FEAT_DIFF
2117 if (first == wp->w_topline)
2118 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2119 else
2120#endif
2121 count += plines_win(wp, first, TRUE);
2122 ++first;
2123 }
2124 }
2125 return (count);
2126}
2127
2128#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2129/*
2130 * Insert string "p" at the cursor position. Stops at a NUL byte.
2131 * Handles Replace mode and multi-byte characters.
2132 */
2133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 ins_bytes_len(p, (int)STRLEN(p));
2137}
2138#endif
2139
2140#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2141 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2142/*
2143 * Insert string "p" with length "len" at the cursor position.
2144 * Handles Replace mode and multi-byte characters.
2145 */
2146 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int i;
2150# ifdef FEAT_MBYTE
2151 int n;
2152
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002153 if (has_mbyte)
2154 for (i = 0; i < len; i += n)
2155 {
2156 if (enc_utf8)
2157 /* avoid reading past p[len] */
2158 n = utfc_ptr2len_len(p + i, len - i);
2159 else
2160 n = (*mb_ptr2len)(p + i);
2161 ins_char_bytes(p + i, n);
2162 }
2163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002165 for (i = 0; i < len; ++i)
2166 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168#endif
2169
2170/*
2171 * Insert or replace a single character at the cursor position.
2172 * When in REPLACE or VREPLACE mode, replace any existing character.
2173 * Caller must have prepared for undo.
2174 * For multi-byte characters we get the whole character, the caller must
2175 * convert bytes to a character.
2176 */
2177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002178ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002180 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002181 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002183#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 n = (*mb_char2bytes)(c, buf);
2185
2186 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2187 * Happens for CTRL-Vu9900. */
2188 if (buf[0] == 0)
2189 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002190#else
2191 buf[0] = c;
2192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 ins_char_bytes(buf, n);
2195}
2196
2197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002198ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 int newlen; /* nr of bytes inserted */
2202 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2203 char_u *p;
2204 char_u *newp;
2205 char_u *oldp;
2206 int linelen; /* length of old line including NUL */
2207 colnr_T col;
2208 linenr_T lnum = curwin->w_cursor.lnum;
2209 int i;
2210
2211#ifdef FEAT_VIRTUALEDIT
2212 /* Break tabs if needed. */
2213 if (virtual_active() && curwin->w_cursor.coladd > 0)
2214 coladvance_force(getviscol());
2215#endif
2216
2217 col = curwin->w_cursor.col;
2218 oldp = ml_get(lnum);
2219 linelen = (int)STRLEN(oldp) + 1;
2220
2221 /* The lengths default to the values for when not replacing. */
2222 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 if (State & REPLACE_FLAG)
2226 {
2227#ifdef FEAT_VREPLACE
2228 if (State & VREPLACE_FLAG)
2229 {
2230 colnr_T new_vcol = 0; /* init for GCC */
2231 colnr_T vcol;
2232 int old_list;
2233#ifndef FEAT_MBYTE
2234 char_u buf[2];
2235#endif
2236
2237 /*
2238 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2239 * Returns the old value of list, so when finished,
2240 * curwin->w_p_list should be set back to this.
2241 */
2242 old_list = curwin->w_p_list;
2243 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2244 curwin->w_p_list = FALSE;
2245
2246 /*
2247 * In virtual replace mode each character may replace one or more
2248 * characters (zero if it's a TAB). Count the number of bytes to
2249 * be deleted to make room for the new character, counting screen
2250 * cells. May result in adding spaces to fill a gap.
2251 */
2252 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2253#ifndef FEAT_MBYTE
2254 buf[0] = c;
2255 buf[1] = NUL;
2256#endif
2257 new_vcol = vcol + chartabsize(buf, vcol);
2258 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2259 {
2260 vcol += chartabsize(oldp + col + oldlen, vcol);
2261 /* Don't need to remove a TAB that takes us to the right
2262 * position. */
2263 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2264 break;
2265#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002266 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#else
2268 ++oldlen;
2269#endif
2270 /* Deleted a bit too much, insert spaces. */
2271 if (vcol > new_vcol)
2272 newlen += vcol - new_vcol;
2273 }
2274 curwin->w_p_list = old_list;
2275 }
2276 else
2277#endif
2278 if (oldp[col] != NUL)
2279 {
2280 /* normal replace */
2281#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002282 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283#else
2284 oldlen = 1;
2285#endif
2286 }
2287
2288
2289 /* Push the replaced bytes onto the replace stack, so that they can be
2290 * put back when BS is used. The bytes of a multi-byte character are
2291 * done the other way around, so that the first byte is popped off
2292 * first (it tells the byte length of the character). */
2293 replace_push(NUL);
2294 for (i = 0; i < oldlen; ++i)
2295 {
2296#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002297 if (has_mbyte)
2298 i += replace_push_mb(oldp + col + i) - 1;
2299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002301 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 }
2304
2305 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2306 if (newp == NULL)
2307 return;
2308
2309 /* Copy bytes before the cursor. */
2310 if (col > 0)
2311 mch_memmove(newp, oldp, (size_t)col);
2312
2313 /* Copy bytes after the changed character(s). */
2314 p = newp + col;
2315 mch_memmove(p + newlen, oldp + col + oldlen,
2316 (size_t)(linelen - col - oldlen));
2317
2318 /* Insert or overwrite the new character. */
2319#ifdef FEAT_MBYTE
2320 mch_memmove(p, buf, charlen);
2321 i = charlen;
2322#else
2323 *p = c;
2324 i = 1;
2325#endif
2326
2327 /* Fill with spaces when necessary. */
2328 while (i < newlen)
2329 p[i++] = ' ';
2330
2331 /* Replace the line in the buffer. */
2332 ml_replace(lnum, newp, FALSE);
2333
2334 /* mark the buffer as changed and prepare for displaying */
2335 changed_bytes(lnum, col);
2336
2337 /*
2338 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2339 * show the match for right parens and braces.
2340 */
2341 if (p_sm && (State & INSERT)
2342 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002343#ifdef FEAT_INS_EXPAND
2344 && !ins_compl_active()
2345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002347 {
2348#ifdef FEAT_MBYTE
2349 if (has_mbyte)
2350 showmatch(mb_ptr2char(buf));
2351 else
2352#endif
2353 showmatch(c);
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356#ifdef FEAT_RIGHTLEFT
2357 if (!p_ri || (State & REPLACE_FLAG))
2358#endif
2359 {
2360 /* Normal insert: move cursor right */
2361#ifdef FEAT_MBYTE
2362 curwin->w_cursor.col += charlen;
2363#else
2364 ++curwin->w_cursor.col;
2365#endif
2366 }
2367 /*
2368 * TODO: should try to update w_row here, to avoid recomputing it later.
2369 */
2370}
2371
2372/*
2373 * Insert a string at the cursor position.
2374 * Note: Does NOT handle Replace mode.
2375 * Caller must have prepared for undo.
2376 */
2377 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002378ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
2380 char_u *oldp, *newp;
2381 int newlen = (int)STRLEN(s);
2382 int oldlen;
2383 colnr_T col;
2384 linenr_T lnum = curwin->w_cursor.lnum;
2385
2386#ifdef FEAT_VIRTUALEDIT
2387 if (virtual_active() && curwin->w_cursor.coladd > 0)
2388 coladvance_force(getviscol());
2389#endif
2390
2391 col = curwin->w_cursor.col;
2392 oldp = ml_get(lnum);
2393 oldlen = (int)STRLEN(oldp);
2394
2395 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2396 if (newp == NULL)
2397 return;
2398 if (col > 0)
2399 mch_memmove(newp, oldp, (size_t)col);
2400 mch_memmove(newp + col, s, (size_t)newlen);
2401 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2402 ml_replace(lnum, newp, FALSE);
2403 changed_bytes(lnum, col);
2404 curwin->w_cursor.col += newlen;
2405}
2406
2407/*
2408 * Delete one character under the cursor.
2409 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2410 * Caller must have prepared for undo.
2411 *
2412 * return FAIL for failure, OK otherwise
2413 */
2414 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002415del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417#ifdef FEAT_MBYTE
2418 if (has_mbyte)
2419 {
2420 /* Make sure the cursor is at the start of a character. */
2421 mb_adjust_cursor();
2422 if (*ml_get_cursor() == NUL)
2423 return FAIL;
2424 return del_chars(1L, fixpos);
2425 }
2426#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002427 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428}
2429
2430#if defined(FEAT_MBYTE) || defined(PROTO)
2431/*
2432 * Like del_bytes(), but delete characters instead of bytes.
2433 */
2434 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002435del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 long bytes = 0;
2438 long i;
2439 char_u *p;
2440 int l;
2441
2442 p = ml_get_cursor();
2443 for (i = 0; i < count && *p != NUL; ++i)
2444 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002445 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 bytes += l;
2447 p += l;
2448 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002449 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450}
2451#endif
2452
2453/*
2454 * Delete "count" bytes under the cursor.
2455 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2456 * Caller must have prepared for undo.
2457 *
2458 * return FAIL for failure, OK otherwise
2459 */
2460 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461del_bytes(
2462 long count,
2463 int fixpos_arg,
2464 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 char_u *oldp, *newp;
2467 colnr_T oldlen;
2468 linenr_T lnum = curwin->w_cursor.lnum;
2469 colnr_T col = curwin->w_cursor.col;
2470 int was_alloced;
2471 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002472 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 oldp = ml_get(lnum);
2475 oldlen = (int)STRLEN(oldp);
2476
2477 /*
2478 * Can't do anything when the cursor is on the NUL after the line.
2479 */
2480 if (col >= oldlen)
2481 return FAIL;
2482
2483#ifdef FEAT_MBYTE
2484 /* If 'delcombine' is set and deleting (less than) one character, only
2485 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002486 if (p_deco && use_delcombine && enc_utf8
2487 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002489 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 int n;
2491
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002492 (void)utfc_ptr2char(oldp + col, cc);
2493 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 /* Find the last composing char, there can be several. */
2496 n = col;
2497 do
2498 {
2499 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002500 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 n += count;
2502 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2503 fixpos = 0;
2504 }
2505 }
2506#endif
2507
2508 /*
2509 * When count is too big, reduce it.
2510 */
2511 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2512 if (movelen <= 1)
2513 {
2514 /*
2515 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002516 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2517 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002519 if (col > 0 && fixpos && restart_edit == 0
2520#ifdef FEAT_VIRTUALEDIT
2521 && (ve_flags & VE_ONEMORE) == 0
2522#endif
2523 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 --curwin->w_cursor.col;
2526#ifdef FEAT_VIRTUALEDIT
2527 curwin->w_cursor.coladd = 0;
2528#endif
2529#ifdef FEAT_MBYTE
2530 if (has_mbyte)
2531 curwin->w_cursor.col -=
2532 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2533#endif
2534 }
2535 count = oldlen - col;
2536 movelen = 1;
2537 }
2538
2539 /*
2540 * If the old line has been allocated the deletion can be done in the
2541 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002542 * Can't do this when using Netbeans, because we would need to invoke
2543 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002544 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002547 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002548 was_alloced = FALSE;
2549 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002551 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (was_alloced)
2553 newp = oldp; /* use same allocated memory */
2554 else
2555 { /* need to allocate a new line */
2556 newp = alloc((unsigned)(oldlen + 1 - count));
2557 if (newp == NULL)
2558 return FAIL;
2559 mch_memmove(newp, oldp, (size_t)col);
2560 }
2561 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2562 if (!was_alloced)
2563 ml_replace(lnum, newp, FALSE);
2564
2565 /* mark the buffer as changed and prepare for displaying */
2566 changed_bytes(lnum, curwin->w_cursor.col);
2567
2568 return OK;
2569}
2570
2571/*
2572 * Delete from cursor to end of line.
2573 * Caller must have prepared for undo.
2574 *
2575 * return FAIL for failure, OK otherwise
2576 */
2577 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002578truncate_line(
2579 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 char_u *newp;
2582 linenr_T lnum = curwin->w_cursor.lnum;
2583 colnr_T col = curwin->w_cursor.col;
2584
2585 if (col == 0)
2586 newp = vim_strsave((char_u *)"");
2587 else
2588 newp = vim_strnsave(ml_get(lnum), col);
2589
2590 if (newp == NULL)
2591 return FAIL;
2592
2593 ml_replace(lnum, newp, FALSE);
2594
2595 /* mark the buffer as changed and prepare for displaying */
2596 changed_bytes(lnum, curwin->w_cursor.col);
2597
2598 /*
2599 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2600 */
2601 if (fixpos && curwin->w_cursor.col > 0)
2602 --curwin->w_cursor.col;
2603
2604 return OK;
2605}
2606
2607/*
2608 * Delete "nlines" lines at the cursor.
2609 * Saves the lines for undo first if "undo" is TRUE.
2610 */
2611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002612del_lines(
2613 long nlines, /* number of lines to delete */
2614 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002617 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 if (nlines <= 0)
2620 return;
2621
2622 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002623 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 return;
2625
2626 for (n = 0; n < nlines; )
2627 {
2628 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2629 break;
2630
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002631 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 ++n;
2633
2634 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002635 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 break;
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002639 /* Correct the cursor position before calling deleted_lines_mark(), it may
2640 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 curwin->w_cursor.col = 0;
2642 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002643
2644 /* adjust marks, mark the buffer as changed and prepare for displaying */
2645 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646}
2647
2648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002649gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 char_u *ptr = ml_get_pos(pos);
2652
2653#ifdef FEAT_MBYTE
2654 if (has_mbyte)
2655 return (*mb_ptr2char)(ptr);
2656#endif
2657 return (int)*ptr;
2658}
2659
2660 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002661gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663#ifdef FEAT_MBYTE
2664 if (has_mbyte)
2665 return (*mb_ptr2char)(ml_get_cursor());
2666#endif
2667 return (int)*ml_get_cursor();
2668}
2669
2670/*
2671 * Write a character at the current cursor position.
2672 * It is directly written into the block.
2673 */
2674 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002675pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2678 + curwin->w_cursor.col) = c;
2679}
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681/*
2682 * When extra == 0: Return TRUE if the cursor is before or on the first
2683 * non-blank in the line.
2684 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2685 * the line.
2686 */
2687 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002688inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 char_u *ptr;
2691 colnr_T col;
2692
2693 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2694 ++ptr;
2695 if (col >= curwin->w_cursor.col + extra)
2696 return TRUE;
2697 else
2698 return FALSE;
2699}
2700
2701/*
2702 * Skip to next part of an option argument: Skip space and comma.
2703 */
2704 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002705skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 if (*p == ',')
2708 ++p;
2709 while (*p == ' ')
2710 ++p;
2711 return p;
2712}
2713
2714/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002715 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 *
2717 * Most often called through changed_bytes() and changed_lines(), which also
2718 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002719 *
2720 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
2722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002723changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2726 /* The text of the preediting area is inserted, but this doesn't
2727 * mean a change of the buffer yet. That is delayed until the
2728 * text is committed. (this means preedit becomes empty) */
2729 if (im_is_preediting() && !xim_changed_while_preediting)
2730 return;
2731 xim_changed_while_preediting = FALSE;
2732#endif
2733
2734 if (!curbuf->b_changed)
2735 {
2736 int save_msg_scroll = msg_scroll;
2737
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002738 /* Give a warning about changing a read-only file. This may also
2739 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /* Create a swap file if that is wanted.
2743 * Don't do this for "nofile" and "nowrite" buffer types. */
2744 if (curbuf->b_may_swap
2745#ifdef FEAT_QUICKFIX
2746 && !bt_dontwrite(curbuf)
2747#endif
2748 )
2749 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002750 int save_need_wait_return = need_wait_return;
2751
2752 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 ml_open_file(curbuf);
2754
2755 /* The ml_open_file() can cause an ATTENTION message.
2756 * Wait two seconds, to make sure the user reads this unexpected
2757 * message. Since we could be anywhere, call wait_return() now,
2758 * and don't let the emsg() set msg_scroll. */
2759 if (need_wait_return && emsg_silent == 0)
2760 {
2761 out_flush();
2762 ui_delay(2000L, TRUE);
2763 wait_return(TRUE);
2764 msg_scroll = save_msg_scroll;
2765 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002766 else
2767 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002769 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002771 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002774/*
2775 * Internal part of changed(), no user interaction.
2776 */
2777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002778changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002779{
2780 curbuf->b_changed = TRUE;
2781 ml_setflags(curbuf);
2782#ifdef FEAT_WINDOWS
2783 check_status(curbuf);
2784 redraw_tabline = TRUE;
2785#endif
2786#ifdef FEAT_TITLE
2787 need_maketitle = TRUE; /* set window title later */
2788#endif
2789}
2790
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002791static void changedOneline(buf_T *buf, linenr_T lnum);
2792static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2793static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795/*
2796 * Changed bytes within a single line for the current buffer.
2797 * - marks the windows on this buffer to be redisplayed
2798 * - marks the buffer changed by calling changed()
2799 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002800 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 */
2802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002803changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002805 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807
2808#ifdef FEAT_DIFF
2809 /* Diff highlighting in other diff windows may need to be updated too. */
2810 if (curwin->w_p_diff)
2811 {
2812 win_T *wp;
2813 linenr_T wlnum;
2814
Bram Moolenaar29323592016-07-24 22:04:11 +02002815 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002816 if (wp->w_p_diff && wp != curwin)
2817 {
2818 redraw_win_later(wp, VALID);
2819 wlnum = diff_lnum_win(lnum, wp);
2820 if (wlnum > 0)
2821 changedOneline(wp->w_buffer, wlnum);
2822 }
2823 }
2824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825}
2826
2827 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002828changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002830 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002833 if (lnum < buf->b_mod_top)
2834 buf->b_mod_top = lnum;
2835 else if (lnum >= buf->b_mod_bot)
2836 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838 else
2839 {
2840 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002841 buf->b_mod_set = TRUE;
2842 buf->b_mod_top = lnum;
2843 buf->b_mod_bot = lnum + 1;
2844 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846}
2847
2848/*
2849 * Appended "count" lines below line "lnum" in the current buffer.
2850 * Must be called AFTER the change and after mark_adjust().
2851 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2852 */
2853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002854appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 changed_lines(lnum + 1, 0, lnum + 1, count);
2857}
2858
2859/*
2860 * Like appended_lines(), but adjust marks first.
2861 */
2862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002863appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002865 /* Skip mark_adjust when adding a line after the last one, there can't
2866 * be marks there. */
2867 if (lnum + count < curbuf->b_ml.ml_line_count)
2868 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 changed_lines(lnum + 1, 0, lnum + 1, count);
2870}
2871
2872/*
2873 * Deleted "count" lines at line "lnum" in the current buffer.
2874 * Must be called AFTER the change and after mark_adjust().
2875 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2876 */
2877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 changed_lines(lnum, 0, lnum + count, -count);
2881}
2882
2883/*
2884 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002885 * Make sure the cursor is on a valid line before calling, a GUI callback may
2886 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
2888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2892 changed_lines(lnum, 0, lnum + count, -count);
2893}
2894
2895/*
2896 * Changed lines for the current buffer.
2897 * Must be called AFTER the change and after mark_adjust().
2898 * - mark the buffer changed by calling changed()
2899 * - mark the windows on this buffer to be redisplayed
2900 * - invalidate cached values
2901 * "lnum" is the first line that needs displaying, "lnume" the first line
2902 * below the changed lines (BEFORE the change).
2903 * When only inserting lines, "lnum" and "lnume" are equal.
2904 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002905 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 */
2907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002908changed_lines(
2909 linenr_T lnum, /* first line with change */
2910 colnr_T col, /* column in first line with change */
2911 linenr_T lnume, /* line below last changed line */
2912 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002914 changed_lines_buf(curbuf, lnum, lnume, xtra);
2915
2916#ifdef FEAT_DIFF
2917 if (xtra == 0 && curwin->w_p_diff)
2918 {
2919 /* When the number of lines doesn't change then mark_adjust() isn't
2920 * called and other diff buffers still need to be marked for
2921 * displaying. */
2922 win_T *wp;
2923 linenr_T wlnum;
2924
Bram Moolenaar29323592016-07-24 22:04:11 +02002925 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002926 if (wp->w_p_diff && wp != curwin)
2927 {
2928 redraw_win_later(wp, VALID);
2929 wlnum = diff_lnum_win(lnum, wp);
2930 if (wlnum > 0)
2931 changed_lines_buf(wp->w_buffer, wlnum,
2932 lnume - lnum + wlnum, 0L);
2933 }
2934 }
2935#endif
2936
2937 changed_common(lnum, col, lnume, xtra);
2938}
2939
2940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002941changed_lines_buf(
2942 buf_T *buf,
2943 linenr_T lnum, /* first line with change */
2944 linenr_T lnume, /* line below last changed line */
2945 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002946{
2947 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002950 if (lnum < buf->b_mod_top)
2951 buf->b_mod_top = lnum;
2952 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
2954 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002955 buf->b_mod_bot += xtra;
2956 if (buf->b_mod_bot < lnum)
2957 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002959 if (lnume + xtra > buf->b_mod_bot)
2960 buf->b_mod_bot = lnume + xtra;
2961 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
2963 else
2964 {
2965 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002966 buf->b_mod_set = TRUE;
2967 buf->b_mod_top = lnum;
2968 buf->b_mod_bot = lnume + xtra;
2969 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002973/*
2974 * Common code for when a change is was made.
2975 * See changed_lines() for the arguments.
2976 * Careful: may trigger autocommands that reload the buffer.
2977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002979changed_common(
2980 linenr_T lnum,
2981 colnr_T col,
2982 linenr_T lnume,
2983 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002986#ifdef FEAT_WINDOWS
2987 tabpage_T *tp;
2988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 int i;
2990#ifdef FEAT_JUMPLIST
2991 int cols;
2992 pos_T *p;
2993 int add;
2994#endif
2995
2996 /* mark the buffer as modified */
2997 changed();
2998
2999 /* set the '. mark */
3000 if (!cmdmod.keepjumps)
3001 {
3002 curbuf->b_last_change.lnum = lnum;
3003 curbuf->b_last_change.col = col;
3004
3005#ifdef FEAT_JUMPLIST
3006 /* Create a new entry if a new undo-able change was started or we
3007 * don't have an entry yet. */
3008 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3009 {
3010 if (curbuf->b_changelistlen == 0)
3011 add = TRUE;
3012 else
3013 {
3014 /* Don't create a new entry when the line number is the same
3015 * as the last one and the column is not too far away. Avoids
3016 * creating many entries for typing "xxxxx". */
3017 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3018 if (p->lnum != lnum)
3019 add = TRUE;
3020 else
3021 {
3022 cols = comp_textwidth(FALSE);
3023 if (cols == 0)
3024 cols = 79;
3025 add = (p->col + cols < col || col + cols < p->col);
3026 }
3027 }
3028 if (add)
3029 {
3030 /* This is the first of a new sequence of undo-able changes
3031 * and it's at some distance of the last change. Use a new
3032 * position in the changelist. */
3033 curbuf->b_new_change = FALSE;
3034
3035 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3036 {
3037 /* changelist is full: remove oldest entry */
3038 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3039 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3040 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003041 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 {
3043 /* Correct position in changelist for other windows on
3044 * this buffer. */
3045 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3046 --wp->w_changelistidx;
3047 }
3048 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003049 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
3051 /* For other windows, if the position in the changelist is
3052 * at the end it stays at the end. */
3053 if (wp->w_buffer == curbuf
3054 && wp->w_changelistidx == curbuf->b_changelistlen)
3055 ++wp->w_changelistidx;
3056 }
3057 ++curbuf->b_changelistlen;
3058 }
3059 }
3060 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3061 curbuf->b_last_change;
3062 /* The current window is always after the last change, so that "g,"
3063 * takes you back to it. */
3064 curwin->w_changelistidx = curbuf->b_changelistlen;
3065#endif
3066 }
3067
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003068 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
3070 if (wp->w_buffer == curbuf)
3071 {
3072 /* Mark this window to be redrawn later. */
3073 if (wp->w_redr_type < VALID)
3074 wp->w_redr_type = VALID;
3075
3076 /* Check if a change in the buffer has invalidated the cached
3077 * values for the cursor. */
3078#ifdef FEAT_FOLDING
3079 /*
3080 * Update the folds for this window. Can't postpone this, because
3081 * a following operator might work on the whole fold: ">>dd".
3082 */
3083 foldUpdate(wp, lnum, lnume + xtra - 1);
3084
3085 /* The change may cause lines above or below the change to become
3086 * included in a fold. Set lnum/lnume to the first/last line that
3087 * might be displayed differently.
3088 * Set w_cline_folded here as an efficient way to update it when
3089 * inserting lines just above a closed fold. */
3090 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3091 if (wp->w_cursor.lnum == lnum)
3092 wp->w_cline_folded = i;
3093 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3094 if (wp->w_cursor.lnum == lnume)
3095 wp->w_cline_folded = i;
3096
3097 /* If the changed line is in a range of previously folded lines,
3098 * compare with the first line in that range. */
3099 if (wp->w_cursor.lnum <= lnum)
3100 {
3101 i = find_wl_entry(wp, lnum);
3102 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3103 changed_line_abv_curs_win(wp);
3104 }
3105#endif
3106
3107 if (wp->w_cursor.lnum > lnum)
3108 changed_line_abv_curs_win(wp);
3109 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3110 changed_cline_bef_curs_win(wp);
3111 if (wp->w_botline >= lnum)
3112 {
3113 /* Assume that botline doesn't change (inserted lines make
3114 * other lines scroll down below botline). */
3115 approximate_botline_win(wp);
3116 }
3117
3118 /* Check if any w_lines[] entries have become invalid.
3119 * For entries below the change: Correct the lnums for
3120 * inserted/deleted lines. Makes it possible to stop displaying
3121 * after the change. */
3122 for (i = 0; i < wp->w_lines_valid; ++i)
3123 if (wp->w_lines[i].wl_valid)
3124 {
3125 if (wp->w_lines[i].wl_lnum >= lnum)
3126 {
3127 if (wp->w_lines[i].wl_lnum < lnume)
3128 {
3129 /* line included in change */
3130 wp->w_lines[i].wl_valid = FALSE;
3131 }
3132 else if (xtra != 0)
3133 {
3134 /* line below change */
3135 wp->w_lines[i].wl_lnum += xtra;
3136#ifdef FEAT_FOLDING
3137 wp->w_lines[i].wl_lastlnum += xtra;
3138#endif
3139 }
3140 }
3141#ifdef FEAT_FOLDING
3142 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3143 {
3144 /* change somewhere inside this range of folded lines,
3145 * may need to be redrawn */
3146 wp->w_lines[i].wl_valid = FALSE;
3147 }
3148#endif
3149 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003150
3151#ifdef FEAT_FOLDING
3152 /* Take care of side effects for setting w_topline when folds have
3153 * changed. Esp. when the buffer was changed in another window. */
3154 if (hasAnyFolding(wp))
3155 set_topline(wp, wp->w_topline);
3156#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003157 /* relative numbering may require updating more */
3158 if (wp->w_p_rnu)
3159 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 }
3162
3163 /* Call update_screen() later, which checks out what needs to be redrawn,
3164 * since it notices b_mod_set and then uses b_mod_*. */
3165 if (must_redraw < VALID)
3166 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003167
3168#ifdef FEAT_AUTOCMD
3169 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003170 if (lnum <= curwin->w_cursor.lnum
3171 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003172 last_cursormoved.lnum = 0;
3173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174}
3175
3176/*
3177 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3178 */
3179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003180unchanged(
3181 buf_T *buf,
3182 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003184 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
3186 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003187 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (ff)
3189 save_file_ff(buf);
3190#ifdef FEAT_WINDOWS
3191 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003192 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#endif
3194#ifdef FEAT_TITLE
3195 need_maketitle = TRUE; /* set window title later */
3196#endif
3197 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003198 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199#ifdef FEAT_NETBEANS_INTG
3200 netbeans_unmodified(buf);
3201#endif
3202}
3203
3204#if defined(FEAT_WINDOWS) || defined(PROTO)
3205/*
3206 * check_status: called when the status bars for the buffer 'buf'
3207 * need to be updated
3208 */
3209 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003210check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 win_T *wp;
3213
Bram Moolenaar29323592016-07-24 22:04:11 +02003214 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (wp->w_buffer == buf && wp->w_status_height)
3216 {
3217 wp->w_redr_status = TRUE;
3218 if (must_redraw < VALID)
3219 must_redraw = VALID;
3220 }
3221}
3222#endif
3223
3224/*
3225 * If the file is readonly, give a warning message with the first change.
3226 * Don't do this for autocommands.
3227 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003228 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003230 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 */
3232 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003233change_warning(
3234 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 mode and 'showmode' is on */
3236{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003237 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (curbuf->b_did_warn == FALSE
3240 && curbufIsChanged() == 0
3241#ifdef FEAT_AUTOCMD
3242 && !autocmd_busy
3243#endif
3244 && curbuf->b_p_ro)
3245 {
3246#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003247 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003249 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (!curbuf->b_p_ro)
3251 return;
3252#endif
3253 /*
3254 * Do what msg() does, but with a column offset if the warning should
3255 * be after the mode message.
3256 */
3257 msg_start();
3258 if (msg_row == Rows - 1)
3259 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003260 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003261 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3262#ifdef FEAT_EVAL
3263 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 msg_clr_eos();
3266 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003267 if (msg_silent == 0 && !silent_mode
3268#ifdef FEAT_EVAL
3269 && time_for_testing != 1
3270#endif
3271 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 out_flush();
3274 ui_delay(1000L, TRUE); /* give the user time to think about it */
3275 }
3276 curbuf->b_did_warn = TRUE;
3277 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3278 if (msg_row < Rows - 1)
3279 showmode();
3280 }
3281}
3282
3283/*
3284 * Ask for a reply from the user, a 'y' or a 'n'.
3285 * No other characters are accepted, the message is repeated until a valid
3286 * reply is entered or CTRL-C is hit.
3287 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3288 * from any buffers but directly from the user.
3289 *
3290 * return the 'y' or 'n'
3291 */
3292 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003293ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 int r = ' ';
3296 int save_State = State;
3297
3298 if (exiting) /* put terminal in raw mode for this question */
3299 settmode(TMODE_RAW);
3300 ++no_wait_return;
3301#ifdef USE_ON_FLY_SCROLL
3302 dont_scroll = TRUE; /* disallow scrolling here */
3303#endif
3304 State = CONFIRM; /* mouse behaves like with :confirm */
3305#ifdef FEAT_MOUSE
3306 setmouse(); /* disables mouse for xterm */
3307#endif
3308 ++no_mapping;
3309 ++allow_keys; /* no mapping here, but recognize keys */
3310
3311 while (r != 'y' && r != 'n')
3312 {
3313 /* same highlighting as for wait_return */
3314 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3315 if (direct)
3316 r = get_keystroke();
3317 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003318 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (r == Ctrl_C || r == ESC)
3320 r = 'n';
3321 msg_putchar(r); /* show what you typed */
3322 out_flush();
3323 }
3324 --no_wait_return;
3325 State = save_State;
3326#ifdef FEAT_MOUSE
3327 setmouse();
3328#endif
3329 --no_mapping;
3330 --allow_keys;
3331
3332 return r;
3333}
3334
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003335#if defined(FEAT_MOUSE) || defined(PROTO)
3336/*
3337 * Return TRUE if "c" is a mouse key.
3338 */
3339 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003340is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003341{
3342 return c == K_LEFTMOUSE
3343 || c == K_LEFTMOUSE_NM
3344 || c == K_LEFTDRAG
3345 || c == K_LEFTRELEASE
3346 || c == K_LEFTRELEASE_NM
3347 || c == K_MIDDLEMOUSE
3348 || c == K_MIDDLEDRAG
3349 || c == K_MIDDLERELEASE
3350 || c == K_RIGHTMOUSE
3351 || c == K_RIGHTDRAG
3352 || c == K_RIGHTRELEASE
3353 || c == K_MOUSEDOWN
3354 || c == K_MOUSEUP
3355 || c == K_MOUSELEFT
3356 || c == K_MOUSERIGHT
3357 || c == K_X1MOUSE
3358 || c == K_X1DRAG
3359 || c == K_X1RELEASE
3360 || c == K_X2MOUSE
3361 || c == K_X2DRAG
3362 || c == K_X2RELEASE;
3363}
3364#endif
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Get a key stroke directly from the user.
3368 * Ignores mouse clicks and scrollbar events, except a click for the left
3369 * button (used at the more prompt).
3370 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3371 * Disadvantage: typeahead is ignored.
3372 * Translates the interrupt character for unix to ESC.
3373 */
3374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003375get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003377 char_u *buf = NULL;
3378 int buflen = 150;
3379 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int len = 0;
3381 int n;
3382 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003383 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 mapped_ctrl_c = FALSE; /* mappings are not used here */
3386 for (;;)
3387 {
3388 cursor_on();
3389 out_flush();
3390
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003391 /* Leave some room for check_termcode() to insert a key code into (max
3392 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3393 * bytes. */
3394 maxlen = (buflen - 6 - len) / 3;
3395 if (buf == NULL)
3396 buf = alloc(buflen);
3397 else if (maxlen < 10)
3398 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003399 char_u *t_buf = buf;
3400
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003401 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003402 * escape sequence. */
3403 buflen += 100;
3404 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003405 if (buf == NULL)
3406 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003407 maxlen = (buflen - 6 - len) / 3;
3408 }
3409 if (buf == NULL)
3410 {
3411 do_outofmem_msg((long_u)buflen);
3412 return ESC; /* panic! */
3413 }
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003416 * terminal code to complete. */
3417 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (n > 0)
3419 {
3420 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003421 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003423 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003425 else if (len > 0)
3426 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar4395a712006-09-05 18:57:57 +00003428 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003429 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003430 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003432
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003433 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003434 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003435 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003436 {
3437 /* Redrawing was postponed, do it now. */
3438 update_screen(0);
3439 setcursor(); /* put cursor back where it belongs */
3440 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003442 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003443 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003445 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 continue;
3447
3448 /* Handle modifier and/or special key code. */
3449 n = buf[0];
3450 if (n == K_SPECIAL)
3451 {
3452 n = TO_SPECIAL(buf[1], buf[2]);
3453 if (buf[1] == KS_MODIFIER
3454 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003455#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003456 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003457#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003458#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 || n == K_VER_SCROLLBAR
3460 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461#endif
3462 )
3463 {
3464 if (buf[1] == KS_MODIFIER)
3465 mod_mask = buf[2];
3466 len -= 3;
3467 if (len > 0)
3468 mch_memmove(buf, buf + 3, (size_t)len);
3469 continue;
3470 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473#ifdef FEAT_MBYTE
3474 if (has_mbyte)
3475 {
3476 if (MB_BYTE2LEN(n) > len)
3477 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003478 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 n = (*mb_ptr2char)(buf);
3480 }
3481#endif
3482#ifdef UNIX
3483 if (n == intr_char)
3484 n = ESC;
3485#endif
3486 break;
3487 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003488 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 mapped_ctrl_c = save_mapped_ctrl_c;
3491 return n;
3492}
3493
3494/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003495 * Get a number from the user.
3496 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003499get_number(
3500 int colon, /* allow colon to abort */
3501 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 int n = 0;
3504 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003505 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003507 if (mouse_used != NULL)
3508 *mouse_used = FALSE;
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /* When not printing messages, the user won't know what to type, return a
3511 * zero (as if CR was hit). */
3512 if (msg_silent != 0)
3513 return 0;
3514
3515#ifdef USE_ON_FLY_SCROLL
3516 dont_scroll = TRUE; /* disallow scrolling here */
3517#endif
3518 ++no_mapping;
3519 ++allow_keys; /* no mapping here, but recognize keys */
3520 for (;;)
3521 {
3522 windgoto(msg_row, msg_col);
3523 c = safe_vgetc();
3524 if (VIM_ISDIGIT(c))
3525 {
3526 n = n * 10 + c - '0';
3527 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003528 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3531 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003532 if (typed > 0)
3533 {
3534 MSG_PUTS("\b \b");
3535 --typed;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003539#ifdef FEAT_MOUSE
3540 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3541 {
3542 *mouse_used = TRUE;
3543 n = mouse_row + 1;
3544 break;
3545 }
3546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 else if (n == 0 && c == ':' && colon)
3548 {
3549 stuffcharReadbuff(':');
3550 if (!exmode_active)
3551 cmdline_row = msg_row;
3552 skip_redraw = TRUE; /* skip redraw once */
3553 do_redraw = FALSE;
3554 break;
3555 }
3556 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3557 break;
3558 }
3559 --no_mapping;
3560 --allow_keys;
3561 return n;
3562}
3563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003564/*
3565 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003566 * When "mouse_used" is not NULL allow using the mouse and in that case return
3567 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 */
3569 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003570prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003571{
3572 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003573 int save_cmdline_row;
3574 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575
3576 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003577 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003578 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003579 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003580 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003581
Bram Moolenaar203335e2006-09-03 14:35:42 +00003582 /* Set the state such that text can be selected/copied/pasted and we still
3583 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003585 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003586 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003587 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003588
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003589 i = get_number(TRUE, mouse_used);
3590 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003591 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003592 /* don't call wait_return() now */
3593 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003594 cmdline_row = msg_row - 1;
3595 need_wait_return = FALSE;
3596 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003597 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003599 else
3600 cmdline_row = save_cmdline_row;
3601 State = save_State;
3602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 return i;
3604}
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003607msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 long pn;
3610
3611 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3613 return;
3614
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003615 /* We don't want to overwrite another important message, but do overwrite
3616 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3617 * then "put" reports the last action. */
3618 if (keep_msg != NULL && !keep_msg_more)
3619 return;
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (n > 0)
3622 pn = n;
3623 else
3624 pn = -n;
3625
3626 if (pn > p_report)
3627 {
3628 if (pn == 1)
3629 {
3630 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003631 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3632 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003634 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3635 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else
3638 {
3639 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003640 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3641 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003643 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3644 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003647 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (msg(msg_buf))
3649 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003650 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003651 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653 }
3654}
3655
3656/*
3657 * flush map and typeahead buffers and give a warning for an error
3658 */
3659 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003660beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
3662 if (emsg_silent == 0)
3663 {
3664 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003665 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667}
3668
3669/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003670 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 */
3672 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003673vim_beep(
3674 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 if (emsg_silent == 0)
3677 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003678 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3679 {
3680 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003682 /* While the GUI is starting up the termcap is set for the
3683 * GUI but the output still goes to a terminal. */
3684 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003686 )
Bram Moolenaar165bc692015-07-21 17:53:25 +02003687 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003689 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003691
3692 /* When 'verbose' is set and we are sourcing a script or executing a
3693 * function give the user a hint where the beep comes from. */
3694 if (vim_strchr(p_debug, 'e') != NULL)
3695 {
3696 msg_source(hl_attr(HLF_W));
3697 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700}
3701
3702/*
3703 * To get the "real" home directory:
3704 * - get value of $HOME
3705 * For Unix:
3706 * - go to that directory
3707 * - do mch_dirname() to get the real name of that directory.
3708 * This also works with mounts and links.
3709 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3710 */
3711static char_u *homedir = NULL;
3712
3713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003714init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 char_u *var;
3717
Bram Moolenaar05159a02005-02-26 23:04:13 +00003718 /* In case we are called a second time (when 'encoding' changes). */
3719 vim_free(homedir);
3720 homedir = NULL;
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#ifdef VMS
3723 var = mch_getenv((char_u *)"SYS$LOGIN");
3724#else
3725 var = mch_getenv((char_u *)"HOME");
3726#endif
3727
3728 if (var != NULL && *var == NUL) /* empty is same as not set */
3729 var = NULL;
3730
3731#ifdef WIN3264
3732 /*
3733 * Weird but true: $HOME may contain an indirect reference to another
3734 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3735 * when $HOME is being set.
3736 */
3737 if (var != NULL && *var == '%')
3738 {
3739 char_u *p;
3740 char_u *exp;
3741
3742 p = vim_strchr(var + 1, '%');
3743 if (p != NULL)
3744 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003745 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 exp = mch_getenv(NameBuff);
3747 if (exp != NULL && *exp != NUL
3748 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3749 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003750 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 var = NameBuff;
3752 /* Also set $HOME, it's needed for _viminfo. */
3753 vim_setenv((char_u *)"HOME", NameBuff);
3754 }
3755 }
3756 }
3757
3758 /*
3759 * Typically, $HOME is not defined on Windows, unless the user has
3760 * specifically defined it for Vim's sake. However, on Windows NT
3761 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3762 * each user. Try constructing $HOME from these.
3763 */
3764 if (var == NULL)
3765 {
3766 char_u *homedrive, *homepath;
3767
3768 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3769 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003770 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003771 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003772 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3774 {
3775 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3776 if (NameBuff[0] != NUL)
3777 {
3778 var = NameBuff;
3779 /* Also set $HOME, it's needed for _viminfo. */
3780 vim_setenv((char_u *)"HOME", NameBuff);
3781 }
3782 }
3783 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003784
3785# if defined(FEAT_MBYTE)
3786 if (enc_utf8 && var != NULL)
3787 {
3788 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003789 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003790
3791 /* Convert from active codepage to UTF-8. Other conversions are
3792 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003793 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003794 if (pp != NULL)
3795 {
3796 homedir = pp;
3797 return;
3798 }
3799 }
3800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801#endif
3802
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003803#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 /*
3805 * Default home dir is C:/
3806 * Best assumption we can make in such a situation.
3807 */
3808 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003809 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810#endif
3811 if (var != NULL)
3812 {
3813#ifdef UNIX
3814 /*
3815 * Change to the directory and get the actual path. This resolves
3816 * links. Don't do it when we can't return.
3817 */
3818 if (mch_dirname(NameBuff, MAXPATHL) == OK
3819 && mch_chdir((char *)NameBuff) == 0)
3820 {
3821 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3822 var = IObuff;
3823 if (mch_chdir((char *)NameBuff) != 0)
3824 EMSG(_(e_prev_dir));
3825 }
3826#endif
3827 homedir = vim_strsave(var);
3828 }
3829}
3830
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003831#if defined(EXITFREE) || defined(PROTO)
3832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003833free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003834{
3835 vim_free(homedir);
3836}
Bram Moolenaar24305862012-08-15 14:05:05 +02003837
3838# ifdef FEAT_CMDL_COMPL
3839 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003840free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003841{
3842 ga_clear_strings(&ga_users);
3843}
3844# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003845#endif
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003848 * Call expand_env() and store the result in an allocated string.
3849 * This is not very memory efficient, this expects the result to be freed
3850 * again soon.
3851 */
3852 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003853expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003854{
3855 return expand_env_save_opt(src, FALSE);
3856}
3857
3858/*
3859 * Idem, but when "one" is TRUE handle the string as one file name, only
3860 * expand "~" at the start.
3861 */
3862 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003863expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003864{
3865 char_u *p;
3866
3867 p = alloc(MAXPATHL);
3868 if (p != NULL)
3869 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3870 return p;
3871}
3872
3873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * Expand environment variable with path name.
3875 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003876 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 * If anything fails no expansion is done and dst equals src.
3878 */
3879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003880expand_env(
3881 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3882 char_u *dst, /* where to put the result */
3883 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003885 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886}
3887
3888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003889expand_env_esc(
3890 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3891 char_u *dst, /* where to put the result */
3892 int dstlen, /* maximum length of the result */
3893 int esc, /* escape spaces in expanded variables */
3894 int one, /* "srcp" is one file name */
3895 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003897 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 char_u *tail;
3899 int c;
3900 char_u *var;
3901 int copy_char;
3902 int mustfree; /* var was allocated, need to free it later */
3903 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003904 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003906 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003907 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003908
3909 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 --dstlen; /* leave one char space for "\," */
3911 while (*src && dstlen > 0)
3912 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003913#ifdef FEAT_EVAL
3914 /* Skip over `=expr`. */
3915 if (src[0] == '`' && src[1] == '=')
3916 {
3917 size_t len;
3918
3919 var = src;
3920 src += 2;
3921 (void)skip_expr(&src);
3922 if (*src == '`')
3923 ++src;
3924 len = src - var;
3925 if (len > (size_t)dstlen)
3926 len = dstlen;
3927 vim_strncpy(dst, var, len);
3928 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003929 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003930 continue;
3931 }
3932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003934 if ((*src == '$'
3935#ifdef VMS
3936 && at_start
3937#endif
3938 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003939#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 || *src == '%'
3941#endif
3942 || (*src == '~' && at_start))
3943 {
3944 mustfree = FALSE;
3945
3946 /*
3947 * The variable name is copied into dst temporarily, because it may
3948 * be a string in read-only memory and a NUL needs to be appended.
3949 */
3950 if (*src != '~') /* environment var */
3951 {
3952 tail = src + 1;
3953 var = dst;
3954 c = dstlen - 1;
3955
3956#ifdef UNIX
3957 /* Unix has ${var-name} type environment vars */
3958 if (*tail == '{' && !vim_isIDc('{'))
3959 {
3960 tail++; /* ignore '{' */
3961 while (c-- > 0 && *tail && *tail != '}')
3962 *var++ = *tail++;
3963 }
3964 else
3965#endif
3966 {
3967 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003968#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 || (*src == '%' && *tail != '%')
3970#endif
3971 ))
3972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003977#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978# ifdef UNIX
3979 if (src[1] == '{' && *tail != '}')
3980# else
3981 if (*src == '%' && *tail != '%')
3982# endif
3983 var = NULL;
3984 else
3985 {
3986# ifdef UNIX
3987 if (src[1] == '{')
3988# else
3989 if (*src == '%')
3990#endif
3991 ++tail;
3992#endif
3993 *var = NUL;
3994 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003995#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997#endif
3998 }
3999 /* home directory */
4000 else if ( src[1] == NUL
4001 || vim_ispathsep(src[1])
4002 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4003 {
4004 var = homedir;
4005 tail = src + 1;
4006 }
4007 else /* user directory */
4008 {
4009#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4010 /*
4011 * Copy ~user to dst[], so we can put a NUL after it.
4012 */
4013 tail = src;
4014 var = dst;
4015 c = dstlen - 1;
4016 while ( c-- > 0
4017 && *tail
4018 && vim_isfilec(*tail)
4019 && !vim_ispathsep(*tail))
4020 *var++ = *tail++;
4021 *var = NUL;
4022# ifdef UNIX
4023 /*
4024 * If the system supports getpwnam(), use it.
4025 * Otherwise, or if getpwnam() fails, the shell is used to
4026 * expand ~user. This is slower and may fail if the shell
4027 * does not support ~user (old versions of /bin/sh).
4028 */
4029# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4030 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004031 /* Note: memory allocated by getpwnam() is never freed.
4032 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004033 struct passwd *pw = (*dst == NUL)
4034 ? NULL : getpwnam((char *)dst + 1);
4035
4036 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 if (var == NULL)
4039# endif
4040 {
4041 expand_T xpc;
4042
4043 ExpandInit(&xpc);
4044 xpc.xp_context = EXPAND_FILES;
4045 var = ExpandOne(&xpc, dst, NULL,
4046 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 mustfree = TRUE;
4048 }
4049
4050# else /* !UNIX, thus VMS */
4051 /*
4052 * USER_HOME is a comma-separated list of
4053 * directories to search for the user account in.
4054 */
4055 {
4056 char_u test[MAXPATHL], paths[MAXPATHL];
4057 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004058 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 STRCPY(paths, USER_HOME);
4061 next_path = paths;
4062 while (*next_path)
4063 {
4064 for (path = next_path; *next_path && *next_path != ',';
4065 next_path++);
4066 if (*next_path)
4067 *next_path++ = NUL;
4068 STRCPY(test, path);
4069 STRCAT(test, "/");
4070 STRCAT(test, dst + 1);
4071 if (mch_stat(test, &st) == 0)
4072 {
4073 var = alloc(STRLEN(test) + 1);
4074 STRCPY(var, test);
4075 mustfree = TRUE;
4076 break;
4077 }
4078 }
4079 }
4080# endif /* UNIX */
4081#else
4082 /* cannot expand user's home directory, so don't try */
4083 var = NULL;
4084 tail = (char_u *)""; /* for gcc */
4085#endif /* UNIX || VMS */
4086 }
4087
4088#ifdef BACKSLASH_IN_FILENAME
4089 /* If 'shellslash' is set change backslashes to forward slashes.
4090 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4091 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4092 {
4093 char_u *p = vim_strsave(var);
4094
4095 if (p != NULL)
4096 {
4097 if (mustfree)
4098 vim_free(var);
4099 var = p;
4100 mustfree = TRUE;
4101 forward_slash(var);
4102 }
4103 }
4104#endif
4105
4106 /* If "var" contains white space, escape it with a backslash.
4107 * Required for ":e ~/tt" when $HOME includes a space. */
4108 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4109 {
4110 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4111
4112 if (p != NULL)
4113 {
4114 if (mustfree)
4115 vim_free(var);
4116 var = p;
4117 mustfree = TRUE;
4118 }
4119 }
4120
4121 if (var != NULL && *var != NUL
4122 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4123 {
4124 STRCPY(dst, var);
4125 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004126 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 /* if var[] ends in a path separator and tail[] starts
4128 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004129 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4131 && dst[-1] != ':'
4132#endif
4133 && vim_ispathsep(*tail))
4134 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004135 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 src = tail;
4137 copy_char = FALSE;
4138 }
4139 if (mustfree)
4140 vim_free(var);
4141 }
4142
4143 if (copy_char) /* copy at least one char */
4144 {
4145 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004146 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004147 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4148 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 */
4150 at_start = FALSE;
4151 if (src[0] == '\\' && src[1] != NUL)
4152 {
4153 *dst++ = *src++;
4154 --dstlen;
4155 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004156 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 at_start = TRUE;
4158 *dst++ = *src++;
4159 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004160
4161 if (startstr != NULL && src - startstr_len >= srcp
4162 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4163 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165 }
4166 *dst = NUL;
4167}
4168
4169/*
4170 * Vim's version of getenv().
4171 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004172 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004173 * "mustfree" is set to TRUE when returned is allocated, it must be
4174 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 */
4176 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004177vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 char_u *p;
4180 char_u *pend;
4181 int vimruntime;
4182
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004183#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 /* use "C:/" when $HOME is not set */
4185 if (STRCMP(name, "HOME") == 0)
4186 return homedir;
4187#endif
4188
4189 p = mch_getenv(name);
4190 if (p != NULL && *p == NUL) /* empty is the same as not set */
4191 p = NULL;
4192
4193 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004194 {
4195#if defined(FEAT_MBYTE) && defined(WIN3264)
4196 if (enc_utf8)
4197 {
4198 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004199 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004200
4201 /* Convert from active codepage to UTF-8. Other conversions are
4202 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004203 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004204 if (pp != NULL)
4205 {
4206 p = pp;
4207 *mustfree = TRUE;
4208 }
4209 }
4210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4215 if (!vimruntime && STRCMP(name, "VIM") != 0)
4216 return NULL;
4217
4218 /*
4219 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4220 * Don't do this when default_vimruntime_dir is non-empty.
4221 */
4222 if (vimruntime
4223#ifdef HAVE_PATHDEF
4224 && *default_vimruntime_dir == NUL
4225#endif
4226 )
4227 {
4228 p = mch_getenv((char_u *)"VIM");
4229 if (p != NULL && *p == NUL) /* empty is the same as not set */
4230 p = NULL;
4231 if (p != NULL)
4232 {
4233 p = vim_version_dir(p);
4234 if (p != NULL)
4235 *mustfree = TRUE;
4236 else
4237 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004238
4239#if defined(FEAT_MBYTE) && defined(WIN3264)
4240 if (enc_utf8)
4241 {
4242 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004243 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004244
4245 /* Convert from active codepage to UTF-8. Other conversions
4246 * are not done, because they would fail for non-ASCII
4247 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004248 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004249 if (pp != NULL)
4250 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004251 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252 vim_free(p);
4253 p = pp;
4254 *mustfree = TRUE;
4255 }
4256 }
4257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 }
4260
4261 /*
4262 * When expanding $VIM or $VIMRUNTIME fails, try using:
4263 * - the directory name from 'helpfile' (unless it contains '$')
4264 * - the executable name from argv[0]
4265 */
4266 if (p == NULL)
4267 {
4268 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4269 p = p_hf;
4270#ifdef USE_EXE_NAME
4271 /*
4272 * Use the name of the executable, obtained from argv[0].
4273 */
4274 else
4275 p = exe_name;
4276#endif
4277 if (p != NULL)
4278 {
4279 /* remove the file name */
4280 pend = gettail(p);
4281
4282 /* remove "doc/" from 'helpfile', if present */
4283 if (p == p_hf)
4284 pend = remove_tail(p, pend, (char_u *)"doc");
4285
4286#ifdef USE_EXE_NAME
4287# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004288 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (p == exe_name)
4290 {
4291 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004292 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004294 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4295 if (pend1 != pend)
4296 {
4297 pnew = alloc((unsigned)(pend1 - p) + 15);
4298 if (pnew != NULL)
4299 {
4300 STRNCPY(pnew, p, (pend1 - p));
4301 STRCPY(pnew + (pend1 - p), "Resources/vim");
4302 p = pnew;
4303 pend = p + STRLEN(p);
4304 }
4305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307# endif
4308 /* remove "src/" from exe_name, if present */
4309 if (p == exe_name)
4310 pend = remove_tail(p, pend, (char_u *)"src");
4311#endif
4312
4313 /* for $VIM, remove "runtime/" or "vim54/", if present */
4314 if (!vimruntime)
4315 {
4316 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4317 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4318 }
4319
4320 /* remove trailing path separator */
4321#ifndef MACOS_CLASSIC
4322 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004323 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004324 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 --pend;
4326#endif
4327
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004328#ifdef MACOS_X
4329 if (p == exe_name || p == p_hf)
4330#endif
4331 /* check that the result is a directory name */
4332 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 if (p != NULL && !mch_isdir(p))
4335 {
4336 vim_free(p);
4337 p = NULL;
4338 }
4339 else
4340 {
4341#ifdef USE_EXE_NAME
4342 /* may add "/vim54" or "/runtime" if it exists */
4343 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4344 {
4345 vim_free(p);
4346 p = pend;
4347 }
4348#endif
4349 *mustfree = TRUE;
4350 }
4351 }
4352 }
4353
4354#ifdef HAVE_PATHDEF
4355 /* When there is a pathdef.c file we can use default_vim_dir and
4356 * default_vimruntime_dir */
4357 if (p == NULL)
4358 {
4359 /* Only use default_vimruntime_dir when it is not empty */
4360 if (vimruntime && *default_vimruntime_dir != NUL)
4361 {
4362 p = default_vimruntime_dir;
4363 *mustfree = FALSE;
4364 }
4365 else if (*default_vim_dir != NUL)
4366 {
4367 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4368 *mustfree = TRUE;
4369 else
4370 {
4371 p = default_vim_dir;
4372 *mustfree = FALSE;
4373 }
4374 }
4375 }
4376#endif
4377
4378 /*
4379 * Set the environment variable, so that the new value can be found fast
4380 * next time, and others can also use it (e.g. Perl).
4381 */
4382 if (p != NULL)
4383 {
4384 if (vimruntime)
4385 {
4386 vim_setenv((char_u *)"VIMRUNTIME", p);
4387 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 else
4390 {
4391 vim_setenv((char_u *)"VIM", p);
4392 didset_vim = TRUE;
4393 }
4394 }
4395 return p;
4396}
4397
4398/*
4399 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4400 * Return NULL if not, return its name in allocated memory otherwise.
4401 */
4402 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004403vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 char_u *p;
4406
4407 if (vimdir == NULL || *vimdir == NUL)
4408 return NULL;
4409 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4410 if (p != NULL && mch_isdir(p))
4411 return p;
4412 vim_free(p);
4413 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4414 if (p != NULL && mch_isdir(p))
4415 return p;
4416 vim_free(p);
4417 return NULL;
4418}
4419
4420/*
4421 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4422 * the length of "name/". Otherwise return "pend".
4423 */
4424 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004425remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
4427 int len = (int)STRLEN(name) + 1;
4428 char_u *newend = pend - len;
4429
4430 if (newend >= p
4431 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004432 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 return newend;
4434 return pend;
4435}
4436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 * Our portable version of setenv.
4439 */
4440 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004441vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
4443#ifdef HAVE_SETENV
4444 mch_setenv((char *)name, (char *)val, 1);
4445#else
4446 char_u *envbuf;
4447
4448 /*
4449 * Putenv does not copy the string, it has to remain
4450 * valid. The allocated memory will never be freed.
4451 */
4452 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4453 if (envbuf != NULL)
4454 {
4455 sprintf((char *)envbuf, "%s=%s", name, val);
4456 putenv((char *)envbuf);
4457 }
4458#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004459#ifdef FEAT_GETTEXT
4460 /*
4461 * When setting $VIMRUNTIME adjust the directory to find message
4462 * translations to $VIMRUNTIME/lang.
4463 */
4464 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4465 {
4466 char_u *buf = concat_str(val, (char_u *)"/lang");
4467
4468 if (buf != NULL)
4469 {
4470 bindtextdomain(VIMPACKAGE, (char *)buf);
4471 vim_free(buf);
4472 }
4473 }
4474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475}
4476
4477#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4478/*
4479 * Function given to ExpandGeneric() to obtain an environment variable name.
4480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004482get_env_name(
4483 expand_T *xp UNUSED,
4484 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485{
4486# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4487 /*
4488 * No environ[] on the Amiga and on the Mac (using MPW).
4489 */
4490 return NULL;
4491# else
4492# ifndef __WIN32__
4493 /* Borland C++ 5.2 has this in a header file. */
4494 extern char **environ;
4495# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004496# define ENVNAMELEN 100
4497 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 char_u *str;
4499 int n;
4500
4501 str = (char_u *)environ[idx];
4502 if (str == NULL)
4503 return NULL;
4504
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004505 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 if (str[n] == '=' || str[n] == NUL)
4508 break;
4509 name[n] = str[n];
4510 }
4511 name[n] = NUL;
4512 return name;
4513# endif
4514}
Bram Moolenaar24305862012-08-15 14:05:05 +02004515
4516/*
4517 * Find all user names for user completion.
4518 * Done only once and then cached.
4519 */
4520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004521init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004522{
Bram Moolenaar24305862012-08-15 14:05:05 +02004523 static int lazy_init_done = FALSE;
4524
4525 if (lazy_init_done)
4526 return;
4527
4528 lazy_init_done = TRUE;
4529 ga_init2(&ga_users, sizeof(char_u *), 20);
4530
4531# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4532 {
4533 char_u* user;
4534 struct passwd* pw;
4535
4536 setpwent();
4537 while ((pw = getpwent()) != NULL)
4538 /* pw->pw_name shouldn't be NULL but just in case... */
4539 if (pw->pw_name != NULL)
4540 {
4541 if (ga_grow(&ga_users, 1) == FAIL)
4542 break;
4543 user = vim_strsave((char_u*)pw->pw_name);
4544 if (user == NULL)
4545 break;
4546 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4547 }
4548 endpwent();
4549 }
4550# endif
4551}
4552
4553/*
4554 * Function given to ExpandGeneric() to obtain an user names.
4555 */
4556 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004557get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004558{
4559 init_users();
4560 if (idx < ga_users.ga_len)
4561 return ((char_u **)ga_users.ga_data)[idx];
4562 return NULL;
4563}
4564
4565/*
4566 * Check whether name matches a user name. Return:
4567 * 0 if name does not match any user name.
4568 * 1 if name partially matches the beginning of a user name.
4569 * 2 is name fully matches a user name.
4570 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004571int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004572{
4573 int i;
4574 int n = (int)STRLEN(name);
4575 int result = 0;
4576
4577 init_users();
4578 for (i = 0; i < ga_users.ga_len; i++)
4579 {
4580 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4581 return 2; /* full match */
4582 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4583 result = 1; /* partial match */
4584 }
4585 return result;
4586}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#endif
4588
4589/*
4590 * Replace home directory by "~" in each space or comma separated file name in
4591 * 'src'.
4592 * If anything fails (except when out of space) dst equals src.
4593 */
4594 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004595home_replace(
4596 buf_T *buf, /* when not NULL, check for help files */
4597 char_u *src, /* input file name */
4598 char_u *dst, /* where to put the result */
4599 int dstlen, /* maximum length of the result */
4600 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 spaces and commas in the file name. */
4602{
4603 size_t dirlen = 0, envlen = 0;
4604 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004605 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 char_u *p;
4607
4608 if (src == NULL)
4609 {
4610 *dst = NUL;
4611 return;
4612 }
4613
4614 /*
4615 * If the file is a help file, remove the path completely.
4616 */
4617 if (buf != NULL && buf->b_help)
4618 {
4619 STRCPY(dst, gettail(src));
4620 return;
4621 }
4622
4623 /*
4624 * We check both the value of the $HOME environment variable and the
4625 * "real" home directory.
4626 */
4627 if (homedir != NULL)
4628 dirlen = STRLEN(homedir);
4629
4630#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004631 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004633 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4634#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004635 /* Empty is the same as not set. */
4636 if (homedir_env != NULL && *homedir_env == NUL)
4637 homedir_env = NULL;
4638
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004639#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004640 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004641 {
4642 int usedlen = 0;
4643 int flen;
4644 char_u *fbuf = NULL;
4645
4646 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004647 (void)modify_fname((char_u *)":p", &usedlen,
4648 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004649 flen = (int)STRLEN(homedir_env);
4650 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4651 /* Remove the trailing / that is added to a directory. */
4652 homedir_env[flen - 1] = NUL;
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
4655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (homedir_env != NULL)
4657 envlen = STRLEN(homedir_env);
4658
4659 if (!one)
4660 src = skipwhite(src);
4661 while (*src && dstlen > 0)
4662 {
4663 /*
4664 * Here we are at the beginning of a file name.
4665 * First, check to see if the beginning of the file name matches
4666 * $HOME or the "real" home directory. Check that there is a '/'
4667 * after the match (so that if e.g. the file is "/home/pieter/bla",
4668 * and the home directory is "/home/piet", the file does not end up
4669 * as "~er/bla" (which would seem to indicate the file "bla" in user
4670 * er's home directory)).
4671 */
4672 p = homedir;
4673 len = dirlen;
4674 for (;;)
4675 {
4676 if ( len
4677 && fnamencmp(src, p, len) == 0
4678 && (vim_ispathsep(src[len])
4679 || (!one && (src[len] == ',' || src[len] == ' '))
4680 || src[len] == NUL))
4681 {
4682 src += len;
4683 if (--dstlen > 0)
4684 *dst++ = '~';
4685
4686 /*
4687 * If it's just the home directory, add "/".
4688 */
4689 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4690 *dst++ = '/';
4691 break;
4692 }
4693 if (p == homedir_env)
4694 break;
4695 p = homedir_env;
4696 len = envlen;
4697 }
4698
4699 /* if (!one) skip to separator: space or comma */
4700 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4701 *dst++ = *src++;
4702 /* skip separator */
4703 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4704 *dst++ = *src++;
4705 }
4706 /* if (dstlen == 0) out of space, what to do??? */
4707
4708 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004709
4710 if (homedir_env != homedir_env_orig)
4711 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712}
4713
4714/*
4715 * Like home_replace, store the replaced string in allocated memory.
4716 * When something fails, NULL is returned.
4717 */
4718 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004719home_replace_save(
4720 buf_T *buf, /* when not NULL, check for help files */
4721 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 char_u *dst;
4724 unsigned len;
4725
4726 len = 3; /* space for "~/" and trailing NUL */
4727 if (src != NULL) /* just in case */
4728 len += (unsigned)STRLEN(src);
4729 dst = alloc(len);
4730 if (dst != NULL)
4731 home_replace(buf, src, dst, len, TRUE);
4732 return dst;
4733}
4734
4735/*
4736 * Compare two file names and return:
4737 * FPC_SAME if they both exist and are the same file.
4738 * FPC_SAMEX if they both don't exist and have the same file name.
4739 * FPC_DIFF if they both exist and are different files.
4740 * FPC_NOTX if they both don't exist.
4741 * FPC_DIFFX if one of them doesn't exist.
4742 * For the first name environment variables are expanded
4743 */
4744 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004745fullpathcmp(
4746 char_u *s1,
4747 char_u *s2,
4748 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750#ifdef UNIX
4751 char_u exp1[MAXPATHL];
4752 char_u full1[MAXPATHL];
4753 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004754 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 int r1, r2;
4756
4757 expand_env(s1, exp1, MAXPATHL);
4758 r1 = mch_stat((char *)exp1, &st1);
4759 r2 = mch_stat((char *)s2, &st2);
4760 if (r1 != 0 && r2 != 0)
4761 {
4762 /* if mch_stat() doesn't work, may compare the names */
4763 if (checkname)
4764 {
4765 if (fnamecmp(exp1, s2) == 0)
4766 return FPC_SAMEX;
4767 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4768 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4769 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4770 return FPC_SAMEX;
4771 }
4772 return FPC_NOTX;
4773 }
4774 if (r1 != 0 || r2 != 0)
4775 return FPC_DIFFX;
4776 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4777 return FPC_SAME;
4778 return FPC_DIFF;
4779#else
4780 char_u *exp1; /* expanded s1 */
4781 char_u *full1; /* full path of s1 */
4782 char_u *full2; /* full path of s2 */
4783 int retval = FPC_DIFF;
4784 int r1, r2;
4785
4786 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4787 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4788 {
4789 full1 = exp1 + MAXPATHL;
4790 full2 = full1 + MAXPATHL;
4791
4792 expand_env(s1, exp1, MAXPATHL);
4793 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4794 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4795
4796 /* If vim_FullName() fails, the file probably doesn't exist. */
4797 if (r1 != OK && r2 != OK)
4798 {
4799 if (checkname && fnamecmp(exp1, s2) == 0)
4800 retval = FPC_SAMEX;
4801 else
4802 retval = FPC_NOTX;
4803 }
4804 else if (r1 != OK || r2 != OK)
4805 retval = FPC_DIFFX;
4806 else if (fnamecmp(full1, full2))
4807 retval = FPC_DIFF;
4808 else
4809 retval = FPC_SAME;
4810 vim_free(exp1);
4811 }
4812 return retval;
4813#endif
4814}
4815
4816/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004817 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004818 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004819 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 */
4821 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004822gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
4824 char_u *p1, *p2;
4825
4826 if (fname == NULL)
4827 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004828 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004830 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004832 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 return p1;
4835}
4836
Bram Moolenaar31710262010-08-13 13:36:15 +02004837#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004838static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004839
4840/*
4841 * Return the end of the directory name, on the first path
4842 * separator:
4843 * "/path/file", "/path/dir/", "/path//dir", "/file"
4844 * ^ ^ ^ ^
4845 */
4846 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004847gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004848{
4849 char_u *dir_end = fname;
4850 char_u *next_dir_end = fname;
4851 int look_for_sep = TRUE;
4852 char_u *p;
4853
4854 for (p = fname; *p != NUL; )
4855 {
4856 if (vim_ispathsep(*p))
4857 {
4858 if (look_for_sep)
4859 {
4860 next_dir_end = p;
4861 look_for_sep = FALSE;
4862 }
4863 }
4864 else
4865 {
4866 if (!look_for_sep)
4867 dir_end = next_dir_end;
4868 look_for_sep = TRUE;
4869 }
4870 mb_ptr_adv(p);
4871 }
4872 return dir_end;
4873}
4874#endif
4875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004877 * Get pointer to tail of "fname", including path separators. Putting a NUL
4878 * here leaves the directory name. Takes care of "c:/" and "//".
4879 * Always returns a valid pointer.
4880 */
4881 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004882gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004883{
4884 char_u *p;
4885 char_u *t;
4886
4887 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4888 t = gettail(fname);
4889 while (t > p && after_pathsep(fname, t))
4890 --t;
4891#ifdef VMS
4892 /* path separator is part of the path */
4893 ++t;
4894#endif
4895 return t;
4896}
4897
4898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 * get the next path component (just after the next path separator).
4900 */
4901 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004902getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
4904 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004905 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (*fname)
4907 ++fname;
4908 return fname;
4909}
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911/*
4912 * Get a pointer to one character past the head of a path name.
4913 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4914 * If there is no head, path is returned.
4915 */
4916 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004917get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
4919 char_u *retval;
4920
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004921#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 /* may skip "c:" */
4923 if (isalpha(path[0]) && path[1] == ':')
4924 retval = path + 2;
4925 else
4926 retval = path;
4927#else
4928# if defined(AMIGA)
4929 /* may skip "label:" */
4930 retval = vim_strchr(path, ':');
4931 if (retval == NULL)
4932 retval = path;
4933# else /* Unix */
4934 retval = path;
4935# endif
4936#endif
4937
4938 while (vim_ispathsep(*retval))
4939 ++retval;
4940
4941 return retval;
4942}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004945 * Return TRUE if 'c' is a path separator.
4946 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 */
4948 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004949vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004951#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004953#else
4954# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004956# else
4957# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4959 return (c == ':' || c == '[' || c == ']' || c == '/'
4960 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004961# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004963# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966}
4967
Bram Moolenaar69c35002013-11-04 02:54:12 +01004968/*
4969 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4970 */
4971 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004972vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004973{
4974 return vim_ispathsep(c)
4975#ifdef BACKSLASH_IN_FILENAME
4976 && c != ':'
4977#endif
4978 ;
4979}
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4982/*
4983 * return TRUE if 'c' is a path list separator.
4984 */
4985 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004986vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987{
4988#ifdef UNIX
4989 return (c == ':');
4990#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004991 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#endif
4993}
4994#endif
4995
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004996#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4997 || defined(FEAT_EVAL) || defined(PROTO)
4998/*
4999 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5000 * It's done in-place.
5001 */
5002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005003shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005004{
5005 char_u *tail, *s, *d;
5006 int skip = FALSE;
5007
5008 tail = gettail(str);
5009 d = str;
5010 for (s = str; ; ++s)
5011 {
5012 if (s >= tail) /* copy the whole tail */
5013 {
5014 *d++ = *s;
5015 if (*s == NUL)
5016 break;
5017 }
5018 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5019 {
5020 *d++ = *s;
5021 skip = FALSE;
5022 }
5023 else if (!skip)
5024 {
5025 *d++ = *s; /* copy next char */
5026 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5027 skip = TRUE;
5028# ifdef FEAT_MBYTE
5029 if (has_mbyte)
5030 {
5031 int l = mb_ptr2len(s);
5032
5033 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005034 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005035 }
5036# endif
5037 }
5038 }
5039}
5040#endif
5041
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005042/*
5043 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5044 * Also returns TRUE if there is no directory name.
5045 * "fname" must be writable!.
5046 */
5047 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005048dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005049{
5050 char_u *p;
5051 int c;
5052 int retval;
5053
5054 p = gettail_sep(fname);
5055 if (p == fname)
5056 return TRUE;
5057 c = *p;
5058 *p = NUL;
5059 retval = mch_isdir(fname);
5060 *p = c;
5061 return retval;
5062}
5063
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005065 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5066 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
5068 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005069vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005071#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005073#else
5074 if (p_fic)
5075 return MB_STRICMP(x, y);
5076 return STRCMP(x, y);
5077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078}
5079
5080 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005083#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005084 char_u *px = x;
5085 char_u *py = y;
5086 int cx = NUL;
5087 int cy = NUL;
5088
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005089 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005091 cx = PTR2CHAR(px);
5092 cy = PTR2CHAR(py);
5093 if (cx == NUL || cy == NUL
5094 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5095 && !(cx == '/' && cy == '\\')
5096 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005098 len -= MB_PTR2LEN(px);
5099 px += MB_PTR2LEN(px);
5100 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102 if (len == 0)
5103 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005104 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005105#else
5106 if (p_fic)
5107 return MB_STRNICMP(x, y, len);
5108 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112/*
5113 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005114 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 */
5116 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005117concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
5119 char_u *dest;
5120
5121 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5122 if (dest != NULL)
5123 {
5124 STRCPY(dest, fname1);
5125 if (sep)
5126 add_pathsep(dest);
5127 STRCAT(dest, fname2);
5128 }
5129 return dest;
5130}
5131
Bram Moolenaard6754642005-01-17 22:18:45 +00005132/*
5133 * Concatenate two strings and return the result in allocated memory.
5134 * Returns NULL when out of memory.
5135 */
5136 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005137concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005138{
5139 char_u *dest;
5140 size_t l = STRLEN(str1);
5141
5142 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5143 if (dest != NULL)
5144 {
5145 STRCPY(dest, str1);
5146 STRCPY(dest + l, str2);
5147 }
5148 return dest;
5149}
Bram Moolenaard6754642005-01-17 22:18:45 +00005150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * Add a path separator to a file name, unless it already ends in a path
5153 * separator.
5154 */
5155 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005156add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005158 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 STRCAT(p, PATHSEPSTR);
5160}
5161
5162/*
5163 * FullName_save - Make an allocated copy of a full file name.
5164 * Returns NULL when out of memory.
5165 */
5166 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005167FullName_save(
5168 char_u *fname,
5169 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005170 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 char_u *buf;
5173 char_u *new_fname = NULL;
5174
5175 if (fname == NULL)
5176 return NULL;
5177
5178 buf = alloc((unsigned)MAXPATHL);
5179 if (buf != NULL)
5180 {
5181 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5182 new_fname = vim_strsave(buf);
5183 else
5184 new_fname = vim_strsave(fname);
5185 vim_free(buf);
5186 }
5187 return new_fname;
5188}
5189
5190#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5191
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005192static char_u *skip_string(char_u *p);
5193static pos_T *ind_find_start_comment(void);
5194static pos_T *ind_find_start_CORS(void);
5195static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
5197/*
5198 * Find the start of a comment, not knowing if we are in a comment right now.
5199 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005200 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005202 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005203ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005204{
5205 return find_start_comment(curbuf->b_ind_maxcomment);
5206}
5207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005209find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 pos_T *pos;
5212 char_u *line;
5213 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005214 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005216 for (;;)
5217 {
5218 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5219 if (pos == NULL)
5220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005222 /*
5223 * Check if the comment start we found is inside a string.
5224 * If it is then restrict the search to below this line and try again.
5225 */
5226 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005227 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005228 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005229 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 break;
5231 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5232 if (cur_maxcomment <= 0)
5233 {
5234 pos = NULL;
5235 break;
5236 }
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 return pos;
5239}
5240
5241/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005242 * Find the start of a comment or raw string, not knowing if we are in a
5243 * comment or raw string right now.
5244 * Search starts at w_cursor.lnum and goes backwards.
5245 * Return NULL when not inside a comment or raw string.
5246 * "CORS" -> Comment Or Raw String
5247 */
5248 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005249ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005250{
Bram Moolenaar089af182015-10-07 11:41:49 +02005251 static pos_T comment_pos_copy;
5252 pos_T *comment_pos;
5253 pos_T *rs_pos;
5254
5255 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5256 if (comment_pos != NULL)
5257 {
5258 /* Need to make a copy of the static pos in findmatchlimit(),
5259 * calling find_start_rawstring() may change it. */
5260 comment_pos_copy = *comment_pos;
5261 comment_pos = &comment_pos_copy;
5262 }
5263 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005264
5265 /* If comment_pos is before rs_pos the raw string is inside the comment.
5266 * If rs_pos is before comment_pos the comment is inside the raw string. */
5267 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5268 return rs_pos;
5269 return comment_pos;
5270}
5271
5272/*
5273 * Find the start of a raw string, not knowing if we are in one right now.
5274 * Search starts at w_cursor.lnum and goes backwards.
5275 * Return NULL when not inside a raw string.
5276 */
5277 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005279{
5280 pos_T *pos;
5281 char_u *line;
5282 char_u *p;
5283 int cur_maxcomment = ind_maxcomment;
5284
5285 for (;;)
5286 {
5287 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5288 if (pos == NULL)
5289 break;
5290
5291 /*
5292 * Check if the raw string start we found is inside a string.
5293 * If it is then restrict the search to below this line and try again.
5294 */
5295 line = ml_get(pos->lnum);
5296 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5297 p = skip_string(p);
5298 if ((colnr_T)(p - line) <= pos->col)
5299 break;
5300 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5301 if (cur_maxcomment <= 0)
5302 {
5303 pos = NULL;
5304 break;
5305 }
5306 }
5307 return pos;
5308}
5309
5310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 * Skip to the end of a "string" and a 'c' character.
5312 * If there is no string or character, return argument unmodified.
5313 */
5314 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005315skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 int i;
5318
5319 /*
5320 * We loop, because strings may be concatenated: "date""time".
5321 */
5322 for ( ; ; ++p)
5323 {
5324 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5325 {
5326 if (!p[1]) /* ' at end of line */
5327 break;
5328 i = 2;
5329 if (p[1] == '\\') /* '\n' or '\000' */
5330 {
5331 ++i;
5332 while (vim_isdigit(p[i - 1])) /* '\000' */
5333 ++i;
5334 }
5335 if (p[i] == '\'') /* check for trailing ' */
5336 {
5337 p += i;
5338 continue;
5339 }
5340 }
5341 else if (p[0] == '"') /* start of string */
5342 {
5343 for (++p; p[0]; ++p)
5344 {
5345 if (p[0] == '\\' && p[1] != NUL)
5346 ++p;
5347 else if (p[0] == '"') /* end of string */
5348 break;
5349 }
5350 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005351 continue; /* continue for another string */
5352 }
5353 else if (p[0] == 'R' && p[1] == '"')
5354 {
5355 /* Raw string: R"[delim](...)[delim]" */
5356 char_u *delim = p + 2;
5357 char_u *paren = vim_strchr(delim, '(');
5358
5359 if (paren != NULL)
5360 {
5361 size_t delim_len = paren - delim;
5362
5363 for (p += 3; *p; ++p)
5364 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5365 && p[delim_len + 1] == '"')
5366 {
5367 p += delim_len + 1;
5368 break;
5369 }
5370 if (p[0] == '"')
5371 continue; /* continue for another string */
5372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374 break; /* no string found */
5375 }
5376 if (!*p)
5377 --p; /* backup from NUL */
5378 return p;
5379}
5380#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5381
5382#if defined(FEAT_CINDENT) || defined(PROTO)
5383
5384/*
5385 * Do C or expression indenting on the current line.
5386 */
5387 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005388do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
5390# ifdef FEAT_EVAL
5391 if (*curbuf->b_p_inde != NUL)
5392 fixthisline(get_expr_indent);
5393 else
5394# endif
5395 fixthisline(get_c_indent);
5396}
5397
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005398/* Find result cache for cpp_baseclass */
5399typedef struct {
5400 int found;
5401 lpos_T lpos;
5402} cpp_baseclass_cache_T;
5403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404/*
5405 * Functions for C-indenting.
5406 * Most of this originally comes from Eric Fischer.
5407 */
5408/*
5409 * Below "XXX" means that this function may unlock the current line.
5410 */
5411
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005412static char_u *cin_skipcomment(char_u *);
5413static int cin_nocode(char_u *);
5414static pos_T *find_line_comment(void);
5415static int cin_has_js_key(char_u *text);
5416static int cin_islabel_skip(char_u **);
5417static int cin_isdefault(char_u *);
5418static char_u *after_label(char_u *l);
5419static int get_indent_nolabel(linenr_T lnum);
5420static int skip_label(linenr_T, char_u **pp);
5421static int cin_first_id_amount(void);
5422static int cin_get_equal_amount(linenr_T lnum);
5423static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005424static int cin_iscomment(char_u *);
5425static int cin_islinecomment(char_u *);
5426static int cin_isterminated(char_u *, int, int);
5427static int cin_isinit(void);
5428static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5429static int cin_isif(char_u *);
5430static int cin_iselse(char_u *);
5431static int cin_isdo(char_u *);
5432static int cin_iswhileofdo(char_u *, linenr_T);
5433static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5434static int cin_iswhileofdo_end(int terminated);
5435static int cin_isbreak(char_u *);
5436static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5437static int get_baseclass_amount(int col);
5438static int cin_ends_in(char_u *, char_u *, char_u *);
5439static int cin_starts_with(char_u *s, char *word);
5440static int cin_skip2pos(pos_T *trypos);
5441static pos_T *find_start_brace(void);
5442static pos_T *find_match_paren(int);
5443static pos_T *find_match_char(int c, int ind_maxparen);
5444static int corr_ind_maxparen(pos_T *startpos);
5445static int find_last_paren(char_u *l, int start, int end);
5446static int find_match(int lookfor, linenr_T ourscope);
5447static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
5449/*
5450 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005451 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 */
5453 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005454cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 while (*s)
5457 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005458 char_u *prev_s = s;
5459
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005461
5462 /* Perl/shell # comment comment continues until eol. Require a space
5463 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005464 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005465 {
5466 s += STRLEN(s);
5467 break;
5468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 if (*s != '/')
5470 break;
5471 ++s;
5472 if (*s == '/') /* slash-slash comment continues till eol */
5473 {
5474 s += STRLEN(s);
5475 break;
5476 }
5477 if (*s != '*')
5478 break;
5479 for (++s; *s; ++s) /* skip slash-star comment */
5480 if (s[0] == '*' && s[1] == '/')
5481 {
5482 s += 2;
5483 break;
5484 }
5485 }
5486 return s;
5487}
5488
5489/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005490 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 * not considered code.
5492 */
5493 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005494cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 return *cin_skipcomment(s) == NUL;
5497}
5498
5499/*
5500 * Check previous lines for a "//" line comment, skipping over blank lines.
5501 */
5502 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005503find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 static pos_T pos;
5506 char_u *line;
5507 char_u *p;
5508
5509 pos = curwin->w_cursor;
5510 while (--pos.lnum > 0)
5511 {
5512 line = ml_get(pos.lnum);
5513 p = skipwhite(line);
5514 if (cin_islinecomment(p))
5515 {
5516 pos.col = (int)(p - line);
5517 return &pos;
5518 }
5519 if (*p != NUL)
5520 break;
5521 }
5522 return NULL;
5523}
5524
5525/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005526 * Return TRUE if "text" starts with "key:".
5527 */
5528 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005529cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005530{
5531 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005532 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005533
5534 if (*s == '\'' || *s == '"')
5535 {
5536 /* can be 'key': or "key": */
5537 quote = *s;
5538 ++s;
5539 }
5540 if (!vim_isIDc(*s)) /* need at least one ID character */
5541 return FALSE;
5542
5543 while (vim_isIDc(*s))
5544 ++s;
5545 if (*s == quote)
5546 ++s;
5547
5548 s = cin_skipcomment(s);
5549
5550 /* "::" is not a label, it's C++ */
5551 return (*s == ':' && s[1] != ':');
5552}
5553
5554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005556 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 */
5558 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005559cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 if (!vim_isIDc(**s)) /* need at least one ID character */
5562 return FALSE;
5563
5564 while (vim_isIDc(**s))
5565 (*s)++;
5566
5567 *s = cin_skipcomment(*s);
5568
5569 /* "::" is not a label, it's C++ */
5570 return (**s == ':' && *++*s != ':');
5571}
5572
5573/*
5574 * Recognize a label: "label:".
5575 * Note: curwin->w_cursor must be where we are looking for the label.
5576 */
5577 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 char_u *s;
5581
5582 s = cin_skipcomment(ml_get_curline());
5583
5584 /*
5585 * Exclude "default" from labels, since it should be indented
5586 * like a switch label. Same for C++ scope declarations.
5587 */
5588 if (cin_isdefault(s))
5589 return FALSE;
5590 if (cin_isscopedecl(s))
5591 return FALSE;
5592
5593 if (cin_islabel_skip(&s))
5594 {
5595 /*
5596 * Only accept a label if the previous line is terminated or is a case
5597 * label.
5598 */
5599 pos_T cursor_save;
5600 pos_T *trypos;
5601 char_u *line;
5602
5603 cursor_save = curwin->w_cursor;
5604 while (curwin->w_cursor.lnum > 1)
5605 {
5606 --curwin->w_cursor.lnum;
5607
5608 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005609 * If we're in a comment or raw string now, skip to the start of
5610 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 */
5612 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005613 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 curwin->w_cursor = *trypos;
5615
5616 line = ml_get_curline();
5617 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5618 continue;
5619 if (*(line = cin_skipcomment(line)) == NUL)
5620 continue;
5621
5622 curwin->w_cursor = cursor_save;
5623 if (cin_isterminated(line, TRUE, FALSE)
5624 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005625 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 || (cin_islabel_skip(&line) && cin_nocode(line)))
5627 return TRUE;
5628 return FALSE;
5629 }
5630 curwin->w_cursor = cursor_save;
5631 return TRUE; /* label at start of file??? */
5632 }
5633 return FALSE;
5634}
5635
5636/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005637 * Recognize structure initialization and enumerations:
5638 * "[typedef] [static|public|protected|private] enum"
5639 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 */
5641 static int
5642cin_isinit(void)
5643{
5644 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005645 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
5647 s = cin_skipcomment(ml_get_curline());
5648
Bram Moolenaar75342212013-03-07 13:13:52 +01005649 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 s = cin_skipcomment(s + 7);
5651
Bram Moolenaar75342212013-03-07 13:13:52 +01005652 for (;;)
5653 {
5654 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005655
Bram Moolenaar75342212013-03-07 13:13:52 +01005656 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5657 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005658 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005659 if (cin_starts_with(s, skip[i]))
5660 {
5661 s = cin_skipcomment(s + l);
5662 l = 0;
5663 break;
5664 }
5665 }
5666 if (l != 0)
5667 break;
5668 }
5669
5670 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 return TRUE;
5672
5673 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5674 return TRUE;
5675
5676 return FALSE;
5677}
5678
5679/*
5680 * Recognize a switch label: "case .*:" or "default:".
5681 */
5682 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005683cin_iscase(
5684 char_u *s,
5685 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005688 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
5690 for (s += 4; *s; ++s)
5691 {
5692 s = cin_skipcomment(s);
5693 if (*s == ':')
5694 {
5695 if (s[1] == ':') /* skip over "::" for C++ */
5696 ++s;
5697 else
5698 return TRUE;
5699 }
5700 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005701 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5703 return FALSE; /* stop at comment */
5704 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005705 {
5706 /* JS etc. */
5707 if (strict)
5708 return FALSE; /* stop at string */
5709 else
5710 return TRUE;
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 return FALSE;
5714 }
5715
5716 if (cin_isdefault(s))
5717 return TRUE;
5718 return FALSE;
5719}
5720
5721/*
5722 * Recognize a "default" switch label.
5723 */
5724 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005725cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 return (STRNCMP(s, "default", 7) == 0
5728 && *(s = cin_skipcomment(s + 7)) == ':'
5729 && s[1] != ':');
5730}
5731
5732/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005733 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 */
5735 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005736cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 int i;
5739
5740 s = cin_skipcomment(s);
5741 if (STRNCMP(s, "public", 6) == 0)
5742 i = 6;
5743 else if (STRNCMP(s, "protected", 9) == 0)
5744 i = 9;
5745 else if (STRNCMP(s, "private", 7) == 0)
5746 i = 7;
5747 else
5748 return FALSE;
5749 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5750}
5751
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005752/* Maximum number of lines to search back for a "namespace" line. */
5753#define FIND_NAMESPACE_LIM 20
5754
5755/*
5756 * Recognize a "namespace" scope declaration.
5757 */
5758 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005759cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005760{
5761 char_u *p;
5762 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005763 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005764
5765 s = cin_skipcomment(s);
5766 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5767 {
5768 p = cin_skipcomment(skipwhite(s + 9));
5769 while (*p != NUL)
5770 {
5771 if (vim_iswhite(*p))
5772 {
5773 has_name = TRUE; /* found end of a name */
5774 p = cin_skipcomment(skipwhite(p));
5775 }
5776 else if (*p == '{')
5777 {
5778 break;
5779 }
5780 else if (vim_iswordc(*p))
5781 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005782 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005783 if (has_name)
5784 return FALSE; /* word character after skipping past name */
5785 ++p;
5786 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005787 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5788 {
5789 if (!has_name_start || has_name)
5790 return FALSE;
5791 /* C++ 17 nested namespace */
5792 p += 3;
5793 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005794 else
5795 {
5796 return FALSE;
5797 }
5798 }
5799 return TRUE;
5800 }
5801 return FALSE;
5802}
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804/*
5805 * Return a pointer to the first non-empty non-comment character after a ':'.
5806 * Return NULL if not found.
5807 * case 234: a = b;
5808 * ^
5809 */
5810 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005811after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 for ( ; *l; ++l)
5814 {
5815 if (*l == ':')
5816 {
5817 if (l[1] == ':') /* skip over "::" for C++ */
5818 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005819 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 break;
5821 }
5822 else if (*l == '\'' && l[1] && l[2] == '\'')
5823 l += 2; /* skip over 'x' */
5824 }
5825 if (*l == NUL)
5826 return NULL;
5827 l = cin_skipcomment(l + 1);
5828 if (*l == NUL)
5829 return NULL;
5830 return l;
5831}
5832
5833/*
5834 * Get indent of line "lnum", skipping a label.
5835 * Return 0 if there is nothing after the label.
5836 */
5837 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005838get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 char_u *l;
5841 pos_T fp;
5842 colnr_T col;
5843 char_u *p;
5844
5845 l = ml_get(lnum);
5846 p = after_label(l);
5847 if (p == NULL)
5848 return 0;
5849
5850 fp.col = (colnr_T)(p - l);
5851 fp.lnum = lnum;
5852 getvcol(curwin, &fp, &col, NULL, NULL);
5853 return (int)col;
5854}
5855
5856/*
5857 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005858 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 * label: if (asdf && asdfasdf)
5860 * ^
5861 */
5862 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005863skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 char_u *l;
5866 int amount;
5867 pos_T cursor_save;
5868
5869 cursor_save = curwin->w_cursor;
5870 curwin->w_cursor.lnum = lnum;
5871 l = ml_get_curline();
5872 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005873 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
5875 amount = get_indent_nolabel(lnum);
5876 l = after_label(ml_get_curline());
5877 if (l == NULL) /* just in case */
5878 l = ml_get_curline();
5879 }
5880 else
5881 {
5882 amount = get_indent();
5883 l = ml_get_curline();
5884 }
5885 *pp = l;
5886
5887 curwin->w_cursor = cursor_save;
5888 return amount;
5889}
5890
5891/*
5892 * Return the indent of the first variable name after a type in a declaration.
5893 * int a, indent of "a"
5894 * static struct foo b, indent of "b"
5895 * enum bla c, indent of "c"
5896 * Returns zero when it doesn't look like a declaration.
5897 */
5898 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005899cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *line, *p, *s;
5902 int len;
5903 pos_T fp;
5904 colnr_T col;
5905
5906 line = ml_get_curline();
5907 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005908 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5910 {
5911 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005912 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5915 p = skipwhite(p + 6);
5916 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5917 p = skipwhite(p + 4);
5918 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5919 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5920 {
5921 s = skipwhite(p + len);
5922 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5923 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5924 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5925 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5926 p = s;
5927 }
5928 for (len = 0; vim_isIDc(p[len]); ++len)
5929 ;
5930 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5931 return 0;
5932
5933 p = skipwhite(p + len);
5934 fp.lnum = curwin->w_cursor.lnum;
5935 fp.col = (colnr_T)(p - line);
5936 getvcol(curwin, &fp, &col, NULL, NULL);
5937 return (int)col;
5938}
5939
5940/*
5941 * Return the indent of the first non-blank after an equal sign.
5942 * char *foo = "here";
5943 * Return zero if no (useful) equal sign found.
5944 * Return -1 if the line above "lnum" ends in a backslash.
5945 * foo = "asdf\
5946 * asdf\
5947 * here";
5948 */
5949 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005950cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952 char_u *line;
5953 char_u *s;
5954 colnr_T col;
5955 pos_T fp;
5956
5957 if (lnum > 1)
5958 {
5959 line = ml_get(lnum - 1);
5960 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5961 return -1;
5962 }
5963
5964 line = s = ml_get(lnum);
5965 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5966 {
5967 if (cin_iscomment(s)) /* ignore comments */
5968 s = cin_skipcomment(s);
5969 else
5970 ++s;
5971 }
5972 if (*s != '=')
5973 return 0;
5974
5975 s = skipwhite(s + 1);
5976 if (cin_nocode(s))
5977 return 0;
5978
5979 if (*s == '"') /* nice alignment for continued strings */
5980 ++s;
5981
5982 fp.lnum = lnum;
5983 fp.col = (colnr_T)(s - line);
5984 getvcol(curwin, &fp, &col, NULL, NULL);
5985 return (int)col;
5986}
5987
5988/*
5989 * Recognize a preprocessor statement: Any line that starts with '#'.
5990 */
5991 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005992cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005994 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 return TRUE;
5996 return FALSE;
5997}
5998
5999/*
6000 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6001 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6002 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006003 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 */
6005 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006006cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 char_u *line = *pp;
6009 linenr_T lnum = *lnump;
6010 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006011 int candidate_amount = *amount;
6012
6013 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6014 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006016 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 {
6018 if (cin_ispreproc(line))
6019 {
6020 retval = TRUE;
6021 *lnump = lnum;
6022 break;
6023 }
6024 if (lnum == 1)
6025 break;
6026 line = ml_get(--lnum);
6027 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6028 break;
6029 }
6030
6031 if (lnum != *lnump)
6032 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006033 if (retval)
6034 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 return retval;
6036}
6037
6038/*
6039 * Recognize the start of a C or C++ comment.
6040 */
6041 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006042cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6045}
6046
6047/*
6048 * Recognize the start of a "//" comment.
6049 */
6050 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006051cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
6053 return (p[0] == '/' && p[1] == '/');
6054}
6055
6056/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006057 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6058 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006060 * If a line begins with an "else", only consider it terminated if no unmatched
6061 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 * Return the character terminating the line (ending char's have precedence if
6063 * both apply in order to determine initializations).
6064 */
6065 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006066cin_isterminated(
6067 char_u *s,
6068 int incl_open, /* include '{' at the end as terminator */
6069 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006071 char_u found_start = 0;
6072 unsigned n_open = 0;
6073 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 s = cin_skipcomment(s);
6076
6077 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6078 found_start = *s;
6079
Bram Moolenaar496f9512011-05-19 16:35:09 +02006080 if (!found_start)
6081 is_else = cin_iselse(s);
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 while (*s)
6084 {
6085 /* skip over comments, "" strings and 'c'haracters */
6086 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006087 if (*s == '}' && n_open > 0)
6088 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006089 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006090 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 && cin_nocode(s + 1))
6092 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006093 else if (*s == '{')
6094 {
6095 if (incl_open && cin_nocode(s + 1))
6096 return *s;
6097 else
6098 ++n_open;
6099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 if (*s)
6102 s++;
6103 }
6104 return found_start;
6105}
6106
6107/*
6108 * Recognize the basic picture of a function declaration -- it needs to
6109 * have an open paren somewhere and a close paren at the end of the line and
6110 * no semicolons anywhere.
6111 * When a line ends in a comma we continue looking in the next line.
6112 * "sp" points to a string with the line. When looking at other lines it must
6113 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006114 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006115 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 */
6117 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006118cin_isfuncdecl(
6119 char_u **sp,
6120 linenr_T first_lnum,
6121 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 char_u *s;
6124 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006125 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006127 pos_T *trypos;
6128 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 if (sp == NULL)
6131 s = ml_get(lnum);
6132 else
6133 s = *sp;
6134
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006135 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006136 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006137 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006138 {
6139 lnum = trypos->lnum;
6140 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006141 {
6142 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006143 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006144 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006145
6146 s = ml_get(lnum);
6147 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006148 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006149
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006150 /* Ignore line starting with #. */
6151 if (cin_ispreproc(s))
6152 return FALSE;
6153
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6155 {
6156 if (cin_iscomment(s)) /* ignore comments */
6157 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006158 else if (*s == ':')
6159 {
6160 if (*(s + 1) == ':')
6161 s += 2;
6162 else
6163 /* To avoid a mistake in the following situation:
6164 * A::A(int a, int b)
6165 * : a(0) // <--not a function decl
6166 * , b(0)
6167 * {...
6168 */
6169 return FALSE;
6170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 else
6172 ++s;
6173 }
6174 if (*s != '(')
6175 return FALSE; /* ';', ' or " before any () or no '(' */
6176
6177 while (*s && *s != ';' && *s != '\'' && *s != '"')
6178 {
6179 if (*s == ')' && cin_nocode(s + 1))
6180 {
6181 /* ')' at the end: may have found a match
6182 * Check for he previous line not to end in a backslash:
6183 * #if defined(x) && \
6184 * defined(y)
6185 */
6186 lnum = first_lnum - 1;
6187 s = ml_get(lnum);
6188 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6189 retval = TRUE;
6190 goto done;
6191 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006192 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006194 int comma = (*s == ',');
6195
6196 /* ',' at the end: continue looking in the next line.
6197 * At the end: check for ',' in the next line, for this style:
6198 * func(arg1
6199 * , arg2) */
6200 for (;;)
6201 {
6202 if (lnum >= curbuf->b_ml.ml_line_count)
6203 break;
6204 s = ml_get(++lnum);
6205 if (!cin_ispreproc(s))
6206 break;
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 if (lnum >= curbuf->b_ml.ml_line_count)
6209 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006210 /* Require a comma at end of the line or a comma or ')' at the
6211 * start of next line. */
6212 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006213 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006214 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006215 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
6217 else if (cin_iscomment(s)) /* ignore comments */
6218 s = cin_skipcomment(s);
6219 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006222 just_started = FALSE;
6223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
6225
6226done:
6227 if (lnum != first_lnum && sp != NULL)
6228 *sp = ml_get(first_lnum);
6229
6230 return retval;
6231}
6232
6233 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006236 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237}
6238
6239 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006240cin_iselse(
6241 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 if (*p == '}') /* accept "} else" */
6244 p = cin_skipcomment(p + 1);
6245 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6246}
6247
6248 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006249cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
6251 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6252}
6253
6254/*
6255 * Check if this is a "while" that should have a matching "do".
6256 * We only accept a "while (condition) ;", with only white space between the
6257 * ')' and ';'. The condition may be spread over several lines.
6258 */
6259 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006260cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 pos_T cursor_save;
6263 pos_T *trypos;
6264 int retval = FALSE;
6265
6266 p = cin_skipcomment(p);
6267 if (*p == '}') /* accept "} while (cond);" */
6268 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006269 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 cursor_save = curwin->w_cursor;
6272 curwin->w_cursor.lnum = lnum;
6273 curwin->w_cursor.col = 0;
6274 p = ml_get_curline();
6275 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6276 {
6277 ++p;
6278 ++curwin->w_cursor.col;
6279 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006280 if ((trypos = findmatchlimit(NULL, 0, 0,
6281 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6283 retval = TRUE;
6284 curwin->w_cursor = cursor_save;
6285 }
6286 return retval;
6287}
6288
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006289/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006290 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006291 * Return 0 if there is none.
6292 * Otherwise return !0 and update "*poffset" to point to the place where the
6293 * string was found.
6294 */
6295 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006296cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006297{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006298 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006299
6300 if (offset-- < 2)
6301 return 0;
6302 while (offset > 2 && vim_iswhite(line[offset]))
6303 --offset;
6304
6305 offset -= 1;
6306 if (!STRNCMP(line + offset, "if", 2))
6307 goto probablyFound;
6308
6309 if (offset >= 1)
6310 {
6311 offset -= 1;
6312 if (!STRNCMP(line + offset, "for", 3))
6313 goto probablyFound;
6314
6315 if (offset >= 2)
6316 {
6317 offset -= 2;
6318 if (!STRNCMP(line + offset, "while", 5))
6319 goto probablyFound;
6320 }
6321 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006322 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006323
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006324probablyFound:
6325 if (!offset || !vim_isIDc(line[offset - 1]))
6326 {
6327 *poffset = offset;
6328 return 1;
6329 }
6330 return 0;
6331}
6332
6333/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006334 * Return TRUE if we are at the end of a do-while.
6335 * do
6336 * nothing;
6337 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006338 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006339 * Adjust the cursor to the line with "while".
6340 */
6341 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006342cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006343{
6344 char_u *line;
6345 char_u *p;
6346 char_u *s;
6347 pos_T *trypos;
6348 int i;
6349
6350 if (terminated != ';') /* there must be a ';' at the end */
6351 return FALSE;
6352
6353 p = line = ml_get_curline();
6354 while (*p != NUL)
6355 {
6356 p = cin_skipcomment(p);
6357 if (*p == ')')
6358 {
6359 s = skipwhite(p + 1);
6360 if (*s == ';' && cin_nocode(s + 1))
6361 {
6362 /* Found ");" at end of the line, now check there is "while"
6363 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006364 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006365 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006366 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006367 if (trypos != NULL)
6368 {
6369 s = cin_skipcomment(ml_get(trypos->lnum));
6370 if (*s == '}') /* accept "} while (cond);" */
6371 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006372 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006373 {
6374 curwin->w_cursor.lnum = trypos->lnum;
6375 return TRUE;
6376 }
6377 }
6378
6379 /* Searching may have made "line" invalid, get it again. */
6380 line = ml_get_curline();
6381 p = line + i;
6382 }
6383 }
6384 if (*p != NUL)
6385 ++p;
6386 }
6387 return FALSE;
6388}
6389
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006391cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6394}
6395
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006396/*
6397 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 * constructor-initialization. eg:
6399 *
6400 * class MyClass :
6401 * baseClass <-- here
6402 * class MyClass : public baseClass,
6403 * anotherBaseClass <-- here (should probably lineup ??)
6404 * MyClass::MyClass(...) :
6405 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006406 *
6407 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 */
6409 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006410cin_is_cpp_baseclass(
6411 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006413 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 char_u *s;
6415 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006416 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006417 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006419 if (pos->lnum <= lnum)
6420 return cached->found; /* Use the cached result */
6421
6422 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006424 s = skipwhite(line);
6425 if (*s == '#') /* skip #define FOO x ? (x) : x */
6426 return FALSE;
6427 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 if (*s == NUL)
6429 return FALSE;
6430
6431 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6432
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006433 /* Search for a line starting with '#', empty, ending in ';' or containing
6434 * '{' or '}' and start below it. This handles the following situations:
6435 * a = cond ?
6436 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006437 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006438 * func::foo()
6439 * : something
6440 * {}
6441 * Foo::Foo (int one, int two)
6442 * : something(4),
6443 * somethingelse(3)
6444 * {}
6445 */
6446 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006448 line = ml_get(lnum - 1);
6449 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006450 if (*s == '#' || *s == NUL)
6451 break;
6452 while (*s != NUL)
6453 {
6454 s = cin_skipcomment(s);
6455 if (*s == '{' || *s == '}'
6456 || (*s == ';' && cin_nocode(s + 1)))
6457 break;
6458 if (*s != NUL)
6459 ++s;
6460 }
6461 if (*s != NUL)
6462 break;
6463 --lnum;
6464 }
6465
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006466 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006467 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006468 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006469 for (;;)
6470 {
6471 if (*s == NUL)
6472 {
6473 if (lnum == curwin->w_cursor.lnum)
6474 break;
6475 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006476 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006477 s = line;
6478 }
6479 if (s == line)
6480 {
6481 /* don't recognize "case (foo):" as a baseclass */
6482 if (cin_iscase(s, FALSE))
6483 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006484 s = cin_skipcomment(line);
6485 if (*s == NUL)
6486 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006487 }
6488
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006489 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006490 s = skip_string(s) + 1;
6491 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 {
6493 if (s[1] == ':')
6494 {
6495 /* skip double colon. It can't be a constructor
6496 * initialization any more */
6497 lookfor_ctor_init = FALSE;
6498 s = cin_skipcomment(s + 2);
6499 }
6500 else if (lookfor_ctor_init || class_or_struct)
6501 {
6502 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006503 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 cpp_base_class = TRUE;
6505 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006506 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 s = cin_skipcomment(s + 1);
6508 }
6509 else
6510 s = cin_skipcomment(s + 1);
6511 }
6512 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6513 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6514 {
6515 class_or_struct = TRUE;
6516 lookfor_ctor_init = FALSE;
6517
6518 if (*s == 'c')
6519 s = cin_skipcomment(s + 5);
6520 else
6521 s = cin_skipcomment(s + 6);
6522 }
6523 else
6524 {
6525 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6526 {
6527 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6528 }
6529 else if (s[0] == ')')
6530 {
6531 /* Constructor-initialization is assumed if we come across
6532 * something like "):" */
6533 class_or_struct = FALSE;
6534 lookfor_ctor_init = TRUE;
6535 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006536 else if (s[0] == '?')
6537 {
6538 /* Avoid seeing '() :' after '?' as constructor init. */
6539 return FALSE;
6540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 else if (!vim_isIDc(s[0]))
6542 {
6543 /* if it is not an identifier, we are wrong */
6544 class_or_struct = FALSE;
6545 lookfor_ctor_init = FALSE;
6546 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006547 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
6549 /* it can't be a constructor-initialization any more */
6550 lookfor_ctor_init = FALSE;
6551
6552 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006553 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006554 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006557 /* When the line ends in a comma don't align with it. */
6558 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006559 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006560
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 s = cin_skipcomment(s + 1);
6562 }
6563 }
6564
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006565 cached->found = cpp_base_class;
6566 if (cpp_base_class)
6567 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 return cpp_base_class;
6569}
6570
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006571 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006572get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006573{
6574 int amount;
6575 colnr_T vcol;
6576 pos_T *trypos;
6577
6578 if (col == 0)
6579 {
6580 amount = get_indent();
6581 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006582 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006583 amount = get_indent_lnum(trypos->lnum); /* XXX */
6584 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006585 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006586 }
6587 else
6588 {
6589 curwin->w_cursor.col = col;
6590 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6591 amount = (int)vcol;
6592 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006593 if (amount < curbuf->b_ind_cpp_baseclass)
6594 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006595 return amount;
6596}
6597
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598/*
6599 * Return TRUE if string "s" ends with the string "find", possibly followed by
6600 * white space and comments. Skip strings and comments.
6601 * Ignore "ignore" after "find" if it's not NULL.
6602 */
6603 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006604cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 char_u *p = s;
6607 char_u *r;
6608 int len = (int)STRLEN(find);
6609
6610 while (*p != NUL)
6611 {
6612 p = cin_skipcomment(p);
6613 if (STRNCMP(p, find, len) == 0)
6614 {
6615 r = skipwhite(p + len);
6616 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6617 r = skipwhite(r + STRLEN(ignore));
6618 if (cin_nocode(r))
6619 return TRUE;
6620 }
6621 if (*p != NUL)
6622 ++p;
6623 }
6624 return FALSE;
6625}
6626
6627/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006628 * Return TRUE when "s" starts with "word" and then a non-ID character.
6629 */
6630 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006631cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006632{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006633 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006634
6635 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6636}
6637
6638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 * Skip strings, chars and comments until at or past "trypos".
6640 * Return the column found.
6641 */
6642 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006643cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 char_u *line;
6646 char_u *p;
6647
6648 p = line = ml_get(trypos->lnum);
6649 while (*p && (colnr_T)(p - line) < trypos->col)
6650 {
6651 if (cin_iscomment(p))
6652 p = cin_skipcomment(p);
6653 else
6654 {
6655 p = skip_string(p);
6656 ++p;
6657 }
6658 }
6659 return (int)(p - line);
6660}
6661
6662/*
6663 * Find the '{' at the start of the block we are in.
6664 * Return NULL if no match found.
6665 * Ignore a '{' that is in a comment, makes indenting the next three lines
6666 * work. */
6667/* foo() */
6668/* { */
6669/* } */
6670
6671 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006672find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 pos_T cursor_save;
6675 pos_T *trypos;
6676 pos_T *pos;
6677 static pos_T pos_copy;
6678
6679 cursor_save = curwin->w_cursor;
6680 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6681 {
6682 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6683 trypos = &pos_copy;
6684 curwin->w_cursor = *trypos;
6685 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006686 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006688 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 break;
6690 if (pos != NULL)
6691 curwin->w_cursor.lnum = pos->lnum;
6692 }
6693 curwin->w_cursor = cursor_save;
6694 return trypos;
6695}
6696
6697/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006698 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006699 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 */
6701 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006704 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006705}
6706
6707 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006708find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006709{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 pos_T cursor_save;
6711 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006712 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006713 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006716 ind_maxp_wk = ind_maxparen;
6717retry:
6718 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
6720 /* check if the ( is in a // comment */
6721 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006722 {
6723 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6724 if (ind_maxp_wk > 0)
6725 {
6726 curwin->w_cursor = *trypos;
6727 curwin->w_cursor.col = 0; /* XXX */
6728 goto retry;
6729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 else
6733 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006734 pos_T *trypos_wk;
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6737 trypos = &pos_copy;
6738 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006739 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006740 {
6741 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6742 - trypos_wk->lnum);
6743 if (ind_maxp_wk > 0)
6744 {
6745 curwin->w_cursor = *trypos_wk;
6746 goto retry;
6747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 }
6751 }
6752 curwin->w_cursor = cursor_save;
6753 return trypos;
6754}
6755
6756/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006757 * Find the matching '(', ignoring it if it is in a comment or before an
6758 * unmatched {.
6759 * Return NULL if no match found.
6760 */
6761 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006762find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006763{
6764 pos_T *trypos = find_match_paren(ind_maxparen);
6765
6766 if (trypos != NULL)
6767 {
6768 pos_T *tryposBrace = find_start_brace();
6769
6770 /* If both an unmatched '(' and '{' is found. Ignore the '('
6771 * position if the '{' is further down. */
6772 if (tryposBrace != NULL
6773 && (trypos->lnum != tryposBrace->lnum
6774 ? trypos->lnum < tryposBrace->lnum
6775 : trypos->col < tryposBrace->col))
6776 trypos = NULL;
6777 }
6778 return trypos;
6779}
6780
6781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 * Return ind_maxparen corrected for the difference in line number between the
6783 * cursor position and "startpos". This makes sure that searching for a
6784 * matching paren above the cursor line doesn't find a match because of
6785 * looking a few lines further.
6786 */
6787 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006788corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6791
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006792 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6793 return curbuf->b_ind_maxparen - (int)n;
6794 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795}
6796
6797/*
6798 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006799 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 */
6801 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006802find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803{
6804 int i;
6805 int retval = FALSE;
6806 int open_count = 0;
6807
6808 curwin->w_cursor.col = 0; /* default is start of line */
6809
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006810 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
6812 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6813 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6814 if (l[i] == start)
6815 ++open_count;
6816 else if (l[i] == end)
6817 {
6818 if (open_count > 0)
6819 --open_count;
6820 else
6821 {
6822 curwin->w_cursor.col = i;
6823 retval = TRUE;
6824 }
6825 }
6826 }
6827 return retval;
6828}
6829
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006830/*
6831 * Parse 'cinoptions' and set the values in "curbuf".
6832 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6833 */
6834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006835parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006836{
6837 char_u *p;
6838 char_u *l;
6839 char_u *digits;
6840 int n;
6841 int divider;
6842 int fraction = 0;
6843 int sw = (int)get_sw_value(buf);
6844
6845 /*
6846 * Set the default values.
6847 */
6848 /* Spaces from a block's opening brace the prevailing indent for that
6849 * block should be. */
6850 buf->b_ind_level = sw;
6851
6852 /* Spaces from the edge of the line an open brace that's at the end of a
6853 * line is imagined to be. */
6854 buf->b_ind_open_imag = 0;
6855
6856 /* Spaces from the prevailing indent for a line that is not preceded by
6857 * an opening brace. */
6858 buf->b_ind_no_brace = 0;
6859
6860 /* Column where the first { of a function should be located }. */
6861 buf->b_ind_first_open = 0;
6862
6863 /* Spaces from the prevailing indent a leftmost open brace should be
6864 * located. */
6865 buf->b_ind_open_extra = 0;
6866
6867 /* Spaces from the matching open brace (real location for one at the left
6868 * edge; imaginary location from one that ends a line) the matching close
6869 * brace should be located. */
6870 buf->b_ind_close_extra = 0;
6871
6872 /* Spaces from the edge of the line an open brace sitting in the leftmost
6873 * column is imagined to be. */
6874 buf->b_ind_open_left_imag = 0;
6875
6876 /* Spaces jump labels should be shifted to the left if N is non-negative,
6877 * otherwise the jump label will be put to column 1. */
6878 buf->b_ind_jump_label = -1;
6879
6880 /* Spaces from the switch() indent a "case xx" label should be located. */
6881 buf->b_ind_case = sw;
6882
6883 /* Spaces from the "case xx:" code after a switch() should be located. */
6884 buf->b_ind_case_code = sw;
6885
6886 /* Lineup break at end of case in switch() with case label. */
6887 buf->b_ind_case_break = 0;
6888
6889 /* Spaces from the class declaration indent a scope declaration label
6890 * should be located. */
6891 buf->b_ind_scopedecl = sw;
6892
6893 /* Spaces from the scope declaration label code should be located. */
6894 buf->b_ind_scopedecl_code = sw;
6895
6896 /* Amount K&R-style parameters should be indented. */
6897 buf->b_ind_param = sw;
6898
6899 /* Amount a function type spec should be indented. */
6900 buf->b_ind_func_type = sw;
6901
6902 /* Amount a cpp base class declaration or constructor initialization
6903 * should be indented. */
6904 buf->b_ind_cpp_baseclass = sw;
6905
6906 /* additional spaces beyond the prevailing indent a continuation line
6907 * should be located. */
6908 buf->b_ind_continuation = sw;
6909
6910 /* Spaces from the indent of the line with an unclosed parentheses. */
6911 buf->b_ind_unclosed = sw * 2;
6912
6913 /* Spaces from the indent of the line with an unclosed parentheses, which
6914 * itself is also unclosed. */
6915 buf->b_ind_unclosed2 = sw;
6916
6917 /* Suppress ignoring spaces from the indent of a line starting with an
6918 * unclosed parentheses. */
6919 buf->b_ind_unclosed_noignore = 0;
6920
6921 /* If the opening paren is the last nonwhite character on the line, and
6922 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6923 * context (for very long lines). */
6924 buf->b_ind_unclosed_wrapped = 0;
6925
6926 /* Suppress ignoring white space when lining up with the character after
6927 * an unclosed parentheses. */
6928 buf->b_ind_unclosed_whiteok = 0;
6929
6930 /* Indent a closing parentheses under the line start of the matching
6931 * opening parentheses. */
6932 buf->b_ind_matching_paren = 0;
6933
6934 /* Indent a closing parentheses under the previous line. */
6935 buf->b_ind_paren_prev = 0;
6936
6937 /* Extra indent for comments. */
6938 buf->b_ind_comment = 0;
6939
6940 /* Spaces from the comment opener when there is nothing after it. */
6941 buf->b_ind_in_comment = 3;
6942
6943 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6944 * after the comment opener. */
6945 buf->b_ind_in_comment2 = 0;
6946
6947 /* Max lines to search for an open paren. */
6948 buf->b_ind_maxparen = 20;
6949
6950 /* Max lines to search for an open comment. */
6951 buf->b_ind_maxcomment = 70;
6952
6953 /* Handle braces for java code. */
6954 buf->b_ind_java = 0;
6955
6956 /* Not to confuse JS object properties with labels. */
6957 buf->b_ind_js = 0;
6958
6959 /* Handle blocked cases correctly. */
6960 buf->b_ind_keep_case_label = 0;
6961
6962 /* Handle C++ namespace. */
6963 buf->b_ind_cpp_namespace = 0;
6964
6965 /* Handle continuation lines containing conditions of if(), for() and
6966 * while(). */
6967 buf->b_ind_if_for_while = 0;
6968
6969 for (p = buf->b_p_cino; *p; )
6970 {
6971 l = p++;
6972 if (*p == '-')
6973 ++p;
6974 digits = p; /* remember where the digits start */
6975 n = getdigits(&p);
6976 divider = 0;
6977 if (*p == '.') /* ".5s" means a fraction */
6978 {
6979 fraction = atol((char *)++p);
6980 while (VIM_ISDIGIT(*p))
6981 {
6982 ++p;
6983 if (divider)
6984 divider *= 10;
6985 else
6986 divider = 10;
6987 }
6988 }
6989 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6990 {
6991 if (p == digits)
6992 n = sw; /* just "s" is one 'shiftwidth' */
6993 else
6994 {
6995 n *= sw;
6996 if (divider)
6997 n += (sw * fraction + divider / 2) / divider;
6998 }
6999 ++p;
7000 }
7001 if (l[1] == '-')
7002 n = -n;
7003
7004 /* When adding an entry here, also update the default 'cinoptions' in
7005 * doc/indent.txt, and add explanation for it! */
7006 switch (*l)
7007 {
7008 case '>': buf->b_ind_level = n; break;
7009 case 'e': buf->b_ind_open_imag = n; break;
7010 case 'n': buf->b_ind_no_brace = n; break;
7011 case 'f': buf->b_ind_first_open = n; break;
7012 case '{': buf->b_ind_open_extra = n; break;
7013 case '}': buf->b_ind_close_extra = n; break;
7014 case '^': buf->b_ind_open_left_imag = n; break;
7015 case 'L': buf->b_ind_jump_label = n; break;
7016 case ':': buf->b_ind_case = n; break;
7017 case '=': buf->b_ind_case_code = n; break;
7018 case 'b': buf->b_ind_case_break = n; break;
7019 case 'p': buf->b_ind_param = n; break;
7020 case 't': buf->b_ind_func_type = n; break;
7021 case '/': buf->b_ind_comment = n; break;
7022 case 'c': buf->b_ind_in_comment = n; break;
7023 case 'C': buf->b_ind_in_comment2 = n; break;
7024 case 'i': buf->b_ind_cpp_baseclass = n; break;
7025 case '+': buf->b_ind_continuation = n; break;
7026 case '(': buf->b_ind_unclosed = n; break;
7027 case 'u': buf->b_ind_unclosed2 = n; break;
7028 case 'U': buf->b_ind_unclosed_noignore = n; break;
7029 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7030 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7031 case 'm': buf->b_ind_matching_paren = n; break;
7032 case 'M': buf->b_ind_paren_prev = n; break;
7033 case ')': buf->b_ind_maxparen = n; break;
7034 case '*': buf->b_ind_maxcomment = n; break;
7035 case 'g': buf->b_ind_scopedecl = n; break;
7036 case 'h': buf->b_ind_scopedecl_code = n; break;
7037 case 'j': buf->b_ind_java = n; break;
7038 case 'J': buf->b_ind_js = n; break;
7039 case 'l': buf->b_ind_keep_case_label = n; break;
7040 case '#': buf->b_ind_hash_comment = n; break;
7041 case 'N': buf->b_ind_cpp_namespace = n; break;
7042 case 'k': buf->b_ind_if_for_while = n; break;
7043 }
7044 if (*p == ',')
7045 ++p;
7046 }
7047}
7048
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007049/*
7050 * Return the desired indent for C code.
7051 * Return -1 if the indent should be left alone (inside a raw string).
7052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007054get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 pos_T cur_curpos;
7057 int amount;
7058 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007059 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 colnr_T col;
7061 char_u *theline;
7062 char_u *linecopy;
7063 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007064 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007066 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 pos_T our_paren_pos;
7068 char_u *start;
7069 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007070#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071#define BRACE_AT_START 2 /* '{' is at start of line */
7072#define BRACE_AT_END 3 /* '{' is at end of line */
7073 linenr_T ourscope;
7074 char_u *l;
7075 char_u *look;
7076 char_u terminated;
7077 int lookfor;
7078#define LOOKFOR_INITIAL 0
7079#define LOOKFOR_IF 1
7080#define LOOKFOR_DO 2
7081#define LOOKFOR_CASE 3
7082#define LOOKFOR_ANY 4
7083#define LOOKFOR_TERM 5
7084#define LOOKFOR_UNTERM 6
7085#define LOOKFOR_SCOPEDECL 7
7086#define LOOKFOR_NOBREAK 8
7087#define LOOKFOR_CPP_BASECLASS 9
7088#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007089#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007090#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
7092 int whilelevel;
7093 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 int n;
7095 int iscase;
7096 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007097 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007099 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007100 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007101 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007102 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007104 /* make a copy, value is changed below */
7105 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 /* remember where the cursor was when we started */
7108 cur_curpos = curwin->w_cursor;
7109
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007110 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007111 if (cur_curpos.lnum == 1)
7112 return 0;
7113
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 /* Get a copy of the current contents of the line.
7115 * This is required, because only the most recent line obtained with
7116 * ml_get is valid! */
7117 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7118 if (linecopy == NULL)
7119 return 0;
7120
7121 /*
7122 * In insert mode and the cursor is on a ')' truncate the line at the
7123 * cursor position. We don't want to line up with the matching '(' when
7124 * inserting new stuff.
7125 * For unknown reasons the cursor might be past the end of the line, thus
7126 * check for that.
7127 */
7128 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007129 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 && linecopy[curwin->w_cursor.col] == ')')
7131 linecopy[curwin->w_cursor.col] = NUL;
7132
7133 theline = skipwhite(linecopy);
7134
7135 /* move the cursor to the start of the line */
7136
7137 curwin->w_cursor.col = 0;
7138
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007139 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007142 * If we are inside a raw string don't change the indent.
7143 * Ignore a raw string inside a comment.
7144 */
7145 comment_pos = ind_find_start_comment();
7146 if (comment_pos != NULL)
7147 {
7148 /* findmatchlimit() static pos is overwritten, make a copy */
7149 tryposCopy = *comment_pos;
7150 comment_pos = &tryposCopy;
7151 }
7152 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7153 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7154 {
7155 amount = -1;
7156 goto laterend;
7157 }
7158
7159 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 * #defines and so on always go at the left when included in 'cinkeys'.
7161 */
7162 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007163 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007164 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007165 goto theend;
7166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007169 * Is it a non-case label? Then that goes at the left margin too unless:
7170 * - JS flag is set.
7171 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007173 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007174 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
7176 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007177 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179
7180 /*
7181 * If we're inside a "//" comment and there is a "//" comment in a
7182 * previous line, lineup with that one.
7183 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007184 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 && (trypos = find_line_comment()) != NULL) /* XXX */
7186 {
7187 /* find how indented the line beginning the comment is */
7188 getvcol(curwin, trypos, &col, NULL, NULL);
7189 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007190 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192
7193 /*
7194 * If we're inside a comment and not looking at the start of the
7195 * comment, try using the 'comments' option.
7196 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007197 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
7199 int lead_start_len = 2;
7200 int lead_middle_len = 1;
7201 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7202 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7203 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7204 char_u *p;
7205 int start_align = 0;
7206 int start_off = 0;
7207 int done = FALSE;
7208
7209 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007210 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007212 *lead_start = NUL;
7213 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
7215 p = curbuf->b_p_com;
7216 while (*p != NUL)
7217 {
7218 int align = 0;
7219 int off = 0;
7220 int what = 0;
7221
7222 while (*p != NUL && *p != ':')
7223 {
7224 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7225 what = *p++;
7226 else if (*p == COM_LEFT || *p == COM_RIGHT)
7227 align = *p++;
7228 else if (VIM_ISDIGIT(*p) || *p == '-')
7229 off = getdigits(&p);
7230 else
7231 ++p;
7232 }
7233
7234 if (*p == ':')
7235 ++p;
7236 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7237 if (what == COM_START)
7238 {
7239 STRCPY(lead_start, lead_end);
7240 lead_start_len = (int)STRLEN(lead_start);
7241 start_off = off;
7242 start_align = align;
7243 }
7244 else if (what == COM_MIDDLE)
7245 {
7246 STRCPY(lead_middle, lead_end);
7247 lead_middle_len = (int)STRLEN(lead_middle);
7248 }
7249 else if (what == COM_END)
7250 {
7251 /* If our line starts with the middle comment string, line it
7252 * up with the comment opener per the 'comments' option. */
7253 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7254 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7255 {
7256 done = TRUE;
7257 if (curwin->w_cursor.lnum > 1)
7258 {
7259 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007260 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 * the middle comment string matches in the previous
7262 * line, use the indent of that line. XXX */
7263 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7264 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7265 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7266 else if (STRNCMP(look, lead_middle,
7267 lead_middle_len) == 0)
7268 {
7269 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7270 break;
7271 }
7272 /* If the start comment string doesn't match with the
7273 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007274 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 lead_start, lead_start_len) != 0)
7276 continue;
7277 }
7278 if (start_off != 0)
7279 amount += start_off;
7280 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007281 amount += vim_strsize(lead_start)
7282 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 break;
7284 }
7285
7286 /* If our line starts with the end comment string, line it up
7287 * with the middle comment */
7288 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7289 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7290 {
7291 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7292 /* XXX */
7293 if (off != 0)
7294 amount += off;
7295 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007296 amount += vim_strsize(lead_start)
7297 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 done = TRUE;
7299 break;
7300 }
7301 }
7302 }
7303
7304 /* If our line starts with an asterisk, line up with the
7305 * asterisk in the comment opener; otherwise, line up
7306 * with the first character of the comment text.
7307 */
7308 if (done)
7309 ;
7310 else if (theline[0] == '*')
7311 amount += 1;
7312 else
7313 {
7314 /*
7315 * If we are more than one line away from the comment opener, take
7316 * the indent of the previous non-empty line. If 'cino' has "CO"
7317 * and we are just below the comment opener and there are any
7318 * white characters after it line up with the text after it;
7319 * otherwise, add the amount specified by "c" in 'cino'
7320 */
7321 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007322 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 {
7324 if (linewhite(lnum)) /* skip blank lines */
7325 continue;
7326 amount = get_indent_lnum(lnum); /* XXX */
7327 break;
7328 }
7329 if (amount == -1) /* use the comment opener */
7330 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007331 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007333 start = ml_get(comment_pos->lnum);
7334 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007336 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007338 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007340 if (curbuf->b_ind_in_comment2 || *look == NUL)
7341 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
7343 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007344 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 }
7346
7347 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007348 * Are we looking at a ']' that has a match?
7349 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007350 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007351 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7352 {
7353 /* align with the line containing the '['. */
7354 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007355 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007356 }
7357
7358 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * Are we inside parentheses or braces?
7360 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007361 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007362 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007363 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 || trypos != NULL)
7365 {
7366 if (trypos != NULL && tryposBrace != NULL)
7367 {
7368 /* Both an unmatched '(' and '{' is found. Use the one which is
7369 * closer to the current cursor position, set the other to NULL. */
7370 if (trypos->lnum != tryposBrace->lnum
7371 ? trypos->lnum < tryposBrace->lnum
7372 : trypos->col < tryposBrace->col)
7373 trypos = NULL;
7374 else
7375 tryposBrace = NULL;
7376 }
7377
7378 if (trypos != NULL)
7379 {
7380 /*
7381 * If the matching paren is more than one line away, use the indent of
7382 * a previous non-empty line that matches the same paren.
7383 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007384 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007386 /* Line up with the start of the matching paren line. */
7387 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7388 }
7389 else
7390 {
7391 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007392 our_paren_pos = *trypos;
7393 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007395 l = skipwhite(ml_get(lnum));
7396 if (cin_nocode(l)) /* skip comment lines */
7397 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007398 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007399 continue; /* ignore #define, #if, etc. */
7400 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007402 /* Skip a comment or raw string. XXX */
7403 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007404 {
7405 lnum = trypos->lnum + 1;
7406 continue;
7407 }
7408
7409 /* XXX */
7410 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007411 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007412 && trypos->lnum == our_paren_pos.lnum
7413 && trypos->col == our_paren_pos.col)
7414 {
7415 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007417 if (theline[0] == ')')
7418 {
7419 if (our_paren_pos.lnum != lnum
7420 && cur_amount > amount)
7421 cur_amount = amount;
7422 amount = -1;
7423 }
7424 break;
7425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427 }
7428
7429 /*
7430 * Line up with line where the matching paren is. XXX
7431 * If the line starts with a '(' or the indent for unclosed
7432 * parentheses is zero, line up with the unclosed parentheses.
7433 */
7434 if (amount == -1)
7435 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007436 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007437 int is_if_for_while = 0;
7438
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007439 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007440 {
7441 /* Look for the outermost opening parenthesis on this line
7442 * and check whether it belongs to an "if", "for" or "while". */
7443
7444 pos_T cursor_save = curwin->w_cursor;
7445 pos_T outermost;
7446 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007447
7448 trypos = &our_paren_pos;
7449 do {
7450 outermost = *trypos;
7451 curwin->w_cursor.lnum = outermost.lnum;
7452 curwin->w_cursor.col = outermost.col;
7453
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007454 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007455 } while (trypos && trypos->lnum == outermost.lnum);
7456
7457 curwin->w_cursor = cursor_save;
7458
7459 line = ml_get(outermost.lnum);
7460
7461 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007462 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007463 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007464
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007465 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007466 look = skipwhite(look);
7467 if (*look == '(')
7468 {
7469 linenr_T save_lnum = curwin->w_cursor.lnum;
7470 char_u *line;
7471 int look_col;
7472
7473 /* Ignore a '(' in front of the line that has a match before
7474 * our matching '('. */
7475 curwin->w_cursor.lnum = our_paren_pos.lnum;
7476 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007477 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007478 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007479 if ((trypos = findmatchlimit(NULL, ')', 0,
7480 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007481 != NULL
7482 && trypos->lnum == our_paren_pos.lnum
7483 && trypos->col < our_paren_pos.col)
7484 ignore_paren_col = trypos->col + 1;
7485
7486 curwin->w_cursor.lnum = save_lnum;
7487 look = ml_get(our_paren_pos.lnum) + look_col;
7488 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007489 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7490 && is_if_for_while == 0)
7491 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007492 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 {
7494 /*
7495 * If we're looking at a close paren, line up right there;
7496 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007497 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 * the last nonwhite character of the line, use either the
7499 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007500 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 * lines).
7502 */
7503 if (theline[0] != ')')
7504 {
7505 cur_amount = MAXCOL;
7506 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007507 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 && cin_ends_in(l, (char_u *)"(", NULL))
7509 {
7510 /* look for opening unmatched paren, indent one level
7511 * for each additional level */
7512 n = 1;
7513 for (col = 0; col < our_paren_pos.col; ++col)
7514 {
7515 switch (l[col])
7516 {
7517 case '(':
7518 case '{': ++n;
7519 break;
7520
7521 case ')':
7522 case '}': if (n > 1)
7523 --n;
7524 break;
7525 }
7526 }
7527
7528 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007529 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007531 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 our_paren_pos.col++;
7533 else
7534 {
7535 col = our_paren_pos.col + 1;
7536 while (vim_iswhite(l[col]))
7537 col++;
7538 if (l[col] != NUL) /* In case of trailing space */
7539 our_paren_pos.col = col;
7540 else
7541 our_paren_pos.col++;
7542 }
7543 }
7544
7545 /*
7546 * Find how indented the paren is, or the character after it
7547 * if we did the above "if".
7548 */
7549 if (our_paren_pos.col > 0)
7550 {
7551 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7552 if (cur_amount > (int)col)
7553 cur_amount = col;
7554 }
7555 }
7556
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007557 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {
7559 /* Line up with the start of the matching paren line. */
7560 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007561 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7562 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007563 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 {
7565 if (cur_amount != MAXCOL)
7566 amount = cur_amount;
7567 }
7568 else
7569 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007570 /* Add b_ind_unclosed2 for each '(' before our matching one,
7571 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007573 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {
7575 --our_paren_pos.col;
7576 switch (*ml_get_pos(&our_paren_pos))
7577 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007578 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 col = our_paren_pos.col;
7580 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007581 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 col = MAXCOL;
7583 break;
7584 }
7585 }
7586
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007587 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 * braces */
7589 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007590 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 else
7592 {
7593 curwin->w_cursor.lnum = our_paren_pos.lnum;
7594 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007595 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7596 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007597 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007599 {
7600 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007601 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007602 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007603 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606 /*
7607 * For a line starting with ')' use the minimum of the two
7608 * positions, to avoid giving it more indent than the previous
7609 * lines:
7610 * func_long_name( if (x
7611 * arg && yy
7612 * ) ^ not here ) ^ not here
7613 */
7614 if (cur_amount < amount)
7615 amount = cur_amount;
7616 }
7617 }
7618
7619 /* add extra indent for a comment */
7620 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007621 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 else
7624 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007625 /*
7626 * We are inside braces, there is a { before this line at the position
7627 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007628 * Make a copy of tryposBrace, it may point to pos_copy inside
7629 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007630 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007631 tryposCopy = *tryposBrace;
7632 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 ourscope = trypos->lnum;
7635 start = ml_get(ourscope);
7636
7637 /*
7638 * Now figure out how indented the line is in general.
7639 * If the brace was at the start of the line, we use that;
7640 * otherwise, check out the indentation of the line as
7641 * a whole and then add the "imaginary indent" to that.
7642 */
7643 look = skipwhite(start);
7644 if (*look == '{')
7645 {
7646 getvcol(curwin, trypos, &col, NULL, NULL);
7647 amount = col;
7648 if (*start == '{')
7649 start_brace = BRACE_IN_COL0;
7650 else
7651 start_brace = BRACE_AT_START;
7652 }
7653 else
7654 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007655 /* That opening brace might have been on a continuation
7656 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 curwin->w_cursor.lnum = ourscope;
7658
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007659 /* Position the cursor over the rightmost paren, so that
7660 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 lnum = ourscope;
7662 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007663 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7664 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 lnum = trypos->lnum;
7666
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007667 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 * case 1: if (asdf &&
7669 * ldfd) {
7670 * }
7671 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007672 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7673 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007675 else if (curbuf->b_ind_js)
7676 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007678 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
7680 start_brace = BRACE_AT_END;
7681 }
7682
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007683 /* For Javascript check if the line starts with "key:". */
7684 if (curbuf->b_ind_js)
7685 js_cur_has_key = cin_has_js_key(theline);
7686
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007688 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 * we want to be. otherwise, add the amount of room
7690 * that an indent is supposed to be.
7691 */
7692 if (theline[0] == '}')
7693 {
7694 /*
7695 * they may want closing braces to line up with something
7696 * other than the open brace. indulge them, if so.
7697 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007698 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700 else
7701 {
7702 /*
7703 * If we're looking at an "else", try to find an "if"
7704 * to match it with.
7705 * If we're looking at a "while", try to find a "do"
7706 * to match it with.
7707 */
7708 lookfor = LOOKFOR_INITIAL;
7709 if (cin_iselse(theline))
7710 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007711 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 lookfor = LOOKFOR_DO;
7713 if (lookfor != LOOKFOR_INITIAL)
7714 {
7715 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007716 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
7718 amount = get_indent(); /* XXX */
7719 goto theend;
7720 }
7721 }
7722
7723 /*
7724 * We get here if we are not on an "while-of-do" or "else" (or
7725 * failed to find a matching "if").
7726 * Search backwards for something to line up with.
7727 * First set amount for when we don't find anything.
7728 */
7729
7730 /*
7731 * if the '{' is _really_ at the left margin, use the imaginary
7732 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007733 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 */
7735
7736 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7737 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007738 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007739 lookfor_cpp_namespace = TRUE;
7740 }
7741 else if (start_brace == BRACE_AT_START &&
7742 lookfor_cpp_namespace) /* '{' is at start */
7743 {
7744
7745 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747 else
7748 {
7749 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007750 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007751 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007752
7753 l = skipwhite(ml_get_curline());
7754 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007755 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 else
7758 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007759 /* Compensate for adding b_ind_open_extra later. */
7760 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (amount < 0)
7762 amount = 0;
7763 }
7764 }
7765
7766 lookfor_break = FALSE;
7767
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007768 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {
7770 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007771 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773 else if (cin_isscopedecl(theline)) /* private:, ... */
7774 {
7775 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007776 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
7778 else
7779 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007780 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7781 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 lookfor_break = TRUE;
7783
7784 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007785 /* b_ind_level from start of block */
7786 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 }
7788 scope_amount = amount;
7789 whilelevel = 0;
7790
7791 /*
7792 * Search backwards. If we find something we recognize, line up
7793 * with that.
7794 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007795 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 * the usual amount relative to the conditional
7797 * that opens the block.
7798 */
7799 curwin->w_cursor = cur_curpos;
7800 for (;;)
7801 {
7802 curwin->w_cursor.lnum--;
7803 curwin->w_cursor.col = 0;
7804
7805 /*
7806 * If we went all the way back to the start of our scope, line
7807 * up with it.
7808 */
7809 if (curwin->w_cursor.lnum <= ourscope)
7810 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007811 /* We reached end of scope:
7812 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007814 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 * don't add ind_continuation, otherwise it is a variable
7816 * declaration:
7817 * int x,
7818 * here; <-- add ind_continuation
7819 */
7820 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7821 {
7822 if (curwin->w_cursor.lnum == 0
7823 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007824 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007826 /* nothing found (abuse curbuf->b_ind_maxparen as
7827 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 * initialization) */
7829 if (cont_amount > 0)
7830 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007831 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 amount += ind_continuation;
7833 break;
7834 }
7835
7836 l = ml_get_curline();
7837
7838 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007839 * If we're in a comment or raw string now, skip to
7840 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007842 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 if (trypos != NULL)
7844 {
7845 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007846 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 continue;
7848 }
7849
7850 /*
7851 * Skip preprocessor directives and blank lines.
7852 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007853 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7854 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 continue;
7856
7857 if (cin_nocode(l))
7858 continue;
7859
7860 terminated = cin_isterminated(l, FALSE, TRUE);
7861
7862 /*
7863 * If we are at top level and the line looks like a
7864 * function declaration, we are done
7865 * (it's a variable declaration).
7866 */
7867 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007868 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {
7870 /* if the line is terminated with another ','
7871 * it is a continued variable initialization.
7872 * don't add extra indent.
7873 * TODO: does not work, if a function
7874 * declaration is split over multiple lines:
7875 * cin_isfuncdecl returns FALSE then.
7876 */
7877 if (terminated == ',')
7878 break;
7879
7880 /* if it es a enum declaration or an assignment,
7881 * we are done.
7882 */
7883 if (terminated != ';' && cin_isinit())
7884 break;
7885
7886 /* nothing useful found */
7887 if (terminated == 0 || terminated == '{')
7888 continue;
7889 }
7890
7891 if (terminated != ';')
7892 {
7893 /* Skip parens and braces. Position the cursor
7894 * over the rightmost paren, so that matching it
7895 * will take us back to the start of the line.
7896 */ /* XXX */
7897 trypos = NULL;
7898 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007899 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007900 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
7902 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007903 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
7905 if (trypos != NULL)
7906 {
7907 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007908 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 continue;
7910 }
7911 }
7912
7913 /* it's a variable declaration, add indentation
7914 * like in
7915 * int a,
7916 * b;
7917 */
7918 if (cont_amount > 0)
7919 amount = cont_amount;
7920 else
7921 amount += ind_continuation;
7922 }
7923 else if (lookfor == LOOKFOR_UNTERM)
7924 {
7925 if (cont_amount > 0)
7926 amount = cont_amount;
7927 else
7928 amount += ind_continuation;
7929 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007930 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007931 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007932 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007933 && lookfor != LOOKFOR_CPP_BASECLASS
7934 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007935 {
7936 amount = scope_amount;
7937 if (theline[0] == '{')
7938 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007939 amount += curbuf->b_ind_open_extra;
7940 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007941 }
7942 }
7943
7944 if (lookfor_cpp_namespace)
7945 {
7946 /*
7947 * Looking for C++ namespace, need to look further
7948 * back.
7949 */
7950 if (curwin->w_cursor.lnum == ourscope)
7951 continue;
7952
7953 if (curwin->w_cursor.lnum == 0
7954 || curwin->w_cursor.lnum
7955 < ourscope - FIND_NAMESPACE_LIM)
7956 break;
7957
7958 l = ml_get_curline();
7959
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007960 /* If we're in a comment or raw string now, skip
7961 * to the start of it. */
7962 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007963 if (trypos != NULL)
7964 {
7965 curwin->w_cursor.lnum = trypos->lnum + 1;
7966 curwin->w_cursor.col = 0;
7967 continue;
7968 }
7969
7970 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007971 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7972 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02007973 continue;
7974
7975 /* Finally the actual check for "namespace". */
7976 if (cin_is_cpp_namespace(l))
7977 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007978 amount += curbuf->b_ind_cpp_namespace
7979 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007980 break;
7981 }
7982
7983 if (cin_nocode(l))
7984 continue;
7985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
7987 break;
7988 }
7989
7990 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007991 * If we're in a comment or raw string now, skip to the start
7992 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007994 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {
7996 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007997 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 continue;
7999 }
8000
8001 l = ml_get_curline();
8002
8003 /*
8004 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008005 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008007 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (iscase || cin_isscopedecl(l))
8009 {
8010 /* we are only looking for cpp base class
8011 * declaration/initialization any longer */
8012 if (lookfor == LOOKFOR_CPP_BASECLASS)
8013 break;
8014
8015 /* When looking for a "do" we are not interested in
8016 * labels. */
8017 if (whilelevel > 0)
8018 continue;
8019
8020 /*
8021 * case xx:
8022 * c = 99 + <- this indent plus continuation
8023 *-> here;
8024 */
8025 if (lookfor == LOOKFOR_UNTERM
8026 || lookfor == LOOKFOR_ENUM_OR_INIT)
8027 {
8028 if (cont_amount > 0)
8029 amount = cont_amount;
8030 else
8031 amount += ind_continuation;
8032 break;
8033 }
8034
8035 /*
8036 * case xx: <- line up with this case
8037 * x = 333;
8038 * case yy:
8039 */
8040 if ( (iscase && lookfor == LOOKFOR_CASE)
8041 || (iscase && lookfor_break)
8042 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8043 {
8044 /*
8045 * Check that this case label is not for another
8046 * switch()
8047 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008048 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008049 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {
8051 amount = get_indent(); /* XXX */
8052 break;
8053 }
8054 continue;
8055 }
8056
8057 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8058
8059 /*
8060 * case xx: if (cond) <- line up with this if
8061 * y = y + 1;
8062 * -> s = 99;
8063 *
8064 * case xx:
8065 * if (cond) <- line up with this line
8066 * y = y + 1;
8067 * -> s = 99;
8068 */
8069 if (lookfor == LOOKFOR_TERM)
8070 {
8071 if (n)
8072 amount = n;
8073
8074 if (!lookfor_break)
8075 break;
8076 }
8077
8078 /*
8079 * case xx: x = x + 1; <- line up with this x
8080 * -> y = y + 1;
8081 *
8082 * case xx: if (cond) <- line up with this if
8083 * -> y = y + 1;
8084 */
8085 if (n)
8086 {
8087 amount = n;
8088 l = after_label(ml_get_curline());
8089 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008090 {
8091 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008092 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008093 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008094 amount += curbuf->b_ind_level
8095 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 break;
8098 }
8099
8100 /*
8101 * Try to get the indent of a statement before the switch
8102 * label. If nothing is found, line up relative to the
8103 * switch label.
8104 * break; <- may line up with this line
8105 * case xx:
8106 * -> y = 1;
8107 */
8108 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008109 ? curbuf->b_ind_case_code
8110 : curbuf->b_ind_scopedecl_code);
8111 lookfor = curbuf->b_ind_case_break
8112 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 continue;
8114 }
8115
8116 /*
8117 * Looking for a switch() label or C++ scope declaration,
8118 * ignore other lines, skip {}-blocks.
8119 */
8120 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8121 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008122 if (find_last_paren(l, '{', '}')
8123 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008126 curwin->w_cursor.col = 0;
8127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 continue;
8129 }
8130
8131 /*
8132 * Ignore jump labels with nothing after them.
8133 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008134 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
8136 l = after_label(ml_get_curline());
8137 if (l == NULL || cin_nocode(l))
8138 continue;
8139 }
8140
8141 /*
8142 * Ignore #defines, #if, etc.
8143 * Ignore comment and empty lines.
8144 * (need to get the line again, cin_islabel() may have
8145 * unlocked it)
8146 */
8147 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008148 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 || cin_nocode(l))
8150 continue;
8151
8152 /*
8153 * Are we at the start of a cpp base class declaration or
8154 * constructor initialization?
8155 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008156 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008157 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008158 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008159 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008160 l = ml_get_curline();
8161 }
8162 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {
8164 if (lookfor == LOOKFOR_UNTERM)
8165 {
8166 if (cont_amount > 0)
8167 amount = cont_amount;
8168 else
8169 amount += ind_continuation;
8170 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008171 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008173 /* Need to find start of the declaration. */
8174 lookfor = LOOKFOR_UNTERM;
8175 ind_continuation = 0;
8176 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 }
8178 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008179 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008180 amount = get_baseclass_amount(
8181 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 break;
8183 }
8184 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8185 {
8186 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008187 * declaration or initialization before the opening brace.
8188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 if (cin_isterminated(l, TRUE, FALSE))
8190 break;
8191 else
8192 continue;
8193 }
8194
8195 /*
8196 * What happens next depends on the line being terminated.
8197 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008198 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 * 123,
8200 * sizeof
8201 * here
8202 * Otherwise check whether it is a enumeration or structure
8203 * initialisation (not indented) or a variable declaration
8204 * (indented).
8205 */
8206 terminated = cin_isterminated(l, FALSE, TRUE);
8207
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008208 if (js_cur_has_key)
8209 {
8210 js_cur_has_key = 0; /* only check the first line */
8211 if (curbuf->b_ind_js && terminated == ',')
8212 {
8213 /* For Javascript we might be inside an object:
8214 * key: something, <- align with this
8215 * key: something
8216 * or:
8217 * key: something + <- align with this
8218 * something,
8219 * key: something
8220 */
8221 lookfor = LOOKFOR_JS_KEY;
8222 }
8223 }
8224 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8225 {
8226 amount = get_indent();
8227 break;
8228 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008229 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008230 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008231 if (tryposBrace != NULL && tryposBrace->lnum
8232 >= curwin->w_cursor.lnum)
8233 break;
8234 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008235 /* line below current line is the one that starts a
8236 * (possibly broken) line ending in a comma. */
8237 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008238 else
8239 {
8240 amount = get_indent();
8241 if (curwin->w_cursor.lnum - 1 == ourscope)
8242 /* line above is start of the scope, thus current
8243 * line is the one that stars a (possibly broken)
8244 * line ending in a comma. */
8245 break;
8246 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008247 }
8248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8250 && terminated == ','))
8251 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008252 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8253 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008254 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 /*
8256 * if we're in the middle of a paren thing,
8257 * go back to the line that starts it so
8258 * we can get the right prevailing indent
8259 * if ( foo &&
8260 * bar )
8261 */
8262 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008263 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008265 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 */
8267 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008268 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008269 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8270 || (trypos->lnum == tryposBrace->lnum
8271 && trypos->col < tryposBrace->col)))
8272 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273
8274 /*
8275 * If we are looking for ',', we also look for matching
8276 * braces.
8277 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008278 if (trypos == NULL && terminated == ','
8279 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008280 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281
8282 if (trypos != NULL)
8283 {
8284 /*
8285 * Check if we are on a case label now. This is
8286 * handled above.
8287 * case xx: if ( asdf &&
8288 * asdf)
8289 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008290 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008292 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
8294 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008295 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 continue;
8297 }
8298 }
8299
8300 /*
8301 * Skip over continuation lines to find the one to get the
8302 * indent from
8303 * char *usethis = "bla\
8304 * bla",
8305 * here;
8306 */
8307 if (terminated == ',')
8308 {
8309 while (curwin->w_cursor.lnum > 1)
8310 {
8311 l = ml_get(curwin->w_cursor.lnum - 1);
8312 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8313 break;
8314 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008315 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317 }
8318
8319 /*
8320 * Get indent and pointer to text for current line,
8321 * ignoring any jump label. XXX
8322 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008323 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008324 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008325 else
8326 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 /*
8328 * If this is just above the line we are indenting, and it
8329 * starts with a '{', line it up with this line.
8330 * while (not)
8331 * -> {
8332 * }
8333 */
8334 if (terminated != ',' && lookfor != LOOKFOR_TERM
8335 && theline[0] == '{')
8336 {
8337 amount = cur_amount;
8338 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008339 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 * doesn't start with a '{', which must have a match
8341 * in the same line (scope is the same). Probably:
8342 * { 1, 2 },
8343 * -> { 3, 4 }
8344 */
8345 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008346 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008348 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {
8350 /* have to look back, whether it is a cpp base
8351 * class declaration or initialization */
8352 lookfor = LOOKFOR_CPP_BASECLASS;
8353 continue;
8354 }
8355 break;
8356 }
8357
8358 /*
8359 * Check if we are after an "if", "while", etc.
8360 * Also allow " } else".
8361 */
8362 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8363 {
8364 /*
8365 * Found an unterminated line after an if (), line up
8366 * with the last one.
8367 * if (cond)
8368 * 100 +
8369 * -> here;
8370 */
8371 if (lookfor == LOOKFOR_UNTERM
8372 || lookfor == LOOKFOR_ENUM_OR_INIT)
8373 {
8374 if (cont_amount > 0)
8375 amount = cont_amount;
8376 else
8377 amount += ind_continuation;
8378 break;
8379 }
8380
8381 /*
8382 * If this is just above the line we are indenting, we
8383 * are finished.
8384 * while (not)
8385 * -> here;
8386 * Otherwise this indent can be used when the line
8387 * before this is terminated.
8388 * yyy;
8389 * if (stat)
8390 * while (not)
8391 * xxx;
8392 * -> here;
8393 */
8394 amount = cur_amount;
8395 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008396 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 if (lookfor != LOOKFOR_TERM)
8398 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008399 amount += curbuf->b_ind_level
8400 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 break;
8402 }
8403
8404 /*
8405 * Special trick: when expecting the while () after a
8406 * do, line up with the while()
8407 * do
8408 * x = 1;
8409 * -> here
8410 */
8411 l = skipwhite(ml_get_curline());
8412 if (cin_isdo(l))
8413 {
8414 if (whilelevel == 0)
8415 break;
8416 --whilelevel;
8417 }
8418
8419 /*
8420 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008421 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 * Need to use the scope of this "else". XXX
8423 * If whilelevel != 0 continue looking for a "do {".
8424 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008425 if (cin_iselse(l) && whilelevel == 0)
8426 {
8427 /* If we're looking at "} else", let's make sure we
8428 * find the opening brace of the enclosing scope,
8429 * not the one from "if () {". */
8430 if (*l == '}')
8431 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008432 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008433
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008434 if ((trypos = find_start_brace()) == NULL
8435 || find_match(LOOKFOR_IF, trypos->lnum)
8436 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008437 break;
8438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
8440
8441 /*
8442 * If we're below an unterminated line that is not an
8443 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008444 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 * the line before this one.
8446 */
8447 else
8448 {
8449 /*
8450 * Found two unterminated lines on a row, line up with
8451 * the last one.
8452 * c = 99 +
8453 * 100 +
8454 * -> here;
8455 */
8456 if (lookfor == LOOKFOR_UNTERM)
8457 {
8458 /* When line ends in a comma add extra indent */
8459 if (terminated == ',')
8460 amount += ind_continuation;
8461 break;
8462 }
8463
8464 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8465 {
8466 /* Found two lines ending in ',', lineup with the
8467 * lowest one, but check for cpp base class
8468 * declaration/initialization, if it is an
8469 * opening brace or we are looking just for
8470 * enumerations/initializations. */
8471 if (terminated == ',')
8472 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008473 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 break;
8475
8476 lookfor = LOOKFOR_CPP_BASECLASS;
8477 continue;
8478 }
8479
8480 /* Ignore unterminated lines in between, but
8481 * reduce indent. */
8482 if (amount > cur_amount)
8483 amount = cur_amount;
8484 }
8485 else
8486 {
8487 /*
8488 * Found first unterminated line on a row, may
8489 * line up with this line, remember its indent
8490 * 100 +
8491 * -> here;
8492 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008493 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008495
8496 n = (int)STRLEN(l);
8497 if (terminated == ',' && (*skipwhite(l) == ']'
8498 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500
8501 /*
8502 * If previous line ends in ',', check whether we
8503 * are in an initialization or enum
8504 * struct xxx =
8505 * {
8506 * sizeof a,
8507 * 124 };
8508 * or a normal possible continuation line.
8509 * but only, of no other statement has been found
8510 * yet.
8511 */
8512 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8513 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008514 if (curbuf->b_ind_js)
8515 {
8516 /* Search for a line ending in a comma
8517 * and line up with the line below it
8518 * (could be the current line).
8519 * some = [
8520 * 1, <- line up here
8521 * 2,
8522 * some = [
8523 * 3 + <- line up here
8524 * 4 *
8525 * 5,
8526 * 6,
8527 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008528 if (cin_iscomment(skipwhite(l)))
8529 break;
8530 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008531 trypos = find_match_char('[',
8532 curbuf->b_ind_maxparen);
8533 if (trypos != NULL)
8534 {
8535 if (trypos->lnum
8536 == curwin->w_cursor.lnum - 1)
8537 {
8538 /* Current line is first inside
8539 * [], line up with it. */
8540 break;
8541 }
8542 ourscope = trypos->lnum;
8543 }
8544 }
8545 else
8546 {
8547 lookfor = LOOKFOR_ENUM_OR_INIT;
8548 cont_amount = cin_first_id_amount();
8549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 }
8551 else
8552 {
8553 if (lookfor == LOOKFOR_INITIAL
8554 && *l != NUL
8555 && l[STRLEN(l) - 1] == '\\')
8556 /* XXX */
8557 cont_amount = cin_get_equal_amount(
8558 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008559 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008560 && lookfor != LOOKFOR_JS_KEY
8561 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 lookfor = LOOKFOR_UNTERM;
8563 }
8564 }
8565 }
8566 }
8567
8568 /*
8569 * Check if we are after a while (cond);
8570 * If so: Ignore until the matching "do".
8571 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008572 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {
8574 /*
8575 * Found an unterminated line after a while ();, line up
8576 * with the last one.
8577 * while (cond);
8578 * 100 + <- line up with this one
8579 * -> here;
8580 */
8581 if (lookfor == LOOKFOR_UNTERM
8582 || lookfor == LOOKFOR_ENUM_OR_INIT)
8583 {
8584 if (cont_amount > 0)
8585 amount = cont_amount;
8586 else
8587 amount += ind_continuation;
8588 break;
8589 }
8590
8591 if (whilelevel == 0)
8592 {
8593 lookfor = LOOKFOR_TERM;
8594 amount = get_indent(); /* XXX */
8595 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008596 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
8598 ++whilelevel;
8599 }
8600
8601 /*
8602 * We are after a "normal" statement.
8603 * If we had another statement we can stop now and use the
8604 * indent of that other statement.
8605 * Otherwise the indent of the current statement may be used,
8606 * search backwards for the next "normal" statement.
8607 */
8608 else
8609 {
8610 /*
8611 * Skip single break line, if before a switch label. It
8612 * may be lined up with the case label.
8613 */
8614 if (lookfor == LOOKFOR_NOBREAK
8615 && cin_isbreak(skipwhite(ml_get_curline())))
8616 {
8617 lookfor = LOOKFOR_ANY;
8618 continue;
8619 }
8620
8621 /*
8622 * Handle "do {" line.
8623 */
8624 if (whilelevel > 0)
8625 {
8626 l = cin_skipcomment(ml_get_curline());
8627 if (cin_isdo(l))
8628 {
8629 amount = get_indent(); /* XXX */
8630 --whilelevel;
8631 continue;
8632 }
8633 }
8634
8635 /*
8636 * Found a terminated line above an unterminated line. Add
8637 * the amount for a continuation line.
8638 * x = 1;
8639 * y = foo +
8640 * -> here;
8641 * or
8642 * int x = 1;
8643 * int foo,
8644 * -> here;
8645 */
8646 if (lookfor == LOOKFOR_UNTERM
8647 || lookfor == LOOKFOR_ENUM_OR_INIT)
8648 {
8649 if (cont_amount > 0)
8650 amount = cont_amount;
8651 else
8652 amount += ind_continuation;
8653 break;
8654 }
8655
8656 /*
8657 * Found a terminated line above a terminated line or "if"
8658 * etc. line. Use the amount of the line below us.
8659 * x = 1; x = 1;
8660 * if (asdf) y = 2;
8661 * while (asdf) ->here;
8662 * here;
8663 * ->foo;
8664 */
8665 if (lookfor == LOOKFOR_TERM)
8666 {
8667 if (!lookfor_break && whilelevel == 0)
8668 break;
8669 }
8670
8671 /*
8672 * First line above the one we're indenting is terminated.
8673 * To know what needs to be done look further backward for
8674 * a terminated line.
8675 */
8676 else
8677 {
8678 /*
8679 * position the cursor over the rightmost paren, so
8680 * that matching it will take us back to the start of
8681 * the line. Helps for:
8682 * func(asdr,
8683 * asdfasdf);
8684 * here;
8685 */
8686term_again:
8687 l = ml_get_curline();
8688 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008689 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008690 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
8692 /*
8693 * Check if we are on a case label now. This is
8694 * handled above.
8695 * case xx: if ( asdf &&
8696 * asdf)
8697 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008698 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008700 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {
8702 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008703 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 continue;
8705 }
8706 }
8707
8708 /* When aligning with the case statement, don't align
8709 * with a statement after it.
8710 * case 1: { <-- don't use this { position
8711 * stat;
8712 * }
8713 * case 2:
8714 * stat;
8715 * }
8716 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008717 iscase = (curbuf->b_ind_keep_case_label
8718 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 /*
8721 * Get indent and pointer to text for current line,
8722 * ignoring any jump label.
8723 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008724 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725
8726 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008727 amount += curbuf->b_ind_open_extra;
8728 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008729 l = skipwhite(l);
8730 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008731 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8733
8734 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008735 * When a terminated line starts with "else" skip to
8736 * the matching "if":
8737 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008738 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008739 * Need to use the scope of this "else". XXX
8740 * If whilelevel != 0 continue looking for a "do {".
8741 */
8742 if (lookfor == LOOKFOR_TERM
8743 && *l != '}'
8744 && cin_iselse(l)
8745 && whilelevel == 0)
8746 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008747 if ((trypos = find_start_brace()) == NULL
8748 || find_match(LOOKFOR_IF, trypos->lnum)
8749 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008750 break;
8751 continue;
8752 }
8753
8754 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 * If we're at the end of a block, skip to the start of
8756 * that block.
8757 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008758 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008759 if (find_last_paren(l, '{', '}') /* XXX */
8760 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008762 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 /* if not "else {" check for terminated again */
8764 /* but skip block for "} else {" */
8765 l = cin_skipcomment(ml_get_curline());
8766 if (*l == '}' || !cin_iselse(l))
8767 goto term_again;
8768 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008769 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
8771 }
8772 }
8773 }
8774 }
8775 }
8776
8777 /* add extra indent for a comment */
8778 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008779 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008780
8781 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008782 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8783 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008784
8785 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008787
8788 /*
8789 * ok -- we're not inside any sort of structure at all!
8790 *
8791 * This means we're at the top level, and everything should
8792 * basically just match where the previous line is, except
8793 * for the lines immediately following a function declaration,
8794 * which are K&R-style parameters and need to be indented.
8795 *
8796 * if our line starts with an open brace, forget about any
8797 * prevailing indent and make sure it looks like the start
8798 * of a function
8799 */
8800
8801 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008803 amount = curbuf->b_ind_first_open;
8804 goto theend;
8805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008807 /*
8808 * If the NEXT line is a function declaration, the current
8809 * line needs to be indented as a function type spec.
8810 * Don't do this if the current line looks like a comment or if the
8811 * current line is terminated, ie. ends in ';', or if the current line
8812 * contains { or }: "void f() {\n if (1)"
8813 */
8814 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8815 && !cin_nocode(theline)
8816 && vim_strchr(theline, '{') == NULL
8817 && vim_strchr(theline, '}') == NULL
8818 && !cin_ends_in(theline, (char_u *)":", NULL)
8819 && !cin_ends_in(theline, (char_u *)",", NULL)
8820 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8821 cur_curpos.lnum + 1)
8822 && !cin_isterminated(theline, FALSE, TRUE))
8823 {
8824 amount = curbuf->b_ind_func_type;
8825 goto theend;
8826 }
8827
8828 /* search backwards until we find something we recognize */
8829 amount = 0;
8830 curwin->w_cursor = cur_curpos;
8831 while (curwin->w_cursor.lnum > 1)
8832 {
8833 curwin->w_cursor.lnum--;
8834 curwin->w_cursor.col = 0;
8835
8836 l = ml_get_curline();
8837
8838 /*
8839 * If we're in a comment or raw string now, skip to the start
8840 * of it.
8841 */ /* XXX */
8842 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008844 curwin->w_cursor.lnum = trypos->lnum + 1;
8845 curwin->w_cursor.col = 0;
8846 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 }
8848
8849 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008850 * Are we at the start of a cpp base class declaration or
8851 * constructor initialization?
8852 */ /* XXX */
8853 n = FALSE;
8854 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008856 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8857 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008859 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008861 /* XXX */
8862 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8863 break;
8864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008866 /*
8867 * Skip preprocessor directives and blank lines.
8868 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008869 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008870 continue;
8871
8872 if (cin_nocode(l))
8873 continue;
8874
8875 /*
8876 * If the previous line ends in ',', use one level of
8877 * indentation:
8878 * int foo,
8879 * bar;
8880 * do this before checking for '}' in case of eg.
8881 * enum foobar
8882 * {
8883 * ...
8884 * } foo,
8885 * bar;
8886 */
8887 n = 0;
8888 if (cin_ends_in(l, (char_u *)",", NULL)
8889 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8890 {
8891 /* take us back to opening paren */
8892 if (find_last_paren(l, '(', ')')
8893 && (trypos = find_match_paren(
8894 curbuf->b_ind_maxparen)) != NULL)
8895 curwin->w_cursor = *trypos;
8896
8897 /* For a line ending in ',' that is a continuation line go
8898 * back to the first line with a backslash:
8899 * char *foo = "bla\
8900 * bla",
8901 * here;
8902 */
8903 while (n == 0 && curwin->w_cursor.lnum > 1)
8904 {
8905 l = ml_get(curwin->w_cursor.lnum - 1);
8906 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8907 break;
8908 --curwin->w_cursor.lnum;
8909 curwin->w_cursor.col = 0;
8910 }
8911
8912 amount = get_indent(); /* XXX */
8913
8914 if (amount == 0)
8915 amount = cin_first_id_amount();
8916 if (amount == 0)
8917 amount = ind_continuation;
8918 break;
8919 }
8920
8921 /*
8922 * If the line looks like a function declaration, and we're
8923 * not in a comment, put it the left margin.
8924 */
8925 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8926 break;
8927 l = ml_get_curline();
8928
8929 /*
8930 * Finding the closing '}' of a previous function. Put
8931 * current line at the left margin. For when 'cino' has "fs".
8932 */
8933 if (*skipwhite(l) == '}')
8934 break;
8935
8936 /* (matching {)
8937 * If the previous line ends on '};' (maybe followed by
8938 * comments) align at column 0. For example:
8939 * char *string_array[] = { "foo",
8940 * / * x * / "b};ar" }; / * foobar * /
8941 */
8942 if (cin_ends_in(l, (char_u *)"};", NULL))
8943 break;
8944
8945 /*
8946 * If the previous line ends on '[' we are probably in an
8947 * array constant:
8948 * something = [
8949 * 234, <- extra indent
8950 */
8951 if (cin_ends_in(l, (char_u *)"[", NULL))
8952 {
8953 amount = get_indent() + ind_continuation;
8954 break;
8955 }
8956
8957 /*
8958 * Find a line only has a semicolon that belongs to a previous
8959 * line ending in '}', e.g. before an #endif. Don't increase
8960 * indent then.
8961 */
8962 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8963 {
8964 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
8966 while (curwin->w_cursor.lnum > 1)
8967 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008968 look = ml_get(--curwin->w_cursor.lnum);
8969 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008970 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008972 }
8973 if (curwin->w_cursor.lnum > 0
8974 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008977 curwin->w_cursor = curpos_save;
8978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008980 /*
8981 * If the PREVIOUS line is a function declaration, the current
8982 * line (and the ones that follow) needs to be indented as
8983 * parameters.
8984 */
8985 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8986 {
8987 amount = curbuf->b_ind_param;
8988 break;
8989 }
8990
8991 /*
8992 * If the previous line ends in ';' and the line before the
8993 * previous line ends in ',' or '\', ident to column zero:
8994 * int foo,
8995 * bar;
8996 * indent_to_0 here;
8997 */
8998 if (cin_ends_in(l, (char_u *)";", NULL))
8999 {
9000 l = ml_get(curwin->w_cursor.lnum - 1);
9001 if (cin_ends_in(l, (char_u *)",", NULL)
9002 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9003 break;
9004 l = ml_get_curline();
9005 }
9006
9007 /*
9008 * Doesn't look like anything interesting -- so just
9009 * use the indent of this line.
9010 *
9011 * Position the cursor over the rightmost paren, so that
9012 * matching it will take us back to the start of the line.
9013 */
9014 find_last_paren(l, '(', ')');
9015
9016 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9017 curwin->w_cursor = *trypos;
9018 amount = get_indent(); /* XXX */
9019 break;
9020 }
9021
9022 /* add extra indent for a comment */
9023 if (cin_iscomment(theline))
9024 amount += curbuf->b_ind_comment;
9025
9026 /* add extra indent if the previous line ended in a backslash:
9027 * "asdfasdf\
9028 * here";
9029 * char *foo = "asdf\
9030 * here";
9031 */
9032 if (cur_curpos.lnum > 1)
9033 {
9034 l = ml_get(cur_curpos.lnum - 1);
9035 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9036 {
9037 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9038 if (cur_amount > 0)
9039 amount = cur_amount;
9040 else if (cur_amount == 0)
9041 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 }
9043 }
9044
9045theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009046 if (amount < 0)
9047 amount = 0;
9048
9049laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 /* put the cursor back where it belongs */
9051 curwin->w_cursor = cur_curpos;
9052
9053 vim_free(linecopy);
9054
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 return amount;
9056}
9057
9058 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009059find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
9061 char_u *look;
9062 pos_T *theirscope;
9063 char_u *mightbeif;
9064 int elselevel;
9065 int whilelevel;
9066
9067 if (lookfor == LOOKFOR_IF)
9068 {
9069 elselevel = 1;
9070 whilelevel = 0;
9071 }
9072 else
9073 {
9074 elselevel = 0;
9075 whilelevel = 1;
9076 }
9077
9078 curwin->w_cursor.col = 0;
9079
9080 while (curwin->w_cursor.lnum > ourscope + 1)
9081 {
9082 curwin->w_cursor.lnum--;
9083 curwin->w_cursor.col = 0;
9084
9085 look = cin_skipcomment(ml_get_curline());
9086 if (cin_iselse(look)
9087 || cin_isif(look)
9088 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009089 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 {
9091 /*
9092 * if we've gone outside the braces entirely,
9093 * we must be out of scope...
9094 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009095 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 if (theirscope == NULL)
9097 break;
9098
9099 /*
9100 * and if the brace enclosing this is further
9101 * back than the one enclosing the else, we're
9102 * out of luck too.
9103 */
9104 if (theirscope->lnum < ourscope)
9105 break;
9106
9107 /*
9108 * and if they're enclosed in a *deeper* brace,
9109 * then we can ignore it because it's in a
9110 * different scope...
9111 */
9112 if (theirscope->lnum > ourscope)
9113 continue;
9114
9115 /*
9116 * if it was an "else" (that's not an "else if")
9117 * then we need to go back to another if, so
9118 * increment elselevel
9119 */
9120 look = cin_skipcomment(ml_get_curline());
9121 if (cin_iselse(look))
9122 {
9123 mightbeif = cin_skipcomment(look + 4);
9124 if (!cin_isif(mightbeif))
9125 ++elselevel;
9126 continue;
9127 }
9128
9129 /*
9130 * if it was a "while" then we need to go back to
9131 * another "do", so increment whilelevel. XXX
9132 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009133 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
9135 ++whilelevel;
9136 continue;
9137 }
9138
9139 /* If it's an "if" decrement elselevel */
9140 look = cin_skipcomment(ml_get_curline());
9141 if (cin_isif(look))
9142 {
9143 elselevel--;
9144 /*
9145 * When looking for an "if" ignore "while"s that
9146 * get in the way.
9147 */
9148 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9149 whilelevel = 0;
9150 }
9151
9152 /* If it's a "do" decrement whilelevel */
9153 if (cin_isdo(look))
9154 whilelevel--;
9155
9156 /*
9157 * if we've used up all the elses, then
9158 * this must be the if that we want!
9159 * match the indent level of that if.
9160 */
9161 if (elselevel <= 0 && whilelevel <= 0)
9162 {
9163 return OK;
9164 }
9165 }
9166 }
9167 return FAIL;
9168}
9169
9170# if defined(FEAT_EVAL) || defined(PROTO)
9171/*
9172 * Get indent level from 'indentexpr'.
9173 */
9174 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009175get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009178 pos_T save_pos;
9179 colnr_T save_curswant;
9180 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009182 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9183 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009185 /* Save and restore cursor position and curswant, in case it was changed
9186 * via :normal commands */
9187 save_pos = curwin->w_cursor;
9188 save_curswant = curwin->w_curswant;
9189 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009191 if (use_sandbox)
9192 ++sandbox;
9193 ++textlock;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009194 indent = (int)eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009195 if (use_sandbox)
9196 --sandbox;
9197 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198
9199 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9200 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9201 * command. */
9202 save_State = State;
9203 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009204 curwin->w_cursor = save_pos;
9205 curwin->w_curswant = save_curswant;
9206 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 check_cursor();
9208 State = save_State;
9209
9210 /* If there is an error, just keep the current indent. */
9211 if (indent < 0)
9212 indent = get_indent();
9213
9214 return indent;
9215}
9216# endif
9217
9218#endif /* FEAT_CINDENT */
9219
9220#if defined(FEAT_LISP) || defined(PROTO)
9221
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009222static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223
9224 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009225lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227 char_u buf[LSIZE];
9228 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009229 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231 while (*word != NUL)
9232 {
9233 (void)copy_option_part(&word, buf, LSIZE, ",");
9234 len = (int)STRLEN(buf);
9235 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9236 return TRUE;
9237 }
9238 return FALSE;
9239}
9240
9241/*
9242 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9243 * The incompatible newer method is quite a bit better at indenting
9244 * code in lisp-like languages than the traditional one; it's still
9245 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9246 *
9247 * TODO:
9248 * Findmatch() should be adapted for lisp, also to make showmatch
9249 * work correctly: now (v5.3) it seems all C/C++ oriented:
9250 * - it does not recognize the #\( and #\) notations as character literals
9251 * - it doesn't know about comments starting with a semicolon
9252 * - it incorrectly interprets '(' as a character literal
9253 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009254 * Update from Sergey Khorev:
9255 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 */
9257 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009258get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009260 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 int amount;
9262 char_u *that;
9263 colnr_T col;
9264 colnr_T firsttry;
9265 int parencount, quotecount;
9266 int vi_lisp;
9267
9268 /* Set vi_lisp to use the vi-compatible method */
9269 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9270
9271 realpos = curwin->w_cursor;
9272 curwin->w_cursor.col = 0;
9273
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009274 if ((pos = findmatch(NULL, '(')) == NULL)
9275 pos = findmatch(NULL, '[');
9276 else
9277 {
9278 paren = *pos;
9279 pos = findmatch(NULL, '[');
9280 if (pos == NULL || ltp(pos, &paren))
9281 pos = &paren;
9282 }
9283 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 {
9285 /* Extra trick: Take the indent of the first previous non-white
9286 * line that is at the same () level. */
9287 amount = -1;
9288 parencount = 0;
9289
9290 while (--curwin->w_cursor.lnum >= pos->lnum)
9291 {
9292 if (linewhite(curwin->w_cursor.lnum))
9293 continue;
9294 for (that = ml_get_curline(); *that != NUL; ++that)
9295 {
9296 if (*that == ';')
9297 {
9298 while (*(that + 1) != NUL)
9299 ++that;
9300 continue;
9301 }
9302 if (*that == '\\')
9303 {
9304 if (*(that + 1) != NUL)
9305 ++that;
9306 continue;
9307 }
9308 if (*that == '"' && *(that + 1) != NUL)
9309 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009310 while (*++that && *that != '"')
9311 {
9312 /* skipping escaped characters in the string */
9313 if (*that == '\\')
9314 {
9315 if (*++that == NUL)
9316 break;
9317 if (that[1] == NUL)
9318 {
9319 ++that;
9320 break;
9321 }
9322 }
9323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009325 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009327 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 --parencount;
9329 }
9330 if (parencount == 0)
9331 {
9332 amount = get_indent();
9333 break;
9334 }
9335 }
9336
9337 if (amount == -1)
9338 {
9339 curwin->w_cursor.lnum = pos->lnum;
9340 curwin->w_cursor.col = pos->col;
9341 col = pos->col;
9342
9343 that = ml_get_curline();
9344
9345 if (vi_lisp && get_indent() == 0)
9346 amount = 2;
9347 else
9348 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009349 char_u *line = that;
9350
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 amount = 0;
9352 while (*that && col)
9353 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009354 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 col--;
9356 }
9357
9358 /*
9359 * Some keywords require "body" indenting rules (the
9360 * non-standard-lisp ones are Scheme special forms):
9361 *
9362 * (let ((a 1)) instead (let ((a 1))
9363 * (...)) of (...))
9364 */
9365
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009366 if (!vi_lisp && (*that == '(' || *that == '[')
9367 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 amount += 2;
9369 else
9370 {
9371 that++;
9372 amount++;
9373 firsttry = amount;
9374
9375 while (vim_iswhite(*that))
9376 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009377 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 ++that;
9379 }
9380
9381 if (*that && *that != ';') /* not a comment line */
9382 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009383 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009385 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 firsttry++;
9387
9388 parencount = 0;
9389 quotecount = 0;
9390
9391 if (vi_lisp
9392 || (*that != '"'
9393 && *that != '\''
9394 && *that != '#'
9395 && (*that < '0' || *that > '9')))
9396 {
9397 while (*that
9398 && (!vim_iswhite(*that)
9399 || quotecount
9400 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009401 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 && !quotecount
9403 && !parencount
9404 && vi_lisp)))
9405 {
9406 if (*that == '"')
9407 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009408 if ((*that == '(' || *that == '[')
9409 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009411 if ((*that == ')' || *that == ']')
9412 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 --parencount;
9414 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009415 amount += lbr_chartabsize_adv(
9416 line, &that, (colnr_T)amount);
9417 amount += lbr_chartabsize_adv(
9418 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 }
9420 }
9421 while (vim_iswhite(*that))
9422 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009423 amount += lbr_chartabsize(
9424 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 that++;
9426 }
9427 if (!*that || *that == ';')
9428 amount = firsttry;
9429 }
9430 }
9431 }
9432 }
9433 }
9434 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009435 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436
9437 curwin->w_cursor = realpos;
9438
9439 return amount;
9440}
9441#endif /* FEAT_LISP */
9442
9443 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009444prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009446#if defined(SIGHUP) && defined(SIG_IGN)
9447 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9448 * makes Vim exit and then handling SIGHUP causes various reentrance
9449 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009450 signal(SIGHUP, SIG_IGN);
9451#endif
9452
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453#ifdef FEAT_GUI
9454 if (gui.in_use)
9455 {
9456 gui.dying = TRUE;
9457 out_trash(); /* trash any pending output */
9458 }
9459 else
9460#endif
9461 {
9462 windgoto((int)Rows - 1, 0);
9463
9464 /*
9465 * Switch terminal mode back now, so messages end up on the "normal"
9466 * screen (if there are two screens).
9467 */
9468 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009469 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 out_flush();
9471 }
9472}
9473
9474/*
9475 * Preserve files and exit.
9476 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009477 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9478 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 */
9480 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009481preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
9483 buf_T *buf;
9484
9485 prepare_to_exit();
9486
Bram Moolenaar4770d092006-01-12 23:22:24 +00009487 /* Setting this will prevent free() calls. That avoids calling free()
9488 * recursively when free() was invoked with a bad pointer. */
9489 really_exiting = TRUE;
9490
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 out_str(IObuff);
9492 screen_start(); /* don't know where cursor is now */
9493 out_flush();
9494
9495 ml_close_notmod(); /* close all not-modified buffers */
9496
Bram Moolenaar29323592016-07-24 22:04:11 +02009497 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 {
9499 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9500 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009501 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 screen_start(); /* don't know where cursor is now */
9503 out_flush();
9504 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9505 break;
9506 }
9507 }
9508
9509 ml_close_all(FALSE); /* close all memfiles, without deleting */
9510
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009511 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512
9513 getout(1);
9514}
9515
9516/*
9517 * return TRUE if "fname" exists.
9518 */
9519 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009520vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009522 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
9524 if (mch_stat((char *)fname, &st))
9525 return FALSE;
9526 return TRUE;
9527}
9528
9529/*
9530 * Check for CTRL-C pressed, but only once in a while.
9531 * Should be used instead of ui_breakcheck() for functions that check for
9532 * each line in the file. Calling ui_breakcheck() each time takes too much
9533 * time, because it can be a system call.
9534 */
9535
9536#ifndef BREAKCHECK_SKIP
9537# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9538# define BREAKCHECK_SKIP 200
9539# else
9540# define BREAKCHECK_SKIP 32
9541# endif
9542#endif
9543
9544static int breakcheck_count = 0;
9545
9546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009547line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549 if (++breakcheck_count >= BREAKCHECK_SKIP)
9550 {
9551 breakcheck_count = 0;
9552 ui_breakcheck();
9553 }
9554}
9555
9556/*
9557 * Like line_breakcheck() but check 10 times less often.
9558 */
9559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009560fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9563 {
9564 breakcheck_count = 0;
9565 ui_breakcheck();
9566 }
9567}
9568
9569/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009570 * Invoke expand_wildcards() for one pattern.
9571 * Expand items like "%:h" before the expansion.
9572 * Returns OK or FAIL.
9573 */
9574 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009575expand_wildcards_eval(
9576 char_u **pat, /* pointer to input pattern */
9577 int *num_file, /* resulting number of files */
9578 char_u ***file, /* array of resulting files */
9579 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009580{
9581 int ret = FAIL;
9582 char_u *eval_pat = NULL;
9583 char_u *exp_pat = *pat;
9584 char_u *ignored_msg;
9585 int usedlen;
9586
9587 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9588 {
9589 ++emsg_off;
9590 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9591 NULL, &ignored_msg, NULL);
9592 --emsg_off;
9593 if (eval_pat != NULL)
9594 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9595 }
9596
9597 if (exp_pat != NULL)
9598 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9599
9600 if (eval_pat != NULL)
9601 {
9602 vim_free(exp_pat);
9603 vim_free(eval_pat);
9604 }
9605
9606 return ret;
9607}
9608
9609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9611 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009612 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 */
9614 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009615expand_wildcards(
9616 int num_pat, /* number of input patterns */
9617 char_u **pat, /* array of input patterns */
9618 int *num_files, /* resulting number of files */
9619 char_u ***files, /* array of resulting files */
9620 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621{
9622 int retval;
9623 int i, j;
9624 char_u *p;
9625 int non_suf_match; /* number without matching suffix */
9626
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009627 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628
9629 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009630 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 return retval;
9632
9633#ifdef FEAT_WILDIGN
9634 /*
9635 * Remove names that match 'wildignore'.
9636 */
9637 if (*p_wig)
9638 {
9639 char_u *ffname;
9640
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009641 /* check all files in (*files)[] */
9642 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009644 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 if (ffname == NULL) /* out of memory */
9646 break;
9647# ifdef VMS
9648 vms_remove_version(ffname);
9649# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009650 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009652 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009653 vim_free((*files)[i]);
9654 for (j = i; j + 1 < *num_files; ++j)
9655 (*files)[j] = (*files)[j + 1];
9656 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 --i;
9658 }
9659 vim_free(ffname);
9660 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009661
9662 /* If the number of matches is now zero, we fail. */
9663 if (*num_files == 0)
9664 {
9665 vim_free(*files);
9666 *files = NULL;
9667 return FAIL;
9668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 }
9670#endif
9671
9672 /*
9673 * Move the names where 'suffixes' match to the end.
9674 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009675 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
9677 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009678 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009680 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 {
9682 /*
9683 * Move the name without matching suffix to the front
9684 * of the list.
9685 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009686 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009688 (*files)[j] = (*files)[j - 1];
9689 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 }
9691 }
9692 }
9693
9694 return retval;
9695}
9696
9697/*
9698 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9699 */
9700 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009701match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 int fnamelen, setsuflen;
9704 char_u *setsuf;
9705#define MAXSUFLEN 30 /* maximum length of a file suffix */
9706 char_u suf_buf[MAXSUFLEN];
9707
9708 fnamelen = (int)STRLEN(fname);
9709 setsuflen = 0;
9710 for (setsuf = p_su; *setsuf; )
9711 {
9712 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009713 if (setsuflen == 0)
9714 {
9715 char_u *tail = gettail(fname);
9716
9717 /* empty entry: match name without a '.' */
9718 if (vim_strchr(tail, '.') == NULL)
9719 {
9720 setsuflen = 1;
9721 break;
9722 }
9723 }
9724 else
9725 {
9726 if (fnamelen >= setsuflen
9727 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9728 (size_t)setsuflen) == 0)
9729 break;
9730 setsuflen = 0;
9731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 }
9733 return (setsuflen != 0);
9734}
9735
9736#if !defined(NO_EXPANDPATH) || defined(PROTO)
9737
9738# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009739static int vim_backtick(char_u *p);
9740static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741# endif
9742
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009743# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744/*
9745 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9746 * it's shared between these systems.
9747 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009748# if defined(PROTO)
9749# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750# else
9751# ifdef __BORLANDC__
9752# define _cdecl _RTLENTRYF
9753# endif
9754# endif
9755
9756/*
9757 * comparison function for qsort in dos_expandpath()
9758 */
9759 static int _cdecl
9760pstrcmp(const void *a, const void *b)
9761{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009762 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009766 * Recursively expand one path component into all matching files and/or
9767 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 * Return the number of matches found.
9769 * "path" has backslashes before chars that are not to be expanded, starting
9770 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009771 * Return the number of matches found.
9772 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 */
9774 static int
9775dos_expandpath(
9776 garray_T *gap,
9777 char_u *path,
9778 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009779 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009780 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009782 char_u *buf;
9783 char_u *path_end;
9784 char_u *p, *s, *e;
9785 int start_len = gap->ga_len;
9786 char_u *pat;
9787 regmatch_T regmatch;
9788 int starts_with_dot;
9789 int matches;
9790 int len;
9791 int starstar = FALSE;
9792 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 WIN32_FIND_DATA fb;
9794 HANDLE hFind = (HANDLE)0;
9795# ifdef FEAT_MBYTE
9796 WIN32_FIND_DATAW wfb;
9797 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009800 int ok;
9801
9802 /* Expanding "**" may take a long time, check for CTRL-C. */
9803 if (stardepth > 0)
9804 {
9805 ui_breakcheck();
9806 if (got_int)
9807 return 0;
9808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009810 /* Make room for file name. When doing encoding conversion the actual
9811 * length may be quite a bit longer, thus use the maximum possible length. */
9812 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 if (buf == NULL)
9814 return 0;
9815
9816 /*
9817 * Find the first part in the path name that contains a wildcard or a ~1.
9818 * Copy it into buf, including the preceding characters.
9819 */
9820 p = buf;
9821 s = buf;
9822 e = NULL;
9823 path_end = path;
9824 while (*path_end != NUL)
9825 {
9826 /* May ignore a wildcard that has a backslash before it; it will
9827 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9828 if (path_end >= path + wildoff && rem_backslash(path_end))
9829 *p++ = *path_end++;
9830 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9831 {
9832 if (e != NULL)
9833 break;
9834 s = p + 1;
9835 }
9836 else if (path_end >= path + wildoff
9837 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9838 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009839# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 if (has_mbyte)
9841 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009842 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 STRNCPY(p, path_end, len);
9844 p += len;
9845 path_end += len;
9846 }
9847 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 *p++ = *path_end++;
9850 }
9851 e = p;
9852 *e = NUL;
9853
9854 /* now we have one wildcard component between s and e */
9855 /* Remove backslashes between "wildoff" and the start of the wildcard
9856 * component. */
9857 for (p = buf + wildoff; p < s; ++p)
9858 if (rem_backslash(p))
9859 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009860 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 --e;
9862 --s;
9863 }
9864
Bram Moolenaar231334e2005-07-25 20:46:57 +00009865 /* Check for "**" between "s" and "e". */
9866 for (p = s; p < e; ++p)
9867 if (p[0] == '*' && p[1] == '*')
9868 starstar = TRUE;
9869
Bram Moolenaard82103e2016-01-17 17:04:05 +01009870 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9872 if (pat == NULL)
9873 {
9874 vim_free(buf);
9875 return 0;
9876 }
9877
9878 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009879 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009880 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 regmatch.rm_ic = TRUE; /* Always ignore case */
9882 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009883 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009884 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 vim_free(pat);
9886
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009887 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 {
9889 vim_free(buf);
9890 return 0;
9891 }
9892
9893 /* remember the pattern or file name being looked for */
9894 matchname = vim_strsave(s);
9895
Bram Moolenaar231334e2005-07-25 20:46:57 +00009896 /* If "**" is by itself, this is the first time we encounter it and more
9897 * is following then find matches without any directory. */
9898 if (!didstar && stardepth < 100 && starstar && e - s == 2
9899 && *path_end == '/')
9900 {
9901 STRCPY(s, path_end + 1);
9902 ++stardepth;
9903 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9904 --stardepth;
9905 }
9906
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 /* Scan all files in the directory with "dir/ *.*" */
9908 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909# ifdef FEAT_MBYTE
9910 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9911 {
9912 /* The active codepage differs from 'encoding'. Attempt using the
9913 * wide function. If it fails because it is not implemented fall back
9914 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009915 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 if (wn != NULL)
9917 {
9918 hFind = FindFirstFileW(wn, &wfb);
9919 if (hFind == INVALID_HANDLE_VALUE
9920 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9921 {
9922 vim_free(wn);
9923 wn = NULL;
9924 }
9925 }
9926 }
9927
9928 if (wn == NULL)
9929# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009930 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932
9933 while (ok)
9934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935# ifdef FEAT_MBYTE
9936 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009937 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 else
9939# endif
9940 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 /* Ignore entries starting with a dot, unless when asked for. Accept
9942 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01009943 if ((p[0] != '.' || starts_with_dot
9944 || ((flags & EW_DODOT)
9945 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009947 || (regmatch.regprog != NULL
9948 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009949 || ((flags & EW_NOTWILD)
9950 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009954
9955 if (starstar && stardepth < 100)
9956 {
9957 /* For "**" in the pattern first go deeper in the tree to
9958 * find matches. */
9959 STRCPY(buf + len, "/**");
9960 STRCPY(buf + len + 3, path_end);
9961 ++stardepth;
9962 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9963 --stardepth;
9964 }
9965
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 STRCPY(buf + len, path_end);
9967 if (mch_has_exp_wildcard(path_end))
9968 {
9969 /* need to expand another component of the path */
9970 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009971 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 }
9973 else
9974 {
9975 /* no more wildcards, check if there is a match */
9976 /* remove backslashes for the remaining components only */
9977 if (*path_end != 0)
9978 backslash_halve(buf + len + 1);
9979 if (mch_getperm(buf) >= 0) /* add existing file */
9980 addfile(gap, buf, flags);
9981 }
9982 }
9983
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984# ifdef FEAT_MBYTE
9985 if (wn != NULL)
9986 {
9987 vim_free(p);
9988 ok = FindNextFileW(hFind, &wfb);
9989 }
9990 else
9991# endif
9992 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993
9994 /* If no more matches and no match was used, try expanding the name
9995 * itself. Finds the long name of a short filename. */
9996 if (!ok && matchname != NULL && gap->ga_len == start_len)
9997 {
9998 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 FindClose(hFind);
10000# ifdef FEAT_MBYTE
10001 if (wn != NULL)
10002 {
10003 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010004 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 if (wn != NULL)
10006 hFind = FindFirstFileW(wn, &wfb);
10007 }
10008 if (wn == NULL)
10009# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010010 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 vim_free(matchname);
10013 matchname = NULL;
10014 }
10015 }
10016
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 FindClose(hFind);
10018# ifdef FEAT_MBYTE
10019 vim_free(wn);
10020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010022 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 vim_free(matchname);
10024
10025 matches = gap->ga_len - start_len;
10026 if (matches > 0)
10027 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10028 sizeof(char_u *), pstrcmp);
10029 return matches;
10030}
10031
10032 int
10033mch_expandpath(
10034 garray_T *gap,
10035 char_u *path,
10036 int flags) /* EW_* flags */
10037{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010038 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010040# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
Bram Moolenaar231334e2005-07-25 20:46:57 +000010042#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10043 || defined(PROTO)
10044/*
10045 * Unix style wildcard expansion code.
10046 * It's here because it's used both for Unix and Mac.
10047 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010048static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010049
10050 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010051pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010052{
10053 return (pathcmp(*(char **)a, *(char **)b, -1));
10054}
10055
10056/*
10057 * Recursively expand one path component into all matching files and/or
10058 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10059 * "path" has backslashes before chars that are not to be expanded, starting
10060 * at "path + wildoff".
10061 * Return the number of matches found.
10062 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10063 */
10064 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010065unix_expandpath(
10066 garray_T *gap,
10067 char_u *path,
10068 int wildoff,
10069 int flags, /* EW_* flags */
10070 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010071{
10072 char_u *buf;
10073 char_u *path_end;
10074 char_u *p, *s, *e;
10075 int start_len = gap->ga_len;
10076 char_u *pat;
10077 regmatch_T regmatch;
10078 int starts_with_dot;
10079 int matches;
10080 int len;
10081 int starstar = FALSE;
10082 static int stardepth = 0; /* depth for "**" expansion */
10083
10084 DIR *dirp;
10085 struct dirent *dp;
10086
10087 /* Expanding "**" may take a long time, check for CTRL-C. */
10088 if (stardepth > 0)
10089 {
10090 ui_breakcheck();
10091 if (got_int)
10092 return 0;
10093 }
10094
10095 /* make room for file name */
10096 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10097 if (buf == NULL)
10098 return 0;
10099
10100 /*
10101 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010102 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010103 * Copy it into "buf", including the preceding characters.
10104 */
10105 p = buf;
10106 s = buf;
10107 e = NULL;
10108 path_end = path;
10109 while (*path_end != NUL)
10110 {
10111 /* May ignore a wildcard that has a backslash before it; it will
10112 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10113 if (path_end >= path + wildoff && rem_backslash(path_end))
10114 *p++ = *path_end++;
10115 else if (*path_end == '/')
10116 {
10117 if (e != NULL)
10118 break;
10119 s = p + 1;
10120 }
10121 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010122 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010123 || (!p_fic && (flags & EW_ICASE)
10124 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010125 e = p;
10126#ifdef FEAT_MBYTE
10127 if (has_mbyte)
10128 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010129 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010130 STRNCPY(p, path_end, len);
10131 p += len;
10132 path_end += len;
10133 }
10134 else
10135#endif
10136 *p++ = *path_end++;
10137 }
10138 e = p;
10139 *e = NUL;
10140
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010141 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010142 /* Remove backslashes between "wildoff" and the start of the wildcard
10143 * component. */
10144 for (p = buf + wildoff; p < s; ++p)
10145 if (rem_backslash(p))
10146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010147 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010148 --e;
10149 --s;
10150 }
10151
10152 /* Check for "**" between "s" and "e". */
10153 for (p = s; p < e; ++p)
10154 if (p[0] == '*' && p[1] == '*')
10155 starstar = TRUE;
10156
10157 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010158 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010159 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10160 if (pat == NULL)
10161 {
10162 vim_free(buf);
10163 return 0;
10164 }
10165
10166 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010167 if (flags & EW_ICASE)
10168 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10169 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010170 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010171 if (flags & (EW_NOERROR | EW_NOTWILD))
10172 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010173 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010174 if (flags & (EW_NOERROR | EW_NOTWILD))
10175 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010176 vim_free(pat);
10177
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010178 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010179 {
10180 vim_free(buf);
10181 return 0;
10182 }
10183
10184 /* If "**" is by itself, this is the first time we encounter it and more
10185 * is following then find matches without any directory. */
10186 if (!didstar && stardepth < 100 && starstar && e - s == 2
10187 && *path_end == '/')
10188 {
10189 STRCPY(s, path_end + 1);
10190 ++stardepth;
10191 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10192 --stardepth;
10193 }
10194
10195 /* open the directory for scanning */
10196 *s = NUL;
10197 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10198
10199 /* Find all matching entries */
10200 if (dirp != NULL)
10201 {
10202 for (;;)
10203 {
10204 dp = readdir(dirp);
10205 if (dp == NULL)
10206 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010207 if ((dp->d_name[0] != '.' || starts_with_dot
10208 || ((flags & EW_DODOT)
10209 && dp->d_name[1] != NUL
10210 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010211 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10212 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010213 || ((flags & EW_NOTWILD)
10214 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010215 {
10216 STRCPY(s, dp->d_name);
10217 len = STRLEN(buf);
10218
10219 if (starstar && stardepth < 100)
10220 {
10221 /* For "**" in the pattern first go deeper in the tree to
10222 * find matches. */
10223 STRCPY(buf + len, "/**");
10224 STRCPY(buf + len + 3, path_end);
10225 ++stardepth;
10226 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10227 --stardepth;
10228 }
10229
10230 STRCPY(buf + len, path_end);
10231 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10232 {
10233 /* need to expand another component of the path */
10234 /* remove backslashes for the remaining components only */
10235 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10236 }
10237 else
10238 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010239 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010240
Bram Moolenaar231334e2005-07-25 20:46:57 +000010241 /* no more wildcards, check if there is a match */
10242 /* remove backslashes for the remaining components only */
10243 if (*path_end != NUL)
10244 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010245 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010246 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010247 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010248 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010249#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010250 size_t precomp_len = STRLEN(buf)+1;
10251 char_u *precomp_buf =
10252 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010253
Bram Moolenaar231334e2005-07-25 20:46:57 +000010254 if (precomp_buf)
10255 {
10256 mch_memmove(buf, precomp_buf, precomp_len);
10257 vim_free(precomp_buf);
10258 }
10259#endif
10260 addfile(gap, buf, flags);
10261 }
10262 }
10263 }
10264 }
10265
10266 closedir(dirp);
10267 }
10268
10269 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010270 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010271
10272 matches = gap->ga_len - start_len;
10273 if (matches > 0)
10274 qsort(((char_u **)gap->ga_data) + start_len, matches,
10275 sizeof(char_u *), pstrcmp);
10276 return matches;
10277}
10278#endif
10279
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010280#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010281static int find_previous_pathsep(char_u *path, char_u **psep);
10282static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10283static void expand_path_option(char_u *curdir, garray_T *gap);
10284static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10285static void uniquefy_paths(garray_T *gap, char_u *pattern);
10286static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010287
10288/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010289 * Moves "*psep" back to the previous path separator in "path".
10290 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010291 */
10292 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010293find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010294{
10295 /* skip the current separator */
10296 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010297 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010298
10299 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010300 while (*psep > path)
10301 {
10302 if (vim_ispathsep(**psep))
10303 return OK;
10304 mb_ptr_back(path, *psep);
10305 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010306
10307 return FAIL;
10308}
10309
10310/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010311 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10312 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010313 */
10314 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010315is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010316{
10317 int j;
10318 int candidate_len;
10319 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010320 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010321 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010322
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010323 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010324 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010325 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010326 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010327
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010328 candidate_len = (int)STRLEN(maybe_unique);
10329 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010330 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010331 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010332
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010333 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010334 if (fnamecmp(maybe_unique, rival) == 0
10335 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010336 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010337 }
10338
Bram Moolenaar162bd912010-07-28 22:29:10 +020010339 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010340}
10341
10342/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010343 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010344 * paths are expanded to their equivalent fullpath. This includes the "."
10345 * (relative to current buffer directory) and empty path (relative to current
10346 * directory) notations.
10347 *
10348 * TODO: handle upward search (;) and path limiter (**N) notations by
10349 * expanding each into their equivalent path(s).
10350 */
10351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010352expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010353{
10354 char_u *path_option = *curbuf->b_p_path == NUL
10355 ? p_path : curbuf->b_p_path;
10356 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010357 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010358 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010359
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010360 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010361 return;
10362
10363 while (*path_option != NUL)
10364 {
10365 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10366
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010367 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010368 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010369 /* Relative to current buffer:
10370 * "/path/file" + "." -> "/path/"
10371 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010372 if (curbuf->b_ffname == NULL)
10373 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010374 p = gettail(curbuf->b_ffname);
10375 len = (int)(p - curbuf->b_ffname);
10376 if (len + (int)STRLEN(buf) >= MAXPATHL)
10377 continue;
10378 if (buf[1] == NUL)
10379 buf[len] = NUL;
10380 else
10381 STRMOVE(buf + len, buf + 2);
10382 mch_memmove(buf, curbuf->b_ffname, len);
10383 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010384 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010385 else if (buf[0] == NUL)
10386 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010387 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010388 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010389 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010390 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010391 else if (!mch_isFullName(buf))
10392 {
10393 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010394 len = (int)STRLEN(curdir);
10395 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010396 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010397 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010398 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010399 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010400 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010401 }
10402
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010403 if (ga_grow(gap, 1) == FAIL)
10404 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010405
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010406# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010407 /* Avoid the path ending in a backslash, it fails when a comma is
10408 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010409 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010410 if (buf[len - 1] == '\\')
10411 buf[len - 1] = '/';
10412# endif
10413
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010414 p = vim_strsave(buf);
10415 if (p == NULL)
10416 break;
10417 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010418 }
10419
10420 vim_free(buf);
10421}
10422
10423/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010424 * Returns a pointer to the file or directory name in "fname" that matches the
10425 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010426 *
10427 * path: /foo/bar/baz
10428 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010429 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010430 */
10431 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010432get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010433{
10434 int i;
10435 int maxlen = 0;
10436 char_u **path_part = (char_u **)gap->ga_data;
10437 char_u *cutoff = NULL;
10438
10439 for (i = 0; i < gap->ga_len; i++)
10440 {
10441 int j = 0;
10442
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010443 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010444# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010445 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10446#endif
10447 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010448 j++;
10449 if (j > maxlen)
10450 {
10451 maxlen = j;
10452 cutoff = &fname[j];
10453 }
10454 }
10455
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010456 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010457 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010458 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010459 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010460
10461 return cutoff;
10462}
10463
10464/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010465 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10466 * that they are unique with respect to each other while conserving the part
10467 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010468 */
10469 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010470uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010471{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010472 int i;
10473 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010474 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010475 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010476 char_u *pat;
10477 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010478 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010479 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010480 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010481 char_u **in_curdir = NULL;
10482 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010483
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010484 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010485 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010486
10487 /*
10488 * We need to prepend a '*' at the beginning of file_pattern so that the
10489 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010490 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010491 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010492 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010493 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010494 if (file_pattern == NULL)
10495 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010496 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010497 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010498 STRCAT(file_pattern, pattern);
10499 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10500 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010501 if (pat == NULL)
10502 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010503
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010504 regmatch.rm_ic = TRUE; /* always ignore case */
10505 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10506 vim_free(pat);
10507 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010508 return;
10509
Bram Moolenaar162bd912010-07-28 22:29:10 +020010510 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010511 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010512 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010513 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010514
10515 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010516 if (in_curdir == NULL)
10517 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010518
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010519 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010520 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010521 char_u *path = fnames[i];
10522 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010523 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010524 char_u *pathsep_p;
10525 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010526
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010527 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010528 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010529 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010530 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010531 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010532
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010533 /* Shorten the filename while maintaining its uniqueness */
10534 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010535
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010536 /* Don't assume all files can be reached without path when search
10537 * pattern starts with star star slash, so only remove path_cutoff
10538 * when possible. */
10539 if (pattern[0] == '*' && pattern[1] == '*'
10540 && vim_ispathsep_nocolon(pattern[2])
10541 && path_cutoff != NULL
10542 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10543 && is_unique(path_cutoff, gap, i))
10544 {
10545 sort_again = TRUE;
10546 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10547 }
10548 else
10549 {
10550 /* Here all files can be reached without path, so get shortest
10551 * unique path. We start at the end of the path. */
10552 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010553
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010554 while (find_previous_pathsep(path, &pathsep_p))
10555 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10556 && is_unique(pathsep_p + 1, gap, i)
10557 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10558 {
10559 sort_again = TRUE;
10560 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10561 break;
10562 }
10563 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010564
10565 if (mch_isFullName(path))
10566 {
10567 /*
10568 * Last resort: shorten relative to curdir if possible.
10569 * 'possible' means:
10570 * 1. It is under the current directory.
10571 * 2. The result is actually shorter than the original.
10572 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010573 * Before curdir After
10574 * /foo/bar/file.txt /foo/bar ./file.txt
10575 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10576 * /file.txt / /file.txt
10577 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010578 */
10579 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010580 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010581#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010582 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010583 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010584 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010585 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010586 && !vim_ispathsep(*short_name)
10587#endif
10588 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010589 {
10590 STRCPY(path, ".");
10591 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010592 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010593 }
10594 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010595 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010596 }
10597
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010598 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010599 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010600 {
10601 char_u *rel_path;
10602 char_u *path = in_curdir[i];
10603
10604 if (path == NULL)
10605 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010606
10607 /* If the {filename} is not unique, change it to ./{filename}.
10608 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010609 short_name = shorten_fname(path, curdir);
10610 if (short_name == NULL)
10611 short_name = path;
10612 if (is_unique(short_name, gap, i))
10613 {
10614 STRCPY(fnames[i], short_name);
10615 continue;
10616 }
10617
10618 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10619 if (rel_path == NULL)
10620 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010621 STRCPY(rel_path, ".");
10622 add_pathsep(rel_path);
10623 STRCAT(rel_path, short_name);
10624
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010625 vim_free(fnames[i]);
10626 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010627 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010628 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010629 }
10630
Bram Moolenaar162bd912010-07-28 22:29:10 +020010631theend:
10632 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010633 if (in_curdir != NULL)
10634 {
10635 for (i = 0; i < gap->ga_len; i++)
10636 vim_free(in_curdir[i]);
10637 vim_free(in_curdir);
10638 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010640 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010641
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010642 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010643 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644}
10645
10646/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010647 * Calls globpath() with 'path' values for the given pattern and stores the
10648 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010649 * Returns the total number of matches.
10650 */
10651 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010652expand_in_path(
10653 garray_T *gap,
10654 char_u *pattern,
10655 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010656{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010657 char_u *curdir;
10658 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010659 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010660
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010661 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010662 return 0;
10663 mch_dirname(curdir, MAXPATHL);
10664
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010665 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010666 expand_path_option(curdir, &path_ga);
10667 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010668 if (path_ga.ga_len == 0)
10669 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010670
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010671 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010672 ga_clear_strings(&path_ga);
10673 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010674 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010675
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010676 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010677 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010678
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010679 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010680}
10681#endif
10682
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010683#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10684/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010685 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10686 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010687 */
10688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010689remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010690{
10691 int i;
10692 int j;
10693 char_u **fnames = (char_u **)gap->ga_data;
10694
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010695 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010696 for (i = gap->ga_len - 1; i > 0; --i)
10697 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10698 {
10699 vim_free(fnames[i]);
10700 for (j = i + 1; j < gap->ga_len; ++j)
10701 fnames[j - 1] = fnames[j];
10702 --gap->ga_len;
10703 }
10704}
10705#endif
10706
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010707static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010708
10709/*
10710 * Return TRUE if "p" contains what looks like an environment variable.
10711 * Allowing for escaping.
10712 */
10713 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010714has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010715{
10716 for ( ; *p; mb_ptr_adv(p))
10717 {
10718 if (*p == '\\' && p[1] != NUL)
10719 ++p;
10720 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010721#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010722 "$%"
10723#else
10724 "$"
10725#endif
10726 , *p) != NULL)
10727 return TRUE;
10728 }
10729 return FALSE;
10730}
10731
10732#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010733static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010734
10735/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010736 * Return TRUE if "p" contains a special wildcard character, one that Vim
10737 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010738 */
10739 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010740has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010741{
10742 for ( ; *p; mb_ptr_adv(p))
10743 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010744 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010745 if (*p == '\\' && p[1] != NUL)
10746 ++p;
10747 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10748 return TRUE;
10749 }
10750 return FALSE;
10751}
10752#endif
10753
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754/*
10755 * Generic wildcard expansion code.
10756 *
10757 * Characters in "pat" that should not be expanded must be preceded with a
10758 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10759 *
10760 * Return FAIL when no single file was found. In this case "num_file" is not
10761 * set, and "file" may contain an error message.
10762 * Return OK when some files found. "num_file" is set to the number of
10763 * matches, "file" to the array of matches. Call FreeWild() later.
10764 */
10765 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010766gen_expand_wildcards(
10767 int num_pat, /* number of input patterns */
10768 char_u **pat, /* array of input patterns */
10769 int *num_file, /* resulting number of files */
10770 char_u ***file, /* array of resulting files */
10771 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772{
10773 int i;
10774 garray_T ga;
10775 char_u *p;
10776 static int recursive = FALSE;
10777 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010778 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010779#if defined(FEAT_SEARCHPATH)
10780 int did_expand_in_path = FALSE;
10781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782
10783 /*
10784 * expand_env() is called to expand things like "~user". If this fails,
10785 * it calls ExpandOne(), which brings us back here. In this case, always
10786 * call the machine specific expansion function, if possible. Otherwise,
10787 * return FAIL.
10788 */
10789 if (recursive)
10790#ifdef SPECIAL_WILDCHAR
10791 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10792#else
10793 return FAIL;
10794#endif
10795
10796#ifdef SPECIAL_WILDCHAR
10797 /*
10798 * If there are any special wildcard characters which we cannot handle
10799 * here, call machine specific function for all the expansion. This
10800 * avoids starting the shell for each argument separately.
10801 * For `=expr` do use the internal function.
10802 */
10803 for (i = 0; i < num_pat; i++)
10804 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010805 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806# ifdef VIM_BACKTICK
10807 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10808# endif
10809 )
10810 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10811 }
10812#endif
10813
10814 recursive = TRUE;
10815
10816 /*
10817 * The matching file names are stored in a growarray. Init it empty.
10818 */
10819 ga_init2(&ga, (int)sizeof(char_u *), 30);
10820
10821 for (i = 0; i < num_pat; ++i)
10822 {
10823 add_pat = -1;
10824 p = pat[i];
10825
10826#ifdef VIM_BACKTICK
10827 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010830 if (add_pat == -1)
10831 retval = FAIL;
10832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 else
10834#endif
10835 {
10836 /*
10837 * First expand environment variables, "~/" and "~user/".
10838 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010839 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010841 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 if (p == NULL)
10843 p = pat[i];
10844#ifdef UNIX
10845 /*
10846 * On Unix, if expand_env() can't expand an environment
10847 * variable, use the shell to do that. Discard previously
10848 * found file names and start all over again.
10849 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010850 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 {
10852 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010853 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010855 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 recursive = FALSE;
10857 return i;
10858 }
10859#endif
10860 }
10861
10862 /*
10863 * If there are wildcards: Expand file names and add each match to
10864 * the list. If there is no match, and EW_NOTFOUND is given, add
10865 * the pattern.
10866 * If there are no wildcards: Add the file name if it exists or
10867 * when EW_NOTFOUND is given.
10868 */
10869 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010870 {
10871#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010872 if ((flags & EW_PATH)
10873 && !mch_isFullName(p)
10874 && !(p[0] == '.'
10875 && (vim_ispathsep(p[1])
10876 || (p[1] == '.' && vim_ispathsep(p[2]))))
10877 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010878 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010879 /* :find completion where 'path' is used.
10880 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010881 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010882 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010883 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010884 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010885 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010886 else
10887#endif
10888 add_pat = mch_expandpath(&ga, p, flags);
10889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 }
10891
10892 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10893 {
10894 char_u *t = backslash_halve_save(p);
10895
10896#if defined(MACOS_CLASSIC)
10897 slash_to_colon(t);
10898#endif
10899 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10900 * "vim c:/" work. */
10901 if (flags & EW_NOTFOUND)
10902 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020010903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 addfile(&ga, t, flags);
10905 vim_free(t);
10906 }
10907
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010908#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010909 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010910 uniquefy_paths(&ga, p);
10911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (p != pat[i])
10913 vim_free(p);
10914 }
10915
10916 *num_file = ga.ga_len;
10917 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10918
10919 recursive = FALSE;
10920
Bram Moolenaar336bd622016-01-17 18:23:58 +010010921 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922}
10923
10924# ifdef VIM_BACKTICK
10925
10926/*
10927 * Return TRUE if we can expand this backtick thing here.
10928 */
10929 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010930vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
10932 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10933}
10934
10935/*
10936 * Expand an item in `backticks` by executing it as a command.
10937 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010938 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 */
10940 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010941expand_backtick(
10942 garray_T *gap,
10943 char_u *pat,
10944 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
10946 char_u *p;
10947 char_u *cmd;
10948 char_u *buffer;
10949 int cnt = 0;
10950 int i;
10951
10952 /* Create the command: lop off the backticks. */
10953 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10954 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010955 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
10957#ifdef FEAT_EVAL
10958 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010959 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 else
10961#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010962 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010963 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 vim_free(cmd);
10965 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010966 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967
10968 cmd = buffer;
10969 while (*cmd != NUL)
10970 {
10971 cmd = skipwhite(cmd); /* skip over white space */
10972 p = cmd;
10973 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10974 ++p;
10975 /* add an entry if it is not empty */
10976 if (p > cmd)
10977 {
10978 i = *p;
10979 *p = NUL;
10980 addfile(gap, cmd, flags);
10981 *p = i;
10982 ++cnt;
10983 }
10984 cmd = p;
10985 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10986 ++cmd;
10987 }
10988
10989 vim_free(buffer);
10990 return cnt;
10991}
10992# endif /* VIM_BACKTICK */
10993
10994/*
10995 * Add a file to a file list. Accepted flags:
10996 * EW_DIR add directories
10997 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010998 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 * EW_NOTFOUND add even when it doesn't exist
11000 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011001 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 */
11003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011004addfile(
11005 garray_T *gap,
11006 char_u *f, /* filename */
11007 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008{
11009 char_u *p;
11010 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011011 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011013 /* if the file/dir/link doesn't exist, may not add it */
11014 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011015 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 return;
11017
11018#ifdef FNAME_ILLEGAL
11019 /* if the file/dir contains illegal characters, don't add it */
11020 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11021 return;
11022#endif
11023
11024 isdir = mch_isdir(f);
11025 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11026 return;
11027
Bram Moolenaarb5971142015-03-21 17:32:19 +010011028 /* If the file isn't executable, may not add it. Do accept directories.
11029 * When invoked from expand_shellcmd() do not use $PATH. */
11030 if (!isdir && (flags & EW_EXEC)
11031 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011032 return;
11033
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 /* Make room for another item in the file list. */
11035 if (ga_grow(gap, 1) == FAIL)
11036 return;
11037
11038 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11039 if (p == NULL)
11040 return;
11041
11042 STRCPY(p, f);
11043#ifdef BACKSLASH_IN_FILENAME
11044 slash_adjust(p);
11045#endif
11046 /*
11047 * Append a slash or backslash after directory names if none is present.
11048 */
11049#ifndef DONT_ADD_PATHSEP_TO_DIR
11050 if (isdir && (flags & EW_ADDSLASH))
11051 add_pathsep(p);
11052#endif
11053 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054}
11055#endif /* !NO_EXPANDPATH */
11056
11057#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11058
11059#ifndef SEEK_SET
11060# define SEEK_SET 0
11061#endif
11062#ifndef SEEK_END
11063# define SEEK_END 2
11064#endif
11065
11066/*
11067 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011068 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11069 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 * Returns an allocated string, or NULL for error.
11071 */
11072 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011073get_cmd_output(
11074 char_u *cmd,
11075 char_u *infile, /* optional input file name */
11076 int flags, /* can be SHELL_SILENT */
11077 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078{
11079 char_u *tempname;
11080 char_u *command;
11081 char_u *buffer = NULL;
11082 int len;
11083 int i = 0;
11084 FILE *fd;
11085
11086 if (check_restricted() || check_secure())
11087 return NULL;
11088
11089 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011090 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
11092 EMSG(_(e_notmp));
11093 return NULL;
11094 }
11095
11096 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011097 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 if (command == NULL)
11099 goto done;
11100
11101 /*
11102 * Call the shell to execute the command (errors are ignored).
11103 * Don't check timestamps here.
11104 */
11105 ++no_check_timestamps;
11106 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11107 --no_check_timestamps;
11108
11109 vim_free(command);
11110
11111 /*
11112 * read the names from the file into memory
11113 */
11114# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011115 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 fd = mch_fopen((char *)tempname, "r");
11117# else
11118 fd = mch_fopen((char *)tempname, READBIN);
11119# endif
11120
11121 if (fd == NULL)
11122 {
11123 EMSG2(_(e_notopen), tempname);
11124 goto done;
11125 }
11126
11127 fseek(fd, 0L, SEEK_END);
11128 len = ftell(fd); /* get size of temp file */
11129 fseek(fd, 0L, SEEK_SET);
11130
11131 buffer = alloc(len + 1);
11132 if (buffer != NULL)
11133 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11134 fclose(fd);
11135 mch_remove(tempname);
11136 if (buffer == NULL)
11137 goto done;
11138#ifdef VMS
11139 len = i; /* VMS doesn't give us what we asked for... */
11140#endif
11141 if (i != len)
11142 {
11143 EMSG2(_(e_notread), tempname);
11144 vim_free(buffer);
11145 buffer = NULL;
11146 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011147 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011148 {
11149 /* Change NUL into SOH, otherwise the string is truncated. */
11150 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011151 if (buffer[i] == NUL)
11152 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011153
Bram Moolenaar162bd912010-07-28 22:29:10 +020011154 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011155 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011156 else
11157 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158
11159done:
11160 vim_free(tempname);
11161 return buffer;
11162}
11163#endif
11164
11165/*
11166 * Free the list of files returned by expand_wildcards() or other expansion
11167 * functions.
11168 */
11169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011170FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011172 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 while (count--)
11175 vim_free(files[count]);
11176 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011180 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 * Don't do this when still processing a command or a mapping.
11182 * Don't do this when inside a ":normal" command.
11183 */
11184 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011185goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186{
11187 return (p_im && stuff_empty() && typebuf_typed());
11188}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011189
11190/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011191 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011192 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11193 * - Remove any argument. E.g., "csh -f" -> "csh".
11194 * But don't allow a space in the path, so that this works:
11195 * "/usr/bin/csh --rcfile ~/.cshrc"
11196 * But don't do that for Windows, it's common to have a space in the path.
11197 */
11198 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011199get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011200{
11201 char_u *p;
11202
11203#ifdef WIN3264
11204 p = gettail(p_sh);
11205 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11206#else
11207 p = skiptowhite(p_sh);
11208 if (*p == NUL)
11209 {
11210 /* No white space, use the tail. */
11211 p = vim_strsave(gettail(p_sh));
11212 }
11213 else
11214 {
11215 char_u *p1, *p2;
11216
11217 /* Find the last path separator before the space. */
11218 p1 = p_sh;
11219 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11220 if (vim_ispathsep(*p2))
11221 p1 = p2 + 1;
11222 p = vim_strnsave(p1, (int)(p - p1));
11223 }
11224#endif
11225 return p;
11226}