blob: 6c77f4b0d2869304579b7f338503bb23b94fc34c [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 Moolenaar071d4272004-06-13 20:20:40 +00004482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 * Our portable version of setenv.
4484 */
4485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004486vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
4488#ifdef HAVE_SETENV
4489 mch_setenv((char *)name, (char *)val, 1);
4490#else
4491 char_u *envbuf;
4492
4493 /*
4494 * Putenv does not copy the string, it has to remain
4495 * valid. The allocated memory will never be freed.
4496 */
4497 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4498 if (envbuf != NULL)
4499 {
4500 sprintf((char *)envbuf, "%s=%s", name, val);
4501 putenv((char *)envbuf);
4502 }
4503#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004504#ifdef FEAT_GETTEXT
4505 /*
4506 * When setting $VIMRUNTIME adjust the directory to find message
4507 * translations to $VIMRUNTIME/lang.
4508 */
4509 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4510 {
4511 char_u *buf = concat_str(val, (char_u *)"/lang");
4512
4513 if (buf != NULL)
4514 {
4515 bindtextdomain(VIMPACKAGE, (char *)buf);
4516 vim_free(buf);
4517 }
4518 }
4519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520}
4521
4522#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4523/*
4524 * Function given to ExpandGeneric() to obtain an environment variable name.
4525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004527get_env_name(
4528 expand_T *xp UNUSED,
4529 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530{
Bram Moolenaard0573012017-10-28 21:11:06 +02004531# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004533 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 */
4535 return NULL;
4536# else
4537# ifndef __WIN32__
4538 /* Borland C++ 5.2 has this in a header file. */
4539 extern char **environ;
4540# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004541# define ENVNAMELEN 100
4542 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 char_u *str;
4544 int n;
4545
4546 str = (char_u *)environ[idx];
4547 if (str == NULL)
4548 return NULL;
4549
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004550 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 if (str[n] == '=' || str[n] == NUL)
4553 break;
4554 name[n] = str[n];
4555 }
4556 name[n] = NUL;
4557 return name;
4558# endif
4559}
Bram Moolenaar24305862012-08-15 14:05:05 +02004560
4561/*
4562 * Find all user names for user completion.
4563 * Done only once and then cached.
4564 */
4565 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004566init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004567{
Bram Moolenaar24305862012-08-15 14:05:05 +02004568 static int lazy_init_done = FALSE;
4569
4570 if (lazy_init_done)
4571 return;
4572
4573 lazy_init_done = TRUE;
4574 ga_init2(&ga_users, sizeof(char_u *), 20);
4575
4576# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4577 {
4578 char_u* user;
4579 struct passwd* pw;
4580
4581 setpwent();
4582 while ((pw = getpwent()) != NULL)
4583 /* pw->pw_name shouldn't be NULL but just in case... */
4584 if (pw->pw_name != NULL)
4585 {
4586 if (ga_grow(&ga_users, 1) == FAIL)
4587 break;
4588 user = vim_strsave((char_u*)pw->pw_name);
4589 if (user == NULL)
4590 break;
4591 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4592 }
4593 endpwent();
4594 }
4595# endif
4596}
4597
4598/*
4599 * Function given to ExpandGeneric() to obtain an user names.
4600 */
4601 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004602get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004603{
4604 init_users();
4605 if (idx < ga_users.ga_len)
4606 return ((char_u **)ga_users.ga_data)[idx];
4607 return NULL;
4608}
4609
4610/*
4611 * Check whether name matches a user name. Return:
4612 * 0 if name does not match any user name.
4613 * 1 if name partially matches the beginning of a user name.
4614 * 2 is name fully matches a user name.
4615 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004616int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004617{
4618 int i;
4619 int n = (int)STRLEN(name);
4620 int result = 0;
4621
4622 init_users();
4623 for (i = 0; i < ga_users.ga_len; i++)
4624 {
4625 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4626 return 2; /* full match */
4627 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4628 result = 1; /* partial match */
4629 }
4630 return result;
4631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
4633
4634/*
4635 * Replace home directory by "~" in each space or comma separated file name in
4636 * 'src'.
4637 * If anything fails (except when out of space) dst equals src.
4638 */
4639 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004640home_replace(
4641 buf_T *buf, /* when not NULL, check for help files */
4642 char_u *src, /* input file name */
4643 char_u *dst, /* where to put the result */
4644 int dstlen, /* maximum length of the result */
4645 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 spaces and commas in the file name. */
4647{
4648 size_t dirlen = 0, envlen = 0;
4649 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004650 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 char_u *p;
4652
4653 if (src == NULL)
4654 {
4655 *dst = NUL;
4656 return;
4657 }
4658
4659 /*
4660 * If the file is a help file, remove the path completely.
4661 */
4662 if (buf != NULL && buf->b_help)
4663 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004664 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 return;
4666 }
4667
4668 /*
4669 * We check both the value of the $HOME environment variable and the
4670 * "real" home directory.
4671 */
4672 if (homedir != NULL)
4673 dirlen = STRLEN(homedir);
4674
4675#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004676 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004678 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4679#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004680#ifdef WIN3264
4681 if (homedir_env == NULL)
4682 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4683#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004684 /* Empty is the same as not set. */
4685 if (homedir_env != NULL && *homedir_env == NUL)
4686 homedir_env = NULL;
4687
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004688#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004689 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004690 {
4691 int usedlen = 0;
4692 int flen;
4693 char_u *fbuf = NULL;
4694
4695 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004696 (void)modify_fname((char_u *)":p", &usedlen,
4697 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004698 flen = (int)STRLEN(homedir_env);
4699 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4700 /* Remove the trailing / that is added to a directory. */
4701 homedir_env[flen - 1] = NUL;
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (homedir_env != NULL)
4706 envlen = STRLEN(homedir_env);
4707
4708 if (!one)
4709 src = skipwhite(src);
4710 while (*src && dstlen > 0)
4711 {
4712 /*
4713 * Here we are at the beginning of a file name.
4714 * First, check to see if the beginning of the file name matches
4715 * $HOME or the "real" home directory. Check that there is a '/'
4716 * after the match (so that if e.g. the file is "/home/pieter/bla",
4717 * and the home directory is "/home/piet", the file does not end up
4718 * as "~er/bla" (which would seem to indicate the file "bla" in user
4719 * er's home directory)).
4720 */
4721 p = homedir;
4722 len = dirlen;
4723 for (;;)
4724 {
4725 if ( len
4726 && fnamencmp(src, p, len) == 0
4727 && (vim_ispathsep(src[len])
4728 || (!one && (src[len] == ',' || src[len] == ' '))
4729 || src[len] == NUL))
4730 {
4731 src += len;
4732 if (--dstlen > 0)
4733 *dst++ = '~';
4734
4735 /*
4736 * If it's just the home directory, add "/".
4737 */
4738 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4739 *dst++ = '/';
4740 break;
4741 }
4742 if (p == homedir_env)
4743 break;
4744 p = homedir_env;
4745 len = envlen;
4746 }
4747
4748 /* if (!one) skip to separator: space or comma */
4749 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4750 *dst++ = *src++;
4751 /* skip separator */
4752 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4753 *dst++ = *src++;
4754 }
4755 /* if (dstlen == 0) out of space, what to do??? */
4756
4757 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004758
4759 if (homedir_env != homedir_env_orig)
4760 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761}
4762
4763/*
4764 * Like home_replace, store the replaced string in allocated memory.
4765 * When something fails, NULL is returned.
4766 */
4767 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004768home_replace_save(
4769 buf_T *buf, /* when not NULL, check for help files */
4770 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 char_u *dst;
4773 unsigned len;
4774
4775 len = 3; /* space for "~/" and trailing NUL */
4776 if (src != NULL) /* just in case */
4777 len += (unsigned)STRLEN(src);
4778 dst = alloc(len);
4779 if (dst != NULL)
4780 home_replace(buf, src, dst, len, TRUE);
4781 return dst;
4782}
4783
4784/*
4785 * Compare two file names and return:
4786 * FPC_SAME if they both exist and are the same file.
4787 * FPC_SAMEX if they both don't exist and have the same file name.
4788 * FPC_DIFF if they both exist and are different files.
4789 * FPC_NOTX if they both don't exist.
4790 * FPC_DIFFX if one of them doesn't exist.
4791 * For the first name environment variables are expanded
4792 */
4793 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004794fullpathcmp(
4795 char_u *s1,
4796 char_u *s2,
4797 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
4799#ifdef UNIX
4800 char_u exp1[MAXPATHL];
4801 char_u full1[MAXPATHL];
4802 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004803 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 int r1, r2;
4805
4806 expand_env(s1, exp1, MAXPATHL);
4807 r1 = mch_stat((char *)exp1, &st1);
4808 r2 = mch_stat((char *)s2, &st2);
4809 if (r1 != 0 && r2 != 0)
4810 {
4811 /* if mch_stat() doesn't work, may compare the names */
4812 if (checkname)
4813 {
4814 if (fnamecmp(exp1, s2) == 0)
4815 return FPC_SAMEX;
4816 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4817 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4818 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4819 return FPC_SAMEX;
4820 }
4821 return FPC_NOTX;
4822 }
4823 if (r1 != 0 || r2 != 0)
4824 return FPC_DIFFX;
4825 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4826 return FPC_SAME;
4827 return FPC_DIFF;
4828#else
4829 char_u *exp1; /* expanded s1 */
4830 char_u *full1; /* full path of s1 */
4831 char_u *full2; /* full path of s2 */
4832 int retval = FPC_DIFF;
4833 int r1, r2;
4834
4835 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4836 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4837 {
4838 full1 = exp1 + MAXPATHL;
4839 full2 = full1 + MAXPATHL;
4840
4841 expand_env(s1, exp1, MAXPATHL);
4842 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4843 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4844
4845 /* If vim_FullName() fails, the file probably doesn't exist. */
4846 if (r1 != OK && r2 != OK)
4847 {
4848 if (checkname && fnamecmp(exp1, s2) == 0)
4849 retval = FPC_SAMEX;
4850 else
4851 retval = FPC_NOTX;
4852 }
4853 else if (r1 != OK || r2 != OK)
4854 retval = FPC_DIFFX;
4855 else if (fnamecmp(full1, full2))
4856 retval = FPC_DIFF;
4857 else
4858 retval = FPC_SAME;
4859 vim_free(exp1);
4860 }
4861 return retval;
4862#endif
4863}
4864
4865/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004866 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004867 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004868 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
4870 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004871gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
4873 char_u *p1, *p2;
4874
4875 if (fname == NULL)
4876 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004877 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004879 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004881 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 return p1;
4884}
4885
Bram Moolenaar31710262010-08-13 13:36:15 +02004886#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004887static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004888
4889/*
4890 * Return the end of the directory name, on the first path
4891 * separator:
4892 * "/path/file", "/path/dir/", "/path//dir", "/file"
4893 * ^ ^ ^ ^
4894 */
4895 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004896gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004897{
4898 char_u *dir_end = fname;
4899 char_u *next_dir_end = fname;
4900 int look_for_sep = TRUE;
4901 char_u *p;
4902
4903 for (p = fname; *p != NUL; )
4904 {
4905 if (vim_ispathsep(*p))
4906 {
4907 if (look_for_sep)
4908 {
4909 next_dir_end = p;
4910 look_for_sep = FALSE;
4911 }
4912 }
4913 else
4914 {
4915 if (!look_for_sep)
4916 dir_end = next_dir_end;
4917 look_for_sep = TRUE;
4918 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004919 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004920 }
4921 return dir_end;
4922}
4923#endif
4924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004926 * Get pointer to tail of "fname", including path separators. Putting a NUL
4927 * here leaves the directory name. Takes care of "c:/" and "//".
4928 * Always returns a valid pointer.
4929 */
4930 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004931gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004932{
4933 char_u *p;
4934 char_u *t;
4935
4936 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4937 t = gettail(fname);
4938 while (t > p && after_pathsep(fname, t))
4939 --t;
4940#ifdef VMS
4941 /* path separator is part of the path */
4942 ++t;
4943#endif
4944 return t;
4945}
4946
4947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 * get the next path component (just after the next path separator).
4949 */
4950 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004951getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
4953 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004954 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (*fname)
4956 ++fname;
4957 return fname;
4958}
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960/*
4961 * Get a pointer to one character past the head of a path name.
4962 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4963 * If there is no head, path is returned.
4964 */
4965 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004966get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 char_u *retval;
4969
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004970#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 /* may skip "c:" */
4972 if (isalpha(path[0]) && path[1] == ':')
4973 retval = path + 2;
4974 else
4975 retval = path;
4976#else
4977# if defined(AMIGA)
4978 /* may skip "label:" */
4979 retval = vim_strchr(path, ':');
4980 if (retval == NULL)
4981 retval = path;
4982# else /* Unix */
4983 retval = path;
4984# endif
4985#endif
4986
4987 while (vim_ispathsep(*retval))
4988 ++retval;
4989
4990 return retval;
4991}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004994 * Return TRUE if 'c' is a path separator.
4995 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 */
4997 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004998vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005000#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005002#else
5003# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005005# else
5006# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5008 return (c == ':' || c == '[' || c == ']' || c == '/'
5009 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005010# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005012# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015}
5016
Bram Moolenaar69c35002013-11-04 02:54:12 +01005017/*
5018 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5019 */
5020 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005021vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005022{
5023 return vim_ispathsep(c)
5024#ifdef BACKSLASH_IN_FILENAME
5025 && c != ':'
5026#endif
5027 ;
5028}
5029
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5031/*
5032 * return TRUE if 'c' is a path list separator.
5033 */
5034 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005035vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
5037#ifdef UNIX
5038 return (c == ':');
5039#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005040 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041#endif
5042}
5043#endif
5044
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005045/*
5046 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5047 * It's done in-place.
5048 */
5049 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005050shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005051{
5052 char_u *tail, *s, *d;
5053 int skip = FALSE;
5054
5055 tail = gettail(str);
5056 d = str;
5057 for (s = str; ; ++s)
5058 {
5059 if (s >= tail) /* copy the whole tail */
5060 {
5061 *d++ = *s;
5062 if (*s == NUL)
5063 break;
5064 }
5065 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5066 {
5067 *d++ = *s;
5068 skip = FALSE;
5069 }
5070 else if (!skip)
5071 {
5072 *d++ = *s; /* copy next char */
5073 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5074 skip = TRUE;
5075# ifdef FEAT_MBYTE
5076 if (has_mbyte)
5077 {
5078 int l = mb_ptr2len(s);
5079
5080 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005081 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005082 }
5083# endif
5084 }
5085 }
5086}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005087
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005088/*
5089 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5090 * Also returns TRUE if there is no directory name.
5091 * "fname" must be writable!.
5092 */
5093 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005094dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005095{
5096 char_u *p;
5097 int c;
5098 int retval;
5099
5100 p = gettail_sep(fname);
5101 if (p == fname)
5102 return TRUE;
5103 c = *p;
5104 *p = NUL;
5105 retval = mch_isdir(fname);
5106 *p = c;
5107 return retval;
5108}
5109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005111 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5112 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 */
5114 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005115vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005117#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005119#else
5120 if (p_fic)
5121 return MB_STRICMP(x, y);
5122 return STRCMP(x, y);
5123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124}
5125
5126 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005127vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005129#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005130 char_u *px = x;
5131 char_u *py = y;
5132 int cx = NUL;
5133 int cy = NUL;
5134
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005135 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005137 cx = PTR2CHAR(px);
5138 cy = PTR2CHAR(py);
5139 if (cx == NUL || cy == NUL
5140 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5141 && !(cx == '/' && cy == '\\')
5142 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005144 len -= MB_PTR2LEN(px);
5145 px += MB_PTR2LEN(px);
5146 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148 if (len == 0)
5149 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005150 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005151#else
5152 if (p_fic)
5153 return MB_STRNICMP(x, y, len);
5154 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158/*
5159 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005160 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 */
5162 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005163concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
5165 char_u *dest;
5166
5167 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5168 if (dest != NULL)
5169 {
5170 STRCPY(dest, fname1);
5171 if (sep)
5172 add_pathsep(dest);
5173 STRCAT(dest, fname2);
5174 }
5175 return dest;
5176}
5177
Bram Moolenaard6754642005-01-17 22:18:45 +00005178/*
5179 * Concatenate two strings and return the result in allocated memory.
5180 * Returns NULL when out of memory.
5181 */
5182 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005183concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005184{
5185 char_u *dest;
5186 size_t l = STRLEN(str1);
5187
5188 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5189 if (dest != NULL)
5190 {
5191 STRCPY(dest, str1);
5192 STRCPY(dest + l, str2);
5193 }
5194 return dest;
5195}
Bram Moolenaard6754642005-01-17 22:18:45 +00005196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197/*
5198 * Add a path separator to a file name, unless it already ends in a path
5199 * separator.
5200 */
5201 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005204 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 STRCAT(p, PATHSEPSTR);
5206}
5207
5208/*
5209 * FullName_save - Make an allocated copy of a full file name.
5210 * Returns NULL when out of memory.
5211 */
5212 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005213FullName_save(
5214 char_u *fname,
5215 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005216 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
5218 char_u *buf;
5219 char_u *new_fname = NULL;
5220
5221 if (fname == NULL)
5222 return NULL;
5223
5224 buf = alloc((unsigned)MAXPATHL);
5225 if (buf != NULL)
5226 {
5227 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5228 new_fname = vim_strsave(buf);
5229 else
5230 new_fname = vim_strsave(fname);
5231 vim_free(buf);
5232 }
5233 return new_fname;
5234}
5235
5236#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5237
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005238static char_u *skip_string(char_u *p);
5239static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005240static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005241static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243/*
5244 * Find the start of a comment, not knowing if we are in a comment right now.
5245 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005246 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005248 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005249ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005250{
5251 return find_start_comment(curbuf->b_ind_maxcomment);
5252}
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005255find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
5257 pos_T *pos;
5258 char_u *line;
5259 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005260 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005262 for (;;)
5263 {
5264 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5265 if (pos == NULL)
5266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005268 /*
5269 * Check if the comment start we found is inside a string.
5270 * If it is then restrict the search to below this line and try again.
5271 */
5272 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005273 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005274 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005275 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005276 break;
5277 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5278 if (cur_maxcomment <= 0)
5279 {
5280 pos = NULL;
5281 break;
5282 }
5283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 return pos;
5285}
5286
5287/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005288 * Find the start of a comment or raw string, not knowing if we are in a
5289 * comment or raw string right now.
5290 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005291 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005292 * Return NULL when not inside a comment or raw string.
5293 * "CORS" -> Comment Or Raw String
5294 */
5295 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005296ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005297{
Bram Moolenaar089af182015-10-07 11:41:49 +02005298 static pos_T comment_pos_copy;
5299 pos_T *comment_pos;
5300 pos_T *rs_pos;
5301
5302 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5303 if (comment_pos != NULL)
5304 {
5305 /* Need to make a copy of the static pos in findmatchlimit(),
5306 * calling find_start_rawstring() may change it. */
5307 comment_pos_copy = *comment_pos;
5308 comment_pos = &comment_pos_copy;
5309 }
5310 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005311
5312 /* If comment_pos is before rs_pos the raw string is inside the comment.
5313 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005314 if (comment_pos == NULL || (rs_pos != NULL
5315 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005316 {
5317 if (is_raw != NULL && rs_pos != NULL)
5318 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005319 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005320 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005321 return comment_pos;
5322}
5323
5324/*
5325 * Find the start of a raw string, not knowing if we are in one right now.
5326 * Search starts at w_cursor.lnum and goes backwards.
5327 * Return NULL when not inside a raw string.
5328 */
5329 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005330find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005331{
5332 pos_T *pos;
5333 char_u *line;
5334 char_u *p;
5335 int cur_maxcomment = ind_maxcomment;
5336
5337 for (;;)
5338 {
5339 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5340 if (pos == NULL)
5341 break;
5342
5343 /*
5344 * Check if the raw string start we found is inside a string.
5345 * If it is then restrict the search to below this line and try again.
5346 */
5347 line = ml_get(pos->lnum);
5348 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5349 p = skip_string(p);
5350 if ((colnr_T)(p - line) <= pos->col)
5351 break;
5352 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5353 if (cur_maxcomment <= 0)
5354 {
5355 pos = NULL;
5356 break;
5357 }
5358 }
5359 return pos;
5360}
5361
5362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 * Skip to the end of a "string" and a 'c' character.
5364 * If there is no string or character, return argument unmodified.
5365 */
5366 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005367skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 int i;
5370
5371 /*
5372 * We loop, because strings may be concatenated: "date""time".
5373 */
5374 for ( ; ; ++p)
5375 {
5376 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5377 {
5378 if (!p[1]) /* ' at end of line */
5379 break;
5380 i = 2;
5381 if (p[1] == '\\') /* '\n' or '\000' */
5382 {
5383 ++i;
5384 while (vim_isdigit(p[i - 1])) /* '\000' */
5385 ++i;
5386 }
5387 if (p[i] == '\'') /* check for trailing ' */
5388 {
5389 p += i;
5390 continue;
5391 }
5392 }
5393 else if (p[0] == '"') /* start of string */
5394 {
5395 for (++p; p[0]; ++p)
5396 {
5397 if (p[0] == '\\' && p[1] != NUL)
5398 ++p;
5399 else if (p[0] == '"') /* end of string */
5400 break;
5401 }
5402 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005403 continue; /* continue for another string */
5404 }
5405 else if (p[0] == 'R' && p[1] == '"')
5406 {
5407 /* Raw string: R"[delim](...)[delim]" */
5408 char_u *delim = p + 2;
5409 char_u *paren = vim_strchr(delim, '(');
5410
5411 if (paren != NULL)
5412 {
5413 size_t delim_len = paren - delim;
5414
5415 for (p += 3; *p; ++p)
5416 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5417 && p[delim_len + 1] == '"')
5418 {
5419 p += delim_len + 1;
5420 break;
5421 }
5422 if (p[0] == '"')
5423 continue; /* continue for another string */
5424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 }
5426 break; /* no string found */
5427 }
5428 if (!*p)
5429 --p; /* backup from NUL */
5430 return p;
5431}
5432#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5433
5434#if defined(FEAT_CINDENT) || defined(PROTO)
5435
5436/*
5437 * Do C or expression indenting on the current line.
5438 */
5439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005440do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442# ifdef FEAT_EVAL
5443 if (*curbuf->b_p_inde != NUL)
5444 fixthisline(get_expr_indent);
5445 else
5446# endif
5447 fixthisline(get_c_indent);
5448}
5449
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005450/* Find result cache for cpp_baseclass */
5451typedef struct {
5452 int found;
5453 lpos_T lpos;
5454} cpp_baseclass_cache_T;
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456/*
5457 * Functions for C-indenting.
5458 * Most of this originally comes from Eric Fischer.
5459 */
5460/*
5461 * Below "XXX" means that this function may unlock the current line.
5462 */
5463
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005464static char_u *cin_skipcomment(char_u *);
5465static int cin_nocode(char_u *);
5466static pos_T *find_line_comment(void);
5467static int cin_has_js_key(char_u *text);
5468static int cin_islabel_skip(char_u **);
5469static int cin_isdefault(char_u *);
5470static char_u *after_label(char_u *l);
5471static int get_indent_nolabel(linenr_T lnum);
5472static int skip_label(linenr_T, char_u **pp);
5473static int cin_first_id_amount(void);
5474static int cin_get_equal_amount(linenr_T lnum);
5475static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005476static int cin_iscomment(char_u *);
5477static int cin_islinecomment(char_u *);
5478static int cin_isterminated(char_u *, int, int);
5479static int cin_isinit(void);
5480static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5481static int cin_isif(char_u *);
5482static int cin_iselse(char_u *);
5483static int cin_isdo(char_u *);
5484static int cin_iswhileofdo(char_u *, linenr_T);
5485static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5486static int cin_iswhileofdo_end(int terminated);
5487static int cin_isbreak(char_u *);
5488static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5489static int get_baseclass_amount(int col);
5490static int cin_ends_in(char_u *, char_u *, char_u *);
5491static int cin_starts_with(char_u *s, char *word);
5492static int cin_skip2pos(pos_T *trypos);
5493static pos_T *find_start_brace(void);
5494static pos_T *find_match_paren(int);
5495static pos_T *find_match_char(int c, int ind_maxparen);
5496static int corr_ind_maxparen(pos_T *startpos);
5497static int find_last_paren(char_u *l, int start, int end);
5498static int find_match(int lookfor, linenr_T ourscope);
5499static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
5501/*
5502 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005503 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 */
5505 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005506cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 while (*s)
5509 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005510 char_u *prev_s = s;
5511
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005513
5514 /* Perl/shell # comment comment continues until eol. Require a space
5515 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005516 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005517 {
5518 s += STRLEN(s);
5519 break;
5520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (*s != '/')
5522 break;
5523 ++s;
5524 if (*s == '/') /* slash-slash comment continues till eol */
5525 {
5526 s += STRLEN(s);
5527 break;
5528 }
5529 if (*s != '*')
5530 break;
5531 for (++s; *s; ++s) /* skip slash-star comment */
5532 if (s[0] == '*' && s[1] == '/')
5533 {
5534 s += 2;
5535 break;
5536 }
5537 }
5538 return s;
5539}
5540
5541/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005542 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 * not considered code.
5544 */
5545 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005546cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 return *cin_skipcomment(s) == NUL;
5549}
5550
5551/*
5552 * Check previous lines for a "//" line comment, skipping over blank lines.
5553 */
5554 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005555find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 static pos_T pos;
5558 char_u *line;
5559 char_u *p;
5560
5561 pos = curwin->w_cursor;
5562 while (--pos.lnum > 0)
5563 {
5564 line = ml_get(pos.lnum);
5565 p = skipwhite(line);
5566 if (cin_islinecomment(p))
5567 {
5568 pos.col = (int)(p - line);
5569 return &pos;
5570 }
5571 if (*p != NUL)
5572 break;
5573 }
5574 return NULL;
5575}
5576
5577/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005578 * Return TRUE if "text" starts with "key:".
5579 */
5580 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005582{
5583 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005584 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005585
5586 if (*s == '\'' || *s == '"')
5587 {
5588 /* can be 'key': or "key": */
5589 quote = *s;
5590 ++s;
5591 }
5592 if (!vim_isIDc(*s)) /* need at least one ID character */
5593 return FALSE;
5594
5595 while (vim_isIDc(*s))
5596 ++s;
5597 if (*s == quote)
5598 ++s;
5599
5600 s = cin_skipcomment(s);
5601
5602 /* "::" is not a label, it's C++ */
5603 return (*s == ':' && s[1] != ':');
5604}
5605
5606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005608 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 */
5610 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005611cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 if (!vim_isIDc(**s)) /* need at least one ID character */
5614 return FALSE;
5615
5616 while (vim_isIDc(**s))
5617 (*s)++;
5618
5619 *s = cin_skipcomment(*s);
5620
5621 /* "::" is not a label, it's C++ */
5622 return (**s == ':' && *++*s != ':');
5623}
5624
5625/*
5626 * Recognize a label: "label:".
5627 * Note: curwin->w_cursor must be where we are looking for the label.
5628 */
5629 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005630cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 char_u *s;
5633
5634 s = cin_skipcomment(ml_get_curline());
5635
5636 /*
5637 * Exclude "default" from labels, since it should be indented
5638 * like a switch label. Same for C++ scope declarations.
5639 */
5640 if (cin_isdefault(s))
5641 return FALSE;
5642 if (cin_isscopedecl(s))
5643 return FALSE;
5644
5645 if (cin_islabel_skip(&s))
5646 {
5647 /*
5648 * Only accept a label if the previous line is terminated or is a case
5649 * label.
5650 */
5651 pos_T cursor_save;
5652 pos_T *trypos;
5653 char_u *line;
5654
5655 cursor_save = curwin->w_cursor;
5656 while (curwin->w_cursor.lnum > 1)
5657 {
5658 --curwin->w_cursor.lnum;
5659
5660 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005661 * If we're in a comment or raw string now, skip to the start of
5662 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 */
5664 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005665 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 curwin->w_cursor = *trypos;
5667
5668 line = ml_get_curline();
5669 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5670 continue;
5671 if (*(line = cin_skipcomment(line)) == NUL)
5672 continue;
5673
5674 curwin->w_cursor = cursor_save;
5675 if (cin_isterminated(line, TRUE, FALSE)
5676 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005677 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 || (cin_islabel_skip(&line) && cin_nocode(line)))
5679 return TRUE;
5680 return FALSE;
5681 }
5682 curwin->w_cursor = cursor_save;
5683 return TRUE; /* label at start of file??? */
5684 }
5685 return FALSE;
5686}
5687
5688/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005689 * Recognize structure initialization and enumerations:
5690 * "[typedef] [static|public|protected|private] enum"
5691 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 */
5693 static int
5694cin_isinit(void)
5695{
5696 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005697 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699 s = cin_skipcomment(ml_get_curline());
5700
Bram Moolenaar75342212013-03-07 13:13:52 +01005701 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 s = cin_skipcomment(s + 7);
5703
Bram Moolenaar75342212013-03-07 13:13:52 +01005704 for (;;)
5705 {
5706 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005707
Bram Moolenaar75342212013-03-07 13:13:52 +01005708 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5709 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005710 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005711 if (cin_starts_with(s, skip[i]))
5712 {
5713 s = cin_skipcomment(s + l);
5714 l = 0;
5715 break;
5716 }
5717 }
5718 if (l != 0)
5719 break;
5720 }
5721
5722 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 return TRUE;
5724
5725 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5726 return TRUE;
5727
5728 return FALSE;
5729}
5730
5731/*
5732 * Recognize a switch label: "case .*:" or "default:".
5733 */
5734 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005735cin_iscase(
5736 char_u *s,
5737 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005740 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
5742 for (s += 4; *s; ++s)
5743 {
5744 s = cin_skipcomment(s);
5745 if (*s == ':')
5746 {
5747 if (s[1] == ':') /* skip over "::" for C++ */
5748 ++s;
5749 else
5750 return TRUE;
5751 }
5752 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005753 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5755 return FALSE; /* stop at comment */
5756 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005757 {
5758 /* JS etc. */
5759 if (strict)
5760 return FALSE; /* stop at string */
5761 else
5762 return TRUE;
5763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 return FALSE;
5766 }
5767
5768 if (cin_isdefault(s))
5769 return TRUE;
5770 return FALSE;
5771}
5772
5773/*
5774 * Recognize a "default" switch label.
5775 */
5776 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005777cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 return (STRNCMP(s, "default", 7) == 0
5780 && *(s = cin_skipcomment(s + 7)) == ':'
5781 && s[1] != ':');
5782}
5783
5784/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005785 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 */
5787 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005788cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 int i;
5791
5792 s = cin_skipcomment(s);
5793 if (STRNCMP(s, "public", 6) == 0)
5794 i = 6;
5795 else if (STRNCMP(s, "protected", 9) == 0)
5796 i = 9;
5797 else if (STRNCMP(s, "private", 7) == 0)
5798 i = 7;
5799 else
5800 return FALSE;
5801 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5802}
5803
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005804/* Maximum number of lines to search back for a "namespace" line. */
5805#define FIND_NAMESPACE_LIM 20
5806
5807/*
5808 * Recognize a "namespace" scope declaration.
5809 */
5810 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005811cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005812{
5813 char_u *p;
5814 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005815 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005816
5817 s = cin_skipcomment(s);
5818 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5819 {
5820 p = cin_skipcomment(skipwhite(s + 9));
5821 while (*p != NUL)
5822 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005823 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005824 {
5825 has_name = TRUE; /* found end of a name */
5826 p = cin_skipcomment(skipwhite(p));
5827 }
5828 else if (*p == '{')
5829 {
5830 break;
5831 }
5832 else if (vim_iswordc(*p))
5833 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005834 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005835 if (has_name)
5836 return FALSE; /* word character after skipping past name */
5837 ++p;
5838 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005839 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5840 {
5841 if (!has_name_start || has_name)
5842 return FALSE;
5843 /* C++ 17 nested namespace */
5844 p += 3;
5845 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005846 else
5847 {
5848 return FALSE;
5849 }
5850 }
5851 return TRUE;
5852 }
5853 return FALSE;
5854}
5855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005857 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5858 */
5859 static int
5860cin_is_cpp_extern_c(char_u *s)
5861{
5862 char_u *p;
5863 int has_string_literal = FALSE;
5864
5865 s = cin_skipcomment(s);
5866 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5867 {
5868 p = cin_skipcomment(skipwhite(s + 6));
5869 while (*p != NUL)
5870 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005871 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005872 {
5873 p = cin_skipcomment(skipwhite(p));
5874 }
5875 else if (*p == '{')
5876 {
5877 break;
5878 }
5879 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5880 {
5881 if (has_string_literal)
5882 return FALSE;
5883 has_string_literal = TRUE;
5884 p += 3;
5885 }
5886 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5887 && p[4] == '"')
5888 {
5889 if (has_string_literal)
5890 return FALSE;
5891 has_string_literal = TRUE;
5892 p += 5;
5893 }
5894 else
5895 {
5896 return FALSE;
5897 }
5898 }
5899 return has_string_literal ? TRUE : FALSE;
5900 }
5901 return FALSE;
5902}
5903
5904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 * Return a pointer to the first non-empty non-comment character after a ':'.
5906 * Return NULL if not found.
5907 * case 234: a = b;
5908 * ^
5909 */
5910 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005911after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 for ( ; *l; ++l)
5914 {
5915 if (*l == ':')
5916 {
5917 if (l[1] == ':') /* skip over "::" for C++ */
5918 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005919 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 break;
5921 }
5922 else if (*l == '\'' && l[1] && l[2] == '\'')
5923 l += 2; /* skip over 'x' */
5924 }
5925 if (*l == NUL)
5926 return NULL;
5927 l = cin_skipcomment(l + 1);
5928 if (*l == NUL)
5929 return NULL;
5930 return l;
5931}
5932
5933/*
5934 * Get indent of line "lnum", skipping a label.
5935 * Return 0 if there is nothing after the label.
5936 */
5937 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005938get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 char_u *l;
5941 pos_T fp;
5942 colnr_T col;
5943 char_u *p;
5944
5945 l = ml_get(lnum);
5946 p = after_label(l);
5947 if (p == NULL)
5948 return 0;
5949
5950 fp.col = (colnr_T)(p - l);
5951 fp.lnum = lnum;
5952 getvcol(curwin, &fp, &col, NULL, NULL);
5953 return (int)col;
5954}
5955
5956/*
5957 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005958 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 * label: if (asdf && asdfasdf)
5960 * ^
5961 */
5962 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005963skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964{
5965 char_u *l;
5966 int amount;
5967 pos_T cursor_save;
5968
5969 cursor_save = curwin->w_cursor;
5970 curwin->w_cursor.lnum = lnum;
5971 l = ml_get_curline();
5972 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005973 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
5975 amount = get_indent_nolabel(lnum);
5976 l = after_label(ml_get_curline());
5977 if (l == NULL) /* just in case */
5978 l = ml_get_curline();
5979 }
5980 else
5981 {
5982 amount = get_indent();
5983 l = ml_get_curline();
5984 }
5985 *pp = l;
5986
5987 curwin->w_cursor = cursor_save;
5988 return amount;
5989}
5990
5991/*
5992 * Return the indent of the first variable name after a type in a declaration.
5993 * int a, indent of "a"
5994 * static struct foo b, indent of "b"
5995 * enum bla c, indent of "c"
5996 * Returns zero when it doesn't look like a declaration.
5997 */
5998 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005999cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 char_u *line, *p, *s;
6002 int len;
6003 pos_T fp;
6004 colnr_T col;
6005
6006 line = ml_get_curline();
6007 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006008 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6010 {
6011 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006012 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 }
6014 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6015 p = skipwhite(p + 6);
6016 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6017 p = skipwhite(p + 4);
6018 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6019 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6020 {
6021 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006022 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6023 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6024 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6025 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 p = s;
6027 }
6028 for (len = 0; vim_isIDc(p[len]); ++len)
6029 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006030 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 return 0;
6032
6033 p = skipwhite(p + len);
6034 fp.lnum = curwin->w_cursor.lnum;
6035 fp.col = (colnr_T)(p - line);
6036 getvcol(curwin, &fp, &col, NULL, NULL);
6037 return (int)col;
6038}
6039
6040/*
6041 * Return the indent of the first non-blank after an equal sign.
6042 * char *foo = "here";
6043 * Return zero if no (useful) equal sign found.
6044 * Return -1 if the line above "lnum" ends in a backslash.
6045 * foo = "asdf\
6046 * asdf\
6047 * here";
6048 */
6049 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006050cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 char_u *line;
6053 char_u *s;
6054 colnr_T col;
6055 pos_T fp;
6056
6057 if (lnum > 1)
6058 {
6059 line = ml_get(lnum - 1);
6060 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6061 return -1;
6062 }
6063
6064 line = s = ml_get(lnum);
6065 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6066 {
6067 if (cin_iscomment(s)) /* ignore comments */
6068 s = cin_skipcomment(s);
6069 else
6070 ++s;
6071 }
6072 if (*s != '=')
6073 return 0;
6074
6075 s = skipwhite(s + 1);
6076 if (cin_nocode(s))
6077 return 0;
6078
6079 if (*s == '"') /* nice alignment for continued strings */
6080 ++s;
6081
6082 fp.lnum = lnum;
6083 fp.col = (colnr_T)(s - line);
6084 getvcol(curwin, &fp, &col, NULL, NULL);
6085 return (int)col;
6086}
6087
6088/*
6089 * Recognize a preprocessor statement: Any line that starts with '#'.
6090 */
6091 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006092cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006094 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 return TRUE;
6096 return FALSE;
6097}
6098
6099/*
6100 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6101 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6102 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006103 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 */
6105 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006106cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 char_u *line = *pp;
6109 linenr_T lnum = *lnump;
6110 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006111 int candidate_amount = *amount;
6112
6113 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6114 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006116 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
6118 if (cin_ispreproc(line))
6119 {
6120 retval = TRUE;
6121 *lnump = lnum;
6122 break;
6123 }
6124 if (lnum == 1)
6125 break;
6126 line = ml_get(--lnum);
6127 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6128 break;
6129 }
6130
6131 if (lnum != *lnump)
6132 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006133 if (retval)
6134 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 return retval;
6136}
6137
6138/*
6139 * Recognize the start of a C or C++ comment.
6140 */
6141 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006142cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6145}
6146
6147/*
6148 * Recognize the start of a "//" comment.
6149 */
6150 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006151cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 return (p[0] == '/' && p[1] == '/');
6154}
6155
6156/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006157 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6158 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006160 * If a line begins with an "else", only consider it terminated if no unmatched
6161 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 * Return the character terminating the line (ending char's have precedence if
6163 * both apply in order to determine initializations).
6164 */
6165 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006166cin_isterminated(
6167 char_u *s,
6168 int incl_open, /* include '{' at the end as terminator */
6169 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006171 char_u found_start = 0;
6172 unsigned n_open = 0;
6173 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
6175 s = cin_skipcomment(s);
6176
6177 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6178 found_start = *s;
6179
Bram Moolenaar496f9512011-05-19 16:35:09 +02006180 if (!found_start)
6181 is_else = cin_iselse(s);
6182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 while (*s)
6184 {
6185 /* skip over comments, "" strings and 'c'haracters */
6186 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006187 if (*s == '}' && n_open > 0)
6188 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006189 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006190 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 && cin_nocode(s + 1))
6192 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006193 else if (*s == '{')
6194 {
6195 if (incl_open && cin_nocode(s + 1))
6196 return *s;
6197 else
6198 ++n_open;
6199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 if (*s)
6202 s++;
6203 }
6204 return found_start;
6205}
6206
6207/*
6208 * Recognize the basic picture of a function declaration -- it needs to
6209 * have an open paren somewhere and a close paren at the end of the line and
6210 * no semicolons anywhere.
6211 * When a line ends in a comma we continue looking in the next line.
6212 * "sp" points to a string with the line. When looking at other lines it must
6213 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006214 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006215 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 */
6217 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006218cin_isfuncdecl(
6219 char_u **sp,
6220 linenr_T first_lnum,
6221 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 char_u *s;
6224 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006225 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006227 pos_T *trypos;
6228 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
6230 if (sp == NULL)
6231 s = ml_get(lnum);
6232 else
6233 s = *sp;
6234
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006235 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006236 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006237 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006238 {
6239 lnum = trypos->lnum;
6240 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006241 {
6242 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006243 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006244 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006245
6246 s = ml_get(lnum);
6247 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006248 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006249
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006250 /* Ignore line starting with #. */
6251 if (cin_ispreproc(s))
6252 return FALSE;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6255 {
6256 if (cin_iscomment(s)) /* ignore comments */
6257 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006258 else if (*s == ':')
6259 {
6260 if (*(s + 1) == ':')
6261 s += 2;
6262 else
6263 /* To avoid a mistake in the following situation:
6264 * A::A(int a, int b)
6265 * : a(0) // <--not a function decl
6266 * , b(0)
6267 * {...
6268 */
6269 return FALSE;
6270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 else
6272 ++s;
6273 }
6274 if (*s != '(')
6275 return FALSE; /* ';', ' or " before any () or no '(' */
6276
6277 while (*s && *s != ';' && *s != '\'' && *s != '"')
6278 {
6279 if (*s == ')' && cin_nocode(s + 1))
6280 {
6281 /* ')' at the end: may have found a match
6282 * Check for he previous line not to end in a backslash:
6283 * #if defined(x) && \
6284 * defined(y)
6285 */
6286 lnum = first_lnum - 1;
6287 s = ml_get(lnum);
6288 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6289 retval = TRUE;
6290 goto done;
6291 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006292 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006294 int comma = (*s == ',');
6295
6296 /* ',' at the end: continue looking in the next line.
6297 * At the end: check for ',' in the next line, for this style:
6298 * func(arg1
6299 * , arg2) */
6300 for (;;)
6301 {
6302 if (lnum >= curbuf->b_ml.ml_line_count)
6303 break;
6304 s = ml_get(++lnum);
6305 if (!cin_ispreproc(s))
6306 break;
6307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (lnum >= curbuf->b_ml.ml_line_count)
6309 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006310 /* Require a comma at end of the line or a comma or ')' at the
6311 * start of next line. */
6312 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006313 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006314 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006315 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317 else if (cin_iscomment(s)) /* ignore comments */
6318 s = cin_skipcomment(s);
6319 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006322 just_started = FALSE;
6323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325
6326done:
6327 if (lnum != first_lnum && sp != NULL)
6328 *sp = ml_get(first_lnum);
6329
6330 return retval;
6331}
6332
6333 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006334cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006336 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337}
6338
6339 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006340cin_iselse(
6341 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 if (*p == '}') /* accept "} else" */
6344 p = cin_skipcomment(p + 1);
6345 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6346}
6347
6348 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006349cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6352}
6353
6354/*
6355 * Check if this is a "while" that should have a matching "do".
6356 * We only accept a "while (condition) ;", with only white space between the
6357 * ')' and ';'. The condition may be spread over several lines.
6358 */
6359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006360cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 pos_T cursor_save;
6363 pos_T *trypos;
6364 int retval = FALSE;
6365
6366 p = cin_skipcomment(p);
6367 if (*p == '}') /* accept "} while (cond);" */
6368 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006369 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 {
6371 cursor_save = curwin->w_cursor;
6372 curwin->w_cursor.lnum = lnum;
6373 curwin->w_cursor.col = 0;
6374 p = ml_get_curline();
6375 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6376 {
6377 ++p;
6378 ++curwin->w_cursor.col;
6379 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006380 if ((trypos = findmatchlimit(NULL, 0, 0,
6381 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6383 retval = TRUE;
6384 curwin->w_cursor = cursor_save;
6385 }
6386 return retval;
6387}
6388
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006389/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006390 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006391 * Return 0 if there is none.
6392 * Otherwise return !0 and update "*poffset" to point to the place where the
6393 * string was found.
6394 */
6395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006396cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006397{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006398 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006399
6400 if (offset-- < 2)
6401 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006402 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006403 --offset;
6404
6405 offset -= 1;
6406 if (!STRNCMP(line + offset, "if", 2))
6407 goto probablyFound;
6408
6409 if (offset >= 1)
6410 {
6411 offset -= 1;
6412 if (!STRNCMP(line + offset, "for", 3))
6413 goto probablyFound;
6414
6415 if (offset >= 2)
6416 {
6417 offset -= 2;
6418 if (!STRNCMP(line + offset, "while", 5))
6419 goto probablyFound;
6420 }
6421 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006422 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006423
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006424probablyFound:
6425 if (!offset || !vim_isIDc(line[offset - 1]))
6426 {
6427 *poffset = offset;
6428 return 1;
6429 }
6430 return 0;
6431}
6432
6433/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006434 * Return TRUE if we are at the end of a do-while.
6435 * do
6436 * nothing;
6437 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006438 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006439 * Adjust the cursor to the line with "while".
6440 */
6441 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006442cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006443{
6444 char_u *line;
6445 char_u *p;
6446 char_u *s;
6447 pos_T *trypos;
6448 int i;
6449
6450 if (terminated != ';') /* there must be a ';' at the end */
6451 return FALSE;
6452
6453 p = line = ml_get_curline();
6454 while (*p != NUL)
6455 {
6456 p = cin_skipcomment(p);
6457 if (*p == ')')
6458 {
6459 s = skipwhite(p + 1);
6460 if (*s == ';' && cin_nocode(s + 1))
6461 {
6462 /* Found ");" at end of the line, now check there is "while"
6463 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006464 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006465 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006466 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006467 if (trypos != NULL)
6468 {
6469 s = cin_skipcomment(ml_get(trypos->lnum));
6470 if (*s == '}') /* accept "} while (cond);" */
6471 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006472 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006473 {
6474 curwin->w_cursor.lnum = trypos->lnum;
6475 return TRUE;
6476 }
6477 }
6478
6479 /* Searching may have made "line" invalid, get it again. */
6480 line = ml_get_curline();
6481 p = line + i;
6482 }
6483 }
6484 if (*p != NUL)
6485 ++p;
6486 }
6487 return FALSE;
6488}
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006491cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6494}
6495
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006496/*
6497 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 * constructor-initialization. eg:
6499 *
6500 * class MyClass :
6501 * baseClass <-- here
6502 * class MyClass : public baseClass,
6503 * anotherBaseClass <-- here (should probably lineup ??)
6504 * MyClass::MyClass(...) :
6505 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006506 *
6507 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 */
6509 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006510cin_is_cpp_baseclass(
6511 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006513 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 char_u *s;
6515 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006516 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006517 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006519 if (pos->lnum <= lnum)
6520 return cached->found; /* Use the cached result */
6521
6522 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006524 s = skipwhite(line);
6525 if (*s == '#') /* skip #define FOO x ? (x) : x */
6526 return FALSE;
6527 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (*s == NUL)
6529 return FALSE;
6530
6531 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6532
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006533 /* Search for a line starting with '#', empty, ending in ';' or containing
6534 * '{' or '}' and start below it. This handles the following situations:
6535 * a = cond ?
6536 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006537 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006538 * func::foo()
6539 * : something
6540 * {}
6541 * Foo::Foo (int one, int two)
6542 * : something(4),
6543 * somethingelse(3)
6544 * {}
6545 */
6546 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006548 line = ml_get(lnum - 1);
6549 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006550 if (*s == '#' || *s == NUL)
6551 break;
6552 while (*s != NUL)
6553 {
6554 s = cin_skipcomment(s);
6555 if (*s == '{' || *s == '}'
6556 || (*s == ';' && cin_nocode(s + 1)))
6557 break;
6558 if (*s != NUL)
6559 ++s;
6560 }
6561 if (*s != NUL)
6562 break;
6563 --lnum;
6564 }
6565
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006566 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006567 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006568 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006569 for (;;)
6570 {
6571 if (*s == NUL)
6572 {
6573 if (lnum == curwin->w_cursor.lnum)
6574 break;
6575 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006576 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006577 s = line;
6578 }
6579 if (s == line)
6580 {
6581 /* don't recognize "case (foo):" as a baseclass */
6582 if (cin_iscase(s, FALSE))
6583 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006584 s = cin_skipcomment(line);
6585 if (*s == NUL)
6586 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006587 }
6588
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006589 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006590 s = skip_string(s) + 1;
6591 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 {
6593 if (s[1] == ':')
6594 {
6595 /* skip double colon. It can't be a constructor
6596 * initialization any more */
6597 lookfor_ctor_init = FALSE;
6598 s = cin_skipcomment(s + 2);
6599 }
6600 else if (lookfor_ctor_init || class_or_struct)
6601 {
6602 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006603 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 cpp_base_class = TRUE;
6605 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006606 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 s = cin_skipcomment(s + 1);
6608 }
6609 else
6610 s = cin_skipcomment(s + 1);
6611 }
6612 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6613 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6614 {
6615 class_or_struct = TRUE;
6616 lookfor_ctor_init = FALSE;
6617
6618 if (*s == 'c')
6619 s = cin_skipcomment(s + 5);
6620 else
6621 s = cin_skipcomment(s + 6);
6622 }
6623 else
6624 {
6625 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6626 {
6627 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6628 }
6629 else if (s[0] == ')')
6630 {
6631 /* Constructor-initialization is assumed if we come across
6632 * something like "):" */
6633 class_or_struct = FALSE;
6634 lookfor_ctor_init = TRUE;
6635 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006636 else if (s[0] == '?')
6637 {
6638 /* Avoid seeing '() :' after '?' as constructor init. */
6639 return FALSE;
6640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 else if (!vim_isIDc(s[0]))
6642 {
6643 /* if it is not an identifier, we are wrong */
6644 class_or_struct = FALSE;
6645 lookfor_ctor_init = FALSE;
6646 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006647 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
6649 /* it can't be a constructor-initialization any more */
6650 lookfor_ctor_init = FALSE;
6651
6652 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006653 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006654 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 }
6656
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006657 /* When the line ends in a comma don't align with it. */
6658 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006659 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 s = cin_skipcomment(s + 1);
6662 }
6663 }
6664
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006665 cached->found = cpp_base_class;
6666 if (cpp_base_class)
6667 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 return cpp_base_class;
6669}
6670
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006671 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006672get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006673{
6674 int amount;
6675 colnr_T vcol;
6676 pos_T *trypos;
6677
6678 if (col == 0)
6679 {
6680 amount = get_indent();
6681 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006682 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006683 amount = get_indent_lnum(trypos->lnum); /* XXX */
6684 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006685 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006686 }
6687 else
6688 {
6689 curwin->w_cursor.col = col;
6690 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6691 amount = (int)vcol;
6692 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006693 if (amount < curbuf->b_ind_cpp_baseclass)
6694 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006695 return amount;
6696}
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698/*
6699 * Return TRUE if string "s" ends with the string "find", possibly followed by
6700 * white space and comments. Skip strings and comments.
6701 * Ignore "ignore" after "find" if it's not NULL.
6702 */
6703 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006704cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 char_u *p = s;
6707 char_u *r;
6708 int len = (int)STRLEN(find);
6709
6710 while (*p != NUL)
6711 {
6712 p = cin_skipcomment(p);
6713 if (STRNCMP(p, find, len) == 0)
6714 {
6715 r = skipwhite(p + len);
6716 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6717 r = skipwhite(r + STRLEN(ignore));
6718 if (cin_nocode(r))
6719 return TRUE;
6720 }
6721 if (*p != NUL)
6722 ++p;
6723 }
6724 return FALSE;
6725}
6726
6727/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006728 * Return TRUE when "s" starts with "word" and then a non-ID character.
6729 */
6730 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006731cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006732{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006733 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006734
6735 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6736}
6737
6738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 * Skip strings, chars and comments until at or past "trypos".
6740 * Return the column found.
6741 */
6742 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006743cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 char_u *line;
6746 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006747 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749 p = line = ml_get(trypos->lnum);
6750 while (*p && (colnr_T)(p - line) < trypos->col)
6751 {
6752 if (cin_iscomment(p))
6753 p = cin_skipcomment(p);
6754 else
6755 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006756 new_p = skip_string(p);
6757 if (new_p == p)
6758 ++p;
6759 else
6760 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
6762 }
6763 return (int)(p - line);
6764}
6765
6766/*
6767 * Find the '{' at the start of the block we are in.
6768 * Return NULL if no match found.
6769 * Ignore a '{' that is in a comment, makes indenting the next three lines
6770 * work. */
6771/* foo() */
6772/* { */
6773/* } */
6774
6775 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006776find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 pos_T cursor_save;
6779 pos_T *trypos;
6780 pos_T *pos;
6781 static pos_T pos_copy;
6782
6783 cursor_save = curwin->w_cursor;
6784 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6785 {
6786 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6787 trypos = &pos_copy;
6788 curwin->w_cursor = *trypos;
6789 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006790 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006792 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 break;
6794 if (pos != NULL)
6795 curwin->w_cursor.lnum = pos->lnum;
6796 }
6797 curwin->w_cursor = cursor_save;
6798 return trypos;
6799}
6800
6801/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006802 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006803 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 */
6805 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006806find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006808 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006809}
6810
6811 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006812find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006813{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 pos_T cursor_save;
6815 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006816 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006817 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006820 ind_maxp_wk = ind_maxparen;
6821retry:
6822 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {
6824 /* check if the ( is in a // comment */
6825 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006826 {
6827 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6828 if (ind_maxp_wk > 0)
6829 {
6830 curwin->w_cursor = *trypos;
6831 curwin->w_cursor.col = 0; /* XXX */
6832 goto retry;
6833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 else
6837 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006838 pos_T *trypos_wk;
6839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6841 trypos = &pos_copy;
6842 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006843 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006844 {
6845 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6846 - trypos_wk->lnum);
6847 if (ind_maxp_wk > 0)
6848 {
6849 curwin->w_cursor = *trypos_wk;
6850 goto retry;
6851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855 }
6856 curwin->w_cursor = cursor_save;
6857 return trypos;
6858}
6859
6860/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006861 * Find the matching '(', ignoring it if it is in a comment or before an
6862 * unmatched {.
6863 * Return NULL if no match found.
6864 */
6865 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006866find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006867{
6868 pos_T *trypos = find_match_paren(ind_maxparen);
6869
6870 if (trypos != NULL)
6871 {
6872 pos_T *tryposBrace = find_start_brace();
6873
6874 /* If both an unmatched '(' and '{' is found. Ignore the '('
6875 * position if the '{' is further down. */
6876 if (tryposBrace != NULL
6877 && (trypos->lnum != tryposBrace->lnum
6878 ? trypos->lnum < tryposBrace->lnum
6879 : trypos->col < tryposBrace->col))
6880 trypos = NULL;
6881 }
6882 return trypos;
6883}
6884
6885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 * Return ind_maxparen corrected for the difference in line number between the
6887 * cursor position and "startpos". This makes sure that searching for a
6888 * matching paren above the cursor line doesn't find a match because of
6889 * looking a few lines further.
6890 */
6891 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006892corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6895
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006896 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6897 return curbuf->b_ind_maxparen - (int)n;
6898 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899}
6900
6901/*
6902 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006903 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 */
6905 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006906find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
6908 int i;
6909 int retval = FALSE;
6910 int open_count = 0;
6911
6912 curwin->w_cursor.col = 0; /* default is start of line */
6913
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006914 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
6916 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6917 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6918 if (l[i] == start)
6919 ++open_count;
6920 else if (l[i] == end)
6921 {
6922 if (open_count > 0)
6923 --open_count;
6924 else
6925 {
6926 curwin->w_cursor.col = i;
6927 retval = TRUE;
6928 }
6929 }
6930 }
6931 return retval;
6932}
6933
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006934/*
6935 * Parse 'cinoptions' and set the values in "curbuf".
6936 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6937 */
6938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006939parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006940{
6941 char_u *p;
6942 char_u *l;
6943 char_u *digits;
6944 int n;
6945 int divider;
6946 int fraction = 0;
6947 int sw = (int)get_sw_value(buf);
6948
6949 /*
6950 * Set the default values.
6951 */
6952 /* Spaces from a block's opening brace the prevailing indent for that
6953 * block should be. */
6954 buf->b_ind_level = sw;
6955
6956 /* Spaces from the edge of the line an open brace that's at the end of a
6957 * line is imagined to be. */
6958 buf->b_ind_open_imag = 0;
6959
6960 /* Spaces from the prevailing indent for a line that is not preceded by
6961 * an opening brace. */
6962 buf->b_ind_no_brace = 0;
6963
6964 /* Column where the first { of a function should be located }. */
6965 buf->b_ind_first_open = 0;
6966
6967 /* Spaces from the prevailing indent a leftmost open brace should be
6968 * located. */
6969 buf->b_ind_open_extra = 0;
6970
6971 /* Spaces from the matching open brace (real location for one at the left
6972 * edge; imaginary location from one that ends a line) the matching close
6973 * brace should be located. */
6974 buf->b_ind_close_extra = 0;
6975
6976 /* Spaces from the edge of the line an open brace sitting in the leftmost
6977 * column is imagined to be. */
6978 buf->b_ind_open_left_imag = 0;
6979
6980 /* Spaces jump labels should be shifted to the left if N is non-negative,
6981 * otherwise the jump label will be put to column 1. */
6982 buf->b_ind_jump_label = -1;
6983
6984 /* Spaces from the switch() indent a "case xx" label should be located. */
6985 buf->b_ind_case = sw;
6986
6987 /* Spaces from the "case xx:" code after a switch() should be located. */
6988 buf->b_ind_case_code = sw;
6989
6990 /* Lineup break at end of case in switch() with case label. */
6991 buf->b_ind_case_break = 0;
6992
6993 /* Spaces from the class declaration indent a scope declaration label
6994 * should be located. */
6995 buf->b_ind_scopedecl = sw;
6996
6997 /* Spaces from the scope declaration label code should be located. */
6998 buf->b_ind_scopedecl_code = sw;
6999
7000 /* Amount K&R-style parameters should be indented. */
7001 buf->b_ind_param = sw;
7002
7003 /* Amount a function type spec should be indented. */
7004 buf->b_ind_func_type = sw;
7005
7006 /* Amount a cpp base class declaration or constructor initialization
7007 * should be indented. */
7008 buf->b_ind_cpp_baseclass = sw;
7009
7010 /* additional spaces beyond the prevailing indent a continuation line
7011 * should be located. */
7012 buf->b_ind_continuation = sw;
7013
7014 /* Spaces from the indent of the line with an unclosed parentheses. */
7015 buf->b_ind_unclosed = sw * 2;
7016
7017 /* Spaces from the indent of the line with an unclosed parentheses, which
7018 * itself is also unclosed. */
7019 buf->b_ind_unclosed2 = sw;
7020
7021 /* Suppress ignoring spaces from the indent of a line starting with an
7022 * unclosed parentheses. */
7023 buf->b_ind_unclosed_noignore = 0;
7024
7025 /* If the opening paren is the last nonwhite character on the line, and
7026 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7027 * context (for very long lines). */
7028 buf->b_ind_unclosed_wrapped = 0;
7029
7030 /* Suppress ignoring white space when lining up with the character after
7031 * an unclosed parentheses. */
7032 buf->b_ind_unclosed_whiteok = 0;
7033
7034 /* Indent a closing parentheses under the line start of the matching
7035 * opening parentheses. */
7036 buf->b_ind_matching_paren = 0;
7037
7038 /* Indent a closing parentheses under the previous line. */
7039 buf->b_ind_paren_prev = 0;
7040
7041 /* Extra indent for comments. */
7042 buf->b_ind_comment = 0;
7043
7044 /* Spaces from the comment opener when there is nothing after it. */
7045 buf->b_ind_in_comment = 3;
7046
7047 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7048 * after the comment opener. */
7049 buf->b_ind_in_comment2 = 0;
7050
7051 /* Max lines to search for an open paren. */
7052 buf->b_ind_maxparen = 20;
7053
7054 /* Max lines to search for an open comment. */
7055 buf->b_ind_maxcomment = 70;
7056
7057 /* Handle braces for java code. */
7058 buf->b_ind_java = 0;
7059
7060 /* Not to confuse JS object properties with labels. */
7061 buf->b_ind_js = 0;
7062
7063 /* Handle blocked cases correctly. */
7064 buf->b_ind_keep_case_label = 0;
7065
7066 /* Handle C++ namespace. */
7067 buf->b_ind_cpp_namespace = 0;
7068
7069 /* Handle continuation lines containing conditions of if(), for() and
7070 * while(). */
7071 buf->b_ind_if_for_while = 0;
7072
Bram Moolenaar6b643942017-03-05 19:44:06 +01007073 /* indentation for # comments */
7074 buf->b_ind_hash_comment = 0;
7075
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007076 /* Handle C++ extern "C" or "C++" */
7077 buf->b_ind_cpp_extern_c = 0;
7078
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007079 for (p = buf->b_p_cino; *p; )
7080 {
7081 l = p++;
7082 if (*p == '-')
7083 ++p;
7084 digits = p; /* remember where the digits start */
7085 n = getdigits(&p);
7086 divider = 0;
7087 if (*p == '.') /* ".5s" means a fraction */
7088 {
7089 fraction = atol((char *)++p);
7090 while (VIM_ISDIGIT(*p))
7091 {
7092 ++p;
7093 if (divider)
7094 divider *= 10;
7095 else
7096 divider = 10;
7097 }
7098 }
7099 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7100 {
7101 if (p == digits)
7102 n = sw; /* just "s" is one 'shiftwidth' */
7103 else
7104 {
7105 n *= sw;
7106 if (divider)
7107 n += (sw * fraction + divider / 2) / divider;
7108 }
7109 ++p;
7110 }
7111 if (l[1] == '-')
7112 n = -n;
7113
7114 /* When adding an entry here, also update the default 'cinoptions' in
7115 * doc/indent.txt, and add explanation for it! */
7116 switch (*l)
7117 {
7118 case '>': buf->b_ind_level = n; break;
7119 case 'e': buf->b_ind_open_imag = n; break;
7120 case 'n': buf->b_ind_no_brace = n; break;
7121 case 'f': buf->b_ind_first_open = n; break;
7122 case '{': buf->b_ind_open_extra = n; break;
7123 case '}': buf->b_ind_close_extra = n; break;
7124 case '^': buf->b_ind_open_left_imag = n; break;
7125 case 'L': buf->b_ind_jump_label = n; break;
7126 case ':': buf->b_ind_case = n; break;
7127 case '=': buf->b_ind_case_code = n; break;
7128 case 'b': buf->b_ind_case_break = n; break;
7129 case 'p': buf->b_ind_param = n; break;
7130 case 't': buf->b_ind_func_type = n; break;
7131 case '/': buf->b_ind_comment = n; break;
7132 case 'c': buf->b_ind_in_comment = n; break;
7133 case 'C': buf->b_ind_in_comment2 = n; break;
7134 case 'i': buf->b_ind_cpp_baseclass = n; break;
7135 case '+': buf->b_ind_continuation = n; break;
7136 case '(': buf->b_ind_unclosed = n; break;
7137 case 'u': buf->b_ind_unclosed2 = n; break;
7138 case 'U': buf->b_ind_unclosed_noignore = n; break;
7139 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7140 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7141 case 'm': buf->b_ind_matching_paren = n; break;
7142 case 'M': buf->b_ind_paren_prev = n; break;
7143 case ')': buf->b_ind_maxparen = n; break;
7144 case '*': buf->b_ind_maxcomment = n; break;
7145 case 'g': buf->b_ind_scopedecl = n; break;
7146 case 'h': buf->b_ind_scopedecl_code = n; break;
7147 case 'j': buf->b_ind_java = n; break;
7148 case 'J': buf->b_ind_js = n; break;
7149 case 'l': buf->b_ind_keep_case_label = n; break;
7150 case '#': buf->b_ind_hash_comment = n; break;
7151 case 'N': buf->b_ind_cpp_namespace = n; break;
7152 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007153 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007154 }
7155 if (*p == ',')
7156 ++p;
7157 }
7158}
7159
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007160/*
7161 * Return the desired indent for C code.
7162 * Return -1 if the indent should be left alone (inside a raw string).
7163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007165get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 pos_T cur_curpos;
7168 int amount;
7169 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007170 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 colnr_T col;
7172 char_u *theline;
7173 char_u *linecopy;
7174 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007175 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007177 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 pos_T our_paren_pos;
7179 char_u *start;
7180 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007181#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182#define BRACE_AT_START 2 /* '{' is at start of line */
7183#define BRACE_AT_END 3 /* '{' is at end of line */
7184 linenr_T ourscope;
7185 char_u *l;
7186 char_u *look;
7187 char_u terminated;
7188 int lookfor;
7189#define LOOKFOR_INITIAL 0
7190#define LOOKFOR_IF 1
7191#define LOOKFOR_DO 2
7192#define LOOKFOR_CASE 3
7193#define LOOKFOR_ANY 4
7194#define LOOKFOR_TERM 5
7195#define LOOKFOR_UNTERM 6
7196#define LOOKFOR_SCOPEDECL 7
7197#define LOOKFOR_NOBREAK 8
7198#define LOOKFOR_CPP_BASECLASS 9
7199#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007200#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007201#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
7203 int whilelevel;
7204 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 int n;
7206 int iscase;
7207 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007208 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007210 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007211 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007212 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007213 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007214 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007216 /* make a copy, value is changed below */
7217 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219 /* remember where the cursor was when we started */
7220 cur_curpos = curwin->w_cursor;
7221
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007222 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007223 if (cur_curpos.lnum == 1)
7224 return 0;
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 /* Get a copy of the current contents of the line.
7227 * This is required, because only the most recent line obtained with
7228 * ml_get is valid! */
7229 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7230 if (linecopy == NULL)
7231 return 0;
7232
7233 /*
7234 * In insert mode and the cursor is on a ')' truncate the line at the
7235 * cursor position. We don't want to line up with the matching '(' when
7236 * inserting new stuff.
7237 * For unknown reasons the cursor might be past the end of the line, thus
7238 * check for that.
7239 */
7240 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007241 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 && linecopy[curwin->w_cursor.col] == ')')
7243 linecopy[curwin->w_cursor.col] = NUL;
7244
7245 theline = skipwhite(linecopy);
7246
7247 /* move the cursor to the start of the line */
7248
7249 curwin->w_cursor.col = 0;
7250
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007251 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007254 * If we are inside a raw string don't change the indent.
7255 * Ignore a raw string inside a comment.
7256 */
7257 comment_pos = ind_find_start_comment();
7258 if (comment_pos != NULL)
7259 {
7260 /* findmatchlimit() static pos is overwritten, make a copy */
7261 tryposCopy = *comment_pos;
7262 comment_pos = &tryposCopy;
7263 }
7264 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007265 if (trypos != NULL && (comment_pos == NULL
7266 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007267 {
7268 amount = -1;
7269 goto laterend;
7270 }
7271
7272 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 * #defines and so on always go at the left when included in 'cinkeys'.
7274 */
7275 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007276 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007277 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007278 goto theend;
7279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280
7281 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007282 * Is it a non-case label? Then that goes at the left margin too unless:
7283 * - JS flag is set.
7284 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007286 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007287 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
7289 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007290 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
7292
7293 /*
7294 * If we're inside a "//" comment and there is a "//" comment in a
7295 * previous line, lineup with that one.
7296 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007297 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 && (trypos = find_line_comment()) != NULL) /* XXX */
7299 {
7300 /* find how indented the line beginning the comment is */
7301 getvcol(curwin, trypos, &col, NULL, NULL);
7302 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007303 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
7305
7306 /*
7307 * If we're inside a comment and not looking at the start of the
7308 * comment, try using the 'comments' option.
7309 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007310 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 {
7312 int lead_start_len = 2;
7313 int lead_middle_len = 1;
7314 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7315 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7316 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7317 char_u *p;
7318 int start_align = 0;
7319 int start_off = 0;
7320 int done = FALSE;
7321
7322 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007323 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007325 *lead_start = NUL;
7326 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327
7328 p = curbuf->b_p_com;
7329 while (*p != NUL)
7330 {
7331 int align = 0;
7332 int off = 0;
7333 int what = 0;
7334
7335 while (*p != NUL && *p != ':')
7336 {
7337 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7338 what = *p++;
7339 else if (*p == COM_LEFT || *p == COM_RIGHT)
7340 align = *p++;
7341 else if (VIM_ISDIGIT(*p) || *p == '-')
7342 off = getdigits(&p);
7343 else
7344 ++p;
7345 }
7346
7347 if (*p == ':')
7348 ++p;
7349 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7350 if (what == COM_START)
7351 {
7352 STRCPY(lead_start, lead_end);
7353 lead_start_len = (int)STRLEN(lead_start);
7354 start_off = off;
7355 start_align = align;
7356 }
7357 else if (what == COM_MIDDLE)
7358 {
7359 STRCPY(lead_middle, lead_end);
7360 lead_middle_len = (int)STRLEN(lead_middle);
7361 }
7362 else if (what == COM_END)
7363 {
7364 /* If our line starts with the middle comment string, line it
7365 * up with the comment opener per the 'comments' option. */
7366 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7367 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7368 {
7369 done = TRUE;
7370 if (curwin->w_cursor.lnum > 1)
7371 {
7372 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007373 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 * the middle comment string matches in the previous
7375 * line, use the indent of that line. XXX */
7376 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7377 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7378 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7379 else if (STRNCMP(look, lead_middle,
7380 lead_middle_len) == 0)
7381 {
7382 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7383 break;
7384 }
7385 /* If the start comment string doesn't match with the
7386 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007387 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 lead_start, lead_start_len) != 0)
7389 continue;
7390 }
7391 if (start_off != 0)
7392 amount += start_off;
7393 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007394 amount += vim_strsize(lead_start)
7395 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 break;
7397 }
7398
7399 /* If our line starts with the end comment string, line it up
7400 * with the middle comment */
7401 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7402 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7403 {
7404 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7405 /* XXX */
7406 if (off != 0)
7407 amount += off;
7408 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007409 amount += vim_strsize(lead_start)
7410 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 done = TRUE;
7412 break;
7413 }
7414 }
7415 }
7416
7417 /* If our line starts with an asterisk, line up with the
7418 * asterisk in the comment opener; otherwise, line up
7419 * with the first character of the comment text.
7420 */
7421 if (done)
7422 ;
7423 else if (theline[0] == '*')
7424 amount += 1;
7425 else
7426 {
7427 /*
7428 * If we are more than one line away from the comment opener, take
7429 * the indent of the previous non-empty line. If 'cino' has "CO"
7430 * and we are just below the comment opener and there are any
7431 * white characters after it line up with the text after it;
7432 * otherwise, add the amount specified by "c" in 'cino'
7433 */
7434 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007435 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 {
7437 if (linewhite(lnum)) /* skip blank lines */
7438 continue;
7439 amount = get_indent_lnum(lnum); /* XXX */
7440 break;
7441 }
7442 if (amount == -1) /* use the comment opener */
7443 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007444 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007446 start = ml_get(comment_pos->lnum);
7447 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007449 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007451 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007453 if (curbuf->b_ind_in_comment2 || *look == NUL)
7454 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
7456 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007457 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459
7460 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007461 * Are we looking at a ']' that has a match?
7462 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007463 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007464 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7465 {
7466 /* align with the line containing the '['. */
7467 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007468 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007469 }
7470
7471 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 * Are we inside parentheses or braces?
7473 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007474 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007475 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007476 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 || trypos != NULL)
7478 {
7479 if (trypos != NULL && tryposBrace != NULL)
7480 {
7481 /* Both an unmatched '(' and '{' is found. Use the one which is
7482 * closer to the current cursor position, set the other to NULL. */
7483 if (trypos->lnum != tryposBrace->lnum
7484 ? trypos->lnum < tryposBrace->lnum
7485 : trypos->col < tryposBrace->col)
7486 trypos = NULL;
7487 else
7488 tryposBrace = NULL;
7489 }
7490
7491 if (trypos != NULL)
7492 {
7493 /*
7494 * If the matching paren is more than one line away, use the indent of
7495 * a previous non-empty line that matches the same paren.
7496 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007497 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007499 /* Line up with the start of the matching paren line. */
7500 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7501 }
7502 else
7503 {
7504 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007505 our_paren_pos = *trypos;
7506 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007508 l = skipwhite(ml_get(lnum));
7509 if (cin_nocode(l)) /* skip comment lines */
7510 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007511 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007512 continue; /* ignore #define, #if, etc. */
7513 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007515 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007516 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007517 {
7518 lnum = trypos->lnum + 1;
7519 continue;
7520 }
7521
7522 /* XXX */
7523 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007524 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007525 && trypos->lnum == our_paren_pos.lnum
7526 && trypos->col == our_paren_pos.col)
7527 {
7528 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007530 if (theline[0] == ')')
7531 {
7532 if (our_paren_pos.lnum != lnum
7533 && cur_amount > amount)
7534 cur_amount = amount;
7535 amount = -1;
7536 }
7537 break;
7538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
7540 }
7541
7542 /*
7543 * Line up with line where the matching paren is. XXX
7544 * If the line starts with a '(' or the indent for unclosed
7545 * parentheses is zero, line up with the unclosed parentheses.
7546 */
7547 if (amount == -1)
7548 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007549 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007550 int is_if_for_while = 0;
7551
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007552 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007553 {
7554 /* Look for the outermost opening parenthesis on this line
7555 * and check whether it belongs to an "if", "for" or "while". */
7556
7557 pos_T cursor_save = curwin->w_cursor;
7558 pos_T outermost;
7559 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007560
7561 trypos = &our_paren_pos;
7562 do {
7563 outermost = *trypos;
7564 curwin->w_cursor.lnum = outermost.lnum;
7565 curwin->w_cursor.col = outermost.col;
7566
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007567 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007568 } while (trypos && trypos->lnum == outermost.lnum);
7569
7570 curwin->w_cursor = cursor_save;
7571
7572 line = ml_get(outermost.lnum);
7573
7574 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007575 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007576 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007577
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007578 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007579 look = skipwhite(look);
7580 if (*look == '(')
7581 {
7582 linenr_T save_lnum = curwin->w_cursor.lnum;
7583 char_u *line;
7584 int look_col;
7585
7586 /* Ignore a '(' in front of the line that has a match before
7587 * our matching '('. */
7588 curwin->w_cursor.lnum = our_paren_pos.lnum;
7589 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007590 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007591 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007592 if ((trypos = findmatchlimit(NULL, ')', 0,
7593 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007594 != NULL
7595 && trypos->lnum == our_paren_pos.lnum
7596 && trypos->col < our_paren_pos.col)
7597 ignore_paren_col = trypos->col + 1;
7598
7599 curwin->w_cursor.lnum = save_lnum;
7600 look = ml_get(our_paren_pos.lnum) + look_col;
7601 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007602 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7603 && is_if_for_while == 0)
7604 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007605 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
7607 /*
7608 * If we're looking at a close paren, line up right there;
7609 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007610 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 * the last nonwhite character of the line, use either the
7612 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007613 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 * lines).
7615 */
7616 if (theline[0] != ')')
7617 {
7618 cur_amount = MAXCOL;
7619 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007620 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 && cin_ends_in(l, (char_u *)"(", NULL))
7622 {
7623 /* look for opening unmatched paren, indent one level
7624 * for each additional level */
7625 n = 1;
7626 for (col = 0; col < our_paren_pos.col; ++col)
7627 {
7628 switch (l[col])
7629 {
7630 case '(':
7631 case '{': ++n;
7632 break;
7633
7634 case ')':
7635 case '}': if (n > 1)
7636 --n;
7637 break;
7638 }
7639 }
7640
7641 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007642 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007644 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 our_paren_pos.col++;
7646 else
7647 {
7648 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007649 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 col++;
7651 if (l[col] != NUL) /* In case of trailing space */
7652 our_paren_pos.col = col;
7653 else
7654 our_paren_pos.col++;
7655 }
7656 }
7657
7658 /*
7659 * Find how indented the paren is, or the character after it
7660 * if we did the above "if".
7661 */
7662 if (our_paren_pos.col > 0)
7663 {
7664 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7665 if (cur_amount > (int)col)
7666 cur_amount = col;
7667 }
7668 }
7669
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007670 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {
7672 /* Line up with the start of the matching paren line. */
7673 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007674 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7675 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007676 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
7678 if (cur_amount != MAXCOL)
7679 amount = cur_amount;
7680 }
7681 else
7682 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007683 /* Add b_ind_unclosed2 for each '(' before our matching one,
7684 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007686 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
7688 --our_paren_pos.col;
7689 switch (*ml_get_pos(&our_paren_pos))
7690 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007691 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 col = our_paren_pos.col;
7693 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007694 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 col = MAXCOL;
7696 break;
7697 }
7698 }
7699
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007700 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 * braces */
7702 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007703 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 else
7705 {
7706 curwin->w_cursor.lnum = our_paren_pos.lnum;
7707 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007708 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7709 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007710 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007712 {
7713 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007714 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007715 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007716 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719 /*
7720 * For a line starting with ')' use the minimum of the two
7721 * positions, to avoid giving it more indent than the previous
7722 * lines:
7723 * func_long_name( if (x
7724 * arg && yy
7725 * ) ^ not here ) ^ not here
7726 */
7727 if (cur_amount < amount)
7728 amount = cur_amount;
7729 }
7730 }
7731
7732 /* add extra indent for a comment */
7733 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007734 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 else
7737 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007738 /*
7739 * We are inside braces, there is a { before this line at the position
7740 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007741 * Make a copy of tryposBrace, it may point to pos_copy inside
7742 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007743 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007744 tryposCopy = *tryposBrace;
7745 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 ourscope = trypos->lnum;
7748 start = ml_get(ourscope);
7749
7750 /*
7751 * Now figure out how indented the line is in general.
7752 * If the brace was at the start of the line, we use that;
7753 * otherwise, check out the indentation of the line as
7754 * a whole and then add the "imaginary indent" to that.
7755 */
7756 look = skipwhite(start);
7757 if (*look == '{')
7758 {
7759 getvcol(curwin, trypos, &col, NULL, NULL);
7760 amount = col;
7761 if (*start == '{')
7762 start_brace = BRACE_IN_COL0;
7763 else
7764 start_brace = BRACE_AT_START;
7765 }
7766 else
7767 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007768 /* That opening brace might have been on a continuation
7769 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 curwin->w_cursor.lnum = ourscope;
7771
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007772 /* Position the cursor over the rightmost paren, so that
7773 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 lnum = ourscope;
7775 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007776 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7777 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 lnum = trypos->lnum;
7779
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007780 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 * case 1: if (asdf &&
7782 * ldfd) {
7783 * }
7784 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007785 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7786 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007788 else if (curbuf->b_ind_js)
7789 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007791 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
7793 start_brace = BRACE_AT_END;
7794 }
7795
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007796 /* For Javascript check if the line starts with "key:". */
7797 if (curbuf->b_ind_js)
7798 js_cur_has_key = cin_has_js_key(theline);
7799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007801 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 * we want to be. otherwise, add the amount of room
7803 * that an indent is supposed to be.
7804 */
7805 if (theline[0] == '}')
7806 {
7807 /*
7808 * they may want closing braces to line up with something
7809 * other than the open brace. indulge them, if so.
7810 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007811 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 }
7813 else
7814 {
7815 /*
7816 * If we're looking at an "else", try to find an "if"
7817 * to match it with.
7818 * If we're looking at a "while", try to find a "do"
7819 * to match it with.
7820 */
7821 lookfor = LOOKFOR_INITIAL;
7822 if (cin_iselse(theline))
7823 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007824 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 lookfor = LOOKFOR_DO;
7826 if (lookfor != LOOKFOR_INITIAL)
7827 {
7828 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007829 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831 amount = get_indent(); /* XXX */
7832 goto theend;
7833 }
7834 }
7835
7836 /*
7837 * We get here if we are not on an "while-of-do" or "else" (or
7838 * failed to find a matching "if").
7839 * Search backwards for something to line up with.
7840 * First set amount for when we don't find anything.
7841 */
7842
7843 /*
7844 * if the '{' is _really_ at the left margin, use the imaginary
7845 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007846 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 */
7848
7849 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7850 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007851 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007852 lookfor_cpp_namespace = TRUE;
7853 }
7854 else if (start_brace == BRACE_AT_START &&
7855 lookfor_cpp_namespace) /* '{' is at start */
7856 {
7857
7858 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 }
7860 else
7861 {
7862 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007863 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007864 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007865
7866 l = skipwhite(ml_get_curline());
7867 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007868 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007869 else if (cin_is_cpp_extern_c(l))
7870 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 else
7873 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007874 /* Compensate for adding b_ind_open_extra later. */
7875 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (amount < 0)
7877 amount = 0;
7878 }
7879 }
7880
7881 lookfor_break = FALSE;
7882
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007883 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {
7885 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007886 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888 else if (cin_isscopedecl(theline)) /* private:, ... */
7889 {
7890 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007891 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 }
7893 else
7894 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007895 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7896 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 lookfor_break = TRUE;
7898
7899 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007900 /* b_ind_level from start of block */
7901 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903 scope_amount = amount;
7904 whilelevel = 0;
7905
7906 /*
7907 * Search backwards. If we find something we recognize, line up
7908 * with that.
7909 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007910 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 * the usual amount relative to the conditional
7912 * that opens the block.
7913 */
7914 curwin->w_cursor = cur_curpos;
7915 for (;;)
7916 {
7917 curwin->w_cursor.lnum--;
7918 curwin->w_cursor.col = 0;
7919
7920 /*
7921 * If we went all the way back to the start of our scope, line
7922 * up with it.
7923 */
7924 if (curwin->w_cursor.lnum <= ourscope)
7925 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007926 /* We reached end of scope:
7927 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007929 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 * don't add ind_continuation, otherwise it is a variable
7931 * declaration:
7932 * int x,
7933 * here; <-- add ind_continuation
7934 */
7935 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7936 {
7937 if (curwin->w_cursor.lnum == 0
7938 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007939 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007941 /* nothing found (abuse curbuf->b_ind_maxparen as
7942 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 * initialization) */
7944 if (cont_amount > 0)
7945 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007946 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 amount += ind_continuation;
7948 break;
7949 }
7950
7951 l = ml_get_curline();
7952
7953 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007954 * If we're in a comment or raw string now, skip to
7955 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007957 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 if (trypos != NULL)
7959 {
7960 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007961 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 continue;
7963 }
7964
7965 /*
7966 * Skip preprocessor directives and blank lines.
7967 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007968 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7969 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 continue;
7971
7972 if (cin_nocode(l))
7973 continue;
7974
7975 terminated = cin_isterminated(l, FALSE, TRUE);
7976
7977 /*
7978 * If we are at top level and the line looks like a
7979 * function declaration, we are done
7980 * (it's a variable declaration).
7981 */
7982 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007983 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
7985 /* if the line is terminated with another ','
7986 * it is a continued variable initialization.
7987 * don't add extra indent.
7988 * TODO: does not work, if a function
7989 * declaration is split over multiple lines:
7990 * cin_isfuncdecl returns FALSE then.
7991 */
7992 if (terminated == ',')
7993 break;
7994
7995 /* if it es a enum declaration or an assignment,
7996 * we are done.
7997 */
7998 if (terminated != ';' && cin_isinit())
7999 break;
8000
8001 /* nothing useful found */
8002 if (terminated == 0 || terminated == '{')
8003 continue;
8004 }
8005
8006 if (terminated != ';')
8007 {
8008 /* Skip parens and braces. Position the cursor
8009 * over the rightmost paren, so that matching it
8010 * will take us back to the start of the line.
8011 */ /* XXX */
8012 trypos = NULL;
8013 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008014 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008015 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008018 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 if (trypos != NULL)
8021 {
8022 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008023 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 continue;
8025 }
8026 }
8027
8028 /* it's a variable declaration, add indentation
8029 * like in
8030 * int a,
8031 * b;
8032 */
8033 if (cont_amount > 0)
8034 amount = cont_amount;
8035 else
8036 amount += ind_continuation;
8037 }
8038 else if (lookfor == LOOKFOR_UNTERM)
8039 {
8040 if (cont_amount > 0)
8041 amount = cont_amount;
8042 else
8043 amount += ind_continuation;
8044 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008045 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008046 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008047 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008048 && lookfor != LOOKFOR_CPP_BASECLASS
8049 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008050 {
8051 amount = scope_amount;
8052 if (theline[0] == '{')
8053 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008054 amount += curbuf->b_ind_open_extra;
8055 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008056 }
8057 }
8058
8059 if (lookfor_cpp_namespace)
8060 {
8061 /*
8062 * Looking for C++ namespace, need to look further
8063 * back.
8064 */
8065 if (curwin->w_cursor.lnum == ourscope)
8066 continue;
8067
8068 if (curwin->w_cursor.lnum == 0
8069 || curwin->w_cursor.lnum
8070 < ourscope - FIND_NAMESPACE_LIM)
8071 break;
8072
8073 l = ml_get_curline();
8074
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008075 /* If we're in a comment or raw string now, skip
8076 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008077 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008078 if (trypos != NULL)
8079 {
8080 curwin->w_cursor.lnum = trypos->lnum + 1;
8081 curwin->w_cursor.col = 0;
8082 continue;
8083 }
8084
8085 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008086 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8087 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008088 continue;
8089
8090 /* Finally the actual check for "namespace". */
8091 if (cin_is_cpp_namespace(l))
8092 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008093 amount += curbuf->b_ind_cpp_namespace
8094 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008095 break;
8096 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008097 else if (cin_is_cpp_extern_c(l))
8098 {
8099 amount += curbuf->b_ind_cpp_extern_c
8100 - added_to_amount;
8101 break;
8102 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008103
8104 if (cin_nocode(l))
8105 continue;
8106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108 break;
8109 }
8110
8111 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008112 * If we're in a comment or raw string now, skip to the start
8113 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008115 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {
8117 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008118 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 continue;
8120 }
8121
8122 l = ml_get_curline();
8123
8124 /*
8125 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008126 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008128 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 if (iscase || cin_isscopedecl(l))
8130 {
8131 /* we are only looking for cpp base class
8132 * declaration/initialization any longer */
8133 if (lookfor == LOOKFOR_CPP_BASECLASS)
8134 break;
8135
8136 /* When looking for a "do" we are not interested in
8137 * labels. */
8138 if (whilelevel > 0)
8139 continue;
8140
8141 /*
8142 * case xx:
8143 * c = 99 + <- this indent plus continuation
8144 *-> here;
8145 */
8146 if (lookfor == LOOKFOR_UNTERM
8147 || lookfor == LOOKFOR_ENUM_OR_INIT)
8148 {
8149 if (cont_amount > 0)
8150 amount = cont_amount;
8151 else
8152 amount += ind_continuation;
8153 break;
8154 }
8155
8156 /*
8157 * case xx: <- line up with this case
8158 * x = 333;
8159 * case yy:
8160 */
8161 if ( (iscase && lookfor == LOOKFOR_CASE)
8162 || (iscase && lookfor_break)
8163 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8164 {
8165 /*
8166 * Check that this case label is not for another
8167 * switch()
8168 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008169 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008170 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
8172 amount = get_indent(); /* XXX */
8173 break;
8174 }
8175 continue;
8176 }
8177
8178 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8179
8180 /*
8181 * case xx: if (cond) <- line up with this if
8182 * y = y + 1;
8183 * -> s = 99;
8184 *
8185 * case xx:
8186 * if (cond) <- line up with this line
8187 * y = y + 1;
8188 * -> s = 99;
8189 */
8190 if (lookfor == LOOKFOR_TERM)
8191 {
8192 if (n)
8193 amount = n;
8194
8195 if (!lookfor_break)
8196 break;
8197 }
8198
8199 /*
8200 * case xx: x = x + 1; <- line up with this x
8201 * -> y = y + 1;
8202 *
8203 * case xx: if (cond) <- line up with this if
8204 * -> y = y + 1;
8205 */
8206 if (n)
8207 {
8208 amount = n;
8209 l = after_label(ml_get_curline());
8210 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008211 {
8212 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008213 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008214 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008215 amount += curbuf->b_ind_level
8216 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 break;
8219 }
8220
8221 /*
8222 * Try to get the indent of a statement before the switch
8223 * label. If nothing is found, line up relative to the
8224 * switch label.
8225 * break; <- may line up with this line
8226 * case xx:
8227 * -> y = 1;
8228 */
8229 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008230 ? curbuf->b_ind_case_code
8231 : curbuf->b_ind_scopedecl_code);
8232 lookfor = curbuf->b_ind_case_break
8233 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 continue;
8235 }
8236
8237 /*
8238 * Looking for a switch() label or C++ scope declaration,
8239 * ignore other lines, skip {}-blocks.
8240 */
8241 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8242 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008243 if (find_last_paren(l, '{', '}')
8244 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008247 curwin->w_cursor.col = 0;
8248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 continue;
8250 }
8251
8252 /*
8253 * Ignore jump labels with nothing after them.
8254 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008255 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
8257 l = after_label(ml_get_curline());
8258 if (l == NULL || cin_nocode(l))
8259 continue;
8260 }
8261
8262 /*
8263 * Ignore #defines, #if, etc.
8264 * Ignore comment and empty lines.
8265 * (need to get the line again, cin_islabel() may have
8266 * unlocked it)
8267 */
8268 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008269 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 || cin_nocode(l))
8271 continue;
8272
8273 /*
8274 * Are we at the start of a cpp base class declaration or
8275 * constructor initialization?
8276 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008277 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008278 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008279 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008280 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008281 l = ml_get_curline();
8282 }
8283 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
8285 if (lookfor == LOOKFOR_UNTERM)
8286 {
8287 if (cont_amount > 0)
8288 amount = cont_amount;
8289 else
8290 amount += ind_continuation;
8291 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008292 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008294 /* Need to find start of the declaration. */
8295 lookfor = LOOKFOR_UNTERM;
8296 ind_continuation = 0;
8297 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
8299 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008300 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008301 amount = get_baseclass_amount(
8302 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 break;
8304 }
8305 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8306 {
8307 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008308 * declaration or initialization before the opening brace.
8309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 if (cin_isterminated(l, TRUE, FALSE))
8311 break;
8312 else
8313 continue;
8314 }
8315
8316 /*
8317 * What happens next depends on the line being terminated.
8318 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008319 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 * 123,
8321 * sizeof
8322 * here
8323 * Otherwise check whether it is a enumeration or structure
8324 * initialisation (not indented) or a variable declaration
8325 * (indented).
8326 */
8327 terminated = cin_isterminated(l, FALSE, TRUE);
8328
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008329 if (js_cur_has_key)
8330 {
8331 js_cur_has_key = 0; /* only check the first line */
8332 if (curbuf->b_ind_js && terminated == ',')
8333 {
8334 /* For Javascript we might be inside an object:
8335 * key: something, <- align with this
8336 * key: something
8337 * or:
8338 * key: something + <- align with this
8339 * something,
8340 * key: something
8341 */
8342 lookfor = LOOKFOR_JS_KEY;
8343 }
8344 }
8345 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8346 {
8347 amount = get_indent();
8348 break;
8349 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008350 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008351 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008352 if (tryposBrace != NULL && tryposBrace->lnum
8353 >= curwin->w_cursor.lnum)
8354 break;
8355 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008356 /* line below current line is the one that starts a
8357 * (possibly broken) line ending in a comma. */
8358 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008359 else
8360 {
8361 amount = get_indent();
8362 if (curwin->w_cursor.lnum - 1 == ourscope)
8363 /* line above is start of the scope, thus current
8364 * line is the one that stars a (possibly broken)
8365 * line ending in a comma. */
8366 break;
8367 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008368 }
8369
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8371 && terminated == ','))
8372 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008373 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8374 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008375 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 /*
8377 * if we're in the middle of a paren thing,
8378 * go back to the line that starts it so
8379 * we can get the right prevailing indent
8380 * if ( foo &&
8381 * bar )
8382 */
8383 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008384 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008386 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 */
8388 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008389 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008390 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8391 || (trypos->lnum == tryposBrace->lnum
8392 && trypos->col < tryposBrace->col)))
8393 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 /*
8396 * If we are looking for ',', we also look for matching
8397 * braces.
8398 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008399 if (trypos == NULL && terminated == ','
8400 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008401 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
8403 if (trypos != NULL)
8404 {
8405 /*
8406 * Check if we are on a case label now. This is
8407 * handled above.
8408 * case xx: if ( asdf &&
8409 * asdf)
8410 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008411 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008413 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
8415 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008416 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 continue;
8418 }
8419 }
8420
8421 /*
8422 * Skip over continuation lines to find the one to get the
8423 * indent from
8424 * char *usethis = "bla\
8425 * bla",
8426 * here;
8427 */
8428 if (terminated == ',')
8429 {
8430 while (curwin->w_cursor.lnum > 1)
8431 {
8432 l = ml_get(curwin->w_cursor.lnum - 1);
8433 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8434 break;
8435 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008436 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
8438 }
8439
8440 /*
8441 * Get indent and pointer to text for current line,
8442 * ignoring any jump label. XXX
8443 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008444 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008445 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008446 else
8447 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 /*
8449 * If this is just above the line we are indenting, and it
8450 * starts with a '{', line it up with this line.
8451 * while (not)
8452 * -> {
8453 * }
8454 */
8455 if (terminated != ',' && lookfor != LOOKFOR_TERM
8456 && theline[0] == '{')
8457 {
8458 amount = cur_amount;
8459 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008460 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 * doesn't start with a '{', which must have a match
8462 * in the same line (scope is the same). Probably:
8463 * { 1, 2 },
8464 * -> { 3, 4 }
8465 */
8466 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008467 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008469 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
8471 /* have to look back, whether it is a cpp base
8472 * class declaration or initialization */
8473 lookfor = LOOKFOR_CPP_BASECLASS;
8474 continue;
8475 }
8476 break;
8477 }
8478
8479 /*
8480 * Check if we are after an "if", "while", etc.
8481 * Also allow " } else".
8482 */
8483 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8484 {
8485 /*
8486 * Found an unterminated line after an if (), line up
8487 * with the last one.
8488 * if (cond)
8489 * 100 +
8490 * -> here;
8491 */
8492 if (lookfor == LOOKFOR_UNTERM
8493 || lookfor == LOOKFOR_ENUM_OR_INIT)
8494 {
8495 if (cont_amount > 0)
8496 amount = cont_amount;
8497 else
8498 amount += ind_continuation;
8499 break;
8500 }
8501
8502 /*
8503 * If this is just above the line we are indenting, we
8504 * are finished.
8505 * while (not)
8506 * -> here;
8507 * Otherwise this indent can be used when the line
8508 * before this is terminated.
8509 * yyy;
8510 * if (stat)
8511 * while (not)
8512 * xxx;
8513 * -> here;
8514 */
8515 amount = cur_amount;
8516 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008517 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (lookfor != LOOKFOR_TERM)
8519 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008520 amount += curbuf->b_ind_level
8521 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 break;
8523 }
8524
8525 /*
8526 * Special trick: when expecting the while () after a
8527 * do, line up with the while()
8528 * do
8529 * x = 1;
8530 * -> here
8531 */
8532 l = skipwhite(ml_get_curline());
8533 if (cin_isdo(l))
8534 {
8535 if (whilelevel == 0)
8536 break;
8537 --whilelevel;
8538 }
8539
8540 /*
8541 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008542 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 * Need to use the scope of this "else". XXX
8544 * If whilelevel != 0 continue looking for a "do {".
8545 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008546 if (cin_iselse(l) && whilelevel == 0)
8547 {
8548 /* If we're looking at "} else", let's make sure we
8549 * find the opening brace of the enclosing scope,
8550 * not the one from "if () {". */
8551 if (*l == '}')
8552 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008553 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008554
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008555 if ((trypos = find_start_brace()) == NULL
8556 || find_match(LOOKFOR_IF, trypos->lnum)
8557 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008558 break;
8559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 }
8561
8562 /*
8563 * If we're below an unterminated line that is not an
8564 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008565 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 * the line before this one.
8567 */
8568 else
8569 {
8570 /*
8571 * Found two unterminated lines on a row, line up with
8572 * the last one.
8573 * c = 99 +
8574 * 100 +
8575 * -> here;
8576 */
8577 if (lookfor == LOOKFOR_UNTERM)
8578 {
8579 /* When line ends in a comma add extra indent */
8580 if (terminated == ',')
8581 amount += ind_continuation;
8582 break;
8583 }
8584
8585 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8586 {
8587 /* Found two lines ending in ',', lineup with the
8588 * lowest one, but check for cpp base class
8589 * declaration/initialization, if it is an
8590 * opening brace or we are looking just for
8591 * enumerations/initializations. */
8592 if (terminated == ',')
8593 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008594 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 break;
8596
8597 lookfor = LOOKFOR_CPP_BASECLASS;
8598 continue;
8599 }
8600
8601 /* Ignore unterminated lines in between, but
8602 * reduce indent. */
8603 if (amount > cur_amount)
8604 amount = cur_amount;
8605 }
8606 else
8607 {
8608 /*
8609 * Found first unterminated line on a row, may
8610 * line up with this line, remember its indent
8611 * 100 +
8612 * -> here;
8613 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008614 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008616
8617 n = (int)STRLEN(l);
8618 if (terminated == ',' && (*skipwhite(l) == ']'
8619 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008620 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621
8622 /*
8623 * If previous line ends in ',', check whether we
8624 * are in an initialization or enum
8625 * struct xxx =
8626 * {
8627 * sizeof a,
8628 * 124 };
8629 * or a normal possible continuation line.
8630 * but only, of no other statement has been found
8631 * yet.
8632 */
8633 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8634 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008635 if (curbuf->b_ind_js)
8636 {
8637 /* Search for a line ending in a comma
8638 * and line up with the line below it
8639 * (could be the current line).
8640 * some = [
8641 * 1, <- line up here
8642 * 2,
8643 * some = [
8644 * 3 + <- line up here
8645 * 4 *
8646 * 5,
8647 * 6,
8648 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008649 if (cin_iscomment(skipwhite(l)))
8650 break;
8651 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008652 trypos = find_match_char('[',
8653 curbuf->b_ind_maxparen);
8654 if (trypos != NULL)
8655 {
8656 if (trypos->lnum
8657 == curwin->w_cursor.lnum - 1)
8658 {
8659 /* Current line is first inside
8660 * [], line up with it. */
8661 break;
8662 }
8663 ourscope = trypos->lnum;
8664 }
8665 }
8666 else
8667 {
8668 lookfor = LOOKFOR_ENUM_OR_INIT;
8669 cont_amount = cin_first_id_amount();
8670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 }
8672 else
8673 {
8674 if (lookfor == LOOKFOR_INITIAL
8675 && *l != NUL
8676 && l[STRLEN(l) - 1] == '\\')
8677 /* XXX */
8678 cont_amount = cin_get_equal_amount(
8679 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008680 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008681 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008682 && lookfor != LOOKFOR_COMMA
8683 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 lookfor = LOOKFOR_UNTERM;
8685 }
8686 }
8687 }
8688 }
8689
8690 /*
8691 * Check if we are after a while (cond);
8692 * If so: Ignore until the matching "do".
8693 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008694 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {
8696 /*
8697 * Found an unterminated line after a while ();, line up
8698 * with the last one.
8699 * while (cond);
8700 * 100 + <- line up with this one
8701 * -> here;
8702 */
8703 if (lookfor == LOOKFOR_UNTERM
8704 || lookfor == LOOKFOR_ENUM_OR_INIT)
8705 {
8706 if (cont_amount > 0)
8707 amount = cont_amount;
8708 else
8709 amount += ind_continuation;
8710 break;
8711 }
8712
8713 if (whilelevel == 0)
8714 {
8715 lookfor = LOOKFOR_TERM;
8716 amount = get_indent(); /* XXX */
8717 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008718 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 }
8720 ++whilelevel;
8721 }
8722
8723 /*
8724 * We are after a "normal" statement.
8725 * If we had another statement we can stop now and use the
8726 * indent of that other statement.
8727 * Otherwise the indent of the current statement may be used,
8728 * search backwards for the next "normal" statement.
8729 */
8730 else
8731 {
8732 /*
8733 * Skip single break line, if before a switch label. It
8734 * may be lined up with the case label.
8735 */
8736 if (lookfor == LOOKFOR_NOBREAK
8737 && cin_isbreak(skipwhite(ml_get_curline())))
8738 {
8739 lookfor = LOOKFOR_ANY;
8740 continue;
8741 }
8742
8743 /*
8744 * Handle "do {" line.
8745 */
8746 if (whilelevel > 0)
8747 {
8748 l = cin_skipcomment(ml_get_curline());
8749 if (cin_isdo(l))
8750 {
8751 amount = get_indent(); /* XXX */
8752 --whilelevel;
8753 continue;
8754 }
8755 }
8756
8757 /*
8758 * Found a terminated line above an unterminated line. Add
8759 * the amount for a continuation line.
8760 * x = 1;
8761 * y = foo +
8762 * -> here;
8763 * or
8764 * int x = 1;
8765 * int foo,
8766 * -> here;
8767 */
8768 if (lookfor == LOOKFOR_UNTERM
8769 || lookfor == LOOKFOR_ENUM_OR_INIT)
8770 {
8771 if (cont_amount > 0)
8772 amount = cont_amount;
8773 else
8774 amount += ind_continuation;
8775 break;
8776 }
8777
8778 /*
8779 * Found a terminated line above a terminated line or "if"
8780 * etc. line. Use the amount of the line below us.
8781 * x = 1; x = 1;
8782 * if (asdf) y = 2;
8783 * while (asdf) ->here;
8784 * here;
8785 * ->foo;
8786 */
8787 if (lookfor == LOOKFOR_TERM)
8788 {
8789 if (!lookfor_break && whilelevel == 0)
8790 break;
8791 }
8792
8793 /*
8794 * First line above the one we're indenting is terminated.
8795 * To know what needs to be done look further backward for
8796 * a terminated line.
8797 */
8798 else
8799 {
8800 /*
8801 * position the cursor over the rightmost paren, so
8802 * that matching it will take us back to the start of
8803 * the line. Helps for:
8804 * func(asdr,
8805 * asdfasdf);
8806 * here;
8807 */
8808term_again:
8809 l = ml_get_curline();
8810 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008811 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008812 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 {
8814 /*
8815 * Check if we are on a case label now. This is
8816 * handled above.
8817 * case xx: if ( asdf &&
8818 * asdf)
8819 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008820 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008822 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 {
8824 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008825 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 continue;
8827 }
8828 }
8829
8830 /* When aligning with the case statement, don't align
8831 * with a statement after it.
8832 * case 1: { <-- don't use this { position
8833 * stat;
8834 * }
8835 * case 2:
8836 * stat;
8837 * }
8838 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008839 iscase = (curbuf->b_ind_keep_case_label
8840 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841
8842 /*
8843 * Get indent and pointer to text for current line,
8844 * ignoring any jump label.
8845 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008846 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008849 amount += curbuf->b_ind_open_extra;
8850 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008851 l = skipwhite(l);
8852 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008853 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8855
8856 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008857 * When a terminated line starts with "else" skip to
8858 * the matching "if":
8859 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008860 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008861 * Need to use the scope of this "else". XXX
8862 * If whilelevel != 0 continue looking for a "do {".
8863 */
8864 if (lookfor == LOOKFOR_TERM
8865 && *l != '}'
8866 && cin_iselse(l)
8867 && whilelevel == 0)
8868 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008869 if ((trypos = find_start_brace()) == NULL
8870 || find_match(LOOKFOR_IF, trypos->lnum)
8871 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008872 break;
8873 continue;
8874 }
8875
8876 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 * If we're at the end of a block, skip to the start of
8878 * that block.
8879 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008880 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008881 if (find_last_paren(l, '{', '}') /* XXX */
8882 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008884 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 /* if not "else {" check for terminated again */
8886 /* but skip block for "} else {" */
8887 l = cin_skipcomment(ml_get_curline());
8888 if (*l == '}' || !cin_iselse(l))
8889 goto term_again;
8890 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008891 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 }
8894 }
8895 }
8896 }
8897 }
8898
8899 /* add extra indent for a comment */
8900 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008901 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008902
8903 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008904 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8905 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008906
8907 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008909
8910 /*
8911 * ok -- we're not inside any sort of structure at all!
8912 *
8913 * This means we're at the top level, and everything should
8914 * basically just match where the previous line is, except
8915 * for the lines immediately following a function declaration,
8916 * which are K&R-style parameters and need to be indented.
8917 *
8918 * if our line starts with an open brace, forget about any
8919 * prevailing indent and make sure it looks like the start
8920 * of a function
8921 */
8922
8923 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008925 amount = curbuf->b_ind_first_open;
8926 goto theend;
8927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008929 /*
8930 * If the NEXT line is a function declaration, the current
8931 * line needs to be indented as a function type spec.
8932 * Don't do this if the current line looks like a comment or if the
8933 * current line is terminated, ie. ends in ';', or if the current line
8934 * contains { or }: "void f() {\n if (1)"
8935 */
8936 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8937 && !cin_nocode(theline)
8938 && vim_strchr(theline, '{') == NULL
8939 && vim_strchr(theline, '}') == NULL
8940 && !cin_ends_in(theline, (char_u *)":", NULL)
8941 && !cin_ends_in(theline, (char_u *)",", NULL)
8942 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8943 cur_curpos.lnum + 1)
8944 && !cin_isterminated(theline, FALSE, TRUE))
8945 {
8946 amount = curbuf->b_ind_func_type;
8947 goto theend;
8948 }
8949
8950 /* search backwards until we find something we recognize */
8951 amount = 0;
8952 curwin->w_cursor = cur_curpos;
8953 while (curwin->w_cursor.lnum > 1)
8954 {
8955 curwin->w_cursor.lnum--;
8956 curwin->w_cursor.col = 0;
8957
8958 l = ml_get_curline();
8959
8960 /*
8961 * If we're in a comment or raw string now, skip to the start
8962 * of it.
8963 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008964 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008966 curwin->w_cursor.lnum = trypos->lnum + 1;
8967 curwin->w_cursor.col = 0;
8968 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 }
8970
8971 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008972 * Are we at the start of a cpp base class declaration or
8973 * constructor initialization?
8974 */ /* XXX */
8975 n = FALSE;
8976 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008978 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8979 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008981 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008983 /* XXX */
8984 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8985 break;
8986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008988 /*
8989 * Skip preprocessor directives and blank lines.
8990 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008991 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008992 continue;
8993
8994 if (cin_nocode(l))
8995 continue;
8996
8997 /*
8998 * If the previous line ends in ',', use one level of
8999 * indentation:
9000 * int foo,
9001 * bar;
9002 * do this before checking for '}' in case of eg.
9003 * enum foobar
9004 * {
9005 * ...
9006 * } foo,
9007 * bar;
9008 */
9009 n = 0;
9010 if (cin_ends_in(l, (char_u *)",", NULL)
9011 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9012 {
9013 /* take us back to opening paren */
9014 if (find_last_paren(l, '(', ')')
9015 && (trypos = find_match_paren(
9016 curbuf->b_ind_maxparen)) != NULL)
9017 curwin->w_cursor = *trypos;
9018
9019 /* For a line ending in ',' that is a continuation line go
9020 * back to the first line with a backslash:
9021 * char *foo = "bla\
9022 * bla",
9023 * here;
9024 */
9025 while (n == 0 && curwin->w_cursor.lnum > 1)
9026 {
9027 l = ml_get(curwin->w_cursor.lnum - 1);
9028 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9029 break;
9030 --curwin->w_cursor.lnum;
9031 curwin->w_cursor.col = 0;
9032 }
9033
9034 amount = get_indent(); /* XXX */
9035
9036 if (amount == 0)
9037 amount = cin_first_id_amount();
9038 if (amount == 0)
9039 amount = ind_continuation;
9040 break;
9041 }
9042
9043 /*
9044 * If the line looks like a function declaration, and we're
9045 * not in a comment, put it the left margin.
9046 */
9047 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9048 break;
9049 l = ml_get_curline();
9050
9051 /*
9052 * Finding the closing '}' of a previous function. Put
9053 * current line at the left margin. For when 'cino' has "fs".
9054 */
9055 if (*skipwhite(l) == '}')
9056 break;
9057
9058 /* (matching {)
9059 * If the previous line ends on '};' (maybe followed by
9060 * comments) align at column 0. For example:
9061 * char *string_array[] = { "foo",
9062 * / * x * / "b};ar" }; / * foobar * /
9063 */
9064 if (cin_ends_in(l, (char_u *)"};", NULL))
9065 break;
9066
9067 /*
9068 * If the previous line ends on '[' we are probably in an
9069 * array constant:
9070 * something = [
9071 * 234, <- extra indent
9072 */
9073 if (cin_ends_in(l, (char_u *)"[", NULL))
9074 {
9075 amount = get_indent() + ind_continuation;
9076 break;
9077 }
9078
9079 /*
9080 * Find a line only has a semicolon that belongs to a previous
9081 * line ending in '}', e.g. before an #endif. Don't increase
9082 * indent then.
9083 */
9084 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9085 {
9086 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
9088 while (curwin->w_cursor.lnum > 1)
9089 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009090 look = ml_get(--curwin->w_cursor.lnum);
9091 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009092 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009094 }
9095 if (curwin->w_cursor.lnum > 0
9096 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009099 curwin->w_cursor = curpos_save;
9100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009102 /*
9103 * If the PREVIOUS line is a function declaration, the current
9104 * line (and the ones that follow) needs to be indented as
9105 * parameters.
9106 */
9107 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9108 {
9109 amount = curbuf->b_ind_param;
9110 break;
9111 }
9112
9113 /*
9114 * If the previous line ends in ';' and the line before the
9115 * previous line ends in ',' or '\', ident to column zero:
9116 * int foo,
9117 * bar;
9118 * indent_to_0 here;
9119 */
9120 if (cin_ends_in(l, (char_u *)";", NULL))
9121 {
9122 l = ml_get(curwin->w_cursor.lnum - 1);
9123 if (cin_ends_in(l, (char_u *)",", NULL)
9124 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9125 break;
9126 l = ml_get_curline();
9127 }
9128
9129 /*
9130 * Doesn't look like anything interesting -- so just
9131 * use the indent of this line.
9132 *
9133 * Position the cursor over the rightmost paren, so that
9134 * matching it will take us back to the start of the line.
9135 */
9136 find_last_paren(l, '(', ')');
9137
9138 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9139 curwin->w_cursor = *trypos;
9140 amount = get_indent(); /* XXX */
9141 break;
9142 }
9143
9144 /* add extra indent for a comment */
9145 if (cin_iscomment(theline))
9146 amount += curbuf->b_ind_comment;
9147
9148 /* add extra indent if the previous line ended in a backslash:
9149 * "asdfasdf\
9150 * here";
9151 * char *foo = "asdf\
9152 * here";
9153 */
9154 if (cur_curpos.lnum > 1)
9155 {
9156 l = ml_get(cur_curpos.lnum - 1);
9157 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9158 {
9159 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9160 if (cur_amount > 0)
9161 amount = cur_amount;
9162 else if (cur_amount == 0)
9163 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 }
9165 }
9166
9167theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009168 if (amount < 0)
9169 amount = 0;
9170
9171laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 /* put the cursor back where it belongs */
9173 curwin->w_cursor = cur_curpos;
9174
9175 vim_free(linecopy);
9176
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 return amount;
9178}
9179
9180 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009181find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
9183 char_u *look;
9184 pos_T *theirscope;
9185 char_u *mightbeif;
9186 int elselevel;
9187 int whilelevel;
9188
9189 if (lookfor == LOOKFOR_IF)
9190 {
9191 elselevel = 1;
9192 whilelevel = 0;
9193 }
9194 else
9195 {
9196 elselevel = 0;
9197 whilelevel = 1;
9198 }
9199
9200 curwin->w_cursor.col = 0;
9201
9202 while (curwin->w_cursor.lnum > ourscope + 1)
9203 {
9204 curwin->w_cursor.lnum--;
9205 curwin->w_cursor.col = 0;
9206
9207 look = cin_skipcomment(ml_get_curline());
9208 if (cin_iselse(look)
9209 || cin_isif(look)
9210 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009211 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 {
9213 /*
9214 * if we've gone outside the braces entirely,
9215 * we must be out of scope...
9216 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009217 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 if (theirscope == NULL)
9219 break;
9220
9221 /*
9222 * and if the brace enclosing this is further
9223 * back than the one enclosing the else, we're
9224 * out of luck too.
9225 */
9226 if (theirscope->lnum < ourscope)
9227 break;
9228
9229 /*
9230 * and if they're enclosed in a *deeper* brace,
9231 * then we can ignore it because it's in a
9232 * different scope...
9233 */
9234 if (theirscope->lnum > ourscope)
9235 continue;
9236
9237 /*
9238 * if it was an "else" (that's not an "else if")
9239 * then we need to go back to another if, so
9240 * increment elselevel
9241 */
9242 look = cin_skipcomment(ml_get_curline());
9243 if (cin_iselse(look))
9244 {
9245 mightbeif = cin_skipcomment(look + 4);
9246 if (!cin_isif(mightbeif))
9247 ++elselevel;
9248 continue;
9249 }
9250
9251 /*
9252 * if it was a "while" then we need to go back to
9253 * another "do", so increment whilelevel. XXX
9254 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009255 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 {
9257 ++whilelevel;
9258 continue;
9259 }
9260
9261 /* If it's an "if" decrement elselevel */
9262 look = cin_skipcomment(ml_get_curline());
9263 if (cin_isif(look))
9264 {
9265 elselevel--;
9266 /*
9267 * When looking for an "if" ignore "while"s that
9268 * get in the way.
9269 */
9270 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9271 whilelevel = 0;
9272 }
9273
9274 /* If it's a "do" decrement whilelevel */
9275 if (cin_isdo(look))
9276 whilelevel--;
9277
9278 /*
9279 * if we've used up all the elses, then
9280 * this must be the if that we want!
9281 * match the indent level of that if.
9282 */
9283 if (elselevel <= 0 && whilelevel <= 0)
9284 {
9285 return OK;
9286 }
9287 }
9288 }
9289 return FAIL;
9290}
9291
9292# if defined(FEAT_EVAL) || defined(PROTO)
9293/*
9294 * Get indent level from 'indentexpr'.
9295 */
9296 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009297get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009299 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009300 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009301 pos_T save_pos;
9302 colnr_T save_curswant;
9303 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009305 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9306 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009308 /* Save and restore cursor position and curswant, in case it was changed
9309 * via :normal commands */
9310 save_pos = curwin->w_cursor;
9311 save_curswant = curwin->w_curswant;
9312 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009314 if (use_sandbox)
9315 ++sandbox;
9316 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009317
9318 /* Need to make a copy, the 'indentexpr' option could be changed while
9319 * evaluating it. */
9320 inde_copy = vim_strsave(curbuf->b_p_inde);
9321 if (inde_copy != NULL)
9322 {
9323 indent = (int)eval_to_number(inde_copy);
9324 vim_free(inde_copy);
9325 }
9326
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009327 if (use_sandbox)
9328 --sandbox;
9329 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330
9331 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9332 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9333 * command. */
9334 save_State = State;
9335 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009336 curwin->w_cursor = save_pos;
9337 curwin->w_curswant = save_curswant;
9338 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 check_cursor();
9340 State = save_State;
9341
9342 /* If there is an error, just keep the current indent. */
9343 if (indent < 0)
9344 indent = get_indent();
9345
9346 return indent;
9347}
9348# endif
9349
9350#endif /* FEAT_CINDENT */
9351
9352#if defined(FEAT_LISP) || defined(PROTO)
9353
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009354static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355
9356 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009357lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358{
9359 char_u buf[LSIZE];
9360 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009361 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362
9363 while (*word != NUL)
9364 {
9365 (void)copy_option_part(&word, buf, LSIZE, ",");
9366 len = (int)STRLEN(buf);
9367 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9368 return TRUE;
9369 }
9370 return FALSE;
9371}
9372
9373/*
9374 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9375 * The incompatible newer method is quite a bit better at indenting
9376 * code in lisp-like languages than the traditional one; it's still
9377 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9378 *
9379 * TODO:
9380 * Findmatch() should be adapted for lisp, also to make showmatch
9381 * work correctly: now (v5.3) it seems all C/C++ oriented:
9382 * - it does not recognize the #\( and #\) notations as character literals
9383 * - it doesn't know about comments starting with a semicolon
9384 * - it incorrectly interprets '(' as a character literal
9385 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009386 * Update from Sergey Khorev:
9387 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 */
9389 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009390get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009392 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 int amount;
9394 char_u *that;
9395 colnr_T col;
9396 colnr_T firsttry;
9397 int parencount, quotecount;
9398 int vi_lisp;
9399
9400 /* Set vi_lisp to use the vi-compatible method */
9401 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9402
9403 realpos = curwin->w_cursor;
9404 curwin->w_cursor.col = 0;
9405
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009406 if ((pos = findmatch(NULL, '(')) == NULL)
9407 pos = findmatch(NULL, '[');
9408 else
9409 {
9410 paren = *pos;
9411 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009412 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009413 pos = &paren;
9414 }
9415 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 {
9417 /* Extra trick: Take the indent of the first previous non-white
9418 * line that is at the same () level. */
9419 amount = -1;
9420 parencount = 0;
9421
9422 while (--curwin->w_cursor.lnum >= pos->lnum)
9423 {
9424 if (linewhite(curwin->w_cursor.lnum))
9425 continue;
9426 for (that = ml_get_curline(); *that != NUL; ++that)
9427 {
9428 if (*that == ';')
9429 {
9430 while (*(that + 1) != NUL)
9431 ++that;
9432 continue;
9433 }
9434 if (*that == '\\')
9435 {
9436 if (*(that + 1) != NUL)
9437 ++that;
9438 continue;
9439 }
9440 if (*that == '"' && *(that + 1) != NUL)
9441 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009442 while (*++that && *that != '"')
9443 {
9444 /* skipping escaped characters in the string */
9445 if (*that == '\\')
9446 {
9447 if (*++that == NUL)
9448 break;
9449 if (that[1] == NUL)
9450 {
9451 ++that;
9452 break;
9453 }
9454 }
9455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009457 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009459 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 --parencount;
9461 }
9462 if (parencount == 0)
9463 {
9464 amount = get_indent();
9465 break;
9466 }
9467 }
9468
9469 if (amount == -1)
9470 {
9471 curwin->w_cursor.lnum = pos->lnum;
9472 curwin->w_cursor.col = pos->col;
9473 col = pos->col;
9474
9475 that = ml_get_curline();
9476
9477 if (vi_lisp && get_indent() == 0)
9478 amount = 2;
9479 else
9480 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009481 char_u *line = that;
9482
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 amount = 0;
9484 while (*that && col)
9485 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009486 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 col--;
9488 }
9489
9490 /*
9491 * Some keywords require "body" indenting rules (the
9492 * non-standard-lisp ones are Scheme special forms):
9493 *
9494 * (let ((a 1)) instead (let ((a 1))
9495 * (...)) of (...))
9496 */
9497
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009498 if (!vi_lisp && (*that == '(' || *that == '[')
9499 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 amount += 2;
9501 else
9502 {
9503 that++;
9504 amount++;
9505 firsttry = amount;
9506
Bram Moolenaar1c465442017-03-12 20:10:05 +01009507 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009509 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 ++that;
9511 }
9512
9513 if (*that && *that != ';') /* not a comment line */
9514 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009515 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009517 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 firsttry++;
9519
9520 parencount = 0;
9521 quotecount = 0;
9522
9523 if (vi_lisp
9524 || (*that != '"'
9525 && *that != '\''
9526 && *that != '#'
9527 && (*that < '0' || *that > '9')))
9528 {
9529 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009530 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 || quotecount
9532 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009533 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 && !quotecount
9535 && !parencount
9536 && vi_lisp)))
9537 {
9538 if (*that == '"')
9539 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009540 if ((*that == '(' || *that == '[')
9541 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009543 if ((*that == ')' || *that == ']')
9544 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 --parencount;
9546 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009547 amount += lbr_chartabsize_adv(
9548 line, &that, (colnr_T)amount);
9549 amount += lbr_chartabsize_adv(
9550 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 }
9552 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009553 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009555 amount += lbr_chartabsize(
9556 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 that++;
9558 }
9559 if (!*that || *that == ';')
9560 amount = firsttry;
9561 }
9562 }
9563 }
9564 }
9565 }
9566 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009567 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568
9569 curwin->w_cursor = realpos;
9570
9571 return amount;
9572}
9573#endif /* FEAT_LISP */
9574
9575 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009576prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009578#if defined(SIGHUP) && defined(SIG_IGN)
9579 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9580 * makes Vim exit and then handling SIGHUP causes various reentrance
9581 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009582 signal(SIGHUP, SIG_IGN);
9583#endif
9584
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585#ifdef FEAT_GUI
9586 if (gui.in_use)
9587 {
9588 gui.dying = TRUE;
9589 out_trash(); /* trash any pending output */
9590 }
9591 else
9592#endif
9593 {
9594 windgoto((int)Rows - 1, 0);
9595
9596 /*
9597 * Switch terminal mode back now, so messages end up on the "normal"
9598 * screen (if there are two screens).
9599 */
9600 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009601 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 out_flush();
9603 }
9604}
9605
9606/*
9607 * Preserve files and exit.
9608 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009609 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9610 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 */
9612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009613preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 buf_T *buf;
9616
9617 prepare_to_exit();
9618
Bram Moolenaar4770d092006-01-12 23:22:24 +00009619 /* Setting this will prevent free() calls. That avoids calling free()
9620 * recursively when free() was invoked with a bad pointer. */
9621 really_exiting = TRUE;
9622
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 out_str(IObuff);
9624 screen_start(); /* don't know where cursor is now */
9625 out_flush();
9626
9627 ml_close_notmod(); /* close all not-modified buffers */
9628
Bram Moolenaar29323592016-07-24 22:04:11 +02009629 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 {
9631 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9632 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009633 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 screen_start(); /* don't know where cursor is now */
9635 out_flush();
9636 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9637 break;
9638 }
9639 }
9640
9641 ml_close_all(FALSE); /* close all memfiles, without deleting */
9642
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009643 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644
9645 getout(1);
9646}
9647
9648/*
9649 * return TRUE if "fname" exists.
9650 */
9651 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009652vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009654 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655
9656 if (mch_stat((char *)fname, &st))
9657 return FALSE;
9658 return TRUE;
9659}
9660
9661/*
9662 * Check for CTRL-C pressed, but only once in a while.
9663 * Should be used instead of ui_breakcheck() for functions that check for
9664 * each line in the file. Calling ui_breakcheck() each time takes too much
9665 * time, because it can be a system call.
9666 */
9667
9668#ifndef BREAKCHECK_SKIP
9669# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9670# define BREAKCHECK_SKIP 200
9671# else
9672# define BREAKCHECK_SKIP 32
9673# endif
9674#endif
9675
9676static int breakcheck_count = 0;
9677
9678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009679line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680{
9681 if (++breakcheck_count >= BREAKCHECK_SKIP)
9682 {
9683 breakcheck_count = 0;
9684 ui_breakcheck();
9685 }
9686}
9687
9688/*
9689 * Like line_breakcheck() but check 10 times less often.
9690 */
9691 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009692fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9695 {
9696 breakcheck_count = 0;
9697 ui_breakcheck();
9698 }
9699}
9700
9701/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009702 * Invoke expand_wildcards() for one pattern.
9703 * Expand items like "%:h" before the expansion.
9704 * Returns OK or FAIL.
9705 */
9706 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009707expand_wildcards_eval(
9708 char_u **pat, /* pointer to input pattern */
9709 int *num_file, /* resulting number of files */
9710 char_u ***file, /* array of resulting files */
9711 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009712{
9713 int ret = FAIL;
9714 char_u *eval_pat = NULL;
9715 char_u *exp_pat = *pat;
9716 char_u *ignored_msg;
9717 int usedlen;
9718
9719 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9720 {
9721 ++emsg_off;
9722 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9723 NULL, &ignored_msg, NULL);
9724 --emsg_off;
9725 if (eval_pat != NULL)
9726 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9727 }
9728
9729 if (exp_pat != NULL)
9730 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9731
9732 if (eval_pat != NULL)
9733 {
9734 vim_free(exp_pat);
9735 vim_free(eval_pat);
9736 }
9737
9738 return ret;
9739}
9740
9741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9743 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009744 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 */
9746 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009747expand_wildcards(
9748 int num_pat, /* number of input patterns */
9749 char_u **pat, /* array of input patterns */
9750 int *num_files, /* resulting number of files */
9751 char_u ***files, /* array of resulting files */
9752 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754 int retval;
9755 int i, j;
9756 char_u *p;
9757 int non_suf_match; /* number without matching suffix */
9758
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009759 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760
9761 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009762 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 return retval;
9764
9765#ifdef FEAT_WILDIGN
9766 /*
9767 * Remove names that match 'wildignore'.
9768 */
9769 if (*p_wig)
9770 {
9771 char_u *ffname;
9772
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009773 /* check all files in (*files)[] */
9774 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009776 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 if (ffname == NULL) /* out of memory */
9778 break;
9779# ifdef VMS
9780 vms_remove_version(ffname);
9781# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009782 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009784 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009785 vim_free((*files)[i]);
9786 for (j = i; j + 1 < *num_files; ++j)
9787 (*files)[j] = (*files)[j + 1];
9788 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 --i;
9790 }
9791 vim_free(ffname);
9792 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009793
9794 /* If the number of matches is now zero, we fail. */
9795 if (*num_files == 0)
9796 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009797 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009798 return FAIL;
9799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 }
9801#endif
9802
9803 /*
9804 * Move the names where 'suffixes' match to the end.
9805 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009806 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 {
9808 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009809 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009811 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
9813 /*
9814 * Move the name without matching suffix to the front
9815 * of the list.
9816 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009817 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009819 (*files)[j] = (*files)[j - 1];
9820 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 }
9822 }
9823 }
9824
9825 return retval;
9826}
9827
9828/*
9829 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9830 */
9831 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009832match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 int fnamelen, setsuflen;
9835 char_u *setsuf;
9836#define MAXSUFLEN 30 /* maximum length of a file suffix */
9837 char_u suf_buf[MAXSUFLEN];
9838
9839 fnamelen = (int)STRLEN(fname);
9840 setsuflen = 0;
9841 for (setsuf = p_su; *setsuf; )
9842 {
9843 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009844 if (setsuflen == 0)
9845 {
9846 char_u *tail = gettail(fname);
9847
9848 /* empty entry: match name without a '.' */
9849 if (vim_strchr(tail, '.') == NULL)
9850 {
9851 setsuflen = 1;
9852 break;
9853 }
9854 }
9855 else
9856 {
9857 if (fnamelen >= setsuflen
9858 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9859 (size_t)setsuflen) == 0)
9860 break;
9861 setsuflen = 0;
9862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 }
9864 return (setsuflen != 0);
9865}
9866
9867#if !defined(NO_EXPANDPATH) || defined(PROTO)
9868
9869# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009870static int vim_backtick(char_u *p);
9871static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872# endif
9873
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009874# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875/*
9876 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9877 * it's shared between these systems.
9878 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009879# if defined(PROTO)
9880# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881# else
9882# ifdef __BORLANDC__
9883# define _cdecl _RTLENTRYF
9884# endif
9885# endif
9886
9887/*
9888 * comparison function for qsort in dos_expandpath()
9889 */
9890 static int _cdecl
9891pstrcmp(const void *a, const void *b)
9892{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009893 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894}
9895
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009897 * Recursively expand one path component into all matching files and/or
9898 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 * Return the number of matches found.
9900 * "path" has backslashes before chars that are not to be expanded, starting
9901 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009902 * Return the number of matches found.
9903 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 */
9905 static int
9906dos_expandpath(
9907 garray_T *gap,
9908 char_u *path,
9909 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009910 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009911 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009913 char_u *buf;
9914 char_u *path_end;
9915 char_u *p, *s, *e;
9916 int start_len = gap->ga_len;
9917 char_u *pat;
9918 regmatch_T regmatch;
9919 int starts_with_dot;
9920 int matches;
9921 int len;
9922 int starstar = FALSE;
9923 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 WIN32_FIND_DATA fb;
9925 HANDLE hFind = (HANDLE)0;
9926# ifdef FEAT_MBYTE
9927 WIN32_FIND_DATAW wfb;
9928 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009931 int ok;
9932
9933 /* Expanding "**" may take a long time, check for CTRL-C. */
9934 if (stardepth > 0)
9935 {
9936 ui_breakcheck();
9937 if (got_int)
9938 return 0;
9939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009941 /* Make room for file name. When doing encoding conversion the actual
9942 * length may be quite a bit longer, thus use the maximum possible length. */
9943 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 if (buf == NULL)
9945 return 0;
9946
9947 /*
9948 * Find the first part in the path name that contains a wildcard or a ~1.
9949 * Copy it into buf, including the preceding characters.
9950 */
9951 p = buf;
9952 s = buf;
9953 e = NULL;
9954 path_end = path;
9955 while (*path_end != NUL)
9956 {
9957 /* May ignore a wildcard that has a backslash before it; it will
9958 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9959 if (path_end >= path + wildoff && rem_backslash(path_end))
9960 *p++ = *path_end++;
9961 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9962 {
9963 if (e != NULL)
9964 break;
9965 s = p + 1;
9966 }
9967 else if (path_end >= path + wildoff
9968 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9969 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009970# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 if (has_mbyte)
9972 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009973 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 STRNCPY(p, path_end, len);
9975 p += len;
9976 path_end += len;
9977 }
9978 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 *p++ = *path_end++;
9981 }
9982 e = p;
9983 *e = NUL;
9984
9985 /* now we have one wildcard component between s and e */
9986 /* Remove backslashes between "wildoff" and the start of the wildcard
9987 * component. */
9988 for (p = buf + wildoff; p < s; ++p)
9989 if (rem_backslash(p))
9990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009991 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 --e;
9993 --s;
9994 }
9995
Bram Moolenaar231334e2005-07-25 20:46:57 +00009996 /* Check for "**" between "s" and "e". */
9997 for (p = s; p < e; ++p)
9998 if (p[0] == '*' && p[1] == '*')
9999 starstar = TRUE;
10000
Bram Moolenaard82103e2016-01-17 17:04:05 +010010001 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10003 if (pat == NULL)
10004 {
10005 vim_free(buf);
10006 return 0;
10007 }
10008
10009 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010010 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010011 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 regmatch.rm_ic = TRUE; /* Always ignore case */
10013 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010014 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010015 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 vim_free(pat);
10017
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010018 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 {
10020 vim_free(buf);
10021 return 0;
10022 }
10023
10024 /* remember the pattern or file name being looked for */
10025 matchname = vim_strsave(s);
10026
Bram Moolenaar231334e2005-07-25 20:46:57 +000010027 /* If "**" is by itself, this is the first time we encounter it and more
10028 * is following then find matches without any directory. */
10029 if (!didstar && stardepth < 100 && starstar && e - s == 2
10030 && *path_end == '/')
10031 {
10032 STRCPY(s, path_end + 1);
10033 ++stardepth;
10034 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10035 --stardepth;
10036 }
10037
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 /* Scan all files in the directory with "dir/ *.*" */
10039 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040# ifdef FEAT_MBYTE
10041 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10042 {
10043 /* The active codepage differs from 'encoding'. Attempt using the
10044 * wide function. If it fails because it is not implemented fall back
10045 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010046 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 if (wn != NULL)
10048 {
10049 hFind = FindFirstFileW(wn, &wfb);
10050 if (hFind == INVALID_HANDLE_VALUE
10051 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010052 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054 }
10055
10056 if (wn == NULL)
10057# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010058 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
10061 while (ok)
10062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063# ifdef FEAT_MBYTE
10064 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010065 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 else
10067# endif
10068 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 /* Ignore entries starting with a dot, unless when asked for. Accept
10070 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010071 if ((p[0] != '.' || starts_with_dot
10072 || ((flags & EW_DODOT)
10073 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010075 || (regmatch.regprog != NULL
10076 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010077 || ((flags & EW_NOTWILD)
10078 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010082
10083 if (starstar && stardepth < 100)
10084 {
10085 /* For "**" in the pattern first go deeper in the tree to
10086 * find matches. */
10087 STRCPY(buf + len, "/**");
10088 STRCPY(buf + len + 3, path_end);
10089 ++stardepth;
10090 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10091 --stardepth;
10092 }
10093
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 STRCPY(buf + len, path_end);
10095 if (mch_has_exp_wildcard(path_end))
10096 {
10097 /* need to expand another component of the path */
10098 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010099 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 }
10101 else
10102 {
10103 /* no more wildcards, check if there is a match */
10104 /* remove backslashes for the remaining components only */
10105 if (*path_end != 0)
10106 backslash_halve(buf + len + 1);
10107 if (mch_getperm(buf) >= 0) /* add existing file */
10108 addfile(gap, buf, flags);
10109 }
10110 }
10111
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112# ifdef FEAT_MBYTE
10113 if (wn != NULL)
10114 {
10115 vim_free(p);
10116 ok = FindNextFileW(hFind, &wfb);
10117 }
10118 else
10119# endif
10120 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
10122 /* If no more matches and no match was used, try expanding the name
10123 * itself. Finds the long name of a short filename. */
10124 if (!ok && matchname != NULL && gap->ga_len == start_len)
10125 {
10126 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 FindClose(hFind);
10128# ifdef FEAT_MBYTE
10129 if (wn != NULL)
10130 {
10131 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010132 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 if (wn != NULL)
10134 hFind = FindFirstFileW(wn, &wfb);
10135 }
10136 if (wn == NULL)
10137# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010138 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010140 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 }
10142 }
10143
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 FindClose(hFind);
10145# ifdef FEAT_MBYTE
10146 vim_free(wn);
10147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010149 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 vim_free(matchname);
10151
10152 matches = gap->ga_len - start_len;
10153 if (matches > 0)
10154 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10155 sizeof(char_u *), pstrcmp);
10156 return matches;
10157}
10158
10159 int
10160mch_expandpath(
10161 garray_T *gap,
10162 char_u *path,
10163 int flags) /* EW_* flags */
10164{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010165 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010167# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168
Bram Moolenaar231334e2005-07-25 20:46:57 +000010169#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10170 || defined(PROTO)
10171/*
10172 * Unix style wildcard expansion code.
10173 * It's here because it's used both for Unix and Mac.
10174 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010175static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010176
10177 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010178pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010179{
10180 return (pathcmp(*(char **)a, *(char **)b, -1));
10181}
10182
10183/*
10184 * Recursively expand one path component into all matching files and/or
10185 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10186 * "path" has backslashes before chars that are not to be expanded, starting
10187 * at "path + wildoff".
10188 * Return the number of matches found.
10189 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10190 */
10191 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010192unix_expandpath(
10193 garray_T *gap,
10194 char_u *path,
10195 int wildoff,
10196 int flags, /* EW_* flags */
10197 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010198{
10199 char_u *buf;
10200 char_u *path_end;
10201 char_u *p, *s, *e;
10202 int start_len = gap->ga_len;
10203 char_u *pat;
10204 regmatch_T regmatch;
10205 int starts_with_dot;
10206 int matches;
10207 int len;
10208 int starstar = FALSE;
10209 static int stardepth = 0; /* depth for "**" expansion */
10210
10211 DIR *dirp;
10212 struct dirent *dp;
10213
10214 /* Expanding "**" may take a long time, check for CTRL-C. */
10215 if (stardepth > 0)
10216 {
10217 ui_breakcheck();
10218 if (got_int)
10219 return 0;
10220 }
10221
10222 /* make room for file name */
10223 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10224 if (buf == NULL)
10225 return 0;
10226
10227 /*
10228 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010229 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010230 * Copy it into "buf", including the preceding characters.
10231 */
10232 p = buf;
10233 s = buf;
10234 e = NULL;
10235 path_end = path;
10236 while (*path_end != NUL)
10237 {
10238 /* May ignore a wildcard that has a backslash before it; it will
10239 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10240 if (path_end >= path + wildoff && rem_backslash(path_end))
10241 *p++ = *path_end++;
10242 else if (*path_end == '/')
10243 {
10244 if (e != NULL)
10245 break;
10246 s = p + 1;
10247 }
10248 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010249 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010250 || (!p_fic && (flags & EW_ICASE)
10251 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010252 e = p;
10253#ifdef FEAT_MBYTE
10254 if (has_mbyte)
10255 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010256 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010257 STRNCPY(p, path_end, len);
10258 p += len;
10259 path_end += len;
10260 }
10261 else
10262#endif
10263 *p++ = *path_end++;
10264 }
10265 e = p;
10266 *e = NUL;
10267
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010268 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010269 /* Remove backslashes between "wildoff" and the start of the wildcard
10270 * component. */
10271 for (p = buf + wildoff; p < s; ++p)
10272 if (rem_backslash(p))
10273 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010274 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010275 --e;
10276 --s;
10277 }
10278
10279 /* Check for "**" between "s" and "e". */
10280 for (p = s; p < e; ++p)
10281 if (p[0] == '*' && p[1] == '*')
10282 starstar = TRUE;
10283
10284 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010285 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010286 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10287 if (pat == NULL)
10288 {
10289 vim_free(buf);
10290 return 0;
10291 }
10292
10293 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010294 if (flags & EW_ICASE)
10295 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10296 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010297 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010298 if (flags & (EW_NOERROR | EW_NOTWILD))
10299 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010300 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010301 if (flags & (EW_NOERROR | EW_NOTWILD))
10302 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010303 vim_free(pat);
10304
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010305 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010306 {
10307 vim_free(buf);
10308 return 0;
10309 }
10310
10311 /* If "**" is by itself, this is the first time we encounter it and more
10312 * is following then find matches without any directory. */
10313 if (!didstar && stardepth < 100 && starstar && e - s == 2
10314 && *path_end == '/')
10315 {
10316 STRCPY(s, path_end + 1);
10317 ++stardepth;
10318 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10319 --stardepth;
10320 }
10321
10322 /* open the directory for scanning */
10323 *s = NUL;
10324 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10325
10326 /* Find all matching entries */
10327 if (dirp != NULL)
10328 {
10329 for (;;)
10330 {
10331 dp = readdir(dirp);
10332 if (dp == NULL)
10333 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010334 if ((dp->d_name[0] != '.' || starts_with_dot
10335 || ((flags & EW_DODOT)
10336 && dp->d_name[1] != NUL
10337 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010338 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10339 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010340 || ((flags & EW_NOTWILD)
10341 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010342 {
10343 STRCPY(s, dp->d_name);
10344 len = STRLEN(buf);
10345
10346 if (starstar && stardepth < 100)
10347 {
10348 /* For "**" in the pattern first go deeper in the tree to
10349 * find matches. */
10350 STRCPY(buf + len, "/**");
10351 STRCPY(buf + len + 3, path_end);
10352 ++stardepth;
10353 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10354 --stardepth;
10355 }
10356
10357 STRCPY(buf + len, path_end);
10358 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10359 {
10360 /* need to expand another component of the path */
10361 /* remove backslashes for the remaining components only */
10362 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10363 }
10364 else
10365 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010366 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010367
Bram Moolenaar231334e2005-07-25 20:46:57 +000010368 /* no more wildcards, check if there is a match */
10369 /* remove backslashes for the remaining components only */
10370 if (*path_end != NUL)
10371 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010372 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010373 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010374 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010375 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010376#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010377 size_t precomp_len = STRLEN(buf)+1;
10378 char_u *precomp_buf =
10379 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010380
Bram Moolenaar231334e2005-07-25 20:46:57 +000010381 if (precomp_buf)
10382 {
10383 mch_memmove(buf, precomp_buf, precomp_len);
10384 vim_free(precomp_buf);
10385 }
10386#endif
10387 addfile(gap, buf, flags);
10388 }
10389 }
10390 }
10391 }
10392
10393 closedir(dirp);
10394 }
10395
10396 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010397 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010398
10399 matches = gap->ga_len - start_len;
10400 if (matches > 0)
10401 qsort(((char_u **)gap->ga_data) + start_len, matches,
10402 sizeof(char_u *), pstrcmp);
10403 return matches;
10404}
10405#endif
10406
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010407#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010408static int find_previous_pathsep(char_u *path, char_u **psep);
10409static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10410static void expand_path_option(char_u *curdir, garray_T *gap);
10411static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10412static void uniquefy_paths(garray_T *gap, char_u *pattern);
10413static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010414
10415/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010416 * Moves "*psep" back to the previous path separator in "path".
10417 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010418 */
10419 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010420find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010421{
10422 /* skip the current separator */
10423 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010424 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010425
10426 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010427 while (*psep > path)
10428 {
10429 if (vim_ispathsep(**psep))
10430 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010431 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010432 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010433
10434 return FAIL;
10435}
10436
10437/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010438 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10439 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010440 */
10441 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010442is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010443{
10444 int j;
10445 int candidate_len;
10446 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010447 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010448 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010449
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010450 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010451 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010452 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010453 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010454
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010455 candidate_len = (int)STRLEN(maybe_unique);
10456 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010457 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010458 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010459
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010460 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010461 if (fnamecmp(maybe_unique, rival) == 0
10462 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010463 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010464 }
10465
Bram Moolenaar162bd912010-07-28 22:29:10 +020010466 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010467}
10468
10469/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010470 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010471 * paths are expanded to their equivalent fullpath. This includes the "."
10472 * (relative to current buffer directory) and empty path (relative to current
10473 * directory) notations.
10474 *
10475 * TODO: handle upward search (;) and path limiter (**N) notations by
10476 * expanding each into their equivalent path(s).
10477 */
10478 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010479expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010480{
10481 char_u *path_option = *curbuf->b_p_path == NUL
10482 ? p_path : curbuf->b_p_path;
10483 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010484 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010485 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010486
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010487 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010488 return;
10489
10490 while (*path_option != NUL)
10491 {
10492 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10493
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010494 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010495 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010496 /* Relative to current buffer:
10497 * "/path/file" + "." -> "/path/"
10498 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010499 if (curbuf->b_ffname == NULL)
10500 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010501 p = gettail(curbuf->b_ffname);
10502 len = (int)(p - curbuf->b_ffname);
10503 if (len + (int)STRLEN(buf) >= MAXPATHL)
10504 continue;
10505 if (buf[1] == NUL)
10506 buf[len] = NUL;
10507 else
10508 STRMOVE(buf + len, buf + 2);
10509 mch_memmove(buf, curbuf->b_ffname, len);
10510 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010511 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010512 else if (buf[0] == NUL)
10513 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010514 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010515 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010516 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010517 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010518 else if (!mch_isFullName(buf))
10519 {
10520 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010521 len = (int)STRLEN(curdir);
10522 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010523 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010524 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010525 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010526 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010527 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010528 }
10529
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010530 if (ga_grow(gap, 1) == FAIL)
10531 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010532
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010533# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010534 /* Avoid the path ending in a backslash, it fails when a comma is
10535 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010536 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010537 if (buf[len - 1] == '\\')
10538 buf[len - 1] = '/';
10539# endif
10540
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010541 p = vim_strsave(buf);
10542 if (p == NULL)
10543 break;
10544 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010545 }
10546
10547 vim_free(buf);
10548}
10549
10550/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010551 * Returns a pointer to the file or directory name in "fname" that matches the
10552 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010553 *
10554 * path: /foo/bar/baz
10555 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010556 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010557 */
10558 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010559get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010560{
10561 int i;
10562 int maxlen = 0;
10563 char_u **path_part = (char_u **)gap->ga_data;
10564 char_u *cutoff = NULL;
10565
10566 for (i = 0; i < gap->ga_len; i++)
10567 {
10568 int j = 0;
10569
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010570 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010571# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010572 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10573#endif
10574 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010575 j++;
10576 if (j > maxlen)
10577 {
10578 maxlen = j;
10579 cutoff = &fname[j];
10580 }
10581 }
10582
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010583 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010584 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010585 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010586 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010587
10588 return cutoff;
10589}
10590
10591/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010592 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10593 * that they are unique with respect to each other while conserving the part
10594 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 */
10596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010597uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010598{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010599 int i;
10600 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010601 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010602 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010603 char_u *pat;
10604 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010605 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010606 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010607 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010608 char_u **in_curdir = NULL;
10609 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010610
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010611 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010612 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010613
10614 /*
10615 * We need to prepend a '*' at the beginning of file_pattern so that the
10616 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010617 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010618 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010619 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010620 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010621 if (file_pattern == NULL)
10622 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010623 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010624 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010625 STRCAT(file_pattern, pattern);
10626 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10627 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010628 if (pat == NULL)
10629 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010630
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010631 regmatch.rm_ic = TRUE; /* always ignore case */
10632 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10633 vim_free(pat);
10634 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010635 return;
10636
Bram Moolenaar162bd912010-07-28 22:29:10 +020010637 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010638 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010639 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010641
10642 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010643 if (in_curdir == NULL)
10644 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010645
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010646 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010647 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010648 char_u *path = fnames[i];
10649 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010650 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010651 char_u *pathsep_p;
10652 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010653
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010654 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010655 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010656 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010657 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010658 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010659
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010660 /* Shorten the filename while maintaining its uniqueness */
10661 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010662
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010663 /* Don't assume all files can be reached without path when search
10664 * pattern starts with star star slash, so only remove path_cutoff
10665 * when possible. */
10666 if (pattern[0] == '*' && pattern[1] == '*'
10667 && vim_ispathsep_nocolon(pattern[2])
10668 && path_cutoff != NULL
10669 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10670 && is_unique(path_cutoff, gap, i))
10671 {
10672 sort_again = TRUE;
10673 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10674 }
10675 else
10676 {
10677 /* Here all files can be reached without path, so get shortest
10678 * unique path. We start at the end of the path. */
10679 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010680
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010681 while (find_previous_pathsep(path, &pathsep_p))
10682 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10683 && is_unique(pathsep_p + 1, gap, i)
10684 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10685 {
10686 sort_again = TRUE;
10687 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10688 break;
10689 }
10690 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010691
10692 if (mch_isFullName(path))
10693 {
10694 /*
10695 * Last resort: shorten relative to curdir if possible.
10696 * 'possible' means:
10697 * 1. It is under the current directory.
10698 * 2. The result is actually shorter than the original.
10699 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010700 * Before curdir After
10701 * /foo/bar/file.txt /foo/bar ./file.txt
10702 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10703 * /file.txt / /file.txt
10704 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010705 */
10706 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010707 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010708#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010709 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010710 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010711 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010712 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010713 && !vim_ispathsep(*short_name)
10714#endif
10715 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010716 {
10717 STRCPY(path, ".");
10718 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010719 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010720 }
10721 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010722 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010723 }
10724
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010725 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010726 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010727 {
10728 char_u *rel_path;
10729 char_u *path = in_curdir[i];
10730
10731 if (path == NULL)
10732 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010733
10734 /* If the {filename} is not unique, change it to ./{filename}.
10735 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010736 short_name = shorten_fname(path, curdir);
10737 if (short_name == NULL)
10738 short_name = path;
10739 if (is_unique(short_name, gap, i))
10740 {
10741 STRCPY(fnames[i], short_name);
10742 continue;
10743 }
10744
10745 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10746 if (rel_path == NULL)
10747 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010748 STRCPY(rel_path, ".");
10749 add_pathsep(rel_path);
10750 STRCAT(rel_path, short_name);
10751
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010752 vim_free(fnames[i]);
10753 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010754 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010755 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010756 }
10757
Bram Moolenaar162bd912010-07-28 22:29:10 +020010758theend:
10759 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010760 if (in_curdir != NULL)
10761 {
10762 for (i = 0; i < gap->ga_len; i++)
10763 vim_free(in_curdir[i]);
10764 vim_free(in_curdir);
10765 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010766 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010767 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010768
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010769 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010770 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010771}
10772
10773/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010774 * Calls globpath() with 'path' values for the given pattern and stores the
10775 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010776 * Returns the total number of matches.
10777 */
10778 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010779expand_in_path(
10780 garray_T *gap,
10781 char_u *pattern,
10782 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010783{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010784 char_u *curdir;
10785 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010786 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010787 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010788
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010789 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010790 return 0;
10791 mch_dirname(curdir, MAXPATHL);
10792
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010793 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010794 expand_path_option(curdir, &path_ga);
10795 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010796 if (path_ga.ga_len == 0)
10797 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010798
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010799 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010800 ga_clear_strings(&path_ga);
10801 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010802 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010803
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010804 if (flags & EW_ICASE)
10805 glob_flags |= WILD_ICASE;
10806 if (flags & EW_ADDSLASH)
10807 glob_flags |= WILD_ADD_SLASH;
10808 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010809 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010810
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010811 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010812}
10813#endif
10814
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010815#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10816/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010817 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10818 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010819 */
10820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010821remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010822{
10823 int i;
10824 int j;
10825 char_u **fnames = (char_u **)gap->ga_data;
10826
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010827 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010828 for (i = gap->ga_len - 1; i > 0; --i)
10829 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10830 {
10831 vim_free(fnames[i]);
10832 for (j = i + 1; j < gap->ga_len; ++j)
10833 fnames[j - 1] = fnames[j];
10834 --gap->ga_len;
10835 }
10836}
10837#endif
10838
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010839static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010840
10841/*
10842 * Return TRUE if "p" contains what looks like an environment variable.
10843 * Allowing for escaping.
10844 */
10845 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010846has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010847{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010848 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010849 {
10850 if (*p == '\\' && p[1] != NUL)
10851 ++p;
10852 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010853#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010854 "$%"
10855#else
10856 "$"
10857#endif
10858 , *p) != NULL)
10859 return TRUE;
10860 }
10861 return FALSE;
10862}
10863
10864#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010865static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010866
10867/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010868 * Return TRUE if "p" contains a special wildcard character, one that Vim
10869 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010870 */
10871 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010872has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010873{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010874 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010875 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010876 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010877 if (*p == '\\' && p[1] != NUL)
10878 ++p;
10879 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10880 return TRUE;
10881 }
10882 return FALSE;
10883}
10884#endif
10885
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886/*
10887 * Generic wildcard expansion code.
10888 *
10889 * Characters in "pat" that should not be expanded must be preceded with a
10890 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10891 *
10892 * Return FAIL when no single file was found. In this case "num_file" is not
10893 * set, and "file" may contain an error message.
10894 * Return OK when some files found. "num_file" is set to the number of
10895 * matches, "file" to the array of matches. Call FreeWild() later.
10896 */
10897 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010898gen_expand_wildcards(
10899 int num_pat, /* number of input patterns */
10900 char_u **pat, /* array of input patterns */
10901 int *num_file, /* resulting number of files */
10902 char_u ***file, /* array of resulting files */
10903 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904{
10905 int i;
10906 garray_T ga;
10907 char_u *p;
10908 static int recursive = FALSE;
10909 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010910 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010911#if defined(FEAT_SEARCHPATH)
10912 int did_expand_in_path = FALSE;
10913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914
10915 /*
10916 * expand_env() is called to expand things like "~user". If this fails,
10917 * it calls ExpandOne(), which brings us back here. In this case, always
10918 * call the machine specific expansion function, if possible. Otherwise,
10919 * return FAIL.
10920 */
10921 if (recursive)
10922#ifdef SPECIAL_WILDCHAR
10923 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10924#else
10925 return FAIL;
10926#endif
10927
10928#ifdef SPECIAL_WILDCHAR
10929 /*
10930 * If there are any special wildcard characters which we cannot handle
10931 * here, call machine specific function for all the expansion. This
10932 * avoids starting the shell for each argument separately.
10933 * For `=expr` do use the internal function.
10934 */
10935 for (i = 0; i < num_pat; i++)
10936 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010937 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938# ifdef VIM_BACKTICK
10939 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10940# endif
10941 )
10942 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10943 }
10944#endif
10945
10946 recursive = TRUE;
10947
10948 /*
10949 * The matching file names are stored in a growarray. Init it empty.
10950 */
10951 ga_init2(&ga, (int)sizeof(char_u *), 30);
10952
10953 for (i = 0; i < num_pat; ++i)
10954 {
10955 add_pat = -1;
10956 p = pat[i];
10957
10958#ifdef VIM_BACKTICK
10959 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010962 if (add_pat == -1)
10963 retval = FAIL;
10964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 else
10966#endif
10967 {
10968 /*
10969 * First expand environment variables, "~/" and "~user/".
10970 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010971 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010973 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 if (p == NULL)
10975 p = pat[i];
10976#ifdef UNIX
10977 /*
10978 * On Unix, if expand_env() can't expand an environment
10979 * variable, use the shell to do that. Discard previously
10980 * found file names and start all over again.
10981 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010982 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 {
10984 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010985 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010987 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 recursive = FALSE;
10989 return i;
10990 }
10991#endif
10992 }
10993
10994 /*
10995 * If there are wildcards: Expand file names and add each match to
10996 * the list. If there is no match, and EW_NOTFOUND is given, add
10997 * the pattern.
10998 * If there are no wildcards: Add the file name if it exists or
10999 * when EW_NOTFOUND is given.
11000 */
11001 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011002 {
11003#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011004 if ((flags & EW_PATH)
11005 && !mch_isFullName(p)
11006 && !(p[0] == '.'
11007 && (vim_ispathsep(p[1])
11008 || (p[1] == '.' && vim_ispathsep(p[2]))))
11009 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011010 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011011 /* :find completion where 'path' is used.
11012 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011013 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011014 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011015 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011016 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011017 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011018 else
11019#endif
11020 add_pat = mch_expandpath(&ga, p, flags);
11021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 }
11023
11024 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11025 {
11026 char_u *t = backslash_halve_save(p);
11027
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11029 * "vim c:/" work. */
11030 if (flags & EW_NOTFOUND)
11031 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 addfile(&ga, t, flags);
11034 vim_free(t);
11035 }
11036
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011037#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011038 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011039 uniquefy_paths(&ga, p);
11040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 if (p != pat[i])
11042 vim_free(p);
11043 }
11044
11045 *num_file = ga.ga_len;
11046 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11047
11048 recursive = FALSE;
11049
Bram Moolenaar336bd622016-01-17 18:23:58 +010011050 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051}
11052
11053# ifdef VIM_BACKTICK
11054
11055/*
11056 * Return TRUE if we can expand this backtick thing here.
11057 */
11058 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011059vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11062}
11063
11064/*
11065 * Expand an item in `backticks` by executing it as a command.
11066 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011067 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 */
11069 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011070expand_backtick(
11071 garray_T *gap,
11072 char_u *pat,
11073 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
11075 char_u *p;
11076 char_u *cmd;
11077 char_u *buffer;
11078 int cnt = 0;
11079 int i;
11080
11081 /* Create the command: lop off the backticks. */
11082 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11083 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011084 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085
11086#ifdef FEAT_EVAL
11087 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011088 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 else
11090#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011091 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011092 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 vim_free(cmd);
11094 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011095 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096
11097 cmd = buffer;
11098 while (*cmd != NUL)
11099 {
11100 cmd = skipwhite(cmd); /* skip over white space */
11101 p = cmd;
11102 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11103 ++p;
11104 /* add an entry if it is not empty */
11105 if (p > cmd)
11106 {
11107 i = *p;
11108 *p = NUL;
11109 addfile(gap, cmd, flags);
11110 *p = i;
11111 ++cnt;
11112 }
11113 cmd = p;
11114 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11115 ++cmd;
11116 }
11117
11118 vim_free(buffer);
11119 return cnt;
11120}
11121# endif /* VIM_BACKTICK */
11122
11123/*
11124 * Add a file to a file list. Accepted flags:
11125 * EW_DIR add directories
11126 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011127 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 * EW_NOTFOUND add even when it doesn't exist
11129 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011130 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 */
11132 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011133addfile(
11134 garray_T *gap,
11135 char_u *f, /* filename */
11136 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138 char_u *p;
11139 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011140 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011142 /* if the file/dir/link doesn't exist, may not add it */
11143 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011144 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 return;
11146
11147#ifdef FNAME_ILLEGAL
11148 /* if the file/dir contains illegal characters, don't add it */
11149 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11150 return;
11151#endif
11152
11153 isdir = mch_isdir(f);
11154 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11155 return;
11156
Bram Moolenaarb5971142015-03-21 17:32:19 +010011157 /* If the file isn't executable, may not add it. Do accept directories.
11158 * When invoked from expand_shellcmd() do not use $PATH. */
11159 if (!isdir && (flags & EW_EXEC)
11160 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011161 return;
11162
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 /* Make room for another item in the file list. */
11164 if (ga_grow(gap, 1) == FAIL)
11165 return;
11166
11167 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11168 if (p == NULL)
11169 return;
11170
11171 STRCPY(p, f);
11172#ifdef BACKSLASH_IN_FILENAME
11173 slash_adjust(p);
11174#endif
11175 /*
11176 * Append a slash or backslash after directory names if none is present.
11177 */
11178#ifndef DONT_ADD_PATHSEP_TO_DIR
11179 if (isdir && (flags & EW_ADDSLASH))
11180 add_pathsep(p);
11181#endif
11182 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183}
11184#endif /* !NO_EXPANDPATH */
11185
11186#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11187
11188#ifndef SEEK_SET
11189# define SEEK_SET 0
11190#endif
11191#ifndef SEEK_END
11192# define SEEK_END 2
11193#endif
11194
11195/*
11196 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011197 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11198 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 * Returns an allocated string, or NULL for error.
11200 */
11201 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011202get_cmd_output(
11203 char_u *cmd,
11204 char_u *infile, /* optional input file name */
11205 int flags, /* can be SHELL_SILENT */
11206 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
11208 char_u *tempname;
11209 char_u *command;
11210 char_u *buffer = NULL;
11211 int len;
11212 int i = 0;
11213 FILE *fd;
11214
11215 if (check_restricted() || check_secure())
11216 return NULL;
11217
11218 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011219 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 {
11221 EMSG(_(e_notmp));
11222 return NULL;
11223 }
11224
11225 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011226 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 if (command == NULL)
11228 goto done;
11229
11230 /*
11231 * Call the shell to execute the command (errors are ignored).
11232 * Don't check timestamps here.
11233 */
11234 ++no_check_timestamps;
11235 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11236 --no_check_timestamps;
11237
11238 vim_free(command);
11239
11240 /*
11241 * read the names from the file into memory
11242 */
11243# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011244 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 fd = mch_fopen((char *)tempname, "r");
11246# else
11247 fd = mch_fopen((char *)tempname, READBIN);
11248# endif
11249
11250 if (fd == NULL)
11251 {
11252 EMSG2(_(e_notopen), tempname);
11253 goto done;
11254 }
11255
11256 fseek(fd, 0L, SEEK_END);
11257 len = ftell(fd); /* get size of temp file */
11258 fseek(fd, 0L, SEEK_SET);
11259
11260 buffer = alloc(len + 1);
11261 if (buffer != NULL)
11262 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11263 fclose(fd);
11264 mch_remove(tempname);
11265 if (buffer == NULL)
11266 goto done;
11267#ifdef VMS
11268 len = i; /* VMS doesn't give us what we asked for... */
11269#endif
11270 if (i != len)
11271 {
11272 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011273 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011275 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011276 {
11277 /* Change NUL into SOH, otherwise the string is truncated. */
11278 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011279 if (buffer[i] == NUL)
11280 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011281
Bram Moolenaar162bd912010-07-28 22:29:10 +020011282 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011283 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011284 else
11285 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286
11287done:
11288 vim_free(tempname);
11289 return buffer;
11290}
11291#endif
11292
11293/*
11294 * Free the list of files returned by expand_wildcards() or other expansion
11295 * functions.
11296 */
11297 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011298FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011300 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 while (count--)
11303 vim_free(files[count]);
11304 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305}
11306
11307/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011308 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 * Don't do this when still processing a command or a mapping.
11310 * Don't do this when inside a ":normal" command.
11311 */
11312 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011313goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314{
11315 return (p_im && stuff_empty() && typebuf_typed());
11316}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011317
11318/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011319 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011320 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11321 * - Remove any argument. E.g., "csh -f" -> "csh".
11322 * But don't allow a space in the path, so that this works:
11323 * "/usr/bin/csh --rcfile ~/.cshrc"
11324 * But don't do that for Windows, it's common to have a space in the path.
11325 */
11326 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011327get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011328{
11329 char_u *p;
11330
11331#ifdef WIN3264
11332 p = gettail(p_sh);
11333 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11334#else
11335 p = skiptowhite(p_sh);
11336 if (*p == NUL)
11337 {
11338 /* No white space, use the tail. */
11339 p = vim_strsave(gettail(p_sh));
11340 }
11341 else
11342 {
11343 char_u *p1, *p2;
11344
11345 /* Find the last path separator before the space. */
11346 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011347 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011348 if (vim_ispathsep(*p2))
11349 p1 = p2 + 1;
11350 p = vim_strnsave(p1, (int)(p - p1));
11351 }
11352#endif
11353 return p;
11354}