blob: 369b716ad5625d2014322fde691fcdc3457f669b [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 */
498 const int eff_wwidth = W_WIDTH(wp)
499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100505 || prev_tick != CHANGEDTICK(wp->w_buffer))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200511 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200513 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514
515 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 if (wp->w_p_brisbr)
517 bri -= vim_strsize(p_sbr);
518
519 /* Add offset for number column, if 'n' is in 'cpoptions' */
520 bri += win_col_off2(wp);
521
522 /* never indent past left window margin */
523 if (bri < 0)
524 bri = 0;
525 /* always leave at least bri_min characters on the left,
526 * if text width is sufficient */
527 else if (bri > eff_wwidth - wp->w_p_brimin)
528 bri = (eff_wwidth - wp->w_p_brimin < 0)
529 ? 0 : eff_wwidth - wp->w_p_brimin;
530
531 return bri;
532}
533#endif
534
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
537
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100538static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
541 * Return TRUE if the string "line" starts with a word from 'cinwords'.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *cinw;
547 char_u *cinw_buf;
548 int cinw_len;
549 int retval = FALSE;
550 int len;
551
552 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
553 cinw_buf = alloc((unsigned)cinw_len);
554 if (cinw_buf != NULL)
555 {
556 line = skipwhite(line);
557 for (cinw = curbuf->b_p_cinw; *cinw; )
558 {
559 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
560 if (STRNCMP(line, cinw_buf, len) == 0
561 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
562 {
563 retval = TRUE;
564 break;
565 }
566 }
567 vim_free(cinw_buf);
568 }
569 return retval;
570}
571#endif
572
573/*
574 * open_line: Add a new line below or above the current line.
575 *
576 * For VREPLACE mode, we only add a new line when we get to the end of the
577 * file, otherwise we just start replacing the next line.
578 *
579 * Caller must take care of undo. Since VREPLACE may affect any number of
580 * lines however, it may call u_save_cursor() again when starting to change a
581 * new line.
582 * "flags": OPENLINE_DELSPACES delete spaces after cursor
583 * OPENLINE_DO_COM format comments
584 * OPENLINE_KEEPTRAIL keep trailing spaces
585 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200586 * OPENLINE_COM_LIST format comments with list or 2nd line indent
587 *
588 * "second_line_indent": indent for after ^^D in Insert mode or if flag
589 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *
591 * Return TRUE for success, FALSE for failure
592 */
593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594open_line(
595 int dir, /* FORWARD or BACKWARD */
596 int flags,
597 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 char_u *saved_line; /* copy of the original line */
600 char_u *next_line = NULL; /* copy of the next line */
601 char_u *p_extra = NULL; /* what goes to next line */
602 int less_cols = 0; /* less columns for mark in new line */
603 int less_cols_off = 0; /* columns to skip for mark adjust */
604 pos_T old_cursor; /* old cursor position */
605 int newcol = 0; /* new cursor column */
606 int newindent = 0; /* auto-indent of the new line */
607 int n;
608 int trunc_line = FALSE; /* truncate current line afterwards */
609 int retval = FALSE; /* return value, default is FAIL */
610#ifdef FEAT_COMMENTS
611 int extra_len = 0; /* length of p_extra string */
612 int lead_len; /* length of comment leader */
613 char_u *lead_flags; /* position in 'comments' for comment leader */
614 char_u *leader = NULL; /* copy of comment leader */
615#endif
616 char_u *allocated = NULL; /* allocated memory */
617#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
618 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
619 char_u *p;
620#endif
621 int saved_char = NUL; /* init for GCC */
622#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
623 pos_T *pos;
624#endif
625#ifdef FEAT_SMARTINDENT
626 int do_si = (!p_paste && curbuf->b_p_si
627# ifdef FEAT_CINDENT
628 && !curbuf->b_p_cin
629# endif
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 */
2029 width = W_WIDTH(wp) - win_col_off(wp);
2030 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 */
2086 width = W_WIDTH(wp) - 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;
2316 mch_memmove(p + newlen, oldp + col + oldlen,
2317 (size_t)(linelen - col - oldlen));
2318
2319 /* Insert or overwrite the new character. */
2320#ifdef FEAT_MBYTE
2321 mch_memmove(p, buf, charlen);
2322 i = charlen;
2323#else
2324 *p = c;
2325 i = 1;
2326#endif
2327
2328 /* Fill with spaces when necessary. */
2329 while (i < newlen)
2330 p[i++] = ' ';
2331
2332 /* Replace the line in the buffer. */
2333 ml_replace(lnum, newp, FALSE);
2334
2335 /* mark the buffer as changed and prepare for displaying */
2336 changed_bytes(lnum, col);
2337
2338 /*
2339 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2340 * show the match for right parens and braces.
2341 */
2342 if (p_sm && (State & INSERT)
2343 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002344#ifdef FEAT_INS_EXPAND
2345 && !ins_compl_active()
2346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002348 {
2349#ifdef FEAT_MBYTE
2350 if (has_mbyte)
2351 showmatch(mb_ptr2char(buf));
2352 else
2353#endif
2354 showmatch(c);
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357#ifdef FEAT_RIGHTLEFT
2358 if (!p_ri || (State & REPLACE_FLAG))
2359#endif
2360 {
2361 /* Normal insert: move cursor right */
2362#ifdef FEAT_MBYTE
2363 curwin->w_cursor.col += charlen;
2364#else
2365 ++curwin->w_cursor.col;
2366#endif
2367 }
2368 /*
2369 * TODO: should try to update w_row here, to avoid recomputing it later.
2370 */
2371}
2372
2373/*
2374 * Insert a string at the cursor position.
2375 * Note: Does NOT handle Replace mode.
2376 * Caller must have prepared for undo.
2377 */
2378 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002379ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 char_u *oldp, *newp;
2382 int newlen = (int)STRLEN(s);
2383 int oldlen;
2384 colnr_T col;
2385 linenr_T lnum = curwin->w_cursor.lnum;
2386
2387#ifdef FEAT_VIRTUALEDIT
2388 if (virtual_active() && curwin->w_cursor.coladd > 0)
2389 coladvance_force(getviscol());
2390#endif
2391
2392 col = curwin->w_cursor.col;
2393 oldp = ml_get(lnum);
2394 oldlen = (int)STRLEN(oldp);
2395
2396 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2397 if (newp == NULL)
2398 return;
2399 if (col > 0)
2400 mch_memmove(newp, oldp, (size_t)col);
2401 mch_memmove(newp + col, s, (size_t)newlen);
2402 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2403 ml_replace(lnum, newp, FALSE);
2404 changed_bytes(lnum, col);
2405 curwin->w_cursor.col += newlen;
2406}
2407
2408/*
2409 * Delete one character under the cursor.
2410 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2411 * Caller must have prepared for undo.
2412 *
2413 * return FAIL for failure, OK otherwise
2414 */
2415 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002416del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417{
2418#ifdef FEAT_MBYTE
2419 if (has_mbyte)
2420 {
2421 /* Make sure the cursor is at the start of a character. */
2422 mb_adjust_cursor();
2423 if (*ml_get_cursor() == NUL)
2424 return FAIL;
2425 return del_chars(1L, fixpos);
2426 }
2427#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002428 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429}
2430
2431#if defined(FEAT_MBYTE) || defined(PROTO)
2432/*
2433 * Like del_bytes(), but delete characters instead of bytes.
2434 */
2435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002436del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 long bytes = 0;
2439 long i;
2440 char_u *p;
2441 int l;
2442
2443 p = ml_get_cursor();
2444 for (i = 0; i < count && *p != NUL; ++i)
2445 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002446 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 bytes += l;
2448 p += l;
2449 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002450 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451}
2452#endif
2453
2454/*
2455 * Delete "count" bytes under the cursor.
2456 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2457 * Caller must have prepared for undo.
2458 *
2459 * return FAIL for failure, OK otherwise
2460 */
2461 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002462del_bytes(
2463 long count,
2464 int fixpos_arg,
2465 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 char_u *oldp, *newp;
2468 colnr_T oldlen;
2469 linenr_T lnum = curwin->w_cursor.lnum;
2470 colnr_T col = curwin->w_cursor.col;
2471 int was_alloced;
2472 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002473 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 oldp = ml_get(lnum);
2476 oldlen = (int)STRLEN(oldp);
2477
2478 /*
2479 * Can't do anything when the cursor is on the NUL after the line.
2480 */
2481 if (col >= oldlen)
2482 return FAIL;
2483
2484#ifdef FEAT_MBYTE
2485 /* If 'delcombine' is set and deleting (less than) one character, only
2486 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002487 if (p_deco && use_delcombine && enc_utf8
2488 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002490 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 int n;
2492
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002493 (void)utfc_ptr2char(oldp + col, cc);
2494 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 /* Find the last composing char, there can be several. */
2497 n = col;
2498 do
2499 {
2500 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002501 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 n += count;
2503 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2504 fixpos = 0;
2505 }
2506 }
2507#endif
2508
2509 /*
2510 * When count is too big, reduce it.
2511 */
2512 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2513 if (movelen <= 1)
2514 {
2515 /*
2516 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002517 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2518 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002520 if (col > 0 && fixpos && restart_edit == 0
2521#ifdef FEAT_VIRTUALEDIT
2522 && (ve_flags & VE_ONEMORE) == 0
2523#endif
2524 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 --curwin->w_cursor.col;
2527#ifdef FEAT_VIRTUALEDIT
2528 curwin->w_cursor.coladd = 0;
2529#endif
2530#ifdef FEAT_MBYTE
2531 if (has_mbyte)
2532 curwin->w_cursor.col -=
2533 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2534#endif
2535 }
2536 count = oldlen - col;
2537 movelen = 1;
2538 }
2539
2540 /*
2541 * If the old line has been allocated the deletion can be done in the
2542 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002543 * Can't do this when using Netbeans, because we would need to invoke
2544 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002545 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002548 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002549 was_alloced = FALSE;
2550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002552 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (was_alloced)
2554 newp = oldp; /* use same allocated memory */
2555 else
2556 { /* need to allocate a new line */
2557 newp = alloc((unsigned)(oldlen + 1 - count));
2558 if (newp == NULL)
2559 return FAIL;
2560 mch_memmove(newp, oldp, (size_t)col);
2561 }
2562 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2563 if (!was_alloced)
2564 ml_replace(lnum, newp, FALSE);
2565
2566 /* mark the buffer as changed and prepare for displaying */
2567 changed_bytes(lnum, curwin->w_cursor.col);
2568
2569 return OK;
2570}
2571
2572/*
2573 * Delete from cursor to end of line.
2574 * Caller must have prepared for undo.
2575 *
2576 * return FAIL for failure, OK otherwise
2577 */
2578 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002579truncate_line(
2580 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 char_u *newp;
2583 linenr_T lnum = curwin->w_cursor.lnum;
2584 colnr_T col = curwin->w_cursor.col;
2585
2586 if (col == 0)
2587 newp = vim_strsave((char_u *)"");
2588 else
2589 newp = vim_strnsave(ml_get(lnum), col);
2590
2591 if (newp == NULL)
2592 return FAIL;
2593
2594 ml_replace(lnum, newp, FALSE);
2595
2596 /* mark the buffer as changed and prepare for displaying */
2597 changed_bytes(lnum, curwin->w_cursor.col);
2598
2599 /*
2600 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2601 */
2602 if (fixpos && curwin->w_cursor.col > 0)
2603 --curwin->w_cursor.col;
2604
2605 return OK;
2606}
2607
2608/*
2609 * Delete "nlines" lines at the cursor.
2610 * Saves the lines for undo first if "undo" is TRUE.
2611 */
2612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002613del_lines(
2614 long nlines, /* number of lines to delete */
2615 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002618 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 if (nlines <= 0)
2621 return;
2622
2623 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002624 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return;
2626
2627 for (n = 0; n < nlines; )
2628 {
2629 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2630 break;
2631
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002632 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 ++n;
2634
2635 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002636 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 break;
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002640 /* Correct the cursor position before calling deleted_lines_mark(), it may
2641 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 curwin->w_cursor.col = 0;
2643 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002644
2645 /* adjust marks, mark the buffer as changed and prepare for displaying */
2646 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647}
2648
2649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002650gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 char_u *ptr = ml_get_pos(pos);
2653
2654#ifdef FEAT_MBYTE
2655 if (has_mbyte)
2656 return (*mb_ptr2char)(ptr);
2657#endif
2658 return (int)*ptr;
2659}
2660
2661 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002662gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663{
2664#ifdef FEAT_MBYTE
2665 if (has_mbyte)
2666 return (*mb_ptr2char)(ml_get_cursor());
2667#endif
2668 return (int)*ml_get_cursor();
2669}
2670
2671/*
2672 * Write a character at the current cursor position.
2673 * It is directly written into the block.
2674 */
2675 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002676pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2679 + curwin->w_cursor.col) = c;
2680}
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682/*
2683 * When extra == 0: Return TRUE if the cursor is before or on the first
2684 * non-blank in the line.
2685 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2686 * the line.
2687 */
2688 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002689inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 char_u *ptr;
2692 colnr_T col;
2693
Bram Moolenaar1c465442017-03-12 20:10:05 +01002694 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 ++ptr;
2696 if (col >= curwin->w_cursor.col + extra)
2697 return TRUE;
2698 else
2699 return FALSE;
2700}
2701
2702/*
2703 * Skip to next part of an option argument: Skip space and comma.
2704 */
2705 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002706skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 if (*p == ',')
2709 ++p;
2710 while (*p == ' ')
2711 ++p;
2712 return p;
2713}
2714
2715/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002716 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 *
2718 * Most often called through changed_bytes() and changed_lines(), which also
2719 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002720 *
2721 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 */
2723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002724changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002727 if (p_imst == IM_ON_THE_SPOT)
2728 {
2729 /* The text of the preediting area is inserted, but this doesn't
2730 * mean a change of the buffer yet. That is delayed until the
2731 * text is committed. (this means preedit becomes empty) */
2732 if (im_is_preediting() && !xim_changed_while_preediting)
2733 return;
2734 xim_changed_while_preediting = FALSE;
2735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#endif
2737
2738 if (!curbuf->b_changed)
2739 {
2740 int save_msg_scroll = msg_scroll;
2741
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002742 /* Give a warning about changing a read-only file. This may also
2743 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 /* Create a swap file if that is wanted.
2747 * Don't do this for "nofile" and "nowrite" buffer types. */
2748 if (curbuf->b_may_swap
2749#ifdef FEAT_QUICKFIX
2750 && !bt_dontwrite(curbuf)
2751#endif
2752 )
2753 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002754 int save_need_wait_return = need_wait_return;
2755
2756 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 ml_open_file(curbuf);
2758
2759 /* The ml_open_file() can cause an ATTENTION message.
2760 * Wait two seconds, to make sure the user reads this unexpected
2761 * message. Since we could be anywhere, call wait_return() now,
2762 * and don't let the emsg() set msg_scroll. */
2763 if (need_wait_return && emsg_silent == 0)
2764 {
2765 out_flush();
2766 ui_delay(2000L, TRUE);
2767 wait_return(TRUE);
2768 msg_scroll = save_msg_scroll;
2769 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002770 else
2771 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002773 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002775 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776}
2777
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002778/*
2779 * Internal part of changed(), no user interaction.
2780 */
2781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002782changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002783{
2784 curbuf->b_changed = TRUE;
2785 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002786 check_status(curbuf);
2787 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002788#ifdef FEAT_TITLE
2789 need_maketitle = TRUE; /* set window title later */
2790#endif
2791}
2792
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002793static void changedOneline(buf_T *buf, linenr_T lnum);
2794static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2795static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797/*
2798 * Changed bytes within a single line for the current buffer.
2799 * - marks the windows on this buffer to be redisplayed
2800 * - marks the buffer changed by calling changed()
2801 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002802 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002805changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002809
2810#ifdef FEAT_DIFF
2811 /* Diff highlighting in other diff windows may need to be updated too. */
2812 if (curwin->w_p_diff)
2813 {
2814 win_T *wp;
2815 linenr_T wlnum;
2816
Bram Moolenaar29323592016-07-24 22:04:11 +02002817 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002818 if (wp->w_p_diff && wp != curwin)
2819 {
2820 redraw_win_later(wp, VALID);
2821 wlnum = diff_lnum_win(lnum, wp);
2822 if (wlnum > 0)
2823 changedOneline(wp->w_buffer, wlnum);
2824 }
2825 }
2826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827}
2828
2829 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002830changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002832 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002835 if (lnum < buf->b_mod_top)
2836 buf->b_mod_top = lnum;
2837 else if (lnum >= buf->b_mod_bot)
2838 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 else
2841 {
2842 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002843 buf->b_mod_set = TRUE;
2844 buf->b_mod_top = lnum;
2845 buf->b_mod_bot = lnum + 1;
2846 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848}
2849
2850/*
2851 * Appended "count" lines below line "lnum" in the current buffer.
2852 * Must be called AFTER the change and after mark_adjust().
2853 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2854 */
2855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002856appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 changed_lines(lnum + 1, 0, lnum + 1, count);
2859}
2860
2861/*
2862 * Like appended_lines(), but adjust marks first.
2863 */
2864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002865appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002867 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002868 * be marks there. But it's still needed in diff mode. */
2869 if (lnum + count < curbuf->b_ml.ml_line_count
2870#ifdef FEAT_DIFF
2871 || curwin->w_p_diff
2872#endif
2873 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002874 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 changed_lines(lnum + 1, 0, lnum + 1, count);
2876}
2877
2878/*
2879 * Deleted "count" lines at line "lnum" in the current buffer.
2880 * Must be called AFTER the change and after mark_adjust().
2881 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2882 */
2883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002884deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 changed_lines(lnum, 0, lnum + count, -count);
2887}
2888
2889/*
2890 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002891 * Make sure the cursor is on a valid line before calling, a GUI callback may
2892 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 */
2894 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002895deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2898 changed_lines(lnum, 0, lnum + count, -count);
2899}
2900
2901/*
2902 * Changed lines for the current buffer.
2903 * Must be called AFTER the change and after mark_adjust().
2904 * - mark the buffer changed by calling changed()
2905 * - mark the windows on this buffer to be redisplayed
2906 * - invalidate cached values
2907 * "lnum" is the first line that needs displaying, "lnume" the first line
2908 * below the changed lines (BEFORE the change).
2909 * When only inserting lines, "lnum" and "lnume" are equal.
2910 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002911 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 */
2913 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002914changed_lines(
2915 linenr_T lnum, /* first line with change */
2916 colnr_T col, /* column in first line with change */
2917 linenr_T lnume, /* line below last changed line */
2918 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002920 changed_lines_buf(curbuf, lnum, lnume, xtra);
2921
2922#ifdef FEAT_DIFF
2923 if (xtra == 0 && curwin->w_p_diff)
2924 {
2925 /* When the number of lines doesn't change then mark_adjust() isn't
2926 * called and other diff buffers still need to be marked for
2927 * displaying. */
2928 win_T *wp;
2929 linenr_T wlnum;
2930
Bram Moolenaar29323592016-07-24 22:04:11 +02002931 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002932 if (wp->w_p_diff && wp != curwin)
2933 {
2934 redraw_win_later(wp, VALID);
2935 wlnum = diff_lnum_win(lnum, wp);
2936 if (wlnum > 0)
2937 changed_lines_buf(wp->w_buffer, wlnum,
2938 lnume - lnum + wlnum, 0L);
2939 }
2940 }
2941#endif
2942
2943 changed_common(lnum, col, lnume, xtra);
2944}
2945
2946 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002947changed_lines_buf(
2948 buf_T *buf,
2949 linenr_T lnum, /* first line with change */
2950 linenr_T lnume, /* line below last changed line */
2951 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002952{
2953 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
2955 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002956 if (lnum < buf->b_mod_top)
2957 buf->b_mod_top = lnum;
2958 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
2960 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002961 buf->b_mod_bot += xtra;
2962 if (buf->b_mod_bot < lnum)
2963 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002965 if (lnume + xtra > buf->b_mod_bot)
2966 buf->b_mod_bot = lnume + xtra;
2967 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
2969 else
2970 {
2971 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002972 buf->b_mod_set = TRUE;
2973 buf->b_mod_top = lnum;
2974 buf->b_mod_bot = lnume + xtra;
2975 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977}
2978
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002979/*
2980 * Common code for when a change is was made.
2981 * See changed_lines() for the arguments.
2982 * Careful: may trigger autocommands that reload the buffer.
2983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002985changed_common(
2986 linenr_T lnum,
2987 colnr_T col,
2988 linenr_T lnume,
2989 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002992 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 int i;
2994#ifdef FEAT_JUMPLIST
2995 int cols;
2996 pos_T *p;
2997 int add;
2998#endif
2999
3000 /* mark the buffer as modified */
3001 changed();
3002
3003 /* set the '. mark */
3004 if (!cmdmod.keepjumps)
3005 {
3006 curbuf->b_last_change.lnum = lnum;
3007 curbuf->b_last_change.col = col;
3008
3009#ifdef FEAT_JUMPLIST
3010 /* Create a new entry if a new undo-able change was started or we
3011 * don't have an entry yet. */
3012 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3013 {
3014 if (curbuf->b_changelistlen == 0)
3015 add = TRUE;
3016 else
3017 {
3018 /* Don't create a new entry when the line number is the same
3019 * as the last one and the column is not too far away. Avoids
3020 * creating many entries for typing "xxxxx". */
3021 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3022 if (p->lnum != lnum)
3023 add = TRUE;
3024 else
3025 {
3026 cols = comp_textwidth(FALSE);
3027 if (cols == 0)
3028 cols = 79;
3029 add = (p->col + cols < col || col + cols < p->col);
3030 }
3031 }
3032 if (add)
3033 {
3034 /* This is the first of a new sequence of undo-able changes
3035 * and it's at some distance of the last change. Use a new
3036 * position in the changelist. */
3037 curbuf->b_new_change = FALSE;
3038
3039 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3040 {
3041 /* changelist is full: remove oldest entry */
3042 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3043 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3044 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003045 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
3047 /* Correct position in changelist for other windows on
3048 * this buffer. */
3049 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3050 --wp->w_changelistidx;
3051 }
3052 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003053 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
3055 /* For other windows, if the position in the changelist is
3056 * at the end it stays at the end. */
3057 if (wp->w_buffer == curbuf
3058 && wp->w_changelistidx == curbuf->b_changelistlen)
3059 ++wp->w_changelistidx;
3060 }
3061 ++curbuf->b_changelistlen;
3062 }
3063 }
3064 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3065 curbuf->b_last_change;
3066 /* The current window is always after the last change, so that "g,"
3067 * takes you back to it. */
3068 curwin->w_changelistidx = curbuf->b_changelistlen;
3069#endif
3070 }
3071
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003072 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
3074 if (wp->w_buffer == curbuf)
3075 {
3076 /* Mark this window to be redrawn later. */
3077 if (wp->w_redr_type < VALID)
3078 wp->w_redr_type = VALID;
3079
3080 /* Check if a change in the buffer has invalidated the cached
3081 * values for the cursor. */
3082#ifdef FEAT_FOLDING
3083 /*
3084 * Update the folds for this window. Can't postpone this, because
3085 * a following operator might work on the whole fold: ">>dd".
3086 */
3087 foldUpdate(wp, lnum, lnume + xtra - 1);
3088
3089 /* The change may cause lines above or below the change to become
3090 * included in a fold. Set lnum/lnume to the first/last line that
3091 * might be displayed differently.
3092 * Set w_cline_folded here as an efficient way to update it when
3093 * inserting lines just above a closed fold. */
3094 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3095 if (wp->w_cursor.lnum == lnum)
3096 wp->w_cline_folded = i;
3097 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3098 if (wp->w_cursor.lnum == lnume)
3099 wp->w_cline_folded = i;
3100
3101 /* If the changed line is in a range of previously folded lines,
3102 * compare with the first line in that range. */
3103 if (wp->w_cursor.lnum <= lnum)
3104 {
3105 i = find_wl_entry(wp, lnum);
3106 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3107 changed_line_abv_curs_win(wp);
3108 }
3109#endif
3110
3111 if (wp->w_cursor.lnum > lnum)
3112 changed_line_abv_curs_win(wp);
3113 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3114 changed_cline_bef_curs_win(wp);
3115 if (wp->w_botline >= lnum)
3116 {
3117 /* Assume that botline doesn't change (inserted lines make
3118 * other lines scroll down below botline). */
3119 approximate_botline_win(wp);
3120 }
3121
3122 /* Check if any w_lines[] entries have become invalid.
3123 * For entries below the change: Correct the lnums for
3124 * inserted/deleted lines. Makes it possible to stop displaying
3125 * after the change. */
3126 for (i = 0; i < wp->w_lines_valid; ++i)
3127 if (wp->w_lines[i].wl_valid)
3128 {
3129 if (wp->w_lines[i].wl_lnum >= lnum)
3130 {
3131 if (wp->w_lines[i].wl_lnum < lnume)
3132 {
3133 /* line included in change */
3134 wp->w_lines[i].wl_valid = FALSE;
3135 }
3136 else if (xtra != 0)
3137 {
3138 /* line below change */
3139 wp->w_lines[i].wl_lnum += xtra;
3140#ifdef FEAT_FOLDING
3141 wp->w_lines[i].wl_lastlnum += xtra;
3142#endif
3143 }
3144 }
3145#ifdef FEAT_FOLDING
3146 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3147 {
3148 /* change somewhere inside this range of folded lines,
3149 * may need to be redrawn */
3150 wp->w_lines[i].wl_valid = FALSE;
3151 }
3152#endif
3153 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003154
3155#ifdef FEAT_FOLDING
3156 /* Take care of side effects for setting w_topline when folds have
3157 * changed. Esp. when the buffer was changed in another window. */
3158 if (hasAnyFolding(wp))
3159 set_topline(wp, wp->w_topline);
3160#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003161 /* relative numbering may require updating more */
3162 if (wp->w_p_rnu)
3163 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
3165 }
3166
3167 /* Call update_screen() later, which checks out what needs to be redrawn,
3168 * since it notices b_mod_set and then uses b_mod_*. */
3169 if (must_redraw < VALID)
3170 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003171
3172#ifdef FEAT_AUTOCMD
3173 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003174 if (lnum <= curwin->w_cursor.lnum
3175 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003176 last_cursormoved.lnum = 0;
3177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178}
3179
3180/*
3181 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3182 */
3183 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003184unchanged(
3185 buf_T *buf,
3186 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003188 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003191 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 if (ff)
3193 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003195 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#ifdef FEAT_TITLE
3197 need_maketitle = TRUE; /* set window title later */
3198#endif
3199 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003200 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#ifdef FEAT_NETBEANS_INTG
3202 netbeans_unmodified(buf);
3203#endif
3204}
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206/*
3207 * check_status: called when the status bars for the buffer 'buf'
3208 * need to be updated
3209 */
3210 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003211check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212{
3213 win_T *wp;
3214
Bram Moolenaar29323592016-07-24 22:04:11 +02003215 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (wp->w_buffer == buf && wp->w_status_height)
3217 {
3218 wp->w_redr_status = TRUE;
3219 if (must_redraw < VALID)
3220 must_redraw = VALID;
3221 }
3222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224/*
3225 * If the file is readonly, give a warning message with the first change.
3226 * Don't do this for autocommands.
3227 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003228 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003230 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 */
3232 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003233change_warning(
3234 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 mode and 'showmode' is on */
3236{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003237 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (curbuf->b_did_warn == FALSE
3240 && curbufIsChanged() == 0
3241#ifdef FEAT_AUTOCMD
3242 && !autocmd_busy
3243#endif
3244 && curbuf->b_p_ro)
3245 {
3246#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003247 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003249 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (!curbuf->b_p_ro)
3251 return;
3252#endif
3253 /*
3254 * Do what msg() does, but with a column offset if the warning should
3255 * be after the mode message.
3256 */
3257 msg_start();
3258 if (msg_row == Rows - 1)
3259 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003260 msg_source(HL_ATTR(HLF_W));
3261 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003262#ifdef FEAT_EVAL
3263 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 msg_clr_eos();
3266 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003267 if (msg_silent == 0 && !silent_mode
3268#ifdef FEAT_EVAL
3269 && time_for_testing != 1
3270#endif
3271 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 out_flush();
3274 ui_delay(1000L, TRUE); /* give the user time to think about it */
3275 }
3276 curbuf->b_did_warn = TRUE;
3277 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3278 if (msg_row < Rows - 1)
3279 showmode();
3280 }
3281}
3282
3283/*
3284 * Ask for a reply from the user, a 'y' or a 'n'.
3285 * No other characters are accepted, the message is repeated until a valid
3286 * reply is entered or CTRL-C is hit.
3287 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3288 * from any buffers but directly from the user.
3289 *
3290 * return the 'y' or 'n'
3291 */
3292 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003293ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 int r = ' ';
3296 int save_State = State;
3297
3298 if (exiting) /* put terminal in raw mode for this question */
3299 settmode(TMODE_RAW);
3300 ++no_wait_return;
3301#ifdef USE_ON_FLY_SCROLL
3302 dont_scroll = TRUE; /* disallow scrolling here */
3303#endif
3304 State = CONFIRM; /* mouse behaves like with :confirm */
3305#ifdef FEAT_MOUSE
3306 setmouse(); /* disables mouse for xterm */
3307#endif
3308 ++no_mapping;
3309 ++allow_keys; /* no mapping here, but recognize keys */
3310
3311 while (r != 'y' && r != 'n')
3312 {
3313 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003314 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (direct)
3316 r = get_keystroke();
3317 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003318 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (r == Ctrl_C || r == ESC)
3320 r = 'n';
3321 msg_putchar(r); /* show what you typed */
3322 out_flush();
3323 }
3324 --no_wait_return;
3325 State = save_State;
3326#ifdef FEAT_MOUSE
3327 setmouse();
3328#endif
3329 --no_mapping;
3330 --allow_keys;
3331
3332 return r;
3333}
3334
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003335#if defined(FEAT_MOUSE) || defined(PROTO)
3336/*
3337 * Return TRUE if "c" is a mouse key.
3338 */
3339 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003340is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003341{
3342 return c == K_LEFTMOUSE
3343 || c == K_LEFTMOUSE_NM
3344 || c == K_LEFTDRAG
3345 || c == K_LEFTRELEASE
3346 || c == K_LEFTRELEASE_NM
3347 || c == K_MIDDLEMOUSE
3348 || c == K_MIDDLEDRAG
3349 || c == K_MIDDLERELEASE
3350 || c == K_RIGHTMOUSE
3351 || c == K_RIGHTDRAG
3352 || c == K_RIGHTRELEASE
3353 || c == K_MOUSEDOWN
3354 || c == K_MOUSEUP
3355 || c == K_MOUSELEFT
3356 || c == K_MOUSERIGHT
3357 || c == K_X1MOUSE
3358 || c == K_X1DRAG
3359 || c == K_X1RELEASE
3360 || c == K_X2MOUSE
3361 || c == K_X2DRAG
3362 || c == K_X2RELEASE;
3363}
3364#endif
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Get a key stroke directly from the user.
3368 * Ignores mouse clicks and scrollbar events, except a click for the left
3369 * button (used at the more prompt).
3370 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3371 * Disadvantage: typeahead is ignored.
3372 * Translates the interrupt character for unix to ESC.
3373 */
3374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003375get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003377 char_u *buf = NULL;
3378 int buflen = 150;
3379 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int len = 0;
3381 int n;
3382 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003383 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 mapped_ctrl_c = FALSE; /* mappings are not used here */
3386 for (;;)
3387 {
3388 cursor_on();
3389 out_flush();
3390
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003391 /* Leave some room for check_termcode() to insert a key code into (max
3392 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3393 * bytes. */
3394 maxlen = (buflen - 6 - len) / 3;
3395 if (buf == NULL)
3396 buf = alloc(buflen);
3397 else if (maxlen < 10)
3398 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003399 char_u *t_buf = buf;
3400
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003401 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003402 * escape sequence. */
3403 buflen += 100;
3404 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003405 if (buf == NULL)
3406 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003407 maxlen = (buflen - 6 - len) / 3;
3408 }
3409 if (buf == NULL)
3410 {
3411 do_outofmem_msg((long_u)buflen);
3412 return ESC; /* panic! */
3413 }
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003416 * terminal code to complete. */
3417 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (n > 0)
3419 {
3420 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003421 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003423 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003425 else if (len > 0)
3426 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar4395a712006-09-05 18:57:57 +00003428 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003429 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003430 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003432
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003433 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003434 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003435 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003436 {
3437 /* Redrawing was postponed, do it now. */
3438 update_screen(0);
3439 setcursor(); /* put cursor back where it belongs */
3440 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003442 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003443 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003445 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 continue;
3447
3448 /* Handle modifier and/or special key code. */
3449 n = buf[0];
3450 if (n == K_SPECIAL)
3451 {
3452 n = TO_SPECIAL(buf[1], buf[2]);
3453 if (buf[1] == KS_MODIFIER
3454 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003455#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003456 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003457#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003458#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 || n == K_VER_SCROLLBAR
3460 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461#endif
3462 )
3463 {
3464 if (buf[1] == KS_MODIFIER)
3465 mod_mask = buf[2];
3466 len -= 3;
3467 if (len > 0)
3468 mch_memmove(buf, buf + 3, (size_t)len);
3469 continue;
3470 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473#ifdef FEAT_MBYTE
3474 if (has_mbyte)
3475 {
3476 if (MB_BYTE2LEN(n) > len)
3477 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003478 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 n = (*mb_ptr2char)(buf);
3480 }
3481#endif
3482#ifdef UNIX
3483 if (n == intr_char)
3484 n = ESC;
3485#endif
3486 break;
3487 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003488 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 mapped_ctrl_c = save_mapped_ctrl_c;
3491 return n;
3492}
3493
3494/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003495 * Get a number from the user.
3496 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003499get_number(
3500 int colon, /* allow colon to abort */
3501 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 int n = 0;
3504 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003505 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003507 if (mouse_used != NULL)
3508 *mouse_used = FALSE;
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /* When not printing messages, the user won't know what to type, return a
3511 * zero (as if CR was hit). */
3512 if (msg_silent != 0)
3513 return 0;
3514
3515#ifdef USE_ON_FLY_SCROLL
3516 dont_scroll = TRUE; /* disallow scrolling here */
3517#endif
3518 ++no_mapping;
3519 ++allow_keys; /* no mapping here, but recognize keys */
3520 for (;;)
3521 {
3522 windgoto(msg_row, msg_col);
3523 c = safe_vgetc();
3524 if (VIM_ISDIGIT(c))
3525 {
3526 n = n * 10 + c - '0';
3527 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003528 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3531 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003532 if (typed > 0)
3533 {
3534 MSG_PUTS("\b \b");
3535 --typed;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003539#ifdef FEAT_MOUSE
3540 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3541 {
3542 *mouse_used = TRUE;
3543 n = mouse_row + 1;
3544 break;
3545 }
3546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 else if (n == 0 && c == ':' && colon)
3548 {
3549 stuffcharReadbuff(':');
3550 if (!exmode_active)
3551 cmdline_row = msg_row;
3552 skip_redraw = TRUE; /* skip redraw once */
3553 do_redraw = FALSE;
3554 break;
3555 }
3556 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3557 break;
3558 }
3559 --no_mapping;
3560 --allow_keys;
3561 return n;
3562}
3563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003564/*
3565 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003566 * When "mouse_used" is not NULL allow using the mouse and in that case return
3567 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 */
3569 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003570prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003571{
3572 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003573 int save_cmdline_row;
3574 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575
3576 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003577 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003578 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003579 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003580 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003581
Bram Moolenaar203335e2006-09-03 14:35:42 +00003582 /* Set the state such that text can be selected/copied/pasted and we still
3583 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003585 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003586 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003587 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003588
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003589 i = get_number(TRUE, mouse_used);
3590 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003591 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003592 /* don't call wait_return() now */
3593 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003594 cmdline_row = msg_row - 1;
3595 need_wait_return = FALSE;
3596 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003597 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003599 else
3600 cmdline_row = save_cmdline_row;
3601 State = save_State;
3602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 return i;
3604}
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003607msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 long pn;
3610
3611 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3613 return;
3614
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003615 /* We don't want to overwrite another important message, but do overwrite
3616 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3617 * then "put" reports the last action. */
3618 if (keep_msg != NULL && !keep_msg_more)
3619 return;
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (n > 0)
3622 pn = n;
3623 else
3624 pn = -n;
3625
3626 if (pn > p_report)
3627 {
3628 if (pn == 1)
3629 {
3630 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003631 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3632 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003634 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3635 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else
3638 {
3639 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003640 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3641 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003643 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3644 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003647 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (msg(msg_buf))
3649 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003650 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003651 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653 }
3654}
3655
3656/*
3657 * flush map and typeahead buffers and give a warning for an error
3658 */
3659 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003660beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
3662 if (emsg_silent == 0)
3663 {
3664 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003665 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667}
3668
3669/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003670 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 */
3672 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003673vim_beep(
3674 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 if (emsg_silent == 0)
3677 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003678 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3679 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003680#ifdef ELAPSED_FUNC
3681 static int did_init = FALSE;
3682 static ELAPSED_TYPE start_tv;
3683
3684 /* Only beep once per half a second, otherwise a sequence of beeps
3685 * would freeze Vim. */
3686 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3687 {
3688 did_init = TRUE;
3689 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003691 if (p_vb
3692#ifdef FEAT_GUI
3693 /* While the GUI is starting up the termcap is set for
3694 * the GUI but the output still goes to a terminal. */
3695 && !(gui.in_use && gui.starting)
3696#endif
3697 )
3698 out_str_cf(T_VB);
3699 else
3700 out_char(BELL);
3701#ifdef ELAPSED_FUNC
3702 }
3703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003705
3706 /* When 'verbose' is set and we are sourcing a script or executing a
3707 * function give the user a hint where the beep comes from. */
3708 if (vim_strchr(p_debug, 'e') != NULL)
3709 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003710 msg_source(HL_ATTR(HLF_W));
3711 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 }
3714}
3715
3716/*
3717 * To get the "real" home directory:
3718 * - get value of $HOME
3719 * For Unix:
3720 * - go to that directory
3721 * - do mch_dirname() to get the real name of that directory.
3722 * This also works with mounts and links.
3723 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3724 */
3725static char_u *homedir = NULL;
3726
3727 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003728init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
3730 char_u *var;
3731
Bram Moolenaar05159a02005-02-26 23:04:13 +00003732 /* In case we are called a second time (when 'encoding' changes). */
3733 vim_free(homedir);
3734 homedir = NULL;
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#ifdef VMS
3737 var = mch_getenv((char_u *)"SYS$LOGIN");
3738#else
3739 var = mch_getenv((char_u *)"HOME");
3740#endif
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#ifdef WIN3264
3743 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003744 * Typically, $HOME is not defined on Windows, unless the user has
3745 * specifically defined it for Vim's sake. However, on Windows NT
3746 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3747 * each user. Try constructing $HOME from these.
3748 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003749 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003750 {
3751 char_u *homedrive, *homepath;
3752
3753 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3754 homepath = mch_getenv((char_u *)"HOMEPATH");
3755 if (homepath == NULL || *homepath == NUL)
3756 homepath = (char_u *)"\\";
3757 if (homedrive != NULL
3758 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3759 {
3760 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3761 if (NameBuff[0] != NUL)
3762 var = NameBuff;
3763 }
3764 }
3765
3766 if (var == NULL)
3767 var = mch_getenv((char_u *)"USERPROFILE");
3768
3769 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 * Weird but true: $HOME may contain an indirect reference to another
3771 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3772 * when $HOME is being set.
3773 */
3774 if (var != NULL && *var == '%')
3775 {
3776 char_u *p;
3777 char_u *exp;
3778
3779 p = vim_strchr(var + 1, '%');
3780 if (p != NULL)
3781 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003782 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 exp = mch_getenv(NameBuff);
3784 if (exp != NULL && *exp != NUL
3785 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3786 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003787 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 }
3791 }
3792
Bram Moolenaar48340b62017-08-29 22:08:53 +02003793 if (var != NULL && *var == NUL) /* empty is same as not set */
3794 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaar48340b62017-08-29 22:08:53 +02003796# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003797 if (enc_utf8 && var != NULL)
3798 {
3799 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003800 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003801
3802 /* Convert from active codepage to UTF-8. Other conversions are
3803 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003804 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003805 if (pp != NULL)
3806 {
3807 homedir = pp;
3808 return;
3809 }
3810 }
3811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 /*
3814 * Default home dir is C:/
3815 * Best assumption we can make in such a situation.
3816 */
3817 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003818 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (var != NULL)
3822 {
3823#ifdef UNIX
3824 /*
3825 * Change to the directory and get the actual path. This resolves
3826 * links. Don't do it when we can't return.
3827 */
3828 if (mch_dirname(NameBuff, MAXPATHL) == OK
3829 && mch_chdir((char *)NameBuff) == 0)
3830 {
3831 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3832 var = IObuff;
3833 if (mch_chdir((char *)NameBuff) != 0)
3834 EMSG(_(e_prev_dir));
3835 }
3836#endif
3837 homedir = vim_strsave(var);
3838 }
3839}
3840
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003841#if defined(EXITFREE) || defined(PROTO)
3842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003843free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003844{
3845 vim_free(homedir);
3846}
Bram Moolenaar24305862012-08-15 14:05:05 +02003847
3848# ifdef FEAT_CMDL_COMPL
3849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003850free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003851{
3852 ga_clear_strings(&ga_users);
3853}
3854# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003855#endif
3856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003858 * Call expand_env() and store the result in an allocated string.
3859 * This is not very memory efficient, this expects the result to be freed
3860 * again soon.
3861 */
3862 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003863expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003864{
3865 return expand_env_save_opt(src, FALSE);
3866}
3867
3868/*
3869 * Idem, but when "one" is TRUE handle the string as one file name, only
3870 * expand "~" at the start.
3871 */
3872 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003873expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003874{
3875 char_u *p;
3876
3877 p = alloc(MAXPATHL);
3878 if (p != NULL)
3879 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3880 return p;
3881}
3882
3883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 * Expand environment variable with path name.
3885 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003886 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 * If anything fails no expansion is done and dst equals src.
3888 */
3889 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003890expand_env(
3891 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3892 char_u *dst, /* where to put the result */
3893 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003895 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896}
3897
3898 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003899expand_env_esc(
3900 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3901 char_u *dst, /* where to put the result */
3902 int dstlen, /* maximum length of the result */
3903 int esc, /* escape spaces in expanded variables */
3904 int one, /* "srcp" is one file name */
3905 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003907 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 char_u *tail;
3909 int c;
3910 char_u *var;
3911 int copy_char;
3912 int mustfree; /* var was allocated, need to free it later */
3913 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003914 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003916 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003917 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003918
3919 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 --dstlen; /* leave one char space for "\," */
3921 while (*src && dstlen > 0)
3922 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003923#ifdef FEAT_EVAL
3924 /* Skip over `=expr`. */
3925 if (src[0] == '`' && src[1] == '=')
3926 {
3927 size_t len;
3928
3929 var = src;
3930 src += 2;
3931 (void)skip_expr(&src);
3932 if (*src == '`')
3933 ++src;
3934 len = src - var;
3935 if (len > (size_t)dstlen)
3936 len = dstlen;
3937 vim_strncpy(dst, var, len);
3938 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003939 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003940 continue;
3941 }
3942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003944 if ((*src == '$'
3945#ifdef VMS
3946 && at_start
3947#endif
3948 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003949#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 || *src == '%'
3951#endif
3952 || (*src == '~' && at_start))
3953 {
3954 mustfree = FALSE;
3955
3956 /*
3957 * The variable name is copied into dst temporarily, because it may
3958 * be a string in read-only memory and a NUL needs to be appended.
3959 */
3960 if (*src != '~') /* environment var */
3961 {
3962 tail = src + 1;
3963 var = dst;
3964 c = dstlen - 1;
3965
3966#ifdef UNIX
3967 /* Unix has ${var-name} type environment vars */
3968 if (*tail == '{' && !vim_isIDc('{'))
3969 {
3970 tail++; /* ignore '{' */
3971 while (c-- > 0 && *tail && *tail != '}')
3972 *var++ = *tail++;
3973 }
3974 else
3975#endif
3976 {
3977 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003978#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 || (*src == '%' && *tail != '%')
3980#endif
3981 ))
3982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 }
3985 }
3986
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003987#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988# ifdef UNIX
3989 if (src[1] == '{' && *tail != '}')
3990# else
3991 if (*src == '%' && *tail != '%')
3992# endif
3993 var = NULL;
3994 else
3995 {
3996# ifdef UNIX
3997 if (src[1] == '{')
3998# else
3999 if (*src == '%')
4000#endif
4001 ++tail;
4002#endif
4003 *var = NUL;
4004 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004005#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007#endif
4008 }
4009 /* home directory */
4010 else if ( src[1] == NUL
4011 || vim_ispathsep(src[1])
4012 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4013 {
4014 var = homedir;
4015 tail = src + 1;
4016 }
4017 else /* user directory */
4018 {
4019#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4020 /*
4021 * Copy ~user to dst[], so we can put a NUL after it.
4022 */
4023 tail = src;
4024 var = dst;
4025 c = dstlen - 1;
4026 while ( c-- > 0
4027 && *tail
4028 && vim_isfilec(*tail)
4029 && !vim_ispathsep(*tail))
4030 *var++ = *tail++;
4031 *var = NUL;
4032# ifdef UNIX
4033 /*
4034 * If the system supports getpwnam(), use it.
4035 * Otherwise, or if getpwnam() fails, the shell is used to
4036 * expand ~user. This is slower and may fail if the shell
4037 * does not support ~user (old versions of /bin/sh).
4038 */
4039# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4040 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004041 /* Note: memory allocated by getpwnam() is never freed.
4042 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004043 struct passwd *pw = (*dst == NUL)
4044 ? NULL : getpwnam((char *)dst + 1);
4045
4046 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048 if (var == NULL)
4049# endif
4050 {
4051 expand_T xpc;
4052
4053 ExpandInit(&xpc);
4054 xpc.xp_context = EXPAND_FILES;
4055 var = ExpandOne(&xpc, dst, NULL,
4056 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 mustfree = TRUE;
4058 }
4059
4060# else /* !UNIX, thus VMS */
4061 /*
4062 * USER_HOME is a comma-separated list of
4063 * directories to search for the user account in.
4064 */
4065 {
4066 char_u test[MAXPATHL], paths[MAXPATHL];
4067 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004068 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 STRCPY(paths, USER_HOME);
4071 next_path = paths;
4072 while (*next_path)
4073 {
4074 for (path = next_path; *next_path && *next_path != ',';
4075 next_path++);
4076 if (*next_path)
4077 *next_path++ = NUL;
4078 STRCPY(test, path);
4079 STRCAT(test, "/");
4080 STRCAT(test, dst + 1);
4081 if (mch_stat(test, &st) == 0)
4082 {
4083 var = alloc(STRLEN(test) + 1);
4084 STRCPY(var, test);
4085 mustfree = TRUE;
4086 break;
4087 }
4088 }
4089 }
4090# endif /* UNIX */
4091#else
4092 /* cannot expand user's home directory, so don't try */
4093 var = NULL;
4094 tail = (char_u *)""; /* for gcc */
4095#endif /* UNIX || VMS */
4096 }
4097
4098#ifdef BACKSLASH_IN_FILENAME
4099 /* If 'shellslash' is set change backslashes to forward slashes.
4100 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4101 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4102 {
4103 char_u *p = vim_strsave(var);
4104
4105 if (p != NULL)
4106 {
4107 if (mustfree)
4108 vim_free(var);
4109 var = p;
4110 mustfree = TRUE;
4111 forward_slash(var);
4112 }
4113 }
4114#endif
4115
4116 /* If "var" contains white space, escape it with a backslash.
4117 * Required for ":e ~/tt" when $HOME includes a space. */
4118 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4119 {
4120 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4121
4122 if (p != NULL)
4123 {
4124 if (mustfree)
4125 vim_free(var);
4126 var = p;
4127 mustfree = TRUE;
4128 }
4129 }
4130
4131 if (var != NULL && *var != NUL
4132 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4133 {
4134 STRCPY(dst, var);
4135 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004136 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 /* if var[] ends in a path separator and tail[] starts
4138 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004139 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4141 && dst[-1] != ':'
4142#endif
4143 && vim_ispathsep(*tail))
4144 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004145 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 src = tail;
4147 copy_char = FALSE;
4148 }
4149 if (mustfree)
4150 vim_free(var);
4151 }
4152
4153 if (copy_char) /* copy at least one char */
4154 {
4155 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004156 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004157 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4158 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 */
4160 at_start = FALSE;
4161 if (src[0] == '\\' && src[1] != NUL)
4162 {
4163 *dst++ = *src++;
4164 --dstlen;
4165 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004166 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004168 if (dstlen > 0)
4169 {
4170 *dst++ = *src++;
4171 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004172
Bram Moolenaar1c864092017-08-06 18:15:45 +02004173 if (startstr != NULL && src - startstr_len >= srcp
4174 && STRNCMP(src - startstr_len, startstr,
4175 startstr_len) == 0)
4176 at_start = TRUE;
4177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 *dst = NUL;
4182}
4183
4184/*
4185 * Vim's version of getenv().
4186 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004187 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004188 * "mustfree" is set to TRUE when returned is allocated, it must be
4189 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 */
4191 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004192vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
4194 char_u *p;
4195 char_u *pend;
4196 int vimruntime;
4197
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004198#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 /* use "C:/" when $HOME is not set */
4200 if (STRCMP(name, "HOME") == 0)
4201 return homedir;
4202#endif
4203
4204 p = mch_getenv(name);
4205 if (p != NULL && *p == NUL) /* empty is the same as not set */
4206 p = NULL;
4207
4208 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004209 {
4210#if defined(FEAT_MBYTE) && defined(WIN3264)
4211 if (enc_utf8)
4212 {
4213 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004214 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215
4216 /* Convert from active codepage to UTF-8. Other conversions are
4217 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004218 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004219 if (pp != NULL)
4220 {
4221 p = pp;
4222 *mustfree = TRUE;
4223 }
4224 }
4225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
4229 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4230 if (!vimruntime && STRCMP(name, "VIM") != 0)
4231 return NULL;
4232
4233 /*
4234 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4235 * Don't do this when default_vimruntime_dir is non-empty.
4236 */
4237 if (vimruntime
4238#ifdef HAVE_PATHDEF
4239 && *default_vimruntime_dir == NUL
4240#endif
4241 )
4242 {
4243 p = mch_getenv((char_u *)"VIM");
4244 if (p != NULL && *p == NUL) /* empty is the same as not set */
4245 p = NULL;
4246 if (p != NULL)
4247 {
4248 p = vim_version_dir(p);
4249 if (p != NULL)
4250 *mustfree = TRUE;
4251 else
4252 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004253
4254#if defined(FEAT_MBYTE) && defined(WIN3264)
4255 if (enc_utf8)
4256 {
4257 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004258 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004259
4260 /* Convert from active codepage to UTF-8. Other conversions
4261 * are not done, because they would fail for non-ASCII
4262 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004263 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004264 if (pp != NULL)
4265 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004266 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004267 vim_free(p);
4268 p = pp;
4269 *mustfree = TRUE;
4270 }
4271 }
4272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275
4276 /*
4277 * When expanding $VIM or $VIMRUNTIME fails, try using:
4278 * - the directory name from 'helpfile' (unless it contains '$')
4279 * - the executable name from argv[0]
4280 */
4281 if (p == NULL)
4282 {
4283 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4284 p = p_hf;
4285#ifdef USE_EXE_NAME
4286 /*
4287 * Use the name of the executable, obtained from argv[0].
4288 */
4289 else
4290 p = exe_name;
4291#endif
4292 if (p != NULL)
4293 {
4294 /* remove the file name */
4295 pend = gettail(p);
4296
4297 /* remove "doc/" from 'helpfile', if present */
4298 if (p == p_hf)
4299 pend = remove_tail(p, pend, (char_u *)"doc");
4300
4301#ifdef USE_EXE_NAME
4302# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004303 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (p == exe_name)
4305 {
4306 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004307 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004309 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4310 if (pend1 != pend)
4311 {
4312 pnew = alloc((unsigned)(pend1 - p) + 15);
4313 if (pnew != NULL)
4314 {
4315 STRNCPY(pnew, p, (pend1 - p));
4316 STRCPY(pnew + (pend1 - p), "Resources/vim");
4317 p = pnew;
4318 pend = p + STRLEN(p);
4319 }
4320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322# endif
4323 /* remove "src/" from exe_name, if present */
4324 if (p == exe_name)
4325 pend = remove_tail(p, pend, (char_u *)"src");
4326#endif
4327
4328 /* for $VIM, remove "runtime/" or "vim54/", if present */
4329 if (!vimruntime)
4330 {
4331 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4332 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4333 }
4334
4335 /* remove trailing path separator */
4336#ifndef MACOS_CLASSIC
4337 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004338 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004339 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 --pend;
4341#endif
4342
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004343#ifdef MACOS_X
4344 if (p == exe_name || p == p_hf)
4345#endif
4346 /* check that the result is a directory name */
4347 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 if (p != NULL && !mch_isdir(p))
4350 {
4351 vim_free(p);
4352 p = NULL;
4353 }
4354 else
4355 {
4356#ifdef USE_EXE_NAME
4357 /* may add "/vim54" or "/runtime" if it exists */
4358 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4359 {
4360 vim_free(p);
4361 p = pend;
4362 }
4363#endif
4364 *mustfree = TRUE;
4365 }
4366 }
4367 }
4368
4369#ifdef HAVE_PATHDEF
4370 /* When there is a pathdef.c file we can use default_vim_dir and
4371 * default_vimruntime_dir */
4372 if (p == NULL)
4373 {
4374 /* Only use default_vimruntime_dir when it is not empty */
4375 if (vimruntime && *default_vimruntime_dir != NUL)
4376 {
4377 p = default_vimruntime_dir;
4378 *mustfree = FALSE;
4379 }
4380 else if (*default_vim_dir != NUL)
4381 {
4382 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4383 *mustfree = TRUE;
4384 else
4385 {
4386 p = default_vim_dir;
4387 *mustfree = FALSE;
4388 }
4389 }
4390 }
4391#endif
4392
4393 /*
4394 * Set the environment variable, so that the new value can be found fast
4395 * next time, and others can also use it (e.g. Perl).
4396 */
4397 if (p != NULL)
4398 {
4399 if (vimruntime)
4400 {
4401 vim_setenv((char_u *)"VIMRUNTIME", p);
4402 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 else
4405 {
4406 vim_setenv((char_u *)"VIM", p);
4407 didset_vim = TRUE;
4408 }
4409 }
4410 return p;
4411}
4412
4413/*
4414 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4415 * Return NULL if not, return its name in allocated memory otherwise.
4416 */
4417 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004418vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
4420 char_u *p;
4421
4422 if (vimdir == NULL || *vimdir == NUL)
4423 return NULL;
4424 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4425 if (p != NULL && mch_isdir(p))
4426 return p;
4427 vim_free(p);
4428 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4429 if (p != NULL && mch_isdir(p))
4430 return p;
4431 vim_free(p);
4432 return NULL;
4433}
4434
4435/*
4436 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4437 * the length of "name/". Otherwise return "pend".
4438 */
4439 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004440remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442 int len = (int)STRLEN(name) + 1;
4443 char_u *newend = pend - len;
4444
4445 if (newend >= p
4446 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004447 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 return newend;
4449 return pend;
4450}
4451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 * Our portable version of setenv.
4454 */
4455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004456vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
4458#ifdef HAVE_SETENV
4459 mch_setenv((char *)name, (char *)val, 1);
4460#else
4461 char_u *envbuf;
4462
4463 /*
4464 * Putenv does not copy the string, it has to remain
4465 * valid. The allocated memory will never be freed.
4466 */
4467 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4468 if (envbuf != NULL)
4469 {
4470 sprintf((char *)envbuf, "%s=%s", name, val);
4471 putenv((char *)envbuf);
4472 }
4473#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004474#ifdef FEAT_GETTEXT
4475 /*
4476 * When setting $VIMRUNTIME adjust the directory to find message
4477 * translations to $VIMRUNTIME/lang.
4478 */
4479 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4480 {
4481 char_u *buf = concat_str(val, (char_u *)"/lang");
4482
4483 if (buf != NULL)
4484 {
4485 bindtextdomain(VIMPACKAGE, (char *)buf);
4486 vim_free(buf);
4487 }
4488 }
4489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490}
4491
4492#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4493/*
4494 * Function given to ExpandGeneric() to obtain an environment variable name.
4495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004497get_env_name(
4498 expand_T *xp UNUSED,
4499 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500{
4501# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4502 /*
4503 * No environ[] on the Amiga and on the Mac (using MPW).
4504 */
4505 return NULL;
4506# else
4507# ifndef __WIN32__
4508 /* Borland C++ 5.2 has this in a header file. */
4509 extern char **environ;
4510# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004511# define ENVNAMELEN 100
4512 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 char_u *str;
4514 int n;
4515
4516 str = (char_u *)environ[idx];
4517 if (str == NULL)
4518 return NULL;
4519
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004520 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522 if (str[n] == '=' || str[n] == NUL)
4523 break;
4524 name[n] = str[n];
4525 }
4526 name[n] = NUL;
4527 return name;
4528# endif
4529}
Bram Moolenaar24305862012-08-15 14:05:05 +02004530
4531/*
4532 * Find all user names for user completion.
4533 * Done only once and then cached.
4534 */
4535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004536init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004537{
Bram Moolenaar24305862012-08-15 14:05:05 +02004538 static int lazy_init_done = FALSE;
4539
4540 if (lazy_init_done)
4541 return;
4542
4543 lazy_init_done = TRUE;
4544 ga_init2(&ga_users, sizeof(char_u *), 20);
4545
4546# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4547 {
4548 char_u* user;
4549 struct passwd* pw;
4550
4551 setpwent();
4552 while ((pw = getpwent()) != NULL)
4553 /* pw->pw_name shouldn't be NULL but just in case... */
4554 if (pw->pw_name != NULL)
4555 {
4556 if (ga_grow(&ga_users, 1) == FAIL)
4557 break;
4558 user = vim_strsave((char_u*)pw->pw_name);
4559 if (user == NULL)
4560 break;
4561 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4562 }
4563 endpwent();
4564 }
4565# endif
4566}
4567
4568/*
4569 * Function given to ExpandGeneric() to obtain an user names.
4570 */
4571 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004572get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004573{
4574 init_users();
4575 if (idx < ga_users.ga_len)
4576 return ((char_u **)ga_users.ga_data)[idx];
4577 return NULL;
4578}
4579
4580/*
4581 * Check whether name matches a user name. Return:
4582 * 0 if name does not match any user name.
4583 * 1 if name partially matches the beginning of a user name.
4584 * 2 is name fully matches a user name.
4585 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004586int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004587{
4588 int i;
4589 int n = (int)STRLEN(name);
4590 int result = 0;
4591
4592 init_users();
4593 for (i = 0; i < ga_users.ga_len; i++)
4594 {
4595 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4596 return 2; /* full match */
4597 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4598 result = 1; /* partial match */
4599 }
4600 return result;
4601}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#endif
4603
4604/*
4605 * Replace home directory by "~" in each space or comma separated file name in
4606 * 'src'.
4607 * If anything fails (except when out of space) dst equals src.
4608 */
4609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004610home_replace(
4611 buf_T *buf, /* when not NULL, check for help files */
4612 char_u *src, /* input file name */
4613 char_u *dst, /* where to put the result */
4614 int dstlen, /* maximum length of the result */
4615 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 spaces and commas in the file name. */
4617{
4618 size_t dirlen = 0, envlen = 0;
4619 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004620 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_u *p;
4622
4623 if (src == NULL)
4624 {
4625 *dst = NUL;
4626 return;
4627 }
4628
4629 /*
4630 * If the file is a help file, remove the path completely.
4631 */
4632 if (buf != NULL && buf->b_help)
4633 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004634 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 return;
4636 }
4637
4638 /*
4639 * We check both the value of the $HOME environment variable and the
4640 * "real" home directory.
4641 */
4642 if (homedir != NULL)
4643 dirlen = STRLEN(homedir);
4644
4645#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004646 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004648 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4649#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004650#ifdef WIN3264
4651 if (homedir_env == NULL)
4652 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4653#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004654 /* Empty is the same as not set. */
4655 if (homedir_env != NULL && *homedir_env == NUL)
4656 homedir_env = NULL;
4657
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004658#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004659 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004660 {
4661 int usedlen = 0;
4662 int flen;
4663 char_u *fbuf = NULL;
4664
4665 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004666 (void)modify_fname((char_u *)":p", &usedlen,
4667 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004668 flen = (int)STRLEN(homedir_env);
4669 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4670 /* Remove the trailing / that is added to a directory. */
4671 homedir_env[flen - 1] = NUL;
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673#endif
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (homedir_env != NULL)
4676 envlen = STRLEN(homedir_env);
4677
4678 if (!one)
4679 src = skipwhite(src);
4680 while (*src && dstlen > 0)
4681 {
4682 /*
4683 * Here we are at the beginning of a file name.
4684 * First, check to see if the beginning of the file name matches
4685 * $HOME or the "real" home directory. Check that there is a '/'
4686 * after the match (so that if e.g. the file is "/home/pieter/bla",
4687 * and the home directory is "/home/piet", the file does not end up
4688 * as "~er/bla" (which would seem to indicate the file "bla" in user
4689 * er's home directory)).
4690 */
4691 p = homedir;
4692 len = dirlen;
4693 for (;;)
4694 {
4695 if ( len
4696 && fnamencmp(src, p, len) == 0
4697 && (vim_ispathsep(src[len])
4698 || (!one && (src[len] == ',' || src[len] == ' '))
4699 || src[len] == NUL))
4700 {
4701 src += len;
4702 if (--dstlen > 0)
4703 *dst++ = '~';
4704
4705 /*
4706 * If it's just the home directory, add "/".
4707 */
4708 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4709 *dst++ = '/';
4710 break;
4711 }
4712 if (p == homedir_env)
4713 break;
4714 p = homedir_env;
4715 len = envlen;
4716 }
4717
4718 /* if (!one) skip to separator: space or comma */
4719 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4720 *dst++ = *src++;
4721 /* skip separator */
4722 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4723 *dst++ = *src++;
4724 }
4725 /* if (dstlen == 0) out of space, what to do??? */
4726
4727 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004728
4729 if (homedir_env != homedir_env_orig)
4730 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731}
4732
4733/*
4734 * Like home_replace, store the replaced string in allocated memory.
4735 * When something fails, NULL is returned.
4736 */
4737 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004738home_replace_save(
4739 buf_T *buf, /* when not NULL, check for help files */
4740 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
4742 char_u *dst;
4743 unsigned len;
4744
4745 len = 3; /* space for "~/" and trailing NUL */
4746 if (src != NULL) /* just in case */
4747 len += (unsigned)STRLEN(src);
4748 dst = alloc(len);
4749 if (dst != NULL)
4750 home_replace(buf, src, dst, len, TRUE);
4751 return dst;
4752}
4753
4754/*
4755 * Compare two file names and return:
4756 * FPC_SAME if they both exist and are the same file.
4757 * FPC_SAMEX if they both don't exist and have the same file name.
4758 * FPC_DIFF if they both exist and are different files.
4759 * FPC_NOTX if they both don't exist.
4760 * FPC_DIFFX if one of them doesn't exist.
4761 * For the first name environment variables are expanded
4762 */
4763 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004764fullpathcmp(
4765 char_u *s1,
4766 char_u *s2,
4767 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769#ifdef UNIX
4770 char_u exp1[MAXPATHL];
4771 char_u full1[MAXPATHL];
4772 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004773 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 int r1, r2;
4775
4776 expand_env(s1, exp1, MAXPATHL);
4777 r1 = mch_stat((char *)exp1, &st1);
4778 r2 = mch_stat((char *)s2, &st2);
4779 if (r1 != 0 && r2 != 0)
4780 {
4781 /* if mch_stat() doesn't work, may compare the names */
4782 if (checkname)
4783 {
4784 if (fnamecmp(exp1, s2) == 0)
4785 return FPC_SAMEX;
4786 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4787 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4788 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4789 return FPC_SAMEX;
4790 }
4791 return FPC_NOTX;
4792 }
4793 if (r1 != 0 || r2 != 0)
4794 return FPC_DIFFX;
4795 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4796 return FPC_SAME;
4797 return FPC_DIFF;
4798#else
4799 char_u *exp1; /* expanded s1 */
4800 char_u *full1; /* full path of s1 */
4801 char_u *full2; /* full path of s2 */
4802 int retval = FPC_DIFF;
4803 int r1, r2;
4804
4805 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4806 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4807 {
4808 full1 = exp1 + MAXPATHL;
4809 full2 = full1 + MAXPATHL;
4810
4811 expand_env(s1, exp1, MAXPATHL);
4812 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4813 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4814
4815 /* If vim_FullName() fails, the file probably doesn't exist. */
4816 if (r1 != OK && r2 != OK)
4817 {
4818 if (checkname && fnamecmp(exp1, s2) == 0)
4819 retval = FPC_SAMEX;
4820 else
4821 retval = FPC_NOTX;
4822 }
4823 else if (r1 != OK || r2 != OK)
4824 retval = FPC_DIFFX;
4825 else if (fnamecmp(full1, full2))
4826 retval = FPC_DIFF;
4827 else
4828 retval = FPC_SAME;
4829 vim_free(exp1);
4830 }
4831 return retval;
4832#endif
4833}
4834
4835/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004836 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004837 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004838 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
4840 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004841gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 char_u *p1, *p2;
4844
4845 if (fname == NULL)
4846 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004847 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004849 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004851 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 return p1;
4854}
4855
Bram Moolenaar31710262010-08-13 13:36:15 +02004856#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004857static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004858
4859/*
4860 * Return the end of the directory name, on the first path
4861 * separator:
4862 * "/path/file", "/path/dir/", "/path//dir", "/file"
4863 * ^ ^ ^ ^
4864 */
4865 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004866gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004867{
4868 char_u *dir_end = fname;
4869 char_u *next_dir_end = fname;
4870 int look_for_sep = TRUE;
4871 char_u *p;
4872
4873 for (p = fname; *p != NUL; )
4874 {
4875 if (vim_ispathsep(*p))
4876 {
4877 if (look_for_sep)
4878 {
4879 next_dir_end = p;
4880 look_for_sep = FALSE;
4881 }
4882 }
4883 else
4884 {
4885 if (!look_for_sep)
4886 dir_end = next_dir_end;
4887 look_for_sep = TRUE;
4888 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004889 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004890 }
4891 return dir_end;
4892}
4893#endif
4894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004896 * Get pointer to tail of "fname", including path separators. Putting a NUL
4897 * here leaves the directory name. Takes care of "c:/" and "//".
4898 * Always returns a valid pointer.
4899 */
4900 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004901gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004902{
4903 char_u *p;
4904 char_u *t;
4905
4906 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4907 t = gettail(fname);
4908 while (t > p && after_pathsep(fname, t))
4909 --t;
4910#ifdef VMS
4911 /* path separator is part of the path */
4912 ++t;
4913#endif
4914 return t;
4915}
4916
4917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 * get the next path component (just after the next path separator).
4919 */
4920 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004921getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004924 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 if (*fname)
4926 ++fname;
4927 return fname;
4928}
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930/*
4931 * Get a pointer to one character past the head of a path name.
4932 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4933 * If there is no head, path is returned.
4934 */
4935 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004936get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
4938 char_u *retval;
4939
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004940#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 /* may skip "c:" */
4942 if (isalpha(path[0]) && path[1] == ':')
4943 retval = path + 2;
4944 else
4945 retval = path;
4946#else
4947# if defined(AMIGA)
4948 /* may skip "label:" */
4949 retval = vim_strchr(path, ':');
4950 if (retval == NULL)
4951 retval = path;
4952# else /* Unix */
4953 retval = path;
4954# endif
4955#endif
4956
4957 while (vim_ispathsep(*retval))
4958 ++retval;
4959
4960 return retval;
4961}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
4963/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004964 * Return TRUE if 'c' is a path separator.
4965 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
4967 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004968vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004970#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004972#else
4973# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004975# else
4976# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4978 return (c == ':' || c == '[' || c == ']' || c == '/'
4979 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004980# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004982# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985}
4986
Bram Moolenaar69c35002013-11-04 02:54:12 +01004987/*
4988 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4989 */
4990 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004991vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004992{
4993 return vim_ispathsep(c)
4994#ifdef BACKSLASH_IN_FILENAME
4995 && c != ':'
4996#endif
4997 ;
4998}
4999
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5001/*
5002 * return TRUE if 'c' is a path list separator.
5003 */
5004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005005vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006{
5007#ifdef UNIX
5008 return (c == ':');
5009#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005010 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#endif
5012}
5013#endif
5014
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005015/*
5016 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5017 * It's done in-place.
5018 */
5019 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005020shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005021{
5022 char_u *tail, *s, *d;
5023 int skip = FALSE;
5024
5025 tail = gettail(str);
5026 d = str;
5027 for (s = str; ; ++s)
5028 {
5029 if (s >= tail) /* copy the whole tail */
5030 {
5031 *d++ = *s;
5032 if (*s == NUL)
5033 break;
5034 }
5035 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5036 {
5037 *d++ = *s;
5038 skip = FALSE;
5039 }
5040 else if (!skip)
5041 {
5042 *d++ = *s; /* copy next char */
5043 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5044 skip = TRUE;
5045# ifdef FEAT_MBYTE
5046 if (has_mbyte)
5047 {
5048 int l = mb_ptr2len(s);
5049
5050 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005051 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005052 }
5053# endif
5054 }
5055 }
5056}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005057
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005058/*
5059 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5060 * Also returns TRUE if there is no directory name.
5061 * "fname" must be writable!.
5062 */
5063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005064dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005065{
5066 char_u *p;
5067 int c;
5068 int retval;
5069
5070 p = gettail_sep(fname);
5071 if (p == fname)
5072 return TRUE;
5073 c = *p;
5074 *p = NUL;
5075 retval = mch_isdir(fname);
5076 *p = c;
5077 return retval;
5078}
5079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005081 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5082 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
5084 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005085vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005087#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005089#else
5090 if (p_fic)
5091 return MB_STRICMP(x, y);
5092 return STRCMP(x, y);
5093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094}
5095
5096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005097vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005099#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005100 char_u *px = x;
5101 char_u *py = y;
5102 int cx = NUL;
5103 int cy = NUL;
5104
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005105 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005107 cx = PTR2CHAR(px);
5108 cy = PTR2CHAR(py);
5109 if (cx == NUL || cy == NUL
5110 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5111 && !(cx == '/' && cy == '\\')
5112 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005114 len -= MB_PTR2LEN(px);
5115 px += MB_PTR2LEN(px);
5116 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118 if (len == 0)
5119 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005120 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005121#else
5122 if (p_fic)
5123 return MB_STRNICMP(x, y, len);
5124 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005126}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127
5128/*
5129 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005130 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 */
5132 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005133concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
5135 char_u *dest;
5136
5137 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5138 if (dest != NULL)
5139 {
5140 STRCPY(dest, fname1);
5141 if (sep)
5142 add_pathsep(dest);
5143 STRCAT(dest, fname2);
5144 }
5145 return dest;
5146}
5147
Bram Moolenaard6754642005-01-17 22:18:45 +00005148/*
5149 * Concatenate two strings and return the result in allocated memory.
5150 * Returns NULL when out of memory.
5151 */
5152 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005153concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005154{
5155 char_u *dest;
5156 size_t l = STRLEN(str1);
5157
5158 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5159 if (dest != NULL)
5160 {
5161 STRCPY(dest, str1);
5162 STRCPY(dest + l, str2);
5163 }
5164 return dest;
5165}
Bram Moolenaard6754642005-01-17 22:18:45 +00005166
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167/*
5168 * Add a path separator to a file name, unless it already ends in a path
5169 * separator.
5170 */
5171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005172add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005174 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 STRCAT(p, PATHSEPSTR);
5176}
5177
5178/*
5179 * FullName_save - Make an allocated copy of a full file name.
5180 * Returns NULL when out of memory.
5181 */
5182 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005183FullName_save(
5184 char_u *fname,
5185 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005186 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
5188 char_u *buf;
5189 char_u *new_fname = NULL;
5190
5191 if (fname == NULL)
5192 return NULL;
5193
5194 buf = alloc((unsigned)MAXPATHL);
5195 if (buf != NULL)
5196 {
5197 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5198 new_fname = vim_strsave(buf);
5199 else
5200 new_fname = vim_strsave(fname);
5201 vim_free(buf);
5202 }
5203 return new_fname;
5204}
5205
5206#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5207
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005208static char_u *skip_string(char_u *p);
5209static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005210static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005211static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213/*
5214 * Find the start of a comment, not knowing if we are in a comment right now.
5215 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005216 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005218 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005219ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005220{
5221 return find_start_comment(curbuf->b_ind_maxcomment);
5222}
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005225find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
5227 pos_T *pos;
5228 char_u *line;
5229 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005232 for (;;)
5233 {
5234 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5235 if (pos == NULL)
5236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005238 /*
5239 * Check if the comment start we found is inside a string.
5240 * If it is then restrict the search to below this line and try again.
5241 */
5242 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005243 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005244 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005245 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005246 break;
5247 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5248 if (cur_maxcomment <= 0)
5249 {
5250 pos = NULL;
5251 break;
5252 }
5253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return pos;
5255}
5256
5257/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005258 * Find the start of a comment or raw string, not knowing if we are in a
5259 * comment or raw string right now.
5260 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005261 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005262 * Return NULL when not inside a comment or raw string.
5263 * "CORS" -> Comment Or Raw String
5264 */
5265 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005266ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005267{
Bram Moolenaar089af182015-10-07 11:41:49 +02005268 static pos_T comment_pos_copy;
5269 pos_T *comment_pos;
5270 pos_T *rs_pos;
5271
5272 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5273 if (comment_pos != NULL)
5274 {
5275 /* Need to make a copy of the static pos in findmatchlimit(),
5276 * calling find_start_rawstring() may change it. */
5277 comment_pos_copy = *comment_pos;
5278 comment_pos = &comment_pos_copy;
5279 }
5280 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005281
5282 /* If comment_pos is before rs_pos the raw string is inside the comment.
5283 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005284 if (comment_pos == NULL || (rs_pos != NULL
5285 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005286 {
5287 if (is_raw != NULL && rs_pos != NULL)
5288 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005289 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005290 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005291 return comment_pos;
5292}
5293
5294/*
5295 * Find the start of a raw string, not knowing if we are in one right now.
5296 * Search starts at w_cursor.lnum and goes backwards.
5297 * Return NULL when not inside a raw string.
5298 */
5299 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005300find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005301{
5302 pos_T *pos;
5303 char_u *line;
5304 char_u *p;
5305 int cur_maxcomment = ind_maxcomment;
5306
5307 for (;;)
5308 {
5309 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5310 if (pos == NULL)
5311 break;
5312
5313 /*
5314 * Check if the raw string start we found is inside a string.
5315 * If it is then restrict the search to below this line and try again.
5316 */
5317 line = ml_get(pos->lnum);
5318 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5319 p = skip_string(p);
5320 if ((colnr_T)(p - line) <= pos->col)
5321 break;
5322 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5323 if (cur_maxcomment <= 0)
5324 {
5325 pos = NULL;
5326 break;
5327 }
5328 }
5329 return pos;
5330}
5331
5332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 * Skip to the end of a "string" and a 'c' character.
5334 * If there is no string or character, return argument unmodified.
5335 */
5336 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005337skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 int i;
5340
5341 /*
5342 * We loop, because strings may be concatenated: "date""time".
5343 */
5344 for ( ; ; ++p)
5345 {
5346 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5347 {
5348 if (!p[1]) /* ' at end of line */
5349 break;
5350 i = 2;
5351 if (p[1] == '\\') /* '\n' or '\000' */
5352 {
5353 ++i;
5354 while (vim_isdigit(p[i - 1])) /* '\000' */
5355 ++i;
5356 }
5357 if (p[i] == '\'') /* check for trailing ' */
5358 {
5359 p += i;
5360 continue;
5361 }
5362 }
5363 else if (p[0] == '"') /* start of string */
5364 {
5365 for (++p; p[0]; ++p)
5366 {
5367 if (p[0] == '\\' && p[1] != NUL)
5368 ++p;
5369 else if (p[0] == '"') /* end of string */
5370 break;
5371 }
5372 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005373 continue; /* continue for another string */
5374 }
5375 else if (p[0] == 'R' && p[1] == '"')
5376 {
5377 /* Raw string: R"[delim](...)[delim]" */
5378 char_u *delim = p + 2;
5379 char_u *paren = vim_strchr(delim, '(');
5380
5381 if (paren != NULL)
5382 {
5383 size_t delim_len = paren - delim;
5384
5385 for (p += 3; *p; ++p)
5386 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5387 && p[delim_len + 1] == '"')
5388 {
5389 p += delim_len + 1;
5390 break;
5391 }
5392 if (p[0] == '"')
5393 continue; /* continue for another string */
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
5396 break; /* no string found */
5397 }
5398 if (!*p)
5399 --p; /* backup from NUL */
5400 return p;
5401}
5402#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5403
5404#if defined(FEAT_CINDENT) || defined(PROTO)
5405
5406/*
5407 * Do C or expression indenting on the current line.
5408 */
5409 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005410do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412# ifdef FEAT_EVAL
5413 if (*curbuf->b_p_inde != NUL)
5414 fixthisline(get_expr_indent);
5415 else
5416# endif
5417 fixthisline(get_c_indent);
5418}
5419
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005420/* Find result cache for cpp_baseclass */
5421typedef struct {
5422 int found;
5423 lpos_T lpos;
5424} cpp_baseclass_cache_T;
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426/*
5427 * Functions for C-indenting.
5428 * Most of this originally comes from Eric Fischer.
5429 */
5430/*
5431 * Below "XXX" means that this function may unlock the current line.
5432 */
5433
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005434static char_u *cin_skipcomment(char_u *);
5435static int cin_nocode(char_u *);
5436static pos_T *find_line_comment(void);
5437static int cin_has_js_key(char_u *text);
5438static int cin_islabel_skip(char_u **);
5439static int cin_isdefault(char_u *);
5440static char_u *after_label(char_u *l);
5441static int get_indent_nolabel(linenr_T lnum);
5442static int skip_label(linenr_T, char_u **pp);
5443static int cin_first_id_amount(void);
5444static int cin_get_equal_amount(linenr_T lnum);
5445static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005446static int cin_iscomment(char_u *);
5447static int cin_islinecomment(char_u *);
5448static int cin_isterminated(char_u *, int, int);
5449static int cin_isinit(void);
5450static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5451static int cin_isif(char_u *);
5452static int cin_iselse(char_u *);
5453static int cin_isdo(char_u *);
5454static int cin_iswhileofdo(char_u *, linenr_T);
5455static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5456static int cin_iswhileofdo_end(int terminated);
5457static int cin_isbreak(char_u *);
5458static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5459static int get_baseclass_amount(int col);
5460static int cin_ends_in(char_u *, char_u *, char_u *);
5461static int cin_starts_with(char_u *s, char *word);
5462static int cin_skip2pos(pos_T *trypos);
5463static pos_T *find_start_brace(void);
5464static pos_T *find_match_paren(int);
5465static pos_T *find_match_char(int c, int ind_maxparen);
5466static int corr_ind_maxparen(pos_T *startpos);
5467static int find_last_paren(char_u *l, int start, int end);
5468static int find_match(int lookfor, linenr_T ourscope);
5469static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
5471/*
5472 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005473 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 */
5475 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005476cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 while (*s)
5479 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005480 char_u *prev_s = s;
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005483
5484 /* Perl/shell # comment comment continues until eol. Require a space
5485 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005486 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005487 {
5488 s += STRLEN(s);
5489 break;
5490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (*s != '/')
5492 break;
5493 ++s;
5494 if (*s == '/') /* slash-slash comment continues till eol */
5495 {
5496 s += STRLEN(s);
5497 break;
5498 }
5499 if (*s != '*')
5500 break;
5501 for (++s; *s; ++s) /* skip slash-star comment */
5502 if (s[0] == '*' && s[1] == '/')
5503 {
5504 s += 2;
5505 break;
5506 }
5507 }
5508 return s;
5509}
5510
5511/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005512 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * not considered code.
5514 */
5515 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005516cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 return *cin_skipcomment(s) == NUL;
5519}
5520
5521/*
5522 * Check previous lines for a "//" line comment, skipping over blank lines.
5523 */
5524 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005525find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526{
5527 static pos_T pos;
5528 char_u *line;
5529 char_u *p;
5530
5531 pos = curwin->w_cursor;
5532 while (--pos.lnum > 0)
5533 {
5534 line = ml_get(pos.lnum);
5535 p = skipwhite(line);
5536 if (cin_islinecomment(p))
5537 {
5538 pos.col = (int)(p - line);
5539 return &pos;
5540 }
5541 if (*p != NUL)
5542 break;
5543 }
5544 return NULL;
5545}
5546
5547/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005548 * Return TRUE if "text" starts with "key:".
5549 */
5550 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005551cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005552{
5553 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005554 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005555
5556 if (*s == '\'' || *s == '"')
5557 {
5558 /* can be 'key': or "key": */
5559 quote = *s;
5560 ++s;
5561 }
5562 if (!vim_isIDc(*s)) /* need at least one ID character */
5563 return FALSE;
5564
5565 while (vim_isIDc(*s))
5566 ++s;
5567 if (*s == quote)
5568 ++s;
5569
5570 s = cin_skipcomment(s);
5571
5572 /* "::" is not a label, it's C++ */
5573 return (*s == ':' && s[1] != ':');
5574}
5575
5576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005578 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 */
5580 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 if (!vim_isIDc(**s)) /* need at least one ID character */
5584 return FALSE;
5585
5586 while (vim_isIDc(**s))
5587 (*s)++;
5588
5589 *s = cin_skipcomment(*s);
5590
5591 /* "::" is not a label, it's C++ */
5592 return (**s == ':' && *++*s != ':');
5593}
5594
5595/*
5596 * Recognize a label: "label:".
5597 * Note: curwin->w_cursor must be where we are looking for the label.
5598 */
5599 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005600cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602 char_u *s;
5603
5604 s = cin_skipcomment(ml_get_curline());
5605
5606 /*
5607 * Exclude "default" from labels, since it should be indented
5608 * like a switch label. Same for C++ scope declarations.
5609 */
5610 if (cin_isdefault(s))
5611 return FALSE;
5612 if (cin_isscopedecl(s))
5613 return FALSE;
5614
5615 if (cin_islabel_skip(&s))
5616 {
5617 /*
5618 * Only accept a label if the previous line is terminated or is a case
5619 * label.
5620 */
5621 pos_T cursor_save;
5622 pos_T *trypos;
5623 char_u *line;
5624
5625 cursor_save = curwin->w_cursor;
5626 while (curwin->w_cursor.lnum > 1)
5627 {
5628 --curwin->w_cursor.lnum;
5629
5630 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005631 * If we're in a comment or raw string now, skip to the start of
5632 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 */
5634 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005635 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 curwin->w_cursor = *trypos;
5637
5638 line = ml_get_curline();
5639 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5640 continue;
5641 if (*(line = cin_skipcomment(line)) == NUL)
5642 continue;
5643
5644 curwin->w_cursor = cursor_save;
5645 if (cin_isterminated(line, TRUE, FALSE)
5646 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005647 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 || (cin_islabel_skip(&line) && cin_nocode(line)))
5649 return TRUE;
5650 return FALSE;
5651 }
5652 curwin->w_cursor = cursor_save;
5653 return TRUE; /* label at start of file??? */
5654 }
5655 return FALSE;
5656}
5657
5658/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005659 * Recognize structure initialization and enumerations:
5660 * "[typedef] [static|public|protected|private] enum"
5661 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 */
5663 static int
5664cin_isinit(void)
5665{
5666 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005667 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 s = cin_skipcomment(ml_get_curline());
5670
Bram Moolenaar75342212013-03-07 13:13:52 +01005671 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 s = cin_skipcomment(s + 7);
5673
Bram Moolenaar75342212013-03-07 13:13:52 +01005674 for (;;)
5675 {
5676 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005677
Bram Moolenaar75342212013-03-07 13:13:52 +01005678 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5679 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005680 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005681 if (cin_starts_with(s, skip[i]))
5682 {
5683 s = cin_skipcomment(s + l);
5684 l = 0;
5685 break;
5686 }
5687 }
5688 if (l != 0)
5689 break;
5690 }
5691
5692 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 return TRUE;
5694
5695 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5696 return TRUE;
5697
5698 return FALSE;
5699}
5700
5701/*
5702 * Recognize a switch label: "case .*:" or "default:".
5703 */
5704 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005705cin_iscase(
5706 char_u *s,
5707 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005710 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
5712 for (s += 4; *s; ++s)
5713 {
5714 s = cin_skipcomment(s);
5715 if (*s == ':')
5716 {
5717 if (s[1] == ':') /* skip over "::" for C++ */
5718 ++s;
5719 else
5720 return TRUE;
5721 }
5722 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005723 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5725 return FALSE; /* stop at comment */
5726 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005727 {
5728 /* JS etc. */
5729 if (strict)
5730 return FALSE; /* stop at string */
5731 else
5732 return TRUE;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735 return FALSE;
5736 }
5737
5738 if (cin_isdefault(s))
5739 return TRUE;
5740 return FALSE;
5741}
5742
5743/*
5744 * Recognize a "default" switch label.
5745 */
5746 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005747cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 return (STRNCMP(s, "default", 7) == 0
5750 && *(s = cin_skipcomment(s + 7)) == ':'
5751 && s[1] != ':');
5752}
5753
5754/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005755 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 */
5757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005758cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759{
5760 int i;
5761
5762 s = cin_skipcomment(s);
5763 if (STRNCMP(s, "public", 6) == 0)
5764 i = 6;
5765 else if (STRNCMP(s, "protected", 9) == 0)
5766 i = 9;
5767 else if (STRNCMP(s, "private", 7) == 0)
5768 i = 7;
5769 else
5770 return FALSE;
5771 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5772}
5773
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005774/* Maximum number of lines to search back for a "namespace" line. */
5775#define FIND_NAMESPACE_LIM 20
5776
5777/*
5778 * Recognize a "namespace" scope declaration.
5779 */
5780 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005781cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005782{
5783 char_u *p;
5784 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005785 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005786
5787 s = cin_skipcomment(s);
5788 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5789 {
5790 p = cin_skipcomment(skipwhite(s + 9));
5791 while (*p != NUL)
5792 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005793 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005794 {
5795 has_name = TRUE; /* found end of a name */
5796 p = cin_skipcomment(skipwhite(p));
5797 }
5798 else if (*p == '{')
5799 {
5800 break;
5801 }
5802 else if (vim_iswordc(*p))
5803 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005804 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005805 if (has_name)
5806 return FALSE; /* word character after skipping past name */
5807 ++p;
5808 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005809 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5810 {
5811 if (!has_name_start || has_name)
5812 return FALSE;
5813 /* C++ 17 nested namespace */
5814 p += 3;
5815 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005816 else
5817 {
5818 return FALSE;
5819 }
5820 }
5821 return TRUE;
5822 }
5823 return FALSE;
5824}
5825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005827 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5828 */
5829 static int
5830cin_is_cpp_extern_c(char_u *s)
5831{
5832 char_u *p;
5833 int has_string_literal = FALSE;
5834
5835 s = cin_skipcomment(s);
5836 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5837 {
5838 p = cin_skipcomment(skipwhite(s + 6));
5839 while (*p != NUL)
5840 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005841 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005842 {
5843 p = cin_skipcomment(skipwhite(p));
5844 }
5845 else if (*p == '{')
5846 {
5847 break;
5848 }
5849 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5850 {
5851 if (has_string_literal)
5852 return FALSE;
5853 has_string_literal = TRUE;
5854 p += 3;
5855 }
5856 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5857 && p[4] == '"')
5858 {
5859 if (has_string_literal)
5860 return FALSE;
5861 has_string_literal = TRUE;
5862 p += 5;
5863 }
5864 else
5865 {
5866 return FALSE;
5867 }
5868 }
5869 return has_string_literal ? TRUE : FALSE;
5870 }
5871 return FALSE;
5872}
5873
5874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 * Return a pointer to the first non-empty non-comment character after a ':'.
5876 * Return NULL if not found.
5877 * case 234: a = b;
5878 * ^
5879 */
5880 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005881after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
5883 for ( ; *l; ++l)
5884 {
5885 if (*l == ':')
5886 {
5887 if (l[1] == ':') /* skip over "::" for C++ */
5888 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005889 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 break;
5891 }
5892 else if (*l == '\'' && l[1] && l[2] == '\'')
5893 l += 2; /* skip over 'x' */
5894 }
5895 if (*l == NUL)
5896 return NULL;
5897 l = cin_skipcomment(l + 1);
5898 if (*l == NUL)
5899 return NULL;
5900 return l;
5901}
5902
5903/*
5904 * Get indent of line "lnum", skipping a label.
5905 * Return 0 if there is nothing after the label.
5906 */
5907 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005908get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 char_u *l;
5911 pos_T fp;
5912 colnr_T col;
5913 char_u *p;
5914
5915 l = ml_get(lnum);
5916 p = after_label(l);
5917 if (p == NULL)
5918 return 0;
5919
5920 fp.col = (colnr_T)(p - l);
5921 fp.lnum = lnum;
5922 getvcol(curwin, &fp, &col, NULL, NULL);
5923 return (int)col;
5924}
5925
5926/*
5927 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005928 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 * label: if (asdf && asdfasdf)
5930 * ^
5931 */
5932 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005933skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 char_u *l;
5936 int amount;
5937 pos_T cursor_save;
5938
5939 cursor_save = curwin->w_cursor;
5940 curwin->w_cursor.lnum = lnum;
5941 l = ml_get_curline();
5942 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005943 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {
5945 amount = get_indent_nolabel(lnum);
5946 l = after_label(ml_get_curline());
5947 if (l == NULL) /* just in case */
5948 l = ml_get_curline();
5949 }
5950 else
5951 {
5952 amount = get_indent();
5953 l = ml_get_curline();
5954 }
5955 *pp = l;
5956
5957 curwin->w_cursor = cursor_save;
5958 return amount;
5959}
5960
5961/*
5962 * Return the indent of the first variable name after a type in a declaration.
5963 * int a, indent of "a"
5964 * static struct foo b, indent of "b"
5965 * enum bla c, indent of "c"
5966 * Returns zero when it doesn't look like a declaration.
5967 */
5968 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005969cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 char_u *line, *p, *s;
5972 int len;
5973 pos_T fp;
5974 colnr_T col;
5975
5976 line = ml_get_curline();
5977 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005978 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5980 {
5981 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005982 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 }
5984 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5985 p = skipwhite(p + 6);
5986 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5987 p = skipwhite(p + 4);
5988 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5989 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5990 {
5991 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01005992 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
5993 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
5994 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
5995 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 p = s;
5997 }
5998 for (len = 0; vim_isIDc(p[len]); ++len)
5999 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006000 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 return 0;
6002
6003 p = skipwhite(p + len);
6004 fp.lnum = curwin->w_cursor.lnum;
6005 fp.col = (colnr_T)(p - line);
6006 getvcol(curwin, &fp, &col, NULL, NULL);
6007 return (int)col;
6008}
6009
6010/*
6011 * Return the indent of the first non-blank after an equal sign.
6012 * char *foo = "here";
6013 * Return zero if no (useful) equal sign found.
6014 * Return -1 if the line above "lnum" ends in a backslash.
6015 * foo = "asdf\
6016 * asdf\
6017 * here";
6018 */
6019 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006020cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 char_u *line;
6023 char_u *s;
6024 colnr_T col;
6025 pos_T fp;
6026
6027 if (lnum > 1)
6028 {
6029 line = ml_get(lnum - 1);
6030 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6031 return -1;
6032 }
6033
6034 line = s = ml_get(lnum);
6035 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6036 {
6037 if (cin_iscomment(s)) /* ignore comments */
6038 s = cin_skipcomment(s);
6039 else
6040 ++s;
6041 }
6042 if (*s != '=')
6043 return 0;
6044
6045 s = skipwhite(s + 1);
6046 if (cin_nocode(s))
6047 return 0;
6048
6049 if (*s == '"') /* nice alignment for continued strings */
6050 ++s;
6051
6052 fp.lnum = lnum;
6053 fp.col = (colnr_T)(s - line);
6054 getvcol(curwin, &fp, &col, NULL, NULL);
6055 return (int)col;
6056}
6057
6058/*
6059 * Recognize a preprocessor statement: Any line that starts with '#'.
6060 */
6061 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006062cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006064 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 return TRUE;
6066 return FALSE;
6067}
6068
6069/*
6070 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6071 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6072 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006073 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 */
6075 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006076cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 char_u *line = *pp;
6079 linenr_T lnum = *lnump;
6080 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006081 int candidate_amount = *amount;
6082
6083 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6084 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006086 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 {
6088 if (cin_ispreproc(line))
6089 {
6090 retval = TRUE;
6091 *lnump = lnum;
6092 break;
6093 }
6094 if (lnum == 1)
6095 break;
6096 line = ml_get(--lnum);
6097 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6098 break;
6099 }
6100
6101 if (lnum != *lnump)
6102 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006103 if (retval)
6104 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 return retval;
6106}
6107
6108/*
6109 * Recognize the start of a C or C++ comment.
6110 */
6111 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006112cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6115}
6116
6117/*
6118 * Recognize the start of a "//" comment.
6119 */
6120 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006121cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 return (p[0] == '/' && p[1] == '/');
6124}
6125
6126/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006127 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6128 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006130 * If a line begins with an "else", only consider it terminated if no unmatched
6131 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 * Return the character terminating the line (ending char's have precedence if
6133 * both apply in order to determine initializations).
6134 */
6135 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006136cin_isterminated(
6137 char_u *s,
6138 int incl_open, /* include '{' at the end as terminator */
6139 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006141 char_u found_start = 0;
6142 unsigned n_open = 0;
6143 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
6145 s = cin_skipcomment(s);
6146
6147 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6148 found_start = *s;
6149
Bram Moolenaar496f9512011-05-19 16:35:09 +02006150 if (!found_start)
6151 is_else = cin_iselse(s);
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 while (*s)
6154 {
6155 /* skip over comments, "" strings and 'c'haracters */
6156 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006157 if (*s == '}' && n_open > 0)
6158 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006159 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006160 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 && cin_nocode(s + 1))
6162 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006163 else if (*s == '{')
6164 {
6165 if (incl_open && cin_nocode(s + 1))
6166 return *s;
6167 else
6168 ++n_open;
6169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170
6171 if (*s)
6172 s++;
6173 }
6174 return found_start;
6175}
6176
6177/*
6178 * Recognize the basic picture of a function declaration -- it needs to
6179 * have an open paren somewhere and a close paren at the end of the line and
6180 * no semicolons anywhere.
6181 * When a line ends in a comma we continue looking in the next line.
6182 * "sp" points to a string with the line. When looking at other lines it must
6183 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006184 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006185 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 */
6187 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006188cin_isfuncdecl(
6189 char_u **sp,
6190 linenr_T first_lnum,
6191 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
6193 char_u *s;
6194 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006195 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006197 pos_T *trypos;
6198 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199
6200 if (sp == NULL)
6201 s = ml_get(lnum);
6202 else
6203 s = *sp;
6204
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006205 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006206 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006207 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006208 {
6209 lnum = trypos->lnum;
6210 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006211 {
6212 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006213 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006214 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006215
6216 s = ml_get(lnum);
6217 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006218 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006219
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006220 /* Ignore line starting with #. */
6221 if (cin_ispreproc(s))
6222 return FALSE;
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6225 {
6226 if (cin_iscomment(s)) /* ignore comments */
6227 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006228 else if (*s == ':')
6229 {
6230 if (*(s + 1) == ':')
6231 s += 2;
6232 else
6233 /* To avoid a mistake in the following situation:
6234 * A::A(int a, int b)
6235 * : a(0) // <--not a function decl
6236 * , b(0)
6237 * {...
6238 */
6239 return FALSE;
6240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 else
6242 ++s;
6243 }
6244 if (*s != '(')
6245 return FALSE; /* ';', ' or " before any () or no '(' */
6246
6247 while (*s && *s != ';' && *s != '\'' && *s != '"')
6248 {
6249 if (*s == ')' && cin_nocode(s + 1))
6250 {
6251 /* ')' at the end: may have found a match
6252 * Check for he previous line not to end in a backslash:
6253 * #if defined(x) && \
6254 * defined(y)
6255 */
6256 lnum = first_lnum - 1;
6257 s = ml_get(lnum);
6258 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6259 retval = TRUE;
6260 goto done;
6261 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006262 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006264 int comma = (*s == ',');
6265
6266 /* ',' at the end: continue looking in the next line.
6267 * At the end: check for ',' in the next line, for this style:
6268 * func(arg1
6269 * , arg2) */
6270 for (;;)
6271 {
6272 if (lnum >= curbuf->b_ml.ml_line_count)
6273 break;
6274 s = ml_get(++lnum);
6275 if (!cin_ispreproc(s))
6276 break;
6277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (lnum >= curbuf->b_ml.ml_line_count)
6279 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006280 /* Require a comma at end of the line or a comma or ')' at the
6281 * start of next line. */
6282 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006283 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006284 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006285 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
6287 else if (cin_iscomment(s)) /* ignore comments */
6288 s = cin_skipcomment(s);
6289 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006292 just_started = FALSE;
6293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 }
6295
6296done:
6297 if (lnum != first_lnum && sp != NULL)
6298 *sp = ml_get(first_lnum);
6299
6300 return retval;
6301}
6302
6303 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006304cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006306 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307}
6308
6309 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006310cin_iselse(
6311 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 if (*p == '}') /* accept "} else" */
6314 p = cin_skipcomment(p + 1);
6315 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6316}
6317
6318 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006319cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6322}
6323
6324/*
6325 * Check if this is a "while" that should have a matching "do".
6326 * We only accept a "while (condition) ;", with only white space between the
6327 * ')' and ';'. The condition may be spread over several lines.
6328 */
6329 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006330cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 pos_T cursor_save;
6333 pos_T *trypos;
6334 int retval = FALSE;
6335
6336 p = cin_skipcomment(p);
6337 if (*p == '}') /* accept "} while (cond);" */
6338 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006339 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341 cursor_save = curwin->w_cursor;
6342 curwin->w_cursor.lnum = lnum;
6343 curwin->w_cursor.col = 0;
6344 p = ml_get_curline();
6345 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6346 {
6347 ++p;
6348 ++curwin->w_cursor.col;
6349 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006350 if ((trypos = findmatchlimit(NULL, 0, 0,
6351 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6353 retval = TRUE;
6354 curwin->w_cursor = cursor_save;
6355 }
6356 return retval;
6357}
6358
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006359/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006360 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006361 * Return 0 if there is none.
6362 * Otherwise return !0 and update "*poffset" to point to the place where the
6363 * string was found.
6364 */
6365 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006367{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006368 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006369
6370 if (offset-- < 2)
6371 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006372 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006373 --offset;
6374
6375 offset -= 1;
6376 if (!STRNCMP(line + offset, "if", 2))
6377 goto probablyFound;
6378
6379 if (offset >= 1)
6380 {
6381 offset -= 1;
6382 if (!STRNCMP(line + offset, "for", 3))
6383 goto probablyFound;
6384
6385 if (offset >= 2)
6386 {
6387 offset -= 2;
6388 if (!STRNCMP(line + offset, "while", 5))
6389 goto probablyFound;
6390 }
6391 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006392 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006393
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006394probablyFound:
6395 if (!offset || !vim_isIDc(line[offset - 1]))
6396 {
6397 *poffset = offset;
6398 return 1;
6399 }
6400 return 0;
6401}
6402
6403/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006404 * Return TRUE if we are at the end of a do-while.
6405 * do
6406 * nothing;
6407 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006408 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006409 * Adjust the cursor to the line with "while".
6410 */
6411 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006412cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006413{
6414 char_u *line;
6415 char_u *p;
6416 char_u *s;
6417 pos_T *trypos;
6418 int i;
6419
6420 if (terminated != ';') /* there must be a ';' at the end */
6421 return FALSE;
6422
6423 p = line = ml_get_curline();
6424 while (*p != NUL)
6425 {
6426 p = cin_skipcomment(p);
6427 if (*p == ')')
6428 {
6429 s = skipwhite(p + 1);
6430 if (*s == ';' && cin_nocode(s + 1))
6431 {
6432 /* Found ");" at end of the line, now check there is "while"
6433 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006434 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006435 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006436 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006437 if (trypos != NULL)
6438 {
6439 s = cin_skipcomment(ml_get(trypos->lnum));
6440 if (*s == '}') /* accept "} while (cond);" */
6441 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006442 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006443 {
6444 curwin->w_cursor.lnum = trypos->lnum;
6445 return TRUE;
6446 }
6447 }
6448
6449 /* Searching may have made "line" invalid, get it again. */
6450 line = ml_get_curline();
6451 p = line + i;
6452 }
6453 }
6454 if (*p != NUL)
6455 ++p;
6456 }
6457 return FALSE;
6458}
6459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006461cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
6463 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6464}
6465
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006466/*
6467 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 * constructor-initialization. eg:
6469 *
6470 * class MyClass :
6471 * baseClass <-- here
6472 * class MyClass : public baseClass,
6473 * anotherBaseClass <-- here (should probably lineup ??)
6474 * MyClass::MyClass(...) :
6475 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006476 *
6477 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 */
6479 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006480cin_is_cpp_baseclass(
6481 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006483 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 char_u *s;
6485 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006486 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006487 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006489 if (pos->lnum <= lnum)
6490 return cached->found; /* Use the cached result */
6491
6492 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006494 s = skipwhite(line);
6495 if (*s == '#') /* skip #define FOO x ? (x) : x */
6496 return FALSE;
6497 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if (*s == NUL)
6499 return FALSE;
6500
6501 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6502
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006503 /* Search for a line starting with '#', empty, ending in ';' or containing
6504 * '{' or '}' and start below it. This handles the following situations:
6505 * a = cond ?
6506 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006507 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006508 * func::foo()
6509 * : something
6510 * {}
6511 * Foo::Foo (int one, int two)
6512 * : something(4),
6513 * somethingelse(3)
6514 * {}
6515 */
6516 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006518 line = ml_get(lnum - 1);
6519 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006520 if (*s == '#' || *s == NUL)
6521 break;
6522 while (*s != NUL)
6523 {
6524 s = cin_skipcomment(s);
6525 if (*s == '{' || *s == '}'
6526 || (*s == ';' && cin_nocode(s + 1)))
6527 break;
6528 if (*s != NUL)
6529 ++s;
6530 }
6531 if (*s != NUL)
6532 break;
6533 --lnum;
6534 }
6535
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006536 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006537 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006538 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006539 for (;;)
6540 {
6541 if (*s == NUL)
6542 {
6543 if (lnum == curwin->w_cursor.lnum)
6544 break;
6545 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006546 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006547 s = line;
6548 }
6549 if (s == line)
6550 {
6551 /* don't recognize "case (foo):" as a baseclass */
6552 if (cin_iscase(s, FALSE))
6553 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006554 s = cin_skipcomment(line);
6555 if (*s == NUL)
6556 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006557 }
6558
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006559 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006560 s = skip_string(s) + 1;
6561 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 {
6563 if (s[1] == ':')
6564 {
6565 /* skip double colon. It can't be a constructor
6566 * initialization any more */
6567 lookfor_ctor_init = FALSE;
6568 s = cin_skipcomment(s + 2);
6569 }
6570 else if (lookfor_ctor_init || class_or_struct)
6571 {
6572 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006573 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 cpp_base_class = TRUE;
6575 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006576 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 s = cin_skipcomment(s + 1);
6578 }
6579 else
6580 s = cin_skipcomment(s + 1);
6581 }
6582 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6583 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6584 {
6585 class_or_struct = TRUE;
6586 lookfor_ctor_init = FALSE;
6587
6588 if (*s == 'c')
6589 s = cin_skipcomment(s + 5);
6590 else
6591 s = cin_skipcomment(s + 6);
6592 }
6593 else
6594 {
6595 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6596 {
6597 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6598 }
6599 else if (s[0] == ')')
6600 {
6601 /* Constructor-initialization is assumed if we come across
6602 * something like "):" */
6603 class_or_struct = FALSE;
6604 lookfor_ctor_init = TRUE;
6605 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006606 else if (s[0] == '?')
6607 {
6608 /* Avoid seeing '() :' after '?' as constructor init. */
6609 return FALSE;
6610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 else if (!vim_isIDc(s[0]))
6612 {
6613 /* if it is not an identifier, we are wrong */
6614 class_or_struct = FALSE;
6615 lookfor_ctor_init = FALSE;
6616 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006617 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
6619 /* it can't be a constructor-initialization any more */
6620 lookfor_ctor_init = FALSE;
6621
6622 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006623 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006624 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 }
6626
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006627 /* When the line ends in a comma don't align with it. */
6628 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006629 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006630
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 s = cin_skipcomment(s + 1);
6632 }
6633 }
6634
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006635 cached->found = cpp_base_class;
6636 if (cpp_base_class)
6637 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 return cpp_base_class;
6639}
6640
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006641 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006642get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006643{
6644 int amount;
6645 colnr_T vcol;
6646 pos_T *trypos;
6647
6648 if (col == 0)
6649 {
6650 amount = get_indent();
6651 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006652 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006653 amount = get_indent_lnum(trypos->lnum); /* XXX */
6654 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006655 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006656 }
6657 else
6658 {
6659 curwin->w_cursor.col = col;
6660 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6661 amount = (int)vcol;
6662 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006663 if (amount < curbuf->b_ind_cpp_baseclass)
6664 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006665 return amount;
6666}
6667
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668/*
6669 * Return TRUE if string "s" ends with the string "find", possibly followed by
6670 * white space and comments. Skip strings and comments.
6671 * Ignore "ignore" after "find" if it's not NULL.
6672 */
6673 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006674cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 char_u *p = s;
6677 char_u *r;
6678 int len = (int)STRLEN(find);
6679
6680 while (*p != NUL)
6681 {
6682 p = cin_skipcomment(p);
6683 if (STRNCMP(p, find, len) == 0)
6684 {
6685 r = skipwhite(p + len);
6686 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6687 r = skipwhite(r + STRLEN(ignore));
6688 if (cin_nocode(r))
6689 return TRUE;
6690 }
6691 if (*p != NUL)
6692 ++p;
6693 }
6694 return FALSE;
6695}
6696
6697/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006698 * Return TRUE when "s" starts with "word" and then a non-ID character.
6699 */
6700 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006702{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006703 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006704
6705 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6706}
6707
6708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 * Skip strings, chars and comments until at or past "trypos".
6710 * Return the column found.
6711 */
6712 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006713cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 char_u *line;
6716 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006717 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 p = line = ml_get(trypos->lnum);
6720 while (*p && (colnr_T)(p - line) < trypos->col)
6721 {
6722 if (cin_iscomment(p))
6723 p = cin_skipcomment(p);
6724 else
6725 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006726 new_p = skip_string(p);
6727 if (new_p == p)
6728 ++p;
6729 else
6730 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
6732 }
6733 return (int)(p - line);
6734}
6735
6736/*
6737 * Find the '{' at the start of the block we are in.
6738 * Return NULL if no match found.
6739 * Ignore a '{' that is in a comment, makes indenting the next three lines
6740 * work. */
6741/* foo() */
6742/* { */
6743/* } */
6744
6745 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006746find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
6748 pos_T cursor_save;
6749 pos_T *trypos;
6750 pos_T *pos;
6751 static pos_T pos_copy;
6752
6753 cursor_save = curwin->w_cursor;
6754 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6755 {
6756 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6757 trypos = &pos_copy;
6758 curwin->w_cursor = *trypos;
6759 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006760 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006762 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 break;
6764 if (pos != NULL)
6765 curwin->w_cursor.lnum = pos->lnum;
6766 }
6767 curwin->w_cursor = cursor_save;
6768 return trypos;
6769}
6770
6771/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006772 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006773 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 */
6775 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006776find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006778 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006779}
6780
6781 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006782find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006783{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 pos_T cursor_save;
6785 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006786 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006787 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
6789 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006790 ind_maxp_wk = ind_maxparen;
6791retry:
6792 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 /* check if the ( is in a // comment */
6795 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006796 {
6797 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6798 if (ind_maxp_wk > 0)
6799 {
6800 curwin->w_cursor = *trypos;
6801 curwin->w_cursor.col = 0; /* XXX */
6802 goto retry;
6803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 else
6807 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006808 pos_T *trypos_wk;
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6811 trypos = &pos_copy;
6812 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006813 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006814 {
6815 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6816 - trypos_wk->lnum);
6817 if (ind_maxp_wk > 0)
6818 {
6819 curwin->w_cursor = *trypos_wk;
6820 goto retry;
6821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
6825 }
6826 curwin->w_cursor = cursor_save;
6827 return trypos;
6828}
6829
6830/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006831 * Find the matching '(', ignoring it if it is in a comment or before an
6832 * unmatched {.
6833 * Return NULL if no match found.
6834 */
6835 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006836find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006837{
6838 pos_T *trypos = find_match_paren(ind_maxparen);
6839
6840 if (trypos != NULL)
6841 {
6842 pos_T *tryposBrace = find_start_brace();
6843
6844 /* If both an unmatched '(' and '{' is found. Ignore the '('
6845 * position if the '{' is further down. */
6846 if (tryposBrace != NULL
6847 && (trypos->lnum != tryposBrace->lnum
6848 ? trypos->lnum < tryposBrace->lnum
6849 : trypos->col < tryposBrace->col))
6850 trypos = NULL;
6851 }
6852 return trypos;
6853}
6854
6855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 * Return ind_maxparen corrected for the difference in line number between the
6857 * cursor position and "startpos". This makes sure that searching for a
6858 * matching paren above the cursor line doesn't find a match because of
6859 * looking a few lines further.
6860 */
6861 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6865
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006866 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6867 return curbuf->b_ind_maxparen - (int)n;
6868 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869}
6870
6871/*
6872 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006873 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 */
6875 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006876find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 int i;
6879 int retval = FALSE;
6880 int open_count = 0;
6881
6882 curwin->w_cursor.col = 0; /* default is start of line */
6883
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006884 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6887 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6888 if (l[i] == start)
6889 ++open_count;
6890 else if (l[i] == end)
6891 {
6892 if (open_count > 0)
6893 --open_count;
6894 else
6895 {
6896 curwin->w_cursor.col = i;
6897 retval = TRUE;
6898 }
6899 }
6900 }
6901 return retval;
6902}
6903
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006904/*
6905 * Parse 'cinoptions' and set the values in "curbuf".
6906 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6907 */
6908 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006909parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006910{
6911 char_u *p;
6912 char_u *l;
6913 char_u *digits;
6914 int n;
6915 int divider;
6916 int fraction = 0;
6917 int sw = (int)get_sw_value(buf);
6918
6919 /*
6920 * Set the default values.
6921 */
6922 /* Spaces from a block's opening brace the prevailing indent for that
6923 * block should be. */
6924 buf->b_ind_level = sw;
6925
6926 /* Spaces from the edge of the line an open brace that's at the end of a
6927 * line is imagined to be. */
6928 buf->b_ind_open_imag = 0;
6929
6930 /* Spaces from the prevailing indent for a line that is not preceded by
6931 * an opening brace. */
6932 buf->b_ind_no_brace = 0;
6933
6934 /* Column where the first { of a function should be located }. */
6935 buf->b_ind_first_open = 0;
6936
6937 /* Spaces from the prevailing indent a leftmost open brace should be
6938 * located. */
6939 buf->b_ind_open_extra = 0;
6940
6941 /* Spaces from the matching open brace (real location for one at the left
6942 * edge; imaginary location from one that ends a line) the matching close
6943 * brace should be located. */
6944 buf->b_ind_close_extra = 0;
6945
6946 /* Spaces from the edge of the line an open brace sitting in the leftmost
6947 * column is imagined to be. */
6948 buf->b_ind_open_left_imag = 0;
6949
6950 /* Spaces jump labels should be shifted to the left if N is non-negative,
6951 * otherwise the jump label will be put to column 1. */
6952 buf->b_ind_jump_label = -1;
6953
6954 /* Spaces from the switch() indent a "case xx" label should be located. */
6955 buf->b_ind_case = sw;
6956
6957 /* Spaces from the "case xx:" code after a switch() should be located. */
6958 buf->b_ind_case_code = sw;
6959
6960 /* Lineup break at end of case in switch() with case label. */
6961 buf->b_ind_case_break = 0;
6962
6963 /* Spaces from the class declaration indent a scope declaration label
6964 * should be located. */
6965 buf->b_ind_scopedecl = sw;
6966
6967 /* Spaces from the scope declaration label code should be located. */
6968 buf->b_ind_scopedecl_code = sw;
6969
6970 /* Amount K&R-style parameters should be indented. */
6971 buf->b_ind_param = sw;
6972
6973 /* Amount a function type spec should be indented. */
6974 buf->b_ind_func_type = sw;
6975
6976 /* Amount a cpp base class declaration or constructor initialization
6977 * should be indented. */
6978 buf->b_ind_cpp_baseclass = sw;
6979
6980 /* additional spaces beyond the prevailing indent a continuation line
6981 * should be located. */
6982 buf->b_ind_continuation = sw;
6983
6984 /* Spaces from the indent of the line with an unclosed parentheses. */
6985 buf->b_ind_unclosed = sw * 2;
6986
6987 /* Spaces from the indent of the line with an unclosed parentheses, which
6988 * itself is also unclosed. */
6989 buf->b_ind_unclosed2 = sw;
6990
6991 /* Suppress ignoring spaces from the indent of a line starting with an
6992 * unclosed parentheses. */
6993 buf->b_ind_unclosed_noignore = 0;
6994
6995 /* If the opening paren is the last nonwhite character on the line, and
6996 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6997 * context (for very long lines). */
6998 buf->b_ind_unclosed_wrapped = 0;
6999
7000 /* Suppress ignoring white space when lining up with the character after
7001 * an unclosed parentheses. */
7002 buf->b_ind_unclosed_whiteok = 0;
7003
7004 /* Indent a closing parentheses under the line start of the matching
7005 * opening parentheses. */
7006 buf->b_ind_matching_paren = 0;
7007
7008 /* Indent a closing parentheses under the previous line. */
7009 buf->b_ind_paren_prev = 0;
7010
7011 /* Extra indent for comments. */
7012 buf->b_ind_comment = 0;
7013
7014 /* Spaces from the comment opener when there is nothing after it. */
7015 buf->b_ind_in_comment = 3;
7016
7017 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7018 * after the comment opener. */
7019 buf->b_ind_in_comment2 = 0;
7020
7021 /* Max lines to search for an open paren. */
7022 buf->b_ind_maxparen = 20;
7023
7024 /* Max lines to search for an open comment. */
7025 buf->b_ind_maxcomment = 70;
7026
7027 /* Handle braces for java code. */
7028 buf->b_ind_java = 0;
7029
7030 /* Not to confuse JS object properties with labels. */
7031 buf->b_ind_js = 0;
7032
7033 /* Handle blocked cases correctly. */
7034 buf->b_ind_keep_case_label = 0;
7035
7036 /* Handle C++ namespace. */
7037 buf->b_ind_cpp_namespace = 0;
7038
7039 /* Handle continuation lines containing conditions of if(), for() and
7040 * while(). */
7041 buf->b_ind_if_for_while = 0;
7042
Bram Moolenaar6b643942017-03-05 19:44:06 +01007043 /* indentation for # comments */
7044 buf->b_ind_hash_comment = 0;
7045
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007046 /* Handle C++ extern "C" or "C++" */
7047 buf->b_ind_cpp_extern_c = 0;
7048
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007049 for (p = buf->b_p_cino; *p; )
7050 {
7051 l = p++;
7052 if (*p == '-')
7053 ++p;
7054 digits = p; /* remember where the digits start */
7055 n = getdigits(&p);
7056 divider = 0;
7057 if (*p == '.') /* ".5s" means a fraction */
7058 {
7059 fraction = atol((char *)++p);
7060 while (VIM_ISDIGIT(*p))
7061 {
7062 ++p;
7063 if (divider)
7064 divider *= 10;
7065 else
7066 divider = 10;
7067 }
7068 }
7069 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7070 {
7071 if (p == digits)
7072 n = sw; /* just "s" is one 'shiftwidth' */
7073 else
7074 {
7075 n *= sw;
7076 if (divider)
7077 n += (sw * fraction + divider / 2) / divider;
7078 }
7079 ++p;
7080 }
7081 if (l[1] == '-')
7082 n = -n;
7083
7084 /* When adding an entry here, also update the default 'cinoptions' in
7085 * doc/indent.txt, and add explanation for it! */
7086 switch (*l)
7087 {
7088 case '>': buf->b_ind_level = n; break;
7089 case 'e': buf->b_ind_open_imag = n; break;
7090 case 'n': buf->b_ind_no_brace = n; break;
7091 case 'f': buf->b_ind_first_open = n; break;
7092 case '{': buf->b_ind_open_extra = n; break;
7093 case '}': buf->b_ind_close_extra = n; break;
7094 case '^': buf->b_ind_open_left_imag = n; break;
7095 case 'L': buf->b_ind_jump_label = n; break;
7096 case ':': buf->b_ind_case = n; break;
7097 case '=': buf->b_ind_case_code = n; break;
7098 case 'b': buf->b_ind_case_break = n; break;
7099 case 'p': buf->b_ind_param = n; break;
7100 case 't': buf->b_ind_func_type = n; break;
7101 case '/': buf->b_ind_comment = n; break;
7102 case 'c': buf->b_ind_in_comment = n; break;
7103 case 'C': buf->b_ind_in_comment2 = n; break;
7104 case 'i': buf->b_ind_cpp_baseclass = n; break;
7105 case '+': buf->b_ind_continuation = n; break;
7106 case '(': buf->b_ind_unclosed = n; break;
7107 case 'u': buf->b_ind_unclosed2 = n; break;
7108 case 'U': buf->b_ind_unclosed_noignore = n; break;
7109 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7110 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7111 case 'm': buf->b_ind_matching_paren = n; break;
7112 case 'M': buf->b_ind_paren_prev = n; break;
7113 case ')': buf->b_ind_maxparen = n; break;
7114 case '*': buf->b_ind_maxcomment = n; break;
7115 case 'g': buf->b_ind_scopedecl = n; break;
7116 case 'h': buf->b_ind_scopedecl_code = n; break;
7117 case 'j': buf->b_ind_java = n; break;
7118 case 'J': buf->b_ind_js = n; break;
7119 case 'l': buf->b_ind_keep_case_label = n; break;
7120 case '#': buf->b_ind_hash_comment = n; break;
7121 case 'N': buf->b_ind_cpp_namespace = n; break;
7122 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007123 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007124 }
7125 if (*p == ',')
7126 ++p;
7127 }
7128}
7129
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007130/*
7131 * Return the desired indent for C code.
7132 * Return -1 if the indent should be left alone (inside a raw string).
7133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007135get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 pos_T cur_curpos;
7138 int amount;
7139 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007140 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 colnr_T col;
7142 char_u *theline;
7143 char_u *linecopy;
7144 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007145 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007147 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 pos_T our_paren_pos;
7149 char_u *start;
7150 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007151#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152#define BRACE_AT_START 2 /* '{' is at start of line */
7153#define BRACE_AT_END 3 /* '{' is at end of line */
7154 linenr_T ourscope;
7155 char_u *l;
7156 char_u *look;
7157 char_u terminated;
7158 int lookfor;
7159#define LOOKFOR_INITIAL 0
7160#define LOOKFOR_IF 1
7161#define LOOKFOR_DO 2
7162#define LOOKFOR_CASE 3
7163#define LOOKFOR_ANY 4
7164#define LOOKFOR_TERM 5
7165#define LOOKFOR_UNTERM 6
7166#define LOOKFOR_SCOPEDECL 7
7167#define LOOKFOR_NOBREAK 8
7168#define LOOKFOR_CPP_BASECLASS 9
7169#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007170#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007171#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
7173 int whilelevel;
7174 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 int n;
7176 int iscase;
7177 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007178 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007180 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007181 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007182 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007183 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007184 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007186 /* make a copy, value is changed below */
7187 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
7189 /* remember where the cursor was when we started */
7190 cur_curpos = curwin->w_cursor;
7191
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007192 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007193 if (cur_curpos.lnum == 1)
7194 return 0;
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 /* Get a copy of the current contents of the line.
7197 * This is required, because only the most recent line obtained with
7198 * ml_get is valid! */
7199 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7200 if (linecopy == NULL)
7201 return 0;
7202
7203 /*
7204 * In insert mode and the cursor is on a ')' truncate the line at the
7205 * cursor position. We don't want to line up with the matching '(' when
7206 * inserting new stuff.
7207 * For unknown reasons the cursor might be past the end of the line, thus
7208 * check for that.
7209 */
7210 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007211 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 && linecopy[curwin->w_cursor.col] == ')')
7213 linecopy[curwin->w_cursor.col] = NUL;
7214
7215 theline = skipwhite(linecopy);
7216
7217 /* move the cursor to the start of the line */
7218
7219 curwin->w_cursor.col = 0;
7220
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007221 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007224 * If we are inside a raw string don't change the indent.
7225 * Ignore a raw string inside a comment.
7226 */
7227 comment_pos = ind_find_start_comment();
7228 if (comment_pos != NULL)
7229 {
7230 /* findmatchlimit() static pos is overwritten, make a copy */
7231 tryposCopy = *comment_pos;
7232 comment_pos = &tryposCopy;
7233 }
7234 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007235 if (trypos != NULL && (comment_pos == NULL
7236 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007237 {
7238 amount = -1;
7239 goto laterend;
7240 }
7241
7242 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 * #defines and so on always go at the left when included in 'cinkeys'.
7244 */
7245 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007246 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007247 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007248 goto theend;
7249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007252 * Is it a non-case label? Then that goes at the left margin too unless:
7253 * - JS flag is set.
7254 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007256 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007257 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007260 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
7262
7263 /*
7264 * If we're inside a "//" comment and there is a "//" comment in a
7265 * previous line, lineup with that one.
7266 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007267 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 && (trypos = find_line_comment()) != NULL) /* XXX */
7269 {
7270 /* find how indented the line beginning the comment is */
7271 getvcol(curwin, trypos, &col, NULL, NULL);
7272 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007273 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 }
7275
7276 /*
7277 * If we're inside a comment and not looking at the start of the
7278 * comment, try using the 'comments' option.
7279 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007280 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
7282 int lead_start_len = 2;
7283 int lead_middle_len = 1;
7284 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7285 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7286 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7287 char_u *p;
7288 int start_align = 0;
7289 int start_off = 0;
7290 int done = FALSE;
7291
7292 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007293 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007295 *lead_start = NUL;
7296 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
7298 p = curbuf->b_p_com;
7299 while (*p != NUL)
7300 {
7301 int align = 0;
7302 int off = 0;
7303 int what = 0;
7304
7305 while (*p != NUL && *p != ':')
7306 {
7307 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7308 what = *p++;
7309 else if (*p == COM_LEFT || *p == COM_RIGHT)
7310 align = *p++;
7311 else if (VIM_ISDIGIT(*p) || *p == '-')
7312 off = getdigits(&p);
7313 else
7314 ++p;
7315 }
7316
7317 if (*p == ':')
7318 ++p;
7319 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7320 if (what == COM_START)
7321 {
7322 STRCPY(lead_start, lead_end);
7323 lead_start_len = (int)STRLEN(lead_start);
7324 start_off = off;
7325 start_align = align;
7326 }
7327 else if (what == COM_MIDDLE)
7328 {
7329 STRCPY(lead_middle, lead_end);
7330 lead_middle_len = (int)STRLEN(lead_middle);
7331 }
7332 else if (what == COM_END)
7333 {
7334 /* If our line starts with the middle comment string, line it
7335 * up with the comment opener per the 'comments' option. */
7336 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7337 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7338 {
7339 done = TRUE;
7340 if (curwin->w_cursor.lnum > 1)
7341 {
7342 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007343 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 * the middle comment string matches in the previous
7345 * line, use the indent of that line. XXX */
7346 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7347 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7348 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7349 else if (STRNCMP(look, lead_middle,
7350 lead_middle_len) == 0)
7351 {
7352 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7353 break;
7354 }
7355 /* If the start comment string doesn't match with the
7356 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007357 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 lead_start, lead_start_len) != 0)
7359 continue;
7360 }
7361 if (start_off != 0)
7362 amount += start_off;
7363 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007364 amount += vim_strsize(lead_start)
7365 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 break;
7367 }
7368
7369 /* If our line starts with the end comment string, line it up
7370 * with the middle comment */
7371 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7372 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7373 {
7374 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7375 /* XXX */
7376 if (off != 0)
7377 amount += off;
7378 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007379 amount += vim_strsize(lead_start)
7380 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 done = TRUE;
7382 break;
7383 }
7384 }
7385 }
7386
7387 /* If our line starts with an asterisk, line up with the
7388 * asterisk in the comment opener; otherwise, line up
7389 * with the first character of the comment text.
7390 */
7391 if (done)
7392 ;
7393 else if (theline[0] == '*')
7394 amount += 1;
7395 else
7396 {
7397 /*
7398 * If we are more than one line away from the comment opener, take
7399 * the indent of the previous non-empty line. If 'cino' has "CO"
7400 * and we are just below the comment opener and there are any
7401 * white characters after it line up with the text after it;
7402 * otherwise, add the amount specified by "c" in 'cino'
7403 */
7404 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007405 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {
7407 if (linewhite(lnum)) /* skip blank lines */
7408 continue;
7409 amount = get_indent_lnum(lnum); /* XXX */
7410 break;
7411 }
7412 if (amount == -1) /* use the comment opener */
7413 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007414 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007416 start = ml_get(comment_pos->lnum);
7417 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007419 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007421 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007423 if (curbuf->b_ind_in_comment2 || *look == NUL)
7424 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007427 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429
7430 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007431 * Are we looking at a ']' that has a match?
7432 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007433 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007434 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7435 {
7436 /* align with the line containing the '['. */
7437 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007438 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007439 }
7440
7441 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 * Are we inside parentheses or braces?
7443 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007444 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007445 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007446 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 || trypos != NULL)
7448 {
7449 if (trypos != NULL && tryposBrace != NULL)
7450 {
7451 /* Both an unmatched '(' and '{' is found. Use the one which is
7452 * closer to the current cursor position, set the other to NULL. */
7453 if (trypos->lnum != tryposBrace->lnum
7454 ? trypos->lnum < tryposBrace->lnum
7455 : trypos->col < tryposBrace->col)
7456 trypos = NULL;
7457 else
7458 tryposBrace = NULL;
7459 }
7460
7461 if (trypos != NULL)
7462 {
7463 /*
7464 * If the matching paren is more than one line away, use the indent of
7465 * a previous non-empty line that matches the same paren.
7466 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007467 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007469 /* Line up with the start of the matching paren line. */
7470 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7471 }
7472 else
7473 {
7474 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007475 our_paren_pos = *trypos;
7476 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007478 l = skipwhite(ml_get(lnum));
7479 if (cin_nocode(l)) /* skip comment lines */
7480 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007481 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007482 continue; /* ignore #define, #if, etc. */
7483 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007485 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007486 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007487 {
7488 lnum = trypos->lnum + 1;
7489 continue;
7490 }
7491
7492 /* XXX */
7493 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007494 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007495 && trypos->lnum == our_paren_pos.lnum
7496 && trypos->col == our_paren_pos.col)
7497 {
7498 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007500 if (theline[0] == ')')
7501 {
7502 if (our_paren_pos.lnum != lnum
7503 && cur_amount > amount)
7504 cur_amount = amount;
7505 amount = -1;
7506 }
7507 break;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 }
7511
7512 /*
7513 * Line up with line where the matching paren is. XXX
7514 * If the line starts with a '(' or the indent for unclosed
7515 * parentheses is zero, line up with the unclosed parentheses.
7516 */
7517 if (amount == -1)
7518 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007519 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007520 int is_if_for_while = 0;
7521
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007522 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007523 {
7524 /* Look for the outermost opening parenthesis on this line
7525 * and check whether it belongs to an "if", "for" or "while". */
7526
7527 pos_T cursor_save = curwin->w_cursor;
7528 pos_T outermost;
7529 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007530
7531 trypos = &our_paren_pos;
7532 do {
7533 outermost = *trypos;
7534 curwin->w_cursor.lnum = outermost.lnum;
7535 curwin->w_cursor.col = outermost.col;
7536
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007537 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007538 } while (trypos && trypos->lnum == outermost.lnum);
7539
7540 curwin->w_cursor = cursor_save;
7541
7542 line = ml_get(outermost.lnum);
7543
7544 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007545 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007546 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007547
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007548 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007549 look = skipwhite(look);
7550 if (*look == '(')
7551 {
7552 linenr_T save_lnum = curwin->w_cursor.lnum;
7553 char_u *line;
7554 int look_col;
7555
7556 /* Ignore a '(' in front of the line that has a match before
7557 * our matching '('. */
7558 curwin->w_cursor.lnum = our_paren_pos.lnum;
7559 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007560 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007561 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007562 if ((trypos = findmatchlimit(NULL, ')', 0,
7563 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007564 != NULL
7565 && trypos->lnum == our_paren_pos.lnum
7566 && trypos->col < our_paren_pos.col)
7567 ignore_paren_col = trypos->col + 1;
7568
7569 curwin->w_cursor.lnum = save_lnum;
7570 look = ml_get(our_paren_pos.lnum) + look_col;
7571 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007572 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7573 && is_if_for_while == 0)
7574 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007575 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 {
7577 /*
7578 * If we're looking at a close paren, line up right there;
7579 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007580 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 * the last nonwhite character of the line, use either the
7582 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007583 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 * lines).
7585 */
7586 if (theline[0] != ')')
7587 {
7588 cur_amount = MAXCOL;
7589 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007590 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 && cin_ends_in(l, (char_u *)"(", NULL))
7592 {
7593 /* look for opening unmatched paren, indent one level
7594 * for each additional level */
7595 n = 1;
7596 for (col = 0; col < our_paren_pos.col; ++col)
7597 {
7598 switch (l[col])
7599 {
7600 case '(':
7601 case '{': ++n;
7602 break;
7603
7604 case ')':
7605 case '}': if (n > 1)
7606 --n;
7607 break;
7608 }
7609 }
7610
7611 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007612 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007614 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 our_paren_pos.col++;
7616 else
7617 {
7618 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007619 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 col++;
7621 if (l[col] != NUL) /* In case of trailing space */
7622 our_paren_pos.col = col;
7623 else
7624 our_paren_pos.col++;
7625 }
7626 }
7627
7628 /*
7629 * Find how indented the paren is, or the character after it
7630 * if we did the above "if".
7631 */
7632 if (our_paren_pos.col > 0)
7633 {
7634 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7635 if (cur_amount > (int)col)
7636 cur_amount = col;
7637 }
7638 }
7639
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007640 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {
7642 /* Line up with the start of the matching paren line. */
7643 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007644 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7645 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007646 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 {
7648 if (cur_amount != MAXCOL)
7649 amount = cur_amount;
7650 }
7651 else
7652 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007653 /* Add b_ind_unclosed2 for each '(' before our matching one,
7654 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007656 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
7658 --our_paren_pos.col;
7659 switch (*ml_get_pos(&our_paren_pos))
7660 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007661 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 col = our_paren_pos.col;
7663 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007664 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 col = MAXCOL;
7666 break;
7667 }
7668 }
7669
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007670 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 * braces */
7672 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007673 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 else
7675 {
7676 curwin->w_cursor.lnum = our_paren_pos.lnum;
7677 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007678 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7679 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007680 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007682 {
7683 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007684 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007685 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007686 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689 /*
7690 * For a line starting with ')' use the minimum of the two
7691 * positions, to avoid giving it more indent than the previous
7692 * lines:
7693 * func_long_name( if (x
7694 * arg && yy
7695 * ) ^ not here ) ^ not here
7696 */
7697 if (cur_amount < amount)
7698 amount = cur_amount;
7699 }
7700 }
7701
7702 /* add extra indent for a comment */
7703 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007704 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 else
7707 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007708 /*
7709 * We are inside braces, there is a { before this line at the position
7710 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007711 * Make a copy of tryposBrace, it may point to pos_copy inside
7712 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007713 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007714 tryposCopy = *tryposBrace;
7715 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 ourscope = trypos->lnum;
7718 start = ml_get(ourscope);
7719
7720 /*
7721 * Now figure out how indented the line is in general.
7722 * If the brace was at the start of the line, we use that;
7723 * otherwise, check out the indentation of the line as
7724 * a whole and then add the "imaginary indent" to that.
7725 */
7726 look = skipwhite(start);
7727 if (*look == '{')
7728 {
7729 getvcol(curwin, trypos, &col, NULL, NULL);
7730 amount = col;
7731 if (*start == '{')
7732 start_brace = BRACE_IN_COL0;
7733 else
7734 start_brace = BRACE_AT_START;
7735 }
7736 else
7737 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007738 /* That opening brace might have been on a continuation
7739 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 curwin->w_cursor.lnum = ourscope;
7741
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007742 /* Position the cursor over the rightmost paren, so that
7743 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 lnum = ourscope;
7745 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007746 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7747 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 lnum = trypos->lnum;
7749
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007750 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 * case 1: if (asdf &&
7752 * ldfd) {
7753 * }
7754 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007755 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7756 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007758 else if (curbuf->b_ind_js)
7759 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007761 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
7763 start_brace = BRACE_AT_END;
7764 }
7765
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007766 /* For Javascript check if the line starts with "key:". */
7767 if (curbuf->b_ind_js)
7768 js_cur_has_key = cin_has_js_key(theline);
7769
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007771 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 * we want to be. otherwise, add the amount of room
7773 * that an indent is supposed to be.
7774 */
7775 if (theline[0] == '}')
7776 {
7777 /*
7778 * they may want closing braces to line up with something
7779 * other than the open brace. indulge them, if so.
7780 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007781 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783 else
7784 {
7785 /*
7786 * If we're looking at an "else", try to find an "if"
7787 * to match it with.
7788 * If we're looking at a "while", try to find a "do"
7789 * to match it with.
7790 */
7791 lookfor = LOOKFOR_INITIAL;
7792 if (cin_iselse(theline))
7793 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007794 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 lookfor = LOOKFOR_DO;
7796 if (lookfor != LOOKFOR_INITIAL)
7797 {
7798 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007799 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {
7801 amount = get_indent(); /* XXX */
7802 goto theend;
7803 }
7804 }
7805
7806 /*
7807 * We get here if we are not on an "while-of-do" or "else" (or
7808 * failed to find a matching "if").
7809 * Search backwards for something to line up with.
7810 * First set amount for when we don't find anything.
7811 */
7812
7813 /*
7814 * if the '{' is _really_ at the left margin, use the imaginary
7815 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007816 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 */
7818
7819 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7820 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007821 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007822 lookfor_cpp_namespace = TRUE;
7823 }
7824 else if (start_brace == BRACE_AT_START &&
7825 lookfor_cpp_namespace) /* '{' is at start */
7826 {
7827
7828 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830 else
7831 {
7832 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007833 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007834 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007835
7836 l = skipwhite(ml_get_curline());
7837 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007838 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007839 else if (cin_is_cpp_extern_c(l))
7840 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 else
7843 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007844 /* Compensate for adding b_ind_open_extra later. */
7845 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 if (amount < 0)
7847 amount = 0;
7848 }
7849 }
7850
7851 lookfor_break = FALSE;
7852
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007853 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
7855 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007856 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 else if (cin_isscopedecl(theline)) /* private:, ... */
7859 {
7860 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007861 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 }
7863 else
7864 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007865 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7866 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 lookfor_break = TRUE;
7868
7869 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007870 /* b_ind_level from start of block */
7871 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873 scope_amount = amount;
7874 whilelevel = 0;
7875
7876 /*
7877 * Search backwards. If we find something we recognize, line up
7878 * with that.
7879 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007880 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 * the usual amount relative to the conditional
7882 * that opens the block.
7883 */
7884 curwin->w_cursor = cur_curpos;
7885 for (;;)
7886 {
7887 curwin->w_cursor.lnum--;
7888 curwin->w_cursor.col = 0;
7889
7890 /*
7891 * If we went all the way back to the start of our scope, line
7892 * up with it.
7893 */
7894 if (curwin->w_cursor.lnum <= ourscope)
7895 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007896 /* We reached end of scope:
7897 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007899 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 * don't add ind_continuation, otherwise it is a variable
7901 * declaration:
7902 * int x,
7903 * here; <-- add ind_continuation
7904 */
7905 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7906 {
7907 if (curwin->w_cursor.lnum == 0
7908 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007909 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007911 /* nothing found (abuse curbuf->b_ind_maxparen as
7912 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 * initialization) */
7914 if (cont_amount > 0)
7915 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007916 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 amount += ind_continuation;
7918 break;
7919 }
7920
7921 l = ml_get_curline();
7922
7923 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007924 * If we're in a comment or raw string now, skip to
7925 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007927 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 if (trypos != NULL)
7929 {
7930 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007931 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 continue;
7933 }
7934
7935 /*
7936 * Skip preprocessor directives and blank lines.
7937 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007938 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7939 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 continue;
7941
7942 if (cin_nocode(l))
7943 continue;
7944
7945 terminated = cin_isterminated(l, FALSE, TRUE);
7946
7947 /*
7948 * If we are at top level and the line looks like a
7949 * function declaration, we are done
7950 * (it's a variable declaration).
7951 */
7952 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007953 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
7955 /* if the line is terminated with another ','
7956 * it is a continued variable initialization.
7957 * don't add extra indent.
7958 * TODO: does not work, if a function
7959 * declaration is split over multiple lines:
7960 * cin_isfuncdecl returns FALSE then.
7961 */
7962 if (terminated == ',')
7963 break;
7964
7965 /* if it es a enum declaration or an assignment,
7966 * we are done.
7967 */
7968 if (terminated != ';' && cin_isinit())
7969 break;
7970
7971 /* nothing useful found */
7972 if (terminated == 0 || terminated == '{')
7973 continue;
7974 }
7975
7976 if (terminated != ';')
7977 {
7978 /* Skip parens and braces. Position the cursor
7979 * over the rightmost paren, so that matching it
7980 * will take us back to the start of the line.
7981 */ /* XXX */
7982 trypos = NULL;
7983 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007984 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007985 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
7987 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007988 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 if (trypos != NULL)
7991 {
7992 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007993 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 continue;
7995 }
7996 }
7997
7998 /* it's a variable declaration, add indentation
7999 * like in
8000 * int a,
8001 * b;
8002 */
8003 if (cont_amount > 0)
8004 amount = cont_amount;
8005 else
8006 amount += ind_continuation;
8007 }
8008 else if (lookfor == LOOKFOR_UNTERM)
8009 {
8010 if (cont_amount > 0)
8011 amount = cont_amount;
8012 else
8013 amount += ind_continuation;
8014 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008015 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008016 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008017 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008018 && lookfor != LOOKFOR_CPP_BASECLASS
8019 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008020 {
8021 amount = scope_amount;
8022 if (theline[0] == '{')
8023 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008024 amount += curbuf->b_ind_open_extra;
8025 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008026 }
8027 }
8028
8029 if (lookfor_cpp_namespace)
8030 {
8031 /*
8032 * Looking for C++ namespace, need to look further
8033 * back.
8034 */
8035 if (curwin->w_cursor.lnum == ourscope)
8036 continue;
8037
8038 if (curwin->w_cursor.lnum == 0
8039 || curwin->w_cursor.lnum
8040 < ourscope - FIND_NAMESPACE_LIM)
8041 break;
8042
8043 l = ml_get_curline();
8044
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008045 /* If we're in a comment or raw string now, skip
8046 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008047 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008048 if (trypos != NULL)
8049 {
8050 curwin->w_cursor.lnum = trypos->lnum + 1;
8051 curwin->w_cursor.col = 0;
8052 continue;
8053 }
8054
8055 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008056 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8057 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008058 continue;
8059
8060 /* Finally the actual check for "namespace". */
8061 if (cin_is_cpp_namespace(l))
8062 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008063 amount += curbuf->b_ind_cpp_namespace
8064 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008065 break;
8066 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008067 else if (cin_is_cpp_extern_c(l))
8068 {
8069 amount += curbuf->b_ind_cpp_extern_c
8070 - added_to_amount;
8071 break;
8072 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008073
8074 if (cin_nocode(l))
8075 continue;
8076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078 break;
8079 }
8080
8081 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008082 * If we're in a comment or raw string now, skip to the start
8083 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008085 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {
8087 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008088 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 continue;
8090 }
8091
8092 l = ml_get_curline();
8093
8094 /*
8095 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008096 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008098 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (iscase || cin_isscopedecl(l))
8100 {
8101 /* we are only looking for cpp base class
8102 * declaration/initialization any longer */
8103 if (lookfor == LOOKFOR_CPP_BASECLASS)
8104 break;
8105
8106 /* When looking for a "do" we are not interested in
8107 * labels. */
8108 if (whilelevel > 0)
8109 continue;
8110
8111 /*
8112 * case xx:
8113 * c = 99 + <- this indent plus continuation
8114 *-> here;
8115 */
8116 if (lookfor == LOOKFOR_UNTERM
8117 || lookfor == LOOKFOR_ENUM_OR_INIT)
8118 {
8119 if (cont_amount > 0)
8120 amount = cont_amount;
8121 else
8122 amount += ind_continuation;
8123 break;
8124 }
8125
8126 /*
8127 * case xx: <- line up with this case
8128 * x = 333;
8129 * case yy:
8130 */
8131 if ( (iscase && lookfor == LOOKFOR_CASE)
8132 || (iscase && lookfor_break)
8133 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8134 {
8135 /*
8136 * Check that this case label is not for another
8137 * switch()
8138 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008139 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008140 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
8142 amount = get_indent(); /* XXX */
8143 break;
8144 }
8145 continue;
8146 }
8147
8148 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8149
8150 /*
8151 * case xx: if (cond) <- line up with this if
8152 * y = y + 1;
8153 * -> s = 99;
8154 *
8155 * case xx:
8156 * if (cond) <- line up with this line
8157 * y = y + 1;
8158 * -> s = 99;
8159 */
8160 if (lookfor == LOOKFOR_TERM)
8161 {
8162 if (n)
8163 amount = n;
8164
8165 if (!lookfor_break)
8166 break;
8167 }
8168
8169 /*
8170 * case xx: x = x + 1; <- line up with this x
8171 * -> y = y + 1;
8172 *
8173 * case xx: if (cond) <- line up with this if
8174 * -> y = y + 1;
8175 */
8176 if (n)
8177 {
8178 amount = n;
8179 l = after_label(ml_get_curline());
8180 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008181 {
8182 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008183 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008184 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008185 amount += curbuf->b_ind_level
8186 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 break;
8189 }
8190
8191 /*
8192 * Try to get the indent of a statement before the switch
8193 * label. If nothing is found, line up relative to the
8194 * switch label.
8195 * break; <- may line up with this line
8196 * case xx:
8197 * -> y = 1;
8198 */
8199 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008200 ? curbuf->b_ind_case_code
8201 : curbuf->b_ind_scopedecl_code);
8202 lookfor = curbuf->b_ind_case_break
8203 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 continue;
8205 }
8206
8207 /*
8208 * Looking for a switch() label or C++ scope declaration,
8209 * ignore other lines, skip {}-blocks.
8210 */
8211 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8212 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008213 if (find_last_paren(l, '{', '}')
8214 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008217 curwin->w_cursor.col = 0;
8218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 continue;
8220 }
8221
8222 /*
8223 * Ignore jump labels with nothing after them.
8224 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008225 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {
8227 l = after_label(ml_get_curline());
8228 if (l == NULL || cin_nocode(l))
8229 continue;
8230 }
8231
8232 /*
8233 * Ignore #defines, #if, etc.
8234 * Ignore comment and empty lines.
8235 * (need to get the line again, cin_islabel() may have
8236 * unlocked it)
8237 */
8238 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008239 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 || cin_nocode(l))
8241 continue;
8242
8243 /*
8244 * Are we at the start of a cpp base class declaration or
8245 * constructor initialization?
8246 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008247 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008248 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008249 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008250 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008251 l = ml_get_curline();
8252 }
8253 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
8255 if (lookfor == LOOKFOR_UNTERM)
8256 {
8257 if (cont_amount > 0)
8258 amount = cont_amount;
8259 else
8260 amount += ind_continuation;
8261 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008262 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008264 /* Need to find start of the declaration. */
8265 lookfor = LOOKFOR_UNTERM;
8266 ind_continuation = 0;
8267 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 }
8269 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008270 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008271 amount = get_baseclass_amount(
8272 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 break;
8274 }
8275 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8276 {
8277 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008278 * declaration or initialization before the opening brace.
8279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (cin_isterminated(l, TRUE, FALSE))
8281 break;
8282 else
8283 continue;
8284 }
8285
8286 /*
8287 * What happens next depends on the line being terminated.
8288 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008289 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 * 123,
8291 * sizeof
8292 * here
8293 * Otherwise check whether it is a enumeration or structure
8294 * initialisation (not indented) or a variable declaration
8295 * (indented).
8296 */
8297 terminated = cin_isterminated(l, FALSE, TRUE);
8298
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008299 if (js_cur_has_key)
8300 {
8301 js_cur_has_key = 0; /* only check the first line */
8302 if (curbuf->b_ind_js && terminated == ',')
8303 {
8304 /* For Javascript we might be inside an object:
8305 * key: something, <- align with this
8306 * key: something
8307 * or:
8308 * key: something + <- align with this
8309 * something,
8310 * key: something
8311 */
8312 lookfor = LOOKFOR_JS_KEY;
8313 }
8314 }
8315 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8316 {
8317 amount = get_indent();
8318 break;
8319 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008320 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008321 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008322 if (tryposBrace != NULL && tryposBrace->lnum
8323 >= curwin->w_cursor.lnum)
8324 break;
8325 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008326 /* line below current line is the one that starts a
8327 * (possibly broken) line ending in a comma. */
8328 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008329 else
8330 {
8331 amount = get_indent();
8332 if (curwin->w_cursor.lnum - 1 == ourscope)
8333 /* line above is start of the scope, thus current
8334 * line is the one that stars a (possibly broken)
8335 * line ending in a comma. */
8336 break;
8337 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008338 }
8339
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8341 && terminated == ','))
8342 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008343 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8344 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008345 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 /*
8347 * if we're in the middle of a paren thing,
8348 * go back to the line that starts it so
8349 * we can get the right prevailing indent
8350 * if ( foo &&
8351 * bar )
8352 */
8353 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008354 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008356 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 */
8358 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008359 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008360 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8361 || (trypos->lnum == tryposBrace->lnum
8362 && trypos->col < tryposBrace->col)))
8363 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364
8365 /*
8366 * If we are looking for ',', we also look for matching
8367 * braces.
8368 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008369 if (trypos == NULL && terminated == ','
8370 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008371 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372
8373 if (trypos != NULL)
8374 {
8375 /*
8376 * Check if we are on a case label now. This is
8377 * handled above.
8378 * case xx: if ( asdf &&
8379 * asdf)
8380 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008381 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008383 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
8385 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008386 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 continue;
8388 }
8389 }
8390
8391 /*
8392 * Skip over continuation lines to find the one to get the
8393 * indent from
8394 * char *usethis = "bla\
8395 * bla",
8396 * here;
8397 */
8398 if (terminated == ',')
8399 {
8400 while (curwin->w_cursor.lnum > 1)
8401 {
8402 l = ml_get(curwin->w_cursor.lnum - 1);
8403 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8404 break;
8405 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008406 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 }
8408 }
8409
8410 /*
8411 * Get indent and pointer to text for current line,
8412 * ignoring any jump label. XXX
8413 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008414 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008415 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008416 else
8417 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 /*
8419 * If this is just above the line we are indenting, and it
8420 * starts with a '{', line it up with this line.
8421 * while (not)
8422 * -> {
8423 * }
8424 */
8425 if (terminated != ',' && lookfor != LOOKFOR_TERM
8426 && theline[0] == '{')
8427 {
8428 amount = cur_amount;
8429 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008430 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 * doesn't start with a '{', which must have a match
8432 * in the same line (scope is the same). Probably:
8433 * { 1, 2 },
8434 * -> { 3, 4 }
8435 */
8436 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008437 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008439 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
8441 /* have to look back, whether it is a cpp base
8442 * class declaration or initialization */
8443 lookfor = LOOKFOR_CPP_BASECLASS;
8444 continue;
8445 }
8446 break;
8447 }
8448
8449 /*
8450 * Check if we are after an "if", "while", etc.
8451 * Also allow " } else".
8452 */
8453 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8454 {
8455 /*
8456 * Found an unterminated line after an if (), line up
8457 * with the last one.
8458 * if (cond)
8459 * 100 +
8460 * -> here;
8461 */
8462 if (lookfor == LOOKFOR_UNTERM
8463 || lookfor == LOOKFOR_ENUM_OR_INIT)
8464 {
8465 if (cont_amount > 0)
8466 amount = cont_amount;
8467 else
8468 amount += ind_continuation;
8469 break;
8470 }
8471
8472 /*
8473 * If this is just above the line we are indenting, we
8474 * are finished.
8475 * while (not)
8476 * -> here;
8477 * Otherwise this indent can be used when the line
8478 * before this is terminated.
8479 * yyy;
8480 * if (stat)
8481 * while (not)
8482 * xxx;
8483 * -> here;
8484 */
8485 amount = cur_amount;
8486 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008487 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 if (lookfor != LOOKFOR_TERM)
8489 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008490 amount += curbuf->b_ind_level
8491 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 break;
8493 }
8494
8495 /*
8496 * Special trick: when expecting the while () after a
8497 * do, line up with the while()
8498 * do
8499 * x = 1;
8500 * -> here
8501 */
8502 l = skipwhite(ml_get_curline());
8503 if (cin_isdo(l))
8504 {
8505 if (whilelevel == 0)
8506 break;
8507 --whilelevel;
8508 }
8509
8510 /*
8511 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008512 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 * Need to use the scope of this "else". XXX
8514 * If whilelevel != 0 continue looking for a "do {".
8515 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008516 if (cin_iselse(l) && whilelevel == 0)
8517 {
8518 /* If we're looking at "} else", let's make sure we
8519 * find the opening brace of the enclosing scope,
8520 * not the one from "if () {". */
8521 if (*l == '}')
8522 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008523 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008524
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008525 if ((trypos = find_start_brace()) == NULL
8526 || find_match(LOOKFOR_IF, trypos->lnum)
8527 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008528 break;
8529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 }
8531
8532 /*
8533 * If we're below an unterminated line that is not an
8534 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008535 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 * the line before this one.
8537 */
8538 else
8539 {
8540 /*
8541 * Found two unterminated lines on a row, line up with
8542 * the last one.
8543 * c = 99 +
8544 * 100 +
8545 * -> here;
8546 */
8547 if (lookfor == LOOKFOR_UNTERM)
8548 {
8549 /* When line ends in a comma add extra indent */
8550 if (terminated == ',')
8551 amount += ind_continuation;
8552 break;
8553 }
8554
8555 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8556 {
8557 /* Found two lines ending in ',', lineup with the
8558 * lowest one, but check for cpp base class
8559 * declaration/initialization, if it is an
8560 * opening brace or we are looking just for
8561 * enumerations/initializations. */
8562 if (terminated == ',')
8563 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008564 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 break;
8566
8567 lookfor = LOOKFOR_CPP_BASECLASS;
8568 continue;
8569 }
8570
8571 /* Ignore unterminated lines in between, but
8572 * reduce indent. */
8573 if (amount > cur_amount)
8574 amount = cur_amount;
8575 }
8576 else
8577 {
8578 /*
8579 * Found first unterminated line on a row, may
8580 * line up with this line, remember its indent
8581 * 100 +
8582 * -> here;
8583 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008584 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008586
8587 n = (int)STRLEN(l);
8588 if (terminated == ',' && (*skipwhite(l) == ']'
8589 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
8592 /*
8593 * If previous line ends in ',', check whether we
8594 * are in an initialization or enum
8595 * struct xxx =
8596 * {
8597 * sizeof a,
8598 * 124 };
8599 * or a normal possible continuation line.
8600 * but only, of no other statement has been found
8601 * yet.
8602 */
8603 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8604 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008605 if (curbuf->b_ind_js)
8606 {
8607 /* Search for a line ending in a comma
8608 * and line up with the line below it
8609 * (could be the current line).
8610 * some = [
8611 * 1, <- line up here
8612 * 2,
8613 * some = [
8614 * 3 + <- line up here
8615 * 4 *
8616 * 5,
8617 * 6,
8618 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008619 if (cin_iscomment(skipwhite(l)))
8620 break;
8621 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008622 trypos = find_match_char('[',
8623 curbuf->b_ind_maxparen);
8624 if (trypos != NULL)
8625 {
8626 if (trypos->lnum
8627 == curwin->w_cursor.lnum - 1)
8628 {
8629 /* Current line is first inside
8630 * [], line up with it. */
8631 break;
8632 }
8633 ourscope = trypos->lnum;
8634 }
8635 }
8636 else
8637 {
8638 lookfor = LOOKFOR_ENUM_OR_INIT;
8639 cont_amount = cin_first_id_amount();
8640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642 else
8643 {
8644 if (lookfor == LOOKFOR_INITIAL
8645 && *l != NUL
8646 && l[STRLEN(l) - 1] == '\\')
8647 /* XXX */
8648 cont_amount = cin_get_equal_amount(
8649 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008650 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008651 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008652 && lookfor != LOOKFOR_COMMA
8653 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 lookfor = LOOKFOR_UNTERM;
8655 }
8656 }
8657 }
8658 }
8659
8660 /*
8661 * Check if we are after a while (cond);
8662 * If so: Ignore until the matching "do".
8663 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008664 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 {
8666 /*
8667 * Found an unterminated line after a while ();, line up
8668 * with the last one.
8669 * while (cond);
8670 * 100 + <- line up with this one
8671 * -> here;
8672 */
8673 if (lookfor == LOOKFOR_UNTERM
8674 || lookfor == LOOKFOR_ENUM_OR_INIT)
8675 {
8676 if (cont_amount > 0)
8677 amount = cont_amount;
8678 else
8679 amount += ind_continuation;
8680 break;
8681 }
8682
8683 if (whilelevel == 0)
8684 {
8685 lookfor = LOOKFOR_TERM;
8686 amount = get_indent(); /* XXX */
8687 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008688 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 }
8690 ++whilelevel;
8691 }
8692
8693 /*
8694 * We are after a "normal" statement.
8695 * If we had another statement we can stop now and use the
8696 * indent of that other statement.
8697 * Otherwise the indent of the current statement may be used,
8698 * search backwards for the next "normal" statement.
8699 */
8700 else
8701 {
8702 /*
8703 * Skip single break line, if before a switch label. It
8704 * may be lined up with the case label.
8705 */
8706 if (lookfor == LOOKFOR_NOBREAK
8707 && cin_isbreak(skipwhite(ml_get_curline())))
8708 {
8709 lookfor = LOOKFOR_ANY;
8710 continue;
8711 }
8712
8713 /*
8714 * Handle "do {" line.
8715 */
8716 if (whilelevel > 0)
8717 {
8718 l = cin_skipcomment(ml_get_curline());
8719 if (cin_isdo(l))
8720 {
8721 amount = get_indent(); /* XXX */
8722 --whilelevel;
8723 continue;
8724 }
8725 }
8726
8727 /*
8728 * Found a terminated line above an unterminated line. Add
8729 * the amount for a continuation line.
8730 * x = 1;
8731 * y = foo +
8732 * -> here;
8733 * or
8734 * int x = 1;
8735 * int foo,
8736 * -> here;
8737 */
8738 if (lookfor == LOOKFOR_UNTERM
8739 || lookfor == LOOKFOR_ENUM_OR_INIT)
8740 {
8741 if (cont_amount > 0)
8742 amount = cont_amount;
8743 else
8744 amount += ind_continuation;
8745 break;
8746 }
8747
8748 /*
8749 * Found a terminated line above a terminated line or "if"
8750 * etc. line. Use the amount of the line below us.
8751 * x = 1; x = 1;
8752 * if (asdf) y = 2;
8753 * while (asdf) ->here;
8754 * here;
8755 * ->foo;
8756 */
8757 if (lookfor == LOOKFOR_TERM)
8758 {
8759 if (!lookfor_break && whilelevel == 0)
8760 break;
8761 }
8762
8763 /*
8764 * First line above the one we're indenting is terminated.
8765 * To know what needs to be done look further backward for
8766 * a terminated line.
8767 */
8768 else
8769 {
8770 /*
8771 * position the cursor over the rightmost paren, so
8772 * that matching it will take us back to the start of
8773 * the line. Helps for:
8774 * func(asdr,
8775 * asdfasdf);
8776 * here;
8777 */
8778term_again:
8779 l = ml_get_curline();
8780 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008781 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008782 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {
8784 /*
8785 * Check if we are on a case label now. This is
8786 * handled above.
8787 * case xx: if ( asdf &&
8788 * asdf)
8789 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008790 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008792 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
8794 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008795 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 continue;
8797 }
8798 }
8799
8800 /* When aligning with the case statement, don't align
8801 * with a statement after it.
8802 * case 1: { <-- don't use this { position
8803 * stat;
8804 * }
8805 * case 2:
8806 * stat;
8807 * }
8808 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008809 iscase = (curbuf->b_ind_keep_case_label
8810 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811
8812 /*
8813 * Get indent and pointer to text for current line,
8814 * ignoring any jump label.
8815 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008816 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817
8818 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008819 amount += curbuf->b_ind_open_extra;
8820 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008821 l = skipwhite(l);
8822 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008823 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8825
8826 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008827 * When a terminated line starts with "else" skip to
8828 * the matching "if":
8829 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008830 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008831 * Need to use the scope of this "else". XXX
8832 * If whilelevel != 0 continue looking for a "do {".
8833 */
8834 if (lookfor == LOOKFOR_TERM
8835 && *l != '}'
8836 && cin_iselse(l)
8837 && whilelevel == 0)
8838 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008839 if ((trypos = find_start_brace()) == NULL
8840 || find_match(LOOKFOR_IF, trypos->lnum)
8841 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008842 break;
8843 continue;
8844 }
8845
8846 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 * If we're at the end of a block, skip to the start of
8848 * that block.
8849 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008850 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008851 if (find_last_paren(l, '{', '}') /* XXX */
8852 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008854 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 /* if not "else {" check for terminated again */
8856 /* but skip block for "} else {" */
8857 l = cin_skipcomment(ml_get_curline());
8858 if (*l == '}' || !cin_iselse(l))
8859 goto term_again;
8860 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008861 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 }
8863 }
8864 }
8865 }
8866 }
8867 }
8868
8869 /* add extra indent for a comment */
8870 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008871 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008872
8873 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008874 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8875 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008876
8877 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008879
8880 /*
8881 * ok -- we're not inside any sort of structure at all!
8882 *
8883 * This means we're at the top level, and everything should
8884 * basically just match where the previous line is, except
8885 * for the lines immediately following a function declaration,
8886 * which are K&R-style parameters and need to be indented.
8887 *
8888 * if our line starts with an open brace, forget about any
8889 * prevailing indent and make sure it looks like the start
8890 * of a function
8891 */
8892
8893 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008895 amount = curbuf->b_ind_first_open;
8896 goto theend;
8897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008899 /*
8900 * If the NEXT line is a function declaration, the current
8901 * line needs to be indented as a function type spec.
8902 * Don't do this if the current line looks like a comment or if the
8903 * current line is terminated, ie. ends in ';', or if the current line
8904 * contains { or }: "void f() {\n if (1)"
8905 */
8906 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8907 && !cin_nocode(theline)
8908 && vim_strchr(theline, '{') == NULL
8909 && vim_strchr(theline, '}') == NULL
8910 && !cin_ends_in(theline, (char_u *)":", NULL)
8911 && !cin_ends_in(theline, (char_u *)",", NULL)
8912 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8913 cur_curpos.lnum + 1)
8914 && !cin_isterminated(theline, FALSE, TRUE))
8915 {
8916 amount = curbuf->b_ind_func_type;
8917 goto theend;
8918 }
8919
8920 /* search backwards until we find something we recognize */
8921 amount = 0;
8922 curwin->w_cursor = cur_curpos;
8923 while (curwin->w_cursor.lnum > 1)
8924 {
8925 curwin->w_cursor.lnum--;
8926 curwin->w_cursor.col = 0;
8927
8928 l = ml_get_curline();
8929
8930 /*
8931 * If we're in a comment or raw string now, skip to the start
8932 * of it.
8933 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008934 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008936 curwin->w_cursor.lnum = trypos->lnum + 1;
8937 curwin->w_cursor.col = 0;
8938 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 }
8940
8941 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008942 * Are we at the start of a cpp base class declaration or
8943 * constructor initialization?
8944 */ /* XXX */
8945 n = FALSE;
8946 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008948 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8949 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008951 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008953 /* XXX */
8954 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8955 break;
8956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008958 /*
8959 * Skip preprocessor directives and blank lines.
8960 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008961 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008962 continue;
8963
8964 if (cin_nocode(l))
8965 continue;
8966
8967 /*
8968 * If the previous line ends in ',', use one level of
8969 * indentation:
8970 * int foo,
8971 * bar;
8972 * do this before checking for '}' in case of eg.
8973 * enum foobar
8974 * {
8975 * ...
8976 * } foo,
8977 * bar;
8978 */
8979 n = 0;
8980 if (cin_ends_in(l, (char_u *)",", NULL)
8981 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8982 {
8983 /* take us back to opening paren */
8984 if (find_last_paren(l, '(', ')')
8985 && (trypos = find_match_paren(
8986 curbuf->b_ind_maxparen)) != NULL)
8987 curwin->w_cursor = *trypos;
8988
8989 /* For a line ending in ',' that is a continuation line go
8990 * back to the first line with a backslash:
8991 * char *foo = "bla\
8992 * bla",
8993 * here;
8994 */
8995 while (n == 0 && curwin->w_cursor.lnum > 1)
8996 {
8997 l = ml_get(curwin->w_cursor.lnum - 1);
8998 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8999 break;
9000 --curwin->w_cursor.lnum;
9001 curwin->w_cursor.col = 0;
9002 }
9003
9004 amount = get_indent(); /* XXX */
9005
9006 if (amount == 0)
9007 amount = cin_first_id_amount();
9008 if (amount == 0)
9009 amount = ind_continuation;
9010 break;
9011 }
9012
9013 /*
9014 * If the line looks like a function declaration, and we're
9015 * not in a comment, put it the left margin.
9016 */
9017 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9018 break;
9019 l = ml_get_curline();
9020
9021 /*
9022 * Finding the closing '}' of a previous function. Put
9023 * current line at the left margin. For when 'cino' has "fs".
9024 */
9025 if (*skipwhite(l) == '}')
9026 break;
9027
9028 /* (matching {)
9029 * If the previous line ends on '};' (maybe followed by
9030 * comments) align at column 0. For example:
9031 * char *string_array[] = { "foo",
9032 * / * x * / "b};ar" }; / * foobar * /
9033 */
9034 if (cin_ends_in(l, (char_u *)"};", NULL))
9035 break;
9036
9037 /*
9038 * If the previous line ends on '[' we are probably in an
9039 * array constant:
9040 * something = [
9041 * 234, <- extra indent
9042 */
9043 if (cin_ends_in(l, (char_u *)"[", NULL))
9044 {
9045 amount = get_indent() + ind_continuation;
9046 break;
9047 }
9048
9049 /*
9050 * Find a line only has a semicolon that belongs to a previous
9051 * line ending in '}', e.g. before an #endif. Don't increase
9052 * indent then.
9053 */
9054 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9055 {
9056 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057
9058 while (curwin->w_cursor.lnum > 1)
9059 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009060 look = ml_get(--curwin->w_cursor.lnum);
9061 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009062 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009064 }
9065 if (curwin->w_cursor.lnum > 0
9066 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009069 curwin->w_cursor = curpos_save;
9070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009072 /*
9073 * If the PREVIOUS line is a function declaration, the current
9074 * line (and the ones that follow) needs to be indented as
9075 * parameters.
9076 */
9077 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9078 {
9079 amount = curbuf->b_ind_param;
9080 break;
9081 }
9082
9083 /*
9084 * If the previous line ends in ';' and the line before the
9085 * previous line ends in ',' or '\', ident to column zero:
9086 * int foo,
9087 * bar;
9088 * indent_to_0 here;
9089 */
9090 if (cin_ends_in(l, (char_u *)";", NULL))
9091 {
9092 l = ml_get(curwin->w_cursor.lnum - 1);
9093 if (cin_ends_in(l, (char_u *)",", NULL)
9094 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9095 break;
9096 l = ml_get_curline();
9097 }
9098
9099 /*
9100 * Doesn't look like anything interesting -- so just
9101 * use the indent of this line.
9102 *
9103 * Position the cursor over the rightmost paren, so that
9104 * matching it will take us back to the start of the line.
9105 */
9106 find_last_paren(l, '(', ')');
9107
9108 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9109 curwin->w_cursor = *trypos;
9110 amount = get_indent(); /* XXX */
9111 break;
9112 }
9113
9114 /* add extra indent for a comment */
9115 if (cin_iscomment(theline))
9116 amount += curbuf->b_ind_comment;
9117
9118 /* add extra indent if the previous line ended in a backslash:
9119 * "asdfasdf\
9120 * here";
9121 * char *foo = "asdf\
9122 * here";
9123 */
9124 if (cur_curpos.lnum > 1)
9125 {
9126 l = ml_get(cur_curpos.lnum - 1);
9127 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9128 {
9129 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9130 if (cur_amount > 0)
9131 amount = cur_amount;
9132 else if (cur_amount == 0)
9133 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 }
9135 }
9136
9137theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009138 if (amount < 0)
9139 amount = 0;
9140
9141laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 /* put the cursor back where it belongs */
9143 curwin->w_cursor = cur_curpos;
9144
9145 vim_free(linecopy);
9146
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 return amount;
9148}
9149
9150 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009151find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152{
9153 char_u *look;
9154 pos_T *theirscope;
9155 char_u *mightbeif;
9156 int elselevel;
9157 int whilelevel;
9158
9159 if (lookfor == LOOKFOR_IF)
9160 {
9161 elselevel = 1;
9162 whilelevel = 0;
9163 }
9164 else
9165 {
9166 elselevel = 0;
9167 whilelevel = 1;
9168 }
9169
9170 curwin->w_cursor.col = 0;
9171
9172 while (curwin->w_cursor.lnum > ourscope + 1)
9173 {
9174 curwin->w_cursor.lnum--;
9175 curwin->w_cursor.col = 0;
9176
9177 look = cin_skipcomment(ml_get_curline());
9178 if (cin_iselse(look)
9179 || cin_isif(look)
9180 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009181 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 {
9183 /*
9184 * if we've gone outside the braces entirely,
9185 * we must be out of scope...
9186 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009187 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 if (theirscope == NULL)
9189 break;
9190
9191 /*
9192 * and if the brace enclosing this is further
9193 * back than the one enclosing the else, we're
9194 * out of luck too.
9195 */
9196 if (theirscope->lnum < ourscope)
9197 break;
9198
9199 /*
9200 * and if they're enclosed in a *deeper* brace,
9201 * then we can ignore it because it's in a
9202 * different scope...
9203 */
9204 if (theirscope->lnum > ourscope)
9205 continue;
9206
9207 /*
9208 * if it was an "else" (that's not an "else if")
9209 * then we need to go back to another if, so
9210 * increment elselevel
9211 */
9212 look = cin_skipcomment(ml_get_curline());
9213 if (cin_iselse(look))
9214 {
9215 mightbeif = cin_skipcomment(look + 4);
9216 if (!cin_isif(mightbeif))
9217 ++elselevel;
9218 continue;
9219 }
9220
9221 /*
9222 * if it was a "while" then we need to go back to
9223 * another "do", so increment whilelevel. XXX
9224 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009225 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 {
9227 ++whilelevel;
9228 continue;
9229 }
9230
9231 /* If it's an "if" decrement elselevel */
9232 look = cin_skipcomment(ml_get_curline());
9233 if (cin_isif(look))
9234 {
9235 elselevel--;
9236 /*
9237 * When looking for an "if" ignore "while"s that
9238 * get in the way.
9239 */
9240 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9241 whilelevel = 0;
9242 }
9243
9244 /* If it's a "do" decrement whilelevel */
9245 if (cin_isdo(look))
9246 whilelevel--;
9247
9248 /*
9249 * if we've used up all the elses, then
9250 * this must be the if that we want!
9251 * match the indent level of that if.
9252 */
9253 if (elselevel <= 0 && whilelevel <= 0)
9254 {
9255 return OK;
9256 }
9257 }
9258 }
9259 return FAIL;
9260}
9261
9262# if defined(FEAT_EVAL) || defined(PROTO)
9263/*
9264 * Get indent level from 'indentexpr'.
9265 */
9266 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009267get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009269 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009270 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009271 pos_T save_pos;
9272 colnr_T save_curswant;
9273 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009275 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9276 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009278 /* Save and restore cursor position and curswant, in case it was changed
9279 * via :normal commands */
9280 save_pos = curwin->w_cursor;
9281 save_curswant = curwin->w_curswant;
9282 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009284 if (use_sandbox)
9285 ++sandbox;
9286 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009287
9288 /* Need to make a copy, the 'indentexpr' option could be changed while
9289 * evaluating it. */
9290 inde_copy = vim_strsave(curbuf->b_p_inde);
9291 if (inde_copy != NULL)
9292 {
9293 indent = (int)eval_to_number(inde_copy);
9294 vim_free(inde_copy);
9295 }
9296
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009297 if (use_sandbox)
9298 --sandbox;
9299 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
9301 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9302 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9303 * command. */
9304 save_State = State;
9305 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009306 curwin->w_cursor = save_pos;
9307 curwin->w_curswant = save_curswant;
9308 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 check_cursor();
9310 State = save_State;
9311
9312 /* If there is an error, just keep the current indent. */
9313 if (indent < 0)
9314 indent = get_indent();
9315
9316 return indent;
9317}
9318# endif
9319
9320#endif /* FEAT_CINDENT */
9321
9322#if defined(FEAT_LISP) || defined(PROTO)
9323
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009324static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325
9326 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009327lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 char_u buf[LSIZE];
9330 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009331 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
9333 while (*word != NUL)
9334 {
9335 (void)copy_option_part(&word, buf, LSIZE, ",");
9336 len = (int)STRLEN(buf);
9337 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9338 return TRUE;
9339 }
9340 return FALSE;
9341}
9342
9343/*
9344 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9345 * The incompatible newer method is quite a bit better at indenting
9346 * code in lisp-like languages than the traditional one; it's still
9347 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9348 *
9349 * TODO:
9350 * Findmatch() should be adapted for lisp, also to make showmatch
9351 * work correctly: now (v5.3) it seems all C/C++ oriented:
9352 * - it does not recognize the #\( and #\) notations as character literals
9353 * - it doesn't know about comments starting with a semicolon
9354 * - it incorrectly interprets '(' as a character literal
9355 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009356 * Update from Sergey Khorev:
9357 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 */
9359 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009360get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009362 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 int amount;
9364 char_u *that;
9365 colnr_T col;
9366 colnr_T firsttry;
9367 int parencount, quotecount;
9368 int vi_lisp;
9369
9370 /* Set vi_lisp to use the vi-compatible method */
9371 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9372
9373 realpos = curwin->w_cursor;
9374 curwin->w_cursor.col = 0;
9375
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009376 if ((pos = findmatch(NULL, '(')) == NULL)
9377 pos = findmatch(NULL, '[');
9378 else
9379 {
9380 paren = *pos;
9381 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009382 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009383 pos = &paren;
9384 }
9385 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 {
9387 /* Extra trick: Take the indent of the first previous non-white
9388 * line that is at the same () level. */
9389 amount = -1;
9390 parencount = 0;
9391
9392 while (--curwin->w_cursor.lnum >= pos->lnum)
9393 {
9394 if (linewhite(curwin->w_cursor.lnum))
9395 continue;
9396 for (that = ml_get_curline(); *that != NUL; ++that)
9397 {
9398 if (*that == ';')
9399 {
9400 while (*(that + 1) != NUL)
9401 ++that;
9402 continue;
9403 }
9404 if (*that == '\\')
9405 {
9406 if (*(that + 1) != NUL)
9407 ++that;
9408 continue;
9409 }
9410 if (*that == '"' && *(that + 1) != NUL)
9411 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009412 while (*++that && *that != '"')
9413 {
9414 /* skipping escaped characters in the string */
9415 if (*that == '\\')
9416 {
9417 if (*++that == NUL)
9418 break;
9419 if (that[1] == NUL)
9420 {
9421 ++that;
9422 break;
9423 }
9424 }
9425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009427 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009429 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 --parencount;
9431 }
9432 if (parencount == 0)
9433 {
9434 amount = get_indent();
9435 break;
9436 }
9437 }
9438
9439 if (amount == -1)
9440 {
9441 curwin->w_cursor.lnum = pos->lnum;
9442 curwin->w_cursor.col = pos->col;
9443 col = pos->col;
9444
9445 that = ml_get_curline();
9446
9447 if (vi_lisp && get_indent() == 0)
9448 amount = 2;
9449 else
9450 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009451 char_u *line = that;
9452
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 amount = 0;
9454 while (*that && col)
9455 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009456 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 col--;
9458 }
9459
9460 /*
9461 * Some keywords require "body" indenting rules (the
9462 * non-standard-lisp ones are Scheme special forms):
9463 *
9464 * (let ((a 1)) instead (let ((a 1))
9465 * (...)) of (...))
9466 */
9467
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009468 if (!vi_lisp && (*that == '(' || *that == '[')
9469 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 amount += 2;
9471 else
9472 {
9473 that++;
9474 amount++;
9475 firsttry = amount;
9476
Bram Moolenaar1c465442017-03-12 20:10:05 +01009477 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009479 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 ++that;
9481 }
9482
9483 if (*that && *that != ';') /* not a comment line */
9484 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009485 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009487 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 firsttry++;
9489
9490 parencount = 0;
9491 quotecount = 0;
9492
9493 if (vi_lisp
9494 || (*that != '"'
9495 && *that != '\''
9496 && *that != '#'
9497 && (*that < '0' || *that > '9')))
9498 {
9499 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009500 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 || quotecount
9502 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009503 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 && !quotecount
9505 && !parencount
9506 && vi_lisp)))
9507 {
9508 if (*that == '"')
9509 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009510 if ((*that == '(' || *that == '[')
9511 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009513 if ((*that == ')' || *that == ']')
9514 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 --parencount;
9516 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009517 amount += lbr_chartabsize_adv(
9518 line, &that, (colnr_T)amount);
9519 amount += lbr_chartabsize_adv(
9520 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 }
9522 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009523 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009525 amount += lbr_chartabsize(
9526 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 that++;
9528 }
9529 if (!*that || *that == ';')
9530 amount = firsttry;
9531 }
9532 }
9533 }
9534 }
9535 }
9536 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009537 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538
9539 curwin->w_cursor = realpos;
9540
9541 return amount;
9542}
9543#endif /* FEAT_LISP */
9544
9545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009546prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009548#if defined(SIGHUP) && defined(SIG_IGN)
9549 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9550 * makes Vim exit and then handling SIGHUP causes various reentrance
9551 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009552 signal(SIGHUP, SIG_IGN);
9553#endif
9554
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555#ifdef FEAT_GUI
9556 if (gui.in_use)
9557 {
9558 gui.dying = TRUE;
9559 out_trash(); /* trash any pending output */
9560 }
9561 else
9562#endif
9563 {
9564 windgoto((int)Rows - 1, 0);
9565
9566 /*
9567 * Switch terminal mode back now, so messages end up on the "normal"
9568 * screen (if there are two screens).
9569 */
9570 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009571 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 out_flush();
9573 }
9574}
9575
9576/*
9577 * Preserve files and exit.
9578 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009579 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9580 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 */
9582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009583preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585 buf_T *buf;
9586
9587 prepare_to_exit();
9588
Bram Moolenaar4770d092006-01-12 23:22:24 +00009589 /* Setting this will prevent free() calls. That avoids calling free()
9590 * recursively when free() was invoked with a bad pointer. */
9591 really_exiting = TRUE;
9592
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 out_str(IObuff);
9594 screen_start(); /* don't know where cursor is now */
9595 out_flush();
9596
9597 ml_close_notmod(); /* close all not-modified buffers */
9598
Bram Moolenaar29323592016-07-24 22:04:11 +02009599 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
9601 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9602 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009603 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 screen_start(); /* don't know where cursor is now */
9605 out_flush();
9606 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9607 break;
9608 }
9609 }
9610
9611 ml_close_all(FALSE); /* close all memfiles, without deleting */
9612
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009613 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614
9615 getout(1);
9616}
9617
9618/*
9619 * return TRUE if "fname" exists.
9620 */
9621 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009622vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009624 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625
9626 if (mch_stat((char *)fname, &st))
9627 return FALSE;
9628 return TRUE;
9629}
9630
9631/*
9632 * Check for CTRL-C pressed, but only once in a while.
9633 * Should be used instead of ui_breakcheck() for functions that check for
9634 * each line in the file. Calling ui_breakcheck() each time takes too much
9635 * time, because it can be a system call.
9636 */
9637
9638#ifndef BREAKCHECK_SKIP
9639# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9640# define BREAKCHECK_SKIP 200
9641# else
9642# define BREAKCHECK_SKIP 32
9643# endif
9644#endif
9645
9646static int breakcheck_count = 0;
9647
9648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009649line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651 if (++breakcheck_count >= BREAKCHECK_SKIP)
9652 {
9653 breakcheck_count = 0;
9654 ui_breakcheck();
9655 }
9656}
9657
9658/*
9659 * Like line_breakcheck() but check 10 times less often.
9660 */
9661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009662fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9665 {
9666 breakcheck_count = 0;
9667 ui_breakcheck();
9668 }
9669}
9670
9671/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009672 * Invoke expand_wildcards() for one pattern.
9673 * Expand items like "%:h" before the expansion.
9674 * Returns OK or FAIL.
9675 */
9676 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009677expand_wildcards_eval(
9678 char_u **pat, /* pointer to input pattern */
9679 int *num_file, /* resulting number of files */
9680 char_u ***file, /* array of resulting files */
9681 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009682{
9683 int ret = FAIL;
9684 char_u *eval_pat = NULL;
9685 char_u *exp_pat = *pat;
9686 char_u *ignored_msg;
9687 int usedlen;
9688
9689 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9690 {
9691 ++emsg_off;
9692 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9693 NULL, &ignored_msg, NULL);
9694 --emsg_off;
9695 if (eval_pat != NULL)
9696 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9697 }
9698
9699 if (exp_pat != NULL)
9700 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9701
9702 if (eval_pat != NULL)
9703 {
9704 vim_free(exp_pat);
9705 vim_free(eval_pat);
9706 }
9707
9708 return ret;
9709}
9710
9711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9713 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009714 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 */
9716 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009717expand_wildcards(
9718 int num_pat, /* number of input patterns */
9719 char_u **pat, /* array of input patterns */
9720 int *num_files, /* resulting number of files */
9721 char_u ***files, /* array of resulting files */
9722 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724 int retval;
9725 int i, j;
9726 char_u *p;
9727 int non_suf_match; /* number without matching suffix */
9728
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009729 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
9731 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009732 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 return retval;
9734
9735#ifdef FEAT_WILDIGN
9736 /*
9737 * Remove names that match 'wildignore'.
9738 */
9739 if (*p_wig)
9740 {
9741 char_u *ffname;
9742
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009743 /* check all files in (*files)[] */
9744 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009746 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 if (ffname == NULL) /* out of memory */
9748 break;
9749# ifdef VMS
9750 vms_remove_version(ffname);
9751# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009752 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009754 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009755 vim_free((*files)[i]);
9756 for (j = i; j + 1 < *num_files; ++j)
9757 (*files)[j] = (*files)[j + 1];
9758 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 --i;
9760 }
9761 vim_free(ffname);
9762 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009763
9764 /* If the number of matches is now zero, we fail. */
9765 if (*num_files == 0)
9766 {
9767 vim_free(*files);
9768 *files = NULL;
9769 return FAIL;
9770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 }
9772#endif
9773
9774 /*
9775 * Move the names where 'suffixes' match to the end.
9776 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009777 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 {
9779 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009780 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009782 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 {
9784 /*
9785 * Move the name without matching suffix to the front
9786 * of the list.
9787 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009788 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009790 (*files)[j] = (*files)[j - 1];
9791 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 }
9793 }
9794 }
9795
9796 return retval;
9797}
9798
9799/*
9800 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9801 */
9802 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009803match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
9805 int fnamelen, setsuflen;
9806 char_u *setsuf;
9807#define MAXSUFLEN 30 /* maximum length of a file suffix */
9808 char_u suf_buf[MAXSUFLEN];
9809
9810 fnamelen = (int)STRLEN(fname);
9811 setsuflen = 0;
9812 for (setsuf = p_su; *setsuf; )
9813 {
9814 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009815 if (setsuflen == 0)
9816 {
9817 char_u *tail = gettail(fname);
9818
9819 /* empty entry: match name without a '.' */
9820 if (vim_strchr(tail, '.') == NULL)
9821 {
9822 setsuflen = 1;
9823 break;
9824 }
9825 }
9826 else
9827 {
9828 if (fnamelen >= setsuflen
9829 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9830 (size_t)setsuflen) == 0)
9831 break;
9832 setsuflen = 0;
9833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 }
9835 return (setsuflen != 0);
9836}
9837
9838#if !defined(NO_EXPANDPATH) || defined(PROTO)
9839
9840# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009841static int vim_backtick(char_u *p);
9842static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843# endif
9844
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009845# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846/*
9847 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9848 * it's shared between these systems.
9849 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009850# if defined(PROTO)
9851# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852# else
9853# ifdef __BORLANDC__
9854# define _cdecl _RTLENTRYF
9855# endif
9856# endif
9857
9858/*
9859 * comparison function for qsort in dos_expandpath()
9860 */
9861 static int _cdecl
9862pstrcmp(const void *a, const void *b)
9863{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009864 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009868 * Recursively expand one path component into all matching files and/or
9869 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 * Return the number of matches found.
9871 * "path" has backslashes before chars that are not to be expanded, starting
9872 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009873 * Return the number of matches found.
9874 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 */
9876 static int
9877dos_expandpath(
9878 garray_T *gap,
9879 char_u *path,
9880 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009881 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009882 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009884 char_u *buf;
9885 char_u *path_end;
9886 char_u *p, *s, *e;
9887 int start_len = gap->ga_len;
9888 char_u *pat;
9889 regmatch_T regmatch;
9890 int starts_with_dot;
9891 int matches;
9892 int len;
9893 int starstar = FALSE;
9894 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 WIN32_FIND_DATA fb;
9896 HANDLE hFind = (HANDLE)0;
9897# ifdef FEAT_MBYTE
9898 WIN32_FIND_DATAW wfb;
9899 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9900# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009902 int ok;
9903
9904 /* Expanding "**" may take a long time, check for CTRL-C. */
9905 if (stardepth > 0)
9906 {
9907 ui_breakcheck();
9908 if (got_int)
9909 return 0;
9910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009912 /* Make room for file name. When doing encoding conversion the actual
9913 * length may be quite a bit longer, thus use the maximum possible length. */
9914 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 if (buf == NULL)
9916 return 0;
9917
9918 /*
9919 * Find the first part in the path name that contains a wildcard or a ~1.
9920 * Copy it into buf, including the preceding characters.
9921 */
9922 p = buf;
9923 s = buf;
9924 e = NULL;
9925 path_end = path;
9926 while (*path_end != NUL)
9927 {
9928 /* May ignore a wildcard that has a backslash before it; it will
9929 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9930 if (path_end >= path + wildoff && rem_backslash(path_end))
9931 *p++ = *path_end++;
9932 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9933 {
9934 if (e != NULL)
9935 break;
9936 s = p + 1;
9937 }
9938 else if (path_end >= path + wildoff
9939 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9940 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009941# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 if (has_mbyte)
9943 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009944 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 STRNCPY(p, path_end, len);
9946 p += len;
9947 path_end += len;
9948 }
9949 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 *p++ = *path_end++;
9952 }
9953 e = p;
9954 *e = NUL;
9955
9956 /* now we have one wildcard component between s and e */
9957 /* Remove backslashes between "wildoff" and the start of the wildcard
9958 * component. */
9959 for (p = buf + wildoff; p < s; ++p)
9960 if (rem_backslash(p))
9961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009962 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 --e;
9964 --s;
9965 }
9966
Bram Moolenaar231334e2005-07-25 20:46:57 +00009967 /* Check for "**" between "s" and "e". */
9968 for (p = s; p < e; ++p)
9969 if (p[0] == '*' && p[1] == '*')
9970 starstar = TRUE;
9971
Bram Moolenaard82103e2016-01-17 17:04:05 +01009972 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9974 if (pat == NULL)
9975 {
9976 vim_free(buf);
9977 return 0;
9978 }
9979
9980 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009981 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009982 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 regmatch.rm_ic = TRUE; /* Always ignore case */
9984 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009985 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009986 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 vim_free(pat);
9988
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009989 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 {
9991 vim_free(buf);
9992 return 0;
9993 }
9994
9995 /* remember the pattern or file name being looked for */
9996 matchname = vim_strsave(s);
9997
Bram Moolenaar231334e2005-07-25 20:46:57 +00009998 /* If "**" is by itself, this is the first time we encounter it and more
9999 * is following then find matches without any directory. */
10000 if (!didstar && stardepth < 100 && starstar && e - s == 2
10001 && *path_end == '/')
10002 {
10003 STRCPY(s, path_end + 1);
10004 ++stardepth;
10005 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10006 --stardepth;
10007 }
10008
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 /* Scan all files in the directory with "dir/ *.*" */
10010 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011# ifdef FEAT_MBYTE
10012 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10013 {
10014 /* The active codepage differs from 'encoding'. Attempt using the
10015 * wide function. If it fails because it is not implemented fall back
10016 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010017 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 if (wn != NULL)
10019 {
10020 hFind = FindFirstFileW(wn, &wfb);
10021 if (hFind == INVALID_HANDLE_VALUE
10022 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
10023 {
10024 vim_free(wn);
10025 wn = NULL;
10026 }
10027 }
10028 }
10029
10030 if (wn == NULL)
10031# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010032 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
10035 while (ok)
10036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037# ifdef FEAT_MBYTE
10038 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010039 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 else
10041# endif
10042 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 /* Ignore entries starting with a dot, unless when asked for. Accept
10044 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010045 if ((p[0] != '.' || starts_with_dot
10046 || ((flags & EW_DODOT)
10047 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010049 || (regmatch.regprog != NULL
10050 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010051 || ((flags & EW_NOTWILD)
10052 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010056
10057 if (starstar && stardepth < 100)
10058 {
10059 /* For "**" in the pattern first go deeper in the tree to
10060 * find matches. */
10061 STRCPY(buf + len, "/**");
10062 STRCPY(buf + len + 3, path_end);
10063 ++stardepth;
10064 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10065 --stardepth;
10066 }
10067
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 STRCPY(buf + len, path_end);
10069 if (mch_has_exp_wildcard(path_end))
10070 {
10071 /* need to expand another component of the path */
10072 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010073 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 }
10075 else
10076 {
10077 /* no more wildcards, check if there is a match */
10078 /* remove backslashes for the remaining components only */
10079 if (*path_end != 0)
10080 backslash_halve(buf + len + 1);
10081 if (mch_getperm(buf) >= 0) /* add existing file */
10082 addfile(gap, buf, flags);
10083 }
10084 }
10085
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086# ifdef FEAT_MBYTE
10087 if (wn != NULL)
10088 {
10089 vim_free(p);
10090 ok = FindNextFileW(hFind, &wfb);
10091 }
10092 else
10093# endif
10094 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095
10096 /* If no more matches and no match was used, try expanding the name
10097 * itself. Finds the long name of a short filename. */
10098 if (!ok && matchname != NULL && gap->ga_len == start_len)
10099 {
10100 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 FindClose(hFind);
10102# ifdef FEAT_MBYTE
10103 if (wn != NULL)
10104 {
10105 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010106 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 if (wn != NULL)
10108 hFind = FindFirstFileW(wn, &wfb);
10109 }
10110 if (wn == NULL)
10111# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010112 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 vim_free(matchname);
10115 matchname = NULL;
10116 }
10117 }
10118
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 FindClose(hFind);
10120# ifdef FEAT_MBYTE
10121 vim_free(wn);
10122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010124 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 vim_free(matchname);
10126
10127 matches = gap->ga_len - start_len;
10128 if (matches > 0)
10129 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10130 sizeof(char_u *), pstrcmp);
10131 return matches;
10132}
10133
10134 int
10135mch_expandpath(
10136 garray_T *gap,
10137 char_u *path,
10138 int flags) /* EW_* flags */
10139{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010140 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010142# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143
Bram Moolenaar231334e2005-07-25 20:46:57 +000010144#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10145 || defined(PROTO)
10146/*
10147 * Unix style wildcard expansion code.
10148 * It's here because it's used both for Unix and Mac.
10149 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010150static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010151
10152 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010153pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010154{
10155 return (pathcmp(*(char **)a, *(char **)b, -1));
10156}
10157
10158/*
10159 * Recursively expand one path component into all matching files and/or
10160 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10161 * "path" has backslashes before chars that are not to be expanded, starting
10162 * at "path + wildoff".
10163 * Return the number of matches found.
10164 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10165 */
10166 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010167unix_expandpath(
10168 garray_T *gap,
10169 char_u *path,
10170 int wildoff,
10171 int flags, /* EW_* flags */
10172 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010173{
10174 char_u *buf;
10175 char_u *path_end;
10176 char_u *p, *s, *e;
10177 int start_len = gap->ga_len;
10178 char_u *pat;
10179 regmatch_T regmatch;
10180 int starts_with_dot;
10181 int matches;
10182 int len;
10183 int starstar = FALSE;
10184 static int stardepth = 0; /* depth for "**" expansion */
10185
10186 DIR *dirp;
10187 struct dirent *dp;
10188
10189 /* Expanding "**" may take a long time, check for CTRL-C. */
10190 if (stardepth > 0)
10191 {
10192 ui_breakcheck();
10193 if (got_int)
10194 return 0;
10195 }
10196
10197 /* make room for file name */
10198 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10199 if (buf == NULL)
10200 return 0;
10201
10202 /*
10203 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010204 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010205 * Copy it into "buf", including the preceding characters.
10206 */
10207 p = buf;
10208 s = buf;
10209 e = NULL;
10210 path_end = path;
10211 while (*path_end != NUL)
10212 {
10213 /* May ignore a wildcard that has a backslash before it; it will
10214 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10215 if (path_end >= path + wildoff && rem_backslash(path_end))
10216 *p++ = *path_end++;
10217 else if (*path_end == '/')
10218 {
10219 if (e != NULL)
10220 break;
10221 s = p + 1;
10222 }
10223 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010224 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010225 || (!p_fic && (flags & EW_ICASE)
10226 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010227 e = p;
10228#ifdef FEAT_MBYTE
10229 if (has_mbyte)
10230 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010231 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010232 STRNCPY(p, path_end, len);
10233 p += len;
10234 path_end += len;
10235 }
10236 else
10237#endif
10238 *p++ = *path_end++;
10239 }
10240 e = p;
10241 *e = NUL;
10242
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010243 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010244 /* Remove backslashes between "wildoff" and the start of the wildcard
10245 * component. */
10246 for (p = buf + wildoff; p < s; ++p)
10247 if (rem_backslash(p))
10248 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010249 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010250 --e;
10251 --s;
10252 }
10253
10254 /* Check for "**" between "s" and "e". */
10255 for (p = s; p < e; ++p)
10256 if (p[0] == '*' && p[1] == '*')
10257 starstar = TRUE;
10258
10259 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010260 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010261 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10262 if (pat == NULL)
10263 {
10264 vim_free(buf);
10265 return 0;
10266 }
10267
10268 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010269 if (flags & EW_ICASE)
10270 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10271 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010272 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010273 if (flags & (EW_NOERROR | EW_NOTWILD))
10274 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010275 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010276 if (flags & (EW_NOERROR | EW_NOTWILD))
10277 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010278 vim_free(pat);
10279
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010280 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010281 {
10282 vim_free(buf);
10283 return 0;
10284 }
10285
10286 /* If "**" is by itself, this is the first time we encounter it and more
10287 * is following then find matches without any directory. */
10288 if (!didstar && stardepth < 100 && starstar && e - s == 2
10289 && *path_end == '/')
10290 {
10291 STRCPY(s, path_end + 1);
10292 ++stardepth;
10293 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10294 --stardepth;
10295 }
10296
10297 /* open the directory for scanning */
10298 *s = NUL;
10299 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10300
10301 /* Find all matching entries */
10302 if (dirp != NULL)
10303 {
10304 for (;;)
10305 {
10306 dp = readdir(dirp);
10307 if (dp == NULL)
10308 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010309 if ((dp->d_name[0] != '.' || starts_with_dot
10310 || ((flags & EW_DODOT)
10311 && dp->d_name[1] != NUL
10312 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010313 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10314 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010315 || ((flags & EW_NOTWILD)
10316 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010317 {
10318 STRCPY(s, dp->d_name);
10319 len = STRLEN(buf);
10320
10321 if (starstar && stardepth < 100)
10322 {
10323 /* For "**" in the pattern first go deeper in the tree to
10324 * find matches. */
10325 STRCPY(buf + len, "/**");
10326 STRCPY(buf + len + 3, path_end);
10327 ++stardepth;
10328 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10329 --stardepth;
10330 }
10331
10332 STRCPY(buf + len, path_end);
10333 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10334 {
10335 /* need to expand another component of the path */
10336 /* remove backslashes for the remaining components only */
10337 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10338 }
10339 else
10340 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010341 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010342
Bram Moolenaar231334e2005-07-25 20:46:57 +000010343 /* no more wildcards, check if there is a match */
10344 /* remove backslashes for the remaining components only */
10345 if (*path_end != NUL)
10346 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010347 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010348 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010349 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010350 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010351#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010352 size_t precomp_len = STRLEN(buf)+1;
10353 char_u *precomp_buf =
10354 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010355
Bram Moolenaar231334e2005-07-25 20:46:57 +000010356 if (precomp_buf)
10357 {
10358 mch_memmove(buf, precomp_buf, precomp_len);
10359 vim_free(precomp_buf);
10360 }
10361#endif
10362 addfile(gap, buf, flags);
10363 }
10364 }
10365 }
10366 }
10367
10368 closedir(dirp);
10369 }
10370
10371 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010372 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010373
10374 matches = gap->ga_len - start_len;
10375 if (matches > 0)
10376 qsort(((char_u **)gap->ga_data) + start_len, matches,
10377 sizeof(char_u *), pstrcmp);
10378 return matches;
10379}
10380#endif
10381
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010382#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010383static int find_previous_pathsep(char_u *path, char_u **psep);
10384static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10385static void expand_path_option(char_u *curdir, garray_T *gap);
10386static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10387static void uniquefy_paths(garray_T *gap, char_u *pattern);
10388static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010389
10390/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010391 * Moves "*psep" back to the previous path separator in "path".
10392 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010393 */
10394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010395find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010396{
10397 /* skip the current separator */
10398 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010399 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010400
10401 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010402 while (*psep > path)
10403 {
10404 if (vim_ispathsep(**psep))
10405 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010406 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010407 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010408
10409 return FAIL;
10410}
10411
10412/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010413 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10414 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010415 */
10416 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010417is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010418{
10419 int j;
10420 int candidate_len;
10421 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010422 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010423 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010424
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010425 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010426 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010427 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010428 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010430 candidate_len = (int)STRLEN(maybe_unique);
10431 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010432 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010433 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010434
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010435 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010436 if (fnamecmp(maybe_unique, rival) == 0
10437 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010438 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010439 }
10440
Bram Moolenaar162bd912010-07-28 22:29:10 +020010441 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010442}
10443
10444/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010445 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010446 * paths are expanded to their equivalent fullpath. This includes the "."
10447 * (relative to current buffer directory) and empty path (relative to current
10448 * directory) notations.
10449 *
10450 * TODO: handle upward search (;) and path limiter (**N) notations by
10451 * expanding each into their equivalent path(s).
10452 */
10453 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010454expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010455{
10456 char_u *path_option = *curbuf->b_p_path == NUL
10457 ? p_path : curbuf->b_p_path;
10458 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010459 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010460 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010461
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010462 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010463 return;
10464
10465 while (*path_option != NUL)
10466 {
10467 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10468
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010469 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010470 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010471 /* Relative to current buffer:
10472 * "/path/file" + "." -> "/path/"
10473 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010474 if (curbuf->b_ffname == NULL)
10475 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010476 p = gettail(curbuf->b_ffname);
10477 len = (int)(p - curbuf->b_ffname);
10478 if (len + (int)STRLEN(buf) >= MAXPATHL)
10479 continue;
10480 if (buf[1] == NUL)
10481 buf[len] = NUL;
10482 else
10483 STRMOVE(buf + len, buf + 2);
10484 mch_memmove(buf, curbuf->b_ffname, len);
10485 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010486 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010487 else if (buf[0] == NUL)
10488 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010489 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010490 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010491 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010492 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010493 else if (!mch_isFullName(buf))
10494 {
10495 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010496 len = (int)STRLEN(curdir);
10497 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010498 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010499 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010500 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010501 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010502 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010503 }
10504
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010505 if (ga_grow(gap, 1) == FAIL)
10506 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010507
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010508# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010509 /* Avoid the path ending in a backslash, it fails when a comma is
10510 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010511 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010512 if (buf[len - 1] == '\\')
10513 buf[len - 1] = '/';
10514# endif
10515
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010516 p = vim_strsave(buf);
10517 if (p == NULL)
10518 break;
10519 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010520 }
10521
10522 vim_free(buf);
10523}
10524
10525/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010526 * Returns a pointer to the file or directory name in "fname" that matches the
10527 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010528 *
10529 * path: /foo/bar/baz
10530 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010531 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010532 */
10533 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010534get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010535{
10536 int i;
10537 int maxlen = 0;
10538 char_u **path_part = (char_u **)gap->ga_data;
10539 char_u *cutoff = NULL;
10540
10541 for (i = 0; i < gap->ga_len; i++)
10542 {
10543 int j = 0;
10544
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010545 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010546# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010547 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10548#endif
10549 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010550 j++;
10551 if (j > maxlen)
10552 {
10553 maxlen = j;
10554 cutoff = &fname[j];
10555 }
10556 }
10557
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010558 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010559 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010560 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010561 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010562
10563 return cutoff;
10564}
10565
10566/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010567 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10568 * that they are unique with respect to each other while conserving the part
10569 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010570 */
10571 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010572uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010573{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010574 int i;
10575 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010576 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010577 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010578 char_u *pat;
10579 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010580 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010581 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010582 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010583 char_u **in_curdir = NULL;
10584 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010585
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010586 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010587 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010588
10589 /*
10590 * We need to prepend a '*' at the beginning of file_pattern so that the
10591 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010592 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010593 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010594 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010596 if (file_pattern == NULL)
10597 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010598 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010599 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010600 STRCAT(file_pattern, pattern);
10601 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10602 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010603 if (pat == NULL)
10604 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010605
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010606 regmatch.rm_ic = TRUE; /* always ignore case */
10607 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10608 vim_free(pat);
10609 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010610 return;
10611
Bram Moolenaar162bd912010-07-28 22:29:10 +020010612 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010613 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010614 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010615 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010616
10617 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010618 if (in_curdir == NULL)
10619 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010620
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010621 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010622 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010623 char_u *path = fnames[i];
10624 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010625 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010626 char_u *pathsep_p;
10627 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010628
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010629 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010630 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010631 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010632 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010633 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010634
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010635 /* Shorten the filename while maintaining its uniqueness */
10636 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010637
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010638 /* Don't assume all files can be reached without path when search
10639 * pattern starts with star star slash, so only remove path_cutoff
10640 * when possible. */
10641 if (pattern[0] == '*' && pattern[1] == '*'
10642 && vim_ispathsep_nocolon(pattern[2])
10643 && path_cutoff != NULL
10644 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10645 && is_unique(path_cutoff, gap, i))
10646 {
10647 sort_again = TRUE;
10648 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10649 }
10650 else
10651 {
10652 /* Here all files can be reached without path, so get shortest
10653 * unique path. We start at the end of the path. */
10654 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010655
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010656 while (find_previous_pathsep(path, &pathsep_p))
10657 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10658 && is_unique(pathsep_p + 1, gap, i)
10659 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10660 {
10661 sort_again = TRUE;
10662 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10663 break;
10664 }
10665 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010666
10667 if (mch_isFullName(path))
10668 {
10669 /*
10670 * Last resort: shorten relative to curdir if possible.
10671 * 'possible' means:
10672 * 1. It is under the current directory.
10673 * 2. The result is actually shorter than the original.
10674 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010675 * Before curdir After
10676 * /foo/bar/file.txt /foo/bar ./file.txt
10677 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10678 * /file.txt / /file.txt
10679 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010680 */
10681 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010682 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010683#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010684 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010685 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010686 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010687 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010688 && !vim_ispathsep(*short_name)
10689#endif
10690 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010691 {
10692 STRCPY(path, ".");
10693 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010694 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010695 }
10696 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010697 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010698 }
10699
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010700 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010701 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010702 {
10703 char_u *rel_path;
10704 char_u *path = in_curdir[i];
10705
10706 if (path == NULL)
10707 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010708
10709 /* If the {filename} is not unique, change it to ./{filename}.
10710 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010711 short_name = shorten_fname(path, curdir);
10712 if (short_name == NULL)
10713 short_name = path;
10714 if (is_unique(short_name, gap, i))
10715 {
10716 STRCPY(fnames[i], short_name);
10717 continue;
10718 }
10719
10720 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10721 if (rel_path == NULL)
10722 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010723 STRCPY(rel_path, ".");
10724 add_pathsep(rel_path);
10725 STRCAT(rel_path, short_name);
10726
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010727 vim_free(fnames[i]);
10728 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010729 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010730 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010731 }
10732
Bram Moolenaar162bd912010-07-28 22:29:10 +020010733theend:
10734 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010735 if (in_curdir != NULL)
10736 {
10737 for (i = 0; i < gap->ga_len; i++)
10738 vim_free(in_curdir[i]);
10739 vim_free(in_curdir);
10740 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010741 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010742 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010743
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010744 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010745 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010746}
10747
10748/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010749 * Calls globpath() with 'path' values for the given pattern and stores the
10750 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010751 * Returns the total number of matches.
10752 */
10753 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010754expand_in_path(
10755 garray_T *gap,
10756 char_u *pattern,
10757 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010758{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010759 char_u *curdir;
10760 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010761 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010762
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010763 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010764 return 0;
10765 mch_dirname(curdir, MAXPATHL);
10766
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010767 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010768 expand_path_option(curdir, &path_ga);
10769 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010770 if (path_ga.ga_len == 0)
10771 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010772
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010773 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010774 ga_clear_strings(&path_ga);
10775 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010776 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010777
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010778 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010779 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010780
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010781 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010782}
10783#endif
10784
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010785#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10786/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010787 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10788 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010789 */
10790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010791remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010792{
10793 int i;
10794 int j;
10795 char_u **fnames = (char_u **)gap->ga_data;
10796
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010797 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010798 for (i = gap->ga_len - 1; i > 0; --i)
10799 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10800 {
10801 vim_free(fnames[i]);
10802 for (j = i + 1; j < gap->ga_len; ++j)
10803 fnames[j - 1] = fnames[j];
10804 --gap->ga_len;
10805 }
10806}
10807#endif
10808
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010809static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010810
10811/*
10812 * Return TRUE if "p" contains what looks like an environment variable.
10813 * Allowing for escaping.
10814 */
10815 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010816has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010817{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010818 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010819 {
10820 if (*p == '\\' && p[1] != NUL)
10821 ++p;
10822 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010823#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010824 "$%"
10825#else
10826 "$"
10827#endif
10828 , *p) != NULL)
10829 return TRUE;
10830 }
10831 return FALSE;
10832}
10833
10834#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010835static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010836
10837/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010838 * Return TRUE if "p" contains a special wildcard character, one that Vim
10839 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010840 */
10841 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010842has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010843{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010844 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010845 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010846 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010847 if (*p == '\\' && p[1] != NUL)
10848 ++p;
10849 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10850 return TRUE;
10851 }
10852 return FALSE;
10853}
10854#endif
10855
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856/*
10857 * Generic wildcard expansion code.
10858 *
10859 * Characters in "pat" that should not be expanded must be preceded with a
10860 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10861 *
10862 * Return FAIL when no single file was found. In this case "num_file" is not
10863 * set, and "file" may contain an error message.
10864 * Return OK when some files found. "num_file" is set to the number of
10865 * matches, "file" to the array of matches. Call FreeWild() later.
10866 */
10867 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010868gen_expand_wildcards(
10869 int num_pat, /* number of input patterns */
10870 char_u **pat, /* array of input patterns */
10871 int *num_file, /* resulting number of files */
10872 char_u ***file, /* array of resulting files */
10873 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874{
10875 int i;
10876 garray_T ga;
10877 char_u *p;
10878 static int recursive = FALSE;
10879 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010880 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010881#if defined(FEAT_SEARCHPATH)
10882 int did_expand_in_path = FALSE;
10883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884
10885 /*
10886 * expand_env() is called to expand things like "~user". If this fails,
10887 * it calls ExpandOne(), which brings us back here. In this case, always
10888 * call the machine specific expansion function, if possible. Otherwise,
10889 * return FAIL.
10890 */
10891 if (recursive)
10892#ifdef SPECIAL_WILDCHAR
10893 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10894#else
10895 return FAIL;
10896#endif
10897
10898#ifdef SPECIAL_WILDCHAR
10899 /*
10900 * If there are any special wildcard characters which we cannot handle
10901 * here, call machine specific function for all the expansion. This
10902 * avoids starting the shell for each argument separately.
10903 * For `=expr` do use the internal function.
10904 */
10905 for (i = 0; i < num_pat; i++)
10906 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010907 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908# ifdef VIM_BACKTICK
10909 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10910# endif
10911 )
10912 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10913 }
10914#endif
10915
10916 recursive = TRUE;
10917
10918 /*
10919 * The matching file names are stored in a growarray. Init it empty.
10920 */
10921 ga_init2(&ga, (int)sizeof(char_u *), 30);
10922
10923 for (i = 0; i < num_pat; ++i)
10924 {
10925 add_pat = -1;
10926 p = pat[i];
10927
10928#ifdef VIM_BACKTICK
10929 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010932 if (add_pat == -1)
10933 retval = FAIL;
10934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 else
10936#endif
10937 {
10938 /*
10939 * First expand environment variables, "~/" and "~user/".
10940 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010941 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010943 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 if (p == NULL)
10945 p = pat[i];
10946#ifdef UNIX
10947 /*
10948 * On Unix, if expand_env() can't expand an environment
10949 * variable, use the shell to do that. Discard previously
10950 * found file names and start all over again.
10951 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010952 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 {
10954 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010955 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010957 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 recursive = FALSE;
10959 return i;
10960 }
10961#endif
10962 }
10963
10964 /*
10965 * If there are wildcards: Expand file names and add each match to
10966 * the list. If there is no match, and EW_NOTFOUND is given, add
10967 * the pattern.
10968 * If there are no wildcards: Add the file name if it exists or
10969 * when EW_NOTFOUND is given.
10970 */
10971 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010972 {
10973#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010974 if ((flags & EW_PATH)
10975 && !mch_isFullName(p)
10976 && !(p[0] == '.'
10977 && (vim_ispathsep(p[1])
10978 || (p[1] == '.' && vim_ispathsep(p[2]))))
10979 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010980 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010981 /* :find completion where 'path' is used.
10982 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010983 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010984 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010985 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010986 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010987 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010988 else
10989#endif
10990 add_pat = mch_expandpath(&ga, p, flags);
10991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 }
10993
10994 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10995 {
10996 char_u *t = backslash_halve_save(p);
10997
10998#if defined(MACOS_CLASSIC)
10999 slash_to_colon(t);
11000#endif
11001 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11002 * "vim c:/" work. */
11003 if (flags & EW_NOTFOUND)
11004 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 addfile(&ga, t, flags);
11007 vim_free(t);
11008 }
11009
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011010#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011011 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011012 uniquefy_paths(&ga, p);
11013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 if (p != pat[i])
11015 vim_free(p);
11016 }
11017
11018 *num_file = ga.ga_len;
11019 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11020
11021 recursive = FALSE;
11022
Bram Moolenaar336bd622016-01-17 18:23:58 +010011023 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024}
11025
11026# ifdef VIM_BACKTICK
11027
11028/*
11029 * Return TRUE if we can expand this backtick thing here.
11030 */
11031 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011032vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
11034 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11035}
11036
11037/*
11038 * Expand an item in `backticks` by executing it as a command.
11039 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011040 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 */
11042 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011043expand_backtick(
11044 garray_T *gap,
11045 char_u *pat,
11046 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047{
11048 char_u *p;
11049 char_u *cmd;
11050 char_u *buffer;
11051 int cnt = 0;
11052 int i;
11053
11054 /* Create the command: lop off the backticks. */
11055 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11056 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011057 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058
11059#ifdef FEAT_EVAL
11060 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011061 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 else
11063#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011064 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011065 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 vim_free(cmd);
11067 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011068 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069
11070 cmd = buffer;
11071 while (*cmd != NUL)
11072 {
11073 cmd = skipwhite(cmd); /* skip over white space */
11074 p = cmd;
11075 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11076 ++p;
11077 /* add an entry if it is not empty */
11078 if (p > cmd)
11079 {
11080 i = *p;
11081 *p = NUL;
11082 addfile(gap, cmd, flags);
11083 *p = i;
11084 ++cnt;
11085 }
11086 cmd = p;
11087 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11088 ++cmd;
11089 }
11090
11091 vim_free(buffer);
11092 return cnt;
11093}
11094# endif /* VIM_BACKTICK */
11095
11096/*
11097 * Add a file to a file list. Accepted flags:
11098 * EW_DIR add directories
11099 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011100 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 * EW_NOTFOUND add even when it doesn't exist
11102 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011103 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 */
11105 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011106addfile(
11107 garray_T *gap,
11108 char_u *f, /* filename */
11109 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110{
11111 char_u *p;
11112 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011113 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011115 /* if the file/dir/link doesn't exist, may not add it */
11116 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011117 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 return;
11119
11120#ifdef FNAME_ILLEGAL
11121 /* if the file/dir contains illegal characters, don't add it */
11122 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11123 return;
11124#endif
11125
11126 isdir = mch_isdir(f);
11127 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11128 return;
11129
Bram Moolenaarb5971142015-03-21 17:32:19 +010011130 /* If the file isn't executable, may not add it. Do accept directories.
11131 * When invoked from expand_shellcmd() do not use $PATH. */
11132 if (!isdir && (flags & EW_EXEC)
11133 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011134 return;
11135
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 /* Make room for another item in the file list. */
11137 if (ga_grow(gap, 1) == FAIL)
11138 return;
11139
11140 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11141 if (p == NULL)
11142 return;
11143
11144 STRCPY(p, f);
11145#ifdef BACKSLASH_IN_FILENAME
11146 slash_adjust(p);
11147#endif
11148 /*
11149 * Append a slash or backslash after directory names if none is present.
11150 */
11151#ifndef DONT_ADD_PATHSEP_TO_DIR
11152 if (isdir && (flags & EW_ADDSLASH))
11153 add_pathsep(p);
11154#endif
11155 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156}
11157#endif /* !NO_EXPANDPATH */
11158
11159#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11160
11161#ifndef SEEK_SET
11162# define SEEK_SET 0
11163#endif
11164#ifndef SEEK_END
11165# define SEEK_END 2
11166#endif
11167
11168/*
11169 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011170 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11171 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 * Returns an allocated string, or NULL for error.
11173 */
11174 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011175get_cmd_output(
11176 char_u *cmd,
11177 char_u *infile, /* optional input file name */
11178 int flags, /* can be SHELL_SILENT */
11179 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180{
11181 char_u *tempname;
11182 char_u *command;
11183 char_u *buffer = NULL;
11184 int len;
11185 int i = 0;
11186 FILE *fd;
11187
11188 if (check_restricted() || check_secure())
11189 return NULL;
11190
11191 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011192 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 {
11194 EMSG(_(e_notmp));
11195 return NULL;
11196 }
11197
11198 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011199 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 if (command == NULL)
11201 goto done;
11202
11203 /*
11204 * Call the shell to execute the command (errors are ignored).
11205 * Don't check timestamps here.
11206 */
11207 ++no_check_timestamps;
11208 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11209 --no_check_timestamps;
11210
11211 vim_free(command);
11212
11213 /*
11214 * read the names from the file into memory
11215 */
11216# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011217 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 fd = mch_fopen((char *)tempname, "r");
11219# else
11220 fd = mch_fopen((char *)tempname, READBIN);
11221# endif
11222
11223 if (fd == NULL)
11224 {
11225 EMSG2(_(e_notopen), tempname);
11226 goto done;
11227 }
11228
11229 fseek(fd, 0L, SEEK_END);
11230 len = ftell(fd); /* get size of temp file */
11231 fseek(fd, 0L, SEEK_SET);
11232
11233 buffer = alloc(len + 1);
11234 if (buffer != NULL)
11235 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11236 fclose(fd);
11237 mch_remove(tempname);
11238 if (buffer == NULL)
11239 goto done;
11240#ifdef VMS
11241 len = i; /* VMS doesn't give us what we asked for... */
11242#endif
11243 if (i != len)
11244 {
11245 EMSG2(_(e_notread), tempname);
11246 vim_free(buffer);
11247 buffer = NULL;
11248 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011249 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011250 {
11251 /* Change NUL into SOH, otherwise the string is truncated. */
11252 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011253 if (buffer[i] == NUL)
11254 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011255
Bram Moolenaar162bd912010-07-28 22:29:10 +020011256 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011257 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011258 else
11259 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260
11261done:
11262 vim_free(tempname);
11263 return buffer;
11264}
11265#endif
11266
11267/*
11268 * Free the list of files returned by expand_wildcards() or other expansion
11269 * functions.
11270 */
11271 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011272FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011274 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 while (count--)
11277 vim_free(files[count]);
11278 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279}
11280
11281/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011282 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 * Don't do this when still processing a command or a mapping.
11284 * Don't do this when inside a ":normal" command.
11285 */
11286 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011287goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
11289 return (p_im && stuff_empty() && typebuf_typed());
11290}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011291
11292/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011293 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011294 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11295 * - Remove any argument. E.g., "csh -f" -> "csh".
11296 * But don't allow a space in the path, so that this works:
11297 * "/usr/bin/csh --rcfile ~/.cshrc"
11298 * But don't do that for Windows, it's common to have a space in the path.
11299 */
11300 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011301get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011302{
11303 char_u *p;
11304
11305#ifdef WIN3264
11306 p = gettail(p_sh);
11307 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11308#else
11309 p = skiptowhite(p_sh);
11310 if (*p == NUL)
11311 {
11312 /* No white space, use the tail. */
11313 p = vim_strsave(gettail(p_sh));
11314 }
11315 else
11316 {
11317 char_u *p1, *p2;
11318
11319 /* Find the last path separator before the space. */
11320 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011321 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011322 if (vim_ispathsep(*p2))
11323 p1 = p2 + 1;
11324 p = vim_strnsave(p1, (int)(p - p1));
11325 }
11326#endif
11327 return p;
11328}