blob: 8ae75e6d85f3ab25885f444c3b79e1e49e00916e [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 Moolenaar44a2f922016-03-19 22:11:51 +01001987#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (wp->w_width == 0)
1989 return 1;
1990#endif
1991
1992#ifdef FEAT_FOLDING
1993 /* A folded lines is handled just like an empty line. */
1994 /* NOTE: Caller must handle lines that are MAYBE folded. */
1995 if (lineFolded(wp, lnum) == TRUE)
1996 return 1;
1997#endif
1998
1999 lines = plines_win_nofold(wp, lnum);
2000 if (winheight > 0 && lines > wp->w_height)
2001 return (int)wp->w_height;
2002 return lines;
2003}
2004
2005/*
2006 * Return number of window lines physical line "lnum" will occupy in window
2007 * "wp". Does not care about folding, 'wrap' or 'diff'.
2008 */
2009 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002010plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 char_u *s;
2013 long col;
2014 int width;
2015
2016 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2017 if (*s == NUL) /* empty line */
2018 return 1;
2019 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2020
2021 /*
2022 * If list mode is on, then the '$' at the end of the line may take up one
2023 * extra column.
2024 */
2025 if (wp->w_p_list && lcs_eol != NUL)
2026 col += 1;
2027
2028 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002029 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 */
2031 width = W_WIDTH(wp) - win_col_off(wp);
2032 if (width <= 0)
2033 return 32000;
2034 if (col <= width)
2035 return 1;
2036 col -= width;
2037 width += win_col_off2(wp);
2038 return (col + (width - 1)) / width + 1;
2039}
2040
2041/*
2042 * Like plines_win(), but only reports the number of physical screen lines
2043 * used from the start of the line to the given column number.
2044 */
2045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002046plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047{
2048 long col;
2049 char_u *s;
2050 int lines = 0;
2051 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002052 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054#ifdef FEAT_DIFF
2055 /* Check for filler lines above this buffer line. When folded the result
2056 * is one line anyway. */
2057 lines = diff_check_fill(wp, lnum);
2058#endif
2059
2060 if (!wp->w_p_wrap)
2061 return lines + 1;
2062
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002063#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (wp->w_width == 0)
2065 return lines + 1;
2066#endif
2067
Bram Moolenaar597a4222014-06-25 14:39:50 +02002068 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 col = 0;
2071 while (*s != NUL && --column >= 0)
2072 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002073 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002074 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076
2077 /*
2078 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2079 * INSERT mode, then col must be adjusted so that it represents the last
2080 * screen position of the TAB. This only fixes an error when the TAB wraps
2081 * from one screen line to the next (when 'columns' is not a multiple of
2082 * 'ts') -- webb.
2083 */
2084 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002085 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002088 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 */
2090 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002091 if (width <= 0)
2092 return 9999;
2093
2094 lines += 1;
2095 if (col > width)
2096 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2097 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098}
2099
2100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002101plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 int count = 0;
2104
2105 while (first <= last)
2106 {
2107#ifdef FEAT_FOLDING
2108 int x;
2109
2110 /* Check if there are any really folded lines, but also included lines
2111 * that are maybe folded. */
2112 x = foldedCount(wp, first, NULL);
2113 if (x > 0)
2114 {
2115 ++count; /* count 1 for "+-- folded" line */
2116 first += x;
2117 }
2118 else
2119#endif
2120 {
2121#ifdef FEAT_DIFF
2122 if (first == wp->w_topline)
2123 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2124 else
2125#endif
2126 count += plines_win(wp, first, TRUE);
2127 ++first;
2128 }
2129 }
2130 return (count);
2131}
2132
2133#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2134/*
2135 * Insert string "p" at the cursor position. Stops at a NUL byte.
2136 * Handles Replace mode and multi-byte characters.
2137 */
2138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 ins_bytes_len(p, (int)STRLEN(p));
2142}
2143#endif
2144
2145#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2146 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2147/*
2148 * Insert string "p" with length "len" at the cursor position.
2149 * Handles Replace mode and multi-byte characters.
2150 */
2151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 int i;
2155# ifdef FEAT_MBYTE
2156 int n;
2157
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002158 if (has_mbyte)
2159 for (i = 0; i < len; i += n)
2160 {
2161 if (enc_utf8)
2162 /* avoid reading past p[len] */
2163 n = utfc_ptr2len_len(p + i, len - i);
2164 else
2165 n = (*mb_ptr2len)(p + i);
2166 ins_char_bytes(p + i, n);
2167 }
2168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002170 for (i = 0; i < len; ++i)
2171 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172}
2173#endif
2174
2175/*
2176 * Insert or replace a single character at the cursor position.
2177 * When in REPLACE or VREPLACE mode, replace any existing character.
2178 * Caller must have prepared for undo.
2179 * For multi-byte characters we get the whole character, the caller must
2180 * convert bytes to a character.
2181 */
2182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002183ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002185 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002186 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002188#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 n = (*mb_char2bytes)(c, buf);
2190
2191 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2192 * Happens for CTRL-Vu9900. */
2193 if (buf[0] == 0)
2194 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002195#else
2196 buf[0] = c;
2197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199 ins_char_bytes(buf, n);
2200}
2201
2202 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002203ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 int newlen; /* nr of bytes inserted */
2207 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2208 char_u *p;
2209 char_u *newp;
2210 char_u *oldp;
2211 int linelen; /* length of old line including NUL */
2212 colnr_T col;
2213 linenr_T lnum = curwin->w_cursor.lnum;
2214 int i;
2215
2216#ifdef FEAT_VIRTUALEDIT
2217 /* Break tabs if needed. */
2218 if (virtual_active() && curwin->w_cursor.coladd > 0)
2219 coladvance_force(getviscol());
2220#endif
2221
2222 col = curwin->w_cursor.col;
2223 oldp = ml_get(lnum);
2224 linelen = (int)STRLEN(oldp) + 1;
2225
2226 /* The lengths default to the values for when not replacing. */
2227 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 if (State & REPLACE_FLAG)
2231 {
2232#ifdef FEAT_VREPLACE
2233 if (State & VREPLACE_FLAG)
2234 {
2235 colnr_T new_vcol = 0; /* init for GCC */
2236 colnr_T vcol;
2237 int old_list;
2238#ifndef FEAT_MBYTE
2239 char_u buf[2];
2240#endif
2241
2242 /*
2243 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2244 * Returns the old value of list, so when finished,
2245 * curwin->w_p_list should be set back to this.
2246 */
2247 old_list = curwin->w_p_list;
2248 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2249 curwin->w_p_list = FALSE;
2250
2251 /*
2252 * In virtual replace mode each character may replace one or more
2253 * characters (zero if it's a TAB). Count the number of bytes to
2254 * be deleted to make room for the new character, counting screen
2255 * cells. May result in adding spaces to fill a gap.
2256 */
2257 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2258#ifndef FEAT_MBYTE
2259 buf[0] = c;
2260 buf[1] = NUL;
2261#endif
2262 new_vcol = vcol + chartabsize(buf, vcol);
2263 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2264 {
2265 vcol += chartabsize(oldp + col + oldlen, vcol);
2266 /* Don't need to remove a TAB that takes us to the right
2267 * position. */
2268 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2269 break;
2270#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002271 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#else
2273 ++oldlen;
2274#endif
2275 /* Deleted a bit too much, insert spaces. */
2276 if (vcol > new_vcol)
2277 newlen += vcol - new_vcol;
2278 }
2279 curwin->w_p_list = old_list;
2280 }
2281 else
2282#endif
2283 if (oldp[col] != NUL)
2284 {
2285 /* normal replace */
2286#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002287 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288#else
2289 oldlen = 1;
2290#endif
2291 }
2292
2293
2294 /* Push the replaced bytes onto the replace stack, so that they can be
2295 * put back when BS is used. The bytes of a multi-byte character are
2296 * done the other way around, so that the first byte is popped off
2297 * first (it tells the byte length of the character). */
2298 replace_push(NUL);
2299 for (i = 0; i < oldlen; ++i)
2300 {
2301#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002302 if (has_mbyte)
2303 i += replace_push_mb(oldp + col + i) - 1;
2304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002306 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308 }
2309
2310 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2311 if (newp == NULL)
2312 return;
2313
2314 /* Copy bytes before the cursor. */
2315 if (col > 0)
2316 mch_memmove(newp, oldp, (size_t)col);
2317
2318 /* Copy bytes after the changed character(s). */
2319 p = newp + col;
2320 mch_memmove(p + newlen, oldp + col + oldlen,
2321 (size_t)(linelen - col - oldlen));
2322
2323 /* Insert or overwrite the new character. */
2324#ifdef FEAT_MBYTE
2325 mch_memmove(p, buf, charlen);
2326 i = charlen;
2327#else
2328 *p = c;
2329 i = 1;
2330#endif
2331
2332 /* Fill with spaces when necessary. */
2333 while (i < newlen)
2334 p[i++] = ' ';
2335
2336 /* Replace the line in the buffer. */
2337 ml_replace(lnum, newp, FALSE);
2338
2339 /* mark the buffer as changed and prepare for displaying */
2340 changed_bytes(lnum, col);
2341
2342 /*
2343 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2344 * show the match for right parens and braces.
2345 */
2346 if (p_sm && (State & INSERT)
2347 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002348#ifdef FEAT_INS_EXPAND
2349 && !ins_compl_active()
2350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002352 {
2353#ifdef FEAT_MBYTE
2354 if (has_mbyte)
2355 showmatch(mb_ptr2char(buf));
2356 else
2357#endif
2358 showmatch(c);
2359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361#ifdef FEAT_RIGHTLEFT
2362 if (!p_ri || (State & REPLACE_FLAG))
2363#endif
2364 {
2365 /* Normal insert: move cursor right */
2366#ifdef FEAT_MBYTE
2367 curwin->w_cursor.col += charlen;
2368#else
2369 ++curwin->w_cursor.col;
2370#endif
2371 }
2372 /*
2373 * TODO: should try to update w_row here, to avoid recomputing it later.
2374 */
2375}
2376
2377/*
2378 * Insert a string at the cursor position.
2379 * Note: Does NOT handle Replace mode.
2380 * Caller must have prepared for undo.
2381 */
2382 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002383ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 char_u *oldp, *newp;
2386 int newlen = (int)STRLEN(s);
2387 int oldlen;
2388 colnr_T col;
2389 linenr_T lnum = curwin->w_cursor.lnum;
2390
2391#ifdef FEAT_VIRTUALEDIT
2392 if (virtual_active() && curwin->w_cursor.coladd > 0)
2393 coladvance_force(getviscol());
2394#endif
2395
2396 col = curwin->w_cursor.col;
2397 oldp = ml_get(lnum);
2398 oldlen = (int)STRLEN(oldp);
2399
2400 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2401 if (newp == NULL)
2402 return;
2403 if (col > 0)
2404 mch_memmove(newp, oldp, (size_t)col);
2405 mch_memmove(newp + col, s, (size_t)newlen);
2406 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2407 ml_replace(lnum, newp, FALSE);
2408 changed_bytes(lnum, col);
2409 curwin->w_cursor.col += newlen;
2410}
2411
2412/*
2413 * Delete one character under the cursor.
2414 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2415 * Caller must have prepared for undo.
2416 *
2417 * return FAIL for failure, OK otherwise
2418 */
2419 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002420del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422#ifdef FEAT_MBYTE
2423 if (has_mbyte)
2424 {
2425 /* Make sure the cursor is at the start of a character. */
2426 mb_adjust_cursor();
2427 if (*ml_get_cursor() == NUL)
2428 return FAIL;
2429 return del_chars(1L, fixpos);
2430 }
2431#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002432 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
2434
2435#if defined(FEAT_MBYTE) || defined(PROTO)
2436/*
2437 * Like del_bytes(), but delete characters instead of bytes.
2438 */
2439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002440del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
2442 long bytes = 0;
2443 long i;
2444 char_u *p;
2445 int l;
2446
2447 p = ml_get_cursor();
2448 for (i = 0; i < count && *p != NUL; ++i)
2449 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002450 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 bytes += l;
2452 p += l;
2453 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002454 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455}
2456#endif
2457
2458/*
2459 * Delete "count" bytes under the cursor.
2460 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2461 * Caller must have prepared for undo.
2462 *
2463 * return FAIL for failure, OK otherwise
2464 */
2465 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002466del_bytes(
2467 long count,
2468 int fixpos_arg,
2469 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471 char_u *oldp, *newp;
2472 colnr_T oldlen;
2473 linenr_T lnum = curwin->w_cursor.lnum;
2474 colnr_T col = curwin->w_cursor.col;
2475 int was_alloced;
2476 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002477 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 oldp = ml_get(lnum);
2480 oldlen = (int)STRLEN(oldp);
2481
2482 /*
2483 * Can't do anything when the cursor is on the NUL after the line.
2484 */
2485 if (col >= oldlen)
2486 return FAIL;
2487
2488#ifdef FEAT_MBYTE
2489 /* If 'delcombine' is set and deleting (less than) one character, only
2490 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002491 if (p_deco && use_delcombine && enc_utf8
2492 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002494 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 int n;
2496
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002497 (void)utfc_ptr2char(oldp + col, cc);
2498 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
2500 /* Find the last composing char, there can be several. */
2501 n = col;
2502 do
2503 {
2504 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002505 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 n += count;
2507 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2508 fixpos = 0;
2509 }
2510 }
2511#endif
2512
2513 /*
2514 * When count is too big, reduce it.
2515 */
2516 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2517 if (movelen <= 1)
2518 {
2519 /*
2520 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002521 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2522 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002524 if (col > 0 && fixpos && restart_edit == 0
2525#ifdef FEAT_VIRTUALEDIT
2526 && (ve_flags & VE_ONEMORE) == 0
2527#endif
2528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 --curwin->w_cursor.col;
2531#ifdef FEAT_VIRTUALEDIT
2532 curwin->w_cursor.coladd = 0;
2533#endif
2534#ifdef FEAT_MBYTE
2535 if (has_mbyte)
2536 curwin->w_cursor.col -=
2537 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2538#endif
2539 }
2540 count = oldlen - col;
2541 movelen = 1;
2542 }
2543
2544 /*
2545 * If the old line has been allocated the deletion can be done in the
2546 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002547 * Can't do this when using Netbeans, because we would need to invoke
2548 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002549 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002552 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002553 was_alloced = FALSE;
2554 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002556 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (was_alloced)
2558 newp = oldp; /* use same allocated memory */
2559 else
2560 { /* need to allocate a new line */
2561 newp = alloc((unsigned)(oldlen + 1 - count));
2562 if (newp == NULL)
2563 return FAIL;
2564 mch_memmove(newp, oldp, (size_t)col);
2565 }
2566 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2567 if (!was_alloced)
2568 ml_replace(lnum, newp, FALSE);
2569
2570 /* mark the buffer as changed and prepare for displaying */
2571 changed_bytes(lnum, curwin->w_cursor.col);
2572
2573 return OK;
2574}
2575
2576/*
2577 * Delete from cursor to end of line.
2578 * Caller must have prepared for undo.
2579 *
2580 * return FAIL for failure, OK otherwise
2581 */
2582 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002583truncate_line(
2584 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585{
2586 char_u *newp;
2587 linenr_T lnum = curwin->w_cursor.lnum;
2588 colnr_T col = curwin->w_cursor.col;
2589
2590 if (col == 0)
2591 newp = vim_strsave((char_u *)"");
2592 else
2593 newp = vim_strnsave(ml_get(lnum), col);
2594
2595 if (newp == NULL)
2596 return FAIL;
2597
2598 ml_replace(lnum, newp, FALSE);
2599
2600 /* mark the buffer as changed and prepare for displaying */
2601 changed_bytes(lnum, curwin->w_cursor.col);
2602
2603 /*
2604 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2605 */
2606 if (fixpos && curwin->w_cursor.col > 0)
2607 --curwin->w_cursor.col;
2608
2609 return OK;
2610}
2611
2612/*
2613 * Delete "nlines" lines at the cursor.
2614 * Saves the lines for undo first if "undo" is TRUE.
2615 */
2616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002617del_lines(
2618 long nlines, /* number of lines to delete */
2619 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
2621 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002622 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
2624 if (nlines <= 0)
2625 return;
2626
2627 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002628 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 return;
2630
2631 for (n = 0; n < nlines; )
2632 {
2633 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2634 break;
2635
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002636 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 ++n;
2638
2639 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002640 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 break;
2642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002644 /* Correct the cursor position before calling deleted_lines_mark(), it may
2645 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 curwin->w_cursor.col = 0;
2647 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002648
2649 /* adjust marks, mark the buffer as changed and prepare for displaying */
2650 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651}
2652
2653 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002654gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 char_u *ptr = ml_get_pos(pos);
2657
2658#ifdef FEAT_MBYTE
2659 if (has_mbyte)
2660 return (*mb_ptr2char)(ptr);
2661#endif
2662 return (int)*ptr;
2663}
2664
2665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002666gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668#ifdef FEAT_MBYTE
2669 if (has_mbyte)
2670 return (*mb_ptr2char)(ml_get_cursor());
2671#endif
2672 return (int)*ml_get_cursor();
2673}
2674
2675/*
2676 * Write a character at the current cursor position.
2677 * It is directly written into the block.
2678 */
2679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002680pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2683 + curwin->w_cursor.col) = c;
2684}
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686/*
2687 * When extra == 0: Return TRUE if the cursor is before or on the first
2688 * non-blank in the line.
2689 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2690 * the line.
2691 */
2692 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002693inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 char_u *ptr;
2696 colnr_T col;
2697
Bram Moolenaar1c465442017-03-12 20:10:05 +01002698 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 ++ptr;
2700 if (col >= curwin->w_cursor.col + extra)
2701 return TRUE;
2702 else
2703 return FALSE;
2704}
2705
2706/*
2707 * Skip to next part of an option argument: Skip space and comma.
2708 */
2709 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002710skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 if (*p == ',')
2713 ++p;
2714 while (*p == ' ')
2715 ++p;
2716 return p;
2717}
2718
2719/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002720 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 *
2722 * Most often called through changed_bytes() and changed_lines(), which also
2723 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002724 *
2725 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
2727 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002728changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002731 if (p_imst == IM_ON_THE_SPOT)
2732 {
2733 /* The text of the preediting area is inserted, but this doesn't
2734 * mean a change of the buffer yet. That is delayed until the
2735 * text is committed. (this means preedit becomes empty) */
2736 if (im_is_preediting() && !xim_changed_while_preediting)
2737 return;
2738 xim_changed_while_preediting = FALSE;
2739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740#endif
2741
2742 if (!curbuf->b_changed)
2743 {
2744 int save_msg_scroll = msg_scroll;
2745
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002746 /* Give a warning about changing a read-only file. This may also
2747 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002749
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 /* Create a swap file if that is wanted.
2751 * Don't do this for "nofile" and "nowrite" buffer types. */
2752 if (curbuf->b_may_swap
2753#ifdef FEAT_QUICKFIX
2754 && !bt_dontwrite(curbuf)
2755#endif
2756 )
2757 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002758 int save_need_wait_return = need_wait_return;
2759
2760 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 ml_open_file(curbuf);
2762
2763 /* The ml_open_file() can cause an ATTENTION message.
2764 * Wait two seconds, to make sure the user reads this unexpected
2765 * message. Since we could be anywhere, call wait_return() now,
2766 * and don't let the emsg() set msg_scroll. */
2767 if (need_wait_return && emsg_silent == 0)
2768 {
2769 out_flush();
2770 ui_delay(2000L, TRUE);
2771 wait_return(TRUE);
2772 msg_scroll = save_msg_scroll;
2773 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002774 else
2775 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002777 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002779 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780}
2781
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002782/*
2783 * Internal part of changed(), no user interaction.
2784 */
2785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002786changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002787{
2788 curbuf->b_changed = TRUE;
2789 ml_setflags(curbuf);
2790#ifdef FEAT_WINDOWS
2791 check_status(curbuf);
2792 redraw_tabline = TRUE;
2793#endif
2794#ifdef FEAT_TITLE
2795 need_maketitle = TRUE; /* set window title later */
2796#endif
2797}
2798
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002799static void changedOneline(buf_T *buf, linenr_T lnum);
2800static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2801static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803/*
2804 * Changed bytes within a single line for the current buffer.
2805 * - marks the windows on this buffer to be redisplayed
2806 * - marks the buffer changed by calling changed()
2807 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002808 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 */
2810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002811changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002813 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002815
2816#ifdef FEAT_DIFF
2817 /* Diff highlighting in other diff windows may need to be updated too. */
2818 if (curwin->w_p_diff)
2819 {
2820 win_T *wp;
2821 linenr_T wlnum;
2822
Bram Moolenaar29323592016-07-24 22:04:11 +02002823 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002824 if (wp->w_p_diff && wp != curwin)
2825 {
2826 redraw_win_later(wp, VALID);
2827 wlnum = diff_lnum_win(lnum, wp);
2828 if (wlnum > 0)
2829 changedOneline(wp->w_buffer, wlnum);
2830 }
2831 }
2832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833}
2834
2835 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002836changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002838 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
2840 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002841 if (lnum < buf->b_mod_top)
2842 buf->b_mod_top = lnum;
2843 else if (lnum >= buf->b_mod_bot)
2844 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846 else
2847 {
2848 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002849 buf->b_mod_set = TRUE;
2850 buf->b_mod_top = lnum;
2851 buf->b_mod_bot = lnum + 1;
2852 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
2854}
2855
2856/*
2857 * Appended "count" lines below line "lnum" in the current buffer.
2858 * Must be called AFTER the change and after mark_adjust().
2859 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2860 */
2861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002862appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 changed_lines(lnum + 1, 0, lnum + 1, count);
2865}
2866
2867/*
2868 * Like appended_lines(), but adjust marks first.
2869 */
2870 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002871appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002873 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002874 * be marks there. But it's still needed in diff mode. */
2875 if (lnum + count < curbuf->b_ml.ml_line_count
2876#ifdef FEAT_DIFF
2877 || curwin->w_p_diff
2878#endif
2879 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002880 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 changed_lines(lnum + 1, 0, lnum + 1, count);
2882}
2883
2884/*
2885 * Deleted "count" lines at line "lnum" in the current buffer.
2886 * Must be called AFTER the change and after mark_adjust().
2887 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2888 */
2889 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002890deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
2892 changed_lines(lnum, 0, lnum + count, -count);
2893}
2894
2895/*
2896 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002897 * Make sure the cursor is on a valid line before calling, a GUI callback may
2898 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 */
2900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002901deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2904 changed_lines(lnum, 0, lnum + count, -count);
2905}
2906
2907/*
2908 * Changed lines for the current buffer.
2909 * Must be called AFTER the change and after mark_adjust().
2910 * - mark the buffer changed by calling changed()
2911 * - mark the windows on this buffer to be redisplayed
2912 * - invalidate cached values
2913 * "lnum" is the first line that needs displaying, "lnume" the first line
2914 * below the changed lines (BEFORE the change).
2915 * When only inserting lines, "lnum" and "lnume" are equal.
2916 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002917 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
2919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002920changed_lines(
2921 linenr_T lnum, /* first line with change */
2922 colnr_T col, /* column in first line with change */
2923 linenr_T lnume, /* line below last changed line */
2924 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002926 changed_lines_buf(curbuf, lnum, lnume, xtra);
2927
2928#ifdef FEAT_DIFF
2929 if (xtra == 0 && curwin->w_p_diff)
2930 {
2931 /* When the number of lines doesn't change then mark_adjust() isn't
2932 * called and other diff buffers still need to be marked for
2933 * displaying. */
2934 win_T *wp;
2935 linenr_T wlnum;
2936
Bram Moolenaar29323592016-07-24 22:04:11 +02002937 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002938 if (wp->w_p_diff && wp != curwin)
2939 {
2940 redraw_win_later(wp, VALID);
2941 wlnum = diff_lnum_win(lnum, wp);
2942 if (wlnum > 0)
2943 changed_lines_buf(wp->w_buffer, wlnum,
2944 lnume - lnum + wlnum, 0L);
2945 }
2946 }
2947#endif
2948
2949 changed_common(lnum, col, lnume, xtra);
2950}
2951
2952 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002953changed_lines_buf(
2954 buf_T *buf,
2955 linenr_T lnum, /* first line with change */
2956 linenr_T lnume, /* line below last changed line */
2957 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002958{
2959 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002962 if (lnum < buf->b_mod_top)
2963 buf->b_mod_top = lnum;
2964 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 {
2966 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002967 buf->b_mod_bot += xtra;
2968 if (buf->b_mod_bot < lnum)
2969 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002971 if (lnume + xtra > buf->b_mod_bot)
2972 buf->b_mod_bot = lnume + xtra;
2973 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 }
2975 else
2976 {
2977 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002978 buf->b_mod_set = TRUE;
2979 buf->b_mod_top = lnum;
2980 buf->b_mod_bot = lnume + xtra;
2981 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002985/*
2986 * Common code for when a change is was made.
2987 * See changed_lines() for the arguments.
2988 * Careful: may trigger autocommands that reload the buffer.
2989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002991changed_common(
2992 linenr_T lnum,
2993 colnr_T col,
2994 linenr_T lnume,
2995 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002998#ifdef FEAT_WINDOWS
2999 tabpage_T *tp;
3000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 int i;
3002#ifdef FEAT_JUMPLIST
3003 int cols;
3004 pos_T *p;
3005 int add;
3006#endif
3007
3008 /* mark the buffer as modified */
3009 changed();
3010
3011 /* set the '. mark */
3012 if (!cmdmod.keepjumps)
3013 {
3014 curbuf->b_last_change.lnum = lnum;
3015 curbuf->b_last_change.col = col;
3016
3017#ifdef FEAT_JUMPLIST
3018 /* Create a new entry if a new undo-able change was started or we
3019 * don't have an entry yet. */
3020 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3021 {
3022 if (curbuf->b_changelistlen == 0)
3023 add = TRUE;
3024 else
3025 {
3026 /* Don't create a new entry when the line number is the same
3027 * as the last one and the column is not too far away. Avoids
3028 * creating many entries for typing "xxxxx". */
3029 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3030 if (p->lnum != lnum)
3031 add = TRUE;
3032 else
3033 {
3034 cols = comp_textwidth(FALSE);
3035 if (cols == 0)
3036 cols = 79;
3037 add = (p->col + cols < col || col + cols < p->col);
3038 }
3039 }
3040 if (add)
3041 {
3042 /* This is the first of a new sequence of undo-able changes
3043 * and it's at some distance of the last change. Use a new
3044 * position in the changelist. */
3045 curbuf->b_new_change = FALSE;
3046
3047 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3048 {
3049 /* changelist is full: remove oldest entry */
3050 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3051 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3052 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003053 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
3055 /* Correct position in changelist for other windows on
3056 * this buffer. */
3057 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3058 --wp->w_changelistidx;
3059 }
3060 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003061 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 {
3063 /* For other windows, if the position in the changelist is
3064 * at the end it stays at the end. */
3065 if (wp->w_buffer == curbuf
3066 && wp->w_changelistidx == curbuf->b_changelistlen)
3067 ++wp->w_changelistidx;
3068 }
3069 ++curbuf->b_changelistlen;
3070 }
3071 }
3072 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3073 curbuf->b_last_change;
3074 /* The current window is always after the last change, so that "g,"
3075 * takes you back to it. */
3076 curwin->w_changelistidx = curbuf->b_changelistlen;
3077#endif
3078 }
3079
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003080 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 {
3082 if (wp->w_buffer == curbuf)
3083 {
3084 /* Mark this window to be redrawn later. */
3085 if (wp->w_redr_type < VALID)
3086 wp->w_redr_type = VALID;
3087
3088 /* Check if a change in the buffer has invalidated the cached
3089 * values for the cursor. */
3090#ifdef FEAT_FOLDING
3091 /*
3092 * Update the folds for this window. Can't postpone this, because
3093 * a following operator might work on the whole fold: ">>dd".
3094 */
3095 foldUpdate(wp, lnum, lnume + xtra - 1);
3096
3097 /* The change may cause lines above or below the change to become
3098 * included in a fold. Set lnum/lnume to the first/last line that
3099 * might be displayed differently.
3100 * Set w_cline_folded here as an efficient way to update it when
3101 * inserting lines just above a closed fold. */
3102 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3103 if (wp->w_cursor.lnum == lnum)
3104 wp->w_cline_folded = i;
3105 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3106 if (wp->w_cursor.lnum == lnume)
3107 wp->w_cline_folded = i;
3108
3109 /* If the changed line is in a range of previously folded lines,
3110 * compare with the first line in that range. */
3111 if (wp->w_cursor.lnum <= lnum)
3112 {
3113 i = find_wl_entry(wp, lnum);
3114 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3115 changed_line_abv_curs_win(wp);
3116 }
3117#endif
3118
3119 if (wp->w_cursor.lnum > lnum)
3120 changed_line_abv_curs_win(wp);
3121 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3122 changed_cline_bef_curs_win(wp);
3123 if (wp->w_botline >= lnum)
3124 {
3125 /* Assume that botline doesn't change (inserted lines make
3126 * other lines scroll down below botline). */
3127 approximate_botline_win(wp);
3128 }
3129
3130 /* Check if any w_lines[] entries have become invalid.
3131 * For entries below the change: Correct the lnums for
3132 * inserted/deleted lines. Makes it possible to stop displaying
3133 * after the change. */
3134 for (i = 0; i < wp->w_lines_valid; ++i)
3135 if (wp->w_lines[i].wl_valid)
3136 {
3137 if (wp->w_lines[i].wl_lnum >= lnum)
3138 {
3139 if (wp->w_lines[i].wl_lnum < lnume)
3140 {
3141 /* line included in change */
3142 wp->w_lines[i].wl_valid = FALSE;
3143 }
3144 else if (xtra != 0)
3145 {
3146 /* line below change */
3147 wp->w_lines[i].wl_lnum += xtra;
3148#ifdef FEAT_FOLDING
3149 wp->w_lines[i].wl_lastlnum += xtra;
3150#endif
3151 }
3152 }
3153#ifdef FEAT_FOLDING
3154 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3155 {
3156 /* change somewhere inside this range of folded lines,
3157 * may need to be redrawn */
3158 wp->w_lines[i].wl_valid = FALSE;
3159 }
3160#endif
3161 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003162
3163#ifdef FEAT_FOLDING
3164 /* Take care of side effects for setting w_topline when folds have
3165 * changed. Esp. when the buffer was changed in another window. */
3166 if (hasAnyFolding(wp))
3167 set_topline(wp, wp->w_topline);
3168#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003169 /* relative numbering may require updating more */
3170 if (wp->w_p_rnu)
3171 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 }
3173 }
3174
3175 /* Call update_screen() later, which checks out what needs to be redrawn,
3176 * since it notices b_mod_set and then uses b_mod_*. */
3177 if (must_redraw < VALID)
3178 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003179
3180#ifdef FEAT_AUTOCMD
3181 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003182 if (lnum <= curwin->w_cursor.lnum
3183 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003184 last_cursormoved.lnum = 0;
3185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186}
3187
3188/*
3189 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3190 */
3191 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003192unchanged(
3193 buf_T *buf,
3194 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003196 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
3198 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003199 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (ff)
3201 save_file_ff(buf);
3202#ifdef FEAT_WINDOWS
3203 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003204 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#endif
3206#ifdef FEAT_TITLE
3207 need_maketitle = TRUE; /* set window title later */
3208#endif
3209 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003210 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#ifdef FEAT_NETBEANS_INTG
3212 netbeans_unmodified(buf);
3213#endif
3214}
3215
3216#if defined(FEAT_WINDOWS) || defined(PROTO)
3217/*
3218 * check_status: called when the status bars for the buffer 'buf'
3219 * need to be updated
3220 */
3221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003222check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 win_T *wp;
3225
Bram Moolenaar29323592016-07-24 22:04:11 +02003226 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (wp->w_buffer == buf && wp->w_status_height)
3228 {
3229 wp->w_redr_status = TRUE;
3230 if (must_redraw < VALID)
3231 must_redraw = VALID;
3232 }
3233}
3234#endif
3235
3236/*
3237 * If the file is readonly, give a warning message with the first change.
3238 * Don't do this for autocommands.
3239 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003240 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003242 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
3244 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003245change_warning(
3246 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 mode and 'showmode' is on */
3248{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003249 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (curbuf->b_did_warn == FALSE
3252 && curbufIsChanged() == 0
3253#ifdef FEAT_AUTOCMD
3254 && !autocmd_busy
3255#endif
3256 && curbuf->b_p_ro)
3257 {
3258#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003259 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003261 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (!curbuf->b_p_ro)
3263 return;
3264#endif
3265 /*
3266 * Do what msg() does, but with a column offset if the warning should
3267 * be after the mode message.
3268 */
3269 msg_start();
3270 if (msg_row == Rows - 1)
3271 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003272 msg_source(HL_ATTR(HLF_W));
3273 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003274#ifdef FEAT_EVAL
3275 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 msg_clr_eos();
3278 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003279 if (msg_silent == 0 && !silent_mode
3280#ifdef FEAT_EVAL
3281 && time_for_testing != 1
3282#endif
3283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
3285 out_flush();
3286 ui_delay(1000L, TRUE); /* give the user time to think about it */
3287 }
3288 curbuf->b_did_warn = TRUE;
3289 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3290 if (msg_row < Rows - 1)
3291 showmode();
3292 }
3293}
3294
3295/*
3296 * Ask for a reply from the user, a 'y' or a 'n'.
3297 * No other characters are accepted, the message is repeated until a valid
3298 * reply is entered or CTRL-C is hit.
3299 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3300 * from any buffers but directly from the user.
3301 *
3302 * return the 'y' or 'n'
3303 */
3304 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003305ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 int r = ' ';
3308 int save_State = State;
3309
3310 if (exiting) /* put terminal in raw mode for this question */
3311 settmode(TMODE_RAW);
3312 ++no_wait_return;
3313#ifdef USE_ON_FLY_SCROLL
3314 dont_scroll = TRUE; /* disallow scrolling here */
3315#endif
3316 State = CONFIRM; /* mouse behaves like with :confirm */
3317#ifdef FEAT_MOUSE
3318 setmouse(); /* disables mouse for xterm */
3319#endif
3320 ++no_mapping;
3321 ++allow_keys; /* no mapping here, but recognize keys */
3322
3323 while (r != 'y' && r != 'n')
3324 {
3325 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003326 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (direct)
3328 r = get_keystroke();
3329 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003330 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (r == Ctrl_C || r == ESC)
3332 r = 'n';
3333 msg_putchar(r); /* show what you typed */
3334 out_flush();
3335 }
3336 --no_wait_return;
3337 State = save_State;
3338#ifdef FEAT_MOUSE
3339 setmouse();
3340#endif
3341 --no_mapping;
3342 --allow_keys;
3343
3344 return r;
3345}
3346
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003347#if defined(FEAT_MOUSE) || defined(PROTO)
3348/*
3349 * Return TRUE if "c" is a mouse key.
3350 */
3351 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003352is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003353{
3354 return c == K_LEFTMOUSE
3355 || c == K_LEFTMOUSE_NM
3356 || c == K_LEFTDRAG
3357 || c == K_LEFTRELEASE
3358 || c == K_LEFTRELEASE_NM
3359 || c == K_MIDDLEMOUSE
3360 || c == K_MIDDLEDRAG
3361 || c == K_MIDDLERELEASE
3362 || c == K_RIGHTMOUSE
3363 || c == K_RIGHTDRAG
3364 || c == K_RIGHTRELEASE
3365 || c == K_MOUSEDOWN
3366 || c == K_MOUSEUP
3367 || c == K_MOUSELEFT
3368 || c == K_MOUSERIGHT
3369 || c == K_X1MOUSE
3370 || c == K_X1DRAG
3371 || c == K_X1RELEASE
3372 || c == K_X2MOUSE
3373 || c == K_X2DRAG
3374 || c == K_X2RELEASE;
3375}
3376#endif
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378/*
3379 * Get a key stroke directly from the user.
3380 * Ignores mouse clicks and scrollbar events, except a click for the left
3381 * button (used at the more prompt).
3382 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3383 * Disadvantage: typeahead is ignored.
3384 * Translates the interrupt character for unix to ESC.
3385 */
3386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003387get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003389 char_u *buf = NULL;
3390 int buflen = 150;
3391 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 int len = 0;
3393 int n;
3394 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003395 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
3397 mapped_ctrl_c = FALSE; /* mappings are not used here */
3398 for (;;)
3399 {
3400 cursor_on();
3401 out_flush();
3402
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003403 /* Leave some room for check_termcode() to insert a key code into (max
3404 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3405 * bytes. */
3406 maxlen = (buflen - 6 - len) / 3;
3407 if (buf == NULL)
3408 buf = alloc(buflen);
3409 else if (maxlen < 10)
3410 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003411 char_u *t_buf = buf;
3412
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003413 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003414 * escape sequence. */
3415 buflen += 100;
3416 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003417 if (buf == NULL)
3418 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003419 maxlen = (buflen - 6 - len) / 3;
3420 }
3421 if (buf == NULL)
3422 {
3423 do_outofmem_msg((long_u)buflen);
3424 return ESC; /* panic! */
3425 }
3426
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003428 * terminal code to complete. */
3429 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (n > 0)
3431 {
3432 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003433 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003435 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003437 else if (len > 0)
3438 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar4395a712006-09-05 18:57:57 +00003440 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003441 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003442 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003444
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003445 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003446 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003447 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003448 {
3449 /* Redrawing was postponed, do it now. */
3450 update_screen(0);
3451 setcursor(); /* put cursor back where it belongs */
3452 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003453 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003454 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003455 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003457 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 continue;
3459
3460 /* Handle modifier and/or special key code. */
3461 n = buf[0];
3462 if (n == K_SPECIAL)
3463 {
3464 n = TO_SPECIAL(buf[1], buf[2]);
3465 if (buf[1] == KS_MODIFIER
3466 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003467#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003468 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003469#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003470#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 || n == K_VER_SCROLLBAR
3472 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
3474 )
3475 {
3476 if (buf[1] == KS_MODIFIER)
3477 mod_mask = buf[2];
3478 len -= 3;
3479 if (len > 0)
3480 mch_memmove(buf, buf + 3, (size_t)len);
3481 continue;
3482 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485#ifdef FEAT_MBYTE
3486 if (has_mbyte)
3487 {
3488 if (MB_BYTE2LEN(n) > len)
3489 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003490 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 n = (*mb_ptr2char)(buf);
3492 }
3493#endif
3494#ifdef UNIX
3495 if (n == intr_char)
3496 n = ESC;
3497#endif
3498 break;
3499 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003500 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 mapped_ctrl_c = save_mapped_ctrl_c;
3503 return n;
3504}
3505
3506/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003507 * Get a number from the user.
3508 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
3510 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003511get_number(
3512 int colon, /* allow colon to abort */
3513 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 int n = 0;
3516 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003517 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003519 if (mouse_used != NULL)
3520 *mouse_used = FALSE;
3521
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 /* When not printing messages, the user won't know what to type, return a
3523 * zero (as if CR was hit). */
3524 if (msg_silent != 0)
3525 return 0;
3526
3527#ifdef USE_ON_FLY_SCROLL
3528 dont_scroll = TRUE; /* disallow scrolling here */
3529#endif
3530 ++no_mapping;
3531 ++allow_keys; /* no mapping here, but recognize keys */
3532 for (;;)
3533 {
3534 windgoto(msg_row, msg_col);
3535 c = safe_vgetc();
3536 if (VIM_ISDIGIT(c))
3537 {
3538 n = n * 10 + c - '0';
3539 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003540 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
3542 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3543 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003544 if (typed > 0)
3545 {
3546 MSG_PUTS("\b \b");
3547 --typed;
3548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003551#ifdef FEAT_MOUSE
3552 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3553 {
3554 *mouse_used = TRUE;
3555 n = mouse_row + 1;
3556 break;
3557 }
3558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 else if (n == 0 && c == ':' && colon)
3560 {
3561 stuffcharReadbuff(':');
3562 if (!exmode_active)
3563 cmdline_row = msg_row;
3564 skip_redraw = TRUE; /* skip redraw once */
3565 do_redraw = FALSE;
3566 break;
3567 }
3568 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3569 break;
3570 }
3571 --no_mapping;
3572 --allow_keys;
3573 return n;
3574}
3575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003576/*
3577 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003578 * When "mouse_used" is not NULL allow using the mouse and in that case return
3579 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580 */
3581 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003582prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003583{
3584 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003585 int save_cmdline_row;
3586 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003587
3588 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003589 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003590 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003591 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003592 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003593
Bram Moolenaar203335e2006-09-03 14:35:42 +00003594 /* Set the state such that text can be selected/copied/pasted and we still
3595 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003596 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003597 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003598 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003599 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003600
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003601 i = get_number(TRUE, mouse_used);
3602 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003604 /* don't call wait_return() now */
3605 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606 cmdline_row = msg_row - 1;
3607 need_wait_return = FALSE;
3608 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003609 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003610 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003611 else
3612 cmdline_row = save_cmdline_row;
3613 State = save_State;
3614
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003615 return i;
3616}
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003619msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 long pn;
3622
3623 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3625 return;
3626
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003627 /* We don't want to overwrite another important message, but do overwrite
3628 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3629 * then "put" reports the last action. */
3630 if (keep_msg != NULL && !keep_msg_more)
3631 return;
3632
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (n > 0)
3634 pn = n;
3635 else
3636 pn = -n;
3637
3638 if (pn > p_report)
3639 {
3640 if (pn == 1)
3641 {
3642 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003643 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3644 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003646 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3647 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
3649 else
3650 {
3651 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003652 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3653 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003655 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3656 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003659 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 if (msg(msg_buf))
3661 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003662 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003663 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665 }
3666}
3667
3668/*
3669 * flush map and typeahead buffers and give a warning for an error
3670 */
3671 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003672beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 if (emsg_silent == 0)
3675 {
3676 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003677 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679}
3680
3681/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003682 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 */
3684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003685vim_beep(
3686 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 if (emsg_silent == 0)
3689 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003690 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3691 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003692#ifdef ELAPSED_FUNC
3693 static int did_init = FALSE;
3694 static ELAPSED_TYPE start_tv;
3695
3696 /* Only beep once per half a second, otherwise a sequence of beeps
3697 * would freeze Vim. */
3698 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3699 {
3700 did_init = TRUE;
3701 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003703 if (p_vb
3704#ifdef FEAT_GUI
3705 /* While the GUI is starting up the termcap is set for
3706 * the GUI but the output still goes to a terminal. */
3707 && !(gui.in_use && gui.starting)
3708#endif
3709 )
3710 out_str_cf(T_VB);
3711 else
3712 out_char(BELL);
3713#ifdef ELAPSED_FUNC
3714 }
3715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003717
3718 /* When 'verbose' is set and we are sourcing a script or executing a
3719 * function give the user a hint where the beep comes from. */
3720 if (vim_strchr(p_debug, 'e') != NULL)
3721 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003722 msg_source(HL_ATTR(HLF_W));
3723 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726}
3727
3728/*
3729 * To get the "real" home directory:
3730 * - get value of $HOME
3731 * For Unix:
3732 * - go to that directory
3733 * - do mch_dirname() to get the real name of that directory.
3734 * This also works with mounts and links.
3735 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3736 */
3737static char_u *homedir = NULL;
3738
3739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003740init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 char_u *var;
3743
Bram Moolenaar05159a02005-02-26 23:04:13 +00003744 /* In case we are called a second time (when 'encoding' changes). */
3745 vim_free(homedir);
3746 homedir = NULL;
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748#ifdef VMS
3749 var = mch_getenv((char_u *)"SYS$LOGIN");
3750#else
3751 var = mch_getenv((char_u *)"HOME");
3752#endif
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754#ifdef WIN3264
3755 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003756 * Typically, $HOME is not defined on Windows, unless the user has
3757 * specifically defined it for Vim's sake. However, on Windows NT
3758 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3759 * each user. Try constructing $HOME from these.
3760 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003761 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003762 {
3763 char_u *homedrive, *homepath;
3764
3765 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3766 homepath = mch_getenv((char_u *)"HOMEPATH");
3767 if (homepath == NULL || *homepath == NUL)
3768 homepath = (char_u *)"\\";
3769 if (homedrive != NULL
3770 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3771 {
3772 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3773 if (NameBuff[0] != NUL)
3774 var = NameBuff;
3775 }
3776 }
3777
3778 if (var == NULL)
3779 var = mch_getenv((char_u *)"USERPROFILE");
3780
3781 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * Weird but true: $HOME may contain an indirect reference to another
3783 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3784 * when $HOME is being set.
3785 */
3786 if (var != NULL && *var == '%')
3787 {
3788 char_u *p;
3789 char_u *exp;
3790
3791 p = vim_strchr(var + 1, '%');
3792 if (p != NULL)
3793 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003794 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 exp = mch_getenv(NameBuff);
3796 if (exp != NULL && *exp != NUL
3797 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3798 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003799 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 }
3803 }
3804
Bram Moolenaar48340b62017-08-29 22:08:53 +02003805 if (var != NULL && *var == NUL) /* empty is same as not set */
3806 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
Bram Moolenaar48340b62017-08-29 22:08:53 +02003808# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003809 if (enc_utf8 && var != NULL)
3810 {
3811 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003812 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003813
3814 /* Convert from active codepage to UTF-8. Other conversions are
3815 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003816 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003817 if (pp != NULL)
3818 {
3819 homedir = pp;
3820 return;
3821 }
3822 }
3823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 /*
3826 * Default home dir is C:/
3827 * Best assumption we can make in such a situation.
3828 */
3829 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003830 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (var != NULL)
3834 {
3835#ifdef UNIX
3836 /*
3837 * Change to the directory and get the actual path. This resolves
3838 * links. Don't do it when we can't return.
3839 */
3840 if (mch_dirname(NameBuff, MAXPATHL) == OK
3841 && mch_chdir((char *)NameBuff) == 0)
3842 {
3843 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3844 var = IObuff;
3845 if (mch_chdir((char *)NameBuff) != 0)
3846 EMSG(_(e_prev_dir));
3847 }
3848#endif
3849 homedir = vim_strsave(var);
3850 }
3851}
3852
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003853#if defined(EXITFREE) || defined(PROTO)
3854 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003855free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003856{
3857 vim_free(homedir);
3858}
Bram Moolenaar24305862012-08-15 14:05:05 +02003859
3860# ifdef FEAT_CMDL_COMPL
3861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003862free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003863{
3864 ga_clear_strings(&ga_users);
3865}
3866# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003867#endif
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003870 * Call expand_env() and store the result in an allocated string.
3871 * This is not very memory efficient, this expects the result to be freed
3872 * again soon.
3873 */
3874 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003875expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003876{
3877 return expand_env_save_opt(src, FALSE);
3878}
3879
3880/*
3881 * Idem, but when "one" is TRUE handle the string as one file name, only
3882 * expand "~" at the start.
3883 */
3884 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003885expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003886{
3887 char_u *p;
3888
3889 p = alloc(MAXPATHL);
3890 if (p != NULL)
3891 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3892 return p;
3893}
3894
3895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 * Expand environment variable with path name.
3897 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003898 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 * If anything fails no expansion is done and dst equals src.
3900 */
3901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003902expand_env(
3903 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3904 char_u *dst, /* where to put the result */
3905 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003907 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908}
3909
3910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003911expand_env_esc(
3912 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3913 char_u *dst, /* where to put the result */
3914 int dstlen, /* maximum length of the result */
3915 int esc, /* escape spaces in expanded variables */
3916 int one, /* "srcp" is one file name */
3917 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003919 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 char_u *tail;
3921 int c;
3922 char_u *var;
3923 int copy_char;
3924 int mustfree; /* var was allocated, need to free it later */
3925 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003926 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003928 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003929 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003930
3931 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 --dstlen; /* leave one char space for "\," */
3933 while (*src && dstlen > 0)
3934 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003935#ifdef FEAT_EVAL
3936 /* Skip over `=expr`. */
3937 if (src[0] == '`' && src[1] == '=')
3938 {
3939 size_t len;
3940
3941 var = src;
3942 src += 2;
3943 (void)skip_expr(&src);
3944 if (*src == '`')
3945 ++src;
3946 len = src - var;
3947 if (len > (size_t)dstlen)
3948 len = dstlen;
3949 vim_strncpy(dst, var, len);
3950 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003951 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003952 continue;
3953 }
3954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003956 if ((*src == '$'
3957#ifdef VMS
3958 && at_start
3959#endif
3960 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003961#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 || *src == '%'
3963#endif
3964 || (*src == '~' && at_start))
3965 {
3966 mustfree = FALSE;
3967
3968 /*
3969 * The variable name is copied into dst temporarily, because it may
3970 * be a string in read-only memory and a NUL needs to be appended.
3971 */
3972 if (*src != '~') /* environment var */
3973 {
3974 tail = src + 1;
3975 var = dst;
3976 c = dstlen - 1;
3977
3978#ifdef UNIX
3979 /* Unix has ${var-name} type environment vars */
3980 if (*tail == '{' && !vim_isIDc('{'))
3981 {
3982 tail++; /* ignore '{' */
3983 while (c-- > 0 && *tail && *tail != '}')
3984 *var++ = *tail++;
3985 }
3986 else
3987#endif
3988 {
3989 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003990#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 || (*src == '%' && *tail != '%')
3992#endif
3993 ))
3994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997 }
3998
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003999#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000# ifdef UNIX
4001 if (src[1] == '{' && *tail != '}')
4002# else
4003 if (*src == '%' && *tail != '%')
4004# endif
4005 var = NULL;
4006 else
4007 {
4008# ifdef UNIX
4009 if (src[1] == '{')
4010# else
4011 if (*src == '%')
4012#endif
4013 ++tail;
4014#endif
4015 *var = NUL;
4016 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004017#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019#endif
4020 }
4021 /* home directory */
4022 else if ( src[1] == NUL
4023 || vim_ispathsep(src[1])
4024 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4025 {
4026 var = homedir;
4027 tail = src + 1;
4028 }
4029 else /* user directory */
4030 {
4031#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4032 /*
4033 * Copy ~user to dst[], so we can put a NUL after it.
4034 */
4035 tail = src;
4036 var = dst;
4037 c = dstlen - 1;
4038 while ( c-- > 0
4039 && *tail
4040 && vim_isfilec(*tail)
4041 && !vim_ispathsep(*tail))
4042 *var++ = *tail++;
4043 *var = NUL;
4044# ifdef UNIX
4045 /*
4046 * If the system supports getpwnam(), use it.
4047 * Otherwise, or if getpwnam() fails, the shell is used to
4048 * expand ~user. This is slower and may fail if the shell
4049 * does not support ~user (old versions of /bin/sh).
4050 */
4051# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4052 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004053 /* Note: memory allocated by getpwnam() is never freed.
4054 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004055 struct passwd *pw = (*dst == NUL)
4056 ? NULL : getpwnam((char *)dst + 1);
4057
4058 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 if (var == NULL)
4061# endif
4062 {
4063 expand_T xpc;
4064
4065 ExpandInit(&xpc);
4066 xpc.xp_context = EXPAND_FILES;
4067 var = ExpandOne(&xpc, dst, NULL,
4068 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 mustfree = TRUE;
4070 }
4071
4072# else /* !UNIX, thus VMS */
4073 /*
4074 * USER_HOME is a comma-separated list of
4075 * directories to search for the user account in.
4076 */
4077 {
4078 char_u test[MAXPATHL], paths[MAXPATHL];
4079 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004080 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 STRCPY(paths, USER_HOME);
4083 next_path = paths;
4084 while (*next_path)
4085 {
4086 for (path = next_path; *next_path && *next_path != ',';
4087 next_path++);
4088 if (*next_path)
4089 *next_path++ = NUL;
4090 STRCPY(test, path);
4091 STRCAT(test, "/");
4092 STRCAT(test, dst + 1);
4093 if (mch_stat(test, &st) == 0)
4094 {
4095 var = alloc(STRLEN(test) + 1);
4096 STRCPY(var, test);
4097 mustfree = TRUE;
4098 break;
4099 }
4100 }
4101 }
4102# endif /* UNIX */
4103#else
4104 /* cannot expand user's home directory, so don't try */
4105 var = NULL;
4106 tail = (char_u *)""; /* for gcc */
4107#endif /* UNIX || VMS */
4108 }
4109
4110#ifdef BACKSLASH_IN_FILENAME
4111 /* If 'shellslash' is set change backslashes to forward slashes.
4112 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4113 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4114 {
4115 char_u *p = vim_strsave(var);
4116
4117 if (p != NULL)
4118 {
4119 if (mustfree)
4120 vim_free(var);
4121 var = p;
4122 mustfree = TRUE;
4123 forward_slash(var);
4124 }
4125 }
4126#endif
4127
4128 /* If "var" contains white space, escape it with a backslash.
4129 * Required for ":e ~/tt" when $HOME includes a space. */
4130 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4131 {
4132 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4133
4134 if (p != NULL)
4135 {
4136 if (mustfree)
4137 vim_free(var);
4138 var = p;
4139 mustfree = TRUE;
4140 }
4141 }
4142
4143 if (var != NULL && *var != NUL
4144 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4145 {
4146 STRCPY(dst, var);
4147 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004148 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 /* if var[] ends in a path separator and tail[] starts
4150 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004151 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4153 && dst[-1] != ':'
4154#endif
4155 && vim_ispathsep(*tail))
4156 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004157 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 src = tail;
4159 copy_char = FALSE;
4160 }
4161 if (mustfree)
4162 vim_free(var);
4163 }
4164
4165 if (copy_char) /* copy at least one char */
4166 {
4167 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004168 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004169 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4170 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 */
4172 at_start = FALSE;
4173 if (src[0] == '\\' && src[1] != NUL)
4174 {
4175 *dst++ = *src++;
4176 --dstlen;
4177 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004178 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004180 if (dstlen > 0)
4181 {
4182 *dst++ = *src++;
4183 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004184
Bram Moolenaar1c864092017-08-06 18:15:45 +02004185 if (startstr != NULL && src - startstr_len >= srcp
4186 && STRNCMP(src - startstr_len, startstr,
4187 startstr_len) == 0)
4188 at_start = TRUE;
4189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 *dst = NUL;
4194}
4195
4196/*
4197 * Vim's version of getenv().
4198 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004199 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004200 * "mustfree" is set to TRUE when returned is allocated, it must be
4201 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 */
4203 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004204vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205{
4206 char_u *p;
4207 char_u *pend;
4208 int vimruntime;
4209
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004210#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 /* use "C:/" when $HOME is not set */
4212 if (STRCMP(name, "HOME") == 0)
4213 return homedir;
4214#endif
4215
4216 p = mch_getenv(name);
4217 if (p != NULL && *p == NUL) /* empty is the same as not set */
4218 p = NULL;
4219
4220 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004221 {
4222#if defined(FEAT_MBYTE) && defined(WIN3264)
4223 if (enc_utf8)
4224 {
4225 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004226 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004227
4228 /* Convert from active codepage to UTF-8. Other conversions are
4229 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004230 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004231 if (pp != NULL)
4232 {
4233 p = pp;
4234 *mustfree = TRUE;
4235 }
4236 }
4237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4242 if (!vimruntime && STRCMP(name, "VIM") != 0)
4243 return NULL;
4244
4245 /*
4246 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4247 * Don't do this when default_vimruntime_dir is non-empty.
4248 */
4249 if (vimruntime
4250#ifdef HAVE_PATHDEF
4251 && *default_vimruntime_dir == NUL
4252#endif
4253 )
4254 {
4255 p = mch_getenv((char_u *)"VIM");
4256 if (p != NULL && *p == NUL) /* empty is the same as not set */
4257 p = NULL;
4258 if (p != NULL)
4259 {
4260 p = vim_version_dir(p);
4261 if (p != NULL)
4262 *mustfree = TRUE;
4263 else
4264 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004265
4266#if defined(FEAT_MBYTE) && defined(WIN3264)
4267 if (enc_utf8)
4268 {
4269 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004270 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004271
4272 /* Convert from active codepage to UTF-8. Other conversions
4273 * are not done, because they would fail for non-ASCII
4274 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004275 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004276 if (pp != NULL)
4277 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004278 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004279 vim_free(p);
4280 p = pp;
4281 *mustfree = TRUE;
4282 }
4283 }
4284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 /*
4289 * When expanding $VIM or $VIMRUNTIME fails, try using:
4290 * - the directory name from 'helpfile' (unless it contains '$')
4291 * - the executable name from argv[0]
4292 */
4293 if (p == NULL)
4294 {
4295 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4296 p = p_hf;
4297#ifdef USE_EXE_NAME
4298 /*
4299 * Use the name of the executable, obtained from argv[0].
4300 */
4301 else
4302 p = exe_name;
4303#endif
4304 if (p != NULL)
4305 {
4306 /* remove the file name */
4307 pend = gettail(p);
4308
4309 /* remove "doc/" from 'helpfile', if present */
4310 if (p == p_hf)
4311 pend = remove_tail(p, pend, (char_u *)"doc");
4312
4313#ifdef USE_EXE_NAME
4314# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004315 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 if (p == exe_name)
4317 {
4318 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004319 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004321 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4322 if (pend1 != pend)
4323 {
4324 pnew = alloc((unsigned)(pend1 - p) + 15);
4325 if (pnew != NULL)
4326 {
4327 STRNCPY(pnew, p, (pend1 - p));
4328 STRCPY(pnew + (pend1 - p), "Resources/vim");
4329 p = pnew;
4330 pend = p + STRLEN(p);
4331 }
4332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334# endif
4335 /* remove "src/" from exe_name, if present */
4336 if (p == exe_name)
4337 pend = remove_tail(p, pend, (char_u *)"src");
4338#endif
4339
4340 /* for $VIM, remove "runtime/" or "vim54/", if present */
4341 if (!vimruntime)
4342 {
4343 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4344 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4345 }
4346
4347 /* remove trailing path separator */
4348#ifndef MACOS_CLASSIC
4349 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004350 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004351 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 --pend;
4353#endif
4354
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004355#ifdef MACOS_X
4356 if (p == exe_name || p == p_hf)
4357#endif
4358 /* check that the result is a directory name */
4359 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 if (p != NULL && !mch_isdir(p))
4362 {
4363 vim_free(p);
4364 p = NULL;
4365 }
4366 else
4367 {
4368#ifdef USE_EXE_NAME
4369 /* may add "/vim54" or "/runtime" if it exists */
4370 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4371 {
4372 vim_free(p);
4373 p = pend;
4374 }
4375#endif
4376 *mustfree = TRUE;
4377 }
4378 }
4379 }
4380
4381#ifdef HAVE_PATHDEF
4382 /* When there is a pathdef.c file we can use default_vim_dir and
4383 * default_vimruntime_dir */
4384 if (p == NULL)
4385 {
4386 /* Only use default_vimruntime_dir when it is not empty */
4387 if (vimruntime && *default_vimruntime_dir != NUL)
4388 {
4389 p = default_vimruntime_dir;
4390 *mustfree = FALSE;
4391 }
4392 else if (*default_vim_dir != NUL)
4393 {
4394 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4395 *mustfree = TRUE;
4396 else
4397 {
4398 p = default_vim_dir;
4399 *mustfree = FALSE;
4400 }
4401 }
4402 }
4403#endif
4404
4405 /*
4406 * Set the environment variable, so that the new value can be found fast
4407 * next time, and others can also use it (e.g. Perl).
4408 */
4409 if (p != NULL)
4410 {
4411 if (vimruntime)
4412 {
4413 vim_setenv((char_u *)"VIMRUNTIME", p);
4414 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 else
4417 {
4418 vim_setenv((char_u *)"VIM", p);
4419 didset_vim = TRUE;
4420 }
4421 }
4422 return p;
4423}
4424
4425/*
4426 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4427 * Return NULL if not, return its name in allocated memory otherwise.
4428 */
4429 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004430vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
4432 char_u *p;
4433
4434 if (vimdir == NULL || *vimdir == NUL)
4435 return NULL;
4436 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4437 if (p != NULL && mch_isdir(p))
4438 return p;
4439 vim_free(p);
4440 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4441 if (p != NULL && mch_isdir(p))
4442 return p;
4443 vim_free(p);
4444 return NULL;
4445}
4446
4447/*
4448 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4449 * the length of "name/". Otherwise return "pend".
4450 */
4451 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004452remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454 int len = (int)STRLEN(name) + 1;
4455 char_u *newend = pend - len;
4456
4457 if (newend >= p
4458 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004459 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 return newend;
4461 return pend;
4462}
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 * Our portable version of setenv.
4466 */
4467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004468vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
4470#ifdef HAVE_SETENV
4471 mch_setenv((char *)name, (char *)val, 1);
4472#else
4473 char_u *envbuf;
4474
4475 /*
4476 * Putenv does not copy the string, it has to remain
4477 * valid. The allocated memory will never be freed.
4478 */
4479 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4480 if (envbuf != NULL)
4481 {
4482 sprintf((char *)envbuf, "%s=%s", name, val);
4483 putenv((char *)envbuf);
4484 }
4485#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004486#ifdef FEAT_GETTEXT
4487 /*
4488 * When setting $VIMRUNTIME adjust the directory to find message
4489 * translations to $VIMRUNTIME/lang.
4490 */
4491 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4492 {
4493 char_u *buf = concat_str(val, (char_u *)"/lang");
4494
4495 if (buf != NULL)
4496 {
4497 bindtextdomain(VIMPACKAGE, (char *)buf);
4498 vim_free(buf);
4499 }
4500 }
4501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502}
4503
4504#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4505/*
4506 * Function given to ExpandGeneric() to obtain an environment variable name.
4507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004509get_env_name(
4510 expand_T *xp UNUSED,
4511 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512{
4513# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4514 /*
4515 * No environ[] on the Amiga and on the Mac (using MPW).
4516 */
4517 return NULL;
4518# else
4519# ifndef __WIN32__
4520 /* Borland C++ 5.2 has this in a header file. */
4521 extern char **environ;
4522# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004523# define ENVNAMELEN 100
4524 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 char_u *str;
4526 int n;
4527
4528 str = (char_u *)environ[idx];
4529 if (str == NULL)
4530 return NULL;
4531
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004532 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
4534 if (str[n] == '=' || str[n] == NUL)
4535 break;
4536 name[n] = str[n];
4537 }
4538 name[n] = NUL;
4539 return name;
4540# endif
4541}
Bram Moolenaar24305862012-08-15 14:05:05 +02004542
4543/*
4544 * Find all user names for user completion.
4545 * Done only once and then cached.
4546 */
4547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004548init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004549{
Bram Moolenaar24305862012-08-15 14:05:05 +02004550 static int lazy_init_done = FALSE;
4551
4552 if (lazy_init_done)
4553 return;
4554
4555 lazy_init_done = TRUE;
4556 ga_init2(&ga_users, sizeof(char_u *), 20);
4557
4558# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4559 {
4560 char_u* user;
4561 struct passwd* pw;
4562
4563 setpwent();
4564 while ((pw = getpwent()) != NULL)
4565 /* pw->pw_name shouldn't be NULL but just in case... */
4566 if (pw->pw_name != NULL)
4567 {
4568 if (ga_grow(&ga_users, 1) == FAIL)
4569 break;
4570 user = vim_strsave((char_u*)pw->pw_name);
4571 if (user == NULL)
4572 break;
4573 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4574 }
4575 endpwent();
4576 }
4577# endif
4578}
4579
4580/*
4581 * Function given to ExpandGeneric() to obtain an user names.
4582 */
4583 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004584get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004585{
4586 init_users();
4587 if (idx < ga_users.ga_len)
4588 return ((char_u **)ga_users.ga_data)[idx];
4589 return NULL;
4590}
4591
4592/*
4593 * Check whether name matches a user name. Return:
4594 * 0 if name does not match any user name.
4595 * 1 if name partially matches the beginning of a user name.
4596 * 2 is name fully matches a user name.
4597 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004598int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004599{
4600 int i;
4601 int n = (int)STRLEN(name);
4602 int result = 0;
4603
4604 init_users();
4605 for (i = 0; i < ga_users.ga_len; i++)
4606 {
4607 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4608 return 2; /* full match */
4609 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4610 result = 1; /* partial match */
4611 }
4612 return result;
4613}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#endif
4615
4616/*
4617 * Replace home directory by "~" in each space or comma separated file name in
4618 * 'src'.
4619 * If anything fails (except when out of space) dst equals src.
4620 */
4621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004622home_replace(
4623 buf_T *buf, /* when not NULL, check for help files */
4624 char_u *src, /* input file name */
4625 char_u *dst, /* where to put the result */
4626 int dstlen, /* maximum length of the result */
4627 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 spaces and commas in the file name. */
4629{
4630 size_t dirlen = 0, envlen = 0;
4631 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004632 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 char_u *p;
4634
4635 if (src == NULL)
4636 {
4637 *dst = NUL;
4638 return;
4639 }
4640
4641 /*
4642 * If the file is a help file, remove the path completely.
4643 */
4644 if (buf != NULL && buf->b_help)
4645 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004646 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 return;
4648 }
4649
4650 /*
4651 * We check both the value of the $HOME environment variable and the
4652 * "real" home directory.
4653 */
4654 if (homedir != NULL)
4655 dirlen = STRLEN(homedir);
4656
4657#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004658 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004660 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4661#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004662#ifdef WIN3264
4663 if (homedir_env == NULL)
4664 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4665#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004666 /* Empty is the same as not set. */
4667 if (homedir_env != NULL && *homedir_env == NUL)
4668 homedir_env = NULL;
4669
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004670#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004671 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004672 {
4673 int usedlen = 0;
4674 int flen;
4675 char_u *fbuf = NULL;
4676
4677 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004678 (void)modify_fname((char_u *)":p", &usedlen,
4679 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004680 flen = (int)STRLEN(homedir_env);
4681 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4682 /* Remove the trailing / that is added to a directory. */
4683 homedir_env[flen - 1] = NUL;
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 if (homedir_env != NULL)
4688 envlen = STRLEN(homedir_env);
4689
4690 if (!one)
4691 src = skipwhite(src);
4692 while (*src && dstlen > 0)
4693 {
4694 /*
4695 * Here we are at the beginning of a file name.
4696 * First, check to see if the beginning of the file name matches
4697 * $HOME or the "real" home directory. Check that there is a '/'
4698 * after the match (so that if e.g. the file is "/home/pieter/bla",
4699 * and the home directory is "/home/piet", the file does not end up
4700 * as "~er/bla" (which would seem to indicate the file "bla" in user
4701 * er's home directory)).
4702 */
4703 p = homedir;
4704 len = dirlen;
4705 for (;;)
4706 {
4707 if ( len
4708 && fnamencmp(src, p, len) == 0
4709 && (vim_ispathsep(src[len])
4710 || (!one && (src[len] == ',' || src[len] == ' '))
4711 || src[len] == NUL))
4712 {
4713 src += len;
4714 if (--dstlen > 0)
4715 *dst++ = '~';
4716
4717 /*
4718 * If it's just the home directory, add "/".
4719 */
4720 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4721 *dst++ = '/';
4722 break;
4723 }
4724 if (p == homedir_env)
4725 break;
4726 p = homedir_env;
4727 len = envlen;
4728 }
4729
4730 /* if (!one) skip to separator: space or comma */
4731 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4732 *dst++ = *src++;
4733 /* skip separator */
4734 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4735 *dst++ = *src++;
4736 }
4737 /* if (dstlen == 0) out of space, what to do??? */
4738
4739 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004740
4741 if (homedir_env != homedir_env_orig)
4742 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
4744
4745/*
4746 * Like home_replace, store the replaced string in allocated memory.
4747 * When something fails, NULL is returned.
4748 */
4749 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004750home_replace_save(
4751 buf_T *buf, /* when not NULL, check for help files */
4752 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753{
4754 char_u *dst;
4755 unsigned len;
4756
4757 len = 3; /* space for "~/" and trailing NUL */
4758 if (src != NULL) /* just in case */
4759 len += (unsigned)STRLEN(src);
4760 dst = alloc(len);
4761 if (dst != NULL)
4762 home_replace(buf, src, dst, len, TRUE);
4763 return dst;
4764}
4765
4766/*
4767 * Compare two file names and return:
4768 * FPC_SAME if they both exist and are the same file.
4769 * FPC_SAMEX if they both don't exist and have the same file name.
4770 * FPC_DIFF if they both exist and are different files.
4771 * FPC_NOTX if they both don't exist.
4772 * FPC_DIFFX if one of them doesn't exist.
4773 * For the first name environment variables are expanded
4774 */
4775 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004776fullpathcmp(
4777 char_u *s1,
4778 char_u *s2,
4779 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781#ifdef UNIX
4782 char_u exp1[MAXPATHL];
4783 char_u full1[MAXPATHL];
4784 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004785 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 int r1, r2;
4787
4788 expand_env(s1, exp1, MAXPATHL);
4789 r1 = mch_stat((char *)exp1, &st1);
4790 r2 = mch_stat((char *)s2, &st2);
4791 if (r1 != 0 && r2 != 0)
4792 {
4793 /* if mch_stat() doesn't work, may compare the names */
4794 if (checkname)
4795 {
4796 if (fnamecmp(exp1, s2) == 0)
4797 return FPC_SAMEX;
4798 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4799 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4800 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4801 return FPC_SAMEX;
4802 }
4803 return FPC_NOTX;
4804 }
4805 if (r1 != 0 || r2 != 0)
4806 return FPC_DIFFX;
4807 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4808 return FPC_SAME;
4809 return FPC_DIFF;
4810#else
4811 char_u *exp1; /* expanded s1 */
4812 char_u *full1; /* full path of s1 */
4813 char_u *full2; /* full path of s2 */
4814 int retval = FPC_DIFF;
4815 int r1, r2;
4816
4817 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4818 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4819 {
4820 full1 = exp1 + MAXPATHL;
4821 full2 = full1 + MAXPATHL;
4822
4823 expand_env(s1, exp1, MAXPATHL);
4824 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4825 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4826
4827 /* If vim_FullName() fails, the file probably doesn't exist. */
4828 if (r1 != OK && r2 != OK)
4829 {
4830 if (checkname && fnamecmp(exp1, s2) == 0)
4831 retval = FPC_SAMEX;
4832 else
4833 retval = FPC_NOTX;
4834 }
4835 else if (r1 != OK || r2 != OK)
4836 retval = FPC_DIFFX;
4837 else if (fnamecmp(full1, full2))
4838 retval = FPC_DIFF;
4839 else
4840 retval = FPC_SAME;
4841 vim_free(exp1);
4842 }
4843 return retval;
4844#endif
4845}
4846
4847/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004848 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004849 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004850 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 */
4852 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004853gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 char_u *p1, *p2;
4856
4857 if (fname == NULL)
4858 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004859 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004861 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004863 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
4865 return p1;
4866}
4867
Bram Moolenaar31710262010-08-13 13:36:15 +02004868#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004869static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004870
4871/*
4872 * Return the end of the directory name, on the first path
4873 * separator:
4874 * "/path/file", "/path/dir/", "/path//dir", "/file"
4875 * ^ ^ ^ ^
4876 */
4877 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004878gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004879{
4880 char_u *dir_end = fname;
4881 char_u *next_dir_end = fname;
4882 int look_for_sep = TRUE;
4883 char_u *p;
4884
4885 for (p = fname; *p != NUL; )
4886 {
4887 if (vim_ispathsep(*p))
4888 {
4889 if (look_for_sep)
4890 {
4891 next_dir_end = p;
4892 look_for_sep = FALSE;
4893 }
4894 }
4895 else
4896 {
4897 if (!look_for_sep)
4898 dir_end = next_dir_end;
4899 look_for_sep = TRUE;
4900 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004901 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004902 }
4903 return dir_end;
4904}
4905#endif
4906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004908 * Get pointer to tail of "fname", including path separators. Putting a NUL
4909 * here leaves the directory name. Takes care of "c:/" and "//".
4910 * Always returns a valid pointer.
4911 */
4912 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004913gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004914{
4915 char_u *p;
4916 char_u *t;
4917
4918 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4919 t = gettail(fname);
4920 while (t > p && after_pathsep(fname, t))
4921 --t;
4922#ifdef VMS
4923 /* path separator is part of the path */
4924 ++t;
4925#endif
4926 return t;
4927}
4928
4929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 * get the next path component (just after the next path separator).
4931 */
4932 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004933getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
4935 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004936 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (*fname)
4938 ++fname;
4939 return fname;
4940}
4941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942/*
4943 * Get a pointer to one character past the head of a path name.
4944 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4945 * If there is no head, path is returned.
4946 */
4947 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004948get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
4950 char_u *retval;
4951
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004952#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 /* may skip "c:" */
4954 if (isalpha(path[0]) && path[1] == ':')
4955 retval = path + 2;
4956 else
4957 retval = path;
4958#else
4959# if defined(AMIGA)
4960 /* may skip "label:" */
4961 retval = vim_strchr(path, ':');
4962 if (retval == NULL)
4963 retval = path;
4964# else /* Unix */
4965 retval = path;
4966# endif
4967#endif
4968
4969 while (vim_ispathsep(*retval))
4970 ++retval;
4971
4972 return retval;
4973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004976 * Return TRUE if 'c' is a path separator.
4977 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 */
4979 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004980vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004982#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004984#else
4985# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004987# else
4988# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4990 return (c == ':' || c == '[' || c == ']' || c == '/'
4991 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004992# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004994# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997}
4998
Bram Moolenaar69c35002013-11-04 02:54:12 +01004999/*
5000 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5001 */
5002 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005003vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005004{
5005 return vim_ispathsep(c)
5006#ifdef BACKSLASH_IN_FILENAME
5007 && c != ':'
5008#endif
5009 ;
5010}
5011
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5013/*
5014 * return TRUE if 'c' is a path list separator.
5015 */
5016 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005017vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019#ifdef UNIX
5020 return (c == ':');
5021#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005022 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#endif
5024}
5025#endif
5026
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005027#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5028 || defined(FEAT_EVAL) || defined(PROTO)
5029/*
5030 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5031 * It's done in-place.
5032 */
5033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005034shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005035{
5036 char_u *tail, *s, *d;
5037 int skip = FALSE;
5038
5039 tail = gettail(str);
5040 d = str;
5041 for (s = str; ; ++s)
5042 {
5043 if (s >= tail) /* copy the whole tail */
5044 {
5045 *d++ = *s;
5046 if (*s == NUL)
5047 break;
5048 }
5049 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5050 {
5051 *d++ = *s;
5052 skip = FALSE;
5053 }
5054 else if (!skip)
5055 {
5056 *d++ = *s; /* copy next char */
5057 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5058 skip = TRUE;
5059# ifdef FEAT_MBYTE
5060 if (has_mbyte)
5061 {
5062 int l = mb_ptr2len(s);
5063
5064 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005065 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005066 }
5067# endif
5068 }
5069 }
5070}
5071#endif
5072
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005073/*
5074 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5075 * Also returns TRUE if there is no directory name.
5076 * "fname" must be writable!.
5077 */
5078 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005079dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005080{
5081 char_u *p;
5082 int c;
5083 int retval;
5084
5085 p = gettail_sep(fname);
5086 if (p == fname)
5087 return TRUE;
5088 c = *p;
5089 *p = NUL;
5090 retval = mch_isdir(fname);
5091 *p = c;
5092 return retval;
5093}
5094
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005096 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5097 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
5099 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005100vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005102#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005104#else
5105 if (p_fic)
5106 return MB_STRICMP(x, y);
5107 return STRCMP(x, y);
5108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109}
5110
5111 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005112vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005114#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005115 char_u *px = x;
5116 char_u *py = y;
5117 int cx = NUL;
5118 int cy = NUL;
5119
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005120 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005122 cx = PTR2CHAR(px);
5123 cy = PTR2CHAR(py);
5124 if (cx == NUL || cy == NUL
5125 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5126 && !(cx == '/' && cy == '\\')
5127 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005129 len -= MB_PTR2LEN(px);
5130 px += MB_PTR2LEN(px);
5131 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 if (len == 0)
5134 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005135 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005136#else
5137 if (p_fic)
5138 return MB_STRNICMP(x, y, len);
5139 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143/*
5144 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005145 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 */
5147 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005148concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149{
5150 char_u *dest;
5151
5152 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5153 if (dest != NULL)
5154 {
5155 STRCPY(dest, fname1);
5156 if (sep)
5157 add_pathsep(dest);
5158 STRCAT(dest, fname2);
5159 }
5160 return dest;
5161}
5162
Bram Moolenaard6754642005-01-17 22:18:45 +00005163/*
5164 * Concatenate two strings and return the result in allocated memory.
5165 * Returns NULL when out of memory.
5166 */
5167 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005168concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005169{
5170 char_u *dest;
5171 size_t l = STRLEN(str1);
5172
5173 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5174 if (dest != NULL)
5175 {
5176 STRCPY(dest, str1);
5177 STRCPY(dest + l, str2);
5178 }
5179 return dest;
5180}
Bram Moolenaard6754642005-01-17 22:18:45 +00005181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182/*
5183 * Add a path separator to a file name, unless it already ends in a path
5184 * separator.
5185 */
5186 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005187add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005189 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 STRCAT(p, PATHSEPSTR);
5191}
5192
5193/*
5194 * FullName_save - Make an allocated copy of a full file name.
5195 * Returns NULL when out of memory.
5196 */
5197 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005198FullName_save(
5199 char_u *fname,
5200 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005201 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203 char_u *buf;
5204 char_u *new_fname = NULL;
5205
5206 if (fname == NULL)
5207 return NULL;
5208
5209 buf = alloc((unsigned)MAXPATHL);
5210 if (buf != NULL)
5211 {
5212 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5213 new_fname = vim_strsave(buf);
5214 else
5215 new_fname = vim_strsave(fname);
5216 vim_free(buf);
5217 }
5218 return new_fname;
5219}
5220
5221#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5222
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005223static char_u *skip_string(char_u *p);
5224static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005225static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005226static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
5228/*
5229 * Find the start of a comment, not knowing if we are in a comment right now.
5230 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005231 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005233 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005234ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005235{
5236 return find_start_comment(curbuf->b_ind_maxcomment);
5237}
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
5242 pos_T *pos;
5243 char_u *line;
5244 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005245 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005247 for (;;)
5248 {
5249 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5250 if (pos == NULL)
5251 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005253 /*
5254 * Check if the comment start we found is inside a string.
5255 * If it is then restrict the search to below this line and try again.
5256 */
5257 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005258 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005259 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005260 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005261 break;
5262 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5263 if (cur_maxcomment <= 0)
5264 {
5265 pos = NULL;
5266 break;
5267 }
5268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return pos;
5270}
5271
5272/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005273 * Find the start of a comment or raw string, not knowing if we are in a
5274 * comment or raw string right now.
5275 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005276 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005277 * Return NULL when not inside a comment or raw string.
5278 * "CORS" -> Comment Or Raw String
5279 */
5280 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005281ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005282{
Bram Moolenaar089af182015-10-07 11:41:49 +02005283 static pos_T comment_pos_copy;
5284 pos_T *comment_pos;
5285 pos_T *rs_pos;
5286
5287 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5288 if (comment_pos != NULL)
5289 {
5290 /* Need to make a copy of the static pos in findmatchlimit(),
5291 * calling find_start_rawstring() may change it. */
5292 comment_pos_copy = *comment_pos;
5293 comment_pos = &comment_pos_copy;
5294 }
5295 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005296
5297 /* If comment_pos is before rs_pos the raw string is inside the comment.
5298 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005299 if (comment_pos == NULL || (rs_pos != NULL
5300 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005301 {
5302 if (is_raw != NULL && rs_pos != NULL)
5303 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005304 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005305 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005306 return comment_pos;
5307}
5308
5309/*
5310 * Find the start of a raw string, not knowing if we are in one right now.
5311 * Search starts at w_cursor.lnum and goes backwards.
5312 * Return NULL when not inside a raw string.
5313 */
5314 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005315find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005316{
5317 pos_T *pos;
5318 char_u *line;
5319 char_u *p;
5320 int cur_maxcomment = ind_maxcomment;
5321
5322 for (;;)
5323 {
5324 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5325 if (pos == NULL)
5326 break;
5327
5328 /*
5329 * Check if the raw string start we found is inside a string.
5330 * If it is then restrict the search to below this line and try again.
5331 */
5332 line = ml_get(pos->lnum);
5333 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5334 p = skip_string(p);
5335 if ((colnr_T)(p - line) <= pos->col)
5336 break;
5337 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5338 if (cur_maxcomment <= 0)
5339 {
5340 pos = NULL;
5341 break;
5342 }
5343 }
5344 return pos;
5345}
5346
5347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 * Skip to the end of a "string" and a 'c' character.
5349 * If there is no string or character, return argument unmodified.
5350 */
5351 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 int i;
5355
5356 /*
5357 * We loop, because strings may be concatenated: "date""time".
5358 */
5359 for ( ; ; ++p)
5360 {
5361 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5362 {
5363 if (!p[1]) /* ' at end of line */
5364 break;
5365 i = 2;
5366 if (p[1] == '\\') /* '\n' or '\000' */
5367 {
5368 ++i;
5369 while (vim_isdigit(p[i - 1])) /* '\000' */
5370 ++i;
5371 }
5372 if (p[i] == '\'') /* check for trailing ' */
5373 {
5374 p += i;
5375 continue;
5376 }
5377 }
5378 else if (p[0] == '"') /* start of string */
5379 {
5380 for (++p; p[0]; ++p)
5381 {
5382 if (p[0] == '\\' && p[1] != NUL)
5383 ++p;
5384 else if (p[0] == '"') /* end of string */
5385 break;
5386 }
5387 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005388 continue; /* continue for another string */
5389 }
5390 else if (p[0] == 'R' && p[1] == '"')
5391 {
5392 /* Raw string: R"[delim](...)[delim]" */
5393 char_u *delim = p + 2;
5394 char_u *paren = vim_strchr(delim, '(');
5395
5396 if (paren != NULL)
5397 {
5398 size_t delim_len = paren - delim;
5399
5400 for (p += 3; *p; ++p)
5401 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5402 && p[delim_len + 1] == '"')
5403 {
5404 p += delim_len + 1;
5405 break;
5406 }
5407 if (p[0] == '"')
5408 continue; /* continue for another string */
5409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
5411 break; /* no string found */
5412 }
5413 if (!*p)
5414 --p; /* backup from NUL */
5415 return p;
5416}
5417#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5418
5419#if defined(FEAT_CINDENT) || defined(PROTO)
5420
5421/*
5422 * Do C or expression indenting on the current line.
5423 */
5424 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005425do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
5427# ifdef FEAT_EVAL
5428 if (*curbuf->b_p_inde != NUL)
5429 fixthisline(get_expr_indent);
5430 else
5431# endif
5432 fixthisline(get_c_indent);
5433}
5434
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005435/* Find result cache for cpp_baseclass */
5436typedef struct {
5437 int found;
5438 lpos_T lpos;
5439} cpp_baseclass_cache_T;
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441/*
5442 * Functions for C-indenting.
5443 * Most of this originally comes from Eric Fischer.
5444 */
5445/*
5446 * Below "XXX" means that this function may unlock the current line.
5447 */
5448
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005449static char_u *cin_skipcomment(char_u *);
5450static int cin_nocode(char_u *);
5451static pos_T *find_line_comment(void);
5452static int cin_has_js_key(char_u *text);
5453static int cin_islabel_skip(char_u **);
5454static int cin_isdefault(char_u *);
5455static char_u *after_label(char_u *l);
5456static int get_indent_nolabel(linenr_T lnum);
5457static int skip_label(linenr_T, char_u **pp);
5458static int cin_first_id_amount(void);
5459static int cin_get_equal_amount(linenr_T lnum);
5460static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005461static int cin_iscomment(char_u *);
5462static int cin_islinecomment(char_u *);
5463static int cin_isterminated(char_u *, int, int);
5464static int cin_isinit(void);
5465static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5466static int cin_isif(char_u *);
5467static int cin_iselse(char_u *);
5468static int cin_isdo(char_u *);
5469static int cin_iswhileofdo(char_u *, linenr_T);
5470static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5471static int cin_iswhileofdo_end(int terminated);
5472static int cin_isbreak(char_u *);
5473static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5474static int get_baseclass_amount(int col);
5475static int cin_ends_in(char_u *, char_u *, char_u *);
5476static int cin_starts_with(char_u *s, char *word);
5477static int cin_skip2pos(pos_T *trypos);
5478static pos_T *find_start_brace(void);
5479static pos_T *find_match_paren(int);
5480static pos_T *find_match_char(int c, int ind_maxparen);
5481static int corr_ind_maxparen(pos_T *startpos);
5482static int find_last_paren(char_u *l, int start, int end);
5483static int find_match(int lookfor, linenr_T ourscope);
5484static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486/*
5487 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005488 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 */
5490 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005491cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 while (*s)
5494 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005495 char_u *prev_s = s;
5496
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005498
5499 /* Perl/shell # comment comment continues until eol. Require a space
5500 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005501 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005502 {
5503 s += STRLEN(s);
5504 break;
5505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 if (*s != '/')
5507 break;
5508 ++s;
5509 if (*s == '/') /* slash-slash comment continues till eol */
5510 {
5511 s += STRLEN(s);
5512 break;
5513 }
5514 if (*s != '*')
5515 break;
5516 for (++s; *s; ++s) /* skip slash-star comment */
5517 if (s[0] == '*' && s[1] == '/')
5518 {
5519 s += 2;
5520 break;
5521 }
5522 }
5523 return s;
5524}
5525
5526/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005527 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 * not considered code.
5529 */
5530 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005531cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 return *cin_skipcomment(s) == NUL;
5534}
5535
5536/*
5537 * Check previous lines for a "//" line comment, skipping over blank lines.
5538 */
5539 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005540find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 static pos_T pos;
5543 char_u *line;
5544 char_u *p;
5545
5546 pos = curwin->w_cursor;
5547 while (--pos.lnum > 0)
5548 {
5549 line = ml_get(pos.lnum);
5550 p = skipwhite(line);
5551 if (cin_islinecomment(p))
5552 {
5553 pos.col = (int)(p - line);
5554 return &pos;
5555 }
5556 if (*p != NUL)
5557 break;
5558 }
5559 return NULL;
5560}
5561
5562/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005563 * Return TRUE if "text" starts with "key:".
5564 */
5565 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005566cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005567{
5568 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005569 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005570
5571 if (*s == '\'' || *s == '"')
5572 {
5573 /* can be 'key': or "key": */
5574 quote = *s;
5575 ++s;
5576 }
5577 if (!vim_isIDc(*s)) /* need at least one ID character */
5578 return FALSE;
5579
5580 while (vim_isIDc(*s))
5581 ++s;
5582 if (*s == quote)
5583 ++s;
5584
5585 s = cin_skipcomment(s);
5586
5587 /* "::" is not a label, it's C++ */
5588 return (*s == ':' && s[1] != ':');
5589}
5590
5591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005593 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 */
5595 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005596cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 if (!vim_isIDc(**s)) /* need at least one ID character */
5599 return FALSE;
5600
5601 while (vim_isIDc(**s))
5602 (*s)++;
5603
5604 *s = cin_skipcomment(*s);
5605
5606 /* "::" is not a label, it's C++ */
5607 return (**s == ':' && *++*s != ':');
5608}
5609
5610/*
5611 * Recognize a label: "label:".
5612 * Note: curwin->w_cursor must be where we are looking for the label.
5613 */
5614 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005615cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 char_u *s;
5618
5619 s = cin_skipcomment(ml_get_curline());
5620
5621 /*
5622 * Exclude "default" from labels, since it should be indented
5623 * like a switch label. Same for C++ scope declarations.
5624 */
5625 if (cin_isdefault(s))
5626 return FALSE;
5627 if (cin_isscopedecl(s))
5628 return FALSE;
5629
5630 if (cin_islabel_skip(&s))
5631 {
5632 /*
5633 * Only accept a label if the previous line is terminated or is a case
5634 * label.
5635 */
5636 pos_T cursor_save;
5637 pos_T *trypos;
5638 char_u *line;
5639
5640 cursor_save = curwin->w_cursor;
5641 while (curwin->w_cursor.lnum > 1)
5642 {
5643 --curwin->w_cursor.lnum;
5644
5645 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005646 * If we're in a comment or raw string now, skip to the start of
5647 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 */
5649 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005650 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 curwin->w_cursor = *trypos;
5652
5653 line = ml_get_curline();
5654 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5655 continue;
5656 if (*(line = cin_skipcomment(line)) == NUL)
5657 continue;
5658
5659 curwin->w_cursor = cursor_save;
5660 if (cin_isterminated(line, TRUE, FALSE)
5661 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005662 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 || (cin_islabel_skip(&line) && cin_nocode(line)))
5664 return TRUE;
5665 return FALSE;
5666 }
5667 curwin->w_cursor = cursor_save;
5668 return TRUE; /* label at start of file??? */
5669 }
5670 return FALSE;
5671}
5672
5673/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005674 * Recognize structure initialization and enumerations:
5675 * "[typedef] [static|public|protected|private] enum"
5676 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 */
5678 static int
5679cin_isinit(void)
5680{
5681 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005682 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684 s = cin_skipcomment(ml_get_curline());
5685
Bram Moolenaar75342212013-03-07 13:13:52 +01005686 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 s = cin_skipcomment(s + 7);
5688
Bram Moolenaar75342212013-03-07 13:13:52 +01005689 for (;;)
5690 {
5691 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005692
Bram Moolenaar75342212013-03-07 13:13:52 +01005693 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5694 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005695 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005696 if (cin_starts_with(s, skip[i]))
5697 {
5698 s = cin_skipcomment(s + l);
5699 l = 0;
5700 break;
5701 }
5702 }
5703 if (l != 0)
5704 break;
5705 }
5706
5707 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 return TRUE;
5709
5710 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5711 return TRUE;
5712
5713 return FALSE;
5714}
5715
5716/*
5717 * Recognize a switch label: "case .*:" or "default:".
5718 */
5719 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005720cin_iscase(
5721 char_u *s,
5722 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005725 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 {
5727 for (s += 4; *s; ++s)
5728 {
5729 s = cin_skipcomment(s);
5730 if (*s == ':')
5731 {
5732 if (s[1] == ':') /* skip over "::" for C++ */
5733 ++s;
5734 else
5735 return TRUE;
5736 }
5737 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005738 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5740 return FALSE; /* stop at comment */
5741 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005742 {
5743 /* JS etc. */
5744 if (strict)
5745 return FALSE; /* stop at string */
5746 else
5747 return TRUE;
5748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
5750 return FALSE;
5751 }
5752
5753 if (cin_isdefault(s))
5754 return TRUE;
5755 return FALSE;
5756}
5757
5758/*
5759 * Recognize a "default" switch label.
5760 */
5761 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005762cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 return (STRNCMP(s, "default", 7) == 0
5765 && *(s = cin_skipcomment(s + 7)) == ':'
5766 && s[1] != ':');
5767}
5768
5769/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005770 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 */
5772 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005773cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 int i;
5776
5777 s = cin_skipcomment(s);
5778 if (STRNCMP(s, "public", 6) == 0)
5779 i = 6;
5780 else if (STRNCMP(s, "protected", 9) == 0)
5781 i = 9;
5782 else if (STRNCMP(s, "private", 7) == 0)
5783 i = 7;
5784 else
5785 return FALSE;
5786 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5787}
5788
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005789/* Maximum number of lines to search back for a "namespace" line. */
5790#define FIND_NAMESPACE_LIM 20
5791
5792/*
5793 * Recognize a "namespace" scope declaration.
5794 */
5795 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005796cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005797{
5798 char_u *p;
5799 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005800 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005801
5802 s = cin_skipcomment(s);
5803 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5804 {
5805 p = cin_skipcomment(skipwhite(s + 9));
5806 while (*p != NUL)
5807 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005808 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005809 {
5810 has_name = TRUE; /* found end of a name */
5811 p = cin_skipcomment(skipwhite(p));
5812 }
5813 else if (*p == '{')
5814 {
5815 break;
5816 }
5817 else if (vim_iswordc(*p))
5818 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005819 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005820 if (has_name)
5821 return FALSE; /* word character after skipping past name */
5822 ++p;
5823 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005824 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5825 {
5826 if (!has_name_start || has_name)
5827 return FALSE;
5828 /* C++ 17 nested namespace */
5829 p += 3;
5830 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005831 else
5832 {
5833 return FALSE;
5834 }
5835 }
5836 return TRUE;
5837 }
5838 return FALSE;
5839}
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005842 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5843 */
5844 static int
5845cin_is_cpp_extern_c(char_u *s)
5846{
5847 char_u *p;
5848 int has_string_literal = FALSE;
5849
5850 s = cin_skipcomment(s);
5851 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5852 {
5853 p = cin_skipcomment(skipwhite(s + 6));
5854 while (*p != NUL)
5855 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005856 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005857 {
5858 p = cin_skipcomment(skipwhite(p));
5859 }
5860 else if (*p == '{')
5861 {
5862 break;
5863 }
5864 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5865 {
5866 if (has_string_literal)
5867 return FALSE;
5868 has_string_literal = TRUE;
5869 p += 3;
5870 }
5871 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5872 && p[4] == '"')
5873 {
5874 if (has_string_literal)
5875 return FALSE;
5876 has_string_literal = TRUE;
5877 p += 5;
5878 }
5879 else
5880 {
5881 return FALSE;
5882 }
5883 }
5884 return has_string_literal ? TRUE : FALSE;
5885 }
5886 return FALSE;
5887}
5888
5889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 * Return a pointer to the first non-empty non-comment character after a ':'.
5891 * Return NULL if not found.
5892 * case 234: a = b;
5893 * ^
5894 */
5895 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005896after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 for ( ; *l; ++l)
5899 {
5900 if (*l == ':')
5901 {
5902 if (l[1] == ':') /* skip over "::" for C++ */
5903 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005904 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 break;
5906 }
5907 else if (*l == '\'' && l[1] && l[2] == '\'')
5908 l += 2; /* skip over 'x' */
5909 }
5910 if (*l == NUL)
5911 return NULL;
5912 l = cin_skipcomment(l + 1);
5913 if (*l == NUL)
5914 return NULL;
5915 return l;
5916}
5917
5918/*
5919 * Get indent of line "lnum", skipping a label.
5920 * Return 0 if there is nothing after the label.
5921 */
5922 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005923get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 char_u *l;
5926 pos_T fp;
5927 colnr_T col;
5928 char_u *p;
5929
5930 l = ml_get(lnum);
5931 p = after_label(l);
5932 if (p == NULL)
5933 return 0;
5934
5935 fp.col = (colnr_T)(p - l);
5936 fp.lnum = lnum;
5937 getvcol(curwin, &fp, &col, NULL, NULL);
5938 return (int)col;
5939}
5940
5941/*
5942 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005943 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 * label: if (asdf && asdfasdf)
5945 * ^
5946 */
5947 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005948skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 char_u *l;
5951 int amount;
5952 pos_T cursor_save;
5953
5954 cursor_save = curwin->w_cursor;
5955 curwin->w_cursor.lnum = lnum;
5956 l = ml_get_curline();
5957 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005958 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {
5960 amount = get_indent_nolabel(lnum);
5961 l = after_label(ml_get_curline());
5962 if (l == NULL) /* just in case */
5963 l = ml_get_curline();
5964 }
5965 else
5966 {
5967 amount = get_indent();
5968 l = ml_get_curline();
5969 }
5970 *pp = l;
5971
5972 curwin->w_cursor = cursor_save;
5973 return amount;
5974}
5975
5976/*
5977 * Return the indent of the first variable name after a type in a declaration.
5978 * int a, indent of "a"
5979 * static struct foo b, indent of "b"
5980 * enum bla c, indent of "c"
5981 * Returns zero when it doesn't look like a declaration.
5982 */
5983 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005984cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 char_u *line, *p, *s;
5987 int len;
5988 pos_T fp;
5989 colnr_T col;
5990
5991 line = ml_get_curline();
5992 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005993 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5995 {
5996 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005997 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
5999 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6000 p = skipwhite(p + 6);
6001 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6002 p = skipwhite(p + 4);
6003 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6004 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6005 {
6006 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006007 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6008 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6009 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6010 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 p = s;
6012 }
6013 for (len = 0; vim_isIDc(p[len]); ++len)
6014 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006015 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 return 0;
6017
6018 p = skipwhite(p + len);
6019 fp.lnum = curwin->w_cursor.lnum;
6020 fp.col = (colnr_T)(p - line);
6021 getvcol(curwin, &fp, &col, NULL, NULL);
6022 return (int)col;
6023}
6024
6025/*
6026 * Return the indent of the first non-blank after an equal sign.
6027 * char *foo = "here";
6028 * Return zero if no (useful) equal sign found.
6029 * Return -1 if the line above "lnum" ends in a backslash.
6030 * foo = "asdf\
6031 * asdf\
6032 * here";
6033 */
6034 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006035cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036{
6037 char_u *line;
6038 char_u *s;
6039 colnr_T col;
6040 pos_T fp;
6041
6042 if (lnum > 1)
6043 {
6044 line = ml_get(lnum - 1);
6045 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6046 return -1;
6047 }
6048
6049 line = s = ml_get(lnum);
6050 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6051 {
6052 if (cin_iscomment(s)) /* ignore comments */
6053 s = cin_skipcomment(s);
6054 else
6055 ++s;
6056 }
6057 if (*s != '=')
6058 return 0;
6059
6060 s = skipwhite(s + 1);
6061 if (cin_nocode(s))
6062 return 0;
6063
6064 if (*s == '"') /* nice alignment for continued strings */
6065 ++s;
6066
6067 fp.lnum = lnum;
6068 fp.col = (colnr_T)(s - line);
6069 getvcol(curwin, &fp, &col, NULL, NULL);
6070 return (int)col;
6071}
6072
6073/*
6074 * Recognize a preprocessor statement: Any line that starts with '#'.
6075 */
6076 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006077cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006079 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 return TRUE;
6081 return FALSE;
6082}
6083
6084/*
6085 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6086 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6087 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006088 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 */
6090 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006091cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
6093 char_u *line = *pp;
6094 linenr_T lnum = *lnump;
6095 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006096 int candidate_amount = *amount;
6097
6098 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6099 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006101 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {
6103 if (cin_ispreproc(line))
6104 {
6105 retval = TRUE;
6106 *lnump = lnum;
6107 break;
6108 }
6109 if (lnum == 1)
6110 break;
6111 line = ml_get(--lnum);
6112 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6113 break;
6114 }
6115
6116 if (lnum != *lnump)
6117 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006118 if (retval)
6119 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 return retval;
6121}
6122
6123/*
6124 * Recognize the start of a C or C++ comment.
6125 */
6126 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006127cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6130}
6131
6132/*
6133 * Recognize the start of a "//" comment.
6134 */
6135 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006136cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 return (p[0] == '/' && p[1] == '/');
6139}
6140
6141/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006142 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6143 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006145 * If a line begins with an "else", only consider it terminated if no unmatched
6146 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 * Return the character terminating the line (ending char's have precedence if
6148 * both apply in order to determine initializations).
6149 */
6150 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006151cin_isterminated(
6152 char_u *s,
6153 int incl_open, /* include '{' at the end as terminator */
6154 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006156 char_u found_start = 0;
6157 unsigned n_open = 0;
6158 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 s = cin_skipcomment(s);
6161
6162 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6163 found_start = *s;
6164
Bram Moolenaar496f9512011-05-19 16:35:09 +02006165 if (!found_start)
6166 is_else = cin_iselse(s);
6167
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 while (*s)
6169 {
6170 /* skip over comments, "" strings and 'c'haracters */
6171 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006172 if (*s == '}' && n_open > 0)
6173 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006174 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006175 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 && cin_nocode(s + 1))
6177 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006178 else if (*s == '{')
6179 {
6180 if (incl_open && cin_nocode(s + 1))
6181 return *s;
6182 else
6183 ++n_open;
6184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 if (*s)
6187 s++;
6188 }
6189 return found_start;
6190}
6191
6192/*
6193 * Recognize the basic picture of a function declaration -- it needs to
6194 * have an open paren somewhere and a close paren at the end of the line and
6195 * no semicolons anywhere.
6196 * When a line ends in a comma we continue looking in the next line.
6197 * "sp" points to a string with the line. When looking at other lines it must
6198 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006199 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006200 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 */
6202 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006203cin_isfuncdecl(
6204 char_u **sp,
6205 linenr_T first_lnum,
6206 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 char_u *s;
6209 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006210 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006212 pos_T *trypos;
6213 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
6215 if (sp == NULL)
6216 s = ml_get(lnum);
6217 else
6218 s = *sp;
6219
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006220 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006221 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006222 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006223 {
6224 lnum = trypos->lnum;
6225 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006226 {
6227 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006228 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006229 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006230
6231 s = ml_get(lnum);
6232 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006233 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006234
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006235 /* Ignore line starting with #. */
6236 if (cin_ispreproc(s))
6237 return FALSE;
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6240 {
6241 if (cin_iscomment(s)) /* ignore comments */
6242 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006243 else if (*s == ':')
6244 {
6245 if (*(s + 1) == ':')
6246 s += 2;
6247 else
6248 /* To avoid a mistake in the following situation:
6249 * A::A(int a, int b)
6250 * : a(0) // <--not a function decl
6251 * , b(0)
6252 * {...
6253 */
6254 return FALSE;
6255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 else
6257 ++s;
6258 }
6259 if (*s != '(')
6260 return FALSE; /* ';', ' or " before any () or no '(' */
6261
6262 while (*s && *s != ';' && *s != '\'' && *s != '"')
6263 {
6264 if (*s == ')' && cin_nocode(s + 1))
6265 {
6266 /* ')' at the end: may have found a match
6267 * Check for he previous line not to end in a backslash:
6268 * #if defined(x) && \
6269 * defined(y)
6270 */
6271 lnum = first_lnum - 1;
6272 s = ml_get(lnum);
6273 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6274 retval = TRUE;
6275 goto done;
6276 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006277 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006279 int comma = (*s == ',');
6280
6281 /* ',' at the end: continue looking in the next line.
6282 * At the end: check for ',' in the next line, for this style:
6283 * func(arg1
6284 * , arg2) */
6285 for (;;)
6286 {
6287 if (lnum >= curbuf->b_ml.ml_line_count)
6288 break;
6289 s = ml_get(++lnum);
6290 if (!cin_ispreproc(s))
6291 break;
6292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (lnum >= curbuf->b_ml.ml_line_count)
6294 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006295 /* Require a comma at end of the line or a comma or ')' at the
6296 * start of next line. */
6297 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006298 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006299 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006300 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 }
6302 else if (cin_iscomment(s)) /* ignore comments */
6303 s = cin_skipcomment(s);
6304 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006307 just_started = FALSE;
6308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 }
6310
6311done:
6312 if (lnum != first_lnum && sp != NULL)
6313 *sp = ml_get(first_lnum);
6314
6315 return retval;
6316}
6317
6318 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006319cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006321 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322}
6323
6324 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006325cin_iselse(
6326 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 if (*p == '}') /* accept "} else" */
6329 p = cin_skipcomment(p + 1);
6330 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6331}
6332
6333 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006334cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
6336 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6337}
6338
6339/*
6340 * Check if this is a "while" that should have a matching "do".
6341 * We only accept a "while (condition) ;", with only white space between the
6342 * ')' and ';'. The condition may be spread over several lines.
6343 */
6344 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006345cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 pos_T cursor_save;
6348 pos_T *trypos;
6349 int retval = FALSE;
6350
6351 p = cin_skipcomment(p);
6352 if (*p == '}') /* accept "} while (cond);" */
6353 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006354 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 {
6356 cursor_save = curwin->w_cursor;
6357 curwin->w_cursor.lnum = lnum;
6358 curwin->w_cursor.col = 0;
6359 p = ml_get_curline();
6360 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6361 {
6362 ++p;
6363 ++curwin->w_cursor.col;
6364 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006365 if ((trypos = findmatchlimit(NULL, 0, 0,
6366 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6368 retval = TRUE;
6369 curwin->w_cursor = cursor_save;
6370 }
6371 return retval;
6372}
6373
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006374/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006375 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006376 * Return 0 if there is none.
6377 * Otherwise return !0 and update "*poffset" to point to the place where the
6378 * string was found.
6379 */
6380 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006381cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006382{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006383 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006384
6385 if (offset-- < 2)
6386 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006387 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006388 --offset;
6389
6390 offset -= 1;
6391 if (!STRNCMP(line + offset, "if", 2))
6392 goto probablyFound;
6393
6394 if (offset >= 1)
6395 {
6396 offset -= 1;
6397 if (!STRNCMP(line + offset, "for", 3))
6398 goto probablyFound;
6399
6400 if (offset >= 2)
6401 {
6402 offset -= 2;
6403 if (!STRNCMP(line + offset, "while", 5))
6404 goto probablyFound;
6405 }
6406 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006407 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006408
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006409probablyFound:
6410 if (!offset || !vim_isIDc(line[offset - 1]))
6411 {
6412 *poffset = offset;
6413 return 1;
6414 }
6415 return 0;
6416}
6417
6418/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006419 * Return TRUE if we are at the end of a do-while.
6420 * do
6421 * nothing;
6422 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006423 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006424 * Adjust the cursor to the line with "while".
6425 */
6426 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006427cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006428{
6429 char_u *line;
6430 char_u *p;
6431 char_u *s;
6432 pos_T *trypos;
6433 int i;
6434
6435 if (terminated != ';') /* there must be a ';' at the end */
6436 return FALSE;
6437
6438 p = line = ml_get_curline();
6439 while (*p != NUL)
6440 {
6441 p = cin_skipcomment(p);
6442 if (*p == ')')
6443 {
6444 s = skipwhite(p + 1);
6445 if (*s == ';' && cin_nocode(s + 1))
6446 {
6447 /* Found ");" at end of the line, now check there is "while"
6448 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006449 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006450 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006451 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006452 if (trypos != NULL)
6453 {
6454 s = cin_skipcomment(ml_get(trypos->lnum));
6455 if (*s == '}') /* accept "} while (cond);" */
6456 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006457 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006458 {
6459 curwin->w_cursor.lnum = trypos->lnum;
6460 return TRUE;
6461 }
6462 }
6463
6464 /* Searching may have made "line" invalid, get it again. */
6465 line = ml_get_curline();
6466 p = line + i;
6467 }
6468 }
6469 if (*p != NUL)
6470 ++p;
6471 }
6472 return FALSE;
6473}
6474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006476cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6479}
6480
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006481/*
6482 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 * constructor-initialization. eg:
6484 *
6485 * class MyClass :
6486 * baseClass <-- here
6487 * class MyClass : public baseClass,
6488 * anotherBaseClass <-- here (should probably lineup ??)
6489 * MyClass::MyClass(...) :
6490 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006491 *
6492 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 */
6494 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006495cin_is_cpp_baseclass(
6496 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006498 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 char_u *s;
6500 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006501 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006502 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006504 if (pos->lnum <= lnum)
6505 return cached->found; /* Use the cached result */
6506
6507 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006509 s = skipwhite(line);
6510 if (*s == '#') /* skip #define FOO x ? (x) : x */
6511 return FALSE;
6512 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 if (*s == NUL)
6514 return FALSE;
6515
6516 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6517
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006518 /* Search for a line starting with '#', empty, ending in ';' or containing
6519 * '{' or '}' and start below it. This handles the following situations:
6520 * a = cond ?
6521 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006522 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006523 * func::foo()
6524 * : something
6525 * {}
6526 * Foo::Foo (int one, int two)
6527 * : something(4),
6528 * somethingelse(3)
6529 * {}
6530 */
6531 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006533 line = ml_get(lnum - 1);
6534 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006535 if (*s == '#' || *s == NUL)
6536 break;
6537 while (*s != NUL)
6538 {
6539 s = cin_skipcomment(s);
6540 if (*s == '{' || *s == '}'
6541 || (*s == ';' && cin_nocode(s + 1)))
6542 break;
6543 if (*s != NUL)
6544 ++s;
6545 }
6546 if (*s != NUL)
6547 break;
6548 --lnum;
6549 }
6550
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006551 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006552 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006553 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006554 for (;;)
6555 {
6556 if (*s == NUL)
6557 {
6558 if (lnum == curwin->w_cursor.lnum)
6559 break;
6560 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006561 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006562 s = line;
6563 }
6564 if (s == line)
6565 {
6566 /* don't recognize "case (foo):" as a baseclass */
6567 if (cin_iscase(s, FALSE))
6568 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006569 s = cin_skipcomment(line);
6570 if (*s == NUL)
6571 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006572 }
6573
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006574 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006575 s = skip_string(s) + 1;
6576 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
6578 if (s[1] == ':')
6579 {
6580 /* skip double colon. It can't be a constructor
6581 * initialization any more */
6582 lookfor_ctor_init = FALSE;
6583 s = cin_skipcomment(s + 2);
6584 }
6585 else if (lookfor_ctor_init || class_or_struct)
6586 {
6587 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006588 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 cpp_base_class = TRUE;
6590 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006591 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 s = cin_skipcomment(s + 1);
6593 }
6594 else
6595 s = cin_skipcomment(s + 1);
6596 }
6597 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6598 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6599 {
6600 class_or_struct = TRUE;
6601 lookfor_ctor_init = FALSE;
6602
6603 if (*s == 'c')
6604 s = cin_skipcomment(s + 5);
6605 else
6606 s = cin_skipcomment(s + 6);
6607 }
6608 else
6609 {
6610 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6611 {
6612 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6613 }
6614 else if (s[0] == ')')
6615 {
6616 /* Constructor-initialization is assumed if we come across
6617 * something like "):" */
6618 class_or_struct = FALSE;
6619 lookfor_ctor_init = TRUE;
6620 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006621 else if (s[0] == '?')
6622 {
6623 /* Avoid seeing '() :' after '?' as constructor init. */
6624 return FALSE;
6625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 else if (!vim_isIDc(s[0]))
6627 {
6628 /* if it is not an identifier, we are wrong */
6629 class_or_struct = FALSE;
6630 lookfor_ctor_init = FALSE;
6631 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006632 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 {
6634 /* it can't be a constructor-initialization any more */
6635 lookfor_ctor_init = FALSE;
6636
6637 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006638 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006639 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006642 /* When the line ends in a comma don't align with it. */
6643 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006644 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 s = cin_skipcomment(s + 1);
6647 }
6648 }
6649
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006650 cached->found = cpp_base_class;
6651 if (cpp_base_class)
6652 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 return cpp_base_class;
6654}
6655
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006656 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006657get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006658{
6659 int amount;
6660 colnr_T vcol;
6661 pos_T *trypos;
6662
6663 if (col == 0)
6664 {
6665 amount = get_indent();
6666 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006667 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006668 amount = get_indent_lnum(trypos->lnum); /* XXX */
6669 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006670 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006671 }
6672 else
6673 {
6674 curwin->w_cursor.col = col;
6675 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6676 amount = (int)vcol;
6677 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006678 if (amount < curbuf->b_ind_cpp_baseclass)
6679 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006680 return amount;
6681}
6682
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683/*
6684 * Return TRUE if string "s" ends with the string "find", possibly followed by
6685 * white space and comments. Skip strings and comments.
6686 * Ignore "ignore" after "find" if it's not NULL.
6687 */
6688 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006689cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 char_u *p = s;
6692 char_u *r;
6693 int len = (int)STRLEN(find);
6694
6695 while (*p != NUL)
6696 {
6697 p = cin_skipcomment(p);
6698 if (STRNCMP(p, find, len) == 0)
6699 {
6700 r = skipwhite(p + len);
6701 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6702 r = skipwhite(r + STRLEN(ignore));
6703 if (cin_nocode(r))
6704 return TRUE;
6705 }
6706 if (*p != NUL)
6707 ++p;
6708 }
6709 return FALSE;
6710}
6711
6712/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006713 * Return TRUE when "s" starts with "word" and then a non-ID character.
6714 */
6715 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006716cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006717{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006718 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006719
6720 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6721}
6722
6723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 * Skip strings, chars and comments until at or past "trypos".
6725 * Return the column found.
6726 */
6727 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006728cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 char_u *line;
6731 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006732 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
6734 p = line = ml_get(trypos->lnum);
6735 while (*p && (colnr_T)(p - line) < trypos->col)
6736 {
6737 if (cin_iscomment(p))
6738 p = cin_skipcomment(p);
6739 else
6740 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006741 new_p = skip_string(p);
6742 if (new_p == p)
6743 ++p;
6744 else
6745 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
6747 }
6748 return (int)(p - line);
6749}
6750
6751/*
6752 * Find the '{' at the start of the block we are in.
6753 * Return NULL if no match found.
6754 * Ignore a '{' that is in a comment, makes indenting the next three lines
6755 * work. */
6756/* foo() */
6757/* { */
6758/* } */
6759
6760 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 pos_T cursor_save;
6764 pos_T *trypos;
6765 pos_T *pos;
6766 static pos_T pos_copy;
6767
6768 cursor_save = curwin->w_cursor;
6769 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6770 {
6771 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6772 trypos = &pos_copy;
6773 curwin->w_cursor = *trypos;
6774 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006775 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006777 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 break;
6779 if (pos != NULL)
6780 curwin->w_cursor.lnum = pos->lnum;
6781 }
6782 curwin->w_cursor = cursor_save;
6783 return trypos;
6784}
6785
6786/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006787 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006788 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 */
6790 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006791find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006793 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006794}
6795
6796 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006797find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006798{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 pos_T cursor_save;
6800 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006801 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006802 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006805 ind_maxp_wk = ind_maxparen;
6806retry:
6807 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 /* check if the ( is in a // comment */
6810 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006811 {
6812 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6813 if (ind_maxp_wk > 0)
6814 {
6815 curwin->w_cursor = *trypos;
6816 curwin->w_cursor.col = 0; /* XXX */
6817 goto retry;
6818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 else
6822 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006823 pos_T *trypos_wk;
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6826 trypos = &pos_copy;
6827 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006828 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006829 {
6830 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6831 - trypos_wk->lnum);
6832 if (ind_maxp_wk > 0)
6833 {
6834 curwin->w_cursor = *trypos_wk;
6835 goto retry;
6836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 }
6840 }
6841 curwin->w_cursor = cursor_save;
6842 return trypos;
6843}
6844
6845/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006846 * Find the matching '(', ignoring it if it is in a comment or before an
6847 * unmatched {.
6848 * Return NULL if no match found.
6849 */
6850 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006851find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006852{
6853 pos_T *trypos = find_match_paren(ind_maxparen);
6854
6855 if (trypos != NULL)
6856 {
6857 pos_T *tryposBrace = find_start_brace();
6858
6859 /* If both an unmatched '(' and '{' is found. Ignore the '('
6860 * position if the '{' is further down. */
6861 if (tryposBrace != NULL
6862 && (trypos->lnum != tryposBrace->lnum
6863 ? trypos->lnum < tryposBrace->lnum
6864 : trypos->col < tryposBrace->col))
6865 trypos = NULL;
6866 }
6867 return trypos;
6868}
6869
6870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 * Return ind_maxparen corrected for the difference in line number between the
6872 * cursor position and "startpos". This makes sure that searching for a
6873 * matching paren above the cursor line doesn't find a match because of
6874 * looking a few lines further.
6875 */
6876 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006877corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6880
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006881 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6882 return curbuf->b_ind_maxparen - (int)n;
6883 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884}
6885
6886/*
6887 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006888 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 */
6890 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006891find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 int i;
6894 int retval = FALSE;
6895 int open_count = 0;
6896
6897 curwin->w_cursor.col = 0; /* default is start of line */
6898
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006899 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
6901 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6902 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6903 if (l[i] == start)
6904 ++open_count;
6905 else if (l[i] == end)
6906 {
6907 if (open_count > 0)
6908 --open_count;
6909 else
6910 {
6911 curwin->w_cursor.col = i;
6912 retval = TRUE;
6913 }
6914 }
6915 }
6916 return retval;
6917}
6918
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006919/*
6920 * Parse 'cinoptions' and set the values in "curbuf".
6921 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6922 */
6923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006924parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006925{
6926 char_u *p;
6927 char_u *l;
6928 char_u *digits;
6929 int n;
6930 int divider;
6931 int fraction = 0;
6932 int sw = (int)get_sw_value(buf);
6933
6934 /*
6935 * Set the default values.
6936 */
6937 /* Spaces from a block's opening brace the prevailing indent for that
6938 * block should be. */
6939 buf->b_ind_level = sw;
6940
6941 /* Spaces from the edge of the line an open brace that's at the end of a
6942 * line is imagined to be. */
6943 buf->b_ind_open_imag = 0;
6944
6945 /* Spaces from the prevailing indent for a line that is not preceded by
6946 * an opening brace. */
6947 buf->b_ind_no_brace = 0;
6948
6949 /* Column where the first { of a function should be located }. */
6950 buf->b_ind_first_open = 0;
6951
6952 /* Spaces from the prevailing indent a leftmost open brace should be
6953 * located. */
6954 buf->b_ind_open_extra = 0;
6955
6956 /* Spaces from the matching open brace (real location for one at the left
6957 * edge; imaginary location from one that ends a line) the matching close
6958 * brace should be located. */
6959 buf->b_ind_close_extra = 0;
6960
6961 /* Spaces from the edge of the line an open brace sitting in the leftmost
6962 * column is imagined to be. */
6963 buf->b_ind_open_left_imag = 0;
6964
6965 /* Spaces jump labels should be shifted to the left if N is non-negative,
6966 * otherwise the jump label will be put to column 1. */
6967 buf->b_ind_jump_label = -1;
6968
6969 /* Spaces from the switch() indent a "case xx" label should be located. */
6970 buf->b_ind_case = sw;
6971
6972 /* Spaces from the "case xx:" code after a switch() should be located. */
6973 buf->b_ind_case_code = sw;
6974
6975 /* Lineup break at end of case in switch() with case label. */
6976 buf->b_ind_case_break = 0;
6977
6978 /* Spaces from the class declaration indent a scope declaration label
6979 * should be located. */
6980 buf->b_ind_scopedecl = sw;
6981
6982 /* Spaces from the scope declaration label code should be located. */
6983 buf->b_ind_scopedecl_code = sw;
6984
6985 /* Amount K&R-style parameters should be indented. */
6986 buf->b_ind_param = sw;
6987
6988 /* Amount a function type spec should be indented. */
6989 buf->b_ind_func_type = sw;
6990
6991 /* Amount a cpp base class declaration or constructor initialization
6992 * should be indented. */
6993 buf->b_ind_cpp_baseclass = sw;
6994
6995 /* additional spaces beyond the prevailing indent a continuation line
6996 * should be located. */
6997 buf->b_ind_continuation = sw;
6998
6999 /* Spaces from the indent of the line with an unclosed parentheses. */
7000 buf->b_ind_unclosed = sw * 2;
7001
7002 /* Spaces from the indent of the line with an unclosed parentheses, which
7003 * itself is also unclosed. */
7004 buf->b_ind_unclosed2 = sw;
7005
7006 /* Suppress ignoring spaces from the indent of a line starting with an
7007 * unclosed parentheses. */
7008 buf->b_ind_unclosed_noignore = 0;
7009
7010 /* If the opening paren is the last nonwhite character on the line, and
7011 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7012 * context (for very long lines). */
7013 buf->b_ind_unclosed_wrapped = 0;
7014
7015 /* Suppress ignoring white space when lining up with the character after
7016 * an unclosed parentheses. */
7017 buf->b_ind_unclosed_whiteok = 0;
7018
7019 /* Indent a closing parentheses under the line start of the matching
7020 * opening parentheses. */
7021 buf->b_ind_matching_paren = 0;
7022
7023 /* Indent a closing parentheses under the previous line. */
7024 buf->b_ind_paren_prev = 0;
7025
7026 /* Extra indent for comments. */
7027 buf->b_ind_comment = 0;
7028
7029 /* Spaces from the comment opener when there is nothing after it. */
7030 buf->b_ind_in_comment = 3;
7031
7032 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7033 * after the comment opener. */
7034 buf->b_ind_in_comment2 = 0;
7035
7036 /* Max lines to search for an open paren. */
7037 buf->b_ind_maxparen = 20;
7038
7039 /* Max lines to search for an open comment. */
7040 buf->b_ind_maxcomment = 70;
7041
7042 /* Handle braces for java code. */
7043 buf->b_ind_java = 0;
7044
7045 /* Not to confuse JS object properties with labels. */
7046 buf->b_ind_js = 0;
7047
7048 /* Handle blocked cases correctly. */
7049 buf->b_ind_keep_case_label = 0;
7050
7051 /* Handle C++ namespace. */
7052 buf->b_ind_cpp_namespace = 0;
7053
7054 /* Handle continuation lines containing conditions of if(), for() and
7055 * while(). */
7056 buf->b_ind_if_for_while = 0;
7057
Bram Moolenaar6b643942017-03-05 19:44:06 +01007058 /* indentation for # comments */
7059 buf->b_ind_hash_comment = 0;
7060
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007061 /* Handle C++ extern "C" or "C++" */
7062 buf->b_ind_cpp_extern_c = 0;
7063
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007064 for (p = buf->b_p_cino; *p; )
7065 {
7066 l = p++;
7067 if (*p == '-')
7068 ++p;
7069 digits = p; /* remember where the digits start */
7070 n = getdigits(&p);
7071 divider = 0;
7072 if (*p == '.') /* ".5s" means a fraction */
7073 {
7074 fraction = atol((char *)++p);
7075 while (VIM_ISDIGIT(*p))
7076 {
7077 ++p;
7078 if (divider)
7079 divider *= 10;
7080 else
7081 divider = 10;
7082 }
7083 }
7084 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7085 {
7086 if (p == digits)
7087 n = sw; /* just "s" is one 'shiftwidth' */
7088 else
7089 {
7090 n *= sw;
7091 if (divider)
7092 n += (sw * fraction + divider / 2) / divider;
7093 }
7094 ++p;
7095 }
7096 if (l[1] == '-')
7097 n = -n;
7098
7099 /* When adding an entry here, also update the default 'cinoptions' in
7100 * doc/indent.txt, and add explanation for it! */
7101 switch (*l)
7102 {
7103 case '>': buf->b_ind_level = n; break;
7104 case 'e': buf->b_ind_open_imag = n; break;
7105 case 'n': buf->b_ind_no_brace = n; break;
7106 case 'f': buf->b_ind_first_open = n; break;
7107 case '{': buf->b_ind_open_extra = n; break;
7108 case '}': buf->b_ind_close_extra = n; break;
7109 case '^': buf->b_ind_open_left_imag = n; break;
7110 case 'L': buf->b_ind_jump_label = n; break;
7111 case ':': buf->b_ind_case = n; break;
7112 case '=': buf->b_ind_case_code = n; break;
7113 case 'b': buf->b_ind_case_break = n; break;
7114 case 'p': buf->b_ind_param = n; break;
7115 case 't': buf->b_ind_func_type = n; break;
7116 case '/': buf->b_ind_comment = n; break;
7117 case 'c': buf->b_ind_in_comment = n; break;
7118 case 'C': buf->b_ind_in_comment2 = n; break;
7119 case 'i': buf->b_ind_cpp_baseclass = n; break;
7120 case '+': buf->b_ind_continuation = n; break;
7121 case '(': buf->b_ind_unclosed = n; break;
7122 case 'u': buf->b_ind_unclosed2 = n; break;
7123 case 'U': buf->b_ind_unclosed_noignore = n; break;
7124 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7125 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7126 case 'm': buf->b_ind_matching_paren = n; break;
7127 case 'M': buf->b_ind_paren_prev = n; break;
7128 case ')': buf->b_ind_maxparen = n; break;
7129 case '*': buf->b_ind_maxcomment = n; break;
7130 case 'g': buf->b_ind_scopedecl = n; break;
7131 case 'h': buf->b_ind_scopedecl_code = n; break;
7132 case 'j': buf->b_ind_java = n; break;
7133 case 'J': buf->b_ind_js = n; break;
7134 case 'l': buf->b_ind_keep_case_label = n; break;
7135 case '#': buf->b_ind_hash_comment = n; break;
7136 case 'N': buf->b_ind_cpp_namespace = n; break;
7137 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007138 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007139 }
7140 if (*p == ',')
7141 ++p;
7142 }
7143}
7144
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007145/*
7146 * Return the desired indent for C code.
7147 * Return -1 if the indent should be left alone (inside a raw string).
7148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007150get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 pos_T cur_curpos;
7153 int amount;
7154 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007155 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 colnr_T col;
7157 char_u *theline;
7158 char_u *linecopy;
7159 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007160 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007162 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 pos_T our_paren_pos;
7164 char_u *start;
7165 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007166#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167#define BRACE_AT_START 2 /* '{' is at start of line */
7168#define BRACE_AT_END 3 /* '{' is at end of line */
7169 linenr_T ourscope;
7170 char_u *l;
7171 char_u *look;
7172 char_u terminated;
7173 int lookfor;
7174#define LOOKFOR_INITIAL 0
7175#define LOOKFOR_IF 1
7176#define LOOKFOR_DO 2
7177#define LOOKFOR_CASE 3
7178#define LOOKFOR_ANY 4
7179#define LOOKFOR_TERM 5
7180#define LOOKFOR_UNTERM 6
7181#define LOOKFOR_SCOPEDECL 7
7182#define LOOKFOR_NOBREAK 8
7183#define LOOKFOR_CPP_BASECLASS 9
7184#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007185#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007186#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
7188 int whilelevel;
7189 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 int n;
7191 int iscase;
7192 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007193 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007195 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007196 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007197 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007198 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007199 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007201 /* make a copy, value is changed below */
7202 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
7204 /* remember where the cursor was when we started */
7205 cur_curpos = curwin->w_cursor;
7206
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007207 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007208 if (cur_curpos.lnum == 1)
7209 return 0;
7210
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 /* Get a copy of the current contents of the line.
7212 * This is required, because only the most recent line obtained with
7213 * ml_get is valid! */
7214 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7215 if (linecopy == NULL)
7216 return 0;
7217
7218 /*
7219 * In insert mode and the cursor is on a ')' truncate the line at the
7220 * cursor position. We don't want to line up with the matching '(' when
7221 * inserting new stuff.
7222 * For unknown reasons the cursor might be past the end of the line, thus
7223 * check for that.
7224 */
7225 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007226 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 && linecopy[curwin->w_cursor.col] == ')')
7228 linecopy[curwin->w_cursor.col] = NUL;
7229
7230 theline = skipwhite(linecopy);
7231
7232 /* move the cursor to the start of the line */
7233
7234 curwin->w_cursor.col = 0;
7235
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007236 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007237
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007239 * If we are inside a raw string don't change the indent.
7240 * Ignore a raw string inside a comment.
7241 */
7242 comment_pos = ind_find_start_comment();
7243 if (comment_pos != NULL)
7244 {
7245 /* findmatchlimit() static pos is overwritten, make a copy */
7246 tryposCopy = *comment_pos;
7247 comment_pos = &tryposCopy;
7248 }
7249 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007250 if (trypos != NULL && (comment_pos == NULL
7251 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007252 {
7253 amount = -1;
7254 goto laterend;
7255 }
7256
7257 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 * #defines and so on always go at the left when included in 'cinkeys'.
7259 */
7260 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007261 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007262 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007263 goto theend;
7264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
7266 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007267 * Is it a non-case label? Then that goes at the left margin too unless:
7268 * - JS flag is set.
7269 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007271 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007272 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
7274 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007275 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277
7278 /*
7279 * If we're inside a "//" comment and there is a "//" comment in a
7280 * previous line, lineup with that one.
7281 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007282 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 && (trypos = find_line_comment()) != NULL) /* XXX */
7284 {
7285 /* find how indented the line beginning the comment is */
7286 getvcol(curwin, trypos, &col, NULL, NULL);
7287 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007288 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 }
7290
7291 /*
7292 * If we're inside a comment and not looking at the start of the
7293 * comment, try using the 'comments' option.
7294 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007295 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {
7297 int lead_start_len = 2;
7298 int lead_middle_len = 1;
7299 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7300 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7301 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7302 char_u *p;
7303 int start_align = 0;
7304 int start_off = 0;
7305 int done = FALSE;
7306
7307 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007308 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007310 *lead_start = NUL;
7311 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
7313 p = curbuf->b_p_com;
7314 while (*p != NUL)
7315 {
7316 int align = 0;
7317 int off = 0;
7318 int what = 0;
7319
7320 while (*p != NUL && *p != ':')
7321 {
7322 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7323 what = *p++;
7324 else if (*p == COM_LEFT || *p == COM_RIGHT)
7325 align = *p++;
7326 else if (VIM_ISDIGIT(*p) || *p == '-')
7327 off = getdigits(&p);
7328 else
7329 ++p;
7330 }
7331
7332 if (*p == ':')
7333 ++p;
7334 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7335 if (what == COM_START)
7336 {
7337 STRCPY(lead_start, lead_end);
7338 lead_start_len = (int)STRLEN(lead_start);
7339 start_off = off;
7340 start_align = align;
7341 }
7342 else if (what == COM_MIDDLE)
7343 {
7344 STRCPY(lead_middle, lead_end);
7345 lead_middle_len = (int)STRLEN(lead_middle);
7346 }
7347 else if (what == COM_END)
7348 {
7349 /* If our line starts with the middle comment string, line it
7350 * up with the comment opener per the 'comments' option. */
7351 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7352 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7353 {
7354 done = TRUE;
7355 if (curwin->w_cursor.lnum > 1)
7356 {
7357 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007358 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * the middle comment string matches in the previous
7360 * line, use the indent of that line. XXX */
7361 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7362 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7363 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7364 else if (STRNCMP(look, lead_middle,
7365 lead_middle_len) == 0)
7366 {
7367 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7368 break;
7369 }
7370 /* If the start comment string doesn't match with the
7371 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007372 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 lead_start, lead_start_len) != 0)
7374 continue;
7375 }
7376 if (start_off != 0)
7377 amount += start_off;
7378 else if (start_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 break;
7382 }
7383
7384 /* If our line starts with the end comment string, line it up
7385 * with the middle comment */
7386 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7387 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7388 {
7389 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7390 /* XXX */
7391 if (off != 0)
7392 amount += off;
7393 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007394 amount += vim_strsize(lead_start)
7395 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 done = TRUE;
7397 break;
7398 }
7399 }
7400 }
7401
7402 /* If our line starts with an asterisk, line up with the
7403 * asterisk in the comment opener; otherwise, line up
7404 * with the first character of the comment text.
7405 */
7406 if (done)
7407 ;
7408 else if (theline[0] == '*')
7409 amount += 1;
7410 else
7411 {
7412 /*
7413 * If we are more than one line away from the comment opener, take
7414 * the indent of the previous non-empty line. If 'cino' has "CO"
7415 * and we are just below the comment opener and there are any
7416 * white characters after it line up with the text after it;
7417 * otherwise, add the amount specified by "c" in 'cino'
7418 */
7419 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007420 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 {
7422 if (linewhite(lnum)) /* skip blank lines */
7423 continue;
7424 amount = get_indent_lnum(lnum); /* XXX */
7425 break;
7426 }
7427 if (amount == -1) /* use the comment opener */
7428 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007429 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007431 start = ml_get(comment_pos->lnum);
7432 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007434 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007436 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007438 if (curbuf->b_ind_in_comment2 || *look == NUL)
7439 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 }
7441 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007442 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 }
7444
7445 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007446 * Are we looking at a ']' that has a match?
7447 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007448 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007449 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7450 {
7451 /* align with the line containing the '['. */
7452 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007453 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007454 }
7455
7456 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 * Are we inside parentheses or braces?
7458 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007459 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007460 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007461 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 || trypos != NULL)
7463 {
7464 if (trypos != NULL && tryposBrace != NULL)
7465 {
7466 /* Both an unmatched '(' and '{' is found. Use the one which is
7467 * closer to the current cursor position, set the other to NULL. */
7468 if (trypos->lnum != tryposBrace->lnum
7469 ? trypos->lnum < tryposBrace->lnum
7470 : trypos->col < tryposBrace->col)
7471 trypos = NULL;
7472 else
7473 tryposBrace = NULL;
7474 }
7475
7476 if (trypos != NULL)
7477 {
7478 /*
7479 * If the matching paren is more than one line away, use the indent of
7480 * a previous non-empty line that matches the same paren.
7481 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007482 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007484 /* Line up with the start of the matching paren line. */
7485 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7486 }
7487 else
7488 {
7489 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007490 our_paren_pos = *trypos;
7491 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007493 l = skipwhite(ml_get(lnum));
7494 if (cin_nocode(l)) /* skip comment lines */
7495 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007496 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007497 continue; /* ignore #define, #if, etc. */
7498 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007500 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007501 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007502 {
7503 lnum = trypos->lnum + 1;
7504 continue;
7505 }
7506
7507 /* XXX */
7508 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007509 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007510 && trypos->lnum == our_paren_pos.lnum
7511 && trypos->col == our_paren_pos.col)
7512 {
7513 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007515 if (theline[0] == ')')
7516 {
7517 if (our_paren_pos.lnum != lnum
7518 && cur_amount > amount)
7519 cur_amount = amount;
7520 amount = -1;
7521 }
7522 break;
7523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 }
7525 }
7526
7527 /*
7528 * Line up with line where the matching paren is. XXX
7529 * If the line starts with a '(' or the indent for unclosed
7530 * parentheses is zero, line up with the unclosed parentheses.
7531 */
7532 if (amount == -1)
7533 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007534 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007535 int is_if_for_while = 0;
7536
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007537 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007538 {
7539 /* Look for the outermost opening parenthesis on this line
7540 * and check whether it belongs to an "if", "for" or "while". */
7541
7542 pos_T cursor_save = curwin->w_cursor;
7543 pos_T outermost;
7544 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007545
7546 trypos = &our_paren_pos;
7547 do {
7548 outermost = *trypos;
7549 curwin->w_cursor.lnum = outermost.lnum;
7550 curwin->w_cursor.col = outermost.col;
7551
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007552 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007553 } while (trypos && trypos->lnum == outermost.lnum);
7554
7555 curwin->w_cursor = cursor_save;
7556
7557 line = ml_get(outermost.lnum);
7558
7559 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007560 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007561 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007562
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007563 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007564 look = skipwhite(look);
7565 if (*look == '(')
7566 {
7567 linenr_T save_lnum = curwin->w_cursor.lnum;
7568 char_u *line;
7569 int look_col;
7570
7571 /* Ignore a '(' in front of the line that has a match before
7572 * our matching '('. */
7573 curwin->w_cursor.lnum = our_paren_pos.lnum;
7574 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007575 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007576 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007577 if ((trypos = findmatchlimit(NULL, ')', 0,
7578 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007579 != NULL
7580 && trypos->lnum == our_paren_pos.lnum
7581 && trypos->col < our_paren_pos.col)
7582 ignore_paren_col = trypos->col + 1;
7583
7584 curwin->w_cursor.lnum = save_lnum;
7585 look = ml_get(our_paren_pos.lnum) + look_col;
7586 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007587 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7588 && is_if_for_while == 0)
7589 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007590 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 {
7592 /*
7593 * If we're looking at a close paren, line up right there;
7594 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007595 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 * the last nonwhite character of the line, use either the
7597 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007598 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 * lines).
7600 */
7601 if (theline[0] != ')')
7602 {
7603 cur_amount = MAXCOL;
7604 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007605 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 && cin_ends_in(l, (char_u *)"(", NULL))
7607 {
7608 /* look for opening unmatched paren, indent one level
7609 * for each additional level */
7610 n = 1;
7611 for (col = 0; col < our_paren_pos.col; ++col)
7612 {
7613 switch (l[col])
7614 {
7615 case '(':
7616 case '{': ++n;
7617 break;
7618
7619 case ')':
7620 case '}': if (n > 1)
7621 --n;
7622 break;
7623 }
7624 }
7625
7626 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007627 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007629 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 our_paren_pos.col++;
7631 else
7632 {
7633 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007634 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 col++;
7636 if (l[col] != NUL) /* In case of trailing space */
7637 our_paren_pos.col = col;
7638 else
7639 our_paren_pos.col++;
7640 }
7641 }
7642
7643 /*
7644 * Find how indented the paren is, or the character after it
7645 * if we did the above "if".
7646 */
7647 if (our_paren_pos.col > 0)
7648 {
7649 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7650 if (cur_amount > (int)col)
7651 cur_amount = col;
7652 }
7653 }
7654
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007655 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {
7657 /* Line up with the start of the matching paren line. */
7658 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007659 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7660 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007661 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
7663 if (cur_amount != MAXCOL)
7664 amount = cur_amount;
7665 }
7666 else
7667 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007668 /* Add b_ind_unclosed2 for each '(' before our matching one,
7669 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007671 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
7673 --our_paren_pos.col;
7674 switch (*ml_get_pos(&our_paren_pos))
7675 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007676 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 col = our_paren_pos.col;
7678 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007679 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 col = MAXCOL;
7681 break;
7682 }
7683 }
7684
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007685 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 * braces */
7687 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007688 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 else
7690 {
7691 curwin->w_cursor.lnum = our_paren_pos.lnum;
7692 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007693 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7694 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007695 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007697 {
7698 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007699 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007700 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007701 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 /*
7705 * For a line starting with ')' use the minimum of the two
7706 * positions, to avoid giving it more indent than the previous
7707 * lines:
7708 * func_long_name( if (x
7709 * arg && yy
7710 * ) ^ not here ) ^ not here
7711 */
7712 if (cur_amount < amount)
7713 amount = cur_amount;
7714 }
7715 }
7716
7717 /* add extra indent for a comment */
7718 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007719 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 else
7722 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007723 /*
7724 * We are inside braces, there is a { before this line at the position
7725 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007726 * Make a copy of tryposBrace, it may point to pos_copy inside
7727 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007728 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007729 tryposCopy = *tryposBrace;
7730 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 ourscope = trypos->lnum;
7733 start = ml_get(ourscope);
7734
7735 /*
7736 * Now figure out how indented the line is in general.
7737 * If the brace was at the start of the line, we use that;
7738 * otherwise, check out the indentation of the line as
7739 * a whole and then add the "imaginary indent" to that.
7740 */
7741 look = skipwhite(start);
7742 if (*look == '{')
7743 {
7744 getvcol(curwin, trypos, &col, NULL, NULL);
7745 amount = col;
7746 if (*start == '{')
7747 start_brace = BRACE_IN_COL0;
7748 else
7749 start_brace = BRACE_AT_START;
7750 }
7751 else
7752 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007753 /* That opening brace might have been on a continuation
7754 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 curwin->w_cursor.lnum = ourscope;
7756
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007757 /* Position the cursor over the rightmost paren, so that
7758 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 lnum = ourscope;
7760 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007761 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7762 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 lnum = trypos->lnum;
7764
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007765 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 * case 1: if (asdf &&
7767 * ldfd) {
7768 * }
7769 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007770 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7771 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007773 else if (curbuf->b_ind_js)
7774 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007776 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777
7778 start_brace = BRACE_AT_END;
7779 }
7780
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007781 /* For Javascript check if the line starts with "key:". */
7782 if (curbuf->b_ind_js)
7783 js_cur_has_key = cin_has_js_key(theline);
7784
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007786 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 * we want to be. otherwise, add the amount of room
7788 * that an indent is supposed to be.
7789 */
7790 if (theline[0] == '}')
7791 {
7792 /*
7793 * they may want closing braces to line up with something
7794 * other than the open brace. indulge them, if so.
7795 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007796 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 }
7798 else
7799 {
7800 /*
7801 * If we're looking at an "else", try to find an "if"
7802 * to match it with.
7803 * If we're looking at a "while", try to find a "do"
7804 * to match it with.
7805 */
7806 lookfor = LOOKFOR_INITIAL;
7807 if (cin_iselse(theline))
7808 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007809 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 lookfor = LOOKFOR_DO;
7811 if (lookfor != LOOKFOR_INITIAL)
7812 {
7813 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007814 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {
7816 amount = get_indent(); /* XXX */
7817 goto theend;
7818 }
7819 }
7820
7821 /*
7822 * We get here if we are not on an "while-of-do" or "else" (or
7823 * failed to find a matching "if").
7824 * Search backwards for something to line up with.
7825 * First set amount for when we don't find anything.
7826 */
7827
7828 /*
7829 * if the '{' is _really_ at the left margin, use the imaginary
7830 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007831 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 */
7833
7834 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7835 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007836 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007837 lookfor_cpp_namespace = TRUE;
7838 }
7839 else if (start_brace == BRACE_AT_START &&
7840 lookfor_cpp_namespace) /* '{' is at start */
7841 {
7842
7843 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845 else
7846 {
7847 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007848 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007849 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007850
7851 l = skipwhite(ml_get_curline());
7852 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007853 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007854 else if (cin_is_cpp_extern_c(l))
7855 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 else
7858 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007859 /* Compensate for adding b_ind_open_extra later. */
7860 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (amount < 0)
7862 amount = 0;
7863 }
7864 }
7865
7866 lookfor_break = FALSE;
7867
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007868 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {
7870 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007871 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 }
7873 else if (cin_isscopedecl(theline)) /* private:, ... */
7874 {
7875 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007876 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
7878 else
7879 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007880 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7881 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 lookfor_break = TRUE;
7883
7884 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007885 /* b_ind_level from start of block */
7886 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888 scope_amount = amount;
7889 whilelevel = 0;
7890
7891 /*
7892 * Search backwards. If we find something we recognize, line up
7893 * with that.
7894 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007895 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 * the usual amount relative to the conditional
7897 * that opens the block.
7898 */
7899 curwin->w_cursor = cur_curpos;
7900 for (;;)
7901 {
7902 curwin->w_cursor.lnum--;
7903 curwin->w_cursor.col = 0;
7904
7905 /*
7906 * If we went all the way back to the start of our scope, line
7907 * up with it.
7908 */
7909 if (curwin->w_cursor.lnum <= ourscope)
7910 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007911 /* We reached end of scope:
7912 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007914 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * don't add ind_continuation, otherwise it is a variable
7916 * declaration:
7917 * int x,
7918 * here; <-- add ind_continuation
7919 */
7920 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7921 {
7922 if (curwin->w_cursor.lnum == 0
7923 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007924 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007926 /* nothing found (abuse curbuf->b_ind_maxparen as
7927 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 * initialization) */
7929 if (cont_amount > 0)
7930 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007931 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 amount += ind_continuation;
7933 break;
7934 }
7935
7936 l = ml_get_curline();
7937
7938 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007939 * If we're in a comment or raw string now, skip to
7940 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007942 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (trypos != NULL)
7944 {
7945 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007946 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 continue;
7948 }
7949
7950 /*
7951 * Skip preprocessor directives and blank lines.
7952 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007953 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7954 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 continue;
7956
7957 if (cin_nocode(l))
7958 continue;
7959
7960 terminated = cin_isterminated(l, FALSE, TRUE);
7961
7962 /*
7963 * If we are at top level and the line looks like a
7964 * function declaration, we are done
7965 * (it's a variable declaration).
7966 */
7967 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007968 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {
7970 /* if the line is terminated with another ','
7971 * it is a continued variable initialization.
7972 * don't add extra indent.
7973 * TODO: does not work, if a function
7974 * declaration is split over multiple lines:
7975 * cin_isfuncdecl returns FALSE then.
7976 */
7977 if (terminated == ',')
7978 break;
7979
7980 /* if it es a enum declaration or an assignment,
7981 * we are done.
7982 */
7983 if (terminated != ';' && cin_isinit())
7984 break;
7985
7986 /* nothing useful found */
7987 if (terminated == 0 || terminated == '{')
7988 continue;
7989 }
7990
7991 if (terminated != ';')
7992 {
7993 /* Skip parens and braces. Position the cursor
7994 * over the rightmost paren, so that matching it
7995 * will take us back to the start of the line.
7996 */ /* XXX */
7997 trypos = NULL;
7998 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007999 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008000 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001
8002 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008003 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
8005 if (trypos != NULL)
8006 {
8007 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008008 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 continue;
8010 }
8011 }
8012
8013 /* it's a variable declaration, add indentation
8014 * like in
8015 * int a,
8016 * b;
8017 */
8018 if (cont_amount > 0)
8019 amount = cont_amount;
8020 else
8021 amount += ind_continuation;
8022 }
8023 else if (lookfor == LOOKFOR_UNTERM)
8024 {
8025 if (cont_amount > 0)
8026 amount = cont_amount;
8027 else
8028 amount += ind_continuation;
8029 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008030 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008031 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008032 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008033 && lookfor != LOOKFOR_CPP_BASECLASS
8034 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008035 {
8036 amount = scope_amount;
8037 if (theline[0] == '{')
8038 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008039 amount += curbuf->b_ind_open_extra;
8040 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008041 }
8042 }
8043
8044 if (lookfor_cpp_namespace)
8045 {
8046 /*
8047 * Looking for C++ namespace, need to look further
8048 * back.
8049 */
8050 if (curwin->w_cursor.lnum == ourscope)
8051 continue;
8052
8053 if (curwin->w_cursor.lnum == 0
8054 || curwin->w_cursor.lnum
8055 < ourscope - FIND_NAMESPACE_LIM)
8056 break;
8057
8058 l = ml_get_curline();
8059
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008060 /* If we're in a comment or raw string now, skip
8061 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008062 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008063 if (trypos != NULL)
8064 {
8065 curwin->w_cursor.lnum = trypos->lnum + 1;
8066 curwin->w_cursor.col = 0;
8067 continue;
8068 }
8069
8070 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008071 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8072 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008073 continue;
8074
8075 /* Finally the actual check for "namespace". */
8076 if (cin_is_cpp_namespace(l))
8077 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008078 amount += curbuf->b_ind_cpp_namespace
8079 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008080 break;
8081 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008082 else if (cin_is_cpp_extern_c(l))
8083 {
8084 amount += curbuf->b_ind_cpp_extern_c
8085 - added_to_amount;
8086 break;
8087 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008088
8089 if (cin_nocode(l))
8090 continue;
8091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 }
8093 break;
8094 }
8095
8096 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008097 * If we're in a comment or raw string now, skip to the start
8098 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008100 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {
8102 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008103 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 continue;
8105 }
8106
8107 l = ml_get_curline();
8108
8109 /*
8110 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008111 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008113 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 if (iscase || cin_isscopedecl(l))
8115 {
8116 /* we are only looking for cpp base class
8117 * declaration/initialization any longer */
8118 if (lookfor == LOOKFOR_CPP_BASECLASS)
8119 break;
8120
8121 /* When looking for a "do" we are not interested in
8122 * labels. */
8123 if (whilelevel > 0)
8124 continue;
8125
8126 /*
8127 * case xx:
8128 * c = 99 + <- this indent plus continuation
8129 *-> here;
8130 */
8131 if (lookfor == LOOKFOR_UNTERM
8132 || lookfor == LOOKFOR_ENUM_OR_INIT)
8133 {
8134 if (cont_amount > 0)
8135 amount = cont_amount;
8136 else
8137 amount += ind_continuation;
8138 break;
8139 }
8140
8141 /*
8142 * case xx: <- line up with this case
8143 * x = 333;
8144 * case yy:
8145 */
8146 if ( (iscase && lookfor == LOOKFOR_CASE)
8147 || (iscase && lookfor_break)
8148 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8149 {
8150 /*
8151 * Check that this case label is not for another
8152 * switch()
8153 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008154 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008155 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
8157 amount = get_indent(); /* XXX */
8158 break;
8159 }
8160 continue;
8161 }
8162
8163 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8164
8165 /*
8166 * case xx: if (cond) <- line up with this if
8167 * y = y + 1;
8168 * -> s = 99;
8169 *
8170 * case xx:
8171 * if (cond) <- line up with this line
8172 * y = y + 1;
8173 * -> s = 99;
8174 */
8175 if (lookfor == LOOKFOR_TERM)
8176 {
8177 if (n)
8178 amount = n;
8179
8180 if (!lookfor_break)
8181 break;
8182 }
8183
8184 /*
8185 * case xx: x = x + 1; <- line up with this x
8186 * -> y = y + 1;
8187 *
8188 * case xx: if (cond) <- line up with this if
8189 * -> y = y + 1;
8190 */
8191 if (n)
8192 {
8193 amount = n;
8194 l = after_label(ml_get_curline());
8195 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008196 {
8197 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008198 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008199 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008200 amount += curbuf->b_ind_level
8201 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 break;
8204 }
8205
8206 /*
8207 * Try to get the indent of a statement before the switch
8208 * label. If nothing is found, line up relative to the
8209 * switch label.
8210 * break; <- may line up with this line
8211 * case xx:
8212 * -> y = 1;
8213 */
8214 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008215 ? curbuf->b_ind_case_code
8216 : curbuf->b_ind_scopedecl_code);
8217 lookfor = curbuf->b_ind_case_break
8218 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 continue;
8220 }
8221
8222 /*
8223 * Looking for a switch() label or C++ scope declaration,
8224 * ignore other lines, skip {}-blocks.
8225 */
8226 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8227 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008228 if (find_last_paren(l, '{', '}')
8229 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008232 curwin->w_cursor.col = 0;
8233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 continue;
8235 }
8236
8237 /*
8238 * Ignore jump labels with nothing after them.
8239 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008240 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {
8242 l = after_label(ml_get_curline());
8243 if (l == NULL || cin_nocode(l))
8244 continue;
8245 }
8246
8247 /*
8248 * Ignore #defines, #if, etc.
8249 * Ignore comment and empty lines.
8250 * (need to get the line again, cin_islabel() may have
8251 * unlocked it)
8252 */
8253 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008254 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 || cin_nocode(l))
8256 continue;
8257
8258 /*
8259 * Are we at the start of a cpp base class declaration or
8260 * constructor initialization?
8261 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008262 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008263 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008264 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008265 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008266 l = ml_get_curline();
8267 }
8268 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {
8270 if (lookfor == LOOKFOR_UNTERM)
8271 {
8272 if (cont_amount > 0)
8273 amount = cont_amount;
8274 else
8275 amount += ind_continuation;
8276 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008277 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008279 /* Need to find start of the declaration. */
8280 lookfor = LOOKFOR_UNTERM;
8281 ind_continuation = 0;
8282 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 }
8284 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008285 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008286 amount = get_baseclass_amount(
8287 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 break;
8289 }
8290 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8291 {
8292 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008293 * declaration or initialization before the opening brace.
8294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 if (cin_isterminated(l, TRUE, FALSE))
8296 break;
8297 else
8298 continue;
8299 }
8300
8301 /*
8302 * What happens next depends on the line being terminated.
8303 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008304 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 * 123,
8306 * sizeof
8307 * here
8308 * Otherwise check whether it is a enumeration or structure
8309 * initialisation (not indented) or a variable declaration
8310 * (indented).
8311 */
8312 terminated = cin_isterminated(l, FALSE, TRUE);
8313
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008314 if (js_cur_has_key)
8315 {
8316 js_cur_has_key = 0; /* only check the first line */
8317 if (curbuf->b_ind_js && terminated == ',')
8318 {
8319 /* For Javascript we might be inside an object:
8320 * key: something, <- align with this
8321 * key: something
8322 * or:
8323 * key: something + <- align with this
8324 * something,
8325 * key: something
8326 */
8327 lookfor = LOOKFOR_JS_KEY;
8328 }
8329 }
8330 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8331 {
8332 amount = get_indent();
8333 break;
8334 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008335 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008336 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008337 if (tryposBrace != NULL && tryposBrace->lnum
8338 >= curwin->w_cursor.lnum)
8339 break;
8340 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008341 /* line below current line is the one that starts a
8342 * (possibly broken) line ending in a comma. */
8343 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008344 else
8345 {
8346 amount = get_indent();
8347 if (curwin->w_cursor.lnum - 1 == ourscope)
8348 /* line above is start of the scope, thus current
8349 * line is the one that stars a (possibly broken)
8350 * line ending in a comma. */
8351 break;
8352 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008353 }
8354
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8356 && terminated == ','))
8357 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008358 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8359 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008360 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 /*
8362 * if we're in the middle of a paren thing,
8363 * go back to the line that starts it so
8364 * we can get the right prevailing indent
8365 * if ( foo &&
8366 * bar )
8367 */
8368 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008369 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008371 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 */
8373 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008374 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008375 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8376 || (trypos->lnum == tryposBrace->lnum
8377 && trypos->col < tryposBrace->col)))
8378 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
8380 /*
8381 * If we are looking for ',', we also look for matching
8382 * braces.
8383 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008384 if (trypos == NULL && terminated == ','
8385 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008386 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387
8388 if (trypos != NULL)
8389 {
8390 /*
8391 * Check if we are on a case label now. This is
8392 * handled above.
8393 * case xx: if ( asdf &&
8394 * asdf)
8395 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008396 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008398 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {
8400 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008401 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 continue;
8403 }
8404 }
8405
8406 /*
8407 * Skip over continuation lines to find the one to get the
8408 * indent from
8409 * char *usethis = "bla\
8410 * bla",
8411 * here;
8412 */
8413 if (terminated == ',')
8414 {
8415 while (curwin->w_cursor.lnum > 1)
8416 {
8417 l = ml_get(curwin->w_cursor.lnum - 1);
8418 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8419 break;
8420 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008421 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
8423 }
8424
8425 /*
8426 * Get indent and pointer to text for current line,
8427 * ignoring any jump label. XXX
8428 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008429 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008430 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008431 else
8432 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 /*
8434 * If this is just above the line we are indenting, and it
8435 * starts with a '{', line it up with this line.
8436 * while (not)
8437 * -> {
8438 * }
8439 */
8440 if (terminated != ',' && lookfor != LOOKFOR_TERM
8441 && theline[0] == '{')
8442 {
8443 amount = cur_amount;
8444 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008445 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 * doesn't start with a '{', which must have a match
8447 * in the same line (scope is the same). Probably:
8448 * { 1, 2 },
8449 * -> { 3, 4 }
8450 */
8451 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008452 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008454 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {
8456 /* have to look back, whether it is a cpp base
8457 * class declaration or initialization */
8458 lookfor = LOOKFOR_CPP_BASECLASS;
8459 continue;
8460 }
8461 break;
8462 }
8463
8464 /*
8465 * Check if we are after an "if", "while", etc.
8466 * Also allow " } else".
8467 */
8468 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8469 {
8470 /*
8471 * Found an unterminated line after an if (), line up
8472 * with the last one.
8473 * if (cond)
8474 * 100 +
8475 * -> here;
8476 */
8477 if (lookfor == LOOKFOR_UNTERM
8478 || lookfor == LOOKFOR_ENUM_OR_INIT)
8479 {
8480 if (cont_amount > 0)
8481 amount = cont_amount;
8482 else
8483 amount += ind_continuation;
8484 break;
8485 }
8486
8487 /*
8488 * If this is just above the line we are indenting, we
8489 * are finished.
8490 * while (not)
8491 * -> here;
8492 * Otherwise this indent can be used when the line
8493 * before this is terminated.
8494 * yyy;
8495 * if (stat)
8496 * while (not)
8497 * xxx;
8498 * -> here;
8499 */
8500 amount = cur_amount;
8501 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008502 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (lookfor != LOOKFOR_TERM)
8504 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008505 amount += curbuf->b_ind_level
8506 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 break;
8508 }
8509
8510 /*
8511 * Special trick: when expecting the while () after a
8512 * do, line up with the while()
8513 * do
8514 * x = 1;
8515 * -> here
8516 */
8517 l = skipwhite(ml_get_curline());
8518 if (cin_isdo(l))
8519 {
8520 if (whilelevel == 0)
8521 break;
8522 --whilelevel;
8523 }
8524
8525 /*
8526 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008527 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 * Need to use the scope of this "else". XXX
8529 * If whilelevel != 0 continue looking for a "do {".
8530 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008531 if (cin_iselse(l) && whilelevel == 0)
8532 {
8533 /* If we're looking at "} else", let's make sure we
8534 * find the opening brace of the enclosing scope,
8535 * not the one from "if () {". */
8536 if (*l == '}')
8537 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008538 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008539
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008540 if ((trypos = find_start_brace()) == NULL
8541 || find_match(LOOKFOR_IF, trypos->lnum)
8542 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008543 break;
8544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 }
8546
8547 /*
8548 * If we're below an unterminated line that is not an
8549 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008550 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 * the line before this one.
8552 */
8553 else
8554 {
8555 /*
8556 * Found two unterminated lines on a row, line up with
8557 * the last one.
8558 * c = 99 +
8559 * 100 +
8560 * -> here;
8561 */
8562 if (lookfor == LOOKFOR_UNTERM)
8563 {
8564 /* When line ends in a comma add extra indent */
8565 if (terminated == ',')
8566 amount += ind_continuation;
8567 break;
8568 }
8569
8570 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8571 {
8572 /* Found two lines ending in ',', lineup with the
8573 * lowest one, but check for cpp base class
8574 * declaration/initialization, if it is an
8575 * opening brace or we are looking just for
8576 * enumerations/initializations. */
8577 if (terminated == ',')
8578 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008579 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 break;
8581
8582 lookfor = LOOKFOR_CPP_BASECLASS;
8583 continue;
8584 }
8585
8586 /* Ignore unterminated lines in between, but
8587 * reduce indent. */
8588 if (amount > cur_amount)
8589 amount = cur_amount;
8590 }
8591 else
8592 {
8593 /*
8594 * Found first unterminated line on a row, may
8595 * line up with this line, remember its indent
8596 * 100 +
8597 * -> here;
8598 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008599 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008601
8602 n = (int)STRLEN(l);
8603 if (terminated == ',' && (*skipwhite(l) == ']'
8604 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
8607 /*
8608 * If previous line ends in ',', check whether we
8609 * are in an initialization or enum
8610 * struct xxx =
8611 * {
8612 * sizeof a,
8613 * 124 };
8614 * or a normal possible continuation line.
8615 * but only, of no other statement has been found
8616 * yet.
8617 */
8618 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8619 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008620 if (curbuf->b_ind_js)
8621 {
8622 /* Search for a line ending in a comma
8623 * and line up with the line below it
8624 * (could be the current line).
8625 * some = [
8626 * 1, <- line up here
8627 * 2,
8628 * some = [
8629 * 3 + <- line up here
8630 * 4 *
8631 * 5,
8632 * 6,
8633 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008634 if (cin_iscomment(skipwhite(l)))
8635 break;
8636 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008637 trypos = find_match_char('[',
8638 curbuf->b_ind_maxparen);
8639 if (trypos != NULL)
8640 {
8641 if (trypos->lnum
8642 == curwin->w_cursor.lnum - 1)
8643 {
8644 /* Current line is first inside
8645 * [], line up with it. */
8646 break;
8647 }
8648 ourscope = trypos->lnum;
8649 }
8650 }
8651 else
8652 {
8653 lookfor = LOOKFOR_ENUM_OR_INIT;
8654 cont_amount = cin_first_id_amount();
8655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
8657 else
8658 {
8659 if (lookfor == LOOKFOR_INITIAL
8660 && *l != NUL
8661 && l[STRLEN(l) - 1] == '\\')
8662 /* XXX */
8663 cont_amount = cin_get_equal_amount(
8664 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008665 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008666 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008667 && lookfor != LOOKFOR_COMMA
8668 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 lookfor = LOOKFOR_UNTERM;
8670 }
8671 }
8672 }
8673 }
8674
8675 /*
8676 * Check if we are after a while (cond);
8677 * If so: Ignore until the matching "do".
8678 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008679 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {
8681 /*
8682 * Found an unterminated line after a while ();, line up
8683 * with the last one.
8684 * while (cond);
8685 * 100 + <- line up with this one
8686 * -> here;
8687 */
8688 if (lookfor == LOOKFOR_UNTERM
8689 || lookfor == LOOKFOR_ENUM_OR_INIT)
8690 {
8691 if (cont_amount > 0)
8692 amount = cont_amount;
8693 else
8694 amount += ind_continuation;
8695 break;
8696 }
8697
8698 if (whilelevel == 0)
8699 {
8700 lookfor = LOOKFOR_TERM;
8701 amount = get_indent(); /* XXX */
8702 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008703 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705 ++whilelevel;
8706 }
8707
8708 /*
8709 * We are after a "normal" statement.
8710 * If we had another statement we can stop now and use the
8711 * indent of that other statement.
8712 * Otherwise the indent of the current statement may be used,
8713 * search backwards for the next "normal" statement.
8714 */
8715 else
8716 {
8717 /*
8718 * Skip single break line, if before a switch label. It
8719 * may be lined up with the case label.
8720 */
8721 if (lookfor == LOOKFOR_NOBREAK
8722 && cin_isbreak(skipwhite(ml_get_curline())))
8723 {
8724 lookfor = LOOKFOR_ANY;
8725 continue;
8726 }
8727
8728 /*
8729 * Handle "do {" line.
8730 */
8731 if (whilelevel > 0)
8732 {
8733 l = cin_skipcomment(ml_get_curline());
8734 if (cin_isdo(l))
8735 {
8736 amount = get_indent(); /* XXX */
8737 --whilelevel;
8738 continue;
8739 }
8740 }
8741
8742 /*
8743 * Found a terminated line above an unterminated line. Add
8744 * the amount for a continuation line.
8745 * x = 1;
8746 * y = foo +
8747 * -> here;
8748 * or
8749 * int x = 1;
8750 * int foo,
8751 * -> here;
8752 */
8753 if (lookfor == LOOKFOR_UNTERM
8754 || lookfor == LOOKFOR_ENUM_OR_INIT)
8755 {
8756 if (cont_amount > 0)
8757 amount = cont_amount;
8758 else
8759 amount += ind_continuation;
8760 break;
8761 }
8762
8763 /*
8764 * Found a terminated line above a terminated line or "if"
8765 * etc. line. Use the amount of the line below us.
8766 * x = 1; x = 1;
8767 * if (asdf) y = 2;
8768 * while (asdf) ->here;
8769 * here;
8770 * ->foo;
8771 */
8772 if (lookfor == LOOKFOR_TERM)
8773 {
8774 if (!lookfor_break && whilelevel == 0)
8775 break;
8776 }
8777
8778 /*
8779 * First line above the one we're indenting is terminated.
8780 * To know what needs to be done look further backward for
8781 * a terminated line.
8782 */
8783 else
8784 {
8785 /*
8786 * position the cursor over the rightmost paren, so
8787 * that matching it will take us back to the start of
8788 * the line. Helps for:
8789 * func(asdr,
8790 * asdfasdf);
8791 * here;
8792 */
8793term_again:
8794 l = ml_get_curline();
8795 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008796 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008797 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 {
8799 /*
8800 * Check if we are on a case label now. This is
8801 * handled above.
8802 * case xx: if ( asdf &&
8803 * asdf)
8804 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008805 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008807 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 {
8809 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008810 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 continue;
8812 }
8813 }
8814
8815 /* When aligning with the case statement, don't align
8816 * with a statement after it.
8817 * case 1: { <-- don't use this { position
8818 * stat;
8819 * }
8820 * case 2:
8821 * stat;
8822 * }
8823 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008824 iscase = (curbuf->b_ind_keep_case_label
8825 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826
8827 /*
8828 * Get indent and pointer to text for current line,
8829 * ignoring any jump label.
8830 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008831 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008834 amount += curbuf->b_ind_open_extra;
8835 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008836 l = skipwhite(l);
8837 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008838 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8840
8841 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008842 * When a terminated line starts with "else" skip to
8843 * the matching "if":
8844 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008845 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008846 * Need to use the scope of this "else". XXX
8847 * If whilelevel != 0 continue looking for a "do {".
8848 */
8849 if (lookfor == LOOKFOR_TERM
8850 && *l != '}'
8851 && cin_iselse(l)
8852 && whilelevel == 0)
8853 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008854 if ((trypos = find_start_brace()) == NULL
8855 || find_match(LOOKFOR_IF, trypos->lnum)
8856 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008857 break;
8858 continue;
8859 }
8860
8861 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 * If we're at the end of a block, skip to the start of
8863 * that block.
8864 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008865 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008866 if (find_last_paren(l, '{', '}') /* XXX */
8867 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008869 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 /* if not "else {" check for terminated again */
8871 /* but skip block for "} else {" */
8872 l = cin_skipcomment(ml_get_curline());
8873 if (*l == '}' || !cin_iselse(l))
8874 goto term_again;
8875 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008876 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 }
8878 }
8879 }
8880 }
8881 }
8882 }
8883
8884 /* add extra indent for a comment */
8885 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008886 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008887
8888 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008889 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8890 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008891
8892 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008894
8895 /*
8896 * ok -- we're not inside any sort of structure at all!
8897 *
8898 * This means we're at the top level, and everything should
8899 * basically just match where the previous line is, except
8900 * for the lines immediately following a function declaration,
8901 * which are K&R-style parameters and need to be indented.
8902 *
8903 * if our line starts with an open brace, forget about any
8904 * prevailing indent and make sure it looks like the start
8905 * of a function
8906 */
8907
8908 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008910 amount = curbuf->b_ind_first_open;
8911 goto theend;
8912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008914 /*
8915 * If the NEXT line is a function declaration, the current
8916 * line needs to be indented as a function type spec.
8917 * Don't do this if the current line looks like a comment or if the
8918 * current line is terminated, ie. ends in ';', or if the current line
8919 * contains { or }: "void f() {\n if (1)"
8920 */
8921 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8922 && !cin_nocode(theline)
8923 && vim_strchr(theline, '{') == NULL
8924 && vim_strchr(theline, '}') == NULL
8925 && !cin_ends_in(theline, (char_u *)":", NULL)
8926 && !cin_ends_in(theline, (char_u *)",", NULL)
8927 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8928 cur_curpos.lnum + 1)
8929 && !cin_isterminated(theline, FALSE, TRUE))
8930 {
8931 amount = curbuf->b_ind_func_type;
8932 goto theend;
8933 }
8934
8935 /* search backwards until we find something we recognize */
8936 amount = 0;
8937 curwin->w_cursor = cur_curpos;
8938 while (curwin->w_cursor.lnum > 1)
8939 {
8940 curwin->w_cursor.lnum--;
8941 curwin->w_cursor.col = 0;
8942
8943 l = ml_get_curline();
8944
8945 /*
8946 * If we're in a comment or raw string now, skip to the start
8947 * of it.
8948 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008949 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008951 curwin->w_cursor.lnum = trypos->lnum + 1;
8952 curwin->w_cursor.col = 0;
8953 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 }
8955
8956 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008957 * Are we at the start of a cpp base class declaration or
8958 * constructor initialization?
8959 */ /* XXX */
8960 n = FALSE;
8961 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008963 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8964 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008966 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008968 /* XXX */
8969 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8970 break;
8971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008973 /*
8974 * Skip preprocessor directives and blank lines.
8975 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008976 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008977 continue;
8978
8979 if (cin_nocode(l))
8980 continue;
8981
8982 /*
8983 * If the previous line ends in ',', use one level of
8984 * indentation:
8985 * int foo,
8986 * bar;
8987 * do this before checking for '}' in case of eg.
8988 * enum foobar
8989 * {
8990 * ...
8991 * } foo,
8992 * bar;
8993 */
8994 n = 0;
8995 if (cin_ends_in(l, (char_u *)",", NULL)
8996 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8997 {
8998 /* take us back to opening paren */
8999 if (find_last_paren(l, '(', ')')
9000 && (trypos = find_match_paren(
9001 curbuf->b_ind_maxparen)) != NULL)
9002 curwin->w_cursor = *trypos;
9003
9004 /* For a line ending in ',' that is a continuation line go
9005 * back to the first line with a backslash:
9006 * char *foo = "bla\
9007 * bla",
9008 * here;
9009 */
9010 while (n == 0 && curwin->w_cursor.lnum > 1)
9011 {
9012 l = ml_get(curwin->w_cursor.lnum - 1);
9013 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9014 break;
9015 --curwin->w_cursor.lnum;
9016 curwin->w_cursor.col = 0;
9017 }
9018
9019 amount = get_indent(); /* XXX */
9020
9021 if (amount == 0)
9022 amount = cin_first_id_amount();
9023 if (amount == 0)
9024 amount = ind_continuation;
9025 break;
9026 }
9027
9028 /*
9029 * If the line looks like a function declaration, and we're
9030 * not in a comment, put it the left margin.
9031 */
9032 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9033 break;
9034 l = ml_get_curline();
9035
9036 /*
9037 * Finding the closing '}' of a previous function. Put
9038 * current line at the left margin. For when 'cino' has "fs".
9039 */
9040 if (*skipwhite(l) == '}')
9041 break;
9042
9043 /* (matching {)
9044 * If the previous line ends on '};' (maybe followed by
9045 * comments) align at column 0. For example:
9046 * char *string_array[] = { "foo",
9047 * / * x * / "b};ar" }; / * foobar * /
9048 */
9049 if (cin_ends_in(l, (char_u *)"};", NULL))
9050 break;
9051
9052 /*
9053 * If the previous line ends on '[' we are probably in an
9054 * array constant:
9055 * something = [
9056 * 234, <- extra indent
9057 */
9058 if (cin_ends_in(l, (char_u *)"[", NULL))
9059 {
9060 amount = get_indent() + ind_continuation;
9061 break;
9062 }
9063
9064 /*
9065 * Find a line only has a semicolon that belongs to a previous
9066 * line ending in '}', e.g. before an #endif. Don't increase
9067 * indent then.
9068 */
9069 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9070 {
9071 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
9073 while (curwin->w_cursor.lnum > 1)
9074 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009075 look = ml_get(--curwin->w_cursor.lnum);
9076 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009077 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009079 }
9080 if (curwin->w_cursor.lnum > 0
9081 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009084 curwin->w_cursor = curpos_save;
9085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009087 /*
9088 * If the PREVIOUS line is a function declaration, the current
9089 * line (and the ones that follow) needs to be indented as
9090 * parameters.
9091 */
9092 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9093 {
9094 amount = curbuf->b_ind_param;
9095 break;
9096 }
9097
9098 /*
9099 * If the previous line ends in ';' and the line before the
9100 * previous line ends in ',' or '\', ident to column zero:
9101 * int foo,
9102 * bar;
9103 * indent_to_0 here;
9104 */
9105 if (cin_ends_in(l, (char_u *)";", NULL))
9106 {
9107 l = ml_get(curwin->w_cursor.lnum - 1);
9108 if (cin_ends_in(l, (char_u *)",", NULL)
9109 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9110 break;
9111 l = ml_get_curline();
9112 }
9113
9114 /*
9115 * Doesn't look like anything interesting -- so just
9116 * use the indent of this line.
9117 *
9118 * Position the cursor over the rightmost paren, so that
9119 * matching it will take us back to the start of the line.
9120 */
9121 find_last_paren(l, '(', ')');
9122
9123 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9124 curwin->w_cursor = *trypos;
9125 amount = get_indent(); /* XXX */
9126 break;
9127 }
9128
9129 /* add extra indent for a comment */
9130 if (cin_iscomment(theline))
9131 amount += curbuf->b_ind_comment;
9132
9133 /* add extra indent if the previous line ended in a backslash:
9134 * "asdfasdf\
9135 * here";
9136 * char *foo = "asdf\
9137 * here";
9138 */
9139 if (cur_curpos.lnum > 1)
9140 {
9141 l = ml_get(cur_curpos.lnum - 1);
9142 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9143 {
9144 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9145 if (cur_amount > 0)
9146 amount = cur_amount;
9147 else if (cur_amount == 0)
9148 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 }
9150 }
9151
9152theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009153 if (amount < 0)
9154 amount = 0;
9155
9156laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 /* put the cursor back where it belongs */
9158 curwin->w_cursor = cur_curpos;
9159
9160 vim_free(linecopy);
9161
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 return amount;
9163}
9164
9165 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009166find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168 char_u *look;
9169 pos_T *theirscope;
9170 char_u *mightbeif;
9171 int elselevel;
9172 int whilelevel;
9173
9174 if (lookfor == LOOKFOR_IF)
9175 {
9176 elselevel = 1;
9177 whilelevel = 0;
9178 }
9179 else
9180 {
9181 elselevel = 0;
9182 whilelevel = 1;
9183 }
9184
9185 curwin->w_cursor.col = 0;
9186
9187 while (curwin->w_cursor.lnum > ourscope + 1)
9188 {
9189 curwin->w_cursor.lnum--;
9190 curwin->w_cursor.col = 0;
9191
9192 look = cin_skipcomment(ml_get_curline());
9193 if (cin_iselse(look)
9194 || cin_isif(look)
9195 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009196 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 {
9198 /*
9199 * if we've gone outside the braces entirely,
9200 * we must be out of scope...
9201 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009202 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 if (theirscope == NULL)
9204 break;
9205
9206 /*
9207 * and if the brace enclosing this is further
9208 * back than the one enclosing the else, we're
9209 * out of luck too.
9210 */
9211 if (theirscope->lnum < ourscope)
9212 break;
9213
9214 /*
9215 * and if they're enclosed in a *deeper* brace,
9216 * then we can ignore it because it's in a
9217 * different scope...
9218 */
9219 if (theirscope->lnum > ourscope)
9220 continue;
9221
9222 /*
9223 * if it was an "else" (that's not an "else if")
9224 * then we need to go back to another if, so
9225 * increment elselevel
9226 */
9227 look = cin_skipcomment(ml_get_curline());
9228 if (cin_iselse(look))
9229 {
9230 mightbeif = cin_skipcomment(look + 4);
9231 if (!cin_isif(mightbeif))
9232 ++elselevel;
9233 continue;
9234 }
9235
9236 /*
9237 * if it was a "while" then we need to go back to
9238 * another "do", so increment whilelevel. XXX
9239 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009240 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
9242 ++whilelevel;
9243 continue;
9244 }
9245
9246 /* If it's an "if" decrement elselevel */
9247 look = cin_skipcomment(ml_get_curline());
9248 if (cin_isif(look))
9249 {
9250 elselevel--;
9251 /*
9252 * When looking for an "if" ignore "while"s that
9253 * get in the way.
9254 */
9255 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9256 whilelevel = 0;
9257 }
9258
9259 /* If it's a "do" decrement whilelevel */
9260 if (cin_isdo(look))
9261 whilelevel--;
9262
9263 /*
9264 * if we've used up all the elses, then
9265 * this must be the if that we want!
9266 * match the indent level of that if.
9267 */
9268 if (elselevel <= 0 && whilelevel <= 0)
9269 {
9270 return OK;
9271 }
9272 }
9273 }
9274 return FAIL;
9275}
9276
9277# if defined(FEAT_EVAL) || defined(PROTO)
9278/*
9279 * Get indent level from 'indentexpr'.
9280 */
9281 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009282get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009284 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009285 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009286 pos_T save_pos;
9287 colnr_T save_curswant;
9288 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009290 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9291 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009293 /* Save and restore cursor position and curswant, in case it was changed
9294 * via :normal commands */
9295 save_pos = curwin->w_cursor;
9296 save_curswant = curwin->w_curswant;
9297 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009299 if (use_sandbox)
9300 ++sandbox;
9301 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009302
9303 /* Need to make a copy, the 'indentexpr' option could be changed while
9304 * evaluating it. */
9305 inde_copy = vim_strsave(curbuf->b_p_inde);
9306 if (inde_copy != NULL)
9307 {
9308 indent = (int)eval_to_number(inde_copy);
9309 vim_free(inde_copy);
9310 }
9311
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009312 if (use_sandbox)
9313 --sandbox;
9314 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315
9316 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9317 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9318 * command. */
9319 save_State = State;
9320 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009321 curwin->w_cursor = save_pos;
9322 curwin->w_curswant = save_curswant;
9323 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 check_cursor();
9325 State = save_State;
9326
9327 /* If there is an error, just keep the current indent. */
9328 if (indent < 0)
9329 indent = get_indent();
9330
9331 return indent;
9332}
9333# endif
9334
9335#endif /* FEAT_CINDENT */
9336
9337#if defined(FEAT_LISP) || defined(PROTO)
9338
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009339static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340
9341 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009342lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
9344 char_u buf[LSIZE];
9345 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009346 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 while (*word != NUL)
9349 {
9350 (void)copy_option_part(&word, buf, LSIZE, ",");
9351 len = (int)STRLEN(buf);
9352 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9353 return TRUE;
9354 }
9355 return FALSE;
9356}
9357
9358/*
9359 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9360 * The incompatible newer method is quite a bit better at indenting
9361 * code in lisp-like languages than the traditional one; it's still
9362 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9363 *
9364 * TODO:
9365 * Findmatch() should be adapted for lisp, also to make showmatch
9366 * work correctly: now (v5.3) it seems all C/C++ oriented:
9367 * - it does not recognize the #\( and #\) notations as character literals
9368 * - it doesn't know about comments starting with a semicolon
9369 * - it incorrectly interprets '(' as a character literal
9370 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009371 * Update from Sergey Khorev:
9372 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 */
9374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009375get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009377 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 int amount;
9379 char_u *that;
9380 colnr_T col;
9381 colnr_T firsttry;
9382 int parencount, quotecount;
9383 int vi_lisp;
9384
9385 /* Set vi_lisp to use the vi-compatible method */
9386 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9387
9388 realpos = curwin->w_cursor;
9389 curwin->w_cursor.col = 0;
9390
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009391 if ((pos = findmatch(NULL, '(')) == NULL)
9392 pos = findmatch(NULL, '[');
9393 else
9394 {
9395 paren = *pos;
9396 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009397 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009398 pos = &paren;
9399 }
9400 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 {
9402 /* Extra trick: Take the indent of the first previous non-white
9403 * line that is at the same () level. */
9404 amount = -1;
9405 parencount = 0;
9406
9407 while (--curwin->w_cursor.lnum >= pos->lnum)
9408 {
9409 if (linewhite(curwin->w_cursor.lnum))
9410 continue;
9411 for (that = ml_get_curline(); *that != NUL; ++that)
9412 {
9413 if (*that == ';')
9414 {
9415 while (*(that + 1) != NUL)
9416 ++that;
9417 continue;
9418 }
9419 if (*that == '\\')
9420 {
9421 if (*(that + 1) != NUL)
9422 ++that;
9423 continue;
9424 }
9425 if (*that == '"' && *(that + 1) != NUL)
9426 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009427 while (*++that && *that != '"')
9428 {
9429 /* skipping escaped characters in the string */
9430 if (*that == '\\')
9431 {
9432 if (*++that == NUL)
9433 break;
9434 if (that[1] == NUL)
9435 {
9436 ++that;
9437 break;
9438 }
9439 }
9440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009442 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009444 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 --parencount;
9446 }
9447 if (parencount == 0)
9448 {
9449 amount = get_indent();
9450 break;
9451 }
9452 }
9453
9454 if (amount == -1)
9455 {
9456 curwin->w_cursor.lnum = pos->lnum;
9457 curwin->w_cursor.col = pos->col;
9458 col = pos->col;
9459
9460 that = ml_get_curline();
9461
9462 if (vi_lisp && get_indent() == 0)
9463 amount = 2;
9464 else
9465 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009466 char_u *line = that;
9467
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 amount = 0;
9469 while (*that && col)
9470 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009471 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 col--;
9473 }
9474
9475 /*
9476 * Some keywords require "body" indenting rules (the
9477 * non-standard-lisp ones are Scheme special forms):
9478 *
9479 * (let ((a 1)) instead (let ((a 1))
9480 * (...)) of (...))
9481 */
9482
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009483 if (!vi_lisp && (*that == '(' || *that == '[')
9484 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 amount += 2;
9486 else
9487 {
9488 that++;
9489 amount++;
9490 firsttry = amount;
9491
Bram Moolenaar1c465442017-03-12 20:10:05 +01009492 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009494 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 ++that;
9496 }
9497
9498 if (*that && *that != ';') /* not a comment line */
9499 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009500 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009502 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 firsttry++;
9504
9505 parencount = 0;
9506 quotecount = 0;
9507
9508 if (vi_lisp
9509 || (*that != '"'
9510 && *that != '\''
9511 && *that != '#'
9512 && (*that < '0' || *that > '9')))
9513 {
9514 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009515 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 || quotecount
9517 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009518 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 && !quotecount
9520 && !parencount
9521 && vi_lisp)))
9522 {
9523 if (*that == '"')
9524 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009525 if ((*that == '(' || *that == '[')
9526 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009528 if ((*that == ')' || *that == ']')
9529 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 --parencount;
9531 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009532 amount += lbr_chartabsize_adv(
9533 line, &that, (colnr_T)amount);
9534 amount += lbr_chartabsize_adv(
9535 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 }
9537 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009538 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009540 amount += lbr_chartabsize(
9541 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 that++;
9543 }
9544 if (!*that || *that == ';')
9545 amount = firsttry;
9546 }
9547 }
9548 }
9549 }
9550 }
9551 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009552 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553
9554 curwin->w_cursor = realpos;
9555
9556 return amount;
9557}
9558#endif /* FEAT_LISP */
9559
9560 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009561prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009563#if defined(SIGHUP) && defined(SIG_IGN)
9564 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9565 * makes Vim exit and then handling SIGHUP causes various reentrance
9566 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009567 signal(SIGHUP, SIG_IGN);
9568#endif
9569
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570#ifdef FEAT_GUI
9571 if (gui.in_use)
9572 {
9573 gui.dying = TRUE;
9574 out_trash(); /* trash any pending output */
9575 }
9576 else
9577#endif
9578 {
9579 windgoto((int)Rows - 1, 0);
9580
9581 /*
9582 * Switch terminal mode back now, so messages end up on the "normal"
9583 * screen (if there are two screens).
9584 */
9585 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009586 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 out_flush();
9588 }
9589}
9590
9591/*
9592 * Preserve files and exit.
9593 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009594 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9595 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 */
9597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009598preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
9600 buf_T *buf;
9601
9602 prepare_to_exit();
9603
Bram Moolenaar4770d092006-01-12 23:22:24 +00009604 /* Setting this will prevent free() calls. That avoids calling free()
9605 * recursively when free() was invoked with a bad pointer. */
9606 really_exiting = TRUE;
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 out_str(IObuff);
9609 screen_start(); /* don't know where cursor is now */
9610 out_flush();
9611
9612 ml_close_notmod(); /* close all not-modified buffers */
9613
Bram Moolenaar29323592016-07-24 22:04:11 +02009614 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 {
9616 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9617 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009618 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 screen_start(); /* don't know where cursor is now */
9620 out_flush();
9621 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9622 break;
9623 }
9624 }
9625
9626 ml_close_all(FALSE); /* close all memfiles, without deleting */
9627
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009628 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
9630 getout(1);
9631}
9632
9633/*
9634 * return TRUE if "fname" exists.
9635 */
9636 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009637vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009639 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640
9641 if (mch_stat((char *)fname, &st))
9642 return FALSE;
9643 return TRUE;
9644}
9645
9646/*
9647 * Check for CTRL-C pressed, but only once in a while.
9648 * Should be used instead of ui_breakcheck() for functions that check for
9649 * each line in the file. Calling ui_breakcheck() each time takes too much
9650 * time, because it can be a system call.
9651 */
9652
9653#ifndef BREAKCHECK_SKIP
9654# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9655# define BREAKCHECK_SKIP 200
9656# else
9657# define BREAKCHECK_SKIP 32
9658# endif
9659#endif
9660
9661static int breakcheck_count = 0;
9662
9663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009664line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666 if (++breakcheck_count >= BREAKCHECK_SKIP)
9667 {
9668 breakcheck_count = 0;
9669 ui_breakcheck();
9670 }
9671}
9672
9673/*
9674 * Like line_breakcheck() but check 10 times less often.
9675 */
9676 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009677fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678{
9679 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9680 {
9681 breakcheck_count = 0;
9682 ui_breakcheck();
9683 }
9684}
9685
9686/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009687 * Invoke expand_wildcards() for one pattern.
9688 * Expand items like "%:h" before the expansion.
9689 * Returns OK or FAIL.
9690 */
9691 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009692expand_wildcards_eval(
9693 char_u **pat, /* pointer to input pattern */
9694 int *num_file, /* resulting number of files */
9695 char_u ***file, /* array of resulting files */
9696 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009697{
9698 int ret = FAIL;
9699 char_u *eval_pat = NULL;
9700 char_u *exp_pat = *pat;
9701 char_u *ignored_msg;
9702 int usedlen;
9703
9704 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9705 {
9706 ++emsg_off;
9707 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9708 NULL, &ignored_msg, NULL);
9709 --emsg_off;
9710 if (eval_pat != NULL)
9711 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9712 }
9713
9714 if (exp_pat != NULL)
9715 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9716
9717 if (eval_pat != NULL)
9718 {
9719 vim_free(exp_pat);
9720 vim_free(eval_pat);
9721 }
9722
9723 return ret;
9724}
9725
9726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9728 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009729 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 */
9731 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009732expand_wildcards(
9733 int num_pat, /* number of input patterns */
9734 char_u **pat, /* array of input patterns */
9735 int *num_files, /* resulting number of files */
9736 char_u ***files, /* array of resulting files */
9737 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738{
9739 int retval;
9740 int i, j;
9741 char_u *p;
9742 int non_suf_match; /* number without matching suffix */
9743
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009744 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745
9746 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009747 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 return retval;
9749
9750#ifdef FEAT_WILDIGN
9751 /*
9752 * Remove names that match 'wildignore'.
9753 */
9754 if (*p_wig)
9755 {
9756 char_u *ffname;
9757
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009758 /* check all files in (*files)[] */
9759 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009761 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (ffname == NULL) /* out of memory */
9763 break;
9764# ifdef VMS
9765 vms_remove_version(ffname);
9766# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009767 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009769 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009770 vim_free((*files)[i]);
9771 for (j = i; j + 1 < *num_files; ++j)
9772 (*files)[j] = (*files)[j + 1];
9773 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 --i;
9775 }
9776 vim_free(ffname);
9777 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009778
9779 /* If the number of matches is now zero, we fail. */
9780 if (*num_files == 0)
9781 {
9782 vim_free(*files);
9783 *files = NULL;
9784 return FAIL;
9785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 }
9787#endif
9788
9789 /*
9790 * Move the names where 'suffixes' match to the end.
9791 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009792 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 {
9794 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009795 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009797 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 {
9799 /*
9800 * Move the name without matching suffix to the front
9801 * of the list.
9802 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009803 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009805 (*files)[j] = (*files)[j - 1];
9806 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
9808 }
9809 }
9810
9811 return retval;
9812}
9813
9814/*
9815 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9816 */
9817 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009818match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819{
9820 int fnamelen, setsuflen;
9821 char_u *setsuf;
9822#define MAXSUFLEN 30 /* maximum length of a file suffix */
9823 char_u suf_buf[MAXSUFLEN];
9824
9825 fnamelen = (int)STRLEN(fname);
9826 setsuflen = 0;
9827 for (setsuf = p_su; *setsuf; )
9828 {
9829 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009830 if (setsuflen == 0)
9831 {
9832 char_u *tail = gettail(fname);
9833
9834 /* empty entry: match name without a '.' */
9835 if (vim_strchr(tail, '.') == NULL)
9836 {
9837 setsuflen = 1;
9838 break;
9839 }
9840 }
9841 else
9842 {
9843 if (fnamelen >= setsuflen
9844 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9845 (size_t)setsuflen) == 0)
9846 break;
9847 setsuflen = 0;
9848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 }
9850 return (setsuflen != 0);
9851}
9852
9853#if !defined(NO_EXPANDPATH) || defined(PROTO)
9854
9855# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009856static int vim_backtick(char_u *p);
9857static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858# endif
9859
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009860# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861/*
9862 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9863 * it's shared between these systems.
9864 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009865# if defined(PROTO)
9866# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867# else
9868# ifdef __BORLANDC__
9869# define _cdecl _RTLENTRYF
9870# endif
9871# endif
9872
9873/*
9874 * comparison function for qsort in dos_expandpath()
9875 */
9876 static int _cdecl
9877pstrcmp(const void *a, const void *b)
9878{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009879 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880}
9881
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009883 * Recursively expand one path component into all matching files and/or
9884 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 * Return the number of matches found.
9886 * "path" has backslashes before chars that are not to be expanded, starting
9887 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009888 * Return the number of matches found.
9889 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 */
9891 static int
9892dos_expandpath(
9893 garray_T *gap,
9894 char_u *path,
9895 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009896 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009897 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009899 char_u *buf;
9900 char_u *path_end;
9901 char_u *p, *s, *e;
9902 int start_len = gap->ga_len;
9903 char_u *pat;
9904 regmatch_T regmatch;
9905 int starts_with_dot;
9906 int matches;
9907 int len;
9908 int starstar = FALSE;
9909 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 WIN32_FIND_DATA fb;
9911 HANDLE hFind = (HANDLE)0;
9912# ifdef FEAT_MBYTE
9913 WIN32_FIND_DATAW wfb;
9914 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9915# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009917 int ok;
9918
9919 /* Expanding "**" may take a long time, check for CTRL-C. */
9920 if (stardepth > 0)
9921 {
9922 ui_breakcheck();
9923 if (got_int)
9924 return 0;
9925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009927 /* Make room for file name. When doing encoding conversion the actual
9928 * length may be quite a bit longer, thus use the maximum possible length. */
9929 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (buf == NULL)
9931 return 0;
9932
9933 /*
9934 * Find the first part in the path name that contains a wildcard or a ~1.
9935 * Copy it into buf, including the preceding characters.
9936 */
9937 p = buf;
9938 s = buf;
9939 e = NULL;
9940 path_end = path;
9941 while (*path_end != NUL)
9942 {
9943 /* May ignore a wildcard that has a backslash before it; it will
9944 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9945 if (path_end >= path + wildoff && rem_backslash(path_end))
9946 *p++ = *path_end++;
9947 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9948 {
9949 if (e != NULL)
9950 break;
9951 s = p + 1;
9952 }
9953 else if (path_end >= path + wildoff
9954 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9955 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009956# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 if (has_mbyte)
9958 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009959 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 STRNCPY(p, path_end, len);
9961 p += len;
9962 path_end += len;
9963 }
9964 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 *p++ = *path_end++;
9967 }
9968 e = p;
9969 *e = NUL;
9970
9971 /* now we have one wildcard component between s and e */
9972 /* Remove backslashes between "wildoff" and the start of the wildcard
9973 * component. */
9974 for (p = buf + wildoff; p < s; ++p)
9975 if (rem_backslash(p))
9976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009977 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 --e;
9979 --s;
9980 }
9981
Bram Moolenaar231334e2005-07-25 20:46:57 +00009982 /* Check for "**" between "s" and "e". */
9983 for (p = s; p < e; ++p)
9984 if (p[0] == '*' && p[1] == '*')
9985 starstar = TRUE;
9986
Bram Moolenaard82103e2016-01-17 17:04:05 +01009987 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9989 if (pat == NULL)
9990 {
9991 vim_free(buf);
9992 return 0;
9993 }
9994
9995 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009996 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009997 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 regmatch.rm_ic = TRUE; /* Always ignore case */
9999 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010000 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010001 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 vim_free(pat);
10003
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010004 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
10006 vim_free(buf);
10007 return 0;
10008 }
10009
10010 /* remember the pattern or file name being looked for */
10011 matchname = vim_strsave(s);
10012
Bram Moolenaar231334e2005-07-25 20:46:57 +000010013 /* If "**" is by itself, this is the first time we encounter it and more
10014 * is following then find matches without any directory. */
10015 if (!didstar && stardepth < 100 && starstar && e - s == 2
10016 && *path_end == '/')
10017 {
10018 STRCPY(s, path_end + 1);
10019 ++stardepth;
10020 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10021 --stardepth;
10022 }
10023
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 /* Scan all files in the directory with "dir/ *.*" */
10025 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026# ifdef FEAT_MBYTE
10027 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10028 {
10029 /* The active codepage differs from 'encoding'. Attempt using the
10030 * wide function. If it fails because it is not implemented fall back
10031 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010032 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 if (wn != NULL)
10034 {
10035 hFind = FindFirstFileW(wn, &wfb);
10036 if (hFind == INVALID_HANDLE_VALUE
10037 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
10038 {
10039 vim_free(wn);
10040 wn = NULL;
10041 }
10042 }
10043 }
10044
10045 if (wn == NULL)
10046# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010047 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
10050 while (ok)
10051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052# ifdef FEAT_MBYTE
10053 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010054 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 else
10056# endif
10057 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 /* Ignore entries starting with a dot, unless when asked for. Accept
10059 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010060 if ((p[0] != '.' || starts_with_dot
10061 || ((flags & EW_DODOT)
10062 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010064 || (regmatch.regprog != NULL
10065 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010066 || ((flags & EW_NOTWILD)
10067 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010071
10072 if (starstar && stardepth < 100)
10073 {
10074 /* For "**" in the pattern first go deeper in the tree to
10075 * find matches. */
10076 STRCPY(buf + len, "/**");
10077 STRCPY(buf + len + 3, path_end);
10078 ++stardepth;
10079 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10080 --stardepth;
10081 }
10082
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 STRCPY(buf + len, path_end);
10084 if (mch_has_exp_wildcard(path_end))
10085 {
10086 /* need to expand another component of the path */
10087 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010088 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 }
10090 else
10091 {
10092 /* no more wildcards, check if there is a match */
10093 /* remove backslashes for the remaining components only */
10094 if (*path_end != 0)
10095 backslash_halve(buf + len + 1);
10096 if (mch_getperm(buf) >= 0) /* add existing file */
10097 addfile(gap, buf, flags);
10098 }
10099 }
10100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101# ifdef FEAT_MBYTE
10102 if (wn != NULL)
10103 {
10104 vim_free(p);
10105 ok = FindNextFileW(hFind, &wfb);
10106 }
10107 else
10108# endif
10109 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110
10111 /* If no more matches and no match was used, try expanding the name
10112 * itself. Finds the long name of a short filename. */
10113 if (!ok && matchname != NULL && gap->ga_len == start_len)
10114 {
10115 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 FindClose(hFind);
10117# ifdef FEAT_MBYTE
10118 if (wn != NULL)
10119 {
10120 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010121 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 if (wn != NULL)
10123 hFind = FindFirstFileW(wn, &wfb);
10124 }
10125 if (wn == NULL)
10126# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010127 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 vim_free(matchname);
10130 matchname = NULL;
10131 }
10132 }
10133
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 FindClose(hFind);
10135# ifdef FEAT_MBYTE
10136 vim_free(wn);
10137# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010139 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 vim_free(matchname);
10141
10142 matches = gap->ga_len - start_len;
10143 if (matches > 0)
10144 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10145 sizeof(char_u *), pstrcmp);
10146 return matches;
10147}
10148
10149 int
10150mch_expandpath(
10151 garray_T *gap,
10152 char_u *path,
10153 int flags) /* EW_* flags */
10154{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010155 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010157# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158
Bram Moolenaar231334e2005-07-25 20:46:57 +000010159#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10160 || defined(PROTO)
10161/*
10162 * Unix style wildcard expansion code.
10163 * It's here because it's used both for Unix and Mac.
10164 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010165static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010166
10167 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010168pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010169{
10170 return (pathcmp(*(char **)a, *(char **)b, -1));
10171}
10172
10173/*
10174 * Recursively expand one path component into all matching files and/or
10175 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10176 * "path" has backslashes before chars that are not to be expanded, starting
10177 * at "path + wildoff".
10178 * Return the number of matches found.
10179 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10180 */
10181 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010182unix_expandpath(
10183 garray_T *gap,
10184 char_u *path,
10185 int wildoff,
10186 int flags, /* EW_* flags */
10187 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010188{
10189 char_u *buf;
10190 char_u *path_end;
10191 char_u *p, *s, *e;
10192 int start_len = gap->ga_len;
10193 char_u *pat;
10194 regmatch_T regmatch;
10195 int starts_with_dot;
10196 int matches;
10197 int len;
10198 int starstar = FALSE;
10199 static int stardepth = 0; /* depth for "**" expansion */
10200
10201 DIR *dirp;
10202 struct dirent *dp;
10203
10204 /* Expanding "**" may take a long time, check for CTRL-C. */
10205 if (stardepth > 0)
10206 {
10207 ui_breakcheck();
10208 if (got_int)
10209 return 0;
10210 }
10211
10212 /* make room for file name */
10213 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10214 if (buf == NULL)
10215 return 0;
10216
10217 /*
10218 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010219 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010220 * Copy it into "buf", including the preceding characters.
10221 */
10222 p = buf;
10223 s = buf;
10224 e = NULL;
10225 path_end = path;
10226 while (*path_end != NUL)
10227 {
10228 /* May ignore a wildcard that has a backslash before it; it will
10229 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10230 if (path_end >= path + wildoff && rem_backslash(path_end))
10231 *p++ = *path_end++;
10232 else if (*path_end == '/')
10233 {
10234 if (e != NULL)
10235 break;
10236 s = p + 1;
10237 }
10238 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010239 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010240 || (!p_fic && (flags & EW_ICASE)
10241 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010242 e = p;
10243#ifdef FEAT_MBYTE
10244 if (has_mbyte)
10245 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010246 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010247 STRNCPY(p, path_end, len);
10248 p += len;
10249 path_end += len;
10250 }
10251 else
10252#endif
10253 *p++ = *path_end++;
10254 }
10255 e = p;
10256 *e = NUL;
10257
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010258 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010259 /* Remove backslashes between "wildoff" and the start of the wildcard
10260 * component. */
10261 for (p = buf + wildoff; p < s; ++p)
10262 if (rem_backslash(p))
10263 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010264 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010265 --e;
10266 --s;
10267 }
10268
10269 /* Check for "**" between "s" and "e". */
10270 for (p = s; p < e; ++p)
10271 if (p[0] == '*' && p[1] == '*')
10272 starstar = TRUE;
10273
10274 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010275 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010276 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10277 if (pat == NULL)
10278 {
10279 vim_free(buf);
10280 return 0;
10281 }
10282
10283 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010284 if (flags & EW_ICASE)
10285 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10286 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010287 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010288 if (flags & (EW_NOERROR | EW_NOTWILD))
10289 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010290 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010291 if (flags & (EW_NOERROR | EW_NOTWILD))
10292 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010293 vim_free(pat);
10294
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010295 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010296 {
10297 vim_free(buf);
10298 return 0;
10299 }
10300
10301 /* If "**" is by itself, this is the first time we encounter it and more
10302 * is following then find matches without any directory. */
10303 if (!didstar && stardepth < 100 && starstar && e - s == 2
10304 && *path_end == '/')
10305 {
10306 STRCPY(s, path_end + 1);
10307 ++stardepth;
10308 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10309 --stardepth;
10310 }
10311
10312 /* open the directory for scanning */
10313 *s = NUL;
10314 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10315
10316 /* Find all matching entries */
10317 if (dirp != NULL)
10318 {
10319 for (;;)
10320 {
10321 dp = readdir(dirp);
10322 if (dp == NULL)
10323 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010324 if ((dp->d_name[0] != '.' || starts_with_dot
10325 || ((flags & EW_DODOT)
10326 && dp->d_name[1] != NUL
10327 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010328 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10329 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010330 || ((flags & EW_NOTWILD)
10331 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010332 {
10333 STRCPY(s, dp->d_name);
10334 len = STRLEN(buf);
10335
10336 if (starstar && stardepth < 100)
10337 {
10338 /* For "**" in the pattern first go deeper in the tree to
10339 * find matches. */
10340 STRCPY(buf + len, "/**");
10341 STRCPY(buf + len + 3, path_end);
10342 ++stardepth;
10343 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10344 --stardepth;
10345 }
10346
10347 STRCPY(buf + len, path_end);
10348 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10349 {
10350 /* need to expand another component of the path */
10351 /* remove backslashes for the remaining components only */
10352 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10353 }
10354 else
10355 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010356 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010357
Bram Moolenaar231334e2005-07-25 20:46:57 +000010358 /* no more wildcards, check if there is a match */
10359 /* remove backslashes for the remaining components only */
10360 if (*path_end != NUL)
10361 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010362 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010363 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010364 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010365 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010366#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010367 size_t precomp_len = STRLEN(buf)+1;
10368 char_u *precomp_buf =
10369 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010370
Bram Moolenaar231334e2005-07-25 20:46:57 +000010371 if (precomp_buf)
10372 {
10373 mch_memmove(buf, precomp_buf, precomp_len);
10374 vim_free(precomp_buf);
10375 }
10376#endif
10377 addfile(gap, buf, flags);
10378 }
10379 }
10380 }
10381 }
10382
10383 closedir(dirp);
10384 }
10385
10386 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010387 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010388
10389 matches = gap->ga_len - start_len;
10390 if (matches > 0)
10391 qsort(((char_u **)gap->ga_data) + start_len, matches,
10392 sizeof(char_u *), pstrcmp);
10393 return matches;
10394}
10395#endif
10396
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010397#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010398static int find_previous_pathsep(char_u *path, char_u **psep);
10399static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10400static void expand_path_option(char_u *curdir, garray_T *gap);
10401static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10402static void uniquefy_paths(garray_T *gap, char_u *pattern);
10403static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010404
10405/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010406 * Moves "*psep" back to the previous path separator in "path".
10407 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010408 */
10409 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010410find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010411{
10412 /* skip the current separator */
10413 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010414 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010415
10416 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010417 while (*psep > path)
10418 {
10419 if (vim_ispathsep(**psep))
10420 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010421 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010422 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010423
10424 return FAIL;
10425}
10426
10427/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010428 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10429 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010430 */
10431 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010432is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010433{
10434 int j;
10435 int candidate_len;
10436 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010437 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010438 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010439
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010440 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010441 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010442 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010443 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010444
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010445 candidate_len = (int)STRLEN(maybe_unique);
10446 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010447 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010448 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010449
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010450 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010451 if (fnamecmp(maybe_unique, rival) == 0
10452 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010453 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010454 }
10455
Bram Moolenaar162bd912010-07-28 22:29:10 +020010456 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010457}
10458
10459/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010460 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010461 * paths are expanded to their equivalent fullpath. This includes the "."
10462 * (relative to current buffer directory) and empty path (relative to current
10463 * directory) notations.
10464 *
10465 * TODO: handle upward search (;) and path limiter (**N) notations by
10466 * expanding each into their equivalent path(s).
10467 */
10468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010469expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010470{
10471 char_u *path_option = *curbuf->b_p_path == NUL
10472 ? p_path : curbuf->b_p_path;
10473 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010474 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010475 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010476
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010477 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010478 return;
10479
10480 while (*path_option != NUL)
10481 {
10482 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10483
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010484 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010485 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010486 /* Relative to current buffer:
10487 * "/path/file" + "." -> "/path/"
10488 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010489 if (curbuf->b_ffname == NULL)
10490 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010491 p = gettail(curbuf->b_ffname);
10492 len = (int)(p - curbuf->b_ffname);
10493 if (len + (int)STRLEN(buf) >= MAXPATHL)
10494 continue;
10495 if (buf[1] == NUL)
10496 buf[len] = NUL;
10497 else
10498 STRMOVE(buf + len, buf + 2);
10499 mch_memmove(buf, curbuf->b_ffname, len);
10500 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010501 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010502 else if (buf[0] == NUL)
10503 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010504 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010505 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010506 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010507 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010508 else if (!mch_isFullName(buf))
10509 {
10510 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010511 len = (int)STRLEN(curdir);
10512 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010513 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010514 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010515 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010516 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010517 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010518 }
10519
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010520 if (ga_grow(gap, 1) == FAIL)
10521 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010522
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010523# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010524 /* Avoid the path ending in a backslash, it fails when a comma is
10525 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010526 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010527 if (buf[len - 1] == '\\')
10528 buf[len - 1] = '/';
10529# endif
10530
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010531 p = vim_strsave(buf);
10532 if (p == NULL)
10533 break;
10534 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010535 }
10536
10537 vim_free(buf);
10538}
10539
10540/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010541 * Returns a pointer to the file or directory name in "fname" that matches the
10542 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010543 *
10544 * path: /foo/bar/baz
10545 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010546 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010547 */
10548 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010549get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010550{
10551 int i;
10552 int maxlen = 0;
10553 char_u **path_part = (char_u **)gap->ga_data;
10554 char_u *cutoff = NULL;
10555
10556 for (i = 0; i < gap->ga_len; i++)
10557 {
10558 int j = 0;
10559
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010560 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010561# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010562 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10563#endif
10564 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010565 j++;
10566 if (j > maxlen)
10567 {
10568 maxlen = j;
10569 cutoff = &fname[j];
10570 }
10571 }
10572
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010573 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010574 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010575 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010576 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010577
10578 return cutoff;
10579}
10580
10581/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010582 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10583 * that they are unique with respect to each other while conserving the part
10584 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010585 */
10586 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010587uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010588{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010589 int i;
10590 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010591 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010592 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010593 char_u *pat;
10594 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010595 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010596 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010597 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010598 char_u **in_curdir = NULL;
10599 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010600
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010601 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010602 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010603
10604 /*
10605 * We need to prepend a '*' at the beginning of file_pattern so that the
10606 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010607 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010608 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010609 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010610 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010611 if (file_pattern == NULL)
10612 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010613 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010614 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010615 STRCAT(file_pattern, pattern);
10616 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10617 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010618 if (pat == NULL)
10619 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010620
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010621 regmatch.rm_ic = TRUE; /* always ignore case */
10622 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10623 vim_free(pat);
10624 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010625 return;
10626
Bram Moolenaar162bd912010-07-28 22:29:10 +020010627 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010628 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010629 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010630 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010631
10632 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010633 if (in_curdir == NULL)
10634 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010635
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010636 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010637 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010638 char_u *path = fnames[i];
10639 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010640 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010641 char_u *pathsep_p;
10642 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010643
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010644 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010645 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010646 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010647 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010648 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010649
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010650 /* Shorten the filename while maintaining its uniqueness */
10651 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010652
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010653 /* Don't assume all files can be reached without path when search
10654 * pattern starts with star star slash, so only remove path_cutoff
10655 * when possible. */
10656 if (pattern[0] == '*' && pattern[1] == '*'
10657 && vim_ispathsep_nocolon(pattern[2])
10658 && path_cutoff != NULL
10659 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10660 && is_unique(path_cutoff, gap, i))
10661 {
10662 sort_again = TRUE;
10663 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10664 }
10665 else
10666 {
10667 /* Here all files can be reached without path, so get shortest
10668 * unique path. We start at the end of the path. */
10669 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010670
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010671 while (find_previous_pathsep(path, &pathsep_p))
10672 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10673 && is_unique(pathsep_p + 1, gap, i)
10674 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10675 {
10676 sort_again = TRUE;
10677 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10678 break;
10679 }
10680 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010681
10682 if (mch_isFullName(path))
10683 {
10684 /*
10685 * Last resort: shorten relative to curdir if possible.
10686 * 'possible' means:
10687 * 1. It is under the current directory.
10688 * 2. The result is actually shorter than the original.
10689 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010690 * Before curdir After
10691 * /foo/bar/file.txt /foo/bar ./file.txt
10692 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10693 * /file.txt / /file.txt
10694 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010695 */
10696 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010697 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010698#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010699 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010700 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010701 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010702 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010703 && !vim_ispathsep(*short_name)
10704#endif
10705 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010706 {
10707 STRCPY(path, ".");
10708 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010709 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010710 }
10711 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010712 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010713 }
10714
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010715 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010716 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010717 {
10718 char_u *rel_path;
10719 char_u *path = in_curdir[i];
10720
10721 if (path == NULL)
10722 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010723
10724 /* If the {filename} is not unique, change it to ./{filename}.
10725 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010726 short_name = shorten_fname(path, curdir);
10727 if (short_name == NULL)
10728 short_name = path;
10729 if (is_unique(short_name, gap, i))
10730 {
10731 STRCPY(fnames[i], short_name);
10732 continue;
10733 }
10734
10735 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10736 if (rel_path == NULL)
10737 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010738 STRCPY(rel_path, ".");
10739 add_pathsep(rel_path);
10740 STRCAT(rel_path, short_name);
10741
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010742 vim_free(fnames[i]);
10743 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010744 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010745 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010746 }
10747
Bram Moolenaar162bd912010-07-28 22:29:10 +020010748theend:
10749 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010750 if (in_curdir != NULL)
10751 {
10752 for (i = 0; i < gap->ga_len; i++)
10753 vim_free(in_curdir[i]);
10754 vim_free(in_curdir);
10755 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010756 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010757 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010758
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010759 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010760 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010761}
10762
10763/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010764 * Calls globpath() with 'path' values for the given pattern and stores the
10765 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010766 * Returns the total number of matches.
10767 */
10768 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010769expand_in_path(
10770 garray_T *gap,
10771 char_u *pattern,
10772 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010773{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010774 char_u *curdir;
10775 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010776 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010777
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010778 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010779 return 0;
10780 mch_dirname(curdir, MAXPATHL);
10781
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010782 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010783 expand_path_option(curdir, &path_ga);
10784 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010785 if (path_ga.ga_len == 0)
10786 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010787
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010788 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010789 ga_clear_strings(&path_ga);
10790 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010791 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010792
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010793 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010794 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010795
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010796 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010797}
10798#endif
10799
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010800#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10801/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010802 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10803 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010804 */
10805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010806remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010807{
10808 int i;
10809 int j;
10810 char_u **fnames = (char_u **)gap->ga_data;
10811
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010812 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010813 for (i = gap->ga_len - 1; i > 0; --i)
10814 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10815 {
10816 vim_free(fnames[i]);
10817 for (j = i + 1; j < gap->ga_len; ++j)
10818 fnames[j - 1] = fnames[j];
10819 --gap->ga_len;
10820 }
10821}
10822#endif
10823
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010824static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010825
10826/*
10827 * Return TRUE if "p" contains what looks like an environment variable.
10828 * Allowing for escaping.
10829 */
10830 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010831has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010832{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010833 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010834 {
10835 if (*p == '\\' && p[1] != NUL)
10836 ++p;
10837 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010838#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010839 "$%"
10840#else
10841 "$"
10842#endif
10843 , *p) != NULL)
10844 return TRUE;
10845 }
10846 return FALSE;
10847}
10848
10849#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010850static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010851
10852/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010853 * Return TRUE if "p" contains a special wildcard character, one that Vim
10854 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010855 */
10856 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010857has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010858{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010859 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010860 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010861 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010862 if (*p == '\\' && p[1] != NUL)
10863 ++p;
10864 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10865 return TRUE;
10866 }
10867 return FALSE;
10868}
10869#endif
10870
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871/*
10872 * Generic wildcard expansion code.
10873 *
10874 * Characters in "pat" that should not be expanded must be preceded with a
10875 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10876 *
10877 * Return FAIL when no single file was found. In this case "num_file" is not
10878 * set, and "file" may contain an error message.
10879 * Return OK when some files found. "num_file" is set to the number of
10880 * matches, "file" to the array of matches. Call FreeWild() later.
10881 */
10882 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010883gen_expand_wildcards(
10884 int num_pat, /* number of input patterns */
10885 char_u **pat, /* array of input patterns */
10886 int *num_file, /* resulting number of files */
10887 char_u ***file, /* array of resulting files */
10888 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889{
10890 int i;
10891 garray_T ga;
10892 char_u *p;
10893 static int recursive = FALSE;
10894 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010895 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010896#if defined(FEAT_SEARCHPATH)
10897 int did_expand_in_path = FALSE;
10898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899
10900 /*
10901 * expand_env() is called to expand things like "~user". If this fails,
10902 * it calls ExpandOne(), which brings us back here. In this case, always
10903 * call the machine specific expansion function, if possible. Otherwise,
10904 * return FAIL.
10905 */
10906 if (recursive)
10907#ifdef SPECIAL_WILDCHAR
10908 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10909#else
10910 return FAIL;
10911#endif
10912
10913#ifdef SPECIAL_WILDCHAR
10914 /*
10915 * If there are any special wildcard characters which we cannot handle
10916 * here, call machine specific function for all the expansion. This
10917 * avoids starting the shell for each argument separately.
10918 * For `=expr` do use the internal function.
10919 */
10920 for (i = 0; i < num_pat; i++)
10921 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010922 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923# ifdef VIM_BACKTICK
10924 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10925# endif
10926 )
10927 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10928 }
10929#endif
10930
10931 recursive = TRUE;
10932
10933 /*
10934 * The matching file names are stored in a growarray. Init it empty.
10935 */
10936 ga_init2(&ga, (int)sizeof(char_u *), 30);
10937
10938 for (i = 0; i < num_pat; ++i)
10939 {
10940 add_pat = -1;
10941 p = pat[i];
10942
10943#ifdef VIM_BACKTICK
10944 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010947 if (add_pat == -1)
10948 retval = FAIL;
10949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 else
10951#endif
10952 {
10953 /*
10954 * First expand environment variables, "~/" and "~user/".
10955 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010956 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010958 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (p == NULL)
10960 p = pat[i];
10961#ifdef UNIX
10962 /*
10963 * On Unix, if expand_env() can't expand an environment
10964 * variable, use the shell to do that. Discard previously
10965 * found file names and start all over again.
10966 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010967 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 {
10969 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010970 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010972 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 recursive = FALSE;
10974 return i;
10975 }
10976#endif
10977 }
10978
10979 /*
10980 * If there are wildcards: Expand file names and add each match to
10981 * the list. If there is no match, and EW_NOTFOUND is given, add
10982 * the pattern.
10983 * If there are no wildcards: Add the file name if it exists or
10984 * when EW_NOTFOUND is given.
10985 */
10986 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010987 {
10988#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010989 if ((flags & EW_PATH)
10990 && !mch_isFullName(p)
10991 && !(p[0] == '.'
10992 && (vim_ispathsep(p[1])
10993 || (p[1] == '.' && vim_ispathsep(p[2]))))
10994 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010995 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010996 /* :find completion where 'path' is used.
10997 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010998 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010999 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011000 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011001 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011002 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011003 else
11004#endif
11005 add_pat = mch_expandpath(&ga, p, flags);
11006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 }
11008
11009 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11010 {
11011 char_u *t = backslash_halve_save(p);
11012
11013#if defined(MACOS_CLASSIC)
11014 slash_to_colon(t);
11015#endif
11016 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11017 * "vim c:/" work. */
11018 if (flags & EW_NOTFOUND)
11019 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 addfile(&ga, t, flags);
11022 vim_free(t);
11023 }
11024
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011025#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011026 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011027 uniquefy_paths(&ga, p);
11028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 if (p != pat[i])
11030 vim_free(p);
11031 }
11032
11033 *num_file = ga.ga_len;
11034 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11035
11036 recursive = FALSE;
11037
Bram Moolenaar336bd622016-01-17 18:23:58 +010011038 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039}
11040
11041# ifdef VIM_BACKTICK
11042
11043/*
11044 * Return TRUE if we can expand this backtick thing here.
11045 */
11046 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011047vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048{
11049 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11050}
11051
11052/*
11053 * Expand an item in `backticks` by executing it as a command.
11054 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011055 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 */
11057 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011058expand_backtick(
11059 garray_T *gap,
11060 char_u *pat,
11061 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062{
11063 char_u *p;
11064 char_u *cmd;
11065 char_u *buffer;
11066 int cnt = 0;
11067 int i;
11068
11069 /* Create the command: lop off the backticks. */
11070 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11071 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011072 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073
11074#ifdef FEAT_EVAL
11075 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011076 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 else
11078#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011079 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011080 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 vim_free(cmd);
11082 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011083 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084
11085 cmd = buffer;
11086 while (*cmd != NUL)
11087 {
11088 cmd = skipwhite(cmd); /* skip over white space */
11089 p = cmd;
11090 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11091 ++p;
11092 /* add an entry if it is not empty */
11093 if (p > cmd)
11094 {
11095 i = *p;
11096 *p = NUL;
11097 addfile(gap, cmd, flags);
11098 *p = i;
11099 ++cnt;
11100 }
11101 cmd = p;
11102 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11103 ++cmd;
11104 }
11105
11106 vim_free(buffer);
11107 return cnt;
11108}
11109# endif /* VIM_BACKTICK */
11110
11111/*
11112 * Add a file to a file list. Accepted flags:
11113 * EW_DIR add directories
11114 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011115 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 * EW_NOTFOUND add even when it doesn't exist
11117 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011118 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 */
11120 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011121addfile(
11122 garray_T *gap,
11123 char_u *f, /* filename */
11124 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125{
11126 char_u *p;
11127 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011128 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011130 /* if the file/dir/link doesn't exist, may not add it */
11131 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011132 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 return;
11134
11135#ifdef FNAME_ILLEGAL
11136 /* if the file/dir contains illegal characters, don't add it */
11137 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11138 return;
11139#endif
11140
11141 isdir = mch_isdir(f);
11142 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11143 return;
11144
Bram Moolenaarb5971142015-03-21 17:32:19 +010011145 /* If the file isn't executable, may not add it. Do accept directories.
11146 * When invoked from expand_shellcmd() do not use $PATH. */
11147 if (!isdir && (flags & EW_EXEC)
11148 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011149 return;
11150
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 /* Make room for another item in the file list. */
11152 if (ga_grow(gap, 1) == FAIL)
11153 return;
11154
11155 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11156 if (p == NULL)
11157 return;
11158
11159 STRCPY(p, f);
11160#ifdef BACKSLASH_IN_FILENAME
11161 slash_adjust(p);
11162#endif
11163 /*
11164 * Append a slash or backslash after directory names if none is present.
11165 */
11166#ifndef DONT_ADD_PATHSEP_TO_DIR
11167 if (isdir && (flags & EW_ADDSLASH))
11168 add_pathsep(p);
11169#endif
11170 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171}
11172#endif /* !NO_EXPANDPATH */
11173
11174#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11175
11176#ifndef SEEK_SET
11177# define SEEK_SET 0
11178#endif
11179#ifndef SEEK_END
11180# define SEEK_END 2
11181#endif
11182
11183/*
11184 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011185 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11186 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 * Returns an allocated string, or NULL for error.
11188 */
11189 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011190get_cmd_output(
11191 char_u *cmd,
11192 char_u *infile, /* optional input file name */
11193 int flags, /* can be SHELL_SILENT */
11194 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195{
11196 char_u *tempname;
11197 char_u *command;
11198 char_u *buffer = NULL;
11199 int len;
11200 int i = 0;
11201 FILE *fd;
11202
11203 if (check_restricted() || check_secure())
11204 return NULL;
11205
11206 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011207 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 {
11209 EMSG(_(e_notmp));
11210 return NULL;
11211 }
11212
11213 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011214 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 if (command == NULL)
11216 goto done;
11217
11218 /*
11219 * Call the shell to execute the command (errors are ignored).
11220 * Don't check timestamps here.
11221 */
11222 ++no_check_timestamps;
11223 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11224 --no_check_timestamps;
11225
11226 vim_free(command);
11227
11228 /*
11229 * read the names from the file into memory
11230 */
11231# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011232 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 fd = mch_fopen((char *)tempname, "r");
11234# else
11235 fd = mch_fopen((char *)tempname, READBIN);
11236# endif
11237
11238 if (fd == NULL)
11239 {
11240 EMSG2(_(e_notopen), tempname);
11241 goto done;
11242 }
11243
11244 fseek(fd, 0L, SEEK_END);
11245 len = ftell(fd); /* get size of temp file */
11246 fseek(fd, 0L, SEEK_SET);
11247
11248 buffer = alloc(len + 1);
11249 if (buffer != NULL)
11250 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11251 fclose(fd);
11252 mch_remove(tempname);
11253 if (buffer == NULL)
11254 goto done;
11255#ifdef VMS
11256 len = i; /* VMS doesn't give us what we asked for... */
11257#endif
11258 if (i != len)
11259 {
11260 EMSG2(_(e_notread), tempname);
11261 vim_free(buffer);
11262 buffer = NULL;
11263 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011264 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011265 {
11266 /* Change NUL into SOH, otherwise the string is truncated. */
11267 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011268 if (buffer[i] == NUL)
11269 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011270
Bram Moolenaar162bd912010-07-28 22:29:10 +020011271 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011272 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011273 else
11274 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275
11276done:
11277 vim_free(tempname);
11278 return buffer;
11279}
11280#endif
11281
11282/*
11283 * Free the list of files returned by expand_wildcards() or other expansion
11284 * functions.
11285 */
11286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011287FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011289 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291 while (count--)
11292 vim_free(files[count]);
11293 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294}
11295
11296/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011297 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 * Don't do this when still processing a command or a mapping.
11299 * Don't do this when inside a ":normal" command.
11300 */
11301 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011302goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303{
11304 return (p_im && stuff_empty() && typebuf_typed());
11305}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011306
11307/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011308 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011309 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11310 * - Remove any argument. E.g., "csh -f" -> "csh".
11311 * But don't allow a space in the path, so that this works:
11312 * "/usr/bin/csh --rcfile ~/.cshrc"
11313 * But don't do that for Windows, it's common to have a space in the path.
11314 */
11315 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011316get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011317{
11318 char_u *p;
11319
11320#ifdef WIN3264
11321 p = gettail(p_sh);
11322 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11323#else
11324 p = skiptowhite(p_sh);
11325 if (*p == NUL)
11326 {
11327 /* No white space, use the tail. */
11328 p = vim_strsave(gettail(p_sh));
11329 }
11330 else
11331 {
11332 char_u *p1, *p2;
11333
11334 /* Find the last path separator before the space. */
11335 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011336 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011337 if (vim_ispathsep(*p2))
11338 p1 = p2 + 1;
11339 p = vim_strnsave(p1, (int)(p - p1));
11340 }
11341#endif
11342 return p;
11343}