blob: 2d635d677faafe35b1764d492388fd62f8a2d691 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
Bram Moolenaar597a4222014-06-25 14:39:50 +020044 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
Bram Moolenaar597a4222014-06-25 14:39:50 +020055 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_str(
65 char_u *ptr,
66 int ts,
67 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 if (*ptr == TAB)
74 {
75 if (!list || lcs_tab1) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020078 /* In list mode, when tab is not set, count screen char width
79 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020080 count += ptr2cells(ptr);
81 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else if (*ptr == ' ')
83 ++count; /* count a space for one */
84 else
85 break;
86 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000088}
89
90/*
91 * Set the indent of the current line.
92 * Leaves the cursor on the first non-blank in the line.
93 * Caller must take care of undo.
94 * "flags":
95 * SIN_CHANGED: call changed_bytes() if the line was changed.
96 * SIN_INSERT: insert the indent in front of the line.
97 * SIN_UNDO: save line for undo before changing it.
98 * Returns TRUE if the line was changed.
99 */
100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100101set_indent(
102 int size, /* measured in spaces */
103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 char_u *p;
106 char_u *newline;
107 char_u *oldline;
108 char_u *s;
109 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 int line_len;
112 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000115 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000116 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119 /*
120 * First check if there is anything to do and compute the number of
121 * characters needed for the indent.
122 */
123 todo = size;
124 ind_len = 0;
125 p = oldline = ml_get_curline();
126
127 /* Calculate the buffer size for the new indent, and check to see if it
128 * isn't already set */
129
Bram Moolenaar5002c292007-07-24 13:26:15 +0000130 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
131 * 'preserveindent' are set count the number of characters at the
132 * beginning of the line to be copied */
133 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 /* If 'preserveindent' is set then reuse as much as possible of
136 * the existing indent structure for the new indent */
137 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
138 {
139 ind_done = 0;
140
141 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100142 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {
144 if (*p == TAB)
145 {
146 tab_pad = (int)curbuf->b_p_ts
147 - (ind_done % (int)curbuf->b_p_ts);
148 /* stop if this tab will overshoot the target */
149 if (todo < tab_pad)
150 break;
151 todo -= tab_pad;
152 ++ind_len;
153 ind_done += tab_pad;
154 }
155 else
156 {
157 --todo;
158 ++ind_len;
159 ++ind_done;
160 }
161 ++p;
162 }
163
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 /* Set initial number of whitespace chars to copy if we are
165 * preserving indent but expandtab is set */
166 if (curbuf->b_p_et)
167 orig_char_len = ind_len;
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* Fill to next tabstop with a tab, if possible */
170 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000171 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 doit = TRUE;
174 todo -= tab_pad;
175 ++ind_len;
176 /* ind_done += tab_pad; */
177 }
178 }
179
180 /* count tabs required for indent */
181 while (todo >= (int)curbuf->b_p_ts)
182 {
183 if (*p != TAB)
184 doit = TRUE;
185 else
186 ++p;
187 todo -= (int)curbuf->b_p_ts;
188 ++ind_len;
189 /* ind_done += (int)curbuf->b_p_ts; */
190 }
191 }
192 /* count spaces required for indent */
193 while (todo > 0)
194 {
195 if (*p != ' ')
196 doit = TRUE;
197 else
198 ++p;
199 --todo;
200 ++ind_len;
201 /* ++ind_done; */
202 }
203
204 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100205 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 return FALSE;
207
208 /* Allocate memory for the new line. */
209 if (flags & SIN_INSERT)
210 p = oldline;
211 else
212 p = skipwhite(p);
213 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214
215 /* If 'preserveindent' and 'expandtab' are both set keep the original
216 * characters and allocate accordingly. We will fill the rest with spaces
217 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 {
220 newline = alloc(orig_char_len + size - ind_done + line_len);
221 if (newline == NULL)
222 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000223 todo = size - ind_done;
224 ind_len = orig_char_len + todo; /* Set total length of indent in
225 * characters, which may have been
226 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 p = oldline;
228 s = newline;
229 while (orig_char_len > 0)
230 {
231 *s++ = *p++;
232 orig_char_len--;
233 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 /* Skip over any additional white space (useful when newindent is less
236 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100237 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000239
Bram Moolenaar5002c292007-07-24 13:26:15 +0000240 }
241 else
242 {
243 todo = size;
244 newline = alloc(ind_len + line_len);
245 if (newline == NULL)
246 return FALSE;
247 s = newline;
248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* if 'expandtab' isn't set: use TABs */
252 if (!curbuf->b_p_et)
253 {
254 /* If 'preserveindent' is set then reuse as much as possible of
255 * the existing indent structure for the new indent */
256 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
257 {
258 p = oldline;
259 ind_done = 0;
260
Bram Moolenaar1c465442017-03-12 20:10:05 +0100261 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 {
263 if (*p == TAB)
264 {
265 tab_pad = (int)curbuf->b_p_ts
266 - (ind_done % (int)curbuf->b_p_ts);
267 /* stop if this tab will overshoot the target */
268 if (todo < tab_pad)
269 break;
270 todo -= tab_pad;
271 ind_done += tab_pad;
272 }
273 else
274 {
275 --todo;
276 ++ind_done;
277 }
278 *s++ = *p++;
279 }
280
281 /* Fill to next tabstop with a tab, if possible */
282 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
283 if (todo >= tab_pad)
284 {
285 *s++ = TAB;
286 todo -= tab_pad;
287 }
288
289 p = skipwhite(p);
290 }
291
292 while (todo >= (int)curbuf->b_p_ts)
293 {
294 *s++ = TAB;
295 todo -= (int)curbuf->b_p_ts;
296 }
297 }
298 while (todo > 0)
299 {
300 *s++ = ' ';
301 --todo;
302 }
303 mch_memmove(s, p, (size_t)line_len);
304
305 /* Replace the line (unless undo fails). */
306 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
307 {
308 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
309 if (flags & SIN_CHANGED)
310 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200311 /* Correct saved cursor position if it is in this line. */
312 if (saved_cursor.lnum == curwin->w_cursor.lnum)
313 {
314 if (saved_cursor.col >= (colnr_T)(p - oldline))
315 /* cursor was after the indent, adjust for the number of
316 * bytes added/removed */
317 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
318 else if (saved_cursor.col >= (colnr_T)(s - newline))
319 /* cursor was in the indent, and is now after it, put it back
320 * at the start of the indent (replacing spaces with TAB) */
321 saved_cursor.col = (colnr_T)(s - newline);
322 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000323 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 else
326 vim_free(newline);
327
328 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000329 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Copy the indent from ptr to the current line (and fill to size)
334 * Leaves the cursor on the first non-blank in the line.
335 * Returns TRUE if the line was changed.
336 */
337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 char_u *p = NULL;
341 char_u *line = NULL;
342 char_u *s;
343 int todo;
344 int ind_len;
345 int line_len = 0;
346 int tab_pad;
347 int ind_done;
348 int round;
349
350 /* Round 1: compute the number of characters needed for the indent
351 * Round 2: copy the characters. */
352 for (round = 1; round <= 2; ++round)
353 {
354 todo = size;
355 ind_len = 0;
356 ind_done = 0;
357 s = src;
358
359 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100360 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
362 if (*s == TAB)
363 {
364 tab_pad = (int)curbuf->b_p_ts
365 - (ind_done % (int)curbuf->b_p_ts);
366 /* Stop if this tab will overshoot the target */
367 if (todo < tab_pad)
368 break;
369 todo -= tab_pad;
370 ind_done += tab_pad;
371 }
372 else
373 {
374 --todo;
375 ++ind_done;
376 }
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = *s;
380 ++s;
381 }
382
383 /* Fill to next tabstop with a tab, if possible */
384 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200385 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
387 todo -= tab_pad;
388 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000389 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 *p++ = TAB;
391 }
392
393 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200394 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 todo -= (int)curbuf->b_p_ts;
397 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 *p++ = TAB;
400 }
401
402 /* Count/add spaces required for indent */
403 while (todo > 0)
404 {
405 --todo;
406 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000407 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 *p++ = ' ';
409 }
410
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 /* Allocate memory for the result: the copied indent, new indent
414 * and the rest of the line. */
415 line_len = (int)STRLEN(ml_get_curline()) + 1;
416 line = alloc(ind_len + line_len);
417 if (line == NULL)
418 return FALSE;
419 p = line;
420 }
421 }
422
423 /* Append the original line */
424 mch_memmove(p, ml_get_curline(), (size_t)line_len);
425
426 /* Replace the line */
427 ml_replace(curwin->w_cursor.lnum, line, FALSE);
428
429 /* Put the cursor after the indent. */
430 curwin->w_cursor.col = ind_len;
431 return TRUE;
432}
433
434/*
435 * Return the indent of the current line after a number. Return -1 if no
436 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 colnr_T col;
443 pos_T pos;
444
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch_T regmatch;
446 int lead_len = 0; /* length of comment leader */
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (lnum > curbuf->b_ml.ml_line_count)
449 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000450 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
452#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
454 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
458 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 /* vim_regexec() expects a pointer to a line. This lets us
463 * start matching for the flp beyond any comment leader... */
464 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 pos.lnum = lnum;
467 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200472 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000474
475 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 getvcol(curwin, &pos, &col, NULL, NULL);
478 return (int)col;
479}
480
Bram Moolenaar597a4222014-06-25 14:39:50 +0200481#if defined(FEAT_LINEBREAK) || defined(PROTO)
482/*
483 * Return appropriate space number for breakindent, taking influencing
484 * parameters into account. Window must be specified, since it is not
485 * necessarily always the current one.
486 */
487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488get_breakindent_win(
489 win_T *wp,
490 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491{
492 static int prev_indent = 0; /* cached indent value */
493 static long prev_ts = 0L; /* cached tabstop value */
494 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100495 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496 int bri = 0;
497 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200498 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100505 || prev_tick != CHANGEDTICK(wp->w_buffer))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200511 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200513 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514
515 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 if (wp->w_p_brisbr)
517 bri -= vim_strsize(p_sbr);
518
519 /* Add offset for number column, if 'n' is in 'cpoptions' */
520 bri += win_col_off2(wp);
521
522 /* never indent past left window margin */
523 if (bri < 0)
524 bri = 0;
525 /* always leave at least bri_min characters on the left,
526 * if text width is sufficient */
527 else if (bri > eff_wwidth - wp->w_p_brimin)
528 bri = (eff_wwidth - wp->w_p_brimin < 0)
529 ? 0 : eff_wwidth - wp->w_p_brimin;
530
531 return bri;
532}
533#endif
534
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
537
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100538static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
541 * Return TRUE if the string "line" starts with a word from 'cinwords'.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *cinw;
547 char_u *cinw_buf;
548 int cinw_len;
549 int retval = FALSE;
550 int len;
551
552 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
553 cinw_buf = alloc((unsigned)cinw_len);
554 if (cinw_buf != NULL)
555 {
556 line = skipwhite(line);
557 for (cinw = curbuf->b_p_cinw; *cinw; )
558 {
559 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
560 if (STRNCMP(line, cinw_buf, len) == 0
561 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
562 {
563 retval = TRUE;
564 break;
565 }
566 }
567 vim_free(cinw_buf);
568 }
569 return retval;
570}
571#endif
572
573/*
574 * open_line: Add a new line below or above the current line.
575 *
576 * For VREPLACE mode, we only add a new line when we get to the end of the
577 * file, otherwise we just start replacing the next line.
578 *
579 * Caller must take care of undo. Since VREPLACE may affect any number of
580 * lines however, it may call u_save_cursor() again when starting to change a
581 * new line.
582 * "flags": OPENLINE_DELSPACES delete spaces after cursor
583 * OPENLINE_DO_COM format comments
584 * OPENLINE_KEEPTRAIL keep trailing spaces
585 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200586 * OPENLINE_COM_LIST format comments with list or 2nd line indent
587 *
588 * "second_line_indent": indent for after ^^D in Insert mode or if flag
589 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *
591 * Return TRUE for success, FALSE for failure
592 */
593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594open_line(
595 int dir, /* FORWARD or BACKWARD */
596 int flags,
597 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 char_u *saved_line; /* copy of the original line */
600 char_u *next_line = NULL; /* copy of the next line */
601 char_u *p_extra = NULL; /* what goes to next line */
602 int less_cols = 0; /* less columns for mark in new line */
603 int less_cols_off = 0; /* columns to skip for mark adjust */
604 pos_T old_cursor; /* old cursor position */
605 int newcol = 0; /* new cursor column */
606 int newindent = 0; /* auto-indent of the new line */
607 int n;
608 int trunc_line = FALSE; /* truncate current line afterwards */
609 int retval = FALSE; /* return value, default is FAIL */
610#ifdef FEAT_COMMENTS
611 int extra_len = 0; /* length of p_extra string */
612 int lead_len; /* length of comment leader */
613 char_u *lead_flags; /* position in 'comments' for comment leader */
614 char_u *leader = NULL; /* copy of comment leader */
615#endif
616 char_u *allocated = NULL; /* allocated memory */
617#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
618 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
619 char_u *p;
620#endif
621 int saved_char = NUL; /* init for GCC */
622#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
623 pos_T *pos;
624#endif
625#ifdef FEAT_SMARTINDENT
626 int do_si = (!p_paste && curbuf->b_p_si
627# ifdef FEAT_CINDENT
628 && !curbuf->b_p_cin
629# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200630# ifdef FEAT_EVAL
631 && *curbuf->b_p_inde == NUL
632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 );
634 int no_si = FALSE; /* reset did_si afterwards */
635 int first_char = NUL; /* init for GCC */
636#endif
637#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
638 int vreplace_mode;
639#endif
640 int did_append; /* appended a new line */
641 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
642
643 /*
644 * make a copy of the current line so we can mess with it
645 */
646 saved_line = vim_strsave(ml_get_curline());
647 if (saved_line == NULL) /* out of memory! */
648 return FALSE;
649
650#ifdef FEAT_VREPLACE
651 if (State & VREPLACE_FLAG)
652 {
653 /*
654 * With VREPLACE we make a copy of the next line, which we will be
655 * starting to replace. First make the new line empty and let vim play
656 * with the indenting and comment leader to its heart's content. Then
657 * we grab what it ended up putting on the new line, put back the
658 * original line, and call ins_char() to put each new character onto
659 * the line, replacing what was there before and pushing the right
660 * stuff onto the replace stack. -- webb.
661 */
662 if (curwin->w_cursor.lnum < orig_line_count)
663 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
664 else
665 next_line = vim_strsave((char_u *)"");
666 if (next_line == NULL) /* out of memory! */
667 goto theend;
668
669 /*
670 * In VREPLACE mode, a NL replaces the rest of the line, and starts
671 * replacing the next line, so push all of the characters left on the
672 * line onto the replace stack. We'll push any other characters that
673 * might be replaced at the start of the next line (due to autoindent
674 * etc) a bit later.
675 */
676 replace_push(NUL); /* Call twice because BS over NL expects it */
677 replace_push(NUL);
678 p = saved_line + curwin->w_cursor.col;
679 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000680 {
681#ifdef FEAT_MBYTE
682 if (has_mbyte)
683 p += replace_push_mb(p);
684 else
685#endif
686 replace_push(*p++);
687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 saved_line[curwin->w_cursor.col] = NUL;
689 }
690#endif
691
692 if ((State & INSERT)
693#ifdef FEAT_VREPLACE
694 && !(State & VREPLACE_FLAG)
695#endif
696 )
697 {
698 p_extra = saved_line + curwin->w_cursor.col;
699#ifdef FEAT_SMARTINDENT
700 if (do_si) /* need first char after new line break */
701 {
702 p = skipwhite(p_extra);
703 first_char = *p;
704 }
705#endif
706#ifdef FEAT_COMMENTS
707 extra_len = (int)STRLEN(p_extra);
708#endif
709 saved_char = *p_extra;
710 *p_extra = NUL;
711 }
712
713 u_clearline(); /* cannot do "U" command when adding lines */
714#ifdef FEAT_SMARTINDENT
715 did_si = FALSE;
716#endif
717 ai_col = 0;
718
719 /*
720 * If we just did an auto-indent, then we didn't type anything on
721 * the prior line, and it should be truncated. Do this even if 'ai' is not
722 * set because automatically inserting a comment leader also sets did_ai.
723 */
724 if (dir == FORWARD && did_ai)
725 trunc_line = TRUE;
726
727 /*
728 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
729 * indent to use for the new line.
730 */
731 if (curbuf->b_p_ai
732#ifdef FEAT_SMARTINDENT
733 || do_si
734#endif
735 )
736 {
737 /*
738 * count white space on current line
739 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200740 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200741 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
742 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744#ifdef FEAT_SMARTINDENT
745 /*
746 * Do smart indenting.
747 * In insert/replace mode (only when dir == FORWARD)
748 * we may move some text to the next line. If it starts with '{'
749 * don't add an indent. Fixes inserting a NL before '{' in line
750 * "if (condition) {"
751 */
752 if (!trunc_line && do_si && *saved_line != NUL
753 && (p_extra == NULL || first_char != '{'))
754 {
755 char_u *ptr;
756 char_u last_char;
757
758 old_cursor = curwin->w_cursor;
759 ptr = saved_line;
760# ifdef FEAT_COMMENTS
761 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200762 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 else
764 lead_len = 0;
765# endif
766 if (dir == FORWARD)
767 {
768 /*
769 * Skip preprocessor directives, unless they are
770 * recognised as comments.
771 */
772 if (
773# ifdef FEAT_COMMENTS
774 lead_len == 0 &&
775# endif
776 ptr[0] == '#')
777 {
778 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
779 ptr = ml_get(--curwin->w_cursor.lnum);
780 newindent = get_indent();
781 }
782# ifdef FEAT_COMMENTS
783 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200784 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 else
786 lead_len = 0;
787 if (lead_len > 0)
788 {
789 /*
790 * This case gets the following right:
791 * \*
792 * * A comment (read '\' as '/').
793 * *\
794 * #define IN_THE_WAY
795 * This should line up here;
796 */
797 p = skipwhite(ptr);
798 if (p[0] == '/' && p[1] == '*')
799 p++;
800 if (p[0] == '*')
801 {
802 for (p++; *p; p++)
803 {
804 if (p[0] == '/' && p[-1] == '*')
805 {
806 /*
807 * End of C comment, indent should line up
808 * with the line containing the start of
809 * the comment
810 */
811 curwin->w_cursor.col = (colnr_T)(p - ptr);
812 if ((pos = findmatch(NULL, NUL)) != NULL)
813 {
814 curwin->w_cursor.lnum = pos->lnum;
815 newindent = get_indent();
816 }
817 }
818 }
819 }
820 }
821 else /* Not a comment line */
822# endif
823 {
824 /* Find last non-blank in line */
825 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100826 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 --p;
828 last_char = *p;
829
830 /*
831 * find the character just before the '{' or ';'
832 */
833 if (last_char == '{' || last_char == ';')
834 {
835 if (p > ptr)
836 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100837 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 --p;
839 }
840 /*
841 * Try to catch lines that are split over multiple
842 * lines. eg:
843 * if (condition &&
844 * condition) {
845 * Should line up here!
846 * }
847 */
848 if (*p == ')')
849 {
850 curwin->w_cursor.col = (colnr_T)(p - ptr);
851 if ((pos = findmatch(NULL, '(')) != NULL)
852 {
853 curwin->w_cursor.lnum = pos->lnum;
854 newindent = get_indent();
855 ptr = ml_get_curline();
856 }
857 }
858 /*
859 * If last character is '{' do indent, without
860 * checking for "if" and the like.
861 */
862 if (last_char == '{')
863 {
864 did_si = TRUE; /* do indent */
865 no_si = TRUE; /* don't delete it when '{' typed */
866 }
867 /*
868 * Look for "if" and the like, use 'cinwords'.
869 * Don't do this if the previous line ended in ';' or
870 * '}'.
871 */
872 else if (last_char != ';' && last_char != '}'
873 && cin_is_cinword(ptr))
874 did_si = TRUE;
875 }
876 }
877 else /* dir == BACKWARD */
878 {
879 /*
880 * Skip preprocessor directives, unless they are
881 * recognised as comments.
882 */
883 if (
884# ifdef FEAT_COMMENTS
885 lead_len == 0 &&
886# endif
887 ptr[0] == '#')
888 {
889 int was_backslashed = FALSE;
890
891 while ((ptr[0] == '#' || was_backslashed) &&
892 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
893 {
894 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
895 was_backslashed = TRUE;
896 else
897 was_backslashed = FALSE;
898 ptr = ml_get(++curwin->w_cursor.lnum);
899 }
900 if (was_backslashed)
901 newindent = 0; /* Got to end of file */
902 else
903 newindent = get_indent();
904 }
905 p = skipwhite(ptr);
906 if (*p == '}') /* if line starts with '}': do indent */
907 did_si = TRUE;
908 else /* can delete indent when '{' typed */
909 can_si_back = TRUE;
910 }
911 curwin->w_cursor = old_cursor;
912 }
913 if (do_si)
914 can_si = TRUE;
915#endif /* FEAT_SMARTINDENT */
916
917 did_ai = TRUE;
918 }
919
920#ifdef FEAT_COMMENTS
921 /*
922 * Find out if the current line starts with a comment leader.
923 * This may then be inserted in front of the new line.
924 */
925 end_comment_pending = NUL;
926 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200927 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 lead_len = 0;
930 if (lead_len > 0)
931 {
932 char_u *lead_repl = NULL; /* replaces comment leader */
933 int lead_repl_len = 0; /* length of *lead_repl */
934 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
935 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
936 char_u *comment_end = NULL; /* where lead_end has been found */
937 int extra_space = FALSE; /* append extra space */
938 int current_flag;
939 int require_blank = FALSE; /* requires blank after middle */
940 char_u *p2;
941
942 /*
943 * If the comment leader has the start, middle or end flag, it may not
944 * be used or may be replaced with the middle leader.
945 */
946 for (p = lead_flags; *p && *p != ':'; ++p)
947 {
948 if (*p == COM_BLANK)
949 {
950 require_blank = TRUE;
951 continue;
952 }
953 if (*p == COM_START || *p == COM_MIDDLE)
954 {
955 current_flag = *p;
956 if (*p == COM_START)
957 {
958 /*
959 * Doing "O" on a start of comment does not insert leader.
960 */
961 if (dir == BACKWARD)
962 {
963 lead_len = 0;
964 break;
965 }
966
967 /* find start of middle part */
968 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
969 require_blank = FALSE;
970 }
971
972 /*
973 * Isolate the strings of the middle and end leader.
974 */
975 while (*p && p[-1] != ':') /* find end of middle flags */
976 {
977 if (*p == COM_BLANK)
978 require_blank = TRUE;
979 ++p;
980 }
981 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
982
983 while (*p && p[-1] != ':') /* find end of end flags */
984 {
985 /* Check whether we allow automatic ending of comments */
986 if (*p == COM_AUTO_END)
987 end_comment_pending = -1; /* means we want to set it */
988 ++p;
989 }
990 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
991
992 if (end_comment_pending == -1) /* we can set it now */
993 end_comment_pending = lead_end[n - 1];
994
995 /*
996 * If the end of the comment is in the same line, don't use
997 * the comment leader.
998 */
999 if (dir == FORWARD)
1000 {
1001 for (p = saved_line + lead_len; *p; ++p)
1002 if (STRNCMP(p, lead_end, n) == 0)
1003 {
1004 comment_end = p;
1005 lead_len = 0;
1006 break;
1007 }
1008 }
1009
1010 /*
1011 * Doing "o" on a start of comment inserts the middle leader.
1012 */
1013 if (lead_len > 0)
1014 {
1015 if (current_flag == COM_START)
1016 {
1017 lead_repl = lead_middle;
1018 lead_repl_len = (int)STRLEN(lead_middle);
1019 }
1020
1021 /*
1022 * If we have hit RETURN immediately after the start
1023 * comment leader, then put a space after the middle
1024 * comment leader on the next line.
1025 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001026 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 && ((p_extra != NULL
1028 && (int)curwin->w_cursor.col == lead_len)
1029 || (p_extra == NULL
1030 && saved_line[lead_len] == NUL)
1031 || require_blank))
1032 extra_space = TRUE;
1033 }
1034 break;
1035 }
1036 if (*p == COM_END)
1037 {
1038 /*
1039 * Doing "o" on the end of a comment does not insert leader.
1040 * Remember where the end is, might want to use it to find the
1041 * start (for C-comments).
1042 */
1043 if (dir == FORWARD)
1044 {
1045 comment_end = skipwhite(saved_line);
1046 lead_len = 0;
1047 break;
1048 }
1049
1050 /*
1051 * Doing "O" on the end of a comment inserts the middle leader.
1052 * Find the string for the middle leader, searching backwards.
1053 */
1054 while (p > curbuf->b_p_com && *p != ',')
1055 --p;
1056 for (lead_repl = p; lead_repl > curbuf->b_p_com
1057 && lead_repl[-1] != ':'; --lead_repl)
1058 ;
1059 lead_repl_len = (int)(p - lead_repl);
1060
1061 /* We can probably always add an extra space when doing "O" on
1062 * the comment-end */
1063 extra_space = TRUE;
1064
1065 /* Check whether we allow automatic ending of comments */
1066 for (p2 = p; *p2 && *p2 != ':'; p2++)
1067 {
1068 if (*p2 == COM_AUTO_END)
1069 end_comment_pending = -1; /* means we want to set it */
1070 }
1071 if (end_comment_pending == -1)
1072 {
1073 /* Find last character in end-comment string */
1074 while (*p2 && *p2 != ',')
1075 p2++;
1076 end_comment_pending = p2[-1];
1077 }
1078 break;
1079 }
1080 if (*p == COM_FIRST)
1081 {
1082 /*
1083 * Comment leader for first line only: Don't repeat leader
1084 * when using "O", blank out leader when using "o".
1085 */
1086 if (dir == BACKWARD)
1087 lead_len = 0;
1088 else
1089 {
1090 lead_repl = (char_u *)"";
1091 lead_repl_len = 0;
1092 }
1093 break;
1094 }
1095 }
1096 if (lead_len)
1097 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001098 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001099 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001100 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 allocated = leader; /* remember to free it later */
1102
1103 if (leader == NULL)
1104 lead_len = 0;
1105 else
1106 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001107 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 /*
1110 * Replace leader with lead_repl, right or left adjusted
1111 */
1112 if (lead_repl != NULL)
1113 {
1114 int c = 0;
1115 int off = 0;
1116
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else if (VIM_ISDIGIT(*p) || *p == '-')
1122 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001123 else
1124 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126 if (c == COM_RIGHT) /* right adjusted leader */
1127 {
1128 /* find last non-white in the leader to line up with */
1129 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001130 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001133
1134#ifdef FEAT_MBYTE
1135 /* Compute the length of the replaced characters in
1136 * screen characters, not bytes. */
1137 {
1138 int repl_size = vim_strnsize(lead_repl,
1139 lead_repl_len);
1140 int old_size = 0;
1141 char_u *endp = p;
1142 int l;
1143
1144 while (old_size < repl_size && p > leader)
1145 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001146 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 old_size += ptr2cells(p);
1148 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001149 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001150 if (l != 0)
1151 mch_memmove(endp + l, endp,
1152 (size_t)((leader + lead_len) - endp));
1153 lead_len += l;
1154 }
1155#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 if (p < leader + lead_repl_len)
1157 p = leader;
1158 else
1159 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1162 if (p + lead_repl_len > leader + lead_len)
1163 p[lead_repl_len] = NUL;
1164
1165 /* blank-out any other chars from the old leader. */
1166 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001167 {
1168#ifdef FEAT_MBYTE
1169 int l = mb_head_off(leader, p);
1170
1171 if (l > 1)
1172 {
1173 p -= l;
1174 if (ptr2cells(p) > 1)
1175 {
1176 p[1] = ' ';
1177 --l;
1178 }
1179 mch_memmove(p + 1, p + l + 1,
1180 (size_t)((leader + lead_len) - (p + l + 1)));
1181 lead_len -= l;
1182 *p = ' ';
1183 }
1184 else
1185#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001186 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 else /* left adjusted leader */
1191 {
1192 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001193#ifdef FEAT_MBYTE
1194 /* Compute the length of the replaced characters in
1195 * screen characters, not bytes. Move the part that is
1196 * not to be overwritten. */
1197 {
1198 int repl_size = vim_strnsize(lead_repl,
1199 lead_repl_len);
1200 int i;
1201 int l;
1202
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001203 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001204 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001205 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001206 if (vim_strnsize(p, i + l) > repl_size)
1207 break;
1208 }
1209 if (i != lead_repl_len)
1210 {
1211 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001212 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001213 lead_len += lead_repl_len - i;
1214 }
1215 }
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1218
1219 /* Replace any remaining non-white chars in the old
1220 * leader by spaces. Keep Tabs, the indent must
1221 * remain the same. */
1222 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001223 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
1225 /* Don't put a space before a TAB. */
1226 if (p + 1 < leader + lead_len && p[1] == TAB)
1227 {
1228 --lead_len;
1229 mch_memmove(p, p + 1,
1230 (leader + lead_len) - p);
1231 }
1232 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233 {
1234#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001235 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001236
1237 if (l > 1)
1238 {
1239 if (ptr2cells(p) > 1)
1240 {
1241 /* Replace a double-wide char with
1242 * two spaces */
1243 --l;
1244 *p++ = ' ';
1245 }
1246 mch_memmove(p + 1, p + l,
1247 (leader + lead_len) - p);
1248 lead_len -= l - 1;
1249 }
1250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 *p = NUL;
1255 }
1256
1257 /* Recompute the indent, it may have changed. */
1258 if (curbuf->b_p_ai
1259#ifdef FEAT_SMARTINDENT
1260 || do_si
1261#endif
1262 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001263 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265 /* Add the indent offset */
1266 if (newindent + off < 0)
1267 {
1268 off = -newindent;
1269 newindent = 0;
1270 }
1271 else
1272 newindent += off;
1273
1274 /* Correct trailing spaces for the shift, so that
1275 * alignment remains equal. */
1276 while (off > 0 && lead_len > 0
1277 && leader[lead_len - 1] == ' ')
1278 {
1279 /* Don't do it when there is a tab before the space */
1280 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1281 break;
1282 --lead_len;
1283 --off;
1284 }
1285
1286 /* If the leader ends in white space, don't add an
1287 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001288 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 extra_space = FALSE;
1290 leader[lead_len] = NUL;
1291 }
1292
1293 if (extra_space)
1294 {
1295 leader[lead_len++] = ' ';
1296 leader[lead_len] = NUL;
1297 }
1298
1299 newcol = lead_len;
1300
1301 /*
1302 * if a new indent will be set below, remove the indent that
1303 * is in the comment leader
1304 */
1305 if (newindent
1306#ifdef FEAT_SMARTINDENT
1307 || did_si
1308#endif
1309 )
1310 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001311 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 --lead_len;
1314 --newcol;
1315 ++leader;
1316 }
1317 }
1318
1319 }
1320#ifdef FEAT_SMARTINDENT
1321 did_si = can_si = FALSE;
1322#endif
1323 }
1324 else if (comment_end != NULL)
1325 {
1326 /*
1327 * We have finished a comment, so we don't use the leader.
1328 * If this was a C-comment and 'ai' or 'si' is set do a normal
1329 * indent to align with the line containing the start of the
1330 * comment.
1331 */
1332 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1333 (curbuf->b_p_ai
1334#ifdef FEAT_SMARTINDENT
1335 || do_si
1336#endif
1337 ))
1338 {
1339 old_cursor = curwin->w_cursor;
1340 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1341 if ((pos = findmatch(NULL, NUL)) != NULL)
1342 {
1343 curwin->w_cursor.lnum = pos->lnum;
1344 newindent = get_indent();
1345 }
1346 curwin->w_cursor = old_cursor;
1347 }
1348 }
1349 }
1350#endif
1351
1352 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1353 if (p_extra != NULL)
1354 {
1355 *p_extra = saved_char; /* restore char that NUL replaced */
1356
1357 /*
1358 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1359 * non-blank.
1360 *
1361 * When in REPLACE mode, put the deleted blanks on the replace stack,
1362 * preceded by a NUL, so they can be put back when a BS is entered.
1363 */
1364 if (REPLACE_NORMAL(State))
1365 replace_push(NUL); /* end of extra blanks */
1366 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1367 {
1368 while ((*p_extra == ' ' || *p_extra == '\t')
1369#ifdef FEAT_MBYTE
1370 && (!enc_utf8
1371 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1372#endif
1373 )
1374 {
1375 if (REPLACE_NORMAL(State))
1376 replace_push(*p_extra);
1377 ++p_extra;
1378 ++less_cols_off;
1379 }
1380 }
1381 if (*p_extra != NUL)
1382 did_ai = FALSE; /* append some text, don't truncate now */
1383
1384 /* columns for marks adjusted for removed columns */
1385 less_cols = (int)(p_extra - saved_line);
1386 }
1387
1388 if (p_extra == NULL)
1389 p_extra = (char_u *)""; /* append empty line */
1390
1391#ifdef FEAT_COMMENTS
1392 /* concatenate leader and p_extra, if there is a leader */
1393 if (lead_len)
1394 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001395 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1396 {
1397 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001398 int padding = second_line_indent
1399 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001400
1401 /* Here whitespace is inserted after the comment char.
1402 * Below, set_indent(newindent, SIN_INSERT) will insert the
1403 * whitespace needed before the comment char. */
1404 for (i = 0; i < padding; i++)
1405 {
1406 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001407 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001408 newcol++;
1409 }
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 STRCAT(leader, p_extra);
1412 p_extra = leader;
1413 did_ai = TRUE; /* So truncating blanks works with comments */
1414 less_cols -= lead_len;
1415 }
1416 else
1417 end_comment_pending = NUL; /* turns out there was no leader */
1418#endif
1419
1420 old_cursor = curwin->w_cursor;
1421 if (dir == BACKWARD)
1422 --curwin->w_cursor.lnum;
1423#ifdef FEAT_VREPLACE
1424 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1425#endif
1426 {
1427 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1428 == FAIL)
1429 goto theend;
1430 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001431 * with markers.
1432 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001433 * be marks there. But still needed in diff mode. */
1434 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1435#ifdef FEAT_DIFF
1436 || curwin->w_p_diff
1437#endif
1438 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001439 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 did_append = TRUE;
1441 }
1442#ifdef FEAT_VREPLACE
1443 else
1444 {
1445 /*
1446 * In VREPLACE mode we are starting to replace the next line.
1447 */
1448 curwin->w_cursor.lnum++;
1449 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1450 {
1451 /* In case we NL to a new line, BS to the previous one, and NL
1452 * again, we don't want to save the new line for undo twice.
1453 */
1454 (void)u_save_cursor(); /* errors are ignored! */
1455 vr_lines_changed++;
1456 }
1457 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1458 changed_bytes(curwin->w_cursor.lnum, 0);
1459 curwin->w_cursor.lnum--;
1460 did_append = FALSE;
1461 }
1462#endif
1463
1464 if (newindent
1465#ifdef FEAT_SMARTINDENT
1466 || did_si
1467#endif
1468 )
1469 {
1470 ++curwin->w_cursor.lnum;
1471#ifdef FEAT_SMARTINDENT
1472 if (did_si)
1473 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001474 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001477 newindent -= newindent % sw;
1478 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001481 /* Copy the indent */
1482 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 (void)copy_indent(newindent, saved_line);
1485
1486 /*
1487 * Set the 'preserveindent' option so that any further screwing
1488 * with the line doesn't entirely destroy our efforts to preserve
1489 * it. It gets restored at the function end.
1490 */
1491 curbuf->b_p_pi = TRUE;
1492 }
1493 else
1494 (void)set_indent(newindent, SIN_INSERT);
1495 less_cols -= curwin->w_cursor.col;
1496
1497 ai_col = curwin->w_cursor.col;
1498
1499 /*
1500 * In REPLACE mode, for each character in the new indent, there must
1501 * be a NUL on the replace stack, for when it is deleted with BS
1502 */
1503 if (REPLACE_NORMAL(State))
1504 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1505 replace_push(NUL);
1506 newcol += curwin->w_cursor.col;
1507#ifdef FEAT_SMARTINDENT
1508 if (no_si)
1509 did_si = FALSE;
1510#endif
1511 }
1512
1513#ifdef FEAT_COMMENTS
1514 /*
1515 * In REPLACE mode, for each character in the extra leader, there must be
1516 * a NUL on the replace stack, for when it is deleted with BS.
1517 */
1518 if (REPLACE_NORMAL(State))
1519 while (lead_len-- > 0)
1520 replace_push(NUL);
1521#endif
1522
1523 curwin->w_cursor = old_cursor;
1524
1525 if (dir == FORWARD)
1526 {
1527 if (trunc_line || (State & INSERT))
1528 {
1529 /* truncate current line at cursor */
1530 saved_line[curwin->w_cursor.col] = NUL;
1531 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1532 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1533 truncate_spaces(saved_line);
1534 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1535 saved_line = NULL;
1536 if (did_append)
1537 {
1538 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1539 curwin->w_cursor.lnum + 1, 1L);
1540 did_append = FALSE;
1541
1542 /* Move marks after the line break to the new line. */
1543 if (flags & OPENLINE_MARKFIX)
1544 mark_col_adjust(curwin->w_cursor.lnum,
1545 curwin->w_cursor.col + less_cols_off,
1546 1L, (long)-less_cols);
1547 }
1548 else
1549 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1550 }
1551
1552 /*
1553 * Put the cursor on the new line. Careful: the scrollup() above may
1554 * have moved w_cursor, we must use old_cursor.
1555 */
1556 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1557 }
1558 if (did_append)
1559 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1560
1561 curwin->w_cursor.col = newcol;
1562#ifdef FEAT_VIRTUALEDIT
1563 curwin->w_cursor.coladd = 0;
1564#endif
1565
1566#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1567 /*
1568 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1569 * fixthisline() from doing it (via change_indent()) by telling it we're in
1570 * normal INSERT mode.
1571 */
1572 if (State & VREPLACE_FLAG)
1573 {
1574 vreplace_mode = State; /* So we know to put things right later */
1575 State = INSERT;
1576 }
1577 else
1578 vreplace_mode = 0;
1579#endif
1580#ifdef FEAT_LISP
1581 /*
1582 * May do lisp indenting.
1583 */
1584 if (!p_paste
1585# ifdef FEAT_COMMENTS
1586 && leader == NULL
1587# endif
1588 && curbuf->b_p_lisp
1589 && curbuf->b_p_ai)
1590 {
1591 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001592 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594#endif
1595#ifdef FEAT_CINDENT
1596 /*
1597 * May do indenting after opening a new line.
1598 */
1599 if (!p_paste
1600 && (curbuf->b_p_cin
1601# ifdef FEAT_EVAL
1602 || *curbuf->b_p_inde != NUL
1603# endif
1604 )
1605 && in_cinkeys(dir == FORWARD
1606 ? KEY_OPEN_FORW
1607 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1608 {
1609 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001610 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612#endif
1613#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1614 if (vreplace_mode != 0)
1615 State = vreplace_mode;
1616#endif
1617
1618#ifdef FEAT_VREPLACE
1619 /*
1620 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1621 * original line, and inserts the new stuff char by char, pushing old stuff
1622 * onto the replace stack (via ins_char()).
1623 */
1624 if (State & VREPLACE_FLAG)
1625 {
1626 /* Put new line in p_extra */
1627 p_extra = vim_strsave(ml_get_curline());
1628 if (p_extra == NULL)
1629 goto theend;
1630
1631 /* Put back original line */
1632 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1633
1634 /* Insert new stuff into line again */
1635 curwin->w_cursor.col = 0;
1636#ifdef FEAT_VIRTUALEDIT
1637 curwin->w_cursor.coladd = 0;
1638#endif
1639 ins_bytes(p_extra); /* will call changed_bytes() */
1640 vim_free(p_extra);
1641 next_line = NULL;
1642 }
1643#endif
1644
1645 retval = TRUE; /* success! */
1646theend:
1647 curbuf->b_p_pi = saved_pi;
1648 vim_free(saved_line);
1649 vim_free(next_line);
1650 vim_free(allocated);
1651 return retval;
1652}
1653
1654#if defined(FEAT_COMMENTS) || defined(PROTO)
1655/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001656 * get_leader_len() returns the length in bytes of the prefix of the given
1657 * string which introduces a comment. If this string is not a comment then
1658 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 * When "flags" is not NULL, it is set to point to the flags of the recognized
1660 * comment leader.
1661 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001662 * If "include_space" is set, include trailing whitespace while calculating the
1663 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 */
1665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001666get_leader_len(
1667 char_u *line,
1668 char_u **flags,
1669 int backward,
1670 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671{
1672 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001673 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 int got_com = FALSE;
1675 int found_one;
1676 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1677 char_u *string; /* pointer to comment string */
1678 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001679 int middle_match_len = 0;
1680 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001681 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaar81340392012-06-06 16:12:59 +02001683 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001684 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 ++i;
1686
1687 /*
1688 * Repeat to match several nested comment strings.
1689 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001690 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 /*
1693 * scan through the 'comments' option for a match
1694 */
1695 found_one = FALSE;
1696 for (list = curbuf->b_p_com; *list; )
1697 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001698 /* Get one option part into part_buf[]. Advance "list" to next
1699 * one. Put "string" at start of string. */
1700 if (!got_com && flags != NULL)
1701 *flags = list; /* remember where flags started */
1702 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1704 string = vim_strchr(part_buf, ':');
1705 if (string == NULL) /* missing ':', ignore this part */
1706 continue;
1707 *string++ = NUL; /* isolate flags from string */
1708
Bram Moolenaara4271d52011-05-10 13:38:27 +02001709 /* If we found a middle match previously, use that match when this
1710 * is not a middle or end. */
1711 if (middle_match_len != 0
1712 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1713 && vim_strchr(part_buf, COM_END) == NULL)
1714 break;
1715
1716 /* When we already found a nested comment, only accept further
1717 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1719 continue;
1720
Bram Moolenaara4271d52011-05-10 13:38:27 +02001721 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1723 continue;
1724
Bram Moolenaara4271d52011-05-10 13:38:27 +02001725 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 * When string starts with white space, must have some white space
1727 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001728 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001729 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001731 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001732 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001733 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 ++string;
1735 }
1736 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1737 ;
1738 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001739 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaara4271d52011-05-10 13:38:27 +02001741 /* When 'b' flag used, there must be white space or an
1742 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001744 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 continue;
1746
Bram Moolenaara4271d52011-05-10 13:38:27 +02001747 /* We have found a match, stop searching unless this is a middle
1748 * comment. The middle comment can be a substring of the end
1749 * comment in which case it's better to return the length of the
1750 * end comment and its flags. Thus we keep searching with middle
1751 * and end matches and use an end match if it matches better. */
1752 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1753 {
1754 if (middle_match_len == 0)
1755 {
1756 middle_match_len = j;
1757 saved_flags = prev_list;
1758 }
1759 continue;
1760 }
1761 if (middle_match_len != 0 && j > middle_match_len)
1762 /* Use this match instead of the middle match, since it's a
1763 * longer thus better match. */
1764 middle_match_len = 0;
1765
1766 if (middle_match_len == 0)
1767 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 found_one = TRUE;
1769 break;
1770 }
1771
Bram Moolenaara4271d52011-05-10 13:38:27 +02001772 if (middle_match_len != 0)
1773 {
1774 /* Use the previously found middle match after failing to find a
1775 * match with an end. */
1776 if (!got_com && flags != NULL)
1777 *flags = saved_flags;
1778 i += middle_match_len;
1779 found_one = TRUE;
1780 }
1781
1782 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (!found_one)
1784 break;
1785
Bram Moolenaar81340392012-06-06 16:12:59 +02001786 result = i;
1787
Bram Moolenaara4271d52011-05-10 13:38:27 +02001788 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001789 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 ++i;
1791
Bram Moolenaar81340392012-06-06 16:12:59 +02001792 if (include_space)
1793 result = i;
1794
Bram Moolenaara4271d52011-05-10 13:38:27 +02001795 /* If this comment doesn't nest, stop here. */
1796 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (vim_strchr(part_buf, COM_NEST) == NULL)
1798 break;
1799 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001800 return result;
1801}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001802
Bram Moolenaar81340392012-06-06 16:12:59 +02001803/*
1804 * Return the offset at which the last comment in line starts. If there is no
1805 * comment in the whole line, -1 is returned.
1806 *
1807 * When "flags" is not null, it is set to point to the flags describing the
1808 * recognized comment leader.
1809 */
1810 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001811get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001812{
1813 int result = -1;
1814 int i, j;
1815 int lower_check_bound = 0;
1816 char_u *string;
1817 char_u *com_leader;
1818 char_u *com_flags;
1819 char_u *list;
1820 int found_one;
1821 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1822
1823 /*
1824 * Repeat to match several nested comment strings.
1825 */
1826 i = (int)STRLEN(line);
1827 while (--i >= lower_check_bound)
1828 {
1829 /*
1830 * scan through the 'comments' option for a match
1831 */
1832 found_one = FALSE;
1833 for (list = curbuf->b_p_com; *list; )
1834 {
1835 char_u *flags_save = list;
1836
1837 /*
1838 * Get one option part into part_buf[]. Advance list to next one.
1839 * put string at start of string.
1840 */
1841 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1842 string = vim_strchr(part_buf, ':');
1843 if (string == NULL) /* If everything is fine, this cannot actually
1844 * happen. */
1845 {
1846 continue;
1847 }
1848 *string++ = NUL; /* Isolate flags from string. */
1849 com_leader = string;
1850
1851 /*
1852 * Line contents and string must match.
1853 * When string starts with white space, must have some white space
1854 * (but the amount does not need to match, there might be a mix of
1855 * TABs and spaces).
1856 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001857 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001858 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001859 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001860 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001861 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 ++string;
1863 }
1864 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1865 /* do nothing */;
1866 if (string[j] != NUL)
1867 continue;
1868
1869 /*
1870 * When 'b' flag used, there must be white space or an
1871 * end-of-line after the string in the line.
1872 */
1873 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001874 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02001875 {
1876 continue;
1877 }
1878
1879 /*
1880 * We have found a match, stop searching.
1881 */
1882 found_one = TRUE;
1883
1884 if (flags)
1885 *flags = flags_save;
1886 com_flags = flags_save;
1887
1888 break;
1889 }
1890
1891 if (found_one)
1892 {
1893 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1894 int len1, len2, off;
1895
1896 result = i;
1897 /*
1898 * If this comment nests, continue searching.
1899 */
1900 if (vim_strchr(part_buf, COM_NEST) != NULL)
1901 continue;
1902
1903 lower_check_bound = i;
1904
1905 /* Let's verify whether the comment leader found is a substring
1906 * of other comment leaders. If it is, let's adjust the
1907 * lower_check_bound so that we make sure that we have determined
1908 * the comment leader correctly.
1909 */
1910
Bram Moolenaar1c465442017-03-12 20:10:05 +01001911 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02001912 ++com_leader;
1913 len1 = (int)STRLEN(com_leader);
1914
1915 for (list = curbuf->b_p_com; *list; )
1916 {
1917 char_u *flags_save = list;
1918
1919 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1920 if (flags_save == com_flags)
1921 continue;
1922 string = vim_strchr(part_buf2, ':');
1923 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001924 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02001925 ++string;
1926 len2 = (int)STRLEN(string);
1927 if (len2 == 0)
1928 continue;
1929
1930 /* Now we have to verify whether string ends with a substring
1931 * beginning the com_leader. */
1932 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1933 {
1934 --off;
1935 if (!STRNCMP(string + off, com_leader, len2 - off))
1936 {
1937 if (i - off < lower_check_bound)
1938 lower_check_bound = i - off;
1939 }
1940 }
1941 }
1942 }
1943 }
1944 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945}
1946#endif
1947
1948/*
1949 * Return the number of window lines occupied by buffer line "lnum".
1950 */
1951 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001952plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953{
1954 return plines_win(curwin, lnum, TRUE);
1955}
1956
1957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001958plines_win(
1959 win_T *wp,
1960 linenr_T lnum,
1961 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963#if defined(FEAT_DIFF) || defined(PROTO)
1964 /* Check for filler lines above this buffer line. When folded the result
1965 * is one line anyway. */
1966 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1967}
1968
1969 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001970plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 return plines_win_nofill(curwin, lnum, TRUE);
1973}
1974
1975 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976plines_win_nofill(
1977 win_T *wp,
1978 linenr_T lnum,
1979 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981#endif
1982 int lines;
1983
1984 if (!wp->w_p_wrap)
1985 return 1;
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (wp->w_width == 0)
1988 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990#ifdef FEAT_FOLDING
1991 /* A folded lines is handled just like an empty line. */
1992 /* NOTE: Caller must handle lines that are MAYBE folded. */
1993 if (lineFolded(wp, lnum) == TRUE)
1994 return 1;
1995#endif
1996
1997 lines = plines_win_nofold(wp, lnum);
1998 if (winheight > 0 && lines > wp->w_height)
1999 return (int)wp->w_height;
2000 return lines;
2001}
2002
2003/*
2004 * Return number of window lines physical line "lnum" will occupy in window
2005 * "wp". Does not care about folding, 'wrap' or 'diff'.
2006 */
2007 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 char_u *s;
2011 long col;
2012 int width;
2013
2014 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2015 if (*s == NUL) /* empty line */
2016 return 1;
2017 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2018
2019 /*
2020 * If list mode is on, then the '$' at the end of the line may take up one
2021 * extra column.
2022 */
2023 if (wp->w_p_list && lcs_eol != NUL)
2024 col += 1;
2025
2026 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002027 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002029 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (width <= 0)
2031 return 32000;
2032 if (col <= width)
2033 return 1;
2034 col -= width;
2035 width += win_col_off2(wp);
2036 return (col + (width - 1)) / width + 1;
2037}
2038
2039/*
2040 * Like plines_win(), but only reports the number of physical screen lines
2041 * used from the start of the line to the given column number.
2042 */
2043 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 long col;
2047 char_u *s;
2048 int lines = 0;
2049 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002050 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052#ifdef FEAT_DIFF
2053 /* Check for filler lines above this buffer line. When folded the result
2054 * is one line anyway. */
2055 lines = diff_check_fill(wp, lnum);
2056#endif
2057
2058 if (!wp->w_p_wrap)
2059 return lines + 1;
2060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (wp->w_width == 0)
2062 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
Bram Moolenaar597a4222014-06-25 14:39:50 +02002064 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 col = 0;
2067 while (*s != NUL && --column >= 0)
2068 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002069 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002070 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072
2073 /*
2074 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2075 * INSERT mode, then col must be adjusted so that it represents the last
2076 * screen position of the TAB. This only fixes an error when the TAB wraps
2077 * from one screen line to the next (when 'columns' is not a multiple of
2078 * 'ts') -- webb.
2079 */
2080 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002081 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002084 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002086 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002087 if (width <= 0)
2088 return 9999;
2089
2090 lines += 1;
2091 if (col > width)
2092 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2093 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094}
2095
2096 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002097plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 int count = 0;
2100
2101 while (first <= last)
2102 {
2103#ifdef FEAT_FOLDING
2104 int x;
2105
2106 /* Check if there are any really folded lines, but also included lines
2107 * that are maybe folded. */
2108 x = foldedCount(wp, first, NULL);
2109 if (x > 0)
2110 {
2111 ++count; /* count 1 for "+-- folded" line */
2112 first += x;
2113 }
2114 else
2115#endif
2116 {
2117#ifdef FEAT_DIFF
2118 if (first == wp->w_topline)
2119 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2120 else
2121#endif
2122 count += plines_win(wp, first, TRUE);
2123 ++first;
2124 }
2125 }
2126 return (count);
2127}
2128
2129#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2130/*
2131 * Insert string "p" at the cursor position. Stops at a NUL byte.
2132 * Handles Replace mode and multi-byte characters.
2133 */
2134 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002135ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
2137 ins_bytes_len(p, (int)STRLEN(p));
2138}
2139#endif
2140
2141#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2142 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2143/*
2144 * Insert string "p" with length "len" at the cursor position.
2145 * Handles Replace mode and multi-byte characters.
2146 */
2147 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002148ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 int i;
2151# ifdef FEAT_MBYTE
2152 int n;
2153
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002154 if (has_mbyte)
2155 for (i = 0; i < len; i += n)
2156 {
2157 if (enc_utf8)
2158 /* avoid reading past p[len] */
2159 n = utfc_ptr2len_len(p + i, len - i);
2160 else
2161 n = (*mb_ptr2len)(p + i);
2162 ins_char_bytes(p + i, n);
2163 }
2164 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002166 for (i = 0; i < len; ++i)
2167 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168}
2169#endif
2170
2171/*
2172 * Insert or replace a single character at the cursor position.
2173 * When in REPLACE or VREPLACE mode, replace any existing character.
2174 * Caller must have prepared for undo.
2175 * For multi-byte characters we get the whole character, the caller must
2176 * convert bytes to a character.
2177 */
2178 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002181 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002182 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002184#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 n = (*mb_char2bytes)(c, buf);
2186
2187 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2188 * Happens for CTRL-Vu9900. */
2189 if (buf[0] == 0)
2190 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002191#else
2192 buf[0] = c;
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 ins_char_bytes(buf, n);
2196}
2197
2198 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002199ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 int newlen; /* nr of bytes inserted */
2203 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2204 char_u *p;
2205 char_u *newp;
2206 char_u *oldp;
2207 int linelen; /* length of old line including NUL */
2208 colnr_T col;
2209 linenr_T lnum = curwin->w_cursor.lnum;
2210 int i;
2211
2212#ifdef FEAT_VIRTUALEDIT
2213 /* Break tabs if needed. */
2214 if (virtual_active() && curwin->w_cursor.coladd > 0)
2215 coladvance_force(getviscol());
2216#endif
2217
2218 col = curwin->w_cursor.col;
2219 oldp = ml_get(lnum);
2220 linelen = (int)STRLEN(oldp) + 1;
2221
2222 /* The lengths default to the values for when not replacing. */
2223 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 if (State & REPLACE_FLAG)
2227 {
2228#ifdef FEAT_VREPLACE
2229 if (State & VREPLACE_FLAG)
2230 {
2231 colnr_T new_vcol = 0; /* init for GCC */
2232 colnr_T vcol;
2233 int old_list;
2234#ifndef FEAT_MBYTE
2235 char_u buf[2];
2236#endif
2237
2238 /*
2239 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2240 * Returns the old value of list, so when finished,
2241 * curwin->w_p_list should be set back to this.
2242 */
2243 old_list = curwin->w_p_list;
2244 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2245 curwin->w_p_list = FALSE;
2246
2247 /*
2248 * In virtual replace mode each character may replace one or more
2249 * characters (zero if it's a TAB). Count the number of bytes to
2250 * be deleted to make room for the new character, counting screen
2251 * cells. May result in adding spaces to fill a gap.
2252 */
2253 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2254#ifndef FEAT_MBYTE
2255 buf[0] = c;
2256 buf[1] = NUL;
2257#endif
2258 new_vcol = vcol + chartabsize(buf, vcol);
2259 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2260 {
2261 vcol += chartabsize(oldp + col + oldlen, vcol);
2262 /* Don't need to remove a TAB that takes us to the right
2263 * position. */
2264 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2265 break;
2266#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002267 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#else
2269 ++oldlen;
2270#endif
2271 /* Deleted a bit too much, insert spaces. */
2272 if (vcol > new_vcol)
2273 newlen += vcol - new_vcol;
2274 }
2275 curwin->w_p_list = old_list;
2276 }
2277 else
2278#endif
2279 if (oldp[col] != NUL)
2280 {
2281 /* normal replace */
2282#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002283 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#else
2285 oldlen = 1;
2286#endif
2287 }
2288
2289
2290 /* Push the replaced bytes onto the replace stack, so that they can be
2291 * put back when BS is used. The bytes of a multi-byte character are
2292 * done the other way around, so that the first byte is popped off
2293 * first (it tells the byte length of the character). */
2294 replace_push(NUL);
2295 for (i = 0; i < oldlen; ++i)
2296 {
2297#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002298 if (has_mbyte)
2299 i += replace_push_mb(oldp + col + i) - 1;
2300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002302 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 }
2305
2306 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2307 if (newp == NULL)
2308 return;
2309
2310 /* Copy bytes before the cursor. */
2311 if (col > 0)
2312 mch_memmove(newp, oldp, (size_t)col);
2313
2314 /* Copy bytes after the changed character(s). */
2315 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002316 if (linelen > col + oldlen)
2317 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 (size_t)(linelen - col - oldlen));
2319
2320 /* Insert or overwrite the new character. */
2321#ifdef FEAT_MBYTE
2322 mch_memmove(p, buf, charlen);
2323 i = charlen;
2324#else
2325 *p = c;
2326 i = 1;
2327#endif
2328
2329 /* Fill with spaces when necessary. */
2330 while (i < newlen)
2331 p[i++] = ' ';
2332
2333 /* Replace the line in the buffer. */
2334 ml_replace(lnum, newp, FALSE);
2335
2336 /* mark the buffer as changed and prepare for displaying */
2337 changed_bytes(lnum, col);
2338
2339 /*
2340 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2341 * show the match for right parens and braces.
2342 */
2343 if (p_sm && (State & INSERT)
2344 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002345#ifdef FEAT_INS_EXPAND
2346 && !ins_compl_active()
2347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002349 {
2350#ifdef FEAT_MBYTE
2351 if (has_mbyte)
2352 showmatch(mb_ptr2char(buf));
2353 else
2354#endif
2355 showmatch(c);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_RIGHTLEFT
2359 if (!p_ri || (State & REPLACE_FLAG))
2360#endif
2361 {
2362 /* Normal insert: move cursor right */
2363#ifdef FEAT_MBYTE
2364 curwin->w_cursor.col += charlen;
2365#else
2366 ++curwin->w_cursor.col;
2367#endif
2368 }
2369 /*
2370 * TODO: should try to update w_row here, to avoid recomputing it later.
2371 */
2372}
2373
2374/*
2375 * Insert a string at the cursor position.
2376 * Note: Does NOT handle Replace mode.
2377 * Caller must have prepared for undo.
2378 */
2379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002380ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char_u *oldp, *newp;
2383 int newlen = (int)STRLEN(s);
2384 int oldlen;
2385 colnr_T col;
2386 linenr_T lnum = curwin->w_cursor.lnum;
2387
2388#ifdef FEAT_VIRTUALEDIT
2389 if (virtual_active() && curwin->w_cursor.coladd > 0)
2390 coladvance_force(getviscol());
2391#endif
2392
2393 col = curwin->w_cursor.col;
2394 oldp = ml_get(lnum);
2395 oldlen = (int)STRLEN(oldp);
2396
2397 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2398 if (newp == NULL)
2399 return;
2400 if (col > 0)
2401 mch_memmove(newp, oldp, (size_t)col);
2402 mch_memmove(newp + col, s, (size_t)newlen);
2403 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2404 ml_replace(lnum, newp, FALSE);
2405 changed_bytes(lnum, col);
2406 curwin->w_cursor.col += newlen;
2407}
2408
2409/*
2410 * Delete one character under the cursor.
2411 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2412 * Caller must have prepared for undo.
2413 *
2414 * return FAIL for failure, OK otherwise
2415 */
2416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419#ifdef FEAT_MBYTE
2420 if (has_mbyte)
2421 {
2422 /* Make sure the cursor is at the start of a character. */
2423 mb_adjust_cursor();
2424 if (*ml_get_cursor() == NUL)
2425 return FAIL;
2426 return del_chars(1L, fixpos);
2427 }
2428#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002429 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
2432#if defined(FEAT_MBYTE) || defined(PROTO)
2433/*
2434 * Like del_bytes(), but delete characters instead of bytes.
2435 */
2436 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002437del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 long bytes = 0;
2440 long i;
2441 char_u *p;
2442 int l;
2443
2444 p = ml_get_cursor();
2445 for (i = 0; i < count && *p != NUL; ++i)
2446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 bytes += l;
2449 p += l;
2450 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002451 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
2453#endif
2454
2455/*
2456 * Delete "count" bytes under the cursor.
2457 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2458 * Caller must have prepared for undo.
2459 *
2460 * return FAIL for failure, OK otherwise
2461 */
2462 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463del_bytes(
2464 long count,
2465 int fixpos_arg,
2466 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 char_u *oldp, *newp;
2469 colnr_T oldlen;
2470 linenr_T lnum = curwin->w_cursor.lnum;
2471 colnr_T col = curwin->w_cursor.col;
2472 int was_alloced;
2473 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002474 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 oldp = ml_get(lnum);
2477 oldlen = (int)STRLEN(oldp);
2478
2479 /*
2480 * Can't do anything when the cursor is on the NUL after the line.
2481 */
2482 if (col >= oldlen)
2483 return FAIL;
2484
2485#ifdef FEAT_MBYTE
2486 /* If 'delcombine' is set and deleting (less than) one character, only
2487 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002488 if (p_deco && use_delcombine && enc_utf8
2489 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002491 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 int n;
2493
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002494 (void)utfc_ptr2char(oldp + col, cc);
2495 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 /* Find the last composing char, there can be several. */
2498 n = col;
2499 do
2500 {
2501 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002502 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 n += count;
2504 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2505 fixpos = 0;
2506 }
2507 }
2508#endif
2509
2510 /*
2511 * When count is too big, reduce it.
2512 */
2513 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2514 if (movelen <= 1)
2515 {
2516 /*
2517 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002518 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2519 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002521 if (col > 0 && fixpos && restart_edit == 0
2522#ifdef FEAT_VIRTUALEDIT
2523 && (ve_flags & VE_ONEMORE) == 0
2524#endif
2525 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 --curwin->w_cursor.col;
2528#ifdef FEAT_VIRTUALEDIT
2529 curwin->w_cursor.coladd = 0;
2530#endif
2531#ifdef FEAT_MBYTE
2532 if (has_mbyte)
2533 curwin->w_cursor.col -=
2534 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2535#endif
2536 }
2537 count = oldlen - col;
2538 movelen = 1;
2539 }
2540
2541 /*
2542 * If the old line has been allocated the deletion can be done in the
2543 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002544 * Can't do this when using Netbeans, because we would need to invoke
2545 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002546 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002549 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002550 was_alloced = FALSE;
2551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002553 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (was_alloced)
2555 newp = oldp; /* use same allocated memory */
2556 else
2557 { /* need to allocate a new line */
2558 newp = alloc((unsigned)(oldlen + 1 - count));
2559 if (newp == NULL)
2560 return FAIL;
2561 mch_memmove(newp, oldp, (size_t)col);
2562 }
2563 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2564 if (!was_alloced)
2565 ml_replace(lnum, newp, FALSE);
2566
2567 /* mark the buffer as changed and prepare for displaying */
2568 changed_bytes(lnum, curwin->w_cursor.col);
2569
2570 return OK;
2571}
2572
2573/*
2574 * Delete from cursor to end of line.
2575 * Caller must have prepared for undo.
2576 *
2577 * return FAIL for failure, OK otherwise
2578 */
2579 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002580truncate_line(
2581 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 char_u *newp;
2584 linenr_T lnum = curwin->w_cursor.lnum;
2585 colnr_T col = curwin->w_cursor.col;
2586
2587 if (col == 0)
2588 newp = vim_strsave((char_u *)"");
2589 else
2590 newp = vim_strnsave(ml_get(lnum), col);
2591
2592 if (newp == NULL)
2593 return FAIL;
2594
2595 ml_replace(lnum, newp, FALSE);
2596
2597 /* mark the buffer as changed and prepare for displaying */
2598 changed_bytes(lnum, curwin->w_cursor.col);
2599
2600 /*
2601 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2602 */
2603 if (fixpos && curwin->w_cursor.col > 0)
2604 --curwin->w_cursor.col;
2605
2606 return OK;
2607}
2608
2609/*
2610 * Delete "nlines" lines at the cursor.
2611 * Saves the lines for undo first if "undo" is TRUE.
2612 */
2613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002614del_lines(
2615 long nlines, /* number of lines to delete */
2616 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002619 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 if (nlines <= 0)
2622 return;
2623
2624 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002625 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return;
2627
2628 for (n = 0; n < nlines; )
2629 {
2630 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2631 break;
2632
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002633 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 ++n;
2635
2636 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002637 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 break;
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002641 /* Correct the cursor position before calling deleted_lines_mark(), it may
2642 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 curwin->w_cursor.col = 0;
2644 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002645
2646 /* adjust marks, mark the buffer as changed and prepare for displaying */
2647 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648}
2649
2650 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002651gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002653 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002655 /* When searching columns is sometimes put at the end of a line. */
2656 if (pos->col == MAXCOL)
2657 return NUL;
2658 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#ifdef FEAT_MBYTE
2660 if (has_mbyte)
2661 return (*mb_ptr2char)(ptr);
2662#endif
2663 return (int)*ptr;
2664}
2665
2666 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002667gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669#ifdef FEAT_MBYTE
2670 if (has_mbyte)
2671 return (*mb_ptr2char)(ml_get_cursor());
2672#endif
2673 return (int)*ml_get_cursor();
2674}
2675
2676/*
2677 * Write a character at the current cursor position.
2678 * It is directly written into the block.
2679 */
2680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002681pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2684 + curwin->w_cursor.col) = c;
2685}
2686
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687/*
2688 * When extra == 0: Return TRUE if the cursor is before or on the first
2689 * non-blank in the line.
2690 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2691 * the line.
2692 */
2693 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002694inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 char_u *ptr;
2697 colnr_T col;
2698
Bram Moolenaar1c465442017-03-12 20:10:05 +01002699 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 ++ptr;
2701 if (col >= curwin->w_cursor.col + extra)
2702 return TRUE;
2703 else
2704 return FALSE;
2705}
2706
2707/*
2708 * Skip to next part of an option argument: Skip space and comma.
2709 */
2710 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002711skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 if (*p == ',')
2714 ++p;
2715 while (*p == ' ')
2716 ++p;
2717 return p;
2718}
2719
2720/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002721 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 *
2723 * Most often called through changed_bytes() and changed_lines(), which also
2724 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002725 *
2726 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 */
2728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002729changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
2731#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002732 if (p_imst == IM_ON_THE_SPOT)
2733 {
2734 /* The text of the preediting area is inserted, but this doesn't
2735 * mean a change of the buffer yet. That is delayed until the
2736 * text is committed. (this means preedit becomes empty) */
2737 if (im_is_preediting() && !xim_changed_while_preediting)
2738 return;
2739 xim_changed_while_preediting = FALSE;
2740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#endif
2742
2743 if (!curbuf->b_changed)
2744 {
2745 int save_msg_scroll = msg_scroll;
2746
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002747 /* Give a warning about changing a read-only file. This may also
2748 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 /* Create a swap file if that is wanted.
2752 * Don't do this for "nofile" and "nowrite" buffer types. */
2753 if (curbuf->b_may_swap
2754#ifdef FEAT_QUICKFIX
2755 && !bt_dontwrite(curbuf)
2756#endif
2757 )
2758 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002759 int save_need_wait_return = need_wait_return;
2760
2761 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 ml_open_file(curbuf);
2763
2764 /* The ml_open_file() can cause an ATTENTION message.
2765 * Wait two seconds, to make sure the user reads this unexpected
2766 * message. Since we could be anywhere, call wait_return() now,
2767 * and don't let the emsg() set msg_scroll. */
2768 if (need_wait_return && emsg_silent == 0)
2769 {
2770 out_flush();
2771 ui_delay(2000L, TRUE);
2772 wait_return(TRUE);
2773 msg_scroll = save_msg_scroll;
2774 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002775 else
2776 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002778 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002780 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781}
2782
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002783/*
2784 * Internal part of changed(), no user interaction.
2785 */
2786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002787changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002788{
2789 curbuf->b_changed = TRUE;
2790 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002791 check_status(curbuf);
2792 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002793#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 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 int i;
2999#ifdef FEAT_JUMPLIST
3000 int cols;
3001 pos_T *p;
3002 int add;
3003#endif
3004
3005 /* mark the buffer as modified */
3006 changed();
3007
3008 /* set the '. mark */
3009 if (!cmdmod.keepjumps)
3010 {
3011 curbuf->b_last_change.lnum = lnum;
3012 curbuf->b_last_change.col = col;
3013
3014#ifdef FEAT_JUMPLIST
3015 /* Create a new entry if a new undo-able change was started or we
3016 * don't have an entry yet. */
3017 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3018 {
3019 if (curbuf->b_changelistlen == 0)
3020 add = TRUE;
3021 else
3022 {
3023 /* Don't create a new entry when the line number is the same
3024 * as the last one and the column is not too far away. Avoids
3025 * creating many entries for typing "xxxxx". */
3026 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3027 if (p->lnum != lnum)
3028 add = TRUE;
3029 else
3030 {
3031 cols = comp_textwidth(FALSE);
3032 if (cols == 0)
3033 cols = 79;
3034 add = (p->col + cols < col || col + cols < p->col);
3035 }
3036 }
3037 if (add)
3038 {
3039 /* This is the first of a new sequence of undo-able changes
3040 * and it's at some distance of the last change. Use a new
3041 * position in the changelist. */
3042 curbuf->b_new_change = FALSE;
3043
3044 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3045 {
3046 /* changelist is full: remove oldest entry */
3047 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3048 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3049 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003050 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 {
3052 /* Correct position in changelist for other windows on
3053 * this buffer. */
3054 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3055 --wp->w_changelistidx;
3056 }
3057 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003058 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
3060 /* For other windows, if the position in the changelist is
3061 * at the end it stays at the end. */
3062 if (wp->w_buffer == curbuf
3063 && wp->w_changelistidx == curbuf->b_changelistlen)
3064 ++wp->w_changelistidx;
3065 }
3066 ++curbuf->b_changelistlen;
3067 }
3068 }
3069 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3070 curbuf->b_last_change;
3071 /* The current window is always after the last change, so that "g,"
3072 * takes you back to it. */
3073 curwin->w_changelistidx = curbuf->b_changelistlen;
3074#endif
3075 }
3076
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003077 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 {
3079 if (wp->w_buffer == curbuf)
3080 {
3081 /* Mark this window to be redrawn later. */
3082 if (wp->w_redr_type < VALID)
3083 wp->w_redr_type = VALID;
3084
3085 /* Check if a change in the buffer has invalidated the cached
3086 * values for the cursor. */
3087#ifdef FEAT_FOLDING
3088 /*
3089 * Update the folds for this window. Can't postpone this, because
3090 * a following operator might work on the whole fold: ">>dd".
3091 */
3092 foldUpdate(wp, lnum, lnume + xtra - 1);
3093
3094 /* The change may cause lines above or below the change to become
3095 * included in a fold. Set lnum/lnume to the first/last line that
3096 * might be displayed differently.
3097 * Set w_cline_folded here as an efficient way to update it when
3098 * inserting lines just above a closed fold. */
3099 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3100 if (wp->w_cursor.lnum == lnum)
3101 wp->w_cline_folded = i;
3102 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3103 if (wp->w_cursor.lnum == lnume)
3104 wp->w_cline_folded = i;
3105
3106 /* If the changed line is in a range of previously folded lines,
3107 * compare with the first line in that range. */
3108 if (wp->w_cursor.lnum <= lnum)
3109 {
3110 i = find_wl_entry(wp, lnum);
3111 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3112 changed_line_abv_curs_win(wp);
3113 }
3114#endif
3115
3116 if (wp->w_cursor.lnum > lnum)
3117 changed_line_abv_curs_win(wp);
3118 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3119 changed_cline_bef_curs_win(wp);
3120 if (wp->w_botline >= lnum)
3121 {
3122 /* Assume that botline doesn't change (inserted lines make
3123 * other lines scroll down below botline). */
3124 approximate_botline_win(wp);
3125 }
3126
3127 /* Check if any w_lines[] entries have become invalid.
3128 * For entries below the change: Correct the lnums for
3129 * inserted/deleted lines. Makes it possible to stop displaying
3130 * after the change. */
3131 for (i = 0; i < wp->w_lines_valid; ++i)
3132 if (wp->w_lines[i].wl_valid)
3133 {
3134 if (wp->w_lines[i].wl_lnum >= lnum)
3135 {
3136 if (wp->w_lines[i].wl_lnum < lnume)
3137 {
3138 /* line included in change */
3139 wp->w_lines[i].wl_valid = FALSE;
3140 }
3141 else if (xtra != 0)
3142 {
3143 /* line below change */
3144 wp->w_lines[i].wl_lnum += xtra;
3145#ifdef FEAT_FOLDING
3146 wp->w_lines[i].wl_lastlnum += xtra;
3147#endif
3148 }
3149 }
3150#ifdef FEAT_FOLDING
3151 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3152 {
3153 /* change somewhere inside this range of folded lines,
3154 * may need to be redrawn */
3155 wp->w_lines[i].wl_valid = FALSE;
3156 }
3157#endif
3158 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003159
3160#ifdef FEAT_FOLDING
3161 /* Take care of side effects for setting w_topline when folds have
3162 * changed. Esp. when the buffer was changed in another window. */
3163 if (hasAnyFolding(wp))
3164 set_topline(wp, wp->w_topline);
3165#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003166 /* relative numbering may require updating more */
3167 if (wp->w_p_rnu)
3168 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 }
3171
3172 /* Call update_screen() later, which checks out what needs to be redrawn,
3173 * since it notices b_mod_set and then uses b_mod_*. */
3174 if (must_redraw < VALID)
3175 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003176
3177#ifdef FEAT_AUTOCMD
3178 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003179 if (lnum <= curwin->w_cursor.lnum
3180 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003181 last_cursormoved.lnum = 0;
3182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183}
3184
3185/*
3186 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3187 */
3188 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003189unchanged(
3190 buf_T *buf,
3191 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003193 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003196 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (ff)
3198 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003200 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#ifdef FEAT_TITLE
3202 need_maketitle = TRUE; /* set window title later */
3203#endif
3204 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003205 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#ifdef FEAT_NETBEANS_INTG
3207 netbeans_unmodified(buf);
3208#endif
3209}
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211/*
3212 * check_status: called when the status bars for the buffer 'buf'
3213 * need to be updated
3214 */
3215 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003216check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
3218 win_T *wp;
3219
Bram Moolenaar29323592016-07-24 22:04:11 +02003220 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (wp->w_buffer == buf && wp->w_status_height)
3222 {
3223 wp->w_redr_status = TRUE;
3224 if (must_redraw < VALID)
3225 must_redraw = VALID;
3226 }
3227}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229/*
3230 * If the file is readonly, give a warning message with the first change.
3231 * Don't do this for autocommands.
3232 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003233 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003235 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 */
3237 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003238change_warning(
3239 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 mode and 'showmode' is on */
3241{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003242 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (curbuf->b_did_warn == FALSE
3245 && curbufIsChanged() == 0
3246#ifdef FEAT_AUTOCMD
3247 && !autocmd_busy
3248#endif
3249 && curbuf->b_p_ro)
3250 {
3251#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003252 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003254 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (!curbuf->b_p_ro)
3256 return;
3257#endif
3258 /*
3259 * Do what msg() does, but with a column offset if the warning should
3260 * be after the mode message.
3261 */
3262 msg_start();
3263 if (msg_row == Rows - 1)
3264 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003265 msg_source(HL_ATTR(HLF_W));
3266 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003267#ifdef FEAT_EVAL
3268 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 msg_clr_eos();
3271 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003272 if (msg_silent == 0 && !silent_mode
3273#ifdef FEAT_EVAL
3274 && time_for_testing != 1
3275#endif
3276 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
3278 out_flush();
3279 ui_delay(1000L, TRUE); /* give the user time to think about it */
3280 }
3281 curbuf->b_did_warn = TRUE;
3282 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3283 if (msg_row < Rows - 1)
3284 showmode();
3285 }
3286}
3287
3288/*
3289 * Ask for a reply from the user, a 'y' or a 'n'.
3290 * No other characters are accepted, the message is repeated until a valid
3291 * reply is entered or CTRL-C is hit.
3292 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3293 * from any buffers but directly from the user.
3294 *
3295 * return the 'y' or 'n'
3296 */
3297 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003298ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 int r = ' ';
3301 int save_State = State;
3302
3303 if (exiting) /* put terminal in raw mode for this question */
3304 settmode(TMODE_RAW);
3305 ++no_wait_return;
3306#ifdef USE_ON_FLY_SCROLL
3307 dont_scroll = TRUE; /* disallow scrolling here */
3308#endif
3309 State = CONFIRM; /* mouse behaves like with :confirm */
3310#ifdef FEAT_MOUSE
3311 setmouse(); /* disables mouse for xterm */
3312#endif
3313 ++no_mapping;
3314 ++allow_keys; /* no mapping here, but recognize keys */
3315
3316 while (r != 'y' && r != 'n')
3317 {
3318 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003319 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (direct)
3321 r = get_keystroke();
3322 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003323 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (r == Ctrl_C || r == ESC)
3325 r = 'n';
3326 msg_putchar(r); /* show what you typed */
3327 out_flush();
3328 }
3329 --no_wait_return;
3330 State = save_State;
3331#ifdef FEAT_MOUSE
3332 setmouse();
3333#endif
3334 --no_mapping;
3335 --allow_keys;
3336
3337 return r;
3338}
3339
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003340#if defined(FEAT_MOUSE) || defined(PROTO)
3341/*
3342 * Return TRUE if "c" is a mouse key.
3343 */
3344 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003345is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003346{
3347 return c == K_LEFTMOUSE
3348 || c == K_LEFTMOUSE_NM
3349 || c == K_LEFTDRAG
3350 || c == K_LEFTRELEASE
3351 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003352 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003353 || c == K_MIDDLEMOUSE
3354 || c == K_MIDDLEDRAG
3355 || c == K_MIDDLERELEASE
3356 || c == K_RIGHTMOUSE
3357 || c == K_RIGHTDRAG
3358 || c == K_RIGHTRELEASE
3359 || c == K_MOUSEDOWN
3360 || c == K_MOUSEUP
3361 || c == K_MOUSELEFT
3362 || c == K_MOUSERIGHT
3363 || c == K_X1MOUSE
3364 || c == K_X1DRAG
3365 || c == K_X1RELEASE
3366 || c == K_X2MOUSE
3367 || c == K_X2DRAG
3368 || c == K_X2RELEASE;
3369}
3370#endif
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372/*
3373 * Get a key stroke directly from the user.
3374 * Ignores mouse clicks and scrollbar events, except a click for the left
3375 * button (used at the more prompt).
3376 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3377 * Disadvantage: typeahead is ignored.
3378 * Translates the interrupt character for unix to ESC.
3379 */
3380 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003381get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003383 char_u *buf = NULL;
3384 int buflen = 150;
3385 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 int len = 0;
3387 int n;
3388 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003389 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 mapped_ctrl_c = FALSE; /* mappings are not used here */
3392 for (;;)
3393 {
3394 cursor_on();
3395 out_flush();
3396
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003397 /* Leave some room for check_termcode() to insert a key code into (max
3398 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3399 * bytes. */
3400 maxlen = (buflen - 6 - len) / 3;
3401 if (buf == NULL)
3402 buf = alloc(buflen);
3403 else if (maxlen < 10)
3404 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003405 char_u *t_buf = buf;
3406
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003407 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003408 * escape sequence. */
3409 buflen += 100;
3410 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003411 if (buf == NULL)
3412 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003413 maxlen = (buflen - 6 - len) / 3;
3414 }
3415 if (buf == NULL)
3416 {
3417 do_outofmem_msg((long_u)buflen);
3418 return ESC; /* panic! */
3419 }
3420
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003422 * terminal code to complete. */
3423 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 if (n > 0)
3425 {
3426 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003427 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003429 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003431 else if (len > 0)
3432 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
Bram Moolenaar4395a712006-09-05 18:57:57 +00003434 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003435 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003436 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003438
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003439 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003440 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003441 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003442 {
3443 /* Redrawing was postponed, do it now. */
3444 update_screen(0);
3445 setcursor(); /* put cursor back where it belongs */
3446 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003447 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003448 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003449 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003451 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 continue;
3453
3454 /* Handle modifier and/or special key code. */
3455 n = buf[0];
3456 if (n == K_SPECIAL)
3457 {
3458 n = TO_SPECIAL(buf[1], buf[2]);
3459 if (buf[1] == KS_MODIFIER
3460 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003461#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003462 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003463#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003464#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 || n == K_VER_SCROLLBAR
3466 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#endif
3468 )
3469 {
3470 if (buf[1] == KS_MODIFIER)
3471 mod_mask = buf[2];
3472 len -= 3;
3473 if (len > 0)
3474 mch_memmove(buf, buf + 3, (size_t)len);
3475 continue;
3476 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479#ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 {
3482 if (MB_BYTE2LEN(n) > len)
3483 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003484 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 n = (*mb_ptr2char)(buf);
3486 }
3487#endif
3488#ifdef UNIX
3489 if (n == intr_char)
3490 n = ESC;
3491#endif
3492 break;
3493 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003494 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 mapped_ctrl_c = save_mapped_ctrl_c;
3497 return n;
3498}
3499
3500/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003501 * Get a number from the user.
3502 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 */
3504 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003505get_number(
3506 int colon, /* allow colon to abort */
3507 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
3509 int n = 0;
3510 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003511 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003513 if (mouse_used != NULL)
3514 *mouse_used = FALSE;
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 /* When not printing messages, the user won't know what to type, return a
3517 * zero (as if CR was hit). */
3518 if (msg_silent != 0)
3519 return 0;
3520
3521#ifdef USE_ON_FLY_SCROLL
3522 dont_scroll = TRUE; /* disallow scrolling here */
3523#endif
3524 ++no_mapping;
3525 ++allow_keys; /* no mapping here, but recognize keys */
3526 for (;;)
3527 {
3528 windgoto(msg_row, msg_col);
3529 c = safe_vgetc();
3530 if (VIM_ISDIGIT(c))
3531 {
3532 n = n * 10 + c - '0';
3533 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003534 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3537 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003538 if (typed > 0)
3539 {
3540 MSG_PUTS("\b \b");
3541 --typed;
3542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003545#ifdef FEAT_MOUSE
3546 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3547 {
3548 *mouse_used = TRUE;
3549 n = mouse_row + 1;
3550 break;
3551 }
3552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 else if (n == 0 && c == ':' && colon)
3554 {
3555 stuffcharReadbuff(':');
3556 if (!exmode_active)
3557 cmdline_row = msg_row;
3558 skip_redraw = TRUE; /* skip redraw once */
3559 do_redraw = FALSE;
3560 break;
3561 }
3562 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3563 break;
3564 }
3565 --no_mapping;
3566 --allow_keys;
3567 return n;
3568}
3569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003570/*
3571 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003572 * When "mouse_used" is not NULL allow using the mouse and in that case return
3573 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003574 */
3575 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003576prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003577{
3578 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003579 int save_cmdline_row;
3580 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581
3582 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003583 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003584 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003585 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003586 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003587
Bram Moolenaar203335e2006-09-03 14:35:42 +00003588 /* Set the state such that text can be selected/copied/pasted and we still
3589 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003590 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003591 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003592 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003593 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003594
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003595 i = get_number(TRUE, mouse_used);
3596 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003597 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003598 /* don't call wait_return() now */
3599 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003600 cmdline_row = msg_row - 1;
3601 need_wait_return = FALSE;
3602 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003603 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003605 else
3606 cmdline_row = save_cmdline_row;
3607 State = save_State;
3608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 return i;
3610}
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003613msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
3615 long pn;
3616
3617 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3619 return;
3620
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003621 /* We don't want to overwrite another important message, but do overwrite
3622 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3623 * then "put" reports the last action. */
3624 if (keep_msg != NULL && !keep_msg_more)
3625 return;
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 if (n > 0)
3628 pn = n;
3629 else
3630 pn = -n;
3631
3632 if (pn > p_report)
3633 {
3634 if (pn == 1)
3635 {
3636 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003637 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3638 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003640 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3641 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643 else
3644 {
3645 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003646 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3647 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003649 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3650 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003653 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (msg(msg_buf))
3655 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003656 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003657 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 }
3660}
3661
3662/*
3663 * flush map and typeahead buffers and give a warning for an error
3664 */
3665 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003666beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 if (emsg_silent == 0)
3669 {
3670 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003671 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
3673}
3674
3675/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003676 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 */
3678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003679vim_beep(
3680 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682 if (emsg_silent == 0)
3683 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003684 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3685 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003686#ifdef ELAPSED_FUNC
3687 static int did_init = FALSE;
3688 static ELAPSED_TYPE start_tv;
3689
3690 /* Only beep once per half a second, otherwise a sequence of beeps
3691 * would freeze Vim. */
3692 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3693 {
3694 did_init = TRUE;
3695 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003697 if (p_vb
3698#ifdef FEAT_GUI
3699 /* While the GUI is starting up the termcap is set for
3700 * the GUI but the output still goes to a terminal. */
3701 && !(gui.in_use && gui.starting)
3702#endif
3703 )
3704 out_str_cf(T_VB);
3705 else
3706 out_char(BELL);
3707#ifdef ELAPSED_FUNC
3708 }
3709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003711
3712 /* When 'verbose' is set and we are sourcing a script or executing a
3713 * function give the user a hint where the beep comes from. */
3714 if (vim_strchr(p_debug, 'e') != NULL)
3715 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003716 msg_source(HL_ATTR(HLF_W));
3717 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720}
3721
3722/*
3723 * To get the "real" home directory:
3724 * - get value of $HOME
3725 * For Unix:
3726 * - go to that directory
3727 * - do mch_dirname() to get the real name of that directory.
3728 * This also works with mounts and links.
3729 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3730 */
3731static char_u *homedir = NULL;
3732
3733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003734init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
3736 char_u *var;
3737
Bram Moolenaar05159a02005-02-26 23:04:13 +00003738 /* In case we are called a second time (when 'encoding' changes). */
3739 vim_free(homedir);
3740 homedir = NULL;
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#ifdef VMS
3743 var = mch_getenv((char_u *)"SYS$LOGIN");
3744#else
3745 var = mch_getenv((char_u *)"HOME");
3746#endif
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748#ifdef WIN3264
3749 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003750 * Typically, $HOME is not defined on Windows, unless the user has
3751 * specifically defined it for Vim's sake. However, on Windows NT
3752 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3753 * each user. Try constructing $HOME from these.
3754 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003755 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003756 {
3757 char_u *homedrive, *homepath;
3758
3759 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3760 homepath = mch_getenv((char_u *)"HOMEPATH");
3761 if (homepath == NULL || *homepath == NUL)
3762 homepath = (char_u *)"\\";
3763 if (homedrive != NULL
3764 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3765 {
3766 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3767 if (NameBuff[0] != NUL)
3768 var = NameBuff;
3769 }
3770 }
3771
3772 if (var == NULL)
3773 var = mch_getenv((char_u *)"USERPROFILE");
3774
3775 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 * Weird but true: $HOME may contain an indirect reference to another
3777 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3778 * when $HOME is being set.
3779 */
3780 if (var != NULL && *var == '%')
3781 {
3782 char_u *p;
3783 char_u *exp;
3784
3785 p = vim_strchr(var + 1, '%');
3786 if (p != NULL)
3787 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003788 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 exp = mch_getenv(NameBuff);
3790 if (exp != NULL && *exp != NUL
3791 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3792 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003793 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796 }
3797 }
3798
Bram Moolenaar48340b62017-08-29 22:08:53 +02003799 if (var != NULL && *var == NUL) /* empty is same as not set */
3800 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
Bram Moolenaar48340b62017-08-29 22:08:53 +02003802# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003803 if (enc_utf8 && var != NULL)
3804 {
3805 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003806 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003807
3808 /* Convert from active codepage to UTF-8. Other conversions are
3809 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003810 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003811 if (pp != NULL)
3812 {
3813 homedir = pp;
3814 return;
3815 }
3816 }
3817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /*
3820 * Default home dir is C:/
3821 * Best assumption we can make in such a situation.
3822 */
3823 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003824 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (var != NULL)
3828 {
3829#ifdef UNIX
3830 /*
3831 * Change to the directory and get the actual path. This resolves
3832 * links. Don't do it when we can't return.
3833 */
3834 if (mch_dirname(NameBuff, MAXPATHL) == OK
3835 && mch_chdir((char *)NameBuff) == 0)
3836 {
3837 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3838 var = IObuff;
3839 if (mch_chdir((char *)NameBuff) != 0)
3840 EMSG(_(e_prev_dir));
3841 }
3842#endif
3843 homedir = vim_strsave(var);
3844 }
3845}
3846
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003847#if defined(EXITFREE) || defined(PROTO)
3848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003849free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003850{
3851 vim_free(homedir);
3852}
Bram Moolenaar24305862012-08-15 14:05:05 +02003853
3854# ifdef FEAT_CMDL_COMPL
3855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003856free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003857{
3858 ga_clear_strings(&ga_users);
3859}
3860# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003861#endif
3862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003864 * Call expand_env() and store the result in an allocated string.
3865 * This is not very memory efficient, this expects the result to be freed
3866 * again soon.
3867 */
3868 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003869expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003870{
3871 return expand_env_save_opt(src, FALSE);
3872}
3873
3874/*
3875 * Idem, but when "one" is TRUE handle the string as one file name, only
3876 * expand "~" at the start.
3877 */
3878 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003879expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003880{
3881 char_u *p;
3882
3883 p = alloc(MAXPATHL);
3884 if (p != NULL)
3885 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3886 return p;
3887}
3888
3889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * Expand environment variable with path name.
3891 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003892 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 * If anything fails no expansion is done and dst equals src.
3894 */
3895 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003896expand_env(
3897 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3898 char_u *dst, /* where to put the result */
3899 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003901 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902}
3903
3904 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003905expand_env_esc(
3906 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3907 char_u *dst, /* where to put the result */
3908 int dstlen, /* maximum length of the result */
3909 int esc, /* escape spaces in expanded variables */
3910 int one, /* "srcp" is one file name */
3911 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003913 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 char_u *tail;
3915 int c;
3916 char_u *var;
3917 int copy_char;
3918 int mustfree; /* var was allocated, need to free it later */
3919 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003920 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003922 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003923 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003924
3925 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 --dstlen; /* leave one char space for "\," */
3927 while (*src && dstlen > 0)
3928 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003929#ifdef FEAT_EVAL
3930 /* Skip over `=expr`. */
3931 if (src[0] == '`' && src[1] == '=')
3932 {
3933 size_t len;
3934
3935 var = src;
3936 src += 2;
3937 (void)skip_expr(&src);
3938 if (*src == '`')
3939 ++src;
3940 len = src - var;
3941 if (len > (size_t)dstlen)
3942 len = dstlen;
3943 vim_strncpy(dst, var, len);
3944 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003945 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003946 continue;
3947 }
3948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003950 if ((*src == '$'
3951#ifdef VMS
3952 && at_start
3953#endif
3954 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003955#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 || *src == '%'
3957#endif
3958 || (*src == '~' && at_start))
3959 {
3960 mustfree = FALSE;
3961
3962 /*
3963 * The variable name is copied into dst temporarily, because it may
3964 * be a string in read-only memory and a NUL needs to be appended.
3965 */
3966 if (*src != '~') /* environment var */
3967 {
3968 tail = src + 1;
3969 var = dst;
3970 c = dstlen - 1;
3971
3972#ifdef UNIX
3973 /* Unix has ${var-name} type environment vars */
3974 if (*tail == '{' && !vim_isIDc('{'))
3975 {
3976 tail++; /* ignore '{' */
3977 while (c-- > 0 && *tail && *tail != '}')
3978 *var++ = *tail++;
3979 }
3980 else
3981#endif
3982 {
3983 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003984#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 || (*src == '%' && *tail != '%')
3986#endif
3987 ))
3988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991 }
3992
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003993#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994# ifdef UNIX
3995 if (src[1] == '{' && *tail != '}')
3996# else
3997 if (*src == '%' && *tail != '%')
3998# endif
3999 var = NULL;
4000 else
4001 {
4002# ifdef UNIX
4003 if (src[1] == '{')
4004# else
4005 if (*src == '%')
4006#endif
4007 ++tail;
4008#endif
4009 *var = NUL;
4010 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004011#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 }
4013#endif
4014 }
4015 /* home directory */
4016 else if ( src[1] == NUL
4017 || vim_ispathsep(src[1])
4018 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4019 {
4020 var = homedir;
4021 tail = src + 1;
4022 }
4023 else /* user directory */
4024 {
4025#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4026 /*
4027 * Copy ~user to dst[], so we can put a NUL after it.
4028 */
4029 tail = src;
4030 var = dst;
4031 c = dstlen - 1;
4032 while ( c-- > 0
4033 && *tail
4034 && vim_isfilec(*tail)
4035 && !vim_ispathsep(*tail))
4036 *var++ = *tail++;
4037 *var = NUL;
4038# ifdef UNIX
4039 /*
4040 * If the system supports getpwnam(), use it.
4041 * Otherwise, or if getpwnam() fails, the shell is used to
4042 * expand ~user. This is slower and may fail if the shell
4043 * does not support ~user (old versions of /bin/sh).
4044 */
4045# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4046 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004047 /* Note: memory allocated by getpwnam() is never freed.
4048 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004049 struct passwd *pw = (*dst == NUL)
4050 ? NULL : getpwnam((char *)dst + 1);
4051
4052 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054 if (var == NULL)
4055# endif
4056 {
4057 expand_T xpc;
4058
4059 ExpandInit(&xpc);
4060 xpc.xp_context = EXPAND_FILES;
4061 var = ExpandOne(&xpc, dst, NULL,
4062 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 mustfree = TRUE;
4064 }
4065
4066# else /* !UNIX, thus VMS */
4067 /*
4068 * USER_HOME is a comma-separated list of
4069 * directories to search for the user account in.
4070 */
4071 {
4072 char_u test[MAXPATHL], paths[MAXPATHL];
4073 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004074 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 STRCPY(paths, USER_HOME);
4077 next_path = paths;
4078 while (*next_path)
4079 {
4080 for (path = next_path; *next_path && *next_path != ',';
4081 next_path++);
4082 if (*next_path)
4083 *next_path++ = NUL;
4084 STRCPY(test, path);
4085 STRCAT(test, "/");
4086 STRCAT(test, dst + 1);
4087 if (mch_stat(test, &st) == 0)
4088 {
4089 var = alloc(STRLEN(test) + 1);
4090 STRCPY(var, test);
4091 mustfree = TRUE;
4092 break;
4093 }
4094 }
4095 }
4096# endif /* UNIX */
4097#else
4098 /* cannot expand user's home directory, so don't try */
4099 var = NULL;
4100 tail = (char_u *)""; /* for gcc */
4101#endif /* UNIX || VMS */
4102 }
4103
4104#ifdef BACKSLASH_IN_FILENAME
4105 /* If 'shellslash' is set change backslashes to forward slashes.
4106 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4107 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4108 {
4109 char_u *p = vim_strsave(var);
4110
4111 if (p != NULL)
4112 {
4113 if (mustfree)
4114 vim_free(var);
4115 var = p;
4116 mustfree = TRUE;
4117 forward_slash(var);
4118 }
4119 }
4120#endif
4121
4122 /* If "var" contains white space, escape it with a backslash.
4123 * Required for ":e ~/tt" when $HOME includes a space. */
4124 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4125 {
4126 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4127
4128 if (p != NULL)
4129 {
4130 if (mustfree)
4131 vim_free(var);
4132 var = p;
4133 mustfree = TRUE;
4134 }
4135 }
4136
4137 if (var != NULL && *var != NUL
4138 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4139 {
4140 STRCPY(dst, var);
4141 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004142 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 /* if var[] ends in a path separator and tail[] starts
4144 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004145 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4147 && dst[-1] != ':'
4148#endif
4149 && vim_ispathsep(*tail))
4150 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004151 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 src = tail;
4153 copy_char = FALSE;
4154 }
4155 if (mustfree)
4156 vim_free(var);
4157 }
4158
4159 if (copy_char) /* copy at least one char */
4160 {
4161 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004162 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004163 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4164 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 */
4166 at_start = FALSE;
4167 if (src[0] == '\\' && src[1] != NUL)
4168 {
4169 *dst++ = *src++;
4170 --dstlen;
4171 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004172 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004174 if (dstlen > 0)
4175 {
4176 *dst++ = *src++;
4177 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004178
Bram Moolenaar1c864092017-08-06 18:15:45 +02004179 if (startstr != NULL && src - startstr_len >= srcp
4180 && STRNCMP(src - startstr_len, startstr,
4181 startstr_len) == 0)
4182 at_start = TRUE;
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187 *dst = NUL;
4188}
4189
4190/*
4191 * Vim's version of getenv().
4192 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004193 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004194 * "mustfree" is set to TRUE when returned is allocated, it must be
4195 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
4197 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004198vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
4200 char_u *p;
4201 char_u *pend;
4202 int vimruntime;
4203
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004204#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 /* use "C:/" when $HOME is not set */
4206 if (STRCMP(name, "HOME") == 0)
4207 return homedir;
4208#endif
4209
4210 p = mch_getenv(name);
4211 if (p != NULL && *p == NUL) /* empty is the same as not set */
4212 p = NULL;
4213
4214 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215 {
4216#if defined(FEAT_MBYTE) && defined(WIN3264)
4217 if (enc_utf8)
4218 {
4219 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004220 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004221
4222 /* Convert from active codepage to UTF-8. Other conversions are
4223 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004224 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004225 if (pp != NULL)
4226 {
4227 p = pp;
4228 *mustfree = TRUE;
4229 }
4230 }
4231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4236 if (!vimruntime && STRCMP(name, "VIM") != 0)
4237 return NULL;
4238
4239 /*
4240 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4241 * Don't do this when default_vimruntime_dir is non-empty.
4242 */
4243 if (vimruntime
4244#ifdef HAVE_PATHDEF
4245 && *default_vimruntime_dir == NUL
4246#endif
4247 )
4248 {
4249 p = mch_getenv((char_u *)"VIM");
4250 if (p != NULL && *p == NUL) /* empty is the same as not set */
4251 p = NULL;
4252 if (p != NULL)
4253 {
4254 p = vim_version_dir(p);
4255 if (p != NULL)
4256 *mustfree = TRUE;
4257 else
4258 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004259
4260#if defined(FEAT_MBYTE) && defined(WIN3264)
4261 if (enc_utf8)
4262 {
4263 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004264 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004265
4266 /* Convert from active codepage to UTF-8. Other conversions
4267 * are not done, because they would fail for non-ASCII
4268 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004269 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004270 if (pp != NULL)
4271 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004272 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004273 vim_free(p);
4274 p = pp;
4275 *mustfree = TRUE;
4276 }
4277 }
4278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281
4282 /*
4283 * When expanding $VIM or $VIMRUNTIME fails, try using:
4284 * - the directory name from 'helpfile' (unless it contains '$')
4285 * - the executable name from argv[0]
4286 */
4287 if (p == NULL)
4288 {
4289 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4290 p = p_hf;
4291#ifdef USE_EXE_NAME
4292 /*
4293 * Use the name of the executable, obtained from argv[0].
4294 */
4295 else
4296 p = exe_name;
4297#endif
4298 if (p != NULL)
4299 {
4300 /* remove the file name */
4301 pend = gettail(p);
4302
4303 /* remove "doc/" from 'helpfile', if present */
4304 if (p == p_hf)
4305 pend = remove_tail(p, pend, (char_u *)"doc");
4306
4307#ifdef USE_EXE_NAME
4308# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004309 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 if (p == exe_name)
4311 {
4312 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004313 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004315 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4316 if (pend1 != pend)
4317 {
4318 pnew = alloc((unsigned)(pend1 - p) + 15);
4319 if (pnew != NULL)
4320 {
4321 STRNCPY(pnew, p, (pend1 - p));
4322 STRCPY(pnew + (pend1 - p), "Resources/vim");
4323 p = pnew;
4324 pend = p + STRLEN(p);
4325 }
4326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328# endif
4329 /* remove "src/" from exe_name, if present */
4330 if (p == exe_name)
4331 pend = remove_tail(p, pend, (char_u *)"src");
4332#endif
4333
4334 /* for $VIM, remove "runtime/" or "vim54/", if present */
4335 if (!vimruntime)
4336 {
4337 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4338 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4339 }
4340
4341 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004342 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004345#ifdef MACOS_X
4346 if (p == exe_name || p == p_hf)
4347#endif
4348 /* check that the result is a directory name */
4349 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
4351 if (p != NULL && !mch_isdir(p))
4352 {
4353 vim_free(p);
4354 p = NULL;
4355 }
4356 else
4357 {
4358#ifdef USE_EXE_NAME
4359 /* may add "/vim54" or "/runtime" if it exists */
4360 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4361 {
4362 vim_free(p);
4363 p = pend;
4364 }
4365#endif
4366 *mustfree = TRUE;
4367 }
4368 }
4369 }
4370
4371#ifdef HAVE_PATHDEF
4372 /* When there is a pathdef.c file we can use default_vim_dir and
4373 * default_vimruntime_dir */
4374 if (p == NULL)
4375 {
4376 /* Only use default_vimruntime_dir when it is not empty */
4377 if (vimruntime && *default_vimruntime_dir != NUL)
4378 {
4379 p = default_vimruntime_dir;
4380 *mustfree = FALSE;
4381 }
4382 else if (*default_vim_dir != NUL)
4383 {
4384 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4385 *mustfree = TRUE;
4386 else
4387 {
4388 p = default_vim_dir;
4389 *mustfree = FALSE;
4390 }
4391 }
4392 }
4393#endif
4394
4395 /*
4396 * Set the environment variable, so that the new value can be found fast
4397 * next time, and others can also use it (e.g. Perl).
4398 */
4399 if (p != NULL)
4400 {
4401 if (vimruntime)
4402 {
4403 vim_setenv((char_u *)"VIMRUNTIME", p);
4404 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 else
4407 {
4408 vim_setenv((char_u *)"VIM", p);
4409 didset_vim = TRUE;
4410 }
4411 }
4412 return p;
4413}
4414
4415/*
4416 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4417 * Return NULL if not, return its name in allocated memory otherwise.
4418 */
4419 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
4422 char_u *p;
4423
4424 if (vimdir == NULL || *vimdir == NUL)
4425 return NULL;
4426 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4427 if (p != NULL && mch_isdir(p))
4428 return p;
4429 vim_free(p);
4430 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4431 if (p != NULL && mch_isdir(p))
4432 return p;
4433 vim_free(p);
4434 return NULL;
4435}
4436
4437/*
4438 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4439 * the length of "name/". Otherwise return "pend".
4440 */
4441 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004442remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 int len = (int)STRLEN(name) + 1;
4445 char_u *newend = pend - len;
4446
4447 if (newend >= p
4448 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004449 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 return newend;
4451 return pend;
4452}
4453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 * Our portable version of setenv.
4456 */
4457 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004458vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
4460#ifdef HAVE_SETENV
4461 mch_setenv((char *)name, (char *)val, 1);
4462#else
4463 char_u *envbuf;
4464
4465 /*
4466 * Putenv does not copy the string, it has to remain
4467 * valid. The allocated memory will never be freed.
4468 */
4469 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4470 if (envbuf != NULL)
4471 {
4472 sprintf((char *)envbuf, "%s=%s", name, val);
4473 putenv((char *)envbuf);
4474 }
4475#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004476#ifdef FEAT_GETTEXT
4477 /*
4478 * When setting $VIMRUNTIME adjust the directory to find message
4479 * translations to $VIMRUNTIME/lang.
4480 */
4481 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4482 {
4483 char_u *buf = concat_str(val, (char_u *)"/lang");
4484
4485 if (buf != NULL)
4486 {
4487 bindtextdomain(VIMPACKAGE, (char *)buf);
4488 vim_free(buf);
4489 }
4490 }
4491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492}
4493
4494#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4495/*
4496 * Function given to ExpandGeneric() to obtain an environment variable name.
4497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004499get_env_name(
4500 expand_T *xp UNUSED,
4501 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502{
Bram Moolenaard0573012017-10-28 21:11:06 +02004503# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004505 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 */
4507 return NULL;
4508# else
4509# ifndef __WIN32__
4510 /* Borland C++ 5.2 has this in a header file. */
4511 extern char **environ;
4512# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004513# define ENVNAMELEN 100
4514 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 char_u *str;
4516 int n;
4517
4518 str = (char_u *)environ[idx];
4519 if (str == NULL)
4520 return NULL;
4521
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004522 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 if (str[n] == '=' || str[n] == NUL)
4525 break;
4526 name[n] = str[n];
4527 }
4528 name[n] = NUL;
4529 return name;
4530# endif
4531}
Bram Moolenaar24305862012-08-15 14:05:05 +02004532
4533/*
4534 * Find all user names for user completion.
4535 * Done only once and then cached.
4536 */
4537 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004538init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004539{
Bram Moolenaar24305862012-08-15 14:05:05 +02004540 static int lazy_init_done = FALSE;
4541
4542 if (lazy_init_done)
4543 return;
4544
4545 lazy_init_done = TRUE;
4546 ga_init2(&ga_users, sizeof(char_u *), 20);
4547
4548# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4549 {
4550 char_u* user;
4551 struct passwd* pw;
4552
4553 setpwent();
4554 while ((pw = getpwent()) != NULL)
4555 /* pw->pw_name shouldn't be NULL but just in case... */
4556 if (pw->pw_name != NULL)
4557 {
4558 if (ga_grow(&ga_users, 1) == FAIL)
4559 break;
4560 user = vim_strsave((char_u*)pw->pw_name);
4561 if (user == NULL)
4562 break;
4563 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4564 }
4565 endpwent();
4566 }
4567# endif
4568}
4569
4570/*
4571 * Function given to ExpandGeneric() to obtain an user names.
4572 */
4573 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004574get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004575{
4576 init_users();
4577 if (idx < ga_users.ga_len)
4578 return ((char_u **)ga_users.ga_data)[idx];
4579 return NULL;
4580}
4581
4582/*
4583 * Check whether name matches a user name. Return:
4584 * 0 if name does not match any user name.
4585 * 1 if name partially matches the beginning of a user name.
4586 * 2 is name fully matches a user name.
4587 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004588int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004589{
4590 int i;
4591 int n = (int)STRLEN(name);
4592 int result = 0;
4593
4594 init_users();
4595 for (i = 0; i < ga_users.ga_len; i++)
4596 {
4597 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4598 return 2; /* full match */
4599 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4600 result = 1; /* partial match */
4601 }
4602 return result;
4603}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604#endif
4605
4606/*
4607 * Replace home directory by "~" in each space or comma separated file name in
4608 * 'src'.
4609 * If anything fails (except when out of space) dst equals src.
4610 */
4611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004612home_replace(
4613 buf_T *buf, /* when not NULL, check for help files */
4614 char_u *src, /* input file name */
4615 char_u *dst, /* where to put the result */
4616 int dstlen, /* maximum length of the result */
4617 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 spaces and commas in the file name. */
4619{
4620 size_t dirlen = 0, envlen = 0;
4621 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004622 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 char_u *p;
4624
4625 if (src == NULL)
4626 {
4627 *dst = NUL;
4628 return;
4629 }
4630
4631 /*
4632 * If the file is a help file, remove the path completely.
4633 */
4634 if (buf != NULL && buf->b_help)
4635 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004636 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 return;
4638 }
4639
4640 /*
4641 * We check both the value of the $HOME environment variable and the
4642 * "real" home directory.
4643 */
4644 if (homedir != NULL)
4645 dirlen = STRLEN(homedir);
4646
4647#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004648 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004650 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4651#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004652#ifdef WIN3264
4653 if (homedir_env == NULL)
4654 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4655#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004656 /* Empty is the same as not set. */
4657 if (homedir_env != NULL && *homedir_env == NUL)
4658 homedir_env = NULL;
4659
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004660#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004661 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004662 {
4663 int usedlen = 0;
4664 int flen;
4665 char_u *fbuf = NULL;
4666
4667 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004668 (void)modify_fname((char_u *)":p", &usedlen,
4669 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004670 flen = (int)STRLEN(homedir_env);
4671 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4672 /* Remove the trailing / that is added to a directory. */
4673 homedir_env[flen - 1] = NUL;
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675#endif
4676
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 if (homedir_env != NULL)
4678 envlen = STRLEN(homedir_env);
4679
4680 if (!one)
4681 src = skipwhite(src);
4682 while (*src && dstlen > 0)
4683 {
4684 /*
4685 * Here we are at the beginning of a file name.
4686 * First, check to see if the beginning of the file name matches
4687 * $HOME or the "real" home directory. Check that there is a '/'
4688 * after the match (so that if e.g. the file is "/home/pieter/bla",
4689 * and the home directory is "/home/piet", the file does not end up
4690 * as "~er/bla" (which would seem to indicate the file "bla" in user
4691 * er's home directory)).
4692 */
4693 p = homedir;
4694 len = dirlen;
4695 for (;;)
4696 {
4697 if ( len
4698 && fnamencmp(src, p, len) == 0
4699 && (vim_ispathsep(src[len])
4700 || (!one && (src[len] == ',' || src[len] == ' '))
4701 || src[len] == NUL))
4702 {
4703 src += len;
4704 if (--dstlen > 0)
4705 *dst++ = '~';
4706
4707 /*
4708 * If it's just the home directory, add "/".
4709 */
4710 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4711 *dst++ = '/';
4712 break;
4713 }
4714 if (p == homedir_env)
4715 break;
4716 p = homedir_env;
4717 len = envlen;
4718 }
4719
4720 /* if (!one) skip to separator: space or comma */
4721 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4722 *dst++ = *src++;
4723 /* skip separator */
4724 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4725 *dst++ = *src++;
4726 }
4727 /* if (dstlen == 0) out of space, what to do??? */
4728
4729 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004730
4731 if (homedir_env != homedir_env_orig)
4732 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733}
4734
4735/*
4736 * Like home_replace, store the replaced string in allocated memory.
4737 * When something fails, NULL is returned.
4738 */
4739 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004740home_replace_save(
4741 buf_T *buf, /* when not NULL, check for help files */
4742 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 char_u *dst;
4745 unsigned len;
4746
4747 len = 3; /* space for "~/" and trailing NUL */
4748 if (src != NULL) /* just in case */
4749 len += (unsigned)STRLEN(src);
4750 dst = alloc(len);
4751 if (dst != NULL)
4752 home_replace(buf, src, dst, len, TRUE);
4753 return dst;
4754}
4755
4756/*
4757 * Compare two file names and return:
4758 * FPC_SAME if they both exist and are the same file.
4759 * FPC_SAMEX if they both don't exist and have the same file name.
4760 * FPC_DIFF if they both exist and are different files.
4761 * FPC_NOTX if they both don't exist.
4762 * FPC_DIFFX if one of them doesn't exist.
4763 * For the first name environment variables are expanded
4764 */
4765 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004766fullpathcmp(
4767 char_u *s1,
4768 char_u *s2,
4769 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771#ifdef UNIX
4772 char_u exp1[MAXPATHL];
4773 char_u full1[MAXPATHL];
4774 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004775 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 int r1, r2;
4777
4778 expand_env(s1, exp1, MAXPATHL);
4779 r1 = mch_stat((char *)exp1, &st1);
4780 r2 = mch_stat((char *)s2, &st2);
4781 if (r1 != 0 && r2 != 0)
4782 {
4783 /* if mch_stat() doesn't work, may compare the names */
4784 if (checkname)
4785 {
4786 if (fnamecmp(exp1, s2) == 0)
4787 return FPC_SAMEX;
4788 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4789 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4790 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4791 return FPC_SAMEX;
4792 }
4793 return FPC_NOTX;
4794 }
4795 if (r1 != 0 || r2 != 0)
4796 return FPC_DIFFX;
4797 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4798 return FPC_SAME;
4799 return FPC_DIFF;
4800#else
4801 char_u *exp1; /* expanded s1 */
4802 char_u *full1; /* full path of s1 */
4803 char_u *full2; /* full path of s2 */
4804 int retval = FPC_DIFF;
4805 int r1, r2;
4806
4807 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4808 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4809 {
4810 full1 = exp1 + MAXPATHL;
4811 full2 = full1 + MAXPATHL;
4812
4813 expand_env(s1, exp1, MAXPATHL);
4814 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4815 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4816
4817 /* If vim_FullName() fails, the file probably doesn't exist. */
4818 if (r1 != OK && r2 != OK)
4819 {
4820 if (checkname && fnamecmp(exp1, s2) == 0)
4821 retval = FPC_SAMEX;
4822 else
4823 retval = FPC_NOTX;
4824 }
4825 else if (r1 != OK || r2 != OK)
4826 retval = FPC_DIFFX;
4827 else if (fnamecmp(full1, full2))
4828 retval = FPC_DIFF;
4829 else
4830 retval = FPC_SAME;
4831 vim_free(exp1);
4832 }
4833 return retval;
4834#endif
4835}
4836
4837/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004838 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004839 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004840 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 */
4842 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004843gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
4845 char_u *p1, *p2;
4846
4847 if (fname == NULL)
4848 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004849 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004851 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004853 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 return p1;
4856}
4857
Bram Moolenaar31710262010-08-13 13:36:15 +02004858#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004859static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004860
4861/*
4862 * Return the end of the directory name, on the first path
4863 * separator:
4864 * "/path/file", "/path/dir/", "/path//dir", "/file"
4865 * ^ ^ ^ ^
4866 */
4867 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004868gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004869{
4870 char_u *dir_end = fname;
4871 char_u *next_dir_end = fname;
4872 int look_for_sep = TRUE;
4873 char_u *p;
4874
4875 for (p = fname; *p != NUL; )
4876 {
4877 if (vim_ispathsep(*p))
4878 {
4879 if (look_for_sep)
4880 {
4881 next_dir_end = p;
4882 look_for_sep = FALSE;
4883 }
4884 }
4885 else
4886 {
4887 if (!look_for_sep)
4888 dir_end = next_dir_end;
4889 look_for_sep = TRUE;
4890 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004891 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004892 }
4893 return dir_end;
4894}
4895#endif
4896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004898 * Get pointer to tail of "fname", including path separators. Putting a NUL
4899 * here leaves the directory name. Takes care of "c:/" and "//".
4900 * Always returns a valid pointer.
4901 */
4902 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004903gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004904{
4905 char_u *p;
4906 char_u *t;
4907
4908 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4909 t = gettail(fname);
4910 while (t > p && after_pathsep(fname, t))
4911 --t;
4912#ifdef VMS
4913 /* path separator is part of the path */
4914 ++t;
4915#endif
4916 return t;
4917}
4918
4919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 * get the next path component (just after the next path separator).
4921 */
4922 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004923getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924{
4925 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004926 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (*fname)
4928 ++fname;
4929 return fname;
4930}
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932/*
4933 * Get a pointer to one character past the head of a path name.
4934 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4935 * If there is no head, path is returned.
4936 */
4937 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004938get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 char_u *retval;
4941
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004942#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 /* may skip "c:" */
4944 if (isalpha(path[0]) && path[1] == ':')
4945 retval = path + 2;
4946 else
4947 retval = path;
4948#else
4949# if defined(AMIGA)
4950 /* may skip "label:" */
4951 retval = vim_strchr(path, ':');
4952 if (retval == NULL)
4953 retval = path;
4954# else /* Unix */
4955 retval = path;
4956# endif
4957#endif
4958
4959 while (vim_ispathsep(*retval))
4960 ++retval;
4961
4962 return retval;
4963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
4965/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004966 * Return TRUE if 'c' is a path separator.
4967 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 */
4969 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004970vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004972#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004974#else
4975# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004977# else
4978# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4980 return (c == ':' || c == '[' || c == ']' || c == '/'
4981 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004982# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004984# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987}
4988
Bram Moolenaar69c35002013-11-04 02:54:12 +01004989/*
4990 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4991 */
4992 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004993vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004994{
4995 return vim_ispathsep(c)
4996#ifdef BACKSLASH_IN_FILENAME
4997 && c != ':'
4998#endif
4999 ;
5000}
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5003/*
5004 * return TRUE if 'c' is a path list separator.
5005 */
5006 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005007vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
5009#ifdef UNIX
5010 return (c == ':');
5011#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005012 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013#endif
5014}
5015#endif
5016
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005017/*
5018 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5019 * It's done in-place.
5020 */
5021 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005022shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005023{
5024 char_u *tail, *s, *d;
5025 int skip = FALSE;
5026
5027 tail = gettail(str);
5028 d = str;
5029 for (s = str; ; ++s)
5030 {
5031 if (s >= tail) /* copy the whole tail */
5032 {
5033 *d++ = *s;
5034 if (*s == NUL)
5035 break;
5036 }
5037 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5038 {
5039 *d++ = *s;
5040 skip = FALSE;
5041 }
5042 else if (!skip)
5043 {
5044 *d++ = *s; /* copy next char */
5045 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5046 skip = TRUE;
5047# ifdef FEAT_MBYTE
5048 if (has_mbyte)
5049 {
5050 int l = mb_ptr2len(s);
5051
5052 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005053 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005054 }
5055# endif
5056 }
5057 }
5058}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005059
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005060/*
5061 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5062 * Also returns TRUE if there is no directory name.
5063 * "fname" must be writable!.
5064 */
5065 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005066dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005067{
5068 char_u *p;
5069 int c;
5070 int retval;
5071
5072 p = gettail_sep(fname);
5073 if (p == fname)
5074 return TRUE;
5075 c = *p;
5076 *p = NUL;
5077 retval = mch_isdir(fname);
5078 *p = c;
5079 return retval;
5080}
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005083 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5084 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 */
5086 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005087vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005089#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005091#else
5092 if (p_fic)
5093 return MB_STRICMP(x, y);
5094 return STRCMP(x, y);
5095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096}
5097
5098 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005099vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005101#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005102 char_u *px = x;
5103 char_u *py = y;
5104 int cx = NUL;
5105 int cy = NUL;
5106
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005107 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005109 cx = PTR2CHAR(px);
5110 cy = PTR2CHAR(py);
5111 if (cx == NUL || cy == NUL
5112 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5113 && !(cx == '/' && cy == '\\')
5114 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005116 len -= MB_PTR2LEN(px);
5117 px += MB_PTR2LEN(px);
5118 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 if (len == 0)
5121 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005122 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005123#else
5124 if (p_fic)
5125 return MB_STRNICMP(x, y, len);
5126 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005128}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
5130/*
5131 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005132 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 */
5134 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005135concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
5137 char_u *dest;
5138
5139 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5140 if (dest != NULL)
5141 {
5142 STRCPY(dest, fname1);
5143 if (sep)
5144 add_pathsep(dest);
5145 STRCAT(dest, fname2);
5146 }
5147 return dest;
5148}
5149
Bram Moolenaard6754642005-01-17 22:18:45 +00005150/*
5151 * Concatenate two strings and return the result in allocated memory.
5152 * Returns NULL when out of memory.
5153 */
5154 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005155concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005156{
5157 char_u *dest;
5158 size_t l = STRLEN(str1);
5159
5160 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5161 if (dest != NULL)
5162 {
5163 STRCPY(dest, str1);
5164 STRCPY(dest + l, str2);
5165 }
5166 return dest;
5167}
Bram Moolenaard6754642005-01-17 22:18:45 +00005168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169/*
5170 * Add a path separator to a file name, unless it already ends in a path
5171 * separator.
5172 */
5173 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005174add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005176 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 STRCAT(p, PATHSEPSTR);
5178}
5179
5180/*
5181 * FullName_save - Make an allocated copy of a full file name.
5182 * Returns NULL when out of memory.
5183 */
5184 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005185FullName_save(
5186 char_u *fname,
5187 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005188 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 char_u *buf;
5191 char_u *new_fname = NULL;
5192
5193 if (fname == NULL)
5194 return NULL;
5195
5196 buf = alloc((unsigned)MAXPATHL);
5197 if (buf != NULL)
5198 {
5199 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5200 new_fname = vim_strsave(buf);
5201 else
5202 new_fname = vim_strsave(fname);
5203 vim_free(buf);
5204 }
5205 return new_fname;
5206}
5207
5208#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5209
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005210static char_u *skip_string(char_u *p);
5211static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005212static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005213static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215/*
5216 * Find the start of a comment, not knowing if we are in a comment right now.
5217 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005218 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005220 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005221ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005222{
5223 return find_start_comment(curbuf->b_ind_maxcomment);
5224}
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005227find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
5229 pos_T *pos;
5230 char_u *line;
5231 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005232 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005234 for (;;)
5235 {
5236 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5237 if (pos == NULL)
5238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005240 /*
5241 * Check if the comment start we found is inside a string.
5242 * If it is then restrict the search to below this line and try again.
5243 */
5244 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005245 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005246 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005247 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005248 break;
5249 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5250 if (cur_maxcomment <= 0)
5251 {
5252 pos = NULL;
5253 break;
5254 }
5255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 return pos;
5257}
5258
5259/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005260 * Find the start of a comment or raw string, not knowing if we are in a
5261 * comment or raw string right now.
5262 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005263 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005264 * Return NULL when not inside a comment or raw string.
5265 * "CORS" -> Comment Or Raw String
5266 */
5267 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005268ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005269{
Bram Moolenaar089af182015-10-07 11:41:49 +02005270 static pos_T comment_pos_copy;
5271 pos_T *comment_pos;
5272 pos_T *rs_pos;
5273
5274 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5275 if (comment_pos != NULL)
5276 {
5277 /* Need to make a copy of the static pos in findmatchlimit(),
5278 * calling find_start_rawstring() may change it. */
5279 comment_pos_copy = *comment_pos;
5280 comment_pos = &comment_pos_copy;
5281 }
5282 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005283
5284 /* If comment_pos is before rs_pos the raw string is inside the comment.
5285 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005286 if (comment_pos == NULL || (rs_pos != NULL
5287 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005288 {
5289 if (is_raw != NULL && rs_pos != NULL)
5290 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005291 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005292 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005293 return comment_pos;
5294}
5295
5296/*
5297 * Find the start of a raw string, not knowing if we are in one right now.
5298 * Search starts at w_cursor.lnum and goes backwards.
5299 * Return NULL when not inside a raw string.
5300 */
5301 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005302find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005303{
5304 pos_T *pos;
5305 char_u *line;
5306 char_u *p;
5307 int cur_maxcomment = ind_maxcomment;
5308
5309 for (;;)
5310 {
5311 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5312 if (pos == NULL)
5313 break;
5314
5315 /*
5316 * Check if the raw string start we found is inside a string.
5317 * If it is then restrict the search to below this line and try again.
5318 */
5319 line = ml_get(pos->lnum);
5320 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5321 p = skip_string(p);
5322 if ((colnr_T)(p - line) <= pos->col)
5323 break;
5324 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5325 if (cur_maxcomment <= 0)
5326 {
5327 pos = NULL;
5328 break;
5329 }
5330 }
5331 return pos;
5332}
5333
5334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 * Skip to the end of a "string" and a 'c' character.
5336 * If there is no string or character, return argument unmodified.
5337 */
5338 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005339skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
5341 int i;
5342
5343 /*
5344 * We loop, because strings may be concatenated: "date""time".
5345 */
5346 for ( ; ; ++p)
5347 {
5348 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5349 {
5350 if (!p[1]) /* ' at end of line */
5351 break;
5352 i = 2;
5353 if (p[1] == '\\') /* '\n' or '\000' */
5354 {
5355 ++i;
5356 while (vim_isdigit(p[i - 1])) /* '\000' */
5357 ++i;
5358 }
5359 if (p[i] == '\'') /* check for trailing ' */
5360 {
5361 p += i;
5362 continue;
5363 }
5364 }
5365 else if (p[0] == '"') /* start of string */
5366 {
5367 for (++p; p[0]; ++p)
5368 {
5369 if (p[0] == '\\' && p[1] != NUL)
5370 ++p;
5371 else if (p[0] == '"') /* end of string */
5372 break;
5373 }
5374 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005375 continue; /* continue for another string */
5376 }
5377 else if (p[0] == 'R' && p[1] == '"')
5378 {
5379 /* Raw string: R"[delim](...)[delim]" */
5380 char_u *delim = p + 2;
5381 char_u *paren = vim_strchr(delim, '(');
5382
5383 if (paren != NULL)
5384 {
5385 size_t delim_len = paren - delim;
5386
5387 for (p += 3; *p; ++p)
5388 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5389 && p[delim_len + 1] == '"')
5390 {
5391 p += delim_len + 1;
5392 break;
5393 }
5394 if (p[0] == '"')
5395 continue; /* continue for another string */
5396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 break; /* no string found */
5399 }
5400 if (!*p)
5401 --p; /* backup from NUL */
5402 return p;
5403}
5404#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5405
5406#if defined(FEAT_CINDENT) || defined(PROTO)
5407
5408/*
5409 * Do C or expression indenting on the current line.
5410 */
5411 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005412do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414# ifdef FEAT_EVAL
5415 if (*curbuf->b_p_inde != NUL)
5416 fixthisline(get_expr_indent);
5417 else
5418# endif
5419 fixthisline(get_c_indent);
5420}
5421
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005422/* Find result cache for cpp_baseclass */
5423typedef struct {
5424 int found;
5425 lpos_T lpos;
5426} cpp_baseclass_cache_T;
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428/*
5429 * Functions for C-indenting.
5430 * Most of this originally comes from Eric Fischer.
5431 */
5432/*
5433 * Below "XXX" means that this function may unlock the current line.
5434 */
5435
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005436static char_u *cin_skipcomment(char_u *);
5437static int cin_nocode(char_u *);
5438static pos_T *find_line_comment(void);
5439static int cin_has_js_key(char_u *text);
5440static int cin_islabel_skip(char_u **);
5441static int cin_isdefault(char_u *);
5442static char_u *after_label(char_u *l);
5443static int get_indent_nolabel(linenr_T lnum);
5444static int skip_label(linenr_T, char_u **pp);
5445static int cin_first_id_amount(void);
5446static int cin_get_equal_amount(linenr_T lnum);
5447static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005448static int cin_iscomment(char_u *);
5449static int cin_islinecomment(char_u *);
5450static int cin_isterminated(char_u *, int, int);
5451static int cin_isinit(void);
5452static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5453static int cin_isif(char_u *);
5454static int cin_iselse(char_u *);
5455static int cin_isdo(char_u *);
5456static int cin_iswhileofdo(char_u *, linenr_T);
5457static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5458static int cin_iswhileofdo_end(int terminated);
5459static int cin_isbreak(char_u *);
5460static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5461static int get_baseclass_amount(int col);
5462static int cin_ends_in(char_u *, char_u *, char_u *);
5463static int cin_starts_with(char_u *s, char *word);
5464static int cin_skip2pos(pos_T *trypos);
5465static pos_T *find_start_brace(void);
5466static pos_T *find_match_paren(int);
5467static pos_T *find_match_char(int c, int ind_maxparen);
5468static int corr_ind_maxparen(pos_T *startpos);
5469static int find_last_paren(char_u *l, int start, int end);
5470static int find_match(int lookfor, linenr_T ourscope);
5471static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473/*
5474 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005475 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 */
5477 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005478cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 while (*s)
5481 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005482 char_u *prev_s = s;
5483
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005485
5486 /* Perl/shell # comment comment continues until eol. Require a space
5487 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005488 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005489 {
5490 s += STRLEN(s);
5491 break;
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (*s != '/')
5494 break;
5495 ++s;
5496 if (*s == '/') /* slash-slash comment continues till eol */
5497 {
5498 s += STRLEN(s);
5499 break;
5500 }
5501 if (*s != '*')
5502 break;
5503 for (++s; *s; ++s) /* skip slash-star comment */
5504 if (s[0] == '*' && s[1] == '/')
5505 {
5506 s += 2;
5507 break;
5508 }
5509 }
5510 return s;
5511}
5512
5513/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005514 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * not considered code.
5516 */
5517 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005518cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 return *cin_skipcomment(s) == NUL;
5521}
5522
5523/*
5524 * Check previous lines for a "//" line comment, skipping over blank lines.
5525 */
5526 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 static pos_T pos;
5530 char_u *line;
5531 char_u *p;
5532
5533 pos = curwin->w_cursor;
5534 while (--pos.lnum > 0)
5535 {
5536 line = ml_get(pos.lnum);
5537 p = skipwhite(line);
5538 if (cin_islinecomment(p))
5539 {
5540 pos.col = (int)(p - line);
5541 return &pos;
5542 }
5543 if (*p != NUL)
5544 break;
5545 }
5546 return NULL;
5547}
5548
5549/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005550 * Return TRUE if "text" starts with "key:".
5551 */
5552 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005553cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005554{
5555 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005556 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005557
5558 if (*s == '\'' || *s == '"')
5559 {
5560 /* can be 'key': or "key": */
5561 quote = *s;
5562 ++s;
5563 }
5564 if (!vim_isIDc(*s)) /* need at least one ID character */
5565 return FALSE;
5566
5567 while (vim_isIDc(*s))
5568 ++s;
5569 if (*s == quote)
5570 ++s;
5571
5572 s = cin_skipcomment(s);
5573
5574 /* "::" is not a label, it's C++ */
5575 return (*s == ':' && s[1] != ':');
5576}
5577
5578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005580 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 */
5582 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005583cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
5585 if (!vim_isIDc(**s)) /* need at least one ID character */
5586 return FALSE;
5587
5588 while (vim_isIDc(**s))
5589 (*s)++;
5590
5591 *s = cin_skipcomment(*s);
5592
5593 /* "::" is not a label, it's C++ */
5594 return (**s == ':' && *++*s != ':');
5595}
5596
5597/*
5598 * Recognize a label: "label:".
5599 * Note: curwin->w_cursor must be where we are looking for the label.
5600 */
5601 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005602cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 char_u *s;
5605
5606 s = cin_skipcomment(ml_get_curline());
5607
5608 /*
5609 * Exclude "default" from labels, since it should be indented
5610 * like a switch label. Same for C++ scope declarations.
5611 */
5612 if (cin_isdefault(s))
5613 return FALSE;
5614 if (cin_isscopedecl(s))
5615 return FALSE;
5616
5617 if (cin_islabel_skip(&s))
5618 {
5619 /*
5620 * Only accept a label if the previous line is terminated or is a case
5621 * label.
5622 */
5623 pos_T cursor_save;
5624 pos_T *trypos;
5625 char_u *line;
5626
5627 cursor_save = curwin->w_cursor;
5628 while (curwin->w_cursor.lnum > 1)
5629 {
5630 --curwin->w_cursor.lnum;
5631
5632 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005633 * If we're in a comment or raw string now, skip to the start of
5634 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 */
5636 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005637 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 curwin->w_cursor = *trypos;
5639
5640 line = ml_get_curline();
5641 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5642 continue;
5643 if (*(line = cin_skipcomment(line)) == NUL)
5644 continue;
5645
5646 curwin->w_cursor = cursor_save;
5647 if (cin_isterminated(line, TRUE, FALSE)
5648 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005649 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 || (cin_islabel_skip(&line) && cin_nocode(line)))
5651 return TRUE;
5652 return FALSE;
5653 }
5654 curwin->w_cursor = cursor_save;
5655 return TRUE; /* label at start of file??? */
5656 }
5657 return FALSE;
5658}
5659
5660/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005661 * Recognize structure initialization and enumerations:
5662 * "[typedef] [static|public|protected|private] enum"
5663 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 */
5665 static int
5666cin_isinit(void)
5667{
5668 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005669 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671 s = cin_skipcomment(ml_get_curline());
5672
Bram Moolenaar75342212013-03-07 13:13:52 +01005673 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 s = cin_skipcomment(s + 7);
5675
Bram Moolenaar75342212013-03-07 13:13:52 +01005676 for (;;)
5677 {
5678 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005679
Bram Moolenaar75342212013-03-07 13:13:52 +01005680 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5681 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005682 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005683 if (cin_starts_with(s, skip[i]))
5684 {
5685 s = cin_skipcomment(s + l);
5686 l = 0;
5687 break;
5688 }
5689 }
5690 if (l != 0)
5691 break;
5692 }
5693
5694 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 return TRUE;
5696
5697 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5698 return TRUE;
5699
5700 return FALSE;
5701}
5702
5703/*
5704 * Recognize a switch label: "case .*:" or "default:".
5705 */
5706 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005707cin_iscase(
5708 char_u *s,
5709 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005712 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 for (s += 4; *s; ++s)
5715 {
5716 s = cin_skipcomment(s);
5717 if (*s == ':')
5718 {
5719 if (s[1] == ':') /* skip over "::" for C++ */
5720 ++s;
5721 else
5722 return TRUE;
5723 }
5724 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005725 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5727 return FALSE; /* stop at comment */
5728 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005729 {
5730 /* JS etc. */
5731 if (strict)
5732 return FALSE; /* stop at string */
5733 else
5734 return TRUE;
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737 return FALSE;
5738 }
5739
5740 if (cin_isdefault(s))
5741 return TRUE;
5742 return FALSE;
5743}
5744
5745/*
5746 * Recognize a "default" switch label.
5747 */
5748 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005749cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 return (STRNCMP(s, "default", 7) == 0
5752 && *(s = cin_skipcomment(s + 7)) == ':'
5753 && s[1] != ':');
5754}
5755
5756/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005757 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 */
5759 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005760cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
5762 int i;
5763
5764 s = cin_skipcomment(s);
5765 if (STRNCMP(s, "public", 6) == 0)
5766 i = 6;
5767 else if (STRNCMP(s, "protected", 9) == 0)
5768 i = 9;
5769 else if (STRNCMP(s, "private", 7) == 0)
5770 i = 7;
5771 else
5772 return FALSE;
5773 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5774}
5775
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005776/* Maximum number of lines to search back for a "namespace" line. */
5777#define FIND_NAMESPACE_LIM 20
5778
5779/*
5780 * Recognize a "namespace" scope declaration.
5781 */
5782 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005783cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005784{
5785 char_u *p;
5786 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005787 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005788
5789 s = cin_skipcomment(s);
5790 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5791 {
5792 p = cin_skipcomment(skipwhite(s + 9));
5793 while (*p != NUL)
5794 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005795 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005796 {
5797 has_name = TRUE; /* found end of a name */
5798 p = cin_skipcomment(skipwhite(p));
5799 }
5800 else if (*p == '{')
5801 {
5802 break;
5803 }
5804 else if (vim_iswordc(*p))
5805 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005806 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005807 if (has_name)
5808 return FALSE; /* word character after skipping past name */
5809 ++p;
5810 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005811 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5812 {
5813 if (!has_name_start || has_name)
5814 return FALSE;
5815 /* C++ 17 nested namespace */
5816 p += 3;
5817 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005818 else
5819 {
5820 return FALSE;
5821 }
5822 }
5823 return TRUE;
5824 }
5825 return FALSE;
5826}
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005829 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5830 */
5831 static int
5832cin_is_cpp_extern_c(char_u *s)
5833{
5834 char_u *p;
5835 int has_string_literal = FALSE;
5836
5837 s = cin_skipcomment(s);
5838 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5839 {
5840 p = cin_skipcomment(skipwhite(s + 6));
5841 while (*p != NUL)
5842 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005843 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005844 {
5845 p = cin_skipcomment(skipwhite(p));
5846 }
5847 else if (*p == '{')
5848 {
5849 break;
5850 }
5851 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5852 {
5853 if (has_string_literal)
5854 return FALSE;
5855 has_string_literal = TRUE;
5856 p += 3;
5857 }
5858 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5859 && p[4] == '"')
5860 {
5861 if (has_string_literal)
5862 return FALSE;
5863 has_string_literal = TRUE;
5864 p += 5;
5865 }
5866 else
5867 {
5868 return FALSE;
5869 }
5870 }
5871 return has_string_literal ? TRUE : FALSE;
5872 }
5873 return FALSE;
5874}
5875
5876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 * Return a pointer to the first non-empty non-comment character after a ':'.
5878 * Return NULL if not found.
5879 * case 234: a = b;
5880 * ^
5881 */
5882 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005883after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
5885 for ( ; *l; ++l)
5886 {
5887 if (*l == ':')
5888 {
5889 if (l[1] == ':') /* skip over "::" for C++ */
5890 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005891 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 break;
5893 }
5894 else if (*l == '\'' && l[1] && l[2] == '\'')
5895 l += 2; /* skip over 'x' */
5896 }
5897 if (*l == NUL)
5898 return NULL;
5899 l = cin_skipcomment(l + 1);
5900 if (*l == NUL)
5901 return NULL;
5902 return l;
5903}
5904
5905/*
5906 * Get indent of line "lnum", skipping a label.
5907 * Return 0 if there is nothing after the label.
5908 */
5909 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005910get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
5912 char_u *l;
5913 pos_T fp;
5914 colnr_T col;
5915 char_u *p;
5916
5917 l = ml_get(lnum);
5918 p = after_label(l);
5919 if (p == NULL)
5920 return 0;
5921
5922 fp.col = (colnr_T)(p - l);
5923 fp.lnum = lnum;
5924 getvcol(curwin, &fp, &col, NULL, NULL);
5925 return (int)col;
5926}
5927
5928/*
5929 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005930 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 * label: if (asdf && asdfasdf)
5932 * ^
5933 */
5934 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005935skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 char_u *l;
5938 int amount;
5939 pos_T cursor_save;
5940
5941 cursor_save = curwin->w_cursor;
5942 curwin->w_cursor.lnum = lnum;
5943 l = ml_get_curline();
5944 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005945 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 {
5947 amount = get_indent_nolabel(lnum);
5948 l = after_label(ml_get_curline());
5949 if (l == NULL) /* just in case */
5950 l = ml_get_curline();
5951 }
5952 else
5953 {
5954 amount = get_indent();
5955 l = ml_get_curline();
5956 }
5957 *pp = l;
5958
5959 curwin->w_cursor = cursor_save;
5960 return amount;
5961}
5962
5963/*
5964 * Return the indent of the first variable name after a type in a declaration.
5965 * int a, indent of "a"
5966 * static struct foo b, indent of "b"
5967 * enum bla c, indent of "c"
5968 * Returns zero when it doesn't look like a declaration.
5969 */
5970 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005971cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972{
5973 char_u *line, *p, *s;
5974 int len;
5975 pos_T fp;
5976 colnr_T col;
5977
5978 line = ml_get_curline();
5979 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005980 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5982 {
5983 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005984 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5987 p = skipwhite(p + 6);
5988 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5989 p = skipwhite(p + 4);
5990 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5991 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5992 {
5993 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01005994 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
5995 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
5996 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
5997 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 p = s;
5999 }
6000 for (len = 0; vim_isIDc(p[len]); ++len)
6001 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006002 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 return 0;
6004
6005 p = skipwhite(p + len);
6006 fp.lnum = curwin->w_cursor.lnum;
6007 fp.col = (colnr_T)(p - line);
6008 getvcol(curwin, &fp, &col, NULL, NULL);
6009 return (int)col;
6010}
6011
6012/*
6013 * Return the indent of the first non-blank after an equal sign.
6014 * char *foo = "here";
6015 * Return zero if no (useful) equal sign found.
6016 * Return -1 if the line above "lnum" ends in a backslash.
6017 * foo = "asdf\
6018 * asdf\
6019 * here";
6020 */
6021 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006022cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 char_u *line;
6025 char_u *s;
6026 colnr_T col;
6027 pos_T fp;
6028
6029 if (lnum > 1)
6030 {
6031 line = ml_get(lnum - 1);
6032 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6033 return -1;
6034 }
6035
6036 line = s = ml_get(lnum);
6037 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6038 {
6039 if (cin_iscomment(s)) /* ignore comments */
6040 s = cin_skipcomment(s);
6041 else
6042 ++s;
6043 }
6044 if (*s != '=')
6045 return 0;
6046
6047 s = skipwhite(s + 1);
6048 if (cin_nocode(s))
6049 return 0;
6050
6051 if (*s == '"') /* nice alignment for continued strings */
6052 ++s;
6053
6054 fp.lnum = lnum;
6055 fp.col = (colnr_T)(s - line);
6056 getvcol(curwin, &fp, &col, NULL, NULL);
6057 return (int)col;
6058}
6059
6060/*
6061 * Recognize a preprocessor statement: Any line that starts with '#'.
6062 */
6063 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006064cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006066 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 return TRUE;
6068 return FALSE;
6069}
6070
6071/*
6072 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6073 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6074 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006075 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
6077 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006078cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
6080 char_u *line = *pp;
6081 linenr_T lnum = *lnump;
6082 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006083 int candidate_amount = *amount;
6084
6085 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6086 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006088 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 {
6090 if (cin_ispreproc(line))
6091 {
6092 retval = TRUE;
6093 *lnump = lnum;
6094 break;
6095 }
6096 if (lnum == 1)
6097 break;
6098 line = ml_get(--lnum);
6099 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6100 break;
6101 }
6102
6103 if (lnum != *lnump)
6104 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006105 if (retval)
6106 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 return retval;
6108}
6109
6110/*
6111 * Recognize the start of a C or C++ comment.
6112 */
6113 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006114cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
6116 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6117}
6118
6119/*
6120 * Recognize the start of a "//" comment.
6121 */
6122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006123cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 return (p[0] == '/' && p[1] == '/');
6126}
6127
6128/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006129 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6130 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006132 * If a line begins with an "else", only consider it terminated if no unmatched
6133 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 * Return the character terminating the line (ending char's have precedence if
6135 * both apply in order to determine initializations).
6136 */
6137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006138cin_isterminated(
6139 char_u *s,
6140 int incl_open, /* include '{' at the end as terminator */
6141 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006143 char_u found_start = 0;
6144 unsigned n_open = 0;
6145 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 s = cin_skipcomment(s);
6148
6149 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6150 found_start = *s;
6151
Bram Moolenaar496f9512011-05-19 16:35:09 +02006152 if (!found_start)
6153 is_else = cin_iselse(s);
6154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 while (*s)
6156 {
6157 /* skip over comments, "" strings and 'c'haracters */
6158 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006159 if (*s == '}' && n_open > 0)
6160 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006161 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006162 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 && cin_nocode(s + 1))
6164 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006165 else if (*s == '{')
6166 {
6167 if (incl_open && cin_nocode(s + 1))
6168 return *s;
6169 else
6170 ++n_open;
6171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 if (*s)
6174 s++;
6175 }
6176 return found_start;
6177}
6178
6179/*
6180 * Recognize the basic picture of a function declaration -- it needs to
6181 * have an open paren somewhere and a close paren at the end of the line and
6182 * no semicolons anywhere.
6183 * When a line ends in a comma we continue looking in the next line.
6184 * "sp" points to a string with the line. When looking at other lines it must
6185 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006186 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006187 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 */
6189 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006190cin_isfuncdecl(
6191 char_u **sp,
6192 linenr_T first_lnum,
6193 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 char_u *s;
6196 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006197 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006199 pos_T *trypos;
6200 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202 if (sp == NULL)
6203 s = ml_get(lnum);
6204 else
6205 s = *sp;
6206
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006207 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006208 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006209 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006210 {
6211 lnum = trypos->lnum;
6212 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006213 {
6214 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006215 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006216 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006217
6218 s = ml_get(lnum);
6219 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006220 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006221
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006222 /* Ignore line starting with #. */
6223 if (cin_ispreproc(s))
6224 return FALSE;
6225
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6227 {
6228 if (cin_iscomment(s)) /* ignore comments */
6229 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006230 else if (*s == ':')
6231 {
6232 if (*(s + 1) == ':')
6233 s += 2;
6234 else
6235 /* To avoid a mistake in the following situation:
6236 * A::A(int a, int b)
6237 * : a(0) // <--not a function decl
6238 * , b(0)
6239 * {...
6240 */
6241 return FALSE;
6242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 else
6244 ++s;
6245 }
6246 if (*s != '(')
6247 return FALSE; /* ';', ' or " before any () or no '(' */
6248
6249 while (*s && *s != ';' && *s != '\'' && *s != '"')
6250 {
6251 if (*s == ')' && cin_nocode(s + 1))
6252 {
6253 /* ')' at the end: may have found a match
6254 * Check for he previous line not to end in a backslash:
6255 * #if defined(x) && \
6256 * defined(y)
6257 */
6258 lnum = first_lnum - 1;
6259 s = ml_get(lnum);
6260 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6261 retval = TRUE;
6262 goto done;
6263 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006264 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006266 int comma = (*s == ',');
6267
6268 /* ',' at the end: continue looking in the next line.
6269 * At the end: check for ',' in the next line, for this style:
6270 * func(arg1
6271 * , arg2) */
6272 for (;;)
6273 {
6274 if (lnum >= curbuf->b_ml.ml_line_count)
6275 break;
6276 s = ml_get(++lnum);
6277 if (!cin_ispreproc(s))
6278 break;
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 if (lnum >= curbuf->b_ml.ml_line_count)
6281 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006282 /* Require a comma at end of the line or a comma or ')' at the
6283 * start of next line. */
6284 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006285 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006286 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006287 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289 else if (cin_iscomment(s)) /* ignore comments */
6290 s = cin_skipcomment(s);
6291 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006294 just_started = FALSE;
6295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 }
6297
6298done:
6299 if (lnum != first_lnum && sp != NULL)
6300 *sp = ml_get(first_lnum);
6301
6302 return retval;
6303}
6304
6305 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006306cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006308 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309}
6310
6311 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006312cin_iselse(
6313 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
6315 if (*p == '}') /* accept "} else" */
6316 p = cin_skipcomment(p + 1);
6317 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6318}
6319
6320 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006321cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6324}
6325
6326/*
6327 * Check if this is a "while" that should have a matching "do".
6328 * We only accept a "while (condition) ;", with only white space between the
6329 * ')' and ';'. The condition may be spread over several lines.
6330 */
6331 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006332cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 pos_T cursor_save;
6335 pos_T *trypos;
6336 int retval = FALSE;
6337
6338 p = cin_skipcomment(p);
6339 if (*p == '}') /* accept "} while (cond);" */
6340 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006341 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
6343 cursor_save = curwin->w_cursor;
6344 curwin->w_cursor.lnum = lnum;
6345 curwin->w_cursor.col = 0;
6346 p = ml_get_curline();
6347 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6348 {
6349 ++p;
6350 ++curwin->w_cursor.col;
6351 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006352 if ((trypos = findmatchlimit(NULL, 0, 0,
6353 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6355 retval = TRUE;
6356 curwin->w_cursor = cursor_save;
6357 }
6358 return retval;
6359}
6360
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006361/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006362 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006363 * Return 0 if there is none.
6364 * Otherwise return !0 and update "*poffset" to point to the place where the
6365 * string was found.
6366 */
6367 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006368cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006369{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006370 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006371
6372 if (offset-- < 2)
6373 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006374 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006375 --offset;
6376
6377 offset -= 1;
6378 if (!STRNCMP(line + offset, "if", 2))
6379 goto probablyFound;
6380
6381 if (offset >= 1)
6382 {
6383 offset -= 1;
6384 if (!STRNCMP(line + offset, "for", 3))
6385 goto probablyFound;
6386
6387 if (offset >= 2)
6388 {
6389 offset -= 2;
6390 if (!STRNCMP(line + offset, "while", 5))
6391 goto probablyFound;
6392 }
6393 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006394 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006395
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006396probablyFound:
6397 if (!offset || !vim_isIDc(line[offset - 1]))
6398 {
6399 *poffset = offset;
6400 return 1;
6401 }
6402 return 0;
6403}
6404
6405/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006406 * Return TRUE if we are at the end of a do-while.
6407 * do
6408 * nothing;
6409 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006410 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006411 * Adjust the cursor to the line with "while".
6412 */
6413 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006414cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006415{
6416 char_u *line;
6417 char_u *p;
6418 char_u *s;
6419 pos_T *trypos;
6420 int i;
6421
6422 if (terminated != ';') /* there must be a ';' at the end */
6423 return FALSE;
6424
6425 p = line = ml_get_curline();
6426 while (*p != NUL)
6427 {
6428 p = cin_skipcomment(p);
6429 if (*p == ')')
6430 {
6431 s = skipwhite(p + 1);
6432 if (*s == ';' && cin_nocode(s + 1))
6433 {
6434 /* Found ");" at end of the line, now check there is "while"
6435 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006436 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006437 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006438 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006439 if (trypos != NULL)
6440 {
6441 s = cin_skipcomment(ml_get(trypos->lnum));
6442 if (*s == '}') /* accept "} while (cond);" */
6443 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006444 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006445 {
6446 curwin->w_cursor.lnum = trypos->lnum;
6447 return TRUE;
6448 }
6449 }
6450
6451 /* Searching may have made "line" invalid, get it again. */
6452 line = ml_get_curline();
6453 p = line + i;
6454 }
6455 }
6456 if (*p != NUL)
6457 ++p;
6458 }
6459 return FALSE;
6460}
6461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006463cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6466}
6467
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006468/*
6469 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 * constructor-initialization. eg:
6471 *
6472 * class MyClass :
6473 * baseClass <-- here
6474 * class MyClass : public baseClass,
6475 * anotherBaseClass <-- here (should probably lineup ??)
6476 * MyClass::MyClass(...) :
6477 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006478 *
6479 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 */
6481 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006482cin_is_cpp_baseclass(
6483 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006485 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 char_u *s;
6487 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006488 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006489 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006491 if (pos->lnum <= lnum)
6492 return cached->found; /* Use the cached result */
6493
6494 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006496 s = skipwhite(line);
6497 if (*s == '#') /* skip #define FOO x ? (x) : x */
6498 return FALSE;
6499 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 if (*s == NUL)
6501 return FALSE;
6502
6503 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6504
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006505 /* Search for a line starting with '#', empty, ending in ';' or containing
6506 * '{' or '}' and start below it. This handles the following situations:
6507 * a = cond ?
6508 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006509 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006510 * func::foo()
6511 * : something
6512 * {}
6513 * Foo::Foo (int one, int two)
6514 * : something(4),
6515 * somethingelse(3)
6516 * {}
6517 */
6518 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006520 line = ml_get(lnum - 1);
6521 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006522 if (*s == '#' || *s == NUL)
6523 break;
6524 while (*s != NUL)
6525 {
6526 s = cin_skipcomment(s);
6527 if (*s == '{' || *s == '}'
6528 || (*s == ';' && cin_nocode(s + 1)))
6529 break;
6530 if (*s != NUL)
6531 ++s;
6532 }
6533 if (*s != NUL)
6534 break;
6535 --lnum;
6536 }
6537
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006538 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006539 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006540 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006541 for (;;)
6542 {
6543 if (*s == NUL)
6544 {
6545 if (lnum == curwin->w_cursor.lnum)
6546 break;
6547 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006548 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006549 s = line;
6550 }
6551 if (s == line)
6552 {
6553 /* don't recognize "case (foo):" as a baseclass */
6554 if (cin_iscase(s, FALSE))
6555 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006556 s = cin_skipcomment(line);
6557 if (*s == NUL)
6558 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006559 }
6560
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006561 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006562 s = skip_string(s) + 1;
6563 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {
6565 if (s[1] == ':')
6566 {
6567 /* skip double colon. It can't be a constructor
6568 * initialization any more */
6569 lookfor_ctor_init = FALSE;
6570 s = cin_skipcomment(s + 2);
6571 }
6572 else if (lookfor_ctor_init || class_or_struct)
6573 {
6574 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006575 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 cpp_base_class = TRUE;
6577 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006578 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 s = cin_skipcomment(s + 1);
6580 }
6581 else
6582 s = cin_skipcomment(s + 1);
6583 }
6584 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6585 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6586 {
6587 class_or_struct = TRUE;
6588 lookfor_ctor_init = FALSE;
6589
6590 if (*s == 'c')
6591 s = cin_skipcomment(s + 5);
6592 else
6593 s = cin_skipcomment(s + 6);
6594 }
6595 else
6596 {
6597 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6598 {
6599 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6600 }
6601 else if (s[0] == ')')
6602 {
6603 /* Constructor-initialization is assumed if we come across
6604 * something like "):" */
6605 class_or_struct = FALSE;
6606 lookfor_ctor_init = TRUE;
6607 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006608 else if (s[0] == '?')
6609 {
6610 /* Avoid seeing '() :' after '?' as constructor init. */
6611 return FALSE;
6612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 else if (!vim_isIDc(s[0]))
6614 {
6615 /* if it is not an identifier, we are wrong */
6616 class_or_struct = FALSE;
6617 lookfor_ctor_init = FALSE;
6618 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006619 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
6621 /* it can't be a constructor-initialization any more */
6622 lookfor_ctor_init = FALSE;
6623
6624 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006625 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006626 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
6628
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006629 /* When the line ends in a comma don't align with it. */
6630 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006631 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006632
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 s = cin_skipcomment(s + 1);
6634 }
6635 }
6636
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006637 cached->found = cpp_base_class;
6638 if (cpp_base_class)
6639 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 return cpp_base_class;
6641}
6642
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006643 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006644get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006645{
6646 int amount;
6647 colnr_T vcol;
6648 pos_T *trypos;
6649
6650 if (col == 0)
6651 {
6652 amount = get_indent();
6653 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006654 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006655 amount = get_indent_lnum(trypos->lnum); /* XXX */
6656 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006657 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006658 }
6659 else
6660 {
6661 curwin->w_cursor.col = col;
6662 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6663 amount = (int)vcol;
6664 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006665 if (amount < curbuf->b_ind_cpp_baseclass)
6666 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006667 return amount;
6668}
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670/*
6671 * Return TRUE if string "s" ends with the string "find", possibly followed by
6672 * white space and comments. Skip strings and comments.
6673 * Ignore "ignore" after "find" if it's not NULL.
6674 */
6675 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006676cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 char_u *p = s;
6679 char_u *r;
6680 int len = (int)STRLEN(find);
6681
6682 while (*p != NUL)
6683 {
6684 p = cin_skipcomment(p);
6685 if (STRNCMP(p, find, len) == 0)
6686 {
6687 r = skipwhite(p + len);
6688 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6689 r = skipwhite(r + STRLEN(ignore));
6690 if (cin_nocode(r))
6691 return TRUE;
6692 }
6693 if (*p != NUL)
6694 ++p;
6695 }
6696 return FALSE;
6697}
6698
6699/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006700 * Return TRUE when "s" starts with "word" and then a non-ID character.
6701 */
6702 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006703cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006704{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006705 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006706
6707 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6708}
6709
6710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 * Skip strings, chars and comments until at or past "trypos".
6712 * Return the column found.
6713 */
6714 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 char_u *line;
6718 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006719 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
6721 p = line = ml_get(trypos->lnum);
6722 while (*p && (colnr_T)(p - line) < trypos->col)
6723 {
6724 if (cin_iscomment(p))
6725 p = cin_skipcomment(p);
6726 else
6727 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006728 new_p = skip_string(p);
6729 if (new_p == p)
6730 ++p;
6731 else
6732 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 }
6734 }
6735 return (int)(p - line);
6736}
6737
6738/*
6739 * Find the '{' at the start of the block we are in.
6740 * Return NULL if no match found.
6741 * Ignore a '{' that is in a comment, makes indenting the next three lines
6742 * work. */
6743/* foo() */
6744/* { */
6745/* } */
6746
6747 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006748find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 pos_T cursor_save;
6751 pos_T *trypos;
6752 pos_T *pos;
6753 static pos_T pos_copy;
6754
6755 cursor_save = curwin->w_cursor;
6756 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6757 {
6758 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6759 trypos = &pos_copy;
6760 curwin->w_cursor = *trypos;
6761 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006762 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006764 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 break;
6766 if (pos != NULL)
6767 curwin->w_cursor.lnum = pos->lnum;
6768 }
6769 curwin->w_cursor = cursor_save;
6770 return trypos;
6771}
6772
6773/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006774 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006775 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 */
6777 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006778find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006780 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006781}
6782
6783 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006784find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006785{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 pos_T cursor_save;
6787 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006788 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006789 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006792 ind_maxp_wk = ind_maxparen;
6793retry:
6794 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
6796 /* check if the ( is in a // comment */
6797 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006798 {
6799 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6800 if (ind_maxp_wk > 0)
6801 {
6802 curwin->w_cursor = *trypos;
6803 curwin->w_cursor.col = 0; /* XXX */
6804 goto retry;
6805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 else
6809 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006810 pos_T *trypos_wk;
6811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6813 trypos = &pos_copy;
6814 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006815 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006816 {
6817 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6818 - trypos_wk->lnum);
6819 if (ind_maxp_wk > 0)
6820 {
6821 curwin->w_cursor = *trypos_wk;
6822 goto retry;
6823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 }
6827 }
6828 curwin->w_cursor = cursor_save;
6829 return trypos;
6830}
6831
6832/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006833 * Find the matching '(', ignoring it if it is in a comment or before an
6834 * unmatched {.
6835 * Return NULL if no match found.
6836 */
6837 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006838find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006839{
6840 pos_T *trypos = find_match_paren(ind_maxparen);
6841
6842 if (trypos != NULL)
6843 {
6844 pos_T *tryposBrace = find_start_brace();
6845
6846 /* If both an unmatched '(' and '{' is found. Ignore the '('
6847 * position if the '{' is further down. */
6848 if (tryposBrace != NULL
6849 && (trypos->lnum != tryposBrace->lnum
6850 ? trypos->lnum < tryposBrace->lnum
6851 : trypos->col < tryposBrace->col))
6852 trypos = NULL;
6853 }
6854 return trypos;
6855}
6856
6857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 * Return ind_maxparen corrected for the difference in line number between the
6859 * cursor position and "startpos". This makes sure that searching for a
6860 * matching paren above the cursor line doesn't find a match because of
6861 * looking a few lines further.
6862 */
6863 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006864corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6867
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006868 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6869 return curbuf->b_ind_maxparen - (int)n;
6870 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871}
6872
6873/*
6874 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006875 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 */
6877 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006878find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 int i;
6881 int retval = FALSE;
6882 int open_count = 0;
6883
6884 curwin->w_cursor.col = 0; /* default is start of line */
6885
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006886 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {
6888 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6889 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6890 if (l[i] == start)
6891 ++open_count;
6892 else if (l[i] == end)
6893 {
6894 if (open_count > 0)
6895 --open_count;
6896 else
6897 {
6898 curwin->w_cursor.col = i;
6899 retval = TRUE;
6900 }
6901 }
6902 }
6903 return retval;
6904}
6905
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006906/*
6907 * Parse 'cinoptions' and set the values in "curbuf".
6908 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6909 */
6910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006911parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006912{
6913 char_u *p;
6914 char_u *l;
6915 char_u *digits;
6916 int n;
6917 int divider;
6918 int fraction = 0;
6919 int sw = (int)get_sw_value(buf);
6920
6921 /*
6922 * Set the default values.
6923 */
6924 /* Spaces from a block's opening brace the prevailing indent for that
6925 * block should be. */
6926 buf->b_ind_level = sw;
6927
6928 /* Spaces from the edge of the line an open brace that's at the end of a
6929 * line is imagined to be. */
6930 buf->b_ind_open_imag = 0;
6931
6932 /* Spaces from the prevailing indent for a line that is not preceded by
6933 * an opening brace. */
6934 buf->b_ind_no_brace = 0;
6935
6936 /* Column where the first { of a function should be located }. */
6937 buf->b_ind_first_open = 0;
6938
6939 /* Spaces from the prevailing indent a leftmost open brace should be
6940 * located. */
6941 buf->b_ind_open_extra = 0;
6942
6943 /* Spaces from the matching open brace (real location for one at the left
6944 * edge; imaginary location from one that ends a line) the matching close
6945 * brace should be located. */
6946 buf->b_ind_close_extra = 0;
6947
6948 /* Spaces from the edge of the line an open brace sitting in the leftmost
6949 * column is imagined to be. */
6950 buf->b_ind_open_left_imag = 0;
6951
6952 /* Spaces jump labels should be shifted to the left if N is non-negative,
6953 * otherwise the jump label will be put to column 1. */
6954 buf->b_ind_jump_label = -1;
6955
6956 /* Spaces from the switch() indent a "case xx" label should be located. */
6957 buf->b_ind_case = sw;
6958
6959 /* Spaces from the "case xx:" code after a switch() should be located. */
6960 buf->b_ind_case_code = sw;
6961
6962 /* Lineup break at end of case in switch() with case label. */
6963 buf->b_ind_case_break = 0;
6964
6965 /* Spaces from the class declaration indent a scope declaration label
6966 * should be located. */
6967 buf->b_ind_scopedecl = sw;
6968
6969 /* Spaces from the scope declaration label code should be located. */
6970 buf->b_ind_scopedecl_code = sw;
6971
6972 /* Amount K&R-style parameters should be indented. */
6973 buf->b_ind_param = sw;
6974
6975 /* Amount a function type spec should be indented. */
6976 buf->b_ind_func_type = sw;
6977
6978 /* Amount a cpp base class declaration or constructor initialization
6979 * should be indented. */
6980 buf->b_ind_cpp_baseclass = sw;
6981
6982 /* additional spaces beyond the prevailing indent a continuation line
6983 * should be located. */
6984 buf->b_ind_continuation = sw;
6985
6986 /* Spaces from the indent of the line with an unclosed parentheses. */
6987 buf->b_ind_unclosed = sw * 2;
6988
6989 /* Spaces from the indent of the line with an unclosed parentheses, which
6990 * itself is also unclosed. */
6991 buf->b_ind_unclosed2 = sw;
6992
6993 /* Suppress ignoring spaces from the indent of a line starting with an
6994 * unclosed parentheses. */
6995 buf->b_ind_unclosed_noignore = 0;
6996
6997 /* If the opening paren is the last nonwhite character on the line, and
6998 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6999 * context (for very long lines). */
7000 buf->b_ind_unclosed_wrapped = 0;
7001
7002 /* Suppress ignoring white space when lining up with the character after
7003 * an unclosed parentheses. */
7004 buf->b_ind_unclosed_whiteok = 0;
7005
7006 /* Indent a closing parentheses under the line start of the matching
7007 * opening parentheses. */
7008 buf->b_ind_matching_paren = 0;
7009
7010 /* Indent a closing parentheses under the previous line. */
7011 buf->b_ind_paren_prev = 0;
7012
7013 /* Extra indent for comments. */
7014 buf->b_ind_comment = 0;
7015
7016 /* Spaces from the comment opener when there is nothing after it. */
7017 buf->b_ind_in_comment = 3;
7018
7019 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7020 * after the comment opener. */
7021 buf->b_ind_in_comment2 = 0;
7022
7023 /* Max lines to search for an open paren. */
7024 buf->b_ind_maxparen = 20;
7025
7026 /* Max lines to search for an open comment. */
7027 buf->b_ind_maxcomment = 70;
7028
7029 /* Handle braces for java code. */
7030 buf->b_ind_java = 0;
7031
7032 /* Not to confuse JS object properties with labels. */
7033 buf->b_ind_js = 0;
7034
7035 /* Handle blocked cases correctly. */
7036 buf->b_ind_keep_case_label = 0;
7037
7038 /* Handle C++ namespace. */
7039 buf->b_ind_cpp_namespace = 0;
7040
7041 /* Handle continuation lines containing conditions of if(), for() and
7042 * while(). */
7043 buf->b_ind_if_for_while = 0;
7044
Bram Moolenaar6b643942017-03-05 19:44:06 +01007045 /* indentation for # comments */
7046 buf->b_ind_hash_comment = 0;
7047
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007048 /* Handle C++ extern "C" or "C++" */
7049 buf->b_ind_cpp_extern_c = 0;
7050
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007051 for (p = buf->b_p_cino; *p; )
7052 {
7053 l = p++;
7054 if (*p == '-')
7055 ++p;
7056 digits = p; /* remember where the digits start */
7057 n = getdigits(&p);
7058 divider = 0;
7059 if (*p == '.') /* ".5s" means a fraction */
7060 {
7061 fraction = atol((char *)++p);
7062 while (VIM_ISDIGIT(*p))
7063 {
7064 ++p;
7065 if (divider)
7066 divider *= 10;
7067 else
7068 divider = 10;
7069 }
7070 }
7071 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7072 {
7073 if (p == digits)
7074 n = sw; /* just "s" is one 'shiftwidth' */
7075 else
7076 {
7077 n *= sw;
7078 if (divider)
7079 n += (sw * fraction + divider / 2) / divider;
7080 }
7081 ++p;
7082 }
7083 if (l[1] == '-')
7084 n = -n;
7085
7086 /* When adding an entry here, also update the default 'cinoptions' in
7087 * doc/indent.txt, and add explanation for it! */
7088 switch (*l)
7089 {
7090 case '>': buf->b_ind_level = n; break;
7091 case 'e': buf->b_ind_open_imag = n; break;
7092 case 'n': buf->b_ind_no_brace = n; break;
7093 case 'f': buf->b_ind_first_open = n; break;
7094 case '{': buf->b_ind_open_extra = n; break;
7095 case '}': buf->b_ind_close_extra = n; break;
7096 case '^': buf->b_ind_open_left_imag = n; break;
7097 case 'L': buf->b_ind_jump_label = n; break;
7098 case ':': buf->b_ind_case = n; break;
7099 case '=': buf->b_ind_case_code = n; break;
7100 case 'b': buf->b_ind_case_break = n; break;
7101 case 'p': buf->b_ind_param = n; break;
7102 case 't': buf->b_ind_func_type = n; break;
7103 case '/': buf->b_ind_comment = n; break;
7104 case 'c': buf->b_ind_in_comment = n; break;
7105 case 'C': buf->b_ind_in_comment2 = n; break;
7106 case 'i': buf->b_ind_cpp_baseclass = n; break;
7107 case '+': buf->b_ind_continuation = n; break;
7108 case '(': buf->b_ind_unclosed = n; break;
7109 case 'u': buf->b_ind_unclosed2 = n; break;
7110 case 'U': buf->b_ind_unclosed_noignore = n; break;
7111 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7112 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7113 case 'm': buf->b_ind_matching_paren = n; break;
7114 case 'M': buf->b_ind_paren_prev = n; break;
7115 case ')': buf->b_ind_maxparen = n; break;
7116 case '*': buf->b_ind_maxcomment = n; break;
7117 case 'g': buf->b_ind_scopedecl = n; break;
7118 case 'h': buf->b_ind_scopedecl_code = n; break;
7119 case 'j': buf->b_ind_java = n; break;
7120 case 'J': buf->b_ind_js = n; break;
7121 case 'l': buf->b_ind_keep_case_label = n; break;
7122 case '#': buf->b_ind_hash_comment = n; break;
7123 case 'N': buf->b_ind_cpp_namespace = n; break;
7124 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007125 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007126 }
7127 if (*p == ',')
7128 ++p;
7129 }
7130}
7131
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007132/*
7133 * Return the desired indent for C code.
7134 * Return -1 if the indent should be left alone (inside a raw string).
7135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007137get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 pos_T cur_curpos;
7140 int amount;
7141 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007142 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 colnr_T col;
7144 char_u *theline;
7145 char_u *linecopy;
7146 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007147 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007149 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 pos_T our_paren_pos;
7151 char_u *start;
7152 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007153#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154#define BRACE_AT_START 2 /* '{' is at start of line */
7155#define BRACE_AT_END 3 /* '{' is at end of line */
7156 linenr_T ourscope;
7157 char_u *l;
7158 char_u *look;
7159 char_u terminated;
7160 int lookfor;
7161#define LOOKFOR_INITIAL 0
7162#define LOOKFOR_IF 1
7163#define LOOKFOR_DO 2
7164#define LOOKFOR_CASE 3
7165#define LOOKFOR_ANY 4
7166#define LOOKFOR_TERM 5
7167#define LOOKFOR_UNTERM 6
7168#define LOOKFOR_SCOPEDECL 7
7169#define LOOKFOR_NOBREAK 8
7170#define LOOKFOR_CPP_BASECLASS 9
7171#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007172#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007173#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175 int whilelevel;
7176 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 int n;
7178 int iscase;
7179 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007180 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007182 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007183 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007184 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007185 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007186 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007188 /* make a copy, value is changed below */
7189 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
7191 /* remember where the cursor was when we started */
7192 cur_curpos = curwin->w_cursor;
7193
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007194 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007195 if (cur_curpos.lnum == 1)
7196 return 0;
7197
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 /* Get a copy of the current contents of the line.
7199 * This is required, because only the most recent line obtained with
7200 * ml_get is valid! */
7201 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7202 if (linecopy == NULL)
7203 return 0;
7204
7205 /*
7206 * In insert mode and the cursor is on a ')' truncate the line at the
7207 * cursor position. We don't want to line up with the matching '(' when
7208 * inserting new stuff.
7209 * For unknown reasons the cursor might be past the end of the line, thus
7210 * check for that.
7211 */
7212 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007213 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 && linecopy[curwin->w_cursor.col] == ')')
7215 linecopy[curwin->w_cursor.col] = NUL;
7216
7217 theline = skipwhite(linecopy);
7218
7219 /* move the cursor to the start of the line */
7220
7221 curwin->w_cursor.col = 0;
7222
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007223 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007224
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007226 * If we are inside a raw string don't change the indent.
7227 * Ignore a raw string inside a comment.
7228 */
7229 comment_pos = ind_find_start_comment();
7230 if (comment_pos != NULL)
7231 {
7232 /* findmatchlimit() static pos is overwritten, make a copy */
7233 tryposCopy = *comment_pos;
7234 comment_pos = &tryposCopy;
7235 }
7236 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007237 if (trypos != NULL && (comment_pos == NULL
7238 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007239 {
7240 amount = -1;
7241 goto laterend;
7242 }
7243
7244 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 * #defines and so on always go at the left when included in 'cinkeys'.
7246 */
7247 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007248 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007249 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007250 goto theend;
7251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007254 * Is it a non-case label? Then that goes at the left margin too unless:
7255 * - JS flag is set.
7256 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007258 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007259 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {
7261 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007262 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
7264
7265 /*
7266 * If we're inside a "//" comment and there is a "//" comment in a
7267 * previous line, lineup with that one.
7268 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007269 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 && (trypos = find_line_comment()) != NULL) /* XXX */
7271 {
7272 /* find how indented the line beginning the comment is */
7273 getvcol(curwin, trypos, &col, NULL, NULL);
7274 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007275 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277
7278 /*
7279 * If we're inside a comment and not looking at the start of the
7280 * comment, try using the 'comments' option.
7281 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007282 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
7284 int lead_start_len = 2;
7285 int lead_middle_len = 1;
7286 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7287 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7288 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7289 char_u *p;
7290 int start_align = 0;
7291 int start_off = 0;
7292 int done = FALSE;
7293
7294 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007295 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007297 *lead_start = NUL;
7298 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299
7300 p = curbuf->b_p_com;
7301 while (*p != NUL)
7302 {
7303 int align = 0;
7304 int off = 0;
7305 int what = 0;
7306
7307 while (*p != NUL && *p != ':')
7308 {
7309 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7310 what = *p++;
7311 else if (*p == COM_LEFT || *p == COM_RIGHT)
7312 align = *p++;
7313 else if (VIM_ISDIGIT(*p) || *p == '-')
7314 off = getdigits(&p);
7315 else
7316 ++p;
7317 }
7318
7319 if (*p == ':')
7320 ++p;
7321 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7322 if (what == COM_START)
7323 {
7324 STRCPY(lead_start, lead_end);
7325 lead_start_len = (int)STRLEN(lead_start);
7326 start_off = off;
7327 start_align = align;
7328 }
7329 else if (what == COM_MIDDLE)
7330 {
7331 STRCPY(lead_middle, lead_end);
7332 lead_middle_len = (int)STRLEN(lead_middle);
7333 }
7334 else if (what == COM_END)
7335 {
7336 /* If our line starts with the middle comment string, line it
7337 * up with the comment opener per the 'comments' option. */
7338 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7339 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7340 {
7341 done = TRUE;
7342 if (curwin->w_cursor.lnum > 1)
7343 {
7344 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007345 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 * the middle comment string matches in the previous
7347 * line, use the indent of that line. XXX */
7348 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7349 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7350 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7351 else if (STRNCMP(look, lead_middle,
7352 lead_middle_len) == 0)
7353 {
7354 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7355 break;
7356 }
7357 /* If the start comment string doesn't match with the
7358 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007359 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 lead_start, lead_start_len) != 0)
7361 continue;
7362 }
7363 if (start_off != 0)
7364 amount += start_off;
7365 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007366 amount += vim_strsize(lead_start)
7367 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 break;
7369 }
7370
7371 /* If our line starts with the end comment string, line it up
7372 * with the middle comment */
7373 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7374 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7375 {
7376 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7377 /* XXX */
7378 if (off != 0)
7379 amount += off;
7380 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007381 amount += vim_strsize(lead_start)
7382 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 done = TRUE;
7384 break;
7385 }
7386 }
7387 }
7388
7389 /* If our line starts with an asterisk, line up with the
7390 * asterisk in the comment opener; otherwise, line up
7391 * with the first character of the comment text.
7392 */
7393 if (done)
7394 ;
7395 else if (theline[0] == '*')
7396 amount += 1;
7397 else
7398 {
7399 /*
7400 * If we are more than one line away from the comment opener, take
7401 * the indent of the previous non-empty line. If 'cino' has "CO"
7402 * and we are just below the comment opener and there are any
7403 * white characters after it line up with the text after it;
7404 * otherwise, add the amount specified by "c" in 'cino'
7405 */
7406 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007407 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 {
7409 if (linewhite(lnum)) /* skip blank lines */
7410 continue;
7411 amount = get_indent_lnum(lnum); /* XXX */
7412 break;
7413 }
7414 if (amount == -1) /* use the comment opener */
7415 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007416 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007418 start = ml_get(comment_pos->lnum);
7419 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007421 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007423 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007425 if (curbuf->b_ind_in_comment2 || *look == NUL)
7426 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007429 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431
7432 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007433 * Are we looking at a ']' that has a match?
7434 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007435 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007436 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7437 {
7438 /* align with the line containing the '['. */
7439 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007440 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007441 }
7442
7443 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 * Are we inside parentheses or braces?
7445 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007446 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007447 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007448 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 || trypos != NULL)
7450 {
7451 if (trypos != NULL && tryposBrace != NULL)
7452 {
7453 /* Both an unmatched '(' and '{' is found. Use the one which is
7454 * closer to the current cursor position, set the other to NULL. */
7455 if (trypos->lnum != tryposBrace->lnum
7456 ? trypos->lnum < tryposBrace->lnum
7457 : trypos->col < tryposBrace->col)
7458 trypos = NULL;
7459 else
7460 tryposBrace = NULL;
7461 }
7462
7463 if (trypos != NULL)
7464 {
7465 /*
7466 * If the matching paren is more than one line away, use the indent of
7467 * a previous non-empty line that matches the same paren.
7468 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007469 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007471 /* Line up with the start of the matching paren line. */
7472 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7473 }
7474 else
7475 {
7476 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007477 our_paren_pos = *trypos;
7478 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007480 l = skipwhite(ml_get(lnum));
7481 if (cin_nocode(l)) /* skip comment lines */
7482 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007483 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007484 continue; /* ignore #define, #if, etc. */
7485 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007487 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007488 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007489 {
7490 lnum = trypos->lnum + 1;
7491 continue;
7492 }
7493
7494 /* XXX */
7495 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007496 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007497 && trypos->lnum == our_paren_pos.lnum
7498 && trypos->col == our_paren_pos.col)
7499 {
7500 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007502 if (theline[0] == ')')
7503 {
7504 if (our_paren_pos.lnum != lnum
7505 && cur_amount > amount)
7506 cur_amount = amount;
7507 amount = -1;
7508 }
7509 break;
7510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
7512 }
7513
7514 /*
7515 * Line up with line where the matching paren is. XXX
7516 * If the line starts with a '(' or the indent for unclosed
7517 * parentheses is zero, line up with the unclosed parentheses.
7518 */
7519 if (amount == -1)
7520 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007521 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007522 int is_if_for_while = 0;
7523
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007524 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007525 {
7526 /* Look for the outermost opening parenthesis on this line
7527 * and check whether it belongs to an "if", "for" or "while". */
7528
7529 pos_T cursor_save = curwin->w_cursor;
7530 pos_T outermost;
7531 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007532
7533 trypos = &our_paren_pos;
7534 do {
7535 outermost = *trypos;
7536 curwin->w_cursor.lnum = outermost.lnum;
7537 curwin->w_cursor.col = outermost.col;
7538
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007539 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007540 } while (trypos && trypos->lnum == outermost.lnum);
7541
7542 curwin->w_cursor = cursor_save;
7543
7544 line = ml_get(outermost.lnum);
7545
7546 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007547 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007548 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007549
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007550 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007551 look = skipwhite(look);
7552 if (*look == '(')
7553 {
7554 linenr_T save_lnum = curwin->w_cursor.lnum;
7555 char_u *line;
7556 int look_col;
7557
7558 /* Ignore a '(' in front of the line that has a match before
7559 * our matching '('. */
7560 curwin->w_cursor.lnum = our_paren_pos.lnum;
7561 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007562 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007563 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007564 if ((trypos = findmatchlimit(NULL, ')', 0,
7565 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007566 != NULL
7567 && trypos->lnum == our_paren_pos.lnum
7568 && trypos->col < our_paren_pos.col)
7569 ignore_paren_col = trypos->col + 1;
7570
7571 curwin->w_cursor.lnum = save_lnum;
7572 look = ml_get(our_paren_pos.lnum) + look_col;
7573 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007574 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7575 && is_if_for_while == 0)
7576 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007577 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 {
7579 /*
7580 * If we're looking at a close paren, line up right there;
7581 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007582 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 * the last nonwhite character of the line, use either the
7584 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007585 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 * lines).
7587 */
7588 if (theline[0] != ')')
7589 {
7590 cur_amount = MAXCOL;
7591 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007592 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 && cin_ends_in(l, (char_u *)"(", NULL))
7594 {
7595 /* look for opening unmatched paren, indent one level
7596 * for each additional level */
7597 n = 1;
7598 for (col = 0; col < our_paren_pos.col; ++col)
7599 {
7600 switch (l[col])
7601 {
7602 case '(':
7603 case '{': ++n;
7604 break;
7605
7606 case ')':
7607 case '}': if (n > 1)
7608 --n;
7609 break;
7610 }
7611 }
7612
7613 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007614 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007616 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 our_paren_pos.col++;
7618 else
7619 {
7620 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007621 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 col++;
7623 if (l[col] != NUL) /* In case of trailing space */
7624 our_paren_pos.col = col;
7625 else
7626 our_paren_pos.col++;
7627 }
7628 }
7629
7630 /*
7631 * Find how indented the paren is, or the character after it
7632 * if we did the above "if".
7633 */
7634 if (our_paren_pos.col > 0)
7635 {
7636 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7637 if (cur_amount > (int)col)
7638 cur_amount = col;
7639 }
7640 }
7641
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007642 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
7644 /* Line up with the start of the matching paren line. */
7645 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007646 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7647 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007648 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {
7650 if (cur_amount != MAXCOL)
7651 amount = cur_amount;
7652 }
7653 else
7654 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007655 /* Add b_ind_unclosed2 for each '(' before our matching one,
7656 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007658 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
7660 --our_paren_pos.col;
7661 switch (*ml_get_pos(&our_paren_pos))
7662 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007663 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 col = our_paren_pos.col;
7665 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007666 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 col = MAXCOL;
7668 break;
7669 }
7670 }
7671
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007672 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 * braces */
7674 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007675 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 else
7677 {
7678 curwin->w_cursor.lnum = our_paren_pos.lnum;
7679 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007680 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7681 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007682 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007684 {
7685 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007686 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007687 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007688 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691 /*
7692 * For a line starting with ')' use the minimum of the two
7693 * positions, to avoid giving it more indent than the previous
7694 * lines:
7695 * func_long_name( if (x
7696 * arg && yy
7697 * ) ^ not here ) ^ not here
7698 */
7699 if (cur_amount < amount)
7700 amount = cur_amount;
7701 }
7702 }
7703
7704 /* add extra indent for a comment */
7705 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007706 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 else
7709 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007710 /*
7711 * We are inside braces, there is a { before this line at the position
7712 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007713 * Make a copy of tryposBrace, it may point to pos_copy inside
7714 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007715 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007716 tryposCopy = *tryposBrace;
7717 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 ourscope = trypos->lnum;
7720 start = ml_get(ourscope);
7721
7722 /*
7723 * Now figure out how indented the line is in general.
7724 * If the brace was at the start of the line, we use that;
7725 * otherwise, check out the indentation of the line as
7726 * a whole and then add the "imaginary indent" to that.
7727 */
7728 look = skipwhite(start);
7729 if (*look == '{')
7730 {
7731 getvcol(curwin, trypos, &col, NULL, NULL);
7732 amount = col;
7733 if (*start == '{')
7734 start_brace = BRACE_IN_COL0;
7735 else
7736 start_brace = BRACE_AT_START;
7737 }
7738 else
7739 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007740 /* That opening brace might have been on a continuation
7741 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 curwin->w_cursor.lnum = ourscope;
7743
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007744 /* Position the cursor over the rightmost paren, so that
7745 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 lnum = ourscope;
7747 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007748 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7749 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 lnum = trypos->lnum;
7751
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007752 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 * case 1: if (asdf &&
7754 * ldfd) {
7755 * }
7756 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007757 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7758 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007760 else if (curbuf->b_ind_js)
7761 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007763 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764
7765 start_brace = BRACE_AT_END;
7766 }
7767
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007768 /* For Javascript check if the line starts with "key:". */
7769 if (curbuf->b_ind_js)
7770 js_cur_has_key = cin_has_js_key(theline);
7771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007773 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 * we want to be. otherwise, add the amount of room
7775 * that an indent is supposed to be.
7776 */
7777 if (theline[0] == '}')
7778 {
7779 /*
7780 * they may want closing braces to line up with something
7781 * other than the open brace. indulge them, if so.
7782 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007783 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
7785 else
7786 {
7787 /*
7788 * If we're looking at an "else", try to find an "if"
7789 * to match it with.
7790 * If we're looking at a "while", try to find a "do"
7791 * to match it with.
7792 */
7793 lookfor = LOOKFOR_INITIAL;
7794 if (cin_iselse(theline))
7795 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007796 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 lookfor = LOOKFOR_DO;
7798 if (lookfor != LOOKFOR_INITIAL)
7799 {
7800 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007801 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {
7803 amount = get_indent(); /* XXX */
7804 goto theend;
7805 }
7806 }
7807
7808 /*
7809 * We get here if we are not on an "while-of-do" or "else" (or
7810 * failed to find a matching "if").
7811 * Search backwards for something to line up with.
7812 * First set amount for when we don't find anything.
7813 */
7814
7815 /*
7816 * if the '{' is _really_ at the left margin, use the imaginary
7817 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007818 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 */
7820
7821 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7822 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007823 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007824 lookfor_cpp_namespace = TRUE;
7825 }
7826 else if (start_brace == BRACE_AT_START &&
7827 lookfor_cpp_namespace) /* '{' is at start */
7828 {
7829
7830 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 }
7832 else
7833 {
7834 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007835 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007836 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007837
7838 l = skipwhite(ml_get_curline());
7839 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007840 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007841 else if (cin_is_cpp_extern_c(l))
7842 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 else
7845 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007846 /* Compensate for adding b_ind_open_extra later. */
7847 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 if (amount < 0)
7849 amount = 0;
7850 }
7851 }
7852
7853 lookfor_break = FALSE;
7854
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007855 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {
7857 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007858 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 }
7860 else if (cin_isscopedecl(theline)) /* private:, ... */
7861 {
7862 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007863 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865 else
7866 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007867 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7868 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 lookfor_break = TRUE;
7870
7871 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007872 /* b_ind_level from start of block */
7873 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875 scope_amount = amount;
7876 whilelevel = 0;
7877
7878 /*
7879 * Search backwards. If we find something we recognize, line up
7880 * with that.
7881 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007882 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 * the usual amount relative to the conditional
7884 * that opens the block.
7885 */
7886 curwin->w_cursor = cur_curpos;
7887 for (;;)
7888 {
7889 curwin->w_cursor.lnum--;
7890 curwin->w_cursor.col = 0;
7891
7892 /*
7893 * If we went all the way back to the start of our scope, line
7894 * up with it.
7895 */
7896 if (curwin->w_cursor.lnum <= ourscope)
7897 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007898 /* We reached end of scope:
7899 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007901 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 * don't add ind_continuation, otherwise it is a variable
7903 * declaration:
7904 * int x,
7905 * here; <-- add ind_continuation
7906 */
7907 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7908 {
7909 if (curwin->w_cursor.lnum == 0
7910 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007911 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007913 /* nothing found (abuse curbuf->b_ind_maxparen as
7914 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * initialization) */
7916 if (cont_amount > 0)
7917 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007918 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 amount += ind_continuation;
7920 break;
7921 }
7922
7923 l = ml_get_curline();
7924
7925 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007926 * If we're in a comment or raw string now, skip to
7927 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007929 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 if (trypos != NULL)
7931 {
7932 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007933 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 continue;
7935 }
7936
7937 /*
7938 * Skip preprocessor directives and blank lines.
7939 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007940 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7941 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 continue;
7943
7944 if (cin_nocode(l))
7945 continue;
7946
7947 terminated = cin_isterminated(l, FALSE, TRUE);
7948
7949 /*
7950 * If we are at top level and the line looks like a
7951 * function declaration, we are done
7952 * (it's a variable declaration).
7953 */
7954 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007955 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {
7957 /* if the line is terminated with another ','
7958 * it is a continued variable initialization.
7959 * don't add extra indent.
7960 * TODO: does not work, if a function
7961 * declaration is split over multiple lines:
7962 * cin_isfuncdecl returns FALSE then.
7963 */
7964 if (terminated == ',')
7965 break;
7966
7967 /* if it es a enum declaration or an assignment,
7968 * we are done.
7969 */
7970 if (terminated != ';' && cin_isinit())
7971 break;
7972
7973 /* nothing useful found */
7974 if (terminated == 0 || terminated == '{')
7975 continue;
7976 }
7977
7978 if (terminated != ';')
7979 {
7980 /* Skip parens and braces. Position the cursor
7981 * over the rightmost paren, so that matching it
7982 * will take us back to the start of the line.
7983 */ /* XXX */
7984 trypos = NULL;
7985 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007986 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007987 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007990 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991
7992 if (trypos != NULL)
7993 {
7994 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007995 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 continue;
7997 }
7998 }
7999
8000 /* it's a variable declaration, add indentation
8001 * like in
8002 * int a,
8003 * b;
8004 */
8005 if (cont_amount > 0)
8006 amount = cont_amount;
8007 else
8008 amount += ind_continuation;
8009 }
8010 else if (lookfor == LOOKFOR_UNTERM)
8011 {
8012 if (cont_amount > 0)
8013 amount = cont_amount;
8014 else
8015 amount += ind_continuation;
8016 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008017 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008018 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008019 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008020 && lookfor != LOOKFOR_CPP_BASECLASS
8021 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008022 {
8023 amount = scope_amount;
8024 if (theline[0] == '{')
8025 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008026 amount += curbuf->b_ind_open_extra;
8027 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008028 }
8029 }
8030
8031 if (lookfor_cpp_namespace)
8032 {
8033 /*
8034 * Looking for C++ namespace, need to look further
8035 * back.
8036 */
8037 if (curwin->w_cursor.lnum == ourscope)
8038 continue;
8039
8040 if (curwin->w_cursor.lnum == 0
8041 || curwin->w_cursor.lnum
8042 < ourscope - FIND_NAMESPACE_LIM)
8043 break;
8044
8045 l = ml_get_curline();
8046
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008047 /* If we're in a comment or raw string now, skip
8048 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008049 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008050 if (trypos != NULL)
8051 {
8052 curwin->w_cursor.lnum = trypos->lnum + 1;
8053 curwin->w_cursor.col = 0;
8054 continue;
8055 }
8056
8057 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008058 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8059 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008060 continue;
8061
8062 /* Finally the actual check for "namespace". */
8063 if (cin_is_cpp_namespace(l))
8064 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008065 amount += curbuf->b_ind_cpp_namespace
8066 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008067 break;
8068 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008069 else if (cin_is_cpp_extern_c(l))
8070 {
8071 amount += curbuf->b_ind_cpp_extern_c
8072 - added_to_amount;
8073 break;
8074 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008075
8076 if (cin_nocode(l))
8077 continue;
8078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 }
8080 break;
8081 }
8082
8083 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008084 * If we're in a comment or raw string now, skip to the start
8085 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008087 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {
8089 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008090 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 continue;
8092 }
8093
8094 l = ml_get_curline();
8095
8096 /*
8097 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008098 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008100 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 if (iscase || cin_isscopedecl(l))
8102 {
8103 /* we are only looking for cpp base class
8104 * declaration/initialization any longer */
8105 if (lookfor == LOOKFOR_CPP_BASECLASS)
8106 break;
8107
8108 /* When looking for a "do" we are not interested in
8109 * labels. */
8110 if (whilelevel > 0)
8111 continue;
8112
8113 /*
8114 * case xx:
8115 * c = 99 + <- this indent plus continuation
8116 *-> here;
8117 */
8118 if (lookfor == LOOKFOR_UNTERM
8119 || lookfor == LOOKFOR_ENUM_OR_INIT)
8120 {
8121 if (cont_amount > 0)
8122 amount = cont_amount;
8123 else
8124 amount += ind_continuation;
8125 break;
8126 }
8127
8128 /*
8129 * case xx: <- line up with this case
8130 * x = 333;
8131 * case yy:
8132 */
8133 if ( (iscase && lookfor == LOOKFOR_CASE)
8134 || (iscase && lookfor_break)
8135 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8136 {
8137 /*
8138 * Check that this case label is not for another
8139 * switch()
8140 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008141 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008142 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {
8144 amount = get_indent(); /* XXX */
8145 break;
8146 }
8147 continue;
8148 }
8149
8150 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8151
8152 /*
8153 * case xx: if (cond) <- line up with this if
8154 * y = y + 1;
8155 * -> s = 99;
8156 *
8157 * case xx:
8158 * if (cond) <- line up with this line
8159 * y = y + 1;
8160 * -> s = 99;
8161 */
8162 if (lookfor == LOOKFOR_TERM)
8163 {
8164 if (n)
8165 amount = n;
8166
8167 if (!lookfor_break)
8168 break;
8169 }
8170
8171 /*
8172 * case xx: x = x + 1; <- line up with this x
8173 * -> y = y + 1;
8174 *
8175 * case xx: if (cond) <- line up with this if
8176 * -> y = y + 1;
8177 */
8178 if (n)
8179 {
8180 amount = n;
8181 l = after_label(ml_get_curline());
8182 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008183 {
8184 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008185 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008186 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008187 amount += curbuf->b_ind_level
8188 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 break;
8191 }
8192
8193 /*
8194 * Try to get the indent of a statement before the switch
8195 * label. If nothing is found, line up relative to the
8196 * switch label.
8197 * break; <- may line up with this line
8198 * case xx:
8199 * -> y = 1;
8200 */
8201 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008202 ? curbuf->b_ind_case_code
8203 : curbuf->b_ind_scopedecl_code);
8204 lookfor = curbuf->b_ind_case_break
8205 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 continue;
8207 }
8208
8209 /*
8210 * Looking for a switch() label or C++ scope declaration,
8211 * ignore other lines, skip {}-blocks.
8212 */
8213 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8214 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008215 if (find_last_paren(l, '{', '}')
8216 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008219 curwin->w_cursor.col = 0;
8220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 continue;
8222 }
8223
8224 /*
8225 * Ignore jump labels with nothing after them.
8226 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008227 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
8229 l = after_label(ml_get_curline());
8230 if (l == NULL || cin_nocode(l))
8231 continue;
8232 }
8233
8234 /*
8235 * Ignore #defines, #if, etc.
8236 * Ignore comment and empty lines.
8237 * (need to get the line again, cin_islabel() may have
8238 * unlocked it)
8239 */
8240 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008241 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 || cin_nocode(l))
8243 continue;
8244
8245 /*
8246 * Are we at the start of a cpp base class declaration or
8247 * constructor initialization?
8248 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008249 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008250 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008251 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008252 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008253 l = ml_get_curline();
8254 }
8255 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
8257 if (lookfor == LOOKFOR_UNTERM)
8258 {
8259 if (cont_amount > 0)
8260 amount = cont_amount;
8261 else
8262 amount += ind_continuation;
8263 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008264 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008266 /* Need to find start of the declaration. */
8267 lookfor = LOOKFOR_UNTERM;
8268 ind_continuation = 0;
8269 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 }
8271 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008272 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008273 amount = get_baseclass_amount(
8274 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 break;
8276 }
8277 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8278 {
8279 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008280 * declaration or initialization before the opening brace.
8281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 if (cin_isterminated(l, TRUE, FALSE))
8283 break;
8284 else
8285 continue;
8286 }
8287
8288 /*
8289 * What happens next depends on the line being terminated.
8290 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008291 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 * 123,
8293 * sizeof
8294 * here
8295 * Otherwise check whether it is a enumeration or structure
8296 * initialisation (not indented) or a variable declaration
8297 * (indented).
8298 */
8299 terminated = cin_isterminated(l, FALSE, TRUE);
8300
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008301 if (js_cur_has_key)
8302 {
8303 js_cur_has_key = 0; /* only check the first line */
8304 if (curbuf->b_ind_js && terminated == ',')
8305 {
8306 /* For Javascript we might be inside an object:
8307 * key: something, <- align with this
8308 * key: something
8309 * or:
8310 * key: something + <- align with this
8311 * something,
8312 * key: something
8313 */
8314 lookfor = LOOKFOR_JS_KEY;
8315 }
8316 }
8317 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8318 {
8319 amount = get_indent();
8320 break;
8321 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008322 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008323 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008324 if (tryposBrace != NULL && tryposBrace->lnum
8325 >= curwin->w_cursor.lnum)
8326 break;
8327 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008328 /* line below current line is the one that starts a
8329 * (possibly broken) line ending in a comma. */
8330 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008331 else
8332 {
8333 amount = get_indent();
8334 if (curwin->w_cursor.lnum - 1 == ourscope)
8335 /* line above is start of the scope, thus current
8336 * line is the one that stars a (possibly broken)
8337 * line ending in a comma. */
8338 break;
8339 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008340 }
8341
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8343 && terminated == ','))
8344 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008345 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8346 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008347 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 /*
8349 * if we're in the middle of a paren thing,
8350 * go back to the line that starts it so
8351 * we can get the right prevailing indent
8352 * if ( foo &&
8353 * bar )
8354 */
8355 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008356 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008358 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 */
8360 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008361 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008362 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8363 || (trypos->lnum == tryposBrace->lnum
8364 && trypos->col < tryposBrace->col)))
8365 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366
8367 /*
8368 * If we are looking for ',', we also look for matching
8369 * braces.
8370 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008371 if (trypos == NULL && terminated == ','
8372 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008373 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374
8375 if (trypos != NULL)
8376 {
8377 /*
8378 * Check if we are on a case label now. This is
8379 * handled above.
8380 * case xx: if ( asdf &&
8381 * asdf)
8382 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008383 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008385 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {
8387 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008388 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 continue;
8390 }
8391 }
8392
8393 /*
8394 * Skip over continuation lines to find the one to get the
8395 * indent from
8396 * char *usethis = "bla\
8397 * bla",
8398 * here;
8399 */
8400 if (terminated == ',')
8401 {
8402 while (curwin->w_cursor.lnum > 1)
8403 {
8404 l = ml_get(curwin->w_cursor.lnum - 1);
8405 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8406 break;
8407 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008408 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 }
8410 }
8411
8412 /*
8413 * Get indent and pointer to text for current line,
8414 * ignoring any jump label. XXX
8415 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008416 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008417 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008418 else
8419 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 /*
8421 * If this is just above the line we are indenting, and it
8422 * starts with a '{', line it up with this line.
8423 * while (not)
8424 * -> {
8425 * }
8426 */
8427 if (terminated != ',' && lookfor != LOOKFOR_TERM
8428 && theline[0] == '{')
8429 {
8430 amount = cur_amount;
8431 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008432 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 * doesn't start with a '{', which must have a match
8434 * in the same line (scope is the same). Probably:
8435 * { 1, 2 },
8436 * -> { 3, 4 }
8437 */
8438 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008439 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008441 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {
8443 /* have to look back, whether it is a cpp base
8444 * class declaration or initialization */
8445 lookfor = LOOKFOR_CPP_BASECLASS;
8446 continue;
8447 }
8448 break;
8449 }
8450
8451 /*
8452 * Check if we are after an "if", "while", etc.
8453 * Also allow " } else".
8454 */
8455 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8456 {
8457 /*
8458 * Found an unterminated line after an if (), line up
8459 * with the last one.
8460 * if (cond)
8461 * 100 +
8462 * -> here;
8463 */
8464 if (lookfor == LOOKFOR_UNTERM
8465 || lookfor == LOOKFOR_ENUM_OR_INIT)
8466 {
8467 if (cont_amount > 0)
8468 amount = cont_amount;
8469 else
8470 amount += ind_continuation;
8471 break;
8472 }
8473
8474 /*
8475 * If this is just above the line we are indenting, we
8476 * are finished.
8477 * while (not)
8478 * -> here;
8479 * Otherwise this indent can be used when the line
8480 * before this is terminated.
8481 * yyy;
8482 * if (stat)
8483 * while (not)
8484 * xxx;
8485 * -> here;
8486 */
8487 amount = cur_amount;
8488 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008489 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 if (lookfor != LOOKFOR_TERM)
8491 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008492 amount += curbuf->b_ind_level
8493 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 break;
8495 }
8496
8497 /*
8498 * Special trick: when expecting the while () after a
8499 * do, line up with the while()
8500 * do
8501 * x = 1;
8502 * -> here
8503 */
8504 l = skipwhite(ml_get_curline());
8505 if (cin_isdo(l))
8506 {
8507 if (whilelevel == 0)
8508 break;
8509 --whilelevel;
8510 }
8511
8512 /*
8513 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008514 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 * Need to use the scope of this "else". XXX
8516 * If whilelevel != 0 continue looking for a "do {".
8517 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008518 if (cin_iselse(l) && whilelevel == 0)
8519 {
8520 /* If we're looking at "} else", let's make sure we
8521 * find the opening brace of the enclosing scope,
8522 * not the one from "if () {". */
8523 if (*l == '}')
8524 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008525 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008526
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008527 if ((trypos = find_start_brace()) == NULL
8528 || find_match(LOOKFOR_IF, trypos->lnum)
8529 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008530 break;
8531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533
8534 /*
8535 * If we're below an unterminated line that is not an
8536 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008537 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 * the line before this one.
8539 */
8540 else
8541 {
8542 /*
8543 * Found two unterminated lines on a row, line up with
8544 * the last one.
8545 * c = 99 +
8546 * 100 +
8547 * -> here;
8548 */
8549 if (lookfor == LOOKFOR_UNTERM)
8550 {
8551 /* When line ends in a comma add extra indent */
8552 if (terminated == ',')
8553 amount += ind_continuation;
8554 break;
8555 }
8556
8557 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8558 {
8559 /* Found two lines ending in ',', lineup with the
8560 * lowest one, but check for cpp base class
8561 * declaration/initialization, if it is an
8562 * opening brace or we are looking just for
8563 * enumerations/initializations. */
8564 if (terminated == ',')
8565 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008566 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 break;
8568
8569 lookfor = LOOKFOR_CPP_BASECLASS;
8570 continue;
8571 }
8572
8573 /* Ignore unterminated lines in between, but
8574 * reduce indent. */
8575 if (amount > cur_amount)
8576 amount = cur_amount;
8577 }
8578 else
8579 {
8580 /*
8581 * Found first unterminated line on a row, may
8582 * line up with this line, remember its indent
8583 * 100 +
8584 * -> here;
8585 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008586 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008588
8589 n = (int)STRLEN(l);
8590 if (terminated == ',' && (*skipwhite(l) == ']'
8591 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
8594 /*
8595 * If previous line ends in ',', check whether we
8596 * are in an initialization or enum
8597 * struct xxx =
8598 * {
8599 * sizeof a,
8600 * 124 };
8601 * or a normal possible continuation line.
8602 * but only, of no other statement has been found
8603 * yet.
8604 */
8605 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8606 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008607 if (curbuf->b_ind_js)
8608 {
8609 /* Search for a line ending in a comma
8610 * and line up with the line below it
8611 * (could be the current line).
8612 * some = [
8613 * 1, <- line up here
8614 * 2,
8615 * some = [
8616 * 3 + <- line up here
8617 * 4 *
8618 * 5,
8619 * 6,
8620 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008621 if (cin_iscomment(skipwhite(l)))
8622 break;
8623 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008624 trypos = find_match_char('[',
8625 curbuf->b_ind_maxparen);
8626 if (trypos != NULL)
8627 {
8628 if (trypos->lnum
8629 == curwin->w_cursor.lnum - 1)
8630 {
8631 /* Current line is first inside
8632 * [], line up with it. */
8633 break;
8634 }
8635 ourscope = trypos->lnum;
8636 }
8637 }
8638 else
8639 {
8640 lookfor = LOOKFOR_ENUM_OR_INIT;
8641 cont_amount = cin_first_id_amount();
8642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 }
8644 else
8645 {
8646 if (lookfor == LOOKFOR_INITIAL
8647 && *l != NUL
8648 && l[STRLEN(l) - 1] == '\\')
8649 /* XXX */
8650 cont_amount = cin_get_equal_amount(
8651 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008652 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008653 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008654 && lookfor != LOOKFOR_COMMA
8655 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 lookfor = LOOKFOR_UNTERM;
8657 }
8658 }
8659 }
8660 }
8661
8662 /*
8663 * Check if we are after a while (cond);
8664 * If so: Ignore until the matching "do".
8665 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008666 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 {
8668 /*
8669 * Found an unterminated line after a while ();, line up
8670 * with the last one.
8671 * while (cond);
8672 * 100 + <- line up with this one
8673 * -> here;
8674 */
8675 if (lookfor == LOOKFOR_UNTERM
8676 || lookfor == LOOKFOR_ENUM_OR_INIT)
8677 {
8678 if (cont_amount > 0)
8679 amount = cont_amount;
8680 else
8681 amount += ind_continuation;
8682 break;
8683 }
8684
8685 if (whilelevel == 0)
8686 {
8687 lookfor = LOOKFOR_TERM;
8688 amount = get_indent(); /* XXX */
8689 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008690 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 }
8692 ++whilelevel;
8693 }
8694
8695 /*
8696 * We are after a "normal" statement.
8697 * If we had another statement we can stop now and use the
8698 * indent of that other statement.
8699 * Otherwise the indent of the current statement may be used,
8700 * search backwards for the next "normal" statement.
8701 */
8702 else
8703 {
8704 /*
8705 * Skip single break line, if before a switch label. It
8706 * may be lined up with the case label.
8707 */
8708 if (lookfor == LOOKFOR_NOBREAK
8709 && cin_isbreak(skipwhite(ml_get_curline())))
8710 {
8711 lookfor = LOOKFOR_ANY;
8712 continue;
8713 }
8714
8715 /*
8716 * Handle "do {" line.
8717 */
8718 if (whilelevel > 0)
8719 {
8720 l = cin_skipcomment(ml_get_curline());
8721 if (cin_isdo(l))
8722 {
8723 amount = get_indent(); /* XXX */
8724 --whilelevel;
8725 continue;
8726 }
8727 }
8728
8729 /*
8730 * Found a terminated line above an unterminated line. Add
8731 * the amount for a continuation line.
8732 * x = 1;
8733 * y = foo +
8734 * -> here;
8735 * or
8736 * int x = 1;
8737 * int foo,
8738 * -> here;
8739 */
8740 if (lookfor == LOOKFOR_UNTERM
8741 || lookfor == LOOKFOR_ENUM_OR_INIT)
8742 {
8743 if (cont_amount > 0)
8744 amount = cont_amount;
8745 else
8746 amount += ind_continuation;
8747 break;
8748 }
8749
8750 /*
8751 * Found a terminated line above a terminated line or "if"
8752 * etc. line. Use the amount of the line below us.
8753 * x = 1; x = 1;
8754 * if (asdf) y = 2;
8755 * while (asdf) ->here;
8756 * here;
8757 * ->foo;
8758 */
8759 if (lookfor == LOOKFOR_TERM)
8760 {
8761 if (!lookfor_break && whilelevel == 0)
8762 break;
8763 }
8764
8765 /*
8766 * First line above the one we're indenting is terminated.
8767 * To know what needs to be done look further backward for
8768 * a terminated line.
8769 */
8770 else
8771 {
8772 /*
8773 * position the cursor over the rightmost paren, so
8774 * that matching it will take us back to the start of
8775 * the line. Helps for:
8776 * func(asdr,
8777 * asdfasdf);
8778 * here;
8779 */
8780term_again:
8781 l = ml_get_curline();
8782 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008783 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008784 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 {
8786 /*
8787 * Check if we are on a case label now. This is
8788 * handled above.
8789 * case xx: if ( asdf &&
8790 * asdf)
8791 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008792 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008794 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 {
8796 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008797 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 continue;
8799 }
8800 }
8801
8802 /* When aligning with the case statement, don't align
8803 * with a statement after it.
8804 * case 1: { <-- don't use this { position
8805 * stat;
8806 * }
8807 * case 2:
8808 * stat;
8809 * }
8810 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008811 iscase = (curbuf->b_ind_keep_case_label
8812 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813
8814 /*
8815 * Get indent and pointer to text for current line,
8816 * ignoring any jump label.
8817 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008818 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819
8820 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008821 amount += curbuf->b_ind_open_extra;
8822 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008823 l = skipwhite(l);
8824 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008825 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8827
8828 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008829 * When a terminated line starts with "else" skip to
8830 * the matching "if":
8831 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008832 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008833 * Need to use the scope of this "else". XXX
8834 * If whilelevel != 0 continue looking for a "do {".
8835 */
8836 if (lookfor == LOOKFOR_TERM
8837 && *l != '}'
8838 && cin_iselse(l)
8839 && whilelevel == 0)
8840 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008841 if ((trypos = find_start_brace()) == NULL
8842 || find_match(LOOKFOR_IF, trypos->lnum)
8843 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008844 break;
8845 continue;
8846 }
8847
8848 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 * If we're at the end of a block, skip to the start of
8850 * that block.
8851 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008852 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008853 if (find_last_paren(l, '{', '}') /* XXX */
8854 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008856 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 /* if not "else {" check for terminated again */
8858 /* but skip block for "} else {" */
8859 l = cin_skipcomment(ml_get_curline());
8860 if (*l == '}' || !cin_iselse(l))
8861 goto term_again;
8862 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008863 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 }
8865 }
8866 }
8867 }
8868 }
8869 }
8870
8871 /* add extra indent for a comment */
8872 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008873 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008874
8875 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008876 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8877 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008878
8879 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008881
8882 /*
8883 * ok -- we're not inside any sort of structure at all!
8884 *
8885 * This means we're at the top level, and everything should
8886 * basically just match where the previous line is, except
8887 * for the lines immediately following a function declaration,
8888 * which are K&R-style parameters and need to be indented.
8889 *
8890 * if our line starts with an open brace, forget about any
8891 * prevailing indent and make sure it looks like the start
8892 * of a function
8893 */
8894
8895 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008897 amount = curbuf->b_ind_first_open;
8898 goto theend;
8899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008901 /*
8902 * If the NEXT line is a function declaration, the current
8903 * line needs to be indented as a function type spec.
8904 * Don't do this if the current line looks like a comment or if the
8905 * current line is terminated, ie. ends in ';', or if the current line
8906 * contains { or }: "void f() {\n if (1)"
8907 */
8908 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8909 && !cin_nocode(theline)
8910 && vim_strchr(theline, '{') == NULL
8911 && vim_strchr(theline, '}') == NULL
8912 && !cin_ends_in(theline, (char_u *)":", NULL)
8913 && !cin_ends_in(theline, (char_u *)",", NULL)
8914 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8915 cur_curpos.lnum + 1)
8916 && !cin_isterminated(theline, FALSE, TRUE))
8917 {
8918 amount = curbuf->b_ind_func_type;
8919 goto theend;
8920 }
8921
8922 /* search backwards until we find something we recognize */
8923 amount = 0;
8924 curwin->w_cursor = cur_curpos;
8925 while (curwin->w_cursor.lnum > 1)
8926 {
8927 curwin->w_cursor.lnum--;
8928 curwin->w_cursor.col = 0;
8929
8930 l = ml_get_curline();
8931
8932 /*
8933 * If we're in a comment or raw string now, skip to the start
8934 * of it.
8935 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008936 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008938 curwin->w_cursor.lnum = trypos->lnum + 1;
8939 curwin->w_cursor.col = 0;
8940 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 }
8942
8943 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008944 * Are we at the start of a cpp base class declaration or
8945 * constructor initialization?
8946 */ /* XXX */
8947 n = FALSE;
8948 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008950 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8951 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008953 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008955 /* XXX */
8956 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8957 break;
8958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008960 /*
8961 * Skip preprocessor directives and blank lines.
8962 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008963 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008964 continue;
8965
8966 if (cin_nocode(l))
8967 continue;
8968
8969 /*
8970 * If the previous line ends in ',', use one level of
8971 * indentation:
8972 * int foo,
8973 * bar;
8974 * do this before checking for '}' in case of eg.
8975 * enum foobar
8976 * {
8977 * ...
8978 * } foo,
8979 * bar;
8980 */
8981 n = 0;
8982 if (cin_ends_in(l, (char_u *)",", NULL)
8983 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8984 {
8985 /* take us back to opening paren */
8986 if (find_last_paren(l, '(', ')')
8987 && (trypos = find_match_paren(
8988 curbuf->b_ind_maxparen)) != NULL)
8989 curwin->w_cursor = *trypos;
8990
8991 /* For a line ending in ',' that is a continuation line go
8992 * back to the first line with a backslash:
8993 * char *foo = "bla\
8994 * bla",
8995 * here;
8996 */
8997 while (n == 0 && curwin->w_cursor.lnum > 1)
8998 {
8999 l = ml_get(curwin->w_cursor.lnum - 1);
9000 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9001 break;
9002 --curwin->w_cursor.lnum;
9003 curwin->w_cursor.col = 0;
9004 }
9005
9006 amount = get_indent(); /* XXX */
9007
9008 if (amount == 0)
9009 amount = cin_first_id_amount();
9010 if (amount == 0)
9011 amount = ind_continuation;
9012 break;
9013 }
9014
9015 /*
9016 * If the line looks like a function declaration, and we're
9017 * not in a comment, put it the left margin.
9018 */
9019 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9020 break;
9021 l = ml_get_curline();
9022
9023 /*
9024 * Finding the closing '}' of a previous function. Put
9025 * current line at the left margin. For when 'cino' has "fs".
9026 */
9027 if (*skipwhite(l) == '}')
9028 break;
9029
9030 /* (matching {)
9031 * If the previous line ends on '};' (maybe followed by
9032 * comments) align at column 0. For example:
9033 * char *string_array[] = { "foo",
9034 * / * x * / "b};ar" }; / * foobar * /
9035 */
9036 if (cin_ends_in(l, (char_u *)"};", NULL))
9037 break;
9038
9039 /*
9040 * If the previous line ends on '[' we are probably in an
9041 * array constant:
9042 * something = [
9043 * 234, <- extra indent
9044 */
9045 if (cin_ends_in(l, (char_u *)"[", NULL))
9046 {
9047 amount = get_indent() + ind_continuation;
9048 break;
9049 }
9050
9051 /*
9052 * Find a line only has a semicolon that belongs to a previous
9053 * line ending in '}', e.g. before an #endif. Don't increase
9054 * indent then.
9055 */
9056 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9057 {
9058 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059
9060 while (curwin->w_cursor.lnum > 1)
9061 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009062 look = ml_get(--curwin->w_cursor.lnum);
9063 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009064 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009066 }
9067 if (curwin->w_cursor.lnum > 0
9068 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009071 curwin->w_cursor = curpos_save;
9072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009074 /*
9075 * If the PREVIOUS line is a function declaration, the current
9076 * line (and the ones that follow) needs to be indented as
9077 * parameters.
9078 */
9079 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9080 {
9081 amount = curbuf->b_ind_param;
9082 break;
9083 }
9084
9085 /*
9086 * If the previous line ends in ';' and the line before the
9087 * previous line ends in ',' or '\', ident to column zero:
9088 * int foo,
9089 * bar;
9090 * indent_to_0 here;
9091 */
9092 if (cin_ends_in(l, (char_u *)";", NULL))
9093 {
9094 l = ml_get(curwin->w_cursor.lnum - 1);
9095 if (cin_ends_in(l, (char_u *)",", NULL)
9096 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9097 break;
9098 l = ml_get_curline();
9099 }
9100
9101 /*
9102 * Doesn't look like anything interesting -- so just
9103 * use the indent of this line.
9104 *
9105 * Position the cursor over the rightmost paren, so that
9106 * matching it will take us back to the start of the line.
9107 */
9108 find_last_paren(l, '(', ')');
9109
9110 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9111 curwin->w_cursor = *trypos;
9112 amount = get_indent(); /* XXX */
9113 break;
9114 }
9115
9116 /* add extra indent for a comment */
9117 if (cin_iscomment(theline))
9118 amount += curbuf->b_ind_comment;
9119
9120 /* add extra indent if the previous line ended in a backslash:
9121 * "asdfasdf\
9122 * here";
9123 * char *foo = "asdf\
9124 * here";
9125 */
9126 if (cur_curpos.lnum > 1)
9127 {
9128 l = ml_get(cur_curpos.lnum - 1);
9129 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9130 {
9131 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9132 if (cur_amount > 0)
9133 amount = cur_amount;
9134 else if (cur_amount == 0)
9135 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 }
9137 }
9138
9139theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009140 if (amount < 0)
9141 amount = 0;
9142
9143laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 /* put the cursor back where it belongs */
9145 curwin->w_cursor = cur_curpos;
9146
9147 vim_free(linecopy);
9148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 return amount;
9150}
9151
9152 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009153find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 char_u *look;
9156 pos_T *theirscope;
9157 char_u *mightbeif;
9158 int elselevel;
9159 int whilelevel;
9160
9161 if (lookfor == LOOKFOR_IF)
9162 {
9163 elselevel = 1;
9164 whilelevel = 0;
9165 }
9166 else
9167 {
9168 elselevel = 0;
9169 whilelevel = 1;
9170 }
9171
9172 curwin->w_cursor.col = 0;
9173
9174 while (curwin->w_cursor.lnum > ourscope + 1)
9175 {
9176 curwin->w_cursor.lnum--;
9177 curwin->w_cursor.col = 0;
9178
9179 look = cin_skipcomment(ml_get_curline());
9180 if (cin_iselse(look)
9181 || cin_isif(look)
9182 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009183 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 {
9185 /*
9186 * if we've gone outside the braces entirely,
9187 * we must be out of scope...
9188 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009189 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 if (theirscope == NULL)
9191 break;
9192
9193 /*
9194 * and if the brace enclosing this is further
9195 * back than the one enclosing the else, we're
9196 * out of luck too.
9197 */
9198 if (theirscope->lnum < ourscope)
9199 break;
9200
9201 /*
9202 * and if they're enclosed in a *deeper* brace,
9203 * then we can ignore it because it's in a
9204 * different scope...
9205 */
9206 if (theirscope->lnum > ourscope)
9207 continue;
9208
9209 /*
9210 * if it was an "else" (that's not an "else if")
9211 * then we need to go back to another if, so
9212 * increment elselevel
9213 */
9214 look = cin_skipcomment(ml_get_curline());
9215 if (cin_iselse(look))
9216 {
9217 mightbeif = cin_skipcomment(look + 4);
9218 if (!cin_isif(mightbeif))
9219 ++elselevel;
9220 continue;
9221 }
9222
9223 /*
9224 * if it was a "while" then we need to go back to
9225 * another "do", so increment whilelevel. XXX
9226 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009227 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 {
9229 ++whilelevel;
9230 continue;
9231 }
9232
9233 /* If it's an "if" decrement elselevel */
9234 look = cin_skipcomment(ml_get_curline());
9235 if (cin_isif(look))
9236 {
9237 elselevel--;
9238 /*
9239 * When looking for an "if" ignore "while"s that
9240 * get in the way.
9241 */
9242 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9243 whilelevel = 0;
9244 }
9245
9246 /* If it's a "do" decrement whilelevel */
9247 if (cin_isdo(look))
9248 whilelevel--;
9249
9250 /*
9251 * if we've used up all the elses, then
9252 * this must be the if that we want!
9253 * match the indent level of that if.
9254 */
9255 if (elselevel <= 0 && whilelevel <= 0)
9256 {
9257 return OK;
9258 }
9259 }
9260 }
9261 return FAIL;
9262}
9263
9264# if defined(FEAT_EVAL) || defined(PROTO)
9265/*
9266 * Get indent level from 'indentexpr'.
9267 */
9268 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009269get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009271 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009272 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009273 pos_T save_pos;
9274 colnr_T save_curswant;
9275 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009277 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9278 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009280 /* Save and restore cursor position and curswant, in case it was changed
9281 * via :normal commands */
9282 save_pos = curwin->w_cursor;
9283 save_curswant = curwin->w_curswant;
9284 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009286 if (use_sandbox)
9287 ++sandbox;
9288 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009289
9290 /* Need to make a copy, the 'indentexpr' option could be changed while
9291 * evaluating it. */
9292 inde_copy = vim_strsave(curbuf->b_p_inde);
9293 if (inde_copy != NULL)
9294 {
9295 indent = (int)eval_to_number(inde_copy);
9296 vim_free(inde_copy);
9297 }
9298
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009299 if (use_sandbox)
9300 --sandbox;
9301 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
9303 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9304 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9305 * command. */
9306 save_State = State;
9307 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009308 curwin->w_cursor = save_pos;
9309 curwin->w_curswant = save_curswant;
9310 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 check_cursor();
9312 State = save_State;
9313
9314 /* If there is an error, just keep the current indent. */
9315 if (indent < 0)
9316 indent = get_indent();
9317
9318 return indent;
9319}
9320# endif
9321
9322#endif /* FEAT_CINDENT */
9323
9324#if defined(FEAT_LISP) || defined(PROTO)
9325
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009326static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327
9328 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009329lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
9331 char_u buf[LSIZE];
9332 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009333 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
9335 while (*word != NUL)
9336 {
9337 (void)copy_option_part(&word, buf, LSIZE, ",");
9338 len = (int)STRLEN(buf);
9339 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9340 return TRUE;
9341 }
9342 return FALSE;
9343}
9344
9345/*
9346 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9347 * The incompatible newer method is quite a bit better at indenting
9348 * code in lisp-like languages than the traditional one; it's still
9349 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9350 *
9351 * TODO:
9352 * Findmatch() should be adapted for lisp, also to make showmatch
9353 * work correctly: now (v5.3) it seems all C/C++ oriented:
9354 * - it does not recognize the #\( and #\) notations as character literals
9355 * - it doesn't know about comments starting with a semicolon
9356 * - it incorrectly interprets '(' as a character literal
9357 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009358 * Update from Sergey Khorev:
9359 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 */
9361 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009362get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009364 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 int amount;
9366 char_u *that;
9367 colnr_T col;
9368 colnr_T firsttry;
9369 int parencount, quotecount;
9370 int vi_lisp;
9371
9372 /* Set vi_lisp to use the vi-compatible method */
9373 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9374
9375 realpos = curwin->w_cursor;
9376 curwin->w_cursor.col = 0;
9377
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009378 if ((pos = findmatch(NULL, '(')) == NULL)
9379 pos = findmatch(NULL, '[');
9380 else
9381 {
9382 paren = *pos;
9383 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009384 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009385 pos = &paren;
9386 }
9387 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 {
9389 /* Extra trick: Take the indent of the first previous non-white
9390 * line that is at the same () level. */
9391 amount = -1;
9392 parencount = 0;
9393
9394 while (--curwin->w_cursor.lnum >= pos->lnum)
9395 {
9396 if (linewhite(curwin->w_cursor.lnum))
9397 continue;
9398 for (that = ml_get_curline(); *that != NUL; ++that)
9399 {
9400 if (*that == ';')
9401 {
9402 while (*(that + 1) != NUL)
9403 ++that;
9404 continue;
9405 }
9406 if (*that == '\\')
9407 {
9408 if (*(that + 1) != NUL)
9409 ++that;
9410 continue;
9411 }
9412 if (*that == '"' && *(that + 1) != NUL)
9413 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009414 while (*++that && *that != '"')
9415 {
9416 /* skipping escaped characters in the string */
9417 if (*that == '\\')
9418 {
9419 if (*++that == NUL)
9420 break;
9421 if (that[1] == NUL)
9422 {
9423 ++that;
9424 break;
9425 }
9426 }
9427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009429 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009431 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 --parencount;
9433 }
9434 if (parencount == 0)
9435 {
9436 amount = get_indent();
9437 break;
9438 }
9439 }
9440
9441 if (amount == -1)
9442 {
9443 curwin->w_cursor.lnum = pos->lnum;
9444 curwin->w_cursor.col = pos->col;
9445 col = pos->col;
9446
9447 that = ml_get_curline();
9448
9449 if (vi_lisp && get_indent() == 0)
9450 amount = 2;
9451 else
9452 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009453 char_u *line = that;
9454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 amount = 0;
9456 while (*that && col)
9457 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009458 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 col--;
9460 }
9461
9462 /*
9463 * Some keywords require "body" indenting rules (the
9464 * non-standard-lisp ones are Scheme special forms):
9465 *
9466 * (let ((a 1)) instead (let ((a 1))
9467 * (...)) of (...))
9468 */
9469
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009470 if (!vi_lisp && (*that == '(' || *that == '[')
9471 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 amount += 2;
9473 else
9474 {
9475 that++;
9476 amount++;
9477 firsttry = amount;
9478
Bram Moolenaar1c465442017-03-12 20:10:05 +01009479 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009481 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 ++that;
9483 }
9484
9485 if (*that && *that != ';') /* not a comment line */
9486 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009487 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009489 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 firsttry++;
9491
9492 parencount = 0;
9493 quotecount = 0;
9494
9495 if (vi_lisp
9496 || (*that != '"'
9497 && *that != '\''
9498 && *that != '#'
9499 && (*that < '0' || *that > '9')))
9500 {
9501 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009502 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 || quotecount
9504 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009505 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 && !quotecount
9507 && !parencount
9508 && vi_lisp)))
9509 {
9510 if (*that == '"')
9511 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009512 if ((*that == '(' || *that == '[')
9513 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009515 if ((*that == ')' || *that == ']')
9516 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 --parencount;
9518 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009519 amount += lbr_chartabsize_adv(
9520 line, &that, (colnr_T)amount);
9521 amount += lbr_chartabsize_adv(
9522 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 }
9524 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009525 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009527 amount += lbr_chartabsize(
9528 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 that++;
9530 }
9531 if (!*that || *that == ';')
9532 amount = firsttry;
9533 }
9534 }
9535 }
9536 }
9537 }
9538 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009539 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540
9541 curwin->w_cursor = realpos;
9542
9543 return amount;
9544}
9545#endif /* FEAT_LISP */
9546
9547 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009548prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009550#if defined(SIGHUP) && defined(SIG_IGN)
9551 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9552 * makes Vim exit and then handling SIGHUP causes various reentrance
9553 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009554 signal(SIGHUP, SIG_IGN);
9555#endif
9556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557#ifdef FEAT_GUI
9558 if (gui.in_use)
9559 {
9560 gui.dying = TRUE;
9561 out_trash(); /* trash any pending output */
9562 }
9563 else
9564#endif
9565 {
9566 windgoto((int)Rows - 1, 0);
9567
9568 /*
9569 * Switch terminal mode back now, so messages end up on the "normal"
9570 * screen (if there are two screens).
9571 */
9572 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009573 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 out_flush();
9575 }
9576}
9577
9578/*
9579 * Preserve files and exit.
9580 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009581 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9582 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 */
9584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009585preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
9587 buf_T *buf;
9588
9589 prepare_to_exit();
9590
Bram Moolenaar4770d092006-01-12 23:22:24 +00009591 /* Setting this will prevent free() calls. That avoids calling free()
9592 * recursively when free() was invoked with a bad pointer. */
9593 really_exiting = TRUE;
9594
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 out_str(IObuff);
9596 screen_start(); /* don't know where cursor is now */
9597 out_flush();
9598
9599 ml_close_notmod(); /* close all not-modified buffers */
9600
Bram Moolenaar29323592016-07-24 22:04:11 +02009601 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 {
9603 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9604 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009605 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 screen_start(); /* don't know where cursor is now */
9607 out_flush();
9608 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9609 break;
9610 }
9611 }
9612
9613 ml_close_all(FALSE); /* close all memfiles, without deleting */
9614
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009615 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
9617 getout(1);
9618}
9619
9620/*
9621 * return TRUE if "fname" exists.
9622 */
9623 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009624vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009626 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627
9628 if (mch_stat((char *)fname, &st))
9629 return FALSE;
9630 return TRUE;
9631}
9632
9633/*
9634 * Check for CTRL-C pressed, but only once in a while.
9635 * Should be used instead of ui_breakcheck() for functions that check for
9636 * each line in the file. Calling ui_breakcheck() each time takes too much
9637 * time, because it can be a system call.
9638 */
9639
9640#ifndef BREAKCHECK_SKIP
9641# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9642# define BREAKCHECK_SKIP 200
9643# else
9644# define BREAKCHECK_SKIP 32
9645# endif
9646#endif
9647
9648static int breakcheck_count = 0;
9649
9650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009651line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653 if (++breakcheck_count >= BREAKCHECK_SKIP)
9654 {
9655 breakcheck_count = 0;
9656 ui_breakcheck();
9657 }
9658}
9659
9660/*
9661 * Like line_breakcheck() but check 10 times less often.
9662 */
9663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009664fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
9666 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9667 {
9668 breakcheck_count = 0;
9669 ui_breakcheck();
9670 }
9671}
9672
9673/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009674 * Invoke expand_wildcards() for one pattern.
9675 * Expand items like "%:h" before the expansion.
9676 * Returns OK or FAIL.
9677 */
9678 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009679expand_wildcards_eval(
9680 char_u **pat, /* pointer to input pattern */
9681 int *num_file, /* resulting number of files */
9682 char_u ***file, /* array of resulting files */
9683 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009684{
9685 int ret = FAIL;
9686 char_u *eval_pat = NULL;
9687 char_u *exp_pat = *pat;
9688 char_u *ignored_msg;
9689 int usedlen;
9690
9691 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9692 {
9693 ++emsg_off;
9694 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9695 NULL, &ignored_msg, NULL);
9696 --emsg_off;
9697 if (eval_pat != NULL)
9698 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9699 }
9700
9701 if (exp_pat != NULL)
9702 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9703
9704 if (eval_pat != NULL)
9705 {
9706 vim_free(exp_pat);
9707 vim_free(eval_pat);
9708 }
9709
9710 return ret;
9711}
9712
9713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9715 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009716 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 */
9718 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009719expand_wildcards(
9720 int num_pat, /* number of input patterns */
9721 char_u **pat, /* array of input patterns */
9722 int *num_files, /* resulting number of files */
9723 char_u ***files, /* array of resulting files */
9724 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726 int retval;
9727 int i, j;
9728 char_u *p;
9729 int non_suf_match; /* number without matching suffix */
9730
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009731 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732
9733 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009734 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 return retval;
9736
9737#ifdef FEAT_WILDIGN
9738 /*
9739 * Remove names that match 'wildignore'.
9740 */
9741 if (*p_wig)
9742 {
9743 char_u *ffname;
9744
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009745 /* check all files in (*files)[] */
9746 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009748 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 if (ffname == NULL) /* out of memory */
9750 break;
9751# ifdef VMS
9752 vms_remove_version(ffname);
9753# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009754 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009756 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009757 vim_free((*files)[i]);
9758 for (j = i; j + 1 < *num_files; ++j)
9759 (*files)[j] = (*files)[j + 1];
9760 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 --i;
9762 }
9763 vim_free(ffname);
9764 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009765
9766 /* If the number of matches is now zero, we fail. */
9767 if (*num_files == 0)
9768 {
9769 vim_free(*files);
9770 *files = NULL;
9771 return FAIL;
9772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 }
9774#endif
9775
9776 /*
9777 * Move the names where 'suffixes' match to the end.
9778 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009779 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 {
9781 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009782 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009784 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
9786 /*
9787 * Move the name without matching suffix to the front
9788 * of the list.
9789 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009790 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009792 (*files)[j] = (*files)[j - 1];
9793 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 }
9795 }
9796 }
9797
9798 return retval;
9799}
9800
9801/*
9802 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9803 */
9804 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009805match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 int fnamelen, setsuflen;
9808 char_u *setsuf;
9809#define MAXSUFLEN 30 /* maximum length of a file suffix */
9810 char_u suf_buf[MAXSUFLEN];
9811
9812 fnamelen = (int)STRLEN(fname);
9813 setsuflen = 0;
9814 for (setsuf = p_su; *setsuf; )
9815 {
9816 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009817 if (setsuflen == 0)
9818 {
9819 char_u *tail = gettail(fname);
9820
9821 /* empty entry: match name without a '.' */
9822 if (vim_strchr(tail, '.') == NULL)
9823 {
9824 setsuflen = 1;
9825 break;
9826 }
9827 }
9828 else
9829 {
9830 if (fnamelen >= setsuflen
9831 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9832 (size_t)setsuflen) == 0)
9833 break;
9834 setsuflen = 0;
9835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 }
9837 return (setsuflen != 0);
9838}
9839
9840#if !defined(NO_EXPANDPATH) || defined(PROTO)
9841
9842# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009843static int vim_backtick(char_u *p);
9844static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845# endif
9846
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009847# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848/*
9849 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9850 * it's shared between these systems.
9851 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009852# if defined(PROTO)
9853# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854# else
9855# ifdef __BORLANDC__
9856# define _cdecl _RTLENTRYF
9857# endif
9858# endif
9859
9860/*
9861 * comparison function for qsort in dos_expandpath()
9862 */
9863 static int _cdecl
9864pstrcmp(const void *a, const void *b)
9865{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009866 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009870 * Recursively expand one path component into all matching files and/or
9871 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 * Return the number of matches found.
9873 * "path" has backslashes before chars that are not to be expanded, starting
9874 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009875 * Return the number of matches found.
9876 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 */
9878 static int
9879dos_expandpath(
9880 garray_T *gap,
9881 char_u *path,
9882 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009883 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009884 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009886 char_u *buf;
9887 char_u *path_end;
9888 char_u *p, *s, *e;
9889 int start_len = gap->ga_len;
9890 char_u *pat;
9891 regmatch_T regmatch;
9892 int starts_with_dot;
9893 int matches;
9894 int len;
9895 int starstar = FALSE;
9896 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 WIN32_FIND_DATA fb;
9898 HANDLE hFind = (HANDLE)0;
9899# ifdef FEAT_MBYTE
9900 WIN32_FIND_DATAW wfb;
9901 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009904 int ok;
9905
9906 /* Expanding "**" may take a long time, check for CTRL-C. */
9907 if (stardepth > 0)
9908 {
9909 ui_breakcheck();
9910 if (got_int)
9911 return 0;
9912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009914 /* Make room for file name. When doing encoding conversion the actual
9915 * length may be quite a bit longer, thus use the maximum possible length. */
9916 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (buf == NULL)
9918 return 0;
9919
9920 /*
9921 * Find the first part in the path name that contains a wildcard or a ~1.
9922 * Copy it into buf, including the preceding characters.
9923 */
9924 p = buf;
9925 s = buf;
9926 e = NULL;
9927 path_end = path;
9928 while (*path_end != NUL)
9929 {
9930 /* May ignore a wildcard that has a backslash before it; it will
9931 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9932 if (path_end >= path + wildoff && rem_backslash(path_end))
9933 *p++ = *path_end++;
9934 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9935 {
9936 if (e != NULL)
9937 break;
9938 s = p + 1;
9939 }
9940 else if (path_end >= path + wildoff
9941 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9942 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009943# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 if (has_mbyte)
9945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009946 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 STRNCPY(p, path_end, len);
9948 p += len;
9949 path_end += len;
9950 }
9951 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 *p++ = *path_end++;
9954 }
9955 e = p;
9956 *e = NUL;
9957
9958 /* now we have one wildcard component between s and e */
9959 /* Remove backslashes between "wildoff" and the start of the wildcard
9960 * component. */
9961 for (p = buf + wildoff; p < s; ++p)
9962 if (rem_backslash(p))
9963 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009964 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 --e;
9966 --s;
9967 }
9968
Bram Moolenaar231334e2005-07-25 20:46:57 +00009969 /* Check for "**" between "s" and "e". */
9970 for (p = s; p < e; ++p)
9971 if (p[0] == '*' && p[1] == '*')
9972 starstar = TRUE;
9973
Bram Moolenaard82103e2016-01-17 17:04:05 +01009974 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9976 if (pat == NULL)
9977 {
9978 vim_free(buf);
9979 return 0;
9980 }
9981
9982 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009983 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009984 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 regmatch.rm_ic = TRUE; /* Always ignore case */
9986 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009987 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009988 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 vim_free(pat);
9990
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009991 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 {
9993 vim_free(buf);
9994 return 0;
9995 }
9996
9997 /* remember the pattern or file name being looked for */
9998 matchname = vim_strsave(s);
9999
Bram Moolenaar231334e2005-07-25 20:46:57 +000010000 /* If "**" is by itself, this is the first time we encounter it and more
10001 * is following then find matches without any directory. */
10002 if (!didstar && stardepth < 100 && starstar && e - s == 2
10003 && *path_end == '/')
10004 {
10005 STRCPY(s, path_end + 1);
10006 ++stardepth;
10007 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10008 --stardepth;
10009 }
10010
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 /* Scan all files in the directory with "dir/ *.*" */
10012 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013# ifdef FEAT_MBYTE
10014 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10015 {
10016 /* The active codepage differs from 'encoding'. Attempt using the
10017 * wide function. If it fails because it is not implemented fall back
10018 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010019 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 if (wn != NULL)
10021 {
10022 hFind = FindFirstFileW(wn, &wfb);
10023 if (hFind == INVALID_HANDLE_VALUE
10024 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
10025 {
10026 vim_free(wn);
10027 wn = NULL;
10028 }
10029 }
10030 }
10031
10032 if (wn == NULL)
10033# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010034 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036
10037 while (ok)
10038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039# ifdef FEAT_MBYTE
10040 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010041 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 else
10043# endif
10044 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 /* Ignore entries starting with a dot, unless when asked for. Accept
10046 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010047 if ((p[0] != '.' || starts_with_dot
10048 || ((flags & EW_DODOT)
10049 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010051 || (regmatch.regprog != NULL
10052 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010053 || ((flags & EW_NOTWILD)
10054 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010058
10059 if (starstar && stardepth < 100)
10060 {
10061 /* For "**" in the pattern first go deeper in the tree to
10062 * find matches. */
10063 STRCPY(buf + len, "/**");
10064 STRCPY(buf + len + 3, path_end);
10065 ++stardepth;
10066 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10067 --stardepth;
10068 }
10069
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 STRCPY(buf + len, path_end);
10071 if (mch_has_exp_wildcard(path_end))
10072 {
10073 /* need to expand another component of the path */
10074 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010075 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 }
10077 else
10078 {
10079 /* no more wildcards, check if there is a match */
10080 /* remove backslashes for the remaining components only */
10081 if (*path_end != 0)
10082 backslash_halve(buf + len + 1);
10083 if (mch_getperm(buf) >= 0) /* add existing file */
10084 addfile(gap, buf, flags);
10085 }
10086 }
10087
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088# ifdef FEAT_MBYTE
10089 if (wn != NULL)
10090 {
10091 vim_free(p);
10092 ok = FindNextFileW(hFind, &wfb);
10093 }
10094 else
10095# endif
10096 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
10098 /* If no more matches and no match was used, try expanding the name
10099 * itself. Finds the long name of a short filename. */
10100 if (!ok && matchname != NULL && gap->ga_len == start_len)
10101 {
10102 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 FindClose(hFind);
10104# ifdef FEAT_MBYTE
10105 if (wn != NULL)
10106 {
10107 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010108 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 if (wn != NULL)
10110 hFind = FindFirstFileW(wn, &wfb);
10111 }
10112 if (wn == NULL)
10113# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010114 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 vim_free(matchname);
10117 matchname = NULL;
10118 }
10119 }
10120
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 FindClose(hFind);
10122# ifdef FEAT_MBYTE
10123 vim_free(wn);
10124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010126 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 vim_free(matchname);
10128
10129 matches = gap->ga_len - start_len;
10130 if (matches > 0)
10131 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10132 sizeof(char_u *), pstrcmp);
10133 return matches;
10134}
10135
10136 int
10137mch_expandpath(
10138 garray_T *gap,
10139 char_u *path,
10140 int flags) /* EW_* flags */
10141{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010142 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010144# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
Bram Moolenaar231334e2005-07-25 20:46:57 +000010146#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10147 || defined(PROTO)
10148/*
10149 * Unix style wildcard expansion code.
10150 * It's here because it's used both for Unix and Mac.
10151 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010152static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010153
10154 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010155pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010156{
10157 return (pathcmp(*(char **)a, *(char **)b, -1));
10158}
10159
10160/*
10161 * Recursively expand one path component into all matching files and/or
10162 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10163 * "path" has backslashes before chars that are not to be expanded, starting
10164 * at "path + wildoff".
10165 * Return the number of matches found.
10166 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10167 */
10168 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010169unix_expandpath(
10170 garray_T *gap,
10171 char_u *path,
10172 int wildoff,
10173 int flags, /* EW_* flags */
10174 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010175{
10176 char_u *buf;
10177 char_u *path_end;
10178 char_u *p, *s, *e;
10179 int start_len = gap->ga_len;
10180 char_u *pat;
10181 regmatch_T regmatch;
10182 int starts_with_dot;
10183 int matches;
10184 int len;
10185 int starstar = FALSE;
10186 static int stardepth = 0; /* depth for "**" expansion */
10187
10188 DIR *dirp;
10189 struct dirent *dp;
10190
10191 /* Expanding "**" may take a long time, check for CTRL-C. */
10192 if (stardepth > 0)
10193 {
10194 ui_breakcheck();
10195 if (got_int)
10196 return 0;
10197 }
10198
10199 /* make room for file name */
10200 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10201 if (buf == NULL)
10202 return 0;
10203
10204 /*
10205 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010206 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010207 * Copy it into "buf", including the preceding characters.
10208 */
10209 p = buf;
10210 s = buf;
10211 e = NULL;
10212 path_end = path;
10213 while (*path_end != NUL)
10214 {
10215 /* May ignore a wildcard that has a backslash before it; it will
10216 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10217 if (path_end >= path + wildoff && rem_backslash(path_end))
10218 *p++ = *path_end++;
10219 else if (*path_end == '/')
10220 {
10221 if (e != NULL)
10222 break;
10223 s = p + 1;
10224 }
10225 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010226 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010227 || (!p_fic && (flags & EW_ICASE)
10228 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010229 e = p;
10230#ifdef FEAT_MBYTE
10231 if (has_mbyte)
10232 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010233 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010234 STRNCPY(p, path_end, len);
10235 p += len;
10236 path_end += len;
10237 }
10238 else
10239#endif
10240 *p++ = *path_end++;
10241 }
10242 e = p;
10243 *e = NUL;
10244
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010245 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010246 /* Remove backslashes between "wildoff" and the start of the wildcard
10247 * component. */
10248 for (p = buf + wildoff; p < s; ++p)
10249 if (rem_backslash(p))
10250 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010251 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010252 --e;
10253 --s;
10254 }
10255
10256 /* Check for "**" between "s" and "e". */
10257 for (p = s; p < e; ++p)
10258 if (p[0] == '*' && p[1] == '*')
10259 starstar = TRUE;
10260
10261 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010262 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010263 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10264 if (pat == NULL)
10265 {
10266 vim_free(buf);
10267 return 0;
10268 }
10269
10270 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010271 if (flags & EW_ICASE)
10272 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10273 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010274 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010275 if (flags & (EW_NOERROR | EW_NOTWILD))
10276 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010277 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010278 if (flags & (EW_NOERROR | EW_NOTWILD))
10279 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010280 vim_free(pat);
10281
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010282 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010283 {
10284 vim_free(buf);
10285 return 0;
10286 }
10287
10288 /* If "**" is by itself, this is the first time we encounter it and more
10289 * is following then find matches without any directory. */
10290 if (!didstar && stardepth < 100 && starstar && e - s == 2
10291 && *path_end == '/')
10292 {
10293 STRCPY(s, path_end + 1);
10294 ++stardepth;
10295 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10296 --stardepth;
10297 }
10298
10299 /* open the directory for scanning */
10300 *s = NUL;
10301 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10302
10303 /* Find all matching entries */
10304 if (dirp != NULL)
10305 {
10306 for (;;)
10307 {
10308 dp = readdir(dirp);
10309 if (dp == NULL)
10310 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010311 if ((dp->d_name[0] != '.' || starts_with_dot
10312 || ((flags & EW_DODOT)
10313 && dp->d_name[1] != NUL
10314 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010315 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10316 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010317 || ((flags & EW_NOTWILD)
10318 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010319 {
10320 STRCPY(s, dp->d_name);
10321 len = STRLEN(buf);
10322
10323 if (starstar && stardepth < 100)
10324 {
10325 /* For "**" in the pattern first go deeper in the tree to
10326 * find matches. */
10327 STRCPY(buf + len, "/**");
10328 STRCPY(buf + len + 3, path_end);
10329 ++stardepth;
10330 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10331 --stardepth;
10332 }
10333
10334 STRCPY(buf + len, path_end);
10335 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10336 {
10337 /* need to expand another component of the path */
10338 /* remove backslashes for the remaining components only */
10339 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10340 }
10341 else
10342 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010343 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010344
Bram Moolenaar231334e2005-07-25 20:46:57 +000010345 /* no more wildcards, check if there is a match */
10346 /* remove backslashes for the remaining components only */
10347 if (*path_end != NUL)
10348 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010349 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010350 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010351 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010352 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010353#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010354 size_t precomp_len = STRLEN(buf)+1;
10355 char_u *precomp_buf =
10356 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010357
Bram Moolenaar231334e2005-07-25 20:46:57 +000010358 if (precomp_buf)
10359 {
10360 mch_memmove(buf, precomp_buf, precomp_len);
10361 vim_free(precomp_buf);
10362 }
10363#endif
10364 addfile(gap, buf, flags);
10365 }
10366 }
10367 }
10368 }
10369
10370 closedir(dirp);
10371 }
10372
10373 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010374 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010375
10376 matches = gap->ga_len - start_len;
10377 if (matches > 0)
10378 qsort(((char_u **)gap->ga_data) + start_len, matches,
10379 sizeof(char_u *), pstrcmp);
10380 return matches;
10381}
10382#endif
10383
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010384#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010385static int find_previous_pathsep(char_u *path, char_u **psep);
10386static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10387static void expand_path_option(char_u *curdir, garray_T *gap);
10388static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10389static void uniquefy_paths(garray_T *gap, char_u *pattern);
10390static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010391
10392/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010393 * Moves "*psep" back to the previous path separator in "path".
10394 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010395 */
10396 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010397find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010398{
10399 /* skip the current separator */
10400 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010401 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010402
10403 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010404 while (*psep > path)
10405 {
10406 if (vim_ispathsep(**psep))
10407 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010408 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010409 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010410
10411 return FAIL;
10412}
10413
10414/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010415 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10416 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010417 */
10418 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010419is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010420{
10421 int j;
10422 int candidate_len;
10423 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010424 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010425 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010426
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010427 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010428 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010430 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010431
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010432 candidate_len = (int)STRLEN(maybe_unique);
10433 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010434 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010435 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010436
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010437 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010438 if (fnamecmp(maybe_unique, rival) == 0
10439 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010440 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010441 }
10442
Bram Moolenaar162bd912010-07-28 22:29:10 +020010443 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010444}
10445
10446/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010447 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010448 * paths are expanded to their equivalent fullpath. This includes the "."
10449 * (relative to current buffer directory) and empty path (relative to current
10450 * directory) notations.
10451 *
10452 * TODO: handle upward search (;) and path limiter (**N) notations by
10453 * expanding each into their equivalent path(s).
10454 */
10455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010456expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010457{
10458 char_u *path_option = *curbuf->b_p_path == NUL
10459 ? p_path : curbuf->b_p_path;
10460 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010461 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010462 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010463
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010464 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010465 return;
10466
10467 while (*path_option != NUL)
10468 {
10469 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10470
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010471 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010472 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010473 /* Relative to current buffer:
10474 * "/path/file" + "." -> "/path/"
10475 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010476 if (curbuf->b_ffname == NULL)
10477 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010478 p = gettail(curbuf->b_ffname);
10479 len = (int)(p - curbuf->b_ffname);
10480 if (len + (int)STRLEN(buf) >= MAXPATHL)
10481 continue;
10482 if (buf[1] == NUL)
10483 buf[len] = NUL;
10484 else
10485 STRMOVE(buf + len, buf + 2);
10486 mch_memmove(buf, curbuf->b_ffname, len);
10487 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010488 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010489 else if (buf[0] == NUL)
10490 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010491 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010492 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010493 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010494 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010495 else if (!mch_isFullName(buf))
10496 {
10497 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010498 len = (int)STRLEN(curdir);
10499 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010500 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010501 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010502 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010503 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010504 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010505 }
10506
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010507 if (ga_grow(gap, 1) == FAIL)
10508 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010509
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010510# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010511 /* Avoid the path ending in a backslash, it fails when a comma is
10512 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010513 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010514 if (buf[len - 1] == '\\')
10515 buf[len - 1] = '/';
10516# endif
10517
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010518 p = vim_strsave(buf);
10519 if (p == NULL)
10520 break;
10521 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010522 }
10523
10524 vim_free(buf);
10525}
10526
10527/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010528 * Returns a pointer to the file or directory name in "fname" that matches the
10529 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010530 *
10531 * path: /foo/bar/baz
10532 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010533 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010534 */
10535 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010536get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010537{
10538 int i;
10539 int maxlen = 0;
10540 char_u **path_part = (char_u **)gap->ga_data;
10541 char_u *cutoff = NULL;
10542
10543 for (i = 0; i < gap->ga_len; i++)
10544 {
10545 int j = 0;
10546
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010547 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010548# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010549 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10550#endif
10551 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010552 j++;
10553 if (j > maxlen)
10554 {
10555 maxlen = j;
10556 cutoff = &fname[j];
10557 }
10558 }
10559
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010560 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010561 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010562 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010563 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010564
10565 return cutoff;
10566}
10567
10568/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010569 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10570 * that they are unique with respect to each other while conserving the part
10571 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010572 */
10573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010574uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010575{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010576 int i;
10577 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010578 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010579 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010580 char_u *pat;
10581 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010582 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010583 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010584 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010585 char_u **in_curdir = NULL;
10586 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010587
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010588 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010589 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010590
10591 /*
10592 * We need to prepend a '*' at the beginning of file_pattern so that the
10593 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010594 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010596 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010597 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010598 if (file_pattern == NULL)
10599 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010600 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010601 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010602 STRCAT(file_pattern, pattern);
10603 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10604 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010605 if (pat == NULL)
10606 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010607
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010608 regmatch.rm_ic = TRUE; /* always ignore case */
10609 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10610 vim_free(pat);
10611 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010612 return;
10613
Bram Moolenaar162bd912010-07-28 22:29:10 +020010614 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010615 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010616 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010617 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010618
10619 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010620 if (in_curdir == NULL)
10621 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010622
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010623 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010624 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010625 char_u *path = fnames[i];
10626 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010627 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010628 char_u *pathsep_p;
10629 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010630
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010631 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010632 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010633 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010634 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010635 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010636
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010637 /* Shorten the filename while maintaining its uniqueness */
10638 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010640 /* Don't assume all files can be reached without path when search
10641 * pattern starts with star star slash, so only remove path_cutoff
10642 * when possible. */
10643 if (pattern[0] == '*' && pattern[1] == '*'
10644 && vim_ispathsep_nocolon(pattern[2])
10645 && path_cutoff != NULL
10646 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10647 && is_unique(path_cutoff, gap, i))
10648 {
10649 sort_again = TRUE;
10650 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10651 }
10652 else
10653 {
10654 /* Here all files can be reached without path, so get shortest
10655 * unique path. We start at the end of the path. */
10656 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010657
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010658 while (find_previous_pathsep(path, &pathsep_p))
10659 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10660 && is_unique(pathsep_p + 1, gap, i)
10661 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10662 {
10663 sort_again = TRUE;
10664 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10665 break;
10666 }
10667 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010668
10669 if (mch_isFullName(path))
10670 {
10671 /*
10672 * Last resort: shorten relative to curdir if possible.
10673 * 'possible' means:
10674 * 1. It is under the current directory.
10675 * 2. The result is actually shorter than the original.
10676 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010677 * Before curdir After
10678 * /foo/bar/file.txt /foo/bar ./file.txt
10679 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10680 * /file.txt / /file.txt
10681 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010682 */
10683 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010684 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010685#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010686 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010687 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010688 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010689 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010690 && !vim_ispathsep(*short_name)
10691#endif
10692 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010693 {
10694 STRCPY(path, ".");
10695 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010696 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010697 }
10698 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010699 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010700 }
10701
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010702 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010703 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010704 {
10705 char_u *rel_path;
10706 char_u *path = in_curdir[i];
10707
10708 if (path == NULL)
10709 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010710
10711 /* If the {filename} is not unique, change it to ./{filename}.
10712 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010713 short_name = shorten_fname(path, curdir);
10714 if (short_name == NULL)
10715 short_name = path;
10716 if (is_unique(short_name, gap, i))
10717 {
10718 STRCPY(fnames[i], short_name);
10719 continue;
10720 }
10721
10722 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10723 if (rel_path == NULL)
10724 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010725 STRCPY(rel_path, ".");
10726 add_pathsep(rel_path);
10727 STRCAT(rel_path, short_name);
10728
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010729 vim_free(fnames[i]);
10730 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010731 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010732 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010733 }
10734
Bram Moolenaar162bd912010-07-28 22:29:10 +020010735theend:
10736 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010737 if (in_curdir != NULL)
10738 {
10739 for (i = 0; i < gap->ga_len; i++)
10740 vim_free(in_curdir[i]);
10741 vim_free(in_curdir);
10742 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010743 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010744 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010745
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010746 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010747 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010748}
10749
10750/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010751 * Calls globpath() with 'path' values for the given pattern and stores the
10752 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010753 * Returns the total number of matches.
10754 */
10755 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010756expand_in_path(
10757 garray_T *gap,
10758 char_u *pattern,
10759 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010760{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010761 char_u *curdir;
10762 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010763 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010764
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010765 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010766 return 0;
10767 mch_dirname(curdir, MAXPATHL);
10768
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010769 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010770 expand_path_option(curdir, &path_ga);
10771 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010772 if (path_ga.ga_len == 0)
10773 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010774
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010775 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010776 ga_clear_strings(&path_ga);
10777 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010778 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010779
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010780 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010781 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010782
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010783 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010784}
10785#endif
10786
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010787#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10788/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010789 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10790 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010791 */
10792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010793remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010794{
10795 int i;
10796 int j;
10797 char_u **fnames = (char_u **)gap->ga_data;
10798
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010799 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010800 for (i = gap->ga_len - 1; i > 0; --i)
10801 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10802 {
10803 vim_free(fnames[i]);
10804 for (j = i + 1; j < gap->ga_len; ++j)
10805 fnames[j - 1] = fnames[j];
10806 --gap->ga_len;
10807 }
10808}
10809#endif
10810
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010811static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010812
10813/*
10814 * Return TRUE if "p" contains what looks like an environment variable.
10815 * Allowing for escaping.
10816 */
10817 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010818has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010819{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010820 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010821 {
10822 if (*p == '\\' && p[1] != NUL)
10823 ++p;
10824 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010825#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010826 "$%"
10827#else
10828 "$"
10829#endif
10830 , *p) != NULL)
10831 return TRUE;
10832 }
10833 return FALSE;
10834}
10835
10836#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010837static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010838
10839/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010840 * Return TRUE if "p" contains a special wildcard character, one that Vim
10841 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010842 */
10843 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010844has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010845{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010846 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010847 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010848 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010849 if (*p == '\\' && p[1] != NUL)
10850 ++p;
10851 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10852 return TRUE;
10853 }
10854 return FALSE;
10855}
10856#endif
10857
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858/*
10859 * Generic wildcard expansion code.
10860 *
10861 * Characters in "pat" that should not be expanded must be preceded with a
10862 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10863 *
10864 * Return FAIL when no single file was found. In this case "num_file" is not
10865 * set, and "file" may contain an error message.
10866 * Return OK when some files found. "num_file" is set to the number of
10867 * matches, "file" to the array of matches. Call FreeWild() later.
10868 */
10869 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010870gen_expand_wildcards(
10871 int num_pat, /* number of input patterns */
10872 char_u **pat, /* array of input patterns */
10873 int *num_file, /* resulting number of files */
10874 char_u ***file, /* array of resulting files */
10875 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876{
10877 int i;
10878 garray_T ga;
10879 char_u *p;
10880 static int recursive = FALSE;
10881 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010882 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010883#if defined(FEAT_SEARCHPATH)
10884 int did_expand_in_path = FALSE;
10885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886
10887 /*
10888 * expand_env() is called to expand things like "~user". If this fails,
10889 * it calls ExpandOne(), which brings us back here. In this case, always
10890 * call the machine specific expansion function, if possible. Otherwise,
10891 * return FAIL.
10892 */
10893 if (recursive)
10894#ifdef SPECIAL_WILDCHAR
10895 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10896#else
10897 return FAIL;
10898#endif
10899
10900#ifdef SPECIAL_WILDCHAR
10901 /*
10902 * If there are any special wildcard characters which we cannot handle
10903 * here, call machine specific function for all the expansion. This
10904 * avoids starting the shell for each argument separately.
10905 * For `=expr` do use the internal function.
10906 */
10907 for (i = 0; i < num_pat; i++)
10908 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010909 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910# ifdef VIM_BACKTICK
10911 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10912# endif
10913 )
10914 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10915 }
10916#endif
10917
10918 recursive = TRUE;
10919
10920 /*
10921 * The matching file names are stored in a growarray. Init it empty.
10922 */
10923 ga_init2(&ga, (int)sizeof(char_u *), 30);
10924
10925 for (i = 0; i < num_pat; ++i)
10926 {
10927 add_pat = -1;
10928 p = pat[i];
10929
10930#ifdef VIM_BACKTICK
10931 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010934 if (add_pat == -1)
10935 retval = FAIL;
10936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 else
10938#endif
10939 {
10940 /*
10941 * First expand environment variables, "~/" and "~user/".
10942 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010943 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010945 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 if (p == NULL)
10947 p = pat[i];
10948#ifdef UNIX
10949 /*
10950 * On Unix, if expand_env() can't expand an environment
10951 * variable, use the shell to do that. Discard previously
10952 * found file names and start all over again.
10953 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010954 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 {
10956 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010957 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010959 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 recursive = FALSE;
10961 return i;
10962 }
10963#endif
10964 }
10965
10966 /*
10967 * If there are wildcards: Expand file names and add each match to
10968 * the list. If there is no match, and EW_NOTFOUND is given, add
10969 * the pattern.
10970 * If there are no wildcards: Add the file name if it exists or
10971 * when EW_NOTFOUND is given.
10972 */
10973 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010974 {
10975#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010976 if ((flags & EW_PATH)
10977 && !mch_isFullName(p)
10978 && !(p[0] == '.'
10979 && (vim_ispathsep(p[1])
10980 || (p[1] == '.' && vim_ispathsep(p[2]))))
10981 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010982 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010983 /* :find completion where 'path' is used.
10984 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010985 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010986 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010987 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010988 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010989 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010990 else
10991#endif
10992 add_pat = mch_expandpath(&ga, p, flags);
10993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 }
10995
10996 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10997 {
10998 char_u *t = backslash_halve_save(p);
10999
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11001 * "vim c:/" work. */
11002 if (flags & EW_NOTFOUND)
11003 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 addfile(&ga, t, flags);
11006 vim_free(t);
11007 }
11008
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011009#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011010 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011011 uniquefy_paths(&ga, p);
11012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 if (p != pat[i])
11014 vim_free(p);
11015 }
11016
11017 *num_file = ga.ga_len;
11018 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11019
11020 recursive = FALSE;
11021
Bram Moolenaar336bd622016-01-17 18:23:58 +010011022 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023}
11024
11025# ifdef VIM_BACKTICK
11026
11027/*
11028 * Return TRUE if we can expand this backtick thing here.
11029 */
11030 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011031vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
11033 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11034}
11035
11036/*
11037 * Expand an item in `backticks` by executing it as a command.
11038 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011039 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 */
11041 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011042expand_backtick(
11043 garray_T *gap,
11044 char_u *pat,
11045 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046{
11047 char_u *p;
11048 char_u *cmd;
11049 char_u *buffer;
11050 int cnt = 0;
11051 int i;
11052
11053 /* Create the command: lop off the backticks. */
11054 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11055 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011056 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057
11058#ifdef FEAT_EVAL
11059 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011060 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 else
11062#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011063 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011064 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 vim_free(cmd);
11066 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011067 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068
11069 cmd = buffer;
11070 while (*cmd != NUL)
11071 {
11072 cmd = skipwhite(cmd); /* skip over white space */
11073 p = cmd;
11074 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11075 ++p;
11076 /* add an entry if it is not empty */
11077 if (p > cmd)
11078 {
11079 i = *p;
11080 *p = NUL;
11081 addfile(gap, cmd, flags);
11082 *p = i;
11083 ++cnt;
11084 }
11085 cmd = p;
11086 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11087 ++cmd;
11088 }
11089
11090 vim_free(buffer);
11091 return cnt;
11092}
11093# endif /* VIM_BACKTICK */
11094
11095/*
11096 * Add a file to a file list. Accepted flags:
11097 * EW_DIR add directories
11098 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011099 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 * EW_NOTFOUND add even when it doesn't exist
11101 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011102 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 */
11104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011105addfile(
11106 garray_T *gap,
11107 char_u *f, /* filename */
11108 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109{
11110 char_u *p;
11111 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011112 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011114 /* if the file/dir/link doesn't exist, may not add it */
11115 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011116 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 return;
11118
11119#ifdef FNAME_ILLEGAL
11120 /* if the file/dir contains illegal characters, don't add it */
11121 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11122 return;
11123#endif
11124
11125 isdir = mch_isdir(f);
11126 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11127 return;
11128
Bram Moolenaarb5971142015-03-21 17:32:19 +010011129 /* If the file isn't executable, may not add it. Do accept directories.
11130 * When invoked from expand_shellcmd() do not use $PATH. */
11131 if (!isdir && (flags & EW_EXEC)
11132 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011133 return;
11134
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 /* Make room for another item in the file list. */
11136 if (ga_grow(gap, 1) == FAIL)
11137 return;
11138
11139 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11140 if (p == NULL)
11141 return;
11142
11143 STRCPY(p, f);
11144#ifdef BACKSLASH_IN_FILENAME
11145 slash_adjust(p);
11146#endif
11147 /*
11148 * Append a slash or backslash after directory names if none is present.
11149 */
11150#ifndef DONT_ADD_PATHSEP_TO_DIR
11151 if (isdir && (flags & EW_ADDSLASH))
11152 add_pathsep(p);
11153#endif
11154 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155}
11156#endif /* !NO_EXPANDPATH */
11157
11158#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11159
11160#ifndef SEEK_SET
11161# define SEEK_SET 0
11162#endif
11163#ifndef SEEK_END
11164# define SEEK_END 2
11165#endif
11166
11167/*
11168 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011169 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11170 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 * Returns an allocated string, or NULL for error.
11172 */
11173 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011174get_cmd_output(
11175 char_u *cmd,
11176 char_u *infile, /* optional input file name */
11177 int flags, /* can be SHELL_SILENT */
11178 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179{
11180 char_u *tempname;
11181 char_u *command;
11182 char_u *buffer = NULL;
11183 int len;
11184 int i = 0;
11185 FILE *fd;
11186
11187 if (check_restricted() || check_secure())
11188 return NULL;
11189
11190 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011191 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 {
11193 EMSG(_(e_notmp));
11194 return NULL;
11195 }
11196
11197 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011198 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 if (command == NULL)
11200 goto done;
11201
11202 /*
11203 * Call the shell to execute the command (errors are ignored).
11204 * Don't check timestamps here.
11205 */
11206 ++no_check_timestamps;
11207 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11208 --no_check_timestamps;
11209
11210 vim_free(command);
11211
11212 /*
11213 * read the names from the file into memory
11214 */
11215# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011216 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 fd = mch_fopen((char *)tempname, "r");
11218# else
11219 fd = mch_fopen((char *)tempname, READBIN);
11220# endif
11221
11222 if (fd == NULL)
11223 {
11224 EMSG2(_(e_notopen), tempname);
11225 goto done;
11226 }
11227
11228 fseek(fd, 0L, SEEK_END);
11229 len = ftell(fd); /* get size of temp file */
11230 fseek(fd, 0L, SEEK_SET);
11231
11232 buffer = alloc(len + 1);
11233 if (buffer != NULL)
11234 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11235 fclose(fd);
11236 mch_remove(tempname);
11237 if (buffer == NULL)
11238 goto done;
11239#ifdef VMS
11240 len = i; /* VMS doesn't give us what we asked for... */
11241#endif
11242 if (i != len)
11243 {
11244 EMSG2(_(e_notread), tempname);
11245 vim_free(buffer);
11246 buffer = NULL;
11247 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011248 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011249 {
11250 /* Change NUL into SOH, otherwise the string is truncated. */
11251 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011252 if (buffer[i] == NUL)
11253 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011254
Bram Moolenaar162bd912010-07-28 22:29:10 +020011255 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011256 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011257 else
11258 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259
11260done:
11261 vim_free(tempname);
11262 return buffer;
11263}
11264#endif
11265
11266/*
11267 * Free the list of files returned by expand_wildcards() or other expansion
11268 * functions.
11269 */
11270 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011271FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011273 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 while (count--)
11276 vim_free(files[count]);
11277 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
11280/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011281 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 * Don't do this when still processing a command or a mapping.
11283 * Don't do this when inside a ":normal" command.
11284 */
11285 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011286goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
11288 return (p_im && stuff_empty() && typebuf_typed());
11289}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011290
11291/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011292 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011293 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11294 * - Remove any argument. E.g., "csh -f" -> "csh".
11295 * But don't allow a space in the path, so that this works:
11296 * "/usr/bin/csh --rcfile ~/.cshrc"
11297 * But don't do that for Windows, it's common to have a space in the path.
11298 */
11299 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011300get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011301{
11302 char_u *p;
11303
11304#ifdef WIN3264
11305 p = gettail(p_sh);
11306 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11307#else
11308 p = skiptowhite(p_sh);
11309 if (*p == NUL)
11310 {
11311 /* No white space, use the tail. */
11312 p = vim_strsave(gettail(p_sh));
11313 }
11314 else
11315 {
11316 char_u *p1, *p2;
11317
11318 /* Find the last path separator before the space. */
11319 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011320 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011321 if (vim_ispathsep(*p2))
11322 p1 = p2 + 1;
11323 p = vim_strnsave(p1, (int)(p - p1));
11324 }
11325#endif
11326 return p;
11327}