blob: 1ab55f0862823f1e517162fa7948d3c09a5a58fd [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 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200591 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 */
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 */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200609 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610#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
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001645 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646theend:
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 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002460 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
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
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002479 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (col >= oldlen)
2481 return FAIL;
2482
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002483 /* If "count" is zero there is nothing to do. */
2484 if (count == 0)
2485 return OK;
2486
2487 /* If "count" is negative the caller must be doing something wrong. */
2488 if (count < 1)
2489 {
2490 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2491 return FAIL;
2492 }
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#ifdef FEAT_MBYTE
2495 /* If 'delcombine' is set and deleting (less than) one character, only
2496 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002497 if (p_deco && use_delcombine && enc_utf8
2498 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002500 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 int n;
2502
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002503 (void)utfc_ptr2char(oldp + col, cc);
2504 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
2506 /* Find the last composing char, there can be several. */
2507 n = col;
2508 do
2509 {
2510 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002511 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 n += count;
2513 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2514 fixpos = 0;
2515 }
2516 }
2517#endif
2518
2519 /*
2520 * When count is too big, reduce it.
2521 */
2522 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2523 if (movelen <= 1)
2524 {
2525 /*
2526 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002527 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2528 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002530 if (col > 0 && fixpos && restart_edit == 0
2531#ifdef FEAT_VIRTUALEDIT
2532 && (ve_flags & VE_ONEMORE) == 0
2533#endif
2534 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 --curwin->w_cursor.col;
2537#ifdef FEAT_VIRTUALEDIT
2538 curwin->w_cursor.coladd = 0;
2539#endif
2540#ifdef FEAT_MBYTE
2541 if (has_mbyte)
2542 curwin->w_cursor.col -=
2543 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2544#endif
2545 }
2546 count = oldlen - col;
2547 movelen = 1;
2548 }
2549
2550 /*
2551 * If the old line has been allocated the deletion can be done in the
2552 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002553 * Can't do this when using Netbeans, because we would need to invoke
2554 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002555 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002558 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002559 was_alloced = FALSE;
2560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002562 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (was_alloced)
2564 newp = oldp; /* use same allocated memory */
2565 else
2566 { /* need to allocate a new line */
2567 newp = alloc((unsigned)(oldlen + 1 - count));
2568 if (newp == NULL)
2569 return FAIL;
2570 mch_memmove(newp, oldp, (size_t)col);
2571 }
2572 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2573 if (!was_alloced)
2574 ml_replace(lnum, newp, FALSE);
2575
2576 /* mark the buffer as changed and prepare for displaying */
2577 changed_bytes(lnum, curwin->w_cursor.col);
2578
2579 return OK;
2580}
2581
2582/*
2583 * Delete from cursor to end of line.
2584 * Caller must have prepared for undo.
2585 *
2586 * return FAIL for failure, OK otherwise
2587 */
2588 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002589truncate_line(
2590 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 char_u *newp;
2593 linenr_T lnum = curwin->w_cursor.lnum;
2594 colnr_T col = curwin->w_cursor.col;
2595
2596 if (col == 0)
2597 newp = vim_strsave((char_u *)"");
2598 else
2599 newp = vim_strnsave(ml_get(lnum), col);
2600
2601 if (newp == NULL)
2602 return FAIL;
2603
2604 ml_replace(lnum, newp, FALSE);
2605
2606 /* mark the buffer as changed and prepare for displaying */
2607 changed_bytes(lnum, curwin->w_cursor.col);
2608
2609 /*
2610 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2611 */
2612 if (fixpos && curwin->w_cursor.col > 0)
2613 --curwin->w_cursor.col;
2614
2615 return OK;
2616}
2617
2618/*
2619 * Delete "nlines" lines at the cursor.
2620 * Saves the lines for undo first if "undo" is TRUE.
2621 */
2622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002623del_lines(
2624 long nlines, /* number of lines to delete */
2625 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626{
2627 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002628 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
2630 if (nlines <= 0)
2631 return;
2632
2633 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002634 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return;
2636
2637 for (n = 0; n < nlines; )
2638 {
2639 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2640 break;
2641
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002642 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 ++n;
2644
2645 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002646 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 break;
2648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002650 /* Correct the cursor position before calling deleted_lines_mark(), it may
2651 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 curwin->w_cursor.col = 0;
2653 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002654
2655 /* adjust marks, mark the buffer as changed and prepare for displaying */
2656 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657}
2658
2659 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002660gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002662 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002664 /* When searching columns is sometimes put at the end of a line. */
2665 if (pos->col == MAXCOL)
2666 return NUL;
2667 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668#ifdef FEAT_MBYTE
2669 if (has_mbyte)
2670 return (*mb_ptr2char)(ptr);
2671#endif
2672 return (int)*ptr;
2673}
2674
2675 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002676gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678#ifdef FEAT_MBYTE
2679 if (has_mbyte)
2680 return (*mb_ptr2char)(ml_get_cursor());
2681#endif
2682 return (int)*ml_get_cursor();
2683}
2684
2685/*
2686 * Write a character at the current cursor position.
2687 * It is directly written into the block.
2688 */
2689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2693 + curwin->w_cursor.col) = c;
2694}
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696/*
2697 * When extra == 0: Return TRUE if the cursor is before or on the first
2698 * non-blank in the line.
2699 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2700 * the line.
2701 */
2702 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002703inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 char_u *ptr;
2706 colnr_T col;
2707
Bram Moolenaar1c465442017-03-12 20:10:05 +01002708 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 ++ptr;
2710 if (col >= curwin->w_cursor.col + extra)
2711 return TRUE;
2712 else
2713 return FALSE;
2714}
2715
2716/*
2717 * Skip to next part of an option argument: Skip space and comma.
2718 */
2719 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002720skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 if (*p == ',')
2723 ++p;
2724 while (*p == ' ')
2725 ++p;
2726 return p;
2727}
2728
2729/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002730 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 *
2732 * Most often called through changed_bytes() and changed_lines(), which also
2733 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002734 *
2735 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 */
2737 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002738changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002741 if (p_imst == IM_ON_THE_SPOT)
2742 {
2743 /* The text of the preediting area is inserted, but this doesn't
2744 * mean a change of the buffer yet. That is delayed until the
2745 * text is committed. (this means preedit becomes empty) */
2746 if (im_is_preediting() && !xim_changed_while_preediting)
2747 return;
2748 xim_changed_while_preediting = FALSE;
2749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750#endif
2751
2752 if (!curbuf->b_changed)
2753 {
2754 int save_msg_scroll = msg_scroll;
2755
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002756 /* Give a warning about changing a read-only file. This may also
2757 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 /* Create a swap file if that is wanted.
2761 * Don't do this for "nofile" and "nowrite" buffer types. */
2762 if (curbuf->b_may_swap
2763#ifdef FEAT_QUICKFIX
2764 && !bt_dontwrite(curbuf)
2765#endif
2766 )
2767 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002768 int save_need_wait_return = need_wait_return;
2769
2770 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 ml_open_file(curbuf);
2772
2773 /* The ml_open_file() can cause an ATTENTION message.
2774 * Wait two seconds, to make sure the user reads this unexpected
2775 * message. Since we could be anywhere, call wait_return() now,
2776 * and don't let the emsg() set msg_scroll. */
2777 if (need_wait_return && emsg_silent == 0)
2778 {
2779 out_flush();
2780 ui_delay(2000L, TRUE);
2781 wait_return(TRUE);
2782 msg_scroll = save_msg_scroll;
2783 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002784 else
2785 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002787 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002789 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790}
2791
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002792/*
2793 * Internal part of changed(), no user interaction.
2794 */
2795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002796changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002797{
2798 curbuf->b_changed = TRUE;
2799 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002800 check_status(curbuf);
2801 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002802#ifdef FEAT_TITLE
2803 need_maketitle = TRUE; /* set window title later */
2804#endif
2805}
2806
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002807static void changedOneline(buf_T *buf, linenr_T lnum);
2808static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2809static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811/*
2812 * Changed bytes within a single line for the current buffer.
2813 * - marks the windows on this buffer to be redisplayed
2814 * - marks the buffer changed by calling changed()
2815 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002816 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 */
2818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002819changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002821 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002823
2824#ifdef FEAT_DIFF
2825 /* Diff highlighting in other diff windows may need to be updated too. */
2826 if (curwin->w_p_diff)
2827 {
2828 win_T *wp;
2829 linenr_T wlnum;
2830
Bram Moolenaar29323592016-07-24 22:04:11 +02002831 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002832 if (wp->w_p_diff && wp != curwin)
2833 {
2834 redraw_win_later(wp, VALID);
2835 wlnum = diff_lnum_win(lnum, wp);
2836 if (wlnum > 0)
2837 changedOneline(wp->w_buffer, wlnum);
2838 }
2839 }
2840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841}
2842
2843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002844changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002846 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 {
2848 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002849 if (lnum < buf->b_mod_top)
2850 buf->b_mod_top = lnum;
2851 else if (lnum >= buf->b_mod_bot)
2852 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
2854 else
2855 {
2856 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002857 buf->b_mod_set = TRUE;
2858 buf->b_mod_top = lnum;
2859 buf->b_mod_bot = lnum + 1;
2860 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862}
2863
2864/*
2865 * Appended "count" lines below line "lnum" in the current buffer.
2866 * Must be called AFTER the change and after mark_adjust().
2867 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2868 */
2869 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002870appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 changed_lines(lnum + 1, 0, lnum + 1, count);
2873}
2874
2875/*
2876 * Like appended_lines(), but adjust marks first.
2877 */
2878 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002879appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002881 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002882 * be marks there. But it's still needed in diff mode. */
2883 if (lnum + count < curbuf->b_ml.ml_line_count
2884#ifdef FEAT_DIFF
2885 || curwin->w_p_diff
2886#endif
2887 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002888 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 changed_lines(lnum + 1, 0, lnum + 1, count);
2890}
2891
2892/*
2893 * Deleted "count" lines at line "lnum" in the current buffer.
2894 * Must be called AFTER the change and after mark_adjust().
2895 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2896 */
2897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002898deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 changed_lines(lnum, 0, lnum + count, -count);
2901}
2902
2903/*
2904 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002905 * Make sure the cursor is on a valid line before calling, a GUI callback may
2906 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 */
2908 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002909deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2912 changed_lines(lnum, 0, lnum + count, -count);
2913}
2914
2915/*
2916 * Changed lines for the current buffer.
2917 * Must be called AFTER the change and after mark_adjust().
2918 * - mark the buffer changed by calling changed()
2919 * - mark the windows on this buffer to be redisplayed
2920 * - invalidate cached values
2921 * "lnum" is the first line that needs displaying, "lnume" the first line
2922 * below the changed lines (BEFORE the change).
2923 * When only inserting lines, "lnum" and "lnume" are equal.
2924 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002925 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
2927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002928changed_lines(
2929 linenr_T lnum, /* first line with change */
2930 colnr_T col, /* column in first line with change */
2931 linenr_T lnume, /* line below last changed line */
2932 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002934 changed_lines_buf(curbuf, lnum, lnume, xtra);
2935
2936#ifdef FEAT_DIFF
2937 if (xtra == 0 && curwin->w_p_diff)
2938 {
2939 /* When the number of lines doesn't change then mark_adjust() isn't
2940 * called and other diff buffers still need to be marked for
2941 * displaying. */
2942 win_T *wp;
2943 linenr_T wlnum;
2944
Bram Moolenaar29323592016-07-24 22:04:11 +02002945 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002946 if (wp->w_p_diff && wp != curwin)
2947 {
2948 redraw_win_later(wp, VALID);
2949 wlnum = diff_lnum_win(lnum, wp);
2950 if (wlnum > 0)
2951 changed_lines_buf(wp->w_buffer, wlnum,
2952 lnume - lnum + wlnum, 0L);
2953 }
2954 }
2955#endif
2956
2957 changed_common(lnum, col, lnume, xtra);
2958}
2959
2960 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002961changed_lines_buf(
2962 buf_T *buf,
2963 linenr_T lnum, /* first line with change */
2964 linenr_T lnume, /* line below last changed line */
2965 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002966{
2967 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
2969 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002970 if (lnum < buf->b_mod_top)
2971 buf->b_mod_top = lnum;
2972 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
2974 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002975 buf->b_mod_bot += xtra;
2976 if (buf->b_mod_bot < lnum)
2977 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002979 if (lnume + xtra > buf->b_mod_bot)
2980 buf->b_mod_bot = lnume + xtra;
2981 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
2983 else
2984 {
2985 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002986 buf->b_mod_set = TRUE;
2987 buf->b_mod_top = lnum;
2988 buf->b_mod_bot = lnume + xtra;
2989 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991}
2992
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002993/*
2994 * Common code for when a change is was made.
2995 * See changed_lines() for the arguments.
2996 * Careful: may trigger autocommands that reload the buffer.
2997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002999changed_common(
3000 linenr_T lnum,
3001 colnr_T col,
3002 linenr_T lnume,
3003 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003006 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 int i;
3008#ifdef FEAT_JUMPLIST
3009 int cols;
3010 pos_T *p;
3011 int add;
3012#endif
3013
3014 /* mark the buffer as modified */
3015 changed();
3016
3017 /* set the '. mark */
3018 if (!cmdmod.keepjumps)
3019 {
3020 curbuf->b_last_change.lnum = lnum;
3021 curbuf->b_last_change.col = col;
3022
3023#ifdef FEAT_JUMPLIST
3024 /* Create a new entry if a new undo-able change was started or we
3025 * don't have an entry yet. */
3026 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3027 {
3028 if (curbuf->b_changelistlen == 0)
3029 add = TRUE;
3030 else
3031 {
3032 /* Don't create a new entry when the line number is the same
3033 * as the last one and the column is not too far away. Avoids
3034 * creating many entries for typing "xxxxx". */
3035 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3036 if (p->lnum != lnum)
3037 add = TRUE;
3038 else
3039 {
3040 cols = comp_textwidth(FALSE);
3041 if (cols == 0)
3042 cols = 79;
3043 add = (p->col + cols < col || col + cols < p->col);
3044 }
3045 }
3046 if (add)
3047 {
3048 /* This is the first of a new sequence of undo-able changes
3049 * and it's at some distance of the last change. Use a new
3050 * position in the changelist. */
3051 curbuf->b_new_change = FALSE;
3052
3053 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3054 {
3055 /* changelist is full: remove oldest entry */
3056 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3057 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3058 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003059 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 {
3061 /* Correct position in changelist for other windows on
3062 * this buffer. */
3063 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3064 --wp->w_changelistidx;
3065 }
3066 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003067 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
3069 /* For other windows, if the position in the changelist is
3070 * at the end it stays at the end. */
3071 if (wp->w_buffer == curbuf
3072 && wp->w_changelistidx == curbuf->b_changelistlen)
3073 ++wp->w_changelistidx;
3074 }
3075 ++curbuf->b_changelistlen;
3076 }
3077 }
3078 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3079 curbuf->b_last_change;
3080 /* The current window is always after the last change, so that "g,"
3081 * takes you back to it. */
3082 curwin->w_changelistidx = curbuf->b_changelistlen;
3083#endif
3084 }
3085
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003086 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
3088 if (wp->w_buffer == curbuf)
3089 {
3090 /* Mark this window to be redrawn later. */
3091 if (wp->w_redr_type < VALID)
3092 wp->w_redr_type = VALID;
3093
3094 /* Check if a change in the buffer has invalidated the cached
3095 * values for the cursor. */
3096#ifdef FEAT_FOLDING
3097 /*
3098 * Update the folds for this window. Can't postpone this, because
3099 * a following operator might work on the whole fold: ">>dd".
3100 */
3101 foldUpdate(wp, lnum, lnume + xtra - 1);
3102
3103 /* The change may cause lines above or below the change to become
3104 * included in a fold. Set lnum/lnume to the first/last line that
3105 * might be displayed differently.
3106 * Set w_cline_folded here as an efficient way to update it when
3107 * inserting lines just above a closed fold. */
3108 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3109 if (wp->w_cursor.lnum == lnum)
3110 wp->w_cline_folded = i;
3111 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3112 if (wp->w_cursor.lnum == lnume)
3113 wp->w_cline_folded = i;
3114
3115 /* If the changed line is in a range of previously folded lines,
3116 * compare with the first line in that range. */
3117 if (wp->w_cursor.lnum <= lnum)
3118 {
3119 i = find_wl_entry(wp, lnum);
3120 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3121 changed_line_abv_curs_win(wp);
3122 }
3123#endif
3124
3125 if (wp->w_cursor.lnum > lnum)
3126 changed_line_abv_curs_win(wp);
3127 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3128 changed_cline_bef_curs_win(wp);
3129 if (wp->w_botline >= lnum)
3130 {
3131 /* Assume that botline doesn't change (inserted lines make
3132 * other lines scroll down below botline). */
3133 approximate_botline_win(wp);
3134 }
3135
3136 /* Check if any w_lines[] entries have become invalid.
3137 * For entries below the change: Correct the lnums for
3138 * inserted/deleted lines. Makes it possible to stop displaying
3139 * after the change. */
3140 for (i = 0; i < wp->w_lines_valid; ++i)
3141 if (wp->w_lines[i].wl_valid)
3142 {
3143 if (wp->w_lines[i].wl_lnum >= lnum)
3144 {
3145 if (wp->w_lines[i].wl_lnum < lnume)
3146 {
3147 /* line included in change */
3148 wp->w_lines[i].wl_valid = FALSE;
3149 }
3150 else if (xtra != 0)
3151 {
3152 /* line below change */
3153 wp->w_lines[i].wl_lnum += xtra;
3154#ifdef FEAT_FOLDING
3155 wp->w_lines[i].wl_lastlnum += xtra;
3156#endif
3157 }
3158 }
3159#ifdef FEAT_FOLDING
3160 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3161 {
3162 /* change somewhere inside this range of folded lines,
3163 * may need to be redrawn */
3164 wp->w_lines[i].wl_valid = FALSE;
3165 }
3166#endif
3167 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003168
3169#ifdef FEAT_FOLDING
3170 /* Take care of side effects for setting w_topline when folds have
3171 * changed. Esp. when the buffer was changed in another window. */
3172 if (hasAnyFolding(wp))
3173 set_topline(wp, wp->w_topline);
3174#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003175 /* relative numbering may require updating more */
3176 if (wp->w_p_rnu)
3177 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179 }
3180
3181 /* Call update_screen() later, which checks out what needs to be redrawn,
3182 * since it notices b_mod_set and then uses b_mod_*. */
3183 if (must_redraw < VALID)
3184 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003185
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003186 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003187 if (lnum <= curwin->w_cursor.lnum
3188 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003189 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190}
3191
3192/*
3193 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3194 */
3195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003196unchanged(
3197 buf_T *buf,
3198 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003200 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
3202 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003203 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (ff)
3205 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003207 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#ifdef FEAT_TITLE
3209 need_maketitle = TRUE; /* set window title later */
3210#endif
3211 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003212 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213#ifdef FEAT_NETBEANS_INTG
3214 netbeans_unmodified(buf);
3215#endif
3216}
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218/*
3219 * check_status: called when the status bars for the buffer 'buf'
3220 * need to be updated
3221 */
3222 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003223check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 win_T *wp;
3226
Bram Moolenaar29323592016-07-24 22:04:11 +02003227 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (wp->w_buffer == buf && wp->w_status_height)
3229 {
3230 wp->w_redr_status = TRUE;
3231 if (must_redraw < VALID)
3232 must_redraw = VALID;
3233 }
3234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236/*
3237 * If the file is readonly, give a warning message with the first change.
3238 * Don't do this for autocommands.
3239 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003240 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003242 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
3244 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003245change_warning(
3246 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 mode and 'showmode' is on */
3248{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003249 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (curbuf->b_did_warn == FALSE
3252 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 && curbuf->b_p_ro)
3255 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003256 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003258 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (!curbuf->b_p_ro)
3260 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 /*
3262 * Do what msg() does, but with a column offset if the warning should
3263 * be after the mode message.
3264 */
3265 msg_start();
3266 if (msg_row == Rows - 1)
3267 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003268 msg_source(HL_ATTR(HLF_W));
3269 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003270#ifdef FEAT_EVAL
3271 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 msg_clr_eos();
3274 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003275 if (msg_silent == 0 && !silent_mode
3276#ifdef FEAT_EVAL
3277 && time_for_testing != 1
3278#endif
3279 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 out_flush();
3282 ui_delay(1000L, TRUE); /* give the user time to think about it */
3283 }
3284 curbuf->b_did_warn = TRUE;
3285 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3286 if (msg_row < Rows - 1)
3287 showmode();
3288 }
3289}
3290
3291/*
3292 * Ask for a reply from the user, a 'y' or a 'n'.
3293 * No other characters are accepted, the message is repeated until a valid
3294 * reply is entered or CTRL-C is hit.
3295 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3296 * from any buffers but directly from the user.
3297 *
3298 * return the 'y' or 'n'
3299 */
3300 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003301ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 int r = ' ';
3304 int save_State = State;
3305
3306 if (exiting) /* put terminal in raw mode for this question */
3307 settmode(TMODE_RAW);
3308 ++no_wait_return;
3309#ifdef USE_ON_FLY_SCROLL
3310 dont_scroll = TRUE; /* disallow scrolling here */
3311#endif
3312 State = CONFIRM; /* mouse behaves like with :confirm */
3313#ifdef FEAT_MOUSE
3314 setmouse(); /* disables mouse for xterm */
3315#endif
3316 ++no_mapping;
3317 ++allow_keys; /* no mapping here, but recognize keys */
3318
3319 while (r != 'y' && r != 'n')
3320 {
3321 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003322 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (direct)
3324 r = get_keystroke();
3325 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003326 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (r == Ctrl_C || r == ESC)
3328 r = 'n';
3329 msg_putchar(r); /* show what you typed */
3330 out_flush();
3331 }
3332 --no_wait_return;
3333 State = save_State;
3334#ifdef FEAT_MOUSE
3335 setmouse();
3336#endif
3337 --no_mapping;
3338 --allow_keys;
3339
3340 return r;
3341}
3342
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003343#if defined(FEAT_MOUSE) || defined(PROTO)
3344/*
3345 * Return TRUE if "c" is a mouse key.
3346 */
3347 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003348is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003349{
3350 return c == K_LEFTMOUSE
3351 || c == K_LEFTMOUSE_NM
3352 || c == K_LEFTDRAG
3353 || c == K_LEFTRELEASE
3354 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003355 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003356 || c == K_MIDDLEMOUSE
3357 || c == K_MIDDLEDRAG
3358 || c == K_MIDDLERELEASE
3359 || c == K_RIGHTMOUSE
3360 || c == K_RIGHTDRAG
3361 || c == K_RIGHTRELEASE
3362 || c == K_MOUSEDOWN
3363 || c == K_MOUSEUP
3364 || c == K_MOUSELEFT
3365 || c == K_MOUSERIGHT
3366 || c == K_X1MOUSE
3367 || c == K_X1DRAG
3368 || c == K_X1RELEASE
3369 || c == K_X2MOUSE
3370 || c == K_X2DRAG
3371 || c == K_X2RELEASE;
3372}
3373#endif
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375/*
3376 * Get a key stroke directly from the user.
3377 * Ignores mouse clicks and scrollbar events, except a click for the left
3378 * button (used at the more prompt).
3379 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3380 * Disadvantage: typeahead is ignored.
3381 * Translates the interrupt character for unix to ESC.
3382 */
3383 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003384get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003386 char_u *buf = NULL;
3387 int buflen = 150;
3388 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 int len = 0;
3390 int n;
3391 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003392 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394 mapped_ctrl_c = FALSE; /* mappings are not used here */
3395 for (;;)
3396 {
3397 cursor_on();
3398 out_flush();
3399
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003400 /* Leave some room for check_termcode() to insert a key code into (max
3401 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3402 * bytes. */
3403 maxlen = (buflen - 6 - len) / 3;
3404 if (buf == NULL)
3405 buf = alloc(buflen);
3406 else if (maxlen < 10)
3407 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003408 char_u *t_buf = buf;
3409
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003410 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003411 * escape sequence. */
3412 buflen += 100;
3413 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003414 if (buf == NULL)
3415 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003416 maxlen = (buflen - 6 - len) / 3;
3417 }
3418 if (buf == NULL)
3419 {
3420 do_outofmem_msg((long_u)buflen);
3421 return ESC; /* panic! */
3422 }
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003425 * terminal code to complete. */
3426 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (n > 0)
3428 {
3429 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003430 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003432 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003434 else if (len > 0)
3435 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar4395a712006-09-05 18:57:57 +00003437 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003438 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003439 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003441
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003442 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003443 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003444 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003445 {
3446 /* Redrawing was postponed, do it now. */
3447 update_screen(0);
3448 setcursor(); /* put cursor back where it belongs */
3449 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003450 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003451 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003452 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003454 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 continue;
3456
3457 /* Handle modifier and/or special key code. */
3458 n = buf[0];
3459 if (n == K_SPECIAL)
3460 {
3461 n = TO_SPECIAL(buf[1], buf[2]);
3462 if (buf[1] == KS_MODIFIER
3463 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003464#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003465 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003466#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003467#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 || n == K_VER_SCROLLBAR
3469 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#endif
3471 )
3472 {
3473 if (buf[1] == KS_MODIFIER)
3474 mod_mask = buf[2];
3475 len -= 3;
3476 if (len > 0)
3477 mch_memmove(buf, buf + 3, (size_t)len);
3478 continue;
3479 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003480 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482#ifdef FEAT_MBYTE
3483 if (has_mbyte)
3484 {
3485 if (MB_BYTE2LEN(n) > len)
3486 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003487 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 n = (*mb_ptr2char)(buf);
3489 }
3490#endif
3491#ifdef UNIX
3492 if (n == intr_char)
3493 n = ESC;
3494#endif
3495 break;
3496 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003497 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 mapped_ctrl_c = save_mapped_ctrl_c;
3500 return n;
3501}
3502
3503/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003504 * Get a number from the user.
3505 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 */
3507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003508get_number(
3509 int colon, /* allow colon to abort */
3510 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
3512 int n = 0;
3513 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003514 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003516 if (mouse_used != NULL)
3517 *mouse_used = FALSE;
3518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 /* When not printing messages, the user won't know what to type, return a
3520 * zero (as if CR was hit). */
3521 if (msg_silent != 0)
3522 return 0;
3523
3524#ifdef USE_ON_FLY_SCROLL
3525 dont_scroll = TRUE; /* disallow scrolling here */
3526#endif
3527 ++no_mapping;
3528 ++allow_keys; /* no mapping here, but recognize keys */
3529 for (;;)
3530 {
3531 windgoto(msg_row, msg_col);
3532 c = safe_vgetc();
3533 if (VIM_ISDIGIT(c))
3534 {
3535 n = n * 10 + c - '0';
3536 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003537 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3540 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003541 if (typed > 0)
3542 {
3543 MSG_PUTS("\b \b");
3544 --typed;
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003548#ifdef FEAT_MOUSE
3549 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3550 {
3551 *mouse_used = TRUE;
3552 n = mouse_row + 1;
3553 break;
3554 }
3555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else if (n == 0 && c == ':' && colon)
3557 {
3558 stuffcharReadbuff(':');
3559 if (!exmode_active)
3560 cmdline_row = msg_row;
3561 skip_redraw = TRUE; /* skip redraw once */
3562 do_redraw = FALSE;
3563 break;
3564 }
3565 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3566 break;
3567 }
3568 --no_mapping;
3569 --allow_keys;
3570 return n;
3571}
3572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003573/*
3574 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003575 * When "mouse_used" is not NULL allow using the mouse and in that case return
3576 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003577 */
3578 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003579prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580{
3581 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003582 int save_cmdline_row;
3583 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584
3585 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003586 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003587 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003588 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003589 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003590
Bram Moolenaar203335e2006-09-03 14:35:42 +00003591 /* Set the state such that text can be selected/copied/pasted and we still
3592 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003593 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003594 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003595 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003596 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaar73658312018-04-24 17:41:57 +02003597#ifdef FEAT_MOUSE
3598 /* May show different mouse shape. */
3599 setmouse();
3600#endif
3601
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003602
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003603 i = get_number(TRUE, mouse_used);
3604 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003605 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003606 /* don't call wait_return() now */
3607 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 cmdline_row = msg_row - 1;
3609 need_wait_return = FALSE;
3610 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003611 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003613 else
3614 cmdline_row = save_cmdline_row;
3615 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003616#ifdef FEAT_MOUSE
3617 /* May need to restore mouse shape. */
3618 setmouse();
3619#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003621 return i;
3622}
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003625msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 long pn;
3628
3629 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3631 return;
3632
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003633 /* We don't want to overwrite another important message, but do overwrite
3634 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3635 * then "put" reports the last action. */
3636 if (keep_msg != NULL && !keep_msg_more)
3637 return;
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (n > 0)
3640 pn = n;
3641 else
3642 pn = -n;
3643
3644 if (pn > p_report)
3645 {
3646 if (pn == 1)
3647 {
3648 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003649 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3650 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003652 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3653 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
3655 else
3656 {
3657 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003658 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3659 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003661 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3662 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003665 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (msg(msg_buf))
3667 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003668 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003669 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 }
3672}
3673
3674/*
3675 * flush map and typeahead buffers and give a warning for an error
3676 */
3677 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003678beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 if (emsg_silent == 0)
3681 {
3682 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003683 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 }
3685}
3686
3687/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003688 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 */
3690 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003691vim_beep(
3692 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003694#ifdef FEAT_EVAL
3695 called_vim_beep = TRUE;
3696#endif
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (emsg_silent == 0)
3699 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003700 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3701 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003702#ifdef ELAPSED_FUNC
3703 static int did_init = FALSE;
3704 static ELAPSED_TYPE start_tv;
3705
3706 /* Only beep once per half a second, otherwise a sequence of beeps
3707 * would freeze Vim. */
3708 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3709 {
3710 did_init = TRUE;
3711 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003713 if (p_vb
3714#ifdef FEAT_GUI
3715 /* While the GUI is starting up the termcap is set for
3716 * the GUI but the output still goes to a terminal. */
3717 && !(gui.in_use && gui.starting)
3718#endif
3719 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003720 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003721 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003722#ifdef FEAT_VTP
3723 /* No restore color information, refresh the screen. */
3724 if (has_vtp_working() != 0
3725# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003726 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003727# endif
3728 )
3729 {
3730 redraw_later(CLEAR);
3731 update_screen(0);
3732 redrawcmd();
3733 }
3734#endif
3735 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003736 else
3737 out_char(BELL);
3738#ifdef ELAPSED_FUNC
3739 }
3740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003742
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003743 /* When 'debug' contains "beep" produce a message. If we are sourcing
3744 * a script or executing a function give the user a hint where the beep
3745 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003746 if (vim_strchr(p_debug, 'e') != NULL)
3747 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003748 msg_source(HL_ATTR(HLF_W));
3749 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752}
3753
3754/*
3755 * To get the "real" home directory:
3756 * - get value of $HOME
3757 * For Unix:
3758 * - go to that directory
3759 * - do mch_dirname() to get the real name of that directory.
3760 * This also works with mounts and links.
3761 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3762 */
3763static char_u *homedir = NULL;
3764
3765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003766init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
3768 char_u *var;
3769
Bram Moolenaar05159a02005-02-26 23:04:13 +00003770 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003771 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773#ifdef VMS
3774 var = mch_getenv((char_u *)"SYS$LOGIN");
3775#else
3776 var = mch_getenv((char_u *)"HOME");
3777#endif
3778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779#ifdef WIN3264
3780 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003781 * Typically, $HOME is not defined on Windows, unless the user has
3782 * specifically defined it for Vim's sake. However, on Windows NT
3783 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3784 * each user. Try constructing $HOME from these.
3785 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003786 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003787 {
3788 char_u *homedrive, *homepath;
3789
3790 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3791 homepath = mch_getenv((char_u *)"HOMEPATH");
3792 if (homepath == NULL || *homepath == NUL)
3793 homepath = (char_u *)"\\";
3794 if (homedrive != NULL
3795 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3796 {
3797 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3798 if (NameBuff[0] != NUL)
3799 var = NameBuff;
3800 }
3801 }
3802
3803 if (var == NULL)
3804 var = mch_getenv((char_u *)"USERPROFILE");
3805
3806 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 * Weird but true: $HOME may contain an indirect reference to another
3808 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3809 * when $HOME is being set.
3810 */
3811 if (var != NULL && *var == '%')
3812 {
3813 char_u *p;
3814 char_u *exp;
3815
3816 p = vim_strchr(var + 1, '%');
3817 if (p != NULL)
3818 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003819 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 exp = mch_getenv(NameBuff);
3821 if (exp != NULL && *exp != NUL
3822 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3823 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003824 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827 }
3828 }
3829
Bram Moolenaar48340b62017-08-29 22:08:53 +02003830 if (var != NULL && *var == NUL) /* empty is same as not set */
3831 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Bram Moolenaar48340b62017-08-29 22:08:53 +02003833# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003834 if (enc_utf8 && var != NULL)
3835 {
3836 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003837 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003838
3839 /* Convert from active codepage to UTF-8. Other conversions are
3840 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003841 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003842 if (pp != NULL)
3843 {
3844 homedir = pp;
3845 return;
3846 }
3847 }
3848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 /*
3851 * Default home dir is C:/
3852 * Best assumption we can make in such a situation.
3853 */
3854 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003855 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (var != NULL)
3859 {
3860#ifdef UNIX
3861 /*
3862 * Change to the directory and get the actual path. This resolves
3863 * links. Don't do it when we can't return.
3864 */
3865 if (mch_dirname(NameBuff, MAXPATHL) == OK
3866 && mch_chdir((char *)NameBuff) == 0)
3867 {
3868 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3869 var = IObuff;
3870 if (mch_chdir((char *)NameBuff) != 0)
3871 EMSG(_(e_prev_dir));
3872 }
3873#endif
3874 homedir = vim_strsave(var);
3875 }
3876}
3877
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003878#if defined(EXITFREE) || defined(PROTO)
3879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003880free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003881{
3882 vim_free(homedir);
3883}
Bram Moolenaar24305862012-08-15 14:05:05 +02003884
3885# ifdef FEAT_CMDL_COMPL
3886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003887free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003888{
3889 ga_clear_strings(&ga_users);
3890}
3891# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003892#endif
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003895 * Call expand_env() and store the result in an allocated string.
3896 * This is not very memory efficient, this expects the result to be freed
3897 * again soon.
3898 */
3899 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003900expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003901{
3902 return expand_env_save_opt(src, FALSE);
3903}
3904
3905/*
3906 * Idem, but when "one" is TRUE handle the string as one file name, only
3907 * expand "~" at the start.
3908 */
3909 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003910expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003911{
3912 char_u *p;
3913
3914 p = alloc(MAXPATHL);
3915 if (p != NULL)
3916 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3917 return p;
3918}
3919
3920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * Expand environment variable with path name.
3922 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003923 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 * If anything fails no expansion is done and dst equals src.
3925 */
3926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003927expand_env(
3928 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3929 char_u *dst, /* where to put the result */
3930 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003932 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933}
3934
3935 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003936expand_env_esc(
3937 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3938 char_u *dst, /* where to put the result */
3939 int dstlen, /* maximum length of the result */
3940 int esc, /* escape spaces in expanded variables */
3941 int one, /* "srcp" is one file name */
3942 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003944 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 char_u *tail;
3946 int c;
3947 char_u *var;
3948 int copy_char;
3949 int mustfree; /* var was allocated, need to free it later */
3950 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003951 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003953 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003954 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003955
3956 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 --dstlen; /* leave one char space for "\," */
3958 while (*src && dstlen > 0)
3959 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003960#ifdef FEAT_EVAL
3961 /* Skip over `=expr`. */
3962 if (src[0] == '`' && src[1] == '=')
3963 {
3964 size_t len;
3965
3966 var = src;
3967 src += 2;
3968 (void)skip_expr(&src);
3969 if (*src == '`')
3970 ++src;
3971 len = src - var;
3972 if (len > (size_t)dstlen)
3973 len = dstlen;
3974 vim_strncpy(dst, var, len);
3975 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003976 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003977 continue;
3978 }
3979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003981 if ((*src == '$'
3982#ifdef VMS
3983 && at_start
3984#endif
3985 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003986#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 || *src == '%'
3988#endif
3989 || (*src == '~' && at_start))
3990 {
3991 mustfree = FALSE;
3992
3993 /*
3994 * The variable name is copied into dst temporarily, because it may
3995 * be a string in read-only memory and a NUL needs to be appended.
3996 */
3997 if (*src != '~') /* environment var */
3998 {
3999 tail = src + 1;
4000 var = dst;
4001 c = dstlen - 1;
4002
4003#ifdef UNIX
4004 /* Unix has ${var-name} type environment vars */
4005 if (*tail == '{' && !vim_isIDc('{'))
4006 {
4007 tail++; /* ignore '{' */
4008 while (c-- > 0 && *tail && *tail != '}')
4009 *var++ = *tail++;
4010 }
4011 else
4012#endif
4013 {
4014 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004015#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 || (*src == '%' && *tail != '%')
4017#endif
4018 ))
4019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 }
4023
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004024#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025# ifdef UNIX
4026 if (src[1] == '{' && *tail != '}')
4027# else
4028 if (*src == '%' && *tail != '%')
4029# endif
4030 var = NULL;
4031 else
4032 {
4033# ifdef UNIX
4034 if (src[1] == '{')
4035# else
4036 if (*src == '%')
4037#endif
4038 ++tail;
4039#endif
4040 *var = NUL;
4041 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004042#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044#endif
4045 }
4046 /* home directory */
4047 else if ( src[1] == NUL
4048 || vim_ispathsep(src[1])
4049 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4050 {
4051 var = homedir;
4052 tail = src + 1;
4053 }
4054 else /* user directory */
4055 {
4056#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4057 /*
4058 * Copy ~user to dst[], so we can put a NUL after it.
4059 */
4060 tail = src;
4061 var = dst;
4062 c = dstlen - 1;
4063 while ( c-- > 0
4064 && *tail
4065 && vim_isfilec(*tail)
4066 && !vim_ispathsep(*tail))
4067 *var++ = *tail++;
4068 *var = NUL;
4069# ifdef UNIX
4070 /*
4071 * If the system supports getpwnam(), use it.
4072 * Otherwise, or if getpwnam() fails, the shell is used to
4073 * expand ~user. This is slower and may fail if the shell
4074 * does not support ~user (old versions of /bin/sh).
4075 */
4076# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4077 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004078 /* Note: memory allocated by getpwnam() is never freed.
4079 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004080 struct passwd *pw = (*dst == NUL)
4081 ? NULL : getpwnam((char *)dst + 1);
4082
4083 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 if (var == NULL)
4086# endif
4087 {
4088 expand_T xpc;
4089
4090 ExpandInit(&xpc);
4091 xpc.xp_context = EXPAND_FILES;
4092 var = ExpandOne(&xpc, dst, NULL,
4093 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 mustfree = TRUE;
4095 }
4096
4097# else /* !UNIX, thus VMS */
4098 /*
4099 * USER_HOME is a comma-separated list of
4100 * directories to search for the user account in.
4101 */
4102 {
4103 char_u test[MAXPATHL], paths[MAXPATHL];
4104 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004105 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 STRCPY(paths, USER_HOME);
4108 next_path = paths;
4109 while (*next_path)
4110 {
4111 for (path = next_path; *next_path && *next_path != ',';
4112 next_path++);
4113 if (*next_path)
4114 *next_path++ = NUL;
4115 STRCPY(test, path);
4116 STRCAT(test, "/");
4117 STRCAT(test, dst + 1);
4118 if (mch_stat(test, &st) == 0)
4119 {
4120 var = alloc(STRLEN(test) + 1);
4121 STRCPY(var, test);
4122 mustfree = TRUE;
4123 break;
4124 }
4125 }
4126 }
4127# endif /* UNIX */
4128#else
4129 /* cannot expand user's home directory, so don't try */
4130 var = NULL;
4131 tail = (char_u *)""; /* for gcc */
4132#endif /* UNIX || VMS */
4133 }
4134
4135#ifdef BACKSLASH_IN_FILENAME
4136 /* If 'shellslash' is set change backslashes to forward slashes.
4137 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4138 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4139 {
4140 char_u *p = vim_strsave(var);
4141
4142 if (p != NULL)
4143 {
4144 if (mustfree)
4145 vim_free(var);
4146 var = p;
4147 mustfree = TRUE;
4148 forward_slash(var);
4149 }
4150 }
4151#endif
4152
4153 /* If "var" contains white space, escape it with a backslash.
4154 * Required for ":e ~/tt" when $HOME includes a space. */
4155 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4156 {
4157 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4158
4159 if (p != NULL)
4160 {
4161 if (mustfree)
4162 vim_free(var);
4163 var = p;
4164 mustfree = TRUE;
4165 }
4166 }
4167
4168 if (var != NULL && *var != NUL
4169 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4170 {
4171 STRCPY(dst, var);
4172 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004173 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 /* if var[] ends in a path separator and tail[] starts
4175 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004176 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4178 && dst[-1] != ':'
4179#endif
4180 && vim_ispathsep(*tail))
4181 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004182 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 src = tail;
4184 copy_char = FALSE;
4185 }
4186 if (mustfree)
4187 vim_free(var);
4188 }
4189
4190 if (copy_char) /* copy at least one char */
4191 {
4192 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004193 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004194 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4195 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
4197 at_start = FALSE;
4198 if (src[0] == '\\' && src[1] != NUL)
4199 {
4200 *dst++ = *src++;
4201 --dstlen;
4202 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004203 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004205 if (dstlen > 0)
4206 {
4207 *dst++ = *src++;
4208 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004209
Bram Moolenaar1c864092017-08-06 18:15:45 +02004210 if (startstr != NULL && src - startstr_len >= srcp
4211 && STRNCMP(src - startstr_len, startstr,
4212 startstr_len) == 0)
4213 at_start = TRUE;
4214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 *dst = NUL;
4219}
4220
4221/*
4222 * Vim's version of getenv().
4223 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004224 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004225 * "mustfree" is set to TRUE when returned is allocated, it must be
4226 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 */
4228 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004229vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 char_u *p;
4232 char_u *pend;
4233 int vimruntime;
4234
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004235#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 /* use "C:/" when $HOME is not set */
4237 if (STRCMP(name, "HOME") == 0)
4238 return homedir;
4239#endif
4240
4241 p = mch_getenv(name);
4242 if (p != NULL && *p == NUL) /* empty is the same as not set */
4243 p = NULL;
4244
4245 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004246 {
4247#if defined(FEAT_MBYTE) && defined(WIN3264)
4248 if (enc_utf8)
4249 {
4250 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004251 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252
4253 /* Convert from active codepage to UTF-8. Other conversions are
4254 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004255 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004256 if (pp != NULL)
4257 {
4258 p = pp;
4259 *mustfree = TRUE;
4260 }
4261 }
4262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4267 if (!vimruntime && STRCMP(name, "VIM") != 0)
4268 return NULL;
4269
4270 /*
4271 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4272 * Don't do this when default_vimruntime_dir is non-empty.
4273 */
4274 if (vimruntime
4275#ifdef HAVE_PATHDEF
4276 && *default_vimruntime_dir == NUL
4277#endif
4278 )
4279 {
4280 p = mch_getenv((char_u *)"VIM");
4281 if (p != NULL && *p == NUL) /* empty is the same as not set */
4282 p = NULL;
4283 if (p != NULL)
4284 {
4285 p = vim_version_dir(p);
4286 if (p != NULL)
4287 *mustfree = TRUE;
4288 else
4289 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004290
4291#if defined(FEAT_MBYTE) && defined(WIN3264)
4292 if (enc_utf8)
4293 {
4294 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004295 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004296
4297 /* Convert from active codepage to UTF-8. Other conversions
4298 * are not done, because they would fail for non-ASCII
4299 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004300 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004301 if (pp != NULL)
4302 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004303 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004304 vim_free(p);
4305 p = pp;
4306 *mustfree = TRUE;
4307 }
4308 }
4309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 }
4312
4313 /*
4314 * When expanding $VIM or $VIMRUNTIME fails, try using:
4315 * - the directory name from 'helpfile' (unless it contains '$')
4316 * - the executable name from argv[0]
4317 */
4318 if (p == NULL)
4319 {
4320 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4321 p = p_hf;
4322#ifdef USE_EXE_NAME
4323 /*
4324 * Use the name of the executable, obtained from argv[0].
4325 */
4326 else
4327 p = exe_name;
4328#endif
4329 if (p != NULL)
4330 {
4331 /* remove the file name */
4332 pend = gettail(p);
4333
4334 /* remove "doc/" from 'helpfile', if present */
4335 if (p == p_hf)
4336 pend = remove_tail(p, pend, (char_u *)"doc");
4337
4338#ifdef USE_EXE_NAME
4339# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004340 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 if (p == exe_name)
4342 {
4343 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004344 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004346 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4347 if (pend1 != pend)
4348 {
4349 pnew = alloc((unsigned)(pend1 - p) + 15);
4350 if (pnew != NULL)
4351 {
4352 STRNCPY(pnew, p, (pend1 - p));
4353 STRCPY(pnew + (pend1 - p), "Resources/vim");
4354 p = pnew;
4355 pend = p + STRLEN(p);
4356 }
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359# endif
4360 /* remove "src/" from exe_name, if present */
4361 if (p == exe_name)
4362 pend = remove_tail(p, pend, (char_u *)"src");
4363#endif
4364
4365 /* for $VIM, remove "runtime/" or "vim54/", if present */
4366 if (!vimruntime)
4367 {
4368 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4369 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4370 }
4371
4372 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004373 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004376#ifdef MACOS_X
4377 if (p == exe_name || p == p_hf)
4378#endif
4379 /* check that the result is a directory name */
4380 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381
4382 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004383 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 else
4385 {
4386#ifdef USE_EXE_NAME
4387 /* may add "/vim54" or "/runtime" if it exists */
4388 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4389 {
4390 vim_free(p);
4391 p = pend;
4392 }
4393#endif
4394 *mustfree = TRUE;
4395 }
4396 }
4397 }
4398
4399#ifdef HAVE_PATHDEF
4400 /* When there is a pathdef.c file we can use default_vim_dir and
4401 * default_vimruntime_dir */
4402 if (p == NULL)
4403 {
4404 /* Only use default_vimruntime_dir when it is not empty */
4405 if (vimruntime && *default_vimruntime_dir != NUL)
4406 {
4407 p = default_vimruntime_dir;
4408 *mustfree = FALSE;
4409 }
4410 else if (*default_vim_dir != NUL)
4411 {
4412 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4413 *mustfree = TRUE;
4414 else
4415 {
4416 p = default_vim_dir;
4417 *mustfree = FALSE;
4418 }
4419 }
4420 }
4421#endif
4422
4423 /*
4424 * Set the environment variable, so that the new value can be found fast
4425 * next time, and others can also use it (e.g. Perl).
4426 */
4427 if (p != NULL)
4428 {
4429 if (vimruntime)
4430 {
4431 vim_setenv((char_u *)"VIMRUNTIME", p);
4432 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 else
4435 {
4436 vim_setenv((char_u *)"VIM", p);
4437 didset_vim = TRUE;
4438 }
4439 }
4440 return p;
4441}
4442
4443/*
4444 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4445 * Return NULL if not, return its name in allocated memory otherwise.
4446 */
4447 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004448vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 char_u *p;
4451
4452 if (vimdir == NULL || *vimdir == NUL)
4453 return NULL;
4454 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4455 if (p != NULL && mch_isdir(p))
4456 return p;
4457 vim_free(p);
4458 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4459 if (p != NULL && mch_isdir(p))
4460 return p;
4461 vim_free(p);
4462 return NULL;
4463}
4464
4465/*
4466 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4467 * the length of "name/". Otherwise return "pend".
4468 */
4469 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004470remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471{
4472 int len = (int)STRLEN(name) + 1;
4473 char_u *newend = pend - len;
4474
4475 if (newend >= p
4476 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004477 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return newend;
4479 return pend;
4480}
4481
Bram Moolenaar137374f2018-05-13 15:59:50 +02004482 void
4483vim_unsetenv(char_u *var)
4484{
4485#ifdef HAVE_UNSETENV
4486 unsetenv((char *)var);
4487#else
4488 mch_setenv((char *)var, "", 0);
4489#endif
4490}
4491
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 * Our portable version of setenv.
4495 */
4496 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004497vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
4499#ifdef HAVE_SETENV
4500 mch_setenv((char *)name, (char *)val, 1);
4501#else
4502 char_u *envbuf;
4503
4504 /*
4505 * Putenv does not copy the string, it has to remain
4506 * valid. The allocated memory will never be freed.
4507 */
4508 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4509 if (envbuf != NULL)
4510 {
4511 sprintf((char *)envbuf, "%s=%s", name, val);
4512 putenv((char *)envbuf);
4513 }
4514#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004515#ifdef FEAT_GETTEXT
4516 /*
4517 * When setting $VIMRUNTIME adjust the directory to find message
4518 * translations to $VIMRUNTIME/lang.
4519 */
4520 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4521 {
4522 char_u *buf = concat_str(val, (char_u *)"/lang");
4523
4524 if (buf != NULL)
4525 {
4526 bindtextdomain(VIMPACKAGE, (char *)buf);
4527 vim_free(buf);
4528 }
4529 }
4530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531}
4532
4533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4534/*
4535 * Function given to ExpandGeneric() to obtain an environment variable name.
4536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004538get_env_name(
4539 expand_T *xp UNUSED,
4540 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
Bram Moolenaard0573012017-10-28 21:11:06 +02004542# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004544 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
4546 return NULL;
4547# else
4548# ifndef __WIN32__
4549 /* Borland C++ 5.2 has this in a header file. */
4550 extern char **environ;
4551# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004552# define ENVNAMELEN 100
4553 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 char_u *str;
4555 int n;
4556
4557 str = (char_u *)environ[idx];
4558 if (str == NULL)
4559 return NULL;
4560
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004561 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 if (str[n] == '=' || str[n] == NUL)
4564 break;
4565 name[n] = str[n];
4566 }
4567 name[n] = NUL;
4568 return name;
4569# endif
4570}
Bram Moolenaar24305862012-08-15 14:05:05 +02004571
4572/*
4573 * Find all user names for user completion.
4574 * Done only once and then cached.
4575 */
4576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004577init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004578{
Bram Moolenaar24305862012-08-15 14:05:05 +02004579 static int lazy_init_done = FALSE;
4580
4581 if (lazy_init_done)
4582 return;
4583
4584 lazy_init_done = TRUE;
4585 ga_init2(&ga_users, sizeof(char_u *), 20);
4586
4587# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4588 {
4589 char_u* user;
4590 struct passwd* pw;
4591
4592 setpwent();
4593 while ((pw = getpwent()) != NULL)
4594 /* pw->pw_name shouldn't be NULL but just in case... */
4595 if (pw->pw_name != NULL)
4596 {
4597 if (ga_grow(&ga_users, 1) == FAIL)
4598 break;
4599 user = vim_strsave((char_u*)pw->pw_name);
4600 if (user == NULL)
4601 break;
4602 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4603 }
4604 endpwent();
4605 }
4606# endif
4607}
4608
4609/*
4610 * Function given to ExpandGeneric() to obtain an user names.
4611 */
4612 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004613get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004614{
4615 init_users();
4616 if (idx < ga_users.ga_len)
4617 return ((char_u **)ga_users.ga_data)[idx];
4618 return NULL;
4619}
4620
4621/*
4622 * Check whether name matches a user name. Return:
4623 * 0 if name does not match any user name.
4624 * 1 if name partially matches the beginning of a user name.
4625 * 2 is name fully matches a user name.
4626 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004627int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004628{
4629 int i;
4630 int n = (int)STRLEN(name);
4631 int result = 0;
4632
4633 init_users();
4634 for (i = 0; i < ga_users.ga_len; i++)
4635 {
4636 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4637 return 2; /* full match */
4638 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4639 result = 1; /* partial match */
4640 }
4641 return result;
4642}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
4644
4645/*
4646 * Replace home directory by "~" in each space or comma separated file name in
4647 * 'src'.
4648 * If anything fails (except when out of space) dst equals src.
4649 */
4650 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004651home_replace(
4652 buf_T *buf, /* when not NULL, check for help files */
4653 char_u *src, /* input file name */
4654 char_u *dst, /* where to put the result */
4655 int dstlen, /* maximum length of the result */
4656 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 spaces and commas in the file name. */
4658{
4659 size_t dirlen = 0, envlen = 0;
4660 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004661 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 char_u *p;
4663
4664 if (src == NULL)
4665 {
4666 *dst = NUL;
4667 return;
4668 }
4669
4670 /*
4671 * If the file is a help file, remove the path completely.
4672 */
4673 if (buf != NULL && buf->b_help)
4674 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004675 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 return;
4677 }
4678
4679 /*
4680 * We check both the value of the $HOME environment variable and the
4681 * "real" home directory.
4682 */
4683 if (homedir != NULL)
4684 dirlen = STRLEN(homedir);
4685
4686#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004687 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004689 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4690#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004691#ifdef WIN3264
4692 if (homedir_env == NULL)
4693 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4694#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004695 /* Empty is the same as not set. */
4696 if (homedir_env != NULL && *homedir_env == NUL)
4697 homedir_env = NULL;
4698
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004699#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004700 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004701 {
4702 int usedlen = 0;
4703 int flen;
4704 char_u *fbuf = NULL;
4705
4706 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004707 (void)modify_fname((char_u *)":p", &usedlen,
4708 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004709 flen = (int)STRLEN(homedir_env);
4710 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4711 /* Remove the trailing / that is added to a directory. */
4712 homedir_env[flen - 1] = NUL;
4713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#endif
4715
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (homedir_env != NULL)
4717 envlen = STRLEN(homedir_env);
4718
4719 if (!one)
4720 src = skipwhite(src);
4721 while (*src && dstlen > 0)
4722 {
4723 /*
4724 * Here we are at the beginning of a file name.
4725 * First, check to see if the beginning of the file name matches
4726 * $HOME or the "real" home directory. Check that there is a '/'
4727 * after the match (so that if e.g. the file is "/home/pieter/bla",
4728 * and the home directory is "/home/piet", the file does not end up
4729 * as "~er/bla" (which would seem to indicate the file "bla" in user
4730 * er's home directory)).
4731 */
4732 p = homedir;
4733 len = dirlen;
4734 for (;;)
4735 {
4736 if ( len
4737 && fnamencmp(src, p, len) == 0
4738 && (vim_ispathsep(src[len])
4739 || (!one && (src[len] == ',' || src[len] == ' '))
4740 || src[len] == NUL))
4741 {
4742 src += len;
4743 if (--dstlen > 0)
4744 *dst++ = '~';
4745
4746 /*
4747 * If it's just the home directory, add "/".
4748 */
4749 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4750 *dst++ = '/';
4751 break;
4752 }
4753 if (p == homedir_env)
4754 break;
4755 p = homedir_env;
4756 len = envlen;
4757 }
4758
4759 /* if (!one) skip to separator: space or comma */
4760 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4761 *dst++ = *src++;
4762 /* skip separator */
4763 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4764 *dst++ = *src++;
4765 }
4766 /* if (dstlen == 0) out of space, what to do??? */
4767
4768 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004769
4770 if (homedir_env != homedir_env_orig)
4771 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772}
4773
4774/*
4775 * Like home_replace, store the replaced string in allocated memory.
4776 * When something fails, NULL is returned.
4777 */
4778 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004779home_replace_save(
4780 buf_T *buf, /* when not NULL, check for help files */
4781 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 char_u *dst;
4784 unsigned len;
4785
4786 len = 3; /* space for "~/" and trailing NUL */
4787 if (src != NULL) /* just in case */
4788 len += (unsigned)STRLEN(src);
4789 dst = alloc(len);
4790 if (dst != NULL)
4791 home_replace(buf, src, dst, len, TRUE);
4792 return dst;
4793}
4794
4795/*
4796 * Compare two file names and return:
4797 * FPC_SAME if they both exist and are the same file.
4798 * FPC_SAMEX if they both don't exist and have the same file name.
4799 * FPC_DIFF if they both exist and are different files.
4800 * FPC_NOTX if they both don't exist.
4801 * FPC_DIFFX if one of them doesn't exist.
4802 * For the first name environment variables are expanded
4803 */
4804 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004805fullpathcmp(
4806 char_u *s1,
4807 char_u *s2,
4808 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
4810#ifdef UNIX
4811 char_u exp1[MAXPATHL];
4812 char_u full1[MAXPATHL];
4813 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004814 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int r1, r2;
4816
4817 expand_env(s1, exp1, MAXPATHL);
4818 r1 = mch_stat((char *)exp1, &st1);
4819 r2 = mch_stat((char *)s2, &st2);
4820 if (r1 != 0 && r2 != 0)
4821 {
4822 /* if mch_stat() doesn't work, may compare the names */
4823 if (checkname)
4824 {
4825 if (fnamecmp(exp1, s2) == 0)
4826 return FPC_SAMEX;
4827 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4828 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4829 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4830 return FPC_SAMEX;
4831 }
4832 return FPC_NOTX;
4833 }
4834 if (r1 != 0 || r2 != 0)
4835 return FPC_DIFFX;
4836 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4837 return FPC_SAME;
4838 return FPC_DIFF;
4839#else
4840 char_u *exp1; /* expanded s1 */
4841 char_u *full1; /* full path of s1 */
4842 char_u *full2; /* full path of s2 */
4843 int retval = FPC_DIFF;
4844 int r1, r2;
4845
4846 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4847 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4848 {
4849 full1 = exp1 + MAXPATHL;
4850 full2 = full1 + MAXPATHL;
4851
4852 expand_env(s1, exp1, MAXPATHL);
4853 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4854 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4855
4856 /* If vim_FullName() fails, the file probably doesn't exist. */
4857 if (r1 != OK && r2 != OK)
4858 {
4859 if (checkname && fnamecmp(exp1, s2) == 0)
4860 retval = FPC_SAMEX;
4861 else
4862 retval = FPC_NOTX;
4863 }
4864 else if (r1 != OK || r2 != OK)
4865 retval = FPC_DIFFX;
4866 else if (fnamecmp(full1, full2))
4867 retval = FPC_DIFF;
4868 else
4869 retval = FPC_SAME;
4870 vim_free(exp1);
4871 }
4872 return retval;
4873#endif
4874}
4875
4876/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004877 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004878 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004879 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 */
4881 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004882gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
4884 char_u *p1, *p2;
4885
4886 if (fname == NULL)
4887 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004888 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004890 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004892 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 return p1;
4895}
4896
Bram Moolenaar31710262010-08-13 13:36:15 +02004897#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004898static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004899
4900/*
4901 * Return the end of the directory name, on the first path
4902 * separator:
4903 * "/path/file", "/path/dir/", "/path//dir", "/file"
4904 * ^ ^ ^ ^
4905 */
4906 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004907gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004908{
4909 char_u *dir_end = fname;
4910 char_u *next_dir_end = fname;
4911 int look_for_sep = TRUE;
4912 char_u *p;
4913
4914 for (p = fname; *p != NUL; )
4915 {
4916 if (vim_ispathsep(*p))
4917 {
4918 if (look_for_sep)
4919 {
4920 next_dir_end = p;
4921 look_for_sep = FALSE;
4922 }
4923 }
4924 else
4925 {
4926 if (!look_for_sep)
4927 dir_end = next_dir_end;
4928 look_for_sep = TRUE;
4929 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004930 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004931 }
4932 return dir_end;
4933}
4934#endif
4935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004937 * Get pointer to tail of "fname", including path separators. Putting a NUL
4938 * here leaves the directory name. Takes care of "c:/" and "//".
4939 * Always returns a valid pointer.
4940 */
4941 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004942gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004943{
4944 char_u *p;
4945 char_u *t;
4946
4947 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4948 t = gettail(fname);
4949 while (t > p && after_pathsep(fname, t))
4950 --t;
4951#ifdef VMS
4952 /* path separator is part of the path */
4953 ++t;
4954#endif
4955 return t;
4956}
4957
4958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * get the next path component (just after the next path separator).
4960 */
4961 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004962getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963{
4964 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004965 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (*fname)
4967 ++fname;
4968 return fname;
4969}
4970
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971/*
4972 * Get a pointer to one character past the head of a path name.
4973 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4974 * If there is no head, path is returned.
4975 */
4976 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004977get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
4979 char_u *retval;
4980
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004981#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 /* may skip "c:" */
4983 if (isalpha(path[0]) && path[1] == ':')
4984 retval = path + 2;
4985 else
4986 retval = path;
4987#else
4988# if defined(AMIGA)
4989 /* may skip "label:" */
4990 retval = vim_strchr(path, ':');
4991 if (retval == NULL)
4992 retval = path;
4993# else /* Unix */
4994 retval = path;
4995# endif
4996#endif
4997
4998 while (vim_ispathsep(*retval))
4999 ++retval;
5000
5001 return retval;
5002}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005005 * Return TRUE if 'c' is a path separator.
5006 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
5008 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005009vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005011#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005013#else
5014# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005016# else
5017# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5019 return (c == ':' || c == '[' || c == ']' || c == '/'
5020 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005021# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005023# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026}
5027
Bram Moolenaar69c35002013-11-04 02:54:12 +01005028/*
5029 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5030 */
5031 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005033{
5034 return vim_ispathsep(c)
5035#ifdef BACKSLASH_IN_FILENAME
5036 && c != ':'
5037#endif
5038 ;
5039}
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5042/*
5043 * return TRUE if 'c' is a path list separator.
5044 */
5045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005046vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
5048#ifdef UNIX
5049 return (c == ':');
5050#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005051 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052#endif
5053}
5054#endif
5055
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005056/*
5057 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5058 * It's done in-place.
5059 */
5060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005061shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005062{
5063 char_u *tail, *s, *d;
5064 int skip = FALSE;
5065
5066 tail = gettail(str);
5067 d = str;
5068 for (s = str; ; ++s)
5069 {
5070 if (s >= tail) /* copy the whole tail */
5071 {
5072 *d++ = *s;
5073 if (*s == NUL)
5074 break;
5075 }
5076 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5077 {
5078 *d++ = *s;
5079 skip = FALSE;
5080 }
5081 else if (!skip)
5082 {
5083 *d++ = *s; /* copy next char */
5084 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5085 skip = TRUE;
5086# ifdef FEAT_MBYTE
5087 if (has_mbyte)
5088 {
5089 int l = mb_ptr2len(s);
5090
5091 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005092 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005093 }
5094# endif
5095 }
5096 }
5097}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005098
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005099/*
5100 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5101 * Also returns TRUE if there is no directory name.
5102 * "fname" must be writable!.
5103 */
5104 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005105dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005106{
5107 char_u *p;
5108 int c;
5109 int retval;
5110
5111 p = gettail_sep(fname);
5112 if (p == fname)
5113 return TRUE;
5114 c = *p;
5115 *p = NUL;
5116 retval = mch_isdir(fname);
5117 *p = c;
5118 return retval;
5119}
5120
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005122 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5123 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 */
5125 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005126vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005128#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005130#else
5131 if (p_fic)
5132 return MB_STRICMP(x, y);
5133 return STRCMP(x, y);
5134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135}
5136
5137 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005138vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005140#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005141 char_u *px = x;
5142 char_u *py = y;
5143 int cx = NUL;
5144 int cy = NUL;
5145
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005146 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005148 cx = PTR2CHAR(px);
5149 cy = PTR2CHAR(py);
5150 if (cx == NUL || cy == NUL
5151 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5152 && !(cx == '/' && cy == '\\')
5153 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005155 len -= MB_PTR2LEN(px);
5156 px += MB_PTR2LEN(px);
5157 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 if (len == 0)
5160 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005161 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005162#else
5163 if (p_fic)
5164 return MB_STRNICMP(x, y, len);
5165 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
5169/*
5170 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005171 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
5173 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005174concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 char_u *dest;
5177
5178 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5179 if (dest != NULL)
5180 {
5181 STRCPY(dest, fname1);
5182 if (sep)
5183 add_pathsep(dest);
5184 STRCAT(dest, fname2);
5185 }
5186 return dest;
5187}
5188
Bram Moolenaard6754642005-01-17 22:18:45 +00005189/*
5190 * Concatenate two strings and return the result in allocated memory.
5191 * Returns NULL when out of memory.
5192 */
5193 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005194concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005195{
5196 char_u *dest;
5197 size_t l = STRLEN(str1);
5198
5199 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5200 if (dest != NULL)
5201 {
5202 STRCPY(dest, str1);
5203 STRCPY(dest + l, str2);
5204 }
5205 return dest;
5206}
Bram Moolenaard6754642005-01-17 22:18:45 +00005207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208/*
5209 * Add a path separator to a file name, unless it already ends in a path
5210 * separator.
5211 */
5212 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005213add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005215 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 STRCAT(p, PATHSEPSTR);
5217}
5218
5219/*
5220 * FullName_save - Make an allocated copy of a full file name.
5221 * Returns NULL when out of memory.
5222 */
5223 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005224FullName_save(
5225 char_u *fname,
5226 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005227 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
5229 char_u *buf;
5230 char_u *new_fname = NULL;
5231
5232 if (fname == NULL)
5233 return NULL;
5234
5235 buf = alloc((unsigned)MAXPATHL);
5236 if (buf != NULL)
5237 {
5238 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5239 new_fname = vim_strsave(buf);
5240 else
5241 new_fname = vim_strsave(fname);
5242 vim_free(buf);
5243 }
5244 return new_fname;
5245}
5246
5247#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5248
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005249static char_u *skip_string(char_u *p);
5250static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005251static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005252static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254/*
5255 * Find the start of a comment, not knowing if we are in a comment right now.
5256 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005257 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005259 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005261{
5262 return find_start_comment(curbuf->b_ind_maxcomment);
5263}
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005266find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 pos_T *pos;
5269 char_u *line;
5270 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005271 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005273 for (;;)
5274 {
5275 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5276 if (pos == NULL)
5277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005279 /*
5280 * Check if the comment start we found is inside a string.
5281 * If it is then restrict the search to below this line and try again.
5282 */
5283 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005284 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005285 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005286 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005287 break;
5288 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5289 if (cur_maxcomment <= 0)
5290 {
5291 pos = NULL;
5292 break;
5293 }
5294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 return pos;
5296}
5297
5298/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005299 * Find the start of a comment or raw string, not knowing if we are in a
5300 * comment or raw string right now.
5301 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005302 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005303 * Return NULL when not inside a comment or raw string.
5304 * "CORS" -> Comment Or Raw String
5305 */
5306 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005307ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005308{
Bram Moolenaar089af182015-10-07 11:41:49 +02005309 static pos_T comment_pos_copy;
5310 pos_T *comment_pos;
5311 pos_T *rs_pos;
5312
5313 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5314 if (comment_pos != NULL)
5315 {
5316 /* Need to make a copy of the static pos in findmatchlimit(),
5317 * calling find_start_rawstring() may change it. */
5318 comment_pos_copy = *comment_pos;
5319 comment_pos = &comment_pos_copy;
5320 }
5321 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005322
5323 /* If comment_pos is before rs_pos the raw string is inside the comment.
5324 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005325 if (comment_pos == NULL || (rs_pos != NULL
5326 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005327 {
5328 if (is_raw != NULL && rs_pos != NULL)
5329 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005330 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005331 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005332 return comment_pos;
5333}
5334
5335/*
5336 * Find the start of a raw string, not knowing if we are in one right now.
5337 * Search starts at w_cursor.lnum and goes backwards.
5338 * Return NULL when not inside a raw string.
5339 */
5340 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005341find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005342{
5343 pos_T *pos;
5344 char_u *line;
5345 char_u *p;
5346 int cur_maxcomment = ind_maxcomment;
5347
5348 for (;;)
5349 {
5350 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5351 if (pos == NULL)
5352 break;
5353
5354 /*
5355 * Check if the raw string start we found is inside a string.
5356 * If it is then restrict the search to below this line and try again.
5357 */
5358 line = ml_get(pos->lnum);
5359 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5360 p = skip_string(p);
5361 if ((colnr_T)(p - line) <= pos->col)
5362 break;
5363 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5364 if (cur_maxcomment <= 0)
5365 {
5366 pos = NULL;
5367 break;
5368 }
5369 }
5370 return pos;
5371}
5372
5373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 * Skip to the end of a "string" and a 'c' character.
5375 * If there is no string or character, return argument unmodified.
5376 */
5377 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005378skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 int i;
5381
5382 /*
5383 * We loop, because strings may be concatenated: "date""time".
5384 */
5385 for ( ; ; ++p)
5386 {
5387 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5388 {
5389 if (!p[1]) /* ' at end of line */
5390 break;
5391 i = 2;
5392 if (p[1] == '\\') /* '\n' or '\000' */
5393 {
5394 ++i;
5395 while (vim_isdigit(p[i - 1])) /* '\000' */
5396 ++i;
5397 }
5398 if (p[i] == '\'') /* check for trailing ' */
5399 {
5400 p += i;
5401 continue;
5402 }
5403 }
5404 else if (p[0] == '"') /* start of string */
5405 {
5406 for (++p; p[0]; ++p)
5407 {
5408 if (p[0] == '\\' && p[1] != NUL)
5409 ++p;
5410 else if (p[0] == '"') /* end of string */
5411 break;
5412 }
5413 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005414 continue; /* continue for another string */
5415 }
5416 else if (p[0] == 'R' && p[1] == '"')
5417 {
5418 /* Raw string: R"[delim](...)[delim]" */
5419 char_u *delim = p + 2;
5420 char_u *paren = vim_strchr(delim, '(');
5421
5422 if (paren != NULL)
5423 {
5424 size_t delim_len = paren - delim;
5425
5426 for (p += 3; *p; ++p)
5427 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5428 && p[delim_len + 1] == '"')
5429 {
5430 p += delim_len + 1;
5431 break;
5432 }
5433 if (p[0] == '"')
5434 continue; /* continue for another string */
5435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 }
5437 break; /* no string found */
5438 }
5439 if (!*p)
5440 --p; /* backup from NUL */
5441 return p;
5442}
5443#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5444
5445#if defined(FEAT_CINDENT) || defined(PROTO)
5446
5447/*
5448 * Do C or expression indenting on the current line.
5449 */
5450 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005451do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453# ifdef FEAT_EVAL
5454 if (*curbuf->b_p_inde != NUL)
5455 fixthisline(get_expr_indent);
5456 else
5457# endif
5458 fixthisline(get_c_indent);
5459}
5460
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005461/* Find result cache for cpp_baseclass */
5462typedef struct {
5463 int found;
5464 lpos_T lpos;
5465} cpp_baseclass_cache_T;
5466
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467/*
5468 * Functions for C-indenting.
5469 * Most of this originally comes from Eric Fischer.
5470 */
5471/*
5472 * Below "XXX" means that this function may unlock the current line.
5473 */
5474
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005475static char_u *cin_skipcomment(char_u *);
5476static int cin_nocode(char_u *);
5477static pos_T *find_line_comment(void);
5478static int cin_has_js_key(char_u *text);
5479static int cin_islabel_skip(char_u **);
5480static int cin_isdefault(char_u *);
5481static char_u *after_label(char_u *l);
5482static int get_indent_nolabel(linenr_T lnum);
5483static int skip_label(linenr_T, char_u **pp);
5484static int cin_first_id_amount(void);
5485static int cin_get_equal_amount(linenr_T lnum);
5486static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005487static int cin_iscomment(char_u *);
5488static int cin_islinecomment(char_u *);
5489static int cin_isterminated(char_u *, int, int);
5490static int cin_isinit(void);
5491static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5492static int cin_isif(char_u *);
5493static int cin_iselse(char_u *);
5494static int cin_isdo(char_u *);
5495static int cin_iswhileofdo(char_u *, linenr_T);
5496static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5497static int cin_iswhileofdo_end(int terminated);
5498static int cin_isbreak(char_u *);
5499static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5500static int get_baseclass_amount(int col);
5501static int cin_ends_in(char_u *, char_u *, char_u *);
5502static int cin_starts_with(char_u *s, char *word);
5503static int cin_skip2pos(pos_T *trypos);
5504static pos_T *find_start_brace(void);
5505static pos_T *find_match_paren(int);
5506static pos_T *find_match_char(int c, int ind_maxparen);
5507static int corr_ind_maxparen(pos_T *startpos);
5508static int find_last_paren(char_u *l, int start, int end);
5509static int find_match(int lookfor, linenr_T ourscope);
5510static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
5512/*
5513 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005514 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 */
5516 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005517cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 while (*s)
5520 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005521 char_u *prev_s = s;
5522
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005524
5525 /* Perl/shell # comment comment continues until eol. Require a space
5526 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005527 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005528 {
5529 s += STRLEN(s);
5530 break;
5531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 if (*s != '/')
5533 break;
5534 ++s;
5535 if (*s == '/') /* slash-slash comment continues till eol */
5536 {
5537 s += STRLEN(s);
5538 break;
5539 }
5540 if (*s != '*')
5541 break;
5542 for (++s; *s; ++s) /* skip slash-star comment */
5543 if (s[0] == '*' && s[1] == '/')
5544 {
5545 s += 2;
5546 break;
5547 }
5548 }
5549 return s;
5550}
5551
5552/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005553 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * not considered code.
5555 */
5556 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005557cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 return *cin_skipcomment(s) == NUL;
5560}
5561
5562/*
5563 * Check previous lines for a "//" line comment, skipping over blank lines.
5564 */
5565 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005566find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568 static pos_T pos;
5569 char_u *line;
5570 char_u *p;
5571
5572 pos = curwin->w_cursor;
5573 while (--pos.lnum > 0)
5574 {
5575 line = ml_get(pos.lnum);
5576 p = skipwhite(line);
5577 if (cin_islinecomment(p))
5578 {
5579 pos.col = (int)(p - line);
5580 return &pos;
5581 }
5582 if (*p != NUL)
5583 break;
5584 }
5585 return NULL;
5586}
5587
5588/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005589 * Return TRUE if "text" starts with "key:".
5590 */
5591 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005592cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005593{
5594 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005595 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005596
5597 if (*s == '\'' || *s == '"')
5598 {
5599 /* can be 'key': or "key": */
5600 quote = *s;
5601 ++s;
5602 }
5603 if (!vim_isIDc(*s)) /* need at least one ID character */
5604 return FALSE;
5605
5606 while (vim_isIDc(*s))
5607 ++s;
5608 if (*s == quote)
5609 ++s;
5610
5611 s = cin_skipcomment(s);
5612
5613 /* "::" is not a label, it's C++ */
5614 return (*s == ':' && s[1] != ':');
5615}
5616
5617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005619 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 */
5621 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005622cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 if (!vim_isIDc(**s)) /* need at least one ID character */
5625 return FALSE;
5626
5627 while (vim_isIDc(**s))
5628 (*s)++;
5629
5630 *s = cin_skipcomment(*s);
5631
5632 /* "::" is not a label, it's C++ */
5633 return (**s == ':' && *++*s != ':');
5634}
5635
5636/*
5637 * Recognize a label: "label:".
5638 * Note: curwin->w_cursor must be where we are looking for the label.
5639 */
5640 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005641cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 char_u *s;
5644
5645 s = cin_skipcomment(ml_get_curline());
5646
5647 /*
5648 * Exclude "default" from labels, since it should be indented
5649 * like a switch label. Same for C++ scope declarations.
5650 */
5651 if (cin_isdefault(s))
5652 return FALSE;
5653 if (cin_isscopedecl(s))
5654 return FALSE;
5655
5656 if (cin_islabel_skip(&s))
5657 {
5658 /*
5659 * Only accept a label if the previous line is terminated or is a case
5660 * label.
5661 */
5662 pos_T cursor_save;
5663 pos_T *trypos;
5664 char_u *line;
5665
5666 cursor_save = curwin->w_cursor;
5667 while (curwin->w_cursor.lnum > 1)
5668 {
5669 --curwin->w_cursor.lnum;
5670
5671 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005672 * If we're in a comment or raw string now, skip to the start of
5673 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 */
5675 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005676 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 curwin->w_cursor = *trypos;
5678
5679 line = ml_get_curline();
5680 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5681 continue;
5682 if (*(line = cin_skipcomment(line)) == NUL)
5683 continue;
5684
5685 curwin->w_cursor = cursor_save;
5686 if (cin_isterminated(line, TRUE, FALSE)
5687 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005688 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 || (cin_islabel_skip(&line) && cin_nocode(line)))
5690 return TRUE;
5691 return FALSE;
5692 }
5693 curwin->w_cursor = cursor_save;
5694 return TRUE; /* label at start of file??? */
5695 }
5696 return FALSE;
5697}
5698
5699/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005700 * Recognize structure initialization and enumerations:
5701 * "[typedef] [static|public|protected|private] enum"
5702 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
5704 static int
5705cin_isinit(void)
5706{
5707 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005708 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
5710 s = cin_skipcomment(ml_get_curline());
5711
Bram Moolenaar75342212013-03-07 13:13:52 +01005712 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 s = cin_skipcomment(s + 7);
5714
Bram Moolenaar75342212013-03-07 13:13:52 +01005715 for (;;)
5716 {
5717 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005718
Bram Moolenaar75342212013-03-07 13:13:52 +01005719 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5720 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005721 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005722 if (cin_starts_with(s, skip[i]))
5723 {
5724 s = cin_skipcomment(s + l);
5725 l = 0;
5726 break;
5727 }
5728 }
5729 if (l != 0)
5730 break;
5731 }
5732
5733 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 return TRUE;
5735
5736 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5737 return TRUE;
5738
5739 return FALSE;
5740}
5741
5742/*
5743 * Recognize a switch label: "case .*:" or "default:".
5744 */
5745 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005746cin_iscase(
5747 char_u *s,
5748 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005751 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 for (s += 4; *s; ++s)
5754 {
5755 s = cin_skipcomment(s);
5756 if (*s == ':')
5757 {
5758 if (s[1] == ':') /* skip over "::" for C++ */
5759 ++s;
5760 else
5761 return TRUE;
5762 }
5763 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005764 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5766 return FALSE; /* stop at comment */
5767 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005768 {
5769 /* JS etc. */
5770 if (strict)
5771 return FALSE; /* stop at string */
5772 else
5773 return TRUE;
5774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776 return FALSE;
5777 }
5778
5779 if (cin_isdefault(s))
5780 return TRUE;
5781 return FALSE;
5782}
5783
5784/*
5785 * Recognize a "default" switch label.
5786 */
5787 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005788cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 return (STRNCMP(s, "default", 7) == 0
5791 && *(s = cin_skipcomment(s + 7)) == ':'
5792 && s[1] != ':');
5793}
5794
5795/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005796 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 */
5798 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005799cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
5801 int i;
5802
5803 s = cin_skipcomment(s);
5804 if (STRNCMP(s, "public", 6) == 0)
5805 i = 6;
5806 else if (STRNCMP(s, "protected", 9) == 0)
5807 i = 9;
5808 else if (STRNCMP(s, "private", 7) == 0)
5809 i = 7;
5810 else
5811 return FALSE;
5812 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5813}
5814
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005815/* Maximum number of lines to search back for a "namespace" line. */
5816#define FIND_NAMESPACE_LIM 20
5817
5818/*
5819 * Recognize a "namespace" scope declaration.
5820 */
5821 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005822cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005823{
5824 char_u *p;
5825 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005826 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005827
5828 s = cin_skipcomment(s);
5829 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5830 {
5831 p = cin_skipcomment(skipwhite(s + 9));
5832 while (*p != NUL)
5833 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005834 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005835 {
5836 has_name = TRUE; /* found end of a name */
5837 p = cin_skipcomment(skipwhite(p));
5838 }
5839 else if (*p == '{')
5840 {
5841 break;
5842 }
5843 else if (vim_iswordc(*p))
5844 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005845 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005846 if (has_name)
5847 return FALSE; /* word character after skipping past name */
5848 ++p;
5849 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005850 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5851 {
5852 if (!has_name_start || has_name)
5853 return FALSE;
5854 /* C++ 17 nested namespace */
5855 p += 3;
5856 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005857 else
5858 {
5859 return FALSE;
5860 }
5861 }
5862 return TRUE;
5863 }
5864 return FALSE;
5865}
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005868 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5869 */
5870 static int
5871cin_is_cpp_extern_c(char_u *s)
5872{
5873 char_u *p;
5874 int has_string_literal = FALSE;
5875
5876 s = cin_skipcomment(s);
5877 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5878 {
5879 p = cin_skipcomment(skipwhite(s + 6));
5880 while (*p != NUL)
5881 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005882 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005883 {
5884 p = cin_skipcomment(skipwhite(p));
5885 }
5886 else if (*p == '{')
5887 {
5888 break;
5889 }
5890 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5891 {
5892 if (has_string_literal)
5893 return FALSE;
5894 has_string_literal = TRUE;
5895 p += 3;
5896 }
5897 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5898 && p[4] == '"')
5899 {
5900 if (has_string_literal)
5901 return FALSE;
5902 has_string_literal = TRUE;
5903 p += 5;
5904 }
5905 else
5906 {
5907 return FALSE;
5908 }
5909 }
5910 return has_string_literal ? TRUE : FALSE;
5911 }
5912 return FALSE;
5913}
5914
5915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 * Return a pointer to the first non-empty non-comment character after a ':'.
5917 * Return NULL if not found.
5918 * case 234: a = b;
5919 * ^
5920 */
5921 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005922after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 for ( ; *l; ++l)
5925 {
5926 if (*l == ':')
5927 {
5928 if (l[1] == ':') /* skip over "::" for C++ */
5929 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005930 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 break;
5932 }
5933 else if (*l == '\'' && l[1] && l[2] == '\'')
5934 l += 2; /* skip over 'x' */
5935 }
5936 if (*l == NUL)
5937 return NULL;
5938 l = cin_skipcomment(l + 1);
5939 if (*l == NUL)
5940 return NULL;
5941 return l;
5942}
5943
5944/*
5945 * Get indent of line "lnum", skipping a label.
5946 * Return 0 if there is nothing after the label.
5947 */
5948 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005949get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 char_u *l;
5952 pos_T fp;
5953 colnr_T col;
5954 char_u *p;
5955
5956 l = ml_get(lnum);
5957 p = after_label(l);
5958 if (p == NULL)
5959 return 0;
5960
5961 fp.col = (colnr_T)(p - l);
5962 fp.lnum = lnum;
5963 getvcol(curwin, &fp, &col, NULL, NULL);
5964 return (int)col;
5965}
5966
5967/*
5968 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005969 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 * label: if (asdf && asdfasdf)
5971 * ^
5972 */
5973 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005974skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 char_u *l;
5977 int amount;
5978 pos_T cursor_save;
5979
5980 cursor_save = curwin->w_cursor;
5981 curwin->w_cursor.lnum = lnum;
5982 l = ml_get_curline();
5983 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005984 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {
5986 amount = get_indent_nolabel(lnum);
5987 l = after_label(ml_get_curline());
5988 if (l == NULL) /* just in case */
5989 l = ml_get_curline();
5990 }
5991 else
5992 {
5993 amount = get_indent();
5994 l = ml_get_curline();
5995 }
5996 *pp = l;
5997
5998 curwin->w_cursor = cursor_save;
5999 return amount;
6000}
6001
6002/*
6003 * Return the indent of the first variable name after a type in a declaration.
6004 * int a, indent of "a"
6005 * static struct foo b, indent of "b"
6006 * enum bla c, indent of "c"
6007 * Returns zero when it doesn't look like a declaration.
6008 */
6009 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006010cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
6012 char_u *line, *p, *s;
6013 int len;
6014 pos_T fp;
6015 colnr_T col;
6016
6017 line = ml_get_curline();
6018 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006019 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6021 {
6022 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006023 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 }
6025 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6026 p = skipwhite(p + 6);
6027 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6028 p = skipwhite(p + 4);
6029 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6030 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6031 {
6032 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006033 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6034 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6035 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6036 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 p = s;
6038 }
6039 for (len = 0; vim_isIDc(p[len]); ++len)
6040 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006041 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 return 0;
6043
6044 p = skipwhite(p + len);
6045 fp.lnum = curwin->w_cursor.lnum;
6046 fp.col = (colnr_T)(p - line);
6047 getvcol(curwin, &fp, &col, NULL, NULL);
6048 return (int)col;
6049}
6050
6051/*
6052 * Return the indent of the first non-blank after an equal sign.
6053 * char *foo = "here";
6054 * Return zero if no (useful) equal sign found.
6055 * Return -1 if the line above "lnum" ends in a backslash.
6056 * foo = "asdf\
6057 * asdf\
6058 * here";
6059 */
6060 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006061cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
6063 char_u *line;
6064 char_u *s;
6065 colnr_T col;
6066 pos_T fp;
6067
6068 if (lnum > 1)
6069 {
6070 line = ml_get(lnum - 1);
6071 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6072 return -1;
6073 }
6074
6075 line = s = ml_get(lnum);
6076 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6077 {
6078 if (cin_iscomment(s)) /* ignore comments */
6079 s = cin_skipcomment(s);
6080 else
6081 ++s;
6082 }
6083 if (*s != '=')
6084 return 0;
6085
6086 s = skipwhite(s + 1);
6087 if (cin_nocode(s))
6088 return 0;
6089
6090 if (*s == '"') /* nice alignment for continued strings */
6091 ++s;
6092
6093 fp.lnum = lnum;
6094 fp.col = (colnr_T)(s - line);
6095 getvcol(curwin, &fp, &col, NULL, NULL);
6096 return (int)col;
6097}
6098
6099/*
6100 * Recognize a preprocessor statement: Any line that starts with '#'.
6101 */
6102 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006103cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006105 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 return TRUE;
6107 return FALSE;
6108}
6109
6110/*
6111 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6112 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6113 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006114 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 */
6116 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006117cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 char_u *line = *pp;
6120 linenr_T lnum = *lnump;
6121 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006122 int candidate_amount = *amount;
6123
6124 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6125 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006127 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 if (cin_ispreproc(line))
6130 {
6131 retval = TRUE;
6132 *lnump = lnum;
6133 break;
6134 }
6135 if (lnum == 1)
6136 break;
6137 line = ml_get(--lnum);
6138 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6139 break;
6140 }
6141
6142 if (lnum != *lnump)
6143 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006144 if (retval)
6145 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 return retval;
6147}
6148
6149/*
6150 * Recognize the start of a C or C++ comment.
6151 */
6152 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006153cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6156}
6157
6158/*
6159 * Recognize the start of a "//" comment.
6160 */
6161 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006162cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 return (p[0] == '/' && p[1] == '/');
6165}
6166
6167/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006168 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6169 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006171 * If a line begins with an "else", only consider it terminated if no unmatched
6172 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 * Return the character terminating the line (ending char's have precedence if
6174 * both apply in order to determine initializations).
6175 */
6176 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006177cin_isterminated(
6178 char_u *s,
6179 int incl_open, /* include '{' at the end as terminator */
6180 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006182 char_u found_start = 0;
6183 unsigned n_open = 0;
6184 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 s = cin_skipcomment(s);
6187
6188 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6189 found_start = *s;
6190
Bram Moolenaar496f9512011-05-19 16:35:09 +02006191 if (!found_start)
6192 is_else = cin_iselse(s);
6193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 while (*s)
6195 {
6196 /* skip over comments, "" strings and 'c'haracters */
6197 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006198 if (*s == '}' && n_open > 0)
6199 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006200 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006201 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 && cin_nocode(s + 1))
6203 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006204 else if (*s == '{')
6205 {
6206 if (incl_open && cin_nocode(s + 1))
6207 return *s;
6208 else
6209 ++n_open;
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212 if (*s)
6213 s++;
6214 }
6215 return found_start;
6216}
6217
6218/*
6219 * Recognize the basic picture of a function declaration -- it needs to
6220 * have an open paren somewhere and a close paren at the end of the line and
6221 * no semicolons anywhere.
6222 * When a line ends in a comma we continue looking in the next line.
6223 * "sp" points to a string with the line. When looking at other lines it must
6224 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006225 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006226 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 */
6228 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006229cin_isfuncdecl(
6230 char_u **sp,
6231 linenr_T first_lnum,
6232 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
6234 char_u *s;
6235 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006236 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006238 pos_T *trypos;
6239 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241 if (sp == NULL)
6242 s = ml_get(lnum);
6243 else
6244 s = *sp;
6245
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006246 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006247 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006248 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006249 {
6250 lnum = trypos->lnum;
6251 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006252 {
6253 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006254 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006255 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006256
6257 s = ml_get(lnum);
6258 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006259 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006260
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006261 /* Ignore line starting with #. */
6262 if (cin_ispreproc(s))
6263 return FALSE;
6264
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6266 {
6267 if (cin_iscomment(s)) /* ignore comments */
6268 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006269 else if (*s == ':')
6270 {
6271 if (*(s + 1) == ':')
6272 s += 2;
6273 else
6274 /* To avoid a mistake in the following situation:
6275 * A::A(int a, int b)
6276 * : a(0) // <--not a function decl
6277 * , b(0)
6278 * {...
6279 */
6280 return FALSE;
6281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 else
6283 ++s;
6284 }
6285 if (*s != '(')
6286 return FALSE; /* ';', ' or " before any () or no '(' */
6287
6288 while (*s && *s != ';' && *s != '\'' && *s != '"')
6289 {
6290 if (*s == ')' && cin_nocode(s + 1))
6291 {
6292 /* ')' at the end: may have found a match
6293 * Check for he previous line not to end in a backslash:
6294 * #if defined(x) && \
6295 * defined(y)
6296 */
6297 lnum = first_lnum - 1;
6298 s = ml_get(lnum);
6299 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6300 retval = TRUE;
6301 goto done;
6302 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006303 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006305 int comma = (*s == ',');
6306
6307 /* ',' at the end: continue looking in the next line.
6308 * At the end: check for ',' in the next line, for this style:
6309 * func(arg1
6310 * , arg2) */
6311 for (;;)
6312 {
6313 if (lnum >= curbuf->b_ml.ml_line_count)
6314 break;
6315 s = ml_get(++lnum);
6316 if (!cin_ispreproc(s))
6317 break;
6318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (lnum >= curbuf->b_ml.ml_line_count)
6320 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006321 /* Require a comma at end of the line or a comma or ')' at the
6322 * start of next line. */
6323 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006324 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006325 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006326 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
6328 else if (cin_iscomment(s)) /* ignore comments */
6329 s = cin_skipcomment(s);
6330 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006333 just_started = FALSE;
6334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336
6337done:
6338 if (lnum != first_lnum && sp != NULL)
6339 *sp = ml_get(first_lnum);
6340
6341 return retval;
6342}
6343
6344 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006345cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006347 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006351cin_iselse(
6352 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 if (*p == '}') /* accept "} else" */
6355 p = cin_skipcomment(p + 1);
6356 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6357}
6358
6359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006360cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6363}
6364
6365/*
6366 * Check if this is a "while" that should have a matching "do".
6367 * We only accept a "while (condition) ;", with only white space between the
6368 * ')' and ';'. The condition may be spread over several lines.
6369 */
6370 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 pos_T cursor_save;
6374 pos_T *trypos;
6375 int retval = FALSE;
6376
6377 p = cin_skipcomment(p);
6378 if (*p == '}') /* accept "} while (cond);" */
6379 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006380 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 {
6382 cursor_save = curwin->w_cursor;
6383 curwin->w_cursor.lnum = lnum;
6384 curwin->w_cursor.col = 0;
6385 p = ml_get_curline();
6386 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6387 {
6388 ++p;
6389 ++curwin->w_cursor.col;
6390 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006391 if ((trypos = findmatchlimit(NULL, 0, 0,
6392 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6394 retval = TRUE;
6395 curwin->w_cursor = cursor_save;
6396 }
6397 return retval;
6398}
6399
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006400/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006401 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006402 * Return 0 if there is none.
6403 * Otherwise return !0 and update "*poffset" to point to the place where the
6404 * string was found.
6405 */
6406 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006407cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006408{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006409 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006410
6411 if (offset-- < 2)
6412 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006413 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006414 --offset;
6415
6416 offset -= 1;
6417 if (!STRNCMP(line + offset, "if", 2))
6418 goto probablyFound;
6419
6420 if (offset >= 1)
6421 {
6422 offset -= 1;
6423 if (!STRNCMP(line + offset, "for", 3))
6424 goto probablyFound;
6425
6426 if (offset >= 2)
6427 {
6428 offset -= 2;
6429 if (!STRNCMP(line + offset, "while", 5))
6430 goto probablyFound;
6431 }
6432 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006433 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006434
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006435probablyFound:
6436 if (!offset || !vim_isIDc(line[offset - 1]))
6437 {
6438 *poffset = offset;
6439 return 1;
6440 }
6441 return 0;
6442}
6443
6444/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006445 * Return TRUE if we are at the end of a do-while.
6446 * do
6447 * nothing;
6448 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006449 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006450 * Adjust the cursor to the line with "while".
6451 */
6452 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006453cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006454{
6455 char_u *line;
6456 char_u *p;
6457 char_u *s;
6458 pos_T *trypos;
6459 int i;
6460
6461 if (terminated != ';') /* there must be a ';' at the end */
6462 return FALSE;
6463
6464 p = line = ml_get_curline();
6465 while (*p != NUL)
6466 {
6467 p = cin_skipcomment(p);
6468 if (*p == ')')
6469 {
6470 s = skipwhite(p + 1);
6471 if (*s == ';' && cin_nocode(s + 1))
6472 {
6473 /* Found ");" at end of the line, now check there is "while"
6474 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006475 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006476 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006477 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006478 if (trypos != NULL)
6479 {
6480 s = cin_skipcomment(ml_get(trypos->lnum));
6481 if (*s == '}') /* accept "} while (cond);" */
6482 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006483 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006484 {
6485 curwin->w_cursor.lnum = trypos->lnum;
6486 return TRUE;
6487 }
6488 }
6489
6490 /* Searching may have made "line" invalid, get it again. */
6491 line = ml_get_curline();
6492 p = line + i;
6493 }
6494 }
6495 if (*p != NUL)
6496 ++p;
6497 }
6498 return FALSE;
6499}
6500
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006502cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6505}
6506
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006507/*
6508 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 * constructor-initialization. eg:
6510 *
6511 * class MyClass :
6512 * baseClass <-- here
6513 * class MyClass : public baseClass,
6514 * anotherBaseClass <-- here (should probably lineup ??)
6515 * MyClass::MyClass(...) :
6516 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006517 *
6518 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 */
6520 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006521cin_is_cpp_baseclass(
6522 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006524 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 char_u *s;
6526 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006527 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006528 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006530 if (pos->lnum <= lnum)
6531 return cached->found; /* Use the cached result */
6532
6533 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006535 s = skipwhite(line);
6536 if (*s == '#') /* skip #define FOO x ? (x) : x */
6537 return FALSE;
6538 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (*s == NUL)
6540 return FALSE;
6541
6542 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6543
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006544 /* Search for a line starting with '#', empty, ending in ';' or containing
6545 * '{' or '}' and start below it. This handles the following situations:
6546 * a = cond ?
6547 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006548 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006549 * func::foo()
6550 * : something
6551 * {}
6552 * Foo::Foo (int one, int two)
6553 * : something(4),
6554 * somethingelse(3)
6555 * {}
6556 */
6557 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006559 line = ml_get(lnum - 1);
6560 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006561 if (*s == '#' || *s == NUL)
6562 break;
6563 while (*s != NUL)
6564 {
6565 s = cin_skipcomment(s);
6566 if (*s == '{' || *s == '}'
6567 || (*s == ';' && cin_nocode(s + 1)))
6568 break;
6569 if (*s != NUL)
6570 ++s;
6571 }
6572 if (*s != NUL)
6573 break;
6574 --lnum;
6575 }
6576
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006577 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006578 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006579 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006580 for (;;)
6581 {
6582 if (*s == NUL)
6583 {
6584 if (lnum == curwin->w_cursor.lnum)
6585 break;
6586 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006587 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006588 s = line;
6589 }
6590 if (s == line)
6591 {
6592 /* don't recognize "case (foo):" as a baseclass */
6593 if (cin_iscase(s, FALSE))
6594 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006595 s = cin_skipcomment(line);
6596 if (*s == NUL)
6597 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006598 }
6599
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006600 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006601 s = skip_string(s) + 1;
6602 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {
6604 if (s[1] == ':')
6605 {
6606 /* skip double colon. It can't be a constructor
6607 * initialization any more */
6608 lookfor_ctor_init = FALSE;
6609 s = cin_skipcomment(s + 2);
6610 }
6611 else if (lookfor_ctor_init || class_or_struct)
6612 {
6613 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006614 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 cpp_base_class = TRUE;
6616 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006617 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 s = cin_skipcomment(s + 1);
6619 }
6620 else
6621 s = cin_skipcomment(s + 1);
6622 }
6623 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6624 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6625 {
6626 class_or_struct = TRUE;
6627 lookfor_ctor_init = FALSE;
6628
6629 if (*s == 'c')
6630 s = cin_skipcomment(s + 5);
6631 else
6632 s = cin_skipcomment(s + 6);
6633 }
6634 else
6635 {
6636 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6637 {
6638 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6639 }
6640 else if (s[0] == ')')
6641 {
6642 /* Constructor-initialization is assumed if we come across
6643 * something like "):" */
6644 class_or_struct = FALSE;
6645 lookfor_ctor_init = TRUE;
6646 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006647 else if (s[0] == '?')
6648 {
6649 /* Avoid seeing '() :' after '?' as constructor init. */
6650 return FALSE;
6651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 else if (!vim_isIDc(s[0]))
6653 {
6654 /* if it is not an identifier, we are wrong */
6655 class_or_struct = FALSE;
6656 lookfor_ctor_init = FALSE;
6657 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006658 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
6660 /* it can't be a constructor-initialization any more */
6661 lookfor_ctor_init = FALSE;
6662
6663 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006664 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006665 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 }
6667
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006668 /* When the line ends in a comma don't align with it. */
6669 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006670 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006671
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 s = cin_skipcomment(s + 1);
6673 }
6674 }
6675
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006676 cached->found = cpp_base_class;
6677 if (cpp_base_class)
6678 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 return cpp_base_class;
6680}
6681
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006682 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006683get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006684{
6685 int amount;
6686 colnr_T vcol;
6687 pos_T *trypos;
6688
6689 if (col == 0)
6690 {
6691 amount = get_indent();
6692 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006693 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006694 amount = get_indent_lnum(trypos->lnum); /* XXX */
6695 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006696 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006697 }
6698 else
6699 {
6700 curwin->w_cursor.col = col;
6701 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6702 amount = (int)vcol;
6703 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006704 if (amount < curbuf->b_ind_cpp_baseclass)
6705 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006706 return amount;
6707}
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709/*
6710 * Return TRUE if string "s" ends with the string "find", possibly followed by
6711 * white space and comments. Skip strings and comments.
6712 * Ignore "ignore" after "find" if it's not NULL.
6713 */
6714 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 char_u *p = s;
6718 char_u *r;
6719 int len = (int)STRLEN(find);
6720
6721 while (*p != NUL)
6722 {
6723 p = cin_skipcomment(p);
6724 if (STRNCMP(p, find, len) == 0)
6725 {
6726 r = skipwhite(p + len);
6727 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6728 r = skipwhite(r + STRLEN(ignore));
6729 if (cin_nocode(r))
6730 return TRUE;
6731 }
6732 if (*p != NUL)
6733 ++p;
6734 }
6735 return FALSE;
6736}
6737
6738/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006739 * Return TRUE when "s" starts with "word" and then a non-ID character.
6740 */
6741 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006743{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006744 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006745
6746 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6747}
6748
6749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 * Skip strings, chars and comments until at or past "trypos".
6751 * Return the column found.
6752 */
6753 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006754cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 char_u *line;
6757 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006758 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760 p = line = ml_get(trypos->lnum);
6761 while (*p && (colnr_T)(p - line) < trypos->col)
6762 {
6763 if (cin_iscomment(p))
6764 p = cin_skipcomment(p);
6765 else
6766 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006767 new_p = skip_string(p);
6768 if (new_p == p)
6769 ++p;
6770 else
6771 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 }
6773 }
6774 return (int)(p - line);
6775}
6776
6777/*
6778 * Find the '{' at the start of the block we are in.
6779 * Return NULL if no match found.
6780 * Ignore a '{' that is in a comment, makes indenting the next three lines
6781 * work. */
6782/* foo() */
6783/* { */
6784/* } */
6785
6786 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006787find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 pos_T cursor_save;
6790 pos_T *trypos;
6791 pos_T *pos;
6792 static pos_T pos_copy;
6793
6794 cursor_save = curwin->w_cursor;
6795 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6796 {
6797 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6798 trypos = &pos_copy;
6799 curwin->w_cursor = *trypos;
6800 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006801 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006803 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 break;
6805 if (pos != NULL)
6806 curwin->w_cursor.lnum = pos->lnum;
6807 }
6808 curwin->w_cursor = cursor_save;
6809 return trypos;
6810}
6811
6812/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006813 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006814 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 */
6816 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006817find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006819 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006820}
6821
6822 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006823find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006824{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 pos_T cursor_save;
6826 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006827 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006828 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
6830 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006831 ind_maxp_wk = ind_maxparen;
6832retry:
6833 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
6835 /* check if the ( is in a // comment */
6836 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006837 {
6838 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6839 if (ind_maxp_wk > 0)
6840 {
6841 curwin->w_cursor = *trypos;
6842 curwin->w_cursor.col = 0; /* XXX */
6843 goto retry;
6844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 else
6848 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006849 pos_T *trypos_wk;
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6852 trypos = &pos_copy;
6853 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006854 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006855 {
6856 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6857 - trypos_wk->lnum);
6858 if (ind_maxp_wk > 0)
6859 {
6860 curwin->w_cursor = *trypos_wk;
6861 goto retry;
6862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866 }
6867 curwin->w_cursor = cursor_save;
6868 return trypos;
6869}
6870
6871/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006872 * Find the matching '(', ignoring it if it is in a comment or before an
6873 * unmatched {.
6874 * Return NULL if no match found.
6875 */
6876 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006877find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006878{
6879 pos_T *trypos = find_match_paren(ind_maxparen);
6880
6881 if (trypos != NULL)
6882 {
6883 pos_T *tryposBrace = find_start_brace();
6884
6885 /* If both an unmatched '(' and '{' is found. Ignore the '('
6886 * position if the '{' is further down. */
6887 if (tryposBrace != NULL
6888 && (trypos->lnum != tryposBrace->lnum
6889 ? trypos->lnum < tryposBrace->lnum
6890 : trypos->col < tryposBrace->col))
6891 trypos = NULL;
6892 }
6893 return trypos;
6894}
6895
6896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 * Return ind_maxparen corrected for the difference in line number between the
6898 * cursor position and "startpos". This makes sure that searching for a
6899 * matching paren above the cursor line doesn't find a match because of
6900 * looking a few lines further.
6901 */
6902 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6906
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006907 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6908 return curbuf->b_ind_maxparen - (int)n;
6909 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910}
6911
6912/*
6913 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006914 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 */
6916 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006917find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
6919 int i;
6920 int retval = FALSE;
6921 int open_count = 0;
6922
6923 curwin->w_cursor.col = 0; /* default is start of line */
6924
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006925 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
6927 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6928 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6929 if (l[i] == start)
6930 ++open_count;
6931 else if (l[i] == end)
6932 {
6933 if (open_count > 0)
6934 --open_count;
6935 else
6936 {
6937 curwin->w_cursor.col = i;
6938 retval = TRUE;
6939 }
6940 }
6941 }
6942 return retval;
6943}
6944
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006945/*
6946 * Parse 'cinoptions' and set the values in "curbuf".
6947 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6948 */
6949 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006950parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006951{
6952 char_u *p;
6953 char_u *l;
6954 char_u *digits;
6955 int n;
6956 int divider;
6957 int fraction = 0;
6958 int sw = (int)get_sw_value(buf);
6959
6960 /*
6961 * Set the default values.
6962 */
6963 /* Spaces from a block's opening brace the prevailing indent for that
6964 * block should be. */
6965 buf->b_ind_level = sw;
6966
6967 /* Spaces from the edge of the line an open brace that's at the end of a
6968 * line is imagined to be. */
6969 buf->b_ind_open_imag = 0;
6970
6971 /* Spaces from the prevailing indent for a line that is not preceded by
6972 * an opening brace. */
6973 buf->b_ind_no_brace = 0;
6974
6975 /* Column where the first { of a function should be located }. */
6976 buf->b_ind_first_open = 0;
6977
6978 /* Spaces from the prevailing indent a leftmost open brace should be
6979 * located. */
6980 buf->b_ind_open_extra = 0;
6981
6982 /* Spaces from the matching open brace (real location for one at the left
6983 * edge; imaginary location from one that ends a line) the matching close
6984 * brace should be located. */
6985 buf->b_ind_close_extra = 0;
6986
6987 /* Spaces from the edge of the line an open brace sitting in the leftmost
6988 * column is imagined to be. */
6989 buf->b_ind_open_left_imag = 0;
6990
6991 /* Spaces jump labels should be shifted to the left if N is non-negative,
6992 * otherwise the jump label will be put to column 1. */
6993 buf->b_ind_jump_label = -1;
6994
6995 /* Spaces from the switch() indent a "case xx" label should be located. */
6996 buf->b_ind_case = sw;
6997
6998 /* Spaces from the "case xx:" code after a switch() should be located. */
6999 buf->b_ind_case_code = sw;
7000
7001 /* Lineup break at end of case in switch() with case label. */
7002 buf->b_ind_case_break = 0;
7003
7004 /* Spaces from the class declaration indent a scope declaration label
7005 * should be located. */
7006 buf->b_ind_scopedecl = sw;
7007
7008 /* Spaces from the scope declaration label code should be located. */
7009 buf->b_ind_scopedecl_code = sw;
7010
7011 /* Amount K&R-style parameters should be indented. */
7012 buf->b_ind_param = sw;
7013
7014 /* Amount a function type spec should be indented. */
7015 buf->b_ind_func_type = sw;
7016
7017 /* Amount a cpp base class declaration or constructor initialization
7018 * should be indented. */
7019 buf->b_ind_cpp_baseclass = sw;
7020
7021 /* additional spaces beyond the prevailing indent a continuation line
7022 * should be located. */
7023 buf->b_ind_continuation = sw;
7024
7025 /* Spaces from the indent of the line with an unclosed parentheses. */
7026 buf->b_ind_unclosed = sw * 2;
7027
7028 /* Spaces from the indent of the line with an unclosed parentheses, which
7029 * itself is also unclosed. */
7030 buf->b_ind_unclosed2 = sw;
7031
7032 /* Suppress ignoring spaces from the indent of a line starting with an
7033 * unclosed parentheses. */
7034 buf->b_ind_unclosed_noignore = 0;
7035
7036 /* If the opening paren is the last nonwhite character on the line, and
7037 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7038 * context (for very long lines). */
7039 buf->b_ind_unclosed_wrapped = 0;
7040
7041 /* Suppress ignoring white space when lining up with the character after
7042 * an unclosed parentheses. */
7043 buf->b_ind_unclosed_whiteok = 0;
7044
7045 /* Indent a closing parentheses under the line start of the matching
7046 * opening parentheses. */
7047 buf->b_ind_matching_paren = 0;
7048
7049 /* Indent a closing parentheses under the previous line. */
7050 buf->b_ind_paren_prev = 0;
7051
7052 /* Extra indent for comments. */
7053 buf->b_ind_comment = 0;
7054
7055 /* Spaces from the comment opener when there is nothing after it. */
7056 buf->b_ind_in_comment = 3;
7057
7058 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7059 * after the comment opener. */
7060 buf->b_ind_in_comment2 = 0;
7061
7062 /* Max lines to search for an open paren. */
7063 buf->b_ind_maxparen = 20;
7064
7065 /* Max lines to search for an open comment. */
7066 buf->b_ind_maxcomment = 70;
7067
7068 /* Handle braces for java code. */
7069 buf->b_ind_java = 0;
7070
7071 /* Not to confuse JS object properties with labels. */
7072 buf->b_ind_js = 0;
7073
7074 /* Handle blocked cases correctly. */
7075 buf->b_ind_keep_case_label = 0;
7076
7077 /* Handle C++ namespace. */
7078 buf->b_ind_cpp_namespace = 0;
7079
7080 /* Handle continuation lines containing conditions of if(), for() and
7081 * while(). */
7082 buf->b_ind_if_for_while = 0;
7083
Bram Moolenaar6b643942017-03-05 19:44:06 +01007084 /* indentation for # comments */
7085 buf->b_ind_hash_comment = 0;
7086
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007087 /* Handle C++ extern "C" or "C++" */
7088 buf->b_ind_cpp_extern_c = 0;
7089
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007090 for (p = buf->b_p_cino; *p; )
7091 {
7092 l = p++;
7093 if (*p == '-')
7094 ++p;
7095 digits = p; /* remember where the digits start */
7096 n = getdigits(&p);
7097 divider = 0;
7098 if (*p == '.') /* ".5s" means a fraction */
7099 {
7100 fraction = atol((char *)++p);
7101 while (VIM_ISDIGIT(*p))
7102 {
7103 ++p;
7104 if (divider)
7105 divider *= 10;
7106 else
7107 divider = 10;
7108 }
7109 }
7110 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7111 {
7112 if (p == digits)
7113 n = sw; /* just "s" is one 'shiftwidth' */
7114 else
7115 {
7116 n *= sw;
7117 if (divider)
7118 n += (sw * fraction + divider / 2) / divider;
7119 }
7120 ++p;
7121 }
7122 if (l[1] == '-')
7123 n = -n;
7124
7125 /* When adding an entry here, also update the default 'cinoptions' in
7126 * doc/indent.txt, and add explanation for it! */
7127 switch (*l)
7128 {
7129 case '>': buf->b_ind_level = n; break;
7130 case 'e': buf->b_ind_open_imag = n; break;
7131 case 'n': buf->b_ind_no_brace = n; break;
7132 case 'f': buf->b_ind_first_open = n; break;
7133 case '{': buf->b_ind_open_extra = n; break;
7134 case '}': buf->b_ind_close_extra = n; break;
7135 case '^': buf->b_ind_open_left_imag = n; break;
7136 case 'L': buf->b_ind_jump_label = n; break;
7137 case ':': buf->b_ind_case = n; break;
7138 case '=': buf->b_ind_case_code = n; break;
7139 case 'b': buf->b_ind_case_break = n; break;
7140 case 'p': buf->b_ind_param = n; break;
7141 case 't': buf->b_ind_func_type = n; break;
7142 case '/': buf->b_ind_comment = n; break;
7143 case 'c': buf->b_ind_in_comment = n; break;
7144 case 'C': buf->b_ind_in_comment2 = n; break;
7145 case 'i': buf->b_ind_cpp_baseclass = n; break;
7146 case '+': buf->b_ind_continuation = n; break;
7147 case '(': buf->b_ind_unclosed = n; break;
7148 case 'u': buf->b_ind_unclosed2 = n; break;
7149 case 'U': buf->b_ind_unclosed_noignore = n; break;
7150 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7151 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7152 case 'm': buf->b_ind_matching_paren = n; break;
7153 case 'M': buf->b_ind_paren_prev = n; break;
7154 case ')': buf->b_ind_maxparen = n; break;
7155 case '*': buf->b_ind_maxcomment = n; break;
7156 case 'g': buf->b_ind_scopedecl = n; break;
7157 case 'h': buf->b_ind_scopedecl_code = n; break;
7158 case 'j': buf->b_ind_java = n; break;
7159 case 'J': buf->b_ind_js = n; break;
7160 case 'l': buf->b_ind_keep_case_label = n; break;
7161 case '#': buf->b_ind_hash_comment = n; break;
7162 case 'N': buf->b_ind_cpp_namespace = n; break;
7163 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007164 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007165 }
7166 if (*p == ',')
7167 ++p;
7168 }
7169}
7170
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007171/*
7172 * Return the desired indent for C code.
7173 * Return -1 if the indent should be left alone (inside a raw string).
7174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007176get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 pos_T cur_curpos;
7179 int amount;
7180 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007181 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 colnr_T col;
7183 char_u *theline;
7184 char_u *linecopy;
7185 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007186 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007188 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 pos_T our_paren_pos;
7190 char_u *start;
7191 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007192#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193#define BRACE_AT_START 2 /* '{' is at start of line */
7194#define BRACE_AT_END 3 /* '{' is at end of line */
7195 linenr_T ourscope;
7196 char_u *l;
7197 char_u *look;
7198 char_u terminated;
7199 int lookfor;
7200#define LOOKFOR_INITIAL 0
7201#define LOOKFOR_IF 1
7202#define LOOKFOR_DO 2
7203#define LOOKFOR_CASE 3
7204#define LOOKFOR_ANY 4
7205#define LOOKFOR_TERM 5
7206#define LOOKFOR_UNTERM 6
7207#define LOOKFOR_SCOPEDECL 7
7208#define LOOKFOR_NOBREAK 8
7209#define LOOKFOR_CPP_BASECLASS 9
7210#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007211#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007212#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214 int whilelevel;
7215 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 int n;
7217 int iscase;
7218 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007219 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007221 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007222 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007223 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007224 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007225 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007227 /* make a copy, value is changed below */
7228 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230 /* remember where the cursor was when we started */
7231 cur_curpos = curwin->w_cursor;
7232
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007233 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007234 if (cur_curpos.lnum == 1)
7235 return 0;
7236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 /* Get a copy of the current contents of the line.
7238 * This is required, because only the most recent line obtained with
7239 * ml_get is valid! */
7240 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7241 if (linecopy == NULL)
7242 return 0;
7243
7244 /*
7245 * In insert mode and the cursor is on a ')' truncate the line at the
7246 * cursor position. We don't want to line up with the matching '(' when
7247 * inserting new stuff.
7248 * For unknown reasons the cursor might be past the end of the line, thus
7249 * check for that.
7250 */
7251 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007252 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 && linecopy[curwin->w_cursor.col] == ')')
7254 linecopy[curwin->w_cursor.col] = NUL;
7255
7256 theline = skipwhite(linecopy);
7257
7258 /* move the cursor to the start of the line */
7259
7260 curwin->w_cursor.col = 0;
7261
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007262 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007263
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007265 * If we are inside a raw string don't change the indent.
7266 * Ignore a raw string inside a comment.
7267 */
7268 comment_pos = ind_find_start_comment();
7269 if (comment_pos != NULL)
7270 {
7271 /* findmatchlimit() static pos is overwritten, make a copy */
7272 tryposCopy = *comment_pos;
7273 comment_pos = &tryposCopy;
7274 }
7275 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007276 if (trypos != NULL && (comment_pos == NULL
7277 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007278 {
7279 amount = -1;
7280 goto laterend;
7281 }
7282
7283 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 * #defines and so on always go at the left when included in 'cinkeys'.
7285 */
7286 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007287 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007288 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007289 goto theend;
7290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
7292 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007293 * Is it a non-case label? Then that goes at the left margin too unless:
7294 * - JS flag is set.
7295 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007297 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007298 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
7300 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007301 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303
7304 /*
7305 * If we're inside a "//" comment and there is a "//" comment in a
7306 * previous line, lineup with that one.
7307 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007308 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 && (trypos = find_line_comment()) != NULL) /* XXX */
7310 {
7311 /* find how indented the line beginning the comment is */
7312 getvcol(curwin, trypos, &col, NULL, NULL);
7313 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007314 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 }
7316
7317 /*
7318 * If we're inside a comment and not looking at the start of the
7319 * comment, try using the 'comments' option.
7320 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007321 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
7323 int lead_start_len = 2;
7324 int lead_middle_len = 1;
7325 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7326 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7327 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7328 char_u *p;
7329 int start_align = 0;
7330 int start_off = 0;
7331 int done = FALSE;
7332
7333 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007334 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007336 *lead_start = NUL;
7337 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
7339 p = curbuf->b_p_com;
7340 while (*p != NUL)
7341 {
7342 int align = 0;
7343 int off = 0;
7344 int what = 0;
7345
7346 while (*p != NUL && *p != ':')
7347 {
7348 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7349 what = *p++;
7350 else if (*p == COM_LEFT || *p == COM_RIGHT)
7351 align = *p++;
7352 else if (VIM_ISDIGIT(*p) || *p == '-')
7353 off = getdigits(&p);
7354 else
7355 ++p;
7356 }
7357
7358 if (*p == ':')
7359 ++p;
7360 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7361 if (what == COM_START)
7362 {
7363 STRCPY(lead_start, lead_end);
7364 lead_start_len = (int)STRLEN(lead_start);
7365 start_off = off;
7366 start_align = align;
7367 }
7368 else if (what == COM_MIDDLE)
7369 {
7370 STRCPY(lead_middle, lead_end);
7371 lead_middle_len = (int)STRLEN(lead_middle);
7372 }
7373 else if (what == COM_END)
7374 {
7375 /* If our line starts with the middle comment string, line it
7376 * up with the comment opener per the 'comments' option. */
7377 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7378 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7379 {
7380 done = TRUE;
7381 if (curwin->w_cursor.lnum > 1)
7382 {
7383 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007384 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 * the middle comment string matches in the previous
7386 * line, use the indent of that line. XXX */
7387 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7388 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7389 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7390 else if (STRNCMP(look, lead_middle,
7391 lead_middle_len) == 0)
7392 {
7393 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7394 break;
7395 }
7396 /* If the start comment string doesn't match with the
7397 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007398 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 lead_start, lead_start_len) != 0)
7400 continue;
7401 }
7402 if (start_off != 0)
7403 amount += start_off;
7404 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007405 amount += vim_strsize(lead_start)
7406 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 break;
7408 }
7409
7410 /* If our line starts with the end comment string, line it up
7411 * with the middle comment */
7412 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7413 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7414 {
7415 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7416 /* XXX */
7417 if (off != 0)
7418 amount += off;
7419 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007420 amount += vim_strsize(lead_start)
7421 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 done = TRUE;
7423 break;
7424 }
7425 }
7426 }
7427
7428 /* If our line starts with an asterisk, line up with the
7429 * asterisk in the comment opener; otherwise, line up
7430 * with the first character of the comment text.
7431 */
7432 if (done)
7433 ;
7434 else if (theline[0] == '*')
7435 amount += 1;
7436 else
7437 {
7438 /*
7439 * If we are more than one line away from the comment opener, take
7440 * the indent of the previous non-empty line. If 'cino' has "CO"
7441 * and we are just below the comment opener and there are any
7442 * white characters after it line up with the text after it;
7443 * otherwise, add the amount specified by "c" in 'cino'
7444 */
7445 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007446 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
7448 if (linewhite(lnum)) /* skip blank lines */
7449 continue;
7450 amount = get_indent_lnum(lnum); /* XXX */
7451 break;
7452 }
7453 if (amount == -1) /* use the comment opener */
7454 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007455 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007457 start = ml_get(comment_pos->lnum);
7458 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007460 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007462 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007464 if (curbuf->b_ind_in_comment2 || *look == NUL)
7465 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007468 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 }
7470
7471 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007472 * Are we looking at a ']' that has a match?
7473 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007474 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007475 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7476 {
7477 /* align with the line containing the '['. */
7478 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007479 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007480 }
7481
7482 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 * Are we inside parentheses or braces?
7484 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007485 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007486 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007487 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 || trypos != NULL)
7489 {
7490 if (trypos != NULL && tryposBrace != NULL)
7491 {
7492 /* Both an unmatched '(' and '{' is found. Use the one which is
7493 * closer to the current cursor position, set the other to NULL. */
7494 if (trypos->lnum != tryposBrace->lnum
7495 ? trypos->lnum < tryposBrace->lnum
7496 : trypos->col < tryposBrace->col)
7497 trypos = NULL;
7498 else
7499 tryposBrace = NULL;
7500 }
7501
7502 if (trypos != NULL)
7503 {
7504 /*
7505 * If the matching paren is more than one line away, use the indent of
7506 * a previous non-empty line that matches the same paren.
7507 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007508 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007510 /* Line up with the start of the matching paren line. */
7511 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7512 }
7513 else
7514 {
7515 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007516 our_paren_pos = *trypos;
7517 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007519 l = skipwhite(ml_get(lnum));
7520 if (cin_nocode(l)) /* skip comment lines */
7521 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007522 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007523 continue; /* ignore #define, #if, etc. */
7524 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007526 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007527 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007528 {
7529 lnum = trypos->lnum + 1;
7530 continue;
7531 }
7532
7533 /* XXX */
7534 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007535 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007536 && trypos->lnum == our_paren_pos.lnum
7537 && trypos->col == our_paren_pos.col)
7538 {
7539 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007541 if (theline[0] == ')')
7542 {
7543 if (our_paren_pos.lnum != lnum
7544 && cur_amount > amount)
7545 cur_amount = amount;
7546 amount = -1;
7547 }
7548 break;
7549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551 }
7552
7553 /*
7554 * Line up with line where the matching paren is. XXX
7555 * If the line starts with a '(' or the indent for unclosed
7556 * parentheses is zero, line up with the unclosed parentheses.
7557 */
7558 if (amount == -1)
7559 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007560 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007561 int is_if_for_while = 0;
7562
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007563 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007564 {
7565 /* Look for the outermost opening parenthesis on this line
7566 * and check whether it belongs to an "if", "for" or "while". */
7567
7568 pos_T cursor_save = curwin->w_cursor;
7569 pos_T outermost;
7570 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007571
7572 trypos = &our_paren_pos;
7573 do {
7574 outermost = *trypos;
7575 curwin->w_cursor.lnum = outermost.lnum;
7576 curwin->w_cursor.col = outermost.col;
7577
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007578 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007579 } while (trypos && trypos->lnum == outermost.lnum);
7580
7581 curwin->w_cursor = cursor_save;
7582
7583 line = ml_get(outermost.lnum);
7584
7585 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007586 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007587 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007588
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007589 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007590 look = skipwhite(look);
7591 if (*look == '(')
7592 {
7593 linenr_T save_lnum = curwin->w_cursor.lnum;
7594 char_u *line;
7595 int look_col;
7596
7597 /* Ignore a '(' in front of the line that has a match before
7598 * our matching '('. */
7599 curwin->w_cursor.lnum = our_paren_pos.lnum;
7600 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007601 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007602 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007603 if ((trypos = findmatchlimit(NULL, ')', 0,
7604 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007605 != NULL
7606 && trypos->lnum == our_paren_pos.lnum
7607 && trypos->col < our_paren_pos.col)
7608 ignore_paren_col = trypos->col + 1;
7609
7610 curwin->w_cursor.lnum = save_lnum;
7611 look = ml_get(our_paren_pos.lnum) + look_col;
7612 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007613 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7614 && is_if_for_while == 0)
7615 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007616 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 {
7618 /*
7619 * If we're looking at a close paren, line up right there;
7620 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007621 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 * the last nonwhite character of the line, use either the
7623 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007624 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 * lines).
7626 */
7627 if (theline[0] != ')')
7628 {
7629 cur_amount = MAXCOL;
7630 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007631 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 && cin_ends_in(l, (char_u *)"(", NULL))
7633 {
7634 /* look for opening unmatched paren, indent one level
7635 * for each additional level */
7636 n = 1;
7637 for (col = 0; col < our_paren_pos.col; ++col)
7638 {
7639 switch (l[col])
7640 {
7641 case '(':
7642 case '{': ++n;
7643 break;
7644
7645 case ')':
7646 case '}': if (n > 1)
7647 --n;
7648 break;
7649 }
7650 }
7651
7652 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007653 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007655 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 our_paren_pos.col++;
7657 else
7658 {
7659 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007660 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 col++;
7662 if (l[col] != NUL) /* In case of trailing space */
7663 our_paren_pos.col = col;
7664 else
7665 our_paren_pos.col++;
7666 }
7667 }
7668
7669 /*
7670 * Find how indented the paren is, or the character after it
7671 * if we did the above "if".
7672 */
7673 if (our_paren_pos.col > 0)
7674 {
7675 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7676 if (cur_amount > (int)col)
7677 cur_amount = col;
7678 }
7679 }
7680
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007681 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
7683 /* Line up with the start of the matching paren line. */
7684 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007685 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7686 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007687 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {
7689 if (cur_amount != MAXCOL)
7690 amount = cur_amount;
7691 }
7692 else
7693 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007694 /* Add b_ind_unclosed2 for each '(' before our matching one,
7695 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007697 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {
7699 --our_paren_pos.col;
7700 switch (*ml_get_pos(&our_paren_pos))
7701 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007702 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 col = our_paren_pos.col;
7704 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007705 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 col = MAXCOL;
7707 break;
7708 }
7709 }
7710
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007711 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 * braces */
7713 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007714 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 else
7716 {
7717 curwin->w_cursor.lnum = our_paren_pos.lnum;
7718 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007719 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7720 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007721 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007723 {
7724 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007725 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007726 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007727 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
7730 /*
7731 * For a line starting with ')' use the minimum of the two
7732 * positions, to avoid giving it more indent than the previous
7733 * lines:
7734 * func_long_name( if (x
7735 * arg && yy
7736 * ) ^ not here ) ^ not here
7737 */
7738 if (cur_amount < amount)
7739 amount = cur_amount;
7740 }
7741 }
7742
7743 /* add extra indent for a comment */
7744 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007745 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 else
7748 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007749 /*
7750 * We are inside braces, there is a { before this line at the position
7751 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007752 * Make a copy of tryposBrace, it may point to pos_copy inside
7753 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007754 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007755 tryposCopy = *tryposBrace;
7756 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 ourscope = trypos->lnum;
7759 start = ml_get(ourscope);
7760
7761 /*
7762 * Now figure out how indented the line is in general.
7763 * If the brace was at the start of the line, we use that;
7764 * otherwise, check out the indentation of the line as
7765 * a whole and then add the "imaginary indent" to that.
7766 */
7767 look = skipwhite(start);
7768 if (*look == '{')
7769 {
7770 getvcol(curwin, trypos, &col, NULL, NULL);
7771 amount = col;
7772 if (*start == '{')
7773 start_brace = BRACE_IN_COL0;
7774 else
7775 start_brace = BRACE_AT_START;
7776 }
7777 else
7778 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007779 /* That opening brace might have been on a continuation
7780 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 curwin->w_cursor.lnum = ourscope;
7782
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007783 /* Position the cursor over the rightmost paren, so that
7784 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 lnum = ourscope;
7786 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007787 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7788 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 lnum = trypos->lnum;
7790
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007791 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 * case 1: if (asdf &&
7793 * ldfd) {
7794 * }
7795 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007796 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7797 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007799 else if (curbuf->b_ind_js)
7800 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007802 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
7804 start_brace = BRACE_AT_END;
7805 }
7806
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007807 /* For Javascript check if the line starts with "key:". */
7808 if (curbuf->b_ind_js)
7809 js_cur_has_key = cin_has_js_key(theline);
7810
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007812 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 * we want to be. otherwise, add the amount of room
7814 * that an indent is supposed to be.
7815 */
7816 if (theline[0] == '}')
7817 {
7818 /*
7819 * they may want closing braces to line up with something
7820 * other than the open brace. indulge them, if so.
7821 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007822 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 }
7824 else
7825 {
7826 /*
7827 * If we're looking at an "else", try to find an "if"
7828 * to match it with.
7829 * If we're looking at a "while", try to find a "do"
7830 * to match it with.
7831 */
7832 lookfor = LOOKFOR_INITIAL;
7833 if (cin_iselse(theline))
7834 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007835 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 lookfor = LOOKFOR_DO;
7837 if (lookfor != LOOKFOR_INITIAL)
7838 {
7839 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007840 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {
7842 amount = get_indent(); /* XXX */
7843 goto theend;
7844 }
7845 }
7846
7847 /*
7848 * We get here if we are not on an "while-of-do" or "else" (or
7849 * failed to find a matching "if").
7850 * Search backwards for something to line up with.
7851 * First set amount for when we don't find anything.
7852 */
7853
7854 /*
7855 * if the '{' is _really_ at the left margin, use the imaginary
7856 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007857 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 */
7859
7860 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7861 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007862 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007863 lookfor_cpp_namespace = TRUE;
7864 }
7865 else if (start_brace == BRACE_AT_START &&
7866 lookfor_cpp_namespace) /* '{' is at start */
7867 {
7868
7869 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871 else
7872 {
7873 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007874 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007875 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007876
7877 l = skipwhite(ml_get_curline());
7878 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007879 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007880 else if (cin_is_cpp_extern_c(l))
7881 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 else
7884 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007885 /* Compensate for adding b_ind_open_extra later. */
7886 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 if (amount < 0)
7888 amount = 0;
7889 }
7890 }
7891
7892 lookfor_break = FALSE;
7893
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007894 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
7896 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007897 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899 else if (cin_isscopedecl(theline)) /* private:, ... */
7900 {
7901 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007902 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904 else
7905 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007906 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7907 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 lookfor_break = TRUE;
7909
7910 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007911 /* b_ind_level from start of block */
7912 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 }
7914 scope_amount = amount;
7915 whilelevel = 0;
7916
7917 /*
7918 * Search backwards. If we find something we recognize, line up
7919 * with that.
7920 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007921 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 * the usual amount relative to the conditional
7923 * that opens the block.
7924 */
7925 curwin->w_cursor = cur_curpos;
7926 for (;;)
7927 {
7928 curwin->w_cursor.lnum--;
7929 curwin->w_cursor.col = 0;
7930
7931 /*
7932 * If we went all the way back to the start of our scope, line
7933 * up with it.
7934 */
7935 if (curwin->w_cursor.lnum <= ourscope)
7936 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007937 /* We reached end of scope:
7938 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007940 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 * don't add ind_continuation, otherwise it is a variable
7942 * declaration:
7943 * int x,
7944 * here; <-- add ind_continuation
7945 */
7946 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7947 {
7948 if (curwin->w_cursor.lnum == 0
7949 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007950 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007952 /* nothing found (abuse curbuf->b_ind_maxparen as
7953 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 * initialization) */
7955 if (cont_amount > 0)
7956 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007957 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 amount += ind_continuation;
7959 break;
7960 }
7961
7962 l = ml_get_curline();
7963
7964 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007965 * If we're in a comment or raw string now, skip to
7966 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007968 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (trypos != NULL)
7970 {
7971 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007972 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 continue;
7974 }
7975
7976 /*
7977 * Skip preprocessor directives and blank lines.
7978 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007979 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7980 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 continue;
7982
7983 if (cin_nocode(l))
7984 continue;
7985
7986 terminated = cin_isterminated(l, FALSE, TRUE);
7987
7988 /*
7989 * If we are at top level and the line looks like a
7990 * function declaration, we are done
7991 * (it's a variable declaration).
7992 */
7993 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007994 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {
7996 /* if the line is terminated with another ','
7997 * it is a continued variable initialization.
7998 * don't add extra indent.
7999 * TODO: does not work, if a function
8000 * declaration is split over multiple lines:
8001 * cin_isfuncdecl returns FALSE then.
8002 */
8003 if (terminated == ',')
8004 break;
8005
8006 /* if it es a enum declaration or an assignment,
8007 * we are done.
8008 */
8009 if (terminated != ';' && cin_isinit())
8010 break;
8011
8012 /* nothing useful found */
8013 if (terminated == 0 || terminated == '{')
8014 continue;
8015 }
8016
8017 if (terminated != ';')
8018 {
8019 /* Skip parens and braces. Position the cursor
8020 * over the rightmost paren, so that matching it
8021 * will take us back to the start of the line.
8022 */ /* XXX */
8023 trypos = NULL;
8024 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008025 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008026 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027
8028 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008029 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
8031 if (trypos != NULL)
8032 {
8033 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008034 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 continue;
8036 }
8037 }
8038
8039 /* it's a variable declaration, add indentation
8040 * like in
8041 * int a,
8042 * b;
8043 */
8044 if (cont_amount > 0)
8045 amount = cont_amount;
8046 else
8047 amount += ind_continuation;
8048 }
8049 else if (lookfor == LOOKFOR_UNTERM)
8050 {
8051 if (cont_amount > 0)
8052 amount = cont_amount;
8053 else
8054 amount += ind_continuation;
8055 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008056 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008057 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008058 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008059 && lookfor != LOOKFOR_CPP_BASECLASS
8060 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008061 {
8062 amount = scope_amount;
8063 if (theline[0] == '{')
8064 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008065 amount += curbuf->b_ind_open_extra;
8066 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008067 }
8068 }
8069
8070 if (lookfor_cpp_namespace)
8071 {
8072 /*
8073 * Looking for C++ namespace, need to look further
8074 * back.
8075 */
8076 if (curwin->w_cursor.lnum == ourscope)
8077 continue;
8078
8079 if (curwin->w_cursor.lnum == 0
8080 || curwin->w_cursor.lnum
8081 < ourscope - FIND_NAMESPACE_LIM)
8082 break;
8083
8084 l = ml_get_curline();
8085
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008086 /* If we're in a comment or raw string now, skip
8087 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008088 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008089 if (trypos != NULL)
8090 {
8091 curwin->w_cursor.lnum = trypos->lnum + 1;
8092 curwin->w_cursor.col = 0;
8093 continue;
8094 }
8095
8096 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008097 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8098 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008099 continue;
8100
8101 /* Finally the actual check for "namespace". */
8102 if (cin_is_cpp_namespace(l))
8103 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008104 amount += curbuf->b_ind_cpp_namespace
8105 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008106 break;
8107 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008108 else if (cin_is_cpp_extern_c(l))
8109 {
8110 amount += curbuf->b_ind_cpp_extern_c
8111 - added_to_amount;
8112 break;
8113 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008114
8115 if (cin_nocode(l))
8116 continue;
8117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 }
8119 break;
8120 }
8121
8122 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008123 * If we're in a comment or raw string now, skip to the start
8124 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008126 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {
8128 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008129 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 continue;
8131 }
8132
8133 l = ml_get_curline();
8134
8135 /*
8136 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008137 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008139 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 if (iscase || cin_isscopedecl(l))
8141 {
8142 /* we are only looking for cpp base class
8143 * declaration/initialization any longer */
8144 if (lookfor == LOOKFOR_CPP_BASECLASS)
8145 break;
8146
8147 /* When looking for a "do" we are not interested in
8148 * labels. */
8149 if (whilelevel > 0)
8150 continue;
8151
8152 /*
8153 * case xx:
8154 * c = 99 + <- this indent plus continuation
8155 *-> here;
8156 */
8157 if (lookfor == LOOKFOR_UNTERM
8158 || lookfor == LOOKFOR_ENUM_OR_INIT)
8159 {
8160 if (cont_amount > 0)
8161 amount = cont_amount;
8162 else
8163 amount += ind_continuation;
8164 break;
8165 }
8166
8167 /*
8168 * case xx: <- line up with this case
8169 * x = 333;
8170 * case yy:
8171 */
8172 if ( (iscase && lookfor == LOOKFOR_CASE)
8173 || (iscase && lookfor_break)
8174 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8175 {
8176 /*
8177 * Check that this case label is not for another
8178 * switch()
8179 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008180 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008181 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {
8183 amount = get_indent(); /* XXX */
8184 break;
8185 }
8186 continue;
8187 }
8188
8189 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8190
8191 /*
8192 * case xx: if (cond) <- line up with this if
8193 * y = y + 1;
8194 * -> s = 99;
8195 *
8196 * case xx:
8197 * if (cond) <- line up with this line
8198 * y = y + 1;
8199 * -> s = 99;
8200 */
8201 if (lookfor == LOOKFOR_TERM)
8202 {
8203 if (n)
8204 amount = n;
8205
8206 if (!lookfor_break)
8207 break;
8208 }
8209
8210 /*
8211 * case xx: x = x + 1; <- line up with this x
8212 * -> y = y + 1;
8213 *
8214 * case xx: if (cond) <- line up with this if
8215 * -> y = y + 1;
8216 */
8217 if (n)
8218 {
8219 amount = n;
8220 l = after_label(ml_get_curline());
8221 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008222 {
8223 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008224 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008225 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008226 amount += curbuf->b_ind_level
8227 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 break;
8230 }
8231
8232 /*
8233 * Try to get the indent of a statement before the switch
8234 * label. If nothing is found, line up relative to the
8235 * switch label.
8236 * break; <- may line up with this line
8237 * case xx:
8238 * -> y = 1;
8239 */
8240 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008241 ? curbuf->b_ind_case_code
8242 : curbuf->b_ind_scopedecl_code);
8243 lookfor = curbuf->b_ind_case_break
8244 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 continue;
8246 }
8247
8248 /*
8249 * Looking for a switch() label or C++ scope declaration,
8250 * ignore other lines, skip {}-blocks.
8251 */
8252 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8253 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008254 if (find_last_paren(l, '{', '}')
8255 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008258 curwin->w_cursor.col = 0;
8259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 continue;
8261 }
8262
8263 /*
8264 * Ignore jump labels with nothing after them.
8265 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008266 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {
8268 l = after_label(ml_get_curline());
8269 if (l == NULL || cin_nocode(l))
8270 continue;
8271 }
8272
8273 /*
8274 * Ignore #defines, #if, etc.
8275 * Ignore comment and empty lines.
8276 * (need to get the line again, cin_islabel() may have
8277 * unlocked it)
8278 */
8279 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008280 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 || cin_nocode(l))
8282 continue;
8283
8284 /*
8285 * Are we at the start of a cpp base class declaration or
8286 * constructor initialization?
8287 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008288 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008289 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008290 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008291 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008292 l = ml_get_curline();
8293 }
8294 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
8296 if (lookfor == LOOKFOR_UNTERM)
8297 {
8298 if (cont_amount > 0)
8299 amount = cont_amount;
8300 else
8301 amount += ind_continuation;
8302 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008303 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008305 /* Need to find start of the declaration. */
8306 lookfor = LOOKFOR_UNTERM;
8307 ind_continuation = 0;
8308 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
8310 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008311 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008312 amount = get_baseclass_amount(
8313 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 break;
8315 }
8316 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8317 {
8318 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008319 * declaration or initialization before the opening brace.
8320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (cin_isterminated(l, TRUE, FALSE))
8322 break;
8323 else
8324 continue;
8325 }
8326
8327 /*
8328 * What happens next depends on the line being terminated.
8329 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008330 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 * 123,
8332 * sizeof
8333 * here
8334 * Otherwise check whether it is a enumeration or structure
8335 * initialisation (not indented) or a variable declaration
8336 * (indented).
8337 */
8338 terminated = cin_isterminated(l, FALSE, TRUE);
8339
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008340 if (js_cur_has_key)
8341 {
8342 js_cur_has_key = 0; /* only check the first line */
8343 if (curbuf->b_ind_js && terminated == ',')
8344 {
8345 /* For Javascript we might be inside an object:
8346 * key: something, <- align with this
8347 * key: something
8348 * or:
8349 * key: something + <- align with this
8350 * something,
8351 * key: something
8352 */
8353 lookfor = LOOKFOR_JS_KEY;
8354 }
8355 }
8356 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8357 {
8358 amount = get_indent();
8359 break;
8360 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008361 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008362 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008363 if (tryposBrace != NULL && tryposBrace->lnum
8364 >= curwin->w_cursor.lnum)
8365 break;
8366 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008367 /* line below current line is the one that starts a
8368 * (possibly broken) line ending in a comma. */
8369 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008370 else
8371 {
8372 amount = get_indent();
8373 if (curwin->w_cursor.lnum - 1 == ourscope)
8374 /* line above is start of the scope, thus current
8375 * line is the one that stars a (possibly broken)
8376 * line ending in a comma. */
8377 break;
8378 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008379 }
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8382 && terminated == ','))
8383 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008384 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8385 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008386 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 /*
8388 * if we're in the middle of a paren thing,
8389 * go back to the line that starts it so
8390 * we can get the right prevailing indent
8391 * if ( foo &&
8392 * bar )
8393 */
8394 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008395 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008397 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 */
8399 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008400 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008401 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8402 || (trypos->lnum == tryposBrace->lnum
8403 && trypos->col < tryposBrace->col)))
8404 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405
8406 /*
8407 * If we are looking for ',', we also look for matching
8408 * braces.
8409 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008410 if (trypos == NULL && terminated == ','
8411 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008412 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414 if (trypos != NULL)
8415 {
8416 /*
8417 * Check if we are on a case label now. This is
8418 * handled above.
8419 * case xx: if ( asdf &&
8420 * asdf)
8421 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008422 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008424 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
8426 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008427 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 continue;
8429 }
8430 }
8431
8432 /*
8433 * Skip over continuation lines to find the one to get the
8434 * indent from
8435 * char *usethis = "bla\
8436 * bla",
8437 * here;
8438 */
8439 if (terminated == ',')
8440 {
8441 while (curwin->w_cursor.lnum > 1)
8442 {
8443 l = ml_get(curwin->w_cursor.lnum - 1);
8444 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8445 break;
8446 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008447 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
8449 }
8450
8451 /*
8452 * Get indent and pointer to text for current line,
8453 * ignoring any jump label. XXX
8454 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008455 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008456 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008457 else
8458 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 /*
8460 * If this is just above the line we are indenting, and it
8461 * starts with a '{', line it up with this line.
8462 * while (not)
8463 * -> {
8464 * }
8465 */
8466 if (terminated != ',' && lookfor != LOOKFOR_TERM
8467 && theline[0] == '{')
8468 {
8469 amount = cur_amount;
8470 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008471 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 * doesn't start with a '{', which must have a match
8473 * in the same line (scope is the same). Probably:
8474 * { 1, 2 },
8475 * -> { 3, 4 }
8476 */
8477 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008478 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008480 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {
8482 /* have to look back, whether it is a cpp base
8483 * class declaration or initialization */
8484 lookfor = LOOKFOR_CPP_BASECLASS;
8485 continue;
8486 }
8487 break;
8488 }
8489
8490 /*
8491 * Check if we are after an "if", "while", etc.
8492 * Also allow " } else".
8493 */
8494 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8495 {
8496 /*
8497 * Found an unterminated line after an if (), line up
8498 * with the last one.
8499 * if (cond)
8500 * 100 +
8501 * -> here;
8502 */
8503 if (lookfor == LOOKFOR_UNTERM
8504 || lookfor == LOOKFOR_ENUM_OR_INIT)
8505 {
8506 if (cont_amount > 0)
8507 amount = cont_amount;
8508 else
8509 amount += ind_continuation;
8510 break;
8511 }
8512
8513 /*
8514 * If this is just above the line we are indenting, we
8515 * are finished.
8516 * while (not)
8517 * -> here;
8518 * Otherwise this indent can be used when the line
8519 * before this is terminated.
8520 * yyy;
8521 * if (stat)
8522 * while (not)
8523 * xxx;
8524 * -> here;
8525 */
8526 amount = cur_amount;
8527 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008528 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (lookfor != LOOKFOR_TERM)
8530 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008531 amount += curbuf->b_ind_level
8532 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 break;
8534 }
8535
8536 /*
8537 * Special trick: when expecting the while () after a
8538 * do, line up with the while()
8539 * do
8540 * x = 1;
8541 * -> here
8542 */
8543 l = skipwhite(ml_get_curline());
8544 if (cin_isdo(l))
8545 {
8546 if (whilelevel == 0)
8547 break;
8548 --whilelevel;
8549 }
8550
8551 /*
8552 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008553 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 * Need to use the scope of this "else". XXX
8555 * If whilelevel != 0 continue looking for a "do {".
8556 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008557 if (cin_iselse(l) && whilelevel == 0)
8558 {
8559 /* If we're looking at "} else", let's make sure we
8560 * find the opening brace of the enclosing scope,
8561 * not the one from "if () {". */
8562 if (*l == '}')
8563 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008564 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008565
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008566 if ((trypos = find_start_brace()) == NULL
8567 || find_match(LOOKFOR_IF, trypos->lnum)
8568 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008569 break;
8570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 }
8572
8573 /*
8574 * If we're below an unterminated line that is not an
8575 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008576 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 * the line before this one.
8578 */
8579 else
8580 {
8581 /*
8582 * Found two unterminated lines on a row, line up with
8583 * the last one.
8584 * c = 99 +
8585 * 100 +
8586 * -> here;
8587 */
8588 if (lookfor == LOOKFOR_UNTERM)
8589 {
8590 /* When line ends in a comma add extra indent */
8591 if (terminated == ',')
8592 amount += ind_continuation;
8593 break;
8594 }
8595
8596 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8597 {
8598 /* Found two lines ending in ',', lineup with the
8599 * lowest one, but check for cpp base class
8600 * declaration/initialization, if it is an
8601 * opening brace or we are looking just for
8602 * enumerations/initializations. */
8603 if (terminated == ',')
8604 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008605 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 break;
8607
8608 lookfor = LOOKFOR_CPP_BASECLASS;
8609 continue;
8610 }
8611
8612 /* Ignore unterminated lines in between, but
8613 * reduce indent. */
8614 if (amount > cur_amount)
8615 amount = cur_amount;
8616 }
8617 else
8618 {
8619 /*
8620 * Found first unterminated line on a row, may
8621 * line up with this line, remember its indent
8622 * 100 +
8623 * -> here;
8624 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008625 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008627
8628 n = (int)STRLEN(l);
8629 if (terminated == ',' && (*skipwhite(l) == ']'
8630 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632
8633 /*
8634 * If previous line ends in ',', check whether we
8635 * are in an initialization or enum
8636 * struct xxx =
8637 * {
8638 * sizeof a,
8639 * 124 };
8640 * or a normal possible continuation line.
8641 * but only, of no other statement has been found
8642 * yet.
8643 */
8644 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8645 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008646 if (curbuf->b_ind_js)
8647 {
8648 /* Search for a line ending in a comma
8649 * and line up with the line below it
8650 * (could be the current line).
8651 * some = [
8652 * 1, <- line up here
8653 * 2,
8654 * some = [
8655 * 3 + <- line up here
8656 * 4 *
8657 * 5,
8658 * 6,
8659 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008660 if (cin_iscomment(skipwhite(l)))
8661 break;
8662 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008663 trypos = find_match_char('[',
8664 curbuf->b_ind_maxparen);
8665 if (trypos != NULL)
8666 {
8667 if (trypos->lnum
8668 == curwin->w_cursor.lnum - 1)
8669 {
8670 /* Current line is first inside
8671 * [], line up with it. */
8672 break;
8673 }
8674 ourscope = trypos->lnum;
8675 }
8676 }
8677 else
8678 {
8679 lookfor = LOOKFOR_ENUM_OR_INIT;
8680 cont_amount = cin_first_id_amount();
8681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 }
8683 else
8684 {
8685 if (lookfor == LOOKFOR_INITIAL
8686 && *l != NUL
8687 && l[STRLEN(l) - 1] == '\\')
8688 /* XXX */
8689 cont_amount = cin_get_equal_amount(
8690 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008691 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008692 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008693 && lookfor != LOOKFOR_COMMA
8694 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 lookfor = LOOKFOR_UNTERM;
8696 }
8697 }
8698 }
8699 }
8700
8701 /*
8702 * Check if we are after a while (cond);
8703 * If so: Ignore until the matching "do".
8704 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008705 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 {
8707 /*
8708 * Found an unterminated line after a while ();, line up
8709 * with the last one.
8710 * while (cond);
8711 * 100 + <- line up with this one
8712 * -> here;
8713 */
8714 if (lookfor == LOOKFOR_UNTERM
8715 || lookfor == LOOKFOR_ENUM_OR_INIT)
8716 {
8717 if (cont_amount > 0)
8718 amount = cont_amount;
8719 else
8720 amount += ind_continuation;
8721 break;
8722 }
8723
8724 if (whilelevel == 0)
8725 {
8726 lookfor = LOOKFOR_TERM;
8727 amount = get_indent(); /* XXX */
8728 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008729 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 }
8731 ++whilelevel;
8732 }
8733
8734 /*
8735 * We are after a "normal" statement.
8736 * If we had another statement we can stop now and use the
8737 * indent of that other statement.
8738 * Otherwise the indent of the current statement may be used,
8739 * search backwards for the next "normal" statement.
8740 */
8741 else
8742 {
8743 /*
8744 * Skip single break line, if before a switch label. It
8745 * may be lined up with the case label.
8746 */
8747 if (lookfor == LOOKFOR_NOBREAK
8748 && cin_isbreak(skipwhite(ml_get_curline())))
8749 {
8750 lookfor = LOOKFOR_ANY;
8751 continue;
8752 }
8753
8754 /*
8755 * Handle "do {" line.
8756 */
8757 if (whilelevel > 0)
8758 {
8759 l = cin_skipcomment(ml_get_curline());
8760 if (cin_isdo(l))
8761 {
8762 amount = get_indent(); /* XXX */
8763 --whilelevel;
8764 continue;
8765 }
8766 }
8767
8768 /*
8769 * Found a terminated line above an unterminated line. Add
8770 * the amount for a continuation line.
8771 * x = 1;
8772 * y = foo +
8773 * -> here;
8774 * or
8775 * int x = 1;
8776 * int foo,
8777 * -> here;
8778 */
8779 if (lookfor == LOOKFOR_UNTERM
8780 || lookfor == LOOKFOR_ENUM_OR_INIT)
8781 {
8782 if (cont_amount > 0)
8783 amount = cont_amount;
8784 else
8785 amount += ind_continuation;
8786 break;
8787 }
8788
8789 /*
8790 * Found a terminated line above a terminated line or "if"
8791 * etc. line. Use the amount of the line below us.
8792 * x = 1; x = 1;
8793 * if (asdf) y = 2;
8794 * while (asdf) ->here;
8795 * here;
8796 * ->foo;
8797 */
8798 if (lookfor == LOOKFOR_TERM)
8799 {
8800 if (!lookfor_break && whilelevel == 0)
8801 break;
8802 }
8803
8804 /*
8805 * First line above the one we're indenting is terminated.
8806 * To know what needs to be done look further backward for
8807 * a terminated line.
8808 */
8809 else
8810 {
8811 /*
8812 * position the cursor over the rightmost paren, so
8813 * that matching it will take us back to the start of
8814 * the line. Helps for:
8815 * func(asdr,
8816 * asdfasdf);
8817 * here;
8818 */
8819term_again:
8820 l = ml_get_curline();
8821 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008822 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008823 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
8825 /*
8826 * Check if we are on a case label now. This is
8827 * handled above.
8828 * case xx: if ( asdf &&
8829 * asdf)
8830 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008831 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008833 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
8835 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008836 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 continue;
8838 }
8839 }
8840
8841 /* When aligning with the case statement, don't align
8842 * with a statement after it.
8843 * case 1: { <-- don't use this { position
8844 * stat;
8845 * }
8846 * case 2:
8847 * stat;
8848 * }
8849 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008850 iscase = (curbuf->b_ind_keep_case_label
8851 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 /*
8854 * Get indent and pointer to text for current line,
8855 * ignoring any jump label.
8856 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008857 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
8859 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008860 amount += curbuf->b_ind_open_extra;
8861 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008862 l = skipwhite(l);
8863 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008864 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8866
8867 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008868 * When a terminated line starts with "else" skip to
8869 * the matching "if":
8870 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008871 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008872 * Need to use the scope of this "else". XXX
8873 * If whilelevel != 0 continue looking for a "do {".
8874 */
8875 if (lookfor == LOOKFOR_TERM
8876 && *l != '}'
8877 && cin_iselse(l)
8878 && whilelevel == 0)
8879 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008880 if ((trypos = find_start_brace()) == NULL
8881 || find_match(LOOKFOR_IF, trypos->lnum)
8882 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008883 break;
8884 continue;
8885 }
8886
8887 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 * If we're at the end of a block, skip to the start of
8889 * that block.
8890 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008891 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008892 if (find_last_paren(l, '{', '}') /* XXX */
8893 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008895 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 /* if not "else {" check for terminated again */
8897 /* but skip block for "} else {" */
8898 l = cin_skipcomment(ml_get_curline());
8899 if (*l == '}' || !cin_iselse(l))
8900 goto term_again;
8901 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008902 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
8904 }
8905 }
8906 }
8907 }
8908 }
8909
8910 /* add extra indent for a comment */
8911 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008912 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008913
8914 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008915 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8916 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008917
8918 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008920
8921 /*
8922 * ok -- we're not inside any sort of structure at all!
8923 *
8924 * This means we're at the top level, and everything should
8925 * basically just match where the previous line is, except
8926 * for the lines immediately following a function declaration,
8927 * which are K&R-style parameters and need to be indented.
8928 *
8929 * if our line starts with an open brace, forget about any
8930 * prevailing indent and make sure it looks like the start
8931 * of a function
8932 */
8933
8934 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008936 amount = curbuf->b_ind_first_open;
8937 goto theend;
8938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008940 /*
8941 * If the NEXT line is a function declaration, the current
8942 * line needs to be indented as a function type spec.
8943 * Don't do this if the current line looks like a comment or if the
8944 * current line is terminated, ie. ends in ';', or if the current line
8945 * contains { or }: "void f() {\n if (1)"
8946 */
8947 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8948 && !cin_nocode(theline)
8949 && vim_strchr(theline, '{') == NULL
8950 && vim_strchr(theline, '}') == NULL
8951 && !cin_ends_in(theline, (char_u *)":", NULL)
8952 && !cin_ends_in(theline, (char_u *)",", NULL)
8953 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8954 cur_curpos.lnum + 1)
8955 && !cin_isterminated(theline, FALSE, TRUE))
8956 {
8957 amount = curbuf->b_ind_func_type;
8958 goto theend;
8959 }
8960
8961 /* search backwards until we find something we recognize */
8962 amount = 0;
8963 curwin->w_cursor = cur_curpos;
8964 while (curwin->w_cursor.lnum > 1)
8965 {
8966 curwin->w_cursor.lnum--;
8967 curwin->w_cursor.col = 0;
8968
8969 l = ml_get_curline();
8970
8971 /*
8972 * If we're in a comment or raw string now, skip to the start
8973 * of it.
8974 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008975 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008977 curwin->w_cursor.lnum = trypos->lnum + 1;
8978 curwin->w_cursor.col = 0;
8979 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
8981
8982 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008983 * Are we at the start of a cpp base class declaration or
8984 * constructor initialization?
8985 */ /* XXX */
8986 n = FALSE;
8987 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008989 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8990 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008992 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008994 /* XXX */
8995 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8996 break;
8997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008999 /*
9000 * Skip preprocessor directives and blank lines.
9001 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009002 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009003 continue;
9004
9005 if (cin_nocode(l))
9006 continue;
9007
9008 /*
9009 * If the previous line ends in ',', use one level of
9010 * indentation:
9011 * int foo,
9012 * bar;
9013 * do this before checking for '}' in case of eg.
9014 * enum foobar
9015 * {
9016 * ...
9017 * } foo,
9018 * bar;
9019 */
9020 n = 0;
9021 if (cin_ends_in(l, (char_u *)",", NULL)
9022 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9023 {
9024 /* take us back to opening paren */
9025 if (find_last_paren(l, '(', ')')
9026 && (trypos = find_match_paren(
9027 curbuf->b_ind_maxparen)) != NULL)
9028 curwin->w_cursor = *trypos;
9029
9030 /* For a line ending in ',' that is a continuation line go
9031 * back to the first line with a backslash:
9032 * char *foo = "bla\
9033 * bla",
9034 * here;
9035 */
9036 while (n == 0 && curwin->w_cursor.lnum > 1)
9037 {
9038 l = ml_get(curwin->w_cursor.lnum - 1);
9039 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9040 break;
9041 --curwin->w_cursor.lnum;
9042 curwin->w_cursor.col = 0;
9043 }
9044
9045 amount = get_indent(); /* XXX */
9046
9047 if (amount == 0)
9048 amount = cin_first_id_amount();
9049 if (amount == 0)
9050 amount = ind_continuation;
9051 break;
9052 }
9053
9054 /*
9055 * If the line looks like a function declaration, and we're
9056 * not in a comment, put it the left margin.
9057 */
9058 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9059 break;
9060 l = ml_get_curline();
9061
9062 /*
9063 * Finding the closing '}' of a previous function. Put
9064 * current line at the left margin. For when 'cino' has "fs".
9065 */
9066 if (*skipwhite(l) == '}')
9067 break;
9068
9069 /* (matching {)
9070 * If the previous line ends on '};' (maybe followed by
9071 * comments) align at column 0. For example:
9072 * char *string_array[] = { "foo",
9073 * / * x * / "b};ar" }; / * foobar * /
9074 */
9075 if (cin_ends_in(l, (char_u *)"};", NULL))
9076 break;
9077
9078 /*
9079 * If the previous line ends on '[' we are probably in an
9080 * array constant:
9081 * something = [
9082 * 234, <- extra indent
9083 */
9084 if (cin_ends_in(l, (char_u *)"[", NULL))
9085 {
9086 amount = get_indent() + ind_continuation;
9087 break;
9088 }
9089
9090 /*
9091 * Find a line only has a semicolon that belongs to a previous
9092 * line ending in '}', e.g. before an #endif. Don't increase
9093 * indent then.
9094 */
9095 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9096 {
9097 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098
9099 while (curwin->w_cursor.lnum > 1)
9100 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009101 look = ml_get(--curwin->w_cursor.lnum);
9102 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009103 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009105 }
9106 if (curwin->w_cursor.lnum > 0
9107 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009110 curwin->w_cursor = curpos_save;
9111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009113 /*
9114 * If the PREVIOUS line is a function declaration, the current
9115 * line (and the ones that follow) needs to be indented as
9116 * parameters.
9117 */
9118 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9119 {
9120 amount = curbuf->b_ind_param;
9121 break;
9122 }
9123
9124 /*
9125 * If the previous line ends in ';' and the line before the
9126 * previous line ends in ',' or '\', ident to column zero:
9127 * int foo,
9128 * bar;
9129 * indent_to_0 here;
9130 */
9131 if (cin_ends_in(l, (char_u *)";", NULL))
9132 {
9133 l = ml_get(curwin->w_cursor.lnum - 1);
9134 if (cin_ends_in(l, (char_u *)",", NULL)
9135 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9136 break;
9137 l = ml_get_curline();
9138 }
9139
9140 /*
9141 * Doesn't look like anything interesting -- so just
9142 * use the indent of this line.
9143 *
9144 * Position the cursor over the rightmost paren, so that
9145 * matching it will take us back to the start of the line.
9146 */
9147 find_last_paren(l, '(', ')');
9148
9149 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9150 curwin->w_cursor = *trypos;
9151 amount = get_indent(); /* XXX */
9152 break;
9153 }
9154
9155 /* add extra indent for a comment */
9156 if (cin_iscomment(theline))
9157 amount += curbuf->b_ind_comment;
9158
9159 /* add extra indent if the previous line ended in a backslash:
9160 * "asdfasdf\
9161 * here";
9162 * char *foo = "asdf\
9163 * here";
9164 */
9165 if (cur_curpos.lnum > 1)
9166 {
9167 l = ml_get(cur_curpos.lnum - 1);
9168 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9169 {
9170 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9171 if (cur_amount > 0)
9172 amount = cur_amount;
9173 else if (cur_amount == 0)
9174 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 }
9176 }
9177
9178theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009179 if (amount < 0)
9180 amount = 0;
9181
9182laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 /* put the cursor back where it belongs */
9184 curwin->w_cursor = cur_curpos;
9185
9186 vim_free(linecopy);
9187
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 return amount;
9189}
9190
9191 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009192find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 char_u *look;
9195 pos_T *theirscope;
9196 char_u *mightbeif;
9197 int elselevel;
9198 int whilelevel;
9199
9200 if (lookfor == LOOKFOR_IF)
9201 {
9202 elselevel = 1;
9203 whilelevel = 0;
9204 }
9205 else
9206 {
9207 elselevel = 0;
9208 whilelevel = 1;
9209 }
9210
9211 curwin->w_cursor.col = 0;
9212
9213 while (curwin->w_cursor.lnum > ourscope + 1)
9214 {
9215 curwin->w_cursor.lnum--;
9216 curwin->w_cursor.col = 0;
9217
9218 look = cin_skipcomment(ml_get_curline());
9219 if (cin_iselse(look)
9220 || cin_isif(look)
9221 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009222 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 {
9224 /*
9225 * if we've gone outside the braces entirely,
9226 * we must be out of scope...
9227 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009228 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 if (theirscope == NULL)
9230 break;
9231
9232 /*
9233 * and if the brace enclosing this is further
9234 * back than the one enclosing the else, we're
9235 * out of luck too.
9236 */
9237 if (theirscope->lnum < ourscope)
9238 break;
9239
9240 /*
9241 * and if they're enclosed in a *deeper* brace,
9242 * then we can ignore it because it's in a
9243 * different scope...
9244 */
9245 if (theirscope->lnum > ourscope)
9246 continue;
9247
9248 /*
9249 * if it was an "else" (that's not an "else if")
9250 * then we need to go back to another if, so
9251 * increment elselevel
9252 */
9253 look = cin_skipcomment(ml_get_curline());
9254 if (cin_iselse(look))
9255 {
9256 mightbeif = cin_skipcomment(look + 4);
9257 if (!cin_isif(mightbeif))
9258 ++elselevel;
9259 continue;
9260 }
9261
9262 /*
9263 * if it was a "while" then we need to go back to
9264 * another "do", so increment whilelevel. XXX
9265 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009266 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 {
9268 ++whilelevel;
9269 continue;
9270 }
9271
9272 /* If it's an "if" decrement elselevel */
9273 look = cin_skipcomment(ml_get_curline());
9274 if (cin_isif(look))
9275 {
9276 elselevel--;
9277 /*
9278 * When looking for an "if" ignore "while"s that
9279 * get in the way.
9280 */
9281 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9282 whilelevel = 0;
9283 }
9284
9285 /* If it's a "do" decrement whilelevel */
9286 if (cin_isdo(look))
9287 whilelevel--;
9288
9289 /*
9290 * if we've used up all the elses, then
9291 * this must be the if that we want!
9292 * match the indent level of that if.
9293 */
9294 if (elselevel <= 0 && whilelevel <= 0)
9295 {
9296 return OK;
9297 }
9298 }
9299 }
9300 return FAIL;
9301}
9302
9303# if defined(FEAT_EVAL) || defined(PROTO)
9304/*
9305 * Get indent level from 'indentexpr'.
9306 */
9307 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009308get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009310 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009311 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009312 pos_T save_pos;
9313 colnr_T save_curswant;
9314 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009316 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9317 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009319 /* Save and restore cursor position and curswant, in case it was changed
9320 * via :normal commands */
9321 save_pos = curwin->w_cursor;
9322 save_curswant = curwin->w_curswant;
9323 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009325 if (use_sandbox)
9326 ++sandbox;
9327 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009328
9329 /* Need to make a copy, the 'indentexpr' option could be changed while
9330 * evaluating it. */
9331 inde_copy = vim_strsave(curbuf->b_p_inde);
9332 if (inde_copy != NULL)
9333 {
9334 indent = (int)eval_to_number(inde_copy);
9335 vim_free(inde_copy);
9336 }
9337
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009338 if (use_sandbox)
9339 --sandbox;
9340 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
9342 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9343 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9344 * command. */
9345 save_State = State;
9346 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009347 curwin->w_cursor = save_pos;
9348 curwin->w_curswant = save_curswant;
9349 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 check_cursor();
9351 State = save_State;
9352
9353 /* If there is an error, just keep the current indent. */
9354 if (indent < 0)
9355 indent = get_indent();
9356
9357 return indent;
9358}
9359# endif
9360
9361#endif /* FEAT_CINDENT */
9362
9363#if defined(FEAT_LISP) || defined(PROTO)
9364
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009365static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366
9367 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009368lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 char_u buf[LSIZE];
9371 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009372 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
9374 while (*word != NUL)
9375 {
9376 (void)copy_option_part(&word, buf, LSIZE, ",");
9377 len = (int)STRLEN(buf);
9378 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9379 return TRUE;
9380 }
9381 return FALSE;
9382}
9383
9384/*
9385 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9386 * The incompatible newer method is quite a bit better at indenting
9387 * code in lisp-like languages than the traditional one; it's still
9388 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9389 *
9390 * TODO:
9391 * Findmatch() should be adapted for lisp, also to make showmatch
9392 * work correctly: now (v5.3) it seems all C/C++ oriented:
9393 * - it does not recognize the #\( and #\) notations as character literals
9394 * - it doesn't know about comments starting with a semicolon
9395 * - it incorrectly interprets '(' as a character literal
9396 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009397 * Update from Sergey Khorev:
9398 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 */
9400 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009401get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009403 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 int amount;
9405 char_u *that;
9406 colnr_T col;
9407 colnr_T firsttry;
9408 int parencount, quotecount;
9409 int vi_lisp;
9410
9411 /* Set vi_lisp to use the vi-compatible method */
9412 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9413
9414 realpos = curwin->w_cursor;
9415 curwin->w_cursor.col = 0;
9416
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009417 if ((pos = findmatch(NULL, '(')) == NULL)
9418 pos = findmatch(NULL, '[');
9419 else
9420 {
9421 paren = *pos;
9422 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009423 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009424 pos = &paren;
9425 }
9426 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 {
9428 /* Extra trick: Take the indent of the first previous non-white
9429 * line that is at the same () level. */
9430 amount = -1;
9431 parencount = 0;
9432
9433 while (--curwin->w_cursor.lnum >= pos->lnum)
9434 {
9435 if (linewhite(curwin->w_cursor.lnum))
9436 continue;
9437 for (that = ml_get_curline(); *that != NUL; ++that)
9438 {
9439 if (*that == ';')
9440 {
9441 while (*(that + 1) != NUL)
9442 ++that;
9443 continue;
9444 }
9445 if (*that == '\\')
9446 {
9447 if (*(that + 1) != NUL)
9448 ++that;
9449 continue;
9450 }
9451 if (*that == '"' && *(that + 1) != NUL)
9452 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009453 while (*++that && *that != '"')
9454 {
9455 /* skipping escaped characters in the string */
9456 if (*that == '\\')
9457 {
9458 if (*++that == NUL)
9459 break;
9460 if (that[1] == NUL)
9461 {
9462 ++that;
9463 break;
9464 }
9465 }
9466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009468 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009470 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 --parencount;
9472 }
9473 if (parencount == 0)
9474 {
9475 amount = get_indent();
9476 break;
9477 }
9478 }
9479
9480 if (amount == -1)
9481 {
9482 curwin->w_cursor.lnum = pos->lnum;
9483 curwin->w_cursor.col = pos->col;
9484 col = pos->col;
9485
9486 that = ml_get_curline();
9487
9488 if (vi_lisp && get_indent() == 0)
9489 amount = 2;
9490 else
9491 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009492 char_u *line = that;
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 amount = 0;
9495 while (*that && col)
9496 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009497 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 col--;
9499 }
9500
9501 /*
9502 * Some keywords require "body" indenting rules (the
9503 * non-standard-lisp ones are Scheme special forms):
9504 *
9505 * (let ((a 1)) instead (let ((a 1))
9506 * (...)) of (...))
9507 */
9508
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009509 if (!vi_lisp && (*that == '(' || *that == '[')
9510 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 amount += 2;
9512 else
9513 {
9514 that++;
9515 amount++;
9516 firsttry = amount;
9517
Bram Moolenaar1c465442017-03-12 20:10:05 +01009518 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009520 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 ++that;
9522 }
9523
9524 if (*that && *that != ';') /* not a comment line */
9525 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009526 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009528 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 firsttry++;
9530
9531 parencount = 0;
9532 quotecount = 0;
9533
9534 if (vi_lisp
9535 || (*that != '"'
9536 && *that != '\''
9537 && *that != '#'
9538 && (*that < '0' || *that > '9')))
9539 {
9540 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009541 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 || quotecount
9543 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009544 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 && !quotecount
9546 && !parencount
9547 && vi_lisp)))
9548 {
9549 if (*that == '"')
9550 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009551 if ((*that == '(' || *that == '[')
9552 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009554 if ((*that == ')' || *that == ']')
9555 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 --parencount;
9557 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009558 amount += lbr_chartabsize_adv(
9559 line, &that, (colnr_T)amount);
9560 amount += lbr_chartabsize_adv(
9561 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 }
9563 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009564 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009566 amount += lbr_chartabsize(
9567 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 that++;
9569 }
9570 if (!*that || *that == ';')
9571 amount = firsttry;
9572 }
9573 }
9574 }
9575 }
9576 }
9577 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009578 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579
9580 curwin->w_cursor = realpos;
9581
9582 return amount;
9583}
9584#endif /* FEAT_LISP */
9585
9586 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009587prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009589#if defined(SIGHUP) && defined(SIG_IGN)
9590 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9591 * makes Vim exit and then handling SIGHUP causes various reentrance
9592 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009593 signal(SIGHUP, SIG_IGN);
9594#endif
9595
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596#ifdef FEAT_GUI
9597 if (gui.in_use)
9598 {
9599 gui.dying = TRUE;
9600 out_trash(); /* trash any pending output */
9601 }
9602 else
9603#endif
9604 {
9605 windgoto((int)Rows - 1, 0);
9606
9607 /*
9608 * Switch terminal mode back now, so messages end up on the "normal"
9609 * screen (if there are two screens).
9610 */
9611 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009612 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 out_flush();
9614 }
9615}
9616
9617/*
9618 * Preserve files and exit.
9619 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009620 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9621 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 */
9623 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009624preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
9626 buf_T *buf;
9627
9628 prepare_to_exit();
9629
Bram Moolenaar4770d092006-01-12 23:22:24 +00009630 /* Setting this will prevent free() calls. That avoids calling free()
9631 * recursively when free() was invoked with a bad pointer. */
9632 really_exiting = TRUE;
9633
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 out_str(IObuff);
9635 screen_start(); /* don't know where cursor is now */
9636 out_flush();
9637
9638 ml_close_notmod(); /* close all not-modified buffers */
9639
Bram Moolenaar29323592016-07-24 22:04:11 +02009640 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 {
9642 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9643 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009644 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 screen_start(); /* don't know where cursor is now */
9646 out_flush();
9647 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9648 break;
9649 }
9650 }
9651
9652 ml_close_all(FALSE); /* close all memfiles, without deleting */
9653
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009654 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655
9656 getout(1);
9657}
9658
9659/*
9660 * return TRUE if "fname" exists.
9661 */
9662 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009663vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009665 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666
9667 if (mch_stat((char *)fname, &st))
9668 return FALSE;
9669 return TRUE;
9670}
9671
9672/*
9673 * Check for CTRL-C pressed, but only once in a while.
9674 * Should be used instead of ui_breakcheck() for functions that check for
9675 * each line in the file. Calling ui_breakcheck() each time takes too much
9676 * time, because it can be a system call.
9677 */
9678
9679#ifndef BREAKCHECK_SKIP
9680# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9681# define BREAKCHECK_SKIP 200
9682# else
9683# define BREAKCHECK_SKIP 32
9684# endif
9685#endif
9686
9687static int breakcheck_count = 0;
9688
9689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009690line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
9692 if (++breakcheck_count >= BREAKCHECK_SKIP)
9693 {
9694 breakcheck_count = 0;
9695 ui_breakcheck();
9696 }
9697}
9698
9699/*
9700 * Like line_breakcheck() but check 10 times less often.
9701 */
9702 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009703fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704{
9705 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9706 {
9707 breakcheck_count = 0;
9708 ui_breakcheck();
9709 }
9710}
9711
9712/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009713 * Invoke expand_wildcards() for one pattern.
9714 * Expand items like "%:h" before the expansion.
9715 * Returns OK or FAIL.
9716 */
9717 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009718expand_wildcards_eval(
9719 char_u **pat, /* pointer to input pattern */
9720 int *num_file, /* resulting number of files */
9721 char_u ***file, /* array of resulting files */
9722 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009723{
9724 int ret = FAIL;
9725 char_u *eval_pat = NULL;
9726 char_u *exp_pat = *pat;
9727 char_u *ignored_msg;
9728 int usedlen;
9729
9730 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9731 {
9732 ++emsg_off;
9733 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9734 NULL, &ignored_msg, NULL);
9735 --emsg_off;
9736 if (eval_pat != NULL)
9737 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9738 }
9739
9740 if (exp_pat != NULL)
9741 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9742
9743 if (eval_pat != NULL)
9744 {
9745 vim_free(exp_pat);
9746 vim_free(eval_pat);
9747 }
9748
9749 return ret;
9750}
9751
9752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9754 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009755 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 */
9757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009758expand_wildcards(
9759 int num_pat, /* number of input patterns */
9760 char_u **pat, /* array of input patterns */
9761 int *num_files, /* resulting number of files */
9762 char_u ***files, /* array of resulting files */
9763 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
9765 int retval;
9766 int i, j;
9767 char_u *p;
9768 int non_suf_match; /* number without matching suffix */
9769
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009770 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771
9772 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009773 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 return retval;
9775
9776#ifdef FEAT_WILDIGN
9777 /*
9778 * Remove names that match 'wildignore'.
9779 */
9780 if (*p_wig)
9781 {
9782 char_u *ffname;
9783
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009784 /* check all files in (*files)[] */
9785 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009787 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (ffname == NULL) /* out of memory */
9789 break;
9790# ifdef VMS
9791 vms_remove_version(ffname);
9792# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009793 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009795 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009796 vim_free((*files)[i]);
9797 for (j = i; j + 1 < *num_files; ++j)
9798 (*files)[j] = (*files)[j + 1];
9799 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 --i;
9801 }
9802 vim_free(ffname);
9803 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009804
9805 /* If the number of matches is now zero, we fail. */
9806 if (*num_files == 0)
9807 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009808 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009809 return FAIL;
9810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 }
9812#endif
9813
9814 /*
9815 * Move the names where 'suffixes' match to the end.
9816 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009817 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 {
9819 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009820 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009822 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 {
9824 /*
9825 * Move the name without matching suffix to the front
9826 * of the list.
9827 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009828 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009830 (*files)[j] = (*files)[j - 1];
9831 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 }
9833 }
9834 }
9835
9836 return retval;
9837}
9838
9839/*
9840 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9841 */
9842 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009843match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
9845 int fnamelen, setsuflen;
9846 char_u *setsuf;
9847#define MAXSUFLEN 30 /* maximum length of a file suffix */
9848 char_u suf_buf[MAXSUFLEN];
9849
9850 fnamelen = (int)STRLEN(fname);
9851 setsuflen = 0;
9852 for (setsuf = p_su; *setsuf; )
9853 {
9854 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009855 if (setsuflen == 0)
9856 {
9857 char_u *tail = gettail(fname);
9858
9859 /* empty entry: match name without a '.' */
9860 if (vim_strchr(tail, '.') == NULL)
9861 {
9862 setsuflen = 1;
9863 break;
9864 }
9865 }
9866 else
9867 {
9868 if (fnamelen >= setsuflen
9869 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9870 (size_t)setsuflen) == 0)
9871 break;
9872 setsuflen = 0;
9873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 }
9875 return (setsuflen != 0);
9876}
9877
9878#if !defined(NO_EXPANDPATH) || defined(PROTO)
9879
9880# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009881static int vim_backtick(char_u *p);
9882static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883# endif
9884
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009885# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886/*
9887 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9888 * it's shared between these systems.
9889 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009890# if defined(PROTO)
9891# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892# else
9893# ifdef __BORLANDC__
9894# define _cdecl _RTLENTRYF
9895# endif
9896# endif
9897
9898/*
9899 * comparison function for qsort in dos_expandpath()
9900 */
9901 static int _cdecl
9902pstrcmp(const void *a, const void *b)
9903{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009904 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009908 * Recursively expand one path component into all matching files and/or
9909 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 * Return the number of matches found.
9911 * "path" has backslashes before chars that are not to be expanded, starting
9912 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009913 * Return the number of matches found.
9914 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 */
9916 static int
9917dos_expandpath(
9918 garray_T *gap,
9919 char_u *path,
9920 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009921 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009922 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009924 char_u *buf;
9925 char_u *path_end;
9926 char_u *p, *s, *e;
9927 int start_len = gap->ga_len;
9928 char_u *pat;
9929 regmatch_T regmatch;
9930 int starts_with_dot;
9931 int matches;
9932 int len;
9933 int starstar = FALSE;
9934 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 WIN32_FIND_DATA fb;
9936 HANDLE hFind = (HANDLE)0;
9937# ifdef FEAT_MBYTE
9938 WIN32_FIND_DATAW wfb;
9939 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009942 int ok;
9943
9944 /* Expanding "**" may take a long time, check for CTRL-C. */
9945 if (stardepth > 0)
9946 {
9947 ui_breakcheck();
9948 if (got_int)
9949 return 0;
9950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009952 /* Make room for file name. When doing encoding conversion the actual
9953 * length may be quite a bit longer, thus use the maximum possible length. */
9954 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 if (buf == NULL)
9956 return 0;
9957
9958 /*
9959 * Find the first part in the path name that contains a wildcard or a ~1.
9960 * Copy it into buf, including the preceding characters.
9961 */
9962 p = buf;
9963 s = buf;
9964 e = NULL;
9965 path_end = path;
9966 while (*path_end != NUL)
9967 {
9968 /* May ignore a wildcard that has a backslash before it; it will
9969 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9970 if (path_end >= path + wildoff && rem_backslash(path_end))
9971 *p++ = *path_end++;
9972 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9973 {
9974 if (e != NULL)
9975 break;
9976 s = p + 1;
9977 }
9978 else if (path_end >= path + wildoff
9979 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9980 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009981# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 if (has_mbyte)
9983 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009984 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 STRNCPY(p, path_end, len);
9986 p += len;
9987 path_end += len;
9988 }
9989 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 *p++ = *path_end++;
9992 }
9993 e = p;
9994 *e = NUL;
9995
9996 /* now we have one wildcard component between s and e */
9997 /* Remove backslashes between "wildoff" and the start of the wildcard
9998 * component. */
9999 for (p = buf + wildoff; p < s; ++p)
10000 if (rem_backslash(p))
10001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010002 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 --e;
10004 --s;
10005 }
10006
Bram Moolenaar231334e2005-07-25 20:46:57 +000010007 /* Check for "**" between "s" and "e". */
10008 for (p = s; p < e; ++p)
10009 if (p[0] == '*' && p[1] == '*')
10010 starstar = TRUE;
10011
Bram Moolenaard82103e2016-01-17 17:04:05 +010010012 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10014 if (pat == NULL)
10015 {
10016 vim_free(buf);
10017 return 0;
10018 }
10019
10020 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010021 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010022 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 regmatch.rm_ic = TRUE; /* Always ignore case */
10024 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010025 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010026 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 vim_free(pat);
10028
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010029 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 {
10031 vim_free(buf);
10032 return 0;
10033 }
10034
10035 /* remember the pattern or file name being looked for */
10036 matchname = vim_strsave(s);
10037
Bram Moolenaar231334e2005-07-25 20:46:57 +000010038 /* If "**" is by itself, this is the first time we encounter it and more
10039 * is following then find matches without any directory. */
10040 if (!didstar && stardepth < 100 && starstar && e - s == 2
10041 && *path_end == '/')
10042 {
10043 STRCPY(s, path_end + 1);
10044 ++stardepth;
10045 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10046 --stardepth;
10047 }
10048
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 /* Scan all files in the directory with "dir/ *.*" */
10050 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051# ifdef FEAT_MBYTE
10052 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10053 {
10054 /* The active codepage differs from 'encoding'. Attempt using the
10055 * wide function. If it fails because it is not implemented fall back
10056 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010057 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 if (wn != NULL)
10059 {
10060 hFind = FindFirstFileW(wn, &wfb);
10061 if (hFind == INVALID_HANDLE_VALUE
10062 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010063 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 }
10065 }
10066
10067 if (wn == NULL)
10068# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010069 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071
10072 while (ok)
10073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074# ifdef FEAT_MBYTE
10075 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010076 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 else
10078# endif
10079 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 /* Ignore entries starting with a dot, unless when asked for. Accept
10081 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010082 if ((p[0] != '.' || starts_with_dot
10083 || ((flags & EW_DODOT)
10084 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010086 || (regmatch.regprog != NULL
10087 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010088 || ((flags & EW_NOTWILD)
10089 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010093
10094 if (starstar && stardepth < 100)
10095 {
10096 /* For "**" in the pattern first go deeper in the tree to
10097 * find matches. */
10098 STRCPY(buf + len, "/**");
10099 STRCPY(buf + len + 3, path_end);
10100 ++stardepth;
10101 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10102 --stardepth;
10103 }
10104
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 STRCPY(buf + len, path_end);
10106 if (mch_has_exp_wildcard(path_end))
10107 {
10108 /* need to expand another component of the path */
10109 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010110 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 }
10112 else
10113 {
10114 /* no more wildcards, check if there is a match */
10115 /* remove backslashes for the remaining components only */
10116 if (*path_end != 0)
10117 backslash_halve(buf + len + 1);
10118 if (mch_getperm(buf) >= 0) /* add existing file */
10119 addfile(gap, buf, flags);
10120 }
10121 }
10122
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123# ifdef FEAT_MBYTE
10124 if (wn != NULL)
10125 {
10126 vim_free(p);
10127 ok = FindNextFileW(hFind, &wfb);
10128 }
10129 else
10130# endif
10131 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
10133 /* If no more matches and no match was used, try expanding the name
10134 * itself. Finds the long name of a short filename. */
10135 if (!ok && matchname != NULL && gap->ga_len == start_len)
10136 {
10137 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 FindClose(hFind);
10139# ifdef FEAT_MBYTE
10140 if (wn != NULL)
10141 {
10142 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010143 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 if (wn != NULL)
10145 hFind = FindFirstFileW(wn, &wfb);
10146 }
10147 if (wn == NULL)
10148# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010149 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010151 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 }
10153 }
10154
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 FindClose(hFind);
10156# ifdef FEAT_MBYTE
10157 vim_free(wn);
10158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010160 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 vim_free(matchname);
10162
10163 matches = gap->ga_len - start_len;
10164 if (matches > 0)
10165 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10166 sizeof(char_u *), pstrcmp);
10167 return matches;
10168}
10169
10170 int
10171mch_expandpath(
10172 garray_T *gap,
10173 char_u *path,
10174 int flags) /* EW_* flags */
10175{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010176 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010178# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179
Bram Moolenaar231334e2005-07-25 20:46:57 +000010180#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10181 || defined(PROTO)
10182/*
10183 * Unix style wildcard expansion code.
10184 * It's here because it's used both for Unix and Mac.
10185 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010186static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010187
10188 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010189pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010190{
10191 return (pathcmp(*(char **)a, *(char **)b, -1));
10192}
10193
10194/*
10195 * Recursively expand one path component into all matching files and/or
10196 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10197 * "path" has backslashes before chars that are not to be expanded, starting
10198 * at "path + wildoff".
10199 * Return the number of matches found.
10200 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10201 */
10202 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010203unix_expandpath(
10204 garray_T *gap,
10205 char_u *path,
10206 int wildoff,
10207 int flags, /* EW_* flags */
10208 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010209{
10210 char_u *buf;
10211 char_u *path_end;
10212 char_u *p, *s, *e;
10213 int start_len = gap->ga_len;
10214 char_u *pat;
10215 regmatch_T regmatch;
10216 int starts_with_dot;
10217 int matches;
10218 int len;
10219 int starstar = FALSE;
10220 static int stardepth = 0; /* depth for "**" expansion */
10221
10222 DIR *dirp;
10223 struct dirent *dp;
10224
10225 /* Expanding "**" may take a long time, check for CTRL-C. */
10226 if (stardepth > 0)
10227 {
10228 ui_breakcheck();
10229 if (got_int)
10230 return 0;
10231 }
10232
10233 /* make room for file name */
10234 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10235 if (buf == NULL)
10236 return 0;
10237
10238 /*
10239 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010240 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010241 * Copy it into "buf", including the preceding characters.
10242 */
10243 p = buf;
10244 s = buf;
10245 e = NULL;
10246 path_end = path;
10247 while (*path_end != NUL)
10248 {
10249 /* May ignore a wildcard that has a backslash before it; it will
10250 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10251 if (path_end >= path + wildoff && rem_backslash(path_end))
10252 *p++ = *path_end++;
10253 else if (*path_end == '/')
10254 {
10255 if (e != NULL)
10256 break;
10257 s = p + 1;
10258 }
10259 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010260 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010261 || (!p_fic && (flags & EW_ICASE)
10262 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010263 e = p;
10264#ifdef FEAT_MBYTE
10265 if (has_mbyte)
10266 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010267 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010268 STRNCPY(p, path_end, len);
10269 p += len;
10270 path_end += len;
10271 }
10272 else
10273#endif
10274 *p++ = *path_end++;
10275 }
10276 e = p;
10277 *e = NUL;
10278
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010279 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010280 /* Remove backslashes between "wildoff" and the start of the wildcard
10281 * component. */
10282 for (p = buf + wildoff; p < s; ++p)
10283 if (rem_backslash(p))
10284 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010285 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010286 --e;
10287 --s;
10288 }
10289
10290 /* Check for "**" between "s" and "e". */
10291 for (p = s; p < e; ++p)
10292 if (p[0] == '*' && p[1] == '*')
10293 starstar = TRUE;
10294
10295 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010296 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010297 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10298 if (pat == NULL)
10299 {
10300 vim_free(buf);
10301 return 0;
10302 }
10303
10304 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010305 if (flags & EW_ICASE)
10306 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10307 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010308 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010309 if (flags & (EW_NOERROR | EW_NOTWILD))
10310 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010311 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010312 if (flags & (EW_NOERROR | EW_NOTWILD))
10313 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010314 vim_free(pat);
10315
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010316 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010317 {
10318 vim_free(buf);
10319 return 0;
10320 }
10321
10322 /* If "**" is by itself, this is the first time we encounter it and more
10323 * is following then find matches without any directory. */
10324 if (!didstar && stardepth < 100 && starstar && e - s == 2
10325 && *path_end == '/')
10326 {
10327 STRCPY(s, path_end + 1);
10328 ++stardepth;
10329 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10330 --stardepth;
10331 }
10332
10333 /* open the directory for scanning */
10334 *s = NUL;
10335 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10336
10337 /* Find all matching entries */
10338 if (dirp != NULL)
10339 {
10340 for (;;)
10341 {
10342 dp = readdir(dirp);
10343 if (dp == NULL)
10344 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010345 if ((dp->d_name[0] != '.' || starts_with_dot
10346 || ((flags & EW_DODOT)
10347 && dp->d_name[1] != NUL
10348 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010349 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10350 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010351 || ((flags & EW_NOTWILD)
10352 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010353 {
10354 STRCPY(s, dp->d_name);
10355 len = STRLEN(buf);
10356
10357 if (starstar && stardepth < 100)
10358 {
10359 /* For "**" in the pattern first go deeper in the tree to
10360 * find matches. */
10361 STRCPY(buf + len, "/**");
10362 STRCPY(buf + len + 3, path_end);
10363 ++stardepth;
10364 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10365 --stardepth;
10366 }
10367
10368 STRCPY(buf + len, path_end);
10369 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10370 {
10371 /* need to expand another component of the path */
10372 /* remove backslashes for the remaining components only */
10373 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10374 }
10375 else
10376 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010377 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010378
Bram Moolenaar231334e2005-07-25 20:46:57 +000010379 /* no more wildcards, check if there is a match */
10380 /* remove backslashes for the remaining components only */
10381 if (*path_end != NUL)
10382 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010383 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010384 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010385 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010386 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010387#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010388 size_t precomp_len = STRLEN(buf)+1;
10389 char_u *precomp_buf =
10390 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010391
Bram Moolenaar231334e2005-07-25 20:46:57 +000010392 if (precomp_buf)
10393 {
10394 mch_memmove(buf, precomp_buf, precomp_len);
10395 vim_free(precomp_buf);
10396 }
10397#endif
10398 addfile(gap, buf, flags);
10399 }
10400 }
10401 }
10402 }
10403
10404 closedir(dirp);
10405 }
10406
10407 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010408 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010409
10410 matches = gap->ga_len - start_len;
10411 if (matches > 0)
10412 qsort(((char_u **)gap->ga_data) + start_len, matches,
10413 sizeof(char_u *), pstrcmp);
10414 return matches;
10415}
10416#endif
10417
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010418#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010419static int find_previous_pathsep(char_u *path, char_u **psep);
10420static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10421static void expand_path_option(char_u *curdir, garray_T *gap);
10422static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10423static void uniquefy_paths(garray_T *gap, char_u *pattern);
10424static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010425
10426/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010427 * Moves "*psep" back to the previous path separator in "path".
10428 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429 */
10430 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010431find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010432{
10433 /* skip the current separator */
10434 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010435 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010436
10437 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010438 while (*psep > path)
10439 {
10440 if (vim_ispathsep(**psep))
10441 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010442 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010443 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010444
10445 return FAIL;
10446}
10447
10448/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010449 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10450 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010451 */
10452 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010453is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010454{
10455 int j;
10456 int candidate_len;
10457 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010458 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010459 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010460
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010461 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010462 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010463 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010464 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010465
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010466 candidate_len = (int)STRLEN(maybe_unique);
10467 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010468 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010469 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010470
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010471 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010472 if (fnamecmp(maybe_unique, rival) == 0
10473 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010474 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010475 }
10476
Bram Moolenaar162bd912010-07-28 22:29:10 +020010477 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010478}
10479
10480/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010481 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010482 * paths are expanded to their equivalent fullpath. This includes the "."
10483 * (relative to current buffer directory) and empty path (relative to current
10484 * directory) notations.
10485 *
10486 * TODO: handle upward search (;) and path limiter (**N) notations by
10487 * expanding each into their equivalent path(s).
10488 */
10489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010490expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010491{
10492 char_u *path_option = *curbuf->b_p_path == NUL
10493 ? p_path : curbuf->b_p_path;
10494 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010495 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010496 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010497
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010498 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010499 return;
10500
10501 while (*path_option != NUL)
10502 {
10503 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10504
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010505 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010506 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010507 /* Relative to current buffer:
10508 * "/path/file" + "." -> "/path/"
10509 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010510 if (curbuf->b_ffname == NULL)
10511 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010512 p = gettail(curbuf->b_ffname);
10513 len = (int)(p - curbuf->b_ffname);
10514 if (len + (int)STRLEN(buf) >= MAXPATHL)
10515 continue;
10516 if (buf[1] == NUL)
10517 buf[len] = NUL;
10518 else
10519 STRMOVE(buf + len, buf + 2);
10520 mch_memmove(buf, curbuf->b_ffname, len);
10521 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010522 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010523 else if (buf[0] == NUL)
10524 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010525 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010526 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010527 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010528 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010529 else if (!mch_isFullName(buf))
10530 {
10531 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010532 len = (int)STRLEN(curdir);
10533 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010534 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010535 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010536 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010537 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010538 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010539 }
10540
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010541 if (ga_grow(gap, 1) == FAIL)
10542 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010543
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010544# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010545 /* Avoid the path ending in a backslash, it fails when a comma is
10546 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010547 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010548 if (buf[len - 1] == '\\')
10549 buf[len - 1] = '/';
10550# endif
10551
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010552 p = vim_strsave(buf);
10553 if (p == NULL)
10554 break;
10555 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010556 }
10557
10558 vim_free(buf);
10559}
10560
10561/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010562 * Returns a pointer to the file or directory name in "fname" that matches the
10563 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010564 *
10565 * path: /foo/bar/baz
10566 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010567 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010568 */
10569 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010570get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010571{
10572 int i;
10573 int maxlen = 0;
10574 char_u **path_part = (char_u **)gap->ga_data;
10575 char_u *cutoff = NULL;
10576
10577 for (i = 0; i < gap->ga_len; i++)
10578 {
10579 int j = 0;
10580
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010581 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010582# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010583 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10584#endif
10585 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010586 j++;
10587 if (j > maxlen)
10588 {
10589 maxlen = j;
10590 cutoff = &fname[j];
10591 }
10592 }
10593
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010594 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010595 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010596 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010597 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010598
10599 return cutoff;
10600}
10601
10602/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010603 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10604 * that they are unique with respect to each other while conserving the part
10605 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010606 */
10607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010608uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010609{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010610 int i;
10611 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010612 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010613 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010614 char_u *pat;
10615 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010616 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010617 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010618 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010619 char_u **in_curdir = NULL;
10620 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010621
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010622 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010623 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010624
10625 /*
10626 * We need to prepend a '*' at the beginning of file_pattern so that the
10627 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010628 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010629 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010630 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010631 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010632 if (file_pattern == NULL)
10633 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010634 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010635 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010636 STRCAT(file_pattern, pattern);
10637 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10638 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010639 if (pat == NULL)
10640 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010641
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010642 regmatch.rm_ic = TRUE; /* always ignore case */
10643 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10644 vim_free(pat);
10645 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010646 return;
10647
Bram Moolenaar162bd912010-07-28 22:29:10 +020010648 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010649 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010650 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010651 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010652
10653 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010654 if (in_curdir == NULL)
10655 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010656
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010657 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010658 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010659 char_u *path = fnames[i];
10660 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010661 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010662 char_u *pathsep_p;
10663 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010664
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010665 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010666 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010667 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010668 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010669 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010670
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010671 /* Shorten the filename while maintaining its uniqueness */
10672 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010673
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010674 /* Don't assume all files can be reached without path when search
10675 * pattern starts with star star slash, so only remove path_cutoff
10676 * when possible. */
10677 if (pattern[0] == '*' && pattern[1] == '*'
10678 && vim_ispathsep_nocolon(pattern[2])
10679 && path_cutoff != NULL
10680 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10681 && is_unique(path_cutoff, gap, i))
10682 {
10683 sort_again = TRUE;
10684 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10685 }
10686 else
10687 {
10688 /* Here all files can be reached without path, so get shortest
10689 * unique path. We start at the end of the path. */
10690 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010691
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010692 while (find_previous_pathsep(path, &pathsep_p))
10693 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10694 && is_unique(pathsep_p + 1, gap, i)
10695 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10696 {
10697 sort_again = TRUE;
10698 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10699 break;
10700 }
10701 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010702
10703 if (mch_isFullName(path))
10704 {
10705 /*
10706 * Last resort: shorten relative to curdir if possible.
10707 * 'possible' means:
10708 * 1. It is under the current directory.
10709 * 2. The result is actually shorter than the original.
10710 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010711 * Before curdir After
10712 * /foo/bar/file.txt /foo/bar ./file.txt
10713 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10714 * /file.txt / /file.txt
10715 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010716 */
10717 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010718 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010719#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010720 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010721 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010722 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010723 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010724 && !vim_ispathsep(*short_name)
10725#endif
10726 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010727 {
10728 STRCPY(path, ".");
10729 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010730 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010731 }
10732 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010733 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010734 }
10735
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010736 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010737 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010738 {
10739 char_u *rel_path;
10740 char_u *path = in_curdir[i];
10741
10742 if (path == NULL)
10743 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010744
10745 /* If the {filename} is not unique, change it to ./{filename}.
10746 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010747 short_name = shorten_fname(path, curdir);
10748 if (short_name == NULL)
10749 short_name = path;
10750 if (is_unique(short_name, gap, i))
10751 {
10752 STRCPY(fnames[i], short_name);
10753 continue;
10754 }
10755
10756 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10757 if (rel_path == NULL)
10758 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010759 STRCPY(rel_path, ".");
10760 add_pathsep(rel_path);
10761 STRCAT(rel_path, short_name);
10762
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010763 vim_free(fnames[i]);
10764 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010765 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010766 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010767 }
10768
Bram Moolenaar162bd912010-07-28 22:29:10 +020010769theend:
10770 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010771 if (in_curdir != NULL)
10772 {
10773 for (i = 0; i < gap->ga_len; i++)
10774 vim_free(in_curdir[i]);
10775 vim_free(in_curdir);
10776 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010777 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010778 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010779
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010780 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010781 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010782}
10783
10784/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010785 * Calls globpath() with 'path' values for the given pattern and stores the
10786 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010787 * Returns the total number of matches.
10788 */
10789 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010790expand_in_path(
10791 garray_T *gap,
10792 char_u *pattern,
10793 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010794{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010795 char_u *curdir;
10796 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010797 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010798 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010799
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010800 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010801 return 0;
10802 mch_dirname(curdir, MAXPATHL);
10803
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010804 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010805 expand_path_option(curdir, &path_ga);
10806 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010807 if (path_ga.ga_len == 0)
10808 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010809
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010810 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010811 ga_clear_strings(&path_ga);
10812 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010813 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010814
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010815 if (flags & EW_ICASE)
10816 glob_flags |= WILD_ICASE;
10817 if (flags & EW_ADDSLASH)
10818 glob_flags |= WILD_ADD_SLASH;
10819 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010820 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010821
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010822 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010823}
10824#endif
10825
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010826#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10827/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010828 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10829 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010830 */
10831 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010832remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010833{
10834 int i;
10835 int j;
10836 char_u **fnames = (char_u **)gap->ga_data;
10837
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010838 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010839 for (i = gap->ga_len - 1; i > 0; --i)
10840 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10841 {
10842 vim_free(fnames[i]);
10843 for (j = i + 1; j < gap->ga_len; ++j)
10844 fnames[j - 1] = fnames[j];
10845 --gap->ga_len;
10846 }
10847}
10848#endif
10849
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010850static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010851
10852/*
10853 * Return TRUE if "p" contains what looks like an environment variable.
10854 * Allowing for escaping.
10855 */
10856 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010857has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010858{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010859 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010860 {
10861 if (*p == '\\' && p[1] != NUL)
10862 ++p;
10863 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010864#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010865 "$%"
10866#else
10867 "$"
10868#endif
10869 , *p) != NULL)
10870 return TRUE;
10871 }
10872 return FALSE;
10873}
10874
10875#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010876static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010877
10878/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010879 * Return TRUE if "p" contains a special wildcard character, one that Vim
10880 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010881 */
10882 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010883has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010884{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010885 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010886 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010887 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010888 if (*p == '\\' && p[1] != NUL)
10889 ++p;
10890 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10891 return TRUE;
10892 }
10893 return FALSE;
10894}
10895#endif
10896
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897/*
10898 * Generic wildcard expansion code.
10899 *
10900 * Characters in "pat" that should not be expanded must be preceded with a
10901 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10902 *
10903 * Return FAIL when no single file was found. In this case "num_file" is not
10904 * set, and "file" may contain an error message.
10905 * Return OK when some files found. "num_file" is set to the number of
10906 * matches, "file" to the array of matches. Call FreeWild() later.
10907 */
10908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010909gen_expand_wildcards(
10910 int num_pat, /* number of input patterns */
10911 char_u **pat, /* array of input patterns */
10912 int *num_file, /* resulting number of files */
10913 char_u ***file, /* array of resulting files */
10914 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915{
10916 int i;
10917 garray_T ga;
10918 char_u *p;
10919 static int recursive = FALSE;
10920 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010921 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010922#if defined(FEAT_SEARCHPATH)
10923 int did_expand_in_path = FALSE;
10924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925
10926 /*
10927 * expand_env() is called to expand things like "~user". If this fails,
10928 * it calls ExpandOne(), which brings us back here. In this case, always
10929 * call the machine specific expansion function, if possible. Otherwise,
10930 * return FAIL.
10931 */
10932 if (recursive)
10933#ifdef SPECIAL_WILDCHAR
10934 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10935#else
10936 return FAIL;
10937#endif
10938
10939#ifdef SPECIAL_WILDCHAR
10940 /*
10941 * If there are any special wildcard characters which we cannot handle
10942 * here, call machine specific function for all the expansion. This
10943 * avoids starting the shell for each argument separately.
10944 * For `=expr` do use the internal function.
10945 */
10946 for (i = 0; i < num_pat; i++)
10947 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010948 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949# ifdef VIM_BACKTICK
10950 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10951# endif
10952 )
10953 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10954 }
10955#endif
10956
10957 recursive = TRUE;
10958
10959 /*
10960 * The matching file names are stored in a growarray. Init it empty.
10961 */
10962 ga_init2(&ga, (int)sizeof(char_u *), 30);
10963
10964 for (i = 0; i < num_pat; ++i)
10965 {
10966 add_pat = -1;
10967 p = pat[i];
10968
10969#ifdef VIM_BACKTICK
10970 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010973 if (add_pat == -1)
10974 retval = FAIL;
10975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 else
10977#endif
10978 {
10979 /*
10980 * First expand environment variables, "~/" and "~user/".
10981 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010982 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010984 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 if (p == NULL)
10986 p = pat[i];
10987#ifdef UNIX
10988 /*
10989 * On Unix, if expand_env() can't expand an environment
10990 * variable, use the shell to do that. Discard previously
10991 * found file names and start all over again.
10992 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010993 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 {
10995 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010996 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010998 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 recursive = FALSE;
11000 return i;
11001 }
11002#endif
11003 }
11004
11005 /*
11006 * If there are wildcards: Expand file names and add each match to
11007 * the list. If there is no match, and EW_NOTFOUND is given, add
11008 * the pattern.
11009 * If there are no wildcards: Add the file name if it exists or
11010 * when EW_NOTFOUND is given.
11011 */
11012 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011013 {
11014#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011015 if ((flags & EW_PATH)
11016 && !mch_isFullName(p)
11017 && !(p[0] == '.'
11018 && (vim_ispathsep(p[1])
11019 || (p[1] == '.' && vim_ispathsep(p[2]))))
11020 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011021 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011022 /* :find completion where 'path' is used.
11023 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011024 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011025 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011026 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011027 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011028 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011029 else
11030#endif
11031 add_pat = mch_expandpath(&ga, p, flags);
11032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 }
11034
11035 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11036 {
11037 char_u *t = backslash_halve_save(p);
11038
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11040 * "vim c:/" work. */
11041 if (flags & EW_NOTFOUND)
11042 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 addfile(&ga, t, flags);
11045 vim_free(t);
11046 }
11047
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011048#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011049 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011050 uniquefy_paths(&ga, p);
11051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 if (p != pat[i])
11053 vim_free(p);
11054 }
11055
11056 *num_file = ga.ga_len;
11057 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11058
11059 recursive = FALSE;
11060
Bram Moolenaar336bd622016-01-17 18:23:58 +010011061 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062}
11063
11064# ifdef VIM_BACKTICK
11065
11066/*
11067 * Return TRUE if we can expand this backtick thing here.
11068 */
11069 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011070vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071{
11072 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11073}
11074
11075/*
11076 * Expand an item in `backticks` by executing it as a command.
11077 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011078 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 */
11080 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011081expand_backtick(
11082 garray_T *gap,
11083 char_u *pat,
11084 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085{
11086 char_u *p;
11087 char_u *cmd;
11088 char_u *buffer;
11089 int cnt = 0;
11090 int i;
11091
11092 /* Create the command: lop off the backticks. */
11093 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11094 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011095 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096
11097#ifdef FEAT_EVAL
11098 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011099 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 else
11101#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011102 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011103 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 vim_free(cmd);
11105 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011106 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
11108 cmd = buffer;
11109 while (*cmd != NUL)
11110 {
11111 cmd = skipwhite(cmd); /* skip over white space */
11112 p = cmd;
11113 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11114 ++p;
11115 /* add an entry if it is not empty */
11116 if (p > cmd)
11117 {
11118 i = *p;
11119 *p = NUL;
11120 addfile(gap, cmd, flags);
11121 *p = i;
11122 ++cnt;
11123 }
11124 cmd = p;
11125 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11126 ++cmd;
11127 }
11128
11129 vim_free(buffer);
11130 return cnt;
11131}
11132# endif /* VIM_BACKTICK */
11133
11134/*
11135 * Add a file to a file list. Accepted flags:
11136 * EW_DIR add directories
11137 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011138 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 * EW_NOTFOUND add even when it doesn't exist
11140 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011141 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 */
11143 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011144addfile(
11145 garray_T *gap,
11146 char_u *f, /* filename */
11147 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148{
11149 char_u *p;
11150 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011151 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011153 /* if the file/dir/link doesn't exist, may not add it */
11154 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011155 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 return;
11157
11158#ifdef FNAME_ILLEGAL
11159 /* if the file/dir contains illegal characters, don't add it */
11160 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11161 return;
11162#endif
11163
11164 isdir = mch_isdir(f);
11165 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11166 return;
11167
Bram Moolenaarb5971142015-03-21 17:32:19 +010011168 /* If the file isn't executable, may not add it. Do accept directories.
11169 * When invoked from expand_shellcmd() do not use $PATH. */
11170 if (!isdir && (flags & EW_EXEC)
11171 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011172 return;
11173
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 /* Make room for another item in the file list. */
11175 if (ga_grow(gap, 1) == FAIL)
11176 return;
11177
11178 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11179 if (p == NULL)
11180 return;
11181
11182 STRCPY(p, f);
11183#ifdef BACKSLASH_IN_FILENAME
11184 slash_adjust(p);
11185#endif
11186 /*
11187 * Append a slash or backslash after directory names if none is present.
11188 */
11189#ifndef DONT_ADD_PATHSEP_TO_DIR
11190 if (isdir && (flags & EW_ADDSLASH))
11191 add_pathsep(p);
11192#endif
11193 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194}
11195#endif /* !NO_EXPANDPATH */
11196
11197#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11198
11199#ifndef SEEK_SET
11200# define SEEK_SET 0
11201#endif
11202#ifndef SEEK_END
11203# define SEEK_END 2
11204#endif
11205
11206/*
11207 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011208 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11209 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 * Returns an allocated string, or NULL for error.
11211 */
11212 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011213get_cmd_output(
11214 char_u *cmd,
11215 char_u *infile, /* optional input file name */
11216 int flags, /* can be SHELL_SILENT */
11217 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218{
11219 char_u *tempname;
11220 char_u *command;
11221 char_u *buffer = NULL;
11222 int len;
11223 int i = 0;
11224 FILE *fd;
11225
11226 if (check_restricted() || check_secure())
11227 return NULL;
11228
11229 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011230 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 {
11232 EMSG(_(e_notmp));
11233 return NULL;
11234 }
11235
11236 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011237 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 if (command == NULL)
11239 goto done;
11240
11241 /*
11242 * Call the shell to execute the command (errors are ignored).
11243 * Don't check timestamps here.
11244 */
11245 ++no_check_timestamps;
11246 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11247 --no_check_timestamps;
11248
11249 vim_free(command);
11250
11251 /*
11252 * read the names from the file into memory
11253 */
11254# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011255 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 fd = mch_fopen((char *)tempname, "r");
11257# else
11258 fd = mch_fopen((char *)tempname, READBIN);
11259# endif
11260
11261 if (fd == NULL)
11262 {
11263 EMSG2(_(e_notopen), tempname);
11264 goto done;
11265 }
11266
11267 fseek(fd, 0L, SEEK_END);
11268 len = ftell(fd); /* get size of temp file */
11269 fseek(fd, 0L, SEEK_SET);
11270
11271 buffer = alloc(len + 1);
11272 if (buffer != NULL)
11273 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11274 fclose(fd);
11275 mch_remove(tempname);
11276 if (buffer == NULL)
11277 goto done;
11278#ifdef VMS
11279 len = i; /* VMS doesn't give us what we asked for... */
11280#endif
11281 if (i != len)
11282 {
11283 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011284 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011286 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011287 {
11288 /* Change NUL into SOH, otherwise the string is truncated. */
11289 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011290 if (buffer[i] == NUL)
11291 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011292
Bram Moolenaar162bd912010-07-28 22:29:10 +020011293 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011294 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011295 else
11296 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297
11298done:
11299 vim_free(tempname);
11300 return buffer;
11301}
11302#endif
11303
11304/*
11305 * Free the list of files returned by expand_wildcards() or other expansion
11306 * functions.
11307 */
11308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011309FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011311 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 while (count--)
11314 vim_free(files[count]);
11315 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316}
11317
11318/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011319 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 * Don't do this when still processing a command or a mapping.
11321 * Don't do this when inside a ":normal" command.
11322 */
11323 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011324goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
11326 return (p_im && stuff_empty() && typebuf_typed());
11327}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011328
11329/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011330 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011331 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11332 * - Remove any argument. E.g., "csh -f" -> "csh".
11333 * But don't allow a space in the path, so that this works:
11334 * "/usr/bin/csh --rcfile ~/.cshrc"
11335 * But don't do that for Windows, it's common to have a space in the path.
11336 */
11337 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011338get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011339{
11340 char_u *p;
11341
11342#ifdef WIN3264
11343 p = gettail(p_sh);
11344 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11345#else
11346 p = skiptowhite(p_sh);
11347 if (*p == NUL)
11348 {
11349 /* No white space, use the tail. */
11350 p = vim_strsave(gettail(p_sh));
11351 }
11352 else
11353 {
11354 char_u *p1, *p2;
11355
11356 /* Find the last path separator before the space. */
11357 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011358 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011359 if (vim_ispathsep(*p2))
11360 p1 = p2 + 1;
11361 p = vim_strnsave(p1, (int)(p - p1));
11362 }
11363#endif
11364 return p;
11365}