blob: 593dce1c3d14a553197907fd55107dc7352f3ada [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). */
3748 vim_free(homedir);
3749 homedir = NULL;
3750
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751#ifdef VMS
3752 var = mch_getenv((char_u *)"SYS$LOGIN");
3753#else
3754 var = mch_getenv((char_u *)"HOME");
3755#endif
3756
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#ifdef WIN3264
3758 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003759 * Typically, $HOME is not defined on Windows, unless the user has
3760 * specifically defined it for Vim's sake. However, on Windows NT
3761 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3762 * each user. Try constructing $HOME from these.
3763 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003764 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003765 {
3766 char_u *homedrive, *homepath;
3767
3768 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3769 homepath = mch_getenv((char_u *)"HOMEPATH");
3770 if (homepath == NULL || *homepath == NUL)
3771 homepath = (char_u *)"\\";
3772 if (homedrive != NULL
3773 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3774 {
3775 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3776 if (NameBuff[0] != NUL)
3777 var = NameBuff;
3778 }
3779 }
3780
3781 if (var == NULL)
3782 var = mch_getenv((char_u *)"USERPROFILE");
3783
3784 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 * Weird but true: $HOME may contain an indirect reference to another
3786 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3787 * when $HOME is being set.
3788 */
3789 if (var != NULL && *var == '%')
3790 {
3791 char_u *p;
3792 char_u *exp;
3793
3794 p = vim_strchr(var + 1, '%');
3795 if (p != NULL)
3796 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003797 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 exp = mch_getenv(NameBuff);
3799 if (exp != NULL && *exp != NUL
3800 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3801 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003802 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 }
3806 }
3807
Bram Moolenaar48340b62017-08-29 22:08:53 +02003808 if (var != NULL && *var == NUL) /* empty is same as not set */
3809 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaar48340b62017-08-29 22:08:53 +02003811# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003812 if (enc_utf8 && var != NULL)
3813 {
3814 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003815 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003816
3817 /* Convert from active codepage to UTF-8. Other conversions are
3818 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003819 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003820 if (pp != NULL)
3821 {
3822 homedir = pp;
3823 return;
3824 }
3825 }
3826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 /*
3829 * Default home dir is C:/
3830 * Best assumption we can make in such a situation.
3831 */
3832 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003833 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 if (var != NULL)
3837 {
3838#ifdef UNIX
3839 /*
3840 * Change to the directory and get the actual path. This resolves
3841 * links. Don't do it when we can't return.
3842 */
3843 if (mch_dirname(NameBuff, MAXPATHL) == OK
3844 && mch_chdir((char *)NameBuff) == 0)
3845 {
3846 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3847 var = IObuff;
3848 if (mch_chdir((char *)NameBuff) != 0)
3849 EMSG(_(e_prev_dir));
3850 }
3851#endif
3852 homedir = vim_strsave(var);
3853 }
3854}
3855
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003856#if defined(EXITFREE) || defined(PROTO)
3857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003858free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003859{
3860 vim_free(homedir);
3861}
Bram Moolenaar24305862012-08-15 14:05:05 +02003862
3863# ifdef FEAT_CMDL_COMPL
3864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003865free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003866{
3867 ga_clear_strings(&ga_users);
3868}
3869# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003870#endif
3871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003873 * Call expand_env() and store the result in an allocated string.
3874 * This is not very memory efficient, this expects the result to be freed
3875 * again soon.
3876 */
3877 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003878expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003879{
3880 return expand_env_save_opt(src, FALSE);
3881}
3882
3883/*
3884 * Idem, but when "one" is TRUE handle the string as one file name, only
3885 * expand "~" at the start.
3886 */
3887 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003888expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003889{
3890 char_u *p;
3891
3892 p = alloc(MAXPATHL);
3893 if (p != NULL)
3894 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3895 return p;
3896}
3897
3898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 * Expand environment variable with path name.
3900 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003901 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 * If anything fails no expansion is done and dst equals src.
3903 */
3904 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003905expand_env(
3906 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3907 char_u *dst, /* where to put the result */
3908 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003910 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911}
3912
3913 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003914expand_env_esc(
3915 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3916 char_u *dst, /* where to put the result */
3917 int dstlen, /* maximum length of the result */
3918 int esc, /* escape spaces in expanded variables */
3919 int one, /* "srcp" is one file name */
3920 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003922 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *tail;
3924 int c;
3925 char_u *var;
3926 int copy_char;
3927 int mustfree; /* var was allocated, need to free it later */
3928 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003929 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003931 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003932 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003933
3934 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 --dstlen; /* leave one char space for "\," */
3936 while (*src && dstlen > 0)
3937 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003938#ifdef FEAT_EVAL
3939 /* Skip over `=expr`. */
3940 if (src[0] == '`' && src[1] == '=')
3941 {
3942 size_t len;
3943
3944 var = src;
3945 src += 2;
3946 (void)skip_expr(&src);
3947 if (*src == '`')
3948 ++src;
3949 len = src - var;
3950 if (len > (size_t)dstlen)
3951 len = dstlen;
3952 vim_strncpy(dst, var, len);
3953 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003954 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003955 continue;
3956 }
3957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003959 if ((*src == '$'
3960#ifdef VMS
3961 && at_start
3962#endif
3963 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003964#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 || *src == '%'
3966#endif
3967 || (*src == '~' && at_start))
3968 {
3969 mustfree = FALSE;
3970
3971 /*
3972 * The variable name is copied into dst temporarily, because it may
3973 * be a string in read-only memory and a NUL needs to be appended.
3974 */
3975 if (*src != '~') /* environment var */
3976 {
3977 tail = src + 1;
3978 var = dst;
3979 c = dstlen - 1;
3980
3981#ifdef UNIX
3982 /* Unix has ${var-name} type environment vars */
3983 if (*tail == '{' && !vim_isIDc('{'))
3984 {
3985 tail++; /* ignore '{' */
3986 while (c-- > 0 && *tail && *tail != '}')
3987 *var++ = *tail++;
3988 }
3989 else
3990#endif
3991 {
3992 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003993#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 || (*src == '%' && *tail != '%')
3995#endif
3996 ))
3997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000 }
4001
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004002#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003# ifdef UNIX
4004 if (src[1] == '{' && *tail != '}')
4005# else
4006 if (*src == '%' && *tail != '%')
4007# endif
4008 var = NULL;
4009 else
4010 {
4011# ifdef UNIX
4012 if (src[1] == '{')
4013# else
4014 if (*src == '%')
4015#endif
4016 ++tail;
4017#endif
4018 *var = NUL;
4019 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004020#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022#endif
4023 }
4024 /* home directory */
4025 else if ( src[1] == NUL
4026 || vim_ispathsep(src[1])
4027 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4028 {
4029 var = homedir;
4030 tail = src + 1;
4031 }
4032 else /* user directory */
4033 {
4034#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4035 /*
4036 * Copy ~user to dst[], so we can put a NUL after it.
4037 */
4038 tail = src;
4039 var = dst;
4040 c = dstlen - 1;
4041 while ( c-- > 0
4042 && *tail
4043 && vim_isfilec(*tail)
4044 && !vim_ispathsep(*tail))
4045 *var++ = *tail++;
4046 *var = NUL;
4047# ifdef UNIX
4048 /*
4049 * If the system supports getpwnam(), use it.
4050 * Otherwise, or if getpwnam() fails, the shell is used to
4051 * expand ~user. This is slower and may fail if the shell
4052 * does not support ~user (old versions of /bin/sh).
4053 */
4054# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4055 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004056 /* Note: memory allocated by getpwnam() is never freed.
4057 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004058 struct passwd *pw = (*dst == NUL)
4059 ? NULL : getpwnam((char *)dst + 1);
4060
4061 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 if (var == NULL)
4064# endif
4065 {
4066 expand_T xpc;
4067
4068 ExpandInit(&xpc);
4069 xpc.xp_context = EXPAND_FILES;
4070 var = ExpandOne(&xpc, dst, NULL,
4071 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 mustfree = TRUE;
4073 }
4074
4075# else /* !UNIX, thus VMS */
4076 /*
4077 * USER_HOME is a comma-separated list of
4078 * directories to search for the user account in.
4079 */
4080 {
4081 char_u test[MAXPATHL], paths[MAXPATHL];
4082 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004083 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 STRCPY(paths, USER_HOME);
4086 next_path = paths;
4087 while (*next_path)
4088 {
4089 for (path = next_path; *next_path && *next_path != ',';
4090 next_path++);
4091 if (*next_path)
4092 *next_path++ = NUL;
4093 STRCPY(test, path);
4094 STRCAT(test, "/");
4095 STRCAT(test, dst + 1);
4096 if (mch_stat(test, &st) == 0)
4097 {
4098 var = alloc(STRLEN(test) + 1);
4099 STRCPY(var, test);
4100 mustfree = TRUE;
4101 break;
4102 }
4103 }
4104 }
4105# endif /* UNIX */
4106#else
4107 /* cannot expand user's home directory, so don't try */
4108 var = NULL;
4109 tail = (char_u *)""; /* for gcc */
4110#endif /* UNIX || VMS */
4111 }
4112
4113#ifdef BACKSLASH_IN_FILENAME
4114 /* If 'shellslash' is set change backslashes to forward slashes.
4115 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4116 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4117 {
4118 char_u *p = vim_strsave(var);
4119
4120 if (p != NULL)
4121 {
4122 if (mustfree)
4123 vim_free(var);
4124 var = p;
4125 mustfree = TRUE;
4126 forward_slash(var);
4127 }
4128 }
4129#endif
4130
4131 /* If "var" contains white space, escape it with a backslash.
4132 * Required for ":e ~/tt" when $HOME includes a space. */
4133 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4134 {
4135 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4136
4137 if (p != NULL)
4138 {
4139 if (mustfree)
4140 vim_free(var);
4141 var = p;
4142 mustfree = TRUE;
4143 }
4144 }
4145
4146 if (var != NULL && *var != NUL
4147 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4148 {
4149 STRCPY(dst, var);
4150 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004151 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /* if var[] ends in a path separator and tail[] starts
4153 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004154 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4156 && dst[-1] != ':'
4157#endif
4158 && vim_ispathsep(*tail))
4159 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004160 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 src = tail;
4162 copy_char = FALSE;
4163 }
4164 if (mustfree)
4165 vim_free(var);
4166 }
4167
4168 if (copy_char) /* copy at least one char */
4169 {
4170 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004171 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004172 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4173 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 */
4175 at_start = FALSE;
4176 if (src[0] == '\\' && src[1] != NUL)
4177 {
4178 *dst++ = *src++;
4179 --dstlen;
4180 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004181 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004183 if (dstlen > 0)
4184 {
4185 *dst++ = *src++;
4186 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004187
Bram Moolenaar1c864092017-08-06 18:15:45 +02004188 if (startstr != NULL && src - startstr_len >= srcp
4189 && STRNCMP(src - startstr_len, startstr,
4190 startstr_len) == 0)
4191 at_start = TRUE;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 *dst = NUL;
4197}
4198
4199/*
4200 * Vim's version of getenv().
4201 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004202 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004203 * "mustfree" is set to TRUE when returned is allocated, it must be
4204 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 */
4206 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004207vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 char_u *p;
4210 char_u *pend;
4211 int vimruntime;
4212
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004213#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 /* use "C:/" when $HOME is not set */
4215 if (STRCMP(name, "HOME") == 0)
4216 return homedir;
4217#endif
4218
4219 p = mch_getenv(name);
4220 if (p != NULL && *p == NUL) /* empty is the same as not set */
4221 p = NULL;
4222
4223 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004224 {
4225#if defined(FEAT_MBYTE) && defined(WIN3264)
4226 if (enc_utf8)
4227 {
4228 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004229 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004230
4231 /* Convert from active codepage to UTF-8. Other conversions are
4232 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004233 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004234 if (pp != NULL)
4235 {
4236 p = pp;
4237 *mustfree = TRUE;
4238 }
4239 }
4240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4245 if (!vimruntime && STRCMP(name, "VIM") != 0)
4246 return NULL;
4247
4248 /*
4249 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4250 * Don't do this when default_vimruntime_dir is non-empty.
4251 */
4252 if (vimruntime
4253#ifdef HAVE_PATHDEF
4254 && *default_vimruntime_dir == NUL
4255#endif
4256 )
4257 {
4258 p = mch_getenv((char_u *)"VIM");
4259 if (p != NULL && *p == NUL) /* empty is the same as not set */
4260 p = NULL;
4261 if (p != NULL)
4262 {
4263 p = vim_version_dir(p);
4264 if (p != NULL)
4265 *mustfree = TRUE;
4266 else
4267 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004268
4269#if defined(FEAT_MBYTE) && defined(WIN3264)
4270 if (enc_utf8)
4271 {
4272 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004273 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004274
4275 /* Convert from active codepage to UTF-8. Other conversions
4276 * are not done, because they would fail for non-ASCII
4277 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004278 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004279 if (pp != NULL)
4280 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004281 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004282 vim_free(p);
4283 p = pp;
4284 *mustfree = TRUE;
4285 }
4286 }
4287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 }
4290
4291 /*
4292 * When expanding $VIM or $VIMRUNTIME fails, try using:
4293 * - the directory name from 'helpfile' (unless it contains '$')
4294 * - the executable name from argv[0]
4295 */
4296 if (p == NULL)
4297 {
4298 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4299 p = p_hf;
4300#ifdef USE_EXE_NAME
4301 /*
4302 * Use the name of the executable, obtained from argv[0].
4303 */
4304 else
4305 p = exe_name;
4306#endif
4307 if (p != NULL)
4308 {
4309 /* remove the file name */
4310 pend = gettail(p);
4311
4312 /* remove "doc/" from 'helpfile', if present */
4313 if (p == p_hf)
4314 pend = remove_tail(p, pend, (char_u *)"doc");
4315
4316#ifdef USE_EXE_NAME
4317# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004318 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (p == exe_name)
4320 {
4321 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004322 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004324 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4325 if (pend1 != pend)
4326 {
4327 pnew = alloc((unsigned)(pend1 - p) + 15);
4328 if (pnew != NULL)
4329 {
4330 STRNCPY(pnew, p, (pend1 - p));
4331 STRCPY(pnew + (pend1 - p), "Resources/vim");
4332 p = pnew;
4333 pend = p + STRLEN(p);
4334 }
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337# endif
4338 /* remove "src/" from exe_name, if present */
4339 if (p == exe_name)
4340 pend = remove_tail(p, pend, (char_u *)"src");
4341#endif
4342
4343 /* for $VIM, remove "runtime/" or "vim54/", if present */
4344 if (!vimruntime)
4345 {
4346 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4347 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4348 }
4349
4350 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004351 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004354#ifdef MACOS_X
4355 if (p == exe_name || p == p_hf)
4356#endif
4357 /* check that the result is a directory name */
4358 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 if (p != NULL && !mch_isdir(p))
4361 {
4362 vim_free(p);
4363 p = NULL;
4364 }
4365 else
4366 {
4367#ifdef USE_EXE_NAME
4368 /* may add "/vim54" or "/runtime" if it exists */
4369 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4370 {
4371 vim_free(p);
4372 p = pend;
4373 }
4374#endif
4375 *mustfree = TRUE;
4376 }
4377 }
4378 }
4379
4380#ifdef HAVE_PATHDEF
4381 /* When there is a pathdef.c file we can use default_vim_dir and
4382 * default_vimruntime_dir */
4383 if (p == NULL)
4384 {
4385 /* Only use default_vimruntime_dir when it is not empty */
4386 if (vimruntime && *default_vimruntime_dir != NUL)
4387 {
4388 p = default_vimruntime_dir;
4389 *mustfree = FALSE;
4390 }
4391 else if (*default_vim_dir != NUL)
4392 {
4393 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4394 *mustfree = TRUE;
4395 else
4396 {
4397 p = default_vim_dir;
4398 *mustfree = FALSE;
4399 }
4400 }
4401 }
4402#endif
4403
4404 /*
4405 * Set the environment variable, so that the new value can be found fast
4406 * next time, and others can also use it (e.g. Perl).
4407 */
4408 if (p != NULL)
4409 {
4410 if (vimruntime)
4411 {
4412 vim_setenv((char_u *)"VIMRUNTIME", p);
4413 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 else
4416 {
4417 vim_setenv((char_u *)"VIM", p);
4418 didset_vim = TRUE;
4419 }
4420 }
4421 return p;
4422}
4423
4424/*
4425 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4426 * Return NULL if not, return its name in allocated memory otherwise.
4427 */
4428 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004429vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
4431 char_u *p;
4432
4433 if (vimdir == NULL || *vimdir == NUL)
4434 return NULL;
4435 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4436 if (p != NULL && mch_isdir(p))
4437 return p;
4438 vim_free(p);
4439 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4440 if (p != NULL && mch_isdir(p))
4441 return p;
4442 vim_free(p);
4443 return NULL;
4444}
4445
4446/*
4447 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4448 * the length of "name/". Otherwise return "pend".
4449 */
4450 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004451remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
4453 int len = (int)STRLEN(name) + 1;
4454 char_u *newend = pend - len;
4455
4456 if (newend >= p
4457 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004458 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return newend;
4460 return pend;
4461}
4462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 * Our portable version of setenv.
4465 */
4466 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004467vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469#ifdef HAVE_SETENV
4470 mch_setenv((char *)name, (char *)val, 1);
4471#else
4472 char_u *envbuf;
4473
4474 /*
4475 * Putenv does not copy the string, it has to remain
4476 * valid. The allocated memory will never be freed.
4477 */
4478 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4479 if (envbuf != NULL)
4480 {
4481 sprintf((char *)envbuf, "%s=%s", name, val);
4482 putenv((char *)envbuf);
4483 }
4484#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004485#ifdef FEAT_GETTEXT
4486 /*
4487 * When setting $VIMRUNTIME adjust the directory to find message
4488 * translations to $VIMRUNTIME/lang.
4489 */
4490 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4491 {
4492 char_u *buf = concat_str(val, (char_u *)"/lang");
4493
4494 if (buf != NULL)
4495 {
4496 bindtextdomain(VIMPACKAGE, (char *)buf);
4497 vim_free(buf);
4498 }
4499 }
4500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501}
4502
4503#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4504/*
4505 * Function given to ExpandGeneric() to obtain an environment variable name.
4506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004508get_env_name(
4509 expand_T *xp UNUSED,
4510 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
Bram Moolenaard0573012017-10-28 21:11:06 +02004512# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004514 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
4516 return NULL;
4517# else
4518# ifndef __WIN32__
4519 /* Borland C++ 5.2 has this in a header file. */
4520 extern char **environ;
4521# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004522# define ENVNAMELEN 100
4523 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 char_u *str;
4525 int n;
4526
4527 str = (char_u *)environ[idx];
4528 if (str == NULL)
4529 return NULL;
4530
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004531 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
4533 if (str[n] == '=' || str[n] == NUL)
4534 break;
4535 name[n] = str[n];
4536 }
4537 name[n] = NUL;
4538 return name;
4539# endif
4540}
Bram Moolenaar24305862012-08-15 14:05:05 +02004541
4542/*
4543 * Find all user names for user completion.
4544 * Done only once and then cached.
4545 */
4546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004547init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004548{
Bram Moolenaar24305862012-08-15 14:05:05 +02004549 static int lazy_init_done = FALSE;
4550
4551 if (lazy_init_done)
4552 return;
4553
4554 lazy_init_done = TRUE;
4555 ga_init2(&ga_users, sizeof(char_u *), 20);
4556
4557# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4558 {
4559 char_u* user;
4560 struct passwd* pw;
4561
4562 setpwent();
4563 while ((pw = getpwent()) != NULL)
4564 /* pw->pw_name shouldn't be NULL but just in case... */
4565 if (pw->pw_name != NULL)
4566 {
4567 if (ga_grow(&ga_users, 1) == FAIL)
4568 break;
4569 user = vim_strsave((char_u*)pw->pw_name);
4570 if (user == NULL)
4571 break;
4572 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4573 }
4574 endpwent();
4575 }
4576# endif
4577}
4578
4579/*
4580 * Function given to ExpandGeneric() to obtain an user names.
4581 */
4582 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004583get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004584{
4585 init_users();
4586 if (idx < ga_users.ga_len)
4587 return ((char_u **)ga_users.ga_data)[idx];
4588 return NULL;
4589}
4590
4591/*
4592 * Check whether name matches a user name. Return:
4593 * 0 if name does not match any user name.
4594 * 1 if name partially matches the beginning of a user name.
4595 * 2 is name fully matches a user name.
4596 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004597int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004598{
4599 int i;
4600 int n = (int)STRLEN(name);
4601 int result = 0;
4602
4603 init_users();
4604 for (i = 0; i < ga_users.ga_len; i++)
4605 {
4606 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4607 return 2; /* full match */
4608 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4609 result = 1; /* partial match */
4610 }
4611 return result;
4612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#endif
4614
4615/*
4616 * Replace home directory by "~" in each space or comma separated file name in
4617 * 'src'.
4618 * If anything fails (except when out of space) dst equals src.
4619 */
4620 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004621home_replace(
4622 buf_T *buf, /* when not NULL, check for help files */
4623 char_u *src, /* input file name */
4624 char_u *dst, /* where to put the result */
4625 int dstlen, /* maximum length of the result */
4626 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 spaces and commas in the file name. */
4628{
4629 size_t dirlen = 0, envlen = 0;
4630 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004631 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 char_u *p;
4633
4634 if (src == NULL)
4635 {
4636 *dst = NUL;
4637 return;
4638 }
4639
4640 /*
4641 * If the file is a help file, remove the path completely.
4642 */
4643 if (buf != NULL && buf->b_help)
4644 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004645 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return;
4647 }
4648
4649 /*
4650 * We check both the value of the $HOME environment variable and the
4651 * "real" home directory.
4652 */
4653 if (homedir != NULL)
4654 dirlen = STRLEN(homedir);
4655
4656#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004657 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004659 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4660#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004661#ifdef WIN3264
4662 if (homedir_env == NULL)
4663 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4664#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004665 /* Empty is the same as not set. */
4666 if (homedir_env != NULL && *homedir_env == NUL)
4667 homedir_env = NULL;
4668
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004669#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004670 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004671 {
4672 int usedlen = 0;
4673 int flen;
4674 char_u *fbuf = NULL;
4675
4676 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004677 (void)modify_fname((char_u *)":p", &usedlen,
4678 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004679 flen = (int)STRLEN(homedir_env);
4680 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4681 /* Remove the trailing / that is added to a directory. */
4682 homedir_env[flen - 1] = NUL;
4683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#endif
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (homedir_env != NULL)
4687 envlen = STRLEN(homedir_env);
4688
4689 if (!one)
4690 src = skipwhite(src);
4691 while (*src && dstlen > 0)
4692 {
4693 /*
4694 * Here we are at the beginning of a file name.
4695 * First, check to see if the beginning of the file name matches
4696 * $HOME or the "real" home directory. Check that there is a '/'
4697 * after the match (so that if e.g. the file is "/home/pieter/bla",
4698 * and the home directory is "/home/piet", the file does not end up
4699 * as "~er/bla" (which would seem to indicate the file "bla" in user
4700 * er's home directory)).
4701 */
4702 p = homedir;
4703 len = dirlen;
4704 for (;;)
4705 {
4706 if ( len
4707 && fnamencmp(src, p, len) == 0
4708 && (vim_ispathsep(src[len])
4709 || (!one && (src[len] == ',' || src[len] == ' '))
4710 || src[len] == NUL))
4711 {
4712 src += len;
4713 if (--dstlen > 0)
4714 *dst++ = '~';
4715
4716 /*
4717 * If it's just the home directory, add "/".
4718 */
4719 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4720 *dst++ = '/';
4721 break;
4722 }
4723 if (p == homedir_env)
4724 break;
4725 p = homedir_env;
4726 len = envlen;
4727 }
4728
4729 /* if (!one) skip to separator: space or comma */
4730 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4731 *dst++ = *src++;
4732 /* skip separator */
4733 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4734 *dst++ = *src++;
4735 }
4736 /* if (dstlen == 0) out of space, what to do??? */
4737
4738 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004739
4740 if (homedir_env != homedir_env_orig)
4741 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
4743
4744/*
4745 * Like home_replace, store the replaced string in allocated memory.
4746 * When something fails, NULL is returned.
4747 */
4748 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004749home_replace_save(
4750 buf_T *buf, /* when not NULL, check for help files */
4751 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 char_u *dst;
4754 unsigned len;
4755
4756 len = 3; /* space for "~/" and trailing NUL */
4757 if (src != NULL) /* just in case */
4758 len += (unsigned)STRLEN(src);
4759 dst = alloc(len);
4760 if (dst != NULL)
4761 home_replace(buf, src, dst, len, TRUE);
4762 return dst;
4763}
4764
4765/*
4766 * Compare two file names and return:
4767 * FPC_SAME if they both exist and are the same file.
4768 * FPC_SAMEX if they both don't exist and have the same file name.
4769 * FPC_DIFF if they both exist and are different files.
4770 * FPC_NOTX if they both don't exist.
4771 * FPC_DIFFX if one of them doesn't exist.
4772 * For the first name environment variables are expanded
4773 */
4774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004775fullpathcmp(
4776 char_u *s1,
4777 char_u *s2,
4778 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780#ifdef UNIX
4781 char_u exp1[MAXPATHL];
4782 char_u full1[MAXPATHL];
4783 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004784 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 int r1, r2;
4786
4787 expand_env(s1, exp1, MAXPATHL);
4788 r1 = mch_stat((char *)exp1, &st1);
4789 r2 = mch_stat((char *)s2, &st2);
4790 if (r1 != 0 && r2 != 0)
4791 {
4792 /* if mch_stat() doesn't work, may compare the names */
4793 if (checkname)
4794 {
4795 if (fnamecmp(exp1, s2) == 0)
4796 return FPC_SAMEX;
4797 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4798 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4799 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4800 return FPC_SAMEX;
4801 }
4802 return FPC_NOTX;
4803 }
4804 if (r1 != 0 || r2 != 0)
4805 return FPC_DIFFX;
4806 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4807 return FPC_SAME;
4808 return FPC_DIFF;
4809#else
4810 char_u *exp1; /* expanded s1 */
4811 char_u *full1; /* full path of s1 */
4812 char_u *full2; /* full path of s2 */
4813 int retval = FPC_DIFF;
4814 int r1, r2;
4815
4816 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4817 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4818 {
4819 full1 = exp1 + MAXPATHL;
4820 full2 = full1 + MAXPATHL;
4821
4822 expand_env(s1, exp1, MAXPATHL);
4823 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4824 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4825
4826 /* If vim_FullName() fails, the file probably doesn't exist. */
4827 if (r1 != OK && r2 != OK)
4828 {
4829 if (checkname && fnamecmp(exp1, s2) == 0)
4830 retval = FPC_SAMEX;
4831 else
4832 retval = FPC_NOTX;
4833 }
4834 else if (r1 != OK || r2 != OK)
4835 retval = FPC_DIFFX;
4836 else if (fnamecmp(full1, full2))
4837 retval = FPC_DIFF;
4838 else
4839 retval = FPC_SAME;
4840 vim_free(exp1);
4841 }
4842 return retval;
4843#endif
4844}
4845
4846/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004847 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004848 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004849 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 */
4851 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004852gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 char_u *p1, *p2;
4855
4856 if (fname == NULL)
4857 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004858 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004860 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004862 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 return p1;
4865}
4866
Bram Moolenaar31710262010-08-13 13:36:15 +02004867#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004868static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004869
4870/*
4871 * Return the end of the directory name, on the first path
4872 * separator:
4873 * "/path/file", "/path/dir/", "/path//dir", "/file"
4874 * ^ ^ ^ ^
4875 */
4876 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004877gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004878{
4879 char_u *dir_end = fname;
4880 char_u *next_dir_end = fname;
4881 int look_for_sep = TRUE;
4882 char_u *p;
4883
4884 for (p = fname; *p != NUL; )
4885 {
4886 if (vim_ispathsep(*p))
4887 {
4888 if (look_for_sep)
4889 {
4890 next_dir_end = p;
4891 look_for_sep = FALSE;
4892 }
4893 }
4894 else
4895 {
4896 if (!look_for_sep)
4897 dir_end = next_dir_end;
4898 look_for_sep = TRUE;
4899 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004900 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004901 }
4902 return dir_end;
4903}
4904#endif
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004907 * Get pointer to tail of "fname", including path separators. Putting a NUL
4908 * here leaves the directory name. Takes care of "c:/" and "//".
4909 * Always returns a valid pointer.
4910 */
4911 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004912gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004913{
4914 char_u *p;
4915 char_u *t;
4916
4917 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4918 t = gettail(fname);
4919 while (t > p && after_pathsep(fname, t))
4920 --t;
4921#ifdef VMS
4922 /* path separator is part of the path */
4923 ++t;
4924#endif
4925 return t;
4926}
4927
4928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 * get the next path component (just after the next path separator).
4930 */
4931 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004932getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004935 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (*fname)
4937 ++fname;
4938 return fname;
4939}
4940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941/*
4942 * Get a pointer to one character past the head of a path name.
4943 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4944 * If there is no head, path is returned.
4945 */
4946 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004947get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
4949 char_u *retval;
4950
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004951#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /* may skip "c:" */
4953 if (isalpha(path[0]) && path[1] == ':')
4954 retval = path + 2;
4955 else
4956 retval = path;
4957#else
4958# if defined(AMIGA)
4959 /* may skip "label:" */
4960 retval = vim_strchr(path, ':');
4961 if (retval == NULL)
4962 retval = path;
4963# else /* Unix */
4964 retval = path;
4965# endif
4966#endif
4967
4968 while (vim_ispathsep(*retval))
4969 ++retval;
4970
4971 return retval;
4972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004975 * Return TRUE if 'c' is a path separator.
4976 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 */
4978 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004979vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004981#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004983#else
4984# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004986# else
4987# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4989 return (c == ':' || c == '[' || c == ']' || c == '/'
4990 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004991# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004993# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996}
4997
Bram Moolenaar69c35002013-11-04 02:54:12 +01004998/*
4999 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5000 */
5001 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005002vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005003{
5004 return vim_ispathsep(c)
5005#ifdef BACKSLASH_IN_FILENAME
5006 && c != ':'
5007#endif
5008 ;
5009}
5010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5012/*
5013 * return TRUE if 'c' is a path list separator.
5014 */
5015 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005016vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018#ifdef UNIX
5019 return (c == ':');
5020#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005021 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022#endif
5023}
5024#endif
5025
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026/*
5027 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5028 * It's done in-place.
5029 */
5030 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005031shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005032{
5033 char_u *tail, *s, *d;
5034 int skip = FALSE;
5035
5036 tail = gettail(str);
5037 d = str;
5038 for (s = str; ; ++s)
5039 {
5040 if (s >= tail) /* copy the whole tail */
5041 {
5042 *d++ = *s;
5043 if (*s == NUL)
5044 break;
5045 }
5046 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5047 {
5048 *d++ = *s;
5049 skip = FALSE;
5050 }
5051 else if (!skip)
5052 {
5053 *d++ = *s; /* copy next char */
5054 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5055 skip = TRUE;
5056# ifdef FEAT_MBYTE
5057 if (has_mbyte)
5058 {
5059 int l = mb_ptr2len(s);
5060
5061 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005062 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005063 }
5064# endif
5065 }
5066 }
5067}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005068
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005069/*
5070 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5071 * Also returns TRUE if there is no directory name.
5072 * "fname" must be writable!.
5073 */
5074 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005075dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005076{
5077 char_u *p;
5078 int c;
5079 int retval;
5080
5081 p = gettail_sep(fname);
5082 if (p == fname)
5083 return TRUE;
5084 c = *p;
5085 *p = NUL;
5086 retval = mch_isdir(fname);
5087 *p = c;
5088 return retval;
5089}
5090
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005092 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5093 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 */
5095 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005096vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005098#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005100#else
5101 if (p_fic)
5102 return MB_STRICMP(x, y);
5103 return STRCMP(x, y);
5104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105}
5106
5107 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005108vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005110#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005111 char_u *px = x;
5112 char_u *py = y;
5113 int cx = NUL;
5114 int cy = NUL;
5115
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005116 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005118 cx = PTR2CHAR(px);
5119 cy = PTR2CHAR(py);
5120 if (cx == NUL || cy == NUL
5121 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5122 && !(cx == '/' && cy == '\\')
5123 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005125 len -= MB_PTR2LEN(px);
5126 px += MB_PTR2LEN(px);
5127 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 if (len == 0)
5130 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005131 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005132#else
5133 if (p_fic)
5134 return MB_STRNICMP(x, y, len);
5135 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139/*
5140 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005141 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 */
5143 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005144concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 char_u *dest;
5147
5148 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5149 if (dest != NULL)
5150 {
5151 STRCPY(dest, fname1);
5152 if (sep)
5153 add_pathsep(dest);
5154 STRCAT(dest, fname2);
5155 }
5156 return dest;
5157}
5158
Bram Moolenaard6754642005-01-17 22:18:45 +00005159/*
5160 * Concatenate two strings and return the result in allocated memory.
5161 * Returns NULL when out of memory.
5162 */
5163 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005164concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005165{
5166 char_u *dest;
5167 size_t l = STRLEN(str1);
5168
5169 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5170 if (dest != NULL)
5171 {
5172 STRCPY(dest, str1);
5173 STRCPY(dest + l, str2);
5174 }
5175 return dest;
5176}
Bram Moolenaard6754642005-01-17 22:18:45 +00005177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178/*
5179 * Add a path separator to a file name, unless it already ends in a path
5180 * separator.
5181 */
5182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005183add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005185 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 STRCAT(p, PATHSEPSTR);
5187}
5188
5189/*
5190 * FullName_save - Make an allocated copy of a full file name.
5191 * Returns NULL when out of memory.
5192 */
5193 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005194FullName_save(
5195 char_u *fname,
5196 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005197 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198{
5199 char_u *buf;
5200 char_u *new_fname = NULL;
5201
5202 if (fname == NULL)
5203 return NULL;
5204
5205 buf = alloc((unsigned)MAXPATHL);
5206 if (buf != NULL)
5207 {
5208 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5209 new_fname = vim_strsave(buf);
5210 else
5211 new_fname = vim_strsave(fname);
5212 vim_free(buf);
5213 }
5214 return new_fname;
5215}
5216
5217#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5218
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005219static char_u *skip_string(char_u *p);
5220static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005221static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005222static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
5224/*
5225 * Find the start of a comment, not knowing if we are in a comment right now.
5226 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005227 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005229 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005230ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005231{
5232 return find_start_comment(curbuf->b_ind_maxcomment);
5233}
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005236find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 pos_T *pos;
5239 char_u *line;
5240 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005241 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005243 for (;;)
5244 {
5245 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5246 if (pos == NULL)
5247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005249 /*
5250 * Check if the comment start we found is inside a string.
5251 * If it is then restrict the search to below this line and try again.
5252 */
5253 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005254 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005255 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005256 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005257 break;
5258 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5259 if (cur_maxcomment <= 0)
5260 {
5261 pos = NULL;
5262 break;
5263 }
5264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 return pos;
5266}
5267
5268/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005269 * Find the start of a comment or raw string, not knowing if we are in a
5270 * comment or raw string right now.
5271 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005272 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005273 * Return NULL when not inside a comment or raw string.
5274 * "CORS" -> Comment Or Raw String
5275 */
5276 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005277ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005278{
Bram Moolenaar089af182015-10-07 11:41:49 +02005279 static pos_T comment_pos_copy;
5280 pos_T *comment_pos;
5281 pos_T *rs_pos;
5282
5283 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5284 if (comment_pos != NULL)
5285 {
5286 /* Need to make a copy of the static pos in findmatchlimit(),
5287 * calling find_start_rawstring() may change it. */
5288 comment_pos_copy = *comment_pos;
5289 comment_pos = &comment_pos_copy;
5290 }
5291 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005292
5293 /* If comment_pos is before rs_pos the raw string is inside the comment.
5294 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005295 if (comment_pos == NULL || (rs_pos != NULL
5296 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005297 {
5298 if (is_raw != NULL && rs_pos != NULL)
5299 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005300 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005301 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005302 return comment_pos;
5303}
5304
5305/*
5306 * Find the start of a raw string, not knowing if we are in one right now.
5307 * Search starts at w_cursor.lnum and goes backwards.
5308 * Return NULL when not inside a raw string.
5309 */
5310 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005311find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005312{
5313 pos_T *pos;
5314 char_u *line;
5315 char_u *p;
5316 int cur_maxcomment = ind_maxcomment;
5317
5318 for (;;)
5319 {
5320 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5321 if (pos == NULL)
5322 break;
5323
5324 /*
5325 * Check if the raw string start we found is inside a string.
5326 * If it is then restrict the search to below this line and try again.
5327 */
5328 line = ml_get(pos->lnum);
5329 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5330 p = skip_string(p);
5331 if ((colnr_T)(p - line) <= pos->col)
5332 break;
5333 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5334 if (cur_maxcomment <= 0)
5335 {
5336 pos = NULL;
5337 break;
5338 }
5339 }
5340 return pos;
5341}
5342
5343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 * Skip to the end of a "string" and a 'c' character.
5345 * If there is no string or character, return argument unmodified.
5346 */
5347 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005348skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 int i;
5351
5352 /*
5353 * We loop, because strings may be concatenated: "date""time".
5354 */
5355 for ( ; ; ++p)
5356 {
5357 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5358 {
5359 if (!p[1]) /* ' at end of line */
5360 break;
5361 i = 2;
5362 if (p[1] == '\\') /* '\n' or '\000' */
5363 {
5364 ++i;
5365 while (vim_isdigit(p[i - 1])) /* '\000' */
5366 ++i;
5367 }
5368 if (p[i] == '\'') /* check for trailing ' */
5369 {
5370 p += i;
5371 continue;
5372 }
5373 }
5374 else if (p[0] == '"') /* start of string */
5375 {
5376 for (++p; p[0]; ++p)
5377 {
5378 if (p[0] == '\\' && p[1] != NUL)
5379 ++p;
5380 else if (p[0] == '"') /* end of string */
5381 break;
5382 }
5383 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005384 continue; /* continue for another string */
5385 }
5386 else if (p[0] == 'R' && p[1] == '"')
5387 {
5388 /* Raw string: R"[delim](...)[delim]" */
5389 char_u *delim = p + 2;
5390 char_u *paren = vim_strchr(delim, '(');
5391
5392 if (paren != NULL)
5393 {
5394 size_t delim_len = paren - delim;
5395
5396 for (p += 3; *p; ++p)
5397 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5398 && p[delim_len + 1] == '"')
5399 {
5400 p += delim_len + 1;
5401 break;
5402 }
5403 if (p[0] == '"')
5404 continue; /* continue for another string */
5405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407 break; /* no string found */
5408 }
5409 if (!*p)
5410 --p; /* backup from NUL */
5411 return p;
5412}
5413#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5414
5415#if defined(FEAT_CINDENT) || defined(PROTO)
5416
5417/*
5418 * Do C or expression indenting on the current line.
5419 */
5420 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423# ifdef FEAT_EVAL
5424 if (*curbuf->b_p_inde != NUL)
5425 fixthisline(get_expr_indent);
5426 else
5427# endif
5428 fixthisline(get_c_indent);
5429}
5430
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005431/* Find result cache for cpp_baseclass */
5432typedef struct {
5433 int found;
5434 lpos_T lpos;
5435} cpp_baseclass_cache_T;
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437/*
5438 * Functions for C-indenting.
5439 * Most of this originally comes from Eric Fischer.
5440 */
5441/*
5442 * Below "XXX" means that this function may unlock the current line.
5443 */
5444
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005445static char_u *cin_skipcomment(char_u *);
5446static int cin_nocode(char_u *);
5447static pos_T *find_line_comment(void);
5448static int cin_has_js_key(char_u *text);
5449static int cin_islabel_skip(char_u **);
5450static int cin_isdefault(char_u *);
5451static char_u *after_label(char_u *l);
5452static int get_indent_nolabel(linenr_T lnum);
5453static int skip_label(linenr_T, char_u **pp);
5454static int cin_first_id_amount(void);
5455static int cin_get_equal_amount(linenr_T lnum);
5456static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005457static int cin_iscomment(char_u *);
5458static int cin_islinecomment(char_u *);
5459static int cin_isterminated(char_u *, int, int);
5460static int cin_isinit(void);
5461static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5462static int cin_isif(char_u *);
5463static int cin_iselse(char_u *);
5464static int cin_isdo(char_u *);
5465static int cin_iswhileofdo(char_u *, linenr_T);
5466static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5467static int cin_iswhileofdo_end(int terminated);
5468static int cin_isbreak(char_u *);
5469static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5470static int get_baseclass_amount(int col);
5471static int cin_ends_in(char_u *, char_u *, char_u *);
5472static int cin_starts_with(char_u *s, char *word);
5473static int cin_skip2pos(pos_T *trypos);
5474static pos_T *find_start_brace(void);
5475static pos_T *find_match_paren(int);
5476static pos_T *find_match_char(int c, int ind_maxparen);
5477static int corr_ind_maxparen(pos_T *startpos);
5478static int find_last_paren(char_u *l, int start, int end);
5479static int find_match(int lookfor, linenr_T ourscope);
5480static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482/*
5483 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005484 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 */
5486 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005487cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 while (*s)
5490 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005491 char_u *prev_s = s;
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005494
5495 /* Perl/shell # comment comment continues until eol. Require a space
5496 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005497 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005498 {
5499 s += STRLEN(s);
5500 break;
5501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (*s != '/')
5503 break;
5504 ++s;
5505 if (*s == '/') /* slash-slash comment continues till eol */
5506 {
5507 s += STRLEN(s);
5508 break;
5509 }
5510 if (*s != '*')
5511 break;
5512 for (++s; *s; ++s) /* skip slash-star comment */
5513 if (s[0] == '*' && s[1] == '/')
5514 {
5515 s += 2;
5516 break;
5517 }
5518 }
5519 return s;
5520}
5521
5522/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005523 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 * not considered code.
5525 */
5526 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 return *cin_skipcomment(s) == NUL;
5530}
5531
5532/*
5533 * Check previous lines for a "//" line comment, skipping over blank lines.
5534 */
5535 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005536find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 static pos_T pos;
5539 char_u *line;
5540 char_u *p;
5541
5542 pos = curwin->w_cursor;
5543 while (--pos.lnum > 0)
5544 {
5545 line = ml_get(pos.lnum);
5546 p = skipwhite(line);
5547 if (cin_islinecomment(p))
5548 {
5549 pos.col = (int)(p - line);
5550 return &pos;
5551 }
5552 if (*p != NUL)
5553 break;
5554 }
5555 return NULL;
5556}
5557
5558/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005559 * Return TRUE if "text" starts with "key:".
5560 */
5561 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005562cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005563{
5564 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005565 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005566
5567 if (*s == '\'' || *s == '"')
5568 {
5569 /* can be 'key': or "key": */
5570 quote = *s;
5571 ++s;
5572 }
5573 if (!vim_isIDc(*s)) /* need at least one ID character */
5574 return FALSE;
5575
5576 while (vim_isIDc(*s))
5577 ++s;
5578 if (*s == quote)
5579 ++s;
5580
5581 s = cin_skipcomment(s);
5582
5583 /* "::" is not a label, it's C++ */
5584 return (*s == ':' && s[1] != ':');
5585}
5586
5587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005589 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 */
5591 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005592cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 if (!vim_isIDc(**s)) /* need at least one ID character */
5595 return FALSE;
5596
5597 while (vim_isIDc(**s))
5598 (*s)++;
5599
5600 *s = cin_skipcomment(*s);
5601
5602 /* "::" is not a label, it's C++ */
5603 return (**s == ':' && *++*s != ':');
5604}
5605
5606/*
5607 * Recognize a label: "label:".
5608 * Note: curwin->w_cursor must be where we are looking for the label.
5609 */
5610 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005611cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 char_u *s;
5614
5615 s = cin_skipcomment(ml_get_curline());
5616
5617 /*
5618 * Exclude "default" from labels, since it should be indented
5619 * like a switch label. Same for C++ scope declarations.
5620 */
5621 if (cin_isdefault(s))
5622 return FALSE;
5623 if (cin_isscopedecl(s))
5624 return FALSE;
5625
5626 if (cin_islabel_skip(&s))
5627 {
5628 /*
5629 * Only accept a label if the previous line is terminated or is a case
5630 * label.
5631 */
5632 pos_T cursor_save;
5633 pos_T *trypos;
5634 char_u *line;
5635
5636 cursor_save = curwin->w_cursor;
5637 while (curwin->w_cursor.lnum > 1)
5638 {
5639 --curwin->w_cursor.lnum;
5640
5641 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005642 * If we're in a comment or raw string now, skip to the start of
5643 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 */
5645 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005646 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 curwin->w_cursor = *trypos;
5648
5649 line = ml_get_curline();
5650 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5651 continue;
5652 if (*(line = cin_skipcomment(line)) == NUL)
5653 continue;
5654
5655 curwin->w_cursor = cursor_save;
5656 if (cin_isterminated(line, TRUE, FALSE)
5657 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005658 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 || (cin_islabel_skip(&line) && cin_nocode(line)))
5660 return TRUE;
5661 return FALSE;
5662 }
5663 curwin->w_cursor = cursor_save;
5664 return TRUE; /* label at start of file??? */
5665 }
5666 return FALSE;
5667}
5668
5669/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005670 * Recognize structure initialization and enumerations:
5671 * "[typedef] [static|public|protected|private] enum"
5672 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 */
5674 static int
5675cin_isinit(void)
5676{
5677 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005678 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
5680 s = cin_skipcomment(ml_get_curline());
5681
Bram Moolenaar75342212013-03-07 13:13:52 +01005682 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 s = cin_skipcomment(s + 7);
5684
Bram Moolenaar75342212013-03-07 13:13:52 +01005685 for (;;)
5686 {
5687 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005688
Bram Moolenaar75342212013-03-07 13:13:52 +01005689 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5690 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005691 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005692 if (cin_starts_with(s, skip[i]))
5693 {
5694 s = cin_skipcomment(s + l);
5695 l = 0;
5696 break;
5697 }
5698 }
5699 if (l != 0)
5700 break;
5701 }
5702
5703 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 return TRUE;
5705
5706 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5707 return TRUE;
5708
5709 return FALSE;
5710}
5711
5712/*
5713 * Recognize a switch label: "case .*:" or "default:".
5714 */
5715 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005716cin_iscase(
5717 char_u *s,
5718 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
5720 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005721 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 for (s += 4; *s; ++s)
5724 {
5725 s = cin_skipcomment(s);
5726 if (*s == ':')
5727 {
5728 if (s[1] == ':') /* skip over "::" for C++ */
5729 ++s;
5730 else
5731 return TRUE;
5732 }
5733 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005734 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5736 return FALSE; /* stop at comment */
5737 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005738 {
5739 /* JS etc. */
5740 if (strict)
5741 return FALSE; /* stop at string */
5742 else
5743 return TRUE;
5744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746 return FALSE;
5747 }
5748
5749 if (cin_isdefault(s))
5750 return TRUE;
5751 return FALSE;
5752}
5753
5754/*
5755 * Recognize a "default" switch label.
5756 */
5757 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005758cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759{
5760 return (STRNCMP(s, "default", 7) == 0
5761 && *(s = cin_skipcomment(s + 7)) == ':'
5762 && s[1] != ':');
5763}
5764
5765/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005766 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 */
5768 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005769cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
5771 int i;
5772
5773 s = cin_skipcomment(s);
5774 if (STRNCMP(s, "public", 6) == 0)
5775 i = 6;
5776 else if (STRNCMP(s, "protected", 9) == 0)
5777 i = 9;
5778 else if (STRNCMP(s, "private", 7) == 0)
5779 i = 7;
5780 else
5781 return FALSE;
5782 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5783}
5784
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005785/* Maximum number of lines to search back for a "namespace" line. */
5786#define FIND_NAMESPACE_LIM 20
5787
5788/*
5789 * Recognize a "namespace" scope declaration.
5790 */
5791 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005792cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005793{
5794 char_u *p;
5795 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005796 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005797
5798 s = cin_skipcomment(s);
5799 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5800 {
5801 p = cin_skipcomment(skipwhite(s + 9));
5802 while (*p != NUL)
5803 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005804 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005805 {
5806 has_name = TRUE; /* found end of a name */
5807 p = cin_skipcomment(skipwhite(p));
5808 }
5809 else if (*p == '{')
5810 {
5811 break;
5812 }
5813 else if (vim_iswordc(*p))
5814 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005815 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005816 if (has_name)
5817 return FALSE; /* word character after skipping past name */
5818 ++p;
5819 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005820 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5821 {
5822 if (!has_name_start || has_name)
5823 return FALSE;
5824 /* C++ 17 nested namespace */
5825 p += 3;
5826 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005827 else
5828 {
5829 return FALSE;
5830 }
5831 }
5832 return TRUE;
5833 }
5834 return FALSE;
5835}
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005838 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5839 */
5840 static int
5841cin_is_cpp_extern_c(char_u *s)
5842{
5843 char_u *p;
5844 int has_string_literal = FALSE;
5845
5846 s = cin_skipcomment(s);
5847 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5848 {
5849 p = cin_skipcomment(skipwhite(s + 6));
5850 while (*p != NUL)
5851 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005852 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005853 {
5854 p = cin_skipcomment(skipwhite(p));
5855 }
5856 else if (*p == '{')
5857 {
5858 break;
5859 }
5860 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5861 {
5862 if (has_string_literal)
5863 return FALSE;
5864 has_string_literal = TRUE;
5865 p += 3;
5866 }
5867 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5868 && p[4] == '"')
5869 {
5870 if (has_string_literal)
5871 return FALSE;
5872 has_string_literal = TRUE;
5873 p += 5;
5874 }
5875 else
5876 {
5877 return FALSE;
5878 }
5879 }
5880 return has_string_literal ? TRUE : FALSE;
5881 }
5882 return FALSE;
5883}
5884
5885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 * Return a pointer to the first non-empty non-comment character after a ':'.
5887 * Return NULL if not found.
5888 * case 234: a = b;
5889 * ^
5890 */
5891 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005892after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 for ( ; *l; ++l)
5895 {
5896 if (*l == ':')
5897 {
5898 if (l[1] == ':') /* skip over "::" for C++ */
5899 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005900 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 break;
5902 }
5903 else if (*l == '\'' && l[1] && l[2] == '\'')
5904 l += 2; /* skip over 'x' */
5905 }
5906 if (*l == NUL)
5907 return NULL;
5908 l = cin_skipcomment(l + 1);
5909 if (*l == NUL)
5910 return NULL;
5911 return l;
5912}
5913
5914/*
5915 * Get indent of line "lnum", skipping a label.
5916 * Return 0 if there is nothing after the label.
5917 */
5918 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005919get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 char_u *l;
5922 pos_T fp;
5923 colnr_T col;
5924 char_u *p;
5925
5926 l = ml_get(lnum);
5927 p = after_label(l);
5928 if (p == NULL)
5929 return 0;
5930
5931 fp.col = (colnr_T)(p - l);
5932 fp.lnum = lnum;
5933 getvcol(curwin, &fp, &col, NULL, NULL);
5934 return (int)col;
5935}
5936
5937/*
5938 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005939 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 * label: if (asdf && asdfasdf)
5941 * ^
5942 */
5943 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005944skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
5946 char_u *l;
5947 int amount;
5948 pos_T cursor_save;
5949
5950 cursor_save = curwin->w_cursor;
5951 curwin->w_cursor.lnum = lnum;
5952 l = ml_get_curline();
5953 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005954 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {
5956 amount = get_indent_nolabel(lnum);
5957 l = after_label(ml_get_curline());
5958 if (l == NULL) /* just in case */
5959 l = ml_get_curline();
5960 }
5961 else
5962 {
5963 amount = get_indent();
5964 l = ml_get_curline();
5965 }
5966 *pp = l;
5967
5968 curwin->w_cursor = cursor_save;
5969 return amount;
5970}
5971
5972/*
5973 * Return the indent of the first variable name after a type in a declaration.
5974 * int a, indent of "a"
5975 * static struct foo b, indent of "b"
5976 * enum bla c, indent of "c"
5977 * Returns zero when it doesn't look like a declaration.
5978 */
5979 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005980cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
5982 char_u *line, *p, *s;
5983 int len;
5984 pos_T fp;
5985 colnr_T col;
5986
5987 line = ml_get_curline();
5988 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005989 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5991 {
5992 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005993 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 }
5995 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5996 p = skipwhite(p + 6);
5997 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5998 p = skipwhite(p + 4);
5999 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6000 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6001 {
6002 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006003 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6004 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6005 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6006 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 p = s;
6008 }
6009 for (len = 0; vim_isIDc(p[len]); ++len)
6010 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006011 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 return 0;
6013
6014 p = skipwhite(p + len);
6015 fp.lnum = curwin->w_cursor.lnum;
6016 fp.col = (colnr_T)(p - line);
6017 getvcol(curwin, &fp, &col, NULL, NULL);
6018 return (int)col;
6019}
6020
6021/*
6022 * Return the indent of the first non-blank after an equal sign.
6023 * char *foo = "here";
6024 * Return zero if no (useful) equal sign found.
6025 * Return -1 if the line above "lnum" ends in a backslash.
6026 * foo = "asdf\
6027 * asdf\
6028 * here";
6029 */
6030 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006031cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 char_u *line;
6034 char_u *s;
6035 colnr_T col;
6036 pos_T fp;
6037
6038 if (lnum > 1)
6039 {
6040 line = ml_get(lnum - 1);
6041 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6042 return -1;
6043 }
6044
6045 line = s = ml_get(lnum);
6046 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6047 {
6048 if (cin_iscomment(s)) /* ignore comments */
6049 s = cin_skipcomment(s);
6050 else
6051 ++s;
6052 }
6053 if (*s != '=')
6054 return 0;
6055
6056 s = skipwhite(s + 1);
6057 if (cin_nocode(s))
6058 return 0;
6059
6060 if (*s == '"') /* nice alignment for continued strings */
6061 ++s;
6062
6063 fp.lnum = lnum;
6064 fp.col = (colnr_T)(s - line);
6065 getvcol(curwin, &fp, &col, NULL, NULL);
6066 return (int)col;
6067}
6068
6069/*
6070 * Recognize a preprocessor statement: Any line that starts with '#'.
6071 */
6072 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006073cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006075 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 return TRUE;
6077 return FALSE;
6078}
6079
6080/*
6081 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6082 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6083 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006084 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 */
6086 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006087cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 char_u *line = *pp;
6090 linenr_T lnum = *lnump;
6091 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006092 int candidate_amount = *amount;
6093
6094 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6095 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006097 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 if (cin_ispreproc(line))
6100 {
6101 retval = TRUE;
6102 *lnump = lnum;
6103 break;
6104 }
6105 if (lnum == 1)
6106 break;
6107 line = ml_get(--lnum);
6108 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6109 break;
6110 }
6111
6112 if (lnum != *lnump)
6113 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006114 if (retval)
6115 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 return retval;
6117}
6118
6119/*
6120 * Recognize the start of a C or C++ comment.
6121 */
6122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006123cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6126}
6127
6128/*
6129 * Recognize the start of a "//" comment.
6130 */
6131 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006132cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 return (p[0] == '/' && p[1] == '/');
6135}
6136
6137/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006138 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6139 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006141 * If a line begins with an "else", only consider it terminated if no unmatched
6142 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 * Return the character terminating the line (ending char's have precedence if
6144 * both apply in order to determine initializations).
6145 */
6146 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006147cin_isterminated(
6148 char_u *s,
6149 int incl_open, /* include '{' at the end as terminator */
6150 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006152 char_u found_start = 0;
6153 unsigned n_open = 0;
6154 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
6156 s = cin_skipcomment(s);
6157
6158 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6159 found_start = *s;
6160
Bram Moolenaar496f9512011-05-19 16:35:09 +02006161 if (!found_start)
6162 is_else = cin_iselse(s);
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 while (*s)
6165 {
6166 /* skip over comments, "" strings and 'c'haracters */
6167 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006168 if (*s == '}' && n_open > 0)
6169 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006170 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006171 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 && cin_nocode(s + 1))
6173 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006174 else if (*s == '{')
6175 {
6176 if (incl_open && cin_nocode(s + 1))
6177 return *s;
6178 else
6179 ++n_open;
6180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182 if (*s)
6183 s++;
6184 }
6185 return found_start;
6186}
6187
6188/*
6189 * Recognize the basic picture of a function declaration -- it needs to
6190 * have an open paren somewhere and a close paren at the end of the line and
6191 * no semicolons anywhere.
6192 * When a line ends in a comma we continue looking in the next line.
6193 * "sp" points to a string with the line. When looking at other lines it must
6194 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006195 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006196 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 */
6198 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006199cin_isfuncdecl(
6200 char_u **sp,
6201 linenr_T first_lnum,
6202 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 char_u *s;
6205 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006206 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006208 pos_T *trypos;
6209 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211 if (sp == NULL)
6212 s = ml_get(lnum);
6213 else
6214 s = *sp;
6215
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006216 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006217 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006218 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006219 {
6220 lnum = trypos->lnum;
6221 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006222 {
6223 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006224 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006225 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006226
6227 s = ml_get(lnum);
6228 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006229 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006230
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006231 /* Ignore line starting with #. */
6232 if (cin_ispreproc(s))
6233 return FALSE;
6234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6236 {
6237 if (cin_iscomment(s)) /* ignore comments */
6238 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006239 else if (*s == ':')
6240 {
6241 if (*(s + 1) == ':')
6242 s += 2;
6243 else
6244 /* To avoid a mistake in the following situation:
6245 * A::A(int a, int b)
6246 * : a(0) // <--not a function decl
6247 * , b(0)
6248 * {...
6249 */
6250 return FALSE;
6251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 else
6253 ++s;
6254 }
6255 if (*s != '(')
6256 return FALSE; /* ';', ' or " before any () or no '(' */
6257
6258 while (*s && *s != ';' && *s != '\'' && *s != '"')
6259 {
6260 if (*s == ')' && cin_nocode(s + 1))
6261 {
6262 /* ')' at the end: may have found a match
6263 * Check for he previous line not to end in a backslash:
6264 * #if defined(x) && \
6265 * defined(y)
6266 */
6267 lnum = first_lnum - 1;
6268 s = ml_get(lnum);
6269 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6270 retval = TRUE;
6271 goto done;
6272 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006273 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006275 int comma = (*s == ',');
6276
6277 /* ',' at the end: continue looking in the next line.
6278 * At the end: check for ',' in the next line, for this style:
6279 * func(arg1
6280 * , arg2) */
6281 for (;;)
6282 {
6283 if (lnum >= curbuf->b_ml.ml_line_count)
6284 break;
6285 s = ml_get(++lnum);
6286 if (!cin_ispreproc(s))
6287 break;
6288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (lnum >= curbuf->b_ml.ml_line_count)
6290 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006291 /* Require a comma at end of the line or a comma or ')' at the
6292 * start of next line. */
6293 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006294 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006295 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006296 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298 else if (cin_iscomment(s)) /* ignore comments */
6299 s = cin_skipcomment(s);
6300 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006303 just_started = FALSE;
6304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 }
6306
6307done:
6308 if (lnum != first_lnum && sp != NULL)
6309 *sp = ml_get(first_lnum);
6310
6311 return retval;
6312}
6313
6314 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006315cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006317 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318}
6319
6320 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006321cin_iselse(
6322 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 if (*p == '}') /* accept "} else" */
6325 p = cin_skipcomment(p + 1);
6326 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6327}
6328
6329 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006330cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6333}
6334
6335/*
6336 * Check if this is a "while" that should have a matching "do".
6337 * We only accept a "while (condition) ;", with only white space between the
6338 * ')' and ';'. The condition may be spread over several lines.
6339 */
6340 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006341cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 pos_T cursor_save;
6344 pos_T *trypos;
6345 int retval = FALSE;
6346
6347 p = cin_skipcomment(p);
6348 if (*p == '}') /* accept "} while (cond);" */
6349 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006350 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
6352 cursor_save = curwin->w_cursor;
6353 curwin->w_cursor.lnum = lnum;
6354 curwin->w_cursor.col = 0;
6355 p = ml_get_curline();
6356 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6357 {
6358 ++p;
6359 ++curwin->w_cursor.col;
6360 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006361 if ((trypos = findmatchlimit(NULL, 0, 0,
6362 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6364 retval = TRUE;
6365 curwin->w_cursor = cursor_save;
6366 }
6367 return retval;
6368}
6369
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006370/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006371 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006372 * Return 0 if there is none.
6373 * Otherwise return !0 and update "*poffset" to point to the place where the
6374 * string was found.
6375 */
6376 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006377cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006378{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006379 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006380
6381 if (offset-- < 2)
6382 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006383 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006384 --offset;
6385
6386 offset -= 1;
6387 if (!STRNCMP(line + offset, "if", 2))
6388 goto probablyFound;
6389
6390 if (offset >= 1)
6391 {
6392 offset -= 1;
6393 if (!STRNCMP(line + offset, "for", 3))
6394 goto probablyFound;
6395
6396 if (offset >= 2)
6397 {
6398 offset -= 2;
6399 if (!STRNCMP(line + offset, "while", 5))
6400 goto probablyFound;
6401 }
6402 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006403 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006404
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006405probablyFound:
6406 if (!offset || !vim_isIDc(line[offset - 1]))
6407 {
6408 *poffset = offset;
6409 return 1;
6410 }
6411 return 0;
6412}
6413
6414/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006415 * Return TRUE if we are at the end of a do-while.
6416 * do
6417 * nothing;
6418 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006419 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006420 * Adjust the cursor to the line with "while".
6421 */
6422 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006424{
6425 char_u *line;
6426 char_u *p;
6427 char_u *s;
6428 pos_T *trypos;
6429 int i;
6430
6431 if (terminated != ';') /* there must be a ';' at the end */
6432 return FALSE;
6433
6434 p = line = ml_get_curline();
6435 while (*p != NUL)
6436 {
6437 p = cin_skipcomment(p);
6438 if (*p == ')')
6439 {
6440 s = skipwhite(p + 1);
6441 if (*s == ';' && cin_nocode(s + 1))
6442 {
6443 /* Found ");" at end of the line, now check there is "while"
6444 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006445 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006446 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006447 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006448 if (trypos != NULL)
6449 {
6450 s = cin_skipcomment(ml_get(trypos->lnum));
6451 if (*s == '}') /* accept "} while (cond);" */
6452 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006453 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006454 {
6455 curwin->w_cursor.lnum = trypos->lnum;
6456 return TRUE;
6457 }
6458 }
6459
6460 /* Searching may have made "line" invalid, get it again. */
6461 line = ml_get_curline();
6462 p = line + i;
6463 }
6464 }
6465 if (*p != NUL)
6466 ++p;
6467 }
6468 return FALSE;
6469}
6470
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6475}
6476
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006477/*
6478 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 * constructor-initialization. eg:
6480 *
6481 * class MyClass :
6482 * baseClass <-- here
6483 * class MyClass : public baseClass,
6484 * anotherBaseClass <-- here (should probably lineup ??)
6485 * MyClass::MyClass(...) :
6486 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006487 *
6488 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 */
6490 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006491cin_is_cpp_baseclass(
6492 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006494 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 char_u *s;
6496 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006497 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006498 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006500 if (pos->lnum <= lnum)
6501 return cached->found; /* Use the cached result */
6502
6503 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006505 s = skipwhite(line);
6506 if (*s == '#') /* skip #define FOO x ? (x) : x */
6507 return FALSE;
6508 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (*s == NUL)
6510 return FALSE;
6511
6512 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6513
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006514 /* Search for a line starting with '#', empty, ending in ';' or containing
6515 * '{' or '}' and start below it. This handles the following situations:
6516 * a = cond ?
6517 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006518 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006519 * func::foo()
6520 * : something
6521 * {}
6522 * Foo::Foo (int one, int two)
6523 * : something(4),
6524 * somethingelse(3)
6525 * {}
6526 */
6527 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006529 line = ml_get(lnum - 1);
6530 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006531 if (*s == '#' || *s == NUL)
6532 break;
6533 while (*s != NUL)
6534 {
6535 s = cin_skipcomment(s);
6536 if (*s == '{' || *s == '}'
6537 || (*s == ';' && cin_nocode(s + 1)))
6538 break;
6539 if (*s != NUL)
6540 ++s;
6541 }
6542 if (*s != NUL)
6543 break;
6544 --lnum;
6545 }
6546
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006547 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006548 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006549 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006550 for (;;)
6551 {
6552 if (*s == NUL)
6553 {
6554 if (lnum == curwin->w_cursor.lnum)
6555 break;
6556 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006557 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006558 s = line;
6559 }
6560 if (s == line)
6561 {
6562 /* don't recognize "case (foo):" as a baseclass */
6563 if (cin_iscase(s, FALSE))
6564 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006565 s = cin_skipcomment(line);
6566 if (*s == NUL)
6567 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006568 }
6569
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006570 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006571 s = skip_string(s) + 1;
6572 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
6574 if (s[1] == ':')
6575 {
6576 /* skip double colon. It can't be a constructor
6577 * initialization any more */
6578 lookfor_ctor_init = FALSE;
6579 s = cin_skipcomment(s + 2);
6580 }
6581 else if (lookfor_ctor_init || class_or_struct)
6582 {
6583 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006584 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 cpp_base_class = TRUE;
6586 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006587 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 s = cin_skipcomment(s + 1);
6589 }
6590 else
6591 s = cin_skipcomment(s + 1);
6592 }
6593 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6594 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6595 {
6596 class_or_struct = TRUE;
6597 lookfor_ctor_init = FALSE;
6598
6599 if (*s == 'c')
6600 s = cin_skipcomment(s + 5);
6601 else
6602 s = cin_skipcomment(s + 6);
6603 }
6604 else
6605 {
6606 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6607 {
6608 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6609 }
6610 else if (s[0] == ')')
6611 {
6612 /* Constructor-initialization is assumed if we come across
6613 * something like "):" */
6614 class_or_struct = FALSE;
6615 lookfor_ctor_init = TRUE;
6616 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006617 else if (s[0] == '?')
6618 {
6619 /* Avoid seeing '() :' after '?' as constructor init. */
6620 return FALSE;
6621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 else if (!vim_isIDc(s[0]))
6623 {
6624 /* if it is not an identifier, we are wrong */
6625 class_or_struct = FALSE;
6626 lookfor_ctor_init = FALSE;
6627 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006628 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 {
6630 /* it can't be a constructor-initialization any more */
6631 lookfor_ctor_init = FALSE;
6632
6633 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006634 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006635 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 }
6637
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006638 /* When the line ends in a comma don't align with it. */
6639 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006640 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 s = cin_skipcomment(s + 1);
6643 }
6644 }
6645
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006646 cached->found = cpp_base_class;
6647 if (cpp_base_class)
6648 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 return cpp_base_class;
6650}
6651
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006652 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006653get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006654{
6655 int amount;
6656 colnr_T vcol;
6657 pos_T *trypos;
6658
6659 if (col == 0)
6660 {
6661 amount = get_indent();
6662 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006663 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006664 amount = get_indent_lnum(trypos->lnum); /* XXX */
6665 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006666 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006667 }
6668 else
6669 {
6670 curwin->w_cursor.col = col;
6671 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6672 amount = (int)vcol;
6673 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006674 if (amount < curbuf->b_ind_cpp_baseclass)
6675 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006676 return amount;
6677}
6678
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679/*
6680 * Return TRUE if string "s" ends with the string "find", possibly followed by
6681 * white space and comments. Skip strings and comments.
6682 * Ignore "ignore" after "find" if it's not NULL.
6683 */
6684 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006685cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 char_u *p = s;
6688 char_u *r;
6689 int len = (int)STRLEN(find);
6690
6691 while (*p != NUL)
6692 {
6693 p = cin_skipcomment(p);
6694 if (STRNCMP(p, find, len) == 0)
6695 {
6696 r = skipwhite(p + len);
6697 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6698 r = skipwhite(r + STRLEN(ignore));
6699 if (cin_nocode(r))
6700 return TRUE;
6701 }
6702 if (*p != NUL)
6703 ++p;
6704 }
6705 return FALSE;
6706}
6707
6708/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006709 * Return TRUE when "s" starts with "word" and then a non-ID character.
6710 */
6711 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006712cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006713{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006714 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006715
6716 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6717}
6718
6719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 * Skip strings, chars and comments until at or past "trypos".
6721 * Return the column found.
6722 */
6723 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006724cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
6726 char_u *line;
6727 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006728 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 p = line = ml_get(trypos->lnum);
6731 while (*p && (colnr_T)(p - line) < trypos->col)
6732 {
6733 if (cin_iscomment(p))
6734 p = cin_skipcomment(p);
6735 else
6736 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006737 new_p = skip_string(p);
6738 if (new_p == p)
6739 ++p;
6740 else
6741 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 }
6744 return (int)(p - line);
6745}
6746
6747/*
6748 * Find the '{' at the start of the block we are in.
6749 * Return NULL if no match found.
6750 * Ignore a '{' that is in a comment, makes indenting the next three lines
6751 * work. */
6752/* foo() */
6753/* { */
6754/* } */
6755
6756 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006757find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
6759 pos_T cursor_save;
6760 pos_T *trypos;
6761 pos_T *pos;
6762 static pos_T pos_copy;
6763
6764 cursor_save = curwin->w_cursor;
6765 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6766 {
6767 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6768 trypos = &pos_copy;
6769 curwin->w_cursor = *trypos;
6770 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006771 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006773 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 break;
6775 if (pos != NULL)
6776 curwin->w_cursor.lnum = pos->lnum;
6777 }
6778 curwin->w_cursor = cursor_save;
6779 return trypos;
6780}
6781
6782/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006783 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006784 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 */
6786 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006787find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006789 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006790}
6791
6792 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006794{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 pos_T cursor_save;
6796 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006797 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006798 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
6800 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006801 ind_maxp_wk = ind_maxparen;
6802retry:
6803 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
6805 /* check if the ( is in a // comment */
6806 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006807 {
6808 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6809 if (ind_maxp_wk > 0)
6810 {
6811 curwin->w_cursor = *trypos;
6812 curwin->w_cursor.col = 0; /* XXX */
6813 goto retry;
6814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 else
6818 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006819 pos_T *trypos_wk;
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6822 trypos = &pos_copy;
6823 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006824 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006825 {
6826 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6827 - trypos_wk->lnum);
6828 if (ind_maxp_wk > 0)
6829 {
6830 curwin->w_cursor = *trypos_wk;
6831 goto retry;
6832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
6836 }
6837 curwin->w_cursor = cursor_save;
6838 return trypos;
6839}
6840
6841/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006842 * Find the matching '(', ignoring it if it is in a comment or before an
6843 * unmatched {.
6844 * Return NULL if no match found.
6845 */
6846 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006847find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006848{
6849 pos_T *trypos = find_match_paren(ind_maxparen);
6850
6851 if (trypos != NULL)
6852 {
6853 pos_T *tryposBrace = find_start_brace();
6854
6855 /* If both an unmatched '(' and '{' is found. Ignore the '('
6856 * position if the '{' is further down. */
6857 if (tryposBrace != NULL
6858 && (trypos->lnum != tryposBrace->lnum
6859 ? trypos->lnum < tryposBrace->lnum
6860 : trypos->col < tryposBrace->col))
6861 trypos = NULL;
6862 }
6863 return trypos;
6864}
6865
6866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 * Return ind_maxparen corrected for the difference in line number between the
6868 * cursor position and "startpos". This makes sure that searching for a
6869 * matching paren above the cursor line doesn't find a match because of
6870 * looking a few lines further.
6871 */
6872 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006873corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
6875 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6876
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006877 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6878 return curbuf->b_ind_maxparen - (int)n;
6879 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880}
6881
6882/*
6883 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006884 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 */
6886 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006887find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888{
6889 int i;
6890 int retval = FALSE;
6891 int open_count = 0;
6892
6893 curwin->w_cursor.col = 0; /* default is start of line */
6894
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006895 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 {
6897 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6898 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6899 if (l[i] == start)
6900 ++open_count;
6901 else if (l[i] == end)
6902 {
6903 if (open_count > 0)
6904 --open_count;
6905 else
6906 {
6907 curwin->w_cursor.col = i;
6908 retval = TRUE;
6909 }
6910 }
6911 }
6912 return retval;
6913}
6914
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006915/*
6916 * Parse 'cinoptions' and set the values in "curbuf".
6917 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6918 */
6919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006920parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006921{
6922 char_u *p;
6923 char_u *l;
6924 char_u *digits;
6925 int n;
6926 int divider;
6927 int fraction = 0;
6928 int sw = (int)get_sw_value(buf);
6929
6930 /*
6931 * Set the default values.
6932 */
6933 /* Spaces from a block's opening brace the prevailing indent for that
6934 * block should be. */
6935 buf->b_ind_level = sw;
6936
6937 /* Spaces from the edge of the line an open brace that's at the end of a
6938 * line is imagined to be. */
6939 buf->b_ind_open_imag = 0;
6940
6941 /* Spaces from the prevailing indent for a line that is not preceded by
6942 * an opening brace. */
6943 buf->b_ind_no_brace = 0;
6944
6945 /* Column where the first { of a function should be located }. */
6946 buf->b_ind_first_open = 0;
6947
6948 /* Spaces from the prevailing indent a leftmost open brace should be
6949 * located. */
6950 buf->b_ind_open_extra = 0;
6951
6952 /* Spaces from the matching open brace (real location for one at the left
6953 * edge; imaginary location from one that ends a line) the matching close
6954 * brace should be located. */
6955 buf->b_ind_close_extra = 0;
6956
6957 /* Spaces from the edge of the line an open brace sitting in the leftmost
6958 * column is imagined to be. */
6959 buf->b_ind_open_left_imag = 0;
6960
6961 /* Spaces jump labels should be shifted to the left if N is non-negative,
6962 * otherwise the jump label will be put to column 1. */
6963 buf->b_ind_jump_label = -1;
6964
6965 /* Spaces from the switch() indent a "case xx" label should be located. */
6966 buf->b_ind_case = sw;
6967
6968 /* Spaces from the "case xx:" code after a switch() should be located. */
6969 buf->b_ind_case_code = sw;
6970
6971 /* Lineup break at end of case in switch() with case label. */
6972 buf->b_ind_case_break = 0;
6973
6974 /* Spaces from the class declaration indent a scope declaration label
6975 * should be located. */
6976 buf->b_ind_scopedecl = sw;
6977
6978 /* Spaces from the scope declaration label code should be located. */
6979 buf->b_ind_scopedecl_code = sw;
6980
6981 /* Amount K&R-style parameters should be indented. */
6982 buf->b_ind_param = sw;
6983
6984 /* Amount a function type spec should be indented. */
6985 buf->b_ind_func_type = sw;
6986
6987 /* Amount a cpp base class declaration or constructor initialization
6988 * should be indented. */
6989 buf->b_ind_cpp_baseclass = sw;
6990
6991 /* additional spaces beyond the prevailing indent a continuation line
6992 * should be located. */
6993 buf->b_ind_continuation = sw;
6994
6995 /* Spaces from the indent of the line with an unclosed parentheses. */
6996 buf->b_ind_unclosed = sw * 2;
6997
6998 /* Spaces from the indent of the line with an unclosed parentheses, which
6999 * itself is also unclosed. */
7000 buf->b_ind_unclosed2 = sw;
7001
7002 /* Suppress ignoring spaces from the indent of a line starting with an
7003 * unclosed parentheses. */
7004 buf->b_ind_unclosed_noignore = 0;
7005
7006 /* If the opening paren is the last nonwhite character on the line, and
7007 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7008 * context (for very long lines). */
7009 buf->b_ind_unclosed_wrapped = 0;
7010
7011 /* Suppress ignoring white space when lining up with the character after
7012 * an unclosed parentheses. */
7013 buf->b_ind_unclosed_whiteok = 0;
7014
7015 /* Indent a closing parentheses under the line start of the matching
7016 * opening parentheses. */
7017 buf->b_ind_matching_paren = 0;
7018
7019 /* Indent a closing parentheses under the previous line. */
7020 buf->b_ind_paren_prev = 0;
7021
7022 /* Extra indent for comments. */
7023 buf->b_ind_comment = 0;
7024
7025 /* Spaces from the comment opener when there is nothing after it. */
7026 buf->b_ind_in_comment = 3;
7027
7028 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7029 * after the comment opener. */
7030 buf->b_ind_in_comment2 = 0;
7031
7032 /* Max lines to search for an open paren. */
7033 buf->b_ind_maxparen = 20;
7034
7035 /* Max lines to search for an open comment. */
7036 buf->b_ind_maxcomment = 70;
7037
7038 /* Handle braces for java code. */
7039 buf->b_ind_java = 0;
7040
7041 /* Not to confuse JS object properties with labels. */
7042 buf->b_ind_js = 0;
7043
7044 /* Handle blocked cases correctly. */
7045 buf->b_ind_keep_case_label = 0;
7046
7047 /* Handle C++ namespace. */
7048 buf->b_ind_cpp_namespace = 0;
7049
7050 /* Handle continuation lines containing conditions of if(), for() and
7051 * while(). */
7052 buf->b_ind_if_for_while = 0;
7053
Bram Moolenaar6b643942017-03-05 19:44:06 +01007054 /* indentation for # comments */
7055 buf->b_ind_hash_comment = 0;
7056
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007057 /* Handle C++ extern "C" or "C++" */
7058 buf->b_ind_cpp_extern_c = 0;
7059
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007060 for (p = buf->b_p_cino; *p; )
7061 {
7062 l = p++;
7063 if (*p == '-')
7064 ++p;
7065 digits = p; /* remember where the digits start */
7066 n = getdigits(&p);
7067 divider = 0;
7068 if (*p == '.') /* ".5s" means a fraction */
7069 {
7070 fraction = atol((char *)++p);
7071 while (VIM_ISDIGIT(*p))
7072 {
7073 ++p;
7074 if (divider)
7075 divider *= 10;
7076 else
7077 divider = 10;
7078 }
7079 }
7080 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7081 {
7082 if (p == digits)
7083 n = sw; /* just "s" is one 'shiftwidth' */
7084 else
7085 {
7086 n *= sw;
7087 if (divider)
7088 n += (sw * fraction + divider / 2) / divider;
7089 }
7090 ++p;
7091 }
7092 if (l[1] == '-')
7093 n = -n;
7094
7095 /* When adding an entry here, also update the default 'cinoptions' in
7096 * doc/indent.txt, and add explanation for it! */
7097 switch (*l)
7098 {
7099 case '>': buf->b_ind_level = n; break;
7100 case 'e': buf->b_ind_open_imag = n; break;
7101 case 'n': buf->b_ind_no_brace = n; break;
7102 case 'f': buf->b_ind_first_open = n; break;
7103 case '{': buf->b_ind_open_extra = n; break;
7104 case '}': buf->b_ind_close_extra = n; break;
7105 case '^': buf->b_ind_open_left_imag = n; break;
7106 case 'L': buf->b_ind_jump_label = n; break;
7107 case ':': buf->b_ind_case = n; break;
7108 case '=': buf->b_ind_case_code = n; break;
7109 case 'b': buf->b_ind_case_break = n; break;
7110 case 'p': buf->b_ind_param = n; break;
7111 case 't': buf->b_ind_func_type = n; break;
7112 case '/': buf->b_ind_comment = n; break;
7113 case 'c': buf->b_ind_in_comment = n; break;
7114 case 'C': buf->b_ind_in_comment2 = n; break;
7115 case 'i': buf->b_ind_cpp_baseclass = n; break;
7116 case '+': buf->b_ind_continuation = n; break;
7117 case '(': buf->b_ind_unclosed = n; break;
7118 case 'u': buf->b_ind_unclosed2 = n; break;
7119 case 'U': buf->b_ind_unclosed_noignore = n; break;
7120 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7121 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7122 case 'm': buf->b_ind_matching_paren = n; break;
7123 case 'M': buf->b_ind_paren_prev = n; break;
7124 case ')': buf->b_ind_maxparen = n; break;
7125 case '*': buf->b_ind_maxcomment = n; break;
7126 case 'g': buf->b_ind_scopedecl = n; break;
7127 case 'h': buf->b_ind_scopedecl_code = n; break;
7128 case 'j': buf->b_ind_java = n; break;
7129 case 'J': buf->b_ind_js = n; break;
7130 case 'l': buf->b_ind_keep_case_label = n; break;
7131 case '#': buf->b_ind_hash_comment = n; break;
7132 case 'N': buf->b_ind_cpp_namespace = n; break;
7133 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007134 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007135 }
7136 if (*p == ',')
7137 ++p;
7138 }
7139}
7140
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007141/*
7142 * Return the desired indent for C code.
7143 * Return -1 if the indent should be left alone (inside a raw string).
7144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007146get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 pos_T cur_curpos;
7149 int amount;
7150 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007151 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 colnr_T col;
7153 char_u *theline;
7154 char_u *linecopy;
7155 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007156 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007158 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 pos_T our_paren_pos;
7160 char_u *start;
7161 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007162#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163#define BRACE_AT_START 2 /* '{' is at start of line */
7164#define BRACE_AT_END 3 /* '{' is at end of line */
7165 linenr_T ourscope;
7166 char_u *l;
7167 char_u *look;
7168 char_u terminated;
7169 int lookfor;
7170#define LOOKFOR_INITIAL 0
7171#define LOOKFOR_IF 1
7172#define LOOKFOR_DO 2
7173#define LOOKFOR_CASE 3
7174#define LOOKFOR_ANY 4
7175#define LOOKFOR_TERM 5
7176#define LOOKFOR_UNTERM 6
7177#define LOOKFOR_SCOPEDECL 7
7178#define LOOKFOR_NOBREAK 8
7179#define LOOKFOR_CPP_BASECLASS 9
7180#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007181#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007182#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
7184 int whilelevel;
7185 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int n;
7187 int iscase;
7188 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007189 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007191 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007192 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007193 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007194 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007195 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007197 /* make a copy, value is changed below */
7198 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
7200 /* remember where the cursor was when we started */
7201 cur_curpos = curwin->w_cursor;
7202
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007203 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007204 if (cur_curpos.lnum == 1)
7205 return 0;
7206
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 /* Get a copy of the current contents of the line.
7208 * This is required, because only the most recent line obtained with
7209 * ml_get is valid! */
7210 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7211 if (linecopy == NULL)
7212 return 0;
7213
7214 /*
7215 * In insert mode and the cursor is on a ')' truncate the line at the
7216 * cursor position. We don't want to line up with the matching '(' when
7217 * inserting new stuff.
7218 * For unknown reasons the cursor might be past the end of the line, thus
7219 * check for that.
7220 */
7221 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007222 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 && linecopy[curwin->w_cursor.col] == ')')
7224 linecopy[curwin->w_cursor.col] = NUL;
7225
7226 theline = skipwhite(linecopy);
7227
7228 /* move the cursor to the start of the line */
7229
7230 curwin->w_cursor.col = 0;
7231
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007232 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007235 * If we are inside a raw string don't change the indent.
7236 * Ignore a raw string inside a comment.
7237 */
7238 comment_pos = ind_find_start_comment();
7239 if (comment_pos != NULL)
7240 {
7241 /* findmatchlimit() static pos is overwritten, make a copy */
7242 tryposCopy = *comment_pos;
7243 comment_pos = &tryposCopy;
7244 }
7245 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007246 if (trypos != NULL && (comment_pos == NULL
7247 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007248 {
7249 amount = -1;
7250 goto laterend;
7251 }
7252
7253 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 * #defines and so on always go at the left when included in 'cinkeys'.
7255 */
7256 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007257 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007258 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007259 goto theend;
7260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007263 * Is it a non-case label? Then that goes at the left margin too unless:
7264 * - JS flag is set.
7265 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007267 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007268 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007271 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273
7274 /*
7275 * If we're inside a "//" comment and there is a "//" comment in a
7276 * previous line, lineup with that one.
7277 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007278 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 && (trypos = find_line_comment()) != NULL) /* XXX */
7280 {
7281 /* find how indented the line beginning the comment is */
7282 getvcol(curwin, trypos, &col, NULL, NULL);
7283 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007284 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286
7287 /*
7288 * If we're inside a comment and not looking at the start of the
7289 * comment, try using the 'comments' option.
7290 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007291 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
7293 int lead_start_len = 2;
7294 int lead_middle_len = 1;
7295 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7296 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7297 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7298 char_u *p;
7299 int start_align = 0;
7300 int start_off = 0;
7301 int done = FALSE;
7302
7303 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007304 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007306 *lead_start = NUL;
7307 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
7309 p = curbuf->b_p_com;
7310 while (*p != NUL)
7311 {
7312 int align = 0;
7313 int off = 0;
7314 int what = 0;
7315
7316 while (*p != NUL && *p != ':')
7317 {
7318 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7319 what = *p++;
7320 else if (*p == COM_LEFT || *p == COM_RIGHT)
7321 align = *p++;
7322 else if (VIM_ISDIGIT(*p) || *p == '-')
7323 off = getdigits(&p);
7324 else
7325 ++p;
7326 }
7327
7328 if (*p == ':')
7329 ++p;
7330 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7331 if (what == COM_START)
7332 {
7333 STRCPY(lead_start, lead_end);
7334 lead_start_len = (int)STRLEN(lead_start);
7335 start_off = off;
7336 start_align = align;
7337 }
7338 else if (what == COM_MIDDLE)
7339 {
7340 STRCPY(lead_middle, lead_end);
7341 lead_middle_len = (int)STRLEN(lead_middle);
7342 }
7343 else if (what == COM_END)
7344 {
7345 /* If our line starts with the middle comment string, line it
7346 * up with the comment opener per the 'comments' option. */
7347 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7348 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7349 {
7350 done = TRUE;
7351 if (curwin->w_cursor.lnum > 1)
7352 {
7353 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007354 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 * the middle comment string matches in the previous
7356 * line, use the indent of that line. XXX */
7357 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7358 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7359 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7360 else if (STRNCMP(look, lead_middle,
7361 lead_middle_len) == 0)
7362 {
7363 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7364 break;
7365 }
7366 /* If the start comment string doesn't match with the
7367 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007368 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 lead_start, lead_start_len) != 0)
7370 continue;
7371 }
7372 if (start_off != 0)
7373 amount += start_off;
7374 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007375 amount += vim_strsize(lead_start)
7376 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 break;
7378 }
7379
7380 /* If our line starts with the end comment string, line it up
7381 * with the middle comment */
7382 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7383 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7384 {
7385 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7386 /* XXX */
7387 if (off != 0)
7388 amount += off;
7389 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007390 amount += vim_strsize(lead_start)
7391 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 done = TRUE;
7393 break;
7394 }
7395 }
7396 }
7397
7398 /* If our line starts with an asterisk, line up with the
7399 * asterisk in the comment opener; otherwise, line up
7400 * with the first character of the comment text.
7401 */
7402 if (done)
7403 ;
7404 else if (theline[0] == '*')
7405 amount += 1;
7406 else
7407 {
7408 /*
7409 * If we are more than one line away from the comment opener, take
7410 * the indent of the previous non-empty line. If 'cino' has "CO"
7411 * and we are just below the comment opener and there are any
7412 * white characters after it line up with the text after it;
7413 * otherwise, add the amount specified by "c" in 'cino'
7414 */
7415 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007416 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
7418 if (linewhite(lnum)) /* skip blank lines */
7419 continue;
7420 amount = get_indent_lnum(lnum); /* XXX */
7421 break;
7422 }
7423 if (amount == -1) /* use the comment opener */
7424 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007425 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007427 start = ml_get(comment_pos->lnum);
7428 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007430 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007432 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007434 if (curbuf->b_ind_in_comment2 || *look == NUL)
7435 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
7437 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007438 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
7440
7441 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007442 * Are we looking at a ']' that has a match?
7443 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007444 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007445 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7446 {
7447 /* align with the line containing the '['. */
7448 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007449 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007450 }
7451
7452 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 * Are we inside parentheses or braces?
7454 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007455 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007456 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007457 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 || trypos != NULL)
7459 {
7460 if (trypos != NULL && tryposBrace != NULL)
7461 {
7462 /* Both an unmatched '(' and '{' is found. Use the one which is
7463 * closer to the current cursor position, set the other to NULL. */
7464 if (trypos->lnum != tryposBrace->lnum
7465 ? trypos->lnum < tryposBrace->lnum
7466 : trypos->col < tryposBrace->col)
7467 trypos = NULL;
7468 else
7469 tryposBrace = NULL;
7470 }
7471
7472 if (trypos != NULL)
7473 {
7474 /*
7475 * If the matching paren is more than one line away, use the indent of
7476 * a previous non-empty line that matches the same paren.
7477 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007478 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007480 /* Line up with the start of the matching paren line. */
7481 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7482 }
7483 else
7484 {
7485 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007486 our_paren_pos = *trypos;
7487 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007489 l = skipwhite(ml_get(lnum));
7490 if (cin_nocode(l)) /* skip comment lines */
7491 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007492 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007493 continue; /* ignore #define, #if, etc. */
7494 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007496 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007497 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007498 {
7499 lnum = trypos->lnum + 1;
7500 continue;
7501 }
7502
7503 /* XXX */
7504 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007505 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007506 && trypos->lnum == our_paren_pos.lnum
7507 && trypos->col == our_paren_pos.col)
7508 {
7509 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007511 if (theline[0] == ')')
7512 {
7513 if (our_paren_pos.lnum != lnum
7514 && cur_amount > amount)
7515 cur_amount = amount;
7516 amount = -1;
7517 }
7518 break;
7519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 }
7521 }
7522
7523 /*
7524 * Line up with line where the matching paren is. XXX
7525 * If the line starts with a '(' or the indent for unclosed
7526 * parentheses is zero, line up with the unclosed parentheses.
7527 */
7528 if (amount == -1)
7529 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007530 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007531 int is_if_for_while = 0;
7532
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007533 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007534 {
7535 /* Look for the outermost opening parenthesis on this line
7536 * and check whether it belongs to an "if", "for" or "while". */
7537
7538 pos_T cursor_save = curwin->w_cursor;
7539 pos_T outermost;
7540 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007541
7542 trypos = &our_paren_pos;
7543 do {
7544 outermost = *trypos;
7545 curwin->w_cursor.lnum = outermost.lnum;
7546 curwin->w_cursor.col = outermost.col;
7547
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007548 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007549 } while (trypos && trypos->lnum == outermost.lnum);
7550
7551 curwin->w_cursor = cursor_save;
7552
7553 line = ml_get(outermost.lnum);
7554
7555 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007556 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007557 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007558
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007559 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007560 look = skipwhite(look);
7561 if (*look == '(')
7562 {
7563 linenr_T save_lnum = curwin->w_cursor.lnum;
7564 char_u *line;
7565 int look_col;
7566
7567 /* Ignore a '(' in front of the line that has a match before
7568 * our matching '('. */
7569 curwin->w_cursor.lnum = our_paren_pos.lnum;
7570 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007571 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007572 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007573 if ((trypos = findmatchlimit(NULL, ')', 0,
7574 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007575 != NULL
7576 && trypos->lnum == our_paren_pos.lnum
7577 && trypos->col < our_paren_pos.col)
7578 ignore_paren_col = trypos->col + 1;
7579
7580 curwin->w_cursor.lnum = save_lnum;
7581 look = ml_get(our_paren_pos.lnum) + look_col;
7582 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007583 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7584 && is_if_for_while == 0)
7585 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007586 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {
7588 /*
7589 * If we're looking at a close paren, line up right there;
7590 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007591 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 * the last nonwhite character of the line, use either the
7593 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007594 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 * lines).
7596 */
7597 if (theline[0] != ')')
7598 {
7599 cur_amount = MAXCOL;
7600 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007601 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 && cin_ends_in(l, (char_u *)"(", NULL))
7603 {
7604 /* look for opening unmatched paren, indent one level
7605 * for each additional level */
7606 n = 1;
7607 for (col = 0; col < our_paren_pos.col; ++col)
7608 {
7609 switch (l[col])
7610 {
7611 case '(':
7612 case '{': ++n;
7613 break;
7614
7615 case ')':
7616 case '}': if (n > 1)
7617 --n;
7618 break;
7619 }
7620 }
7621
7622 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007623 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007625 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 our_paren_pos.col++;
7627 else
7628 {
7629 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007630 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 col++;
7632 if (l[col] != NUL) /* In case of trailing space */
7633 our_paren_pos.col = col;
7634 else
7635 our_paren_pos.col++;
7636 }
7637 }
7638
7639 /*
7640 * Find how indented the paren is, or the character after it
7641 * if we did the above "if".
7642 */
7643 if (our_paren_pos.col > 0)
7644 {
7645 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7646 if (cur_amount > (int)col)
7647 cur_amount = col;
7648 }
7649 }
7650
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007651 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
7653 /* Line up with the start of the matching paren line. */
7654 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007655 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7656 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007657 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
7659 if (cur_amount != MAXCOL)
7660 amount = cur_amount;
7661 }
7662 else
7663 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007664 /* Add b_ind_unclosed2 for each '(' before our matching one,
7665 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007667 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {
7669 --our_paren_pos.col;
7670 switch (*ml_get_pos(&our_paren_pos))
7671 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007672 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 col = our_paren_pos.col;
7674 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007675 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 col = MAXCOL;
7677 break;
7678 }
7679 }
7680
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007681 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 * braces */
7683 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007684 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 else
7686 {
7687 curwin->w_cursor.lnum = our_paren_pos.lnum;
7688 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007689 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7690 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007691 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007693 {
7694 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007695 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007696 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007697 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700 /*
7701 * For a line starting with ')' use the minimum of the two
7702 * positions, to avoid giving it more indent than the previous
7703 * lines:
7704 * func_long_name( if (x
7705 * arg && yy
7706 * ) ^ not here ) ^ not here
7707 */
7708 if (cur_amount < amount)
7709 amount = cur_amount;
7710 }
7711 }
7712
7713 /* add extra indent for a comment */
7714 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007715 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 else
7718 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007719 /*
7720 * We are inside braces, there is a { before this line at the position
7721 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007722 * Make a copy of tryposBrace, it may point to pos_copy inside
7723 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007724 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007725 tryposCopy = *tryposBrace;
7726 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 ourscope = trypos->lnum;
7729 start = ml_get(ourscope);
7730
7731 /*
7732 * Now figure out how indented the line is in general.
7733 * If the brace was at the start of the line, we use that;
7734 * otherwise, check out the indentation of the line as
7735 * a whole and then add the "imaginary indent" to that.
7736 */
7737 look = skipwhite(start);
7738 if (*look == '{')
7739 {
7740 getvcol(curwin, trypos, &col, NULL, NULL);
7741 amount = col;
7742 if (*start == '{')
7743 start_brace = BRACE_IN_COL0;
7744 else
7745 start_brace = BRACE_AT_START;
7746 }
7747 else
7748 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007749 /* That opening brace might have been on a continuation
7750 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 curwin->w_cursor.lnum = ourscope;
7752
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007753 /* Position the cursor over the rightmost paren, so that
7754 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 lnum = ourscope;
7756 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007757 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7758 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 lnum = trypos->lnum;
7760
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007761 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 * case 1: if (asdf &&
7763 * ldfd) {
7764 * }
7765 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007766 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7767 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007769 else if (curbuf->b_ind_js)
7770 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007772 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773
7774 start_brace = BRACE_AT_END;
7775 }
7776
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007777 /* For Javascript check if the line starts with "key:". */
7778 if (curbuf->b_ind_js)
7779 js_cur_has_key = cin_has_js_key(theline);
7780
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007782 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 * we want to be. otherwise, add the amount of room
7784 * that an indent is supposed to be.
7785 */
7786 if (theline[0] == '}')
7787 {
7788 /*
7789 * they may want closing braces to line up with something
7790 * other than the open brace. indulge them, if so.
7791 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007792 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 }
7794 else
7795 {
7796 /*
7797 * If we're looking at an "else", try to find an "if"
7798 * to match it with.
7799 * If we're looking at a "while", try to find a "do"
7800 * to match it with.
7801 */
7802 lookfor = LOOKFOR_INITIAL;
7803 if (cin_iselse(theline))
7804 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007805 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 lookfor = LOOKFOR_DO;
7807 if (lookfor != LOOKFOR_INITIAL)
7808 {
7809 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007810 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {
7812 amount = get_indent(); /* XXX */
7813 goto theend;
7814 }
7815 }
7816
7817 /*
7818 * We get here if we are not on an "while-of-do" or "else" (or
7819 * failed to find a matching "if").
7820 * Search backwards for something to line up with.
7821 * First set amount for when we don't find anything.
7822 */
7823
7824 /*
7825 * if the '{' is _really_ at the left margin, use the imaginary
7826 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007827 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 */
7829
7830 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7831 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007832 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007833 lookfor_cpp_namespace = TRUE;
7834 }
7835 else if (start_brace == BRACE_AT_START &&
7836 lookfor_cpp_namespace) /* '{' is at start */
7837 {
7838
7839 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 }
7841 else
7842 {
7843 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007844 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007845 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007846
7847 l = skipwhite(ml_get_curline());
7848 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007849 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007850 else if (cin_is_cpp_extern_c(l))
7851 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 else
7854 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007855 /* Compensate for adding b_ind_open_extra later. */
7856 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 if (amount < 0)
7858 amount = 0;
7859 }
7860 }
7861
7862 lookfor_break = FALSE;
7863
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007864 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {
7866 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007867 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869 else if (cin_isscopedecl(theline)) /* private:, ... */
7870 {
7871 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007872 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 }
7874 else
7875 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007876 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7877 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 lookfor_break = TRUE;
7879
7880 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007881 /* b_ind_level from start of block */
7882 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 }
7884 scope_amount = amount;
7885 whilelevel = 0;
7886
7887 /*
7888 * Search backwards. If we find something we recognize, line up
7889 * with that.
7890 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007891 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 * the usual amount relative to the conditional
7893 * that opens the block.
7894 */
7895 curwin->w_cursor = cur_curpos;
7896 for (;;)
7897 {
7898 curwin->w_cursor.lnum--;
7899 curwin->w_cursor.col = 0;
7900
7901 /*
7902 * If we went all the way back to the start of our scope, line
7903 * up with it.
7904 */
7905 if (curwin->w_cursor.lnum <= ourscope)
7906 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007907 /* We reached end of scope:
7908 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007910 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 * don't add ind_continuation, otherwise it is a variable
7912 * declaration:
7913 * int x,
7914 * here; <-- add ind_continuation
7915 */
7916 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7917 {
7918 if (curwin->w_cursor.lnum == 0
7919 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007920 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007922 /* nothing found (abuse curbuf->b_ind_maxparen as
7923 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 * initialization) */
7925 if (cont_amount > 0)
7926 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007927 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 amount += ind_continuation;
7929 break;
7930 }
7931
7932 l = ml_get_curline();
7933
7934 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007935 * If we're in a comment or raw string now, skip to
7936 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007938 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (trypos != NULL)
7940 {
7941 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007942 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 continue;
7944 }
7945
7946 /*
7947 * Skip preprocessor directives and blank lines.
7948 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007949 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7950 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 continue;
7952
7953 if (cin_nocode(l))
7954 continue;
7955
7956 terminated = cin_isterminated(l, FALSE, TRUE);
7957
7958 /*
7959 * If we are at top level and the line looks like a
7960 * function declaration, we are done
7961 * (it's a variable declaration).
7962 */
7963 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007964 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {
7966 /* if the line is terminated with another ','
7967 * it is a continued variable initialization.
7968 * don't add extra indent.
7969 * TODO: does not work, if a function
7970 * declaration is split over multiple lines:
7971 * cin_isfuncdecl returns FALSE then.
7972 */
7973 if (terminated == ',')
7974 break;
7975
7976 /* if it es a enum declaration or an assignment,
7977 * we are done.
7978 */
7979 if (terminated != ';' && cin_isinit())
7980 break;
7981
7982 /* nothing useful found */
7983 if (terminated == 0 || terminated == '{')
7984 continue;
7985 }
7986
7987 if (terminated != ';')
7988 {
7989 /* Skip parens and braces. Position the cursor
7990 * over the rightmost paren, so that matching it
7991 * will take us back to the start of the line.
7992 */ /* XXX */
7993 trypos = NULL;
7994 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007995 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007996 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997
7998 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007999 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000
8001 if (trypos != NULL)
8002 {
8003 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008004 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 continue;
8006 }
8007 }
8008
8009 /* it's a variable declaration, add indentation
8010 * like in
8011 * int a,
8012 * b;
8013 */
8014 if (cont_amount > 0)
8015 amount = cont_amount;
8016 else
8017 amount += ind_continuation;
8018 }
8019 else if (lookfor == LOOKFOR_UNTERM)
8020 {
8021 if (cont_amount > 0)
8022 amount = cont_amount;
8023 else
8024 amount += ind_continuation;
8025 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008026 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008027 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008028 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008029 && lookfor != LOOKFOR_CPP_BASECLASS
8030 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008031 {
8032 amount = scope_amount;
8033 if (theline[0] == '{')
8034 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008035 amount += curbuf->b_ind_open_extra;
8036 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008037 }
8038 }
8039
8040 if (lookfor_cpp_namespace)
8041 {
8042 /*
8043 * Looking for C++ namespace, need to look further
8044 * back.
8045 */
8046 if (curwin->w_cursor.lnum == ourscope)
8047 continue;
8048
8049 if (curwin->w_cursor.lnum == 0
8050 || curwin->w_cursor.lnum
8051 < ourscope - FIND_NAMESPACE_LIM)
8052 break;
8053
8054 l = ml_get_curline();
8055
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008056 /* If we're in a comment or raw string now, skip
8057 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008058 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008059 if (trypos != NULL)
8060 {
8061 curwin->w_cursor.lnum = trypos->lnum + 1;
8062 curwin->w_cursor.col = 0;
8063 continue;
8064 }
8065
8066 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008067 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8068 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008069 continue;
8070
8071 /* Finally the actual check for "namespace". */
8072 if (cin_is_cpp_namespace(l))
8073 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008074 amount += curbuf->b_ind_cpp_namespace
8075 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008076 break;
8077 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008078 else if (cin_is_cpp_extern_c(l))
8079 {
8080 amount += curbuf->b_ind_cpp_extern_c
8081 - added_to_amount;
8082 break;
8083 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008084
8085 if (cin_nocode(l))
8086 continue;
8087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089 break;
8090 }
8091
8092 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008093 * If we're in a comment or raw string now, skip to the start
8094 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008096 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {
8098 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008099 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 continue;
8101 }
8102
8103 l = ml_get_curline();
8104
8105 /*
8106 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008107 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008109 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (iscase || cin_isscopedecl(l))
8111 {
8112 /* we are only looking for cpp base class
8113 * declaration/initialization any longer */
8114 if (lookfor == LOOKFOR_CPP_BASECLASS)
8115 break;
8116
8117 /* When looking for a "do" we are not interested in
8118 * labels. */
8119 if (whilelevel > 0)
8120 continue;
8121
8122 /*
8123 * case xx:
8124 * c = 99 + <- this indent plus continuation
8125 *-> here;
8126 */
8127 if (lookfor == LOOKFOR_UNTERM
8128 || lookfor == LOOKFOR_ENUM_OR_INIT)
8129 {
8130 if (cont_amount > 0)
8131 amount = cont_amount;
8132 else
8133 amount += ind_continuation;
8134 break;
8135 }
8136
8137 /*
8138 * case xx: <- line up with this case
8139 * x = 333;
8140 * case yy:
8141 */
8142 if ( (iscase && lookfor == LOOKFOR_CASE)
8143 || (iscase && lookfor_break)
8144 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8145 {
8146 /*
8147 * Check that this case label is not for another
8148 * switch()
8149 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008150 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008151 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
8153 amount = get_indent(); /* XXX */
8154 break;
8155 }
8156 continue;
8157 }
8158
8159 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8160
8161 /*
8162 * case xx: if (cond) <- line up with this if
8163 * y = y + 1;
8164 * -> s = 99;
8165 *
8166 * case xx:
8167 * if (cond) <- line up with this line
8168 * y = y + 1;
8169 * -> s = 99;
8170 */
8171 if (lookfor == LOOKFOR_TERM)
8172 {
8173 if (n)
8174 amount = n;
8175
8176 if (!lookfor_break)
8177 break;
8178 }
8179
8180 /*
8181 * case xx: x = x + 1; <- line up with this x
8182 * -> y = y + 1;
8183 *
8184 * case xx: if (cond) <- line up with this if
8185 * -> y = y + 1;
8186 */
8187 if (n)
8188 {
8189 amount = n;
8190 l = after_label(ml_get_curline());
8191 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008192 {
8193 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008194 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008195 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008196 amount += curbuf->b_ind_level
8197 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 break;
8200 }
8201
8202 /*
8203 * Try to get the indent of a statement before the switch
8204 * label. If nothing is found, line up relative to the
8205 * switch label.
8206 * break; <- may line up with this line
8207 * case xx:
8208 * -> y = 1;
8209 */
8210 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008211 ? curbuf->b_ind_case_code
8212 : curbuf->b_ind_scopedecl_code);
8213 lookfor = curbuf->b_ind_case_break
8214 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 continue;
8216 }
8217
8218 /*
8219 * Looking for a switch() label or C++ scope declaration,
8220 * ignore other lines, skip {}-blocks.
8221 */
8222 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8223 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008224 if (find_last_paren(l, '{', '}')
8225 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008228 curwin->w_cursor.col = 0;
8229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 continue;
8231 }
8232
8233 /*
8234 * Ignore jump labels with nothing after them.
8235 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008236 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {
8238 l = after_label(ml_get_curline());
8239 if (l == NULL || cin_nocode(l))
8240 continue;
8241 }
8242
8243 /*
8244 * Ignore #defines, #if, etc.
8245 * Ignore comment and empty lines.
8246 * (need to get the line again, cin_islabel() may have
8247 * unlocked it)
8248 */
8249 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008250 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 || cin_nocode(l))
8252 continue;
8253
8254 /*
8255 * Are we at the start of a cpp base class declaration or
8256 * constructor initialization?
8257 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008258 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008259 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008260 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008261 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008262 l = ml_get_curline();
8263 }
8264 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {
8266 if (lookfor == LOOKFOR_UNTERM)
8267 {
8268 if (cont_amount > 0)
8269 amount = cont_amount;
8270 else
8271 amount += ind_continuation;
8272 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008273 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008275 /* Need to find start of the declaration. */
8276 lookfor = LOOKFOR_UNTERM;
8277 ind_continuation = 0;
8278 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 }
8280 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008281 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008282 amount = get_baseclass_amount(
8283 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 break;
8285 }
8286 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8287 {
8288 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008289 * declaration or initialization before the opening brace.
8290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 if (cin_isterminated(l, TRUE, FALSE))
8292 break;
8293 else
8294 continue;
8295 }
8296
8297 /*
8298 * What happens next depends on the line being terminated.
8299 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008300 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 * 123,
8302 * sizeof
8303 * here
8304 * Otherwise check whether it is a enumeration or structure
8305 * initialisation (not indented) or a variable declaration
8306 * (indented).
8307 */
8308 terminated = cin_isterminated(l, FALSE, TRUE);
8309
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008310 if (js_cur_has_key)
8311 {
8312 js_cur_has_key = 0; /* only check the first line */
8313 if (curbuf->b_ind_js && terminated == ',')
8314 {
8315 /* For Javascript we might be inside an object:
8316 * key: something, <- align with this
8317 * key: something
8318 * or:
8319 * key: something + <- align with this
8320 * something,
8321 * key: something
8322 */
8323 lookfor = LOOKFOR_JS_KEY;
8324 }
8325 }
8326 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8327 {
8328 amount = get_indent();
8329 break;
8330 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008331 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008332 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008333 if (tryposBrace != NULL && tryposBrace->lnum
8334 >= curwin->w_cursor.lnum)
8335 break;
8336 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008337 /* line below current line is the one that starts a
8338 * (possibly broken) line ending in a comma. */
8339 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008340 else
8341 {
8342 amount = get_indent();
8343 if (curwin->w_cursor.lnum - 1 == ourscope)
8344 /* line above is start of the scope, thus current
8345 * line is the one that stars a (possibly broken)
8346 * line ending in a comma. */
8347 break;
8348 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008349 }
8350
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8352 && terminated == ','))
8353 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008354 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8355 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008356 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 /*
8358 * if we're in the middle of a paren thing,
8359 * go back to the line that starts it so
8360 * we can get the right prevailing indent
8361 * if ( foo &&
8362 * bar )
8363 */
8364 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008365 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008367 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 */
8369 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008370 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008371 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8372 || (trypos->lnum == tryposBrace->lnum
8373 && trypos->col < tryposBrace->col)))
8374 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
8376 /*
8377 * If we are looking for ',', we also look for matching
8378 * braces.
8379 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008380 if (trypos == NULL && terminated == ','
8381 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008382 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384 if (trypos != NULL)
8385 {
8386 /*
8387 * Check if we are on a case label now. This is
8388 * handled above.
8389 * case xx: if ( asdf &&
8390 * asdf)
8391 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008392 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008394 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {
8396 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008397 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 continue;
8399 }
8400 }
8401
8402 /*
8403 * Skip over continuation lines to find the one to get the
8404 * indent from
8405 * char *usethis = "bla\
8406 * bla",
8407 * here;
8408 */
8409 if (terminated == ',')
8410 {
8411 while (curwin->w_cursor.lnum > 1)
8412 {
8413 l = ml_get(curwin->w_cursor.lnum - 1);
8414 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8415 break;
8416 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008417 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
8419 }
8420
8421 /*
8422 * Get indent and pointer to text for current line,
8423 * ignoring any jump label. XXX
8424 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008425 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008426 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008427 else
8428 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 /*
8430 * If this is just above the line we are indenting, and it
8431 * starts with a '{', line it up with this line.
8432 * while (not)
8433 * -> {
8434 * }
8435 */
8436 if (terminated != ',' && lookfor != LOOKFOR_TERM
8437 && theline[0] == '{')
8438 {
8439 amount = cur_amount;
8440 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008441 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 * doesn't start with a '{', which must have a match
8443 * in the same line (scope is the same). Probably:
8444 * { 1, 2 },
8445 * -> { 3, 4 }
8446 */
8447 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008448 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008450 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {
8452 /* have to look back, whether it is a cpp base
8453 * class declaration or initialization */
8454 lookfor = LOOKFOR_CPP_BASECLASS;
8455 continue;
8456 }
8457 break;
8458 }
8459
8460 /*
8461 * Check if we are after an "if", "while", etc.
8462 * Also allow " } else".
8463 */
8464 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8465 {
8466 /*
8467 * Found an unterminated line after an if (), line up
8468 * with the last one.
8469 * if (cond)
8470 * 100 +
8471 * -> here;
8472 */
8473 if (lookfor == LOOKFOR_UNTERM
8474 || lookfor == LOOKFOR_ENUM_OR_INIT)
8475 {
8476 if (cont_amount > 0)
8477 amount = cont_amount;
8478 else
8479 amount += ind_continuation;
8480 break;
8481 }
8482
8483 /*
8484 * If this is just above the line we are indenting, we
8485 * are finished.
8486 * while (not)
8487 * -> here;
8488 * Otherwise this indent can be used when the line
8489 * before this is terminated.
8490 * yyy;
8491 * if (stat)
8492 * while (not)
8493 * xxx;
8494 * -> here;
8495 */
8496 amount = cur_amount;
8497 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008498 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (lookfor != LOOKFOR_TERM)
8500 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008501 amount += curbuf->b_ind_level
8502 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 break;
8504 }
8505
8506 /*
8507 * Special trick: when expecting the while () after a
8508 * do, line up with the while()
8509 * do
8510 * x = 1;
8511 * -> here
8512 */
8513 l = skipwhite(ml_get_curline());
8514 if (cin_isdo(l))
8515 {
8516 if (whilelevel == 0)
8517 break;
8518 --whilelevel;
8519 }
8520
8521 /*
8522 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008523 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 * Need to use the scope of this "else". XXX
8525 * If whilelevel != 0 continue looking for a "do {".
8526 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008527 if (cin_iselse(l) && whilelevel == 0)
8528 {
8529 /* If we're looking at "} else", let's make sure we
8530 * find the opening brace of the enclosing scope,
8531 * not the one from "if () {". */
8532 if (*l == '}')
8533 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008534 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008535
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008536 if ((trypos = find_start_brace()) == NULL
8537 || find_match(LOOKFOR_IF, trypos->lnum)
8538 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008539 break;
8540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 }
8542
8543 /*
8544 * If we're below an unterminated line that is not an
8545 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008546 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 * the line before this one.
8548 */
8549 else
8550 {
8551 /*
8552 * Found two unterminated lines on a row, line up with
8553 * the last one.
8554 * c = 99 +
8555 * 100 +
8556 * -> here;
8557 */
8558 if (lookfor == LOOKFOR_UNTERM)
8559 {
8560 /* When line ends in a comma add extra indent */
8561 if (terminated == ',')
8562 amount += ind_continuation;
8563 break;
8564 }
8565
8566 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8567 {
8568 /* Found two lines ending in ',', lineup with the
8569 * lowest one, but check for cpp base class
8570 * declaration/initialization, if it is an
8571 * opening brace or we are looking just for
8572 * enumerations/initializations. */
8573 if (terminated == ',')
8574 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008575 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 break;
8577
8578 lookfor = LOOKFOR_CPP_BASECLASS;
8579 continue;
8580 }
8581
8582 /* Ignore unterminated lines in between, but
8583 * reduce indent. */
8584 if (amount > cur_amount)
8585 amount = cur_amount;
8586 }
8587 else
8588 {
8589 /*
8590 * Found first unterminated line on a row, may
8591 * line up with this line, remember its indent
8592 * 100 +
8593 * -> here;
8594 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008595 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008597
8598 n = (int)STRLEN(l);
8599 if (terminated == ',' && (*skipwhite(l) == ']'
8600 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603 /*
8604 * If previous line ends in ',', check whether we
8605 * are in an initialization or enum
8606 * struct xxx =
8607 * {
8608 * sizeof a,
8609 * 124 };
8610 * or a normal possible continuation line.
8611 * but only, of no other statement has been found
8612 * yet.
8613 */
8614 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8615 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008616 if (curbuf->b_ind_js)
8617 {
8618 /* Search for a line ending in a comma
8619 * and line up with the line below it
8620 * (could be the current line).
8621 * some = [
8622 * 1, <- line up here
8623 * 2,
8624 * some = [
8625 * 3 + <- line up here
8626 * 4 *
8627 * 5,
8628 * 6,
8629 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008630 if (cin_iscomment(skipwhite(l)))
8631 break;
8632 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008633 trypos = find_match_char('[',
8634 curbuf->b_ind_maxparen);
8635 if (trypos != NULL)
8636 {
8637 if (trypos->lnum
8638 == curwin->w_cursor.lnum - 1)
8639 {
8640 /* Current line is first inside
8641 * [], line up with it. */
8642 break;
8643 }
8644 ourscope = trypos->lnum;
8645 }
8646 }
8647 else
8648 {
8649 lookfor = LOOKFOR_ENUM_OR_INIT;
8650 cont_amount = cin_first_id_amount();
8651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653 else
8654 {
8655 if (lookfor == LOOKFOR_INITIAL
8656 && *l != NUL
8657 && l[STRLEN(l) - 1] == '\\')
8658 /* XXX */
8659 cont_amount = cin_get_equal_amount(
8660 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008661 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008662 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008663 && lookfor != LOOKFOR_COMMA
8664 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 lookfor = LOOKFOR_UNTERM;
8666 }
8667 }
8668 }
8669 }
8670
8671 /*
8672 * Check if we are after a while (cond);
8673 * If so: Ignore until the matching "do".
8674 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008675 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 {
8677 /*
8678 * Found an unterminated line after a while ();, line up
8679 * with the last one.
8680 * while (cond);
8681 * 100 + <- line up with this one
8682 * -> here;
8683 */
8684 if (lookfor == LOOKFOR_UNTERM
8685 || lookfor == LOOKFOR_ENUM_OR_INIT)
8686 {
8687 if (cont_amount > 0)
8688 amount = cont_amount;
8689 else
8690 amount += ind_continuation;
8691 break;
8692 }
8693
8694 if (whilelevel == 0)
8695 {
8696 lookfor = LOOKFOR_TERM;
8697 amount = get_indent(); /* XXX */
8698 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008699 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 }
8701 ++whilelevel;
8702 }
8703
8704 /*
8705 * We are after a "normal" statement.
8706 * If we had another statement we can stop now and use the
8707 * indent of that other statement.
8708 * Otherwise the indent of the current statement may be used,
8709 * search backwards for the next "normal" statement.
8710 */
8711 else
8712 {
8713 /*
8714 * Skip single break line, if before a switch label. It
8715 * may be lined up with the case label.
8716 */
8717 if (lookfor == LOOKFOR_NOBREAK
8718 && cin_isbreak(skipwhite(ml_get_curline())))
8719 {
8720 lookfor = LOOKFOR_ANY;
8721 continue;
8722 }
8723
8724 /*
8725 * Handle "do {" line.
8726 */
8727 if (whilelevel > 0)
8728 {
8729 l = cin_skipcomment(ml_get_curline());
8730 if (cin_isdo(l))
8731 {
8732 amount = get_indent(); /* XXX */
8733 --whilelevel;
8734 continue;
8735 }
8736 }
8737
8738 /*
8739 * Found a terminated line above an unterminated line. Add
8740 * the amount for a continuation line.
8741 * x = 1;
8742 * y = foo +
8743 * -> here;
8744 * or
8745 * int x = 1;
8746 * int foo,
8747 * -> here;
8748 */
8749 if (lookfor == LOOKFOR_UNTERM
8750 || lookfor == LOOKFOR_ENUM_OR_INIT)
8751 {
8752 if (cont_amount > 0)
8753 amount = cont_amount;
8754 else
8755 amount += ind_continuation;
8756 break;
8757 }
8758
8759 /*
8760 * Found a terminated line above a terminated line or "if"
8761 * etc. line. Use the amount of the line below us.
8762 * x = 1; x = 1;
8763 * if (asdf) y = 2;
8764 * while (asdf) ->here;
8765 * here;
8766 * ->foo;
8767 */
8768 if (lookfor == LOOKFOR_TERM)
8769 {
8770 if (!lookfor_break && whilelevel == 0)
8771 break;
8772 }
8773
8774 /*
8775 * First line above the one we're indenting is terminated.
8776 * To know what needs to be done look further backward for
8777 * a terminated line.
8778 */
8779 else
8780 {
8781 /*
8782 * position the cursor over the rightmost paren, so
8783 * that matching it will take us back to the start of
8784 * the line. Helps for:
8785 * func(asdr,
8786 * asdfasdf);
8787 * here;
8788 */
8789term_again:
8790 l = ml_get_curline();
8791 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008792 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008793 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 {
8795 /*
8796 * Check if we are on a case label now. This is
8797 * handled above.
8798 * case xx: if ( asdf &&
8799 * asdf)
8800 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008801 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008803 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {
8805 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008806 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 continue;
8808 }
8809 }
8810
8811 /* When aligning with the case statement, don't align
8812 * with a statement after it.
8813 * case 1: { <-- don't use this { position
8814 * stat;
8815 * }
8816 * case 2:
8817 * stat;
8818 * }
8819 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008820 iscase = (curbuf->b_ind_keep_case_label
8821 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
8823 /*
8824 * Get indent and pointer to text for current line,
8825 * ignoring any jump label.
8826 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008827 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008830 amount += curbuf->b_ind_open_extra;
8831 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008832 l = skipwhite(l);
8833 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008834 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8836
8837 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008838 * When a terminated line starts with "else" skip to
8839 * the matching "if":
8840 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008841 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008842 * Need to use the scope of this "else". XXX
8843 * If whilelevel != 0 continue looking for a "do {".
8844 */
8845 if (lookfor == LOOKFOR_TERM
8846 && *l != '}'
8847 && cin_iselse(l)
8848 && whilelevel == 0)
8849 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008850 if ((trypos = find_start_brace()) == NULL
8851 || find_match(LOOKFOR_IF, trypos->lnum)
8852 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008853 break;
8854 continue;
8855 }
8856
8857 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 * If we're at the end of a block, skip to the start of
8859 * that block.
8860 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008861 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008862 if (find_last_paren(l, '{', '}') /* XXX */
8863 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008865 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 /* if not "else {" check for terminated again */
8867 /* but skip block for "} else {" */
8868 l = cin_skipcomment(ml_get_curline());
8869 if (*l == '}' || !cin_iselse(l))
8870 goto term_again;
8871 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008872 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 }
8874 }
8875 }
8876 }
8877 }
8878 }
8879
8880 /* add extra indent for a comment */
8881 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008882 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008883
8884 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008885 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8886 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008887
8888 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008890
8891 /*
8892 * ok -- we're not inside any sort of structure at all!
8893 *
8894 * This means we're at the top level, and everything should
8895 * basically just match where the previous line is, except
8896 * for the lines immediately following a function declaration,
8897 * which are K&R-style parameters and need to be indented.
8898 *
8899 * if our line starts with an open brace, forget about any
8900 * prevailing indent and make sure it looks like the start
8901 * of a function
8902 */
8903
8904 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008906 amount = curbuf->b_ind_first_open;
8907 goto theend;
8908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008910 /*
8911 * If the NEXT line is a function declaration, the current
8912 * line needs to be indented as a function type spec.
8913 * Don't do this if the current line looks like a comment or if the
8914 * current line is terminated, ie. ends in ';', or if the current line
8915 * contains { or }: "void f() {\n if (1)"
8916 */
8917 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8918 && !cin_nocode(theline)
8919 && vim_strchr(theline, '{') == NULL
8920 && vim_strchr(theline, '}') == NULL
8921 && !cin_ends_in(theline, (char_u *)":", NULL)
8922 && !cin_ends_in(theline, (char_u *)",", NULL)
8923 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8924 cur_curpos.lnum + 1)
8925 && !cin_isterminated(theline, FALSE, TRUE))
8926 {
8927 amount = curbuf->b_ind_func_type;
8928 goto theend;
8929 }
8930
8931 /* search backwards until we find something we recognize */
8932 amount = 0;
8933 curwin->w_cursor = cur_curpos;
8934 while (curwin->w_cursor.lnum > 1)
8935 {
8936 curwin->w_cursor.lnum--;
8937 curwin->w_cursor.col = 0;
8938
8939 l = ml_get_curline();
8940
8941 /*
8942 * If we're in a comment or raw string now, skip to the start
8943 * of it.
8944 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008945 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008947 curwin->w_cursor.lnum = trypos->lnum + 1;
8948 curwin->w_cursor.col = 0;
8949 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951
8952 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008953 * Are we at the start of a cpp base class declaration or
8954 * constructor initialization?
8955 */ /* XXX */
8956 n = FALSE;
8957 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008959 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8960 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008962 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008964 /* XXX */
8965 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8966 break;
8967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008969 /*
8970 * Skip preprocessor directives and blank lines.
8971 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008972 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008973 continue;
8974
8975 if (cin_nocode(l))
8976 continue;
8977
8978 /*
8979 * If the previous line ends in ',', use one level of
8980 * indentation:
8981 * int foo,
8982 * bar;
8983 * do this before checking for '}' in case of eg.
8984 * enum foobar
8985 * {
8986 * ...
8987 * } foo,
8988 * bar;
8989 */
8990 n = 0;
8991 if (cin_ends_in(l, (char_u *)",", NULL)
8992 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8993 {
8994 /* take us back to opening paren */
8995 if (find_last_paren(l, '(', ')')
8996 && (trypos = find_match_paren(
8997 curbuf->b_ind_maxparen)) != NULL)
8998 curwin->w_cursor = *trypos;
8999
9000 /* For a line ending in ',' that is a continuation line go
9001 * back to the first line with a backslash:
9002 * char *foo = "bla\
9003 * bla",
9004 * here;
9005 */
9006 while (n == 0 && curwin->w_cursor.lnum > 1)
9007 {
9008 l = ml_get(curwin->w_cursor.lnum - 1);
9009 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9010 break;
9011 --curwin->w_cursor.lnum;
9012 curwin->w_cursor.col = 0;
9013 }
9014
9015 amount = get_indent(); /* XXX */
9016
9017 if (amount == 0)
9018 amount = cin_first_id_amount();
9019 if (amount == 0)
9020 amount = ind_continuation;
9021 break;
9022 }
9023
9024 /*
9025 * If the line looks like a function declaration, and we're
9026 * not in a comment, put it the left margin.
9027 */
9028 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9029 break;
9030 l = ml_get_curline();
9031
9032 /*
9033 * Finding the closing '}' of a previous function. Put
9034 * current line at the left margin. For when 'cino' has "fs".
9035 */
9036 if (*skipwhite(l) == '}')
9037 break;
9038
9039 /* (matching {)
9040 * If the previous line ends on '};' (maybe followed by
9041 * comments) align at column 0. For example:
9042 * char *string_array[] = { "foo",
9043 * / * x * / "b};ar" }; / * foobar * /
9044 */
9045 if (cin_ends_in(l, (char_u *)"};", NULL))
9046 break;
9047
9048 /*
9049 * If the previous line ends on '[' we are probably in an
9050 * array constant:
9051 * something = [
9052 * 234, <- extra indent
9053 */
9054 if (cin_ends_in(l, (char_u *)"[", NULL))
9055 {
9056 amount = get_indent() + ind_continuation;
9057 break;
9058 }
9059
9060 /*
9061 * Find a line only has a semicolon that belongs to a previous
9062 * line ending in '}', e.g. before an #endif. Don't increase
9063 * indent then.
9064 */
9065 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9066 {
9067 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068
9069 while (curwin->w_cursor.lnum > 1)
9070 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009071 look = ml_get(--curwin->w_cursor.lnum);
9072 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009073 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009075 }
9076 if (curwin->w_cursor.lnum > 0
9077 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009080 curwin->w_cursor = curpos_save;
9081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009083 /*
9084 * If the PREVIOUS line is a function declaration, the current
9085 * line (and the ones that follow) needs to be indented as
9086 * parameters.
9087 */
9088 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9089 {
9090 amount = curbuf->b_ind_param;
9091 break;
9092 }
9093
9094 /*
9095 * If the previous line ends in ';' and the line before the
9096 * previous line ends in ',' or '\', ident to column zero:
9097 * int foo,
9098 * bar;
9099 * indent_to_0 here;
9100 */
9101 if (cin_ends_in(l, (char_u *)";", NULL))
9102 {
9103 l = ml_get(curwin->w_cursor.lnum - 1);
9104 if (cin_ends_in(l, (char_u *)",", NULL)
9105 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9106 break;
9107 l = ml_get_curline();
9108 }
9109
9110 /*
9111 * Doesn't look like anything interesting -- so just
9112 * use the indent of this line.
9113 *
9114 * Position the cursor over the rightmost paren, so that
9115 * matching it will take us back to the start of the line.
9116 */
9117 find_last_paren(l, '(', ')');
9118
9119 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9120 curwin->w_cursor = *trypos;
9121 amount = get_indent(); /* XXX */
9122 break;
9123 }
9124
9125 /* add extra indent for a comment */
9126 if (cin_iscomment(theline))
9127 amount += curbuf->b_ind_comment;
9128
9129 /* add extra indent if the previous line ended in a backslash:
9130 * "asdfasdf\
9131 * here";
9132 * char *foo = "asdf\
9133 * here";
9134 */
9135 if (cur_curpos.lnum > 1)
9136 {
9137 l = ml_get(cur_curpos.lnum - 1);
9138 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9139 {
9140 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9141 if (cur_amount > 0)
9142 amount = cur_amount;
9143 else if (cur_amount == 0)
9144 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 }
9146 }
9147
9148theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009149 if (amount < 0)
9150 amount = 0;
9151
9152laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 /* put the cursor back where it belongs */
9154 curwin->w_cursor = cur_curpos;
9155
9156 vim_free(linecopy);
9157
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 return amount;
9159}
9160
9161 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009162find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164 char_u *look;
9165 pos_T *theirscope;
9166 char_u *mightbeif;
9167 int elselevel;
9168 int whilelevel;
9169
9170 if (lookfor == LOOKFOR_IF)
9171 {
9172 elselevel = 1;
9173 whilelevel = 0;
9174 }
9175 else
9176 {
9177 elselevel = 0;
9178 whilelevel = 1;
9179 }
9180
9181 curwin->w_cursor.col = 0;
9182
9183 while (curwin->w_cursor.lnum > ourscope + 1)
9184 {
9185 curwin->w_cursor.lnum--;
9186 curwin->w_cursor.col = 0;
9187
9188 look = cin_skipcomment(ml_get_curline());
9189 if (cin_iselse(look)
9190 || cin_isif(look)
9191 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009192 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 {
9194 /*
9195 * if we've gone outside the braces entirely,
9196 * we must be out of scope...
9197 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009198 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 if (theirscope == NULL)
9200 break;
9201
9202 /*
9203 * and if the brace enclosing this is further
9204 * back than the one enclosing the else, we're
9205 * out of luck too.
9206 */
9207 if (theirscope->lnum < ourscope)
9208 break;
9209
9210 /*
9211 * and if they're enclosed in a *deeper* brace,
9212 * then we can ignore it because it's in a
9213 * different scope...
9214 */
9215 if (theirscope->lnum > ourscope)
9216 continue;
9217
9218 /*
9219 * if it was an "else" (that's not an "else if")
9220 * then we need to go back to another if, so
9221 * increment elselevel
9222 */
9223 look = cin_skipcomment(ml_get_curline());
9224 if (cin_iselse(look))
9225 {
9226 mightbeif = cin_skipcomment(look + 4);
9227 if (!cin_isif(mightbeif))
9228 ++elselevel;
9229 continue;
9230 }
9231
9232 /*
9233 * if it was a "while" then we need to go back to
9234 * another "do", so increment whilelevel. XXX
9235 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009236 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 {
9238 ++whilelevel;
9239 continue;
9240 }
9241
9242 /* If it's an "if" decrement elselevel */
9243 look = cin_skipcomment(ml_get_curline());
9244 if (cin_isif(look))
9245 {
9246 elselevel--;
9247 /*
9248 * When looking for an "if" ignore "while"s that
9249 * get in the way.
9250 */
9251 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9252 whilelevel = 0;
9253 }
9254
9255 /* If it's a "do" decrement whilelevel */
9256 if (cin_isdo(look))
9257 whilelevel--;
9258
9259 /*
9260 * if we've used up all the elses, then
9261 * this must be the if that we want!
9262 * match the indent level of that if.
9263 */
9264 if (elselevel <= 0 && whilelevel <= 0)
9265 {
9266 return OK;
9267 }
9268 }
9269 }
9270 return FAIL;
9271}
9272
9273# if defined(FEAT_EVAL) || defined(PROTO)
9274/*
9275 * Get indent level from 'indentexpr'.
9276 */
9277 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009278get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009280 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009281 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009282 pos_T save_pos;
9283 colnr_T save_curswant;
9284 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009286 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9287 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009289 /* Save and restore cursor position and curswant, in case it was changed
9290 * via :normal commands */
9291 save_pos = curwin->w_cursor;
9292 save_curswant = curwin->w_curswant;
9293 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009295 if (use_sandbox)
9296 ++sandbox;
9297 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009298
9299 /* Need to make a copy, the 'indentexpr' option could be changed while
9300 * evaluating it. */
9301 inde_copy = vim_strsave(curbuf->b_p_inde);
9302 if (inde_copy != NULL)
9303 {
9304 indent = (int)eval_to_number(inde_copy);
9305 vim_free(inde_copy);
9306 }
9307
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009308 if (use_sandbox)
9309 --sandbox;
9310 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
9312 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9313 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9314 * command. */
9315 save_State = State;
9316 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009317 curwin->w_cursor = save_pos;
9318 curwin->w_curswant = save_curswant;
9319 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 check_cursor();
9321 State = save_State;
9322
9323 /* If there is an error, just keep the current indent. */
9324 if (indent < 0)
9325 indent = get_indent();
9326
9327 return indent;
9328}
9329# endif
9330
9331#endif /* FEAT_CINDENT */
9332
9333#if defined(FEAT_LISP) || defined(PROTO)
9334
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009335static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336
9337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009338lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339{
9340 char_u buf[LSIZE];
9341 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009342 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343
9344 while (*word != NUL)
9345 {
9346 (void)copy_option_part(&word, buf, LSIZE, ",");
9347 len = (int)STRLEN(buf);
9348 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9349 return TRUE;
9350 }
9351 return FALSE;
9352}
9353
9354/*
9355 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9356 * The incompatible newer method is quite a bit better at indenting
9357 * code in lisp-like languages than the traditional one; it's still
9358 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9359 *
9360 * TODO:
9361 * Findmatch() should be adapted for lisp, also to make showmatch
9362 * work correctly: now (v5.3) it seems all C/C++ oriented:
9363 * - it does not recognize the #\( and #\) notations as character literals
9364 * - it doesn't know about comments starting with a semicolon
9365 * - it incorrectly interprets '(' as a character literal
9366 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009367 * Update from Sergey Khorev:
9368 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 */
9370 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009371get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009373 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 int amount;
9375 char_u *that;
9376 colnr_T col;
9377 colnr_T firsttry;
9378 int parencount, quotecount;
9379 int vi_lisp;
9380
9381 /* Set vi_lisp to use the vi-compatible method */
9382 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9383
9384 realpos = curwin->w_cursor;
9385 curwin->w_cursor.col = 0;
9386
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009387 if ((pos = findmatch(NULL, '(')) == NULL)
9388 pos = findmatch(NULL, '[');
9389 else
9390 {
9391 paren = *pos;
9392 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009393 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009394 pos = &paren;
9395 }
9396 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 {
9398 /* Extra trick: Take the indent of the first previous non-white
9399 * line that is at the same () level. */
9400 amount = -1;
9401 parencount = 0;
9402
9403 while (--curwin->w_cursor.lnum >= pos->lnum)
9404 {
9405 if (linewhite(curwin->w_cursor.lnum))
9406 continue;
9407 for (that = ml_get_curline(); *that != NUL; ++that)
9408 {
9409 if (*that == ';')
9410 {
9411 while (*(that + 1) != NUL)
9412 ++that;
9413 continue;
9414 }
9415 if (*that == '\\')
9416 {
9417 if (*(that + 1) != NUL)
9418 ++that;
9419 continue;
9420 }
9421 if (*that == '"' && *(that + 1) != NUL)
9422 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009423 while (*++that && *that != '"')
9424 {
9425 /* skipping escaped characters in the string */
9426 if (*that == '\\')
9427 {
9428 if (*++that == NUL)
9429 break;
9430 if (that[1] == NUL)
9431 {
9432 ++that;
9433 break;
9434 }
9435 }
9436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009438 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009440 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 --parencount;
9442 }
9443 if (parencount == 0)
9444 {
9445 amount = get_indent();
9446 break;
9447 }
9448 }
9449
9450 if (amount == -1)
9451 {
9452 curwin->w_cursor.lnum = pos->lnum;
9453 curwin->w_cursor.col = pos->col;
9454 col = pos->col;
9455
9456 that = ml_get_curline();
9457
9458 if (vi_lisp && get_indent() == 0)
9459 amount = 2;
9460 else
9461 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009462 char_u *line = that;
9463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 amount = 0;
9465 while (*that && col)
9466 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009467 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 col--;
9469 }
9470
9471 /*
9472 * Some keywords require "body" indenting rules (the
9473 * non-standard-lisp ones are Scheme special forms):
9474 *
9475 * (let ((a 1)) instead (let ((a 1))
9476 * (...)) of (...))
9477 */
9478
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009479 if (!vi_lisp && (*that == '(' || *that == '[')
9480 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 amount += 2;
9482 else
9483 {
9484 that++;
9485 amount++;
9486 firsttry = amount;
9487
Bram Moolenaar1c465442017-03-12 20:10:05 +01009488 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009490 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 ++that;
9492 }
9493
9494 if (*that && *that != ';') /* not a comment line */
9495 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009496 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009498 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 firsttry++;
9500
9501 parencount = 0;
9502 quotecount = 0;
9503
9504 if (vi_lisp
9505 || (*that != '"'
9506 && *that != '\''
9507 && *that != '#'
9508 && (*that < '0' || *that > '9')))
9509 {
9510 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009511 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 || quotecount
9513 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009514 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 && !quotecount
9516 && !parencount
9517 && vi_lisp)))
9518 {
9519 if (*that == '"')
9520 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009521 if ((*that == '(' || *that == '[')
9522 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009524 if ((*that == ')' || *that == ']')
9525 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 --parencount;
9527 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009528 amount += lbr_chartabsize_adv(
9529 line, &that, (colnr_T)amount);
9530 amount += lbr_chartabsize_adv(
9531 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 }
9533 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009534 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009536 amount += lbr_chartabsize(
9537 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 that++;
9539 }
9540 if (!*that || *that == ';')
9541 amount = firsttry;
9542 }
9543 }
9544 }
9545 }
9546 }
9547 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009548 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
9550 curwin->w_cursor = realpos;
9551
9552 return amount;
9553}
9554#endif /* FEAT_LISP */
9555
9556 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009557prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009559#if defined(SIGHUP) && defined(SIG_IGN)
9560 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9561 * makes Vim exit and then handling SIGHUP causes various reentrance
9562 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009563 signal(SIGHUP, SIG_IGN);
9564#endif
9565
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566#ifdef FEAT_GUI
9567 if (gui.in_use)
9568 {
9569 gui.dying = TRUE;
9570 out_trash(); /* trash any pending output */
9571 }
9572 else
9573#endif
9574 {
9575 windgoto((int)Rows - 1, 0);
9576
9577 /*
9578 * Switch terminal mode back now, so messages end up on the "normal"
9579 * screen (if there are two screens).
9580 */
9581 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009582 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 out_flush();
9584 }
9585}
9586
9587/*
9588 * Preserve files and exit.
9589 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009590 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9591 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 */
9593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009594preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 buf_T *buf;
9597
9598 prepare_to_exit();
9599
Bram Moolenaar4770d092006-01-12 23:22:24 +00009600 /* Setting this will prevent free() calls. That avoids calling free()
9601 * recursively when free() was invoked with a bad pointer. */
9602 really_exiting = TRUE;
9603
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 out_str(IObuff);
9605 screen_start(); /* don't know where cursor is now */
9606 out_flush();
9607
9608 ml_close_notmod(); /* close all not-modified buffers */
9609
Bram Moolenaar29323592016-07-24 22:04:11 +02009610 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 {
9612 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9613 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009614 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 screen_start(); /* don't know where cursor is now */
9616 out_flush();
9617 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9618 break;
9619 }
9620 }
9621
9622 ml_close_all(FALSE); /* close all memfiles, without deleting */
9623
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009624 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625
9626 getout(1);
9627}
9628
9629/*
9630 * return TRUE if "fname" exists.
9631 */
9632 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009633vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009635 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636
9637 if (mch_stat((char *)fname, &st))
9638 return FALSE;
9639 return TRUE;
9640}
9641
9642/*
9643 * Check for CTRL-C pressed, but only once in a while.
9644 * Should be used instead of ui_breakcheck() for functions that check for
9645 * each line in the file. Calling ui_breakcheck() each time takes too much
9646 * time, because it can be a system call.
9647 */
9648
9649#ifndef BREAKCHECK_SKIP
9650# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9651# define BREAKCHECK_SKIP 200
9652# else
9653# define BREAKCHECK_SKIP 32
9654# endif
9655#endif
9656
9657static int breakcheck_count = 0;
9658
9659 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009660line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 if (++breakcheck_count >= BREAKCHECK_SKIP)
9663 {
9664 breakcheck_count = 0;
9665 ui_breakcheck();
9666 }
9667}
9668
9669/*
9670 * Like line_breakcheck() but check 10 times less often.
9671 */
9672 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009673fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674{
9675 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9676 {
9677 breakcheck_count = 0;
9678 ui_breakcheck();
9679 }
9680}
9681
9682/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009683 * Invoke expand_wildcards() for one pattern.
9684 * Expand items like "%:h" before the expansion.
9685 * Returns OK or FAIL.
9686 */
9687 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009688expand_wildcards_eval(
9689 char_u **pat, /* pointer to input pattern */
9690 int *num_file, /* resulting number of files */
9691 char_u ***file, /* array of resulting files */
9692 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009693{
9694 int ret = FAIL;
9695 char_u *eval_pat = NULL;
9696 char_u *exp_pat = *pat;
9697 char_u *ignored_msg;
9698 int usedlen;
9699
9700 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9701 {
9702 ++emsg_off;
9703 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9704 NULL, &ignored_msg, NULL);
9705 --emsg_off;
9706 if (eval_pat != NULL)
9707 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9708 }
9709
9710 if (exp_pat != NULL)
9711 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9712
9713 if (eval_pat != NULL)
9714 {
9715 vim_free(exp_pat);
9716 vim_free(eval_pat);
9717 }
9718
9719 return ret;
9720}
9721
9722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9724 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009725 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 */
9727 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009728expand_wildcards(
9729 int num_pat, /* number of input patterns */
9730 char_u **pat, /* array of input patterns */
9731 int *num_files, /* resulting number of files */
9732 char_u ***files, /* array of resulting files */
9733 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 int retval;
9736 int i, j;
9737 char_u *p;
9738 int non_suf_match; /* number without matching suffix */
9739
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009740 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741
9742 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009743 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 return retval;
9745
9746#ifdef FEAT_WILDIGN
9747 /*
9748 * Remove names that match 'wildignore'.
9749 */
9750 if (*p_wig)
9751 {
9752 char_u *ffname;
9753
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009754 /* check all files in (*files)[] */
9755 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009757 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (ffname == NULL) /* out of memory */
9759 break;
9760# ifdef VMS
9761 vms_remove_version(ffname);
9762# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009763 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009765 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009766 vim_free((*files)[i]);
9767 for (j = i; j + 1 < *num_files; ++j)
9768 (*files)[j] = (*files)[j + 1];
9769 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 --i;
9771 }
9772 vim_free(ffname);
9773 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009774
9775 /* If the number of matches is now zero, we fail. */
9776 if (*num_files == 0)
9777 {
9778 vim_free(*files);
9779 *files = NULL;
9780 return FAIL;
9781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 }
9783#endif
9784
9785 /*
9786 * Move the names where 'suffixes' match to the end.
9787 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009788 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 {
9790 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009791 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009793 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 {
9795 /*
9796 * Move the name without matching suffix to the front
9797 * of the list.
9798 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009799 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009801 (*files)[j] = (*files)[j - 1];
9802 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 }
9804 }
9805 }
9806
9807 return retval;
9808}
9809
9810/*
9811 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9812 */
9813 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009814match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816 int fnamelen, setsuflen;
9817 char_u *setsuf;
9818#define MAXSUFLEN 30 /* maximum length of a file suffix */
9819 char_u suf_buf[MAXSUFLEN];
9820
9821 fnamelen = (int)STRLEN(fname);
9822 setsuflen = 0;
9823 for (setsuf = p_su; *setsuf; )
9824 {
9825 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009826 if (setsuflen == 0)
9827 {
9828 char_u *tail = gettail(fname);
9829
9830 /* empty entry: match name without a '.' */
9831 if (vim_strchr(tail, '.') == NULL)
9832 {
9833 setsuflen = 1;
9834 break;
9835 }
9836 }
9837 else
9838 {
9839 if (fnamelen >= setsuflen
9840 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9841 (size_t)setsuflen) == 0)
9842 break;
9843 setsuflen = 0;
9844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 }
9846 return (setsuflen != 0);
9847}
9848
9849#if !defined(NO_EXPANDPATH) || defined(PROTO)
9850
9851# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009852static int vim_backtick(char_u *p);
9853static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854# endif
9855
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009856# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857/*
9858 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9859 * it's shared between these systems.
9860 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009861# if defined(PROTO)
9862# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863# else
9864# ifdef __BORLANDC__
9865# define _cdecl _RTLENTRYF
9866# endif
9867# endif
9868
9869/*
9870 * comparison function for qsort in dos_expandpath()
9871 */
9872 static int _cdecl
9873pstrcmp(const void *a, const void *b)
9874{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009875 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876}
9877
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009879 * Recursively expand one path component into all matching files and/or
9880 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 * Return the number of matches found.
9882 * "path" has backslashes before chars that are not to be expanded, starting
9883 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009884 * Return the number of matches found.
9885 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 */
9887 static int
9888dos_expandpath(
9889 garray_T *gap,
9890 char_u *path,
9891 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009892 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009893 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009895 char_u *buf;
9896 char_u *path_end;
9897 char_u *p, *s, *e;
9898 int start_len = gap->ga_len;
9899 char_u *pat;
9900 regmatch_T regmatch;
9901 int starts_with_dot;
9902 int matches;
9903 int len;
9904 int starstar = FALSE;
9905 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 WIN32_FIND_DATA fb;
9907 HANDLE hFind = (HANDLE)0;
9908# ifdef FEAT_MBYTE
9909 WIN32_FIND_DATAW wfb;
9910 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009913 int ok;
9914
9915 /* Expanding "**" may take a long time, check for CTRL-C. */
9916 if (stardepth > 0)
9917 {
9918 ui_breakcheck();
9919 if (got_int)
9920 return 0;
9921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009923 /* Make room for file name. When doing encoding conversion the actual
9924 * length may be quite a bit longer, thus use the maximum possible length. */
9925 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 if (buf == NULL)
9927 return 0;
9928
9929 /*
9930 * Find the first part in the path name that contains a wildcard or a ~1.
9931 * Copy it into buf, including the preceding characters.
9932 */
9933 p = buf;
9934 s = buf;
9935 e = NULL;
9936 path_end = path;
9937 while (*path_end != NUL)
9938 {
9939 /* May ignore a wildcard that has a backslash before it; it will
9940 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9941 if (path_end >= path + wildoff && rem_backslash(path_end))
9942 *p++ = *path_end++;
9943 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9944 {
9945 if (e != NULL)
9946 break;
9947 s = p + 1;
9948 }
9949 else if (path_end >= path + wildoff
9950 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9951 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009952# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (has_mbyte)
9954 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009955 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 STRNCPY(p, path_end, len);
9957 p += len;
9958 path_end += len;
9959 }
9960 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 *p++ = *path_end++;
9963 }
9964 e = p;
9965 *e = NUL;
9966
9967 /* now we have one wildcard component between s and e */
9968 /* Remove backslashes between "wildoff" and the start of the wildcard
9969 * component. */
9970 for (p = buf + wildoff; p < s; ++p)
9971 if (rem_backslash(p))
9972 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009973 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 --e;
9975 --s;
9976 }
9977
Bram Moolenaar231334e2005-07-25 20:46:57 +00009978 /* Check for "**" between "s" and "e". */
9979 for (p = s; p < e; ++p)
9980 if (p[0] == '*' && p[1] == '*')
9981 starstar = TRUE;
9982
Bram Moolenaard82103e2016-01-17 17:04:05 +01009983 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9985 if (pat == NULL)
9986 {
9987 vim_free(buf);
9988 return 0;
9989 }
9990
9991 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009992 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009993 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 regmatch.rm_ic = TRUE; /* Always ignore case */
9995 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009996 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009997 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 vim_free(pat);
9999
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010000 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 {
10002 vim_free(buf);
10003 return 0;
10004 }
10005
10006 /* remember the pattern or file name being looked for */
10007 matchname = vim_strsave(s);
10008
Bram Moolenaar231334e2005-07-25 20:46:57 +000010009 /* If "**" is by itself, this is the first time we encounter it and more
10010 * is following then find matches without any directory. */
10011 if (!didstar && stardepth < 100 && starstar && e - s == 2
10012 && *path_end == '/')
10013 {
10014 STRCPY(s, path_end + 1);
10015 ++stardepth;
10016 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10017 --stardepth;
10018 }
10019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 /* Scan all files in the directory with "dir/ *.*" */
10021 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022# ifdef FEAT_MBYTE
10023 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10024 {
10025 /* The active codepage differs from 'encoding'. Attempt using the
10026 * wide function. If it fails because it is not implemented fall back
10027 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010028 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 if (wn != NULL)
10030 {
10031 hFind = FindFirstFileW(wn, &wfb);
10032 if (hFind == INVALID_HANDLE_VALUE
10033 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
10034 {
10035 vim_free(wn);
10036 wn = NULL;
10037 }
10038 }
10039 }
10040
10041 if (wn == NULL)
10042# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010043 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
10046 while (ok)
10047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048# ifdef FEAT_MBYTE
10049 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010050 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 else
10052# endif
10053 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 /* Ignore entries starting with a dot, unless when asked for. Accept
10055 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010056 if ((p[0] != '.' || starts_with_dot
10057 || ((flags & EW_DODOT)
10058 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010060 || (regmatch.regprog != NULL
10061 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010062 || ((flags & EW_NOTWILD)
10063 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010067
10068 if (starstar && stardepth < 100)
10069 {
10070 /* For "**" in the pattern first go deeper in the tree to
10071 * find matches. */
10072 STRCPY(buf + len, "/**");
10073 STRCPY(buf + len + 3, path_end);
10074 ++stardepth;
10075 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10076 --stardepth;
10077 }
10078
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 STRCPY(buf + len, path_end);
10080 if (mch_has_exp_wildcard(path_end))
10081 {
10082 /* need to expand another component of the path */
10083 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010084 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 }
10086 else
10087 {
10088 /* no more wildcards, check if there is a match */
10089 /* remove backslashes for the remaining components only */
10090 if (*path_end != 0)
10091 backslash_halve(buf + len + 1);
10092 if (mch_getperm(buf) >= 0) /* add existing file */
10093 addfile(gap, buf, flags);
10094 }
10095 }
10096
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097# ifdef FEAT_MBYTE
10098 if (wn != NULL)
10099 {
10100 vim_free(p);
10101 ok = FindNextFileW(hFind, &wfb);
10102 }
10103 else
10104# endif
10105 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
10107 /* If no more matches and no match was used, try expanding the name
10108 * itself. Finds the long name of a short filename. */
10109 if (!ok && matchname != NULL && gap->ga_len == start_len)
10110 {
10111 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 FindClose(hFind);
10113# ifdef FEAT_MBYTE
10114 if (wn != NULL)
10115 {
10116 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010117 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 if (wn != NULL)
10119 hFind = FindFirstFileW(wn, &wfb);
10120 }
10121 if (wn == NULL)
10122# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010123 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 vim_free(matchname);
10126 matchname = NULL;
10127 }
10128 }
10129
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 FindClose(hFind);
10131# ifdef FEAT_MBYTE
10132 vim_free(wn);
10133# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010135 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 vim_free(matchname);
10137
10138 matches = gap->ga_len - start_len;
10139 if (matches > 0)
10140 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10141 sizeof(char_u *), pstrcmp);
10142 return matches;
10143}
10144
10145 int
10146mch_expandpath(
10147 garray_T *gap,
10148 char_u *path,
10149 int flags) /* EW_* flags */
10150{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010151 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010153# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154
Bram Moolenaar231334e2005-07-25 20:46:57 +000010155#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10156 || defined(PROTO)
10157/*
10158 * Unix style wildcard expansion code.
10159 * It's here because it's used both for Unix and Mac.
10160 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010161static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010162
10163 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010164pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010165{
10166 return (pathcmp(*(char **)a, *(char **)b, -1));
10167}
10168
10169/*
10170 * Recursively expand one path component into all matching files and/or
10171 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10172 * "path" has backslashes before chars that are not to be expanded, starting
10173 * at "path + wildoff".
10174 * Return the number of matches found.
10175 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10176 */
10177 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010178unix_expandpath(
10179 garray_T *gap,
10180 char_u *path,
10181 int wildoff,
10182 int flags, /* EW_* flags */
10183 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010184{
10185 char_u *buf;
10186 char_u *path_end;
10187 char_u *p, *s, *e;
10188 int start_len = gap->ga_len;
10189 char_u *pat;
10190 regmatch_T regmatch;
10191 int starts_with_dot;
10192 int matches;
10193 int len;
10194 int starstar = FALSE;
10195 static int stardepth = 0; /* depth for "**" expansion */
10196
10197 DIR *dirp;
10198 struct dirent *dp;
10199
10200 /* Expanding "**" may take a long time, check for CTRL-C. */
10201 if (stardepth > 0)
10202 {
10203 ui_breakcheck();
10204 if (got_int)
10205 return 0;
10206 }
10207
10208 /* make room for file name */
10209 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10210 if (buf == NULL)
10211 return 0;
10212
10213 /*
10214 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010215 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010216 * Copy it into "buf", including the preceding characters.
10217 */
10218 p = buf;
10219 s = buf;
10220 e = NULL;
10221 path_end = path;
10222 while (*path_end != NUL)
10223 {
10224 /* May ignore a wildcard that has a backslash before it; it will
10225 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10226 if (path_end >= path + wildoff && rem_backslash(path_end))
10227 *p++ = *path_end++;
10228 else if (*path_end == '/')
10229 {
10230 if (e != NULL)
10231 break;
10232 s = p + 1;
10233 }
10234 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010235 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010236 || (!p_fic && (flags & EW_ICASE)
10237 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010238 e = p;
10239#ifdef FEAT_MBYTE
10240 if (has_mbyte)
10241 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010242 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010243 STRNCPY(p, path_end, len);
10244 p += len;
10245 path_end += len;
10246 }
10247 else
10248#endif
10249 *p++ = *path_end++;
10250 }
10251 e = p;
10252 *e = NUL;
10253
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010254 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010255 /* Remove backslashes between "wildoff" and the start of the wildcard
10256 * component. */
10257 for (p = buf + wildoff; p < s; ++p)
10258 if (rem_backslash(p))
10259 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010260 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010261 --e;
10262 --s;
10263 }
10264
10265 /* Check for "**" between "s" and "e". */
10266 for (p = s; p < e; ++p)
10267 if (p[0] == '*' && p[1] == '*')
10268 starstar = TRUE;
10269
10270 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010271 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010272 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10273 if (pat == NULL)
10274 {
10275 vim_free(buf);
10276 return 0;
10277 }
10278
10279 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010280 if (flags & EW_ICASE)
10281 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10282 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010283 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010284 if (flags & (EW_NOERROR | EW_NOTWILD))
10285 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010286 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010287 if (flags & (EW_NOERROR | EW_NOTWILD))
10288 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010289 vim_free(pat);
10290
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010291 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010292 {
10293 vim_free(buf);
10294 return 0;
10295 }
10296
10297 /* If "**" is by itself, this is the first time we encounter it and more
10298 * is following then find matches without any directory. */
10299 if (!didstar && stardepth < 100 && starstar && e - s == 2
10300 && *path_end == '/')
10301 {
10302 STRCPY(s, path_end + 1);
10303 ++stardepth;
10304 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10305 --stardepth;
10306 }
10307
10308 /* open the directory for scanning */
10309 *s = NUL;
10310 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10311
10312 /* Find all matching entries */
10313 if (dirp != NULL)
10314 {
10315 for (;;)
10316 {
10317 dp = readdir(dirp);
10318 if (dp == NULL)
10319 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010320 if ((dp->d_name[0] != '.' || starts_with_dot
10321 || ((flags & EW_DODOT)
10322 && dp->d_name[1] != NUL
10323 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010324 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10325 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010326 || ((flags & EW_NOTWILD)
10327 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010328 {
10329 STRCPY(s, dp->d_name);
10330 len = STRLEN(buf);
10331
10332 if (starstar && stardepth < 100)
10333 {
10334 /* For "**" in the pattern first go deeper in the tree to
10335 * find matches. */
10336 STRCPY(buf + len, "/**");
10337 STRCPY(buf + len + 3, path_end);
10338 ++stardepth;
10339 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10340 --stardepth;
10341 }
10342
10343 STRCPY(buf + len, path_end);
10344 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10345 {
10346 /* need to expand another component of the path */
10347 /* remove backslashes for the remaining components only */
10348 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10349 }
10350 else
10351 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010352 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010353
Bram Moolenaar231334e2005-07-25 20:46:57 +000010354 /* no more wildcards, check if there is a match */
10355 /* remove backslashes for the remaining components only */
10356 if (*path_end != NUL)
10357 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010358 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010359 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010360 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010361 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010362#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010363 size_t precomp_len = STRLEN(buf)+1;
10364 char_u *precomp_buf =
10365 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010366
Bram Moolenaar231334e2005-07-25 20:46:57 +000010367 if (precomp_buf)
10368 {
10369 mch_memmove(buf, precomp_buf, precomp_len);
10370 vim_free(precomp_buf);
10371 }
10372#endif
10373 addfile(gap, buf, flags);
10374 }
10375 }
10376 }
10377 }
10378
10379 closedir(dirp);
10380 }
10381
10382 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010383 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010384
10385 matches = gap->ga_len - start_len;
10386 if (matches > 0)
10387 qsort(((char_u **)gap->ga_data) + start_len, matches,
10388 sizeof(char_u *), pstrcmp);
10389 return matches;
10390}
10391#endif
10392
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010393#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010394static int find_previous_pathsep(char_u *path, char_u **psep);
10395static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10396static void expand_path_option(char_u *curdir, garray_T *gap);
10397static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10398static void uniquefy_paths(garray_T *gap, char_u *pattern);
10399static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010400
10401/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010402 * Moves "*psep" back to the previous path separator in "path".
10403 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010404 */
10405 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010406find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010407{
10408 /* skip the current separator */
10409 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010410 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010411
10412 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010413 while (*psep > path)
10414 {
10415 if (vim_ispathsep(**psep))
10416 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010417 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010418 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010419
10420 return FAIL;
10421}
10422
10423/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010424 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10425 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010426 */
10427 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010428is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429{
10430 int j;
10431 int candidate_len;
10432 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010433 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010434 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010435
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010436 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010437 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010438 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010439 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010440
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010441 candidate_len = (int)STRLEN(maybe_unique);
10442 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010443 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010444 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010445
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010446 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010447 if (fnamecmp(maybe_unique, rival) == 0
10448 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010449 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010450 }
10451
Bram Moolenaar162bd912010-07-28 22:29:10 +020010452 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010453}
10454
10455/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010456 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010457 * paths are expanded to their equivalent fullpath. This includes the "."
10458 * (relative to current buffer directory) and empty path (relative to current
10459 * directory) notations.
10460 *
10461 * TODO: handle upward search (;) and path limiter (**N) notations by
10462 * expanding each into their equivalent path(s).
10463 */
10464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010465expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010466{
10467 char_u *path_option = *curbuf->b_p_path == NUL
10468 ? p_path : curbuf->b_p_path;
10469 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010470 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010471 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010472
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010473 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010474 return;
10475
10476 while (*path_option != NUL)
10477 {
10478 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10479
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010480 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010481 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010482 /* Relative to current buffer:
10483 * "/path/file" + "." -> "/path/"
10484 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010485 if (curbuf->b_ffname == NULL)
10486 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010487 p = gettail(curbuf->b_ffname);
10488 len = (int)(p - curbuf->b_ffname);
10489 if (len + (int)STRLEN(buf) >= MAXPATHL)
10490 continue;
10491 if (buf[1] == NUL)
10492 buf[len] = NUL;
10493 else
10494 STRMOVE(buf + len, buf + 2);
10495 mch_memmove(buf, curbuf->b_ffname, len);
10496 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010497 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010498 else if (buf[0] == NUL)
10499 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010500 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010501 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010502 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010503 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010504 else if (!mch_isFullName(buf))
10505 {
10506 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010507 len = (int)STRLEN(curdir);
10508 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010509 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010510 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010511 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010512 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010513 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010514 }
10515
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010516 if (ga_grow(gap, 1) == FAIL)
10517 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010518
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010519# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010520 /* Avoid the path ending in a backslash, it fails when a comma is
10521 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010522 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010523 if (buf[len - 1] == '\\')
10524 buf[len - 1] = '/';
10525# endif
10526
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010527 p = vim_strsave(buf);
10528 if (p == NULL)
10529 break;
10530 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010531 }
10532
10533 vim_free(buf);
10534}
10535
10536/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010537 * Returns a pointer to the file or directory name in "fname" that matches the
10538 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010539 *
10540 * path: /foo/bar/baz
10541 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010542 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010543 */
10544 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010545get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010546{
10547 int i;
10548 int maxlen = 0;
10549 char_u **path_part = (char_u **)gap->ga_data;
10550 char_u *cutoff = NULL;
10551
10552 for (i = 0; i < gap->ga_len; i++)
10553 {
10554 int j = 0;
10555
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010556 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010557# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010558 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10559#endif
10560 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010561 j++;
10562 if (j > maxlen)
10563 {
10564 maxlen = j;
10565 cutoff = &fname[j];
10566 }
10567 }
10568
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010569 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010570 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010571 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010572 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010573
10574 return cutoff;
10575}
10576
10577/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010578 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10579 * that they are unique with respect to each other while conserving the part
10580 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010581 */
10582 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010583uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010584{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010585 int i;
10586 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010587 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010588 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010589 char_u *pat;
10590 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010591 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010592 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010593 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010594 char_u **in_curdir = NULL;
10595 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010596
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010597 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010598 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010599
10600 /*
10601 * We need to prepend a '*' at the beginning of file_pattern so that the
10602 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010603 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010604 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010605 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010606 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010607 if (file_pattern == NULL)
10608 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010609 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010610 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010611 STRCAT(file_pattern, pattern);
10612 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10613 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010614 if (pat == NULL)
10615 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010616
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010617 regmatch.rm_ic = TRUE; /* always ignore case */
10618 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10619 vim_free(pat);
10620 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010621 return;
10622
Bram Moolenaar162bd912010-07-28 22:29:10 +020010623 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010624 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010625 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010626 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010627
10628 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010629 if (in_curdir == NULL)
10630 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010631
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010632 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010633 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010634 char_u *path = fnames[i];
10635 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010636 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010637 char_u *pathsep_p;
10638 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010640 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010641 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010642 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010643 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010644 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010645
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010646 /* Shorten the filename while maintaining its uniqueness */
10647 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010648
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010649 /* Don't assume all files can be reached without path when search
10650 * pattern starts with star star slash, so only remove path_cutoff
10651 * when possible. */
10652 if (pattern[0] == '*' && pattern[1] == '*'
10653 && vim_ispathsep_nocolon(pattern[2])
10654 && path_cutoff != NULL
10655 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10656 && is_unique(path_cutoff, gap, i))
10657 {
10658 sort_again = TRUE;
10659 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10660 }
10661 else
10662 {
10663 /* Here all files can be reached without path, so get shortest
10664 * unique path. We start at the end of the path. */
10665 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010666
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010667 while (find_previous_pathsep(path, &pathsep_p))
10668 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10669 && is_unique(pathsep_p + 1, gap, i)
10670 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10671 {
10672 sort_again = TRUE;
10673 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10674 break;
10675 }
10676 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010677
10678 if (mch_isFullName(path))
10679 {
10680 /*
10681 * Last resort: shorten relative to curdir if possible.
10682 * 'possible' means:
10683 * 1. It is under the current directory.
10684 * 2. The result is actually shorter than the original.
10685 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010686 * Before curdir After
10687 * /foo/bar/file.txt /foo/bar ./file.txt
10688 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10689 * /file.txt / /file.txt
10690 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010691 */
10692 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010693 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010694#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010695 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010696 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010697 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010698 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010699 && !vim_ispathsep(*short_name)
10700#endif
10701 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010702 {
10703 STRCPY(path, ".");
10704 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010705 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010706 }
10707 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010708 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010709 }
10710
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010711 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010712 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010713 {
10714 char_u *rel_path;
10715 char_u *path = in_curdir[i];
10716
10717 if (path == NULL)
10718 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010719
10720 /* If the {filename} is not unique, change it to ./{filename}.
10721 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010722 short_name = shorten_fname(path, curdir);
10723 if (short_name == NULL)
10724 short_name = path;
10725 if (is_unique(short_name, gap, i))
10726 {
10727 STRCPY(fnames[i], short_name);
10728 continue;
10729 }
10730
10731 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10732 if (rel_path == NULL)
10733 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010734 STRCPY(rel_path, ".");
10735 add_pathsep(rel_path);
10736 STRCAT(rel_path, short_name);
10737
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010738 vim_free(fnames[i]);
10739 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010740 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010741 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010742 }
10743
Bram Moolenaar162bd912010-07-28 22:29:10 +020010744theend:
10745 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010746 if (in_curdir != NULL)
10747 {
10748 for (i = 0; i < gap->ga_len; i++)
10749 vim_free(in_curdir[i]);
10750 vim_free(in_curdir);
10751 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010752 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010753 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010754
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010755 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010756 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010757}
10758
10759/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010760 * Calls globpath() with 'path' values for the given pattern and stores the
10761 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010762 * Returns the total number of matches.
10763 */
10764 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010765expand_in_path(
10766 garray_T *gap,
10767 char_u *pattern,
10768 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010769{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010770 char_u *curdir;
10771 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010772 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010773 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010774
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010775 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010776 return 0;
10777 mch_dirname(curdir, MAXPATHL);
10778
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010779 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010780 expand_path_option(curdir, &path_ga);
10781 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010782 if (path_ga.ga_len == 0)
10783 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010784
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010785 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010786 ga_clear_strings(&path_ga);
10787 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010788 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010789
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010790 if (flags & EW_ICASE)
10791 glob_flags |= WILD_ICASE;
10792 if (flags & EW_ADDSLASH)
10793 glob_flags |= WILD_ADD_SLASH;
10794 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010795 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010796
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010797 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010798}
10799#endif
10800
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010801#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10802/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010803 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10804 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010805 */
10806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010807remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010808{
10809 int i;
10810 int j;
10811 char_u **fnames = (char_u **)gap->ga_data;
10812
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010813 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010814 for (i = gap->ga_len - 1; i > 0; --i)
10815 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10816 {
10817 vim_free(fnames[i]);
10818 for (j = i + 1; j < gap->ga_len; ++j)
10819 fnames[j - 1] = fnames[j];
10820 --gap->ga_len;
10821 }
10822}
10823#endif
10824
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010825static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010826
10827/*
10828 * Return TRUE if "p" contains what looks like an environment variable.
10829 * Allowing for escaping.
10830 */
10831 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010832has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010833{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010834 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010835 {
10836 if (*p == '\\' && p[1] != NUL)
10837 ++p;
10838 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010839#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010840 "$%"
10841#else
10842 "$"
10843#endif
10844 , *p) != NULL)
10845 return TRUE;
10846 }
10847 return FALSE;
10848}
10849
10850#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010851static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010852
10853/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010854 * Return TRUE if "p" contains a special wildcard character, one that Vim
10855 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010856 */
10857 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010858has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010859{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010860 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010861 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010862 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010863 if (*p == '\\' && p[1] != NUL)
10864 ++p;
10865 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10866 return TRUE;
10867 }
10868 return FALSE;
10869}
10870#endif
10871
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872/*
10873 * Generic wildcard expansion code.
10874 *
10875 * Characters in "pat" that should not be expanded must be preceded with a
10876 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10877 *
10878 * Return FAIL when no single file was found. In this case "num_file" is not
10879 * set, and "file" may contain an error message.
10880 * Return OK when some files found. "num_file" is set to the number of
10881 * matches, "file" to the array of matches. Call FreeWild() later.
10882 */
10883 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010884gen_expand_wildcards(
10885 int num_pat, /* number of input patterns */
10886 char_u **pat, /* array of input patterns */
10887 int *num_file, /* resulting number of files */
10888 char_u ***file, /* array of resulting files */
10889 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890{
10891 int i;
10892 garray_T ga;
10893 char_u *p;
10894 static int recursive = FALSE;
10895 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010896 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010897#if defined(FEAT_SEARCHPATH)
10898 int did_expand_in_path = FALSE;
10899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900
10901 /*
10902 * expand_env() is called to expand things like "~user". If this fails,
10903 * it calls ExpandOne(), which brings us back here. In this case, always
10904 * call the machine specific expansion function, if possible. Otherwise,
10905 * return FAIL.
10906 */
10907 if (recursive)
10908#ifdef SPECIAL_WILDCHAR
10909 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10910#else
10911 return FAIL;
10912#endif
10913
10914#ifdef SPECIAL_WILDCHAR
10915 /*
10916 * If there are any special wildcard characters which we cannot handle
10917 * here, call machine specific function for all the expansion. This
10918 * avoids starting the shell for each argument separately.
10919 * For `=expr` do use the internal function.
10920 */
10921 for (i = 0; i < num_pat; i++)
10922 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010923 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924# ifdef VIM_BACKTICK
10925 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10926# endif
10927 )
10928 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10929 }
10930#endif
10931
10932 recursive = TRUE;
10933
10934 /*
10935 * The matching file names are stored in a growarray. Init it empty.
10936 */
10937 ga_init2(&ga, (int)sizeof(char_u *), 30);
10938
10939 for (i = 0; i < num_pat; ++i)
10940 {
10941 add_pat = -1;
10942 p = pat[i];
10943
10944#ifdef VIM_BACKTICK
10945 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010948 if (add_pat == -1)
10949 retval = FAIL;
10950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 else
10952#endif
10953 {
10954 /*
10955 * First expand environment variables, "~/" and "~user/".
10956 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010957 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010959 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 if (p == NULL)
10961 p = pat[i];
10962#ifdef UNIX
10963 /*
10964 * On Unix, if expand_env() can't expand an environment
10965 * variable, use the shell to do that. Discard previously
10966 * found file names and start all over again.
10967 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010968 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 {
10970 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010971 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010973 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 recursive = FALSE;
10975 return i;
10976 }
10977#endif
10978 }
10979
10980 /*
10981 * If there are wildcards: Expand file names and add each match to
10982 * the list. If there is no match, and EW_NOTFOUND is given, add
10983 * the pattern.
10984 * If there are no wildcards: Add the file name if it exists or
10985 * when EW_NOTFOUND is given.
10986 */
10987 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010988 {
10989#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010990 if ((flags & EW_PATH)
10991 && !mch_isFullName(p)
10992 && !(p[0] == '.'
10993 && (vim_ispathsep(p[1])
10994 || (p[1] == '.' && vim_ispathsep(p[2]))))
10995 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010996 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010997 /* :find completion where 'path' is used.
10998 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010999 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011000 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011001 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011002 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011003 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011004 else
11005#endif
11006 add_pat = mch_expandpath(&ga, p, flags);
11007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 }
11009
11010 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11011 {
11012 char_u *t = backslash_halve_save(p);
11013
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11015 * "vim c:/" work. */
11016 if (flags & EW_NOTFOUND)
11017 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 addfile(&ga, t, flags);
11020 vim_free(t);
11021 }
11022
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011023#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011024 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011025 uniquefy_paths(&ga, p);
11026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 if (p != pat[i])
11028 vim_free(p);
11029 }
11030
11031 *num_file = ga.ga_len;
11032 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11033
11034 recursive = FALSE;
11035
Bram Moolenaar336bd622016-01-17 18:23:58 +010011036 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037}
11038
11039# ifdef VIM_BACKTICK
11040
11041/*
11042 * Return TRUE if we can expand this backtick thing here.
11043 */
11044 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011045vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046{
11047 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11048}
11049
11050/*
11051 * Expand an item in `backticks` by executing it as a command.
11052 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011053 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 */
11055 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011056expand_backtick(
11057 garray_T *gap,
11058 char_u *pat,
11059 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061 char_u *p;
11062 char_u *cmd;
11063 char_u *buffer;
11064 int cnt = 0;
11065 int i;
11066
11067 /* Create the command: lop off the backticks. */
11068 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11069 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011070 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071
11072#ifdef FEAT_EVAL
11073 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011074 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 else
11076#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011077 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011078 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 vim_free(cmd);
11080 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011081 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082
11083 cmd = buffer;
11084 while (*cmd != NUL)
11085 {
11086 cmd = skipwhite(cmd); /* skip over white space */
11087 p = cmd;
11088 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11089 ++p;
11090 /* add an entry if it is not empty */
11091 if (p > cmd)
11092 {
11093 i = *p;
11094 *p = NUL;
11095 addfile(gap, cmd, flags);
11096 *p = i;
11097 ++cnt;
11098 }
11099 cmd = p;
11100 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11101 ++cmd;
11102 }
11103
11104 vim_free(buffer);
11105 return cnt;
11106}
11107# endif /* VIM_BACKTICK */
11108
11109/*
11110 * Add a file to a file list. Accepted flags:
11111 * EW_DIR add directories
11112 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011113 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 * EW_NOTFOUND add even when it doesn't exist
11115 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011116 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 */
11118 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011119addfile(
11120 garray_T *gap,
11121 char_u *f, /* filename */
11122 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123{
11124 char_u *p;
11125 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011126 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011128 /* if the file/dir/link doesn't exist, may not add it */
11129 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011130 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 return;
11132
11133#ifdef FNAME_ILLEGAL
11134 /* if the file/dir contains illegal characters, don't add it */
11135 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11136 return;
11137#endif
11138
11139 isdir = mch_isdir(f);
11140 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11141 return;
11142
Bram Moolenaarb5971142015-03-21 17:32:19 +010011143 /* If the file isn't executable, may not add it. Do accept directories.
11144 * When invoked from expand_shellcmd() do not use $PATH. */
11145 if (!isdir && (flags & EW_EXEC)
11146 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011147 return;
11148
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 /* Make room for another item in the file list. */
11150 if (ga_grow(gap, 1) == FAIL)
11151 return;
11152
11153 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11154 if (p == NULL)
11155 return;
11156
11157 STRCPY(p, f);
11158#ifdef BACKSLASH_IN_FILENAME
11159 slash_adjust(p);
11160#endif
11161 /*
11162 * Append a slash or backslash after directory names if none is present.
11163 */
11164#ifndef DONT_ADD_PATHSEP_TO_DIR
11165 if (isdir && (flags & EW_ADDSLASH))
11166 add_pathsep(p);
11167#endif
11168 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169}
11170#endif /* !NO_EXPANDPATH */
11171
11172#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11173
11174#ifndef SEEK_SET
11175# define SEEK_SET 0
11176#endif
11177#ifndef SEEK_END
11178# define SEEK_END 2
11179#endif
11180
11181/*
11182 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011183 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11184 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 * Returns an allocated string, or NULL for error.
11186 */
11187 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011188get_cmd_output(
11189 char_u *cmd,
11190 char_u *infile, /* optional input file name */
11191 int flags, /* can be SHELL_SILENT */
11192 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193{
11194 char_u *tempname;
11195 char_u *command;
11196 char_u *buffer = NULL;
11197 int len;
11198 int i = 0;
11199 FILE *fd;
11200
11201 if (check_restricted() || check_secure())
11202 return NULL;
11203
11204 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011205 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206 {
11207 EMSG(_(e_notmp));
11208 return NULL;
11209 }
11210
11211 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011212 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 if (command == NULL)
11214 goto done;
11215
11216 /*
11217 * Call the shell to execute the command (errors are ignored).
11218 * Don't check timestamps here.
11219 */
11220 ++no_check_timestamps;
11221 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11222 --no_check_timestamps;
11223
11224 vim_free(command);
11225
11226 /*
11227 * read the names from the file into memory
11228 */
11229# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011230 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 fd = mch_fopen((char *)tempname, "r");
11232# else
11233 fd = mch_fopen((char *)tempname, READBIN);
11234# endif
11235
11236 if (fd == NULL)
11237 {
11238 EMSG2(_(e_notopen), tempname);
11239 goto done;
11240 }
11241
11242 fseek(fd, 0L, SEEK_END);
11243 len = ftell(fd); /* get size of temp file */
11244 fseek(fd, 0L, SEEK_SET);
11245
11246 buffer = alloc(len + 1);
11247 if (buffer != NULL)
11248 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11249 fclose(fd);
11250 mch_remove(tempname);
11251 if (buffer == NULL)
11252 goto done;
11253#ifdef VMS
11254 len = i; /* VMS doesn't give us what we asked for... */
11255#endif
11256 if (i != len)
11257 {
11258 EMSG2(_(e_notread), tempname);
11259 vim_free(buffer);
11260 buffer = NULL;
11261 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011262 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011263 {
11264 /* Change NUL into SOH, otherwise the string is truncated. */
11265 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011266 if (buffer[i] == NUL)
11267 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011268
Bram Moolenaar162bd912010-07-28 22:29:10 +020011269 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011270 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011271 else
11272 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273
11274done:
11275 vim_free(tempname);
11276 return buffer;
11277}
11278#endif
11279
11280/*
11281 * Free the list of files returned by expand_wildcards() or other expansion
11282 * functions.
11283 */
11284 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011285FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011287 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 while (count--)
11290 vim_free(files[count]);
11291 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292}
11293
11294/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011295 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 * Don't do this when still processing a command or a mapping.
11297 * Don't do this when inside a ":normal" command.
11298 */
11299 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011300goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301{
11302 return (p_im && stuff_empty() && typebuf_typed());
11303}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011304
11305/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011306 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011307 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11308 * - Remove any argument. E.g., "csh -f" -> "csh".
11309 * But don't allow a space in the path, so that this works:
11310 * "/usr/bin/csh --rcfile ~/.cshrc"
11311 * But don't do that for Windows, it's common to have a space in the path.
11312 */
11313 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011314get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011315{
11316 char_u *p;
11317
11318#ifdef WIN3264
11319 p = gettail(p_sh);
11320 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11321#else
11322 p = skiptowhite(p_sh);
11323 if (*p == NUL)
11324 {
11325 /* No white space, use the tail. */
11326 p = vim_strsave(gettail(p_sh));
11327 }
11328 else
11329 {
11330 char_u *p1, *p2;
11331
11332 /* Find the last path separator before the space. */
11333 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011334 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011335 if (vim_ispathsep(*p2))
11336 p1 = p2 + 1;
11337 p = vim_strnsave(p1, (int)(p - p1));
11338 }
11339#endif
11340 return p;
11341}