blob: abc455e951a199eca6bb1dd33ed65ed0043b12b5 [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);
1592 p = ml_get_curline();
1593 ai_col = (colnr_T)(skipwhite(p) - p);
1594 }
1595#endif
1596#ifdef FEAT_CINDENT
1597 /*
1598 * May do indenting after opening a new line.
1599 */
1600 if (!p_paste
1601 && (curbuf->b_p_cin
1602# ifdef FEAT_EVAL
1603 || *curbuf->b_p_inde != NUL
1604# endif
1605 )
1606 && in_cinkeys(dir == FORWARD
1607 ? KEY_OPEN_FORW
1608 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1609 {
1610 do_c_expr_indent();
1611 p = ml_get_curline();
1612 ai_col = (colnr_T)(skipwhite(p) - p);
1613 }
1614#endif
1615#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1616 if (vreplace_mode != 0)
1617 State = vreplace_mode;
1618#endif
1619
1620#ifdef FEAT_VREPLACE
1621 /*
1622 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1623 * original line, and inserts the new stuff char by char, pushing old stuff
1624 * onto the replace stack (via ins_char()).
1625 */
1626 if (State & VREPLACE_FLAG)
1627 {
1628 /* Put new line in p_extra */
1629 p_extra = vim_strsave(ml_get_curline());
1630 if (p_extra == NULL)
1631 goto theend;
1632
1633 /* Put back original line */
1634 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1635
1636 /* Insert new stuff into line again */
1637 curwin->w_cursor.col = 0;
1638#ifdef FEAT_VIRTUALEDIT
1639 curwin->w_cursor.coladd = 0;
1640#endif
1641 ins_bytes(p_extra); /* will call changed_bytes() */
1642 vim_free(p_extra);
1643 next_line = NULL;
1644 }
1645#endif
1646
1647 retval = TRUE; /* success! */
1648theend:
1649 curbuf->b_p_pi = saved_pi;
1650 vim_free(saved_line);
1651 vim_free(next_line);
1652 vim_free(allocated);
1653 return retval;
1654}
1655
1656#if defined(FEAT_COMMENTS) || defined(PROTO)
1657/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001658 * get_leader_len() returns the length in bytes of the prefix of the given
1659 * string which introduces a comment. If this string is not a comment then
1660 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 * When "flags" is not NULL, it is set to point to the flags of the recognized
1662 * comment leader.
1663 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001664 * If "include_space" is set, include trailing whitespace while calculating the
1665 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 */
1667 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001668get_leader_len(
1669 char_u *line,
1670 char_u **flags,
1671 int backward,
1672 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001675 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 int got_com = FALSE;
1677 int found_one;
1678 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1679 char_u *string; /* pointer to comment string */
1680 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001681 int middle_match_len = 0;
1682 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001683 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar81340392012-06-06 16:12:59 +02001685 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001686 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 ++i;
1688
1689 /*
1690 * Repeat to match several nested comment strings.
1691 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001692 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 /*
1695 * scan through the 'comments' option for a match
1696 */
1697 found_one = FALSE;
1698 for (list = curbuf->b_p_com; *list; )
1699 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001700 /* Get one option part into part_buf[]. Advance "list" to next
1701 * one. Put "string" at start of string. */
1702 if (!got_com && flags != NULL)
1703 *flags = list; /* remember where flags started */
1704 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1706 string = vim_strchr(part_buf, ':');
1707 if (string == NULL) /* missing ':', ignore this part */
1708 continue;
1709 *string++ = NUL; /* isolate flags from string */
1710
Bram Moolenaara4271d52011-05-10 13:38:27 +02001711 /* If we found a middle match previously, use that match when this
1712 * is not a middle or end. */
1713 if (middle_match_len != 0
1714 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1715 && vim_strchr(part_buf, COM_END) == NULL)
1716 break;
1717
1718 /* When we already found a nested comment, only accept further
1719 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1721 continue;
1722
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1725 continue;
1726
Bram Moolenaara4271d52011-05-10 13:38:27 +02001727 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 * When string starts with white space, must have some white space
1729 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001730 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001731 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001733 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001734 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001735 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 ++string;
1737 }
1738 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1739 ;
1740 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001741 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaara4271d52011-05-10 13:38:27 +02001743 /* When 'b' flag used, there must be white space or an
1744 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001746 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 continue;
1748
Bram Moolenaara4271d52011-05-10 13:38:27 +02001749 /* We have found a match, stop searching unless this is a middle
1750 * comment. The middle comment can be a substring of the end
1751 * comment in which case it's better to return the length of the
1752 * end comment and its flags. Thus we keep searching with middle
1753 * and end matches and use an end match if it matches better. */
1754 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1755 {
1756 if (middle_match_len == 0)
1757 {
1758 middle_match_len = j;
1759 saved_flags = prev_list;
1760 }
1761 continue;
1762 }
1763 if (middle_match_len != 0 && j > middle_match_len)
1764 /* Use this match instead of the middle match, since it's a
1765 * longer thus better match. */
1766 middle_match_len = 0;
1767
1768 if (middle_match_len == 0)
1769 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 found_one = TRUE;
1771 break;
1772 }
1773
Bram Moolenaara4271d52011-05-10 13:38:27 +02001774 if (middle_match_len != 0)
1775 {
1776 /* Use the previously found middle match after failing to find a
1777 * match with an end. */
1778 if (!got_com && flags != NULL)
1779 *flags = saved_flags;
1780 i += middle_match_len;
1781 found_one = TRUE;
1782 }
1783
1784 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (!found_one)
1786 break;
1787
Bram Moolenaar81340392012-06-06 16:12:59 +02001788 result = i;
1789
Bram Moolenaara4271d52011-05-10 13:38:27 +02001790 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001791 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 ++i;
1793
Bram Moolenaar81340392012-06-06 16:12:59 +02001794 if (include_space)
1795 result = i;
1796
Bram Moolenaara4271d52011-05-10 13:38:27 +02001797 /* If this comment doesn't nest, stop here. */
1798 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (vim_strchr(part_buf, COM_NEST) == NULL)
1800 break;
1801 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001802 return result;
1803}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001804
Bram Moolenaar81340392012-06-06 16:12:59 +02001805/*
1806 * Return the offset at which the last comment in line starts. If there is no
1807 * comment in the whole line, -1 is returned.
1808 *
1809 * When "flags" is not null, it is set to point to the flags describing the
1810 * recognized comment leader.
1811 */
1812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001813get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001814{
1815 int result = -1;
1816 int i, j;
1817 int lower_check_bound = 0;
1818 char_u *string;
1819 char_u *com_leader;
1820 char_u *com_flags;
1821 char_u *list;
1822 int found_one;
1823 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1824
1825 /*
1826 * Repeat to match several nested comment strings.
1827 */
1828 i = (int)STRLEN(line);
1829 while (--i >= lower_check_bound)
1830 {
1831 /*
1832 * scan through the 'comments' option for a match
1833 */
1834 found_one = FALSE;
1835 for (list = curbuf->b_p_com; *list; )
1836 {
1837 char_u *flags_save = list;
1838
1839 /*
1840 * Get one option part into part_buf[]. Advance list to next one.
1841 * put string at start of string.
1842 */
1843 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1844 string = vim_strchr(part_buf, ':');
1845 if (string == NULL) /* If everything is fine, this cannot actually
1846 * happen. */
1847 {
1848 continue;
1849 }
1850 *string++ = NUL; /* Isolate flags from string. */
1851 com_leader = string;
1852
1853 /*
1854 * Line contents and string must match.
1855 * When string starts with white space, must have some white space
1856 * (but the amount does not need to match, there might be a mix of
1857 * TABs and spaces).
1858 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001859 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001860 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001861 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001863 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001864 ++string;
1865 }
1866 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1867 /* do nothing */;
1868 if (string[j] != NUL)
1869 continue;
1870
1871 /*
1872 * When 'b' flag used, there must be white space or an
1873 * end-of-line after the string in the line.
1874 */
1875 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001876 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02001877 {
1878 continue;
1879 }
1880
1881 /*
1882 * We have found a match, stop searching.
1883 */
1884 found_one = TRUE;
1885
1886 if (flags)
1887 *flags = flags_save;
1888 com_flags = flags_save;
1889
1890 break;
1891 }
1892
1893 if (found_one)
1894 {
1895 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1896 int len1, len2, off;
1897
1898 result = i;
1899 /*
1900 * If this comment nests, continue searching.
1901 */
1902 if (vim_strchr(part_buf, COM_NEST) != NULL)
1903 continue;
1904
1905 lower_check_bound = i;
1906
1907 /* Let's verify whether the comment leader found is a substring
1908 * of other comment leaders. If it is, let's adjust the
1909 * lower_check_bound so that we make sure that we have determined
1910 * the comment leader correctly.
1911 */
1912
Bram Moolenaar1c465442017-03-12 20:10:05 +01001913 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02001914 ++com_leader;
1915 len1 = (int)STRLEN(com_leader);
1916
1917 for (list = curbuf->b_p_com; *list; )
1918 {
1919 char_u *flags_save = list;
1920
1921 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1922 if (flags_save == com_flags)
1923 continue;
1924 string = vim_strchr(part_buf2, ':');
1925 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001926 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02001927 ++string;
1928 len2 = (int)STRLEN(string);
1929 if (len2 == 0)
1930 continue;
1931
1932 /* Now we have to verify whether string ends with a substring
1933 * beginning the com_leader. */
1934 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1935 {
1936 --off;
1937 if (!STRNCMP(string + off, com_leader, len2 - off))
1938 {
1939 if (i - off < lower_check_bound)
1940 lower_check_bound = i - off;
1941 }
1942 }
1943 }
1944 }
1945 }
1946 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947}
1948#endif
1949
1950/*
1951 * Return the number of window lines occupied by buffer line "lnum".
1952 */
1953 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001954plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
1956 return plines_win(curwin, lnum, TRUE);
1957}
1958
1959 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001960plines_win(
1961 win_T *wp,
1962 linenr_T lnum,
1963 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
1965#if defined(FEAT_DIFF) || defined(PROTO)
1966 /* Check for filler lines above this buffer line. When folded the result
1967 * is one line anyway. */
1968 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1969}
1970
1971 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001972plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974 return plines_win_nofill(curwin, lnum, TRUE);
1975}
1976
1977 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001978plines_win_nofill(
1979 win_T *wp,
1980 linenr_T lnum,
1981 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
1983#endif
1984 int lines;
1985
1986 if (!wp->w_p_wrap)
1987 return 1;
1988
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001989#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (wp->w_width == 0)
1991 return 1;
1992#endif
1993
1994#ifdef FEAT_FOLDING
1995 /* A folded lines is handled just like an empty line. */
1996 /* NOTE: Caller must handle lines that are MAYBE folded. */
1997 if (lineFolded(wp, lnum) == TRUE)
1998 return 1;
1999#endif
2000
2001 lines = plines_win_nofold(wp, lnum);
2002 if (winheight > 0 && lines > wp->w_height)
2003 return (int)wp->w_height;
2004 return lines;
2005}
2006
2007/*
2008 * Return number of window lines physical line "lnum" will occupy in window
2009 * "wp". Does not care about folding, 'wrap' or 'diff'.
2010 */
2011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 char_u *s;
2015 long col;
2016 int width;
2017
2018 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2019 if (*s == NUL) /* empty line */
2020 return 1;
2021 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2022
2023 /*
2024 * If list mode is on, then the '$' at the end of the line may take up one
2025 * extra column.
2026 */
2027 if (wp->w_p_list && lcs_eol != NUL)
2028 col += 1;
2029
2030 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002031 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
2033 width = W_WIDTH(wp) - win_col_off(wp);
2034 if (width <= 0)
2035 return 32000;
2036 if (col <= width)
2037 return 1;
2038 col -= width;
2039 width += win_col_off2(wp);
2040 return (col + (width - 1)) / width + 1;
2041}
2042
2043/*
2044 * Like plines_win(), but only reports the number of physical screen lines
2045 * used from the start of the line to the given column number.
2046 */
2047 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002048plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 long col;
2051 char_u *s;
2052 int lines = 0;
2053 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002054 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056#ifdef FEAT_DIFF
2057 /* Check for filler lines above this buffer line. When folded the result
2058 * is one line anyway. */
2059 lines = diff_check_fill(wp, lnum);
2060#endif
2061
2062 if (!wp->w_p_wrap)
2063 return lines + 1;
2064
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002065#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (wp->w_width == 0)
2067 return lines + 1;
2068#endif
2069
Bram Moolenaar597a4222014-06-25 14:39:50 +02002070 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 col = 0;
2073 while (*s != NUL && --column >= 0)
2074 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002075 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002076 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078
2079 /*
2080 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2081 * INSERT mode, then col must be adjusted so that it represents the last
2082 * screen position of the TAB. This only fixes an error when the TAB wraps
2083 * from one screen line to the next (when 'columns' is not a multiple of
2084 * 'ts') -- webb.
2085 */
2086 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002087 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002090 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 */
2092 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002093 if (width <= 0)
2094 return 9999;
2095
2096 lines += 1;
2097 if (col > width)
2098 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2099 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100}
2101
2102 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002103plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 int count = 0;
2106
2107 while (first <= last)
2108 {
2109#ifdef FEAT_FOLDING
2110 int x;
2111
2112 /* Check if there are any really folded lines, but also included lines
2113 * that are maybe folded. */
2114 x = foldedCount(wp, first, NULL);
2115 if (x > 0)
2116 {
2117 ++count; /* count 1 for "+-- folded" line */
2118 first += x;
2119 }
2120 else
2121#endif
2122 {
2123#ifdef FEAT_DIFF
2124 if (first == wp->w_topline)
2125 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2126 else
2127#endif
2128 count += plines_win(wp, first, TRUE);
2129 ++first;
2130 }
2131 }
2132 return (count);
2133}
2134
2135#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2136/*
2137 * Insert string "p" at the cursor position. Stops at a NUL byte.
2138 * Handles Replace mode and multi-byte characters.
2139 */
2140 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002141ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 ins_bytes_len(p, (int)STRLEN(p));
2144}
2145#endif
2146
2147#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2148 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2149/*
2150 * Insert string "p" with length "len" at the cursor position.
2151 * Handles Replace mode and multi-byte characters.
2152 */
2153 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002154ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155{
2156 int i;
2157# ifdef FEAT_MBYTE
2158 int n;
2159
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002160 if (has_mbyte)
2161 for (i = 0; i < len; i += n)
2162 {
2163 if (enc_utf8)
2164 /* avoid reading past p[len] */
2165 n = utfc_ptr2len_len(p + i, len - i);
2166 else
2167 n = (*mb_ptr2len)(p + i);
2168 ins_char_bytes(p + i, n);
2169 }
2170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002172 for (i = 0; i < len; ++i)
2173 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174}
2175#endif
2176
2177/*
2178 * Insert or replace a single character at the cursor position.
2179 * When in REPLACE or VREPLACE mode, replace any existing character.
2180 * Caller must have prepared for undo.
2181 * For multi-byte characters we get the whole character, the caller must
2182 * convert bytes to a character.
2183 */
2184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002185ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002187 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002188 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002190#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 n = (*mb_char2bytes)(c, buf);
2192
2193 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2194 * Happens for CTRL-Vu9900. */
2195 if (buf[0] == 0)
2196 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002197#else
2198 buf[0] = c;
2199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201 ins_char_bytes(buf, n);
2202}
2203
2204 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002205ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
2207 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 int newlen; /* nr of bytes inserted */
2209 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2210 char_u *p;
2211 char_u *newp;
2212 char_u *oldp;
2213 int linelen; /* length of old line including NUL */
2214 colnr_T col;
2215 linenr_T lnum = curwin->w_cursor.lnum;
2216 int i;
2217
2218#ifdef FEAT_VIRTUALEDIT
2219 /* Break tabs if needed. */
2220 if (virtual_active() && curwin->w_cursor.coladd > 0)
2221 coladvance_force(getviscol());
2222#endif
2223
2224 col = curwin->w_cursor.col;
2225 oldp = ml_get(lnum);
2226 linelen = (int)STRLEN(oldp) + 1;
2227
2228 /* The lengths default to the values for when not replacing. */
2229 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 if (State & REPLACE_FLAG)
2233 {
2234#ifdef FEAT_VREPLACE
2235 if (State & VREPLACE_FLAG)
2236 {
2237 colnr_T new_vcol = 0; /* init for GCC */
2238 colnr_T vcol;
2239 int old_list;
2240#ifndef FEAT_MBYTE
2241 char_u buf[2];
2242#endif
2243
2244 /*
2245 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2246 * Returns the old value of list, so when finished,
2247 * curwin->w_p_list should be set back to this.
2248 */
2249 old_list = curwin->w_p_list;
2250 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2251 curwin->w_p_list = FALSE;
2252
2253 /*
2254 * In virtual replace mode each character may replace one or more
2255 * characters (zero if it's a TAB). Count the number of bytes to
2256 * be deleted to make room for the new character, counting screen
2257 * cells. May result in adding spaces to fill a gap.
2258 */
2259 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2260#ifndef FEAT_MBYTE
2261 buf[0] = c;
2262 buf[1] = NUL;
2263#endif
2264 new_vcol = vcol + chartabsize(buf, vcol);
2265 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2266 {
2267 vcol += chartabsize(oldp + col + oldlen, vcol);
2268 /* Don't need to remove a TAB that takes us to the right
2269 * position. */
2270 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2271 break;
2272#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002273 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#else
2275 ++oldlen;
2276#endif
2277 /* Deleted a bit too much, insert spaces. */
2278 if (vcol > new_vcol)
2279 newlen += vcol - new_vcol;
2280 }
2281 curwin->w_p_list = old_list;
2282 }
2283 else
2284#endif
2285 if (oldp[col] != NUL)
2286 {
2287 /* normal replace */
2288#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002289 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#else
2291 oldlen = 1;
2292#endif
2293 }
2294
2295
2296 /* Push the replaced bytes onto the replace stack, so that they can be
2297 * put back when BS is used. The bytes of a multi-byte character are
2298 * done the other way around, so that the first byte is popped off
2299 * first (it tells the byte length of the character). */
2300 replace_push(NUL);
2301 for (i = 0; i < oldlen; ++i)
2302 {
2303#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002304 if (has_mbyte)
2305 i += replace_push_mb(oldp + col + i) - 1;
2306 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002308 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310 }
2311
2312 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2313 if (newp == NULL)
2314 return;
2315
2316 /* Copy bytes before the cursor. */
2317 if (col > 0)
2318 mch_memmove(newp, oldp, (size_t)col);
2319
2320 /* Copy bytes after the changed character(s). */
2321 p = newp + col;
2322 mch_memmove(p + newlen, oldp + col + oldlen,
2323 (size_t)(linelen - col - oldlen));
2324
2325 /* Insert or overwrite the new character. */
2326#ifdef FEAT_MBYTE
2327 mch_memmove(p, buf, charlen);
2328 i = charlen;
2329#else
2330 *p = c;
2331 i = 1;
2332#endif
2333
2334 /* Fill with spaces when necessary. */
2335 while (i < newlen)
2336 p[i++] = ' ';
2337
2338 /* Replace the line in the buffer. */
2339 ml_replace(lnum, newp, FALSE);
2340
2341 /* mark the buffer as changed and prepare for displaying */
2342 changed_bytes(lnum, col);
2343
2344 /*
2345 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2346 * show the match for right parens and braces.
2347 */
2348 if (p_sm && (State & INSERT)
2349 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002350#ifdef FEAT_INS_EXPAND
2351 && !ins_compl_active()
2352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002354 {
2355#ifdef FEAT_MBYTE
2356 if (has_mbyte)
2357 showmatch(mb_ptr2char(buf));
2358 else
2359#endif
2360 showmatch(c);
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363#ifdef FEAT_RIGHTLEFT
2364 if (!p_ri || (State & REPLACE_FLAG))
2365#endif
2366 {
2367 /* Normal insert: move cursor right */
2368#ifdef FEAT_MBYTE
2369 curwin->w_cursor.col += charlen;
2370#else
2371 ++curwin->w_cursor.col;
2372#endif
2373 }
2374 /*
2375 * TODO: should try to update w_row here, to avoid recomputing it later.
2376 */
2377}
2378
2379/*
2380 * Insert a string at the cursor position.
2381 * Note: Does NOT handle Replace mode.
2382 * Caller must have prepared for undo.
2383 */
2384 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002385ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
2387 char_u *oldp, *newp;
2388 int newlen = (int)STRLEN(s);
2389 int oldlen;
2390 colnr_T col;
2391 linenr_T lnum = curwin->w_cursor.lnum;
2392
2393#ifdef FEAT_VIRTUALEDIT
2394 if (virtual_active() && curwin->w_cursor.coladd > 0)
2395 coladvance_force(getviscol());
2396#endif
2397
2398 col = curwin->w_cursor.col;
2399 oldp = ml_get(lnum);
2400 oldlen = (int)STRLEN(oldp);
2401
2402 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2403 if (newp == NULL)
2404 return;
2405 if (col > 0)
2406 mch_memmove(newp, oldp, (size_t)col);
2407 mch_memmove(newp + col, s, (size_t)newlen);
2408 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2409 ml_replace(lnum, newp, FALSE);
2410 changed_bytes(lnum, col);
2411 curwin->w_cursor.col += newlen;
2412}
2413
2414/*
2415 * Delete one character under the cursor.
2416 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2417 * Caller must have prepared for undo.
2418 *
2419 * return FAIL for failure, OK otherwise
2420 */
2421 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002422del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424#ifdef FEAT_MBYTE
2425 if (has_mbyte)
2426 {
2427 /* Make sure the cursor is at the start of a character. */
2428 mb_adjust_cursor();
2429 if (*ml_get_cursor() == NUL)
2430 return FAIL;
2431 return del_chars(1L, fixpos);
2432 }
2433#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002434 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435}
2436
2437#if defined(FEAT_MBYTE) || defined(PROTO)
2438/*
2439 * Like del_bytes(), but delete characters instead of bytes.
2440 */
2441 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002442del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443{
2444 long bytes = 0;
2445 long i;
2446 char_u *p;
2447 int l;
2448
2449 p = ml_get_cursor();
2450 for (i = 0; i < count && *p != NUL; ++i)
2451 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002452 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 bytes += l;
2454 p += l;
2455 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002456 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457}
2458#endif
2459
2460/*
2461 * Delete "count" bytes under the cursor.
2462 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2463 * Caller must have prepared for undo.
2464 *
2465 * return FAIL for failure, OK otherwise
2466 */
2467 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002468del_bytes(
2469 long count,
2470 int fixpos_arg,
2471 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
2473 char_u *oldp, *newp;
2474 colnr_T oldlen;
2475 linenr_T lnum = curwin->w_cursor.lnum;
2476 colnr_T col = curwin->w_cursor.col;
2477 int was_alloced;
2478 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002479 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 oldp = ml_get(lnum);
2482 oldlen = (int)STRLEN(oldp);
2483
2484 /*
2485 * Can't do anything when the cursor is on the NUL after the line.
2486 */
2487 if (col >= oldlen)
2488 return FAIL;
2489
2490#ifdef FEAT_MBYTE
2491 /* If 'delcombine' is set and deleting (less than) one character, only
2492 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002493 if (p_deco && use_delcombine && enc_utf8
2494 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002496 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 int n;
2498
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002499 (void)utfc_ptr2char(oldp + col, cc);
2500 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
2502 /* Find the last composing char, there can be several. */
2503 n = col;
2504 do
2505 {
2506 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002507 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 n += count;
2509 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2510 fixpos = 0;
2511 }
2512 }
2513#endif
2514
2515 /*
2516 * When count is too big, reduce it.
2517 */
2518 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2519 if (movelen <= 1)
2520 {
2521 /*
2522 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002523 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2524 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002526 if (col > 0 && fixpos && restart_edit == 0
2527#ifdef FEAT_VIRTUALEDIT
2528 && (ve_flags & VE_ONEMORE) == 0
2529#endif
2530 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 --curwin->w_cursor.col;
2533#ifdef FEAT_VIRTUALEDIT
2534 curwin->w_cursor.coladd = 0;
2535#endif
2536#ifdef FEAT_MBYTE
2537 if (has_mbyte)
2538 curwin->w_cursor.col -=
2539 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2540#endif
2541 }
2542 count = oldlen - col;
2543 movelen = 1;
2544 }
2545
2546 /*
2547 * If the old line has been allocated the deletion can be done in the
2548 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002549 * Can't do this when using Netbeans, because we would need to invoke
2550 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002551 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002554 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002555 was_alloced = FALSE;
2556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002558 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (was_alloced)
2560 newp = oldp; /* use same allocated memory */
2561 else
2562 { /* need to allocate a new line */
2563 newp = alloc((unsigned)(oldlen + 1 - count));
2564 if (newp == NULL)
2565 return FAIL;
2566 mch_memmove(newp, oldp, (size_t)col);
2567 }
2568 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2569 if (!was_alloced)
2570 ml_replace(lnum, newp, FALSE);
2571
2572 /* mark the buffer as changed and prepare for displaying */
2573 changed_bytes(lnum, curwin->w_cursor.col);
2574
2575 return OK;
2576}
2577
2578/*
2579 * Delete from cursor to end of line.
2580 * Caller must have prepared for undo.
2581 *
2582 * return FAIL for failure, OK otherwise
2583 */
2584 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002585truncate_line(
2586 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
2588 char_u *newp;
2589 linenr_T lnum = curwin->w_cursor.lnum;
2590 colnr_T col = curwin->w_cursor.col;
2591
2592 if (col == 0)
2593 newp = vim_strsave((char_u *)"");
2594 else
2595 newp = vim_strnsave(ml_get(lnum), col);
2596
2597 if (newp == NULL)
2598 return FAIL;
2599
2600 ml_replace(lnum, newp, FALSE);
2601
2602 /* mark the buffer as changed and prepare for displaying */
2603 changed_bytes(lnum, curwin->w_cursor.col);
2604
2605 /*
2606 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2607 */
2608 if (fixpos && curwin->w_cursor.col > 0)
2609 --curwin->w_cursor.col;
2610
2611 return OK;
2612}
2613
2614/*
2615 * Delete "nlines" lines at the cursor.
2616 * Saves the lines for undo first if "undo" is TRUE.
2617 */
2618 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002619del_lines(
2620 long nlines, /* number of lines to delete */
2621 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622{
2623 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002624 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626 if (nlines <= 0)
2627 return;
2628
2629 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002630 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 return;
2632
2633 for (n = 0; n < nlines; )
2634 {
2635 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2636 break;
2637
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002638 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ++n;
2640
2641 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002642 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 break;
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002646 /* Correct the cursor position before calling deleted_lines_mark(), it may
2647 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 curwin->w_cursor.col = 0;
2649 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002650
2651 /* adjust marks, mark the buffer as changed and prepare for displaying */
2652 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653}
2654
2655 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002656gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
2658 char_u *ptr = ml_get_pos(pos);
2659
2660#ifdef FEAT_MBYTE
2661 if (has_mbyte)
2662 return (*mb_ptr2char)(ptr);
2663#endif
2664 return (int)*ptr;
2665}
2666
2667 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002668gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
2670#ifdef FEAT_MBYTE
2671 if (has_mbyte)
2672 return (*mb_ptr2char)(ml_get_cursor());
2673#endif
2674 return (int)*ml_get_cursor();
2675}
2676
2677/*
2678 * Write a character at the current cursor position.
2679 * It is directly written into the block.
2680 */
2681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002682pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683{
2684 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2685 + curwin->w_cursor.col) = c;
2686}
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688/*
2689 * When extra == 0: Return TRUE if the cursor is before or on the first
2690 * non-blank in the line.
2691 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2692 * the line.
2693 */
2694 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002695inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 char_u *ptr;
2698 colnr_T col;
2699
Bram Moolenaar1c465442017-03-12 20:10:05 +01002700 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 ++ptr;
2702 if (col >= curwin->w_cursor.col + extra)
2703 return TRUE;
2704 else
2705 return FALSE;
2706}
2707
2708/*
2709 * Skip to next part of an option argument: Skip space and comma.
2710 */
2711 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002712skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 if (*p == ',')
2715 ++p;
2716 while (*p == ' ')
2717 ++p;
2718 return p;
2719}
2720
2721/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002722 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 *
2724 * Most often called through changed_bytes() and changed_lines(), which also
2725 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002726 *
2727 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
2729 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002730changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002733 if (p_imst == IM_ON_THE_SPOT)
2734 {
2735 /* The text of the preediting area is inserted, but this doesn't
2736 * mean a change of the buffer yet. That is delayed until the
2737 * text is committed. (this means preedit becomes empty) */
2738 if (im_is_preediting() && !xim_changed_while_preediting)
2739 return;
2740 xim_changed_while_preediting = FALSE;
2741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#endif
2743
2744 if (!curbuf->b_changed)
2745 {
2746 int save_msg_scroll = msg_scroll;
2747
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002748 /* Give a warning about changing a read-only file. This may also
2749 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002751
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 /* Create a swap file if that is wanted.
2753 * Don't do this for "nofile" and "nowrite" buffer types. */
2754 if (curbuf->b_may_swap
2755#ifdef FEAT_QUICKFIX
2756 && !bt_dontwrite(curbuf)
2757#endif
2758 )
2759 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002760 int save_need_wait_return = need_wait_return;
2761
2762 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 ml_open_file(curbuf);
2764
2765 /* The ml_open_file() can cause an ATTENTION message.
2766 * Wait two seconds, to make sure the user reads this unexpected
2767 * message. Since we could be anywhere, call wait_return() now,
2768 * and don't let the emsg() set msg_scroll. */
2769 if (need_wait_return && emsg_silent == 0)
2770 {
2771 out_flush();
2772 ui_delay(2000L, TRUE);
2773 wait_return(TRUE);
2774 msg_scroll = save_msg_scroll;
2775 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002776 else
2777 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002779 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002781 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782}
2783
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002784/*
2785 * Internal part of changed(), no user interaction.
2786 */
2787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002788changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002789{
2790 curbuf->b_changed = TRUE;
2791 ml_setflags(curbuf);
2792#ifdef FEAT_WINDOWS
2793 check_status(curbuf);
2794 redraw_tabline = TRUE;
2795#endif
2796#ifdef FEAT_TITLE
2797 need_maketitle = TRUE; /* set window title later */
2798#endif
2799}
2800
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002801static void changedOneline(buf_T *buf, linenr_T lnum);
2802static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2803static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805/*
2806 * Changed bytes within a single line for the current buffer.
2807 * - marks the windows on this buffer to be redisplayed
2808 * - marks the buffer changed by calling changed()
2809 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002810 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 */
2812 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002813changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002815 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002817
2818#ifdef FEAT_DIFF
2819 /* Diff highlighting in other diff windows may need to be updated too. */
2820 if (curwin->w_p_diff)
2821 {
2822 win_T *wp;
2823 linenr_T wlnum;
2824
Bram Moolenaar29323592016-07-24 22:04:11 +02002825 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002826 if (wp->w_p_diff && wp != curwin)
2827 {
2828 redraw_win_later(wp, VALID);
2829 wlnum = diff_lnum_win(lnum, wp);
2830 if (wlnum > 0)
2831 changedOneline(wp->w_buffer, wlnum);
2832 }
2833 }
2834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835}
2836
2837 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002838changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002840 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
2842 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002843 if (lnum < buf->b_mod_top)
2844 buf->b_mod_top = lnum;
2845 else if (lnum >= buf->b_mod_bot)
2846 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 else
2849 {
2850 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002851 buf->b_mod_set = TRUE;
2852 buf->b_mod_top = lnum;
2853 buf->b_mod_bot = lnum + 1;
2854 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
2856}
2857
2858/*
2859 * Appended "count" lines below line "lnum" in the current buffer.
2860 * Must be called AFTER the change and after mark_adjust().
2861 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2862 */
2863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002864appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865{
2866 changed_lines(lnum + 1, 0, lnum + 1, count);
2867}
2868
2869/*
2870 * Like appended_lines(), but adjust marks first.
2871 */
2872 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002873appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002875 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002876 * be marks there. But it's still needed in diff mode. */
2877 if (lnum + count < curbuf->b_ml.ml_line_count
2878#ifdef FEAT_DIFF
2879 || curwin->w_p_diff
2880#endif
2881 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002882 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 changed_lines(lnum + 1, 0, lnum + 1, count);
2884}
2885
2886/*
2887 * Deleted "count" lines at line "lnum" in the current buffer.
2888 * Must be called AFTER the change and after mark_adjust().
2889 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2890 */
2891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002892deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894 changed_lines(lnum, 0, lnum + count, -count);
2895}
2896
2897/*
2898 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002899 * Make sure the cursor is on a valid line before calling, a GUI callback may
2900 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
2902 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002903deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
2905 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2906 changed_lines(lnum, 0, lnum + count, -count);
2907}
2908
2909/*
2910 * Changed lines for the current buffer.
2911 * Must be called AFTER the change and after mark_adjust().
2912 * - mark the buffer changed by calling changed()
2913 * - mark the windows on this buffer to be redisplayed
2914 * - invalidate cached values
2915 * "lnum" is the first line that needs displaying, "lnume" the first line
2916 * below the changed lines (BEFORE the change).
2917 * When only inserting lines, "lnum" and "lnume" are equal.
2918 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002919 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
2921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002922changed_lines(
2923 linenr_T lnum, /* first line with change */
2924 colnr_T col, /* column in first line with change */
2925 linenr_T lnume, /* line below last changed line */
2926 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002928 changed_lines_buf(curbuf, lnum, lnume, xtra);
2929
2930#ifdef FEAT_DIFF
2931 if (xtra == 0 && curwin->w_p_diff)
2932 {
2933 /* When the number of lines doesn't change then mark_adjust() isn't
2934 * called and other diff buffers still need to be marked for
2935 * displaying. */
2936 win_T *wp;
2937 linenr_T wlnum;
2938
Bram Moolenaar29323592016-07-24 22:04:11 +02002939 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002940 if (wp->w_p_diff && wp != curwin)
2941 {
2942 redraw_win_later(wp, VALID);
2943 wlnum = diff_lnum_win(lnum, wp);
2944 if (wlnum > 0)
2945 changed_lines_buf(wp->w_buffer, wlnum,
2946 lnume - lnum + wlnum, 0L);
2947 }
2948 }
2949#endif
2950
2951 changed_common(lnum, col, lnume, xtra);
2952}
2953
2954 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002955changed_lines_buf(
2956 buf_T *buf,
2957 linenr_T lnum, /* first line with change */
2958 linenr_T lnume, /* line below last changed line */
2959 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002960{
2961 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
2963 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002964 if (lnum < buf->b_mod_top)
2965 buf->b_mod_top = lnum;
2966 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002969 buf->b_mod_bot += xtra;
2970 if (buf->b_mod_bot < lnum)
2971 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002973 if (lnume + xtra > buf->b_mod_bot)
2974 buf->b_mod_bot = lnume + xtra;
2975 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977 else
2978 {
2979 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002980 buf->b_mod_set = TRUE;
2981 buf->b_mod_top = lnum;
2982 buf->b_mod_bot = lnume + xtra;
2983 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985}
2986
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002987/*
2988 * Common code for when a change is was made.
2989 * See changed_lines() for the arguments.
2990 * Careful: may trigger autocommands that reload the buffer.
2991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002993changed_common(
2994 linenr_T lnum,
2995 colnr_T col,
2996 linenr_T lnume,
2997 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
2999 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003000#ifdef FEAT_WINDOWS
3001 tabpage_T *tp;
3002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 int i;
3004#ifdef FEAT_JUMPLIST
3005 int cols;
3006 pos_T *p;
3007 int add;
3008#endif
3009
3010 /* mark the buffer as modified */
3011 changed();
3012
3013 /* set the '. mark */
3014 if (!cmdmod.keepjumps)
3015 {
3016 curbuf->b_last_change.lnum = lnum;
3017 curbuf->b_last_change.col = col;
3018
3019#ifdef FEAT_JUMPLIST
3020 /* Create a new entry if a new undo-able change was started or we
3021 * don't have an entry yet. */
3022 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3023 {
3024 if (curbuf->b_changelistlen == 0)
3025 add = TRUE;
3026 else
3027 {
3028 /* Don't create a new entry when the line number is the same
3029 * as the last one and the column is not too far away. Avoids
3030 * creating many entries for typing "xxxxx". */
3031 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3032 if (p->lnum != lnum)
3033 add = TRUE;
3034 else
3035 {
3036 cols = comp_textwidth(FALSE);
3037 if (cols == 0)
3038 cols = 79;
3039 add = (p->col + cols < col || col + cols < p->col);
3040 }
3041 }
3042 if (add)
3043 {
3044 /* This is the first of a new sequence of undo-able changes
3045 * and it's at some distance of the last change. Use a new
3046 * position in the changelist. */
3047 curbuf->b_new_change = FALSE;
3048
3049 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3050 {
3051 /* changelist is full: remove oldest entry */
3052 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3053 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3054 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003055 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 {
3057 /* Correct position in changelist for other windows on
3058 * this buffer. */
3059 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3060 --wp->w_changelistidx;
3061 }
3062 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003063 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
3065 /* For other windows, if the position in the changelist is
3066 * at the end it stays at the end. */
3067 if (wp->w_buffer == curbuf
3068 && wp->w_changelistidx == curbuf->b_changelistlen)
3069 ++wp->w_changelistidx;
3070 }
3071 ++curbuf->b_changelistlen;
3072 }
3073 }
3074 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3075 curbuf->b_last_change;
3076 /* The current window is always after the last change, so that "g,"
3077 * takes you back to it. */
3078 curwin->w_changelistidx = curbuf->b_changelistlen;
3079#endif
3080 }
3081
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003082 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
3084 if (wp->w_buffer == curbuf)
3085 {
3086 /* Mark this window to be redrawn later. */
3087 if (wp->w_redr_type < VALID)
3088 wp->w_redr_type = VALID;
3089
3090 /* Check if a change in the buffer has invalidated the cached
3091 * values for the cursor. */
3092#ifdef FEAT_FOLDING
3093 /*
3094 * Update the folds for this window. Can't postpone this, because
3095 * a following operator might work on the whole fold: ">>dd".
3096 */
3097 foldUpdate(wp, lnum, lnume + xtra - 1);
3098
3099 /* The change may cause lines above or below the change to become
3100 * included in a fold. Set lnum/lnume to the first/last line that
3101 * might be displayed differently.
3102 * Set w_cline_folded here as an efficient way to update it when
3103 * inserting lines just above a closed fold. */
3104 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3105 if (wp->w_cursor.lnum == lnum)
3106 wp->w_cline_folded = i;
3107 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3108 if (wp->w_cursor.lnum == lnume)
3109 wp->w_cline_folded = i;
3110
3111 /* If the changed line is in a range of previously folded lines,
3112 * compare with the first line in that range. */
3113 if (wp->w_cursor.lnum <= lnum)
3114 {
3115 i = find_wl_entry(wp, lnum);
3116 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3117 changed_line_abv_curs_win(wp);
3118 }
3119#endif
3120
3121 if (wp->w_cursor.lnum > lnum)
3122 changed_line_abv_curs_win(wp);
3123 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3124 changed_cline_bef_curs_win(wp);
3125 if (wp->w_botline >= lnum)
3126 {
3127 /* Assume that botline doesn't change (inserted lines make
3128 * other lines scroll down below botline). */
3129 approximate_botline_win(wp);
3130 }
3131
3132 /* Check if any w_lines[] entries have become invalid.
3133 * For entries below the change: Correct the lnums for
3134 * inserted/deleted lines. Makes it possible to stop displaying
3135 * after the change. */
3136 for (i = 0; i < wp->w_lines_valid; ++i)
3137 if (wp->w_lines[i].wl_valid)
3138 {
3139 if (wp->w_lines[i].wl_lnum >= lnum)
3140 {
3141 if (wp->w_lines[i].wl_lnum < lnume)
3142 {
3143 /* line included in change */
3144 wp->w_lines[i].wl_valid = FALSE;
3145 }
3146 else if (xtra != 0)
3147 {
3148 /* line below change */
3149 wp->w_lines[i].wl_lnum += xtra;
3150#ifdef FEAT_FOLDING
3151 wp->w_lines[i].wl_lastlnum += xtra;
3152#endif
3153 }
3154 }
3155#ifdef FEAT_FOLDING
3156 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3157 {
3158 /* change somewhere inside this range of folded lines,
3159 * may need to be redrawn */
3160 wp->w_lines[i].wl_valid = FALSE;
3161 }
3162#endif
3163 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003164
3165#ifdef FEAT_FOLDING
3166 /* Take care of side effects for setting w_topline when folds have
3167 * changed. Esp. when the buffer was changed in another window. */
3168 if (hasAnyFolding(wp))
3169 set_topline(wp, wp->w_topline);
3170#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003171 /* relative numbering may require updating more */
3172 if (wp->w_p_rnu)
3173 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 }
3176
3177 /* Call update_screen() later, which checks out what needs to be redrawn,
3178 * since it notices b_mod_set and then uses b_mod_*. */
3179 if (must_redraw < VALID)
3180 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003181
3182#ifdef FEAT_AUTOCMD
3183 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003184 if (lnum <= curwin->w_cursor.lnum
3185 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003186 last_cursormoved.lnum = 0;
3187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188}
3189
3190/*
3191 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3192 */
3193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003194unchanged(
3195 buf_T *buf,
3196 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003198 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003201 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (ff)
3203 save_file_ff(buf);
3204#ifdef FEAT_WINDOWS
3205 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003206 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207#endif
3208#ifdef FEAT_TITLE
3209 need_maketitle = TRUE; /* set window title later */
3210#endif
3211 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003212 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213#ifdef FEAT_NETBEANS_INTG
3214 netbeans_unmodified(buf);
3215#endif
3216}
3217
3218#if defined(FEAT_WINDOWS) || defined(PROTO)
3219/*
3220 * check_status: called when the status bars for the buffer 'buf'
3221 * need to be updated
3222 */
3223 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003224check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226 win_T *wp;
3227
Bram Moolenaar29323592016-07-24 22:04:11 +02003228 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (wp->w_buffer == buf && wp->w_status_height)
3230 {
3231 wp->w_redr_status = TRUE;
3232 if (must_redraw < VALID)
3233 must_redraw = VALID;
3234 }
3235}
3236#endif
3237
3238/*
3239 * If the file is readonly, give a warning message with the first change.
3240 * Don't do this for autocommands.
3241 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003242 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003244 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 */
3246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003247change_warning(
3248 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 mode and 'showmode' is on */
3250{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003251 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3252
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 if (curbuf->b_did_warn == FALSE
3254 && curbufIsChanged() == 0
3255#ifdef FEAT_AUTOCMD
3256 && !autocmd_busy
3257#endif
3258 && curbuf->b_p_ro)
3259 {
3260#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003261 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003263 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (!curbuf->b_p_ro)
3265 return;
3266#endif
3267 /*
3268 * Do what msg() does, but with a column offset if the warning should
3269 * be after the mode message.
3270 */
3271 msg_start();
3272 if (msg_row == Rows - 1)
3273 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003274 msg_source(HL_ATTR(HLF_W));
3275 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003276#ifdef FEAT_EVAL
3277 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 msg_clr_eos();
3280 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003281 if (msg_silent == 0 && !silent_mode
3282#ifdef FEAT_EVAL
3283 && time_for_testing != 1
3284#endif
3285 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 out_flush();
3288 ui_delay(1000L, TRUE); /* give the user time to think about it */
3289 }
3290 curbuf->b_did_warn = TRUE;
3291 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3292 if (msg_row < Rows - 1)
3293 showmode();
3294 }
3295}
3296
3297/*
3298 * Ask for a reply from the user, a 'y' or a 'n'.
3299 * No other characters are accepted, the message is repeated until a valid
3300 * reply is entered or CTRL-C is hit.
3301 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3302 * from any buffers but directly from the user.
3303 *
3304 * return the 'y' or 'n'
3305 */
3306 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003307ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 int r = ' ';
3310 int save_State = State;
3311
3312 if (exiting) /* put terminal in raw mode for this question */
3313 settmode(TMODE_RAW);
3314 ++no_wait_return;
3315#ifdef USE_ON_FLY_SCROLL
3316 dont_scroll = TRUE; /* disallow scrolling here */
3317#endif
3318 State = CONFIRM; /* mouse behaves like with :confirm */
3319#ifdef FEAT_MOUSE
3320 setmouse(); /* disables mouse for xterm */
3321#endif
3322 ++no_mapping;
3323 ++allow_keys; /* no mapping here, but recognize keys */
3324
3325 while (r != 'y' && r != 'n')
3326 {
3327 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003328 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (direct)
3330 r = get_keystroke();
3331 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003332 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (r == Ctrl_C || r == ESC)
3334 r = 'n';
3335 msg_putchar(r); /* show what you typed */
3336 out_flush();
3337 }
3338 --no_wait_return;
3339 State = save_State;
3340#ifdef FEAT_MOUSE
3341 setmouse();
3342#endif
3343 --no_mapping;
3344 --allow_keys;
3345
3346 return r;
3347}
3348
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003349#if defined(FEAT_MOUSE) || defined(PROTO)
3350/*
3351 * Return TRUE if "c" is a mouse key.
3352 */
3353 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003354is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003355{
3356 return c == K_LEFTMOUSE
3357 || c == K_LEFTMOUSE_NM
3358 || c == K_LEFTDRAG
3359 || c == K_LEFTRELEASE
3360 || c == K_LEFTRELEASE_NM
3361 || c == K_MIDDLEMOUSE
3362 || c == K_MIDDLEDRAG
3363 || c == K_MIDDLERELEASE
3364 || c == K_RIGHTMOUSE
3365 || c == K_RIGHTDRAG
3366 || c == K_RIGHTRELEASE
3367 || c == K_MOUSEDOWN
3368 || c == K_MOUSEUP
3369 || c == K_MOUSELEFT
3370 || c == K_MOUSERIGHT
3371 || c == K_X1MOUSE
3372 || c == K_X1DRAG
3373 || c == K_X1RELEASE
3374 || c == K_X2MOUSE
3375 || c == K_X2DRAG
3376 || c == K_X2RELEASE;
3377}
3378#endif
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380/*
3381 * Get a key stroke directly from the user.
3382 * Ignores mouse clicks and scrollbar events, except a click for the left
3383 * button (used at the more prompt).
3384 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3385 * Disadvantage: typeahead is ignored.
3386 * Translates the interrupt character for unix to ESC.
3387 */
3388 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003389get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003391 char_u *buf = NULL;
3392 int buflen = 150;
3393 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 int len = 0;
3395 int n;
3396 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003397 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399 mapped_ctrl_c = FALSE; /* mappings are not used here */
3400 for (;;)
3401 {
3402 cursor_on();
3403 out_flush();
3404
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003405 /* Leave some room for check_termcode() to insert a key code into (max
3406 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3407 * bytes. */
3408 maxlen = (buflen - 6 - len) / 3;
3409 if (buf == NULL)
3410 buf = alloc(buflen);
3411 else if (maxlen < 10)
3412 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003413 char_u *t_buf = buf;
3414
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003415 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003416 * escape sequence. */
3417 buflen += 100;
3418 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003419 if (buf == NULL)
3420 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003421 maxlen = (buflen - 6 - len) / 3;
3422 }
3423 if (buf == NULL)
3424 {
3425 do_outofmem_msg((long_u)buflen);
3426 return ESC; /* panic! */
3427 }
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003430 * terminal code to complete. */
3431 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (n > 0)
3433 {
3434 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003435 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003437 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003439 else if (len > 0)
3440 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
Bram Moolenaar4395a712006-09-05 18:57:57 +00003442 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003443 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003444 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003446
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003447 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003448 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003449 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003450 {
3451 /* Redrawing was postponed, do it now. */
3452 update_screen(0);
3453 setcursor(); /* put cursor back where it belongs */
3454 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003455 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003456 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003457 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003459 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 continue;
3461
3462 /* Handle modifier and/or special key code. */
3463 n = buf[0];
3464 if (n == K_SPECIAL)
3465 {
3466 n = TO_SPECIAL(buf[1], buf[2]);
3467 if (buf[1] == KS_MODIFIER
3468 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003469#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003470 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003471#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003472#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 || n == K_VER_SCROLLBAR
3474 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#endif
3476 )
3477 {
3478 if (buf[1] == KS_MODIFIER)
3479 mod_mask = buf[2];
3480 len -= 3;
3481 if (len > 0)
3482 mch_memmove(buf, buf + 3, (size_t)len);
3483 continue;
3484 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487#ifdef FEAT_MBYTE
3488 if (has_mbyte)
3489 {
3490 if (MB_BYTE2LEN(n) > len)
3491 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003492 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 n = (*mb_ptr2char)(buf);
3494 }
3495#endif
3496#ifdef UNIX
3497 if (n == intr_char)
3498 n = ESC;
3499#endif
3500 break;
3501 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003502 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
3504 mapped_ctrl_c = save_mapped_ctrl_c;
3505 return n;
3506}
3507
3508/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003509 * Get a number from the user.
3510 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 */
3512 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003513get_number(
3514 int colon, /* allow colon to abort */
3515 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
3517 int n = 0;
3518 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003519 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003521 if (mouse_used != NULL)
3522 *mouse_used = FALSE;
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 /* When not printing messages, the user won't know what to type, return a
3525 * zero (as if CR was hit). */
3526 if (msg_silent != 0)
3527 return 0;
3528
3529#ifdef USE_ON_FLY_SCROLL
3530 dont_scroll = TRUE; /* disallow scrolling here */
3531#endif
3532 ++no_mapping;
3533 ++allow_keys; /* no mapping here, but recognize keys */
3534 for (;;)
3535 {
3536 windgoto(msg_row, msg_col);
3537 c = safe_vgetc();
3538 if (VIM_ISDIGIT(c))
3539 {
3540 n = n * 10 + c - '0';
3541 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003542 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3545 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003546 if (typed > 0)
3547 {
3548 MSG_PUTS("\b \b");
3549 --typed;
3550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003553#ifdef FEAT_MOUSE
3554 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3555 {
3556 *mouse_used = TRUE;
3557 n = mouse_row + 1;
3558 break;
3559 }
3560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else if (n == 0 && c == ':' && colon)
3562 {
3563 stuffcharReadbuff(':');
3564 if (!exmode_active)
3565 cmdline_row = msg_row;
3566 skip_redraw = TRUE; /* skip redraw once */
3567 do_redraw = FALSE;
3568 break;
3569 }
3570 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3571 break;
3572 }
3573 --no_mapping;
3574 --allow_keys;
3575 return n;
3576}
3577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003578/*
3579 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003580 * When "mouse_used" is not NULL allow using the mouse and in that case return
3581 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003582 */
3583 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003584prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003585{
3586 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003587 int save_cmdline_row;
3588 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589
3590 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003591 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003592 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003593 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003594 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003595
Bram Moolenaar203335e2006-09-03 14:35:42 +00003596 /* Set the state such that text can be selected/copied/pasted and we still
3597 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003598 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003599 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003600 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003601 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003602
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003603 i = get_number(TRUE, mouse_used);
3604 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003605 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003606 /* don't call wait_return() now */
3607 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 cmdline_row = msg_row - 1;
3609 need_wait_return = FALSE;
3610 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003611 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003613 else
3614 cmdline_row = save_cmdline_row;
3615 State = save_State;
3616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003617 return i;
3618}
3619
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003621msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
3623 long pn;
3624
3625 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3627 return;
3628
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003629 /* We don't want to overwrite another important message, but do overwrite
3630 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3631 * then "put" reports the last action. */
3632 if (keep_msg != NULL && !keep_msg_more)
3633 return;
3634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (n > 0)
3636 pn = n;
3637 else
3638 pn = -n;
3639
3640 if (pn > p_report)
3641 {
3642 if (pn == 1)
3643 {
3644 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003645 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3646 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003648 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3649 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651 else
3652 {
3653 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003654 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3655 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003657 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3658 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003661 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (msg(msg_buf))
3663 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003664 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003665 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667 }
3668}
3669
3670/*
3671 * flush map and typeahead buffers and give a warning for an error
3672 */
3673 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003674beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 if (emsg_silent == 0)
3677 {
3678 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003679 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681}
3682
3683/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003684 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
3686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003687vim_beep(
3688 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
3690 if (emsg_silent == 0)
3691 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003692 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3693 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003694#ifdef ELAPSED_FUNC
3695 static int did_init = FALSE;
3696 static ELAPSED_TYPE start_tv;
3697
3698 /* Only beep once per half a second, otherwise a sequence of beeps
3699 * would freeze Vim. */
3700 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3701 {
3702 did_init = TRUE;
3703 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003705 if (p_vb
3706#ifdef FEAT_GUI
3707 /* While the GUI is starting up the termcap is set for
3708 * the GUI but the output still goes to a terminal. */
3709 && !(gui.in_use && gui.starting)
3710#endif
3711 )
3712 out_str_cf(T_VB);
3713 else
3714 out_char(BELL);
3715#ifdef ELAPSED_FUNC
3716 }
3717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003719
3720 /* When 'verbose' is set and we are sourcing a script or executing a
3721 * function give the user a hint where the beep comes from. */
3722 if (vim_strchr(p_debug, 'e') != NULL)
3723 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003724 msg_source(HL_ATTR(HLF_W));
3725 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728}
3729
3730/*
3731 * To get the "real" home directory:
3732 * - get value of $HOME
3733 * For Unix:
3734 * - go to that directory
3735 * - do mch_dirname() to get the real name of that directory.
3736 * This also works with mounts and links.
3737 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3738 */
3739static char_u *homedir = NULL;
3740
3741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003742init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 char_u *var;
3745
Bram Moolenaar05159a02005-02-26 23:04:13 +00003746 /* In case we are called a second time (when 'encoding' changes). */
3747 vim_free(homedir);
3748 homedir = NULL;
3749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750#ifdef VMS
3751 var = mch_getenv((char_u *)"SYS$LOGIN");
3752#else
3753 var = mch_getenv((char_u *)"HOME");
3754#endif
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#ifdef WIN3264
3757 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003758 * Typically, $HOME is not defined on Windows, unless the user has
3759 * specifically defined it for Vim's sake. However, on Windows NT
3760 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3761 * each user. Try constructing $HOME from these.
3762 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003763 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003764 {
3765 char_u *homedrive, *homepath;
3766
3767 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3768 homepath = mch_getenv((char_u *)"HOMEPATH");
3769 if (homepath == NULL || *homepath == NUL)
3770 homepath = (char_u *)"\\";
3771 if (homedrive != NULL
3772 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3773 {
3774 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3775 if (NameBuff[0] != NUL)
3776 var = NameBuff;
3777 }
3778 }
3779
3780 if (var == NULL)
3781 var = mch_getenv((char_u *)"USERPROFILE");
3782
3783 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 * Weird but true: $HOME may contain an indirect reference to another
3785 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3786 * when $HOME is being set.
3787 */
3788 if (var != NULL && *var == '%')
3789 {
3790 char_u *p;
3791 char_u *exp;
3792
3793 p = vim_strchr(var + 1, '%');
3794 if (p != NULL)
3795 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003796 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 exp = mch_getenv(NameBuff);
3798 if (exp != NULL && *exp != NUL
3799 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3800 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003801 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804 }
3805 }
3806
Bram Moolenaar48340b62017-08-29 22:08:53 +02003807 if (var != NULL && *var == NUL) /* empty is same as not set */
3808 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
Bram Moolenaar48340b62017-08-29 22:08:53 +02003810# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003811 if (enc_utf8 && var != NULL)
3812 {
3813 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003814 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003815
3816 /* Convert from active codepage to UTF-8. Other conversions are
3817 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003818 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003819 if (pp != NULL)
3820 {
3821 homedir = pp;
3822 return;
3823 }
3824 }
3825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 /*
3828 * Default home dir is C:/
3829 * Best assumption we can make in such a situation.
3830 */
3831 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003832 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (var != NULL)
3836 {
3837#ifdef UNIX
3838 /*
3839 * Change to the directory and get the actual path. This resolves
3840 * links. Don't do it when we can't return.
3841 */
3842 if (mch_dirname(NameBuff, MAXPATHL) == OK
3843 && mch_chdir((char *)NameBuff) == 0)
3844 {
3845 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3846 var = IObuff;
3847 if (mch_chdir((char *)NameBuff) != 0)
3848 EMSG(_(e_prev_dir));
3849 }
3850#endif
3851 homedir = vim_strsave(var);
3852 }
3853}
3854
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003855#if defined(EXITFREE) || defined(PROTO)
3856 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003857free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003858{
3859 vim_free(homedir);
3860}
Bram Moolenaar24305862012-08-15 14:05:05 +02003861
3862# ifdef FEAT_CMDL_COMPL
3863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003864free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003865{
3866 ga_clear_strings(&ga_users);
3867}
3868# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003869#endif
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003872 * Call expand_env() and store the result in an allocated string.
3873 * This is not very memory efficient, this expects the result to be freed
3874 * again soon.
3875 */
3876 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003877expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003878{
3879 return expand_env_save_opt(src, FALSE);
3880}
3881
3882/*
3883 * Idem, but when "one" is TRUE handle the string as one file name, only
3884 * expand "~" at the start.
3885 */
3886 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003887expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003888{
3889 char_u *p;
3890
3891 p = alloc(MAXPATHL);
3892 if (p != NULL)
3893 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3894 return p;
3895}
3896
3897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 * Expand environment variable with path name.
3899 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003900 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 * If anything fails no expansion is done and dst equals src.
3902 */
3903 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003904expand_env(
3905 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3906 char_u *dst, /* where to put the result */
3907 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003909 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910}
3911
3912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003913expand_env_esc(
3914 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3915 char_u *dst, /* where to put the result */
3916 int dstlen, /* maximum length of the result */
3917 int esc, /* escape spaces in expanded variables */
3918 int one, /* "srcp" is one file name */
3919 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003921 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 char_u *tail;
3923 int c;
3924 char_u *var;
3925 int copy_char;
3926 int mustfree; /* var was allocated, need to free it later */
3927 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003928 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003930 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003931 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003932
3933 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 --dstlen; /* leave one char space for "\," */
3935 while (*src && dstlen > 0)
3936 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003937#ifdef FEAT_EVAL
3938 /* Skip over `=expr`. */
3939 if (src[0] == '`' && src[1] == '=')
3940 {
3941 size_t len;
3942
3943 var = src;
3944 src += 2;
3945 (void)skip_expr(&src);
3946 if (*src == '`')
3947 ++src;
3948 len = src - var;
3949 if (len > (size_t)dstlen)
3950 len = dstlen;
3951 vim_strncpy(dst, var, len);
3952 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003953 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003954 continue;
3955 }
3956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003958 if ((*src == '$'
3959#ifdef VMS
3960 && at_start
3961#endif
3962 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003963#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 || *src == '%'
3965#endif
3966 || (*src == '~' && at_start))
3967 {
3968 mustfree = FALSE;
3969
3970 /*
3971 * The variable name is copied into dst temporarily, because it may
3972 * be a string in read-only memory and a NUL needs to be appended.
3973 */
3974 if (*src != '~') /* environment var */
3975 {
3976 tail = src + 1;
3977 var = dst;
3978 c = dstlen - 1;
3979
3980#ifdef UNIX
3981 /* Unix has ${var-name} type environment vars */
3982 if (*tail == '{' && !vim_isIDc('{'))
3983 {
3984 tail++; /* ignore '{' */
3985 while (c-- > 0 && *tail && *tail != '}')
3986 *var++ = *tail++;
3987 }
3988 else
3989#endif
3990 {
3991 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003992#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 || (*src == '%' && *tail != '%')
3994#endif
3995 ))
3996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
4000
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004001#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002# ifdef UNIX
4003 if (src[1] == '{' && *tail != '}')
4004# else
4005 if (*src == '%' && *tail != '%')
4006# endif
4007 var = NULL;
4008 else
4009 {
4010# ifdef UNIX
4011 if (src[1] == '{')
4012# else
4013 if (*src == '%')
4014#endif
4015 ++tail;
4016#endif
4017 *var = NUL;
4018 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004019#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021#endif
4022 }
4023 /* home directory */
4024 else if ( src[1] == NUL
4025 || vim_ispathsep(src[1])
4026 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4027 {
4028 var = homedir;
4029 tail = src + 1;
4030 }
4031 else /* user directory */
4032 {
4033#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4034 /*
4035 * Copy ~user to dst[], so we can put a NUL after it.
4036 */
4037 tail = src;
4038 var = dst;
4039 c = dstlen - 1;
4040 while ( c-- > 0
4041 && *tail
4042 && vim_isfilec(*tail)
4043 && !vim_ispathsep(*tail))
4044 *var++ = *tail++;
4045 *var = NUL;
4046# ifdef UNIX
4047 /*
4048 * If the system supports getpwnam(), use it.
4049 * Otherwise, or if getpwnam() fails, the shell is used to
4050 * expand ~user. This is slower and may fail if the shell
4051 * does not support ~user (old versions of /bin/sh).
4052 */
4053# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4054 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004055 /* Note: memory allocated by getpwnam() is never freed.
4056 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004057 struct passwd *pw = (*dst == NUL)
4058 ? NULL : getpwnam((char *)dst + 1);
4059
4060 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 if (var == NULL)
4063# endif
4064 {
4065 expand_T xpc;
4066
4067 ExpandInit(&xpc);
4068 xpc.xp_context = EXPAND_FILES;
4069 var = ExpandOne(&xpc, dst, NULL,
4070 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 mustfree = TRUE;
4072 }
4073
4074# else /* !UNIX, thus VMS */
4075 /*
4076 * USER_HOME is a comma-separated list of
4077 * directories to search for the user account in.
4078 */
4079 {
4080 char_u test[MAXPATHL], paths[MAXPATHL];
4081 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004082 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 STRCPY(paths, USER_HOME);
4085 next_path = paths;
4086 while (*next_path)
4087 {
4088 for (path = next_path; *next_path && *next_path != ',';
4089 next_path++);
4090 if (*next_path)
4091 *next_path++ = NUL;
4092 STRCPY(test, path);
4093 STRCAT(test, "/");
4094 STRCAT(test, dst + 1);
4095 if (mch_stat(test, &st) == 0)
4096 {
4097 var = alloc(STRLEN(test) + 1);
4098 STRCPY(var, test);
4099 mustfree = TRUE;
4100 break;
4101 }
4102 }
4103 }
4104# endif /* UNIX */
4105#else
4106 /* cannot expand user's home directory, so don't try */
4107 var = NULL;
4108 tail = (char_u *)""; /* for gcc */
4109#endif /* UNIX || VMS */
4110 }
4111
4112#ifdef BACKSLASH_IN_FILENAME
4113 /* If 'shellslash' is set change backslashes to forward slashes.
4114 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4115 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4116 {
4117 char_u *p = vim_strsave(var);
4118
4119 if (p != NULL)
4120 {
4121 if (mustfree)
4122 vim_free(var);
4123 var = p;
4124 mustfree = TRUE;
4125 forward_slash(var);
4126 }
4127 }
4128#endif
4129
4130 /* If "var" contains white space, escape it with a backslash.
4131 * Required for ":e ~/tt" when $HOME includes a space. */
4132 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4133 {
4134 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4135
4136 if (p != NULL)
4137 {
4138 if (mustfree)
4139 vim_free(var);
4140 var = p;
4141 mustfree = TRUE;
4142 }
4143 }
4144
4145 if (var != NULL && *var != NUL
4146 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4147 {
4148 STRCPY(dst, var);
4149 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004150 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 /* if var[] ends in a path separator and tail[] starts
4152 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004153 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4155 && dst[-1] != ':'
4156#endif
4157 && vim_ispathsep(*tail))
4158 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004159 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 src = tail;
4161 copy_char = FALSE;
4162 }
4163 if (mustfree)
4164 vim_free(var);
4165 }
4166
4167 if (copy_char) /* copy at least one char */
4168 {
4169 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004170 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004171 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4172 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 */
4174 at_start = FALSE;
4175 if (src[0] == '\\' && src[1] != NUL)
4176 {
4177 *dst++ = *src++;
4178 --dstlen;
4179 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004180 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004182 if (dstlen > 0)
4183 {
4184 *dst++ = *src++;
4185 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004186
Bram Moolenaar1c864092017-08-06 18:15:45 +02004187 if (startstr != NULL && src - startstr_len >= srcp
4188 && STRNCMP(src - startstr_len, startstr,
4189 startstr_len) == 0)
4190 at_start = TRUE;
4191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 *dst = NUL;
4196}
4197
4198/*
4199 * Vim's version of getenv().
4200 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004201 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004202 * "mustfree" is set to TRUE when returned is allocated, it must be
4203 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
4205 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004206vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 char_u *p;
4209 char_u *pend;
4210 int vimruntime;
4211
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004212#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 /* use "C:/" when $HOME is not set */
4214 if (STRCMP(name, "HOME") == 0)
4215 return homedir;
4216#endif
4217
4218 p = mch_getenv(name);
4219 if (p != NULL && *p == NUL) /* empty is the same as not set */
4220 p = NULL;
4221
4222 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004223 {
4224#if defined(FEAT_MBYTE) && defined(WIN3264)
4225 if (enc_utf8)
4226 {
4227 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004228 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004229
4230 /* Convert from active codepage to UTF-8. Other conversions are
4231 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004232 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004233 if (pp != NULL)
4234 {
4235 p = pp;
4236 *mustfree = TRUE;
4237 }
4238 }
4239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4244 if (!vimruntime && STRCMP(name, "VIM") != 0)
4245 return NULL;
4246
4247 /*
4248 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4249 * Don't do this when default_vimruntime_dir is non-empty.
4250 */
4251 if (vimruntime
4252#ifdef HAVE_PATHDEF
4253 && *default_vimruntime_dir == NUL
4254#endif
4255 )
4256 {
4257 p = mch_getenv((char_u *)"VIM");
4258 if (p != NULL && *p == NUL) /* empty is the same as not set */
4259 p = NULL;
4260 if (p != NULL)
4261 {
4262 p = vim_version_dir(p);
4263 if (p != NULL)
4264 *mustfree = TRUE;
4265 else
4266 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004267
4268#if defined(FEAT_MBYTE) && defined(WIN3264)
4269 if (enc_utf8)
4270 {
4271 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004272 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004273
4274 /* Convert from active codepage to UTF-8. Other conversions
4275 * are not done, because they would fail for non-ASCII
4276 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004277 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004278 if (pp != NULL)
4279 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004280 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281 vim_free(p);
4282 p = pp;
4283 *mustfree = TRUE;
4284 }
4285 }
4286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289
4290 /*
4291 * When expanding $VIM or $VIMRUNTIME fails, try using:
4292 * - the directory name from 'helpfile' (unless it contains '$')
4293 * - the executable name from argv[0]
4294 */
4295 if (p == NULL)
4296 {
4297 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4298 p = p_hf;
4299#ifdef USE_EXE_NAME
4300 /*
4301 * Use the name of the executable, obtained from argv[0].
4302 */
4303 else
4304 p = exe_name;
4305#endif
4306 if (p != NULL)
4307 {
4308 /* remove the file name */
4309 pend = gettail(p);
4310
4311 /* remove "doc/" from 'helpfile', if present */
4312 if (p == p_hf)
4313 pend = remove_tail(p, pend, (char_u *)"doc");
4314
4315#ifdef USE_EXE_NAME
4316# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004317 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (p == exe_name)
4319 {
4320 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004321 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004323 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4324 if (pend1 != pend)
4325 {
4326 pnew = alloc((unsigned)(pend1 - p) + 15);
4327 if (pnew != NULL)
4328 {
4329 STRNCPY(pnew, p, (pend1 - p));
4330 STRCPY(pnew + (pend1 - p), "Resources/vim");
4331 p = pnew;
4332 pend = p + STRLEN(p);
4333 }
4334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336# endif
4337 /* remove "src/" from exe_name, if present */
4338 if (p == exe_name)
4339 pend = remove_tail(p, pend, (char_u *)"src");
4340#endif
4341
4342 /* for $VIM, remove "runtime/" or "vim54/", if present */
4343 if (!vimruntime)
4344 {
4345 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4346 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4347 }
4348
4349 /* remove trailing path separator */
4350#ifndef MACOS_CLASSIC
4351 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004352 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004353 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 --pend;
4355#endif
4356
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004357#ifdef MACOS_X
4358 if (p == exe_name || p == p_hf)
4359#endif
4360 /* check that the result is a directory name */
4361 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 if (p != NULL && !mch_isdir(p))
4364 {
4365 vim_free(p);
4366 p = NULL;
4367 }
4368 else
4369 {
4370#ifdef USE_EXE_NAME
4371 /* may add "/vim54" or "/runtime" if it exists */
4372 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4373 {
4374 vim_free(p);
4375 p = pend;
4376 }
4377#endif
4378 *mustfree = TRUE;
4379 }
4380 }
4381 }
4382
4383#ifdef HAVE_PATHDEF
4384 /* When there is a pathdef.c file we can use default_vim_dir and
4385 * default_vimruntime_dir */
4386 if (p == NULL)
4387 {
4388 /* Only use default_vimruntime_dir when it is not empty */
4389 if (vimruntime && *default_vimruntime_dir != NUL)
4390 {
4391 p = default_vimruntime_dir;
4392 *mustfree = FALSE;
4393 }
4394 else if (*default_vim_dir != NUL)
4395 {
4396 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4397 *mustfree = TRUE;
4398 else
4399 {
4400 p = default_vim_dir;
4401 *mustfree = FALSE;
4402 }
4403 }
4404 }
4405#endif
4406
4407 /*
4408 * Set the environment variable, so that the new value can be found fast
4409 * next time, and others can also use it (e.g. Perl).
4410 */
4411 if (p != NULL)
4412 {
4413 if (vimruntime)
4414 {
4415 vim_setenv((char_u *)"VIMRUNTIME", p);
4416 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 else
4419 {
4420 vim_setenv((char_u *)"VIM", p);
4421 didset_vim = TRUE;
4422 }
4423 }
4424 return p;
4425}
4426
4427/*
4428 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4429 * Return NULL if not, return its name in allocated memory otherwise.
4430 */
4431 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004432vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433{
4434 char_u *p;
4435
4436 if (vimdir == NULL || *vimdir == NUL)
4437 return NULL;
4438 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4439 if (p != NULL && mch_isdir(p))
4440 return p;
4441 vim_free(p);
4442 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4443 if (p != NULL && mch_isdir(p))
4444 return p;
4445 vim_free(p);
4446 return NULL;
4447}
4448
4449/*
4450 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4451 * the length of "name/". Otherwise return "pend".
4452 */
4453 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004454remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455{
4456 int len = (int)STRLEN(name) + 1;
4457 char_u *newend = pend - len;
4458
4459 if (newend >= p
4460 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004461 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 return newend;
4463 return pend;
4464}
4465
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 * Our portable version of setenv.
4468 */
4469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004470vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471{
4472#ifdef HAVE_SETENV
4473 mch_setenv((char *)name, (char *)val, 1);
4474#else
4475 char_u *envbuf;
4476
4477 /*
4478 * Putenv does not copy the string, it has to remain
4479 * valid. The allocated memory will never be freed.
4480 */
4481 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4482 if (envbuf != NULL)
4483 {
4484 sprintf((char *)envbuf, "%s=%s", name, val);
4485 putenv((char *)envbuf);
4486 }
4487#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004488#ifdef FEAT_GETTEXT
4489 /*
4490 * When setting $VIMRUNTIME adjust the directory to find message
4491 * translations to $VIMRUNTIME/lang.
4492 */
4493 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4494 {
4495 char_u *buf = concat_str(val, (char_u *)"/lang");
4496
4497 if (buf != NULL)
4498 {
4499 bindtextdomain(VIMPACKAGE, (char *)buf);
4500 vim_free(buf);
4501 }
4502 }
4503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504}
4505
4506#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4507/*
4508 * Function given to ExpandGeneric() to obtain an environment variable name.
4509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004511get_env_name(
4512 expand_T *xp UNUSED,
4513 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514{
4515# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4516 /*
4517 * No environ[] on the Amiga and on the Mac (using MPW).
4518 */
4519 return NULL;
4520# else
4521# ifndef __WIN32__
4522 /* Borland C++ 5.2 has this in a header file. */
4523 extern char **environ;
4524# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004525# define ENVNAMELEN 100
4526 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 char_u *str;
4528 int n;
4529
4530 str = (char_u *)environ[idx];
4531 if (str == NULL)
4532 return NULL;
4533
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004534 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
4536 if (str[n] == '=' || str[n] == NUL)
4537 break;
4538 name[n] = str[n];
4539 }
4540 name[n] = NUL;
4541 return name;
4542# endif
4543}
Bram Moolenaar24305862012-08-15 14:05:05 +02004544
4545/*
4546 * Find all user names for user completion.
4547 * Done only once and then cached.
4548 */
4549 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004550init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004551{
Bram Moolenaar24305862012-08-15 14:05:05 +02004552 static int lazy_init_done = FALSE;
4553
4554 if (lazy_init_done)
4555 return;
4556
4557 lazy_init_done = TRUE;
4558 ga_init2(&ga_users, sizeof(char_u *), 20);
4559
4560# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4561 {
4562 char_u* user;
4563 struct passwd* pw;
4564
4565 setpwent();
4566 while ((pw = getpwent()) != NULL)
4567 /* pw->pw_name shouldn't be NULL but just in case... */
4568 if (pw->pw_name != NULL)
4569 {
4570 if (ga_grow(&ga_users, 1) == FAIL)
4571 break;
4572 user = vim_strsave((char_u*)pw->pw_name);
4573 if (user == NULL)
4574 break;
4575 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4576 }
4577 endpwent();
4578 }
4579# endif
4580}
4581
4582/*
4583 * Function given to ExpandGeneric() to obtain an user names.
4584 */
4585 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004586get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004587{
4588 init_users();
4589 if (idx < ga_users.ga_len)
4590 return ((char_u **)ga_users.ga_data)[idx];
4591 return NULL;
4592}
4593
4594/*
4595 * Check whether name matches a user name. Return:
4596 * 0 if name does not match any user name.
4597 * 1 if name partially matches the beginning of a user name.
4598 * 2 is name fully matches a user name.
4599 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004600int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004601{
4602 int i;
4603 int n = (int)STRLEN(name);
4604 int result = 0;
4605
4606 init_users();
4607 for (i = 0; i < ga_users.ga_len; i++)
4608 {
4609 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4610 return 2; /* full match */
4611 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4612 result = 1; /* partial match */
4613 }
4614 return result;
4615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616#endif
4617
4618/*
4619 * Replace home directory by "~" in each space or comma separated file name in
4620 * 'src'.
4621 * If anything fails (except when out of space) dst equals src.
4622 */
4623 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004624home_replace(
4625 buf_T *buf, /* when not NULL, check for help files */
4626 char_u *src, /* input file name */
4627 char_u *dst, /* where to put the result */
4628 int dstlen, /* maximum length of the result */
4629 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 spaces and commas in the file name. */
4631{
4632 size_t dirlen = 0, envlen = 0;
4633 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004634 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 char_u *p;
4636
4637 if (src == NULL)
4638 {
4639 *dst = NUL;
4640 return;
4641 }
4642
4643 /*
4644 * If the file is a help file, remove the path completely.
4645 */
4646 if (buf != NULL && buf->b_help)
4647 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004648 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return;
4650 }
4651
4652 /*
4653 * We check both the value of the $HOME environment variable and the
4654 * "real" home directory.
4655 */
4656 if (homedir != NULL)
4657 dirlen = STRLEN(homedir);
4658
4659#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004660 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004662 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4663#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004664#ifdef WIN3264
4665 if (homedir_env == NULL)
4666 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4667#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004668 /* Empty is the same as not set. */
4669 if (homedir_env != NULL && *homedir_env == NUL)
4670 homedir_env = NULL;
4671
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004672#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004673 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004674 {
4675 int usedlen = 0;
4676 int flen;
4677 char_u *fbuf = NULL;
4678
4679 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004680 (void)modify_fname((char_u *)":p", &usedlen,
4681 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004682 flen = (int)STRLEN(homedir_env);
4683 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4684 /* Remove the trailing / that is added to a directory. */
4685 homedir_env[flen - 1] = NUL;
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#endif
4688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (homedir_env != NULL)
4690 envlen = STRLEN(homedir_env);
4691
4692 if (!one)
4693 src = skipwhite(src);
4694 while (*src && dstlen > 0)
4695 {
4696 /*
4697 * Here we are at the beginning of a file name.
4698 * First, check to see if the beginning of the file name matches
4699 * $HOME or the "real" home directory. Check that there is a '/'
4700 * after the match (so that if e.g. the file is "/home/pieter/bla",
4701 * and the home directory is "/home/piet", the file does not end up
4702 * as "~er/bla" (which would seem to indicate the file "bla" in user
4703 * er's home directory)).
4704 */
4705 p = homedir;
4706 len = dirlen;
4707 for (;;)
4708 {
4709 if ( len
4710 && fnamencmp(src, p, len) == 0
4711 && (vim_ispathsep(src[len])
4712 || (!one && (src[len] == ',' || src[len] == ' '))
4713 || src[len] == NUL))
4714 {
4715 src += len;
4716 if (--dstlen > 0)
4717 *dst++ = '~';
4718
4719 /*
4720 * If it's just the home directory, add "/".
4721 */
4722 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4723 *dst++ = '/';
4724 break;
4725 }
4726 if (p == homedir_env)
4727 break;
4728 p = homedir_env;
4729 len = envlen;
4730 }
4731
4732 /* if (!one) skip to separator: space or comma */
4733 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4734 *dst++ = *src++;
4735 /* skip separator */
4736 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4737 *dst++ = *src++;
4738 }
4739 /* if (dstlen == 0) out of space, what to do??? */
4740
4741 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004742
4743 if (homedir_env != homedir_env_orig)
4744 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745}
4746
4747/*
4748 * Like home_replace, store the replaced string in allocated memory.
4749 * When something fails, NULL is returned.
4750 */
4751 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004752home_replace_save(
4753 buf_T *buf, /* when not NULL, check for help files */
4754 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 char_u *dst;
4757 unsigned len;
4758
4759 len = 3; /* space for "~/" and trailing NUL */
4760 if (src != NULL) /* just in case */
4761 len += (unsigned)STRLEN(src);
4762 dst = alloc(len);
4763 if (dst != NULL)
4764 home_replace(buf, src, dst, len, TRUE);
4765 return dst;
4766}
4767
4768/*
4769 * Compare two file names and return:
4770 * FPC_SAME if they both exist and are the same file.
4771 * FPC_SAMEX if they both don't exist and have the same file name.
4772 * FPC_DIFF if they both exist and are different files.
4773 * FPC_NOTX if they both don't exist.
4774 * FPC_DIFFX if one of them doesn't exist.
4775 * For the first name environment variables are expanded
4776 */
4777 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004778fullpathcmp(
4779 char_u *s1,
4780 char_u *s2,
4781 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783#ifdef UNIX
4784 char_u exp1[MAXPATHL];
4785 char_u full1[MAXPATHL];
4786 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004787 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 int r1, r2;
4789
4790 expand_env(s1, exp1, MAXPATHL);
4791 r1 = mch_stat((char *)exp1, &st1);
4792 r2 = mch_stat((char *)s2, &st2);
4793 if (r1 != 0 && r2 != 0)
4794 {
4795 /* if mch_stat() doesn't work, may compare the names */
4796 if (checkname)
4797 {
4798 if (fnamecmp(exp1, s2) == 0)
4799 return FPC_SAMEX;
4800 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4801 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4802 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4803 return FPC_SAMEX;
4804 }
4805 return FPC_NOTX;
4806 }
4807 if (r1 != 0 || r2 != 0)
4808 return FPC_DIFFX;
4809 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4810 return FPC_SAME;
4811 return FPC_DIFF;
4812#else
4813 char_u *exp1; /* expanded s1 */
4814 char_u *full1; /* full path of s1 */
4815 char_u *full2; /* full path of s2 */
4816 int retval = FPC_DIFF;
4817 int r1, r2;
4818
4819 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4820 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4821 {
4822 full1 = exp1 + MAXPATHL;
4823 full2 = full1 + MAXPATHL;
4824
4825 expand_env(s1, exp1, MAXPATHL);
4826 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4827 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4828
4829 /* If vim_FullName() fails, the file probably doesn't exist. */
4830 if (r1 != OK && r2 != OK)
4831 {
4832 if (checkname && fnamecmp(exp1, s2) == 0)
4833 retval = FPC_SAMEX;
4834 else
4835 retval = FPC_NOTX;
4836 }
4837 else if (r1 != OK || r2 != OK)
4838 retval = FPC_DIFFX;
4839 else if (fnamecmp(full1, full2))
4840 retval = FPC_DIFF;
4841 else
4842 retval = FPC_SAME;
4843 vim_free(exp1);
4844 }
4845 return retval;
4846#endif
4847}
4848
4849/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004850 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004851 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004852 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
4854 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004855gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
4857 char_u *p1, *p2;
4858
4859 if (fname == NULL)
4860 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004861 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004863 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004865 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 return p1;
4868}
4869
Bram Moolenaar31710262010-08-13 13:36:15 +02004870#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004871static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004872
4873/*
4874 * Return the end of the directory name, on the first path
4875 * separator:
4876 * "/path/file", "/path/dir/", "/path//dir", "/file"
4877 * ^ ^ ^ ^
4878 */
4879 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004880gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004881{
4882 char_u *dir_end = fname;
4883 char_u *next_dir_end = fname;
4884 int look_for_sep = TRUE;
4885 char_u *p;
4886
4887 for (p = fname; *p != NUL; )
4888 {
4889 if (vim_ispathsep(*p))
4890 {
4891 if (look_for_sep)
4892 {
4893 next_dir_end = p;
4894 look_for_sep = FALSE;
4895 }
4896 }
4897 else
4898 {
4899 if (!look_for_sep)
4900 dir_end = next_dir_end;
4901 look_for_sep = TRUE;
4902 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004903 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004904 }
4905 return dir_end;
4906}
4907#endif
4908
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004910 * Get pointer to tail of "fname", including path separators. Putting a NUL
4911 * here leaves the directory name. Takes care of "c:/" and "//".
4912 * Always returns a valid pointer.
4913 */
4914 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004915gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004916{
4917 char_u *p;
4918 char_u *t;
4919
4920 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4921 t = gettail(fname);
4922 while (t > p && after_pathsep(fname, t))
4923 --t;
4924#ifdef VMS
4925 /* path separator is part of the path */
4926 ++t;
4927#endif
4928 return t;
4929}
4930
4931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 * get the next path component (just after the next path separator).
4933 */
4934 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004935getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004938 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (*fname)
4940 ++fname;
4941 return fname;
4942}
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944/*
4945 * Get a pointer to one character past the head of a path name.
4946 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4947 * If there is no head, path is returned.
4948 */
4949 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004950get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 char_u *retval;
4953
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004954#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /* may skip "c:" */
4956 if (isalpha(path[0]) && path[1] == ':')
4957 retval = path + 2;
4958 else
4959 retval = path;
4960#else
4961# if defined(AMIGA)
4962 /* may skip "label:" */
4963 retval = vim_strchr(path, ':');
4964 if (retval == NULL)
4965 retval = path;
4966# else /* Unix */
4967 retval = path;
4968# endif
4969#endif
4970
4971 while (vim_ispathsep(*retval))
4972 ++retval;
4973
4974 return retval;
4975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004978 * Return TRUE if 'c' is a path separator.
4979 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
4981 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004982vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004984#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004986#else
4987# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004989# else
4990# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4992 return (c == ':' || c == '[' || c == ']' || c == '/'
4993 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004994# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004996# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999}
5000
Bram Moolenaar69c35002013-11-04 02:54:12 +01005001/*
5002 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5003 */
5004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005005vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005006{
5007 return vim_ispathsep(c)
5008#ifdef BACKSLASH_IN_FILENAME
5009 && c != ':'
5010#endif
5011 ;
5012}
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5015/*
5016 * return TRUE if 'c' is a path list separator.
5017 */
5018 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005019vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021#ifdef UNIX
5022 return (c == ':');
5023#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005024 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#endif
5026}
5027#endif
5028
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005029#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5030 || defined(FEAT_EVAL) || defined(PROTO)
5031/*
5032 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5033 * It's done in-place.
5034 */
5035 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005036shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005037{
5038 char_u *tail, *s, *d;
5039 int skip = FALSE;
5040
5041 tail = gettail(str);
5042 d = str;
5043 for (s = str; ; ++s)
5044 {
5045 if (s >= tail) /* copy the whole tail */
5046 {
5047 *d++ = *s;
5048 if (*s == NUL)
5049 break;
5050 }
5051 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5052 {
5053 *d++ = *s;
5054 skip = FALSE;
5055 }
5056 else if (!skip)
5057 {
5058 *d++ = *s; /* copy next char */
5059 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5060 skip = TRUE;
5061# ifdef FEAT_MBYTE
5062 if (has_mbyte)
5063 {
5064 int l = mb_ptr2len(s);
5065
5066 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005067 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005068 }
5069# endif
5070 }
5071 }
5072}
5073#endif
5074
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005075/*
5076 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5077 * Also returns TRUE if there is no directory name.
5078 * "fname" must be writable!.
5079 */
5080 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005082{
5083 char_u *p;
5084 int c;
5085 int retval;
5086
5087 p = gettail_sep(fname);
5088 if (p == fname)
5089 return TRUE;
5090 c = *p;
5091 *p = NUL;
5092 retval = mch_isdir(fname);
5093 *p = c;
5094 return retval;
5095}
5096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005098 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5099 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
5101 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005102vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005104#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005106#else
5107 if (p_fic)
5108 return MB_STRICMP(x, y);
5109 return STRCMP(x, y);
5110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111}
5112
5113 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005114vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005116#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005117 char_u *px = x;
5118 char_u *py = y;
5119 int cx = NUL;
5120 int cy = NUL;
5121
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005122 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005124 cx = PTR2CHAR(px);
5125 cy = PTR2CHAR(py);
5126 if (cx == NUL || cy == NUL
5127 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5128 && !(cx == '/' && cy == '\\')
5129 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005131 len -= MB_PTR2LEN(px);
5132 px += MB_PTR2LEN(px);
5133 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 if (len == 0)
5136 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005137 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005138#else
5139 if (p_fic)
5140 return MB_STRNICMP(x, y, len);
5141 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145/*
5146 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005147 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 */
5149 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005150concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 char_u *dest;
5153
5154 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5155 if (dest != NULL)
5156 {
5157 STRCPY(dest, fname1);
5158 if (sep)
5159 add_pathsep(dest);
5160 STRCAT(dest, fname2);
5161 }
5162 return dest;
5163}
5164
Bram Moolenaard6754642005-01-17 22:18:45 +00005165/*
5166 * Concatenate two strings and return the result in allocated memory.
5167 * Returns NULL when out of memory.
5168 */
5169 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005171{
5172 char_u *dest;
5173 size_t l = STRLEN(str1);
5174
5175 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5176 if (dest != NULL)
5177 {
5178 STRCPY(dest, str1);
5179 STRCPY(dest + l, str2);
5180 }
5181 return dest;
5182}
Bram Moolenaard6754642005-01-17 22:18:45 +00005183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184/*
5185 * Add a path separator to a file name, unless it already ends in a path
5186 * separator.
5187 */
5188 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005189add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005191 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 STRCAT(p, PATHSEPSTR);
5193}
5194
5195/*
5196 * FullName_save - Make an allocated copy of a full file name.
5197 * Returns NULL when out of memory.
5198 */
5199 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005200FullName_save(
5201 char_u *fname,
5202 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005203 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204{
5205 char_u *buf;
5206 char_u *new_fname = NULL;
5207
5208 if (fname == NULL)
5209 return NULL;
5210
5211 buf = alloc((unsigned)MAXPATHL);
5212 if (buf != NULL)
5213 {
5214 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5215 new_fname = vim_strsave(buf);
5216 else
5217 new_fname = vim_strsave(fname);
5218 vim_free(buf);
5219 }
5220 return new_fname;
5221}
5222
5223#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5224
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005225static char_u *skip_string(char_u *p);
5226static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005227static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005228static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230/*
5231 * Find the start of a comment, not knowing if we are in a comment right now.
5232 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005233 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005235 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005236ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005237{
5238 return find_start_comment(curbuf->b_ind_maxcomment);
5239}
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005242find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 pos_T *pos;
5245 char_u *line;
5246 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005247 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005249 for (;;)
5250 {
5251 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5252 if (pos == NULL)
5253 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005255 /*
5256 * Check if the comment start we found is inside a string.
5257 * If it is then restrict the search to below this line and try again.
5258 */
5259 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005260 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005261 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005262 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005263 break;
5264 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5265 if (cur_maxcomment <= 0)
5266 {
5267 pos = NULL;
5268 break;
5269 }
5270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 return pos;
5272}
5273
5274/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005275 * Find the start of a comment or raw string, not knowing if we are in a
5276 * comment or raw string right now.
5277 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005278 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005279 * Return NULL when not inside a comment or raw string.
5280 * "CORS" -> Comment Or Raw String
5281 */
5282 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005283ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005284{
Bram Moolenaar089af182015-10-07 11:41:49 +02005285 static pos_T comment_pos_copy;
5286 pos_T *comment_pos;
5287 pos_T *rs_pos;
5288
5289 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5290 if (comment_pos != NULL)
5291 {
5292 /* Need to make a copy of the static pos in findmatchlimit(),
5293 * calling find_start_rawstring() may change it. */
5294 comment_pos_copy = *comment_pos;
5295 comment_pos = &comment_pos_copy;
5296 }
5297 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005298
5299 /* If comment_pos is before rs_pos the raw string is inside the comment.
5300 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005301 if (comment_pos == NULL || (rs_pos != NULL
5302 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005303 {
5304 if (is_raw != NULL && rs_pos != NULL)
5305 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005306 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005307 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005308 return comment_pos;
5309}
5310
5311/*
5312 * Find the start of a raw string, not knowing if we are in one right now.
5313 * Search starts at w_cursor.lnum and goes backwards.
5314 * Return NULL when not inside a raw string.
5315 */
5316 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005317find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005318{
5319 pos_T *pos;
5320 char_u *line;
5321 char_u *p;
5322 int cur_maxcomment = ind_maxcomment;
5323
5324 for (;;)
5325 {
5326 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5327 if (pos == NULL)
5328 break;
5329
5330 /*
5331 * Check if the raw string start we found is inside a string.
5332 * If it is then restrict the search to below this line and try again.
5333 */
5334 line = ml_get(pos->lnum);
5335 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5336 p = skip_string(p);
5337 if ((colnr_T)(p - line) <= pos->col)
5338 break;
5339 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5340 if (cur_maxcomment <= 0)
5341 {
5342 pos = NULL;
5343 break;
5344 }
5345 }
5346 return pos;
5347}
5348
5349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 * Skip to the end of a "string" and a 'c' character.
5351 * If there is no string or character, return argument unmodified.
5352 */
5353 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005354skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 int i;
5357
5358 /*
5359 * We loop, because strings may be concatenated: "date""time".
5360 */
5361 for ( ; ; ++p)
5362 {
5363 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5364 {
5365 if (!p[1]) /* ' at end of line */
5366 break;
5367 i = 2;
5368 if (p[1] == '\\') /* '\n' or '\000' */
5369 {
5370 ++i;
5371 while (vim_isdigit(p[i - 1])) /* '\000' */
5372 ++i;
5373 }
5374 if (p[i] == '\'') /* check for trailing ' */
5375 {
5376 p += i;
5377 continue;
5378 }
5379 }
5380 else if (p[0] == '"') /* start of string */
5381 {
5382 for (++p; p[0]; ++p)
5383 {
5384 if (p[0] == '\\' && p[1] != NUL)
5385 ++p;
5386 else if (p[0] == '"') /* end of string */
5387 break;
5388 }
5389 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005390 continue; /* continue for another string */
5391 }
5392 else if (p[0] == 'R' && p[1] == '"')
5393 {
5394 /* Raw string: R"[delim](...)[delim]" */
5395 char_u *delim = p + 2;
5396 char_u *paren = vim_strchr(delim, '(');
5397
5398 if (paren != NULL)
5399 {
5400 size_t delim_len = paren - delim;
5401
5402 for (p += 3; *p; ++p)
5403 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5404 && p[delim_len + 1] == '"')
5405 {
5406 p += delim_len + 1;
5407 break;
5408 }
5409 if (p[0] == '"')
5410 continue; /* continue for another string */
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413 break; /* no string found */
5414 }
5415 if (!*p)
5416 --p; /* backup from NUL */
5417 return p;
5418}
5419#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5420
5421#if defined(FEAT_CINDENT) || defined(PROTO)
5422
5423/*
5424 * Do C or expression indenting on the current line.
5425 */
5426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005427do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429# ifdef FEAT_EVAL
5430 if (*curbuf->b_p_inde != NUL)
5431 fixthisline(get_expr_indent);
5432 else
5433# endif
5434 fixthisline(get_c_indent);
5435}
5436
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005437/* Find result cache for cpp_baseclass */
5438typedef struct {
5439 int found;
5440 lpos_T lpos;
5441} cpp_baseclass_cache_T;
5442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443/*
5444 * Functions for C-indenting.
5445 * Most of this originally comes from Eric Fischer.
5446 */
5447/*
5448 * Below "XXX" means that this function may unlock the current line.
5449 */
5450
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005451static char_u *cin_skipcomment(char_u *);
5452static int cin_nocode(char_u *);
5453static pos_T *find_line_comment(void);
5454static int cin_has_js_key(char_u *text);
5455static int cin_islabel_skip(char_u **);
5456static int cin_isdefault(char_u *);
5457static char_u *after_label(char_u *l);
5458static int get_indent_nolabel(linenr_T lnum);
5459static int skip_label(linenr_T, char_u **pp);
5460static int cin_first_id_amount(void);
5461static int cin_get_equal_amount(linenr_T lnum);
5462static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005463static int cin_iscomment(char_u *);
5464static int cin_islinecomment(char_u *);
5465static int cin_isterminated(char_u *, int, int);
5466static int cin_isinit(void);
5467static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5468static int cin_isif(char_u *);
5469static int cin_iselse(char_u *);
5470static int cin_isdo(char_u *);
5471static int cin_iswhileofdo(char_u *, linenr_T);
5472static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5473static int cin_iswhileofdo_end(int terminated);
5474static int cin_isbreak(char_u *);
5475static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5476static int get_baseclass_amount(int col);
5477static int cin_ends_in(char_u *, char_u *, char_u *);
5478static int cin_starts_with(char_u *s, char *word);
5479static int cin_skip2pos(pos_T *trypos);
5480static pos_T *find_start_brace(void);
5481static pos_T *find_match_paren(int);
5482static pos_T *find_match_char(int c, int ind_maxparen);
5483static int corr_ind_maxparen(pos_T *startpos);
5484static int find_last_paren(char_u *l, int start, int end);
5485static int find_match(int lookfor, linenr_T ourscope);
5486static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488/*
5489 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005490 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 */
5492 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005493cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 while (*s)
5496 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005497 char_u *prev_s = s;
5498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005500
5501 /* Perl/shell # comment comment continues until eol. Require a space
5502 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005503 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005504 {
5505 s += STRLEN(s);
5506 break;
5507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (*s != '/')
5509 break;
5510 ++s;
5511 if (*s == '/') /* slash-slash comment continues till eol */
5512 {
5513 s += STRLEN(s);
5514 break;
5515 }
5516 if (*s != '*')
5517 break;
5518 for (++s; *s; ++s) /* skip slash-star comment */
5519 if (s[0] == '*' && s[1] == '/')
5520 {
5521 s += 2;
5522 break;
5523 }
5524 }
5525 return s;
5526}
5527
5528/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005529 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 * not considered code.
5531 */
5532 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005533cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 return *cin_skipcomment(s) == NUL;
5536}
5537
5538/*
5539 * Check previous lines for a "//" line comment, skipping over blank lines.
5540 */
5541 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005542find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 static pos_T pos;
5545 char_u *line;
5546 char_u *p;
5547
5548 pos = curwin->w_cursor;
5549 while (--pos.lnum > 0)
5550 {
5551 line = ml_get(pos.lnum);
5552 p = skipwhite(line);
5553 if (cin_islinecomment(p))
5554 {
5555 pos.col = (int)(p - line);
5556 return &pos;
5557 }
5558 if (*p != NUL)
5559 break;
5560 }
5561 return NULL;
5562}
5563
5564/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005565 * Return TRUE if "text" starts with "key:".
5566 */
5567 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005568cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005569{
5570 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005571 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005572
5573 if (*s == '\'' || *s == '"')
5574 {
5575 /* can be 'key': or "key": */
5576 quote = *s;
5577 ++s;
5578 }
5579 if (!vim_isIDc(*s)) /* need at least one ID character */
5580 return FALSE;
5581
5582 while (vim_isIDc(*s))
5583 ++s;
5584 if (*s == quote)
5585 ++s;
5586
5587 s = cin_skipcomment(s);
5588
5589 /* "::" is not a label, it's C++ */
5590 return (*s == ':' && s[1] != ':');
5591}
5592
5593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005595 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 */
5597 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005598cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 if (!vim_isIDc(**s)) /* need at least one ID character */
5601 return FALSE;
5602
5603 while (vim_isIDc(**s))
5604 (*s)++;
5605
5606 *s = cin_skipcomment(*s);
5607
5608 /* "::" is not a label, it's C++ */
5609 return (**s == ':' && *++*s != ':');
5610}
5611
5612/*
5613 * Recognize a label: "label:".
5614 * Note: curwin->w_cursor must be where we are looking for the label.
5615 */
5616 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005617cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 char_u *s;
5620
5621 s = cin_skipcomment(ml_get_curline());
5622
5623 /*
5624 * Exclude "default" from labels, since it should be indented
5625 * like a switch label. Same for C++ scope declarations.
5626 */
5627 if (cin_isdefault(s))
5628 return FALSE;
5629 if (cin_isscopedecl(s))
5630 return FALSE;
5631
5632 if (cin_islabel_skip(&s))
5633 {
5634 /*
5635 * Only accept a label if the previous line is terminated or is a case
5636 * label.
5637 */
5638 pos_T cursor_save;
5639 pos_T *trypos;
5640 char_u *line;
5641
5642 cursor_save = curwin->w_cursor;
5643 while (curwin->w_cursor.lnum > 1)
5644 {
5645 --curwin->w_cursor.lnum;
5646
5647 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005648 * If we're in a comment or raw string now, skip to the start of
5649 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 */
5651 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005652 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 curwin->w_cursor = *trypos;
5654
5655 line = ml_get_curline();
5656 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5657 continue;
5658 if (*(line = cin_skipcomment(line)) == NUL)
5659 continue;
5660
5661 curwin->w_cursor = cursor_save;
5662 if (cin_isterminated(line, TRUE, FALSE)
5663 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005664 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 || (cin_islabel_skip(&line) && cin_nocode(line)))
5666 return TRUE;
5667 return FALSE;
5668 }
5669 curwin->w_cursor = cursor_save;
5670 return TRUE; /* label at start of file??? */
5671 }
5672 return FALSE;
5673}
5674
5675/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005676 * Recognize structure initialization and enumerations:
5677 * "[typedef] [static|public|protected|private] enum"
5678 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 */
5680 static int
5681cin_isinit(void)
5682{
5683 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005684 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686 s = cin_skipcomment(ml_get_curline());
5687
Bram Moolenaar75342212013-03-07 13:13:52 +01005688 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 s = cin_skipcomment(s + 7);
5690
Bram Moolenaar75342212013-03-07 13:13:52 +01005691 for (;;)
5692 {
5693 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005694
Bram Moolenaar75342212013-03-07 13:13:52 +01005695 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5696 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005697 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005698 if (cin_starts_with(s, skip[i]))
5699 {
5700 s = cin_skipcomment(s + l);
5701 l = 0;
5702 break;
5703 }
5704 }
5705 if (l != 0)
5706 break;
5707 }
5708
5709 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 return TRUE;
5711
5712 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5713 return TRUE;
5714
5715 return FALSE;
5716}
5717
5718/*
5719 * Recognize a switch label: "case .*:" or "default:".
5720 */
5721 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005722cin_iscase(
5723 char_u *s,
5724 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005727 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 for (s += 4; *s; ++s)
5730 {
5731 s = cin_skipcomment(s);
5732 if (*s == ':')
5733 {
5734 if (s[1] == ':') /* skip over "::" for C++ */
5735 ++s;
5736 else
5737 return TRUE;
5738 }
5739 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005740 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5742 return FALSE; /* stop at comment */
5743 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005744 {
5745 /* JS etc. */
5746 if (strict)
5747 return FALSE; /* stop at string */
5748 else
5749 return TRUE;
5750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 return FALSE;
5753 }
5754
5755 if (cin_isdefault(s))
5756 return TRUE;
5757 return FALSE;
5758}
5759
5760/*
5761 * Recognize a "default" switch label.
5762 */
5763 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005764cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 return (STRNCMP(s, "default", 7) == 0
5767 && *(s = cin_skipcomment(s + 7)) == ':'
5768 && s[1] != ':');
5769}
5770
5771/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005772 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 */
5774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005775cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 int i;
5778
5779 s = cin_skipcomment(s);
5780 if (STRNCMP(s, "public", 6) == 0)
5781 i = 6;
5782 else if (STRNCMP(s, "protected", 9) == 0)
5783 i = 9;
5784 else if (STRNCMP(s, "private", 7) == 0)
5785 i = 7;
5786 else
5787 return FALSE;
5788 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5789}
5790
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005791/* Maximum number of lines to search back for a "namespace" line. */
5792#define FIND_NAMESPACE_LIM 20
5793
5794/*
5795 * Recognize a "namespace" scope declaration.
5796 */
5797 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005798cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005799{
5800 char_u *p;
5801 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005802 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005803
5804 s = cin_skipcomment(s);
5805 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5806 {
5807 p = cin_skipcomment(skipwhite(s + 9));
5808 while (*p != NUL)
5809 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005810 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005811 {
5812 has_name = TRUE; /* found end of a name */
5813 p = cin_skipcomment(skipwhite(p));
5814 }
5815 else if (*p == '{')
5816 {
5817 break;
5818 }
5819 else if (vim_iswordc(*p))
5820 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005821 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005822 if (has_name)
5823 return FALSE; /* word character after skipping past name */
5824 ++p;
5825 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005826 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5827 {
5828 if (!has_name_start || has_name)
5829 return FALSE;
5830 /* C++ 17 nested namespace */
5831 p += 3;
5832 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005833 else
5834 {
5835 return FALSE;
5836 }
5837 }
5838 return TRUE;
5839 }
5840 return FALSE;
5841}
5842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005844 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5845 */
5846 static int
5847cin_is_cpp_extern_c(char_u *s)
5848{
5849 char_u *p;
5850 int has_string_literal = FALSE;
5851
5852 s = cin_skipcomment(s);
5853 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5854 {
5855 p = cin_skipcomment(skipwhite(s + 6));
5856 while (*p != NUL)
5857 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005858 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005859 {
5860 p = cin_skipcomment(skipwhite(p));
5861 }
5862 else if (*p == '{')
5863 {
5864 break;
5865 }
5866 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5867 {
5868 if (has_string_literal)
5869 return FALSE;
5870 has_string_literal = TRUE;
5871 p += 3;
5872 }
5873 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5874 && p[4] == '"')
5875 {
5876 if (has_string_literal)
5877 return FALSE;
5878 has_string_literal = TRUE;
5879 p += 5;
5880 }
5881 else
5882 {
5883 return FALSE;
5884 }
5885 }
5886 return has_string_literal ? TRUE : FALSE;
5887 }
5888 return FALSE;
5889}
5890
5891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 * Return a pointer to the first non-empty non-comment character after a ':'.
5893 * Return NULL if not found.
5894 * case 234: a = b;
5895 * ^
5896 */
5897 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005898after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
5900 for ( ; *l; ++l)
5901 {
5902 if (*l == ':')
5903 {
5904 if (l[1] == ':') /* skip over "::" for C++ */
5905 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005906 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 break;
5908 }
5909 else if (*l == '\'' && l[1] && l[2] == '\'')
5910 l += 2; /* skip over 'x' */
5911 }
5912 if (*l == NUL)
5913 return NULL;
5914 l = cin_skipcomment(l + 1);
5915 if (*l == NUL)
5916 return NULL;
5917 return l;
5918}
5919
5920/*
5921 * Get indent of line "lnum", skipping a label.
5922 * Return 0 if there is nothing after the label.
5923 */
5924 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005925get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 char_u *l;
5928 pos_T fp;
5929 colnr_T col;
5930 char_u *p;
5931
5932 l = ml_get(lnum);
5933 p = after_label(l);
5934 if (p == NULL)
5935 return 0;
5936
5937 fp.col = (colnr_T)(p - l);
5938 fp.lnum = lnum;
5939 getvcol(curwin, &fp, &col, NULL, NULL);
5940 return (int)col;
5941}
5942
5943/*
5944 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005945 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 * label: if (asdf && asdfasdf)
5947 * ^
5948 */
5949 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005950skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952 char_u *l;
5953 int amount;
5954 pos_T cursor_save;
5955
5956 cursor_save = curwin->w_cursor;
5957 curwin->w_cursor.lnum = lnum;
5958 l = ml_get_curline();
5959 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005960 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
5962 amount = get_indent_nolabel(lnum);
5963 l = after_label(ml_get_curline());
5964 if (l == NULL) /* just in case */
5965 l = ml_get_curline();
5966 }
5967 else
5968 {
5969 amount = get_indent();
5970 l = ml_get_curline();
5971 }
5972 *pp = l;
5973
5974 curwin->w_cursor = cursor_save;
5975 return amount;
5976}
5977
5978/*
5979 * Return the indent of the first variable name after a type in a declaration.
5980 * int a, indent of "a"
5981 * static struct foo b, indent of "b"
5982 * enum bla c, indent of "c"
5983 * Returns zero when it doesn't look like a declaration.
5984 */
5985 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005986cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
5988 char_u *line, *p, *s;
5989 int len;
5990 pos_T fp;
5991 colnr_T col;
5992
5993 line = ml_get_curline();
5994 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005995 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5997 {
5998 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005999 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6002 p = skipwhite(p + 6);
6003 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6004 p = skipwhite(p + 4);
6005 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6006 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6007 {
6008 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006009 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6010 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6011 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6012 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 p = s;
6014 }
6015 for (len = 0; vim_isIDc(p[len]); ++len)
6016 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006017 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 return 0;
6019
6020 p = skipwhite(p + len);
6021 fp.lnum = curwin->w_cursor.lnum;
6022 fp.col = (colnr_T)(p - line);
6023 getvcol(curwin, &fp, &col, NULL, NULL);
6024 return (int)col;
6025}
6026
6027/*
6028 * Return the indent of the first non-blank after an equal sign.
6029 * char *foo = "here";
6030 * Return zero if no (useful) equal sign found.
6031 * Return -1 if the line above "lnum" ends in a backslash.
6032 * foo = "asdf\
6033 * asdf\
6034 * here";
6035 */
6036 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006037cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
6039 char_u *line;
6040 char_u *s;
6041 colnr_T col;
6042 pos_T fp;
6043
6044 if (lnum > 1)
6045 {
6046 line = ml_get(lnum - 1);
6047 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6048 return -1;
6049 }
6050
6051 line = s = ml_get(lnum);
6052 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6053 {
6054 if (cin_iscomment(s)) /* ignore comments */
6055 s = cin_skipcomment(s);
6056 else
6057 ++s;
6058 }
6059 if (*s != '=')
6060 return 0;
6061
6062 s = skipwhite(s + 1);
6063 if (cin_nocode(s))
6064 return 0;
6065
6066 if (*s == '"') /* nice alignment for continued strings */
6067 ++s;
6068
6069 fp.lnum = lnum;
6070 fp.col = (colnr_T)(s - line);
6071 getvcol(curwin, &fp, &col, NULL, NULL);
6072 return (int)col;
6073}
6074
6075/*
6076 * Recognize a preprocessor statement: Any line that starts with '#'.
6077 */
6078 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006079cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006081 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 return TRUE;
6083 return FALSE;
6084}
6085
6086/*
6087 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6088 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6089 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006090 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 */
6092 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006093cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 char_u *line = *pp;
6096 linenr_T lnum = *lnump;
6097 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006098 int candidate_amount = *amount;
6099
6100 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6101 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006103 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 {
6105 if (cin_ispreproc(line))
6106 {
6107 retval = TRUE;
6108 *lnump = lnum;
6109 break;
6110 }
6111 if (lnum == 1)
6112 break;
6113 line = ml_get(--lnum);
6114 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6115 break;
6116 }
6117
6118 if (lnum != *lnump)
6119 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006120 if (retval)
6121 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 return retval;
6123}
6124
6125/*
6126 * Recognize the start of a C or C++ comment.
6127 */
6128 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006129cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6132}
6133
6134/*
6135 * Recognize the start of a "//" comment.
6136 */
6137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006138cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 return (p[0] == '/' && p[1] == '/');
6141}
6142
6143/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006144 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6145 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006147 * If a line begins with an "else", only consider it terminated if no unmatched
6148 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 * Return the character terminating the line (ending char's have precedence if
6150 * both apply in order to determine initializations).
6151 */
6152 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006153cin_isterminated(
6154 char_u *s,
6155 int incl_open, /* include '{' at the end as terminator */
6156 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006158 char_u found_start = 0;
6159 unsigned n_open = 0;
6160 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 s = cin_skipcomment(s);
6163
6164 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6165 found_start = *s;
6166
Bram Moolenaar496f9512011-05-19 16:35:09 +02006167 if (!found_start)
6168 is_else = cin_iselse(s);
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 while (*s)
6171 {
6172 /* skip over comments, "" strings and 'c'haracters */
6173 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006174 if (*s == '}' && n_open > 0)
6175 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006176 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006177 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 && cin_nocode(s + 1))
6179 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006180 else if (*s == '{')
6181 {
6182 if (incl_open && cin_nocode(s + 1))
6183 return *s;
6184 else
6185 ++n_open;
6186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 if (*s)
6189 s++;
6190 }
6191 return found_start;
6192}
6193
6194/*
6195 * Recognize the basic picture of a function declaration -- it needs to
6196 * have an open paren somewhere and a close paren at the end of the line and
6197 * no semicolons anywhere.
6198 * When a line ends in a comma we continue looking in the next line.
6199 * "sp" points to a string with the line. When looking at other lines it must
6200 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006201 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006202 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 */
6204 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006205cin_isfuncdecl(
6206 char_u **sp,
6207 linenr_T first_lnum,
6208 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 char_u *s;
6211 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006212 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006214 pos_T *trypos;
6215 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217 if (sp == NULL)
6218 s = ml_get(lnum);
6219 else
6220 s = *sp;
6221
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006222 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006223 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006224 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006225 {
6226 lnum = trypos->lnum;
6227 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006228 {
6229 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006230 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006231 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006232
6233 s = ml_get(lnum);
6234 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006235 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006236
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006237 /* Ignore line starting with #. */
6238 if (cin_ispreproc(s))
6239 return FALSE;
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6242 {
6243 if (cin_iscomment(s)) /* ignore comments */
6244 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006245 else if (*s == ':')
6246 {
6247 if (*(s + 1) == ':')
6248 s += 2;
6249 else
6250 /* To avoid a mistake in the following situation:
6251 * A::A(int a, int b)
6252 * : a(0) // <--not a function decl
6253 * , b(0)
6254 * {...
6255 */
6256 return FALSE;
6257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 else
6259 ++s;
6260 }
6261 if (*s != '(')
6262 return FALSE; /* ';', ' or " before any () or no '(' */
6263
6264 while (*s && *s != ';' && *s != '\'' && *s != '"')
6265 {
6266 if (*s == ')' && cin_nocode(s + 1))
6267 {
6268 /* ')' at the end: may have found a match
6269 * Check for he previous line not to end in a backslash:
6270 * #if defined(x) && \
6271 * defined(y)
6272 */
6273 lnum = first_lnum - 1;
6274 s = ml_get(lnum);
6275 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6276 retval = TRUE;
6277 goto done;
6278 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006279 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006281 int comma = (*s == ',');
6282
6283 /* ',' at the end: continue looking in the next line.
6284 * At the end: check for ',' in the next line, for this style:
6285 * func(arg1
6286 * , arg2) */
6287 for (;;)
6288 {
6289 if (lnum >= curbuf->b_ml.ml_line_count)
6290 break;
6291 s = ml_get(++lnum);
6292 if (!cin_ispreproc(s))
6293 break;
6294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (lnum >= curbuf->b_ml.ml_line_count)
6296 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006297 /* Require a comma at end of the line or a comma or ')' at the
6298 * start of next line. */
6299 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006300 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006301 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006302 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 }
6304 else if (cin_iscomment(s)) /* ignore comments */
6305 s = cin_skipcomment(s);
6306 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006309 just_started = FALSE;
6310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
6312
6313done:
6314 if (lnum != first_lnum && sp != NULL)
6315 *sp = ml_get(first_lnum);
6316
6317 return retval;
6318}
6319
6320 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006321cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006323 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324}
6325
6326 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006327cin_iselse(
6328 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 if (*p == '}') /* accept "} else" */
6331 p = cin_skipcomment(p + 1);
6332 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6333}
6334
6335 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006336cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
6338 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6339}
6340
6341/*
6342 * Check if this is a "while" that should have a matching "do".
6343 * We only accept a "while (condition) ;", with only white space between the
6344 * ')' and ';'. The condition may be spread over several lines.
6345 */
6346 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006347cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
6349 pos_T cursor_save;
6350 pos_T *trypos;
6351 int retval = FALSE;
6352
6353 p = cin_skipcomment(p);
6354 if (*p == '}') /* accept "} while (cond);" */
6355 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006356 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358 cursor_save = curwin->w_cursor;
6359 curwin->w_cursor.lnum = lnum;
6360 curwin->w_cursor.col = 0;
6361 p = ml_get_curline();
6362 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6363 {
6364 ++p;
6365 ++curwin->w_cursor.col;
6366 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006367 if ((trypos = findmatchlimit(NULL, 0, 0,
6368 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6370 retval = TRUE;
6371 curwin->w_cursor = cursor_save;
6372 }
6373 return retval;
6374}
6375
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006376/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006377 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006378 * Return 0 if there is none.
6379 * Otherwise return !0 and update "*poffset" to point to the place where the
6380 * string was found.
6381 */
6382 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006383cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006384{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006385 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006386
6387 if (offset-- < 2)
6388 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006389 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006390 --offset;
6391
6392 offset -= 1;
6393 if (!STRNCMP(line + offset, "if", 2))
6394 goto probablyFound;
6395
6396 if (offset >= 1)
6397 {
6398 offset -= 1;
6399 if (!STRNCMP(line + offset, "for", 3))
6400 goto probablyFound;
6401
6402 if (offset >= 2)
6403 {
6404 offset -= 2;
6405 if (!STRNCMP(line + offset, "while", 5))
6406 goto probablyFound;
6407 }
6408 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006409 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006410
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006411probablyFound:
6412 if (!offset || !vim_isIDc(line[offset - 1]))
6413 {
6414 *poffset = offset;
6415 return 1;
6416 }
6417 return 0;
6418}
6419
6420/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006421 * Return TRUE if we are at the end of a do-while.
6422 * do
6423 * nothing;
6424 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006425 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006426 * Adjust the cursor to the line with "while".
6427 */
6428 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006429cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006430{
6431 char_u *line;
6432 char_u *p;
6433 char_u *s;
6434 pos_T *trypos;
6435 int i;
6436
6437 if (terminated != ';') /* there must be a ';' at the end */
6438 return FALSE;
6439
6440 p = line = ml_get_curline();
6441 while (*p != NUL)
6442 {
6443 p = cin_skipcomment(p);
6444 if (*p == ')')
6445 {
6446 s = skipwhite(p + 1);
6447 if (*s == ';' && cin_nocode(s + 1))
6448 {
6449 /* Found ");" at end of the line, now check there is "while"
6450 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006451 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006452 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006453 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006454 if (trypos != NULL)
6455 {
6456 s = cin_skipcomment(ml_get(trypos->lnum));
6457 if (*s == '}') /* accept "} while (cond);" */
6458 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006459 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006460 {
6461 curwin->w_cursor.lnum = trypos->lnum;
6462 return TRUE;
6463 }
6464 }
6465
6466 /* Searching may have made "line" invalid, get it again. */
6467 line = ml_get_curline();
6468 p = line + i;
6469 }
6470 }
6471 if (*p != NUL)
6472 ++p;
6473 }
6474 return FALSE;
6475}
6476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006478cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6481}
6482
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006483/*
6484 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 * constructor-initialization. eg:
6486 *
6487 * class MyClass :
6488 * baseClass <-- here
6489 * class MyClass : public baseClass,
6490 * anotherBaseClass <-- here (should probably lineup ??)
6491 * MyClass::MyClass(...) :
6492 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006493 *
6494 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 */
6496 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006497cin_is_cpp_baseclass(
6498 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006500 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 char_u *s;
6502 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006503 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006504 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006506 if (pos->lnum <= lnum)
6507 return cached->found; /* Use the cached result */
6508
6509 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006511 s = skipwhite(line);
6512 if (*s == '#') /* skip #define FOO x ? (x) : x */
6513 return FALSE;
6514 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (*s == NUL)
6516 return FALSE;
6517
6518 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6519
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006520 /* Search for a line starting with '#', empty, ending in ';' or containing
6521 * '{' or '}' and start below it. This handles the following situations:
6522 * a = cond ?
6523 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006524 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006525 * func::foo()
6526 * : something
6527 * {}
6528 * Foo::Foo (int one, int two)
6529 * : something(4),
6530 * somethingelse(3)
6531 * {}
6532 */
6533 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006535 line = ml_get(lnum - 1);
6536 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006537 if (*s == '#' || *s == NUL)
6538 break;
6539 while (*s != NUL)
6540 {
6541 s = cin_skipcomment(s);
6542 if (*s == '{' || *s == '}'
6543 || (*s == ';' && cin_nocode(s + 1)))
6544 break;
6545 if (*s != NUL)
6546 ++s;
6547 }
6548 if (*s != NUL)
6549 break;
6550 --lnum;
6551 }
6552
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006553 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006554 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006555 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006556 for (;;)
6557 {
6558 if (*s == NUL)
6559 {
6560 if (lnum == curwin->w_cursor.lnum)
6561 break;
6562 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006563 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006564 s = line;
6565 }
6566 if (s == line)
6567 {
6568 /* don't recognize "case (foo):" as a baseclass */
6569 if (cin_iscase(s, FALSE))
6570 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006571 s = cin_skipcomment(line);
6572 if (*s == NUL)
6573 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006574 }
6575
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006576 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006577 s = skip_string(s) + 1;
6578 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 {
6580 if (s[1] == ':')
6581 {
6582 /* skip double colon. It can't be a constructor
6583 * initialization any more */
6584 lookfor_ctor_init = FALSE;
6585 s = cin_skipcomment(s + 2);
6586 }
6587 else if (lookfor_ctor_init || class_or_struct)
6588 {
6589 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006590 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 cpp_base_class = TRUE;
6592 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006593 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 s = cin_skipcomment(s + 1);
6595 }
6596 else
6597 s = cin_skipcomment(s + 1);
6598 }
6599 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6600 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6601 {
6602 class_or_struct = TRUE;
6603 lookfor_ctor_init = FALSE;
6604
6605 if (*s == 'c')
6606 s = cin_skipcomment(s + 5);
6607 else
6608 s = cin_skipcomment(s + 6);
6609 }
6610 else
6611 {
6612 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6613 {
6614 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6615 }
6616 else if (s[0] == ')')
6617 {
6618 /* Constructor-initialization is assumed if we come across
6619 * something like "):" */
6620 class_or_struct = FALSE;
6621 lookfor_ctor_init = TRUE;
6622 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006623 else if (s[0] == '?')
6624 {
6625 /* Avoid seeing '() :' after '?' as constructor init. */
6626 return FALSE;
6627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 else if (!vim_isIDc(s[0]))
6629 {
6630 /* if it is not an identifier, we are wrong */
6631 class_or_struct = FALSE;
6632 lookfor_ctor_init = FALSE;
6633 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006634 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 {
6636 /* it can't be a constructor-initialization any more */
6637 lookfor_ctor_init = FALSE;
6638
6639 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006640 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006641 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006644 /* When the line ends in a comma don't align with it. */
6645 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006646 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 s = cin_skipcomment(s + 1);
6649 }
6650 }
6651
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006652 cached->found = cpp_base_class;
6653 if (cpp_base_class)
6654 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 return cpp_base_class;
6656}
6657
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006658 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006659get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006660{
6661 int amount;
6662 colnr_T vcol;
6663 pos_T *trypos;
6664
6665 if (col == 0)
6666 {
6667 amount = get_indent();
6668 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006669 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006670 amount = get_indent_lnum(trypos->lnum); /* XXX */
6671 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006672 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006673 }
6674 else
6675 {
6676 curwin->w_cursor.col = col;
6677 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6678 amount = (int)vcol;
6679 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006680 if (amount < curbuf->b_ind_cpp_baseclass)
6681 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006682 return amount;
6683}
6684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
6686 * Return TRUE if string "s" ends with the string "find", possibly followed by
6687 * white space and comments. Skip strings and comments.
6688 * Ignore "ignore" after "find" if it's not NULL.
6689 */
6690 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006691cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 char_u *p = s;
6694 char_u *r;
6695 int len = (int)STRLEN(find);
6696
6697 while (*p != NUL)
6698 {
6699 p = cin_skipcomment(p);
6700 if (STRNCMP(p, find, len) == 0)
6701 {
6702 r = skipwhite(p + len);
6703 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6704 r = skipwhite(r + STRLEN(ignore));
6705 if (cin_nocode(r))
6706 return TRUE;
6707 }
6708 if (*p != NUL)
6709 ++p;
6710 }
6711 return FALSE;
6712}
6713
6714/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006715 * Return TRUE when "s" starts with "word" and then a non-ID character.
6716 */
6717 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006718cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006719{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006720 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006721
6722 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6723}
6724
6725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 * Skip strings, chars and comments until at or past "trypos".
6727 * Return the column found.
6728 */
6729 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006730cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 char_u *line;
6733 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006734 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735
6736 p = line = ml_get(trypos->lnum);
6737 while (*p && (colnr_T)(p - line) < trypos->col)
6738 {
6739 if (cin_iscomment(p))
6740 p = cin_skipcomment(p);
6741 else
6742 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006743 new_p = skip_string(p);
6744 if (new_p == p)
6745 ++p;
6746 else
6747 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 }
6749 }
6750 return (int)(p - line);
6751}
6752
6753/*
6754 * Find the '{' at the start of the block we are in.
6755 * Return NULL if no match found.
6756 * Ignore a '{' that is in a comment, makes indenting the next three lines
6757 * work. */
6758/* foo() */
6759/* { */
6760/* } */
6761
6762 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006763find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
6765 pos_T cursor_save;
6766 pos_T *trypos;
6767 pos_T *pos;
6768 static pos_T pos_copy;
6769
6770 cursor_save = curwin->w_cursor;
6771 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6772 {
6773 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6774 trypos = &pos_copy;
6775 curwin->w_cursor = *trypos;
6776 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006777 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006779 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 break;
6781 if (pos != NULL)
6782 curwin->w_cursor.lnum = pos->lnum;
6783 }
6784 curwin->w_cursor = cursor_save;
6785 return trypos;
6786}
6787
6788/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006789 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006790 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 */
6792 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006795 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006796}
6797
6798 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006799find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006800{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 pos_T cursor_save;
6802 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006803 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006804 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006807 ind_maxp_wk = ind_maxparen;
6808retry:
6809 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
6811 /* check if the ( is in a // comment */
6812 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006813 {
6814 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6815 if (ind_maxp_wk > 0)
6816 {
6817 curwin->w_cursor = *trypos;
6818 curwin->w_cursor.col = 0; /* XXX */
6819 goto retry;
6820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 else
6824 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006825 pos_T *trypos_wk;
6826
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6828 trypos = &pos_copy;
6829 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006830 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006831 {
6832 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6833 - trypos_wk->lnum);
6834 if (ind_maxp_wk > 0)
6835 {
6836 curwin->w_cursor = *trypos_wk;
6837 goto retry;
6838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842 }
6843 curwin->w_cursor = cursor_save;
6844 return trypos;
6845}
6846
6847/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006848 * Find the matching '(', ignoring it if it is in a comment or before an
6849 * unmatched {.
6850 * Return NULL if no match found.
6851 */
6852 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006853find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006854{
6855 pos_T *trypos = find_match_paren(ind_maxparen);
6856
6857 if (trypos != NULL)
6858 {
6859 pos_T *tryposBrace = find_start_brace();
6860
6861 /* If both an unmatched '(' and '{' is found. Ignore the '('
6862 * position if the '{' is further down. */
6863 if (tryposBrace != NULL
6864 && (trypos->lnum != tryposBrace->lnum
6865 ? trypos->lnum < tryposBrace->lnum
6866 : trypos->col < tryposBrace->col))
6867 trypos = NULL;
6868 }
6869 return trypos;
6870}
6871
6872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 * Return ind_maxparen corrected for the difference in line number between the
6874 * cursor position and "startpos". This makes sure that searching for a
6875 * matching paren above the cursor line doesn't find a match because of
6876 * looking a few lines further.
6877 */
6878 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006879corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
6881 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6882
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006883 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6884 return curbuf->b_ind_maxparen - (int)n;
6885 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886}
6887
6888/*
6889 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006890 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 */
6892 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006893find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
6895 int i;
6896 int retval = FALSE;
6897 int open_count = 0;
6898
6899 curwin->w_cursor.col = 0; /* default is start of line */
6900
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006901 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 {
6903 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6904 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6905 if (l[i] == start)
6906 ++open_count;
6907 else if (l[i] == end)
6908 {
6909 if (open_count > 0)
6910 --open_count;
6911 else
6912 {
6913 curwin->w_cursor.col = i;
6914 retval = TRUE;
6915 }
6916 }
6917 }
6918 return retval;
6919}
6920
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006921/*
6922 * Parse 'cinoptions' and set the values in "curbuf".
6923 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6924 */
6925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006926parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006927{
6928 char_u *p;
6929 char_u *l;
6930 char_u *digits;
6931 int n;
6932 int divider;
6933 int fraction = 0;
6934 int sw = (int)get_sw_value(buf);
6935
6936 /*
6937 * Set the default values.
6938 */
6939 /* Spaces from a block's opening brace the prevailing indent for that
6940 * block should be. */
6941 buf->b_ind_level = sw;
6942
6943 /* Spaces from the edge of the line an open brace that's at the end of a
6944 * line is imagined to be. */
6945 buf->b_ind_open_imag = 0;
6946
6947 /* Spaces from the prevailing indent for a line that is not preceded by
6948 * an opening brace. */
6949 buf->b_ind_no_brace = 0;
6950
6951 /* Column where the first { of a function should be located }. */
6952 buf->b_ind_first_open = 0;
6953
6954 /* Spaces from the prevailing indent a leftmost open brace should be
6955 * located. */
6956 buf->b_ind_open_extra = 0;
6957
6958 /* Spaces from the matching open brace (real location for one at the left
6959 * edge; imaginary location from one that ends a line) the matching close
6960 * brace should be located. */
6961 buf->b_ind_close_extra = 0;
6962
6963 /* Spaces from the edge of the line an open brace sitting in the leftmost
6964 * column is imagined to be. */
6965 buf->b_ind_open_left_imag = 0;
6966
6967 /* Spaces jump labels should be shifted to the left if N is non-negative,
6968 * otherwise the jump label will be put to column 1. */
6969 buf->b_ind_jump_label = -1;
6970
6971 /* Spaces from the switch() indent a "case xx" label should be located. */
6972 buf->b_ind_case = sw;
6973
6974 /* Spaces from the "case xx:" code after a switch() should be located. */
6975 buf->b_ind_case_code = sw;
6976
6977 /* Lineup break at end of case in switch() with case label. */
6978 buf->b_ind_case_break = 0;
6979
6980 /* Spaces from the class declaration indent a scope declaration label
6981 * should be located. */
6982 buf->b_ind_scopedecl = sw;
6983
6984 /* Spaces from the scope declaration label code should be located. */
6985 buf->b_ind_scopedecl_code = sw;
6986
6987 /* Amount K&R-style parameters should be indented. */
6988 buf->b_ind_param = sw;
6989
6990 /* Amount a function type spec should be indented. */
6991 buf->b_ind_func_type = sw;
6992
6993 /* Amount a cpp base class declaration or constructor initialization
6994 * should be indented. */
6995 buf->b_ind_cpp_baseclass = sw;
6996
6997 /* additional spaces beyond the prevailing indent a continuation line
6998 * should be located. */
6999 buf->b_ind_continuation = sw;
7000
7001 /* Spaces from the indent of the line with an unclosed parentheses. */
7002 buf->b_ind_unclosed = sw * 2;
7003
7004 /* Spaces from the indent of the line with an unclosed parentheses, which
7005 * itself is also unclosed. */
7006 buf->b_ind_unclosed2 = sw;
7007
7008 /* Suppress ignoring spaces from the indent of a line starting with an
7009 * unclosed parentheses. */
7010 buf->b_ind_unclosed_noignore = 0;
7011
7012 /* If the opening paren is the last nonwhite character on the line, and
7013 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7014 * context (for very long lines). */
7015 buf->b_ind_unclosed_wrapped = 0;
7016
7017 /* Suppress ignoring white space when lining up with the character after
7018 * an unclosed parentheses. */
7019 buf->b_ind_unclosed_whiteok = 0;
7020
7021 /* Indent a closing parentheses under the line start of the matching
7022 * opening parentheses. */
7023 buf->b_ind_matching_paren = 0;
7024
7025 /* Indent a closing parentheses under the previous line. */
7026 buf->b_ind_paren_prev = 0;
7027
7028 /* Extra indent for comments. */
7029 buf->b_ind_comment = 0;
7030
7031 /* Spaces from the comment opener when there is nothing after it. */
7032 buf->b_ind_in_comment = 3;
7033
7034 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7035 * after the comment opener. */
7036 buf->b_ind_in_comment2 = 0;
7037
7038 /* Max lines to search for an open paren. */
7039 buf->b_ind_maxparen = 20;
7040
7041 /* Max lines to search for an open comment. */
7042 buf->b_ind_maxcomment = 70;
7043
7044 /* Handle braces for java code. */
7045 buf->b_ind_java = 0;
7046
7047 /* Not to confuse JS object properties with labels. */
7048 buf->b_ind_js = 0;
7049
7050 /* Handle blocked cases correctly. */
7051 buf->b_ind_keep_case_label = 0;
7052
7053 /* Handle C++ namespace. */
7054 buf->b_ind_cpp_namespace = 0;
7055
7056 /* Handle continuation lines containing conditions of if(), for() and
7057 * while(). */
7058 buf->b_ind_if_for_while = 0;
7059
Bram Moolenaar6b643942017-03-05 19:44:06 +01007060 /* indentation for # comments */
7061 buf->b_ind_hash_comment = 0;
7062
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007063 /* Handle C++ extern "C" or "C++" */
7064 buf->b_ind_cpp_extern_c = 0;
7065
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007066 for (p = buf->b_p_cino; *p; )
7067 {
7068 l = p++;
7069 if (*p == '-')
7070 ++p;
7071 digits = p; /* remember where the digits start */
7072 n = getdigits(&p);
7073 divider = 0;
7074 if (*p == '.') /* ".5s" means a fraction */
7075 {
7076 fraction = atol((char *)++p);
7077 while (VIM_ISDIGIT(*p))
7078 {
7079 ++p;
7080 if (divider)
7081 divider *= 10;
7082 else
7083 divider = 10;
7084 }
7085 }
7086 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7087 {
7088 if (p == digits)
7089 n = sw; /* just "s" is one 'shiftwidth' */
7090 else
7091 {
7092 n *= sw;
7093 if (divider)
7094 n += (sw * fraction + divider / 2) / divider;
7095 }
7096 ++p;
7097 }
7098 if (l[1] == '-')
7099 n = -n;
7100
7101 /* When adding an entry here, also update the default 'cinoptions' in
7102 * doc/indent.txt, and add explanation for it! */
7103 switch (*l)
7104 {
7105 case '>': buf->b_ind_level = n; break;
7106 case 'e': buf->b_ind_open_imag = n; break;
7107 case 'n': buf->b_ind_no_brace = n; break;
7108 case 'f': buf->b_ind_first_open = n; break;
7109 case '{': buf->b_ind_open_extra = n; break;
7110 case '}': buf->b_ind_close_extra = n; break;
7111 case '^': buf->b_ind_open_left_imag = n; break;
7112 case 'L': buf->b_ind_jump_label = n; break;
7113 case ':': buf->b_ind_case = n; break;
7114 case '=': buf->b_ind_case_code = n; break;
7115 case 'b': buf->b_ind_case_break = n; break;
7116 case 'p': buf->b_ind_param = n; break;
7117 case 't': buf->b_ind_func_type = n; break;
7118 case '/': buf->b_ind_comment = n; break;
7119 case 'c': buf->b_ind_in_comment = n; break;
7120 case 'C': buf->b_ind_in_comment2 = n; break;
7121 case 'i': buf->b_ind_cpp_baseclass = n; break;
7122 case '+': buf->b_ind_continuation = n; break;
7123 case '(': buf->b_ind_unclosed = n; break;
7124 case 'u': buf->b_ind_unclosed2 = n; break;
7125 case 'U': buf->b_ind_unclosed_noignore = n; break;
7126 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7127 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7128 case 'm': buf->b_ind_matching_paren = n; break;
7129 case 'M': buf->b_ind_paren_prev = n; break;
7130 case ')': buf->b_ind_maxparen = n; break;
7131 case '*': buf->b_ind_maxcomment = n; break;
7132 case 'g': buf->b_ind_scopedecl = n; break;
7133 case 'h': buf->b_ind_scopedecl_code = n; break;
7134 case 'j': buf->b_ind_java = n; break;
7135 case 'J': buf->b_ind_js = n; break;
7136 case 'l': buf->b_ind_keep_case_label = n; break;
7137 case '#': buf->b_ind_hash_comment = n; break;
7138 case 'N': buf->b_ind_cpp_namespace = n; break;
7139 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007140 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007141 }
7142 if (*p == ',')
7143 ++p;
7144 }
7145}
7146
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007147/*
7148 * Return the desired indent for C code.
7149 * Return -1 if the indent should be left alone (inside a raw string).
7150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007152get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 pos_T cur_curpos;
7155 int amount;
7156 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007157 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 colnr_T col;
7159 char_u *theline;
7160 char_u *linecopy;
7161 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007162 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007164 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 pos_T our_paren_pos;
7166 char_u *start;
7167 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007168#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169#define BRACE_AT_START 2 /* '{' is at start of line */
7170#define BRACE_AT_END 3 /* '{' is at end of line */
7171 linenr_T ourscope;
7172 char_u *l;
7173 char_u *look;
7174 char_u terminated;
7175 int lookfor;
7176#define LOOKFOR_INITIAL 0
7177#define LOOKFOR_IF 1
7178#define LOOKFOR_DO 2
7179#define LOOKFOR_CASE 3
7180#define LOOKFOR_ANY 4
7181#define LOOKFOR_TERM 5
7182#define LOOKFOR_UNTERM 6
7183#define LOOKFOR_SCOPEDECL 7
7184#define LOOKFOR_NOBREAK 8
7185#define LOOKFOR_CPP_BASECLASS 9
7186#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007187#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007188#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190 int whilelevel;
7191 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 int n;
7193 int iscase;
7194 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007195 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007197 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007198 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007199 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007200 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007201 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007203 /* make a copy, value is changed below */
7204 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
7206 /* remember where the cursor was when we started */
7207 cur_curpos = curwin->w_cursor;
7208
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007209 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007210 if (cur_curpos.lnum == 1)
7211 return 0;
7212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 /* Get a copy of the current contents of the line.
7214 * This is required, because only the most recent line obtained with
7215 * ml_get is valid! */
7216 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7217 if (linecopy == NULL)
7218 return 0;
7219
7220 /*
7221 * In insert mode and the cursor is on a ')' truncate the line at the
7222 * cursor position. We don't want to line up with the matching '(' when
7223 * inserting new stuff.
7224 * For unknown reasons the cursor might be past the end of the line, thus
7225 * check for that.
7226 */
7227 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007228 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 && linecopy[curwin->w_cursor.col] == ')')
7230 linecopy[curwin->w_cursor.col] = NUL;
7231
7232 theline = skipwhite(linecopy);
7233
7234 /* move the cursor to the start of the line */
7235
7236 curwin->w_cursor.col = 0;
7237
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007238 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007239
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007241 * If we are inside a raw string don't change the indent.
7242 * Ignore a raw string inside a comment.
7243 */
7244 comment_pos = ind_find_start_comment();
7245 if (comment_pos != NULL)
7246 {
7247 /* findmatchlimit() static pos is overwritten, make a copy */
7248 tryposCopy = *comment_pos;
7249 comment_pos = &tryposCopy;
7250 }
7251 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007252 if (trypos != NULL && (comment_pos == NULL
7253 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007254 {
7255 amount = -1;
7256 goto laterend;
7257 }
7258
7259 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 * #defines and so on always go at the left when included in 'cinkeys'.
7261 */
7262 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007263 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007264 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007265 goto theend;
7266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
7268 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007269 * Is it a non-case label? Then that goes at the left margin too unless:
7270 * - JS flag is set.
7271 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007273 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007274 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
7276 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007277 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279
7280 /*
7281 * If we're inside a "//" comment and there is a "//" comment in a
7282 * previous line, lineup with that one.
7283 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007284 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 && (trypos = find_line_comment()) != NULL) /* XXX */
7286 {
7287 /* find how indented the line beginning the comment is */
7288 getvcol(curwin, trypos, &col, NULL, NULL);
7289 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007290 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292
7293 /*
7294 * If we're inside a comment and not looking at the start of the
7295 * comment, try using the 'comments' option.
7296 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007297 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 int lead_start_len = 2;
7300 int lead_middle_len = 1;
7301 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7302 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7303 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7304 char_u *p;
7305 int start_align = 0;
7306 int start_off = 0;
7307 int done = FALSE;
7308
7309 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007310 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007312 *lead_start = NUL;
7313 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
7315 p = curbuf->b_p_com;
7316 while (*p != NUL)
7317 {
7318 int align = 0;
7319 int off = 0;
7320 int what = 0;
7321
7322 while (*p != NUL && *p != ':')
7323 {
7324 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7325 what = *p++;
7326 else if (*p == COM_LEFT || *p == COM_RIGHT)
7327 align = *p++;
7328 else if (VIM_ISDIGIT(*p) || *p == '-')
7329 off = getdigits(&p);
7330 else
7331 ++p;
7332 }
7333
7334 if (*p == ':')
7335 ++p;
7336 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7337 if (what == COM_START)
7338 {
7339 STRCPY(lead_start, lead_end);
7340 lead_start_len = (int)STRLEN(lead_start);
7341 start_off = off;
7342 start_align = align;
7343 }
7344 else if (what == COM_MIDDLE)
7345 {
7346 STRCPY(lead_middle, lead_end);
7347 lead_middle_len = (int)STRLEN(lead_middle);
7348 }
7349 else if (what == COM_END)
7350 {
7351 /* If our line starts with the middle comment string, line it
7352 * up with the comment opener per the 'comments' option. */
7353 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7354 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7355 {
7356 done = TRUE;
7357 if (curwin->w_cursor.lnum > 1)
7358 {
7359 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007360 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 * the middle comment string matches in the previous
7362 * line, use the indent of that line. XXX */
7363 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7364 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7365 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7366 else if (STRNCMP(look, lead_middle,
7367 lead_middle_len) == 0)
7368 {
7369 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7370 break;
7371 }
7372 /* If the start comment string doesn't match with the
7373 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007374 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 lead_start, lead_start_len) != 0)
7376 continue;
7377 }
7378 if (start_off != 0)
7379 amount += start_off;
7380 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007381 amount += vim_strsize(lead_start)
7382 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 break;
7384 }
7385
7386 /* If our line starts with the end comment string, line it up
7387 * with the middle comment */
7388 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7389 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7390 {
7391 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7392 /* XXX */
7393 if (off != 0)
7394 amount += off;
7395 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007396 amount += vim_strsize(lead_start)
7397 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 done = TRUE;
7399 break;
7400 }
7401 }
7402 }
7403
7404 /* If our line starts with an asterisk, line up with the
7405 * asterisk in the comment opener; otherwise, line up
7406 * with the first character of the comment text.
7407 */
7408 if (done)
7409 ;
7410 else if (theline[0] == '*')
7411 amount += 1;
7412 else
7413 {
7414 /*
7415 * If we are more than one line away from the comment opener, take
7416 * the indent of the previous non-empty line. If 'cino' has "CO"
7417 * and we are just below the comment opener and there are any
7418 * white characters after it line up with the text after it;
7419 * otherwise, add the amount specified by "c" in 'cino'
7420 */
7421 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007422 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 {
7424 if (linewhite(lnum)) /* skip blank lines */
7425 continue;
7426 amount = get_indent_lnum(lnum); /* XXX */
7427 break;
7428 }
7429 if (amount == -1) /* use the comment opener */
7430 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007431 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007433 start = ml_get(comment_pos->lnum);
7434 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007436 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007438 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007440 if (curbuf->b_ind_in_comment2 || *look == NUL)
7441 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007444 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 }
7446
7447 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007448 * Are we looking at a ']' that has a match?
7449 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007450 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007451 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7452 {
7453 /* align with the line containing the '['. */
7454 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007455 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007456 }
7457
7458 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 * Are we inside parentheses or braces?
7460 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007461 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007462 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007463 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 || trypos != NULL)
7465 {
7466 if (trypos != NULL && tryposBrace != NULL)
7467 {
7468 /* Both an unmatched '(' and '{' is found. Use the one which is
7469 * closer to the current cursor position, set the other to NULL. */
7470 if (trypos->lnum != tryposBrace->lnum
7471 ? trypos->lnum < tryposBrace->lnum
7472 : trypos->col < tryposBrace->col)
7473 trypos = NULL;
7474 else
7475 tryposBrace = NULL;
7476 }
7477
7478 if (trypos != NULL)
7479 {
7480 /*
7481 * If the matching paren is more than one line away, use the indent of
7482 * a previous non-empty line that matches the same paren.
7483 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007484 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007486 /* Line up with the start of the matching paren line. */
7487 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7488 }
7489 else
7490 {
7491 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007492 our_paren_pos = *trypos;
7493 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007495 l = skipwhite(ml_get(lnum));
7496 if (cin_nocode(l)) /* skip comment lines */
7497 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007498 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007499 continue; /* ignore #define, #if, etc. */
7500 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007502 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007503 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007504 {
7505 lnum = trypos->lnum + 1;
7506 continue;
7507 }
7508
7509 /* XXX */
7510 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007511 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007512 && trypos->lnum == our_paren_pos.lnum
7513 && trypos->col == our_paren_pos.col)
7514 {
7515 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007517 if (theline[0] == ')')
7518 {
7519 if (our_paren_pos.lnum != lnum
7520 && cur_amount > amount)
7521 cur_amount = amount;
7522 amount = -1;
7523 }
7524 break;
7525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 }
7527 }
7528
7529 /*
7530 * Line up with line where the matching paren is. XXX
7531 * If the line starts with a '(' or the indent for unclosed
7532 * parentheses is zero, line up with the unclosed parentheses.
7533 */
7534 if (amount == -1)
7535 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007536 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007537 int is_if_for_while = 0;
7538
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007539 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007540 {
7541 /* Look for the outermost opening parenthesis on this line
7542 * and check whether it belongs to an "if", "for" or "while". */
7543
7544 pos_T cursor_save = curwin->w_cursor;
7545 pos_T outermost;
7546 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007547
7548 trypos = &our_paren_pos;
7549 do {
7550 outermost = *trypos;
7551 curwin->w_cursor.lnum = outermost.lnum;
7552 curwin->w_cursor.col = outermost.col;
7553
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007554 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007555 } while (trypos && trypos->lnum == outermost.lnum);
7556
7557 curwin->w_cursor = cursor_save;
7558
7559 line = ml_get(outermost.lnum);
7560
7561 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007562 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007563 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007564
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007565 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007566 look = skipwhite(look);
7567 if (*look == '(')
7568 {
7569 linenr_T save_lnum = curwin->w_cursor.lnum;
7570 char_u *line;
7571 int look_col;
7572
7573 /* Ignore a '(' in front of the line that has a match before
7574 * our matching '('. */
7575 curwin->w_cursor.lnum = our_paren_pos.lnum;
7576 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007577 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007578 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007579 if ((trypos = findmatchlimit(NULL, ')', 0,
7580 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007581 != NULL
7582 && trypos->lnum == our_paren_pos.lnum
7583 && trypos->col < our_paren_pos.col)
7584 ignore_paren_col = trypos->col + 1;
7585
7586 curwin->w_cursor.lnum = save_lnum;
7587 look = ml_get(our_paren_pos.lnum) + look_col;
7588 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007589 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7590 && is_if_for_while == 0)
7591 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007592 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
7594 /*
7595 * If we're looking at a close paren, line up right there;
7596 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007597 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 * the last nonwhite character of the line, use either the
7599 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007600 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 * lines).
7602 */
7603 if (theline[0] != ')')
7604 {
7605 cur_amount = MAXCOL;
7606 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007607 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 && cin_ends_in(l, (char_u *)"(", NULL))
7609 {
7610 /* look for opening unmatched paren, indent one level
7611 * for each additional level */
7612 n = 1;
7613 for (col = 0; col < our_paren_pos.col; ++col)
7614 {
7615 switch (l[col])
7616 {
7617 case '(':
7618 case '{': ++n;
7619 break;
7620
7621 case ')':
7622 case '}': if (n > 1)
7623 --n;
7624 break;
7625 }
7626 }
7627
7628 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007629 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007631 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 our_paren_pos.col++;
7633 else
7634 {
7635 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007636 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 col++;
7638 if (l[col] != NUL) /* In case of trailing space */
7639 our_paren_pos.col = col;
7640 else
7641 our_paren_pos.col++;
7642 }
7643 }
7644
7645 /*
7646 * Find how indented the paren is, or the character after it
7647 * if we did the above "if".
7648 */
7649 if (our_paren_pos.col > 0)
7650 {
7651 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7652 if (cur_amount > (int)col)
7653 cur_amount = col;
7654 }
7655 }
7656
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007657 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 {
7659 /* Line up with the start of the matching paren line. */
7660 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007661 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7662 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007663 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {
7665 if (cur_amount != MAXCOL)
7666 amount = cur_amount;
7667 }
7668 else
7669 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007670 /* Add b_ind_unclosed2 for each '(' before our matching one,
7671 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007673 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
7675 --our_paren_pos.col;
7676 switch (*ml_get_pos(&our_paren_pos))
7677 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007678 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 col = our_paren_pos.col;
7680 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007681 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 col = MAXCOL;
7683 break;
7684 }
7685 }
7686
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007687 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 * braces */
7689 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007690 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 else
7692 {
7693 curwin->w_cursor.lnum = our_paren_pos.lnum;
7694 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007695 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7696 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007697 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007699 {
7700 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007701 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007702 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007703 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706 /*
7707 * For a line starting with ')' use the minimum of the two
7708 * positions, to avoid giving it more indent than the previous
7709 * lines:
7710 * func_long_name( if (x
7711 * arg && yy
7712 * ) ^ not here ) ^ not here
7713 */
7714 if (cur_amount < amount)
7715 amount = cur_amount;
7716 }
7717 }
7718
7719 /* add extra indent for a comment */
7720 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007721 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 else
7724 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007725 /*
7726 * We are inside braces, there is a { before this line at the position
7727 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007728 * Make a copy of tryposBrace, it may point to pos_copy inside
7729 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007730 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007731 tryposCopy = *tryposBrace;
7732 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 ourscope = trypos->lnum;
7735 start = ml_get(ourscope);
7736
7737 /*
7738 * Now figure out how indented the line is in general.
7739 * If the brace was at the start of the line, we use that;
7740 * otherwise, check out the indentation of the line as
7741 * a whole and then add the "imaginary indent" to that.
7742 */
7743 look = skipwhite(start);
7744 if (*look == '{')
7745 {
7746 getvcol(curwin, trypos, &col, NULL, NULL);
7747 amount = col;
7748 if (*start == '{')
7749 start_brace = BRACE_IN_COL0;
7750 else
7751 start_brace = BRACE_AT_START;
7752 }
7753 else
7754 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007755 /* That opening brace might have been on a continuation
7756 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 curwin->w_cursor.lnum = ourscope;
7758
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007759 /* Position the cursor over the rightmost paren, so that
7760 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 lnum = ourscope;
7762 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007763 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7764 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 lnum = trypos->lnum;
7766
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007767 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 * case 1: if (asdf &&
7769 * ldfd) {
7770 * }
7771 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007772 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7773 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007775 else if (curbuf->b_ind_js)
7776 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007778 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
7780 start_brace = BRACE_AT_END;
7781 }
7782
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007783 /* For Javascript check if the line starts with "key:". */
7784 if (curbuf->b_ind_js)
7785 js_cur_has_key = cin_has_js_key(theline);
7786
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007788 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 * we want to be. otherwise, add the amount of room
7790 * that an indent is supposed to be.
7791 */
7792 if (theline[0] == '}')
7793 {
7794 /*
7795 * they may want closing braces to line up with something
7796 * other than the open brace. indulge them, if so.
7797 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007798 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
7800 else
7801 {
7802 /*
7803 * If we're looking at an "else", try to find an "if"
7804 * to match it with.
7805 * If we're looking at a "while", try to find a "do"
7806 * to match it with.
7807 */
7808 lookfor = LOOKFOR_INITIAL;
7809 if (cin_iselse(theline))
7810 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007811 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 lookfor = LOOKFOR_DO;
7813 if (lookfor != LOOKFOR_INITIAL)
7814 {
7815 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007816 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {
7818 amount = get_indent(); /* XXX */
7819 goto theend;
7820 }
7821 }
7822
7823 /*
7824 * We get here if we are not on an "while-of-do" or "else" (or
7825 * failed to find a matching "if").
7826 * Search backwards for something to line up with.
7827 * First set amount for when we don't find anything.
7828 */
7829
7830 /*
7831 * if the '{' is _really_ at the left margin, use the imaginary
7832 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007833 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 */
7835
7836 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7837 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007838 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007839 lookfor_cpp_namespace = TRUE;
7840 }
7841 else if (start_brace == BRACE_AT_START &&
7842 lookfor_cpp_namespace) /* '{' is at start */
7843 {
7844
7845 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 }
7847 else
7848 {
7849 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007850 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007851 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007852
7853 l = skipwhite(ml_get_curline());
7854 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007855 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007856 else if (cin_is_cpp_extern_c(l))
7857 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 else
7860 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007861 /* Compensate for adding b_ind_open_extra later. */
7862 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (amount < 0)
7864 amount = 0;
7865 }
7866 }
7867
7868 lookfor_break = FALSE;
7869
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007870 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
7872 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007873 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875 else if (cin_isscopedecl(theline)) /* private:, ... */
7876 {
7877 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007878 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 }
7880 else
7881 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007882 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7883 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 lookfor_break = TRUE;
7885
7886 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007887 /* b_ind_level from start of block */
7888 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 scope_amount = amount;
7891 whilelevel = 0;
7892
7893 /*
7894 * Search backwards. If we find something we recognize, line up
7895 * with that.
7896 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007897 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 * the usual amount relative to the conditional
7899 * that opens the block.
7900 */
7901 curwin->w_cursor = cur_curpos;
7902 for (;;)
7903 {
7904 curwin->w_cursor.lnum--;
7905 curwin->w_cursor.col = 0;
7906
7907 /*
7908 * If we went all the way back to the start of our scope, line
7909 * up with it.
7910 */
7911 if (curwin->w_cursor.lnum <= ourscope)
7912 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007913 /* We reached end of scope:
7914 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007916 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 * don't add ind_continuation, otherwise it is a variable
7918 * declaration:
7919 * int x,
7920 * here; <-- add ind_continuation
7921 */
7922 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7923 {
7924 if (curwin->w_cursor.lnum == 0
7925 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007926 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007928 /* nothing found (abuse curbuf->b_ind_maxparen as
7929 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 * initialization) */
7931 if (cont_amount > 0)
7932 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007933 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 amount += ind_continuation;
7935 break;
7936 }
7937
7938 l = ml_get_curline();
7939
7940 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007941 * If we're in a comment or raw string now, skip to
7942 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007944 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 if (trypos != NULL)
7946 {
7947 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007948 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 continue;
7950 }
7951
7952 /*
7953 * Skip preprocessor directives and blank lines.
7954 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007955 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7956 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 continue;
7958
7959 if (cin_nocode(l))
7960 continue;
7961
7962 terminated = cin_isterminated(l, FALSE, TRUE);
7963
7964 /*
7965 * If we are at top level and the line looks like a
7966 * function declaration, we are done
7967 * (it's a variable declaration).
7968 */
7969 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007970 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {
7972 /* if the line is terminated with another ','
7973 * it is a continued variable initialization.
7974 * don't add extra indent.
7975 * TODO: does not work, if a function
7976 * declaration is split over multiple lines:
7977 * cin_isfuncdecl returns FALSE then.
7978 */
7979 if (terminated == ',')
7980 break;
7981
7982 /* if it es a enum declaration or an assignment,
7983 * we are done.
7984 */
7985 if (terminated != ';' && cin_isinit())
7986 break;
7987
7988 /* nothing useful found */
7989 if (terminated == 0 || terminated == '{')
7990 continue;
7991 }
7992
7993 if (terminated != ';')
7994 {
7995 /* Skip parens and braces. Position the cursor
7996 * over the rightmost paren, so that matching it
7997 * will take us back to the start of the line.
7998 */ /* XXX */
7999 trypos = NULL;
8000 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008001 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008002 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008005 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
8007 if (trypos != NULL)
8008 {
8009 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008010 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 continue;
8012 }
8013 }
8014
8015 /* it's a variable declaration, add indentation
8016 * like in
8017 * int a,
8018 * b;
8019 */
8020 if (cont_amount > 0)
8021 amount = cont_amount;
8022 else
8023 amount += ind_continuation;
8024 }
8025 else if (lookfor == LOOKFOR_UNTERM)
8026 {
8027 if (cont_amount > 0)
8028 amount = cont_amount;
8029 else
8030 amount += ind_continuation;
8031 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008032 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008033 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008034 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008035 && lookfor != LOOKFOR_CPP_BASECLASS
8036 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008037 {
8038 amount = scope_amount;
8039 if (theline[0] == '{')
8040 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008041 amount += curbuf->b_ind_open_extra;
8042 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008043 }
8044 }
8045
8046 if (lookfor_cpp_namespace)
8047 {
8048 /*
8049 * Looking for C++ namespace, need to look further
8050 * back.
8051 */
8052 if (curwin->w_cursor.lnum == ourscope)
8053 continue;
8054
8055 if (curwin->w_cursor.lnum == 0
8056 || curwin->w_cursor.lnum
8057 < ourscope - FIND_NAMESPACE_LIM)
8058 break;
8059
8060 l = ml_get_curline();
8061
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008062 /* If we're in a comment or raw string now, skip
8063 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008064 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008065 if (trypos != NULL)
8066 {
8067 curwin->w_cursor.lnum = trypos->lnum + 1;
8068 curwin->w_cursor.col = 0;
8069 continue;
8070 }
8071
8072 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008073 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8074 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008075 continue;
8076
8077 /* Finally the actual check for "namespace". */
8078 if (cin_is_cpp_namespace(l))
8079 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008080 amount += curbuf->b_ind_cpp_namespace
8081 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008082 break;
8083 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008084 else if (cin_is_cpp_extern_c(l))
8085 {
8086 amount += curbuf->b_ind_cpp_extern_c
8087 - added_to_amount;
8088 break;
8089 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008090
8091 if (cin_nocode(l))
8092 continue;
8093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 }
8095 break;
8096 }
8097
8098 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008099 * If we're in a comment or raw string now, skip to the start
8100 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008102 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {
8104 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008105 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 continue;
8107 }
8108
8109 l = ml_get_curline();
8110
8111 /*
8112 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008113 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008115 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 if (iscase || cin_isscopedecl(l))
8117 {
8118 /* we are only looking for cpp base class
8119 * declaration/initialization any longer */
8120 if (lookfor == LOOKFOR_CPP_BASECLASS)
8121 break;
8122
8123 /* When looking for a "do" we are not interested in
8124 * labels. */
8125 if (whilelevel > 0)
8126 continue;
8127
8128 /*
8129 * case xx:
8130 * c = 99 + <- this indent plus continuation
8131 *-> here;
8132 */
8133 if (lookfor == LOOKFOR_UNTERM
8134 || lookfor == LOOKFOR_ENUM_OR_INIT)
8135 {
8136 if (cont_amount > 0)
8137 amount = cont_amount;
8138 else
8139 amount += ind_continuation;
8140 break;
8141 }
8142
8143 /*
8144 * case xx: <- line up with this case
8145 * x = 333;
8146 * case yy:
8147 */
8148 if ( (iscase && lookfor == LOOKFOR_CASE)
8149 || (iscase && lookfor_break)
8150 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8151 {
8152 /*
8153 * Check that this case label is not for another
8154 * switch()
8155 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008156 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008157 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
8159 amount = get_indent(); /* XXX */
8160 break;
8161 }
8162 continue;
8163 }
8164
8165 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8166
8167 /*
8168 * case xx: if (cond) <- line up with this if
8169 * y = y + 1;
8170 * -> s = 99;
8171 *
8172 * case xx:
8173 * if (cond) <- line up with this line
8174 * y = y + 1;
8175 * -> s = 99;
8176 */
8177 if (lookfor == LOOKFOR_TERM)
8178 {
8179 if (n)
8180 amount = n;
8181
8182 if (!lookfor_break)
8183 break;
8184 }
8185
8186 /*
8187 * case xx: x = x + 1; <- line up with this x
8188 * -> y = y + 1;
8189 *
8190 * case xx: if (cond) <- line up with this if
8191 * -> y = y + 1;
8192 */
8193 if (n)
8194 {
8195 amount = n;
8196 l = after_label(ml_get_curline());
8197 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008198 {
8199 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008200 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008201 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008202 amount += curbuf->b_ind_level
8203 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 break;
8206 }
8207
8208 /*
8209 * Try to get the indent of a statement before the switch
8210 * label. If nothing is found, line up relative to the
8211 * switch label.
8212 * break; <- may line up with this line
8213 * case xx:
8214 * -> y = 1;
8215 */
8216 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008217 ? curbuf->b_ind_case_code
8218 : curbuf->b_ind_scopedecl_code);
8219 lookfor = curbuf->b_ind_case_break
8220 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 continue;
8222 }
8223
8224 /*
8225 * Looking for a switch() label or C++ scope declaration,
8226 * ignore other lines, skip {}-blocks.
8227 */
8228 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8229 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008230 if (find_last_paren(l, '{', '}')
8231 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008234 curwin->w_cursor.col = 0;
8235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 continue;
8237 }
8238
8239 /*
8240 * Ignore jump labels with nothing after them.
8241 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008242 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
8244 l = after_label(ml_get_curline());
8245 if (l == NULL || cin_nocode(l))
8246 continue;
8247 }
8248
8249 /*
8250 * Ignore #defines, #if, etc.
8251 * Ignore comment and empty lines.
8252 * (need to get the line again, cin_islabel() may have
8253 * unlocked it)
8254 */
8255 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008256 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 || cin_nocode(l))
8258 continue;
8259
8260 /*
8261 * Are we at the start of a cpp base class declaration or
8262 * constructor initialization?
8263 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008264 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008265 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008266 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008267 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008268 l = ml_get_curline();
8269 }
8270 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
8272 if (lookfor == LOOKFOR_UNTERM)
8273 {
8274 if (cont_amount > 0)
8275 amount = cont_amount;
8276 else
8277 amount += ind_continuation;
8278 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008279 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008281 /* Need to find start of the declaration. */
8282 lookfor = LOOKFOR_UNTERM;
8283 ind_continuation = 0;
8284 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 }
8286 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008287 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008288 amount = get_baseclass_amount(
8289 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 break;
8291 }
8292 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8293 {
8294 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008295 * declaration or initialization before the opening brace.
8296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 if (cin_isterminated(l, TRUE, FALSE))
8298 break;
8299 else
8300 continue;
8301 }
8302
8303 /*
8304 * What happens next depends on the line being terminated.
8305 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008306 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 * 123,
8308 * sizeof
8309 * here
8310 * Otherwise check whether it is a enumeration or structure
8311 * initialisation (not indented) or a variable declaration
8312 * (indented).
8313 */
8314 terminated = cin_isterminated(l, FALSE, TRUE);
8315
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008316 if (js_cur_has_key)
8317 {
8318 js_cur_has_key = 0; /* only check the first line */
8319 if (curbuf->b_ind_js && terminated == ',')
8320 {
8321 /* For Javascript we might be inside an object:
8322 * key: something, <- align with this
8323 * key: something
8324 * or:
8325 * key: something + <- align with this
8326 * something,
8327 * key: something
8328 */
8329 lookfor = LOOKFOR_JS_KEY;
8330 }
8331 }
8332 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8333 {
8334 amount = get_indent();
8335 break;
8336 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008337 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008338 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008339 if (tryposBrace != NULL && tryposBrace->lnum
8340 >= curwin->w_cursor.lnum)
8341 break;
8342 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008343 /* line below current line is the one that starts a
8344 * (possibly broken) line ending in a comma. */
8345 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008346 else
8347 {
8348 amount = get_indent();
8349 if (curwin->w_cursor.lnum - 1 == ourscope)
8350 /* line above is start of the scope, thus current
8351 * line is the one that stars a (possibly broken)
8352 * line ending in a comma. */
8353 break;
8354 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008355 }
8356
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8358 && terminated == ','))
8359 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008360 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8361 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008362 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 /*
8364 * if we're in the middle of a paren thing,
8365 * go back to the line that starts it so
8366 * we can get the right prevailing indent
8367 * if ( foo &&
8368 * bar )
8369 */
8370 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008371 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008373 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 */
8375 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008376 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008377 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8378 || (trypos->lnum == tryposBrace->lnum
8379 && trypos->col < tryposBrace->col)))
8380 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
8382 /*
8383 * If we are looking for ',', we also look for matching
8384 * braces.
8385 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008386 if (trypos == NULL && terminated == ','
8387 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008388 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389
8390 if (trypos != NULL)
8391 {
8392 /*
8393 * Check if we are on a case label now. This is
8394 * handled above.
8395 * case xx: if ( asdf &&
8396 * asdf)
8397 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008398 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008400 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {
8402 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008403 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 continue;
8405 }
8406 }
8407
8408 /*
8409 * Skip over continuation lines to find the one to get the
8410 * indent from
8411 * char *usethis = "bla\
8412 * bla",
8413 * here;
8414 */
8415 if (terminated == ',')
8416 {
8417 while (curwin->w_cursor.lnum > 1)
8418 {
8419 l = ml_get(curwin->w_cursor.lnum - 1);
8420 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8421 break;
8422 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008423 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 }
8425 }
8426
8427 /*
8428 * Get indent and pointer to text for current line,
8429 * ignoring any jump label. XXX
8430 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008431 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008432 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008433 else
8434 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 /*
8436 * If this is just above the line we are indenting, and it
8437 * starts with a '{', line it up with this line.
8438 * while (not)
8439 * -> {
8440 * }
8441 */
8442 if (terminated != ',' && lookfor != LOOKFOR_TERM
8443 && theline[0] == '{')
8444 {
8445 amount = cur_amount;
8446 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008447 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 * doesn't start with a '{', which must have a match
8449 * in the same line (scope is the same). Probably:
8450 * { 1, 2 },
8451 * -> { 3, 4 }
8452 */
8453 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008454 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008456 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 /* have to look back, whether it is a cpp base
8459 * class declaration or initialization */
8460 lookfor = LOOKFOR_CPP_BASECLASS;
8461 continue;
8462 }
8463 break;
8464 }
8465
8466 /*
8467 * Check if we are after an "if", "while", etc.
8468 * Also allow " } else".
8469 */
8470 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8471 {
8472 /*
8473 * Found an unterminated line after an if (), line up
8474 * with the last one.
8475 * if (cond)
8476 * 100 +
8477 * -> here;
8478 */
8479 if (lookfor == LOOKFOR_UNTERM
8480 || lookfor == LOOKFOR_ENUM_OR_INIT)
8481 {
8482 if (cont_amount > 0)
8483 amount = cont_amount;
8484 else
8485 amount += ind_continuation;
8486 break;
8487 }
8488
8489 /*
8490 * If this is just above the line we are indenting, we
8491 * are finished.
8492 * while (not)
8493 * -> here;
8494 * Otherwise this indent can be used when the line
8495 * before this is terminated.
8496 * yyy;
8497 * if (stat)
8498 * while (not)
8499 * xxx;
8500 * -> here;
8501 */
8502 amount = cur_amount;
8503 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008504 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 if (lookfor != LOOKFOR_TERM)
8506 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008507 amount += curbuf->b_ind_level
8508 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 break;
8510 }
8511
8512 /*
8513 * Special trick: when expecting the while () after a
8514 * do, line up with the while()
8515 * do
8516 * x = 1;
8517 * -> here
8518 */
8519 l = skipwhite(ml_get_curline());
8520 if (cin_isdo(l))
8521 {
8522 if (whilelevel == 0)
8523 break;
8524 --whilelevel;
8525 }
8526
8527 /*
8528 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008529 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 * Need to use the scope of this "else". XXX
8531 * If whilelevel != 0 continue looking for a "do {".
8532 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008533 if (cin_iselse(l) && whilelevel == 0)
8534 {
8535 /* If we're looking at "} else", let's make sure we
8536 * find the opening brace of the enclosing scope,
8537 * not the one from "if () {". */
8538 if (*l == '}')
8539 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008540 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008541
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008542 if ((trypos = find_start_brace()) == NULL
8543 || find_match(LOOKFOR_IF, trypos->lnum)
8544 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008545 break;
8546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
8548
8549 /*
8550 * If we're below an unterminated line that is not an
8551 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008552 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 * the line before this one.
8554 */
8555 else
8556 {
8557 /*
8558 * Found two unterminated lines on a row, line up with
8559 * the last one.
8560 * c = 99 +
8561 * 100 +
8562 * -> here;
8563 */
8564 if (lookfor == LOOKFOR_UNTERM)
8565 {
8566 /* When line ends in a comma add extra indent */
8567 if (terminated == ',')
8568 amount += ind_continuation;
8569 break;
8570 }
8571
8572 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8573 {
8574 /* Found two lines ending in ',', lineup with the
8575 * lowest one, but check for cpp base class
8576 * declaration/initialization, if it is an
8577 * opening brace or we are looking just for
8578 * enumerations/initializations. */
8579 if (terminated == ',')
8580 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008581 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 break;
8583
8584 lookfor = LOOKFOR_CPP_BASECLASS;
8585 continue;
8586 }
8587
8588 /* Ignore unterminated lines in between, but
8589 * reduce indent. */
8590 if (amount > cur_amount)
8591 amount = cur_amount;
8592 }
8593 else
8594 {
8595 /*
8596 * Found first unterminated line on a row, may
8597 * line up with this line, remember its indent
8598 * 100 +
8599 * -> here;
8600 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008601 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008603
8604 n = (int)STRLEN(l);
8605 if (terminated == ',' && (*skipwhite(l) == ']'
8606 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609 /*
8610 * If previous line ends in ',', check whether we
8611 * are in an initialization or enum
8612 * struct xxx =
8613 * {
8614 * sizeof a,
8615 * 124 };
8616 * or a normal possible continuation line.
8617 * but only, of no other statement has been found
8618 * yet.
8619 */
8620 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8621 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008622 if (curbuf->b_ind_js)
8623 {
8624 /* Search for a line ending in a comma
8625 * and line up with the line below it
8626 * (could be the current line).
8627 * some = [
8628 * 1, <- line up here
8629 * 2,
8630 * some = [
8631 * 3 + <- line up here
8632 * 4 *
8633 * 5,
8634 * 6,
8635 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008636 if (cin_iscomment(skipwhite(l)))
8637 break;
8638 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008639 trypos = find_match_char('[',
8640 curbuf->b_ind_maxparen);
8641 if (trypos != NULL)
8642 {
8643 if (trypos->lnum
8644 == curwin->w_cursor.lnum - 1)
8645 {
8646 /* Current line is first inside
8647 * [], line up with it. */
8648 break;
8649 }
8650 ourscope = trypos->lnum;
8651 }
8652 }
8653 else
8654 {
8655 lookfor = LOOKFOR_ENUM_OR_INIT;
8656 cont_amount = cin_first_id_amount();
8657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 }
8659 else
8660 {
8661 if (lookfor == LOOKFOR_INITIAL
8662 && *l != NUL
8663 && l[STRLEN(l) - 1] == '\\')
8664 /* XXX */
8665 cont_amount = cin_get_equal_amount(
8666 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008667 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008668 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008669 && lookfor != LOOKFOR_COMMA
8670 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 lookfor = LOOKFOR_UNTERM;
8672 }
8673 }
8674 }
8675 }
8676
8677 /*
8678 * Check if we are after a while (cond);
8679 * If so: Ignore until the matching "do".
8680 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008681 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 {
8683 /*
8684 * Found an unterminated line after a while ();, line up
8685 * with the last one.
8686 * while (cond);
8687 * 100 + <- line up with this one
8688 * -> here;
8689 */
8690 if (lookfor == LOOKFOR_UNTERM
8691 || lookfor == LOOKFOR_ENUM_OR_INIT)
8692 {
8693 if (cont_amount > 0)
8694 amount = cont_amount;
8695 else
8696 amount += ind_continuation;
8697 break;
8698 }
8699
8700 if (whilelevel == 0)
8701 {
8702 lookfor = LOOKFOR_TERM;
8703 amount = get_indent(); /* XXX */
8704 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008705 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 }
8707 ++whilelevel;
8708 }
8709
8710 /*
8711 * We are after a "normal" statement.
8712 * If we had another statement we can stop now and use the
8713 * indent of that other statement.
8714 * Otherwise the indent of the current statement may be used,
8715 * search backwards for the next "normal" statement.
8716 */
8717 else
8718 {
8719 /*
8720 * Skip single break line, if before a switch label. It
8721 * may be lined up with the case label.
8722 */
8723 if (lookfor == LOOKFOR_NOBREAK
8724 && cin_isbreak(skipwhite(ml_get_curline())))
8725 {
8726 lookfor = LOOKFOR_ANY;
8727 continue;
8728 }
8729
8730 /*
8731 * Handle "do {" line.
8732 */
8733 if (whilelevel > 0)
8734 {
8735 l = cin_skipcomment(ml_get_curline());
8736 if (cin_isdo(l))
8737 {
8738 amount = get_indent(); /* XXX */
8739 --whilelevel;
8740 continue;
8741 }
8742 }
8743
8744 /*
8745 * Found a terminated line above an unterminated line. Add
8746 * the amount for a continuation line.
8747 * x = 1;
8748 * y = foo +
8749 * -> here;
8750 * or
8751 * int x = 1;
8752 * int foo,
8753 * -> here;
8754 */
8755 if (lookfor == LOOKFOR_UNTERM
8756 || lookfor == LOOKFOR_ENUM_OR_INIT)
8757 {
8758 if (cont_amount > 0)
8759 amount = cont_amount;
8760 else
8761 amount += ind_continuation;
8762 break;
8763 }
8764
8765 /*
8766 * Found a terminated line above a terminated line or "if"
8767 * etc. line. Use the amount of the line below us.
8768 * x = 1; x = 1;
8769 * if (asdf) y = 2;
8770 * while (asdf) ->here;
8771 * here;
8772 * ->foo;
8773 */
8774 if (lookfor == LOOKFOR_TERM)
8775 {
8776 if (!lookfor_break && whilelevel == 0)
8777 break;
8778 }
8779
8780 /*
8781 * First line above the one we're indenting is terminated.
8782 * To know what needs to be done look further backward for
8783 * a terminated line.
8784 */
8785 else
8786 {
8787 /*
8788 * position the cursor over the rightmost paren, so
8789 * that matching it will take us back to the start of
8790 * the line. Helps for:
8791 * func(asdr,
8792 * asdfasdf);
8793 * here;
8794 */
8795term_again:
8796 l = ml_get_curline();
8797 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008798 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008799 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 {
8801 /*
8802 * Check if we are on a case label now. This is
8803 * handled above.
8804 * case xx: if ( asdf &&
8805 * asdf)
8806 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008807 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008809 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {
8811 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008812 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 continue;
8814 }
8815 }
8816
8817 /* When aligning with the case statement, don't align
8818 * with a statement after it.
8819 * case 1: { <-- don't use this { position
8820 * stat;
8821 * }
8822 * case 2:
8823 * stat;
8824 * }
8825 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008826 iscase = (curbuf->b_ind_keep_case_label
8827 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829 /*
8830 * Get indent and pointer to text for current line,
8831 * ignoring any jump label.
8832 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008833 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
8835 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008836 amount += curbuf->b_ind_open_extra;
8837 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008838 l = skipwhite(l);
8839 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008840 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8842
8843 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008844 * When a terminated line starts with "else" skip to
8845 * the matching "if":
8846 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008847 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008848 * Need to use the scope of this "else". XXX
8849 * If whilelevel != 0 continue looking for a "do {".
8850 */
8851 if (lookfor == LOOKFOR_TERM
8852 && *l != '}'
8853 && cin_iselse(l)
8854 && whilelevel == 0)
8855 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008856 if ((trypos = find_start_brace()) == NULL
8857 || find_match(LOOKFOR_IF, trypos->lnum)
8858 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008859 break;
8860 continue;
8861 }
8862
8863 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 * If we're at the end of a block, skip to the start of
8865 * that block.
8866 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008867 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008868 if (find_last_paren(l, '{', '}') /* XXX */
8869 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008871 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 /* if not "else {" check for terminated again */
8873 /* but skip block for "} else {" */
8874 l = cin_skipcomment(ml_get_curline());
8875 if (*l == '}' || !cin_iselse(l))
8876 goto term_again;
8877 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008878 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 }
8880 }
8881 }
8882 }
8883 }
8884 }
8885
8886 /* add extra indent for a comment */
8887 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008888 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008889
8890 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008891 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8892 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008893
8894 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008896
8897 /*
8898 * ok -- we're not inside any sort of structure at all!
8899 *
8900 * This means we're at the top level, and everything should
8901 * basically just match where the previous line is, except
8902 * for the lines immediately following a function declaration,
8903 * which are K&R-style parameters and need to be indented.
8904 *
8905 * if our line starts with an open brace, forget about any
8906 * prevailing indent and make sure it looks like the start
8907 * of a function
8908 */
8909
8910 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008912 amount = curbuf->b_ind_first_open;
8913 goto theend;
8914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008916 /*
8917 * If the NEXT line is a function declaration, the current
8918 * line needs to be indented as a function type spec.
8919 * Don't do this if the current line looks like a comment or if the
8920 * current line is terminated, ie. ends in ';', or if the current line
8921 * contains { or }: "void f() {\n if (1)"
8922 */
8923 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8924 && !cin_nocode(theline)
8925 && vim_strchr(theline, '{') == NULL
8926 && vim_strchr(theline, '}') == NULL
8927 && !cin_ends_in(theline, (char_u *)":", NULL)
8928 && !cin_ends_in(theline, (char_u *)",", NULL)
8929 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8930 cur_curpos.lnum + 1)
8931 && !cin_isterminated(theline, FALSE, TRUE))
8932 {
8933 amount = curbuf->b_ind_func_type;
8934 goto theend;
8935 }
8936
8937 /* search backwards until we find something we recognize */
8938 amount = 0;
8939 curwin->w_cursor = cur_curpos;
8940 while (curwin->w_cursor.lnum > 1)
8941 {
8942 curwin->w_cursor.lnum--;
8943 curwin->w_cursor.col = 0;
8944
8945 l = ml_get_curline();
8946
8947 /*
8948 * If we're in a comment or raw string now, skip to the start
8949 * of it.
8950 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008951 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008953 curwin->w_cursor.lnum = trypos->lnum + 1;
8954 curwin->w_cursor.col = 0;
8955 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 }
8957
8958 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008959 * Are we at the start of a cpp base class declaration or
8960 * constructor initialization?
8961 */ /* XXX */
8962 n = FALSE;
8963 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008965 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8966 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008968 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008970 /* XXX */
8971 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8972 break;
8973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008975 /*
8976 * Skip preprocessor directives and blank lines.
8977 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008978 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008979 continue;
8980
8981 if (cin_nocode(l))
8982 continue;
8983
8984 /*
8985 * If the previous line ends in ',', use one level of
8986 * indentation:
8987 * int foo,
8988 * bar;
8989 * do this before checking for '}' in case of eg.
8990 * enum foobar
8991 * {
8992 * ...
8993 * } foo,
8994 * bar;
8995 */
8996 n = 0;
8997 if (cin_ends_in(l, (char_u *)",", NULL)
8998 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8999 {
9000 /* take us back to opening paren */
9001 if (find_last_paren(l, '(', ')')
9002 && (trypos = find_match_paren(
9003 curbuf->b_ind_maxparen)) != NULL)
9004 curwin->w_cursor = *trypos;
9005
9006 /* For a line ending in ',' that is a continuation line go
9007 * back to the first line with a backslash:
9008 * char *foo = "bla\
9009 * bla",
9010 * here;
9011 */
9012 while (n == 0 && curwin->w_cursor.lnum > 1)
9013 {
9014 l = ml_get(curwin->w_cursor.lnum - 1);
9015 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9016 break;
9017 --curwin->w_cursor.lnum;
9018 curwin->w_cursor.col = 0;
9019 }
9020
9021 amount = get_indent(); /* XXX */
9022
9023 if (amount == 0)
9024 amount = cin_first_id_amount();
9025 if (amount == 0)
9026 amount = ind_continuation;
9027 break;
9028 }
9029
9030 /*
9031 * If the line looks like a function declaration, and we're
9032 * not in a comment, put it the left margin.
9033 */
9034 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9035 break;
9036 l = ml_get_curline();
9037
9038 /*
9039 * Finding the closing '}' of a previous function. Put
9040 * current line at the left margin. For when 'cino' has "fs".
9041 */
9042 if (*skipwhite(l) == '}')
9043 break;
9044
9045 /* (matching {)
9046 * If the previous line ends on '};' (maybe followed by
9047 * comments) align at column 0. For example:
9048 * char *string_array[] = { "foo",
9049 * / * x * / "b};ar" }; / * foobar * /
9050 */
9051 if (cin_ends_in(l, (char_u *)"};", NULL))
9052 break;
9053
9054 /*
9055 * If the previous line ends on '[' we are probably in an
9056 * array constant:
9057 * something = [
9058 * 234, <- extra indent
9059 */
9060 if (cin_ends_in(l, (char_u *)"[", NULL))
9061 {
9062 amount = get_indent() + ind_continuation;
9063 break;
9064 }
9065
9066 /*
9067 * Find a line only has a semicolon that belongs to a previous
9068 * line ending in '}', e.g. before an #endif. Don't increase
9069 * indent then.
9070 */
9071 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9072 {
9073 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
9075 while (curwin->w_cursor.lnum > 1)
9076 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009077 look = ml_get(--curwin->w_cursor.lnum);
9078 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009079 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009081 }
9082 if (curwin->w_cursor.lnum > 0
9083 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009086 curwin->w_cursor = curpos_save;
9087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009089 /*
9090 * If the PREVIOUS line is a function declaration, the current
9091 * line (and the ones that follow) needs to be indented as
9092 * parameters.
9093 */
9094 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9095 {
9096 amount = curbuf->b_ind_param;
9097 break;
9098 }
9099
9100 /*
9101 * If the previous line ends in ';' and the line before the
9102 * previous line ends in ',' or '\', ident to column zero:
9103 * int foo,
9104 * bar;
9105 * indent_to_0 here;
9106 */
9107 if (cin_ends_in(l, (char_u *)";", NULL))
9108 {
9109 l = ml_get(curwin->w_cursor.lnum - 1);
9110 if (cin_ends_in(l, (char_u *)",", NULL)
9111 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9112 break;
9113 l = ml_get_curline();
9114 }
9115
9116 /*
9117 * Doesn't look like anything interesting -- so just
9118 * use the indent of this line.
9119 *
9120 * Position the cursor over the rightmost paren, so that
9121 * matching it will take us back to the start of the line.
9122 */
9123 find_last_paren(l, '(', ')');
9124
9125 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9126 curwin->w_cursor = *trypos;
9127 amount = get_indent(); /* XXX */
9128 break;
9129 }
9130
9131 /* add extra indent for a comment */
9132 if (cin_iscomment(theline))
9133 amount += curbuf->b_ind_comment;
9134
9135 /* add extra indent if the previous line ended in a backslash:
9136 * "asdfasdf\
9137 * here";
9138 * char *foo = "asdf\
9139 * here";
9140 */
9141 if (cur_curpos.lnum > 1)
9142 {
9143 l = ml_get(cur_curpos.lnum - 1);
9144 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9145 {
9146 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9147 if (cur_amount > 0)
9148 amount = cur_amount;
9149 else if (cur_amount == 0)
9150 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 }
9152 }
9153
9154theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009155 if (amount < 0)
9156 amount = 0;
9157
9158laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 /* put the cursor back where it belongs */
9160 curwin->w_cursor = cur_curpos;
9161
9162 vim_free(linecopy);
9163
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 return amount;
9165}
9166
9167 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009168find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
9170 char_u *look;
9171 pos_T *theirscope;
9172 char_u *mightbeif;
9173 int elselevel;
9174 int whilelevel;
9175
9176 if (lookfor == LOOKFOR_IF)
9177 {
9178 elselevel = 1;
9179 whilelevel = 0;
9180 }
9181 else
9182 {
9183 elselevel = 0;
9184 whilelevel = 1;
9185 }
9186
9187 curwin->w_cursor.col = 0;
9188
9189 while (curwin->w_cursor.lnum > ourscope + 1)
9190 {
9191 curwin->w_cursor.lnum--;
9192 curwin->w_cursor.col = 0;
9193
9194 look = cin_skipcomment(ml_get_curline());
9195 if (cin_iselse(look)
9196 || cin_isif(look)
9197 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009198 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
9200 /*
9201 * if we've gone outside the braces entirely,
9202 * we must be out of scope...
9203 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009204 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 if (theirscope == NULL)
9206 break;
9207
9208 /*
9209 * and if the brace enclosing this is further
9210 * back than the one enclosing the else, we're
9211 * out of luck too.
9212 */
9213 if (theirscope->lnum < ourscope)
9214 break;
9215
9216 /*
9217 * and if they're enclosed in a *deeper* brace,
9218 * then we can ignore it because it's in a
9219 * different scope...
9220 */
9221 if (theirscope->lnum > ourscope)
9222 continue;
9223
9224 /*
9225 * if it was an "else" (that's not an "else if")
9226 * then we need to go back to another if, so
9227 * increment elselevel
9228 */
9229 look = cin_skipcomment(ml_get_curline());
9230 if (cin_iselse(look))
9231 {
9232 mightbeif = cin_skipcomment(look + 4);
9233 if (!cin_isif(mightbeif))
9234 ++elselevel;
9235 continue;
9236 }
9237
9238 /*
9239 * if it was a "while" then we need to go back to
9240 * another "do", so increment whilelevel. XXX
9241 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009242 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 {
9244 ++whilelevel;
9245 continue;
9246 }
9247
9248 /* If it's an "if" decrement elselevel */
9249 look = cin_skipcomment(ml_get_curline());
9250 if (cin_isif(look))
9251 {
9252 elselevel--;
9253 /*
9254 * When looking for an "if" ignore "while"s that
9255 * get in the way.
9256 */
9257 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9258 whilelevel = 0;
9259 }
9260
9261 /* If it's a "do" decrement whilelevel */
9262 if (cin_isdo(look))
9263 whilelevel--;
9264
9265 /*
9266 * if we've used up all the elses, then
9267 * this must be the if that we want!
9268 * match the indent level of that if.
9269 */
9270 if (elselevel <= 0 && whilelevel <= 0)
9271 {
9272 return OK;
9273 }
9274 }
9275 }
9276 return FAIL;
9277}
9278
9279# if defined(FEAT_EVAL) || defined(PROTO)
9280/*
9281 * Get indent level from 'indentexpr'.
9282 */
9283 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009284get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009286 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009287 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009288 pos_T save_pos;
9289 colnr_T save_curswant;
9290 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009292 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9293 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009295 /* Save and restore cursor position and curswant, in case it was changed
9296 * via :normal commands */
9297 save_pos = curwin->w_cursor;
9298 save_curswant = curwin->w_curswant;
9299 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009301 if (use_sandbox)
9302 ++sandbox;
9303 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009304
9305 /* Need to make a copy, the 'indentexpr' option could be changed while
9306 * evaluating it. */
9307 inde_copy = vim_strsave(curbuf->b_p_inde);
9308 if (inde_copy != NULL)
9309 {
9310 indent = (int)eval_to_number(inde_copy);
9311 vim_free(inde_copy);
9312 }
9313
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009314 if (use_sandbox)
9315 --sandbox;
9316 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317
9318 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9319 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9320 * command. */
9321 save_State = State;
9322 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009323 curwin->w_cursor = save_pos;
9324 curwin->w_curswant = save_curswant;
9325 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 check_cursor();
9327 State = save_State;
9328
9329 /* If there is an error, just keep the current indent. */
9330 if (indent < 0)
9331 indent = get_indent();
9332
9333 return indent;
9334}
9335# endif
9336
9337#endif /* FEAT_CINDENT */
9338
9339#if defined(FEAT_LISP) || defined(PROTO)
9340
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009341static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342
9343 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009344lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345{
9346 char_u buf[LSIZE];
9347 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009348 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349
9350 while (*word != NUL)
9351 {
9352 (void)copy_option_part(&word, buf, LSIZE, ",");
9353 len = (int)STRLEN(buf);
9354 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9355 return TRUE;
9356 }
9357 return FALSE;
9358}
9359
9360/*
9361 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9362 * The incompatible newer method is quite a bit better at indenting
9363 * code in lisp-like languages than the traditional one; it's still
9364 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9365 *
9366 * TODO:
9367 * Findmatch() should be adapted for lisp, also to make showmatch
9368 * work correctly: now (v5.3) it seems all C/C++ oriented:
9369 * - it does not recognize the #\( and #\) notations as character literals
9370 * - it doesn't know about comments starting with a semicolon
9371 * - it incorrectly interprets '(' as a character literal
9372 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009373 * Update from Sergey Khorev:
9374 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 */
9376 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009377get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009379 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 int amount;
9381 char_u *that;
9382 colnr_T col;
9383 colnr_T firsttry;
9384 int parencount, quotecount;
9385 int vi_lisp;
9386
9387 /* Set vi_lisp to use the vi-compatible method */
9388 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9389
9390 realpos = curwin->w_cursor;
9391 curwin->w_cursor.col = 0;
9392
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009393 if ((pos = findmatch(NULL, '(')) == NULL)
9394 pos = findmatch(NULL, '[');
9395 else
9396 {
9397 paren = *pos;
9398 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009399 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009400 pos = &paren;
9401 }
9402 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 {
9404 /* Extra trick: Take the indent of the first previous non-white
9405 * line that is at the same () level. */
9406 amount = -1;
9407 parencount = 0;
9408
9409 while (--curwin->w_cursor.lnum >= pos->lnum)
9410 {
9411 if (linewhite(curwin->w_cursor.lnum))
9412 continue;
9413 for (that = ml_get_curline(); *that != NUL; ++that)
9414 {
9415 if (*that == ';')
9416 {
9417 while (*(that + 1) != NUL)
9418 ++that;
9419 continue;
9420 }
9421 if (*that == '\\')
9422 {
9423 if (*(that + 1) != NUL)
9424 ++that;
9425 continue;
9426 }
9427 if (*that == '"' && *(that + 1) != NUL)
9428 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009429 while (*++that && *that != '"')
9430 {
9431 /* skipping escaped characters in the string */
9432 if (*that == '\\')
9433 {
9434 if (*++that == NUL)
9435 break;
9436 if (that[1] == NUL)
9437 {
9438 ++that;
9439 break;
9440 }
9441 }
9442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009444 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009446 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 --parencount;
9448 }
9449 if (parencount == 0)
9450 {
9451 amount = get_indent();
9452 break;
9453 }
9454 }
9455
9456 if (amount == -1)
9457 {
9458 curwin->w_cursor.lnum = pos->lnum;
9459 curwin->w_cursor.col = pos->col;
9460 col = pos->col;
9461
9462 that = ml_get_curline();
9463
9464 if (vi_lisp && get_indent() == 0)
9465 amount = 2;
9466 else
9467 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009468 char_u *line = that;
9469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 amount = 0;
9471 while (*that && col)
9472 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009473 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 col--;
9475 }
9476
9477 /*
9478 * Some keywords require "body" indenting rules (the
9479 * non-standard-lisp ones are Scheme special forms):
9480 *
9481 * (let ((a 1)) instead (let ((a 1))
9482 * (...)) of (...))
9483 */
9484
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009485 if (!vi_lisp && (*that == '(' || *that == '[')
9486 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 amount += 2;
9488 else
9489 {
9490 that++;
9491 amount++;
9492 firsttry = amount;
9493
Bram Moolenaar1c465442017-03-12 20:10:05 +01009494 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009496 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 ++that;
9498 }
9499
9500 if (*that && *that != ';') /* not a comment line */
9501 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009502 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009504 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 firsttry++;
9506
9507 parencount = 0;
9508 quotecount = 0;
9509
9510 if (vi_lisp
9511 || (*that != '"'
9512 && *that != '\''
9513 && *that != '#'
9514 && (*that < '0' || *that > '9')))
9515 {
9516 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009517 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 || quotecount
9519 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009520 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 && !quotecount
9522 && !parencount
9523 && vi_lisp)))
9524 {
9525 if (*that == '"')
9526 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009527 if ((*that == '(' || *that == '[')
9528 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009530 if ((*that == ')' || *that == ']')
9531 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 --parencount;
9533 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009534 amount += lbr_chartabsize_adv(
9535 line, &that, (colnr_T)amount);
9536 amount += lbr_chartabsize_adv(
9537 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
9539 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009540 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009542 amount += lbr_chartabsize(
9543 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 that++;
9545 }
9546 if (!*that || *that == ';')
9547 amount = firsttry;
9548 }
9549 }
9550 }
9551 }
9552 }
9553 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009554 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
9556 curwin->w_cursor = realpos;
9557
9558 return amount;
9559}
9560#endif /* FEAT_LISP */
9561
9562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009563prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009565#if defined(SIGHUP) && defined(SIG_IGN)
9566 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9567 * makes Vim exit and then handling SIGHUP causes various reentrance
9568 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009569 signal(SIGHUP, SIG_IGN);
9570#endif
9571
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572#ifdef FEAT_GUI
9573 if (gui.in_use)
9574 {
9575 gui.dying = TRUE;
9576 out_trash(); /* trash any pending output */
9577 }
9578 else
9579#endif
9580 {
9581 windgoto((int)Rows - 1, 0);
9582
9583 /*
9584 * Switch terminal mode back now, so messages end up on the "normal"
9585 * screen (if there are two screens).
9586 */
9587 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009588 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 out_flush();
9590 }
9591}
9592
9593/*
9594 * Preserve files and exit.
9595 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009596 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9597 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 */
9599 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009600preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 buf_T *buf;
9603
9604 prepare_to_exit();
9605
Bram Moolenaar4770d092006-01-12 23:22:24 +00009606 /* Setting this will prevent free() calls. That avoids calling free()
9607 * recursively when free() was invoked with a bad pointer. */
9608 really_exiting = TRUE;
9609
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 out_str(IObuff);
9611 screen_start(); /* don't know where cursor is now */
9612 out_flush();
9613
9614 ml_close_notmod(); /* close all not-modified buffers */
9615
Bram Moolenaar29323592016-07-24 22:04:11 +02009616 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 {
9618 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9619 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009620 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 screen_start(); /* don't know where cursor is now */
9622 out_flush();
9623 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9624 break;
9625 }
9626 }
9627
9628 ml_close_all(FALSE); /* close all memfiles, without deleting */
9629
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009630 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631
9632 getout(1);
9633}
9634
9635/*
9636 * return TRUE if "fname" exists.
9637 */
9638 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009639vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009641 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
9643 if (mch_stat((char *)fname, &st))
9644 return FALSE;
9645 return TRUE;
9646}
9647
9648/*
9649 * Check for CTRL-C pressed, but only once in a while.
9650 * Should be used instead of ui_breakcheck() for functions that check for
9651 * each line in the file. Calling ui_breakcheck() each time takes too much
9652 * time, because it can be a system call.
9653 */
9654
9655#ifndef BREAKCHECK_SKIP
9656# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9657# define BREAKCHECK_SKIP 200
9658# else
9659# define BREAKCHECK_SKIP 32
9660# endif
9661#endif
9662
9663static int breakcheck_count = 0;
9664
9665 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009666line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668 if (++breakcheck_count >= BREAKCHECK_SKIP)
9669 {
9670 breakcheck_count = 0;
9671 ui_breakcheck();
9672 }
9673}
9674
9675/*
9676 * Like line_breakcheck() but check 10 times less often.
9677 */
9678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009679fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9682 {
9683 breakcheck_count = 0;
9684 ui_breakcheck();
9685 }
9686}
9687
9688/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009689 * Invoke expand_wildcards() for one pattern.
9690 * Expand items like "%:h" before the expansion.
9691 * Returns OK or FAIL.
9692 */
9693 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009694expand_wildcards_eval(
9695 char_u **pat, /* pointer to input pattern */
9696 int *num_file, /* resulting number of files */
9697 char_u ***file, /* array of resulting files */
9698 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009699{
9700 int ret = FAIL;
9701 char_u *eval_pat = NULL;
9702 char_u *exp_pat = *pat;
9703 char_u *ignored_msg;
9704 int usedlen;
9705
9706 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9707 {
9708 ++emsg_off;
9709 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9710 NULL, &ignored_msg, NULL);
9711 --emsg_off;
9712 if (eval_pat != NULL)
9713 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9714 }
9715
9716 if (exp_pat != NULL)
9717 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9718
9719 if (eval_pat != NULL)
9720 {
9721 vim_free(exp_pat);
9722 vim_free(eval_pat);
9723 }
9724
9725 return ret;
9726}
9727
9728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9730 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009731 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 */
9733 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009734expand_wildcards(
9735 int num_pat, /* number of input patterns */
9736 char_u **pat, /* array of input patterns */
9737 int *num_files, /* resulting number of files */
9738 char_u ***files, /* array of resulting files */
9739 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741 int retval;
9742 int i, j;
9743 char_u *p;
9744 int non_suf_match; /* number without matching suffix */
9745
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009746 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747
9748 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009749 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 return retval;
9751
9752#ifdef FEAT_WILDIGN
9753 /*
9754 * Remove names that match 'wildignore'.
9755 */
9756 if (*p_wig)
9757 {
9758 char_u *ffname;
9759
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009760 /* check all files in (*files)[] */
9761 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009763 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 if (ffname == NULL) /* out of memory */
9765 break;
9766# ifdef VMS
9767 vms_remove_version(ffname);
9768# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009769 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009771 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009772 vim_free((*files)[i]);
9773 for (j = i; j + 1 < *num_files; ++j)
9774 (*files)[j] = (*files)[j + 1];
9775 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 --i;
9777 }
9778 vim_free(ffname);
9779 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009780
9781 /* If the number of matches is now zero, we fail. */
9782 if (*num_files == 0)
9783 {
9784 vim_free(*files);
9785 *files = NULL;
9786 return FAIL;
9787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 }
9789#endif
9790
9791 /*
9792 * Move the names where 'suffixes' match to the end.
9793 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009794 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 {
9796 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009797 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009799 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 {
9801 /*
9802 * Move the name without matching suffix to the front
9803 * of the list.
9804 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009805 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009807 (*files)[j] = (*files)[j - 1];
9808 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 }
9810 }
9811 }
9812
9813 return retval;
9814}
9815
9816/*
9817 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9818 */
9819 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009820match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822 int fnamelen, setsuflen;
9823 char_u *setsuf;
9824#define MAXSUFLEN 30 /* maximum length of a file suffix */
9825 char_u suf_buf[MAXSUFLEN];
9826
9827 fnamelen = (int)STRLEN(fname);
9828 setsuflen = 0;
9829 for (setsuf = p_su; *setsuf; )
9830 {
9831 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009832 if (setsuflen == 0)
9833 {
9834 char_u *tail = gettail(fname);
9835
9836 /* empty entry: match name without a '.' */
9837 if (vim_strchr(tail, '.') == NULL)
9838 {
9839 setsuflen = 1;
9840 break;
9841 }
9842 }
9843 else
9844 {
9845 if (fnamelen >= setsuflen
9846 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9847 (size_t)setsuflen) == 0)
9848 break;
9849 setsuflen = 0;
9850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 }
9852 return (setsuflen != 0);
9853}
9854
9855#if !defined(NO_EXPANDPATH) || defined(PROTO)
9856
9857# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009858static int vim_backtick(char_u *p);
9859static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860# endif
9861
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009862# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863/*
9864 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9865 * it's shared between these systems.
9866 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009867# if defined(PROTO)
9868# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869# else
9870# ifdef __BORLANDC__
9871# define _cdecl _RTLENTRYF
9872# endif
9873# endif
9874
9875/*
9876 * comparison function for qsort in dos_expandpath()
9877 */
9878 static int _cdecl
9879pstrcmp(const void *a, const void *b)
9880{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009881 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882}
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009885 * Recursively expand one path component into all matching files and/or
9886 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 * Return the number of matches found.
9888 * "path" has backslashes before chars that are not to be expanded, starting
9889 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009890 * Return the number of matches found.
9891 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 */
9893 static int
9894dos_expandpath(
9895 garray_T *gap,
9896 char_u *path,
9897 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009898 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009899 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009901 char_u *buf;
9902 char_u *path_end;
9903 char_u *p, *s, *e;
9904 int start_len = gap->ga_len;
9905 char_u *pat;
9906 regmatch_T regmatch;
9907 int starts_with_dot;
9908 int matches;
9909 int len;
9910 int starstar = FALSE;
9911 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 WIN32_FIND_DATA fb;
9913 HANDLE hFind = (HANDLE)0;
9914# ifdef FEAT_MBYTE
9915 WIN32_FIND_DATAW wfb;
9916 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9917# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009919 int ok;
9920
9921 /* Expanding "**" may take a long time, check for CTRL-C. */
9922 if (stardepth > 0)
9923 {
9924 ui_breakcheck();
9925 if (got_int)
9926 return 0;
9927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009929 /* Make room for file name. When doing encoding conversion the actual
9930 * length may be quite a bit longer, thus use the maximum possible length. */
9931 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 if (buf == NULL)
9933 return 0;
9934
9935 /*
9936 * Find the first part in the path name that contains a wildcard or a ~1.
9937 * Copy it into buf, including the preceding characters.
9938 */
9939 p = buf;
9940 s = buf;
9941 e = NULL;
9942 path_end = path;
9943 while (*path_end != NUL)
9944 {
9945 /* May ignore a wildcard that has a backslash before it; it will
9946 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9947 if (path_end >= path + wildoff && rem_backslash(path_end))
9948 *p++ = *path_end++;
9949 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9950 {
9951 if (e != NULL)
9952 break;
9953 s = p + 1;
9954 }
9955 else if (path_end >= path + wildoff
9956 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9957 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009958# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 if (has_mbyte)
9960 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009961 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 STRNCPY(p, path_end, len);
9963 p += len;
9964 path_end += len;
9965 }
9966 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 *p++ = *path_end++;
9969 }
9970 e = p;
9971 *e = NUL;
9972
9973 /* now we have one wildcard component between s and e */
9974 /* Remove backslashes between "wildoff" and the start of the wildcard
9975 * component. */
9976 for (p = buf + wildoff; p < s; ++p)
9977 if (rem_backslash(p))
9978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009979 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 --e;
9981 --s;
9982 }
9983
Bram Moolenaar231334e2005-07-25 20:46:57 +00009984 /* Check for "**" between "s" and "e". */
9985 for (p = s; p < e; ++p)
9986 if (p[0] == '*' && p[1] == '*')
9987 starstar = TRUE;
9988
Bram Moolenaard82103e2016-01-17 17:04:05 +01009989 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9991 if (pat == NULL)
9992 {
9993 vim_free(buf);
9994 return 0;
9995 }
9996
9997 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009998 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009999 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 regmatch.rm_ic = TRUE; /* Always ignore case */
10001 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010002 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010003 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 vim_free(pat);
10005
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010006 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 {
10008 vim_free(buf);
10009 return 0;
10010 }
10011
10012 /* remember the pattern or file name being looked for */
10013 matchname = vim_strsave(s);
10014
Bram Moolenaar231334e2005-07-25 20:46:57 +000010015 /* If "**" is by itself, this is the first time we encounter it and more
10016 * is following then find matches without any directory. */
10017 if (!didstar && stardepth < 100 && starstar && e - s == 2
10018 && *path_end == '/')
10019 {
10020 STRCPY(s, path_end + 1);
10021 ++stardepth;
10022 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10023 --stardepth;
10024 }
10025
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 /* Scan all files in the directory with "dir/ *.*" */
10027 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028# ifdef FEAT_MBYTE
10029 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10030 {
10031 /* The active codepage differs from 'encoding'. Attempt using the
10032 * wide function. If it fails because it is not implemented fall back
10033 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010034 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 if (wn != NULL)
10036 {
10037 hFind = FindFirstFileW(wn, &wfb);
10038 if (hFind == INVALID_HANDLE_VALUE
10039 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
10040 {
10041 vim_free(wn);
10042 wn = NULL;
10043 }
10044 }
10045 }
10046
10047 if (wn == NULL)
10048# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010049 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051
10052 while (ok)
10053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054# ifdef FEAT_MBYTE
10055 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010056 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 else
10058# endif
10059 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 /* Ignore entries starting with a dot, unless when asked for. Accept
10061 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010062 if ((p[0] != '.' || starts_with_dot
10063 || ((flags & EW_DODOT)
10064 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010066 || (regmatch.regprog != NULL
10067 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010068 || ((flags & EW_NOTWILD)
10069 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010073
10074 if (starstar && stardepth < 100)
10075 {
10076 /* For "**" in the pattern first go deeper in the tree to
10077 * find matches. */
10078 STRCPY(buf + len, "/**");
10079 STRCPY(buf + len + 3, path_end);
10080 ++stardepth;
10081 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10082 --stardepth;
10083 }
10084
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 STRCPY(buf + len, path_end);
10086 if (mch_has_exp_wildcard(path_end))
10087 {
10088 /* need to expand another component of the path */
10089 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010090 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 }
10092 else
10093 {
10094 /* no more wildcards, check if there is a match */
10095 /* remove backslashes for the remaining components only */
10096 if (*path_end != 0)
10097 backslash_halve(buf + len + 1);
10098 if (mch_getperm(buf) >= 0) /* add existing file */
10099 addfile(gap, buf, flags);
10100 }
10101 }
10102
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103# ifdef FEAT_MBYTE
10104 if (wn != NULL)
10105 {
10106 vim_free(p);
10107 ok = FindNextFileW(hFind, &wfb);
10108 }
10109 else
10110# endif
10111 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112
10113 /* If no more matches and no match was used, try expanding the name
10114 * itself. Finds the long name of a short filename. */
10115 if (!ok && matchname != NULL && gap->ga_len == start_len)
10116 {
10117 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 FindClose(hFind);
10119# ifdef FEAT_MBYTE
10120 if (wn != NULL)
10121 {
10122 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010123 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 if (wn != NULL)
10125 hFind = FindFirstFileW(wn, &wfb);
10126 }
10127 if (wn == NULL)
10128# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010129 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 vim_free(matchname);
10132 matchname = NULL;
10133 }
10134 }
10135
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 FindClose(hFind);
10137# ifdef FEAT_MBYTE
10138 vim_free(wn);
10139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010141 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 vim_free(matchname);
10143
10144 matches = gap->ga_len - start_len;
10145 if (matches > 0)
10146 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10147 sizeof(char_u *), pstrcmp);
10148 return matches;
10149}
10150
10151 int
10152mch_expandpath(
10153 garray_T *gap,
10154 char_u *path,
10155 int flags) /* EW_* flags */
10156{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010157 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010159# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160
Bram Moolenaar231334e2005-07-25 20:46:57 +000010161#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10162 || defined(PROTO)
10163/*
10164 * Unix style wildcard expansion code.
10165 * It's here because it's used both for Unix and Mac.
10166 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010167static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010168
10169 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010170pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010171{
10172 return (pathcmp(*(char **)a, *(char **)b, -1));
10173}
10174
10175/*
10176 * Recursively expand one path component into all matching files and/or
10177 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10178 * "path" has backslashes before chars that are not to be expanded, starting
10179 * at "path + wildoff".
10180 * Return the number of matches found.
10181 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10182 */
10183 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010184unix_expandpath(
10185 garray_T *gap,
10186 char_u *path,
10187 int wildoff,
10188 int flags, /* EW_* flags */
10189 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010190{
10191 char_u *buf;
10192 char_u *path_end;
10193 char_u *p, *s, *e;
10194 int start_len = gap->ga_len;
10195 char_u *pat;
10196 regmatch_T regmatch;
10197 int starts_with_dot;
10198 int matches;
10199 int len;
10200 int starstar = FALSE;
10201 static int stardepth = 0; /* depth for "**" expansion */
10202
10203 DIR *dirp;
10204 struct dirent *dp;
10205
10206 /* Expanding "**" may take a long time, check for CTRL-C. */
10207 if (stardepth > 0)
10208 {
10209 ui_breakcheck();
10210 if (got_int)
10211 return 0;
10212 }
10213
10214 /* make room for file name */
10215 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10216 if (buf == NULL)
10217 return 0;
10218
10219 /*
10220 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010221 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010222 * Copy it into "buf", including the preceding characters.
10223 */
10224 p = buf;
10225 s = buf;
10226 e = NULL;
10227 path_end = path;
10228 while (*path_end != NUL)
10229 {
10230 /* May ignore a wildcard that has a backslash before it; it will
10231 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10232 if (path_end >= path + wildoff && rem_backslash(path_end))
10233 *p++ = *path_end++;
10234 else if (*path_end == '/')
10235 {
10236 if (e != NULL)
10237 break;
10238 s = p + 1;
10239 }
10240 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010241 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010242 || (!p_fic && (flags & EW_ICASE)
10243 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010244 e = p;
10245#ifdef FEAT_MBYTE
10246 if (has_mbyte)
10247 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010248 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010249 STRNCPY(p, path_end, len);
10250 p += len;
10251 path_end += len;
10252 }
10253 else
10254#endif
10255 *p++ = *path_end++;
10256 }
10257 e = p;
10258 *e = NUL;
10259
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010260 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010261 /* Remove backslashes between "wildoff" and the start of the wildcard
10262 * component. */
10263 for (p = buf + wildoff; p < s; ++p)
10264 if (rem_backslash(p))
10265 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010266 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010267 --e;
10268 --s;
10269 }
10270
10271 /* Check for "**" between "s" and "e". */
10272 for (p = s; p < e; ++p)
10273 if (p[0] == '*' && p[1] == '*')
10274 starstar = TRUE;
10275
10276 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010277 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010278 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10279 if (pat == NULL)
10280 {
10281 vim_free(buf);
10282 return 0;
10283 }
10284
10285 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010286 if (flags & EW_ICASE)
10287 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10288 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010289 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010290 if (flags & (EW_NOERROR | EW_NOTWILD))
10291 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010292 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010293 if (flags & (EW_NOERROR | EW_NOTWILD))
10294 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010295 vim_free(pat);
10296
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010297 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010298 {
10299 vim_free(buf);
10300 return 0;
10301 }
10302
10303 /* If "**" is by itself, this is the first time we encounter it and more
10304 * is following then find matches without any directory. */
10305 if (!didstar && stardepth < 100 && starstar && e - s == 2
10306 && *path_end == '/')
10307 {
10308 STRCPY(s, path_end + 1);
10309 ++stardepth;
10310 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10311 --stardepth;
10312 }
10313
10314 /* open the directory for scanning */
10315 *s = NUL;
10316 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10317
10318 /* Find all matching entries */
10319 if (dirp != NULL)
10320 {
10321 for (;;)
10322 {
10323 dp = readdir(dirp);
10324 if (dp == NULL)
10325 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010326 if ((dp->d_name[0] != '.' || starts_with_dot
10327 || ((flags & EW_DODOT)
10328 && dp->d_name[1] != NUL
10329 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010330 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10331 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010332 || ((flags & EW_NOTWILD)
10333 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010334 {
10335 STRCPY(s, dp->d_name);
10336 len = STRLEN(buf);
10337
10338 if (starstar && stardepth < 100)
10339 {
10340 /* For "**" in the pattern first go deeper in the tree to
10341 * find matches. */
10342 STRCPY(buf + len, "/**");
10343 STRCPY(buf + len + 3, path_end);
10344 ++stardepth;
10345 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10346 --stardepth;
10347 }
10348
10349 STRCPY(buf + len, path_end);
10350 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10351 {
10352 /* need to expand another component of the path */
10353 /* remove backslashes for the remaining components only */
10354 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10355 }
10356 else
10357 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010358 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010359
Bram Moolenaar231334e2005-07-25 20:46:57 +000010360 /* no more wildcards, check if there is a match */
10361 /* remove backslashes for the remaining components only */
10362 if (*path_end != NUL)
10363 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010364 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010365 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010366 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010367 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010368#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010369 size_t precomp_len = STRLEN(buf)+1;
10370 char_u *precomp_buf =
10371 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010372
Bram Moolenaar231334e2005-07-25 20:46:57 +000010373 if (precomp_buf)
10374 {
10375 mch_memmove(buf, precomp_buf, precomp_len);
10376 vim_free(precomp_buf);
10377 }
10378#endif
10379 addfile(gap, buf, flags);
10380 }
10381 }
10382 }
10383 }
10384
10385 closedir(dirp);
10386 }
10387
10388 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010389 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010390
10391 matches = gap->ga_len - start_len;
10392 if (matches > 0)
10393 qsort(((char_u **)gap->ga_data) + start_len, matches,
10394 sizeof(char_u *), pstrcmp);
10395 return matches;
10396}
10397#endif
10398
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010399#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010400static int find_previous_pathsep(char_u *path, char_u **psep);
10401static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10402static void expand_path_option(char_u *curdir, garray_T *gap);
10403static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10404static void uniquefy_paths(garray_T *gap, char_u *pattern);
10405static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010406
10407/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010408 * Moves "*psep" back to the previous path separator in "path".
10409 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010410 */
10411 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010412find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010413{
10414 /* skip the current separator */
10415 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010416 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010417
10418 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010419 while (*psep > path)
10420 {
10421 if (vim_ispathsep(**psep))
10422 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010423 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010424 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010425
10426 return FAIL;
10427}
10428
10429/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010430 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10431 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010432 */
10433 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010434is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010435{
10436 int j;
10437 int candidate_len;
10438 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010439 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010440 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010441
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010442 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010443 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010444 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010445 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010446
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010447 candidate_len = (int)STRLEN(maybe_unique);
10448 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010449 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010450 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010451
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010452 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010453 if (fnamecmp(maybe_unique, rival) == 0
10454 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010455 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010456 }
10457
Bram Moolenaar162bd912010-07-28 22:29:10 +020010458 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010459}
10460
10461/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010462 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010463 * paths are expanded to their equivalent fullpath. This includes the "."
10464 * (relative to current buffer directory) and empty path (relative to current
10465 * directory) notations.
10466 *
10467 * TODO: handle upward search (;) and path limiter (**N) notations by
10468 * expanding each into their equivalent path(s).
10469 */
10470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010471expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010472{
10473 char_u *path_option = *curbuf->b_p_path == NUL
10474 ? p_path : curbuf->b_p_path;
10475 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010476 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010477 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010478
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010479 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010480 return;
10481
10482 while (*path_option != NUL)
10483 {
10484 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10485
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010486 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010487 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010488 /* Relative to current buffer:
10489 * "/path/file" + "." -> "/path/"
10490 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010491 if (curbuf->b_ffname == NULL)
10492 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010493 p = gettail(curbuf->b_ffname);
10494 len = (int)(p - curbuf->b_ffname);
10495 if (len + (int)STRLEN(buf) >= MAXPATHL)
10496 continue;
10497 if (buf[1] == NUL)
10498 buf[len] = NUL;
10499 else
10500 STRMOVE(buf + len, buf + 2);
10501 mch_memmove(buf, curbuf->b_ffname, len);
10502 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010503 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010504 else if (buf[0] == NUL)
10505 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010506 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010507 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010508 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010509 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010510 else if (!mch_isFullName(buf))
10511 {
10512 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010513 len = (int)STRLEN(curdir);
10514 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010515 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010516 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010517 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010518 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010519 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010520 }
10521
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010522 if (ga_grow(gap, 1) == FAIL)
10523 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010524
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010525# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010526 /* Avoid the path ending in a backslash, it fails when a comma is
10527 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010528 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010529 if (buf[len - 1] == '\\')
10530 buf[len - 1] = '/';
10531# endif
10532
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010533 p = vim_strsave(buf);
10534 if (p == NULL)
10535 break;
10536 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010537 }
10538
10539 vim_free(buf);
10540}
10541
10542/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010543 * Returns a pointer to the file or directory name in "fname" that matches the
10544 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010545 *
10546 * path: /foo/bar/baz
10547 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010548 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010549 */
10550 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010551get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010552{
10553 int i;
10554 int maxlen = 0;
10555 char_u **path_part = (char_u **)gap->ga_data;
10556 char_u *cutoff = NULL;
10557
10558 for (i = 0; i < gap->ga_len; i++)
10559 {
10560 int j = 0;
10561
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010562 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010563# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010564 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10565#endif
10566 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010567 j++;
10568 if (j > maxlen)
10569 {
10570 maxlen = j;
10571 cutoff = &fname[j];
10572 }
10573 }
10574
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010575 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010576 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010577 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010578 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010579
10580 return cutoff;
10581}
10582
10583/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010584 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10585 * that they are unique with respect to each other while conserving the part
10586 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010587 */
10588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010589uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010590{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010591 int i;
10592 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010593 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010594 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 char_u *pat;
10596 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010597 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010598 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010599 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010600 char_u **in_curdir = NULL;
10601 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010602
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010603 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010604 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010605
10606 /*
10607 * We need to prepend a '*' at the beginning of file_pattern so that the
10608 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010609 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010610 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010611 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010612 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010613 if (file_pattern == NULL)
10614 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010615 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010616 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010617 STRCAT(file_pattern, pattern);
10618 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10619 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010620 if (pat == NULL)
10621 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010622
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010623 regmatch.rm_ic = TRUE; /* always ignore case */
10624 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10625 vim_free(pat);
10626 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010627 return;
10628
Bram Moolenaar162bd912010-07-28 22:29:10 +020010629 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010630 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010631 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010632 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010633
10634 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010635 if (in_curdir == NULL)
10636 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010637
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010638 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010639 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640 char_u *path = fnames[i];
10641 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010642 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010643 char_u *pathsep_p;
10644 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010645
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010646 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010647 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010648 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010649 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010650 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010651
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010652 /* Shorten the filename while maintaining its uniqueness */
10653 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010654
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010655 /* Don't assume all files can be reached without path when search
10656 * pattern starts with star star slash, so only remove path_cutoff
10657 * when possible. */
10658 if (pattern[0] == '*' && pattern[1] == '*'
10659 && vim_ispathsep_nocolon(pattern[2])
10660 && path_cutoff != NULL
10661 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10662 && is_unique(path_cutoff, gap, i))
10663 {
10664 sort_again = TRUE;
10665 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10666 }
10667 else
10668 {
10669 /* Here all files can be reached without path, so get shortest
10670 * unique path. We start at the end of the path. */
10671 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010672
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010673 while (find_previous_pathsep(path, &pathsep_p))
10674 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10675 && is_unique(pathsep_p + 1, gap, i)
10676 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10677 {
10678 sort_again = TRUE;
10679 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10680 break;
10681 }
10682 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010683
10684 if (mch_isFullName(path))
10685 {
10686 /*
10687 * Last resort: shorten relative to curdir if possible.
10688 * 'possible' means:
10689 * 1. It is under the current directory.
10690 * 2. The result is actually shorter than the original.
10691 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010692 * Before curdir After
10693 * /foo/bar/file.txt /foo/bar ./file.txt
10694 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10695 * /file.txt / /file.txt
10696 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010697 */
10698 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010699 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010700#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010701 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010702 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010703 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010704 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010705 && !vim_ispathsep(*short_name)
10706#endif
10707 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010708 {
10709 STRCPY(path, ".");
10710 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010711 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010712 }
10713 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010714 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010715 }
10716
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010717 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010718 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010719 {
10720 char_u *rel_path;
10721 char_u *path = in_curdir[i];
10722
10723 if (path == NULL)
10724 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010725
10726 /* If the {filename} is not unique, change it to ./{filename}.
10727 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010728 short_name = shorten_fname(path, curdir);
10729 if (short_name == NULL)
10730 short_name = path;
10731 if (is_unique(short_name, gap, i))
10732 {
10733 STRCPY(fnames[i], short_name);
10734 continue;
10735 }
10736
10737 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10738 if (rel_path == NULL)
10739 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010740 STRCPY(rel_path, ".");
10741 add_pathsep(rel_path);
10742 STRCAT(rel_path, short_name);
10743
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010744 vim_free(fnames[i]);
10745 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010746 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010747 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010748 }
10749
Bram Moolenaar162bd912010-07-28 22:29:10 +020010750theend:
10751 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010752 if (in_curdir != NULL)
10753 {
10754 for (i = 0; i < gap->ga_len; i++)
10755 vim_free(in_curdir[i]);
10756 vim_free(in_curdir);
10757 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010758 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010759 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010760
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010761 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010762 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010763}
10764
10765/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010766 * Calls globpath() with 'path' values for the given pattern and stores the
10767 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010768 * Returns the total number of matches.
10769 */
10770 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010771expand_in_path(
10772 garray_T *gap,
10773 char_u *pattern,
10774 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010775{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010776 char_u *curdir;
10777 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010778 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010779
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010780 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010781 return 0;
10782 mch_dirname(curdir, MAXPATHL);
10783
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010784 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010785 expand_path_option(curdir, &path_ga);
10786 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010787 if (path_ga.ga_len == 0)
10788 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010789
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010790 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010791 ga_clear_strings(&path_ga);
10792 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010793 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010794
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010795 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010796 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010797
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010798 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010799}
10800#endif
10801
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010802#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10803/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010804 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10805 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010806 */
10807 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010808remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010809{
10810 int i;
10811 int j;
10812 char_u **fnames = (char_u **)gap->ga_data;
10813
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010814 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010815 for (i = gap->ga_len - 1; i > 0; --i)
10816 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10817 {
10818 vim_free(fnames[i]);
10819 for (j = i + 1; j < gap->ga_len; ++j)
10820 fnames[j - 1] = fnames[j];
10821 --gap->ga_len;
10822 }
10823}
10824#endif
10825
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010826static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010827
10828/*
10829 * Return TRUE if "p" contains what looks like an environment variable.
10830 * Allowing for escaping.
10831 */
10832 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010833has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010834{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010835 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010836 {
10837 if (*p == '\\' && p[1] != NUL)
10838 ++p;
10839 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010840#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010841 "$%"
10842#else
10843 "$"
10844#endif
10845 , *p) != NULL)
10846 return TRUE;
10847 }
10848 return FALSE;
10849}
10850
10851#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010852static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010853
10854/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010855 * Return TRUE if "p" contains a special wildcard character, one that Vim
10856 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010857 */
10858 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010859has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010860{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010861 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010862 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010863 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010864 if (*p == '\\' && p[1] != NUL)
10865 ++p;
10866 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10867 return TRUE;
10868 }
10869 return FALSE;
10870}
10871#endif
10872
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873/*
10874 * Generic wildcard expansion code.
10875 *
10876 * Characters in "pat" that should not be expanded must be preceded with a
10877 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10878 *
10879 * Return FAIL when no single file was found. In this case "num_file" is not
10880 * set, and "file" may contain an error message.
10881 * Return OK when some files found. "num_file" is set to the number of
10882 * matches, "file" to the array of matches. Call FreeWild() later.
10883 */
10884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010885gen_expand_wildcards(
10886 int num_pat, /* number of input patterns */
10887 char_u **pat, /* array of input patterns */
10888 int *num_file, /* resulting number of files */
10889 char_u ***file, /* array of resulting files */
10890 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891{
10892 int i;
10893 garray_T ga;
10894 char_u *p;
10895 static int recursive = FALSE;
10896 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010897 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010898#if defined(FEAT_SEARCHPATH)
10899 int did_expand_in_path = FALSE;
10900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901
10902 /*
10903 * expand_env() is called to expand things like "~user". If this fails,
10904 * it calls ExpandOne(), which brings us back here. In this case, always
10905 * call the machine specific expansion function, if possible. Otherwise,
10906 * return FAIL.
10907 */
10908 if (recursive)
10909#ifdef SPECIAL_WILDCHAR
10910 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10911#else
10912 return FAIL;
10913#endif
10914
10915#ifdef SPECIAL_WILDCHAR
10916 /*
10917 * If there are any special wildcard characters which we cannot handle
10918 * here, call machine specific function for all the expansion. This
10919 * avoids starting the shell for each argument separately.
10920 * For `=expr` do use the internal function.
10921 */
10922 for (i = 0; i < num_pat; i++)
10923 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010924 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925# ifdef VIM_BACKTICK
10926 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10927# endif
10928 )
10929 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10930 }
10931#endif
10932
10933 recursive = TRUE;
10934
10935 /*
10936 * The matching file names are stored in a growarray. Init it empty.
10937 */
10938 ga_init2(&ga, (int)sizeof(char_u *), 30);
10939
10940 for (i = 0; i < num_pat; ++i)
10941 {
10942 add_pat = -1;
10943 p = pat[i];
10944
10945#ifdef VIM_BACKTICK
10946 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010949 if (add_pat == -1)
10950 retval = FAIL;
10951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 else
10953#endif
10954 {
10955 /*
10956 * First expand environment variables, "~/" and "~user/".
10957 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010958 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010960 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 if (p == NULL)
10962 p = pat[i];
10963#ifdef UNIX
10964 /*
10965 * On Unix, if expand_env() can't expand an environment
10966 * variable, use the shell to do that. Discard previously
10967 * found file names and start all over again.
10968 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010969 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 {
10971 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010972 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010974 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 recursive = FALSE;
10976 return i;
10977 }
10978#endif
10979 }
10980
10981 /*
10982 * If there are wildcards: Expand file names and add each match to
10983 * the list. If there is no match, and EW_NOTFOUND is given, add
10984 * the pattern.
10985 * If there are no wildcards: Add the file name if it exists or
10986 * when EW_NOTFOUND is given.
10987 */
10988 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010989 {
10990#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010991 if ((flags & EW_PATH)
10992 && !mch_isFullName(p)
10993 && !(p[0] == '.'
10994 && (vim_ispathsep(p[1])
10995 || (p[1] == '.' && vim_ispathsep(p[2]))))
10996 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010997 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010998 /* :find completion where 'path' is used.
10999 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011000 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011001 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011002 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011003 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011004 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011005 else
11006#endif
11007 add_pat = mch_expandpath(&ga, p, flags);
11008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 }
11010
11011 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11012 {
11013 char_u *t = backslash_halve_save(p);
11014
11015#if defined(MACOS_CLASSIC)
11016 slash_to_colon(t);
11017#endif
11018 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11019 * "vim c:/" work. */
11020 if (flags & EW_NOTFOUND)
11021 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 addfile(&ga, t, flags);
11024 vim_free(t);
11025 }
11026
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011027#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011028 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011029 uniquefy_paths(&ga, p);
11030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 if (p != pat[i])
11032 vim_free(p);
11033 }
11034
11035 *num_file = ga.ga_len;
11036 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11037
11038 recursive = FALSE;
11039
Bram Moolenaar336bd622016-01-17 18:23:58 +010011040 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041}
11042
11043# ifdef VIM_BACKTICK
11044
11045/*
11046 * Return TRUE if we can expand this backtick thing here.
11047 */
11048 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011049vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050{
11051 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11052}
11053
11054/*
11055 * Expand an item in `backticks` by executing it as a command.
11056 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011057 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 */
11059 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011060expand_backtick(
11061 garray_T *gap,
11062 char_u *pat,
11063 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064{
11065 char_u *p;
11066 char_u *cmd;
11067 char_u *buffer;
11068 int cnt = 0;
11069 int i;
11070
11071 /* Create the command: lop off the backticks. */
11072 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11073 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011074 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075
11076#ifdef FEAT_EVAL
11077 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011078 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 else
11080#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011081 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011082 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 vim_free(cmd);
11084 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011085 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086
11087 cmd = buffer;
11088 while (*cmd != NUL)
11089 {
11090 cmd = skipwhite(cmd); /* skip over white space */
11091 p = cmd;
11092 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11093 ++p;
11094 /* add an entry if it is not empty */
11095 if (p > cmd)
11096 {
11097 i = *p;
11098 *p = NUL;
11099 addfile(gap, cmd, flags);
11100 *p = i;
11101 ++cnt;
11102 }
11103 cmd = p;
11104 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11105 ++cmd;
11106 }
11107
11108 vim_free(buffer);
11109 return cnt;
11110}
11111# endif /* VIM_BACKTICK */
11112
11113/*
11114 * Add a file to a file list. Accepted flags:
11115 * EW_DIR add directories
11116 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011117 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 * EW_NOTFOUND add even when it doesn't exist
11119 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011120 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 */
11122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011123addfile(
11124 garray_T *gap,
11125 char_u *f, /* filename */
11126 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127{
11128 char_u *p;
11129 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011130 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011132 /* if the file/dir/link doesn't exist, may not add it */
11133 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011134 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 return;
11136
11137#ifdef FNAME_ILLEGAL
11138 /* if the file/dir contains illegal characters, don't add it */
11139 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11140 return;
11141#endif
11142
11143 isdir = mch_isdir(f);
11144 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11145 return;
11146
Bram Moolenaarb5971142015-03-21 17:32:19 +010011147 /* If the file isn't executable, may not add it. Do accept directories.
11148 * When invoked from expand_shellcmd() do not use $PATH. */
11149 if (!isdir && (flags & EW_EXEC)
11150 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011151 return;
11152
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 /* Make room for another item in the file list. */
11154 if (ga_grow(gap, 1) == FAIL)
11155 return;
11156
11157 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11158 if (p == NULL)
11159 return;
11160
11161 STRCPY(p, f);
11162#ifdef BACKSLASH_IN_FILENAME
11163 slash_adjust(p);
11164#endif
11165 /*
11166 * Append a slash or backslash after directory names if none is present.
11167 */
11168#ifndef DONT_ADD_PATHSEP_TO_DIR
11169 if (isdir && (flags & EW_ADDSLASH))
11170 add_pathsep(p);
11171#endif
11172 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173}
11174#endif /* !NO_EXPANDPATH */
11175
11176#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11177
11178#ifndef SEEK_SET
11179# define SEEK_SET 0
11180#endif
11181#ifndef SEEK_END
11182# define SEEK_END 2
11183#endif
11184
11185/*
11186 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011187 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11188 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 * Returns an allocated string, or NULL for error.
11190 */
11191 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011192get_cmd_output(
11193 char_u *cmd,
11194 char_u *infile, /* optional input file name */
11195 int flags, /* can be SHELL_SILENT */
11196 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197{
11198 char_u *tempname;
11199 char_u *command;
11200 char_u *buffer = NULL;
11201 int len;
11202 int i = 0;
11203 FILE *fd;
11204
11205 if (check_restricted() || check_secure())
11206 return NULL;
11207
11208 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011209 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 {
11211 EMSG(_(e_notmp));
11212 return NULL;
11213 }
11214
11215 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011216 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 if (command == NULL)
11218 goto done;
11219
11220 /*
11221 * Call the shell to execute the command (errors are ignored).
11222 * Don't check timestamps here.
11223 */
11224 ++no_check_timestamps;
11225 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11226 --no_check_timestamps;
11227
11228 vim_free(command);
11229
11230 /*
11231 * read the names from the file into memory
11232 */
11233# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011234 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 fd = mch_fopen((char *)tempname, "r");
11236# else
11237 fd = mch_fopen((char *)tempname, READBIN);
11238# endif
11239
11240 if (fd == NULL)
11241 {
11242 EMSG2(_(e_notopen), tempname);
11243 goto done;
11244 }
11245
11246 fseek(fd, 0L, SEEK_END);
11247 len = ftell(fd); /* get size of temp file */
11248 fseek(fd, 0L, SEEK_SET);
11249
11250 buffer = alloc(len + 1);
11251 if (buffer != NULL)
11252 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11253 fclose(fd);
11254 mch_remove(tempname);
11255 if (buffer == NULL)
11256 goto done;
11257#ifdef VMS
11258 len = i; /* VMS doesn't give us what we asked for... */
11259#endif
11260 if (i != len)
11261 {
11262 EMSG2(_(e_notread), tempname);
11263 vim_free(buffer);
11264 buffer = NULL;
11265 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011266 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011267 {
11268 /* Change NUL into SOH, otherwise the string is truncated. */
11269 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011270 if (buffer[i] == NUL)
11271 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011272
Bram Moolenaar162bd912010-07-28 22:29:10 +020011273 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011274 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011275 else
11276 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
11278done:
11279 vim_free(tempname);
11280 return buffer;
11281}
11282#endif
11283
11284/*
11285 * Free the list of files returned by expand_wildcards() or other expansion
11286 * functions.
11287 */
11288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011289FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011291 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 while (count--)
11294 vim_free(files[count]);
11295 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296}
11297
11298/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011299 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 * Don't do this when still processing a command or a mapping.
11301 * Don't do this when inside a ":normal" command.
11302 */
11303 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011304goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305{
11306 return (p_im && stuff_empty() && typebuf_typed());
11307}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011308
11309/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011310 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011311 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11312 * - Remove any argument. E.g., "csh -f" -> "csh".
11313 * But don't allow a space in the path, so that this works:
11314 * "/usr/bin/csh --rcfile ~/.cshrc"
11315 * But don't do that for Windows, it's common to have a space in the path.
11316 */
11317 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011318get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011319{
11320 char_u *p;
11321
11322#ifdef WIN3264
11323 p = gettail(p_sh);
11324 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11325#else
11326 p = skiptowhite(p_sh);
11327 if (*p == NUL)
11328 {
11329 /* No white space, use the tail. */
11330 p = vim_strsave(gettail(p_sh));
11331 }
11332 else
11333 {
11334 char_u *p1, *p2;
11335
11336 /* Find the last path separator before the space. */
11337 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011338 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011339 if (vim_ispathsep(*p2))
11340 p1 = p2 + 1;
11341 p = vim_strnsave(p1, (int)(p - p1));
11342 }
11343#endif
11344 return p;
11345}