blob: 821869aa3255521117a195e3af8c3907b3924b52 [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 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100142 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {
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. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100205 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 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) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100237 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
Bram Moolenaar1c465442017-03-12 20:10:05 +0100261 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
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 */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100360 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
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 */
Bram Moolenaar02631462017-09-22 15:20:32 +0200498 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200499 - ((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
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200630# ifdef FEAT_EVAL
631 && *curbuf->b_p_inde == NUL
632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 );
634 int no_si = FALSE; /* reset did_si afterwards */
635 int first_char = NUL; /* init for GCC */
636#endif
637#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
638 int vreplace_mode;
639#endif
640 int did_append; /* appended a new line */
641 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
642
643 /*
644 * make a copy of the current line so we can mess with it
645 */
646 saved_line = vim_strsave(ml_get_curline());
647 if (saved_line == NULL) /* out of memory! */
648 return FALSE;
649
650#ifdef FEAT_VREPLACE
651 if (State & VREPLACE_FLAG)
652 {
653 /*
654 * With VREPLACE we make a copy of the next line, which we will be
655 * starting to replace. First make the new line empty and let vim play
656 * with the indenting and comment leader to its heart's content. Then
657 * we grab what it ended up putting on the new line, put back the
658 * original line, and call ins_char() to put each new character onto
659 * the line, replacing what was there before and pushing the right
660 * stuff onto the replace stack. -- webb.
661 */
662 if (curwin->w_cursor.lnum < orig_line_count)
663 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
664 else
665 next_line = vim_strsave((char_u *)"");
666 if (next_line == NULL) /* out of memory! */
667 goto theend;
668
669 /*
670 * In VREPLACE mode, a NL replaces the rest of the line, and starts
671 * replacing the next line, so push all of the characters left on the
672 * line onto the replace stack. We'll push any other characters that
673 * might be replaced at the start of the next line (due to autoindent
674 * etc) a bit later.
675 */
676 replace_push(NUL); /* Call twice because BS over NL expects it */
677 replace_push(NUL);
678 p = saved_line + curwin->w_cursor.col;
679 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000680 {
681#ifdef FEAT_MBYTE
682 if (has_mbyte)
683 p += replace_push_mb(p);
684 else
685#endif
686 replace_push(*p++);
687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 saved_line[curwin->w_cursor.col] = NUL;
689 }
690#endif
691
692 if ((State & INSERT)
693#ifdef FEAT_VREPLACE
694 && !(State & VREPLACE_FLAG)
695#endif
696 )
697 {
698 p_extra = saved_line + curwin->w_cursor.col;
699#ifdef FEAT_SMARTINDENT
700 if (do_si) /* need first char after new line break */
701 {
702 p = skipwhite(p_extra);
703 first_char = *p;
704 }
705#endif
706#ifdef FEAT_COMMENTS
707 extra_len = (int)STRLEN(p_extra);
708#endif
709 saved_char = *p_extra;
710 *p_extra = NUL;
711 }
712
713 u_clearline(); /* cannot do "U" command when adding lines */
714#ifdef FEAT_SMARTINDENT
715 did_si = FALSE;
716#endif
717 ai_col = 0;
718
719 /*
720 * If we just did an auto-indent, then we didn't type anything on
721 * the prior line, and it should be truncated. Do this even if 'ai' is not
722 * set because automatically inserting a comment leader also sets did_ai.
723 */
724 if (dir == FORWARD && did_ai)
725 trunc_line = TRUE;
726
727 /*
728 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
729 * indent to use for the new line.
730 */
731 if (curbuf->b_p_ai
732#ifdef FEAT_SMARTINDENT
733 || do_si
734#endif
735 )
736 {
737 /*
738 * count white space on current line
739 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200740 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200741 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
742 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744#ifdef FEAT_SMARTINDENT
745 /*
746 * Do smart indenting.
747 * In insert/replace mode (only when dir == FORWARD)
748 * we may move some text to the next line. If it starts with '{'
749 * don't add an indent. Fixes inserting a NL before '{' in line
750 * "if (condition) {"
751 */
752 if (!trunc_line && do_si && *saved_line != NUL
753 && (p_extra == NULL || first_char != '{'))
754 {
755 char_u *ptr;
756 char_u last_char;
757
758 old_cursor = curwin->w_cursor;
759 ptr = saved_line;
760# ifdef FEAT_COMMENTS
761 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200762 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 else
764 lead_len = 0;
765# endif
766 if (dir == FORWARD)
767 {
768 /*
769 * Skip preprocessor directives, unless they are
770 * recognised as comments.
771 */
772 if (
773# ifdef FEAT_COMMENTS
774 lead_len == 0 &&
775# endif
776 ptr[0] == '#')
777 {
778 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
779 ptr = ml_get(--curwin->w_cursor.lnum);
780 newindent = get_indent();
781 }
782# ifdef FEAT_COMMENTS
783 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200784 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 else
786 lead_len = 0;
787 if (lead_len > 0)
788 {
789 /*
790 * This case gets the following right:
791 * \*
792 * * A comment (read '\' as '/').
793 * *\
794 * #define IN_THE_WAY
795 * This should line up here;
796 */
797 p = skipwhite(ptr);
798 if (p[0] == '/' && p[1] == '*')
799 p++;
800 if (p[0] == '*')
801 {
802 for (p++; *p; p++)
803 {
804 if (p[0] == '/' && p[-1] == '*')
805 {
806 /*
807 * End of C comment, indent should line up
808 * with the line containing the start of
809 * the comment
810 */
811 curwin->w_cursor.col = (colnr_T)(p - ptr);
812 if ((pos = findmatch(NULL, NUL)) != NULL)
813 {
814 curwin->w_cursor.lnum = pos->lnum;
815 newindent = get_indent();
816 }
817 }
818 }
819 }
820 }
821 else /* Not a comment line */
822# endif
823 {
824 /* Find last non-blank in line */
825 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100826 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 --p;
828 last_char = *p;
829
830 /*
831 * find the character just before the '{' or ';'
832 */
833 if (last_char == '{' || last_char == ';')
834 {
835 if (p > ptr)
836 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100837 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 --p;
839 }
840 /*
841 * Try to catch lines that are split over multiple
842 * lines. eg:
843 * if (condition &&
844 * condition) {
845 * Should line up here!
846 * }
847 */
848 if (*p == ')')
849 {
850 curwin->w_cursor.col = (colnr_T)(p - ptr);
851 if ((pos = findmatch(NULL, '(')) != NULL)
852 {
853 curwin->w_cursor.lnum = pos->lnum;
854 newindent = get_indent();
855 ptr = ml_get_curline();
856 }
857 }
858 /*
859 * If last character is '{' do indent, without
860 * checking for "if" and the like.
861 */
862 if (last_char == '{')
863 {
864 did_si = TRUE; /* do indent */
865 no_si = TRUE; /* don't delete it when '{' typed */
866 }
867 /*
868 * Look for "if" and the like, use 'cinwords'.
869 * Don't do this if the previous line ended in ';' or
870 * '}'.
871 */
872 else if (last_char != ';' && last_char != '}'
873 && cin_is_cinword(ptr))
874 did_si = TRUE;
875 }
876 }
877 else /* dir == BACKWARD */
878 {
879 /*
880 * Skip preprocessor directives, unless they are
881 * recognised as comments.
882 */
883 if (
884# ifdef FEAT_COMMENTS
885 lead_len == 0 &&
886# endif
887 ptr[0] == '#')
888 {
889 int was_backslashed = FALSE;
890
891 while ((ptr[0] == '#' || was_backslashed) &&
892 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
893 {
894 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
895 was_backslashed = TRUE;
896 else
897 was_backslashed = FALSE;
898 ptr = ml_get(++curwin->w_cursor.lnum);
899 }
900 if (was_backslashed)
901 newindent = 0; /* Got to end of file */
902 else
903 newindent = get_indent();
904 }
905 p = skipwhite(ptr);
906 if (*p == '}') /* if line starts with '}': do indent */
907 did_si = TRUE;
908 else /* can delete indent when '{' typed */
909 can_si_back = TRUE;
910 }
911 curwin->w_cursor = old_cursor;
912 }
913 if (do_si)
914 can_si = TRUE;
915#endif /* FEAT_SMARTINDENT */
916
917 did_ai = TRUE;
918 }
919
920#ifdef FEAT_COMMENTS
921 /*
922 * Find out if the current line starts with a comment leader.
923 * This may then be inserted in front of the new line.
924 */
925 end_comment_pending = NUL;
926 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200927 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 lead_len = 0;
930 if (lead_len > 0)
931 {
932 char_u *lead_repl = NULL; /* replaces comment leader */
933 int lead_repl_len = 0; /* length of *lead_repl */
934 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
935 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
936 char_u *comment_end = NULL; /* where lead_end has been found */
937 int extra_space = FALSE; /* append extra space */
938 int current_flag;
939 int require_blank = FALSE; /* requires blank after middle */
940 char_u *p2;
941
942 /*
943 * If the comment leader has the start, middle or end flag, it may not
944 * be used or may be replaced with the middle leader.
945 */
946 for (p = lead_flags; *p && *p != ':'; ++p)
947 {
948 if (*p == COM_BLANK)
949 {
950 require_blank = TRUE;
951 continue;
952 }
953 if (*p == COM_START || *p == COM_MIDDLE)
954 {
955 current_flag = *p;
956 if (*p == COM_START)
957 {
958 /*
959 * Doing "O" on a start of comment does not insert leader.
960 */
961 if (dir == BACKWARD)
962 {
963 lead_len = 0;
964 break;
965 }
966
967 /* find start of middle part */
968 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
969 require_blank = FALSE;
970 }
971
972 /*
973 * Isolate the strings of the middle and end leader.
974 */
975 while (*p && p[-1] != ':') /* find end of middle flags */
976 {
977 if (*p == COM_BLANK)
978 require_blank = TRUE;
979 ++p;
980 }
981 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
982
983 while (*p && p[-1] != ':') /* find end of end flags */
984 {
985 /* Check whether we allow automatic ending of comments */
986 if (*p == COM_AUTO_END)
987 end_comment_pending = -1; /* means we want to set it */
988 ++p;
989 }
990 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
991
992 if (end_comment_pending == -1) /* we can set it now */
993 end_comment_pending = lead_end[n - 1];
994
995 /*
996 * If the end of the comment is in the same line, don't use
997 * the comment leader.
998 */
999 if (dir == FORWARD)
1000 {
1001 for (p = saved_line + lead_len; *p; ++p)
1002 if (STRNCMP(p, lead_end, n) == 0)
1003 {
1004 comment_end = p;
1005 lead_len = 0;
1006 break;
1007 }
1008 }
1009
1010 /*
1011 * Doing "o" on a start of comment inserts the middle leader.
1012 */
1013 if (lead_len > 0)
1014 {
1015 if (current_flag == COM_START)
1016 {
1017 lead_repl = lead_middle;
1018 lead_repl_len = (int)STRLEN(lead_middle);
1019 }
1020
1021 /*
1022 * If we have hit RETURN immediately after the start
1023 * comment leader, then put a space after the middle
1024 * comment leader on the next line.
1025 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001026 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 && ((p_extra != NULL
1028 && (int)curwin->w_cursor.col == lead_len)
1029 || (p_extra == NULL
1030 && saved_line[lead_len] == NUL)
1031 || require_blank))
1032 extra_space = TRUE;
1033 }
1034 break;
1035 }
1036 if (*p == COM_END)
1037 {
1038 /*
1039 * Doing "o" on the end of a comment does not insert leader.
1040 * Remember where the end is, might want to use it to find the
1041 * start (for C-comments).
1042 */
1043 if (dir == FORWARD)
1044 {
1045 comment_end = skipwhite(saved_line);
1046 lead_len = 0;
1047 break;
1048 }
1049
1050 /*
1051 * Doing "O" on the end of a comment inserts the middle leader.
1052 * Find the string for the middle leader, searching backwards.
1053 */
1054 while (p > curbuf->b_p_com && *p != ',')
1055 --p;
1056 for (lead_repl = p; lead_repl > curbuf->b_p_com
1057 && lead_repl[-1] != ':'; --lead_repl)
1058 ;
1059 lead_repl_len = (int)(p - lead_repl);
1060
1061 /* We can probably always add an extra space when doing "O" on
1062 * the comment-end */
1063 extra_space = TRUE;
1064
1065 /* Check whether we allow automatic ending of comments */
1066 for (p2 = p; *p2 && *p2 != ':'; p2++)
1067 {
1068 if (*p2 == COM_AUTO_END)
1069 end_comment_pending = -1; /* means we want to set it */
1070 }
1071 if (end_comment_pending == -1)
1072 {
1073 /* Find last character in end-comment string */
1074 while (*p2 && *p2 != ',')
1075 p2++;
1076 end_comment_pending = p2[-1];
1077 }
1078 break;
1079 }
1080 if (*p == COM_FIRST)
1081 {
1082 /*
1083 * Comment leader for first line only: Don't repeat leader
1084 * when using "O", blank out leader when using "o".
1085 */
1086 if (dir == BACKWARD)
1087 lead_len = 0;
1088 else
1089 {
1090 lead_repl = (char_u *)"";
1091 lead_repl_len = 0;
1092 }
1093 break;
1094 }
1095 }
1096 if (lead_len)
1097 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001098 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001099 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001100 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 allocated = leader; /* remember to free it later */
1102
1103 if (leader == NULL)
1104 lead_len = 0;
1105 else
1106 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001107 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 /*
1110 * Replace leader with lead_repl, right or left adjusted
1111 */
1112 if (lead_repl != NULL)
1113 {
1114 int c = 0;
1115 int off = 0;
1116
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else if (VIM_ISDIGIT(*p) || *p == '-')
1122 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001123 else
1124 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 if (c == COM_RIGHT) /* right adjusted leader */
1127 {
1128 /* find last non-white in the leader to line up with */
1129 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001130 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001133
1134#ifdef FEAT_MBYTE
1135 /* Compute the length of the replaced characters in
1136 * screen characters, not bytes. */
1137 {
1138 int repl_size = vim_strnsize(lead_repl,
1139 lead_repl_len);
1140 int old_size = 0;
1141 char_u *endp = p;
1142 int l;
1143
1144 while (old_size < repl_size && p > leader)
1145 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001146 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 old_size += ptr2cells(p);
1148 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001149 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001150 if (l != 0)
1151 mch_memmove(endp + l, endp,
1152 (size_t)((leader + lead_len) - endp));
1153 lead_len += l;
1154 }
1155#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (p < leader + lead_repl_len)
1157 p = leader;
1158 else
1159 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1162 if (p + lead_repl_len > leader + lead_len)
1163 p[lead_repl_len] = NUL;
1164
1165 /* blank-out any other chars from the old leader. */
1166 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001167 {
1168#ifdef FEAT_MBYTE
1169 int l = mb_head_off(leader, p);
1170
1171 if (l > 1)
1172 {
1173 p -= l;
1174 if (ptr2cells(p) > 1)
1175 {
1176 p[1] = ' ';
1177 --l;
1178 }
1179 mch_memmove(p + 1, p + l + 1,
1180 (size_t)((leader + lead_len) - (p + l + 1)));
1181 lead_len -= l;
1182 *p = ' ';
1183 }
1184 else
1185#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001186 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else /* left adjusted leader */
1191 {
1192 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001193#ifdef FEAT_MBYTE
1194 /* Compute the length of the replaced characters in
1195 * screen characters, not bytes. Move the part that is
1196 * not to be overwritten. */
1197 {
1198 int repl_size = vim_strnsize(lead_repl,
1199 lead_repl_len);
1200 int i;
1201 int l;
1202
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001203 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001204 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001205 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001206 if (vim_strnsize(p, i + l) > repl_size)
1207 break;
1208 }
1209 if (i != lead_repl_len)
1210 {
1211 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001212 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001213 lead_len += lead_repl_len - i;
1214 }
1215 }
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1218
1219 /* Replace any remaining non-white chars in the old
1220 * leader by spaces. Keep Tabs, the indent must
1221 * remain the same. */
1222 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001223 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
1225 /* Don't put a space before a TAB. */
1226 if (p + 1 < leader + lead_len && p[1] == TAB)
1227 {
1228 --lead_len;
1229 mch_memmove(p, p + 1,
1230 (leader + lead_len) - p);
1231 }
1232 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233 {
1234#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001235 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001236
1237 if (l > 1)
1238 {
1239 if (ptr2cells(p) > 1)
1240 {
1241 /* Replace a double-wide char with
1242 * two spaces */
1243 --l;
1244 *p++ = ' ';
1245 }
1246 mch_memmove(p + 1, p + l,
1247 (leader + lead_len) - p);
1248 lead_len -= l - 1;
1249 }
1250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 *p = NUL;
1255 }
1256
1257 /* Recompute the indent, it may have changed. */
1258 if (curbuf->b_p_ai
1259#ifdef FEAT_SMARTINDENT
1260 || do_si
1261#endif
1262 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001263 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 /* Add the indent offset */
1266 if (newindent + off < 0)
1267 {
1268 off = -newindent;
1269 newindent = 0;
1270 }
1271 else
1272 newindent += off;
1273
1274 /* Correct trailing spaces for the shift, so that
1275 * alignment remains equal. */
1276 while (off > 0 && lead_len > 0
1277 && leader[lead_len - 1] == ' ')
1278 {
1279 /* Don't do it when there is a tab before the space */
1280 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1281 break;
1282 --lead_len;
1283 --off;
1284 }
1285
1286 /* If the leader ends in white space, don't add an
1287 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001288 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 extra_space = FALSE;
1290 leader[lead_len] = NUL;
1291 }
1292
1293 if (extra_space)
1294 {
1295 leader[lead_len++] = ' ';
1296 leader[lead_len] = NUL;
1297 }
1298
1299 newcol = lead_len;
1300
1301 /*
1302 * if a new indent will be set below, remove the indent that
1303 * is in the comment leader
1304 */
1305 if (newindent
1306#ifdef FEAT_SMARTINDENT
1307 || did_si
1308#endif
1309 )
1310 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001311 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 --lead_len;
1314 --newcol;
1315 ++leader;
1316 }
1317 }
1318
1319 }
1320#ifdef FEAT_SMARTINDENT
1321 did_si = can_si = FALSE;
1322#endif
1323 }
1324 else if (comment_end != NULL)
1325 {
1326 /*
1327 * We have finished a comment, so we don't use the leader.
1328 * If this was a C-comment and 'ai' or 'si' is set do a normal
1329 * indent to align with the line containing the start of the
1330 * comment.
1331 */
1332 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1333 (curbuf->b_p_ai
1334#ifdef FEAT_SMARTINDENT
1335 || do_si
1336#endif
1337 ))
1338 {
1339 old_cursor = curwin->w_cursor;
1340 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1341 if ((pos = findmatch(NULL, NUL)) != NULL)
1342 {
1343 curwin->w_cursor.lnum = pos->lnum;
1344 newindent = get_indent();
1345 }
1346 curwin->w_cursor = old_cursor;
1347 }
1348 }
1349 }
1350#endif
1351
1352 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1353 if (p_extra != NULL)
1354 {
1355 *p_extra = saved_char; /* restore char that NUL replaced */
1356
1357 /*
1358 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1359 * non-blank.
1360 *
1361 * When in REPLACE mode, put the deleted blanks on the replace stack,
1362 * preceded by a NUL, so they can be put back when a BS is entered.
1363 */
1364 if (REPLACE_NORMAL(State))
1365 replace_push(NUL); /* end of extra blanks */
1366 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1367 {
1368 while ((*p_extra == ' ' || *p_extra == '\t')
1369#ifdef FEAT_MBYTE
1370 && (!enc_utf8
1371 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1372#endif
1373 )
1374 {
1375 if (REPLACE_NORMAL(State))
1376 replace_push(*p_extra);
1377 ++p_extra;
1378 ++less_cols_off;
1379 }
1380 }
1381 if (*p_extra != NUL)
1382 did_ai = FALSE; /* append some text, don't truncate now */
1383
1384 /* columns for marks adjusted for removed columns */
1385 less_cols = (int)(p_extra - saved_line);
1386 }
1387
1388 if (p_extra == NULL)
1389 p_extra = (char_u *)""; /* append empty line */
1390
1391#ifdef FEAT_COMMENTS
1392 /* concatenate leader and p_extra, if there is a leader */
1393 if (lead_len)
1394 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001395 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1396 {
1397 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001398 int padding = second_line_indent
1399 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001400
1401 /* Here whitespace is inserted after the comment char.
1402 * Below, set_indent(newindent, SIN_INSERT) will insert the
1403 * whitespace needed before the comment char. */
1404 for (i = 0; i < padding; i++)
1405 {
1406 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001407 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001408 newcol++;
1409 }
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 STRCAT(leader, p_extra);
1412 p_extra = leader;
1413 did_ai = TRUE; /* So truncating blanks works with comments */
1414 less_cols -= lead_len;
1415 }
1416 else
1417 end_comment_pending = NUL; /* turns out there was no leader */
1418#endif
1419
1420 old_cursor = curwin->w_cursor;
1421 if (dir == BACKWARD)
1422 --curwin->w_cursor.lnum;
1423#ifdef FEAT_VREPLACE
1424 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1425#endif
1426 {
1427 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1428 == FAIL)
1429 goto theend;
1430 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001431 * with markers.
1432 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001433 * be marks there. But still needed in diff mode. */
1434 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1435#ifdef FEAT_DIFF
1436 || curwin->w_p_diff
1437#endif
1438 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001439 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 did_append = TRUE;
1441 }
1442#ifdef FEAT_VREPLACE
1443 else
1444 {
1445 /*
1446 * In VREPLACE mode we are starting to replace the next line.
1447 */
1448 curwin->w_cursor.lnum++;
1449 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1450 {
1451 /* In case we NL to a new line, BS to the previous one, and NL
1452 * again, we don't want to save the new line for undo twice.
1453 */
1454 (void)u_save_cursor(); /* errors are ignored! */
1455 vr_lines_changed++;
1456 }
1457 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1458 changed_bytes(curwin->w_cursor.lnum, 0);
1459 curwin->w_cursor.lnum--;
1460 did_append = FALSE;
1461 }
1462#endif
1463
1464 if (newindent
1465#ifdef FEAT_SMARTINDENT
1466 || did_si
1467#endif
1468 )
1469 {
1470 ++curwin->w_cursor.lnum;
1471#ifdef FEAT_SMARTINDENT
1472 if (did_si)
1473 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001474 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001477 newindent -= newindent % sw;
1478 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001481 /* Copy the indent */
1482 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 (void)copy_indent(newindent, saved_line);
1485
1486 /*
1487 * Set the 'preserveindent' option so that any further screwing
1488 * with the line doesn't entirely destroy our efforts to preserve
1489 * it. It gets restored at the function end.
1490 */
1491 curbuf->b_p_pi = TRUE;
1492 }
1493 else
1494 (void)set_indent(newindent, SIN_INSERT);
1495 less_cols -= curwin->w_cursor.col;
1496
1497 ai_col = curwin->w_cursor.col;
1498
1499 /*
1500 * In REPLACE mode, for each character in the new indent, there must
1501 * be a NUL on the replace stack, for when it is deleted with BS
1502 */
1503 if (REPLACE_NORMAL(State))
1504 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1505 replace_push(NUL);
1506 newcol += curwin->w_cursor.col;
1507#ifdef FEAT_SMARTINDENT
1508 if (no_si)
1509 did_si = FALSE;
1510#endif
1511 }
1512
1513#ifdef FEAT_COMMENTS
1514 /*
1515 * In REPLACE mode, for each character in the extra leader, there must be
1516 * a NUL on the replace stack, for when it is deleted with BS.
1517 */
1518 if (REPLACE_NORMAL(State))
1519 while (lead_len-- > 0)
1520 replace_push(NUL);
1521#endif
1522
1523 curwin->w_cursor = old_cursor;
1524
1525 if (dir == FORWARD)
1526 {
1527 if (trunc_line || (State & INSERT))
1528 {
1529 /* truncate current line at cursor */
1530 saved_line[curwin->w_cursor.col] = NUL;
1531 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1532 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1533 truncate_spaces(saved_line);
1534 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1535 saved_line = NULL;
1536 if (did_append)
1537 {
1538 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1539 curwin->w_cursor.lnum + 1, 1L);
1540 did_append = FALSE;
1541
1542 /* Move marks after the line break to the new line. */
1543 if (flags & OPENLINE_MARKFIX)
1544 mark_col_adjust(curwin->w_cursor.lnum,
1545 curwin->w_cursor.col + less_cols_off,
1546 1L, (long)-less_cols);
1547 }
1548 else
1549 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1550 }
1551
1552 /*
1553 * Put the cursor on the new line. Careful: the scrollup() above may
1554 * have moved w_cursor, we must use old_cursor.
1555 */
1556 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1557 }
1558 if (did_append)
1559 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1560
1561 curwin->w_cursor.col = newcol;
1562#ifdef FEAT_VIRTUALEDIT
1563 curwin->w_cursor.coladd = 0;
1564#endif
1565
1566#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1567 /*
1568 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1569 * fixthisline() from doing it (via change_indent()) by telling it we're in
1570 * normal INSERT mode.
1571 */
1572 if (State & VREPLACE_FLAG)
1573 {
1574 vreplace_mode = State; /* So we know to put things right later */
1575 State = INSERT;
1576 }
1577 else
1578 vreplace_mode = 0;
1579#endif
1580#ifdef FEAT_LISP
1581 /*
1582 * May do lisp indenting.
1583 */
1584 if (!p_paste
1585# ifdef FEAT_COMMENTS
1586 && leader == NULL
1587# endif
1588 && curbuf->b_p_lisp
1589 && curbuf->b_p_ai)
1590 {
1591 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001592 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594#endif
1595#ifdef FEAT_CINDENT
1596 /*
1597 * May do indenting after opening a new line.
1598 */
1599 if (!p_paste
1600 && (curbuf->b_p_cin
1601# ifdef FEAT_EVAL
1602 || *curbuf->b_p_inde != NUL
1603# endif
1604 )
1605 && in_cinkeys(dir == FORWARD
1606 ? KEY_OPEN_FORW
1607 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1608 {
1609 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001610 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612#endif
1613#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1614 if (vreplace_mode != 0)
1615 State = vreplace_mode;
1616#endif
1617
1618#ifdef FEAT_VREPLACE
1619 /*
1620 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1621 * original line, and inserts the new stuff char by char, pushing old stuff
1622 * onto the replace stack (via ins_char()).
1623 */
1624 if (State & VREPLACE_FLAG)
1625 {
1626 /* Put new line in p_extra */
1627 p_extra = vim_strsave(ml_get_curline());
1628 if (p_extra == NULL)
1629 goto theend;
1630
1631 /* Put back original line */
1632 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1633
1634 /* Insert new stuff into line again */
1635 curwin->w_cursor.col = 0;
1636#ifdef FEAT_VIRTUALEDIT
1637 curwin->w_cursor.coladd = 0;
1638#endif
1639 ins_bytes(p_extra); /* will call changed_bytes() */
1640 vim_free(p_extra);
1641 next_line = NULL;
1642 }
1643#endif
1644
1645 retval = TRUE; /* success! */
1646theend:
1647 curbuf->b_p_pi = saved_pi;
1648 vim_free(saved_line);
1649 vim_free(next_line);
1650 vim_free(allocated);
1651 return retval;
1652}
1653
1654#if defined(FEAT_COMMENTS) || defined(PROTO)
1655/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001656 * get_leader_len() returns the length in bytes of the prefix of the given
1657 * string which introduces a comment. If this string is not a comment then
1658 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * When "flags" is not NULL, it is set to point to the flags of the recognized
1660 * comment leader.
1661 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001662 * If "include_space" is set, include trailing whitespace while calculating the
1663 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 */
1665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001666get_leader_len(
1667 char_u *line,
1668 char_u **flags,
1669 int backward,
1670 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001673 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 int got_com = FALSE;
1675 int found_one;
1676 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1677 char_u *string; /* pointer to comment string */
1678 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001679 int middle_match_len = 0;
1680 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001681 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaar81340392012-06-06 16:12:59 +02001683 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001684 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 ++i;
1686
1687 /*
1688 * Repeat to match several nested comment strings.
1689 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001690 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 /*
1693 * scan through the 'comments' option for a match
1694 */
1695 found_one = FALSE;
1696 for (list = curbuf->b_p_com; *list; )
1697 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001698 /* Get one option part into part_buf[]. Advance "list" to next
1699 * one. Put "string" at start of string. */
1700 if (!got_com && flags != NULL)
1701 *flags = list; /* remember where flags started */
1702 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1704 string = vim_strchr(part_buf, ':');
1705 if (string == NULL) /* missing ':', ignore this part */
1706 continue;
1707 *string++ = NUL; /* isolate flags from string */
1708
Bram Moolenaara4271d52011-05-10 13:38:27 +02001709 /* If we found a middle match previously, use that match when this
1710 * is not a middle or end. */
1711 if (middle_match_len != 0
1712 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1713 && vim_strchr(part_buf, COM_END) == NULL)
1714 break;
1715
1716 /* When we already found a nested comment, only accept further
1717 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1719 continue;
1720
Bram Moolenaara4271d52011-05-10 13:38:27 +02001721 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1723 continue;
1724
Bram Moolenaara4271d52011-05-10 13:38:27 +02001725 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 * When string starts with white space, must have some white space
1727 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001728 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001729 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001731 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001732 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001733 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 ++string;
1735 }
1736 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1737 ;
1738 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001739 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaara4271d52011-05-10 13:38:27 +02001741 /* When 'b' flag used, there must be white space or an
1742 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001744 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 continue;
1746
Bram Moolenaara4271d52011-05-10 13:38:27 +02001747 /* We have found a match, stop searching unless this is a middle
1748 * comment. The middle comment can be a substring of the end
1749 * comment in which case it's better to return the length of the
1750 * end comment and its flags. Thus we keep searching with middle
1751 * and end matches and use an end match if it matches better. */
1752 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1753 {
1754 if (middle_match_len == 0)
1755 {
1756 middle_match_len = j;
1757 saved_flags = prev_list;
1758 }
1759 continue;
1760 }
1761 if (middle_match_len != 0 && j > middle_match_len)
1762 /* Use this match instead of the middle match, since it's a
1763 * longer thus better match. */
1764 middle_match_len = 0;
1765
1766 if (middle_match_len == 0)
1767 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 found_one = TRUE;
1769 break;
1770 }
1771
Bram Moolenaara4271d52011-05-10 13:38:27 +02001772 if (middle_match_len != 0)
1773 {
1774 /* Use the previously found middle match after failing to find a
1775 * match with an end. */
1776 if (!got_com && flags != NULL)
1777 *flags = saved_flags;
1778 i += middle_match_len;
1779 found_one = TRUE;
1780 }
1781
1782 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (!found_one)
1784 break;
1785
Bram Moolenaar81340392012-06-06 16:12:59 +02001786 result = i;
1787
Bram Moolenaara4271d52011-05-10 13:38:27 +02001788 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001789 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 ++i;
1791
Bram Moolenaar81340392012-06-06 16:12:59 +02001792 if (include_space)
1793 result = i;
1794
Bram Moolenaara4271d52011-05-10 13:38:27 +02001795 /* If this comment doesn't nest, stop here. */
1796 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (vim_strchr(part_buf, COM_NEST) == NULL)
1798 break;
1799 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001800 return result;
1801}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001802
Bram Moolenaar81340392012-06-06 16:12:59 +02001803/*
1804 * Return the offset at which the last comment in line starts. If there is no
1805 * comment in the whole line, -1 is returned.
1806 *
1807 * When "flags" is not null, it is set to point to the flags describing the
1808 * recognized comment leader.
1809 */
1810 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001811get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001812{
1813 int result = -1;
1814 int i, j;
1815 int lower_check_bound = 0;
1816 char_u *string;
1817 char_u *com_leader;
1818 char_u *com_flags;
1819 char_u *list;
1820 int found_one;
1821 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1822
1823 /*
1824 * Repeat to match several nested comment strings.
1825 */
1826 i = (int)STRLEN(line);
1827 while (--i >= lower_check_bound)
1828 {
1829 /*
1830 * scan through the 'comments' option for a match
1831 */
1832 found_one = FALSE;
1833 for (list = curbuf->b_p_com; *list; )
1834 {
1835 char_u *flags_save = list;
1836
1837 /*
1838 * Get one option part into part_buf[]. Advance list to next one.
1839 * put string at start of string.
1840 */
1841 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1842 string = vim_strchr(part_buf, ':');
1843 if (string == NULL) /* If everything is fine, this cannot actually
1844 * happen. */
1845 {
1846 continue;
1847 }
1848 *string++ = NUL; /* Isolate flags from string. */
1849 com_leader = string;
1850
1851 /*
1852 * Line contents and string must match.
1853 * When string starts with white space, must have some white space
1854 * (but the amount does not need to match, there might be a mix of
1855 * TABs and spaces).
1856 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001857 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001858 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001859 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001860 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001861 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 ++string;
1863 }
1864 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1865 /* do nothing */;
1866 if (string[j] != NUL)
1867 continue;
1868
1869 /*
1870 * When 'b' flag used, there must be white space or an
1871 * end-of-line after the string in the line.
1872 */
1873 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001874 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02001875 {
1876 continue;
1877 }
1878
1879 /*
1880 * We have found a match, stop searching.
1881 */
1882 found_one = TRUE;
1883
1884 if (flags)
1885 *flags = flags_save;
1886 com_flags = flags_save;
1887
1888 break;
1889 }
1890
1891 if (found_one)
1892 {
1893 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1894 int len1, len2, off;
1895
1896 result = i;
1897 /*
1898 * If this comment nests, continue searching.
1899 */
1900 if (vim_strchr(part_buf, COM_NEST) != NULL)
1901 continue;
1902
1903 lower_check_bound = i;
1904
1905 /* Let's verify whether the comment leader found is a substring
1906 * of other comment leaders. If it is, let's adjust the
1907 * lower_check_bound so that we make sure that we have determined
1908 * the comment leader correctly.
1909 */
1910
Bram Moolenaar1c465442017-03-12 20:10:05 +01001911 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02001912 ++com_leader;
1913 len1 = (int)STRLEN(com_leader);
1914
1915 for (list = curbuf->b_p_com; *list; )
1916 {
1917 char_u *flags_save = list;
1918
1919 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1920 if (flags_save == com_flags)
1921 continue;
1922 string = vim_strchr(part_buf2, ':');
1923 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001924 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 ++string;
1926 len2 = (int)STRLEN(string);
1927 if (len2 == 0)
1928 continue;
1929
1930 /* Now we have to verify whether string ends with a substring
1931 * beginning the com_leader. */
1932 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1933 {
1934 --off;
1935 if (!STRNCMP(string + off, com_leader, len2 - off))
1936 {
1937 if (i - off < lower_check_bound)
1938 lower_check_bound = i - off;
1939 }
1940 }
1941 }
1942 }
1943 }
1944 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945}
1946#endif
1947
1948/*
1949 * Return the number of window lines occupied by buffer line "lnum".
1950 */
1951 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001952plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 return plines_win(curwin, lnum, TRUE);
1955}
1956
1957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001958plines_win(
1959 win_T *wp,
1960 linenr_T lnum,
1961 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963#if defined(FEAT_DIFF) || defined(PROTO)
1964 /* Check for filler lines above this buffer line. When folded the result
1965 * is one line anyway. */
1966 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1967}
1968
1969 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001970plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 return plines_win_nofill(curwin, lnum, TRUE);
1973}
1974
1975 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976plines_win_nofill(
1977 win_T *wp,
1978 linenr_T lnum,
1979 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981#endif
1982 int lines;
1983
1984 if (!wp->w_p_wrap)
1985 return 1;
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (wp->w_width == 0)
1988 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990#ifdef FEAT_FOLDING
1991 /* A folded lines is handled just like an empty line. */
1992 /* NOTE: Caller must handle lines that are MAYBE folded. */
1993 if (lineFolded(wp, lnum) == TRUE)
1994 return 1;
1995#endif
1996
1997 lines = plines_win_nofold(wp, lnum);
1998 if (winheight > 0 && lines > wp->w_height)
1999 return (int)wp->w_height;
2000 return lines;
2001}
2002
2003/*
2004 * Return number of window lines physical line "lnum" will occupy in window
2005 * "wp". Does not care about folding, 'wrap' or 'diff'.
2006 */
2007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 char_u *s;
2011 long col;
2012 int width;
2013
2014 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2015 if (*s == NUL) /* empty line */
2016 return 1;
2017 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2018
2019 /*
2020 * If list mode is on, then the '$' at the end of the line may take up one
2021 * extra column.
2022 */
2023 if (wp->w_p_list && lcs_eol != NUL)
2024 col += 1;
2025
2026 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002027 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002029 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (width <= 0)
2031 return 32000;
2032 if (col <= width)
2033 return 1;
2034 col -= width;
2035 width += win_col_off2(wp);
2036 return (col + (width - 1)) / width + 1;
2037}
2038
2039/*
2040 * Like plines_win(), but only reports the number of physical screen lines
2041 * used from the start of the line to the given column number.
2042 */
2043 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 long col;
2047 char_u *s;
2048 int lines = 0;
2049 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002050 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052#ifdef FEAT_DIFF
2053 /* Check for filler lines above this buffer line. When folded the result
2054 * is one line anyway. */
2055 lines = diff_check_fill(wp, lnum);
2056#endif
2057
2058 if (!wp->w_p_wrap)
2059 return lines + 1;
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (wp->w_width == 0)
2062 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaar597a4222014-06-25 14:39:50 +02002064 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 col = 0;
2067 while (*s != NUL && --column >= 0)
2068 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002069 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002070 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072
2073 /*
2074 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2075 * INSERT mode, then col must be adjusted so that it represents the last
2076 * screen position of the TAB. This only fixes an error when the TAB wraps
2077 * from one screen line to the next (when 'columns' is not a multiple of
2078 * 'ts') -- webb.
2079 */
2080 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002081 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002084 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002086 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002087 if (width <= 0)
2088 return 9999;
2089
2090 lines += 1;
2091 if (col > width)
2092 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2093 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094}
2095
2096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002097plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 int count = 0;
2100
2101 while (first <= last)
2102 {
2103#ifdef FEAT_FOLDING
2104 int x;
2105
2106 /* Check if there are any really folded lines, but also included lines
2107 * that are maybe folded. */
2108 x = foldedCount(wp, first, NULL);
2109 if (x > 0)
2110 {
2111 ++count; /* count 1 for "+-- folded" line */
2112 first += x;
2113 }
2114 else
2115#endif
2116 {
2117#ifdef FEAT_DIFF
2118 if (first == wp->w_topline)
2119 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2120 else
2121#endif
2122 count += plines_win(wp, first, TRUE);
2123 ++first;
2124 }
2125 }
2126 return (count);
2127}
2128
2129#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2130/*
2131 * Insert string "p" at the cursor position. Stops at a NUL byte.
2132 * Handles Replace mode and multi-byte characters.
2133 */
2134 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002135ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
2137 ins_bytes_len(p, (int)STRLEN(p));
2138}
2139#endif
2140
2141#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2142 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2143/*
2144 * Insert string "p" with length "len" at the cursor position.
2145 * Handles Replace mode and multi-byte characters.
2146 */
2147 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002148ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 int i;
2151# ifdef FEAT_MBYTE
2152 int n;
2153
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002154 if (has_mbyte)
2155 for (i = 0; i < len; i += n)
2156 {
2157 if (enc_utf8)
2158 /* avoid reading past p[len] */
2159 n = utfc_ptr2len_len(p + i, len - i);
2160 else
2161 n = (*mb_ptr2len)(p + i);
2162 ins_char_bytes(p + i, n);
2163 }
2164 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002166 for (i = 0; i < len; ++i)
2167 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168}
2169#endif
2170
2171/*
2172 * Insert or replace a single character at the cursor position.
2173 * When in REPLACE or VREPLACE mode, replace any existing character.
2174 * Caller must have prepared for undo.
2175 * For multi-byte characters we get the whole character, the caller must
2176 * convert bytes to a character.
2177 */
2178 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002181 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002182 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002184#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 n = (*mb_char2bytes)(c, buf);
2186
2187 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2188 * Happens for CTRL-Vu9900. */
2189 if (buf[0] == 0)
2190 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002191#else
2192 buf[0] = c;
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 ins_char_bytes(buf, n);
2196}
2197
2198 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002199ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 int newlen; /* nr of bytes inserted */
2203 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2204 char_u *p;
2205 char_u *newp;
2206 char_u *oldp;
2207 int linelen; /* length of old line including NUL */
2208 colnr_T col;
2209 linenr_T lnum = curwin->w_cursor.lnum;
2210 int i;
2211
2212#ifdef FEAT_VIRTUALEDIT
2213 /* Break tabs if needed. */
2214 if (virtual_active() && curwin->w_cursor.coladd > 0)
2215 coladvance_force(getviscol());
2216#endif
2217
2218 col = curwin->w_cursor.col;
2219 oldp = ml_get(lnum);
2220 linelen = (int)STRLEN(oldp) + 1;
2221
2222 /* The lengths default to the values for when not replacing. */
2223 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 if (State & REPLACE_FLAG)
2227 {
2228#ifdef FEAT_VREPLACE
2229 if (State & VREPLACE_FLAG)
2230 {
2231 colnr_T new_vcol = 0; /* init for GCC */
2232 colnr_T vcol;
2233 int old_list;
2234#ifndef FEAT_MBYTE
2235 char_u buf[2];
2236#endif
2237
2238 /*
2239 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2240 * Returns the old value of list, so when finished,
2241 * curwin->w_p_list should be set back to this.
2242 */
2243 old_list = curwin->w_p_list;
2244 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2245 curwin->w_p_list = FALSE;
2246
2247 /*
2248 * In virtual replace mode each character may replace one or more
2249 * characters (zero if it's a TAB). Count the number of bytes to
2250 * be deleted to make room for the new character, counting screen
2251 * cells. May result in adding spaces to fill a gap.
2252 */
2253 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2254#ifndef FEAT_MBYTE
2255 buf[0] = c;
2256 buf[1] = NUL;
2257#endif
2258 new_vcol = vcol + chartabsize(buf, vcol);
2259 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2260 {
2261 vcol += chartabsize(oldp + col + oldlen, vcol);
2262 /* Don't need to remove a TAB that takes us to the right
2263 * position. */
2264 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2265 break;
2266#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002267 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#else
2269 ++oldlen;
2270#endif
2271 /* Deleted a bit too much, insert spaces. */
2272 if (vcol > new_vcol)
2273 newlen += vcol - new_vcol;
2274 }
2275 curwin->w_p_list = old_list;
2276 }
2277 else
2278#endif
2279 if (oldp[col] != NUL)
2280 {
2281 /* normal replace */
2282#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002283 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#else
2285 oldlen = 1;
2286#endif
2287 }
2288
2289
2290 /* Push the replaced bytes onto the replace stack, so that they can be
2291 * put back when BS is used. The bytes of a multi-byte character are
2292 * done the other way around, so that the first byte is popped off
2293 * first (it tells the byte length of the character). */
2294 replace_push(NUL);
2295 for (i = 0; i < oldlen; ++i)
2296 {
2297#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002298 if (has_mbyte)
2299 i += replace_push_mb(oldp + col + i) - 1;
2300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002302 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 }
2305
2306 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2307 if (newp == NULL)
2308 return;
2309
2310 /* Copy bytes before the cursor. */
2311 if (col > 0)
2312 mch_memmove(newp, oldp, (size_t)col);
2313
2314 /* Copy bytes after the changed character(s). */
2315 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002316 if (linelen > col + oldlen)
2317 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 (size_t)(linelen - col - oldlen));
2319
2320 /* Insert or overwrite the new character. */
2321#ifdef FEAT_MBYTE
2322 mch_memmove(p, buf, charlen);
2323 i = charlen;
2324#else
2325 *p = c;
2326 i = 1;
2327#endif
2328
2329 /* Fill with spaces when necessary. */
2330 while (i < newlen)
2331 p[i++] = ' ';
2332
2333 /* Replace the line in the buffer. */
2334 ml_replace(lnum, newp, FALSE);
2335
2336 /* mark the buffer as changed and prepare for displaying */
2337 changed_bytes(lnum, col);
2338
2339 /*
2340 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2341 * show the match for right parens and braces.
2342 */
2343 if (p_sm && (State & INSERT)
2344 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002345#ifdef FEAT_INS_EXPAND
2346 && !ins_compl_active()
2347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002349 {
2350#ifdef FEAT_MBYTE
2351 if (has_mbyte)
2352 showmatch(mb_ptr2char(buf));
2353 else
2354#endif
2355 showmatch(c);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_RIGHTLEFT
2359 if (!p_ri || (State & REPLACE_FLAG))
2360#endif
2361 {
2362 /* Normal insert: move cursor right */
2363#ifdef FEAT_MBYTE
2364 curwin->w_cursor.col += charlen;
2365#else
2366 ++curwin->w_cursor.col;
2367#endif
2368 }
2369 /*
2370 * TODO: should try to update w_row here, to avoid recomputing it later.
2371 */
2372}
2373
2374/*
2375 * Insert a string at the cursor position.
2376 * Note: Does NOT handle Replace mode.
2377 * Caller must have prepared for undo.
2378 */
2379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002380ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char_u *oldp, *newp;
2383 int newlen = (int)STRLEN(s);
2384 int oldlen;
2385 colnr_T col;
2386 linenr_T lnum = curwin->w_cursor.lnum;
2387
2388#ifdef FEAT_VIRTUALEDIT
2389 if (virtual_active() && curwin->w_cursor.coladd > 0)
2390 coladvance_force(getviscol());
2391#endif
2392
2393 col = curwin->w_cursor.col;
2394 oldp = ml_get(lnum);
2395 oldlen = (int)STRLEN(oldp);
2396
2397 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2398 if (newp == NULL)
2399 return;
2400 if (col > 0)
2401 mch_memmove(newp, oldp, (size_t)col);
2402 mch_memmove(newp + col, s, (size_t)newlen);
2403 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2404 ml_replace(lnum, newp, FALSE);
2405 changed_bytes(lnum, col);
2406 curwin->w_cursor.col += newlen;
2407}
2408
2409/*
2410 * Delete one character under the cursor.
2411 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2412 * Caller must have prepared for undo.
2413 *
2414 * return FAIL for failure, OK otherwise
2415 */
2416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419#ifdef FEAT_MBYTE
2420 if (has_mbyte)
2421 {
2422 /* Make sure the cursor is at the start of a character. */
2423 mb_adjust_cursor();
2424 if (*ml_get_cursor() == NUL)
2425 return FAIL;
2426 return del_chars(1L, fixpos);
2427 }
2428#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002429 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
2432#if defined(FEAT_MBYTE) || defined(PROTO)
2433/*
2434 * Like del_bytes(), but delete characters instead of bytes.
2435 */
2436 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002437del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 long bytes = 0;
2440 long i;
2441 char_u *p;
2442 int l;
2443
2444 p = ml_get_cursor();
2445 for (i = 0; i < count && *p != NUL; ++i)
2446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 bytes += l;
2449 p += l;
2450 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002451 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
2453#endif
2454
2455/*
2456 * Delete "count" bytes under the cursor.
2457 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2458 * Caller must have prepared for undo.
2459 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002460 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
2462 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463del_bytes(
2464 long count,
2465 int fixpos_arg,
2466 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 char_u *oldp, *newp;
2469 colnr_T oldlen;
2470 linenr_T lnum = curwin->w_cursor.lnum;
2471 colnr_T col = curwin->w_cursor.col;
2472 int was_alloced;
2473 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002474 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 oldp = ml_get(lnum);
2477 oldlen = (int)STRLEN(oldp);
2478
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002479 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (col >= oldlen)
2481 return FAIL;
2482
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002483 /* If "count" is zero there is nothing to do. */
2484 if (count == 0)
2485 return OK;
2486
2487 /* If "count" is negative the caller must be doing something wrong. */
2488 if (count < 1)
2489 {
2490 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2491 return FAIL;
2492 }
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#ifdef FEAT_MBYTE
2495 /* If 'delcombine' is set and deleting (less than) one character, only
2496 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002497 if (p_deco && use_delcombine && enc_utf8
2498 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002500 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 int n;
2502
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002503 (void)utfc_ptr2char(oldp + col, cc);
2504 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
2506 /* Find the last composing char, there can be several. */
2507 n = col;
2508 do
2509 {
2510 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002511 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 n += count;
2513 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2514 fixpos = 0;
2515 }
2516 }
2517#endif
2518
2519 /*
2520 * When count is too big, reduce it.
2521 */
2522 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2523 if (movelen <= 1)
2524 {
2525 /*
2526 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002527 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2528 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002530 if (col > 0 && fixpos && restart_edit == 0
2531#ifdef FEAT_VIRTUALEDIT
2532 && (ve_flags & VE_ONEMORE) == 0
2533#endif
2534 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 --curwin->w_cursor.col;
2537#ifdef FEAT_VIRTUALEDIT
2538 curwin->w_cursor.coladd = 0;
2539#endif
2540#ifdef FEAT_MBYTE
2541 if (has_mbyte)
2542 curwin->w_cursor.col -=
2543 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2544#endif
2545 }
2546 count = oldlen - col;
2547 movelen = 1;
2548 }
2549
2550 /*
2551 * If the old line has been allocated the deletion can be done in the
2552 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002553 * Can't do this when using Netbeans, because we would need to invoke
2554 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002555 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002558 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002559 was_alloced = FALSE;
2560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002562 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (was_alloced)
2564 newp = oldp; /* use same allocated memory */
2565 else
2566 { /* need to allocate a new line */
2567 newp = alloc((unsigned)(oldlen + 1 - count));
2568 if (newp == NULL)
2569 return FAIL;
2570 mch_memmove(newp, oldp, (size_t)col);
2571 }
2572 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2573 if (!was_alloced)
2574 ml_replace(lnum, newp, FALSE);
2575
2576 /* mark the buffer as changed and prepare for displaying */
2577 changed_bytes(lnum, curwin->w_cursor.col);
2578
2579 return OK;
2580}
2581
2582/*
2583 * Delete from cursor to end of line.
2584 * Caller must have prepared for undo.
2585 *
2586 * return FAIL for failure, OK otherwise
2587 */
2588 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002589truncate_line(
2590 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 char_u *newp;
2593 linenr_T lnum = curwin->w_cursor.lnum;
2594 colnr_T col = curwin->w_cursor.col;
2595
2596 if (col == 0)
2597 newp = vim_strsave((char_u *)"");
2598 else
2599 newp = vim_strnsave(ml_get(lnum), col);
2600
2601 if (newp == NULL)
2602 return FAIL;
2603
2604 ml_replace(lnum, newp, FALSE);
2605
2606 /* mark the buffer as changed and prepare for displaying */
2607 changed_bytes(lnum, curwin->w_cursor.col);
2608
2609 /*
2610 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2611 */
2612 if (fixpos && curwin->w_cursor.col > 0)
2613 --curwin->w_cursor.col;
2614
2615 return OK;
2616}
2617
2618/*
2619 * Delete "nlines" lines at the cursor.
2620 * Saves the lines for undo first if "undo" is TRUE.
2621 */
2622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002623del_lines(
2624 long nlines, /* number of lines to delete */
2625 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002628 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
2630 if (nlines <= 0)
2631 return;
2632
2633 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002634 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return;
2636
2637 for (n = 0; n < nlines; )
2638 {
2639 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2640 break;
2641
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002642 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 ++n;
2644
2645 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002646 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002650 /* Correct the cursor position before calling deleted_lines_mark(), it may
2651 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 curwin->w_cursor.col = 0;
2653 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002654
2655 /* adjust marks, mark the buffer as changed and prepare for displaying */
2656 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657}
2658
2659 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002660gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002662 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002664 /* When searching columns is sometimes put at the end of a line. */
2665 if (pos->col == MAXCOL)
2666 return NUL;
2667 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668#ifdef FEAT_MBYTE
2669 if (has_mbyte)
2670 return (*mb_ptr2char)(ptr);
2671#endif
2672 return (int)*ptr;
2673}
2674
2675 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002676gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678#ifdef FEAT_MBYTE
2679 if (has_mbyte)
2680 return (*mb_ptr2char)(ml_get_cursor());
2681#endif
2682 return (int)*ml_get_cursor();
2683}
2684
2685/*
2686 * Write a character at the current cursor position.
2687 * It is directly written into the block.
2688 */
2689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2693 + curwin->w_cursor.col) = c;
2694}
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696/*
2697 * When extra == 0: Return TRUE if the cursor is before or on the first
2698 * non-blank in the line.
2699 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2700 * the line.
2701 */
2702 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002703inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 char_u *ptr;
2706 colnr_T col;
2707
Bram Moolenaar1c465442017-03-12 20:10:05 +01002708 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 ++ptr;
2710 if (col >= curwin->w_cursor.col + extra)
2711 return TRUE;
2712 else
2713 return FALSE;
2714}
2715
2716/*
2717 * Skip to next part of an option argument: Skip space and comma.
2718 */
2719 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002720skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 if (*p == ',')
2723 ++p;
2724 while (*p == ' ')
2725 ++p;
2726 return p;
2727}
2728
2729/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002730 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 *
2732 * Most often called through changed_bytes() and changed_lines(), which also
2733 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002734 *
2735 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 */
2737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002738changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002741 if (p_imst == IM_ON_THE_SPOT)
2742 {
2743 /* The text of the preediting area is inserted, but this doesn't
2744 * mean a change of the buffer yet. That is delayed until the
2745 * text is committed. (this means preedit becomes empty) */
2746 if (im_is_preediting() && !xim_changed_while_preediting)
2747 return;
2748 xim_changed_while_preediting = FALSE;
2749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750#endif
2751
2752 if (!curbuf->b_changed)
2753 {
2754 int save_msg_scroll = msg_scroll;
2755
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002756 /* Give a warning about changing a read-only file. This may also
2757 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 /* Create a swap file if that is wanted.
2761 * Don't do this for "nofile" and "nowrite" buffer types. */
2762 if (curbuf->b_may_swap
2763#ifdef FEAT_QUICKFIX
2764 && !bt_dontwrite(curbuf)
2765#endif
2766 )
2767 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002768 int save_need_wait_return = need_wait_return;
2769
2770 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 ml_open_file(curbuf);
2772
2773 /* The ml_open_file() can cause an ATTENTION message.
2774 * Wait two seconds, to make sure the user reads this unexpected
2775 * message. Since we could be anywhere, call wait_return() now,
2776 * and don't let the emsg() set msg_scroll. */
2777 if (need_wait_return && emsg_silent == 0)
2778 {
2779 out_flush();
2780 ui_delay(2000L, TRUE);
2781 wait_return(TRUE);
2782 msg_scroll = save_msg_scroll;
2783 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002784 else
2785 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002787 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002789 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002792/*
2793 * Internal part of changed(), no user interaction.
2794 */
2795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002796changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002797{
2798 curbuf->b_changed = TRUE;
2799 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002800 check_status(curbuf);
2801 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002802#ifdef FEAT_TITLE
2803 need_maketitle = TRUE; /* set window title later */
2804#endif
2805}
2806
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002807static void changedOneline(buf_T *buf, linenr_T lnum);
2808static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2809static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811/*
2812 * Changed bytes within a single line for the current buffer.
2813 * - marks the windows on this buffer to be redisplayed
2814 * - marks the buffer changed by calling changed()
2815 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002816 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 */
2818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002819changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002821 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002823
2824#ifdef FEAT_DIFF
2825 /* Diff highlighting in other diff windows may need to be updated too. */
2826 if (curwin->w_p_diff)
2827 {
2828 win_T *wp;
2829 linenr_T wlnum;
2830
Bram Moolenaar29323592016-07-24 22:04:11 +02002831 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002832 if (wp->w_p_diff && wp != curwin)
2833 {
2834 redraw_win_later(wp, VALID);
2835 wlnum = diff_lnum_win(lnum, wp);
2836 if (wlnum > 0)
2837 changedOneline(wp->w_buffer, wlnum);
2838 }
2839 }
2840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841}
2842
2843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002844changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002846 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
2848 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002849 if (lnum < buf->b_mod_top)
2850 buf->b_mod_top = lnum;
2851 else if (lnum >= buf->b_mod_bot)
2852 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
2854 else
2855 {
2856 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002857 buf->b_mod_set = TRUE;
2858 buf->b_mod_top = lnum;
2859 buf->b_mod_bot = lnum + 1;
2860 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862}
2863
2864/*
2865 * Appended "count" lines below line "lnum" in the current buffer.
2866 * Must be called AFTER the change and after mark_adjust().
2867 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2868 */
2869 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002870appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 changed_lines(lnum + 1, 0, lnum + 1, count);
2873}
2874
2875/*
2876 * Like appended_lines(), but adjust marks first.
2877 */
2878 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002879appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002881 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002882 * be marks there. But it's still needed in diff mode. */
2883 if (lnum + count < curbuf->b_ml.ml_line_count
2884#ifdef FEAT_DIFF
2885 || curwin->w_p_diff
2886#endif
2887 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002888 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 changed_lines(lnum + 1, 0, lnum + 1, count);
2890}
2891
2892/*
2893 * Deleted "count" lines at line "lnum" in the current buffer.
2894 * Must be called AFTER the change and after mark_adjust().
2895 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2896 */
2897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002898deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 changed_lines(lnum, 0, lnum + count, -count);
2901}
2902
2903/*
2904 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002905 * Make sure the cursor is on a valid line before calling, a GUI callback may
2906 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 */
2908 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002909deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2912 changed_lines(lnum, 0, lnum + count, -count);
2913}
2914
2915/*
2916 * Changed lines for the current buffer.
2917 * Must be called AFTER the change and after mark_adjust().
2918 * - mark the buffer changed by calling changed()
2919 * - mark the windows on this buffer to be redisplayed
2920 * - invalidate cached values
2921 * "lnum" is the first line that needs displaying, "lnume" the first line
2922 * below the changed lines (BEFORE the change).
2923 * When only inserting lines, "lnum" and "lnume" are equal.
2924 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002925 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
2927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002928changed_lines(
2929 linenr_T lnum, /* first line with change */
2930 colnr_T col, /* column in first line with change */
2931 linenr_T lnume, /* line below last changed line */
2932 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002934 changed_lines_buf(curbuf, lnum, lnume, xtra);
2935
2936#ifdef FEAT_DIFF
2937 if (xtra == 0 && curwin->w_p_diff)
2938 {
2939 /* When the number of lines doesn't change then mark_adjust() isn't
2940 * called and other diff buffers still need to be marked for
2941 * displaying. */
2942 win_T *wp;
2943 linenr_T wlnum;
2944
Bram Moolenaar29323592016-07-24 22:04:11 +02002945 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002946 if (wp->w_p_diff && wp != curwin)
2947 {
2948 redraw_win_later(wp, VALID);
2949 wlnum = diff_lnum_win(lnum, wp);
2950 if (wlnum > 0)
2951 changed_lines_buf(wp->w_buffer, wlnum,
2952 lnume - lnum + wlnum, 0L);
2953 }
2954 }
2955#endif
2956
2957 changed_common(lnum, col, lnume, xtra);
2958}
2959
2960 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002961changed_lines_buf(
2962 buf_T *buf,
2963 linenr_T lnum, /* first line with change */
2964 linenr_T lnume, /* line below last changed line */
2965 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002966{
2967 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
2969 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002970 if (lnum < buf->b_mod_top)
2971 buf->b_mod_top = lnum;
2972 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
2974 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002975 buf->b_mod_bot += xtra;
2976 if (buf->b_mod_bot < lnum)
2977 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002979 if (lnume + xtra > buf->b_mod_bot)
2980 buf->b_mod_bot = lnume + xtra;
2981 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983 else
2984 {
2985 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002986 buf->b_mod_set = TRUE;
2987 buf->b_mod_top = lnum;
2988 buf->b_mod_bot = lnume + xtra;
2989 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991}
2992
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002993/*
2994 * Common code for when a change is was made.
2995 * See changed_lines() for the arguments.
2996 * Careful: may trigger autocommands that reload the buffer.
2997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002999changed_common(
3000 linenr_T lnum,
3001 colnr_T col,
3002 linenr_T lnume,
3003 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003006 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 int i;
3008#ifdef FEAT_JUMPLIST
3009 int cols;
3010 pos_T *p;
3011 int add;
3012#endif
3013
3014 /* mark the buffer as modified */
3015 changed();
3016
3017 /* set the '. mark */
3018 if (!cmdmod.keepjumps)
3019 {
3020 curbuf->b_last_change.lnum = lnum;
3021 curbuf->b_last_change.col = col;
3022
3023#ifdef FEAT_JUMPLIST
3024 /* Create a new entry if a new undo-able change was started or we
3025 * don't have an entry yet. */
3026 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3027 {
3028 if (curbuf->b_changelistlen == 0)
3029 add = TRUE;
3030 else
3031 {
3032 /* Don't create a new entry when the line number is the same
3033 * as the last one and the column is not too far away. Avoids
3034 * creating many entries for typing "xxxxx". */
3035 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3036 if (p->lnum != lnum)
3037 add = TRUE;
3038 else
3039 {
3040 cols = comp_textwidth(FALSE);
3041 if (cols == 0)
3042 cols = 79;
3043 add = (p->col + cols < col || col + cols < p->col);
3044 }
3045 }
3046 if (add)
3047 {
3048 /* This is the first of a new sequence of undo-able changes
3049 * and it's at some distance of the last change. Use a new
3050 * position in the changelist. */
3051 curbuf->b_new_change = FALSE;
3052
3053 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3054 {
3055 /* changelist is full: remove oldest entry */
3056 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3057 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3058 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003059 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 {
3061 /* Correct position in changelist for other windows on
3062 * this buffer. */
3063 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3064 --wp->w_changelistidx;
3065 }
3066 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003067 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
3069 /* For other windows, if the position in the changelist is
3070 * at the end it stays at the end. */
3071 if (wp->w_buffer == curbuf
3072 && wp->w_changelistidx == curbuf->b_changelistlen)
3073 ++wp->w_changelistidx;
3074 }
3075 ++curbuf->b_changelistlen;
3076 }
3077 }
3078 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3079 curbuf->b_last_change;
3080 /* The current window is always after the last change, so that "g,"
3081 * takes you back to it. */
3082 curwin->w_changelistidx = curbuf->b_changelistlen;
3083#endif
3084 }
3085
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003086 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
3088 if (wp->w_buffer == curbuf)
3089 {
3090 /* Mark this window to be redrawn later. */
3091 if (wp->w_redr_type < VALID)
3092 wp->w_redr_type = VALID;
3093
3094 /* Check if a change in the buffer has invalidated the cached
3095 * values for the cursor. */
3096#ifdef FEAT_FOLDING
3097 /*
3098 * Update the folds for this window. Can't postpone this, because
3099 * a following operator might work on the whole fold: ">>dd".
3100 */
3101 foldUpdate(wp, lnum, lnume + xtra - 1);
3102
3103 /* The change may cause lines above or below the change to become
3104 * included in a fold. Set lnum/lnume to the first/last line that
3105 * might be displayed differently.
3106 * Set w_cline_folded here as an efficient way to update it when
3107 * inserting lines just above a closed fold. */
3108 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3109 if (wp->w_cursor.lnum == lnum)
3110 wp->w_cline_folded = i;
3111 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3112 if (wp->w_cursor.lnum == lnume)
3113 wp->w_cline_folded = i;
3114
3115 /* If the changed line is in a range of previously folded lines,
3116 * compare with the first line in that range. */
3117 if (wp->w_cursor.lnum <= lnum)
3118 {
3119 i = find_wl_entry(wp, lnum);
3120 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3121 changed_line_abv_curs_win(wp);
3122 }
3123#endif
3124
3125 if (wp->w_cursor.lnum > lnum)
3126 changed_line_abv_curs_win(wp);
3127 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3128 changed_cline_bef_curs_win(wp);
3129 if (wp->w_botline >= lnum)
3130 {
3131 /* Assume that botline doesn't change (inserted lines make
3132 * other lines scroll down below botline). */
3133 approximate_botline_win(wp);
3134 }
3135
3136 /* Check if any w_lines[] entries have become invalid.
3137 * For entries below the change: Correct the lnums for
3138 * inserted/deleted lines. Makes it possible to stop displaying
3139 * after the change. */
3140 for (i = 0; i < wp->w_lines_valid; ++i)
3141 if (wp->w_lines[i].wl_valid)
3142 {
3143 if (wp->w_lines[i].wl_lnum >= lnum)
3144 {
3145 if (wp->w_lines[i].wl_lnum < lnume)
3146 {
3147 /* line included in change */
3148 wp->w_lines[i].wl_valid = FALSE;
3149 }
3150 else if (xtra != 0)
3151 {
3152 /* line below change */
3153 wp->w_lines[i].wl_lnum += xtra;
3154#ifdef FEAT_FOLDING
3155 wp->w_lines[i].wl_lastlnum += xtra;
3156#endif
3157 }
3158 }
3159#ifdef FEAT_FOLDING
3160 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3161 {
3162 /* change somewhere inside this range of folded lines,
3163 * may need to be redrawn */
3164 wp->w_lines[i].wl_valid = FALSE;
3165 }
3166#endif
3167 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003168
3169#ifdef FEAT_FOLDING
3170 /* Take care of side effects for setting w_topline when folds have
3171 * changed. Esp. when the buffer was changed in another window. */
3172 if (hasAnyFolding(wp))
3173 set_topline(wp, wp->w_topline);
3174#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003175 /* relative numbering may require updating more */
3176 if (wp->w_p_rnu)
3177 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179 }
3180
3181 /* Call update_screen() later, which checks out what needs to be redrawn,
3182 * since it notices b_mod_set and then uses b_mod_*. */
3183 if (must_redraw < VALID)
3184 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003185
3186#ifdef FEAT_AUTOCMD
3187 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003188 if (lnum <= curwin->w_cursor.lnum
3189 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003190 last_cursormoved.lnum = 0;
3191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192}
3193
3194/*
3195 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3196 */
3197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003198unchanged(
3199 buf_T *buf,
3200 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003202 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
3204 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003205 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (ff)
3207 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003209 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#ifdef FEAT_TITLE
3211 need_maketitle = TRUE; /* set window title later */
3212#endif
3213 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003214 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#ifdef FEAT_NETBEANS_INTG
3216 netbeans_unmodified(buf);
3217#endif
3218}
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220/*
3221 * check_status: called when the status bars for the buffer 'buf'
3222 * need to be updated
3223 */
3224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003225check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 win_T *wp;
3228
Bram Moolenaar29323592016-07-24 22:04:11 +02003229 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 if (wp->w_buffer == buf && wp->w_status_height)
3231 {
3232 wp->w_redr_status = TRUE;
3233 if (must_redraw < VALID)
3234 must_redraw = VALID;
3235 }
3236}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238/*
3239 * If the file is readonly, give a warning message with the first change.
3240 * Don't do this for autocommands.
3241 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003242 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003244 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 */
3246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003247change_warning(
3248 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 mode and 'showmode' is on */
3250{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003251 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3252
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (curbuf->b_did_warn == FALSE
3254 && curbufIsChanged() == 0
3255#ifdef FEAT_AUTOCMD
3256 && !autocmd_busy
3257#endif
3258 && curbuf->b_p_ro)
3259 {
3260#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003261 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003263 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (!curbuf->b_p_ro)
3265 return;
3266#endif
3267 /*
3268 * Do what msg() does, but with a column offset if the warning should
3269 * be after the mode message.
3270 */
3271 msg_start();
3272 if (msg_row == Rows - 1)
3273 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003274 msg_source(HL_ATTR(HLF_W));
3275 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003276#ifdef FEAT_EVAL
3277 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 msg_clr_eos();
3280 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003281 if (msg_silent == 0 && !silent_mode
3282#ifdef FEAT_EVAL
3283 && time_for_testing != 1
3284#endif
3285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 out_flush();
3288 ui_delay(1000L, TRUE); /* give the user time to think about it */
3289 }
3290 curbuf->b_did_warn = TRUE;
3291 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3292 if (msg_row < Rows - 1)
3293 showmode();
3294 }
3295}
3296
3297/*
3298 * Ask for a reply from the user, a 'y' or a 'n'.
3299 * No other characters are accepted, the message is repeated until a valid
3300 * reply is entered or CTRL-C is hit.
3301 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3302 * from any buffers but directly from the user.
3303 *
3304 * return the 'y' or 'n'
3305 */
3306 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003307ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 int r = ' ';
3310 int save_State = State;
3311
3312 if (exiting) /* put terminal in raw mode for this question */
3313 settmode(TMODE_RAW);
3314 ++no_wait_return;
3315#ifdef USE_ON_FLY_SCROLL
3316 dont_scroll = TRUE; /* disallow scrolling here */
3317#endif
3318 State = CONFIRM; /* mouse behaves like with :confirm */
3319#ifdef FEAT_MOUSE
3320 setmouse(); /* disables mouse for xterm */
3321#endif
3322 ++no_mapping;
3323 ++allow_keys; /* no mapping here, but recognize keys */
3324
3325 while (r != 'y' && r != 'n')
3326 {
3327 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003328 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (direct)
3330 r = get_keystroke();
3331 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003332 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (r == Ctrl_C || r == ESC)
3334 r = 'n';
3335 msg_putchar(r); /* show what you typed */
3336 out_flush();
3337 }
3338 --no_wait_return;
3339 State = save_State;
3340#ifdef FEAT_MOUSE
3341 setmouse();
3342#endif
3343 --no_mapping;
3344 --allow_keys;
3345
3346 return r;
3347}
3348
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003349#if defined(FEAT_MOUSE) || defined(PROTO)
3350/*
3351 * Return TRUE if "c" is a mouse key.
3352 */
3353 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003354is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003355{
3356 return c == K_LEFTMOUSE
3357 || c == K_LEFTMOUSE_NM
3358 || c == K_LEFTDRAG
3359 || c == K_LEFTRELEASE
3360 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003361 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003362 || c == K_MIDDLEMOUSE
3363 || c == K_MIDDLEDRAG
3364 || c == K_MIDDLERELEASE
3365 || c == K_RIGHTMOUSE
3366 || c == K_RIGHTDRAG
3367 || c == K_RIGHTRELEASE
3368 || c == K_MOUSEDOWN
3369 || c == K_MOUSEUP
3370 || c == K_MOUSELEFT
3371 || c == K_MOUSERIGHT
3372 || c == K_X1MOUSE
3373 || c == K_X1DRAG
3374 || c == K_X1RELEASE
3375 || c == K_X2MOUSE
3376 || c == K_X2DRAG
3377 || c == K_X2RELEASE;
3378}
3379#endif
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381/*
3382 * Get a key stroke directly from the user.
3383 * Ignores mouse clicks and scrollbar events, except a click for the left
3384 * button (used at the more prompt).
3385 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3386 * Disadvantage: typeahead is ignored.
3387 * Translates the interrupt character for unix to ESC.
3388 */
3389 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003390get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003392 char_u *buf = NULL;
3393 int buflen = 150;
3394 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 int len = 0;
3396 int n;
3397 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003398 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400 mapped_ctrl_c = FALSE; /* mappings are not used here */
3401 for (;;)
3402 {
3403 cursor_on();
3404 out_flush();
3405
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003406 /* Leave some room for check_termcode() to insert a key code into (max
3407 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3408 * bytes. */
3409 maxlen = (buflen - 6 - len) / 3;
3410 if (buf == NULL)
3411 buf = alloc(buflen);
3412 else if (maxlen < 10)
3413 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003414 char_u *t_buf = buf;
3415
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003416 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003417 * escape sequence. */
3418 buflen += 100;
3419 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003420 if (buf == NULL)
3421 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003422 maxlen = (buflen - 6 - len) / 3;
3423 }
3424 if (buf == NULL)
3425 {
3426 do_outofmem_msg((long_u)buflen);
3427 return ESC; /* panic! */
3428 }
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003431 * terminal code to complete. */
3432 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (n > 0)
3434 {
3435 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003436 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003438 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003440 else if (len > 0)
3441 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar4395a712006-09-05 18:57:57 +00003443 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003444 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003445 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003447
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003448 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003449 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003450 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003451 {
3452 /* Redrawing was postponed, do it now. */
3453 update_screen(0);
3454 setcursor(); /* put cursor back where it belongs */
3455 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003456 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003457 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003458 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003460 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 continue;
3462
3463 /* Handle modifier and/or special key code. */
3464 n = buf[0];
3465 if (n == K_SPECIAL)
3466 {
3467 n = TO_SPECIAL(buf[1], buf[2]);
3468 if (buf[1] == KS_MODIFIER
3469 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003470#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003471 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003472#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003473#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 || n == K_VER_SCROLLBAR
3475 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#endif
3477 )
3478 {
3479 if (buf[1] == KS_MODIFIER)
3480 mod_mask = buf[2];
3481 len -= 3;
3482 if (len > 0)
3483 mch_memmove(buf, buf + 3, (size_t)len);
3484 continue;
3485 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488#ifdef FEAT_MBYTE
3489 if (has_mbyte)
3490 {
3491 if (MB_BYTE2LEN(n) > len)
3492 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003493 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 n = (*mb_ptr2char)(buf);
3495 }
3496#endif
3497#ifdef UNIX
3498 if (n == intr_char)
3499 n = ESC;
3500#endif
3501 break;
3502 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003503 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 mapped_ctrl_c = save_mapped_ctrl_c;
3506 return n;
3507}
3508
3509/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003510 * Get a number from the user.
3511 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 */
3513 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003514get_number(
3515 int colon, /* allow colon to abort */
3516 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 int n = 0;
3519 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003520 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003522 if (mouse_used != NULL)
3523 *mouse_used = FALSE;
3524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 /* When not printing messages, the user won't know what to type, return a
3526 * zero (as if CR was hit). */
3527 if (msg_silent != 0)
3528 return 0;
3529
3530#ifdef USE_ON_FLY_SCROLL
3531 dont_scroll = TRUE; /* disallow scrolling here */
3532#endif
3533 ++no_mapping;
3534 ++allow_keys; /* no mapping here, but recognize keys */
3535 for (;;)
3536 {
3537 windgoto(msg_row, msg_col);
3538 c = safe_vgetc();
3539 if (VIM_ISDIGIT(c))
3540 {
3541 n = n * 10 + c - '0';
3542 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003543 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3546 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003547 if (typed > 0)
3548 {
3549 MSG_PUTS("\b \b");
3550 --typed;
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003554#ifdef FEAT_MOUSE
3555 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3556 {
3557 *mouse_used = TRUE;
3558 n = mouse_row + 1;
3559 break;
3560 }
3561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else if (n == 0 && c == ':' && colon)
3563 {
3564 stuffcharReadbuff(':');
3565 if (!exmode_active)
3566 cmdline_row = msg_row;
3567 skip_redraw = TRUE; /* skip redraw once */
3568 do_redraw = FALSE;
3569 break;
3570 }
3571 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3572 break;
3573 }
3574 --no_mapping;
3575 --allow_keys;
3576 return n;
3577}
3578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003579/*
3580 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003581 * When "mouse_used" is not NULL allow using the mouse and in that case return
3582 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003583 */
3584 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003585prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003586{
3587 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003588 int save_cmdline_row;
3589 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003590
3591 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003592 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003593 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003594 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003595 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003596
Bram Moolenaar203335e2006-09-03 14:35:42 +00003597 /* Set the state such that text can be selected/copied/pasted and we still
3598 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003599 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003600 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003601 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003602 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003603
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003604 i = get_number(TRUE, mouse_used);
3605 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003607 /* don't call wait_return() now */
3608 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 cmdline_row = msg_row - 1;
3610 need_wait_return = FALSE;
3611 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003612 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003613 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003614 else
3615 cmdline_row = save_cmdline_row;
3616 State = save_State;
3617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003618 return i;
3619}
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003622msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
3624 long pn;
3625
3626 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3628 return;
3629
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003630 /* We don't want to overwrite another important message, but do overwrite
3631 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3632 * then "put" reports the last action. */
3633 if (keep_msg != NULL && !keep_msg_more)
3634 return;
3635
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (n > 0)
3637 pn = n;
3638 else
3639 pn = -n;
3640
3641 if (pn > p_report)
3642 {
3643 if (pn == 1)
3644 {
3645 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003646 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3647 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003649 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3650 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652 else
3653 {
3654 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003655 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3656 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003658 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3659 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003662 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (msg(msg_buf))
3664 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003665 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003666 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 }
3669}
3670
3671/*
3672 * flush map and typeahead buffers and give a warning for an error
3673 */
3674 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003675beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 if (emsg_silent == 0)
3678 {
3679 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003680 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682}
3683
3684/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003685 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 */
3687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003688vim_beep(
3689 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 if (emsg_silent == 0)
3692 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003693 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3694 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003695#ifdef ELAPSED_FUNC
3696 static int did_init = FALSE;
3697 static ELAPSED_TYPE start_tv;
3698
3699 /* Only beep once per half a second, otherwise a sequence of beeps
3700 * would freeze Vim. */
3701 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3702 {
3703 did_init = TRUE;
3704 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003706 if (p_vb
3707#ifdef FEAT_GUI
3708 /* While the GUI is starting up the termcap is set for
3709 * the GUI but the output still goes to a terminal. */
3710 && !(gui.in_use && gui.starting)
3711#endif
3712 )
3713 out_str_cf(T_VB);
3714 else
3715 out_char(BELL);
3716#ifdef ELAPSED_FUNC
3717 }
3718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003720
3721 /* When 'verbose' is set and we are sourcing a script or executing a
3722 * function give the user a hint where the beep comes from. */
3723 if (vim_strchr(p_debug, 'e') != NULL)
3724 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003725 msg_source(HL_ATTR(HLF_W));
3726 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729}
3730
3731/*
3732 * To get the "real" home directory:
3733 * - get value of $HOME
3734 * For Unix:
3735 * - go to that directory
3736 * - do mch_dirname() to get the real name of that directory.
3737 * This also works with mounts and links.
3738 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3739 */
3740static char_u *homedir = NULL;
3741
3742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003743init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 char_u *var;
3746
Bram Moolenaar05159a02005-02-26 23:04:13 +00003747 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003748 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750#ifdef VMS
3751 var = mch_getenv((char_u *)"SYS$LOGIN");
3752#else
3753 var = mch_getenv((char_u *)"HOME");
3754#endif
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#ifdef WIN3264
3757 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003758 * Typically, $HOME is not defined on Windows, unless the user has
3759 * specifically defined it for Vim's sake. However, on Windows NT
3760 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3761 * each user. Try constructing $HOME from these.
3762 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003763 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003764 {
3765 char_u *homedrive, *homepath;
3766
3767 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3768 homepath = mch_getenv((char_u *)"HOMEPATH");
3769 if (homepath == NULL || *homepath == NUL)
3770 homepath = (char_u *)"\\";
3771 if (homedrive != NULL
3772 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3773 {
3774 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3775 if (NameBuff[0] != NUL)
3776 var = NameBuff;
3777 }
3778 }
3779
3780 if (var == NULL)
3781 var = mch_getenv((char_u *)"USERPROFILE");
3782
3783 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 * Weird but true: $HOME may contain an indirect reference to another
3785 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3786 * when $HOME is being set.
3787 */
3788 if (var != NULL && *var == '%')
3789 {
3790 char_u *p;
3791 char_u *exp;
3792
3793 p = vim_strchr(var + 1, '%');
3794 if (p != NULL)
3795 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003796 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 exp = mch_getenv(NameBuff);
3798 if (exp != NULL && *exp != NUL
3799 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3800 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003801 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 }
3805 }
3806
Bram Moolenaar48340b62017-08-29 22:08:53 +02003807 if (var != NULL && *var == NUL) /* empty is same as not set */
3808 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
Bram Moolenaar48340b62017-08-29 22:08:53 +02003810# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003811 if (enc_utf8 && var != NULL)
3812 {
3813 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003814 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003815
3816 /* Convert from active codepage to UTF-8. Other conversions are
3817 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003818 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003819 if (pp != NULL)
3820 {
3821 homedir = pp;
3822 return;
3823 }
3824 }
3825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 /*
3828 * Default home dir is C:/
3829 * Best assumption we can make in such a situation.
3830 */
3831 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003832 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (var != NULL)
3836 {
3837#ifdef UNIX
3838 /*
3839 * Change to the directory and get the actual path. This resolves
3840 * links. Don't do it when we can't return.
3841 */
3842 if (mch_dirname(NameBuff, MAXPATHL) == OK
3843 && mch_chdir((char *)NameBuff) == 0)
3844 {
3845 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3846 var = IObuff;
3847 if (mch_chdir((char *)NameBuff) != 0)
3848 EMSG(_(e_prev_dir));
3849 }
3850#endif
3851 homedir = vim_strsave(var);
3852 }
3853}
3854
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003855#if defined(EXITFREE) || defined(PROTO)
3856 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003857free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003858{
3859 vim_free(homedir);
3860}
Bram Moolenaar24305862012-08-15 14:05:05 +02003861
3862# ifdef FEAT_CMDL_COMPL
3863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003864free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003865{
3866 ga_clear_strings(&ga_users);
3867}
3868# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003869#endif
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003872 * Call expand_env() and store the result in an allocated string.
3873 * This is not very memory efficient, this expects the result to be freed
3874 * again soon.
3875 */
3876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003877expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003878{
3879 return expand_env_save_opt(src, FALSE);
3880}
3881
3882/*
3883 * Idem, but when "one" is TRUE handle the string as one file name, only
3884 * expand "~" at the start.
3885 */
3886 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003887expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003888{
3889 char_u *p;
3890
3891 p = alloc(MAXPATHL);
3892 if (p != NULL)
3893 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3894 return p;
3895}
3896
3897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 * Expand environment variable with path name.
3899 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003900 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 * If anything fails no expansion is done and dst equals src.
3902 */
3903 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003904expand_env(
3905 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3906 char_u *dst, /* where to put the result */
3907 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003909 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910}
3911
3912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003913expand_env_esc(
3914 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3915 char_u *dst, /* where to put the result */
3916 int dstlen, /* maximum length of the result */
3917 int esc, /* escape spaces in expanded variables */
3918 int one, /* "srcp" is one file name */
3919 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003921 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 char_u *tail;
3923 int c;
3924 char_u *var;
3925 int copy_char;
3926 int mustfree; /* var was allocated, need to free it later */
3927 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003928 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003930 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003931 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003932
3933 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 --dstlen; /* leave one char space for "\," */
3935 while (*src && dstlen > 0)
3936 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003937#ifdef FEAT_EVAL
3938 /* Skip over `=expr`. */
3939 if (src[0] == '`' && src[1] == '=')
3940 {
3941 size_t len;
3942
3943 var = src;
3944 src += 2;
3945 (void)skip_expr(&src);
3946 if (*src == '`')
3947 ++src;
3948 len = src - var;
3949 if (len > (size_t)dstlen)
3950 len = dstlen;
3951 vim_strncpy(dst, var, len);
3952 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003953 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003954 continue;
3955 }
3956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003958 if ((*src == '$'
3959#ifdef VMS
3960 && at_start
3961#endif
3962 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003963#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 || *src == '%'
3965#endif
3966 || (*src == '~' && at_start))
3967 {
3968 mustfree = FALSE;
3969
3970 /*
3971 * The variable name is copied into dst temporarily, because it may
3972 * be a string in read-only memory and a NUL needs to be appended.
3973 */
3974 if (*src != '~') /* environment var */
3975 {
3976 tail = src + 1;
3977 var = dst;
3978 c = dstlen - 1;
3979
3980#ifdef UNIX
3981 /* Unix has ${var-name} type environment vars */
3982 if (*tail == '{' && !vim_isIDc('{'))
3983 {
3984 tail++; /* ignore '{' */
3985 while (c-- > 0 && *tail && *tail != '}')
3986 *var++ = *tail++;
3987 }
3988 else
3989#endif
3990 {
3991 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003992#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 || (*src == '%' && *tail != '%')
3994#endif
3995 ))
3996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
4000
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004001#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002# ifdef UNIX
4003 if (src[1] == '{' && *tail != '}')
4004# else
4005 if (*src == '%' && *tail != '%')
4006# endif
4007 var = NULL;
4008 else
4009 {
4010# ifdef UNIX
4011 if (src[1] == '{')
4012# else
4013 if (*src == '%')
4014#endif
4015 ++tail;
4016#endif
4017 *var = NUL;
4018 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004019#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021#endif
4022 }
4023 /* home directory */
4024 else if ( src[1] == NUL
4025 || vim_ispathsep(src[1])
4026 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4027 {
4028 var = homedir;
4029 tail = src + 1;
4030 }
4031 else /* user directory */
4032 {
4033#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4034 /*
4035 * Copy ~user to dst[], so we can put a NUL after it.
4036 */
4037 tail = src;
4038 var = dst;
4039 c = dstlen - 1;
4040 while ( c-- > 0
4041 && *tail
4042 && vim_isfilec(*tail)
4043 && !vim_ispathsep(*tail))
4044 *var++ = *tail++;
4045 *var = NUL;
4046# ifdef UNIX
4047 /*
4048 * If the system supports getpwnam(), use it.
4049 * Otherwise, or if getpwnam() fails, the shell is used to
4050 * expand ~user. This is slower and may fail if the shell
4051 * does not support ~user (old versions of /bin/sh).
4052 */
4053# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4054 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004055 /* Note: memory allocated by getpwnam() is never freed.
4056 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004057 struct passwd *pw = (*dst == NUL)
4058 ? NULL : getpwnam((char *)dst + 1);
4059
4060 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 if (var == NULL)
4063# endif
4064 {
4065 expand_T xpc;
4066
4067 ExpandInit(&xpc);
4068 xpc.xp_context = EXPAND_FILES;
4069 var = ExpandOne(&xpc, dst, NULL,
4070 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 mustfree = TRUE;
4072 }
4073
4074# else /* !UNIX, thus VMS */
4075 /*
4076 * USER_HOME is a comma-separated list of
4077 * directories to search for the user account in.
4078 */
4079 {
4080 char_u test[MAXPATHL], paths[MAXPATHL];
4081 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004082 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 STRCPY(paths, USER_HOME);
4085 next_path = paths;
4086 while (*next_path)
4087 {
4088 for (path = next_path; *next_path && *next_path != ',';
4089 next_path++);
4090 if (*next_path)
4091 *next_path++ = NUL;
4092 STRCPY(test, path);
4093 STRCAT(test, "/");
4094 STRCAT(test, dst + 1);
4095 if (mch_stat(test, &st) == 0)
4096 {
4097 var = alloc(STRLEN(test) + 1);
4098 STRCPY(var, test);
4099 mustfree = TRUE;
4100 break;
4101 }
4102 }
4103 }
4104# endif /* UNIX */
4105#else
4106 /* cannot expand user's home directory, so don't try */
4107 var = NULL;
4108 tail = (char_u *)""; /* for gcc */
4109#endif /* UNIX || VMS */
4110 }
4111
4112#ifdef BACKSLASH_IN_FILENAME
4113 /* If 'shellslash' is set change backslashes to forward slashes.
4114 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4115 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4116 {
4117 char_u *p = vim_strsave(var);
4118
4119 if (p != NULL)
4120 {
4121 if (mustfree)
4122 vim_free(var);
4123 var = p;
4124 mustfree = TRUE;
4125 forward_slash(var);
4126 }
4127 }
4128#endif
4129
4130 /* If "var" contains white space, escape it with a backslash.
4131 * Required for ":e ~/tt" when $HOME includes a space. */
4132 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4133 {
4134 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4135
4136 if (p != NULL)
4137 {
4138 if (mustfree)
4139 vim_free(var);
4140 var = p;
4141 mustfree = TRUE;
4142 }
4143 }
4144
4145 if (var != NULL && *var != NUL
4146 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4147 {
4148 STRCPY(dst, var);
4149 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004150 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 /* if var[] ends in a path separator and tail[] starts
4152 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004153 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4155 && dst[-1] != ':'
4156#endif
4157 && vim_ispathsep(*tail))
4158 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004159 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 src = tail;
4161 copy_char = FALSE;
4162 }
4163 if (mustfree)
4164 vim_free(var);
4165 }
4166
4167 if (copy_char) /* copy at least one char */
4168 {
4169 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004170 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004171 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4172 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 */
4174 at_start = FALSE;
4175 if (src[0] == '\\' && src[1] != NUL)
4176 {
4177 *dst++ = *src++;
4178 --dstlen;
4179 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004180 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004182 if (dstlen > 0)
4183 {
4184 *dst++ = *src++;
4185 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004186
Bram Moolenaar1c864092017-08-06 18:15:45 +02004187 if (startstr != NULL && src - startstr_len >= srcp
4188 && STRNCMP(src - startstr_len, startstr,
4189 startstr_len) == 0)
4190 at_start = TRUE;
4191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 *dst = NUL;
4196}
4197
4198/*
4199 * Vim's version of getenv().
4200 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004201 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004202 * "mustfree" is set to TRUE when returned is allocated, it must be
4203 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
4205 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004206vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 char_u *p;
4209 char_u *pend;
4210 int vimruntime;
4211
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004212#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 /* use "C:/" when $HOME is not set */
4214 if (STRCMP(name, "HOME") == 0)
4215 return homedir;
4216#endif
4217
4218 p = mch_getenv(name);
4219 if (p != NULL && *p == NUL) /* empty is the same as not set */
4220 p = NULL;
4221
4222 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004223 {
4224#if defined(FEAT_MBYTE) && defined(WIN3264)
4225 if (enc_utf8)
4226 {
4227 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004228 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004229
4230 /* Convert from active codepage to UTF-8. Other conversions are
4231 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004232 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004233 if (pp != NULL)
4234 {
4235 p = pp;
4236 *mustfree = TRUE;
4237 }
4238 }
4239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4244 if (!vimruntime && STRCMP(name, "VIM") != 0)
4245 return NULL;
4246
4247 /*
4248 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4249 * Don't do this when default_vimruntime_dir is non-empty.
4250 */
4251 if (vimruntime
4252#ifdef HAVE_PATHDEF
4253 && *default_vimruntime_dir == NUL
4254#endif
4255 )
4256 {
4257 p = mch_getenv((char_u *)"VIM");
4258 if (p != NULL && *p == NUL) /* empty is the same as not set */
4259 p = NULL;
4260 if (p != NULL)
4261 {
4262 p = vim_version_dir(p);
4263 if (p != NULL)
4264 *mustfree = TRUE;
4265 else
4266 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004267
4268#if defined(FEAT_MBYTE) && defined(WIN3264)
4269 if (enc_utf8)
4270 {
4271 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004272 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004273
4274 /* Convert from active codepage to UTF-8. Other conversions
4275 * are not done, because they would fail for non-ASCII
4276 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004277 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004278 if (pp != NULL)
4279 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004280 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281 vim_free(p);
4282 p = pp;
4283 *mustfree = TRUE;
4284 }
4285 }
4286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289
4290 /*
4291 * When expanding $VIM or $VIMRUNTIME fails, try using:
4292 * - the directory name from 'helpfile' (unless it contains '$')
4293 * - the executable name from argv[0]
4294 */
4295 if (p == NULL)
4296 {
4297 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4298 p = p_hf;
4299#ifdef USE_EXE_NAME
4300 /*
4301 * Use the name of the executable, obtained from argv[0].
4302 */
4303 else
4304 p = exe_name;
4305#endif
4306 if (p != NULL)
4307 {
4308 /* remove the file name */
4309 pend = gettail(p);
4310
4311 /* remove "doc/" from 'helpfile', if present */
4312 if (p == p_hf)
4313 pend = remove_tail(p, pend, (char_u *)"doc");
4314
4315#ifdef USE_EXE_NAME
4316# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004317 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (p == exe_name)
4319 {
4320 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004321 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004323 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4324 if (pend1 != pend)
4325 {
4326 pnew = alloc((unsigned)(pend1 - p) + 15);
4327 if (pnew != NULL)
4328 {
4329 STRNCPY(pnew, p, (pend1 - p));
4330 STRCPY(pnew + (pend1 - p), "Resources/vim");
4331 p = pnew;
4332 pend = p + STRLEN(p);
4333 }
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336# endif
4337 /* remove "src/" from exe_name, if present */
4338 if (p == exe_name)
4339 pend = remove_tail(p, pend, (char_u *)"src");
4340#endif
4341
4342 /* for $VIM, remove "runtime/" or "vim54/", if present */
4343 if (!vimruntime)
4344 {
4345 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4346 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4347 }
4348
4349 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004350 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004353#ifdef MACOS_X
4354 if (p == exe_name || p == p_hf)
4355#endif
4356 /* check that the result is a directory name */
4357 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
4359 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004360 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 else
4362 {
4363#ifdef USE_EXE_NAME
4364 /* may add "/vim54" or "/runtime" if it exists */
4365 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4366 {
4367 vim_free(p);
4368 p = pend;
4369 }
4370#endif
4371 *mustfree = TRUE;
4372 }
4373 }
4374 }
4375
4376#ifdef HAVE_PATHDEF
4377 /* When there is a pathdef.c file we can use default_vim_dir and
4378 * default_vimruntime_dir */
4379 if (p == NULL)
4380 {
4381 /* Only use default_vimruntime_dir when it is not empty */
4382 if (vimruntime && *default_vimruntime_dir != NUL)
4383 {
4384 p = default_vimruntime_dir;
4385 *mustfree = FALSE;
4386 }
4387 else if (*default_vim_dir != NUL)
4388 {
4389 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4390 *mustfree = TRUE;
4391 else
4392 {
4393 p = default_vim_dir;
4394 *mustfree = FALSE;
4395 }
4396 }
4397 }
4398#endif
4399
4400 /*
4401 * Set the environment variable, so that the new value can be found fast
4402 * next time, and others can also use it (e.g. Perl).
4403 */
4404 if (p != NULL)
4405 {
4406 if (vimruntime)
4407 {
4408 vim_setenv((char_u *)"VIMRUNTIME", p);
4409 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 else
4412 {
4413 vim_setenv((char_u *)"VIM", p);
4414 didset_vim = TRUE;
4415 }
4416 }
4417 return p;
4418}
4419
4420/*
4421 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4422 * Return NULL if not, return its name in allocated memory otherwise.
4423 */
4424 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004425vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
4427 char_u *p;
4428
4429 if (vimdir == NULL || *vimdir == NUL)
4430 return NULL;
4431 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4432 if (p != NULL && mch_isdir(p))
4433 return p;
4434 vim_free(p);
4435 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4436 if (p != NULL && mch_isdir(p))
4437 return p;
4438 vim_free(p);
4439 return NULL;
4440}
4441
4442/*
4443 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4444 * the length of "name/". Otherwise return "pend".
4445 */
4446 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004447remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
4449 int len = (int)STRLEN(name) + 1;
4450 char_u *newend = pend - len;
4451
4452 if (newend >= p
4453 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004454 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return newend;
4456 return pend;
4457}
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 * Our portable version of setenv.
4461 */
4462 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004463vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465#ifdef HAVE_SETENV
4466 mch_setenv((char *)name, (char *)val, 1);
4467#else
4468 char_u *envbuf;
4469
4470 /*
4471 * Putenv does not copy the string, it has to remain
4472 * valid. The allocated memory will never be freed.
4473 */
4474 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4475 if (envbuf != NULL)
4476 {
4477 sprintf((char *)envbuf, "%s=%s", name, val);
4478 putenv((char *)envbuf);
4479 }
4480#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004481#ifdef FEAT_GETTEXT
4482 /*
4483 * When setting $VIMRUNTIME adjust the directory to find message
4484 * translations to $VIMRUNTIME/lang.
4485 */
4486 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4487 {
4488 char_u *buf = concat_str(val, (char_u *)"/lang");
4489
4490 if (buf != NULL)
4491 {
4492 bindtextdomain(VIMPACKAGE, (char *)buf);
4493 vim_free(buf);
4494 }
4495 }
4496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497}
4498
4499#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4500/*
4501 * Function given to ExpandGeneric() to obtain an environment variable name.
4502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004504get_env_name(
4505 expand_T *xp UNUSED,
4506 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507{
Bram Moolenaard0573012017-10-28 21:11:06 +02004508# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004510 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 */
4512 return NULL;
4513# else
4514# ifndef __WIN32__
4515 /* Borland C++ 5.2 has this in a header file. */
4516 extern char **environ;
4517# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004518# define ENVNAMELEN 100
4519 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 char_u *str;
4521 int n;
4522
4523 str = (char_u *)environ[idx];
4524 if (str == NULL)
4525 return NULL;
4526
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004527 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 if (str[n] == '=' || str[n] == NUL)
4530 break;
4531 name[n] = str[n];
4532 }
4533 name[n] = NUL;
4534 return name;
4535# endif
4536}
Bram Moolenaar24305862012-08-15 14:05:05 +02004537
4538/*
4539 * Find all user names for user completion.
4540 * Done only once and then cached.
4541 */
4542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004543init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004544{
Bram Moolenaar24305862012-08-15 14:05:05 +02004545 static int lazy_init_done = FALSE;
4546
4547 if (lazy_init_done)
4548 return;
4549
4550 lazy_init_done = TRUE;
4551 ga_init2(&ga_users, sizeof(char_u *), 20);
4552
4553# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4554 {
4555 char_u* user;
4556 struct passwd* pw;
4557
4558 setpwent();
4559 while ((pw = getpwent()) != NULL)
4560 /* pw->pw_name shouldn't be NULL but just in case... */
4561 if (pw->pw_name != NULL)
4562 {
4563 if (ga_grow(&ga_users, 1) == FAIL)
4564 break;
4565 user = vim_strsave((char_u*)pw->pw_name);
4566 if (user == NULL)
4567 break;
4568 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4569 }
4570 endpwent();
4571 }
4572# endif
4573}
4574
4575/*
4576 * Function given to ExpandGeneric() to obtain an user names.
4577 */
4578 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004579get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004580{
4581 init_users();
4582 if (idx < ga_users.ga_len)
4583 return ((char_u **)ga_users.ga_data)[idx];
4584 return NULL;
4585}
4586
4587/*
4588 * Check whether name matches a user name. Return:
4589 * 0 if name does not match any user name.
4590 * 1 if name partially matches the beginning of a user name.
4591 * 2 is name fully matches a user name.
4592 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004593int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004594{
4595 int i;
4596 int n = (int)STRLEN(name);
4597 int result = 0;
4598
4599 init_users();
4600 for (i = 0; i < ga_users.ga_len; i++)
4601 {
4602 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4603 return 2; /* full match */
4604 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4605 result = 1; /* partial match */
4606 }
4607 return result;
4608}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609#endif
4610
4611/*
4612 * Replace home directory by "~" in each space or comma separated file name in
4613 * 'src'.
4614 * If anything fails (except when out of space) dst equals src.
4615 */
4616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004617home_replace(
4618 buf_T *buf, /* when not NULL, check for help files */
4619 char_u *src, /* input file name */
4620 char_u *dst, /* where to put the result */
4621 int dstlen, /* maximum length of the result */
4622 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 spaces and commas in the file name. */
4624{
4625 size_t dirlen = 0, envlen = 0;
4626 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004627 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 char_u *p;
4629
4630 if (src == NULL)
4631 {
4632 *dst = NUL;
4633 return;
4634 }
4635
4636 /*
4637 * If the file is a help file, remove the path completely.
4638 */
4639 if (buf != NULL && buf->b_help)
4640 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004641 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 return;
4643 }
4644
4645 /*
4646 * We check both the value of the $HOME environment variable and the
4647 * "real" home directory.
4648 */
4649 if (homedir != NULL)
4650 dirlen = STRLEN(homedir);
4651
4652#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004653 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004655 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4656#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004657#ifdef WIN3264
4658 if (homedir_env == NULL)
4659 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4660#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004661 /* Empty is the same as not set. */
4662 if (homedir_env != NULL && *homedir_env == NUL)
4663 homedir_env = NULL;
4664
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004665#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004666 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004667 {
4668 int usedlen = 0;
4669 int flen;
4670 char_u *fbuf = NULL;
4671
4672 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004673 (void)modify_fname((char_u *)":p", &usedlen,
4674 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004675 flen = (int)STRLEN(homedir_env);
4676 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4677 /* Remove the trailing / that is added to a directory. */
4678 homedir_env[flen - 1] = NUL;
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680#endif
4681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 if (homedir_env != NULL)
4683 envlen = STRLEN(homedir_env);
4684
4685 if (!one)
4686 src = skipwhite(src);
4687 while (*src && dstlen > 0)
4688 {
4689 /*
4690 * Here we are at the beginning of a file name.
4691 * First, check to see if the beginning of the file name matches
4692 * $HOME or the "real" home directory. Check that there is a '/'
4693 * after the match (so that if e.g. the file is "/home/pieter/bla",
4694 * and the home directory is "/home/piet", the file does not end up
4695 * as "~er/bla" (which would seem to indicate the file "bla" in user
4696 * er's home directory)).
4697 */
4698 p = homedir;
4699 len = dirlen;
4700 for (;;)
4701 {
4702 if ( len
4703 && fnamencmp(src, p, len) == 0
4704 && (vim_ispathsep(src[len])
4705 || (!one && (src[len] == ',' || src[len] == ' '))
4706 || src[len] == NUL))
4707 {
4708 src += len;
4709 if (--dstlen > 0)
4710 *dst++ = '~';
4711
4712 /*
4713 * If it's just the home directory, add "/".
4714 */
4715 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4716 *dst++ = '/';
4717 break;
4718 }
4719 if (p == homedir_env)
4720 break;
4721 p = homedir_env;
4722 len = envlen;
4723 }
4724
4725 /* if (!one) skip to separator: space or comma */
4726 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4727 *dst++ = *src++;
4728 /* skip separator */
4729 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4730 *dst++ = *src++;
4731 }
4732 /* if (dstlen == 0) out of space, what to do??? */
4733
4734 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004735
4736 if (homedir_env != homedir_env_orig)
4737 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738}
4739
4740/*
4741 * Like home_replace, store the replaced string in allocated memory.
4742 * When something fails, NULL is returned.
4743 */
4744 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004745home_replace_save(
4746 buf_T *buf, /* when not NULL, check for help files */
4747 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 char_u *dst;
4750 unsigned len;
4751
4752 len = 3; /* space for "~/" and trailing NUL */
4753 if (src != NULL) /* just in case */
4754 len += (unsigned)STRLEN(src);
4755 dst = alloc(len);
4756 if (dst != NULL)
4757 home_replace(buf, src, dst, len, TRUE);
4758 return dst;
4759}
4760
4761/*
4762 * Compare two file names and return:
4763 * FPC_SAME if they both exist and are the same file.
4764 * FPC_SAMEX if they both don't exist and have the same file name.
4765 * FPC_DIFF if they both exist and are different files.
4766 * FPC_NOTX if they both don't exist.
4767 * FPC_DIFFX if one of them doesn't exist.
4768 * For the first name environment variables are expanded
4769 */
4770 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004771fullpathcmp(
4772 char_u *s1,
4773 char_u *s2,
4774 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776#ifdef UNIX
4777 char_u exp1[MAXPATHL];
4778 char_u full1[MAXPATHL];
4779 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004780 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 int r1, r2;
4782
4783 expand_env(s1, exp1, MAXPATHL);
4784 r1 = mch_stat((char *)exp1, &st1);
4785 r2 = mch_stat((char *)s2, &st2);
4786 if (r1 != 0 && r2 != 0)
4787 {
4788 /* if mch_stat() doesn't work, may compare the names */
4789 if (checkname)
4790 {
4791 if (fnamecmp(exp1, s2) == 0)
4792 return FPC_SAMEX;
4793 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4794 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4795 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4796 return FPC_SAMEX;
4797 }
4798 return FPC_NOTX;
4799 }
4800 if (r1 != 0 || r2 != 0)
4801 return FPC_DIFFX;
4802 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4803 return FPC_SAME;
4804 return FPC_DIFF;
4805#else
4806 char_u *exp1; /* expanded s1 */
4807 char_u *full1; /* full path of s1 */
4808 char_u *full2; /* full path of s2 */
4809 int retval = FPC_DIFF;
4810 int r1, r2;
4811
4812 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4813 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4814 {
4815 full1 = exp1 + MAXPATHL;
4816 full2 = full1 + MAXPATHL;
4817
4818 expand_env(s1, exp1, MAXPATHL);
4819 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4820 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4821
4822 /* If vim_FullName() fails, the file probably doesn't exist. */
4823 if (r1 != OK && r2 != OK)
4824 {
4825 if (checkname && fnamecmp(exp1, s2) == 0)
4826 retval = FPC_SAMEX;
4827 else
4828 retval = FPC_NOTX;
4829 }
4830 else if (r1 != OK || r2 != OK)
4831 retval = FPC_DIFFX;
4832 else if (fnamecmp(full1, full2))
4833 retval = FPC_DIFF;
4834 else
4835 retval = FPC_SAME;
4836 vim_free(exp1);
4837 }
4838 return retval;
4839#endif
4840}
4841
4842/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004843 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004844 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004845 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
4847 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004848gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 char_u *p1, *p2;
4851
4852 if (fname == NULL)
4853 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004854 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004856 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004858 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 return p1;
4861}
4862
Bram Moolenaar31710262010-08-13 13:36:15 +02004863#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004864static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004865
4866/*
4867 * Return the end of the directory name, on the first path
4868 * separator:
4869 * "/path/file", "/path/dir/", "/path//dir", "/file"
4870 * ^ ^ ^ ^
4871 */
4872 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004873gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004874{
4875 char_u *dir_end = fname;
4876 char_u *next_dir_end = fname;
4877 int look_for_sep = TRUE;
4878 char_u *p;
4879
4880 for (p = fname; *p != NUL; )
4881 {
4882 if (vim_ispathsep(*p))
4883 {
4884 if (look_for_sep)
4885 {
4886 next_dir_end = p;
4887 look_for_sep = FALSE;
4888 }
4889 }
4890 else
4891 {
4892 if (!look_for_sep)
4893 dir_end = next_dir_end;
4894 look_for_sep = TRUE;
4895 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004896 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004897 }
4898 return dir_end;
4899}
4900#endif
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004903 * Get pointer to tail of "fname", including path separators. Putting a NUL
4904 * here leaves the directory name. Takes care of "c:/" and "//".
4905 * Always returns a valid pointer.
4906 */
4907 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004908gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004909{
4910 char_u *p;
4911 char_u *t;
4912
4913 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4914 t = gettail(fname);
4915 while (t > p && after_pathsep(fname, t))
4916 --t;
4917#ifdef VMS
4918 /* path separator is part of the path */
4919 ++t;
4920#endif
4921 return t;
4922}
4923
4924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 * get the next path component (just after the next path separator).
4926 */
4927 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004928getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
4930 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004931 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (*fname)
4933 ++fname;
4934 return fname;
4935}
4936
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937/*
4938 * Get a pointer to one character past the head of a path name.
4939 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4940 * If there is no head, path is returned.
4941 */
4942 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004943get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 char_u *retval;
4946
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004947#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /* may skip "c:" */
4949 if (isalpha(path[0]) && path[1] == ':')
4950 retval = path + 2;
4951 else
4952 retval = path;
4953#else
4954# if defined(AMIGA)
4955 /* may skip "label:" */
4956 retval = vim_strchr(path, ':');
4957 if (retval == NULL)
4958 retval = path;
4959# else /* Unix */
4960 retval = path;
4961# endif
4962#endif
4963
4964 while (vim_ispathsep(*retval))
4965 ++retval;
4966
4967 return retval;
4968}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004971 * Return TRUE if 'c' is a path separator.
4972 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
4974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004975vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004977#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004979#else
4980# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004982# else
4983# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4985 return (c == ':' || c == '[' || c == ']' || c == '/'
4986 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004987# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004989# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992}
4993
Bram Moolenaar69c35002013-11-04 02:54:12 +01004994/*
4995 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4996 */
4997 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004998vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004999{
5000 return vim_ispathsep(c)
5001#ifdef BACKSLASH_IN_FILENAME
5002 && c != ':'
5003#endif
5004 ;
5005}
5006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5008/*
5009 * return TRUE if 'c' is a path list separator.
5010 */
5011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005012vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014#ifdef UNIX
5015 return (c == ':');
5016#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005017 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018#endif
5019}
5020#endif
5021
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005022/*
5023 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5024 * It's done in-place.
5025 */
5026 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005027shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005028{
5029 char_u *tail, *s, *d;
5030 int skip = FALSE;
5031
5032 tail = gettail(str);
5033 d = str;
5034 for (s = str; ; ++s)
5035 {
5036 if (s >= tail) /* copy the whole tail */
5037 {
5038 *d++ = *s;
5039 if (*s == NUL)
5040 break;
5041 }
5042 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5043 {
5044 *d++ = *s;
5045 skip = FALSE;
5046 }
5047 else if (!skip)
5048 {
5049 *d++ = *s; /* copy next char */
5050 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5051 skip = TRUE;
5052# ifdef FEAT_MBYTE
5053 if (has_mbyte)
5054 {
5055 int l = mb_ptr2len(s);
5056
5057 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005058 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005059 }
5060# endif
5061 }
5062 }
5063}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005064
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005065/*
5066 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5067 * Also returns TRUE if there is no directory name.
5068 * "fname" must be writable!.
5069 */
5070 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005071dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005072{
5073 char_u *p;
5074 int c;
5075 int retval;
5076
5077 p = gettail_sep(fname);
5078 if (p == fname)
5079 return TRUE;
5080 c = *p;
5081 *p = NUL;
5082 retval = mch_isdir(fname);
5083 *p = c;
5084 return retval;
5085}
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005088 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5089 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
5091 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005092vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005094#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005096#else
5097 if (p_fic)
5098 return MB_STRICMP(x, y);
5099 return STRCMP(x, y);
5100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101}
5102
5103 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005104vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005106#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005107 char_u *px = x;
5108 char_u *py = y;
5109 int cx = NUL;
5110 int cy = NUL;
5111
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005112 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005114 cx = PTR2CHAR(px);
5115 cy = PTR2CHAR(py);
5116 if (cx == NUL || cy == NUL
5117 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5118 && !(cx == '/' && cy == '\\')
5119 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005121 len -= MB_PTR2LEN(px);
5122 px += MB_PTR2LEN(px);
5123 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 if (len == 0)
5126 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005127 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005128#else
5129 if (p_fic)
5130 return MB_STRNICMP(x, y, len);
5131 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135/*
5136 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005137 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 */
5139 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005140concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 char_u *dest;
5143
5144 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5145 if (dest != NULL)
5146 {
5147 STRCPY(dest, fname1);
5148 if (sep)
5149 add_pathsep(dest);
5150 STRCAT(dest, fname2);
5151 }
5152 return dest;
5153}
5154
Bram Moolenaard6754642005-01-17 22:18:45 +00005155/*
5156 * Concatenate two strings and return the result in allocated memory.
5157 * Returns NULL when out of memory.
5158 */
5159 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005160concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005161{
5162 char_u *dest;
5163 size_t l = STRLEN(str1);
5164
5165 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5166 if (dest != NULL)
5167 {
5168 STRCPY(dest, str1);
5169 STRCPY(dest + l, str2);
5170 }
5171 return dest;
5172}
Bram Moolenaard6754642005-01-17 22:18:45 +00005173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174/*
5175 * Add a path separator to a file name, unless it already ends in a path
5176 * separator.
5177 */
5178 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005179add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005181 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 STRCAT(p, PATHSEPSTR);
5183}
5184
5185/*
5186 * FullName_save - Make an allocated copy of a full file name.
5187 * Returns NULL when out of memory.
5188 */
5189 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005190FullName_save(
5191 char_u *fname,
5192 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005193 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 char_u *buf;
5196 char_u *new_fname = NULL;
5197
5198 if (fname == NULL)
5199 return NULL;
5200
5201 buf = alloc((unsigned)MAXPATHL);
5202 if (buf != NULL)
5203 {
5204 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5205 new_fname = vim_strsave(buf);
5206 else
5207 new_fname = vim_strsave(fname);
5208 vim_free(buf);
5209 }
5210 return new_fname;
5211}
5212
5213#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5214
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005215static char_u *skip_string(char_u *p);
5216static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005217static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005218static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
5220/*
5221 * Find the start of a comment, not knowing if we are in a comment right now.
5222 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005223 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005225 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005226ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005227{
5228 return find_start_comment(curbuf->b_ind_maxcomment);
5229}
5230
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005232find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
5234 pos_T *pos;
5235 char_u *line;
5236 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005237 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005239 for (;;)
5240 {
5241 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5242 if (pos == NULL)
5243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005245 /*
5246 * Check if the comment start we found is inside a string.
5247 * If it is then restrict the search to below this line and try again.
5248 */
5249 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005250 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005251 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005252 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005253 break;
5254 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5255 if (cur_maxcomment <= 0)
5256 {
5257 pos = NULL;
5258 break;
5259 }
5260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 return pos;
5262}
5263
5264/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005265 * Find the start of a comment or raw string, not knowing if we are in a
5266 * comment or raw string right now.
5267 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005268 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005269 * Return NULL when not inside a comment or raw string.
5270 * "CORS" -> Comment Or Raw String
5271 */
5272 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005273ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005274{
Bram Moolenaar089af182015-10-07 11:41:49 +02005275 static pos_T comment_pos_copy;
5276 pos_T *comment_pos;
5277 pos_T *rs_pos;
5278
5279 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5280 if (comment_pos != NULL)
5281 {
5282 /* Need to make a copy of the static pos in findmatchlimit(),
5283 * calling find_start_rawstring() may change it. */
5284 comment_pos_copy = *comment_pos;
5285 comment_pos = &comment_pos_copy;
5286 }
5287 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005288
5289 /* If comment_pos is before rs_pos the raw string is inside the comment.
5290 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005291 if (comment_pos == NULL || (rs_pos != NULL
5292 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005293 {
5294 if (is_raw != NULL && rs_pos != NULL)
5295 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005296 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005297 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005298 return comment_pos;
5299}
5300
5301/*
5302 * Find the start of a raw string, not knowing if we are in one right now.
5303 * Search starts at w_cursor.lnum and goes backwards.
5304 * Return NULL when not inside a raw string.
5305 */
5306 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005307find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005308{
5309 pos_T *pos;
5310 char_u *line;
5311 char_u *p;
5312 int cur_maxcomment = ind_maxcomment;
5313
5314 for (;;)
5315 {
5316 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5317 if (pos == NULL)
5318 break;
5319
5320 /*
5321 * Check if the raw string start we found is inside a string.
5322 * If it is then restrict the search to below this line and try again.
5323 */
5324 line = ml_get(pos->lnum);
5325 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5326 p = skip_string(p);
5327 if ((colnr_T)(p - line) <= pos->col)
5328 break;
5329 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5330 if (cur_maxcomment <= 0)
5331 {
5332 pos = NULL;
5333 break;
5334 }
5335 }
5336 return pos;
5337}
5338
5339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 * Skip to the end of a "string" and a 'c' character.
5341 * If there is no string or character, return argument unmodified.
5342 */
5343 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005344skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
5346 int i;
5347
5348 /*
5349 * We loop, because strings may be concatenated: "date""time".
5350 */
5351 for ( ; ; ++p)
5352 {
5353 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5354 {
5355 if (!p[1]) /* ' at end of line */
5356 break;
5357 i = 2;
5358 if (p[1] == '\\') /* '\n' or '\000' */
5359 {
5360 ++i;
5361 while (vim_isdigit(p[i - 1])) /* '\000' */
5362 ++i;
5363 }
5364 if (p[i] == '\'') /* check for trailing ' */
5365 {
5366 p += i;
5367 continue;
5368 }
5369 }
5370 else if (p[0] == '"') /* start of string */
5371 {
5372 for (++p; p[0]; ++p)
5373 {
5374 if (p[0] == '\\' && p[1] != NUL)
5375 ++p;
5376 else if (p[0] == '"') /* end of string */
5377 break;
5378 }
5379 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005380 continue; /* continue for another string */
5381 }
5382 else if (p[0] == 'R' && p[1] == '"')
5383 {
5384 /* Raw string: R"[delim](...)[delim]" */
5385 char_u *delim = p + 2;
5386 char_u *paren = vim_strchr(delim, '(');
5387
5388 if (paren != NULL)
5389 {
5390 size_t delim_len = paren - delim;
5391
5392 for (p += 3; *p; ++p)
5393 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5394 && p[delim_len + 1] == '"')
5395 {
5396 p += delim_len + 1;
5397 break;
5398 }
5399 if (p[0] == '"')
5400 continue; /* continue for another string */
5401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 break; /* no string found */
5404 }
5405 if (!*p)
5406 --p; /* backup from NUL */
5407 return p;
5408}
5409#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5410
5411#if defined(FEAT_CINDENT) || defined(PROTO)
5412
5413/*
5414 * Do C or expression indenting on the current line.
5415 */
5416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005417do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419# ifdef FEAT_EVAL
5420 if (*curbuf->b_p_inde != NUL)
5421 fixthisline(get_expr_indent);
5422 else
5423# endif
5424 fixthisline(get_c_indent);
5425}
5426
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005427/* Find result cache for cpp_baseclass */
5428typedef struct {
5429 int found;
5430 lpos_T lpos;
5431} cpp_baseclass_cache_T;
5432
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433/*
5434 * Functions for C-indenting.
5435 * Most of this originally comes from Eric Fischer.
5436 */
5437/*
5438 * Below "XXX" means that this function may unlock the current line.
5439 */
5440
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005441static char_u *cin_skipcomment(char_u *);
5442static int cin_nocode(char_u *);
5443static pos_T *find_line_comment(void);
5444static int cin_has_js_key(char_u *text);
5445static int cin_islabel_skip(char_u **);
5446static int cin_isdefault(char_u *);
5447static char_u *after_label(char_u *l);
5448static int get_indent_nolabel(linenr_T lnum);
5449static int skip_label(linenr_T, char_u **pp);
5450static int cin_first_id_amount(void);
5451static int cin_get_equal_amount(linenr_T lnum);
5452static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005453static int cin_iscomment(char_u *);
5454static int cin_islinecomment(char_u *);
5455static int cin_isterminated(char_u *, int, int);
5456static int cin_isinit(void);
5457static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5458static int cin_isif(char_u *);
5459static int cin_iselse(char_u *);
5460static int cin_isdo(char_u *);
5461static int cin_iswhileofdo(char_u *, linenr_T);
5462static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5463static int cin_iswhileofdo_end(int terminated);
5464static int cin_isbreak(char_u *);
5465static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5466static int get_baseclass_amount(int col);
5467static int cin_ends_in(char_u *, char_u *, char_u *);
5468static int cin_starts_with(char_u *s, char *word);
5469static int cin_skip2pos(pos_T *trypos);
5470static pos_T *find_start_brace(void);
5471static pos_T *find_match_paren(int);
5472static pos_T *find_match_char(int c, int ind_maxparen);
5473static int corr_ind_maxparen(pos_T *startpos);
5474static int find_last_paren(char_u *l, int start, int end);
5475static int find_match(int lookfor, linenr_T ourscope);
5476static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478/*
5479 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005480 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 */
5482 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005483cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 while (*s)
5486 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005487 char_u *prev_s = s;
5488
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005490
5491 /* Perl/shell # comment comment continues until eol. Require a space
5492 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005493 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005494 {
5495 s += STRLEN(s);
5496 break;
5497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 if (*s != '/')
5499 break;
5500 ++s;
5501 if (*s == '/') /* slash-slash comment continues till eol */
5502 {
5503 s += STRLEN(s);
5504 break;
5505 }
5506 if (*s != '*')
5507 break;
5508 for (++s; *s; ++s) /* skip slash-star comment */
5509 if (s[0] == '*' && s[1] == '/')
5510 {
5511 s += 2;
5512 break;
5513 }
5514 }
5515 return s;
5516}
5517
5518/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005519 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 * not considered code.
5521 */
5522 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 return *cin_skipcomment(s) == NUL;
5526}
5527
5528/*
5529 * Check previous lines for a "//" line comment, skipping over blank lines.
5530 */
5531 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005532find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 static pos_T pos;
5535 char_u *line;
5536 char_u *p;
5537
5538 pos = curwin->w_cursor;
5539 while (--pos.lnum > 0)
5540 {
5541 line = ml_get(pos.lnum);
5542 p = skipwhite(line);
5543 if (cin_islinecomment(p))
5544 {
5545 pos.col = (int)(p - line);
5546 return &pos;
5547 }
5548 if (*p != NUL)
5549 break;
5550 }
5551 return NULL;
5552}
5553
5554/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005555 * Return TRUE if "text" starts with "key:".
5556 */
5557 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005558cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005559{
5560 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005561 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005562
5563 if (*s == '\'' || *s == '"')
5564 {
5565 /* can be 'key': or "key": */
5566 quote = *s;
5567 ++s;
5568 }
5569 if (!vim_isIDc(*s)) /* need at least one ID character */
5570 return FALSE;
5571
5572 while (vim_isIDc(*s))
5573 ++s;
5574 if (*s == quote)
5575 ++s;
5576
5577 s = cin_skipcomment(s);
5578
5579 /* "::" is not a label, it's C++ */
5580 return (*s == ':' && s[1] != ':');
5581}
5582
5583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005585 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 */
5587 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590 if (!vim_isIDc(**s)) /* need at least one ID character */
5591 return FALSE;
5592
5593 while (vim_isIDc(**s))
5594 (*s)++;
5595
5596 *s = cin_skipcomment(*s);
5597
5598 /* "::" is not a label, it's C++ */
5599 return (**s == ':' && *++*s != ':');
5600}
5601
5602/*
5603 * Recognize a label: "label:".
5604 * Note: curwin->w_cursor must be where we are looking for the label.
5605 */
5606 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005607cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
5609 char_u *s;
5610
5611 s = cin_skipcomment(ml_get_curline());
5612
5613 /*
5614 * Exclude "default" from labels, since it should be indented
5615 * like a switch label. Same for C++ scope declarations.
5616 */
5617 if (cin_isdefault(s))
5618 return FALSE;
5619 if (cin_isscopedecl(s))
5620 return FALSE;
5621
5622 if (cin_islabel_skip(&s))
5623 {
5624 /*
5625 * Only accept a label if the previous line is terminated or is a case
5626 * label.
5627 */
5628 pos_T cursor_save;
5629 pos_T *trypos;
5630 char_u *line;
5631
5632 cursor_save = curwin->w_cursor;
5633 while (curwin->w_cursor.lnum > 1)
5634 {
5635 --curwin->w_cursor.lnum;
5636
5637 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005638 * If we're in a comment or raw string now, skip to the start of
5639 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 */
5641 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005642 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 curwin->w_cursor = *trypos;
5644
5645 line = ml_get_curline();
5646 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5647 continue;
5648 if (*(line = cin_skipcomment(line)) == NUL)
5649 continue;
5650
5651 curwin->w_cursor = cursor_save;
5652 if (cin_isterminated(line, TRUE, FALSE)
5653 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005654 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 || (cin_islabel_skip(&line) && cin_nocode(line)))
5656 return TRUE;
5657 return FALSE;
5658 }
5659 curwin->w_cursor = cursor_save;
5660 return TRUE; /* label at start of file??? */
5661 }
5662 return FALSE;
5663}
5664
5665/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005666 * Recognize structure initialization and enumerations:
5667 * "[typedef] [static|public|protected|private] enum"
5668 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 */
5670 static int
5671cin_isinit(void)
5672{
5673 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005674 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
5676 s = cin_skipcomment(ml_get_curline());
5677
Bram Moolenaar75342212013-03-07 13:13:52 +01005678 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 s = cin_skipcomment(s + 7);
5680
Bram Moolenaar75342212013-03-07 13:13:52 +01005681 for (;;)
5682 {
5683 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005684
Bram Moolenaar75342212013-03-07 13:13:52 +01005685 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5686 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005687 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005688 if (cin_starts_with(s, skip[i]))
5689 {
5690 s = cin_skipcomment(s + l);
5691 l = 0;
5692 break;
5693 }
5694 }
5695 if (l != 0)
5696 break;
5697 }
5698
5699 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return TRUE;
5701
5702 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5703 return TRUE;
5704
5705 return FALSE;
5706}
5707
5708/*
5709 * Recognize a switch label: "case .*:" or "default:".
5710 */
5711 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005712cin_iscase(
5713 char_u *s,
5714 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
5716 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005717 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 for (s += 4; *s; ++s)
5720 {
5721 s = cin_skipcomment(s);
5722 if (*s == ':')
5723 {
5724 if (s[1] == ':') /* skip over "::" for C++ */
5725 ++s;
5726 else
5727 return TRUE;
5728 }
5729 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005730 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5732 return FALSE; /* stop at comment */
5733 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005734 {
5735 /* JS etc. */
5736 if (strict)
5737 return FALSE; /* stop at string */
5738 else
5739 return TRUE;
5740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742 return FALSE;
5743 }
5744
5745 if (cin_isdefault(s))
5746 return TRUE;
5747 return FALSE;
5748}
5749
5750/*
5751 * Recognize a "default" switch label.
5752 */
5753 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005754cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 return (STRNCMP(s, "default", 7) == 0
5757 && *(s = cin_skipcomment(s + 7)) == ':'
5758 && s[1] != ':');
5759}
5760
5761/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005762 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 */
5764 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005765cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 int i;
5768
5769 s = cin_skipcomment(s);
5770 if (STRNCMP(s, "public", 6) == 0)
5771 i = 6;
5772 else if (STRNCMP(s, "protected", 9) == 0)
5773 i = 9;
5774 else if (STRNCMP(s, "private", 7) == 0)
5775 i = 7;
5776 else
5777 return FALSE;
5778 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5779}
5780
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005781/* Maximum number of lines to search back for a "namespace" line. */
5782#define FIND_NAMESPACE_LIM 20
5783
5784/*
5785 * Recognize a "namespace" scope declaration.
5786 */
5787 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005788cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005789{
5790 char_u *p;
5791 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005792 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005793
5794 s = cin_skipcomment(s);
5795 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5796 {
5797 p = cin_skipcomment(skipwhite(s + 9));
5798 while (*p != NUL)
5799 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005800 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005801 {
5802 has_name = TRUE; /* found end of a name */
5803 p = cin_skipcomment(skipwhite(p));
5804 }
5805 else if (*p == '{')
5806 {
5807 break;
5808 }
5809 else if (vim_iswordc(*p))
5810 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005811 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005812 if (has_name)
5813 return FALSE; /* word character after skipping past name */
5814 ++p;
5815 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005816 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5817 {
5818 if (!has_name_start || has_name)
5819 return FALSE;
5820 /* C++ 17 nested namespace */
5821 p += 3;
5822 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005823 else
5824 {
5825 return FALSE;
5826 }
5827 }
5828 return TRUE;
5829 }
5830 return FALSE;
5831}
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005834 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5835 */
5836 static int
5837cin_is_cpp_extern_c(char_u *s)
5838{
5839 char_u *p;
5840 int has_string_literal = FALSE;
5841
5842 s = cin_skipcomment(s);
5843 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5844 {
5845 p = cin_skipcomment(skipwhite(s + 6));
5846 while (*p != NUL)
5847 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005848 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005849 {
5850 p = cin_skipcomment(skipwhite(p));
5851 }
5852 else if (*p == '{')
5853 {
5854 break;
5855 }
5856 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5857 {
5858 if (has_string_literal)
5859 return FALSE;
5860 has_string_literal = TRUE;
5861 p += 3;
5862 }
5863 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5864 && p[4] == '"')
5865 {
5866 if (has_string_literal)
5867 return FALSE;
5868 has_string_literal = TRUE;
5869 p += 5;
5870 }
5871 else
5872 {
5873 return FALSE;
5874 }
5875 }
5876 return has_string_literal ? TRUE : FALSE;
5877 }
5878 return FALSE;
5879}
5880
5881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 * Return a pointer to the first non-empty non-comment character after a ':'.
5883 * Return NULL if not found.
5884 * case 234: a = b;
5885 * ^
5886 */
5887 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005888after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 for ( ; *l; ++l)
5891 {
5892 if (*l == ':')
5893 {
5894 if (l[1] == ':') /* skip over "::" for C++ */
5895 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005896 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 break;
5898 }
5899 else if (*l == '\'' && l[1] && l[2] == '\'')
5900 l += 2; /* skip over 'x' */
5901 }
5902 if (*l == NUL)
5903 return NULL;
5904 l = cin_skipcomment(l + 1);
5905 if (*l == NUL)
5906 return NULL;
5907 return l;
5908}
5909
5910/*
5911 * Get indent of line "lnum", skipping a label.
5912 * Return 0 if there is nothing after the label.
5913 */
5914 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005915get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
5917 char_u *l;
5918 pos_T fp;
5919 colnr_T col;
5920 char_u *p;
5921
5922 l = ml_get(lnum);
5923 p = after_label(l);
5924 if (p == NULL)
5925 return 0;
5926
5927 fp.col = (colnr_T)(p - l);
5928 fp.lnum = lnum;
5929 getvcol(curwin, &fp, &col, NULL, NULL);
5930 return (int)col;
5931}
5932
5933/*
5934 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005935 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 * label: if (asdf && asdfasdf)
5937 * ^
5938 */
5939 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005940skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 char_u *l;
5943 int amount;
5944 pos_T cursor_save;
5945
5946 cursor_save = curwin->w_cursor;
5947 curwin->w_cursor.lnum = lnum;
5948 l = ml_get_curline();
5949 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005950 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
5952 amount = get_indent_nolabel(lnum);
5953 l = after_label(ml_get_curline());
5954 if (l == NULL) /* just in case */
5955 l = ml_get_curline();
5956 }
5957 else
5958 {
5959 amount = get_indent();
5960 l = ml_get_curline();
5961 }
5962 *pp = l;
5963
5964 curwin->w_cursor = cursor_save;
5965 return amount;
5966}
5967
5968/*
5969 * Return the indent of the first variable name after a type in a declaration.
5970 * int a, indent of "a"
5971 * static struct foo b, indent of "b"
5972 * enum bla c, indent of "c"
5973 * Returns zero when it doesn't look like a declaration.
5974 */
5975 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005976cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 char_u *line, *p, *s;
5979 int len;
5980 pos_T fp;
5981 colnr_T col;
5982
5983 line = ml_get_curline();
5984 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005985 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5987 {
5988 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005989 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 }
5991 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5992 p = skipwhite(p + 6);
5993 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5994 p = skipwhite(p + 4);
5995 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5996 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5997 {
5998 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01005999 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6000 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6001 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6002 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 p = s;
6004 }
6005 for (len = 0; vim_isIDc(p[len]); ++len)
6006 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006007 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 return 0;
6009
6010 p = skipwhite(p + len);
6011 fp.lnum = curwin->w_cursor.lnum;
6012 fp.col = (colnr_T)(p - line);
6013 getvcol(curwin, &fp, &col, NULL, NULL);
6014 return (int)col;
6015}
6016
6017/*
6018 * Return the indent of the first non-blank after an equal sign.
6019 * char *foo = "here";
6020 * Return zero if no (useful) equal sign found.
6021 * Return -1 if the line above "lnum" ends in a backslash.
6022 * foo = "asdf\
6023 * asdf\
6024 * here";
6025 */
6026 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006027cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 char_u *line;
6030 char_u *s;
6031 colnr_T col;
6032 pos_T fp;
6033
6034 if (lnum > 1)
6035 {
6036 line = ml_get(lnum - 1);
6037 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6038 return -1;
6039 }
6040
6041 line = s = ml_get(lnum);
6042 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6043 {
6044 if (cin_iscomment(s)) /* ignore comments */
6045 s = cin_skipcomment(s);
6046 else
6047 ++s;
6048 }
6049 if (*s != '=')
6050 return 0;
6051
6052 s = skipwhite(s + 1);
6053 if (cin_nocode(s))
6054 return 0;
6055
6056 if (*s == '"') /* nice alignment for continued strings */
6057 ++s;
6058
6059 fp.lnum = lnum;
6060 fp.col = (colnr_T)(s - line);
6061 getvcol(curwin, &fp, &col, NULL, NULL);
6062 return (int)col;
6063}
6064
6065/*
6066 * Recognize a preprocessor statement: Any line that starts with '#'.
6067 */
6068 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006069cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006071 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 return TRUE;
6073 return FALSE;
6074}
6075
6076/*
6077 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6078 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6079 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006080 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 */
6082 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006083cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
6085 char_u *line = *pp;
6086 linenr_T lnum = *lnump;
6087 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006088 int candidate_amount = *amount;
6089
6090 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6091 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006093 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 {
6095 if (cin_ispreproc(line))
6096 {
6097 retval = TRUE;
6098 *lnump = lnum;
6099 break;
6100 }
6101 if (lnum == 1)
6102 break;
6103 line = ml_get(--lnum);
6104 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6105 break;
6106 }
6107
6108 if (lnum != *lnump)
6109 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006110 if (retval)
6111 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 return retval;
6113}
6114
6115/*
6116 * Recognize the start of a C or C++ comment.
6117 */
6118 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006119cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6122}
6123
6124/*
6125 * Recognize the start of a "//" comment.
6126 */
6127 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006128cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
6130 return (p[0] == '/' && p[1] == '/');
6131}
6132
6133/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006134 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6135 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006137 * If a line begins with an "else", only consider it terminated if no unmatched
6138 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 * Return the character terminating the line (ending char's have precedence if
6140 * both apply in order to determine initializations).
6141 */
6142 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006143cin_isterminated(
6144 char_u *s,
6145 int incl_open, /* include '{' at the end as terminator */
6146 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006148 char_u found_start = 0;
6149 unsigned n_open = 0;
6150 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 s = cin_skipcomment(s);
6153
6154 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6155 found_start = *s;
6156
Bram Moolenaar496f9512011-05-19 16:35:09 +02006157 if (!found_start)
6158 is_else = cin_iselse(s);
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 while (*s)
6161 {
6162 /* skip over comments, "" strings and 'c'haracters */
6163 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006164 if (*s == '}' && n_open > 0)
6165 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006166 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006167 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 && cin_nocode(s + 1))
6169 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006170 else if (*s == '{')
6171 {
6172 if (incl_open && cin_nocode(s + 1))
6173 return *s;
6174 else
6175 ++n_open;
6176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 if (*s)
6179 s++;
6180 }
6181 return found_start;
6182}
6183
6184/*
6185 * Recognize the basic picture of a function declaration -- it needs to
6186 * have an open paren somewhere and a close paren at the end of the line and
6187 * no semicolons anywhere.
6188 * When a line ends in a comma we continue looking in the next line.
6189 * "sp" points to a string with the line. When looking at other lines it must
6190 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006191 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006192 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 */
6194 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006195cin_isfuncdecl(
6196 char_u **sp,
6197 linenr_T first_lnum,
6198 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 char_u *s;
6201 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006202 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006204 pos_T *trypos;
6205 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 if (sp == NULL)
6208 s = ml_get(lnum);
6209 else
6210 s = *sp;
6211
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006212 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006213 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006214 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006215 {
6216 lnum = trypos->lnum;
6217 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006218 {
6219 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006220 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006221 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006222
6223 s = ml_get(lnum);
6224 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006225 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006226
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006227 /* Ignore line starting with #. */
6228 if (cin_ispreproc(s))
6229 return FALSE;
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6232 {
6233 if (cin_iscomment(s)) /* ignore comments */
6234 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006235 else if (*s == ':')
6236 {
6237 if (*(s + 1) == ':')
6238 s += 2;
6239 else
6240 /* To avoid a mistake in the following situation:
6241 * A::A(int a, int b)
6242 * : a(0) // <--not a function decl
6243 * , b(0)
6244 * {...
6245 */
6246 return FALSE;
6247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 else
6249 ++s;
6250 }
6251 if (*s != '(')
6252 return FALSE; /* ';', ' or " before any () or no '(' */
6253
6254 while (*s && *s != ';' && *s != '\'' && *s != '"')
6255 {
6256 if (*s == ')' && cin_nocode(s + 1))
6257 {
6258 /* ')' at the end: may have found a match
6259 * Check for he previous line not to end in a backslash:
6260 * #if defined(x) && \
6261 * defined(y)
6262 */
6263 lnum = first_lnum - 1;
6264 s = ml_get(lnum);
6265 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6266 retval = TRUE;
6267 goto done;
6268 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006269 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006271 int comma = (*s == ',');
6272
6273 /* ',' at the end: continue looking in the next line.
6274 * At the end: check for ',' in the next line, for this style:
6275 * func(arg1
6276 * , arg2) */
6277 for (;;)
6278 {
6279 if (lnum >= curbuf->b_ml.ml_line_count)
6280 break;
6281 s = ml_get(++lnum);
6282 if (!cin_ispreproc(s))
6283 break;
6284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (lnum >= curbuf->b_ml.ml_line_count)
6286 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006287 /* Require a comma at end of the line or a comma or ')' at the
6288 * start of next line. */
6289 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006290 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006291 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006292 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294 else if (cin_iscomment(s)) /* ignore comments */
6295 s = cin_skipcomment(s);
6296 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006299 just_started = FALSE;
6300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 }
6302
6303done:
6304 if (lnum != first_lnum && sp != NULL)
6305 *sp = ml_get(first_lnum);
6306
6307 return retval;
6308}
6309
6310 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006311cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006313 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314}
6315
6316 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006317cin_iselse(
6318 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
6320 if (*p == '}') /* accept "} else" */
6321 p = cin_skipcomment(p + 1);
6322 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6323}
6324
6325 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006326cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6329}
6330
6331/*
6332 * Check if this is a "while" that should have a matching "do".
6333 * We only accept a "while (condition) ;", with only white space between the
6334 * ')' and ';'. The condition may be spread over several lines.
6335 */
6336 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006337cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
6339 pos_T cursor_save;
6340 pos_T *trypos;
6341 int retval = FALSE;
6342
6343 p = cin_skipcomment(p);
6344 if (*p == '}') /* accept "} while (cond);" */
6345 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006346 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 {
6348 cursor_save = curwin->w_cursor;
6349 curwin->w_cursor.lnum = lnum;
6350 curwin->w_cursor.col = 0;
6351 p = ml_get_curline();
6352 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6353 {
6354 ++p;
6355 ++curwin->w_cursor.col;
6356 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006357 if ((trypos = findmatchlimit(NULL, 0, 0,
6358 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6360 retval = TRUE;
6361 curwin->w_cursor = cursor_save;
6362 }
6363 return retval;
6364}
6365
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006366/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006367 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006368 * Return 0 if there is none.
6369 * Otherwise return !0 and update "*poffset" to point to the place where the
6370 * string was found.
6371 */
6372 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006373cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006374{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006375 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006376
6377 if (offset-- < 2)
6378 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006379 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006380 --offset;
6381
6382 offset -= 1;
6383 if (!STRNCMP(line + offset, "if", 2))
6384 goto probablyFound;
6385
6386 if (offset >= 1)
6387 {
6388 offset -= 1;
6389 if (!STRNCMP(line + offset, "for", 3))
6390 goto probablyFound;
6391
6392 if (offset >= 2)
6393 {
6394 offset -= 2;
6395 if (!STRNCMP(line + offset, "while", 5))
6396 goto probablyFound;
6397 }
6398 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006399 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006400
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006401probablyFound:
6402 if (!offset || !vim_isIDc(line[offset - 1]))
6403 {
6404 *poffset = offset;
6405 return 1;
6406 }
6407 return 0;
6408}
6409
6410/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006411 * Return TRUE if we are at the end of a do-while.
6412 * do
6413 * nothing;
6414 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006415 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006416 * Adjust the cursor to the line with "while".
6417 */
6418 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006419cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006420{
6421 char_u *line;
6422 char_u *p;
6423 char_u *s;
6424 pos_T *trypos;
6425 int i;
6426
6427 if (terminated != ';') /* there must be a ';' at the end */
6428 return FALSE;
6429
6430 p = line = ml_get_curline();
6431 while (*p != NUL)
6432 {
6433 p = cin_skipcomment(p);
6434 if (*p == ')')
6435 {
6436 s = skipwhite(p + 1);
6437 if (*s == ';' && cin_nocode(s + 1))
6438 {
6439 /* Found ");" at end of the line, now check there is "while"
6440 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006441 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006442 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006443 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006444 if (trypos != NULL)
6445 {
6446 s = cin_skipcomment(ml_get(trypos->lnum));
6447 if (*s == '}') /* accept "} while (cond);" */
6448 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006449 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006450 {
6451 curwin->w_cursor.lnum = trypos->lnum;
6452 return TRUE;
6453 }
6454 }
6455
6456 /* Searching may have made "line" invalid, get it again. */
6457 line = ml_get_curline();
6458 p = line + i;
6459 }
6460 }
6461 if (*p != NUL)
6462 ++p;
6463 }
6464 return FALSE;
6465}
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6471}
6472
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006473/*
6474 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 * constructor-initialization. eg:
6476 *
6477 * class MyClass :
6478 * baseClass <-- here
6479 * class MyClass : public baseClass,
6480 * anotherBaseClass <-- here (should probably lineup ??)
6481 * MyClass::MyClass(...) :
6482 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006483 *
6484 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 */
6486 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006487cin_is_cpp_baseclass(
6488 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006490 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 char_u *s;
6492 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006493 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006494 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006496 if (pos->lnum <= lnum)
6497 return cached->found; /* Use the cached result */
6498
6499 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006501 s = skipwhite(line);
6502 if (*s == '#') /* skip #define FOO x ? (x) : x */
6503 return FALSE;
6504 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (*s == NUL)
6506 return FALSE;
6507
6508 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6509
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006510 /* Search for a line starting with '#', empty, ending in ';' or containing
6511 * '{' or '}' and start below it. This handles the following situations:
6512 * a = cond ?
6513 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006514 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006515 * func::foo()
6516 * : something
6517 * {}
6518 * Foo::Foo (int one, int two)
6519 * : something(4),
6520 * somethingelse(3)
6521 * {}
6522 */
6523 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006525 line = ml_get(lnum - 1);
6526 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006527 if (*s == '#' || *s == NUL)
6528 break;
6529 while (*s != NUL)
6530 {
6531 s = cin_skipcomment(s);
6532 if (*s == '{' || *s == '}'
6533 || (*s == ';' && cin_nocode(s + 1)))
6534 break;
6535 if (*s != NUL)
6536 ++s;
6537 }
6538 if (*s != NUL)
6539 break;
6540 --lnum;
6541 }
6542
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006543 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006544 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006545 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006546 for (;;)
6547 {
6548 if (*s == NUL)
6549 {
6550 if (lnum == curwin->w_cursor.lnum)
6551 break;
6552 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006553 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006554 s = line;
6555 }
6556 if (s == line)
6557 {
6558 /* don't recognize "case (foo):" as a baseclass */
6559 if (cin_iscase(s, FALSE))
6560 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006561 s = cin_skipcomment(line);
6562 if (*s == NUL)
6563 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006564 }
6565
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006566 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006567 s = skip_string(s) + 1;
6568 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
6570 if (s[1] == ':')
6571 {
6572 /* skip double colon. It can't be a constructor
6573 * initialization any more */
6574 lookfor_ctor_init = FALSE;
6575 s = cin_skipcomment(s + 2);
6576 }
6577 else if (lookfor_ctor_init || class_or_struct)
6578 {
6579 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006580 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 cpp_base_class = TRUE;
6582 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006583 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 s = cin_skipcomment(s + 1);
6585 }
6586 else
6587 s = cin_skipcomment(s + 1);
6588 }
6589 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6590 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6591 {
6592 class_or_struct = TRUE;
6593 lookfor_ctor_init = FALSE;
6594
6595 if (*s == 'c')
6596 s = cin_skipcomment(s + 5);
6597 else
6598 s = cin_skipcomment(s + 6);
6599 }
6600 else
6601 {
6602 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6603 {
6604 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6605 }
6606 else if (s[0] == ')')
6607 {
6608 /* Constructor-initialization is assumed if we come across
6609 * something like "):" */
6610 class_or_struct = FALSE;
6611 lookfor_ctor_init = TRUE;
6612 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006613 else if (s[0] == '?')
6614 {
6615 /* Avoid seeing '() :' after '?' as constructor init. */
6616 return FALSE;
6617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 else if (!vim_isIDc(s[0]))
6619 {
6620 /* if it is not an identifier, we are wrong */
6621 class_or_struct = FALSE;
6622 lookfor_ctor_init = FALSE;
6623 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006624 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 {
6626 /* it can't be a constructor-initialization any more */
6627 lookfor_ctor_init = FALSE;
6628
6629 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006630 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006631 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 }
6633
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006634 /* When the line ends in a comma don't align with it. */
6635 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006636 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006637
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 s = cin_skipcomment(s + 1);
6639 }
6640 }
6641
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006642 cached->found = cpp_base_class;
6643 if (cpp_base_class)
6644 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 return cpp_base_class;
6646}
6647
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006648 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006649get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006650{
6651 int amount;
6652 colnr_T vcol;
6653 pos_T *trypos;
6654
6655 if (col == 0)
6656 {
6657 amount = get_indent();
6658 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006659 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006660 amount = get_indent_lnum(trypos->lnum); /* XXX */
6661 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006662 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006663 }
6664 else
6665 {
6666 curwin->w_cursor.col = col;
6667 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6668 amount = (int)vcol;
6669 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006670 if (amount < curbuf->b_ind_cpp_baseclass)
6671 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006672 return amount;
6673}
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675/*
6676 * Return TRUE if string "s" ends with the string "find", possibly followed by
6677 * white space and comments. Skip strings and comments.
6678 * Ignore "ignore" after "find" if it's not NULL.
6679 */
6680 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006681cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 char_u *p = s;
6684 char_u *r;
6685 int len = (int)STRLEN(find);
6686
6687 while (*p != NUL)
6688 {
6689 p = cin_skipcomment(p);
6690 if (STRNCMP(p, find, len) == 0)
6691 {
6692 r = skipwhite(p + len);
6693 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6694 r = skipwhite(r + STRLEN(ignore));
6695 if (cin_nocode(r))
6696 return TRUE;
6697 }
6698 if (*p != NUL)
6699 ++p;
6700 }
6701 return FALSE;
6702}
6703
6704/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006705 * Return TRUE when "s" starts with "word" and then a non-ID character.
6706 */
6707 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006708cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006709{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006710 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006711
6712 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6713}
6714
6715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 * Skip strings, chars and comments until at or past "trypos".
6717 * Return the column found.
6718 */
6719 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006720cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 char_u *line;
6723 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006724 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
6726 p = line = ml_get(trypos->lnum);
6727 while (*p && (colnr_T)(p - line) < trypos->col)
6728 {
6729 if (cin_iscomment(p))
6730 p = cin_skipcomment(p);
6731 else
6732 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006733 new_p = skip_string(p);
6734 if (new_p == p)
6735 ++p;
6736 else
6737 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739 }
6740 return (int)(p - line);
6741}
6742
6743/*
6744 * Find the '{' at the start of the block we are in.
6745 * Return NULL if no match found.
6746 * Ignore a '{' that is in a comment, makes indenting the next three lines
6747 * work. */
6748/* foo() */
6749/* { */
6750/* } */
6751
6752 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006753find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 pos_T cursor_save;
6756 pos_T *trypos;
6757 pos_T *pos;
6758 static pos_T pos_copy;
6759
6760 cursor_save = curwin->w_cursor;
6761 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6762 {
6763 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6764 trypos = &pos_copy;
6765 curwin->w_cursor = *trypos;
6766 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006767 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006769 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 break;
6771 if (pos != NULL)
6772 curwin->w_cursor.lnum = pos->lnum;
6773 }
6774 curwin->w_cursor = cursor_save;
6775 return trypos;
6776}
6777
6778/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006779 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006780 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 */
6782 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006783find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006785 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006786}
6787
6788 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006789find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006790{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 pos_T cursor_save;
6792 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006793 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006794 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006797 ind_maxp_wk = ind_maxparen;
6798retry:
6799 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
6801 /* check if the ( is in a // comment */
6802 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006803 {
6804 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6805 if (ind_maxp_wk > 0)
6806 {
6807 curwin->w_cursor = *trypos;
6808 curwin->w_cursor.col = 0; /* XXX */
6809 goto retry;
6810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 else
6814 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006815 pos_T *trypos_wk;
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6818 trypos = &pos_copy;
6819 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006820 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006821 {
6822 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6823 - trypos_wk->lnum);
6824 if (ind_maxp_wk > 0)
6825 {
6826 curwin->w_cursor = *trypos_wk;
6827 goto retry;
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832 }
6833 curwin->w_cursor = cursor_save;
6834 return trypos;
6835}
6836
6837/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006838 * Find the matching '(', ignoring it if it is in a comment or before an
6839 * unmatched {.
6840 * Return NULL if no match found.
6841 */
6842 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006843find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006844{
6845 pos_T *trypos = find_match_paren(ind_maxparen);
6846
6847 if (trypos != NULL)
6848 {
6849 pos_T *tryposBrace = find_start_brace();
6850
6851 /* If both an unmatched '(' and '{' is found. Ignore the '('
6852 * position if the '{' is further down. */
6853 if (tryposBrace != NULL
6854 && (trypos->lnum != tryposBrace->lnum
6855 ? trypos->lnum < tryposBrace->lnum
6856 : trypos->col < tryposBrace->col))
6857 trypos = NULL;
6858 }
6859 return trypos;
6860}
6861
6862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 * Return ind_maxparen corrected for the difference in line number between the
6864 * cursor position and "startpos". This makes sure that searching for a
6865 * matching paren above the cursor line doesn't find a match because of
6866 * looking a few lines further.
6867 */
6868 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006869corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6872
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006873 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6874 return curbuf->b_ind_maxparen - (int)n;
6875 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876}
6877
6878/*
6879 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006880 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 */
6882 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006883find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 int i;
6886 int retval = FALSE;
6887 int open_count = 0;
6888
6889 curwin->w_cursor.col = 0; /* default is start of line */
6890
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006891 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {
6893 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6894 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6895 if (l[i] == start)
6896 ++open_count;
6897 else if (l[i] == end)
6898 {
6899 if (open_count > 0)
6900 --open_count;
6901 else
6902 {
6903 curwin->w_cursor.col = i;
6904 retval = TRUE;
6905 }
6906 }
6907 }
6908 return retval;
6909}
6910
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006911/*
6912 * Parse 'cinoptions' and set the values in "curbuf".
6913 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6914 */
6915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006916parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006917{
6918 char_u *p;
6919 char_u *l;
6920 char_u *digits;
6921 int n;
6922 int divider;
6923 int fraction = 0;
6924 int sw = (int)get_sw_value(buf);
6925
6926 /*
6927 * Set the default values.
6928 */
6929 /* Spaces from a block's opening brace the prevailing indent for that
6930 * block should be. */
6931 buf->b_ind_level = sw;
6932
6933 /* Spaces from the edge of the line an open brace that's at the end of a
6934 * line is imagined to be. */
6935 buf->b_ind_open_imag = 0;
6936
6937 /* Spaces from the prevailing indent for a line that is not preceded by
6938 * an opening brace. */
6939 buf->b_ind_no_brace = 0;
6940
6941 /* Column where the first { of a function should be located }. */
6942 buf->b_ind_first_open = 0;
6943
6944 /* Spaces from the prevailing indent a leftmost open brace should be
6945 * located. */
6946 buf->b_ind_open_extra = 0;
6947
6948 /* Spaces from the matching open brace (real location for one at the left
6949 * edge; imaginary location from one that ends a line) the matching close
6950 * brace should be located. */
6951 buf->b_ind_close_extra = 0;
6952
6953 /* Spaces from the edge of the line an open brace sitting in the leftmost
6954 * column is imagined to be. */
6955 buf->b_ind_open_left_imag = 0;
6956
6957 /* Spaces jump labels should be shifted to the left if N is non-negative,
6958 * otherwise the jump label will be put to column 1. */
6959 buf->b_ind_jump_label = -1;
6960
6961 /* Spaces from the switch() indent a "case xx" label should be located. */
6962 buf->b_ind_case = sw;
6963
6964 /* Spaces from the "case xx:" code after a switch() should be located. */
6965 buf->b_ind_case_code = sw;
6966
6967 /* Lineup break at end of case in switch() with case label. */
6968 buf->b_ind_case_break = 0;
6969
6970 /* Spaces from the class declaration indent a scope declaration label
6971 * should be located. */
6972 buf->b_ind_scopedecl = sw;
6973
6974 /* Spaces from the scope declaration label code should be located. */
6975 buf->b_ind_scopedecl_code = sw;
6976
6977 /* Amount K&R-style parameters should be indented. */
6978 buf->b_ind_param = sw;
6979
6980 /* Amount a function type spec should be indented. */
6981 buf->b_ind_func_type = sw;
6982
6983 /* Amount a cpp base class declaration or constructor initialization
6984 * should be indented. */
6985 buf->b_ind_cpp_baseclass = sw;
6986
6987 /* additional spaces beyond the prevailing indent a continuation line
6988 * should be located. */
6989 buf->b_ind_continuation = sw;
6990
6991 /* Spaces from the indent of the line with an unclosed parentheses. */
6992 buf->b_ind_unclosed = sw * 2;
6993
6994 /* Spaces from the indent of the line with an unclosed parentheses, which
6995 * itself is also unclosed. */
6996 buf->b_ind_unclosed2 = sw;
6997
6998 /* Suppress ignoring spaces from the indent of a line starting with an
6999 * unclosed parentheses. */
7000 buf->b_ind_unclosed_noignore = 0;
7001
7002 /* If the opening paren is the last nonwhite character on the line, and
7003 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7004 * context (for very long lines). */
7005 buf->b_ind_unclosed_wrapped = 0;
7006
7007 /* Suppress ignoring white space when lining up with the character after
7008 * an unclosed parentheses. */
7009 buf->b_ind_unclosed_whiteok = 0;
7010
7011 /* Indent a closing parentheses under the line start of the matching
7012 * opening parentheses. */
7013 buf->b_ind_matching_paren = 0;
7014
7015 /* Indent a closing parentheses under the previous line. */
7016 buf->b_ind_paren_prev = 0;
7017
7018 /* Extra indent for comments. */
7019 buf->b_ind_comment = 0;
7020
7021 /* Spaces from the comment opener when there is nothing after it. */
7022 buf->b_ind_in_comment = 3;
7023
7024 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7025 * after the comment opener. */
7026 buf->b_ind_in_comment2 = 0;
7027
7028 /* Max lines to search for an open paren. */
7029 buf->b_ind_maxparen = 20;
7030
7031 /* Max lines to search for an open comment. */
7032 buf->b_ind_maxcomment = 70;
7033
7034 /* Handle braces for java code. */
7035 buf->b_ind_java = 0;
7036
7037 /* Not to confuse JS object properties with labels. */
7038 buf->b_ind_js = 0;
7039
7040 /* Handle blocked cases correctly. */
7041 buf->b_ind_keep_case_label = 0;
7042
7043 /* Handle C++ namespace. */
7044 buf->b_ind_cpp_namespace = 0;
7045
7046 /* Handle continuation lines containing conditions of if(), for() and
7047 * while(). */
7048 buf->b_ind_if_for_while = 0;
7049
Bram Moolenaar6b643942017-03-05 19:44:06 +01007050 /* indentation for # comments */
7051 buf->b_ind_hash_comment = 0;
7052
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007053 /* Handle C++ extern "C" or "C++" */
7054 buf->b_ind_cpp_extern_c = 0;
7055
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007056 for (p = buf->b_p_cino; *p; )
7057 {
7058 l = p++;
7059 if (*p == '-')
7060 ++p;
7061 digits = p; /* remember where the digits start */
7062 n = getdigits(&p);
7063 divider = 0;
7064 if (*p == '.') /* ".5s" means a fraction */
7065 {
7066 fraction = atol((char *)++p);
7067 while (VIM_ISDIGIT(*p))
7068 {
7069 ++p;
7070 if (divider)
7071 divider *= 10;
7072 else
7073 divider = 10;
7074 }
7075 }
7076 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7077 {
7078 if (p == digits)
7079 n = sw; /* just "s" is one 'shiftwidth' */
7080 else
7081 {
7082 n *= sw;
7083 if (divider)
7084 n += (sw * fraction + divider / 2) / divider;
7085 }
7086 ++p;
7087 }
7088 if (l[1] == '-')
7089 n = -n;
7090
7091 /* When adding an entry here, also update the default 'cinoptions' in
7092 * doc/indent.txt, and add explanation for it! */
7093 switch (*l)
7094 {
7095 case '>': buf->b_ind_level = n; break;
7096 case 'e': buf->b_ind_open_imag = n; break;
7097 case 'n': buf->b_ind_no_brace = n; break;
7098 case 'f': buf->b_ind_first_open = n; break;
7099 case '{': buf->b_ind_open_extra = n; break;
7100 case '}': buf->b_ind_close_extra = n; break;
7101 case '^': buf->b_ind_open_left_imag = n; break;
7102 case 'L': buf->b_ind_jump_label = n; break;
7103 case ':': buf->b_ind_case = n; break;
7104 case '=': buf->b_ind_case_code = n; break;
7105 case 'b': buf->b_ind_case_break = n; break;
7106 case 'p': buf->b_ind_param = n; break;
7107 case 't': buf->b_ind_func_type = n; break;
7108 case '/': buf->b_ind_comment = n; break;
7109 case 'c': buf->b_ind_in_comment = n; break;
7110 case 'C': buf->b_ind_in_comment2 = n; break;
7111 case 'i': buf->b_ind_cpp_baseclass = n; break;
7112 case '+': buf->b_ind_continuation = n; break;
7113 case '(': buf->b_ind_unclosed = n; break;
7114 case 'u': buf->b_ind_unclosed2 = n; break;
7115 case 'U': buf->b_ind_unclosed_noignore = n; break;
7116 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7117 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7118 case 'm': buf->b_ind_matching_paren = n; break;
7119 case 'M': buf->b_ind_paren_prev = n; break;
7120 case ')': buf->b_ind_maxparen = n; break;
7121 case '*': buf->b_ind_maxcomment = n; break;
7122 case 'g': buf->b_ind_scopedecl = n; break;
7123 case 'h': buf->b_ind_scopedecl_code = n; break;
7124 case 'j': buf->b_ind_java = n; break;
7125 case 'J': buf->b_ind_js = n; break;
7126 case 'l': buf->b_ind_keep_case_label = n; break;
7127 case '#': buf->b_ind_hash_comment = n; break;
7128 case 'N': buf->b_ind_cpp_namespace = n; break;
7129 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007130 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007131 }
7132 if (*p == ',')
7133 ++p;
7134 }
7135}
7136
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007137/*
7138 * Return the desired indent for C code.
7139 * Return -1 if the indent should be left alone (inside a raw string).
7140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007142get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 pos_T cur_curpos;
7145 int amount;
7146 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007147 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 colnr_T col;
7149 char_u *theline;
7150 char_u *linecopy;
7151 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007152 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007154 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 pos_T our_paren_pos;
7156 char_u *start;
7157 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007158#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159#define BRACE_AT_START 2 /* '{' is at start of line */
7160#define BRACE_AT_END 3 /* '{' is at end of line */
7161 linenr_T ourscope;
7162 char_u *l;
7163 char_u *look;
7164 char_u terminated;
7165 int lookfor;
7166#define LOOKFOR_INITIAL 0
7167#define LOOKFOR_IF 1
7168#define LOOKFOR_DO 2
7169#define LOOKFOR_CASE 3
7170#define LOOKFOR_ANY 4
7171#define LOOKFOR_TERM 5
7172#define LOOKFOR_UNTERM 6
7173#define LOOKFOR_SCOPEDECL 7
7174#define LOOKFOR_NOBREAK 8
7175#define LOOKFOR_CPP_BASECLASS 9
7176#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007177#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007178#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
7180 int whilelevel;
7181 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 int n;
7183 int iscase;
7184 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007185 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007187 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007188 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007189 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007190 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007191 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007193 /* make a copy, value is changed below */
7194 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
7196 /* remember where the cursor was when we started */
7197 cur_curpos = curwin->w_cursor;
7198
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007199 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007200 if (cur_curpos.lnum == 1)
7201 return 0;
7202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 /* Get a copy of the current contents of the line.
7204 * This is required, because only the most recent line obtained with
7205 * ml_get is valid! */
7206 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7207 if (linecopy == NULL)
7208 return 0;
7209
7210 /*
7211 * In insert mode and the cursor is on a ')' truncate the line at the
7212 * cursor position. We don't want to line up with the matching '(' when
7213 * inserting new stuff.
7214 * For unknown reasons the cursor might be past the end of the line, thus
7215 * check for that.
7216 */
7217 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007218 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 && linecopy[curwin->w_cursor.col] == ')')
7220 linecopy[curwin->w_cursor.col] = NUL;
7221
7222 theline = skipwhite(linecopy);
7223
7224 /* move the cursor to the start of the line */
7225
7226 curwin->w_cursor.col = 0;
7227
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007228 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007231 * If we are inside a raw string don't change the indent.
7232 * Ignore a raw string inside a comment.
7233 */
7234 comment_pos = ind_find_start_comment();
7235 if (comment_pos != NULL)
7236 {
7237 /* findmatchlimit() static pos is overwritten, make a copy */
7238 tryposCopy = *comment_pos;
7239 comment_pos = &tryposCopy;
7240 }
7241 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007242 if (trypos != NULL && (comment_pos == NULL
7243 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007244 {
7245 amount = -1;
7246 goto laterend;
7247 }
7248
7249 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 * #defines and so on always go at the left when included in 'cinkeys'.
7251 */
7252 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007253 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007254 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007255 goto theend;
7256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
7258 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007259 * Is it a non-case label? Then that goes at the left margin too unless:
7260 * - JS flag is set.
7261 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007263 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007264 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007267 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
7269
7270 /*
7271 * If we're inside a "//" comment and there is a "//" comment in a
7272 * previous line, lineup with that one.
7273 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007274 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 && (trypos = find_line_comment()) != NULL) /* XXX */
7276 {
7277 /* find how indented the line beginning the comment is */
7278 getvcol(curwin, trypos, &col, NULL, NULL);
7279 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007280 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282
7283 /*
7284 * If we're inside a comment and not looking at the start of the
7285 * comment, try using the 'comments' option.
7286 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007287 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
7289 int lead_start_len = 2;
7290 int lead_middle_len = 1;
7291 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7292 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7293 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7294 char_u *p;
7295 int start_align = 0;
7296 int start_off = 0;
7297 int done = FALSE;
7298
7299 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007300 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007302 *lead_start = NUL;
7303 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304
7305 p = curbuf->b_p_com;
7306 while (*p != NUL)
7307 {
7308 int align = 0;
7309 int off = 0;
7310 int what = 0;
7311
7312 while (*p != NUL && *p != ':')
7313 {
7314 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7315 what = *p++;
7316 else if (*p == COM_LEFT || *p == COM_RIGHT)
7317 align = *p++;
7318 else if (VIM_ISDIGIT(*p) || *p == '-')
7319 off = getdigits(&p);
7320 else
7321 ++p;
7322 }
7323
7324 if (*p == ':')
7325 ++p;
7326 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7327 if (what == COM_START)
7328 {
7329 STRCPY(lead_start, lead_end);
7330 lead_start_len = (int)STRLEN(lead_start);
7331 start_off = off;
7332 start_align = align;
7333 }
7334 else if (what == COM_MIDDLE)
7335 {
7336 STRCPY(lead_middle, lead_end);
7337 lead_middle_len = (int)STRLEN(lead_middle);
7338 }
7339 else if (what == COM_END)
7340 {
7341 /* If our line starts with the middle comment string, line it
7342 * up with the comment opener per the 'comments' option. */
7343 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7344 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7345 {
7346 done = TRUE;
7347 if (curwin->w_cursor.lnum > 1)
7348 {
7349 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007350 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 * the middle comment string matches in the previous
7352 * line, use the indent of that line. XXX */
7353 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7354 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7355 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7356 else if (STRNCMP(look, lead_middle,
7357 lead_middle_len) == 0)
7358 {
7359 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7360 break;
7361 }
7362 /* If the start comment string doesn't match with the
7363 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007364 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 lead_start, lead_start_len) != 0)
7366 continue;
7367 }
7368 if (start_off != 0)
7369 amount += start_off;
7370 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007371 amount += vim_strsize(lead_start)
7372 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 break;
7374 }
7375
7376 /* If our line starts with the end comment string, line it up
7377 * with the middle comment */
7378 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7379 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7380 {
7381 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7382 /* XXX */
7383 if (off != 0)
7384 amount += off;
7385 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007386 amount += vim_strsize(lead_start)
7387 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 done = TRUE;
7389 break;
7390 }
7391 }
7392 }
7393
7394 /* If our line starts with an asterisk, line up with the
7395 * asterisk in the comment opener; otherwise, line up
7396 * with the first character of the comment text.
7397 */
7398 if (done)
7399 ;
7400 else if (theline[0] == '*')
7401 amount += 1;
7402 else
7403 {
7404 /*
7405 * If we are more than one line away from the comment opener, take
7406 * the indent of the previous non-empty line. If 'cino' has "CO"
7407 * and we are just below the comment opener and there are any
7408 * white characters after it line up with the text after it;
7409 * otherwise, add the amount specified by "c" in 'cino'
7410 */
7411 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007412 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
7414 if (linewhite(lnum)) /* skip blank lines */
7415 continue;
7416 amount = get_indent_lnum(lnum); /* XXX */
7417 break;
7418 }
7419 if (amount == -1) /* use the comment opener */
7420 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007421 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007423 start = ml_get(comment_pos->lnum);
7424 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007426 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007428 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007430 if (curbuf->b_ind_in_comment2 || *look == NUL)
7431 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
7433 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007434 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
7436
7437 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007438 * Are we looking at a ']' that has a match?
7439 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007440 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007441 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7442 {
7443 /* align with the line containing the '['. */
7444 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007445 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007446 }
7447
7448 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 * Are we inside parentheses or braces?
7450 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007451 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007452 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007453 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 || trypos != NULL)
7455 {
7456 if (trypos != NULL && tryposBrace != NULL)
7457 {
7458 /* Both an unmatched '(' and '{' is found. Use the one which is
7459 * closer to the current cursor position, set the other to NULL. */
7460 if (trypos->lnum != tryposBrace->lnum
7461 ? trypos->lnum < tryposBrace->lnum
7462 : trypos->col < tryposBrace->col)
7463 trypos = NULL;
7464 else
7465 tryposBrace = NULL;
7466 }
7467
7468 if (trypos != NULL)
7469 {
7470 /*
7471 * If the matching paren is more than one line away, use the indent of
7472 * a previous non-empty line that matches the same paren.
7473 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007474 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007476 /* Line up with the start of the matching paren line. */
7477 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7478 }
7479 else
7480 {
7481 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007482 our_paren_pos = *trypos;
7483 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007485 l = skipwhite(ml_get(lnum));
7486 if (cin_nocode(l)) /* skip comment lines */
7487 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007488 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007489 continue; /* ignore #define, #if, etc. */
7490 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007492 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007493 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007494 {
7495 lnum = trypos->lnum + 1;
7496 continue;
7497 }
7498
7499 /* XXX */
7500 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007501 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007502 && trypos->lnum == our_paren_pos.lnum
7503 && trypos->col == our_paren_pos.col)
7504 {
7505 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007507 if (theline[0] == ')')
7508 {
7509 if (our_paren_pos.lnum != lnum
7510 && cur_amount > amount)
7511 cur_amount = amount;
7512 amount = -1;
7513 }
7514 break;
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 }
7518
7519 /*
7520 * Line up with line where the matching paren is. XXX
7521 * If the line starts with a '(' or the indent for unclosed
7522 * parentheses is zero, line up with the unclosed parentheses.
7523 */
7524 if (amount == -1)
7525 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007526 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007527 int is_if_for_while = 0;
7528
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007529 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007530 {
7531 /* Look for the outermost opening parenthesis on this line
7532 * and check whether it belongs to an "if", "for" or "while". */
7533
7534 pos_T cursor_save = curwin->w_cursor;
7535 pos_T outermost;
7536 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007537
7538 trypos = &our_paren_pos;
7539 do {
7540 outermost = *trypos;
7541 curwin->w_cursor.lnum = outermost.lnum;
7542 curwin->w_cursor.col = outermost.col;
7543
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007544 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007545 } while (trypos && trypos->lnum == outermost.lnum);
7546
7547 curwin->w_cursor = cursor_save;
7548
7549 line = ml_get(outermost.lnum);
7550
7551 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007552 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007553 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007554
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007555 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007556 look = skipwhite(look);
7557 if (*look == '(')
7558 {
7559 linenr_T save_lnum = curwin->w_cursor.lnum;
7560 char_u *line;
7561 int look_col;
7562
7563 /* Ignore a '(' in front of the line that has a match before
7564 * our matching '('. */
7565 curwin->w_cursor.lnum = our_paren_pos.lnum;
7566 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007567 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007568 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007569 if ((trypos = findmatchlimit(NULL, ')', 0,
7570 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007571 != NULL
7572 && trypos->lnum == our_paren_pos.lnum
7573 && trypos->col < our_paren_pos.col)
7574 ignore_paren_col = trypos->col + 1;
7575
7576 curwin->w_cursor.lnum = save_lnum;
7577 look = ml_get(our_paren_pos.lnum) + look_col;
7578 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007579 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7580 && is_if_for_while == 0)
7581 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007582 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 {
7584 /*
7585 * If we're looking at a close paren, line up right there;
7586 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007587 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 * the last nonwhite character of the line, use either the
7589 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007590 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 * lines).
7592 */
7593 if (theline[0] != ')')
7594 {
7595 cur_amount = MAXCOL;
7596 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007597 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 && cin_ends_in(l, (char_u *)"(", NULL))
7599 {
7600 /* look for opening unmatched paren, indent one level
7601 * for each additional level */
7602 n = 1;
7603 for (col = 0; col < our_paren_pos.col; ++col)
7604 {
7605 switch (l[col])
7606 {
7607 case '(':
7608 case '{': ++n;
7609 break;
7610
7611 case ')':
7612 case '}': if (n > 1)
7613 --n;
7614 break;
7615 }
7616 }
7617
7618 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007619 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007621 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 our_paren_pos.col++;
7623 else
7624 {
7625 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007626 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 col++;
7628 if (l[col] != NUL) /* In case of trailing space */
7629 our_paren_pos.col = col;
7630 else
7631 our_paren_pos.col++;
7632 }
7633 }
7634
7635 /*
7636 * Find how indented the paren is, or the character after it
7637 * if we did the above "if".
7638 */
7639 if (our_paren_pos.col > 0)
7640 {
7641 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7642 if (cur_amount > (int)col)
7643 cur_amount = col;
7644 }
7645 }
7646
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007647 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {
7649 /* Line up with the start of the matching paren line. */
7650 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007651 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7652 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007653 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {
7655 if (cur_amount != MAXCOL)
7656 amount = cur_amount;
7657 }
7658 else
7659 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007660 /* Add b_ind_unclosed2 for each '(' before our matching one,
7661 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007663 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
7665 --our_paren_pos.col;
7666 switch (*ml_get_pos(&our_paren_pos))
7667 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007668 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 col = our_paren_pos.col;
7670 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007671 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 col = MAXCOL;
7673 break;
7674 }
7675 }
7676
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007677 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 * braces */
7679 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007680 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 else
7682 {
7683 curwin->w_cursor.lnum = our_paren_pos.lnum;
7684 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007685 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7686 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007687 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007689 {
7690 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007691 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007692 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007693 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
7696 /*
7697 * For a line starting with ')' use the minimum of the two
7698 * positions, to avoid giving it more indent than the previous
7699 * lines:
7700 * func_long_name( if (x
7701 * arg && yy
7702 * ) ^ not here ) ^ not here
7703 */
7704 if (cur_amount < amount)
7705 amount = cur_amount;
7706 }
7707 }
7708
7709 /* add extra indent for a comment */
7710 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007711 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 else
7714 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007715 /*
7716 * We are inside braces, there is a { before this line at the position
7717 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007718 * Make a copy of tryposBrace, it may point to pos_copy inside
7719 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007720 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007721 tryposCopy = *tryposBrace;
7722 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 ourscope = trypos->lnum;
7725 start = ml_get(ourscope);
7726
7727 /*
7728 * Now figure out how indented the line is in general.
7729 * If the brace was at the start of the line, we use that;
7730 * otherwise, check out the indentation of the line as
7731 * a whole and then add the "imaginary indent" to that.
7732 */
7733 look = skipwhite(start);
7734 if (*look == '{')
7735 {
7736 getvcol(curwin, trypos, &col, NULL, NULL);
7737 amount = col;
7738 if (*start == '{')
7739 start_brace = BRACE_IN_COL0;
7740 else
7741 start_brace = BRACE_AT_START;
7742 }
7743 else
7744 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007745 /* That opening brace might have been on a continuation
7746 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 curwin->w_cursor.lnum = ourscope;
7748
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007749 /* Position the cursor over the rightmost paren, so that
7750 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 lnum = ourscope;
7752 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007753 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7754 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 lnum = trypos->lnum;
7756
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007757 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 * case 1: if (asdf &&
7759 * ldfd) {
7760 * }
7761 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007762 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7763 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007765 else if (curbuf->b_ind_js)
7766 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007768 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
7770 start_brace = BRACE_AT_END;
7771 }
7772
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007773 /* For Javascript check if the line starts with "key:". */
7774 if (curbuf->b_ind_js)
7775 js_cur_has_key = cin_has_js_key(theline);
7776
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007778 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 * we want to be. otherwise, add the amount of room
7780 * that an indent is supposed to be.
7781 */
7782 if (theline[0] == '}')
7783 {
7784 /*
7785 * they may want closing braces to line up with something
7786 * other than the open brace. indulge them, if so.
7787 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007788 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
7790 else
7791 {
7792 /*
7793 * If we're looking at an "else", try to find an "if"
7794 * to match it with.
7795 * If we're looking at a "while", try to find a "do"
7796 * to match it with.
7797 */
7798 lookfor = LOOKFOR_INITIAL;
7799 if (cin_iselse(theline))
7800 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007801 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 lookfor = LOOKFOR_DO;
7803 if (lookfor != LOOKFOR_INITIAL)
7804 {
7805 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007806 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {
7808 amount = get_indent(); /* XXX */
7809 goto theend;
7810 }
7811 }
7812
7813 /*
7814 * We get here if we are not on an "while-of-do" or "else" (or
7815 * failed to find a matching "if").
7816 * Search backwards for something to line up with.
7817 * First set amount for when we don't find anything.
7818 */
7819
7820 /*
7821 * if the '{' is _really_ at the left margin, use the imaginary
7822 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007823 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 */
7825
7826 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7827 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007828 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007829 lookfor_cpp_namespace = TRUE;
7830 }
7831 else if (start_brace == BRACE_AT_START &&
7832 lookfor_cpp_namespace) /* '{' is at start */
7833 {
7834
7835 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
7837 else
7838 {
7839 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007840 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007841 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007842
7843 l = skipwhite(ml_get_curline());
7844 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007845 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007846 else if (cin_is_cpp_extern_c(l))
7847 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 else
7850 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007851 /* Compensate for adding b_ind_open_extra later. */
7852 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (amount < 0)
7854 amount = 0;
7855 }
7856 }
7857
7858 lookfor_break = FALSE;
7859
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007860 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
7862 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007863 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865 else if (cin_isscopedecl(theline)) /* private:, ... */
7866 {
7867 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007868 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870 else
7871 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007872 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7873 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 lookfor_break = TRUE;
7875
7876 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007877 /* b_ind_level from start of block */
7878 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880 scope_amount = amount;
7881 whilelevel = 0;
7882
7883 /*
7884 * Search backwards. If we find something we recognize, line up
7885 * with that.
7886 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007887 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 * the usual amount relative to the conditional
7889 * that opens the block.
7890 */
7891 curwin->w_cursor = cur_curpos;
7892 for (;;)
7893 {
7894 curwin->w_cursor.lnum--;
7895 curwin->w_cursor.col = 0;
7896
7897 /*
7898 * If we went all the way back to the start of our scope, line
7899 * up with it.
7900 */
7901 if (curwin->w_cursor.lnum <= ourscope)
7902 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007903 /* We reached end of scope:
7904 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007906 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 * don't add ind_continuation, otherwise it is a variable
7908 * declaration:
7909 * int x,
7910 * here; <-- add ind_continuation
7911 */
7912 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7913 {
7914 if (curwin->w_cursor.lnum == 0
7915 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007916 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007918 /* nothing found (abuse curbuf->b_ind_maxparen as
7919 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 * initialization) */
7921 if (cont_amount > 0)
7922 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007923 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 amount += ind_continuation;
7925 break;
7926 }
7927
7928 l = ml_get_curline();
7929
7930 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007931 * If we're in a comment or raw string now, skip to
7932 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007934 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 if (trypos != NULL)
7936 {
7937 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007938 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 continue;
7940 }
7941
7942 /*
7943 * Skip preprocessor directives and blank lines.
7944 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007945 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7946 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 continue;
7948
7949 if (cin_nocode(l))
7950 continue;
7951
7952 terminated = cin_isterminated(l, FALSE, TRUE);
7953
7954 /*
7955 * If we are at top level and the line looks like a
7956 * function declaration, we are done
7957 * (it's a variable declaration).
7958 */
7959 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007960 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
7962 /* if the line is terminated with another ','
7963 * it is a continued variable initialization.
7964 * don't add extra indent.
7965 * TODO: does not work, if a function
7966 * declaration is split over multiple lines:
7967 * cin_isfuncdecl returns FALSE then.
7968 */
7969 if (terminated == ',')
7970 break;
7971
7972 /* if it es a enum declaration or an assignment,
7973 * we are done.
7974 */
7975 if (terminated != ';' && cin_isinit())
7976 break;
7977
7978 /* nothing useful found */
7979 if (terminated == 0 || terminated == '{')
7980 continue;
7981 }
7982
7983 if (terminated != ';')
7984 {
7985 /* Skip parens and braces. Position the cursor
7986 * over the rightmost paren, so that matching it
7987 * will take us back to the start of the line.
7988 */ /* XXX */
7989 trypos = NULL;
7990 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007991 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007992 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
7994 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007995 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996
7997 if (trypos != NULL)
7998 {
7999 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008000 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 continue;
8002 }
8003 }
8004
8005 /* it's a variable declaration, add indentation
8006 * like in
8007 * int a,
8008 * b;
8009 */
8010 if (cont_amount > 0)
8011 amount = cont_amount;
8012 else
8013 amount += ind_continuation;
8014 }
8015 else if (lookfor == LOOKFOR_UNTERM)
8016 {
8017 if (cont_amount > 0)
8018 amount = cont_amount;
8019 else
8020 amount += ind_continuation;
8021 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008022 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008023 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008024 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008025 && lookfor != LOOKFOR_CPP_BASECLASS
8026 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008027 {
8028 amount = scope_amount;
8029 if (theline[0] == '{')
8030 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008031 amount += curbuf->b_ind_open_extra;
8032 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008033 }
8034 }
8035
8036 if (lookfor_cpp_namespace)
8037 {
8038 /*
8039 * Looking for C++ namespace, need to look further
8040 * back.
8041 */
8042 if (curwin->w_cursor.lnum == ourscope)
8043 continue;
8044
8045 if (curwin->w_cursor.lnum == 0
8046 || curwin->w_cursor.lnum
8047 < ourscope - FIND_NAMESPACE_LIM)
8048 break;
8049
8050 l = ml_get_curline();
8051
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008052 /* If we're in a comment or raw string now, skip
8053 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008054 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008055 if (trypos != NULL)
8056 {
8057 curwin->w_cursor.lnum = trypos->lnum + 1;
8058 curwin->w_cursor.col = 0;
8059 continue;
8060 }
8061
8062 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008063 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8064 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008065 continue;
8066
8067 /* Finally the actual check for "namespace". */
8068 if (cin_is_cpp_namespace(l))
8069 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008070 amount += curbuf->b_ind_cpp_namespace
8071 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008072 break;
8073 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008074 else if (cin_is_cpp_extern_c(l))
8075 {
8076 amount += curbuf->b_ind_cpp_extern_c
8077 - added_to_amount;
8078 break;
8079 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008080
8081 if (cin_nocode(l))
8082 continue;
8083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 }
8085 break;
8086 }
8087
8088 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008089 * If we're in a comment or raw string now, skip to the start
8090 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008092 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {
8094 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008095 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 continue;
8097 }
8098
8099 l = ml_get_curline();
8100
8101 /*
8102 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008103 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008105 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 if (iscase || cin_isscopedecl(l))
8107 {
8108 /* we are only looking for cpp base class
8109 * declaration/initialization any longer */
8110 if (lookfor == LOOKFOR_CPP_BASECLASS)
8111 break;
8112
8113 /* When looking for a "do" we are not interested in
8114 * labels. */
8115 if (whilelevel > 0)
8116 continue;
8117
8118 /*
8119 * case xx:
8120 * c = 99 + <- this indent plus continuation
8121 *-> here;
8122 */
8123 if (lookfor == LOOKFOR_UNTERM
8124 || lookfor == LOOKFOR_ENUM_OR_INIT)
8125 {
8126 if (cont_amount > 0)
8127 amount = cont_amount;
8128 else
8129 amount += ind_continuation;
8130 break;
8131 }
8132
8133 /*
8134 * case xx: <- line up with this case
8135 * x = 333;
8136 * case yy:
8137 */
8138 if ( (iscase && lookfor == LOOKFOR_CASE)
8139 || (iscase && lookfor_break)
8140 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8141 {
8142 /*
8143 * Check that this case label is not for another
8144 * switch()
8145 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008146 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008147 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {
8149 amount = get_indent(); /* XXX */
8150 break;
8151 }
8152 continue;
8153 }
8154
8155 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8156
8157 /*
8158 * case xx: if (cond) <- line up with this if
8159 * y = y + 1;
8160 * -> s = 99;
8161 *
8162 * case xx:
8163 * if (cond) <- line up with this line
8164 * y = y + 1;
8165 * -> s = 99;
8166 */
8167 if (lookfor == LOOKFOR_TERM)
8168 {
8169 if (n)
8170 amount = n;
8171
8172 if (!lookfor_break)
8173 break;
8174 }
8175
8176 /*
8177 * case xx: x = x + 1; <- line up with this x
8178 * -> y = y + 1;
8179 *
8180 * case xx: if (cond) <- line up with this if
8181 * -> y = y + 1;
8182 */
8183 if (n)
8184 {
8185 amount = n;
8186 l = after_label(ml_get_curline());
8187 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008188 {
8189 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008190 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008191 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008192 amount += curbuf->b_ind_level
8193 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 break;
8196 }
8197
8198 /*
8199 * Try to get the indent of a statement before the switch
8200 * label. If nothing is found, line up relative to the
8201 * switch label.
8202 * break; <- may line up with this line
8203 * case xx:
8204 * -> y = 1;
8205 */
8206 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008207 ? curbuf->b_ind_case_code
8208 : curbuf->b_ind_scopedecl_code);
8209 lookfor = curbuf->b_ind_case_break
8210 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 continue;
8212 }
8213
8214 /*
8215 * Looking for a switch() label or C++ scope declaration,
8216 * ignore other lines, skip {}-blocks.
8217 */
8218 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8219 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008220 if (find_last_paren(l, '{', '}')
8221 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008224 curwin->w_cursor.col = 0;
8225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 continue;
8227 }
8228
8229 /*
8230 * Ignore jump labels with nothing after them.
8231 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008232 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {
8234 l = after_label(ml_get_curline());
8235 if (l == NULL || cin_nocode(l))
8236 continue;
8237 }
8238
8239 /*
8240 * Ignore #defines, #if, etc.
8241 * Ignore comment and empty lines.
8242 * (need to get the line again, cin_islabel() may have
8243 * unlocked it)
8244 */
8245 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008246 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 || cin_nocode(l))
8248 continue;
8249
8250 /*
8251 * Are we at the start of a cpp base class declaration or
8252 * constructor initialization?
8253 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008254 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008255 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008256 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008257 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008258 l = ml_get_curline();
8259 }
8260 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
8262 if (lookfor == LOOKFOR_UNTERM)
8263 {
8264 if (cont_amount > 0)
8265 amount = cont_amount;
8266 else
8267 amount += ind_continuation;
8268 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008269 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008271 /* Need to find start of the declaration. */
8272 lookfor = LOOKFOR_UNTERM;
8273 ind_continuation = 0;
8274 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 }
8276 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008277 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008278 amount = get_baseclass_amount(
8279 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 break;
8281 }
8282 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8283 {
8284 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008285 * declaration or initialization before the opening brace.
8286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (cin_isterminated(l, TRUE, FALSE))
8288 break;
8289 else
8290 continue;
8291 }
8292
8293 /*
8294 * What happens next depends on the line being terminated.
8295 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008296 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 * 123,
8298 * sizeof
8299 * here
8300 * Otherwise check whether it is a enumeration or structure
8301 * initialisation (not indented) or a variable declaration
8302 * (indented).
8303 */
8304 terminated = cin_isterminated(l, FALSE, TRUE);
8305
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008306 if (js_cur_has_key)
8307 {
8308 js_cur_has_key = 0; /* only check the first line */
8309 if (curbuf->b_ind_js && terminated == ',')
8310 {
8311 /* For Javascript we might be inside an object:
8312 * key: something, <- align with this
8313 * key: something
8314 * or:
8315 * key: something + <- align with this
8316 * something,
8317 * key: something
8318 */
8319 lookfor = LOOKFOR_JS_KEY;
8320 }
8321 }
8322 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8323 {
8324 amount = get_indent();
8325 break;
8326 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008327 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008328 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008329 if (tryposBrace != NULL && tryposBrace->lnum
8330 >= curwin->w_cursor.lnum)
8331 break;
8332 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008333 /* line below current line is the one that starts a
8334 * (possibly broken) line ending in a comma. */
8335 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008336 else
8337 {
8338 amount = get_indent();
8339 if (curwin->w_cursor.lnum - 1 == ourscope)
8340 /* line above is start of the scope, thus current
8341 * line is the one that stars a (possibly broken)
8342 * line ending in a comma. */
8343 break;
8344 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008345 }
8346
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8348 && terminated == ','))
8349 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008350 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8351 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008352 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 /*
8354 * if we're in the middle of a paren thing,
8355 * go back to the line that starts it so
8356 * we can get the right prevailing indent
8357 * if ( foo &&
8358 * bar )
8359 */
8360 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008361 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008363 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 */
8365 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008366 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008367 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8368 || (trypos->lnum == tryposBrace->lnum
8369 && trypos->col < tryposBrace->col)))
8370 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371
8372 /*
8373 * If we are looking for ',', we also look for matching
8374 * braces.
8375 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008376 if (trypos == NULL && terminated == ','
8377 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008378 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
8380 if (trypos != NULL)
8381 {
8382 /*
8383 * Check if we are on a case label now. This is
8384 * handled above.
8385 * case xx: if ( asdf &&
8386 * asdf)
8387 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008388 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008390 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {
8392 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008393 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 continue;
8395 }
8396 }
8397
8398 /*
8399 * Skip over continuation lines to find the one to get the
8400 * indent from
8401 * char *usethis = "bla\
8402 * bla",
8403 * here;
8404 */
8405 if (terminated == ',')
8406 {
8407 while (curwin->w_cursor.lnum > 1)
8408 {
8409 l = ml_get(curwin->w_cursor.lnum - 1);
8410 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8411 break;
8412 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008413 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
8415 }
8416
8417 /*
8418 * Get indent and pointer to text for current line,
8419 * ignoring any jump label. XXX
8420 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008421 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008422 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008423 else
8424 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 /*
8426 * If this is just above the line we are indenting, and it
8427 * starts with a '{', line it up with this line.
8428 * while (not)
8429 * -> {
8430 * }
8431 */
8432 if (terminated != ',' && lookfor != LOOKFOR_TERM
8433 && theline[0] == '{')
8434 {
8435 amount = cur_amount;
8436 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008437 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 * doesn't start with a '{', which must have a match
8439 * in the same line (scope is the same). Probably:
8440 * { 1, 2 },
8441 * -> { 3, 4 }
8442 */
8443 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008444 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008446 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
8448 /* have to look back, whether it is a cpp base
8449 * class declaration or initialization */
8450 lookfor = LOOKFOR_CPP_BASECLASS;
8451 continue;
8452 }
8453 break;
8454 }
8455
8456 /*
8457 * Check if we are after an "if", "while", etc.
8458 * Also allow " } else".
8459 */
8460 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8461 {
8462 /*
8463 * Found an unterminated line after an if (), line up
8464 * with the last one.
8465 * if (cond)
8466 * 100 +
8467 * -> here;
8468 */
8469 if (lookfor == LOOKFOR_UNTERM
8470 || lookfor == LOOKFOR_ENUM_OR_INIT)
8471 {
8472 if (cont_amount > 0)
8473 amount = cont_amount;
8474 else
8475 amount += ind_continuation;
8476 break;
8477 }
8478
8479 /*
8480 * If this is just above the line we are indenting, we
8481 * are finished.
8482 * while (not)
8483 * -> here;
8484 * Otherwise this indent can be used when the line
8485 * before this is terminated.
8486 * yyy;
8487 * if (stat)
8488 * while (not)
8489 * xxx;
8490 * -> here;
8491 */
8492 amount = cur_amount;
8493 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008494 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 if (lookfor != LOOKFOR_TERM)
8496 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008497 amount += curbuf->b_ind_level
8498 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 break;
8500 }
8501
8502 /*
8503 * Special trick: when expecting the while () after a
8504 * do, line up with the while()
8505 * do
8506 * x = 1;
8507 * -> here
8508 */
8509 l = skipwhite(ml_get_curline());
8510 if (cin_isdo(l))
8511 {
8512 if (whilelevel == 0)
8513 break;
8514 --whilelevel;
8515 }
8516
8517 /*
8518 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008519 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 * Need to use the scope of this "else". XXX
8521 * If whilelevel != 0 continue looking for a "do {".
8522 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008523 if (cin_iselse(l) && whilelevel == 0)
8524 {
8525 /* If we're looking at "} else", let's make sure we
8526 * find the opening brace of the enclosing scope,
8527 * not the one from "if () {". */
8528 if (*l == '}')
8529 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008530 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008531
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008532 if ((trypos = find_start_brace()) == NULL
8533 || find_match(LOOKFOR_IF, trypos->lnum)
8534 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008535 break;
8536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
8538
8539 /*
8540 * If we're below an unterminated line that is not an
8541 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008542 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 * the line before this one.
8544 */
8545 else
8546 {
8547 /*
8548 * Found two unterminated lines on a row, line up with
8549 * the last one.
8550 * c = 99 +
8551 * 100 +
8552 * -> here;
8553 */
8554 if (lookfor == LOOKFOR_UNTERM)
8555 {
8556 /* When line ends in a comma add extra indent */
8557 if (terminated == ',')
8558 amount += ind_continuation;
8559 break;
8560 }
8561
8562 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8563 {
8564 /* Found two lines ending in ',', lineup with the
8565 * lowest one, but check for cpp base class
8566 * declaration/initialization, if it is an
8567 * opening brace or we are looking just for
8568 * enumerations/initializations. */
8569 if (terminated == ',')
8570 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008571 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 break;
8573
8574 lookfor = LOOKFOR_CPP_BASECLASS;
8575 continue;
8576 }
8577
8578 /* Ignore unterminated lines in between, but
8579 * reduce indent. */
8580 if (amount > cur_amount)
8581 amount = cur_amount;
8582 }
8583 else
8584 {
8585 /*
8586 * Found first unterminated line on a row, may
8587 * line up with this line, remember its indent
8588 * 100 +
8589 * -> here;
8590 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008591 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008593
8594 n = (int)STRLEN(l);
8595 if (terminated == ',' && (*skipwhite(l) == ']'
8596 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 /*
8600 * If previous line ends in ',', check whether we
8601 * are in an initialization or enum
8602 * struct xxx =
8603 * {
8604 * sizeof a,
8605 * 124 };
8606 * or a normal possible continuation line.
8607 * but only, of no other statement has been found
8608 * yet.
8609 */
8610 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8611 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008612 if (curbuf->b_ind_js)
8613 {
8614 /* Search for a line ending in a comma
8615 * and line up with the line below it
8616 * (could be the current line).
8617 * some = [
8618 * 1, <- line up here
8619 * 2,
8620 * some = [
8621 * 3 + <- line up here
8622 * 4 *
8623 * 5,
8624 * 6,
8625 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008626 if (cin_iscomment(skipwhite(l)))
8627 break;
8628 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008629 trypos = find_match_char('[',
8630 curbuf->b_ind_maxparen);
8631 if (trypos != NULL)
8632 {
8633 if (trypos->lnum
8634 == curwin->w_cursor.lnum - 1)
8635 {
8636 /* Current line is first inside
8637 * [], line up with it. */
8638 break;
8639 }
8640 ourscope = trypos->lnum;
8641 }
8642 }
8643 else
8644 {
8645 lookfor = LOOKFOR_ENUM_OR_INIT;
8646 cont_amount = cin_first_id_amount();
8647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 }
8649 else
8650 {
8651 if (lookfor == LOOKFOR_INITIAL
8652 && *l != NUL
8653 && l[STRLEN(l) - 1] == '\\')
8654 /* XXX */
8655 cont_amount = cin_get_equal_amount(
8656 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008657 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008658 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008659 && lookfor != LOOKFOR_COMMA
8660 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 lookfor = LOOKFOR_UNTERM;
8662 }
8663 }
8664 }
8665 }
8666
8667 /*
8668 * Check if we are after a while (cond);
8669 * If so: Ignore until the matching "do".
8670 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008671 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 {
8673 /*
8674 * Found an unterminated line after a while ();, line up
8675 * with the last one.
8676 * while (cond);
8677 * 100 + <- line up with this one
8678 * -> here;
8679 */
8680 if (lookfor == LOOKFOR_UNTERM
8681 || lookfor == LOOKFOR_ENUM_OR_INIT)
8682 {
8683 if (cont_amount > 0)
8684 amount = cont_amount;
8685 else
8686 amount += ind_continuation;
8687 break;
8688 }
8689
8690 if (whilelevel == 0)
8691 {
8692 lookfor = LOOKFOR_TERM;
8693 amount = get_indent(); /* XXX */
8694 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008695 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 }
8697 ++whilelevel;
8698 }
8699
8700 /*
8701 * We are after a "normal" statement.
8702 * If we had another statement we can stop now and use the
8703 * indent of that other statement.
8704 * Otherwise the indent of the current statement may be used,
8705 * search backwards for the next "normal" statement.
8706 */
8707 else
8708 {
8709 /*
8710 * Skip single break line, if before a switch label. It
8711 * may be lined up with the case label.
8712 */
8713 if (lookfor == LOOKFOR_NOBREAK
8714 && cin_isbreak(skipwhite(ml_get_curline())))
8715 {
8716 lookfor = LOOKFOR_ANY;
8717 continue;
8718 }
8719
8720 /*
8721 * Handle "do {" line.
8722 */
8723 if (whilelevel > 0)
8724 {
8725 l = cin_skipcomment(ml_get_curline());
8726 if (cin_isdo(l))
8727 {
8728 amount = get_indent(); /* XXX */
8729 --whilelevel;
8730 continue;
8731 }
8732 }
8733
8734 /*
8735 * Found a terminated line above an unterminated line. Add
8736 * the amount for a continuation line.
8737 * x = 1;
8738 * y = foo +
8739 * -> here;
8740 * or
8741 * int x = 1;
8742 * int foo,
8743 * -> here;
8744 */
8745 if (lookfor == LOOKFOR_UNTERM
8746 || lookfor == LOOKFOR_ENUM_OR_INIT)
8747 {
8748 if (cont_amount > 0)
8749 amount = cont_amount;
8750 else
8751 amount += ind_continuation;
8752 break;
8753 }
8754
8755 /*
8756 * Found a terminated line above a terminated line or "if"
8757 * etc. line. Use the amount of the line below us.
8758 * x = 1; x = 1;
8759 * if (asdf) y = 2;
8760 * while (asdf) ->here;
8761 * here;
8762 * ->foo;
8763 */
8764 if (lookfor == LOOKFOR_TERM)
8765 {
8766 if (!lookfor_break && whilelevel == 0)
8767 break;
8768 }
8769
8770 /*
8771 * First line above the one we're indenting is terminated.
8772 * To know what needs to be done look further backward for
8773 * a terminated line.
8774 */
8775 else
8776 {
8777 /*
8778 * position the cursor over the rightmost paren, so
8779 * that matching it will take us back to the start of
8780 * the line. Helps for:
8781 * func(asdr,
8782 * asdfasdf);
8783 * here;
8784 */
8785term_again:
8786 l = ml_get_curline();
8787 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008788 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008789 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 {
8791 /*
8792 * Check if we are on a case label now. This is
8793 * handled above.
8794 * case xx: if ( asdf &&
8795 * asdf)
8796 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008797 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008799 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
8801 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008802 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 continue;
8804 }
8805 }
8806
8807 /* When aligning with the case statement, don't align
8808 * with a statement after it.
8809 * case 1: { <-- don't use this { position
8810 * stat;
8811 * }
8812 * case 2:
8813 * stat;
8814 * }
8815 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008816 iscase = (curbuf->b_ind_keep_case_label
8817 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
8819 /*
8820 * Get indent and pointer to text for current line,
8821 * ignoring any jump label.
8822 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008823 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
8825 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008826 amount += curbuf->b_ind_open_extra;
8827 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008828 l = skipwhite(l);
8829 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008830 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8832
8833 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008834 * When a terminated line starts with "else" skip to
8835 * the matching "if":
8836 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008837 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008838 * Need to use the scope of this "else". XXX
8839 * If whilelevel != 0 continue looking for a "do {".
8840 */
8841 if (lookfor == LOOKFOR_TERM
8842 && *l != '}'
8843 && cin_iselse(l)
8844 && whilelevel == 0)
8845 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008846 if ((trypos = find_start_brace()) == NULL
8847 || find_match(LOOKFOR_IF, trypos->lnum)
8848 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008849 break;
8850 continue;
8851 }
8852
8853 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 * If we're at the end of a block, skip to the start of
8855 * that block.
8856 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008857 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008858 if (find_last_paren(l, '{', '}') /* XXX */
8859 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008861 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 /* if not "else {" check for terminated again */
8863 /* but skip block for "} else {" */
8864 l = cin_skipcomment(ml_get_curline());
8865 if (*l == '}' || !cin_iselse(l))
8866 goto term_again;
8867 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008868 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 }
8870 }
8871 }
8872 }
8873 }
8874 }
8875
8876 /* add extra indent for a comment */
8877 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008878 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008879
8880 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008881 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8882 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008883
8884 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008886
8887 /*
8888 * ok -- we're not inside any sort of structure at all!
8889 *
8890 * This means we're at the top level, and everything should
8891 * basically just match where the previous line is, except
8892 * for the lines immediately following a function declaration,
8893 * which are K&R-style parameters and need to be indented.
8894 *
8895 * if our line starts with an open brace, forget about any
8896 * prevailing indent and make sure it looks like the start
8897 * of a function
8898 */
8899
8900 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008902 amount = curbuf->b_ind_first_open;
8903 goto theend;
8904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008906 /*
8907 * If the NEXT line is a function declaration, the current
8908 * line needs to be indented as a function type spec.
8909 * Don't do this if the current line looks like a comment or if the
8910 * current line is terminated, ie. ends in ';', or if the current line
8911 * contains { or }: "void f() {\n if (1)"
8912 */
8913 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8914 && !cin_nocode(theline)
8915 && vim_strchr(theline, '{') == NULL
8916 && vim_strchr(theline, '}') == NULL
8917 && !cin_ends_in(theline, (char_u *)":", NULL)
8918 && !cin_ends_in(theline, (char_u *)",", NULL)
8919 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8920 cur_curpos.lnum + 1)
8921 && !cin_isterminated(theline, FALSE, TRUE))
8922 {
8923 amount = curbuf->b_ind_func_type;
8924 goto theend;
8925 }
8926
8927 /* search backwards until we find something we recognize */
8928 amount = 0;
8929 curwin->w_cursor = cur_curpos;
8930 while (curwin->w_cursor.lnum > 1)
8931 {
8932 curwin->w_cursor.lnum--;
8933 curwin->w_cursor.col = 0;
8934
8935 l = ml_get_curline();
8936
8937 /*
8938 * If we're in a comment or raw string now, skip to the start
8939 * of it.
8940 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008941 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008943 curwin->w_cursor.lnum = trypos->lnum + 1;
8944 curwin->w_cursor.col = 0;
8945 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947
8948 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008949 * Are we at the start of a cpp base class declaration or
8950 * constructor initialization?
8951 */ /* XXX */
8952 n = FALSE;
8953 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008955 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8956 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008958 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008960 /* XXX */
8961 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8962 break;
8963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008965 /*
8966 * Skip preprocessor directives and blank lines.
8967 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008968 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008969 continue;
8970
8971 if (cin_nocode(l))
8972 continue;
8973
8974 /*
8975 * If the previous line ends in ',', use one level of
8976 * indentation:
8977 * int foo,
8978 * bar;
8979 * do this before checking for '}' in case of eg.
8980 * enum foobar
8981 * {
8982 * ...
8983 * } foo,
8984 * bar;
8985 */
8986 n = 0;
8987 if (cin_ends_in(l, (char_u *)",", NULL)
8988 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8989 {
8990 /* take us back to opening paren */
8991 if (find_last_paren(l, '(', ')')
8992 && (trypos = find_match_paren(
8993 curbuf->b_ind_maxparen)) != NULL)
8994 curwin->w_cursor = *trypos;
8995
8996 /* For a line ending in ',' that is a continuation line go
8997 * back to the first line with a backslash:
8998 * char *foo = "bla\
8999 * bla",
9000 * here;
9001 */
9002 while (n == 0 && curwin->w_cursor.lnum > 1)
9003 {
9004 l = ml_get(curwin->w_cursor.lnum - 1);
9005 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9006 break;
9007 --curwin->w_cursor.lnum;
9008 curwin->w_cursor.col = 0;
9009 }
9010
9011 amount = get_indent(); /* XXX */
9012
9013 if (amount == 0)
9014 amount = cin_first_id_amount();
9015 if (amount == 0)
9016 amount = ind_continuation;
9017 break;
9018 }
9019
9020 /*
9021 * If the line looks like a function declaration, and we're
9022 * not in a comment, put it the left margin.
9023 */
9024 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9025 break;
9026 l = ml_get_curline();
9027
9028 /*
9029 * Finding the closing '}' of a previous function. Put
9030 * current line at the left margin. For when 'cino' has "fs".
9031 */
9032 if (*skipwhite(l) == '}')
9033 break;
9034
9035 /* (matching {)
9036 * If the previous line ends on '};' (maybe followed by
9037 * comments) align at column 0. For example:
9038 * char *string_array[] = { "foo",
9039 * / * x * / "b};ar" }; / * foobar * /
9040 */
9041 if (cin_ends_in(l, (char_u *)"};", NULL))
9042 break;
9043
9044 /*
9045 * If the previous line ends on '[' we are probably in an
9046 * array constant:
9047 * something = [
9048 * 234, <- extra indent
9049 */
9050 if (cin_ends_in(l, (char_u *)"[", NULL))
9051 {
9052 amount = get_indent() + ind_continuation;
9053 break;
9054 }
9055
9056 /*
9057 * Find a line only has a semicolon that belongs to a previous
9058 * line ending in '}', e.g. before an #endif. Don't increase
9059 * indent then.
9060 */
9061 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9062 {
9063 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064
9065 while (curwin->w_cursor.lnum > 1)
9066 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009067 look = ml_get(--curwin->w_cursor.lnum);
9068 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009069 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009071 }
9072 if (curwin->w_cursor.lnum > 0
9073 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009076 curwin->w_cursor = curpos_save;
9077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009079 /*
9080 * If the PREVIOUS line is a function declaration, the current
9081 * line (and the ones that follow) needs to be indented as
9082 * parameters.
9083 */
9084 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9085 {
9086 amount = curbuf->b_ind_param;
9087 break;
9088 }
9089
9090 /*
9091 * If the previous line ends in ';' and the line before the
9092 * previous line ends in ',' or '\', ident to column zero:
9093 * int foo,
9094 * bar;
9095 * indent_to_0 here;
9096 */
9097 if (cin_ends_in(l, (char_u *)";", NULL))
9098 {
9099 l = ml_get(curwin->w_cursor.lnum - 1);
9100 if (cin_ends_in(l, (char_u *)",", NULL)
9101 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9102 break;
9103 l = ml_get_curline();
9104 }
9105
9106 /*
9107 * Doesn't look like anything interesting -- so just
9108 * use the indent of this line.
9109 *
9110 * Position the cursor over the rightmost paren, so that
9111 * matching it will take us back to the start of the line.
9112 */
9113 find_last_paren(l, '(', ')');
9114
9115 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9116 curwin->w_cursor = *trypos;
9117 amount = get_indent(); /* XXX */
9118 break;
9119 }
9120
9121 /* add extra indent for a comment */
9122 if (cin_iscomment(theline))
9123 amount += curbuf->b_ind_comment;
9124
9125 /* add extra indent if the previous line ended in a backslash:
9126 * "asdfasdf\
9127 * here";
9128 * char *foo = "asdf\
9129 * here";
9130 */
9131 if (cur_curpos.lnum > 1)
9132 {
9133 l = ml_get(cur_curpos.lnum - 1);
9134 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9135 {
9136 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9137 if (cur_amount > 0)
9138 amount = cur_amount;
9139 else if (cur_amount == 0)
9140 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 }
9142 }
9143
9144theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009145 if (amount < 0)
9146 amount = 0;
9147
9148laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 /* put the cursor back where it belongs */
9150 curwin->w_cursor = cur_curpos;
9151
9152 vim_free(linecopy);
9153
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 return amount;
9155}
9156
9157 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009158find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160 char_u *look;
9161 pos_T *theirscope;
9162 char_u *mightbeif;
9163 int elselevel;
9164 int whilelevel;
9165
9166 if (lookfor == LOOKFOR_IF)
9167 {
9168 elselevel = 1;
9169 whilelevel = 0;
9170 }
9171 else
9172 {
9173 elselevel = 0;
9174 whilelevel = 1;
9175 }
9176
9177 curwin->w_cursor.col = 0;
9178
9179 while (curwin->w_cursor.lnum > ourscope + 1)
9180 {
9181 curwin->w_cursor.lnum--;
9182 curwin->w_cursor.col = 0;
9183
9184 look = cin_skipcomment(ml_get_curline());
9185 if (cin_iselse(look)
9186 || cin_isif(look)
9187 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009188 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 {
9190 /*
9191 * if we've gone outside the braces entirely,
9192 * we must be out of scope...
9193 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009194 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 if (theirscope == NULL)
9196 break;
9197
9198 /*
9199 * and if the brace enclosing this is further
9200 * back than the one enclosing the else, we're
9201 * out of luck too.
9202 */
9203 if (theirscope->lnum < ourscope)
9204 break;
9205
9206 /*
9207 * and if they're enclosed in a *deeper* brace,
9208 * then we can ignore it because it's in a
9209 * different scope...
9210 */
9211 if (theirscope->lnum > ourscope)
9212 continue;
9213
9214 /*
9215 * if it was an "else" (that's not an "else if")
9216 * then we need to go back to another if, so
9217 * increment elselevel
9218 */
9219 look = cin_skipcomment(ml_get_curline());
9220 if (cin_iselse(look))
9221 {
9222 mightbeif = cin_skipcomment(look + 4);
9223 if (!cin_isif(mightbeif))
9224 ++elselevel;
9225 continue;
9226 }
9227
9228 /*
9229 * if it was a "while" then we need to go back to
9230 * another "do", so increment whilelevel. XXX
9231 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009232 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 {
9234 ++whilelevel;
9235 continue;
9236 }
9237
9238 /* If it's an "if" decrement elselevel */
9239 look = cin_skipcomment(ml_get_curline());
9240 if (cin_isif(look))
9241 {
9242 elselevel--;
9243 /*
9244 * When looking for an "if" ignore "while"s that
9245 * get in the way.
9246 */
9247 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9248 whilelevel = 0;
9249 }
9250
9251 /* If it's a "do" decrement whilelevel */
9252 if (cin_isdo(look))
9253 whilelevel--;
9254
9255 /*
9256 * if we've used up all the elses, then
9257 * this must be the if that we want!
9258 * match the indent level of that if.
9259 */
9260 if (elselevel <= 0 && whilelevel <= 0)
9261 {
9262 return OK;
9263 }
9264 }
9265 }
9266 return FAIL;
9267}
9268
9269# if defined(FEAT_EVAL) || defined(PROTO)
9270/*
9271 * Get indent level from 'indentexpr'.
9272 */
9273 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009274get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009276 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009277 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009278 pos_T save_pos;
9279 colnr_T save_curswant;
9280 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009282 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9283 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009285 /* Save and restore cursor position and curswant, in case it was changed
9286 * via :normal commands */
9287 save_pos = curwin->w_cursor;
9288 save_curswant = curwin->w_curswant;
9289 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009291 if (use_sandbox)
9292 ++sandbox;
9293 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009294
9295 /* Need to make a copy, the 'indentexpr' option could be changed while
9296 * evaluating it. */
9297 inde_copy = vim_strsave(curbuf->b_p_inde);
9298 if (inde_copy != NULL)
9299 {
9300 indent = (int)eval_to_number(inde_copy);
9301 vim_free(inde_copy);
9302 }
9303
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009304 if (use_sandbox)
9305 --sandbox;
9306 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307
9308 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9309 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9310 * command. */
9311 save_State = State;
9312 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009313 curwin->w_cursor = save_pos;
9314 curwin->w_curswant = save_curswant;
9315 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 check_cursor();
9317 State = save_State;
9318
9319 /* If there is an error, just keep the current indent. */
9320 if (indent < 0)
9321 indent = get_indent();
9322
9323 return indent;
9324}
9325# endif
9326
9327#endif /* FEAT_CINDENT */
9328
9329#if defined(FEAT_LISP) || defined(PROTO)
9330
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009331static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
9333 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009334lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
9336 char_u buf[LSIZE];
9337 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009338 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
9340 while (*word != NUL)
9341 {
9342 (void)copy_option_part(&word, buf, LSIZE, ",");
9343 len = (int)STRLEN(buf);
9344 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9345 return TRUE;
9346 }
9347 return FALSE;
9348}
9349
9350/*
9351 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9352 * The incompatible newer method is quite a bit better at indenting
9353 * code in lisp-like languages than the traditional one; it's still
9354 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9355 *
9356 * TODO:
9357 * Findmatch() should be adapted for lisp, also to make showmatch
9358 * work correctly: now (v5.3) it seems all C/C++ oriented:
9359 * - it does not recognize the #\( and #\) notations as character literals
9360 * - it doesn't know about comments starting with a semicolon
9361 * - it incorrectly interprets '(' as a character literal
9362 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009363 * Update from Sergey Khorev:
9364 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 */
9366 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009367get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009369 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 int amount;
9371 char_u *that;
9372 colnr_T col;
9373 colnr_T firsttry;
9374 int parencount, quotecount;
9375 int vi_lisp;
9376
9377 /* Set vi_lisp to use the vi-compatible method */
9378 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9379
9380 realpos = curwin->w_cursor;
9381 curwin->w_cursor.col = 0;
9382
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009383 if ((pos = findmatch(NULL, '(')) == NULL)
9384 pos = findmatch(NULL, '[');
9385 else
9386 {
9387 paren = *pos;
9388 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009389 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009390 pos = &paren;
9391 }
9392 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 {
9394 /* Extra trick: Take the indent of the first previous non-white
9395 * line that is at the same () level. */
9396 amount = -1;
9397 parencount = 0;
9398
9399 while (--curwin->w_cursor.lnum >= pos->lnum)
9400 {
9401 if (linewhite(curwin->w_cursor.lnum))
9402 continue;
9403 for (that = ml_get_curline(); *that != NUL; ++that)
9404 {
9405 if (*that == ';')
9406 {
9407 while (*(that + 1) != NUL)
9408 ++that;
9409 continue;
9410 }
9411 if (*that == '\\')
9412 {
9413 if (*(that + 1) != NUL)
9414 ++that;
9415 continue;
9416 }
9417 if (*that == '"' && *(that + 1) != NUL)
9418 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009419 while (*++that && *that != '"')
9420 {
9421 /* skipping escaped characters in the string */
9422 if (*that == '\\')
9423 {
9424 if (*++that == NUL)
9425 break;
9426 if (that[1] == NUL)
9427 {
9428 ++that;
9429 break;
9430 }
9431 }
9432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009434 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009436 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 --parencount;
9438 }
9439 if (parencount == 0)
9440 {
9441 amount = get_indent();
9442 break;
9443 }
9444 }
9445
9446 if (amount == -1)
9447 {
9448 curwin->w_cursor.lnum = pos->lnum;
9449 curwin->w_cursor.col = pos->col;
9450 col = pos->col;
9451
9452 that = ml_get_curline();
9453
9454 if (vi_lisp && get_indent() == 0)
9455 amount = 2;
9456 else
9457 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009458 char_u *line = that;
9459
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 amount = 0;
9461 while (*that && col)
9462 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009463 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 col--;
9465 }
9466
9467 /*
9468 * Some keywords require "body" indenting rules (the
9469 * non-standard-lisp ones are Scheme special forms):
9470 *
9471 * (let ((a 1)) instead (let ((a 1))
9472 * (...)) of (...))
9473 */
9474
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009475 if (!vi_lisp && (*that == '(' || *that == '[')
9476 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 amount += 2;
9478 else
9479 {
9480 that++;
9481 amount++;
9482 firsttry = amount;
9483
Bram Moolenaar1c465442017-03-12 20:10:05 +01009484 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009486 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 ++that;
9488 }
9489
9490 if (*that && *that != ';') /* not a comment line */
9491 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009492 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009494 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 firsttry++;
9496
9497 parencount = 0;
9498 quotecount = 0;
9499
9500 if (vi_lisp
9501 || (*that != '"'
9502 && *that != '\''
9503 && *that != '#'
9504 && (*that < '0' || *that > '9')))
9505 {
9506 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009507 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 || quotecount
9509 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009510 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 && !quotecount
9512 && !parencount
9513 && vi_lisp)))
9514 {
9515 if (*that == '"')
9516 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009517 if ((*that == '(' || *that == '[')
9518 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009520 if ((*that == ')' || *that == ']')
9521 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 --parencount;
9523 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009524 amount += lbr_chartabsize_adv(
9525 line, &that, (colnr_T)amount);
9526 amount += lbr_chartabsize_adv(
9527 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 }
9529 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009530 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009532 amount += lbr_chartabsize(
9533 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 that++;
9535 }
9536 if (!*that || *that == ';')
9537 amount = firsttry;
9538 }
9539 }
9540 }
9541 }
9542 }
9543 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009544 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546 curwin->w_cursor = realpos;
9547
9548 return amount;
9549}
9550#endif /* FEAT_LISP */
9551
9552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009553prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009555#if defined(SIGHUP) && defined(SIG_IGN)
9556 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9557 * makes Vim exit and then handling SIGHUP causes various reentrance
9558 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009559 signal(SIGHUP, SIG_IGN);
9560#endif
9561
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562#ifdef FEAT_GUI
9563 if (gui.in_use)
9564 {
9565 gui.dying = TRUE;
9566 out_trash(); /* trash any pending output */
9567 }
9568 else
9569#endif
9570 {
9571 windgoto((int)Rows - 1, 0);
9572
9573 /*
9574 * Switch terminal mode back now, so messages end up on the "normal"
9575 * screen (if there are two screens).
9576 */
9577 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009578 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 out_flush();
9580 }
9581}
9582
9583/*
9584 * Preserve files and exit.
9585 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009586 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9587 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 */
9589 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009590preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591{
9592 buf_T *buf;
9593
9594 prepare_to_exit();
9595
Bram Moolenaar4770d092006-01-12 23:22:24 +00009596 /* Setting this will prevent free() calls. That avoids calling free()
9597 * recursively when free() was invoked with a bad pointer. */
9598 really_exiting = TRUE;
9599
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 out_str(IObuff);
9601 screen_start(); /* don't know where cursor is now */
9602 out_flush();
9603
9604 ml_close_notmod(); /* close all not-modified buffers */
9605
Bram Moolenaar29323592016-07-24 22:04:11 +02009606 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 {
9608 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9609 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009610 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 screen_start(); /* don't know where cursor is now */
9612 out_flush();
9613 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9614 break;
9615 }
9616 }
9617
9618 ml_close_all(FALSE); /* close all memfiles, without deleting */
9619
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009620 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621
9622 getout(1);
9623}
9624
9625/*
9626 * return TRUE if "fname" exists.
9627 */
9628 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009629vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009631 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632
9633 if (mch_stat((char *)fname, &st))
9634 return FALSE;
9635 return TRUE;
9636}
9637
9638/*
9639 * Check for CTRL-C pressed, but only once in a while.
9640 * Should be used instead of ui_breakcheck() for functions that check for
9641 * each line in the file. Calling ui_breakcheck() each time takes too much
9642 * time, because it can be a system call.
9643 */
9644
9645#ifndef BREAKCHECK_SKIP
9646# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9647# define BREAKCHECK_SKIP 200
9648# else
9649# define BREAKCHECK_SKIP 32
9650# endif
9651#endif
9652
9653static int breakcheck_count = 0;
9654
9655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009656line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657{
9658 if (++breakcheck_count >= BREAKCHECK_SKIP)
9659 {
9660 breakcheck_count = 0;
9661 ui_breakcheck();
9662 }
9663}
9664
9665/*
9666 * Like line_breakcheck() but check 10 times less often.
9667 */
9668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009669fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9672 {
9673 breakcheck_count = 0;
9674 ui_breakcheck();
9675 }
9676}
9677
9678/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009679 * Invoke expand_wildcards() for one pattern.
9680 * Expand items like "%:h" before the expansion.
9681 * Returns OK or FAIL.
9682 */
9683 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009684expand_wildcards_eval(
9685 char_u **pat, /* pointer to input pattern */
9686 int *num_file, /* resulting number of files */
9687 char_u ***file, /* array of resulting files */
9688 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009689{
9690 int ret = FAIL;
9691 char_u *eval_pat = NULL;
9692 char_u *exp_pat = *pat;
9693 char_u *ignored_msg;
9694 int usedlen;
9695
9696 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9697 {
9698 ++emsg_off;
9699 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9700 NULL, &ignored_msg, NULL);
9701 --emsg_off;
9702 if (eval_pat != NULL)
9703 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9704 }
9705
9706 if (exp_pat != NULL)
9707 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9708
9709 if (eval_pat != NULL)
9710 {
9711 vim_free(exp_pat);
9712 vim_free(eval_pat);
9713 }
9714
9715 return ret;
9716}
9717
9718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9720 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009721 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 */
9723 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009724expand_wildcards(
9725 int num_pat, /* number of input patterns */
9726 char_u **pat, /* array of input patterns */
9727 int *num_files, /* resulting number of files */
9728 char_u ***files, /* array of resulting files */
9729 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 int retval;
9732 int i, j;
9733 char_u *p;
9734 int non_suf_match; /* number without matching suffix */
9735
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009736 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737
9738 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009739 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 return retval;
9741
9742#ifdef FEAT_WILDIGN
9743 /*
9744 * Remove names that match 'wildignore'.
9745 */
9746 if (*p_wig)
9747 {
9748 char_u *ffname;
9749
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009750 /* check all files in (*files)[] */
9751 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009753 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 if (ffname == NULL) /* out of memory */
9755 break;
9756# ifdef VMS
9757 vms_remove_version(ffname);
9758# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009759 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009761 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009762 vim_free((*files)[i]);
9763 for (j = i; j + 1 < *num_files; ++j)
9764 (*files)[j] = (*files)[j + 1];
9765 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 --i;
9767 }
9768 vim_free(ffname);
9769 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009770
9771 /* If the number of matches is now zero, we fail. */
9772 if (*num_files == 0)
9773 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009774 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009775 return FAIL;
9776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 }
9778#endif
9779
9780 /*
9781 * Move the names where 'suffixes' match to the end.
9782 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009783 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
9785 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009786 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009788 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 {
9790 /*
9791 * Move the name without matching suffix to the front
9792 * of the list.
9793 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009794 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009796 (*files)[j] = (*files)[j - 1];
9797 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 }
9799 }
9800 }
9801
9802 return retval;
9803}
9804
9805/*
9806 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9807 */
9808 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009809match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810{
9811 int fnamelen, setsuflen;
9812 char_u *setsuf;
9813#define MAXSUFLEN 30 /* maximum length of a file suffix */
9814 char_u suf_buf[MAXSUFLEN];
9815
9816 fnamelen = (int)STRLEN(fname);
9817 setsuflen = 0;
9818 for (setsuf = p_su; *setsuf; )
9819 {
9820 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009821 if (setsuflen == 0)
9822 {
9823 char_u *tail = gettail(fname);
9824
9825 /* empty entry: match name without a '.' */
9826 if (vim_strchr(tail, '.') == NULL)
9827 {
9828 setsuflen = 1;
9829 break;
9830 }
9831 }
9832 else
9833 {
9834 if (fnamelen >= setsuflen
9835 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9836 (size_t)setsuflen) == 0)
9837 break;
9838 setsuflen = 0;
9839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 }
9841 return (setsuflen != 0);
9842}
9843
9844#if !defined(NO_EXPANDPATH) || defined(PROTO)
9845
9846# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009847static int vim_backtick(char_u *p);
9848static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849# endif
9850
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009851# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852/*
9853 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9854 * it's shared between these systems.
9855 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009856# if defined(PROTO)
9857# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858# else
9859# ifdef __BORLANDC__
9860# define _cdecl _RTLENTRYF
9861# endif
9862# endif
9863
9864/*
9865 * comparison function for qsort in dos_expandpath()
9866 */
9867 static int _cdecl
9868pstrcmp(const void *a, const void *b)
9869{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009870 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871}
9872
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009874 * Recursively expand one path component into all matching files and/or
9875 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 * Return the number of matches found.
9877 * "path" has backslashes before chars that are not to be expanded, starting
9878 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009879 * Return the number of matches found.
9880 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 */
9882 static int
9883dos_expandpath(
9884 garray_T *gap,
9885 char_u *path,
9886 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009887 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009888 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009890 char_u *buf;
9891 char_u *path_end;
9892 char_u *p, *s, *e;
9893 int start_len = gap->ga_len;
9894 char_u *pat;
9895 regmatch_T regmatch;
9896 int starts_with_dot;
9897 int matches;
9898 int len;
9899 int starstar = FALSE;
9900 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 WIN32_FIND_DATA fb;
9902 HANDLE hFind = (HANDLE)0;
9903# ifdef FEAT_MBYTE
9904 WIN32_FIND_DATAW wfb;
9905 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009908 int ok;
9909
9910 /* Expanding "**" may take a long time, check for CTRL-C. */
9911 if (stardepth > 0)
9912 {
9913 ui_breakcheck();
9914 if (got_int)
9915 return 0;
9916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009918 /* Make room for file name. When doing encoding conversion the actual
9919 * length may be quite a bit longer, thus use the maximum possible length. */
9920 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 if (buf == NULL)
9922 return 0;
9923
9924 /*
9925 * Find the first part in the path name that contains a wildcard or a ~1.
9926 * Copy it into buf, including the preceding characters.
9927 */
9928 p = buf;
9929 s = buf;
9930 e = NULL;
9931 path_end = path;
9932 while (*path_end != NUL)
9933 {
9934 /* May ignore a wildcard that has a backslash before it; it will
9935 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9936 if (path_end >= path + wildoff && rem_backslash(path_end))
9937 *p++ = *path_end++;
9938 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9939 {
9940 if (e != NULL)
9941 break;
9942 s = p + 1;
9943 }
9944 else if (path_end >= path + wildoff
9945 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9946 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009947# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 if (has_mbyte)
9949 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009950 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 STRNCPY(p, path_end, len);
9952 p += len;
9953 path_end += len;
9954 }
9955 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 *p++ = *path_end++;
9958 }
9959 e = p;
9960 *e = NUL;
9961
9962 /* now we have one wildcard component between s and e */
9963 /* Remove backslashes between "wildoff" and the start of the wildcard
9964 * component. */
9965 for (p = buf + wildoff; p < s; ++p)
9966 if (rem_backslash(p))
9967 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009968 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 --e;
9970 --s;
9971 }
9972
Bram Moolenaar231334e2005-07-25 20:46:57 +00009973 /* Check for "**" between "s" and "e". */
9974 for (p = s; p < e; ++p)
9975 if (p[0] == '*' && p[1] == '*')
9976 starstar = TRUE;
9977
Bram Moolenaard82103e2016-01-17 17:04:05 +01009978 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9980 if (pat == NULL)
9981 {
9982 vim_free(buf);
9983 return 0;
9984 }
9985
9986 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009987 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009988 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 regmatch.rm_ic = TRUE; /* Always ignore case */
9990 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009991 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009992 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 vim_free(pat);
9994
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009995 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 {
9997 vim_free(buf);
9998 return 0;
9999 }
10000
10001 /* remember the pattern or file name being looked for */
10002 matchname = vim_strsave(s);
10003
Bram Moolenaar231334e2005-07-25 20:46:57 +000010004 /* If "**" is by itself, this is the first time we encounter it and more
10005 * is following then find matches without any directory. */
10006 if (!didstar && stardepth < 100 && starstar && e - s == 2
10007 && *path_end == '/')
10008 {
10009 STRCPY(s, path_end + 1);
10010 ++stardepth;
10011 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10012 --stardepth;
10013 }
10014
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 /* Scan all files in the directory with "dir/ *.*" */
10016 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017# ifdef FEAT_MBYTE
10018 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10019 {
10020 /* The active codepage differs from 'encoding'. Attempt using the
10021 * wide function. If it fails because it is not implemented fall back
10022 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010023 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if (wn != NULL)
10025 {
10026 hFind = FindFirstFileW(wn, &wfb);
10027 if (hFind == INVALID_HANDLE_VALUE
10028 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010029 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031 }
10032
10033 if (wn == NULL)
10034# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010035 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
10038 while (ok)
10039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040# ifdef FEAT_MBYTE
10041 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010042 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 else
10044# endif
10045 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 /* Ignore entries starting with a dot, unless when asked for. Accept
10047 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010048 if ((p[0] != '.' || starts_with_dot
10049 || ((flags & EW_DODOT)
10050 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010052 || (regmatch.regprog != NULL
10053 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010054 || ((flags & EW_NOTWILD)
10055 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010059
10060 if (starstar && stardepth < 100)
10061 {
10062 /* For "**" in the pattern first go deeper in the tree to
10063 * find matches. */
10064 STRCPY(buf + len, "/**");
10065 STRCPY(buf + len + 3, path_end);
10066 ++stardepth;
10067 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10068 --stardepth;
10069 }
10070
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 STRCPY(buf + len, path_end);
10072 if (mch_has_exp_wildcard(path_end))
10073 {
10074 /* need to expand another component of the path */
10075 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010076 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078 else
10079 {
10080 /* no more wildcards, check if there is a match */
10081 /* remove backslashes for the remaining components only */
10082 if (*path_end != 0)
10083 backslash_halve(buf + len + 1);
10084 if (mch_getperm(buf) >= 0) /* add existing file */
10085 addfile(gap, buf, flags);
10086 }
10087 }
10088
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089# ifdef FEAT_MBYTE
10090 if (wn != NULL)
10091 {
10092 vim_free(p);
10093 ok = FindNextFileW(hFind, &wfb);
10094 }
10095 else
10096# endif
10097 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
10099 /* If no more matches and no match was used, try expanding the name
10100 * itself. Finds the long name of a short filename. */
10101 if (!ok && matchname != NULL && gap->ga_len == start_len)
10102 {
10103 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 FindClose(hFind);
10105# ifdef FEAT_MBYTE
10106 if (wn != NULL)
10107 {
10108 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010109 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if (wn != NULL)
10111 hFind = FindFirstFileW(wn, &wfb);
10112 }
10113 if (wn == NULL)
10114# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010115 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010117 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 }
10119 }
10120
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 FindClose(hFind);
10122# ifdef FEAT_MBYTE
10123 vim_free(wn);
10124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010126 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 vim_free(matchname);
10128
10129 matches = gap->ga_len - start_len;
10130 if (matches > 0)
10131 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10132 sizeof(char_u *), pstrcmp);
10133 return matches;
10134}
10135
10136 int
10137mch_expandpath(
10138 garray_T *gap,
10139 char_u *path,
10140 int flags) /* EW_* flags */
10141{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010142 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010144# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
Bram Moolenaar231334e2005-07-25 20:46:57 +000010146#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10147 || defined(PROTO)
10148/*
10149 * Unix style wildcard expansion code.
10150 * It's here because it's used both for Unix and Mac.
10151 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010152static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010153
10154 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010155pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010156{
10157 return (pathcmp(*(char **)a, *(char **)b, -1));
10158}
10159
10160/*
10161 * Recursively expand one path component into all matching files and/or
10162 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10163 * "path" has backslashes before chars that are not to be expanded, starting
10164 * at "path + wildoff".
10165 * Return the number of matches found.
10166 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10167 */
10168 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010169unix_expandpath(
10170 garray_T *gap,
10171 char_u *path,
10172 int wildoff,
10173 int flags, /* EW_* flags */
10174 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010175{
10176 char_u *buf;
10177 char_u *path_end;
10178 char_u *p, *s, *e;
10179 int start_len = gap->ga_len;
10180 char_u *pat;
10181 regmatch_T regmatch;
10182 int starts_with_dot;
10183 int matches;
10184 int len;
10185 int starstar = FALSE;
10186 static int stardepth = 0; /* depth for "**" expansion */
10187
10188 DIR *dirp;
10189 struct dirent *dp;
10190
10191 /* Expanding "**" may take a long time, check for CTRL-C. */
10192 if (stardepth > 0)
10193 {
10194 ui_breakcheck();
10195 if (got_int)
10196 return 0;
10197 }
10198
10199 /* make room for file name */
10200 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10201 if (buf == NULL)
10202 return 0;
10203
10204 /*
10205 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010206 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010207 * Copy it into "buf", including the preceding characters.
10208 */
10209 p = buf;
10210 s = buf;
10211 e = NULL;
10212 path_end = path;
10213 while (*path_end != NUL)
10214 {
10215 /* May ignore a wildcard that has a backslash before it; it will
10216 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10217 if (path_end >= path + wildoff && rem_backslash(path_end))
10218 *p++ = *path_end++;
10219 else if (*path_end == '/')
10220 {
10221 if (e != NULL)
10222 break;
10223 s = p + 1;
10224 }
10225 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010226 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010227 || (!p_fic && (flags & EW_ICASE)
10228 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010229 e = p;
10230#ifdef FEAT_MBYTE
10231 if (has_mbyte)
10232 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010233 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010234 STRNCPY(p, path_end, len);
10235 p += len;
10236 path_end += len;
10237 }
10238 else
10239#endif
10240 *p++ = *path_end++;
10241 }
10242 e = p;
10243 *e = NUL;
10244
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010245 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010246 /* Remove backslashes between "wildoff" and the start of the wildcard
10247 * component. */
10248 for (p = buf + wildoff; p < s; ++p)
10249 if (rem_backslash(p))
10250 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010251 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010252 --e;
10253 --s;
10254 }
10255
10256 /* Check for "**" between "s" and "e". */
10257 for (p = s; p < e; ++p)
10258 if (p[0] == '*' && p[1] == '*')
10259 starstar = TRUE;
10260
10261 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010262 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010263 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10264 if (pat == NULL)
10265 {
10266 vim_free(buf);
10267 return 0;
10268 }
10269
10270 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010271 if (flags & EW_ICASE)
10272 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10273 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010274 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010275 if (flags & (EW_NOERROR | EW_NOTWILD))
10276 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010277 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010278 if (flags & (EW_NOERROR | EW_NOTWILD))
10279 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010280 vim_free(pat);
10281
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010282 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010283 {
10284 vim_free(buf);
10285 return 0;
10286 }
10287
10288 /* If "**" is by itself, this is the first time we encounter it and more
10289 * is following then find matches without any directory. */
10290 if (!didstar && stardepth < 100 && starstar && e - s == 2
10291 && *path_end == '/')
10292 {
10293 STRCPY(s, path_end + 1);
10294 ++stardepth;
10295 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10296 --stardepth;
10297 }
10298
10299 /* open the directory for scanning */
10300 *s = NUL;
10301 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10302
10303 /* Find all matching entries */
10304 if (dirp != NULL)
10305 {
10306 for (;;)
10307 {
10308 dp = readdir(dirp);
10309 if (dp == NULL)
10310 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010311 if ((dp->d_name[0] != '.' || starts_with_dot
10312 || ((flags & EW_DODOT)
10313 && dp->d_name[1] != NUL
10314 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010315 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10316 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010317 || ((flags & EW_NOTWILD)
10318 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010319 {
10320 STRCPY(s, dp->d_name);
10321 len = STRLEN(buf);
10322
10323 if (starstar && stardepth < 100)
10324 {
10325 /* For "**" in the pattern first go deeper in the tree to
10326 * find matches. */
10327 STRCPY(buf + len, "/**");
10328 STRCPY(buf + len + 3, path_end);
10329 ++stardepth;
10330 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10331 --stardepth;
10332 }
10333
10334 STRCPY(buf + len, path_end);
10335 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10336 {
10337 /* need to expand another component of the path */
10338 /* remove backslashes for the remaining components only */
10339 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10340 }
10341 else
10342 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010343 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010344
Bram Moolenaar231334e2005-07-25 20:46:57 +000010345 /* no more wildcards, check if there is a match */
10346 /* remove backslashes for the remaining components only */
10347 if (*path_end != NUL)
10348 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010349 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010350 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010351 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010352 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010353#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010354 size_t precomp_len = STRLEN(buf)+1;
10355 char_u *precomp_buf =
10356 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010357
Bram Moolenaar231334e2005-07-25 20:46:57 +000010358 if (precomp_buf)
10359 {
10360 mch_memmove(buf, precomp_buf, precomp_len);
10361 vim_free(precomp_buf);
10362 }
10363#endif
10364 addfile(gap, buf, flags);
10365 }
10366 }
10367 }
10368 }
10369
10370 closedir(dirp);
10371 }
10372
10373 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010374 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010375
10376 matches = gap->ga_len - start_len;
10377 if (matches > 0)
10378 qsort(((char_u **)gap->ga_data) + start_len, matches,
10379 sizeof(char_u *), pstrcmp);
10380 return matches;
10381}
10382#endif
10383
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010384#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010385static int find_previous_pathsep(char_u *path, char_u **psep);
10386static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10387static void expand_path_option(char_u *curdir, garray_T *gap);
10388static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10389static void uniquefy_paths(garray_T *gap, char_u *pattern);
10390static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010391
10392/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010393 * Moves "*psep" back to the previous path separator in "path".
10394 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010395 */
10396 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010397find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010398{
10399 /* skip the current separator */
10400 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010401 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010402
10403 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010404 while (*psep > path)
10405 {
10406 if (vim_ispathsep(**psep))
10407 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010408 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010409 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010410
10411 return FAIL;
10412}
10413
10414/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010415 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10416 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010417 */
10418 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010419is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010420{
10421 int j;
10422 int candidate_len;
10423 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010424 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010425 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010426
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010427 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010428 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010430 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010431
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010432 candidate_len = (int)STRLEN(maybe_unique);
10433 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010434 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010435 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010436
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010437 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010438 if (fnamecmp(maybe_unique, rival) == 0
10439 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010440 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010441 }
10442
Bram Moolenaar162bd912010-07-28 22:29:10 +020010443 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010444}
10445
10446/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010447 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010448 * paths are expanded to their equivalent fullpath. This includes the "."
10449 * (relative to current buffer directory) and empty path (relative to current
10450 * directory) notations.
10451 *
10452 * TODO: handle upward search (;) and path limiter (**N) notations by
10453 * expanding each into their equivalent path(s).
10454 */
10455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010456expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010457{
10458 char_u *path_option = *curbuf->b_p_path == NUL
10459 ? p_path : curbuf->b_p_path;
10460 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010461 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010462 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010463
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010464 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010465 return;
10466
10467 while (*path_option != NUL)
10468 {
10469 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10470
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010471 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010472 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010473 /* Relative to current buffer:
10474 * "/path/file" + "." -> "/path/"
10475 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010476 if (curbuf->b_ffname == NULL)
10477 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010478 p = gettail(curbuf->b_ffname);
10479 len = (int)(p - curbuf->b_ffname);
10480 if (len + (int)STRLEN(buf) >= MAXPATHL)
10481 continue;
10482 if (buf[1] == NUL)
10483 buf[len] = NUL;
10484 else
10485 STRMOVE(buf + len, buf + 2);
10486 mch_memmove(buf, curbuf->b_ffname, len);
10487 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010488 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010489 else if (buf[0] == NUL)
10490 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010491 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010492 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010493 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010494 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010495 else if (!mch_isFullName(buf))
10496 {
10497 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010498 len = (int)STRLEN(curdir);
10499 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010500 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010501 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010502 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010503 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010504 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010505 }
10506
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010507 if (ga_grow(gap, 1) == FAIL)
10508 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010509
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010510# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010511 /* Avoid the path ending in a backslash, it fails when a comma is
10512 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010513 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010514 if (buf[len - 1] == '\\')
10515 buf[len - 1] = '/';
10516# endif
10517
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010518 p = vim_strsave(buf);
10519 if (p == NULL)
10520 break;
10521 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010522 }
10523
10524 vim_free(buf);
10525}
10526
10527/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010528 * Returns a pointer to the file or directory name in "fname" that matches the
10529 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010530 *
10531 * path: /foo/bar/baz
10532 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010533 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010534 */
10535 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010536get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010537{
10538 int i;
10539 int maxlen = 0;
10540 char_u **path_part = (char_u **)gap->ga_data;
10541 char_u *cutoff = NULL;
10542
10543 for (i = 0; i < gap->ga_len; i++)
10544 {
10545 int j = 0;
10546
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010547 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010548# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010549 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10550#endif
10551 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010552 j++;
10553 if (j > maxlen)
10554 {
10555 maxlen = j;
10556 cutoff = &fname[j];
10557 }
10558 }
10559
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010560 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010561 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010562 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010563 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010564
10565 return cutoff;
10566}
10567
10568/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010569 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10570 * that they are unique with respect to each other while conserving the part
10571 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010572 */
10573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010574uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010575{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010576 int i;
10577 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010578 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010579 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010580 char_u *pat;
10581 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010582 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010583 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010584 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010585 char_u **in_curdir = NULL;
10586 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010587
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010588 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010589 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010590
10591 /*
10592 * We need to prepend a '*' at the beginning of file_pattern so that the
10593 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010594 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010596 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010597 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010598 if (file_pattern == NULL)
10599 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010600 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010601 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010602 STRCAT(file_pattern, pattern);
10603 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10604 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010605 if (pat == NULL)
10606 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010607
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010608 regmatch.rm_ic = TRUE; /* always ignore case */
10609 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10610 vim_free(pat);
10611 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010612 return;
10613
Bram Moolenaar162bd912010-07-28 22:29:10 +020010614 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010615 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010616 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010617 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010618
10619 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010620 if (in_curdir == NULL)
10621 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010622
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010623 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010624 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010625 char_u *path = fnames[i];
10626 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010627 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010628 char_u *pathsep_p;
10629 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010630
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010631 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010632 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010633 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010634 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010635 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010636
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010637 /* Shorten the filename while maintaining its uniqueness */
10638 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010640 /* Don't assume all files can be reached without path when search
10641 * pattern starts with star star slash, so only remove path_cutoff
10642 * when possible. */
10643 if (pattern[0] == '*' && pattern[1] == '*'
10644 && vim_ispathsep_nocolon(pattern[2])
10645 && path_cutoff != NULL
10646 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10647 && is_unique(path_cutoff, gap, i))
10648 {
10649 sort_again = TRUE;
10650 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10651 }
10652 else
10653 {
10654 /* Here all files can be reached without path, so get shortest
10655 * unique path. We start at the end of the path. */
10656 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010657
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010658 while (find_previous_pathsep(path, &pathsep_p))
10659 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10660 && is_unique(pathsep_p + 1, gap, i)
10661 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10662 {
10663 sort_again = TRUE;
10664 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10665 break;
10666 }
10667 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010668
10669 if (mch_isFullName(path))
10670 {
10671 /*
10672 * Last resort: shorten relative to curdir if possible.
10673 * 'possible' means:
10674 * 1. It is under the current directory.
10675 * 2. The result is actually shorter than the original.
10676 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010677 * Before curdir After
10678 * /foo/bar/file.txt /foo/bar ./file.txt
10679 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10680 * /file.txt / /file.txt
10681 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010682 */
10683 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010684 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010685#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010686 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010687 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010688 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010689 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010690 && !vim_ispathsep(*short_name)
10691#endif
10692 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010693 {
10694 STRCPY(path, ".");
10695 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010696 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010697 }
10698 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010699 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010700 }
10701
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010702 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010703 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010704 {
10705 char_u *rel_path;
10706 char_u *path = in_curdir[i];
10707
10708 if (path == NULL)
10709 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010710
10711 /* If the {filename} is not unique, change it to ./{filename}.
10712 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010713 short_name = shorten_fname(path, curdir);
10714 if (short_name == NULL)
10715 short_name = path;
10716 if (is_unique(short_name, gap, i))
10717 {
10718 STRCPY(fnames[i], short_name);
10719 continue;
10720 }
10721
10722 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10723 if (rel_path == NULL)
10724 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010725 STRCPY(rel_path, ".");
10726 add_pathsep(rel_path);
10727 STRCAT(rel_path, short_name);
10728
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010729 vim_free(fnames[i]);
10730 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010731 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010732 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010733 }
10734
Bram Moolenaar162bd912010-07-28 22:29:10 +020010735theend:
10736 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010737 if (in_curdir != NULL)
10738 {
10739 for (i = 0; i < gap->ga_len; i++)
10740 vim_free(in_curdir[i]);
10741 vim_free(in_curdir);
10742 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010743 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010744 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010745
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010746 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010747 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010748}
10749
10750/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010751 * Calls globpath() with 'path' values for the given pattern and stores the
10752 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010753 * Returns the total number of matches.
10754 */
10755 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010756expand_in_path(
10757 garray_T *gap,
10758 char_u *pattern,
10759 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010760{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010761 char_u *curdir;
10762 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010763 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010764 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010765
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010766 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010767 return 0;
10768 mch_dirname(curdir, MAXPATHL);
10769
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010770 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010771 expand_path_option(curdir, &path_ga);
10772 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010773 if (path_ga.ga_len == 0)
10774 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010775
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010776 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010777 ga_clear_strings(&path_ga);
10778 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010779 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010780
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010781 if (flags & EW_ICASE)
10782 glob_flags |= WILD_ICASE;
10783 if (flags & EW_ADDSLASH)
10784 glob_flags |= WILD_ADD_SLASH;
10785 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010786 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010787
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010788 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010789}
10790#endif
10791
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010792#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10793/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010794 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10795 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010796 */
10797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010798remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010799{
10800 int i;
10801 int j;
10802 char_u **fnames = (char_u **)gap->ga_data;
10803
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010804 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010805 for (i = gap->ga_len - 1; i > 0; --i)
10806 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10807 {
10808 vim_free(fnames[i]);
10809 for (j = i + 1; j < gap->ga_len; ++j)
10810 fnames[j - 1] = fnames[j];
10811 --gap->ga_len;
10812 }
10813}
10814#endif
10815
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010816static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010817
10818/*
10819 * Return TRUE if "p" contains what looks like an environment variable.
10820 * Allowing for escaping.
10821 */
10822 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010823has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010824{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010825 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010826 {
10827 if (*p == '\\' && p[1] != NUL)
10828 ++p;
10829 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010830#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010831 "$%"
10832#else
10833 "$"
10834#endif
10835 , *p) != NULL)
10836 return TRUE;
10837 }
10838 return FALSE;
10839}
10840
10841#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010842static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010843
10844/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010845 * Return TRUE if "p" contains a special wildcard character, one that Vim
10846 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010847 */
10848 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010849has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010850{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010851 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010852 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010853 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010854 if (*p == '\\' && p[1] != NUL)
10855 ++p;
10856 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10857 return TRUE;
10858 }
10859 return FALSE;
10860}
10861#endif
10862
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863/*
10864 * Generic wildcard expansion code.
10865 *
10866 * Characters in "pat" that should not be expanded must be preceded with a
10867 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10868 *
10869 * Return FAIL when no single file was found. In this case "num_file" is not
10870 * set, and "file" may contain an error message.
10871 * Return OK when some files found. "num_file" is set to the number of
10872 * matches, "file" to the array of matches. Call FreeWild() later.
10873 */
10874 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010875gen_expand_wildcards(
10876 int num_pat, /* number of input patterns */
10877 char_u **pat, /* array of input patterns */
10878 int *num_file, /* resulting number of files */
10879 char_u ***file, /* array of resulting files */
10880 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881{
10882 int i;
10883 garray_T ga;
10884 char_u *p;
10885 static int recursive = FALSE;
10886 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010887 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010888#if defined(FEAT_SEARCHPATH)
10889 int did_expand_in_path = FALSE;
10890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891
10892 /*
10893 * expand_env() is called to expand things like "~user". If this fails,
10894 * it calls ExpandOne(), which brings us back here. In this case, always
10895 * call the machine specific expansion function, if possible. Otherwise,
10896 * return FAIL.
10897 */
10898 if (recursive)
10899#ifdef SPECIAL_WILDCHAR
10900 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10901#else
10902 return FAIL;
10903#endif
10904
10905#ifdef SPECIAL_WILDCHAR
10906 /*
10907 * If there are any special wildcard characters which we cannot handle
10908 * here, call machine specific function for all the expansion. This
10909 * avoids starting the shell for each argument separately.
10910 * For `=expr` do use the internal function.
10911 */
10912 for (i = 0; i < num_pat; i++)
10913 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010914 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915# ifdef VIM_BACKTICK
10916 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10917# endif
10918 )
10919 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10920 }
10921#endif
10922
10923 recursive = TRUE;
10924
10925 /*
10926 * The matching file names are stored in a growarray. Init it empty.
10927 */
10928 ga_init2(&ga, (int)sizeof(char_u *), 30);
10929
10930 for (i = 0; i < num_pat; ++i)
10931 {
10932 add_pat = -1;
10933 p = pat[i];
10934
10935#ifdef VIM_BACKTICK
10936 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010939 if (add_pat == -1)
10940 retval = FAIL;
10941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 else
10943#endif
10944 {
10945 /*
10946 * First expand environment variables, "~/" and "~user/".
10947 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010948 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010950 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 if (p == NULL)
10952 p = pat[i];
10953#ifdef UNIX
10954 /*
10955 * On Unix, if expand_env() can't expand an environment
10956 * variable, use the shell to do that. Discard previously
10957 * found file names and start all over again.
10958 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010959 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 {
10961 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010962 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010964 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 recursive = FALSE;
10966 return i;
10967 }
10968#endif
10969 }
10970
10971 /*
10972 * If there are wildcards: Expand file names and add each match to
10973 * the list. If there is no match, and EW_NOTFOUND is given, add
10974 * the pattern.
10975 * If there are no wildcards: Add the file name if it exists or
10976 * when EW_NOTFOUND is given.
10977 */
10978 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010979 {
10980#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010981 if ((flags & EW_PATH)
10982 && !mch_isFullName(p)
10983 && !(p[0] == '.'
10984 && (vim_ispathsep(p[1])
10985 || (p[1] == '.' && vim_ispathsep(p[2]))))
10986 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010987 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010988 /* :find completion where 'path' is used.
10989 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010990 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010991 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010992 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010993 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010994 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010995 else
10996#endif
10997 add_pat = mch_expandpath(&ga, p, flags);
10998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 }
11000
11001 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11002 {
11003 char_u *t = backslash_halve_save(p);
11004
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11006 * "vim c:/" work. */
11007 if (flags & EW_NOTFOUND)
11008 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011009 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 addfile(&ga, t, flags);
11011 vim_free(t);
11012 }
11013
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011014#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011015 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011016 uniquefy_paths(&ga, p);
11017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 if (p != pat[i])
11019 vim_free(p);
11020 }
11021
11022 *num_file = ga.ga_len;
11023 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11024
11025 recursive = FALSE;
11026
Bram Moolenaar336bd622016-01-17 18:23:58 +010011027 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028}
11029
11030# ifdef VIM_BACKTICK
11031
11032/*
11033 * Return TRUE if we can expand this backtick thing here.
11034 */
11035 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011036vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
11038 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11039}
11040
11041/*
11042 * Expand an item in `backticks` by executing it as a command.
11043 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011044 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 */
11046 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011047expand_backtick(
11048 garray_T *gap,
11049 char_u *pat,
11050 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051{
11052 char_u *p;
11053 char_u *cmd;
11054 char_u *buffer;
11055 int cnt = 0;
11056 int i;
11057
11058 /* Create the command: lop off the backticks. */
11059 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11060 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011061 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062
11063#ifdef FEAT_EVAL
11064 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011065 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 else
11067#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011068 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011069 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 vim_free(cmd);
11071 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011072 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073
11074 cmd = buffer;
11075 while (*cmd != NUL)
11076 {
11077 cmd = skipwhite(cmd); /* skip over white space */
11078 p = cmd;
11079 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11080 ++p;
11081 /* add an entry if it is not empty */
11082 if (p > cmd)
11083 {
11084 i = *p;
11085 *p = NUL;
11086 addfile(gap, cmd, flags);
11087 *p = i;
11088 ++cnt;
11089 }
11090 cmd = p;
11091 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11092 ++cmd;
11093 }
11094
11095 vim_free(buffer);
11096 return cnt;
11097}
11098# endif /* VIM_BACKTICK */
11099
11100/*
11101 * Add a file to a file list. Accepted flags:
11102 * EW_DIR add directories
11103 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011104 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 * EW_NOTFOUND add even when it doesn't exist
11106 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011107 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 */
11109 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011110addfile(
11111 garray_T *gap,
11112 char_u *f, /* filename */
11113 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114{
11115 char_u *p;
11116 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011117 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011119 /* if the file/dir/link doesn't exist, may not add it */
11120 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011121 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 return;
11123
11124#ifdef FNAME_ILLEGAL
11125 /* if the file/dir contains illegal characters, don't add it */
11126 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11127 return;
11128#endif
11129
11130 isdir = mch_isdir(f);
11131 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11132 return;
11133
Bram Moolenaarb5971142015-03-21 17:32:19 +010011134 /* If the file isn't executable, may not add it. Do accept directories.
11135 * When invoked from expand_shellcmd() do not use $PATH. */
11136 if (!isdir && (flags & EW_EXEC)
11137 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011138 return;
11139
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 /* Make room for another item in the file list. */
11141 if (ga_grow(gap, 1) == FAIL)
11142 return;
11143
11144 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11145 if (p == NULL)
11146 return;
11147
11148 STRCPY(p, f);
11149#ifdef BACKSLASH_IN_FILENAME
11150 slash_adjust(p);
11151#endif
11152 /*
11153 * Append a slash or backslash after directory names if none is present.
11154 */
11155#ifndef DONT_ADD_PATHSEP_TO_DIR
11156 if (isdir && (flags & EW_ADDSLASH))
11157 add_pathsep(p);
11158#endif
11159 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160}
11161#endif /* !NO_EXPANDPATH */
11162
11163#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11164
11165#ifndef SEEK_SET
11166# define SEEK_SET 0
11167#endif
11168#ifndef SEEK_END
11169# define SEEK_END 2
11170#endif
11171
11172/*
11173 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011174 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11175 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 * Returns an allocated string, or NULL for error.
11177 */
11178 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011179get_cmd_output(
11180 char_u *cmd,
11181 char_u *infile, /* optional input file name */
11182 int flags, /* can be SHELL_SILENT */
11183 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
11185 char_u *tempname;
11186 char_u *command;
11187 char_u *buffer = NULL;
11188 int len;
11189 int i = 0;
11190 FILE *fd;
11191
11192 if (check_restricted() || check_secure())
11193 return NULL;
11194
11195 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011196 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 {
11198 EMSG(_(e_notmp));
11199 return NULL;
11200 }
11201
11202 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011203 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 if (command == NULL)
11205 goto done;
11206
11207 /*
11208 * Call the shell to execute the command (errors are ignored).
11209 * Don't check timestamps here.
11210 */
11211 ++no_check_timestamps;
11212 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11213 --no_check_timestamps;
11214
11215 vim_free(command);
11216
11217 /*
11218 * read the names from the file into memory
11219 */
11220# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011221 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 fd = mch_fopen((char *)tempname, "r");
11223# else
11224 fd = mch_fopen((char *)tempname, READBIN);
11225# endif
11226
11227 if (fd == NULL)
11228 {
11229 EMSG2(_(e_notopen), tempname);
11230 goto done;
11231 }
11232
11233 fseek(fd, 0L, SEEK_END);
11234 len = ftell(fd); /* get size of temp file */
11235 fseek(fd, 0L, SEEK_SET);
11236
11237 buffer = alloc(len + 1);
11238 if (buffer != NULL)
11239 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11240 fclose(fd);
11241 mch_remove(tempname);
11242 if (buffer == NULL)
11243 goto done;
11244#ifdef VMS
11245 len = i; /* VMS doesn't give us what we asked for... */
11246#endif
11247 if (i != len)
11248 {
11249 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011250 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011252 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011253 {
11254 /* Change NUL into SOH, otherwise the string is truncated. */
11255 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011256 if (buffer[i] == NUL)
11257 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011258
Bram Moolenaar162bd912010-07-28 22:29:10 +020011259 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011260 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011261 else
11262 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263
11264done:
11265 vim_free(tempname);
11266 return buffer;
11267}
11268#endif
11269
11270/*
11271 * Free the list of files returned by expand_wildcards() or other expansion
11272 * functions.
11273 */
11274 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011275FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011277 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 while (count--)
11280 vim_free(files[count]);
11281 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282}
11283
11284/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011285 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 * Don't do this when still processing a command or a mapping.
11287 * Don't do this when inside a ":normal" command.
11288 */
11289 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011290goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
11292 return (p_im && stuff_empty() && typebuf_typed());
11293}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011294
11295/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011296 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011297 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11298 * - Remove any argument. E.g., "csh -f" -> "csh".
11299 * But don't allow a space in the path, so that this works:
11300 * "/usr/bin/csh --rcfile ~/.cshrc"
11301 * But don't do that for Windows, it's common to have a space in the path.
11302 */
11303 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011304get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011305{
11306 char_u *p;
11307
11308#ifdef WIN3264
11309 p = gettail(p_sh);
11310 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11311#else
11312 p = skiptowhite(p_sh);
11313 if (*p == NUL)
11314 {
11315 /* No white space, use the tail. */
11316 p = vim_strsave(gettail(p_sh));
11317 }
11318 else
11319 {
11320 char_u *p1, *p2;
11321
11322 /* Find the last path separator before the space. */
11323 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011324 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011325 if (vim_ispathsep(*p2))
11326 p1 = p2 + 1;
11327 p = vim_strnsave(p1, (int)(p - p1));
11328 }
11329#endif
11330 return p;
11331}