blob: 632571dbe17d9f3c794681ea907fcdb55aa0f19d [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)
2733 /* The text of the preediting area is inserted, but this doesn't
2734 * mean a change of the buffer yet. That is delayed until the
2735 * text is committed. (this means preedit becomes empty) */
2736 if (im_is_preediting() && !xim_changed_while_preediting)
2737 return;
2738 xim_changed_while_preediting = FALSE;
2739#endif
2740
2741 if (!curbuf->b_changed)
2742 {
2743 int save_msg_scroll = msg_scroll;
2744
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002745 /* Give a warning about changing a read-only file. This may also
2746 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 /* Create a swap file if that is wanted.
2750 * Don't do this for "nofile" and "nowrite" buffer types. */
2751 if (curbuf->b_may_swap
2752#ifdef FEAT_QUICKFIX
2753 && !bt_dontwrite(curbuf)
2754#endif
2755 )
2756 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002757 int save_need_wait_return = need_wait_return;
2758
2759 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 ml_open_file(curbuf);
2761
2762 /* The ml_open_file() can cause an ATTENTION message.
2763 * Wait two seconds, to make sure the user reads this unexpected
2764 * message. Since we could be anywhere, call wait_return() now,
2765 * and don't let the emsg() set msg_scroll. */
2766 if (need_wait_return && emsg_silent == 0)
2767 {
2768 out_flush();
2769 ui_delay(2000L, TRUE);
2770 wait_return(TRUE);
2771 msg_scroll = save_msg_scroll;
2772 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002773 else
2774 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002776 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002778 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779}
2780
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002781/*
2782 * Internal part of changed(), no user interaction.
2783 */
2784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002785changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002786{
2787 curbuf->b_changed = TRUE;
2788 ml_setflags(curbuf);
2789#ifdef FEAT_WINDOWS
2790 check_status(curbuf);
2791 redraw_tabline = TRUE;
2792#endif
2793#ifdef FEAT_TITLE
2794 need_maketitle = TRUE; /* set window title later */
2795#endif
2796}
2797
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002798static void changedOneline(buf_T *buf, linenr_T lnum);
2799static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2800static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802/*
2803 * Changed bytes within a single line for the current buffer.
2804 * - marks the windows on this buffer to be redisplayed
2805 * - marks the buffer changed by calling changed()
2806 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002807 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
2809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002810changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002812 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002814
2815#ifdef FEAT_DIFF
2816 /* Diff highlighting in other diff windows may need to be updated too. */
2817 if (curwin->w_p_diff)
2818 {
2819 win_T *wp;
2820 linenr_T wlnum;
2821
Bram Moolenaar29323592016-07-24 22:04:11 +02002822 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002823 if (wp->w_p_diff && wp != curwin)
2824 {
2825 redraw_win_later(wp, VALID);
2826 wlnum = diff_lnum_win(lnum, wp);
2827 if (wlnum > 0)
2828 changedOneline(wp->w_buffer, wlnum);
2829 }
2830 }
2831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832}
2833
2834 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002835changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002837 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
2839 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002840 if (lnum < buf->b_mod_top)
2841 buf->b_mod_top = lnum;
2842 else if (lnum >= buf->b_mod_bot)
2843 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
2845 else
2846 {
2847 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002848 buf->b_mod_set = TRUE;
2849 buf->b_mod_top = lnum;
2850 buf->b_mod_bot = lnum + 1;
2851 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853}
2854
2855/*
2856 * Appended "count" lines below line "lnum" in the current buffer.
2857 * Must be called AFTER the change and after mark_adjust().
2858 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2859 */
2860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002861appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 changed_lines(lnum + 1, 0, lnum + 1, count);
2864}
2865
2866/*
2867 * Like appended_lines(), but adjust marks first.
2868 */
2869 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002870appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002872 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002873 * be marks there. But it's still needed in diff mode. */
2874 if (lnum + count < curbuf->b_ml.ml_line_count
2875#ifdef FEAT_DIFF
2876 || curwin->w_p_diff
2877#endif
2878 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002879 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 changed_lines(lnum + 1, 0, lnum + 1, count);
2881}
2882
2883/*
2884 * Deleted "count" lines at line "lnum" in the current buffer.
2885 * Must be called AFTER the change and after mark_adjust().
2886 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2887 */
2888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 changed_lines(lnum, 0, lnum + count, -count);
2892}
2893
2894/*
2895 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002896 * Make sure the cursor is on a valid line before calling, a GUI callback may
2897 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 */
2899 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002900deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2903 changed_lines(lnum, 0, lnum + count, -count);
2904}
2905
2906/*
2907 * Changed lines for the current buffer.
2908 * Must be called AFTER the change and after mark_adjust().
2909 * - mark the buffer changed by calling changed()
2910 * - mark the windows on this buffer to be redisplayed
2911 * - invalidate cached values
2912 * "lnum" is the first line that needs displaying, "lnume" the first line
2913 * below the changed lines (BEFORE the change).
2914 * When only inserting lines, "lnum" and "lnume" are equal.
2915 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002916 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 */
2918 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002919changed_lines(
2920 linenr_T lnum, /* first line with change */
2921 colnr_T col, /* column in first line with change */
2922 linenr_T lnume, /* line below last changed line */
2923 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002925 changed_lines_buf(curbuf, lnum, lnume, xtra);
2926
2927#ifdef FEAT_DIFF
2928 if (xtra == 0 && curwin->w_p_diff)
2929 {
2930 /* When the number of lines doesn't change then mark_adjust() isn't
2931 * called and other diff buffers still need to be marked for
2932 * displaying. */
2933 win_T *wp;
2934 linenr_T wlnum;
2935
Bram Moolenaar29323592016-07-24 22:04:11 +02002936 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002937 if (wp->w_p_diff && wp != curwin)
2938 {
2939 redraw_win_later(wp, VALID);
2940 wlnum = diff_lnum_win(lnum, wp);
2941 if (wlnum > 0)
2942 changed_lines_buf(wp->w_buffer, wlnum,
2943 lnume - lnum + wlnum, 0L);
2944 }
2945 }
2946#endif
2947
2948 changed_common(lnum, col, lnume, xtra);
2949}
2950
2951 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002952changed_lines_buf(
2953 buf_T *buf,
2954 linenr_T lnum, /* first line with change */
2955 linenr_T lnume, /* line below last changed line */
2956 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002957{
2958 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
2960 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002961 if (lnum < buf->b_mod_top)
2962 buf->b_mod_top = lnum;
2963 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
2965 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002966 buf->b_mod_bot += xtra;
2967 if (buf->b_mod_bot < lnum)
2968 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002970 if (lnume + xtra > buf->b_mod_bot)
2971 buf->b_mod_bot = lnume + xtra;
2972 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974 else
2975 {
2976 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002977 buf->b_mod_set = TRUE;
2978 buf->b_mod_top = lnum;
2979 buf->b_mod_bot = lnume + xtra;
2980 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982}
2983
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002984/*
2985 * Common code for when a change is was made.
2986 * See changed_lines() for the arguments.
2987 * Careful: may trigger autocommands that reload the buffer.
2988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002990changed_common(
2991 linenr_T lnum,
2992 colnr_T col,
2993 linenr_T lnume,
2994 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002997#ifdef FEAT_WINDOWS
2998 tabpage_T *tp;
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 int i;
3001#ifdef FEAT_JUMPLIST
3002 int cols;
3003 pos_T *p;
3004 int add;
3005#endif
3006
3007 /* mark the buffer as modified */
3008 changed();
3009
3010 /* set the '. mark */
3011 if (!cmdmod.keepjumps)
3012 {
3013 curbuf->b_last_change.lnum = lnum;
3014 curbuf->b_last_change.col = col;
3015
3016#ifdef FEAT_JUMPLIST
3017 /* Create a new entry if a new undo-able change was started or we
3018 * don't have an entry yet. */
3019 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3020 {
3021 if (curbuf->b_changelistlen == 0)
3022 add = TRUE;
3023 else
3024 {
3025 /* Don't create a new entry when the line number is the same
3026 * as the last one and the column is not too far away. Avoids
3027 * creating many entries for typing "xxxxx". */
3028 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3029 if (p->lnum != lnum)
3030 add = TRUE;
3031 else
3032 {
3033 cols = comp_textwidth(FALSE);
3034 if (cols == 0)
3035 cols = 79;
3036 add = (p->col + cols < col || col + cols < p->col);
3037 }
3038 }
3039 if (add)
3040 {
3041 /* This is the first of a new sequence of undo-able changes
3042 * and it's at some distance of the last change. Use a new
3043 * position in the changelist. */
3044 curbuf->b_new_change = FALSE;
3045
3046 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3047 {
3048 /* changelist is full: remove oldest entry */
3049 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3050 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3051 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003052 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 {
3054 /* Correct position in changelist for other windows on
3055 * this buffer. */
3056 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3057 --wp->w_changelistidx;
3058 }
3059 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003060 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 {
3062 /* For other windows, if the position in the changelist is
3063 * at the end it stays at the end. */
3064 if (wp->w_buffer == curbuf
3065 && wp->w_changelistidx == curbuf->b_changelistlen)
3066 ++wp->w_changelistidx;
3067 }
3068 ++curbuf->b_changelistlen;
3069 }
3070 }
3071 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3072 curbuf->b_last_change;
3073 /* The current window is always after the last change, so that "g,"
3074 * takes you back to it. */
3075 curwin->w_changelistidx = curbuf->b_changelistlen;
3076#endif
3077 }
3078
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003079 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
3081 if (wp->w_buffer == curbuf)
3082 {
3083 /* Mark this window to be redrawn later. */
3084 if (wp->w_redr_type < VALID)
3085 wp->w_redr_type = VALID;
3086
3087 /* Check if a change in the buffer has invalidated the cached
3088 * values for the cursor. */
3089#ifdef FEAT_FOLDING
3090 /*
3091 * Update the folds for this window. Can't postpone this, because
3092 * a following operator might work on the whole fold: ">>dd".
3093 */
3094 foldUpdate(wp, lnum, lnume + xtra - 1);
3095
3096 /* The change may cause lines above or below the change to become
3097 * included in a fold. Set lnum/lnume to the first/last line that
3098 * might be displayed differently.
3099 * Set w_cline_folded here as an efficient way to update it when
3100 * inserting lines just above a closed fold. */
3101 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3102 if (wp->w_cursor.lnum == lnum)
3103 wp->w_cline_folded = i;
3104 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3105 if (wp->w_cursor.lnum == lnume)
3106 wp->w_cline_folded = i;
3107
3108 /* If the changed line is in a range of previously folded lines,
3109 * compare with the first line in that range. */
3110 if (wp->w_cursor.lnum <= lnum)
3111 {
3112 i = find_wl_entry(wp, lnum);
3113 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3114 changed_line_abv_curs_win(wp);
3115 }
3116#endif
3117
3118 if (wp->w_cursor.lnum > lnum)
3119 changed_line_abv_curs_win(wp);
3120 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3121 changed_cline_bef_curs_win(wp);
3122 if (wp->w_botline >= lnum)
3123 {
3124 /* Assume that botline doesn't change (inserted lines make
3125 * other lines scroll down below botline). */
3126 approximate_botline_win(wp);
3127 }
3128
3129 /* Check if any w_lines[] entries have become invalid.
3130 * For entries below the change: Correct the lnums for
3131 * inserted/deleted lines. Makes it possible to stop displaying
3132 * after the change. */
3133 for (i = 0; i < wp->w_lines_valid; ++i)
3134 if (wp->w_lines[i].wl_valid)
3135 {
3136 if (wp->w_lines[i].wl_lnum >= lnum)
3137 {
3138 if (wp->w_lines[i].wl_lnum < lnume)
3139 {
3140 /* line included in change */
3141 wp->w_lines[i].wl_valid = FALSE;
3142 }
3143 else if (xtra != 0)
3144 {
3145 /* line below change */
3146 wp->w_lines[i].wl_lnum += xtra;
3147#ifdef FEAT_FOLDING
3148 wp->w_lines[i].wl_lastlnum += xtra;
3149#endif
3150 }
3151 }
3152#ifdef FEAT_FOLDING
3153 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3154 {
3155 /* change somewhere inside this range of folded lines,
3156 * may need to be redrawn */
3157 wp->w_lines[i].wl_valid = FALSE;
3158 }
3159#endif
3160 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003161
3162#ifdef FEAT_FOLDING
3163 /* Take care of side effects for setting w_topline when folds have
3164 * changed. Esp. when the buffer was changed in another window. */
3165 if (hasAnyFolding(wp))
3166 set_topline(wp, wp->w_topline);
3167#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003168 /* relative numbering may require updating more */
3169 if (wp->w_p_rnu)
3170 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 }
3173
3174 /* Call update_screen() later, which checks out what needs to be redrawn,
3175 * since it notices b_mod_set and then uses b_mod_*. */
3176 if (must_redraw < VALID)
3177 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003178
3179#ifdef FEAT_AUTOCMD
3180 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003181 if (lnum <= curwin->w_cursor.lnum
3182 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003183 last_cursormoved.lnum = 0;
3184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185}
3186
3187/*
3188 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3189 */
3190 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003191unchanged(
3192 buf_T *buf,
3193 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003195 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
3197 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003198 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (ff)
3200 save_file_ff(buf);
3201#ifdef FEAT_WINDOWS
3202 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003203 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#endif
3205#ifdef FEAT_TITLE
3206 need_maketitle = TRUE; /* set window title later */
3207#endif
3208 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003209 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#ifdef FEAT_NETBEANS_INTG
3211 netbeans_unmodified(buf);
3212#endif
3213}
3214
3215#if defined(FEAT_WINDOWS) || defined(PROTO)
3216/*
3217 * check_status: called when the status bars for the buffer 'buf'
3218 * need to be updated
3219 */
3220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003221check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 win_T *wp;
3224
Bram Moolenaar29323592016-07-24 22:04:11 +02003225 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (wp->w_buffer == buf && wp->w_status_height)
3227 {
3228 wp->w_redr_status = TRUE;
3229 if (must_redraw < VALID)
3230 must_redraw = VALID;
3231 }
3232}
3233#endif
3234
3235/*
3236 * If the file is readonly, give a warning message with the first change.
3237 * Don't do this for autocommands.
3238 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003239 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003241 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 */
3243 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003244change_warning(
3245 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 mode and 'showmode' is on */
3247{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003248 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (curbuf->b_did_warn == FALSE
3251 && curbufIsChanged() == 0
3252#ifdef FEAT_AUTOCMD
3253 && !autocmd_busy
3254#endif
3255 && curbuf->b_p_ro)
3256 {
3257#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003258 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003260 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (!curbuf->b_p_ro)
3262 return;
3263#endif
3264 /*
3265 * Do what msg() does, but with a column offset if the warning should
3266 * be after the mode message.
3267 */
3268 msg_start();
3269 if (msg_row == Rows - 1)
3270 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003271 msg_source(HL_ATTR(HLF_W));
3272 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003273#ifdef FEAT_EVAL
3274 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 msg_clr_eos();
3277 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003278 if (msg_silent == 0 && !silent_mode
3279#ifdef FEAT_EVAL
3280 && time_for_testing != 1
3281#endif
3282 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
3284 out_flush();
3285 ui_delay(1000L, TRUE); /* give the user time to think about it */
3286 }
3287 curbuf->b_did_warn = TRUE;
3288 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3289 if (msg_row < Rows - 1)
3290 showmode();
3291 }
3292}
3293
3294/*
3295 * Ask for a reply from the user, a 'y' or a 'n'.
3296 * No other characters are accepted, the message is repeated until a valid
3297 * reply is entered or CTRL-C is hit.
3298 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3299 * from any buffers but directly from the user.
3300 *
3301 * return the 'y' or 'n'
3302 */
3303 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 int r = ' ';
3307 int save_State = State;
3308
3309 if (exiting) /* put terminal in raw mode for this question */
3310 settmode(TMODE_RAW);
3311 ++no_wait_return;
3312#ifdef USE_ON_FLY_SCROLL
3313 dont_scroll = TRUE; /* disallow scrolling here */
3314#endif
3315 State = CONFIRM; /* mouse behaves like with :confirm */
3316#ifdef FEAT_MOUSE
3317 setmouse(); /* disables mouse for xterm */
3318#endif
3319 ++no_mapping;
3320 ++allow_keys; /* no mapping here, but recognize keys */
3321
3322 while (r != 'y' && r != 'n')
3323 {
3324 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003325 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 if (direct)
3327 r = get_keystroke();
3328 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003329 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 if (r == Ctrl_C || r == ESC)
3331 r = 'n';
3332 msg_putchar(r); /* show what you typed */
3333 out_flush();
3334 }
3335 --no_wait_return;
3336 State = save_State;
3337#ifdef FEAT_MOUSE
3338 setmouse();
3339#endif
3340 --no_mapping;
3341 --allow_keys;
3342
3343 return r;
3344}
3345
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003346#if defined(FEAT_MOUSE) || defined(PROTO)
3347/*
3348 * Return TRUE if "c" is a mouse key.
3349 */
3350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003351is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003352{
3353 return c == K_LEFTMOUSE
3354 || c == K_LEFTMOUSE_NM
3355 || c == K_LEFTDRAG
3356 || c == K_LEFTRELEASE
3357 || c == K_LEFTRELEASE_NM
3358 || c == K_MIDDLEMOUSE
3359 || c == K_MIDDLEDRAG
3360 || c == K_MIDDLERELEASE
3361 || c == K_RIGHTMOUSE
3362 || c == K_RIGHTDRAG
3363 || c == K_RIGHTRELEASE
3364 || c == K_MOUSEDOWN
3365 || c == K_MOUSEUP
3366 || c == K_MOUSELEFT
3367 || c == K_MOUSERIGHT
3368 || c == K_X1MOUSE
3369 || c == K_X1DRAG
3370 || c == K_X1RELEASE
3371 || c == K_X2MOUSE
3372 || c == K_X2DRAG
3373 || c == K_X2RELEASE;
3374}
3375#endif
3376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377/*
3378 * Get a key stroke directly from the user.
3379 * Ignores mouse clicks and scrollbar events, except a click for the left
3380 * button (used at the more prompt).
3381 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3382 * Disadvantage: typeahead is ignored.
3383 * Translates the interrupt character for unix to ESC.
3384 */
3385 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003386get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003388 char_u *buf = NULL;
3389 int buflen = 150;
3390 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 int len = 0;
3392 int n;
3393 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003394 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 mapped_ctrl_c = FALSE; /* mappings are not used here */
3397 for (;;)
3398 {
3399 cursor_on();
3400 out_flush();
3401
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003402 /* Leave some room for check_termcode() to insert a key code into (max
3403 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3404 * bytes. */
3405 maxlen = (buflen - 6 - len) / 3;
3406 if (buf == NULL)
3407 buf = alloc(buflen);
3408 else if (maxlen < 10)
3409 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003410 char_u *t_buf = buf;
3411
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003412 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003413 * escape sequence. */
3414 buflen += 100;
3415 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003416 if (buf == NULL)
3417 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003418 maxlen = (buflen - 6 - len) / 3;
3419 }
3420 if (buf == NULL)
3421 {
3422 do_outofmem_msg((long_u)buflen);
3423 return ESC; /* panic! */
3424 }
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003427 * terminal code to complete. */
3428 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (n > 0)
3430 {
3431 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003432 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003434 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003436 else if (len > 0)
3437 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar4395a712006-09-05 18:57:57 +00003439 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003440 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003441 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003443
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003444 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003445 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003446 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003447 {
3448 /* Redrawing was postponed, do it now. */
3449 update_screen(0);
3450 setcursor(); /* put cursor back where it belongs */
3451 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003452 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003453 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003454 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003456 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 continue;
3458
3459 /* Handle modifier and/or special key code. */
3460 n = buf[0];
3461 if (n == K_SPECIAL)
3462 {
3463 n = TO_SPECIAL(buf[1], buf[2]);
3464 if (buf[1] == KS_MODIFIER
3465 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003466#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003467 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003468#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003469#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 || n == K_VER_SCROLLBAR
3471 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#endif
3473 )
3474 {
3475 if (buf[1] == KS_MODIFIER)
3476 mod_mask = buf[2];
3477 len -= 3;
3478 if (len > 0)
3479 mch_memmove(buf, buf + 3, (size_t)len);
3480 continue;
3481 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003482 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484#ifdef FEAT_MBYTE
3485 if (has_mbyte)
3486 {
3487 if (MB_BYTE2LEN(n) > len)
3488 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003489 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 n = (*mb_ptr2char)(buf);
3491 }
3492#endif
3493#ifdef UNIX
3494 if (n == intr_char)
3495 n = ESC;
3496#endif
3497 break;
3498 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003499 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
3501 mapped_ctrl_c = save_mapped_ctrl_c;
3502 return n;
3503}
3504
3505/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003506 * Get a number from the user.
3507 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
3509 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003510get_number(
3511 int colon, /* allow colon to abort */
3512 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
3514 int n = 0;
3515 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003516 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003518 if (mouse_used != NULL)
3519 *mouse_used = FALSE;
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 /* When not printing messages, the user won't know what to type, return a
3522 * zero (as if CR was hit). */
3523 if (msg_silent != 0)
3524 return 0;
3525
3526#ifdef USE_ON_FLY_SCROLL
3527 dont_scroll = TRUE; /* disallow scrolling here */
3528#endif
3529 ++no_mapping;
3530 ++allow_keys; /* no mapping here, but recognize keys */
3531 for (;;)
3532 {
3533 windgoto(msg_row, msg_col);
3534 c = safe_vgetc();
3535 if (VIM_ISDIGIT(c))
3536 {
3537 n = n * 10 + c - '0';
3538 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003539 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3542 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003543 if (typed > 0)
3544 {
3545 MSG_PUTS("\b \b");
3546 --typed;
3547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003550#ifdef FEAT_MOUSE
3551 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3552 {
3553 *mouse_used = TRUE;
3554 n = mouse_row + 1;
3555 break;
3556 }
3557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else if (n == 0 && c == ':' && colon)
3559 {
3560 stuffcharReadbuff(':');
3561 if (!exmode_active)
3562 cmdline_row = msg_row;
3563 skip_redraw = TRUE; /* skip redraw once */
3564 do_redraw = FALSE;
3565 break;
3566 }
3567 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3568 break;
3569 }
3570 --no_mapping;
3571 --allow_keys;
3572 return n;
3573}
3574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575/*
3576 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003577 * When "mouse_used" is not NULL allow using the mouse and in that case return
3578 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003579 */
3580 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003581prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003582{
3583 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584 int save_cmdline_row;
3585 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003586
3587 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003588 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003589 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003590 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003591 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003592
Bram Moolenaar203335e2006-09-03 14:35:42 +00003593 /* Set the state such that text can be selected/copied/pasted and we still
3594 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003595 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003596 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003597 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003598 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003599
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003600 i = get_number(TRUE, mouse_used);
3601 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003602 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003603 /* don't call wait_return() now */
3604 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003605 cmdline_row = msg_row - 1;
3606 need_wait_return = FALSE;
3607 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003608 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003610 else
3611 cmdline_row = save_cmdline_row;
3612 State = save_State;
3613
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003614 return i;
3615}
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003618msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 long pn;
3621
3622 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3624 return;
3625
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003626 /* We don't want to overwrite another important message, but do overwrite
3627 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3628 * then "put" reports the last action. */
3629 if (keep_msg != NULL && !keep_msg_more)
3630 return;
3631
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 if (n > 0)
3633 pn = n;
3634 else
3635 pn = -n;
3636
3637 if (pn > p_report)
3638 {
3639 if (pn == 1)
3640 {
3641 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003642 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3643 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003645 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3646 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
3648 else
3649 {
3650 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003651 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3652 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003654 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3655 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003658 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (msg(msg_buf))
3660 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003661 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003662 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 }
3665}
3666
3667/*
3668 * flush map and typeahead buffers and give a warning for an error
3669 */
3670 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003671beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 if (emsg_silent == 0)
3674 {
3675 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003676 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678}
3679
3680/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003681 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 */
3683 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003684vim_beep(
3685 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 if (emsg_silent == 0)
3688 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003689 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3690 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003691#ifdef ELAPSED_FUNC
3692 static int did_init = FALSE;
3693 static ELAPSED_TYPE start_tv;
3694
3695 /* Only beep once per half a second, otherwise a sequence of beeps
3696 * would freeze Vim. */
3697 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3698 {
3699 did_init = TRUE;
3700 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003702 if (p_vb
3703#ifdef FEAT_GUI
3704 /* While the GUI is starting up the termcap is set for
3705 * the GUI but the output still goes to a terminal. */
3706 && !(gui.in_use && gui.starting)
3707#endif
3708 )
3709 out_str_cf(T_VB);
3710 else
3711 out_char(BELL);
3712#ifdef ELAPSED_FUNC
3713 }
3714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003716
3717 /* When 'verbose' is set and we are sourcing a script or executing a
3718 * function give the user a hint where the beep comes from. */
3719 if (vim_strchr(p_debug, 'e') != NULL)
3720 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003721 msg_source(HL_ATTR(HLF_W));
3722 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725}
3726
3727/*
3728 * To get the "real" home directory:
3729 * - get value of $HOME
3730 * For Unix:
3731 * - go to that directory
3732 * - do mch_dirname() to get the real name of that directory.
3733 * This also works with mounts and links.
3734 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3735 */
3736static char_u *homedir = NULL;
3737
3738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003739init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
3741 char_u *var;
3742
Bram Moolenaar05159a02005-02-26 23:04:13 +00003743 /* In case we are called a second time (when 'encoding' changes). */
3744 vim_free(homedir);
3745 homedir = NULL;
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#ifdef VMS
3748 var = mch_getenv((char_u *)"SYS$LOGIN");
3749#else
3750 var = mch_getenv((char_u *)"HOME");
3751#endif
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#ifdef WIN3264
3754 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003755 * Typically, $HOME is not defined on Windows, unless the user has
3756 * specifically defined it for Vim's sake. However, on Windows NT
3757 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3758 * each user. Try constructing $HOME from these.
3759 */
3760 if (var == NULL || *var == NULL)
3761 {
3762 char_u *homedrive, *homepath;
3763
3764 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3765 homepath = mch_getenv((char_u *)"HOMEPATH");
3766 if (homepath == NULL || *homepath == NUL)
3767 homepath = (char_u *)"\\";
3768 if (homedrive != NULL
3769 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3770 {
3771 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3772 if (NameBuff[0] != NUL)
3773 var = NameBuff;
3774 }
3775 }
3776
3777 if (var == NULL)
3778 var = mch_getenv((char_u *)"USERPROFILE");
3779
3780 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 * Weird but true: $HOME may contain an indirect reference to another
3782 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3783 * when $HOME is being set.
3784 */
3785 if (var != NULL && *var == '%')
3786 {
3787 char_u *p;
3788 char_u *exp;
3789
3790 p = vim_strchr(var + 1, '%');
3791 if (p != NULL)
3792 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003793 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 exp = mch_getenv(NameBuff);
3795 if (exp != NULL && *exp != NUL
3796 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3797 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003798 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801 }
3802 }
3803
Bram Moolenaar48340b62017-08-29 22:08:53 +02003804 if (var != NULL && *var == NUL) /* empty is same as not set */
3805 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
Bram Moolenaar48340b62017-08-29 22:08:53 +02003807# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003808 if (enc_utf8 && var != NULL)
3809 {
3810 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003811 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003812
3813 /* Convert from active codepage to UTF-8. Other conversions are
3814 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003815 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003816 if (pp != NULL)
3817 {
3818 homedir = pp;
3819 return;
3820 }
3821 }
3822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 /*
3825 * Default home dir is C:/
3826 * Best assumption we can make in such a situation.
3827 */
3828 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003829 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (var != NULL)
3833 {
3834#ifdef UNIX
3835 /*
3836 * Change to the directory and get the actual path. This resolves
3837 * links. Don't do it when we can't return.
3838 */
3839 if (mch_dirname(NameBuff, MAXPATHL) == OK
3840 && mch_chdir((char *)NameBuff) == 0)
3841 {
3842 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3843 var = IObuff;
3844 if (mch_chdir((char *)NameBuff) != 0)
3845 EMSG(_(e_prev_dir));
3846 }
3847#endif
3848 homedir = vim_strsave(var);
3849 }
3850}
3851
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003852#if defined(EXITFREE) || defined(PROTO)
3853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003854free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003855{
3856 vim_free(homedir);
3857}
Bram Moolenaar24305862012-08-15 14:05:05 +02003858
3859# ifdef FEAT_CMDL_COMPL
3860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003861free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003862{
3863 ga_clear_strings(&ga_users);
3864}
3865# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003866#endif
3867
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003869 * Call expand_env() and store the result in an allocated string.
3870 * This is not very memory efficient, this expects the result to be freed
3871 * again soon.
3872 */
3873 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003874expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003875{
3876 return expand_env_save_opt(src, FALSE);
3877}
3878
3879/*
3880 * Idem, but when "one" is TRUE handle the string as one file name, only
3881 * expand "~" at the start.
3882 */
3883 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003884expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003885{
3886 char_u *p;
3887
3888 p = alloc(MAXPATHL);
3889 if (p != NULL)
3890 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3891 return p;
3892}
3893
3894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 * Expand environment variable with path name.
3896 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003897 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 * If anything fails no expansion is done and dst equals src.
3899 */
3900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003901expand_env(
3902 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3903 char_u *dst, /* where to put the result */
3904 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003906 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907}
3908
3909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003910expand_env_esc(
3911 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3912 char_u *dst, /* where to put the result */
3913 int dstlen, /* maximum length of the result */
3914 int esc, /* escape spaces in expanded variables */
3915 int one, /* "srcp" is one file name */
3916 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003918 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 char_u *tail;
3920 int c;
3921 char_u *var;
3922 int copy_char;
3923 int mustfree; /* var was allocated, need to free it later */
3924 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003925 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003927 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003928 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003929
3930 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 --dstlen; /* leave one char space for "\," */
3932 while (*src && dstlen > 0)
3933 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003934#ifdef FEAT_EVAL
3935 /* Skip over `=expr`. */
3936 if (src[0] == '`' && src[1] == '=')
3937 {
3938 size_t len;
3939
3940 var = src;
3941 src += 2;
3942 (void)skip_expr(&src);
3943 if (*src == '`')
3944 ++src;
3945 len = src - var;
3946 if (len > (size_t)dstlen)
3947 len = dstlen;
3948 vim_strncpy(dst, var, len);
3949 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003950 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003951 continue;
3952 }
3953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003955 if ((*src == '$'
3956#ifdef VMS
3957 && at_start
3958#endif
3959 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003960#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 || *src == '%'
3962#endif
3963 || (*src == '~' && at_start))
3964 {
3965 mustfree = FALSE;
3966
3967 /*
3968 * The variable name is copied into dst temporarily, because it may
3969 * be a string in read-only memory and a NUL needs to be appended.
3970 */
3971 if (*src != '~') /* environment var */
3972 {
3973 tail = src + 1;
3974 var = dst;
3975 c = dstlen - 1;
3976
3977#ifdef UNIX
3978 /* Unix has ${var-name} type environment vars */
3979 if (*tail == '{' && !vim_isIDc('{'))
3980 {
3981 tail++; /* ignore '{' */
3982 while (c-- > 0 && *tail && *tail != '}')
3983 *var++ = *tail++;
3984 }
3985 else
3986#endif
3987 {
3988 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003989#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 || (*src == '%' && *tail != '%')
3991#endif
3992 ))
3993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 }
3997
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003998#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999# ifdef UNIX
4000 if (src[1] == '{' && *tail != '}')
4001# else
4002 if (*src == '%' && *tail != '%')
4003# endif
4004 var = NULL;
4005 else
4006 {
4007# ifdef UNIX
4008 if (src[1] == '{')
4009# else
4010 if (*src == '%')
4011#endif
4012 ++tail;
4013#endif
4014 *var = NUL;
4015 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004016#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018#endif
4019 }
4020 /* home directory */
4021 else if ( src[1] == NUL
4022 || vim_ispathsep(src[1])
4023 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4024 {
4025 var = homedir;
4026 tail = src + 1;
4027 }
4028 else /* user directory */
4029 {
4030#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4031 /*
4032 * Copy ~user to dst[], so we can put a NUL after it.
4033 */
4034 tail = src;
4035 var = dst;
4036 c = dstlen - 1;
4037 while ( c-- > 0
4038 && *tail
4039 && vim_isfilec(*tail)
4040 && !vim_ispathsep(*tail))
4041 *var++ = *tail++;
4042 *var = NUL;
4043# ifdef UNIX
4044 /*
4045 * If the system supports getpwnam(), use it.
4046 * Otherwise, or if getpwnam() fails, the shell is used to
4047 * expand ~user. This is slower and may fail if the shell
4048 * does not support ~user (old versions of /bin/sh).
4049 */
4050# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4051 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004052 /* Note: memory allocated by getpwnam() is never freed.
4053 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004054 struct passwd *pw = (*dst == NUL)
4055 ? NULL : getpwnam((char *)dst + 1);
4056
4057 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059 if (var == NULL)
4060# endif
4061 {
4062 expand_T xpc;
4063
4064 ExpandInit(&xpc);
4065 xpc.xp_context = EXPAND_FILES;
4066 var = ExpandOne(&xpc, dst, NULL,
4067 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 mustfree = TRUE;
4069 }
4070
4071# else /* !UNIX, thus VMS */
4072 /*
4073 * USER_HOME is a comma-separated list of
4074 * directories to search for the user account in.
4075 */
4076 {
4077 char_u test[MAXPATHL], paths[MAXPATHL];
4078 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004079 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 STRCPY(paths, USER_HOME);
4082 next_path = paths;
4083 while (*next_path)
4084 {
4085 for (path = next_path; *next_path && *next_path != ',';
4086 next_path++);
4087 if (*next_path)
4088 *next_path++ = NUL;
4089 STRCPY(test, path);
4090 STRCAT(test, "/");
4091 STRCAT(test, dst + 1);
4092 if (mch_stat(test, &st) == 0)
4093 {
4094 var = alloc(STRLEN(test) + 1);
4095 STRCPY(var, test);
4096 mustfree = TRUE;
4097 break;
4098 }
4099 }
4100 }
4101# endif /* UNIX */
4102#else
4103 /* cannot expand user's home directory, so don't try */
4104 var = NULL;
4105 tail = (char_u *)""; /* for gcc */
4106#endif /* UNIX || VMS */
4107 }
4108
4109#ifdef BACKSLASH_IN_FILENAME
4110 /* If 'shellslash' is set change backslashes to forward slashes.
4111 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4112 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4113 {
4114 char_u *p = vim_strsave(var);
4115
4116 if (p != NULL)
4117 {
4118 if (mustfree)
4119 vim_free(var);
4120 var = p;
4121 mustfree = TRUE;
4122 forward_slash(var);
4123 }
4124 }
4125#endif
4126
4127 /* If "var" contains white space, escape it with a backslash.
4128 * Required for ":e ~/tt" when $HOME includes a space. */
4129 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4130 {
4131 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4132
4133 if (p != NULL)
4134 {
4135 if (mustfree)
4136 vim_free(var);
4137 var = p;
4138 mustfree = TRUE;
4139 }
4140 }
4141
4142 if (var != NULL && *var != NUL
4143 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4144 {
4145 STRCPY(dst, var);
4146 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004147 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 /* if var[] ends in a path separator and tail[] starts
4149 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004150 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4152 && dst[-1] != ':'
4153#endif
4154 && vim_ispathsep(*tail))
4155 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004156 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 src = tail;
4158 copy_char = FALSE;
4159 }
4160 if (mustfree)
4161 vim_free(var);
4162 }
4163
4164 if (copy_char) /* copy at least one char */
4165 {
4166 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004167 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004168 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4169 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 */
4171 at_start = FALSE;
4172 if (src[0] == '\\' && src[1] != NUL)
4173 {
4174 *dst++ = *src++;
4175 --dstlen;
4176 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004177 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004179 if (dstlen > 0)
4180 {
4181 *dst++ = *src++;
4182 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004183
Bram Moolenaar1c864092017-08-06 18:15:45 +02004184 if (startstr != NULL && src - startstr_len >= srcp
4185 && STRNCMP(src - startstr_len, startstr,
4186 startstr_len) == 0)
4187 at_start = TRUE;
4188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 *dst = NUL;
4193}
4194
4195/*
4196 * Vim's version of getenv().
4197 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004198 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004199 * "mustfree" is set to TRUE when returned is allocated, it must be
4200 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 */
4202 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004203vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 char_u *p;
4206 char_u *pend;
4207 int vimruntime;
4208
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004209#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 /* use "C:/" when $HOME is not set */
4211 if (STRCMP(name, "HOME") == 0)
4212 return homedir;
4213#endif
4214
4215 p = mch_getenv(name);
4216 if (p != NULL && *p == NUL) /* empty is the same as not set */
4217 p = NULL;
4218
4219 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004220 {
4221#if defined(FEAT_MBYTE) && defined(WIN3264)
4222 if (enc_utf8)
4223 {
4224 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004225 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226
4227 /* Convert from active codepage to UTF-8. Other conversions are
4228 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004229 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004230 if (pp != NULL)
4231 {
4232 p = pp;
4233 *mustfree = TRUE;
4234 }
4235 }
4236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4241 if (!vimruntime && STRCMP(name, "VIM") != 0)
4242 return NULL;
4243
4244 /*
4245 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4246 * Don't do this when default_vimruntime_dir is non-empty.
4247 */
4248 if (vimruntime
4249#ifdef HAVE_PATHDEF
4250 && *default_vimruntime_dir == NUL
4251#endif
4252 )
4253 {
4254 p = mch_getenv((char_u *)"VIM");
4255 if (p != NULL && *p == NUL) /* empty is the same as not set */
4256 p = NULL;
4257 if (p != NULL)
4258 {
4259 p = vim_version_dir(p);
4260 if (p != NULL)
4261 *mustfree = TRUE;
4262 else
4263 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004264
4265#if defined(FEAT_MBYTE) && defined(WIN3264)
4266 if (enc_utf8)
4267 {
4268 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004269 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004270
4271 /* Convert from active codepage to UTF-8. Other conversions
4272 * are not done, because they would fail for non-ASCII
4273 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004274 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004275 if (pp != NULL)
4276 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004277 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004278 vim_free(p);
4279 p = pp;
4280 *mustfree = TRUE;
4281 }
4282 }
4283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 }
4286
4287 /*
4288 * When expanding $VIM or $VIMRUNTIME fails, try using:
4289 * - the directory name from 'helpfile' (unless it contains '$')
4290 * - the executable name from argv[0]
4291 */
4292 if (p == NULL)
4293 {
4294 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4295 p = p_hf;
4296#ifdef USE_EXE_NAME
4297 /*
4298 * Use the name of the executable, obtained from argv[0].
4299 */
4300 else
4301 p = exe_name;
4302#endif
4303 if (p != NULL)
4304 {
4305 /* remove the file name */
4306 pend = gettail(p);
4307
4308 /* remove "doc/" from 'helpfile', if present */
4309 if (p == p_hf)
4310 pend = remove_tail(p, pend, (char_u *)"doc");
4311
4312#ifdef USE_EXE_NAME
4313# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004314 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 if (p == exe_name)
4316 {
4317 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004318 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004320 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4321 if (pend1 != pend)
4322 {
4323 pnew = alloc((unsigned)(pend1 - p) + 15);
4324 if (pnew != NULL)
4325 {
4326 STRNCPY(pnew, p, (pend1 - p));
4327 STRCPY(pnew + (pend1 - p), "Resources/vim");
4328 p = pnew;
4329 pend = p + STRLEN(p);
4330 }
4331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333# endif
4334 /* remove "src/" from exe_name, if present */
4335 if (p == exe_name)
4336 pend = remove_tail(p, pend, (char_u *)"src");
4337#endif
4338
4339 /* for $VIM, remove "runtime/" or "vim54/", if present */
4340 if (!vimruntime)
4341 {
4342 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4343 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4344 }
4345
4346 /* remove trailing path separator */
4347#ifndef MACOS_CLASSIC
4348 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004349 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004350 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 --pend;
4352#endif
4353
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004354#ifdef MACOS_X
4355 if (p == exe_name || p == p_hf)
4356#endif
4357 /* check that the result is a directory name */
4358 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 if (p != NULL && !mch_isdir(p))
4361 {
4362 vim_free(p);
4363 p = NULL;
4364 }
4365 else
4366 {
4367#ifdef USE_EXE_NAME
4368 /* may add "/vim54" or "/runtime" if it exists */
4369 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4370 {
4371 vim_free(p);
4372 p = pend;
4373 }
4374#endif
4375 *mustfree = TRUE;
4376 }
4377 }
4378 }
4379
4380#ifdef HAVE_PATHDEF
4381 /* When there is a pathdef.c file we can use default_vim_dir and
4382 * default_vimruntime_dir */
4383 if (p == NULL)
4384 {
4385 /* Only use default_vimruntime_dir when it is not empty */
4386 if (vimruntime && *default_vimruntime_dir != NUL)
4387 {
4388 p = default_vimruntime_dir;
4389 *mustfree = FALSE;
4390 }
4391 else if (*default_vim_dir != NUL)
4392 {
4393 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4394 *mustfree = TRUE;
4395 else
4396 {
4397 p = default_vim_dir;
4398 *mustfree = FALSE;
4399 }
4400 }
4401 }
4402#endif
4403
4404 /*
4405 * Set the environment variable, so that the new value can be found fast
4406 * next time, and others can also use it (e.g. Perl).
4407 */
4408 if (p != NULL)
4409 {
4410 if (vimruntime)
4411 {
4412 vim_setenv((char_u *)"VIMRUNTIME", p);
4413 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 else
4416 {
4417 vim_setenv((char_u *)"VIM", p);
4418 didset_vim = TRUE;
4419 }
4420 }
4421 return p;
4422}
4423
4424/*
4425 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4426 * Return NULL if not, return its name in allocated memory otherwise.
4427 */
4428 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004429vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430{
4431 char_u *p;
4432
4433 if (vimdir == NULL || *vimdir == NUL)
4434 return NULL;
4435 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4436 if (p != NULL && mch_isdir(p))
4437 return p;
4438 vim_free(p);
4439 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4440 if (p != NULL && mch_isdir(p))
4441 return p;
4442 vim_free(p);
4443 return NULL;
4444}
4445
4446/*
4447 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4448 * the length of "name/". Otherwise return "pend".
4449 */
4450 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004451remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452{
4453 int len = (int)STRLEN(name) + 1;
4454 char_u *newend = pend - len;
4455
4456 if (newend >= p
4457 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004458 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 return newend;
4460 return pend;
4461}
4462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 * Our portable version of setenv.
4465 */
4466 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004467vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469#ifdef HAVE_SETENV
4470 mch_setenv((char *)name, (char *)val, 1);
4471#else
4472 char_u *envbuf;
4473
4474 /*
4475 * Putenv does not copy the string, it has to remain
4476 * valid. The allocated memory will never be freed.
4477 */
4478 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4479 if (envbuf != NULL)
4480 {
4481 sprintf((char *)envbuf, "%s=%s", name, val);
4482 putenv((char *)envbuf);
4483 }
4484#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004485#ifdef FEAT_GETTEXT
4486 /*
4487 * When setting $VIMRUNTIME adjust the directory to find message
4488 * translations to $VIMRUNTIME/lang.
4489 */
4490 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4491 {
4492 char_u *buf = concat_str(val, (char_u *)"/lang");
4493
4494 if (buf != NULL)
4495 {
4496 bindtextdomain(VIMPACKAGE, (char *)buf);
4497 vim_free(buf);
4498 }
4499 }
4500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501}
4502
4503#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4504/*
4505 * Function given to ExpandGeneric() to obtain an environment variable name.
4506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004508get_env_name(
4509 expand_T *xp UNUSED,
4510 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
4512# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4513 /*
4514 * No environ[] on the Amiga and on the Mac (using MPW).
4515 */
4516 return NULL;
4517# else
4518# ifndef __WIN32__
4519 /* Borland C++ 5.2 has this in a header file. */
4520 extern char **environ;
4521# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004522# define ENVNAMELEN 100
4523 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 char_u *str;
4525 int n;
4526
4527 str = (char_u *)environ[idx];
4528 if (str == NULL)
4529 return NULL;
4530
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004531 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
4533 if (str[n] == '=' || str[n] == NUL)
4534 break;
4535 name[n] = str[n];
4536 }
4537 name[n] = NUL;
4538 return name;
4539# endif
4540}
Bram Moolenaar24305862012-08-15 14:05:05 +02004541
4542/*
4543 * Find all user names for user completion.
4544 * Done only once and then cached.
4545 */
4546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004547init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004548{
Bram Moolenaar24305862012-08-15 14:05:05 +02004549 static int lazy_init_done = FALSE;
4550
4551 if (lazy_init_done)
4552 return;
4553
4554 lazy_init_done = TRUE;
4555 ga_init2(&ga_users, sizeof(char_u *), 20);
4556
4557# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4558 {
4559 char_u* user;
4560 struct passwd* pw;
4561
4562 setpwent();
4563 while ((pw = getpwent()) != NULL)
4564 /* pw->pw_name shouldn't be NULL but just in case... */
4565 if (pw->pw_name != NULL)
4566 {
4567 if (ga_grow(&ga_users, 1) == FAIL)
4568 break;
4569 user = vim_strsave((char_u*)pw->pw_name);
4570 if (user == NULL)
4571 break;
4572 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4573 }
4574 endpwent();
4575 }
4576# endif
4577}
4578
4579/*
4580 * Function given to ExpandGeneric() to obtain an user names.
4581 */
4582 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004583get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004584{
4585 init_users();
4586 if (idx < ga_users.ga_len)
4587 return ((char_u **)ga_users.ga_data)[idx];
4588 return NULL;
4589}
4590
4591/*
4592 * Check whether name matches a user name. Return:
4593 * 0 if name does not match any user name.
4594 * 1 if name partially matches the beginning of a user name.
4595 * 2 is name fully matches a user name.
4596 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004597int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004598{
4599 int i;
4600 int n = (int)STRLEN(name);
4601 int result = 0;
4602
4603 init_users();
4604 for (i = 0; i < ga_users.ga_len; i++)
4605 {
4606 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4607 return 2; /* full match */
4608 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4609 result = 1; /* partial match */
4610 }
4611 return result;
4612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#endif
4614
4615/*
4616 * Replace home directory by "~" in each space or comma separated file name in
4617 * 'src'.
4618 * If anything fails (except when out of space) dst equals src.
4619 */
4620 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004621home_replace(
4622 buf_T *buf, /* when not NULL, check for help files */
4623 char_u *src, /* input file name */
4624 char_u *dst, /* where to put the result */
4625 int dstlen, /* maximum length of the result */
4626 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 spaces and commas in the file name. */
4628{
4629 size_t dirlen = 0, envlen = 0;
4630 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004631 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 char_u *p;
4633
4634 if (src == NULL)
4635 {
4636 *dst = NUL;
4637 return;
4638 }
4639
4640 /*
4641 * If the file is a help file, remove the path completely.
4642 */
4643 if (buf != NULL && buf->b_help)
4644 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004645 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 return;
4647 }
4648
4649 /*
4650 * We check both the value of the $HOME environment variable and the
4651 * "real" home directory.
4652 */
4653 if (homedir != NULL)
4654 dirlen = STRLEN(homedir);
4655
4656#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004657 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004659 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4660#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004661#ifdef WIN3264
4662 if (homedir_env == NULL)
4663 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4664#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004665 /* Empty is the same as not set. */
4666 if (homedir_env != NULL && *homedir_env == NUL)
4667 homedir_env = NULL;
4668
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004669#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004670 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004671 {
4672 int usedlen = 0;
4673 int flen;
4674 char_u *fbuf = NULL;
4675
4676 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004677 (void)modify_fname((char_u *)":p", &usedlen,
4678 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004679 flen = (int)STRLEN(homedir_env);
4680 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4681 /* Remove the trailing / that is added to a directory. */
4682 homedir_env[flen - 1] = NUL;
4683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#endif
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (homedir_env != NULL)
4687 envlen = STRLEN(homedir_env);
4688
4689 if (!one)
4690 src = skipwhite(src);
4691 while (*src && dstlen > 0)
4692 {
4693 /*
4694 * Here we are at the beginning of a file name.
4695 * First, check to see if the beginning of the file name matches
4696 * $HOME or the "real" home directory. Check that there is a '/'
4697 * after the match (so that if e.g. the file is "/home/pieter/bla",
4698 * and the home directory is "/home/piet", the file does not end up
4699 * as "~er/bla" (which would seem to indicate the file "bla" in user
4700 * er's home directory)).
4701 */
4702 p = homedir;
4703 len = dirlen;
4704 for (;;)
4705 {
4706 if ( len
4707 && fnamencmp(src, p, len) == 0
4708 && (vim_ispathsep(src[len])
4709 || (!one && (src[len] == ',' || src[len] == ' '))
4710 || src[len] == NUL))
4711 {
4712 src += len;
4713 if (--dstlen > 0)
4714 *dst++ = '~';
4715
4716 /*
4717 * If it's just the home directory, add "/".
4718 */
4719 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4720 *dst++ = '/';
4721 break;
4722 }
4723 if (p == homedir_env)
4724 break;
4725 p = homedir_env;
4726 len = envlen;
4727 }
4728
4729 /* if (!one) skip to separator: space or comma */
4730 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4731 *dst++ = *src++;
4732 /* skip separator */
4733 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4734 *dst++ = *src++;
4735 }
4736 /* if (dstlen == 0) out of space, what to do??? */
4737
4738 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004739
4740 if (homedir_env != homedir_env_orig)
4741 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
4743
4744/*
4745 * Like home_replace, store the replaced string in allocated memory.
4746 * When something fails, NULL is returned.
4747 */
4748 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004749home_replace_save(
4750 buf_T *buf, /* when not NULL, check for help files */
4751 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 char_u *dst;
4754 unsigned len;
4755
4756 len = 3; /* space for "~/" and trailing NUL */
4757 if (src != NULL) /* just in case */
4758 len += (unsigned)STRLEN(src);
4759 dst = alloc(len);
4760 if (dst != NULL)
4761 home_replace(buf, src, dst, len, TRUE);
4762 return dst;
4763}
4764
4765/*
4766 * Compare two file names and return:
4767 * FPC_SAME if they both exist and are the same file.
4768 * FPC_SAMEX if they both don't exist and have the same file name.
4769 * FPC_DIFF if they both exist and are different files.
4770 * FPC_NOTX if they both don't exist.
4771 * FPC_DIFFX if one of them doesn't exist.
4772 * For the first name environment variables are expanded
4773 */
4774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004775fullpathcmp(
4776 char_u *s1,
4777 char_u *s2,
4778 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780#ifdef UNIX
4781 char_u exp1[MAXPATHL];
4782 char_u full1[MAXPATHL];
4783 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004784 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 int r1, r2;
4786
4787 expand_env(s1, exp1, MAXPATHL);
4788 r1 = mch_stat((char *)exp1, &st1);
4789 r2 = mch_stat((char *)s2, &st2);
4790 if (r1 != 0 && r2 != 0)
4791 {
4792 /* if mch_stat() doesn't work, may compare the names */
4793 if (checkname)
4794 {
4795 if (fnamecmp(exp1, s2) == 0)
4796 return FPC_SAMEX;
4797 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4798 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4799 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4800 return FPC_SAMEX;
4801 }
4802 return FPC_NOTX;
4803 }
4804 if (r1 != 0 || r2 != 0)
4805 return FPC_DIFFX;
4806 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4807 return FPC_SAME;
4808 return FPC_DIFF;
4809#else
4810 char_u *exp1; /* expanded s1 */
4811 char_u *full1; /* full path of s1 */
4812 char_u *full2; /* full path of s2 */
4813 int retval = FPC_DIFF;
4814 int r1, r2;
4815
4816 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4817 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4818 {
4819 full1 = exp1 + MAXPATHL;
4820 full2 = full1 + MAXPATHL;
4821
4822 expand_env(s1, exp1, MAXPATHL);
4823 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4824 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4825
4826 /* If vim_FullName() fails, the file probably doesn't exist. */
4827 if (r1 != OK && r2 != OK)
4828 {
4829 if (checkname && fnamecmp(exp1, s2) == 0)
4830 retval = FPC_SAMEX;
4831 else
4832 retval = FPC_NOTX;
4833 }
4834 else if (r1 != OK || r2 != OK)
4835 retval = FPC_DIFFX;
4836 else if (fnamecmp(full1, full2))
4837 retval = FPC_DIFF;
4838 else
4839 retval = FPC_SAME;
4840 vim_free(exp1);
4841 }
4842 return retval;
4843#endif
4844}
4845
4846/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004847 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004848 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004849 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 */
4851 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004852gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 char_u *p1, *p2;
4855
4856 if (fname == NULL)
4857 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004858 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004860 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004862 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 return p1;
4865}
4866
Bram Moolenaar31710262010-08-13 13:36:15 +02004867#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004868static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004869
4870/*
4871 * Return the end of the directory name, on the first path
4872 * separator:
4873 * "/path/file", "/path/dir/", "/path//dir", "/file"
4874 * ^ ^ ^ ^
4875 */
4876 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004877gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004878{
4879 char_u *dir_end = fname;
4880 char_u *next_dir_end = fname;
4881 int look_for_sep = TRUE;
4882 char_u *p;
4883
4884 for (p = fname; *p != NUL; )
4885 {
4886 if (vim_ispathsep(*p))
4887 {
4888 if (look_for_sep)
4889 {
4890 next_dir_end = p;
4891 look_for_sep = FALSE;
4892 }
4893 }
4894 else
4895 {
4896 if (!look_for_sep)
4897 dir_end = next_dir_end;
4898 look_for_sep = TRUE;
4899 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004900 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004901 }
4902 return dir_end;
4903}
4904#endif
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004907 * Get pointer to tail of "fname", including path separators. Putting a NUL
4908 * here leaves the directory name. Takes care of "c:/" and "//".
4909 * Always returns a valid pointer.
4910 */
4911 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004912gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004913{
4914 char_u *p;
4915 char_u *t;
4916
4917 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4918 t = gettail(fname);
4919 while (t > p && after_pathsep(fname, t))
4920 --t;
4921#ifdef VMS
4922 /* path separator is part of the path */
4923 ++t;
4924#endif
4925 return t;
4926}
4927
4928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 * get the next path component (just after the next path separator).
4930 */
4931 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004932getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004935 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (*fname)
4937 ++fname;
4938 return fname;
4939}
4940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941/*
4942 * Get a pointer to one character past the head of a path name.
4943 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4944 * If there is no head, path is returned.
4945 */
4946 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004947get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
4949 char_u *retval;
4950
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004951#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /* may skip "c:" */
4953 if (isalpha(path[0]) && path[1] == ':')
4954 retval = path + 2;
4955 else
4956 retval = path;
4957#else
4958# if defined(AMIGA)
4959 /* may skip "label:" */
4960 retval = vim_strchr(path, ':');
4961 if (retval == NULL)
4962 retval = path;
4963# else /* Unix */
4964 retval = path;
4965# endif
4966#endif
4967
4968 while (vim_ispathsep(*retval))
4969 ++retval;
4970
4971 return retval;
4972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004975 * Return TRUE if 'c' is a path separator.
4976 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 */
4978 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004979vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004981#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004983#else
4984# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004986# else
4987# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4989 return (c == ':' || c == '[' || c == ']' || c == '/'
4990 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004991# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004993# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996}
4997
Bram Moolenaar69c35002013-11-04 02:54:12 +01004998/*
4999 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5000 */
5001 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005002vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005003{
5004 return vim_ispathsep(c)
5005#ifdef BACKSLASH_IN_FILENAME
5006 && c != ':'
5007#endif
5008 ;
5009}
5010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5012/*
5013 * return TRUE if 'c' is a path list separator.
5014 */
5015 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005016vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018#ifdef UNIX
5019 return (c == ':');
5020#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005021 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022#endif
5023}
5024#endif
5025
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5027 || defined(FEAT_EVAL) || defined(PROTO)
5028/*
5029 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5030 * It's done in-place.
5031 */
5032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005033shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005034{
5035 char_u *tail, *s, *d;
5036 int skip = FALSE;
5037
5038 tail = gettail(str);
5039 d = str;
5040 for (s = str; ; ++s)
5041 {
5042 if (s >= tail) /* copy the whole tail */
5043 {
5044 *d++ = *s;
5045 if (*s == NUL)
5046 break;
5047 }
5048 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5049 {
5050 *d++ = *s;
5051 skip = FALSE;
5052 }
5053 else if (!skip)
5054 {
5055 *d++ = *s; /* copy next char */
5056 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5057 skip = TRUE;
5058# ifdef FEAT_MBYTE
5059 if (has_mbyte)
5060 {
5061 int l = mb_ptr2len(s);
5062
5063 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005064 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005065 }
5066# endif
5067 }
5068 }
5069}
5070#endif
5071
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005072/*
5073 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5074 * Also returns TRUE if there is no directory name.
5075 * "fname" must be writable!.
5076 */
5077 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005078dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005079{
5080 char_u *p;
5081 int c;
5082 int retval;
5083
5084 p = gettail_sep(fname);
5085 if (p == fname)
5086 return TRUE;
5087 c = *p;
5088 *p = NUL;
5089 retval = mch_isdir(fname);
5090 *p = c;
5091 return retval;
5092}
5093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005095 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5096 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 */
5098 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005099vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005101#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005103#else
5104 if (p_fic)
5105 return MB_STRICMP(x, y);
5106 return STRCMP(x, y);
5107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108}
5109
5110 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005111vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005113#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005114 char_u *px = x;
5115 char_u *py = y;
5116 int cx = NUL;
5117 int cy = NUL;
5118
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005119 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005121 cx = PTR2CHAR(px);
5122 cy = PTR2CHAR(py);
5123 if (cx == NUL || cy == NUL
5124 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5125 && !(cx == '/' && cy == '\\')
5126 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005128 len -= MB_PTR2LEN(px);
5129 px += MB_PTR2LEN(px);
5130 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132 if (len == 0)
5133 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005134 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005135#else
5136 if (p_fic)
5137 return MB_STRNICMP(x, y, len);
5138 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142/*
5143 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005144 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
5146 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005147concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148{
5149 char_u *dest;
5150
5151 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5152 if (dest != NULL)
5153 {
5154 STRCPY(dest, fname1);
5155 if (sep)
5156 add_pathsep(dest);
5157 STRCAT(dest, fname2);
5158 }
5159 return dest;
5160}
5161
Bram Moolenaard6754642005-01-17 22:18:45 +00005162/*
5163 * Concatenate two strings and return the result in allocated memory.
5164 * Returns NULL when out of memory.
5165 */
5166 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005167concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005168{
5169 char_u *dest;
5170 size_t l = STRLEN(str1);
5171
5172 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5173 if (dest != NULL)
5174 {
5175 STRCPY(dest, str1);
5176 STRCPY(dest + l, str2);
5177 }
5178 return dest;
5179}
Bram Moolenaard6754642005-01-17 22:18:45 +00005180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181/*
5182 * Add a path separator to a file name, unless it already ends in a path
5183 * separator.
5184 */
5185 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005186add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005188 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 STRCAT(p, PATHSEPSTR);
5190}
5191
5192/*
5193 * FullName_save - Make an allocated copy of a full file name.
5194 * Returns NULL when out of memory.
5195 */
5196 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005197FullName_save(
5198 char_u *fname,
5199 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005200 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
5202 char_u *buf;
5203 char_u *new_fname = NULL;
5204
5205 if (fname == NULL)
5206 return NULL;
5207
5208 buf = alloc((unsigned)MAXPATHL);
5209 if (buf != NULL)
5210 {
5211 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5212 new_fname = vim_strsave(buf);
5213 else
5214 new_fname = vim_strsave(fname);
5215 vim_free(buf);
5216 }
5217 return new_fname;
5218}
5219
5220#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5221
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005222static char_u *skip_string(char_u *p);
5223static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005224static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005225static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
5227/*
5228 * Find the start of a comment, not knowing if we are in a comment right now.
5229 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005230 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005232 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005233ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005234{
5235 return find_start_comment(curbuf->b_ind_maxcomment);
5236}
5237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005239find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
5241 pos_T *pos;
5242 char_u *line;
5243 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005244 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005246 for (;;)
5247 {
5248 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5249 if (pos == NULL)
5250 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005252 /*
5253 * Check if the comment start we found is inside a string.
5254 * If it is then restrict the search to below this line and try again.
5255 */
5256 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005257 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005258 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005259 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005260 break;
5261 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5262 if (cur_maxcomment <= 0)
5263 {
5264 pos = NULL;
5265 break;
5266 }
5267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 return pos;
5269}
5270
5271/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005272 * Find the start of a comment or raw string, not knowing if we are in a
5273 * comment or raw string right now.
5274 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005275 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005276 * Return NULL when not inside a comment or raw string.
5277 * "CORS" -> Comment Or Raw String
5278 */
5279 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005280ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005281{
Bram Moolenaar089af182015-10-07 11:41:49 +02005282 static pos_T comment_pos_copy;
5283 pos_T *comment_pos;
5284 pos_T *rs_pos;
5285
5286 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5287 if (comment_pos != NULL)
5288 {
5289 /* Need to make a copy of the static pos in findmatchlimit(),
5290 * calling find_start_rawstring() may change it. */
5291 comment_pos_copy = *comment_pos;
5292 comment_pos = &comment_pos_copy;
5293 }
5294 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005295
5296 /* If comment_pos is before rs_pos the raw string is inside the comment.
5297 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005298 if (comment_pos == NULL || (rs_pos != NULL
5299 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005300 {
5301 if (is_raw != NULL && rs_pos != NULL)
5302 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005303 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005304 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005305 return comment_pos;
5306}
5307
5308/*
5309 * Find the start of a raw string, not knowing if we are in one right now.
5310 * Search starts at w_cursor.lnum and goes backwards.
5311 * Return NULL when not inside a raw string.
5312 */
5313 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005314find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005315{
5316 pos_T *pos;
5317 char_u *line;
5318 char_u *p;
5319 int cur_maxcomment = ind_maxcomment;
5320
5321 for (;;)
5322 {
5323 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5324 if (pos == NULL)
5325 break;
5326
5327 /*
5328 * Check if the raw string start we found is inside a string.
5329 * If it is then restrict the search to below this line and try again.
5330 */
5331 line = ml_get(pos->lnum);
5332 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5333 p = skip_string(p);
5334 if ((colnr_T)(p - line) <= pos->col)
5335 break;
5336 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5337 if (cur_maxcomment <= 0)
5338 {
5339 pos = NULL;
5340 break;
5341 }
5342 }
5343 return pos;
5344}
5345
5346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 * Skip to the end of a "string" and a 'c' character.
5348 * If there is no string or character, return argument unmodified.
5349 */
5350 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
5353 int i;
5354
5355 /*
5356 * We loop, because strings may be concatenated: "date""time".
5357 */
5358 for ( ; ; ++p)
5359 {
5360 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5361 {
5362 if (!p[1]) /* ' at end of line */
5363 break;
5364 i = 2;
5365 if (p[1] == '\\') /* '\n' or '\000' */
5366 {
5367 ++i;
5368 while (vim_isdigit(p[i - 1])) /* '\000' */
5369 ++i;
5370 }
5371 if (p[i] == '\'') /* check for trailing ' */
5372 {
5373 p += i;
5374 continue;
5375 }
5376 }
5377 else if (p[0] == '"') /* start of string */
5378 {
5379 for (++p; p[0]; ++p)
5380 {
5381 if (p[0] == '\\' && p[1] != NUL)
5382 ++p;
5383 else if (p[0] == '"') /* end of string */
5384 break;
5385 }
5386 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005387 continue; /* continue for another string */
5388 }
5389 else if (p[0] == 'R' && p[1] == '"')
5390 {
5391 /* Raw string: R"[delim](...)[delim]" */
5392 char_u *delim = p + 2;
5393 char_u *paren = vim_strchr(delim, '(');
5394
5395 if (paren != NULL)
5396 {
5397 size_t delim_len = paren - delim;
5398
5399 for (p += 3; *p; ++p)
5400 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5401 && p[delim_len + 1] == '"')
5402 {
5403 p += delim_len + 1;
5404 break;
5405 }
5406 if (p[0] == '"')
5407 continue; /* continue for another string */
5408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410 break; /* no string found */
5411 }
5412 if (!*p)
5413 --p; /* backup from NUL */
5414 return p;
5415}
5416#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5417
5418#if defined(FEAT_CINDENT) || defined(PROTO)
5419
5420/*
5421 * Do C or expression indenting on the current line.
5422 */
5423 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005424do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426# ifdef FEAT_EVAL
5427 if (*curbuf->b_p_inde != NUL)
5428 fixthisline(get_expr_indent);
5429 else
5430# endif
5431 fixthisline(get_c_indent);
5432}
5433
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005434/* Find result cache for cpp_baseclass */
5435typedef struct {
5436 int found;
5437 lpos_T lpos;
5438} cpp_baseclass_cache_T;
5439
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440/*
5441 * Functions for C-indenting.
5442 * Most of this originally comes from Eric Fischer.
5443 */
5444/*
5445 * Below "XXX" means that this function may unlock the current line.
5446 */
5447
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005448static char_u *cin_skipcomment(char_u *);
5449static int cin_nocode(char_u *);
5450static pos_T *find_line_comment(void);
5451static int cin_has_js_key(char_u *text);
5452static int cin_islabel_skip(char_u **);
5453static int cin_isdefault(char_u *);
5454static char_u *after_label(char_u *l);
5455static int get_indent_nolabel(linenr_T lnum);
5456static int skip_label(linenr_T, char_u **pp);
5457static int cin_first_id_amount(void);
5458static int cin_get_equal_amount(linenr_T lnum);
5459static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005460static int cin_iscomment(char_u *);
5461static int cin_islinecomment(char_u *);
5462static int cin_isterminated(char_u *, int, int);
5463static int cin_isinit(void);
5464static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5465static int cin_isif(char_u *);
5466static int cin_iselse(char_u *);
5467static int cin_isdo(char_u *);
5468static int cin_iswhileofdo(char_u *, linenr_T);
5469static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5470static int cin_iswhileofdo_end(int terminated);
5471static int cin_isbreak(char_u *);
5472static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5473static int get_baseclass_amount(int col);
5474static int cin_ends_in(char_u *, char_u *, char_u *);
5475static int cin_starts_with(char_u *s, char *word);
5476static int cin_skip2pos(pos_T *trypos);
5477static pos_T *find_start_brace(void);
5478static pos_T *find_match_paren(int);
5479static pos_T *find_match_char(int c, int ind_maxparen);
5480static int corr_ind_maxparen(pos_T *startpos);
5481static int find_last_paren(char_u *l, int start, int end);
5482static int find_match(int lookfor, linenr_T ourscope);
5483static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
5485/*
5486 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005487 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 */
5489 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005490cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 while (*s)
5493 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005494 char_u *prev_s = s;
5495
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005497
5498 /* Perl/shell # comment comment continues until eol. Require a space
5499 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005500 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005501 {
5502 s += STRLEN(s);
5503 break;
5504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (*s != '/')
5506 break;
5507 ++s;
5508 if (*s == '/') /* slash-slash comment continues till eol */
5509 {
5510 s += STRLEN(s);
5511 break;
5512 }
5513 if (*s != '*')
5514 break;
5515 for (++s; *s; ++s) /* skip slash-star comment */
5516 if (s[0] == '*' && s[1] == '/')
5517 {
5518 s += 2;
5519 break;
5520 }
5521 }
5522 return s;
5523}
5524
5525/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005526 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * not considered code.
5528 */
5529 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005530cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 return *cin_skipcomment(s) == NUL;
5533}
5534
5535/*
5536 * Check previous lines for a "//" line comment, skipping over blank lines.
5537 */
5538 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 static pos_T pos;
5542 char_u *line;
5543 char_u *p;
5544
5545 pos = curwin->w_cursor;
5546 while (--pos.lnum > 0)
5547 {
5548 line = ml_get(pos.lnum);
5549 p = skipwhite(line);
5550 if (cin_islinecomment(p))
5551 {
5552 pos.col = (int)(p - line);
5553 return &pos;
5554 }
5555 if (*p != NUL)
5556 break;
5557 }
5558 return NULL;
5559}
5560
5561/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005562 * Return TRUE if "text" starts with "key:".
5563 */
5564 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005565cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005566{
5567 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005568 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005569
5570 if (*s == '\'' || *s == '"')
5571 {
5572 /* can be 'key': or "key": */
5573 quote = *s;
5574 ++s;
5575 }
5576 if (!vim_isIDc(*s)) /* need at least one ID character */
5577 return FALSE;
5578
5579 while (vim_isIDc(*s))
5580 ++s;
5581 if (*s == quote)
5582 ++s;
5583
5584 s = cin_skipcomment(s);
5585
5586 /* "::" is not a label, it's C++ */
5587 return (*s == ':' && s[1] != ':');
5588}
5589
5590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005592 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 */
5594 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005595cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597 if (!vim_isIDc(**s)) /* need at least one ID character */
5598 return FALSE;
5599
5600 while (vim_isIDc(**s))
5601 (*s)++;
5602
5603 *s = cin_skipcomment(*s);
5604
5605 /* "::" is not a label, it's C++ */
5606 return (**s == ':' && *++*s != ':');
5607}
5608
5609/*
5610 * Recognize a label: "label:".
5611 * Note: curwin->w_cursor must be where we are looking for the label.
5612 */
5613 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005614cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 char_u *s;
5617
5618 s = cin_skipcomment(ml_get_curline());
5619
5620 /*
5621 * Exclude "default" from labels, since it should be indented
5622 * like a switch label. Same for C++ scope declarations.
5623 */
5624 if (cin_isdefault(s))
5625 return FALSE;
5626 if (cin_isscopedecl(s))
5627 return FALSE;
5628
5629 if (cin_islabel_skip(&s))
5630 {
5631 /*
5632 * Only accept a label if the previous line is terminated or is a case
5633 * label.
5634 */
5635 pos_T cursor_save;
5636 pos_T *trypos;
5637 char_u *line;
5638
5639 cursor_save = curwin->w_cursor;
5640 while (curwin->w_cursor.lnum > 1)
5641 {
5642 --curwin->w_cursor.lnum;
5643
5644 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005645 * If we're in a comment or raw string now, skip to the start of
5646 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 */
5648 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005649 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 curwin->w_cursor = *trypos;
5651
5652 line = ml_get_curline();
5653 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5654 continue;
5655 if (*(line = cin_skipcomment(line)) == NUL)
5656 continue;
5657
5658 curwin->w_cursor = cursor_save;
5659 if (cin_isterminated(line, TRUE, FALSE)
5660 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005661 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 || (cin_islabel_skip(&line) && cin_nocode(line)))
5663 return TRUE;
5664 return FALSE;
5665 }
5666 curwin->w_cursor = cursor_save;
5667 return TRUE; /* label at start of file??? */
5668 }
5669 return FALSE;
5670}
5671
5672/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005673 * Recognize structure initialization and enumerations:
5674 * "[typedef] [static|public|protected|private] enum"
5675 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 */
5677 static int
5678cin_isinit(void)
5679{
5680 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005681 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 s = cin_skipcomment(ml_get_curline());
5684
Bram Moolenaar75342212013-03-07 13:13:52 +01005685 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 s = cin_skipcomment(s + 7);
5687
Bram Moolenaar75342212013-03-07 13:13:52 +01005688 for (;;)
5689 {
5690 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005691
Bram Moolenaar75342212013-03-07 13:13:52 +01005692 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5693 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005694 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005695 if (cin_starts_with(s, skip[i]))
5696 {
5697 s = cin_skipcomment(s + l);
5698 l = 0;
5699 break;
5700 }
5701 }
5702 if (l != 0)
5703 break;
5704 }
5705
5706 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 return TRUE;
5708
5709 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5710 return TRUE;
5711
5712 return FALSE;
5713}
5714
5715/*
5716 * Recognize a switch label: "case .*:" or "default:".
5717 */
5718 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005719cin_iscase(
5720 char_u *s,
5721 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005724 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 for (s += 4; *s; ++s)
5727 {
5728 s = cin_skipcomment(s);
5729 if (*s == ':')
5730 {
5731 if (s[1] == ':') /* skip over "::" for C++ */
5732 ++s;
5733 else
5734 return TRUE;
5735 }
5736 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005737 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5739 return FALSE; /* stop at comment */
5740 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005741 {
5742 /* JS etc. */
5743 if (strict)
5744 return FALSE; /* stop at string */
5745 else
5746 return TRUE;
5747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749 return FALSE;
5750 }
5751
5752 if (cin_isdefault(s))
5753 return TRUE;
5754 return FALSE;
5755}
5756
5757/*
5758 * Recognize a "default" switch label.
5759 */
5760 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005761cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
5763 return (STRNCMP(s, "default", 7) == 0
5764 && *(s = cin_skipcomment(s + 7)) == ':'
5765 && s[1] != ':');
5766}
5767
5768/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005769 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 */
5771 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005772cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 int i;
5775
5776 s = cin_skipcomment(s);
5777 if (STRNCMP(s, "public", 6) == 0)
5778 i = 6;
5779 else if (STRNCMP(s, "protected", 9) == 0)
5780 i = 9;
5781 else if (STRNCMP(s, "private", 7) == 0)
5782 i = 7;
5783 else
5784 return FALSE;
5785 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5786}
5787
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005788/* Maximum number of lines to search back for a "namespace" line. */
5789#define FIND_NAMESPACE_LIM 20
5790
5791/*
5792 * Recognize a "namespace" scope declaration.
5793 */
5794 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005795cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005796{
5797 char_u *p;
5798 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005799 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005800
5801 s = cin_skipcomment(s);
5802 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5803 {
5804 p = cin_skipcomment(skipwhite(s + 9));
5805 while (*p != NUL)
5806 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005807 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005808 {
5809 has_name = TRUE; /* found end of a name */
5810 p = cin_skipcomment(skipwhite(p));
5811 }
5812 else if (*p == '{')
5813 {
5814 break;
5815 }
5816 else if (vim_iswordc(*p))
5817 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005818 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005819 if (has_name)
5820 return FALSE; /* word character after skipping past name */
5821 ++p;
5822 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005823 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5824 {
5825 if (!has_name_start || has_name)
5826 return FALSE;
5827 /* C++ 17 nested namespace */
5828 p += 3;
5829 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005830 else
5831 {
5832 return FALSE;
5833 }
5834 }
5835 return TRUE;
5836 }
5837 return FALSE;
5838}
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005841 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5842 */
5843 static int
5844cin_is_cpp_extern_c(char_u *s)
5845{
5846 char_u *p;
5847 int has_string_literal = FALSE;
5848
5849 s = cin_skipcomment(s);
5850 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5851 {
5852 p = cin_skipcomment(skipwhite(s + 6));
5853 while (*p != NUL)
5854 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005855 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005856 {
5857 p = cin_skipcomment(skipwhite(p));
5858 }
5859 else if (*p == '{')
5860 {
5861 break;
5862 }
5863 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5864 {
5865 if (has_string_literal)
5866 return FALSE;
5867 has_string_literal = TRUE;
5868 p += 3;
5869 }
5870 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5871 && p[4] == '"')
5872 {
5873 if (has_string_literal)
5874 return FALSE;
5875 has_string_literal = TRUE;
5876 p += 5;
5877 }
5878 else
5879 {
5880 return FALSE;
5881 }
5882 }
5883 return has_string_literal ? TRUE : FALSE;
5884 }
5885 return FALSE;
5886}
5887
5888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 * Return a pointer to the first non-empty non-comment character after a ':'.
5890 * Return NULL if not found.
5891 * case 234: a = b;
5892 * ^
5893 */
5894 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005895after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 for ( ; *l; ++l)
5898 {
5899 if (*l == ':')
5900 {
5901 if (l[1] == ':') /* skip over "::" for C++ */
5902 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005903 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 break;
5905 }
5906 else if (*l == '\'' && l[1] && l[2] == '\'')
5907 l += 2; /* skip over 'x' */
5908 }
5909 if (*l == NUL)
5910 return NULL;
5911 l = cin_skipcomment(l + 1);
5912 if (*l == NUL)
5913 return NULL;
5914 return l;
5915}
5916
5917/*
5918 * Get indent of line "lnum", skipping a label.
5919 * Return 0 if there is nothing after the label.
5920 */
5921 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005922get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 char_u *l;
5925 pos_T fp;
5926 colnr_T col;
5927 char_u *p;
5928
5929 l = ml_get(lnum);
5930 p = after_label(l);
5931 if (p == NULL)
5932 return 0;
5933
5934 fp.col = (colnr_T)(p - l);
5935 fp.lnum = lnum;
5936 getvcol(curwin, &fp, &col, NULL, NULL);
5937 return (int)col;
5938}
5939
5940/*
5941 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005942 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 * label: if (asdf && asdfasdf)
5944 * ^
5945 */
5946 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005947skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 char_u *l;
5950 int amount;
5951 pos_T cursor_save;
5952
5953 cursor_save = curwin->w_cursor;
5954 curwin->w_cursor.lnum = lnum;
5955 l = ml_get_curline();
5956 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005957 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {
5959 amount = get_indent_nolabel(lnum);
5960 l = after_label(ml_get_curline());
5961 if (l == NULL) /* just in case */
5962 l = ml_get_curline();
5963 }
5964 else
5965 {
5966 amount = get_indent();
5967 l = ml_get_curline();
5968 }
5969 *pp = l;
5970
5971 curwin->w_cursor = cursor_save;
5972 return amount;
5973}
5974
5975/*
5976 * Return the indent of the first variable name after a type in a declaration.
5977 * int a, indent of "a"
5978 * static struct foo b, indent of "b"
5979 * enum bla c, indent of "c"
5980 * Returns zero when it doesn't look like a declaration.
5981 */
5982 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005983cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 char_u *line, *p, *s;
5986 int len;
5987 pos_T fp;
5988 colnr_T col;
5989
5990 line = ml_get_curline();
5991 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005992 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5994 {
5995 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005996 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 }
5998 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5999 p = skipwhite(p + 6);
6000 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6001 p = skipwhite(p + 4);
6002 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6003 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6004 {
6005 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006006 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6007 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6008 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6009 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 p = s;
6011 }
6012 for (len = 0; vim_isIDc(p[len]); ++len)
6013 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006014 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 return 0;
6016
6017 p = skipwhite(p + len);
6018 fp.lnum = curwin->w_cursor.lnum;
6019 fp.col = (colnr_T)(p - line);
6020 getvcol(curwin, &fp, &col, NULL, NULL);
6021 return (int)col;
6022}
6023
6024/*
6025 * Return the indent of the first non-blank after an equal sign.
6026 * char *foo = "here";
6027 * Return zero if no (useful) equal sign found.
6028 * Return -1 if the line above "lnum" ends in a backslash.
6029 * foo = "asdf\
6030 * asdf\
6031 * here";
6032 */
6033 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006034cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 char_u *line;
6037 char_u *s;
6038 colnr_T col;
6039 pos_T fp;
6040
6041 if (lnum > 1)
6042 {
6043 line = ml_get(lnum - 1);
6044 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6045 return -1;
6046 }
6047
6048 line = s = ml_get(lnum);
6049 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6050 {
6051 if (cin_iscomment(s)) /* ignore comments */
6052 s = cin_skipcomment(s);
6053 else
6054 ++s;
6055 }
6056 if (*s != '=')
6057 return 0;
6058
6059 s = skipwhite(s + 1);
6060 if (cin_nocode(s))
6061 return 0;
6062
6063 if (*s == '"') /* nice alignment for continued strings */
6064 ++s;
6065
6066 fp.lnum = lnum;
6067 fp.col = (colnr_T)(s - line);
6068 getvcol(curwin, &fp, &col, NULL, NULL);
6069 return (int)col;
6070}
6071
6072/*
6073 * Recognize a preprocessor statement: Any line that starts with '#'.
6074 */
6075 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006076cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006078 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 return TRUE;
6080 return FALSE;
6081}
6082
6083/*
6084 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6085 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6086 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006087 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 */
6089 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006090cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 char_u *line = *pp;
6093 linenr_T lnum = *lnump;
6094 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006095 int candidate_amount = *amount;
6096
6097 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6098 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006100 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 {
6102 if (cin_ispreproc(line))
6103 {
6104 retval = TRUE;
6105 *lnump = lnum;
6106 break;
6107 }
6108 if (lnum == 1)
6109 break;
6110 line = ml_get(--lnum);
6111 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6112 break;
6113 }
6114
6115 if (lnum != *lnump)
6116 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006117 if (retval)
6118 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 return retval;
6120}
6121
6122/*
6123 * Recognize the start of a C or C++ comment.
6124 */
6125 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006126cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6129}
6130
6131/*
6132 * Recognize the start of a "//" comment.
6133 */
6134 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006135cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 return (p[0] == '/' && p[1] == '/');
6138}
6139
6140/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006141 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6142 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006144 * If a line begins with an "else", only consider it terminated if no unmatched
6145 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 * Return the character terminating the line (ending char's have precedence if
6147 * both apply in order to determine initializations).
6148 */
6149 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006150cin_isterminated(
6151 char_u *s,
6152 int incl_open, /* include '{' at the end as terminator */
6153 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006155 char_u found_start = 0;
6156 unsigned n_open = 0;
6157 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 s = cin_skipcomment(s);
6160
6161 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6162 found_start = *s;
6163
Bram Moolenaar496f9512011-05-19 16:35:09 +02006164 if (!found_start)
6165 is_else = cin_iselse(s);
6166
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 while (*s)
6168 {
6169 /* skip over comments, "" strings and 'c'haracters */
6170 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006171 if (*s == '}' && n_open > 0)
6172 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006173 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006174 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 && cin_nocode(s + 1))
6176 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006177 else if (*s == '{')
6178 {
6179 if (incl_open && cin_nocode(s + 1))
6180 return *s;
6181 else
6182 ++n_open;
6183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
6185 if (*s)
6186 s++;
6187 }
6188 return found_start;
6189}
6190
6191/*
6192 * Recognize the basic picture of a function declaration -- it needs to
6193 * have an open paren somewhere and a close paren at the end of the line and
6194 * no semicolons anywhere.
6195 * When a line ends in a comma we continue looking in the next line.
6196 * "sp" points to a string with the line. When looking at other lines it must
6197 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006198 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006199 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 */
6201 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006202cin_isfuncdecl(
6203 char_u **sp,
6204 linenr_T first_lnum,
6205 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 char_u *s;
6208 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006209 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006211 pos_T *trypos;
6212 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 if (sp == NULL)
6215 s = ml_get(lnum);
6216 else
6217 s = *sp;
6218
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006219 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006220 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006221 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006222 {
6223 lnum = trypos->lnum;
6224 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006225 {
6226 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006227 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006228 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006229
6230 s = ml_get(lnum);
6231 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006232 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006233
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006234 /* Ignore line starting with #. */
6235 if (cin_ispreproc(s))
6236 return FALSE;
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6239 {
6240 if (cin_iscomment(s)) /* ignore comments */
6241 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006242 else if (*s == ':')
6243 {
6244 if (*(s + 1) == ':')
6245 s += 2;
6246 else
6247 /* To avoid a mistake in the following situation:
6248 * A::A(int a, int b)
6249 * : a(0) // <--not a function decl
6250 * , b(0)
6251 * {...
6252 */
6253 return FALSE;
6254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 else
6256 ++s;
6257 }
6258 if (*s != '(')
6259 return FALSE; /* ';', ' or " before any () or no '(' */
6260
6261 while (*s && *s != ';' && *s != '\'' && *s != '"')
6262 {
6263 if (*s == ')' && cin_nocode(s + 1))
6264 {
6265 /* ')' at the end: may have found a match
6266 * Check for he previous line not to end in a backslash:
6267 * #if defined(x) && \
6268 * defined(y)
6269 */
6270 lnum = first_lnum - 1;
6271 s = ml_get(lnum);
6272 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6273 retval = TRUE;
6274 goto done;
6275 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006276 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006278 int comma = (*s == ',');
6279
6280 /* ',' at the end: continue looking in the next line.
6281 * At the end: check for ',' in the next line, for this style:
6282 * func(arg1
6283 * , arg2) */
6284 for (;;)
6285 {
6286 if (lnum >= curbuf->b_ml.ml_line_count)
6287 break;
6288 s = ml_get(++lnum);
6289 if (!cin_ispreproc(s))
6290 break;
6291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 if (lnum >= curbuf->b_ml.ml_line_count)
6293 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006294 /* Require a comma at end of the line or a comma or ')' at the
6295 * start of next line. */
6296 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006297 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006298 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006299 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 }
6301 else if (cin_iscomment(s)) /* ignore comments */
6302 s = cin_skipcomment(s);
6303 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006306 just_started = FALSE;
6307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 }
6309
6310done:
6311 if (lnum != first_lnum && sp != NULL)
6312 *sp = ml_get(first_lnum);
6313
6314 return retval;
6315}
6316
6317 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006318cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006320 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321}
6322
6323 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006324cin_iselse(
6325 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 if (*p == '}') /* accept "} else" */
6328 p = cin_skipcomment(p + 1);
6329 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6330}
6331
6332 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006333cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6336}
6337
6338/*
6339 * Check if this is a "while" that should have a matching "do".
6340 * We only accept a "while (condition) ;", with only white space between the
6341 * ')' and ';'. The condition may be spread over several lines.
6342 */
6343 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006344cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 pos_T cursor_save;
6347 pos_T *trypos;
6348 int retval = FALSE;
6349
6350 p = cin_skipcomment(p);
6351 if (*p == '}') /* accept "} while (cond);" */
6352 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006353 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355 cursor_save = curwin->w_cursor;
6356 curwin->w_cursor.lnum = lnum;
6357 curwin->w_cursor.col = 0;
6358 p = ml_get_curline();
6359 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6360 {
6361 ++p;
6362 ++curwin->w_cursor.col;
6363 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006364 if ((trypos = findmatchlimit(NULL, 0, 0,
6365 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6367 retval = TRUE;
6368 curwin->w_cursor = cursor_save;
6369 }
6370 return retval;
6371}
6372
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006373/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006374 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006375 * Return 0 if there is none.
6376 * Otherwise return !0 and update "*poffset" to point to the place where the
6377 * string was found.
6378 */
6379 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006380cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006381{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006382 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006383
6384 if (offset-- < 2)
6385 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006386 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006387 --offset;
6388
6389 offset -= 1;
6390 if (!STRNCMP(line + offset, "if", 2))
6391 goto probablyFound;
6392
6393 if (offset >= 1)
6394 {
6395 offset -= 1;
6396 if (!STRNCMP(line + offset, "for", 3))
6397 goto probablyFound;
6398
6399 if (offset >= 2)
6400 {
6401 offset -= 2;
6402 if (!STRNCMP(line + offset, "while", 5))
6403 goto probablyFound;
6404 }
6405 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006406 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006407
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006408probablyFound:
6409 if (!offset || !vim_isIDc(line[offset - 1]))
6410 {
6411 *poffset = offset;
6412 return 1;
6413 }
6414 return 0;
6415}
6416
6417/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006418 * Return TRUE if we are at the end of a do-while.
6419 * do
6420 * nothing;
6421 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006422 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006423 * Adjust the cursor to the line with "while".
6424 */
6425 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006426cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006427{
6428 char_u *line;
6429 char_u *p;
6430 char_u *s;
6431 pos_T *trypos;
6432 int i;
6433
6434 if (terminated != ';') /* there must be a ';' at the end */
6435 return FALSE;
6436
6437 p = line = ml_get_curline();
6438 while (*p != NUL)
6439 {
6440 p = cin_skipcomment(p);
6441 if (*p == ')')
6442 {
6443 s = skipwhite(p + 1);
6444 if (*s == ';' && cin_nocode(s + 1))
6445 {
6446 /* Found ");" at end of the line, now check there is "while"
6447 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006448 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006449 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006450 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006451 if (trypos != NULL)
6452 {
6453 s = cin_skipcomment(ml_get(trypos->lnum));
6454 if (*s == '}') /* accept "} while (cond);" */
6455 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006456 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006457 {
6458 curwin->w_cursor.lnum = trypos->lnum;
6459 return TRUE;
6460 }
6461 }
6462
6463 /* Searching may have made "line" invalid, get it again. */
6464 line = ml_get_curline();
6465 p = line + i;
6466 }
6467 }
6468 if (*p != NUL)
6469 ++p;
6470 }
6471 return FALSE;
6472}
6473
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006475cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6478}
6479
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006480/*
6481 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 * constructor-initialization. eg:
6483 *
6484 * class MyClass :
6485 * baseClass <-- here
6486 * class MyClass : public baseClass,
6487 * anotherBaseClass <-- here (should probably lineup ??)
6488 * MyClass::MyClass(...) :
6489 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006490 *
6491 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 */
6493 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006494cin_is_cpp_baseclass(
6495 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006497 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 char_u *s;
6499 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006500 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006501 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006503 if (pos->lnum <= lnum)
6504 return cached->found; /* Use the cached result */
6505
6506 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006508 s = skipwhite(line);
6509 if (*s == '#') /* skip #define FOO x ? (x) : x */
6510 return FALSE;
6511 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (*s == NUL)
6513 return FALSE;
6514
6515 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6516
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006517 /* Search for a line starting with '#', empty, ending in ';' or containing
6518 * '{' or '}' and start below it. This handles the following situations:
6519 * a = cond ?
6520 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006521 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006522 * func::foo()
6523 * : something
6524 * {}
6525 * Foo::Foo (int one, int two)
6526 * : something(4),
6527 * somethingelse(3)
6528 * {}
6529 */
6530 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006532 line = ml_get(lnum - 1);
6533 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006534 if (*s == '#' || *s == NUL)
6535 break;
6536 while (*s != NUL)
6537 {
6538 s = cin_skipcomment(s);
6539 if (*s == '{' || *s == '}'
6540 || (*s == ';' && cin_nocode(s + 1)))
6541 break;
6542 if (*s != NUL)
6543 ++s;
6544 }
6545 if (*s != NUL)
6546 break;
6547 --lnum;
6548 }
6549
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006550 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006551 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006552 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006553 for (;;)
6554 {
6555 if (*s == NUL)
6556 {
6557 if (lnum == curwin->w_cursor.lnum)
6558 break;
6559 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006560 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006561 s = line;
6562 }
6563 if (s == line)
6564 {
6565 /* don't recognize "case (foo):" as a baseclass */
6566 if (cin_iscase(s, FALSE))
6567 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006568 s = cin_skipcomment(line);
6569 if (*s == NUL)
6570 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006571 }
6572
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006573 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006574 s = skip_string(s) + 1;
6575 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 {
6577 if (s[1] == ':')
6578 {
6579 /* skip double colon. It can't be a constructor
6580 * initialization any more */
6581 lookfor_ctor_init = FALSE;
6582 s = cin_skipcomment(s + 2);
6583 }
6584 else if (lookfor_ctor_init || class_or_struct)
6585 {
6586 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006587 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 cpp_base_class = TRUE;
6589 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006590 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 s = cin_skipcomment(s + 1);
6592 }
6593 else
6594 s = cin_skipcomment(s + 1);
6595 }
6596 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6597 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6598 {
6599 class_or_struct = TRUE;
6600 lookfor_ctor_init = FALSE;
6601
6602 if (*s == 'c')
6603 s = cin_skipcomment(s + 5);
6604 else
6605 s = cin_skipcomment(s + 6);
6606 }
6607 else
6608 {
6609 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6610 {
6611 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6612 }
6613 else if (s[0] == ')')
6614 {
6615 /* Constructor-initialization is assumed if we come across
6616 * something like "):" */
6617 class_or_struct = FALSE;
6618 lookfor_ctor_init = TRUE;
6619 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006620 else if (s[0] == '?')
6621 {
6622 /* Avoid seeing '() :' after '?' as constructor init. */
6623 return FALSE;
6624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 else if (!vim_isIDc(s[0]))
6626 {
6627 /* if it is not an identifier, we are wrong */
6628 class_or_struct = FALSE;
6629 lookfor_ctor_init = FALSE;
6630 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006631 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 {
6633 /* it can't be a constructor-initialization any more */
6634 lookfor_ctor_init = FALSE;
6635
6636 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006637 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006638 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
6640
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006641 /* When the line ends in a comma don't align with it. */
6642 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006643 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006644
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 s = cin_skipcomment(s + 1);
6646 }
6647 }
6648
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006649 cached->found = cpp_base_class;
6650 if (cpp_base_class)
6651 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 return cpp_base_class;
6653}
6654
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006655 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006656get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006657{
6658 int amount;
6659 colnr_T vcol;
6660 pos_T *trypos;
6661
6662 if (col == 0)
6663 {
6664 amount = get_indent();
6665 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006666 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006667 amount = get_indent_lnum(trypos->lnum); /* XXX */
6668 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006669 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006670 }
6671 else
6672 {
6673 curwin->w_cursor.col = col;
6674 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6675 amount = (int)vcol;
6676 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006677 if (amount < curbuf->b_ind_cpp_baseclass)
6678 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006679 return amount;
6680}
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682/*
6683 * Return TRUE if string "s" ends with the string "find", possibly followed by
6684 * white space and comments. Skip strings and comments.
6685 * Ignore "ignore" after "find" if it's not NULL.
6686 */
6687 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006688cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690 char_u *p = s;
6691 char_u *r;
6692 int len = (int)STRLEN(find);
6693
6694 while (*p != NUL)
6695 {
6696 p = cin_skipcomment(p);
6697 if (STRNCMP(p, find, len) == 0)
6698 {
6699 r = skipwhite(p + len);
6700 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6701 r = skipwhite(r + STRLEN(ignore));
6702 if (cin_nocode(r))
6703 return TRUE;
6704 }
6705 if (*p != NUL)
6706 ++p;
6707 }
6708 return FALSE;
6709}
6710
6711/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006712 * Return TRUE when "s" starts with "word" and then a non-ID character.
6713 */
6714 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006716{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006717 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006718
6719 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6720}
6721
6722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 * Skip strings, chars and comments until at or past "trypos".
6724 * Return the column found.
6725 */
6726 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006727cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 char_u *line;
6730 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006731 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733 p = line = ml_get(trypos->lnum);
6734 while (*p && (colnr_T)(p - line) < trypos->col)
6735 {
6736 if (cin_iscomment(p))
6737 p = cin_skipcomment(p);
6738 else
6739 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006740 new_p = skip_string(p);
6741 if (new_p == p)
6742 ++p;
6743 else
6744 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 }
6746 }
6747 return (int)(p - line);
6748}
6749
6750/*
6751 * Find the '{' at the start of the block we are in.
6752 * Return NULL if no match found.
6753 * Ignore a '{' that is in a comment, makes indenting the next three lines
6754 * work. */
6755/* foo() */
6756/* { */
6757/* } */
6758
6759 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006760find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 pos_T cursor_save;
6763 pos_T *trypos;
6764 pos_T *pos;
6765 static pos_T pos_copy;
6766
6767 cursor_save = curwin->w_cursor;
6768 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6769 {
6770 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6771 trypos = &pos_copy;
6772 curwin->w_cursor = *trypos;
6773 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006774 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006776 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 break;
6778 if (pos != NULL)
6779 curwin->w_cursor.lnum = pos->lnum;
6780 }
6781 curwin->w_cursor = cursor_save;
6782 return trypos;
6783}
6784
6785/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006786 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006787 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 */
6789 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006790find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006792 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006793}
6794
6795 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006796find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006797{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 pos_T cursor_save;
6799 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006800 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006801 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006804 ind_maxp_wk = ind_maxparen;
6805retry:
6806 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
6808 /* check if the ( is in a // comment */
6809 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006810 {
6811 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6812 if (ind_maxp_wk > 0)
6813 {
6814 curwin->w_cursor = *trypos;
6815 curwin->w_cursor.col = 0; /* XXX */
6816 goto retry;
6817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 else
6821 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006822 pos_T *trypos_wk;
6823
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6825 trypos = &pos_copy;
6826 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006827 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006828 {
6829 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6830 - trypos_wk->lnum);
6831 if (ind_maxp_wk > 0)
6832 {
6833 curwin->w_cursor = *trypos_wk;
6834 goto retry;
6835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 }
6839 }
6840 curwin->w_cursor = cursor_save;
6841 return trypos;
6842}
6843
6844/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006845 * Find the matching '(', ignoring it if it is in a comment or before an
6846 * unmatched {.
6847 * Return NULL if no match found.
6848 */
6849 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006850find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006851{
6852 pos_T *trypos = find_match_paren(ind_maxparen);
6853
6854 if (trypos != NULL)
6855 {
6856 pos_T *tryposBrace = find_start_brace();
6857
6858 /* If both an unmatched '(' and '{' is found. Ignore the '('
6859 * position if the '{' is further down. */
6860 if (tryposBrace != NULL
6861 && (trypos->lnum != tryposBrace->lnum
6862 ? trypos->lnum < tryposBrace->lnum
6863 : trypos->col < tryposBrace->col))
6864 trypos = NULL;
6865 }
6866 return trypos;
6867}
6868
6869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * Return ind_maxparen corrected for the difference in line number between the
6871 * cursor position and "startpos". This makes sure that searching for a
6872 * matching paren above the cursor line doesn't find a match because of
6873 * looking a few lines further.
6874 */
6875 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006876corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6879
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006880 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6881 return curbuf->b_ind_maxparen - (int)n;
6882 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883}
6884
6885/*
6886 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006887 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 */
6889 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006890find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 int i;
6893 int retval = FALSE;
6894 int open_count = 0;
6895
6896 curwin->w_cursor.col = 0; /* default is start of line */
6897
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006898 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
6900 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6901 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6902 if (l[i] == start)
6903 ++open_count;
6904 else if (l[i] == end)
6905 {
6906 if (open_count > 0)
6907 --open_count;
6908 else
6909 {
6910 curwin->w_cursor.col = i;
6911 retval = TRUE;
6912 }
6913 }
6914 }
6915 return retval;
6916}
6917
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006918/*
6919 * Parse 'cinoptions' and set the values in "curbuf".
6920 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6921 */
6922 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006923parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006924{
6925 char_u *p;
6926 char_u *l;
6927 char_u *digits;
6928 int n;
6929 int divider;
6930 int fraction = 0;
6931 int sw = (int)get_sw_value(buf);
6932
6933 /*
6934 * Set the default values.
6935 */
6936 /* Spaces from a block's opening brace the prevailing indent for that
6937 * block should be. */
6938 buf->b_ind_level = sw;
6939
6940 /* Spaces from the edge of the line an open brace that's at the end of a
6941 * line is imagined to be. */
6942 buf->b_ind_open_imag = 0;
6943
6944 /* Spaces from the prevailing indent for a line that is not preceded by
6945 * an opening brace. */
6946 buf->b_ind_no_brace = 0;
6947
6948 /* Column where the first { of a function should be located }. */
6949 buf->b_ind_first_open = 0;
6950
6951 /* Spaces from the prevailing indent a leftmost open brace should be
6952 * located. */
6953 buf->b_ind_open_extra = 0;
6954
6955 /* Spaces from the matching open brace (real location for one at the left
6956 * edge; imaginary location from one that ends a line) the matching close
6957 * brace should be located. */
6958 buf->b_ind_close_extra = 0;
6959
6960 /* Spaces from the edge of the line an open brace sitting in the leftmost
6961 * column is imagined to be. */
6962 buf->b_ind_open_left_imag = 0;
6963
6964 /* Spaces jump labels should be shifted to the left if N is non-negative,
6965 * otherwise the jump label will be put to column 1. */
6966 buf->b_ind_jump_label = -1;
6967
6968 /* Spaces from the switch() indent a "case xx" label should be located. */
6969 buf->b_ind_case = sw;
6970
6971 /* Spaces from the "case xx:" code after a switch() should be located. */
6972 buf->b_ind_case_code = sw;
6973
6974 /* Lineup break at end of case in switch() with case label. */
6975 buf->b_ind_case_break = 0;
6976
6977 /* Spaces from the class declaration indent a scope declaration label
6978 * should be located. */
6979 buf->b_ind_scopedecl = sw;
6980
6981 /* Spaces from the scope declaration label code should be located. */
6982 buf->b_ind_scopedecl_code = sw;
6983
6984 /* Amount K&R-style parameters should be indented. */
6985 buf->b_ind_param = sw;
6986
6987 /* Amount a function type spec should be indented. */
6988 buf->b_ind_func_type = sw;
6989
6990 /* Amount a cpp base class declaration or constructor initialization
6991 * should be indented. */
6992 buf->b_ind_cpp_baseclass = sw;
6993
6994 /* additional spaces beyond the prevailing indent a continuation line
6995 * should be located. */
6996 buf->b_ind_continuation = sw;
6997
6998 /* Spaces from the indent of the line with an unclosed parentheses. */
6999 buf->b_ind_unclosed = sw * 2;
7000
7001 /* Spaces from the indent of the line with an unclosed parentheses, which
7002 * itself is also unclosed. */
7003 buf->b_ind_unclosed2 = sw;
7004
7005 /* Suppress ignoring spaces from the indent of a line starting with an
7006 * unclosed parentheses. */
7007 buf->b_ind_unclosed_noignore = 0;
7008
7009 /* If the opening paren is the last nonwhite character on the line, and
7010 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7011 * context (for very long lines). */
7012 buf->b_ind_unclosed_wrapped = 0;
7013
7014 /* Suppress ignoring white space when lining up with the character after
7015 * an unclosed parentheses. */
7016 buf->b_ind_unclosed_whiteok = 0;
7017
7018 /* Indent a closing parentheses under the line start of the matching
7019 * opening parentheses. */
7020 buf->b_ind_matching_paren = 0;
7021
7022 /* Indent a closing parentheses under the previous line. */
7023 buf->b_ind_paren_prev = 0;
7024
7025 /* Extra indent for comments. */
7026 buf->b_ind_comment = 0;
7027
7028 /* Spaces from the comment opener when there is nothing after it. */
7029 buf->b_ind_in_comment = 3;
7030
7031 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7032 * after the comment opener. */
7033 buf->b_ind_in_comment2 = 0;
7034
7035 /* Max lines to search for an open paren. */
7036 buf->b_ind_maxparen = 20;
7037
7038 /* Max lines to search for an open comment. */
7039 buf->b_ind_maxcomment = 70;
7040
7041 /* Handle braces for java code. */
7042 buf->b_ind_java = 0;
7043
7044 /* Not to confuse JS object properties with labels. */
7045 buf->b_ind_js = 0;
7046
7047 /* Handle blocked cases correctly. */
7048 buf->b_ind_keep_case_label = 0;
7049
7050 /* Handle C++ namespace. */
7051 buf->b_ind_cpp_namespace = 0;
7052
7053 /* Handle continuation lines containing conditions of if(), for() and
7054 * while(). */
7055 buf->b_ind_if_for_while = 0;
7056
Bram Moolenaar6b643942017-03-05 19:44:06 +01007057 /* indentation for # comments */
7058 buf->b_ind_hash_comment = 0;
7059
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007060 /* Handle C++ extern "C" or "C++" */
7061 buf->b_ind_cpp_extern_c = 0;
7062
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007063 for (p = buf->b_p_cino; *p; )
7064 {
7065 l = p++;
7066 if (*p == '-')
7067 ++p;
7068 digits = p; /* remember where the digits start */
7069 n = getdigits(&p);
7070 divider = 0;
7071 if (*p == '.') /* ".5s" means a fraction */
7072 {
7073 fraction = atol((char *)++p);
7074 while (VIM_ISDIGIT(*p))
7075 {
7076 ++p;
7077 if (divider)
7078 divider *= 10;
7079 else
7080 divider = 10;
7081 }
7082 }
7083 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7084 {
7085 if (p == digits)
7086 n = sw; /* just "s" is one 'shiftwidth' */
7087 else
7088 {
7089 n *= sw;
7090 if (divider)
7091 n += (sw * fraction + divider / 2) / divider;
7092 }
7093 ++p;
7094 }
7095 if (l[1] == '-')
7096 n = -n;
7097
7098 /* When adding an entry here, also update the default 'cinoptions' in
7099 * doc/indent.txt, and add explanation for it! */
7100 switch (*l)
7101 {
7102 case '>': buf->b_ind_level = n; break;
7103 case 'e': buf->b_ind_open_imag = n; break;
7104 case 'n': buf->b_ind_no_brace = n; break;
7105 case 'f': buf->b_ind_first_open = n; break;
7106 case '{': buf->b_ind_open_extra = n; break;
7107 case '}': buf->b_ind_close_extra = n; break;
7108 case '^': buf->b_ind_open_left_imag = n; break;
7109 case 'L': buf->b_ind_jump_label = n; break;
7110 case ':': buf->b_ind_case = n; break;
7111 case '=': buf->b_ind_case_code = n; break;
7112 case 'b': buf->b_ind_case_break = n; break;
7113 case 'p': buf->b_ind_param = n; break;
7114 case 't': buf->b_ind_func_type = n; break;
7115 case '/': buf->b_ind_comment = n; break;
7116 case 'c': buf->b_ind_in_comment = n; break;
7117 case 'C': buf->b_ind_in_comment2 = n; break;
7118 case 'i': buf->b_ind_cpp_baseclass = n; break;
7119 case '+': buf->b_ind_continuation = n; break;
7120 case '(': buf->b_ind_unclosed = n; break;
7121 case 'u': buf->b_ind_unclosed2 = n; break;
7122 case 'U': buf->b_ind_unclosed_noignore = n; break;
7123 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7124 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7125 case 'm': buf->b_ind_matching_paren = n; break;
7126 case 'M': buf->b_ind_paren_prev = n; break;
7127 case ')': buf->b_ind_maxparen = n; break;
7128 case '*': buf->b_ind_maxcomment = n; break;
7129 case 'g': buf->b_ind_scopedecl = n; break;
7130 case 'h': buf->b_ind_scopedecl_code = n; break;
7131 case 'j': buf->b_ind_java = n; break;
7132 case 'J': buf->b_ind_js = n; break;
7133 case 'l': buf->b_ind_keep_case_label = n; break;
7134 case '#': buf->b_ind_hash_comment = n; break;
7135 case 'N': buf->b_ind_cpp_namespace = n; break;
7136 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007137 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007138 }
7139 if (*p == ',')
7140 ++p;
7141 }
7142}
7143
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007144/*
7145 * Return the desired indent for C code.
7146 * Return -1 if the indent should be left alone (inside a raw string).
7147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007149get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 pos_T cur_curpos;
7152 int amount;
7153 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007154 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 colnr_T col;
7156 char_u *theline;
7157 char_u *linecopy;
7158 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007159 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007161 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 pos_T our_paren_pos;
7163 char_u *start;
7164 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007165#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166#define BRACE_AT_START 2 /* '{' is at start of line */
7167#define BRACE_AT_END 3 /* '{' is at end of line */
7168 linenr_T ourscope;
7169 char_u *l;
7170 char_u *look;
7171 char_u terminated;
7172 int lookfor;
7173#define LOOKFOR_INITIAL 0
7174#define LOOKFOR_IF 1
7175#define LOOKFOR_DO 2
7176#define LOOKFOR_CASE 3
7177#define LOOKFOR_ANY 4
7178#define LOOKFOR_TERM 5
7179#define LOOKFOR_UNTERM 6
7180#define LOOKFOR_SCOPEDECL 7
7181#define LOOKFOR_NOBREAK 8
7182#define LOOKFOR_CPP_BASECLASS 9
7183#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007184#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007185#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
7187 int whilelevel;
7188 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 int n;
7190 int iscase;
7191 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007192 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007194 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007195 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007196 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007197 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007198 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007200 /* make a copy, value is changed below */
7201 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
7203 /* remember where the cursor was when we started */
7204 cur_curpos = curwin->w_cursor;
7205
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007206 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007207 if (cur_curpos.lnum == 1)
7208 return 0;
7209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 /* Get a copy of the current contents of the line.
7211 * This is required, because only the most recent line obtained with
7212 * ml_get is valid! */
7213 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7214 if (linecopy == NULL)
7215 return 0;
7216
7217 /*
7218 * In insert mode and the cursor is on a ')' truncate the line at the
7219 * cursor position. We don't want to line up with the matching '(' when
7220 * inserting new stuff.
7221 * For unknown reasons the cursor might be past the end of the line, thus
7222 * check for that.
7223 */
7224 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007225 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 && linecopy[curwin->w_cursor.col] == ')')
7227 linecopy[curwin->w_cursor.col] = NUL;
7228
7229 theline = skipwhite(linecopy);
7230
7231 /* move the cursor to the start of the line */
7232
7233 curwin->w_cursor.col = 0;
7234
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007235 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007238 * If we are inside a raw string don't change the indent.
7239 * Ignore a raw string inside a comment.
7240 */
7241 comment_pos = ind_find_start_comment();
7242 if (comment_pos != NULL)
7243 {
7244 /* findmatchlimit() static pos is overwritten, make a copy */
7245 tryposCopy = *comment_pos;
7246 comment_pos = &tryposCopy;
7247 }
7248 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007249 if (trypos != NULL && (comment_pos == NULL
7250 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007251 {
7252 amount = -1;
7253 goto laterend;
7254 }
7255
7256 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 * #defines and so on always go at the left when included in 'cinkeys'.
7258 */
7259 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007260 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007261 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007262 goto theend;
7263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007266 * Is it a non-case label? Then that goes at the left margin too unless:
7267 * - JS flag is set.
7268 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007270 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007271 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {
7273 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007274 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276
7277 /*
7278 * If we're inside a "//" comment and there is a "//" comment in a
7279 * previous line, lineup with that one.
7280 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007281 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 && (trypos = find_line_comment()) != NULL) /* XXX */
7283 {
7284 /* find how indented the line beginning the comment is */
7285 getvcol(curwin, trypos, &col, NULL, NULL);
7286 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007287 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 }
7289
7290 /*
7291 * If we're inside a comment and not looking at the start of the
7292 * comment, try using the 'comments' option.
7293 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007294 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
7296 int lead_start_len = 2;
7297 int lead_middle_len = 1;
7298 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7299 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7300 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7301 char_u *p;
7302 int start_align = 0;
7303 int start_off = 0;
7304 int done = FALSE;
7305
7306 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007307 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007309 *lead_start = NUL;
7310 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312 p = curbuf->b_p_com;
7313 while (*p != NUL)
7314 {
7315 int align = 0;
7316 int off = 0;
7317 int what = 0;
7318
7319 while (*p != NUL && *p != ':')
7320 {
7321 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7322 what = *p++;
7323 else if (*p == COM_LEFT || *p == COM_RIGHT)
7324 align = *p++;
7325 else if (VIM_ISDIGIT(*p) || *p == '-')
7326 off = getdigits(&p);
7327 else
7328 ++p;
7329 }
7330
7331 if (*p == ':')
7332 ++p;
7333 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7334 if (what == COM_START)
7335 {
7336 STRCPY(lead_start, lead_end);
7337 lead_start_len = (int)STRLEN(lead_start);
7338 start_off = off;
7339 start_align = align;
7340 }
7341 else if (what == COM_MIDDLE)
7342 {
7343 STRCPY(lead_middle, lead_end);
7344 lead_middle_len = (int)STRLEN(lead_middle);
7345 }
7346 else if (what == COM_END)
7347 {
7348 /* If our line starts with the middle comment string, line it
7349 * up with the comment opener per the 'comments' option. */
7350 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7351 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7352 {
7353 done = TRUE;
7354 if (curwin->w_cursor.lnum > 1)
7355 {
7356 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007357 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 * the middle comment string matches in the previous
7359 * line, use the indent of that line. XXX */
7360 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7361 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7362 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7363 else if (STRNCMP(look, lead_middle,
7364 lead_middle_len) == 0)
7365 {
7366 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7367 break;
7368 }
7369 /* If the start comment string doesn't match with the
7370 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007371 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 lead_start, lead_start_len) != 0)
7373 continue;
7374 }
7375 if (start_off != 0)
7376 amount += start_off;
7377 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007378 amount += vim_strsize(lead_start)
7379 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 break;
7381 }
7382
7383 /* If our line starts with the end comment string, line it up
7384 * with the middle comment */
7385 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7386 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7387 {
7388 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7389 /* XXX */
7390 if (off != 0)
7391 amount += off;
7392 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007393 amount += vim_strsize(lead_start)
7394 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 done = TRUE;
7396 break;
7397 }
7398 }
7399 }
7400
7401 /* If our line starts with an asterisk, line up with the
7402 * asterisk in the comment opener; otherwise, line up
7403 * with the first character of the comment text.
7404 */
7405 if (done)
7406 ;
7407 else if (theline[0] == '*')
7408 amount += 1;
7409 else
7410 {
7411 /*
7412 * If we are more than one line away from the comment opener, take
7413 * the indent of the previous non-empty line. If 'cino' has "CO"
7414 * and we are just below the comment opener and there are any
7415 * white characters after it line up with the text after it;
7416 * otherwise, add the amount specified by "c" in 'cino'
7417 */
7418 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007419 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 {
7421 if (linewhite(lnum)) /* skip blank lines */
7422 continue;
7423 amount = get_indent_lnum(lnum); /* XXX */
7424 break;
7425 }
7426 if (amount == -1) /* use the comment opener */
7427 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007428 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007430 start = ml_get(comment_pos->lnum);
7431 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007433 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007435 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007437 if (curbuf->b_ind_in_comment2 || *look == NUL)
7438 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
7440 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007441 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 }
7443
7444 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007445 * Are we looking at a ']' that has a match?
7446 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007447 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007448 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7449 {
7450 /* align with the line containing the '['. */
7451 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007452 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007453 }
7454
7455 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 * Are we inside parentheses or braces?
7457 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007458 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007459 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007460 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 || trypos != NULL)
7462 {
7463 if (trypos != NULL && tryposBrace != NULL)
7464 {
7465 /* Both an unmatched '(' and '{' is found. Use the one which is
7466 * closer to the current cursor position, set the other to NULL. */
7467 if (trypos->lnum != tryposBrace->lnum
7468 ? trypos->lnum < tryposBrace->lnum
7469 : trypos->col < tryposBrace->col)
7470 trypos = NULL;
7471 else
7472 tryposBrace = NULL;
7473 }
7474
7475 if (trypos != NULL)
7476 {
7477 /*
7478 * If the matching paren is more than one line away, use the indent of
7479 * a previous non-empty line that matches the same paren.
7480 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007481 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007483 /* Line up with the start of the matching paren line. */
7484 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7485 }
7486 else
7487 {
7488 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007489 our_paren_pos = *trypos;
7490 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007492 l = skipwhite(ml_get(lnum));
7493 if (cin_nocode(l)) /* skip comment lines */
7494 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007495 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007496 continue; /* ignore #define, #if, etc. */
7497 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007499 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007500 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007501 {
7502 lnum = trypos->lnum + 1;
7503 continue;
7504 }
7505
7506 /* XXX */
7507 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007508 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007509 && trypos->lnum == our_paren_pos.lnum
7510 && trypos->col == our_paren_pos.col)
7511 {
7512 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007514 if (theline[0] == ')')
7515 {
7516 if (our_paren_pos.lnum != lnum
7517 && cur_amount > amount)
7518 cur_amount = amount;
7519 amount = -1;
7520 }
7521 break;
7522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524 }
7525
7526 /*
7527 * Line up with line where the matching paren is. XXX
7528 * If the line starts with a '(' or the indent for unclosed
7529 * parentheses is zero, line up with the unclosed parentheses.
7530 */
7531 if (amount == -1)
7532 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007533 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007534 int is_if_for_while = 0;
7535
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007536 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007537 {
7538 /* Look for the outermost opening parenthesis on this line
7539 * and check whether it belongs to an "if", "for" or "while". */
7540
7541 pos_T cursor_save = curwin->w_cursor;
7542 pos_T outermost;
7543 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007544
7545 trypos = &our_paren_pos;
7546 do {
7547 outermost = *trypos;
7548 curwin->w_cursor.lnum = outermost.lnum;
7549 curwin->w_cursor.col = outermost.col;
7550
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007551 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007552 } while (trypos && trypos->lnum == outermost.lnum);
7553
7554 curwin->w_cursor = cursor_save;
7555
7556 line = ml_get(outermost.lnum);
7557
7558 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007559 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007560 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007561
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007562 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007563 look = skipwhite(look);
7564 if (*look == '(')
7565 {
7566 linenr_T save_lnum = curwin->w_cursor.lnum;
7567 char_u *line;
7568 int look_col;
7569
7570 /* Ignore a '(' in front of the line that has a match before
7571 * our matching '('. */
7572 curwin->w_cursor.lnum = our_paren_pos.lnum;
7573 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007574 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007575 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007576 if ((trypos = findmatchlimit(NULL, ')', 0,
7577 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007578 != NULL
7579 && trypos->lnum == our_paren_pos.lnum
7580 && trypos->col < our_paren_pos.col)
7581 ignore_paren_col = trypos->col + 1;
7582
7583 curwin->w_cursor.lnum = save_lnum;
7584 look = ml_get(our_paren_pos.lnum) + look_col;
7585 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007586 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7587 && is_if_for_while == 0)
7588 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007589 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {
7591 /*
7592 * If we're looking at a close paren, line up right there;
7593 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007594 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 * the last nonwhite character of the line, use either the
7596 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007597 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 * lines).
7599 */
7600 if (theline[0] != ')')
7601 {
7602 cur_amount = MAXCOL;
7603 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007604 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 && cin_ends_in(l, (char_u *)"(", NULL))
7606 {
7607 /* look for opening unmatched paren, indent one level
7608 * for each additional level */
7609 n = 1;
7610 for (col = 0; col < our_paren_pos.col; ++col)
7611 {
7612 switch (l[col])
7613 {
7614 case '(':
7615 case '{': ++n;
7616 break;
7617
7618 case ')':
7619 case '}': if (n > 1)
7620 --n;
7621 break;
7622 }
7623 }
7624
7625 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007626 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007628 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 our_paren_pos.col++;
7630 else
7631 {
7632 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007633 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 col++;
7635 if (l[col] != NUL) /* In case of trailing space */
7636 our_paren_pos.col = col;
7637 else
7638 our_paren_pos.col++;
7639 }
7640 }
7641
7642 /*
7643 * Find how indented the paren is, or the character after it
7644 * if we did the above "if".
7645 */
7646 if (our_paren_pos.col > 0)
7647 {
7648 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7649 if (cur_amount > (int)col)
7650 cur_amount = col;
7651 }
7652 }
7653
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007654 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
7656 /* Line up with the start of the matching paren line. */
7657 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007658 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7659 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007660 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {
7662 if (cur_amount != MAXCOL)
7663 amount = cur_amount;
7664 }
7665 else
7666 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007667 /* Add b_ind_unclosed2 for each '(' before our matching one,
7668 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007670 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {
7672 --our_paren_pos.col;
7673 switch (*ml_get_pos(&our_paren_pos))
7674 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007675 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 col = our_paren_pos.col;
7677 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007678 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 col = MAXCOL;
7680 break;
7681 }
7682 }
7683
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007684 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 * braces */
7686 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007687 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 else
7689 {
7690 curwin->w_cursor.lnum = our_paren_pos.lnum;
7691 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007692 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7693 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007694 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007696 {
7697 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007698 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007699 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007700 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703 /*
7704 * For a line starting with ')' use the minimum of the two
7705 * positions, to avoid giving it more indent than the previous
7706 * lines:
7707 * func_long_name( if (x
7708 * arg && yy
7709 * ) ^ not here ) ^ not here
7710 */
7711 if (cur_amount < amount)
7712 amount = cur_amount;
7713 }
7714 }
7715
7716 /* add extra indent for a comment */
7717 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007718 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 else
7721 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007722 /*
7723 * We are inside braces, there is a { before this line at the position
7724 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007725 * Make a copy of tryposBrace, it may point to pos_copy inside
7726 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007727 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007728 tryposCopy = *tryposBrace;
7729 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 ourscope = trypos->lnum;
7732 start = ml_get(ourscope);
7733
7734 /*
7735 * Now figure out how indented the line is in general.
7736 * If the brace was at the start of the line, we use that;
7737 * otherwise, check out the indentation of the line as
7738 * a whole and then add the "imaginary indent" to that.
7739 */
7740 look = skipwhite(start);
7741 if (*look == '{')
7742 {
7743 getvcol(curwin, trypos, &col, NULL, NULL);
7744 amount = col;
7745 if (*start == '{')
7746 start_brace = BRACE_IN_COL0;
7747 else
7748 start_brace = BRACE_AT_START;
7749 }
7750 else
7751 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007752 /* That opening brace might have been on a continuation
7753 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 curwin->w_cursor.lnum = ourscope;
7755
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007756 /* Position the cursor over the rightmost paren, so that
7757 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 lnum = ourscope;
7759 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007760 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7761 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 lnum = trypos->lnum;
7763
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007764 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 * case 1: if (asdf &&
7766 * ldfd) {
7767 * }
7768 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007769 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7770 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007772 else if (curbuf->b_ind_js)
7773 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007775 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777 start_brace = BRACE_AT_END;
7778 }
7779
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007780 /* For Javascript check if the line starts with "key:". */
7781 if (curbuf->b_ind_js)
7782 js_cur_has_key = cin_has_js_key(theline);
7783
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007785 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 * we want to be. otherwise, add the amount of room
7787 * that an indent is supposed to be.
7788 */
7789 if (theline[0] == '}')
7790 {
7791 /*
7792 * they may want closing braces to line up with something
7793 * other than the open brace. indulge them, if so.
7794 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007795 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
7797 else
7798 {
7799 /*
7800 * If we're looking at an "else", try to find an "if"
7801 * to match it with.
7802 * If we're looking at a "while", try to find a "do"
7803 * to match it with.
7804 */
7805 lookfor = LOOKFOR_INITIAL;
7806 if (cin_iselse(theline))
7807 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007808 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 lookfor = LOOKFOR_DO;
7810 if (lookfor != LOOKFOR_INITIAL)
7811 {
7812 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007813 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 {
7815 amount = get_indent(); /* XXX */
7816 goto theend;
7817 }
7818 }
7819
7820 /*
7821 * We get here if we are not on an "while-of-do" or "else" (or
7822 * failed to find a matching "if").
7823 * Search backwards for something to line up with.
7824 * First set amount for when we don't find anything.
7825 */
7826
7827 /*
7828 * if the '{' is _really_ at the left margin, use the imaginary
7829 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007830 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 */
7832
7833 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7834 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007835 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007836 lookfor_cpp_namespace = TRUE;
7837 }
7838 else if (start_brace == BRACE_AT_START &&
7839 lookfor_cpp_namespace) /* '{' is at start */
7840 {
7841
7842 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 }
7844 else
7845 {
7846 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007847 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007848 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007849
7850 l = skipwhite(ml_get_curline());
7851 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007852 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007853 else if (cin_is_cpp_extern_c(l))
7854 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 else
7857 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007858 /* Compensate for adding b_ind_open_extra later. */
7859 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 if (amount < 0)
7861 amount = 0;
7862 }
7863 }
7864
7865 lookfor_break = FALSE;
7866
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007867 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
7869 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007870 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 }
7872 else if (cin_isscopedecl(theline)) /* private:, ... */
7873 {
7874 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007875 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
7877 else
7878 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007879 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7880 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 lookfor_break = TRUE;
7882
7883 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007884 /* b_ind_level from start of block */
7885 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887 scope_amount = amount;
7888 whilelevel = 0;
7889
7890 /*
7891 * Search backwards. If we find something we recognize, line up
7892 * with that.
7893 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007894 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 * the usual amount relative to the conditional
7896 * that opens the block.
7897 */
7898 curwin->w_cursor = cur_curpos;
7899 for (;;)
7900 {
7901 curwin->w_cursor.lnum--;
7902 curwin->w_cursor.col = 0;
7903
7904 /*
7905 * If we went all the way back to the start of our scope, line
7906 * up with it.
7907 */
7908 if (curwin->w_cursor.lnum <= ourscope)
7909 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007910 /* We reached end of scope:
7911 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007913 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 * don't add ind_continuation, otherwise it is a variable
7915 * declaration:
7916 * int x,
7917 * here; <-- add ind_continuation
7918 */
7919 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7920 {
7921 if (curwin->w_cursor.lnum == 0
7922 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007923 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007925 /* nothing found (abuse curbuf->b_ind_maxparen as
7926 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 * initialization) */
7928 if (cont_amount > 0)
7929 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007930 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 amount += ind_continuation;
7932 break;
7933 }
7934
7935 l = ml_get_curline();
7936
7937 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007938 * If we're in a comment or raw string now, skip to
7939 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007941 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 if (trypos != NULL)
7943 {
7944 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007945 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 continue;
7947 }
7948
7949 /*
7950 * Skip preprocessor directives and blank lines.
7951 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007952 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7953 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 continue;
7955
7956 if (cin_nocode(l))
7957 continue;
7958
7959 terminated = cin_isterminated(l, FALSE, TRUE);
7960
7961 /*
7962 * If we are at top level and the line looks like a
7963 * function declaration, we are done
7964 * (it's a variable declaration).
7965 */
7966 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007967 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {
7969 /* if the line is terminated with another ','
7970 * it is a continued variable initialization.
7971 * don't add extra indent.
7972 * TODO: does not work, if a function
7973 * declaration is split over multiple lines:
7974 * cin_isfuncdecl returns FALSE then.
7975 */
7976 if (terminated == ',')
7977 break;
7978
7979 /* if it es a enum declaration or an assignment,
7980 * we are done.
7981 */
7982 if (terminated != ';' && cin_isinit())
7983 break;
7984
7985 /* nothing useful found */
7986 if (terminated == 0 || terminated == '{')
7987 continue;
7988 }
7989
7990 if (terminated != ';')
7991 {
7992 /* Skip parens and braces. Position the cursor
7993 * over the rightmost paren, so that matching it
7994 * will take us back to the start of the line.
7995 */ /* XXX */
7996 trypos = NULL;
7997 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007998 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007999 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000
8001 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008002 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 if (trypos != NULL)
8005 {
8006 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008007 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 continue;
8009 }
8010 }
8011
8012 /* it's a variable declaration, add indentation
8013 * like in
8014 * int a,
8015 * b;
8016 */
8017 if (cont_amount > 0)
8018 amount = cont_amount;
8019 else
8020 amount += ind_continuation;
8021 }
8022 else if (lookfor == LOOKFOR_UNTERM)
8023 {
8024 if (cont_amount > 0)
8025 amount = cont_amount;
8026 else
8027 amount += ind_continuation;
8028 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008029 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008030 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008031 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008032 && lookfor != LOOKFOR_CPP_BASECLASS
8033 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008034 {
8035 amount = scope_amount;
8036 if (theline[0] == '{')
8037 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008038 amount += curbuf->b_ind_open_extra;
8039 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008040 }
8041 }
8042
8043 if (lookfor_cpp_namespace)
8044 {
8045 /*
8046 * Looking for C++ namespace, need to look further
8047 * back.
8048 */
8049 if (curwin->w_cursor.lnum == ourscope)
8050 continue;
8051
8052 if (curwin->w_cursor.lnum == 0
8053 || curwin->w_cursor.lnum
8054 < ourscope - FIND_NAMESPACE_LIM)
8055 break;
8056
8057 l = ml_get_curline();
8058
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008059 /* If we're in a comment or raw string now, skip
8060 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008061 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008062 if (trypos != NULL)
8063 {
8064 curwin->w_cursor.lnum = trypos->lnum + 1;
8065 curwin->w_cursor.col = 0;
8066 continue;
8067 }
8068
8069 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008070 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8071 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008072 continue;
8073
8074 /* Finally the actual check for "namespace". */
8075 if (cin_is_cpp_namespace(l))
8076 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008077 amount += curbuf->b_ind_cpp_namespace
8078 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008079 break;
8080 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008081 else if (cin_is_cpp_extern_c(l))
8082 {
8083 amount += curbuf->b_ind_cpp_extern_c
8084 - added_to_amount;
8085 break;
8086 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008087
8088 if (cin_nocode(l))
8089 continue;
8090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 break;
8093 }
8094
8095 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008096 * If we're in a comment or raw string now, skip to the start
8097 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008099 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
8101 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008102 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 continue;
8104 }
8105
8106 l = ml_get_curline();
8107
8108 /*
8109 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008110 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008112 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (iscase || cin_isscopedecl(l))
8114 {
8115 /* we are only looking for cpp base class
8116 * declaration/initialization any longer */
8117 if (lookfor == LOOKFOR_CPP_BASECLASS)
8118 break;
8119
8120 /* When looking for a "do" we are not interested in
8121 * labels. */
8122 if (whilelevel > 0)
8123 continue;
8124
8125 /*
8126 * case xx:
8127 * c = 99 + <- this indent plus continuation
8128 *-> here;
8129 */
8130 if (lookfor == LOOKFOR_UNTERM
8131 || lookfor == LOOKFOR_ENUM_OR_INIT)
8132 {
8133 if (cont_amount > 0)
8134 amount = cont_amount;
8135 else
8136 amount += ind_continuation;
8137 break;
8138 }
8139
8140 /*
8141 * case xx: <- line up with this case
8142 * x = 333;
8143 * case yy:
8144 */
8145 if ( (iscase && lookfor == LOOKFOR_CASE)
8146 || (iscase && lookfor_break)
8147 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8148 {
8149 /*
8150 * Check that this case label is not for another
8151 * switch()
8152 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008153 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008154 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {
8156 amount = get_indent(); /* XXX */
8157 break;
8158 }
8159 continue;
8160 }
8161
8162 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8163
8164 /*
8165 * case xx: if (cond) <- line up with this if
8166 * y = y + 1;
8167 * -> s = 99;
8168 *
8169 * case xx:
8170 * if (cond) <- line up with this line
8171 * y = y + 1;
8172 * -> s = 99;
8173 */
8174 if (lookfor == LOOKFOR_TERM)
8175 {
8176 if (n)
8177 amount = n;
8178
8179 if (!lookfor_break)
8180 break;
8181 }
8182
8183 /*
8184 * case xx: x = x + 1; <- line up with this x
8185 * -> y = y + 1;
8186 *
8187 * case xx: if (cond) <- line up with this if
8188 * -> y = y + 1;
8189 */
8190 if (n)
8191 {
8192 amount = n;
8193 l = after_label(ml_get_curline());
8194 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008195 {
8196 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008197 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008198 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008199 amount += curbuf->b_ind_level
8200 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 break;
8203 }
8204
8205 /*
8206 * Try to get the indent of a statement before the switch
8207 * label. If nothing is found, line up relative to the
8208 * switch label.
8209 * break; <- may line up with this line
8210 * case xx:
8211 * -> y = 1;
8212 */
8213 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008214 ? curbuf->b_ind_case_code
8215 : curbuf->b_ind_scopedecl_code);
8216 lookfor = curbuf->b_ind_case_break
8217 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 continue;
8219 }
8220
8221 /*
8222 * Looking for a switch() label or C++ scope declaration,
8223 * ignore other lines, skip {}-blocks.
8224 */
8225 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8226 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008227 if (find_last_paren(l, '{', '}')
8228 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008229 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008231 curwin->w_cursor.col = 0;
8232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 continue;
8234 }
8235
8236 /*
8237 * Ignore jump labels with nothing after them.
8238 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008239 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {
8241 l = after_label(ml_get_curline());
8242 if (l == NULL || cin_nocode(l))
8243 continue;
8244 }
8245
8246 /*
8247 * Ignore #defines, #if, etc.
8248 * Ignore comment and empty lines.
8249 * (need to get the line again, cin_islabel() may have
8250 * unlocked it)
8251 */
8252 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008253 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 || cin_nocode(l))
8255 continue;
8256
8257 /*
8258 * Are we at the start of a cpp base class declaration or
8259 * constructor initialization?
8260 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008261 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008262 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008263 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008264 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008265 l = ml_get_curline();
8266 }
8267 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 {
8269 if (lookfor == LOOKFOR_UNTERM)
8270 {
8271 if (cont_amount > 0)
8272 amount = cont_amount;
8273 else
8274 amount += ind_continuation;
8275 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008276 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008278 /* Need to find start of the declaration. */
8279 lookfor = LOOKFOR_UNTERM;
8280 ind_continuation = 0;
8281 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 }
8283 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008284 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008285 amount = get_baseclass_amount(
8286 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 break;
8288 }
8289 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8290 {
8291 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008292 * declaration or initialization before the opening brace.
8293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (cin_isterminated(l, TRUE, FALSE))
8295 break;
8296 else
8297 continue;
8298 }
8299
8300 /*
8301 * What happens next depends on the line being terminated.
8302 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008303 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 * 123,
8305 * sizeof
8306 * here
8307 * Otherwise check whether it is a enumeration or structure
8308 * initialisation (not indented) or a variable declaration
8309 * (indented).
8310 */
8311 terminated = cin_isterminated(l, FALSE, TRUE);
8312
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008313 if (js_cur_has_key)
8314 {
8315 js_cur_has_key = 0; /* only check the first line */
8316 if (curbuf->b_ind_js && terminated == ',')
8317 {
8318 /* For Javascript we might be inside an object:
8319 * key: something, <- align with this
8320 * key: something
8321 * or:
8322 * key: something + <- align with this
8323 * something,
8324 * key: something
8325 */
8326 lookfor = LOOKFOR_JS_KEY;
8327 }
8328 }
8329 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8330 {
8331 amount = get_indent();
8332 break;
8333 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008334 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008335 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008336 if (tryposBrace != NULL && tryposBrace->lnum
8337 >= curwin->w_cursor.lnum)
8338 break;
8339 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008340 /* line below current line is the one that starts a
8341 * (possibly broken) line ending in a comma. */
8342 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008343 else
8344 {
8345 amount = get_indent();
8346 if (curwin->w_cursor.lnum - 1 == ourscope)
8347 /* line above is start of the scope, thus current
8348 * line is the one that stars a (possibly broken)
8349 * line ending in a comma. */
8350 break;
8351 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008352 }
8353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8355 && terminated == ','))
8356 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008357 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8358 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008359 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 /*
8361 * if we're in the middle of a paren thing,
8362 * go back to the line that starts it so
8363 * we can get the right prevailing indent
8364 * if ( foo &&
8365 * bar )
8366 */
8367 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008368 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008370 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 */
8372 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008373 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008374 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8375 || (trypos->lnum == tryposBrace->lnum
8376 && trypos->col < tryposBrace->col)))
8377 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 /*
8380 * If we are looking for ',', we also look for matching
8381 * braces.
8382 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008383 if (trypos == NULL && terminated == ','
8384 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008385 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
8387 if (trypos != NULL)
8388 {
8389 /*
8390 * Check if we are on a case label now. This is
8391 * handled above.
8392 * case xx: if ( asdf &&
8393 * asdf)
8394 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008395 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008397 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {
8399 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008400 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 continue;
8402 }
8403 }
8404
8405 /*
8406 * Skip over continuation lines to find the one to get the
8407 * indent from
8408 * char *usethis = "bla\
8409 * bla",
8410 * here;
8411 */
8412 if (terminated == ',')
8413 {
8414 while (curwin->w_cursor.lnum > 1)
8415 {
8416 l = ml_get(curwin->w_cursor.lnum - 1);
8417 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8418 break;
8419 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008420 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
8422 }
8423
8424 /*
8425 * Get indent and pointer to text for current line,
8426 * ignoring any jump label. XXX
8427 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008428 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008429 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008430 else
8431 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 /*
8433 * If this is just above the line we are indenting, and it
8434 * starts with a '{', line it up with this line.
8435 * while (not)
8436 * -> {
8437 * }
8438 */
8439 if (terminated != ',' && lookfor != LOOKFOR_TERM
8440 && theline[0] == '{')
8441 {
8442 amount = cur_amount;
8443 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008444 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 * doesn't start with a '{', which must have a match
8446 * in the same line (scope is the same). Probably:
8447 * { 1, 2 },
8448 * -> { 3, 4 }
8449 */
8450 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008451 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008453 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {
8455 /* have to look back, whether it is a cpp base
8456 * class declaration or initialization */
8457 lookfor = LOOKFOR_CPP_BASECLASS;
8458 continue;
8459 }
8460 break;
8461 }
8462
8463 /*
8464 * Check if we are after an "if", "while", etc.
8465 * Also allow " } else".
8466 */
8467 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8468 {
8469 /*
8470 * Found an unterminated line after an if (), line up
8471 * with the last one.
8472 * if (cond)
8473 * 100 +
8474 * -> here;
8475 */
8476 if (lookfor == LOOKFOR_UNTERM
8477 || lookfor == LOOKFOR_ENUM_OR_INIT)
8478 {
8479 if (cont_amount > 0)
8480 amount = cont_amount;
8481 else
8482 amount += ind_continuation;
8483 break;
8484 }
8485
8486 /*
8487 * If this is just above the line we are indenting, we
8488 * are finished.
8489 * while (not)
8490 * -> here;
8491 * Otherwise this indent can be used when the line
8492 * before this is terminated.
8493 * yyy;
8494 * if (stat)
8495 * while (not)
8496 * xxx;
8497 * -> here;
8498 */
8499 amount = cur_amount;
8500 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008501 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 if (lookfor != LOOKFOR_TERM)
8503 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008504 amount += curbuf->b_ind_level
8505 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 break;
8507 }
8508
8509 /*
8510 * Special trick: when expecting the while () after a
8511 * do, line up with the while()
8512 * do
8513 * x = 1;
8514 * -> here
8515 */
8516 l = skipwhite(ml_get_curline());
8517 if (cin_isdo(l))
8518 {
8519 if (whilelevel == 0)
8520 break;
8521 --whilelevel;
8522 }
8523
8524 /*
8525 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008526 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 * Need to use the scope of this "else". XXX
8528 * If whilelevel != 0 continue looking for a "do {".
8529 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008530 if (cin_iselse(l) && whilelevel == 0)
8531 {
8532 /* If we're looking at "} else", let's make sure we
8533 * find the opening brace of the enclosing scope,
8534 * not the one from "if () {". */
8535 if (*l == '}')
8536 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008537 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008538
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008539 if ((trypos = find_start_brace()) == NULL
8540 || find_match(LOOKFOR_IF, trypos->lnum)
8541 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008542 break;
8543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 }
8545
8546 /*
8547 * If we're below an unterminated line that is not an
8548 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008549 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 * the line before this one.
8551 */
8552 else
8553 {
8554 /*
8555 * Found two unterminated lines on a row, line up with
8556 * the last one.
8557 * c = 99 +
8558 * 100 +
8559 * -> here;
8560 */
8561 if (lookfor == LOOKFOR_UNTERM)
8562 {
8563 /* When line ends in a comma add extra indent */
8564 if (terminated == ',')
8565 amount += ind_continuation;
8566 break;
8567 }
8568
8569 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8570 {
8571 /* Found two lines ending in ',', lineup with the
8572 * lowest one, but check for cpp base class
8573 * declaration/initialization, if it is an
8574 * opening brace or we are looking just for
8575 * enumerations/initializations. */
8576 if (terminated == ',')
8577 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008578 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 break;
8580
8581 lookfor = LOOKFOR_CPP_BASECLASS;
8582 continue;
8583 }
8584
8585 /* Ignore unterminated lines in between, but
8586 * reduce indent. */
8587 if (amount > cur_amount)
8588 amount = cur_amount;
8589 }
8590 else
8591 {
8592 /*
8593 * Found first unterminated line on a row, may
8594 * line up with this line, remember its indent
8595 * 100 +
8596 * -> here;
8597 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008598 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008600
8601 n = (int)STRLEN(l);
8602 if (terminated == ',' && (*skipwhite(l) == ']'
8603 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008604 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
8606 /*
8607 * If previous line ends in ',', check whether we
8608 * are in an initialization or enum
8609 * struct xxx =
8610 * {
8611 * sizeof a,
8612 * 124 };
8613 * or a normal possible continuation line.
8614 * but only, of no other statement has been found
8615 * yet.
8616 */
8617 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8618 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008619 if (curbuf->b_ind_js)
8620 {
8621 /* Search for a line ending in a comma
8622 * and line up with the line below it
8623 * (could be the current line).
8624 * some = [
8625 * 1, <- line up here
8626 * 2,
8627 * some = [
8628 * 3 + <- line up here
8629 * 4 *
8630 * 5,
8631 * 6,
8632 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008633 if (cin_iscomment(skipwhite(l)))
8634 break;
8635 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008636 trypos = find_match_char('[',
8637 curbuf->b_ind_maxparen);
8638 if (trypos != NULL)
8639 {
8640 if (trypos->lnum
8641 == curwin->w_cursor.lnum - 1)
8642 {
8643 /* Current line is first inside
8644 * [], line up with it. */
8645 break;
8646 }
8647 ourscope = trypos->lnum;
8648 }
8649 }
8650 else
8651 {
8652 lookfor = LOOKFOR_ENUM_OR_INIT;
8653 cont_amount = cin_first_id_amount();
8654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 }
8656 else
8657 {
8658 if (lookfor == LOOKFOR_INITIAL
8659 && *l != NUL
8660 && l[STRLEN(l) - 1] == '\\')
8661 /* XXX */
8662 cont_amount = cin_get_equal_amount(
8663 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008664 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008665 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008666 && lookfor != LOOKFOR_COMMA
8667 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 lookfor = LOOKFOR_UNTERM;
8669 }
8670 }
8671 }
8672 }
8673
8674 /*
8675 * Check if we are after a while (cond);
8676 * If so: Ignore until the matching "do".
8677 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008678 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 {
8680 /*
8681 * Found an unterminated line after a while ();, line up
8682 * with the last one.
8683 * while (cond);
8684 * 100 + <- line up with this one
8685 * -> here;
8686 */
8687 if (lookfor == LOOKFOR_UNTERM
8688 || lookfor == LOOKFOR_ENUM_OR_INIT)
8689 {
8690 if (cont_amount > 0)
8691 amount = cont_amount;
8692 else
8693 amount += ind_continuation;
8694 break;
8695 }
8696
8697 if (whilelevel == 0)
8698 {
8699 lookfor = LOOKFOR_TERM;
8700 amount = get_indent(); /* XXX */
8701 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008702 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 }
8704 ++whilelevel;
8705 }
8706
8707 /*
8708 * We are after a "normal" statement.
8709 * If we had another statement we can stop now and use the
8710 * indent of that other statement.
8711 * Otherwise the indent of the current statement may be used,
8712 * search backwards for the next "normal" statement.
8713 */
8714 else
8715 {
8716 /*
8717 * Skip single break line, if before a switch label. It
8718 * may be lined up with the case label.
8719 */
8720 if (lookfor == LOOKFOR_NOBREAK
8721 && cin_isbreak(skipwhite(ml_get_curline())))
8722 {
8723 lookfor = LOOKFOR_ANY;
8724 continue;
8725 }
8726
8727 /*
8728 * Handle "do {" line.
8729 */
8730 if (whilelevel > 0)
8731 {
8732 l = cin_skipcomment(ml_get_curline());
8733 if (cin_isdo(l))
8734 {
8735 amount = get_indent(); /* XXX */
8736 --whilelevel;
8737 continue;
8738 }
8739 }
8740
8741 /*
8742 * Found a terminated line above an unterminated line. Add
8743 * the amount for a continuation line.
8744 * x = 1;
8745 * y = foo +
8746 * -> here;
8747 * or
8748 * int x = 1;
8749 * int foo,
8750 * -> here;
8751 */
8752 if (lookfor == LOOKFOR_UNTERM
8753 || lookfor == LOOKFOR_ENUM_OR_INIT)
8754 {
8755 if (cont_amount > 0)
8756 amount = cont_amount;
8757 else
8758 amount += ind_continuation;
8759 break;
8760 }
8761
8762 /*
8763 * Found a terminated line above a terminated line or "if"
8764 * etc. line. Use the amount of the line below us.
8765 * x = 1; x = 1;
8766 * if (asdf) y = 2;
8767 * while (asdf) ->here;
8768 * here;
8769 * ->foo;
8770 */
8771 if (lookfor == LOOKFOR_TERM)
8772 {
8773 if (!lookfor_break && whilelevel == 0)
8774 break;
8775 }
8776
8777 /*
8778 * First line above the one we're indenting is terminated.
8779 * To know what needs to be done look further backward for
8780 * a terminated line.
8781 */
8782 else
8783 {
8784 /*
8785 * position the cursor over the rightmost paren, so
8786 * that matching it will take us back to the start of
8787 * the line. Helps for:
8788 * func(asdr,
8789 * asdfasdf);
8790 * here;
8791 */
8792term_again:
8793 l = ml_get_curline();
8794 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008795 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008796 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 {
8798 /*
8799 * Check if we are on a case label now. This is
8800 * handled above.
8801 * case xx: if ( asdf &&
8802 * asdf)
8803 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008804 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008806 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
8808 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008809 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 continue;
8811 }
8812 }
8813
8814 /* When aligning with the case statement, don't align
8815 * with a statement after it.
8816 * case 1: { <-- don't use this { position
8817 * stat;
8818 * }
8819 * case 2:
8820 * stat;
8821 * }
8822 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008823 iscase = (curbuf->b_ind_keep_case_label
8824 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826 /*
8827 * Get indent and pointer to text for current line,
8828 * ignoring any jump label.
8829 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008830 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831
8832 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008833 amount += curbuf->b_ind_open_extra;
8834 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008835 l = skipwhite(l);
8836 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008837 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8839
8840 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008841 * When a terminated line starts with "else" skip to
8842 * the matching "if":
8843 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008844 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008845 * Need to use the scope of this "else". XXX
8846 * If whilelevel != 0 continue looking for a "do {".
8847 */
8848 if (lookfor == LOOKFOR_TERM
8849 && *l != '}'
8850 && cin_iselse(l)
8851 && whilelevel == 0)
8852 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008853 if ((trypos = find_start_brace()) == NULL
8854 || find_match(LOOKFOR_IF, trypos->lnum)
8855 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008856 break;
8857 continue;
8858 }
8859
8860 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 * If we're at the end of a block, skip to the start of
8862 * that block.
8863 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008864 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008865 if (find_last_paren(l, '{', '}') /* XXX */
8866 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008868 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 /* if not "else {" check for terminated again */
8870 /* but skip block for "} else {" */
8871 l = cin_skipcomment(ml_get_curline());
8872 if (*l == '}' || !cin_iselse(l))
8873 goto term_again;
8874 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008875 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 }
8877 }
8878 }
8879 }
8880 }
8881 }
8882
8883 /* add extra indent for a comment */
8884 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008885 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008886
8887 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008888 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8889 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008890
8891 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008893
8894 /*
8895 * ok -- we're not inside any sort of structure at all!
8896 *
8897 * This means we're at the top level, and everything should
8898 * basically just match where the previous line is, except
8899 * for the lines immediately following a function declaration,
8900 * which are K&R-style parameters and need to be indented.
8901 *
8902 * if our line starts with an open brace, forget about any
8903 * prevailing indent and make sure it looks like the start
8904 * of a function
8905 */
8906
8907 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008909 amount = curbuf->b_ind_first_open;
8910 goto theend;
8911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008913 /*
8914 * If the NEXT line is a function declaration, the current
8915 * line needs to be indented as a function type spec.
8916 * Don't do this if the current line looks like a comment or if the
8917 * current line is terminated, ie. ends in ';', or if the current line
8918 * contains { or }: "void f() {\n if (1)"
8919 */
8920 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8921 && !cin_nocode(theline)
8922 && vim_strchr(theline, '{') == NULL
8923 && vim_strchr(theline, '}') == NULL
8924 && !cin_ends_in(theline, (char_u *)":", NULL)
8925 && !cin_ends_in(theline, (char_u *)",", NULL)
8926 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8927 cur_curpos.lnum + 1)
8928 && !cin_isterminated(theline, FALSE, TRUE))
8929 {
8930 amount = curbuf->b_ind_func_type;
8931 goto theend;
8932 }
8933
8934 /* search backwards until we find something we recognize */
8935 amount = 0;
8936 curwin->w_cursor = cur_curpos;
8937 while (curwin->w_cursor.lnum > 1)
8938 {
8939 curwin->w_cursor.lnum--;
8940 curwin->w_cursor.col = 0;
8941
8942 l = ml_get_curline();
8943
8944 /*
8945 * If we're in a comment or raw string now, skip to the start
8946 * of it.
8947 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008948 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008950 curwin->w_cursor.lnum = trypos->lnum + 1;
8951 curwin->w_cursor.col = 0;
8952 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 }
8954
8955 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008956 * Are we at the start of a cpp base class declaration or
8957 * constructor initialization?
8958 */ /* XXX */
8959 n = FALSE;
8960 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008962 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8963 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008965 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008967 /* XXX */
8968 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8969 break;
8970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008972 /*
8973 * Skip preprocessor directives and blank lines.
8974 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008975 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008976 continue;
8977
8978 if (cin_nocode(l))
8979 continue;
8980
8981 /*
8982 * If the previous line ends in ',', use one level of
8983 * indentation:
8984 * int foo,
8985 * bar;
8986 * do this before checking for '}' in case of eg.
8987 * enum foobar
8988 * {
8989 * ...
8990 * } foo,
8991 * bar;
8992 */
8993 n = 0;
8994 if (cin_ends_in(l, (char_u *)",", NULL)
8995 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8996 {
8997 /* take us back to opening paren */
8998 if (find_last_paren(l, '(', ')')
8999 && (trypos = find_match_paren(
9000 curbuf->b_ind_maxparen)) != NULL)
9001 curwin->w_cursor = *trypos;
9002
9003 /* For a line ending in ',' that is a continuation line go
9004 * back to the first line with a backslash:
9005 * char *foo = "bla\
9006 * bla",
9007 * here;
9008 */
9009 while (n == 0 && curwin->w_cursor.lnum > 1)
9010 {
9011 l = ml_get(curwin->w_cursor.lnum - 1);
9012 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9013 break;
9014 --curwin->w_cursor.lnum;
9015 curwin->w_cursor.col = 0;
9016 }
9017
9018 amount = get_indent(); /* XXX */
9019
9020 if (amount == 0)
9021 amount = cin_first_id_amount();
9022 if (amount == 0)
9023 amount = ind_continuation;
9024 break;
9025 }
9026
9027 /*
9028 * If the line looks like a function declaration, and we're
9029 * not in a comment, put it the left margin.
9030 */
9031 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9032 break;
9033 l = ml_get_curline();
9034
9035 /*
9036 * Finding the closing '}' of a previous function. Put
9037 * current line at the left margin. For when 'cino' has "fs".
9038 */
9039 if (*skipwhite(l) == '}')
9040 break;
9041
9042 /* (matching {)
9043 * If the previous line ends on '};' (maybe followed by
9044 * comments) align at column 0. For example:
9045 * char *string_array[] = { "foo",
9046 * / * x * / "b};ar" }; / * foobar * /
9047 */
9048 if (cin_ends_in(l, (char_u *)"};", NULL))
9049 break;
9050
9051 /*
9052 * If the previous line ends on '[' we are probably in an
9053 * array constant:
9054 * something = [
9055 * 234, <- extra indent
9056 */
9057 if (cin_ends_in(l, (char_u *)"[", NULL))
9058 {
9059 amount = get_indent() + ind_continuation;
9060 break;
9061 }
9062
9063 /*
9064 * Find a line only has a semicolon that belongs to a previous
9065 * line ending in '}', e.g. before an #endif. Don't increase
9066 * indent then.
9067 */
9068 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9069 {
9070 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071
9072 while (curwin->w_cursor.lnum > 1)
9073 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009074 look = ml_get(--curwin->w_cursor.lnum);
9075 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009076 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009078 }
9079 if (curwin->w_cursor.lnum > 0
9080 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009083 curwin->w_cursor = curpos_save;
9084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009086 /*
9087 * If the PREVIOUS line is a function declaration, the current
9088 * line (and the ones that follow) needs to be indented as
9089 * parameters.
9090 */
9091 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9092 {
9093 amount = curbuf->b_ind_param;
9094 break;
9095 }
9096
9097 /*
9098 * If the previous line ends in ';' and the line before the
9099 * previous line ends in ',' or '\', ident to column zero:
9100 * int foo,
9101 * bar;
9102 * indent_to_0 here;
9103 */
9104 if (cin_ends_in(l, (char_u *)";", NULL))
9105 {
9106 l = ml_get(curwin->w_cursor.lnum - 1);
9107 if (cin_ends_in(l, (char_u *)",", NULL)
9108 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9109 break;
9110 l = ml_get_curline();
9111 }
9112
9113 /*
9114 * Doesn't look like anything interesting -- so just
9115 * use the indent of this line.
9116 *
9117 * Position the cursor over the rightmost paren, so that
9118 * matching it will take us back to the start of the line.
9119 */
9120 find_last_paren(l, '(', ')');
9121
9122 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9123 curwin->w_cursor = *trypos;
9124 amount = get_indent(); /* XXX */
9125 break;
9126 }
9127
9128 /* add extra indent for a comment */
9129 if (cin_iscomment(theline))
9130 amount += curbuf->b_ind_comment;
9131
9132 /* add extra indent if the previous line ended in a backslash:
9133 * "asdfasdf\
9134 * here";
9135 * char *foo = "asdf\
9136 * here";
9137 */
9138 if (cur_curpos.lnum > 1)
9139 {
9140 l = ml_get(cur_curpos.lnum - 1);
9141 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9142 {
9143 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9144 if (cur_amount > 0)
9145 amount = cur_amount;
9146 else if (cur_amount == 0)
9147 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 }
9149 }
9150
9151theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009152 if (amount < 0)
9153 amount = 0;
9154
9155laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 /* put the cursor back where it belongs */
9157 curwin->w_cursor = cur_curpos;
9158
9159 vim_free(linecopy);
9160
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 return amount;
9162}
9163
9164 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009165find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167 char_u *look;
9168 pos_T *theirscope;
9169 char_u *mightbeif;
9170 int elselevel;
9171 int whilelevel;
9172
9173 if (lookfor == LOOKFOR_IF)
9174 {
9175 elselevel = 1;
9176 whilelevel = 0;
9177 }
9178 else
9179 {
9180 elselevel = 0;
9181 whilelevel = 1;
9182 }
9183
9184 curwin->w_cursor.col = 0;
9185
9186 while (curwin->w_cursor.lnum > ourscope + 1)
9187 {
9188 curwin->w_cursor.lnum--;
9189 curwin->w_cursor.col = 0;
9190
9191 look = cin_skipcomment(ml_get_curline());
9192 if (cin_iselse(look)
9193 || cin_isif(look)
9194 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009195 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
9197 /*
9198 * if we've gone outside the braces entirely,
9199 * we must be out of scope...
9200 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009201 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 if (theirscope == NULL)
9203 break;
9204
9205 /*
9206 * and if the brace enclosing this is further
9207 * back than the one enclosing the else, we're
9208 * out of luck too.
9209 */
9210 if (theirscope->lnum < ourscope)
9211 break;
9212
9213 /*
9214 * and if they're enclosed in a *deeper* brace,
9215 * then we can ignore it because it's in a
9216 * different scope...
9217 */
9218 if (theirscope->lnum > ourscope)
9219 continue;
9220
9221 /*
9222 * if it was an "else" (that's not an "else if")
9223 * then we need to go back to another if, so
9224 * increment elselevel
9225 */
9226 look = cin_skipcomment(ml_get_curline());
9227 if (cin_iselse(look))
9228 {
9229 mightbeif = cin_skipcomment(look + 4);
9230 if (!cin_isif(mightbeif))
9231 ++elselevel;
9232 continue;
9233 }
9234
9235 /*
9236 * if it was a "while" then we need to go back to
9237 * another "do", so increment whilelevel. XXX
9238 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009239 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 {
9241 ++whilelevel;
9242 continue;
9243 }
9244
9245 /* If it's an "if" decrement elselevel */
9246 look = cin_skipcomment(ml_get_curline());
9247 if (cin_isif(look))
9248 {
9249 elselevel--;
9250 /*
9251 * When looking for an "if" ignore "while"s that
9252 * get in the way.
9253 */
9254 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9255 whilelevel = 0;
9256 }
9257
9258 /* If it's a "do" decrement whilelevel */
9259 if (cin_isdo(look))
9260 whilelevel--;
9261
9262 /*
9263 * if we've used up all the elses, then
9264 * this must be the if that we want!
9265 * match the indent level of that if.
9266 */
9267 if (elselevel <= 0 && whilelevel <= 0)
9268 {
9269 return OK;
9270 }
9271 }
9272 }
9273 return FAIL;
9274}
9275
9276# if defined(FEAT_EVAL) || defined(PROTO)
9277/*
9278 * Get indent level from 'indentexpr'.
9279 */
9280 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009281get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009283 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009284 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009285 pos_T save_pos;
9286 colnr_T save_curswant;
9287 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009289 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9290 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009292 /* Save and restore cursor position and curswant, in case it was changed
9293 * via :normal commands */
9294 save_pos = curwin->w_cursor;
9295 save_curswant = curwin->w_curswant;
9296 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009298 if (use_sandbox)
9299 ++sandbox;
9300 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009301
9302 /* Need to make a copy, the 'indentexpr' option could be changed while
9303 * evaluating it. */
9304 inde_copy = vim_strsave(curbuf->b_p_inde);
9305 if (inde_copy != NULL)
9306 {
9307 indent = (int)eval_to_number(inde_copy);
9308 vim_free(inde_copy);
9309 }
9310
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009311 if (use_sandbox)
9312 --sandbox;
9313 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314
9315 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9316 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9317 * command. */
9318 save_State = State;
9319 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009320 curwin->w_cursor = save_pos;
9321 curwin->w_curswant = save_curswant;
9322 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 check_cursor();
9324 State = save_State;
9325
9326 /* If there is an error, just keep the current indent. */
9327 if (indent < 0)
9328 indent = get_indent();
9329
9330 return indent;
9331}
9332# endif
9333
9334#endif /* FEAT_CINDENT */
9335
9336#if defined(FEAT_LISP) || defined(PROTO)
9337
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009338static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
9340 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009341lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343 char_u buf[LSIZE];
9344 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009345 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346
9347 while (*word != NUL)
9348 {
9349 (void)copy_option_part(&word, buf, LSIZE, ",");
9350 len = (int)STRLEN(buf);
9351 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9352 return TRUE;
9353 }
9354 return FALSE;
9355}
9356
9357/*
9358 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9359 * The incompatible newer method is quite a bit better at indenting
9360 * code in lisp-like languages than the traditional one; it's still
9361 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9362 *
9363 * TODO:
9364 * Findmatch() should be adapted for lisp, also to make showmatch
9365 * work correctly: now (v5.3) it seems all C/C++ oriented:
9366 * - it does not recognize the #\( and #\) notations as character literals
9367 * - it doesn't know about comments starting with a semicolon
9368 * - it incorrectly interprets '(' as a character literal
9369 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009370 * Update from Sergey Khorev:
9371 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 */
9373 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009374get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009376 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 int amount;
9378 char_u *that;
9379 colnr_T col;
9380 colnr_T firsttry;
9381 int parencount, quotecount;
9382 int vi_lisp;
9383
9384 /* Set vi_lisp to use the vi-compatible method */
9385 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9386
9387 realpos = curwin->w_cursor;
9388 curwin->w_cursor.col = 0;
9389
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009390 if ((pos = findmatch(NULL, '(')) == NULL)
9391 pos = findmatch(NULL, '[');
9392 else
9393 {
9394 paren = *pos;
9395 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009396 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009397 pos = &paren;
9398 }
9399 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 {
9401 /* Extra trick: Take the indent of the first previous non-white
9402 * line that is at the same () level. */
9403 amount = -1;
9404 parencount = 0;
9405
9406 while (--curwin->w_cursor.lnum >= pos->lnum)
9407 {
9408 if (linewhite(curwin->w_cursor.lnum))
9409 continue;
9410 for (that = ml_get_curline(); *that != NUL; ++that)
9411 {
9412 if (*that == ';')
9413 {
9414 while (*(that + 1) != NUL)
9415 ++that;
9416 continue;
9417 }
9418 if (*that == '\\')
9419 {
9420 if (*(that + 1) != NUL)
9421 ++that;
9422 continue;
9423 }
9424 if (*that == '"' && *(that + 1) != NUL)
9425 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009426 while (*++that && *that != '"')
9427 {
9428 /* skipping escaped characters in the string */
9429 if (*that == '\\')
9430 {
9431 if (*++that == NUL)
9432 break;
9433 if (that[1] == NUL)
9434 {
9435 ++that;
9436 break;
9437 }
9438 }
9439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009441 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009443 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 --parencount;
9445 }
9446 if (parencount == 0)
9447 {
9448 amount = get_indent();
9449 break;
9450 }
9451 }
9452
9453 if (amount == -1)
9454 {
9455 curwin->w_cursor.lnum = pos->lnum;
9456 curwin->w_cursor.col = pos->col;
9457 col = pos->col;
9458
9459 that = ml_get_curline();
9460
9461 if (vi_lisp && get_indent() == 0)
9462 amount = 2;
9463 else
9464 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009465 char_u *line = that;
9466
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 amount = 0;
9468 while (*that && col)
9469 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009470 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 col--;
9472 }
9473
9474 /*
9475 * Some keywords require "body" indenting rules (the
9476 * non-standard-lisp ones are Scheme special forms):
9477 *
9478 * (let ((a 1)) instead (let ((a 1))
9479 * (...)) of (...))
9480 */
9481
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009482 if (!vi_lisp && (*that == '(' || *that == '[')
9483 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 amount += 2;
9485 else
9486 {
9487 that++;
9488 amount++;
9489 firsttry = amount;
9490
Bram Moolenaar1c465442017-03-12 20:10:05 +01009491 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009493 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 ++that;
9495 }
9496
9497 if (*that && *that != ';') /* not a comment line */
9498 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009499 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009501 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 firsttry++;
9503
9504 parencount = 0;
9505 quotecount = 0;
9506
9507 if (vi_lisp
9508 || (*that != '"'
9509 && *that != '\''
9510 && *that != '#'
9511 && (*that < '0' || *that > '9')))
9512 {
9513 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009514 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 || quotecount
9516 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009517 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 && !quotecount
9519 && !parencount
9520 && vi_lisp)))
9521 {
9522 if (*that == '"')
9523 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009524 if ((*that == '(' || *that == '[')
9525 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009527 if ((*that == ')' || *that == ']')
9528 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 --parencount;
9530 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009531 amount += lbr_chartabsize_adv(
9532 line, &that, (colnr_T)amount);
9533 amount += lbr_chartabsize_adv(
9534 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 }
9536 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009537 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009539 amount += lbr_chartabsize(
9540 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 that++;
9542 }
9543 if (!*that || *that == ';')
9544 amount = firsttry;
9545 }
9546 }
9547 }
9548 }
9549 }
9550 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009551 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
9553 curwin->w_cursor = realpos;
9554
9555 return amount;
9556}
9557#endif /* FEAT_LISP */
9558
9559 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009560prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009562#if defined(SIGHUP) && defined(SIG_IGN)
9563 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9564 * makes Vim exit and then handling SIGHUP causes various reentrance
9565 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009566 signal(SIGHUP, SIG_IGN);
9567#endif
9568
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569#ifdef FEAT_GUI
9570 if (gui.in_use)
9571 {
9572 gui.dying = TRUE;
9573 out_trash(); /* trash any pending output */
9574 }
9575 else
9576#endif
9577 {
9578 windgoto((int)Rows - 1, 0);
9579
9580 /*
9581 * Switch terminal mode back now, so messages end up on the "normal"
9582 * screen (if there are two screens).
9583 */
9584 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009585 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 out_flush();
9587 }
9588}
9589
9590/*
9591 * Preserve files and exit.
9592 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009593 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9594 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 */
9596 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009597preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599 buf_T *buf;
9600
9601 prepare_to_exit();
9602
Bram Moolenaar4770d092006-01-12 23:22:24 +00009603 /* Setting this will prevent free() calls. That avoids calling free()
9604 * recursively when free() was invoked with a bad pointer. */
9605 really_exiting = TRUE;
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 out_str(IObuff);
9608 screen_start(); /* don't know where cursor is now */
9609 out_flush();
9610
9611 ml_close_notmod(); /* close all not-modified buffers */
9612
Bram Moolenaar29323592016-07-24 22:04:11 +02009613 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
9615 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9616 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009617 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 screen_start(); /* don't know where cursor is now */
9619 out_flush();
9620 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9621 break;
9622 }
9623 }
9624
9625 ml_close_all(FALSE); /* close all memfiles, without deleting */
9626
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009627 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628
9629 getout(1);
9630}
9631
9632/*
9633 * return TRUE if "fname" exists.
9634 */
9635 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009636vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009638 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639
9640 if (mch_stat((char *)fname, &st))
9641 return FALSE;
9642 return TRUE;
9643}
9644
9645/*
9646 * Check for CTRL-C pressed, but only once in a while.
9647 * Should be used instead of ui_breakcheck() for functions that check for
9648 * each line in the file. Calling ui_breakcheck() each time takes too much
9649 * time, because it can be a system call.
9650 */
9651
9652#ifndef BREAKCHECK_SKIP
9653# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9654# define BREAKCHECK_SKIP 200
9655# else
9656# define BREAKCHECK_SKIP 32
9657# endif
9658#endif
9659
9660static int breakcheck_count = 0;
9661
9662 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009663line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 if (++breakcheck_count >= BREAKCHECK_SKIP)
9666 {
9667 breakcheck_count = 0;
9668 ui_breakcheck();
9669 }
9670}
9671
9672/*
9673 * Like line_breakcheck() but check 10 times less often.
9674 */
9675 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009676fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
9678 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9679 {
9680 breakcheck_count = 0;
9681 ui_breakcheck();
9682 }
9683}
9684
9685/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009686 * Invoke expand_wildcards() for one pattern.
9687 * Expand items like "%:h" before the expansion.
9688 * Returns OK or FAIL.
9689 */
9690 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009691expand_wildcards_eval(
9692 char_u **pat, /* pointer to input pattern */
9693 int *num_file, /* resulting number of files */
9694 char_u ***file, /* array of resulting files */
9695 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009696{
9697 int ret = FAIL;
9698 char_u *eval_pat = NULL;
9699 char_u *exp_pat = *pat;
9700 char_u *ignored_msg;
9701 int usedlen;
9702
9703 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9704 {
9705 ++emsg_off;
9706 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9707 NULL, &ignored_msg, NULL);
9708 --emsg_off;
9709 if (eval_pat != NULL)
9710 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9711 }
9712
9713 if (exp_pat != NULL)
9714 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9715
9716 if (eval_pat != NULL)
9717 {
9718 vim_free(exp_pat);
9719 vim_free(eval_pat);
9720 }
9721
9722 return ret;
9723}
9724
9725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9727 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009728 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 */
9730 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009731expand_wildcards(
9732 int num_pat, /* number of input patterns */
9733 char_u **pat, /* array of input patterns */
9734 int *num_files, /* resulting number of files */
9735 char_u ***files, /* array of resulting files */
9736 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738 int retval;
9739 int i, j;
9740 char_u *p;
9741 int non_suf_match; /* number without matching suffix */
9742
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009743 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744
9745 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009746 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 return retval;
9748
9749#ifdef FEAT_WILDIGN
9750 /*
9751 * Remove names that match 'wildignore'.
9752 */
9753 if (*p_wig)
9754 {
9755 char_u *ffname;
9756
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009757 /* check all files in (*files)[] */
9758 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009760 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (ffname == NULL) /* out of memory */
9762 break;
9763# ifdef VMS
9764 vms_remove_version(ffname);
9765# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009766 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009768 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009769 vim_free((*files)[i]);
9770 for (j = i; j + 1 < *num_files; ++j)
9771 (*files)[j] = (*files)[j + 1];
9772 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 --i;
9774 }
9775 vim_free(ffname);
9776 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009777
9778 /* If the number of matches is now zero, we fail. */
9779 if (*num_files == 0)
9780 {
9781 vim_free(*files);
9782 *files = NULL;
9783 return FAIL;
9784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 }
9786#endif
9787
9788 /*
9789 * Move the names where 'suffixes' match to the end.
9790 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009791 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 {
9793 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009794 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009796 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 {
9798 /*
9799 * Move the name without matching suffix to the front
9800 * of the list.
9801 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009802 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009804 (*files)[j] = (*files)[j - 1];
9805 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 }
9807 }
9808 }
9809
9810 return retval;
9811}
9812
9813/*
9814 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9815 */
9816 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009817match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819 int fnamelen, setsuflen;
9820 char_u *setsuf;
9821#define MAXSUFLEN 30 /* maximum length of a file suffix */
9822 char_u suf_buf[MAXSUFLEN];
9823
9824 fnamelen = (int)STRLEN(fname);
9825 setsuflen = 0;
9826 for (setsuf = p_su; *setsuf; )
9827 {
9828 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009829 if (setsuflen == 0)
9830 {
9831 char_u *tail = gettail(fname);
9832
9833 /* empty entry: match name without a '.' */
9834 if (vim_strchr(tail, '.') == NULL)
9835 {
9836 setsuflen = 1;
9837 break;
9838 }
9839 }
9840 else
9841 {
9842 if (fnamelen >= setsuflen
9843 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9844 (size_t)setsuflen) == 0)
9845 break;
9846 setsuflen = 0;
9847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 }
9849 return (setsuflen != 0);
9850}
9851
9852#if !defined(NO_EXPANDPATH) || defined(PROTO)
9853
9854# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009855static int vim_backtick(char_u *p);
9856static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857# endif
9858
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009859# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860/*
9861 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9862 * it's shared between these systems.
9863 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009864# if defined(PROTO)
9865# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866# else
9867# ifdef __BORLANDC__
9868# define _cdecl _RTLENTRYF
9869# endif
9870# endif
9871
9872/*
9873 * comparison function for qsort in dos_expandpath()
9874 */
9875 static int _cdecl
9876pstrcmp(const void *a, const void *b)
9877{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009878 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879}
9880
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009882 * Recursively expand one path component into all matching files and/or
9883 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 * Return the number of matches found.
9885 * "path" has backslashes before chars that are not to be expanded, starting
9886 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009887 * Return the number of matches found.
9888 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 */
9890 static int
9891dos_expandpath(
9892 garray_T *gap,
9893 char_u *path,
9894 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009895 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009896 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009898 char_u *buf;
9899 char_u *path_end;
9900 char_u *p, *s, *e;
9901 int start_len = gap->ga_len;
9902 char_u *pat;
9903 regmatch_T regmatch;
9904 int starts_with_dot;
9905 int matches;
9906 int len;
9907 int starstar = FALSE;
9908 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 WIN32_FIND_DATA fb;
9910 HANDLE hFind = (HANDLE)0;
9911# ifdef FEAT_MBYTE
9912 WIN32_FIND_DATAW wfb;
9913 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009916 int ok;
9917
9918 /* Expanding "**" may take a long time, check for CTRL-C. */
9919 if (stardepth > 0)
9920 {
9921 ui_breakcheck();
9922 if (got_int)
9923 return 0;
9924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009926 /* Make room for file name. When doing encoding conversion the actual
9927 * length may be quite a bit longer, thus use the maximum possible length. */
9928 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (buf == NULL)
9930 return 0;
9931
9932 /*
9933 * Find the first part in the path name that contains a wildcard or a ~1.
9934 * Copy it into buf, including the preceding characters.
9935 */
9936 p = buf;
9937 s = buf;
9938 e = NULL;
9939 path_end = path;
9940 while (*path_end != NUL)
9941 {
9942 /* May ignore a wildcard that has a backslash before it; it will
9943 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9944 if (path_end >= path + wildoff && rem_backslash(path_end))
9945 *p++ = *path_end++;
9946 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9947 {
9948 if (e != NULL)
9949 break;
9950 s = p + 1;
9951 }
9952 else if (path_end >= path + wildoff
9953 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9954 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009955# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 if (has_mbyte)
9957 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009958 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 STRNCPY(p, path_end, len);
9960 p += len;
9961 path_end += len;
9962 }
9963 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 *p++ = *path_end++;
9966 }
9967 e = p;
9968 *e = NUL;
9969
9970 /* now we have one wildcard component between s and e */
9971 /* Remove backslashes between "wildoff" and the start of the wildcard
9972 * component. */
9973 for (p = buf + wildoff; p < s; ++p)
9974 if (rem_backslash(p))
9975 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009976 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 --e;
9978 --s;
9979 }
9980
Bram Moolenaar231334e2005-07-25 20:46:57 +00009981 /* Check for "**" between "s" and "e". */
9982 for (p = s; p < e; ++p)
9983 if (p[0] == '*' && p[1] == '*')
9984 starstar = TRUE;
9985
Bram Moolenaard82103e2016-01-17 17:04:05 +01009986 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9988 if (pat == NULL)
9989 {
9990 vim_free(buf);
9991 return 0;
9992 }
9993
9994 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009995 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009996 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 regmatch.rm_ic = TRUE; /* Always ignore case */
9998 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009999 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010000 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 vim_free(pat);
10002
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010003 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 {
10005 vim_free(buf);
10006 return 0;
10007 }
10008
10009 /* remember the pattern or file name being looked for */
10010 matchname = vim_strsave(s);
10011
Bram Moolenaar231334e2005-07-25 20:46:57 +000010012 /* If "**" is by itself, this is the first time we encounter it and more
10013 * is following then find matches without any directory. */
10014 if (!didstar && stardepth < 100 && starstar && e - s == 2
10015 && *path_end == '/')
10016 {
10017 STRCPY(s, path_end + 1);
10018 ++stardepth;
10019 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10020 --stardepth;
10021 }
10022
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 /* Scan all files in the directory with "dir/ *.*" */
10024 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025# ifdef FEAT_MBYTE
10026 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10027 {
10028 /* The active codepage differs from 'encoding'. Attempt using the
10029 * wide function. If it fails because it is not implemented fall back
10030 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010031 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 if (wn != NULL)
10033 {
10034 hFind = FindFirstFileW(wn, &wfb);
10035 if (hFind == INVALID_HANDLE_VALUE
10036 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
10037 {
10038 vim_free(wn);
10039 wn = NULL;
10040 }
10041 }
10042 }
10043
10044 if (wn == NULL)
10045# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010046 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048
10049 while (ok)
10050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051# ifdef FEAT_MBYTE
10052 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010053 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 else
10055# endif
10056 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 /* Ignore entries starting with a dot, unless when asked for. Accept
10058 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010059 if ((p[0] != '.' || starts_with_dot
10060 || ((flags & EW_DODOT)
10061 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010063 || (regmatch.regprog != NULL
10064 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010065 || ((flags & EW_NOTWILD)
10066 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010070
10071 if (starstar && stardepth < 100)
10072 {
10073 /* For "**" in the pattern first go deeper in the tree to
10074 * find matches. */
10075 STRCPY(buf + len, "/**");
10076 STRCPY(buf + len + 3, path_end);
10077 ++stardepth;
10078 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10079 --stardepth;
10080 }
10081
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 STRCPY(buf + len, path_end);
10083 if (mch_has_exp_wildcard(path_end))
10084 {
10085 /* need to expand another component of the path */
10086 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010087 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 }
10089 else
10090 {
10091 /* no more wildcards, check if there is a match */
10092 /* remove backslashes for the remaining components only */
10093 if (*path_end != 0)
10094 backslash_halve(buf + len + 1);
10095 if (mch_getperm(buf) >= 0) /* add existing file */
10096 addfile(gap, buf, flags);
10097 }
10098 }
10099
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100# ifdef FEAT_MBYTE
10101 if (wn != NULL)
10102 {
10103 vim_free(p);
10104 ok = FindNextFileW(hFind, &wfb);
10105 }
10106 else
10107# endif
10108 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109
10110 /* If no more matches and no match was used, try expanding the name
10111 * itself. Finds the long name of a short filename. */
10112 if (!ok && matchname != NULL && gap->ga_len == start_len)
10113 {
10114 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 FindClose(hFind);
10116# ifdef FEAT_MBYTE
10117 if (wn != NULL)
10118 {
10119 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010120 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (wn != NULL)
10122 hFind = FindFirstFileW(wn, &wfb);
10123 }
10124 if (wn == NULL)
10125# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010126 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 vim_free(matchname);
10129 matchname = NULL;
10130 }
10131 }
10132
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 FindClose(hFind);
10134# ifdef FEAT_MBYTE
10135 vim_free(wn);
10136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010138 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 vim_free(matchname);
10140
10141 matches = gap->ga_len - start_len;
10142 if (matches > 0)
10143 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10144 sizeof(char_u *), pstrcmp);
10145 return matches;
10146}
10147
10148 int
10149mch_expandpath(
10150 garray_T *gap,
10151 char_u *path,
10152 int flags) /* EW_* flags */
10153{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010154 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010156# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157
Bram Moolenaar231334e2005-07-25 20:46:57 +000010158#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10159 || defined(PROTO)
10160/*
10161 * Unix style wildcard expansion code.
10162 * It's here because it's used both for Unix and Mac.
10163 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010164static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010165
10166 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010167pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010168{
10169 return (pathcmp(*(char **)a, *(char **)b, -1));
10170}
10171
10172/*
10173 * Recursively expand one path component into all matching files and/or
10174 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10175 * "path" has backslashes before chars that are not to be expanded, starting
10176 * at "path + wildoff".
10177 * Return the number of matches found.
10178 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10179 */
10180 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010181unix_expandpath(
10182 garray_T *gap,
10183 char_u *path,
10184 int wildoff,
10185 int flags, /* EW_* flags */
10186 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010187{
10188 char_u *buf;
10189 char_u *path_end;
10190 char_u *p, *s, *e;
10191 int start_len = gap->ga_len;
10192 char_u *pat;
10193 regmatch_T regmatch;
10194 int starts_with_dot;
10195 int matches;
10196 int len;
10197 int starstar = FALSE;
10198 static int stardepth = 0; /* depth for "**" expansion */
10199
10200 DIR *dirp;
10201 struct dirent *dp;
10202
10203 /* Expanding "**" may take a long time, check for CTRL-C. */
10204 if (stardepth > 0)
10205 {
10206 ui_breakcheck();
10207 if (got_int)
10208 return 0;
10209 }
10210
10211 /* make room for file name */
10212 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10213 if (buf == NULL)
10214 return 0;
10215
10216 /*
10217 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010218 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010219 * Copy it into "buf", including the preceding characters.
10220 */
10221 p = buf;
10222 s = buf;
10223 e = NULL;
10224 path_end = path;
10225 while (*path_end != NUL)
10226 {
10227 /* May ignore a wildcard that has a backslash before it; it will
10228 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10229 if (path_end >= path + wildoff && rem_backslash(path_end))
10230 *p++ = *path_end++;
10231 else if (*path_end == '/')
10232 {
10233 if (e != NULL)
10234 break;
10235 s = p + 1;
10236 }
10237 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010238 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010239 || (!p_fic && (flags & EW_ICASE)
10240 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010241 e = p;
10242#ifdef FEAT_MBYTE
10243 if (has_mbyte)
10244 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010245 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010246 STRNCPY(p, path_end, len);
10247 p += len;
10248 path_end += len;
10249 }
10250 else
10251#endif
10252 *p++ = *path_end++;
10253 }
10254 e = p;
10255 *e = NUL;
10256
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010257 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010258 /* Remove backslashes between "wildoff" and the start of the wildcard
10259 * component. */
10260 for (p = buf + wildoff; p < s; ++p)
10261 if (rem_backslash(p))
10262 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010263 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010264 --e;
10265 --s;
10266 }
10267
10268 /* Check for "**" between "s" and "e". */
10269 for (p = s; p < e; ++p)
10270 if (p[0] == '*' && p[1] == '*')
10271 starstar = TRUE;
10272
10273 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010274 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010275 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10276 if (pat == NULL)
10277 {
10278 vim_free(buf);
10279 return 0;
10280 }
10281
10282 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010283 if (flags & EW_ICASE)
10284 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10285 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010286 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010287 if (flags & (EW_NOERROR | EW_NOTWILD))
10288 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010289 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
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 vim_free(pat);
10293
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010294 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010295 {
10296 vim_free(buf);
10297 return 0;
10298 }
10299
10300 /* If "**" is by itself, this is the first time we encounter it and more
10301 * is following then find matches without any directory. */
10302 if (!didstar && stardepth < 100 && starstar && e - s == 2
10303 && *path_end == '/')
10304 {
10305 STRCPY(s, path_end + 1);
10306 ++stardepth;
10307 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10308 --stardepth;
10309 }
10310
10311 /* open the directory for scanning */
10312 *s = NUL;
10313 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10314
10315 /* Find all matching entries */
10316 if (dirp != NULL)
10317 {
10318 for (;;)
10319 {
10320 dp = readdir(dirp);
10321 if (dp == NULL)
10322 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010323 if ((dp->d_name[0] != '.' || starts_with_dot
10324 || ((flags & EW_DODOT)
10325 && dp->d_name[1] != NUL
10326 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010327 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10328 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010329 || ((flags & EW_NOTWILD)
10330 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010331 {
10332 STRCPY(s, dp->d_name);
10333 len = STRLEN(buf);
10334
10335 if (starstar && stardepth < 100)
10336 {
10337 /* For "**" in the pattern first go deeper in the tree to
10338 * find matches. */
10339 STRCPY(buf + len, "/**");
10340 STRCPY(buf + len + 3, path_end);
10341 ++stardepth;
10342 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10343 --stardepth;
10344 }
10345
10346 STRCPY(buf + len, path_end);
10347 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10348 {
10349 /* need to expand another component of the path */
10350 /* remove backslashes for the remaining components only */
10351 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10352 }
10353 else
10354 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010355 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010356
Bram Moolenaar231334e2005-07-25 20:46:57 +000010357 /* no more wildcards, check if there is a match */
10358 /* remove backslashes for the remaining components only */
10359 if (*path_end != NUL)
10360 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010361 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010362 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010363 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010364 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010365#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010366 size_t precomp_len = STRLEN(buf)+1;
10367 char_u *precomp_buf =
10368 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010369
Bram Moolenaar231334e2005-07-25 20:46:57 +000010370 if (precomp_buf)
10371 {
10372 mch_memmove(buf, precomp_buf, precomp_len);
10373 vim_free(precomp_buf);
10374 }
10375#endif
10376 addfile(gap, buf, flags);
10377 }
10378 }
10379 }
10380 }
10381
10382 closedir(dirp);
10383 }
10384
10385 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010386 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010387
10388 matches = gap->ga_len - start_len;
10389 if (matches > 0)
10390 qsort(((char_u **)gap->ga_data) + start_len, matches,
10391 sizeof(char_u *), pstrcmp);
10392 return matches;
10393}
10394#endif
10395
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010396#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010397static int find_previous_pathsep(char_u *path, char_u **psep);
10398static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10399static void expand_path_option(char_u *curdir, garray_T *gap);
10400static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10401static void uniquefy_paths(garray_T *gap, char_u *pattern);
10402static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010403
10404/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010405 * Moves "*psep" back to the previous path separator in "path".
10406 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010407 */
10408 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010409find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010410{
10411 /* skip the current separator */
10412 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010413 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010414
10415 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010416 while (*psep > path)
10417 {
10418 if (vim_ispathsep(**psep))
10419 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010420 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010421 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010422
10423 return FAIL;
10424}
10425
10426/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010427 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10428 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429 */
10430 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010431is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010432{
10433 int j;
10434 int candidate_len;
10435 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010436 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010437 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010438
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010439 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010440 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010441 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010442 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010443
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010444 candidate_len = (int)STRLEN(maybe_unique);
10445 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010446 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010447 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010448
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010449 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010450 if (fnamecmp(maybe_unique, rival) == 0
10451 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010452 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010453 }
10454
Bram Moolenaar162bd912010-07-28 22:29:10 +020010455 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010456}
10457
10458/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010459 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010460 * paths are expanded to their equivalent fullpath. This includes the "."
10461 * (relative to current buffer directory) and empty path (relative to current
10462 * directory) notations.
10463 *
10464 * TODO: handle upward search (;) and path limiter (**N) notations by
10465 * expanding each into their equivalent path(s).
10466 */
10467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010468expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010469{
10470 char_u *path_option = *curbuf->b_p_path == NUL
10471 ? p_path : curbuf->b_p_path;
10472 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010473 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010474 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010475
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010476 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010477 return;
10478
10479 while (*path_option != NUL)
10480 {
10481 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10482
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010483 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010484 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010485 /* Relative to current buffer:
10486 * "/path/file" + "." -> "/path/"
10487 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010488 if (curbuf->b_ffname == NULL)
10489 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010490 p = gettail(curbuf->b_ffname);
10491 len = (int)(p - curbuf->b_ffname);
10492 if (len + (int)STRLEN(buf) >= MAXPATHL)
10493 continue;
10494 if (buf[1] == NUL)
10495 buf[len] = NUL;
10496 else
10497 STRMOVE(buf + len, buf + 2);
10498 mch_memmove(buf, curbuf->b_ffname, len);
10499 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010500 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010501 else if (buf[0] == NUL)
10502 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010503 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010504 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010505 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010506 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010507 else if (!mch_isFullName(buf))
10508 {
10509 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010510 len = (int)STRLEN(curdir);
10511 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010512 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010513 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010514 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010515 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010516 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010517 }
10518
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010519 if (ga_grow(gap, 1) == FAIL)
10520 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010521
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010522# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010523 /* Avoid the path ending in a backslash, it fails when a comma is
10524 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010525 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010526 if (buf[len - 1] == '\\')
10527 buf[len - 1] = '/';
10528# endif
10529
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010530 p = vim_strsave(buf);
10531 if (p == NULL)
10532 break;
10533 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010534 }
10535
10536 vim_free(buf);
10537}
10538
10539/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010540 * Returns a pointer to the file or directory name in "fname" that matches the
10541 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010542 *
10543 * path: /foo/bar/baz
10544 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010545 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010546 */
10547 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010548get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010549{
10550 int i;
10551 int maxlen = 0;
10552 char_u **path_part = (char_u **)gap->ga_data;
10553 char_u *cutoff = NULL;
10554
10555 for (i = 0; i < gap->ga_len; i++)
10556 {
10557 int j = 0;
10558
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010559 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010560# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010561 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10562#endif
10563 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010564 j++;
10565 if (j > maxlen)
10566 {
10567 maxlen = j;
10568 cutoff = &fname[j];
10569 }
10570 }
10571
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010572 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010573 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010574 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010575 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010576
10577 return cutoff;
10578}
10579
10580/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010581 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10582 * that they are unique with respect to each other while conserving the part
10583 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010584 */
10585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010586uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010587{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010588 int i;
10589 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010590 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010591 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010592 char_u *pat;
10593 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010594 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010596 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010597 char_u **in_curdir = NULL;
10598 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010599
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010600 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010601 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010602
10603 /*
10604 * We need to prepend a '*' at the beginning of file_pattern so that the
10605 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010606 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010607 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010608 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010609 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010610 if (file_pattern == NULL)
10611 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010612 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010613 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010614 STRCAT(file_pattern, pattern);
10615 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10616 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010617 if (pat == NULL)
10618 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010619
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010620 regmatch.rm_ic = TRUE; /* always ignore case */
10621 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10622 vim_free(pat);
10623 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010624 return;
10625
Bram Moolenaar162bd912010-07-28 22:29:10 +020010626 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010627 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010628 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010629 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010630
10631 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010632 if (in_curdir == NULL)
10633 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010634
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010635 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010636 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010637 char_u *path = fnames[i];
10638 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010639 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010640 char_u *pathsep_p;
10641 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010642
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010643 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010644 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010645 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010646 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010647 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010648
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010649 /* Shorten the filename while maintaining its uniqueness */
10650 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010651
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010652 /* Don't assume all files can be reached without path when search
10653 * pattern starts with star star slash, so only remove path_cutoff
10654 * when possible. */
10655 if (pattern[0] == '*' && pattern[1] == '*'
10656 && vim_ispathsep_nocolon(pattern[2])
10657 && path_cutoff != NULL
10658 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10659 && is_unique(path_cutoff, gap, i))
10660 {
10661 sort_again = TRUE;
10662 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10663 }
10664 else
10665 {
10666 /* Here all files can be reached without path, so get shortest
10667 * unique path. We start at the end of the path. */
10668 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010669
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010670 while (find_previous_pathsep(path, &pathsep_p))
10671 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10672 && is_unique(pathsep_p + 1, gap, i)
10673 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10674 {
10675 sort_again = TRUE;
10676 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10677 break;
10678 }
10679 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010680
10681 if (mch_isFullName(path))
10682 {
10683 /*
10684 * Last resort: shorten relative to curdir if possible.
10685 * 'possible' means:
10686 * 1. It is under the current directory.
10687 * 2. The result is actually shorter than the original.
10688 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010689 * Before curdir After
10690 * /foo/bar/file.txt /foo/bar ./file.txt
10691 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10692 * /file.txt / /file.txt
10693 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010694 */
10695 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010696 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010697#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010698 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010699 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010700 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010701 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010702 && !vim_ispathsep(*short_name)
10703#endif
10704 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010705 {
10706 STRCPY(path, ".");
10707 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010708 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010709 }
10710 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010711 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010712 }
10713
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010714 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010715 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010716 {
10717 char_u *rel_path;
10718 char_u *path = in_curdir[i];
10719
10720 if (path == NULL)
10721 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010722
10723 /* If the {filename} is not unique, change it to ./{filename}.
10724 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010725 short_name = shorten_fname(path, curdir);
10726 if (short_name == NULL)
10727 short_name = path;
10728 if (is_unique(short_name, gap, i))
10729 {
10730 STRCPY(fnames[i], short_name);
10731 continue;
10732 }
10733
10734 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10735 if (rel_path == NULL)
10736 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010737 STRCPY(rel_path, ".");
10738 add_pathsep(rel_path);
10739 STRCAT(rel_path, short_name);
10740
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010741 vim_free(fnames[i]);
10742 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010743 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010744 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010745 }
10746
Bram Moolenaar162bd912010-07-28 22:29:10 +020010747theend:
10748 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010749 if (in_curdir != NULL)
10750 {
10751 for (i = 0; i < gap->ga_len; i++)
10752 vim_free(in_curdir[i]);
10753 vim_free(in_curdir);
10754 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010755 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010756 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010757
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010758 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010759 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010760}
10761
10762/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010763 * Calls globpath() with 'path' values for the given pattern and stores the
10764 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010765 * Returns the total number of matches.
10766 */
10767 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010768expand_in_path(
10769 garray_T *gap,
10770 char_u *pattern,
10771 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010772{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010773 char_u *curdir;
10774 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010775 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010776
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010777 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010778 return 0;
10779 mch_dirname(curdir, MAXPATHL);
10780
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010781 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010782 expand_path_option(curdir, &path_ga);
10783 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010784 if (path_ga.ga_len == 0)
10785 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010786
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010787 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010788 ga_clear_strings(&path_ga);
10789 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010790 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010791
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010792 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010793 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010794
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010795 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010796}
10797#endif
10798
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010799#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10800/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010801 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10802 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010803 */
10804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010805remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010806{
10807 int i;
10808 int j;
10809 char_u **fnames = (char_u **)gap->ga_data;
10810
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010811 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010812 for (i = gap->ga_len - 1; i > 0; --i)
10813 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10814 {
10815 vim_free(fnames[i]);
10816 for (j = i + 1; j < gap->ga_len; ++j)
10817 fnames[j - 1] = fnames[j];
10818 --gap->ga_len;
10819 }
10820}
10821#endif
10822
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010823static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010824
10825/*
10826 * Return TRUE if "p" contains what looks like an environment variable.
10827 * Allowing for escaping.
10828 */
10829 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010830has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010831{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010832 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010833 {
10834 if (*p == '\\' && p[1] != NUL)
10835 ++p;
10836 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010837#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010838 "$%"
10839#else
10840 "$"
10841#endif
10842 , *p) != NULL)
10843 return TRUE;
10844 }
10845 return FALSE;
10846}
10847
10848#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010849static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010850
10851/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010852 * Return TRUE if "p" contains a special wildcard character, one that Vim
10853 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010854 */
10855 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010856has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010857{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010858 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010859 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010860 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010861 if (*p == '\\' && p[1] != NUL)
10862 ++p;
10863 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10864 return TRUE;
10865 }
10866 return FALSE;
10867}
10868#endif
10869
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870/*
10871 * Generic wildcard expansion code.
10872 *
10873 * Characters in "pat" that should not be expanded must be preceded with a
10874 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10875 *
10876 * Return FAIL when no single file was found. In this case "num_file" is not
10877 * set, and "file" may contain an error message.
10878 * Return OK when some files found. "num_file" is set to the number of
10879 * matches, "file" to the array of matches. Call FreeWild() later.
10880 */
10881 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010882gen_expand_wildcards(
10883 int num_pat, /* number of input patterns */
10884 char_u **pat, /* array of input patterns */
10885 int *num_file, /* resulting number of files */
10886 char_u ***file, /* array of resulting files */
10887 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
10889 int i;
10890 garray_T ga;
10891 char_u *p;
10892 static int recursive = FALSE;
10893 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010894 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010895#if defined(FEAT_SEARCHPATH)
10896 int did_expand_in_path = FALSE;
10897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898
10899 /*
10900 * expand_env() is called to expand things like "~user". If this fails,
10901 * it calls ExpandOne(), which brings us back here. In this case, always
10902 * call the machine specific expansion function, if possible. Otherwise,
10903 * return FAIL.
10904 */
10905 if (recursive)
10906#ifdef SPECIAL_WILDCHAR
10907 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10908#else
10909 return FAIL;
10910#endif
10911
10912#ifdef SPECIAL_WILDCHAR
10913 /*
10914 * If there are any special wildcard characters which we cannot handle
10915 * here, call machine specific function for all the expansion. This
10916 * avoids starting the shell for each argument separately.
10917 * For `=expr` do use the internal function.
10918 */
10919 for (i = 0; i < num_pat; i++)
10920 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010921 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922# ifdef VIM_BACKTICK
10923 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10924# endif
10925 )
10926 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10927 }
10928#endif
10929
10930 recursive = TRUE;
10931
10932 /*
10933 * The matching file names are stored in a growarray. Init it empty.
10934 */
10935 ga_init2(&ga, (int)sizeof(char_u *), 30);
10936
10937 for (i = 0; i < num_pat; ++i)
10938 {
10939 add_pat = -1;
10940 p = pat[i];
10941
10942#ifdef VIM_BACKTICK
10943 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010946 if (add_pat == -1)
10947 retval = FAIL;
10948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 else
10950#endif
10951 {
10952 /*
10953 * First expand environment variables, "~/" and "~user/".
10954 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010955 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010957 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 if (p == NULL)
10959 p = pat[i];
10960#ifdef UNIX
10961 /*
10962 * On Unix, if expand_env() can't expand an environment
10963 * variable, use the shell to do that. Discard previously
10964 * found file names and start all over again.
10965 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010966 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 {
10968 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010969 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010971 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 recursive = FALSE;
10973 return i;
10974 }
10975#endif
10976 }
10977
10978 /*
10979 * If there are wildcards: Expand file names and add each match to
10980 * the list. If there is no match, and EW_NOTFOUND is given, add
10981 * the pattern.
10982 * If there are no wildcards: Add the file name if it exists or
10983 * when EW_NOTFOUND is given.
10984 */
10985 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010986 {
10987#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010988 if ((flags & EW_PATH)
10989 && !mch_isFullName(p)
10990 && !(p[0] == '.'
10991 && (vim_ispathsep(p[1])
10992 || (p[1] == '.' && vim_ispathsep(p[2]))))
10993 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010994 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010995 /* :find completion where 'path' is used.
10996 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010997 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010998 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010999 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011000 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011001 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011002 else
11003#endif
11004 add_pat = mch_expandpath(&ga, p, flags);
11005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 }
11007
11008 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11009 {
11010 char_u *t = backslash_halve_save(p);
11011
11012#if defined(MACOS_CLASSIC)
11013 slash_to_colon(t);
11014#endif
11015 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11016 * "vim c:/" work. */
11017 if (flags & EW_NOTFOUND)
11018 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 addfile(&ga, t, flags);
11021 vim_free(t);
11022 }
11023
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011024#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011025 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011026 uniquefy_paths(&ga, p);
11027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 if (p != pat[i])
11029 vim_free(p);
11030 }
11031
11032 *num_file = ga.ga_len;
11033 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11034
11035 recursive = FALSE;
11036
Bram Moolenaar336bd622016-01-17 18:23:58 +010011037 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038}
11039
11040# ifdef VIM_BACKTICK
11041
11042/*
11043 * Return TRUE if we can expand this backtick thing here.
11044 */
11045 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011046vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047{
11048 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11049}
11050
11051/*
11052 * Expand an item in `backticks` by executing it as a command.
11053 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011054 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 */
11056 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011057expand_backtick(
11058 garray_T *gap,
11059 char_u *pat,
11060 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062 char_u *p;
11063 char_u *cmd;
11064 char_u *buffer;
11065 int cnt = 0;
11066 int i;
11067
11068 /* Create the command: lop off the backticks. */
11069 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11070 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011071 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072
11073#ifdef FEAT_EVAL
11074 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011075 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 else
11077#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011078 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011079 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 vim_free(cmd);
11081 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011082 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083
11084 cmd = buffer;
11085 while (*cmd != NUL)
11086 {
11087 cmd = skipwhite(cmd); /* skip over white space */
11088 p = cmd;
11089 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11090 ++p;
11091 /* add an entry if it is not empty */
11092 if (p > cmd)
11093 {
11094 i = *p;
11095 *p = NUL;
11096 addfile(gap, cmd, flags);
11097 *p = i;
11098 ++cnt;
11099 }
11100 cmd = p;
11101 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11102 ++cmd;
11103 }
11104
11105 vim_free(buffer);
11106 return cnt;
11107}
11108# endif /* VIM_BACKTICK */
11109
11110/*
11111 * Add a file to a file list. Accepted flags:
11112 * EW_DIR add directories
11113 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011114 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 * EW_NOTFOUND add even when it doesn't exist
11116 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011117 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 */
11119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011120addfile(
11121 garray_T *gap,
11122 char_u *f, /* filename */
11123 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124{
11125 char_u *p;
11126 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011127 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011129 /* if the file/dir/link doesn't exist, may not add it */
11130 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011131 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 return;
11133
11134#ifdef FNAME_ILLEGAL
11135 /* if the file/dir contains illegal characters, don't add it */
11136 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11137 return;
11138#endif
11139
11140 isdir = mch_isdir(f);
11141 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11142 return;
11143
Bram Moolenaarb5971142015-03-21 17:32:19 +010011144 /* If the file isn't executable, may not add it. Do accept directories.
11145 * When invoked from expand_shellcmd() do not use $PATH. */
11146 if (!isdir && (flags & EW_EXEC)
11147 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011148 return;
11149
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150 /* Make room for another item in the file list. */
11151 if (ga_grow(gap, 1) == FAIL)
11152 return;
11153
11154 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11155 if (p == NULL)
11156 return;
11157
11158 STRCPY(p, f);
11159#ifdef BACKSLASH_IN_FILENAME
11160 slash_adjust(p);
11161#endif
11162 /*
11163 * Append a slash or backslash after directory names if none is present.
11164 */
11165#ifndef DONT_ADD_PATHSEP_TO_DIR
11166 if (isdir && (flags & EW_ADDSLASH))
11167 add_pathsep(p);
11168#endif
11169 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170}
11171#endif /* !NO_EXPANDPATH */
11172
11173#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11174
11175#ifndef SEEK_SET
11176# define SEEK_SET 0
11177#endif
11178#ifndef SEEK_END
11179# define SEEK_END 2
11180#endif
11181
11182/*
11183 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011184 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11185 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 * Returns an allocated string, or NULL for error.
11187 */
11188 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011189get_cmd_output(
11190 char_u *cmd,
11191 char_u *infile, /* optional input file name */
11192 int flags, /* can be SHELL_SILENT */
11193 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194{
11195 char_u *tempname;
11196 char_u *command;
11197 char_u *buffer = NULL;
11198 int len;
11199 int i = 0;
11200 FILE *fd;
11201
11202 if (check_restricted() || check_secure())
11203 return NULL;
11204
11205 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011206 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 {
11208 EMSG(_(e_notmp));
11209 return NULL;
11210 }
11211
11212 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011213 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 if (command == NULL)
11215 goto done;
11216
11217 /*
11218 * Call the shell to execute the command (errors are ignored).
11219 * Don't check timestamps here.
11220 */
11221 ++no_check_timestamps;
11222 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11223 --no_check_timestamps;
11224
11225 vim_free(command);
11226
11227 /*
11228 * read the names from the file into memory
11229 */
11230# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011231 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 fd = mch_fopen((char *)tempname, "r");
11233# else
11234 fd = mch_fopen((char *)tempname, READBIN);
11235# endif
11236
11237 if (fd == NULL)
11238 {
11239 EMSG2(_(e_notopen), tempname);
11240 goto done;
11241 }
11242
11243 fseek(fd, 0L, SEEK_END);
11244 len = ftell(fd); /* get size of temp file */
11245 fseek(fd, 0L, SEEK_SET);
11246
11247 buffer = alloc(len + 1);
11248 if (buffer != NULL)
11249 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11250 fclose(fd);
11251 mch_remove(tempname);
11252 if (buffer == NULL)
11253 goto done;
11254#ifdef VMS
11255 len = i; /* VMS doesn't give us what we asked for... */
11256#endif
11257 if (i != len)
11258 {
11259 EMSG2(_(e_notread), tempname);
11260 vim_free(buffer);
11261 buffer = NULL;
11262 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011263 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011264 {
11265 /* Change NUL into SOH, otherwise the string is truncated. */
11266 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011267 if (buffer[i] == NUL)
11268 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011269
Bram Moolenaar162bd912010-07-28 22:29:10 +020011270 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011271 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011272 else
11273 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274
11275done:
11276 vim_free(tempname);
11277 return buffer;
11278}
11279#endif
11280
11281/*
11282 * Free the list of files returned by expand_wildcards() or other expansion
11283 * functions.
11284 */
11285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011286FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011288 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 while (count--)
11291 vim_free(files[count]);
11292 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293}
11294
11295/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011296 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 * Don't do this when still processing a command or a mapping.
11298 * Don't do this when inside a ":normal" command.
11299 */
11300 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011301goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
11303 return (p_im && stuff_empty() && typebuf_typed());
11304}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011305
11306/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011307 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011308 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11309 * - Remove any argument. E.g., "csh -f" -> "csh".
11310 * But don't allow a space in the path, so that this works:
11311 * "/usr/bin/csh --rcfile ~/.cshrc"
11312 * But don't do that for Windows, it's common to have a space in the path.
11313 */
11314 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011315get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011316{
11317 char_u *p;
11318
11319#ifdef WIN3264
11320 p = gettail(p_sh);
11321 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11322#else
11323 p = skiptowhite(p_sh);
11324 if (*p == NUL)
11325 {
11326 /* No white space, use the tail. */
11327 p = vim_strsave(gettail(p_sh));
11328 }
11329 else
11330 {
11331 char_u *p1, *p2;
11332
11333 /* Find the last path separator before the space. */
11334 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011335 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011336 if (vim_ispathsep(*p2))
11337 p1 = p2 + 1;
11338 p = vim_strnsave(p1, (int)(p - p1));
11339 }
11340#endif
11341 return p;
11342}